nexus.c revision 86227
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 86227 2001-11-09 20:01:25Z 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>
7186227Stmm#include <machine/resource.h>
7286227Stmm
7386227Stmm#include <sys/rman.h>
7486227Stmm
7586227Stmm/*
7686227Stmm * The nexus (which is a pseudo-bus actually) iterates over the nodes that
7786227Stmm * hang from the OpenFirmware root node and add them as devices to this bus
7886227Stmm * (except some special nodes which are excluded) so that drivers can be
7986227Stmm * attached to them. This saves lots of detection work.
8086227Stmm * Usually, all devices and bridges that are attached to the UltraSparc UPA
8186227Stmm * bus will show up here, plus some pseudo-nodes which are excluded.
8286227Stmm * For now, the only node that gets used is probably the pci bus one.
8386227Stmm *
8486227Stmm * Additionally, interrupt setup/teardown and some resource management are
8586227Stmm * done at this level.
8686227Stmm *
8786227Stmm * Maybe this code should get into dev/ofw to some extent, as some of it should
8886227Stmm * work for all OpenFirmware based machines...
8986227Stmm */
9086227Stmm
9186227Stmmstatic MALLOC_DEFINE(M_NEXUS, "nexus", "nexus device information");
9286227Stmm
9386227Stmmstruct nexus_devinfo {
9486227Stmm	phandle_t	ndi_node;
9586227Stmm	/* Some common properties. */
9686227Stmm	char		*ndi_name;
9786227Stmm	char		*ndi_device_type;
9886227Stmm	char		*ndi_model;
9986227Stmm	struct		ofw_nexus_reg *ndi_reg;
10086227Stmm	int		ndi_nreg;
10186227Stmm	u_int		*ndi_interrupts;
10286227Stmm	int		ndi_ninterrupts;
10386227Stmm	bus_space_tag_t	ndi_bustag;
10486227Stmm	bus_dma_tag_t	ndi_dmatag;
10586227Stmm};
10686227Stmm
10786227Stmmstatic int nexus_probe(device_t);
10886227Stmmstatic void nexus_probe_nomatch(device_t, device_t);
10986227Stmmstatic int nexus_read_ivar(device_t, device_t, int, uintptr_t *);
11086227Stmmstatic int nexus_write_ivar(device_t, device_t, int, uintptr_t);
11186227Stmmstatic int nexus_setup_intr(device_t, device_t, struct resource *, int,
11286227Stmm    driver_intr_t *, void *, void **);
11386227Stmmstatic int nexus_teardown_intr(device_t, device_t, struct resource *,
11486227Stmm    void *);
11586227Stmmstatic struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
11686227Stmm    u_long, u_long, u_long, u_int);
11786227Stmmstatic int nexus_activate_resource(device_t, device_t, int, int,
11886227Stmm    struct resource *);
11986227Stmmstatic int nexus_deactivate_resource(device_t, device_t, int, int,
12086227Stmm    struct resource *);
12186227Stmmstatic int nexus_release_resource(device_t, device_t, int, int,
12286227Stmm    struct resource *);
12386227Stmm
12486227Stmmstatic device_method_t nexus_methods[] = {
12586227Stmm	/* Device interface */
12686227Stmm	DEVMETHOD(device_probe,		nexus_probe),
12786227Stmm	DEVMETHOD(device_attach,	bus_generic_attach),
12886227Stmm	DEVMETHOD(device_detach,	bus_generic_detach),
12986227Stmm	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
13086227Stmm	DEVMETHOD(device_suspend,	bus_generic_suspend),
13186227Stmm	DEVMETHOD(device_resume,	bus_generic_resume),
13286227Stmm
13386227Stmm	/* Bus interface. Resource management is business of the children... */
13486227Stmm	DEVMETHOD(bus_print_child,	bus_generic_print_child),
13586227Stmm	DEVMETHOD(bus_probe_nomatch,	nexus_probe_nomatch),
13686227Stmm	DEVMETHOD(bus_read_ivar,	nexus_read_ivar),
13786227Stmm	DEVMETHOD(bus_write_ivar,	nexus_write_ivar),
13886227Stmm	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
13986227Stmm	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
14086227Stmm	DEVMETHOD(bus_alloc_resource,	nexus_alloc_resource),
14186227Stmm	DEVMETHOD(bus_activate_resource,	nexus_activate_resource),
14286227Stmm	DEVMETHOD(bus_deactivate_resource,	nexus_deactivate_resource),
14386227Stmm	DEVMETHOD(bus_release_resource,	nexus_release_resource),
14486227Stmm
14586227Stmm	{ 0, 0 }
14686227Stmm};
14786227Stmm
14886227Stmmstatic driver_t nexus_driver = {
14986227Stmm	"nexus",
15086227Stmm	nexus_methods,
15186227Stmm	1,			/* no softc */
15286227Stmm};
15386227Stmm
15486227Stmmstatic devclass_t nexus_devclass;
15586227Stmm
15686227StmmDRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
15786227Stmm
15886227Stmmstatic char *nexus_excl_name[] = {
15986227Stmm	"virtual-memory",
16086227Stmm	"memory",
16186227Stmm	"aliases",
16286227Stmm	"options",
16386227Stmm	"openprom",
16486227Stmm	"chosen",
16586227Stmm	"packages",
16686227Stmm	NULL
16786227Stmm};
16886227Stmm
16986227Stmmstatic char *nexus_excl_type[] = {
17086227Stmm	"cpu",
17186227Stmm	NULL
17286227Stmm};
17386227Stmm
17486227Stmmstruct rman intr_rman;
17586227Stmmextern struct bus_space_tag nexus_bustag;
17686227Stmmextern struct bus_dma_tag nexus_dmatag;
17786227Stmm
17886227Stmmstatic int
17986227Stmmnexus_inlist(char *name, char *list[])
18086227Stmm{
18186227Stmm	int i;
18286227Stmm
18386227Stmm	for (i = 0; list[i] != NULL; i++)
18486227Stmm		if (strcmp(name, list[i]) == 0)
18586227Stmm			return (1);
18686227Stmm	return (0);
18786227Stmm}
18886227Stmm
18986227Stmm#define	NEXUS_EXCLUDED(name, type)					\
19086227Stmm	(nexus_inlist((name), nexus_excl_name) ||			\
19186227Stmm	((type) != NULL && nexus_inlist((type), nexus_excl_type)))
19286227Stmm
19386227Stmmstatic int
19486227Stmmnexus_probe(device_t dev)
19586227Stmm{
19686227Stmm	phandle_t root;
19786227Stmm	phandle_t child;
19886227Stmm	device_t cdev;
19986227Stmm	struct nexus_devinfo *dinfo;
20086227Stmm	char *name, *type;
20186227Stmm
20286227Stmm	if ((root = OF_peer(0)) == -1)
20386227Stmm		panic("nexus_probe: OF_peer failed.");
20486227Stmm
20586227Stmm	intr_rman.rm_type = RMAN_ARRAY;
20686227Stmm	intr_rman.rm_descr = "Interrupts";
20786227Stmm	if (rman_init(&intr_rman) != 0 ||
20886227Stmm	    rman_manage_region(&intr_rman, 0, NIV - 1) != 0)
20986227Stmm		panic("nexus_probe: failed to set up rman");
21086227Stmm	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
21186227Stmm		if (child == -1)
21286227Stmm			panic("nexus_probe(): OF_child failed.");
21386227Stmm		if (OF_getprop_alloc(child, "name", 1, (void **)&name) == -1)
21486227Stmm			continue;
21586227Stmm		OF_getprop_alloc(child, "device_type", 1, (void **)&type);
21686227Stmm		if (NEXUS_EXCLUDED(name, type)) {
21786227Stmm			free(name, M_OFWPROP);
21886227Stmm			if (type != NULL)
21986227Stmm				free(type, M_OFWPROP);
22086227Stmm			continue;
22186227Stmm		}
22286227Stmm		cdev = device_add_child(dev, NULL, -1);
22386227Stmm		if (cdev != NULL) {
22486227Stmm			dinfo = malloc(sizeof(*dinfo), M_NEXUS, M_WAITOK);
22586227Stmm			dinfo->ndi_node = child;
22686227Stmm			dinfo->ndi_name = name;
22786227Stmm			dinfo->ndi_device_type = type;
22886227Stmm			OF_getprop_alloc(child, "model", 1,
22986227Stmm			    (void **)&dinfo->ndi_model);
23086227Stmm			dinfo->ndi_nreg = OF_getprop_alloc(child, "reg",
23186227Stmm			    sizeof(*dinfo->ndi_reg), (void **)&dinfo->ndi_reg);
23286227Stmm			dinfo->ndi_ninterrupts = OF_getprop_alloc(child,
23386227Stmm			    "interrupts", sizeof(*dinfo->ndi_interrupts),
23486227Stmm			    (void **)&dinfo->ndi_interrupts);
23586227Stmm			dinfo->ndi_bustag = &nexus_bustag;
23686227Stmm			dinfo->ndi_dmatag = &nexus_dmatag;
23786227Stmm			device_set_ivars(cdev, dinfo);
23886227Stmm		} else
23986227Stmm			free(name, M_OFWPROP);
24086227Stmm
24186227Stmm	}
24286227Stmm	device_set_desc(dev, "OpenFirmware Nexus device");
24386227Stmm	return (0);
24486227Stmm}
24586227Stmm
24686227Stmmstatic void
24786227Stmmnexus_probe_nomatch(device_t dev, device_t child)
24886227Stmm{
24986227Stmm	char *name;
25086227Stmm	char *type;
25186227Stmm
25286227Stmm	if (BUS_READ_IVAR(dev, child, NEXUS_IVAR_NAME,
25386227Stmm	    (uintptr_t *)&name) != 0 ||
25486227Stmm	    BUS_READ_IVAR(dev, child, NEXUS_IVAR_DEVICE_TYPE,
25586227Stmm	    (uintptr_t *)&type) != 0)
25686227Stmm		return;
25786227Stmm
25886227Stmm	if (type == NULL)
25986227Stmm		type = "(unknown)";
26086227Stmm	device_printf(dev, "<%s>, type %s (no driver attached)\n",
26186227Stmm	    name, type);
26286227Stmm}
26386227Stmm
26486227Stmmstatic int
26586227Stmmnexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
26686227Stmm{
26786227Stmm	struct nexus_devinfo *dinfo;
26886227Stmm
26986227Stmm	if ((dinfo = device_get_ivars(child)) == 0)
27086227Stmm		return (ENOENT);
27186227Stmm	switch (which) {
27286227Stmm	case NEXUS_IVAR_NODE:
27386227Stmm		*result = dinfo->ndi_node;
27486227Stmm		break;
27586227Stmm	case NEXUS_IVAR_NAME:
27686227Stmm		*result = (uintptr_t)dinfo->ndi_name;
27786227Stmm		break;
27886227Stmm	case NEXUS_IVAR_DEVICE_TYPE:
27986227Stmm		*result = (uintptr_t)dinfo->ndi_device_type;
28086227Stmm		break;
28186227Stmm	case NEXUS_IVAR_MODEL:
28286227Stmm		*result = (uintptr_t)dinfo->ndi_model;
28386227Stmm		break;
28486227Stmm	case NEXUS_IVAR_REG:
28586227Stmm		*result = (uintptr_t)dinfo->ndi_reg;
28686227Stmm		break;
28786227Stmm	case NEXUS_IVAR_NREG:
28886227Stmm		*result = dinfo->ndi_nreg;
28986227Stmm		break;
29086227Stmm	case NEXUS_IVAR_INTERRUPTS:
29186227Stmm		*result = (uintptr_t)dinfo->ndi_interrupts;
29286227Stmm		break;
29386227Stmm	case NEXUS_IVAR_NINTERRUPTS:
29486227Stmm		*result = dinfo->ndi_ninterrupts;
29586227Stmm		break;
29686227Stmm	case NEXUS_IVAR_BUSTAG:
29786227Stmm		*result = (uintptr_t)dinfo->ndi_bustag;
29886227Stmm		break;
29986227Stmm	case NEXUS_IVAR_DMATAG:
30086227Stmm		*result = (uintptr_t)dinfo->ndi_dmatag;
30186227Stmm		break;
30286227Stmm	default:
30386227Stmm		return (ENOENT);
30486227Stmm	}
30586227Stmm	return 0;
30686227Stmm}
30786227Stmm
30886227Stmmstatic int
30986227Stmmnexus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
31086227Stmm{
31186227Stmm	struct nexus_devinfo *dinfo;
31286227Stmm
31386227Stmm	if ((dinfo = device_get_ivars(child)) == 0)
31486227Stmm		return (ENOENT);
31586227Stmm
31686227Stmm	switch (which) {
31786227Stmm	case NEXUS_IVAR_NODE:
31886227Stmm	case NEXUS_IVAR_NAME:
31986227Stmm	case NEXUS_IVAR_DEVICE_TYPE:
32086227Stmm	case NEXUS_IVAR_MODEL:
32186227Stmm	case NEXUS_IVAR_REG:
32286227Stmm	case NEXUS_IVAR_NREG:
32386227Stmm	case NEXUS_IVAR_INTERRUPTS:
32486227Stmm	case NEXUS_IVAR_NINTERRUPTS:
32586227Stmm	case NEXUS_IVAR_BUSTAG:
32686227Stmm	case NEXUS_IVAR_DMATAG:
32786227Stmm		return (EINVAL);
32886227Stmm	default:
32986227Stmm		return (ENOENT);
33086227Stmm	}
33186227Stmm	return 0;
33286227Stmm}
33386227Stmm
33486227Stmmstatic int
33586227Stmmnexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
33686227Stmm    driver_intr_t *intr, void *arg, void **cookiep)
33786227Stmm{
33886227Stmm	int error;
33986227Stmm
34086227Stmm	if (res == NULL)
34186227Stmm		panic("nexus_setup_intr: NULL interrupt resource!");
34286227Stmm
34386227Stmm	if ((res->r_flags & RF_SHAREABLE) == 0)
34486227Stmm		flags |= INTR_EXCL;
34586227Stmm
34686227Stmm	/*
34786227Stmm	 * We depend here on rman_activate_resource() being idempotent.
34886227Stmm	 */
34986227Stmm	error = rman_activate_resource(res);
35086227Stmm	if (error)
35186227Stmm		return (error);
35286227Stmm
35386227Stmm	error = inthand_add(device_get_nameunit(child), res->r_start,
35486227Stmm	    intr, arg, flags, cookiep);
35586227Stmm
35686227Stmm	return (error);
35786227Stmm}
35886227Stmm
35986227Stmmstatic int
36086227Stmmnexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
36186227Stmm{
36286227Stmm	inthand_remove(r->r_start, ih);
36386227Stmm	return (0);
36486227Stmm}
36586227Stmm
36686227Stmm/*
36786227Stmm * Allocate resources at the behalf of a child. This only handles interrupts,
36886227Stmm * since i/o resources are usually set up by the firmware, and thus need not
36986227Stmm * be handled here.
37086227Stmm */
37186227Stmmstatic struct resource *
37286227Stmmnexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
37386227Stmm    u_long start, u_long end, u_long count, u_int flags)
37486227Stmm{
37586227Stmm	struct	resource *rv;
37686227Stmm	struct	rman *rm;
37786227Stmm	int needactivate = flags & RF_ACTIVE;
37886227Stmm
37986227Stmm	flags &= ~RF_ACTIVE;
38086227Stmm
38186227Stmm	switch (type) {
38286227Stmm	case SYS_RES_IRQ:
38386227Stmm		rm = &intr_rman;
38486227Stmm		break;
38586227Stmm	default:
38686227Stmm		return (NULL);
38786227Stmm	}
38886227Stmm
38986227Stmm	rv = rman_reserve_resource(rm, start, end, count, flags, child);
39086227Stmm	if (rv == NULL)
39186227Stmm		return (NULL);
39286227Stmm	/* XXX: no bus_space_tag/bus_handle yet... */
39386227Stmm
39486227Stmm	if (needactivate) {
39586227Stmm		if (bus_activate_resource(child, type, *rid, rv)) {
39686227Stmm			rman_release_resource(rv);
39786227Stmm			return (NULL);
39886227Stmm		}
39986227Stmm	}
40086227Stmm
40186227Stmm	return (rv);
40286227Stmm}
40386227Stmm
40486227Stmmstatic int
40586227Stmmnexus_activate_resource(device_t bus, device_t child, int type, int rid,
40686227Stmm    struct resource *r)
40786227Stmm{
40886227Stmm
40986227Stmm	/* Not much to be done yet... */
41086227Stmm	return (rman_activate_resource(r));
41186227Stmm}
41286227Stmm
41386227Stmmstatic int
41486227Stmmnexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
41586227Stmm    struct resource *r)
41686227Stmm{
41786227Stmm
41886227Stmm	/* Not much to be done yet... */
41986227Stmm	return (rman_deactivate_resource(r));
42086227Stmm}
42186227Stmm
42286227Stmmstatic int
42386227Stmmnexus_release_resource(device_t bus, device_t child, int type, int rid,
42486227Stmm		       struct resource *r)
42586227Stmm{
42686227Stmm	int error;
42786227Stmm
42886227Stmm	if (rman_get_flags(r) & RF_ACTIVE) {
42986227Stmm		error = bus_deactivate_resource(child, type, rid, r);
43086227Stmm		if (error)
43186227Stmm			return error;
43286227Stmm	}
43386227Stmm	return (rman_release_resource(r));
43486227Stmm}
435