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)
51 kWalkSyncIn
= 0x01, // bounce -> md
52 kWalkSyncOut
= 0x02, // bounce <- md
53 kWalkSyncAlways
= 0x04,
54 kWalkPreflight
= 0x08,
55 kWalkDoubleBuffer
= 0x10,
62 #define fInternalState reserved
63 #define fState reserved->fState
64 #define fMDSummary reserved->fMDSummary
68 // no direction => OutIn
69 #define SHOULD_COPY_DIR(op, direction) \
70 ((kIODirectionNone == (direction)) \
71 || (kWalkSyncAlways & (op)) \
72 || (((kWalkSyncIn & (op)) ? kIODirectionIn : kIODirectionOut) \
76 #define SHOULD_COPY_DIR(state, direction) (true)
80 #define DEBG(fmt, args...) { kprintf(fmt, ## args); }
82 #define DEBG(fmt, args...) {}
86 /**************************** class IODMACommand ***************************/
89 #define super OSObject
90 OSDefineMetaClassAndStructors(IODMACommand
, IOCommand
);
92 OSMetaClassDefineReservedUsed(IODMACommand
, 0);
93 OSMetaClassDefineReservedUsed(IODMACommand
, 1);
94 OSMetaClassDefineReservedUsed(IODMACommand
, 2);
95 OSMetaClassDefineReservedUnused(IODMACommand
, 3);
96 OSMetaClassDefineReservedUnused(IODMACommand
, 4);
97 OSMetaClassDefineReservedUnused(IODMACommand
, 5);
98 OSMetaClassDefineReservedUnused(IODMACommand
, 6);
99 OSMetaClassDefineReservedUnused(IODMACommand
, 7);
100 OSMetaClassDefineReservedUnused(IODMACommand
, 8);
101 OSMetaClassDefineReservedUnused(IODMACommand
, 9);
102 OSMetaClassDefineReservedUnused(IODMACommand
, 10);
103 OSMetaClassDefineReservedUnused(IODMACommand
, 11);
104 OSMetaClassDefineReservedUnused(IODMACommand
, 12);
105 OSMetaClassDefineReservedUnused(IODMACommand
, 13);
106 OSMetaClassDefineReservedUnused(IODMACommand
, 14);
107 OSMetaClassDefineReservedUnused(IODMACommand
, 15);
110 IODMACommand::withSpecification(SegmentFunction outSegFunc
,
111 UInt8 numAddressBits
,
112 UInt64 maxSegmentSize
,
113 MappingOptions mappingOptions
,
114 UInt64 maxTransferSize
,
119 IODMACommand
* me
= new IODMACommand
;
121 if (me
&& !me
->initWithSpecification(outSegFunc
,
122 numAddressBits
, maxSegmentSize
,
123 mappingOptions
, maxTransferSize
,
124 alignment
, mapper
, refCon
))
134 IODMACommand::cloneCommand(void *refCon
)
136 return withSpecification(fOutSeg
, fNumAddressBits
, fMaxSegmentSize
,
137 fMappingOptions
, fMaxTransferSize
, fAlignMask
+ 1, fMapper
, refCon
);
140 #define kLastOutputFunction ((SegmentFunction) kLastOutputFunction)
143 IODMACommand::initWithSpecification(SegmentFunction outSegFunc
,
144 UInt8 numAddressBits
,
145 UInt64 maxSegmentSize
,
146 MappingOptions mappingOptions
,
147 UInt64 maxTransferSize
,
152 if (!super::init() || !outSegFunc
|| !numAddressBits
)
155 bool is32Bit
= (OutputHost32
== outSegFunc
|| OutputBig32
== outSegFunc
156 || OutputLittle32
== outSegFunc
);
161 else if (numAddressBits
> 32)
162 return false; // Wrong output function for bits
165 if (numAddressBits
&& (numAddressBits
< PAGE_SHIFT
))
169 maxSegmentSize
--; // Set Max segment to -1
170 if (!maxTransferSize
)
171 maxTransferSize
--; // Set Max transfer to -1
175 IOMapper::checkForSystemMapper();
176 mapper
= IOMapper::gSystem
;
181 fOutSeg
= outSegFunc
;
182 fNumAddressBits
= numAddressBits
;
183 fMaxSegmentSize
= maxSegmentSize
;
184 fMappingOptions
= mappingOptions
;
185 fMaxTransferSize
= maxTransferSize
;
188 fAlignMask
= alignment
- 1;
192 switch (MAPTYPE(mappingOptions
))
195 case kNonCoherent
: fMapper
= 0; break;
197 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);
230 IODMACommand::setMemoryDescriptor(const IOMemoryDescriptor
*mem
, bool autoPrepare
)
239 return kIOReturnSuccess
;
243 // As we are almost certainly being called from a work loop thread
244 // if fActive is true it is probably not a good time to potentially
245 // block. Just test for it and return an error
247 return kIOReturnBusy
;
248 clearMemoryDescriptor();
252 bzero(&fMDSummary
, sizeof(fMDSummary
));
253 IOReturn rtn
= mem
->dmaCommandOperation(
254 kIOMDGetCharacteristics
,
255 &fMDSummary
, sizeof(fMDSummary
));
259 ppnum_t highPage
= fMDSummary
.fHighestPage
? fMDSummary
.fHighestPage
: gIOLastPage
;
261 if ((kMapped
== MAPTYPE(fMappingOptions
))
263 && (!fNumAddressBits
|| (fNumAddressBits
>= 31)))
264 // assuming mapped space is 2G
265 fInternalState
->fCheckAddressing
= false;
267 fInternalState
->fCheckAddressing
= (fNumAddressBits
&& (highPage
>= (1UL << (fNumAddressBits
- PAGE_SHIFT
))));
269 fInternalState
->fNewMD
= true;
273 mem
->dmaCommandOperation(kIOMDSetDMAActive
, this, 0);
278 return kIOReturnSuccess
;
282 IODMACommand::clearMemoryDescriptor(bool autoComplete
)
284 if (fActive
&& !autoComplete
)
285 return (kIOReturnNotReady
);
290 fMemory
->dmaCommandOperation(kIOMDSetDMAInactive
, this, 0);
295 return (kIOReturnSuccess
);
298 const IOMemoryDescriptor
*
299 IODMACommand::getMemoryDescriptor() const
306 IODMACommand::segmentOp(
308 IODMACommand
*target
,
313 IOOptionBits op
= (uintptr_t) reference
;
314 addr64_t maxPhys
, address
;
315 addr64_t remapAddr
= 0;
319 IODMACommandInternal
* state
= target
->reserved
;
321 if (target
->fNumAddressBits
&& (target
->fNumAddressBits
< 64) && !state
->fLocalMapper
)
322 maxPhys
= (1ULL << target
->fNumAddressBits
);
327 address
= segment
.fIOVMAddr
;
328 length
= segment
.fLength
;
333 if (!state
->fMisaligned
)
335 state
->fMisaligned
|= (0 != (state
->fSourceAlignMask
& address
));
336 if (state
->fMisaligned
) DEBG("misaligned %qx:%qx, %lx\n", address
, length
, state
->fSourceAlignMask
);
339 if (state
->fMisaligned
&& (kWalkPreflight
& op
))
340 return (kIOReturnNotAligned
);
342 if (!state
->fDoubleBuffer
)
344 if ((address
+ length
- 1) <= maxPhys
)
348 else if (address
<= maxPhys
)
350 DEBG("tail %qx, %qx", address
, length
);
351 length
= (address
+ length
- maxPhys
- 1);
352 address
= maxPhys
+ 1;
353 DEBG("-> %qx, %qx\n", address
, length
);
358 return (kIOReturnSuccess
);
360 numPages
= atop_64(round_page_64(length
));
361 remapAddr
= state
->fCopyNext
;
363 if (kWalkPreflight
& op
)
365 state
->fCopyPageCount
+= numPages
;
369 if (kWalkPrepare
& op
)
371 for (IOItemCount idx
= 0; idx
< numPages
; idx
++)
372 gIOCopyMapper
->iovmInsert(atop_64(remapAddr
), idx
, atop_64(address
) + idx
);
374 if (state
->fDoubleBuffer
)
375 state
->fCopyNext
+= length
;
378 state
->fCopyNext
+= round_page(length
);
379 remapAddr
+= (address
& PAGE_MASK
);
382 if (SHOULD_COPY_DIR(op
, target
->fMDSummary
.fDirection
))
384 DEBG("cpv: 0x%qx %s 0x%qx, 0x%qx, 0x%02lx\n", remapAddr
,
385 (kWalkSyncIn
& op
) ? "->" : "<-",
386 address
, length
, op
);
387 if (kWalkSyncIn
& op
)
389 copypv(remapAddr
, address
, length
,
390 cppvPsnk
| cppvFsnk
| cppvPsrc
| cppvNoRefSrc
);
394 copypv(address
, remapAddr
, length
,
395 cppvPsnk
| cppvFsnk
| cppvPsrc
| cppvNoRefSrc
);
400 return kIOReturnSuccess
;
404 IODMACommand::walkAll(UInt8 op
)
406 IODMACommandInternal
* state
= fInternalState
;
408 IOReturn ret
= kIOReturnSuccess
;
412 if (kWalkPreflight
& op
)
414 state
->fMapContig
= false;
415 state
->fMisaligned
= false;
416 state
->fDoubleBuffer
= false;
417 state
->fPrepared
= false;
418 state
->fCopyNext
= 0;
419 state
->fCopyMapperPageAlloc
= 0;
420 state
->fLocalMapperPageAlloc
= 0;
421 state
->fCopyPageCount
= 0;
422 state
->fNextRemapIndex
= 0;
425 if (!(kWalkDoubleBuffer
& op
))
429 ret
= genIOVMSegments(op
, segmentOp
, (void *) op
, &offset
, state
, &numSegments
);
432 op
&= ~kWalkPreflight
;
434 state
->fDoubleBuffer
= (state
->fMisaligned
|| (kWalkDoubleBuffer
& op
));
435 if (state
->fDoubleBuffer
)
436 state
->fCopyPageCount
= atop_64(round_page(state
->fPreparedLength
));
438 if (state
->fCopyPageCount
)
443 DEBG("preflight fCopyPageCount %d\n", state
->fCopyPageCount
);
445 mapper
= gIOCopyMapper
;
447 mapBase
= mapper
->iovmAlloc(state
->fCopyPageCount
);
450 state
->fCopyMapperPageAlloc
= mapBase
;
451 if (state
->fCopyMapperPageAlloc
&& state
->fDoubleBuffer
)
453 DEBG("contig copy map\n");
454 state
->fMapContig
= true;
457 state
->fCopyNext
= ptoa_64(state
->fCopyMapperPageAlloc
);
460 ret
= genIOVMSegments(op
, segmentOp
, (void *) op
, &offset
, state
, &numSegments
);
461 state
->fPrepared
= true;
462 op
&= ~(kWalkSyncIn
| kWalkSyncOut
);
466 DEBG("alloc IOBMD\n");
467 state
->fCopyMD
= IOBufferMemoryDescriptor::withOptions(
468 fMDSummary
.fDirection
, state
->fPreparedLength
, state
->fSourceAlignMask
);
472 ret
= kIOReturnSuccess
;
473 state
->fPrepared
= true;
477 DEBG("IODMACommand !iovmAlloc");
478 return (kIOReturnNoResources
);
483 if (state
->fLocalMapper
)
485 state
->fLocalMapperPageCount
= atop_64(round_page(state
->fPreparedLength
));
486 state
->fLocalMapperPageAlloc
= fMapper
->iovmAllocDMACommand(this, state
->fLocalMapperPageCount
);
487 state
->fMapContig
= true;
491 if (state
->fPrepared
&& ((kWalkSyncIn
| kWalkSyncOut
) & op
))
493 if (state
->fCopyPageCount
)
495 DEBG("sync fCopyPageCount %d\n", state
->fCopyPageCount
);
497 if (state
->fCopyMapperPageAlloc
)
499 state
->fCopyNext
= ptoa_64(state
->fCopyMapperPageAlloc
);
502 ret
= genIOVMSegments(op
, segmentOp
, (void *) op
, &offset
, state
, &numSegments
);
504 else if (state
->fCopyMD
)
506 DEBG("sync IOBMD\n");
508 if (SHOULD_COPY_DIR(op
, fMDSummary
.fDirection
))
510 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
514 if (kWalkSyncIn
& op
)
515 bytes
= poMD
->writeBytes(state
->fPreparedOffset
,
516 state
->fCopyMD
->getBytesNoCopy(),
517 state
->fPreparedLength
);
519 bytes
= poMD
->readBytes(state
->fPreparedOffset
,
520 state
->fCopyMD
->getBytesNoCopy(),
521 state
->fPreparedLength
);
522 DEBG("fCopyMD %s %lx bytes\n", (kWalkSyncIn
& op
) ? "wrote" : "read", bytes
);
523 ret
= (bytes
== state
->fPreparedLength
) ? kIOReturnSuccess
: kIOReturnUnderrun
;
526 ret
= kIOReturnSuccess
;
531 if (kWalkComplete
& op
)
533 if (state
->fLocalMapperPageAlloc
)
535 fMapper
->iovmFreeDMACommand(this, state
->fLocalMapperPageAlloc
, state
->fLocalMapperPageCount
);
536 state
->fLocalMapperPageAlloc
= 0;
537 state
->fLocalMapperPageCount
= 0;
539 if (state
->fCopyMapperPageAlloc
)
541 gIOCopyMapper
->iovmFree(state
->fCopyMapperPageAlloc
, state
->fCopyPageCount
);
542 state
->fCopyMapperPageAlloc
= 0;
543 state
->fCopyPageCount
= 0;
547 state
->fCopyMD
->release();
551 state
->fPrepared
= false;
557 IODMACommand::getNumAddressBits(void)
559 return (fNumAddressBits
);
563 IODMACommand::getAlignment(void)
565 return (fAlignMask
+ 1);
569 IODMACommand::prepareWithSpecification(SegmentFunction outSegFunc
,
570 UInt8 numAddressBits
,
571 UInt64 maxSegmentSize
,
572 MappingOptions mappingOptions
,
573 UInt64 maxTransferSize
,
582 return kIOReturnNotPermitted
;
584 if (!outSegFunc
|| !numAddressBits
)
585 return kIOReturnBadArgument
;
587 bool is32Bit
= (OutputHost32
== outSegFunc
|| OutputBig32
== outSegFunc
588 || OutputLittle32
== outSegFunc
);
593 else if (numAddressBits
> 32)
594 return kIOReturnBadArgument
; // Wrong output function for bits
597 if (numAddressBits
&& (numAddressBits
< PAGE_SHIFT
))
598 return kIOReturnBadArgument
;
601 maxSegmentSize
--; // Set Max segment to -1
602 if (!maxTransferSize
)
603 maxTransferSize
--; // Set Max transfer to -1
607 IOMapper::checkForSystemMapper();
608 mapper
= IOMapper::gSystem
;
611 switch (MAPTYPE(mappingOptions
))
614 case kNonCoherent
: fMapper
= 0; break;
616 if (mapper
&& !mapper
->getBypassMask(&fBypassMask
))
617 return kIOReturnBadArgument
;
620 return kIOReturnBadArgument
;
625 fOutSeg
= outSegFunc
;
626 fNumAddressBits
= numAddressBits
;
627 fMaxSegmentSize
= maxSegmentSize
;
628 fMappingOptions
= mappingOptions
;
629 fMaxTransferSize
= maxTransferSize
;
632 fAlignMask
= alignment
- 1;
633 if (mapper
!= fMapper
)
640 fInternalState
->fIterateOnly
= (0 != (kIterateOnly
& mappingOptions
));
642 return prepare(offset
, length
, flushCache
, synchronize
);
647 IODMACommand::prepare(UInt64 offset
, UInt64 length
, bool flushCache
, bool synchronize
)
649 IODMACommandInternal
* state
= fInternalState
;
650 IOReturn ret
= kIOReturnSuccess
;
651 MappingOptions mappingOptions
= fMappingOptions
;
654 length
= fMDSummary
.fLength
;
656 if (length
> fMaxTransferSize
)
657 return kIOReturnNoSpace
;
659 if (IS_NONCOHERENT(mappingOptions
) && flushCache
) {
660 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
662 poMD
->performOperation(kIOMemoryIncoherentIOStore
, offset
, length
);
666 if ((state
->fPreparedOffset
!= offset
)
667 || (state
->fPreparedLength
!= length
))
668 ret
= kIOReturnNotReady
;
672 state
->fPreparedOffset
= offset
;
673 state
->fPreparedLength
= length
;
675 state
->fMapContig
= false;
676 state
->fMisaligned
= false;
677 state
->fDoubleBuffer
= false;
678 state
->fPrepared
= false;
679 state
->fCopyNext
= 0;
680 state
->fCopyMapperPageAlloc
= 0;
681 state
->fCopyPageCount
= 0;
682 state
->fNextRemapIndex
= 0;
684 state
->fLocalMapperPageAlloc
= 0;
685 state
->fLocalMapperPageCount
= 0;
687 state
->fLocalMapper
= (fMapper
&& (fMapper
!= IOMapper::gSystem
));
689 state
->fSourceAlignMask
= fAlignMask
;
690 if (state
->fLocalMapper
)
691 state
->fSourceAlignMask
&= page_mask
;
693 state
->fCursor
= state
->fIterateOnly
694 || (!state
->fCheckAddressing
695 && !state
->fLocalMapper
696 && (!state
->fSourceAlignMask
697 || ((fMDSummary
.fPageAlign
& (1 << 31)) && (0 == (fMDSummary
.fPageAlign
& state
->fSourceAlignMask
)))));
701 IOOptionBits op
= kWalkPrepare
| kWalkPreflight
;
706 if (kIOReturnSuccess
== ret
)
707 state
->fPrepared
= true;
713 IODMACommand::complete(bool invalidateCache
, bool synchronize
)
715 IODMACommandInternal
* state
= fInternalState
;
716 IOReturn ret
= kIOReturnSuccess
;
719 return kIOReturnNotReady
;
725 IOOptionBits op
= kWalkComplete
;
730 state
->fPrepared
= false;
732 if (IS_NONCOHERENT(fMappingOptions
) && invalidateCache
)
734 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
736 poMD
->performOperation(kIOMemoryIncoherentIOFlush
, state
->fPreparedOffset
, state
->fPreparedLength
);
744 IODMACommand::getPreparedOffsetAndLength(UInt64
* offset
, UInt64
* length
)
746 IODMACommandInternal
* state
= fInternalState
;
748 return (kIOReturnNotReady
);
751 *offset
= state
->fPreparedOffset
;
753 *length
= state
->fPreparedLength
;
755 return (kIOReturnSuccess
);
759 IODMACommand::synchronize(IOOptionBits options
)
761 IODMACommandInternal
* state
= fInternalState
;
762 IOReturn ret
= kIOReturnSuccess
;
765 if (kIODirectionOutIn
== (kIODirectionOutIn
& options
))
766 return kIOReturnBadArgument
;
769 return kIOReturnNotReady
;
772 if (kForceDoubleBuffer
& options
)
774 if (state
->fDoubleBuffer
)
775 return kIOReturnSuccess
;
777 state
->fCursor
= false;
779 ret
= walkAll(kWalkComplete
);
781 op
|= kWalkPrepare
| kWalkPreflight
| kWalkDoubleBuffer
;
783 else if (state
->fCursor
)
784 return kIOReturnSuccess
;
786 if (kIODirectionIn
& options
)
787 op
|= kWalkSyncIn
| kWalkSyncAlways
;
788 else if (kIODirectionOut
& options
)
789 op
|= kWalkSyncOut
| kWalkSyncAlways
;
796 struct IODMACommandTransferContext
805 kIODMACommandTransferOpReadBytes
= 1,
806 kIODMACommandTransferOpWriteBytes
= 2
810 IODMACommand::transferSegment(void *reference
,
811 IODMACommand
*target
,
816 IODMACommandTransferContext
* context
= (IODMACommandTransferContext
*) reference
;
817 UInt64 length
= min(segment
.fLength
, context
->remaining
);
818 addr64_t ioAddr
= segment
.fIOVMAddr
;
819 addr64_t cpuAddr
= ioAddr
;
821 context
->remaining
-= length
;
825 UInt64 copyLen
= length
;
826 if ((kMapped
== MAPTYPE(target
->fMappingOptions
))
829 cpuAddr
= target
->fMapper
->mapAddr(ioAddr
);
830 copyLen
= min(copyLen
, page_size
- (ioAddr
& (page_size
- 1)));
836 case kIODMACommandTransferOpReadBytes
:
837 copypv(cpuAddr
, context
->bufferOffset
+ (addr64_t
) context
->buffer
, copyLen
,
838 cppvPsrc
| cppvNoRefSrc
| cppvFsnk
| cppvKmap
);
840 case kIODMACommandTransferOpWriteBytes
:
841 copypv(context
->bufferOffset
+ (addr64_t
) context
->buffer
, cpuAddr
, copyLen
,
842 cppvPsnk
| cppvFsnk
| cppvNoRefSrc
| cppvNoModSnk
| cppvKmap
);
846 context
->bufferOffset
+= copyLen
;
849 return (context
->remaining
? kIOReturnSuccess
: kIOReturnOverrun
);
853 IODMACommand::transfer(IOOptionBits transferOp
, UInt64 offset
, void * buffer
, UInt64 length
)
855 IODMACommandInternal
* state
= fInternalState
;
856 IODMACommandTransferContext context
;
857 Segment64 segments
[1];
858 UInt32 numSegments
= 0-1;
863 if (offset
>= state
->fPreparedLength
)
865 length
= min(length
, state
->fPreparedLength
- offset
);
867 context
.buffer
= buffer
;
868 context
.bufferOffset
= 0;
869 context
.remaining
= length
;
870 context
.op
= transferOp
;
871 (void) genIOVMSegments(kWalkClient
, transferSegment
, &context
, &offset
, &segments
[0], &numSegments
);
873 return (length
- context
.remaining
);
877 IODMACommand::readBytes(UInt64 offset
, void *bytes
, UInt64 length
)
879 return (transfer(kIODMACommandTransferOpReadBytes
, offset
, bytes
, length
));
883 IODMACommand::writeBytes(UInt64 offset
, const void *bytes
, UInt64 length
)
885 return (transfer(kIODMACommandTransferOpWriteBytes
, offset
, const_cast<void *>(bytes
), length
));
889 IODMACommand::genIOVMSegments(UInt64
*offsetP
,
891 UInt32
*numSegmentsP
)
893 return (genIOVMSegments(kWalkClient
, clientOutputSegment
, (void *) fOutSeg
,
894 offsetP
, segmentsP
, numSegmentsP
));
898 IODMACommand::genIOVMSegments(uint32_t op
,
899 InternalSegmentFunction outSegFunc
,
903 UInt32
*numSegmentsP
)
905 IODMACommandInternal
* internalState
= fInternalState
;
906 IOOptionBits mdOp
= kIOMDWalkSegments
;
907 IOReturn ret
= kIOReturnSuccess
;
909 if (!(kWalkComplete
& op
) && !fActive
)
910 return kIOReturnNotReady
;
912 if (!offsetP
|| !segmentsP
|| !numSegmentsP
|| !*numSegmentsP
)
913 return kIOReturnBadArgument
;
915 IOMDDMAWalkSegmentArgs
*state
=
916 (IOMDDMAWalkSegmentArgs
*) fState
;
918 UInt64 offset
= *offsetP
+ internalState
->fPreparedOffset
;
919 UInt64 memLength
= internalState
->fPreparedOffset
+ internalState
->fPreparedLength
;
921 if (offset
>= memLength
)
922 return kIOReturnOverrun
;
924 if ((offset
== internalState
->fPreparedOffset
) || (offset
!= state
->fOffset
) || internalState
->fNewMD
) {
926 state
->fIOVMAddr
= 0;
927 internalState
->fNextRemapIndex
= 0;
928 internalState
->fNewMD
= false;
929 state
->fMapped
= (IS_MAPPED(fMappingOptions
) && fMapper
);
930 mdOp
= kIOMDFirstSegment
;
933 UInt64 bypassMask
= fBypassMask
;
935 UInt32 numSegments
= *numSegmentsP
;
936 Segment64 curSeg
= { 0, 0 };
939 if (fNumAddressBits
&& (fNumAddressBits
< 64))
940 maxPhys
= (1ULL << fNumAddressBits
);
945 while ((state
->fIOVMAddr
) || state
->fOffset
< memLength
)
947 if (!state
->fIOVMAddr
) {
951 state
->fOffset
= offset
;
952 state
->fLength
= memLength
- offset
;
954 if (internalState
->fMapContig
&& (kWalkClient
& op
))
956 ppnum_t pageNum
= internalState
->fLocalMapperPageAlloc
;
958 pageNum
= internalState
->fCopyMapperPageAlloc
;
959 state
->fIOVMAddr
= ptoa_64(pageNum
)
960 + offset
- internalState
->fPreparedOffset
;
961 rtn
= kIOReturnSuccess
;
965 const IOMemoryDescriptor
* memory
=
966 internalState
->fCopyMD
? internalState
->fCopyMD
: fMemory
;
967 rtn
= memory
->dmaCommandOperation(mdOp
, fState
, sizeof(fState
));
968 mdOp
= kIOMDWalkSegments
;
971 if (rtn
== kIOReturnSuccess
) {
972 assert(state
->fIOVMAddr
);
973 assert(state
->fLength
);
975 else if (rtn
== kIOReturnOverrun
)
976 state
->fIOVMAddr
= state
->fLength
= 0; // At end
981 if (!curSeg
.fIOVMAddr
) {
982 UInt64 length
= state
->fLength
;
985 curSeg
.fIOVMAddr
= state
->fIOVMAddr
| bypassMask
;
986 curSeg
.fLength
= length
;
987 state
->fIOVMAddr
= 0;
989 else if ((curSeg
.fIOVMAddr
+ curSeg
.fLength
== state
->fIOVMAddr
)) {
990 UInt64 length
= state
->fLength
;
992 curSeg
.fLength
+= length
;
993 state
->fIOVMAddr
= 0;
997 if (!state
->fIOVMAddr
)
999 if (kWalkClient
& op
)
1001 if ((curSeg
.fIOVMAddr
+ curSeg
.fLength
- 1) > maxPhys
)
1003 if (internalState
->fCursor
)
1005 curSeg
.fIOVMAddr
= 0;
1006 ret
= kIOReturnMessageTooLarge
;
1009 else if (curSeg
.fIOVMAddr
<= maxPhys
)
1011 UInt64 remain
, newLength
;
1013 newLength
= (maxPhys
+ 1 - curSeg
.fIOVMAddr
);
1014 DEBG("trunc %qx, %qx-> %qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
, newLength
);
1015 remain
= curSeg
.fLength
- newLength
;
1016 state
->fIOVMAddr
= newLength
+ curSeg
.fIOVMAddr
;
1017 curSeg
.fLength
= newLength
;
1018 state
->fLength
= remain
;
1021 else if (gIOCopyMapper
)
1023 DEBG("sparse switch %qx, %qx ", curSeg
.fIOVMAddr
, curSeg
.fLength
);
1024 if (trunc_page_64(curSeg
.fIOVMAddr
) == gIOCopyMapper
->mapAddr(
1025 ptoa_64(internalState
->fCopyMapperPageAlloc
+ internalState
->fNextRemapIndex
)))
1028 curSeg
.fIOVMAddr
= ptoa_64(internalState
->fCopyMapperPageAlloc
+ internalState
->fNextRemapIndex
)
1029 + (curSeg
.fIOVMAddr
& PAGE_MASK
);
1030 internalState
->fNextRemapIndex
+= atop_64(round_page(curSeg
.fLength
));
1032 else for (UInt checkRemapIndex
= 0; checkRemapIndex
< internalState
->fCopyPageCount
; checkRemapIndex
++)
1034 if (trunc_page_64(curSeg
.fIOVMAddr
) == gIOCopyMapper
->mapAddr(
1035 ptoa_64(internalState
->fCopyMapperPageAlloc
+ checkRemapIndex
)))
1037 curSeg
.fIOVMAddr
= ptoa_64(internalState
->fCopyMapperPageAlloc
+ checkRemapIndex
)
1038 + (curSeg
.fIOVMAddr
& PAGE_MASK
);
1039 internalState
->fNextRemapIndex
= checkRemapIndex
+ atop_64(round_page(curSeg
.fLength
));
1043 DEBG("-> %qx, %qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
);
1048 if (curSeg
.fLength
> fMaxSegmentSize
)
1050 UInt64 remain
= curSeg
.fLength
- fMaxSegmentSize
;
1052 state
->fIOVMAddr
= fMaxSegmentSize
+ curSeg
.fIOVMAddr
;
1053 curSeg
.fLength
= fMaxSegmentSize
;
1055 state
->fLength
= remain
;
1059 if (internalState
->fCursor
1060 && (0 != (internalState
->fSourceAlignMask
& curSeg
.fIOVMAddr
)))
1062 curSeg
.fIOVMAddr
= 0;
1063 ret
= kIOReturnNotAligned
;
1067 if (offset
>= memLength
)
1069 curSeg
.fLength
-= (offset
- memLength
);
1071 state
->fIOVMAddr
= state
->fLength
= 0; // At end
1076 if (state
->fIOVMAddr
) {
1077 if ((segIndex
+ 1 == numSegments
))
1080 ret
= (*outSegFunc
)(reference
, this, curSeg
, segmentsP
, segIndex
++);
1081 curSeg
.fIOVMAddr
= 0;
1082 if (kIOReturnSuccess
!= ret
)
1087 if (curSeg
.fIOVMAddr
) {
1088 ret
= (*outSegFunc
)(reference
, this, curSeg
, segmentsP
, segIndex
++);
1091 if (kIOReturnSuccess
== ret
)
1093 state
->fOffset
= offset
;
1094 *offsetP
= offset
- internalState
->fPreparedOffset
;
1095 *numSegmentsP
= segIndex
;
1101 IODMACommand::clientOutputSegment(
1102 void *reference
, IODMACommand
*target
,
1103 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1105 SegmentFunction segmentFunction
= (SegmentFunction
) reference
;
1106 IOReturn ret
= kIOReturnSuccess
;
1108 if ((target
->fNumAddressBits
< 64)
1109 && ((segment
.fIOVMAddr
+ segment
.fLength
- 1) >> target
->fNumAddressBits
)
1110 && (target
->reserved
->fLocalMapperPageAlloc
|| !target
->reserved
->fLocalMapper
))
1112 DEBG("kIOReturnMessageTooLarge(fNumAddressBits) %qx, %qx\n", segment
.fIOVMAddr
, segment
.fLength
);
1113 ret
= kIOReturnMessageTooLarge
;
1116 if (!(*segmentFunction
)(target
, segment
, vSegList
, outSegIndex
))
1118 DEBG("kIOReturnMessageTooLarge(fOutSeg) %qx, %qx\n", segment
.fIOVMAddr
, segment
.fLength
);
1119 ret
= kIOReturnMessageTooLarge
;
1126 IODMACommand::genIOVMSegments(SegmentFunction segmentFunction
,
1129 UInt32
*numSegmentsP
)
1131 return (genIOVMSegments(kWalkClient
, clientOutputSegment
, (void *) segmentFunction
,
1132 offsetP
, segmentsP
, numSegmentsP
));
1136 IODMACommand::OutputHost32(IODMACommand
*,
1137 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1139 Segment32
*base
= (Segment32
*) vSegList
;
1140 base
[outSegIndex
].fIOVMAddr
= (UInt32
) segment
.fIOVMAddr
;
1141 base
[outSegIndex
].fLength
= (UInt32
) segment
.fLength
;
1146 IODMACommand::OutputBig32(IODMACommand
*,
1147 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1149 const UInt offAddr
= outSegIndex
* sizeof(Segment32
);
1150 const UInt offLen
= offAddr
+ sizeof(UInt32
);
1151 OSWriteBigInt32(vSegList
, offAddr
, (UInt32
) segment
.fIOVMAddr
);
1152 OSWriteBigInt32(vSegList
, offLen
, (UInt32
) segment
.fLength
);
1157 IODMACommand::OutputLittle32(IODMACommand
*,
1158 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1160 const UInt offAddr
= outSegIndex
* sizeof(Segment32
);
1161 const UInt offLen
= offAddr
+ sizeof(UInt32
);
1162 OSWriteLittleInt32(vSegList
, offAddr
, (UInt32
) segment
.fIOVMAddr
);
1163 OSWriteLittleInt32(vSegList
, offLen
, (UInt32
) segment
.fLength
);
1168 IODMACommand::OutputHost64(IODMACommand
*,
1169 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1171 Segment64
*base
= (Segment64
*) vSegList
;
1172 base
[outSegIndex
] = segment
;
1177 IODMACommand::OutputBig64(IODMACommand
*,
1178 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1180 const UInt offAddr
= outSegIndex
* sizeof(Segment64
);
1181 const UInt offLen
= offAddr
+ sizeof(UInt64
);
1182 OSWriteBigInt64(vSegList
, offAddr
, (UInt64
) segment
.fIOVMAddr
);
1183 OSWriteBigInt64(vSegList
, offLen
, (UInt64
) segment
.fLength
);
1188 IODMACommand::OutputLittle64(IODMACommand
*,
1189 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1191 const UInt offAddr
= outSegIndex
* sizeof(Segment64
);
1192 const UInt offLen
= offAddr
+ sizeof(UInt64
);
1193 OSWriteLittleInt64(vSegList
, offAddr
, (UInt64
) segment
.fIOVMAddr
);
1194 OSWriteLittleInt64(vSegList
, offLen
, (UInt64
) segment
.fLength
);