summaryrefslogtreecommitdiff
path: root/sys/src/9/port/pci.c
blob: 5ca42d5ac6833f15af57666b62d23deccc5241aa (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "../port/pci.h"

typedef struct Pcisiz Pcisiz;
struct Pcisiz
{
	Pcidev*	dev;
	vlong	siz;
	int	bar;
	int	typ;
};

int pcimaxdno;

static Lock pcicfglock;
static Pcidev *pcilist, **pcitail;

static char* bustypes[] = {
	"CBUSI",
	"CBUSII",
	"EISA",
	"FUTURE",
	"INTERN",
	"ISA",
	"MBI",
	"MBII",
	"MCA",
	"MPI",
	"MPSA",
	"NUBUS",
	"PCI",
	"PCMCIA",
	"TC",
	"VL",
	"VME",
	"XPRESS",
};

int
tbdffmt(Fmt* fmt)
{
	int type, tbdf;

	switch(fmt->r){
	default:
		return fmtstrcpy(fmt, "(tbdffmt)");

	case 'T':
		tbdf = va_arg(fmt->args, int);
		if(tbdf == BUSUNKNOWN) {
			return fmtstrcpy(fmt, "unknown");
		} else {
			type = BUSTYPE(tbdf);
			if(type < nelem(bustypes)) {
				return fmtprint(fmt, "%s.%d.%d.%d",
					bustypes[type], BUSBNO(tbdf), BUSDNO(tbdf), BUSFNO(tbdf));
			} else {
				return fmtprint(fmt, "%d.%d.%d.%d",
					type, BUSBNO(tbdf), BUSDNO(tbdf), BUSFNO(tbdf));
			}
		}
	}
}

static Pcidev*
pcidevalloc(void)
{
	Pcidev *p;

	p = xalloc(sizeof(*p));
	if(p == nil)
		panic("pci: no memory for Pcidev");
	return p;
}

void
pcidevfree(Pcidev *p)
{
	Pcidev **l;

	if(p == nil)
		return;

	while(p->bridge != nil)
		pcidevfree(p->bridge);

	if(p->parent != nil){
		for(l = &p->parent->bridge; *l != nil; l = &(*l)->link) {
			if(*l == p) {
				*l = p->link;
				break;
			}
		}
	}
	for(l = &pcilist; *l != nil; l = &(*l)->list) {
		if(*l == p) {
			if((*l = p->list) == nil)
				pcitail = l;
			break;
		}
	}
	/* leaked */
}

int
pcicfgr8(Pcidev* p, int rno)
{
	int data;

	ilock(&pcicfglock);
	data = pcicfgrw8(p->tbdf, rno, 0, 1);
	iunlock(&pcicfglock);

	return data;
}
void
pcicfgw8(Pcidev* p, int rno, int data)
{
	ilock(&pcicfglock);
	pcicfgrw8(p->tbdf, rno, data, 0);
	iunlock(&pcicfglock);
}
int
pcicfgr16(Pcidev* p, int rno)
{
	int data;

	ilock(&pcicfglock);
	data = pcicfgrw16(p->tbdf, rno, 0, 1);
	iunlock(&pcicfglock);

	return data;
}
void
pcicfgw16(Pcidev* p, int rno, int data)
{
	ilock(&pcicfglock);
	pcicfgrw16(p->tbdf, rno, data, 0);
	iunlock(&pcicfglock);
}
int
pcicfgr32(Pcidev* p, int rno)
{
	int data;

	ilock(&pcicfglock);
	data = pcicfgrw32(p->tbdf, rno, 0, 1);
	iunlock(&pcicfglock);

	return data;
}
void
pcicfgw32(Pcidev* p, int rno, int data)
{
	ilock(&pcicfglock);
	pcicfgrw32(p->tbdf, rno, data, 0);
	iunlock(&pcicfglock);
}

vlong
pcibarsize(Pcidev *p, int rno)
{
	vlong size;
	int v;

	ilock(&pcicfglock);

	v = pcicfgrw32(p->tbdf, rno, 0, 1);
	pcicfgrw32(p->tbdf, rno, -1, 0);
	size = (int)pcicfgrw32(p->tbdf, rno, 0, 1);
	pcicfgrw32(p->tbdf, rno, v, 0);

	if(rno == PciEBAR0 || rno == PciEBAR1)
		size &= ~0x7FFLL;
	else if(v & 1)
		size = (short)size & ~0x3LL;
	else {
		size &= ~0xFLL;

		if(size >= 0 && (v&7)==4 && rno < PciBAR0+4*(nelem(p->mem)-1)){
			rno += 4;
			v = pcicfgrw32(p->tbdf, rno, 0, 1);
			pcicfgrw32(p->tbdf, rno, -1, 0);
			size |= (vlong)pcicfgrw32(p->tbdf, rno, 0, 1)<<32;
			pcicfgrw32(p->tbdf, rno, v, 0);
		}
	}
	size = -size;

	iunlock(&pcicfglock);

	if(size < 0 || (size & size-1) != 0){
		print("pcibarsize: %T invalid bar rno %#x size %#llux mask %#llux\n",
			p->tbdf, rno, size, -size);
		return 0;
	}
	return size;
}

void
pcisetbar(Pcidev *p, int rno, uvlong bar)
{
	ilock(&pcicfglock);
	pcicfgrw32(p->tbdf, rno, bar, 0);
	if((bar&7) == 4 && rno >= PciBAR0 && rno < PciBAR0+4*(nelem(p->mem)-1))
		pcicfgrw32(p->tbdf, rno+4, bar>>32, 0);
	iunlock(&pcicfglock);
}

void
pcisetwin(Pcidev *p, uvlong base, uvlong limit)
{
	ilock(&pcicfglock);
	if(base & 1){
		pcicfgrw16(p->tbdf, PciIBR, (limit & 0xF000)|((base & 0xF000)>>8), 0);
		pcicfgrw32(p->tbdf, PciIUBR, (limit & 0xFFFF0000)|(base>>16), 0);
	} else if(base & 8){
		pcicfgrw32(p->tbdf, PciPMBR, (limit & 0xFFF00000)|((base & 0xFFF00000)>>16), 0);
		pcicfgrw32(p->tbdf, PciPUBR, base >> 32, 0);
		pcicfgrw32(p->tbdf, PciPULR, limit >> 32, 0);
	} else {
		pcicfgrw32(p->tbdf, PciMBR, (limit & 0xFFF00000)|((base & 0xFFF00000)>>16), 0);
	}
	iunlock(&pcicfglock);
}

static int
pcisizcmp(void *a, void *b)
{
	Pcisiz *aa = a, *bb = b;

	if(aa->siz > bb->siz)
		return 1;
	if(aa->siz < bb->siz)
		return -1;
	return 0;
}

static vlong
pcimask(vlong v)
{
	uvlong m;

	for(m = 1ULL<<63; m != 0; m >>= 1) {
		if(m & v)
			break;
	}

	m--;
	if(v & m){
		v |= m;
		v++;
	}
	return v;
}

void
pcibusmap(Pcidev *root, uvlong *pmema, ulong *pioa, int wrreg)
{
	Pcidev *p;
	int ntb, i, rno;
	uvlong mema, smema;
	ulong ioa, sioa, v;
	vlong hole, size;
	Pcisiz *table, *tptr, *mtb, *itb;

	ioa = *pioa;
	mema = *pmema;

	ntb = 0;
	for(p = root; p != nil; p = p->link)
		ntb++;

	ntb *= (PciCIS-PciBAR0)/4;
	table = malloc((2*ntb+1)*sizeof(Pcisiz));
	if(table == nil)
		panic("pcibusmap: can't allocate memory");
	itb = table;
	mtb = table+ntb;

	/*
	 * Build a table of sizes
	 */
	for(p = root; p != nil; p = p->link) {
		if(p->ccrb == 0x06) {
			/* carbus bridge? */
			if(p->ccru == 0x07){
				if(pcicfgr32(p, PciBAR0) & 1)
					continue;
				size = pcibarsize(p, PciBAR0);
				if(size == 0)
					continue;
				mtb->dev = p;
				mtb->bar = 0;
				mtb->siz = size;
				mtb->typ = 0;
				mtb++;
				continue;
			}

			/* pci bridge? */
			if(p->ccru != 0x04 || p->bridge == nil)
				continue;

			sioa = ioa;
			smema = mema;
			pcibusmap(p->bridge, &smema, &sioa, 0);

			hole = pcimask(sioa-ioa);
			if(hole < (1<<12))
				hole = 1<<12;
			itb->dev = p;
			itb->bar = -1;
			itb->siz = hole;
			itb->typ = 0;
			itb++;

			hole = pcimask(smema-mema);
			if(hole < (1<<20))
				hole = 1<<20;
			mtb->dev = p;
			mtb->bar = -1;
			mtb->siz = hole;
			mtb->typ = 0;
			mtb++;

			size = pcibarsize(p, PciEBAR1);
			if(size != 0){
				mtb->dev = p;
				mtb->bar = -3;
				mtb->siz = size;
				mtb->typ = 0;
				mtb++;
			}
			continue;
		}

		size = pcibarsize(p, PciEBAR0);
		if(size != 0){
			mtb->dev = p;
			mtb->bar = -2;
			mtb->siz = size;
			mtb->typ = 0;
			mtb++;
		}

		for(i = 0; i < nelem(p->mem); i++) {
			rno = PciBAR0 + i*4;
			v = pcicfgr32(p, rno);
			size = pcibarsize(p, rno);
			if(size == 0)
				continue;
			if(v & 1) {
				itb->dev = p;
				itb->bar = i;
				itb->siz = size;
				itb->typ = 1;
				itb++;
			} else {
				mtb->dev = p;
				mtb->bar = i;
				mtb->siz = size;
				mtb->typ = v & 7;
				if(mtb->typ == 4)
					i++;
				mtb++;
			}
		}
	}

	/*
	 * Sort both tables IO smallest first, Memory largest
	 */
	qsort(table, itb-table, sizeof(Pcisiz), pcisizcmp);
	tptr = table+ntb;
	qsort(tptr, mtb-tptr, sizeof(Pcisiz), pcisizcmp);

	/*
	 * Allocate IO address space on this bus
	 */
	for(tptr = table; tptr < itb; tptr++) {
		hole = tptr->siz;
		if(tptr->bar == -1)
			hole = 1<<12;
		ioa = (ioa+hole-1) & ~(hole-1);
		if(wrreg){
			p = tptr->dev;
			if(tptr->bar == -1) {
				p->ioa.bar = ioa;
				p->ioa.size = tptr->siz;
			} else {
				p->mem[tptr->bar].size = tptr->siz;
				p->mem[tptr->bar].bar = ioa|1;
				pcisetbar(p, PciBAR0+tptr->bar*4, p->mem[tptr->bar].bar);
			}
		}
		ioa += tptr->siz;
	}

	/*
	 * Allocate Memory address space on this bus
	 */
	for(tptr = table+ntb; tptr < mtb; tptr++) {
		hole = tptr->siz;
		if(tptr->bar == -1)
			hole = 1<<20;
		mema = (mema+hole-1) & ~((uvlong)hole-1);
		if(wrreg){
			p = tptr->dev;
			if(tptr->bar == -1) {
				p->mema.bar = mema;
				p->mema.size = tptr->siz;
			} else if(tptr->bar == -2) {
				p->rom.bar = mema|1;
				p->rom.size = tptr->siz;
				pcisetbar(p, PciEBAR0, p->rom.bar);
			} else if(tptr->bar == -3) {
				p->rom.bar = mema|1;
				p->rom.size = tptr->siz;
				pcisetbar(p, PciEBAR1, p->rom.bar);
			} else {
				p->mem[tptr->bar].size = tptr->siz;
				p->mem[tptr->bar].bar = mema|tptr->typ;
				pcisetbar(p, PciBAR0+tptr->bar*4, p->mem[tptr->bar].bar);
			}
		}
		mema += tptr->siz;
	}

	*pmema = mema;
	*pioa = ioa;
	free(table);

	if(wrreg == 0)
		return;

	/*
	 * Finally set all the bridge addresses & registers
	 */
	for(p = root; p != nil; p = p->link) {
		if(p->bridge == nil) {
			pcienable(p);
			continue;
		}

		/* Set I/O and Mem windows */
		pcisetwin(p, p->ioa.bar|1, p->ioa.bar+p->ioa.size-1);
		pcisetwin(p, p->mema.bar|0, p->mema.bar+p->mema.size-1);

		/* Disable prefetch */
		pcisetwin(p, 0xFFF00000|8, 0);

		/* Enable the bridge */
		pcienable(p);

		sioa = p->ioa.bar;
		smema = p->mema.bar;
		pcibusmap(p->bridge, &smema, &sioa, 1);
	}
}

static int
pcivalidwin(Pcidev *p, uvlong base, uvlong limit)
{
	Pcidev *bridge = p->parent;
	char *typ;

	if(base & 1){
		typ = "io";
		base &= ~3;
		if(base > limit)
			return 0;
		if(bridge == nil)
			return 1;
		if(base >= bridge->ioa.bar && limit < (bridge->ioa.bar + bridge->ioa.size))
			return 1;
	} else {
		typ = "mem";
		base &= ~0xFULL;
		if(base > limit)
			return 0;
		if(bridge == nil)
			return 1;
		if(base >= bridge->mema.bar && limit < (bridge->mema.bar + bridge->mema.size))
			return 1;
		if(base >= bridge->prefa.bar && limit < (bridge->prefa.bar + bridge->prefa.size))
			return 1;
	}
	print("%T: %.2uX invalid %s-window: %.8llux-%.8llux\n", p->tbdf, p->ccrb, typ, base, limit);
	return 0;
}

static int
pcivalidbar(Pcidev *p, uvlong bar, vlong size)
{
	if(bar & 1){
		bar &= ~3;
		if(bar == 0 || size < 4 || (bar & (size-1)) != 0)
			return 0;
		return pcivalidwin(p, bar|1, bar+size-1);
	} else {
		bar &= ~0xFULL;
		if(bar == 0 || size < 16 || (bar & (size-1)) != 0)
			return 0;
		return pcivalidwin(p, bar|0, bar+size-1);
	}
}

int
pciscan(int bno, Pcidev** list, Pcidev *parent)
{
	Pcidev *p, *head, **tail;
	int dno, fno, i, hdt, l, maxfno, maxubn, rno, sbn, tbdf, ubn;

	maxubn = bno;
	head = nil;
	tail = nil;
	for(dno = 0; dno <= pcimaxdno; dno++){
		maxfno = 0;
		for(fno = 0; fno <= maxfno; fno++){
			/*
			 * For this possible device, form the
			 * bus+device+function triplet needed to address it
			 * and try to read the vendor and device ID.
			 * If successful, allocate a device struct and
			 * start to fill it in with some useful information
			 * from the device's configuration space.
			 */
			tbdf = MKBUS(BusPCI, bno, dno, fno);

			lock(&pcicfglock);
			l = pcicfgrw32(tbdf, PciVID, 0, 1);
			unlock(&pcicfglock);

			if(l == 0xFFFFFFFF || l == 0)
				continue;
			p = pcidevalloc();
			p->tbdf = tbdf;
			p->vid = l;
			p->did = l>>16;

			p->pcr = pcicfgr16(p, PciPCR);
			p->rid = pcicfgr8(p, PciRID);
			p->ccrp = pcicfgr8(p, PciCCRp);
			p->ccru = pcicfgr8(p, PciCCRu);
			p->ccrb = pcicfgr8(p, PciCCRb);
			p->cls = pcicfgr8(p, PciCLS);
			p->ltr = pcicfgr8(p, PciLTR);
			p->intl = pcicfgr8(p, PciINTL);

			/*
			 * If the device is a multi-function device adjust the
			 * loop count so all possible functions are checked.
			 */
			hdt = pcicfgr8(p, PciHDT);
			if(hdt & 0x80)
				maxfno = MaxFNO;

			/*
			 * If appropriate, read the base address registers
			 * and work out the sizes.
			 */
			switch(p->ccrb) {
			case 0x00:		/* prehistoric */
			case 0x01:		/* mass storage controller */
			case 0x02:		/* network controller */
			case 0x03:		/* display controller */
			case 0x04:		/* multimedia device */
			case 0x07:		/* simple comm. controllers */
			case 0x08:		/* base system peripherals */
			case 0x09:		/* input devices */
			case 0x0A:		/* docking stations */
			case 0x0B:		/* processors */
			case 0x0C:		/* serial bus controllers */
			case 0x0D:		/* wireless controllers */
			case 0x0E:		/* intelligent I/O controllers */
			case 0x0F:		/* sattelite communication controllers */
			case 0x10:		/* encryption/decryption controllers */
			case 0x11:		/* signal processing controllers */
				if((hdt & 0x7F) != 0)
					break;
				rno = PciBAR0;
				for(i = 0; i < nelem(p->mem); i++) {
					p->mem[i].bar = (ulong)pcicfgr32(p, rno);
					p->mem[i].size = pcibarsize(p, rno);
					if((p->mem[i].bar & 7) == 4 && i < nelem(p->mem)-1){
						rno += 4;
						p->mem[i].bar |= (uvlong)pcicfgr32(p, rno) << 32;
						i++;
						p->mem[i].bar = 0;
						p->mem[i].size = 0;
					}
					rno += 4;
				}

				p->rom.bar = (ulong)pcicfgr32(p, PciEBAR0);
				p->rom.size = pcibarsize(p, PciEBAR0);
				break;

			case 0x06:		/* bridge device */
				/* cardbus bridge? */
				if(p->ccru == 0x07){
					p->mem[0].bar = (ulong)pcicfgr32(p, PciBAR0);
					p->mem[0].size = pcibarsize(p, PciBAR0);
					break;
				}

				/* pci bridge? */
				if(p->ccru != 0x04)
					break;

				p->rom.bar = (ulong)pcicfgr32(p, PciEBAR1);
				p->rom.size = pcibarsize(p, PciEBAR1);
				break;
			case 0x05:		/* memory controller */
			default:
				break;
			}

			p->parent = parent;
			if(head != nil)
				*tail = p;
			else
				head = p;
			tail = &p->link;

			if(pcilist != nil)
				*pcitail = p;
			else
				pcilist = p;
			pcitail = &p->list;
		}
	}

	*list = head;
	for(p = head; p != nil; p = p->link){
		/*
		 * Find PCI-PCI bridges and recursively descend the tree.
		 */
		switch(p->ccrb) {
		case 0x06:
			if(p->ccru == 0x04)
				break;
		default:
			/* check and clear invalid membars for non bridges */
			for(i = 0; i < nelem(p->mem); i++) {
				if(p->mem[i].size == 0)
					continue;
				if(!pcivalidbar(p, p->mem[i].bar, p->mem[i].size)){
					if(p->mem[i].bar & 1)
						p->mem[i].bar &= 3;
					else
						p->mem[i].bar &= 0xF;
					pcisetbar(p, PciBAR0 + i*4, p->mem[i].bar);
				}
			}
			if(p->rom.size) {
				if((p->rom.bar & 1) == 0
				|| !pcivalidbar(p, p->rom.bar & ~0x7FFULL, p->rom.size)){
					p->rom.bar = 0;
					pcisetbar(p, PciEBAR0, p->rom.bar);
				}
			}
			continue;
		}

		if(p->rom.size) {
			if((p->rom.bar & 1) == 0
			|| !pcivalidbar(p, p->rom.bar & ~0x7FFULL, p->rom.size)){
				p->rom.bar = 0;
				pcisetbar(p, PciEBAR1, p->rom.bar);
			}
		}

		/*
		 * If the secondary or subordinate bus number is not
		 * initialised try to do what the PCI BIOS should have
		 * done and fill in the numbers as the tree is descended.
		 * On the way down the subordinate bus number is set to
		 * the maximum as it's not known how many buses are behind
		 * this one; the final value is set on the way back up.
		 */
		sbn = pcicfgr8(p, PciSBN);
		ubn = pcicfgr8(p, PciUBN);

		if(sbn == 0 || ubn == 0) {
			sbn = maxubn+1;
			/*
			 * Make sure memory, I/O and master enables are
			 * off, set the primary, secondary and subordinate
			 * bus numbers and clear the secondary status before
			 * attempting to scan the secondary bus.
			 *
			 * Initialisation of the bridge should be done here.
			 */
			p->pcr = 0;
			pcicfgw32(p, PciPCR, 0xFFFF0000);
			l = (MaxUBN<<16)|(sbn<<8)|bno;
			pcicfgw32(p, PciPBN, l);
			pcicfgw16(p, PciSPSR, 0xFFFF);

			p->ioa.bar = 0;
			p->ioa.size = 0;
			p->mema.bar = 0;
			p->mema.size = 0;
			p->prefa.bar = 0;
			p->prefa.size = 0;

			pcisetwin(p, 0xFFFFF000|1, 0);
			pcisetwin(p, 0xFFF00000|0, 0);
			pcisetwin(p, 0xFFF00000|8, 0);

			maxubn = pciscan(sbn, &p->bridge, p);
			l = (maxubn<<16)|(sbn<<8)|bno;

			pcicfgw32(p, PciPBN, l);
		}
		else {
			uvlong base, limit;
			ulong v;

			v = pcicfgr16(p, PciIBR);
			limit = (v & 0xF000) | 0x0FFF;
			base  = (v & 0x00F0) << 8;
			if((v & 0x0F) == 0x01){
				v = pcicfgr32(p, PciIUBR);
				limit |= (v & 0xFFFF0000);
				base  |= (v & 0x0000FFFF) << 16;
			}
			if(pcivalidwin(p, base|1, limit)){
				p->ioa.bar = base;
				p->ioa.size = (limit - base)+1;
			} else {
				pcisetwin(p, 0xFFFFF000|1, 0);
				p->ioa.bar = 0;
				p->ioa.size = 0;
			}

			v = pcicfgr32(p, PciMBR);
			limit = (v & 0xFFF00000) | 0x000FFFFF;
			base  = (v & 0x0000FFF0) << 16;
			if(pcivalidwin(p, base|0, limit)){
				p->mema.bar = base;
				p->mema.size = (limit - base)+1;
			} else {
				pcisetwin(p, 0xFFF00000|0, 0);
				p->mema.bar = 0;
				p->mema.size = 0;
			}

			v = pcicfgr32(p, PciPMBR);
			limit = (v & 0xFFF00000) | 0x000FFFFF;
			limit |= (uvlong)pcicfgr32(p, PciPULR) << 32;
			base  = (v & 0x0000FFF0) << 16;
			base  |= (uvlong)pcicfgr32(p, PciPUBR) << 32;
			if(pcivalidwin(p, base|8, limit)){
				p->prefa.bar = base;
				p->prefa.size = (limit - base)+1;
			} else {
				pcisetwin(p, 0xFFF00000|8, 0);
				p->prefa.bar = 0;
				p->prefa.size = 0;
			}

			if(ubn > maxubn)
				maxubn = ubn;
			pciscan(sbn, &p->bridge, p);
		}
	}

	return maxubn;
}

void
pcibussize(Pcidev *root, uvlong *msize, ulong *iosize)
{
	*msize = 0;
	*iosize = 0;
	pcibusmap(root, msize, iosize, 0);
}

Pcidev*
pcimatch(Pcidev* prev, int vid, int did)
{
	if(prev == nil)
		prev = pcilist;
	else
		prev = prev->list;

	while(prev != nil){
		if((vid == 0 || prev->vid == vid)
		&& (did == 0 || prev->did == did))
			break;
		prev = prev->list;
	}
	return prev;
}

Pcidev*
pcimatchtbdf(int tbdf)
{
	Pcidev *pcidev;

	for(pcidev = pcilist; pcidev != nil; pcidev = pcidev->list) {
		if(pcidev->tbdf == tbdf)
			break;
	}
	return pcidev;
}

uchar
pciipin(Pcidev *pci, uchar pin)
{
	if (pci == nil)
		pci = pcilist;

	while (pci != nil) {
		uchar intl;

		if (pcicfgr8(pci, PciINTP) == pin && pci->intl != 0 && pci->intl != 0xff)
			return pci->intl;

		if (pci->bridge && (intl = pciipin(pci->bridge, pin)) != 0)
			return intl;

		pci = pci->list;
	}
	return 0;
}

static void
pcilhinv(Pcidev* p)
{
	int i;
	Pcidev *t;

	for(t = p; t != nil; t = t->link) {
		print("%d  %2d/%d %.2ux %.2ux %.2ux %.4ux %.4ux %3d  ",
			BUSBNO(t->tbdf), BUSDNO(t->tbdf), BUSFNO(t->tbdf),
			t->ccrb, t->ccru, t->ccrp, t->vid, t->did, t->intl);
		for(i = 0; i < nelem(p->mem); i++) {
			if(t->mem[i].size == 0)
				continue;
			print("%d:%.8llux %lld ", i, t->mem[i].bar, t->mem[i].size);
		}
		if(t->rom.bar || t->rom.size)
			print("rom:%.8llux %lld ", t->rom.bar, t->rom.size);
		if(t->ioa.bar || t->ioa.size)
			print("ioa:%.8llux-%.8llux %lld ", t->ioa.bar, t->ioa.bar+t->ioa.size, t->ioa.size);
		if(t->mema.bar || t->mema.size)
			print("mema:%.8llux-%.8llux %lld ", t->mema.bar, t->mema.bar+t->mema.size, t->mema.size);
		if(t->prefa.bar || t->prefa.size)
			print("prefa:%.8llux-%.8llux %lld ", t->prefa.bar, t->prefa.bar+t->prefa.size, t->prefa.size);
		if(t->bridge)
			print("->%d", BUSBNO(t->bridge->tbdf));
		print("\n");
	}
	while(p != nil) {
		if(p->bridge != nil)
			pcilhinv(p->bridge);
		p = p->link;
	}
}

void
pcihinv(Pcidev* p)
{
	print("bus dev type     vid  did  intl memory\n");
	pcilhinv(p);
}

void
pcireset(void)
{
	Pcidev *p;

	for(p = pcilist; p != nil; p = p->list) {
		/* don't mess with the bridges */
		if(p->ccrb == 0x06)
			continue;
		pcidisable(p);
	}
}

void
pcisetioe(Pcidev* p)
{
	p->pcr |= IOen;
	pcicfgw16(p, PciPCR, p->pcr);
}

void
pciclrioe(Pcidev* p)
{
	p->pcr &= ~IOen;
	pcicfgw16(p, PciPCR, p->pcr);
}

void
pcisetbme(Pcidev* p)
{
	p->pcr |= MASen;
	pcicfgw16(p, PciPCR, p->pcr);
}

void
pciclrbme(Pcidev* p)
{
	p->pcr &= ~MASen;
	pcicfgw16(p, PciPCR, p->pcr);
}

void
pcisetmwi(Pcidev* p)
{
	p->pcr |= MemWrInv;
	pcicfgw16(p, PciPCR, p->pcr);
}

void
pciclrmwi(Pcidev* p)
{
	p->pcr &= ~MemWrInv;
	pcicfgw16(p, PciPCR, p->pcr);
}

int
pcienumcaps(Pcidev *p, int (*fmatch)(Pcidev*, int, int, int), int arg)
{
	int i, r, cap, off;

	/* status register bit 4 has capabilities */
	if((pcicfgr16(p, PciPSR) & 1<<4) == 0)
		return -1;      
	switch(pcicfgr8(p, PciHDT) & 0x7F){
	default:
		return -1;
	case 0:                         /* etc */
	case 1:                         /* pci to pci bridge */
		off = 0x34;
		break;
	case 2:                         /* cardbus bridge */
		off = 0x14;
		break;
	}
	for(i = 48; i--;){
		off = pcicfgr8(p, off);
		if(off < 0x40 || (off & 3))
			break;
		off &= ~3;
		cap = pcicfgr8(p, off);
		if(cap == 0xff)
			break;
		r = (*fmatch)(p, cap, off, arg);
		if(r < 0)
			break;
		if(r == 0)
			return off;
		off++;
	}
	return -1;
}

static int
matchcap(Pcidev *, int cap, int, int arg)
{
	return cap != arg;
}

static int
matchhtcap(Pcidev *p, int cap, int off, int arg)
{
	int mask;

	if(cap != PciCapHTC)
		return 1;
	if(arg == 0x00 || arg == 0x20)
		mask = 0xE0;
	else
		mask = 0xF8;
	cap = pcicfgr8(p, off+3);
	return (cap & mask) != arg;
}

int
pcicap(Pcidev *p, int cap)
{
	return pcienumcaps(p, matchcap, cap);
}

int
pcihtcap(Pcidev *p, int cap)
{
	return pcienumcaps(p, matchhtcap, cap);
}

static int
pcigetmsi(Pcidev *p)
{
	if(p->msi != 0)
		return p->msi;
	return p->msi = pcicap(p, PciCapMSI);
}

enum {
	MSICtrl = 0x02, /* message control register (16 bit) */
	MSIAddr = 0x04, /* message address register (64 bit) */
	MSIData32 = 0x08, /* message data register for 32 bit MSI (16 bit) */
	MSIData64 = 0x0C, /* message data register for 64 bit MSI (16 bit) */
};

int
pcimsienable(Pcidev *p, uvlong addr, ulong data)
{
	int off, ok64;

	if((off = pcigetmsi(p)) < 0)
		return -1;
	ok64 = (pcicfgr16(p, off + MSICtrl) & (1<<7)) != 0;
	pcicfgw32(p, off + MSIAddr, addr);
	if(ok64) pcicfgw32(p, off + MSIAddr+4, addr >> 32);
	pcicfgw16(p, off + (ok64 ? MSIData64 : MSIData32), data);
	pcicfgw16(p, off + MSICtrl, 1);
	return 0;
}

int
pcimsidisable(Pcidev *p)
{
	int off;

	if((off = pcigetmsi(p)) < 0)
		return -1;
	pcicfgw16(p, off + MSICtrl, 0);
	return 0;
}

enum {
	MSIXCtrl = 0x02,
};

static int
pcimsixdisable(Pcidev *p)
{
	int off;

	if((off = pcicap(p, PciCapMSIX)) < 0)
		return -1;
	pcicfgw16(p, off + MSIXCtrl, 0);
	return 0;
}

static int
pcigetpmrb(Pcidev *p)
{
        if(p->pmrb != 0)
                return p->pmrb;
        return p->pmrb = pcicap(p, PciCapPMG);
}

int
pcigetpms(Pcidev* p)
{
	int pmcsr, ptr;

	if((ptr = pcigetpmrb(p)) == -1)
		return -1;

	/*
	 * Power Management Register Block:
	 *  offset 0:	Capability ID
	 *	   1:	next item pointer
	 *	   2:	capabilities
	 *	   4:	control/status
	 *	   6:	bridge support extensions
	 *	   7:	data
	 */
	pmcsr = pcicfgr16(p, ptr+4);

	return pmcsr & 0x0003;
}

int
pcisetpms(Pcidev* p, int state)
{
	int ostate, pmc, pmcsr, ptr;

	if((ptr = pcigetpmrb(p)) == -1)
		return -1;

	pmc = pcicfgr16(p, ptr+2);
	pmcsr = pcicfgr16(p, ptr+4);
	ostate = pmcsr & 0x0003;
	pmcsr &= ~0x0003;

	switch(state){
	default:
		return -1;
	case 0:
		break;
	case 1:
		if(!(pmc & 0x0200))
			return -1;
		break;
	case 2:
		if(!(pmc & 0x0400))
			return -1;
		break;
	case 3:
		break;
	}
	pmcsr |= state;
	pcicfgw16(p, ptr+4, pmcsr);

	return ostate;
}

void
pcienable(Pcidev *p)
{
	uint pcr;
	int i;

	if(p == nil)
		return;

	pcienable(p->parent);

	switch(pcisetpms(p, 0)){
	case 1:
		print("pcienable %T: wakeup from D1\n", p->tbdf);
		break;
	case 2:
		print("pcienable %T: wakeup from D2\n", p->tbdf);
		if(p->bridge != nil)
			delay(100);	/* B2: minimum delay 50ms */
		else
			delay(1);	/* D2: minimum delay 200µs */
		break;
	case 3:
		print("pcienable %T: wakeup from D3\n", p->tbdf);
		delay(100);		/* D3: minimum delay 50ms */

		/* restore registers */
		for(i = 0; i < nelem(p->mem); i++){
			if(p->mem[i].size == 0)
				continue;
			pcisetbar(p, PciBAR0+i*4, p->mem[i].bar);
		}

		pcicfgw8(p, PciINTL, p->intl);
		pcicfgw8(p, PciLTR, p->ltr);
		pcicfgw8(p, PciCLS, p->cls);
		pcicfgw16(p, PciPCR, p->pcr);
		break;
	}

	if(p->ltr == 0 || p->ltr == 0xFF){
		p->ltr = 64;
		pcicfgw8(p,PciLTR, p->ltr);
	}
	if(p->cls == 0 || p->cls == 0xFF){
		p->cls = 64/4;
		pcicfgw8(p, PciCLS, p->cls);
	}

	if(p->bridge != nil)
		pcr = IOen|MEMen|MASen;
	else {
		pcr = 0;
		for(i = 0; i < nelem(p->mem); i++){
			if(p->mem[i].size == 0)
				continue;
			if(p->mem[i].bar & 1)
				pcr |= IOen;
			else
				pcr |= MEMen;
		}
	}

	if((p->pcr & pcr) != pcr){
		print("pcienable %T: pcr %ux->%ux\n", p->tbdf, p->pcr, p->pcr|pcr);
		p->pcr |= pcr;
		pcicfgw32(p, PciPCR, 0xFFFF0000|p->pcr);
	}
}

void
pcidisable(Pcidev *p)
{
	if(p == nil)
		return;
	pcimsixdisable(p);
	pcimsidisable(p);
	pciclrbme(p);
}