xref: /xnu-10002.81.5/iokit/Kernel/PassthruInterruptController.cpp (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2019 Apple Computer, Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions extern "C" {
30*5e3eaea3SApple OSS Distributions #include <mach/task.h>
31*5e3eaea3SApple OSS Distributions #include <pexpert/pexpert.h>
32*5e3eaea3SApple OSS Distributions };
33*5e3eaea3SApple OSS Distributions 
34*5e3eaea3SApple OSS Distributions #include <machine/machine_routines.h>
35*5e3eaea3SApple OSS Distributions #include <IOKit/IOPlatformExpert.h>
36*5e3eaea3SApple OSS Distributions #include <IOKit/IOService.h>
37*5e3eaea3SApple OSS Distributions #include <IOKit/PassthruInterruptController.h>
38*5e3eaea3SApple OSS Distributions 
39*5e3eaea3SApple OSS Distributions #define super IOInterruptController
40*5e3eaea3SApple OSS Distributions OSDefineMetaClassAndStructors(PassthruInterruptController, IOInterruptController);
41*5e3eaea3SApple OSS Distributions 
42*5e3eaea3SApple OSS Distributions bool
init(void)43*5e3eaea3SApple OSS Distributions PassthruInterruptController::init(void)
44*5e3eaea3SApple OSS Distributions {
45*5e3eaea3SApple OSS Distributions 	if (!super::init() ||
46*5e3eaea3SApple OSS Distributions 	    !this->setProperty(gPlatformInterruptControllerName, kOSBooleanTrue) ||
47*5e3eaea3SApple OSS Distributions 	    !this->attach(getPlatform())) {
48*5e3eaea3SApple OSS Distributions 		return false;
49*5e3eaea3SApple OSS Distributions 	}
50*5e3eaea3SApple OSS Distributions 	registerService();
51*5e3eaea3SApple OSS Distributions 	if (getPlatform()->registerInterruptController(gPlatformInterruptControllerName, this) != kIOReturnSuccess) {
52*5e3eaea3SApple OSS Distributions 		return false;
53*5e3eaea3SApple OSS Distributions 	}
54*5e3eaea3SApple OSS Distributions 	if (semaphore_create(kernel_task, &child_sentinel, SYNC_POLICY_FIFO, 0) != KERN_SUCCESS) {
55*5e3eaea3SApple OSS Distributions 		return false;
56*5e3eaea3SApple OSS Distributions 	}
57*5e3eaea3SApple OSS Distributions 	return true;
58*5e3eaea3SApple OSS Distributions }
59*5e3eaea3SApple OSS Distributions 
60*5e3eaea3SApple OSS Distributions void
setCPUInterruptProperties(IOService * service)61*5e3eaea3SApple OSS Distributions PassthruInterruptController::setCPUInterruptProperties(IOService *service)
62*5e3eaea3SApple OSS Distributions {
63*5e3eaea3SApple OSS Distributions 	if ((service->getProperty(gIOInterruptControllersKey) != NULL) &&
64*5e3eaea3SApple OSS Distributions 	    (service->getProperty(gIOInterruptSpecifiersKey) != NULL)) {
65*5e3eaea3SApple OSS Distributions 		return;
66*5e3eaea3SApple OSS Distributions 	}
67*5e3eaea3SApple OSS Distributions 
68*5e3eaea3SApple OSS Distributions 	long         zero = 0;
69*5e3eaea3SApple OSS Distributions 	OSArray *specifier = OSArray::withCapacity(1);
70*5e3eaea3SApple OSS Distributions 	OSData *tmpData = OSData::withValue(zero);
71*5e3eaea3SApple OSS Distributions 	specifier->setObject(tmpData);
72*5e3eaea3SApple OSS Distributions 	tmpData->release();
73*5e3eaea3SApple OSS Distributions 	service->setProperty(gIOInterruptSpecifiersKey, specifier);
74*5e3eaea3SApple OSS Distributions 	specifier->release();
75*5e3eaea3SApple OSS Distributions 
76*5e3eaea3SApple OSS Distributions 	OSArray *controller = OSArray::withCapacity(1);
77*5e3eaea3SApple OSS Distributions 	controller->setObject(gPlatformInterruptControllerName);
78*5e3eaea3SApple OSS Distributions 	service->setProperty(gIOInterruptControllersKey, controller);
79*5e3eaea3SApple OSS Distributions 	controller->release();
80*5e3eaea3SApple OSS Distributions }
81*5e3eaea3SApple OSS Distributions 
82*5e3eaea3SApple OSS Distributions IOReturn
registerInterrupt(IOService * nub,int source,void * target,IOInterruptHandler handler,void * refCon)83*5e3eaea3SApple OSS Distributions PassthruInterruptController::registerInterrupt(IOService *nub,
84*5e3eaea3SApple OSS Distributions     int source,
85*5e3eaea3SApple OSS Distributions     void *target,
86*5e3eaea3SApple OSS Distributions     IOInterruptHandler handler,
87*5e3eaea3SApple OSS Distributions     void *refCon)
88*5e3eaea3SApple OSS Distributions {
89*5e3eaea3SApple OSS Distributions 	child_handler = handler;
90*5e3eaea3SApple OSS Distributions 	child_nub = nub;
91*5e3eaea3SApple OSS Distributions 	child_target = target;
92*5e3eaea3SApple OSS Distributions 	child_refCon = refCon;
93*5e3eaea3SApple OSS Distributions 
94*5e3eaea3SApple OSS Distributions 	// Wake up waitForChildController() to tell it that AIC is registered
95*5e3eaea3SApple OSS Distributions 	semaphore_signal(child_sentinel);
96*5e3eaea3SApple OSS Distributions 	return kIOReturnSuccess;
97*5e3eaea3SApple OSS Distributions }
98*5e3eaea3SApple OSS Distributions 
99*5e3eaea3SApple OSS Distributions void *
waitForChildController(void)100*5e3eaea3SApple OSS Distributions PassthruInterruptController::waitForChildController(void)
101*5e3eaea3SApple OSS Distributions {
102*5e3eaea3SApple OSS Distributions 	// Block if child controller isn't registered yet.  Assumes that this
103*5e3eaea3SApple OSS Distributions 	// is only called from one place.
104*5e3eaea3SApple OSS Distributions 	semaphore_wait(child_sentinel);
105*5e3eaea3SApple OSS Distributions 
106*5e3eaea3SApple OSS Distributions 	// NOTE: Assumes that AppleInterruptController passes |this| as the target argument.
107*5e3eaea3SApple OSS Distributions 	return child_target;
108*5e3eaea3SApple OSS Distributions }
109*5e3eaea3SApple OSS Distributions 
110*5e3eaea3SApple OSS Distributions IOReturn
getInterruptType(IOService *,int,int * interruptType)111*5e3eaea3SApple OSS Distributions PassthruInterruptController::getInterruptType(IOService */*nub*/,
112*5e3eaea3SApple OSS Distributions     int /*source*/,
113*5e3eaea3SApple OSS Distributions     int *interruptType)
114*5e3eaea3SApple OSS Distributions {
115*5e3eaea3SApple OSS Distributions 	if (interruptType == NULL) {
116*5e3eaea3SApple OSS Distributions 		return kIOReturnBadArgument;
117*5e3eaea3SApple OSS Distributions 	}
118*5e3eaea3SApple OSS Distributions 
119*5e3eaea3SApple OSS Distributions 	*interruptType = kIOInterruptTypeLevel;
120*5e3eaea3SApple OSS Distributions 
121*5e3eaea3SApple OSS Distributions 	return kIOReturnSuccess;
122*5e3eaea3SApple OSS Distributions }
123*5e3eaea3SApple OSS Distributions 
124*5e3eaea3SApple OSS Distributions IOReturn
enableInterrupt(IOService *,int)125*5e3eaea3SApple OSS Distributions PassthruInterruptController::enableInterrupt(IOService */*nub*/,
126*5e3eaea3SApple OSS Distributions     int /*source*/)
127*5e3eaea3SApple OSS Distributions {
128*5e3eaea3SApple OSS Distributions 	return kIOReturnSuccess;
129*5e3eaea3SApple OSS Distributions }
130*5e3eaea3SApple OSS Distributions 
131*5e3eaea3SApple OSS Distributions IOReturn
disableInterrupt(IOService *,int)132*5e3eaea3SApple OSS Distributions PassthruInterruptController::disableInterrupt(IOService */*nub*/,
133*5e3eaea3SApple OSS Distributions     int /*source*/)
134*5e3eaea3SApple OSS Distributions {
135*5e3eaea3SApple OSS Distributions 	return kIOReturnSuccess;
136*5e3eaea3SApple OSS Distributions }
137*5e3eaea3SApple OSS Distributions 
138*5e3eaea3SApple OSS Distributions IOReturn
causeInterrupt(IOService *,int)139*5e3eaea3SApple OSS Distributions PassthruInterruptController::causeInterrupt(IOService */*nub*/,
140*5e3eaea3SApple OSS Distributions     int /*source*/)
141*5e3eaea3SApple OSS Distributions {
142*5e3eaea3SApple OSS Distributions 	ml_cause_interrupt();
143*5e3eaea3SApple OSS Distributions 	return kIOReturnSuccess;
144*5e3eaea3SApple OSS Distributions }
145*5e3eaea3SApple OSS Distributions 
146*5e3eaea3SApple OSS Distributions IOReturn
handleInterrupt(void *,IOService *,int source)147*5e3eaea3SApple OSS Distributions PassthruInterruptController::handleInterrupt(void */*refCon*/,
148*5e3eaea3SApple OSS Distributions     IOService */*nub*/,
149*5e3eaea3SApple OSS Distributions     int source)
150*5e3eaea3SApple OSS Distributions {
151*5e3eaea3SApple OSS Distributions 	panic("handleInterrupt shouldn't be invoked directly");
152*5e3eaea3SApple OSS Distributions }
153*5e3eaea3SApple OSS Distributions 
154*5e3eaea3SApple OSS Distributions void
externalInterrupt(void)155*5e3eaea3SApple OSS Distributions PassthruInterruptController::externalInterrupt(void)
156*5e3eaea3SApple OSS Distributions {
157*5e3eaea3SApple OSS Distributions 	child_handler(child_target, child_refCon, child_nub, 0);
158*5e3eaea3SApple OSS Distributions }
159