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"
43 #define MAPTYPE(type) ((UInt) (type) & kTypeMask)
44 #define IS_MAPPED(type) (MAPTYPE(type) == kMapped)
45 #define IS_BYPASSED(type) (MAPTYPE(type) == kBypassed)
46 #define IS_NONCOHERENT(type) (MAPTYPE(type) == kNonCoherent)
50 kWalkSyncIn
= 0x01, // bounce -> md
51 kWalkSyncOut
= 0x02, // bounce <- md
52 kWalkSyncAlways
= 0x04,
53 kWalkPreflight
= 0x08,
54 kWalkDoubleBuffer
= 0x10,
61 #define fInternalState reserved
62 #define fState reserved->fState
63 #define fMDSummary reserved->fMDSummary
67 // no direction => OutIn
68 #define SHOULD_COPY_DIR(op, direction) \
69 ((kIODirectionNone == (direction)) \
70 || (kWalkSyncAlways & (op)) \
71 || (((kWalkSyncIn & (op)) ? kIODirectionIn : kIODirectionOut) \
75 #define SHOULD_COPY_DIR(state, direction) (true)
79 #define DEBG(fmt, args...) { IOLog(fmt, ## args); kprintf(fmt, ## args); }
81 #define DEBG(fmt, args...) {}
84 /**************************** class IODMACommand ***************************/
87 #define super IOCommand
88 OSDefineMetaClassAndStructors(IODMACommand
, IOCommand
);
90 OSMetaClassDefineReservedUsed(IODMACommand
, 0);
91 OSMetaClassDefineReservedUsed(IODMACommand
, 1);
92 OSMetaClassDefineReservedUsed(IODMACommand
, 2);
93 OSMetaClassDefineReservedUnused(IODMACommand
, 3);
94 OSMetaClassDefineReservedUnused(IODMACommand
, 4);
95 OSMetaClassDefineReservedUnused(IODMACommand
, 5);
96 OSMetaClassDefineReservedUnused(IODMACommand
, 6);
97 OSMetaClassDefineReservedUnused(IODMACommand
, 7);
98 OSMetaClassDefineReservedUnused(IODMACommand
, 8);
99 OSMetaClassDefineReservedUnused(IODMACommand
, 9);
100 OSMetaClassDefineReservedUnused(IODMACommand
, 10);
101 OSMetaClassDefineReservedUnused(IODMACommand
, 11);
102 OSMetaClassDefineReservedUnused(IODMACommand
, 12);
103 OSMetaClassDefineReservedUnused(IODMACommand
, 13);
104 OSMetaClassDefineReservedUnused(IODMACommand
, 14);
105 OSMetaClassDefineReservedUnused(IODMACommand
, 15);
108 IODMACommand::withSpecification(SegmentFunction outSegFunc
,
109 UInt8 numAddressBits
,
110 UInt64 maxSegmentSize
,
111 MappingOptions mappingOptions
,
112 UInt64 maxTransferSize
,
117 IODMACommand
* me
= new IODMACommand
;
119 if (me
&& !me
->initWithSpecification(outSegFunc
,
120 numAddressBits
, maxSegmentSize
,
121 mappingOptions
, maxTransferSize
,
122 alignment
, mapper
, refCon
))
132 IODMACommand::cloneCommand(void *refCon
)
134 return withSpecification(fOutSeg
, fNumAddressBits
, fMaxSegmentSize
,
135 fMappingOptions
, fMaxTransferSize
, fAlignMask
+ 1, fMapper
, refCon
);
138 #define kLastOutputFunction ((SegmentFunction) kLastOutputFunction)
141 IODMACommand::initWithSpecification(SegmentFunction outSegFunc
,
142 UInt8 numAddressBits
,
143 UInt64 maxSegmentSize
,
144 MappingOptions mappingOptions
,
145 UInt64 maxTransferSize
,
150 if (!super::init() || !outSegFunc
|| !numAddressBits
)
153 bool is32Bit
= (OutputHost32
== outSegFunc
|| OutputBig32
== outSegFunc
154 || OutputLittle32
== outSegFunc
);
159 else if (numAddressBits
> 32)
160 return false; // Wrong output function for bits
163 if (numAddressBits
&& (numAddressBits
< PAGE_SHIFT
))
167 maxSegmentSize
--; // Set Max segment to -1
168 if (!maxTransferSize
)
169 maxTransferSize
--; // Set Max transfer to -1
173 IOMapper::checkForSystemMapper();
174 mapper
= IOMapper::gSystem
;
179 fOutSeg
= outSegFunc
;
180 fNumAddressBits
= numAddressBits
;
181 fMaxSegmentSize
= maxSegmentSize
;
182 fMappingOptions
= mappingOptions
;
183 fMaxTransferSize
= maxTransferSize
;
186 fAlignMask
= alignment
- 1;
190 switch (MAPTYPE(mappingOptions
))
193 case kNonCoherent
: fMapper
= 0; break;
195 if (mapper
&& !mapper
->getBypassMask(&fBypassMask
))
205 reserved
= IONew(IODMACommandInternal
, 1);
208 bzero(reserved
, sizeof(IODMACommandInternal
));
210 fInternalState
->fIterateOnly
= (0 != (kIterateOnly
& mappingOptions
));
219 IODelete(reserved
, IODMACommandInternal
, 1);
228 IODMACommand::setMemoryDescriptor(const IOMemoryDescriptor
*mem
, bool autoPrepare
)
230 IOReturn err
= kIOReturnSuccess
;
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 err
= 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);
277 clearMemoryDescriptor();
286 IODMACommand::clearMemoryDescriptor(bool autoComplete
)
288 if (fActive
&& !autoComplete
)
289 return (kIOReturnNotReady
);
294 fMemory
->dmaCommandOperation(kIOMDSetDMAInactive
, this, 0);
299 return (kIOReturnSuccess
);
302 const IOMemoryDescriptor
*
303 IODMACommand::getMemoryDescriptor() const
310 IODMACommand::segmentOp(
312 IODMACommand
*target
,
317 IOOptionBits op
= (uintptr_t) reference
;
318 addr64_t maxPhys
, address
;
322 IODMACommandInternal
* state
= target
->reserved
;
324 if (target
->fNumAddressBits
&& (target
->fNumAddressBits
< 64) && !state
->fLocalMapper
)
325 maxPhys
= (1ULL << target
->fNumAddressBits
);
330 address
= segment
.fIOVMAddr
;
331 length
= segment
.fLength
;
336 if (!state
->fMisaligned
)
338 state
->fMisaligned
|= (0 != (state
->fSourceAlignMask
& address
));
339 if (state
->fMisaligned
) DEBG("misaligned %qx:%qx, %lx\n", address
, length
, state
->fSourceAlignMask
);
342 if (state
->fMisaligned
&& (kWalkPreflight
& op
))
343 return (kIOReturnNotAligned
);
345 if (!state
->fDoubleBuffer
)
347 if ((address
+ length
- 1) <= maxPhys
)
351 else if (address
<= maxPhys
)
353 DEBG("tail %qx, %qx", address
, length
);
354 length
= (address
+ length
- maxPhys
- 1);
355 address
= maxPhys
+ 1;
356 DEBG("-> %qx, %qx\n", address
, length
);
361 return (kIOReturnSuccess
);
363 numPages
= atop_64(round_page_64((address
& PAGE_MASK
) + length
));
365 if (kWalkPreflight
& op
)
367 state
->fCopyPageCount
+= numPages
;
373 if (kWalkPrepare
& op
)
375 lastPage
= state
->fCopyNext
;
376 for (IOItemCount idx
= 0; idx
< numPages
; idx
++)
378 vm_page_set_offset(lastPage
, atop_64(address
) + idx
);
379 lastPage
= vm_page_get_next(lastPage
);
383 if (!lastPage
|| SHOULD_COPY_DIR(op
, target
->fMDSummary
.fDirection
))
385 lastPage
= state
->fCopyNext
;
386 for (IOItemCount idx
= 0; idx
< numPages
; idx
++)
388 if (SHOULD_COPY_DIR(op
, target
->fMDSummary
.fDirection
))
393 remapAddr
= ptoa_64(vm_page_get_phys_page(lastPage
));
394 if (!state
->fDoubleBuffer
)
396 remapAddr
+= (address
& PAGE_MASK
);
398 chunk
= PAGE_SIZE
- (address
& PAGE_MASK
);
402 DEBG("cpv: 0x%qx %s 0x%qx, 0x%qx, 0x%02lx\n", remapAddr
,
403 (kWalkSyncIn
& op
) ? "->" : "<-",
406 if (kWalkSyncIn
& op
)
408 copypv(remapAddr
, address
, chunk
,
409 cppvPsnk
| cppvFsnk
| cppvPsrc
| cppvNoRefSrc
);
413 copypv(address
, remapAddr
, chunk
,
414 cppvPsnk
| cppvFsnk
| cppvPsrc
| cppvNoRefSrc
);
419 lastPage
= vm_page_get_next(lastPage
);
422 state
->fCopyNext
= lastPage
;
425 return kIOReturnSuccess
;
429 IODMACommand::walkAll(UInt8 op
)
431 IODMACommandInternal
* state
= fInternalState
;
433 IOReturn ret
= kIOReturnSuccess
;
437 if (kWalkPreflight
& op
)
439 state
->fMapContig
= false;
440 state
->fMisaligned
= false;
441 state
->fDoubleBuffer
= false;
442 state
->fPrepared
= false;
443 state
->fCopyNext
= NULL
;
444 state
->fCopyPageAlloc
= 0;
445 state
->fLocalMapperPageAlloc
= 0;
446 state
->fCopyPageCount
= 0;
447 state
->fNextRemapPage
= NULL
;
450 if (!(kWalkDoubleBuffer
& op
))
454 ret
= genIOVMSegments(op
, segmentOp
, (void *) op
, &offset
, state
, &numSegments
);
457 op
&= ~kWalkPreflight
;
459 state
->fDoubleBuffer
= (state
->fMisaligned
|| (kWalkDoubleBuffer
& op
));
460 if (state
->fDoubleBuffer
)
461 state
->fCopyPageCount
= atop_64(round_page(state
->fPreparedLength
));
463 if (state
->fCopyPageCount
)
465 vm_page_t mapBase
= NULL
;
467 DEBG("preflight fCopyPageCount %d\n", state
->fCopyPageCount
);
469 if (!state
->fDoubleBuffer
)
472 kr
= vm_page_alloc_list(state
->fCopyPageCount
,
473 KMA_LOMEM
| KMA_NOPAGEWAIT
, &mapBase
);
474 if (KERN_SUCCESS
!= kr
)
476 DEBG("vm_page_alloc_list(%d) failed (%d)\n", state
->fCopyPageCount
, kr
);
483 state
->fCopyPageAlloc
= mapBase
;
484 state
->fCopyNext
= state
->fCopyPageAlloc
;
487 ret
= genIOVMSegments(op
, segmentOp
, (void *) op
, &offset
, state
, &numSegments
);
488 state
->fPrepared
= true;
489 op
&= ~(kWalkSyncIn
| kWalkSyncOut
);
493 DEBG("alloc IOBMD\n");
494 mach_vm_address_t mask
= 0xFFFFF000; //state->fSourceAlignMask
495 state
->fCopyMD
= IOBufferMemoryDescriptor::inTaskWithPhysicalMask(kernel_task
,
496 fMDSummary
.fDirection
, state
->fPreparedLength
, mask
);
500 ret
= kIOReturnSuccess
;
501 state
->fPrepared
= true;
505 DEBG("IODMACommand !iovmAlloc");
506 return (kIOReturnNoResources
);
511 if (state
->fLocalMapper
)
513 state
->fLocalMapperPageCount
= atop_64(round_page(
514 state
->fPreparedLength
+ ((state
->fPreparedOffset
+ fMDSummary
.fPageAlign
) & page_mask
)));
515 state
->fLocalMapperPageAlloc
= fMapper
->iovmAllocDMACommand(this, state
->fLocalMapperPageCount
);
516 state
->fMapContig
= true;
520 if (state
->fPrepared
&& ((kWalkSyncIn
| kWalkSyncOut
) & op
))
522 if (state
->fCopyPageCount
)
524 DEBG("sync fCopyPageCount %d\n", state
->fCopyPageCount
);
526 if (state
->fCopyPageAlloc
)
528 state
->fCopyNext
= state
->fCopyPageAlloc
;
531 ret
= genIOVMSegments(op
, segmentOp
, (void *) op
, &offset
, state
, &numSegments
);
533 else if (state
->fCopyMD
)
535 DEBG("sync IOBMD\n");
537 if (SHOULD_COPY_DIR(op
, fMDSummary
.fDirection
))
539 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
543 if (kWalkSyncIn
& op
)
544 bytes
= poMD
->writeBytes(state
->fPreparedOffset
,
545 state
->fCopyMD
->getBytesNoCopy(),
546 state
->fPreparedLength
);
548 bytes
= poMD
->readBytes(state
->fPreparedOffset
,
549 state
->fCopyMD
->getBytesNoCopy(),
550 state
->fPreparedLength
);
551 DEBG("fCopyMD %s %lx bytes\n", (kWalkSyncIn
& op
) ? "wrote" : "read", bytes
);
552 ret
= (bytes
== state
->fPreparedLength
) ? kIOReturnSuccess
: kIOReturnUnderrun
;
555 ret
= kIOReturnSuccess
;
560 if (kWalkComplete
& op
)
562 if (state
->fLocalMapperPageAlloc
)
564 fMapper
->iovmFreeDMACommand(this, state
->fLocalMapperPageAlloc
, state
->fLocalMapperPageCount
);
565 state
->fLocalMapperPageAlloc
= 0;
566 state
->fLocalMapperPageCount
= 0;
568 if (state
->fCopyPageAlloc
)
570 vm_page_free_list(state
->fCopyPageAlloc
, FALSE
);
571 state
->fCopyPageAlloc
= 0;
572 state
->fCopyPageCount
= 0;
576 state
->fCopyMD
->release();
580 state
->fPrepared
= false;
586 IODMACommand::getNumAddressBits(void)
588 return (fNumAddressBits
);
592 IODMACommand::getAlignment(void)
594 return (fAlignMask
+ 1);
598 IODMACommand::prepareWithSpecification(SegmentFunction outSegFunc
,
599 UInt8 numAddressBits
,
600 UInt64 maxSegmentSize
,
601 MappingOptions mappingOptions
,
602 UInt64 maxTransferSize
,
611 return kIOReturnNotPermitted
;
613 if (!outSegFunc
|| !numAddressBits
)
614 return kIOReturnBadArgument
;
616 bool is32Bit
= (OutputHost32
== outSegFunc
|| OutputBig32
== outSegFunc
617 || OutputLittle32
== outSegFunc
);
622 else if (numAddressBits
> 32)
623 return kIOReturnBadArgument
; // Wrong output function for bits
626 if (numAddressBits
&& (numAddressBits
< PAGE_SHIFT
))
627 return kIOReturnBadArgument
;
630 maxSegmentSize
--; // Set Max segment to -1
631 if (!maxTransferSize
)
632 maxTransferSize
--; // Set Max transfer to -1
636 IOMapper::checkForSystemMapper();
637 mapper
= IOMapper::gSystem
;
640 switch (MAPTYPE(mappingOptions
))
643 case kNonCoherent
: fMapper
= 0; break;
645 if (mapper
&& !mapper
->getBypassMask(&fBypassMask
))
646 return kIOReturnBadArgument
;
649 return kIOReturnBadArgument
;
654 fOutSeg
= outSegFunc
;
655 fNumAddressBits
= numAddressBits
;
656 fMaxSegmentSize
= maxSegmentSize
;
657 fMappingOptions
= mappingOptions
;
658 fMaxTransferSize
= maxTransferSize
;
661 fAlignMask
= alignment
- 1;
662 if (mapper
!= fMapper
)
669 fInternalState
->fIterateOnly
= (0 != (kIterateOnly
& mappingOptions
));
671 return prepare(offset
, length
, flushCache
, synchronize
);
676 IODMACommand::prepare(UInt64 offset
, UInt64 length
, bool flushCache
, bool synchronize
)
678 IODMACommandInternal
* state
= fInternalState
;
679 IOReturn ret
= kIOReturnSuccess
;
680 MappingOptions mappingOptions
= fMappingOptions
;
683 length
= fMDSummary
.fLength
;
685 if (length
> fMaxTransferSize
)
686 return kIOReturnNoSpace
;
688 if (IS_NONCOHERENT(mappingOptions
) && flushCache
) {
689 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
691 poMD
->performOperation(kIOMemoryIncoherentIOStore
, offset
, length
);
695 if ((state
->fPreparedOffset
!= offset
)
696 || (state
->fPreparedLength
!= length
))
697 ret
= kIOReturnNotReady
;
701 state
->fPreparedOffset
= offset
;
702 state
->fPreparedLength
= length
;
704 state
->fMapContig
= false;
705 state
->fMisaligned
= false;
706 state
->fDoubleBuffer
= false;
707 state
->fPrepared
= false;
708 state
->fCopyNext
= NULL
;
709 state
->fCopyPageAlloc
= 0;
710 state
->fCopyPageCount
= 0;
711 state
->fNextRemapPage
= NULL
;
713 state
->fLocalMapperPageAlloc
= 0;
714 state
->fLocalMapperPageCount
= 0;
716 state
->fLocalMapper
= (fMapper
&& (fMapper
!= IOMapper::gSystem
));
718 state
->fSourceAlignMask
= fAlignMask
;
719 if (state
->fLocalMapper
)
720 state
->fSourceAlignMask
&= page_mask
;
722 state
->fCursor
= state
->fIterateOnly
723 || (!state
->fCheckAddressing
724 && !state
->fLocalMapper
725 && (!state
->fSourceAlignMask
726 || ((fMDSummary
.fPageAlign
& (1 << 31)) && (0 == (fMDSummary
.fPageAlign
& state
->fSourceAlignMask
)))));
730 IOOptionBits op
= kWalkPrepare
| kWalkPreflight
;
735 if (kIOReturnSuccess
== ret
)
736 state
->fPrepared
= true;
742 IODMACommand::complete(bool invalidateCache
, bool synchronize
)
744 IODMACommandInternal
* state
= fInternalState
;
745 IOReturn ret
= kIOReturnSuccess
;
748 return kIOReturnNotReady
;
754 IOOptionBits op
= kWalkComplete
;
759 state
->fPrepared
= false;
761 if (IS_NONCOHERENT(fMappingOptions
) && invalidateCache
)
763 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
765 poMD
->performOperation(kIOMemoryIncoherentIOFlush
, state
->fPreparedOffset
, state
->fPreparedLength
);
773 IODMACommand::getPreparedOffsetAndLength(UInt64
* offset
, UInt64
* length
)
775 IODMACommandInternal
* state
= fInternalState
;
777 return (kIOReturnNotReady
);
780 *offset
= state
->fPreparedOffset
;
782 *length
= state
->fPreparedLength
;
784 return (kIOReturnSuccess
);
788 IODMACommand::synchronize(IOOptionBits options
)
790 IODMACommandInternal
* state
= fInternalState
;
791 IOReturn ret
= kIOReturnSuccess
;
794 if (kIODirectionOutIn
== (kIODirectionOutIn
& options
))
795 return kIOReturnBadArgument
;
798 return kIOReturnNotReady
;
801 if (kForceDoubleBuffer
& options
)
803 if (state
->fDoubleBuffer
)
804 return kIOReturnSuccess
;
806 state
->fCursor
= false;
808 ret
= walkAll(kWalkComplete
);
810 op
|= kWalkPrepare
| kWalkPreflight
| kWalkDoubleBuffer
;
812 else if (state
->fCursor
)
813 return kIOReturnSuccess
;
815 if (kIODirectionIn
& options
)
816 op
|= kWalkSyncIn
| kWalkSyncAlways
;
817 else if (kIODirectionOut
& options
)
818 op
|= kWalkSyncOut
| kWalkSyncAlways
;
825 struct IODMACommandTransferContext
834 kIODMACommandTransferOpReadBytes
= 1,
835 kIODMACommandTransferOpWriteBytes
= 2
839 IODMACommand::transferSegment(void *reference
,
840 IODMACommand
*target
,
845 IODMACommandTransferContext
* context
= (IODMACommandTransferContext
*) reference
;
846 UInt64 length
= min(segment
.fLength
, context
->remaining
);
847 addr64_t ioAddr
= segment
.fIOVMAddr
;
848 addr64_t cpuAddr
= ioAddr
;
850 context
->remaining
-= length
;
854 UInt64 copyLen
= length
;
855 if ((kMapped
== MAPTYPE(target
->fMappingOptions
))
858 cpuAddr
= target
->fMapper
->mapAddr(ioAddr
);
859 copyLen
= min(copyLen
, page_size
- (ioAddr
& (page_size
- 1)));
865 case kIODMACommandTransferOpReadBytes
:
866 copypv(cpuAddr
, context
->bufferOffset
+ (addr64_t
) context
->buffer
, copyLen
,
867 cppvPsrc
| cppvNoRefSrc
| cppvFsnk
| cppvKmap
);
869 case kIODMACommandTransferOpWriteBytes
:
870 copypv(context
->bufferOffset
+ (addr64_t
) context
->buffer
, cpuAddr
, copyLen
,
871 cppvPsnk
| cppvFsnk
| cppvNoRefSrc
| cppvNoModSnk
| cppvKmap
);
875 context
->bufferOffset
+= copyLen
;
878 return (context
->remaining
? kIOReturnSuccess
: kIOReturnOverrun
);
882 IODMACommand::transfer(IOOptionBits transferOp
, UInt64 offset
, void * buffer
, UInt64 length
)
884 IODMACommandInternal
* state
= fInternalState
;
885 IODMACommandTransferContext context
;
886 Segment64 segments
[1];
887 UInt32 numSegments
= 0-1;
892 if (offset
>= state
->fPreparedLength
)
894 length
= min(length
, state
->fPreparedLength
- offset
);
896 context
.buffer
= buffer
;
897 context
.bufferOffset
= 0;
898 context
.remaining
= length
;
899 context
.op
= transferOp
;
900 (void) genIOVMSegments(kWalkClient
, transferSegment
, &context
, &offset
, &segments
[0], &numSegments
);
902 return (length
- context
.remaining
);
906 IODMACommand::readBytes(UInt64 offset
, void *bytes
, UInt64 length
)
908 return (transfer(kIODMACommandTransferOpReadBytes
, offset
, bytes
, length
));
912 IODMACommand::writeBytes(UInt64 offset
, const void *bytes
, UInt64 length
)
914 return (transfer(kIODMACommandTransferOpWriteBytes
, offset
, const_cast<void *>(bytes
), length
));
918 IODMACommand::genIOVMSegments(UInt64
*offsetP
,
920 UInt32
*numSegmentsP
)
922 return (genIOVMSegments(kWalkClient
, clientOutputSegment
, (void *) fOutSeg
,
923 offsetP
, segmentsP
, numSegmentsP
));
927 IODMACommand::genIOVMSegments(uint32_t op
,
928 InternalSegmentFunction outSegFunc
,
932 UInt32
*numSegmentsP
)
934 IODMACommandInternal
* internalState
= fInternalState
;
935 IOOptionBits mdOp
= kIOMDWalkSegments
;
936 IOReturn ret
= kIOReturnSuccess
;
938 if (!(kWalkComplete
& op
) && !fActive
)
939 return kIOReturnNotReady
;
941 if (!offsetP
|| !segmentsP
|| !numSegmentsP
|| !*numSegmentsP
)
942 return kIOReturnBadArgument
;
944 IOMDDMAWalkSegmentArgs
*state
=
945 (IOMDDMAWalkSegmentArgs
*) fState
;
947 UInt64 offset
= *offsetP
+ internalState
->fPreparedOffset
;
948 UInt64 memLength
= internalState
->fPreparedOffset
+ internalState
->fPreparedLength
;
950 if (offset
>= memLength
)
951 return kIOReturnOverrun
;
953 if ((offset
== internalState
->fPreparedOffset
) || (offset
!= state
->fOffset
) || internalState
->fNewMD
) {
955 state
->fIOVMAddr
= 0;
956 internalState
->fNextRemapPage
= NULL
;
957 internalState
->fNewMD
= false;
958 state
->fMapped
= (IS_MAPPED(fMappingOptions
) && fMapper
);
959 mdOp
= kIOMDFirstSegment
;
962 UInt64 bypassMask
= fBypassMask
;
964 UInt32 numSegments
= *numSegmentsP
;
965 Segment64 curSeg
= { 0, 0 };
968 if (fNumAddressBits
&& (fNumAddressBits
< 64))
969 maxPhys
= (1ULL << fNumAddressBits
);
974 while (state
->fIOVMAddr
|| (state
->fOffset
< memLength
))
977 if (!state
->fIOVMAddr
) {
981 state
->fOffset
= offset
;
982 state
->fLength
= memLength
- offset
;
984 if (internalState
->fMapContig
&& (kWalkClient
& op
))
986 ppnum_t pageNum
= internalState
->fLocalMapperPageAlloc
;
987 state
->fIOVMAddr
= ptoa_64(pageNum
)
988 + offset
- internalState
->fPreparedOffset
;
989 rtn
= kIOReturnSuccess
;
993 const IOMemoryDescriptor
* memory
=
994 internalState
->fCopyMD
? internalState
->fCopyMD
: fMemory
;
995 rtn
= memory
->dmaCommandOperation(mdOp
, fState
, sizeof(fState
));
996 mdOp
= kIOMDWalkSegments
;
999 if (rtn
== kIOReturnSuccess
)
1001 assert(state
->fIOVMAddr
);
1002 assert(state
->fLength
);
1003 if ((curSeg
.fIOVMAddr
+ curSeg
.fLength
) == state
->fIOVMAddr
) {
1004 UInt64 length
= state
->fLength
;
1006 curSeg
.fLength
+= length
;
1007 state
->fIOVMAddr
= 0;
1010 else if (rtn
== kIOReturnOverrun
)
1011 state
->fIOVMAddr
= state
->fLength
= 0; // At end
1016 // seg = state, offset = end of seg
1017 if (!curSeg
.fIOVMAddr
)
1019 UInt64 length
= state
->fLength
;
1021 curSeg
.fIOVMAddr
= state
->fIOVMAddr
| bypassMask
;
1022 curSeg
.fLength
= length
;
1023 state
->fIOVMAddr
= 0;
1026 if (!state
->fIOVMAddr
)
1028 if ((kWalkClient
& op
) && (curSeg
.fIOVMAddr
+ curSeg
.fLength
- 1) > maxPhys
)
1030 if (internalState
->fCursor
)
1032 curSeg
.fIOVMAddr
= 0;
1033 ret
= kIOReturnMessageTooLarge
;
1036 else if (curSeg
.fIOVMAddr
<= maxPhys
)
1038 UInt64 remain
, newLength
;
1040 newLength
= (maxPhys
+ 1 - curSeg
.fIOVMAddr
);
1041 DEBG("trunc %qx, %qx-> %qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
, newLength
);
1042 remain
= curSeg
.fLength
- newLength
;
1043 state
->fIOVMAddr
= newLength
+ curSeg
.fIOVMAddr
;
1044 curSeg
.fLength
= newLength
;
1045 state
->fLength
= remain
;
1050 UInt64 addr
= curSeg
.fIOVMAddr
;
1051 ppnum_t addrPage
= atop_64(addr
);
1052 vm_page_t remap
= NULL
;
1053 UInt64 remain
, newLength
;
1055 DEBG("sparse switch %qx, %qx ", addr
, curSeg
.fLength
);
1057 remap
= internalState
->fNextRemapPage
;
1058 if (remap
&& (addrPage
== vm_page_get_offset(remap
)))
1061 else for (remap
= internalState
->fCopyPageAlloc
;
1062 remap
&& (addrPage
!= vm_page_get_offset(remap
));
1063 remap
= vm_page_get_next(remap
))
1067 if (!remap
) panic("no remap page found");
1069 curSeg
.fIOVMAddr
= ptoa_64(vm_page_get_phys_page(remap
))
1070 + (addr
& PAGE_MASK
);
1071 internalState
->fNextRemapPage
= vm_page_get_next(remap
);
1073 newLength
= PAGE_SIZE
- (addr
& PAGE_MASK
);
1074 if (newLength
< curSeg
.fLength
)
1076 remain
= curSeg
.fLength
- newLength
;
1077 state
->fIOVMAddr
= addr
+ newLength
;
1078 curSeg
.fLength
= newLength
;
1079 state
->fLength
= remain
;
1082 DEBG("-> %qx, %qx offset %qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
, offset
);
1086 if (curSeg
.fLength
> fMaxSegmentSize
)
1088 UInt64 remain
= curSeg
.fLength
- fMaxSegmentSize
;
1090 state
->fIOVMAddr
= fMaxSegmentSize
+ curSeg
.fIOVMAddr
;
1091 curSeg
.fLength
= fMaxSegmentSize
;
1093 state
->fLength
= remain
;
1097 if (internalState
->fCursor
1098 && (0 != (internalState
->fSourceAlignMask
& curSeg
.fIOVMAddr
)))
1100 curSeg
.fIOVMAddr
= 0;
1101 ret
= kIOReturnNotAligned
;
1105 if (offset
>= memLength
)
1107 curSeg
.fLength
-= (offset
- memLength
);
1109 state
->fIOVMAddr
= state
->fLength
= 0; // At end
1114 if (state
->fIOVMAddr
) {
1115 if ((segIndex
+ 1 == numSegments
))
1118 ret
= (*outSegFunc
)(reference
, this, curSeg
, segmentsP
, segIndex
++);
1119 curSeg
.fIOVMAddr
= 0;
1120 if (kIOReturnSuccess
!= ret
)
1125 if (curSeg
.fIOVMAddr
) {
1126 ret
= (*outSegFunc
)(reference
, this, curSeg
, segmentsP
, segIndex
++);
1129 if (kIOReturnSuccess
== ret
)
1131 state
->fOffset
= offset
;
1132 *offsetP
= offset
- internalState
->fPreparedOffset
;
1133 *numSegmentsP
= segIndex
;
1139 IODMACommand::clientOutputSegment(
1140 void *reference
, IODMACommand
*target
,
1141 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1143 SegmentFunction segmentFunction
= (SegmentFunction
) reference
;
1144 IOReturn ret
= kIOReturnSuccess
;
1146 if ((target
->fNumAddressBits
< 64)
1147 && ((segment
.fIOVMAddr
+ segment
.fLength
- 1) >> target
->fNumAddressBits
)
1148 && (target
->reserved
->fLocalMapperPageAlloc
|| !target
->reserved
->fLocalMapper
))
1150 DEBG("kIOReturnMessageTooLarge(fNumAddressBits) %qx, %qx\n", segment
.fIOVMAddr
, segment
.fLength
);
1151 ret
= kIOReturnMessageTooLarge
;
1154 if (!(*segmentFunction
)(target
, segment
, vSegList
, outSegIndex
))
1156 DEBG("kIOReturnMessageTooLarge(fOutSeg) %qx, %qx\n", segment
.fIOVMAddr
, segment
.fLength
);
1157 ret
= kIOReturnMessageTooLarge
;
1164 IODMACommand::genIOVMSegments(SegmentFunction segmentFunction
,
1167 UInt32
*numSegmentsP
)
1169 return (genIOVMSegments(kWalkClient
, clientOutputSegment
, (void *) segmentFunction
,
1170 offsetP
, segmentsP
, numSegmentsP
));
1174 IODMACommand::OutputHost32(IODMACommand
*,
1175 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1177 Segment32
*base
= (Segment32
*) vSegList
;
1178 base
[outSegIndex
].fIOVMAddr
= (UInt32
) segment
.fIOVMAddr
;
1179 base
[outSegIndex
].fLength
= (UInt32
) segment
.fLength
;
1184 IODMACommand::OutputBig32(IODMACommand
*,
1185 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1187 const UInt offAddr
= outSegIndex
* sizeof(Segment32
);
1188 const UInt offLen
= offAddr
+ sizeof(UInt32
);
1189 OSWriteBigInt32(vSegList
, offAddr
, (UInt32
) segment
.fIOVMAddr
);
1190 OSWriteBigInt32(vSegList
, offLen
, (UInt32
) segment
.fLength
);
1195 IODMACommand::OutputLittle32(IODMACommand
*,
1196 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1198 const UInt offAddr
= outSegIndex
* sizeof(Segment32
);
1199 const UInt offLen
= offAddr
+ sizeof(UInt32
);
1200 OSWriteLittleInt32(vSegList
, offAddr
, (UInt32
) segment
.fIOVMAddr
);
1201 OSWriteLittleInt32(vSegList
, offLen
, (UInt32
) segment
.fLength
);
1206 IODMACommand::OutputHost64(IODMACommand
*,
1207 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1209 Segment64
*base
= (Segment64
*) vSegList
;
1210 base
[outSegIndex
] = segment
;
1215 IODMACommand::OutputBig64(IODMACommand
*,
1216 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1218 const UInt offAddr
= outSegIndex
* sizeof(Segment64
);
1219 const UInt offLen
= offAddr
+ sizeof(UInt64
);
1220 OSWriteBigInt64(vSegList
, offAddr
, (UInt64
) segment
.fIOVMAddr
);
1221 OSWriteBigInt64(vSegList
, offLen
, (UInt64
) segment
.fLength
);
1226 IODMACommand::OutputLittle64(IODMACommand
*,
1227 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1229 const UInt offAddr
= outSegIndex
* sizeof(Segment64
);
1230 const UInt offLen
= offAddr
+ sizeof(UInt64
);
1231 OSWriteLittleInt64(vSegList
, offAddr
, (UInt64
) segment
.fIOVMAddr
);
1232 OSWriteLittleInt64(vSegList
, offLen
, (UInt64
) segment
.fLength
);