1230993Snwhitehorn/*- 2230993Snwhitehorn * Copyright (c) 2011 Nathan Whitehorn 3230993Snwhitehorn * All rights reserved. 4230993Snwhitehorn * 5230993Snwhitehorn * Redistribution and use in source and binary forms, with or without 6230993Snwhitehorn * modification, are permitted provided that the following conditions 7230993Snwhitehorn * are met: 8230993Snwhitehorn * 1. Redistributions of source code must retain the above copyright 9230993Snwhitehorn * notice, this list of conditions and the following disclaimer. 10230993Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright 11230993Snwhitehorn * notice, this list of conditions and the following disclaimer in the 12230993Snwhitehorn * documentation and/or other materials provided with the distribution. 13230993Snwhitehorn * 14230993Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15230993Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16230993Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17230993Snwhitehorn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18230993Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19230993Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20230993Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21230993Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22230993Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23230993Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24230993Snwhitehorn * SUCH DAMAGE. 25230993Snwhitehorn */ 26230993Snwhitehorn 27230993Snwhitehorn#include <sys/cdefs.h> 28230993Snwhitehorn__FBSDID("$FreeBSD: releng/11.0/sys/powerpc/ofw/ofw_pci.c 297000 2016-03-18 01:28:41Z jhibbits $"); 29230993Snwhitehorn#include <sys/param.h> 30230993Snwhitehorn#include <sys/systm.h> 31230993Snwhitehorn#include <sys/module.h> 32230993Snwhitehorn#include <sys/bus.h> 33230993Snwhitehorn#include <sys/conf.h> 34230993Snwhitehorn#include <sys/kernel.h> 35230993Snwhitehorn 36230993Snwhitehorn#include <dev/ofw/openfirm.h> 37230993Snwhitehorn#include <dev/ofw/ofw_pci.h> 38230993Snwhitehorn#include <dev/ofw/ofw_bus.h> 39230993Snwhitehorn#include <dev/ofw/ofw_bus_subr.h> 40230993Snwhitehorn 41230993Snwhitehorn#include <dev/pci/pcivar.h> 42230993Snwhitehorn#include <dev/pci/pcireg.h> 43230993Snwhitehorn 44230993Snwhitehorn#include <machine/bus.h> 45230993Snwhitehorn#include <machine/intr_machdep.h> 46230993Snwhitehorn#include <machine/md_var.h> 47230993Snwhitehorn#include <machine/pio.h> 48230993Snwhitehorn#include <machine/resource.h> 49230993Snwhitehorn 50230993Snwhitehorn#include <sys/rman.h> 51230993Snwhitehorn 52230993Snwhitehorn#include <vm/vm.h> 53230993Snwhitehorn#include <vm/pmap.h> 54230993Snwhitehorn 55230993Snwhitehorn#include <powerpc/ofw/ofw_pci.h> 56230993Snwhitehorn 57230993Snwhitehorn#include "pcib_if.h" 58230993Snwhitehorn 59230993Snwhitehorn/* 60230993Snwhitehorn * Bus interface. 61230993Snwhitehorn */ 62230993Snwhitehornstatic int ofw_pci_read_ivar(device_t, device_t, int, 63230993Snwhitehorn uintptr_t *); 64230993Snwhitehornstatic struct resource * ofw_pci_alloc_resource(device_t bus, 65294883Sjhibbits device_t child, int type, int *rid, rman_res_t start, 66294883Sjhibbits rman_res_t end, rman_res_t count, u_int flags); 67230993Snwhitehornstatic int ofw_pci_release_resource(device_t bus, device_t child, 68230993Snwhitehorn int type, int rid, struct resource *res); 69230993Snwhitehornstatic int ofw_pci_activate_resource(device_t bus, device_t child, 70230993Snwhitehorn int type, int rid, struct resource *res); 71230993Snwhitehornstatic int ofw_pci_deactivate_resource(device_t bus, 72230993Snwhitehorn device_t child, int type, int rid, 73230993Snwhitehorn struct resource *res); 74231003Snwhitehornstatic int ofw_pci_adjust_resource(device_t bus, device_t child, 75294883Sjhibbits int type, struct resource *res, rman_res_t start, 76294883Sjhibbits rman_res_t end); 77230993Snwhitehorn 78230993Snwhitehorn/* 79230993Snwhitehorn * pcib interface. 80230993Snwhitehorn */ 81230993Snwhitehornstatic int ofw_pci_maxslots(device_t); 82230993Snwhitehornstatic int ofw_pci_route_interrupt(device_t, device_t, int); 83230993Snwhitehorn 84230993Snwhitehorn/* 85230993Snwhitehorn * ofw_bus interface 86230993Snwhitehorn */ 87230993Snwhitehornstatic phandle_t ofw_pci_get_node(device_t bus, device_t dev); 88230993Snwhitehorn 89230993Snwhitehorn/* 90230993Snwhitehorn * local methods 91230993Snwhitehorn */ 92230993Snwhitehorn 93230993Snwhitehornstatic int ofw_pci_nranges(phandle_t node); 94230993Snwhitehornstatic int ofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges); 95230993Snwhitehorn 96230993Snwhitehorn/* 97230993Snwhitehorn * Driver methods. 98230993Snwhitehorn */ 99230993Snwhitehornstatic device_method_t ofw_pci_methods[] = { 100230993Snwhitehorn /* Device interface */ 101230993Snwhitehorn DEVMETHOD(device_attach, ofw_pci_attach), 102230993Snwhitehorn 103230993Snwhitehorn /* Bus interface */ 104230993Snwhitehorn DEVMETHOD(bus_print_child, bus_generic_print_child), 105230993Snwhitehorn DEVMETHOD(bus_read_ivar, ofw_pci_read_ivar), 106230993Snwhitehorn DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 107230993Snwhitehorn DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 108230993Snwhitehorn DEVMETHOD(bus_alloc_resource, ofw_pci_alloc_resource), 109230993Snwhitehorn DEVMETHOD(bus_release_resource, ofw_pci_release_resource), 110230993Snwhitehorn DEVMETHOD(bus_activate_resource, ofw_pci_activate_resource), 111230993Snwhitehorn DEVMETHOD(bus_deactivate_resource, ofw_pci_deactivate_resource), 112231003Snwhitehorn DEVMETHOD(bus_adjust_resource, ofw_pci_adjust_resource), 113230993Snwhitehorn 114230993Snwhitehorn /* pcib interface */ 115230993Snwhitehorn DEVMETHOD(pcib_maxslots, ofw_pci_maxslots), 116230993Snwhitehorn DEVMETHOD(pcib_route_interrupt, ofw_pci_route_interrupt), 117230993Snwhitehorn 118230993Snwhitehorn /* ofw_bus interface */ 119230993Snwhitehorn DEVMETHOD(ofw_bus_get_node, ofw_pci_get_node), 120230993Snwhitehorn 121230993Snwhitehorn DEVMETHOD_END 122230993Snwhitehorn}; 123230993Snwhitehorn 124230993SnwhitehornDEFINE_CLASS_0(ofw_pci, ofw_pci_driver, ofw_pci_methods, 0); 125230993Snwhitehorn 126230993Snwhitehornint 127256846Snwhitehornofw_pci_init(device_t dev) 128230993Snwhitehorn{ 129230993Snwhitehorn struct ofw_pci_softc *sc; 130230993Snwhitehorn phandle_t node; 131230993Snwhitehorn u_int32_t busrange[2]; 132230993Snwhitehorn struct ofw_pci_range *rp; 133230993Snwhitehorn int error; 134230993Snwhitehorn 135230993Snwhitehorn node = ofw_bus_get_node(dev); 136230993Snwhitehorn sc = device_get_softc(dev); 137256846Snwhitehorn sc->sc_initialized = 1; 138230993Snwhitehorn 139290989Snwhitehorn if (OF_getencprop(node, "bus-range", busrange, sizeof(busrange)) != 8) 140230993Snwhitehorn busrange[0] = 0; 141230993Snwhitehorn 142230993Snwhitehorn sc->sc_dev = dev; 143230993Snwhitehorn sc->sc_node = node; 144230993Snwhitehorn sc->sc_bus = busrange[0]; 145230993Snwhitehorn 146230993Snwhitehorn if (sc->sc_quirks & OFW_PCI_QUIRK_RANGES_ON_CHILDREN) { 147230993Snwhitehorn phandle_t c; 148230993Snwhitehorn int n, i; 149230993Snwhitehorn 150230993Snwhitehorn sc->sc_nrange = 0; 151230993Snwhitehorn for (c = OF_child(node); c != 0; c = OF_peer(c)) { 152230993Snwhitehorn n = ofw_pci_nranges(c); 153230993Snwhitehorn if (n > 0) 154230993Snwhitehorn sc->sc_nrange += n; 155230993Snwhitehorn } 156230993Snwhitehorn if (sc->sc_nrange == 0) 157230993Snwhitehorn return (ENXIO); 158230993Snwhitehorn sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]), 159230993Snwhitehorn M_DEVBUF, M_WAITOK); 160230993Snwhitehorn i = 0; 161230993Snwhitehorn for (c = OF_child(node); c != 0; c = OF_peer(c)) { 162230993Snwhitehorn n = ofw_pci_fill_ranges(c, &sc->sc_range[i]); 163230993Snwhitehorn if (n > 0) 164230993Snwhitehorn i += n; 165230993Snwhitehorn } 166230993Snwhitehorn KASSERT(i == sc->sc_nrange, ("range count mismatch")); 167230993Snwhitehorn } else { 168230993Snwhitehorn sc->sc_nrange = ofw_pci_nranges(node); 169230993Snwhitehorn if (sc->sc_nrange <= 0) { 170230993Snwhitehorn device_printf(dev, "could not get ranges\n"); 171230993Snwhitehorn return (ENXIO); 172230993Snwhitehorn } 173230993Snwhitehorn sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]), 174230993Snwhitehorn M_DEVBUF, M_WAITOK); 175230993Snwhitehorn ofw_pci_fill_ranges(node, sc->sc_range); 176230993Snwhitehorn } 177230993Snwhitehorn 178230993Snwhitehorn sc->sc_io_rman.rm_type = RMAN_ARRAY; 179230993Snwhitehorn sc->sc_io_rman.rm_descr = "PCI I/O Ports"; 180230993Snwhitehorn error = rman_init(&sc->sc_io_rman); 181230993Snwhitehorn if (error) { 182230993Snwhitehorn device_printf(dev, "rman_init() failed. error = %d\n", error); 183230993Snwhitehorn return (error); 184230993Snwhitehorn } 185230993Snwhitehorn 186230993Snwhitehorn sc->sc_mem_rman.rm_type = RMAN_ARRAY; 187230993Snwhitehorn sc->sc_mem_rman.rm_descr = "PCI Memory"; 188230993Snwhitehorn error = rman_init(&sc->sc_mem_rman); 189230993Snwhitehorn if (error) { 190230993Snwhitehorn device_printf(dev, "rman_init() failed. error = %d\n", error); 191230993Snwhitehorn return (error); 192230993Snwhitehorn } 193230993Snwhitehorn 194230993Snwhitehorn for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange && 195230993Snwhitehorn rp->pci_hi != 0; rp++) { 196230993Snwhitehorn error = 0; 197230993Snwhitehorn 198230993Snwhitehorn switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) { 199230993Snwhitehorn case OFW_PCI_PHYS_HI_SPACE_CONFIG: 200230993Snwhitehorn break; 201230993Snwhitehorn case OFW_PCI_PHYS_HI_SPACE_IO: 202230993Snwhitehorn error = rman_manage_region(&sc->sc_io_rman, rp->pci, 203230993Snwhitehorn rp->pci + rp->size - 1); 204230993Snwhitehorn break; 205230993Snwhitehorn case OFW_PCI_PHYS_HI_SPACE_MEM32: 206230993Snwhitehorn case OFW_PCI_PHYS_HI_SPACE_MEM64: 207230993Snwhitehorn error = rman_manage_region(&sc->sc_mem_rman, rp->pci, 208230993Snwhitehorn rp->pci + rp->size - 1); 209230993Snwhitehorn break; 210230993Snwhitehorn } 211230993Snwhitehorn 212230993Snwhitehorn if (error) { 213230993Snwhitehorn device_printf(dev, 214230993Snwhitehorn "rman_manage_region(%x, %#jx, %#jx) failed. " 215230993Snwhitehorn "error = %d\n", rp->pci_hi & 216230993Snwhitehorn OFW_PCI_PHYS_HI_SPACEMASK, rp->pci, 217230993Snwhitehorn rp->pci + rp->size - 1, error); 218230993Snwhitehorn return (error); 219230993Snwhitehorn } 220230993Snwhitehorn } 221230993Snwhitehorn 222230993Snwhitehorn ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(cell_t)); 223230993Snwhitehorn 224256846Snwhitehorn return (error); 225256846Snwhitehorn} 226256846Snwhitehorn 227256846Snwhitehornint 228256846Snwhitehornofw_pci_attach(device_t dev) 229256846Snwhitehorn{ 230256846Snwhitehorn struct ofw_pci_softc *sc; 231256846Snwhitehorn int error; 232256846Snwhitehorn 233256846Snwhitehorn sc = device_get_softc(dev); 234256846Snwhitehorn if (!sc->sc_initialized) { 235256846Snwhitehorn error = ofw_pci_init(dev); 236256846Snwhitehorn if (error) 237256846Snwhitehorn return (error); 238256846Snwhitehorn } 239256846Snwhitehorn 240287882Szbb device_add_child(dev, "pci", -1); 241230993Snwhitehorn return (bus_generic_attach(dev)); 242230993Snwhitehorn} 243230993Snwhitehorn 244230993Snwhitehornstatic int 245230993Snwhitehornofw_pci_maxslots(device_t dev) 246230993Snwhitehorn{ 247230993Snwhitehorn 248230993Snwhitehorn return (PCI_SLOTMAX); 249230993Snwhitehorn} 250230993Snwhitehorn 251230993Snwhitehornstatic int 252230993Snwhitehornofw_pci_route_interrupt(device_t bus, device_t dev, int pin) 253230993Snwhitehorn{ 254230993Snwhitehorn struct ofw_pci_softc *sc; 255230993Snwhitehorn struct ofw_pci_register reg; 256259513Snwhitehorn uint32_t pintr, mintr[2]; 257259513Snwhitehorn int intrcells; 258230993Snwhitehorn phandle_t iparent; 259230993Snwhitehorn 260230993Snwhitehorn sc = device_get_softc(bus); 261230993Snwhitehorn pintr = pin; 262256816Snwhitehorn 263256816Snwhitehorn /* Fabricate imap information in case this isn't an OFW device */ 264256816Snwhitehorn bzero(®, sizeof(reg)); 265256816Snwhitehorn reg.phys_hi = (pci_get_bus(dev) << OFW_PCI_PHYS_HI_BUSSHIFT) | 266256816Snwhitehorn (pci_get_slot(dev) << OFW_PCI_PHYS_HI_DEVICESHIFT) | 267256816Snwhitehorn (pci_get_function(dev) << OFW_PCI_PHYS_HI_FUNCTIONSHIFT); 268256816Snwhitehorn 269259513Snwhitehorn intrcells = ofw_bus_lookup_imap(ofw_bus_get_node(dev), 270259513Snwhitehorn &sc->sc_pci_iinfo, ®, sizeof(reg), &pintr, sizeof(pintr), 271259516Snwhitehorn mintr, sizeof(mintr), &iparent); 272259513Snwhitehorn if (intrcells) { 273261351Snwhitehorn pintr = ofw_bus_map_intr(dev, iparent, intrcells, mintr); 274259513Snwhitehorn return (pintr); 275259513Snwhitehorn } 276230993Snwhitehorn 277230993Snwhitehorn /* Maybe it's a real interrupt, not an intpin */ 278230993Snwhitehorn if (pin > 4) 279230993Snwhitehorn return (pin); 280230993Snwhitehorn 281230993Snwhitehorn device_printf(bus, "could not route pin %d for device %d.%d\n", 282230993Snwhitehorn pin, pci_get_slot(dev), pci_get_function(dev)); 283230993Snwhitehorn return (PCI_INVALID_IRQ); 284230993Snwhitehorn} 285230993Snwhitehorn 286230993Snwhitehornstatic int 287230993Snwhitehornofw_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 288230993Snwhitehorn{ 289230993Snwhitehorn struct ofw_pci_softc *sc; 290230993Snwhitehorn 291230993Snwhitehorn sc = device_get_softc(dev); 292230993Snwhitehorn 293230993Snwhitehorn switch (which) { 294230993Snwhitehorn case PCIB_IVAR_DOMAIN: 295230993Snwhitehorn *result = device_get_unit(dev); 296230993Snwhitehorn return (0); 297230993Snwhitehorn case PCIB_IVAR_BUS: 298230993Snwhitehorn *result = sc->sc_bus; 299230993Snwhitehorn return (0); 300230993Snwhitehorn } 301230993Snwhitehorn 302230993Snwhitehorn return (ENOENT); 303230993Snwhitehorn} 304230993Snwhitehorn 305230993Snwhitehornstatic struct resource * 306230993Snwhitehornofw_pci_alloc_resource(device_t bus, device_t child, int type, int *rid, 307294883Sjhibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 308230993Snwhitehorn{ 309230993Snwhitehorn struct ofw_pci_softc *sc; 310230993Snwhitehorn struct resource *rv; 311230993Snwhitehorn struct rman *rm; 312230993Snwhitehorn int needactivate; 313230993Snwhitehorn 314230993Snwhitehorn needactivate = flags & RF_ACTIVE; 315230993Snwhitehorn flags &= ~RF_ACTIVE; 316230993Snwhitehorn 317230993Snwhitehorn sc = device_get_softc(bus); 318230993Snwhitehorn 319230993Snwhitehorn switch (type) { 320230993Snwhitehorn case SYS_RES_MEMORY: 321230993Snwhitehorn rm = &sc->sc_mem_rman; 322230993Snwhitehorn break; 323230993Snwhitehorn 324230993Snwhitehorn case SYS_RES_IOPORT: 325230993Snwhitehorn rm = &sc->sc_io_rman; 326230993Snwhitehorn break; 327230993Snwhitehorn 328230993Snwhitehorn case SYS_RES_IRQ: 329230993Snwhitehorn return (bus_alloc_resource(bus, type, rid, start, end, count, 330230993Snwhitehorn flags)); 331230993Snwhitehorn 332230993Snwhitehorn default: 333230993Snwhitehorn device_printf(bus, "unknown resource request from %s\n", 334230993Snwhitehorn device_get_nameunit(child)); 335230993Snwhitehorn return (NULL); 336230993Snwhitehorn } 337230993Snwhitehorn 338230993Snwhitehorn rv = rman_reserve_resource(rm, start, end, count, flags, child); 339230993Snwhitehorn if (rv == NULL) { 340230993Snwhitehorn device_printf(bus, "failed to reserve resource for %s\n", 341230993Snwhitehorn device_get_nameunit(child)); 342230993Snwhitehorn return (NULL); 343230993Snwhitehorn } 344230993Snwhitehorn 345230993Snwhitehorn rman_set_rid(rv, *rid); 346230993Snwhitehorn 347230993Snwhitehorn if (needactivate) { 348230993Snwhitehorn if (bus_activate_resource(child, type, *rid, rv) != 0) { 349230993Snwhitehorn device_printf(bus, 350230993Snwhitehorn "failed to activate resource for %s\n", 351230993Snwhitehorn device_get_nameunit(child)); 352230993Snwhitehorn rman_release_resource(rv); 353230993Snwhitehorn return (NULL); 354230993Snwhitehorn } 355230993Snwhitehorn } 356230993Snwhitehorn 357230993Snwhitehorn return (rv); 358230993Snwhitehorn} 359230993Snwhitehorn 360230993Snwhitehornstatic int 361230993Snwhitehornofw_pci_release_resource(device_t bus, device_t child, int type, int rid, 362230993Snwhitehorn struct resource *res) 363230993Snwhitehorn{ 364230993Snwhitehorn if (rman_get_flags(res) & RF_ACTIVE) { 365230993Snwhitehorn int error = bus_deactivate_resource(child, type, rid, res); 366230993Snwhitehorn if (error) 367230993Snwhitehorn return error; 368230993Snwhitehorn } 369230993Snwhitehorn 370230993Snwhitehorn return (rman_release_resource(res)); 371230993Snwhitehorn} 372230993Snwhitehorn 373230993Snwhitehornstatic int 374230993Snwhitehornofw_pci_activate_resource(device_t bus, device_t child, int type, int rid, 375230993Snwhitehorn struct resource *res) 376230993Snwhitehorn{ 377230993Snwhitehorn struct ofw_pci_softc *sc; 378230993Snwhitehorn void *p; 379230993Snwhitehorn 380230993Snwhitehorn sc = device_get_softc(bus); 381230993Snwhitehorn 382230993Snwhitehorn if (type == SYS_RES_IRQ) { 383230993Snwhitehorn return (bus_activate_resource(bus, type, rid, res)); 384230993Snwhitehorn } 385230993Snwhitehorn if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 386230993Snwhitehorn struct ofw_pci_range *rp; 387297000Sjhibbits vm_paddr_t start; 388230993Snwhitehorn int space; 389230993Snwhitehorn 390297000Sjhibbits start = (vm_paddr_t)rman_get_start(res); 391230993Snwhitehorn 392230993Snwhitehorn /* 393230993Snwhitehorn * Map this through the ranges list 394230993Snwhitehorn */ 395230993Snwhitehorn for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange && 396230993Snwhitehorn rp->pci_hi != 0; rp++) { 397230993Snwhitehorn if (start < rp->pci || start >= rp->pci + rp->size) 398230993Snwhitehorn continue; 399230993Snwhitehorn 400230993Snwhitehorn switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) { 401230993Snwhitehorn case OFW_PCI_PHYS_HI_SPACE_IO: 402230993Snwhitehorn space = SYS_RES_IOPORT; 403230993Snwhitehorn break; 404230993Snwhitehorn case OFW_PCI_PHYS_HI_SPACE_MEM32: 405230993Snwhitehorn case OFW_PCI_PHYS_HI_SPACE_MEM64: 406230993Snwhitehorn space = SYS_RES_MEMORY; 407230993Snwhitehorn break; 408230993Snwhitehorn default: 409230993Snwhitehorn space = -1; 410230993Snwhitehorn } 411230993Snwhitehorn 412230993Snwhitehorn if (type == space) { 413230993Snwhitehorn start += (rp->host - rp->pci); 414230993Snwhitehorn break; 415230993Snwhitehorn } 416230993Snwhitehorn } 417230993Snwhitehorn 418230993Snwhitehorn if (bootverbose) 419297000Sjhibbits printf("ofw_pci mapdev: start %jx, len %jd\n", 420297000Sjhibbits (rman_res_t)start, rman_get_size(res)); 421230993Snwhitehorn 422230993Snwhitehorn p = pmap_mapdev(start, (vm_size_t)rman_get_size(res)); 423230993Snwhitehorn if (p == NULL) 424230993Snwhitehorn return (ENOMEM); 425230993Snwhitehorn 426230993Snwhitehorn rman_set_virtual(res, p); 427230993Snwhitehorn rman_set_bustag(res, &bs_le_tag); 428230993Snwhitehorn rman_set_bushandle(res, (u_long)p); 429230993Snwhitehorn } 430230993Snwhitehorn 431230993Snwhitehorn return (rman_activate_resource(res)); 432230993Snwhitehorn} 433230993Snwhitehorn 434230993Snwhitehornstatic int 435230993Snwhitehornofw_pci_deactivate_resource(device_t bus, device_t child, int type, int rid, 436230993Snwhitehorn struct resource *res) 437230993Snwhitehorn{ 438230993Snwhitehorn /* 439230993Snwhitehorn * If this is a memory resource, unmap it. 440230993Snwhitehorn */ 441230993Snwhitehorn if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 442230993Snwhitehorn u_int32_t psize; 443230993Snwhitehorn 444230993Snwhitehorn psize = rman_get_size(res); 445230993Snwhitehorn pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize); 446230993Snwhitehorn } 447230993Snwhitehorn 448230993Snwhitehorn return (rman_deactivate_resource(res)); 449230993Snwhitehorn} 450230993Snwhitehorn 451231003Snwhitehornstatic int 452231003Snwhitehornofw_pci_adjust_resource(device_t bus, device_t child, int type, 453294883Sjhibbits struct resource *res, rman_res_t start, rman_res_t end) 454231003Snwhitehorn{ 455231003Snwhitehorn struct rman *rm = NULL; 456231003Snwhitehorn struct ofw_pci_softc *sc = device_get_softc(bus); 457231003Snwhitehorn 458231149Snwhitehorn KASSERT(!(rman_get_flags(res) & RF_ACTIVE), 459231149Snwhitehorn ("active resources cannot be adjusted")); 460231149Snwhitehorn if (rman_get_flags(res) & RF_ACTIVE) 461231149Snwhitehorn return (EINVAL); 462231149Snwhitehorn 463231003Snwhitehorn switch (type) { 464231003Snwhitehorn case SYS_RES_MEMORY: 465231003Snwhitehorn rm = &sc->sc_mem_rman; 466231003Snwhitehorn break; 467231003Snwhitehorn case SYS_RES_IOPORT: 468231003Snwhitehorn rm = &sc->sc_io_rman; 469231003Snwhitehorn break; 470231003Snwhitehorn default: 471231003Snwhitehorn return (ENXIO); 472231003Snwhitehorn } 473231003Snwhitehorn 474231003Snwhitehorn if (!rman_is_region_manager(res, rm)) 475231003Snwhitehorn return (EINVAL); 476231026Snwhitehorn 477231149Snwhitehorn return (rman_adjust_resource(res, start, end)); 478231003Snwhitehorn} 479231003Snwhitehorn 480231003Snwhitehorn 481230993Snwhitehornstatic phandle_t 482230993Snwhitehornofw_pci_get_node(device_t bus, device_t dev) 483230993Snwhitehorn{ 484230993Snwhitehorn struct ofw_pci_softc *sc; 485230993Snwhitehorn 486230993Snwhitehorn sc = device_get_softc(bus); 487230993Snwhitehorn /* We only have one child, the PCI bus, which needs our own node. */ 488230993Snwhitehorn 489230993Snwhitehorn return (sc->sc_node); 490230993Snwhitehorn} 491230993Snwhitehorn 492230993Snwhitehornstatic int 493230993Snwhitehornofw_pci_nranges(phandle_t node) 494230993Snwhitehorn{ 495230993Snwhitehorn int host_address_cells = 1, pci_address_cells = 3, size_cells = 2; 496230993Snwhitehorn ssize_t nbase_ranges; 497230993Snwhitehorn 498290989Snwhitehorn OF_getencprop(OF_parent(node), "#address-cells", &host_address_cells, 499230993Snwhitehorn sizeof(host_address_cells)); 500290989Snwhitehorn OF_getencprop(node, "#address-cells", &pci_address_cells, 501230993Snwhitehorn sizeof(pci_address_cells)); 502290989Snwhitehorn OF_getencprop(node, "#size-cells", &size_cells, sizeof(size_cells)); 503230993Snwhitehorn 504230993Snwhitehorn nbase_ranges = OF_getproplen(node, "ranges"); 505230993Snwhitehorn if (nbase_ranges <= 0) 506230993Snwhitehorn return (-1); 507230993Snwhitehorn 508230993Snwhitehorn return (nbase_ranges / sizeof(cell_t) / 509230993Snwhitehorn (pci_address_cells + host_address_cells + size_cells)); 510230993Snwhitehorn} 511230993Snwhitehorn 512230993Snwhitehornstatic int 513230993Snwhitehornofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges) 514230993Snwhitehorn{ 515230993Snwhitehorn int host_address_cells = 1, pci_address_cells = 3, size_cells = 2; 516230993Snwhitehorn cell_t *base_ranges; 517230993Snwhitehorn ssize_t nbase_ranges; 518230993Snwhitehorn int nranges; 519230993Snwhitehorn int i, j, k; 520230993Snwhitehorn 521290989Snwhitehorn OF_getencprop(OF_parent(node), "#address-cells", &host_address_cells, 522230993Snwhitehorn sizeof(host_address_cells)); 523290989Snwhitehorn OF_getencprop(node, "#address-cells", &pci_address_cells, 524230993Snwhitehorn sizeof(pci_address_cells)); 525290989Snwhitehorn OF_getencprop(node, "#size-cells", &size_cells, sizeof(size_cells)); 526230993Snwhitehorn 527230993Snwhitehorn nbase_ranges = OF_getproplen(node, "ranges"); 528230993Snwhitehorn if (nbase_ranges <= 0) 529230993Snwhitehorn return (-1); 530230993Snwhitehorn nranges = nbase_ranges / sizeof(cell_t) / 531230993Snwhitehorn (pci_address_cells + host_address_cells + size_cells); 532230993Snwhitehorn 533230993Snwhitehorn base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK); 534290989Snwhitehorn OF_getencprop(node, "ranges", base_ranges, nbase_ranges); 535230993Snwhitehorn 536230993Snwhitehorn for (i = 0, j = 0; i < nranges; i++) { 537230993Snwhitehorn ranges[i].pci_hi = base_ranges[j++]; 538230993Snwhitehorn ranges[i].pci = 0; 539230993Snwhitehorn for (k = 0; k < pci_address_cells - 1; k++) { 540230993Snwhitehorn ranges[i].pci <<= 32; 541230993Snwhitehorn ranges[i].pci |= base_ranges[j++]; 542230993Snwhitehorn } 543230993Snwhitehorn ranges[i].host = 0; 544230993Snwhitehorn for (k = 0; k < host_address_cells; k++) { 545230993Snwhitehorn ranges[i].host <<= 32; 546230993Snwhitehorn ranges[i].host |= base_ranges[j++]; 547230993Snwhitehorn } 548230993Snwhitehorn ranges[i].size = 0; 549230993Snwhitehorn for (k = 0; k < size_cells; k++) { 550230993Snwhitehorn ranges[i].size <<= 32; 551230993Snwhitehorn ranges[i].size |= base_ranges[j++]; 552230993Snwhitehorn } 553230993Snwhitehorn } 554230993Snwhitehorn 555230993Snwhitehorn free(base_ranges, M_DEVBUF); 556230993Snwhitehorn return (nranges); 557230993Snwhitehorn} 558230993Snwhitehorn 559