xref: /freebsd/sys/dev/ofw/ofw_cpu.c (revision ac9de183f37006fc2089757779d6d5065a530d5b)
15cead939SRui Paulo /*-
25cead939SRui Paulo  * Copyright (C) 2009 Nathan Whitehorn
309eb425aSAndrew Turner  * Copyright (C) 2015 The FreeBSD Foundation
45cead939SRui Paulo  * All rights reserved.
55cead939SRui Paulo  *
609eb425aSAndrew Turner  * Portions of this software were developed by Andrew Turner
709eb425aSAndrew Turner  * under sponsorship from the FreeBSD Foundation.
809eb425aSAndrew Turner  *
95cead939SRui Paulo  * Redistribution and use in source and binary forms, with or without
105cead939SRui Paulo  * modification, are permitted provided that the following conditions
115cead939SRui Paulo  * are met:
125cead939SRui Paulo  * 1. Redistributions of source code must retain the above copyright
135cead939SRui Paulo  *    notice, this list of conditions and the following disclaimer.
145cead939SRui Paulo  * 2. Redistributions in binary form must reproduce the above copyright
155cead939SRui Paulo  *    notice, this list of conditions and the following disclaimer in the
165cead939SRui Paulo  *    documentation and/or other materials provided with the distribution.
175cead939SRui Paulo  *
1855629a87SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1955629a87SEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2055629a87SEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2155629a87SEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2255629a87SEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2355629a87SEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2455629a87SEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2555629a87SEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2655629a87SEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2755629a87SEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2855629a87SEd Maste  * SUCH DAMAGE.
295cead939SRui Paulo  */
305cead939SRui Paulo 
315cead939SRui Paulo #include <sys/param.h>
325cead939SRui Paulo #include <sys/systm.h>
335cead939SRui Paulo #include <sys/kernel.h>
345cead939SRui Paulo #include <sys/module.h>
355cead939SRui Paulo #include <sys/malloc.h>
365cead939SRui Paulo #include <sys/bus.h>
375cead939SRui Paulo #include <sys/cpu.h>
385cead939SRui Paulo #include <machine/bus.h>
395cead939SRui Paulo 
405cead939SRui Paulo #include <dev/ofw/openfirm.h>
415cead939SRui Paulo #include <dev/ofw/ofw_bus.h>
425cead939SRui Paulo #include <dev/ofw/ofw_bus_subr.h>
4316a1c0a8SAndrew Turner #include <dev/ofw/ofw_cpu.h>
445cead939SRui Paulo 
45aa766e2aSMitchell Horne #if defined(__arm__) || defined(__arm64__) || defined(__riscv)
46be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
474ea0c3f0SMitchell Horne #define	HAS_CLK
48bc9ba24fSEmmanuel Vadot #endif
49bc9ba24fSEmmanuel Vadot 
505cead939SRui Paulo static int	ofw_cpulist_probe(device_t);
515cead939SRui Paulo static int	ofw_cpulist_attach(device_t);
525cead939SRui Paulo static const struct ofw_bus_devinfo *ofw_cpulist_get_devinfo(device_t dev,
535cead939SRui Paulo     device_t child);
545cead939SRui Paulo 
555cead939SRui Paulo static MALLOC_DEFINE(M_OFWCPU, "ofwcpu", "OFW CPU device information");
565cead939SRui Paulo 
575b86b0eaSAndrew Turner struct ofw_cpulist_softc {
585b86b0eaSAndrew Turner 	pcell_t	 sc_addr_cells;
595b86b0eaSAndrew Turner };
605b86b0eaSAndrew Turner 
615cead939SRui Paulo static device_method_t ofw_cpulist_methods[] = {
625cead939SRui Paulo 	/* Device interface */
635cead939SRui Paulo 	DEVMETHOD(device_probe,		ofw_cpulist_probe),
645cead939SRui Paulo 	DEVMETHOD(device_attach,	ofw_cpulist_attach),
655cead939SRui Paulo 
665cead939SRui Paulo 	/* Bus interface */
675cead939SRui Paulo 	DEVMETHOD(bus_add_child,	bus_generic_add_child),
68ddfc9c4cSWarner Losh 	DEVMETHOD(bus_child_pnpinfo,	ofw_bus_gen_child_pnpinfo),
697b5d62bbSTakanori Watanabe 	DEVMETHOD(bus_get_device_path,  ofw_bus_gen_get_device_path),
705cead939SRui Paulo 
715cead939SRui Paulo 	/* ofw_bus interface */
725cead939SRui Paulo 	DEVMETHOD(ofw_bus_get_devinfo,	ofw_cpulist_get_devinfo),
735cead939SRui Paulo 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
745cead939SRui Paulo 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
755cead939SRui Paulo 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
765cead939SRui Paulo 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
775cead939SRui Paulo 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
785cead939SRui Paulo 
795cead939SRui Paulo 	DEVMETHOD_END
805cead939SRui Paulo };
815cead939SRui Paulo 
825cead939SRui Paulo static driver_t ofw_cpulist_driver = {
835cead939SRui Paulo 	"cpulist",
845cead939SRui Paulo 	ofw_cpulist_methods,
855b86b0eaSAndrew Turner 	sizeof(struct ofw_cpulist_softc)
865cead939SRui Paulo };
875cead939SRui Paulo 
8803f6459cSJohn Baldwin DRIVER_MODULE(ofw_cpulist, ofwbus, ofw_cpulist_driver, 0, 0);
895cead939SRui Paulo 
905cead939SRui Paulo static int
ofw_cpulist_probe(device_t dev)915cead939SRui Paulo ofw_cpulist_probe(device_t dev)
925cead939SRui Paulo {
935cead939SRui Paulo 	const char *name;
945cead939SRui Paulo 
955cead939SRui Paulo 	name = ofw_bus_get_name(dev);
965cead939SRui Paulo 
975cead939SRui Paulo 	if (name == NULL || strcmp(name, "cpus") != 0)
985cead939SRui Paulo 		return (ENXIO);
995cead939SRui Paulo 
1005cead939SRui Paulo 	device_set_desc(dev, "Open Firmware CPU Group");
1015cead939SRui Paulo 
1025cead939SRui Paulo 	return (0);
1035cead939SRui Paulo }
1045cead939SRui Paulo 
1055cead939SRui Paulo static int
ofw_cpulist_attach(device_t dev)1065cead939SRui Paulo ofw_cpulist_attach(device_t dev)
1075cead939SRui Paulo {
1085b86b0eaSAndrew Turner 	struct ofw_cpulist_softc *sc;
1095cead939SRui Paulo 	phandle_t root, child;
1105cead939SRui Paulo 	device_t cdev;
1115cead939SRui Paulo 	struct ofw_bus_devinfo *dinfo;
1125cead939SRui Paulo 
1135b86b0eaSAndrew Turner 	sc = device_get_softc(dev);
1145cead939SRui Paulo 	root = ofw_bus_get_node(dev);
1155cead939SRui Paulo 
1165b86b0eaSAndrew Turner 	sc->sc_addr_cells = 1;
1175b86b0eaSAndrew Turner 	OF_getencprop(root, "#address-cells", &sc->sc_addr_cells,
1185b86b0eaSAndrew Turner 	    sizeof(sc->sc_addr_cells));
1195b86b0eaSAndrew Turner 
1205cead939SRui Paulo 	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
1215cead939SRui Paulo 		dinfo = malloc(sizeof(*dinfo), M_OFWCPU, M_WAITOK | M_ZERO);
1225cead939SRui Paulo 
1235cead939SRui Paulo 		if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
1245cead939SRui Paulo 			free(dinfo, M_OFWCPU);
1255cead939SRui Paulo 			continue;
1265cead939SRui Paulo 		}
1275b56413dSWarner Losh 		cdev = device_add_child(dev, NULL, DEVICE_UNIT_ANY);
1285cead939SRui Paulo 		if (cdev == NULL) {
1295cead939SRui Paulo 			device_printf(dev, "<%s>: device_add_child failed\n",
1305cead939SRui Paulo 			    dinfo->obd_name);
1315cead939SRui Paulo 			ofw_bus_gen_destroy_devinfo(dinfo);
1325cead939SRui Paulo 			free(dinfo, M_OFWCPU);
1335cead939SRui Paulo 			continue;
1345cead939SRui Paulo 		}
1355cead939SRui Paulo 		device_set_ivars(cdev, dinfo);
1365cead939SRui Paulo 	}
1375cead939SRui Paulo 
13818250ec6SJohn Baldwin 	bus_attach_children(dev);
13918250ec6SJohn Baldwin 	return (0);
1405cead939SRui Paulo }
1415cead939SRui Paulo 
1425cead939SRui Paulo static const struct ofw_bus_devinfo *
ofw_cpulist_get_devinfo(device_t dev,device_t child)1435cead939SRui Paulo ofw_cpulist_get_devinfo(device_t dev, device_t child)
1445cead939SRui Paulo {
1455cead939SRui Paulo 	return (device_get_ivars(child));
1465cead939SRui Paulo }
1475cead939SRui Paulo 
1485cead939SRui Paulo static int	ofw_cpu_probe(device_t);
1495cead939SRui Paulo static int	ofw_cpu_attach(device_t);
1505cead939SRui Paulo static int	ofw_cpu_read_ivar(device_t dev, device_t child, int index,
1515cead939SRui Paulo     uintptr_t *result);
1525cead939SRui Paulo 
1535cead939SRui Paulo struct ofw_cpu_softc {
1545cead939SRui Paulo 	struct pcpu	*sc_cpu_pcpu;
1555cead939SRui Paulo 	uint32_t	 sc_nominal_mhz;
156609cdb12SMitchell Horne 	bool		 sc_reg_valid;
1575b86b0eaSAndrew Turner 	pcell_t		 sc_reg[2];
1585cead939SRui Paulo };
1595cead939SRui Paulo 
1605cead939SRui Paulo static device_method_t ofw_cpu_methods[] = {
1615cead939SRui Paulo 	/* Device interface */
1625cead939SRui Paulo 	DEVMETHOD(device_probe,		ofw_cpu_probe),
1635cead939SRui Paulo 	DEVMETHOD(device_attach,	ofw_cpu_attach),
1645cead939SRui Paulo 
1655cead939SRui Paulo 	/* Bus interface */
1665cead939SRui Paulo 	DEVMETHOD(bus_add_child,	bus_generic_add_child),
1675cead939SRui Paulo 	DEVMETHOD(bus_read_ivar,	ofw_cpu_read_ivar),
1685cead939SRui Paulo 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
1695cead939SRui Paulo 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
1705cead939SRui Paulo 	DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
1715cead939SRui Paulo 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
1725cead939SRui Paulo 	DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
1735cead939SRui Paulo 
1745cead939SRui Paulo 	DEVMETHOD_END
1755cead939SRui Paulo };
1765cead939SRui Paulo 
1775cead939SRui Paulo static driver_t ofw_cpu_driver = {
1785cead939SRui Paulo 	"cpu",
1795cead939SRui Paulo 	ofw_cpu_methods,
1805cead939SRui Paulo 	sizeof(struct ofw_cpu_softc)
1815cead939SRui Paulo };
1825cead939SRui Paulo 
18303f6459cSJohn Baldwin DRIVER_MODULE(ofw_cpu, cpulist, ofw_cpu_driver, 0, 0);
1845cead939SRui Paulo 
185*ac9de183SMitchell Horne static bool
ofw_cpu_is_runnable(phandle_t node)186*ac9de183SMitchell Horne ofw_cpu_is_runnable(phandle_t node)
187*ac9de183SMitchell Horne {
188*ac9de183SMitchell Horne 	/*
189*ac9de183SMitchell Horne 	 * Per the DeviceTree Specification, a cpu node (under /cpus) that
190*ac9de183SMitchell Horne 	 * has 'status = disabled' indicates that "the CPU is in a quiescent
191*ac9de183SMitchell Horne 	 * state."
192*ac9de183SMitchell Horne 	 *
193*ac9de183SMitchell Horne 	 * A quiescent CPU that specifies an "enable-method", such as
194*ac9de183SMitchell Horne 	 * "spin-table", can still be used by the kernel.
195*ac9de183SMitchell Horne 	 *
196*ac9de183SMitchell Horne 	 * Lacking this, any CPU marked "disabled" or other non-okay status
197*ac9de183SMitchell Horne 	 * should be excluded from the kernel's view.
198*ac9de183SMitchell Horne 	 */
199*ac9de183SMitchell Horne 	return (ofw_bus_node_status_okay(node) ||
200*ac9de183SMitchell Horne 	    OF_hasprop(node, "enable-method"));
201*ac9de183SMitchell Horne }
202*ac9de183SMitchell Horne 
2035cead939SRui Paulo static int
ofw_cpu_probe(device_t dev)2045cead939SRui Paulo ofw_cpu_probe(device_t dev)
2055cead939SRui Paulo {
2065cead939SRui Paulo 	const char *type = ofw_bus_get_type(dev);
2075cead939SRui Paulo 
208cbe686f7SIan Lepore 	if (type == NULL || strcmp(type, "cpu") != 0)
2095cead939SRui Paulo 		return (ENXIO);
2105cead939SRui Paulo 
211*ac9de183SMitchell Horne 	if (!ofw_cpu_is_runnable(ofw_bus_get_node(dev)))
212*ac9de183SMitchell Horne 		return (ENXIO);
213*ac9de183SMitchell Horne 
2145cead939SRui Paulo 	device_set_desc(dev, "Open Firmware CPU");
21508637d5dSMitchell Horne 	if (!bootverbose && device_get_unit(dev) != 0) {
21608637d5dSMitchell Horne 		device_quiet(dev);
21708637d5dSMitchell Horne 		device_quiet_children(dev);
21808637d5dSMitchell Horne 	}
21908637d5dSMitchell Horne 
2205cead939SRui Paulo 	return (0);
2215cead939SRui Paulo }
2225cead939SRui Paulo 
2235cead939SRui Paulo static int
get_freq_from_clk(device_t dev,struct ofw_cpu_softc * sc)2244ea0c3f0SMitchell Horne get_freq_from_clk(device_t dev, struct ofw_cpu_softc *sc)
2254ea0c3f0SMitchell Horne {
2264ea0c3f0SMitchell Horne #ifdef HAS_CLK
2274ea0c3f0SMitchell Horne 	clk_t cpuclk;
2284ea0c3f0SMitchell Horne 	uint64_t freq;
2294ea0c3f0SMitchell Horne 	int rv;
2304ea0c3f0SMitchell Horne 
2314ea0c3f0SMitchell Horne 	rv = clk_get_by_ofw_index(dev, 0, 0, &cpuclk);
2324ea0c3f0SMitchell Horne 	if (rv == 0) {
2334ea0c3f0SMitchell Horne 		rv = clk_get_freq(cpuclk, &freq);
2344ea0c3f0SMitchell Horne 		if (rv != 0 && bootverbose)
2354ea0c3f0SMitchell Horne 			device_printf(dev,
2364ea0c3f0SMitchell Horne 			    "Cannot get freq of property clocks\n");
2374ea0c3f0SMitchell Horne 		else
2384ea0c3f0SMitchell Horne 			sc->sc_nominal_mhz = freq / 1000000;
2394ea0c3f0SMitchell Horne 	}
2404ea0c3f0SMitchell Horne 
2414ea0c3f0SMitchell Horne 	return (rv);
2424ea0c3f0SMitchell Horne #else
2434ea0c3f0SMitchell Horne 	return (ENODEV);
2444ea0c3f0SMitchell Horne #endif
2454ea0c3f0SMitchell Horne }
2464ea0c3f0SMitchell Horne 
2474ea0c3f0SMitchell Horne static int
ofw_cpu_attach(device_t dev)2485cead939SRui Paulo ofw_cpu_attach(device_t dev)
2495cead939SRui Paulo {
2505b86b0eaSAndrew Turner 	struct ofw_cpulist_softc *psc;
2515cead939SRui Paulo 	struct ofw_cpu_softc *sc;
252cbe686f7SIan Lepore 	phandle_t node;
2535b86b0eaSAndrew Turner 	pcell_t cell;
2545b86b0eaSAndrew Turner 	int rv;
2555cead939SRui Paulo 
2565cead939SRui Paulo 	sc = device_get_softc(dev);
2575b86b0eaSAndrew Turner 	psc = device_get_softc(device_get_parent(dev));
2585b86b0eaSAndrew Turner 
2595b86b0eaSAndrew Turner 	if (nitems(sc->sc_reg) < psc->sc_addr_cells) {
2605b86b0eaSAndrew Turner 		if (bootverbose)
2615b86b0eaSAndrew Turner 			device_printf(dev, "Too many address cells\n");
2625b86b0eaSAndrew Turner 		return (EINVAL);
263cbe686f7SIan Lepore 	}
2645b86b0eaSAndrew Turner 
2655b86b0eaSAndrew Turner 	node = ofw_bus_get_node(dev);
2665b86b0eaSAndrew Turner 
2675b86b0eaSAndrew Turner 	/* Read and validate the reg property for use later */
2685b86b0eaSAndrew Turner 	sc->sc_reg_valid = false;
2695b86b0eaSAndrew Turner 	rv = OF_getencprop(node, "reg", sc->sc_reg, sizeof(sc->sc_reg));
2705b86b0eaSAndrew Turner 	if (rv < 0)
2715b86b0eaSAndrew Turner 		device_printf(dev, "missing 'reg' property\n");
2725b86b0eaSAndrew Turner 	else if ((rv % 4) != 0) {
2735b86b0eaSAndrew Turner 		if (bootverbose)
2745b86b0eaSAndrew Turner 			device_printf(dev, "Malformed reg property\n");
2755b86b0eaSAndrew Turner 	} else if ((rv / 4) != psc->sc_addr_cells) {
2765b86b0eaSAndrew Turner 		if (bootverbose)
2775b86b0eaSAndrew Turner 			device_printf(dev, "Invalid reg size %u\n", rv);
2785b86b0eaSAndrew Turner 	} else
2795b86b0eaSAndrew Turner 		sc->sc_reg_valid = true;
2805b86b0eaSAndrew Turner 
281d23d5d73SJustin Hibbits #ifdef __powerpc__
282d23d5d73SJustin Hibbits 	/*
283d23d5d73SJustin Hibbits 	 * On powerpc, "interrupt-servers" denotes a SMT CPU.  Look for any
284d23d5d73SJustin Hibbits 	 * thread on this CPU, and assign that.
285d23d5d73SJustin Hibbits 	 */
286d23d5d73SJustin Hibbits 	if (OF_hasprop(node, "ibm,ppc-interrupt-server#s")) {
287d23d5d73SJustin Hibbits 		struct cpuref cpuref;
288d23d5d73SJustin Hibbits 		cell_t *servers;
289d23d5d73SJustin Hibbits 		int i, nservers, rv;
290d23d5d73SJustin Hibbits 
291d23d5d73SJustin Hibbits 		if ((nservers = OF_getencprop_alloc(node,
292d23d5d73SJustin Hibbits 		    "ibm,ppc-interrupt-server#s", (void **)&servers)) < 0)
293d23d5d73SJustin Hibbits 			return (ENXIO);
294d23d5d73SJustin Hibbits 		nservers /= sizeof(cell_t);
295d23d5d73SJustin Hibbits 		for (i = 0; i < nservers; i++) {
296d23d5d73SJustin Hibbits 			for (rv = platform_smp_first_cpu(&cpuref); rv == 0;
297d23d5d73SJustin Hibbits 			    rv = platform_smp_next_cpu(&cpuref)) {
298d23d5d73SJustin Hibbits 				if (cpuref.cr_hwref == servers[i]) {
299d23d5d73SJustin Hibbits 					sc->sc_cpu_pcpu =
300d23d5d73SJustin Hibbits 					    pcpu_find(cpuref.cr_cpuid);
301d23d5d73SJustin Hibbits 					if (sc->sc_cpu_pcpu == NULL) {
302d23d5d73SJustin Hibbits 						OF_prop_free(servers);
303d23d5d73SJustin Hibbits 						return (ENXIO);
304d23d5d73SJustin Hibbits 					}
305d23d5d73SJustin Hibbits 					break;
306d23d5d73SJustin Hibbits 				}
307d23d5d73SJustin Hibbits 			}
308d23d5d73SJustin Hibbits 			if (rv != ENOENT)
309d23d5d73SJustin Hibbits 				break;
310d23d5d73SJustin Hibbits 		}
311d23d5d73SJustin Hibbits 		OF_prop_free(servers);
312d23d5d73SJustin Hibbits 		if (sc->sc_cpu_pcpu == NULL) {
313d23d5d73SJustin Hibbits 			device_printf(dev, "No CPU found for this device.\n");
314d23d5d73SJustin Hibbits 			return (ENXIO);
315d23d5d73SJustin Hibbits 		}
316d23d5d73SJustin Hibbits 	} else
317d23d5d73SJustin Hibbits #endif
3185b86b0eaSAndrew Turner 	sc->sc_cpu_pcpu = pcpu_find(device_get_unit(dev));
3195b86b0eaSAndrew Turner 
320cbe686f7SIan Lepore 	if (OF_getencprop(node, "clock-frequency", &cell, sizeof(cell)) < 0) {
3214ea0c3f0SMitchell Horne 		if (get_freq_from_clk(dev, sc) != 0) {
32209eb425aSAndrew Turner 			if (bootverbose)
32309eb425aSAndrew Turner 				device_printf(dev,
32409eb425aSAndrew Turner 				    "missing 'clock-frequency' property\n");
325bc9ba24fSEmmanuel Vadot 		}
32609eb425aSAndrew Turner 	} else
3275cead939SRui Paulo 		sc->sc_nominal_mhz = cell / 1000000; /* convert to MHz */
3285cead939SRui Paulo 
329bc9ba24fSEmmanuel Vadot 	if (sc->sc_nominal_mhz != 0 && bootverbose)
330bc9ba24fSEmmanuel Vadot 		device_printf(dev, "Nominal frequency %dMhz\n",
331bc9ba24fSEmmanuel Vadot 		    sc->sc_nominal_mhz);
3324ea0c3f0SMitchell Horne 
333723da5d9SJohn Baldwin 	bus_identify_children(dev);
33418250ec6SJohn Baldwin 	bus_attach_children(dev);
33518250ec6SJohn Baldwin 	return (0);
3365cead939SRui Paulo }
3375cead939SRui Paulo 
3385cead939SRui Paulo static int
ofw_cpu_read_ivar(device_t dev,device_t child,int index,uintptr_t * result)3395cead939SRui Paulo ofw_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
3405cead939SRui Paulo {
341deb77283SAndrew Turner 	struct ofw_cpulist_softc *psc;
3425cead939SRui Paulo 	struct ofw_cpu_softc *sc;
3435cead939SRui Paulo 
3445cead939SRui Paulo 	sc = device_get_softc(dev);
3455cead939SRui Paulo 
3465cead939SRui Paulo 	switch (index) {
3475cead939SRui Paulo 	case CPU_IVAR_PCPU:
3485cead939SRui Paulo 		*result = (uintptr_t)sc->sc_cpu_pcpu;
3495cead939SRui Paulo 		return (0);
3505cead939SRui Paulo 	case CPU_IVAR_NOMINAL_MHZ:
35109eb425aSAndrew Turner 		if (sc->sc_nominal_mhz > 0) {
3525cead939SRui Paulo 			*result = (uintptr_t)sc->sc_nominal_mhz;
3535cead939SRui Paulo 			return (0);
3545cead939SRui Paulo 		}
35509eb425aSAndrew Turner 		break;
356deb77283SAndrew Turner 	case CPU_IVAR_CPUID_SIZE:
357deb77283SAndrew Turner 		psc = device_get_softc(device_get_parent(dev));
358deb77283SAndrew Turner 		*result = psc->sc_addr_cells;
359deb77283SAndrew Turner 		return (0);
360deb77283SAndrew Turner 	case CPU_IVAR_CPUID:
361deb77283SAndrew Turner 		if (sc->sc_reg_valid) {
362deb77283SAndrew Turner 			*result = (uintptr_t)sc->sc_reg;
363deb77283SAndrew Turner 			return (0);
364deb77283SAndrew Turner 		}
365deb77283SAndrew Turner 		break;
36609eb425aSAndrew Turner 	}
3675cead939SRui Paulo 
3685cead939SRui Paulo 	return (ENOENT);
3695cead939SRui Paulo }
3705cead939SRui Paulo 
37116a1c0a8SAndrew Turner int
ofw_cpu_early_foreach(ofw_cpu_foreach_cb callback,bool only_runnable)372609cdb12SMitchell Horne ofw_cpu_early_foreach(ofw_cpu_foreach_cb callback, bool only_runnable)
37316a1c0a8SAndrew Turner {
37416a1c0a8SAndrew Turner 	phandle_t node, child;
37516a1c0a8SAndrew Turner 	pcell_t addr_cells, reg[2];
3768b21d6aeSZbigniew Bodek 	char device_type[16];
3778b21d6aeSZbigniew Bodek 	u_int id, next_id;
37816a1c0a8SAndrew Turner 	int count, rv;
37916a1c0a8SAndrew Turner 
38016a1c0a8SAndrew Turner 	count = 0;
38116a1c0a8SAndrew Turner 	id = 0;
3828b21d6aeSZbigniew Bodek 	next_id = 0;
38316a1c0a8SAndrew Turner 
38416a1c0a8SAndrew Turner 	node = OF_finddevice("/cpus");
38516a1c0a8SAndrew Turner 	if (node == -1)
38616a1c0a8SAndrew Turner 		return (-1);
38716a1c0a8SAndrew Turner 
38816a1c0a8SAndrew Turner 	/* Find the number of cells in the cpu register */
38916a1c0a8SAndrew Turner 	if (OF_getencprop(node, "#address-cells", &addr_cells,
39016a1c0a8SAndrew Turner 	    sizeof(addr_cells)) < 0)
39116a1c0a8SAndrew Turner 		return (-1);
39216a1c0a8SAndrew Turner 
3938b21d6aeSZbigniew Bodek 	for (child = OF_child(node); child != 0; child = OF_peer(child),
3948b21d6aeSZbigniew Bodek 	    id = next_id) {
3958b21d6aeSZbigniew Bodek 		/* Check if child is a CPU */
3968b21d6aeSZbigniew Bodek 		memset(device_type, 0, sizeof(device_type));
3978b21d6aeSZbigniew Bodek 		rv = OF_getprop(child, "device_type", device_type,
3988b21d6aeSZbigniew Bodek 		    sizeof(device_type) - 1);
3998b21d6aeSZbigniew Bodek 		if (rv < 0)
4008b21d6aeSZbigniew Bodek 			continue;
4018b21d6aeSZbigniew Bodek 		if (strcmp(device_type, "cpu") != 0)
4028b21d6aeSZbigniew Bodek 			continue;
4038b21d6aeSZbigniew Bodek 
4048b21d6aeSZbigniew Bodek 		/* We're processing CPU, update next_id used in the next iteration */
4058b21d6aeSZbigniew Bodek 		next_id++;
4068b21d6aeSZbigniew Bodek 
40716a1c0a8SAndrew Turner 		/*
40816a1c0a8SAndrew Turner 		 * If we are filtering by runnable then limit to only
4095d840968SOlivier Houchard 		 * those that have been enabled, or do provide a method
4105d840968SOlivier Houchard 		 * to enable them.
41116a1c0a8SAndrew Turner 		 */
412*ac9de183SMitchell Horne 		if (only_runnable && !ofw_cpu_is_runnable(child))
41316a1c0a8SAndrew Turner 			continue;
41416a1c0a8SAndrew Turner 
41516a1c0a8SAndrew Turner 		/*
41616a1c0a8SAndrew Turner 		 * Check we have a register to identify the cpu
41716a1c0a8SAndrew Turner 		 */
41816a1c0a8SAndrew Turner 		rv = OF_getencprop(child, "reg", reg,
41916a1c0a8SAndrew Turner 		    addr_cells * sizeof(cell_t));
42016a1c0a8SAndrew Turner 		if (rv != addr_cells * sizeof(cell_t))
42116a1c0a8SAndrew Turner 			continue;
42216a1c0a8SAndrew Turner 
42316a1c0a8SAndrew Turner 		if (callback == NULL || callback(id, child, addr_cells, reg))
42416a1c0a8SAndrew Turner 			count++;
42516a1c0a8SAndrew Turner 	}
42616a1c0a8SAndrew Turner 
42716a1c0a8SAndrew Turner 	return (only_runnable ? count : id);
42816a1c0a8SAndrew Turner }
429