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
,
62 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
65 IOMemoryCursor::initWithSpecification(SegmentFunction inSegFunc
,
66 IOPhysicalLength inMaxSegmentSize
,
67 IOPhysicalLength inMaxTransferSize
,
68 IOPhysicalLength inAlignment
)
70 // @@@ gvdl: Remove me
72 static UInt sMaxDBDMASegment
;
73 if (!sMaxDBDMASegment
) {
74 sMaxDBDMASegment
= (UInt
) -1;
75 if (PE_parse_boot_argn("mseg", &sMaxDBDMASegment
, sizeof (sMaxDBDMASegment
)))
76 IOLog("Setting MaxDBDMASegment to %d\n", sMaxDBDMASegment
);
79 if (inMaxSegmentSize
> sMaxDBDMASegment
) inMaxSegmentSize
= sMaxDBDMASegment
;
89 maxSegmentSize
= inMaxSegmentSize
;
90 if (inMaxTransferSize
)
91 maxTransferSize
= inMaxTransferSize
;
93 maxTransferSize
= (IOPhysicalLength
) -1;
94 alignMask
= inAlignment
- 1;
95 assert(alignMask
== 0); // No alignment code yet!
100 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
103 IOMemoryCursor::genPhysicalSegments(IOMemoryDescriptor
*inDescriptor
,
104 IOByteCount fromPosition
,
106 UInt32 inMaxSegments
,
107 UInt32 inMaxTransferSize
,
108 IOByteCount
*outTransferSize
)
116 if (!inMaxTransferSize
)
117 inMaxTransferSize
= maxTransferSize
;
120 * Iterate over the packet, translating segments where allowed
122 * If we finished cleanly return number of segments found
123 * and update the position in the descriptor.
125 PhysicalSegment curSeg
= { 0, 0 };
126 UInt curSegIndex
= 0;
127 UInt curTransferSize
= 0;
128 IOByteCount inDescriptorLength
= inDescriptor
->getLength();
129 PhysicalSegment seg
= { 0, 0 };
131 while ((seg
.location
) || (fromPosition
< inDescriptorLength
))
135 seg
.location
= inDescriptor
->getPhysicalSegment(
136 fromPosition
, (IOByteCount
*)&seg
.length
);
137 assert(seg
.location
);
139 fromPosition
+= seg
.length
;
142 if (!curSeg
.location
)
144 curTransferSize
+= seg
.length
;
148 else if ((curSeg
.location
+ curSeg
.length
== seg
.location
))
150 curTransferSize
+= seg
.length
;
151 curSeg
.length
+= seg
.length
;
157 if ((curSeg
.length
> maxSegmentSize
))
159 seg
.location
= curSeg
.location
+ maxSegmentSize
;
160 seg
.length
= curSeg
.length
- maxSegmentSize
;
161 curTransferSize
-= seg
.length
;
162 curSeg
.length
-= seg
.length
;
165 if ((curTransferSize
>= inMaxTransferSize
))
167 curSeg
.length
-= curTransferSize
- inMaxTransferSize
;
168 curTransferSize
= inMaxTransferSize
;
175 if ((curSegIndex
+ 1 == inMaxSegments
))
177 (*outSeg
)(curSeg
, inSegments
, curSegIndex
++);
183 (*outSeg
)(curSeg
, inSegments
, curSegIndex
++);
186 *outTransferSize
= curTransferSize
;
191 /************************ class IONaturalMemoryCursor ************************/
194 #define super IOMemoryCursor
195 OSDefineMetaClassAndStructors(IONaturalMemoryCursor
, IOMemoryCursor
)
197 void IONaturalMemoryCursor::outputSegment(PhysicalSegment segment
,
199 UInt32 outSegmentIndex
)
201 ((PhysicalSegment
*) outSegments
)[outSegmentIndex
] = segment
;
204 IONaturalMemoryCursor
*
205 IONaturalMemoryCursor::withSpecification(IOPhysicalLength inMaxSegmentSize
,
206 IOPhysicalLength inMaxTransferSize
,
207 IOPhysicalLength inAlignment
)
209 IONaturalMemoryCursor
*me
= new IONaturalMemoryCursor
;
211 if (me
&& !me
->initWithSpecification(inMaxSegmentSize
,
223 IONaturalMemoryCursor::initWithSpecification(IOPhysicalLength inMaxSegmentSize
,
224 IOPhysicalLength inMaxTransferSize
,
225 IOPhysicalLength inAlignment
)
227 return super::initWithSpecification(&IONaturalMemoryCursor::outputSegment
,
233 /************************** class IOBigMemoryCursor **************************/
236 #define super IOMemoryCursor
237 OSDefineMetaClassAndStructors(IOBigMemoryCursor
, IOMemoryCursor
)
240 IOBigMemoryCursor::outputSegment(PhysicalSegment inSegment
,
242 UInt32 inSegmentIndex
)
244 IOPhysicalAddress
* segment
;
246 segment
= &((PhysicalSegment
*) inSegments
)[inSegmentIndex
].location
;
247 OSWriteBigInt(segment
, 0, inSegment
.location
);
248 OSWriteBigInt(segment
, sizeof(IOPhysicalAddress
), inSegment
.length
);
252 IOBigMemoryCursor::withSpecification(IOPhysicalLength inMaxSegmentSize
,
253 IOPhysicalLength inMaxTransferSize
,
254 IOPhysicalLength inAlignment
)
256 IOBigMemoryCursor
* me
= new IOBigMemoryCursor
;
258 if (me
&& !me
->initWithSpecification(inMaxSegmentSize
,
270 IOBigMemoryCursor::initWithSpecification(IOPhysicalLength inMaxSegmentSize
,
271 IOPhysicalLength inMaxTransferSize
,
272 IOPhysicalLength inAlignment
)
274 return super::initWithSpecification(&IOBigMemoryCursor::outputSegment
,
280 /************************* class IOLittleMemoryCursor ************************/
283 #define super IOMemoryCursor
284 OSDefineMetaClassAndStructors(IOLittleMemoryCursor
, IOMemoryCursor
)
287 IOLittleMemoryCursor::outputSegment(PhysicalSegment inSegment
,
289 UInt32 inSegmentIndex
)
291 IOPhysicalAddress
* segment
;
293 segment
= &((PhysicalSegment
*) inSegments
)[inSegmentIndex
].location
;
294 OSWriteLittleInt(segment
, 0, inSegment
.location
);
295 OSWriteLittleInt(segment
, sizeof(IOPhysicalAddress
), inSegment
.length
);
298 IOLittleMemoryCursor
*
299 IOLittleMemoryCursor::withSpecification(IOPhysicalLength inMaxSegmentSize
,
300 IOPhysicalLength inMaxTransferSize
,
301 IOPhysicalLength inAlignment
)
303 IOLittleMemoryCursor
* me
= new IOLittleMemoryCursor
;
305 if (me
&& !me
->initWithSpecification(inMaxSegmentSize
,
316 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
319 IOLittleMemoryCursor::initWithSpecification(IOPhysicalLength inMaxSegmentSize
,
320 IOPhysicalLength inMaxTransferSize
,
321 IOPhysicalLength inAlignment
)
323 return super::initWithSpecification(&IOLittleMemoryCursor::outputSegment
,