]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc/pci.c
decad75bad7612f22aa826d0ebba36f913192974
[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)
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                         if(head != nil)
457                                 tail->link = p;
458                         else
459                                 head = p;
460                         tail = p;
461                 }
462         }
463
464         *list = head;
465         for(p = head; p != nil; p = p->link){
466                 /*
467                  * Find PCI-PCI bridges and recursively descend the tree.
468                  */
469                 if(p->ccrb != 0x06 || p->ccru != 0x04)
470                         continue;
471
472                 /*
473                  * If the secondary or subordinate bus number is not
474                  * initialised try to do what the PCI BIOS should have
475                  * done and fill in the numbers as the tree is descended.
476                  * On the way down the subordinate bus number is set to
477                  * the maximum as it's not known how many buses are behind
478                  * this one; the final value is set on the way back up.
479                  */
480                 sbn = pcicfgr8(p, PciSBN);
481                 ubn = pcicfgr8(p, PciUBN);
482
483                 if(sbn == 0 || ubn == 0 || nobios) {
484                         sbn = maxubn+1;
485                         /*
486                          * Make sure memory, I/O and master enables are
487                          * off, set the primary, secondary and subordinate
488                          * bus numbers and clear the secondary status before
489                          * attempting to scan the secondary bus.
490                          *
491                          * Initialisation of the bridge should be done here.
492                          */
493                         pcicfgw32(p, PciPCR, 0xFFFF0000);
494                         l = (MaxUBN<<16)|(sbn<<8)|bno;
495                         pcicfgw32(p, PciPBN, l);
496                         pcicfgw16(p, PciSPSR, 0xFFFF);
497                         maxubn = pcilscan(sbn, &p->bridge);
498                         l = (maxubn<<16)|(sbn<<8)|bno;
499
500                         pcicfgw32(p, PciPBN, l);
501                 }
502                 else {
503                         if(ubn > maxubn)
504                                 maxubn = ubn;
505                         pcilscan(sbn, &p->bridge);
506                 }
507         }
508
509         return maxubn;
510 }
511
512 int
513 pciscan(int bno, Pcidev **list)
514 {
515         int ubn;
516
517         lock(&pcicfginitlock);
518         ubn = pcilscan(bno, list);
519         unlock(&pcicfginitlock);
520         return ubn;
521 }
522
523 static uchar
524 pIIxget(Pcidev *router, uchar link)
525 {
526         uchar pirq;
527
528         /* link should be 0x60, 0x61, 0x62, 0x63 */
529         pirq = pcicfgr8(router, link);
530         return (pirq < 16)? pirq: 0;
531 }
532
533 static void
534 pIIxset(Pcidev *router, uchar link, uchar irq)
535 {
536         pcicfgw8(router, link, irq);
537 }
538
539 static uchar
540 viaget(Pcidev *router, uchar link)
541 {
542         uchar pirq;
543
544         /* link should be 1, 2, 3, 5 */
545         pirq = (link < 6)? pcicfgr8(router, 0x55 + (link>>1)): 0;
546
547         return (link & 1)? (pirq >> 4): (pirq & 15);
548 }
549
550 static void
551 viaset(Pcidev *router, uchar link, uchar irq)
552 {
553         uchar pirq;
554
555         pirq = pcicfgr8(router, 0x55 + (link >> 1));
556         pirq &= (link & 1)? 0x0f: 0xf0;
557         pirq |= (link & 1)? (irq << 4): (irq & 15);
558         pcicfgw8(router, 0x55 + (link>>1), pirq);
559 }
560
561 static uchar
562 optiget(Pcidev *router, uchar link)
563 {
564         uchar pirq = 0;
565
566         /* link should be 0x02, 0x12, 0x22, 0x32 */
567         if ((link & 0xcf) == 0x02)
568                 pirq = pcicfgr8(router, 0xb8 + (link >> 5));
569         return (link & 0x10)? (pirq >> 4): (pirq & 15);
570 }
571
572 static void
573 optiset(Pcidev *router, uchar link, uchar irq)
574 {
575         uchar pirq;
576
577         pirq = pcicfgr8(router, 0xb8 + (link >> 5));
578         pirq &= (link & 0x10)? 0x0f : 0xf0;
579         pirq |= (link & 0x10)? (irq << 4): (irq & 15);
580         pcicfgw8(router, 0xb8 + (link >> 5), pirq);
581 }
582
583 static uchar
584 aliget(Pcidev *router, uchar link)
585 {
586         /* No, you're not dreaming */
587         static const uchar map[] = { 0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15 };
588         uchar pirq;
589
590         /* link should be 0x01..0x08 */
591         pirq = pcicfgr8(router, 0x48 + ((link-1)>>1));
592         return (link & 1)? map[pirq&15]: map[pirq>>4];
593 }
594
595 static void
596 aliset(Pcidev *router, uchar link, uchar irq)
597 {
598         /* Inverse of map in aliget */
599         static const uchar map[] = { 0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15 };
600         uchar pirq;
601
602         pirq = pcicfgr8(router, 0x48 + ((link-1)>>1));
603         pirq &= (link & 1)? 0x0f: 0xf0;
604         pirq |= (link & 1)? (map[irq] << 4): (map[irq] & 15);
605         pcicfgw8(router, 0x48 + ((link-1)>>1), pirq);
606 }
607
608 static uchar
609 cyrixget(Pcidev *router, uchar link)
610 {
611         uchar pirq;
612
613         /* link should be 1, 2, 3, 4 */
614         pirq = pcicfgr8(router, 0x5c + ((link-1)>>1));
615         return ((link & 1)? pirq >> 4: pirq & 15);
616 }
617
618 static void
619 cyrixset(Pcidev *router, uchar link, uchar irq)
620 {
621         uchar pirq;
622
623         pirq = pcicfgr8(router, 0x5c + (link>>1));
624         pirq &= (link & 1)? 0x0f: 0xf0;
625         pirq |= (link & 1)? (irq << 4): (irq & 15);
626         pcicfgw8(router, 0x5c + (link>>1), pirq);
627 }
628
629 typedef struct Bridge Bridge;
630 struct Bridge
631 {
632         ushort  vid;
633         ushort  did;
634         uchar   (*get)(Pcidev *, uchar);
635         void    (*set)(Pcidev *, uchar, uchar);
636 };
637
638 static Bridge southbridges[] = {
639         { 0x8086, 0x122e, pIIxget, pIIxset },   /* Intel 82371FB */
640         { 0x8086, 0x1234, pIIxget, pIIxset },   /* Intel 82371MX */
641         { 0x8086, 0x7000, pIIxget, pIIxset },   /* Intel 82371SB */
642         { 0x8086, 0x7110, pIIxget, pIIxset },   /* Intel 82371AB */
643         { 0x8086, 0x7198, pIIxget, pIIxset },   /* Intel 82443MX (fn 1) */
644         { 0x8086, 0x2410, pIIxget, pIIxset },   /* Intel 82801AA */
645         { 0x8086, 0x2420, pIIxget, pIIxset },   /* Intel 82801AB */
646         { 0x8086, 0x2440, pIIxget, pIIxset },   /* Intel 82801BA */
647         { 0x8086, 0x2448, pIIxget, pIIxset },   /* Intel 82801BAM/CAM/DBM */
648         { 0x8086, 0x244c, pIIxget, pIIxset },   /* Intel 82801BAM */
649         { 0x8086, 0x244e, pIIxget, pIIxset },   /* Intel 82801 */
650         { 0x8086, 0x2480, pIIxget, pIIxset },   /* Intel 82801CA */
651         { 0x8086, 0x248c, pIIxget, pIIxset },   /* Intel 82801CAM */
652         { 0x8086, 0x24c0, pIIxget, pIIxset },   /* Intel 82801DBL */
653         { 0x8086, 0x24cc, pIIxget, pIIxset },   /* Intel 82801DBM */
654         { 0x8086, 0x24d0, pIIxget, pIIxset },   /* Intel 82801EB */
655         { 0x8086, 0x25a1, pIIxget, pIIxset },   /* Intel 6300ESB */
656         { 0x8086, 0x2640, pIIxget, pIIxset },   /* Intel 82801FB */
657         { 0x8086, 0x2641, pIIxget, pIIxset },   /* Intel 82801FBM */
658         { 0x8086, 0x2670, pIIxget, pIIxset },   /* Intel 632xesb */     
659         { 0x8086, 0x27b8, pIIxget, pIIxset },   /* Intel 82801GB */
660         { 0x8086, 0x27b9, pIIxget, pIIxset },   /* Intel 82801GBM */
661         { 0x8086, 0x2810, pIIxget, pIIxset },   /* Intel 82801HB/HR (ich8/r) */
662         { 0x8086, 0x2812, pIIxget, pIIxset },   /* Intel 82801HH (ich8dh) */
663         { 0x8086, 0x2912, pIIxget, pIIxset },   /* Intel 82801ih ich9dh */
664         { 0x8086, 0x2914, pIIxget, pIIxset },   /* Intel 82801io ich9do */
665         { 0x8086, 0x2916, pIIxget, pIIxset },   /* Intel 82801ibr ich9r */
666         { 0x8086, 0x2917, pIIxget, pIIxset },   /* Intel 82801iem ich9m-e  */
667         { 0x8086, 0x2918, pIIxget, pIIxset },   /* Intel 82801ib ich9 */
668         { 0x8086, 0x2919, pIIxget, pIIxset },   /* Intel 82801? ich9m  */
669         { 0x8086, 0x3a16, pIIxget, pIIxset },   /* Intel 82801jir ich10r */
670         { 0x8086, 0x3a40, pIIxget, pIIxset },   /* Intel 82801ji */
671         { 0x8086, 0x3a42, pIIxget, pIIxset },   /* Intel 82801ji */
672         { 0x8086, 0x3a48, pIIxget, pIIxset },   /* Intel 82801ji */
673         { 0x8086, 0x3b06, pIIxget, pIIxset },   /* Intel 82801? ibex peak */
674         { 0x8086, 0x3b14, pIIxget, pIIxset },   /* Intel 82801? 3420 */
675         { 0x8086, 0x1c54, pIIxget, pIIxset },   /* Intel 82q67 cougar point pch */
676         { 0x1106, 0x0586, viaget, viaset },     /* Viatech 82C586 */
677         { 0x1106, 0x0596, viaget, viaset },     /* Viatech 82C596 */
678         { 0x1106, 0x0686, viaget, viaset },     /* Viatech 82C686 */
679         { 0x1106, 0x3227, viaget, viaset },     /* Viatech VT8237 */
680         { 0x1106, 0x8410, viaget, viaset },     /* Viatech PV530 bridge */
681         { 0x1045, 0xc700, optiget, optiset },   /* Opti 82C700 */
682         { 0x10b9, 0x1533, aliget, aliset },     /* Al M1533 */
683         { 0x1039, 0x0008, pIIxget, pIIxset },   /* SI 503 */
684         { 0x1039, 0x0496, pIIxget, pIIxset },   /* SI 496 */
685         { 0x1078, 0x0100, cyrixget, cyrixset }, /* Cyrix 5530 Legacy */
686
687         { 0x1022, 0x746B, nil, nil },           /* AMD 8111 */
688         { 0x10DE, 0x00D1, nil, nil },           /* NVIDIA nForce 3 */
689         { 0x10DE, 0x00E0, nil, nil },           /* NVIDIA nForce 3 250 Series */
690         { 0x10DE, 0x00E1, nil, nil },           /* NVIDIA nForce 3 250 Series */
691         { 0x1166, 0x0200, nil, nil },           /* ServerWorks ServerSet III LE */
692         { 0x1002, 0x4377, nil, nil },           /* ATI Radeon Xpress 200M */
693         { 0x1002, 0x4372, nil, nil },           /* ATI SB400 */
694         { 0x1002, 0x9601, nil, nil },           /* AMD SB710 */
695         { 0x1002, 0x438D, nil, nil },           /* AMD SB600 */
696 };
697
698 typedef struct Slot Slot;
699 struct Slot {
700         uchar   bus;            /* Pci bus number */
701         uchar   dev;            /* Pci device number */
702         uchar   maps[12];       /* Avoid structs!  Link and mask. */
703         uchar   slot;           /* Add-in/built-in slot */
704         uchar   reserved;
705 };
706
707 typedef struct Router Router;
708 struct Router {
709         uchar   signature[4];   /* Routing table signature */
710         uchar   version[2];     /* Version number */
711         uchar   size[2];        /* Total table size */
712         uchar   bus;            /* Interrupt router bus number */
713         uchar   devfn;          /* Router's devfunc */
714         uchar   pciirqs[2];     /* Exclusive PCI irqs */
715         uchar   compat[4];      /* Compatible PCI interrupt router */
716         uchar   miniport[4];    /* Miniport data */
717         uchar   reserved[11];
718         uchar   checksum;
719 };
720
721 static ushort pciirqs;          /* Exclusive PCI irqs */
722 static Bridge *southbridge;     /* Which southbridge to use. */
723
724 static void
725 pcirouting(void)
726 {
727         Slot *e;
728         Router *r;
729         int size, i, fn, tbdf;
730         Pcidev *sbpci, *pci;
731         uchar *p, pin, irq, link, *map;
732
733         /* Search for PCI interrupt routing table in BIOS */
734         for(p = (uchar *)KADDR(0xf0000); p < (uchar *)KADDR(0xfffff); p += 16)
735                 if(p[0] == '$' && p[1] == 'P' && p[2] == 'I' && p[3] == 'R')
736                         break;
737
738         if(p >= (uchar *)KADDR(0xfffff))
739                 return;
740
741         r = (Router *)p;
742
743         // print("PCI interrupt routing table version %d.%d at %.6uX\n",
744         //      r->version[0], r->version[1], (ulong)r & 0xfffff);
745
746         tbdf = (BusPCI << 24)|(r->bus << 16)|(r->devfn << 8);
747         sbpci = pcimatchtbdf(tbdf);
748         if(sbpci == nil) {
749                 print("pcirouting: Cannot find south bridge %T\n", tbdf);
750                 return;
751         }
752
753         for(i = 0; i != nelem(southbridges); i++)
754                 if(sbpci->vid == southbridges[i].vid && sbpci->did == southbridges[i].did)
755                         break;
756
757         if(i == nelem(southbridges)) {
758                 print("pcirouting: ignoring south bridge %T %.4uX/%.4uX\n", tbdf, sbpci->vid, sbpci->did);
759                 return;
760         }
761         southbridge = &southbridges[i];
762         if(southbridge->get == nil || southbridge->set == nil)
763                 return;
764
765         pciirqs = (r->pciirqs[1] << 8)|r->pciirqs[0];
766
767         size = (r->size[1] << 8)|r->size[0];
768         for(e = (Slot *)&r[1]; (uchar *)e < p + size; e++) {
769                 if (0) {
770                         print("%.2uX/%.2uX %.2uX: ", e->bus, e->dev, e->slot);
771                         for (i = 0; i != 4; i++) {
772                                 uchar *m = &e->maps[i * 3];
773                                 print("[%d] %.2uX %.4uX ",
774                                         i, m[0], (m[2] << 8)|m[1]);
775                         }
776                         print("\n");
777                 }
778                 for(fn = 0; fn != 8; fn++) {
779                         tbdf = (BusPCI << 24)|(e->bus << 16)|((e->dev | fn) << 8);
780                         pci = pcimatchtbdf(tbdf);
781                         if(pci == nil)
782                                 continue;
783                         pin = pcicfgr8(pci, PciINTP);
784                         if(pin == 0 || pin == 0xff)
785                                 continue;
786
787                         map = &e->maps[(pin - 1) * 3];
788                         link = map[0];
789                         irq = southbridge->get(sbpci, link);
790                         if(irq == 0 || irq == pci->intl)
791                                 continue;
792                         if(pci->intl != 0 && pci->intl != 0xFF) {
793                                 print("pcirouting: BIOS workaround: %T at pin %d link %d irq %d -> %d\n",
794                                           tbdf, pin, link, irq, pci->intl);
795                                 southbridge->set(sbpci, link, pci->intl);
796                                 continue;
797                         }
798                         print("pcirouting: %T at pin %d link %d irq %d\n", tbdf, pin, link, irq);
799                         pcicfgw8(pci, PciINTL, irq);
800                         pci->intl = irq;
801                 }
802         }
803 }
804
805 static void pcireservemem(void);
806
807 static int
808 pcicfgrw8bios(int tbdf, int rno, int data, int read)
809 {
810         BIOS32ci ci;
811
812         if(pcibiossi == nil)
813                 return -1;
814
815         memset(&ci, 0, sizeof(BIOS32ci));
816         ci.ebx = (BUSBNO(tbdf)<<8)|(BUSDNO(tbdf)<<3)|BUSFNO(tbdf);
817         ci.edi = rno;
818         if(read){
819                 ci.eax = 0xB108;
820                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
821                         return ci.ecx & 0xFF;
822         }
823         else{
824                 ci.eax = 0xB10B;
825                 ci.ecx = data & 0xFF;
826                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
827                         return 0;
828         }
829
830         return -1;
831 }
832
833 static int
834 pcicfgrw16bios(int tbdf, int rno, int data, int read)
835 {
836         BIOS32ci ci;
837
838         if(pcibiossi == nil)
839                 return -1;
840
841         memset(&ci, 0, sizeof(BIOS32ci));
842         ci.ebx = (BUSBNO(tbdf)<<8)|(BUSDNO(tbdf)<<3)|BUSFNO(tbdf);
843         ci.edi = rno;
844         if(read){
845                 ci.eax = 0xB109;
846                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
847                         return ci.ecx & 0xFFFF;
848         }
849         else{
850                 ci.eax = 0xB10C;
851                 ci.ecx = data & 0xFFFF;
852                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
853                         return 0;
854         }
855
856         return -1;
857 }
858
859 static int
860 pcicfgrw32bios(int tbdf, int rno, int data, int read)
861 {
862         BIOS32ci ci;
863
864         if(pcibiossi == nil)
865                 return -1;
866
867         memset(&ci, 0, sizeof(BIOS32ci));
868         ci.ebx = (BUSBNO(tbdf)<<8)|(BUSDNO(tbdf)<<3)|BUSFNO(tbdf);
869         ci.edi = rno;
870         if(read){
871                 ci.eax = 0xB10A;
872                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
873                         return ci.ecx;
874         }
875         else{
876                 ci.eax = 0xB10D;
877                 ci.ecx = data;
878                 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
879                         return 0;
880         }
881
882         return -1;
883 }
884
885 static BIOS32si*
886 pcibiosinit(void)
887 {
888         BIOS32ci ci;
889         BIOS32si *si;
890
891         if((si = bios32open("$PCI")) == nil)
892                 return nil;
893
894         memset(&ci, 0, sizeof(BIOS32ci));
895         ci.eax = 0xB101;
896         if(bios32ci(si, &ci) || ci.edx != ((' '<<24)|('I'<<16)|('C'<<8)|'P')){
897                 free(si);
898                 return nil;
899         }
900         if(ci.eax & 0x01)
901                 pcimaxdno = 31;
902         else
903                 pcimaxdno = 15;
904         pcimaxbno = ci.ecx & 0xff;
905
906         return si;
907 }
908
909 void
910 pcibussize(Pcidev *root, ulong *msize, ulong *iosize)
911 {
912         *msize = 0;
913         *iosize = 0;
914         pcibusmap(root, msize, iosize, 0);
915 }
916
917 static void
918 pcicfginit(void)
919 {
920         char *p;
921         Pcidev **list;
922         ulong mema, ioa;
923         int bno, n, pcibios;
924
925         lock(&pcicfginitlock);
926         if(pcicfgmode != -1)
927                 goto out;
928
929         pcibios = 0;
930         if(getconf("*nobios"))
931                 nobios = 1;
932         else if(getconf("*pcibios"))
933                 pcibios = 1;
934         if(getconf("*nopcirouting"))
935                 nopcirouting = 1;
936
937         /*
938          * Try to determine which PCI configuration mode is implemented.
939          * Mode2 uses a byte at 0xCF8 and another at 0xCFA; Mode1 uses
940          * a DWORD at 0xCF8 and another at 0xCFC and will pass through
941          * any non-DWORD accesses as normal I/O cycles. There shouldn't be
942          * a device behind these addresses so if Mode1 accesses fail try
943          * for Mode2 (Mode2 is deprecated).
944          */
945         if(!pcibios){
946                 /*
947                  * Bits [30:24] of PciADDR must be 0,
948                  * according to the spec.
949                  */
950                 n = inl(PciADDR);
951                 if(!(n & 0x7F000000)){
952                         outl(PciADDR, 0x80000000);
953                         outb(PciADDR+3, 0);
954                         if(inl(PciADDR) & 0x80000000){
955                                 pcicfgmode = 1;
956                                 pcimaxdno = 31;
957                         }
958                 }
959                 outl(PciADDR, n);
960
961                 if(pcicfgmode < 0){
962                         /*
963                          * The 'key' part of PciCSE should be 0.
964                          */
965                         n = inb(PciCSE);
966                         if(!(n & 0xF0)){
967                                 outb(PciCSE, 0x0E);
968                                 if(inb(PciCSE) == 0x0E){
969                                         pcicfgmode = 2;
970                                         pcimaxdno = 15;
971                                 }
972                         }
973                         outb(PciCSE, n);
974                 }
975         }
976
977         if(pcicfgmode < 0 || pcibios) {
978                 if((pcibiossi = pcibiosinit()) == nil)
979                         goto out;
980                 pcicfgrw8 = pcicfgrw8bios;
981                 pcicfgrw16 = pcicfgrw16bios;
982                 pcicfgrw32 = pcicfgrw32bios;
983                 pcicfgmode = 3;
984         }
985
986         fmtinstall('T', tbdffmt);
987
988         if(p = getconf("*pcimaxbno"))
989                 pcimaxbno = strtoul(p, 0, 0) & 0xff;
990         if(p = getconf("*pcimaxdno")){
991                 n = strtoul(p, 0, 0);
992                 if(n < pcimaxdno)
993                         pcimaxdno = n;
994         }
995
996         list = &pciroot;
997         for(bno = 0; bno <= pcimaxbno; bno++) {
998                 int sbno = bno;
999                 bno = pcilscan(bno, list);
1000
1001                 while(*list)
1002                         list = &(*list)->link;
1003
1004                 if (sbno == 0) {
1005                         Pcidev *pci;
1006
1007                         /*
1008                           * If we have found a PCI-to-Cardbus bridge, make sure
1009                           * it has no valid mappings anymore.
1010                           */
1011                         for(pci = pciroot; pci != nil; pci = pci->link){
1012                                 if (pci->ccrb == 6 && pci->ccru == 7) {
1013                                         ushort bcr;
1014
1015                                         /* reset the cardbus */
1016                                         bcr = pcicfgr16(pci, PciBCR);
1017                                         pcicfgw16(pci, PciBCR, 0x40 | bcr);
1018                                         delay(50);
1019                                 }
1020                         }
1021                 }
1022         }
1023
1024         if(pciroot == nil)
1025                 goto out;
1026
1027         if(nobios) {
1028                 /*
1029                  * Work out how big the top bus is
1030                  */
1031                 pcibussize(pciroot, &mema, &ioa);
1032
1033                 /*
1034                  * Align the windows and map it
1035                  */
1036                 ioa = 0x1000;
1037                 mema = 0x90000000;
1038
1039                 pcilog("Mask sizes: mem=%lux io=%lux\n", mema, ioa);
1040
1041                 pcibusmap(pciroot, &mema, &ioa, 1);
1042                 DBG("Sizes2: mem=%lux io=%lux\n", mema, ioa);
1043
1044                 unlock(&pcicfginitlock);
1045                 return;
1046         }
1047
1048         if (!nopcirouting)
1049                 pcirouting();
1050
1051 out:
1052         pcireservemem();
1053         unlock(&pcicfginitlock);
1054
1055         if(getconf("*pcihinv"))
1056                 pcihinv(nil);
1057 }
1058
1059 static void
1060 pcireservemem(void)
1061 {
1062         int i;
1063         Pcidev *p;
1064
1065         /*
1066          * mark all the physical address space claimed by pci devices
1067          * as in use, so that upaalloc doesn't give it out.
1068          */
1069         for(p=pciroot; p; p=p->list)
1070                 for(i=0; i<nelem(p->mem); i++)
1071                         if(p->mem[i].bar && (p->mem[i].bar&1) == 0)
1072                                 upareserve(p->mem[i].bar&~0x0F, p->mem[i].size);
1073 }
1074
1075 static int
1076 pcicfgrw8raw(int tbdf, int rno, int data, int read)
1077 {
1078         int o, type, x;
1079
1080         if(pcicfgmode == -1)
1081                 pcicfginit();
1082
1083         if(BUSBNO(tbdf))
1084                 type = 0x01;
1085         else
1086                 type = 0x00;
1087         x = -1;
1088         if(BUSDNO(tbdf) > pcimaxdno)
1089                 return x;
1090
1091         lock(&pcicfglock);
1092         switch(pcicfgmode){
1093
1094         case 1:
1095                 o = rno & 0x03;
1096                 rno &= ~0x03;
1097                 outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
1098                 if(read)
1099                         x = inb(PciDATA+o);
1100                 else
1101                         outb(PciDATA+o, data);
1102                 outl(PciADDR, 0);
1103                 break;
1104
1105         case 2:
1106                 outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
1107                 outb(PciFORWARD, BUSBNO(tbdf));
1108                 if(read)
1109                         x = inb((0xC000|(BUSDNO(tbdf)<<8)) + rno);
1110                 else
1111                         outb((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
1112                 outb(PciCSE, 0);
1113                 break;
1114         }
1115         unlock(&pcicfglock);
1116
1117         return x;
1118 }
1119
1120 int
1121 pcicfgr8(Pcidev* pcidev, int rno)
1122 {
1123         return pcicfgrw8(pcidev->tbdf, rno, 0, 1);
1124 }
1125
1126 void
1127 pcicfgw8(Pcidev* pcidev, int rno, int data)
1128 {
1129         pcicfgrw8(pcidev->tbdf, rno, data, 0);
1130 }
1131
1132 static int
1133 pcicfgrw16raw(int tbdf, int rno, int data, int read)
1134 {
1135         int o, type, x;
1136
1137         if(pcicfgmode == -1)
1138                 pcicfginit();
1139
1140         if(BUSBNO(tbdf))
1141                 type = 0x01;
1142         else
1143                 type = 0x00;
1144         x = -1;
1145         if(BUSDNO(tbdf) > pcimaxdno)
1146                 return x;
1147
1148         lock(&pcicfglock);
1149         switch(pcicfgmode){
1150
1151         case 1:
1152                 o = rno & 0x02;
1153                 rno &= ~0x03;
1154                 outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
1155                 if(read)
1156                         x = ins(PciDATA+o);
1157                 else
1158                         outs(PciDATA+o, data);
1159                 outl(PciADDR, 0);
1160                 break;
1161
1162         case 2:
1163                 outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
1164                 outb(PciFORWARD, BUSBNO(tbdf));
1165                 if(read)
1166                         x = ins((0xC000|(BUSDNO(tbdf)<<8)) + rno);
1167                 else
1168                         outs((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
1169                 outb(PciCSE, 0);
1170                 break;
1171         }
1172         unlock(&pcicfglock);
1173
1174         return x;
1175 }
1176
1177 int
1178 pcicfgr16(Pcidev* pcidev, int rno)
1179 {
1180         return pcicfgrw16(pcidev->tbdf, rno, 0, 1);
1181 }
1182
1183 void
1184 pcicfgw16(Pcidev* pcidev, int rno, int data)
1185 {
1186         pcicfgrw16(pcidev->tbdf, rno, data, 0);
1187 }
1188
1189 static int
1190 pcicfgrw32raw(int tbdf, int rno, int data, int read)
1191 {
1192         int type, x;
1193
1194         if(pcicfgmode == -1)
1195                 pcicfginit();
1196
1197         if(BUSBNO(tbdf))
1198                 type = 0x01;
1199         else
1200                 type = 0x00;
1201         x = -1;
1202         if(BUSDNO(tbdf) > pcimaxdno)
1203                 return x;
1204
1205         lock(&pcicfglock);
1206         switch(pcicfgmode){
1207
1208         case 1:
1209                 rno &= ~0x03;
1210                 outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
1211                 if(read)
1212                         x = inl(PciDATA);
1213                 else
1214                         outl(PciDATA, data);
1215                 outl(PciADDR, 0);
1216                 break;
1217
1218         case 2:
1219                 outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
1220                 outb(PciFORWARD, BUSBNO(tbdf));
1221                 if(read)
1222                         x = inl((0xC000|(BUSDNO(tbdf)<<8)) + rno);
1223                 else
1224                         outl((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
1225                 outb(PciCSE, 0);
1226                 break;
1227         }
1228         unlock(&pcicfglock);
1229
1230         return x;
1231 }
1232
1233 int
1234 pcicfgr32(Pcidev* pcidev, int rno)
1235 {
1236         return pcicfgrw32(pcidev->tbdf, rno, 0, 1);
1237 }
1238
1239 void
1240 pcicfgw32(Pcidev* pcidev, int rno, int data)
1241 {
1242         pcicfgrw32(pcidev->tbdf, rno, data, 0);
1243 }
1244
1245 Pcidev*
1246 pcimatch(Pcidev* prev, int vid, int did)
1247 {
1248         if(pcicfgmode == -1)
1249                 pcicfginit();
1250
1251         if(prev == nil)
1252                 prev = pcilist;
1253         else
1254                 prev = prev->list;
1255
1256         while(prev != nil){
1257                 if((vid == 0 || prev->vid == vid)
1258                 && (did == 0 || prev->did == did))
1259                         break;
1260                 prev = prev->list;
1261         }
1262         return prev;
1263 }
1264
1265 Pcidev*
1266 pcimatchtbdf(int tbdf)
1267 {
1268         Pcidev *pcidev;
1269
1270         if(pcicfgmode == -1)
1271                 pcicfginit();
1272
1273         for(pcidev = pcilist; pcidev != nil; pcidev = pcidev->list) {
1274                 if(pcidev->tbdf == tbdf)
1275                         break;
1276         }
1277         return pcidev;
1278 }
1279
1280 uchar
1281 pciipin(Pcidev *pci, uchar pin)
1282 {
1283         if (pci == nil)
1284                 pci = pcilist;
1285
1286         while (pci) {
1287                 uchar intl;
1288
1289                 if (pcicfgr8(pci, PciINTP) == pin && pci->intl != 0 && pci->intl != 0xff)
1290                         return pci->intl;
1291
1292                 if (pci->bridge && (intl = pciipin(pci->bridge, pin)) != 0)
1293                         return intl;
1294
1295                 pci = pci->list;
1296         }
1297         return 0;
1298 }
1299
1300 static void
1301 pcilhinv(Pcidev* p)
1302 {
1303         int i;
1304         Pcidev *t;
1305
1306         if(p == nil) {
1307                 putstrn(PCICONS.output, PCICONS.ptr);
1308                 p = pciroot;
1309                 print("bus dev type vid  did intl memory\n");
1310         }
1311         for(t = p; t != nil; t = t->link) {
1312                 print("%d  %2d/%d %.2ux %.2ux %.2ux %.4ux %.4ux %3d  ",
1313                         BUSBNO(t->tbdf), BUSDNO(t->tbdf), BUSFNO(t->tbdf),
1314                         t->ccrb, t->ccru, t->ccrp, t->vid, t->did, t->intl);
1315
1316                 for(i = 0; i < nelem(p->mem); i++) {
1317                         if(t->mem[i].size == 0)
1318                                 continue;
1319                         print("%d:%.8lux %d ", i,
1320                                 t->mem[i].bar, t->mem[i].size);
1321                 }
1322                 if(t->ioa.bar || t->ioa.size)
1323                         print("ioa:%.8lux %d ", t->ioa.bar, t->ioa.size);
1324                 if(t->mema.bar || t->mema.size)
1325                         print("mema:%.8lux %d ", t->mema.bar, t->mema.size);
1326                 if(t->bridge)
1327                         print("->%d", BUSBNO(t->bridge->tbdf));
1328                 print("\n");
1329         }
1330         while(p != nil) {
1331                 if(p->bridge != nil)
1332                         pcilhinv(p->bridge);
1333                 p = p->link;
1334         }
1335 }
1336
1337 void
1338 pcihinv(Pcidev* p)
1339 {
1340         if(pcicfgmode == -1)
1341                 pcicfginit();
1342         lock(&pcicfginitlock);
1343         pcilhinv(p);
1344         unlock(&pcicfginitlock);
1345 }
1346
1347 void
1348 pcireset(void)
1349 {
1350         Pcidev *p;
1351
1352         if(pcicfgmode == -1)
1353                 pcicfginit();
1354
1355         for(p = pcilist; p != nil; p = p->list) {
1356                 /* don't mess with the bridges */
1357                 if(p->ccrb == 0x06)
1358                         continue;
1359                 pciclrbme(p);
1360         }
1361 }
1362
1363 void
1364 pcisetioe(Pcidev* p)
1365 {
1366         p->pcr |= IOen;
1367         pcicfgw16(p, PciPCR, p->pcr);
1368 }
1369
1370 void
1371 pciclrioe(Pcidev* p)
1372 {
1373         p->pcr &= ~IOen;
1374         pcicfgw16(p, PciPCR, p->pcr);
1375 }
1376
1377 void
1378 pcisetbme(Pcidev* p)
1379 {
1380         p->pcr |= MASen;
1381         pcicfgw16(p, PciPCR, p->pcr);
1382 }
1383
1384 void
1385 pciclrbme(Pcidev* p)
1386 {
1387         p->pcr &= ~MASen;
1388         pcicfgw16(p, PciPCR, p->pcr);
1389 }
1390
1391 void
1392 pcisetmwi(Pcidev* p)
1393 {
1394         p->pcr |= MemWrInv;
1395         pcicfgw16(p, PciPCR, p->pcr);
1396 }
1397
1398 void
1399 pciclrmwi(Pcidev* p)
1400 {
1401         p->pcr &= ~MemWrInv;
1402         pcicfgw16(p, PciPCR, p->pcr);
1403 }
1404
1405 static int
1406 pcigetpmrb(Pcidev* p)
1407 {
1408         int ptr;
1409
1410         if(p->pmrb != 0)
1411                 return p->pmrb;
1412         p->pmrb = -1;
1413
1414         /*
1415          * If there are no extended capabilities implemented,
1416          * (bit 4 in the status register) assume there's no standard
1417          * power management method.
1418          * Find the capabilities pointer based on PCI header type.
1419          */
1420         if(!(pcicfgr16(p, PciPSR) & 0x0010))
1421                 return -1;
1422         switch(pcicfgr8(p, PciHDT)){
1423         default:
1424                 return -1;
1425         case 0:                                 /* all other */
1426         case 1:                                 /* PCI to PCI bridge */
1427                 ptr = 0x34;
1428                 break;
1429         case 2:                                 /* CardBus bridge */
1430                 ptr = 0x14;
1431                 break;
1432         }
1433         ptr = pcicfgr32(p, ptr);
1434
1435         while(ptr != 0){
1436                 /*
1437                  * Check for validity.
1438                  * Can't be in standard header and must be double
1439                  * word aligned.
1440                  */
1441                 if(ptr < 0x40 || (ptr & ~0xFC))
1442                         return -1;
1443                 if(pcicfgr8(p, ptr) == 0x01){
1444                         p->pmrb = ptr;
1445                         return ptr;
1446                 }
1447
1448                 ptr = pcicfgr8(p, ptr+1);
1449         }
1450
1451         return -1;
1452 }
1453
1454 int
1455 pcigetpms(Pcidev* p)
1456 {
1457         int pmcsr, ptr;
1458
1459         if((ptr = pcigetpmrb(p)) == -1)
1460                 return -1;
1461
1462         /*
1463          * Power Management Register Block:
1464          *  offset 0:   Capability ID
1465          *         1:   next item pointer
1466          *         2:   capabilities
1467          *         4:   control/status
1468          *         6:   bridge support extensions
1469          *         7:   data
1470          */
1471         pmcsr = pcicfgr16(p, ptr+4);
1472
1473         return pmcsr & 0x0003;
1474 }
1475
1476 int
1477 pcisetpms(Pcidev* p, int state)
1478 {
1479         int ostate, pmc, pmcsr, ptr;
1480
1481         if((ptr = pcigetpmrb(p)) == -1)
1482                 return -1;
1483
1484         pmc = pcicfgr16(p, ptr+2);
1485         pmcsr = pcicfgr16(p, ptr+4);
1486         ostate = pmcsr & 0x0003;
1487         pmcsr &= ~0x0003;
1488
1489         switch(state){
1490         default:
1491                 return -1;
1492         case 0:
1493                 break;
1494         case 1:
1495                 if(!(pmc & 0x0200))
1496                         return -1;
1497                 break;
1498         case 2:
1499                 if(!(pmc & 0x0400))
1500                         return -1;
1501                 break;
1502         case 3:
1503                 break;
1504         }
1505         pmcsr |= state;
1506         pcicfgw16(p, ptr+4, pmcsr);
1507
1508         return ostate;
1509 }
1510
1511 int
1512 pcinextcap(Pcidev *pci, int offset)
1513 {
1514         if(offset == 0) {
1515                 if((pcicfgr16(pci, PciPSR) & (1<<4)) == 0)
1516                         return 0; /* no capabilities */
1517                 offset = PciCAP-1;
1518         }
1519         return pcicfgr8(pci, offset+1) & ~3;
1520 }