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