1 /*- 2 * Copyright (c) 2011 Nathan Whitehorn 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/module.h> 32 #include <sys/bus.h> 33 #include <sys/conf.h> 34 #include <sys/kernel.h> 35 #include <sys/rman.h> 36 37 #include <dev/ofw/openfirm.h> 38 #include <dev/ofw/ofw_pci.h> 39 #include <dev/ofw/ofw_bus.h> 40 #include <dev/ofw/ofw_bus_subr.h> 41 #include <dev/ofw/ofwpci.h> 42 43 #include <dev/pci/pcivar.h> 44 #include <dev/pci/pcireg.h> 45 #include <dev/pci/pcib_private.h> 46 47 #include <machine/bus.h> 48 #include <machine/md_var.h> 49 #include <machine/resource.h> 50 51 #include <vm/vm.h> 52 #include <vm/pmap.h> 53 54 #include "pcib_if.h" 55 56 /* 57 * If it is necessary to set another value of this for 58 * some platforms it should be set at fdt.h file 59 */ 60 #ifndef PCI_MAP_INTR 61 #define PCI_MAP_INTR 4 62 #endif 63 64 #define PCI_INTR_PINS 4 65 66 /* 67 * bus interface. 68 */ 69 static struct resource * ofw_pci_alloc_resource(device_t, device_t, 70 int, int *, rman_res_t, rman_res_t, rman_res_t, u_int); 71 static int ofw_pci_release_resource(device_t, device_t, int, int, 72 struct resource *); 73 static int ofw_pci_activate_resource(device_t, device_t, int, int, 74 struct resource *); 75 static int ofw_pci_deactivate_resource(device_t, device_t, int, int, 76 struct resource *); 77 static int ofw_pci_adjust_resource(device_t, device_t, int, 78 struct resource *, rman_res_t, rman_res_t); 79 80 #ifdef __powerpc__ 81 static bus_space_tag_t ofw_pci_bus_get_bus_tag(device_t, device_t); 82 #endif 83 84 /* 85 * pcib interface 86 */ 87 static int ofw_pci_maxslots(device_t); 88 89 /* 90 * ofw_bus interface 91 */ 92 static phandle_t ofw_pci_get_node(device_t, device_t); 93 94 /* 95 * local methods 96 */ 97 static int ofw_pci_fill_ranges(phandle_t, struct ofw_pci_range *); 98 static struct rman *ofw_pci_get_rman(struct ofw_pci_softc *, int, u_int); 99 100 /* 101 * Driver methods. 102 */ 103 static device_method_t ofw_pci_methods[] = { 104 105 /* Device interface */ 106 DEVMETHOD(device_attach, ofw_pci_attach), 107 108 /* Bus interface */ 109 DEVMETHOD(bus_print_child, bus_generic_print_child), 110 DEVMETHOD(bus_read_ivar, ofw_pci_read_ivar), 111 DEVMETHOD(bus_write_ivar, ofw_pci_write_ivar), 112 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 113 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 114 DEVMETHOD(bus_alloc_resource, ofw_pci_alloc_resource), 115 DEVMETHOD(bus_release_resource, ofw_pci_release_resource), 116 DEVMETHOD(bus_activate_resource, ofw_pci_activate_resource), 117 DEVMETHOD(bus_deactivate_resource, ofw_pci_deactivate_resource), 118 DEVMETHOD(bus_adjust_resource, ofw_pci_adjust_resource), 119 #ifdef __powerpc__ 120 DEVMETHOD(bus_get_bus_tag, ofw_pci_bus_get_bus_tag), 121 #endif 122 123 /* pcib interface */ 124 DEVMETHOD(pcib_maxslots, ofw_pci_maxslots), 125 DEVMETHOD(pcib_route_interrupt, ofw_pci_route_interrupt), 126 127 /* ofw_bus interface */ 128 DEVMETHOD(ofw_bus_get_node, ofw_pci_get_node), 129 130 DEVMETHOD_END 131 }; 132 133 DEFINE_CLASS_0(ofw_pci, ofw_pci_driver, ofw_pci_methods, 0); 134 135 int 136 ofw_pci_init(device_t dev) 137 { 138 struct ofw_pci_softc *sc; 139 phandle_t node; 140 u_int32_t busrange[2]; 141 struct ofw_pci_range *rp; 142 int i, error; 143 struct ofw_pci_cell_info *cell_info; 144 145 node = ofw_bus_get_node(dev); 146 sc = device_get_softc(dev); 147 sc->sc_initialized = 1; 148 sc->sc_range = NULL; 149 sc->sc_pci_domain = device_get_unit(dev); 150 151 cell_info = (struct ofw_pci_cell_info *)malloc(sizeof(*cell_info), 152 M_DEVBUF, M_WAITOK | M_ZERO); 153 154 sc->sc_cell_info = cell_info; 155 156 if (OF_getencprop(node, "bus-range", busrange, sizeof(busrange)) != 8) 157 busrange[0] = 0; 158 159 sc->sc_dev = dev; 160 sc->sc_node = node; 161 sc->sc_bus = busrange[0]; 162 163 if (sc->sc_quirks & OFW_PCI_QUIRK_RANGES_ON_CHILDREN) { 164 phandle_t c; 165 int n, i; 166 167 sc->sc_nrange = 0; 168 for (c = OF_child(node); c != 0; c = OF_peer(c)) { 169 n = ofw_pci_nranges(c, cell_info); 170 if (n > 0) 171 sc->sc_nrange += n; 172 } 173 if (sc->sc_nrange == 0) { 174 error = ENXIO; 175 goto out; 176 } 177 sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]), 178 M_DEVBUF, M_WAITOK); 179 i = 0; 180 for (c = OF_child(node); c != 0; c = OF_peer(c)) { 181 n = ofw_pci_fill_ranges(c, &sc->sc_range[i]); 182 if (n > 0) 183 i += n; 184 } 185 KASSERT(i == sc->sc_nrange, ("range count mismatch")); 186 } else { 187 sc->sc_nrange = ofw_pci_nranges(node, cell_info); 188 if (sc->sc_nrange <= 0) { 189 device_printf(dev, "could not getranges\n"); 190 error = ENXIO; 191 goto out; 192 } 193 sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]), 194 M_DEVBUF, M_WAITOK); 195 ofw_pci_fill_ranges(node, sc->sc_range); 196 } 197 198 sc->sc_io_rman.rm_type = RMAN_ARRAY; 199 sc->sc_io_rman.rm_descr = "PCI I/O Ports"; 200 error = rman_init(&sc->sc_io_rman); 201 if (error != 0) { 202 device_printf(dev, "rman_init() failed. error = %d\n", error); 203 goto out; 204 } 205 206 sc->sc_mem_rman.rm_type = RMAN_ARRAY; 207 sc->sc_mem_rman.rm_descr = "PCI Non Prefetchable Memory"; 208 error = rman_init(&sc->sc_mem_rman); 209 if (error != 0) { 210 device_printf(dev, "rman_init() failed. error = %d\n", error); 211 goto out; 212 } 213 214 sc->sc_pmem_rman.rm_type = RMAN_ARRAY; 215 sc->sc_pmem_rman.rm_descr = "PCI Prefetchable Memory"; 216 error = rman_init(&sc->sc_pmem_rman); 217 if (error != 0) { 218 device_printf(dev, "rman_init() failed. error = %d\n", error); 219 goto out; 220 } 221 222 for (i = 0; i < sc->sc_nrange; i++) { 223 error = 0; 224 rp = sc->sc_range + i; 225 226 if (sc->sc_range_mask & ((uint64_t)1 << i)) 227 continue; 228 switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) { 229 case OFW_PCI_PHYS_HI_SPACE_CONFIG: 230 break; 231 case OFW_PCI_PHYS_HI_SPACE_IO: 232 error = rman_manage_region(&sc->sc_io_rman, rp->pci, 233 rp->pci + rp->size - 1); 234 break; 235 case OFW_PCI_PHYS_HI_SPACE_MEM32: 236 case OFW_PCI_PHYS_HI_SPACE_MEM64: 237 if (rp->pci_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) { 238 sc->sc_have_pmem = 1; 239 error = rman_manage_region(&sc->sc_pmem_rman, 240 rp->pci, rp->pci + rp->size - 1); 241 } else { 242 error = rman_manage_region(&sc->sc_mem_rman, 243 rp->pci, rp->pci + rp->size - 1); 244 } 245 break; 246 } 247 248 if (error != 0) { 249 device_printf(dev, 250 "rman_manage_region(%x, %#jx, %#jx) failed. " 251 "error = %d\n", rp->pci_hi & 252 OFW_PCI_PHYS_HI_SPACEMASK, rp->pci, 253 rp->pci + rp->size - 1, error); 254 goto out; 255 } 256 } 257 258 ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(cell_t)); 259 return (0); 260 261 out: 262 free(cell_info, M_DEVBUF); 263 free(sc->sc_range, M_DEVBUF); 264 rman_fini(&sc->sc_io_rman); 265 rman_fini(&sc->sc_mem_rman); 266 rman_fini(&sc->sc_pmem_rman); 267 268 return (error); 269 } 270 271 int 272 ofw_pci_attach(device_t dev) 273 { 274 struct ofw_pci_softc *sc; 275 int error; 276 277 sc = device_get_softc(dev); 278 if (!sc->sc_initialized) { 279 error = ofw_pci_init(dev); 280 if (error != 0) 281 return (error); 282 } 283 284 device_add_child(dev, "pci", -1); 285 return (bus_generic_attach(dev)); 286 } 287 288 static int 289 ofw_pci_maxslots(device_t dev) 290 { 291 292 return (PCI_SLOTMAX); 293 } 294 295 int 296 ofw_pci_route_interrupt(device_t bus, device_t dev, int pin) 297 { 298 struct ofw_pci_softc *sc; 299 struct ofw_pci_register reg; 300 uint32_t pintr, mintr[PCI_MAP_INTR]; 301 int intrcells; 302 phandle_t iparent; 303 304 sc = device_get_softc(bus); 305 pintr = pin; 306 307 /* Fabricate imap information in case this isn't an OFW device */ 308 bzero(®, sizeof(reg)); 309 reg.phys_hi = (pci_get_bus(dev) << OFW_PCI_PHYS_HI_BUSSHIFT) | 310 (pci_get_slot(dev) << OFW_PCI_PHYS_HI_DEVICESHIFT) | 311 (pci_get_function(dev) << OFW_PCI_PHYS_HI_FUNCTIONSHIFT); 312 313 intrcells = ofw_bus_lookup_imap(ofw_bus_get_node(dev), 314 &sc->sc_pci_iinfo, ®, sizeof(reg), &pintr, sizeof(pintr), 315 mintr, sizeof(mintr), &iparent); 316 if (intrcells != 0) { 317 pintr = ofw_bus_map_intr(dev, iparent, intrcells, mintr); 318 return (pintr); 319 } 320 321 /* 322 * Maybe it's a real interrupt, not an intpin 323 */ 324 if (pin > PCI_INTR_PINS) 325 return (pin); 326 327 device_printf(bus, "could not route pin %d for device %d.%d\n", 328 pin, pci_get_slot(dev), pci_get_function(dev)); 329 return (PCI_INVALID_IRQ); 330 } 331 332 int 333 ofw_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 334 { 335 struct ofw_pci_softc *sc; 336 337 sc = device_get_softc(dev); 338 339 switch (which) { 340 case PCIB_IVAR_DOMAIN: 341 *result = sc->sc_pci_domain; 342 return (0); 343 case PCIB_IVAR_BUS: 344 *result = sc->sc_bus; 345 return (0); 346 default: 347 break; 348 } 349 350 return (ENOENT); 351 } 352 353 int 354 ofw_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 355 { 356 struct ofw_pci_softc *sc; 357 358 sc = device_get_softc(dev); 359 360 switch (which) { 361 case PCIB_IVAR_BUS: 362 sc->sc_bus = value; 363 return (0); 364 default: 365 break; 366 } 367 368 return (ENOENT); 369 } 370 371 int 372 ofw_pci_nranges(phandle_t node, struct ofw_pci_cell_info *info) 373 { 374 ssize_t nbase_ranges; 375 376 if (info == NULL) 377 return (-1); 378 379 info->host_address_cells = 1; 380 info->size_cells = 2; 381 info->pci_address_cell = 3; 382 383 OF_getencprop(OF_parent(node), "#address-cells", 384 &(info->host_address_cells), sizeof(info->host_address_cells)); 385 OF_getencprop(node, "#address-cells", 386 &(info->pci_address_cell), sizeof(info->pci_address_cell)); 387 OF_getencprop(node, "#size-cells", &(info->size_cells), 388 sizeof(info->size_cells)); 389 390 nbase_ranges = OF_getproplen(node, "ranges"); 391 if (nbase_ranges <= 0) 392 return (-1); 393 394 return (nbase_ranges / sizeof(cell_t) / 395 (info->pci_address_cell + info->host_address_cells + 396 info->size_cells)); 397 } 398 399 static struct resource * 400 ofw_pci_alloc_resource(device_t bus, device_t child, int type, int *rid, 401 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 402 { 403 struct ofw_pci_softc *sc; 404 struct resource *rv; 405 struct rman *rm; 406 int needactivate; 407 408 409 needactivate = flags & RF_ACTIVE; 410 flags &= ~RF_ACTIVE; 411 412 sc = device_get_softc(bus); 413 414 #if defined(NEW_PCIB) && defined(PCI_RES_BUS) 415 if (type == PCI_RES_BUS) { 416 return (pci_domain_alloc_bus(sc->sc_pci_domain, child, rid, 417 start, end, count, flags | needactivate)); 418 } 419 #endif 420 421 rm = ofw_pci_get_rman(sc, type, flags); 422 if (rm == NULL) { 423 return (bus_generic_alloc_resource(bus, child, type, rid, 424 start, end, count, flags | needactivate)); 425 } 426 427 rv = rman_reserve_resource(rm, start, end, count, flags, child); 428 if (rv == NULL) { 429 device_printf(bus, "failed to reserve resource for %s\n", 430 device_get_nameunit(child)); 431 return (NULL); 432 } 433 434 rman_set_rid(rv, *rid); 435 436 if (needactivate) { 437 if (bus_activate_resource(child, type, *rid, rv) != 0) { 438 device_printf(bus, 439 "failed to activate resource for %s\n", 440 device_get_nameunit(child)); 441 rman_release_resource(rv); 442 return (NULL); 443 } 444 } 445 446 return (rv); 447 } 448 449 static int 450 ofw_pci_release_resource(device_t bus, device_t child, int type, int rid, 451 struct resource *res) 452 { 453 struct ofw_pci_softc *sc; 454 struct rman *rm; 455 int error; 456 457 sc = device_get_softc(bus); 458 459 #if defined(NEW_PCIB) && defined(PCI_RES_BUS) 460 if (type == PCI_RES_BUS) 461 return (pci_domain_release_bus(sc->sc_pci_domain, child, rid, 462 res)); 463 #endif 464 465 rm = ofw_pci_get_rman(sc, type, rman_get_flags(res)); 466 if (rm == NULL) { 467 return (bus_generic_release_resource(bus, child, type, rid, 468 res)); 469 } 470 KASSERT(rman_is_region_manager(res, rm), ("rman mismatch")); 471 472 if (rman_get_flags(res) & RF_ACTIVE) { 473 error = bus_deactivate_resource(child, type, rid, res); 474 if (error != 0) 475 return (error); 476 } 477 return (rman_release_resource(res)); 478 } 479 480 static int 481 ofw_pci_activate_resource(device_t bus, device_t child, int type, int rid, 482 struct resource *res) 483 { 484 struct ofw_pci_softc *sc; 485 bus_space_handle_t handle; 486 bus_space_tag_t tag; 487 struct ofw_pci_range *rp; 488 vm_paddr_t start; 489 int space; 490 int rv; 491 492 sc = device_get_softc(bus); 493 494 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY) { 495 return (bus_generic_activate_resource(bus, child, type, rid, 496 res)); 497 } 498 499 start = (vm_paddr_t)rman_get_start(res); 500 501 /* 502 * Map this through the ranges list 503 */ 504 for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange && 505 rp->pci_hi != 0; rp++) { 506 if (start < rp->pci || start >= rp->pci + rp->size) 507 continue; 508 509 switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) { 510 case OFW_PCI_PHYS_HI_SPACE_IO: 511 space = SYS_RES_IOPORT; 512 break; 513 case OFW_PCI_PHYS_HI_SPACE_MEM32: 514 case OFW_PCI_PHYS_HI_SPACE_MEM64: 515 space = SYS_RES_MEMORY; 516 break; 517 default: 518 space = -1; 519 } 520 521 if (type == space) { 522 start += (rp->host - rp->pci); 523 break; 524 } 525 } 526 527 if (bootverbose) 528 printf("ofw_pci mapdev: start %jx, len %jd\n", 529 (rman_res_t)start, rman_get_size(res)); 530 531 tag = BUS_GET_BUS_TAG(child, child); 532 if (tag == NULL) 533 return (ENOMEM); 534 535 rman_set_bustag(res, tag); 536 rv = bus_space_map(tag, start, 537 rman_get_size(res), 0, &handle); 538 if (rv != 0) 539 return (ENOMEM); 540 541 rman_set_bushandle(res, handle); 542 rman_set_virtual(res, (void *)handle); /* XXX for powerpc only ? */ 543 544 return (rman_activate_resource(res)); 545 } 546 547 #ifdef __powerpc__ 548 static bus_space_tag_t 549 ofw_pci_bus_get_bus_tag(device_t bus, device_t child) 550 { 551 552 return (&bs_le_tag); 553 } 554 #endif 555 556 static int 557 ofw_pci_deactivate_resource(device_t bus, device_t child, int type, int rid, 558 struct resource *res) 559 { 560 struct ofw_pci_softc *sc; 561 vm_size_t psize; 562 563 sc = device_get_softc(bus); 564 565 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY) { 566 return (bus_generic_deactivate_resource(bus, child, type, rid, 567 res)); 568 } 569 570 psize = rman_get_size(res); 571 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize); 572 573 return (rman_deactivate_resource(res)); 574 } 575 576 static int 577 ofw_pci_adjust_resource(device_t bus, device_t child, int type, 578 struct resource *res, rman_res_t start, rman_res_t end) 579 { 580 struct rman *rm; 581 struct ofw_pci_softc *sc; 582 583 sc = device_get_softc(bus); 584 #if defined(NEW_PCIB) && defined(PCI_RES_BUS) 585 if (type == PCI_RES_BUS) 586 return (pci_domain_adjust_bus(sc->sc_pci_domain, child, res, 587 start, end)); 588 #endif 589 590 rm = ofw_pci_get_rman(sc, type, rman_get_flags(res)); 591 if (rm == NULL) { 592 return (bus_generic_adjust_resource(bus, child, type, res, 593 start, end)); 594 } 595 KASSERT(rman_is_region_manager(res, rm), ("rman mismatch")); 596 KASSERT(!(rman_get_flags(res) & RF_ACTIVE), 597 ("active resources cannot be adjusted")); 598 599 return (rman_adjust_resource(res, start, end)); 600 } 601 602 static phandle_t 603 ofw_pci_get_node(device_t bus, device_t dev) 604 { 605 struct ofw_pci_softc *sc; 606 607 sc = device_get_softc(bus); 608 /* We only have one child, the PCI bus, which needs our own node. */ 609 610 return (sc->sc_node); 611 } 612 613 static int 614 ofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges) 615 { 616 int host_address_cells = 1, pci_address_cells = 3, size_cells = 2; 617 cell_t *base_ranges; 618 ssize_t nbase_ranges; 619 int nranges; 620 int i, j, k; 621 622 OF_getencprop(OF_parent(node), "#address-cells", &host_address_cells, 623 sizeof(host_address_cells)); 624 OF_getencprop(node, "#address-cells", &pci_address_cells, 625 sizeof(pci_address_cells)); 626 OF_getencprop(node, "#size-cells", &size_cells, sizeof(size_cells)); 627 628 nbase_ranges = OF_getproplen(node, "ranges"); 629 if (nbase_ranges <= 0) 630 return (-1); 631 nranges = nbase_ranges / sizeof(cell_t) / 632 (pci_address_cells + host_address_cells + size_cells); 633 634 base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK); 635 OF_getencprop(node, "ranges", base_ranges, nbase_ranges); 636 637 for (i = 0, j = 0; i < nranges; i++) { 638 ranges[i].pci_hi = base_ranges[j++]; 639 ranges[i].pci = 0; 640 for (k = 0; k < pci_address_cells - 1; k++) { 641 ranges[i].pci <<= 32; 642 ranges[i].pci |= base_ranges[j++]; 643 } 644 ranges[i].host = 0; 645 for (k = 0; k < host_address_cells; k++) { 646 ranges[i].host <<= 32; 647 ranges[i].host |= base_ranges[j++]; 648 } 649 ranges[i].size = 0; 650 for (k = 0; k < size_cells; k++) { 651 ranges[i].size <<= 32; 652 ranges[i].size |= base_ranges[j++]; 653 } 654 } 655 656 free(base_ranges, M_DEVBUF); 657 return (nranges); 658 } 659 660 static struct rman * 661 ofw_pci_get_rman(struct ofw_pci_softc *sc, int type, u_int flags) 662 { 663 664 switch (type) { 665 case SYS_RES_IOPORT: 666 return (&sc->sc_io_rman); 667 case SYS_RES_MEMORY: 668 if (sc->sc_have_pmem && (flags & RF_PREFETCHABLE)) 669 return (&sc->sc_pmem_rman); 670 else 671 return (&sc->sc_mem_rman); 672 default: 673 break; 674 } 675 676 return (NULL); 677 } 678