2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #include <IOKit/assert.h>
33 #include <libkern/OSTypes.h>
34 #include <libkern/OSByteOrder.h>
36 #include <IOKit/IOReturn.h>
37 #include <IOKit/IOLib.h>
38 #include <IOKit/IODMACommand.h>
39 #include <IOKit/IOMapper.h>
40 #include <IOKit/IOMemoryDescriptor.h>
41 #include <IOKit/IOBufferMemoryDescriptor.h>
43 #include "IOKitKernelInternal.h"
44 #include "IOCopyMapper.h"
46 #define MAPTYPE(type) ((UInt) (type) & kTypeMask)
47 #define IS_MAPPED(type) (MAPTYPE(type) == kMapped)
48 #define IS_BYPASSED(type) (MAPTYPE(type) == kBypassed)
49 #define IS_NONCOHERENT(type) (MAPTYPE(type) == kNonCoherent)
52 static bool gIOEnableCopyMapper
= true;
56 kWalkSyncIn
= 0x01, // bounce -> md
57 kWalkSyncOut
= 0x02, // bounce <- md
58 kWalkSyncAlways
= 0x04,
59 kWalkPreflight
= 0x08,
60 kWalkDoubleBuffer
= 0x10,
68 IOMDDMAWalkSegmentState fState
;
69 IOMDDMACharacteristics fMDSummary
;
71 UInt64 fPreparedOffset
;
72 UInt64 fPreparedLength
;
75 UInt8 fCheckAddressing
;
83 ppnum_t fCopyPageAlloc
;
84 ppnum_t fCopyPageCount
;
87 class IOBufferMemoryDescriptor
* fCopyMD
;
89 typedef ExpansionData IODMACommandInternal
;
91 #define fInternalState reserved
92 #define fState reserved->fState
93 #define fMDSummary reserved->fMDSummary
97 // no direction => OutIn
98 #define SHOULD_COPY_DIR(op, direction) \
99 ((kIODirectionNone == (direction)) \
100 || (kWalkSyncAlways & (op)) \
101 || (((kWalkSyncIn & (op)) ? kIODirectionIn : kIODirectionOut) \
105 #define SHOULD_COPY_DIR(state, direction) (true)
109 #define DEBG(fmt, args...) { kprintf(fmt, ## args); }
111 #define DEBG(fmt, args...) {}
115 /**************************** class IODMACommand ***************************/
118 #define super OSObject
119 OSDefineMetaClassAndStructors(IODMACommand
, IOCommand
);
121 OSMetaClassDefineReservedUnused(IODMACommand
, 0);
122 OSMetaClassDefineReservedUnused(IODMACommand
, 1);
123 OSMetaClassDefineReservedUnused(IODMACommand
, 2);
124 OSMetaClassDefineReservedUnused(IODMACommand
, 3);
125 OSMetaClassDefineReservedUnused(IODMACommand
, 4);
126 OSMetaClassDefineReservedUnused(IODMACommand
, 5);
127 OSMetaClassDefineReservedUnused(IODMACommand
, 6);
128 OSMetaClassDefineReservedUnused(IODMACommand
, 7);
129 OSMetaClassDefineReservedUnused(IODMACommand
, 8);
130 OSMetaClassDefineReservedUnused(IODMACommand
, 9);
131 OSMetaClassDefineReservedUnused(IODMACommand
, 10);
132 OSMetaClassDefineReservedUnused(IODMACommand
, 11);
133 OSMetaClassDefineReservedUnused(IODMACommand
, 12);
134 OSMetaClassDefineReservedUnused(IODMACommand
, 13);
135 OSMetaClassDefineReservedUnused(IODMACommand
, 14);
136 OSMetaClassDefineReservedUnused(IODMACommand
, 15);
139 IODMACommand::withSpecification(SegmentFunction outSegFunc
,
140 UInt8 numAddressBits
,
141 UInt64 maxSegmentSize
,
142 MappingOptions mappingOptions
,
143 UInt64 maxTransferSize
,
148 IODMACommand
* me
= new IODMACommand
;
150 if (me
&& !me
->initWithSpecification(outSegFunc
,
151 numAddressBits
, maxSegmentSize
,
152 mappingOptions
, maxTransferSize
,
153 alignment
, mapper
, refCon
))
163 IODMACommand::cloneCommand(void *refCon
)
165 return withSpecification(fOutSeg
, fNumAddressBits
, fMaxSegmentSize
,
166 fMappingOptions
, fMaxTransferSize
, fAlignMask
+ 1, fMapper
, refCon
);
169 #define kLastOutputFunction ((SegmentFunction) kLastOutputFunction)
172 IODMACommand::initWithSpecification(SegmentFunction outSegFunc
,
173 UInt8 numAddressBits
,
174 UInt64 maxSegmentSize
,
175 MappingOptions mappingOptions
,
176 UInt64 maxTransferSize
,
181 if (!super::init() || !outSegFunc
|| !numAddressBits
)
184 bool is32Bit
= (OutputHost32
== outSegFunc
|| OutputBig32
== outSegFunc
185 || OutputLittle32
== outSegFunc
);
190 else if (numAddressBits
> 32)
191 return false; // Wrong output function for bits
194 if (numAddressBits
&& (numAddressBits
< PAGE_SHIFT
))
198 maxSegmentSize
--; // Set Max segment to -1
199 if (!maxTransferSize
)
200 maxTransferSize
--; // Set Max transfer to -1
204 IOMapper::checkForSystemMapper();
205 mapper
= IOMapper::gSystem
;
210 fOutSeg
= outSegFunc
;
211 fNumAddressBits
= numAddressBits
;
212 fMaxSegmentSize
= maxSegmentSize
;
213 fMappingOptions
= mappingOptions
;
214 fMaxTransferSize
= maxTransferSize
;
217 fAlignMask
= alignment
- 1;
221 switch (MAPTYPE(mappingOptions
))
224 case kNonCoherent
: fMapper
= 0; break;
226 if (mapper
&& !mapper
->getBypassMask(&fBypassMask
))
233 reserved
= IONew(ExpansionData
, 1);
236 bzero(reserved
, sizeof(ExpansionData
));
238 fInternalState
->fIterateOnly
= (0 != (kIterateOnly
& mappingOptions
));
247 IODelete(reserved
, ExpansionData
, 1);
253 IODMACommand::setMemoryDescriptor(const IOMemoryDescriptor
*mem
, bool autoPrepare
)
262 return kIOReturnSuccess
;
266 // As we are almost certainly being called from a work loop thread
267 // if fActive is true it is probably not a good time to potentially
268 // block. Just test for it and return an error
270 return kIOReturnBusy
;
271 clearMemoryDescriptor();
275 bzero(&fMDSummary
, sizeof(fMDSummary
));
276 IOReturn rtn
= mem
->dmaCommandOperation(
277 kIOMDGetCharacteristics
,
278 &fMDSummary
, sizeof(fMDSummary
));
282 ppnum_t highPage
= fMDSummary
.fHighestPage
? fMDSummary
.fHighestPage
: gIOLastPage
;
284 if ((kMapped
== MAPTYPE(fMappingOptions
))
286 && (!fNumAddressBits
|| (fNumAddressBits
>= 31)))
287 // assuming mapped space is 2G
288 fInternalState
->fCheckAddressing
= false;
290 fInternalState
->fCheckAddressing
= (fNumAddressBits
&& (highPage
>= (1UL << (fNumAddressBits
- PAGE_SHIFT
))));
299 return kIOReturnSuccess
;
303 IODMACommand::clearMemoryDescriptor(bool autoComplete
)
305 if (fActive
&& !autoComplete
)
306 return (kIOReturnNotReady
);
315 return (kIOReturnSuccess
);
318 const IOMemoryDescriptor
*
319 IODMACommand::getMemoryDescriptor() const
326 IODMACommand::segmentOp(
328 IODMACommand
*target
,
333 IOOptionBits op
= (IOOptionBits
) reference
;
334 addr64_t maxPhys
, address
;
335 addr64_t remapAddr
= 0;
339 IODMACommandInternal
* state
= target
->reserved
;
341 if (target
->fNumAddressBits
&& (target
->fNumAddressBits
< 64))
342 maxPhys
= (1ULL << target
->fNumAddressBits
);
347 address
= segment
.fIOVMAddr
;
348 length
= segment
.fLength
;
353 if (!state
->fMisaligned
)
355 state
->fMisaligned
|= (0 != (target
->fAlignMask
& address
));
356 if (state
->fMisaligned
) DEBG("misaligned %qx:%qx, %lx\n", address
, length
, target
->fAlignMask
);
359 if (state
->fMisaligned
&& (kWalkPreflight
& op
))
360 return (kIOReturnNotAligned
);
362 if (!state
->fDoubleBuffer
)
364 if ((address
+ length
- 1) <= maxPhys
)
368 else if (address
<= maxPhys
)
370 DEBG("tail %qx, %qx", address
, length
);
371 length
= (address
+ length
- maxPhys
- 1);
372 address
= maxPhys
+ 1;
373 DEBG("-> %qx, %qx\n", address
, length
);
378 return (kIOReturnSuccess
);
380 numPages
= atop_64(round_page_64(length
));
381 remapAddr
= state
->fCopyNext
;
383 if (kWalkPreflight
& op
)
385 state
->fCopyPageCount
+= numPages
;
389 if (kWalkPrepare
& op
)
391 for (IOItemCount idx
= 0; idx
< numPages
; idx
++)
392 gIOCopyMapper
->iovmInsert(atop_64(remapAddr
), idx
, atop_64(address
) + idx
);
394 if (state
->fDoubleBuffer
)
395 state
->fCopyNext
+= length
;
398 state
->fCopyNext
+= round_page(length
);
399 remapAddr
+= (address
& PAGE_MASK
);
402 if (SHOULD_COPY_DIR(op
, target
->fMDSummary
.fDirection
))
404 DEBG("cpv: 0x%qx %s 0x%qx, 0x%qx, 0x%02lx\n", remapAddr
,
405 (kWalkSyncIn
& op
) ? "->" : "<-",
406 address
, length
, op
);
407 if (kWalkSyncIn
& op
)
409 copypv(remapAddr
, address
, length
,
410 cppvPsnk
| cppvFsnk
| cppvPsrc
| cppvNoRefSrc
);
414 copypv(address
, remapAddr
, length
,
415 cppvPsnk
| cppvFsnk
| cppvPsrc
| cppvNoRefSrc
);
420 return kIOReturnSuccess
;
424 IODMACommand::walkAll(UInt8 op
)
426 IODMACommandInternal
* state
= fInternalState
;
428 IOReturn ret
= kIOReturnSuccess
;
432 if (gIOEnableCopyMapper
&& (kWalkPreflight
& op
))
434 state
->fCopyContig
= false;
435 state
->fMisaligned
= false;
436 state
->fDoubleBuffer
= false;
437 state
->fPrepared
= false;
438 state
->fCopyNext
= 0;
439 state
->fCopyPageAlloc
= 0;
440 state
->fCopyPageCount
= 0;
443 if (!(kWalkDoubleBuffer
& op
))
447 ret
= genIOVMSegments(segmentOp
, (void *) op
, &offset
, state
, &numSegments
);
450 op
&= ~kWalkPreflight
;
452 state
->fDoubleBuffer
= (state
->fMisaligned
|| (kWalkDoubleBuffer
& op
));
453 if (state
->fDoubleBuffer
)
454 state
->fCopyPageCount
= atop_64(round_page(state
->fPreparedLength
));
456 if (state
->fCopyPageCount
)
461 DEBG("preflight fCopyPageCount %d\n", state
->fCopyPageCount
);
463 mapper
= gIOCopyMapper
;
465 mapBase
= mapper
->iovmAlloc(state
->fCopyPageCount
);
468 state
->fCopyPageAlloc
= mapBase
;
469 if (state
->fCopyPageAlloc
&& state
->fDoubleBuffer
)
471 DEBG("contig copy map\n");
472 state
->fCopyContig
= true;
475 state
->fCopyNext
= ptoa_64(state
->fCopyPageAlloc
);
478 ret
= genIOVMSegments(segmentOp
, (void *) op
, &offset
, state
, &numSegments
);
479 state
->fPrepared
= true;
480 op
&= ~(kWalkSyncIn
| kWalkSyncOut
);
484 DEBG("alloc IOBMD\n");
485 state
->fCopyMD
= IOBufferMemoryDescriptor::withOptions(
486 fMDSummary
.fDirection
, state
->fPreparedLength
, page_size
);
490 ret
= kIOReturnSuccess
;
491 state
->fPrepared
= true;
495 DEBG("IODMACommand !iovmAlloc");
496 return (kIOReturnNoResources
);
502 if (gIOEnableCopyMapper
&& state
->fPrepared
&& ((kWalkSyncIn
| kWalkSyncOut
) & op
))
504 if (state
->fCopyPageCount
)
506 DEBG("sync fCopyPageCount %d\n", state
->fCopyPageCount
);
508 if (state
->fCopyPageAlloc
)
510 state
->fCopyNext
= ptoa_64(state
->fCopyPageAlloc
);
513 ret
= genIOVMSegments(segmentOp
, (void *) op
, &offset
, state
, &numSegments
);
515 else if (state
->fCopyMD
)
517 DEBG("sync IOBMD\n");
519 if (SHOULD_COPY_DIR(op
, fMDSummary
.fDirection
))
521 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
525 if (kWalkSyncIn
& op
)
526 bytes
= poMD
->writeBytes(state
->fPreparedOffset
,
527 state
->fCopyMD
->getBytesNoCopy(),
528 state
->fPreparedLength
);
530 bytes
= poMD
->readBytes(state
->fPreparedOffset
,
531 state
->fCopyMD
->getBytesNoCopy(),
532 state
->fPreparedLength
);
533 DEBG("fCopyMD %s %lx bytes\n", (kWalkSyncIn
& op
) ? "wrote" : "read", bytes
);
534 ret
= (bytes
== state
->fPreparedLength
) ? kIOReturnSuccess
: kIOReturnUnderrun
;
537 ret
= kIOReturnSuccess
;
542 if (kWalkComplete
& op
)
544 if (state
->fCopyPageAlloc
)
546 gIOCopyMapper
->iovmFree(state
->fCopyPageAlloc
, state
->fCopyPageCount
);
547 state
->fCopyPageAlloc
= 0;
548 state
->fCopyPageCount
= 0;
552 state
->fCopyMD
->release();
556 state
->fPrepared
= false;
562 IODMACommand::prepare(UInt64 offset
, UInt64 length
, bool flushCache
, bool synchronize
)
564 IODMACommandInternal
* state
= fInternalState
;
565 IOReturn ret
= kIOReturnSuccess
;
568 length
= fMDSummary
.fLength
;
570 if (length
> fMaxTransferSize
)
571 return kIOReturnNoSpace
;
574 if (IS_NONCOHERENT(mappingOptions
) && flushCache
) {
575 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
577 poMD
->performOperation(kIOMemoryIncoherentIOStore
, 0, fMDSummary
.fLength
);
582 if ((state
->fPreparedOffset
!= offset
)
583 || (state
->fPreparedLength
!= length
))
584 ret
= kIOReturnNotReady
;
588 state
->fPreparedOffset
= offset
;
589 state
->fPreparedLength
= length
;
591 state
->fCopyContig
= false;
592 state
->fMisaligned
= false;
593 state
->fDoubleBuffer
= false;
594 state
->fPrepared
= false;
595 state
->fCopyNext
= 0;
596 state
->fCopyPageAlloc
= 0;
597 state
->fCopyPageCount
= 0;
600 state
->fCursor
= state
->fIterateOnly
601 || (!state
->fCheckAddressing
603 || ((fMDSummary
.fPageAlign
& (1 << 31)) && (0 == (fMDSummary
.fPageAlign
& fAlignMask
)))));
606 IOOptionBits op
= kWalkPrepare
| kWalkPreflight
;
611 if (kIOReturnSuccess
== ret
)
612 state
->fPrepared
= true;
618 IODMACommand::complete(bool invalidateCache
, bool synchronize
)
620 IODMACommandInternal
* state
= fInternalState
;
621 IOReturn ret
= kIOReturnSuccess
;
624 return kIOReturnNotReady
;
630 IOOptionBits op
= kWalkComplete
;
635 state
->fPrepared
= false;
638 if (IS_NONCOHERENT(fMappingOptions
) && invalidateCache
)
640 // XXX gvdl: need invalidate before Chardonnay ships
641 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
643 poMD
->performOperation(kIOMemoryIncoherentIOInvalidate
, 0, fMDSummary
.fLength
);
652 IODMACommand::synchronize(IOOptionBits options
)
654 IODMACommandInternal
* state
= fInternalState
;
655 IOReturn ret
= kIOReturnSuccess
;
658 if (kIODirectionOutIn
== (kIODirectionOutIn
& options
))
659 return kIOReturnBadArgument
;
662 return kIOReturnNotReady
;
665 if (kForceDoubleBuffer
& options
)
667 if (state
->fDoubleBuffer
)
668 return kIOReturnSuccess
;
670 state
->fCursor
= false;
672 ret
= walkAll(kWalkComplete
);
674 op
|= kWalkPrepare
| kWalkPreflight
| kWalkDoubleBuffer
;
676 else if (state
->fCursor
)
677 return kIOReturnSuccess
;
679 if (kIODirectionIn
& options
)
680 op
|= kWalkSyncIn
| kWalkSyncAlways
;
681 else if (kIODirectionOut
& options
)
682 op
|= kWalkSyncOut
| kWalkSyncAlways
;
690 IODMACommand::genIOVMSegments(UInt64
*offsetP
,
692 UInt32
*numSegmentsP
)
694 return (genIOVMSegments(clientOutputSegment
, (void *) kWalkClient
, offsetP
, segmentsP
, numSegmentsP
));
698 IODMACommand::genIOVMSegments(InternalSegmentFunction outSegFunc
,
702 UInt32
*numSegmentsP
)
704 IOOptionBits op
= (IOOptionBits
) reference
;
705 IODMACommandInternal
* internalState
= fInternalState
;
706 IOOptionBits mdOp
= kIOMDWalkSegments
;
707 IOReturn ret
= kIOReturnSuccess
;
709 if (!(kWalkComplete
& op
) && !fActive
)
710 return kIOReturnNotReady
;
712 if (!offsetP
|| !segmentsP
|| !numSegmentsP
|| !*numSegmentsP
)
713 return kIOReturnBadArgument
;
715 IOMDDMAWalkSegmentArgs
*state
=
716 (IOMDDMAWalkSegmentArgs
*) fState
;
718 UInt64 offset
= *offsetP
+ internalState
->fPreparedOffset
;
719 UInt64 memLength
= internalState
->fPreparedOffset
+ internalState
->fPreparedLength
;
721 if (offset
>= memLength
)
722 return kIOReturnOverrun
;
724 if (!offset
|| offset
!= state
->fOffset
) {
726 state
->fIOVMAddr
= 0;
727 state
->fMapped
= (IS_MAPPED(fMappingOptions
) && fMapper
);
728 mdOp
= kIOMDFirstSegment
;
731 UInt64 bypassMask
= fBypassMask
;
733 UInt32 numSegments
= *numSegmentsP
;
734 Segment64 curSeg
= { 0, 0 };
737 if (fNumAddressBits
&& (fNumAddressBits
< 64))
738 maxPhys
= (1ULL << fNumAddressBits
);
743 while ((state
->fIOVMAddr
) || state
->fOffset
< memLength
)
745 if (!state
->fIOVMAddr
) {
749 state
->fOffset
= offset
;
750 state
->fLength
= memLength
- offset
;
752 if (internalState
->fCopyContig
&& (kWalkClient
& op
))
754 state
->fIOVMAddr
= ptoa_64(internalState
->fCopyPageAlloc
)
755 + offset
- internalState
->fPreparedOffset
;
756 rtn
= kIOReturnSuccess
;
760 const IOMemoryDescriptor
* memory
=
761 internalState
->fCopyMD
? internalState
->fCopyMD
: fMemory
;
762 rtn
= memory
->dmaCommandOperation(mdOp
, fState
, sizeof(fState
));
763 mdOp
= kIOMDWalkSegments
;
766 if (rtn
== kIOReturnSuccess
) {
767 assert(state
->fIOVMAddr
);
768 assert(state
->fLength
);
770 else if (rtn
== kIOReturnOverrun
)
771 state
->fIOVMAddr
= state
->fLength
= 0; // At end
776 if (!curSeg
.fIOVMAddr
) {
777 UInt64 length
= state
->fLength
;
780 curSeg
.fIOVMAddr
= state
->fIOVMAddr
| bypassMask
;
781 curSeg
.fLength
= length
;
782 state
->fIOVMAddr
= 0;
784 else if ((curSeg
.fIOVMAddr
+ curSeg
.fLength
== state
->fIOVMAddr
)) {
785 UInt64 length
= state
->fLength
;
787 curSeg
.fLength
+= length
;
788 state
->fIOVMAddr
= 0;
792 if (!state
->fIOVMAddr
)
794 if (kWalkClient
& op
)
796 if ((curSeg
.fIOVMAddr
+ curSeg
.fLength
- 1) > maxPhys
)
798 if (internalState
->fCursor
)
800 curSeg
.fIOVMAddr
= 0;
801 ret
= kIOReturnMessageTooLarge
;
804 else if (curSeg
.fIOVMAddr
<= maxPhys
)
806 UInt64 remain
, newLength
;
808 newLength
= (maxPhys
+ 1 - curSeg
.fIOVMAddr
);
809 DEBG("trunc %qx, %qx-> %qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
, newLength
);
810 remain
= curSeg
.fLength
- newLength
;
811 state
->fIOVMAddr
= newLength
+ curSeg
.fIOVMAddr
;
812 curSeg
.fLength
= newLength
;
813 state
->fLength
= remain
;
816 else if (gIOCopyMapper
)
818 DEBG("sparse switch %qx, %qx ", curSeg
.fIOVMAddr
, curSeg
.fLength
);
820 for (UInt checkRemapIndex
= 0; checkRemapIndex
< internalState
->fCopyPageCount
; checkRemapIndex
++)
822 if (trunc_page_64(curSeg
.fIOVMAddr
) == gIOCopyMapper
->mapAddr(
823 ptoa_64(internalState
->fCopyPageAlloc
+ checkRemapIndex
)))
825 curSeg
.fIOVMAddr
= ptoa_64(internalState
->fCopyPageAlloc
+ checkRemapIndex
) + (curSeg
.fIOVMAddr
& PAGE_MASK
);
829 DEBG("-> %qx, %qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
);
834 if (curSeg
.fLength
> fMaxSegmentSize
)
836 UInt64 remain
= curSeg
.fLength
- fMaxSegmentSize
;
838 state
->fIOVMAddr
= fMaxSegmentSize
+ curSeg
.fIOVMAddr
;
839 curSeg
.fLength
= fMaxSegmentSize
;
841 state
->fLength
= remain
;
845 if (internalState
->fCursor
846 && (0 != (fAlignMask
& curSeg
.fIOVMAddr
)))
848 curSeg
.fIOVMAddr
= 0;
849 ret
= kIOReturnNotAligned
;
853 if (offset
>= memLength
)
855 curSeg
.fLength
-= (offset
- memLength
);
857 state
->fIOVMAddr
= state
->fLength
= 0; // At end
862 if (state
->fIOVMAddr
) {
863 if ((segIndex
+ 1 == numSegments
))
866 ret
= (*outSegFunc
)(reference
, this, curSeg
, segmentsP
, segIndex
++);
867 curSeg
.fIOVMAddr
= 0;
868 if (kIOReturnSuccess
!= ret
)
873 if (curSeg
.fIOVMAddr
) {
874 ret
= (*outSegFunc
)(reference
, this, curSeg
, segmentsP
, segIndex
++);
877 if (kIOReturnSuccess
== ret
)
879 state
->fOffset
= offset
;
880 *offsetP
= offset
- internalState
->fPreparedOffset
;
881 *numSegmentsP
= segIndex
;
887 IODMACommand::clientOutputSegment(
888 void *reference
, IODMACommand
*target
,
889 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
891 IOReturn ret
= kIOReturnSuccess
;
893 if ((target
->fNumAddressBits
< 64)
894 && ((segment
.fIOVMAddr
+ segment
.fLength
- 1) >> target
->fNumAddressBits
))
896 DEBG("kIOReturnMessageTooLarge(fNumAddressBits) %qx, %qx\n", segment
.fIOVMAddr
, segment
.fLength
);
897 ret
= kIOReturnMessageTooLarge
;
900 if (!(*target
->fOutSeg
)(target
, segment
, vSegList
, outSegIndex
))
902 DEBG("kIOReturnMessageTooLarge(fOutSeg) %qx, %qx\n", segment
.fIOVMAddr
, segment
.fLength
);
903 ret
= kIOReturnMessageTooLarge
;
910 IODMACommand::OutputHost32(IODMACommand
*,
911 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
913 Segment32
*base
= (Segment32
*) vSegList
;
914 base
[outSegIndex
].fIOVMAddr
= (UInt32
) segment
.fIOVMAddr
;
915 base
[outSegIndex
].fLength
= (UInt32
) segment
.fLength
;
920 IODMACommand::OutputBig32(IODMACommand
*,
921 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
923 const UInt offAddr
= outSegIndex
* sizeof(Segment32
);
924 const UInt offLen
= offAddr
+ sizeof(UInt32
);
925 OSWriteBigInt32(vSegList
, offAddr
, (UInt32
) segment
.fIOVMAddr
);
926 OSWriteBigInt32(vSegList
, offLen
, (UInt32
) segment
.fLength
);
931 IODMACommand::OutputLittle32(IODMACommand
*,
932 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
934 const UInt offAddr
= outSegIndex
* sizeof(Segment32
);
935 const UInt offLen
= offAddr
+ sizeof(UInt32
);
936 OSWriteLittleInt32(vSegList
, offAddr
, (UInt32
) segment
.fIOVMAddr
);
937 OSWriteLittleInt32(vSegList
, offLen
, (UInt32
) segment
.fLength
);
942 IODMACommand::OutputHost64(IODMACommand
*,
943 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
945 Segment64
*base
= (Segment64
*) vSegList
;
946 base
[outSegIndex
] = segment
;
951 IODMACommand::OutputBig64(IODMACommand
*,
952 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
954 const UInt offAddr
= outSegIndex
* sizeof(Segment64
);
955 const UInt offLen
= offAddr
+ sizeof(UInt64
);
956 OSWriteBigInt64(vSegList
, offAddr
, (UInt64
) segment
.fIOVMAddr
);
957 OSWriteBigInt64(vSegList
, offLen
, (UInt64
) segment
.fLength
);
962 IODMACommand::OutputLittle64(IODMACommand
*,
963 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
965 const UInt offAddr
= outSegIndex
* sizeof(Segment64
);
966 const UInt offLen
= offAddr
+ sizeof(UInt64
);
967 OSWriteLittleInt64(vSegList
, offAddr
, (UInt64
) segment
.fIOVMAddr
);
968 OSWriteLittleInt64(vSegList
, offLen
, (UInt64
) segment
.fLength
);