ofw_pci.c revision 231003
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: head/sys/powerpc/ofw/ofw_pci.c 231003 2012-02-05 00:51:59Z nwhitehorn $"); 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 36#include <dev/ofw/openfirm.h> 37#include <dev/ofw/ofw_pci.h> 38#include <dev/ofw/ofw_bus.h> 39#include <dev/ofw/ofw_bus_subr.h> 40 41#include <dev/pci/pcivar.h> 42#include <dev/pci/pcireg.h> 43 44#include <machine/bus.h> 45#include <machine/intr_machdep.h> 46#include <machine/md_var.h> 47#include <machine/pio.h> 48#include <machine/resource.h> 49 50#include <sys/rman.h> 51 52#include <vm/vm.h> 53#include <vm/pmap.h> 54 55#include <powerpc/ofw/ofw_pci.h> 56 57#include "pcib_if.h" 58 59/* 60 * Bus interface. 61 */ 62static int ofw_pci_read_ivar(device_t, device_t, int, 63 uintptr_t *); 64static struct resource * ofw_pci_alloc_resource(device_t bus, 65 device_t child, int type, int *rid, u_long start, 66 u_long end, u_long count, u_int flags); 67static int ofw_pci_release_resource(device_t bus, device_t child, 68 int type, int rid, struct resource *res); 69static int ofw_pci_activate_resource(device_t bus, device_t child, 70 int type, int rid, struct resource *res); 71static int ofw_pci_deactivate_resource(device_t bus, 72 device_t child, int type, int rid, 73 struct resource *res); 74static int ofw_pci_adjust_resource(device_t bus, device_t child, 75 int type, struct resource *res, u_long start, 76 u_long end); 77 78/* 79 * pcib interface. 80 */ 81static int ofw_pci_maxslots(device_t); 82static int ofw_pci_route_interrupt(device_t, device_t, int); 83 84/* 85 * ofw_bus interface 86 */ 87static phandle_t ofw_pci_get_node(device_t bus, device_t dev); 88 89/* 90 * local methods 91 */ 92 93static int ofw_pci_nranges(phandle_t node); 94static int ofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges); 95 96/* 97 * Driver methods. 98 */ 99static device_method_t ofw_pci_methods[] = { 100 /* Device interface */ 101 DEVMETHOD(device_attach, ofw_pci_attach), 102 103 /* Bus interface */ 104 DEVMETHOD(bus_print_child, bus_generic_print_child), 105 DEVMETHOD(bus_read_ivar, ofw_pci_read_ivar), 106 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 107 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 108 DEVMETHOD(bus_alloc_resource, ofw_pci_alloc_resource), 109 DEVMETHOD(bus_release_resource, ofw_pci_release_resource), 110 DEVMETHOD(bus_activate_resource, ofw_pci_activate_resource), 111 DEVMETHOD(bus_deactivate_resource, ofw_pci_deactivate_resource), 112 DEVMETHOD(bus_adjust_resource, ofw_pci_adjust_resource), 113 114 /* pcib interface */ 115 DEVMETHOD(pcib_maxslots, ofw_pci_maxslots), 116 DEVMETHOD(pcib_route_interrupt, ofw_pci_route_interrupt), 117 118 /* ofw_bus interface */ 119 DEVMETHOD(ofw_bus_get_node, ofw_pci_get_node), 120 121 DEVMETHOD_END 122}; 123 124DEFINE_CLASS_0(ofw_pci, ofw_pci_driver, ofw_pci_methods, 0); 125 126int 127ofw_pci_attach(device_t dev) 128{ 129 struct ofw_pci_softc *sc; 130 phandle_t node; 131 u_int32_t busrange[2]; 132 struct ofw_pci_range *rp; 133 int error; 134 135 node = ofw_bus_get_node(dev); 136 sc = device_get_softc(dev); 137 138 if (OF_getprop(node, "reg", &sc->sc_pcir, sizeof(sc->sc_pcir)) == -1) 139 return (ENXIO); 140 141 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8) 142 busrange[0] = 0; 143 144 sc->sc_dev = dev; 145 sc->sc_node = node; 146 sc->sc_bus = busrange[0]; 147 148 if (sc->sc_quirks & OFW_PCI_QUIRK_RANGES_ON_CHILDREN) { 149 phandle_t c; 150 int n, i; 151 152 sc->sc_nrange = 0; 153 for (c = OF_child(node); c != 0; c = OF_peer(c)) { 154 n = ofw_pci_nranges(c); 155 if (n > 0) 156 sc->sc_nrange += n; 157 } 158 if (sc->sc_nrange == 0) 159 return (ENXIO); 160 sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]), 161 M_DEVBUF, M_WAITOK); 162 i = 0; 163 for (c = OF_child(node); c != 0; c = OF_peer(c)) { 164 n = ofw_pci_fill_ranges(c, &sc->sc_range[i]); 165 if (n > 0) 166 i += n; 167 } 168 KASSERT(i == sc->sc_nrange, ("range count mismatch")); 169 } else { 170 sc->sc_nrange = ofw_pci_nranges(node); 171 if (sc->sc_nrange <= 0) { 172 device_printf(dev, "could not get ranges\n"); 173 return (ENXIO); 174 } 175 sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]), 176 M_DEVBUF, M_WAITOK); 177 ofw_pci_fill_ranges(node, sc->sc_range); 178 } 179 180 sc->sc_io_rman.rm_type = RMAN_ARRAY; 181 sc->sc_io_rman.rm_descr = "PCI I/O Ports"; 182 error = rman_init(&sc->sc_io_rman); 183 if (error) { 184 device_printf(dev, "rman_init() failed. error = %d\n", error); 185 return (error); 186 } 187 188 sc->sc_mem_rman.rm_type = RMAN_ARRAY; 189 sc->sc_mem_rman.rm_descr = "PCI Memory"; 190 error = rman_init(&sc->sc_mem_rman); 191 if (error) { 192 device_printf(dev, "rman_init() failed. error = %d\n", error); 193 return (error); 194 } 195 196 for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange && 197 rp->pci_hi != 0; rp++) { 198 error = 0; 199 200 switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) { 201 case OFW_PCI_PHYS_HI_SPACE_CONFIG: 202 break; 203 case OFW_PCI_PHYS_HI_SPACE_IO: 204 error = rman_manage_region(&sc->sc_io_rman, rp->pci, 205 rp->pci + rp->size - 1); 206 break; 207 case OFW_PCI_PHYS_HI_SPACE_MEM32: 208 case OFW_PCI_PHYS_HI_SPACE_MEM64: 209 error = rman_manage_region(&sc->sc_mem_rman, rp->pci, 210 rp->pci + rp->size - 1); 211 break; 212 } 213 214 if (error) { 215 device_printf(dev, 216 "rman_manage_region(%x, %#jx, %#jx) failed. " 217 "error = %d\n", rp->pci_hi & 218 OFW_PCI_PHYS_HI_SPACEMASK, rp->pci, 219 rp->pci + rp->size - 1, error); 220 panic("AHOY"); 221 return (error); 222 } 223 } 224 225 ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(cell_t)); 226 227 device_add_child(dev, "pci", device_get_unit(dev)); 228 return (bus_generic_attach(dev)); 229} 230 231static int 232ofw_pci_maxslots(device_t dev) 233{ 234 235 return (PCI_SLOTMAX); 236} 237 238static int 239ofw_pci_route_interrupt(device_t bus, device_t dev, int pin) 240{ 241 struct ofw_pci_softc *sc; 242 struct ofw_pci_register reg; 243 uint32_t pintr, mintr; 244 phandle_t iparent; 245 uint8_t maskbuf[sizeof(reg) + sizeof(pintr)]; 246 247 sc = device_get_softc(bus); 248 pintr = pin; 249 if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), &sc->sc_pci_iinfo, ®, 250 sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr), 251 &iparent, maskbuf)) 252 return (MAP_IRQ(iparent, mintr)); 253 254 /* Maybe it's a real interrupt, not an intpin */ 255 if (pin > 4) 256 return (pin); 257 258 device_printf(bus, "could not route pin %d for device %d.%d\n", 259 pin, pci_get_slot(dev), pci_get_function(dev)); 260 return (PCI_INVALID_IRQ); 261} 262 263static int 264ofw_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 265{ 266 struct ofw_pci_softc *sc; 267 268 sc = device_get_softc(dev); 269 270 switch (which) { 271 case PCIB_IVAR_DOMAIN: 272 *result = device_get_unit(dev); 273 return (0); 274 case PCIB_IVAR_BUS: 275 *result = sc->sc_bus; 276 return (0); 277 } 278 279 return (ENOENT); 280} 281 282static struct resource * 283ofw_pci_alloc_resource(device_t bus, device_t child, int type, int *rid, 284 u_long start, u_long end, u_long count, u_int flags) 285{ 286 struct ofw_pci_softc *sc; 287 struct resource *rv; 288 struct rman *rm; 289 int needactivate; 290 291 needactivate = flags & RF_ACTIVE; 292 flags &= ~RF_ACTIVE; 293 294 sc = device_get_softc(bus); 295 296 switch (type) { 297 case SYS_RES_MEMORY: 298 rm = &sc->sc_mem_rman; 299 break; 300 301 case SYS_RES_IOPORT: 302 rm = &sc->sc_io_rman; 303 break; 304 305 case SYS_RES_IRQ: 306 return (bus_alloc_resource(bus, type, rid, start, end, count, 307 flags)); 308 309 default: 310 device_printf(bus, "unknown resource request from %s\n", 311 device_get_nameunit(child)); 312 return (NULL); 313 } 314 315 rv = rman_reserve_resource(rm, start, end, count, flags, child); 316 if (rv == NULL) { 317 device_printf(bus, "failed to reserve resource for %s\n", 318 device_get_nameunit(child)); 319 return (NULL); 320 } 321 322 rman_set_rid(rv, *rid); 323 324 if (needactivate) { 325 if (bus_activate_resource(child, type, *rid, rv) != 0) { 326 device_printf(bus, 327 "failed to activate resource for %s\n", 328 device_get_nameunit(child)); 329 rman_release_resource(rv); 330 return (NULL); 331 } 332 } 333 334 return (rv); 335} 336 337static int 338ofw_pci_release_resource(device_t bus, device_t child, int type, int rid, 339 struct resource *res) 340{ 341 if (rman_get_flags(res) & RF_ACTIVE) { 342 int error = bus_deactivate_resource(child, type, rid, res); 343 if (error) 344 return error; 345 } 346 347 return (rman_release_resource(res)); 348} 349 350static int 351ofw_pci_activate_resource(device_t bus, device_t child, int type, int rid, 352 struct resource *res) 353{ 354 struct ofw_pci_softc *sc; 355 void *p; 356 357 sc = device_get_softc(bus); 358 359 if (type == SYS_RES_IRQ) { 360 return (bus_activate_resource(bus, type, rid, res)); 361 } 362 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 363 struct ofw_pci_range *rp; 364 vm_offset_t start; 365 int space; 366 367 start = (vm_offset_t)rman_get_start(res); 368 369 /* 370 * Map this through the ranges list 371 */ 372 for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange && 373 rp->pci_hi != 0; rp++) { 374 if (start < rp->pci || start >= rp->pci + rp->size) 375 continue; 376 377 switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) { 378 case OFW_PCI_PHYS_HI_SPACE_IO: 379 space = SYS_RES_IOPORT; 380 break; 381 case OFW_PCI_PHYS_HI_SPACE_MEM32: 382 case OFW_PCI_PHYS_HI_SPACE_MEM64: 383 space = SYS_RES_MEMORY; 384 break; 385 default: 386 space = -1; 387 } 388 389 if (type == space) { 390 start += (rp->host - rp->pci); 391 break; 392 } 393 } 394 395 if (bootverbose) 396 printf("ofw_pci mapdev: start %zx, len %ld\n", start, 397 rman_get_size(res)); 398 399 p = pmap_mapdev(start, (vm_size_t)rman_get_size(res)); 400 if (p == NULL) 401 return (ENOMEM); 402 403 rman_set_virtual(res, p); 404 rman_set_bustag(res, &bs_le_tag); 405 rman_set_bushandle(res, (u_long)p); 406 } 407 408 return (rman_activate_resource(res)); 409} 410 411static int 412ofw_pci_deactivate_resource(device_t bus, device_t child, int type, int rid, 413 struct resource *res) 414{ 415 /* 416 * If this is a memory resource, unmap it. 417 */ 418 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 419 u_int32_t psize; 420 421 psize = rman_get_size(res); 422 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize); 423 } 424 425 return (rman_deactivate_resource(res)); 426} 427 428static int 429ofw_pci_adjust_resource(device_t bus, device_t child, int type, 430 struct resource *res, u_long start, u_long end) 431{ 432 struct rman *rm = NULL; 433 struct ofw_pci_softc *sc = device_get_softc(bus); 434 435 switch (type) { 436 case SYS_RES_MEMORY: 437 rm = &sc->sc_mem_rman; 438 break; 439 case SYS_RES_IOPORT: 440 rm = &sc->sc_io_rman; 441 break; 442 default: 443 return (ENXIO); 444 } 445 446 if (!rman_is_region_manager(res, rm)) 447 return (EINVAL); 448 return (rman_adjust_resource(res, start, end)); 449} 450 451 452static phandle_t 453ofw_pci_get_node(device_t bus, device_t dev) 454{ 455 struct ofw_pci_softc *sc; 456 457 sc = device_get_softc(bus); 458 /* We only have one child, the PCI bus, which needs our own node. */ 459 460 return (sc->sc_node); 461} 462 463static int 464ofw_pci_nranges(phandle_t node) 465{ 466 int host_address_cells = 1, pci_address_cells = 3, size_cells = 2; 467 ssize_t nbase_ranges; 468 469 OF_getprop(OF_parent(node), "#address-cells", &host_address_cells, 470 sizeof(host_address_cells)); 471 OF_getprop(node, "#address-cells", &pci_address_cells, 472 sizeof(pci_address_cells)); 473 OF_getprop(node, "#size-cells", &size_cells, sizeof(size_cells)); 474 475 nbase_ranges = OF_getproplen(node, "ranges"); 476 if (nbase_ranges <= 0) 477 return (-1); 478 479 return (nbase_ranges / sizeof(cell_t) / 480 (pci_address_cells + host_address_cells + size_cells)); 481} 482 483static int 484ofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges) 485{ 486 int host_address_cells = 1, pci_address_cells = 3, size_cells = 2; 487 cell_t *base_ranges; 488 ssize_t nbase_ranges; 489 int nranges; 490 int i, j, k; 491 492 OF_getprop(OF_parent(node), "#address-cells", &host_address_cells, 493 sizeof(host_address_cells)); 494 OF_getprop(node, "#address-cells", &pci_address_cells, 495 sizeof(pci_address_cells)); 496 OF_getprop(node, "#size-cells", &size_cells, sizeof(size_cells)); 497 498 nbase_ranges = OF_getproplen(node, "ranges"); 499 if (nbase_ranges <= 0) 500 return (-1); 501 nranges = nbase_ranges / sizeof(cell_t) / 502 (pci_address_cells + host_address_cells + size_cells); 503 504 base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK); 505 OF_getprop(node, "ranges", base_ranges, nbase_ranges); 506 507 for (i = 0, j = 0; i < nranges; i++) { 508 ranges[i].pci_hi = base_ranges[j++]; 509 ranges[i].pci = 0; 510 for (k = 0; k < pci_address_cells - 1; k++) { 511 ranges[i].pci <<= 32; 512 ranges[i].pci |= base_ranges[j++]; 513 } 514 ranges[i].host = 0; 515 for (k = 0; k < host_address_cells; k++) { 516 ranges[i].host <<= 32; 517 ranges[i].host |= base_ranges[j++]; 518 } 519 ranges[i].size = 0; 520 for (k = 0; k < size_cells; k++) { 521 ranges[i].size <<= 32; 522 ranges[i].size |= base_ranges[j++]; 523 } 524 } 525 526 free(base_ranges, M_DEVBUF); 527 return (nranges); 528} 529 530