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