xref: /freebsd/sys/dev/ofw/ofwbus.c (revision f9bdaab95ec469738fbfc1f0edd3e8c744b7f71f)
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>
46ad5244ecSSvatopluk Kraus #ifdef INTRNG
47ad5244ecSSvatopluk Kraus #include <sys/intr.h>
48ad5244ecSSvatopluk Kraus #endif
4965d08437SNathan Whitehorn 
5065d08437SNathan Whitehorn #include <vm/vm.h>
5165d08437SNathan Whitehorn #include <vm/pmap.h>
5265d08437SNathan Whitehorn 
5365d08437SNathan Whitehorn #include <dev/ofw/ofw_bus.h>
5465d08437SNathan Whitehorn #include <dev/ofw/ofw_bus_subr.h>
5565d08437SNathan Whitehorn #include <dev/ofw/openfirm.h>
56ecaecbc7SIan Lepore #include <dev/fdt/simplebus.h>
5765d08437SNathan Whitehorn 
5865d08437SNathan Whitehorn #include <machine/bus.h>
5965d08437SNathan Whitehorn #include <machine/resource.h>
6065d08437SNathan Whitehorn 
6165d08437SNathan Whitehorn /*
6265d08437SNathan Whitehorn  * The ofwbus (which is a pseudo-bus actually) iterates over the nodes that
6365d08437SNathan Whitehorn  * hang from the Open Firmware root node and adds them as devices to this bus
6465d08437SNathan Whitehorn  * (except some special nodes which are excluded) so that drivers can be
6565d08437SNathan Whitehorn  * attached to them.
6665d08437SNathan Whitehorn  */
6765d08437SNathan Whitehorn 
682c0d026bSAndrew Turner #ifndef __aarch64__
6965d08437SNathan Whitehorn static device_identify_t ofwbus_identify;
702c0d026bSAndrew Turner #endif
7165d08437SNathan Whitehorn static device_probe_t ofwbus_probe;
7265d08437SNathan Whitehorn static device_attach_t ofwbus_attach;
7365d08437SNathan Whitehorn static bus_alloc_resource_t ofwbus_alloc_resource;
7465d08437SNathan Whitehorn static bus_release_resource_t ofwbus_release_resource;
7565d08437SNathan Whitehorn 
7665d08437SNathan Whitehorn static device_method_t ofwbus_methods[] = {
7765d08437SNathan Whitehorn 	/* Device interface */
782c0d026bSAndrew Turner #ifndef __aarch64__
7965d08437SNathan Whitehorn 	DEVMETHOD(device_identify,	ofwbus_identify),
802c0d026bSAndrew Turner #endif
8165d08437SNathan Whitehorn 	DEVMETHOD(device_probe,		ofwbus_probe),
8265d08437SNathan Whitehorn 	DEVMETHOD(device_attach,	ofwbus_attach),
8365d08437SNathan Whitehorn 
8465d08437SNathan Whitehorn 	/* Bus interface */
8565d08437SNathan Whitehorn 	DEVMETHOD(bus_alloc_resource,	ofwbus_alloc_resource),
86*f9bdaab9SElliott Mitchell 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
8765d08437SNathan Whitehorn 	DEVMETHOD(bus_release_resource,	ofwbus_release_resource),
8865d08437SNathan Whitehorn 
8965d08437SNathan Whitehorn 	DEVMETHOD_END
9065d08437SNathan Whitehorn };
9165d08437SNathan Whitehorn 
92ecaecbc7SIan Lepore DEFINE_CLASS_1(ofwbus, ofwbus_driver, ofwbus_methods,
93*f9bdaab9SElliott Mitchell     sizeof(struct simplebus_softc), simplebus_driver);
9403f6459cSJohn Baldwin EARLY_DRIVER_MODULE(ofwbus, nexus, ofwbus_driver, 0, 0,
95633dbf2eSIan Lepore     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
9665d08437SNathan Whitehorn MODULE_VERSION(ofwbus, 1);
9765d08437SNathan Whitehorn 
982c0d026bSAndrew Turner #ifndef __aarch64__
9965d08437SNathan Whitehorn static void
10065d08437SNathan Whitehorn ofwbus_identify(driver_t *driver, device_t parent)
10165d08437SNathan Whitehorn {
10265d08437SNathan Whitehorn 
10365d08437SNathan Whitehorn 	/* Check if Open Firmware has been instantiated */
104e2fc1af4SNathan Whitehorn 	if (OF_peer(0) == 0)
10565d08437SNathan Whitehorn 		return;
10665d08437SNathan Whitehorn 
10765d08437SNathan Whitehorn 	if (device_find_child(parent, "ofwbus", -1) == NULL)
10865d08437SNathan Whitehorn 		BUS_ADD_CHILD(parent, 0, "ofwbus", -1);
10965d08437SNathan Whitehorn }
1102c0d026bSAndrew Turner #endif
11165d08437SNathan Whitehorn 
11265d08437SNathan Whitehorn static int
11365d08437SNathan Whitehorn ofwbus_probe(device_t dev)
11465d08437SNathan Whitehorn {
11565d08437SNathan Whitehorn 
1162c0d026bSAndrew Turner #ifdef __aarch64__
1172c0d026bSAndrew Turner 	if (OF_peer(0) == 0)
1182c0d026bSAndrew Turner 		return (ENXIO);
1192c0d026bSAndrew Turner #endif
1202c0d026bSAndrew Turner 
12165d08437SNathan Whitehorn 	device_set_desc(dev, "Open Firmware Device Tree");
12265d08437SNathan Whitehorn 	return (BUS_PROBE_NOWILDCARD);
12365d08437SNathan Whitehorn }
12465d08437SNathan Whitehorn 
12565d08437SNathan Whitehorn static int
12665d08437SNathan Whitehorn ofwbus_attach(device_t dev)
12765d08437SNathan Whitehorn {
12865d08437SNathan Whitehorn 	phandle_t node;
129ecaecbc7SIan Lepore 	struct ofw_bus_devinfo obd;
13065d08437SNathan Whitehorn 
13165d08437SNathan Whitehorn 	node = OF_peer(0);
13265d08437SNathan Whitehorn 
13365d08437SNathan Whitehorn 	/*
13465d08437SNathan Whitehorn 	 * If no Open Firmware, bail early
13565d08437SNathan Whitehorn 	 */
13665d08437SNathan Whitehorn 	if (node == -1)
13765d08437SNathan Whitehorn 		return (ENXIO);
13865d08437SNathan Whitehorn 
139ecaecbc7SIan Lepore 	/*
140ecaecbc7SIan Lepore 	 * ofwbus bus starts on unamed node in FDT, so we cannot make
141ecaecbc7SIan Lepore 	 * ofw_bus_devinfo from it. Pass node to simplebus_init directly.
142ecaecbc7SIan Lepore 	 */
143ecaecbc7SIan Lepore 	simplebus_init(dev, node);
14465d08437SNathan Whitehorn 
14565d08437SNathan Whitehorn 	/*
14665d08437SNathan Whitehorn 	 * Allow devices to identify.
14765d08437SNathan Whitehorn 	 */
14865d08437SNathan Whitehorn 	bus_generic_probe(dev);
14965d08437SNathan Whitehorn 
15065d08437SNathan Whitehorn 	/*
15165d08437SNathan Whitehorn 	 * Now walk the OFW tree and attach top-level devices.
15265d08437SNathan Whitehorn 	 */
15365d08437SNathan Whitehorn 	for (node = OF_child(node); node > 0; node = OF_peer(node)) {
154ecaecbc7SIan Lepore 		if (ofw_bus_gen_setup_devinfo(&obd, node) != 0)
15565d08437SNathan Whitehorn 			continue;
156ecaecbc7SIan Lepore 		simplebus_add_device(dev, node, 0, NULL, -1, NULL);
15765d08437SNathan Whitehorn 	}
15865d08437SNathan Whitehorn 	return (bus_generic_attach(dev));
15965d08437SNathan Whitehorn }
16065d08437SNathan Whitehorn 
16165d08437SNathan Whitehorn static struct resource *
16265d08437SNathan Whitehorn ofwbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
1632dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
16465d08437SNathan Whitehorn {
16565d08437SNathan Whitehorn 	struct resource *rv;
16665d08437SNathan Whitehorn 	struct resource_list_entry *rle;
167*f9bdaab9SElliott Mitchell 	bool isdefault, passthrough;
16865d08437SNathan Whitehorn 
1697915adb5SJustin Hibbits 	isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
17065d08437SNathan Whitehorn 	passthrough = (device_get_parent(child) != bus);
17165d08437SNathan Whitehorn 	rle = NULL;
17265d08437SNathan Whitehorn 	if (!passthrough && isdefault) {
17365d08437SNathan Whitehorn 		rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child),
17465d08437SNathan Whitehorn 		    type, *rid);
175ecaecbc7SIan Lepore 		if (rle == NULL) {
176ecaecbc7SIan Lepore 			if (bootverbose)
177ecaecbc7SIan Lepore 				device_printf(bus, "no default resources for "
178ecaecbc7SIan Lepore 				    "rid = %d, type = %d\n", *rid, type);
17965d08437SNathan Whitehorn 			return (NULL);
180ecaecbc7SIan Lepore 		}
18165d08437SNathan Whitehorn 		start = rle->start;
182da1b038aSJustin Hibbits 		count = ummax(count, rle->count);
183da1b038aSJustin Hibbits 		end = ummax(rle->end, start + count - 1);
18465d08437SNathan Whitehorn 	}
18565d08437SNathan Whitehorn 
186*f9bdaab9SElliott Mitchell 	/* Let nexus handle the allocation. */
187*f9bdaab9SElliott Mitchell 	rv = bus_generic_alloc_resource(bus, child, type, rid, start, end,
188*f9bdaab9SElliott Mitchell 	    count, flags);
18965d08437SNathan Whitehorn 	if (rv == NULL)
19065d08437SNathan Whitehorn 		return (NULL);
19165d08437SNathan Whitehorn 
19265d08437SNathan Whitehorn 	if (!passthrough && rle != NULL) {
19365d08437SNathan Whitehorn 		rle->res = rv;
19465d08437SNathan Whitehorn 		rle->start = rman_get_start(rv);
19565d08437SNathan Whitehorn 		rle->end = rman_get_end(rv);
19665d08437SNathan Whitehorn 		rle->count = rle->end - rle->start + 1;
19765d08437SNathan Whitehorn 	}
19865d08437SNathan Whitehorn 
19965d08437SNathan Whitehorn 	return (rv);
20065d08437SNathan Whitehorn }
20165d08437SNathan Whitehorn 
20265d08437SNathan Whitehorn static int
20376a8ef26SZbigniew Bodek ofwbus_release_resource(device_t bus, device_t child, int type,
20465d08437SNathan Whitehorn     int rid, struct resource *r)
20565d08437SNathan Whitehorn {
20676a8ef26SZbigniew Bodek 	struct resource_list_entry *rle;
207*f9bdaab9SElliott Mitchell 	bool passthrough;
20865d08437SNathan Whitehorn 
209e2d4f32fSZbigniew Bodek 	passthrough = (device_get_parent(child) != bus);
210e2d4f32fSZbigniew Bodek 	if (!passthrough) {
21176a8ef26SZbigniew Bodek 		/* Clean resource list entry */
212e2d4f32fSZbigniew Bodek 		rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child),
213e2d4f32fSZbigniew Bodek 		    type, rid);
21476a8ef26SZbigniew Bodek 		if (rle != NULL)
21576a8ef26SZbigniew Bodek 			rle->res = NULL;
216e2d4f32fSZbigniew Bodek 	}
21776a8ef26SZbigniew Bodek 
218*f9bdaab9SElliott Mitchell 	/* Let nexus handle the release. */
219*f9bdaab9SElliott Mitchell 	return (bus_generic_release_resource(bus, child, type, rid, r));
22065d08437SNathan Whitehorn }
221