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;
385 fInternalState
->fTag
= IOMemoryTag(kernel_map
);
386 __IODEQUALIFY(IOMemoryDescriptor
*, mem
)->prepare((IODirection
)
387 (kIODirectionDMACommand
| (fInternalState
->fTag
<< kIODirectionDMACommandShift
)));
388 IOTrackingAdd(gIOWireTracking
, &fInternalState
->fWireTracking
, fMemory
->getLength(), false);
389 #endif /* IOTRACKING */
394 clearMemoryDescriptor();
403 IODMACommand::clearMemoryDescriptor(bool autoComplete
)
405 if (fActive
&& !autoComplete
) return (kIOReturnNotReady
);
409 while (fActive
) complete();
413 __IODEQUALIFY(IOMemoryDescriptor
*, fMemory
)->complete((IODirection
)
414 (kIODirectionDMACommand
| (fInternalState
->fTag
<< kIODirectionDMACommandShift
)));
415 IOTrackingRemove(gIOWireTracking
, &fInternalState
->fWireTracking
, fMemory
->getLength());
416 #endif /* IOTRACKING */
422 return (kIOReturnSuccess
);
425 const IOMemoryDescriptor
*
426 IODMACommand::getMemoryDescriptor() const
432 IODMACommand::getIOMemoryDescriptor() const
434 IOMemoryDescriptor
* mem
;
436 mem
= reserved
->fCopyMD
;
437 if (!mem
) mem
= __IODEQUALIFY(IOMemoryDescriptor
*, fMemory
);
443 IODMACommand::segmentOp(
445 IODMACommand
*target
,
450 IOOptionBits op
= (uintptr_t) reference
;
451 addr64_t maxPhys
, address
;
456 IODMACommandInternal
* state
= target
->reserved
;
458 if (target
->fNumAddressBits
&& (target
->fNumAddressBits
< 64) && (state
->fLocalMapperAlloc
|| !target
->fMapper
))
459 maxPhys
= (1ULL << target
->fNumAddressBits
);
464 address
= segment
.fIOVMAddr
;
465 length
= segment
.fLength
;
470 if (!state
->fMisaligned
)
472 mask
= (segmentIndex
? target
->fAlignMaskInternalSegments
: state
->fSourceAlignMask
);
473 state
->fMisaligned
|= (0 != (mask
& address
));
474 if (state
->fMisaligned
) DEBG("misaligned address %qx:%qx, %x\n", address
, length
, mask
);
476 if (!state
->fMisaligned
)
478 mask
= target
->fAlignMaskLength
;
479 state
->fMisaligned
|= (0 != (mask
& length
));
480 if (state
->fMisaligned
) DEBG("misaligned length %qx:%qx, %x\n", address
, length
, mask
);
483 if (state
->fMisaligned
&& (kWalkPreflight
& op
))
484 return (kIOReturnNotAligned
);
486 if (!state
->fDoubleBuffer
)
488 if ((address
+ length
- 1) <= maxPhys
)
492 else if (address
<= maxPhys
)
494 DEBG("tail %qx, %qx", address
, length
);
495 length
= (address
+ length
- maxPhys
- 1);
496 address
= maxPhys
+ 1;
497 DEBG("-> %qx, %qx\n", address
, length
);
502 return (kIOReturnSuccess
);
504 numPages
= atop_64(round_page_64((address
& PAGE_MASK
) + length
));
506 if (kWalkPreflight
& op
)
508 state
->fCopyPageCount
+= numPages
;
514 if (kWalkPrepare
& op
)
516 lastPage
= state
->fCopyNext
;
517 for (IOItemCount idx
= 0; idx
< numPages
; idx
++)
519 vm_page_set_offset(lastPage
, atop_64(address
) + idx
);
520 lastPage
= vm_page_get_next(lastPage
);
524 if (!lastPage
|| SHOULD_COPY_DIR(op
, target
->fMDSummary
.fDirection
))
526 lastPage
= state
->fCopyNext
;
527 for (IOItemCount idx
= 0; idx
< numPages
; idx
++)
529 if (SHOULD_COPY_DIR(op
, target
->fMDSummary
.fDirection
))
531 addr64_t cpuAddr
= address
;
535 if ((kMapped
== MAPTYPE(target
->fMappingOptions
))
538 cpuAddr
= target
->fMapper
->mapToPhysicalAddress(address
);
541 remapAddr
= ptoa_64(vm_page_get_phys_page(lastPage
));
542 if (!state
->fDoubleBuffer
)
544 remapAddr
+= (address
& PAGE_MASK
);
546 chunk
= PAGE_SIZE
- (address
& PAGE_MASK
);
550 DEBG("cpv: 0x%qx %s 0x%qx, 0x%qx, 0x%02lx\n", remapAddr
,
551 (kWalkSyncIn
& op
) ? "->" : "<-",
554 if (kWalkSyncIn
& op
)
556 copypv(remapAddr
, cpuAddr
, chunk
,
557 cppvPsnk
| cppvFsnk
| cppvPsrc
| cppvNoRefSrc
);
561 copypv(cpuAddr
, remapAddr
, chunk
,
562 cppvPsnk
| cppvFsnk
| cppvPsrc
| cppvNoRefSrc
);
567 lastPage
= vm_page_get_next(lastPage
);
570 state
->fCopyNext
= lastPage
;
573 return kIOReturnSuccess
;
576 IOBufferMemoryDescriptor
*
577 IODMACommand::createCopyBuffer(IODirection direction
, UInt64 length
)
579 mach_vm_address_t mask
= 0xFFFFF000; //state->fSourceAlignMask
580 return (IOBufferMemoryDescriptor::inTaskWithPhysicalMask(kernel_task
,
581 direction
, length
, mask
));
585 IODMACommand::walkAll(UInt8 op
)
587 IODMACommandInternal
* state
= fInternalState
;
589 IOReturn ret
= kIOReturnSuccess
;
593 if (kWalkPreflight
& op
)
595 state
->fMisaligned
= false;
596 state
->fDoubleBuffer
= false;
597 state
->fPrepared
= false;
598 state
->fCopyNext
= NULL
;
599 state
->fCopyPageAlloc
= 0;
600 state
->fCopyPageCount
= 0;
601 state
->fNextRemapPage
= NULL
;
604 if (!(kWalkDoubleBuffer
& op
))
608 ret
= genIOVMSegments(op
, segmentOp
, (void *)(uintptr_t) op
, &offset
, state
, &numSegments
);
611 op
&= ~kWalkPreflight
;
613 state
->fDoubleBuffer
= (state
->fMisaligned
|| (kWalkDoubleBuffer
& op
));
614 if (state
->fDoubleBuffer
)
615 state
->fCopyPageCount
= atop_64(round_page(state
->fPreparedLength
));
617 if (state
->fCopyPageCount
)
619 vm_page_t mapBase
= NULL
;
621 DEBG("preflight fCopyPageCount %d\n", state
->fCopyPageCount
);
623 if (!fMapper
&& !state
->fDoubleBuffer
)
627 if (fMapper
) panic("fMapper copying");
629 kr
= vm_page_alloc_list(state
->fCopyPageCount
,
630 KMA_LOMEM
| KMA_NOPAGEWAIT
, &mapBase
);
631 if (KERN_SUCCESS
!= kr
)
633 DEBG("vm_page_alloc_list(%d) failed (%d)\n", state
->fCopyPageCount
, kr
);
640 state
->fCopyPageAlloc
= mapBase
;
641 state
->fCopyNext
= state
->fCopyPageAlloc
;
644 ret
= genIOVMSegments(op
, segmentOp
, (void *)(uintptr_t) op
, &offset
, state
, &numSegments
);
645 state
->fPrepared
= true;
646 op
&= ~(kWalkSyncIn
| kWalkSyncOut
);
650 DEBG("alloc IOBMD\n");
651 state
->fCopyMD
= createCopyBuffer(fMDSummary
.fDirection
, state
->fPreparedLength
);
655 ret
= kIOReturnSuccess
;
656 state
->fPrepared
= true;
660 DEBG("IODMACommand !alloc IOBMD");
661 return (kIOReturnNoResources
);
667 if (state
->fPrepared
&& ((kWalkSyncIn
| kWalkSyncOut
) & op
))
669 if (state
->fCopyPageCount
)
671 DEBG("sync fCopyPageCount %d\n", state
->fCopyPageCount
);
673 if (state
->fCopyPageAlloc
)
675 state
->fCopyNext
= state
->fCopyPageAlloc
;
678 ret
= genIOVMSegments(op
, segmentOp
, (void *)(uintptr_t) op
, &offset
, state
, &numSegments
);
680 else if (state
->fCopyMD
)
682 DEBG("sync IOBMD\n");
684 if (SHOULD_COPY_DIR(op
, fMDSummary
.fDirection
))
686 IOMemoryDescriptor
*poMD
= const_cast<IOMemoryDescriptor
*>(fMemory
);
690 if (kWalkSyncIn
& op
)
691 bytes
= poMD
->writeBytes(state
->fPreparedOffset
,
692 state
->fCopyMD
->getBytesNoCopy(),
693 state
->fPreparedLength
);
695 bytes
= poMD
->readBytes(state
->fPreparedOffset
,
696 state
->fCopyMD
->getBytesNoCopy(),
697 state
->fPreparedLength
);
698 DEBG("fCopyMD %s %lx bytes\n", (kWalkSyncIn
& op
) ? "wrote" : "read", bytes
);
699 ret
= (bytes
== state
->fPreparedLength
) ? kIOReturnSuccess
: kIOReturnUnderrun
;
702 ret
= kIOReturnSuccess
;
707 if (kWalkComplete
& op
)
709 if (state
->fCopyPageAlloc
)
711 vm_page_free_list(state
->fCopyPageAlloc
, FALSE
);
712 state
->fCopyPageAlloc
= 0;
713 state
->fCopyPageCount
= 0;
717 state
->fCopyMD
->release();
721 state
->fPrepared
= false;
727 IODMACommand::getNumAddressBits(void)
729 return (fNumAddressBits
);
733 IODMACommand::getAlignment(void)
735 return (fAlignMask
+ 1);
739 IODMACommand::getAlignmentLength(void)
741 return (fAlignMaskLength
+ 1);
745 IODMACommand::getAlignmentInternalSegments(void)
747 return (fAlignMaskInternalSegments
+ 1);
751 IODMACommand::prepareWithSpecification(SegmentFunction outSegFunc
,
752 const SegmentOptions
* segmentOptions
,
753 uint32_t mappingOptions
,
762 if (fActive
) return kIOReturnNotPermitted
;
764 ret
= setSpecification(outSegFunc
, segmentOptions
, mappingOptions
, mapper
);
765 if (kIOReturnSuccess
!= ret
) return (ret
);
767 ret
= prepare(offset
, length
, flushCache
, synchronize
);
773 IODMACommand::prepareWithSpecification(SegmentFunction outSegFunc
,
774 UInt8 numAddressBits
,
775 UInt64 maxSegmentSize
,
776 MappingOptions mappingOptions
,
777 UInt64 maxTransferSize
,
785 SegmentOptions segmentOptions
=
787 .fStructSize
= sizeof(segmentOptions
),
788 .fNumAddressBits
= numAddressBits
,
789 .fMaxSegmentSize
= maxSegmentSize
,
790 .fMaxTransferSize
= maxTransferSize
,
791 .fAlignment
= alignment
,
792 .fAlignmentLength
= 1,
793 .fAlignmentInternalSegments
= alignment
796 return (prepareWithSpecification(outSegFunc
, &segmentOptions
, mappingOptions
, mapper
,
797 offset
, length
, flushCache
, synchronize
));
802 IODMACommand::prepare(UInt64 offset
, UInt64 length
, bool flushCache
, bool synchronize
)
804 IODMACommandInternal
* state
= fInternalState
;
805 IOReturn ret
= kIOReturnSuccess
;
806 uint32_t mappingOptions
= fMappingOptions
;
808 // check specification has been set
809 if (!fOutSeg
) return (kIOReturnNotReady
);
811 if (!length
) length
= fMDSummary
.fLength
;
813 if (length
> fMaxTransferSize
) return kIOReturnNoSpace
;
817 if ((state
->fPreparedOffset
!= offset
)
818 || (state
->fPreparedLength
!= length
))
819 ret
= kIOReturnNotReady
;
823 if (fAlignMaskLength
& length
) return (kIOReturnNotAligned
);
825 state
->fPreparedOffset
= offset
;
826 state
->fPreparedLength
= length
;
828 state
->fMapContig
= false;
829 state
->fMisaligned
= false;
830 state
->fDoubleBuffer
= false;
831 state
->fPrepared
= false;
832 state
->fCopyNext
= NULL
;
833 state
->fCopyPageAlloc
= 0;
834 state
->fCopyPageCount
= 0;
835 state
->fNextRemapPage
= NULL
;
837 state
->fLocalMapperAlloc
= 0;
838 state
->fLocalMapperAllocLength
= 0;
840 state
->fLocalMapper
= (fMapper
&& (fMapper
!= IOMapper::gSystem
));
842 state
->fSourceAlignMask
= fAlignMask
;
844 state
->fSourceAlignMask
&= page_mask
;
846 state
->fCursor
= state
->fIterateOnly
847 || (!state
->fCheckAddressing
848 && (!state
->fSourceAlignMask
849 || ((fMDSummary
.fPageAlign
& (1 << 31)) && (0 == (fMDSummary
.fPageAlign
& state
->fSourceAlignMask
)))));
853 IOOptionBits op
= kWalkPrepare
| kWalkPreflight
;
859 if (IS_NONCOHERENT(mappingOptions
) && flushCache
)
863 state
->fCopyMD
->performOperation(kIOMemoryIncoherentIOStore
, 0, length
);
867 IOMemoryDescriptor
* md
= const_cast<IOMemoryDescriptor
*>(fMemory
);
868 md
->performOperation(kIOMemoryIncoherentIOStore
, offset
, length
);
874 IOMDDMAMapArgs mapArgs
;
875 bzero(&mapArgs
, sizeof(mapArgs
));
876 mapArgs
.fMapper
= fMapper
;
877 mapArgs
.fCommand
= this;
878 mapArgs
.fMapSpec
.device
= state
->fDevice
;
879 mapArgs
.fMapSpec
.alignment
= fAlignMask
+ 1;
880 mapArgs
.fMapSpec
.numAddressBits
= fNumAddressBits
? fNumAddressBits
: 64;
881 mapArgs
.fLength
= state
->fPreparedLength
;
882 const IOMemoryDescriptor
* md
= state
->fCopyMD
;
883 if (md
) { mapArgs
.fOffset
= 0; }
887 mapArgs
.fOffset
= state
->fPreparedOffset
;
889 ret
= md
->dmaCommandOperation(kIOMDDMAMap
| state
->fIterateOnly
, &mapArgs
, sizeof(mapArgs
));
890 //IOLog("dma %p 0x%x 0x%qx-0x%qx 0x%qx-0x%qx\n", this, ret, state->fPreparedOffset, state->fPreparedLength, mapArgs.fAlloc, mapArgs.fAllocLength);
892 if (kIOReturnSuccess
== ret
)
894 state
->fLocalMapperAlloc
= mapArgs
.fAlloc
;
895 state
->fLocalMapperAllocLength
= mapArgs
.fAllocLength
;
896 state
->fMapContig
= mapArgs
.fMapContig
;
898 if (NULL
!= IOMapper::gSystem
) ret
= kIOReturnSuccess
;
900 if (kIOReturnSuccess
== ret
) state
->fPrepared
= true;
906 IODMACommand::complete(bool invalidateCache
, bool synchronize
)
908 IODMACommandInternal
* state
= fInternalState
;
909 IOReturn ret
= kIOReturnSuccess
;
912 return kIOReturnNotReady
;
916 if (IS_NONCOHERENT(fMappingOptions
) && invalidateCache
)
920 state
->fCopyMD
->performOperation(kIOMemoryIncoherentIOFlush
, 0, state
->fPreparedLength
);
924 IOMemoryDescriptor
* md
= const_cast<IOMemoryDescriptor
*>(fMemory
);
925 md
->performOperation(kIOMemoryIncoherentIOFlush
, state
->fPreparedOffset
, state
->fPreparedLength
);
931 IOOptionBits op
= kWalkComplete
;
936 if (state
->fLocalMapperAlloc
)
938 if (state
->fLocalMapperAllocLength
)
940 fMapper
->iovmUnmapMemory(getIOMemoryDescriptor(), this,
941 state
->fLocalMapperAlloc
, state
->fLocalMapperAllocLength
);
943 state
->fLocalMapperAlloc
= 0;
944 state
->fLocalMapperAllocLength
= 0;
947 state
->fPrepared
= false;
954 IODMACommand::getPreparedOffsetAndLength(UInt64
* offset
, UInt64
* length
)
956 IODMACommandInternal
* state
= fInternalState
;
958 return (kIOReturnNotReady
);
961 *offset
= state
->fPreparedOffset
;
963 *length
= state
->fPreparedLength
;
965 return (kIOReturnSuccess
);
969 IODMACommand::synchronize(IOOptionBits options
)
971 IODMACommandInternal
* state
= fInternalState
;
972 IOReturn ret
= kIOReturnSuccess
;
975 if (kIODirectionOutIn
== (kIODirectionOutIn
& options
))
976 return kIOReturnBadArgument
;
979 return kIOReturnNotReady
;
982 if (kForceDoubleBuffer
& options
)
984 if (state
->fDoubleBuffer
)
985 return kIOReturnSuccess
;
987 state
->fCursor
= false;
989 ret
= walkAll(kWalkComplete
);
991 op
|= kWalkPrepare
| kWalkPreflight
| kWalkDoubleBuffer
;
993 else if (state
->fCursor
)
994 return kIOReturnSuccess
;
996 if (kIODirectionIn
& options
)
997 op
|= kWalkSyncIn
| kWalkSyncAlways
;
998 else if (kIODirectionOut
& options
)
999 op
|= kWalkSyncOut
| kWalkSyncAlways
;
1006 struct IODMACommandTransferContext
1009 UInt64 bufferOffset
;
1015 kIODMACommandTransferOpReadBytes
= 1,
1016 kIODMACommandTransferOpWriteBytes
= 2
1020 IODMACommand::transferSegment(void *reference
,
1021 IODMACommand
*target
,
1024 UInt32 segmentIndex
)
1026 IODMACommandTransferContext
* context
= (IODMACommandTransferContext
*) reference
;
1027 UInt64 length
= min(segment
.fLength
, context
->remaining
);
1028 addr64_t ioAddr
= segment
.fIOVMAddr
;
1029 addr64_t cpuAddr
= ioAddr
;
1031 context
->remaining
-= length
;
1035 UInt64 copyLen
= length
;
1036 if ((kMapped
== MAPTYPE(target
->fMappingOptions
))
1039 cpuAddr
= target
->fMapper
->mapToPhysicalAddress(ioAddr
);
1040 copyLen
= min(copyLen
, page_size
- (ioAddr
& (page_size
- 1)));
1044 switch (context
->op
)
1046 case kIODMACommandTransferOpReadBytes
:
1047 copypv(cpuAddr
, context
->bufferOffset
+ (addr64_t
) context
->buffer
, copyLen
,
1048 cppvPsrc
| cppvNoRefSrc
| cppvFsnk
| cppvKmap
);
1050 case kIODMACommandTransferOpWriteBytes
:
1051 copypv(context
->bufferOffset
+ (addr64_t
) context
->buffer
, cpuAddr
, copyLen
,
1052 cppvPsnk
| cppvFsnk
| cppvNoRefSrc
| cppvNoModSnk
| cppvKmap
);
1056 context
->bufferOffset
+= copyLen
;
1059 return (context
->remaining
? kIOReturnSuccess
: kIOReturnOverrun
);
1063 IODMACommand::transfer(IOOptionBits transferOp
, UInt64 offset
, void * buffer
, UInt64 length
)
1065 IODMACommandInternal
* state
= fInternalState
;
1066 IODMACommandTransferContext context
;
1067 Segment64 segments
[1];
1068 UInt32 numSegments
= 0-1;
1073 if (offset
>= state
->fPreparedLength
)
1075 length
= min(length
, state
->fPreparedLength
- offset
);
1077 context
.buffer
= buffer
;
1078 context
.bufferOffset
= 0;
1079 context
.remaining
= length
;
1080 context
.op
= transferOp
;
1081 (void) genIOVMSegments(kWalkClient
, transferSegment
, &context
, &offset
, &segments
[0], &numSegments
);
1083 return (length
- context
.remaining
);
1087 IODMACommand::readBytes(UInt64 offset
, void *bytes
, UInt64 length
)
1089 return (transfer(kIODMACommandTransferOpReadBytes
, offset
, bytes
, length
));
1093 IODMACommand::writeBytes(UInt64 offset
, const void *bytes
, UInt64 length
)
1095 return (transfer(kIODMACommandTransferOpWriteBytes
, offset
, const_cast<void *>(bytes
), length
));
1099 IODMACommand::genIOVMSegments(UInt64
*offsetP
,
1101 UInt32
*numSegmentsP
)
1103 return (genIOVMSegments(kWalkClient
, clientOutputSegment
, (void *) fOutSeg
,
1104 offsetP
, segmentsP
, numSegmentsP
));
1108 IODMACommand::genIOVMSegments(uint32_t op
,
1109 InternalSegmentFunction outSegFunc
,
1113 UInt32
*numSegmentsP
)
1115 IODMACommandInternal
* internalState
= fInternalState
;
1116 IOOptionBits mdOp
= kIOMDWalkSegments
;
1117 IOReturn ret
= kIOReturnSuccess
;
1119 if (!(kWalkComplete
& op
) && !fActive
)
1120 return kIOReturnNotReady
;
1122 if (!offsetP
|| !segmentsP
|| !numSegmentsP
|| !*numSegmentsP
)
1123 return kIOReturnBadArgument
;
1125 IOMDDMAWalkSegmentArgs
*state
=
1126 (IOMDDMAWalkSegmentArgs
*)(void *) fState
;
1128 UInt64 offset
= *offsetP
+ internalState
->fPreparedOffset
;
1129 UInt64 memLength
= internalState
->fPreparedOffset
+ internalState
->fPreparedLength
;
1131 if (offset
>= memLength
)
1132 return kIOReturnOverrun
;
1134 if ((offset
== internalState
->fPreparedOffset
) || (offset
!= state
->fOffset
) || internalState
->fNewMD
) {
1136 state
->fIOVMAddr
= 0;
1137 internalState
->fNextRemapPage
= NULL
;
1138 internalState
->fNewMD
= false;
1139 state
->fMapped
= (0 != fMapper
);
1140 mdOp
= kIOMDFirstSegment
;
1143 UInt32 segIndex
= 0;
1144 UInt32 numSegments
= *numSegmentsP
;
1145 Segment64 curSeg
= { 0, 0 };
1148 if (fNumAddressBits
&& (fNumAddressBits
< 64))
1149 maxPhys
= (1ULL << fNumAddressBits
);
1154 while (state
->fIOVMAddr
|| (state
->fOffset
< memLength
))
1157 if (!state
->fIOVMAddr
) {
1161 state
->fOffset
= offset
;
1162 state
->fLength
= memLength
- offset
;
1164 if (internalState
->fMapContig
&& internalState
->fLocalMapperAlloc
)
1166 state
->fIOVMAddr
= internalState
->fLocalMapperAlloc
+ offset
- internalState
->fPreparedOffset
;
1167 rtn
= kIOReturnSuccess
;
1170 uint64_t checkOffset
;
1171 IOPhysicalLength segLen
;
1172 for (checkOffset
= 0; checkOffset
< state
->fLength
; )
1174 addr64_t phys
= const_cast<IOMemoryDescriptor
*>(fMemory
)->getPhysicalSegment(checkOffset
+ offset
, &segLen
, kIOMemoryMapperNone
);
1175 if (fMapper
->mapAddr(state
->fIOVMAddr
+ checkOffset
) != phys
)
1177 panic("%llx != %llx:%llx, %llx phys: %llx %llx\n", offset
,
1178 state
->fIOVMAddr
+ checkOffset
, fMapper
->mapAddr(state
->fIOVMAddr
+ checkOffset
), state
->fLength
,
1181 checkOffset
+= page_size
- (phys
& page_mask
);
1188 const IOMemoryDescriptor
* memory
=
1189 internalState
->fCopyMD
? internalState
->fCopyMD
: fMemory
;
1190 rtn
= memory
->dmaCommandOperation(mdOp
, fState
, sizeof(fState
));
1191 mdOp
= kIOMDWalkSegments
;
1194 if (rtn
== kIOReturnSuccess
)
1196 assert(state
->fIOVMAddr
);
1197 assert(state
->fLength
);
1198 if ((curSeg
.fIOVMAddr
+ curSeg
.fLength
) == state
->fIOVMAddr
) {
1199 UInt64 length
= state
->fLength
;
1201 curSeg
.fLength
+= length
;
1202 state
->fIOVMAddr
= 0;
1205 else if (rtn
== kIOReturnOverrun
)
1206 state
->fIOVMAddr
= state
->fLength
= 0; // At end
1211 // seg = state, offset = end of seg
1212 if (!curSeg
.fIOVMAddr
)
1214 UInt64 length
= state
->fLength
;
1216 curSeg
.fIOVMAddr
= state
->fIOVMAddr
;
1217 curSeg
.fLength
= length
;
1218 state
->fIOVMAddr
= 0;
1221 if (!state
->fIOVMAddr
)
1224 if ((kWalkClient
& op
) && (curSeg
.fIOVMAddr
+ curSeg
.fLength
- 1) > maxPhys
)
1226 if (internalState
->fCursor
)
1228 curSeg
.fIOVMAddr
= 0;
1229 ret
= kIOReturnMessageTooLarge
;
1232 else if (curSeg
.fIOVMAddr
<= maxPhys
)
1234 UInt64 remain
, newLength
;
1236 newLength
= (maxPhys
+ 1 - curSeg
.fIOVMAddr
);
1237 DEBG("trunc %qx, %qx-> %qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
, newLength
);
1238 remain
= curSeg
.fLength
- newLength
;
1239 state
->fIOVMAddr
= newLength
+ curSeg
.fIOVMAddr
;
1240 curSeg
.fLength
= newLength
;
1241 state
->fLength
= remain
;
1246 UInt64 addr
= curSeg
.fIOVMAddr
;
1247 ppnum_t addrPage
= atop_64(addr
);
1248 vm_page_t remap
= NULL
;
1249 UInt64 remain
, newLength
;
1251 DEBG("sparse switch %qx, %qx ", addr
, curSeg
.fLength
);
1253 remap
= internalState
->fNextRemapPage
;
1254 if (remap
&& (addrPage
== vm_page_get_offset(remap
)))
1257 else for (remap
= internalState
->fCopyPageAlloc
;
1258 remap
&& (addrPage
!= vm_page_get_offset(remap
));
1259 remap
= vm_page_get_next(remap
))
1263 if (!remap
) panic("no remap page found");
1265 curSeg
.fIOVMAddr
= ptoa_64(vm_page_get_phys_page(remap
))
1266 + (addr
& PAGE_MASK
);
1267 internalState
->fNextRemapPage
= vm_page_get_next(remap
);
1269 newLength
= PAGE_SIZE
- (addr
& PAGE_MASK
);
1270 if (newLength
< curSeg
.fLength
)
1272 remain
= curSeg
.fLength
- newLength
;
1273 state
->fIOVMAddr
= addr
+ newLength
;
1274 curSeg
.fLength
= newLength
;
1275 state
->fLength
= remain
;
1278 DEBG("-> %qx, %qx offset %qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
, offset
);
1282 // reduce size of output segment
1283 uint64_t reduce
, leftover
= 0;
1286 if (curSeg
.fLength
> fMaxSegmentSize
)
1288 leftover
+= curSeg
.fLength
- fMaxSegmentSize
;
1289 curSeg
.fLength
= fMaxSegmentSize
;
1290 state
->fIOVMAddr
= curSeg
.fLength
+ curSeg
.fIOVMAddr
;
1293 // alignment current length
1295 reduce
= (curSeg
.fLength
& fAlignMaskLength
);
1296 if (reduce
&& (curSeg
.fLength
> reduce
))
1299 curSeg
.fLength
-= reduce
;
1300 state
->fIOVMAddr
= curSeg
.fLength
+ curSeg
.fIOVMAddr
;
1303 // alignment next address
1305 reduce
= (state
->fIOVMAddr
& fAlignMaskInternalSegments
);
1306 if (reduce
&& (curSeg
.fLength
> reduce
))
1309 curSeg
.fLength
-= reduce
;
1310 state
->fIOVMAddr
= curSeg
.fLength
+ curSeg
.fIOVMAddr
;
1315 DEBG("reduce seg by 0x%llx @ 0x%llx [0x%llx, 0x%llx]\n",
1317 curSeg
.fIOVMAddr
, curSeg
.fLength
);
1318 state
->fLength
= leftover
;
1324 if (internalState
->fCursor
)
1329 mask
= (segIndex
? fAlignMaskInternalSegments
: internalState
->fSourceAlignMask
);
1330 misaligned
= (0 != (mask
& curSeg
.fIOVMAddr
));
1333 mask
= fAlignMaskLength
;
1334 misaligned
|= (0 != (mask
& curSeg
.fLength
));
1338 if (misaligned
) DEBG("cursor misaligned %qx:%qx\n", curSeg
.fIOVMAddr
, curSeg
.fLength
);
1339 curSeg
.fIOVMAddr
= 0;
1340 ret
= kIOReturnNotAligned
;
1345 if (offset
>= memLength
)
1347 curSeg
.fLength
-= (offset
- memLength
);
1349 state
->fIOVMAddr
= state
->fLength
= 0; // At end
1354 if (state
->fIOVMAddr
) {
1355 if ((segIndex
+ 1 == numSegments
))
1358 ret
= (*outSegFunc
)(reference
, this, curSeg
, segmentsP
, segIndex
++);
1359 curSeg
.fIOVMAddr
= 0;
1360 if (kIOReturnSuccess
!= ret
)
1365 if (curSeg
.fIOVMAddr
) {
1366 ret
= (*outSegFunc
)(reference
, this, curSeg
, segmentsP
, segIndex
++);
1369 if (kIOReturnSuccess
== ret
)
1371 state
->fOffset
= offset
;
1372 *offsetP
= offset
- internalState
->fPreparedOffset
;
1373 *numSegmentsP
= segIndex
;
1379 IODMACommand::clientOutputSegment(
1380 void *reference
, IODMACommand
*target
,
1381 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1383 SegmentFunction segmentFunction
= (SegmentFunction
) reference
;
1384 IOReturn ret
= kIOReturnSuccess
;
1386 if (target
->fNumAddressBits
&& (target
->fNumAddressBits
< 64)
1387 && ((segment
.fIOVMAddr
+ segment
.fLength
- 1) >> target
->fNumAddressBits
)
1388 && (target
->reserved
->fLocalMapperAlloc
|| !target
->fMapper
))
1390 DEBG("kIOReturnMessageTooLarge(fNumAddressBits) %qx, %qx\n", segment
.fIOVMAddr
, segment
.fLength
);
1391 ret
= kIOReturnMessageTooLarge
;
1394 if (!(*segmentFunction
)(target
, segment
, vSegList
, outSegIndex
))
1396 DEBG("kIOReturnMessageTooLarge(fOutSeg) %qx, %qx\n", segment
.fIOVMAddr
, segment
.fLength
);
1397 ret
= kIOReturnMessageTooLarge
;
1404 IODMACommand::genIOVMSegments(SegmentFunction segmentFunction
,
1407 UInt32
*numSegmentsP
)
1409 return (genIOVMSegments(kWalkClient
, clientOutputSegment
, (void *) segmentFunction
,
1410 offsetP
, segmentsP
, numSegmentsP
));
1414 IODMACommand::OutputHost32(IODMACommand
*,
1415 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1417 Segment32
*base
= (Segment32
*) vSegList
;
1418 base
[outSegIndex
].fIOVMAddr
= (UInt32
) segment
.fIOVMAddr
;
1419 base
[outSegIndex
].fLength
= (UInt32
) segment
.fLength
;
1424 IODMACommand::OutputBig32(IODMACommand
*,
1425 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1427 const UInt offAddr
= outSegIndex
* sizeof(Segment32
);
1428 const UInt offLen
= offAddr
+ sizeof(UInt32
);
1429 OSWriteBigInt32(vSegList
, offAddr
, (UInt32
) segment
.fIOVMAddr
);
1430 OSWriteBigInt32(vSegList
, offLen
, (UInt32
) segment
.fLength
);
1435 IODMACommand::OutputLittle32(IODMACommand
*,
1436 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1438 const UInt offAddr
= outSegIndex
* sizeof(Segment32
);
1439 const UInt offLen
= offAddr
+ sizeof(UInt32
);
1440 OSWriteLittleInt32(vSegList
, offAddr
, (UInt32
) segment
.fIOVMAddr
);
1441 OSWriteLittleInt32(vSegList
, offLen
, (UInt32
) segment
.fLength
);
1446 IODMACommand::OutputHost64(IODMACommand
*,
1447 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1449 Segment64
*base
= (Segment64
*) vSegList
;
1450 base
[outSegIndex
] = segment
;
1455 IODMACommand::OutputBig64(IODMACommand
*,
1456 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1458 const UInt offAddr
= outSegIndex
* sizeof(Segment64
);
1459 const UInt offLen
= offAddr
+ sizeof(UInt64
);
1460 OSWriteBigInt64(vSegList
, offAddr
, (UInt64
) segment
.fIOVMAddr
);
1461 OSWriteBigInt64(vSegList
, offLen
, (UInt64
) segment
.fLength
);
1466 IODMACommand::OutputLittle64(IODMACommand
*,
1467 Segment64 segment
, void *vSegList
, UInt32 outSegIndex
)
1469 const UInt offAddr
= outSegIndex
* sizeof(Segment64
);
1470 const UInt offLen
= offAddr
+ sizeof(UInt64
);
1471 OSWriteLittleInt64(vSegList
, offAddr
, (UInt64
) segment
.fIOVMAddr
);
1472 OSWriteLittleInt64(vSegList
, offLen
, (UInt64
) segment
.fLength
);