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