]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc/pci.c
pc, pc64: new memory map code
[plan9front.git] / sys / src / 9 / pc / pci.c
1 /*
2  * PCI support code.
3  * Needs a massive rewrite.
4  */
5 #include "u.h"
6 #include "../port/lib.h"
7 #include "mem.h"
8 #include "dat.h"
9 #include "fns.h"
10 #include "io.h"
11 #include "../port/error.h"
12
13 #define DBG     if(0) print
14
15 enum
16 {                                       /* configuration mechanism #1 */
17         PciADDR         = 0xCF8,        /* CONFIG_ADDRESS */
18         PciDATA         = 0xCFC,        /* CONFIG_DATA */
19
20                                         /* configuration mechanism #2 */
21         PciCSE          = 0xCF8,        /* configuration space enable */
22         PciFORWARD      = 0xCFA,        /* which bus */
23
24         MaxFNO          = 7,
25         MaxUBN          = 255,
26 };
27
28 enum
29 {                                       /* command register */
30         IOen            = (1<<0),
31         MEMen           = (1<<1),
32         MASen           = (1<<2),
33         MemWrInv        = (1<<4),
34         PErrEn          = (1<<6),
35         SErrEn          = (1<<8),
36 };
37
38 static Lock pcicfglock;
39 static Lock pcicfginitlock;
40 static int pcicfgmode = -1;
41 static int pcimaxbno = 255;
42 static int pcimaxdno;
43 static Pcidev* pciroot;
44 static Pcidev* pcilist;
45 static Pcidev* pcitail;
46 static int nobios, nopcirouting;
47 static BIOS32si* pcibiossi;
48
49 static int pcicfgrw8raw(int, int, int, int);
50 static int pcicfgrw16raw(int, int, int, int);
51 static int pcicfgrw32raw(int, int, int, int);
52
53 static int (*pcicfgrw8)(int, int, int, int) = pcicfgrw8raw;
54 static int (*pcicfgrw16)(int, int, int, int) = pcicfgrw16raw;
55 static int (*pcicfgrw32)(int, int, int, int) = pcicfgrw32raw;
56
57 static char* bustypes[] = {
58         "CBUSI",
59         "CBUSII",
60         "EISA",
61         "FUTURE",
62         "INTERN",
63         "ISA",
64         "MBI",
65         "MBII",
66         "MCA",
67         "MPI",
68         "MPSA",
69         "NUBUS",
70         "PCI",
71         "PCMCIA",
72         "TC",
73         "VL",
74         "VME",
75         "XPRESS",
76 };
77
78 static int
79 tbdffmt(Fmt* fmt)
80 {
81         char *p;
82         int l, r;
83         uint type, tbdf;
84
85         if((p = malloc(READSTR)) == nil)
86                 return fmtstrcpy(fmt, "(tbdfconv)");
87
88         switch(fmt->r){
89         case 'T':
90                 tbdf = va_arg(fmt->args, int);
91                 if(tbdf == BUSUNKNOWN)
92                         snprint(p, READSTR, "unknown");
93                 else{
94                         type = BUSTYPE(tbdf);
95                         if(type < nelem(bustypes))
96                                 l = snprint(p, READSTR, bustypes[type]);
97                         else
98                                 l = snprint(p, READSTR, "%d", type);
99                         snprint(p+l, READSTR-l, ".%d.%d.%d",
100                                 BUSBNO(tbdf), BUSDNO(tbdf), BUSFNO(tbdf));
101                 }
102                 break;
103
104         default:
105                 snprint(p, READSTR, "(tbdfconv)");
106                 break;
107         }
108         r = fmtstrcpy(fmt, p);
109         free(p);
110
111         return r;
112 }
113
114 ulong
115 pcibarsize(Pcidev *p, int rno)
116 {
117         ulong v, size;
118
119         v = pcicfgrw32(p->tbdf, rno, 0, 1);
120         pcicfgrw32(p->tbdf, rno, 0xFFFFFFF0, 0);
121         size = pcicfgrw32(p->tbdf, rno, 0, 1);
122         if(v & 1)
123                 size |= 0xFFFF0000;
124         pcicfgrw32(p->tbdf, rno, v, 0);
125
126         return -(size & ~0x0F);
127 }
128
129 static int
130 pcisizcmp(void *a, void *b)
131 {
132         Pcisiz *aa, *bb;
133
134         aa = a;
135         bb = b;
136         return aa->siz - bb->siz;
137 }
138
139 static ulong
140 pcimask(ulong v)
141 {
142         ulong m;
143
144         m = BI2BY*sizeof(v);
145         for(m = 1<<(m-1); m != 0; m >>= 1) {
146                 if(m & v)
147                         break;
148         }
149
150         m--;
151         if((v & m) == 0)
152                 return v;
153
154         v |= m;
155         return v+1;
156 }
157
158 static void
159 pcibusmap(Pcidev *root, ulong *pmema, ulong *pioa, int wrreg)
160 {
161         Pcidev *p;
162         int ntb, i, size, rno, hole;
163         ulong v, mema, ioa, sioa, smema, base, limit;
164         Pcisiz *table, *tptr, *mtb, *itb;
165
166         if(!nobios)
167                 return;
168
169         ioa = *pioa;
170         mema = *pmema;
171
172         DBG("pcibusmap wr=%d %T mem=%luX io=%luX\n",
173                 wrreg, root->tbdf, mema, ioa);
174
175         ntb = 0;
176         for(p = root; p != nil; p = p->link)
177                 ntb++;
178
179         ntb *= (PciCIS-PciBAR0)/4;
180         table = malloc(2*ntb*sizeof(Pcisiz));
181         if(table == nil)
182                 panic("pcibusmap: can't allocate memory");
183         itb = table;
184         mtb = table+ntb;
185
186         /*
187          * Build a table of sizes
188          */
189         for(p = root; p != nil; p = p->link) {
190                 if(p->ccrb == 0x06) {
191                         if(p->ccru != 0x04 || p->bridge == nil) {
192                                 DBG("pci: ignored bridge %T\n", p->tbdf);
193                                 continue;
194                         }
195
196                         sioa = ioa;
197                         smema = mema;
198                         pcibusmap(p->bridge, &smema, &sioa, 0);
199
200                         hole = pcimask(smema-mema);
201                         if(hole < (1<<20))
202                                 hole = 1<<20;
203                         p->mema.size = hole;
204
205                         hole = pcimask(sioa-ioa);
206                         if(hole < (1<<12))
207                                 hole = 1<<12;
208
209                         p->ioa.size = hole;
210
211                         itb->dev = p;
212                         itb->bar = -1;
213                         itb->siz = p->ioa.size;
214                         itb++;
215
216                         mtb->dev = p;
217                         mtb->bar = -1;
218                         mtb->siz = p->mema.size;
219                         mtb++;
220                         continue;
221                 }
222
223                 for(i = 0; i <= 5; i++) {
224                         rno = PciBAR0 + i*4;
225                         v = pcicfgrw32(p->tbdf, rno, 0, 1);
226                         size = pcibarsize(p, rno);
227                         if(size == 0)
228                                 continue;
229
230                         p->mem[i].size = size;
231                         if(v & 1) {
232                                 itb->dev = p;
233                                 itb->bar = i;
234                                 itb->siz = size;
235                                 itb++;
236                         }
237                         else {
238                                 mtb->dev = p;
239                                 mtb->bar = i;
240                                 mtb->siz = size;
241                                 mtb++;
242
243                                 if((v & 7) == 4)
244                                         i++;
245                         }
246                 }
247         }
248
249         /*
250          * Sort both tables IO smallest first, Memory largest
251          */
252         qsort(table, itb-table, sizeof(Pcisiz), pcisizcmp);
253         tptr = table+ntb;
254         qsort(tptr, mtb-tptr, sizeof(Pcisiz), pcisizcmp);
255
256         /*
257          * Allocate IO address space on this bus
258          */
259         for(tptr = table; tptr < itb; tptr++) {
260                 hole = tptr->siz;
261                 if(tptr->bar == -1)
262                         hole = 1<<12;
263                 ioa = (ioa+hole-1) & ~(hole-1);
264
265                 p = tptr->dev;
266                 if(tptr->bar == -1)
267                         p->ioa.bar = ioa;
268                 else {
269                         p->pcr |= IOen;
270                         p->mem[tptr->bar].bar = ioa|1;
271                         if(wrreg)
272                                 pcicfgrw32(p->tbdf, PciBAR0+(tptr->bar*4), ioa|1, 0);
273                 }
274
275                 ioa += tptr->siz;
276         }
277
278         /*
279          * Allocate Memory address space on this bus
280          */
281         for(tptr = table+ntb; tptr < mtb; tptr++) {
282                 hole = tptr->siz;
283                 if(tptr->bar == -1)
284                         hole = 1<<20;
285                 mema = (mema+hole-1) & ~(hole-1);
286
287                 p = tptr->dev;
288                 if(tptr->bar == -1)
289                         p->mema.bar = mema;
290                 else {
291                         p->pcr |= MEMen;
292                         p->mem[tptr->bar].bar = mema;
293                         if(wrreg)
294                                 pcicfgrw32(p->tbdf, PciBAR0+(tptr->bar*4), mema, 0);
295                 }
296                 mema += tptr->siz;
297         }
298
299         *pmema = mema;
300         *pioa = ioa;
301         free(table);
302
303         if(wrreg == 0)
304                 return;
305
306         /*
307          * Finally set all the bridge addresses & registers
308          */
309         for(p = root; p != nil; p = p->link) {
310                 if(p->bridge == nil) {
311                         pcicfgrw8(p->tbdf, PciLTR, 64, 0);
312
313                         p->pcr |= MASen;
314                         pcicfgrw16(p->tbdf, PciPCR, p->pcr, 0);
315                         continue;
316                 }
317
318                 base = p->ioa.bar;
319                 limit = base+p->ioa.size-1;
320                 v = pcicfgrw32(p->tbdf, PciIBR, 0, 1);
321                 v = (v&0xFFFF0000)|(limit & 0xF000)|((base & 0xF000)>>8);
322                 pcicfgrw32(p->tbdf, PciIBR, v, 0);
323                 v = (limit & 0xFFFF0000)|(base>>16);
324                 pcicfgrw32(p->tbdf, PciIUBR, v, 0);
325
326                 base = p->mema.bar;
327                 limit = base+p->mema.size-1;
328                 v = (limit & 0xFFF00000)|((base & 0xFFF00000)>>16);
329                 pcicfgrw32(p->tbdf, PciMBR, v, 0);
330
331                 /*
332                  * Disable memory prefetch
333                  */
334                 pcicfgrw32(p->tbdf, PciPMBR, 0x0000FFFF, 0);
335                 pcicfgrw8(p->tbdf, PciLTR, 64, 0);
336
337                 /*
338                  * Enable the bridge
339                  */
340                 p->pcr |= IOen|MEMen|MASen;
341                 pcicfgrw32(p->tbdf, PciPCR, 0xFFFF0000|p->pcr , 0);
342
343                 sioa = p->ioa.bar;
344                 smema = p->mema.bar;
345                 pcibusmap(p->bridge, &smema, &sioa, 1);
346         }
347 }
348
349 static int
350 pcilscan(int bno, Pcidev** list, Pcidev *parent)
351 {
352         Pcidev *p, *head, *tail;
353         int dno, fno, i, hdt, l, maxfno, maxubn, rno, sbn, tbdf, ubn;
354
355         maxubn = bno;
356         head = nil;
357         tail = nil;
358         for(dno = 0; dno <= pcimaxdno; dno++){
359                 maxfno = 0;
360                 for(fno = 0; fno <= maxfno; fno++){
361                         /*
362                          * For this possible device, form the
363                          * bus+device+function triplet needed to address it
364                          * and try to read the vendor and device ID.
365                          * If successful, allocate a device struct and
366                          * start to fill it in with some useful information
367                          * from the device's configuration space.
368                          */
369                         tbdf = MKBUS(BusPCI, bno, dno, fno);
370                         l = pcicfgrw32(tbdf, PciVID, 0, 1);
371                         if(l == 0xFFFFFFFF || l == 0)
372                                 continue;
373                         p = malloc(sizeof(*p));
374                         if(p == nil)
375                                 panic("pcilscan: can't allocate memory");
376                         p->tbdf = tbdf;
377                         p->vid = l;
378                         p->did = l>>16;
379
380                         if(pcilist != nil)
381                                 pcitail->list = p;
382                         else
383                                 pcilist = p;
384                         pcitail = p;
385
386                         p->pcr = pcicfgr16(p, PciPCR);
387                         p->rid = pcicfgr8(p, PciRID);
388                         p->ccrp = pcicfgr8(p, PciCCRp);
389                         p->ccru = pcicfgr8(p, PciCCRu);
390                         p->ccrb = pcicfgr8(p, PciCCRb);
391                         p->cls = pcicfgr8(p, PciCLS);
392                         p->ltr = pcicfgr8(p, PciLTR);
393
394                         p->intl = pcicfgr8(p, PciINTL);
395
396                         /*
397                          * If the device is a multi-function device adjust the
398                          * loop count so all possible functions are checked.
399                          */
400                         hdt = pcicfgr8(p, PciHDT);
401                         if(hdt & 0x80)
402                                 maxfno = MaxFNO;
403
404                         /*
405                          * If appropriate, read the base address registers
406                          * and work out the sizes.
407                          */
408                         switch(p->ccrb) {
409                         case 0x00:              /* prehistoric */
410                         case 0x01:              /* mass storage controller */
411                         case 0x02:              /* network controller */
412                         case 0x03:              /* display controller */
413                         case 0x04:              /* multimedia device */
414                         case 0x07:              /* simple comm. controllers */
415                         case 0x08:              /* base system peripherals */
416                         case 0x09:              /* input devices */
417                         case 0x0A:              /* docking stations */
418                         case 0x0B:              /* processors */
419                         case 0x0C:              /* serial bus controllers */
420                         case 0x0D:              /* wireless controllers */
421                         case 0x0E:              /* intelligent I/O controllers */
422                         case 0x0F:              /* sattelite communication controllers */
423                         case 0x10:              /* encryption/decryption controllers */
424                         case 0x11:              /* signal processing controllers */
425                                 if((hdt & 0x7F) != 0)
426                                         break;
427                                 rno = PciBAR0;
428                                 for(i = 0; i <= 5; i++) {
429                                         p->mem[i].bar = pcicfgr32(p, rno);
430                                         p->mem[i].size = pcibarsize(p, rno);
431                                         if((p->mem[i].bar & 7) == 4 && i < 5){
432                                                 ulong hi;
433
434                                                 rno += 4;
435                                                 hi = pcicfgr32(p, rno);
436                                                 if(hi != 0){
437                                                         print("ignoring 64-bit bar %d: %llux %d from %T\n",
438                                                                 i, (uvlong)hi<<32 | p->mem[i].bar, p->mem[i].size, p->tbdf);
439                                                         p->mem[i].bar = 0;
440                                                         p->mem[i].size = 0;
441                                                 }
442                                                 i++;
443                                         }
444                                         rno += 4;
445                                 }
446                                 break;
447
448                         case 0x05:              /* memory controller */
449                         case 0x06:              /* bridge device */
450                         default:
451                                 break;
452                         }
453
454                         p->parent = parent;
455                         if(head != nil)
456                                 tail->link = p;
457                         else
458                                 head = p;
459                         tail = p;
460                 }
461         }
462
463         *list = head;
464         for(p = head; p != nil; p = p->link){
465                 /*
466                  * Find PCI-PCI bridges and recursively descend the tree.
467                  */
468                 if(p->ccrb != 0x06 || p->ccru != 0x04)
469                         continue;
470
471                 /*
472                  * If the secondary or subordinate bus number is not
473                  * initialised try to do what the PCI BIOS should have
474                  * done and fill in the numbers as the tree is descended.
475                  * On the way down the subordinate bus number is set to
476                  * the maximum as it's not known how many buses are behind
477                  * this one; the final value is set on the way back up.
478                  */
479                 sbn = pcicfgr8(p, PciSBN);
480                 ubn = pcicfgr8(p, PciUBN);
481
482                 if(sbn == 0 || ubn == 0 || nobios) {
483                         sbn = maxubn+1;
484                         /*
485                          * Make sure memory, I/O and master enables are
486                          * off, set the primary, secondary and subordinate
487                          * bus numbers and clear the secondary status before
488                          * attempting to scan the secondary bus.
489                          *
490                          * Initialisation of the bridge should be done here.
491                          */
492                         pcicfgw32(p, PciPCR, 0xFFFF0000);
493                         l = (MaxUBN<<16)|(sbn<<8)|bno;
494                         pcicfgw32(p, PciPBN, l);
495                         pcicfgw16(p, PciSPSR, 0xFFFF);
496                         maxubn = pcilscan(sbn, &p->bridge, p);
497                         l = (maxubn<<16)|(sbn<<8)|bno;
498
499                         pcicfgw32(p, PciPBN, l);
500                 }
501                 else {
502                         if(ubn > maxubn)
503                                 maxubn = ubn;
504                         pcilscan(sbn, &p->bridge, p);
505                 }
506         }
507
508         return maxubn;
509 }
510
511 int
512 pciscan(int bno, Pcidev **list)
513 {
514         int ubn;
515
516         lock(&pcicfginitlock);
517         ubn = pcilscan(bno, list, nil);
518         unlock(&pcicfginitlock);
519         return ubn;
520 }
521
522 static uchar
523 pIIxget(Pcidev *router, uchar link)
524 {
525         uchar pirq;
526
527         /* link should be 0x60, 0x61, 0x62, 0x63 */
528         pirq = pcicfgr8(router, link);
529         return (pirq < 16)? pirq: 0;
530 }
531
532 static void
533 pIIxset(Pcidev *router, uchar link, uchar irq)
534 {
535         pcicfgw8(router, link, irq);
536 }
537
538 static uchar
539 viaget(Pcidev *router, uchar link)
540 {
541         uchar pirq;
542
543         /* link should be 1, 2, 3, 5 */
544         pirq = (link < 6)? pcicfgr8(router, 0x55 + (link>>1)): 0;
545
546         return (link & 1)? (pirq >> 4): (pirq & 15);
547 }
548
549 static void
550 viaset(Pcidev *router, uchar link, uchar irq)
551 {
552         uchar pirq;
553
554         pirq = pcicfgr8(router, 0x55 + (link >> 1));
555         pirq &= (link & 1)? 0x0f: 0xf0;
556         pirq |= (link & 1)? (irq << 4): (irq & 15);
557         pcicfgw8(router, 0x55 + (link>>1), pirq);
558 }
559
560 static uchar
561 optiget(Pcidev *router, uchar link)
562 {
563         uchar pirq = 0;
564
565         /* link should be 0x02, 0x12, 0x22, 0x32 */
566         if ((link & 0xcf) == 0x02)
567                 pirq = pcicfgr8(router, 0xb8 + (link >> 5));
568         return (link & 0x10)? (pirq >> 4): (pirq & 15);
569 }
570
571 static void
572 optiset(Pcidev *router, uchar link, uchar irq)
573 {
574         uchar pirq;
575
576         pirq = pcicfgr8(router, 0xb8 + (link >> 5));
577         pirq &= (link & 0x10)? 0x0f : 0xf0;
578         pirq |= (link & 0x10)? (irq << 4): (irq & 15);
579         pcicfgw8(router, 0xb8 + (link >> 5), pirq);
580 }
581
582 static uchar
583 aliget(Pcidev *router, uchar link)
584 {
585         /* No, you're not dreaming */
586         static const uchar map[] = { 0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15 };
587         uchar pirq;
588
589         /* link should be 0x01..0x08 */
590         pirq = pcicfgr8(router, 0x48 + ((link-1)>>1));
591         return (link & 1)? map[pirq&15]: map[pirq>>4];
592 }
593
594 static void
595 aliset(Pcidev *router, uchar link, uchar irq)
596 {
597         /* Inverse of map in aliget */
598         static const uchar map[] = { 0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15 };
599         uchar pirq;
600
601         pirq = pcicfgr8(router, 0x48 + ((link-1)>>1));
602         pirq &= (link & 1)? 0x0f: 0xf0;
603         pirq |= (link & 1)? (map[irq] << 4): (map[irq] & 15);
604         pcicfgw8(router, 0x48 + ((link-1)>>1), pirq);
605 }
606
607 static uchar
608 cyrixget(Pcidev *router, uchar link)
609 {
610         uchar pirq;
611
612         /* link should be 1, 2, 3, 4 */
613         pirq = pcicfgr8(router, 0x5c + ((link-1)>>1));
614         return ((link & 1)? pirq >> 4: pirq & 15);
615 }
616
617 static void
618 cyrixset(Pcidev *router, uchar link, uchar irq)
619 {
620         uchar pirq;
621
622         pirq = pcicfgr8(router, 0x5c + (link>>1));
623         pirq &= (link & 1)? 0x0f: 0xf0;
624         pirq |= (link & 1)? (irq << 4): (irq & 15);
625         pcicfgw8(router, 0x5c + (link>>1), pirq);
626 }
627
628 typedef struct Bridge Bridge;
629 struct Bridge
630 {
631         ushort  vid;
632         ushort  did;
633         uchar   (*get)(Pcidev *, uchar);
634         void    (*set)(Pcidev *, uchar, uchar);
635 };
636
637 static Bridge southbridges[] = {
638         { 0x8086, 0x122e, pIIxget, pIIxset },   /* Intel 82371FB */
639         { 0x8086, 0x1234, pIIxget, pIIxset },   /* Intel 82371MX */
640         { 0x8086, 0x7000, pIIxget, pIIxset },   /* Intel 82371SB */
641         { 0x8086, 0x7110, pIIxget, pIIxset },   /* Intel 82371AB */
642         { 0x8086, 0x7198, pIIxget, pIIxset },   /* Intel 82443MX (fn 1) */
643         { 0x8086, 0x2410, pIIxget, pIIxset },   /* Intel 82801AA */
644         { 0x8086, 0x2420, pIIxget, pIIxset },   /* Intel 82801AB */
645         { 0x8086, 0x2440, pIIxget, pIIxset },   /* Intel 82801BA */
646         { 0x8086, 0x2448, pIIxget, pIIxset },   /* Intel 82801BAM/CAM/DBM */
647         { 0x8086, 0x244c, pIIxget, pIIxset },   /* Intel 82801BAM */
648         { 0x8086, 0x244e, pIIxget, pIIxset },   /* Intel 82801 */
649         { 0x8086, 0x2480, pIIxget, pIIxset },   /* Intel 82801CA */
650         { 0x8086, 0x248c, pIIxget, pIIxset },   /* Intel 82801CAM */
651         { 0x8086, 0x24c0, pIIxget, pIIxset },   /* Intel 82801DBL */
652         { 0x8086, 0x24cc, pIIxget, pIIxset },   /* Intel 82801DBM */
653         { 0x8086, 0x24d0, pIIxget, pIIxset },   /* Intel 82801EB */
654         { 0x8086, 0x25a1, pIIxget, pIIxset },   /* Intel 6300ESB */
655         { 0x8086, 0x2640, pIIxget, pIIxset },   /* Intel 82801FB */
656         { 0x8086, 0x2641, pIIxget, pIIxset },   /* Intel 82801FBM */
657         { 0x8086, 0x2670, pIIxget, pIIxset },   /* Intel 632xesb */
658         { 0x8086, 0x27b8, pIIxget, pIIxset },   /* Intel 82801GB */
659         { 0x8086, 0x27b9, pIIxget, pIIxset },   /* Intel 82801GBM */
660         { 0x8086, 0x27bd, pIIxget, pIIxset },   /* Intel 82801GB/GR */
661         { 0x8086, 0x3a16, pIIxget, pIIxset },   /* Intel 82801JIR */
662         { 0x8086, 0x3a40, pIIxget, pIIxset },   /* Intel 82801JI */
663         { 0x8086, 0x3a42, pIIxget, pIIxset },   /* Intel 82801JI */
664         { 0x8086, 0x3a48, pIIxget, pIIxset },   /* Intel 82801JI */
665         { 0x8086, 0x2916, pIIxget, pIIxset },   /* Intel 82801? */
666         { 0x8086, 0x1c02, pIIxget, pIIxset },   /* Intel 6 Series/C200 */
667         { 0x8086, 0x1e53, pIIxget, pIIxset },   /* Intel 7 Series/C216 */
668         { 0x8086, 0x8c56, pIIxget, pIIxset },   /* Intel 8 Series/C226 */
669         { 0x8086, 0x2810, pIIxget, pIIxset },   /* Intel 82801HB/HR (ich8/r) */
670         { 0x8086, 0x2812, pIIxget, pIIxset },   /* Intel 82801HH (ich8dh) */
671         { 0x8086, 0x2912, pIIxget, pIIxset },   /* Intel 82801ih ich9dh */
672         { 0x8086, 0x2914, pIIxget, pIIxset },   /* Intel 82801io ich9do */
673         { 0x8086, 0x2916, pIIxget, pIIxset },   /* Intel 82801ibr ich9r */
674         { 0x8086, 0x2917, pIIxget, pIIxset },   /* Intel 82801iem ich9m-e  */
675         { 0x8086, 0x2918, pIIxget, pIIxset },   /* Intel 82801ib ich9 */
676         { 0x8086, 0x2919, pIIxget, pIIxset },   /* Intel 82801? ich9m  */
677         { 0x8086, 0x3a16, pIIxget, pIIxset },   /* Intel 82801jir ich10r */
678         { 0x8086, 0x3a18, pIIxget, pIIxset },   /* Intel 82801jib ich10 */
679         { 0x8086, 0x3a40, pIIxget, pIIxset },   /* Intel 82801ji */
680         { 0x8086, 0x3a42, pIIxget, pIIxset },   /* Intel 82801ji */
681         { 0x8086, 0x3a48, pIIxget, pIIxset },   /* Intel 82801ji */
682         { 0x8086, 0x3b06, pIIxget, pIIxset },   /* Intel 82801? ibex peak */
683         { 0x8086, 0x3b14, pIIxget, pIIxset },   /* Intel 82801? 3420 */
684         { 0x8086, 0x1c49, pIIxget, pIIxset },   /* Intel 82hm65 cougar point pch */
685         { 0x8086, 0x1c4b, pIIxget, pIIxset },   /* Intel 82hm67 */
686         { 0x8086, 0x1c4f, pIIxget, pIIxset },   /* Intel 82qm67 cougar point pch */
687         { 0x8086, 0x1c52, pIIxget, pIIxset },   /* Intel 82q65 cougar point pch */
688         { 0x8086, 0x1c54, pIIxget, pIIxset },   /* Intel 82q67 cougar point pch */
689         { 0x8086, 0x1e55, pIIxget, pIIxset },   /* Intel QM77 panter point lpc */
690
691         { 0x1106, 0x0586, viaget, viaset },     /* Viatech 82C586 */
692         { 0x1106, 0x0596, viaget, viaset },     /* Viatech 82C596 */
693         { 0x1106, 0x0686, viaget, viaset },     /* Viatech 82C686 */
694         { 0x1106, 0x3177, viaget, viaset },     /* Viatech VT8235 */
695         { 0x1106, 0x3227, viaget, viaset },     /* Viatech VT8237 */
696         { 0x1106, 0x3287, viaget, viaset },     /* Viatech VT8251 */
697         { 0x1106, 0x8410, viaget, viaset },     /* Viatech PV530 bridge */
698         { 0x1045, 0xc700, optiget, optiset },   /* Opti 82C700 */
699         { 0x10b9, 0x1533, aliget, aliset },     /* Al M1533 */
700         { 0x1039, 0x0008, pIIxget, pIIxset },   /* SI 503 */
701         { 0x1039, 0x0496, pIIxget, pIIxset },   /* SI 496 */
702         { 0x1078, 0x0100, cyrixget, cyrixset }, /* Cyrix 5530 Legacy */
703
704         { 0x1022, 0x790e, nil, nil },           /* AMD FCH LPC bridge */
705         { 0x1022, 0x746b, nil, nil },           /* AMD 8111 */
706         { 0x10de, 0x00d1, nil, nil },           /* NVIDIA nForce 3 */
707         { 0x10de, 0x00e0, nil, nil },           /* NVIDIA nForce 3 250 Series */
708         { 0x10de, 0x00e1, nil, nil },           /* NVIDIA nForce 3 250 Series */
709         { 0x1166, 0x0200, nil, nil },           /* ServerWorks ServerSet III LE */
710         { 0x1002, 0x4377, nil, nil },           /* ATI Radeon Xpress 200M */
711         { 0x1002, 0x4372, nil, nil },           /* ATI SB400 */
712         { 0x1002, 0x9601, nil, nil },           /* AMD SB710 */
713         { 0x1002, 0x438d, nil, nil },           /* AMD SB600 */
714         { 0x1002, 0x439d, nil, nil },           /* AMD SB810 */
715 };
716
717 typedef struct Slot Slot;
718 struct Slot {
719         uchar   bus;            /* Pci bus number */
720         uchar   dev;            /* Pci device number */
721         uchar   maps[12];       /* Avoid structs!  Link and mask. */
722         uchar   slot;           /* Add-in/built-in slot */
723         uchar   reserved;
724 };
725
726 typedef struct Router Router;
727 struct Router {
728         uchar   signature[4];   /* Routing table signature */
729         uchar   version[2];     /* Version number */
730         uchar   size[2];        /* Total table size */
731         uchar   bus;            /* Interrupt router bus number */
732         uchar   devfn;          /* Router's devfunc */
733         uchar   pciirqs[2];     /* Exclusive PCI irqs */
734         uchar   compat[4];      /* Compatible PCI interrupt router */
735         uchar   miniport[4];    /* Miniport data */
736         uchar   reserved[11];
737         uchar   checksum;
738 };
739
740 static ushort pciirqs;          /* Exclusive PCI irqs */
741 static Bridge *southbridge;     /* Which southbridge to use. */
742
743 static void
744 pcirouting(void)
745 {
746         Slot *e;
747         Router *r;
748         int i, size, tbdf;
749         Pcidev *sbpci, *pci;
750         uchar *p, pin, irq, link, *map;
751
752         if((p = sigsearch("$PIR", 0)) == nil)
753                 return;
754
755         r = (Router*)p;
756         size = (r->size[1] << 8)|r->size[0];
757         if(size < sizeof(Router) || checksum(r, size))
758                 return;
759
760         if(0) print("PCI interrupt routing table version %d.%d at %p\n",
761                 r->version[0], r->version[1], r);
762
763         tbdf = MKBUS(BusPCI, r->bus, (r->devfn>>3)&0x1f, r->devfn&7);
764         sbpci = pcimatchtbdf(tbdf);
765         if(sbpci == nil) {
766                 print("pcirouting: Cannot find south bridge %T\n", tbdf);
767                 return;
768         }
769
770         for(i = 0; i < nelem(southbridges); i++)
771                 if(sbpci->vid == southbridges[i].vid && sbpci->did == southbridges[i].did)
772                         break;
773
774         if(i == nelem(southbridges)) {
775                 print("pcirouting: ignoring south bridge %T %.4uX/%.4uX\n", tbdf, sbpci->vid, sbpci->did);
776                 return;
777         }
778         southbridge = &southbridges[i];
779         if(southbridge->get == nil)
780                 return;
781
782         pciirqs = (r->pciirqs[1] << 8)|r->pciirqs[0];
783         for(e = (Slot *)&r[1]; (uchar *)e < p + size; e++) {
784                 if(0) {
785                         print("%.2uX/%.2uX %.2uX: ", e->bus, e->dev, e->slot);
786                         for (i = 0; i < 4; i++) {
787                                 map = &e->maps[i * 3];
788                                 print("[%d] %.2uX %.4uX ", i, map[0], (map[2] << 8)|map[1]);
789                         }
790                         print("\n");
791                 }
792                 for(i = 0; i < 8; i++) {
793                         tbdf = MKBUS(BusPCI, e->bus, (e->dev>>3)&0x1f, i);
794                         pci = pcimatchtbdf(tbdf);
795                         if(pci == nil)
796                                 continue;
797                         pin = pcicfgr8(pci, PciINTP);
798                         if(pin == 0 || pin == 0xff)
799                                 continue;
800
801                         map = &e->maps[((pin - 1) % 4) * 3];
802                         link = map[0];
803                         irq = southbridge->get(sbpci, link);
804                         if(irq == pci->intl)
805                                 continue;
806                         if(irq == 0 || (irq & 0x80) != 0){
807                                 irq = pci->intl;
808                                 if(irq == 0 || irq == 0xff)
809                                         continue;
810                                 if(southbridge->set == nil)
811                                         continue;
812                                 southbridge->set(sbpci, link, irq);
813                         }
814                         print("pcirouting: %T at pin %d link %.2uX irq %d -> %d\n", tbdf, pin, link, pci->intl, irq);
815                         pcicfgw8(pci, PciINTL, irq);
816                         pci->intl = irq;
817                 }
818         }
819 }
820
821 static void pcireservemem(void);
822
823 static int
824 pcicfgrw8bios(int tbdf, int rno, int data, int read)
825 {
826         BIOS32ci ci;
827
828         if(pcibiossi == nil)
829                 return -1;
830
831         memset(&ci, 0, sizeof(BIOS32ci));
832         ci.ebx = (BUSBNO(tbdf)<<8)|(BUSDNO(tbdf)<<3)|BUSFNO(tbdf);
833         ci.edi = rno;
834         if(read){
835                 ci.eax = 0xB108;
836                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
837                         return ci.ecx & 0xFF;
838         }
839         else{
840                 ci.eax = 0xB10B;
841                 ci.ecx = data & 0xFF;
842                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
843                         return 0;
844         }
845
846         return -1;
847 }
848
849 static int
850 pcicfgrw16bios(int tbdf, int rno, int data, int read)
851 {
852         BIOS32ci ci;
853
854         if(pcibiossi == nil)
855                 return -1;
856
857         memset(&ci, 0, sizeof(BIOS32ci));
858         ci.ebx = (BUSBNO(tbdf)<<8)|(BUSDNO(tbdf)<<3)|BUSFNO(tbdf);
859         ci.edi = rno;
860         if(read){
861                 ci.eax = 0xB109;
862                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
863                         return ci.ecx & 0xFFFF;
864         }
865         else{
866                 ci.eax = 0xB10C;
867                 ci.ecx = data & 0xFFFF;
868                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
869                         return 0;
870         }
871
872         return -1;
873 }
874
875 static int
876 pcicfgrw32bios(int tbdf, int rno, int data, int read)
877 {
878         BIOS32ci ci;
879
880         if(pcibiossi == nil)
881                 return -1;
882
883         memset(&ci, 0, sizeof(BIOS32ci));
884         ci.ebx = (BUSBNO(tbdf)<<8)|(BUSDNO(tbdf)<<3)|BUSFNO(tbdf);
885         ci.edi = rno;
886         if(read){
887                 ci.eax = 0xB10A;
888                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
889                         return ci.ecx;
890         }
891         else{
892                 ci.eax = 0xB10D;
893                 ci.ecx = data;
894                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
895                         return 0;
896         }
897
898         return -1;
899 }
900
901 static BIOS32si*
902 pcibiosinit(void)
903 {
904         BIOS32ci ci;
905         BIOS32si *si;
906
907         if((si = bios32open("$PCI")) == nil)
908                 return nil;
909
910         memset(&ci, 0, sizeof(BIOS32ci));
911         ci.eax = 0xB101;
912         if(bios32ci(si, &ci) || ci.edx != ((' '<<24)|('I'<<16)|('C'<<8)|'P')){
913                 free(si);
914                 return nil;
915         }
916         if(ci.eax & 0x01)
917                 pcimaxdno = 31;
918         else
919                 pcimaxdno = 15;
920         pcimaxbno = ci.ecx & 0xff;
921
922         return si;
923 }
924
925 void
926 pcibussize(Pcidev *root, ulong *msize, ulong *iosize)
927 {
928         *msize = 0;
929         *iosize = 0;
930         pcibusmap(root, msize, iosize, 0);
931 }
932
933 static void
934 pcicfginit(void)
935 {
936         char *p;
937         Pcidev **list;
938         ulong mema, ioa;
939         int bno, n, pcibios;
940
941         lock(&pcicfginitlock);
942         if(pcicfgmode != -1)
943                 goto out;
944
945         pcibios = 0;
946         if(getconf("*nobios"))
947                 nobios = 1;
948         else if(getconf("*pcibios"))
949                 pcibios = 1;
950         if(getconf("*nopcirouting"))
951                 nopcirouting = 1;
952
953         /*
954          * Try to determine which PCI configuration mode is implemented.
955          * Mode2 uses a byte at 0xCF8 and another at 0xCFA; Mode1 uses
956          * a DWORD at 0xCF8 and another at 0xCFC and will pass through
957          * any non-DWORD accesses as normal I/O cycles. There shouldn't be
958          * a device behind these addresses so if Mode1 accesses fail try
959          * for Mode2 (Mode2 is deprecated).
960          */
961         if(!pcibios){
962                 /*
963                  * Bits [30:24] of PciADDR must be 0,
964                  * according to the spec.
965                  */
966                 n = inl(PciADDR);
967                 if(!(n & 0x7F000000)){
968                         outl(PciADDR, 0x80000000);
969                         outb(PciADDR+3, 0);
970                         if(inl(PciADDR) & 0x80000000){
971                                 pcicfgmode = 1;
972                                 pcimaxdno = 31;
973                         }
974                 }
975                 outl(PciADDR, n);
976
977                 if(pcicfgmode < 0){
978                         /*
979                          * The 'key' part of PciCSE should be 0.
980                          */
981                         n = inb(PciCSE);
982                         if(!(n & 0xF0)){
983                                 outb(PciCSE, 0x0E);
984                                 if(inb(PciCSE) == 0x0E){
985                                         pcicfgmode = 2;
986                                         pcimaxdno = 15;
987                                 }
988                         }
989                         outb(PciCSE, n);
990                 }
991         }
992
993         if(pcicfgmode < 0 || pcibios) {
994                 if((pcibiossi = pcibiosinit()) == nil)
995                         goto out;
996                 pcicfgrw8 = pcicfgrw8bios;
997                 pcicfgrw16 = pcicfgrw16bios;
998                 pcicfgrw32 = pcicfgrw32bios;
999                 pcicfgmode = 3;
1000         }
1001
1002         fmtinstall('T', tbdffmt);
1003
1004         if(p = getconf("*pcimaxbno"))
1005                 pcimaxbno = strtoul(p, 0, 0);
1006         if(p = getconf("*pcimaxdno")){
1007                 n = strtoul(p, 0, 0);
1008                 if(n < pcimaxdno)
1009                         pcimaxdno = n;
1010         }
1011
1012         list = &pciroot;
1013         for(bno = 0; bno <= pcimaxbno; bno++) {
1014                 int sbno = bno;
1015                 bno = pcilscan(bno, list, nil);
1016
1017                 while(*list)
1018                         list = &(*list)->link;
1019
1020                 if (sbno == 0) {
1021                         Pcidev *pci;
1022
1023                         /*
1024                           * If we have found a PCI-to-Cardbus bridge, make sure
1025                           * it has no valid mappings anymore.
1026                           */
1027                         for(pci = pciroot; pci != nil; pci = pci->link){
1028                                 if (pci->ccrb == 6 && pci->ccru == 7) {
1029                                         ushort bcr;
1030
1031                                         /* reset the cardbus */
1032                                         bcr = pcicfgr16(pci, PciBCR);
1033                                         pcicfgw16(pci, PciBCR, 0x40 | bcr);
1034                                         delay(50);
1035                                 }
1036                         }
1037                 }
1038         }
1039
1040         if(pciroot == nil)
1041                 goto out;
1042
1043         if(nobios) {
1044                 /*
1045                  * Work out how big the top bus is
1046                  */
1047                 pcibussize(pciroot, &mema, &ioa);
1048
1049                 /*
1050                  * Align the windows and map it
1051                  */
1052                 ioa = 0x1000;
1053                 mema = 0x90000000;
1054
1055                 DBG("Mask sizes: mem=%lux io=%lux\n", mema, ioa);
1056
1057                 pcibusmap(pciroot, &mema, &ioa, 1);
1058                 DBG("Sizes2: mem=%lux io=%lux\n", mema, ioa);
1059
1060                 goto out;
1061         }
1062
1063         if(!nopcirouting)
1064                 pcirouting();
1065
1066 out:
1067         pcireservemem();
1068         unlock(&pcicfginitlock);
1069
1070         if(getconf("*pcihinv"))
1071                 pcihinv(nil);
1072 }
1073
1074 static void
1075 pcireservemem(void)
1076 {
1077         int i;
1078         Pcidev *p;
1079
1080         /*
1081          * mark all the physical address space claimed by pci devices
1082          * as in use, so that upaalloc doesn't give it out.
1083          */
1084         for(p=pciroot; p; p=p->list)
1085                 for(i=0; i<nelem(p->mem); i++)
1086                         if((p->mem[i].bar&~4) != 0 && (p->mem[i].bar&1) == 0)
1087                                 upaalloc(p->mem[i].bar&~0x0F, p->mem[i].size, 0);
1088 }
1089
1090 static int
1091 pcicfgrw8raw(int tbdf, int rno, int data, int read)
1092 {
1093         int o, type, x;
1094
1095         if(pcicfgmode == -1)
1096                 pcicfginit();
1097
1098         if(BUSBNO(tbdf))
1099                 type = 0x01;
1100         else
1101                 type = 0x00;
1102         x = -1;
1103         if(BUSDNO(tbdf) > pcimaxdno)
1104                 return x;
1105
1106         lock(&pcicfglock);
1107         switch(pcicfgmode){
1108
1109         case 1:
1110                 o = rno & 0x03;
1111                 rno &= ~0x03;
1112                 outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
1113                 if(read)
1114                         x = inb(PciDATA+o);
1115                 else
1116                         outb(PciDATA+o, data);
1117                 outl(PciADDR, 0);
1118                 break;
1119
1120         case 2:
1121                 outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
1122                 outb(PciFORWARD, BUSBNO(tbdf));
1123                 if(read)
1124                         x = inb((0xC000|(BUSDNO(tbdf)<<8)) + rno);
1125                 else
1126                         outb((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
1127                 outb(PciCSE, 0);
1128                 break;
1129         }
1130         unlock(&pcicfglock);
1131
1132         return x;
1133 }
1134
1135 int
1136 pcicfgr8(Pcidev* pcidev, int rno)
1137 {
1138         return pcicfgrw8(pcidev->tbdf, rno, 0, 1);
1139 }
1140
1141 void
1142 pcicfgw8(Pcidev* pcidev, int rno, int data)
1143 {
1144         pcicfgrw8(pcidev->tbdf, rno, data, 0);
1145 }
1146
1147 static int
1148 pcicfgrw16raw(int tbdf, int rno, int data, int read)
1149 {
1150         int o, type, x;
1151
1152         if(pcicfgmode == -1)
1153                 pcicfginit();
1154
1155         if(BUSBNO(tbdf))
1156                 type = 0x01;
1157         else
1158                 type = 0x00;
1159         x = -1;
1160         if(BUSDNO(tbdf) > pcimaxdno)
1161                 return x;
1162
1163         lock(&pcicfglock);
1164         switch(pcicfgmode){
1165
1166         case 1:
1167                 o = rno & 0x02;
1168                 rno &= ~0x03;
1169                 outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
1170                 if(read)
1171                         x = ins(PciDATA+o);
1172                 else
1173                         outs(PciDATA+o, data);
1174                 outl(PciADDR, 0);
1175                 break;
1176
1177         case 2:
1178                 outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
1179                 outb(PciFORWARD, BUSBNO(tbdf));
1180                 if(read)
1181                         x = ins((0xC000|(BUSDNO(tbdf)<<8)) + rno);
1182                 else
1183                         outs((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
1184                 outb(PciCSE, 0);
1185                 break;
1186         }
1187         unlock(&pcicfglock);
1188
1189         return x;
1190 }
1191
1192 int
1193 pcicfgr16(Pcidev* pcidev, int rno)
1194 {
1195         return pcicfgrw16(pcidev->tbdf, rno, 0, 1);
1196 }
1197
1198 void
1199 pcicfgw16(Pcidev* pcidev, int rno, int data)
1200 {
1201         pcicfgrw16(pcidev->tbdf, rno, data, 0);
1202 }
1203
1204 static int
1205 pcicfgrw32raw(int tbdf, int rno, int data, int read)
1206 {
1207         int type, x;
1208
1209         if(pcicfgmode == -1)
1210                 pcicfginit();
1211
1212         if(BUSBNO(tbdf))
1213                 type = 0x01;
1214         else
1215                 type = 0x00;
1216         x = -1;
1217         if(BUSDNO(tbdf) > pcimaxdno)
1218                 return x;
1219
1220         lock(&pcicfglock);
1221         switch(pcicfgmode){
1222
1223         case 1:
1224                 rno &= ~0x03;
1225                 outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
1226                 if(read)
1227                         x = inl(PciDATA);
1228                 else
1229                         outl(PciDATA, data);
1230                 outl(PciADDR, 0);
1231                 break;
1232
1233         case 2:
1234                 outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
1235                 outb(PciFORWARD, BUSBNO(tbdf));
1236                 if(read)
1237                         x = inl((0xC000|(BUSDNO(tbdf)<<8)) + rno);
1238                 else
1239                         outl((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
1240                 outb(PciCSE, 0);
1241                 break;
1242         }
1243         unlock(&pcicfglock);
1244
1245         return x;
1246 }
1247
1248 int
1249 pcicfgr32(Pcidev* pcidev, int rno)
1250 {
1251         return pcicfgrw32(pcidev->tbdf, rno, 0, 1);
1252 }
1253
1254 void
1255 pcicfgw32(Pcidev* pcidev, int rno, int data)
1256 {
1257         pcicfgrw32(pcidev->tbdf, rno, data, 0);
1258 }
1259
1260 Pcidev*
1261 pcimatch(Pcidev* prev, int vid, int did)
1262 {
1263         if(pcicfgmode == -1)
1264                 pcicfginit();
1265
1266         if(prev == nil)
1267                 prev = pcilist;
1268         else
1269                 prev = prev->list;
1270
1271         while(prev != nil){
1272                 if((vid == 0 || prev->vid == vid)
1273                 && (did == 0 || prev->did == did))
1274                         break;
1275                 prev = prev->list;
1276         }
1277         return prev;
1278 }
1279
1280 Pcidev*
1281 pcimatchtbdf(int tbdf)
1282 {
1283         Pcidev *pcidev;
1284
1285         if(pcicfgmode == -1)
1286                 pcicfginit();
1287
1288         for(pcidev = pcilist; pcidev != nil; pcidev = pcidev->list) {
1289                 if(pcidev->tbdf == tbdf)
1290                         break;
1291         }
1292         return pcidev;
1293 }
1294
1295 uchar
1296 pciipin(Pcidev *pci, uchar pin)
1297 {
1298         if (pci == nil)
1299                 pci = pcilist;
1300
1301         while (pci) {
1302                 uchar intl;
1303
1304                 if (pcicfgr8(pci, PciINTP) == pin && pci->intl != 0 && pci->intl != 0xff)
1305                         return pci->intl;
1306
1307                 if (pci->bridge && (intl = pciipin(pci->bridge, pin)) != 0)
1308                         return intl;
1309
1310                 pci = pci->list;
1311         }
1312         return 0;
1313 }
1314
1315 static void
1316 pcilhinv(Pcidev* p)
1317 {
1318         int i;
1319         Pcidev *t;
1320
1321         if(p == nil) {
1322                 p = pciroot;
1323                 print("bus dev type vid  did intl memory\n");
1324         }
1325         for(t = p; t != nil; t = t->link) {
1326                 print("%d  %2d/%d %.2ux %.2ux %.2ux %.4ux %.4ux %3d  ",
1327                         BUSBNO(t->tbdf), BUSDNO(t->tbdf), BUSFNO(t->tbdf),
1328                         t->ccrb, t->ccru, t->ccrp, t->vid, t->did, t->intl);
1329
1330                 for(i = 0; i < nelem(p->mem); i++) {
1331                         if(t->mem[i].size == 0)
1332                                 continue;
1333                         print("%d:%.8lux %d ", i,
1334                                 t->mem[i].bar, t->mem[i].size);
1335                 }
1336                 if(t->ioa.bar || t->ioa.size)
1337                         print("ioa:%.8lux %d ", t->ioa.bar, t->ioa.size);
1338                 if(t->mema.bar || t->mema.size)
1339                         print("mema:%.8lux %d ", t->mema.bar, t->mema.size);
1340                 if(t->bridge)
1341                         print("->%d", BUSBNO(t->bridge->tbdf));
1342                 print("\n");
1343         }
1344         while(p != nil) {
1345                 if(p->bridge != nil)
1346                         pcilhinv(p->bridge);
1347                 p = p->link;
1348         }
1349 }
1350
1351 void
1352 pcihinv(Pcidev* p)
1353 {
1354         if(pcicfgmode == -1)
1355                 pcicfginit();
1356         lock(&pcicfginitlock);
1357         pcilhinv(p);
1358         unlock(&pcicfginitlock);
1359 }
1360
1361 void
1362 pcireset(void)
1363 {
1364         Pcidev *p;
1365
1366         if(pcicfgmode == -1)
1367                 pcicfginit();
1368
1369         for(p = pcilist; p != nil; p = p->list) {
1370                 /* don't mess with the bridges */
1371                 if(p->ccrb == 0x06)
1372                         continue;
1373                 pciclrbme(p);
1374         }
1375 }
1376
1377 void
1378 pcisetioe(Pcidev* p)
1379 {
1380         p->pcr |= IOen;
1381         pcicfgw16(p, PciPCR, p->pcr);
1382 }
1383
1384 void
1385 pciclrioe(Pcidev* p)
1386 {
1387         p->pcr &= ~IOen;
1388         pcicfgw16(p, PciPCR, p->pcr);
1389 }
1390
1391 void
1392 pcisetbme(Pcidev* p)
1393 {
1394         p->pcr |= MASen;
1395         pcicfgw16(p, PciPCR, p->pcr);
1396 }
1397
1398 void
1399 pciclrbme(Pcidev* p)
1400 {
1401         p->pcr &= ~MASen;
1402         pcicfgw16(p, PciPCR, p->pcr);
1403 }
1404
1405 void
1406 pcisetmwi(Pcidev* p)
1407 {
1408         p->pcr |= MemWrInv;
1409         pcicfgw16(p, PciPCR, p->pcr);
1410 }
1411
1412 void
1413 pciclrmwi(Pcidev* p)
1414 {
1415         p->pcr &= ~MemWrInv;
1416         pcicfgw16(p, PciPCR, p->pcr);
1417 }
1418
1419 static int
1420 enumcaps(Pcidev *p, int (*fmatch)(Pcidev*, int, int, int), int arg)
1421 {
1422         int i, r, cap, off;
1423
1424         /* status register bit 4 has capabilities */
1425         if((pcicfgr16(p, PciPSR) & 1<<4) == 0)
1426                 return -1;      
1427         switch(pcicfgr8(p, PciHDT) & 0x7F){
1428         default:
1429                 return -1;
1430         case 0:                         /* etc */
1431         case 1:                         /* pci to pci bridge */
1432                 off = 0x34;
1433                 break;
1434         case 2:                         /* cardbus bridge */
1435                 off = 0x14;
1436                 break;
1437         }
1438         for(i = 48; i--;){
1439                 off = pcicfgr8(p, off);
1440                 if(off < 0x40 || (off & 3))
1441                         break;
1442                 off &= ~3;
1443                 cap = pcicfgr8(p, off);
1444                 if(cap == 0xff)
1445                         break;
1446                 r = (*fmatch)(p, cap, off, arg);
1447                 if(r < 0)
1448                         break;
1449                 if(r == 0)
1450                         return off;
1451                 off++;
1452         }
1453         return -1;
1454 }
1455
1456 static int
1457 matchcap(Pcidev *, int cap, int, int arg)
1458 {
1459         return cap != arg;
1460 }
1461
1462 static int
1463 matchhtcap(Pcidev *p, int cap, int off, int arg)
1464 {
1465         int mask;
1466
1467         if(cap != PciCapHTC)
1468                 return 1;
1469         if(arg == 0x00 || arg == 0x20)
1470                 mask = 0xE0;
1471         else
1472                 mask = 0xF8;
1473         cap = pcicfgr8(p, off+3);
1474         return (cap & mask) != arg;
1475 }
1476
1477 int
1478 pcicap(Pcidev *p, int cap)
1479 {
1480         return enumcaps(p, matchcap, cap);
1481 }
1482
1483 int
1484 pcihtcap(Pcidev *p, int cap)
1485 {
1486         return enumcaps(p, matchhtcap, cap);
1487 }
1488
1489 static int
1490 pcigetpmrb(Pcidev* p)
1491 {
1492         if(p->pmrb != 0)
1493                 return p->pmrb;
1494         return p->pmrb = pcicap(p, PciCapPMG);
1495 }
1496
1497 int
1498 pcigetpms(Pcidev* p)
1499 {
1500         int pmcsr, ptr;
1501
1502         if((ptr = pcigetpmrb(p)) == -1)
1503                 return -1;
1504
1505         /*
1506          * Power Management Register Block:
1507          *  offset 0:   Capability ID
1508          *         1:   next item pointer
1509          *         2:   capabilities
1510          *         4:   control/status
1511          *         6:   bridge support extensions
1512          *         7:   data
1513          */
1514         pmcsr = pcicfgr16(p, ptr+4);
1515
1516         return pmcsr & 0x0003;
1517 }
1518
1519 int
1520 pcisetpms(Pcidev* p, int state)
1521 {
1522         int ostate, pmc, pmcsr, ptr;
1523
1524         if((ptr = pcigetpmrb(p)) == -1)
1525                 return -1;
1526
1527         pmc = pcicfgr16(p, ptr+2);
1528         pmcsr = pcicfgr16(p, ptr+4);
1529         ostate = pmcsr & 0x0003;
1530         pmcsr &= ~0x0003;
1531
1532         switch(state){
1533         default:
1534                 return -1;
1535         case 0:
1536                 break;
1537         case 1:
1538                 if(!(pmc & 0x0200))
1539                         return -1;
1540                 break;
1541         case 2:
1542                 if(!(pmc & 0x0400))
1543                         return -1;
1544                 break;
1545         case 3:
1546                 break;
1547         }
1548         pmcsr |= state;
1549         pcicfgw16(p, ptr+4, pmcsr);
1550
1551         return ostate;
1552 }
1553
1554 int
1555 pcinextcap(Pcidev *pci, int offset)
1556 {
1557         if(offset == 0) {
1558                 if((pcicfgr16(pci, PciPSR) & (1<<4)) == 0)
1559                         return 0; /* no capabilities */
1560                 offset = PciCAP-1;
1561         }
1562         return pcicfgr8(pci, offset+1) & ~3;
1563 }
1564
1565 void
1566 pcienable(Pcidev *p)
1567 {
1568         uint pcr;
1569         int i;
1570
1571         if(p == nil)
1572                 return;
1573
1574         pcienable(p->parent);
1575
1576         switch(pcisetpms(p, 0)){
1577         case 1:
1578                 print("pcienable %T: wakeup from D1\n", p->tbdf);
1579                 break;
1580         case 2:
1581                 print("pcienable %T: wakeup from D2\n", p->tbdf);
1582                 if(p->bridge != nil)
1583                         delay(100);     /* B2: minimum delay 50ms */
1584                 else
1585                         delay(1);       /* D2: minimum delay 200µs */
1586                 break;
1587         case 3:
1588                 print("pcienable %T: wakeup from D3\n", p->tbdf);
1589                 delay(100);             /* D3: minimum delay 50ms */
1590
1591                 /* restore registers */
1592                 for(i = 0; i < 6; i++)
1593                         pcicfgw32(p, PciBAR0+i*4, p->mem[i].bar);
1594                 pcicfgw8(p, PciINTL, p->intl);
1595                 pcicfgw8(p, PciLTR, p->ltr);
1596                 pcicfgw8(p, PciCLS, p->cls);
1597                 pcicfgw16(p, PciPCR, p->pcr);
1598                 break;
1599         }
1600
1601         if(p->bridge != nil)
1602                 pcr = IOen|MEMen|MASen;
1603         else {
1604                 pcr = 0;
1605                 for(i = 0; i < 6; i++){
1606                         if(p->mem[i].size == 0)
1607                                 continue;
1608                         if(p->mem[i].bar & 1)
1609                                 pcr |= IOen;
1610                         else
1611                                 pcr |= MEMen;
1612                 }
1613         }
1614
1615         if((p->pcr & pcr) != pcr){
1616                 print("pcienable %T: pcr %ux->%ux\n", p->tbdf, p->pcr, p->pcr|pcr);
1617                 p->pcr |= pcr;
1618                 pcicfgrw32(p->tbdf, PciPCR, 0xFFFF0000|p->pcr, 0);
1619         }
1620 }
1621
1622 void
1623 pcidisable(Pcidev *p)
1624 {
1625         if(p == nil)
1626                 return;
1627         pciclrbme(p);
1628 }