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