Home | History | Annotate | Line # | Download | only in sparc
      1 /*	$NetBSD: cpuunit.c,v 1.19 2025/10/04 16:10:05 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Autoconfiguration support for Sun4d "cpu units".
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: cpuunit.c,v 1.19 2025/10/04 16:10:05 thorpej Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/malloc.h>
     41 #include <sys/kmem.h>
     42 #include <sys/systm.h>
     43 #include <sys/device.h>
     44 
     45 #include <machine/autoconf.h>
     46 #include <sys/bus.h>
     47 
     48 #include <sparc/sparc/cpuunitvar.h>
     49 
     50 struct cpuunit_softc {
     51 	int sc_node;				/* our OBP node */
     52 
     53 	bus_space_tag_t sc_st;			/* ours */
     54 	bus_space_tag_t sc_bustag;		/* passed on to children */
     55 
     56 	int sc_device_id;			/* device-id */
     57 	int sc_board;				/* board number */
     58 };
     59 
     60 static int cpuunit_match(device_t, cfdata_t, void *);
     61 static void cpuunit_attach(device_t, device_t, void *);
     62 
     63 CFATTACH_DECL_NEW(cpuunit, sizeof(struct cpuunit_softc),
     64     cpuunit_match, cpuunit_attach, NULL, NULL);
     65 
     66 static int cpuunit_print(void *, const char *);
     67 
     68 static int cpuunit_setup_attach_args(struct cpuunit_softc *, bus_space_tag_t,
     69     int, struct cpuunit_attach_args *);
     70 static void cpuunit_destroy_attach_args(struct cpuunit_attach_args *);
     71 
     72 static int
     73 cpuunit_match(device_t parent, cfdata_t cf, void *aux)
     74 {
     75 	struct mainbus_attach_args *ma = aux;
     76 
     77 	if (strcmp(ma->ma_name, "cpu-unit") == 0)
     78 		return (1);
     79 
     80 	return (0);
     81 }
     82 
     83 static void
     84 cpuunit_attach(device_t parent, device_t self, void *aux)
     85 {
     86 	struct cpuunit_softc *sc = device_private(self);
     87 	struct mainbus_attach_args *ma = aux;
     88 	devhandle_t selfh = device_handle(self);
     89 	int node, error;
     90 	bus_space_tag_t sbt;
     91 
     92 	sc->sc_node = ma->ma_node;
     93 	sc->sc_st = ma->ma_bustag;
     94 
     95 	sc->sc_device_id = prom_getpropint(sc->sc_node, "device-id", -1);
     96 	sc->sc_board = prom_getpropint(sc->sc_node, "board#", -1);
     97 
     98 	printf(": board #%d, ID %d\n", sc->sc_board, sc->sc_device_id);
     99 
    100 	/*
    101 	 * Initialize the bus space tag we pass on to our children.
    102 	 */
    103 	sbt = sc->sc_bustag = kmem_alloc(sizeof(*sbt), KM_SLEEP);
    104 	memcpy(sbt, sc->sc_st, sizeof(*sbt));
    105 	sbt->cookie = sc;
    106 	sbt->parent = sc->sc_st;
    107 	sbt->nranges = 0;
    108 	sbt->ranges = NULL;
    109 
    110 	/*
    111 	 * Collect address translations from the OBP.
    112 	 */
    113 	error = prom_getprop(sc->sc_node, "ranges",
    114 	    sizeof(struct openprom_range), &sbt->nranges, &sbt->ranges);
    115 	if (error) {
    116 		printf("%s: error %d getting \"ranges\" property\n",
    117 		    device_xname(self), error);
    118 		panic("cpuunit_attach");
    119 	}
    120 
    121 	/* Attach the CPU (and possibly bootbus) child nodes. */
    122 	for (node = prom_firstchild(sc->sc_node); node != 0;
    123 	     node = prom_nextsibling(node)) {
    124 		struct cpuunit_attach_args cpua;
    125 
    126 		if (cpuunit_setup_attach_args(sc, sbt, node, &cpua))
    127 			panic("cpuunit_attach: failed to set up attach args");
    128 
    129 		(void) config_found(self, &cpua, cpuunit_print,
    130 		    CFARGS(.devhandle = prom_node_to_devhandle(selfh, node)));
    131 
    132 		cpuunit_destroy_attach_args(&cpua);
    133 	}
    134 }
    135 
    136 static int
    137 cpuunit_print(void *aux, const char *pnp)
    138 {
    139 	struct cpuunit_attach_args *cpua = aux;
    140 
    141 	if (pnp)
    142 		aprint_normal("%s at %s", cpua->cpua_name, pnp);
    143 
    144 	return (UNCONF);
    145 }
    146 
    147 static int
    148 cpuunit_setup_attach_args(struct cpuunit_softc *sc, bus_space_tag_t bustag,
    149     int node, struct cpuunit_attach_args *cpua)
    150 {
    151 	int n, error;
    152 
    153 	memset(cpua, 0, sizeof(*cpua));
    154 
    155 	error = prom_getprop(node, "name", 1, &n, &cpua->cpua_name);
    156 	if (error)
    157 		return (error);
    158 	cpua->cpua_name[n] = '\0';
    159 
    160 	error = prom_getprop(node, "device_type", 1, &n,
    161 	    &cpua->cpua_type);
    162 	if (error) {
    163 		free(cpua->cpua_name, M_DEVBUF);
    164 		return (error);
    165 	}
    166 
    167 	cpua->cpua_bustag = bustag;
    168 	cpua->cpua_node = node;
    169 	cpua->cpua_device_id = sc->sc_device_id;
    170 
    171 	return (0);
    172 }
    173 
    174 static void
    175 cpuunit_destroy_attach_args(struct cpuunit_attach_args *cpua)
    176 {
    177 
    178 	if (cpua->cpua_name != NULL)
    179 		free(cpua->cpua_name, M_DEVBUF);
    180 
    181 	if (cpua->cpua_type != NULL)
    182 		free(cpua->cpua_type, M_DEVBUF);
    183 }
    184