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