2 * Copyright (C) 2009 Nathan Whitehorn
3 * Copyright (C) 2015 The FreeBSD Foundation
6 * Portions of this software were developed by Andrew Turner
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/malloc.h>
41 #include <machine/bus.h>
43 #include <dev/ofw/openfirm.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/ofw/ofw_cpu.h>
48 static int ofw_cpulist_probe(device_t);
49 static int ofw_cpulist_attach(device_t);
50 static const struct ofw_bus_devinfo *ofw_cpulist_get_devinfo(device_t dev,
53 static MALLOC_DEFINE(M_OFWCPU, "ofwcpu", "OFW CPU device information");
55 struct ofw_cpulist_softc {
56 pcell_t sc_addr_cells;
59 static device_method_t ofw_cpulist_methods[] = {
60 /* Device interface */
61 DEVMETHOD(device_probe, ofw_cpulist_probe),
62 DEVMETHOD(device_attach, ofw_cpulist_attach),
65 DEVMETHOD(bus_add_child, bus_generic_add_child),
66 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
68 /* ofw_bus interface */
69 DEVMETHOD(ofw_bus_get_devinfo, ofw_cpulist_get_devinfo),
70 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
71 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
72 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
73 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
74 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
79 static driver_t ofw_cpulist_driver = {
82 sizeof(struct ofw_cpulist_softc)
85 static devclass_t ofw_cpulist_devclass;
87 DRIVER_MODULE(ofw_cpulist, ofwbus, ofw_cpulist_driver, ofw_cpulist_devclass,
91 ofw_cpulist_probe(device_t dev)
95 name = ofw_bus_get_name(dev);
97 if (name == NULL || strcmp(name, "cpus") != 0)
100 device_set_desc(dev, "Open Firmware CPU Group");
106 ofw_cpulist_attach(device_t dev)
108 struct ofw_cpulist_softc *sc;
109 phandle_t root, child;
111 struct ofw_bus_devinfo *dinfo;
113 sc = device_get_softc(dev);
114 root = ofw_bus_get_node(dev);
116 sc->sc_addr_cells = 1;
117 OF_getencprop(root, "#address-cells", &sc->sc_addr_cells,
118 sizeof(sc->sc_addr_cells));
120 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
121 dinfo = malloc(sizeof(*dinfo), M_OFWCPU, M_WAITOK | M_ZERO);
123 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
124 free(dinfo, M_OFWCPU);
127 cdev = device_add_child(dev, NULL, -1);
129 device_printf(dev, "<%s>: device_add_child failed\n",
131 ofw_bus_gen_destroy_devinfo(dinfo);
132 free(dinfo, M_OFWCPU);
135 device_set_ivars(cdev, dinfo);
138 return (bus_generic_attach(dev));
141 static const struct ofw_bus_devinfo *
142 ofw_cpulist_get_devinfo(device_t dev, device_t child)
144 return (device_get_ivars(child));
147 static int ofw_cpu_probe(device_t);
148 static int ofw_cpu_attach(device_t);
149 static int ofw_cpu_read_ivar(device_t dev, device_t child, int index,
152 struct ofw_cpu_softc {
153 struct pcpu *sc_cpu_pcpu;
154 uint32_t sc_nominal_mhz;
155 boolean_t sc_reg_valid;
159 static device_method_t ofw_cpu_methods[] = {
160 /* Device interface */
161 DEVMETHOD(device_probe, ofw_cpu_probe),
162 DEVMETHOD(device_attach, ofw_cpu_attach),
165 DEVMETHOD(bus_add_child, bus_generic_add_child),
166 DEVMETHOD(bus_read_ivar, ofw_cpu_read_ivar),
167 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
168 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
169 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
170 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
171 DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
176 static driver_t ofw_cpu_driver = {
179 sizeof(struct ofw_cpu_softc)
182 static devclass_t ofw_cpu_devclass;
184 DRIVER_MODULE(ofw_cpu, cpulist, ofw_cpu_driver, ofw_cpu_devclass, 0, 0);
187 ofw_cpu_probe(device_t dev)
189 const char *type = ofw_bus_get_type(dev);
191 if (type == NULL || strcmp(type, "cpu") != 0)
194 /* Skip SMT CPUs, which we can't reasonably represent with this code */
195 if (OF_hasprop(ofw_bus_get_node(dev), "ibm,ppc-interrupt-server#s"))
198 device_set_desc(dev, "Open Firmware CPU");
203 ofw_cpu_attach(device_t dev)
205 struct ofw_cpulist_softc *psc;
206 struct ofw_cpu_softc *sc;
211 sc = device_get_softc(dev);
212 psc = device_get_softc(device_get_parent(dev));
214 if (nitems(sc->sc_reg) < psc->sc_addr_cells) {
216 device_printf(dev, "Too many address cells\n");
220 node = ofw_bus_get_node(dev);
222 /* Read and validate the reg property for use later */
223 sc->sc_reg_valid = false;
224 rv = OF_getencprop(node, "reg", sc->sc_reg, sizeof(sc->sc_reg));
226 device_printf(dev, "missing 'reg' property\n");
227 else if ((rv % 4) != 0) {
229 device_printf(dev, "Malformed reg property\n");
230 } else if ((rv / 4) != psc->sc_addr_cells) {
232 device_printf(dev, "Invalid reg size %u\n", rv);
234 sc->sc_reg_valid = true;
236 sc->sc_cpu_pcpu = pcpu_find(device_get_unit(dev));
238 if (OF_getencprop(node, "clock-frequency", &cell, sizeof(cell)) < 0) {
241 "missing 'clock-frequency' property\n");
243 sc->sc_nominal_mhz = cell / 1000000; /* convert to MHz */
245 bus_generic_probe(dev);
246 return (bus_generic_attach(dev));
250 ofw_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
252 struct ofw_cpulist_softc *psc;
253 struct ofw_cpu_softc *sc;
255 sc = device_get_softc(dev);
259 *result = (uintptr_t)sc->sc_cpu_pcpu;
261 case CPU_IVAR_NOMINAL_MHZ:
262 if (sc->sc_nominal_mhz > 0) {
263 *result = (uintptr_t)sc->sc_nominal_mhz;
267 case CPU_IVAR_CPUID_SIZE:
268 psc = device_get_softc(device_get_parent(dev));
269 *result = psc->sc_addr_cells;
272 if (sc->sc_reg_valid) {
273 *result = (uintptr_t)sc->sc_reg;
283 ofw_cpu_early_foreach(ofw_cpu_foreach_cb callback, boolean_t only_runnable)
285 phandle_t node, child;
286 pcell_t addr_cells, reg[2];
288 char device_type[16];
296 node = OF_finddevice("/cpus");
300 /* Find the number of cells in the cpu register */
301 if (OF_getencprop(node, "#address-cells", &addr_cells,
302 sizeof(addr_cells)) < 0)
305 for (child = OF_child(node); child != 0; child = OF_peer(child),
308 /* Check if child is a CPU */
309 memset(device_type, 0, sizeof(device_type));
310 rv = OF_getprop(child, "device_type", device_type,
311 sizeof(device_type) - 1);
314 if (strcmp(device_type, "cpu") != 0)
317 /* We're processing CPU, update next_id used in the next iteration */
321 * If we are filtering by runnable then limit to only
322 * those that have been enabled, or do provide a method
327 OF_getprop(child, "status", status, sizeof(status));
328 if (status[0] != '\0' && strcmp(status, "okay") != 0 &&
329 strcmp(status, "ok") != 0 &&
330 !OF_hasprop(child, "enable-method"))
335 * Check we have a register to identify the cpu
337 rv = OF_getencprop(child, "reg", reg,
338 addr_cells * sizeof(cell_t));
339 if (rv != addr_cells * sizeof(cell_t))
342 if (callback == NULL || callback(id, child, addr_cells, reg))
346 return (only_runnable ? count : id);