2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* IOMemoryCursor.cpp created by wgulland on 1999-3-02 */
27 #include <IOKit/assert.h>
28 #include <IOKit/IOLib.h>
29 #include <IOKit/IOMemoryCursor.h>
30 #include <IOKit/IOMemoryDescriptor.h>
31 #include <libkern/OSByteOrder.h>
33 /**************************** class IOMemoryCursor ***************************/
36 #define super OSObject
37 OSDefineMetaClassAndStructors(IOMemoryCursor
, OSObject
)
40 IOMemoryCursor::withSpecification(SegmentFunction inSegFunc
,
41 IOPhysicalLength inMaxSegmentSize
,
42 IOPhysicalLength inMaxTransferSize
,
43 IOPhysicalLength inAlignment
)
45 IOMemoryCursor
* me
= new IOMemoryCursor
;
47 if (me
&& !me
->initWithSpecification(inSegFunc
,
59 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
62 IOMemoryCursor::initWithSpecification(SegmentFunction inSegFunc
,
63 IOPhysicalLength inMaxSegmentSize
,
64 IOPhysicalLength inMaxTransferSize
,
65 IOPhysicalLength inAlignment
)
67 // @@@ gvdl: Remove me
69 static UInt sMaxDBDMASegment
;
70 if (!sMaxDBDMASegment
) {
71 sMaxDBDMASegment
= (UInt
) -1;
72 if (PE_parse_boot_arg("mseg", &sMaxDBDMASegment
))
73 IOLog("Setting MaxDBDMASegment to %d\n", sMaxDBDMASegment
);
76 if (inMaxSegmentSize
> sMaxDBDMASegment
) inMaxSegmentSize
= sMaxDBDMASegment
;
86 maxSegmentSize
= inMaxSegmentSize
;
87 if (inMaxTransferSize
)
88 maxTransferSize
= inMaxTransferSize
;
90 maxTransferSize
= (IOPhysicalLength
) -1;
91 alignMask
= inAlignment
- 1;
92 assert(alignMask
== 0); // No alignment code yet!
97 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
100 IOMemoryCursor::genPhysicalSegments(IOMemoryDescriptor
*inDescriptor
,
101 IOPhysicalLength fromPosition
,
103 UInt32 inMaxSegments
,
104 UInt32 inMaxTransferSize
,
105 IOByteCount
*outTransferSize
)
113 if (!inMaxTransferSize
)
114 inMaxTransferSize
= maxTransferSize
;
117 * Iterate over the packet, translating segments where allowed
119 * If we finished cleanly return number of segments found
120 * and update the position in the descriptor.
122 UInt curSegIndex
= 0;
123 UInt curTransferSize
= 0;
126 while ((curSegIndex
< inMaxSegments
)
127 && (curTransferSize
< inMaxTransferSize
)
128 && (seg
.location
= inDescriptor
->getPhysicalSegment(
129 fromPosition
+ curTransferSize
, &seg
.length
)))
132 seg
.length
= min(inMaxTransferSize
-curTransferSize
,
133 (min(seg
.length
, maxSegmentSize
)));
134 (*outSeg
)(seg
, inSegments
, curSegIndex
++);
135 curTransferSize
+= seg
.length
;
139 *outTransferSize
= curTransferSize
;
144 /************************ class IONaturalMemoryCursor ************************/
147 #define super IOMemoryCursor
148 OSDefineMetaClassAndStructors(IONaturalMemoryCursor
, IOMemoryCursor
)
150 void IONaturalMemoryCursor::outputSegment(PhysicalSegment segment
,
152 UInt32 outSegmentIndex
)
154 ((PhysicalSegment
*) outSegments
)[outSegmentIndex
] = segment
;
157 IONaturalMemoryCursor
*
158 IONaturalMemoryCursor::withSpecification(IOPhysicalLength inMaxSegmentSize
,
159 IOPhysicalLength inMaxTransferSize
,
160 IOPhysicalLength inAlignment
)
162 IONaturalMemoryCursor
*me
= new IONaturalMemoryCursor
;
164 if (me
&& !me
->initWithSpecification(inMaxSegmentSize
,
176 IONaturalMemoryCursor::initWithSpecification(IOPhysicalLength inMaxSegmentSize
,
177 IOPhysicalLength inMaxTransferSize
,
178 IOPhysicalLength inAlignment
)
180 return super::initWithSpecification(&IONaturalMemoryCursor::outputSegment
,
186 /************************** class IOBigMemoryCursor **************************/
189 #define super IOMemoryCursor
190 OSDefineMetaClassAndStructors(IOBigMemoryCursor
, IOMemoryCursor
)
193 IOBigMemoryCursor::outputSegment(PhysicalSegment inSegment
,
195 UInt32 inSegmentIndex
)
197 IOPhysicalAddress
* segment
;
199 segment
= &((PhysicalSegment
*) inSegments
)[inSegmentIndex
].location
;
200 OSWriteBigInt(segment
, 0, inSegment
.location
);
201 OSWriteBigInt(segment
, sizeof(IOPhysicalAddress
), inSegment
.length
);
205 IOBigMemoryCursor::withSpecification(IOPhysicalLength inMaxSegmentSize
,
206 IOPhysicalLength inMaxTransferSize
,
207 IOPhysicalLength inAlignment
)
209 IOBigMemoryCursor
* me
= new IOBigMemoryCursor
;
211 if (me
&& !me
->initWithSpecification(inMaxSegmentSize
,
223 IOBigMemoryCursor::initWithSpecification(IOPhysicalLength inMaxSegmentSize
,
224 IOPhysicalLength inMaxTransferSize
,
225 IOPhysicalLength inAlignment
)
227 return super::initWithSpecification(&IOBigMemoryCursor::outputSegment
,
233 /************************* class IOLittleMemoryCursor ************************/
236 #define super IOMemoryCursor
237 OSDefineMetaClassAndStructors(IOLittleMemoryCursor
, IOMemoryCursor
)
240 IOLittleMemoryCursor::outputSegment(PhysicalSegment inSegment
,
242 UInt32 inSegmentIndex
)
244 IOPhysicalAddress
* segment
;
246 segment
= &((PhysicalSegment
*) inSegments
)[inSegmentIndex
].location
;
247 OSWriteLittleInt(segment
, 0, inSegment
.location
);
248 OSWriteLittleInt(segment
, sizeof(IOPhysicalAddress
), inSegment
.length
);
251 IOLittleMemoryCursor
*
252 IOLittleMemoryCursor::withSpecification(IOPhysicalLength inMaxSegmentSize
,
253 IOPhysicalLength inMaxTransferSize
,
254 IOPhysicalLength inAlignment
)
256 IOLittleMemoryCursor
* me
= new IOLittleMemoryCursor
;
258 if (me
&& !me
->initWithSpecification(inMaxSegmentSize
,
269 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
272 IOLittleMemoryCursor::initWithSpecification(IOPhysicalLength inMaxSegmentSize
,
273 IOPhysicalLength inMaxTransferSize
,
274 IOPhysicalLength inAlignment
)
276 return super::initWithSpecification(&IOLittleMemoryCursor::outputSegment
,
282 /************************* class IODBDMAMemoryCursor *************************/
286 #include <IOKit/ppc/IODBDMA.h>
289 #define super IOMemoryCursor
290 OSDefineMetaClassAndStructors(IODBDMAMemoryCursor
, IOMemoryCursor
)
293 IODBDMAMemoryCursor::outputSegment(PhysicalSegment inSegment
,
295 UInt32 inSegmentIndex
)
297 IODBDMADescriptor
*segment
;
299 segment
= &((IODBDMADescriptor
*) inSegments
)[inSegmentIndex
];
301 // Write location into address field
302 OSWriteSwapInt32((UInt32
*) segment
, 4, inSegment
.location
);
304 // Write count into 1st two bytes of operation field.
305 // DO NOT touch rest of operation field as it should contain a STOP command.
306 OSWriteSwapInt16((UInt16
*) segment
, 0, inSegment
.length
);
309 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
311 IODBDMAMemoryCursor
*
312 IODBDMAMemoryCursor::withSpecification(IOPhysicalLength inMaxSegmentSize
,
313 IOPhysicalLength inMaxTransferSize
,
314 IOPhysicalLength inAlignment
)
316 IODBDMAMemoryCursor
*me
= new IODBDMAMemoryCursor
;
318 if (me
&& !me
->initWithSpecification(inMaxSegmentSize
,
329 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
332 IODBDMAMemoryCursor::initWithSpecification(IOPhysicalLength inMaxSegmentSize
,
333 IOPhysicalLength inMaxTransferSize
,
334 IOPhysicalLength inAlignment
)
336 return super::initWithSpecification(&IODBDMAMemoryCursor::outputSegment
,
342 #endif /* defined(__ppc__) */