xref: /xnu-8796.121.2/iokit/Kernel/IODMAEventSource.cpp (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  *
4*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions  *
6*c54f35caSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions  *
15*c54f35caSApple OSS Distributions  * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions  *
18*c54f35caSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions  * limitations under the License.
25*c54f35caSApple OSS Distributions  *
26*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions  */
28*c54f35caSApple OSS Distributions 
29*c54f35caSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
30*c54f35caSApple OSS Distributions 
31*c54f35caSApple OSS Distributions #include <IOKit/IODMAEventSource.h>
32*c54f35caSApple OSS Distributions #include <IOKit/IOService.h>
33*c54f35caSApple OSS Distributions 
34*c54f35caSApple OSS Distributions #include "IOKitKernelInternal.h"
35*c54f35caSApple OSS Distributions 
36*c54f35caSApple OSS Distributions 
37*c54f35caSApple OSS Distributions #define super IOEventSource
38*c54f35caSApple OSS Distributions OSDefineMetaClassAndStructors(IODMAEventSource, IOEventSource);
39*c54f35caSApple OSS Distributions 
40*c54f35caSApple OSS Distributions bool
init(OSObject * inOwner,IOService * inProvider,Action inCompletion,Action inNotification,UInt32 inDMAIndex)41*c54f35caSApple OSS Distributions IODMAEventSource::init(OSObject *inOwner,
42*c54f35caSApple OSS Distributions     IOService *inProvider,
43*c54f35caSApple OSS Distributions     Action inCompletion,
44*c54f35caSApple OSS Distributions     Action inNotification,
45*c54f35caSApple OSS Distributions     UInt32 inDMAIndex)
46*c54f35caSApple OSS Distributions {
47*c54f35caSApple OSS Distributions 	IOReturn result;
48*c54f35caSApple OSS Distributions 
49*c54f35caSApple OSS Distributions 	if (!super::init(inOwner)) {
50*c54f35caSApple OSS Distributions 		return false;
51*c54f35caSApple OSS Distributions 	}
52*c54f35caSApple OSS Distributions 
53*c54f35caSApple OSS Distributions 	if (inProvider == NULL) {
54*c54f35caSApple OSS Distributions 		return false;
55*c54f35caSApple OSS Distributions 	}
56*c54f35caSApple OSS Distributions 
57*c54f35caSApple OSS Distributions 	dmaProvider = inProvider;
58*c54f35caSApple OSS Distributions 	dmaIndex = 0xFFFFFFFF;
59*c54f35caSApple OSS Distributions 	dmaCompletionAction = inCompletion;
60*c54f35caSApple OSS Distributions 	dmaNotificationAction = inNotification;
61*c54f35caSApple OSS Distributions 
62*c54f35caSApple OSS Distributions 	dmaController.reset(IODMAController::getController(dmaProvider, inDMAIndex), OSRetain);
63*c54f35caSApple OSS Distributions 	if (dmaController == NULL) {
64*c54f35caSApple OSS Distributions 		return false;
65*c54f35caSApple OSS Distributions 	}
66*c54f35caSApple OSS Distributions 
67*c54f35caSApple OSS Distributions 	result = dmaController->initDMAChannel(dmaProvider, this, &dmaIndex, inDMAIndex);
68*c54f35caSApple OSS Distributions 	if (result != kIOReturnSuccess) {
69*c54f35caSApple OSS Distributions 		return false;
70*c54f35caSApple OSS Distributions 	}
71*c54f35caSApple OSS Distributions 
72*c54f35caSApple OSS Distributions 	queue_init(&dmaCommandsCompleted);
73*c54f35caSApple OSS Distributions 	dmaCommandsCompletedLock = IOSimpleLockAlloc();
74*c54f35caSApple OSS Distributions 
75*c54f35caSApple OSS Distributions 	return true;
76*c54f35caSApple OSS Distributions }
77*c54f35caSApple OSS Distributions 
78*c54f35caSApple OSS Distributions void
free()79*c54f35caSApple OSS Distributions IODMAEventSource::free()
80*c54f35caSApple OSS Distributions {
81*c54f35caSApple OSS Distributions 	if (dmaCommandsCompletedLock != NULL) {
82*c54f35caSApple OSS Distributions 		IOSimpleLockFree(dmaCommandsCompletedLock);
83*c54f35caSApple OSS Distributions 	}
84*c54f35caSApple OSS Distributions 	super::free();
85*c54f35caSApple OSS Distributions }
86*c54f35caSApple OSS Distributions 
87*c54f35caSApple OSS Distributions OSSharedPtr<IODMAEventSource>
dmaEventSource(OSObject * inOwner,IOService * inProvider,Action inCompletion,Action inNotification,UInt32 inDMAIndex)88*c54f35caSApple OSS Distributions IODMAEventSource::dmaEventSource(OSObject *inOwner,
89*c54f35caSApple OSS Distributions     IOService *inProvider,
90*c54f35caSApple OSS Distributions     Action inCompletion,
91*c54f35caSApple OSS Distributions     Action inNotification,
92*c54f35caSApple OSS Distributions     UInt32 inDMAIndex)
93*c54f35caSApple OSS Distributions {
94*c54f35caSApple OSS Distributions 	OSSharedPtr<IODMAEventSource> dmaES = OSMakeShared<IODMAEventSource>();
95*c54f35caSApple OSS Distributions 
96*c54f35caSApple OSS Distributions 	if (dmaES && !dmaES->init(inOwner, inProvider, inCompletion, inNotification, inDMAIndex)) {
97*c54f35caSApple OSS Distributions 		return nullptr;
98*c54f35caSApple OSS Distributions 	}
99*c54f35caSApple OSS Distributions 
100*c54f35caSApple OSS Distributions 	return dmaES;
101*c54f35caSApple OSS Distributions }
102*c54f35caSApple OSS Distributions 
103*c54f35caSApple OSS Distributions IOReturn
startDMACommand(IODMACommand * dmaCommand,IODirection direction,IOByteCount byteCount,IOByteCount byteOffset)104*c54f35caSApple OSS Distributions IODMAEventSource::startDMACommand(IODMACommand *dmaCommand, IODirection direction, IOByteCount byteCount, IOByteCount byteOffset)
105*c54f35caSApple OSS Distributions {
106*c54f35caSApple OSS Distributions 	IOReturn result;
107*c54f35caSApple OSS Distributions 
108*c54f35caSApple OSS Distributions 	if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
109*c54f35caSApple OSS Distributions 		return kIOReturnError;
110*c54f35caSApple OSS Distributions 	}
111*c54f35caSApple OSS Distributions 
112*c54f35caSApple OSS Distributions 	if (dmaSynchBusy) {
113*c54f35caSApple OSS Distributions 		return kIOReturnBusy;
114*c54f35caSApple OSS Distributions 	}
115*c54f35caSApple OSS Distributions 
116*c54f35caSApple OSS Distributions 	if (dmaCompletionAction == NULL) {
117*c54f35caSApple OSS Distributions 		dmaSynchBusy = true;
118*c54f35caSApple OSS Distributions 	}
119*c54f35caSApple OSS Distributions 
120*c54f35caSApple OSS Distributions 	result = dmaController->startDMACommand(dmaIndex, dmaCommand, direction, byteCount, byteOffset);
121*c54f35caSApple OSS Distributions 
122*c54f35caSApple OSS Distributions 	if (result != kIOReturnSuccess) {
123*c54f35caSApple OSS Distributions 		dmaSynchBusy = false;
124*c54f35caSApple OSS Distributions 		return result;
125*c54f35caSApple OSS Distributions 	}
126*c54f35caSApple OSS Distributions 
127*c54f35caSApple OSS Distributions 	while (dmaSynchBusy) {
128*c54f35caSApple OSS Distributions 		sleepGate(&dmaSynchBusy, THREAD_UNINT);
129*c54f35caSApple OSS Distributions 	}
130*c54f35caSApple OSS Distributions 
131*c54f35caSApple OSS Distributions 	return kIOReturnSuccess;
132*c54f35caSApple OSS Distributions }
133*c54f35caSApple OSS Distributions 
134*c54f35caSApple OSS Distributions IOReturn
stopDMACommand(bool flush,uint64_t timeout)135*c54f35caSApple OSS Distributions IODMAEventSource::stopDMACommand(bool flush, uint64_t timeout)
136*c54f35caSApple OSS Distributions {
137*c54f35caSApple OSS Distributions 	if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
138*c54f35caSApple OSS Distributions 		return kIOReturnError;
139*c54f35caSApple OSS Distributions 	}
140*c54f35caSApple OSS Distributions 
141*c54f35caSApple OSS Distributions 	return dmaController->stopDMACommand(dmaIndex, flush, timeout);
142*c54f35caSApple OSS Distributions }
143*c54f35caSApple OSS Distributions 
144*c54f35caSApple OSS Distributions 
145*c54f35caSApple OSS Distributions IOReturn
queryDMACommand(IODMACommand ** dmaCommand,IOByteCount * transferCount,bool waitForIdle)146*c54f35caSApple OSS Distributions IODMAEventSource::queryDMACommand(IODMACommand **dmaCommand, IOByteCount *transferCount, bool waitForIdle)
147*c54f35caSApple OSS Distributions {
148*c54f35caSApple OSS Distributions 	if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
149*c54f35caSApple OSS Distributions 		return kIOReturnError;
150*c54f35caSApple OSS Distributions 	}
151*c54f35caSApple OSS Distributions 
152*c54f35caSApple OSS Distributions 	return dmaController->queryDMACommand(dmaIndex, dmaCommand, transferCount, waitForIdle);
153*c54f35caSApple OSS Distributions }
154*c54f35caSApple OSS Distributions 
155*c54f35caSApple OSS Distributions 
156*c54f35caSApple OSS Distributions IOByteCount
getFIFODepth(IODirection direction)157*c54f35caSApple OSS Distributions IODMAEventSource::getFIFODepth(IODirection direction)
158*c54f35caSApple OSS Distributions {
159*c54f35caSApple OSS Distributions 	if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
160*c54f35caSApple OSS Distributions 		return 0;
161*c54f35caSApple OSS Distributions 	}
162*c54f35caSApple OSS Distributions 
163*c54f35caSApple OSS Distributions 	return dmaController->getFIFODepth(dmaIndex, direction);
164*c54f35caSApple OSS Distributions }
165*c54f35caSApple OSS Distributions 
166*c54f35caSApple OSS Distributions 
167*c54f35caSApple OSS Distributions IOReturn
setFIFODepth(IOByteCount depth)168*c54f35caSApple OSS Distributions IODMAEventSource::setFIFODepth(IOByteCount depth)
169*c54f35caSApple OSS Distributions {
170*c54f35caSApple OSS Distributions 	if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
171*c54f35caSApple OSS Distributions 		return kIOReturnError;
172*c54f35caSApple OSS Distributions 	}
173*c54f35caSApple OSS Distributions 
174*c54f35caSApple OSS Distributions 	return dmaController->setFIFODepth(dmaIndex, depth);
175*c54f35caSApple OSS Distributions }
176*c54f35caSApple OSS Distributions 
177*c54f35caSApple OSS Distributions 
178*c54f35caSApple OSS Distributions IOByteCount
validFIFODepth(IOByteCount depth,IODirection direction)179*c54f35caSApple OSS Distributions IODMAEventSource::validFIFODepth(IOByteCount depth, IODirection direction)
180*c54f35caSApple OSS Distributions {
181*c54f35caSApple OSS Distributions 	if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
182*c54f35caSApple OSS Distributions 		return kIOReturnError;
183*c54f35caSApple OSS Distributions 	}
184*c54f35caSApple OSS Distributions 
185*c54f35caSApple OSS Distributions 	return dmaController->validFIFODepth(dmaIndex, depth, direction);
186*c54f35caSApple OSS Distributions }
187*c54f35caSApple OSS Distributions 
188*c54f35caSApple OSS Distributions 
189*c54f35caSApple OSS Distributions IOReturn
setFrameSize(UInt8 byteCount)190*c54f35caSApple OSS Distributions IODMAEventSource::setFrameSize(UInt8 byteCount)
191*c54f35caSApple OSS Distributions {
192*c54f35caSApple OSS Distributions 	if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
193*c54f35caSApple OSS Distributions 		return kIOReturnError;
194*c54f35caSApple OSS Distributions 	}
195*c54f35caSApple OSS Distributions 
196*c54f35caSApple OSS Distributions 	return dmaController->setFrameSize(dmaIndex, byteCount);
197*c54f35caSApple OSS Distributions }
198*c54f35caSApple OSS Distributions 
199*c54f35caSApple OSS Distributions // protected
200*c54f35caSApple OSS Distributions 
201*c54f35caSApple OSS Distributions bool
checkForWork(void)202*c54f35caSApple OSS Distributions IODMAEventSource::checkForWork(void)
203*c54f35caSApple OSS Distributions {
204*c54f35caSApple OSS Distributions 	IODMACommand     *dmaCommand = NULL;
205*c54f35caSApple OSS Distributions 	bool work, again;
206*c54f35caSApple OSS Distributions 
207*c54f35caSApple OSS Distributions 	IOSimpleLockLock(dmaCommandsCompletedLock);
208*c54f35caSApple OSS Distributions 	work = !queue_empty(&dmaCommandsCompleted);
209*c54f35caSApple OSS Distributions 	if (work) {
210*c54f35caSApple OSS Distributions 		queue_remove_first(&dmaCommandsCompleted, dmaCommand, IODMACommand *, fCommandChain);
211*c54f35caSApple OSS Distributions 		again = !queue_empty(&dmaCommandsCompleted);
212*c54f35caSApple OSS Distributions 	} else {
213*c54f35caSApple OSS Distributions 		again = false;
214*c54f35caSApple OSS Distributions 	}
215*c54f35caSApple OSS Distributions 	IOSimpleLockUnlock(dmaCommandsCompletedLock);
216*c54f35caSApple OSS Distributions 
217*c54f35caSApple OSS Distributions 	if (work) {
218*c54f35caSApple OSS Distributions 		(*dmaCompletionAction)(owner, this, dmaCommand, dmaCommand->reserved->fStatus, dmaCommand->reserved->fActualByteCount, dmaCommand->reserved->fTimeStamp);
219*c54f35caSApple OSS Distributions 	}
220*c54f35caSApple OSS Distributions 
221*c54f35caSApple OSS Distributions 	return again;
222*c54f35caSApple OSS Distributions }
223*c54f35caSApple OSS Distributions 
224*c54f35caSApple OSS Distributions void
completeDMACommand(IODMACommand * dmaCommand)225*c54f35caSApple OSS Distributions IODMAEventSource::completeDMACommand(IODMACommand *dmaCommand)
226*c54f35caSApple OSS Distributions {
227*c54f35caSApple OSS Distributions 	if (dmaCompletionAction != NULL) {
228*c54f35caSApple OSS Distributions 		IOSimpleLockLock(dmaCommandsCompletedLock);
229*c54f35caSApple OSS Distributions 		queue_enter(&dmaCommandsCompleted, dmaCommand, IODMACommand *, fCommandChain);
230*c54f35caSApple OSS Distributions 		IOSimpleLockUnlock(dmaCommandsCompletedLock);
231*c54f35caSApple OSS Distributions 
232*c54f35caSApple OSS Distributions 		signalWorkAvailable();
233*c54f35caSApple OSS Distributions 	} else {
234*c54f35caSApple OSS Distributions 		dmaSynchBusy = false;
235*c54f35caSApple OSS Distributions 		wakeupGate(&dmaSynchBusy, true);
236*c54f35caSApple OSS Distributions 	}
237*c54f35caSApple OSS Distributions }
238*c54f35caSApple OSS Distributions 
239*c54f35caSApple OSS Distributions void
notifyDMACommand(IODMACommand * dmaCommand,IOReturn status,IOByteCount actualByteCount,AbsoluteTime timeStamp)240*c54f35caSApple OSS Distributions IODMAEventSource::notifyDMACommand(IODMACommand *dmaCommand, IOReturn status, IOByteCount actualByteCount, AbsoluteTime timeStamp)
241*c54f35caSApple OSS Distributions {
242*c54f35caSApple OSS Distributions 	dmaCommand->reserved->fStatus = status;
243*c54f35caSApple OSS Distributions 	dmaCommand->reserved->fActualByteCount = actualByteCount;
244*c54f35caSApple OSS Distributions 	dmaCommand->reserved->fTimeStamp = timeStamp;
245*c54f35caSApple OSS Distributions 
246*c54f35caSApple OSS Distributions 	if (dmaNotificationAction != NULL) {
247*c54f35caSApple OSS Distributions 		(*dmaNotificationAction)(owner, this, dmaCommand, status, actualByteCount, timeStamp);
248*c54f35caSApple OSS Distributions 	}
249*c54f35caSApple OSS Distributions }
250*c54f35caSApple OSS Distributions 
251*c54f35caSApple OSS Distributions IOReturn
setDMAConfig(UInt32 newReqIndex)252*c54f35caSApple OSS Distributions IODMAEventSource::setDMAConfig(UInt32 newReqIndex)
253*c54f35caSApple OSS Distributions {
254*c54f35caSApple OSS Distributions 	return dmaController->setDMAConfig(dmaIndex, dmaProvider, newReqIndex);
255*c54f35caSApple OSS Distributions }
256*c54f35caSApple OSS Distributions 
257*c54f35caSApple OSS Distributions bool
validDMAConfig(UInt32 newReqIndex)258*c54f35caSApple OSS Distributions IODMAEventSource::validDMAConfig(UInt32 newReqIndex)
259*c54f35caSApple OSS Distributions {
260*c54f35caSApple OSS Distributions 	return dmaController->validDMAConfig(dmaIndex, dmaProvider, newReqIndex);
261*c54f35caSApple OSS Distributions }
262