3 * Needs a massive rewrite.
6 #include "../port/lib.h"
11 #include "../port/error.h"
13 #define DBG if(0) pcilog
22 pcilog(char *fmt, ...)
29 n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
32 memmove(PCICONS.output+PCICONS.ptr, buf, n);
38 { /* configuration mechanism #1 */
39 PciADDR = 0xCF8, /* CONFIG_ADDRESS */
40 PciDATA = 0xCFC, /* CONFIG_DATA */
42 /* configuration mechanism #2 */
43 PciCSE = 0xCF8, /* configuration space enable */
44 PciFORWARD = 0xCFA, /* which bus */
51 { /* command register */
60 static Lock pcicfglock;
61 static Lock pcicfginitlock;
62 static int pcicfgmode = -1;
63 static int pcimaxbno = 255;
65 static Pcidev* pciroot;
66 static Pcidev* pcilist;
67 static Pcidev* pcitail;
68 static int nobios, nopcirouting;
69 static BIOS32si* pcibiossi;
71 static int pcicfgrw8raw(int, int, int, int);
72 static int pcicfgrw16raw(int, int, int, int);
73 static int pcicfgrw32raw(int, int, int, int);
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;
79 static char* bustypes[] = {
107 if((p = malloc(READSTR)) == nil)
108 return fmtstrcpy(fmt, "(tbdfconv)");
112 tbdf = va_arg(fmt->args, int);
113 if(tbdf == BUSUNKNOWN)
114 snprint(p, READSTR, "unknown");
116 type = BUSTYPE(tbdf);
117 if(type < nelem(bustypes))
118 l = snprint(p, READSTR, bustypes[type]);
120 l = snprint(p, READSTR, "%d", type);
121 snprint(p+l, READSTR-l, ".%d.%d.%d",
122 BUSBNO(tbdf), BUSDNO(tbdf), BUSFNO(tbdf));
127 snprint(p, READSTR, "(tbdfconv)");
130 r = fmtstrcpy(fmt, p);
137 pcibarsize(Pcidev *p, int rno)
141 v = pcicfgrw32(p->tbdf, rno, 0, 1);
142 pcicfgrw32(p->tbdf, rno, 0xFFFFFFF0, 0);
143 size = pcicfgrw32(p->tbdf, rno, 0, 1);
146 pcicfgrw32(p->tbdf, rno, v, 0);
148 return -(size & ~0x0F);
152 pcisizcmp(void *a, void *b)
158 return aa->siz - bb->siz;
167 for(m = 1<<(m-1); m != 0; m >>= 1) {
181 pcibusmap(Pcidev *root, ulong *pmema, ulong *pioa, int wrreg)
184 int ntb, i, size, rno, hole;
185 ulong v, mema, ioa, sioa, smema, base, limit;
186 Pcisiz *table, *tptr, *mtb, *itb;
194 DBG("pcibusmap wr=%d %T mem=%luX io=%luX\n",
195 wrreg, root->tbdf, mema, ioa);
198 for(p = root; p != nil; p = p->link)
201 ntb *= (PciCIS-PciBAR0)/4;
202 table = malloc(2*ntb*sizeof(Pcisiz));
204 panic("pcibusmap: can't allocate memory");
209 * Build a table of sizes
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);
220 pcibusmap(p->bridge, &smema, &sioa, 0);
222 hole = pcimask(smema-mema);
227 hole = pcimask(sioa-ioa);
235 itb->siz = p->ioa.size;
240 mtb->siz = p->mema.size;
245 for(i = 0; i <= 5; i++) {
247 v = pcicfgrw32(p->tbdf, rno, 0, 1);
248 size = pcibarsize(p, rno);
265 p->mem[i].size = size;
270 * Sort both tables IO smallest first, Memory largest
272 qsort(table, itb-table, sizeof(Pcisiz), pcisizcmp);
274 qsort(tptr, mtb-tptr, sizeof(Pcisiz), pcisizcmp);
277 * Allocate IO address space on this bus
279 for(tptr = table; tptr < itb; tptr++) {
283 ioa = (ioa+hole-1) & ~(hole-1);
290 p->mem[tptr->bar].bar = ioa|1;
292 pcicfgrw32(p->tbdf, PciBAR0+(tptr->bar*4), ioa|1, 0);
299 * Allocate Memory address space on this bus
301 for(tptr = table+ntb; tptr < mtb; tptr++) {
305 mema = (mema+hole-1) & ~(hole-1);
312 p->mem[tptr->bar].bar = mema;
314 pcicfgrw32(p->tbdf, PciBAR0+(tptr->bar*4), mema, 0);
327 * Finally set all the bridge addresses & registers
329 for(p = root; p != nil; p = p->link) {
330 if(p->bridge == nil) {
331 pcicfgrw8(p->tbdf, PciLTR, 64, 0);
334 pcicfgrw16(p->tbdf, PciPCR, p->pcr, 0);
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);
347 limit = base+p->mema.size-1;
348 v = (limit & 0xFFF00000)|((base & 0xFFF00000)>>16);
349 pcicfgrw32(p->tbdf, PciMBR, v, 0);
352 * Disable memory prefetch
354 pcicfgrw32(p->tbdf, PciPMBR, 0x0000FFFF, 0);
355 pcicfgrw8(p->tbdf, PciLTR, 64, 0);
360 p->pcr |= IOen|MEMen|MASen;
361 pcicfgrw32(p->tbdf, PciPCR, 0xFFFF0000|p->pcr , 0);
365 pcibusmap(p->bridge, &smema, &sioa, 1);
370 pcilscan(int bno, Pcidev** list, Pcidev *parent)
372 Pcidev *p, *head, *tail;
373 int dno, fno, i, hdt, l, maxfno, maxubn, rno, sbn, tbdf, ubn;
378 for(dno = 0; dno <= pcimaxdno; dno++){
380 for(fno = 0; fno <= maxfno; fno++){
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.
389 tbdf = MKBUS(BusPCI, bno, dno, fno);
390 l = pcicfgrw32(tbdf, PciVID, 0, 1);
391 if(l == 0xFFFFFFFF || l == 0)
393 p = malloc(sizeof(*p));
395 panic("pcilscan: can't allocate memory");
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);
414 p->intl = pcicfgr8(p, PciINTL);
417 * If the device is a multi-function device adjust the
418 * loop count so all possible functions are checked.
420 hdt = pcicfgr8(p, PciHDT);
425 * If appropriate, read the base address registers
426 * and work out the sizes.
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)
442 for(i = 0; i < nelem(p->mem); i++) {
444 p->mem[i].bar = pcicfgr32(p, rno);
445 p->mem[i].size = pcibarsize(p, rno);
450 case 0x05: /* memory controller */
451 case 0x06: /* bridge device */
466 for(p = head; p != nil; p = p->link){
468 * Find PCI-PCI bridges and recursively descend the tree.
470 if(p->ccrb != 0x06 || p->ccru != 0x04)
474 * If the secondary or subordinate bus number is not
475 * initialised try to do what the PCI BIOS should have
476 * done and fill in the numbers as the tree is descended.
477 * On the way down the subordinate bus number is set to
478 * the maximum as it's not known how many buses are behind
479 * this one; the final value is set on the way back up.
481 sbn = pcicfgr8(p, PciSBN);
482 ubn = pcicfgr8(p, PciUBN);
484 if(sbn == 0 || ubn == 0 || nobios) {
487 * Make sure memory, I/O and master enables are
488 * off, set the primary, secondary and subordinate
489 * bus numbers and clear the secondary status before
490 * attempting to scan the secondary bus.
492 * Initialisation of the bridge should be done here.
494 pcicfgw32(p, PciPCR, 0xFFFF0000);
495 l = (MaxUBN<<16)|(sbn<<8)|bno;
496 pcicfgw32(p, PciPBN, l);
497 pcicfgw16(p, PciSPSR, 0xFFFF);
498 maxubn = pcilscan(sbn, &p->bridge, p);
499 l = (maxubn<<16)|(sbn<<8)|bno;
501 pcicfgw32(p, PciPBN, l);
506 pcilscan(sbn, &p->bridge, p);
514 pciscan(int bno, Pcidev **list)
518 lock(&pcicfginitlock);
519 ubn = pcilscan(bno, list, nil);
520 unlock(&pcicfginitlock);
525 pIIxget(Pcidev *router, uchar link)
529 /* link should be 0x60, 0x61, 0x62, 0x63 */
530 pirq = pcicfgr8(router, link);
531 return (pirq < 16)? pirq: 0;
535 pIIxset(Pcidev *router, uchar link, uchar irq)
537 pcicfgw8(router, link, irq);
541 viaget(Pcidev *router, uchar link)
545 /* link should be 1, 2, 3, 5 */
546 pirq = (link < 6)? pcicfgr8(router, 0x55 + (link>>1)): 0;
548 return (link & 1)? (pirq >> 4): (pirq & 15);
552 viaset(Pcidev *router, uchar link, uchar irq)
556 pirq = pcicfgr8(router, 0x55 + (link >> 1));
557 pirq &= (link & 1)? 0x0f: 0xf0;
558 pirq |= (link & 1)? (irq << 4): (irq & 15);
559 pcicfgw8(router, 0x55 + (link>>1), pirq);
563 optiget(Pcidev *router, uchar link)
567 /* link should be 0x02, 0x12, 0x22, 0x32 */
568 if ((link & 0xcf) == 0x02)
569 pirq = pcicfgr8(router, 0xb8 + (link >> 5));
570 return (link & 0x10)? (pirq >> 4): (pirq & 15);
574 optiset(Pcidev *router, uchar link, uchar irq)
578 pirq = pcicfgr8(router, 0xb8 + (link >> 5));
579 pirq &= (link & 0x10)? 0x0f : 0xf0;
580 pirq |= (link & 0x10)? (irq << 4): (irq & 15);
581 pcicfgw8(router, 0xb8 + (link >> 5), pirq);
585 aliget(Pcidev *router, uchar link)
587 /* No, you're not dreaming */
588 static const uchar map[] = { 0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15 };
591 /* link should be 0x01..0x08 */
592 pirq = pcicfgr8(router, 0x48 + ((link-1)>>1));
593 return (link & 1)? map[pirq&15]: map[pirq>>4];
597 aliset(Pcidev *router, uchar link, uchar irq)
599 /* Inverse of map in aliget */
600 static const uchar map[] = { 0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15 };
603 pirq = pcicfgr8(router, 0x48 + ((link-1)>>1));
604 pirq &= (link & 1)? 0x0f: 0xf0;
605 pirq |= (link & 1)? (map[irq] << 4): (map[irq] & 15);
606 pcicfgw8(router, 0x48 + ((link-1)>>1), pirq);
610 cyrixget(Pcidev *router, uchar link)
614 /* link should be 1, 2, 3, 4 */
615 pirq = pcicfgr8(router, 0x5c + ((link-1)>>1));
616 return ((link & 1)? pirq >> 4: pirq & 15);
620 cyrixset(Pcidev *router, uchar link, uchar irq)
624 pirq = pcicfgr8(router, 0x5c + (link>>1));
625 pirq &= (link & 1)? 0x0f: 0xf0;
626 pirq |= (link & 1)? (irq << 4): (irq & 15);
627 pcicfgw8(router, 0x5c + (link>>1), pirq);
630 typedef struct Bridge Bridge;
635 uchar (*get)(Pcidev *, uchar);
636 void (*set)(Pcidev *, uchar, uchar);
639 static Bridge southbridges[] = {
640 { 0x8086, 0x122e, pIIxget, pIIxset }, /* Intel 82371FB */
641 { 0x8086, 0x1234, pIIxget, pIIxset }, /* Intel 82371MX */
642 { 0x8086, 0x7000, pIIxget, pIIxset }, /* Intel 82371SB */
643 { 0x8086, 0x7110, pIIxget, pIIxset }, /* Intel 82371AB */
644 { 0x8086, 0x7198, pIIxget, pIIxset }, /* Intel 82443MX (fn 1) */
645 { 0x8086, 0x2410, pIIxget, pIIxset }, /* Intel 82801AA */
646 { 0x8086, 0x2420, pIIxget, pIIxset }, /* Intel 82801AB */
647 { 0x8086, 0x2440, pIIxget, pIIxset }, /* Intel 82801BA */
648 { 0x8086, 0x2448, pIIxget, pIIxset }, /* Intel 82801BAM/CAM/DBM */
649 { 0x8086, 0x244c, pIIxget, pIIxset }, /* Intel 82801BAM */
650 { 0x8086, 0x244e, pIIxget, pIIxset }, /* Intel 82801 */
651 { 0x8086, 0x2480, pIIxget, pIIxset }, /* Intel 82801CA */
652 { 0x8086, 0x248c, pIIxget, pIIxset }, /* Intel 82801CAM */
653 { 0x8086, 0x24c0, pIIxget, pIIxset }, /* Intel 82801DBL */
654 { 0x8086, 0x24cc, pIIxget, pIIxset }, /* Intel 82801DBM */
655 { 0x8086, 0x24d0, pIIxget, pIIxset }, /* Intel 82801EB */
656 { 0x8086, 0x25a1, pIIxget, pIIxset }, /* Intel 6300ESB */
657 { 0x8086, 0x2640, pIIxget, pIIxset }, /* Intel 82801FB */
658 { 0x8086, 0x2641, pIIxget, pIIxset }, /* Intel 82801FBM */
659 { 0x8086, 0x2670, pIIxget, pIIxset }, /* Intel 632xesb */
660 { 0x8086, 0x27b8, pIIxget, pIIxset }, /* Intel 82801GB */
661 { 0x8086, 0x27b9, pIIxget, pIIxset }, /* Intel 82801GBM */
662 { 0x8086, 0x27bd, pIIxget, pIIxset }, /* Intel 82801GB/GR */
663 { 0x8086, 0x3a16, pIIxget, pIIxset }, /* Intel 82801JIR */
664 { 0x8086, 0x3a40, pIIxget, pIIxset }, /* Intel 82801JI */
665 { 0x8086, 0x3a42, pIIxget, pIIxset }, /* Intel 82801JI */
666 { 0x8086, 0x3a48, pIIxget, pIIxset }, /* Intel 82801JI */
667 { 0x8086, 0x2916, pIIxget, pIIxset }, /* Intel 82801? */
668 { 0x8086, 0x2810, pIIxget, pIIxset }, /* Intel 82801HB/HR (ich8/r) */
669 { 0x8086, 0x2812, pIIxget, pIIxset }, /* Intel 82801HH (ich8dh) */
670 { 0x8086, 0x2912, pIIxget, pIIxset }, /* Intel 82801ih ich9dh */
671 { 0x8086, 0x2914, pIIxget, pIIxset }, /* Intel 82801io ich9do */
672 { 0x8086, 0x2916, pIIxget, pIIxset }, /* Intel 82801ibr ich9r */
673 { 0x8086, 0x2917, pIIxget, pIIxset }, /* Intel 82801iem ich9m-e */
674 { 0x8086, 0x2918, pIIxget, pIIxset }, /* Intel 82801ib ich9 */
675 { 0x8086, 0x2919, pIIxget, pIIxset }, /* Intel 82801? ich9m */
676 { 0x8086, 0x3a16, pIIxget, pIIxset }, /* Intel 82801jir ich10r */
677 { 0x8086, 0x3a40, pIIxget, pIIxset }, /* Intel 82801ji */
678 { 0x8086, 0x3a42, pIIxget, pIIxset }, /* Intel 82801ji */
679 { 0x8086, 0x3a48, pIIxget, pIIxset }, /* Intel 82801ji */
680 { 0x8086, 0x3b06, pIIxget, pIIxset }, /* Intel 82801? ibex peak */
681 { 0x8086, 0x3b14, pIIxget, pIIxset }, /* Intel 82801? 3420 */
682 { 0x8086, 0x1c52, pIIxget, pIIxset }, /* Intel 82q65 cougar point pch */
683 { 0x8086, 0x1c54, pIIxget, pIIxset }, /* Intel 82q67 cougar point pch */
684 { 0x1106, 0x0586, viaget, viaset }, /* Viatech 82C586 */
685 { 0x1106, 0x0596, viaget, viaset }, /* Viatech 82C596 */
686 { 0x1106, 0x0686, viaget, viaset }, /* Viatech 82C686 */
687 { 0x1106, 0x3227, viaget, viaset }, /* Viatech VT8237 */
688 { 0x1106, 0x8410, viaget, viaset }, /* Viatech PV530 bridge */
689 { 0x1045, 0xc700, optiget, optiset }, /* Opti 82C700 */
690 { 0x10b9, 0x1533, aliget, aliset }, /* Al M1533 */
691 { 0x1039, 0x0008, pIIxget, pIIxset }, /* SI 503 */
692 { 0x1039, 0x0496, pIIxget, pIIxset }, /* SI 496 */
693 { 0x1078, 0x0100, cyrixget, cyrixset }, /* Cyrix 5530 Legacy */
695 { 0x1022, 0x746b, nil, nil }, /* AMD 8111 */
696 { 0x10de, 0x00d1, nil, nil }, /* NVIDIA nForce 3 */
697 { 0x10de, 0x00e0, nil, nil }, /* NVIDIA nForce 3 250 Series */
698 { 0x10de, 0x00e1, nil, nil }, /* NVIDIA nForce 3 250 Series */
699 { 0x1166, 0x0200, nil, nil }, /* ServerWorks ServerSet III LE */
700 { 0x1002, 0x4377, nil, nil }, /* ATI Radeon Xpress 200M */
701 { 0x1002, 0x4372, nil, nil }, /* ATI SB400 */
702 { 0x1002, 0x9601, nil, nil }, /* AMD SB710 */
703 { 0x1002, 0x438d, nil, nil }, /* AMD SB600 */
704 { 0x1002, 0x439d, nil, nil }, /* AMD SB810 */
707 typedef struct Slot Slot;
709 uchar bus; /* Pci bus number */
710 uchar dev; /* Pci device number */
711 uchar maps[12]; /* Avoid structs! Link and mask. */
712 uchar slot; /* Add-in/built-in slot */
716 typedef struct Router Router;
718 uchar signature[4]; /* Routing table signature */
719 uchar version[2]; /* Version number */
720 uchar size[2]; /* Total table size */
721 uchar bus; /* Interrupt router bus number */
722 uchar devfn; /* Router's devfunc */
723 uchar pciirqs[2]; /* Exclusive PCI irqs */
724 uchar compat[4]; /* Compatible PCI interrupt router */
725 uchar miniport[4]; /* Miniport data */
730 static ushort pciirqs; /* Exclusive PCI irqs */
731 static Bridge *southbridge; /* Which southbridge to use. */
738 int size, i, fn, tbdf;
740 uchar *p, pin, irq, link, *map;
742 if((p = sigsearch("$PIR")) == 0)
746 size = (r->size[1] << 8)|r->size[0];
747 if(size < sizeof(Router) || checksum(r, size))
750 if(0) print("PCI interrupt routing table version %d.%d at %p\n",
751 r->version[0], r->version[1], r);
753 tbdf = (BusPCI << 24)|(r->bus << 16)|(r->devfn << 8);
754 sbpci = pcimatchtbdf(tbdf);
756 print("pcirouting: Cannot find south bridge %T\n", tbdf);
760 for(i = 0; i != nelem(southbridges); i++)
761 if(sbpci->vid == southbridges[i].vid && sbpci->did == southbridges[i].did)
764 if(i == nelem(southbridges)) {
765 print("pcirouting: ignoring south bridge %T %.4uX/%.4uX\n", tbdf, sbpci->vid, sbpci->did);
768 southbridge = &southbridges[i];
769 if(southbridge->get == nil || southbridge->set == nil)
772 pciirqs = (r->pciirqs[1] << 8)|r->pciirqs[0];
773 for(e = (Slot *)&r[1]; (uchar *)e < p + size; e++) {
775 print("%.2uX/%.2uX %.2uX: ", e->bus, e->dev, e->slot);
776 for (i = 0; i != 4; i++) {
777 uchar *m = &e->maps[i * 3];
778 print("[%d] %.2uX %.4uX ",
779 i, m[0], (m[2] << 8)|m[1]);
783 for(fn = 0; fn != 8; fn++) {
784 tbdf = (BusPCI << 24)|(e->bus << 16)|((e->dev | fn) << 8);
785 pci = pcimatchtbdf(tbdf);
788 pin = pcicfgr8(pci, PciINTP);
789 if(pin == 0 || pin == 0xff)
792 map = &e->maps[(pin - 1) * 3];
794 irq = southbridge->get(sbpci, link);
795 if(irq == 0 || irq == pci->intl)
797 if(pci->intl != 0 && pci->intl != 0xFF) {
798 print("pcirouting: BIOS workaround: %T at pin %d link %d irq %d -> %d\n",
799 tbdf, pin, link, irq, pci->intl);
800 southbridge->set(sbpci, link, pci->intl);
803 print("pcirouting: %T at pin %d link %d irq %d\n", tbdf, pin, link, irq);
804 pcicfgw8(pci, PciINTL, irq);
810 static void pcireservemem(void);
813 pcicfgrw8bios(int tbdf, int rno, int data, int read)
820 memset(&ci, 0, sizeof(BIOS32ci));
821 ci.ebx = (BUSBNO(tbdf)<<8)|(BUSDNO(tbdf)<<3)|BUSFNO(tbdf);
825 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
826 return ci.ecx & 0xFF;
830 ci.ecx = data & 0xFF;
831 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
839 pcicfgrw16bios(int tbdf, int rno, int data, int read)
846 memset(&ci, 0, sizeof(BIOS32ci));
847 ci.ebx = (BUSBNO(tbdf)<<8)|(BUSDNO(tbdf)<<3)|BUSFNO(tbdf);
851 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
852 return ci.ecx & 0xFFFF;
856 ci.ecx = data & 0xFFFF;
857 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
865 pcicfgrw32bios(int tbdf, int rno, int data, int read)
872 memset(&ci, 0, sizeof(BIOS32ci));
873 ci.ebx = (BUSBNO(tbdf)<<8)|(BUSDNO(tbdf)<<3)|BUSFNO(tbdf);
877 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
883 if(!bios32ci(pcibiossi, &ci)/* && !(ci.eax & 0xFF)*/)
896 if((si = bios32open("$PCI")) == nil)
899 memset(&ci, 0, sizeof(BIOS32ci));
901 if(bios32ci(si, &ci) || ci.edx != ((' '<<24)|('I'<<16)|('C'<<8)|'P')){
909 pcimaxbno = ci.ecx & 0xff;
915 pcibussize(Pcidev *root, ulong *msize, ulong *iosize)
919 pcibusmap(root, msize, iosize, 0);
930 lock(&pcicfginitlock);
935 if(getconf("*nobios"))
937 else if(getconf("*pcibios"))
939 if(getconf("*nopcirouting"))
943 * Try to determine which PCI configuration mode is implemented.
944 * Mode2 uses a byte at 0xCF8 and another at 0xCFA; Mode1 uses
945 * a DWORD at 0xCF8 and another at 0xCFC and will pass through
946 * any non-DWORD accesses as normal I/O cycles. There shouldn't be
947 * a device behind these addresses so if Mode1 accesses fail try
948 * for Mode2 (Mode2 is deprecated).
952 * Bits [30:24] of PciADDR must be 0,
953 * according to the spec.
956 if(!(n & 0x7F000000)){
957 outl(PciADDR, 0x80000000);
959 if(inl(PciADDR) & 0x80000000){
968 * The 'key' part of PciCSE should be 0.
973 if(inb(PciCSE) == 0x0E){
982 if(pcicfgmode < 0 || pcibios) {
983 if((pcibiossi = pcibiosinit()) == nil)
985 pcicfgrw8 = pcicfgrw8bios;
986 pcicfgrw16 = pcicfgrw16bios;
987 pcicfgrw32 = pcicfgrw32bios;
991 fmtinstall('T', tbdffmt);
993 if(p = getconf("*pcimaxbno"))
994 pcimaxbno = strtoul(p, 0, 0);
995 if(p = getconf("*pcimaxdno")){
996 n = strtoul(p, 0, 0);
1002 for(bno = 0; bno <= pcimaxbno; bno++) {
1004 bno = pcilscan(bno, list, nil);
1007 list = &(*list)->link;
1013 * If we have found a PCI-to-Cardbus bridge, make sure
1014 * it has no valid mappings anymore.
1016 for(pci = pciroot; pci != nil; pci = pci->link){
1017 if (pci->ccrb == 6 && pci->ccru == 7) {
1020 /* reset the cardbus */
1021 bcr = pcicfgr16(pci, PciBCR);
1022 pcicfgw16(pci, PciBCR, 0x40 | bcr);
1034 * Work out how big the top bus is
1036 pcibussize(pciroot, &mema, &ioa);
1039 * Align the windows and map it
1044 pcilog("Mask sizes: mem=%lux io=%lux\n", mema, ioa);
1046 pcibusmap(pciroot, &mema, &ioa, 1);
1047 DBG("Sizes2: mem=%lux io=%lux\n", mema, ioa);
1057 unlock(&pcicfginitlock);
1059 if(getconf("*pcihinv"))
1070 * mark all the physical address space claimed by pci devices
1071 * as in use, so that upaalloc doesn't give it out.
1073 for(p=pciroot; p; p=p->list)
1074 for(i=0; i<nelem(p->mem); i++)
1075 if(p->mem[i].bar && (p->mem[i].bar&1) == 0)
1076 upareserve(p->mem[i].bar&~0x0F, p->mem[i].size);
1080 pcicfgrw8raw(int tbdf, int rno, int data, int read)
1084 if(pcicfgmode == -1)
1092 if(BUSDNO(tbdf) > pcimaxdno)
1101 outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
1105 outb(PciDATA+o, data);
1110 outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
1111 outb(PciFORWARD, BUSBNO(tbdf));
1113 x = inb((0xC000|(BUSDNO(tbdf)<<8)) + rno);
1115 outb((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
1119 unlock(&pcicfglock);
1125 pcicfgr8(Pcidev* pcidev, int rno)
1127 return pcicfgrw8(pcidev->tbdf, rno, 0, 1);
1131 pcicfgw8(Pcidev* pcidev, int rno, int data)
1133 pcicfgrw8(pcidev->tbdf, rno, data, 0);
1137 pcicfgrw16raw(int tbdf, int rno, int data, int read)
1141 if(pcicfgmode == -1)
1149 if(BUSDNO(tbdf) > pcimaxdno)
1158 outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
1162 outs(PciDATA+o, data);
1167 outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
1168 outb(PciFORWARD, BUSBNO(tbdf));
1170 x = ins((0xC000|(BUSDNO(tbdf)<<8)) + rno);
1172 outs((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
1176 unlock(&pcicfglock);
1182 pcicfgr16(Pcidev* pcidev, int rno)
1184 return pcicfgrw16(pcidev->tbdf, rno, 0, 1);
1188 pcicfgw16(Pcidev* pcidev, int rno, int data)
1190 pcicfgrw16(pcidev->tbdf, rno, data, 0);
1194 pcicfgrw32raw(int tbdf, int rno, int data, int read)
1198 if(pcicfgmode == -1)
1206 if(BUSDNO(tbdf) > pcimaxdno)
1214 outl(PciADDR, 0x80000000|BUSBDF(tbdf)|rno|type);
1218 outl(PciDATA, data);
1223 outb(PciCSE, 0x80|(BUSFNO(tbdf)<<1));
1224 outb(PciFORWARD, BUSBNO(tbdf));
1226 x = inl((0xC000|(BUSDNO(tbdf)<<8)) + rno);
1228 outl((0xC000|(BUSDNO(tbdf)<<8)) + rno, data);
1232 unlock(&pcicfglock);
1238 pcicfgr32(Pcidev* pcidev, int rno)
1240 return pcicfgrw32(pcidev->tbdf, rno, 0, 1);
1244 pcicfgw32(Pcidev* pcidev, int rno, int data)
1246 pcicfgrw32(pcidev->tbdf, rno, data, 0);
1250 pcimatch(Pcidev* prev, int vid, int did)
1252 if(pcicfgmode == -1)
1261 if((vid == 0 || prev->vid == vid)
1262 && (did == 0 || prev->did == did))
1270 pcimatchtbdf(int tbdf)
1274 if(pcicfgmode == -1)
1277 for(pcidev = pcilist; pcidev != nil; pcidev = pcidev->list) {
1278 if(pcidev->tbdf == tbdf)
1285 pciipin(Pcidev *pci, uchar pin)
1293 if (pcicfgr8(pci, PciINTP) == pin && pci->intl != 0 && pci->intl != 0xff)
1296 if (pci->bridge && (intl = pciipin(pci->bridge, pin)) != 0)
1311 putstrn(PCICONS.output, PCICONS.ptr);
1313 print("bus dev type vid did intl memory\n");
1315 for(t = p; t != nil; t = t->link) {
1316 print("%d %2d/%d %.2ux %.2ux %.2ux %.4ux %.4ux %3d ",
1317 BUSBNO(t->tbdf), BUSDNO(t->tbdf), BUSFNO(t->tbdf),
1318 t->ccrb, t->ccru, t->ccrp, t->vid, t->did, t->intl);
1320 for(i = 0; i < nelem(p->mem); i++) {
1321 if(t->mem[i].size == 0)
1323 print("%d:%.8lux %d ", i,
1324 t->mem[i].bar, t->mem[i].size);
1326 if(t->ioa.bar || t->ioa.size)
1327 print("ioa:%.8lux %d ", t->ioa.bar, t->ioa.size);
1328 if(t->mema.bar || t->mema.size)
1329 print("mema:%.8lux %d ", t->mema.bar, t->mema.size);
1331 print("->%d", BUSBNO(t->bridge->tbdf));
1335 if(p->bridge != nil)
1336 pcilhinv(p->bridge);
1344 if(pcicfgmode == -1)
1346 lock(&pcicfginitlock);
1348 unlock(&pcicfginitlock);
1356 if(pcicfgmode == -1)
1359 for(p = pcilist; p != nil; p = p->list) {
1360 /* don't mess with the bridges */
1368 pcisetioe(Pcidev* p)
1371 pcicfgw16(p, PciPCR, p->pcr);
1375 pciclrioe(Pcidev* p)
1378 pcicfgw16(p, PciPCR, p->pcr);
1382 pcisetbme(Pcidev* p)
1385 pcicfgw16(p, PciPCR, p->pcr);
1389 pciclrbme(Pcidev* p)
1392 pcicfgw16(p, PciPCR, p->pcr);
1396 pcisetmwi(Pcidev* p)
1399 pcicfgw16(p, PciPCR, p->pcr);
1403 pciclrmwi(Pcidev* p)
1405 p->pcr &= ~MemWrInv;
1406 pcicfgw16(p, PciPCR, p->pcr);
1410 pcicap(Pcidev *p, int cap)
1414 /* status register bit 4 has capabilities */
1415 if((pcicfgr16(p, PciPSR) & 1<<4) == 0)
1417 switch(pcicfgr8(p, PciHDT) & 0x7F){
1421 case 1: /* pci to pci bridge */
1424 case 2: /* cardbus bridge */
1429 off = pcicfgr8(p, off);
1430 if(off < 0x40 || (off & 3))
1433 c = pcicfgr8(p, off);
1444 pcigetpmrb(Pcidev* p)
1448 return p->pmrb = pcicap(p, PciCapPMG);
1452 pcigetpms(Pcidev* p)
1456 if((ptr = pcigetpmrb(p)) == -1)
1460 * Power Management Register Block:
1461 * offset 0: Capability ID
1462 * 1: next item pointer
1465 * 6: bridge support extensions
1468 pmcsr = pcicfgr16(p, ptr+4);
1470 return pmcsr & 0x0003;
1474 pcisetpms(Pcidev* p, int state)
1476 int ostate, pmc, pmcsr, ptr;
1478 if((ptr = pcigetpmrb(p)) == -1)
1481 pmc = pcicfgr16(p, ptr+2);
1482 pmcsr = pcicfgr16(p, ptr+4);
1483 ostate = pmcsr & 0x0003;
1503 pcicfgw16(p, ptr+4, pmcsr);
1509 pcinextcap(Pcidev *pci, int offset)
1512 if((pcicfgr16(pci, PciPSR) & (1<<4)) == 0)
1513 return 0; /* no capabilities */
1516 return pcicfgr8(pci, offset+1) & ~3;