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.6 2004/02/07 15:56:58 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>
39 #include <sys/sysctl.h>
42 #include <machine/md_var.h>
43 #include <bus/pci/pcivar.h>
44 #include <bus/pci/pcireg.h>
45 #include <bus/isa/isavar.h>
46 #include <bus/pci/i386/pci_cfgreg.h>
47 #include <machine/segments.h>
48 #include <machine/pc/bios.h>
51 #include <machine/smp.h>
54 #define PRVERB(a) do { \
59 static int pci_disable_bios_route = 0;
60 SYSCTL_INT(_hw, OID_AUTO, pci_disable_bios_route, CTLFLAG_RD,
61 &pci_disable_bios_route, 0, "disable interrupt routing via PCI-BIOS");
62 TUNABLE_INT("hw.pci_disable_bios_route", &pci_disable_bios_route);
67 static int pci_cfgintr_valid(struct PIR_entry *pe, int pin, int irq);
68 static int pci_cfgintr_unique(struct PIR_entry *pe, int pin);
69 static int pci_cfgintr_linked(struct PIR_entry *pe, int pin);
70 static int pci_cfgintr_search(struct PIR_entry *pe, int bus, int device, int matchpin, int pin);
71 static int pci_cfgintr_virgin(struct PIR_entry *pe, int pin);
73 static void pci_print_irqmask(u_int16_t irqs);
74 static void pci_print_route_table(struct PIR_table *prt, int size);
75 static int pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
76 static void pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
77 static int pcireg_cfgopen(void);
79 static struct PIR_table *pci_route_table;
80 static int pci_route_count;
83 * Some BIOS writers seem to want to ignore the spec and put
84 * 0 in the intline rather than 255 to indicate none. Some use
85 * numbers in the range 128-254 to indicate something strange and
86 * apparently undocumented anywhere. Assume these are completely bogus
87 * and map them to 255, which means "none".
90 pci_i386_map_intline(int line)
92 if (line == 0 || line >= 128)
93 return (PCI_INVALID_IRQ);
98 pcibios_get_version(void)
100 struct bios_regs args;
102 if (PCIbios.ventry == 0) {
103 PRVERB(("pcibios: No call entry point\n"));
106 args.eax = PCIBIOS_BIOS_PRESENT;
107 if (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))) {
108 PRVERB(("pcibios: BIOS_PRESENT call failed\n"));
111 if (args.edx != 0x20494350) {
112 PRVERB(("pcibios: BIOS_PRESENT didn't return 'PCI ' in edx\n"));
115 return (args.ebx & 0xffff);
119 * Initialise access to PCI configuration space
124 static int opened = 0;
126 static struct PIR_table *pt;
134 if (pcireg_cfgopen() == 0)
137 v = pcibios_get_version();
139 printf("pcibios: BIOS version %x.%02x\n", (v & 0xff00) >> 8,
143 * Look for the interrupt routing table.
145 * We use PCI BIOS's PIR table if it's available $PIR is the
146 * standard way to do this. Sadly some machines are not
147 * standards conforming and have _PIR instead. We shrug and cope
148 * by looking for both.
150 if (pcibios_get_version() >= 0x0210 && pt == NULL) {
151 sigaddr = bios_sigsearch(0, "$PIR", 4, 16, 0);
153 sigaddr = bios_sigsearch(0, "_PIR", 4, 16, 0);
155 pt = (struct PIR_table *)(uintptr_t)
156 BIOS_PADDRTOVADDR(sigaddr);
157 for (cv = (u_int8_t *)pt, ck = 0, i = 0;
158 i < (pt->pt_header.ph_length); i++)
160 if (ck == 0 && pt->pt_header.ph_length >
161 sizeof(struct PIR_header)) {
162 pci_route_table = pt;
163 pci_route_count = (pt->pt_header.ph_length -
164 sizeof(struct PIR_header)) /
165 sizeof(struct PIR_entry);
166 printf("Using $PIR table, %d entries at %p\n",
167 pci_route_count, pci_route_table);
169 pci_print_route_table(pci_route_table,
179 * Read configuration space register
182 pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
189 * If we are using the APIC, the contents of the intline
190 * register will probably be wrong (since they are set up for
191 * use with the PIC. Rather than rewrite these registers
192 * (maybe that would be smarter) we trap attempts to read them
193 * and translate to our private vector numbers.
195 if ((reg == PCIR_INTLINE) && (bytes == 1)) {
197 pin = pcireg_cfgread(bus, slot, func, PCIR_INTPIN, 1);
198 line = pcireg_cfgread(bus, slot, func, PCIR_INTLINE, 1);
203 airq = pci_apic_irq(bus, slot, pin);
205 /* PCI specific entry found in MP table */
207 undirect_pci_irq(line);
211 * PCI interrupts might be redirected to the
212 * ISA bus according to some MP tables. Use the
213 * same methods as used by the ISA devices
214 * devices to find the proper IOAPIC int pin.
216 airq = isa_apic_irq(line);
217 if ((airq >= 0) && (airq != line)) {
218 /* XXX: undirect_pci_irq() ? */
219 undirect_isa_irq(line);
228 * Some BIOS writers seem to want to ignore the spec and put
229 * 0 in the intline rather than 255 to indicate none. The rest of
230 * the code uses 255 as an invalid IRQ.
232 if (reg == PCIR_INTLINE && bytes == 1) {
233 line = pcireg_cfgread(bus, slot, func, PCIR_INTLINE, 1);
234 return pci_i386_map_intline(line);
237 return (pcireg_cfgread(bus, slot, func, reg, bytes));
241 * Write configuration space register
244 pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
246 pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
250 pci_cfgread(pcicfgregs *cfg, int reg, int bytes)
252 return (pci_cfgregread(cfg->bus, cfg->slot, cfg->func, reg, bytes));
256 pci_cfgwrite(pcicfgregs *cfg, int reg, int data, int bytes)
258 pci_cfgregwrite(cfg->bus, cfg->slot, cfg->func, reg, data, bytes);
263 * Route a PCI interrupt
266 pci_cfgintr(int bus, int device, int pin, int oldirq)
268 struct PIR_entry *pe;
270 struct bios_regs args;
276 v = pcibios_get_version();
279 "pci_cfgintr: BIOS %x.%02x doesn't support interrupt routing\n",
280 (v & 0xff00) >> 8, v & 0xff));
281 return (PCI_INVALID_IRQ);
283 if ((bus < 0) || (bus > 255) || (device < 0) || (device > 255) ||
284 (pin < 1) || (pin > 4))
285 return (PCI_INVALID_IRQ);
288 * Scan the entry table for a contender
290 for (i = 0, pe = &pci_route_table->pt_entry[0]; i < pci_route_count;
292 if ((bus != pe->pe_bus) || (device != pe->pe_device))
296 * A link of 0 means that this intpin is not connected to
297 * any other device's interrupt pins and is not connected to
298 * any of the Interrupt Router's interrupt pins, so we can't
301 if (pe->pe_intpin[pin - 1].link == 0)
304 if (pci_cfgintr_valid(pe, pin, oldirq)) {
305 printf("pci_cfgintr: %d:%d INT%c BIOS irq %d\n", bus,
306 device, 'A' + pin - 1, oldirq);
311 * We try to find a linked interrupt, then we look to see
312 * if the interrupt is uniquely routed, then we look for
313 * a virgin interrupt. The virgin interrupt should return
314 * an interrupt we can route, but if that fails, maybe we
315 * should try harder to route a different interrupt.
316 * However, experience has shown that that's rarely the
317 * failure mode we see.
319 irq = pci_cfgintr_linked(pe, pin);
320 if (irq != PCI_INVALID_IRQ)
322 if (irq == PCI_INVALID_IRQ) {
323 irq = pci_cfgintr_unique(pe, pin);
324 if (irq != PCI_INVALID_IRQ)
327 if (irq == PCI_INVALID_IRQ)
328 irq = pci_cfgintr_virgin(pe, pin);
330 if (irq == PCI_INVALID_IRQ)
333 if (pci_disable_bios_route != 0)
336 * Ask the BIOS to route the interrupt. If we picked an
337 * interrupt that failed, we should really try other
338 * choices that the BIOS offers us.
340 * For uniquely routed interrupts, we need to try
341 * to route them on some machines. Yet other machines
342 * fail to route, so we have to pretend that in that
343 * case it worked. Isn't PC hardware fun?
345 * NOTE: if we want to whack hardware to do this, then
346 * I think the right way to do that would be to have
347 * bridge drivers that do this. I'm not sure that the
348 * $PIR table would be valid for those interrupt
351 args.eax = PCIBIOS_ROUTE_INTERRUPT;
352 args.ebx = (bus << 8) | (device << 3);
353 /* pin value is 0xa - 0xd */
354 args.ecx = (irq << 8) | (0xa + pin -1);
356 bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)) &&
358 PRVERB(("pci_cfgintr: ROUTE_INTERRUPT failed.\n"));
359 return (PCI_INVALID_IRQ);
361 printf("pci_cfgintr: %d:%d INT%c routed to irq %d\n", bus,
362 device, 'A' + pin - 1, irq);
366 PRVERB(("pci_cfgintr: can't route an interrupt to %d:%d INT%c\n", bus,
367 device, 'A' + pin - 1));
368 return (PCI_INVALID_IRQ);
372 * Check to see if an existing IRQ setting is valid.
375 pci_cfgintr_valid(struct PIR_entry *pe, int pin, int irq)
379 if (!PCI_INTERRUPT_VALID(irq))
381 irqmask = pe->pe_intpin[pin - 1].irqs;
382 if (irqmask & (1 << irq)) {
383 PRVERB(("pci_cfgintr_valid: BIOS irq %d is valid\n", irq));
390 * Look to see if the routing table claims this pin is uniquely routed.
393 pci_cfgintr_unique(struct PIR_entry *pe, int pin)
398 irqmask = pe->pe_intpin[pin - 1].irqs;
399 if(irqmask != 0 && powerof2(irqmask)) {
400 irq = ffs(irqmask) - 1;
401 PRVERB(("pci_cfgintr_unique: hard-routed to irq %d\n", irq));
404 return (PCI_INVALID_IRQ);
408 * Look for another device which shares the same link byte and
409 * already has a unique IRQ, or which has had one routed already.
412 pci_cfgintr_linked(struct PIR_entry *pe, int pin)
414 struct PIR_entry *oe;
415 struct PIR_intpin *pi;
421 for (i = 0, oe = &pci_route_table->pt_entry[0]; i < pci_route_count;
423 /* scan interrupt pins */
424 for (j = 0, pi = &oe->pe_intpin[0]; j < 4; j++, pi++) {
426 /* don't look at the entry we're trying to match */
427 if ((pe == oe) && (i == (pin - 1)))
429 /* compare link bytes */
430 if (pi->link != pe->pe_intpin[pin - 1].link)
432 /* link destination mapped to a unique interrupt? */
433 if (pi->irqs != 0 && powerof2(pi->irqs)) {
434 irq = ffs(pi->irqs) - 1;
435 PRVERB(("pci_cfgintr_linked: linked (%x) to hard-routed irq %d\n",
441 * look for the real PCI device that matches this
444 irq = pci_cfgintr_search(pe, oe->pe_bus, oe->pe_device,
446 if (irq != PCI_INVALID_IRQ)
450 return (PCI_INVALID_IRQ);
454 * Scan for the real PCI device at (bus)/(device) using intpin (matchpin) and
455 * see if it has already been assigned an interrupt.
458 pci_cfgintr_search(struct PIR_entry *pe, int bus, int device, int matchpin, int pin)
460 devclass_t pci_devclass;
461 device_t *pci_devices;
463 device_t *pci_children;
465 device_t *busp, *childp;
469 * Find all the PCI busses.
472 if ((pci_devclass = devclass_find("pci")) != NULL)
473 devclass_get_devices(pci_devclass, &pci_devices, &pci_count);
476 * Scan all the PCI busses/devices looking for this one.
478 irq = PCI_INVALID_IRQ;
479 for (i = 0, busp = pci_devices; (i < pci_count) && (irq == PCI_INVALID_IRQ);
482 device_get_children(*busp, &pci_children, &pci_childcount);
484 for (j = 0, childp = pci_children; j < pci_childcount; j++,
486 if ((pci_get_bus(*childp) == bus) &&
487 (pci_get_slot(*childp) == device) &&
488 (pci_get_intpin(*childp) == matchpin)) {
489 irq = pci_i386_map_intline(pci_get_irq(*childp));
490 if (irq != PCI_INVALID_IRQ)
491 PRVERB(("pci_cfgintr_search: linked (%x) to configured irq %d at %d:%d:%d\n",
492 pe->pe_intpin[pin - 1].link, irq,
493 pci_get_bus(*childp),
494 pci_get_slot(*childp),
495 pci_get_function(*childp)));
499 if (pci_children != NULL)
500 free(pci_children, M_TEMP);
502 if (pci_devices != NULL)
503 free(pci_devices, M_TEMP);
508 * Pick a suitable IRQ from those listed as routable to this device.
511 pci_cfgintr_virgin(struct PIR_entry *pe, int pin)
516 * first scan the set of PCI-only interrupts and see if any of these
519 for (irq = 0; irq < 16; irq++) {
522 /* can we use this interrupt? */
523 if ((pci_route_table->pt_header.ph_pci_irqs & ibit) &&
524 (pe->pe_intpin[pin - 1].irqs & ibit)) {
525 PRVERB(("pci_cfgintr_virgin: using routable PCI-only interrupt %d\n", irq));
530 /* life is tough, so just pick an interrupt */
531 for (irq = 0; irq < 16; irq++) {
534 if (pe->pe_intpin[pin - 1].irqs & ibit) {
535 PRVERB(("pci_cfgintr_virgin: using routable interrupt %d\n", irq));
539 return (PCI_INVALID_IRQ);
543 pci_print_irqmask(u_int16_t irqs)
552 for (i = 0; i < 16; i++, irqs >>= 1)
563 * Dump the contents of a PCI BIOS Interrupt Routing Table to the console.
566 pci_print_route_table(struct PIR_table *ptr, int size)
568 struct PIR_entry *entry;
569 struct PIR_intpin *intpin;
572 printf("PCI-Only Interrupts: ");
573 pci_print_irqmask(ptr->pt_header.ph_pci_irqs);
574 printf("\nLocation Bus Device Pin Link IRQs\n");
575 entry = &ptr->pt_entry[0];
576 for (i = 0; i < size; i++, entry++) {
577 intpin = &entry->pe_intpin[0];
578 for (pin = 0; pin < 4; pin++, intpin++)
579 if (intpin->link != 0) {
580 if (entry->pe_slot == 0)
583 printf("slot %-3d ", entry->pe_slot);
584 printf(" %3d %3d %c 0x%02x ",
585 entry->pe_bus, entry->pe_device,
586 'A' + pin, intpin->link);
587 pci_print_irqmask(intpin->irqs);
594 * See if any interrupts for a given PCI bus are routed in the PIR. Don't
595 * even bother looking if the BIOS doesn't support routing anyways.
598 pci_probe_route_table(int bus)
603 v = pcibios_get_version();
606 for (i = 0; i < pci_route_count; i++)
607 if (pci_route_table->pt_entry[i].pe_bus == bus)
613 * Configuration space access using direct register operations
616 /* enable configuration space accesses and return data port address */
618 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
622 if (bus <= PCI_BUSMAX
624 && func <= PCI_FUNCMAX
627 && (unsigned) bytes <= 4
628 && (reg & (bytes - 1)) == 0) {
631 outl(CONF1_ADDR_PORT, (1 << 31)
632 | (bus << 16) | (slot << 11)
633 | (func << 8) | (reg & ~0x03));
634 dataport = CONF1_DATA_PORT + (reg & 0x03);
637 outb(CONF2_ENABLE_PORT, 0xf0 | (func << 1));
638 outb(CONF2_FORWARD_PORT, bus);
639 dataport = 0xc000 | (slot << 8) | reg;
646 /* disable configuration space accesses */
652 outl(CONF1_ADDR_PORT, 0);
655 outb(CONF2_ENABLE_PORT, 0);
656 outb(CONF2_FORWARD_PORT, 0);
662 pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
667 port = pci_cfgenable(bus, slot, func, reg, bytes);
686 pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
690 port = pci_cfgenable(bus, slot, func, reg, bytes);
707 /* check whether the configuration mechanism has been correctly identified */
709 pci_cfgcheck(int maxdev)
717 printf("pci_cfgcheck:\tdevice ");
719 for (device = 0; device < maxdev; device++) {
721 printf("%d ", device);
723 port = pci_cfgenable(0, device, 0, 0, 4);
725 if (id == 0 || id == 0xffffffff)
728 port = pci_cfgenable(0, device, 0, 8, 4);
729 class = inl(port) >> 8;
731 printf("[class=%06x] ", class);
732 if (class == 0 || (class & 0xf870ff) != 0)
735 port = pci_cfgenable(0, device, 0, 14, 1);
738 printf("[hdr=%02x] ", header);
739 if ((header & 0x7e) != 0)
743 printf("is there (id=%08x)\n", id);
749 printf("-- nothing found\n");
758 uint32_t mode1res,oldval1;
759 uint8_t mode2res,oldval2;
761 oldval1 = inl(CONF1_ADDR_PORT);
764 printf("pci_open(1):\tmode 1 addr port (0x0cf8) is 0x%08x\n",
768 if ((oldval1 & CONF1_ENABLE_MSK) == 0) {
773 outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK);
774 outb(CONF1_ADDR_PORT + 3, 0);
775 mode1res = inl(CONF1_ADDR_PORT);
776 outl(CONF1_ADDR_PORT, oldval1);
779 printf("pci_open(1a):\tmode1res=0x%08x (0x%08lx)\n",
780 mode1res, CONF1_ENABLE_CHK);
783 if (pci_cfgcheck(32))
787 outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK1);
788 mode1res = inl(CONF1_ADDR_PORT);
789 outl(CONF1_ADDR_PORT, oldval1);
792 printf("pci_open(1b):\tmode1res=0x%08x (0x%08lx)\n",
793 mode1res, CONF1_ENABLE_CHK1);
795 if ((mode1res & CONF1_ENABLE_MSK1) == CONF1_ENABLE_RES1) {
796 if (pci_cfgcheck(32))
801 oldval2 = inb(CONF2_ENABLE_PORT);
804 printf("pci_open(2):\tmode 2 enable port (0x0cf8) is 0x%02x\n",
808 if ((oldval2 & 0xf0) == 0) {
813 outb(CONF2_ENABLE_PORT, CONF2_ENABLE_CHK);
814 mode2res = inb(CONF2_ENABLE_PORT);
815 outb(CONF2_ENABLE_PORT, oldval2);
818 printf("pci_open(2a):\tmode2res=0x%02x (0x%02x)\n",
819 mode2res, CONF2_ENABLE_CHK);
821 if (mode2res == CONF2_ENABLE_RES) {
823 printf("pci_open(2a):\tnow trying mechanism 2\n");
825 if (pci_cfgcheck(16))