xref: /freebsd/sys/dev/ofw/ofw_cpu.c (revision 5d840968f84c3ff9d546e7316e25f16b4ea3fbfe)
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/cdefs.h>
325cead939SRui Paulo __FBSDID("$FreeBSD$");
335cead939SRui Paulo 
345cead939SRui Paulo #include <sys/param.h>
355cead939SRui Paulo #include <sys/systm.h>
365cead939SRui Paulo #include <sys/kernel.h>
375cead939SRui Paulo #include <sys/module.h>
385cead939SRui Paulo #include <sys/malloc.h>
395cead939SRui Paulo #include <sys/bus.h>
405cead939SRui Paulo #include <sys/cpu.h>
415cead939SRui Paulo #include <machine/bus.h>
425cead939SRui Paulo 
435cead939SRui Paulo #include <dev/ofw/openfirm.h>
445cead939SRui Paulo #include <dev/ofw/ofw_bus.h>
455cead939SRui Paulo #include <dev/ofw/ofw_bus_subr.h>
4616a1c0a8SAndrew Turner #include <dev/ofw/ofw_cpu.h>
475cead939SRui Paulo 
485cead939SRui Paulo static int	ofw_cpulist_probe(device_t);
495cead939SRui Paulo static int	ofw_cpulist_attach(device_t);
505cead939SRui Paulo static const struct ofw_bus_devinfo *ofw_cpulist_get_devinfo(device_t dev,
515cead939SRui Paulo     device_t child);
525cead939SRui Paulo 
535cead939SRui Paulo static MALLOC_DEFINE(M_OFWCPU, "ofwcpu", "OFW CPU device information");
545cead939SRui Paulo 
555b86b0eaSAndrew Turner struct ofw_cpulist_softc {
565b86b0eaSAndrew Turner 	pcell_t	 sc_addr_cells;
575b86b0eaSAndrew Turner };
585b86b0eaSAndrew Turner 
595cead939SRui Paulo static device_method_t ofw_cpulist_methods[] = {
605cead939SRui Paulo 	/* Device interface */
615cead939SRui Paulo 	DEVMETHOD(device_probe,		ofw_cpulist_probe),
625cead939SRui Paulo 	DEVMETHOD(device_attach,	ofw_cpulist_attach),
635cead939SRui Paulo 
645cead939SRui Paulo 	/* Bus interface */
655cead939SRui Paulo 	DEVMETHOD(bus_add_child,	bus_generic_add_child),
665cead939SRui Paulo 	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
675cead939SRui Paulo 
685cead939SRui Paulo 	/* ofw_bus interface */
695cead939SRui Paulo 	DEVMETHOD(ofw_bus_get_devinfo,	ofw_cpulist_get_devinfo),
705cead939SRui Paulo 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
715cead939SRui Paulo 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
725cead939SRui Paulo 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
735cead939SRui Paulo 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
745cead939SRui Paulo 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
755cead939SRui Paulo 
765cead939SRui Paulo 	DEVMETHOD_END
775cead939SRui Paulo };
785cead939SRui Paulo 
795cead939SRui Paulo static driver_t ofw_cpulist_driver = {
805cead939SRui Paulo 	"cpulist",
815cead939SRui Paulo 	ofw_cpulist_methods,
825b86b0eaSAndrew Turner 	sizeof(struct ofw_cpulist_softc)
835cead939SRui Paulo };
845cead939SRui Paulo 
855cead939SRui Paulo static devclass_t ofw_cpulist_devclass;
865cead939SRui Paulo 
875cead939SRui Paulo DRIVER_MODULE(ofw_cpulist, ofwbus, ofw_cpulist_driver, ofw_cpulist_devclass,
885cead939SRui Paulo     0, 0);
895cead939SRui Paulo 
905cead939SRui Paulo static int
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
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                 }
1275cead939SRui Paulo                 cdev = device_add_child(dev, NULL, -1);
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 
1385cead939SRui Paulo 	return (bus_generic_attach(dev));
1395cead939SRui Paulo }
1405cead939SRui Paulo 
1415cead939SRui Paulo static const struct ofw_bus_devinfo *
1425cead939SRui Paulo ofw_cpulist_get_devinfo(device_t dev, device_t child)
1435cead939SRui Paulo {
1445cead939SRui Paulo 	return (device_get_ivars(child));
1455cead939SRui Paulo }
1465cead939SRui Paulo 
1475cead939SRui Paulo static int	ofw_cpu_probe(device_t);
1485cead939SRui Paulo static int	ofw_cpu_attach(device_t);
1495cead939SRui Paulo static int	ofw_cpu_read_ivar(device_t dev, device_t child, int index,
1505cead939SRui Paulo     uintptr_t *result);
1515cead939SRui Paulo 
1525cead939SRui Paulo struct ofw_cpu_softc {
1535cead939SRui Paulo 	struct pcpu	*sc_cpu_pcpu;
1545cead939SRui Paulo 	uint32_t	 sc_nominal_mhz;
1555b86b0eaSAndrew Turner 	boolean_t	 sc_reg_valid;
1565b86b0eaSAndrew Turner 	pcell_t		 sc_reg[2];
1575cead939SRui Paulo };
1585cead939SRui Paulo 
1595cead939SRui Paulo static device_method_t ofw_cpu_methods[] = {
1605cead939SRui Paulo 	/* Device interface */
1615cead939SRui Paulo 	DEVMETHOD(device_probe,		ofw_cpu_probe),
1625cead939SRui Paulo 	DEVMETHOD(device_attach,	ofw_cpu_attach),
1635cead939SRui Paulo 
1645cead939SRui Paulo 	/* Bus interface */
1655cead939SRui Paulo 	DEVMETHOD(bus_add_child,	bus_generic_add_child),
1665cead939SRui Paulo 	DEVMETHOD(bus_read_ivar,	ofw_cpu_read_ivar),
1675cead939SRui Paulo 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
1685cead939SRui Paulo 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
1695cead939SRui Paulo 	DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
1705cead939SRui Paulo 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
1715cead939SRui Paulo 	DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
1725cead939SRui Paulo 
1735cead939SRui Paulo 	DEVMETHOD_END
1745cead939SRui Paulo };
1755cead939SRui Paulo 
1765cead939SRui Paulo static driver_t ofw_cpu_driver = {
1775cead939SRui Paulo 	"cpu",
1785cead939SRui Paulo 	ofw_cpu_methods,
1795cead939SRui Paulo 	sizeof(struct ofw_cpu_softc)
1805cead939SRui Paulo };
1815cead939SRui Paulo 
1825cead939SRui Paulo static devclass_t ofw_cpu_devclass;
1835cead939SRui Paulo 
1845cead939SRui Paulo DRIVER_MODULE(ofw_cpu, cpulist, ofw_cpu_driver, ofw_cpu_devclass, 0, 0);
1855cead939SRui Paulo 
1865cead939SRui Paulo static int
1875cead939SRui Paulo ofw_cpu_probe(device_t dev)
1885cead939SRui Paulo {
1895cead939SRui Paulo 	const char *type = ofw_bus_get_type(dev);
1905cead939SRui Paulo 
191cbe686f7SIan Lepore 	if (type == NULL || strcmp(type, "cpu") != 0)
1925cead939SRui Paulo 		return (ENXIO);
1935cead939SRui Paulo 
1945cead939SRui Paulo 	device_set_desc(dev, "Open Firmware CPU");
1955cead939SRui Paulo 	return (0);
1965cead939SRui Paulo }
1975cead939SRui Paulo 
1985cead939SRui Paulo static int
1995cead939SRui Paulo ofw_cpu_attach(device_t dev)
2005cead939SRui Paulo {
2015b86b0eaSAndrew Turner 	struct ofw_cpulist_softc *psc;
2025cead939SRui Paulo 	struct ofw_cpu_softc *sc;
203cbe686f7SIan Lepore 	phandle_t node;
2045b86b0eaSAndrew Turner 	pcell_t cell;
2055b86b0eaSAndrew Turner 	int rv;
2065cead939SRui Paulo 
2075cead939SRui Paulo 	sc = device_get_softc(dev);
2085b86b0eaSAndrew Turner 	psc = device_get_softc(device_get_parent(dev));
2095b86b0eaSAndrew Turner 
2105b86b0eaSAndrew Turner 	if (nitems(sc->sc_reg) < psc->sc_addr_cells) {
2115b86b0eaSAndrew Turner 		if (bootverbose)
2125b86b0eaSAndrew Turner 			device_printf(dev, "Too many address cells\n");
2135b86b0eaSAndrew Turner 		return (EINVAL);
214cbe686f7SIan Lepore 	}
2155b86b0eaSAndrew Turner 
2165b86b0eaSAndrew Turner 	node = ofw_bus_get_node(dev);
2175b86b0eaSAndrew Turner 
2185b86b0eaSAndrew Turner 	/* Read and validate the reg property for use later */
2195b86b0eaSAndrew Turner 	sc->sc_reg_valid = false;
2205b86b0eaSAndrew Turner 	rv = OF_getencprop(node, "reg", sc->sc_reg, sizeof(sc->sc_reg));
2215b86b0eaSAndrew Turner 	if (rv < 0)
2225b86b0eaSAndrew Turner 		device_printf(dev, "missing 'reg' property\n");
2235b86b0eaSAndrew Turner 	else if ((rv % 4) != 0) {
2245b86b0eaSAndrew Turner 		if (bootverbose)
2255b86b0eaSAndrew Turner 			device_printf(dev, "Malformed reg property\n");
2265b86b0eaSAndrew Turner 	} else if ((rv / 4) != psc->sc_addr_cells) {
2275b86b0eaSAndrew Turner 		if (bootverbose)
2285b86b0eaSAndrew Turner 			device_printf(dev, "Invalid reg size %u\n", rv);
2295b86b0eaSAndrew Turner 	} else
2305b86b0eaSAndrew Turner 		sc->sc_reg_valid = true;
2315b86b0eaSAndrew Turner 
2325b86b0eaSAndrew Turner 	sc->sc_cpu_pcpu = pcpu_find(device_get_unit(dev));
2335b86b0eaSAndrew Turner 
234cbe686f7SIan Lepore 	if (OF_getencprop(node, "clock-frequency", &cell, sizeof(cell)) < 0) {
23509eb425aSAndrew Turner 		if (bootverbose)
23609eb425aSAndrew Turner 			device_printf(dev,
23709eb425aSAndrew Turner 			    "missing 'clock-frequency' property\n");
23809eb425aSAndrew Turner 	} else
2395cead939SRui Paulo 		sc->sc_nominal_mhz = cell / 1000000; /* convert to MHz */
2405cead939SRui Paulo 
2415cead939SRui Paulo 	bus_generic_probe(dev);
2425cead939SRui Paulo 	return (bus_generic_attach(dev));
2435cead939SRui Paulo }
2445cead939SRui Paulo 
2455cead939SRui Paulo static int
2465cead939SRui Paulo ofw_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
2475cead939SRui Paulo {
248deb77283SAndrew Turner 	struct ofw_cpulist_softc *psc;
2495cead939SRui Paulo 	struct ofw_cpu_softc *sc;
2505cead939SRui Paulo 
2515cead939SRui Paulo 	sc = device_get_softc(dev);
2525cead939SRui Paulo 
2535cead939SRui Paulo 	switch (index) {
2545cead939SRui Paulo 	case CPU_IVAR_PCPU:
2555cead939SRui Paulo 		*result = (uintptr_t)sc->sc_cpu_pcpu;
2565cead939SRui Paulo 		return (0);
2575cead939SRui Paulo 	case CPU_IVAR_NOMINAL_MHZ:
25809eb425aSAndrew Turner 		if (sc->sc_nominal_mhz > 0) {
2595cead939SRui Paulo 			*result = (uintptr_t)sc->sc_nominal_mhz;
2605cead939SRui Paulo 			return (0);
2615cead939SRui Paulo 		}
26209eb425aSAndrew Turner 		break;
263deb77283SAndrew Turner 	case CPU_IVAR_CPUID_SIZE:
264deb77283SAndrew Turner 		psc = device_get_softc(device_get_parent(dev));
265deb77283SAndrew Turner 		*result = psc->sc_addr_cells;
266deb77283SAndrew Turner 		return (0);
267deb77283SAndrew Turner 	case CPU_IVAR_CPUID:
268deb77283SAndrew Turner 		if (sc->sc_reg_valid) {
269deb77283SAndrew Turner 			*result = (uintptr_t)sc->sc_reg;
270deb77283SAndrew Turner 			return (0);
271deb77283SAndrew Turner 		}
272deb77283SAndrew Turner 		break;
27309eb425aSAndrew Turner 	}
2745cead939SRui Paulo 
2755cead939SRui Paulo 	return (ENOENT);
2765cead939SRui Paulo }
2775cead939SRui Paulo 
27816a1c0a8SAndrew Turner int
27916a1c0a8SAndrew Turner ofw_cpu_early_foreach(ofw_cpu_foreach_cb callback, boolean_t only_runnable)
28016a1c0a8SAndrew Turner {
28116a1c0a8SAndrew Turner 	phandle_t node, child;
28216a1c0a8SAndrew Turner 	pcell_t addr_cells, reg[2];
28316a1c0a8SAndrew Turner 	char status[16];
2848b21d6aeSZbigniew Bodek 	char device_type[16];
2858b21d6aeSZbigniew Bodek 	u_int id, next_id;
28616a1c0a8SAndrew Turner 	int count, rv;
28716a1c0a8SAndrew Turner 
28816a1c0a8SAndrew Turner 	count = 0;
28916a1c0a8SAndrew Turner 	id = 0;
2908b21d6aeSZbigniew Bodek 	next_id = 0;
29116a1c0a8SAndrew Turner 
29216a1c0a8SAndrew Turner 	node = OF_finddevice("/cpus");
29316a1c0a8SAndrew Turner 	if (node == -1)
29416a1c0a8SAndrew Turner 		return (-1);
29516a1c0a8SAndrew Turner 
29616a1c0a8SAndrew Turner 	/* Find the number of cells in the cpu register */
29716a1c0a8SAndrew Turner 	if (OF_getencprop(node, "#address-cells", &addr_cells,
29816a1c0a8SAndrew Turner 	    sizeof(addr_cells)) < 0)
29916a1c0a8SAndrew Turner 		return (-1);
30016a1c0a8SAndrew Turner 
3018b21d6aeSZbigniew Bodek 	for (child = OF_child(node); child != 0; child = OF_peer(child),
3028b21d6aeSZbigniew Bodek 	    id = next_id) {
3038b21d6aeSZbigniew Bodek 
3048b21d6aeSZbigniew Bodek 		/* Check if child is a CPU */
3058b21d6aeSZbigniew Bodek 		memset(device_type, 0, sizeof(device_type));
3068b21d6aeSZbigniew Bodek 		rv = OF_getprop(child, "device_type", device_type,
3078b21d6aeSZbigniew Bodek 		    sizeof(device_type) - 1);
3088b21d6aeSZbigniew Bodek 		if (rv < 0)
3098b21d6aeSZbigniew Bodek 			continue;
3108b21d6aeSZbigniew Bodek 		if (strcmp(device_type, "cpu") != 0)
3118b21d6aeSZbigniew Bodek 			continue;
3128b21d6aeSZbigniew Bodek 
3138b21d6aeSZbigniew Bodek 		/* We're processing CPU, update next_id used in the next iteration */
3148b21d6aeSZbigniew Bodek 		next_id++;
3158b21d6aeSZbigniew Bodek 
31616a1c0a8SAndrew Turner 		/*
31716a1c0a8SAndrew Turner 		 * If we are filtering by runnable then limit to only
318*5d840968SOlivier Houchard 		 * those that have been enabled, or do provide a method
319*5d840968SOlivier Houchard 		 * to enable them.
32016a1c0a8SAndrew Turner 		 */
32116a1c0a8SAndrew Turner 		if (only_runnable) {
32216a1c0a8SAndrew Turner 			status[0] = '\0';
32316a1c0a8SAndrew Turner 			OF_getprop(child, "status", status, sizeof(status));
32421ce594eSOlivier Houchard 			if (status[0] != '\0' && strcmp(status, "okay") != 0 &&
32521ce594eSOlivier Houchard 				strcmp(status, "ok") != 0 &&
32621ce594eSOlivier Houchard 				!OF_hasprop(child, "enable-method"))
32716a1c0a8SAndrew Turner 					continue;
32816a1c0a8SAndrew Turner 		}
32916a1c0a8SAndrew Turner 
33016a1c0a8SAndrew Turner 		/*
33116a1c0a8SAndrew Turner 		 * Check we have a register to identify the cpu
33216a1c0a8SAndrew Turner 		 */
33316a1c0a8SAndrew Turner 		rv = OF_getencprop(child, "reg", reg,
33416a1c0a8SAndrew Turner 		    addr_cells * sizeof(cell_t));
33516a1c0a8SAndrew Turner 		if (rv != addr_cells * sizeof(cell_t))
33616a1c0a8SAndrew Turner 			continue;
33716a1c0a8SAndrew Turner 
33816a1c0a8SAndrew Turner 		if (callback == NULL || callback(id, child, addr_cells, reg))
33916a1c0a8SAndrew Turner 			count++;
34016a1c0a8SAndrew Turner 	}
34116a1c0a8SAndrew Turner 
34216a1c0a8SAndrew Turner 	return (only_runnable ? count : id);
34316a1c0a8SAndrew Turner }
344