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