This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
IODMACommand.cpp
1719 lines (1478 loc) · 47.7 KB
/
IODMACommand.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
#define IOKIT_ENABLE_SHARED_PTR
#include <IOKit/assert.h>
#include <libkern/OSTypes.h>
#include <libkern/OSByteOrder.h>
#include <libkern/OSDebug.h>
#include <IOKit/IOReturn.h>
#include <IOKit/IOLib.h>
#include <IOKit/IODMACommand.h>
#include <IOKit/IOMapper.h>
#include <IOKit/IOMemoryDescriptor.h>
#include <IOKit/IOBufferMemoryDescriptor.h>
#include "IOKitKernelInternal.h"
#define MAPTYPE(type) ((UInt) (type) & kTypeMask)
#define IS_NONCOHERENT(type) (MAPTYPE(type) == kNonCoherent)
enum{
kWalkSyncIn = 0x01,// bounce -> md
kWalkSyncOut = 0x02,// bounce <- md
kWalkSyncAlways = 0x04,
kWalkPreflight = 0x08,
kWalkDoubleBuffer = 0x10,
kWalkPrepare = 0x20,
kWalkComplete = 0x40,
kWalkClient = 0x80
};
#define fInternalState reserved
#define fState reserved->fState
#define fMDSummary reserved->fMDSummary
#if 1
// no direction => OutIn
#define SHOULD_COPY_DIR(op, direction) \
((kIODirectionNone == (direction)) \
|| (kWalkSyncAlways & (op)) \
|| (((kWalkSyncIn & (op)) ? kIODirectionIn : kIODirectionOut) \
& (direction)))
#else
#define SHOULD_COPY_DIR(state, direction) (true)
#endif
#if 0
#define DEBG(fmt, args...) { IOLog(fmt, ## args); kprintf(fmt, ## args); }
#else
#define DEBG(fmt, args...) {}
#endif
#if 0
#define LOGTAG 0x87654321
#endif
/**************************** class IODMACommand ***************************/
#undef super
#define super IOCommand
OSDefineMetaClassAndStructorsWithZone(IODMACommand, IOCommand, ZC_NONE);
OSMetaClassDefineReservedUsedX86(IODMACommand, 0);
OSMetaClassDefineReservedUsedX86(IODMACommand, 1);
OSMetaClassDefineReservedUsedX86(IODMACommand, 2);
OSMetaClassDefineReservedUsedX86(IODMACommand, 3);
OSMetaClassDefineReservedUsedX86(IODMACommand, 4);
OSMetaClassDefineReservedUsedX86(IODMACommand, 5);
OSMetaClassDefineReservedUsedX86(IODMACommand, 6);
OSMetaClassDefineReservedUnused(IODMACommand, 7);
OSMetaClassDefineReservedUnused(IODMACommand, 8);
OSMetaClassDefineReservedUnused(IODMACommand, 9);
OSMetaClassDefineReservedUnused(IODMACommand, 10);
OSMetaClassDefineReservedUnused(IODMACommand, 11);
OSMetaClassDefineReservedUnused(IODMACommand, 12);
OSMetaClassDefineReservedUnused(IODMACommand, 13);
OSMetaClassDefineReservedUnused(IODMACommand, 14);
OSMetaClassDefineReservedUnused(IODMACommand, 15);
OSSharedPtr<IODMACommand>
IODMACommand::withRefCon(void * refCon)
{
OSSharedPtr<IODMACommand> me = OSMakeShared<IODMACommand>();
if (me && !me->initWithRefCon(refCon)) {
return nullptr;
}
return me;
}
OSSharedPtr<IODMACommand>
IODMACommand::withSpecification(SegmentFunction outSegFunc,
const SegmentOptions * segmentOptions,
uint32_t mappingOptions,
IOMapper * mapper,
void * refCon)
{
OSSharedPtr<IODMACommand> me = OSMakeShared<IODMACommand>();
if (me && !me->initWithSpecification(outSegFunc, segmentOptions, mappingOptions,
mapper, refCon)) {
return nullptr;
}
return me;
}
OSSharedPtr<IODMACommand>
IODMACommand::withSpecification(SegmentFunction outSegFunc,
UInt8 numAddressBits,
UInt64 maxSegmentSize,
MappingOptions mappingOptions,
UInt64 maxTransferSize,
UInt32 alignment,
IOMapper *mapper,
void *refCon)
{
OSSharedPtr<IODMACommand> me = OSMakeShared<IODMACommand>();
if (me && !me->initWithSpecification(outSegFunc,
numAddressBits, maxSegmentSize,
mappingOptions, maxTransferSize,
alignment, mapper, refCon)) {
return nullptr;
}
return me;
}
OSSharedPtr<IODMACommand>
IODMACommand::cloneCommand(void *refCon)
{
SegmentOptions segmentOptions =
{
.fStructSize = sizeof(segmentOptions),
.fNumAddressBits = (uint8_t)fNumAddressBits,
.fMaxSegmentSize = fMaxSegmentSize,
.fMaxTransferSize = fMaxTransferSize,
.fAlignment = fAlignMask + 1,
.fAlignmentLength = fAlignMaskInternalSegments + 1,
.fAlignmentInternalSegments = fAlignMaskLength + 1
};
return IODMACommand::withSpecification(fOutSeg, &segmentOptions,
fMappingOptions, fMapper.get(), refCon);
}
#define kLastOutputFunction ((SegmentFunction) kLastOutputFunction)
bool
IODMACommand::initWithRefCon(void * refCon)
{
if (!super::init()) {
return false;
}
if (!reserved) {
reserved = IONew(IODMACommandInternal, 1);
if (!reserved) {
return false;
}
}
bzero(reserved, sizeof(IODMACommandInternal));
fRefCon = refCon;
return true;
}
bool
IODMACommand::initWithSpecification(SegmentFunction outSegFunc,
const SegmentOptions * segmentOptions,
uint32_t mappingOptions,
IOMapper * mapper,
void * refCon)
{
if (!initWithRefCon(refCon)) {
return false;
}
if (kIOReturnSuccess != setSpecification(outSegFunc, segmentOptions,
mappingOptions, mapper)) {
return false;
}
return true;
}
bool
IODMACommand::initWithSpecification(SegmentFunction outSegFunc,
UInt8 numAddressBits,
UInt64 maxSegmentSize,
MappingOptions mappingOptions,
UInt64 maxTransferSize,
UInt32 alignment,
IOMapper *mapper,
void *refCon)
{
SegmentOptions segmentOptions =
{
.fStructSize = sizeof(segmentOptions),
.fNumAddressBits = numAddressBits,
.fMaxSegmentSize = maxSegmentSize,
.fMaxTransferSize = maxTransferSize,
.fAlignment = alignment,
.fAlignmentLength = 1,
.fAlignmentInternalSegments = alignment
};
return initWithSpecification(outSegFunc, &segmentOptions, mappingOptions, mapper, refCon);
}
IOReturn
IODMACommand::setSpecification(SegmentFunction outSegFunc,
const SegmentOptions * segmentOptions,
uint32_t mappingOptions,
IOMapper * mapper)
{
IOService * device = NULL;
UInt8 numAddressBits;
UInt64 maxSegmentSize;
UInt64 maxTransferSize;
UInt32 alignment;
bool is32Bit;
if (!outSegFunc || !segmentOptions) {
return kIOReturnBadArgument;
}
is32Bit = ((OutputHost32 == outSegFunc)
|| (OutputBig32 == outSegFunc)
|| (OutputLittle32 == outSegFunc));
numAddressBits = segmentOptions->fNumAddressBits;
maxSegmentSize = segmentOptions->fMaxSegmentSize;
maxTransferSize = segmentOptions->fMaxTransferSize;
alignment = segmentOptions->fAlignment;
if (is32Bit) {
if (!numAddressBits) {
numAddressBits = 32;
} else if (numAddressBits > 32) {
return kIOReturnBadArgument; // Wrong output function for bits
}
}
if (numAddressBits && (numAddressBits < PAGE_SHIFT)) {
return kIOReturnBadArgument;
}
if (!maxSegmentSize) {
maxSegmentSize--; // Set Max segment to -1
}
if (!maxTransferSize) {
maxTransferSize--; // Set Max transfer to -1
}
if (mapper && !OSDynamicCast(IOMapper, mapper)) {
device = mapper;
mapper = NULL;
}
if (!mapper && (kUnmapped != MAPTYPE(mappingOptions))) {
IOMapper::checkForSystemMapper();
mapper = IOMapper::gSystem;
}
fNumSegments = 0;
fOutSeg = outSegFunc;
fNumAddressBits = numAddressBits;
fMaxSegmentSize = maxSegmentSize;
fMappingOptions = mappingOptions;
fMaxTransferSize = maxTransferSize;
if (!alignment) {
alignment = 1;
}
fAlignMask = alignment - 1;
alignment = segmentOptions->fAlignmentLength;
if (!alignment) {
alignment = 1;
}
fAlignMaskLength = alignment - 1;
alignment = segmentOptions->fAlignmentInternalSegments;
if (!alignment) {
alignment = (fAlignMask + 1);
}
fAlignMaskInternalSegments = alignment - 1;
switch (MAPTYPE(mappingOptions)) {
case kMapped: break;
case kUnmapped: break;
case kNonCoherent: break;
case kBypassed:
if (!mapper) {
break;
}
return kIOReturnBadArgument;
default:
return kIOReturnBadArgument;
}
;
if (mapper != fMapper) {
fMapper.reset(mapper, OSRetain);
}
fInternalState->fIterateOnly = (0 != (kIterateOnly & mappingOptions));
fInternalState->fDevice = device;
return kIOReturnSuccess;
}
void
IODMACommand::free()
{
if (reserved) {
IODelete(reserved, IODMACommandInternal, 1);
}
fMapper.reset();
// Correct use of this class when setting an IOMemoryDescriptor
// in fMemory via setMemoryDescriptor(desc) is, for the caller, to
// have a matching call to clearMemoryDescriptor() before releasing
// the object. The matching call has also the effect of releasing
// the ref taken on the IOMemoryDescriptor in setMemoryDescriptor().
//
// A number of "misbehaving" drivers has been found during testing,
// whereby a matching call to clearMemoryDescriptor() is missing:
//
// rdar://59947343
// rdar://59946968
//
// Both the approaches taken in said drivers are wrong, but have gone
// basically silent with fMemory being a regular pointer. With fMemory
// becoming a OSSharedPtr, the IODMACommand destructor expects to find
// either fMemory reset (through the call to clearMemoryDescriptor()) or
// a reference hold for the release.
//
// For this reason, this workaround of detaching fMemory is put in
// place here, choosing the leak over the panic for misbehaving
// drivers. Once all instances are fixed, this workaround will be
// removed.
//
// Note: all well behaving drivers that have matching calls for
// setMemoryDescriptor() and clearMemoryDescriptor() are unaffected
// since fMemory will be null at this point.
fMemory.detach();
super::free();
}
IOReturn
IODMACommand::setMemoryDescriptor(const IOMemoryDescriptor *mem, bool autoPrepare)
{
IOReturn err = kIOReturnSuccess;
if (mem == fMemory) {
if (!autoPrepare) {
while (fActive) {
complete();
}
}
return kIOReturnSuccess;
}
if (fMemory) {
// As we are almost certainly being called from a work loop thread
// if fActive is true it is probably not a good time to potentially
// block. Just test for it and return an error
if (fActive) {
return kIOReturnBusy;
}
clearMemoryDescriptor();
}
if (mem) {
bzero(&fMDSummary, sizeof(fMDSummary));
err = mem->dmaCommandOperation(kIOMDGetCharacteristics | (kMapped == MAPTYPE(fMappingOptions)),
&fMDSummary, sizeof(fMDSummary));
if (err) {
return err;
}
ppnum_t highPage = fMDSummary.fHighestPage ? fMDSummary.fHighestPage : gIOLastPage;
if ((kMapped == MAPTYPE(fMappingOptions))
&& fMapper) {
fInternalState->fCheckAddressing = false;
} else {
fInternalState->fCheckAddressing = (fNumAddressBits && (highPage >= (1UL << (fNumAddressBits - PAGE_SHIFT))));
}
fInternalState->fNewMD = true;
fMemory.reset(const_cast<IOMemoryDescriptor *>(mem), OSRetain);
fInternalState->fSetActiveNoMapper = (!fMapper);
if (fInternalState->fSetActiveNoMapper) {
mem->dmaCommandOperation(kIOMDSetDMAActive, this, 0);
}
if (autoPrepare) {
err = prepare();
if (err) {
clearMemoryDescriptor();
}
}
}
return err;
}
IOReturn
IODMACommand::clearMemoryDescriptor(bool autoComplete)
{
if (fActive && !autoComplete) {
return kIOReturnNotReady;
}
if (fMemory) {
while (fActive) {
complete();
}
if (fInternalState->fSetActiveNoMapper) {
fMemory->dmaCommandOperation(kIOMDSetDMAInactive, this, 0);
}
fMemory.reset();
}
return kIOReturnSuccess;
}
const IOMemoryDescriptor *
IODMACommand::getMemoryDescriptor() const
{
return fMemory.get();
}
IOMemoryDescriptor *
IODMACommand::getIOMemoryDescriptor() const
{
OSSharedPtr<IOMemoryDescriptor> mem;
mem = reserved->fCopyMD;
if (!mem) {
mem = fMemory;
}
return mem.get();
}
IOReturn
IODMACommand::segmentOp(
void *reference,
IODMACommand *target,
Segment64 segment,
void *segments,
UInt32 segmentIndex)
{
IOOptionBits op = (IOOptionBits)(uintptr_t) reference;
addr64_t maxPhys, address;
uint64_t length;
uint32_t numPages;
uint32_t mask;
IODMACommandInternal * state = target->reserved;
if (target->fNumAddressBits && (target->fNumAddressBits < 64) && (state->fLocalMapperAllocValid || !target->fMapper)) {
maxPhys = (1ULL << target->fNumAddressBits);
} else {
maxPhys = 0;
}
maxPhys--;
address = segment.fIOVMAddr;
length = segment.fLength;
assert(length);
if (!state->fMisaligned) {
mask = (segmentIndex ? target->fAlignMaskInternalSegments : state->fSourceAlignMask);
state->fMisaligned |= (0 != (mask & address));
if (state->fMisaligned) {
DEBG("misaligned address %qx:%qx, %x\n", address, length, mask);
}
}
if (!state->fMisaligned) {
mask = target->fAlignMaskLength;
state->fMisaligned |= (0 != (mask & length));
if (state->fMisaligned) {
DEBG("misaligned length %qx:%qx, %x\n", address, length, mask);
}
}
if (state->fMisaligned && (kWalkPreflight & op)) {
return kIOReturnNotAligned;
}
if (!state->fDoubleBuffer) {
if ((address + length - 1) <= maxPhys) {
length = 0;
} else if (address <= maxPhys) {
DEBG("tail %qx, %qx", address, length);
length = (address + length - maxPhys - 1);
address = maxPhys + 1;
DEBG("-> %qx, %qx\n", address, length);
}
}
if (!length) {
return kIOReturnSuccess;
}
uint64_t numPages64 = atop_64(round_page_64((address & PAGE_MASK) + length));
if (numPages64 > UINT_MAX) {
return kIOReturnVMError;
}
numPages = (typeof(numPages))numPages64;
if (kWalkPreflight & op) {
state->fCopyPageCount += numPages;
} else {
vm_page_t lastPage;
lastPage = NULL;
if (kWalkPrepare & op) {
lastPage = state->fCopyNext;
for (IOItemCount idx = 0; idx < numPages; idx++) {
vm_page_set_offset(lastPage, atop_64(address) + idx);
lastPage = vm_page_get_next(lastPage);
}
}
if (!lastPage || SHOULD_COPY_DIR(op, target->fMDSummary.fDirection)) {
lastPage = state->fCopyNext;
for (IOItemCount idx = 0; idx < numPages; idx++) {
if (SHOULD_COPY_DIR(op, target->fMDSummary.fDirection)) {
addr64_t cpuAddr = address;
addr64_t remapAddr;
uint64_t chunk;
if ((kMapped == MAPTYPE(target->fMappingOptions))
&& target->fMapper) {
cpuAddr = target->fMapper->mapToPhysicalAddress(address);
}
remapAddr = ptoa_64(vm_page_get_phys_page(lastPage));
if (!state->fDoubleBuffer) {
remapAddr += (address & PAGE_MASK);
}
chunk = PAGE_SIZE - (address & PAGE_MASK);
if (chunk > length) {
chunk = length;
}
if (chunk > (UINT_MAX - PAGE_SIZE + 1)) {
chunk = (UINT_MAX - PAGE_SIZE + 1);
}
DEBG("cpv: 0x%qx %s 0x%qx, 0x%qx, 0x%02lx\n", remapAddr,
(kWalkSyncIn & op) ? "->" : "<-",
address, chunk, op);
if (kWalkSyncIn & op) { // cppvNoModSnk
copypv(remapAddr, cpuAddr, (unsigned int) chunk,
cppvPsnk | cppvFsnk | cppvPsrc | cppvNoRefSrc );
} else {
copypv(cpuAddr, remapAddr, (unsigned int) chunk,
cppvPsnk | cppvFsnk | cppvPsrc | cppvNoRefSrc );
}
address += chunk;
length -= chunk;
}
lastPage = vm_page_get_next(lastPage);
}
}
state->fCopyNext = lastPage;
}
return kIOReturnSuccess;
}
OSSharedPtr<IOBufferMemoryDescriptor>
IODMACommand::createCopyBuffer(IODirection direction, UInt64 length)
{
mach_vm_address_t mask = 0xFFFFF000; //state->fSourceAlignMask
return IOBufferMemoryDescriptor::inTaskWithPhysicalMask(kernel_task,
direction, length, mask);
}
IOReturn
IODMACommand::walkAll(uint32_t op)
{
IODMACommandInternal * state = fInternalState;
IOReturn ret = kIOReturnSuccess;
UInt32 numSegments;
UInt64 offset;
if (kWalkPreflight & op) {
state->fMisaligned = false;
state->fDoubleBuffer = false;
state->fPrepared = false;
state->fCopyNext = NULL;
state->fCopyPageAlloc = NULL;
state->fCopyPageCount = 0;
state->fNextRemapPage = NULL;
state->fCopyMD = NULL;
if (!(kWalkDoubleBuffer & op)) {
offset = 0;
numSegments = 0 - 1;
ret = genIOVMSegments(op, segmentOp, (void *)(uintptr_t) op, &offset, state, &numSegments);
}
op &= ~kWalkPreflight;
state->fDoubleBuffer = (state->fMisaligned || state->fForceDoubleBuffer);
state->fForceDoubleBuffer = false;
if (state->fDoubleBuffer) {
state->fCopyPageCount = (typeof(state->fCopyPageCount))(atop_64(round_page(state->fPreparedLength)));
}
if (state->fCopyPageCount) {
vm_page_t mapBase = NULL;
DEBG("preflight fCopyPageCount %d\n", state->fCopyPageCount);
if (!fMapper && !state->fDoubleBuffer) {
kern_return_t kr;
if (fMapper) {
panic("fMapper copying");
}
kr = vm_page_alloc_list(state->fCopyPageCount,
(kma_flags_t)(KMA_LOMEM | KMA_NOPAGEWAIT), &mapBase);
if (KERN_SUCCESS != kr) {
DEBG("vm_page_alloc_list(%d) failed (%d)\n", state->fCopyPageCount, kr);
mapBase = NULL;
}
}
if (mapBase) {
state->fCopyPageAlloc = mapBase;
state->fCopyNext = state->fCopyPageAlloc;
offset = 0;
numSegments = 0 - 1;
ret = genIOVMSegments(op, segmentOp, (void *)(uintptr_t) op, &offset, state, &numSegments);
state->fPrepared = true;
op &= ~(kWalkSyncIn | kWalkSyncOut);
} else {
DEBG("alloc IOBMD\n");
state->fCopyMD = createCopyBuffer(fMDSummary.fDirection, state->fPreparedLength);
if (state->fCopyMD) {
ret = kIOReturnSuccess;
state->fPrepared = true;
} else {
DEBG("IODMACommand !alloc IOBMD");
return kIOReturnNoResources;
}
}
}
}
if (state->fPrepared && ((kWalkSyncIn | kWalkSyncOut) & op)) {
if (state->fCopyPageCount) {
DEBG("sync fCopyPageCount %d\n", state->fCopyPageCount);
if (state->fCopyPageAlloc) {
state->fCopyNext = state->fCopyPageAlloc;
offset = 0;
numSegments = 0 - 1;
ret = genIOVMSegments(op, segmentOp, (void *)(uintptr_t) op, &offset, state, &numSegments);
} else if (state->fCopyMD) {
DEBG("sync IOBMD\n");
if (SHOULD_COPY_DIR(op, fMDSummary.fDirection)) {
OSSharedPtr<IOMemoryDescriptor> poMD = fMemory;
IOByteCount bytes;
if (kWalkSyncIn & op) {
bytes = poMD->writeBytes(state->fPreparedOffset,
state->fCopyMD->getBytesNoCopy(),
state->fPreparedLength);
} else {
bytes = poMD->readBytes(state->fPreparedOffset,
state->fCopyMD->getBytesNoCopy(),
state->fPreparedLength);
}
DEBG("fCopyMD %s %lx bytes\n", (kWalkSyncIn & op) ? "wrote" : "read", bytes);
ret = (bytes == state->fPreparedLength) ? kIOReturnSuccess : kIOReturnUnderrun;
} else {
ret = kIOReturnSuccess;
}
}
}
}
if (kWalkComplete & op) {
if (state->fCopyPageAlloc) {
vm_page_free_list(state->fCopyPageAlloc, FALSE);
state->fCopyPageAlloc = NULL;
state->fCopyPageCount = 0;
}
if (state->fCopyMD) {
state->fCopyMD.reset();
}
state->fPrepared = false;
}
return ret;
}
UInt8
IODMACommand::getNumAddressBits(void)
{
return (UInt8) fNumAddressBits;
}
UInt32
IODMACommand::getAlignment(void)
{
return fAlignMask + 1;
}
uint32_t
IODMACommand::getAlignmentLength(void)
{
return fAlignMaskLength + 1;
}
uint32_t
IODMACommand::getAlignmentInternalSegments(void)
{
return fAlignMaskInternalSegments + 1;
}
IOReturn
IODMACommand::prepareWithSpecification(SegmentFunction outSegFunc,
const SegmentOptions * segmentOptions,
uint32_t mappingOptions,
IOMapper * mapper,
UInt64 offset,
UInt64 length,
bool flushCache,
bool synchronize)
{
IOReturn ret;
if (fActive) {
return kIOReturnNotPermitted;
}
ret = setSpecification(outSegFunc, segmentOptions, mappingOptions, mapper);
if (kIOReturnSuccess != ret) {
return ret;
}
ret = prepare(offset, length, flushCache, synchronize);
return ret;
}
IOReturn
IODMACommand::prepareWithSpecification(SegmentFunction outSegFunc,
UInt8 numAddressBits,
UInt64 maxSegmentSize,
MappingOptions mappingOptions,
UInt64 maxTransferSize,
UInt32 alignment,
IOMapper *mapper,
UInt64 offset,
UInt64 length,
bool flushCache,
bool synchronize)
{
SegmentOptions segmentOptions =
{
.fStructSize = sizeof(segmentOptions),
.fNumAddressBits = numAddressBits,
.fMaxSegmentSize = maxSegmentSize,
.fMaxTransferSize = maxTransferSize,
.fAlignment = alignment,
.fAlignmentLength = 1,
.fAlignmentInternalSegments = alignment
};
return prepareWithSpecification(outSegFunc, &segmentOptions, mappingOptions, mapper,
offset, length, flushCache, synchronize);
}
IOReturn
IODMACommand::prepare(UInt64 offset, UInt64 length, bool flushCache, bool synchronize)
{
IODMACommandInternal * state = fInternalState;
IOReturn ret = kIOReturnSuccess;
uint32_t mappingOptions = fMappingOptions;
// check specification has been set
if (!fOutSeg) {
return kIOReturnNotReady;
}
if (!length) {
length = fMDSummary.fLength;
}
if (length > fMaxTransferSize) {
return kIOReturnNoSpace;
}
if (fActive++) {
if ((state->fPreparedOffset != offset)
|| (state->fPreparedLength != length)) {
ret = kIOReturnNotReady;
}
} else {
if (fAlignMaskLength & length) {
return kIOReturnNotAligned;
}
if (atop_64(state->fPreparedLength) > UINT_MAX) {
return kIOReturnVMError;
}
state->fPreparedOffset = offset;
state->fPreparedLength = length;
state->fMisaligned = false;
state->fDoubleBuffer = false;
state->fPrepared = false;
state->fCopyNext = NULL;
state->fCopyPageAlloc = NULL;
state->fCopyPageCount = 0;
state->fNextRemapPage = NULL;
state->fCopyMD = NULL;
state->fLocalMapperAlloc = 0;
state->fLocalMapperAllocValid = false;
state->fLocalMapperAllocLength = 0;
state->fSourceAlignMask = fAlignMask;
if (fMapper) {
state->fSourceAlignMask &= page_mask;
}
state->fCursor = state->fIterateOnly
|| (!state->fCheckAddressing
&& (!state->fSourceAlignMask
|| ((fMDSummary.fPageAlign & (1 << 31)) && (0 == (fMDSummary.fPageAlign & state->fSourceAlignMask)))));
if (!state->fCursor) {
IOOptionBits op = kWalkPrepare | kWalkPreflight;
if (synchronize) {
op |= kWalkSyncOut;
}
ret = walkAll(op);
}
if (IS_NONCOHERENT(mappingOptions) && flushCache) {
if (state->fCopyMD) {
state->fCopyMD->performOperation(kIOMemoryIncoherentIOStore, 0, length);
} else {
fMemory->performOperation(kIOMemoryIncoherentIOStore, offset, length);
}
}
if (fMapper) {
IOMDDMAMapArgs mapArgs;
bzero(&mapArgs, sizeof(mapArgs));
mapArgs.fMapper = fMapper.get();
mapArgs.fCommand = this;
mapArgs.fMapSpec.device = state->fDevice;
mapArgs.fMapSpec.alignment = fAlignMask + 1;
mapArgs.fMapSpec.numAddressBits = fNumAddressBits ? ((UInt8) fNumAddressBits) : 64;
mapArgs.fLength = state->fPreparedLength;
OSSharedPtr<IOMemoryDescriptor> md = state->fCopyMD;
if (md) {
mapArgs.fOffset = 0;
} else {
md = fMemory;
mapArgs.fOffset = state->fPreparedOffset;
}
ret = md->dmaCommandOperation(kIOMDDMAMap, &mapArgs, sizeof(mapArgs));
if ((kIOReturnSuccess == ret)
&& mapArgs.fAllocLength
&& (mapArgs.fAllocLength != mapArgs.fLength)) {
do {
// multisegment case
IOMDDMAWalkSegmentState walkState;
IOMDDMAWalkSegmentArgs * walkArgs = (IOMDDMAWalkSegmentArgs *) (void *)&walkState;
IOOptionBits mdOp;
uint64_t index;
IOPhysicalLength segLen;
uint32_t segCount;
uint64_t phys, align;
uint64_t mapperPageMask;
uint64_t mapperPageShift;
uint64_t insertOffset;
uint32_t mapOptions;
uint64_t length;
assert(mapArgs.fAllocLength > mapArgs.fLength);
mapperPageMask = fMapper->getPageSize();
assert(mapperPageMask);
mapperPageMask -= 1;
mapperPageShift = (64 - __builtin_clzll(mapperPageMask));
walkArgs->fMapped = false;
length = state->fPreparedLength;
mdOp = kIOMDFirstSegment;
segCount = 0;
for (index = 0; index < length; segCount++) {
walkArgs->fOffset = state->fPreparedOffset + index;
ret = md->dmaCommandOperation(mdOp, &walkState, sizeof(walkState));
mdOp = kIOMDWalkSegments;
assert(kIOReturnSuccess == ret);
if (ret != kIOReturnSuccess) {
panic("dmaCommandOperation");
}
segLen = walkArgs->fLength;
index += segLen;
}
if (ret != kIOReturnSuccess) {
break;
}
#if defined(LOGTAG)
if (LOGTAG == fMemory->getTag()) {
IOLog("DMA[%p] alloc 0x%qx, 0x%qx\n", this, mapArgs.fAlloc, mapArgs.fAllocLength);
}
#endif /* defined(LOGTAG) */
state->fMapSegments = IONewZero(IODMACommandMapSegment, segCount);
if (!state->fMapSegments) {
ret = kIOReturnNoMemory;
break;
}
state->fMapSegmentsCount = segCount;
switch (kIODirectionOutIn & fMDSummary.fDirection) {
case kIODirectionOut:
mapOptions = kIODMAMapReadAccess;
break;
case kIODirectionIn:
mapOptions = kIODMAMapWriteAccess;
break;
default:
mapOptions = kIODMAMapReadAccess | kIODMAMapWriteAccess;
break;
}
mdOp = kIOMDFirstSegment;
segCount = 0;
for (insertOffset = 0, index = 0; index < length; segCount++) {
walkArgs->fOffset = state->fPreparedOffset + index;
ret = md->dmaCommandOperation(mdOp, &walkState, sizeof(walkState));
mdOp = kIOMDWalkSegments;
if (ret != kIOReturnSuccess) {
panic("dmaCommandOperation 0x%x", ret);
}
phys = walkArgs->fIOVMAddr;
segLen = walkArgs->fLength;
#if defined(LOGTAG)