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>
33 #include <libkern/OSDebug.h>
35 #include <IOKit/IOReturn.h>
36 #include <IOKit/IOLib.h>
37 #include <IOKit/IODMACommand.h>
38 #include <IOKit/IOMapper.h>
39 #include <IOKit/IOMemoryDescriptor.h>
40 #include <IOKit/IOBufferMemoryDescriptor.h>
42 #include "IOKitKernelInternal.h"
44 #define MAPTYPE(type) ((UInt) (type) & kTypeMask)
45 #define IS_NONCOHERENT(type) (MAPTYPE(type) == kNonCoherent)
49 kWalkSyncIn
= 0x01, // bounce -> md
50 kWalkSyncOut
= 0x02, // bounce <- md
51 kWalkSyncAlways
= 0x04,
52 kWalkPreflight
= 0x08,
53 kWalkDoubleBuffer
= 0x10,
60 #define fInternalState reserved
61 #define fState reserved->fState
62 #define fMDSummary reserved->fMDSummary
66 // no direction => OutIn
67 #define SHOULD_COPY_DIR(op, direction) \
68 ((kIODirectionNone == (direction)) \
69 || (kWalkSyncAlways & (op)) \
70 || (((kWalkSyncIn & (op)) ? kIODirectionIn : kIODirectionOut) \
74 #define SHOULD_COPY_DIR(state, direction) (true)
78 #define DEBG(fmt, args...) { IOLog(fmt, ## args); kprintf(fmt, ## args); }
80 #define DEBG(fmt, args...) {}
83 /**************************** class IODMACommand ***************************/
86 #define super IOCommand
87 OSDefineMetaClassAndStructors(IODMACommand
, IOCommand
);
89 OSMetaClassDefineReservedUsed(IODMACommand
, 0);
90 OSMetaClassDefineReservedUsed(IODMACommand
, 1);
91 OSMetaClassDefineReservedUsed(IODMACommand
, 2);
92 OSMetaClassDefineReservedUsed(IODMACommand
, 3);
93 OSMetaClassDefineReservedUsed(IODMACommand
, 4);
94 OSMetaClassDefineReservedUsed(IODMACommand
, 5);
95 OSMetaClassDefineReservedUsed(IODMACommand
, 6);
96 OSMetaClassDefineReservedUnused(IODMACommand
, 7);
97 OSMetaClassDefineReservedUnused(IODMACommand
, 8);
98 OSMetaClassDefineReservedUnused(IODMACommand
, 9);
99 OSMetaClassDefineReservedUnused(IODMACommand
, 10);
100 OSMetaClassDefineReservedUnused(IODMACommand
, 11);
101 OSMetaClassDefineReservedUnused(IODMACommand
, 12);
102 OSMetaClassDefineReservedUnused(IODMACommand
, 13);
103 OSMetaClassDefineReservedUnused(IODMACommand
, 14);
104 OSMetaClassDefineReservedUnused(IODMACommand
, 15);
107 IODMACommand::withRefCon(void * refCon
)
109 IODMACommand
* me
= new IODMACommand
;
111 if (me
&& !me
->initWithRefCon(refCon
))
121 IODMACommand::withSpecification(SegmentFunction outSegFunc
,
122 const SegmentOptions
* segmentOptions
,
123 uint32_t mappingOptions
,
127 IODMACommand
* me
= new IODMACommand
;
129 if (me
&& !me
->initWithSpecification(outSegFunc
, segmentOptions
, mappingOptions
,
140 IODMACommand::withSpecification(SegmentFunction outSegFunc
,
141 UInt8 numAddressBits
,
142 UInt64 maxSegmentSize
,
143 MappingOptions mappingOptions
,
144 UInt64 maxTransferSize
,
149 IODMACommand
* me
= new IODMACommand
;
151 if (me
&& !me
->initWithSpecification(outSegFunc
,
152 numAddressBits
, maxSegmentSize
,
153 mappingOptions
, maxTransferSize
,
154 alignment
, mapper
, refCon
))
164 IODMACommand::cloneCommand(void *refCon
)
166 SegmentOptions segmentOptions
=
168 .fStructSize
= sizeof(segmentOptions
),
169 .fNumAddressBits
= fNumAddressBits
,
170 .fMaxSegmentSize
= fMaxSegmentSize
,
171 .fMaxTransferSize
= fMaxTransferSize
,
172 .fAlignment
= fAlignMask
+ 1,
173 .fAlignmentLength
= fAlignMaskInternalSegments
+ 1,
174 .fAlignmentInternalSegments
= fAlignMaskLength
+ 1
177 return (IODMACommand::withSpecification(fOutSeg
, &segmentOptions
,
178 fMappingOptions
, fMapper
, refCon
));
181 #define kLastOutputFunction ((SegmentFunction) kLastOutputFunction)
184 IODMACommand::initWithRefCon(void * refCon
)
186 if (!super::init()) return (false);
190 reserved
= IONew(IODMACommandInternal
, 1);
191 if (!reserved
) return false;
193 bzero(reserved
, sizeof(IODMACommandInternal
));
200 IODMACommand::initWithSpecification(SegmentFunction outSegFunc
,
201 const SegmentOptions
* segmentOptions
,
202 uint32_t mappingOptions
,
206 if (!initWithRefCon(refCon
)) return false;
208 if (kIOReturnSuccess
!= setSpecification(outSegFunc
, segmentOptions
,
209 mappingOptions
, mapper
)) return false;
215 IODMACommand::initWithSpecification(SegmentFunction outSegFunc
,
216 UInt8 numAddressBits
,
217 UInt64 maxSegmentSize
,
218 MappingOptions mappingOptions
,
219 UInt64 maxTransferSize
,
224 SegmentOptions segmentOptions
=
226 .fStructSize
= sizeof(segmentOptions
),
227 .fNumAddressBits
= numAddressBits
,
228 .fMaxSegmentSize
= maxSegmentSize
,
229 .fMaxTransferSize
= maxTransferSize
,
230 .fAlignment
= alignment
,
231 .fAlignmentLength
= 1,
232 .fAlignmentInternalSegments
= alignment
235 return (initWithSpecification(outSegFunc
, &segmentOptions
, mappingOptions
, mapper
, refCon
));
239 IODMACommand::setSpecification(SegmentFunction outSegFunc
,
240 const SegmentOptions
* segmentOptions
,
241 uint32_t mappingOptions
,
244 IOService
* device
= 0;
245 UInt8 numAddressBits
;
246 UInt64 maxSegmentSize
;
247 UInt64 maxTransferSize
;
252 if (!outSegFunc
|| !segmentOptions
) return (kIOReturnBadArgument
);
254 is32Bit
= ((OutputHost32
== outSegFunc
)
255 || (OutputBig32
== outSegFunc
)
256 || (OutputLittle32
== outSegFunc
));
258 numAddressBits
= segmentOptions
->fNumAddressBits
;
259 maxSegmentSize
= segmentOptions
->fMaxSegmentSize
;
260 maxTransferSize
= segmentOptions
->fMaxTransferSize
;
261 alignment
= segmentOptions
->fAlignment
;
266 else if (numAddressBits
> 32)
267 return (kIOReturnBadArgument
); // Wrong output function for bits
270 if (numAddressBits
&& (numAddressBits
< PAGE_SHIFT
)) return (kIOReturnBadArgument
);
272 if (!maxSegmentSize
) maxSegmentSize
--; // Set Max segment to -1
273 if (!maxTransferSize
) maxTransferSize
--; // Set Max transfer to -1
275 if (mapper
&& !OSDynamicCast(IOMapper
, mapper
))
280 if (!mapper
&& (kUnmapped
!= MAPTYPE(mappingOptions
)))
282 IOMapper::checkForSystemMapper();
283 mapper
= IOMapper::gSystem
;
287 fOutSeg
= outSegFunc
;
288 fNumAddressBits
= numAddressBits
;
289 fMaxSegmentSize
= maxSegmentSize
;
290 fMappingOptions
= mappingOptions
;
291 fMaxTransferSize
= maxTransferSize
;
292 if (!alignment
) alignment
= 1;
293 fAlignMask
= alignment
- 1;
295 alignment
= segmentOptions
->fAlignmentLength
;
296 if (!alignment
) alignment
= 1;
297 fAlignMaskLength
= alignment
- 1;
299 alignment
= segmentOptions
->fAlignmentInternalSegments
;
300 if (!alignment
) alignment
= (fAlignMask
+ 1);
301 fAlignMaskInternalSegments
= alignment
- 1;
303 switch (MAPTYPE(mappingOptions
))
306 case kUnmapped
: break;
307 case kNonCoherent
: break;
311 return (kIOReturnBadArgument
);
314 return (kIOReturnBadArgument
);
317 if (mapper
!= fMapper
)
319 if (mapper
) mapper
->retain();
320 if (fMapper
) fMapper
->release();
324 fInternalState
->fIterateOnly
= (0 != (kIterateOnly
& mappingOptions
));
325 fInternalState
->fDevice
= device
;
327 return (kIOReturnSuccess
);
333 if (reserved
) IODelete(reserved
, IODMACommandInternal
, 1);
335 if (fMapper
) fMapper
->release();
341 IODMACommand::setMemoryDescriptor(const IOMemoryDescriptor
*mem
, bool autoPrepare
)
343 IOReturn err
= kIOReturnSuccess
;
352 return kIOReturnSuccess
;
356 // As we are almost certainly being called from a work loop thread
357 // if fActive is true it is probably not a good time to potentially
358 // block. Just test for it and return an error
360 return kIOReturnBusy
;
361 clearMemoryDescriptor();
365 bzero(&fMDSummary
, sizeof(fMDSummary
));
366 err
= mem
->dmaCommandOperation(kIOMDGetCharacteristics
| (kMapped
== MAPTYPE(fMappingOptions
)),
367 &fMDSummary
, sizeof(fMDSummary
));
371 ppnum_t highPage
= fMDSummary
.fHighestPage
? fMDSummary
.fHighestPage
: gIOLastPage
;
373 if ((kMapped
== MAPTYPE(fMappingOptions
))
375 fInternalState
->fCheckAddressing
= false;
377 fInternalState
->fCheckAddressing
= (fNumAddressBits
&& (highPage
>= (1UL << (fNumAddressBits
- PAGE_SHIFT
))));
379 fInternalState
->fNewMD
= true;
383 mem
->dmaCommandOperation(kIOMDSetDMAActive
, this, 0);
387 clearMemoryDescriptor();
396 IODMACommand::clearMemoryDescriptor(bool autoComplete
)
398 if (fActive
&& !autoComplete
)
399 return (kIOReturnNotReady
);
404 fMemory
->dmaCommandOperation(kIOMDSetDMAInactive
, this, 0);
409 return (kIOReturnSuccess
);
412 const IOMemoryDescriptor
*
413 IODMACommand::getMemoryDescriptor() const
419 IODMACommand::getIOMemoryDescriptor() const
421 IOMemoryDescriptor
* mem
;
423 mem
= reserved
->fCopyMD
;
424 if (!mem
) mem
= __IODEQUALIFY(IOMemoryDescriptor
*, fMemory
);
430 IODMACommand::segmentOp(
432 IODMACommand
*target
,
437 IOOptionBits op
= (uintptr_t) reference
;
438 addr64_t maxPhys
, address
;
443 IODMACommandInternal
* state
= target
->reserved
;
445 if (target
->fNumAddressBits
&& (target
->fNumAddressBits
< 64) && (state
->fLocalMapperAlloc
|| !target
->fMapper
))
446 maxPhys
= (1ULL << target
->fNumAddressBits
);
451 address
= segment
.fIOVMAddr
;
452 length
= segment
.fLength
;
457 if (!state
->fMisaligned
)
459 mask
= (segmentIndex
? target
->fAlignMaskInternalSegments
: state
->fSourceAlignMask
);
460 state
->fMisaligned
|= (0 != (mask
& address
));
461 if (state
->fMisaligned
) DEBG("misaligned address %qx:%qx, %x\n", address
, length
, mask
);
463 if (!state
->fMisaligned
)
465 mask
= target
->fAlignMaskLength
;
466 state
->fMisaligned
|= (0 != (mask
& length
));
467 if (state
->fMisaligned
) DEBG("misaligned length %qx:%qx, %x\n", address
, length
, mask
);
470 if (state
->fMisaligned
&& (kWalkPreflight
& op
))
471 return (kIOReturnNotAligned
);
473 if (!state
->fDoubleBuffer
)
475 if ((address
+ length
- 1) <= maxPhys
)
479 else if (address
<= maxPhys
)
481 DEBG("tail %qx, %qx", address
, length
);
482 length
= (address
+ length
- maxPhys
- 1);
483 address
= maxPhys
+ 1;
484 DEBG("-> %qx, %qx\n", address
, length
);
489 return (kIOReturnSuccess
);
491 numPages
= atop_64(round_page_64((address
& PAGE_MASK
) + length
));
493 if (kWalkPreflight
& op
)
495 state
->fCopyPageCount
+= numPages
;
501 if (kWalkPrepare
& op
)
503 lastPage
= state
->fCopyNext
;
504 for (IOItemCount idx
= 0; idx
< numPages
; idx
++)
506 vm_page_set_offset(lastPage
, atop_64(address
) + idx
);
507 lastPage
= vm_page_get_next(lastPage
);
511 if (!lastPage
|| SHOULD_COPY_DIR(op
, target
->fMDSummary
.fDirection
))
513 lastPage
= state
->fCopyNext
;
514 for (IOItemCount idx
= 0; idx
< numPages
; idx
++)
516 if (SHOULD_COPY_DIR(op
, target
->fMDSummary
.fDirection
))
518 addr64_t cpuAddr
= address
;
522 if ((kMapped
== MAPTYPE(target
->fMappingOptions
))
525 cpuAddr
= target
->fMapper
->mapToPhysicalAddress(address
);
528 remapAddr
= ptoa_64(vm_page_get_phys_page(lastPage
));
529 if (!state
->fDoubleBuffer
)
531 remapAddr
+= (address
& PAGE_MASK
);
533 chunk
= PAGE_SIZE
- (address
& PAGE_MASK
);
537 DEBG("cpv: 0x%qx %s 0x%qx, 0x%qx, 0x%02lx\n", remapAddr
,
538 (kWalkSyncIn
& op
) ? "->" : "<-",
541 if (kWalkSyncIn
& op
)
543 copypv(remapAddr
, cpuAddr
, chunk
,
544 cppvPsnk
| cppvFsnk
| cppvPsrc
| cppvNoRefSrc
);
548 copypv(cpuAddr
, remapAddr
, chunk
,
549 cppvPsnk
| cppvFsnk
| cppvPsrc
| cppvNoRefSrc
);
554 lastPage
= vm_page_get_next(lastPage
);
557 state
->fCopyNext
= lastPage
;
560 return kIOReturnSuccess
;
563 IOBufferMemoryDescriptor
*
564 IODMACommand::createCopyBuffer(IODirection direction
, UInt64 length
)
566 mach_vm_address_t mask
= 0xFFFFF000; //state->fSourceAlignMask
567 return (IOBufferMemoryDescriptor::inTaskWithPhysicalMask(kernel_task
,
568 direction
, length
, mask
));
572 IODMACommand::walkAll(UInt8 op
)
574 IODMACommandInternal
* state
= fInternalState
;
576 IOReturn ret
= kIOReturnSuccess
;
580 if (kWalkPreflight
& op
)
582 state
->fMisaligned
= false;
583 state
->fDoubleBuffer
= false;
584 state
->fPrepared
= false;
585 state
->fCopyNext
= NULL
;
586 state
->fCopyPageAlloc
= 0;
587 state
->fCopyPageCount
= 0;
588 state
->fNextRemapPage
= NULL
;
591 if (!(kWalkDoubleBuffer
& op
))
595 ret
= genIOVMSegments(op
, segmentOp
, (void *)(uintptr_t) op
, &offset
, state
, &numSegments
);
598 op
&= ~kWalkPreflight
;
600 state
->fDoubleBuffer
= (state
->fMisaligned
|| (kWalkDoubleBuffer
& op
));
601 if (state
->fDoubleBuffer
)
602 state
->fCopyPageCount
= atop_64(round_page(state
->fPreparedLength
));
604 if (state
->fCopyPageCount
)
606 vm_page_t mapBase
= NULL
;
608 DEBG("preflight fCopyPageCount %d\n", state
->fCopyPageCount
);
610 if (!fMapper
&& !state
->fDoubleBuffer
)
614 if (fMapper
) panic("fMapper copying");
616 kr
= vm_page_alloc_list(state
->fCopyPageCount
,
617 KMA_LOMEM
| KMA_NOPAGEWAIT
, &mapBase
);
618 if (KERN_SUCCESS
!= kr
)
620 DEBG("vm_page_alloc_list(%d) failed (%d)\n", state
->fCopyPageCount
, kr
);
627 state
->fCopyPageAlloc
= mapBase
;
628 state
->fCopyNext
= state
->fCopyPageAlloc
;
631 ret
= genIOVMSegments(op
, segmentOp
, (void *)(uintptr_t) op
, &offset
, state
, &numSegments
);
632 state
->fPrepared
= true;
633 op
&= ~(kWalkSyncIn
| kWalkSyncOut
);
637 DEBG("alloc IOBMD\n");
638 state
->fCopyMD
= createCopyBuffer(fMDSummary
.fDirection
, state
->fPreparedLength
);
642 ret
= kIOReturnSuccess
;
643 state
->fPrepared
= true;
647 DEBG("IODMACommand !alloc IOBMD");
648 return (kIOReturnNoResources
);
654 if (state
->fPrepared
&& ((kWalkSyncIn
| kWalkSyncOut
) & op
))
656 if (state
->fCopyPageCount
)
658 DEBG("sync fCopyPageCount %d\n", state
->fCopyPageCount
);
660 if (state
->fCopyPageAlloc
)
662 state
->fCopyNext
= state
->fCopyPageAlloc
;
665 ret
= genIOVMSegments(op
, segmentOp
, (void *)(uintptr_t) op
, &offset
, state
, &numSegments
);
667 else if (state
->fCopyMD
)
669 DEBG("sync IOBMD\n");
671 if (SHOULD_COPY_DIR(op
, fMDSummary
.fDirection
))
673 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
677 if (kWalkSyncIn
& op
)
678 bytes
= poMD
->writeBytes(state
->fPreparedOffset
,
679 state
->fCopyMD
->getBytesNoCopy(),
680 state
->fPreparedLength
);
682 bytes
= poMD
->readBytes(state
->fPreparedOffset
,
683 state
->fCopyMD
->getBytesNoCopy(),
684 state
->fPreparedLength
);
685 DEBG("fCopyMD %s %lx bytes\n", (kWalkSyncIn
& op
) ? "wrote" : "read", bytes
);
686 ret
= (bytes
== state
->fPreparedLength
) ? kIOReturnSuccess
: kIOReturnUnderrun
;
689 ret
= kIOReturnSuccess
;
694 if (kWalkComplete
& op
)
696 if (state
->fCopyPageAlloc
)
698 vm_page_free_list(state
->fCopyPageAlloc
, FALSE
);
699 state
->fCopyPageAlloc
= 0;
700 state
->fCopyPageCount
= 0;
704 state
->fCopyMD
->release();
708 state
->fPrepared
= false;
714 IODMACommand::getNumAddressBits(void)
716 return (fNumAddressBits
);
720 IODMACommand::getAlignment(void)
722 return (fAlignMask
+ 1);
726 IODMACommand::getAlignmentLength(void)
728 return (fAlignMaskLength
+ 1);
732 IODMACommand::getAlignmentInternalSegments(void)
734 return (fAlignMaskInternalSegments
+ 1);
738 IODMACommand::prepareWithSpecification(SegmentFunction outSegFunc
,
739 const SegmentOptions
* segmentOptions
,
740 uint32_t mappingOptions
,
749 if (fActive
) return kIOReturnNotPermitted
;
751 ret
= setSpecification(outSegFunc
, segmentOptions
, mappingOptions
, mapper
);
752 if (kIOReturnSuccess
!= ret
) return (ret
);
754 ret
= prepare(offset
, length
, flushCache
, synchronize
);
760 IODMACommand::prepareWithSpecification(SegmentFunction outSegFunc
,
761 UInt8 numAddressBits
,
762 UInt64 maxSegmentSize
,
763 MappingOptions mappingOptions
,
764 UInt64 maxTransferSize
,
772 SegmentOptions segmentOptions
=
774 .fStructSize
= sizeof(segmentOptions
),
775 .fNumAddressBits
= numAddressBits
,
776 .fMaxSegmentSize
= maxSegmentSize
,
777 .fMaxTransferSize
= maxTransferSize
,
778 .fAlignment
= alignment
,
779 .fAlignmentLength
= 1,
780 .fAlignmentInternalSegments
= alignment
783 return (prepareWithSpecification(outSegFunc
, &segmentOptions
, mappingOptions
, mapper
,
784 offset
, length
, flushCache
, synchronize
));
789 IODMACommand::prepare(UInt64 offset
, UInt64 length
, bool flushCache
, bool synchronize
)
791 IODMACommandInternal
* state
= fInternalState
;
792 IOReturn ret
= kIOReturnSuccess
;
793 uint32_t mappingOptions
= fMappingOptions
;
795 // check specification has been set
796 if (!fOutSeg
) return (kIOReturnNotReady
);
798 if (!length
) length
= fMDSummary
.fLength
;
800 if (length
> fMaxTransferSize
) return kIOReturnNoSpace
;
804 if ((state
->fPreparedOffset
!= offset
)
805 || (state
->fPreparedLength
!= length
))
806 ret
= kIOReturnNotReady
;
810 if (fAlignMaskLength
& length
) return (kIOReturnNotAligned
);
812 state
->fPreparedOffset
= offset
;
813 state
->fPreparedLength
= length
;
815 state
->fMapContig
= false;
816 state
->fMisaligned
= false;
817 state
->fDoubleBuffer
= false;
818 state
->fPrepared
= false;
819 state
->fCopyNext
= NULL
;
820 state
->fCopyPageAlloc
= 0;
821 state
->fCopyPageCount
= 0;
822 state
->fNextRemapPage
= NULL
;
824 state
->fLocalMapperAlloc
= 0;
825 state
->fLocalMapperAllocLength
= 0;
827 state
->fLocalMapper
= (fMapper
&& (fMapper
!= IOMapper::gSystem
));
829 state
->fSourceAlignMask
= fAlignMask
;
831 state
->fSourceAlignMask
&= page_mask
;
833 state
->fCursor
= state
->fIterateOnly
834 || (!state
->fCheckAddressing
835 && (!state
->fSourceAlignMask
836 || ((fMDSummary
.fPageAlign
& (1 << 31)) && (0 == (fMDSummary
.fPageAlign
& state
->fSourceAlignMask
)))));
840 IOOptionBits op
= kWalkPrepare
| kWalkPreflight
;
846 if (IS_NONCOHERENT(mappingOptions
) && flushCache
)
850 state
->fCopyMD
->performOperation(kIOMemoryIncoherentIOStore
, 0, length
);
854 IOMemoryDescriptor
* md
= const_cast<IOMemoryDescriptor
*>(fMemory
);
855 md
->performOperation(kIOMemoryIncoherentIOStore
, offset
, length
);
861 IOMDDMAMapArgs mapArgs
;
862 bzero(&mapArgs
, sizeof(mapArgs
));
863 mapArgs
.fMapper
= fMapper
;
864 mapArgs
.fCommand
= this;
865 mapArgs
.fMapSpec
.device
= state
->fDevice
;
866 mapArgs
.fMapSpec
.alignment
= fAlignMask
+ 1;
867 mapArgs
.fMapSpec
.numAddressBits
= fNumAddressBits
? fNumAddressBits
: 64;
868 mapArgs
.fLength
= state
->fPreparedLength
;
869 const IOMemoryDescriptor
* md
= state
->fCopyMD
;
870 if (md
) { mapArgs
.fOffset
= 0; }
874 mapArgs
.fOffset
= state
->fPreparedOffset
;
876 ret
= md
->dmaCommandOperation(kIOMDDMAMap
| state
->fIterateOnly
, &mapArgs
, sizeof(mapArgs
));
877 //IOLog("dma %p 0x%x 0x%qx-0x%qx 0x%qx-0x%qx\n", this, ret, state->fPreparedOffset, state->fPreparedLength, mapArgs.fAlloc, mapArgs.fAllocLength);
879 if (kIOReturnSuccess
== ret
)
881 state
->fLocalMapperAlloc
= mapArgs
.fAlloc
;
882 state
->fLocalMapperAllocLength
= mapArgs
.fAllocLength
;
883 state
->fMapContig
= mapArgs
.fMapContig
;
885 if (NULL
!= IOMapper::gSystem
) ret
= kIOReturnSuccess
;
887 if (kIOReturnSuccess
== ret
) state
->fPrepared
= true;
893 IODMACommand::complete(bool invalidateCache
, bool synchronize
)
895 IODMACommandInternal
* state
= fInternalState
;
896 IOReturn ret
= kIOReturnSuccess
;
899 return kIOReturnNotReady
;
903 if (IS_NONCOHERENT(fMappingOptions
) && invalidateCache
)
907 state
->fCopyMD
->performOperation(kIOMemoryIncoherentIOFlush
, 0, state
->fPreparedLength
);
911 IOMemoryDescriptor
* md
= const_cast<IOMemoryDescriptor
*>(fMemory
);
912 md
->performOperation(kIOMemoryIncoherentIOFlush
, state
->fPreparedOffset
, state
->fPreparedLength
);
918 IOOptionBits op
= kWalkComplete
;
923 if (state
->fLocalMapperAlloc
)
925 if (state
->fLocalMapperAllocLength
)
927 fMapper
->iovmUnmapMemory(getIOMemoryDescriptor(), this,
928 state
->fLocalMapperAlloc
, state
->fLocalMapperAllocLength
);
930 state
->fLocalMapperAlloc
= 0;
931 state
->fLocalMapperAllocLength
= 0;
934 state
->fPrepared
= false;
941 IODMACommand::getPreparedOffsetAndLength(UInt64
* offset
, UInt64
* length
)
943 IODMACommandInternal
* state
= fInternalState
;
945 return (kIOReturnNotReady
);
948 *offset
= state
->fPreparedOffset
;
950 *length
= state
->fPreparedLength
;
952 return (kIOReturnSuccess
);
956 IODMACommand::synchronize(IOOptionBits options
)
958 IODMACommandInternal
* state
= fInternalState
;
959 IOReturn ret
= kIOReturnSuccess
;
962 if (kIODirectionOutIn
== (kIODirectionOutIn
& options
))
963 return kIOReturnBadArgument
;
966 return kIOReturnNotReady
;
969 if (kForceDoubleBuffer
& options
)
971 if (state
->fDoubleBuffer
)
972 return kIOReturnSuccess
;
974 state
->fCursor
= false;
976 ret
= walkAll(kWalkComplete
);
978 op
|= kWalkPrepare
| kWalkPreflight
| kWalkDoubleBuffer
;
980 else if (state
->fCursor
)
981 return kIOReturnSuccess
;
983 if (kIODirectionIn
& options
)
984 op
|= kWalkSyncIn
| kWalkSyncAlways
;
985 else if (kIODirectionOut
& options
)
986 op
|= kWalkSyncOut
| kWalkSyncAlways
;
993 struct IODMACommandTransferContext
1002 kIODMACommandTransferOpReadBytes
= 1,
1003 kIODMACommandTransferOpWriteBytes
= 2
1007 IODMACommand::transferSegment(void *reference
,
1008 IODMACommand
*target
,
1011 UInt32 segmentIndex
)
1013 IODMACommandTransferContext
* context
= (IODMACommandTransferContext
*) reference
;
1014 UInt64 length
= min(segment
.fLength
, context
->remaining
);
1015 addr64_t ioAddr
= segment
.fIOVMAddr
;
1016 addr64_t cpuAddr
= ioAddr
;
1018 context
->remaining
-= length
;
1022 UInt64 copyLen
= length
;
1023 if ((kMapped
== MAPTYPE(target
->fMappingOptions
))
1026 cpuAddr
= target
->fMapper
->mapToPhysicalAddress(ioAddr
);
1027 copyLen
= min(copyLen
, page_size
- (ioAddr
& (page_size
- 1)));
1031 switch (context
->op
)
1033 case kIODMACommandTransferOpReadBytes
:
1034 copypv(cpuAddr
, context
->bufferOffset
+ (addr64_t
) context
->buffer
, copyLen
,
1035 cppvPsrc
| cppvNoRefSrc
| cppvFsnk
| cppvKmap
);
1037 case kIODMACommandTransferOpWriteBytes
:
1038 copypv(context
->bufferOffset
+ (addr64_t
) context
->buffer
, cpuAddr
, copyLen
,
1039 cppvPsnk
| cppvFsnk
| cppvNoRefSrc
| cppvNoModSnk
| cppvKmap
);
1043 context
->bufferOffset
+= copyLen
;
1046 return (context
->remaining
? kIOReturnSuccess
: kIOReturnOverrun
);
1050 IODMACommand::transfer(IOOptionBits transferOp
, UInt64 offset
, void * buffer
, UInt64 length
)
1052 IODMACommandInternal
* state
= fInternalState
;
1053 IODMACommandTransferContext context
;
1054 Segment64 segments
[1];
1055 UInt32 numSegments
= 0-1;
1060 if (offset
>= state
->fPreparedLength
)
1062 length
= min(length
, state
->fPreparedLength
- offset
);
1064 context
.buffer
= buffer
;
1065 context
.bufferOffset
= 0;
1066 context
.remaining
= length
;
1067 context
.op
= transferOp
;
1068 (void) genIOVMSegments(kWalkClient
, transferSegment
, &context
, &offset
, &segments
[0], &numSegments
);
1070 return (length
- context
.remaining
);
1074 IODMACommand::readBytes(UInt64 offset
, void *bytes
, UInt64 length
)
1076 return (transfer(kIODMACommandTransferOpReadBytes
, offset
, bytes
, length
));
1080 IODMACommand::writeBytes(UInt64 offset
, const void *bytes
, UInt64 length
)
1082 return (transfer(kIODMACommandTransferOpWriteBytes
, offset
, const_cast<void *>(bytes
), length
));
1086 IODMACommand::genIOVMSegments(UInt64
*offsetP
,
1088 UInt32
*numSegmentsP
)
1090 return (genIOVMSegments(kWalkClient
, clientOutputSegment
, (void *) fOutSeg
,
1091 offsetP
, segmentsP
, numSegmentsP
));
1095 IODMACommand::genIOVMSegments(uint32_t op
,
1096 InternalSegmentFunction outSegFunc
,
1100 UInt32
*numSegmentsP
)
1102 IODMACommandInternal
* internalState
= fInternalState
;
1103 IOOptionBits mdOp
= kIOMDWalkSegments
;
1104 IOReturn ret
= kIOReturnSuccess
;
1106 if (!(kWalkComplete
& op
) && !fActive
)
1107 return kIOReturnNotReady
;
1109 if (!offsetP
|| !segmentsP
|| !numSegmentsP
|| !*numSegmentsP
)
1110 return kIOReturnBadArgument
;
1112 IOMDDMAWalkSegmentArgs
*state
=
1113 (IOMDDMAWalkSegmentArgs
*)(void *) fState
;
1115 UInt64 offset
= *offsetP
+ internalState
->fPreparedOffset
;
1116 UInt64 memLength
= internalState
->fPreparedOffset
+ internalState
->fPreparedLength
;
1118 if (offset
>= memLength
)
1119 return kIOReturnOverrun
;
1121 if ((offset
== internalState
->fPreparedOffset
) || (offset
!= state
->fOffset
) || internalState
->fNewMD
) {
1123 state
->fIOVMAddr
= 0;
1124 internalState
->fNextRemapPage
= NULL
;
1125 internalState
->fNewMD
= false;
1126 state
->fMapped
= (0 != fMapper
);
1127 mdOp
= kIOMDFirstSegment
;
1130 UInt32 segIndex
= 0;
1131 UInt32 numSegments
= *numSegmentsP
;
1132 Segment64 curSeg
= { 0, 0 };
1135 if (fNumAddressBits
&& (fNumAddressBits
< 64))
1136 maxPhys
= (1ULL << fNumAddressBits
);
1141 while (state
->fIOVMAddr
|| (state
->fOffset
< memLength
))
1144 if (!state
->fIOVMAddr
) {
1148 state
->fOffset
= offset
;
1149 state
->fLength
= memLength
- offset
;
1151 if (internalState
->fMapContig
&& internalState
->fLocalMapperAlloc
)
1153 state
->fIOVMAddr
= internalState
->fLocalMapperAlloc
+ offset
;
1154 rtn
= kIOReturnSuccess
;
1157 uint64_t checkOffset
;
1158 IOPhysicalLength segLen
;
1159 for (checkOffset
= 0; checkOffset
< state
->fLength
; )
1161 addr64_t phys
= const_cast<IOMemoryDescriptor
*>(fMemory
)->getPhysicalSegment(checkOffset
+ offset
, &segLen
, kIOMemoryMapperNone
);
1162 if (fMapper
->mapAddr(state
->fIOVMAddr
+ checkOffset
) != phys
)
1164 panic("%llx != %llx:%llx, %llx phys: %llx %llx\n", offset
,
1165 state
->fIOVMAddr
+ checkOffset
, fMapper
->mapAddr(state
->fIOVMAddr
+ checkOffset
), state
->fLength
,
1168 checkOffset
+= page_size
- (phys
& page_mask
);
1175 const IOMemoryDescriptor
* memory
=
1176 internalState
->fCopyMD
? internalState
->fCopyMD
: fMemory
;
1177 rtn
= memory
->dmaCommandOperation(mdOp
, fState
, sizeof(fState
));
1178 mdOp
= kIOMDWalkSegments
;
1181 if (rtn
== kIOReturnSuccess
)
1183 assert(state
->fIOVMAddr
);
1184 assert(state
->fLength
);
1185 if ((curSeg
.fIOVMAddr
+ curSeg
.fLength
) == state
->fIOVMAddr
) {
1186 UInt64 length
= state
->fLength
;
1188 curSeg
.fLength
+= length
;
1189 state
->fIOVMAddr
= 0;
1192 else if (rtn
== kIOReturnOverrun
)
1193 state
->fIOVMAddr
= state
->fLength
= 0; // At end
1198 // seg = state, offset = end of seg
1199 if (!curSeg
.fIOVMAddr
)
1201 UInt64 length
= state
->fLength
;
1203 curSeg
.fIOVMAddr
= state
->fIOVMAddr
;
1204 curSeg
.fLength
= length
;
1205 state
->fIOVMAddr
= 0;
1208 if (!state
->fIOVMAddr
)
1211 if ((kWalkClient
& op
) && (curSeg
.fIOVMAddr
+ curSeg
.fLength
- 1) > maxPhys
)
1213 if (internalState
->fCursor
)
1215 curSeg
.fIOVMAddr
= 0;
1216 ret
= kIOReturnMessageTooLarge
;
1219 else if (curSeg
.fIOVMAddr
<= maxPhys
)
1221 UInt64 remain
, newLength
;
1223 newLength
= (maxPhys
+ 1 - curSeg
.fIOVMAddr
);
1224 DEBG("trunc %qx, %qx-> %qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
, newLength
);
1225 remain
= curSeg
.fLength
- newLength
;
1226 state
->fIOVMAddr
= newLength
+ curSeg
.fIOVMAddr
;
1227 curSeg
.fLength
= newLength
;
1228 state
->fLength
= remain
;
1233 UInt64 addr
= curSeg
.fIOVMAddr
;
1234 ppnum_t addrPage
= atop_64(addr
);
1235 vm_page_t remap
= NULL
;
1236 UInt64 remain
, newLength
;
1238 DEBG("sparse switch %qx, %qx ", addr
, curSeg
.fLength
);
1240 remap
= internalState
->fNextRemapPage
;
1241 if (remap
&& (addrPage
== vm_page_get_offset(remap
)))
1244 else for (remap
= internalState
->fCopyPageAlloc
;
1245 remap
&& (addrPage
!= vm_page_get_offset(remap
));
1246 remap
= vm_page_get_next(remap
))
1250 if (!remap
) panic("no remap page found");
1252 curSeg
.fIOVMAddr
= ptoa_64(vm_page_get_phys_page(remap
))
1253 + (addr
& PAGE_MASK
);
1254 internalState
->fNextRemapPage
= vm_page_get_next(remap
);
1256 newLength
= PAGE_SIZE
- (addr
& PAGE_MASK
);
1257 if (newLength
< curSeg
.fLength
)
1259 remain
= curSeg
.fLength
- newLength
;
1260 state
->fIOVMAddr
= addr
+ newLength
;
1261 curSeg
.fLength
= newLength
;
1262 state
->fLength
= remain
;
1265 DEBG("-> %qx, %qx offset %qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
, offset
);
1269 // reduce size of output segment
1270 uint64_t reduce
, leftover
= 0;
1273 if (curSeg
.fLength
> fMaxSegmentSize
)
1275 leftover
+= curSeg
.fLength
- fMaxSegmentSize
;
1276 curSeg
.fLength
= fMaxSegmentSize
;
1277 state
->fIOVMAddr
= curSeg
.fLength
+ curSeg
.fIOVMAddr
;
1280 // alignment current length
1282 reduce
= (curSeg
.fLength
& fAlignMaskLength
);
1283 if (reduce
&& (curSeg
.fLength
> reduce
))
1286 curSeg
.fLength
-= reduce
;
1287 state
->fIOVMAddr
= curSeg
.fLength
+ curSeg
.fIOVMAddr
;
1290 // alignment next address
1292 reduce
= (state
->fIOVMAddr
& fAlignMaskInternalSegments
);
1293 if (reduce
&& (curSeg
.fLength
> reduce
))
1296 curSeg
.fLength
-= reduce
;
1297 state
->fIOVMAddr
= curSeg
.fLength
+ curSeg
.fIOVMAddr
;
1302 DEBG("reduce seg by 0x%llx @ 0x%llx [0x%llx, 0x%llx]\n",
1304 curSeg
.fIOVMAddr
, curSeg
.fLength
);
1305 state
->fLength
= leftover
;
1311 if (internalState
->fCursor
)
1316 mask
= (segIndex
? fAlignMaskInternalSegments
: internalState
->fSourceAlignMask
);
1317 misaligned
= (0 != (mask
& curSeg
.fIOVMAddr
));
1320 mask
= fAlignMaskLength
;
1321 misaligned
|= (0 != (mask
& curSeg
.fLength
));
1325 if (misaligned
) DEBG("cursor misaligned %qx:%qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
);
1326 curSeg
.fIOVMAddr
= 0;
1327 ret
= kIOReturnNotAligned
;
1332 if (offset
>= memLength
)
1334 curSeg
.fLength
-= (offset
- memLength
);
1336 state
->fIOVMAddr
= state
->fLength
= 0; // At end
1341 if (state
->fIOVMAddr
) {
1342 if ((segIndex
+ 1 == numSegments
))
1345 ret
= (*outSegFunc
)(reference
, this, curSeg
, segmentsP
, segIndex
++);
1346 curSeg
.fIOVMAddr
= 0;
1347 if (kIOReturnSuccess
!= ret
)
1352 if (curSeg
.fIOVMAddr
) {
1353 ret
= (*outSegFunc
)(reference
, this, curSeg
, segmentsP
, segIndex
++);
1356 if (kIOReturnSuccess
== ret
)
1358 state
->fOffset
= offset
;
1359 *offsetP
= offset
- internalState
->fPreparedOffset
;
1360 *numSegmentsP
= segIndex
;
1366 IODMACommand::clientOutputSegment(
1367 void *reference
, IODMACommand
*target
,
1368 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1370 SegmentFunction segmentFunction
= (SegmentFunction
) reference
;
1371 IOReturn ret
= kIOReturnSuccess
;
1373 if (target
->fNumAddressBits
&& (target
->fNumAddressBits
< 64)
1374 && ((segment
.fIOVMAddr
+ segment
.fLength
- 1) >> target
->fNumAddressBits
)
1375 && (target
->reserved
->fLocalMapperAlloc
|| !target
->fMapper
))
1377 DEBG("kIOReturnMessageTooLarge(fNumAddressBits) %qx, %qx\n", segment
.fIOVMAddr
, segment
.fLength
);
1378 ret
= kIOReturnMessageTooLarge
;
1381 if (!(*segmentFunction
)(target
, segment
, vSegList
, outSegIndex
))
1383 DEBG("kIOReturnMessageTooLarge(fOutSeg) %qx, %qx\n", segment
.fIOVMAddr
, segment
.fLength
);
1384 ret
= kIOReturnMessageTooLarge
;
1391 IODMACommand::genIOVMSegments(SegmentFunction segmentFunction
,
1394 UInt32
*numSegmentsP
)
1396 return (genIOVMSegments(kWalkClient
, clientOutputSegment
, (void *) segmentFunction
,
1397 offsetP
, segmentsP
, numSegmentsP
));
1401 IODMACommand::OutputHost32(IODMACommand
*,
1402 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1404 Segment32
*base
= (Segment32
*) vSegList
;
1405 base
[outSegIndex
].fIOVMAddr
= (UInt32
) segment
.fIOVMAddr
;
1406 base
[outSegIndex
].fLength
= (UInt32
) segment
.fLength
;
1411 IODMACommand::OutputBig32(IODMACommand
*,
1412 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1414 const UInt offAddr
= outSegIndex
* sizeof(Segment32
);
1415 const UInt offLen
= offAddr
+ sizeof(UInt32
);
1416 OSWriteBigInt32(vSegList
, offAddr
, (UInt32
) segment
.fIOVMAddr
);
1417 OSWriteBigInt32(vSegList
, offLen
, (UInt32
) segment
.fLength
);
1422 IODMACommand::OutputLittle32(IODMACommand
*,
1423 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1425 const UInt offAddr
= outSegIndex
* sizeof(Segment32
);
1426 const UInt offLen
= offAddr
+ sizeof(UInt32
);
1427 OSWriteLittleInt32(vSegList
, offAddr
, (UInt32
) segment
.fIOVMAddr
);
1428 OSWriteLittleInt32(vSegList
, offLen
, (UInt32
) segment
.fLength
);
1433 IODMACommand::OutputHost64(IODMACommand
*,
1434 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1436 Segment64
*base
= (Segment64
*) vSegList
;
1437 base
[outSegIndex
] = segment
;
1442 IODMACommand::OutputBig64(IODMACommand
*,
1443 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1445 const UInt offAddr
= outSegIndex
* sizeof(Segment64
);
1446 const UInt offLen
= offAddr
+ sizeof(UInt64
);
1447 OSWriteBigInt64(vSegList
, offAddr
, (UInt64
) segment
.fIOVMAddr
);
1448 OSWriteBigInt64(vSegList
, offLen
, (UInt64
) segment
.fLength
);
1453 IODMACommand::OutputLittle64(IODMACommand
*,
1454 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1456 const UInt offAddr
= outSegIndex
* sizeof(Segment64
);
1457 const UInt offLen
= offAddr
+ sizeof(UInt64
);
1458 OSWriteLittleInt64(vSegList
, offAddr
, (UInt64
) segment
.fIOVMAddr
);
1459 OSWriteLittleInt64(vSegList
, offLen
, (UInt64
) segment
.fLength
);