]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOInterruptEventSource.cpp
xnu-1699.32.7.tar.gz
[apple/xnu.git] / iokit / Kernel / IOInterruptEventSource.cpp
CommitLineData
1c79356b 1/*
6d2010ae 2 * Copyright (c) 1998-2010 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
1c79356b 28
1c79356b 29#include <IOKit/IOInterruptEventSource.h>
060df5ea 30#include <IOKit/IOKitDebug.h>
1c79356b
A
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>
36
6d2010ae
A
37#if IOKITSTATS
38
39#define IOStatisticsInitializeCounter() \
40do { \
41 IOStatistics::setCounterType(IOEventSource::reserved->counter, kIOStatisticsInterruptEventSourceCounter); \
42} while (0)
43
44#define IOStatisticsCheckForWork() \
45do { \
46 IOStatistics::countInterruptCheckForWork(IOEventSource::reserved->counter); \
47} while (0)
48
49#define IOStatisticsInterrupt() \
50do { \
51 IOStatistics::countInterrupt(IOEventSource::reserved->counter); \
52} while (0)
53
54#else
55
56#define IOStatisticsInitializeCounter()
57#define IOStatisticsCheckForWork()
58#define IOStatisticsInterrupt()
59
60#endif // IOKITSTATS
61
1c79356b
A
62#define super IOEventSource
63
64OSDefineMetaClassAndStructors(IOInterruptEventSource, IOEventSource)
65OSMetaClassDefineReservedUnused(IOInterruptEventSource, 0);
66OSMetaClassDefineReservedUnused(IOInterruptEventSource, 1);
67OSMetaClassDefineReservedUnused(IOInterruptEventSource, 2);
68OSMetaClassDefineReservedUnused(IOInterruptEventSource, 3);
69OSMetaClassDefineReservedUnused(IOInterruptEventSource, 4);
70OSMetaClassDefineReservedUnused(IOInterruptEventSource, 5);
71OSMetaClassDefineReservedUnused(IOInterruptEventSource, 6);
72OSMetaClassDefineReservedUnused(IOInterruptEventSource, 7);
73
74bool IOInterruptEventSource::init(OSObject *inOwner,
55e303ae
A
75 Action inAction,
76 IOService *inProvider,
77 int inIntIndex)
1c79356b
A
78{
79 bool res = true;
80
81 if ( !super::init(inOwner, (IOEventSourceAction) inAction) )
82 return false;
83
84 provider = inProvider;
85 producerCount = consumerCount = 0;
86 autoDisable = explicitDisable = false;
060df5ea 87 intIndex = ~inIntIndex;
1c79356b
A
88
89 // Assumes inOwner holds a reference(retain) on the provider
90 if (inProvider) {
060df5ea
A
91 res = (kIOReturnSuccess == registerInterruptHandler(inProvider, inIntIndex));
92 if (res)
93 intIndex = inIntIndex;
1c79356b
A
94 }
95
6d2010ae
A
96 IOStatisticsInitializeCounter();
97
1c79356b
A
98 return res;
99}
100
060df5ea
A
101IOReturn IOInterruptEventSource::registerInterruptHandler(IOService *inProvider,
102 int inIntIndex)
103{
104 IOReturn ret;
105 int intType;
106 IOInterruptAction intHandler;
107
108 ret = inProvider->getInterruptType(inIntIndex, &intType);
109 if (kIOReturnSuccess != ret)
110 return (ret);
111
112 autoDisable = (intType == kIOInterruptTypeLevel);
113 if (autoDisable) {
114 intHandler = OSMemberFunctionCast(IOInterruptAction,
115 this, &IOInterruptEventSource::disableInterruptOccurred);
116 }
117 else
118 intHandler = OSMemberFunctionCast(IOInterruptAction,
119 this, &IOInterruptEventSource::normalInterruptOccurred);
120
121 ret = provider->registerInterrupt(inIntIndex, this, intHandler);
122
123 return (ret);
124}
125
1c79356b
A
126IOInterruptEventSource *
127IOInterruptEventSource::interruptEventSource(OSObject *inOwner,
128 Action inAction,
129 IOService *inProvider,
130 int inIntIndex)
131{
132 IOInterruptEventSource *me = new IOInterruptEventSource;
133
134 if (me && !me->init(inOwner, inAction, inProvider, inIntIndex)) {
55e303ae 135 me->release();
1c79356b
A
136 return 0;
137 }
138
139 return me;
140}
141
142void IOInterruptEventSource::free()
143{
060df5ea 144 if (provider && intIndex >= 0)
1c79356b
A
145 provider->unregisterInterrupt(intIndex);
146
147 super::free();
148}
149
150void IOInterruptEventSource::enable()
151{
060df5ea 152 if (provider && intIndex >= 0) {
1c79356b
A
153 provider->enableInterrupt(intIndex);
154 explicitDisable = false;
2d21ac55 155 enabled = true;
1c79356b
A
156 }
157}
158
159void IOInterruptEventSource::disable()
160{
060df5ea 161 if (provider && intIndex >= 0) {
1c79356b
A
162 provider->disableInterrupt(intIndex);
163 explicitDisable = true;
2d21ac55 164 enabled = false;
1c79356b
A
165 }
166}
167
060df5ea
A
168void IOInterruptEventSource::setWorkLoop(IOWorkLoop *inWorkLoop)
169{
170 super::setWorkLoop(inWorkLoop);
171
172 if (!provider)
173 return;
174
175 if ( !inWorkLoop ) {
176 if (intIndex >= 0) {
177 provider->unregisterInterrupt(intIndex);
178 intIndex = ~intIndex;
179 }
180 } else if ((intIndex < 0) && (kIOReturnSuccess == registerInterruptHandler(provider, ~intIndex))) {
181 intIndex = ~intIndex;
182 }
183}
184
1c79356b
A
185const IOService *IOInterruptEventSource::getProvider() const
186{
187 return provider;
188}
189
190int IOInterruptEventSource::getIntIndex() const
191{
192 return intIndex;
193}
194
195bool IOInterruptEventSource::getAutoDisable() const
196{
197 return autoDisable;
198}
199
200bool IOInterruptEventSource::checkForWork()
201{
202 unsigned int cacheProdCount = producerCount;
203 int numInts = cacheProdCount - consumerCount;
204 IOInterruptEventAction intAction = (IOInterruptEventAction) action;
060df5ea 205 bool trace = (gIOKitTrace & kIOTraceIntEventSource) ? true : false;
6d2010ae
A
206
207 IOStatisticsCheckForWork();
208
060df5ea
A
209 if ( numInts > 0 )
210 {
211 if (trace)
212 IOTimeStampStartConstant(IODBG_INTES(IOINTES_ACTION),
213 (uintptr_t) intAction, (uintptr_t) owner, (uintptr_t) this, (uintptr_t) workLoop);
6d2010ae 214
060df5ea 215 // Call the handler
6d2010ae 216 (*intAction)(owner, this, numInts);
060df5ea
A
217
218 if (trace)
219 IOTimeStampEndConstant(IODBG_INTES(IOINTES_ACTION),
220 (uintptr_t) intAction, (uintptr_t) owner, (uintptr_t) this, (uintptr_t) workLoop);
6d2010ae
A
221
222 consumerCount = cacheProdCount;
223 if (autoDisable && !explicitDisable)
224 enable();
225 }
060df5ea
A
226
227 else if ( numInts < 0 )
228 {
229 if (trace)
230 IOTimeStampStartConstant(IODBG_INTES(IOINTES_ACTION),
231 (uintptr_t) intAction, (uintptr_t) owner, (uintptr_t) this, (uintptr_t) workLoop);
232
233 // Call the handler
6d2010ae 234 (*intAction)(owner, this, -numInts);
060df5ea
A
235
236 if (trace)
237 IOTimeStampEndConstant(IODBG_INTES(IOINTES_ACTION),
238 (uintptr_t) intAction, (uintptr_t) owner, (uintptr_t) this, (uintptr_t) workLoop);
6d2010ae
A
239
240 consumerCount = cacheProdCount;
241 if (autoDisable && !explicitDisable)
242 enable();
243 }
244
1c79356b
A
245 return false;
246}
247
248void IOInterruptEventSource::normalInterruptOccurred
249 (void */*refcon*/, IOService */*prov*/, int /*source*/)
250{
060df5ea 251 bool trace = (gIOKitTrace & kIOTraceIntEventSource) ? true : false;
6d2010ae
A
252
253 IOStatisticsInterrupt();
1c79356b 254 producerCount++;
6d2010ae 255
060df5ea
A
256 if (trace)
257 IOTimeStampStartConstant(IODBG_INTES(IOINTES_SEMA), (uintptr_t) this, (uintptr_t) owner);
258
1c79356b 259 signalWorkAvailable();
6d2010ae 260
060df5ea
A
261 if (trace)
262 IOTimeStampEndConstant(IODBG_INTES(IOINTES_SEMA), (uintptr_t) this, (uintptr_t) owner);
1c79356b
A
263}
264
265void IOInterruptEventSource::disableInterruptOccurred
266 (void */*refcon*/, IOService *prov, int source)
267{
060df5ea 268 bool trace = (gIOKitTrace & kIOTraceIntEventSource) ? true : false;
6d2010ae 269
1c79356b 270 prov->disableInterrupt(source); /* disable the interrupt */
6d2010ae
A
271
272 IOStatisticsInterrupt();
1c79356b 273 producerCount++;
6d2010ae 274
060df5ea
A
275 if (trace)
276 IOTimeStampStartConstant(IODBG_INTES(IOINTES_SEMA), (uintptr_t) this, (uintptr_t) owner);
277
1c79356b 278 signalWorkAvailable();
6d2010ae 279
060df5ea
A
280 if (trace)
281 IOTimeStampEndConstant(IODBG_INTES(IOINTES_SEMA), (uintptr_t) this, (uintptr_t) owner);
1c79356b
A
282}
283
284void IOInterruptEventSource::interruptOccurred
285 (void *refcon, IOService *prov, int source)
286{
287 if (autoDisable && prov)
288 disableInterruptOccurred(refcon, prov, source);
289 else
290 normalInterruptOccurred(refcon, prov, source);
291}
6d2010ae
A
292
293IOReturn IOInterruptEventSource::warmCPU
294 (uint64_t abstime)
295{
296
297 return ml_interrupt_prewarm(abstime);
298}