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