nexus.c revision 88823
186227Stmm/*
286227Stmm * Copyright 1998 Massachusetts Institute of Technology
386227Stmm *
486227Stmm * Permission to use, copy, modify, and distribute this software and
586227Stmm * its documentation for any purpose and without fee is hereby
686227Stmm * granted, provided that both the above copyright notice and this
786227Stmm * permission notice appear in all copies, that both the above
886227Stmm * copyright notice and this permission notice appear in all
986227Stmm * supporting documentation, and that the name of M.I.T. not be used
1086227Stmm * in advertising or publicity pertaining to distribution of the
1186227Stmm * software without specific, written prior permission.  M.I.T. makes
1286227Stmm * no representations about the suitability of this software for any
1386227Stmm * purpose.  It is provided "as is" without express or implied
1486227Stmm * warranty.
1586227Stmm *
1686227Stmm * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
1786227Stmm * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
1886227Stmm * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1986227Stmm * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
2086227Stmm * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2186227Stmm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2286227Stmm * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2386227Stmm * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2486227Stmm * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2586227Stmm * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2686227Stmm * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2786227Stmm * SUCH DAMAGE.
2886227Stmm */
2986227Stmm/*-
3086227Stmm * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>.  All rights reserved.
3186227Stmm *
3286227Stmm * Redistribution and use in source and binary forms, with or without
3386227Stmm * modification, are permitted provided that the following conditions
3486227Stmm * are met:
3586227Stmm * 1. Redistributions of source code must retain the above copyright
3686227Stmm *    notice, this list of conditions and the following disclaimer.
3786227Stmm * 2. Redistributions in binary form must reproduce the above copyright
3886227Stmm *    notice, this list of conditions and the following disclaimer in the
3986227Stmm *    documentation and/or other materials provided with the distribution.
4086227Stmm *
4186227Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
4286227Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4386227Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4486227Stmm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4586227Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4686227Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4786227Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4886227Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4986227Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5086227Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5186227Stmm * SUCH DAMAGE.
5286227Stmm *
5386227Stmm * 	from: FreeBSD: src/sys/i386/i386/nexus.c,v 1.43 2001/02/09
5486227Stmm *
5586227Stmm * $FreeBSD: head/sys/sparc64/sparc64/nexus.c 88823 2002-01-02 18:27:13Z tmm $
5686227Stmm */
5786227Stmm
5886227Stmm#include <sys/param.h>
5986227Stmm#include <sys/systm.h>
6086227Stmm#include <sys/bus.h>
6186227Stmm#include <sys/cons.h>
6286227Stmm#include <sys/kernel.h>
6386227Stmm#include <sys/malloc.h>
6486227Stmm
6586227Stmm#include <dev/ofw/openfirm.h>
6686227Stmm
6786227Stmm#include <machine/bus.h>
6886227Stmm#include <machine/frame.h>
6986227Stmm#include <machine/intr_machdep.h>
7086227Stmm#include <machine/nexusvar.h>
7188823Stmm#include <machine/ofw_upa.h>
7286227Stmm#include <machine/resource.h>
7388823Stmm#include <machine/upa.h>
7486227Stmm
7586227Stmm#include <sys/rman.h>
7686227Stmm
7786227Stmm/*
7886227Stmm * The nexus (which is a pseudo-bus actually) iterates over the nodes that
7986227Stmm * hang from the OpenFirmware root node and add them as devices to this bus
8086227Stmm * (except some special nodes which are excluded) so that drivers can be
8186227Stmm * attached to them. This saves lots of detection work.
8286227Stmm * Usually, all devices and bridges that are attached to the UltraSparc UPA
8386227Stmm * bus will show up here, plus some pseudo-nodes which are excluded.
8486227Stmm * For now, the only node that gets used is probably the pci bus one.
8586227Stmm *
8686227Stmm * Additionally, interrupt setup/teardown and some resource management are
8786227Stmm * done at this level.
8886227Stmm *
8986227Stmm * Maybe this code should get into dev/ofw to some extent, as some of it should
9086227Stmm * work for all OpenFirmware based machines...
9186227Stmm */
9286227Stmm
9386227Stmmstatic MALLOC_DEFINE(M_NEXUS, "nexus", "nexus device information");
9486227Stmm
9586227Stmmstruct nexus_devinfo {
9686227Stmm	phandle_t	ndi_node;
9786227Stmm	/* Some common properties. */
9886227Stmm	char		*ndi_name;
9986227Stmm	char		*ndi_device_type;
10086227Stmm	char		*ndi_model;
10188823Stmm	struct		upa_regs *ndi_reg;
10286227Stmm	int		ndi_nreg;
10386227Stmm	u_int		*ndi_interrupts;
10486227Stmm	int		ndi_ninterrupts;
10586227Stmm	bus_space_tag_t	ndi_bustag;
10686227Stmm	bus_dma_tag_t	ndi_dmatag;
10786227Stmm};
10886227Stmm
10988823Stmmstruct nexus_softc {
11088823Stmm	struct rman	sc_intr_rman;
11188823Stmm	struct rman	sc_mem_rman;
11288823Stmm};
11388823Stmm
11486227Stmmstatic int nexus_probe(device_t);
11586227Stmmstatic void nexus_probe_nomatch(device_t, device_t);
11686227Stmmstatic int nexus_read_ivar(device_t, device_t, int, uintptr_t *);
11786227Stmmstatic int nexus_write_ivar(device_t, device_t, int, uintptr_t);
11886227Stmmstatic int nexus_setup_intr(device_t, device_t, struct resource *, int,
11986227Stmm    driver_intr_t *, void *, void **);
12086227Stmmstatic int nexus_teardown_intr(device_t, device_t, struct resource *,
12186227Stmm    void *);
12286227Stmmstatic struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
12386227Stmm    u_long, u_long, u_long, u_int);
12486227Stmmstatic int nexus_activate_resource(device_t, device_t, int, int,
12586227Stmm    struct resource *);
12686227Stmmstatic int nexus_deactivate_resource(device_t, device_t, int, int,
12786227Stmm    struct resource *);
12886227Stmmstatic int nexus_release_resource(device_t, device_t, int, int,
12986227Stmm    struct resource *);
13086227Stmm
13186227Stmmstatic device_method_t nexus_methods[] = {
13286227Stmm	/* Device interface */
13386227Stmm	DEVMETHOD(device_probe,		nexus_probe),
13486227Stmm	DEVMETHOD(device_attach,	bus_generic_attach),
13586227Stmm	DEVMETHOD(device_detach,	bus_generic_detach),
13686227Stmm	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
13786227Stmm	DEVMETHOD(device_suspend,	bus_generic_suspend),
13886227Stmm	DEVMETHOD(device_resume,	bus_generic_resume),
13986227Stmm
14086227Stmm	/* Bus interface. Resource management is business of the children... */
14186227Stmm	DEVMETHOD(bus_print_child,	bus_generic_print_child),
14286227Stmm	DEVMETHOD(bus_probe_nomatch,	nexus_probe_nomatch),
14386227Stmm	DEVMETHOD(bus_read_ivar,	nexus_read_ivar),
14486227Stmm	DEVMETHOD(bus_write_ivar,	nexus_write_ivar),
14586227Stmm	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
14686227Stmm	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
14786227Stmm	DEVMETHOD(bus_alloc_resource,	nexus_alloc_resource),
14886227Stmm	DEVMETHOD(bus_activate_resource,	nexus_activate_resource),
14986227Stmm	DEVMETHOD(bus_deactivate_resource,	nexus_deactivate_resource),
15086227Stmm	DEVMETHOD(bus_release_resource,	nexus_release_resource),
15186227Stmm
15286227Stmm	{ 0, 0 }
15386227Stmm};
15486227Stmm
15586227Stmmstatic driver_t nexus_driver = {
15686227Stmm	"nexus",
15786227Stmm	nexus_methods,
15888823Stmm	sizeof(struct nexus_softc),
15986227Stmm};
16086227Stmm
16186227Stmmstatic devclass_t nexus_devclass;
16286227Stmm
16386227StmmDRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
16486227Stmm
16586227Stmmstatic char *nexus_excl_name[] = {
16686227Stmm	"virtual-memory",
16786227Stmm	"memory",
16886227Stmm	"aliases",
16986227Stmm	"options",
17086227Stmm	"openprom",
17186227Stmm	"chosen",
17286227Stmm	"packages",
17386227Stmm	NULL
17486227Stmm};
17586227Stmm
17686227Stmmstatic char *nexus_excl_type[] = {
17786227Stmm	"cpu",
17886227Stmm	NULL
17986227Stmm};
18086227Stmm
18186227Stmmextern struct bus_space_tag nexus_bustag;
18286227Stmmextern struct bus_dma_tag nexus_dmatag;
18386227Stmm
18486227Stmmstatic int
18586227Stmmnexus_inlist(char *name, char *list[])
18686227Stmm{
18786227Stmm	int i;
18886227Stmm
18986227Stmm	for (i = 0; list[i] != NULL; i++)
19086227Stmm		if (strcmp(name, list[i]) == 0)
19186227Stmm			return (1);
19286227Stmm	return (0);
19386227Stmm}
19486227Stmm
19586227Stmm#define	NEXUS_EXCLUDED(name, type)					\
19686227Stmm	(nexus_inlist((name), nexus_excl_name) ||			\
19786227Stmm	((type) != NULL && nexus_inlist((type), nexus_excl_type)))
19886227Stmm
19986227Stmmstatic int
20086227Stmmnexus_probe(device_t dev)
20186227Stmm{
20286227Stmm	phandle_t root;
20386227Stmm	phandle_t child;
20486227Stmm	device_t cdev;
20586227Stmm	struct nexus_devinfo *dinfo;
20688823Stmm	struct nexus_softc *sc;
20786227Stmm	char *name, *type;
20886227Stmm
20986227Stmm	if ((root = OF_peer(0)) == -1)
21086227Stmm		panic("nexus_probe: OF_peer failed.");
21186227Stmm
21288823Stmm	sc = device_get_softc(dev);
21388823Stmm	sc->sc_intr_rman.rm_type = RMAN_ARRAY;
21488823Stmm	sc->sc_intr_rman.rm_descr = "Interrupts";
21588823Stmm	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
21688823Stmm	sc->sc_mem_rman.rm_descr = "UPA Device Memory";
21788823Stmm	if (rman_init(&sc->sc_intr_rman) != 0 ||
21888823Stmm	    rman_init(&sc->sc_mem_rman) != 0 ||
21988823Stmm	    rman_manage_region(&sc->sc_intr_rman, 0, NIV - 1) != 0 ||
22088823Stmm	    rman_manage_region(&sc->sc_mem_rman, UPA_MEMSTART, UPA_MEMEND) != 0)
22188823Stmm		panic("nexus_probe: failed to set up rmans");
22286227Stmm	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
22386227Stmm		if (child == -1)
22486227Stmm			panic("nexus_probe(): OF_child failed.");
22586227Stmm		if (OF_getprop_alloc(child, "name", 1, (void **)&name) == -1)
22686227Stmm			continue;
22786227Stmm		OF_getprop_alloc(child, "device_type", 1, (void **)&type);
22886227Stmm		if (NEXUS_EXCLUDED(name, type)) {
22986227Stmm			free(name, M_OFWPROP);
23086227Stmm			if (type != NULL)
23186227Stmm				free(type, M_OFWPROP);
23286227Stmm			continue;
23388823Stmm		}
23486227Stmm		cdev = device_add_child(dev, NULL, -1);
23586227Stmm		if (cdev != NULL) {
23686227Stmm			dinfo = malloc(sizeof(*dinfo), M_NEXUS, M_WAITOK);
23786227Stmm			dinfo->ndi_node = child;
23886227Stmm			dinfo->ndi_name = name;
23986227Stmm			dinfo->ndi_device_type = type;
24086227Stmm			OF_getprop_alloc(child, "model", 1,
24186227Stmm			    (void **)&dinfo->ndi_model);
24286227Stmm			dinfo->ndi_nreg = OF_getprop_alloc(child, "reg",
24386227Stmm			    sizeof(*dinfo->ndi_reg), (void **)&dinfo->ndi_reg);
24486227Stmm			dinfo->ndi_ninterrupts = OF_getprop_alloc(child,
24586227Stmm			    "interrupts", sizeof(*dinfo->ndi_interrupts),
24686227Stmm			    (void **)&dinfo->ndi_interrupts);
24786227Stmm			dinfo->ndi_bustag = &nexus_bustag;
24886227Stmm			dinfo->ndi_dmatag = &nexus_dmatag;
24986227Stmm			device_set_ivars(cdev, dinfo);
25086227Stmm		} else
25186227Stmm			free(name, M_OFWPROP);
25286227Stmm
25386227Stmm	}
25486227Stmm	device_set_desc(dev, "OpenFirmware Nexus device");
25586227Stmm	return (0);
25686227Stmm}
25786227Stmm
25886227Stmmstatic void
25986227Stmmnexus_probe_nomatch(device_t dev, device_t child)
26086227Stmm{
26186227Stmm	char *name;
26286227Stmm	char *type;
26386227Stmm
26486227Stmm	if (BUS_READ_IVAR(dev, child, NEXUS_IVAR_NAME,
26586227Stmm	    (uintptr_t *)&name) != 0 ||
26686227Stmm	    BUS_READ_IVAR(dev, child, NEXUS_IVAR_DEVICE_TYPE,
26786227Stmm	    (uintptr_t *)&type) != 0)
26886227Stmm		return;
26988823Stmm
27086227Stmm	if (type == NULL)
27186227Stmm		type = "(unknown)";
27286227Stmm	device_printf(dev, "<%s>, type %s (no driver attached)\n",
27386227Stmm	    name, type);
27486227Stmm}
27586227Stmm
27686227Stmmstatic int
27786227Stmmnexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
27886227Stmm{
27986227Stmm	struct nexus_devinfo *dinfo;
28086227Stmm
28186227Stmm	if ((dinfo = device_get_ivars(child)) == 0)
28286227Stmm		return (ENOENT);
28386227Stmm	switch (which) {
28486227Stmm	case NEXUS_IVAR_NODE:
28586227Stmm		*result = dinfo->ndi_node;
28686227Stmm		break;
28786227Stmm	case NEXUS_IVAR_NAME:
28886227Stmm		*result = (uintptr_t)dinfo->ndi_name;
28986227Stmm		break;
29086227Stmm	case NEXUS_IVAR_DEVICE_TYPE:
29186227Stmm		*result = (uintptr_t)dinfo->ndi_device_type;
29286227Stmm		break;
29386227Stmm	case NEXUS_IVAR_MODEL:
29486227Stmm		*result = (uintptr_t)dinfo->ndi_model;
29586227Stmm		break;
29686227Stmm	case NEXUS_IVAR_REG:
29786227Stmm		*result = (uintptr_t)dinfo->ndi_reg;
29886227Stmm		break;
29986227Stmm	case NEXUS_IVAR_NREG:
30086227Stmm		*result = dinfo->ndi_nreg;
30186227Stmm		break;
30286227Stmm	case NEXUS_IVAR_INTERRUPTS:
30386227Stmm		*result = (uintptr_t)dinfo->ndi_interrupts;
30486227Stmm		break;
30586227Stmm	case NEXUS_IVAR_NINTERRUPTS:
30686227Stmm		*result = dinfo->ndi_ninterrupts;
30786227Stmm		break;
30886227Stmm	case NEXUS_IVAR_DMATAG:
30986227Stmm		*result = (uintptr_t)dinfo->ndi_dmatag;
31086227Stmm		break;
31186227Stmm	default:
31286227Stmm		return (ENOENT);
31386227Stmm	}
31486227Stmm	return 0;
31586227Stmm}
31686227Stmm
31786227Stmmstatic int
31886227Stmmnexus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
31986227Stmm{
32086227Stmm	struct nexus_devinfo *dinfo;
32186227Stmm
32286227Stmm	if ((dinfo = device_get_ivars(child)) == 0)
32386227Stmm		return (ENOENT);
32486227Stmm
32586227Stmm	switch (which) {
32686227Stmm	case NEXUS_IVAR_NODE:
32786227Stmm	case NEXUS_IVAR_NAME:
32886227Stmm	case NEXUS_IVAR_DEVICE_TYPE:
32986227Stmm	case NEXUS_IVAR_MODEL:
33086227Stmm	case NEXUS_IVAR_REG:
33186227Stmm	case NEXUS_IVAR_NREG:
33286227Stmm	case NEXUS_IVAR_INTERRUPTS:
33386227Stmm	case NEXUS_IVAR_NINTERRUPTS:
33486227Stmm	case NEXUS_IVAR_DMATAG:
33586227Stmm		return (EINVAL);
33686227Stmm	default:
33786227Stmm		return (ENOENT);
33886227Stmm	}
33986227Stmm	return 0;
34086227Stmm}
34186227Stmm
34286227Stmmstatic int
34386227Stmmnexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
34486227Stmm    driver_intr_t *intr, void *arg, void **cookiep)
34586227Stmm{
34686227Stmm	int error;
34788823Stmm
34886227Stmm	if (res == NULL)
34986227Stmm		panic("nexus_setup_intr: NULL interrupt resource!");
35086227Stmm
35186227Stmm	if ((res->r_flags & RF_SHAREABLE) == 0)
35286227Stmm		flags |= INTR_EXCL;
35386227Stmm
35486227Stmm	/*
35586227Stmm	 * We depend here on rman_activate_resource() being idempotent.
35686227Stmm	 */
35786227Stmm	error = rman_activate_resource(res);
35886227Stmm	if (error)
35986227Stmm		return (error);
36086227Stmm
36186227Stmm	error = inthand_add(device_get_nameunit(child), res->r_start,
36286227Stmm	    intr, arg, flags, cookiep);
36386227Stmm
36486227Stmm	return (error);
36586227Stmm}
36686227Stmm
36786227Stmmstatic int
36886227Stmmnexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
36986227Stmm{
37086227Stmm	inthand_remove(r->r_start, ih);
37186227Stmm	return (0);
37286227Stmm}
37386227Stmm
37486227Stmm/*
37586227Stmm * Allocate resources at the behalf of a child. This only handles interrupts,
37686227Stmm * since i/o resources are usually set up by the firmware, and thus need not
37786227Stmm * be handled here.
37886227Stmm */
37986227Stmmstatic struct resource *
38086227Stmmnexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
38186227Stmm    u_long start, u_long end, u_long count, u_int flags)
38286227Stmm{
38388823Stmm	struct	nexus_softc *sc = device_get_softc(bus);
38486227Stmm	struct	resource *rv;
38586227Stmm	struct	rman *rm;
38686227Stmm	int needactivate = flags & RF_ACTIVE;
38786227Stmm
38886227Stmm	flags &= ~RF_ACTIVE;
38986227Stmm
39086227Stmm	switch (type) {
39186227Stmm	case SYS_RES_IRQ:
39288823Stmm		rm = &sc->sc_intr_rman;
39386227Stmm		break;
39488823Stmm	case SYS_RES_MEMORY:
39588823Stmm		rm = &sc->sc_mem_rman;
39688823Stmm		break;
39786227Stmm	default:
39886227Stmm		return (NULL);
39986227Stmm	}
40086227Stmm
40186227Stmm	rv = rman_reserve_resource(rm, start, end, count, flags, child);
40286227Stmm	if (rv == NULL)
40386227Stmm		return (NULL);
40488823Stmm	if (type == SYS_RES_MEMORY) {
40588823Stmm		rman_set_bustag(rv, &nexus_bustag);
40688823Stmm		rman_set_bushandle(rv, rman_get_start(rv));
40788823Stmm	}
40886227Stmm
40986227Stmm	if (needactivate) {
41086227Stmm		if (bus_activate_resource(child, type, *rid, rv)) {
41186227Stmm			rman_release_resource(rv);
41286227Stmm			return (NULL);
41386227Stmm		}
41486227Stmm	}
41588823Stmm
41686227Stmm	return (rv);
41786227Stmm}
41886227Stmm
41986227Stmmstatic int
42086227Stmmnexus_activate_resource(device_t bus, device_t child, int type, int rid,
42186227Stmm    struct resource *r)
42286227Stmm{
42386227Stmm
42486227Stmm	/* Not much to be done yet... */
42586227Stmm	return (rman_activate_resource(r));
42686227Stmm}
42786227Stmm
42886227Stmmstatic int
42986227Stmmnexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
43086227Stmm    struct resource *r)
43186227Stmm{
43288823Stmm
43386227Stmm	/* Not much to be done yet... */
43486227Stmm	return (rman_deactivate_resource(r));
43586227Stmm}
43686227Stmm
43786227Stmmstatic int
43886227Stmmnexus_release_resource(device_t bus, device_t child, int type, int rid,
43986227Stmm		       struct resource *r)
44086227Stmm{
44186227Stmm	int error;
44286227Stmm
44386227Stmm	if (rman_get_flags(r) & RF_ACTIVE) {
44486227Stmm		error = bus_deactivate_resource(child, type, rid, r);
44586227Stmm		if (error)
44686227Stmm			return error;
44786227Stmm	}
44886227Stmm	return (rman_release_resource(r));
44986227Stmm}
450