]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOInterruptController.cpp
xnu-6153.141.1.tar.gz
[apple/xnu.git] / iokit / Kernel / IOInterruptController.cpp
1 /*
2 * Copyright (c) 2007-2012 Apple Inc. All rights reserved.
3 * Copyright (c) 1998-2006 Apple Computer, Inc. All rights reserved.
4 *
5 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 *
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.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 *
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
21 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
22 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 *
27 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 */
29
30 #include <IOKit/IOLib.h>
31 #include <IOKit/IOService.h>
32 #include <IOKit/IOPlatformExpert.h>
33 #include <IOKit/IODeviceTreeSupport.h>
34 #include <IOKit/IOInterrupts.h>
35 #include <IOKit/IOInterruptController.h>
36 #include <IOKit/IOKitDebug.h>
37 #include <IOKit/IOTimeStamp.h>
38
39
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
55 IOReturn
56 IOInterruptController::registerInterrupt(IOService *nub, int source,
57 void *target,
58 IOInterruptHandler handler,
59 void *refCon)
60 {
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);
85 #if defined(__i386__) || defined(__x86_64__)
86 int interruptType;
87 if (OSDynamicCast(IOPlatformDevice, getProvider()) &&
88 (getInterruptType(nub, source, &interruptType) == kIOReturnSuccess) &&
89 (kIOInterruptTypeLevel & interruptType)) {
90 options |= kIODTInterruptShared;
91 }
92 #endif
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;
100 }
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.
107 if (vector->sharedController == NULL) {
108 // Make the IOShareInterruptController instance
109 vector->sharedController = new IOSharedInterruptController;
110 if (vector->sharedController == NULL) {
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();
136 vector->sharedController = NULL;
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();
166 vector->sharedController = NULL;
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;
177 vector->refCon = NULL;
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;
227 }
228
229 IOReturn
230 IOInterruptController::unregisterInterrupt(IOService *nub, int source)
231 {
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;
262 vector->nub = NULL;
263 vector->source = 0;
264 vector->handler = NULL;
265 vector->target = NULL;
266 vector->refCon = NULL;
267
268 IOLockUnlock(vector->interruptLock);
269 return kIOReturnSuccess;
270 }
271
272 IOReturn
273 IOInterruptController::getInterruptType(IOService *nub, int source,
274 int *interruptType)
275 {
276 IOInterruptSource *interruptSources;
277 IOInterruptVectorNumber vectorNumber;
278 IOInterruptVector *vector;
279 OSData *vectorData;
280
281 if (interruptType == NULL) {
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;
293 }
294
295 IOReturn
296 IOInterruptController::enableInterrupt(IOService *nub, int source)
297 {
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;
310 #if !defined(__i386__) && !defined(__x86_64__)
311 OSMemoryBarrier();
312 #endif
313
314 if (!getPlatform()->atInterruptLevel()) {
315 while (vector->interruptActive) {
316 }
317 }
318 if (vector->interruptDisabledHard) {
319 vector->interruptDisabledHard = 0;
320 #if !defined(__i386__) && !defined(__x86_64__)
321 OSMemoryBarrier();
322 #endif
323 enableVector(vectorNumber, vector);
324 }
325 }
326
327 return kIOReturnSuccess;
328 }
329
330 IOReturn
331 IOInterruptController::disableInterrupt(IOService *nub, int source)
332 {
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;
344 #if !defined(__i386__) && !defined(__x86_64__)
345 OSMemoryBarrier();
346 #endif
347
348 if (!getPlatform()->atInterruptLevel()) {
349 while (vector->interruptActive) {
350 }
351 }
352
353 return kIOReturnSuccess;
354 }
355
356 IOReturn
357 IOInterruptController::causeInterrupt(IOService *nub, int source)
358 {
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;
372 }
373
374 IOInterruptAction
375 IOInterruptController::getInterruptHandlerAddress(void)
376 {
377 return NULL;
378 }
379
380 IOReturn
381 IOInterruptController::handleInterrupt(void *refCon, IOService *nub,
382 int source)
383 {
384 return kIOReturnInvalid;
385 }
386
387
388 // Methods to be overridden for simplifed interrupt controller subclasses.
389
390 bool
391 IOInterruptController::vectorCanBeShared(IOInterruptVectorNumber /*vectorNumber*/,
392 IOInterruptVector */*vector*/)
393 {
394 return false;
395 }
396
397 void
398 IOInterruptController::initVector(IOInterruptVectorNumber /*vectorNumber*/,
399 IOInterruptVector */*vector*/)
400 {
401 }
402
403 int
404 IOInterruptController::getVectorType(IOInterruptVectorNumber /*vectorNumber*/,
405 IOInterruptVector */*vector*/)
406 {
407 return kIOInterruptTypeEdge;
408 }
409
410 void
411 IOInterruptController::disableVectorHard(IOInterruptVectorNumber /*vectorNumber*/,
412 IOInterruptVector */*vector*/)
413 {
414 }
415
416 void
417 IOInterruptController::enableVector(IOInterruptVectorNumber /*vectorNumber*/,
418 IOInterruptVector */*vector*/)
419 {
420 }
421
422 void
423 IOInterruptController::causeVector(IOInterruptVectorNumber /*vectorNumber*/,
424 IOInterruptVector */*vector*/)
425 {
426 }
427
428 void
429 IOInterruptController::timeStampSpuriousInterrupt(void)
430 {
431 uint64_t providerID = 0;
432 IOService * provider = getProvider();
433
434 if (provider) {
435 providerID = provider->getRegistryEntryID();
436 }
437
438 IOTimeStampConstant(IODBG_INTC(IOINTC_SPURIOUS), providerID);
439 }
440
441 void
442 IOInterruptController::timeStampInterruptHandlerInternal(bool isStart, IOInterruptVectorNumber vectorNumber, IOInterruptVector *vector)
443 {
444 uint64_t providerID = 0;
445 vm_offset_t unslidHandler = 0;
446 vm_offset_t unslidTarget = 0;
447
448 IOService * provider = getProvider();
449
450 if (provider) {
451 providerID = provider->getRegistryEntryID();
452 }
453
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 }
458
459
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 }
467 }
468
469 void
470 IOInterruptController::timeStampInterruptHandlerStart(IOInterruptVectorNumber vectorNumber, IOInterruptVector *vector)
471 {
472 timeStampInterruptHandlerInternal(true, vectorNumber, vector);
473 }
474
475 void
476 IOInterruptController::timeStampInterruptHandlerEnd(IOInterruptVectorNumber vectorNumber, IOInterruptVector *vector)
477 {
478 timeStampInterruptHandlerInternal(false, vectorNumber, vector);
479 }
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
495 #define kIOSharedInterruptControllerDefaultVectors (128)
496
497 IOReturn
498 IOSharedInterruptController::initInterruptController(IOInterruptController *parentController, OSData *parentSource)
499 {
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));
512 if (_interruptSources == NULL) {
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();
542 if (controllerLock == NULL) {
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;
565 }
566
567 IOReturn
568 IOSharedInterruptController::registerInterrupt(IOService *nub,
569 int source,
570 void *target,
571 IOInterruptHandler handler,
572 void *refCon)
573 {
574 IOInterruptSource *interruptSources;
575 IOInterruptVectorNumber vectorNumber;
576 IOInterruptVector *vector = NULL;
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));
612 if (vectorData == NULL) {
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;
641 }
642
643 IOReturn
644 IOSharedInterruptController::unregisterInterrupt(IOService *nub,
645 int source)
646 {
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;
672 vector->nub = NULL;
673 vector->source = 0;
674 vector->handler = NULL;
675 vector->target = NULL;
676 vector->refCon = NULL;
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;
693 }
694
695 IOReturn
696 IOSharedInterruptController::getInterruptType(IOService */*nub*/,
697 int /*source*/,
698 int *interruptType)
699 {
700 return provider->getInterruptType(0, interruptType);
701 }
702
703 IOReturn
704 IOSharedInterruptController::enableInterrupt(IOService *nub,
705 int source)
706 {
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;
734 }
735
736 IOReturn
737 IOSharedInterruptController::disableInterrupt(IOService *nub,
738 int source)
739 {
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;
754 #if !defined(__i386__) && !defined(__x86_64__)
755 OSMemoryBarrier();
756 #endif
757
758 vectorsEnabled--;
759 }
760 IOSimpleLockUnlockEnableInterrupt(controllerLock, interruptState);
761
762 if (!getPlatform()->atInterruptLevel()) {
763 while (vector->interruptActive) {
764 }
765 }
766
767 return kIOReturnSuccess;
768 }
769
770 IOInterruptAction
771 IOSharedInterruptController::getInterruptHandlerAddress(void)
772 {
773 return OSMemberFunctionCast(IOInterruptAction,
774 this, &IOSharedInterruptController::handleInterrupt);
775 }
776
777 IOReturn
778 IOSharedInterruptController::handleInterrupt(void * /*refCon*/,
779 IOService * nub,
780 int /*source*/)
781 {
782 IOInterruptVectorNumber vectorNumber;
783 IOInterruptVector *vector;
784
785 for (vectorNumber = 0; vectorNumber < numVectors; vectorNumber++) {
786 vector = &vectors[vectorNumber];
787
788 vector->interruptActive = 1;
789 #if !defined(__i386__) && !defined(__x86_64__)
790 OSMemoryBarrier();
791 #endif
792
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 }
810
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 }