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. 138a8c18609SWarner Losh * 139a8c18609SWarner Losh * We use PCI BIOS's PIR table if it's available $PIR is the 140a8c18609SWarner Losh * standard way to do this. Sadly, some machines are not 141a8c18609SWarner Losh * standards conforming and have _PIR instead. We shrug and cope 142a8c18609SWarner Losh * by looking for both. 14354c9005fSWarner Losh */ 144a8c18609SWarner Losh if (pcibios_get_version() >= 0x0210 && pt == NULL) { 145a8c18609SWarner Losh sigaddr = bios_sigsearch(0, "$PIR", 4, 16, 0); 146a8c18609SWarner Losh if (sigaddr == 0) 147a8c18609SWarner Losh sigaddr = bios_sigsearch(0, "_PIR", 4, 16, 0); 148a8c18609SWarner Losh if (sigaddr != 0) { 14954c9005fSWarner Losh pt = (struct PIR_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr); 150a8c18609SWarner Losh for (cv = (u_int8_t *)pt, ck = 0, i = 0; 151a8c18609SWarner Losh i < (pt->pt_header.ph_length); i++) { 15254c9005fSWarner Losh ck += cv[i]; 15354c9005fSWarner Losh } 15454c9005fSWarner Losh if (ck == 0) { 155099d058bSMike Smith pci_route_table = pt; 156a8c18609SWarner Losh pci_route_count = (pt->pt_header.ph_length - 157a8c18609SWarner Losh sizeof(struct PIR_header)) / sizeof(struct PIR_entry); 158a8c18609SWarner Losh printf("Using $PIR table, %d entries at %p\n", 159a8c18609SWarner Losh pci_route_count, pci_route_table); 16054c9005fSWarner Losh } 16154c9005fSWarner Losh } 162a8c18609SWarner Losh } 16312a02d6eSMike Smith opened = 1; 164300451c4SMike Smith return(1); 165300451c4SMike Smith } 166300451c4SMike Smith 16712a02d6eSMike Smith /* 16812a02d6eSMike Smith * Read configuration space register 16912a02d6eSMike Smith */ 17047c6b726SPeter Wemm static u_int32_t 171bb0d0a8eSMike Smith pci_do_cfgregread(int bus, int slot, int func, int reg, int bytes) 17212a02d6eSMike Smith { 17312a02d6eSMike Smith return(usebios ? 17412a02d6eSMike Smith pcibios_cfgread(bus, slot, func, reg, bytes) : 17512a02d6eSMike Smith pcireg_cfgread(bus, slot, func, reg, bytes)); 17612a02d6eSMike Smith } 177300451c4SMike Smith 178bb0d0a8eSMike Smith u_int32_t 179bb0d0a8eSMike Smith pci_cfgregread(int bus, int slot, int func, int reg, int bytes) 180bb0d0a8eSMike Smith { 181d5ccecfaSWarner Losh uint32_t line, pin; 182bb0d0a8eSMike Smith #ifdef APIC_IO 183bb0d0a8eSMike Smith /* 184bb0d0a8eSMike Smith * If we are using the APIC, the contents of the intline register will probably 185bb0d0a8eSMike Smith * be wrong (since they are set up for use with the PIC. 186bb0d0a8eSMike Smith * Rather than rewrite these registers (maybe that would be smarter) we trap 187bb0d0a8eSMike Smith * attempts to read them and translate to our private vector numbers. 188bb0d0a8eSMike Smith */ 189bb0d0a8eSMike Smith if ((reg == PCIR_INTLINE) && (bytes == 1)) { 190bb0d0a8eSMike Smith 191bb0d0a8eSMike Smith pin = pci_do_cfgregread(bus, slot, func, PCIR_INTPIN, 1); 192bb0d0a8eSMike Smith line = pci_do_cfgregread(bus, slot, func, PCIR_INTLINE, 1); 193bb0d0a8eSMike Smith 194bb0d0a8eSMike Smith if (pin != 0) { 195bb0d0a8eSMike Smith int airq; 196bb0d0a8eSMike Smith 197bb0d0a8eSMike Smith airq = pci_apic_irq(bus, slot, pin); 198bb0d0a8eSMike Smith if (airq >= 0) { 199bb0d0a8eSMike Smith /* PCI specific entry found in MP table */ 200bb0d0a8eSMike Smith if (airq != line) 201bb0d0a8eSMike Smith undirect_pci_irq(line); 202bb0d0a8eSMike Smith return(airq); 203bb0d0a8eSMike Smith } else { 204bb0d0a8eSMike Smith /* 205bb0d0a8eSMike Smith * PCI interrupts might be redirected to the 206bb0d0a8eSMike Smith * ISA bus according to some MP tables. Use the 207bb0d0a8eSMike Smith * same methods as used by the ISA devices 208bb0d0a8eSMike Smith * devices to find the proper IOAPIC int pin. 209bb0d0a8eSMike Smith */ 210bb0d0a8eSMike Smith airq = isa_apic_irq(line); 211bb0d0a8eSMike Smith if ((airq >= 0) && (airq != line)) { 212bb0d0a8eSMike Smith /* XXX: undirect_pci_irq() ? */ 213bb0d0a8eSMike Smith undirect_isa_irq(line); 214bb0d0a8eSMike Smith return(airq); 215bb0d0a8eSMike Smith } 216bb0d0a8eSMike Smith } 217bb0d0a8eSMike Smith } 218bb0d0a8eSMike Smith return(line); 219bb0d0a8eSMike Smith } 220d5ccecfaSWarner Losh #else 221d5ccecfaSWarner Losh /* 222d5ccecfaSWarner Losh * Some BIOS writers seem to want to ignore the spec and put 223d5ccecfaSWarner Losh * 0 in the intline rather than 255 to indicate none. The rest of 224d5ccecfaSWarner Losh * the code uses 255 as an invalid IRQ. 225d5ccecfaSWarner Losh */ 226d5ccecfaSWarner Losh if (reg == PCIR_INTLINE && bytes == 1) { 227d5ccecfaSWarner Losh line = pci_do_cfgregread(bus, slot, func, PCIR_INTLINE, 1); 228d5ccecfaSWarner Losh pin = pci_do_cfgregread(bus, slot, func, PCIR_INTPIN, 1); 229d5ccecfaSWarner Losh if (pin != 0 && (line == 0 || line >= 128)) 230d5ccecfaSWarner Losh return (255); 231d5ccecfaSWarner Losh } 232bb0d0a8eSMike Smith #endif /* APIC_IO */ 233bb0d0a8eSMike Smith return(pci_do_cfgregread(bus, slot, func, reg, bytes)); 234bb0d0a8eSMike Smith } 235bb0d0a8eSMike Smith 23612a02d6eSMike Smith /* 23712a02d6eSMike Smith * Write configuration space register 23812a02d6eSMike Smith */ 23912a02d6eSMike Smith void 24012a02d6eSMike Smith pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes) 24112a02d6eSMike Smith { 24212a02d6eSMike Smith return(usebios ? 24312a02d6eSMike Smith pcibios_cfgwrite(bus, slot, func, reg, data, bytes) : 24412a02d6eSMike Smith pcireg_cfgwrite(bus, slot, func, reg, data, bytes)); 24512a02d6eSMike Smith } 24612a02d6eSMike Smith 24712a02d6eSMike Smith /* 24854c9005fSWarner Losh * Route a PCI interrupt 24954c9005fSWarner Losh * 2509d558634SMike Smith * XXX we don't do anything "right" with the function number in the PIR table 251099d058bSMike Smith * (because the consumer isn't currently passing it in). We don't care 252099d058bSMike Smith * anyway, due to the way PCI interrupts are assigned. 25354c9005fSWarner Losh */ 25454c9005fSWarner Losh int 25554c9005fSWarner Losh pci_cfgintr(int bus, int device, int pin) 25654c9005fSWarner Losh { 25754c9005fSWarner Losh struct PIR_entry *pe; 2589d558634SMike Smith int i, irq; 2599d558634SMike Smith struct bios_regs args; 260d626906bSWarner Losh u_int16_t v; 261d3b6477aSWarner Losh int already = 0; 26254c9005fSWarner Losh 263d626906bSWarner Losh v = pcibios_get_version(); 264d626906bSWarner Losh if (v < 0x0210) { 265d626906bSWarner Losh PRVERB(( 266d626906bSWarner Losh "pci_cfgintr: BIOS %x.%02x doesn't support interrupt routing\n", 267d626906bSWarner Losh (v & 0xff00) >> 8, v & 0xff)); 268d626906bSWarner Losh return (255); 269d626906bSWarner Losh } 270a3793252SWarner Losh if ((bus < 0) || (bus > 255) || (device < 0) || (device > 255) || 271a3793252SWarner Losh (pin < 1) || (pin > 4)) 27254c9005fSWarner Losh return(255); 27354c9005fSWarner Losh 27454c9005fSWarner Losh /* 27554c9005fSWarner Losh * Scan the entry table for a contender 27654c9005fSWarner Losh */ 277099d058bSMike Smith for (i = 0, pe = &pci_route_table->pt_entry[0]; i < pci_route_count; i++, pe++) { 27854c9005fSWarner Losh if ((bus != pe->pe_bus) || (device != pe->pe_device)) 27954c9005fSWarner Losh continue; 280099d058bSMike Smith 281099d058bSMike Smith irq = pci_cfgintr_linked(pe, pin); 282d3b6477aSWarner Losh if (irq == 255) 283d626906bSWarner Losh irq = pci_cfgintr_unique(pe, pin); 284654d58caSWarner Losh if (irq != 255) 285654d58caSWarner Losh already = 1; 286099d058bSMike Smith if (irq == 255) 287099d058bSMike Smith irq = pci_cfgintr_virgin(pe, pin); 288099d058bSMike Smith if (irq == 255) 28954c9005fSWarner Losh break; 290099d058bSMike Smith 2919d558634SMike Smith /* 2929d558634SMike Smith * Ask the BIOS to route the interrupt 2939d558634SMike Smith */ 2949d558634SMike Smith args.eax = PCIBIOS_ROUTE_INTERRUPT; 2959d558634SMike Smith args.ebx = (bus << 8) | (device << 3); 2969d558634SMike Smith args.ecx = (irq << 8) | (0xa + pin - 1); /* pin value is 0xa - 0xd */ 297654d58caSWarner Losh if (!already && bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))) { 298d626906bSWarner Losh /* 299d626906bSWarner Losh * XXX if it fails, we should try to smack the router 300d626906bSWarner Losh * hardware directly. 301d626906bSWarner Losh * XXX Also, there may be other choices that we can try that 302d626906bSWarner Losh * will work. 303d626906bSWarner Losh */ 304d3b6477aSWarner Losh PRVERB(("pci_cfgintr: ROUTE_INTERRUPT failed.\n")); 305d626906bSWarner Losh return(255); 306d626906bSWarner Losh } 307d3b6477aSWarner Losh printf("pci_cfgintr: %d:%d INT%c routed to irq %d\n", bus, device, 'A' + pin - 1, irq); 3089d558634SMike Smith return(irq); 30954c9005fSWarner Losh } 310099d058bSMike Smith 311d626906bSWarner Losh PRVERB(("pci_cfgintr: can't route an interrupt to %d:%d INT%c\n", bus, device, 'A' + pin - 1)); 312099d058bSMike Smith return(255); 313099d058bSMike Smith } 314099d058bSMike Smith 315099d058bSMike Smith /* 316099d058bSMike Smith * Look to see if the routing table claims this pin is uniquely routed. 317099d058bSMike Smith */ 318099d058bSMike Smith static int 319099d058bSMike Smith pci_cfgintr_unique(struct PIR_entry *pe, int pin) 320099d058bSMike Smith { 321099d058bSMike Smith int irq; 322d5ccecfaSWarner Losh uint32_t irqmask; 323099d058bSMike Smith 324d5ccecfaSWarner Losh irqmask = pe->pe_intpin[pin - 1].irqs; 325d5ccecfaSWarner Losh if (irqmask != 0 && powerof2(irqmask)) { 326d5ccecfaSWarner Losh irq = ffs(irqmask) - 1; 327d626906bSWarner Losh PRVERB(("pci_cfgintr_unique: hard-routed to irq %d\n", irq)); 328099d058bSMike Smith return(irq); 329099d058bSMike Smith } 330099d058bSMike Smith return(255); 331099d058bSMike Smith } 332099d058bSMike Smith 333099d058bSMike Smith /* 334099d058bSMike Smith * Look for another device which shares the same link byte and 335099d058bSMike Smith * already has a unique IRQ, or which has had one routed already. 336099d058bSMike Smith */ 337099d058bSMike Smith static int 338099d058bSMike Smith pci_cfgintr_linked(struct PIR_entry *pe, int pin) 339099d058bSMike Smith { 340099d058bSMike Smith struct PIR_entry *oe; 341099d058bSMike Smith struct PIR_intpin *pi; 342099d058bSMike Smith int i, j, irq; 343099d058bSMike Smith 344099d058bSMike Smith /* 345099d058bSMike Smith * Scan table slots. 346099d058bSMike Smith */ 347099d058bSMike Smith for (i = 0, oe = &pci_route_table->pt_entry[0]; i < pci_route_count; i++, oe++) { 348099d058bSMike Smith 349099d058bSMike Smith /* scan interrupt pins */ 350099d058bSMike Smith for (j = 0, pi = &oe->pe_intpin[0]; j < 4; j++, pi++) { 351099d058bSMike Smith 352099d058bSMike Smith /* don't look at the entry we're trying to match with */ 353099d058bSMike Smith if ((pe == oe) && (i == (pin - 1))) 354099d058bSMike Smith continue; 355099d058bSMike Smith 356099d058bSMike Smith /* compare link bytes */ 357099d058bSMike Smith if (pi->link != pe->pe_intpin[pin - 1].link) 358099d058bSMike Smith continue; 359099d058bSMike Smith 360099d058bSMike Smith /* link destination mapped to a unique interrupt? */ 361d5ccecfaSWarner Losh if (pi->irqs != 0 && powerof2(pi->irqs)) { 362099d058bSMike Smith irq = ffs(pi->irqs) - 1; 363d626906bSWarner Losh PRVERB(("pci_cfgintr_linked: linked (%x) to hard-routed irq %d\n", 364d626906bSWarner Losh pi->link, irq)); 365099d058bSMike Smith return(irq); 366099d058bSMike Smith } 367099d058bSMike Smith 368099d058bSMike Smith /* look for the real PCI device that matches this table entry */ 369099d058bSMike Smith if ((irq = pci_cfgintr_search(pe, oe->pe_bus, oe->pe_device, j, pin)) != 255) 370099d058bSMike Smith return(irq); 371099d058bSMike Smith } 372099d058bSMike Smith } 373099d058bSMike Smith return(255); 374099d058bSMike Smith } 375099d058bSMike Smith 376099d058bSMike Smith /* 377099d058bSMike Smith * Scan for the real PCI device at (bus)/(device) using intpin (matchpin) and 378099d058bSMike Smith * see if it has already been assigned an interrupt. 379099d058bSMike Smith */ 380099d058bSMike Smith static int 381099d058bSMike Smith pci_cfgintr_search(struct PIR_entry *pe, int bus, int device, int matchpin, int pin) 382099d058bSMike Smith { 383099d058bSMike Smith devclass_t pci_devclass; 384099d058bSMike Smith device_t *pci_devices; 385099d058bSMike Smith int pci_count; 386099d058bSMike Smith device_t *pci_children; 387099d058bSMike Smith int pci_childcount; 388099d058bSMike Smith device_t *busp, *childp; 389099d058bSMike Smith int i, j, irq; 390099d058bSMike Smith 391099d058bSMike Smith /* 392099d058bSMike Smith * Find all the PCI busses. 393099d058bSMike Smith */ 394099d058bSMike Smith pci_count = 0; 395099d058bSMike Smith if ((pci_devclass = devclass_find("pci")) != NULL) 396099d058bSMike Smith devclass_get_devices(pci_devclass, &pci_devices, &pci_count); 397099d058bSMike Smith 398099d058bSMike Smith /* 399099d058bSMike Smith * Scan all the PCI busses/devices looking for this one. 400099d058bSMike Smith */ 4016c9eb5f3SMike Smith irq = 255; 4026c9eb5f3SMike Smith for (i = 0, busp = pci_devices; (i < pci_count) && (irq == 255); i++, busp++) { 403099d058bSMike Smith pci_childcount = 0; 404099d058bSMike Smith device_get_children(*busp, &pci_children, &pci_childcount); 405099d058bSMike Smith 406099d058bSMike Smith for (j = 0, childp = pci_children; j < pci_childcount; j++, childp++) { 407099d058bSMike Smith if ((pci_get_bus(*childp) == bus) && 408099d058bSMike Smith (pci_get_slot(*childp) == device) && 4091cf5f555SWarner Losh (pci_get_intpin(*childp) == matchpin)) { 4101cf5f555SWarner Losh irq = pci_get_irq(*childp); 4111cf5f555SWarner Losh if (irq != 255) 412d626906bSWarner Losh PRVERB(("pci_cfgintr_search: linked (%x) to configured irq %d at %d:%d:%d\n", 41385fab963SMike Smith pe->pe_intpin[pin - 1].link, irq, 414d626906bSWarner Losh pci_get_bus(*childp), pci_get_slot(*childp), pci_get_function(*childp))); 4156c9eb5f3SMike Smith break; 4166c9eb5f3SMike Smith } 4176c9eb5f3SMike Smith } 4186c9eb5f3SMike Smith if (pci_children != NULL) 4196c9eb5f3SMike Smith free(pci_children, M_TEMP); 4206c9eb5f3SMike Smith } 4216c9eb5f3SMike Smith if (pci_devices != NULL) 4226c9eb5f3SMike Smith free(pci_devices, M_TEMP); 423099d058bSMike Smith return(irq); 424099d058bSMike Smith } 425099d058bSMike Smith 426099d058bSMike Smith /* 427099d058bSMike Smith * Pick a suitable IRQ from those listed as routable to this device. 428099d058bSMike Smith */ 429099d058bSMike Smith static int 430099d058bSMike Smith pci_cfgintr_virgin(struct PIR_entry *pe, int pin) 431099d058bSMike Smith { 432099d058bSMike Smith int irq, ibit; 433099d058bSMike Smith 434099d058bSMike Smith /* first scan the set of PCI-only interrupts and see if any of these are routable */ 435099d058bSMike Smith for (irq = 0; irq < 16; irq++) { 436099d058bSMike Smith ibit = (1 << irq); 437099d058bSMike Smith 438099d058bSMike Smith /* can we use this interrupt? */ 439099d058bSMike Smith if ((pci_route_table->pt_header.ph_pci_irqs & ibit) && 440099d058bSMike Smith (pe->pe_intpin[pin - 1].irqs & ibit)) { 441d626906bSWarner Losh PRVERB(("pci_cfgintr_virgin: using routable PCI-only interrupt %d\n", irq)); 442099d058bSMike Smith return(irq); 443099d058bSMike Smith } 444099d058bSMike Smith } 445099d058bSMike Smith 446099d058bSMike Smith /* life is tough, so just pick an interrupt */ 447099d058bSMike Smith for (irq = 0; irq < 16; irq++) { 448099d058bSMike Smith ibit = (1 << irq); 449099d058bSMike Smith 450099d058bSMike Smith if (pe->pe_intpin[pin - 1].irqs & ibit) { 451d626906bSWarner Losh PRVERB(("pci_cfgintr_virgin: using routable interrupt %d\n", irq)); 452099d058bSMike Smith return(irq); 453099d058bSMike Smith } 454099d058bSMike Smith } 45554c9005fSWarner Losh return(255); 45654c9005fSWarner Losh } 45754c9005fSWarner Losh 45854c9005fSWarner Losh 45954c9005fSWarner Losh /* 46012a02d6eSMike Smith * Config space access using BIOS functions 46112a02d6eSMike Smith */ 462300451c4SMike Smith static int 46321c3015aSDoug Rabson pcibios_cfgread(int bus, int slot, int func, int reg, int bytes) 464300451c4SMike Smith { 465300451c4SMike Smith struct bios_regs args; 466ac9b3dacSMike Smith u_int mask; 467300451c4SMike Smith 468300451c4SMike Smith switch(bytes) { 469300451c4SMike Smith case 1: 470300451c4SMike Smith args.eax = PCIBIOS_READ_CONFIG_BYTE; 471ac9b3dacSMike Smith mask = 0xff; 472300451c4SMike Smith break; 473300451c4SMike Smith case 2: 474300451c4SMike Smith args.eax = PCIBIOS_READ_CONFIG_WORD; 475ac9b3dacSMike Smith mask = 0xffff; 476300451c4SMike Smith break; 477300451c4SMike Smith case 4: 478300451c4SMike Smith args.eax = PCIBIOS_READ_CONFIG_DWORD; 479ac9b3dacSMike Smith mask = 0xffffffff; 480300451c4SMike Smith break; 481300451c4SMike Smith default: 482300451c4SMike Smith return(-1); 483300451c4SMike Smith } 48421c3015aSDoug Rabson args.ebx = (bus << 8) | (slot << 3) | func; 485300451c4SMike Smith args.edi = reg; 486300451c4SMike Smith bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)); 487300451c4SMike Smith /* check call results? */ 488ac9b3dacSMike Smith return(args.ecx & mask); 489300451c4SMike Smith } 490300451c4SMike Smith 491300451c4SMike Smith static void 49221c3015aSDoug Rabson pcibios_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes) 493300451c4SMike Smith { 494300451c4SMike Smith struct bios_regs args; 495300451c4SMike Smith 496300451c4SMike Smith switch(bytes) { 497300451c4SMike Smith case 1: 498300451c4SMike Smith args.eax = PCIBIOS_WRITE_CONFIG_BYTE; 499300451c4SMike Smith break; 500300451c4SMike Smith case 2: 501300451c4SMike Smith args.eax = PCIBIOS_WRITE_CONFIG_WORD; 502300451c4SMike Smith break; 503300451c4SMike Smith case 4: 504300451c4SMike Smith args.eax = PCIBIOS_WRITE_CONFIG_DWORD; 505300451c4SMike Smith break; 506300451c4SMike Smith default: 507300451c4SMike Smith return; 508300451c4SMike Smith } 50921c3015aSDoug Rabson args.ebx = (bus << 8) | (slot << 3) | func; 510300451c4SMike Smith args.ecx = data; 511300451c4SMike Smith args.edi = reg; 512300451c4SMike Smith bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)); 513300451c4SMike Smith } 514300451c4SMike Smith 51512a02d6eSMike Smith /* 51612a02d6eSMike Smith * Determine whether there is a PCI BIOS present 51712a02d6eSMike Smith */ 518300451c4SMike Smith static int 519300451c4SMike Smith pcibios_cfgopen(void) 520300451c4SMike Smith { 5210b9427deSWarner Losh u_int16_t v = 0; 5220b9427deSWarner Losh 5230b9427deSWarner Losh if (PCIbios.entry != 0 && enable_pcibios) { 5240b9427deSWarner Losh v = pcibios_get_version(); 5250b9427deSWarner Losh if (v > 0) 5260b9427deSWarner Losh printf("pcibios: BIOS version %x.%02x\n", (v & 0xff00) >> 8, 5270b9427deSWarner Losh v & 0xff); 5280b9427deSWarner Losh } 5290b9427deSWarner Losh return (v > 0); 530300451c4SMike Smith } 531300451c4SMike Smith 53212a02d6eSMike Smith /* 53312a02d6eSMike Smith * Configuration space access using direct register operations 53412a02d6eSMike Smith */ 535ac19f918SStefan Eßer 5365bec6157SStefan Eßer /* enable configuration space accesses and return data port address */ 537a3adc4f8SStefan Eßer static int 5385bec6157SStefan Eßer pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes) 5395bec6157SStefan Eßer { 5405bec6157SStefan Eßer int dataport = 0; 5415bec6157SStefan Eßer 5425bec6157SStefan Eßer if (bus <= PCI_BUSMAX 5435bec6157SStefan Eßer && slot < devmax 5445bec6157SStefan Eßer && func <= PCI_FUNCMAX 5455bec6157SStefan Eßer && reg <= PCI_REGMAX 5465bec6157SStefan Eßer && bytes != 3 5475bec6157SStefan Eßer && (unsigned) bytes <= 4 5485bec6157SStefan Eßer && (reg & (bytes -1)) == 0) { 5495bec6157SStefan Eßer switch (cfgmech) { 5505bec6157SStefan Eßer case 1: 551b3daa02eSStefan Eßer outl(CONF1_ADDR_PORT, (1 << 31) 552b3daa02eSStefan Eßer | (bus << 16) | (slot << 11) 553b3daa02eSStefan Eßer | (func << 8) | (reg & ~0x03)); 554b3daa02eSStefan Eßer dataport = CONF1_DATA_PORT + (reg & 0x03); 5555bec6157SStefan Eßer break; 5565bec6157SStefan Eßer case 2: 5575bec6157SStefan Eßer outb(CONF2_ENABLE_PORT, 0xf0 | (func << 1)); 5585bec6157SStefan Eßer outb(CONF2_FORWARD_PORT, bus); 5595bec6157SStefan Eßer dataport = 0xc000 | (slot << 8) | reg; 5605bec6157SStefan Eßer break; 5615bec6157SStefan Eßer } 5625bec6157SStefan Eßer } 5635bec6157SStefan Eßer return (dataport); 5645bec6157SStefan Eßer } 5655bec6157SStefan Eßer 5665bec6157SStefan Eßer /* disable configuration space accesses */ 5675bec6157SStefan Eßer static void 5685bec6157SStefan Eßer pci_cfgdisable(void) 5695bec6157SStefan Eßer { 5705bec6157SStefan Eßer switch (cfgmech) { 5715bec6157SStefan Eßer case 1: 5725bec6157SStefan Eßer outl(CONF1_ADDR_PORT, 0); 5735bec6157SStefan Eßer break; 5745bec6157SStefan Eßer case 2: 5755bec6157SStefan Eßer outb(CONF2_ENABLE_PORT, 0); 5765bec6157SStefan Eßer outb(CONF2_FORWARD_PORT, 0); 5775bec6157SStefan Eßer break; 5785bec6157SStefan Eßer } 5795bec6157SStefan Eßer } 5805bec6157SStefan Eßer 581300451c4SMike Smith static int 58221c3015aSDoug Rabson pcireg_cfgread(int bus, int slot, int func, int reg, int bytes) 5835bec6157SStefan Eßer { 5845bec6157SStefan Eßer int data = -1; 5855bec6157SStefan Eßer int port; 5865bec6157SStefan Eßer 58721c3015aSDoug Rabson port = pci_cfgenable(bus, slot, func, reg, bytes); 5885bec6157SStefan Eßer 5895bec6157SStefan Eßer if (port != 0) { 5905bec6157SStefan Eßer switch (bytes) { 5915bec6157SStefan Eßer case 1: 5925bec6157SStefan Eßer data = inb(port); 5935bec6157SStefan Eßer break; 5945bec6157SStefan Eßer case 2: 5955bec6157SStefan Eßer data = inw(port); 5965bec6157SStefan Eßer break; 5975bec6157SStefan Eßer case 4: 5985bec6157SStefan Eßer data = inl(port); 5995bec6157SStefan Eßer break; 6005bec6157SStefan Eßer } 6015bec6157SStefan Eßer pci_cfgdisable(); 6025bec6157SStefan Eßer } 6035bec6157SStefan Eßer return (data); 6045bec6157SStefan Eßer } 6055bec6157SStefan Eßer 606300451c4SMike Smith static void 60721c3015aSDoug Rabson pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes) 6085bec6157SStefan Eßer { 6095bec6157SStefan Eßer int port; 6105bec6157SStefan Eßer 61121c3015aSDoug Rabson port = pci_cfgenable(bus, slot, func, reg, bytes); 6125bec6157SStefan Eßer if (port != 0) { 6135bec6157SStefan Eßer switch (bytes) { 6145bec6157SStefan Eßer case 1: 6155bec6157SStefan Eßer outb(port, data); 6165bec6157SStefan Eßer break; 6175bec6157SStefan Eßer case 2: 6185bec6157SStefan Eßer outw(port, data); 6195bec6157SStefan Eßer break; 6205bec6157SStefan Eßer case 4: 6215bec6157SStefan Eßer outl(port, data); 6225bec6157SStefan Eßer break; 6235bec6157SStefan Eßer } 6245bec6157SStefan Eßer pci_cfgdisable(); 6255bec6157SStefan Eßer } 6265bec6157SStefan Eßer } 6275bec6157SStefan Eßer 62812a02d6eSMike Smith /* check whether the configuration mechanism has been correctly identified */ 6295bec6157SStefan Eßer static int 6305bec6157SStefan Eßer pci_cfgcheck(int maxdev) 631a3adc4f8SStefan Eßer { 632a3adc4f8SStefan Eßer u_char device; 633a3adc4f8SStefan Eßer 6345bec6157SStefan Eßer if (bootverbose) 6355bec6157SStefan Eßer printf("pci_cfgcheck:\tdevice "); 63677b57314SStefan Eßer 6375bec6157SStefan Eßer for (device = 0; device < maxdev; device++) { 6385bec6157SStefan Eßer unsigned id, class, header; 639c7483249SStefan Eßer if (bootverbose) 640c7483249SStefan Eßer printf("%d ", device); 6415bec6157SStefan Eßer 6425bec6157SStefan Eßer id = inl(pci_cfgenable(0, device, 0, 0, 4)); 6435bec6157SStefan Eßer if (id == 0 || id == -1) 64481cf5d7aSStefan Eßer continue; 64581cf5d7aSStefan Eßer 6465bec6157SStefan Eßer class = inl(pci_cfgenable(0, device, 0, 8, 4)) >> 8; 64781cf5d7aSStefan Eßer if (bootverbose) 6485bec6157SStefan Eßer printf("[class=%06x] ", class); 6498277ac25SStefan Eßer if (class == 0 || (class & 0xf870ff) != 0) 65081cf5d7aSStefan Eßer continue; 65181cf5d7aSStefan Eßer 6525bec6157SStefan Eßer header = inb(pci_cfgenable(0, device, 0, 14, 1)); 65381cf5d7aSStefan Eßer if (bootverbose) 6545bec6157SStefan Eßer printf("[hdr=%02x] ", header); 6555bec6157SStefan Eßer if ((header & 0x7e) != 0) 65681cf5d7aSStefan Eßer continue; 65781cf5d7aSStefan Eßer 6585bec6157SStefan Eßer if (bootverbose) 6595bec6157SStefan Eßer printf("is there (id=%08x)\n", id); 6605bec6157SStefan Eßer 6615bec6157SStefan Eßer pci_cfgdisable(); 6625bec6157SStefan Eßer return (1); 663a3adc4f8SStefan Eßer } 664c7483249SStefan Eßer if (bootverbose) 665c7483249SStefan Eßer printf("-- nothing found\n"); 6665bec6157SStefan Eßer 6675bec6157SStefan Eßer pci_cfgdisable(); 6685bec6157SStefan Eßer return (0); 669a3adc4f8SStefan Eßer } 670d7ea35fcSStefan Eßer 6718dc26439SPeter Wemm static int 672300451c4SMike Smith pcireg_cfgopen(void) 673ac19f918SStefan Eßer { 674287911bdSStefan Eßer unsigned long mode1res,oldval1; 675287911bdSStefan Eßer unsigned char mode2res,oldval2; 6760847c06dSStefan Eßer 677287911bdSStefan Eßer oldval1 = inl(CONF1_ADDR_PORT); 678a3adc4f8SStefan Eßer 67977b57314SStefan Eßer if (bootverbose) { 6805bec6157SStefan Eßer printf("pci_open(1):\tmode 1 addr port (0x0cf8) is 0x%08lx\n", 6815bec6157SStefan Eßer oldval1); 682a3adc4f8SStefan Eßer } 683a3adc4f8SStefan Eßer 6840e2f699bSStefan Eßer if ((oldval1 & CONF1_ENABLE_MSK) == 0) { 685287911bdSStefan Eßer 6865bec6157SStefan Eßer cfgmech = 1; 6875bec6157SStefan Eßer devmax = 32; 68877b57314SStefan Eßer 68977b57314SStefan Eßer outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK); 69077b57314SStefan Eßer outb(CONF1_ADDR_PORT +3, 0); 69177b57314SStefan Eßer mode1res = inl(CONF1_ADDR_PORT); 692287911bdSStefan Eßer outl(CONF1_ADDR_PORT, oldval1); 69377b57314SStefan Eßer 69477b57314SStefan Eßer if (bootverbose) 6955bec6157SStefan Eßer printf("pci_open(1a):\tmode1res=0x%08lx (0x%08lx)\n", 69677b57314SStefan Eßer mode1res, CONF1_ENABLE_CHK); 69777b57314SStefan Eßer 69877b57314SStefan Eßer if (mode1res) { 6995bec6157SStefan Eßer if (pci_cfgcheck(32)) 7005bec6157SStefan Eßer return (cfgmech); 7015bec6157SStefan Eßer } 70277b57314SStefan Eßer 70377b57314SStefan Eßer outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK1); 70477b57314SStefan Eßer mode1res = inl(CONF1_ADDR_PORT); 705287911bdSStefan Eßer outl(CONF1_ADDR_PORT, oldval1); 70677b57314SStefan Eßer 70777b57314SStefan Eßer if (bootverbose) 7085bec6157SStefan Eßer printf("pci_open(1b):\tmode1res=0x%08lx (0x%08lx)\n", 70977b57314SStefan Eßer mode1res, CONF1_ENABLE_CHK1); 71077b57314SStefan Eßer 711c7483249SStefan Eßer if ((mode1res & CONF1_ENABLE_MSK1) == CONF1_ENABLE_RES1) { 7125bec6157SStefan Eßer if (pci_cfgcheck(32)) 7135bec6157SStefan Eßer return (cfgmech); 714287911bdSStefan Eßer } 7155bec6157SStefan Eßer } 71677b57314SStefan Eßer 717287911bdSStefan Eßer oldval2 = inb(CONF2_ENABLE_PORT); 718287911bdSStefan Eßer 719287911bdSStefan Eßer if (bootverbose) { 7205bec6157SStefan Eßer printf("pci_open(2):\tmode 2 enable port (0x0cf8) is 0x%02x\n", 7215bec6157SStefan Eßer oldval2); 722287911bdSStefan Eßer } 723287911bdSStefan Eßer 724287911bdSStefan Eßer if ((oldval2 & 0xf0) == 0) { 725c7483249SStefan Eßer 7265bec6157SStefan Eßer cfgmech = 2; 7275bec6157SStefan Eßer devmax = 16; 72877b57314SStefan Eßer 729287911bdSStefan Eßer outb(CONF2_ENABLE_PORT, CONF2_ENABLE_CHK); 730287911bdSStefan Eßer mode2res = inb(CONF2_ENABLE_PORT); 731287911bdSStefan Eßer outb(CONF2_ENABLE_PORT, oldval2); 732287911bdSStefan Eßer 733287911bdSStefan Eßer if (bootverbose) 7345bec6157SStefan Eßer printf("pci_open(2a):\tmode2res=0x%02x (0x%02x)\n", 735287911bdSStefan Eßer mode2res, CONF2_ENABLE_CHK); 736287911bdSStefan Eßer 737287911bdSStefan Eßer if (mode2res == CONF2_ENABLE_RES) { 738287911bdSStefan Eßer if (bootverbose) 7395bec6157SStefan Eßer printf("pci_open(2a):\tnow trying mechanism 2\n"); 740287911bdSStefan Eßer 7415bec6157SStefan Eßer if (pci_cfgcheck(16)) 7425bec6157SStefan Eßer return (cfgmech); 743287911bdSStefan Eßer } 744287911bdSStefan Eßer } 74577b57314SStefan Eßer 7465bec6157SStefan Eßer cfgmech = 0; 7475bec6157SStefan Eßer devmax = 0; 7485bec6157SStefan Eßer return (cfgmech); 749ac19f918SStefan Eßer } 7508dc26439SPeter Wemm 751