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