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 79573be827SPeter Wemm int 80573be827SPeter Wemm pci_pcibios_active(void) 81573be827SPeter Wemm { 82573be827SPeter Wemm return usebios; 83573be827SPeter Wemm } 84573be827SPeter Wemm 85573be827SPeter Wemm int 86573be827SPeter Wemm pci_kill_pcibios(void) 87573be827SPeter Wemm { 88573be827SPeter Wemm usebios = 0; 89573be827SPeter Wemm return pcireg_cfgopen() != 0; 90573be827SPeter Wemm } 91573be827SPeter Wemm 92d626906bSWarner Losh static u_int16_t 93d626906bSWarner Losh pcibios_get_version(void) 94d626906bSWarner Losh { 95d626906bSWarner Losh struct bios_regs args; 96d626906bSWarner Losh 97d626906bSWarner Losh if (PCIbios.entry == 0) { 98d626906bSWarner Losh PRVERB(("pcibios: No call entry point\n")); 99d626906bSWarner Losh return (0); 100d626906bSWarner Losh } 101d626906bSWarner Losh args.eax = PCIBIOS_BIOS_PRESENT; 102d626906bSWarner Losh if (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))) { 103d626906bSWarner Losh PRVERB(("pcibios: BIOS_PRESENT call failed\n")); 104d626906bSWarner Losh return (0); 105d626906bSWarner Losh } 106d626906bSWarner Losh if (args.edx != 0x20494350) { 107d626906bSWarner Losh PRVERB(("pcibios: BIOS_PRESENT didn't return 'PCI ' in edx\n")); 108d626906bSWarner Losh return (0); 109d626906bSWarner Losh } 110d626906bSWarner Losh return (args.ebx & 0xffff); 111d626906bSWarner Losh } 112d626906bSWarner Losh 11312a02d6eSMike Smith /* 11412a02d6eSMike Smith * Initialise access to PCI configuration space 11512a02d6eSMike Smith */ 11612a02d6eSMike Smith int 11712a02d6eSMike Smith pci_cfgregopen(void) 11821c3015aSDoug Rabson { 11912a02d6eSMike Smith static int opened = 0; 12054c9005fSWarner Losh u_long sigaddr; 12154c9005fSWarner Losh static struct PIR_table *pt; 12254c9005fSWarner Losh u_int8_t ck, *cv; 12354c9005fSWarner Losh int i; 12421c3015aSDoug Rabson 12512a02d6eSMike Smith if (opened) 12612a02d6eSMike Smith return(1); 127300451c4SMike Smith 128300451c4SMike Smith if (pcibios_cfgopen() != 0) { 129300451c4SMike Smith usebios = 1; 130300451c4SMike Smith } else if (pcireg_cfgopen() != 0) { 131300451c4SMike Smith usebios = 0; 132300451c4SMike Smith } else { 133300451c4SMike Smith return(0); 134300451c4SMike Smith } 13554c9005fSWarner Losh 13654c9005fSWarner Losh /* 13754c9005fSWarner Losh * Look for the interrupt routing table. 13854c9005fSWarner Losh */ 139d626906bSWarner Losh /* We use PCI BIOS's PIR table if it's available */ 140d626906bSWarner Losh if (pcibios_get_version() >= 0x0210 && pt == NULL && 141d626906bSWarner Losh (sigaddr = bios_sigsearch(0, "$PIR", 4, 16, 0)) != 0) { 14254c9005fSWarner Losh pt = (struct PIR_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr); 14354c9005fSWarner Losh for (cv = (u_int8_t *)pt, ck = 0, i = 0; i < (pt->pt_header.ph_length); i++) { 14454c9005fSWarner Losh ck += cv[i]; 14554c9005fSWarner Losh } 14654c9005fSWarner Losh if (ck == 0) { 147099d058bSMike Smith pci_route_table = pt; 14854c9005fSWarner Losh pci_route_count = (pt->pt_header.ph_length - sizeof(struct PIR_header)) / sizeof(struct PIR_entry); 14954c9005fSWarner Losh printf("Using $PIR table, %d entries at %p\n", pci_route_count, pci_route_table); 15054c9005fSWarner Losh } 15154c9005fSWarner Losh } 15254c9005fSWarner Losh 15312a02d6eSMike Smith opened = 1; 154300451c4SMike Smith return(1); 155300451c4SMike Smith } 156300451c4SMike Smith 15712a02d6eSMike Smith /* 15812a02d6eSMike Smith * Read configuration space register 15912a02d6eSMike Smith */ 16047c6b726SPeter Wemm static u_int32_t 161bb0d0a8eSMike Smith pci_do_cfgregread(int bus, int slot, int func, int reg, int bytes) 16212a02d6eSMike Smith { 16312a02d6eSMike Smith return(usebios ? 16412a02d6eSMike Smith pcibios_cfgread(bus, slot, func, reg, bytes) : 16512a02d6eSMike Smith pcireg_cfgread(bus, slot, func, reg, bytes)); 16612a02d6eSMike Smith } 167300451c4SMike Smith 168bb0d0a8eSMike Smith u_int32_t 169bb0d0a8eSMike Smith pci_cfgregread(int bus, int slot, int func, int reg, int bytes) 170bb0d0a8eSMike Smith { 171bb0d0a8eSMike Smith #ifdef APIC_IO 172bb0d0a8eSMike Smith /* 173bb0d0a8eSMike Smith * If we are using the APIC, the contents of the intline register will probably 174bb0d0a8eSMike Smith * be wrong (since they are set up for use with the PIC. 175bb0d0a8eSMike Smith * Rather than rewrite these registers (maybe that would be smarter) we trap 176bb0d0a8eSMike Smith * attempts to read them and translate to our private vector numbers. 177bb0d0a8eSMike Smith */ 178bb0d0a8eSMike Smith if ((reg == PCIR_INTLINE) && (bytes == 1)) { 1796a47d852SBosko Milekic int pin, line; 180bb0d0a8eSMike Smith 181bb0d0a8eSMike Smith pin = pci_do_cfgregread(bus, slot, func, PCIR_INTPIN, 1); 182bb0d0a8eSMike Smith line = pci_do_cfgregread(bus, slot, func, PCIR_INTLINE, 1); 183bb0d0a8eSMike Smith 184bb0d0a8eSMike Smith if (pin != 0) { 185bb0d0a8eSMike Smith int airq; 186bb0d0a8eSMike Smith 187bb0d0a8eSMike Smith airq = pci_apic_irq(bus, slot, pin); 188bb0d0a8eSMike Smith if (airq >= 0) { 189bb0d0a8eSMike Smith /* PCI specific entry found in MP table */ 190bb0d0a8eSMike Smith if (airq != line) 191bb0d0a8eSMike Smith undirect_pci_irq(line); 192bb0d0a8eSMike Smith return(airq); 193bb0d0a8eSMike Smith } else { 194bb0d0a8eSMike Smith /* 195bb0d0a8eSMike Smith * PCI interrupts might be redirected to the 196bb0d0a8eSMike Smith * ISA bus according to some MP tables. Use the 197bb0d0a8eSMike Smith * same methods as used by the ISA devices 198bb0d0a8eSMike Smith * devices to find the proper IOAPIC int pin. 199bb0d0a8eSMike Smith */ 200bb0d0a8eSMike Smith airq = isa_apic_irq(line); 201bb0d0a8eSMike Smith if ((airq >= 0) && (airq != line)) { 202bb0d0a8eSMike Smith /* XXX: undirect_pci_irq() ? */ 203bb0d0a8eSMike Smith undirect_isa_irq(line); 204bb0d0a8eSMike Smith return(airq); 205bb0d0a8eSMike Smith } 206bb0d0a8eSMike Smith } 207bb0d0a8eSMike Smith } 208bb0d0a8eSMike Smith return(line); 209bb0d0a8eSMike Smith } 210bb0d0a8eSMike Smith #endif /* APIC_IO */ 211bb0d0a8eSMike Smith return(pci_do_cfgregread(bus, slot, func, reg, bytes)); 212bb0d0a8eSMike Smith } 213bb0d0a8eSMike Smith 21412a02d6eSMike Smith /* 21512a02d6eSMike Smith * Write configuration space register 21612a02d6eSMike Smith */ 21712a02d6eSMike Smith void 21812a02d6eSMike Smith pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes) 21912a02d6eSMike Smith { 22012a02d6eSMike Smith return(usebios ? 22112a02d6eSMike Smith pcibios_cfgwrite(bus, slot, func, reg, data, bytes) : 22212a02d6eSMike Smith pcireg_cfgwrite(bus, slot, func, reg, data, bytes)); 22312a02d6eSMike Smith } 22412a02d6eSMike Smith 22512a02d6eSMike Smith /* 22654c9005fSWarner Losh * Route a PCI interrupt 22754c9005fSWarner Losh * 2289d558634SMike Smith * XXX we don't do anything "right" with the function number in the PIR table 229099d058bSMike Smith * (because the consumer isn't currently passing it in). We don't care 230099d058bSMike Smith * anyway, due to the way PCI interrupts are assigned. 23154c9005fSWarner Losh */ 23254c9005fSWarner Losh int 23354c9005fSWarner Losh pci_cfgintr(int bus, int device, int pin) 23454c9005fSWarner Losh { 23554c9005fSWarner Losh struct PIR_entry *pe; 2369d558634SMike Smith int i, irq; 2379d558634SMike Smith struct bios_regs args; 238d626906bSWarner Losh u_int16_t v; 23954c9005fSWarner Losh 240d626906bSWarner Losh v = pcibios_get_version(); 241d626906bSWarner Losh if (v < 0x0210) { 242d626906bSWarner Losh PRVERB(( 243d626906bSWarner Losh "pci_cfgintr: BIOS %x.%02x doesn't support interrupt routing\n", 244d626906bSWarner Losh (v & 0xff00) >> 8, v & 0xff)); 245d626906bSWarner Losh return (255); 246d626906bSWarner Losh } 247a3793252SWarner Losh if ((bus < 0) || (bus > 255) || (device < 0) || (device > 255) || 248a3793252SWarner Losh (pin < 1) || (pin > 4)) 24954c9005fSWarner Losh return(255); 25054c9005fSWarner Losh 25154c9005fSWarner Losh /* 25254c9005fSWarner Losh * Scan the entry table for a contender 25354c9005fSWarner Losh */ 254099d058bSMike Smith for (i = 0, pe = &pci_route_table->pt_entry[0]; i < pci_route_count; i++, pe++) { 25554c9005fSWarner Losh if ((bus != pe->pe_bus) || (device != pe->pe_device)) 25654c9005fSWarner Losh continue; 257099d058bSMike Smith 258099d058bSMike Smith irq = pci_cfgintr_linked(pe, pin); 259d626906bSWarner Losh if (irq != 255) { 260d626906bSWarner Losh PRVERB(("pci_cfgintr: %d:%d INT%c already routed to irq %d\n", 261d626906bSWarner Losh bus, device, 'A' + pin - 1, irq)); 262d626906bSWarner Losh return (irq); 263d626906bSWarner Losh } 264d626906bSWarner Losh irq = pci_cfgintr_unique(pe, pin); 265099d058bSMike Smith if (irq == 255) 266099d058bSMike Smith irq = pci_cfgintr_virgin(pe, pin); 267099d058bSMike Smith 268099d058bSMike Smith if (irq == 255) 26954c9005fSWarner Losh break; 270099d058bSMike Smith 2719d558634SMike Smith 2729d558634SMike Smith /* 2739d558634SMike Smith * Ask the BIOS to route the interrupt 2749d558634SMike Smith */ 2759d558634SMike Smith args.eax = PCIBIOS_ROUTE_INTERRUPT; 2769d558634SMike Smith args.ebx = (bus << 8) | (device << 3); 2779d558634SMike Smith args.ecx = (irq << 8) | (0xa + pin - 1); /* pin value is 0xa - 0xd */ 278d626906bSWarner Losh if (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))) { 279d626906bSWarner Losh /* 280d626906bSWarner Losh * XXX if it fails, we should try to smack the router 281d626906bSWarner Losh * hardware directly. 282d626906bSWarner Losh * XXX Also, there may be other choices that we can try that 283d626906bSWarner Losh * will work. 284d626906bSWarner Losh */ 285d626906bSWarner Losh PRVERB(("pci_cfgintr: ROUTE_INTERRUPT failured.\n")); 286d626906bSWarner Losh return(255); 287d626906bSWarner Losh } 2889d558634SMike Smith 289099d058bSMike Smith /* 290099d058bSMike Smith * XXX if it fails, we should try to smack the router hardware directly 291099d058bSMike Smith */ 292d626906bSWarner Losh PRVERB(("pci_cfgintr: %d:%d INT%c routed to irq %d\n", 293d626906bSWarner Losh bus, device, 'A' + pin - 1, irq)); 2949d558634SMike Smith return(irq); 29554c9005fSWarner Losh } 296099d058bSMike Smith 297d626906bSWarner Losh PRVERB(("pci_cfgintr: can't route an interrupt to %d:%d INT%c\n", bus, device, 'A' + pin - 1)); 298099d058bSMike Smith return(255); 299099d058bSMike Smith } 300099d058bSMike Smith 301099d058bSMike Smith /* 302099d058bSMike Smith * Look to see if the routing table claims this pin is uniquely routed. 303099d058bSMike Smith */ 304099d058bSMike Smith static int 305099d058bSMike Smith pci_cfgintr_unique(struct PIR_entry *pe, int pin) 306099d058bSMike Smith { 307099d058bSMike Smith int irq; 308099d058bSMike Smith 309099d058bSMike Smith if (powerof2(pe->pe_intpin[pin - 1].irqs)) { 310099d058bSMike Smith irq = ffs(pe->pe_intpin[pin - 1].irqs) - 1; 311d626906bSWarner Losh PRVERB(("pci_cfgintr_unique: hard-routed to irq %d\n", irq)); 312099d058bSMike Smith return(irq); 313099d058bSMike Smith } 314099d058bSMike Smith return(255); 315099d058bSMike Smith } 316099d058bSMike Smith 317099d058bSMike Smith /* 318099d058bSMike Smith * Look for another device which shares the same link byte and 319099d058bSMike Smith * already has a unique IRQ, or which has had one routed already. 320099d058bSMike Smith */ 321099d058bSMike Smith static int 322099d058bSMike Smith pci_cfgintr_linked(struct PIR_entry *pe, int pin) 323099d058bSMike Smith { 324099d058bSMike Smith struct PIR_entry *oe; 325099d058bSMike Smith struct PIR_intpin *pi; 326099d058bSMike Smith int i, j, irq; 327099d058bSMike Smith 328099d058bSMike Smith /* 329099d058bSMike Smith * Scan table slots. 330099d058bSMike Smith */ 331099d058bSMike Smith for (i = 0, oe = &pci_route_table->pt_entry[0]; i < pci_route_count; i++, oe++) { 332099d058bSMike Smith 333099d058bSMike Smith /* scan interrupt pins */ 334099d058bSMike Smith for (j = 0, pi = &oe->pe_intpin[0]; j < 4; j++, pi++) { 335099d058bSMike Smith 336099d058bSMike Smith /* don't look at the entry we're trying to match with */ 337099d058bSMike Smith if ((pe == oe) && (i == (pin - 1))) 338099d058bSMike Smith continue; 339099d058bSMike Smith 340099d058bSMike Smith /* compare link bytes */ 341099d058bSMike Smith if (pi->link != pe->pe_intpin[pin - 1].link) 342099d058bSMike Smith continue; 343099d058bSMike Smith 344099d058bSMike Smith /* link destination mapped to a unique interrupt? */ 345099d058bSMike Smith if (powerof2(pi->irqs)) { 346099d058bSMike Smith irq = ffs(pi->irqs) - 1; 347d626906bSWarner Losh PRVERB(("pci_cfgintr_linked: linked (%x) to hard-routed irq %d\n", 348d626906bSWarner Losh pi->link, irq)); 349099d058bSMike Smith return(irq); 350099d058bSMike Smith } 351099d058bSMike Smith 352099d058bSMike Smith /* look for the real PCI device that matches this table entry */ 353099d058bSMike Smith if ((irq = pci_cfgintr_search(pe, oe->pe_bus, oe->pe_device, j, pin)) != 255) 354099d058bSMike Smith return(irq); 355099d058bSMike Smith } 356099d058bSMike Smith } 357099d058bSMike Smith return(255); 358099d058bSMike Smith } 359099d058bSMike Smith 360099d058bSMike Smith /* 361099d058bSMike Smith * Scan for the real PCI device at (bus)/(device) using intpin (matchpin) and 362099d058bSMike Smith * see if it has already been assigned an interrupt. 363099d058bSMike Smith */ 364099d058bSMike Smith static int 365099d058bSMike Smith pci_cfgintr_search(struct PIR_entry *pe, int bus, int device, int matchpin, int pin) 366099d058bSMike Smith { 367099d058bSMike Smith devclass_t pci_devclass; 368099d058bSMike Smith device_t *pci_devices; 369099d058bSMike Smith int pci_count; 370099d058bSMike Smith device_t *pci_children; 371099d058bSMike Smith int pci_childcount; 372099d058bSMike Smith device_t *busp, *childp; 373099d058bSMike Smith int i, j, irq; 374099d058bSMike Smith 375099d058bSMike Smith /* 376099d058bSMike Smith * Find all the PCI busses. 377099d058bSMike Smith */ 378099d058bSMike Smith pci_count = 0; 379099d058bSMike Smith if ((pci_devclass = devclass_find("pci")) != NULL) 380099d058bSMike Smith devclass_get_devices(pci_devclass, &pci_devices, &pci_count); 381099d058bSMike Smith 382099d058bSMike Smith /* 383099d058bSMike Smith * Scan all the PCI busses/devices looking for this one. 384099d058bSMike Smith */ 3856c9eb5f3SMike Smith irq = 255; 3866c9eb5f3SMike Smith for (i = 0, busp = pci_devices; (i < pci_count) && (irq == 255); i++, busp++) { 387099d058bSMike Smith pci_childcount = 0; 388099d058bSMike Smith device_get_children(*busp, &pci_children, &pci_childcount); 389099d058bSMike Smith 390099d058bSMike Smith for (j = 0, childp = pci_children; j < pci_childcount; j++, childp++) { 391099d058bSMike Smith if ((pci_get_bus(*childp) == bus) && 392099d058bSMike Smith (pci_get_slot(*childp) == device) && 393099d058bSMike Smith (pci_get_intpin(*childp) == matchpin) && 394099d058bSMike Smith ((irq = pci_get_irq(*childp)) != 255)) { 395d626906bSWarner Losh PRVERB(("pci_cfgintr_search: linked (%x) to configured irq %d at %d:%d:%d\n", 39685fab963SMike Smith pe->pe_intpin[pin - 1].link, irq, 397d626906bSWarner Losh pci_get_bus(*childp), pci_get_slot(*childp), pci_get_function(*childp))); 3986c9eb5f3SMike Smith break; 3996c9eb5f3SMike Smith } 4006c9eb5f3SMike Smith } 4016c9eb5f3SMike Smith if (pci_children != NULL) 4026c9eb5f3SMike Smith free(pci_children, M_TEMP); 4036c9eb5f3SMike Smith } 4046c9eb5f3SMike Smith if (pci_devices != NULL) 4056c9eb5f3SMike Smith free(pci_devices, M_TEMP); 406099d058bSMike Smith return(irq); 407099d058bSMike Smith } 408099d058bSMike Smith 409099d058bSMike Smith /* 410099d058bSMike Smith * Pick a suitable IRQ from those listed as routable to this device. 411099d058bSMike Smith */ 412099d058bSMike Smith static int 413099d058bSMike Smith pci_cfgintr_virgin(struct PIR_entry *pe, int pin) 414099d058bSMike Smith { 415099d058bSMike Smith int irq, ibit; 416099d058bSMike Smith 417099d058bSMike Smith /* first scan the set of PCI-only interrupts and see if any of these are routable */ 418099d058bSMike Smith for (irq = 0; irq < 16; irq++) { 419099d058bSMike Smith ibit = (1 << irq); 420099d058bSMike Smith 421099d058bSMike Smith /* can we use this interrupt? */ 422099d058bSMike Smith if ((pci_route_table->pt_header.ph_pci_irqs & ibit) && 423099d058bSMike Smith (pe->pe_intpin[pin - 1].irqs & ibit)) { 424d626906bSWarner Losh PRVERB(("pci_cfgintr_virgin: using routable PCI-only interrupt %d\n", irq)); 425099d058bSMike Smith return(irq); 426099d058bSMike Smith } 427099d058bSMike Smith } 428099d058bSMike Smith 429099d058bSMike Smith /* life is tough, so just pick an interrupt */ 430099d058bSMike Smith for (irq = 0; irq < 16; irq++) { 431099d058bSMike Smith ibit = (1 << irq); 432099d058bSMike Smith 433099d058bSMike Smith if (pe->pe_intpin[pin - 1].irqs & ibit) { 434d626906bSWarner Losh PRVERB(("pci_cfgintr_virgin: using routable interrupt %d\n", irq)); 435099d058bSMike Smith return(irq); 436099d058bSMike Smith } 437099d058bSMike Smith } 43854c9005fSWarner Losh return(255); 43954c9005fSWarner Losh } 44054c9005fSWarner Losh 44154c9005fSWarner Losh 44254c9005fSWarner Losh /* 44312a02d6eSMike Smith * Config space access using BIOS functions 44412a02d6eSMike Smith */ 445300451c4SMike Smith static int 44621c3015aSDoug Rabson pcibios_cfgread(int bus, int slot, int func, int reg, int bytes) 447300451c4SMike Smith { 448300451c4SMike Smith struct bios_regs args; 449ac9b3dacSMike Smith u_int mask; 450300451c4SMike Smith 451300451c4SMike Smith switch(bytes) { 452300451c4SMike Smith case 1: 453300451c4SMike Smith args.eax = PCIBIOS_READ_CONFIG_BYTE; 454ac9b3dacSMike Smith mask = 0xff; 455300451c4SMike Smith break; 456300451c4SMike Smith case 2: 457300451c4SMike Smith args.eax = PCIBIOS_READ_CONFIG_WORD; 458ac9b3dacSMike Smith mask = 0xffff; 459300451c4SMike Smith break; 460300451c4SMike Smith case 4: 461300451c4SMike Smith args.eax = PCIBIOS_READ_CONFIG_DWORD; 462ac9b3dacSMike Smith mask = 0xffffffff; 463300451c4SMike Smith break; 464300451c4SMike Smith default: 465300451c4SMike Smith return(-1); 466300451c4SMike Smith } 46721c3015aSDoug Rabson args.ebx = (bus << 8) | (slot << 3) | func; 468300451c4SMike Smith args.edi = reg; 469300451c4SMike Smith bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)); 470300451c4SMike Smith /* check call results? */ 471ac9b3dacSMike Smith return(args.ecx & mask); 472300451c4SMike Smith } 473300451c4SMike Smith 474300451c4SMike Smith static void 47521c3015aSDoug Rabson pcibios_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes) 476300451c4SMike Smith { 477300451c4SMike Smith struct bios_regs args; 478300451c4SMike Smith 479300451c4SMike Smith switch(bytes) { 480300451c4SMike Smith case 1: 481300451c4SMike Smith args.eax = PCIBIOS_WRITE_CONFIG_BYTE; 482300451c4SMike Smith break; 483300451c4SMike Smith case 2: 484300451c4SMike Smith args.eax = PCIBIOS_WRITE_CONFIG_WORD; 485300451c4SMike Smith break; 486300451c4SMike Smith case 4: 487300451c4SMike Smith args.eax = PCIBIOS_WRITE_CONFIG_DWORD; 488300451c4SMike Smith break; 489300451c4SMike Smith default: 490300451c4SMike Smith return; 491300451c4SMike Smith } 49221c3015aSDoug Rabson args.ebx = (bus << 8) | (slot << 3) | func; 493300451c4SMike Smith args.ecx = data; 494300451c4SMike Smith args.edi = reg; 495300451c4SMike Smith bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)); 496300451c4SMike Smith } 497300451c4SMike Smith 49812a02d6eSMike Smith /* 49912a02d6eSMike Smith * Determine whether there is a PCI BIOS present 50012a02d6eSMike Smith */ 501300451c4SMike Smith static int 502300451c4SMike Smith pcibios_cfgopen(void) 503300451c4SMike Smith { 5040b9427deSWarner Losh u_int16_t v = 0; 5050b9427deSWarner Losh 5060b9427deSWarner Losh if (PCIbios.entry != 0 && enable_pcibios) { 5070b9427deSWarner Losh v = pcibios_get_version(); 5080b9427deSWarner Losh if (v > 0) 5090b9427deSWarner Losh printf("pcibios: BIOS version %x.%02x\n", (v & 0xff00) >> 8, 5100b9427deSWarner Losh v & 0xff); 5110b9427deSWarner Losh } 5120b9427deSWarner Losh return (v > 0); 513300451c4SMike Smith } 514300451c4SMike Smith 51512a02d6eSMike Smith /* 51612a02d6eSMike Smith * Configuration space access using direct register operations 51712a02d6eSMike Smith */ 518ac19f918SStefan Eßer 5195bec6157SStefan Eßer /* enable configuration space accesses and return data port address */ 520a3adc4f8SStefan Eßer static int 5215bec6157SStefan Eßer pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes) 5225bec6157SStefan Eßer { 5235bec6157SStefan Eßer int dataport = 0; 5245bec6157SStefan Eßer 5255bec6157SStefan Eßer if (bus <= PCI_BUSMAX 5265bec6157SStefan Eßer && slot < devmax 5275bec6157SStefan Eßer && func <= PCI_FUNCMAX 5285bec6157SStefan Eßer && reg <= PCI_REGMAX 5295bec6157SStefan Eßer && bytes != 3 5305bec6157SStefan Eßer && (unsigned) bytes <= 4 5315bec6157SStefan Eßer && (reg & (bytes -1)) == 0) { 5325bec6157SStefan Eßer switch (cfgmech) { 5335bec6157SStefan Eßer case 1: 534b3daa02eSStefan Eßer outl(CONF1_ADDR_PORT, (1 << 31) 535b3daa02eSStefan Eßer | (bus << 16) | (slot << 11) 536b3daa02eSStefan Eßer | (func << 8) | (reg & ~0x03)); 537b3daa02eSStefan Eßer dataport = CONF1_DATA_PORT + (reg & 0x03); 5385bec6157SStefan Eßer break; 5395bec6157SStefan Eßer case 2: 5405bec6157SStefan Eßer outb(CONF2_ENABLE_PORT, 0xf0 | (func << 1)); 5415bec6157SStefan Eßer outb(CONF2_FORWARD_PORT, bus); 5425bec6157SStefan Eßer dataport = 0xc000 | (slot << 8) | reg; 5435bec6157SStefan Eßer break; 5445bec6157SStefan Eßer } 5455bec6157SStefan Eßer } 5465bec6157SStefan Eßer return (dataport); 5475bec6157SStefan Eßer } 5485bec6157SStefan Eßer 5495bec6157SStefan Eßer /* disable configuration space accesses */ 5505bec6157SStefan Eßer static void 5515bec6157SStefan Eßer pci_cfgdisable(void) 5525bec6157SStefan Eßer { 5535bec6157SStefan Eßer switch (cfgmech) { 5545bec6157SStefan Eßer case 1: 5555bec6157SStefan Eßer outl(CONF1_ADDR_PORT, 0); 5565bec6157SStefan Eßer break; 5575bec6157SStefan Eßer case 2: 5585bec6157SStefan Eßer outb(CONF2_ENABLE_PORT, 0); 5595bec6157SStefan Eßer outb(CONF2_FORWARD_PORT, 0); 5605bec6157SStefan Eßer break; 5615bec6157SStefan Eßer } 5625bec6157SStefan Eßer } 5635bec6157SStefan Eßer 564300451c4SMike Smith static int 56521c3015aSDoug Rabson pcireg_cfgread(int bus, int slot, int func, int reg, int bytes) 5665bec6157SStefan Eßer { 5675bec6157SStefan Eßer int data = -1; 5685bec6157SStefan Eßer int port; 5695bec6157SStefan Eßer 57021c3015aSDoug Rabson port = pci_cfgenable(bus, slot, func, reg, bytes); 5715bec6157SStefan Eßer 5725bec6157SStefan Eßer if (port != 0) { 5735bec6157SStefan Eßer switch (bytes) { 5745bec6157SStefan Eßer case 1: 5755bec6157SStefan Eßer data = inb(port); 5765bec6157SStefan Eßer break; 5775bec6157SStefan Eßer case 2: 5785bec6157SStefan Eßer data = inw(port); 5795bec6157SStefan Eßer break; 5805bec6157SStefan Eßer case 4: 5815bec6157SStefan Eßer data = inl(port); 5825bec6157SStefan Eßer break; 5835bec6157SStefan Eßer } 5845bec6157SStefan Eßer pci_cfgdisable(); 5855bec6157SStefan Eßer } 5865bec6157SStefan Eßer return (data); 5875bec6157SStefan Eßer } 5885bec6157SStefan Eßer 589300451c4SMike Smith static void 59021c3015aSDoug Rabson pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes) 5915bec6157SStefan Eßer { 5925bec6157SStefan Eßer int port; 5935bec6157SStefan Eßer 59421c3015aSDoug Rabson port = pci_cfgenable(bus, slot, func, reg, bytes); 5955bec6157SStefan Eßer if (port != 0) { 5965bec6157SStefan Eßer switch (bytes) { 5975bec6157SStefan Eßer case 1: 5985bec6157SStefan Eßer outb(port, data); 5995bec6157SStefan Eßer break; 6005bec6157SStefan Eßer case 2: 6015bec6157SStefan Eßer outw(port, data); 6025bec6157SStefan Eßer break; 6035bec6157SStefan Eßer case 4: 6045bec6157SStefan Eßer outl(port, data); 6055bec6157SStefan Eßer break; 6065bec6157SStefan Eßer } 6075bec6157SStefan Eßer pci_cfgdisable(); 6085bec6157SStefan Eßer } 6095bec6157SStefan Eßer } 6105bec6157SStefan Eßer 61112a02d6eSMike Smith /* check whether the configuration mechanism has been correctly identified */ 6125bec6157SStefan Eßer static int 6135bec6157SStefan Eßer pci_cfgcheck(int maxdev) 614a3adc4f8SStefan Eßer { 615a3adc4f8SStefan Eßer u_char device; 616a3adc4f8SStefan Eßer 6175bec6157SStefan Eßer if (bootverbose) 6185bec6157SStefan Eßer printf("pci_cfgcheck:\tdevice "); 61977b57314SStefan Eßer 6205bec6157SStefan Eßer for (device = 0; device < maxdev; device++) { 6215bec6157SStefan Eßer unsigned id, class, header; 622c7483249SStefan Eßer if (bootverbose) 623c7483249SStefan Eßer printf("%d ", device); 6245bec6157SStefan Eßer 6255bec6157SStefan Eßer id = inl(pci_cfgenable(0, device, 0, 0, 4)); 6265bec6157SStefan Eßer if (id == 0 || id == -1) 62781cf5d7aSStefan Eßer continue; 62881cf5d7aSStefan Eßer 6295bec6157SStefan Eßer class = inl(pci_cfgenable(0, device, 0, 8, 4)) >> 8; 63081cf5d7aSStefan Eßer if (bootverbose) 6315bec6157SStefan Eßer printf("[class=%06x] ", class); 6328277ac25SStefan Eßer if (class == 0 || (class & 0xf870ff) != 0) 63381cf5d7aSStefan Eßer continue; 63481cf5d7aSStefan Eßer 6355bec6157SStefan Eßer header = inb(pci_cfgenable(0, device, 0, 14, 1)); 63681cf5d7aSStefan Eßer if (bootverbose) 6375bec6157SStefan Eßer printf("[hdr=%02x] ", header); 6385bec6157SStefan Eßer if ((header & 0x7e) != 0) 63981cf5d7aSStefan Eßer continue; 64081cf5d7aSStefan Eßer 6415bec6157SStefan Eßer if (bootverbose) 6425bec6157SStefan Eßer printf("is there (id=%08x)\n", id); 6435bec6157SStefan Eßer 6445bec6157SStefan Eßer pci_cfgdisable(); 6455bec6157SStefan Eßer return (1); 646a3adc4f8SStefan Eßer } 647c7483249SStefan Eßer if (bootverbose) 648c7483249SStefan Eßer printf("-- nothing found\n"); 6495bec6157SStefan Eßer 6505bec6157SStefan Eßer pci_cfgdisable(); 6515bec6157SStefan Eßer return (0); 652a3adc4f8SStefan Eßer } 653d7ea35fcSStefan Eßer 6548dc26439SPeter Wemm static int 655300451c4SMike Smith pcireg_cfgopen(void) 656ac19f918SStefan Eßer { 657287911bdSStefan Eßer unsigned long mode1res,oldval1; 658287911bdSStefan Eßer unsigned char mode2res,oldval2; 6590847c06dSStefan Eßer 660287911bdSStefan Eßer oldval1 = inl(CONF1_ADDR_PORT); 661a3adc4f8SStefan Eßer 66277b57314SStefan Eßer if (bootverbose) { 6635bec6157SStefan Eßer printf("pci_open(1):\tmode 1 addr port (0x0cf8) is 0x%08lx\n", 6645bec6157SStefan Eßer oldval1); 665a3adc4f8SStefan Eßer } 666a3adc4f8SStefan Eßer 6670e2f699bSStefan Eßer if ((oldval1 & CONF1_ENABLE_MSK) == 0) { 668287911bdSStefan Eßer 6695bec6157SStefan Eßer cfgmech = 1; 6705bec6157SStefan Eßer devmax = 32; 67177b57314SStefan Eßer 67277b57314SStefan Eßer outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK); 67377b57314SStefan Eßer outb(CONF1_ADDR_PORT +3, 0); 67477b57314SStefan Eßer mode1res = inl(CONF1_ADDR_PORT); 675287911bdSStefan Eßer outl(CONF1_ADDR_PORT, oldval1); 67677b57314SStefan Eßer 67777b57314SStefan Eßer if (bootverbose) 6785bec6157SStefan Eßer printf("pci_open(1a):\tmode1res=0x%08lx (0x%08lx)\n", 67977b57314SStefan Eßer mode1res, CONF1_ENABLE_CHK); 68077b57314SStefan Eßer 68177b57314SStefan Eßer if (mode1res) { 6825bec6157SStefan Eßer if (pci_cfgcheck(32)) 6835bec6157SStefan Eßer return (cfgmech); 6845bec6157SStefan Eßer } 68577b57314SStefan Eßer 68677b57314SStefan Eßer outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK1); 68777b57314SStefan Eßer mode1res = inl(CONF1_ADDR_PORT); 688287911bdSStefan Eßer outl(CONF1_ADDR_PORT, oldval1); 68977b57314SStefan Eßer 69077b57314SStefan Eßer if (bootverbose) 6915bec6157SStefan Eßer printf("pci_open(1b):\tmode1res=0x%08lx (0x%08lx)\n", 69277b57314SStefan Eßer mode1res, CONF1_ENABLE_CHK1); 69377b57314SStefan Eßer 694c7483249SStefan Eßer if ((mode1res & CONF1_ENABLE_MSK1) == CONF1_ENABLE_RES1) { 6955bec6157SStefan Eßer if (pci_cfgcheck(32)) 6965bec6157SStefan Eßer return (cfgmech); 697287911bdSStefan Eßer } 6985bec6157SStefan Eßer } 69977b57314SStefan Eßer 700287911bdSStefan Eßer oldval2 = inb(CONF2_ENABLE_PORT); 701287911bdSStefan Eßer 702287911bdSStefan Eßer if (bootverbose) { 7035bec6157SStefan Eßer printf("pci_open(2):\tmode 2 enable port (0x0cf8) is 0x%02x\n", 7045bec6157SStefan Eßer oldval2); 705287911bdSStefan Eßer } 706287911bdSStefan Eßer 707287911bdSStefan Eßer if ((oldval2 & 0xf0) == 0) { 708c7483249SStefan Eßer 7095bec6157SStefan Eßer cfgmech = 2; 7105bec6157SStefan Eßer devmax = 16; 71177b57314SStefan Eßer 712287911bdSStefan Eßer outb(CONF2_ENABLE_PORT, CONF2_ENABLE_CHK); 713287911bdSStefan Eßer mode2res = inb(CONF2_ENABLE_PORT); 714287911bdSStefan Eßer outb(CONF2_ENABLE_PORT, oldval2); 715287911bdSStefan Eßer 716287911bdSStefan Eßer if (bootverbose) 7175bec6157SStefan Eßer printf("pci_open(2a):\tmode2res=0x%02x (0x%02x)\n", 718287911bdSStefan Eßer mode2res, CONF2_ENABLE_CHK); 719287911bdSStefan Eßer 720287911bdSStefan Eßer if (mode2res == CONF2_ENABLE_RES) { 721287911bdSStefan Eßer if (bootverbose) 7225bec6157SStefan Eßer printf("pci_open(2a):\tnow trying mechanism 2\n"); 723287911bdSStefan Eßer 7245bec6157SStefan Eßer if (pci_cfgcheck(16)) 7255bec6157SStefan Eßer return (cfgmech); 726287911bdSStefan Eßer } 727287911bdSStefan Eßer } 72877b57314SStefan Eßer 7295bec6157SStefan Eßer cfgmech = 0; 7305bec6157SStefan Eßer devmax = 0; 7315bec6157SStefan Eßer return (cfgmech); 732ac19f918SStefan Eßer } 7338dc26439SPeter Wemm 734