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
= (uint8_t)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;
382 if (!fMapper
) mem
->dmaCommandOperation(kIOMDSetDMAActive
, this, 0);
386 clearMemoryDescriptor();
395 IODMACommand::clearMemoryDescriptor(bool autoComplete
)
397 if (fActive
&& !autoComplete
) return (kIOReturnNotReady
);
401 while (fActive
) complete();
402 if (!fMapper
) fMemory
->dmaCommandOperation(kIOMDSetDMAInactive
, this, 0);
407 return (kIOReturnSuccess
);
410 const IOMemoryDescriptor
*
411 IODMACommand::getMemoryDescriptor() const
417 IODMACommand::getIOMemoryDescriptor() const
419 IOMemoryDescriptor
* mem
;
421 mem
= reserved
->fCopyMD
;
422 if (!mem
) mem
= __IODEQUALIFY(IOMemoryDescriptor
*, fMemory
);
428 IODMACommand::segmentOp(
430 IODMACommand
*target
,
435 IOOptionBits op
= (uintptr_t) reference
;
436 addr64_t maxPhys
, address
;
441 IODMACommandInternal
* state
= target
->reserved
;
443 if (target
->fNumAddressBits
&& (target
->fNumAddressBits
< 64) && (state
->fLocalMapperAllocValid
|| !target
->fMapper
))
444 maxPhys
= (1ULL << target
->fNumAddressBits
);
449 address
= segment
.fIOVMAddr
;
450 length
= segment
.fLength
;
454 if (!state
->fMisaligned
)
456 mask
= (segmentIndex
? target
->fAlignMaskInternalSegments
: state
->fSourceAlignMask
);
457 state
->fMisaligned
|= (0 != (mask
& address
));
458 if (state
->fMisaligned
) DEBG("misaligned address %qx:%qx, %x\n", address
, length
, mask
);
460 if (!state
->fMisaligned
)
462 mask
= target
->fAlignMaskLength
;
463 state
->fMisaligned
|= (0 != (mask
& length
));
464 if (state
->fMisaligned
) DEBG("misaligned length %qx:%qx, %x\n", address
, length
, mask
);
467 if (state
->fMisaligned
&& (kWalkPreflight
& op
))
468 return (kIOReturnNotAligned
);
470 if (!state
->fDoubleBuffer
)
472 if ((address
+ length
- 1) <= maxPhys
)
476 else if (address
<= maxPhys
)
478 DEBG("tail %qx, %qx", address
, length
);
479 length
= (address
+ length
- maxPhys
- 1);
480 address
= maxPhys
+ 1;
481 DEBG("-> %qx, %qx\n", address
, length
);
486 return (kIOReturnSuccess
);
488 numPages
= atop_64(round_page_64((address
& PAGE_MASK
) + length
));
490 if (kWalkPreflight
& op
)
492 state
->fCopyPageCount
+= numPages
;
498 if (kWalkPrepare
& op
)
500 lastPage
= state
->fCopyNext
;
501 for (IOItemCount idx
= 0; idx
< numPages
; idx
++)
503 vm_page_set_offset(lastPage
, atop_64(address
) + idx
);
504 lastPage
= vm_page_get_next(lastPage
);
508 if (!lastPage
|| SHOULD_COPY_DIR(op
, target
->fMDSummary
.fDirection
))
510 lastPage
= state
->fCopyNext
;
511 for (IOItemCount idx
= 0; idx
< numPages
; idx
++)
513 if (SHOULD_COPY_DIR(op
, target
->fMDSummary
.fDirection
))
515 addr64_t cpuAddr
= address
;
519 if ((kMapped
== MAPTYPE(target
->fMappingOptions
))
522 cpuAddr
= target
->fMapper
->mapToPhysicalAddress(address
);
525 remapAddr
= ptoa_64(vm_page_get_phys_page(lastPage
));
526 if (!state
->fDoubleBuffer
)
528 remapAddr
+= (address
& PAGE_MASK
);
530 chunk
= PAGE_SIZE
- (address
& PAGE_MASK
);
534 DEBG("cpv: 0x%qx %s 0x%qx, 0x%qx, 0x%02lx\n", remapAddr
,
535 (kWalkSyncIn
& op
) ? "->" : "<-",
538 if (kWalkSyncIn
& op
)
540 copypv(remapAddr
, cpuAddr
, chunk
,
541 cppvPsnk
| cppvFsnk
| cppvPsrc
| cppvNoRefSrc
);
545 copypv(cpuAddr
, remapAddr
, chunk
,
546 cppvPsnk
| cppvFsnk
| cppvPsrc
| cppvNoRefSrc
);
551 lastPage
= vm_page_get_next(lastPage
);
554 state
->fCopyNext
= lastPage
;
557 return kIOReturnSuccess
;
560 IOBufferMemoryDescriptor
*
561 IODMACommand::createCopyBuffer(IODirection direction
, UInt64 length
)
563 mach_vm_address_t mask
= 0xFFFFF000; //state->fSourceAlignMask
564 return (IOBufferMemoryDescriptor::inTaskWithPhysicalMask(kernel_task
,
565 direction
, length
, mask
));
569 IODMACommand::walkAll(UInt8 op
)
571 IODMACommandInternal
* state
= fInternalState
;
573 IOReturn ret
= kIOReturnSuccess
;
577 if (kWalkPreflight
& op
)
579 state
->fMisaligned
= false;
580 state
->fDoubleBuffer
= false;
581 state
->fPrepared
= false;
582 state
->fCopyNext
= NULL
;
583 state
->fCopyPageAlloc
= 0;
584 state
->fCopyPageCount
= 0;
585 state
->fNextRemapPage
= NULL
;
588 if (!(kWalkDoubleBuffer
& op
))
592 ret
= genIOVMSegments(op
, segmentOp
, (void *)(uintptr_t) op
, &offset
, state
, &numSegments
);
595 op
&= ~kWalkPreflight
;
597 state
->fDoubleBuffer
= (state
->fMisaligned
|| state
->fForceDoubleBuffer
);
598 state
->fForceDoubleBuffer
= false;
599 if (state
->fDoubleBuffer
)
600 state
->fCopyPageCount
= atop_64(round_page(state
->fPreparedLength
));
602 if (state
->fCopyPageCount
)
604 vm_page_t mapBase
= NULL
;
606 DEBG("preflight fCopyPageCount %d\n", state
->fCopyPageCount
);
608 if (!fMapper
&& !state
->fDoubleBuffer
)
612 if (fMapper
) panic("fMapper copying");
614 kr
= vm_page_alloc_list(state
->fCopyPageCount
,
615 KMA_LOMEM
| KMA_NOPAGEWAIT
, &mapBase
);
616 if (KERN_SUCCESS
!= kr
)
618 DEBG("vm_page_alloc_list(%d) failed (%d)\n", state
->fCopyPageCount
, kr
);
625 state
->fCopyPageAlloc
= mapBase
;
626 state
->fCopyNext
= state
->fCopyPageAlloc
;
629 ret
= genIOVMSegments(op
, segmentOp
, (void *)(uintptr_t) op
, &offset
, state
, &numSegments
);
630 state
->fPrepared
= true;
631 op
&= ~(kWalkSyncIn
| kWalkSyncOut
);
635 DEBG("alloc IOBMD\n");
636 state
->fCopyMD
= createCopyBuffer(fMDSummary
.fDirection
, state
->fPreparedLength
);
640 ret
= kIOReturnSuccess
;
641 state
->fPrepared
= true;
645 DEBG("IODMACommand !alloc IOBMD");
646 return (kIOReturnNoResources
);
652 if (state
->fPrepared
&& ((kWalkSyncIn
| kWalkSyncOut
) & op
))
654 if (state
->fCopyPageCount
)
656 DEBG("sync fCopyPageCount %d\n", state
->fCopyPageCount
);
658 if (state
->fCopyPageAlloc
)
660 state
->fCopyNext
= state
->fCopyPageAlloc
;
663 ret
= genIOVMSegments(op
, segmentOp
, (void *)(uintptr_t) op
, &offset
, state
, &numSegments
);
665 else if (state
->fCopyMD
)
667 DEBG("sync IOBMD\n");
669 if (SHOULD_COPY_DIR(op
, fMDSummary
.fDirection
))
671 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
675 if (kWalkSyncIn
& op
)
676 bytes
= poMD
->writeBytes(state
->fPreparedOffset
,
677 state
->fCopyMD
->getBytesNoCopy(),
678 state
->fPreparedLength
);
680 bytes
= poMD
->readBytes(state
->fPreparedOffset
,
681 state
->fCopyMD
->getBytesNoCopy(),
682 state
->fPreparedLength
);
683 DEBG("fCopyMD %s %lx bytes\n", (kWalkSyncIn
& op
) ? "wrote" : "read", bytes
);
684 ret
= (bytes
== state
->fPreparedLength
) ? kIOReturnSuccess
: kIOReturnUnderrun
;
687 ret
= kIOReturnSuccess
;
692 if (kWalkComplete
& op
)
694 if (state
->fCopyPageAlloc
)
696 vm_page_free_list(state
->fCopyPageAlloc
, FALSE
);
697 state
->fCopyPageAlloc
= 0;
698 state
->fCopyPageCount
= 0;
702 state
->fCopyMD
->release();
706 state
->fPrepared
= false;
712 IODMACommand::getNumAddressBits(void)
714 return (fNumAddressBits
);
718 IODMACommand::getAlignment(void)
720 return (fAlignMask
+ 1);
724 IODMACommand::getAlignmentLength(void)
726 return (fAlignMaskLength
+ 1);
730 IODMACommand::getAlignmentInternalSegments(void)
732 return (fAlignMaskInternalSegments
+ 1);
736 IODMACommand::prepareWithSpecification(SegmentFunction outSegFunc
,
737 const SegmentOptions
* segmentOptions
,
738 uint32_t mappingOptions
,
747 if (fActive
) return kIOReturnNotPermitted
;
749 ret
= setSpecification(outSegFunc
, segmentOptions
, mappingOptions
, mapper
);
750 if (kIOReturnSuccess
!= ret
) return (ret
);
752 ret
= prepare(offset
, length
, flushCache
, synchronize
);
758 IODMACommand::prepareWithSpecification(SegmentFunction outSegFunc
,
759 UInt8 numAddressBits
,
760 UInt64 maxSegmentSize
,
761 MappingOptions mappingOptions
,
762 UInt64 maxTransferSize
,
770 SegmentOptions segmentOptions
=
772 .fStructSize
= sizeof(segmentOptions
),
773 .fNumAddressBits
= numAddressBits
,
774 .fMaxSegmentSize
= maxSegmentSize
,
775 .fMaxTransferSize
= maxTransferSize
,
776 .fAlignment
= alignment
,
777 .fAlignmentLength
= 1,
778 .fAlignmentInternalSegments
= alignment
781 return (prepareWithSpecification(outSegFunc
, &segmentOptions
, mappingOptions
, mapper
,
782 offset
, length
, flushCache
, synchronize
));
787 IODMACommand::prepare(UInt64 offset
, UInt64 length
, bool flushCache
, bool synchronize
)
789 IODMACommandInternal
* state
= fInternalState
;
790 IOReturn ret
= kIOReturnSuccess
;
791 uint32_t mappingOptions
= fMappingOptions
;
793 // check specification has been set
794 if (!fOutSeg
) return (kIOReturnNotReady
);
796 if (!length
) length
= fMDSummary
.fLength
;
798 if (length
> fMaxTransferSize
) return kIOReturnNoSpace
;
802 if ((state
->fPreparedOffset
!= offset
)
803 || (state
->fPreparedLength
!= length
))
804 ret
= kIOReturnNotReady
;
808 if (fAlignMaskLength
& length
) return (kIOReturnNotAligned
);
810 state
->fPreparedOffset
= offset
;
811 state
->fPreparedLength
= length
;
813 state
->fMapContig
= false;
814 state
->fMisaligned
= false;
815 state
->fDoubleBuffer
= false;
816 state
->fPrepared
= false;
817 state
->fCopyNext
= NULL
;
818 state
->fCopyPageAlloc
= 0;
819 state
->fCopyPageCount
= 0;
820 state
->fNextRemapPage
= NULL
;
822 state
->fLocalMapperAlloc
= 0;
823 state
->fLocalMapperAllocValid
= false;
824 state
->fLocalMapperAllocLength
= 0;
826 state
->fLocalMapper
= (fMapper
&& (fMapper
!= IOMapper::gSystem
));
828 state
->fSourceAlignMask
= fAlignMask
;
830 state
->fSourceAlignMask
&= page_mask
;
832 state
->fCursor
= state
->fIterateOnly
833 || (!state
->fCheckAddressing
834 && (!state
->fSourceAlignMask
835 || ((fMDSummary
.fPageAlign
& (1 << 31)) && (0 == (fMDSummary
.fPageAlign
& state
->fSourceAlignMask
)))));
839 IOOptionBits op
= kWalkPrepare
| kWalkPreflight
;
845 if (IS_NONCOHERENT(mappingOptions
) && flushCache
)
849 state
->fCopyMD
->performOperation(kIOMemoryIncoherentIOStore
, 0, length
);
853 IOMemoryDescriptor
* md
= const_cast<IOMemoryDescriptor
*>(fMemory
);
854 md
->performOperation(kIOMemoryIncoherentIOStore
, offset
, length
);
860 IOMDDMAMapArgs mapArgs
;
861 bzero(&mapArgs
, sizeof(mapArgs
));
862 mapArgs
.fMapper
= fMapper
;
863 mapArgs
.fCommand
= this;
864 mapArgs
.fMapSpec
.device
= state
->fDevice
;
865 mapArgs
.fMapSpec
.alignment
= fAlignMask
+ 1;
866 mapArgs
.fMapSpec
.numAddressBits
= fNumAddressBits
? fNumAddressBits
: 64;
867 mapArgs
.fLength
= state
->fPreparedLength
;
868 const IOMemoryDescriptor
* md
= state
->fCopyMD
;
869 if (md
) { mapArgs
.fOffset
= 0; } else
872 mapArgs
.fOffset
= state
->fPreparedOffset
;
874 ret
= md
->dmaCommandOperation(kIOMDDMAMap
| state
->fIterateOnly
, &mapArgs
, sizeof(mapArgs
));
875 //IOLog("dma %p 0x%x 0x%qx-0x%qx 0x%qx-0x%qx\n", this, ret, state->fPreparedOffset, state->fPreparedLength, mapArgs.fAlloc, mapArgs.fAllocLength);
877 if (kIOReturnSuccess
== ret
)
879 state
->fLocalMapperAlloc
= mapArgs
.fAlloc
;
880 state
->fLocalMapperAllocValid
= true;
881 state
->fLocalMapperAllocLength
= mapArgs
.fAllocLength
;
882 state
->fMapContig
= mapArgs
.fMapContig
;
884 if (NULL
!= IOMapper::gSystem
) ret
= kIOReturnSuccess
;
886 if (kIOReturnSuccess
== ret
) state
->fPrepared
= true;
892 IODMACommand::complete(bool invalidateCache
, bool synchronize
)
894 IODMACommandInternal
* state
= fInternalState
;
895 IOReturn ret
= kIOReturnSuccess
;
896 IOMemoryDescriptor
* copyMD
;
899 return kIOReturnNotReady
;
903 copyMD
= state
->fCopyMD
;
904 if (copyMD
) copyMD
->retain();
906 if (IS_NONCOHERENT(fMappingOptions
) && invalidateCache
)
910 copyMD
->performOperation(kIOMemoryIncoherentIOFlush
, 0, state
->fPreparedLength
);
914 IOMemoryDescriptor
* md
= const_cast<IOMemoryDescriptor
*>(fMemory
);
915 md
->performOperation(kIOMemoryIncoherentIOFlush
, state
->fPreparedOffset
, state
->fPreparedLength
);
921 IOOptionBits op
= kWalkComplete
;
927 if (state
->fLocalMapperAllocValid
)
929 IOMDDMAMapArgs mapArgs
;
930 bzero(&mapArgs
, sizeof(mapArgs
));
931 mapArgs
.fMapper
= fMapper
;
932 mapArgs
.fCommand
= this;
933 mapArgs
.fAlloc
= state
->fLocalMapperAlloc
;
934 mapArgs
.fAllocLength
= state
->fLocalMapperAllocLength
;
935 const IOMemoryDescriptor
* md
= copyMD
;
936 if (md
) { mapArgs
.fOffset
= 0; }
940 mapArgs
.fOffset
= state
->fPreparedOffset
;
943 ret
= md
->dmaCommandOperation(kIOMDDMAUnmap
, &mapArgs
, sizeof(mapArgs
));
945 state
->fLocalMapperAlloc
= 0;
946 state
->fLocalMapperAllocValid
= false;
947 state
->fLocalMapperAllocLength
= 0;
949 if (copyMD
) copyMD
->release();
950 state
->fPrepared
= false;
957 IODMACommand::getPreparedOffsetAndLength(UInt64
* offset
, UInt64
* length
)
959 IODMACommandInternal
* state
= fInternalState
;
961 return (kIOReturnNotReady
);
964 *offset
= state
->fPreparedOffset
;
966 *length
= state
->fPreparedLength
;
968 return (kIOReturnSuccess
);
972 IODMACommand::synchronize(IOOptionBits options
)
974 IODMACommandInternal
* state
= fInternalState
;
975 IOReturn ret
= kIOReturnSuccess
;
978 if (kIODirectionOutIn
== (kIODirectionOutIn
& options
))
979 return kIOReturnBadArgument
;
982 return kIOReturnNotReady
;
985 if (kForceDoubleBuffer
& options
)
987 if (state
->fDoubleBuffer
) return kIOReturnSuccess
;
988 ret
= complete(false /* invalidateCache */, true /* synchronize */);
989 state
->fCursor
= false;
990 state
->fForceDoubleBuffer
= true;
991 ret
= prepare(state
->fPreparedOffset
, state
->fPreparedLength
, false /* flushCache */, true /* synchronize */);
995 else if (state
->fCursor
)
996 return kIOReturnSuccess
;
998 if (kIODirectionIn
& options
)
999 op
|= kWalkSyncIn
| kWalkSyncAlways
;
1000 else if (kIODirectionOut
& options
)
1001 op
|= kWalkSyncOut
| kWalkSyncAlways
;
1008 struct IODMACommandTransferContext
1011 UInt64 bufferOffset
;
1017 kIODMACommandTransferOpReadBytes
= 1,
1018 kIODMACommandTransferOpWriteBytes
= 2
1022 IODMACommand::transferSegment(void *reference
,
1023 IODMACommand
*target
,
1026 UInt32 segmentIndex
)
1028 IODMACommandTransferContext
* context
= (IODMACommandTransferContext
*) reference
;
1029 UInt64 length
= min(segment
.fLength
, context
->remaining
);
1030 addr64_t ioAddr
= segment
.fIOVMAddr
;
1031 addr64_t cpuAddr
= ioAddr
;
1033 context
->remaining
-= length
;
1037 UInt64 copyLen
= length
;
1038 if ((kMapped
== MAPTYPE(target
->fMappingOptions
))
1041 cpuAddr
= target
->fMapper
->mapToPhysicalAddress(ioAddr
);
1042 copyLen
= min(copyLen
, page_size
- (ioAddr
& (page_size
- 1)));
1046 switch (context
->op
)
1048 case kIODMACommandTransferOpReadBytes
:
1049 copypv(cpuAddr
, context
->bufferOffset
+ (addr64_t
) context
->buffer
, copyLen
,
1050 cppvPsrc
| cppvNoRefSrc
| cppvFsnk
| cppvKmap
);
1052 case kIODMACommandTransferOpWriteBytes
:
1053 copypv(context
->bufferOffset
+ (addr64_t
) context
->buffer
, cpuAddr
, copyLen
,
1054 cppvPsnk
| cppvFsnk
| cppvNoRefSrc
| cppvNoModSnk
| cppvKmap
);
1058 context
->bufferOffset
+= copyLen
;
1061 return (context
->remaining
? kIOReturnSuccess
: kIOReturnOverrun
);
1065 IODMACommand::transfer(IOOptionBits transferOp
, UInt64 offset
, void * buffer
, UInt64 length
)
1067 IODMACommandInternal
* state
= fInternalState
;
1068 IODMACommandTransferContext context
;
1069 Segment64 segments
[1];
1070 UInt32 numSegments
= 0-1;
1075 if (offset
>= state
->fPreparedLength
)
1077 length
= min(length
, state
->fPreparedLength
- offset
);
1079 context
.buffer
= buffer
;
1080 context
.bufferOffset
= 0;
1081 context
.remaining
= length
;
1082 context
.op
= transferOp
;
1083 (void) genIOVMSegments(kWalkClient
, transferSegment
, &context
, &offset
, &segments
[0], &numSegments
);
1085 return (length
- context
.remaining
);
1089 IODMACommand::readBytes(UInt64 offset
, void *bytes
, UInt64 length
)
1091 return (transfer(kIODMACommandTransferOpReadBytes
, offset
, bytes
, length
));
1095 IODMACommand::writeBytes(UInt64 offset
, const void *bytes
, UInt64 length
)
1097 return (transfer(kIODMACommandTransferOpWriteBytes
, offset
, const_cast<void *>(bytes
), length
));
1101 IODMACommand::genIOVMSegments(UInt64
*offsetP
,
1103 UInt32
*numSegmentsP
)
1105 return (genIOVMSegments(kWalkClient
, clientOutputSegment
, (void *) fOutSeg
,
1106 offsetP
, segmentsP
, numSegmentsP
));
1110 IODMACommand::genIOVMSegments(uint32_t op
,
1111 InternalSegmentFunction outSegFunc
,
1115 UInt32
*numSegmentsP
)
1117 IODMACommandInternal
* internalState
= fInternalState
;
1118 IOOptionBits mdOp
= kIOMDWalkSegments
;
1119 IOReturn ret
= kIOReturnSuccess
;
1121 if (!(kWalkComplete
& op
) && !fActive
)
1122 return kIOReturnNotReady
;
1124 if (!offsetP
|| !segmentsP
|| !numSegmentsP
|| !*numSegmentsP
)
1125 return kIOReturnBadArgument
;
1127 IOMDDMAWalkSegmentArgs
*state
=
1128 (IOMDDMAWalkSegmentArgs
*)(void *) fState
;
1130 UInt64 offset
= *offsetP
+ internalState
->fPreparedOffset
;
1131 UInt64 memLength
= internalState
->fPreparedOffset
+ internalState
->fPreparedLength
;
1133 if (offset
>= memLength
)
1134 return kIOReturnOverrun
;
1136 if ((offset
== internalState
->fPreparedOffset
) || (offset
!= state
->fOffset
) || internalState
->fNewMD
) {
1138 internalState
->fIOVMAddrValid
= state
->fIOVMAddr
= 0;
1139 internalState
->fNextRemapPage
= NULL
;
1140 internalState
->fNewMD
= false;
1141 mdOp
= kIOMDFirstSegment
;
1144 if (internalState
->fLocalMapperAllocValid
)
1146 state
->fMapped
= kIOMDDMAWalkMappedLocal
;
1147 state
->fMappedBase
= internalState
->fLocalMapperAlloc
;
1149 else state
->fMapped
= true;
1153 UInt32 segIndex
= 0;
1154 UInt32 numSegments
= *numSegmentsP
;
1155 Segment64 curSeg
= { 0, 0 };
1156 bool curSegValid
= false;
1159 if (fNumAddressBits
&& (fNumAddressBits
< 64))
1160 maxPhys
= (1ULL << fNumAddressBits
);
1165 while (internalState
->fIOVMAddrValid
|| (state
->fOffset
< memLength
))
1168 if (!internalState
->fIOVMAddrValid
) {
1172 state
->fOffset
= offset
;
1173 state
->fLength
= memLength
- offset
;
1175 if (internalState
->fMapContig
&& internalState
->fLocalMapperAllocValid
)
1177 state
->fIOVMAddr
= internalState
->fLocalMapperAlloc
+ offset
- internalState
->fPreparedOffset
;
1178 rtn
= kIOReturnSuccess
;
1181 uint64_t checkOffset
;
1182 IOPhysicalLength segLen
;
1183 for (checkOffset
= 0; checkOffset
< state
->fLength
; )
1185 addr64_t phys
= const_cast<IOMemoryDescriptor
*>(fMemory
)->getPhysicalSegment(checkOffset
+ offset
, &segLen
, kIOMemoryMapperNone
);
1186 if (fMapper
->mapAddr(state
->fIOVMAddr
+ checkOffset
) != phys
)
1188 panic("%llx != %llx:%llx, %llx phys: %llx %llx\n", offset
,
1189 state
->fIOVMAddr
+ checkOffset
, fMapper
->mapAddr(state
->fIOVMAddr
+ checkOffset
), state
->fLength
,
1192 checkOffset
+= page_size
- (phys
& page_mask
);
1199 const IOMemoryDescriptor
* memory
=
1200 internalState
->fCopyMD
? internalState
->fCopyMD
: fMemory
;
1201 rtn
= memory
->dmaCommandOperation(mdOp
, fState
, sizeof(fState
));
1202 mdOp
= kIOMDWalkSegments
;
1205 if (rtn
== kIOReturnSuccess
)
1207 internalState
->fIOVMAddrValid
= true;
1208 assert(state
->fLength
);
1209 if (curSegValid
&& ((curSeg
.fIOVMAddr
+ curSeg
.fLength
) == state
->fIOVMAddr
)) {
1210 UInt64 length
= state
->fLength
;
1212 curSeg
.fLength
+= length
;
1213 internalState
->fIOVMAddrValid
= state
->fIOVMAddr
= 0;
1216 else if (rtn
== kIOReturnOverrun
)
1217 internalState
->fIOVMAddrValid
= state
->fIOVMAddr
= state
->fLength
= 0; // At end
1222 // seg = state, offset = end of seg
1225 UInt64 length
= state
->fLength
;
1227 curSeg
.fIOVMAddr
= state
->fIOVMAddr
;
1228 curSeg
.fLength
= length
;
1230 internalState
->fIOVMAddrValid
= state
->fIOVMAddr
= 0;
1233 if (!internalState
->fIOVMAddrValid
)
1236 if ((kWalkClient
& op
) && (curSeg
.fIOVMAddr
+ curSeg
.fLength
- 1) > maxPhys
)
1238 if (internalState
->fCursor
)
1240 curSegValid
= curSeg
.fIOVMAddr
= 0;
1241 ret
= kIOReturnMessageTooLarge
;
1244 else if (curSeg
.fIOVMAddr
<= maxPhys
)
1246 UInt64 remain
, newLength
;
1248 newLength
= (maxPhys
+ 1 - curSeg
.fIOVMAddr
);
1249 DEBG("trunc %qx, %qx-> %qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
, newLength
);
1250 remain
= curSeg
.fLength
- newLength
;
1251 state
->fIOVMAddr
= newLength
+ curSeg
.fIOVMAddr
;
1252 internalState
->fIOVMAddrValid
= true;
1253 curSeg
.fLength
= newLength
;
1254 state
->fLength
= remain
;
1259 UInt64 addr
= curSeg
.fIOVMAddr
;
1260 ppnum_t addrPage
= atop_64(addr
);
1261 vm_page_t remap
= NULL
;
1262 UInt64 remain
, newLength
;
1264 DEBG("sparse switch %qx, %qx ", addr
, curSeg
.fLength
);
1266 remap
= internalState
->fNextRemapPage
;
1267 if (remap
&& (addrPage
== vm_page_get_offset(remap
)))
1270 else for (remap
= internalState
->fCopyPageAlloc
;
1271 remap
&& (addrPage
!= vm_page_get_offset(remap
));
1272 remap
= vm_page_get_next(remap
))
1276 if (!remap
) panic("no remap page found");
1278 curSeg
.fIOVMAddr
= ptoa_64(vm_page_get_phys_page(remap
))
1279 + (addr
& PAGE_MASK
);
1281 internalState
->fNextRemapPage
= vm_page_get_next(remap
);
1283 newLength
= PAGE_SIZE
- (addr
& PAGE_MASK
);
1284 if (newLength
< curSeg
.fLength
)
1286 remain
= curSeg
.fLength
- newLength
;
1287 state
->fIOVMAddr
= addr
+ newLength
;
1288 internalState
->fIOVMAddrValid
= true;
1289 curSeg
.fLength
= newLength
;
1290 state
->fLength
= remain
;
1293 DEBG("-> %qx, %qx offset %qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
, offset
);
1297 // reduce size of output segment
1298 uint64_t reduce
, leftover
= 0;
1301 if (curSeg
.fLength
> fMaxSegmentSize
)
1303 leftover
+= curSeg
.fLength
- fMaxSegmentSize
;
1304 curSeg
.fLength
= fMaxSegmentSize
;
1305 state
->fIOVMAddr
= curSeg
.fLength
+ curSeg
.fIOVMAddr
;
1306 internalState
->fIOVMAddrValid
= true;
1309 // alignment current length
1311 reduce
= (curSeg
.fLength
& fAlignMaskLength
);
1312 if (reduce
&& (curSeg
.fLength
> reduce
))
1315 curSeg
.fLength
-= reduce
;
1316 state
->fIOVMAddr
= curSeg
.fLength
+ curSeg
.fIOVMAddr
;
1317 internalState
->fIOVMAddrValid
= true;
1320 // alignment next address
1322 reduce
= (state
->fIOVMAddr
& fAlignMaskInternalSegments
);
1323 if (reduce
&& (curSeg
.fLength
> reduce
))
1326 curSeg
.fLength
-= reduce
;
1327 state
->fIOVMAddr
= curSeg
.fLength
+ curSeg
.fIOVMAddr
;
1328 internalState
->fIOVMAddrValid
= true;
1333 DEBG("reduce seg by 0x%llx @ 0x%llx [0x%llx, 0x%llx]\n",
1335 curSeg
.fIOVMAddr
, curSeg
.fLength
);
1336 state
->fLength
= leftover
;
1342 if (internalState
->fCursor
)
1347 mask
= (segIndex
? fAlignMaskInternalSegments
: internalState
->fSourceAlignMask
);
1348 misaligned
= (0 != (mask
& curSeg
.fIOVMAddr
));
1351 mask
= fAlignMaskLength
;
1352 misaligned
|= (0 != (mask
& curSeg
.fLength
));
1356 if (misaligned
) DEBG("cursor misaligned %qx:%qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
);
1357 curSegValid
= curSeg
.fIOVMAddr
= 0;
1358 ret
= kIOReturnNotAligned
;
1363 if (offset
>= memLength
)
1365 curSeg
.fLength
-= (offset
- memLength
);
1367 internalState
->fIOVMAddrValid
= state
->fIOVMAddr
= state
->fLength
= 0; // At end
1372 if (internalState
->fIOVMAddrValid
) {
1373 if ((segIndex
+ 1 == numSegments
))
1376 ret
= (*outSegFunc
)(reference
, this, curSeg
, segmentsP
, segIndex
++);
1377 curSegValid
= curSeg
.fIOVMAddr
= 0;
1378 if (kIOReturnSuccess
!= ret
)
1384 ret
= (*outSegFunc
)(reference
, this, curSeg
, segmentsP
, segIndex
++);
1387 if (kIOReturnSuccess
== ret
)
1389 state
->fOffset
= offset
;
1390 *offsetP
= offset
- internalState
->fPreparedOffset
;
1391 *numSegmentsP
= segIndex
;
1397 IODMACommand::clientOutputSegment(
1398 void *reference
, IODMACommand
*target
,
1399 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1401 SegmentFunction segmentFunction
= (SegmentFunction
) reference
;
1402 IOReturn ret
= kIOReturnSuccess
;
1404 if (target
->fNumAddressBits
&& (target
->fNumAddressBits
< 64)
1405 && ((segment
.fIOVMAddr
+ segment
.fLength
- 1) >> target
->fNumAddressBits
)
1406 && (target
->reserved
->fLocalMapperAllocValid
|| !target
->fMapper
))
1408 DEBG("kIOReturnMessageTooLarge(fNumAddressBits) %qx, %qx\n", segment
.fIOVMAddr
, segment
.fLength
);
1409 ret
= kIOReturnMessageTooLarge
;
1412 if (!(*segmentFunction
)(target
, segment
, vSegList
, outSegIndex
))
1414 DEBG("kIOReturnMessageTooLarge(fOutSeg) %qx, %qx\n", segment
.fIOVMAddr
, segment
.fLength
);
1415 ret
= kIOReturnMessageTooLarge
;
1422 IODMACommand::genIOVMSegments(SegmentFunction segmentFunction
,
1425 UInt32
*numSegmentsP
)
1427 return (genIOVMSegments(kWalkClient
, clientOutputSegment
, (void *) segmentFunction
,
1428 offsetP
, segmentsP
, numSegmentsP
));
1432 IODMACommand::OutputHost32(IODMACommand
*,
1433 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1435 Segment32
*base
= (Segment32
*) vSegList
;
1436 base
[outSegIndex
].fIOVMAddr
= (UInt32
) segment
.fIOVMAddr
;
1437 base
[outSegIndex
].fLength
= (UInt32
) segment
.fLength
;
1442 IODMACommand::OutputBig32(IODMACommand
*,
1443 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1445 const UInt offAddr
= outSegIndex
* sizeof(Segment32
);
1446 const UInt offLen
= offAddr
+ sizeof(UInt32
);
1447 OSWriteBigInt32(vSegList
, offAddr
, (UInt32
) segment
.fIOVMAddr
);
1448 OSWriteBigInt32(vSegList
, offLen
, (UInt32
) segment
.fLength
);
1453 IODMACommand::OutputLittle32(IODMACommand
*,
1454 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1456 const UInt offAddr
= outSegIndex
* sizeof(Segment32
);
1457 const UInt offLen
= offAddr
+ sizeof(UInt32
);
1458 OSWriteLittleInt32(vSegList
, offAddr
, (UInt32
) segment
.fIOVMAddr
);
1459 OSWriteLittleInt32(vSegList
, offLen
, (UInt32
) segment
.fLength
);
1464 IODMACommand::OutputHost64(IODMACommand
*,
1465 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1467 Segment64
*base
= (Segment64
*) vSegList
;
1468 base
[outSegIndex
] = segment
;
1473 IODMACommand::OutputBig64(IODMACommand
*,
1474 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1476 const UInt offAddr
= outSegIndex
* sizeof(Segment64
);
1477 const UInt offLen
= offAddr
+ sizeof(UInt64
);
1478 OSWriteBigInt64(vSegList
, offAddr
, (UInt64
) segment
.fIOVMAddr
);
1479 OSWriteBigInt64(vSegList
, offLen
, (UInt64
) segment
.fLength
);
1484 IODMACommand::OutputLittle64(IODMACommand
*,
1485 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1487 const UInt offAddr
= outSegIndex
* sizeof(Segment64
);
1488 const UInt offLen
= offAddr
+ sizeof(UInt64
);
1489 OSWriteLittleInt64(vSegList
, offAddr
, (UInt64
) segment
.fIOVMAddr
);
1490 OSWriteLittleInt64(vSegList
, offLen
, (UInt64
) segment
.fLength
);