2 * Copyright (c) 2005-2006 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@
29 #include <IOKit/assert.h>
31 #include <libkern/OSTypes.h>
32 #include <libkern/OSByteOrder.h>
34 #include <IOKit/IOReturn.h>
35 #include <IOKit/IOLib.h>
36 #include <IOKit/IODMACommand.h>
37 #include <IOKit/IOMapper.h>
38 #include <IOKit/IOMemoryDescriptor.h>
39 #include <IOKit/IOBufferMemoryDescriptor.h>
41 #include "IOKitKernelInternal.h"
42 #include "IOCopyMapper.h"
44 #define MAPTYPE(type) ((UInt) (type) & kTypeMask)
45 #define IS_MAPPED(type) (MAPTYPE(type) == kMapped)
46 #define IS_BYPASSED(type) (MAPTYPE(type) == kBypassed)
47 #define IS_NONCOHERENT(type) (MAPTYPE(type) == kNonCoherent)
50 static bool gIOEnableCopyMapper
= true;
54 kWalkSyncIn
= 0x01, // bounce -> md
55 kWalkSyncOut
= 0x02, // bounce <- md
56 kWalkSyncAlways
= 0x04,
57 kWalkPreflight
= 0x08,
58 kWalkDoubleBuffer
= 0x10,
65 #define fInternalState reserved
66 #define fState reserved->fState
67 #define fMDSummary reserved->fMDSummary
71 // no direction => OutIn
72 #define SHOULD_COPY_DIR(op, direction) \
73 ((kIODirectionNone == (direction)) \
74 || (kWalkSyncAlways & (op)) \
75 || (((kWalkSyncIn & (op)) ? kIODirectionIn : kIODirectionOut) \
79 #define SHOULD_COPY_DIR(state, direction) (true)
83 #define DEBG(fmt, args...) { kprintf(fmt, ## args); }
85 #define DEBG(fmt, args...) {}
89 /**************************** class IODMACommand ***************************/
92 #define super OSObject
93 OSDefineMetaClassAndStructors(IODMACommand
, IOCommand
);
95 OSMetaClassDefineReservedUsed(IODMACommand
, 0);
96 OSMetaClassDefineReservedUsed(IODMACommand
, 1);
97 OSMetaClassDefineReservedUnused(IODMACommand
, 2);
98 OSMetaClassDefineReservedUnused(IODMACommand
, 3);
99 OSMetaClassDefineReservedUnused(IODMACommand
, 4);
100 OSMetaClassDefineReservedUnused(IODMACommand
, 5);
101 OSMetaClassDefineReservedUnused(IODMACommand
, 6);
102 OSMetaClassDefineReservedUnused(IODMACommand
, 7);
103 OSMetaClassDefineReservedUnused(IODMACommand
, 8);
104 OSMetaClassDefineReservedUnused(IODMACommand
, 9);
105 OSMetaClassDefineReservedUnused(IODMACommand
, 10);
106 OSMetaClassDefineReservedUnused(IODMACommand
, 11);
107 OSMetaClassDefineReservedUnused(IODMACommand
, 12);
108 OSMetaClassDefineReservedUnused(IODMACommand
, 13);
109 OSMetaClassDefineReservedUnused(IODMACommand
, 14);
110 OSMetaClassDefineReservedUnused(IODMACommand
, 15);
113 IODMACommand::withSpecification(SegmentFunction outSegFunc
,
114 UInt8 numAddressBits
,
115 UInt64 maxSegmentSize
,
116 MappingOptions mappingOptions
,
117 UInt64 maxTransferSize
,
122 IODMACommand
* me
= new IODMACommand
;
124 if (me
&& !me
->initWithSpecification(outSegFunc
,
125 numAddressBits
, maxSegmentSize
,
126 mappingOptions
, maxTransferSize
,
127 alignment
, mapper
, refCon
))
137 IODMACommand::cloneCommand(void *refCon
)
139 return withSpecification(fOutSeg
, fNumAddressBits
, fMaxSegmentSize
,
140 fMappingOptions
, fMaxTransferSize
, fAlignMask
+ 1, fMapper
, refCon
);
143 #define kLastOutputFunction ((SegmentFunction) kLastOutputFunction)
146 IODMACommand::initWithSpecification(SegmentFunction outSegFunc
,
147 UInt8 numAddressBits
,
148 UInt64 maxSegmentSize
,
149 MappingOptions mappingOptions
,
150 UInt64 maxTransferSize
,
155 if (!super::init() || !outSegFunc
|| !numAddressBits
)
158 bool is32Bit
= (OutputHost32
== outSegFunc
|| OutputBig32
== outSegFunc
159 || OutputLittle32
== outSegFunc
);
164 else if (numAddressBits
> 32)
165 return false; // Wrong output function for bits
168 if (numAddressBits
&& (numAddressBits
< PAGE_SHIFT
))
172 maxSegmentSize
--; // Set Max segment to -1
173 if (!maxTransferSize
)
174 maxTransferSize
--; // Set Max transfer to -1
178 IOMapper::checkForSystemMapper();
179 mapper
= IOMapper::gSystem
;
184 fOutSeg
= outSegFunc
;
185 fNumAddressBits
= numAddressBits
;
186 fMaxSegmentSize
= maxSegmentSize
;
187 fMappingOptions
= mappingOptions
;
188 fMaxTransferSize
= maxTransferSize
;
191 fAlignMask
= alignment
- 1;
195 switch (MAPTYPE(mappingOptions
))
198 case kNonCoherent
: fMapper
= 0; break;
200 if (mapper
&& !mapper
->getBypassMask(&fBypassMask
))
207 reserved
= IONew(IODMACommandInternal
, 1);
210 bzero(reserved
, sizeof(IODMACommandInternal
));
212 fInternalState
->fIterateOnly
= (0 != (kIterateOnly
& mappingOptions
));
221 IODelete(reserved
, IODMACommandInternal
, 1);
227 IODMACommand::setMemoryDescriptor(const IOMemoryDescriptor
*mem
, bool autoPrepare
)
236 return kIOReturnSuccess
;
240 // As we are almost certainly being called from a work loop thread
241 // if fActive is true it is probably not a good time to potentially
242 // block. Just test for it and return an error
244 return kIOReturnBusy
;
245 clearMemoryDescriptor();
249 bzero(&fMDSummary
, sizeof(fMDSummary
));
250 IOReturn rtn
= mem
->dmaCommandOperation(
251 kIOMDGetCharacteristics
,
252 &fMDSummary
, sizeof(fMDSummary
));
256 ppnum_t highPage
= fMDSummary
.fHighestPage
? fMDSummary
.fHighestPage
: gIOLastPage
;
258 if ((kMapped
== MAPTYPE(fMappingOptions
))
260 && (!fNumAddressBits
|| (fNumAddressBits
>= 31)))
261 // assuming mapped space is 2G
262 fInternalState
->fCheckAddressing
= false;
264 fInternalState
->fCheckAddressing
= (fNumAddressBits
&& (highPage
>= (1UL << (fNumAddressBits
- PAGE_SHIFT
))));
273 return kIOReturnSuccess
;
277 IODMACommand::clearMemoryDescriptor(bool autoComplete
)
279 if (fActive
&& !autoComplete
)
280 return (kIOReturnNotReady
);
289 return (kIOReturnSuccess
);
292 const IOMemoryDescriptor
*
293 IODMACommand::getMemoryDescriptor() const
300 IODMACommand::segmentOp(
302 IODMACommand
*target
,
307 IOOptionBits op
= (IOOptionBits
) reference
;
308 addr64_t maxPhys
, address
;
309 addr64_t remapAddr
= 0;
313 IODMACommandInternal
* state
= target
->reserved
;
315 if (target
->fNumAddressBits
&& (target
->fNumAddressBits
< 64))
316 maxPhys
= (1ULL << target
->fNumAddressBits
);
321 address
= segment
.fIOVMAddr
;
322 length
= segment
.fLength
;
327 if (!state
->fMisaligned
)
329 state
->fMisaligned
|= (0 != (target
->fAlignMask
& address
));
330 if (state
->fMisaligned
) DEBG("misaligned %qx:%qx, %lx\n", address
, length
, target
->fAlignMask
);
333 if (state
->fMisaligned
&& (kWalkPreflight
& op
))
334 return (kIOReturnNotAligned
);
336 if (!state
->fDoubleBuffer
)
338 if ((address
+ length
- 1) <= maxPhys
)
342 else if (address
<= maxPhys
)
344 DEBG("tail %qx, %qx", address
, length
);
345 length
= (address
+ length
- maxPhys
- 1);
346 address
= maxPhys
+ 1;
347 DEBG("-> %qx, %qx\n", address
, length
);
352 return (kIOReturnSuccess
);
354 numPages
= atop_64(round_page_64(length
));
355 remapAddr
= state
->fCopyNext
;
357 if (kWalkPreflight
& op
)
359 state
->fCopyPageCount
+= numPages
;
363 if (kWalkPrepare
& op
)
365 for (IOItemCount idx
= 0; idx
< numPages
; idx
++)
366 gIOCopyMapper
->iovmInsert(atop_64(remapAddr
), idx
, atop_64(address
) + idx
);
368 if (state
->fDoubleBuffer
)
369 state
->fCopyNext
+= length
;
372 state
->fCopyNext
+= round_page(length
);
373 remapAddr
+= (address
& PAGE_MASK
);
376 if (SHOULD_COPY_DIR(op
, target
->fMDSummary
.fDirection
))
378 DEBG("cpv: 0x%qx %s 0x%qx, 0x%qx, 0x%02lx\n", remapAddr
,
379 (kWalkSyncIn
& op
) ? "->" : "<-",
380 address
, length
, op
);
381 if (kWalkSyncIn
& op
)
383 copypv(remapAddr
, address
, length
,
384 cppvPsnk
| cppvFsnk
| cppvPsrc
| cppvNoRefSrc
);
388 copypv(address
, remapAddr
, length
,
389 cppvPsnk
| cppvFsnk
| cppvPsrc
| cppvNoRefSrc
);
394 return kIOReturnSuccess
;
398 IODMACommand::walkAll(UInt8 op
)
400 IODMACommandInternal
* state
= fInternalState
;
402 IOReturn ret
= kIOReturnSuccess
;
406 if (gIOEnableCopyMapper
&& (kWalkPreflight
& op
))
408 state
->fCopyContig
= false;
409 state
->fMisaligned
= false;
410 state
->fDoubleBuffer
= false;
411 state
->fPrepared
= false;
412 state
->fCopyNext
= 0;
413 state
->fCopyPageAlloc
= 0;
414 state
->fCopyPageCount
= 0;
415 state
->fNextRemapIndex
= 0;
418 if (!(kWalkDoubleBuffer
& op
))
422 ret
= genIOVMSegments(segmentOp
, (void *) op
, &offset
, state
, &numSegments
);
425 op
&= ~kWalkPreflight
;
427 state
->fDoubleBuffer
= (state
->fMisaligned
|| (kWalkDoubleBuffer
& op
));
428 if (state
->fDoubleBuffer
)
429 state
->fCopyPageCount
= atop_64(round_page(state
->fPreparedLength
));
431 if (state
->fCopyPageCount
)
436 DEBG("preflight fCopyPageCount %d\n", state
->fCopyPageCount
);
438 mapper
= gIOCopyMapper
;
440 mapBase
= mapper
->iovmAlloc(state
->fCopyPageCount
);
443 state
->fCopyPageAlloc
= mapBase
;
444 if (state
->fCopyPageAlloc
&& state
->fDoubleBuffer
)
446 DEBG("contig copy map\n");
447 state
->fCopyContig
= true;
450 state
->fCopyNext
= ptoa_64(state
->fCopyPageAlloc
);
453 ret
= genIOVMSegments(segmentOp
, (void *) op
, &offset
, state
, &numSegments
);
454 state
->fPrepared
= true;
455 op
&= ~(kWalkSyncIn
| kWalkSyncOut
);
459 DEBG("alloc IOBMD\n");
460 state
->fCopyMD
= IOBufferMemoryDescriptor::withOptions(
461 fMDSummary
.fDirection
, state
->fPreparedLength
, page_size
);
465 ret
= kIOReturnSuccess
;
466 state
->fPrepared
= true;
470 DEBG("IODMACommand !iovmAlloc");
471 return (kIOReturnNoResources
);
477 if (gIOEnableCopyMapper
&& state
->fPrepared
&& ((kWalkSyncIn
| kWalkSyncOut
) & op
))
479 if (state
->fCopyPageCount
)
481 DEBG("sync fCopyPageCount %d\n", state
->fCopyPageCount
);
483 if (state
->fCopyPageAlloc
)
485 state
->fCopyNext
= ptoa_64(state
->fCopyPageAlloc
);
488 ret
= genIOVMSegments(segmentOp
, (void *) op
, &offset
, state
, &numSegments
);
490 else if (state
->fCopyMD
)
492 DEBG("sync IOBMD\n");
494 if (SHOULD_COPY_DIR(op
, fMDSummary
.fDirection
))
496 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
500 if (kWalkSyncIn
& op
)
501 bytes
= poMD
->writeBytes(state
->fPreparedOffset
,
502 state
->fCopyMD
->getBytesNoCopy(),
503 state
->fPreparedLength
);
505 bytes
= poMD
->readBytes(state
->fPreparedOffset
,
506 state
->fCopyMD
->getBytesNoCopy(),
507 state
->fPreparedLength
);
508 DEBG("fCopyMD %s %lx bytes\n", (kWalkSyncIn
& op
) ? "wrote" : "read", bytes
);
509 ret
= (bytes
== state
->fPreparedLength
) ? kIOReturnSuccess
: kIOReturnUnderrun
;
512 ret
= kIOReturnSuccess
;
517 if (kWalkComplete
& op
)
519 if (state
->fCopyPageAlloc
)
521 gIOCopyMapper
->iovmFree(state
->fCopyPageAlloc
, state
->fCopyPageCount
);
522 state
->fCopyPageAlloc
= 0;
523 state
->fCopyPageCount
= 0;
527 state
->fCopyMD
->release();
531 state
->fPrepared
= false;
537 IODMACommand::prepareWithSpecification(SegmentFunction outSegFunc
,
538 UInt8 numAddressBits
,
539 UInt64 maxSegmentSize
,
540 MappingOptions mappingOptions
,
541 UInt64 maxTransferSize
,
550 return kIOReturnNotPermitted
;
552 if (!outSegFunc
|| !numAddressBits
)
553 return kIOReturnBadArgument
;
555 bool is32Bit
= (OutputHost32
== outSegFunc
|| OutputBig32
== outSegFunc
556 || OutputLittle32
== outSegFunc
);
561 else if (numAddressBits
> 32)
562 return kIOReturnBadArgument
; // Wrong output function for bits
565 if (numAddressBits
&& (numAddressBits
< PAGE_SHIFT
))
566 return kIOReturnBadArgument
;
569 maxSegmentSize
--; // Set Max segment to -1
570 if (!maxTransferSize
)
571 maxTransferSize
--; // Set Max transfer to -1
575 IOMapper::checkForSystemMapper();
576 mapper
= IOMapper::gSystem
;
579 switch (MAPTYPE(mappingOptions
))
582 case kNonCoherent
: fMapper
= 0; break;
584 if (mapper
&& !mapper
->getBypassMask(&fBypassMask
))
585 return kIOReturnBadArgument
;
588 return kIOReturnBadArgument
;
593 fOutSeg
= outSegFunc
;
594 fNumAddressBits
= numAddressBits
;
595 fMaxSegmentSize
= maxSegmentSize
;
596 fMappingOptions
= mappingOptions
;
597 fMaxTransferSize
= maxTransferSize
;
600 fAlignMask
= alignment
- 1;
603 fInternalState
->fIterateOnly
= (0 != (kIterateOnly
& mappingOptions
));
605 return prepare(offset
, length
, flushCache
, synchronize
);
610 IODMACommand::prepare(UInt64 offset
, UInt64 length
, bool flushCache
, bool synchronize
)
612 IODMACommandInternal
* state
= fInternalState
;
613 IOReturn ret
= kIOReturnSuccess
;
614 MappingOptions mappingOptions
= fMappingOptions
;
617 length
= fMDSummary
.fLength
;
619 if (length
> fMaxTransferSize
)
620 return kIOReturnNoSpace
;
622 if (IS_NONCOHERENT(mappingOptions
) && flushCache
) {
623 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
625 poMD
->performOperation(kIOMemoryIncoherentIOStore
, 0, fMDSummary
.fLength
);
629 if ((state
->fPreparedOffset
!= offset
)
630 || (state
->fPreparedLength
!= length
))
631 ret
= kIOReturnNotReady
;
635 state
->fPreparedOffset
= offset
;
636 state
->fPreparedLength
= length
;
638 state
->fCopyContig
= false;
639 state
->fMisaligned
= false;
640 state
->fDoubleBuffer
= false;
641 state
->fPrepared
= false;
642 state
->fCopyNext
= 0;
643 state
->fCopyPageAlloc
= 0;
644 state
->fCopyPageCount
= 0;
645 state
->fNextRemapIndex
= 0;
648 state
->fCursor
= state
->fIterateOnly
649 || (!state
->fCheckAddressing
651 || ((fMDSummary
.fPageAlign
& (1 << 31)) && (0 == (fMDSummary
.fPageAlign
& fAlignMask
)))));
654 IOOptionBits op
= kWalkPrepare
| kWalkPreflight
;
659 if (kIOReturnSuccess
== ret
)
660 state
->fPrepared
= true;
666 IODMACommand::complete(bool invalidateCache
, bool synchronize
)
668 IODMACommandInternal
* state
= fInternalState
;
669 IOReturn ret
= kIOReturnSuccess
;
672 return kIOReturnNotReady
;
678 IOOptionBits op
= kWalkComplete
;
683 state
->fPrepared
= false;
685 if (IS_NONCOHERENT(fMappingOptions
) && invalidateCache
)
687 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
689 poMD
->performOperation(kIOMemoryIncoherentIOFlush
, 0, fMDSummary
.fLength
);
697 IODMACommand::synchronize(IOOptionBits options
)
699 IODMACommandInternal
* state
= fInternalState
;
700 IOReturn ret
= kIOReturnSuccess
;
703 if (kIODirectionOutIn
== (kIODirectionOutIn
& options
))
704 return kIOReturnBadArgument
;
707 return kIOReturnNotReady
;
710 if (kForceDoubleBuffer
& options
)
712 if (state
->fDoubleBuffer
)
713 return kIOReturnSuccess
;
715 state
->fCursor
= false;
717 ret
= walkAll(kWalkComplete
);
719 op
|= kWalkPrepare
| kWalkPreflight
| kWalkDoubleBuffer
;
721 else if (state
->fCursor
)
722 return kIOReturnSuccess
;
724 if (kIODirectionIn
& options
)
725 op
|= kWalkSyncIn
| kWalkSyncAlways
;
726 else if (kIODirectionOut
& options
)
727 op
|= kWalkSyncOut
| kWalkSyncAlways
;
734 struct IODMACommandTransferContext
743 kIODMACommandTransferOpReadBytes
= 1,
744 kIODMACommandTransferOpWriteBytes
= 2
748 IODMACommand::transferSegment(void *reference
,
749 IODMACommand
*target
,
754 IODMACommandTransferContext
* context
= (IODMACommandTransferContext
*) segments
;
755 UInt64 length
= min(segment
.fLength
, context
->remaining
);
756 addr64_t ioAddr
= segment
.fIOVMAddr
;
757 addr64_t cpuAddr
= ioAddr
;
759 context
->remaining
-= length
;
763 UInt64 copyLen
= length
;
764 if ((kMapped
== MAPTYPE(target
->fMappingOptions
))
767 cpuAddr
= target
->fMapper
->mapAddr(ioAddr
);
768 copyLen
= min(copyLen
, page_size
- (ioAddr
& (page_size
- 1)));
774 case kIODMACommandTransferOpReadBytes
:
775 copypv(cpuAddr
, context
->bufferOffset
+ (addr64_t
) context
->buffer
, copyLen
,
776 cppvPsrc
| cppvNoRefSrc
| cppvFsnk
| cppvKmap
);
778 case kIODMACommandTransferOpWriteBytes
:
779 copypv(context
->bufferOffset
+ (addr64_t
) context
->buffer
, cpuAddr
, copyLen
,
780 cppvPsnk
| cppvFsnk
| cppvNoRefSrc
| cppvNoModSnk
| cppvKmap
);
784 context
->bufferOffset
+= copyLen
;
787 return (context
->remaining
? kIOReturnSuccess
: kIOReturnOverrun
);
791 IODMACommand::transfer(IOOptionBits transferOp
, UInt64 offset
, void * buffer
, UInt64 length
)
793 IODMACommandInternal
* state
= fInternalState
;
794 IODMACommandTransferContext context
;
795 UInt32 numSegments
= 0-1;
800 if (offset
>= state
->fPreparedLength
)
802 length
= min(length
, state
->fPreparedLength
- offset
);
804 context
.buffer
= buffer
;
805 context
.bufferOffset
= 0;
806 context
.remaining
= length
;
807 context
.op
= transferOp
;
808 (void) genIOVMSegments(transferSegment
, (void *) kWalkClient
, &offset
, &context
, &numSegments
);
810 return (length
- context
.remaining
);
814 IODMACommand::readBytes(UInt64 offset
, void *bytes
, UInt64 length
)
816 return (transfer(kIODMACommandTransferOpReadBytes
, offset
, bytes
, length
));
820 IODMACommand::writeBytes(UInt64 offset
, const void *bytes
, UInt64 length
)
822 return (transfer(kIODMACommandTransferOpWriteBytes
, offset
, const_cast<void *>(bytes
), length
));
826 IODMACommand::genIOVMSegments(UInt64
*offsetP
,
828 UInt32
*numSegmentsP
)
830 return (genIOVMSegments(clientOutputSegment
, (void *) kWalkClient
, offsetP
, segmentsP
, numSegmentsP
));
834 IODMACommand::genIOVMSegments(InternalSegmentFunction outSegFunc
,
838 UInt32
*numSegmentsP
)
840 IOOptionBits op
= (IOOptionBits
) reference
;
841 IODMACommandInternal
* internalState
= fInternalState
;
842 IOOptionBits mdOp
= kIOMDWalkSegments
;
843 IOReturn ret
= kIOReturnSuccess
;
845 if (!(kWalkComplete
& op
) && !fActive
)
846 return kIOReturnNotReady
;
848 if (!offsetP
|| !segmentsP
|| !numSegmentsP
|| !*numSegmentsP
)
849 return kIOReturnBadArgument
;
851 IOMDDMAWalkSegmentArgs
*state
=
852 (IOMDDMAWalkSegmentArgs
*) fState
;
854 UInt64 offset
= *offsetP
+ internalState
->fPreparedOffset
;
855 UInt64 memLength
= internalState
->fPreparedOffset
+ internalState
->fPreparedLength
;
857 if (offset
>= memLength
)
858 return kIOReturnOverrun
;
860 if ((offset
== internalState
->fPreparedOffset
) || (offset
!= state
->fOffset
)) {
862 state
->fIOVMAddr
= 0;
863 internalState
->fNextRemapIndex
= 0;
864 state
->fMapped
= (IS_MAPPED(fMappingOptions
) && fMapper
);
865 mdOp
= kIOMDFirstSegment
;
868 UInt64 bypassMask
= fBypassMask
;
870 UInt32 numSegments
= *numSegmentsP
;
871 Segment64 curSeg
= { 0, 0 };
874 if (fNumAddressBits
&& (fNumAddressBits
< 64))
875 maxPhys
= (1ULL << fNumAddressBits
);
880 while ((state
->fIOVMAddr
) || state
->fOffset
< memLength
)
882 if (!state
->fIOVMAddr
) {
886 state
->fOffset
= offset
;
887 state
->fLength
= memLength
- offset
;
889 if (internalState
->fCopyContig
&& (kWalkClient
& op
))
891 state
->fIOVMAddr
= ptoa_64(internalState
->fCopyPageAlloc
)
892 + offset
- internalState
->fPreparedOffset
;
893 rtn
= kIOReturnSuccess
;
897 const IOMemoryDescriptor
* memory
=
898 internalState
->fCopyMD
? internalState
->fCopyMD
: fMemory
;
899 rtn
= memory
->dmaCommandOperation(mdOp
, fState
, sizeof(fState
));
900 mdOp
= kIOMDWalkSegments
;
903 if (rtn
== kIOReturnSuccess
) {
904 assert(state
->fIOVMAddr
);
905 assert(state
->fLength
);
907 else if (rtn
== kIOReturnOverrun
)
908 state
->fIOVMAddr
= state
->fLength
= 0; // At end
913 if (!curSeg
.fIOVMAddr
) {
914 UInt64 length
= state
->fLength
;
917 curSeg
.fIOVMAddr
= state
->fIOVMAddr
| bypassMask
;
918 curSeg
.fLength
= length
;
919 state
->fIOVMAddr
= 0;
921 else if ((curSeg
.fIOVMAddr
+ curSeg
.fLength
== state
->fIOVMAddr
)) {
922 UInt64 length
= state
->fLength
;
924 curSeg
.fLength
+= length
;
925 state
->fIOVMAddr
= 0;
929 if (!state
->fIOVMAddr
)
931 if (kWalkClient
& op
)
933 if ((curSeg
.fIOVMAddr
+ curSeg
.fLength
- 1) > maxPhys
)
935 if (internalState
->fCursor
)
937 curSeg
.fIOVMAddr
= 0;
938 ret
= kIOReturnMessageTooLarge
;
941 else if (curSeg
.fIOVMAddr
<= maxPhys
)
943 UInt64 remain
, newLength
;
945 newLength
= (maxPhys
+ 1 - curSeg
.fIOVMAddr
);
946 DEBG("trunc %qx, %qx-> %qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
, newLength
);
947 remain
= curSeg
.fLength
- newLength
;
948 state
->fIOVMAddr
= newLength
+ curSeg
.fIOVMAddr
;
949 curSeg
.fLength
= newLength
;
950 state
->fLength
= remain
;
953 else if (gIOCopyMapper
)
955 DEBG("sparse switch %qx, %qx ", curSeg
.fIOVMAddr
, curSeg
.fLength
);
956 if (trunc_page_64(curSeg
.fIOVMAddr
) == gIOCopyMapper
->mapAddr(
957 ptoa_64(internalState
->fCopyPageAlloc
+ internalState
->fNextRemapIndex
)))
960 curSeg
.fIOVMAddr
= ptoa_64(internalState
->fCopyPageAlloc
+ internalState
->fNextRemapIndex
)
961 + (curSeg
.fIOVMAddr
& PAGE_MASK
);
962 internalState
->fNextRemapIndex
+= atop_64(round_page(curSeg
.fLength
));
964 else for (UInt checkRemapIndex
= 0; checkRemapIndex
< internalState
->fCopyPageCount
; checkRemapIndex
++)
966 if (trunc_page_64(curSeg
.fIOVMAddr
) == gIOCopyMapper
->mapAddr(
967 ptoa_64(internalState
->fCopyPageAlloc
+ checkRemapIndex
)))
969 curSeg
.fIOVMAddr
= ptoa_64(internalState
->fCopyPageAlloc
+ checkRemapIndex
)
970 + (curSeg
.fIOVMAddr
& PAGE_MASK
);
971 internalState
->fNextRemapIndex
= checkRemapIndex
+ atop_64(round_page(curSeg
.fLength
));
975 DEBG("-> %qx, %qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
);
980 if (curSeg
.fLength
> fMaxSegmentSize
)
982 UInt64 remain
= curSeg
.fLength
- fMaxSegmentSize
;
984 state
->fIOVMAddr
= fMaxSegmentSize
+ curSeg
.fIOVMAddr
;
985 curSeg
.fLength
= fMaxSegmentSize
;
987 state
->fLength
= remain
;
991 if (internalState
->fCursor
992 && (0 != (fAlignMask
& curSeg
.fIOVMAddr
)))
994 curSeg
.fIOVMAddr
= 0;
995 ret
= kIOReturnNotAligned
;
999 if (offset
>= memLength
)
1001 curSeg
.fLength
-= (offset
- memLength
);
1003 state
->fIOVMAddr
= state
->fLength
= 0; // At end
1008 if (state
->fIOVMAddr
) {
1009 if ((segIndex
+ 1 == numSegments
))
1012 ret
= (*outSegFunc
)(reference
, this, curSeg
, segmentsP
, segIndex
++);
1013 curSeg
.fIOVMAddr
= 0;
1014 if (kIOReturnSuccess
!= ret
)
1019 if (curSeg
.fIOVMAddr
) {
1020 ret
= (*outSegFunc
)(reference
, this, curSeg
, segmentsP
, segIndex
++);
1023 if (kIOReturnSuccess
== ret
)
1025 state
->fOffset
= offset
;
1026 *offsetP
= offset
- internalState
->fPreparedOffset
;
1027 *numSegmentsP
= segIndex
;
1033 IODMACommand::clientOutputSegment(
1034 void *reference
, IODMACommand
*target
,
1035 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1037 IOReturn ret
= kIOReturnSuccess
;
1039 if ((target
->fNumAddressBits
< 64)
1040 && ((segment
.fIOVMAddr
+ segment
.fLength
- 1) >> target
->fNumAddressBits
))
1042 DEBG("kIOReturnMessageTooLarge(fNumAddressBits) %qx, %qx\n", segment
.fIOVMAddr
, segment
.fLength
);
1043 ret
= kIOReturnMessageTooLarge
;
1046 if (!(*target
->fOutSeg
)(target
, segment
, vSegList
, outSegIndex
))
1048 DEBG("kIOReturnMessageTooLarge(fOutSeg) %qx, %qx\n", segment
.fIOVMAddr
, segment
.fLength
);
1049 ret
= kIOReturnMessageTooLarge
;
1056 IODMACommand::OutputHost32(IODMACommand
*,
1057 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1059 Segment32
*base
= (Segment32
*) vSegList
;
1060 base
[outSegIndex
].fIOVMAddr
= (UInt32
) segment
.fIOVMAddr
;
1061 base
[outSegIndex
].fLength
= (UInt32
) segment
.fLength
;
1066 IODMACommand::OutputBig32(IODMACommand
*,
1067 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1069 const UInt offAddr
= outSegIndex
* sizeof(Segment32
);
1070 const UInt offLen
= offAddr
+ sizeof(UInt32
);
1071 OSWriteBigInt32(vSegList
, offAddr
, (UInt32
) segment
.fIOVMAddr
);
1072 OSWriteBigInt32(vSegList
, offLen
, (UInt32
) segment
.fLength
);
1077 IODMACommand::OutputLittle32(IODMACommand
*,
1078 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1080 const UInt offAddr
= outSegIndex
* sizeof(Segment32
);
1081 const UInt offLen
= offAddr
+ sizeof(UInt32
);
1082 OSWriteLittleInt32(vSegList
, offAddr
, (UInt32
) segment
.fIOVMAddr
);
1083 OSWriteLittleInt32(vSegList
, offLen
, (UInt32
) segment
.fLength
);
1088 IODMACommand::OutputHost64(IODMACommand
*,
1089 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1091 Segment64
*base
= (Segment64
*) vSegList
;
1092 base
[outSegIndex
] = segment
;
1097 IODMACommand::OutputBig64(IODMACommand
*,
1098 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1100 const UInt offAddr
= outSegIndex
* sizeof(Segment64
);
1101 const UInt offLen
= offAddr
+ sizeof(UInt64
);
1102 OSWriteBigInt64(vSegList
, offAddr
, (UInt64
) segment
.fIOVMAddr
);
1103 OSWriteBigInt64(vSegList
, offLen
, (UInt64
) segment
.fLength
);
1108 IODMACommand::OutputLittle64(IODMACommand
*,
1109 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1111 const UInt offAddr
= outSegIndex
* sizeof(Segment64
);
1112 const UInt offLen
= offAddr
+ sizeof(UInt64
);
1113 OSWriteLittleInt64(vSegList
, offAddr
, (UInt64
) segment
.fIOVMAddr
);
1114 OSWriteLittleInt64(vSegList
, offLen
, (UInt64
) segment
.fLength
);