1 /* $NetBSD: linux_pci.c,v 1.1 2018/08/27 14:16:38 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Taylor R. Campbell. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: linux_pci.c,v 1.1 2018/08/27 14:16:38 riastradh Exp $"); 34 35 #include <linux/pci.h> 36 37 device_t 38 pci_dev_dev(struct pci_dev *pdev) 39 { 40 41 return pdev->pd_dev; 42 } 43 44 /* XXX Nouveau kludge! */ 45 struct drm_device * 46 pci_get_drvdata(struct pci_dev *pdev) 47 { 48 49 return pdev->pd_drm_dev; 50 } 51 52 void 53 linux_pci_dev_init(struct pci_dev *pdev, device_t dev, device_t parent, 54 const struct pci_attach_args *pa, int kludges) 55 { 56 const uint32_t subsystem_id = pci_conf_read(pa->pa_pc, pa->pa_tag, 57 PCI_SUBSYS_ID_REG); 58 unsigned i; 59 60 pdev->pd_pa = *pa; 61 pdev->pd_kludges = kludges; 62 pdev->pd_rom_vaddr = NULL; 63 pdev->pd_dev = dev; 64 #if (NACPICA > 0) 65 pdev->pd_ad = acpi_pcidev_find(0 /*XXX segment*/, pa->pa_bus, 66 pa->pa_device, pa->pa_function); 67 #else 68 pdev->pd_ad = NULL; 69 #endif 70 pdev->pd_saved_state = NULL; 71 pdev->pd_intr_handles = NULL; 72 pdev->bus = kmem_zalloc(sizeof(*pdev->bus), KM_NOSLEEP); 73 pdev->bus->pb_pc = pa->pa_pc; 74 pdev->bus->pb_dev = parent; 75 pdev->bus->number = pa->pa_bus; 76 pdev->devfn = PCI_DEVFN(pa->pa_device, pa->pa_function); 77 pdev->vendor = PCI_VENDOR(pa->pa_id); 78 pdev->device = PCI_PRODUCT(pa->pa_id); 79 pdev->subsystem_vendor = PCI_SUBSYS_VENDOR(subsystem_id); 80 pdev->subsystem_device = PCI_SUBSYS_ID(subsystem_id); 81 pdev->revision = PCI_REVISION(pa->pa_class); 82 pdev->class = __SHIFTOUT(pa->pa_class, 0xffffff00UL); /* ? */ 83 84 CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES); 85 for (i = 0; i < PCI_NUM_RESOURCES; i++) { 86 const int reg = PCI_BAR(i); 87 88 pdev->pd_resources[i].type = pci_mapreg_type(pa->pa_pc, 89 pa->pa_tag, reg); 90 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, 91 pdev->pd_resources[i].type, 92 &pdev->pd_resources[i].addr, 93 &pdev->pd_resources[i].size, 94 &pdev->pd_resources[i].flags)) { 95 pdev->pd_resources[i].addr = 0; 96 pdev->pd_resources[i].size = 0; 97 pdev->pd_resources[i].flags = 0; 98 } 99 pdev->pd_resources[i].kva = NULL; 100 } 101 } 102 103 int 104 pci_find_capability(struct pci_dev *pdev, int cap) 105 { 106 107 return pci_get_capability(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, cap, 108 NULL, NULL); 109 } 110 111 int 112 pci_read_config_dword(struct pci_dev *pdev, int reg, uint32_t *valuep) 113 { 114 115 KASSERT(!ISSET(reg, 3)); 116 *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg); 117 return 0; 118 } 119 120 int 121 pci_read_config_word(struct pci_dev *pdev, int reg, uint16_t *valuep) 122 { 123 124 KASSERT(!ISSET(reg, 1)); 125 *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 126 (reg &~ 2)) >> (8 * (reg & 2)); 127 return 0; 128 } 129 130 int 131 pci_read_config_byte(struct pci_dev *pdev, int reg, uint8_t *valuep) 132 { 133 134 *valuep = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 135 (reg &~ 3)) >> (8 * (reg & 3)); 136 return 0; 137 } 138 139 int 140 pci_write_config_dword(struct pci_dev *pdev, int reg, uint32_t value) 141 { 142 143 KASSERT(!ISSET(reg, 3)); 144 pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, value); 145 return 0; 146 } 147 148 int 149 pci_bus_read_config_dword(struct pci_bus *bus, unsigned devfn, int reg, 150 uint32_t *valuep) 151 { 152 pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), 153 PCI_FUNC(devfn)); 154 155 KASSERT(!ISSET(reg, 1)); 156 *valuep = pci_conf_read(bus->pb_pc, tag, reg & ~3) >> (8 * (reg & 3)); 157 return 0; 158 } 159 160 int 161 pci_bus_read_config_word(struct pci_bus *bus, unsigned devfn, int reg, 162 uint16_t *valuep) 163 { 164 pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), 165 PCI_FUNC(devfn)); 166 167 KASSERT(!ISSET(reg, 1)); 168 *valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 2) >> (8 * (reg & 2)); 169 return 0; 170 } 171 172 int 173 pci_bus_read_config_byte(struct pci_bus *bus, unsigned devfn, int reg, 174 uint8_t *valuep) 175 { 176 pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), 177 PCI_FUNC(devfn)); 178 179 *valuep = pci_conf_read(bus->pb_pc, tag, reg &~ 3) >> (8 * (reg & 3)); 180 return 0; 181 } 182 183 int 184 pci_bus_write_config_dword(struct pci_bus *bus, unsigned devfn, int reg, 185 uint32_t value) 186 { 187 pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), 188 PCI_FUNC(devfn)); 189 190 KASSERT(!ISSET(reg, 3)); 191 pci_conf_write(bus->pb_pc, tag, reg, value); 192 return 0; 193 } 194 195 static void 196 pci_rmw_config(pci_chipset_tag_t pc, pcitag_t tag, int reg, unsigned int bytes, 197 uint32_t value) 198 { 199 const uint32_t mask = ~((~0UL) << (8 * bytes)); 200 const int reg32 = (reg &~ 3); 201 const unsigned int shift = (8 * (reg & 3)); 202 uint32_t value32; 203 204 KASSERT(bytes <= 4); 205 KASSERT(!ISSET(value, ~mask)); 206 value32 = pci_conf_read(pc, tag, reg32); 207 value32 &=~ (mask << shift); 208 value32 |= (value << shift); 209 pci_conf_write(pc, tag, reg32, value32); 210 } 211 212 int 213 pci_write_config_word(struct pci_dev *pdev, int reg, uint16_t value) 214 { 215 216 KASSERT(!ISSET(reg, 1)); 217 pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 2, value); 218 return 0; 219 } 220 221 int 222 pci_write_config_byte(struct pci_dev *pdev, int reg, uint8_t value) 223 { 224 225 pci_rmw_config(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, reg, 1, value); 226 return 0; 227 } 228 229 int 230 pci_bus_write_config_word(struct pci_bus *bus, unsigned devfn, int reg, 231 uint16_t value) 232 { 233 pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), 234 PCI_FUNC(devfn)); 235 236 KASSERT(!ISSET(reg, 1)); 237 pci_rmw_config(bus->pb_pc, tag, reg, 2, value); 238 return 0; 239 } 240 241 int 242 pci_bus_write_config_byte(struct pci_bus *bus, unsigned devfn, int reg, 243 uint8_t value) 244 { 245 pcitag_t tag = pci_make_tag(bus->pb_pc, bus->number, PCI_SLOT(devfn), 246 PCI_FUNC(devfn)); 247 248 pci_rmw_config(bus->pb_pc, tag, reg, 1, value); 249 return 0; 250 } 251 252 int 253 pci_enable_msi(struct pci_dev *pdev) 254 { 255 #ifdef notyet 256 const struct pci_attach_args *const pa = &pdev->pd_pa; 257 258 if (pci_msi_alloc_exact(pa, &pdev->pd_intr_handles, 1)) 259 return -EINVAL; 260 261 pdev->msi_enabled = 1; 262 return 0; 263 #else 264 return -ENOSYS; 265 #endif 266 } 267 268 void 269 pci_disable_msi(struct pci_dev *pdev __unused) 270 { 271 const struct pci_attach_args *const pa = &pdev->pd_pa; 272 273 if (pdev->pd_intr_handles != NULL) { 274 pci_intr_release(pa->pa_pc, pdev->pd_intr_handles, 1); 275 pdev->pd_intr_handles = NULL; 276 } 277 pdev->msi_enabled = 0; 278 } 279 280 void 281 pci_set_master(struct pci_dev *pdev) 282 { 283 pcireg_t csr; 284 285 csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 286 PCI_COMMAND_STATUS_REG); 287 csr |= PCI_COMMAND_MASTER_ENABLE; 288 pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 289 PCI_COMMAND_STATUS_REG, csr); 290 } 291 292 void 293 pci_clear_master(struct pci_dev *pdev) 294 { 295 pcireg_t csr; 296 297 csr = pci_conf_read(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 298 PCI_COMMAND_STATUS_REG); 299 csr &= ~(pcireg_t)PCI_COMMAND_MASTER_ENABLE; 300 pci_conf_write(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 301 PCI_COMMAND_STATUS_REG, csr); 302 } 303 304 bus_addr_t 305 pcibios_align_resource(void *p, const struct resource *resource, 306 bus_addr_t addr, bus_size_t size) 307 { 308 panic("pcibios_align_resource has accessed unaligned neurons!"); 309 } 310 311 int 312 pci_bus_alloc_resource(struct pci_bus *bus, struct resource *resource, 313 bus_size_t size, bus_size_t align, bus_addr_t start, int type __unused, 314 bus_addr_t (*align_fn)(void *, const struct resource *, bus_addr_t, 315 bus_size_t) __unused, 316 struct pci_dev *pdev) 317 { 318 const struct pci_attach_args *const pa = &pdev->pd_pa; 319 bus_space_tag_t bst; 320 int error; 321 322 switch (resource->flags) { 323 case IORESOURCE_MEM: 324 bst = pa->pa_memt; 325 break; 326 327 case IORESOURCE_IO: 328 bst = pa->pa_iot; 329 break; 330 331 default: 332 panic("I don't know what kind of resource you want!"); 333 } 334 335 resource->r_bst = bst; 336 error = bus_space_alloc(bst, start, __type_max(bus_addr_t), 337 size, align, 0, 0, &resource->start, &resource->r_bsh); 338 if (error) 339 return error; 340 341 resource->size = size; 342 return 0; 343 } 344 345 /* 346 * XXX Mega-kludgerific! pci_get_bus_and_slot and pci_get_class are 347 * defined only for their single purposes in i915drm, in 348 * i915_get_bridge_dev and intel_detect_pch. We can't define them more 349 * generally without adapting pci_find_device (and pci_enumerate_bus 350 * internally) to pass a cookie through. 351 */ 352 353 static int 354 pci_kludgey_match_bus0_dev0_func0(const struct pci_attach_args *pa) 355 { 356 357 if (pa->pa_bus != 0) 358 return 0; 359 if (pa->pa_device != 0) 360 return 0; 361 if (pa->pa_function != 0) 362 return 0; 363 364 return 1; 365 } 366 367 struct pci_dev * 368 pci_get_bus_and_slot(int bus, int slot) 369 { 370 struct pci_attach_args pa; 371 372 KASSERT(bus == 0); 373 KASSERT(slot == PCI_DEVFN(0, 0)); 374 375 if (!pci_find_device(&pa, &pci_kludgey_match_bus0_dev0_func0)) 376 return NULL; 377 378 struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP); 379 linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE); 380 381 return pdev; 382 } 383 384 static int 385 pci_kludgey_match_isa_bridge(const struct pci_attach_args *pa) 386 { 387 388 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE) 389 return 0; 390 if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA) 391 return 0; 392 393 return 1; 394 } 395 396 void 397 pci_dev_put(struct pci_dev *pdev) 398 { 399 400 if (pdev == NULL) 401 return; 402 403 KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_GET_MUMBLE)); 404 kmem_free(pdev->bus, sizeof(*pdev->bus)); 405 kmem_free(pdev, sizeof(*pdev)); 406 } 407 408 struct pci_dev * /* XXX i915 kludge */ 409 pci_get_class(uint32_t class_subclass_shifted __unused, struct pci_dev *from) 410 { 411 struct pci_attach_args pa; 412 413 KASSERT(class_subclass_shifted == (PCI_CLASS_BRIDGE_ISA << 8)); 414 415 if (from != NULL) { 416 pci_dev_put(from); 417 return NULL; 418 } 419 420 if (!pci_find_device(&pa, &pci_kludgey_match_isa_bridge)) 421 return NULL; 422 423 struct pci_dev *const pdev = kmem_zalloc(sizeof(*pdev), KM_SLEEP); 424 linux_pci_dev_init(pdev, NULL, NULL, &pa, NBPCI_KLUDGE_GET_MUMBLE); 425 426 return pdev; 427 } 428 429 void 430 pci_unmap_rom(struct pci_dev *pdev, void __pci_rom_iomem *vaddr __unused) 431 { 432 433 /* XXX Disable the ROM address decoder. */ 434 KASSERT(ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)); 435 KASSERT(vaddr == pdev->pd_rom_vaddr); 436 bus_space_unmap(pdev->pd_rom_bst, pdev->pd_rom_bsh, pdev->pd_rom_size); 437 pdev->pd_kludges &= ~NBPCI_KLUDGE_MAP_ROM; 438 pdev->pd_rom_vaddr = NULL; 439 } 440 441 /* XXX Whattakludge! Should move this in sys/arch/. */ 442 static int 443 pci_map_rom_md(struct pci_dev *pdev) 444 { 445 #if defined(__i386__) || defined(__x86_64__) || defined(__ia64__) 446 const bus_addr_t rom_base = 0xc0000; 447 const bus_size_t rom_size = 0x20000; 448 bus_space_handle_t rom_bsh; 449 int error; 450 451 if (PCI_CLASS(pdev->pd_pa.pa_class) != PCI_CLASS_DISPLAY) 452 return ENXIO; 453 if (PCI_SUBCLASS(pdev->pd_pa.pa_class) != PCI_SUBCLASS_DISPLAY_VGA) 454 return ENXIO; 455 /* XXX Check whether this is the primary VGA card? */ 456 error = bus_space_map(pdev->pd_pa.pa_memt, rom_base, rom_size, 457 (BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE), &rom_bsh); 458 if (error) 459 return ENXIO; 460 461 pdev->pd_rom_bst = pdev->pd_pa.pa_memt; 462 pdev->pd_rom_bsh = rom_bsh; 463 pdev->pd_rom_size = rom_size; 464 pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM; 465 466 return 0; 467 #else 468 return ENXIO; 469 #endif 470 } 471 472 void __pci_rom_iomem * 473 pci_map_rom(struct pci_dev *pdev, size_t *sizep) 474 { 475 476 KASSERT(!ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)); 477 478 if (pci_mapreg_map(&pdev->pd_pa, PCI_MAPREG_ROM, PCI_MAPREG_TYPE_ROM, 479 (BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR), 480 &pdev->pd_rom_bst, &pdev->pd_rom_bsh, NULL, &pdev->pd_rom_size) 481 != 0) 482 goto fail_mi; 483 pdev->pd_kludges |= NBPCI_KLUDGE_MAP_ROM; 484 485 /* XXX This type is obviously wrong in general... */ 486 if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh, 487 pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86, 488 &pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) { 489 pci_unmap_rom(pdev, NULL); 490 goto fail_mi; 491 } 492 goto success; 493 494 fail_mi: 495 if (pci_map_rom_md(pdev) != 0) 496 goto fail_md; 497 498 /* XXX This type is obviously wrong in general... */ 499 if (pci_find_rom(&pdev->pd_pa, pdev->pd_rom_bst, pdev->pd_rom_bsh, 500 pdev->pd_rom_size, PCI_ROM_CODE_TYPE_X86, 501 &pdev->pd_rom_found_bsh, &pdev->pd_rom_found_size)) { 502 pci_unmap_rom(pdev, NULL); 503 goto fail_md; 504 } 505 506 success: 507 KASSERT(pdev->pd_rom_found_size <= SIZE_T_MAX); 508 *sizep = pdev->pd_rom_found_size; 509 pdev->pd_rom_vaddr = bus_space_vaddr(pdev->pd_rom_bst, 510 pdev->pd_rom_found_bsh); 511 return pdev->pd_rom_vaddr; 512 513 fail_md: 514 return NULL; 515 } 516 517 void __pci_rom_iomem * 518 pci_platform_rom(struct pci_dev *pdev __unused, size_t *sizep) 519 { 520 521 *sizep = 0; 522 return NULL; 523 } 524 525 int 526 pci_enable_rom(struct pci_dev *pdev) 527 { 528 const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc; 529 const pcitag_t tag = pdev->pd_pa.pa_tag; 530 pcireg_t addr; 531 int s; 532 533 /* XXX Don't do anything if the ROM isn't there. */ 534 535 s = splhigh(); 536 addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM); 537 addr |= PCI_MAPREG_ROM_ENABLE; 538 pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr); 539 splx(s); 540 541 return 0; 542 } 543 544 void 545 pci_disable_rom(struct pci_dev *pdev) 546 { 547 const pci_chipset_tag_t pc = pdev->pd_pa.pa_pc; 548 const pcitag_t tag = pdev->pd_pa.pa_tag; 549 pcireg_t addr; 550 int s; 551 552 s = splhigh(); 553 addr = pci_conf_read(pc, tag, PCI_MAPREG_ROM); 554 addr &= ~(pcireg_t)PCI_MAPREG_ROM_ENABLE; 555 pci_conf_write(pc, tag, PCI_MAPREG_ROM, addr); 556 splx(s); 557 } 558 559 bus_addr_t 560 pci_resource_start(struct pci_dev *pdev, unsigned i) 561 { 562 563 KASSERT(i < PCI_NUM_RESOURCES); 564 return pdev->pd_resources[i].addr; 565 } 566 567 bus_size_t 568 pci_resource_len(struct pci_dev *pdev, unsigned i) 569 { 570 571 KASSERT(i < PCI_NUM_RESOURCES); 572 return pdev->pd_resources[i].size; 573 } 574 575 bus_addr_t 576 pci_resource_end(struct pci_dev *pdev, unsigned i) 577 { 578 579 return pci_resource_start(pdev, i) + (pci_resource_len(pdev, i) - 1); 580 } 581 582 int 583 pci_resource_flags(struct pci_dev *pdev, unsigned i) 584 { 585 586 KASSERT(i < PCI_NUM_RESOURCES); 587 return pdev->pd_resources[i].flags; 588 } 589 590 void __pci_iomem * 591 pci_iomap(struct pci_dev *pdev, unsigned i, bus_size_t size) 592 { 593 int error; 594 595 KASSERT(i < PCI_NUM_RESOURCES); 596 KASSERT(pdev->pd_resources[i].kva == NULL); 597 598 if (PCI_MAPREG_TYPE(pdev->pd_resources[i].type) != PCI_MAPREG_TYPE_MEM) 599 return NULL; 600 if (pdev->pd_resources[i].size < size) 601 return NULL; 602 error = bus_space_map(pdev->pd_pa.pa_memt, pdev->pd_resources[i].addr, 603 size, BUS_SPACE_MAP_LINEAR | pdev->pd_resources[i].flags, 604 &pdev->pd_resources[i].bsh); 605 if (error) { 606 /* Horrible hack: try asking the fake AGP device. */ 607 if (!agp_i810_borrow(pdev->pd_resources[i].addr, size, 608 &pdev->pd_resources[i].bsh)) 609 return NULL; 610 } 611 pdev->pd_resources[i].bst = pdev->pd_pa.pa_memt; 612 pdev->pd_resources[i].kva = bus_space_vaddr(pdev->pd_resources[i].bst, 613 pdev->pd_resources[i].bsh); 614 pdev->pd_resources[i].mapped = true; 615 616 return pdev->pd_resources[i].kva; 617 } 618 619 void 620 pci_iounmap(struct pci_dev *pdev, void __pci_iomem *kva) 621 { 622 unsigned i; 623 624 CTASSERT(__arraycount(pdev->pd_resources) == PCI_NUM_RESOURCES); 625 for (i = 0; i < PCI_NUM_RESOURCES; i++) { 626 if (pdev->pd_resources[i].kva == kva) 627 break; 628 } 629 KASSERT(i < PCI_NUM_RESOURCES); 630 631 pdev->pd_resources[i].kva = NULL; 632 bus_space_unmap(pdev->pd_resources[i].bst, pdev->pd_resources[i].bsh, 633 pdev->pd_resources[i].size); 634 } 635 636 void 637 pci_save_state(struct pci_dev *pdev) 638 { 639 640 KASSERT(pdev->pd_saved_state == NULL); 641 pdev->pd_saved_state = kmem_alloc(sizeof(*pdev->pd_saved_state), 642 KM_SLEEP); 643 pci_conf_capture(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 644 pdev->pd_saved_state); 645 } 646 647 void 648 pci_restore_state(struct pci_dev *pdev) 649 { 650 651 KASSERT(pdev->pd_saved_state != NULL); 652 pci_conf_restore(pdev->pd_pa.pa_pc, pdev->pd_pa.pa_tag, 653 pdev->pd_saved_state); 654 kmem_free(pdev->pd_saved_state, sizeof(*pdev->pd_saved_state)); 655 pdev->pd_saved_state = NULL; 656 } 657 658 bool 659 pci_is_pcie(struct pci_dev *pdev) 660 { 661 662 return (pci_find_capability(pdev, PCI_CAP_PCIEXPRESS) != 0); 663 } 664 665 bool 666 pci_dma_supported(struct pci_dev *pdev, uintmax_t mask) 667 { 668 669 /* XXX Cop-out. */ 670 if (mask > DMA_BIT_MASK(32)) 671 return pci_dma64_available(&pdev->pd_pa); 672 else 673 return true; 674 } 675 676 bool 677 pci_is_root_bus(struct pci_bus *bus) 678 { 679 680 /* XXX Cop-out. */ 681 return false; 682 } 683 684 int 685 pci_domain_nr(struct pci_bus *bus) 686 { 687 688 return device_unit(bus->pb_dev); 689 } 690 691 /* 692 * We explicitly rename pci_enable/disable_device so that you have to 693 * review each use of them, since NetBSD's PCI API does _not_ respect 694 * our local enablecnt here, but there are different parts of NetBSD 695 * that automatically enable/disable like PMF, so you have to decide 696 * for each one whether to call it or not. 697 */ 698 699 int 700 linux_pci_enable_device(struct pci_dev *pdev) 701 { 702 const struct pci_attach_args *pa = &pdev->pd_pa; 703 pcireg_t csr; 704 int s; 705 706 if (pdev->pd_enablecnt++) 707 return 0; 708 709 s = splhigh(); 710 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 711 csr |= PCI_COMMAND_IO_ENABLE; 712 csr |= PCI_COMMAND_MEM_ENABLE; 713 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr); 714 splx(s); 715 716 return 0; 717 } 718 719 void 720 linux_pci_disable_device(struct pci_dev *pdev) 721 { 722 const struct pci_attach_args *pa = &pdev->pd_pa; 723 pcireg_t csr; 724 int s; 725 726 if (--pdev->pd_enablecnt) 727 return; 728 729 s = splhigh(); 730 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 731 csr &= ~PCI_COMMAND_IO_ENABLE; 732 csr &= ~PCI_COMMAND_MEM_ENABLE; 733 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr); 734 splx(s); 735 } 736 737 void 738 linux_pci_dev_destroy(struct pci_dev *pdev) 739 { 740 unsigned i; 741 742 if (pdev->bus != NULL) { 743 kmem_free(pdev->bus, sizeof(*pdev->bus)); 744 pdev->bus = NULL; 745 } 746 if (ISSET(pdev->pd_kludges, NBPCI_KLUDGE_MAP_ROM)) { 747 pci_unmap_rom(pdev, pdev->pd_rom_vaddr); 748 pdev->pd_rom_vaddr = 0; 749 } 750 for (i = 0; i < __arraycount(pdev->pd_resources); i++) { 751 if (!pdev->pd_resources[i].mapped) 752 continue; 753 bus_space_unmap(pdev->pd_resources[i].bst, 754 pdev->pd_resources[i].bsh, pdev->pd_resources[i].size); 755 } 756 757 /* There is no way these should be still in use. */ 758 KASSERT(pdev->pd_saved_state == NULL); 759 KASSERT(pdev->pd_intr_handles == NULL); 760 } 761