1/*
2 * Copyright (c) 1998-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
30
31HISTORY
32 1998-7-13 Godfrey van der Linden(gvdl)
33 Created.
34 1998-10-30 Godfrey van der Linden(gvdl)
35 Converted to C++
36*/
37
38#ifndef _IOKIT_IOINTERRUPTEVENTSOURCE_H
39#define _IOKIT_IOINTERRUPTEVENTSOURCE_H
40
41#include <IOKit/IOEventSource.h>
42
43class IOService;
44
45struct IOInterruptAccountingData;
46
47/*! @class IOInterruptEventSource : public IOEventSource
48 @abstract Event source for interrupt delivery to work-loop based drivers.
49 @discussion The IOInterruptEventSource is a generic object that delivers calls interrupt routines in it's client in a guaranteed single-threaded manner. IOInterruptEventSource is part of the IOKit $link IOWorkLoop infrastructure where the semantic that one and only one action method is executing within a work-loops event chain.
50<br><br>
51When the action method is called in the client member function will receive 2 arguments, (IOEventSource *) sender and (int) count, See $link IOInterruptEventSource::Action. Where sender will be reference to the interrupt that occurred and the count will be computed by the difference between the $link producerCount and $link consumerCount. This number may not be reliable as no attempt is made to adjust for around the world type problems but is provided for general information and statistic gathering.
52<br><br>
53In general a client will use the factory member function to create and initialise the event source and then add it to their work-loop. It is the work loop's responsiblity to maintain the new event source in it's event chain. See $link IOWorkLoop.
54<br><br>
55An interrupt event source attaches itself to the given provider's interrupt source at initialisation time. At this time it determines if it is connected to a level or edge triggered interrupt. If the interrupt is an level triggered interrupt the event source automatically disables the interrupt source at primary interrupt time and after it call's the client it automatically reenables the interrupt. This action is fairly expensive but it is 100% safe and defaults sensibly so that the driver writer does not have to implement type dependant interrupt routines. So to repeat, the driver writer does not have to be concerned by the actual underlying interrupt mechanism as the event source hides the complexity.
56<br><br>
57Saying this if the hardware is a multi-device card, for instance a 4 port NIC, where all of the devices are sharing one level triggered interrupt AND it is possible to determine each port's interrupt state non-destructively then the $link IOFilterInterruptEventSource would be a better choice.
58<br><br>
59Warning: All IOInterruptEventSources are created in the disabled state. If you want to actually schedule interrupt delivery do not forget to enable the source.
60*/
61class IOInterruptEventSource : public IOEventSource
62{
63 OSDeclareDefaultStructors(IOInterruptEventSource)
64
65public:
66/*! @typedef Action
67 @discussion 'C' pointer prototype of functions that are called in a single threaded context when an interrupt occurs.
68 @param owner Pointer to client instance.
69 @param sender Pointer to generation interrupt event source.
70 @param count Number of interrupts seen before delivery. */
71 typedef void (*Action)(OSObject *owner, IOInterruptEventSource *sender, int count);
72
73#ifdef __BLOCKS__
74 typedef void (^ActionBlock)(IOInterruptEventSource *sender, int count);
75#endif /* __BLOCKS__ */
76
77/*! @defined IOInterruptEventAction
78 @discussion Backward compatibilty define for the old non-class scoped type definition. See $link IOInterruptEventSource::Action */
79#define IOInterruptEventAction IOInterruptEventSource::Action
80
81protected:
82/*! @var provider IOService that provides interrupts for delivery. */
83 IOService *provider;
84
85/*! @var intIndex */
86 int intIndex;
87
88/*! @var producerCount
89 Current count of produced interrupts that have been received. */
90 volatile unsigned int producerCount;
91
92/*! @var consumerCount
93 Current count of produced interrupts that the owner has been informed of. */
94 unsigned int consumerCount;
95
96/*! @var autoDisable Do we need to automatically disable the interrupt source when we take an interrupt, i.e. we are level triggered. */
97 bool autoDisable;
98
99/*! @var explicitDisable Has the user expicitly disabled this event source, if so then do not overide their request when returning from the callout */
100 bool explicitDisable;
101
102/*! @struct ExpansionData
103 @discussion This structure will be used to expand the capablilties of the IOWorkLoop in the future.
104 */
105 struct ExpansionData {
106 IOInterruptAccountingData * statistics;
107 };
108
109/*! @var reserved
110 Reserved for future use. (Internal use only) */
111 APPLE_KEXT_WSHADOW_PUSH;
112 ExpansionData *reserved;
113 APPLE_KEXT_WSHADOW_POP;
114
115/*! @function free
116 @abstract Sub-class implementation of free method, disconnects from the interrupt source. */
117 virtual void free() APPLE_KEXT_OVERRIDE;
118
119/*! @function checkForWork
120 @abstract Pure Virtual member function used by IOWorkLoop for issueing a client calls.
121 @discussion This function called when the work-loop is ready to check for any work to do and then to call out the owner/action.
122 @result Return true if this function needs to be called again before all its outstanding events have been processed. */
123 virtual bool checkForWork() APPLE_KEXT_OVERRIDE;
124
125/*! @function setWorkLoop
126 @abstract Sub-class implementation of setWorkLoop method. */
127 virtual void setWorkLoop(IOWorkLoop *inWorkLoop) APPLE_KEXT_OVERRIDE;
128
129public:
130
131/*! @function interruptEventSource
132 @abstract Factory function for IOInterruptEventSources creation and initialisation.
133 @param owner Owning client of the new event source.
134 @param action 'C' Function to call when something happens.
135 @param provider IOService that represents the interrupt source. Defaults to 0. When no provider is defined the event source assumes that the client will in some manner call the interruptOccured method explicitly. This will start the ball rolling for safe delivery of asynchronous event's into the driver.
136 @param intIndex The index of the interrupt within the provider's interrupt sources. Defaults to 0, i.e. the first interrupt in the provider.
137 @result A new interrupt event source if successfully created and initialised, 0 otherwise. */
138 static IOInterruptEventSource *
139 interruptEventSource(OSObject *owner,
140 Action action,
141 IOService *provider = 0,
142 int intIndex = 0);
143
144
145#ifdef __BLOCKS__
146/*! @function interruptEventSource
147 @abstract Factory function for IOInterruptEventSources creation and initialisation.
148 @param owner Owning client of the new event source.
149 @param provider IOService that represents the interrupt source. When no provider is defined the event source assumes that the client will in some manner call the interruptOccured method explicitly. This will start the ball rolling for safe delivery of asynchronous event's into the driver.
150 @param intIndex The index of the interrupt within the provider's interrupt sources.
151 @param action Block for the callout routine of this event source..
152 @result A new interrupt event source if successfully created and initialised, 0 otherwise. */
153 static IOInterruptEventSource *
154 interruptEventSource(OSObject *owner,
155 IOService *provider,
156 int intIndex,
157 ActionBlock action);
158#endif /* __BLOCKS__ */
159
160#if XNU_KERNEL_PRIVATE
161 static void actionToBlock(OSObject *owner, IOInterruptEventSource *sender, int count);
162#endif /* XNU_KERNEL_PRIVATE */
163
164/*! @function init
165 @abstract Primary initialiser for the IOInterruptEventSource class.
166 @param owner Owning client of the new event source.
167 @param action 'C' Function to call when something happens.
168 @param provider IOService that represents the interrupt source. Defaults to 0. When no provider is defined the event source assumes that the client will in some manner call the interruptOccured method explicitly. This will start the ball rolling for safe delivery of asynchronous event's into the driver.
169 @param intIndex The index of the interrupt within the provider's interrupt sources. Defaults to 0, i.e. the first interrupt in the provider.
170 @result true if the inherited classes and this instance initialise
171successfully. */
172 virtual bool init(OSObject *owner,
173 Action action,
174 IOService *provider = 0,
175 int intIndex = 0);
176
177/*! @function enable
178 @abstract Enable event source.
179 @discussion A subclass implementation is expected to respect the enabled
180state when checkForWork is called. Calling this function will cause the
181work-loop to be signalled so that a checkForWork is performed. */
182 virtual void enable() APPLE_KEXT_OVERRIDE;
183
184/*! @function disable
185 @abstract Disable event source.
186 @discussion A subclass implementation is expected to respect the enabled
187state when checkForWork is called. */
188 virtual void disable() APPLE_KEXT_OVERRIDE;
189
190/*! @function getProvider
191 @abstract Get'ter for $link provider variable.
192 @result value of provider. */
193 virtual const IOService *getProvider() const;
194
195/*! @function getIntIndex
196 @abstract Get'ter for $link intIndex interrupt index variable.
197 @result value of intIndex. */
198 virtual int getIntIndex() const;
199
200/*! @function getAutoDisable
201 @abstract Get'ter for $link autoDisable variable.
202 @result value of autoDisable. */
203 virtual bool getAutoDisable() const;
204
205/*! @function interruptOccurred
206 @abstract Functions that get called by the interrupt controller. See $link IOService::registerInterrupt
207 @param nub Where did the interrupt originate from
208 @param ind What is this interrupts index within 'nub'. */
209 virtual void interruptOccurred(void *, IOService *nub, int ind);
210
211/*! @function normalInterruptOccurred
212 @abstract Functions that get called by the interrupt controller.See $link IOService::registerInterrupt
213 @param nub Where did the interrupt originate from
214 @param ind What is this interrupts index within 'nub'. */
215 virtual void normalInterruptOccurred(void *, IOService *nub, int ind);
216
217/*! @function disableInterruptOccurred
218 @abstract Functions that get called by the interrupt controller.See $link IOService::registerInterrupt
219 @param nub Where did the interrupt originate from
220 @param ind What is this interrupts index within 'nub'. */
221 virtual void disableInterruptOccurred(void *, IOService *nub, int ind);
222
223/*! @function warmCPU
224 @abstract Tries to reduce latency for an interrupt which will be received near a specified time.
225 @discussion Warms up a CPU in advance of an interrupt so that the interrupt may be serviced with predictable latency.
226 The warm-up is not periodic; callers should call warmCPU once in advance of each interrupt. It is recommended that
227 requests be issues in serial (i.e. each after the target for the previous call has elapsed), as there is a systemwide
228 cap on the number of outstanding requests. This routine may be disruptive to the system if used with very small intervals
229 between requests; it should be used only in cases where interrupt latency is absolutely critical, and tens or hundreds of
230 milliseconds between targets is the expected time scale. NOTE: it is not safe to call this method with interrupts disabled.
231 @param abstime Time at which interrupt is expected. */
232 IOReturn warmCPU(uint64_t abstime);
233
234private:
235 IOReturn registerInterruptHandler(IOService *inProvider, int inIntIndex);
236 void unregisterInterruptHandler(IOService *inProvider, int inIntIndex);
237
238private:
239 OSMetaClassDeclareReservedUnused(IOInterruptEventSource, 0);
240 OSMetaClassDeclareReservedUnused(IOInterruptEventSource, 1);
241 OSMetaClassDeclareReservedUnused(IOInterruptEventSource, 2);
242 OSMetaClassDeclareReservedUnused(IOInterruptEventSource, 3);
243 OSMetaClassDeclareReservedUnused(IOInterruptEventSource, 4);
244 OSMetaClassDeclareReservedUnused(IOInterruptEventSource, 5);
245 OSMetaClassDeclareReservedUnused(IOInterruptEventSource, 6);
246 OSMetaClassDeclareReservedUnused(IOInterruptEventSource, 7);
247};
248
249#endif /* !_IOKIT_IOINTERRUPTEVENTSOURCE_H */
250