nexus.c revision 200948
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: head/sys/sparc64/sparc64/nexus.c 200948 2009-12-24 15:43:37Z marius $");
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
45#include <dev/ofw/ofw_bus.h>
46#include <dev/ofw/ofw_bus_subr.h>
47#include <dev/ofw/openfirm.h>
48
49#include <machine/bus.h>
50#include <machine/bus_common.h>
51#include <machine/intr_machdep.h>
52#include <machine/ofw_nexus.h>
53#include <machine/resource.h>
54#include <machine/ver.h>
55
56#include <sys/rman.h>
57
58/*
59 * The nexus (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 * Additionally, interrupt setup/teardown and some resource management are
65 * done at this level.
66 *
67 * Maybe this code should get into dev/ofw to some extent, as some of it should
68 * work for all Open Firmware based machines...
69 */
70
71struct nexus_devinfo {
72	struct ofw_bus_devinfo	ndi_obdinfo;
73	struct resource_list	ndi_rl;
74};
75
76struct nexus_softc {
77	struct rman	sc_intr_rman;
78	struct rman	sc_mem_rman;
79};
80
81static device_probe_t nexus_probe;
82static device_attach_t nexus_attach;
83static bus_print_child_t nexus_print_child;
84static bus_add_child_t nexus_add_child;
85static bus_probe_nomatch_t nexus_probe_nomatch;
86static bus_setup_intr_t nexus_setup_intr;
87static bus_teardown_intr_t nexus_teardown_intr;
88static bus_alloc_resource_t nexus_alloc_resource;
89static bus_activate_resource_t nexus_activate_resource;
90static bus_deactivate_resource_t nexus_deactivate_resource;
91static bus_release_resource_t nexus_release_resource;
92static bus_get_resource_list_t nexus_get_resource_list;
93#ifdef SMP
94static bus_bind_intr_t nexus_bind_intr;
95#endif
96static bus_describe_intr_t nexus_describe_intr;
97static bus_get_dma_tag_t nexus_get_dma_tag;
98static ofw_bus_get_devinfo_t nexus_get_devinfo;
99
100static int nexus_inlist(const char *, const char *const *);
101static struct nexus_devinfo * nexus_setup_dinfo(device_t, phandle_t);
102static void nexus_destroy_dinfo(struct nexus_devinfo *);
103static int nexus_print_res(struct nexus_devinfo *);
104
105static device_method_t nexus_methods[] = {
106	/* Device interface */
107	DEVMETHOD(device_probe,		nexus_probe),
108	DEVMETHOD(device_attach,	nexus_attach),
109	DEVMETHOD(device_detach,	bus_generic_detach),
110	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
111	DEVMETHOD(device_suspend,	bus_generic_suspend),
112	DEVMETHOD(device_resume,	bus_generic_resume),
113
114	/* Bus interface */
115	DEVMETHOD(bus_print_child,	nexus_print_child),
116	DEVMETHOD(bus_probe_nomatch,	nexus_probe_nomatch),
117	DEVMETHOD(bus_read_ivar,	bus_generic_read_ivar),
118	DEVMETHOD(bus_write_ivar,	bus_generic_write_ivar),
119	DEVMETHOD(bus_add_child,	nexus_add_child),
120	DEVMETHOD(bus_alloc_resource,	nexus_alloc_resource),
121	DEVMETHOD(bus_activate_resource,	nexus_activate_resource),
122	DEVMETHOD(bus_deactivate_resource,	nexus_deactivate_resource),
123	DEVMETHOD(bus_release_resource,	nexus_release_resource),
124	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
125	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
126	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
127	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
128	DEVMETHOD(bus_get_resource_list, nexus_get_resource_list),
129#ifdef SMP
130	DEVMETHOD(bus_bind_intr,	nexus_bind_intr),
131#endif
132	DEVMETHOD(bus_describe_intr,	nexus_describe_intr),
133	DEVMETHOD(bus_get_dma_tag,	nexus_get_dma_tag),
134
135	/* ofw_bus interface */
136	DEVMETHOD(ofw_bus_get_devinfo,	nexus_get_devinfo),
137	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
138	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
139	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
140	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
141	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
142
143	KOBJMETHOD_END
144};
145
146static devclass_t nexus_devclass;
147
148DEFINE_CLASS_0(nexus, nexus_driver, nexus_methods, sizeof(struct nexus_softc));
149EARLY_DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0,
150    BUS_PASS_BUS);
151MODULE_VERSION(nexus, 1);
152
153static const char *const nexus_excl_name[] = {
154	"aliases",
155	"associations",
156	"chosen",
157	"counter-timer",	/* No separate device; handled by psycho/sbus */
158	"memory",
159	"openprom",
160	"options",
161	"packages",
162	"rsc",
163	"virtual-memory",
164	NULL
165};
166
167static const char *const nexus_excl_type[] = {
168	"cpu",
169	NULL
170};
171
172extern struct bus_space_tag nexus_bustag;
173extern struct bus_dma_tag nexus_dmatag;
174
175static int
176nexus_inlist(const char *name, const char *const *list)
177{
178	int i;
179
180	if (name == NULL)
181		return (0);
182	for (i = 0; list[i] != NULL; i++)
183		if (strcmp(name, list[i]) == 0)
184			return (1);
185	return (0);
186}
187
188#define	NEXUS_EXCLUDED(name, type)					\
189	(nexus_inlist((name), nexus_excl_name) ||			\
190	((type) != NULL && nexus_inlist((type), nexus_excl_type)))
191
192static int
193nexus_probe(device_t dev)
194{
195
196	/* Nexus does always match. */
197	device_set_desc(dev, "Open Firmware Nexus device");
198	return (0);
199}
200
201static int
202nexus_attach(device_t dev)
203{
204	struct nexus_devinfo *ndi;
205	struct nexus_softc *sc;
206	device_t cdev;
207	phandle_t node;
208
209	node = OF_peer(0);
210	if (node == -1)
211		panic("%s: OF_peer failed.", __func__);
212
213	sc = device_get_softc(dev);
214	sc->sc_intr_rman.rm_type = RMAN_ARRAY;
215	sc->sc_intr_rman.rm_descr = "Interrupts";
216	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
217	sc->sc_mem_rman.rm_descr = "Device Memory";
218	if (rman_init(&sc->sc_intr_rman) != 0 ||
219	    rman_init(&sc->sc_mem_rman) != 0 ||
220	    rman_manage_region(&sc->sc_intr_rman, 0, IV_MAX - 1) != 0 ||
221	    rman_manage_region(&sc->sc_mem_rman, 0ULL, ~0ULL) != 0)
222		panic("%s: failed to set up rmans.", __func__);
223
224	/*
225	 * Allow devices to identify.
226	 */
227	bus_generic_probe(dev);
228
229	/*
230	 * Now walk the OFW tree and attach top-level devices.
231	 */
232	for (node = OF_child(node); node > 0; node = OF_peer(node)) {
233		if ((ndi = nexus_setup_dinfo(dev, node)) == NULL)
234			continue;
235		cdev = device_add_child(dev, NULL, -1);
236		if (cdev == NULL) {
237			device_printf(dev, "<%s>: device_add_child failed\n",
238			    ndi->ndi_obdinfo.obd_name);
239			nexus_destroy_dinfo(ndi);
240			continue;
241		}
242		device_set_ivars(cdev, ndi);
243	}
244	return (bus_generic_attach(dev));
245}
246
247static device_t
248nexus_add_child(device_t dev, int order, const char *name, int unit)
249{
250	device_t cdev;
251	struct nexus_devinfo *ndi;
252
253	cdev = device_add_child_ordered(dev, order, name, unit);
254	if (cdev == NULL)
255		return (NULL);
256
257	ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
258	ndi->ndi_obdinfo.obd_node = -1;
259	ndi->ndi_obdinfo.obd_name = strdup(name, M_OFWPROP);
260	resource_list_init(&ndi->ndi_rl);
261	device_set_ivars(cdev, ndi);
262
263	return (cdev);
264}
265
266static int
267nexus_print_child(device_t dev, device_t child)
268{
269	int rv;
270
271	rv = bus_print_child_header(dev, child);
272	rv += nexus_print_res(device_get_ivars(child));
273	rv += bus_print_child_footer(dev, child);
274	return (rv);
275}
276
277static void
278nexus_probe_nomatch(device_t dev, device_t child)
279{
280	const char *type;
281
282	device_printf(dev, "<%s>", ofw_bus_get_name(child));
283	nexus_print_res(device_get_ivars(child));
284	type = ofw_bus_get_type(child);
285	printf(" type %s (no driver attached)\n",
286	    type != NULL ? type : "unknown");
287}
288
289static int
290nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
291    driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
292{
293	int error;
294
295	if (res == NULL)
296		panic("%s: NULL interrupt resource!", __func__);
297
298	if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
299		flags |= INTR_EXCL;
300
301	/* We depend here on rman_activate_resource() being idempotent. */
302	error = rman_activate_resource(res);
303	if (error)
304		return (error);
305
306	error = inthand_add(device_get_nameunit(child), rman_get_start(res),
307	    filt, intr, arg, flags, cookiep);
308
309	/*
310	 * XXX in case of the AFB/FFB interrupt and a Psycho, Sabre or U2S
311	 * bridge enable the interrupt in the respective bridge.
312	 */
313
314	return (error);
315}
316
317static int
318nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
319{
320
321	inthand_remove(rman_get_start(r), ih);
322	return (0);
323}
324
325#ifdef SMP
326static int
327nexus_bind_intr(device_t dev, device_t child, struct resource *r, int cpu)
328{
329
330	return (intr_bind(rman_get_start(r), cpu));
331}
332#endif
333
334static int
335nexus_describe_intr(device_t dev, device_t child, struct resource *r,
336    void *cookie, const char *descr)
337{
338
339	return (intr_describe(rman_get_start(r), cookie, descr));
340}
341
342static struct resource *
343nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
344    u_long start, u_long end, u_long count, u_int flags)
345{
346	struct nexus_softc *sc;
347	struct rman *rm;
348	struct resource *rv;
349	struct resource_list_entry *rle;
350	int isdefault, needactivate, passthrough;
351
352	isdefault = (start == 0UL && end == ~0UL);
353	needactivate = flags & RF_ACTIVE;
354	passthrough = (device_get_parent(child) != bus);
355	sc = device_get_softc(bus);
356	rle = NULL;
357
358	if (!passthrough) {
359		rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child),
360		    type, *rid);
361		if (rle == NULL)
362			return (NULL);
363		if (rle->res != NULL)
364			panic("%s: resource entry is busy", __func__);
365		if (isdefault) {
366			start = rle->start;
367			count = ulmax(count, rle->count);
368			end = ulmax(rle->end, start + count - 1);
369		}
370	}
371
372	switch (type) {
373	case SYS_RES_IRQ:
374		rm = &sc->sc_intr_rman;
375		break;
376	case SYS_RES_MEMORY:
377		rm = &sc->sc_mem_rman;
378		break;
379	default:
380		return (NULL);
381	}
382
383	flags &= ~RF_ACTIVE;
384	rv = rman_reserve_resource(rm, start, end, count, flags, child);
385	if (rv == NULL)
386		return (NULL);
387	rman_set_rid(rv, *rid);
388	if (type == SYS_RES_MEMORY) {
389		rman_set_bustag(rv, &nexus_bustag);
390		rman_set_bushandle(rv, rman_get_start(rv));
391	}
392
393	if (needactivate) {
394		if (bus_activate_resource(child, type, *rid, rv) != 0) {
395			rman_release_resource(rv);
396			return (NULL);
397		}
398	}
399
400	if (!passthrough) {
401		rle->res = rv;
402		rle->start = rman_get_start(rv);
403		rle->end = rman_get_end(rv);
404		rle->count = rle->end - rle->start + 1;
405	}
406
407	return (rv);
408}
409
410static int
411nexus_activate_resource(device_t bus, device_t child, int type, int rid,
412    struct resource *r)
413{
414
415	/* Not much to be done yet... */
416	return (rman_activate_resource(r));
417}
418
419static int
420nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
421    struct resource *r)
422{
423
424	/* Not much to be done yet... */
425	return (rman_deactivate_resource(r));
426}
427
428static int
429nexus_release_resource(device_t bus, device_t child, int type, int rid,
430    struct resource *r)
431{
432	int error;
433
434	if (rman_get_flags(r) & RF_ACTIVE) {
435		error = bus_deactivate_resource(child, type, rid, r);
436		if (error)
437			return (error);
438	}
439	return (rman_release_resource(r));
440}
441
442static struct resource_list *
443nexus_get_resource_list(device_t dev, device_t child)
444{
445	struct nexus_devinfo *ndi;
446
447	ndi = device_get_ivars(child);
448	return (&ndi->ndi_rl);
449}
450
451static bus_dma_tag_t
452nexus_get_dma_tag(device_t bus, device_t child)
453{
454
455	return (&nexus_dmatag);
456}
457
458static const struct ofw_bus_devinfo *
459nexus_get_devinfo(device_t dev, device_t child)
460{
461	struct nexus_devinfo *ndi;
462
463	ndi = device_get_ivars(child);
464	return (&ndi->ndi_obdinfo);
465}
466
467static struct nexus_devinfo *
468nexus_setup_dinfo(device_t dev, phandle_t node)
469{
470	struct nexus_devinfo *ndi;
471	struct nexus_regs *reg;
472	bus_addr_t phys;
473	bus_size_t size;
474	uint32_t ign;
475	uint32_t *intr;
476	int i;
477	int nintr;
478	int nreg;
479
480	ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
481	if (ofw_bus_gen_setup_devinfo(&ndi->ndi_obdinfo, node) != 0) {
482		free(ndi, M_DEVBUF);
483		return (NULL);
484	}
485	if (NEXUS_EXCLUDED(ndi->ndi_obdinfo.obd_name,
486	    ndi->ndi_obdinfo.obd_type)) {
487		ofw_bus_gen_destroy_devinfo(&ndi->ndi_obdinfo);
488		free(ndi, M_DEVBUF);
489		return (NULL);
490	}
491	resource_list_init(&ndi->ndi_rl);
492	nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
493	if (nreg == -1) {
494		device_printf(dev, "<%s>: incomplete\n",
495		    ndi->ndi_obdinfo.obd_name);
496		goto fail;
497	}
498	for (i = 0; i < nreg; i++) {
499		phys = NEXUS_REG_PHYS(&reg[i]);
500		size = NEXUS_REG_SIZE(&reg[i]);
501		resource_list_add(&ndi->ndi_rl, SYS_RES_MEMORY, i, phys,
502		    phys + size - 1, size);
503	}
504	free(reg, M_OFWPROP);
505
506	nintr = OF_getprop_alloc(node, "interrupts",  sizeof(*intr),
507	    (void **)&intr);
508	if (nintr > 0) {
509		if (OF_getprop(node, cpu_impl < CPU_IMPL_ULTRASPARCIII ?
510		    "upa-portid" : "portid", &ign, sizeof(ign)) <= 0) {
511			device_printf(dev, "<%s>: could not determine portid\n",
512			    ndi->ndi_obdinfo.obd_name);
513			free(intr, M_OFWPROP);
514			goto fail;
515		}
516
517		/* XXX 7-bit MID on Starfire */
518		ign = (ign << INTMAP_IGN_SHIFT) & INTMAP_IGN_MASK;
519		for (i = 0; i < nintr; i++) {
520			intr[i] |= ign;
521			resource_list_add(&ndi->ndi_rl, SYS_RES_IRQ, i, intr[i],
522			    intr[i], 1);
523		}
524		free(intr, M_OFWPROP);
525	}
526
527	return (ndi);
528
529 fail:
530	nexus_destroy_dinfo(ndi);
531	return (NULL);
532}
533
534static void
535nexus_destroy_dinfo(struct nexus_devinfo *ndi)
536{
537
538	resource_list_free(&ndi->ndi_rl);
539	ofw_bus_gen_destroy_devinfo(&ndi->ndi_obdinfo);
540	free(ndi, M_DEVBUF);
541}
542
543static int
544nexus_print_res(struct nexus_devinfo *ndi)
545{
546	int rv;
547
548	rv = 0;
549	rv += resource_list_print_type(&ndi->ndi_rl, "mem", SYS_RES_MEMORY,
550	    "%#lx");
551	rv += resource_list_print_type(&ndi->ndi_rl, "irq", SYS_RES_IRQ,
552	    "%ld");
553	return (rv);
554}
555