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