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