2 * Copyright (c) 1998-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <IOKit/IOInterruptEventSource.h>
30 #include <IOKit/IOKitDebug.h>
31 #include <IOKit/IOLib.h>
32 #include <IOKit/IOService.h>
33 #include <IOKit/IOInterrupts.h>
34 #include <IOKit/IOTimeStamp.h>
35 #include <IOKit/IOWorkLoop.h>
39 #define IOStatisticsInitializeCounter() \
41 IOStatistics::setCounterType(IOEventSource::reserved->counter, kIOStatisticsInterruptEventSourceCounter); \
44 #define IOStatisticsCheckForWork() \
46 IOStatistics::countInterruptCheckForWork(IOEventSource::reserved->counter); \
49 #define IOStatisticsInterrupt() \
51 IOStatistics::countInterrupt(IOEventSource::reserved->counter); \
56 #define IOStatisticsInitializeCounter()
57 #define IOStatisticsCheckForWork()
58 #define IOStatisticsInterrupt()
62 #define super IOEventSource
64 OSDefineMetaClassAndStructors(IOInterruptEventSource
, IOEventSource
)
65 OSMetaClassDefineReservedUnused(IOInterruptEventSource
, 0);
66 OSMetaClassDefineReservedUnused(IOInterruptEventSource
, 1);
67 OSMetaClassDefineReservedUnused(IOInterruptEventSource
, 2);
68 OSMetaClassDefineReservedUnused(IOInterruptEventSource
, 3);
69 OSMetaClassDefineReservedUnused(IOInterruptEventSource
, 4);
70 OSMetaClassDefineReservedUnused(IOInterruptEventSource
, 5);
71 OSMetaClassDefineReservedUnused(IOInterruptEventSource
, 6);
72 OSMetaClassDefineReservedUnused(IOInterruptEventSource
, 7);
74 bool IOInterruptEventSource::init(OSObject
*inOwner
,
76 IOService
*inProvider
,
81 if ( !super::init(inOwner
, (IOEventSourceAction
) inAction
) )
84 provider
= inProvider
;
85 producerCount
= consumerCount
= 0;
86 autoDisable
= explicitDisable
= false;
87 intIndex
= ~inIntIndex
;
89 // Assumes inOwner holds a reference(retain) on the provider
91 res
= (kIOReturnSuccess
== registerInterruptHandler(inProvider
, inIntIndex
));
93 intIndex
= inIntIndex
;
96 IOStatisticsInitializeCounter();
101 IOReturn
IOInterruptEventSource::registerInterruptHandler(IOService
*inProvider
,
106 IOInterruptAction intHandler
;
108 ret
= inProvider
->getInterruptType(inIntIndex
, &intType
);
109 if (kIOReturnSuccess
!= ret
)
112 autoDisable
= (intType
== kIOInterruptTypeLevel
);
114 intHandler
= OSMemberFunctionCast(IOInterruptAction
,
115 this, &IOInterruptEventSource::disableInterruptOccurred
);
118 intHandler
= OSMemberFunctionCast(IOInterruptAction
,
119 this, &IOInterruptEventSource::normalInterruptOccurred
);
121 ret
= provider
->registerInterrupt(inIntIndex
, this, intHandler
);
126 IOInterruptEventSource
*
127 IOInterruptEventSource::interruptEventSource(OSObject
*inOwner
,
129 IOService
*inProvider
,
132 IOInterruptEventSource
*me
= new IOInterruptEventSource
;
134 if (me
&& !me
->init(inOwner
, inAction
, inProvider
, inIntIndex
)) {
142 void IOInterruptEventSource::free()
144 if (provider
&& intIndex
>= 0)
145 provider
->unregisterInterrupt(intIndex
);
150 void IOInterruptEventSource::enable()
152 if (provider
&& intIndex
>= 0) {
153 provider
->enableInterrupt(intIndex
);
154 explicitDisable
= false;
159 void IOInterruptEventSource::disable()
161 if (provider
&& intIndex
>= 0) {
162 provider
->disableInterrupt(intIndex
);
163 explicitDisable
= true;
168 void IOInterruptEventSource::setWorkLoop(IOWorkLoop
*inWorkLoop
)
170 if (inWorkLoop
) super::setWorkLoop(inWorkLoop
);
175 provider
->unregisterInterrupt(intIndex
);
176 intIndex
= ~intIndex
;
178 } else if ((intIndex
< 0) && (kIOReturnSuccess
== registerInterruptHandler(provider
, ~intIndex
))) {
179 intIndex
= ~intIndex
;
183 if (!inWorkLoop
) super::setWorkLoop(inWorkLoop
);
186 const IOService
*IOInterruptEventSource::getProvider() const
191 int IOInterruptEventSource::getIntIndex() const
196 bool IOInterruptEventSource::getAutoDisable() const
201 bool IOInterruptEventSource::checkForWork()
203 unsigned int cacheProdCount
= producerCount
;
204 int numInts
= cacheProdCount
- consumerCount
;
205 IOInterruptEventAction intAction
= (IOInterruptEventAction
) action
;
206 bool trace
= (gIOKitTrace
& kIOTraceIntEventSource
) ? true : false;
208 IOStatisticsCheckForWork();
213 IOTimeStampStartConstant(IODBG_INTES(IOINTES_ACTION
),
214 VM_KERNEL_UNSLIDE(intAction
), (uintptr_t) owner
, (uintptr_t) this, (uintptr_t) workLoop
);
217 (*intAction
)(owner
, this, numInts
);
220 IOTimeStampEndConstant(IODBG_INTES(IOINTES_ACTION
),
221 VM_KERNEL_UNSLIDE(intAction
), (uintptr_t) owner
, (uintptr_t) this, (uintptr_t) workLoop
);
223 consumerCount
= cacheProdCount
;
224 if (autoDisable
&& !explicitDisable
)
228 else if ( numInts
< 0 )
231 IOTimeStampStartConstant(IODBG_INTES(IOINTES_ACTION
),
232 VM_KERNEL_UNSLIDE(intAction
), (uintptr_t) owner
, (uintptr_t) this, (uintptr_t) workLoop
);
235 (*intAction
)(owner
, this, -numInts
);
238 IOTimeStampEndConstant(IODBG_INTES(IOINTES_ACTION
),
239 VM_KERNEL_UNSLIDE(intAction
), (uintptr_t) owner
, (uintptr_t) this, (uintptr_t) workLoop
);
241 consumerCount
= cacheProdCount
;
242 if (autoDisable
&& !explicitDisable
)
249 void IOInterruptEventSource::normalInterruptOccurred
250 (void */
*refcon*/
, IOService */
*prov*/
, int /*source*/)
252 bool trace
= (gIOKitTrace
& kIOTraceIntEventSource
) ? true : false;
254 IOStatisticsInterrupt();
258 IOTimeStampStartConstant(IODBG_INTES(IOINTES_SEMA
), (uintptr_t) this, (uintptr_t) owner
);
260 signalWorkAvailable();
263 IOTimeStampEndConstant(IODBG_INTES(IOINTES_SEMA
), (uintptr_t) this, (uintptr_t) owner
);
266 void IOInterruptEventSource::disableInterruptOccurred
267 (void */
*refcon*/
, IOService
*prov
, int source
)
269 bool trace
= (gIOKitTrace
& kIOTraceIntEventSource
) ? true : false;
271 prov
->disableInterrupt(source
); /* disable the interrupt */
273 IOStatisticsInterrupt();
277 IOTimeStampStartConstant(IODBG_INTES(IOINTES_SEMA
), (uintptr_t) this, (uintptr_t) owner
);
279 signalWorkAvailable();
282 IOTimeStampEndConstant(IODBG_INTES(IOINTES_SEMA
), (uintptr_t) this, (uintptr_t) owner
);
285 void IOInterruptEventSource::interruptOccurred
286 (void *refcon
, IOService
*prov
, int source
)
288 if (autoDisable
&& prov
)
289 disableInterruptOccurred(refcon
, prov
, source
);
291 normalInterruptOccurred(refcon
, prov
, source
);
294 IOReturn
IOInterruptEventSource::warmCPU
298 return ml_interrupt_prewarm(abstime
);