2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* IOMemoryCursor.cpp created by wgulland on 1999-3-02 */
30 #include <IOKit/assert.h>
31 #include <IOKit/IOLib.h>
32 #include <IOKit/IOMemoryCursor.h>
33 #include <IOKit/IOMemoryDescriptor.h>
34 #include <libkern/OSByteOrder.h>
36 /**************************** class IOMemoryCursor ***************************/
39 #define super OSObject
40 OSDefineMetaClassAndStructors(IOMemoryCursor
, OSObject
)
43 IOMemoryCursor::withSpecification(SegmentFunction inSegFunc
,
44 IOPhysicalLength inMaxSegmentSize
,
45 IOPhysicalLength inMaxTransferSize
,
46 IOPhysicalLength inAlignment
)
48 IOMemoryCursor
* me
= new IOMemoryCursor
;
50 if (me
&& !me
->initWithSpecification(inSegFunc
,
61 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
64 IOMemoryCursor::initWithSpecification(SegmentFunction inSegFunc
,
65 IOPhysicalLength inMaxSegmentSize
,
66 IOPhysicalLength inMaxTransferSize
,
67 IOPhysicalLength inAlignment
)
69 // @@@ gvdl: Remove me
71 static UInt sMaxDBDMASegment
;
72 if (!sMaxDBDMASegment
) {
73 sMaxDBDMASegment
= (UInt
) - 1;
74 if (PE_parse_boot_argn("mseg", &sMaxDBDMASegment
, sizeof(sMaxDBDMASegment
))) {
75 IOLog("Setting MaxDBDMASegment to %d\n", sMaxDBDMASegment
);
79 if (inMaxSegmentSize
> sMaxDBDMASegment
) {
80 inMaxSegmentSize
= sMaxDBDMASegment
;
93 maxSegmentSize
= inMaxSegmentSize
;
94 if (inMaxTransferSize
) {
95 maxTransferSize
= inMaxTransferSize
;
97 maxTransferSize
= (IOPhysicalLength
) - 1;
99 alignMask
= inAlignment
- 1;
100 assert(alignMask
== 0); // No alignment code yet!
105 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
108 IOMemoryCursor::genPhysicalSegments(IOMemoryDescriptor
*inDescriptor
,
109 IOByteCount fromPosition
,
111 UInt32 inMaxSegments
,
112 UInt32 inMaxTransferSize
,
113 IOByteCount
*outTransferSize
)
119 if (!inMaxSegments
) {
123 if (!inMaxTransferSize
) {
124 inMaxTransferSize
= maxTransferSize
;
128 * Iterate over the packet, translating segments where allowed
130 * If we finished cleanly return number of segments found
131 * and update the position in the descriptor.
133 PhysicalSegment curSeg
= { 0, 0 };
134 UInt curSegIndex
= 0;
135 UInt curTransferSize
= 0;
136 IOByteCount inDescriptorLength
= inDescriptor
->getLength();
137 PhysicalSegment seg
= { 0, 0 };
139 while ((seg
.location
) || (fromPosition
< inDescriptorLength
)) {
141 seg
.location
= inDescriptor
->getPhysicalSegment(
142 fromPosition
, (IOByteCount
*)&seg
.length
);
143 assert(seg
.location
);
145 fromPosition
+= seg
.length
;
148 if (!curSeg
.location
) {
149 curTransferSize
+= seg
.length
;
152 } else if ((curSeg
.location
+ curSeg
.length
== seg
.location
)) {
153 curTransferSize
+= seg
.length
;
154 curSeg
.length
+= seg
.length
;
159 if ((curSeg
.length
> maxSegmentSize
)) {
160 seg
.location
= curSeg
.location
+ maxSegmentSize
;
161 seg
.length
= curSeg
.length
- maxSegmentSize
;
162 curTransferSize
-= seg
.length
;
163 curSeg
.length
-= seg
.length
;
166 if ((curTransferSize
>= inMaxTransferSize
)) {
167 curSeg
.length
-= curTransferSize
- inMaxTransferSize
;
168 curTransferSize
= inMaxTransferSize
;
174 if ((curSegIndex
+ 1 == inMaxSegments
)) {
177 (*outSeg
)(curSeg
, inSegments
, curSegIndex
++);
182 if (curSeg
.location
) {
183 (*outSeg
)(curSeg
, inSegments
, curSegIndex
++);
186 if (outTransferSize
) {
187 *outTransferSize
= curTransferSize
;
193 /************************ class IONaturalMemoryCursor ************************/
196 #define super IOMemoryCursor
197 OSDefineMetaClassAndStructors(IONaturalMemoryCursor
, IOMemoryCursor
)
200 IONaturalMemoryCursor::outputSegment(PhysicalSegment segment
,
202 UInt32 outSegmentIndex
)
204 ((PhysicalSegment
*) outSegments
)[outSegmentIndex
] = segment
;
207 IONaturalMemoryCursor
*
208 IONaturalMemoryCursor::withSpecification(IOPhysicalLength inMaxSegmentSize
,
209 IOPhysicalLength inMaxTransferSize
,
210 IOPhysicalLength inAlignment
)
212 IONaturalMemoryCursor
*me
= new IONaturalMemoryCursor
;
214 if (me
&& !me
->initWithSpecification(inMaxSegmentSize
,
225 IONaturalMemoryCursor::initWithSpecification(IOPhysicalLength inMaxSegmentSize
,
226 IOPhysicalLength inMaxTransferSize
,
227 IOPhysicalLength inAlignment
)
229 return super::initWithSpecification(&IONaturalMemoryCursor::outputSegment
,
235 /************************** class IOBigMemoryCursor **************************/
238 #define super IOMemoryCursor
239 OSDefineMetaClassAndStructors(IOBigMemoryCursor
, IOMemoryCursor
)
242 IOBigMemoryCursor::outputSegment(PhysicalSegment inSegment
,
244 UInt32 inSegmentIndex
)
246 IOPhysicalAddress
* segment
;
248 segment
= &((PhysicalSegment
*) inSegments
)[inSegmentIndex
].location
;
250 OSWriteBigInt64(segment
, 0, inSegment
.location
);
251 OSWriteBigInt64(segment
, sizeof(IOPhysicalAddress
), inSegment
.length
);
253 OSWriteBigInt(segment
, 0, inSegment
.location
);
254 OSWriteBigInt(segment
, sizeof(IOPhysicalAddress
), inSegment
.length
);
259 IOBigMemoryCursor::withSpecification(IOPhysicalLength inMaxSegmentSize
,
260 IOPhysicalLength inMaxTransferSize
,
261 IOPhysicalLength inAlignment
)
263 IOBigMemoryCursor
* me
= new IOBigMemoryCursor
;
265 if (me
&& !me
->initWithSpecification(inMaxSegmentSize
,
276 IOBigMemoryCursor::initWithSpecification(IOPhysicalLength inMaxSegmentSize
,
277 IOPhysicalLength inMaxTransferSize
,
278 IOPhysicalLength inAlignment
)
280 return super::initWithSpecification(&IOBigMemoryCursor::outputSegment
,
286 /************************* class IOLittleMemoryCursor ************************/
289 #define super IOMemoryCursor
290 OSDefineMetaClassAndStructors(IOLittleMemoryCursor
, IOMemoryCursor
)
293 IOLittleMemoryCursor::outputSegment(PhysicalSegment inSegment
,
295 UInt32 inSegmentIndex
)
297 IOPhysicalAddress
* segment
;
299 segment
= &((PhysicalSegment
*) inSegments
)[inSegmentIndex
].location
;
301 OSWriteLittleInt64(segment
, 0, inSegment
.location
);
302 OSWriteLittleInt64(segment
, sizeof(IOPhysicalAddress
), inSegment
.length
);
304 OSWriteLittleInt(segment
, 0, inSegment
.location
);
305 OSWriteLittleInt(segment
, sizeof(IOPhysicalAddress
), inSegment
.length
);
309 IOLittleMemoryCursor
*
310 IOLittleMemoryCursor::withSpecification(IOPhysicalLength inMaxSegmentSize
,
311 IOPhysicalLength inMaxTransferSize
,
312 IOPhysicalLength inAlignment
)
314 IOLittleMemoryCursor
* me
= new IOLittleMemoryCursor
;
316 if (me
&& !me
->initWithSpecification(inMaxSegmentSize
,
326 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
329 IOLittleMemoryCursor::initWithSpecification(IOPhysicalLength inMaxSegmentSize
,
330 IOPhysicalLength inMaxTransferSize
,
331 IOPhysicalLength inAlignment
)
333 return super::initWithSpecification(&IOLittleMemoryCursor::outputSegment
,