]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOFilterInterruptEventSource.cpp
e3b9803cf3b813343fadfae0401b7e032a681a2c
[apple/xnu.git] / iokit / Kernel / IOFilterInterruptEventSource.cpp
1 /*
2 * Copyright (c) 1998-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <IOKit/IOFilterInterruptEventSource.h>
30 #include <IOKit/IOService.h>
31 #include <IOKit/IOKitDebug.h>
32 #include <IOKit/IOTimeStamp.h>
33 #include <IOKit/IOWorkLoop.h>
34 #include <IOKit/IOInterruptAccountingPrivate.h>
35 #include <libkern/Block.h>
36
37 #if IOKITSTATS
38
39 #define IOStatisticsInitializeCounter() \
40 do { \
41 IOStatistics::setCounterType(IOEventSource::reserved->counter, kIOStatisticsFilterInterruptEventSourceCounter); \
42 } while (0)
43
44 #define IOStatisticsInterrupt() \
45 do { \
46 IOStatistics::countInterrupt(IOEventSource::reserved->counter); \
47 } while (0)
48
49 #else
50
51 #define IOStatisticsInitializeCounter()
52 #define IOStatisticsInterrupt()
53
54 #endif /* IOKITSTATS */
55
56 #define super IOInterruptEventSource
57
58 OSDefineMetaClassAndStructors
59 (IOFilterInterruptEventSource, IOInterruptEventSource)
60 OSMetaClassDefineReservedUnused(IOFilterInterruptEventSource, 0);
61 OSMetaClassDefineReservedUnused(IOFilterInterruptEventSource, 1);
62 OSMetaClassDefineReservedUnused(IOFilterInterruptEventSource, 2);
63 OSMetaClassDefineReservedUnused(IOFilterInterruptEventSource, 3);
64 OSMetaClassDefineReservedUnused(IOFilterInterruptEventSource, 4);
65 OSMetaClassDefineReservedUnused(IOFilterInterruptEventSource, 5);
66 OSMetaClassDefineReservedUnused(IOFilterInterruptEventSource, 6);
67 OSMetaClassDefineReservedUnused(IOFilterInterruptEventSource, 7);
68
69 /*
70 * Implement the call throughs for the private protection conversion
71 */
72 bool
73 IOFilterInterruptEventSource::init(OSObject *inOwner,
74 Action inAction,
75 IOService *inProvider,
76 int inIntIndex)
77 {
78 return false;
79 }
80
81 IOInterruptEventSource *
82 IOFilterInterruptEventSource::interruptEventSource(OSObject *inOwner,
83 Action inAction,
84 IOService *inProvider,
85 int inIntIndex)
86 {
87 return 0;
88 }
89
90 bool
91 IOFilterInterruptEventSource::init(OSObject *inOwner,
92 Action inAction,
93 Filter inFilterAction,
94 IOService *inProvider,
95 int inIntIndex)
96 {
97 if (!super::init(inOwner, inAction, inProvider, inIntIndex)) {
98 return false;
99 }
100
101 if (!inFilterAction) {
102 return false;
103 }
104
105 filterAction = inFilterAction;
106
107 IOStatisticsInitializeCounter();
108
109 return true;
110 }
111
112 IOFilterInterruptEventSource *
113 IOFilterInterruptEventSource
114 ::filterInterruptEventSource(OSObject *inOwner,
115 Action inAction,
116 Filter inFilterAction,
117 IOService *inProvider,
118 int inIntIndex)
119 {
120 IOFilterInterruptEventSource *me = new IOFilterInterruptEventSource;
121
122 if (me
123 && !me->init(inOwner, inAction, inFilterAction, inProvider, inIntIndex)) {
124 me->release();
125 return 0;
126 }
127
128 return me;
129 }
130
131
132 IOFilterInterruptEventSource *
133 IOFilterInterruptEventSource
134 ::filterInterruptEventSource(OSObject *inOwner,
135 IOService *inProvider,
136 int inIntIndex,
137 ActionBlock inAction,
138 FilterBlock inFilterAction)
139 {
140 IOFilterInterruptEventSource *me = new IOFilterInterruptEventSource;
141
142 FilterBlock filter = Block_copy(inFilterAction);
143 if (!filter) {
144 return 0;
145 }
146
147 if (me
148 && !me->init(inOwner, (Action) NULL, (Filter) filter, inProvider, inIntIndex)) {
149 me->release();
150 Block_release(filter);
151 return 0;
152 }
153 me->flags |= kFilterBlock;
154 me->setActionBlock((IOEventSource::ActionBlock) inAction);
155
156 return me;
157 }
158
159
160 void
161 IOFilterInterruptEventSource::free( void )
162 {
163 if ((kFilterBlock & flags) && filterActionBlock) {
164 Block_release(filterActionBlock);
165 }
166
167 super::free();
168 }
169
170 void
171 IOFilterInterruptEventSource::signalInterrupt()
172 {
173 bool trace = (gIOKitTrace & kIOTraceIntEventSource) ? true : false;
174
175 IOStatisticsInterrupt();
176 producerCount++;
177
178 if (trace) {
179 IOTimeStampStartConstant(IODBG_INTES(IOINTES_SEMA), VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(owner));
180 }
181
182 signalWorkAvailable();
183
184 if (trace) {
185 IOTimeStampEndConstant(IODBG_INTES(IOINTES_SEMA), VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(owner));
186 }
187 }
188
189
190 IOFilterInterruptEventSource::Filter
191 IOFilterInterruptEventSource::getFilterAction() const
192 {
193 if (kFilterBlock & flags) {
194 return NULL;
195 }
196 return filterAction;
197 }
198
199 IOFilterInterruptEventSource::FilterBlock
200 IOFilterInterruptEventSource::getFilterActionBlock() const
201 {
202 if (kFilterBlock & flags) {
203 return filterActionBlock;
204 }
205 return NULL;
206 }
207
208 void
209 IOFilterInterruptEventSource::normalInterruptOccurred
210 (void */*refcon*/, IOService */*prov*/, int /*source*/)
211 {
212 bool filterRes;
213 uint64_t startTime = 0;
214 uint64_t endTime = 0;
215 bool trace = (gIOKitTrace & kIOTraceIntEventSource) ? true : false;
216
217 if (trace) {
218 IOTimeStampStartConstant(IODBG_INTES(IOINTES_FILTER),
219 VM_KERNEL_UNSLIDE(filterAction), VM_KERNEL_ADDRHIDE(owner), VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(workLoop));
220 }
221
222 if (IOInterruptEventSource::reserved->statistics) {
223 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingFirstLevelTimeIndex)) {
224 startTime = mach_absolute_time();
225 }
226 }
227
228 // Call the filter.
229 if (kFilterBlock & flags) {
230 filterRes = (filterActionBlock)(this);
231 } else {
232 filterRes = (*filterAction)(owner, this);
233 }
234
235 if (IOInterruptEventSource::reserved->statistics) {
236 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingFirstLevelCountIndex)) {
237 IA_ADD_VALUE(&IOInterruptEventSource::reserved->statistics->interruptStatistics[kInterruptAccountingFirstLevelCountIndex], 1);
238 }
239
240 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingFirstLevelTimeIndex)) {
241 endTime = mach_absolute_time();
242 IA_ADD_VALUE(&IOInterruptEventSource::reserved->statistics->interruptStatistics[kInterruptAccountingFirstLevelTimeIndex], endTime - startTime);
243 }
244 }
245
246 if (trace) {
247 IOTimeStampEndConstant(IODBG_INTES(IOINTES_FILTER),
248 VM_KERNEL_ADDRHIDE(filterAction), VM_KERNEL_ADDRHIDE(owner),
249 VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(workLoop));
250 }
251
252 if (filterRes) {
253 signalInterrupt();
254 }
255 }
256
257 void
258 IOFilterInterruptEventSource::disableInterruptOccurred
259 (void */*refcon*/, IOService *prov, int source)
260 {
261 bool filterRes;
262 uint64_t startTime = 0;
263 uint64_t endTime = 0;
264 bool trace = (gIOKitTrace & kIOTraceIntEventSource) ? true : false;
265
266 if (trace) {
267 IOTimeStampStartConstant(IODBG_INTES(IOINTES_FILTER),
268 VM_KERNEL_UNSLIDE(filterAction), VM_KERNEL_ADDRHIDE(owner), VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(workLoop));
269 }
270
271 if (IOInterruptEventSource::reserved->statistics) {
272 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingFirstLevelTimeIndex)) {
273 startTime = mach_absolute_time();
274 }
275 }
276
277 // Call the filter.
278 if (kFilterBlock & flags) {
279 filterRes = (filterActionBlock)(this);
280 } else {
281 filterRes = (*filterAction)(owner, this);
282 }
283
284 if (IOInterruptEventSource::reserved->statistics) {
285 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingFirstLevelCountIndex)) {
286 IA_ADD_VALUE(&IOInterruptEventSource::reserved->statistics->interruptStatistics[kInterruptAccountingFirstLevelCountIndex], 1);
287 }
288
289 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingFirstLevelTimeIndex)) {
290 endTime = mach_absolute_time();
291 IA_ADD_VALUE(&IOInterruptEventSource::reserved->statistics->interruptStatistics[kInterruptAccountingFirstLevelTimeIndex], endTime - startTime);
292 }
293 }
294
295 if (trace) {
296 IOTimeStampEndConstant(IODBG_INTES(IOINTES_FILTER),
297 VM_KERNEL_UNSLIDE(filterAction), VM_KERNEL_ADDRHIDE(owner), VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(workLoop));
298 }
299
300 if (filterRes) {
301 prov->disableInterrupt(source); /* disable the interrupt */
302 signalInterrupt();
303 }
304 }