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