2 * Copyright (c) 1999-2016 Apple 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@
30 #include <machine/machine_routines.h>
31 #include <pexpert/pexpert.h>
32 #include <kern/cpu_number.h>
33 extern void kperf_kernel_configure(char *);
36 #include <IOKit/IOLib.h>
37 #include <IOKit/IOPlatformExpert.h>
38 #include <IOKit/pwr_mgt/RootDomain.h>
39 #include <IOKit/pwr_mgt/IOPMPrivate.h>
40 #include <IOKit/IOUserClient.h>
41 #include <IOKit/IOKitKeysPrivate.h>
42 #include <IOKit/IOCPU.h>
43 #include "IOKitKernelInternal.h"
45 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
46 #include <kern/queue.h>
48 extern "C" void console_suspend();
49 extern "C" void console_resume();
50 extern "C" void sched_override_recommended_cores_for_sleep(void);
51 extern "C" void sched_restore_recommended_cores_after_sleep(void);
53 typedef kern_return_t (*iocpu_platform_action_t
)(void * refcon0
, void * refcon1
, uint32_t priority
,
54 void * param1
, void * param2
, void * param3
,
57 struct iocpu_platform_action_entry
{
59 iocpu_platform_action_t action
;
64 boolean_t callout_in_progress
;
65 struct iocpu_platform_action_entry
* alloc_list
;
67 typedef struct iocpu_platform_action_entry iocpu_platform_action_entry_t
;
69 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
71 static IOLock
*gIOCPUsLock
;
72 static OSArray
*gIOCPUs
;
73 static const OSSymbol
*gIOCPUStateKey
;
74 static OSString
*gIOCPUStateNames
[kIOCPUStateCount
];
81 kQueueHaltRestart
= 4,
86 const OSSymbol
* gIOPlatformSleepActionKey
;
87 const OSSymbol
* gIOPlatformWakeActionKey
;
88 const OSSymbol
* gIOPlatformQuiesceActionKey
;
89 const OSSymbol
* gIOPlatformActiveActionKey
;
90 const OSSymbol
* gIOPlatformHaltRestartActionKey
;
91 const OSSymbol
* gIOPlatformPanicActionKey
;
93 static queue_head_t gActionQueues
[kQueueCount
];
94 static const OSSymbol
* gActionSymbols
[kQueueCount
];
96 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
99 iocpu_add_platform_action(queue_head_t
* queue
, iocpu_platform_action_entry_t
* entry
)
101 iocpu_platform_action_entry_t
* next
;
103 queue_iterate(queue
, next
, iocpu_platform_action_entry_t
*, link
)
105 if (next
->priority
> entry
->priority
) {
106 queue_insert_before(queue
, entry
, next
, iocpu_platform_action_entry_t
*, link
);
110 queue_enter(queue
, entry
, iocpu_platform_action_entry_t
*, link
); // at tail
114 iocpu_remove_platform_action(iocpu_platform_action_entry_t
* entry
)
116 remque(&entry
->link
);
120 iocpu_run_platform_actions(queue_head_t
* queue
, uint32_t first_priority
, uint32_t last_priority
,
121 void * param1
, void * param2
, void * param3
, boolean_t allow_nested_callouts
)
123 kern_return_t ret
= KERN_SUCCESS
;
124 kern_return_t result
= KERN_SUCCESS
;
125 iocpu_platform_action_entry_t
* next
;
127 queue_iterate(queue
, next
, iocpu_platform_action_entry_t
*, link
)
129 uint32_t pri
= (next
->priority
< 0) ? -next
->priority
: next
->priority
;
130 if ((pri
>= first_priority
) && (pri
<= last_priority
)) {
131 //kprintf("[%p]", next->action);
132 if (!allow_nested_callouts
&& !next
->callout_in_progress
) {
133 next
->callout_in_progress
= TRUE
;
134 ret
= (*next
->action
)(next
->refcon0
, next
->refcon1
, pri
, param1
, param2
, param3
, next
->name
);
135 next
->callout_in_progress
= FALSE
;
136 } else if (allow_nested_callouts
) {
137 ret
= (*next
->action
)(next
->refcon0
, next
->refcon1
, pri
, param1
, param2
, param3
, next
->name
);
140 if (KERN_SUCCESS
== result
) {
147 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
149 extern "C" kern_return_t
150 IOCPURunPlatformQuiesceActions(void)
152 return iocpu_run_platform_actions(&gActionQueues
[kQueueQuiesce
], 0, 0U - 1,
153 NULL
, NULL
, NULL
, TRUE
);
156 extern "C" kern_return_t
157 IOCPURunPlatformActiveActions(void)
159 return iocpu_run_platform_actions(&gActionQueues
[kQueueActive
], 0, 0U - 1,
160 NULL
, NULL
, NULL
, TRUE
);
163 extern "C" kern_return_t
164 IOCPURunPlatformHaltRestartActions(uint32_t message
)
166 if (!gActionQueues
[kQueueHaltRestart
].next
) {
167 return kIOReturnNotReady
;
169 return iocpu_run_platform_actions(&gActionQueues
[kQueueHaltRestart
], 0, 0U - 1,
170 (void *)(uintptr_t) message
, NULL
, NULL
, TRUE
);
173 extern "C" kern_return_t
174 IOCPURunPlatformPanicActions(uint32_t message
)
176 // Don't allow nested calls of panic actions
177 if (!gActionQueues
[kQueuePanic
].next
) {
178 return kIOReturnNotReady
;
180 return iocpu_run_platform_actions(&gActionQueues
[kQueuePanic
], 0, 0U - 1,
181 (void *)(uintptr_t) message
, NULL
, NULL
, FALSE
);
185 extern "C" kern_return_t
186 IOCPURunPlatformPanicSyncAction(void *addr
, uint32_t offset
, uint32_t len
)
188 PE_panic_save_context_t context
= {
190 .psc_offset
= offset
,
194 // Don't allow nested calls of panic actions
195 if (!gActionQueues
[kQueuePanic
].next
) {
196 return kIOReturnNotReady
;
198 return iocpu_run_platform_actions(&gActionQueues
[kQueuePanic
], 0, 0U - 1,
199 (void *)(uintptr_t)(kPEPanicSync
), &context
, NULL
, FALSE
);
202 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
205 IOServicePlatformAction(void * refcon0
, void * refcon1
, uint32_t priority
,
206 void * param1
, void * param2
, void * param3
,
207 const char * service_name
)
210 IOService
* service
= (IOService
*) refcon0
;
211 const OSSymbol
* function
= (const OSSymbol
*) refcon1
;
213 kprintf("%s -> %s\n", function
->getCStringNoCopy(), service_name
);
215 ret
= service
->callPlatformFunction(function
, false,
216 (void *)(uintptr_t) priority
, param1
, param2
, param3
);
222 IOInstallServicePlatformAction(IOService
* service
, uint32_t qidx
)
224 iocpu_platform_action_entry_t
* entry
;
227 const OSSymbol
* key
= gActionSymbols
[qidx
];
228 queue_head_t
* queue
= &gActionQueues
[qidx
];
232 num
= OSDynamicCast(OSNumber
, service
->getProperty(key
));
244 case kQueueHaltRestart
:
250 queue_iterate(queue
, entry
, iocpu_platform_action_entry_t
*, link
)
252 if (service
== entry
->refcon0
) {
258 entry
= IONew(iocpu_platform_action_entry_t
, 1);
259 entry
->action
= &IOServicePlatformAction
;
260 entry
->name
= service
->getName();
261 priority
= num
->unsigned32BitValue();
263 entry
->priority
= -priority
;
265 entry
->priority
= priority
;
267 entry
->refcon0
= service
;
268 entry
->refcon1
= (void *) key
;
269 entry
->callout_in_progress
= FALSE
;
271 iocpu_add_platform_action(queue
, entry
);
274 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
277 IOCPUInitialize(void)
279 gIOCPUsLock
= IOLockAlloc();
280 gIOCPUs
= OSArray::withCapacity(1);
282 for (uint32_t qidx
= kQueueSleep
; qidx
< kQueueCount
; qidx
++) {
283 queue_init(&gActionQueues
[qidx
]);
286 gIOCPUStateKey
= OSSymbol::withCStringNoCopy("IOCPUState");
288 gIOCPUStateNames
[kIOCPUStateUnregistered
] =
289 OSString::withCStringNoCopy("Unregistered");
290 gIOCPUStateNames
[kIOCPUStateUninitalized
] =
291 OSString::withCStringNoCopy("Uninitalized");
292 gIOCPUStateNames
[kIOCPUStateStopped
] =
293 OSString::withCStringNoCopy("Stopped");
294 gIOCPUStateNames
[kIOCPUStateRunning
] =
295 OSString::withCStringNoCopy("Running");
297 gIOPlatformSleepActionKey
= gActionSymbols
[kQueueSleep
]
298 = OSSymbol::withCStringNoCopy(kIOPlatformSleepActionKey
);
299 gIOPlatformWakeActionKey
= gActionSymbols
[kQueueWake
]
300 = OSSymbol::withCStringNoCopy(kIOPlatformWakeActionKey
);
301 gIOPlatformQuiesceActionKey
= gActionSymbols
[kQueueQuiesce
]
302 = OSSymbol::withCStringNoCopy(kIOPlatformQuiesceActionKey
);
303 gIOPlatformActiveActionKey
= gActionSymbols
[kQueueActive
]
304 = OSSymbol::withCStringNoCopy(kIOPlatformActiveActionKey
);
305 gIOPlatformHaltRestartActionKey
= gActionSymbols
[kQueueHaltRestart
]
306 = OSSymbol::withCStringNoCopy(kIOPlatformHaltRestartActionKey
);
307 gIOPlatformPanicActionKey
= gActionSymbols
[kQueuePanic
]
308 = OSSymbol::withCStringNoCopy(kIOPlatformPanicActionKey
);
312 IOInstallServicePlatformActions(IOService
* service
)
314 IOLockLock(gIOCPUsLock
);
316 IOInstallServicePlatformAction(service
, kQueueHaltRestart
);
317 IOInstallServicePlatformAction(service
, kQueuePanic
);
319 IOLockUnlock(gIOCPUsLock
);
321 return kIOReturnSuccess
;
325 IORemoveServicePlatformActions(IOService
* service
)
327 iocpu_platform_action_entry_t
* entry
;
328 iocpu_platform_action_entry_t
* next
;
330 IOLockLock(gIOCPUsLock
);
332 for (uint32_t qidx
= kQueueSleep
; qidx
< kQueueCount
; qidx
++) {
333 next
= (typeof(entry
))queue_first(&gActionQueues
[qidx
]);
334 while (!queue_end(&gActionQueues
[qidx
], &next
->link
)) {
336 next
= (typeof(entry
))queue_next(&entry
->link
);
337 if (service
== entry
->refcon0
) {
338 iocpu_remove_platform_action(entry
);
339 IODelete(entry
, iocpu_platform_action_entry_t
, 1);
344 IOLockUnlock(gIOCPUsLock
);
346 return kIOReturnSuccess
;
350 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
353 PE_cpu_start(cpu_id_t target
,
354 vm_offset_t start_paddr
, vm_offset_t arg_paddr
)
356 IOCPU
*targetCPU
= (IOCPU
*)target
;
358 if (targetCPU
== NULL
) {
361 return targetCPU
->startCPU(start_paddr
, arg_paddr
);
365 PE_cpu_halt(cpu_id_t target
)
367 IOCPU
*targetCPU
= (IOCPU
*)target
;
369 targetCPU
->haltCPU();
373 PE_cpu_signal(cpu_id_t source
, cpu_id_t target
)
375 IOCPU
*sourceCPU
= (IOCPU
*)source
;
376 IOCPU
*targetCPU
= (IOCPU
*)target
;
378 sourceCPU
->signalCPU(targetCPU
);
382 PE_cpu_signal_deferred(cpu_id_t source
, cpu_id_t target
)
384 IOCPU
*sourceCPU
= (IOCPU
*)source
;
385 IOCPU
*targetCPU
= (IOCPU
*)target
;
387 sourceCPU
->signalCPUDeferred(targetCPU
);
391 PE_cpu_signal_cancel(cpu_id_t source
, cpu_id_t target
)
393 IOCPU
*sourceCPU
= (IOCPU
*)source
;
394 IOCPU
*targetCPU
= (IOCPU
*)target
;
396 sourceCPU
->signalCPUCancel(targetCPU
);
400 PE_cpu_machine_init(cpu_id_t target
, boolean_t bootb
)
402 IOCPU
*targetCPU
= OSDynamicCast(IOCPU
, (OSObject
*)target
);
404 if (targetCPU
== NULL
) {
405 panic("%s: invalid target CPU %p", __func__
, target
);
408 targetCPU
->initCPU(bootb
);
409 #if defined(__arm__) || defined(__arm64__)
410 if (!bootb
&& (targetCPU
->getCPUNumber() == (UInt32
)master_cpu
)) {
411 ml_set_is_quiescing(false);
413 #endif /* defined(__arm__) || defined(__arm64__) */
417 PE_cpu_machine_quiesce(cpu_id_t target
)
419 IOCPU
*targetCPU
= (IOCPU
*)target
;
420 #if defined(__arm__) || defined(__arm64__)
421 if (targetCPU
->getCPUNumber() == (UInt32
)master_cpu
) {
422 ml_set_is_quiescing(true);
424 #endif /* defined(__arm__) || defined(__arm64__) */
425 targetCPU
->quiesceCPU();
428 #if defined(__arm__) || defined(__arm64__)
429 static perfmon_interrupt_handler_func pmi_handler
= 0;
432 PE_cpu_perfmon_interrupt_install_handler(perfmon_interrupt_handler_func handler
)
434 pmi_handler
= handler
;
440 PE_cpu_perfmon_interrupt_enable(cpu_id_t target
, boolean_t enable
)
442 IOCPU
*targetCPU
= (IOCPU
*)target
;
444 if (targetCPU
== nullptr) {
449 targetCPU
->getProvider()->registerInterrupt(1, targetCPU
, (IOInterruptAction
)pmi_handler
, 0);
450 targetCPU
->getProvider()->enableInterrupt(1);
452 targetCPU
->getProvider()->disableInterrupt(1);
457 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
459 #define super IOService
461 OSDefineMetaClassAndAbstractStructors(IOCPU
, IOService
);
462 OSMetaClassDefineReservedUnused(IOCPU
, 0);
463 OSMetaClassDefineReservedUnused(IOCPU
, 1);
464 OSMetaClassDefineReservedUnused(IOCPU
, 2);
465 OSMetaClassDefineReservedUnused(IOCPU
, 3);
466 OSMetaClassDefineReservedUnused(IOCPU
, 4);
467 OSMetaClassDefineReservedUnused(IOCPU
, 5);
468 OSMetaClassDefineReservedUnused(IOCPU
, 6);
469 OSMetaClassDefineReservedUnused(IOCPU
, 7);
471 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
474 IOCPUSleepKernel(void)
476 #if defined(__x86_64__)
477 extern IOCPU
*currentShutdownTarget
;
481 IOCPU
*bootCPU
= NULL
;
482 IOPMrootDomain
*rootDomain
= IOService::getPMRootDomain();
484 kprintf("IOCPUSleepKernel\n");
485 #if defined(__arm64__)
486 sched_override_recommended_cores_for_sleep();
489 IORegistryIterator
* iter
;
493 rootDomain
->tracePoint( kIOPMTracePointSleepPlatformActions
);
495 iter
= IORegistryIterator::iterateOver( gIOServicePlane
,
496 kIORegistryIterateRecursively
);
503 all
= iter
->iterateAll();
504 }while (!iter
->isValid());
508 while ((service
= (IOService
*) all
->getFirstObject())) {
509 for (uint32_t qidx
= kQueueSleep
; qidx
<= kQueueActive
; qidx
++) {
510 IOInstallServicePlatformAction(service
, qidx
);
512 all
->removeObject(service
);
518 iocpu_run_platform_actions(&gActionQueues
[kQueueSleep
], 0, 0U - 1,
519 NULL
, NULL
, NULL
, TRUE
);
521 rootDomain
->tracePoint( kIOPMTracePointSleepCPUs
);
523 numCPUs
= gIOCPUs
->getCount();
524 #if defined(__x86_64__)
525 currentShutdownTarget
= NULL
;
531 target
= OSDynamicCast(IOCPU
, gIOCPUs
->getObject(cnt
));
533 // We make certain that the bootCPU is the last to sleep
534 // We'll skip it for now, and halt it after finishing the
536 if (target
->getCPUNumber() == (UInt32
)master_cpu
) {
538 } else if (target
->getCPUState() == kIOCPUStateRunning
) {
539 #if defined(__x86_64__)
540 currentShutdownTarget
= target
;
546 assert(bootCPU
!= NULL
);
547 assert(cpu_number() == master_cpu
);
551 rootDomain
->tracePoint( kIOPMTracePointSleepPlatformDriver
);
552 rootDomain
->stop_watchdog_timer();
554 // Now sleep the boot CPU.
557 rootDomain
->start_watchdog_timer();
558 rootDomain
->tracePoint( kIOPMTracePointWakePlatformActions
);
562 iocpu_run_platform_actions(&gActionQueues
[kQueueWake
], 0, 0U - 1,
563 NULL
, NULL
, NULL
, TRUE
);
565 iocpu_platform_action_entry_t
* entry
;
566 for (uint32_t qidx
= kQueueSleep
; qidx
<= kQueueActive
; qidx
++) {
567 while (!(queue_empty(&gActionQueues
[qidx
]))) {
568 entry
= (typeof(entry
))queue_first(&gActionQueues
[qidx
]);
569 iocpu_remove_platform_action(entry
);
570 IODelete(entry
, iocpu_platform_action_entry_t
, 1);
574 rootDomain
->tracePoint( kIOPMTracePointWakeCPUs
);
576 // Wake the other CPUs.
577 for (cnt
= 0; cnt
< numCPUs
; cnt
++) {
578 target
= OSDynamicCast(IOCPU
, gIOCPUs
->getObject(cnt
));
580 // Skip the already-woken boot CPU.
581 if (target
->getCPUNumber() != (UInt32
)master_cpu
) {
582 if (target
->getCPUState() == kIOCPUStateRunning
) {
583 panic("Spurious wakeup of cpu %u", (unsigned int)(target
->getCPUNumber()));
586 if (target
->getCPUState() == kIOCPUStateStopped
) {
587 processor_start(target
->getMachProcessor());
592 #if defined(__arm64__)
593 sched_restore_recommended_cores_after_sleep();
598 IOCPU::start(IOService
*provider
)
600 OSData
*busFrequency
, *cpuFrequency
, *timebaseFrequency
;
602 if (!super::start(provider
)) {
609 IOLockLock(gIOCPUsLock
);
610 gIOCPUs
->setObject(this);
611 IOLockUnlock(gIOCPUsLock
);
613 // Correct the bus, cpu and timebase frequencies in the device tree.
614 if (gPEClockFrequencyInfo
.bus_frequency_hz
< 0x100000000ULL
) {
615 busFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.bus_clock_rate_hz
, 4);
617 busFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.bus_frequency_hz
, 8);
619 provider
->setProperty("bus-frequency", busFrequency
);
620 busFrequency
->release();
622 if (gPEClockFrequencyInfo
.cpu_frequency_hz
< 0x100000000ULL
) {
623 cpuFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.cpu_clock_rate_hz
, 4);
625 cpuFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.cpu_frequency_hz
, 8);
627 provider
->setProperty("clock-frequency", cpuFrequency
);
628 cpuFrequency
->release();
630 timebaseFrequency
= OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo
.timebase_frequency_hz
, 4);
631 provider
->setProperty("timebase-frequency", timebaseFrequency
);
632 timebaseFrequency
->release();
634 super::setProperty("IOCPUID", getRegistryEntryID(), sizeof(uint64_t) * 8);
637 setCPUState(kIOCPUStateUnregistered
);
643 IOCPU::getProperty(const OSSymbol
*aKey
) const
645 if (aKey
== gIOCPUStateKey
) {
646 return gIOCPUStateNames
[_cpuState
];
649 return super::getProperty(aKey
);
653 IOCPU::setProperty(const OSSymbol
*aKey
, OSObject
*anObject
)
655 if (aKey
== gIOCPUStateKey
) {
659 return super::setProperty(aKey
, anObject
);
663 IOCPU::serializeProperties(OSSerialize
*serialize
) const
666 OSDictionary
*dict
= dictionaryWithProperties();
670 dict
->setObject(gIOCPUStateKey
, gIOCPUStateNames
[_cpuState
]);
671 result
= dict
->serialize(serialize
);
677 IOCPU::setProperties(OSObject
*properties
)
679 OSDictionary
*dict
= OSDynamicCast(OSDictionary
, properties
);
684 return kIOReturnUnsupported
;
687 stateStr
= OSDynamicCast(OSString
, dict
->getObject(gIOCPUStateKey
));
689 result
= IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator
);
690 if (result
!= kIOReturnSuccess
) {
694 if (setProperty(gIOCPUStateKey
, stateStr
)) {
695 return kIOReturnSuccess
;
698 return kIOReturnUnsupported
;
701 return kIOReturnUnsupported
;
705 IOCPU::signalCPU(IOCPU */
*target*/
)
710 IOCPU::signalCPUDeferred(IOCPU
*target
)
712 // Our CPU may not support deferred IPIs,
713 // so send a regular IPI by default
718 IOCPU::signalCPUCancel(IOCPU */
*target*/
)
720 // Meant to cancel signals sent by
721 // signalCPUDeferred; unsupported
726 IOCPU::enableCPUTimeBase(bool /*enable*/)
731 IOCPU::getCPUNumber(void)
737 IOCPU::setCPUNumber(UInt32 cpuNumber
)
739 _cpuNumber
= cpuNumber
;
740 super::setProperty("IOCPUNumber", _cpuNumber
, 32);
744 IOCPU::getCPUState(void)
750 IOCPU::setCPUState(UInt32 cpuState
)
752 if (cpuState
< kIOCPUStateCount
) {
753 _cpuState
= cpuState
;
758 IOCPU::getCPUGroup(void)
764 IOCPU::getCPUGroupSize(void)
766 return _cpuGroup
->getCount();
770 IOCPU::getMachProcessor(void)
772 return machProcessor
;
776 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
779 #define super IOInterruptController
781 OSDefineMetaClassAndStructors(IOCPUInterruptController
, IOInterruptController
);
783 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 1);
784 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 2);
785 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 3);
786 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 4);
787 OSMetaClassDefineReservedUnused(IOCPUInterruptController
, 5);
791 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
794 IOCPUInterruptController::initCPUInterruptController(int sources
)
796 return initCPUInterruptController(sources
, sources
);
800 IOCPUInterruptController::initCPUInterruptController(int sources
, int cpus
)
804 if (!super::init()) {
805 return kIOReturnInvalid
;
808 numSources
= sources
;
811 vectors
= (IOInterruptVector
*)IOMalloc(numSources
* sizeof(IOInterruptVector
));
813 return kIOReturnNoMemory
;
815 bzero(vectors
, numSources
* sizeof(IOInterruptVector
));
817 // Allocate a lock for each vector
818 for (cnt
= 0; cnt
< numSources
; cnt
++) {
819 vectors
[cnt
].interruptLock
= IOLockAlloc();
820 if (vectors
[cnt
].interruptLock
== NULL
) {
821 for (cnt
= 0; cnt
< numSources
; cnt
++) {
822 if (vectors
[cnt
].interruptLock
!= NULL
) {
823 IOLockFree(vectors
[cnt
].interruptLock
);
826 return kIOReturnNoResources
;
830 ml_init_max_cpus(numSources
);
834 * kperf allocates based on the number of CPUs and requires them to all be
837 boolean_t found_kperf
= FALSE
;
838 char kperf_config_str
[64];
839 found_kperf
= PE_parse_boot_arg_str("kperf", kperf_config_str
, sizeof(kperf_config_str
));
840 if (found_kperf
&& kperf_config_str
[0] != '\0') {
841 kperf_kernel_configure(kperf_config_str
);
845 return kIOReturnSuccess
;
849 IOCPUInterruptController::registerCPUInterruptController(void)
853 getPlatform()->registerInterruptController(gPlatformInterruptControllerName
,
858 IOCPUInterruptController::setCPUInterruptProperties(IOService
*service
)
866 if ((service
->getProperty(gIOInterruptControllersKey
) != 0) &&
867 (service
->getProperty(gIOInterruptSpecifiersKey
) != 0)) {
871 // Create the interrupt specifer array.
872 specifier
= OSArray::withCapacity(numSources
);
873 for (cnt
= 0; cnt
< numSources
; cnt
++) {
875 tmpData
= OSData::withBytes(&tmpLong
, sizeof(tmpLong
));
876 specifier
->setObject(tmpData
);
881 // Create the interrupt controller array.
882 controller
= OSArray::withCapacity(numSources
);
883 for (cnt
= 0; cnt
< numSources
; cnt
++) {
884 controller
->setObject(gPlatformInterruptControllerName
);
887 // Put the two arrays into the property table.
888 service
->setProperty(gIOInterruptControllersKey
, controller
);
889 service
->setProperty(gIOInterruptSpecifiersKey
, specifier
);
890 controller
->release();
891 specifier
->release();
895 IOCPUInterruptController::enableCPUInterrupt(IOCPU
*cpu
)
897 IOInterruptHandler handler
= OSMemberFunctionCast(
898 IOInterruptHandler
, this, &IOCPUInterruptController::handleInterrupt
);
902 ml_install_interrupt_handler(cpu
, cpu
->getCPUNumber(), this, handler
, 0);
904 IOTakeLock(vectors
[0].interruptLock
);
907 if (enabledCPUs
== numCPUs
) {
908 IOService::cpusRunning();
911 IOUnlock(vectors
[0].interruptLock
);
915 IOCPUInterruptController::registerInterrupt(IOService
*nub
,
918 IOInterruptHandler handler
,
921 IOInterruptVector
*vector
;
923 if (source
>= numSources
) {
924 return kIOReturnNoResources
;
927 vector
= &vectors
[source
];
929 // Get the lock for this vector.
930 IOTakeLock(vector
->interruptLock
);
932 // Make sure the vector is not in use.
933 if (vector
->interruptRegistered
) {
934 IOUnlock(vector
->interruptLock
);
935 return kIOReturnNoResources
;
938 // Fill in vector with the client's info.
939 vector
->handler
= handler
;
941 vector
->source
= source
;
942 vector
->target
= target
;
943 vector
->refCon
= refCon
;
945 // Get the vector ready. It starts hard disabled.
946 vector
->interruptDisabledHard
= 1;
947 vector
->interruptDisabledSoft
= 1;
948 vector
->interruptRegistered
= 1;
950 IOUnlock(vector
->interruptLock
);
952 IOTakeLock(vectors
[0].interruptLock
);
953 if (enabledCPUs
!= numCPUs
) {
954 assert_wait(this, THREAD_UNINT
);
955 IOUnlock(vectors
[0].interruptLock
);
956 thread_block(THREAD_CONTINUE_NULL
);
958 IOUnlock(vectors
[0].interruptLock
);
961 return kIOReturnSuccess
;
965 IOCPUInterruptController::getInterruptType(IOService */
*nub*/
,
969 if (interruptType
== 0) {
970 return kIOReturnBadArgument
;
973 *interruptType
= kIOInterruptTypeLevel
;
975 return kIOReturnSuccess
;
979 IOCPUInterruptController::enableInterrupt(IOService */
*nub*/
,
982 // ml_set_interrupts_enabled(true);
983 return kIOReturnSuccess
;
987 IOCPUInterruptController::disableInterrupt(IOService */
*nub*/
,
990 // ml_set_interrupts_enabled(false);
991 return kIOReturnSuccess
;
995 IOCPUInterruptController::causeInterrupt(IOService */
*nub*/
,
998 ml_cause_interrupt();
999 return kIOReturnSuccess
;
1003 IOCPUInterruptController::handleInterrupt(void */
*refCon*/
,
1007 IOInterruptVector
*vector
;
1009 vector
= &vectors
[source
];
1011 if (!vector
->interruptRegistered
) {
1012 return kIOReturnInvalid
;
1015 vector
->handler(vector
->target
, vector
->refCon
,
1016 vector
->nub
, vector
->source
);
1018 return kIOReturnSuccess
;
1021 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */