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