xref: /freebsd/sys/dev/ofw/ofwbus.c (revision 2dd1bdf1834c53d048d3d9a7079b45afea5cecd7)
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>
53ecaecbc7SIan 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 {
67ecaecbc7SIan Lepore 	struct simplebus_softc simplebus_sc;
6865d08437SNathan Whitehorn 	struct rman	sc_intr_rman;
6965d08437SNathan Whitehorn 	struct rman	sc_mem_rman;
7065d08437SNathan Whitehorn };
7165d08437SNathan Whitehorn 
722c0d026bSAndrew Turner #ifndef __aarch64__
7365d08437SNathan Whitehorn static device_identify_t ofwbus_identify;
742c0d026bSAndrew Turner #endif
7565d08437SNathan Whitehorn static device_probe_t ofwbus_probe;
7665d08437SNathan Whitehorn static device_attach_t ofwbus_attach;
7765d08437SNathan Whitehorn static bus_alloc_resource_t ofwbus_alloc_resource;
7865d08437SNathan Whitehorn static bus_adjust_resource_t ofwbus_adjust_resource;
7965d08437SNathan Whitehorn static bus_release_resource_t ofwbus_release_resource;
8065d08437SNathan Whitehorn 
8165d08437SNathan Whitehorn static device_method_t ofwbus_methods[] = {
8265d08437SNathan Whitehorn 	/* Device interface */
832c0d026bSAndrew Turner #ifndef __aarch64__
8465d08437SNathan Whitehorn 	DEVMETHOD(device_identify,	ofwbus_identify),
852c0d026bSAndrew Turner #endif
8665d08437SNathan Whitehorn 	DEVMETHOD(device_probe,		ofwbus_probe),
8765d08437SNathan Whitehorn 	DEVMETHOD(device_attach,	ofwbus_attach),
8865d08437SNathan Whitehorn 
8965d08437SNathan Whitehorn 	/* Bus interface */
9065d08437SNathan Whitehorn 	DEVMETHOD(bus_alloc_resource,	ofwbus_alloc_resource),
9165d08437SNathan Whitehorn 	DEVMETHOD(bus_adjust_resource,	ofwbus_adjust_resource),
9265d08437SNathan Whitehorn 	DEVMETHOD(bus_release_resource,	ofwbus_release_resource),
9365d08437SNathan Whitehorn 
9465d08437SNathan Whitehorn 	DEVMETHOD_END
9565d08437SNathan Whitehorn };
9665d08437SNathan Whitehorn 
97ecaecbc7SIan Lepore DEFINE_CLASS_1(ofwbus, ofwbus_driver, ofwbus_methods,
98ecaecbc7SIan Lepore     sizeof(struct ofwbus_softc), simplebus_driver);
9965d08437SNathan Whitehorn static devclass_t ofwbus_devclass;
1002d12d35cSIan Lepore EARLY_DRIVER_MODULE(ofwbus, nexus, ofwbus_driver, ofwbus_devclass, 0, 0,
101633dbf2eSIan Lepore     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
10265d08437SNathan Whitehorn MODULE_VERSION(ofwbus, 1);
10365d08437SNathan Whitehorn 
1042c0d026bSAndrew Turner #ifndef __aarch64__
10565d08437SNathan Whitehorn static void
10665d08437SNathan Whitehorn ofwbus_identify(driver_t *driver, device_t parent)
10765d08437SNathan Whitehorn {
10865d08437SNathan Whitehorn 
10965d08437SNathan Whitehorn 	/* Check if Open Firmware has been instantiated */
110e2fc1af4SNathan Whitehorn 	if (OF_peer(0) == 0)
11165d08437SNathan Whitehorn 		return;
11265d08437SNathan Whitehorn 
11365d08437SNathan Whitehorn 	if (device_find_child(parent, "ofwbus", -1) == NULL)
11465d08437SNathan Whitehorn 		BUS_ADD_CHILD(parent, 0, "ofwbus", -1);
11565d08437SNathan Whitehorn }
1162c0d026bSAndrew Turner #endif
11765d08437SNathan Whitehorn 
11865d08437SNathan Whitehorn static int
11965d08437SNathan Whitehorn ofwbus_probe(device_t dev)
12065d08437SNathan Whitehorn {
12165d08437SNathan Whitehorn 
1222c0d026bSAndrew Turner #ifdef __aarch64__
1232c0d026bSAndrew Turner 	if (OF_peer(0) == 0)
1242c0d026bSAndrew Turner 		return (ENXIO);
1252c0d026bSAndrew Turner #endif
1262c0d026bSAndrew Turner 
12765d08437SNathan Whitehorn 	device_set_desc(dev, "Open Firmware Device Tree");
12865d08437SNathan Whitehorn 	return (BUS_PROBE_NOWILDCARD);
12965d08437SNathan Whitehorn }
13065d08437SNathan Whitehorn 
13165d08437SNathan Whitehorn static int
13265d08437SNathan Whitehorn ofwbus_attach(device_t dev)
13365d08437SNathan Whitehorn {
13465d08437SNathan Whitehorn 	struct ofwbus_softc *sc;
13565d08437SNathan Whitehorn 	phandle_t node;
136ecaecbc7SIan Lepore 	struct ofw_bus_devinfo obd;
13765d08437SNathan Whitehorn 
13865d08437SNathan Whitehorn 	sc = device_get_softc(dev);
13965d08437SNathan Whitehorn 
14065d08437SNathan Whitehorn 	node = OF_peer(0);
14165d08437SNathan Whitehorn 
14265d08437SNathan Whitehorn 	/*
14365d08437SNathan Whitehorn 	 * If no Open Firmware, bail early
14465d08437SNathan Whitehorn 	 */
14565d08437SNathan Whitehorn 	if (node == -1)
14665d08437SNathan Whitehorn 		return (ENXIO);
14765d08437SNathan Whitehorn 
148ecaecbc7SIan Lepore 	/*
149ecaecbc7SIan Lepore 	 * ofwbus bus starts on unamed node in FDT, so we cannot make
150ecaecbc7SIan Lepore 	 * ofw_bus_devinfo from it. Pass node to simplebus_init directly.
151ecaecbc7SIan Lepore 	 */
152ecaecbc7SIan Lepore 	simplebus_init(dev, node);
15365d08437SNathan Whitehorn 	sc->sc_intr_rman.rm_type = RMAN_ARRAY;
15465d08437SNathan Whitehorn 	sc->sc_intr_rman.rm_descr = "Interrupts";
15565d08437SNathan Whitehorn 	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
15665d08437SNathan Whitehorn 	sc->sc_mem_rman.rm_descr = "Device Memory";
15765d08437SNathan Whitehorn 	if (rman_init(&sc->sc_intr_rman) != 0 ||
15865d08437SNathan Whitehorn 	    rman_init(&sc->sc_mem_rman) != 0 ||
15965d08437SNathan Whitehorn 	    rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0 ||
16065d08437SNathan Whitehorn 	    rman_manage_region(&sc->sc_mem_rman, 0, BUS_SPACE_MAXADDR) != 0)
16165d08437SNathan Whitehorn 		panic("%s: failed to set up rmans.", __func__);
16265d08437SNathan Whitehorn 
16365d08437SNathan Whitehorn 	/*
16465d08437SNathan Whitehorn 	 * Allow devices to identify.
16565d08437SNathan Whitehorn 	 */
16665d08437SNathan Whitehorn 	bus_generic_probe(dev);
16765d08437SNathan Whitehorn 
16865d08437SNathan Whitehorn 	/*
16965d08437SNathan Whitehorn 	 * Now walk the OFW tree and attach top-level devices.
17065d08437SNathan Whitehorn 	 */
17165d08437SNathan Whitehorn 	for (node = OF_child(node); node > 0; node = OF_peer(node)) {
172ecaecbc7SIan Lepore 		if (ofw_bus_gen_setup_devinfo(&obd, node) != 0)
17365d08437SNathan Whitehorn 			continue;
174ecaecbc7SIan Lepore 		simplebus_add_device(dev, node, 0, NULL, -1, NULL);
17565d08437SNathan Whitehorn 	}
17665d08437SNathan Whitehorn 	return (bus_generic_attach(dev));
17765d08437SNathan Whitehorn }
17865d08437SNathan Whitehorn 
17965d08437SNathan Whitehorn static struct resource *
18065d08437SNathan Whitehorn ofwbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
181*2dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
18265d08437SNathan Whitehorn {
18365d08437SNathan Whitehorn 	struct ofwbus_softc *sc;
18465d08437SNathan Whitehorn 	struct rman *rm;
18565d08437SNathan Whitehorn 	struct resource *rv;
18665d08437SNathan Whitehorn 	struct resource_list_entry *rle;
18765d08437SNathan Whitehorn 	int isdefault, passthrough;
18865d08437SNathan Whitehorn 
18965d08437SNathan Whitehorn 	isdefault = (start == 0UL && end == ~0UL);
19065d08437SNathan Whitehorn 	passthrough = (device_get_parent(child) != bus);
19165d08437SNathan Whitehorn 	sc = device_get_softc(bus);
19265d08437SNathan Whitehorn 	rle = NULL;
19365d08437SNathan Whitehorn 	if (!passthrough && isdefault) {
19465d08437SNathan Whitehorn 		rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child),
19565d08437SNathan Whitehorn 		    type, *rid);
196ecaecbc7SIan Lepore 		if (rle == NULL) {
197ecaecbc7SIan Lepore 			if (bootverbose)
198ecaecbc7SIan Lepore 				device_printf(bus, "no default resources for "
199ecaecbc7SIan Lepore 				    "rid = %d, type = %d\n", *rid, type);
20065d08437SNathan Whitehorn 			return (NULL);
201ecaecbc7SIan Lepore 		}
20265d08437SNathan Whitehorn 		start = rle->start;
20365d08437SNathan Whitehorn 		count = ulmax(count, rle->count);
20465d08437SNathan Whitehorn 		end = ulmax(rle->end, start + count - 1);
20565d08437SNathan Whitehorn 	}
20665d08437SNathan Whitehorn 
20765d08437SNathan Whitehorn 	switch (type) {
20865d08437SNathan Whitehorn 	case SYS_RES_IRQ:
20965d08437SNathan Whitehorn 		rm = &sc->sc_intr_rman;
21065d08437SNathan Whitehorn 		break;
21165d08437SNathan Whitehorn 	case SYS_RES_MEMORY:
21265d08437SNathan Whitehorn 		rm = &sc->sc_mem_rman;
21365d08437SNathan Whitehorn 		break;
21465d08437SNathan Whitehorn 	default:
21565d08437SNathan Whitehorn 		return (NULL);
21665d08437SNathan Whitehorn 	}
21765d08437SNathan Whitehorn 
21865d08437SNathan Whitehorn 	rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
21965d08437SNathan Whitehorn 	    child);
22065d08437SNathan Whitehorn 	if (rv == NULL)
22165d08437SNathan Whitehorn 		return (NULL);
22265d08437SNathan Whitehorn 	rman_set_rid(rv, *rid);
22365d08437SNathan Whitehorn 
22465d08437SNathan Whitehorn 	if ((flags & RF_ACTIVE) != 0 && bus_activate_resource(child, type,
22565d08437SNathan Whitehorn 	    *rid, rv) != 0) {
22665d08437SNathan Whitehorn 		rman_release_resource(rv);
22765d08437SNathan Whitehorn 		return (NULL);
22865d08437SNathan Whitehorn 	}
22965d08437SNathan Whitehorn 
23065d08437SNathan Whitehorn 	if (!passthrough && rle != NULL) {
23165d08437SNathan Whitehorn 		rle->res = rv;
23265d08437SNathan Whitehorn 		rle->start = rman_get_start(rv);
23365d08437SNathan Whitehorn 		rle->end = rman_get_end(rv);
23465d08437SNathan Whitehorn 		rle->count = rle->end - rle->start + 1;
23565d08437SNathan Whitehorn 	}
23665d08437SNathan Whitehorn 
23765d08437SNathan Whitehorn 	return (rv);
23865d08437SNathan Whitehorn }
23965d08437SNathan Whitehorn 
24065d08437SNathan Whitehorn static int
24165d08437SNathan Whitehorn ofwbus_adjust_resource(device_t bus, device_t child __unused, int type,
242*2dd1bdf1SJustin Hibbits     struct resource *r, rman_res_t start, rman_res_t end)
24365d08437SNathan Whitehorn {
24465d08437SNathan Whitehorn 	struct ofwbus_softc *sc;
24565d08437SNathan Whitehorn 	struct rman *rm;
24665d08437SNathan Whitehorn 	device_t ofwbus;
24765d08437SNathan Whitehorn 
24865d08437SNathan Whitehorn 	ofwbus = bus;
24965d08437SNathan Whitehorn 	while (strcmp(device_get_name(device_get_parent(ofwbus)), "root") != 0)
25065d08437SNathan Whitehorn 		ofwbus = device_get_parent(ofwbus);
25165d08437SNathan Whitehorn 	sc = device_get_softc(ofwbus);
25265d08437SNathan Whitehorn 	switch (type) {
25365d08437SNathan Whitehorn 	case SYS_RES_IRQ:
25465d08437SNathan Whitehorn 		rm = &sc->sc_intr_rman;
25565d08437SNathan Whitehorn 		break;
25665d08437SNathan Whitehorn 	case SYS_RES_MEMORY:
25765d08437SNathan Whitehorn 		rm = &sc->sc_mem_rman;
25865d08437SNathan Whitehorn 		break;
25965d08437SNathan Whitehorn 	default:
26065d08437SNathan Whitehorn 		return (EINVAL);
26165d08437SNathan Whitehorn 	}
26265d08437SNathan Whitehorn 	if (rm == NULL)
26365d08437SNathan Whitehorn 		return (ENXIO);
26465d08437SNathan Whitehorn 	if (rman_is_region_manager(r, rm) == 0)
26565d08437SNathan Whitehorn 		return (EINVAL);
26665d08437SNathan Whitehorn 	return (rman_adjust_resource(r, start, end));
26765d08437SNathan Whitehorn }
26865d08437SNathan Whitehorn 
26965d08437SNathan Whitehorn static int
27076a8ef26SZbigniew Bodek ofwbus_release_resource(device_t bus, device_t child, int type,
27165d08437SNathan Whitehorn     int rid, struct resource *r)
27265d08437SNathan Whitehorn {
27376a8ef26SZbigniew Bodek 	struct resource_list_entry *rle;
27465d08437SNathan Whitehorn 	int error;
27565d08437SNathan Whitehorn 
27676a8ef26SZbigniew Bodek 	/* Clean resource list entry */
27776a8ef26SZbigniew Bodek 	rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child), type, rid);
27876a8ef26SZbigniew Bodek 	if (rle != NULL)
27976a8ef26SZbigniew Bodek 		rle->res = NULL;
28076a8ef26SZbigniew Bodek 
28165d08437SNathan Whitehorn 	if ((rman_get_flags(r) & RF_ACTIVE) != 0) {
28265d08437SNathan Whitehorn 		error = bus_deactivate_resource(child, type, rid, r);
28365d08437SNathan Whitehorn 		if (error)
28465d08437SNathan Whitehorn 			return (error);
28565d08437SNathan Whitehorn 	}
28665d08437SNathan Whitehorn 	return (rman_release_resource(r));
28765d08437SNathan Whitehorn }
288