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