15cead939SRui Paulo /*- 25cead939SRui Paulo * Copyright (C) 2009 Nathan Whitehorn 35cead939SRui Paulo * All rights reserved. 45cead939SRui Paulo * 55cead939SRui Paulo * Redistribution and use in source and binary forms, with or without 65cead939SRui Paulo * modification, are permitted provided that the following conditions 75cead939SRui Paulo * are met: 85cead939SRui Paulo * 1. Redistributions of source code must retain the above copyright 95cead939SRui Paulo * notice, this list of conditions and the following disclaimer. 105cead939SRui Paulo * 2. Redistributions in binary form must reproduce the above copyright 115cead939SRui Paulo * notice, this list of conditions and the following disclaimer in the 125cead939SRui Paulo * documentation and/or other materials provided with the distribution. 135cead939SRui Paulo * 1455629a87SEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1555629a87SEd Maste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1655629a87SEd Maste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1755629a87SEd Maste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1855629a87SEd Maste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1955629a87SEd Maste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2055629a87SEd Maste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2155629a87SEd Maste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2255629a87SEd Maste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2355629a87SEd Maste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2455629a87SEd Maste * SUCH DAMAGE. 255cead939SRui Paulo */ 265cead939SRui Paulo 275cead939SRui Paulo #include <sys/cdefs.h> 285cead939SRui Paulo __FBSDID("$FreeBSD$"); 295cead939SRui Paulo 305cead939SRui Paulo #include <sys/param.h> 315cead939SRui Paulo #include <sys/systm.h> 325cead939SRui Paulo #include <sys/kernel.h> 335cead939SRui Paulo #include <sys/module.h> 345cead939SRui Paulo #include <sys/malloc.h> 355cead939SRui Paulo #include <sys/bus.h> 365cead939SRui Paulo #include <sys/cpu.h> 375cead939SRui Paulo #include <machine/bus.h> 385cead939SRui Paulo 395cead939SRui Paulo #include <dev/ofw/openfirm.h> 405cead939SRui Paulo #include <dev/ofw/ofw_bus.h> 415cead939SRui Paulo #include <dev/ofw/ofw_bus_subr.h> 425cead939SRui Paulo 435cead939SRui Paulo static int ofw_cpulist_probe(device_t); 445cead939SRui Paulo static int ofw_cpulist_attach(device_t); 455cead939SRui Paulo static const struct ofw_bus_devinfo *ofw_cpulist_get_devinfo(device_t dev, 465cead939SRui Paulo device_t child); 475cead939SRui Paulo 485cead939SRui Paulo static MALLOC_DEFINE(M_OFWCPU, "ofwcpu", "OFW CPU device information"); 495cead939SRui Paulo 505cead939SRui Paulo static device_method_t ofw_cpulist_methods[] = { 515cead939SRui Paulo /* Device interface */ 525cead939SRui Paulo DEVMETHOD(device_probe, ofw_cpulist_probe), 535cead939SRui Paulo DEVMETHOD(device_attach, ofw_cpulist_attach), 545cead939SRui Paulo 555cead939SRui Paulo /* Bus interface */ 565cead939SRui Paulo DEVMETHOD(bus_add_child, bus_generic_add_child), 575cead939SRui Paulo DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 585cead939SRui Paulo 595cead939SRui Paulo /* ofw_bus interface */ 605cead939SRui Paulo DEVMETHOD(ofw_bus_get_devinfo, ofw_cpulist_get_devinfo), 615cead939SRui Paulo DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 625cead939SRui Paulo DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 635cead939SRui Paulo DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 645cead939SRui Paulo DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 655cead939SRui Paulo DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 665cead939SRui Paulo 675cead939SRui Paulo DEVMETHOD_END 685cead939SRui Paulo }; 695cead939SRui Paulo 705cead939SRui Paulo static driver_t ofw_cpulist_driver = { 715cead939SRui Paulo "cpulist", 725cead939SRui Paulo ofw_cpulist_methods, 735cead939SRui Paulo 0 745cead939SRui Paulo }; 755cead939SRui Paulo 765cead939SRui Paulo static devclass_t ofw_cpulist_devclass; 775cead939SRui Paulo 785cead939SRui Paulo DRIVER_MODULE(ofw_cpulist, ofwbus, ofw_cpulist_driver, ofw_cpulist_devclass, 795cead939SRui Paulo 0, 0); 805cead939SRui Paulo 815cead939SRui Paulo static int 825cead939SRui Paulo ofw_cpulist_probe(device_t dev) 835cead939SRui Paulo { 845cead939SRui Paulo const char *name; 855cead939SRui Paulo 865cead939SRui Paulo name = ofw_bus_get_name(dev); 875cead939SRui Paulo 885cead939SRui Paulo if (name == NULL || strcmp(name, "cpus") != 0) 895cead939SRui Paulo return (ENXIO); 905cead939SRui Paulo 915cead939SRui Paulo device_set_desc(dev, "Open Firmware CPU Group"); 925cead939SRui Paulo 935cead939SRui Paulo return (0); 945cead939SRui Paulo } 955cead939SRui Paulo 965cead939SRui Paulo static int 975cead939SRui Paulo ofw_cpulist_attach(device_t dev) 985cead939SRui Paulo { 995cead939SRui Paulo phandle_t root, child; 1005cead939SRui Paulo device_t cdev; 1015cead939SRui Paulo struct ofw_bus_devinfo *dinfo; 1025cead939SRui Paulo 1035cead939SRui Paulo root = ofw_bus_get_node(dev); 1045cead939SRui Paulo 1055cead939SRui Paulo for (child = OF_child(root); child != 0; child = OF_peer(child)) { 1065cead939SRui Paulo dinfo = malloc(sizeof(*dinfo), M_OFWCPU, M_WAITOK | M_ZERO); 1075cead939SRui Paulo 1085cead939SRui Paulo if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) { 1095cead939SRui Paulo free(dinfo, M_OFWCPU); 1105cead939SRui Paulo continue; 1115cead939SRui Paulo } 1125cead939SRui Paulo cdev = device_add_child(dev, NULL, -1); 1135cead939SRui Paulo if (cdev == NULL) { 1145cead939SRui Paulo device_printf(dev, "<%s>: device_add_child failed\n", 1155cead939SRui Paulo dinfo->obd_name); 1165cead939SRui Paulo ofw_bus_gen_destroy_devinfo(dinfo); 1175cead939SRui Paulo free(dinfo, M_OFWCPU); 1185cead939SRui Paulo continue; 1195cead939SRui Paulo } 1205cead939SRui Paulo device_set_ivars(cdev, dinfo); 1215cead939SRui Paulo } 1225cead939SRui Paulo 1235cead939SRui Paulo return (bus_generic_attach(dev)); 1245cead939SRui Paulo } 1255cead939SRui Paulo 1265cead939SRui Paulo static const struct ofw_bus_devinfo * 1275cead939SRui Paulo ofw_cpulist_get_devinfo(device_t dev, device_t child) 1285cead939SRui Paulo { 1295cead939SRui Paulo return (device_get_ivars(child)); 1305cead939SRui Paulo } 1315cead939SRui Paulo 1325cead939SRui Paulo static int ofw_cpu_probe(device_t); 1335cead939SRui Paulo static int ofw_cpu_attach(device_t); 1345cead939SRui Paulo static int ofw_cpu_read_ivar(device_t dev, device_t child, int index, 1355cead939SRui Paulo uintptr_t *result); 1365cead939SRui Paulo 1375cead939SRui Paulo struct ofw_cpu_softc { 1385cead939SRui Paulo struct pcpu *sc_cpu_pcpu; 1395cead939SRui Paulo uint32_t sc_nominal_mhz; 1405cead939SRui Paulo }; 1415cead939SRui Paulo 1425cead939SRui Paulo static device_method_t ofw_cpu_methods[] = { 1435cead939SRui Paulo /* Device interface */ 1445cead939SRui Paulo DEVMETHOD(device_probe, ofw_cpu_probe), 1455cead939SRui Paulo DEVMETHOD(device_attach, ofw_cpu_attach), 1465cead939SRui Paulo 1475cead939SRui Paulo /* Bus interface */ 1485cead939SRui Paulo DEVMETHOD(bus_add_child, bus_generic_add_child), 1495cead939SRui Paulo DEVMETHOD(bus_read_ivar, ofw_cpu_read_ivar), 1505cead939SRui Paulo DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 1515cead939SRui Paulo DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 1525cead939SRui Paulo DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource), 1535cead939SRui Paulo DEVMETHOD(bus_release_resource, bus_generic_release_resource), 1545cead939SRui Paulo DEVMETHOD(bus_activate_resource,bus_generic_activate_resource), 1555cead939SRui Paulo 1565cead939SRui Paulo DEVMETHOD_END 1575cead939SRui Paulo }; 1585cead939SRui Paulo 1595cead939SRui Paulo static driver_t ofw_cpu_driver = { 1605cead939SRui Paulo "cpu", 1615cead939SRui Paulo ofw_cpu_methods, 1625cead939SRui Paulo sizeof(struct ofw_cpu_softc) 1635cead939SRui Paulo }; 1645cead939SRui Paulo 1655cead939SRui Paulo static devclass_t ofw_cpu_devclass; 1665cead939SRui Paulo 1675cead939SRui Paulo DRIVER_MODULE(ofw_cpu, cpulist, ofw_cpu_driver, ofw_cpu_devclass, 0, 0); 1685cead939SRui Paulo 1695cead939SRui Paulo static int 1705cead939SRui Paulo ofw_cpu_probe(device_t dev) 1715cead939SRui Paulo { 1725cead939SRui Paulo const char *type = ofw_bus_get_type(dev); 1735cead939SRui Paulo 174*cbe686f7SIan Lepore if (type == NULL || strcmp(type, "cpu") != 0) 1755cead939SRui Paulo return (ENXIO); 1765cead939SRui Paulo 1775cead939SRui Paulo device_set_desc(dev, "Open Firmware CPU"); 1785cead939SRui Paulo return (0); 1795cead939SRui Paulo } 1805cead939SRui Paulo 1815cead939SRui Paulo static int 1825cead939SRui Paulo ofw_cpu_attach(device_t dev) 1835cead939SRui Paulo { 1845cead939SRui Paulo struct ofw_cpu_softc *sc; 185*cbe686f7SIan Lepore phandle_t node; 1865cead939SRui Paulo uint32_t cell; 1875cead939SRui Paulo 1885cead939SRui Paulo sc = device_get_softc(dev); 189*cbe686f7SIan Lepore node = ofw_bus_get_node(dev); 190*cbe686f7SIan Lepore if (OF_getencprop(node, "reg", &cell, sizeof(cell)) < 0) { 191*cbe686f7SIan Lepore cell = device_get_unit(dev); 192*cbe686f7SIan Lepore device_printf(dev, "missing 'reg' property, using %u\n", cell); 193*cbe686f7SIan Lepore } 1945cead939SRui Paulo sc->sc_cpu_pcpu = pcpu_find(cell); 195*cbe686f7SIan Lepore if (OF_getencprop(node, "clock-frequency", &cell, sizeof(cell)) < 0) { 196*cbe686f7SIan Lepore device_printf(dev, "missing 'clock-frequency' property\n"); 197*cbe686f7SIan Lepore return (ENXIO); 198*cbe686f7SIan Lepore } 1995cead939SRui Paulo sc->sc_nominal_mhz = cell / 1000000; /* convert to MHz */ 2005cead939SRui Paulo 2015cead939SRui Paulo bus_generic_probe(dev); 2025cead939SRui Paulo return (bus_generic_attach(dev)); 2035cead939SRui Paulo } 2045cead939SRui Paulo 2055cead939SRui Paulo static int 2065cead939SRui Paulo ofw_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 2075cead939SRui Paulo { 2085cead939SRui Paulo struct ofw_cpu_softc *sc; 2095cead939SRui Paulo 2105cead939SRui Paulo sc = device_get_softc(dev); 2115cead939SRui Paulo 2125cead939SRui Paulo switch (index) { 2135cead939SRui Paulo case CPU_IVAR_PCPU: 2145cead939SRui Paulo *result = (uintptr_t)sc->sc_cpu_pcpu; 2155cead939SRui Paulo return (0); 2165cead939SRui Paulo case CPU_IVAR_NOMINAL_MHZ: 2175cead939SRui Paulo *result = (uintptr_t)sc->sc_nominal_mhz; 2185cead939SRui Paulo return (0); 2195cead939SRui Paulo } 2205cead939SRui Paulo 2215cead939SRui Paulo return (ENOENT); 2225cead939SRui Paulo } 2235cead939SRui Paulo 224