2 * Copyright (c) 1999-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1999-2000 Apple Computer, Inc. All rights reserved.
36 #include <machine/machine_routines.h>
37 #include <pexpert/pexpert.h>
40 #include <machine/machine_routines.h>
42 #include <IOKit/IOLib.h>
43 #include <IOKit/IOPlatformExpert.h>
44 #include <IOKit/pwr_mgt/RootDomain.h>
45 #include <IOKit/IOUserClient.h>
46 #include <IOKit/IOKitKeysPrivate.h>
47 #include <IOKit/IOCPU.h>
49 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
50 #include <kern/queue.h>
52 typedef kern_return_t (*iocpu_platform_action_t
)(void * refcon0
, void * refcon1
, uint32_t priority
,
53 void * param1
, void * param2
, void * param3
);
55 struct iocpu_platform_action_entry
58 iocpu_platform_action_t action
;
62 struct iocpu_platform_action_entry
* alloc_list
;
64 typedef struct iocpu_platform_action_entry iocpu_platform_action_entry_t
;
67 iocpu_get_platform_quiesce_queue(void);
70 iocpu_get_platform_active_queue(void);
73 iocpu_platform_cpu_action_init(queue_head_t
* quiesce_queue
, queue_head_t
* init_queue
);
76 iocpu_add_platform_action(queue_head_t
* queue
, iocpu_platform_action_entry_t
* entry
);
79 iocpu_remove_platform_action(iocpu_platform_action_entry_t
* entry
);
82 iocpu_run_platform_actions(queue_head_t
* queue
, uint32_t first_priority
, uint32_t last_priority
,
83 void * param1
, void * param2
, void * param3
);
85 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
87 #define kBootCPUNumber 0
89 static iocpu_platform_action_entry_t
* gIOAllActionsQueue
;
90 static queue_head_t gIOSleepActionQueue
;
91 static queue_head_t gIOWakeActionQueue
;
93 static queue_head_t iocpu_quiesce_queue
;
94 static queue_head_t iocpu_active_queue
;
96 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
99 iocpu_platform_cpu_action_init(queue_head_t
* quiesce_queue
, __unused queue_head_t
* init_queue
)
102 enum { kNumQuiesceActions
= 2 };
103 static iocpu_platform_action_entry_t quiesce_actions
[kNumQuiesceActions
] =
105 { { NULL
, NULL
}, (iocpu_platform_action_t
) &clean_mmu_dcache
, 97000, 0, 0, NULL
},
106 { { NULL
, NULL
}, (iocpu_platform_action_t
) &arm_sleep
, 99000, 0, 0, NULL
},
110 for (idx
= 0; idx
< kNumQuiesceActions
; idx
++)
111 iocpu_add_platform_action(quiesce_queue
, &quiesce_actions
[idx
]);
115 queue_head_t
* iocpu_get_platform_quiesce_queue(void)
117 if (!iocpu_quiesce_queue
.next
)
119 queue_init(&iocpu_quiesce_queue
);
120 queue_init(&iocpu_active_queue
);
121 iocpu_platform_cpu_action_init(&iocpu_quiesce_queue
, &iocpu_active_queue
);
123 return (&iocpu_quiesce_queue
);
126 queue_head_t
* iocpu_get_platform_active_queue(void)
128 return (&iocpu_active_queue
);
131 void iocpu_add_platform_action(queue_head_t
* queue
, iocpu_platform_action_entry_t
* entry
)
133 iocpu_platform_action_entry_t
* next
;
135 queue_iterate(queue
, next
, iocpu_platform_action_entry_t
*, link
)
137 if (next
->priority
> entry
->priority
)
139 queue_insert_before(queue
, entry
, next
, iocpu_platform_action_entry_t
*, link
);
143 queue_enter(queue
, entry
, iocpu_platform_action_entry_t
*, link
); // at tail
146 void iocpu_remove_platform_action(iocpu_platform_action_entry_t
* entry
)
148 remque(&entry
->link
);
152 iocpu_run_platform_actions(queue_head_t
* queue
, uint32_t first_priority
, uint32_t last_priority
,
153 void * param1
, void * param2
, void * param3
)
155 kern_return_t ret
= KERN_SUCCESS
;
156 kern_return_t result
= KERN_SUCCESS
;
157 iocpu_platform_action_entry_t
* next
;
159 queue_iterate(queue
, next
, iocpu_platform_action_entry_t
*, link
)
161 uint32_t pri
= (next
->priority
< 0) ? -next
->priority
: next
->priority
;
162 if ((pri
>= first_priority
) && (pri
<= last_priority
))
164 //kprintf("[%p]", next->action);
165 ret
= (*next
->action
)(next
->refcon0
, next
->refcon1
, pri
, param1
, param2
, param3
);
167 if (KERN_SUCCESS
== result
)
173 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
175 extern "C" kern_return_t
176 IOCPURunPlatformQuiesceActions(void)
178 return (iocpu_run_platform_actions(iocpu_get_platform_quiesce_queue(), 0, 0UL-1,
182 extern "C" kern_return_t
183 IOCPURunPlatformActiveActions(void)
185 return (iocpu_run_platform_actions(iocpu_get_platform_active_queue(), 0, 0UL-1,
190 IOServicePlatformAction(void * refcon0
, void * refcon1
, uint32_t priority
,
191 void * param1
, void * param2
, void * param3
)
194 IOService
* service
= (IOService
*) refcon0
;
195 const OSSymbol
* function
= (const OSSymbol
*) refcon1
;
197 kprintf("%s -> %s\n", function
->getCStringNoCopy(), service
->getName());
199 ret
= service
->callPlatformFunction(function
, false,
200 (void *) priority
, param1
, param2
, param3
);
206 IOInstallServicePlatformAction(IOService
* service
,
207 const OSSymbol
* key
, queue_head_t
* queue
,
211 iocpu_platform_action_entry_t
* entry
;
214 num
= OSDynamicCast(OSNumber
, service
->getProperty(key
));
218 entry
= IONew(iocpu_platform_action_entry_t
, 1);
219 entry
->action
= &IOServicePlatformAction
;
220 priority
= num
->unsigned32BitValue();
222 entry
->priority
= -priority
;
224 entry
->priority
= priority
;
225 entry
->refcon0
= service
;
226 entry
->refcon1
= (void *) key
;
228 iocpu_add_platform_action(queue
, entry
);
229 entry
->alloc_list
= gIOAllActionsQueue
;
230 gIOAllActionsQueue
= entry
;
233 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
235 kern_return_t
PE_cpu_start(cpu_id_t target
,
236 vm_offset_t start_paddr
, vm_offset_t arg_paddr
)
238 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
240 if (targetCPU
== 0) return KERN_FAILURE
;
241 return targetCPU
->startCPU(start_paddr
, arg_paddr
);
244 void PE_cpu_halt(cpu_id_t target
)
246 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
248 if (targetCPU
) targetCPU
->haltCPU();
251 void PE_cpu_signal(cpu_id_t source
, cpu_id_t target
)
253 IOCPU
*sourceCPU
= OSDynamicCast(IOCPU
, (OSObject
*)source
);
254 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
256 if (sourceCPU
&& targetCPU
) sourceCPU
->signalCPU(targetCPU
);
259 void PE_cpu_machine_init(cpu_id_t target
, boolean_t bootb
)
261 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
263 if (targetCPU
) targetCPU
->initCPU(bootb
);
266 void PE_cpu_machine_quiesce(cpu_id_t target
)
268 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
270 if (targetCPU
) targetCPU
->quiesceCPU();
273 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
275 #define super IOService
277 OSDefineMetaClassAndAbstractStructors(IOCPU
, IOService
);
278 OSMetaClassDefineReservedUnused(IOCPU
, 0);
279 OSMetaClassDefineReservedUnused(IOCPU
, 1);
280 OSMetaClassDefineReservedUnused(IOCPU
, 2);
281 OSMetaClassDefineReservedUnused(IOCPU
, 3);
282 OSMetaClassDefineReservedUnused(IOCPU
, 4);
283 OSMetaClassDefineReservedUnused(IOCPU
, 5);
284 OSMetaClassDefineReservedUnused(IOCPU
, 6);
285 OSMetaClassDefineReservedUnused(IOCPU
, 7);
287 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
289 static OSArray
*gIOCPUs
;
290 static const OSSymbol
*gIOCPUStateKey
;
291 static OSString
*gIOCPUStateNames
[kIOCPUStateCount
];
293 void IOCPUSleepKernel(void)
297 IOCPU
*bootCPU
= NULL
;
299 kprintf("IOCPUSleepKernel\n");
304 queue_init(&gIOSleepActionQueue
);
305 queue_init(&gIOWakeActionQueue
);
307 iter
= IORegistryIterator::iterateOver( gIOServicePlane
,
308 kIORegistryIterateRecursively
);
314 while((service
= (IOService
*) iter
->getNextObject()))
316 IOInstallServicePlatformAction(service
, gIOPlatformSleepActionKey
, &gIOSleepActionQueue
, false);
317 IOInstallServicePlatformAction(service
, gIOPlatformWakeActionKey
, &gIOWakeActionQueue
, true);
318 IOInstallServicePlatformAction(service
, gIOPlatformQuiesceActionKey
, iocpu_get_platform_quiesce_queue(), false);
319 IOInstallServicePlatformAction(service
, gIOPlatformActiveActionKey
, iocpu_get_platform_active_queue(), true);
322 while( !service
&& !iter
->isValid());
326 iocpu_run_platform_actions(&gIOSleepActionQueue
, 0, 0UL-1,
329 numCPUs
= gIOCPUs
->getCount();
334 target
= OSDynamicCast(IOCPU
, gIOCPUs
->getObject(cnt
));
336 // We make certain that the bootCPU is the last to sleep
337 // We'll skip it for now, and halt it after finishing the
339 if (target
->getCPUNumber() == kBootCPUNumber
)
342 } else if (target
->getCPUState() == kIOCPUStateRunning
)
348 // Now sleep the boot CPU.
352 iocpu_run_platform_actions(&gIOWakeActionQueue
, 0, 0UL-1,
355 iocpu_platform_action_entry_t
* entry
;
356 while ((entry
= gIOAllActionsQueue
))
358 gIOAllActionsQueue
= entry
->alloc_list
;
359 iocpu_remove_platform_action(entry
);
360 IODelete(entry
, iocpu_platform_action_entry_t
, 1);
363 if (!queue_empty(&gIOSleepActionQueue
))
364 IOPanic("gIOSleepActionQueue");
365 if (!queue_empty(&gIOWakeActionQueue
))
366 IOPanic("gIOWakeActionQueue");
368 // Wake the other CPUs.
369 for (cnt
= 0; cnt
< numCPUs
; cnt
++)
371 target
= OSDynamicCast(IOCPU
, gIOCPUs
->getObject(cnt
));
373 // Skip the already-woken boot CPU.
374 if ((target
->getCPUNumber() != kBootCPUNumber
)
375 && (target
->getCPUState() == kIOCPUStateStopped
))
377 processor_start(target
->getMachProcessor());
382 void IOCPU::initCPUs(void)
385 gIOCPUs
= OSArray::withCapacity(1);
387 gIOCPUStateKey
= OSSymbol::withCStringNoCopy("IOCPUState");
389 gIOCPUStateNames
[kIOCPUStateUnregistered
] =
390 OSString::withCStringNoCopy("Unregistered");
391 gIOCPUStateNames
[kIOCPUStateUninitalized
] =
392 OSString::withCStringNoCopy("Uninitalized");
393 gIOCPUStateNames
[kIOCPUStateStopped
] =
394 OSString::withCStringNoCopy("Stopped");
395 gIOCPUStateNames
[kIOCPUStateRunning
] =
396 OSString::withCStringNoCopy("Running");
400 bool IOCPU::start(IOService
*provider
)
402 OSData
*busFrequency
, *cpuFrequency
, *timebaseFrequency
;
404 if (!super::start(provider
)) return false;
411 gIOCPUs
->setObject(this);
413 // Correct the bus, cpu and timebase frequencies in the device tree.
414 if (gPEClockFrequencyInfo
.bus_frequency_hz
< 0x100000000ULL
) {
415 busFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.bus_clock_rate_hz
, 4);
417 busFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.bus_frequency_hz
, 8);
419 provider
->setProperty("bus-frequency", busFrequency
);
420 busFrequency
->release();
422 if (gPEClockFrequencyInfo
.cpu_frequency_hz
< 0x100000000ULL
) {
423 cpuFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.cpu_clock_rate_hz
, 4);
425 cpuFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.cpu_frequency_hz
, 8);
427 provider
->setProperty("clock-frequency", cpuFrequency
);
428 cpuFrequency
->release();
430 timebaseFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.timebase_frequency_hz
, 4);
431 provider
->setProperty("timebase-frequency", timebaseFrequency
);
432 timebaseFrequency
->release();
434 super::setProperty("IOCPUID", (UInt32
)this, 32);
437 setCPUState(kIOCPUStateUnregistered
);
442 OSObject
*IOCPU::getProperty(const OSSymbol
*aKey
) const
444 if (aKey
== gIOCPUStateKey
) return gIOCPUStateNames
[_cpuState
];
446 return super::getProperty(aKey
);
449 bool IOCPU::setProperty(const OSSymbol
*aKey
, OSObject
*anObject
)
453 if (aKey
== gIOCPUStateKey
) {
454 stateStr
= OSDynamicCast(OSString
, anObject
);
455 if (stateStr
== 0) return false;
457 if (_cpuNumber
== 0) return false;
459 if (stateStr
->isEqualTo("running")) {
460 if (_cpuState
== kIOCPUStateStopped
) {
461 processor_start(machProcessor
);
462 } else if (_cpuState
!= kIOCPUStateRunning
) {
465 } else if (stateStr
->isEqualTo("stopped")) {
466 if (_cpuState
== kIOCPUStateRunning
) {
468 } else if (_cpuState
!= kIOCPUStateStopped
) {
476 return super::setProperty(aKey
, anObject
);
479 bool IOCPU::serializeProperties(OSSerialize
*serialize
) const
482 OSDictionary
*dict
= dictionaryWithProperties();
483 dict
->setObject(gIOCPUStateKey
, gIOCPUStateNames
[_cpuState
]);
484 result
= dict
->serialize(serialize
);
489 IOReturn
IOCPU::setProperties(OSObject
*properties
)
491 OSDictionary
*dict
= OSDynamicCast(OSDictionary
, properties
);
495 if (dict
== 0) return kIOReturnUnsupported
;
497 stateStr
= OSDynamicCast(OSString
, dict
->getObject(gIOCPUStateKey
));
499 result
= IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator
);
500 if (result
!= kIOReturnSuccess
) return result
;
502 if (setProperty(gIOCPUStateKey
, stateStr
)) return kIOReturnSuccess
;
504 return kIOReturnUnsupported
;
507 return kIOReturnUnsupported
;
510 void IOCPU::signalCPU(IOCPU */
*target*/
)
514 void IOCPU::enableCPUTimeBase(bool /*enable*/)
518 UInt32
IOCPU::getCPUNumber(void)
523 void IOCPU::setCPUNumber(UInt32 cpuNumber
)
525 _cpuNumber
= cpuNumber
;
526 super::setProperty("IOCPUNumber", _cpuNumber
, 32);
529 UInt32
IOCPU::getCPUState(void)
534 void IOCPU::setCPUState(UInt32 cpuState
)
536 if (cpuState
< kIOCPUStateCount
) {
537 _cpuState
= cpuState
;
541 OSArray
*IOCPU::getCPUGroup(void)
546 UInt32
IOCPU::getCPUGroupSize(void)
548 return _cpuGroup
->getCount();
551 processor_t
IOCPU::getMachProcessor(void)
553 return machProcessor
;
557 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
560 #define super IOInterruptController
562 OSDefineMetaClassAndStructors(IOCPUInterruptController
, IOInterruptController
);
564 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 0);
565 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 1);
566 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 2);
567 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 3);
568 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 4);
569 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 5);
573 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
576 IOReturn
IOCPUInterruptController::initCPUInterruptController(int sources
)
580 if (!super::init()) return kIOReturnInvalid
;
584 cpus
= (IOCPU
**)IOMalloc(numCPUs
* sizeof(IOCPU
*));
585 if (cpus
== 0) return kIOReturnNoMemory
;
586 bzero(cpus
, numCPUs
* sizeof(IOCPU
*));
588 vectors
= (IOInterruptVector
*)IOMalloc(numCPUs
* sizeof(IOInterruptVector
));
589 if (vectors
== 0) return kIOReturnNoMemory
;
590 bzero(vectors
, numCPUs
* sizeof(IOInterruptVector
));
592 // Allocate locks for the
593 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
594 vectors
[cnt
].interruptLock
= IOLockAlloc();
595 if (vectors
[cnt
].interruptLock
== NULL
) {
596 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
597 if (vectors
[cnt
].interruptLock
!= NULL
)
598 IOLockFree(vectors
[cnt
].interruptLock
);
600 return kIOReturnNoResources
;
604 ml_init_max_cpus(numCPUs
);
606 return kIOReturnSuccess
;
609 void IOCPUInterruptController::registerCPUInterruptController(void)
613 getPlatform()->registerInterruptController(gPlatformInterruptControllerName
,
617 void IOCPUInterruptController::setCPUInterruptProperties(IOService
*service
)
625 if ((service
->getProperty(gIOInterruptControllersKey
) != 0) &&
626 (service
->getProperty(gIOInterruptSpecifiersKey
) != 0))
629 // Create the interrupt specifer array.
630 specifier
= OSArray::withCapacity(numCPUs
);
631 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
633 tmpData
= OSData::withBytes(&tmpLong
, sizeof(tmpLong
));
634 specifier
->setObject(tmpData
);
638 // Create the interrupt controller array.
639 controller
= OSArray::withCapacity(numCPUs
);
640 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
641 controller
->setObject(gPlatformInterruptControllerName
);
644 // Put the two arrays into the property table.
645 service
->setProperty(gIOInterruptControllersKey
, controller
);
646 service
->setProperty(gIOInterruptSpecifiersKey
, specifier
);
647 controller
->release();
648 specifier
->release();
651 void IOCPUInterruptController::enableCPUInterrupt(IOCPU
*cpu
)
653 IOInterruptHandler handler
= OSMemberFunctionCast(
654 IOInterruptHandler
, this, &IOCPUInterruptController::handleInterrupt
);
656 ml_install_interrupt_handler(cpu
, cpu
->getCPUNumber(), this, handler
, 0);
660 if (enabledCPUs
== numCPUs
) thread_wakeup(this);
663 IOReturn
IOCPUInterruptController::registerInterrupt(IOService
*nub
,
666 IOInterruptHandler handler
,
669 IOInterruptVector
*vector
;
671 if (source
>= numCPUs
) return kIOReturnNoResources
;
673 vector
= &vectors
[source
];
675 // Get the lock for this vector.
676 IOTakeLock(vector
->interruptLock
);
678 // Make sure the vector is not in use.
679 if (vector
->interruptRegistered
) {
680 IOUnlock(vector
->interruptLock
);
681 return kIOReturnNoResources
;
684 // Fill in vector with the client's info.
685 vector
->handler
= handler
;
687 vector
->source
= source
;
688 vector
->target
= target
;
689 vector
->refCon
= refCon
;
691 // Get the vector ready. It starts hard disabled.
692 vector
->interruptDisabledHard
= 1;
693 vector
->interruptDisabledSoft
= 1;
694 vector
->interruptRegistered
= 1;
696 IOUnlock(vector
->interruptLock
);
698 if (enabledCPUs
!= numCPUs
) {
699 assert_wait(this, THREAD_UNINT
);
700 thread_block(THREAD_CONTINUE_NULL
);
703 return kIOReturnSuccess
;
706 IOReturn
IOCPUInterruptController::getInterruptType(IOService */
*nub*/
,
710 if (interruptType
== 0) return kIOReturnBadArgument
;
712 *interruptType
= kIOInterruptTypeLevel
;
714 return kIOReturnSuccess
;
717 IOReturn
IOCPUInterruptController::enableInterrupt(IOService */
*nub*/
,
720 // ml_set_interrupts_enabled(true);
721 return kIOReturnSuccess
;
724 IOReturn
IOCPUInterruptController::disableInterrupt(IOService */
*nub*/
,
727 // ml_set_interrupts_enabled(false);
728 return kIOReturnSuccess
;
731 IOReturn
IOCPUInterruptController::causeInterrupt(IOService */
*nub*/
,
734 ml_cause_interrupt();
735 return kIOReturnSuccess
;
738 IOReturn
IOCPUInterruptController::handleInterrupt(void */
*refCon*/
,
742 IOInterruptVector
*vector
;
744 vector
= &vectors
[source
];
746 if (!vector
->interruptRegistered
) return kIOReturnInvalid
;
748 vector
->handler(vector
->target
, vector
->refCon
,
749 vector
->nub
, vector
->source
);
751 return kIOReturnSuccess
;
754 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */