165d08437SNathan Whitehorn /*- 265d08437SNathan Whitehorn * Copyright 1998 Massachusetts Institute of Technology 365d08437SNathan Whitehorn * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>. 465d08437SNathan Whitehorn * Copyright 2006 by Marius Strobl <marius@FreeBSD.org>. 565d08437SNathan Whitehorn * All rights reserved. 665d08437SNathan Whitehorn * 765d08437SNathan Whitehorn * Permission to use, copy, modify, and distribute this software and 865d08437SNathan Whitehorn * its documentation for any purpose and without fee is hereby 965d08437SNathan Whitehorn * granted, provided that both the above copyright notice and this 1065d08437SNathan Whitehorn * permission notice appear in all copies, that both the above 1165d08437SNathan Whitehorn * copyright notice and this permission notice appear in all 1265d08437SNathan Whitehorn * supporting documentation, and that the name of M.I.T. not be used 1365d08437SNathan Whitehorn * in advertising or publicity pertaining to distribution of the 1465d08437SNathan Whitehorn * software without specific, written prior permission. M.I.T. makes 1565d08437SNathan Whitehorn * no representations about the suitability of this software for any 1665d08437SNathan Whitehorn * purpose. It is provided "as is" without express or implied 1765d08437SNathan Whitehorn * warranty. 1865d08437SNathan Whitehorn * 1965d08437SNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 2065d08437SNathan Whitehorn * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 2165d08437SNathan Whitehorn * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 2265d08437SNathan Whitehorn * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 2365d08437SNathan Whitehorn * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2465d08437SNathan Whitehorn * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2565d08437SNathan Whitehorn * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 2665d08437SNathan Whitehorn * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 2765d08437SNathan Whitehorn * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2865d08437SNathan Whitehorn * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 2965d08437SNathan Whitehorn * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3065d08437SNathan Whitehorn * SUCH DAMAGE. 3165d08437SNathan Whitehorn * 3265d08437SNathan Whitehorn * from: FreeBSD: src/sys/i386/i386/nexus.c,v 1.43 2001/02/09 3365d08437SNathan Whitehorn */ 3465d08437SNathan Whitehorn 3565d08437SNathan Whitehorn #include <sys/cdefs.h> 3665d08437SNathan Whitehorn __FBSDID("$FreeBSD$"); 3765d08437SNathan Whitehorn 3865d08437SNathan Whitehorn #include <sys/param.h> 3965d08437SNathan Whitehorn #include <sys/systm.h> 4065d08437SNathan Whitehorn #include <sys/bus.h> 4165d08437SNathan Whitehorn #include <sys/kernel.h> 4265d08437SNathan Whitehorn #include <sys/malloc.h> 4365d08437SNathan Whitehorn #include <sys/module.h> 4465d08437SNathan Whitehorn #include <sys/pcpu.h> 4565d08437SNathan Whitehorn #include <sys/rman.h> 4665d08437SNathan Whitehorn 4765d08437SNathan Whitehorn #include <vm/vm.h> 4865d08437SNathan Whitehorn #include <vm/pmap.h> 4965d08437SNathan Whitehorn 5065d08437SNathan Whitehorn #include <dev/ofw/ofw_bus.h> 5165d08437SNathan Whitehorn #include <dev/ofw/ofw_bus_subr.h> 5265d08437SNathan Whitehorn #include <dev/ofw/openfirm.h> 53*ecaecbc7SIan Lepore #include <dev/fdt/simplebus.h> 5465d08437SNathan Whitehorn 5565d08437SNathan Whitehorn #include <machine/bus.h> 5665d08437SNathan Whitehorn #include <machine/resource.h> 5765d08437SNathan Whitehorn 5865d08437SNathan Whitehorn /* 5965d08437SNathan Whitehorn * The ofwbus (which is a pseudo-bus actually) iterates over the nodes that 6065d08437SNathan Whitehorn * hang from the Open Firmware root node and adds them as devices to this bus 6165d08437SNathan Whitehorn * (except some special nodes which are excluded) so that drivers can be 6265d08437SNathan Whitehorn * attached to them. 6365d08437SNathan Whitehorn * 6465d08437SNathan Whitehorn */ 6565d08437SNathan Whitehorn 6665d08437SNathan Whitehorn struct ofwbus_softc { 67*ecaecbc7SIan Lepore struct simplebus_softc simplebus_sc; 6865d08437SNathan Whitehorn struct rman sc_intr_rman; 6965d08437SNathan Whitehorn struct rman sc_mem_rman; 7065d08437SNathan Whitehorn }; 7165d08437SNathan Whitehorn 7265d08437SNathan Whitehorn static device_identify_t ofwbus_identify; 7365d08437SNathan Whitehorn static device_probe_t ofwbus_probe; 7465d08437SNathan Whitehorn static device_attach_t ofwbus_attach; 7565d08437SNathan Whitehorn static bus_alloc_resource_t ofwbus_alloc_resource; 7665d08437SNathan Whitehorn static bus_adjust_resource_t ofwbus_adjust_resource; 7765d08437SNathan Whitehorn static bus_release_resource_t ofwbus_release_resource; 7865d08437SNathan Whitehorn 7965d08437SNathan Whitehorn static device_method_t ofwbus_methods[] = { 8065d08437SNathan Whitehorn /* Device interface */ 8165d08437SNathan Whitehorn DEVMETHOD(device_identify, ofwbus_identify), 8265d08437SNathan Whitehorn DEVMETHOD(device_probe, ofwbus_probe), 8365d08437SNathan Whitehorn DEVMETHOD(device_attach, ofwbus_attach), 8465d08437SNathan Whitehorn 8565d08437SNathan Whitehorn /* Bus interface */ 8665d08437SNathan Whitehorn DEVMETHOD(bus_alloc_resource, ofwbus_alloc_resource), 8765d08437SNathan Whitehorn DEVMETHOD(bus_adjust_resource, ofwbus_adjust_resource), 8865d08437SNathan Whitehorn DEVMETHOD(bus_release_resource, ofwbus_release_resource), 8965d08437SNathan Whitehorn 9065d08437SNathan Whitehorn DEVMETHOD_END 9165d08437SNathan Whitehorn }; 9265d08437SNathan Whitehorn 93*ecaecbc7SIan Lepore DEFINE_CLASS_1(ofwbus, ofwbus_driver, ofwbus_methods, 94*ecaecbc7SIan Lepore sizeof(struct ofwbus_softc), simplebus_driver); 9565d08437SNathan Whitehorn static devclass_t ofwbus_devclass; 962d12d35cSIan Lepore EARLY_DRIVER_MODULE(ofwbus, nexus, ofwbus_driver, ofwbus_devclass, 0, 0, 97633dbf2eSIan Lepore BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); 9865d08437SNathan Whitehorn MODULE_VERSION(ofwbus, 1); 9965d08437SNathan Whitehorn 10065d08437SNathan Whitehorn static void 10165d08437SNathan Whitehorn ofwbus_identify(driver_t *driver, device_t parent) 10265d08437SNathan Whitehorn { 10365d08437SNathan Whitehorn 10465d08437SNathan Whitehorn /* Check if Open Firmware has been instantiated */ 105e2fc1af4SNathan Whitehorn if (OF_peer(0) == 0) 10665d08437SNathan Whitehorn return; 10765d08437SNathan Whitehorn 10865d08437SNathan Whitehorn if (device_find_child(parent, "ofwbus", -1) == NULL) 10965d08437SNathan Whitehorn BUS_ADD_CHILD(parent, 0, "ofwbus", -1); 11065d08437SNathan Whitehorn } 11165d08437SNathan Whitehorn 11265d08437SNathan Whitehorn static int 11365d08437SNathan Whitehorn ofwbus_probe(device_t dev) 11465d08437SNathan Whitehorn { 11565d08437SNathan Whitehorn 11665d08437SNathan Whitehorn device_set_desc(dev, "Open Firmware Device Tree"); 11765d08437SNathan Whitehorn return (BUS_PROBE_NOWILDCARD); 11865d08437SNathan Whitehorn } 11965d08437SNathan Whitehorn 12065d08437SNathan Whitehorn static int 12165d08437SNathan Whitehorn ofwbus_attach(device_t dev) 12265d08437SNathan Whitehorn { 12365d08437SNathan Whitehorn struct ofwbus_softc *sc; 12465d08437SNathan Whitehorn phandle_t node; 125*ecaecbc7SIan Lepore struct ofw_bus_devinfo obd; 12665d08437SNathan Whitehorn 12765d08437SNathan Whitehorn sc = device_get_softc(dev); 12865d08437SNathan Whitehorn 12965d08437SNathan Whitehorn node = OF_peer(0); 13065d08437SNathan Whitehorn 13165d08437SNathan Whitehorn /* 13265d08437SNathan Whitehorn * If no Open Firmware, bail early 13365d08437SNathan Whitehorn */ 13465d08437SNathan Whitehorn if (node == -1) 13565d08437SNathan Whitehorn return (ENXIO); 13665d08437SNathan Whitehorn 137*ecaecbc7SIan Lepore /* 138*ecaecbc7SIan Lepore * ofwbus bus starts on unamed node in FDT, so we cannot make 139*ecaecbc7SIan Lepore * ofw_bus_devinfo from it. Pass node to simplebus_init directly. 140*ecaecbc7SIan Lepore */ 141*ecaecbc7SIan Lepore simplebus_init(dev, node); 14265d08437SNathan Whitehorn sc->sc_intr_rman.rm_type = RMAN_ARRAY; 14365d08437SNathan Whitehorn sc->sc_intr_rman.rm_descr = "Interrupts"; 14465d08437SNathan Whitehorn sc->sc_mem_rman.rm_type = RMAN_ARRAY; 14565d08437SNathan Whitehorn sc->sc_mem_rman.rm_descr = "Device Memory"; 14665d08437SNathan Whitehorn if (rman_init(&sc->sc_intr_rman) != 0 || 14765d08437SNathan Whitehorn rman_init(&sc->sc_mem_rman) != 0 || 14865d08437SNathan Whitehorn rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0 || 14965d08437SNathan Whitehorn rman_manage_region(&sc->sc_mem_rman, 0, BUS_SPACE_MAXADDR) != 0) 15065d08437SNathan Whitehorn panic("%s: failed to set up rmans.", __func__); 15165d08437SNathan Whitehorn 15265d08437SNathan Whitehorn /* 15365d08437SNathan Whitehorn * Allow devices to identify. 15465d08437SNathan Whitehorn */ 15565d08437SNathan Whitehorn bus_generic_probe(dev); 15665d08437SNathan Whitehorn 15765d08437SNathan Whitehorn /* 15865d08437SNathan Whitehorn * Now walk the OFW tree and attach top-level devices. 15965d08437SNathan Whitehorn */ 16065d08437SNathan Whitehorn for (node = OF_child(node); node > 0; node = OF_peer(node)) { 161*ecaecbc7SIan Lepore if (ofw_bus_gen_setup_devinfo(&obd, node) != 0) 16265d08437SNathan Whitehorn continue; 163*ecaecbc7SIan Lepore simplebus_add_device(dev, node, 0, NULL, -1, NULL); 16465d08437SNathan Whitehorn } 16565d08437SNathan Whitehorn return (bus_generic_attach(dev)); 16665d08437SNathan Whitehorn } 16765d08437SNathan Whitehorn 16865d08437SNathan Whitehorn static struct resource * 16965d08437SNathan Whitehorn ofwbus_alloc_resource(device_t bus, device_t child, int type, int *rid, 17065d08437SNathan Whitehorn u_long start, u_long end, u_long count, u_int flags) 17165d08437SNathan Whitehorn { 17265d08437SNathan Whitehorn struct ofwbus_softc *sc; 17365d08437SNathan Whitehorn struct rman *rm; 17465d08437SNathan Whitehorn struct resource *rv; 17565d08437SNathan Whitehorn struct resource_list_entry *rle; 17665d08437SNathan Whitehorn int isdefault, passthrough; 17765d08437SNathan Whitehorn 17865d08437SNathan Whitehorn isdefault = (start == 0UL && end == ~0UL); 17965d08437SNathan Whitehorn passthrough = (device_get_parent(child) != bus); 18065d08437SNathan Whitehorn sc = device_get_softc(bus); 18165d08437SNathan Whitehorn rle = NULL; 18265d08437SNathan Whitehorn if (!passthrough && isdefault) { 18365d08437SNathan Whitehorn rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child), 18465d08437SNathan Whitehorn type, *rid); 185*ecaecbc7SIan Lepore if (rle == NULL) { 186*ecaecbc7SIan Lepore if (bootverbose) 187*ecaecbc7SIan Lepore device_printf(bus, "no default resources for " 188*ecaecbc7SIan Lepore "rid = %d, type = %d\n", *rid, type); 18965d08437SNathan Whitehorn return (NULL); 190*ecaecbc7SIan Lepore } 19165d08437SNathan Whitehorn start = rle->start; 19265d08437SNathan Whitehorn count = ulmax(count, rle->count); 19365d08437SNathan Whitehorn end = ulmax(rle->end, start + count - 1); 19465d08437SNathan Whitehorn } 19565d08437SNathan Whitehorn 19665d08437SNathan Whitehorn switch (type) { 19765d08437SNathan Whitehorn case SYS_RES_IRQ: 19865d08437SNathan Whitehorn rm = &sc->sc_intr_rman; 19965d08437SNathan Whitehorn break; 20065d08437SNathan Whitehorn case SYS_RES_MEMORY: 20165d08437SNathan Whitehorn rm = &sc->sc_mem_rman; 20265d08437SNathan Whitehorn break; 20365d08437SNathan Whitehorn default: 20465d08437SNathan Whitehorn return (NULL); 20565d08437SNathan Whitehorn } 20665d08437SNathan Whitehorn 20765d08437SNathan Whitehorn rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 20865d08437SNathan Whitehorn child); 20965d08437SNathan Whitehorn if (rv == NULL) 21065d08437SNathan Whitehorn return (NULL); 21165d08437SNathan Whitehorn rman_set_rid(rv, *rid); 21265d08437SNathan Whitehorn 21365d08437SNathan Whitehorn if ((flags & RF_ACTIVE) != 0 && bus_activate_resource(child, type, 21465d08437SNathan Whitehorn *rid, rv) != 0) { 21565d08437SNathan Whitehorn rman_release_resource(rv); 21665d08437SNathan Whitehorn return (NULL); 21765d08437SNathan Whitehorn } 21865d08437SNathan Whitehorn 21965d08437SNathan Whitehorn if (!passthrough && rle != NULL) { 22065d08437SNathan Whitehorn rle->res = rv; 22165d08437SNathan Whitehorn rle->start = rman_get_start(rv); 22265d08437SNathan Whitehorn rle->end = rman_get_end(rv); 22365d08437SNathan Whitehorn rle->count = rle->end - rle->start + 1; 22465d08437SNathan Whitehorn } 22565d08437SNathan Whitehorn 22665d08437SNathan Whitehorn return (rv); 22765d08437SNathan Whitehorn } 22865d08437SNathan Whitehorn 22965d08437SNathan Whitehorn static int 23065d08437SNathan Whitehorn ofwbus_adjust_resource(device_t bus, device_t child __unused, int type, 23165d08437SNathan Whitehorn struct resource *r, u_long start, u_long end) 23265d08437SNathan Whitehorn { 23365d08437SNathan Whitehorn struct ofwbus_softc *sc; 23465d08437SNathan Whitehorn struct rman *rm; 23565d08437SNathan Whitehorn device_t ofwbus; 23665d08437SNathan Whitehorn 23765d08437SNathan Whitehorn ofwbus = bus; 23865d08437SNathan Whitehorn while (strcmp(device_get_name(device_get_parent(ofwbus)), "root") != 0) 23965d08437SNathan Whitehorn ofwbus = device_get_parent(ofwbus); 24065d08437SNathan Whitehorn sc = device_get_softc(ofwbus); 24165d08437SNathan Whitehorn switch (type) { 24265d08437SNathan Whitehorn case SYS_RES_IRQ: 24365d08437SNathan Whitehorn rm = &sc->sc_intr_rman; 24465d08437SNathan Whitehorn break; 24565d08437SNathan Whitehorn case SYS_RES_MEMORY: 24665d08437SNathan Whitehorn rm = &sc->sc_mem_rman; 24765d08437SNathan Whitehorn break; 24865d08437SNathan Whitehorn default: 24965d08437SNathan Whitehorn return (EINVAL); 25065d08437SNathan Whitehorn } 25165d08437SNathan Whitehorn if (rm == NULL) 25265d08437SNathan Whitehorn return (ENXIO); 25365d08437SNathan Whitehorn if (rman_is_region_manager(r, rm) == 0) 25465d08437SNathan Whitehorn return (EINVAL); 25565d08437SNathan Whitehorn return (rman_adjust_resource(r, start, end)); 25665d08437SNathan Whitehorn } 25765d08437SNathan Whitehorn 25865d08437SNathan Whitehorn static int 25976a8ef26SZbigniew Bodek ofwbus_release_resource(device_t bus, device_t child, int type, 26065d08437SNathan Whitehorn int rid, struct resource *r) 26165d08437SNathan Whitehorn { 26276a8ef26SZbigniew Bodek struct resource_list_entry *rle; 26365d08437SNathan Whitehorn int error; 26465d08437SNathan Whitehorn 26576a8ef26SZbigniew Bodek /* Clean resource list entry */ 26676a8ef26SZbigniew Bodek rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child), type, rid); 26776a8ef26SZbigniew Bodek if (rle != NULL) 26876a8ef26SZbigniew Bodek rle->res = NULL; 26976a8ef26SZbigniew Bodek 27065d08437SNathan Whitehorn if ((rman_get_flags(r) & RF_ACTIVE) != 0) { 27165d08437SNathan Whitehorn error = bus_deactivate_resource(child, type, rid, r); 27265d08437SNathan Whitehorn if (error) 27365d08437SNathan Whitehorn return (error); 27465d08437SNathan Whitehorn } 27565d08437SNathan Whitehorn return (rman_release_resource(r)); 27665d08437SNathan Whitehorn } 277