xref: /xnu-8796.141.3/iokit/Kernel/IOInterruptController.cpp (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2007-2012 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  * Copyright (c) 1998-2006 Apple Computer, Inc. All rights reserved.
4*1b191cb5SApple OSS Distributions  *
5*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6*1b191cb5SApple OSS Distributions  *
7*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
8*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
9*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
10*1b191cb5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
11*1b191cb5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
12*1b191cb5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
13*1b191cb5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
14*1b191cb5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
15*1b191cb5SApple OSS Distributions  *
16*1b191cb5SApple OSS Distributions  * Please obtain a copy of the License at
17*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
18*1b191cb5SApple OSS Distributions  *
19*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
20*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
21*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
22*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
23*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
24*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
25*1b191cb5SApple OSS Distributions  * limitations under the License.
26*1b191cb5SApple OSS Distributions  *
27*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28*1b191cb5SApple OSS Distributions  */
29*1b191cb5SApple OSS Distributions 
30*1b191cb5SApple OSS Distributions #include <IOKit/IOLib.h>
31*1b191cb5SApple OSS Distributions #include <IOKit/IOService.h>
32*1b191cb5SApple OSS Distributions #include <IOKit/IOPlatformExpert.h>
33*1b191cb5SApple OSS Distributions #include <IOKit/IODeviceTreeSupport.h>
34*1b191cb5SApple OSS Distributions #include <IOKit/IOInterrupts.h>
35*1b191cb5SApple OSS Distributions #include <IOKit/IOInterruptController.h>
36*1b191cb5SApple OSS Distributions #include <IOKit/IOKitDebug.h>
37*1b191cb5SApple OSS Distributions #include <IOKit/IOTimeStamp.h>
38*1b191cb5SApple OSS Distributions 
39*1b191cb5SApple OSS Distributions 
40*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
41*1b191cb5SApple OSS Distributions 
42*1b191cb5SApple OSS Distributions #define super IOService
43*1b191cb5SApple OSS Distributions 
44*1b191cb5SApple OSS Distributions OSDefineMetaClassAndAbstractStructors(IOInterruptController, IOService);
45*1b191cb5SApple OSS Distributions 
46*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOInterruptController, 0);
47*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOInterruptController, 1);
48*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOInterruptController, 2);
49*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOInterruptController, 3);
50*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOInterruptController, 4);
51*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOInterruptController, 5);
52*1b191cb5SApple OSS Distributions 
53*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
54*1b191cb5SApple OSS Distributions 
55*1b191cb5SApple OSS Distributions IOReturn
registerInterrupt(IOService * nub,int source,void * target,IOInterruptHandler handler,void * refCon)56*1b191cb5SApple OSS Distributions IOInterruptController::registerInterrupt(IOService *nub, int source,
57*1b191cb5SApple OSS Distributions     void *target,
58*1b191cb5SApple OSS Distributions     IOInterruptHandler handler,
59*1b191cb5SApple OSS Distributions     void *refCon)
60*1b191cb5SApple OSS Distributions {
61*1b191cb5SApple OSS Distributions 	IOInterruptSource *interruptSources;
62*1b191cb5SApple OSS Distributions 	IOInterruptVectorNumber vectorNumber;
63*1b191cb5SApple OSS Distributions 	IOInterruptVector *vector;
64*1b191cb5SApple OSS Distributions 	int               wasDisabledSoft;
65*1b191cb5SApple OSS Distributions 	IOReturn          error;
66*1b191cb5SApple OSS Distributions 	OSData            *vectorData;
67*1b191cb5SApple OSS Distributions 	IOOptionBits      options;
68*1b191cb5SApple OSS Distributions 	bool              canBeShared, shouldBeShared, wasAlreadyRegisterd;
69*1b191cb5SApple OSS Distributions 
70*1b191cb5SApple OSS Distributions 	IOService         *originalNub = NULL;// Protected by wasAlreadyRegisterd
71*1b191cb5SApple OSS Distributions 	int               originalSource = 0;// Protected by wasAlreadyRegisterd
72*1b191cb5SApple OSS Distributions 
73*1b191cb5SApple OSS Distributions 
74*1b191cb5SApple OSS Distributions 	interruptSources = nub->_interruptSources;
75*1b191cb5SApple OSS Distributions 	vectorData = interruptSources[source].vectorData;
76*1b191cb5SApple OSS Distributions 	vectorNumber = *(IOInterruptVectorNumber *)vectorData->getBytesNoCopy();
77*1b191cb5SApple OSS Distributions 	vector = &vectors[vectorNumber];
78*1b191cb5SApple OSS Distributions 
79*1b191cb5SApple OSS Distributions 	// Get the lock for this vector.
80*1b191cb5SApple OSS Distributions 	IOLockLock(vector->interruptLock);
81*1b191cb5SApple OSS Distributions 
82*1b191cb5SApple OSS Distributions 	// Check if the interrupt source can/should be shared.
83*1b191cb5SApple OSS Distributions 	canBeShared = vectorCanBeShared(vectorNumber, vector);
84*1b191cb5SApple OSS Distributions 	IODTGetInterruptOptions(nub, source, &options);
85*1b191cb5SApple OSS Distributions #if defined(__i386__) || defined(__x86_64__)
86*1b191cb5SApple OSS Distributions 	int   interruptType;
87*1b191cb5SApple OSS Distributions 	if (OSDynamicCast(IOPlatformDevice, getProvider()) &&
88*1b191cb5SApple OSS Distributions 	    (getInterruptType(nub, source, &interruptType) == kIOReturnSuccess) &&
89*1b191cb5SApple OSS Distributions 	    (kIOInterruptTypeLevel & interruptType)) {
90*1b191cb5SApple OSS Distributions 		options |= kIODTInterruptShared;
91*1b191cb5SApple OSS Distributions 	}
92*1b191cb5SApple OSS Distributions #endif
93*1b191cb5SApple OSS Distributions 	shouldBeShared = canBeShared && (options & kIODTInterruptShared);
94*1b191cb5SApple OSS Distributions 	wasAlreadyRegisterd = vector->interruptRegistered;
95*1b191cb5SApple OSS Distributions 
96*1b191cb5SApple OSS Distributions 	// If the vector is registered and can not be shared return error.
97*1b191cb5SApple OSS Distributions 	if (wasAlreadyRegisterd && !canBeShared) {
98*1b191cb5SApple OSS Distributions 		IOLockUnlock(vector->interruptLock);
99*1b191cb5SApple OSS Distributions 		return kIOReturnNoResources;
100*1b191cb5SApple OSS Distributions 	}
101*1b191cb5SApple OSS Distributions 
102*1b191cb5SApple OSS Distributions 	// If this vector is already in use, and can be shared (implied),
103*1b191cb5SApple OSS Distributions 	// or it is not registered and should be shared,
104*1b191cb5SApple OSS Distributions 	// register as a shared interrupt.
105*1b191cb5SApple OSS Distributions 	if (wasAlreadyRegisterd || shouldBeShared) {
106*1b191cb5SApple OSS Distributions 		// If this vector is not already shared, break it out.
107*1b191cb5SApple OSS Distributions 		if (vector->sharedController == NULL) {
108*1b191cb5SApple OSS Distributions 			// Make the IOShareInterruptController instance
109*1b191cb5SApple OSS Distributions 			vector->sharedController = new IOSharedInterruptController;
110*1b191cb5SApple OSS Distributions 			if (vector->sharedController == NULL) {
111*1b191cb5SApple OSS Distributions 				IOLockUnlock(vector->interruptLock);
112*1b191cb5SApple OSS Distributions 				return kIOReturnNoMemory;
113*1b191cb5SApple OSS Distributions 			}
114*1b191cb5SApple OSS Distributions 
115*1b191cb5SApple OSS Distributions 			if (wasAlreadyRegisterd) {
116*1b191cb5SApple OSS Distributions 				// Save the nub and source for the original consumer.
117*1b191cb5SApple OSS Distributions 				originalNub = vector->nub;
118*1b191cb5SApple OSS Distributions 				originalSource = vector->source;
119*1b191cb5SApple OSS Distributions 
120*1b191cb5SApple OSS Distributions 				// Physically disable the interrupt, but mark it as being enabled in the hardware.
121*1b191cb5SApple OSS Distributions 				// The interruptDisabledSoft now indicates the driver's request for enablement.
122*1b191cb5SApple OSS Distributions 				disableVectorHard(vectorNumber, vector);
123*1b191cb5SApple OSS Distributions 				vector->interruptDisabledHard = 0;
124*1b191cb5SApple OSS Distributions 			}
125*1b191cb5SApple OSS Distributions 
126*1b191cb5SApple OSS Distributions 			// Initialize the new shared interrupt controller.
127*1b191cb5SApple OSS Distributions 			error = vector->sharedController->initInterruptController(this, vectorData);
128*1b191cb5SApple OSS Distributions 			// If the IOSharedInterruptController could not be initalized,
129*1b191cb5SApple OSS Distributions 			// if needed, put the original consumer's interrupt back to normal and
130*1b191cb5SApple OSS Distributions 			// get rid of whats left of the shared controller.
131*1b191cb5SApple OSS Distributions 			if (error != kIOReturnSuccess) {
132*1b191cb5SApple OSS Distributions 				if (wasAlreadyRegisterd) {
133*1b191cb5SApple OSS Distributions 					enableInterrupt(originalNub, originalSource);
134*1b191cb5SApple OSS Distributions 				}
135*1b191cb5SApple OSS Distributions 				vector->sharedController->release();
136*1b191cb5SApple OSS Distributions 				vector->sharedController = NULL;
137*1b191cb5SApple OSS Distributions 				IOLockUnlock(vector->interruptLock);
138*1b191cb5SApple OSS Distributions 				return error;
139*1b191cb5SApple OSS Distributions 			}
140*1b191cb5SApple OSS Distributions 
141*1b191cb5SApple OSS Distributions 			// If there was an original consumer try to register it on the shared controller.
142*1b191cb5SApple OSS Distributions 			if (wasAlreadyRegisterd) {
143*1b191cb5SApple OSS Distributions 				error = vector->sharedController->registerInterrupt(originalNub,
144*1b191cb5SApple OSS Distributions 				    originalSource,
145*1b191cb5SApple OSS Distributions 				    vector->target,
146*1b191cb5SApple OSS Distributions 				    vector->handler,
147*1b191cb5SApple OSS Distributions 				    vector->refCon);
148*1b191cb5SApple OSS Distributions 				// If the original consumer could not be moved to the shared controller,
149*1b191cb5SApple OSS Distributions 				// put the original consumor's interrupt back to normal and
150*1b191cb5SApple OSS Distributions 				// get rid of whats left of the shared controller.
151*1b191cb5SApple OSS Distributions 				if (error != kIOReturnSuccess) {
152*1b191cb5SApple OSS Distributions 					// Save the driver's interrupt enablement state.
153*1b191cb5SApple OSS Distributions 					wasDisabledSoft = vector->interruptDisabledSoft;
154*1b191cb5SApple OSS Distributions 
155*1b191cb5SApple OSS Distributions 					// Make the interrupt really hard disabled.
156*1b191cb5SApple OSS Distributions 					vector->interruptDisabledSoft = 1;
157*1b191cb5SApple OSS Distributions 					vector->interruptDisabledHard = 1;
158*1b191cb5SApple OSS Distributions 
159*1b191cb5SApple OSS Distributions 					// Enable the original consumer's interrupt if needed.
160*1b191cb5SApple OSS Distributions 					if (!wasDisabledSoft) {
161*1b191cb5SApple OSS Distributions 						originalNub->enableInterrupt(originalSource);
162*1b191cb5SApple OSS Distributions 					}
163*1b191cb5SApple OSS Distributions 					enableInterrupt(originalNub, originalSource);
164*1b191cb5SApple OSS Distributions 
165*1b191cb5SApple OSS Distributions 					vector->sharedController->release();
166*1b191cb5SApple OSS Distributions 					vector->sharedController = NULL;
167*1b191cb5SApple OSS Distributions 					IOLockUnlock(vector->interruptLock);
168*1b191cb5SApple OSS Distributions 					return error;
169*1b191cb5SApple OSS Distributions 				}
170*1b191cb5SApple OSS Distributions 			}
171*1b191cb5SApple OSS Distributions 
172*1b191cb5SApple OSS Distributions 			// Fill in vector with the shared controller's info.
173*1b191cb5SApple OSS Distributions 			vector->handler = (IOInterruptHandler)vector->sharedController->getInterruptHandlerAddress();
174*1b191cb5SApple OSS Distributions 			vector->nub     = vector->sharedController;
175*1b191cb5SApple OSS Distributions 			vector->source  = 0;
176*1b191cb5SApple OSS Distributions 			vector->target  = vector->sharedController;
177*1b191cb5SApple OSS Distributions 			vector->refCon  = NULL;
178*1b191cb5SApple OSS Distributions 
179*1b191cb5SApple OSS Distributions 			// If the interrupt was already registered,
180*1b191cb5SApple OSS Distributions 			// save the driver's interrupt enablement state.
181*1b191cb5SApple OSS Distributions 			if (wasAlreadyRegisterd) {
182*1b191cb5SApple OSS Distributions 				wasDisabledSoft = vector->interruptDisabledSoft;
183*1b191cb5SApple OSS Distributions 			} else {
184*1b191cb5SApple OSS Distributions 				wasDisabledSoft = true;
185*1b191cb5SApple OSS Distributions 			}
186*1b191cb5SApple OSS Distributions 
187*1b191cb5SApple OSS Distributions 			// Do any specific initalization for this vector if it has not yet been used.
188*1b191cb5SApple OSS Distributions 			if (!wasAlreadyRegisterd) {
189*1b191cb5SApple OSS Distributions 				initVector(vectorNumber, vector);
190*1b191cb5SApple OSS Distributions 			}
191*1b191cb5SApple OSS Distributions 
192*1b191cb5SApple OSS Distributions 			// Make the interrupt really hard disabled.
193*1b191cb5SApple OSS Distributions 			vector->interruptDisabledSoft = 1;
194*1b191cb5SApple OSS Distributions 			vector->interruptDisabledHard = 1;
195*1b191cb5SApple OSS Distributions 			vector->interruptRegistered   = 1;
196*1b191cb5SApple OSS Distributions 
197*1b191cb5SApple OSS Distributions 			// Enable the original consumer's interrupt if needed.
198*1b191cb5SApple OSS Distributions 			// originalNub is protected by wasAlreadyRegisterd here (see line 184).
199*1b191cb5SApple OSS Distributions 			if (!wasDisabledSoft) {
200*1b191cb5SApple OSS Distributions 				originalNub->enableInterrupt(originalSource);
201*1b191cb5SApple OSS Distributions 			}
202*1b191cb5SApple OSS Distributions 		}
203*1b191cb5SApple OSS Distributions 
204*1b191cb5SApple OSS Distributions 		error = vector->sharedController->registerInterrupt(nub, source, target,
205*1b191cb5SApple OSS Distributions 		    handler, refCon);
206*1b191cb5SApple OSS Distributions 		IOLockUnlock(vector->interruptLock);
207*1b191cb5SApple OSS Distributions 		return error;
208*1b191cb5SApple OSS Distributions 	}
209*1b191cb5SApple OSS Distributions 
210*1b191cb5SApple OSS Distributions 	// Fill in vector with the client's info.
211*1b191cb5SApple OSS Distributions 	vector->handler = handler;
212*1b191cb5SApple OSS Distributions 	vector->nub     = nub;
213*1b191cb5SApple OSS Distributions 	vector->source  = source;
214*1b191cb5SApple OSS Distributions 	vector->target  = target;
215*1b191cb5SApple OSS Distributions 	vector->refCon  = refCon;
216*1b191cb5SApple OSS Distributions 
217*1b191cb5SApple OSS Distributions 	// Do any specific initalization for this vector.
218*1b191cb5SApple OSS Distributions 	initVector(vectorNumber, vector);
219*1b191cb5SApple OSS Distributions 
220*1b191cb5SApple OSS Distributions 	// Get the vector ready.  It starts hard disabled.
221*1b191cb5SApple OSS Distributions 	vector->interruptDisabledHard = 1;
222*1b191cb5SApple OSS Distributions 	vector->interruptDisabledSoft = 1;
223*1b191cb5SApple OSS Distributions 	vector->interruptRegistered   = 1;
224*1b191cb5SApple OSS Distributions 
225*1b191cb5SApple OSS Distributions 	IOLockUnlock(vector->interruptLock);
226*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
227*1b191cb5SApple OSS Distributions }
228*1b191cb5SApple OSS Distributions 
229*1b191cb5SApple OSS Distributions IOReturn
unregisterInterrupt(IOService * nub,int source)230*1b191cb5SApple OSS Distributions IOInterruptController::unregisterInterrupt(IOService *nub, int source)
231*1b191cb5SApple OSS Distributions {
232*1b191cb5SApple OSS Distributions 	IOInterruptSource *interruptSources;
233*1b191cb5SApple OSS Distributions 	IOInterruptVectorNumber vectorNumber;
234*1b191cb5SApple OSS Distributions 	IOInterruptVector *vector;
235*1b191cb5SApple OSS Distributions 	OSData            *vectorData;
236*1b191cb5SApple OSS Distributions 
237*1b191cb5SApple OSS Distributions 	interruptSources = nub->_interruptSources;
238*1b191cb5SApple OSS Distributions 	vectorData = interruptSources[source].vectorData;
239*1b191cb5SApple OSS Distributions 	vectorNumber = *(IOInterruptVectorNumber *)vectorData->getBytesNoCopy();
240*1b191cb5SApple OSS Distributions 	vector = &vectors[vectorNumber];
241*1b191cb5SApple OSS Distributions 
242*1b191cb5SApple OSS Distributions 	// Get the lock for this vector.
243*1b191cb5SApple OSS Distributions 	IOLockLock(vector->interruptLock);
244*1b191cb5SApple OSS Distributions 
245*1b191cb5SApple OSS Distributions 	// Return success if it is not already registered
246*1b191cb5SApple OSS Distributions 	if (!vector->interruptRegistered) {
247*1b191cb5SApple OSS Distributions 		IOLockUnlock(vector->interruptLock);
248*1b191cb5SApple OSS Distributions 		return kIOReturnSuccess;
249*1b191cb5SApple OSS Distributions 	}
250*1b191cb5SApple OSS Distributions 
251*1b191cb5SApple OSS Distributions 	// Soft disable the source.
252*1b191cb5SApple OSS Distributions 	disableInterrupt(nub, source);
253*1b191cb5SApple OSS Distributions 
254*1b191cb5SApple OSS Distributions 	// Turn the source off at hardware.
255*1b191cb5SApple OSS Distributions 	disableVectorHard(vectorNumber, vector);
256*1b191cb5SApple OSS Distributions 
257*1b191cb5SApple OSS Distributions 	// Clear all the storage for the vector except for interruptLock.
258*1b191cb5SApple OSS Distributions 	vector->interruptActive = 0;
259*1b191cb5SApple OSS Distributions 	vector->interruptDisabledSoft = 0;
260*1b191cb5SApple OSS Distributions 	vector->interruptDisabledHard = 0;
261*1b191cb5SApple OSS Distributions 	vector->interruptRegistered = 0;
262*1b191cb5SApple OSS Distributions 	vector->nub = NULL;
263*1b191cb5SApple OSS Distributions 	vector->source = 0;
264*1b191cb5SApple OSS Distributions 	vector->handler = NULL;
265*1b191cb5SApple OSS Distributions 	vector->target = NULL;
266*1b191cb5SApple OSS Distributions 	vector->refCon = NULL;
267*1b191cb5SApple OSS Distributions 
268*1b191cb5SApple OSS Distributions 	IOLockUnlock(vector->interruptLock);
269*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
270*1b191cb5SApple OSS Distributions }
271*1b191cb5SApple OSS Distributions 
272*1b191cb5SApple OSS Distributions IOReturn
getInterruptType(IOService * nub,int source,int * interruptType)273*1b191cb5SApple OSS Distributions IOInterruptController::getInterruptType(IOService *nub, int source,
274*1b191cb5SApple OSS Distributions     int *interruptType)
275*1b191cb5SApple OSS Distributions {
276*1b191cb5SApple OSS Distributions 	IOInterruptSource *interruptSources;
277*1b191cb5SApple OSS Distributions 	IOInterruptVectorNumber vectorNumber;
278*1b191cb5SApple OSS Distributions 	IOInterruptVector *vector;
279*1b191cb5SApple OSS Distributions 	OSData            *vectorData;
280*1b191cb5SApple OSS Distributions 
281*1b191cb5SApple OSS Distributions 	if (interruptType == NULL) {
282*1b191cb5SApple OSS Distributions 		return kIOReturnBadArgument;
283*1b191cb5SApple OSS Distributions 	}
284*1b191cb5SApple OSS Distributions 
285*1b191cb5SApple OSS Distributions 	interruptSources = nub->_interruptSources;
286*1b191cb5SApple OSS Distributions 	vectorData = interruptSources[source].vectorData;
287*1b191cb5SApple OSS Distributions 	vectorNumber = *(IOInterruptVectorNumber *)vectorData->getBytesNoCopy();
288*1b191cb5SApple OSS Distributions 	vector = &vectors[vectorNumber];
289*1b191cb5SApple OSS Distributions 
290*1b191cb5SApple OSS Distributions 	*interruptType = getVectorType(vectorNumber, vector);
291*1b191cb5SApple OSS Distributions 
292*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
293*1b191cb5SApple OSS Distributions }
294*1b191cb5SApple OSS Distributions 
295*1b191cb5SApple OSS Distributions IOReturn
enableInterrupt(IOService * nub,int source)296*1b191cb5SApple OSS Distributions IOInterruptController::enableInterrupt(IOService *nub, int source)
297*1b191cb5SApple OSS Distributions {
298*1b191cb5SApple OSS Distributions 	IOInterruptSource *interruptSources;
299*1b191cb5SApple OSS Distributions 	IOInterruptVectorNumber vectorNumber;
300*1b191cb5SApple OSS Distributions 	IOInterruptVector *vector;
301*1b191cb5SApple OSS Distributions 	OSData            *vectorData;
302*1b191cb5SApple OSS Distributions 
303*1b191cb5SApple OSS Distributions 	interruptSources = nub->_interruptSources;
304*1b191cb5SApple OSS Distributions 	vectorData = interruptSources[source].vectorData;
305*1b191cb5SApple OSS Distributions 	vectorNumber = *(IOInterruptVectorNumber *)vectorData->getBytesNoCopy();
306*1b191cb5SApple OSS Distributions 	vector = &vectors[vectorNumber];
307*1b191cb5SApple OSS Distributions 
308*1b191cb5SApple OSS Distributions 	if (vector->interruptDisabledSoft) {
309*1b191cb5SApple OSS Distributions 		vector->interruptDisabledSoft = 0;
310*1b191cb5SApple OSS Distributions #if !defined(__i386__) && !defined(__x86_64__)
311*1b191cb5SApple OSS Distributions 		OSMemoryBarrier();
312*1b191cb5SApple OSS Distributions #endif
313*1b191cb5SApple OSS Distributions 
314*1b191cb5SApple OSS Distributions 		if (!getPlatform()->atInterruptLevel()) {
315*1b191cb5SApple OSS Distributions 			while (vector->interruptActive) {
316*1b191cb5SApple OSS Distributions 			}
317*1b191cb5SApple OSS Distributions 		}
318*1b191cb5SApple OSS Distributions 		if (vector->interruptDisabledHard) {
319*1b191cb5SApple OSS Distributions 			vector->interruptDisabledHard = 0;
320*1b191cb5SApple OSS Distributions 
321*1b191cb5SApple OSS Distributions 			// A DSB ISH on ARM is needed to make sure the vector data are
322*1b191cb5SApple OSS Distributions 			// properly initialized before the MMIO enabling the interrupts
323*1b191cb5SApple OSS Distributions 			// in hardware. OSMemoryBarrier(), which maps to DMB, is not
324*1b191cb5SApple OSS Distributions 			// sufficient here as the CPUs are not consumers of the device
325*1b191cb5SApple OSS Distributions 			// write. Hence, the DMB does not guarantee the CPUs won't see an
326*1b191cb5SApple OSS Distributions 			// interrupt before it initalizes the vector data properly.
327*1b191cb5SApple OSS Distributions 			OSSynchronizeIO();
328*1b191cb5SApple OSS Distributions 
329*1b191cb5SApple OSS Distributions 			enableVector(vectorNumber, vector);
330*1b191cb5SApple OSS Distributions 		}
331*1b191cb5SApple OSS Distributions 	}
332*1b191cb5SApple OSS Distributions 
333*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
334*1b191cb5SApple OSS Distributions }
335*1b191cb5SApple OSS Distributions 
336*1b191cb5SApple OSS Distributions IOReturn
disableInterrupt(IOService * nub,int source)337*1b191cb5SApple OSS Distributions IOInterruptController::disableInterrupt(IOService *nub, int source)
338*1b191cb5SApple OSS Distributions {
339*1b191cb5SApple OSS Distributions 	IOInterruptSource *interruptSources;
340*1b191cb5SApple OSS Distributions 	IOInterruptVectorNumber vectorNumber;
341*1b191cb5SApple OSS Distributions 	IOInterruptVector *vector;
342*1b191cb5SApple OSS Distributions 	OSData            *vectorData;
343*1b191cb5SApple OSS Distributions 
344*1b191cb5SApple OSS Distributions 	interruptSources = nub->_interruptSources;
345*1b191cb5SApple OSS Distributions 	vectorData = interruptSources[source].vectorData;
346*1b191cb5SApple OSS Distributions 	vectorNumber = *(IOInterruptVectorNumber *)vectorData->getBytesNoCopy();
347*1b191cb5SApple OSS Distributions 	vector = &vectors[vectorNumber];
348*1b191cb5SApple OSS Distributions 
349*1b191cb5SApple OSS Distributions 	vector->interruptDisabledSoft = 1;
350*1b191cb5SApple OSS Distributions #if !defined(__i386__) && !defined(__x86_64__)
351*1b191cb5SApple OSS Distributions 	OSMemoryBarrier();
352*1b191cb5SApple OSS Distributions #endif
353*1b191cb5SApple OSS Distributions 
354*1b191cb5SApple OSS Distributions 	if (!getPlatform()->atInterruptLevel()) {
355*1b191cb5SApple OSS Distributions 		while (vector->interruptActive) {
356*1b191cb5SApple OSS Distributions 		}
357*1b191cb5SApple OSS Distributions 	}
358*1b191cb5SApple OSS Distributions 
359*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
360*1b191cb5SApple OSS Distributions }
361*1b191cb5SApple OSS Distributions 
362*1b191cb5SApple OSS Distributions IOReturn
causeInterrupt(IOService * nub,int source)363*1b191cb5SApple OSS Distributions IOInterruptController::causeInterrupt(IOService *nub, int source)
364*1b191cb5SApple OSS Distributions {
365*1b191cb5SApple OSS Distributions 	IOInterruptSource *interruptSources;
366*1b191cb5SApple OSS Distributions 	IOInterruptVectorNumber vectorNumber;
367*1b191cb5SApple OSS Distributions 	IOInterruptVector *vector;
368*1b191cb5SApple OSS Distributions 	OSData            *vectorData;
369*1b191cb5SApple OSS Distributions 
370*1b191cb5SApple OSS Distributions 	interruptSources = nub->_interruptSources;
371*1b191cb5SApple OSS Distributions 	vectorData = interruptSources[source].vectorData;
372*1b191cb5SApple OSS Distributions 	vectorNumber = *(IOInterruptVectorNumber *)vectorData->getBytesNoCopy();
373*1b191cb5SApple OSS Distributions 	vector = &vectors[vectorNumber];
374*1b191cb5SApple OSS Distributions 
375*1b191cb5SApple OSS Distributions 	causeVector(vectorNumber, vector);
376*1b191cb5SApple OSS Distributions 
377*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
378*1b191cb5SApple OSS Distributions }
379*1b191cb5SApple OSS Distributions 
380*1b191cb5SApple OSS Distributions IOInterruptAction
getInterruptHandlerAddress(void)381*1b191cb5SApple OSS Distributions IOInterruptController::getInterruptHandlerAddress(void)
382*1b191cb5SApple OSS Distributions {
383*1b191cb5SApple OSS Distributions 	return NULL;
384*1b191cb5SApple OSS Distributions }
385*1b191cb5SApple OSS Distributions 
386*1b191cb5SApple OSS Distributions IOReturn
handleInterrupt(void * refCon,IOService * nub,int source)387*1b191cb5SApple OSS Distributions IOInterruptController::handleInterrupt(void *refCon, IOService *nub,
388*1b191cb5SApple OSS Distributions     int source)
389*1b191cb5SApple OSS Distributions {
390*1b191cb5SApple OSS Distributions 	return kIOReturnInvalid;
391*1b191cb5SApple OSS Distributions }
392*1b191cb5SApple OSS Distributions 
393*1b191cb5SApple OSS Distributions 
394*1b191cb5SApple OSS Distributions // Methods to be overridden for simplifed interrupt controller subclasses.
395*1b191cb5SApple OSS Distributions 
396*1b191cb5SApple OSS Distributions bool
vectorCanBeShared(IOInterruptVectorNumber,IOInterruptVector *)397*1b191cb5SApple OSS Distributions IOInterruptController::vectorCanBeShared(IOInterruptVectorNumber /*vectorNumber*/,
398*1b191cb5SApple OSS Distributions     IOInterruptVector */*vector*/)
399*1b191cb5SApple OSS Distributions {
400*1b191cb5SApple OSS Distributions 	return false;
401*1b191cb5SApple OSS Distributions }
402*1b191cb5SApple OSS Distributions 
403*1b191cb5SApple OSS Distributions void
initVector(IOInterruptVectorNumber,IOInterruptVector *)404*1b191cb5SApple OSS Distributions IOInterruptController::initVector(IOInterruptVectorNumber /*vectorNumber*/,
405*1b191cb5SApple OSS Distributions     IOInterruptVector */*vector*/)
406*1b191cb5SApple OSS Distributions {
407*1b191cb5SApple OSS Distributions }
408*1b191cb5SApple OSS Distributions 
409*1b191cb5SApple OSS Distributions int
getVectorType(IOInterruptVectorNumber,IOInterruptVector *)410*1b191cb5SApple OSS Distributions IOInterruptController::getVectorType(IOInterruptVectorNumber /*vectorNumber*/,
411*1b191cb5SApple OSS Distributions     IOInterruptVector */*vector*/)
412*1b191cb5SApple OSS Distributions {
413*1b191cb5SApple OSS Distributions 	return kIOInterruptTypeEdge;
414*1b191cb5SApple OSS Distributions }
415*1b191cb5SApple OSS Distributions 
416*1b191cb5SApple OSS Distributions void
disableVectorHard(IOInterruptVectorNumber,IOInterruptVector *)417*1b191cb5SApple OSS Distributions IOInterruptController::disableVectorHard(IOInterruptVectorNumber /*vectorNumber*/,
418*1b191cb5SApple OSS Distributions     IOInterruptVector */*vector*/)
419*1b191cb5SApple OSS Distributions {
420*1b191cb5SApple OSS Distributions }
421*1b191cb5SApple OSS Distributions 
422*1b191cb5SApple OSS Distributions void
enableVector(IOInterruptVectorNumber,IOInterruptVector *)423*1b191cb5SApple OSS Distributions IOInterruptController::enableVector(IOInterruptVectorNumber /*vectorNumber*/,
424*1b191cb5SApple OSS Distributions     IOInterruptVector */*vector*/)
425*1b191cb5SApple OSS Distributions {
426*1b191cb5SApple OSS Distributions }
427*1b191cb5SApple OSS Distributions 
428*1b191cb5SApple OSS Distributions void
causeVector(IOInterruptVectorNumber,IOInterruptVector *)429*1b191cb5SApple OSS Distributions IOInterruptController::causeVector(IOInterruptVectorNumber /*vectorNumber*/,
430*1b191cb5SApple OSS Distributions     IOInterruptVector */*vector*/)
431*1b191cb5SApple OSS Distributions {
432*1b191cb5SApple OSS Distributions }
433*1b191cb5SApple OSS Distributions 
434*1b191cb5SApple OSS Distributions void
setCPUInterruptProperties(IOService *)435*1b191cb5SApple OSS Distributions IOInterruptController::setCPUInterruptProperties(IOService */*service*/)
436*1b191cb5SApple OSS Distributions {
437*1b191cb5SApple OSS Distributions }
438*1b191cb5SApple OSS Distributions 
439*1b191cb5SApple OSS Distributions void
sendIPI(unsigned int,bool)440*1b191cb5SApple OSS Distributions IOInterruptController::sendIPI(unsigned int /*cpu_id*/, bool /*deferred*/)
441*1b191cb5SApple OSS Distributions {
442*1b191cb5SApple OSS Distributions }
443*1b191cb5SApple OSS Distributions 
444*1b191cb5SApple OSS Distributions void
cancelDeferredIPI(unsigned int)445*1b191cb5SApple OSS Distributions IOInterruptController::cancelDeferredIPI(unsigned int /*cpu_id*/)
446*1b191cb5SApple OSS Distributions {
447*1b191cb5SApple OSS Distributions }
448*1b191cb5SApple OSS Distributions 
449*1b191cb5SApple OSS Distributions void
timeStampSpuriousInterrupt(void)450*1b191cb5SApple OSS Distributions IOInterruptController::timeStampSpuriousInterrupt(void)
451*1b191cb5SApple OSS Distributions {
452*1b191cb5SApple OSS Distributions 	uint64_t providerID = 0;
453*1b191cb5SApple OSS Distributions 	IOService * provider = getProvider();
454*1b191cb5SApple OSS Distributions 
455*1b191cb5SApple OSS Distributions 	if (provider) {
456*1b191cb5SApple OSS Distributions 		providerID = provider->getRegistryEntryID();
457*1b191cb5SApple OSS Distributions 	}
458*1b191cb5SApple OSS Distributions 
459*1b191cb5SApple OSS Distributions 	IOTimeStampConstant(IODBG_INTC(IOINTC_SPURIOUS), providerID);
460*1b191cb5SApple OSS Distributions }
461*1b191cb5SApple OSS Distributions 
462*1b191cb5SApple OSS Distributions void
timeStampInterruptHandlerInternal(bool isStart,IOInterruptVectorNumber vectorNumber,IOInterruptVector * vector)463*1b191cb5SApple OSS Distributions IOInterruptController::timeStampInterruptHandlerInternal(bool isStart, IOInterruptVectorNumber vectorNumber, IOInterruptVector *vector)
464*1b191cb5SApple OSS Distributions {
465*1b191cb5SApple OSS Distributions 	uint64_t providerID = 0;
466*1b191cb5SApple OSS Distributions 	vm_offset_t unslidHandler = 0;
467*1b191cb5SApple OSS Distributions 	vm_offset_t unslidTarget = 0;
468*1b191cb5SApple OSS Distributions 
469*1b191cb5SApple OSS Distributions 	IOService * provider = getProvider();
470*1b191cb5SApple OSS Distributions 
471*1b191cb5SApple OSS Distributions 	if (provider) {
472*1b191cb5SApple OSS Distributions 		providerID = provider->getRegistryEntryID();
473*1b191cb5SApple OSS Distributions 	}
474*1b191cb5SApple OSS Distributions 
475*1b191cb5SApple OSS Distributions 	if (vector) {
476*1b191cb5SApple OSS Distributions 		unslidHandler = VM_KERNEL_UNSLIDE((vm_offset_t)vector->handler);
477*1b191cb5SApple OSS Distributions 		unslidTarget = VM_KERNEL_UNSLIDE_OR_PERM((vm_offset_t)vector->target);
478*1b191cb5SApple OSS Distributions 	}
479*1b191cb5SApple OSS Distributions 
480*1b191cb5SApple OSS Distributions 
481*1b191cb5SApple OSS Distributions 	if (isStart) {
482*1b191cb5SApple OSS Distributions #if SCHED_HYGIENE_DEBUG
483*1b191cb5SApple OSS Distributions 		ml_irq_debug_start((uintptr_t)vector->handler, (uintptr_t)vector);
484*1b191cb5SApple OSS Distributions #endif
485*1b191cb5SApple OSS Distributions 		IOTimeStampStartConstant(IODBG_INTC(IOINTC_HANDLER), (uintptr_t)vectorNumber, (uintptr_t)unslidHandler,
486*1b191cb5SApple OSS Distributions 		    (uintptr_t)unslidTarget, (uintptr_t)providerID);
487*1b191cb5SApple OSS Distributions 	} else {
488*1b191cb5SApple OSS Distributions 		IOTimeStampEndConstant(IODBG_INTC(IOINTC_HANDLER), (uintptr_t)vectorNumber, (uintptr_t)unslidHandler,
489*1b191cb5SApple OSS Distributions 		    (uintptr_t)unslidTarget, (uintptr_t)providerID);
490*1b191cb5SApple OSS Distributions #if SCHED_HYGIENE_DEBUG
491*1b191cb5SApple OSS Distributions 		ml_irq_debug_end();
492*1b191cb5SApple OSS Distributions #endif
493*1b191cb5SApple OSS Distributions 	}
494*1b191cb5SApple OSS Distributions }
495*1b191cb5SApple OSS Distributions 
496*1b191cb5SApple OSS Distributions void
timeStampInterruptHandlerStart(IOInterruptVectorNumber vectorNumber,IOInterruptVector * vector)497*1b191cb5SApple OSS Distributions IOInterruptController::timeStampInterruptHandlerStart(IOInterruptVectorNumber vectorNumber, IOInterruptVector *vector)
498*1b191cb5SApple OSS Distributions {
499*1b191cb5SApple OSS Distributions 	timeStampInterruptHandlerInternal(true, vectorNumber, vector);
500*1b191cb5SApple OSS Distributions }
501*1b191cb5SApple OSS Distributions 
502*1b191cb5SApple OSS Distributions void
timeStampInterruptHandlerEnd(IOInterruptVectorNumber vectorNumber,IOInterruptVector * vector)503*1b191cb5SApple OSS Distributions IOInterruptController::timeStampInterruptHandlerEnd(IOInterruptVectorNumber vectorNumber, IOInterruptVector *vector)
504*1b191cb5SApple OSS Distributions {
505*1b191cb5SApple OSS Distributions 	timeStampInterruptHandlerInternal(false, vectorNumber, vector);
506*1b191cb5SApple OSS Distributions }
507*1b191cb5SApple OSS Distributions 
508*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
509*1b191cb5SApple OSS Distributions 
510*1b191cb5SApple OSS Distributions #undef  super
511*1b191cb5SApple OSS Distributions #define super IOInterruptController
512*1b191cb5SApple OSS Distributions 
513*1b191cb5SApple OSS Distributions OSDefineMetaClassAndStructors(IOSharedInterruptController, IOInterruptController);
514*1b191cb5SApple OSS Distributions 
515*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedInterruptController, 0);
516*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedInterruptController, 1);
517*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedInterruptController, 2);
518*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedInterruptController, 3);
519*1b191cb5SApple OSS Distributions 
520*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
521*1b191cb5SApple OSS Distributions 
522*1b191cb5SApple OSS Distributions #define kIOSharedInterruptControllerDefaultVectors (128)
523*1b191cb5SApple OSS Distributions 
524*1b191cb5SApple OSS Distributions IOReturn
initInterruptController(IOInterruptController * parentController,OSData * parentSource)525*1b191cb5SApple OSS Distributions IOSharedInterruptController::initInterruptController(IOInterruptController *parentController, OSData *parentSource)
526*1b191cb5SApple OSS Distributions {
527*1b191cb5SApple OSS Distributions 	int      cnt, interruptType;
528*1b191cb5SApple OSS Distributions 	IOReturn error;
529*1b191cb5SApple OSS Distributions 
530*1b191cb5SApple OSS Distributions 	if (!super::init()) {
531*1b191cb5SApple OSS Distributions 		return kIOReturnNoResources;
532*1b191cb5SApple OSS Distributions 	}
533*1b191cb5SApple OSS Distributions 
534*1b191cb5SApple OSS Distributions 	// Set provider to this so enable/disable nub stuff works.
535*1b191cb5SApple OSS Distributions 	provider = this;
536*1b191cb5SApple OSS Distributions 
537*1b191cb5SApple OSS Distributions 	// Allocate the IOInterruptSource so this can act like a nub.
538*1b191cb5SApple OSS Distributions 	_interruptSources = IONew(IOInterruptSource, 1);
539*1b191cb5SApple OSS Distributions 	if (_interruptSources == NULL) {
540*1b191cb5SApple OSS Distributions 		return kIOReturnNoMemory;
541*1b191cb5SApple OSS Distributions 	}
542*1b191cb5SApple OSS Distributions 	_numInterruptSources = 1;
543*1b191cb5SApple OSS Distributions 
544*1b191cb5SApple OSS Distributions 	// Set up the IOInterruptSource to point at this.
545*1b191cb5SApple OSS Distributions 	parentController->retain();
546*1b191cb5SApple OSS Distributions 	parentSource->retain();
547*1b191cb5SApple OSS Distributions 	_interruptSources[0].interruptController = parentController;
548*1b191cb5SApple OSS Distributions 	_interruptSources[0].vectorData = parentSource;
549*1b191cb5SApple OSS Distributions 
550*1b191cb5SApple OSS Distributions 	sourceIsLevel = false;
551*1b191cb5SApple OSS Distributions 	error = provider->getInterruptType(0, &interruptType);
552*1b191cb5SApple OSS Distributions 	if (error == kIOReturnSuccess) {
553*1b191cb5SApple OSS Distributions 		if (interruptType & kIOInterruptTypeLevel) {
554*1b191cb5SApple OSS Distributions 			sourceIsLevel = true;
555*1b191cb5SApple OSS Distributions 		}
556*1b191cb5SApple OSS Distributions 	}
557*1b191cb5SApple OSS Distributions 
558*1b191cb5SApple OSS Distributions 	// Allocate the memory for the vectors
559*1b191cb5SApple OSS Distributions 	numVectors = kIOSharedInterruptControllerDefaultVectors; // For now a constant number.
560*1b191cb5SApple OSS Distributions 	vectors = IONewZero(IOInterruptVector, numVectors);
561*1b191cb5SApple OSS Distributions 	if (vectors == NULL) {
562*1b191cb5SApple OSS Distributions 		IODelete(_interruptSources, IOInterruptSource, 1);
563*1b191cb5SApple OSS Distributions 		return kIOReturnNoMemory;
564*1b191cb5SApple OSS Distributions 	}
565*1b191cb5SApple OSS Distributions 
566*1b191cb5SApple OSS Distributions 	// Allocate the lock for the controller.
567*1b191cb5SApple OSS Distributions 	controllerLock = IOSimpleLockAlloc();
568*1b191cb5SApple OSS Distributions 	if (controllerLock == NULL) {
569*1b191cb5SApple OSS Distributions 		return kIOReturnNoResources;
570*1b191cb5SApple OSS Distributions 	}
571*1b191cb5SApple OSS Distributions 
572*1b191cb5SApple OSS Distributions 	// Allocate locks for the vectors.
573*1b191cb5SApple OSS Distributions 	for (cnt = 0; cnt < numVectors; cnt++) {
574*1b191cb5SApple OSS Distributions 		vectors[cnt].interruptLock = IOLockAlloc();
575*1b191cb5SApple OSS Distributions 		if (vectors[cnt].interruptLock == NULL) {
576*1b191cb5SApple OSS Distributions 			for (cnt = 0; cnt < numVectors; cnt++) {
577*1b191cb5SApple OSS Distributions 				if (vectors[cnt].interruptLock != NULL) {
578*1b191cb5SApple OSS Distributions 					IOLockFree(vectors[cnt].interruptLock);
579*1b191cb5SApple OSS Distributions 				}
580*1b191cb5SApple OSS Distributions 			}
581*1b191cb5SApple OSS Distributions 			return kIOReturnNoResources;
582*1b191cb5SApple OSS Distributions 		}
583*1b191cb5SApple OSS Distributions 	}
584*1b191cb5SApple OSS Distributions 
585*1b191cb5SApple OSS Distributions 	numVectors = 0; // reset the high water mark for used vectors
586*1b191cb5SApple OSS Distributions 	vectorsRegistered = 0;
587*1b191cb5SApple OSS Distributions 	vectorsEnabled = 0;
588*1b191cb5SApple OSS Distributions 	controllerDisabled = 1;
589*1b191cb5SApple OSS Distributions 
590*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
591*1b191cb5SApple OSS Distributions }
592*1b191cb5SApple OSS Distributions 
593*1b191cb5SApple OSS Distributions IOReturn
registerInterrupt(IOService * nub,int source,void * target,IOInterruptHandler handler,void * refCon)594*1b191cb5SApple OSS Distributions IOSharedInterruptController::registerInterrupt(IOService *nub,
595*1b191cb5SApple OSS Distributions     int source,
596*1b191cb5SApple OSS Distributions     void *target,
597*1b191cb5SApple OSS Distributions     IOInterruptHandler handler,
598*1b191cb5SApple OSS Distributions     void *refCon)
599*1b191cb5SApple OSS Distributions {
600*1b191cb5SApple OSS Distributions 	IOInterruptSource *interruptSources;
601*1b191cb5SApple OSS Distributions 	IOInterruptVectorNumber vectorNumber;
602*1b191cb5SApple OSS Distributions 	IOInterruptVector *vector = NULL;
603*1b191cb5SApple OSS Distributions 	OSData            *vectorData;
604*1b191cb5SApple OSS Distributions 	IOInterruptState  interruptState;
605*1b191cb5SApple OSS Distributions 
606*1b191cb5SApple OSS Distributions 	interruptSources = nub->_interruptSources;
607*1b191cb5SApple OSS Distributions 
608*1b191cb5SApple OSS Distributions 	// Find a free vector.
609*1b191cb5SApple OSS Distributions 	vectorNumber = kIOSharedInterruptControllerDefaultVectors;
610*1b191cb5SApple OSS Distributions 	while (vectorsRegistered != kIOSharedInterruptControllerDefaultVectors) {
611*1b191cb5SApple OSS Distributions 		for (vectorNumber = 0; vectorNumber < kIOSharedInterruptControllerDefaultVectors; vectorNumber++) {
612*1b191cb5SApple OSS Distributions 			vector = &vectors[vectorNumber];
613*1b191cb5SApple OSS Distributions 
614*1b191cb5SApple OSS Distributions 			// Get the lock for this vector.
615*1b191cb5SApple OSS Distributions 			IOLockLock(vector->interruptLock);
616*1b191cb5SApple OSS Distributions 
617*1b191cb5SApple OSS Distributions 			// Is it unregistered?
618*1b191cb5SApple OSS Distributions 			if (!vector->interruptRegistered) {
619*1b191cb5SApple OSS Distributions 				break;
620*1b191cb5SApple OSS Distributions 			}
621*1b191cb5SApple OSS Distributions 
622*1b191cb5SApple OSS Distributions 			// Move along to the next one.
623*1b191cb5SApple OSS Distributions 			IOLockUnlock(vector->interruptLock);
624*1b191cb5SApple OSS Distributions 		}
625*1b191cb5SApple OSS Distributions 
626*1b191cb5SApple OSS Distributions 		if (vectorNumber != kIOSharedInterruptControllerDefaultVectors) {
627*1b191cb5SApple OSS Distributions 			break;
628*1b191cb5SApple OSS Distributions 		}
629*1b191cb5SApple OSS Distributions 	}
630*1b191cb5SApple OSS Distributions 
631*1b191cb5SApple OSS Distributions 	// Could not find a free one, so give up.
632*1b191cb5SApple OSS Distributions 	if (vectorNumber == kIOSharedInterruptControllerDefaultVectors) {
633*1b191cb5SApple OSS Distributions 		return kIOReturnNoResources;
634*1b191cb5SApple OSS Distributions 	}
635*1b191cb5SApple OSS Distributions 
636*1b191cb5SApple OSS Distributions 	// Create the vectorData for the IOInterruptSource.
637*1b191cb5SApple OSS Distributions 	vectorData = OSData::withValue(vectorNumber);
638*1b191cb5SApple OSS Distributions 	if (vectorData == NULL) {
639*1b191cb5SApple OSS Distributions 		IOLockUnlock(vector->interruptLock);
640*1b191cb5SApple OSS Distributions 		return kIOReturnNoMemory;
641*1b191cb5SApple OSS Distributions 	}
642*1b191cb5SApple OSS Distributions 
643*1b191cb5SApple OSS Distributions 	// Fill in the IOInterruptSource with the controller's info.
644*1b191cb5SApple OSS Distributions 	interruptSources[source].interruptController = this;
645*1b191cb5SApple OSS Distributions 	interruptSources[source].vectorData = vectorData;
646*1b191cb5SApple OSS Distributions 
647*1b191cb5SApple OSS Distributions 	// Fill in vector with the client's info.
648*1b191cb5SApple OSS Distributions 	vector->handler = handler;
649*1b191cb5SApple OSS Distributions 	vector->nub     = nub;
650*1b191cb5SApple OSS Distributions 	vector->source  = source;
651*1b191cb5SApple OSS Distributions 	vector->target  = target;
652*1b191cb5SApple OSS Distributions 	vector->refCon  = refCon;
653*1b191cb5SApple OSS Distributions 
654*1b191cb5SApple OSS Distributions 	// Get the vector ready.  It starts off soft disabled.
655*1b191cb5SApple OSS Distributions 	vector->interruptDisabledSoft = 1;
656*1b191cb5SApple OSS Distributions 	vector->interruptRegistered   = 1;
657*1b191cb5SApple OSS Distributions 
658*1b191cb5SApple OSS Distributions 	interruptState = IOSimpleLockLockDisableInterrupt(controllerLock);
659*1b191cb5SApple OSS Distributions 	// Move the high water mark if needed
660*1b191cb5SApple OSS Distributions 	if (++vectorsRegistered > numVectors) {
661*1b191cb5SApple OSS Distributions 		numVectors = vectorsRegistered;
662*1b191cb5SApple OSS Distributions 	}
663*1b191cb5SApple OSS Distributions 	IOSimpleLockUnlockEnableInterrupt(controllerLock, interruptState);
664*1b191cb5SApple OSS Distributions 
665*1b191cb5SApple OSS Distributions 	IOLockUnlock(vector->interruptLock);
666*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
667*1b191cb5SApple OSS Distributions }
668*1b191cb5SApple OSS Distributions 
669*1b191cb5SApple OSS Distributions IOReturn
unregisterInterrupt(IOService * nub,int source)670*1b191cb5SApple OSS Distributions IOSharedInterruptController::unregisterInterrupt(IOService *nub,
671*1b191cb5SApple OSS Distributions     int source)
672*1b191cb5SApple OSS Distributions {
673*1b191cb5SApple OSS Distributions 	IOInterruptVectorNumber vectorNumber;
674*1b191cb5SApple OSS Distributions 	IOInterruptVector *vector;
675*1b191cb5SApple OSS Distributions 	IOInterruptState  interruptState;
676*1b191cb5SApple OSS Distributions 
677*1b191cb5SApple OSS Distributions 	for (vectorNumber = 0; vectorNumber < kIOSharedInterruptControllerDefaultVectors; vectorNumber++) {
678*1b191cb5SApple OSS Distributions 		vector = &vectors[vectorNumber];
679*1b191cb5SApple OSS Distributions 
680*1b191cb5SApple OSS Distributions 		// Get the lock for this vector.
681*1b191cb5SApple OSS Distributions 		IOLockLock(vector->interruptLock);
682*1b191cb5SApple OSS Distributions 
683*1b191cb5SApple OSS Distributions 		// Return success if it is not already registered
684*1b191cb5SApple OSS Distributions 		if (!vector->interruptRegistered
685*1b191cb5SApple OSS Distributions 		    || (vector->nub != nub) || (vector->source != source)) {
686*1b191cb5SApple OSS Distributions 			IOLockUnlock(vector->interruptLock);
687*1b191cb5SApple OSS Distributions 			continue;
688*1b191cb5SApple OSS Distributions 		}
689*1b191cb5SApple OSS Distributions 
690*1b191cb5SApple OSS Distributions 		// Soft disable the source and the controller too.
691*1b191cb5SApple OSS Distributions 		disableInterrupt(nub, source);
692*1b191cb5SApple OSS Distributions 
693*1b191cb5SApple OSS Distributions 		// Free vectorData
694*1b191cb5SApple OSS Distributions 		IOInterruptSource *interruptSources = nub->_interruptSources;
695*1b191cb5SApple OSS Distributions 		OSSafeReleaseNULL(interruptSources[source].vectorData);
696*1b191cb5SApple OSS Distributions 
697*1b191cb5SApple OSS Distributions 		// Clear all the storage for the vector except for interruptLock.
698*1b191cb5SApple OSS Distributions 		vector->interruptActive = 0;
699*1b191cb5SApple OSS Distributions 		vector->interruptDisabledSoft = 0;
700*1b191cb5SApple OSS Distributions 		vector->interruptDisabledHard = 0;
701*1b191cb5SApple OSS Distributions 		vector->interruptRegistered = 0;
702*1b191cb5SApple OSS Distributions 		vector->nub = NULL;
703*1b191cb5SApple OSS Distributions 		vector->source = 0;
704*1b191cb5SApple OSS Distributions 		vector->handler = NULL;
705*1b191cb5SApple OSS Distributions 		vector->target = NULL;
706*1b191cb5SApple OSS Distributions 		vector->refCon = NULL;
707*1b191cb5SApple OSS Distributions 
708*1b191cb5SApple OSS Distributions 		interruptState = IOSimpleLockLockDisableInterrupt(controllerLock);
709*1b191cb5SApple OSS Distributions 		vectorsRegistered--;
710*1b191cb5SApple OSS Distributions 		IOSimpleLockUnlockEnableInterrupt(controllerLock, interruptState);
711*1b191cb5SApple OSS Distributions 
712*1b191cb5SApple OSS Distributions 		// Move along to the next one.
713*1b191cb5SApple OSS Distributions 		IOLockUnlock(vector->interruptLock);
714*1b191cb5SApple OSS Distributions 	}
715*1b191cb5SApple OSS Distributions 
716*1b191cb5SApple OSS Distributions 	// Re-enable the controller if all vectors are enabled.
717*1b191cb5SApple OSS Distributions 	if (vectorsEnabled == vectorsRegistered) {
718*1b191cb5SApple OSS Distributions 		controllerDisabled = 0;
719*1b191cb5SApple OSS Distributions 		provider->enableInterrupt(0);
720*1b191cb5SApple OSS Distributions 	}
721*1b191cb5SApple OSS Distributions 
722*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
723*1b191cb5SApple OSS Distributions }
724*1b191cb5SApple OSS Distributions 
725*1b191cb5SApple OSS Distributions IOReturn
getInterruptType(IOService *,int,int * interruptType)726*1b191cb5SApple OSS Distributions IOSharedInterruptController::getInterruptType(IOService */*nub*/,
727*1b191cb5SApple OSS Distributions     int /*source*/,
728*1b191cb5SApple OSS Distributions     int *interruptType)
729*1b191cb5SApple OSS Distributions {
730*1b191cb5SApple OSS Distributions 	return provider->getInterruptType(0, interruptType);
731*1b191cb5SApple OSS Distributions }
732*1b191cb5SApple OSS Distributions 
733*1b191cb5SApple OSS Distributions IOReturn
enableInterrupt(IOService * nub,int source)734*1b191cb5SApple OSS Distributions IOSharedInterruptController::enableInterrupt(IOService *nub,
735*1b191cb5SApple OSS Distributions     int source)
736*1b191cb5SApple OSS Distributions {
737*1b191cb5SApple OSS Distributions 	IOInterruptSource *interruptSources;
738*1b191cb5SApple OSS Distributions 	IOInterruptVectorNumber vectorNumber;
739*1b191cb5SApple OSS Distributions 	IOInterruptVector *vector;
740*1b191cb5SApple OSS Distributions 	OSData            *vectorData;
741*1b191cb5SApple OSS Distributions 	IOInterruptState  interruptState;
742*1b191cb5SApple OSS Distributions 
743*1b191cb5SApple OSS Distributions 	interruptSources = nub->_interruptSources;
744*1b191cb5SApple OSS Distributions 	vectorData = interruptSources[source].vectorData;
745*1b191cb5SApple OSS Distributions 	vectorNumber = *(IOInterruptVectorNumber *)vectorData->getBytesNoCopy();
746*1b191cb5SApple OSS Distributions 	vector = &vectors[vectorNumber];
747*1b191cb5SApple OSS Distributions 
748*1b191cb5SApple OSS Distributions 	interruptState = IOSimpleLockLockDisableInterrupt(controllerLock);
749*1b191cb5SApple OSS Distributions 	if (!vector->interruptDisabledSoft) {
750*1b191cb5SApple OSS Distributions 		IOSimpleLockUnlockEnableInterrupt(controllerLock, interruptState);
751*1b191cb5SApple OSS Distributions 		return kIOReturnSuccess;
752*1b191cb5SApple OSS Distributions 	}
753*1b191cb5SApple OSS Distributions 
754*1b191cb5SApple OSS Distributions 	vector->interruptDisabledSoft = 0;
755*1b191cb5SApple OSS Distributions 	vectorsEnabled++;
756*1b191cb5SApple OSS Distributions 	IOSimpleLockUnlockEnableInterrupt(controllerLock, interruptState);
757*1b191cb5SApple OSS Distributions 
758*1b191cb5SApple OSS Distributions 	if (controllerDisabled && (vectorsEnabled == vectorsRegistered)) {
759*1b191cb5SApple OSS Distributions 		controllerDisabled = 0;
760*1b191cb5SApple OSS Distributions 		provider->enableInterrupt(0);
761*1b191cb5SApple OSS Distributions 	}
762*1b191cb5SApple OSS Distributions 
763*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
764*1b191cb5SApple OSS Distributions }
765*1b191cb5SApple OSS Distributions 
766*1b191cb5SApple OSS Distributions IOReturn
disableInterrupt(IOService * nub,int source)767*1b191cb5SApple OSS Distributions IOSharedInterruptController::disableInterrupt(IOService *nub,
768*1b191cb5SApple OSS Distributions     int source)
769*1b191cb5SApple OSS Distributions {
770*1b191cb5SApple OSS Distributions 	IOInterruptSource *interruptSources;
771*1b191cb5SApple OSS Distributions 	IOInterruptVectorNumber vectorNumber;
772*1b191cb5SApple OSS Distributions 	IOInterruptVector *vector;
773*1b191cb5SApple OSS Distributions 	OSData            *vectorData;
774*1b191cb5SApple OSS Distributions 	IOInterruptState  interruptState;
775*1b191cb5SApple OSS Distributions 
776*1b191cb5SApple OSS Distributions 	interruptSources = nub->_interruptSources;
777*1b191cb5SApple OSS Distributions 	vectorData = interruptSources[source].vectorData;
778*1b191cb5SApple OSS Distributions 	vectorNumber = *(IOInterruptVectorNumber *)vectorData->getBytesNoCopy();
779*1b191cb5SApple OSS Distributions 	vector = &vectors[vectorNumber];
780*1b191cb5SApple OSS Distributions 
781*1b191cb5SApple OSS Distributions 	interruptState = IOSimpleLockLockDisableInterrupt(controllerLock);
782*1b191cb5SApple OSS Distributions 	if (!vector->interruptDisabledSoft) {
783*1b191cb5SApple OSS Distributions 		vector->interruptDisabledSoft = 1;
784*1b191cb5SApple OSS Distributions #if !defined(__i386__) && !defined(__x86_64__)
785*1b191cb5SApple OSS Distributions 		OSMemoryBarrier();
786*1b191cb5SApple OSS Distributions #endif
787*1b191cb5SApple OSS Distributions 
788*1b191cb5SApple OSS Distributions 		vectorsEnabled--;
789*1b191cb5SApple OSS Distributions 	}
790*1b191cb5SApple OSS Distributions 	IOSimpleLockUnlockEnableInterrupt(controllerLock, interruptState);
791*1b191cb5SApple OSS Distributions 
792*1b191cb5SApple OSS Distributions 	if (!getPlatform()->atInterruptLevel()) {
793*1b191cb5SApple OSS Distributions 		while (vector->interruptActive) {
794*1b191cb5SApple OSS Distributions 		}
795*1b191cb5SApple OSS Distributions 	}
796*1b191cb5SApple OSS Distributions 
797*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
798*1b191cb5SApple OSS Distributions }
799*1b191cb5SApple OSS Distributions 
800*1b191cb5SApple OSS Distributions IOInterruptAction
getInterruptHandlerAddress(void)801*1b191cb5SApple OSS Distributions IOSharedInterruptController::getInterruptHandlerAddress(void)
802*1b191cb5SApple OSS Distributions {
803*1b191cb5SApple OSS Distributions 	return OSMemberFunctionCast(IOInterruptAction,
804*1b191cb5SApple OSS Distributions 	           this, &IOSharedInterruptController::handleInterrupt);
805*1b191cb5SApple OSS Distributions }
806*1b191cb5SApple OSS Distributions 
807*1b191cb5SApple OSS Distributions IOReturn
handleInterrupt(void *,IOService * nub,int)808*1b191cb5SApple OSS Distributions IOSharedInterruptController::handleInterrupt(void * /*refCon*/,
809*1b191cb5SApple OSS Distributions     IOService * nub,
810*1b191cb5SApple OSS Distributions     int /*source*/)
811*1b191cb5SApple OSS Distributions {
812*1b191cb5SApple OSS Distributions 	IOInterruptVectorNumber vectorNumber;
813*1b191cb5SApple OSS Distributions 	IOInterruptVector *vector;
814*1b191cb5SApple OSS Distributions 
815*1b191cb5SApple OSS Distributions 	for (vectorNumber = 0; vectorNumber < numVectors; vectorNumber++) {
816*1b191cb5SApple OSS Distributions 		vector = &vectors[vectorNumber];
817*1b191cb5SApple OSS Distributions 
818*1b191cb5SApple OSS Distributions 		vector->interruptActive = 1;
819*1b191cb5SApple OSS Distributions #if !defined(__i386__) && !defined(__x86_64__)
820*1b191cb5SApple OSS Distributions 		OSMemoryBarrier();
821*1b191cb5SApple OSS Distributions #endif
822*1b191cb5SApple OSS Distributions 
823*1b191cb5SApple OSS Distributions 		if (!vector->interruptDisabledSoft) {
824*1b191cb5SApple OSS Distributions 			// Call the handler if it exists.
825*1b191cb5SApple OSS Distributions 			if (vector->interruptRegistered) {
826*1b191cb5SApple OSS Distributions 				bool trace = (gIOKitTrace & kIOTraceInterrupts) ? true : false;
827*1b191cb5SApple OSS Distributions 
828*1b191cb5SApple OSS Distributions 				if (trace) {
829*1b191cb5SApple OSS Distributions 					timeStampInterruptHandlerStart(vectorNumber, vector);
830*1b191cb5SApple OSS Distributions 				}
831*1b191cb5SApple OSS Distributions 
832*1b191cb5SApple OSS Distributions 				// Call handler.
833*1b191cb5SApple OSS Distributions 				vector->handler(vector->target, vector->refCon, vector->nub, vector->source);
834*1b191cb5SApple OSS Distributions 
835*1b191cb5SApple OSS Distributions 				if (trace) {
836*1b191cb5SApple OSS Distributions 					timeStampInterruptHandlerEnd(vectorNumber, vector);
837*1b191cb5SApple OSS Distributions 				}
838*1b191cb5SApple OSS Distributions 			}
839*1b191cb5SApple OSS Distributions 		}
840*1b191cb5SApple OSS Distributions 
841*1b191cb5SApple OSS Distributions 		vector->interruptActive = 0;
842*1b191cb5SApple OSS Distributions 	}
843*1b191cb5SApple OSS Distributions 
844*1b191cb5SApple OSS Distributions 	// if any of the vectors are dissabled, then dissable this controller.
845*1b191cb5SApple OSS Distributions 	IOSimpleLockLock(controllerLock);
846*1b191cb5SApple OSS Distributions 	if (vectorsEnabled != vectorsRegistered) {
847*1b191cb5SApple OSS Distributions 		nub->disableInterrupt(0);
848*1b191cb5SApple OSS Distributions 		controllerDisabled = 1;
849*1b191cb5SApple OSS Distributions 	}
850*1b191cb5SApple OSS Distributions 	IOSimpleLockUnlock(controllerLock);
851*1b191cb5SApple OSS Distributions 
852*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
853*1b191cb5SApple OSS Distributions }
854