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