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