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