1 1.30 riastrad /* $NetBSD: linux_pci.c,v 1.30 2024/06/24 21:23:53 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /*- 4 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 1.1 riastrad * All rights reserved. 6 1.1 riastrad * 7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation 8 1.1 riastrad * by Taylor R. Campbell. 9 1.1 riastrad * 10 1.1 riastrad * Redistribution and use in source and binary forms, with or without 11 1.1 riastrad * modification, are permitted provided that the following conditions 12 1.1 riastrad * are met: 13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright 14 1.1 riastrad * notice, this list of conditions and the following disclaimer. 15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the 17 1.1 riastrad * documentation and/or other materials provided with the distribution. 18 1.1 riastrad * 19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE. 30 1.1 riastrad */ 31 1.1 riastrad 32 1.7 jmcneill #ifdef _KERNEL_OPT 33 1.17 riastrad #include "acpica.h" 34 1.7 jmcneill #include "opt_pci.h" 35 1.7 jmcneill #endif 36 1.7 jmcneill 37 1.1 riastrad #include <sys/cdefs.h> 38 1.30 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_pci.c,v 1.30 2024/06/24 21:23:53 riastradh Exp $"); 39 1.16 riastrad 40 1.16 riastrad #if NACPICA > 0 41 1.16 riastrad #include <dev/acpi/acpivar.h> 42 1.16 riastrad #include <dev/acpi/acpi_pci.h> 43 1.16 riastrad #endif 44 1.1 riastrad 45 1.1 riastrad #include <linux/pci.h> 46 1.1 riastrad 47 1.5 riastrad #include <drm/drm_agp_netbsd.h> 48 1.5 riastrad 49 1.1 riastrad device_t 50 1.1 riastrad pci_dev_dev(struct pci_dev *pdev) 51 1.1 riastrad { 52 1.1 riastrad 53 1.1 riastrad return pdev->pd_dev; 54 1.1 riastrad } 55 1.1 riastrad 56 1.12 riastrad void 57 1.12 riastrad pci_set_drvdata(struct pci_dev *pdev, void *drvdata) 58 1.12 riastrad { 59 1.12 riastrad pdev->pd_drvdata = drvdata; 60 1.12 riastrad } 61 1.12 riastrad 62 1.12 riastrad void * 63 1.1 riastrad pci_get_drvdata(struct pci_dev *pdev) 64 1.1 riastrad { 65 1.12 riastrad return pdev->pd_drvdata; 66 1.1 riastrad } 67 1.1 riastrad 68 1.20 riastrad const char * 69 1.20 riastrad pci_name(struct pci_dev *pdev) 70 1.20 riastrad { 71 1.20 riastrad 72 1.20 riastrad /* XXX not sure this has the right format */ 73 1.20 riastrad return device_xname(pci_dev_dev(pdev)); 74 1.20 riastrad } 75 1.20 riastrad 76 1.25 mrg /* 77 1.25 mrg * Setup enough of a parent that we can access config space. 78 1.25 mrg * This is gross and grovels pci(4) and ppb(4) internals. 79 1.25 mrg */ 80 1.25 mrg static struct pci_dev * 81 1.25 mrg alloc_fake_parent_device(device_t parent, const struct pci_attach_args *pa) 82 1.25 mrg { 83 1.25 mrg 84 1.25 mrg if (parent == NULL || !device_is_a(parent, "pci")) 85 1.25 mrg return NULL; 86 1.25 mrg 87 1.25 mrg device_t pparent = device_parent(parent); 88 1.25 mrg if (pparent == NULL || !device_is_a(pparent, "ppb")) 89 1.25 mrg return NULL; 90 1.25 mrg 91 1.25 mrg struct pci_softc *pcisc = device_private(parent); 92 1.25 mrg struct ppb_softc *ppbsc = device_private(pparent); 93 1.25 mrg 94 1.25 mrg struct pci_dev *parentdev = kmem_zalloc(sizeof(*parentdev), KM_SLEEP); 95 1.25 mrg 96 1.25 mrg /* Copy this device's pci_attach_args{} as a base-line. */ 97 1.25 mrg struct pci_attach_args *npa = &parentdev->pd_pa; 98 1.25 mrg *npa = *pa; 99 1.25 mrg 100 1.25 mrg /* Now update with stuff found in parent. */ 101 1.25 mrg npa->pa_iot = pcisc->sc_iot; 102 1.25 mrg npa->pa_memt = pcisc->sc_memt; 103 1.25 mrg npa->pa_dmat = pcisc->sc_dmat; 104 1.25 mrg npa->pa_dmat64 = pcisc->sc_dmat64; 105 1.25 mrg npa->pa_pc = pcisc->sc_pc; 106 1.25 mrg npa->pa_flags = 0; /* XXX? */ 107 1.25 mrg 108 1.25 mrg /* Copy the parent tag, and read some info about it. */ 109 1.25 mrg npa->pa_tag = ppbsc->sc_tag; 110 1.25 mrg pcireg_t id = pci_conf_read(npa->pa_pc, npa->pa_tag, PCI_ID_REG); 111 1.25 mrg pcireg_t subid = pci_conf_read(npa->pa_pc, npa->pa_tag, 112 1.25 mrg PCI_SUBSYS_ID_REG); 113 1.25 mrg pcireg_t class = pci_conf_read(npa->pa_pc, npa->pa_tag, PCI_CLASS_REG); 114 1.25 mrg 115 1.25 mrg /* 116 1.25 mrg * Fill in as much of pci_attach_args and pci_dev as reasonably possible. 117 1.25 mrg * Most of this is not used currently. 118 1.25 mrg */ 119 1.25 mrg int bus, device, function; 120 1.25 mrg pci_decompose_tag(npa->pa_pc, npa->pa_tag, &bus, &device, &function); 121 1.25 mrg npa->pa_device = device; 122 1.25 mrg npa->pa_function = function; 123 1.25 mrg npa->pa_bus = bus; 124 1.25 mrg npa->pa_id = id; 125 1.25 mrg npa->pa_class = class; 126 1.25 mrg npa->pa_intrswiz = pcisc->sc_intrswiz; 127 1.25 mrg npa->pa_intrtag = pcisc->sc_intrtag; 128 1.25 mrg npa->pa_intrpin = PCI_INTERRUPT_PIN_NONE; 129 1.25 mrg 130 1.25 mrg parentdev->pd_dev = parent; 131 1.25 mrg 132 1.25 mrg parentdev->bus = NULL; 133 1.25 mrg parentdev->devfn = device << 3 | function; 134 1.25 mrg parentdev->vendor = PCI_VENDOR(id); 135 1.25 mrg parentdev->device = PCI_PRODUCT(id); 136 1.25 mrg parentdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subid); 137 1.25 mrg parentdev->subsystem_device = PCI_SUBSYS_ID(subid); 138 1.25 mrg parentdev->revision = PCI_REVISION(class); 139 1.25 mrg parentdev->class = __SHIFTOUT(class, 0xffffff00UL); /* ? */ 140 1.25 mrg 141 1.25 mrg return parentdev; 142 1.25 mrg } 143 1.25 mrg 144 1.1 riastrad void 145 1.1 riastrad linux_pci_dev_init(struct pci_dev *pdev, device_t dev, device_t parent, 146 1.1 riastrad const struct pci_attach_args *pa, int kludges) 147 1.1 riastrad { 148 1.1 riastrad const uint32_t subsystem_id = pci_conf_read(pa->pa_pc, pa->pa_tag, 149 1.1 riastrad PCI_SUBSYS_ID_REG); 150 1.1 riastrad unsigned i; 151 1.1 riastrad 152 1.3 riastrad memset(pdev, 0, sizeof(*pdev)); /* paranoia */ 153 1.3 riastrad 154 1.1 riastrad pdev->pd_pa = *pa; 155 1.1 riastrad pdev->pd_kludges = kludges; 156 1.1 riastrad pdev->pd_rom_vaddr = NULL; 157 1.1 riastrad pdev->pd_dev = dev; 158 1.1 riastrad #if (NACPICA > 0) 159 1.7 jmcneill const int seg = pci_get_segment(pa->pa_pc); 160 1.7 jmcneill pdev->pd_ad = acpi_pcidev_find(seg, pa->pa_bus, 161 1.1 riastrad pa->pa_device, pa->pa_function); 162 1.1 riastrad #else 163 1.1 riastrad pdev->pd_ad = NULL; 164 1.1 riastrad #endif 165 1.1 riastrad pdev->pd_saved_state = NULL; 166 1.1 riastrad pdev->pd_intr_handles = NULL; 167 1.12 riastrad pdev->pd_drvdata = NULL; 168 1.1 riastrad pdev->bus = kmem_zalloc(sizeof(*pdev->bus), KM_NOSLEEP); 169 1.1 riastrad pdev->bus->pb_pc = pa->pa_pc; 170 1.1 riastrad pdev->bus->pb_dev = parent; 171 1.1 riastrad pdev->bus->number = pa->pa_bus; 172 1.27 mrg /* 173 1.27 mrg * NetBSD doesn't have an easy "am I PCIe" or "give me PCIe speed 174 1.27 mrg * from capability" function, but we already emulate the Linux 175 1.27 mrg * versions that do. 176 1.27 mrg */ 177 1.27 mrg if (pci_is_pcie(pdev)) { 178 1.27 mrg pdev->bus->max_bus_speed = pcie_get_speed_cap(pdev); 179 1.27 mrg } else { 180 1.27 mrg /* XXX: Do AGP/PCI-X, etc.? */ 181 1.27 mrg pdev->bus->max_bus_speed = PCI_SPEED_UNKNOWN; 182 1.27 mrg } 183 1.25 mrg pdev->bus->self = alloc_fake_parent_device(parent, pa); 184 1.1 riastrad pdev->devfn = PCI_DEVFN(pa->pa_device, pa->pa_function); 185 1.1 riastrad pdev->vendor = PCI_VENDOR(pa->pa_id); 186 1.1 riastrad pdev->device = PCI_PRODUCT(pa->pa_id); 187 1.1 riastrad pdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subsystem_id); 188 1.1 riastrad pdev->subsystem_device = PCI_SUBSYS_ID(subsystem_id); 189 1.1 riastrad pdev->revision = PCI_REVISION(pa->pa_class); 190 1.1 riastrad pdev->class = __SHIFTOUT(pa->pa_class, 0xffffff00UL); /* ? */ 191 1.1 riastrad 192 1.1 riastrad CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES); 193 1.1 riastrad for (i = 0; i < PCI_NUM_RESOURCES; i++) { 194 1.1 riastrad const int reg = PCI_BAR(i); 195 1.1 riastrad 196 1.1 riastrad pdev->pd_resources[i].type = pci_mapreg_type(pa->pa_pc, 197 1.1 riastrad pa->pa_tag, reg); 198 1.1 riastrad if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, 199 1.1 riastrad pdev->pd_resources[i].type, 200 1.1 riastrad &pdev->pd_resources[i].addr, 201 1.1 riastrad &pdev->pd_resources[i].size, 202 1.1 riastrad &pdev->pd_resources[i].flags)) { 203 1.1 riastrad pdev->pd_resources[i].addr = 0; 204 1.1 riastrad pdev->pd_resources[i].size = 0; 205 1.1 riastrad pdev->pd_resources[i].flags = 0; 206 1.1 riastrad } 207 1.1 riastrad pdev->pd_resources[i].kva = NULL; 208 1.2 riastrad pdev->pd_resources[i].mapped = false; 209 1.1 riastrad } 210 1.1 riastrad } 211 1.1 riastrad 212 1.1 riastrad int 213 1.1 riastrad pci_find_capability(struct pci_dev *pdev, int cap) 214 1.1 riastrad { 215 1.1 riastrad 216 1.1 riastrad return pci_get_capability(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, cap, 217 1.1 riastrad NULL, NULL); 218 1.1 riastrad } 219 1.1 riastrad 220 1.1 riastrad int 221 1.1 riastrad pci_read_config_dword(struct pci_dev *pdev, int reg, uint32_t *valuep) 222 1.1 riastrad { 223 1.1 riastrad 224 1.1 riastrad KASSERT(!ISSET(reg, 3)); 225 1.1 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg); 226 1.1 riastrad return 0; 227 1.1 riastrad } 228 1.1 riastrad 229 1.1 riastrad int 230 1.1 riastrad pci_read_config_word(struct pci_dev *pdev, int reg, uint16_t *valuep) 231 1.1 riastrad { 232 1.1 riastrad 233 1.1 riastrad KASSERT(!ISSET(reg, 1)); 234 1.1 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 235 1.1 riastrad (reg &~ 2)) >> (8 * (reg & 2)); 236 1.1 riastrad return 0; 237 1.1 riastrad } 238 1.1 riastrad 239 1.1 riastrad int 240 1.1 riastrad pci_read_config_byte(struct pci_dev *pdev, int reg, uint8_t *valuep) 241 1.1 riastrad { 242 1.1 riastrad 243 1.1 riastrad *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 244 1.1 riastrad (reg &~ 3)) >> (8 * (reg & 3)); 245 1.1 riastrad return 0; 246 1.1 riastrad } 247 1.1 riastrad 248 1.1 riastrad int 249 1.1 riastrad pci_write_config_dword(struct pci_dev *pdev, int reg, uint32_t value) 250 1.1 riastrad { 251 1.1 riastrad 252 1.1 riastrad KASSERT(!ISSET(reg, 3)); 253 1.1 riastrad pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, value); 254 1.1 riastrad return 0; 255 1.1 riastrad } 256 1.1 riastrad 257 1.1 riastrad int 258 1.1 riastrad pci_bus_read_config_dword(struct pci_bus *bus, unsigned devfn, int reg, 259 1.1 riastrad uint32_t *valuep) 260 1.1 riastrad { 261 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), 262 1.1 riastrad PCI_FUNC(devfn)); 263 1.1 riastrad 264 1.1 riastrad KASSERT(!ISSET(reg, 1)); 265 1.1 riastrad *valuep = pci_conf_read(bus->pb_pc, tag, reg & ~3) >> (8 * (reg & 3)); 266 1.1 riastrad return 0; 267 1.1 riastrad } 268 1.1 riastrad 269 1.1 riastrad int 270 1.1 riastrad pci_bus_read_config_word(struct pci_bus *bus, unsigned devfn, int reg, 271 1.1 riastrad uint16_t *valuep) 272 1.1 riastrad { 273 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), 274 1.1 riastrad PCI_FUNC(devfn)); 275 1.1 riastrad 276 1.1 riastrad KASSERT(!ISSET(reg, 1)); 277 1.1 riastrad *valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 2) >> (8 * (reg & 2)); 278 1.1 riastrad return 0; 279 1.1 riastrad } 280 1.1 riastrad 281 1.1 riastrad int 282 1.1 riastrad pci_bus_read_config_byte(struct pci_bus *bus, unsigned devfn, int reg, 283 1.1 riastrad uint8_t *valuep) 284 1.1 riastrad { 285 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), 286 1.1 riastrad PCI_FUNC(devfn)); 287 1.1 riastrad 288 1.1 riastrad *valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 3) >> (8 * (reg & 3)); 289 1.1 riastrad return 0; 290 1.1 riastrad } 291 1.1 riastrad 292 1.1 riastrad int 293 1.1 riastrad pci_bus_write_config_dword(struct pci_bus *bus, unsigned devfn, int reg, 294 1.1 riastrad uint32_t value) 295 1.1 riastrad { 296 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), 297 1.1 riastrad PCI_FUNC(devfn)); 298 1.1 riastrad 299 1.1 riastrad KASSERT(!ISSET(reg, 3)); 300 1.1 riastrad pci_conf_write(bus->pb_pc, tag, reg, value); 301 1.1 riastrad return 0; 302 1.1 riastrad } 303 1.1 riastrad 304 1.1 riastrad static void 305 1.1 riastrad pci_rmw_config(pci_chipset_tag_t pc, pcitag_t tag, int reg, unsigned int bytes, 306 1.1 riastrad uint32_t value) 307 1.1 riastrad { 308 1.1 riastrad const uint32_t mask = ~((~0UL) << (8 * bytes)); 309 1.1 riastrad const int reg32 = (reg &~ 3); 310 1.1 riastrad const unsigned int shift = (8 * (reg & 3)); 311 1.1 riastrad uint32_t value32; 312 1.1 riastrad 313 1.1 riastrad KASSERT(bytes <= 4); 314 1.1 riastrad KASSERT(!ISSET(value, ~mask)); 315 1.1 riastrad value32 = pci_conf_read(pc, tag, reg32); 316 1.1 riastrad value32 &=~ (mask << shift); 317 1.1 riastrad value32 |= (value << shift); 318 1.1 riastrad pci_conf_write(pc, tag, reg32, value32); 319 1.1 riastrad } 320 1.1 riastrad 321 1.1 riastrad int 322 1.1 riastrad pci_write_config_word(struct pci_dev *pdev, int reg, uint16_t value) 323 1.1 riastrad { 324 1.1 riastrad 325 1.1 riastrad KASSERT(!ISSET(reg, 1)); 326 1.1 riastrad pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 2, value); 327 1.1 riastrad return 0; 328 1.1 riastrad } 329 1.1 riastrad 330 1.1 riastrad int 331 1.1 riastrad pci_write_config_byte(struct pci_dev *pdev, int reg, uint8_t value) 332 1.1 riastrad { 333 1.1 riastrad 334 1.1 riastrad pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 1, value); 335 1.1 riastrad return 0; 336 1.1 riastrad } 337 1.1 riastrad 338 1.1 riastrad int 339 1.1 riastrad pci_bus_write_config_word(struct pci_bus *bus, unsigned devfn, int reg, 340 1.1 riastrad uint16_t value) 341 1.1 riastrad { 342 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), 343 1.1 riastrad PCI_FUNC(devfn)); 344 1.1 riastrad 345 1.1 riastrad KASSERT(!ISSET(reg, 1)); 346 1.1 riastrad pci_rmw_config(bus->pb_pc, tag, reg, 2, value); 347 1.1 riastrad return 0; 348 1.1 riastrad } 349 1.1 riastrad 350 1.1 riastrad int 351 1.1 riastrad pci_bus_write_config_byte(struct pci_bus *bus, unsigned devfn, int reg, 352 1.1 riastrad uint8_t value) 353 1.1 riastrad { 354 1.1 riastrad pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), 355 1.1 riastrad PCI_FUNC(devfn)); 356 1.1 riastrad 357 1.1 riastrad pci_rmw_config(bus->pb_pc, tag, reg, 1, value); 358 1.1 riastrad return 0; 359 1.1 riastrad } 360 1.1 riastrad 361 1.1 riastrad int 362 1.1 riastrad pci_enable_msi(struct pci_dev *pdev) 363 1.1 riastrad { 364 1.1 riastrad const struct pci_attach_args *const pa = &pdev->pd_pa; 365 1.1 riastrad 366 1.1 riastrad if (pci_msi_alloc_exact(pa, &pdev->pd_intr_handles, 1)) 367 1.1 riastrad return -EINVAL; 368 1.1 riastrad 369 1.1 riastrad pdev->msi_enabled = 1; 370 1.1 riastrad return 0; 371 1.1 riastrad } 372 1.1 riastrad 373 1.1 riastrad void 374 1.1 riastrad pci_disable_msi(struct pci_dev *pdev __unused) 375 1.1 riastrad { 376 1.1 riastrad const struct pci_attach_args *const pa = &pdev->pd_pa; 377 1.1 riastrad 378 1.1 riastrad if (pdev->pd_intr_handles != NULL) { 379 1.1 riastrad pci_intr_release(pa->pa_pc, pdev->pd_intr_handles, 1); 380 1.1 riastrad pdev->pd_intr_handles = NULL; 381 1.1 riastrad } 382 1.1 riastrad pdev->msi_enabled = 0; 383 1.1 riastrad } 384 1.1 riastrad 385 1.1 riastrad void 386 1.1 riastrad pci_set_master(struct pci_dev *pdev) 387 1.1 riastrad { 388 1.1 riastrad pcireg_t csr; 389 1.1 riastrad 390 1.1 riastrad csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 391 1.1 riastrad PCI_COMMAND_STATUS_REG); 392 1.1 riastrad csr |= PCI_COMMAND_MASTER_ENABLE; 393 1.1 riastrad pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 394 1.1 riastrad PCI_COMMAND_STATUS_REG, csr); 395 1.1 riastrad } 396 1.1 riastrad 397 1.1 riastrad void 398 1.1 riastrad pci_clear_master(struct pci_dev *pdev) 399 1.1 riastrad { 400 1.1 riastrad pcireg_t csr; 401 1.1 riastrad 402 1.1 riastrad csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 403 1.1 riastrad PCI_COMMAND_STATUS_REG); 404 1.1 riastrad csr &= ~(pcireg_t)PCI_COMMAND_MASTER_ENABLE; 405 1.1 riastrad pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 406 1.1 riastrad PCI_COMMAND_STATUS_REG, csr); 407 1.1 riastrad } 408 1.1 riastrad 409 1.25 mrg int 410 1.25 mrg pcie_capability_read_dword(struct pci_dev *pdev, int reg, uint32_t *valuep) 411 1.25 mrg { 412 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc; 413 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag; 414 1.25 mrg int off; 415 1.25 mrg 416 1.25 mrg *valuep = 0; 417 1.25 mrg 418 1.25 mrg /* Must have capabilities. */ 419 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0) 420 1.25 mrg return 1; 421 1.25 mrg 422 1.25 mrg *valuep = pci_conf_read(pc, tag, off + reg); 423 1.25 mrg 424 1.25 mrg return 0; 425 1.25 mrg } 426 1.25 mrg 427 1.25 mrg int 428 1.25 mrg pcie_capability_read_word(struct pci_dev *pdev, int reg, uint16_t *valuep) 429 1.25 mrg { 430 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc; 431 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag; 432 1.25 mrg int off; 433 1.25 mrg 434 1.25 mrg *valuep = 0; 435 1.25 mrg 436 1.25 mrg /* Must have capabilities. */ 437 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0) 438 1.25 mrg return 1; 439 1.25 mrg 440 1.25 mrg *valuep = pci_conf_read(pc, tag, off + (reg &~ 2)) >> (8 * (reg & 2)); 441 1.25 mrg 442 1.25 mrg return 0; 443 1.25 mrg } 444 1.25 mrg 445 1.25 mrg int 446 1.25 mrg pcie_capability_write_dword(struct pci_dev *pdev, int reg, uint32_t value) 447 1.25 mrg { 448 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc; 449 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag; 450 1.25 mrg int off; 451 1.25 mrg 452 1.25 mrg /* Must have capabilities. */ 453 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0) 454 1.25 mrg return 1; 455 1.25 mrg 456 1.25 mrg pci_conf_write(pc, tag, off + reg, value); 457 1.25 mrg 458 1.25 mrg return 0; 459 1.25 mrg } 460 1.25 mrg 461 1.25 mrg int 462 1.25 mrg pcie_capability_write_word(struct pci_dev *pdev, int reg, uint16_t value) 463 1.25 mrg { 464 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc; 465 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag; 466 1.25 mrg int off; 467 1.25 mrg 468 1.25 mrg /* Must have capabilities. */ 469 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0) 470 1.25 mrg return 1; 471 1.25 mrg 472 1.25 mrg pci_rmw_config(pc, tag, off + reg, 2, value); 473 1.25 mrg 474 1.25 mrg return 0; 475 1.25 mrg } 476 1.25 mrg 477 1.25 mrg /* From PCIe 5.0 7.5.3.4 "Device Control Register" */ 478 1.25 mrg static const unsigned readrqmax[] = { 479 1.25 mrg 128, 480 1.25 mrg 256, 481 1.25 mrg 512, 482 1.25 mrg 1024, 483 1.25 mrg 2048, 484 1.25 mrg 4096, 485 1.25 mrg }; 486 1.25 mrg 487 1.25 mrg int 488 1.25 mrg pcie_get_readrq(struct pci_dev *pdev) 489 1.25 mrg { 490 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc; 491 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag; 492 1.25 mrg unsigned val; 493 1.25 mrg int off; 494 1.25 mrg 495 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0) 496 1.25 mrg return -EINVAL; /* XXX NetBSD->Linux */ 497 1.25 mrg 498 1.25 mrg val = __SHIFTOUT(pci_conf_read(pc, tag, off + PCIE_DCSR), 499 1.25 mrg PCIE_DCSR_MAX_READ_REQ); 500 1.25 mrg 501 1.25 mrg if (val >= __arraycount(readrqmax)) 502 1.25 mrg val = 0; 503 1.25 mrg return readrqmax[val]; 504 1.25 mrg } 505 1.25 mrg 506 1.25 mrg int 507 1.25 mrg pcie_set_readrq(struct pci_dev *pdev, int val) 508 1.25 mrg { 509 1.25 mrg pci_chipset_tag_t pc = pdev->pd_pa.pa_pc; 510 1.25 mrg pcitag_t tag = pdev->pd_pa.pa_tag; 511 1.25 mrg pcireg_t reg, newval = 0; 512 1.25 mrg unsigned i; 513 1.25 mrg int off; 514 1.25 mrg 515 1.25 mrg for (i = 0; i < __arraycount(readrqmax); i++) { 516 1.25 mrg if (readrqmax[i] == val) { 517 1.25 mrg newval = i; 518 1.25 mrg break; 519 1.25 mrg } 520 1.25 mrg } 521 1.25 mrg 522 1.25 mrg if (i == __arraycount(readrqmax)) 523 1.25 mrg return -EINVAL; 524 1.25 mrg 525 1.25 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0) 526 1.25 mrg return -EINVAL; /* XXX NetBSD->Linux */ 527 1.25 mrg 528 1.25 mrg reg = pci_conf_read(pc, tag, off + PCIE_DCSR); 529 1.25 mrg reg &= ~PCIE_DCSR_MAX_READ_REQ | (newval << 12); 530 1.25 mrg pci_conf_write(pc, tag, off + PCIE_DCSR, reg); 531 1.25 mrg 532 1.25 mrg return 0; 533 1.25 mrg } 534 1.25 mrg 535 1.1 riastrad bus_addr_t 536 1.1 riastrad pcibios_align_resource(void *p, const struct resource *resource, 537 1.1 riastrad bus_addr_t addr, bus_size_t size) 538 1.1 riastrad { 539 1.1 riastrad panic("pcibios_align_resource has accessed unaligned neurons!"); 540 1.1 riastrad } 541 1.1 riastrad 542 1.1 riastrad int 543 1.1 riastrad pci_bus_alloc_resource(struct pci_bus *bus, struct resource *resource, 544 1.1 riastrad bus_size_t size, bus_size_t align, bus_addr_t start, int type __unused, 545 1.1 riastrad bus_addr_t (*align_fn)(void *, const struct resource *, bus_addr_t, 546 1.1 riastrad bus_size_t) __unused, 547 1.1 riastrad struct pci_dev *pdev) 548 1.1 riastrad { 549 1.1 riastrad const struct pci_attach_args *const pa = &pdev->pd_pa; 550 1.1 riastrad bus_space_tag_t bst; 551 1.1 riastrad int error; 552 1.1 riastrad 553 1.1 riastrad switch (resource->flags) { 554 1.1 riastrad case IORESOURCE_MEM: 555 1.1 riastrad bst = pa->pa_memt; 556 1.1 riastrad break; 557 1.1 riastrad 558 1.1 riastrad case IORESOURCE_IO: 559 1.1 riastrad bst = pa->pa_iot; 560 1.1 riastrad break; 561 1.1 riastrad 562 1.1 riastrad default: 563 1.1 riastrad panic("I don't know what kind of resource you want!"); 564 1.1 riastrad } 565 1.1 riastrad 566 1.1 riastrad resource->r_bst = bst; 567 1.1 riastrad error = bus_space_alloc(bst, start, __type_max(bus_addr_t), 568 1.1 riastrad size, align, 0, 0, &resource->start, &resource->r_bsh); 569 1.1 riastrad if (error) 570 1.1 riastrad return error; 571 1.1 riastrad 572 1.13 riastrad resource->end = start + (size - 1); 573 1.1 riastrad return 0; 574 1.1 riastrad } 575 1.1 riastrad 576 1.29 riastrad struct pci_domain_bus_and_slot { 577 1.29 riastrad int domain, bus, slot; 578 1.29 riastrad }; 579 1.1 riastrad 580 1.1 riastrad static int 581 1.29 riastrad pci_match_domain_bus_and_slot(void *cookie, const struct pci_attach_args *pa) 582 1.1 riastrad { 583 1.29 riastrad const struct pci_domain_bus_and_slot *C = cookie; 584 1.1 riastrad 585 1.29 riastrad if (pci_get_segment(pa->pa_pc) != C->domain) 586 1.1 riastrad return 0; 587 1.29 riastrad if (pa->pa_bus != C->bus) 588 1.1 riastrad return 0; 589 1.29 riastrad if (PCI_DEVFN(pa->pa_device, pa->pa_function) != C->slot) 590 1.1 riastrad return 0; 591 1.1 riastrad 592 1.1 riastrad return 1; 593 1.1 riastrad } 594 1.1 riastrad 595 1.1 riastrad struct pci_dev * 596 1.11 riastrad pci_get_domain_bus_and_slot(int domain, int bus, int slot) 597 1.1 riastrad { 598 1.1 riastrad struct pci_attach_args pa; 599 1.29 riastrad struct pci_domain_bus_and_slot context = {domain, bus, slot}, 600 1.29 riastrad *C = &context; 601 1.1 riastrad 602 1.29 riastrad if (!pci_find_device1(&pa, &pci_match_domain_bus_and_slot, C)) 603 1.1 riastrad return NULL; 604 1.1 riastrad 605 1.1 riastrad struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP); 606 1.1 riastrad linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE); 607 1.1 riastrad 608 1.1 riastrad return pdev; 609 1.1 riastrad } 610 1.1 riastrad 611 1.29 riastrad void 612 1.29 riastrad pci_dev_put(struct pci_dev *pdev) 613 1.1 riastrad { 614 1.1 riastrad 615 1.29 riastrad if (pdev == NULL) 616 1.29 riastrad return; 617 1.1 riastrad 618 1.29 riastrad KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_GET_MUMBLE)); 619 1.29 riastrad kmem_free(pdev->bus, sizeof(*pdev->bus)); 620 1.29 riastrad kmem_free(pdev, sizeof(*pdev)); 621 1.1 riastrad } 622 1.1 riastrad 623 1.29 riastrad struct pci_get_class_state { 624 1.30 riastrad uint32_t class_subclass_interface; 625 1.29 riastrad const struct pci_dev *from; 626 1.29 riastrad }; 627 1.28 riastrad 628 1.28 riastrad static int 629 1.29 riastrad pci_get_class_match(void *cookie, const struct pci_attach_args *pa) 630 1.28 riastrad { 631 1.29 riastrad struct pci_get_class_state *C = cookie; 632 1.28 riastrad 633 1.29 riastrad if (C->from) { 634 1.29 riastrad if ((pci_get_segment(C->from->pd_pa.pa_pc) == 635 1.29 riastrad pci_get_segment(pa->pa_pc)) && 636 1.29 riastrad C->from->pd_pa.pa_bus == pa->pa_bus && 637 1.29 riastrad C->from->pd_pa.pa_device == pa->pa_device && 638 1.29 riastrad C->from->pd_pa.pa_function == pa->pa_function) 639 1.29 riastrad C->from = NULL; 640 1.28 riastrad return 0; 641 1.29 riastrad } 642 1.30 riastrad if (C->class_subclass_interface != 643 1.30 riastrad (PCI_CLASS(pa->pa_class) << 16 | 644 1.30 riastrad PCI_SUBCLASS(pa->pa_class) << 8 | 645 1.30 riastrad PCI_INTERFACE(pa->pa_class))) 646 1.28 riastrad return 0; 647 1.28 riastrad 648 1.28 riastrad return 1; 649 1.28 riastrad } 650 1.28 riastrad 651 1.29 riastrad struct pci_dev * 652 1.30 riastrad pci_get_class(uint32_t class_subclass_interface, struct pci_dev *from) 653 1.1 riastrad { 654 1.30 riastrad struct pci_get_class_state context = {class_subclass_interface, from}, 655 1.29 riastrad *C = &context; 656 1.1 riastrad struct pci_attach_args pa; 657 1.29 riastrad struct pci_dev *pdev = NULL; 658 1.1 riastrad 659 1.29 riastrad if (!pci_find_device1(&pa, &pci_get_class_match, C)) 660 1.29 riastrad goto out; 661 1.29 riastrad pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP); 662 1.1 riastrad linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE); 663 1.1 riastrad 664 1.29 riastrad out: if (from) 665 1.29 riastrad pci_dev_put(from); 666 1.1 riastrad return pdev; 667 1.1 riastrad } 668 1.1 riastrad 669 1.19 riastrad int 670 1.19 riastrad pci_dev_present(const struct pci_device_id *ids) 671 1.19 riastrad { 672 1.19 riastrad 673 1.19 riastrad /* XXX implement me -- pci_find_device doesn't pass a cookie */ 674 1.19 riastrad return 0; 675 1.19 riastrad } 676 1.19 riastrad 677 1.1 riastrad void 678 1.1 riastrad pci_unmap_rom(struct pci_dev *pdev, void __pci_rom_iomem *vaddr __unused) 679 1.1 riastrad { 680 1.1 riastrad 681 1.1 riastrad /* XXX Disable the ROM address decoder. */ 682 1.1 riastrad KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)); 683 1.1 riastrad KASSERT(vaddr == pdev->pd_rom_vaddr); 684 1.1 riastrad bus_space_unmap(pdev->pd_rom_bst, pdev->pd_rom_bsh, pdev->pd_rom_size); 685 1.1 riastrad pdev->pd_kludges &= ~NBPCI_KLUDGE_MAP_ROM; 686 1.1 riastrad pdev->pd_rom_vaddr = NULL; 687 1.1 riastrad } 688 1.1 riastrad 689 1.1 riastrad /* XXX Whattakludge! Should move this in sys/arch/. */ 690 1.1 riastrad static int 691 1.1 riastrad pci_map_rom_md(struct pci_dev *pdev) 692 1.1 riastrad { 693 1.1 riastrad #if defined(__i386__) || defined(__x86_64__) || defined(__ia64__) 694 1.1 riastrad const bus_addr_t rom_base = 0xc0000; 695 1.1 riastrad const bus_size_t rom_size = 0x20000; 696 1.1 riastrad bus_space_handle_t rom_bsh; 697 1.1 riastrad int error; 698 1.1 riastrad 699 1.1 riastrad if (PCI_CLASS(pdev->pd_pa.pa_class) != PCI_CLASS_DISPLAY) 700 1.1 riastrad return ENXIO; 701 1.1 riastrad if (PCI_SUBCLASS(pdev->pd_pa.pa_class) != PCI_SUBCLASS_DISPLAY_VGA) 702 1.1 riastrad return ENXIO; 703 1.1 riastrad /* XXX Check whether this is the primary VGA card? */ 704 1.1 riastrad error = bus_space_map(pdev->pd_pa.pa_memt, rom_base, rom_size, 705 1.1 riastrad (BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE), &rom_bsh); 706 1.1 riastrad if (error) 707 1.1 riastrad return ENXIO; 708 1.1 riastrad 709 1.1 riastrad pdev->pd_rom_bst = pdev->pd_pa.pa_memt; 710 1.1 riastrad pdev->pd_rom_bsh = rom_bsh; 711 1.1 riastrad pdev->pd_rom_size = rom_size; 712 1.1 riastrad pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM; 713 1.1 riastrad 714 1.1 riastrad return 0; 715 1.1 riastrad #else 716 1.1 riastrad return ENXIO; 717 1.1 riastrad #endif 718 1.1 riastrad } 719 1.1 riastrad 720 1.1 riastrad void __pci_rom_iomem * 721 1.1 riastrad pci_map_rom(struct pci_dev *pdev, size_t *sizep) 722 1.1 riastrad { 723 1.1 riastrad 724 1.1 riastrad KASSERT(!ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)); 725 1.1 riastrad 726 1.1 riastrad if (pci_mapreg_map(&pdev->pd_pa, PCI_MAPREG_ROM, PCI_MAPREG_TYPE_ROM, 727 1.1 riastrad (BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR), 728 1.1 riastrad &pdev->pd_rom_bst, &pdev->pd_rom_bsh, NULL, &pdev->pd_rom_size) 729 1.1 riastrad != 0) 730 1.1 riastrad goto fail_mi; 731 1.1 riastrad pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM; 732 1.1 riastrad 733 1.1 riastrad /* XXX This type is obviously wrong in general... */ 734 1.1 riastrad if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh, 735 1.1 riastrad pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86, 736 1.1 riastrad &pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) { 737 1.1 riastrad pci_unmap_rom(pdev, NULL); 738 1.1 riastrad goto fail_mi; 739 1.1 riastrad } 740 1.1 riastrad goto success; 741 1.1 riastrad 742 1.1 riastrad fail_mi: 743 1.1 riastrad if (pci_map_rom_md(pdev) != 0) 744 1.1 riastrad goto fail_md; 745 1.1 riastrad 746 1.1 riastrad /* XXX This type is obviously wrong in general... */ 747 1.1 riastrad if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh, 748 1.1 riastrad pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86, 749 1.1 riastrad &pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) { 750 1.1 riastrad pci_unmap_rom(pdev, NULL); 751 1.1 riastrad goto fail_md; 752 1.1 riastrad } 753 1.1 riastrad 754 1.1 riastrad success: 755 1.1 riastrad KASSERT(pdev->pd_rom_found_size <= SIZE_T_MAX); 756 1.1 riastrad *sizep = pdev->pd_rom_found_size; 757 1.1 riastrad pdev->pd_rom_vaddr = bus_space_vaddr(pdev->pd_rom_bst, 758 1.1 riastrad pdev->pd_rom_found_bsh); 759 1.1 riastrad return pdev->pd_rom_vaddr; 760 1.1 riastrad 761 1.1 riastrad fail_md: 762 1.1 riastrad return NULL; 763 1.1 riastrad } 764 1.1 riastrad 765 1.1 riastrad void __pci_rom_iomem * 766 1.1 riastrad pci_platform_rom(struct pci_dev *pdev __unused, size_t *sizep) 767 1.1 riastrad { 768 1.1 riastrad 769 1.1 riastrad *sizep = 0; 770 1.1 riastrad return NULL; 771 1.1 riastrad } 772 1.1 riastrad 773 1.1 riastrad int 774 1.1 riastrad pci_enable_rom(struct pci_dev *pdev) 775 1.1 riastrad { 776 1.1 riastrad const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc; 777 1.1 riastrad const pcitag_t tag = pdev->pd_pa.pa_tag; 778 1.1 riastrad pcireg_t addr; 779 1.1 riastrad int s; 780 1.1 riastrad 781 1.1 riastrad /* XXX Don't do anything if the ROM isn't there. */ 782 1.1 riastrad 783 1.1 riastrad s = splhigh(); 784 1.1 riastrad addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM); 785 1.1 riastrad addr |= PCI_MAPREG_ROM_ENABLE; 786 1.1 riastrad pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr); 787 1.1 riastrad splx(s); 788 1.1 riastrad 789 1.1 riastrad return 0; 790 1.1 riastrad } 791 1.1 riastrad 792 1.1 riastrad void 793 1.1 riastrad pci_disable_rom(struct pci_dev *pdev) 794 1.1 riastrad { 795 1.1 riastrad const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc; 796 1.1 riastrad const pcitag_t tag = pdev->pd_pa.pa_tag; 797 1.1 riastrad pcireg_t addr; 798 1.1 riastrad int s; 799 1.1 riastrad 800 1.1 riastrad s = splhigh(); 801 1.1 riastrad addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM); 802 1.1 riastrad addr &= ~(pcireg_t)PCI_MAPREG_ROM_ENABLE; 803 1.1 riastrad pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr); 804 1.1 riastrad splx(s); 805 1.1 riastrad } 806 1.1 riastrad 807 1.1 riastrad bus_addr_t 808 1.1 riastrad pci_resource_start(struct pci_dev *pdev, unsigned i) 809 1.1 riastrad { 810 1.1 riastrad 811 1.26 mrg if (i >= PCI_NUM_RESOURCES) 812 1.26 mrg panic("resource %d >= max %d", i, PCI_NUM_RESOURCES); 813 1.1 riastrad return pdev->pd_resources[i].addr; 814 1.1 riastrad } 815 1.1 riastrad 816 1.1 riastrad bus_size_t 817 1.1 riastrad pci_resource_len(struct pci_dev *pdev, unsigned i) 818 1.1 riastrad { 819 1.1 riastrad 820 1.26 mrg if (i >= PCI_NUM_RESOURCES) 821 1.26 mrg panic("resource %d >= max %d", i, PCI_NUM_RESOURCES); 822 1.1 riastrad return pdev->pd_resources[i].size; 823 1.1 riastrad } 824 1.1 riastrad 825 1.1 riastrad bus_addr_t 826 1.1 riastrad pci_resource_end(struct pci_dev *pdev, unsigned i) 827 1.1 riastrad { 828 1.1 riastrad 829 1.1 riastrad return pci_resource_start(pdev, i) + (pci_resource_len(pdev, i) - 1); 830 1.1 riastrad } 831 1.1 riastrad 832 1.1 riastrad int 833 1.1 riastrad pci_resource_flags(struct pci_dev *pdev, unsigned i) 834 1.1 riastrad { 835 1.1 riastrad 836 1.26 mrg if (i >= PCI_NUM_RESOURCES) 837 1.26 mrg panic("resource %d >= max %d", i, PCI_NUM_RESOURCES); 838 1.1 riastrad return pdev->pd_resources[i].flags; 839 1.1 riastrad } 840 1.1 riastrad 841 1.1 riastrad void __pci_iomem * 842 1.1 riastrad pci_iomap(struct pci_dev *pdev, unsigned i, bus_size_t size) 843 1.1 riastrad { 844 1.1 riastrad int error; 845 1.1 riastrad 846 1.1 riastrad KASSERT(i < PCI_NUM_RESOURCES); 847 1.1 riastrad KASSERT(pdev->pd_resources[i].kva == NULL); 848 1.1 riastrad 849 1.1 riastrad if (PCI_MAPREG_TYPE(pdev->pd_resources[i].type) != PCI_MAPREG_TYPE_MEM) 850 1.1 riastrad return NULL; 851 1.1 riastrad if (pdev->pd_resources[i].size < size) 852 1.1 riastrad return NULL; 853 1.1 riastrad error = bus_space_map(pdev->pd_pa.pa_memt, pdev->pd_resources[i].addr, 854 1.1 riastrad size, BUS_SPACE_MAP_LINEAR | pdev->pd_resources[i].flags, 855 1.1 riastrad &pdev->pd_resources[i].bsh); 856 1.6 riastrad if (error) 857 1.5 riastrad return NULL; 858 1.1 riastrad pdev->pd_resources[i].bst = pdev->pd_pa.pa_memt; 859 1.1 riastrad pdev->pd_resources[i].kva = bus_space_vaddr(pdev->pd_resources[i].bst, 860 1.1 riastrad pdev->pd_resources[i].bsh); 861 1.1 riastrad pdev->pd_resources[i].mapped = true; 862 1.1 riastrad 863 1.1 riastrad return pdev->pd_resources[i].kva; 864 1.1 riastrad } 865 1.1 riastrad 866 1.1 riastrad void 867 1.1 riastrad pci_iounmap(struct pci_dev *pdev, void __pci_iomem *kva) 868 1.1 riastrad { 869 1.1 riastrad unsigned i; 870 1.1 riastrad 871 1.1 riastrad CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES); 872 1.1 riastrad for (i = 0; i < PCI_NUM_RESOURCES; i++) { 873 1.1 riastrad if (pdev->pd_resources[i].kva == kva) 874 1.1 riastrad break; 875 1.1 riastrad } 876 1.1 riastrad KASSERT(i < PCI_NUM_RESOURCES); 877 1.1 riastrad 878 1.1 riastrad pdev->pd_resources[i].kva = NULL; 879 1.1 riastrad bus_space_unmap(pdev->pd_resources[i].bst, pdev->pd_resources[i].bsh, 880 1.1 riastrad pdev->pd_resources[i].size); 881 1.1 riastrad } 882 1.1 riastrad 883 1.1 riastrad void 884 1.1 riastrad pci_save_state(struct pci_dev *pdev) 885 1.1 riastrad { 886 1.1 riastrad 887 1.1 riastrad KASSERT(pdev->pd_saved_state == NULL); 888 1.1 riastrad pdev->pd_saved_state = kmem_alloc(sizeof(*pdev->pd_saved_state), 889 1.1 riastrad KM_SLEEP); 890 1.1 riastrad pci_conf_capture(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 891 1.1 riastrad pdev->pd_saved_state); 892 1.1 riastrad } 893 1.1 riastrad 894 1.1 riastrad void 895 1.1 riastrad pci_restore_state(struct pci_dev *pdev) 896 1.1 riastrad { 897 1.1 riastrad 898 1.1 riastrad KASSERT(pdev->pd_saved_state != NULL); 899 1.1 riastrad pci_conf_restore(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 900 1.1 riastrad pdev->pd_saved_state); 901 1.1 riastrad kmem_free(pdev->pd_saved_state, sizeof(*pdev->pd_saved_state)); 902 1.1 riastrad pdev->pd_saved_state = NULL; 903 1.1 riastrad } 904 1.1 riastrad 905 1.1 riastrad bool 906 1.1 riastrad pci_is_pcie(struct pci_dev *pdev) 907 1.1 riastrad { 908 1.1 riastrad 909 1.1 riastrad return (pci_find_capability(pdev, PCI_CAP_PCIEXPRESS) != 0); 910 1.1 riastrad } 911 1.1 riastrad 912 1.1 riastrad bool 913 1.1 riastrad pci_dma_supported(struct pci_dev *pdev, uintmax_t mask) 914 1.1 riastrad { 915 1.1 riastrad 916 1.1 riastrad /* XXX Cop-out. */ 917 1.1 riastrad if (mask > DMA_BIT_MASK(32)) 918 1.1 riastrad return pci_dma64_available(&pdev->pd_pa); 919 1.1 riastrad else 920 1.1 riastrad return true; 921 1.1 riastrad } 922 1.1 riastrad 923 1.1 riastrad bool 924 1.14 riastrad pci_is_thunderbolt_attached(struct pci_dev *pdev) 925 1.14 riastrad { 926 1.14 riastrad 927 1.14 riastrad /* XXX Cop-out. */ 928 1.14 riastrad return false; 929 1.14 riastrad } 930 1.14 riastrad 931 1.14 riastrad bool 932 1.1 riastrad pci_is_root_bus(struct pci_bus *bus) 933 1.1 riastrad { 934 1.1 riastrad 935 1.24 mrg return bus->number == 0; 936 1.1 riastrad } 937 1.1 riastrad 938 1.1 riastrad int 939 1.1 riastrad pci_domain_nr(struct pci_bus *bus) 940 1.1 riastrad { 941 1.1 riastrad 942 1.23 riastrad return pci_get_segment(bus->pb_pc); 943 1.1 riastrad } 944 1.1 riastrad 945 1.1 riastrad /* 946 1.1 riastrad * We explicitly rename pci_enable/disable_device so that you have to 947 1.1 riastrad * review each use of them, since NetBSD's PCI API does _not_ respect 948 1.1 riastrad * our local enablecnt here, but there are different parts of NetBSD 949 1.1 riastrad * that automatically enable/disable like PMF, so you have to decide 950 1.1 riastrad * for each one whether to call it or not. 951 1.1 riastrad */ 952 1.1 riastrad 953 1.1 riastrad int 954 1.1 riastrad linux_pci_enable_device(struct pci_dev *pdev) 955 1.1 riastrad { 956 1.1 riastrad const struct pci_attach_args *pa = &pdev->pd_pa; 957 1.1 riastrad pcireg_t csr; 958 1.1 riastrad int s; 959 1.1 riastrad 960 1.1 riastrad if (pdev->pd_enablecnt++) 961 1.1 riastrad return 0; 962 1.1 riastrad 963 1.1 riastrad s = splhigh(); 964 1.1 riastrad csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 965 1.4 riastrad /* If someone else (firmware) already enabled it, credit them. */ 966 1.4 riastrad if (csr & (PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE)) 967 1.4 riastrad pdev->pd_enablecnt++; 968 1.1 riastrad csr |= PCI_COMMAND_IO_ENABLE; 969 1.1 riastrad csr |= PCI_COMMAND_MEM_ENABLE; 970 1.1 riastrad pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr); 971 1.1 riastrad splx(s); 972 1.1 riastrad 973 1.1 riastrad return 0; 974 1.1 riastrad } 975 1.1 riastrad 976 1.1 riastrad void 977 1.1 riastrad linux_pci_disable_device(struct pci_dev *pdev) 978 1.1 riastrad { 979 1.1 riastrad const struct pci_attach_args *pa = &pdev->pd_pa; 980 1.1 riastrad pcireg_t csr; 981 1.1 riastrad int s; 982 1.1 riastrad 983 1.1 riastrad if (--pdev->pd_enablecnt) 984 1.1 riastrad return; 985 1.1 riastrad 986 1.1 riastrad s = splhigh(); 987 1.1 riastrad csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 988 1.1 riastrad csr &= ~PCI_COMMAND_IO_ENABLE; 989 1.1 riastrad csr &= ~PCI_COMMAND_MEM_ENABLE; 990 1.1 riastrad pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr); 991 1.1 riastrad splx(s); 992 1.1 riastrad } 993 1.1 riastrad 994 1.1 riastrad void 995 1.1 riastrad linux_pci_dev_destroy(struct pci_dev *pdev) 996 1.1 riastrad { 997 1.1 riastrad unsigned i; 998 1.1 riastrad 999 1.25 mrg if (pdev->bus->self != NULL) { 1000 1.25 mrg kmem_free(pdev->bus->self, sizeof(*pdev->bus->self)); 1001 1.25 mrg } 1002 1.1 riastrad if (pdev->bus != NULL) { 1003 1.1 riastrad kmem_free(pdev->bus, sizeof(*pdev->bus)); 1004 1.1 riastrad pdev->bus = NULL; 1005 1.1 riastrad } 1006 1.1 riastrad if (ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)) { 1007 1.1 riastrad pci_unmap_rom(pdev, pdev->pd_rom_vaddr); 1008 1.1 riastrad pdev->pd_rom_vaddr = 0; 1009 1.1 riastrad } 1010 1.1 riastrad for (i = 0; i < __arraycount(pdev->pd_resources); i++) { 1011 1.1 riastrad if (!pdev->pd_resources[i].mapped) 1012 1.1 riastrad continue; 1013 1.1 riastrad bus_space_unmap(pdev->pd_resources[i].bst, 1014 1.1 riastrad pdev->pd_resources[i].bsh, pdev->pd_resources[i].size); 1015 1.1 riastrad } 1016 1.1 riastrad 1017 1.1 riastrad /* There is no way these should be still in use. */ 1018 1.1 riastrad KASSERT(pdev->pd_saved_state == NULL); 1019 1.1 riastrad KASSERT(pdev->pd_intr_handles == NULL); 1020 1.1 riastrad } 1021 1.24 mrg 1022 1.24 mrg enum pci_bus_speed 1023 1.24 mrg pcie_get_speed_cap(struct pci_dev *dev) 1024 1.24 mrg { 1025 1.24 mrg pci_chipset_tag_t pc = dev->pd_pa.pa_pc; 1026 1.24 mrg pcitag_t tag = dev->pd_pa.pa_tag; 1027 1.24 mrg pcireg_t lcap, lcap2, xcap; 1028 1.24 mrg int off; 1029 1.24 mrg 1030 1.24 mrg /* Must have capabilities. */ 1031 1.24 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0) 1032 1.24 mrg return PCI_SPEED_UNKNOWN; 1033 1.24 mrg 1034 1.24 mrg /* Only PCIe 3.x has LCAP2. */ 1035 1.24 mrg xcap = pci_conf_read(pc, tag, off + PCIE_XCAP); 1036 1.24 mrg if (__SHIFTOUT(xcap, PCIE_XCAP_VER_MASK) >= 2) { 1037 1.24 mrg lcap2 = pci_conf_read(pc, tag, off + PCIE_LCAP2); 1038 1.24 mrg if (lcap2) { 1039 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS64) != 0) { 1040 1.24 mrg return PCIE_SPEED_64_0GT; 1041 1.24 mrg } 1042 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS32) != 0) { 1043 1.24 mrg return PCIE_SPEED_32_0GT; 1044 1.24 mrg } 1045 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS16) != 0) { 1046 1.24 mrg return PCIE_SPEED_16_0GT; 1047 1.24 mrg } 1048 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS8) != 0) { 1049 1.24 mrg return PCIE_SPEED_8_0GT; 1050 1.24 mrg } 1051 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS5) != 0) { 1052 1.24 mrg return PCIE_SPEED_5_0GT; 1053 1.24 mrg } 1054 1.24 mrg if ((lcap2 & PCIE_LCAP2_SUP_LNKS2) != 0) { 1055 1.24 mrg return PCIE_SPEED_2_5GT; 1056 1.24 mrg } 1057 1.24 mrg } 1058 1.24 mrg } 1059 1.24 mrg 1060 1.24 mrg lcap = pci_conf_read(pc, tag, off + PCIE_LCAP); 1061 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_64) { 1062 1.24 mrg return PCIE_SPEED_64_0GT; 1063 1.24 mrg } 1064 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_32) { 1065 1.24 mrg return PCIE_SPEED_32_0GT; 1066 1.24 mrg } 1067 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_16) { 1068 1.24 mrg return PCIE_SPEED_16_0GT; 1069 1.24 mrg } 1070 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_8) { 1071 1.24 mrg return PCIE_SPEED_8_0GT; 1072 1.24 mrg } 1073 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_5) { 1074 1.24 mrg return PCIE_SPEED_5_0GT; 1075 1.24 mrg } 1076 1.24 mrg if ((lcap & PCIE_LCAP_MAX_SPEED) == PCIE_LCAP_MAX_SPEED_2) { 1077 1.24 mrg return PCIE_SPEED_2_5GT; 1078 1.24 mrg } 1079 1.24 mrg 1080 1.24 mrg return PCI_SPEED_UNKNOWN; 1081 1.24 mrg } 1082 1.24 mrg 1083 1.24 mrg /* 1084 1.24 mrg * This should walk the tree, it only checks this device currently. 1085 1.24 mrg * It also does not write to limiting_dev (the only caller in drm2 1086 1.24 mrg * currently does not use it.) 1087 1.24 mrg */ 1088 1.24 mrg unsigned 1089 1.24 mrg pcie_bandwidth_available(struct pci_dev *dev, 1090 1.24 mrg struct pci_dev **limiting_dev, 1091 1.24 mrg enum pci_bus_speed *speed, 1092 1.24 mrg enum pcie_link_width *width) 1093 1.24 mrg { 1094 1.24 mrg pci_chipset_tag_t pc = dev->pd_pa.pa_pc; 1095 1.24 mrg pcitag_t tag = dev->pd_pa.pa_tag; 1096 1.24 mrg pcireg_t lcsr; 1097 1.24 mrg unsigned per_line_speed, num_lanes; 1098 1.24 mrg int off; 1099 1.24 mrg 1100 1.24 mrg /* Must have capabilities. */ 1101 1.24 mrg if (pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, &off, NULL) == 0) 1102 1.24 mrg return 0; 1103 1.24 mrg 1104 1.24 mrg if (speed) 1105 1.24 mrg *speed = PCI_SPEED_UNKNOWN; 1106 1.24 mrg if (width) 1107 1.24 mrg *width = 0; 1108 1.24 mrg 1109 1.24 mrg lcsr = pci_conf_read(pc, tag, off + PCIE_LCSR); 1110 1.24 mrg 1111 1.24 mrg switch (lcsr & PCIE_LCSR_NLW) { 1112 1.24 mrg case PCIE_LCSR_NLW_X1: 1113 1.24 mrg case PCIE_LCSR_NLW_X2: 1114 1.24 mrg case PCIE_LCSR_NLW_X4: 1115 1.24 mrg case PCIE_LCSR_NLW_X8: 1116 1.24 mrg case PCIE_LCSR_NLW_X12: 1117 1.24 mrg case PCIE_LCSR_NLW_X16: 1118 1.24 mrg case PCIE_LCSR_NLW_X32: 1119 1.24 mrg num_lanes = __SHIFTOUT(lcsr, PCIE_LCSR_NLW); 1120 1.24 mrg if (width) 1121 1.24 mrg *width = num_lanes; 1122 1.24 mrg break; 1123 1.24 mrg default: 1124 1.24 mrg num_lanes = 0; 1125 1.24 mrg break; 1126 1.24 mrg } 1127 1.24 mrg 1128 1.24 mrg switch (__SHIFTOUT(lcsr, PCIE_LCSR_LINKSPEED)) { 1129 1.24 mrg case PCIE_LCSR_LINKSPEED_2: 1130 1.24 mrg *speed = PCIE_SPEED_2_5GT; 1131 1.24 mrg per_line_speed = 2500 * 8 / 10; 1132 1.24 mrg break; 1133 1.24 mrg case PCIE_LCSR_LINKSPEED_5: 1134 1.24 mrg *speed = PCIE_SPEED_5_0GT; 1135 1.24 mrg per_line_speed = 5000 * 8 / 10; 1136 1.24 mrg break; 1137 1.24 mrg case PCIE_LCSR_LINKSPEED_8: 1138 1.24 mrg *speed = PCIE_SPEED_8_0GT; 1139 1.24 mrg per_line_speed = 8000 * 128 / 130; 1140 1.24 mrg break; 1141 1.24 mrg case PCIE_LCSR_LINKSPEED_16: 1142 1.24 mrg *speed = PCIE_SPEED_16_0GT; 1143 1.24 mrg per_line_speed = 16000 * 128 / 130; 1144 1.24 mrg break; 1145 1.24 mrg case PCIE_LCSR_LINKSPEED_32: 1146 1.24 mrg *speed = PCIE_SPEED_32_0GT; 1147 1.24 mrg per_line_speed = 32000 * 128 / 130; 1148 1.24 mrg break; 1149 1.24 mrg case PCIE_LCSR_LINKSPEED_64: 1150 1.24 mrg *speed = PCIE_SPEED_64_0GT; 1151 1.24 mrg per_line_speed = 64000 * 128 / 130; 1152 1.24 mrg break; 1153 1.24 mrg default: 1154 1.24 mrg per_line_speed = 0; 1155 1.24 mrg } 1156 1.24 mrg 1157 1.24 mrg return num_lanes * per_line_speed; 1158 1.24 mrg } 1159