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