1 /* $OpenBSD: pci_bus_fixup.c,v 1.10 2023/01/30 10:49:05 jsg Exp $ */ 2 /* $NetBSD: pci_bus_fixup.c,v 1.1 1999/11/17 07:32:58 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1999, by UCHIYAMA Yasushi 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. The name of the developer may NOT be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 /* 30 * PCI bus renumbering support. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 36 #include <machine/bus.h> 37 38 #include <dev/pci/pcireg.h> 39 #include <dev/pci/pcivar.h> 40 #include <dev/pci/pcidevs.h> 41 #include <dev/pci/ppbreg.h> 42 43 #include <i386/pci/pcibiosvar.h> 44 45 int pci_bus_check(pci_chipset_tag_t, int); 46 int pci_bus_assign(pci_chipset_tag_t, int); 47 void pcibus_print_devid(pci_chipset_tag_t, pcitag_t); 48 49 int 50 pci_bus_check(pci_chipset_tag_t pc, int bus) 51 { 52 int device, maxdevs, function, nfuncs, bus_max, bus_sub; 53 const struct pci_quirkdata *qd; 54 pcireg_t reg; 55 pcitag_t tag; 56 57 bus_max = bus; 58 59 maxdevs = pci_bus_maxdevs(pc, bus); 60 for (device = 0; device < maxdevs; device++) { 61 tag = pci_make_tag(pc, bus, device, 0); 62 reg = pci_conf_read(pc, tag, PCI_ID_REG); 63 64 /* can't be that many */ 65 if (bus_max == 255) 66 break; 67 68 /* Invalid vendor ID value? */ 69 if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID) 70 continue; 71 /* XXX Not invalid, but we've done this ~forever. */ 72 if (PCI_VENDOR(reg) == 0) 73 continue; 74 75 qd = pci_lookup_quirkdata(PCI_VENDOR(reg), PCI_PRODUCT(reg)); 76 77 reg = pci_conf_read(pc, tag, PCI_BHLC_REG); 78 if (PCI_HDRTYPE_MULTIFN(reg) || 79 (qd != NULL && 80 (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0)) 81 nfuncs = 8; 82 else 83 nfuncs = 1; 84 85 for (function = 0; function < nfuncs; function++) { 86 tag = pci_make_tag(pc, bus, device, function); 87 reg = pci_conf_read(pc, tag, PCI_ID_REG); 88 89 /* Invalid vendor ID value? */ 90 if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID) 91 continue; 92 /* XXX Not invalid, but we've done this ~forever. */ 93 if (PCI_VENDOR(reg) == 0) 94 continue; 95 96 reg = pci_conf_read(pc, tag, PCI_CLASS_REG); 97 if (PCI_CLASS(reg) == PCI_CLASS_BRIDGE && 98 (PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_PCI || 99 PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_CARDBUS)) { 100 101 reg = pci_conf_read(pc, tag, PPB_REG_BUSINFO); 102 if (PPB_BUSINFO_PRIMARY(reg) != bus) { 103 if (pcibios_flags & PCIBIOS_VERBOSE) { 104 pcibus_print_devid(pc, tag); 105 printf("Mismatched primary bus: " 106 "primary %d, secondary %d, " 107 "subordinate %d\n", 108 PPB_BUSINFO_PRIMARY(reg), 109 PPB_BUSINFO_SECONDARY(reg), 110 PPB_BUSINFO_SUBORDINATE(reg)); 111 } 112 return (-1); 113 } 114 if (PPB_BUSINFO_SECONDARY(reg) <= bus) { 115 if (pcibios_flags & PCIBIOS_VERBOSE) { 116 pcibus_print_devid(pc, tag); 117 printf("Incorrect secondary bus: " 118 "primary %d, secondary %d, " 119 "subordinate %d\n", 120 PPB_BUSINFO_PRIMARY(reg), 121 PPB_BUSINFO_SECONDARY(reg), 122 PPB_BUSINFO_SUBORDINATE(reg)); 123 } 124 return (-1); 125 } 126 127 /* Scan subordinate bus. */ 128 bus_sub = pci_bus_check(pc, 129 PPB_BUSINFO_SECONDARY(reg)); 130 if (bus_sub == -1) 131 return (-1); 132 133 if (PPB_BUSINFO_SUBORDINATE(reg) < bus_sub) { 134 if (pcibios_flags & PCIBIOS_VERBOSE) { 135 pcibus_print_devid(pc, tag); 136 printf("Incorrect subordinate bus %d: " 137 "primary %d, secondary %d, " 138 "subordinate %d\n", bus_sub, 139 PPB_BUSINFO_PRIMARY(reg), 140 PPB_BUSINFO_SECONDARY(reg), 141 PPB_BUSINFO_SUBORDINATE(reg)); 142 } 143 return (-1); 144 } 145 146 bus_max = (bus_sub > bus_max) ? 147 bus_sub : bus_max; 148 } 149 } 150 } 151 152 return (bus_max); /* last # of subordinate bus */ 153 } 154 155 int 156 pci_bus_assign(pci_chipset_tag_t pc, int bus) 157 { 158 static int bridge_cnt; 159 int bridge, device, maxdevs, function, nfuncs, bus_max, bus_sub; 160 const struct pci_quirkdata *qd; 161 pcireg_t reg; 162 pcitag_t tag; 163 164 bus_max = bus; 165 166 maxdevs = pci_bus_maxdevs(pc, bus); 167 for (device = 0; device < maxdevs; device++) { 168 tag = pci_make_tag(pc, bus, device, 0); 169 reg = pci_conf_read(pc, tag, PCI_ID_REG); 170 171 /* can't be that many */ 172 if (bus_max == 255) 173 break; 174 175 /* Invalid vendor ID value? */ 176 if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID) 177 continue; 178 /* XXX Not invalid, but we've done this ~forever. */ 179 if (PCI_VENDOR(reg) == 0) 180 continue; 181 182 qd = pci_lookup_quirkdata(PCI_VENDOR(reg), PCI_PRODUCT(reg)); 183 184 reg = pci_conf_read(pc, tag, PCI_BHLC_REG); 185 if (PCI_HDRTYPE_MULTIFN(reg) || 186 (qd != NULL && 187 (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0)) 188 nfuncs = 8; 189 else 190 nfuncs = 1; 191 192 for (function = 0; function < nfuncs; function++) { 193 tag = pci_make_tag(pc, bus, device, function); 194 reg = pci_conf_read(pc, tag, PCI_ID_REG); 195 196 /* Invalid vendor ID value? */ 197 if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID) 198 continue; 199 /* XXX Not invalid, but we've done this ~forever. */ 200 if (PCI_VENDOR(reg) == 0) 201 continue; 202 203 reg = pci_conf_read(pc, tag, PCI_CLASS_REG); 204 if (PCI_CLASS(reg) == PCI_CLASS_BRIDGE && 205 (PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_PCI || 206 PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_CARDBUS)) { 207 /* Assign the bridge's secondary bus #. */ 208 bus_max++; 209 210 reg = pci_conf_read(pc, tag, PPB_REG_BUSINFO); 211 reg &= 0xff000000; 212 reg |= bus | (bus_max << 8) | (0xff << 16); 213 pci_conf_write(pc, tag, PPB_REG_BUSINFO, reg); 214 215 /* Scan subordinate bus. */ 216 bus_sub = pci_bus_assign(pc, bus_max); 217 218 /* Configure the bridge. */ 219 reg &= 0xff000000; 220 reg |= bus | (bus_max << 8) | (bus_sub << 16); 221 pci_conf_write(pc, tag, PPB_REG_BUSINFO, reg); 222 223 if (pcibios_flags & PCIBIOS_VERBOSE) { 224 /* Assign the bridge #. */ 225 bridge = bridge_cnt++; 226 227 printf("PCI bridge %d: primary %d, " 228 "secondary %d, subordinate %d\n", 229 bridge, bus, bus_max, bus_sub); 230 } 231 232 /* Next bridge's secondary bus #. */ 233 bus_max = (bus_sub > bus_max) ? 234 bus_sub : bus_max; 235 } 236 } 237 } 238 239 return (bus_max); /* last # of subordinate bus */ 240 } 241 242 int 243 pci_bus_fixup(pci_chipset_tag_t pc, int bus) 244 { 245 int bus_max; 246 247 bus_max = pci_bus_check(pc, bus); 248 if (bus_max != -1) 249 return (bus_max); 250 251 if (pcibios_flags & PCIBIOS_VERBOSE) 252 printf("PCI bus renumbering needed\n"); 253 return pci_bus_assign(pc, bus); 254 } 255 256 void 257 pcibus_print_devid(pci_chipset_tag_t pc, pcitag_t tag) 258 { 259 int bus, device, function; 260 pcireg_t id; 261 262 id = pci_conf_read(pc, tag, PCI_ID_REG); 263 pci_decompose_tag(pc, tag, &bus, &device, &function); 264 printf("%03d:%02d:%d %04x:%04x\n", bus, device, function, 265 PCI_VENDOR(id), PCI_PRODUCT(id)); 266 } 267