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 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
88 static iocpu_platform_action_entry_t
* gIOAllActionsQueue
;
89 static queue_head_t gIOSleepActionQueue
;
90 static queue_head_t gIOWakeActionQueue
;
92 static queue_head_t iocpu_quiesce_queue
;
93 static queue_head_t iocpu_active_queue
;
95 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
98 iocpu_platform_cpu_action_init(queue_head_t
* quiesce_queue
, __unused queue_head_t
* init_queue
)
101 enum { kNumQuiesceActions
= 2 };
102 static iocpu_platform_action_entry_t quiesce_actions
[kNumQuiesceActions
] =
104 { { NULL
, NULL
}, (iocpu_platform_action_t
) &clean_mmu_dcache
, 97000, 0, 0, NULL
},
105 { { NULL
, NULL
}, (iocpu_platform_action_t
) &arm_sleep
, 99000, 0, 0, NULL
},
109 for (idx
= 0; idx
< kNumQuiesceActions
; idx
++)
110 iocpu_add_platform_action(quiesce_queue
, &quiesce_actions
[idx
]);
114 queue_head_t
* iocpu_get_platform_quiesce_queue(void)
116 if (!iocpu_quiesce_queue
.next
)
118 queue_init(&iocpu_quiesce_queue
);
119 queue_init(&iocpu_active_queue
);
120 iocpu_platform_cpu_action_init(&iocpu_quiesce_queue
, &iocpu_active_queue
);
122 return (&iocpu_quiesce_queue
);
125 queue_head_t
* iocpu_get_platform_active_queue(void)
127 return (&iocpu_active_queue
);
130 void iocpu_add_platform_action(queue_head_t
* queue
, iocpu_platform_action_entry_t
* entry
)
132 iocpu_platform_action_entry_t
* next
;
134 queue_iterate(queue
, next
, iocpu_platform_action_entry_t
*, link
)
136 if (next
->priority
> entry
->priority
)
138 queue_insert_before(queue
, entry
, next
, iocpu_platform_action_entry_t
*, link
);
142 queue_enter(queue
, entry
, iocpu_platform_action_entry_t
*, link
); // at tail
145 void iocpu_remove_platform_action(iocpu_platform_action_entry_t
* entry
)
147 remque(&entry
->link
);
151 iocpu_run_platform_actions(queue_head_t
* queue
, uint32_t first_priority
, uint32_t last_priority
,
152 void * param1
, void * param2
, void * param3
)
154 kern_return_t ret
= KERN_SUCCESS
;
155 kern_return_t result
= KERN_SUCCESS
;
156 iocpu_platform_action_entry_t
* next
;
158 queue_iterate(queue
, next
, iocpu_platform_action_entry_t
*, link
)
160 uint32_t pri
= (next
->priority
< 0) ? -next
->priority
: next
->priority
;
161 if ((pri
>= first_priority
) && (pri
<= last_priority
))
163 //kprintf("[%p]", next->action);
164 ret
= (*next
->action
)(next
->refcon0
, next
->refcon1
, pri
, param1
, param2
, param3
);
166 if (KERN_SUCCESS
== result
)
172 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
174 extern "C" kern_return_t
175 IOCPURunPlatformQuiesceActions(void)
177 return (iocpu_run_platform_actions(iocpu_get_platform_quiesce_queue(), 0, 0UL-1,
181 extern "C" kern_return_t
182 IOCPURunPlatformActiveActions(void)
184 return (iocpu_run_platform_actions(iocpu_get_platform_active_queue(), 0, 0UL-1,
189 IOServicePlatformAction(void * refcon0
, void * refcon1
, uint32_t priority
,
190 void * param1
, void * param2
, void * param3
)
193 IOService
* service
= (IOService
*) refcon0
;
194 const OSSymbol
* function
= (const OSSymbol
*) refcon1
;
196 kprintf("%s -> %s\n", function
->getCStringNoCopy(), service
->getName());
198 ret
= service
->callPlatformFunction(function
, false,
199 (void *) priority
, param1
, param2
, param3
);
205 IOInstallServicePlatformAction(IOService
* service
,
206 const OSSymbol
* key
, queue_head_t
* queue
,
210 iocpu_platform_action_entry_t
* entry
;
213 num
= OSDynamicCast(OSNumber
, service
->getProperty(key
));
217 entry
= IONew(iocpu_platform_action_entry_t
, 1);
218 entry
->action
= &IOServicePlatformAction
;
219 priority
= num
->unsigned32BitValue();
221 entry
->priority
= -priority
;
223 entry
->priority
= priority
;
224 entry
->refcon0
= service
;
225 entry
->refcon1
= (void *) key
;
227 iocpu_add_platform_action(queue
, entry
);
228 entry
->alloc_list
= gIOAllActionsQueue
;
229 gIOAllActionsQueue
= entry
;
232 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
234 kern_return_t
PE_cpu_start(cpu_id_t target
,
235 vm_offset_t start_paddr
, vm_offset_t arg_paddr
)
237 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
239 if (targetCPU
== 0) return KERN_FAILURE
;
240 return targetCPU
->startCPU(start_paddr
, arg_paddr
);
243 void PE_cpu_halt(cpu_id_t target
)
245 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
247 if (targetCPU
) targetCPU
->haltCPU();
250 void PE_cpu_signal(cpu_id_t source
, cpu_id_t target
)
252 IOCPU
*sourceCPU
= OSDynamicCast(IOCPU
, (OSObject
*)source
);
253 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
255 if (sourceCPU
&& targetCPU
) sourceCPU
->signalCPU(targetCPU
);
258 void PE_cpu_machine_init(cpu_id_t target
, boolean_t bootb
)
260 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
262 if (targetCPU
) targetCPU
->initCPU(bootb
);
265 void PE_cpu_machine_quiesce(cpu_id_t target
)
267 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
269 if (targetCPU
) targetCPU
->quiesceCPU();
272 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
274 #define super IOService
276 OSDefineMetaClassAndAbstractStructors(IOCPU
, IOService
);
277 OSMetaClassDefineReservedUnused(IOCPU
, 0);
278 OSMetaClassDefineReservedUnused(IOCPU
, 1);
279 OSMetaClassDefineReservedUnused(IOCPU
, 2);
280 OSMetaClassDefineReservedUnused(IOCPU
, 3);
281 OSMetaClassDefineReservedUnused(IOCPU
, 4);
282 OSMetaClassDefineReservedUnused(IOCPU
, 5);
283 OSMetaClassDefineReservedUnused(IOCPU
, 6);
284 OSMetaClassDefineReservedUnused(IOCPU
, 7);
286 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
288 static OSArray
*gIOCPUs
;
289 static const OSSymbol
*gIOCPUStateKey
;
290 static OSString
*gIOCPUStateNames
[kIOCPUStateCount
];
292 void IOCPUSleepKernel(void)
297 kprintf("IOCPUSleepKernel\n");
302 queue_init(&gIOSleepActionQueue
);
303 queue_init(&gIOWakeActionQueue
);
305 iter
= IORegistryIterator::iterateOver( gIOServicePlane
,
306 kIORegistryIterateRecursively
);
312 while((service
= (IOService
*) iter
->getNextObject()))
314 IOInstallServicePlatformAction(service
, gIOPlatformSleepActionKey
, &gIOSleepActionQueue
, false);
315 IOInstallServicePlatformAction(service
, gIOPlatformWakeActionKey
, &gIOWakeActionQueue
, true);
316 IOInstallServicePlatformAction(service
, gIOPlatformQuiesceActionKey
, iocpu_get_platform_quiesce_queue(), false);
317 IOInstallServicePlatformAction(service
, gIOPlatformActiveActionKey
, iocpu_get_platform_active_queue(), true);
320 while( !service
&& !iter
->isValid());
324 iocpu_run_platform_actions(&gIOSleepActionQueue
, 0, 0UL-1,
327 numCPUs
= gIOCPUs
->getCount();
331 target
= OSDynamicCast(IOCPU
, gIOCPUs
->getObject(cnt
));
332 if (target
->getCPUState() == kIOCPUStateRunning
) {
337 iocpu_run_platform_actions(&gIOWakeActionQueue
, 0, 0UL-1,
340 iocpu_platform_action_entry_t
* entry
;
341 while ((entry
= gIOAllActionsQueue
))
343 gIOAllActionsQueue
= entry
->alloc_list
;
344 iocpu_remove_platform_action(entry
);
345 IODelete(entry
, iocpu_platform_action_entry_t
, 1);
348 if (!queue_empty(&gIOSleepActionQueue
))
349 IOPanic("gIOSleepActionQueue");
350 if (!queue_empty(&gIOWakeActionQueue
))
351 IOPanic("gIOWakeActionQueue");
353 // Wake the other CPUs.
354 for (cnt
= 1; cnt
< numCPUs
; cnt
++) {
355 target
= OSDynamicCast(IOCPU
, gIOCPUs
->getObject(cnt
));
356 if (target
->getCPUState() == kIOCPUStateStopped
) {
357 processor_start(target
->getMachProcessor());
362 void IOCPU::initCPUs(void)
365 gIOCPUs
= OSArray::withCapacity(1);
367 gIOCPUStateKey
= OSSymbol::withCStringNoCopy("IOCPUState");
369 gIOCPUStateNames
[kIOCPUStateUnregistered
] =
370 OSString::withCStringNoCopy("Unregistered");
371 gIOCPUStateNames
[kIOCPUStateUninitalized
] =
372 OSString::withCStringNoCopy("Uninitalized");
373 gIOCPUStateNames
[kIOCPUStateStopped
] =
374 OSString::withCStringNoCopy("Stopped");
375 gIOCPUStateNames
[kIOCPUStateRunning
] =
376 OSString::withCStringNoCopy("Running");
380 bool IOCPU::start(IOService
*provider
)
382 OSData
*busFrequency
, *cpuFrequency
, *timebaseFrequency
;
384 if (!super::start(provider
)) return false;
391 gIOCPUs
->setObject(this);
393 // Correct the bus, cpu and timebase frequencies in the device tree.
394 if (gPEClockFrequencyInfo
.bus_frequency_hz
< 0x100000000ULL
) {
395 busFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.bus_clock_rate_hz
, 4);
397 busFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.bus_frequency_hz
, 8);
399 provider
->setProperty("bus-frequency", busFrequency
);
400 busFrequency
->release();
402 if (gPEClockFrequencyInfo
.cpu_frequency_hz
< 0x100000000ULL
) {
403 cpuFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.cpu_clock_rate_hz
, 4);
405 cpuFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.cpu_frequency_hz
, 8);
407 provider
->setProperty("clock-frequency", cpuFrequency
);
408 cpuFrequency
->release();
410 timebaseFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.timebase_frequency_hz
, 4);
411 provider
->setProperty("timebase-frequency", timebaseFrequency
);
412 timebaseFrequency
->release();
414 super::setProperty("IOCPUID", (UInt32
)this, 32);
417 setCPUState(kIOCPUStateUnregistered
);
422 OSObject
*IOCPU::getProperty(const OSSymbol
*aKey
) const
424 if (aKey
== gIOCPUStateKey
) return gIOCPUStateNames
[_cpuState
];
426 return super::getProperty(aKey
);
429 bool IOCPU::setProperty(const OSSymbol
*aKey
, OSObject
*anObject
)
433 if (aKey
== gIOCPUStateKey
) {
434 stateStr
= OSDynamicCast(OSString
, anObject
);
435 if (stateStr
== 0) return false;
437 if (_cpuNumber
== 0) return false;
439 if (stateStr
->isEqualTo("running")) {
440 if (_cpuState
== kIOCPUStateStopped
) {
441 processor_start(machProcessor
);
442 } else if (_cpuState
!= kIOCPUStateRunning
) {
445 } else if (stateStr
->isEqualTo("stopped")) {
446 if (_cpuState
== kIOCPUStateRunning
) {
448 } else if (_cpuState
!= kIOCPUStateStopped
) {
456 return super::setProperty(aKey
, anObject
);
459 bool IOCPU::serializeProperties(OSSerialize
*serialize
) const
462 OSDictionary
*dict
= dictionaryWithProperties();
463 dict
->setObject(gIOCPUStateKey
, gIOCPUStateNames
[_cpuState
]);
464 result
= dict
->serialize(serialize
);
469 IOReturn
IOCPU::setProperties(OSObject
*properties
)
471 OSDictionary
*dict
= OSDynamicCast(OSDictionary
, properties
);
475 if (dict
== 0) return kIOReturnUnsupported
;
477 stateStr
= OSDynamicCast(OSString
, dict
->getObject(gIOCPUStateKey
));
479 result
= IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator
);
480 if (result
!= kIOReturnSuccess
) return result
;
482 if (setProperty(gIOCPUStateKey
, stateStr
)) return kIOReturnSuccess
;
484 return kIOReturnUnsupported
;
487 return kIOReturnUnsupported
;
490 void IOCPU::signalCPU(IOCPU */
*target*/
)
494 void IOCPU::enableCPUTimeBase(bool /*enable*/)
498 UInt32
IOCPU::getCPUNumber(void)
503 void IOCPU::setCPUNumber(UInt32 cpuNumber
)
505 _cpuNumber
= cpuNumber
;
506 super::setProperty("IOCPUNumber", _cpuNumber
, 32);
509 UInt32
IOCPU::getCPUState(void)
514 void IOCPU::setCPUState(UInt32 cpuState
)
516 if (cpuState
< kIOCPUStateCount
) {
517 _cpuState
= cpuState
;
521 OSArray
*IOCPU::getCPUGroup(void)
526 UInt32
IOCPU::getCPUGroupSize(void)
528 return _cpuGroup
->getCount();
531 processor_t
IOCPU::getMachProcessor(void)
533 return machProcessor
;
537 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
540 #define super IOInterruptController
542 OSDefineMetaClassAndStructors(IOCPUInterruptController
, IOInterruptController
);
544 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 0);
545 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 1);
546 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 2);
547 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 3);
548 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 4);
549 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 5);
553 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
556 IOReturn
IOCPUInterruptController::initCPUInterruptController(int sources
)
560 if (!super::init()) return kIOReturnInvalid
;
564 cpus
= (IOCPU
**)IOMalloc(numCPUs
* sizeof(IOCPU
*));
565 if (cpus
== 0) return kIOReturnNoMemory
;
566 bzero(cpus
, numCPUs
* sizeof(IOCPU
*));
568 vectors
= (IOInterruptVector
*)IOMalloc(numCPUs
* sizeof(IOInterruptVector
));
569 if (vectors
== 0) return kIOReturnNoMemory
;
570 bzero(vectors
, numCPUs
* sizeof(IOInterruptVector
));
572 // Allocate locks for the
573 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
574 vectors
[cnt
].interruptLock
= IOLockAlloc();
575 if (vectors
[cnt
].interruptLock
== NULL
) {
576 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
577 if (vectors
[cnt
].interruptLock
!= NULL
)
578 IOLockFree(vectors
[cnt
].interruptLock
);
580 return kIOReturnNoResources
;
584 ml_init_max_cpus(numCPUs
);
586 return kIOReturnSuccess
;
589 void IOCPUInterruptController::registerCPUInterruptController(void)
593 getPlatform()->registerInterruptController(gPlatformInterruptControllerName
,
597 void IOCPUInterruptController::setCPUInterruptProperties(IOService
*service
)
605 if ((service
->getProperty(gIOInterruptControllersKey
) != 0) &&
606 (service
->getProperty(gIOInterruptSpecifiersKey
) != 0))
609 // Create the interrupt specifer array.
610 specifier
= OSArray::withCapacity(numCPUs
);
611 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
613 tmpData
= OSData::withBytes(&tmpLong
, sizeof(tmpLong
));
614 specifier
->setObject(tmpData
);
618 // Create the interrupt controller array.
619 controller
= OSArray::withCapacity(numCPUs
);
620 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
621 controller
->setObject(gPlatformInterruptControllerName
);
624 // Put the two arrays into the property table.
625 service
->setProperty(gIOInterruptControllersKey
, controller
);
626 service
->setProperty(gIOInterruptSpecifiersKey
, specifier
);
627 controller
->release();
628 specifier
->release();
631 void IOCPUInterruptController::enableCPUInterrupt(IOCPU
*cpu
)
633 IOInterruptHandler handler
= OSMemberFunctionCast(
634 IOInterruptHandler
, this, &IOCPUInterruptController::handleInterrupt
);
636 ml_install_interrupt_handler(cpu
, cpu
->getCPUNumber(), this, handler
, 0);
640 if (enabledCPUs
== numCPUs
) thread_wakeup(this);
643 IOReturn
IOCPUInterruptController::registerInterrupt(IOService
*nub
,
646 IOInterruptHandler handler
,
649 IOInterruptVector
*vector
;
651 if (source
>= numCPUs
) return kIOReturnNoResources
;
653 vector
= &vectors
[source
];
655 // Get the lock for this vector.
656 IOTakeLock(vector
->interruptLock
);
658 // Make sure the vector is not in use.
659 if (vector
->interruptRegistered
) {
660 IOUnlock(vector
->interruptLock
);
661 return kIOReturnNoResources
;
664 // Fill in vector with the client's info.
665 vector
->handler
= handler
;
667 vector
->source
= source
;
668 vector
->target
= target
;
669 vector
->refCon
= refCon
;
671 // Get the vector ready. It starts hard disabled.
672 vector
->interruptDisabledHard
= 1;
673 vector
->interruptDisabledSoft
= 1;
674 vector
->interruptRegistered
= 1;
676 IOUnlock(vector
->interruptLock
);
678 if (enabledCPUs
!= numCPUs
) {
679 assert_wait(this, THREAD_UNINT
);
680 thread_block(THREAD_CONTINUE_NULL
);
683 return kIOReturnSuccess
;
686 IOReturn
IOCPUInterruptController::getInterruptType(IOService */
*nub*/
,
690 if (interruptType
== 0) return kIOReturnBadArgument
;
692 *interruptType
= kIOInterruptTypeLevel
;
694 return kIOReturnSuccess
;
697 IOReturn
IOCPUInterruptController::enableInterrupt(IOService */
*nub*/
,
700 // ml_set_interrupts_enabled(true);
701 return kIOReturnSuccess
;
704 IOReturn
IOCPUInterruptController::disableInterrupt(IOService */
*nub*/
,
707 // ml_set_interrupts_enabled(false);
708 return kIOReturnSuccess
;
711 IOReturn
IOCPUInterruptController::causeInterrupt(IOService */
*nub*/
,
714 ml_cause_interrupt();
715 return kIOReturnSuccess
;
718 IOReturn
IOCPUInterruptController::handleInterrupt(void */
*refCon*/
,
722 IOInterruptVector
*vector
;
724 vector
= &vectors
[source
];
726 if (!vector
->interruptRegistered
) return kIOReturnInvalid
;
728 vector
->handler(vector
->target
, vector
->refCon
,
729 vector
->nub
, vector
->source
);
731 return kIOReturnSuccess
;
734 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */