]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOInterruptEventSource.cpp
xnu-6153.141.1.tar.gz
[apple/xnu.git] / iokit / Kernel / IOInterruptEventSource.cpp
CommitLineData
1c79356b 1/*
fe8ab488 2 * Copyright (c) 1998-2014 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 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.
0a7de745 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.
0a7de745 17 *
2d21ac55
A
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.
0a7de745 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>
fe8ab488 36#include <IOKit/IOInterruptAccountingPrivate.h>
1c79356b 37
6d2010ae
A
38#if IOKITSTATS
39
40#define IOStatisticsInitializeCounter() \
41do { \
42 IOStatistics::setCounterType(IOEventSource::reserved->counter, kIOStatisticsInterruptEventSourceCounter); \
43} while (0)
44
45#define IOStatisticsCheckForWork() \
46do { \
47 IOStatistics::countInterruptCheckForWork(IOEventSource::reserved->counter); \
48} while (0)
49
50#define IOStatisticsInterrupt() \
51do { \
52 IOStatistics::countInterrupt(IOEventSource::reserved->counter); \
53} while (0)
54
55#else
56
57#define IOStatisticsInitializeCounter()
58#define IOStatisticsCheckForWork()
59#define IOStatisticsInterrupt()
60
61#endif // IOKITSTATS
62
1c79356b
A
63#define super IOEventSource
64
65OSDefineMetaClassAndStructors(IOInterruptEventSource, IOEventSource)
66OSMetaClassDefineReservedUnused(IOInterruptEventSource, 0);
67OSMetaClassDefineReservedUnused(IOInterruptEventSource, 1);
68OSMetaClassDefineReservedUnused(IOInterruptEventSource, 2);
69OSMetaClassDefineReservedUnused(IOInterruptEventSource, 3);
70OSMetaClassDefineReservedUnused(IOInterruptEventSource, 4);
71OSMetaClassDefineReservedUnused(IOInterruptEventSource, 5);
72OSMetaClassDefineReservedUnused(IOInterruptEventSource, 6);
73OSMetaClassDefineReservedUnused(IOInterruptEventSource, 7);
74
0a7de745
A
75bool
76IOInterruptEventSource::init(OSObject *inOwner,
77 Action inAction,
78 IOService *inProvider,
79 int inIntIndex)
1c79356b 80{
0a7de745
A
81 bool res = true;
82
83 if (!super::init(inOwner, (IOEventSourceAction) inAction)) {
84 return false;
85 }
1c79356b 86
0a7de745 87 reserved = IONew(ExpansionData, 1);
1c79356b 88
0a7de745
A
89 if (!reserved) {
90 return false;
91 }
fe8ab488 92
0a7de745
A
93 bzero(reserved, sizeof(ExpansionData));
94
95 provider = inProvider;
96 producerCount = consumerCount = 0;
97 autoDisable = explicitDisable = false;
98 intIndex = ~inIntIndex;
99
100 // Assumes inOwner holds a reference(retain) on the provider
101 if (inProvider) {
102 if (IA_ANY_STATISTICS_ENABLED) {
103 /*
104 * We only treat this as an "interrupt" if it has a provider; if it does,
105 * set up the objects necessary to track interrupt statistics. Interrupt
106 * event sources without providers are most likely being used as simple
107 * event source in order to poke at workloops and kick off work.
108 *
109 * We also avoid try to avoid interrupt accounting overhead if none of
110 * the statistics are enabled.
111 */
112 reserved->statistics = IONew(IOInterruptAccountingData, 1);
113
114 if (!reserved->statistics) {
115 /*
116 * We rely on the free() routine to clean up after us if init fails
117 * midway.
118 */
119 return false;
120 }
fe8ab488 121
0a7de745 122 bzero(reserved->statistics, sizeof(IOInterruptAccountingData));
fe8ab488 123
0a7de745
A
124 reserved->statistics->owner = this;
125 }
1c79356b 126
0a7de745 127 res = (kIOReturnSuccess == registerInterruptHandler(inProvider, inIntIndex));
fe8ab488 128
0a7de745
A
129 if (res) {
130 intIndex = inIntIndex;
131 }
132 }
fe8ab488 133
0a7de745 134 IOStatisticsInitializeCounter();
fe8ab488 135
0a7de745
A
136 return res;
137}
fe8ab488 138
0a7de745
A
139IOReturn
140IOInterruptEventSource::registerInterruptHandler(IOService *inProvider,
141 int inIntIndex)
142{
143 IOReturn ret;
144 int intType;
145 IOInterruptAction intHandler;
fe8ab488 146
0a7de745
A
147 ret = inProvider->getInterruptType(inIntIndex, &intType);
148 if (kIOReturnSuccess != ret) {
149 return ret;
150 }
1c79356b 151
0a7de745
A
152 autoDisable = (intType == kIOInterruptTypeLevel);
153 if (autoDisable) {
154 intHandler = OSMemberFunctionCast(IOInterruptAction,
155 this, &IOInterruptEventSource::disableInterruptOccurred);
156 } else {
157 intHandler = OSMemberFunctionCast(IOInterruptAction,
158 this, &IOInterruptEventSource::normalInterruptOccurred);
159 }
6d2010ae 160
0a7de745
A
161 ret = provider->registerInterrupt(inIntIndex, this, intHandler);
162
163 /*
164 * Add statistics to the provider. The setWorkLoop convention should ensure
165 * that we always go down the unregister path before we register (outside of
166 * init()), so we don't have to worry that we will invoke addInterruptStatistics
167 * erroneously.
168 */
169 if ((ret == kIOReturnSuccess) && (reserved->statistics)) {
170 /*
171 * Stash the normal index value, for the sake of debugging.
172 */
173 reserved->statistics->interruptIndex = inIntIndex;
174
175 /*
176 * We need to hook the interrupt information up to the provider so that it
177 * can find the statistics for this interrupt when desired. The provider is
178 * responsible for maintaining the reporter for a particular interrupt, and
179 * needs a handle on the statistics so that it can request that the reporter
180 * be updated as needed. Errors are considered "soft" for the moment (it
181 * will either panic, or fail in a way such that we can still service the
182 * interrupt).
183 */
184 provider->addInterruptStatistics(reserved->statistics, inIntIndex);
185
186 /*
187 * Add the statistics object to the global list of statistics objects; this
188 * is an aid to debugging (we can trivially find statistics for all eligible
189 * interrupts, and dump them; potentially helpful if the system is wedged
190 * due to interrupt activity).
191 */
192 interruptAccountingDataAddToList(reserved->statistics);
193 }
1c79356b 194
0a7de745 195 return ret;
060df5ea
A
196}
197
fe8ab488
A
198void
199IOInterruptEventSource::unregisterInterruptHandler(IOService *inProvider,
0a7de745 200 int inIntIndex)
fe8ab488 201{
0a7de745
A
202 if (reserved->statistics) {
203 interruptAccountingDataRemoveFromList(reserved->statistics);
204 provider->removeInterruptStatistics(reserved->statistics->interruptIndex);
205 }
fe8ab488 206
0a7de745 207 provider->unregisterInterrupt(inIntIndex);
fe8ab488
A
208}
209
210
1c79356b
A
211IOInterruptEventSource *
212IOInterruptEventSource::interruptEventSource(OSObject *inOwner,
0a7de745
A
213 Action inAction,
214 IOService *inProvider,
215 int inIntIndex)
1c79356b 216{
0a7de745 217 IOInterruptEventSource *me = new IOInterruptEventSource;
1c79356b 218
0a7de745
A
219 if (me && !me->init(inOwner, inAction, inProvider, inIntIndex)) {
220 me->release();
cb323159 221 return NULL;
0a7de745 222 }
1c79356b 223
0a7de745 224 return me;
1c79356b
A
225}
226
d9a64523
A
227IOInterruptEventSource *
228IOInterruptEventSource::interruptEventSource(OSObject *inOwner,
0a7de745
A
229 IOService *inProvider,
230 int inIntIndex,
231 ActionBlock inAction)
d9a64523 232{
0a7de745
A
233 IOInterruptEventSource * ies;
234 ies = IOInterruptEventSource::interruptEventSource(inOwner, (Action) NULL, inProvider, inIntIndex);
235 if (ies) {
236 ies->setActionBlock((IOEventSource::ActionBlock) inAction);
237 }
d9a64523 238
0a7de745 239 return ies;
d9a64523
A
240}
241
0a7de745
A
242void
243IOInterruptEventSource::free()
1c79356b 244{
0a7de745
A
245 if (provider && intIndex >= 0) {
246 unregisterInterruptHandler(provider, intIndex);
247 }
fe8ab488 248
0a7de745
A
249 if (reserved) {
250 if (reserved->statistics) {
251 IODelete(reserved->statistics, IOInterruptAccountingData, 1);
252 }
fe8ab488 253
0a7de745
A
254 IODelete(reserved, ExpansionData, 1);
255 }
1c79356b 256
0a7de745 257 super::free();
1c79356b
A
258}
259
0a7de745
A
260void
261IOInterruptEventSource::enable()
1c79356b 262{
0a7de745
A
263 if (provider && intIndex >= 0) {
264 provider->enableInterrupt(intIndex);
265 explicitDisable = false;
266 enabled = true;
267 }
1c79356b
A
268}
269
0a7de745
A
270void
271IOInterruptEventSource::disable()
1c79356b 272{
0a7de745
A
273 if (provider && intIndex >= 0) {
274 provider->disableInterrupt(intIndex);
275 explicitDisable = true;
276 enabled = false;
277 }
1c79356b
A
278}
279
0a7de745
A
280void
281IOInterruptEventSource::setWorkLoop(IOWorkLoop *inWorkLoop)
060df5ea 282{
0a7de745
A
283 if (inWorkLoop) {
284 super::setWorkLoop(inWorkLoop);
285 }
99c3a104 286
0a7de745
A
287 if (provider) {
288 if (!inWorkLoop) {
289 if (intIndex >= 0) {
290 /*
291 * It isn't necessarily safe to wait until free() to unregister the interrupt;
292 * our provider may disappear.
293 */
294 unregisterInterruptHandler(provider, intIndex);
295 intIndex = ~intIndex;
296 }
297 } else if ((intIndex < 0) && (kIOReturnSuccess == registerInterruptHandler(provider, ~intIndex))) {
298 intIndex = ~intIndex;
299 }
060df5ea 300 }
99c3a104 301
0a7de745
A
302 if (!inWorkLoop) {
303 super::setWorkLoop(inWorkLoop);
304 }
060df5ea
A
305}
306
0a7de745
A
307const IOService *
308IOInterruptEventSource::getProvider() const
1c79356b 309{
0a7de745 310 return provider;
1c79356b
A
311}
312
0a7de745
A
313int
314IOInterruptEventSource::getIntIndex() const
1c79356b 315{
0a7de745 316 return intIndex;
1c79356b
A
317}
318
0a7de745
A
319bool
320IOInterruptEventSource::getAutoDisable() const
1c79356b 321{
0a7de745 322 return autoDisable;
1c79356b
A
323}
324
0a7de745
A
325bool
326IOInterruptEventSource::checkForWork()
1c79356b 327{
0a7de745
A
328 uint64_t startSystemTime = 0;
329 uint64_t endSystemTime = 0;
330 uint64_t startCPUTime = 0;
331 uint64_t endCPUTime = 0;
332 unsigned int cacheProdCount = producerCount;
333 int numInts = cacheProdCount - consumerCount;
334 IOInterruptEventAction intAction = (IOInterruptEventAction) action;
335 ActionBlock intActionBlock = (ActionBlock) actionBlock;
060df5ea 336 bool trace = (gIOKitTrace & kIOTraceIntEventSource) ? true : false;
0a7de745
A
337
338 IOStatisticsCheckForWork();
339
340 if (numInts > 0) {
341 if (trace) {
060df5ea 342 IOTimeStampStartConstant(IODBG_INTES(IOINTES_ACTION),
0a7de745
A
343 VM_KERNEL_ADDRHIDE(intAction), VM_KERNEL_ADDRHIDE(owner),
344 VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(workLoop));
345 }
fe8ab488
A
346
347 if (reserved->statistics) {
348 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingSecondLevelSystemTimeIndex)) {
349 startSystemTime = mach_absolute_time();
350 }
351
352 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingSecondLevelCPUTimeIndex)) {
353 startCPUTime = thread_get_runtime_self();
354 }
355 }
356
060df5ea 357 // Call the handler
0a7de745
A
358 if (kActionBlock & flags) {
359 (intActionBlock)(this, numInts);
360 } else {
361 (*intAction)(owner, this, numInts);
362 }
fe8ab488
A
363
364 if (reserved->statistics) {
365 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingSecondLevelCountIndex)) {
366 IA_ADD_VALUE(&reserved->statistics->interruptStatistics[kInterruptAccountingSecondLevelCountIndex], 1);
367 }
368
369 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingSecondLevelCPUTimeIndex)) {
370 endCPUTime = thread_get_runtime_self();
371 IA_ADD_VALUE(&reserved->statistics->interruptStatistics[kInterruptAccountingSecondLevelCPUTimeIndex], endCPUTime - startCPUTime);
372 }
373
374 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingSecondLevelSystemTimeIndex)) {
375 endSystemTime = mach_absolute_time();
376 IA_ADD_VALUE(&reserved->statistics->interruptStatistics[kInterruptAccountingSecondLevelSystemTimeIndex], endSystemTime - startSystemTime);
377 }
378 }
0a7de745
A
379
380 if (trace) {
060df5ea 381 IOTimeStampEndConstant(IODBG_INTES(IOINTES_ACTION),
0a7de745
A
382 VM_KERNEL_ADDRHIDE(intAction), VM_KERNEL_ADDRHIDE(owner),
383 VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(workLoop));
384 }
385
6d2010ae 386 consumerCount = cacheProdCount;
0a7de745 387 if (autoDisable && !explicitDisable) {
6d2010ae 388 enable();
0a7de745
A
389 }
390 } else if (numInts < 0) {
391 if (trace) {
060df5ea 392 IOTimeStampStartConstant(IODBG_INTES(IOINTES_ACTION),
0a7de745
A
393 VM_KERNEL_ADDRHIDE(intAction), VM_KERNEL_ADDRHIDE(owner),
394 VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(workLoop));
395 }
fe8ab488
A
396
397 if (reserved->statistics) {
398 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingSecondLevelSystemTimeIndex)) {
399 startSystemTime = mach_absolute_time();
400 }
401
402 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingSecondLevelCPUTimeIndex)) {
403 startCPUTime = thread_get_runtime_self();
404 }
405 }
0a7de745 406
060df5ea 407 // Call the handler
0a7de745
A
408 if (kActionBlock & flags) {
409 (intActionBlock)(this, numInts);
410 } else {
411 (*intAction)(owner, this, numInts);
412 }
fe8ab488
A
413
414 if (reserved->statistics) {
415 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingSecondLevelCountIndex)) {
416 IA_ADD_VALUE(&reserved->statistics->interruptStatistics[kInterruptAccountingSecondLevelCountIndex], 1);
417 }
418
419 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingSecondLevelCPUTimeIndex)) {
420 endCPUTime = thread_get_runtime_self();
421 IA_ADD_VALUE(&reserved->statistics->interruptStatistics[kInterruptAccountingSecondLevelCPUTimeIndex], endCPUTime - startCPUTime);
422 }
423
424 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingSecondLevelSystemTimeIndex)) {
425 endSystemTime = mach_absolute_time();
426 IA_ADD_VALUE(&reserved->statistics->interruptStatistics[kInterruptAccountingSecondLevelSystemTimeIndex], endSystemTime - startSystemTime);
427 }
428 }
0a7de745
A
429
430 if (trace) {
060df5ea 431 IOTimeStampEndConstant(IODBG_INTES(IOINTES_ACTION),
0a7de745
A
432 VM_KERNEL_ADDRHIDE(intAction), VM_KERNEL_ADDRHIDE(owner),
433 VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(workLoop));
434 }
435
6d2010ae 436 consumerCount = cacheProdCount;
0a7de745 437 if (autoDisable && !explicitDisable) {
6d2010ae 438 enable();
0a7de745 439 }
6d2010ae 440 }
0a7de745
A
441
442 return false;
1c79356b
A
443}
444
0a7de745
A
445void
446IOInterruptEventSource::normalInterruptOccurred
447(void */*refcon*/, IOService */*prov*/, int /*source*/)
1c79356b 448{
060df5ea 449 bool trace = (gIOKitTrace & kIOTraceIntEventSource) ? true : false;
0a7de745
A
450
451 IOStatisticsInterrupt();
452 producerCount++;
453
454 if (trace) {
455 IOTimeStampStartConstant(IODBG_INTES(IOINTES_SEMA), VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(owner));
456 }
457
458 if (reserved->statistics) {
cb323159
A
459 if (reserved->statistics->enablePrimaryTimestamp) {
460 reserved->statistics->primaryTimestamp = mach_absolute_time();
461 }
0a7de745
A
462 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingFirstLevelCountIndex)) {
463 IA_ADD_VALUE(&reserved->statistics->interruptStatistics[kInterruptAccountingFirstLevelCountIndex], 1);
464 }
465 }
466
467 signalWorkAvailable();
468
469 if (trace) {
470 IOTimeStampEndConstant(IODBG_INTES(IOINTES_SEMA), VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(owner));
471 }
1c79356b
A
472}
473
0a7de745
A
474void
475IOInterruptEventSource::disableInterruptOccurred
476(void */*refcon*/, IOService *prov, int source)
1c79356b 477{
060df5ea 478 bool trace = (gIOKitTrace & kIOTraceIntEventSource) ? true : false;
0a7de745
A
479
480 prov->disableInterrupt(source); /* disable the interrupt */
481
482 IOStatisticsInterrupt();
483 producerCount++;
484
485 if (trace) {
486 IOTimeStampStartConstant(IODBG_INTES(IOINTES_SEMA), VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(owner));
487 }
488
489 if (reserved->statistics) {
cb323159
A
490 if (reserved->statistics->enablePrimaryTimestamp) {
491 reserved->statistics->primaryTimestamp = mach_absolute_time();
492 }
0a7de745
A
493 if (IA_GET_STATISTIC_ENABLED(kInterruptAccountingFirstLevelCountIndex)) {
494 IA_ADD_VALUE(&reserved->statistics->interruptStatistics[kInterruptAccountingFirstLevelCountIndex], 1);
495 }
496 }
497
498 signalWorkAvailable();
499
500 if (trace) {
501 IOTimeStampEndConstant(IODBG_INTES(IOINTES_SEMA), VM_KERNEL_ADDRHIDE(this), VM_KERNEL_ADDRHIDE(owner));
502 }
1c79356b
A
503}
504
0a7de745
A
505void
506IOInterruptEventSource::interruptOccurred
cb323159 507(void *_refcon, IOService *prov, int source)
1c79356b 508{
0a7de745 509 if (autoDisable && prov) {
cb323159 510 disableInterruptOccurred(_refcon, prov, source);
0a7de745 511 } else {
cb323159 512 normalInterruptOccurred(_refcon, prov, source);
0a7de745 513 }
1c79356b 514}
6d2010ae 515
0a7de745
A
516IOReturn
517IOInterruptEventSource::warmCPU
518(uint64_t abstime)
6d2010ae 519{
6d2010ae
A
520 return ml_interrupt_prewarm(abstime);
521}
cb323159
A
522
523void
524IOInterruptEventSource::enablePrimaryInterruptTimestamp(bool enable)
525{
526 if (reserved->statistics) {
527 reserved->statistics->enablePrimaryTimestamp = enable;
528 }
529}
530
531uint64_t
532IOInterruptEventSource::getPimaryInterruptTimestamp()
533{
534 if (reserved->statistics && reserved->statistics->enablePrimaryTimestamp) {
535 return reserved->statistics->primaryTimestamp;
536 }
537 return -1ULL;
538}