xref: /xnu-12377.81.4/iokit/Kernel/IOInterleavedMemoryDescriptor.cpp (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions  * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3*043036a2SApple OSS Distributions  *
4*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions  *
6*043036a2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions  *
15*043036a2SApple OSS Distributions  * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions  *
18*043036a2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions  * limitations under the License.
25*043036a2SApple OSS Distributions  *
26*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions  */
28*043036a2SApple OSS Distributions 
29*043036a2SApple OSS Distributions #include <IOKit/IOLib.h>
30*043036a2SApple OSS Distributions #include <IOKit/IOInterleavedMemoryDescriptor.h>
31*043036a2SApple OSS Distributions 
32*043036a2SApple OSS Distributions #define super IOMemoryDescriptor
OSDefineMetaClassAndStructors(IOInterleavedMemoryDescriptor,IOMemoryDescriptor)33*043036a2SApple OSS Distributions OSDefineMetaClassAndStructors(IOInterleavedMemoryDescriptor, IOMemoryDescriptor)
34*043036a2SApple OSS Distributions 
35*043036a2SApple OSS Distributions IOInterleavedMemoryDescriptor * IOInterleavedMemoryDescriptor::withCapacity(
36*043036a2SApple OSS Distributions 	IOByteCount           capacity,
37*043036a2SApple OSS Distributions 	IODirection           direction )
38*043036a2SApple OSS Distributions {
39*043036a2SApple OSS Distributions 	//
40*043036a2SApple OSS Distributions 	// Create a new IOInterleavedMemoryDescriptor.  The "buffer" will be made up
41*043036a2SApple OSS Distributions 	// of several memory descriptors, that are to be chained end-to-end to make up
42*043036a2SApple OSS Distributions 	// a single memory descriptor.
43*043036a2SApple OSS Distributions 	//
44*043036a2SApple OSS Distributions 
45*043036a2SApple OSS Distributions 	IOInterleavedMemoryDescriptor * me = new IOInterleavedMemoryDescriptor;
46*043036a2SApple OSS Distributions 
47*043036a2SApple OSS Distributions 	if (me && !me->initWithCapacity(
48*043036a2SApple OSS Distributions 		    /* capacity  */ capacity,
49*043036a2SApple OSS Distributions 		    /* direction */ direction )) {
50*043036a2SApple OSS Distributions 		me->release();
51*043036a2SApple OSS Distributions 		me = NULL;
52*043036a2SApple OSS Distributions 	}
53*043036a2SApple OSS Distributions 
54*043036a2SApple OSS Distributions 	return me;
55*043036a2SApple OSS Distributions }
56*043036a2SApple OSS Distributions 
57*043036a2SApple OSS Distributions bool
initWithCapacity(IOByteCount capacity,IODirection direction)58*043036a2SApple OSS Distributions IOInterleavedMemoryDescriptor::initWithCapacity(
59*043036a2SApple OSS Distributions 	IOByteCount           capacity,
60*043036a2SApple OSS Distributions 	IODirection           direction )
61*043036a2SApple OSS Distributions {
62*043036a2SApple OSS Distributions 	//
63*043036a2SApple OSS Distributions 	// Initialize an IOInterleavedMemoryDescriptor. The "buffer" will be made up
64*043036a2SApple OSS Distributions 	// of several memory descriptors, that are to be chained end-to-end to make up
65*043036a2SApple OSS Distributions 	// a single memory descriptor.
66*043036a2SApple OSS Distributions 	//
67*043036a2SApple OSS Distributions 
68*043036a2SApple OSS Distributions 	assert(capacity);
69*043036a2SApple OSS Distributions 
70*043036a2SApple OSS Distributions 	// Ask our superclass' opinion.
71*043036a2SApple OSS Distributions 	if (super::init() == false) {
72*043036a2SApple OSS Distributions 		return false;
73*043036a2SApple OSS Distributions 	}
74*043036a2SApple OSS Distributions 
75*043036a2SApple OSS Distributions 	// Initialize our minimal state.
76*043036a2SApple OSS Distributions 
77*043036a2SApple OSS Distributions 	_flags                  = direction;
78*043036a2SApple OSS Distributions #ifndef __LP64__
79*043036a2SApple OSS Distributions 	_direction              = (IODirection) (_flags & kIOMemoryDirectionMask);
80*043036a2SApple OSS Distributions #endif /* !__LP64__ */
81*043036a2SApple OSS Distributions 	_length                 = 0;
82*043036a2SApple OSS Distributions 	_mappings               = NULL;
83*043036a2SApple OSS Distributions 	_tag                    = 0;
84*043036a2SApple OSS Distributions 	_descriptorCount        = 0;
85*043036a2SApple OSS Distributions 	_descriptors            = IONew(IOMemoryDescriptor *, capacity);
86*043036a2SApple OSS Distributions 	_descriptorOffsets      = IONewData(IOByteCount, capacity);
87*043036a2SApple OSS Distributions 	_descriptorLengths      = IONewData(IOByteCount, capacity);
88*043036a2SApple OSS Distributions 
89*043036a2SApple OSS Distributions 	if ((_descriptors == NULL) || (_descriptorOffsets == NULL) || (_descriptorLengths == NULL)) {
90*043036a2SApple OSS Distributions 		return false;
91*043036a2SApple OSS Distributions 	}
92*043036a2SApple OSS Distributions 
93*043036a2SApple OSS Distributions 	_descriptorCapacity     = capacity;
94*043036a2SApple OSS Distributions 
95*043036a2SApple OSS Distributions 	return true;
96*043036a2SApple OSS Distributions }
97*043036a2SApple OSS Distributions 
98*043036a2SApple OSS Distributions void
clearMemoryDescriptors(IODirection direction)99*043036a2SApple OSS Distributions IOInterleavedMemoryDescriptor::clearMemoryDescriptors( IODirection direction )
100*043036a2SApple OSS Distributions {
101*043036a2SApple OSS Distributions 	UInt32 index;
102*043036a2SApple OSS Distributions 
103*043036a2SApple OSS Distributions 	for (index = 0; index < _descriptorCount; index++) {
104*043036a2SApple OSS Distributions 		if (_descriptorPrepared) {
105*043036a2SApple OSS Distributions 			_descriptors[index]->complete(getDirection());
106*043036a2SApple OSS Distributions 		}
107*043036a2SApple OSS Distributions 
108*043036a2SApple OSS Distributions 		_descriptors[index]->release();
109*043036a2SApple OSS Distributions 		_descriptors[index] = NULL;
110*043036a2SApple OSS Distributions 
111*043036a2SApple OSS Distributions 		_descriptorOffsets[index] = 0;
112*043036a2SApple OSS Distributions 		_descriptorLengths[index] = 0;
113*043036a2SApple OSS Distributions 	}
114*043036a2SApple OSS Distributions 
115*043036a2SApple OSS Distributions 	if (direction != kIODirectionNone) {
116*043036a2SApple OSS Distributions 		_flags = (_flags & ~kIOMemoryDirectionMask) | direction;
117*043036a2SApple OSS Distributions #ifndef __LP64__
118*043036a2SApple OSS Distributions 		_direction = (IODirection) (_flags & kIOMemoryDirectionMask);
119*043036a2SApple OSS Distributions #endif /* !__LP64__ */
120*043036a2SApple OSS Distributions 	}
121*043036a2SApple OSS Distributions 
122*043036a2SApple OSS Distributions 	_descriptorCount = 0;
123*043036a2SApple OSS Distributions 	_length = 0;
124*043036a2SApple OSS Distributions 	_mappings = NULL;
125*043036a2SApple OSS Distributions 	_tag = 0;
126*043036a2SApple OSS Distributions };
127*043036a2SApple OSS Distributions 
128*043036a2SApple OSS Distributions bool
setMemoryDescriptor(IOMemoryDescriptor * descriptor,IOByteCount offset,IOByteCount length)129*043036a2SApple OSS Distributions IOInterleavedMemoryDescriptor::setMemoryDescriptor(
130*043036a2SApple OSS Distributions 	IOMemoryDescriptor * descriptor,
131*043036a2SApple OSS Distributions 	IOByteCount offset,
132*043036a2SApple OSS Distributions 	IOByteCount length )
133*043036a2SApple OSS Distributions {
134*043036a2SApple OSS Distributions 	if (_descriptorPrepared || (_descriptorCount == _descriptorCapacity)) {
135*043036a2SApple OSS Distributions 		return false;
136*043036a2SApple OSS Distributions 	}
137*043036a2SApple OSS Distributions 
138*043036a2SApple OSS Distributions 	if ((offset + length) > descriptor->getLength()) {
139*043036a2SApple OSS Distributions 		return false;
140*043036a2SApple OSS Distributions 	}
141*043036a2SApple OSS Distributions 
142*043036a2SApple OSS Distributions //    if ( descriptor->getDirection() != getDirection() )
143*043036a2SApple OSS Distributions //        return false;
144*043036a2SApple OSS Distributions 
145*043036a2SApple OSS Distributions 	descriptor->retain();
146*043036a2SApple OSS Distributions 	_descriptors[_descriptorCount] = descriptor;
147*043036a2SApple OSS Distributions 	_descriptorOffsets[_descriptorCount] = offset;
148*043036a2SApple OSS Distributions 	_descriptorLengths[_descriptorCount] = length;
149*043036a2SApple OSS Distributions 
150*043036a2SApple OSS Distributions 	_descriptorCount++;
151*043036a2SApple OSS Distributions 
152*043036a2SApple OSS Distributions 	_length += length;
153*043036a2SApple OSS Distributions 
154*043036a2SApple OSS Distributions 	return true;
155*043036a2SApple OSS Distributions }
156*043036a2SApple OSS Distributions 
157*043036a2SApple OSS Distributions void
free()158*043036a2SApple OSS Distributions IOInterleavedMemoryDescriptor::free()
159*043036a2SApple OSS Distributions {
160*043036a2SApple OSS Distributions 	//
161*043036a2SApple OSS Distributions 	// Free all of this object's outstanding resources.
162*043036a2SApple OSS Distributions 	//
163*043036a2SApple OSS Distributions 
164*043036a2SApple OSS Distributions 	if (_descriptors) {
165*043036a2SApple OSS Distributions 		for (unsigned index = 0; index < _descriptorCount; index++) {
166*043036a2SApple OSS Distributions 			_descriptors[index]->release();
167*043036a2SApple OSS Distributions 		}
168*043036a2SApple OSS Distributions 
169*043036a2SApple OSS Distributions 		if (_descriptors != NULL) {
170*043036a2SApple OSS Distributions 			IODelete(_descriptors, IOMemoryDescriptor *, _descriptorCapacity);
171*043036a2SApple OSS Distributions 		}
172*043036a2SApple OSS Distributions 
173*043036a2SApple OSS Distributions 		if (_descriptorOffsets != NULL) {
174*043036a2SApple OSS Distributions 			IODeleteData(_descriptorOffsets, IOByteCount, _descriptorCapacity);
175*043036a2SApple OSS Distributions 		}
176*043036a2SApple OSS Distributions 
177*043036a2SApple OSS Distributions 		if (_descriptorLengths != NULL) {
178*043036a2SApple OSS Distributions 			IODeleteData(_descriptorLengths, IOByteCount, _descriptorCapacity);
179*043036a2SApple OSS Distributions 		}
180*043036a2SApple OSS Distributions 	}
181*043036a2SApple OSS Distributions 
182*043036a2SApple OSS Distributions 	super::free();
183*043036a2SApple OSS Distributions }
184*043036a2SApple OSS Distributions 
185*043036a2SApple OSS Distributions IOReturn
prepare(IODirection forDirection)186*043036a2SApple OSS Distributions IOInterleavedMemoryDescriptor::prepare(IODirection forDirection)
187*043036a2SApple OSS Distributions {
188*043036a2SApple OSS Distributions 	//
189*043036a2SApple OSS Distributions 	// Prepare the memory for an I/O transfer.
190*043036a2SApple OSS Distributions 	//
191*043036a2SApple OSS Distributions 	// This involves paging in the memory and wiring it down for the duration
192*043036a2SApple OSS Distributions 	// of the transfer.  The complete() method finishes the processing of the
193*043036a2SApple OSS Distributions 	// memory after the I/O transfer finishes.
194*043036a2SApple OSS Distributions 	//
195*043036a2SApple OSS Distributions 
196*043036a2SApple OSS Distributions 	unsigned index;
197*043036a2SApple OSS Distributions 	IOReturn status = kIOReturnSuccess;
198*043036a2SApple OSS Distributions 	IOReturn statusUndo;
199*043036a2SApple OSS Distributions 
200*043036a2SApple OSS Distributions 	if (forDirection == kIODirectionNone) {
201*043036a2SApple OSS Distributions 		forDirection = getDirection();
202*043036a2SApple OSS Distributions 	}
203*043036a2SApple OSS Distributions 
204*043036a2SApple OSS Distributions 	for (index = 0; index < _descriptorCount; index++) {
205*043036a2SApple OSS Distributions 		status = _descriptors[index]->prepare(forDirection);
206*043036a2SApple OSS Distributions 		if (status != kIOReturnSuccess) {
207*043036a2SApple OSS Distributions 			break;
208*043036a2SApple OSS Distributions 		}
209*043036a2SApple OSS Distributions 	}
210*043036a2SApple OSS Distributions 
211*043036a2SApple OSS Distributions 	if (status != kIOReturnSuccess) {
212*043036a2SApple OSS Distributions 		for (unsigned indexUndo = 0; indexUndo < index; indexUndo++) {
213*043036a2SApple OSS Distributions 			statusUndo = _descriptors[index]->complete(forDirection);
214*043036a2SApple OSS Distributions 			assert(statusUndo == kIOReturnSuccess);
215*043036a2SApple OSS Distributions 		}
216*043036a2SApple OSS Distributions 	}
217*043036a2SApple OSS Distributions 
218*043036a2SApple OSS Distributions 	if (status == kIOReturnSuccess) {
219*043036a2SApple OSS Distributions 		_descriptorPrepared = true;
220*043036a2SApple OSS Distributions 	}
221*043036a2SApple OSS Distributions 
222*043036a2SApple OSS Distributions 	return status;
223*043036a2SApple OSS Distributions }
224*043036a2SApple OSS Distributions 
225*043036a2SApple OSS Distributions IOReturn
complete(IODirection forDirection)226*043036a2SApple OSS Distributions IOInterleavedMemoryDescriptor::complete(IODirection forDirection)
227*043036a2SApple OSS Distributions {
228*043036a2SApple OSS Distributions 	//
229*043036a2SApple OSS Distributions 	// Complete processing of the memory after an I/O transfer finishes.
230*043036a2SApple OSS Distributions 	//
231*043036a2SApple OSS Distributions 	// This method shouldn't be called unless a prepare() was previously issued;
232*043036a2SApple OSS Distributions 	// the prepare() and complete() must occur in pairs, before and after an I/O
233*043036a2SApple OSS Distributions 	// transfer.
234*043036a2SApple OSS Distributions 	//
235*043036a2SApple OSS Distributions 
236*043036a2SApple OSS Distributions 	IOReturn status;
237*043036a2SApple OSS Distributions 	IOReturn statusFinal = kIOReturnSuccess;
238*043036a2SApple OSS Distributions 
239*043036a2SApple OSS Distributions 	if (forDirection == kIODirectionNone) {
240*043036a2SApple OSS Distributions 		forDirection = getDirection();
241*043036a2SApple OSS Distributions 	}
242*043036a2SApple OSS Distributions 
243*043036a2SApple OSS Distributions 	for (unsigned index = 0; index < _descriptorCount; index++) {
244*043036a2SApple OSS Distributions 		status = _descriptors[index]->complete(forDirection);
245*043036a2SApple OSS Distributions 		if (status != kIOReturnSuccess) {
246*043036a2SApple OSS Distributions 			statusFinal = status;
247*043036a2SApple OSS Distributions 		}
248*043036a2SApple OSS Distributions 		assert(status == kIOReturnSuccess);
249*043036a2SApple OSS Distributions 	}
250*043036a2SApple OSS Distributions 
251*043036a2SApple OSS Distributions 	_descriptorPrepared = false;
252*043036a2SApple OSS Distributions 
253*043036a2SApple OSS Distributions 	return statusFinal;
254*043036a2SApple OSS Distributions }
255*043036a2SApple OSS Distributions 
256*043036a2SApple OSS Distributions addr64_t
getPhysicalSegment(IOByteCount offset,IOByteCount * length,IOOptionBits options)257*043036a2SApple OSS Distributions IOInterleavedMemoryDescriptor::getPhysicalSegment(
258*043036a2SApple OSS Distributions 	IOByteCount   offset,
259*043036a2SApple OSS Distributions 	IOByteCount * length,
260*043036a2SApple OSS Distributions 	IOOptionBits  options )
261*043036a2SApple OSS Distributions {
262*043036a2SApple OSS Distributions 	//
263*043036a2SApple OSS Distributions 	// This method returns the physical address of the byte at the given offset
264*043036a2SApple OSS Distributions 	// into the memory,  and optionally the length of the physically contiguous
265*043036a2SApple OSS Distributions 	// segment from that offset.
266*043036a2SApple OSS Distributions 	//
267*043036a2SApple OSS Distributions 
268*043036a2SApple OSS Distributions 	addr64_t pa;
269*043036a2SApple OSS Distributions 
270*043036a2SApple OSS Distributions 	assert(offset <= _length);
271*043036a2SApple OSS Distributions 
272*043036a2SApple OSS Distributions 	for (unsigned index = 0; index < _descriptorCount; index++) {
273*043036a2SApple OSS Distributions 		if (offset < _descriptorLengths[index]) {
274*043036a2SApple OSS Distributions 			pa = _descriptors[index]->getPhysicalSegment(_descriptorOffsets[index] + offset, length, options);
275*043036a2SApple OSS Distributions 			if ((_descriptorLengths[index] - offset) < *length) {
276*043036a2SApple OSS Distributions 				*length = _descriptorLengths[index] - offset;
277*043036a2SApple OSS Distributions 			}
278*043036a2SApple OSS Distributions 			return pa;
279*043036a2SApple OSS Distributions 		}
280*043036a2SApple OSS Distributions 		offset -= _descriptorLengths[index];
281*043036a2SApple OSS Distributions 	}
282*043036a2SApple OSS Distributions 
283*043036a2SApple OSS Distributions 	if (length) {
284*043036a2SApple OSS Distributions 		*length = 0;
285*043036a2SApple OSS Distributions 	}
286*043036a2SApple OSS Distributions 
287*043036a2SApple OSS Distributions 	return 0;
288*043036a2SApple OSS Distributions }
289