xref: /freebsd/sys/dev/ofw/ofwbus.c (revision 788ca347b816afd83b2885e0c79aeeb88649b2ab)
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>.
4  * Copyright 2006 by Marius Strobl <marius@FreeBSD.org>.
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and
8  * its documentation for any purpose and without fee is hereby
9  * granted, provided that both the above copyright notice and this
10  * permission notice appear in all copies, that both the above
11  * copyright notice and this permission notice appear in all
12  * supporting documentation, and that the name of M.I.T. not be used
13  * in advertising or publicity pertaining to distribution of the
14  * software without specific, written prior permission.  M.I.T. makes
15  * no representations about the suitability of this software for any
16  * purpose.  It is provided "as is" without express or implied
17  * warranty.
18  *
19  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
20  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
23  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * 	from: FreeBSD: src/sys/i386/i386/nexus.c,v 1.43 2001/02/09
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/pcpu.h>
45 #include <sys/rman.h>
46 
47 #include <vm/vm.h>
48 #include <vm/pmap.h>
49 
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/ofw/openfirm.h>
53 #include <dev/fdt/simplebus.h>
54 
55 #include <machine/bus.h>
56 #include <machine/resource.h>
57 
58 /*
59  * The ofwbus (which is a pseudo-bus actually) iterates over the nodes that
60  * hang from the Open Firmware root node and adds them as devices to this bus
61  * (except some special nodes which are excluded) so that drivers can be
62  * attached to them.
63  *
64  */
65 
66 struct ofwbus_softc {
67 	struct simplebus_softc simplebus_sc;
68 	struct rman	sc_intr_rman;
69 	struct rman	sc_mem_rman;
70 };
71 
72 static device_identify_t ofwbus_identify;
73 static device_probe_t ofwbus_probe;
74 static device_attach_t ofwbus_attach;
75 static bus_alloc_resource_t ofwbus_alloc_resource;
76 static bus_adjust_resource_t ofwbus_adjust_resource;
77 static bus_release_resource_t ofwbus_release_resource;
78 
79 static device_method_t ofwbus_methods[] = {
80 	/* Device interface */
81 	DEVMETHOD(device_identify,	ofwbus_identify),
82 	DEVMETHOD(device_probe,		ofwbus_probe),
83 	DEVMETHOD(device_attach,	ofwbus_attach),
84 
85 	/* Bus interface */
86 	DEVMETHOD(bus_alloc_resource,	ofwbus_alloc_resource),
87 	DEVMETHOD(bus_adjust_resource,	ofwbus_adjust_resource),
88 	DEVMETHOD(bus_release_resource,	ofwbus_release_resource),
89 
90 	DEVMETHOD_END
91 };
92 
93 DEFINE_CLASS_1(ofwbus, ofwbus_driver, ofwbus_methods,
94     sizeof(struct ofwbus_softc), simplebus_driver);
95 static devclass_t ofwbus_devclass;
96 EARLY_DRIVER_MODULE(ofwbus, nexus, ofwbus_driver, ofwbus_devclass, 0, 0,
97     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
98 MODULE_VERSION(ofwbus, 1);
99 
100 static void
101 ofwbus_identify(driver_t *driver, device_t parent)
102 {
103 
104 	/* Check if Open Firmware has been instantiated */
105 	if (OF_peer(0) == 0)
106 		return;
107 
108 	if (device_find_child(parent, "ofwbus", -1) == NULL)
109 		BUS_ADD_CHILD(parent, 0, "ofwbus", -1);
110 }
111 
112 static int
113 ofwbus_probe(device_t dev)
114 {
115 
116 	device_set_desc(dev, "Open Firmware Device Tree");
117 	return (BUS_PROBE_NOWILDCARD);
118 }
119 
120 static int
121 ofwbus_attach(device_t dev)
122 {
123 	struct ofwbus_softc *sc;
124 	phandle_t node;
125 	struct ofw_bus_devinfo obd;
126 
127 	sc = device_get_softc(dev);
128 
129 	node = OF_peer(0);
130 
131 	/*
132 	 * If no Open Firmware, bail early
133 	 */
134 	if (node == -1)
135 		return (ENXIO);
136 
137 	/*
138 	 * ofwbus bus starts on unamed node in FDT, so we cannot make
139 	 * ofw_bus_devinfo from it. Pass node to simplebus_init directly.
140 	 */
141 	simplebus_init(dev, node);
142 	sc->sc_intr_rman.rm_type = RMAN_ARRAY;
143 	sc->sc_intr_rman.rm_descr = "Interrupts";
144 	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
145 	sc->sc_mem_rman.rm_descr = "Device Memory";
146 	if (rman_init(&sc->sc_intr_rman) != 0 ||
147 	    rman_init(&sc->sc_mem_rman) != 0 ||
148 	    rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0 ||
149 	    rman_manage_region(&sc->sc_mem_rman, 0, BUS_SPACE_MAXADDR) != 0)
150 		panic("%s: failed to set up rmans.", __func__);
151 
152 	/*
153 	 * Allow devices to identify.
154 	 */
155 	bus_generic_probe(dev);
156 
157 	/*
158 	 * Now walk the OFW tree and attach top-level devices.
159 	 */
160 	for (node = OF_child(node); node > 0; node = OF_peer(node)) {
161 		if (ofw_bus_gen_setup_devinfo(&obd, node) != 0)
162 			continue;
163 		simplebus_add_device(dev, node, 0, NULL, -1, NULL);
164 	}
165 	return (bus_generic_attach(dev));
166 }
167 
168 static struct resource *
169 ofwbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
170     u_long start, u_long end, u_long count, u_int flags)
171 {
172 	struct ofwbus_softc *sc;
173 	struct rman *rm;
174 	struct resource *rv;
175 	struct resource_list_entry *rle;
176 	int isdefault, passthrough;
177 
178 	isdefault = (start == 0UL && end == ~0UL);
179 	passthrough = (device_get_parent(child) != bus);
180 	sc = device_get_softc(bus);
181 	rle = NULL;
182 	if (!passthrough && isdefault) {
183 		rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child),
184 		    type, *rid);
185 		if (rle == NULL) {
186 			if (bootverbose)
187 				device_printf(bus, "no default resources for "
188 				    "rid = %d, type = %d\n", *rid, type);
189 			return (NULL);
190 		}
191 		start = rle->start;
192 		count = ulmax(count, rle->count);
193 		end = ulmax(rle->end, start + count - 1);
194 	}
195 
196 	switch (type) {
197 	case SYS_RES_IRQ:
198 		rm = &sc->sc_intr_rman;
199 		break;
200 	case SYS_RES_MEMORY:
201 		rm = &sc->sc_mem_rman;
202 		break;
203 	default:
204 		return (NULL);
205 	}
206 
207 	rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
208 	    child);
209 	if (rv == NULL)
210 		return (NULL);
211 	rman_set_rid(rv, *rid);
212 
213 	if ((flags & RF_ACTIVE) != 0 && bus_activate_resource(child, type,
214 	    *rid, rv) != 0) {
215 		rman_release_resource(rv);
216 		return (NULL);
217 	}
218 
219 	if (!passthrough && rle != NULL) {
220 		rle->res = rv;
221 		rle->start = rman_get_start(rv);
222 		rle->end = rman_get_end(rv);
223 		rle->count = rle->end - rle->start + 1;
224 	}
225 
226 	return (rv);
227 }
228 
229 static int
230 ofwbus_adjust_resource(device_t bus, device_t child __unused, int type,
231     struct resource *r, u_long start, u_long end)
232 {
233 	struct ofwbus_softc *sc;
234 	struct rman *rm;
235 	device_t ofwbus;
236 
237 	ofwbus = bus;
238 	while (strcmp(device_get_name(device_get_parent(ofwbus)), "root") != 0)
239 		ofwbus = device_get_parent(ofwbus);
240 	sc = device_get_softc(ofwbus);
241 	switch (type) {
242 	case SYS_RES_IRQ:
243 		rm = &sc->sc_intr_rman;
244 		break;
245 	case SYS_RES_MEMORY:
246 		rm = &sc->sc_mem_rman;
247 		break;
248 	default:
249 		return (EINVAL);
250 	}
251 	if (rm == NULL)
252 		return (ENXIO);
253 	if (rman_is_region_manager(r, rm) == 0)
254 		return (EINVAL);
255 	return (rman_adjust_resource(r, start, end));
256 }
257 
258 static int
259 ofwbus_release_resource(device_t bus, device_t child, int type,
260     int rid, struct resource *r)
261 {
262 	struct resource_list_entry *rle;
263 	int error;
264 
265 	/* Clean resource list entry */
266 	rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child), type, rid);
267 	if (rle != NULL)
268 		rle->res = NULL;
269 
270 	if ((rman_get_flags(r) & RF_ACTIVE) != 0) {
271 		error = bus_deactivate_resource(child, type, rid, r);
272 		if (error)
273 			return (error);
274 	}
275 	return (rman_release_resource(r));
276 }
277