]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc/pci.c
usbehci: catch interrupt in tsleep
[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, 0x746b, nil, nil },           /* AMD 8111 */
705         { 0x10de, 0x00d1, nil, nil },           /* NVIDIA nForce 3 */
706         { 0x10de, 0x00e0, nil, nil },           /* NVIDIA nForce 3 250 Series */
707         { 0x10de, 0x00e1, nil, nil },           /* NVIDIA nForce 3 250 Series */
708         { 0x1166, 0x0200, nil, nil },           /* ServerWorks ServerSet III LE */
709         { 0x1002, 0x4377, nil, nil },           /* ATI Radeon Xpress 200M */
710         { 0x1002, 0x4372, nil, nil },           /* ATI SB400 */
711         { 0x1002, 0x9601, nil, nil },           /* AMD SB710 */
712         { 0x1002, 0x438d, nil, nil },           /* AMD SB600 */
713         { 0x1002, 0x439d, nil, nil },           /* AMD SB810 */
714 };
715
716 typedef struct Slot Slot;
717 struct Slot {
718         uchar   bus;            /* Pci bus number */
719         uchar   dev;            /* Pci device number */
720         uchar   maps[12];       /* Avoid structs!  Link and mask. */
721         uchar   slot;           /* Add-in/built-in slot */
722         uchar   reserved;
723 };
724
725 typedef struct Router Router;
726 struct Router {
727         uchar   signature[4];   /* Routing table signature */
728         uchar   version[2];     /* Version number */
729         uchar   size[2];        /* Total table size */
730         uchar   bus;            /* Interrupt router bus number */
731         uchar   devfn;          /* Router's devfunc */
732         uchar   pciirqs[2];     /* Exclusive PCI irqs */
733         uchar   compat[4];      /* Compatible PCI interrupt router */
734         uchar   miniport[4];    /* Miniport data */
735         uchar   reserved[11];
736         uchar   checksum;
737 };
738
739 static ushort pciirqs;          /* Exclusive PCI irqs */
740 static Bridge *southbridge;     /* Which southbridge to use. */
741
742 static void
743 pcirouting(void)
744 {
745         Slot *e;
746         Router *r;
747         int i, size, tbdf;
748         Pcidev *sbpci, *pci;
749         uchar *p, pin, irq, link, *map;
750
751         if((p = sigsearch("$PIR")) == nil)
752                 return;
753
754         r = (Router*)p;
755         size = (r->size[1] << 8)|r->size[0];
756         if(size < sizeof(Router) || checksum(r, size))
757                 return;
758
759         if(0) print("PCI interrupt routing table version %d.%d at %p\n",
760                 r->version[0], r->version[1], r);
761
762         tbdf = MKBUS(BusPCI, r->bus, (r->devfn>>3)&0x1f, r->devfn&7);
763         sbpci = pcimatchtbdf(tbdf);
764         if(sbpci == nil) {
765                 print("pcirouting: Cannot find south bridge %T\n", tbdf);
766                 return;
767         }
768
769         for(i = 0; i < nelem(southbridges); i++)
770                 if(sbpci->vid == southbridges[i].vid && sbpci->did == southbridges[i].did)
771                         break;
772
773         if(i == nelem(southbridges)) {
774                 print("pcirouting: ignoring south bridge %T %.4uX/%.4uX\n", tbdf, sbpci->vid, sbpci->did);
775                 return;
776         }
777         southbridge = &southbridges[i];
778         if(southbridge->get == nil)
779                 return;
780
781         pciirqs = (r->pciirqs[1] << 8)|r->pciirqs[0];
782         for(e = (Slot *)&r[1]; (uchar *)e < p + size; e++) {
783                 if(0) {
784                         print("%.2uX/%.2uX %.2uX: ", e->bus, e->dev, e->slot);
785                         for (i = 0; i < 4; i++) {
786                                 map = &e->maps[i * 3];
787                                 print("[%d] %.2uX %.4uX ", i, map[0], (map[2] << 8)|map[1]);
788                         }
789                         print("\n");
790                 }
791                 for(i = 0; i < 8; i++) {
792                         tbdf = MKBUS(BusPCI, e->bus, (e->dev>>3)&0x1f, i);
793                         pci = pcimatchtbdf(tbdf);
794                         if(pci == nil)
795                                 continue;
796                         pin = pcicfgr8(pci, PciINTP);
797                         if(pin == 0 || pin == 0xff)
798                                 continue;
799
800                         map = &e->maps[((pin - 1) % 4) * 3];
801                         link = map[0];
802                         irq = southbridge->get(sbpci, link);
803                         if(irq == pci->intl)
804                                 continue;
805                         if(irq == 0 || (irq & 0x80) != 0){
806                                 irq = pci->intl;
807                                 if(irq == 0 || irq == 0xff)
808                                         continue;
809                                 if(southbridge->set == nil)
810                                         continue;
811                                 southbridge->set(sbpci, link, irq);
812                         }
813                         print("pcirouting: %T at pin %d link %.2uX irq %d -> %d\n", tbdf, pin, link, pci->intl, irq);
814                         pcicfgw8(pci, PciINTL, irq);
815                         pci->intl = irq;
816                 }
817         }
818 }
819
820 static void pcireservemem(void);
821
822 static int
823 pcicfgrw8bios(int tbdf, int rno, int data, int read)
824 {
825         BIOS32ci ci;
826
827         if(pcibiossi == nil)
828                 return -1;
829
830         memset(&ci, 0, sizeof(BIOS32ci));
831         ci.ebx = (BUSBNO(tbdf)<<8)|(BUSDNO(tbdf)<<3)|BUSFNO(tbdf);
832         ci.edi = rno;
833         if(read){
834                 ci.eax = 0xB108;
835                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
836                         return ci.ecx & 0xFF;
837         }
838         else{
839                 ci.eax = 0xB10B;
840                 ci.ecx = data & 0xFF;
841                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
842                         return 0;
843         }
844
845         return -1;
846 }
847
848 static int
849 pcicfgrw16bios(int tbdf, int rno, int data, int read)
850 {
851         BIOS32ci ci;
852
853         if(pcibiossi == nil)
854                 return -1;
855
856         memset(&ci, 0, sizeof(BIOS32ci));
857         ci.ebx = (BUSBNO(tbdf)<<8)|(BUSDNO(tbdf)<<3)|BUSFNO(tbdf);
858         ci.edi = rno;
859         if(read){
860                 ci.eax = 0xB109;
861                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
862                         return ci.ecx & 0xFFFF;
863         }
864         else{
865                 ci.eax = 0xB10C;
866                 ci.ecx = data & 0xFFFF;
867                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
868                         return 0;
869         }
870
871         return -1;
872 }
873
874 static int
875 pcicfgrw32bios(int tbdf, int rno, int data, int read)
876 {
877         BIOS32ci ci;
878
879         if(pcibiossi == nil)
880                 return -1;
881
882         memset(&ci, 0, sizeof(BIOS32ci));
883         ci.ebx = (BUSBNO(tbdf)<<8)|(BUSDNO(tbdf)<<3)|BUSFNO(tbdf);
884         ci.edi = rno;
885         if(read){
886                 ci.eax = 0xB10A;
887                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
888                         return ci.ecx;
889         }
890         else{
891                 ci.eax = 0xB10D;
892                 ci.ecx = data;
893                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
894                         return 0;
895         }
896
897         return -1;
898 }
899
900 static BIOS32si*
901 pcibiosinit(void)
902 {
903         BIOS32ci ci;
904         BIOS32si *si;
905
906         if((si = bios32open("$PCI")) == nil)
907                 return nil;
908
909         memset(&ci, 0, sizeof(BIOS32ci));
910         ci.eax = 0xB101;
911         if(bios32ci(si, &ci) || ci.edx != ((' '<<24)|('I'<<16)|('C'<<8)|'P')){
912                 free(si);
913                 return nil;
914         }
915         if(ci.eax & 0x01)
916                 pcimaxdno = 31;
917         else
918                 pcimaxdno = 15;
919         pcimaxbno = ci.ecx & 0xff;
920
921         return si;
922 }
923
924 void
925 pcibussize(Pcidev *root, ulong *msize, ulong *iosize)
926 {
927         *msize = 0;
928         *iosize = 0;
929         pcibusmap(root, msize, iosize, 0);
930 }
931
932 static void
933 pcicfginit(void)
934 {
935         char *p;
936         Pcidev **list;
937         ulong mema, ioa;
938         int bno, n, pcibios;
939
940         lock(&pcicfginitlock);
941         if(pcicfgmode != -1)
942                 goto out;
943
944         pcibios = 0;
945         if(getconf("*nobios"))
946                 nobios = 1;
947         else if(getconf("*pcibios"))
948                 pcibios = 1;
949         if(getconf("*nopcirouting"))
950                 nopcirouting = 1;
951
952         /*
953          * Try to determine which PCI configuration mode is implemented.
954          * Mode2 uses a byte at 0xCF8 and another at 0xCFA; Mode1 uses
955          * a DWORD at 0xCF8 and another at 0xCFC and will pass through
956          * any non-DWORD accesses as normal I/O cycles. There shouldn't be
957          * a device behind these addresses so if Mode1 accesses fail try
958          * for Mode2 (Mode2 is deprecated).
959          */
960         if(!pcibios){
961                 /*
962                  * Bits [30:24] of PciADDR must be 0,
963                  * according to the spec.
964                  */
965                 n = inl(PciADDR);
966                 if(!(n & 0x7F000000)){
967                         outl(PciADDR, 0x80000000);
968                         outb(PciADDR+3, 0);
969                         if(inl(PciADDR) & 0x80000000){
970                                 pcicfgmode = 1;
971                                 pcimaxdno = 31;
972                         }
973                 }
974                 outl(PciADDR, n);
975
976                 if(pcicfgmode < 0){
977                         /*
978                          * The 'key' part of PciCSE should be 0.
979                          */
980                         n = inb(PciCSE);
981                         if(!(n & 0xF0)){
982                                 outb(PciCSE, 0x0E);
983                                 if(inb(PciCSE) == 0x0E){
984                                         pcicfgmode = 2;
985                                         pcimaxdno = 15;
986                                 }
987                         }
988                         outb(PciCSE, n);
989                 }
990         }
991
992         if(pcicfgmode < 0 || pcibios) {
993                 if((pcibiossi = pcibiosinit()) == nil)
994                         goto out;
995                 pcicfgrw8 = pcicfgrw8bios;
996                 pcicfgrw16 = pcicfgrw16bios;
997                 pcicfgrw32 = pcicfgrw32bios;
998                 pcicfgmode = 3;
999         }
1000
1001         fmtinstall('T', tbdffmt);
1002
1003         if(p = getconf("*pcimaxbno"))
1004                 pcimaxbno = strtoul(p, 0, 0);
1005         if(p = getconf("*pcimaxdno")){
1006                 n = strtoul(p, 0, 0);
1007                 if(n < pcimaxdno)
1008                         pcimaxdno = n;
1009         }
1010
1011         list = &pciroot;
1012         for(bno = 0; bno <= pcimaxbno; bno++) {
1013                 int sbno = bno;
1014                 bno = pcilscan(bno, list, nil);
1015
1016                 while(*list)
1017                         list = &(*list)->link;
1018
1019                 if (sbno == 0) {
1020                         Pcidev *pci;
1021
1022                         /*
1023                           * If we have found a PCI-to-Cardbus bridge, make sure
1024                           * it has no valid mappings anymore.
1025                           */
1026                         for(pci = pciroot; pci != nil; pci = pci->link){
1027                                 if (pci->ccrb == 6 && pci->ccru == 7) {
1028                                         ushort bcr;
1029
1030                                         /* reset the cardbus */
1031                                         bcr = pcicfgr16(pci, PciBCR);
1032                                         pcicfgw16(pci, PciBCR, 0x40 | bcr);
1033                                         delay(50);
1034                                 }
1035                         }
1036                 }
1037         }
1038
1039         if(pciroot == nil)
1040                 goto out;
1041
1042         if(nobios) {
1043                 /*
1044                  * Work out how big the top bus is
1045                  */
1046                 pcibussize(pciroot, &mema, &ioa);
1047
1048                 /*
1049                  * Align the windows and map it
1050                  */
1051                 ioa = 0x1000;
1052                 mema = 0x90000000;
1053
1054                 DBG("Mask sizes: mem=%lux io=%lux\n", mema, ioa);
1055
1056                 pcibusmap(pciroot, &mema, &ioa, 1);
1057                 DBG("Sizes2: mem=%lux io=%lux\n", mema, ioa);
1058
1059                 goto out;
1060         }
1061
1062         if(!nopcirouting)
1063                 pcirouting();
1064
1065 out:
1066         pcireservemem();
1067         unlock(&pcicfginitlock);
1068
1069         if(getconf("*pcihinv"))
1070                 pcihinv(nil);
1071 }
1072
1073 static void
1074 pcireservemem(void)
1075 {
1076         int i;
1077         Pcidev *p;
1078
1079         /*
1080          * mark all the physical address space claimed by pci devices
1081          * as in use, so that upaalloc doesn't give it out.
1082          */
1083         for(p=pciroot; p; p=p->list)
1084                 for(i=0; i<nelem(p->mem); i++)
1085                         if(p->mem[i].bar && (p->mem[i].bar&1) == 0)
1086                                 upareserve(p->mem[i].bar&~0x0F, p->mem[i].size);
1087 }
1088
1089 static int
1090 pcicfgrw8raw(int tbdf, int rno, int data, int read)
1091 {
1092         int o, type, x;
1093
1094         if(pcicfgmode == -1)
1095                 pcicfginit();
1096
1097         if(BUSBNO(tbdf))
1098                 type = 0x01;
1099         else
1100                 type = 0x00;
1101         x = -1;
1102         if(BUSDNO(tbdf) > pcimaxdno)
1103                 return x;
1104
1105         lock(&pcicfglock);
1106         switch(pcicfgmode){
1107
1108         case 1:
1109                 o = rno & 0x03;
1110                 rno &= ~0x03;
1111                 outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
1112                 if(read)
1113                         x = inb(PciDATA+o);
1114                 else
1115                         outb(PciDATA+o, data);
1116                 outl(PciADDR, 0);
1117                 break;
1118
1119         case 2:
1120                 outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
1121                 outb(PciFORWARD, BUSBNO(tbdf));
1122                 if(read)
1123                         x = inb((0xC000|(BUSDNO(tbdf)<<8)) + rno);
1124                 else
1125                         outb((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
1126                 outb(PciCSE, 0);
1127                 break;
1128         }
1129         unlock(&pcicfglock);
1130
1131         return x;
1132 }
1133
1134 int
1135 pcicfgr8(Pcidev* pcidev, int rno)
1136 {
1137         return pcicfgrw8(pcidev->tbdf, rno, 0, 1);
1138 }
1139
1140 void
1141 pcicfgw8(Pcidev* pcidev, int rno, int data)
1142 {
1143         pcicfgrw8(pcidev->tbdf, rno, data, 0);
1144 }
1145
1146 static int
1147 pcicfgrw16raw(int tbdf, int rno, int data, int read)
1148 {
1149         int o, type, x;
1150
1151         if(pcicfgmode == -1)
1152                 pcicfginit();
1153
1154         if(BUSBNO(tbdf))
1155                 type = 0x01;
1156         else
1157                 type = 0x00;
1158         x = -1;
1159         if(BUSDNO(tbdf) > pcimaxdno)
1160                 return x;
1161
1162         lock(&pcicfglock);
1163         switch(pcicfgmode){
1164
1165         case 1:
1166                 o = rno & 0x02;
1167                 rno &= ~0x03;
1168                 outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
1169                 if(read)
1170                         x = ins(PciDATA+o);
1171                 else
1172                         outs(PciDATA+o, data);
1173                 outl(PciADDR, 0);
1174                 break;
1175
1176         case 2:
1177                 outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
1178                 outb(PciFORWARD, BUSBNO(tbdf));
1179                 if(read)
1180                         x = ins((0xC000|(BUSDNO(tbdf)<<8)) + rno);
1181                 else
1182                         outs((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
1183                 outb(PciCSE, 0);
1184                 break;
1185         }
1186         unlock(&pcicfglock);
1187
1188         return x;
1189 }
1190
1191 int
1192 pcicfgr16(Pcidev* pcidev, int rno)
1193 {
1194         return pcicfgrw16(pcidev->tbdf, rno, 0, 1);
1195 }
1196
1197 void
1198 pcicfgw16(Pcidev* pcidev, int rno, int data)
1199 {
1200         pcicfgrw16(pcidev->tbdf, rno, data, 0);
1201 }
1202
1203 static int
1204 pcicfgrw32raw(int tbdf, int rno, int data, int read)
1205 {
1206         int type, x;
1207
1208         if(pcicfgmode == -1)
1209                 pcicfginit();
1210
1211         if(BUSBNO(tbdf))
1212                 type = 0x01;
1213         else
1214                 type = 0x00;
1215         x = -1;
1216         if(BUSDNO(tbdf) > pcimaxdno)
1217                 return x;
1218
1219         lock(&pcicfglock);
1220         switch(pcicfgmode){
1221
1222         case 1:
1223                 rno &= ~0x03;
1224                 outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
1225                 if(read)
1226                         x = inl(PciDATA);
1227                 else
1228                         outl(PciDATA, data);
1229                 outl(PciADDR, 0);
1230                 break;
1231
1232         case 2:
1233                 outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
1234                 outb(PciFORWARD, BUSBNO(tbdf));
1235                 if(read)
1236                         x = inl((0xC000|(BUSDNO(tbdf)<<8)) + rno);
1237                 else
1238                         outl((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
1239                 outb(PciCSE, 0);
1240                 break;
1241         }
1242         unlock(&pcicfglock);
1243
1244         return x;
1245 }
1246
1247 int
1248 pcicfgr32(Pcidev* pcidev, int rno)
1249 {
1250         return pcicfgrw32(pcidev->tbdf, rno, 0, 1);
1251 }
1252
1253 void
1254 pcicfgw32(Pcidev* pcidev, int rno, int data)
1255 {
1256         pcicfgrw32(pcidev->tbdf, rno, data, 0);
1257 }
1258
1259 Pcidev*
1260 pcimatch(Pcidev* prev, int vid, int did)
1261 {
1262         if(pcicfgmode == -1)
1263                 pcicfginit();
1264
1265         if(prev == nil)
1266                 prev = pcilist;
1267         else
1268                 prev = prev->list;
1269
1270         while(prev != nil){
1271                 if((vid == 0 || prev->vid == vid)
1272                 && (did == 0 || prev->did == did))
1273                         break;
1274                 prev = prev->list;
1275         }
1276         return prev;
1277 }
1278
1279 Pcidev*
1280 pcimatchtbdf(int tbdf)
1281 {
1282         Pcidev *pcidev;
1283
1284         if(pcicfgmode == -1)
1285                 pcicfginit();
1286
1287         for(pcidev = pcilist; pcidev != nil; pcidev = pcidev->list) {
1288                 if(pcidev->tbdf == tbdf)
1289                         break;
1290         }
1291         return pcidev;
1292 }
1293
1294 uchar
1295 pciipin(Pcidev *pci, uchar pin)
1296 {
1297         if (pci == nil)
1298                 pci = pcilist;
1299
1300         while (pci) {
1301                 uchar intl;
1302
1303                 if (pcicfgr8(pci, PciINTP) == pin && pci->intl != 0 && pci->intl != 0xff)
1304                         return pci->intl;
1305
1306                 if (pci->bridge && (intl = pciipin(pci->bridge, pin)) != 0)
1307                         return intl;
1308
1309                 pci = pci->list;
1310         }
1311         return 0;
1312 }
1313
1314 static void
1315 pcilhinv(Pcidev* p)
1316 {
1317         int i;
1318         Pcidev *t;
1319
1320         if(p == nil) {
1321                 p = pciroot;
1322                 print("bus dev type vid  did intl memory\n");
1323         }
1324         for(t = p; t != nil; t = t->link) {
1325                 print("%d  %2d/%d %.2ux %.2ux %.2ux %.4ux %.4ux %3d  ",
1326                         BUSBNO(t->tbdf), BUSDNO(t->tbdf), BUSFNO(t->tbdf),
1327                         t->ccrb, t->ccru, t->ccrp, t->vid, t->did, t->intl);
1328
1329                 for(i = 0; i < nelem(p->mem); i++) {
1330                         if(t->mem[i].size == 0)
1331                                 continue;
1332                         print("%d:%.8lux %d ", i,
1333                                 t->mem[i].bar, t->mem[i].size);
1334                 }
1335                 if(t->ioa.bar || t->ioa.size)
1336                         print("ioa:%.8lux %d ", t->ioa.bar, t->ioa.size);
1337                 if(t->mema.bar || t->mema.size)
1338                         print("mema:%.8lux %d ", t->mema.bar, t->mema.size);
1339                 if(t->bridge)
1340                         print("->%d", BUSBNO(t->bridge->tbdf));
1341                 print("\n");
1342         }
1343         while(p != nil) {
1344                 if(p->bridge != nil)
1345                         pcilhinv(p->bridge);
1346                 p = p->link;
1347         }
1348 }
1349
1350 void
1351 pcihinv(Pcidev* p)
1352 {
1353         if(pcicfgmode == -1)
1354                 pcicfginit();
1355         lock(&pcicfginitlock);
1356         pcilhinv(p);
1357         unlock(&pcicfginitlock);
1358 }
1359
1360 void
1361 pcireset(void)
1362 {
1363         Pcidev *p;
1364
1365         if(pcicfgmode == -1)
1366                 pcicfginit();
1367
1368         for(p = pcilist; p != nil; p = p->list) {
1369                 /* don't mess with the bridges */
1370                 if(p->ccrb == 0x06)
1371                         continue;
1372                 pciclrbme(p);
1373         }
1374 }
1375
1376 void
1377 pcisetioe(Pcidev* p)
1378 {
1379         p->pcr |= IOen;
1380         pcicfgw16(p, PciPCR, p->pcr);
1381 }
1382
1383 void
1384 pciclrioe(Pcidev* p)
1385 {
1386         p->pcr &= ~IOen;
1387         pcicfgw16(p, PciPCR, p->pcr);
1388 }
1389
1390 void
1391 pcisetbme(Pcidev* p)
1392 {
1393         p->pcr |= MASen;
1394         pcicfgw16(p, PciPCR, p->pcr);
1395 }
1396
1397 void
1398 pciclrbme(Pcidev* p)
1399 {
1400         p->pcr &= ~MASen;
1401         pcicfgw16(p, PciPCR, p->pcr);
1402 }
1403
1404 void
1405 pcisetmwi(Pcidev* p)
1406 {
1407         p->pcr |= MemWrInv;
1408         pcicfgw16(p, PciPCR, p->pcr);
1409 }
1410
1411 void
1412 pciclrmwi(Pcidev* p)
1413 {
1414         p->pcr &= ~MemWrInv;
1415         pcicfgw16(p, PciPCR, p->pcr);
1416 }
1417
1418 static int
1419 enumcaps(Pcidev *p, int (*fmatch)(Pcidev*, int, int, int), int arg)
1420 {
1421         int i, r, cap, off;
1422
1423         /* status register bit 4 has capabilities */
1424         if((pcicfgr16(p, PciPSR) & 1<<4) == 0)
1425                 return -1;      
1426         switch(pcicfgr8(p, PciHDT) & 0x7F){
1427         default:
1428                 return -1;
1429         case 0:                         /* etc */
1430         case 1:                         /* pci to pci bridge */
1431                 off = 0x34;
1432                 break;
1433         case 2:                         /* cardbus bridge */
1434                 off = 0x14;
1435                 break;
1436         }
1437         for(i = 48; i--;){
1438                 off = pcicfgr8(p, off);
1439                 if(off < 0x40 || (off & 3))
1440                         break;
1441                 off &= ~3;
1442                 cap = pcicfgr8(p, off);
1443                 if(cap == 0xff)
1444                         break;
1445                 r = (*fmatch)(p, cap, off, arg);
1446                 if(r < 0)
1447                         break;
1448                 if(r == 0)
1449                         return off;
1450                 off++;
1451         }
1452         return -1;
1453 }
1454
1455 static int
1456 matchcap(Pcidev *, int cap, int, int arg)
1457 {
1458         return cap != arg;
1459 }
1460
1461 static int
1462 matchhtcap(Pcidev *p, int cap, int off, int arg)
1463 {
1464         int mask;
1465
1466         if(cap != PciCapHTC)
1467                 return 1;
1468         if(arg == 0x00 || arg == 0x20)
1469                 mask = 0xE0;
1470         else
1471                 mask = 0xF8;
1472         cap = pcicfgr8(p, off+3);
1473         return (cap & mask) != arg;
1474 }
1475
1476 int
1477 pcicap(Pcidev *p, int cap)
1478 {
1479         return enumcaps(p, matchcap, cap);
1480 }
1481
1482 int
1483 pcihtcap(Pcidev *p, int cap)
1484 {
1485         return enumcaps(p, matchhtcap, cap);
1486 }
1487
1488 static int
1489 pcigetpmrb(Pcidev* p)
1490 {
1491         if(p->pmrb != 0)
1492                 return p->pmrb;
1493         return p->pmrb = pcicap(p, PciCapPMG);
1494 }
1495
1496 int
1497 pcigetpms(Pcidev* p)
1498 {
1499         int pmcsr, ptr;
1500
1501         if((ptr = pcigetpmrb(p)) == -1)
1502                 return -1;
1503
1504         /*
1505          * Power Management Register Block:
1506          *  offset 0:   Capability ID
1507          *         1:   next item pointer
1508          *         2:   capabilities
1509          *         4:   control/status
1510          *         6:   bridge support extensions
1511          *         7:   data
1512          */
1513         pmcsr = pcicfgr16(p, ptr+4);
1514
1515         return pmcsr & 0x0003;
1516 }
1517
1518 int
1519 pcisetpms(Pcidev* p, int state)
1520 {
1521         int ostate, pmc, pmcsr, ptr;
1522
1523         if((ptr = pcigetpmrb(p)) == -1)
1524                 return -1;
1525
1526         pmc = pcicfgr16(p, ptr+2);
1527         pmcsr = pcicfgr16(p, ptr+4);
1528         ostate = pmcsr & 0x0003;
1529         pmcsr &= ~0x0003;
1530
1531         switch(state){
1532         default:
1533                 return -1;
1534         case 0:
1535                 break;
1536         case 1:
1537                 if(!(pmc & 0x0200))
1538                         return -1;
1539                 break;
1540         case 2:
1541                 if(!(pmc & 0x0400))
1542                         return -1;
1543                 break;
1544         case 3:
1545                 break;
1546         }
1547         pmcsr |= state;
1548         pcicfgw16(p, ptr+4, pmcsr);
1549
1550         return ostate;
1551 }
1552
1553 int
1554 pcinextcap(Pcidev *pci, int offset)
1555 {
1556         if(offset == 0) {
1557                 if((pcicfgr16(pci, PciPSR) & (1<<4)) == 0)
1558                         return 0; /* no capabilities */
1559                 offset = PciCAP-1;
1560         }
1561         return pcicfgr8(pci, offset+1) & ~3;
1562 }