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