xref: /xnu-8796.141.3/iokit/Examples/drvGenericInterruptController/GenericInterruptController.cpp (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions  *
15*1b191cb5SApple OSS Distributions  * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions  *
18*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions  * limitations under the License.
25*1b191cb5SApple OSS Distributions  *
26*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions  */
28*1b191cb5SApple OSS Distributions /*
29*1b191cb5SApple OSS Distributions  * Copyright (c) 1999 Apple Computer, Inc.  All rights reserved.
30*1b191cb5SApple OSS Distributions  *
31*1b191cb5SApple OSS Distributions  *  DRI: Josh de Cesare
32*1b191cb5SApple OSS Distributions  */
33*1b191cb5SApple OSS Distributions 
34*1b191cb5SApple OSS Distributions #include <IOKit/IOPlatformExpert.h>
35*1b191cb5SApple OSS Distributions 
36*1b191cb5SApple OSS Distributions #include "GenericInterruptController.h"
37*1b191cb5SApple OSS Distributions 
38*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
39*1b191cb5SApple OSS Distributions 
40*1b191cb5SApple OSS Distributions #undef  super
41*1b191cb5SApple OSS Distributions #define super IOInterruptController
42*1b191cb5SApple OSS Distributions 
43*1b191cb5SApple OSS Distributions IODefineMetaClassAndStructors(GenericInterruptController,
44*1b191cb5SApple OSS Distributions     IOInterruptController);
45*1b191cb5SApple OSS Distributions 
46*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47*1b191cb5SApple OSS Distributions 
48*1b191cb5SApple OSS Distributions 
49*1b191cb5SApple OSS Distributions bool
start(IOService * provider)50*1b191cb5SApple OSS Distributions GenericInterruptController::start(IOService *provider)
51*1b191cb5SApple OSS Distributions {
52*1b191cb5SApple OSS Distributions 	IOInterruptAction    handler;
53*1b191cb5SApple OSS Distributions 	IOSymbol             *interruptControllerName;
54*1b191cb5SApple OSS Distributions 
55*1b191cb5SApple OSS Distributions 	// If needed call the parents start.
56*1b191cb5SApple OSS Distributions 	if (!super::start(provider)) {
57*1b191cb5SApple OSS Distributions 		return false;
58*1b191cb5SApple OSS Distributions 	}
59*1b191cb5SApple OSS Distributions 
60*1b191cb5SApple OSS Distributions 	// Map the device's memory and initalize its state.
61*1b191cb5SApple OSS Distributions 
62*1b191cb5SApple OSS Distributions 	// For now you must allocate storage for the vectors.
63*1b191cb5SApple OSS Distributions 	// This will probably changed to something like: initVectors(numVectors).
64*1b191cb5SApple OSS Distributions 	// In the mean time something like this works well.
65*1b191cb5SApple OSS Distributions #if 0
66*1b191cb5SApple OSS Distributions 	// Allocate the memory for the vectors.
67*1b191cb5SApple OSS Distributions 	vectors = (IOInterruptVector *)IOMalloc(numVectors *
68*1b191cb5SApple OSS Distributions 	    sizeof(IOInterruptVector));
69*1b191cb5SApple OSS Distributions 	if (vectors == NULL) {
70*1b191cb5SApple OSS Distributions 		return false;
71*1b191cb5SApple OSS Distributions 	}
72*1b191cb5SApple OSS Distributions 	bzero(vectors, numVectors * sizeof(IOInterruptVector));
73*1b191cb5SApple OSS Distributions 
74*1b191cb5SApple OSS Distributions 	// Allocate locks for the vectors.
75*1b191cb5SApple OSS Distributions 	for (cnt = 0; cnt < numVectors; cnt++) {
76*1b191cb5SApple OSS Distributions 		vectors[cnt].interruptLock = IOLockAlloc();
77*1b191cb5SApple OSS Distributions 		if (vectors[cnt].interruptLock == NULL) {
78*1b191cb5SApple OSS Distributions 			for (cnt = 0; cnt < numVectors; cnt++) {
79*1b191cb5SApple OSS Distributions 				if (vectors[cnt].interruptLock != NULL) {
80*1b191cb5SApple OSS Distributions 					IOLockFree(vectors[cnt].interruptLock);
81*1b191cb5SApple OSS Distributions 				}
82*1b191cb5SApple OSS Distributions 			}
83*1b191cb5SApple OSS Distributions 		}
84*1b191cb5SApple OSS Distributions 	}
85*1b191cb5SApple OSS Distributions #endif
86*1b191cb5SApple OSS Distributions 
87*1b191cb5SApple OSS Distributions 	// If you know that this interrupt controller is the primary
88*1b191cb5SApple OSS Distributions 	// interrupt controller, use this to set it nub properties properly.
89*1b191cb5SApple OSS Distributions 	// This may be done by the nub's creator.
90*1b191cb5SApple OSS Distributions 	getPlatform()->setCPUInterruptProperties(provider);
91*1b191cb5SApple OSS Distributions 
92*1b191cb5SApple OSS Distributions 	// register the interrupt handler so it can receive interrupts.
93*1b191cb5SApple OSS Distributions 	handler = getInterruptHandlerAddress();
94*1b191cb5SApple OSS Distributions 	provider->registerInterrupt(0, this, handler, 0);
95*1b191cb5SApple OSS Distributions 
96*1b191cb5SApple OSS Distributions 	// Just like any interrupt source, you must enable it to receive interrupts.
97*1b191cb5SApple OSS Distributions 	provider->enableInterrupt(0);
98*1b191cb5SApple OSS Distributions 
99*1b191cb5SApple OSS Distributions 	// Set interruptControllerName to the proper symbol.
100*1b191cb5SApple OSS Distributions 	//interruptControllerName = xxx;
101*1b191cb5SApple OSS Distributions 
102*1b191cb5SApple OSS Distributions 	// Register this interrupt controller so clients can find it.
103*1b191cb5SApple OSS Distributions 	getPlatform()->registerInterruptController(interruptControllerName, this);
104*1b191cb5SApple OSS Distributions 
105*1b191cb5SApple OSS Distributions 	// All done, so return true.
106*1b191cb5SApple OSS Distributions 	return true;
107*1b191cb5SApple OSS Distributions }
108*1b191cb5SApple OSS Distributions 
109*1b191cb5SApple OSS Distributions IOReturn
getInterruptType(IOService * nub,int source,int * interruptType)110*1b191cb5SApple OSS Distributions GenericInterruptController::getInterruptType(IOService *nub,
111*1b191cb5SApple OSS Distributions     int source,
112*1b191cb5SApple OSS Distributions     int *interruptType)
113*1b191cb5SApple OSS Distributions {
114*1b191cb5SApple OSS Distributions 	if (interruptType == 0) {
115*1b191cb5SApple OSS Distributions 		return kIOReturnBadArgument;
116*1b191cb5SApple OSS Distributions 	}
117*1b191cb5SApple OSS Distributions 
118*1b191cb5SApple OSS Distributions 	// Given the nub and source, set interruptType to level or edge.
119*1b191cb5SApple OSS Distributions 
120*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
121*1b191cb5SApple OSS Distributions }
122*1b191cb5SApple OSS Distributions 
123*1b191cb5SApple OSS Distributions // Sadly this just has to be replicated in every interrupt controller.
124*1b191cb5SApple OSS Distributions IOInterruptAction
getInterruptHandlerAddress(void)125*1b191cb5SApple OSS Distributions GenericInterruptController::getInterruptHandlerAddress(void)
126*1b191cb5SApple OSS Distributions {
127*1b191cb5SApple OSS Distributions 	return (IOInterruptAction)handleInterrupt;
128*1b191cb5SApple OSS Distributions }
129*1b191cb5SApple OSS Distributions 
130*1b191cb5SApple OSS Distributions // Handle all current interrupts.
131*1b191cb5SApple OSS Distributions IOReturn
handleInterrupt(void * refCon,IOService * nub,int source)132*1b191cb5SApple OSS Distributions GenericInterruptController::handleInterrupt(void * refCon,
133*1b191cb5SApple OSS Distributions     IOService * nub,
134*1b191cb5SApple OSS Distributions     int source)
135*1b191cb5SApple OSS Distributions {
136*1b191cb5SApple OSS Distributions 	IOInterruptVector *vector;
137*1b191cb5SApple OSS Distributions 	int               vectorNumber;
138*1b191cb5SApple OSS Distributions 
139*1b191cb5SApple OSS Distributions 	while (1) {
140*1b191cb5SApple OSS Distributions 		// Get vectorNumber from hardware some how and clear the event.
141*1b191cb5SApple OSS Distributions 
142*1b191cb5SApple OSS Distributions 		// Break if there are no more vectors to handle.
143*1b191cb5SApple OSS Distributions 		if (vectorNumber == 0 /*kNoVector*/) {
144*1b191cb5SApple OSS Distributions 			break;
145*1b191cb5SApple OSS Distributions 		}
146*1b191cb5SApple OSS Distributions 
147*1b191cb5SApple OSS Distributions 		// Get the vector's date from the controller's array.
148*1b191cb5SApple OSS Distributions 		vector = &vectors[vectorNumber];
149*1b191cb5SApple OSS Distributions 
150*1b191cb5SApple OSS Distributions 		// Set the vector as active. This store must compleat before
151*1b191cb5SApple OSS Distributions 		// moving on to prevent the disableInterrupt fuction from
152*1b191cb5SApple OSS Distributions 		// geting out of sync.
153*1b191cb5SApple OSS Distributions 		vector->interruptActive = 1;
154*1b191cb5SApple OSS Distributions 		//sync();
155*1b191cb5SApple OSS Distributions 		//isync();
156*1b191cb5SApple OSS Distributions 
157*1b191cb5SApple OSS Distributions 		// If the vector is not disabled soft, handle it.
158*1b191cb5SApple OSS Distributions 		if (!vector->interruptDisabledSoft) {
159*1b191cb5SApple OSS Distributions 			// Prevent speculative exacution as needed on your processor.
160*1b191cb5SApple OSS Distributions 			//isync();
161*1b191cb5SApple OSS Distributions 
162*1b191cb5SApple OSS Distributions 			// Call the handler if it exists.
163*1b191cb5SApple OSS Distributions 			if (vector->interruptRegistered) {
164*1b191cb5SApple OSS Distributions 				vector->handler(vector->target, vector->refCon,
165*1b191cb5SApple OSS Distributions 				    vector->nub, vector->source);
166*1b191cb5SApple OSS Distributions 			}
167*1b191cb5SApple OSS Distributions 		} else {
168*1b191cb5SApple OSS Distributions 			// Hard disable the vector if is was only soft disabled.
169*1b191cb5SApple OSS Distributions 			vector->interruptDisabledHard = 1;
170*1b191cb5SApple OSS Distributions 			disableVectorHard(vectorNumber, vector);
171*1b191cb5SApple OSS Distributions 		}
172*1b191cb5SApple OSS Distributions 
173*1b191cb5SApple OSS Distributions 		// Done with this vector so, set it back to inactive.
174*1b191cb5SApple OSS Distributions 		vector->interruptActive = 0;
175*1b191cb5SApple OSS Distributions 	}
176*1b191cb5SApple OSS Distributions 
177*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
178*1b191cb5SApple OSS Distributions }
179*1b191cb5SApple OSS Distributions 
180*1b191cb5SApple OSS Distributions bool
vectorCanBeShared(long vectorNumber,IOInterruptVector * vector)181*1b191cb5SApple OSS Distributions GenericInterruptController::vectorCanBeShared(long vectorNumber,
182*1b191cb5SApple OSS Distributions     IOInterruptVector *vector)
183*1b191cb5SApple OSS Distributions {
184*1b191cb5SApple OSS Distributions 	// Given the vector number and the vector data, return if it can be shared.
185*1b191cb5SApple OSS Distributions 	return true;
186*1b191cb5SApple OSS Distributions }
187*1b191cb5SApple OSS Distributions 
188*1b191cb5SApple OSS Distributions void
initVector(long vectorNumber,IOInterruptVector * vector)189*1b191cb5SApple OSS Distributions GenericInterruptController::initVector(long vectorNumber,
190*1b191cb5SApple OSS Distributions     IOInterruptVector *vector)
191*1b191cb5SApple OSS Distributions {
192*1b191cb5SApple OSS Distributions 	// Given the vector number and the vector data,
193*1b191cb5SApple OSS Distributions 	// get the hardware ready for the vector to generate interrupts.
194*1b191cb5SApple OSS Distributions 	// Make sure the vector is left disabled.
195*1b191cb5SApple OSS Distributions }
196*1b191cb5SApple OSS Distributions 
197*1b191cb5SApple OSS Distributions void
disableVectorHard(long vectorNumber,IOInterruptVector * vector)198*1b191cb5SApple OSS Distributions GenericInterruptController::disableVectorHard(long vectorNumber,
199*1b191cb5SApple OSS Distributions     IOInterruptVector *vector)
200*1b191cb5SApple OSS Distributions {
201*1b191cb5SApple OSS Distributions 	// Given the vector number and the vector data,
202*1b191cb5SApple OSS Distributions 	// disable the vector at the hardware.
203*1b191cb5SApple OSS Distributions }
204*1b191cb5SApple OSS Distributions 
205*1b191cb5SApple OSS Distributions void
enableVector(long vectorNumber,IOInterruptVector * vector)206*1b191cb5SApple OSS Distributions GenericInterruptController::enableVector(long vectorNumber,
207*1b191cb5SApple OSS Distributions     IOInterruptVector *vector)
208*1b191cb5SApple OSS Distributions {
209*1b191cb5SApple OSS Distributions 	// Given the vector number and the vector data,
210*1b191cb5SApple OSS Distributions 	// enable the vector at the hardware.
211*1b191cb5SApple OSS Distributions }
212*1b191cb5SApple OSS Distributions 
213*1b191cb5SApple OSS Distributions void
causeVector(long vectorNumber,IOInterruptVector * vector)214*1b191cb5SApple OSS Distributions GenericInterruptController::causeVector(long vectorNumber,
215*1b191cb5SApple OSS Distributions     IOInterruptVector *vector)
216*1b191cb5SApple OSS Distributions {
217*1b191cb5SApple OSS Distributions 	// Given the vector number and the vector data,
218*1b191cb5SApple OSS Distributions 	// Set the vector pending and cause an interrupt at the parent controller.
219*1b191cb5SApple OSS Distributions 
220*1b191cb5SApple OSS Distributions 	// cause the interrupt at the parent controller.  Source is usually zero,
221*1b191cb5SApple OSS Distributions 	// but it could be different for your controller.
222*1b191cb5SApple OSS Distributions 	getPlatform()->causeInterrupt(0);
223*1b191cb5SApple OSS Distributions }
224