-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxhci.h
1631 lines (1474 loc) · 53.2 KB
/
xhci.h
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
#pragma once
#include "generic.h"
#include "mem.h"
#include "pci.h"
#include "usb.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <semaphore.h>
#include <pthread.h>
#include "hub.h"
class DevXhci : public DevUsbController {
public:
void Init();
void Run() {
pthread_t tid;
if (pthread_create(&tid, NULL, AttachAll, this) != 0) {
perror("pthread_create:");
exit(1);
}
while(true) {
_pci.WaitInterrupt();
pthread_mutex_lock(&_mp);
_interrupter.Handle();
pthread_mutex_unlock(&_mp);
}
}
virtual bool SendControlTransfer(UsbCtrl::DeviceRequest &request, Memory &mem, size_t data_size, int device_addr) override {
assert(_device_list[device_addr] != nullptr);
return _device_list[device_addr]->SendControlTransfer(request, mem, data_size);
}
virtual void InitHub(int number_of_ports, int ttt, int device_addr) override {
assert(_device_list[device_addr] != nullptr);
return _device_list[device_addr]->InitHub(number_of_ports, ttt);
}
virtual DevUsb *AttachDevice(Hub *hub, int hub_addr, int hub_port_id) override;
virtual ReturnState SetupEndpoint(uint8_t endpt_address, int device_addr, int interval, UsbCtrl::TransferType type, UsbCtrl::PacketIdentification direction, int max_packetsize, RingBuffer<uint8_t *> *buf) override {
assert(_device_list[device_addr] != nullptr);
return _device_list[device_addr]->SetupEndpoint(endpt_address, interval, type, direction, max_packetsize, buf);
}
private:
static const int kCapRegOffsetCapLength = 0x00;
static const int kCapRegOffsetHciVersion = 0x02;
static const int kCapReg32OffsetHcsParams1 = 0x04 / sizeof(uint32_t);
static const int kCapReg32OffsetHcsParams2 = 0x08 / sizeof(uint32_t);
static const int kCapReg32OffsetHccParams1 = 0x10 / sizeof(uint32_t);
static const int kCapReg32OffsetDboff = 0x14 / sizeof(uint32_t);
static const int kCapReg32OffsetRtsoff = 0x18 / sizeof(uint32_t);
static const int kOpRegOffsetUsbCmd = 0x00 / sizeof(uint32_t);
static const int kOpRegOffsetUsbSts = 0x04 / sizeof(uint32_t);
static const int kOpRegOffsetPageSize = 0x08 / sizeof(uint32_t);
static const int kOpRegOffsetCrcr = 0x18 / sizeof(uint32_t);
static const int kOpRegOffsetDcbaap = 0x30 / sizeof(uint32_t);
static const int kOpRegOffsetConfig = 0x38 / sizeof(uint32_t);
static const int kOpRegOffsetPortsc = 0x400 / sizeof(uint32_t);
static const int kRunRegIntRegSet = 0x20 / sizeof(uint32_t);
// Table 23: Host Controller Structural Parameters 1 (HCSPARAMS1)
struct CapReg32HcsParams1MaxSlots {
static const int kOffset = 0;
static const int kLen = 8;
};
struct CapReg32HcsParams1MaxPorts {
static const int kOffset = 24;
static const int kLen = 8;
};
// Table 24: Host Controller Structural Parameters 2 (HCSPARAMS2)
struct CapReg32HcsParams2MaxScratchpadHi {
static const int kOffset = 21;
static const int kLen = 5;
};
struct CapReg32HcsParams2MaxScratchpadLow {
static const int kOffset = 27;
static const int kLen = 5;
};
// Table 26: Host Controller Capability 1 Parameters (HCCPARAMS1)
static const uint32_t kCapReg32HccParams1FlagContextSize = 1 << 2;
static const uint32_t kCapReg32HccParams1FlagPortPowerControl = 1 << 3;
struct CapReg32HccParams1Xecp {
static const int kOffset = 16;
static const int kLen = 16;
};
// Table 27: Doorbell Offset Register (DBOFF)
struct CapReg32DboffDoorbellArrayOffset {
static const int kOffset = 2;
static const int kLen = 31 - 2 + 1;
};
// Table 28: Runtime Register Space Offset Register (RTSOFF)
struct CapReg32TrsoffRuntimeSpaceOffset {
static const int kOffset = 5;
static const int kLen = 31 - 5 + 1;
};
// Table 32: USB Command Register Bit Definitions (USBCMD)
static const uint32_t kOpRegUsbCmdFlagRunStop = 1 << 0;
static const uint32_t kOpRegUsbCmdFlagReset = 1 << 1;
static const uint32_t kOpRegUsbCmdFlagInterrupterEnable = 1 << 2;
// Table 33: USB Status Register Bit Definitions (USBSTS)
static const uint32_t kOpRegUsbStsFlagHchalted = 1 << 0;
static const uint32_t kOpRegUsbStsFlagControllerNotReady = 1 << 11;
// Table 34: Page Size Register Bit Definitions (PAGESIZE)
static const uint32_t kOpRegPageSizeMask = (1 << 16) - 1;
// Table 36: Command Ring Control Register Bit Definitions (CRCR)
static const uint32_t kOpRegCrcrMaskCommandRingPointer = ~0 - ((1 << 6) - 1);
static const uint32_t kOpRegCrcrFlagRingCycleStatus = 1 << 0;
// Table 38: Configure Register Bit Definitions(CONFIG)
struct OpRegConfigMaxSlotsEn {
static const int kOffset = 0;
static const int kLen = 8;
};
// Table 39: Port Status and Control Register Bit Definitions (PORTSC)
static const uint32_t kOpRegPortscFlagCcs = 1 << 0;
static const uint32_t kOpRegPortscFlagPortEnabled = 1 << 1;
static const uint32_t kOpRegPortscFlagPortReset = 1 << 4;
static const uint32_t kOpRegPortscFlagCsc = 1 << 17;
static const uint32_t kOpRegPortscFlagPec = 1 << 18;
static const uint32_t kOpRegPortscFlagWrc = 1 << 19;
static const uint32_t kOpRegPortscFlagOcc = 1 << 20;
static const uint32_t kOpRegPortscFlagPrc = 1 << 21;
static const uint32_t kOpRegPortscFlagPlc = 1 << 22;
static const uint32_t kOpRegPortscFlagCec = 1 << 23;
static const uint32_t kOpRegPortscFlagsRwcBits = kOpRegPortscFlagCsc | kOpRegPortscFlagPec | kOpRegPortscFlagWrc | kOpRegPortscFlagOcc | kOpRegPortscFlagPrc | kOpRegPortscFlagPlc | kOpRegPortscFlagPortEnabled;
struct OpRegPortscPortSpeed {
static const int kOffset = 10;
static const int kLen = 13 - 10 + 1;
};
// Table 53: Doorbell Register Bit Field Definitions
struct DoorbellRegDbTarget {
static const int kOffset = 0;
static const int kLen = 8;
};
struct DoorbellRegDbStreamId {
static const int kOffset = 16;
static const int kLen = 16;
};
// Table 145: Format of xHCI Extended Capability Pointer Register
static const int kExtCapRegOffsetCapabilityId = 0;
static const int kExtCapRegLenCapabilityId = 0b11111111;
static const int kExtCapRegOffsetNextPointer = 8;
static const int kExtCapRegLenNextPointer = 8;
// Table 146: xHCI Extended Capability Codes
static const uint8_t kExCapCodeSupportedProtocol = 2;
// Table 152: xHCI Supported Protocol Capability Field Definitions
static const int kSupportedProtocolCap8OffsetCompatiblePortOffset = 0;
static const int kSupportedProtocolCap8LenCompatiblePortOffset = 8;
static const int kSupportedProtocolCap8OffsetCompatiblePortCount = 8;
static const int kSupportedProtocolCap8LenCompatiblePortCount = 8;
// Table 153: xHCI Supported Protocol Capability Field Definitions
static const int kSupportedProtocolCapCOffsetProtocolSlotType = 0;
static const int kSupportedProtocolCapCLenProtocolSlotType = 4;
volatile uint8_t *_capreg_base_addr;
volatile uint32_t *_capreg_base_addr32;
volatile uint32_t *_opreg_base_addr;
volatile uint32_t *_excapreg_base_addr;
volatile uint32_t *_doorbell_array_base_addr;
volatile uint32_t *_runtime_base_addr;
class Device;
class TrbRingBase {
public:
class Trb {
public:
enum class Direction : bool
{
kOut = false,
kIn = true,
};
virtual void Set(uint32_t *addr, bool cycle_flag) = 0;
static bool GetCycleBit(uint32_t *addr) {
return ((addr[3] & kFlagCycleBit) != 0);
}
static void ToggleCycleBit(uint32_t *addr) {
if (GetCycleBit(addr)) {
addr[3] &= ~kFlagCycleBit;
} else {
addr[3] |= kFlagCycleBit;
}
}
protected:
void SetSub(uint32_t *addr, uint32_t type, bool cycle_flag) {
addr[3]
|= GenerateValue<TrbType, uint32_t>(type)
| (cycle_flag ? kFlagCycleBit : 0);
}
// Table 75: Offset 0Ch – Normal TRB Field Definitions
static const uint32_t kFlagCycleBit = 1 << 0;
struct TrbType {
static const int kOffset = 10;
static const int kLen = 6;
};
};
protected:
void InitSub(DevXhci *hc) {
_hc = hc;
}
DevXhci *_hc;
};
class TrbHandler {
public:
int index;
int handle_index;
bool cycle_flag;
virtual void Handle() = 0;
};
class BlockingTrbHandler : public TrbHandler {
public:
BlockingTrbHandler() {
pthread_cond_init(&_cond, NULL);
}
virtual void Handle() override {
pthread_cond_broadcast(&_cond);
}
void Wait(pthread_mutex_t *mutex) {
pthread_cond_wait(&_cond, mutex);
}
~BlockingTrbHandler() {
pthread_cond_destroy(&_cond);
}
private:
pthread_cond_t _cond;
};
class InTransferRing;
class BufferingNormalTrbHandler : public TrbHandler {
public:
BufferingNormalTrbHandler(InTransferRing *ring) : _ring(ring) {
}
void SetIndexOfRing(int index) {
_index = index;
}
virtual void Handle() override;
private:
InTransferRing *_ring;
int _index;
};
class DummyTrbHandler : public TrbHandler {
public:
DummyTrbHandler(BlockingTrbHandler *bhandler) : _bhandler(bhandler) {
}
virtual void Handle() override {
_bhandler->Handle();
}
private:
BlockingTrbHandler *_bhandler;
};
class TrbRing : public TrbRingBase {
public:
void Init(DevXhci *hc) {
InitSub(hc);
_mem = new Memory(kEntrySize * kEntryNum);
_ring_address = _mem->GetVirtPtr<uint32_t>();
_enqueue_index = 0;
_cycle_flag = true;
pthread_cond_init(&_cond, NULL);
for (int i = 0; i < sizeof(_context) / sizeof(_context[0]); i++) {
_context[i].status = ContextStatus::kOwnedBySoftware;
}
memset(_ring_address, 0, kEntrySize * kEntryNum);
// set link TRB
LinkTrb trb(_mem->GetPhysPtr());
trb.Set(_ring_address + (kEntryNum - 1) * (kEntrySize / sizeof(uint32_t)), true);
}
Memory &GetMemory() {
return *_mem;
}
int GetIndexFromEntryAddr(phys_addr addr) {
assert(_mem->GetPhysPtr() <= addr);
int index = (addr - _mem->GetPhysPtr()) / kEntrySize;
assert(index < kEntryNum);
return index;
}
static const int kEntrySize = 16;
static const int kEntryNum = 256;
protected:
class LinkTrb : public Trb {
public:
LinkTrb(phys_addr ring_address) : _ring_address(ring_address) {
}
virtual void Set(uint32_t *addr, bool cycle_flag) override {
addr[0] = _ring_address;
addr[1] = _ring_address >> 32;
addr[2] = 0;
addr[3] = kLinkTrbFlagToggleCycle;
SetSub(addr, kValueTrbType, cycle_flag);
}
static void ToggleCycle(uint32_t *addr) {
}
private:
// Table 134: Offset 0Ch – Link TRB Field Definitions
static const uint32_t kLinkTrbFlagToggleCycle = 1 << 1;
// Table 139: TRB Type Definitions
static const uint32_t kValueTrbType = 6;
const phys_addr _ring_address;
};
void AllocTrb(TrbHandler &handler, pthread_mutex_t *mutex) {
while(_context[_enqueue_index].status == ContextStatus::kOwnedByHardware) {
if (pthread_cond_wait(&_cond, mutex) < 0) {
perror("pthread_cond_wait:");
}
}
handler.index = _enqueue_index;
handler.cycle_flag = _cycle_flag;
_context[_enqueue_index].handler = &handler;
_context[_enqueue_index].status = ContextStatus::kOwnedByHardware;
_enqueue_index++;
if (_enqueue_index == kEntryNum - 1) {
_enqueue_index = 0;
_cycle_flag = !_cycle_flag;
LinkTrb trb(0); // dummy
assert(_cycle_flag != trb.GetCycleBit(_ring_address + (kEntryNum - 1) * (kEntrySize / sizeof(uint32_t))));
trb.ToggleCycleBit(_ring_address + (kEntryNum - 1) * (kEntrySize / sizeof(uint32_t)));
uint32_t *tmp = _ring_address + (kEntryNum - 1) * (kEntrySize / sizeof(uint32_t));
}
return;
}
// release trb from consumer
TrbHandler *ReleaseTrb(int index) {
assert(index < kEntryNum);
TrbContext *context = &_context[index];
assert(context->status == ContextStatus::kOwnedByHardware);
if (pthread_cond_signal(&_cond) < 0) {
perror("pthread_cond_signal:");
}
context->handler->handle_index = index;
context->status = ContextStatus::kOwnedBySoftware;
context->handler->Handle();
return context->handler;
}
uint32_t *_ring_address;
private:
enum class ContextStatus : bool
{
kOwnedByHardware,
kOwnedBySoftware,
};
struct TrbContext {
ContextStatus status;
TrbHandler *handler;
};
Memory *_mem;
int _enqueue_index;
bool _cycle_flag;
TrbContext _context[kEntryNum];
pthread_cond_t _cond;
};
enum class TrbCompletionCode : uint8_t
{
kSuccess = 1,
};
static const char* const _completion_code_table[];
static const char * const GetString(TrbCompletionCode code) {
return _completion_code_table[static_cast<uint8_t>(code)];
}
class TransferRing : public TrbRing {
public:
struct CompletionInfo {
int transfer_length;
TrbCompletionCode completion_code;
bool event_data;
uint8_t endpoint_id;
uint8_t slot_id;
};
class TransferTrb : public Trb {
public:
TransferTrb(bool chain, bool ioc, bool idt) : _chain(chain), _ioc(ioc), _idt(idt) {
}
bool GetIoc() {
return _ioc;
}
void SetSub(uint32_t *addr, uint32_t type, bool cycle_flag) {
addr[3]
|= (_chain ? kFlagChainBit : 0)
| (_ioc ? kFlagInterruptOnComplete : 0)
| (_idt ? kFlagImmediateData : 0);
Trb::SetSub(addr, type, cycle_flag);
}
public:
// Table 79: Offset 0Ch – Setup Stage TRB Field Definitions
static const int kFlagChainBit = 1 << 4;
static const int kFlagInterruptOnComplete = 1 << 5;
static const int kFlagImmediateData = 1 << 6;
const bool _chain;
const bool _ioc;
const bool _idt;
};
class NormalTrb : public TransferTrb {
public:
NormalTrb() = delete;
NormalTrb(phys_addr addr, int transfer_len, bool ioc, bool idt) : TransferTrb(false, ioc, idt), _addr(addr), _transfer_len(transfer_len) {
}
virtual void Set(uint32_t *addr, bool cycle_flag) override {
addr[0] = _addr;
addr[1] = _addr >> 32;
addr[2]
= GenerateValue<TransferLength, uint32_t>(_transfer_len)
| GenerateValue<TdSize, uint32_t>(0)
| GenerateValue<InterruptTarget, uint32_t>(0);
addr[3] = 0;
SetSub(addr, kValueTrbType, cycle_flag);
}
private:
// Table 139: TRB Type Definitions
static const uint32_t kValueTrbType = 1;
// Table 74: Offset 08h – Normal TRB Field Definitions Bits
struct TransferLength {
static const int kOffset = 0;
static const int kLen = 16;
};
struct TdSize {
static const int kOffset = 17;
static const int kLen = 21 - 17 + 1;
};
struct InterruptTarget {
static const int kOffset = 22;
static const int kLen = 10;
};
const phys_addr _addr;
const int _transfer_len;
};
class SetupStageTrb : public TransferTrb {
public:
enum class ValueTransferType : uint8_t
{
kNoDataStage = 0,
kOutDataStage = 2,
kInDataStage = 3,
};
SetupStageTrb() = delete;
SetupStageTrb(ValueTransferType type, bool ioc, bool idt, UsbCtrl::DeviceRequest &request) : TransferTrb(false, ioc, idt), _request(request), _type(type) {
}
virtual void Set(uint32_t *addr, bool cycle_flag) override {
memcpy(addr, &_request, sizeof(UsbCtrl::DeviceRequest));
addr[2]
= GenerateValue<TrbTransferLength, uint32_t>(8)
| GenerateValue<InterruptTarget, uint32_t>(0);
addr[3]
= GenerateValue<TransferType, uint32_t>(static_cast<uint8_t>(_type));
SetSub(addr, kValueTrbType, cycle_flag);
}
private:
// Table 139: TRB Type Definitions
static const uint32_t kValueTrbType = 2;
// Table 78: Offset 08h – Setup Stage TRB Field Definitions
struct TrbTransferLength {
static const int kOffset = 0;
static const int kLen = 16;
};
struct InterruptTarget {
static const int kOffset = 22;
static const int kLen = 10;
};
// Table 79: Offset 0Ch – Setup Stage TRB Field Definitions
struct TransferType {
static const int kOffset = 16;
static const int kLen = 2;
};
const ValueTransferType _type;
const UsbCtrl::DeviceRequest _request;
};
class DataStageTrb : public TransferTrb {
public:
DataStageTrb() = delete;
DataStageTrb(Direction dir, int transfer_len, bool chain, bool ioc, bool idt, phys_addr buf) : TransferTrb(chain, ioc, idt), _dir(dir), _transfer_len(transfer_len), _buf(buf) {
}
virtual void Set(uint32_t *addr, bool cycle_flag) override {
addr[0] = _buf;
addr[1] = _buf >> 32;
addr[2]
= GenerateValue<TransferLength, uint32_t>(_transfer_len)
| GenerateValue<TdSize, uint32_t>(0)
| GenerateValue<InterruptTarget, uint32_t>(0);
addr[3]
= (static_cast<bool>(_dir) ? kFlagDirection : 0);
SetSub(addr, kValueTrbType, cycle_flag);
}
private:
// Table 139: TRB Type Definitions
static const uint32_t kValueTrbType = 3;
// Table 81: Offset 08h – Data Stage TRB Field Definitions
struct TransferLength {
static const int kOffset = 0;
static const int kLen = 17;
};
struct TdSize {
static const int kOffset = 17;
static const int kLen = 21 - 17 + 1;
};
struct InterruptTarget {
static const int kOffset = 22;
static const int kLen = 31 - 22 + 1;
};
// Table 82: Offset 0Ch – Data Stage TRB Field Definitions
static const int kFlagEvaluateNextTrb = 1 << 1;
static const int kFlagInterruptOnShortPacket = 1 << 2;
static const int kFlagNoSnoop = 1 << 3;
static const int kFlagDirection = 1 << 16;
const Direction _dir;
const int _transfer_len;
const phys_addr _buf;
};
class StatusStageTrb : public TransferTrb {
public:
StatusStageTrb() = delete;
StatusStageTrb(Direction dir, bool chain, bool ioc, bool idt) : TransferTrb(chain, ioc, idt), _dir(dir) {
}
virtual void Set(uint32_t *addr, bool cycle_flag) override {
addr[0] = 0;
addr[1] = 0;
addr[2]
= GenerateValue<InterruptTarget, uint32_t>(0);
addr[3]
= (static_cast<bool>(_dir) ? kFlagDirection : 0);
SetSub(addr, kValueTrbType, cycle_flag);
}
private:
// Table 139: TRB Type Definitions
static const uint32_t kValueTrbType = 4;
// Table 83: Offset 08h – Status Stage TRB Field Definitions
struct InterruptTarget {
static const int kOffset = 22;
static const int kLen = 31 - 22 + 1;
};
// Table 84: Offset 0Ch – Status Stage TRB Field Definitions
static const int kFlagDirection = 1 << 16;
const Direction _dir;
};
void Init(DevXhci *hc) = delete;
void Init(Device *device, int dci) {
_device = device;
_dci = dci;
TrbRing::Init(device->GetHc());
}
void CompleteTransfer(int index, CompletionInfo &info) {
_info[index] = info;
ReleaseTrb(index);
}
// insert TRBs to the ring. get state from completion event.
CompletionInfo Issue(TransferTrb *trb[], const int array_len, pthread_mutex_t *mutex) {
BlockingTrbHandler bhandler;
for(int i = 0; i < array_len - 1; i++) {
assert(!trb[i]->GetIoc());
DummyTrbHandler handler(&bhandler);
AllocTrb(handler, mutex);
trb[i]->Set(_ring_address + handler.index * (kEntrySize / sizeof(uint32_t)), handler.cycle_flag);
}
assert(trb[array_len - 1]->GetIoc());
AllocTrb(bhandler, mutex);
trb[array_len - 1]->Set(_ring_address + bhandler.index * (kEntrySize / sizeof(uint32_t)), bhandler.cycle_flag);
_device->RingEndpointDoorbell(_dci);
bhandler.Wait(mutex);
return _info[bhandler.handle_index];
}
protected:
CompletionInfo _info[kEntryNum];
Device *_device;
int _dci;
};
class OutTransferRing : public TransferRing {
public:
};
class InTransferRing : public TransferRing {
public:
~InTransferRing() {
delete _mem;
}
void Fill(pthread_mutex_t *mutex, int max_packet_size, RingBuffer<uint8_t *> *buf) {
_mutex = mutex;
_max_packet_size = max_packet_size;
_buf = buf;
_mem = new Memory(max_packet_size * (kEntryNum - 1));
for (int i = 0; i < kEntryNum - 1; i++) {
TransferRing::NormalTrb trb(_mem->GetPhysPtr() + i * max_packet_size, max_packet_size, true, false);
_handlers[i] = new BufferingNormalTrbHandler(this);
_handlers[i]->SetIndexOfRing(i);
AllocTrb(*_handlers[i], mutex);
assert(i == _handlers[i]->index);
trb.Set(_ring_address + _handlers[i]->index * (kEntrySize / sizeof(uint32_t)), _handlers[i]->cycle_flag);
}
}
void Handle(int index) {
uint8_t *data = new uint8_t[_max_packet_size];
memcpy(data, _mem->GetVirtPtr<uint8_t>() + index * _max_packet_size, _max_packet_size);
_buf->Push(data);
TransferRing::NormalTrb trb(_mem->GetPhysPtr() + index * _max_packet_size, _max_packet_size, true, false);
AllocTrb(*_handlers[index], _mutex);
trb.Set(_ring_address + _handlers[index]->index * (kEntrySize / sizeof(uint32_t)), _handlers[index]->cycle_flag);
}
private:
Memory *_mem = nullptr;
BufferingNormalTrbHandler *_handlers[kEntryNum - 1];
RingBuffer<uint8_t *> *_buf;
int _max_packet_size;
pthread_mutex_t *_mutex;
};
class CommandRing : public TrbRing {
public:
struct CompletionInfo {
TrbCompletionCode completion_code;
uint32_t completion_parameter;
uint8_t slot_id;
};
class EnableSlotCommandTrb : public Trb {
public:
EnableSlotCommandTrb() = delete;
EnableSlotCommandTrb(uint32_t slot_type) : _slot_type(slot_type) {
}
virtual void Set(uint32_t *addr, bool cycle_flag) override {
addr[0] = 0;
addr[1] = 0;
addr[2] = 0;
addr[3]
= GenerateValue<SlotType, uint32_t>(_slot_type);
SetSub(addr, kValueTrbType, cycle_flag);
}
private:
// Table 112: Offset 0Ch – Enable Slot Command TRB Field Definitions
struct SlotType {
static const int kOffset = 16;
static const int kLen = 5;
};
// Table 139: TRB Type Definitions
static const uint32_t kValueTrbType = 9;
const uint32_t _slot_type;
};
class DisableSlotCommandTrb : public Trb {
public:
DisableSlotCommandTrb() = delete;
DisableSlotCommandTrb(uint8_t slot_id) : _slot_id(slot_id) {
}
virtual void Set(uint32_t *addr, bool cycle_flag) override {
addr[0] = 0;
addr[1] = 0;
addr[2] = 0;
addr[3]
= GenerateValue<SlotId, uint32_t>(_slot_id);
SetSub(addr, kValueTrbType, cycle_flag);
}
private:
// Table 113: Offset 0Ch – Disable Slot Command TRB Field Definitions
struct SlotId {
static const int kOffset = 24;
static const int kLen = 31 - 24 + 1;
};
// Table 139: TRB Type Definitions
static const uint32_t kValueTrbType = 10;
const uint8_t _slot_id;
};
class AddressDeviceCommandTrb : public Trb {
public:
AddressDeviceCommandTrb() = delete;
AddressDeviceCommandTrb(phys_addr input_context, uint8_t slot_id, bool bsr) : _input_context(input_context), _slot_id(slot_id), _bsr(bsr) {
}
virtual void Set(uint32_t *addr, bool cycle_flag) override {
addr[0] = _input_context;
addr[1] = _input_context >> 32;
addr[2] = 0;
addr[3]
= GenerateValue<SlotId, uint32_t>(_slot_id)
| (_bsr ? kFlagBlockSetAddressRequest : 0);
SetSub(addr, kValueTrbType, cycle_flag);
}
private:
// Table 115: Offset 0Ch – Address Device Command TRB Field Definitions
struct SlotId {
static const int kOffset = 24;
static const int kLen = 8;
};
static const int kFlagBlockSetAddressRequest = 1 << 9;
// Table 139: TRB Type Definitions
static const uint32_t kValueTrbType = 11;
const phys_addr _input_context;
const uint8_t _slot_id;
const bool _bsr;
};
class ConfigureEndpointCommandTrb : public Trb {
public:
ConfigureEndpointCommandTrb() = delete;
ConfigureEndpointCommandTrb(phys_addr input_context, uint8_t slot_id, bool deconfigure) : _input_context(input_context), _slot_id(slot_id), _deconfigure(deconfigure) {
}
virtual void Set(uint32_t *addr, bool cycle_flag) override {
addr[0] = _input_context;
addr[1] = _input_context >> 32;
addr[2] = 0;
addr[3]
= (_deconfigure ? kFlagDeconfigure : 0)
| GenerateValue<SlotId, uint32_t>(_slot_id);
SetSub(addr, kValueTrbType, cycle_flag);
}
private:
// Table 117: Offset 0Ch – Configure Endpoint Command TRB Field Definitions
static const uint32_t kFlagDeconfigure = 1 << 9;
struct SlotId {
static const int kOffset = 24;
static const int kLen = 8;
};
// Table 139: TRB Type Definitions
static const uint32_t kValueTrbType = 12;
const phys_addr _input_context;
const uint8_t _slot_id;
const bool _deconfigure;
};
class EvaluateContextCommandTrb : public Trb {
public:
EvaluateContextCommandTrb() = delete;
EvaluateContextCommandTrb(phys_addr input_context, uint8_t slot_id) : _input_context(input_context), _slot_id(slot_id) {
}
virtual void Set(uint32_t *addr, bool cycle_flag) override {
addr[0] = _input_context;
addr[1] = _input_context >> 32;
addr[2] = 0;
addr[3]
= GenerateValue<SlotId, uint32_t>(_slot_id);
SetSub(addr, kValueTrbType, cycle_flag);
}
private:
// note: The Evaluate Context Command TRB uses the same format as the Address Device Command TRB
// Table 115: Offset 0Ch – Address Device Command TRB Field Definitions
struct SlotId {
static const int kOffset = 24;
static const int kLen = 8;
};
// Table 139: TRB Type Definitions
static const uint32_t kValueTrbType = 13;
const phys_addr _input_context;
const uint8_t _slot_id;
};
class ResetDeviceCommandTrb : public Trb {
public:
ResetDeviceCommandTrb() = delete;
ResetDeviceCommandTrb(uint8_t slot_id) : _slot_id(slot_id) {
}
virtual void Set(uint32_t *addr, bool cycle_flag) override {
addr[0] = 0;
addr[1] = 0;
addr[2] = 0;
addr[3]
= GenerateValue<SlotId, uint32_t>(_slot_id);
SetSub(addr, kValueTrbType, cycle_flag);
}
private:
// Table 123: Offset 0Ch – Address Device Command TRB Field Definitions
struct SlotId {
static const int kOffset = 24;
static const int kLen = 8;
};
// Table 139: TRB Type Definitions
static const uint32_t kValueTrbType = 17;
const uint8_t _slot_id;
};
CompletionInfo Issue(Trb &trb, pthread_mutex_t *mutex) {
BlockingTrbHandler handler;
AllocTrb(handler, mutex);
memset(_ring_address + handler.index * (kEntrySize / sizeof(uint32_t)), 0, kEntrySize);
trb.Set(_ring_address + handler.index * (kEntrySize / sizeof(uint32_t)), handler.cycle_flag);
_hc->RingCommandDoorbell();
handler.Wait(mutex);
return _completion_info[handler.handle_index];
}
void CompleteCommand(int index, CompletionInfo &completion_info) {
_completion_info[index] = completion_info;
ReleaseTrb(index);
}
private:
CompletionInfo _completion_info[kEntryNum];
};
struct ContainerForPortStatusChangeHandler {
DevXhci *that;
int root_port_id;
};
class EventRing : public TrbRingBase {
public:
void Init(DevXhci *hc) {
InitSub(hc);
_mem = new Memory(kEntrySize * kEntryNum);
memset(_mem->GetVirtPtr<uint8_t>(), 0, kEntrySize * kEntryNum);
_consumer_cycle_bit = true;
}
Memory &GetMemory() {
return *_mem;
}
int GetEntryNum() {
return kEntryNum;
}
// return value: dequeue_ptr is incremented or not
bool Handle(phys_addr &dequeue_ptr);
private:
class EventTrb : public Trb {
public:
EventTrb() = delete;
EventTrb(uint32_t *addr) : _addr(addr) {
}
virtual void Set(uint32_t *addr, bool cycle_flag) override {
assert(false);
}
bool GetCycleBit() {
return (_addr[3] & kFlagCycleBit) != 0;
}
uint32_t GetType() {
return MaskValue<TrbType>(_addr[3]);
}
void ShowErrUnknown() {
printf("warning: unknown event trb: %d\n", MaskValue<TrbType>(_addr[3]));
fflush(stdout);
}
protected:
uint32_t *_addr;
};
class TransferEventTrb : public EventTrb {
public:
TransferEventTrb() = delete;
TransferEventTrb(uint32_t *addr) : EventTrb(addr) {
}
void SetContainer(TransferRing::CompletionInfo &info, phys_addr &pointer) {
pointer = _addr[1];
pointer = (pointer << 32)| _addr[0];
info.transfer_length = MaskValue<TransferLength>(_addr[2]);
info.completion_code = static_cast<TrbCompletionCode>(MaskValue<CompletionCode>(_addr[2]));
info.event_data = (_addr[3] & kFlagEventData) ? true : false;
info.endpoint_id = MaskValue<EndpointId>(_addr[3]);
info.slot_id = MaskValue<SlotId>(_addr[3]);
}
// Table 139: TRB Type Definitions
static const uint32_t kValueTrbType = 32;
private:
// Table 91: Offset 08h – Transfer Event TRB Field Definitions
struct TransferLength {
static const int kOffset = 0;
static const int kLen = 23 - 0 + 1;
};
struct CompletionCode {
static const int kOffset = 24;
static const int kLen = 31 - 24 + 1;
};
// Table 92: Offset 0Ch – Transfer Event TRB Field Definitions
static const uint32_t kFlagEventData = 1 << 2;
struct EndpointId {
static const int kOffset = 16;
static const int kLen = 20 - 16 + 1;
};
struct SlotId {
static const int kOffset = 24;
static const int kLen = 31 - 24 + 1;
};
};
class CommandCompletionEventTrb : public EventTrb {
public:
CommandCompletionEventTrb() = delete;
CommandCompletionEventTrb(uint32_t *addr) : EventTrb(addr) {
}
void SetContainer(CommandRing::CompletionInfo &info, phys_addr &pointer) {
pointer = _addr[1];
pointer = (pointer << 32)| _addr[0];
pointer &= GenerateMask<CommandTrbPointer, uint64_t>();
info.completion_code = static_cast<TrbCompletionCode>(MaskValue<CompletionCode>(_addr[2]));
info.completion_parameter = MaskValue<Parameter>(_addr[2]);
info.slot_id = MaskValue<SlotId>(_addr[3]);
}
// Table 139: TRB Type Definitions
static const uint32_t kValueTrbType = 33;
private:
// Table 93: Offset 00h and 04h – Command Completion Event TRB Field Definition
struct CommandTrbPointer {
static const int kOffset = 4;
static const int kLen = 63 - 4 + 1;
};
// Table 94: Offset 08h – Command Completion Event TRB Field Definitions
struct Parameter {
static const int kOffset = 0;
static const int kLen = 23 - 0 + 1;
};
struct CompletionCode {
static const int kOffset = 24;
static const int kLen = 31 - 24 + 1;
};
// Table 95: Offset 0Ch – Command Completion Event TRB Field Definitions Bits
struct SlotId {
static const int kOffset = 24;
static const int kLen = 31 - 24 + 1;
};
};
class PortStatusChangeEventTrb : public EventTrb {
public:
PortStatusChangeEventTrb() = delete;
PortStatusChangeEventTrb(uint32_t *addr) : EventTrb(addr) {
}
void SetContainer(ContainerForPortStatusChangeHandler &container) {
container.root_port_id = MaskValue<PortId>(_addr[0]);
}
// Table 139: TRB Type Definitions
static const uint32_t kValueTrbType = 34;
private:
// Table 96: Offset 00h – Port Status Change Event TRB Field Definitions
struct PortId {
static const int kOffset = 24;
static const int kLen = 31 - 24 + 1;
};
};
Memory *_mem;
static const int kEntrySize = 16;
static const int kEntryNum = 256;
bool _consumer_cycle_bit;
};
class EventRingSegmentTable {
public: