2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2000, BSDi
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/sys/i386/isa/pci_cfgreg.c,v 1.1.2.7 2001/11/28 05:47:03 imp Exp $
29 * $DragonFly: src/sys/bus/pci/i386/pci_cfgreg.c,v 1.5 2004/01/15 08:05:41 joerg Exp $
33 #include <sys/param.h> /* XXX trim includes */
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/malloc.h>
41 #include <machine/md_var.h>
42 #include <bus/pci/pcivar.h>
43 #include <bus/pci/pcireg.h>
44 #include <bus/isa/isavar.h>
45 #include <bus/pci/i386/pci_cfgreg.h>
46 #include <machine/segments.h>
47 #include <machine/pc/bios.h>
50 #include <machine/smp.h>
53 #define PRVERB(a) do { \
61 static int pci_cfgintr_valid(struct PIR_entry *pe, int pin, int irq);
62 static int pci_cfgintr_unique(struct PIR_entry *pe, int pin);
63 static int pci_cfgintr_linked(struct PIR_entry *pe, int pin);
64 static int pci_cfgintr_search(struct PIR_entry *pe, int bus, int device, int matchpin, int pin);
65 static int pci_cfgintr_virgin(struct PIR_entry *pe, int pin);
67 static void pci_print_irqmask(u_int16_t irqs);
68 static void pci_print_route_table(struct PIR_table *prt, int size);
69 static int pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
70 static void pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
71 static int pcireg_cfgopen(void);
73 static struct PIR_table *pci_route_table;
74 static int pci_route_count;
77 * Some BIOS writers seem to want to ignore the spec and put
78 * 0 in the intline rather than 255 to indicate none. Some use
79 * numbers in the range 128-254 to indicate something strange and
80 * apparently undocumented anywhere. Assume these are completely bogus
81 * and map them to 255, which means "none".
84 pci_i386_map_intline(int line)
86 if (line == 0 || line >= 128)
87 return (PCI_INVALID_IRQ);
92 pcibios_get_version(void)
94 struct bios_regs args;
96 if (PCIbios.ventry == 0) {
97 PRVERB(("pcibios: No call entry point\n"));
100 args.eax = PCIBIOS_BIOS_PRESENT;
101 if (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))) {
102 PRVERB(("pcibios: BIOS_PRESENT call failed\n"));
105 if (args.edx != 0x20494350) {
106 PRVERB(("pcibios: BIOS_PRESENT didn't return 'PCI ' in edx\n"));
109 return (args.ebx & 0xffff);
113 * Initialise access to PCI configuration space
118 static int opened = 0;
120 static struct PIR_table *pt;
128 if (pcireg_cfgopen() == 0)
131 v = pcibios_get_version();
133 printf("pcibios: BIOS version %x.%02x\n", (v & 0xff00) >> 8,
137 * Look for the interrupt routing table.
139 * We use PCI BIOS's PIR table if it's available $PIR is the
140 * standard way to do this. Sadly some machines are not
141 * standards conforming and have _PIR instead. We shrug and cope
142 * by looking for both.
144 if (pcibios_get_version() >= 0x0210 && pt == NULL) {
145 sigaddr = bios_sigsearch(0, "$PIR", 4, 16, 0);
147 sigaddr = bios_sigsearch(0, "_PIR", 4, 16, 0);
149 pt = (struct PIR_table *)(uintptr_t)
150 BIOS_PADDRTOVADDR(sigaddr);
151 for (cv = (u_int8_t *)pt, ck = 0, i = 0;
152 i < (pt->pt_header.ph_length); i++)
154 if (ck == 0 && pt->pt_header.ph_length >
155 sizeof(struct PIR_header)) {
156 pci_route_table = pt;
157 pci_route_count = (pt->pt_header.ph_length -
158 sizeof(struct PIR_header)) /
159 sizeof(struct PIR_entry);
160 printf("Using $PIR table, %d entries at %p\n",
161 pci_route_count, pci_route_table);
163 pci_print_route_table(pci_route_table,
173 * Read configuration space register
176 pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
183 * If we are using the APIC, the contents of the intline
184 * register will probably be wrong (since they are set up for
185 * use with the PIC. Rather than rewrite these registers
186 * (maybe that would be smarter) we trap attempts to read them
187 * and translate to our private vector numbers.
189 if ((reg == PCIR_INTLINE) && (bytes == 1)) {
191 pin = pcireg_cfgread(bus, slot, func, PCIR_INTPIN, 1);
192 line = pcireg_cfgread(bus, slot, func, PCIR_INTLINE, 1);
197 airq = pci_apic_irq(bus, slot, pin);
199 /* PCI specific entry found in MP table */
201 undirect_pci_irq(line);
205 * PCI interrupts might be redirected to the
206 * ISA bus according to some MP tables. Use the
207 * same methods as used by the ISA devices
208 * devices to find the proper IOAPIC int pin.
210 airq = isa_apic_irq(line);
211 if ((airq >= 0) && (airq != line)) {
212 /* XXX: undirect_pci_irq() ? */
213 undirect_isa_irq(line);
222 * Some BIOS writers seem to want to ignore the spec and put
223 * 0 in the intline rather than 255 to indicate none. The rest of
224 * the code uses 255 as an invalid IRQ.
226 if (reg == PCIR_INTLINE && bytes == 1) {
227 line = pcireg_cfgread(bus, slot, func, PCIR_INTLINE, 1);
228 return pci_i386_map_intline(line);
231 return (pcireg_cfgread(bus, slot, func, reg, bytes));
235 * Write configuration space register
238 pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
240 pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
244 pci_cfgread(pcicfgregs *cfg, int reg, int bytes)
246 return (pci_cfgregread(cfg->bus, cfg->slot, cfg->func, reg, bytes));
250 pci_cfgwrite(pcicfgregs *cfg, int reg, int data, int bytes)
252 pci_cfgregwrite(cfg->bus, cfg->slot, cfg->func, reg, data, bytes);
257 * Route a PCI interrupt
260 pci_cfgintr(int bus, int device, int pin, int oldirq)
262 struct PIR_entry *pe;
264 struct bios_regs args;
270 v = pcibios_get_version();
273 "pci_cfgintr: BIOS %x.%02x doesn't support interrupt routing\n",
274 (v & 0xff00) >> 8, v & 0xff));
275 return (PCI_INVALID_IRQ);
277 if ((bus < 0) || (bus > 255) || (device < 0) || (device > 255) ||
278 (pin < 1) || (pin > 4))
279 return (PCI_INVALID_IRQ);
282 * Scan the entry table for a contender
284 for (i = 0, pe = &pci_route_table->pt_entry[0]; i < pci_route_count;
286 if ((bus != pe->pe_bus) || (device != pe->pe_device))
290 * A link of 0 means that this intpin is not connected to
291 * any other device's interrupt pins and is not connected to
292 * any of the Interrupt Router's interrupt pins, so we can't
295 if (pe->pe_intpin[pin - 1].link == 0)
298 if (pci_cfgintr_valid(pe, pin, oldirq)) {
299 printf("pci_cfgintr: %d:%d INT%c BIOS irq %d\n", bus,
300 device, 'A' + pin - 1, oldirq);
305 * We try to find a linked interrupt, then we look to see
306 * if the interrupt is uniquely routed, then we look for
307 * a virgin interrupt. The virgin interrupt should return
308 * an interrupt we can route, but if that fails, maybe we
309 * should try harder to route a different interrupt.
310 * However, experience has shown that that's rarely the
311 * failure mode we see.
313 irq = pci_cfgintr_linked(pe, pin);
314 if (irq != PCI_INVALID_IRQ)
316 if (irq == PCI_INVALID_IRQ) {
317 irq = pci_cfgintr_unique(pe, pin);
318 if (irq != PCI_INVALID_IRQ)
321 if (irq == PCI_INVALID_IRQ)
322 irq = pci_cfgintr_virgin(pe, pin);
324 if (irq == PCI_INVALID_IRQ)
328 * Ask the BIOS to route the interrupt. If we picked an
329 * interrupt that failed, we should really try other
330 * choices that the BIOS offers us.
332 * For uniquely routed interrupts, we need to try
333 * to route them on some machines. Yet other machines
334 * fail to route, so we have to pretend that in that
335 * case it worked. Isn't PC hardware fun?
337 * NOTE: if we want to whack hardware to do this, then
338 * I think the right way to do that would be to have
339 * bridge drivers that do this. I'm not sure that the
340 * $PIR table would be valid for those interrupt
343 args.eax = PCIBIOS_ROUTE_INTERRUPT;
344 args.ebx = (bus << 8) | (device << 3);
345 /* pin value is 0xa - 0xd */
346 args.ecx = (irq << 8) | (0xa + pin -1);
348 bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)) &&
350 PRVERB(("pci_cfgintr: ROUTE_INTERRUPT failed.\n"));
351 return (PCI_INVALID_IRQ);
353 printf("pci_cfgintr: %d:%d INT%c routed to irq %d\n", bus,
354 device, 'A' + pin - 1, irq);
358 PRVERB(("pci_cfgintr: can't route an interrupt to %d:%d INT%c\n", bus,
359 device, 'A' + pin - 1));
360 return (PCI_INVALID_IRQ);
364 * Check to see if an existing IRQ setting is valid.
367 pci_cfgintr_valid(struct PIR_entry *pe, int pin, int irq)
371 if (!PCI_INTERRUPT_VALID(irq))
373 irqmask = pe->pe_intpin[pin - 1].irqs;
374 if (irqmask & (1 << irq)) {
375 PRVERB(("pci_cfgintr_valid: BIOS irq %d is valid\n", irq));
382 * Look to see if the routing table claims this pin is uniquely routed.
385 pci_cfgintr_unique(struct PIR_entry *pe, int pin)
390 irqmask = pe->pe_intpin[pin - 1].irqs;
391 if(irqmask != 0 && powerof2(irqmask)) {
392 irq = ffs(irqmask) - 1;
393 PRVERB(("pci_cfgintr_unique: hard-routed to irq %d\n", irq));
396 return (PCI_INVALID_IRQ);
400 * Look for another device which shares the same link byte and
401 * already has a unique IRQ, or which has had one routed already.
404 pci_cfgintr_linked(struct PIR_entry *pe, int pin)
406 struct PIR_entry *oe;
407 struct PIR_intpin *pi;
413 for (i = 0, oe = &pci_route_table->pt_entry[0]; i < pci_route_count;
415 /* scan interrupt pins */
416 for (j = 0, pi = &oe->pe_intpin[0]; j < 4; j++, pi++) {
418 /* don't look at the entry we're trying to match */
419 if ((pe == oe) && (i == (pin - 1)))
421 /* compare link bytes */
422 if (pi->link != pe->pe_intpin[pin - 1].link)
424 /* link destination mapped to a unique interrupt? */
425 if (pi->irqs != 0 && powerof2(pi->irqs)) {
426 irq = ffs(pi->irqs) - 1;
427 PRVERB(("pci_cfgintr_linked: linked (%x) to hard-routed irq %d\n",
433 * look for the real PCI device that matches this
436 irq = pci_cfgintr_search(pe, oe->pe_bus, oe->pe_device,
438 if (irq != PCI_INVALID_IRQ)
442 return (PCI_INVALID_IRQ);
446 * Scan for the real PCI device at (bus)/(device) using intpin (matchpin) and
447 * see if it has already been assigned an interrupt.
450 pci_cfgintr_search(struct PIR_entry *pe, int bus, int device, int matchpin, int pin)
452 devclass_t pci_devclass;
453 device_t *pci_devices;
455 device_t *pci_children;
457 device_t *busp, *childp;
461 * Find all the PCI busses.
464 if ((pci_devclass = devclass_find("pci")) != NULL)
465 devclass_get_devices(pci_devclass, &pci_devices, &pci_count);
468 * Scan all the PCI busses/devices looking for this one.
470 irq = PCI_INVALID_IRQ;
471 for (i = 0, busp = pci_devices; (i < pci_count) && (irq == PCI_INVALID_IRQ);
474 device_get_children(*busp, &pci_children, &pci_childcount);
476 for (j = 0, childp = pci_children; j < pci_childcount; j++,
478 if ((pci_get_bus(*childp) == bus) &&
479 (pci_get_slot(*childp) == device) &&
480 (pci_get_intpin(*childp) == matchpin)) {
481 irq = pci_i386_map_intline(pci_get_irq(*childp));
482 if (irq != PCI_INVALID_IRQ)
483 PRVERB(("pci_cfgintr_search: linked (%x) to configured irq %d at %d:%d:%d\n",
484 pe->pe_intpin[pin - 1].link, irq,
485 pci_get_bus(*childp),
486 pci_get_slot(*childp),
487 pci_get_function(*childp)));
491 if (pci_children != NULL)
492 free(pci_children, M_TEMP);
494 if (pci_devices != NULL)
495 free(pci_devices, M_TEMP);
500 * Pick a suitable IRQ from those listed as routable to this device.
503 pci_cfgintr_virgin(struct PIR_entry *pe, int pin)
508 * first scan the set of PCI-only interrupts and see if any of these
511 for (irq = 0; irq < 16; irq++) {
514 /* can we use this interrupt? */
515 if ((pci_route_table->pt_header.ph_pci_irqs & ibit) &&
516 (pe->pe_intpin[pin - 1].irqs & ibit)) {
517 PRVERB(("pci_cfgintr_virgin: using routable PCI-only interrupt %d\n", irq));
522 /* life is tough, so just pick an interrupt */
523 for (irq = 0; irq < 16; irq++) {
526 if (pe->pe_intpin[pin - 1].irqs & ibit) {
527 PRVERB(("pci_cfgintr_virgin: using routable interrupt %d\n", irq));
531 return (PCI_INVALID_IRQ);
535 pci_print_irqmask(u_int16_t irqs)
544 for (i = 0; i < 16; i++, irqs >>= 1)
555 * Dump the contents of a PCI BIOS Interrupt Routing Table to the console.
558 pci_print_route_table(struct PIR_table *ptr, int size)
560 struct PIR_entry *entry;
561 struct PIR_intpin *intpin;
564 printf("PCI-Only Interrupts: ");
565 pci_print_irqmask(ptr->pt_header.ph_pci_irqs);
566 printf("\nLocation Bus Device Pin Link IRQs\n");
567 entry = &ptr->pt_entry[0];
568 for (i = 0; i < size; i++, entry++) {
569 intpin = &entry->pe_intpin[0];
570 for (pin = 0; pin < 4; pin++, intpin++)
571 if (intpin->link != 0) {
572 if (entry->pe_slot == 0)
575 printf("slot %-3d ", entry->pe_slot);
576 printf(" %3d %3d %c 0x%02x ",
577 entry->pe_bus, entry->pe_device,
578 'A' + pin, intpin->link);
579 pci_print_irqmask(intpin->irqs);
586 * See if any interrupts for a given PCI bus are routed in the PIR. Don't
587 * even bother looking if the BIOS doesn't support routing anyways.
590 pci_probe_route_table(int bus)
595 v = pcibios_get_version();
598 for (i = 0; i < pci_route_count; i++)
599 if (pci_route_table->pt_entry[i].pe_bus == bus)
605 * Configuration space access using direct register operations
608 /* enable configuration space accesses and return data port address */
610 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
614 if (bus <= PCI_BUSMAX
616 && func <= PCI_FUNCMAX
619 && (unsigned) bytes <= 4
620 && (reg & (bytes - 1)) == 0) {
623 outl(CONF1_ADDR_PORT, (1 << 31)
624 | (bus << 16) | (slot << 11)
625 | (func << 8) | (reg & ~0x03));
626 dataport = CONF1_DATA_PORT + (reg & 0x03);
629 outb(CONF2_ENABLE_PORT, 0xf0 | (func << 1));
630 outb(CONF2_FORWARD_PORT, bus);
631 dataport = 0xc000 | (slot << 8) | reg;
638 /* disable configuration space accesses */
644 outl(CONF1_ADDR_PORT, 0);
647 outb(CONF2_ENABLE_PORT, 0);
648 outb(CONF2_FORWARD_PORT, 0);
654 pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
659 port = pci_cfgenable(bus, slot, func, reg, bytes);
678 pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
682 port = pci_cfgenable(bus, slot, func, reg, bytes);
699 /* check whether the configuration mechanism has been correctly identified */
701 pci_cfgcheck(int maxdev)
709 printf("pci_cfgcheck:\tdevice ");
711 for (device = 0; device < maxdev; device++) {
713 printf("%d ", device);
715 port = pci_cfgenable(0, device, 0, 0, 4);
717 if (id == 0 || id == 0xffffffff)
720 port = pci_cfgenable(0, device, 0, 8, 4);
721 class = inl(port) >> 8;
723 printf("[class=%06x] ", class);
724 if (class == 0 || (class & 0xf870ff) != 0)
727 port = pci_cfgenable(0, device, 0, 14, 1);
730 printf("[hdr=%02x] ", header);
731 if ((header & 0x7e) != 0)
735 printf("is there (id=%08x)\n", id);
741 printf("-- nothing found\n");
750 uint32_t mode1res,oldval1;
751 uint8_t mode2res,oldval2;
753 oldval1 = inl(CONF1_ADDR_PORT);
756 printf("pci_open(1):\tmode 1 addr port (0x0cf8) is 0x%08x\n",
760 if ((oldval1 & CONF1_ENABLE_MSK) == 0) {
765 outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK);
766 outb(CONF1_ADDR_PORT + 3, 0);
767 mode1res = inl(CONF1_ADDR_PORT);
768 outl(CONF1_ADDR_PORT, oldval1);
771 printf("pci_open(1a):\tmode1res=0x%08x (0x%08lx)\n",
772 mode1res, CONF1_ENABLE_CHK);
775 if (pci_cfgcheck(32))
779 outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK1);
780 mode1res = inl(CONF1_ADDR_PORT);
781 outl(CONF1_ADDR_PORT, oldval1);
784 printf("pci_open(1b):\tmode1res=0x%08x (0x%08lx)\n",
785 mode1res, CONF1_ENABLE_CHK1);
787 if ((mode1res & CONF1_ENABLE_MSK1) == CONF1_ENABLE_RES1) {
788 if (pci_cfgcheck(32))
793 oldval2 = inb(CONF2_ENABLE_PORT);
796 printf("pci_open(2):\tmode 2 enable port (0x0cf8) is 0x%02x\n",
800 if ((oldval2 & 0xf0) == 0) {
805 outb(CONF2_ENABLE_PORT, CONF2_ENABLE_CHK);
806 mode2res = inb(CONF2_ENABLE_PORT);
807 outb(CONF2_ENABLE_PORT, oldval2);
810 printf("pci_open(2a):\tmode2res=0x%02x (0x%02x)\n",
811 mode2res, CONF2_ENABLE_CHK);
813 if (mode2res == CONF2_ENABLE_RES) {
815 printf("pci_open(2a):\tnow trying mechanism 2\n");
817 if (pci_cfgcheck(16))