1ac19f918SStefan Eßer /* 25bec6157SStefan Eßer * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 312a02d6eSMike Smith * Copyright (c) 2000, Michael Smith <msmith@freebsd.org> 412a02d6eSMike Smith * Copyright (c) 2000, BSDi 55bec6157SStefan Eßer * All rights reserved. 65bec6157SStefan Eßer * 75bec6157SStefan Eßer * Redistribution and use in source and binary forms, with or without 85bec6157SStefan Eßer * modification, are permitted provided that the following conditions 95bec6157SStefan Eßer * are met: 105bec6157SStefan Eßer * 1. Redistributions of source code must retain the above copyright 115bec6157SStefan Eßer * notice unmodified, this list of conditions, and the following 125bec6157SStefan Eßer * disclaimer. 135bec6157SStefan Eßer * 2. Redistributions in binary form must reproduce the above copyright 145bec6157SStefan Eßer * notice, this list of conditions and the following disclaimer in the 155bec6157SStefan Eßer * documentation and/or other materials provided with the distribution. 165bec6157SStefan Eßer * 175bec6157SStefan Eßer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 185bec6157SStefan Eßer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 195bec6157SStefan Eßer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 205bec6157SStefan Eßer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 215bec6157SStefan Eßer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 225bec6157SStefan Eßer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 235bec6157SStefan Eßer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 245bec6157SStefan Eßer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255bec6157SStefan Eßer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 265bec6157SStefan Eßer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 275bec6157SStefan Eßer * 28c3aac50fSPeter Wemm * $FreeBSD$ 295bec6157SStefan Eßer * 30ac19f918SStefan Eßer */ 31ac19f918SStefan Eßer 3212a02d6eSMike Smith #include <sys/param.h> /* XXX trim includes */ 335bec6157SStefan Eßer #include <sys/systm.h> 348dc26439SPeter Wemm #include <sys/bus.h> 358dc26439SPeter Wemm #include <sys/kernel.h> 362a50a6d7SMike Smith #include <sys/module.h> 37280b4748SPeter Wemm #include <sys/malloc.h> 3854c9005fSWarner Losh #include <vm/vm.h> 3954c9005fSWarner Losh #include <vm/pmap.h> 4054c9005fSWarner Losh #include <machine/md_var.h> 415bec6157SStefan Eßer #include <pci/pcivar.h> 4285001303SMike Smith #include <pci/pcireg.h> 432a50a6d7SMike Smith #include <isa/isavar.h> 44b6c84078SPeter Wemm #include <machine/nexusvar.h> 4512a02d6eSMike Smith #include <machine/pci_cfgreg.h> 46300451c4SMike Smith #include <machine/segments.h> 47300451c4SMike Smith #include <machine/pc/bios.h> 48300451c4SMike Smith 49bb0d0a8eSMike Smith #ifdef APIC_IO 50bb0d0a8eSMike Smith #include <machine/smp.h> 51bb0d0a8eSMike Smith #endif /* APIC_IO */ 52bb0d0a8eSMike Smith 5321c3015aSDoug Rabson #include "pcib_if.h" 5421c3015aSDoug Rabson 55d626906bSWarner Losh #define PRVERB(a) printf a 56d626906bSWarner Losh 575bec6157SStefan Eßer static int cfgmech; 585bec6157SStefan Eßer static int devmax; 59300451c4SMike Smith static int usebios; 600b9427deSWarner Losh static int enable_pcibios = 0; 610b9427deSWarner Losh 620b9427deSWarner Losh TUNABLE_INT("hw.pci.enable_pcibios", &enable_pcibios); 63300451c4SMike Smith 64099d058bSMike Smith static int pci_cfgintr_unique(struct PIR_entry *pe, int pin); 65099d058bSMike Smith static int pci_cfgintr_linked(struct PIR_entry *pe, int pin); 66099d058bSMike Smith static int pci_cfgintr_search(struct PIR_entry *pe, int bus, int device, int matchpin, int pin); 67099d058bSMike Smith static int pci_cfgintr_virgin(struct PIR_entry *pe, int pin); 68099d058bSMike Smith 6912a02d6eSMike Smith static int pcibios_cfgread(int bus, int slot, int func, int reg, int bytes); 7012a02d6eSMike Smith static void pcibios_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes); 71300451c4SMike Smith static int pcibios_cfgopen(void); 7212a02d6eSMike Smith static int pcireg_cfgread(int bus, int slot, int func, int reg, int bytes); 7312a02d6eSMike Smith static void pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes); 74300451c4SMike Smith static int pcireg_cfgopen(void); 75300451c4SMike Smith 76099d058bSMike Smith static struct PIR_table *pci_route_table; 7754c9005fSWarner Losh static int pci_route_count; 7854c9005fSWarner Losh 798ce1ab3aSWarner Losh /* 808ce1ab3aSWarner Losh * Some BIOS writers seem to want to ignore the spec and put 818ce1ab3aSWarner Losh * 0 in the intline rather than 255 to indicate none. Some use 828ce1ab3aSWarner Losh * numbers in the range 128-254 to indicate something strange and 838ce1ab3aSWarner Losh * apparently undocumented anywhere. Assume these are completely bogus 848ce1ab3aSWarner Losh * and map them to 255, which means "none". 858ce1ab3aSWarner Losh */ 868ce1ab3aSWarner Losh static __inline__ int 878ce1ab3aSWarner Losh pci_i386_map_intline(int line) 888ce1ab3aSWarner Losh { 898ce1ab3aSWarner Losh if (line == 0 || line >= 128) 908ce1ab3aSWarner Losh return (255); 918ce1ab3aSWarner Losh return (line); 928ce1ab3aSWarner Losh } 938ce1ab3aSWarner Losh 94573be827SPeter Wemm int 95573be827SPeter Wemm pci_pcibios_active(void) 96573be827SPeter Wemm { 97573be827SPeter Wemm return usebios; 98573be827SPeter Wemm } 99573be827SPeter Wemm 100573be827SPeter Wemm int 101573be827SPeter Wemm pci_kill_pcibios(void) 102573be827SPeter Wemm { 103573be827SPeter Wemm usebios = 0; 104573be827SPeter Wemm return pcireg_cfgopen() != 0; 105573be827SPeter Wemm } 106573be827SPeter Wemm 107d626906bSWarner Losh static u_int16_t 108d626906bSWarner Losh pcibios_get_version(void) 109d626906bSWarner Losh { 110d626906bSWarner Losh struct bios_regs args; 111d626906bSWarner Losh 112d626906bSWarner Losh if (PCIbios.entry == 0) { 113d626906bSWarner Losh PRVERB(("pcibios: No call entry point\n")); 114d626906bSWarner Losh return (0); 115d626906bSWarner Losh } 116d626906bSWarner Losh args.eax = PCIBIOS_BIOS_PRESENT; 117d626906bSWarner Losh if (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))) { 118d626906bSWarner Losh PRVERB(("pcibios: BIOS_PRESENT call failed\n")); 119d626906bSWarner Losh return (0); 120d626906bSWarner Losh } 121d626906bSWarner Losh if (args.edx != 0x20494350) { 122d626906bSWarner Losh PRVERB(("pcibios: BIOS_PRESENT didn't return 'PCI ' in edx\n")); 123d626906bSWarner Losh return (0); 124d626906bSWarner Losh } 125d626906bSWarner Losh return (args.ebx & 0xffff); 126d626906bSWarner Losh } 127d626906bSWarner Losh 12812a02d6eSMike Smith /* 12912a02d6eSMike Smith * Initialise access to PCI configuration space 13012a02d6eSMike Smith */ 13112a02d6eSMike Smith int 13212a02d6eSMike Smith pci_cfgregopen(void) 13321c3015aSDoug Rabson { 13412a02d6eSMike Smith static int opened = 0; 13554c9005fSWarner Losh u_long sigaddr; 13654c9005fSWarner Losh static struct PIR_table *pt; 13754c9005fSWarner Losh u_int8_t ck, *cv; 13854c9005fSWarner Losh int i; 13921c3015aSDoug Rabson 14012a02d6eSMike Smith if (opened) 14112a02d6eSMike Smith return(1); 142300451c4SMike Smith 143300451c4SMike Smith if (pcibios_cfgopen() != 0) { 144300451c4SMike Smith usebios = 1; 145300451c4SMike Smith } else if (pcireg_cfgopen() != 0) { 146300451c4SMike Smith usebios = 0; 147300451c4SMike Smith } else { 148300451c4SMike Smith return(0); 149300451c4SMike Smith } 15054c9005fSWarner Losh 15154c9005fSWarner Losh /* 15254c9005fSWarner Losh * Look for the interrupt routing table. 153a8c18609SWarner Losh * 154a8c18609SWarner Losh * We use PCI BIOS's PIR table if it's available $PIR is the 155a8c18609SWarner Losh * standard way to do this. Sadly, some machines are not 156a8c18609SWarner Losh * standards conforming and have _PIR instead. We shrug and cope 157a8c18609SWarner Losh * by looking for both. 15854c9005fSWarner Losh */ 159a8c18609SWarner Losh if (pcibios_get_version() >= 0x0210 && pt == NULL) { 160a8c18609SWarner Losh sigaddr = bios_sigsearch(0, "$PIR", 4, 16, 0); 161a8c18609SWarner Losh if (sigaddr == 0) 162a8c18609SWarner Losh sigaddr = bios_sigsearch(0, "_PIR", 4, 16, 0); 163a8c18609SWarner Losh if (sigaddr != 0) { 16454c9005fSWarner Losh pt = (struct PIR_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr); 165a8c18609SWarner Losh for (cv = (u_int8_t *)pt, ck = 0, i = 0; 166a8c18609SWarner Losh i < (pt->pt_header.ph_length); i++) { 16754c9005fSWarner Losh ck += cv[i]; 16854c9005fSWarner Losh } 16954c9005fSWarner Losh if (ck == 0) { 170099d058bSMike Smith pci_route_table = pt; 171a8c18609SWarner Losh pci_route_count = (pt->pt_header.ph_length - 172a8c18609SWarner Losh sizeof(struct PIR_header)) / sizeof(struct PIR_entry); 173a8c18609SWarner Losh printf("Using $PIR table, %d entries at %p\n", 174a8c18609SWarner Losh pci_route_count, pci_route_table); 17554c9005fSWarner Losh } 17654c9005fSWarner Losh } 177a8c18609SWarner Losh } 17812a02d6eSMike Smith opened = 1; 179300451c4SMike Smith return(1); 180300451c4SMike Smith } 181300451c4SMike Smith 18212a02d6eSMike Smith /* 18312a02d6eSMike Smith * Read configuration space register 18412a02d6eSMike Smith */ 18547c6b726SPeter Wemm static u_int32_t 186bb0d0a8eSMike Smith pci_do_cfgregread(int bus, int slot, int func, int reg, int bytes) 18712a02d6eSMike Smith { 18812a02d6eSMike Smith return(usebios ? 18912a02d6eSMike Smith pcibios_cfgread(bus, slot, func, reg, bytes) : 19012a02d6eSMike Smith pcireg_cfgread(bus, slot, func, reg, bytes)); 19112a02d6eSMike Smith } 192300451c4SMike Smith 193bb0d0a8eSMike Smith u_int32_t 194bb0d0a8eSMike Smith pci_cfgregread(int bus, int slot, int func, int reg, int bytes) 195bb0d0a8eSMike Smith { 196d5ccecfaSWarner Losh uint32_t line, pin; 197bb0d0a8eSMike Smith #ifdef APIC_IO 198bb0d0a8eSMike Smith /* 199bb0d0a8eSMike Smith * If we are using the APIC, the contents of the intline register will probably 200bb0d0a8eSMike Smith * be wrong (since they are set up for use with the PIC. 201bb0d0a8eSMike Smith * Rather than rewrite these registers (maybe that would be smarter) we trap 202bb0d0a8eSMike Smith * attempts to read them and translate to our private vector numbers. 203bb0d0a8eSMike Smith */ 204bb0d0a8eSMike Smith if ((reg == PCIR_INTLINE) && (bytes == 1)) { 205bb0d0a8eSMike Smith 206bb0d0a8eSMike Smith pin = pci_do_cfgregread(bus, slot, func, PCIR_INTPIN, 1); 207bb0d0a8eSMike Smith line = pci_do_cfgregread(bus, slot, func, PCIR_INTLINE, 1); 208bb0d0a8eSMike Smith 209bb0d0a8eSMike Smith if (pin != 0) { 210bb0d0a8eSMike Smith int airq; 211bb0d0a8eSMike Smith 212bb0d0a8eSMike Smith airq = pci_apic_irq(bus, slot, pin); 213bb0d0a8eSMike Smith if (airq >= 0) { 214bb0d0a8eSMike Smith /* PCI specific entry found in MP table */ 215bb0d0a8eSMike Smith if (airq != line) 216bb0d0a8eSMike Smith undirect_pci_irq(line); 217bb0d0a8eSMike Smith return(airq); 218bb0d0a8eSMike Smith } else { 219bb0d0a8eSMike Smith /* 220bb0d0a8eSMike Smith * PCI interrupts might be redirected to the 221bb0d0a8eSMike Smith * ISA bus according to some MP tables. Use the 222bb0d0a8eSMike Smith * same methods as used by the ISA devices 223bb0d0a8eSMike Smith * devices to find the proper IOAPIC int pin. 224bb0d0a8eSMike Smith */ 225bb0d0a8eSMike Smith airq = isa_apic_irq(line); 226bb0d0a8eSMike Smith if ((airq >= 0) && (airq != line)) { 227bb0d0a8eSMike Smith /* XXX: undirect_pci_irq() ? */ 228bb0d0a8eSMike Smith undirect_isa_irq(line); 229bb0d0a8eSMike Smith return(airq); 230bb0d0a8eSMike Smith } 231bb0d0a8eSMike Smith } 232bb0d0a8eSMike Smith } 233bb0d0a8eSMike Smith return(line); 234bb0d0a8eSMike Smith } 235d5ccecfaSWarner Losh #else 236d5ccecfaSWarner Losh /* 237d5ccecfaSWarner Losh * Some BIOS writers seem to want to ignore the spec and put 238d5ccecfaSWarner Losh * 0 in the intline rather than 255 to indicate none. The rest of 239d5ccecfaSWarner Losh * the code uses 255 as an invalid IRQ. 240d5ccecfaSWarner Losh */ 241d5ccecfaSWarner Losh if (reg == PCIR_INTLINE && bytes == 1) { 242d5ccecfaSWarner Losh line = pci_do_cfgregread(bus, slot, func, PCIR_INTLINE, 1); 243d5ccecfaSWarner Losh pin = pci_do_cfgregread(bus, slot, func, PCIR_INTPIN, 1); 2448ce1ab3aSWarner Losh return pci_i386_map_intline(line); 245d5ccecfaSWarner Losh } 246bb0d0a8eSMike Smith #endif /* APIC_IO */ 247bb0d0a8eSMike Smith return(pci_do_cfgregread(bus, slot, func, reg, bytes)); 248bb0d0a8eSMike Smith } 249bb0d0a8eSMike Smith 25012a02d6eSMike Smith /* 25112a02d6eSMike Smith * Write configuration space register 25212a02d6eSMike Smith */ 25312a02d6eSMike Smith void 25412a02d6eSMike Smith pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes) 25512a02d6eSMike Smith { 25612a02d6eSMike Smith return(usebios ? 25712a02d6eSMike Smith pcibios_cfgwrite(bus, slot, func, reg, data, bytes) : 25812a02d6eSMike Smith pcireg_cfgwrite(bus, slot, func, reg, data, bytes)); 25912a02d6eSMike Smith } 26012a02d6eSMike Smith 26112a02d6eSMike Smith /* 26254c9005fSWarner Losh * Route a PCI interrupt 26354c9005fSWarner Losh * 2649d558634SMike Smith * XXX we don't do anything "right" with the function number in the PIR table 265099d058bSMike Smith * (because the consumer isn't currently passing it in). We don't care 266099d058bSMike Smith * anyway, due to the way PCI interrupts are assigned. 26754c9005fSWarner Losh */ 26854c9005fSWarner Losh int 26954c9005fSWarner Losh pci_cfgintr(int bus, int device, int pin) 27054c9005fSWarner Losh { 27154c9005fSWarner Losh struct PIR_entry *pe; 2729d558634SMike Smith int i, irq; 2739d558634SMike Smith struct bios_regs args; 274d626906bSWarner Losh u_int16_t v; 275d3b6477aSWarner Losh int already = 0; 27654c9005fSWarner Losh 277d626906bSWarner Losh v = pcibios_get_version(); 278d626906bSWarner Losh if (v < 0x0210) { 279d626906bSWarner Losh PRVERB(( 280d626906bSWarner Losh "pci_cfgintr: BIOS %x.%02x doesn't support interrupt routing\n", 281d626906bSWarner Losh (v & 0xff00) >> 8, v & 0xff)); 282d626906bSWarner Losh return (255); 283d626906bSWarner Losh } 284a3793252SWarner Losh if ((bus < 0) || (bus > 255) || (device < 0) || (device > 255) || 285a3793252SWarner Losh (pin < 1) || (pin > 4)) 28654c9005fSWarner Losh return(255); 28754c9005fSWarner Losh 28854c9005fSWarner Losh /* 28954c9005fSWarner Losh * Scan the entry table for a contender 29054c9005fSWarner Losh */ 291099d058bSMike Smith for (i = 0, pe = &pci_route_table->pt_entry[0]; i < pci_route_count; i++, pe++) { 29254c9005fSWarner Losh if ((bus != pe->pe_bus) || (device != pe->pe_device)) 29354c9005fSWarner Losh continue; 294099d058bSMike Smith 295099d058bSMike Smith irq = pci_cfgintr_linked(pe, pin); 296d3b6477aSWarner Losh if (irq == 255) 297d626906bSWarner Losh irq = pci_cfgintr_unique(pe, pin); 298654d58caSWarner Losh if (irq != 255) 299654d58caSWarner Losh already = 1; 300099d058bSMike Smith if (irq == 255) 301099d058bSMike Smith irq = pci_cfgintr_virgin(pe, pin); 302099d058bSMike Smith if (irq == 255) 30354c9005fSWarner Losh break; 304099d058bSMike Smith 3059d558634SMike Smith /* 3069d558634SMike Smith * Ask the BIOS to route the interrupt 3079d558634SMike Smith */ 3089d558634SMike Smith args.eax = PCIBIOS_ROUTE_INTERRUPT; 3099d558634SMike Smith args.ebx = (bus << 8) | (device << 3); 3109d558634SMike Smith args.ecx = (irq << 8) | (0xa + pin - 1); /* pin value is 0xa - 0xd */ 311654d58caSWarner Losh if (!already && bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))) { 312d626906bSWarner Losh /* 313d626906bSWarner Losh * XXX if it fails, we should try to smack the router 314d626906bSWarner Losh * hardware directly. 315d626906bSWarner Losh * XXX Also, there may be other choices that we can try that 316d626906bSWarner Losh * will work. 317d626906bSWarner Losh */ 318d3b6477aSWarner Losh PRVERB(("pci_cfgintr: ROUTE_INTERRUPT failed.\n")); 319d626906bSWarner Losh return(255); 320d626906bSWarner Losh } 321d3b6477aSWarner Losh printf("pci_cfgintr: %d:%d INT%c routed to irq %d\n", bus, device, 'A' + pin - 1, irq); 3229d558634SMike Smith return(irq); 32354c9005fSWarner Losh } 324099d058bSMike Smith 325d626906bSWarner Losh PRVERB(("pci_cfgintr: can't route an interrupt to %d:%d INT%c\n", bus, device, 'A' + pin - 1)); 326099d058bSMike Smith return(255); 327099d058bSMike Smith } 328099d058bSMike Smith 329099d058bSMike Smith /* 330099d058bSMike Smith * Look to see if the routing table claims this pin is uniquely routed. 331099d058bSMike Smith */ 332099d058bSMike Smith static int 333099d058bSMike Smith pci_cfgintr_unique(struct PIR_entry *pe, int pin) 334099d058bSMike Smith { 335099d058bSMike Smith int irq; 336d5ccecfaSWarner Losh uint32_t irqmask; 337099d058bSMike Smith 338d5ccecfaSWarner Losh irqmask = pe->pe_intpin[pin - 1].irqs; 339d5ccecfaSWarner Losh if (irqmask != 0 && powerof2(irqmask)) { 340d5ccecfaSWarner Losh irq = ffs(irqmask) - 1; 341d626906bSWarner Losh PRVERB(("pci_cfgintr_unique: hard-routed to irq %d\n", irq)); 342099d058bSMike Smith return(irq); 343099d058bSMike Smith } 344099d058bSMike Smith return(255); 345099d058bSMike Smith } 346099d058bSMike Smith 347099d058bSMike Smith /* 348099d058bSMike Smith * Look for another device which shares the same link byte and 349099d058bSMike Smith * already has a unique IRQ, or which has had one routed already. 350099d058bSMike Smith */ 351099d058bSMike Smith static int 352099d058bSMike Smith pci_cfgintr_linked(struct PIR_entry *pe, int pin) 353099d058bSMike Smith { 354099d058bSMike Smith struct PIR_entry *oe; 355099d058bSMike Smith struct PIR_intpin *pi; 356099d058bSMike Smith int i, j, irq; 357099d058bSMike Smith 358099d058bSMike Smith /* 359099d058bSMike Smith * Scan table slots. 360099d058bSMike Smith */ 361099d058bSMike Smith for (i = 0, oe = &pci_route_table->pt_entry[0]; i < pci_route_count; i++, oe++) { 362099d058bSMike Smith 363099d058bSMike Smith /* scan interrupt pins */ 364099d058bSMike Smith for (j = 0, pi = &oe->pe_intpin[0]; j < 4; j++, pi++) { 365099d058bSMike Smith 366099d058bSMike Smith /* don't look at the entry we're trying to match with */ 367099d058bSMike Smith if ((pe == oe) && (i == (pin - 1))) 368099d058bSMike Smith continue; 369099d058bSMike Smith 370099d058bSMike Smith /* compare link bytes */ 371099d058bSMike Smith if (pi->link != pe->pe_intpin[pin - 1].link) 372099d058bSMike Smith continue; 373099d058bSMike Smith 374099d058bSMike Smith /* link destination mapped to a unique interrupt? */ 375d5ccecfaSWarner Losh if (pi->irqs != 0 && powerof2(pi->irqs)) { 376099d058bSMike Smith irq = ffs(pi->irqs) - 1; 377d626906bSWarner Losh PRVERB(("pci_cfgintr_linked: linked (%x) to hard-routed irq %d\n", 378d626906bSWarner Losh pi->link, irq)); 379099d058bSMike Smith return(irq); 380099d058bSMike Smith } 381099d058bSMike Smith 382099d058bSMike Smith /* look for the real PCI device that matches this table entry */ 383099d058bSMike Smith if ((irq = pci_cfgintr_search(pe, oe->pe_bus, oe->pe_device, j, pin)) != 255) 384099d058bSMike Smith return(irq); 385099d058bSMike Smith } 386099d058bSMike Smith } 387099d058bSMike Smith return(255); 388099d058bSMike Smith } 389099d058bSMike Smith 390099d058bSMike Smith /* 391099d058bSMike Smith * Scan for the real PCI device at (bus)/(device) using intpin (matchpin) and 392099d058bSMike Smith * see if it has already been assigned an interrupt. 393099d058bSMike Smith */ 394099d058bSMike Smith static int 395099d058bSMike Smith pci_cfgintr_search(struct PIR_entry *pe, int bus, int device, int matchpin, int pin) 396099d058bSMike Smith { 397099d058bSMike Smith devclass_t pci_devclass; 398099d058bSMike Smith device_t *pci_devices; 399099d058bSMike Smith int pci_count; 400099d058bSMike Smith device_t *pci_children; 401099d058bSMike Smith int pci_childcount; 402099d058bSMike Smith device_t *busp, *childp; 403099d058bSMike Smith int i, j, irq; 404099d058bSMike Smith 405099d058bSMike Smith /* 406099d058bSMike Smith * Find all the PCI busses. 407099d058bSMike Smith */ 408099d058bSMike Smith pci_count = 0; 409099d058bSMike Smith if ((pci_devclass = devclass_find("pci")) != NULL) 410099d058bSMike Smith devclass_get_devices(pci_devclass, &pci_devices, &pci_count); 411099d058bSMike Smith 412099d058bSMike Smith /* 413099d058bSMike Smith * Scan all the PCI busses/devices looking for this one. 414099d058bSMike Smith */ 4156c9eb5f3SMike Smith irq = 255; 4166c9eb5f3SMike Smith for (i = 0, busp = pci_devices; (i < pci_count) && (irq == 255); i++, busp++) { 417099d058bSMike Smith pci_childcount = 0; 418099d058bSMike Smith device_get_children(*busp, &pci_children, &pci_childcount); 419099d058bSMike Smith 420099d058bSMike Smith for (j = 0, childp = pci_children; j < pci_childcount; j++, childp++) { 421099d058bSMike Smith if ((pci_get_bus(*childp) == bus) && 422099d058bSMike Smith (pci_get_slot(*childp) == device) && 4231cf5f555SWarner Losh (pci_get_intpin(*childp) == matchpin)) { 4248ce1ab3aSWarner Losh irq = pci_i386_map_intline(pci_get_irq(*childp)); 4251cf5f555SWarner Losh if (irq != 255) 426d626906bSWarner Losh PRVERB(("pci_cfgintr_search: linked (%x) to configured irq %d at %d:%d:%d\n", 42785fab963SMike Smith pe->pe_intpin[pin - 1].link, irq, 428d626906bSWarner Losh pci_get_bus(*childp), pci_get_slot(*childp), pci_get_function(*childp))); 4296c9eb5f3SMike Smith break; 4306c9eb5f3SMike Smith } 4316c9eb5f3SMike Smith } 4326c9eb5f3SMike Smith if (pci_children != NULL) 4336c9eb5f3SMike Smith free(pci_children, M_TEMP); 4346c9eb5f3SMike Smith } 4356c9eb5f3SMike Smith if (pci_devices != NULL) 4366c9eb5f3SMike Smith free(pci_devices, M_TEMP); 437099d058bSMike Smith return(irq); 438099d058bSMike Smith } 439099d058bSMike Smith 440099d058bSMike Smith /* 441099d058bSMike Smith * Pick a suitable IRQ from those listed as routable to this device. 442099d058bSMike Smith */ 443099d058bSMike Smith static int 444099d058bSMike Smith pci_cfgintr_virgin(struct PIR_entry *pe, int pin) 445099d058bSMike Smith { 446099d058bSMike Smith int irq, ibit; 447099d058bSMike Smith 448099d058bSMike Smith /* first scan the set of PCI-only interrupts and see if any of these are routable */ 449099d058bSMike Smith for (irq = 0; irq < 16; irq++) { 450099d058bSMike Smith ibit = (1 << irq); 451099d058bSMike Smith 452099d058bSMike Smith /* can we use this interrupt? */ 453099d058bSMike Smith if ((pci_route_table->pt_header.ph_pci_irqs & ibit) && 454099d058bSMike Smith (pe->pe_intpin[pin - 1].irqs & ibit)) { 455d626906bSWarner Losh PRVERB(("pci_cfgintr_virgin: using routable PCI-only interrupt %d\n", irq)); 456099d058bSMike Smith return(irq); 457099d058bSMike Smith } 458099d058bSMike Smith } 459099d058bSMike Smith 460099d058bSMike Smith /* life is tough, so just pick an interrupt */ 461099d058bSMike Smith for (irq = 0; irq < 16; irq++) { 462099d058bSMike Smith ibit = (1 << irq); 463099d058bSMike Smith 464099d058bSMike Smith if (pe->pe_intpin[pin - 1].irqs & ibit) { 465d626906bSWarner Losh PRVERB(("pci_cfgintr_virgin: using routable interrupt %d\n", irq)); 466099d058bSMike Smith return(irq); 467099d058bSMike Smith } 468099d058bSMike Smith } 46954c9005fSWarner Losh return(255); 47054c9005fSWarner Losh } 47154c9005fSWarner Losh 47254c9005fSWarner Losh 47354c9005fSWarner Losh /* 47412a02d6eSMike Smith * Config space access using BIOS functions 47512a02d6eSMike Smith */ 476300451c4SMike Smith static int 47721c3015aSDoug Rabson pcibios_cfgread(int bus, int slot, int func, int reg, int bytes) 478300451c4SMike Smith { 479300451c4SMike Smith struct bios_regs args; 480ac9b3dacSMike Smith u_int mask; 481300451c4SMike Smith 482300451c4SMike Smith switch(bytes) { 483300451c4SMike Smith case 1: 484300451c4SMike Smith args.eax = PCIBIOS_READ_CONFIG_BYTE; 485ac9b3dacSMike Smith mask = 0xff; 486300451c4SMike Smith break; 487300451c4SMike Smith case 2: 488300451c4SMike Smith args.eax = PCIBIOS_READ_CONFIG_WORD; 489ac9b3dacSMike Smith mask = 0xffff; 490300451c4SMike Smith break; 491300451c4SMike Smith case 4: 492300451c4SMike Smith args.eax = PCIBIOS_READ_CONFIG_DWORD; 493ac9b3dacSMike Smith mask = 0xffffffff; 494300451c4SMike Smith break; 495300451c4SMike Smith default: 496300451c4SMike Smith return(-1); 497300451c4SMike Smith } 49821c3015aSDoug Rabson args.ebx = (bus << 8) | (slot << 3) | func; 499300451c4SMike Smith args.edi = reg; 500300451c4SMike Smith bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)); 501300451c4SMike Smith /* check call results? */ 502ac9b3dacSMike Smith return(args.ecx & mask); 503300451c4SMike Smith } 504300451c4SMike Smith 505300451c4SMike Smith static void 50621c3015aSDoug Rabson pcibios_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes) 507300451c4SMike Smith { 508300451c4SMike Smith struct bios_regs args; 509300451c4SMike Smith 510300451c4SMike Smith switch(bytes) { 511300451c4SMike Smith case 1: 512300451c4SMike Smith args.eax = PCIBIOS_WRITE_CONFIG_BYTE; 513300451c4SMike Smith break; 514300451c4SMike Smith case 2: 515300451c4SMike Smith args.eax = PCIBIOS_WRITE_CONFIG_WORD; 516300451c4SMike Smith break; 517300451c4SMike Smith case 4: 518300451c4SMike Smith args.eax = PCIBIOS_WRITE_CONFIG_DWORD; 519300451c4SMike Smith break; 520300451c4SMike Smith default: 521300451c4SMike Smith return; 522300451c4SMike Smith } 52321c3015aSDoug Rabson args.ebx = (bus << 8) | (slot << 3) | func; 524300451c4SMike Smith args.ecx = data; 525300451c4SMike Smith args.edi = reg; 526300451c4SMike Smith bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)); 527300451c4SMike Smith } 528300451c4SMike Smith 52912a02d6eSMike Smith /* 53012a02d6eSMike Smith * Determine whether there is a PCI BIOS present 53112a02d6eSMike Smith */ 532300451c4SMike Smith static int 533300451c4SMike Smith pcibios_cfgopen(void) 534300451c4SMike Smith { 5350b9427deSWarner Losh u_int16_t v = 0; 5360b9427deSWarner Losh 5370b9427deSWarner Losh if (PCIbios.entry != 0 && enable_pcibios) { 5380b9427deSWarner Losh v = pcibios_get_version(); 5390b9427deSWarner Losh if (v > 0) 5400b9427deSWarner Losh printf("pcibios: BIOS version %x.%02x\n", (v & 0xff00) >> 8, 5410b9427deSWarner Losh v & 0xff); 5420b9427deSWarner Losh } 5430b9427deSWarner Losh return (v > 0); 544300451c4SMike Smith } 545300451c4SMike Smith 54612a02d6eSMike Smith /* 54712a02d6eSMike Smith * Configuration space access using direct register operations 54812a02d6eSMike Smith */ 549ac19f918SStefan Eßer 5505bec6157SStefan Eßer /* enable configuration space accesses and return data port address */ 551a3adc4f8SStefan Eßer static int 5525bec6157SStefan Eßer pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes) 5535bec6157SStefan Eßer { 5545bec6157SStefan Eßer int dataport = 0; 5555bec6157SStefan Eßer 5565bec6157SStefan Eßer if (bus <= PCI_BUSMAX 5575bec6157SStefan Eßer && slot < devmax 5585bec6157SStefan Eßer && func <= PCI_FUNCMAX 5595bec6157SStefan Eßer && reg <= PCI_REGMAX 5605bec6157SStefan Eßer && bytes != 3 5615bec6157SStefan Eßer && (unsigned) bytes <= 4 5625bec6157SStefan Eßer && (reg & (bytes -1)) == 0) { 5635bec6157SStefan Eßer switch (cfgmech) { 5645bec6157SStefan Eßer case 1: 565b3daa02eSStefan Eßer outl(CONF1_ADDR_PORT, (1 << 31) 566b3daa02eSStefan Eßer | (bus << 16) | (slot << 11) 567b3daa02eSStefan Eßer | (func << 8) | (reg & ~0x03)); 568b3daa02eSStefan Eßer dataport = CONF1_DATA_PORT + (reg & 0x03); 5695bec6157SStefan Eßer break; 5705bec6157SStefan Eßer case 2: 5715bec6157SStefan Eßer outb(CONF2_ENABLE_PORT, 0xf0 | (func << 1)); 5725bec6157SStefan Eßer outb(CONF2_FORWARD_PORT, bus); 5735bec6157SStefan Eßer dataport = 0xc000 | (slot << 8) | reg; 5745bec6157SStefan Eßer break; 5755bec6157SStefan Eßer } 5765bec6157SStefan Eßer } 5775bec6157SStefan Eßer return (dataport); 5785bec6157SStefan Eßer } 5795bec6157SStefan Eßer 5805bec6157SStefan Eßer /* disable configuration space accesses */ 5815bec6157SStefan Eßer static void 5825bec6157SStefan Eßer pci_cfgdisable(void) 5835bec6157SStefan Eßer { 5845bec6157SStefan Eßer switch (cfgmech) { 5855bec6157SStefan Eßer case 1: 5865bec6157SStefan Eßer outl(CONF1_ADDR_PORT, 0); 5875bec6157SStefan Eßer break; 5885bec6157SStefan Eßer case 2: 5895bec6157SStefan Eßer outb(CONF2_ENABLE_PORT, 0); 5905bec6157SStefan Eßer outb(CONF2_FORWARD_PORT, 0); 5915bec6157SStefan Eßer break; 5925bec6157SStefan Eßer } 5935bec6157SStefan Eßer } 5945bec6157SStefan Eßer 595300451c4SMike Smith static int 59621c3015aSDoug Rabson pcireg_cfgread(int bus, int slot, int func, int reg, int bytes) 5975bec6157SStefan Eßer { 5985bec6157SStefan Eßer int data = -1; 5995bec6157SStefan Eßer int port; 6005bec6157SStefan Eßer 60121c3015aSDoug Rabson port = pci_cfgenable(bus, slot, func, reg, bytes); 6025bec6157SStefan Eßer 6035bec6157SStefan Eßer if (port != 0) { 6045bec6157SStefan Eßer switch (bytes) { 6055bec6157SStefan Eßer case 1: 6065bec6157SStefan Eßer data = inb(port); 6075bec6157SStefan Eßer break; 6085bec6157SStefan Eßer case 2: 6095bec6157SStefan Eßer data = inw(port); 6105bec6157SStefan Eßer break; 6115bec6157SStefan Eßer case 4: 6125bec6157SStefan Eßer data = inl(port); 6135bec6157SStefan Eßer break; 6145bec6157SStefan Eßer } 6155bec6157SStefan Eßer pci_cfgdisable(); 6165bec6157SStefan Eßer } 6175bec6157SStefan Eßer return (data); 6185bec6157SStefan Eßer } 6195bec6157SStefan Eßer 620300451c4SMike Smith static void 62121c3015aSDoug Rabson pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes) 6225bec6157SStefan Eßer { 6235bec6157SStefan Eßer int port; 6245bec6157SStefan Eßer 62521c3015aSDoug Rabson port = pci_cfgenable(bus, slot, func, reg, bytes); 6265bec6157SStefan Eßer if (port != 0) { 6275bec6157SStefan Eßer switch (bytes) { 6285bec6157SStefan Eßer case 1: 6295bec6157SStefan Eßer outb(port, data); 6305bec6157SStefan Eßer break; 6315bec6157SStefan Eßer case 2: 6325bec6157SStefan Eßer outw(port, data); 6335bec6157SStefan Eßer break; 6345bec6157SStefan Eßer case 4: 6355bec6157SStefan Eßer outl(port, data); 6365bec6157SStefan Eßer break; 6375bec6157SStefan Eßer } 6385bec6157SStefan Eßer pci_cfgdisable(); 6395bec6157SStefan Eßer } 6405bec6157SStefan Eßer } 6415bec6157SStefan Eßer 64212a02d6eSMike Smith /* check whether the configuration mechanism has been correctly identified */ 6435bec6157SStefan Eßer static int 6445bec6157SStefan Eßer pci_cfgcheck(int maxdev) 645a3adc4f8SStefan Eßer { 646a3adc4f8SStefan Eßer u_char device; 647a3adc4f8SStefan Eßer 6485bec6157SStefan Eßer if (bootverbose) 6495bec6157SStefan Eßer printf("pci_cfgcheck:\tdevice "); 65077b57314SStefan Eßer 6515bec6157SStefan Eßer for (device = 0; device < maxdev; device++) { 6525bec6157SStefan Eßer unsigned id, class, header; 653c7483249SStefan Eßer if (bootverbose) 654c7483249SStefan Eßer printf("%d ", device); 6555bec6157SStefan Eßer 6565bec6157SStefan Eßer id = inl(pci_cfgenable(0, device, 0, 0, 4)); 6575bec6157SStefan Eßer if (id == 0 || id == -1) 65881cf5d7aSStefan Eßer continue; 65981cf5d7aSStefan Eßer 6605bec6157SStefan Eßer class = inl(pci_cfgenable(0, device, 0, 8, 4)) >> 8; 66181cf5d7aSStefan Eßer if (bootverbose) 6625bec6157SStefan Eßer printf("[class=%06x] ", class); 6638277ac25SStefan Eßer if (class == 0 || (class & 0xf870ff) != 0) 66481cf5d7aSStefan Eßer continue; 66581cf5d7aSStefan Eßer 6665bec6157SStefan Eßer header = inb(pci_cfgenable(0, device, 0, 14, 1)); 66781cf5d7aSStefan Eßer if (bootverbose) 6685bec6157SStefan Eßer printf("[hdr=%02x] ", header); 6695bec6157SStefan Eßer if ((header & 0x7e) != 0) 67081cf5d7aSStefan Eßer continue; 67181cf5d7aSStefan Eßer 6725bec6157SStefan Eßer if (bootverbose) 6735bec6157SStefan Eßer printf("is there (id=%08x)\n", id); 6745bec6157SStefan Eßer 6755bec6157SStefan Eßer pci_cfgdisable(); 6765bec6157SStefan Eßer return (1); 677a3adc4f8SStefan Eßer } 678c7483249SStefan Eßer if (bootverbose) 679c7483249SStefan Eßer printf("-- nothing found\n"); 6805bec6157SStefan Eßer 6815bec6157SStefan Eßer pci_cfgdisable(); 6825bec6157SStefan Eßer return (0); 683a3adc4f8SStefan Eßer } 684d7ea35fcSStefan Eßer 6858dc26439SPeter Wemm static int 686300451c4SMike Smith pcireg_cfgopen(void) 687ac19f918SStefan Eßer { 688287911bdSStefan Eßer unsigned long mode1res,oldval1; 689287911bdSStefan Eßer unsigned char mode2res,oldval2; 6900847c06dSStefan Eßer 691287911bdSStefan Eßer oldval1 = inl(CONF1_ADDR_PORT); 692a3adc4f8SStefan Eßer 69377b57314SStefan Eßer if (bootverbose) { 6945bec6157SStefan Eßer printf("pci_open(1):\tmode 1 addr port (0x0cf8) is 0x%08lx\n", 6955bec6157SStefan Eßer oldval1); 696a3adc4f8SStefan Eßer } 697a3adc4f8SStefan Eßer 6980e2f699bSStefan Eßer if ((oldval1 & CONF1_ENABLE_MSK) == 0) { 699287911bdSStefan Eßer 7005bec6157SStefan Eßer cfgmech = 1; 7015bec6157SStefan Eßer devmax = 32; 70277b57314SStefan Eßer 70377b57314SStefan Eßer outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK); 70477b57314SStefan Eßer outb(CONF1_ADDR_PORT +3, 0); 70577b57314SStefan Eßer mode1res = inl(CONF1_ADDR_PORT); 706287911bdSStefan Eßer outl(CONF1_ADDR_PORT, oldval1); 70777b57314SStefan Eßer 70877b57314SStefan Eßer if (bootverbose) 7095bec6157SStefan Eßer printf("pci_open(1a):\tmode1res=0x%08lx (0x%08lx)\n", 71077b57314SStefan Eßer mode1res, CONF1_ENABLE_CHK); 71177b57314SStefan Eßer 71277b57314SStefan Eßer if (mode1res) { 7135bec6157SStefan Eßer if (pci_cfgcheck(32)) 7145bec6157SStefan Eßer return (cfgmech); 7155bec6157SStefan Eßer } 71677b57314SStefan Eßer 71777b57314SStefan Eßer outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK1); 71877b57314SStefan Eßer mode1res = inl(CONF1_ADDR_PORT); 719287911bdSStefan Eßer outl(CONF1_ADDR_PORT, oldval1); 72077b57314SStefan Eßer 72177b57314SStefan Eßer if (bootverbose) 7225bec6157SStefan Eßer printf("pci_open(1b):\tmode1res=0x%08lx (0x%08lx)\n", 72377b57314SStefan Eßer mode1res, CONF1_ENABLE_CHK1); 72477b57314SStefan Eßer 725c7483249SStefan Eßer if ((mode1res & CONF1_ENABLE_MSK1) == CONF1_ENABLE_RES1) { 7265bec6157SStefan Eßer if (pci_cfgcheck(32)) 7275bec6157SStefan Eßer return (cfgmech); 728287911bdSStefan Eßer } 7295bec6157SStefan Eßer } 73077b57314SStefan Eßer 731287911bdSStefan Eßer oldval2 = inb(CONF2_ENABLE_PORT); 732287911bdSStefan Eßer 733287911bdSStefan Eßer if (bootverbose) { 7345bec6157SStefan Eßer printf("pci_open(2):\tmode 2 enable port (0x0cf8) is 0x%02x\n", 7355bec6157SStefan Eßer oldval2); 736287911bdSStefan Eßer } 737287911bdSStefan Eßer 738287911bdSStefan Eßer if ((oldval2 & 0xf0) == 0) { 739c7483249SStefan Eßer 7405bec6157SStefan Eßer cfgmech = 2; 7415bec6157SStefan Eßer devmax = 16; 74277b57314SStefan Eßer 743287911bdSStefan Eßer outb(CONF2_ENABLE_PORT, CONF2_ENABLE_CHK); 744287911bdSStefan Eßer mode2res = inb(CONF2_ENABLE_PORT); 745287911bdSStefan Eßer outb(CONF2_ENABLE_PORT, oldval2); 746287911bdSStefan Eßer 747287911bdSStefan Eßer if (bootverbose) 7485bec6157SStefan Eßer printf("pci_open(2a):\tmode2res=0x%02x (0x%02x)\n", 749287911bdSStefan Eßer mode2res, CONF2_ENABLE_CHK); 750287911bdSStefan Eßer 751287911bdSStefan Eßer if (mode2res == CONF2_ENABLE_RES) { 752287911bdSStefan Eßer if (bootverbose) 7535bec6157SStefan Eßer printf("pci_open(2a):\tnow trying mechanism 2\n"); 754287911bdSStefan Eßer 7555bec6157SStefan Eßer if (pci_cfgcheck(16)) 7565bec6157SStefan Eßer return (cfgmech); 757287911bdSStefan Eßer } 758287911bdSStefan Eßer } 75977b57314SStefan Eßer 7605bec6157SStefan Eßer cfgmech = 0; 7615bec6157SStefan Eßer devmax = 0; 7625bec6157SStefan Eßer return (cfgmech); 763ac19f918SStefan Eßer } 7648dc26439SPeter Wemm 765