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 super::setWorkLoop(inWorkLoop
);
177 provider
->unregisterInterrupt(intIndex
);
178 intIndex
= ~intIndex
;
180 } else if ((intIndex
< 0) && (kIOReturnSuccess
== registerInterruptHandler(provider
, ~intIndex
))) {
181 intIndex
= ~intIndex
;
185 const IOService
*IOInterruptEventSource::getProvider() const
190 int IOInterruptEventSource::getIntIndex() const
195 bool IOInterruptEventSource::getAutoDisable() const
200 bool IOInterruptEventSource::checkForWork()
202 unsigned int cacheProdCount
= producerCount
;
203 int numInts
= cacheProdCount
- consumerCount
;
204 IOInterruptEventAction intAction
= (IOInterruptEventAction
) action
;
205 bool trace
= (gIOKitTrace
& kIOTraceIntEventSource
) ? true : false;
207 IOStatisticsCheckForWork();
212 IOTimeStampStartConstant(IODBG_INTES(IOINTES_ACTION
),
213 (uintptr_t) intAction
, (uintptr_t) owner
, (uintptr_t) this, (uintptr_t) workLoop
);
216 (*intAction
)(owner
, this, numInts
);
219 IOTimeStampEndConstant(IODBG_INTES(IOINTES_ACTION
),
220 (uintptr_t) intAction
, (uintptr_t) owner
, (uintptr_t) this, (uintptr_t) workLoop
);
222 consumerCount
= cacheProdCount
;
223 if (autoDisable
&& !explicitDisable
)
227 else if ( numInts
< 0 )
230 IOTimeStampStartConstant(IODBG_INTES(IOINTES_ACTION
),
231 (uintptr_t) intAction
, (uintptr_t) owner
, (uintptr_t) this, (uintptr_t) workLoop
);
234 (*intAction
)(owner
, this, -numInts
);
237 IOTimeStampEndConstant(IODBG_INTES(IOINTES_ACTION
),
238 (uintptr_t) intAction
, (uintptr_t) owner
, (uintptr_t) this, (uintptr_t) workLoop
);
240 consumerCount
= cacheProdCount
;
241 if (autoDisable
&& !explicitDisable
)
248 void IOInterruptEventSource::normalInterruptOccurred
249 (void */
*refcon*/
, IOService */
*prov*/
, int /*source*/)
251 bool trace
= (gIOKitTrace
& kIOTraceIntEventSource
) ? true : false;
253 IOStatisticsInterrupt();
257 IOTimeStampStartConstant(IODBG_INTES(IOINTES_SEMA
), (uintptr_t) this, (uintptr_t) owner
);
259 signalWorkAvailable();
262 IOTimeStampEndConstant(IODBG_INTES(IOINTES_SEMA
), (uintptr_t) this, (uintptr_t) owner
);
265 void IOInterruptEventSource::disableInterruptOccurred
266 (void */
*refcon*/
, IOService
*prov
, int source
)
268 bool trace
= (gIOKitTrace
& kIOTraceIntEventSource
) ? true : false;
270 prov
->disableInterrupt(source
); /* disable the interrupt */
272 IOStatisticsInterrupt();
276 IOTimeStampStartConstant(IODBG_INTES(IOINTES_SEMA
), (uintptr_t) this, (uintptr_t) owner
);
278 signalWorkAvailable();
281 IOTimeStampEndConstant(IODBG_INTES(IOINTES_SEMA
), (uintptr_t) this, (uintptr_t) owner
);
284 void IOInterruptEventSource::interruptOccurred
285 (void *refcon
, IOService
*prov
, int source
)
287 if (autoDisable
&& prov
)
288 disableInterruptOccurred(refcon
, prov
, source
);
290 normalInterruptOccurred(refcon
, prov
, source
);
293 IOReturn
IOInterruptEventSource::warmCPU
297 return ml_interrupt_prewarm(abstime
);