xref: /xnu-8019.80.24/iokit/Kernel/IOCPU.cpp (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
1 /*
2  * Copyright (c) 1999-2016 Apple Inc.  All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #define IOKIT_ENABLE_SHARED_PTR
30 
31 extern "C" {
32 #include <pexpert/pexpert.h>
33 #include <kern/cpu_number.h>
34 extern void kperf_kernel_configure(char *);
35 }
36 
37 #include <machine/machine_routines.h>
38 #include <IOKit/IOLib.h>
39 #include <IOKit/IOPlatformExpert.h>
40 #include <IOKit/pwr_mgt/RootDomain.h>
41 #include <IOKit/pwr_mgt/IOPMPrivate.h>
42 #include <libkern/c++/OSSharedPtr.h>
43 #include <IOKit/IOUserClient.h>
44 #include <IOKit/IOKitKeysPrivate.h>
45 #include <IOKit/IOCPU.h>
46 #include "IOKitKernelInternal.h"
47 
48 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
49 
50 #include <kern/queue.h>
51 #include <kern/sched_prim.h>
52 
53 extern "C" void console_suspend();
54 extern "C" void console_resume();
55 extern "C" void sched_override_recommended_cores_for_sleep(void);
56 extern "C" void sched_restore_recommended_cores_after_sleep(void);
57 
58 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
59 
60 static IOLock *gIOCPUsLock;
61 static OSSharedPtr<OSArray> gIOCPUs;
62 static OSSharedPtr<const OSSymbol> gIOCPUStateKey;
63 static OSSharedPtr<OSString> gIOCPUStateNames[kIOCPUStateCount];
64 
65 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
66 
67 #if !USE_APPLEARMSMP
68 
69 void
IOCPUInitialize(void)70 IOCPUInitialize(void)
71 {
72 	gIOCPUsLock = IOLockAlloc();
73 	gIOCPUs     = OSArray::withCapacity(1);
74 
75 	gIOCPUStateKey = OSSymbol::withCStringNoCopy("IOCPUState");
76 
77 	gIOCPUStateNames[kIOCPUStateUnregistered] =
78 	    OSString::withCStringNoCopy("Unregistered");
79 	gIOCPUStateNames[kIOCPUStateUninitalized] =
80 	    OSString::withCStringNoCopy("Uninitalized");
81 	gIOCPUStateNames[kIOCPUStateStopped] =
82 	    OSString::withCStringNoCopy("Stopped");
83 	gIOCPUStateNames[kIOCPUStateRunning] =
84 	    OSString::withCStringNoCopy("Running");
85 }
86 
87 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
88 
89 kern_return_t
PE_cpu_start(cpu_id_t target,vm_offset_t start_paddr,vm_offset_t arg_paddr)90 PE_cpu_start(cpu_id_t target,
91     vm_offset_t start_paddr, vm_offset_t arg_paddr)
92 {
93 	IOCPU *targetCPU = (IOCPU *)target;
94 
95 	if (targetCPU == NULL) {
96 		return KERN_FAILURE;
97 	}
98 	return targetCPU->startCPU(start_paddr, arg_paddr);
99 }
100 
101 void
PE_cpu_halt(cpu_id_t target)102 PE_cpu_halt(cpu_id_t target)
103 {
104 	IOCPU *targetCPU = (IOCPU *)target;
105 
106 	targetCPU->haltCPU();
107 }
108 
109 void
PE_cpu_signal(cpu_id_t source,cpu_id_t target)110 PE_cpu_signal(cpu_id_t source, cpu_id_t target)
111 {
112 	IOCPU *sourceCPU = (IOCPU *)source;
113 	IOCPU *targetCPU = (IOCPU *)target;
114 
115 	sourceCPU->signalCPU(targetCPU);
116 }
117 
118 void
PE_cpu_signal_deferred(cpu_id_t source,cpu_id_t target)119 PE_cpu_signal_deferred(cpu_id_t source, cpu_id_t target)
120 {
121 	IOCPU *sourceCPU = (IOCPU *)source;
122 	IOCPU *targetCPU = (IOCPU *)target;
123 
124 	sourceCPU->signalCPUDeferred(targetCPU);
125 }
126 
127 void
PE_cpu_signal_cancel(cpu_id_t source,cpu_id_t target)128 PE_cpu_signal_cancel(cpu_id_t source, cpu_id_t target)
129 {
130 	IOCPU *sourceCPU = (IOCPU *)source;
131 	IOCPU *targetCPU = (IOCPU *)target;
132 
133 	sourceCPU->signalCPUCancel(targetCPU);
134 }
135 
136 void
PE_cpu_machine_init(cpu_id_t target,boolean_t bootb)137 PE_cpu_machine_init(cpu_id_t target, boolean_t bootb)
138 {
139 	IOCPU *targetCPU = OSDynamicCast(IOCPU, (OSObject *)target);
140 
141 	if (targetCPU == NULL) {
142 		panic("%s: invalid target CPU %p", __func__, target);
143 	}
144 
145 	targetCPU->initCPU(bootb);
146 #if defined(__arm__) || defined(__arm64__)
147 	if (!bootb && (targetCPU->getCPUNumber() == (UInt32)master_cpu)) {
148 		ml_set_is_quiescing(false);
149 	}
150 #endif /* defined(__arm__) || defined(__arm64__) */
151 }
152 
153 void
PE_cpu_machine_quiesce(cpu_id_t target)154 PE_cpu_machine_quiesce(cpu_id_t target)
155 {
156 	IOCPU *targetCPU = (IOCPU*)target;
157 #if defined(__arm__) || defined(__arm64__)
158 	if (targetCPU->getCPUNumber() == (UInt32)master_cpu) {
159 		ml_set_is_quiescing(true);
160 	}
161 #endif /* defined(__arm__) || defined(__arm64__) */
162 	targetCPU->quiesceCPU();
163 }
164 
165 #if defined(__arm__) || defined(__arm64__)
166 static perfmon_interrupt_handler_func pmi_handler = NULL;
167 
168 kern_return_t
PE_cpu_perfmon_interrupt_install_handler(perfmon_interrupt_handler_func handler)169 PE_cpu_perfmon_interrupt_install_handler(perfmon_interrupt_handler_func handler)
170 {
171 	pmi_handler = handler;
172 
173 	return KERN_SUCCESS;
174 }
175 
176 void
PE_cpu_perfmon_interrupt_enable(cpu_id_t target,boolean_t enable)177 PE_cpu_perfmon_interrupt_enable(cpu_id_t target, boolean_t enable)
178 {
179 	IOCPU *targetCPU = (IOCPU*)target;
180 
181 	if (targetCPU == nullptr) {
182 		return;
183 	}
184 
185 	if (enable) {
186 		targetCPU->getProvider()->registerInterrupt(1, targetCPU, (IOInterruptAction)pmi_handler, NULL);
187 		targetCPU->getProvider()->enableInterrupt(1);
188 	} else {
189 		targetCPU->getProvider()->disableInterrupt(1);
190 	}
191 }
192 #endif
193 
194 #endif /* !USE_APPLEARMSMP */
195 
196 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
197 
198 #define super IOService
199 
200 OSDefineMetaClassAndAbstractStructors(IOCPU, IOService);
201 OSMetaClassDefineReservedUnused(IOCPU, 0);
202 OSMetaClassDefineReservedUnused(IOCPU, 1);
203 OSMetaClassDefineReservedUnused(IOCPU, 2);
204 OSMetaClassDefineReservedUnused(IOCPU, 3);
205 OSMetaClassDefineReservedUnused(IOCPU, 4);
206 OSMetaClassDefineReservedUnused(IOCPU, 5);
207 OSMetaClassDefineReservedUnused(IOCPU, 6);
208 OSMetaClassDefineReservedUnused(IOCPU, 7);
209 
210 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
211 
212 #if !USE_APPLEARMSMP
213 void
IOCPUSleepKernel(void)214 IOCPUSleepKernel(void)
215 {
216 #if defined(__x86_64__)
217 	extern IOCPU *currentShutdownTarget;
218 #endif
219 	unsigned int cnt, numCPUs;
220 	IOCPU *target;
221 	IOCPU *bootCPU = NULL;
222 	IOPMrootDomain  *rootDomain = IOService::getPMRootDomain();
223 
224 	printf("IOCPUSleepKernel enter\n");
225 #if defined(__arm64__)
226 	sched_override_recommended_cores_for_sleep();
227 #endif
228 
229 	rootDomain->tracePoint( kIOPMTracePointSleepPlatformActions );
230 	IOPlatformActionsPreSleep();
231 	rootDomain->tracePoint( kIOPMTracePointSleepCPUs );
232 
233 	numCPUs = gIOCPUs->getCount();
234 #if defined(__x86_64__)
235 	currentShutdownTarget = NULL;
236 #endif
237 
238 	integer_t old_pri;
239 	thread_t self = current_thread();
240 
241 	/*
242 	 * We need to boost this thread's priority to the maximum kernel priority to
243 	 * ensure we can urgently preempt ANY thread currently executing on the
244 	 * target CPU.  Note that realtime threads have their own mechanism to eventually
245 	 * demote their priority below MAXPRI_KERNEL if they hog the CPU for too long.
246 	 */
247 	old_pri = thread_kern_get_pri(self);
248 	thread_kern_set_pri(self, thread_kern_get_kernel_maxpri());
249 
250 	// Sleep the CPUs.
251 	ml_set_is_quiescing(true);
252 	cnt = numCPUs;
253 	while (cnt--) {
254 		target = OSDynamicCast(IOCPU, gIOCPUs->getObject(cnt));
255 
256 		// We make certain that the bootCPU is the last to sleep
257 		// We'll skip it for now, and halt it after finishing the
258 		// non-boot CPU's.
259 		if (target->getCPUNumber() == (UInt32)master_cpu) {
260 			bootCPU = target;
261 		} else if (target->getCPUState() == kIOCPUStateRunning) {
262 #if defined(__x86_64__)
263 			currentShutdownTarget = target;
264 #endif
265 			target->haltCPU();
266 		}
267 	}
268 
269 	assert(bootCPU != NULL);
270 	assert(cpu_number() == master_cpu);
271 
272 	console_suspend();
273 
274 	rootDomain->tracePoint( kIOPMTracePointSleepPlatformDriver );
275 	rootDomain->stop_watchdog_timer();
276 
277 	/*
278 	 * Now sleep the boot CPU, including calling the kQueueQuiesce actions.
279 	 * The system sleeps here.
280 	 */
281 
282 	bootCPU->haltCPU();
283 	ml_set_is_quiescing(false);
284 
285 	/*
286 	 * The system is now coming back from sleep on the boot CPU.
287 	 * The kQueueActive actions have already been called.
288 	 */
289 
290 	rootDomain->start_watchdog_timer();
291 	rootDomain->tracePoint( kIOPMTracePointWakePlatformActions );
292 
293 	console_resume();
294 
295 	IOPlatformActionsPostResume();
296 	rootDomain->tracePoint( kIOPMTracePointWakeCPUs );
297 
298 	// Wake the other CPUs.
299 	for (cnt = 0; cnt < numCPUs; cnt++) {
300 		target = OSDynamicCast(IOCPU, gIOCPUs->getObject(cnt));
301 
302 		// Skip the already-woken boot CPU.
303 		if (target->getCPUNumber() != (UInt32)master_cpu) {
304 			if (target->getCPUState() == kIOCPUStateRunning) {
305 				panic("Spurious wakeup of cpu %u", (unsigned int)(target->getCPUNumber()));
306 			}
307 
308 			if (target->getCPUState() == kIOCPUStateStopped) {
309 				processor_start(target->getMachProcessor());
310 			}
311 		}
312 	}
313 
314 #if defined(__arm64__)
315 	sched_restore_recommended_cores_after_sleep();
316 #endif
317 
318 	thread_kern_set_pri(self, old_pri);
319 	printf("IOCPUSleepKernel exit\n");
320 }
321 
322 static bool
is_IOCPU_disabled(void)323 is_IOCPU_disabled(void)
324 {
325 	return false;
326 }
327 #else /* !USE_APPLEARMSMP */
328 static bool
is_IOCPU_disabled(void)329 is_IOCPU_disabled(void)
330 {
331 	return true;
332 }
333 #endif /* !USE_APPLEARMSMP */
334 
335 bool
start(IOService * provider)336 IOCPU::start(IOService *provider)
337 {
338 	if (is_IOCPU_disabled()) {
339 		return false;
340 	}
341 
342 	if (!super::start(provider)) {
343 		return false;
344 	}
345 
346 	_cpuGroup = gIOCPUs;
347 	cpuNub = provider;
348 
349 	IOLockLock(gIOCPUsLock);
350 	gIOCPUs->setObject(this);
351 	IOLockUnlock(gIOCPUsLock);
352 
353 	// Correct the bus, cpu and timebase frequencies in the device tree.
354 	if (gPEClockFrequencyInfo.bus_frequency_hz < 0x100000000ULL) {
355 		OSSharedPtr<OSData> busFrequency = OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo.bus_clock_rate_hz, 4);
356 		provider->setProperty("bus-frequency", busFrequency.get());
357 	} else {
358 		OSSharedPtr<OSData> busFrequency = OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo.bus_frequency_hz, 8);
359 		provider->setProperty("bus-frequency", busFrequency.get());
360 	}
361 
362 	if (gPEClockFrequencyInfo.cpu_frequency_hz < 0x100000000ULL) {
363 		OSSharedPtr<OSData> cpuFrequency = OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo.cpu_clock_rate_hz, 4);
364 		provider->setProperty("clock-frequency", cpuFrequency.get());
365 	} else {
366 		OSSharedPtr<OSData> cpuFrequency = OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo.cpu_frequency_hz, 8);
367 		provider->setProperty("clock-frequency", cpuFrequency.get());
368 	}
369 
370 	OSSharedPtr<OSData> timebaseFrequency = OSData::withBytesNoCopy((void *)&gPEClockFrequencyInfo.timebase_frequency_hz, 4);
371 	provider->setProperty("timebase-frequency", timebaseFrequency.get());
372 
373 	super::setProperty("IOCPUID", getRegistryEntryID(), sizeof(uint64_t) * 8);
374 
375 	setCPUNumber(0);
376 	setCPUState(kIOCPUStateUnregistered);
377 
378 	return true;
379 }
380 
381 void
detach(IOService * provider)382 IOCPU::detach(IOService *provider)
383 {
384 	if (is_IOCPU_disabled()) {
385 		return;
386 	}
387 
388 	super::detach(provider);
389 	IOLockLock(gIOCPUsLock);
390 	unsigned int index = gIOCPUs->getNextIndexOfObject(this, 0);
391 	if (index != (unsigned int)-1) {
392 		gIOCPUs->removeObject(index);
393 	}
394 	IOLockUnlock(gIOCPUsLock);
395 }
396 
397 OSObject *
getProperty(const OSSymbol * aKey) const398 IOCPU::getProperty(const OSSymbol *aKey) const
399 {
400 	if (aKey == gIOCPUStateKey) {
401 		return gIOCPUStateNames[_cpuState].get();
402 	}
403 #pragma clang diagnostic push
404 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
405 	return super::getProperty(aKey);
406 #pragma clang diagnostic pop
407 }
408 
409 bool
setProperty(const OSSymbol * aKey,OSObject * anObject)410 IOCPU::setProperty(const OSSymbol *aKey, OSObject *anObject)
411 {
412 	if (aKey == gIOCPUStateKey) {
413 		return false;
414 	}
415 
416 	return super::setProperty(aKey, anObject);
417 }
418 
419 bool
serializeProperties(OSSerialize * serialize) const420 IOCPU::serializeProperties(OSSerialize *serialize) const
421 {
422 	bool result;
423 	OSSharedPtr<OSDictionary> dict = dictionaryWithProperties();
424 	if (!dict) {
425 		return false;
426 	}
427 	dict->setObject(gIOCPUStateKey.get(), gIOCPUStateNames[_cpuState].get());
428 	result = dict->serialize(serialize);
429 	return result;
430 }
431 
432 IOReturn
setProperties(OSObject * properties)433 IOCPU::setProperties(OSObject *properties)
434 {
435 	OSDictionary *dict = OSDynamicCast(OSDictionary, properties);
436 	OSString     *stateStr;
437 	IOReturn     result;
438 
439 	if (dict == NULL) {
440 		return kIOReturnUnsupported;
441 	}
442 
443 	stateStr = OSDynamicCast(OSString, dict->getObject(gIOCPUStateKey.get()));
444 	if (stateStr != NULL) {
445 		result = IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator);
446 		if (result != kIOReturnSuccess) {
447 			return result;
448 		}
449 
450 		if (setProperty(gIOCPUStateKey.get(), stateStr)) {
451 			return kIOReturnSuccess;
452 		}
453 
454 		return kIOReturnUnsupported;
455 	}
456 
457 	return kIOReturnUnsupported;
458 }
459 
460 void
signalCPU(IOCPU *)461 IOCPU::signalCPU(IOCPU */*target*/)
462 {
463 }
464 
465 void
signalCPUDeferred(IOCPU * target)466 IOCPU::signalCPUDeferred(IOCPU *target)
467 {
468 	// Our CPU may not support deferred IPIs,
469 	// so send a regular IPI by default
470 	signalCPU(target);
471 }
472 
473 void
signalCPUCancel(IOCPU *)474 IOCPU::signalCPUCancel(IOCPU */*target*/)
475 {
476 	// Meant to cancel signals sent by
477 	// signalCPUDeferred; unsupported
478 	// by default
479 }
480 
481 void
enableCPUTimeBase(bool)482 IOCPU::enableCPUTimeBase(bool /*enable*/)
483 {
484 }
485 
486 UInt32
getCPUNumber(void)487 IOCPU::getCPUNumber(void)
488 {
489 	return _cpuNumber;
490 }
491 
492 void
setCPUNumber(UInt32 cpuNumber)493 IOCPU::setCPUNumber(UInt32 cpuNumber)
494 {
495 	_cpuNumber = cpuNumber;
496 	super::setProperty("IOCPUNumber", _cpuNumber, 32);
497 }
498 
499 UInt32
getCPUState(void)500 IOCPU::getCPUState(void)
501 {
502 	return _cpuState;
503 }
504 
505 void
setCPUState(UInt32 cpuState)506 IOCPU::setCPUState(UInt32 cpuState)
507 {
508 	if (cpuState < kIOCPUStateCount) {
509 		_cpuState = cpuState;
510 	}
511 }
512 
513 OSArray *
getCPUGroup(void)514 IOCPU::getCPUGroup(void)
515 {
516 	return _cpuGroup.get();
517 }
518 
519 UInt32
getCPUGroupSize(void)520 IOCPU::getCPUGroupSize(void)
521 {
522 	return _cpuGroup->getCount();
523 }
524 
525 processor_t
getMachProcessor(void)526 IOCPU::getMachProcessor(void)
527 {
528 	return machProcessor;
529 }
530 
531 
532 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
533 
534 #undef super
535 #define super IOInterruptController
536 
537 OSDefineMetaClassAndStructors(IOCPUInterruptController, IOInterruptController);
538 
539 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 1);
540 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 2);
541 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 3);
542 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 4);
543 OSMetaClassDefineReservedUnused(IOCPUInterruptController, 5);
544 
545 
546 
547 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
548 
549 IOReturn
initCPUInterruptController(int sources)550 IOCPUInterruptController::initCPUInterruptController(int sources)
551 {
552 	return initCPUInterruptController(sources, sources);
553 }
554 
555 IOReturn
initCPUInterruptController(int sources,int cpus)556 IOCPUInterruptController::initCPUInterruptController(int sources, int cpus)
557 {
558 	int cnt;
559 
560 	if (!super::init()) {
561 		return kIOReturnInvalid;
562 	}
563 
564 	numSources = sources;
565 	numCPUs = cpus;
566 
567 	vectors = (IOInterruptVector *)zalloc_permanent(numSources *
568 	    sizeof(IOInterruptVector), ZALIGN(IOInterruptVector));
569 
570 	// Allocate a lock for each vector
571 	for (cnt = 0; cnt < numSources; cnt++) {
572 		vectors[cnt].interruptLock = IOLockAlloc();
573 		if (vectors[cnt].interruptLock == NULL) {
574 			for (cnt = 0; cnt < numSources; cnt++) {
575 				if (vectors[cnt].interruptLock != NULL) {
576 					IOLockFree(vectors[cnt].interruptLock);
577 				}
578 			}
579 			return kIOReturnNoResources;
580 		}
581 	}
582 
583 	ml_set_max_cpus(numSources);
584 	return kIOReturnSuccess;
585 }
586 
587 void
registerCPUInterruptController(void)588 IOCPUInterruptController::registerCPUInterruptController(void)
589 {
590 	setProperty(gPlatformInterruptControllerName, kOSBooleanTrue);
591 	registerService();
592 
593 	getPlatform()->registerInterruptController(gPlatformInterruptControllerName,
594 	    this);
595 }
596 
597 void
setCPUInterruptProperties(IOService * service)598 IOCPUInterruptController::setCPUInterruptProperties(IOService *service)
599 {
600 	int          cnt;
601 	OSSharedPtr<OSArray> specifier;
602 	OSSharedPtr<OSArray> controller;
603 	long         tmpLong;
604 
605 	if ((service->propertyExists(gIOInterruptControllersKey)) &&
606 	    (service->propertyExists(gIOInterruptSpecifiersKey))) {
607 		return;
608 	}
609 
610 	// Create the interrupt specifer array.
611 	specifier = OSArray::withCapacity(numSources);
612 	for (cnt = 0; cnt < numSources; cnt++) {
613 		tmpLong = cnt;
614 		OSSharedPtr<OSData> tmpData = OSData::withBytes(&tmpLong, sizeof(tmpLong));
615 		specifier->setObject(tmpData.get());
616 	}
617 
618 	// Create the interrupt controller array.
619 	controller = OSArray::withCapacity(numSources);
620 	for (cnt = 0; cnt < numSources; cnt++) {
621 		controller->setObject(gPlatformInterruptControllerName);
622 	}
623 
624 	// Put the two arrays into the property table.
625 	service->setProperty(gIOInterruptControllersKey, controller.get());
626 	service->setProperty(gIOInterruptSpecifiersKey, specifier.get());
627 }
628 
629 void
enableCPUInterrupt(IOCPU * cpu)630 IOCPUInterruptController::enableCPUInterrupt(IOCPU *cpu)
631 {
632 	IOInterruptHandler handler = OSMemberFunctionCast(
633 		IOInterruptHandler, this, &IOCPUInterruptController::handleInterrupt);
634 
635 	assert(numCPUs > 0);
636 
637 	ml_install_interrupt_handler(cpu, cpu->getCPUNumber(), this, handler, NULL);
638 
639 	IOTakeLock(vectors[0].interruptLock);
640 	++enabledCPUs;
641 
642 	if (enabledCPUs == numCPUs) {
643 		IOService::cpusRunning();
644 		thread_wakeup(this);
645 	}
646 	IOUnlock(vectors[0].interruptLock);
647 }
648 
649 IOReturn
registerInterrupt(IOService * nub,int source,void * target,IOInterruptHandler handler,void * refCon)650 IOCPUInterruptController::registerInterrupt(IOService *nub,
651     int source,
652     void *target,
653     IOInterruptHandler handler,
654     void *refCon)
655 {
656 	IOInterruptVector *vector;
657 
658 	// Interrupts must be enabled, as this can allocate memory.
659 	assert(ml_get_interrupts_enabled() == TRUE);
660 
661 	if (source >= numSources) {
662 		return kIOReturnNoResources;
663 	}
664 
665 	vector = &vectors[source];
666 
667 	// Get the lock for this vector.
668 	IOTakeLock(vector->interruptLock);
669 
670 	// Make sure the vector is not in use.
671 	if (vector->interruptRegistered) {
672 		IOUnlock(vector->interruptLock);
673 		return kIOReturnNoResources;
674 	}
675 
676 	// Fill in vector with the client's info.
677 	vector->handler = handler;
678 	vector->nub     = nub;
679 	vector->source  = source;
680 	vector->target  = target;
681 	vector->refCon  = refCon;
682 
683 	// Get the vector ready.  It starts hard disabled.
684 	vector->interruptDisabledHard = 1;
685 	vector->interruptDisabledSoft = 1;
686 	vector->interruptRegistered   = 1;
687 
688 	IOUnlock(vector->interruptLock);
689 
690 	IOTakeLock(vectors[0].interruptLock);
691 	if (enabledCPUs != numCPUs) {
692 		assert_wait(this, THREAD_UNINT);
693 		IOUnlock(vectors[0].interruptLock);
694 		thread_block(THREAD_CONTINUE_NULL);
695 	} else {
696 		IOUnlock(vectors[0].interruptLock);
697 	}
698 
699 	return kIOReturnSuccess;
700 }
701 
702 IOReturn
getInterruptType(IOService *,int,int * interruptType)703 IOCPUInterruptController::getInterruptType(IOService */*nub*/,
704     int /*source*/,
705     int *interruptType)
706 {
707 	if (interruptType == NULL) {
708 		return kIOReturnBadArgument;
709 	}
710 
711 	*interruptType = kIOInterruptTypeLevel;
712 
713 	return kIOReturnSuccess;
714 }
715 
716 IOReturn
enableInterrupt(IOService *,int)717 IOCPUInterruptController::enableInterrupt(IOService */*nub*/,
718     int /*source*/)
719 {
720 //  ml_set_interrupts_enabled(true);
721 	return kIOReturnSuccess;
722 }
723 
724 IOReturn
disableInterrupt(IOService *,int)725 IOCPUInterruptController::disableInterrupt(IOService */*nub*/,
726     int /*source*/)
727 {
728 //  ml_set_interrupts_enabled(false);
729 	return kIOReturnSuccess;
730 }
731 
732 IOReturn
causeInterrupt(IOService *,int)733 IOCPUInterruptController::causeInterrupt(IOService */*nub*/,
734     int /*source*/)
735 {
736 	ml_cause_interrupt();
737 	return kIOReturnSuccess;
738 }
739 
740 IOReturn
handleInterrupt(void *,IOService *,int source)741 IOCPUInterruptController::handleInterrupt(void */*refCon*/,
742     IOService */*nub*/,
743     int source)
744 {
745 	IOInterruptVector *vector;
746 
747 	vector = &vectors[source];
748 
749 	if (!vector->interruptRegistered) {
750 		return kIOReturnInvalid;
751 	}
752 
753 	vector->handler(vector->target, vector->refCon,
754 	    vector->nub, vector->source);
755 
756 	return kIOReturnSuccess;
757 }
758 
759 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
760