1193156Snwhitehorn/*- 2193156Snwhitehorn * Copyright (C) 2009 Nathan Whitehorn 3193156Snwhitehorn * All rights reserved. 4193156Snwhitehorn * 5193156Snwhitehorn * Redistribution and use in source and binary forms, with or without 6193156Snwhitehorn * modification, are permitted provided that the following conditions 7193156Snwhitehorn * are met: 8193156Snwhitehorn * 1. Redistributions of source code must retain the above copyright 9193156Snwhitehorn * notice, this list of conditions and the following disclaimer. 10193156Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright 11193156Snwhitehorn * notice, this list of conditions and the following disclaimer in the 12193156Snwhitehorn * documentation and/or other materials provided with the distribution. 13193156Snwhitehorn * 14193156Snwhitehorn * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 15193156Snwhitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16193156Snwhitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17193156Snwhitehorn * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 18193156Snwhitehorn * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19193156Snwhitehorn * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20193156Snwhitehorn * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21193156Snwhitehorn * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 22193156Snwhitehorn * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 23193156Snwhitehorn * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24193156Snwhitehorn */ 25193156Snwhitehorn 26227843Smarius#include <sys/cdefs.h> 27227843Smarius__FBSDID("$FreeBSD: releng/10.3/sys/dev/ofw/ofw_cpu.c 278609 2015-02-12 00:35:58Z ian $"); 28227843Smarius 29193156Snwhitehorn#include <sys/param.h> 30193156Snwhitehorn#include <sys/systm.h> 31193156Snwhitehorn#include <sys/kernel.h> 32193156Snwhitehorn#include <sys/module.h> 33193156Snwhitehorn#include <sys/malloc.h> 34193156Snwhitehorn#include <sys/bus.h> 35193156Snwhitehorn#include <sys/cpu.h> 36193156Snwhitehorn#include <machine/bus.h> 37193156Snwhitehorn 38193156Snwhitehorn#include <dev/ofw/openfirm.h> 39193156Snwhitehorn#include <dev/ofw/ofw_bus.h> 40193156Snwhitehorn#include <dev/ofw/ofw_bus_subr.h> 41193156Snwhitehorn 42193156Snwhitehornstatic int ofw_cpulist_probe(device_t); 43193156Snwhitehornstatic int ofw_cpulist_attach(device_t); 44193156Snwhitehornstatic const struct ofw_bus_devinfo *ofw_cpulist_get_devinfo(device_t dev, 45193156Snwhitehorn device_t child); 46193156Snwhitehorn 47193156Snwhitehornstatic MALLOC_DEFINE(M_OFWCPU, "ofwcpu", "OFW CPU device information"); 48193156Snwhitehorn 49193156Snwhitehornstatic device_method_t ofw_cpulist_methods[] = { 50193156Snwhitehorn /* Device interface */ 51193156Snwhitehorn DEVMETHOD(device_probe, ofw_cpulist_probe), 52193156Snwhitehorn DEVMETHOD(device_attach, ofw_cpulist_attach), 53193156Snwhitehorn 54193156Snwhitehorn /* Bus interface */ 55193156Snwhitehorn DEVMETHOD(bus_add_child, bus_generic_add_child), 56193156Snwhitehorn DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 57193156Snwhitehorn 58193156Snwhitehorn /* ofw_bus interface */ 59193156Snwhitehorn DEVMETHOD(ofw_bus_get_devinfo, ofw_cpulist_get_devinfo), 60193156Snwhitehorn DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 61193156Snwhitehorn DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 62193156Snwhitehorn DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 63193156Snwhitehorn DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 64193156Snwhitehorn DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 65193156Snwhitehorn 66227843Smarius DEVMETHOD_END 67193156Snwhitehorn}; 68193156Snwhitehorn 69193156Snwhitehornstatic driver_t ofw_cpulist_driver = { 70193156Snwhitehorn "cpulist", 71193156Snwhitehorn ofw_cpulist_methods, 72193156Snwhitehorn 0 73193156Snwhitehorn}; 74193156Snwhitehorn 75193156Snwhitehornstatic devclass_t ofw_cpulist_devclass; 76193156Snwhitehorn 77261513SnwhitehornDRIVER_MODULE(ofw_cpulist, ofwbus, ofw_cpulist_driver, ofw_cpulist_devclass, 78193156Snwhitehorn 0, 0); 79193156Snwhitehorn 80193156Snwhitehornstatic int 81193156Snwhitehornofw_cpulist_probe(device_t dev) 82193156Snwhitehorn{ 83193156Snwhitehorn const char *name; 84193156Snwhitehorn 85193156Snwhitehorn name = ofw_bus_get_name(dev); 86193156Snwhitehorn 87193156Snwhitehorn if (name == NULL || strcmp(name, "cpus") != 0) 88193156Snwhitehorn return (ENXIO); 89193156Snwhitehorn 90193156Snwhitehorn device_set_desc(dev, "Open Firmware CPU Group"); 91193156Snwhitehorn 92193156Snwhitehorn return (0); 93193156Snwhitehorn} 94193156Snwhitehorn 95193156Snwhitehornstatic int 96193156Snwhitehornofw_cpulist_attach(device_t dev) 97193156Snwhitehorn{ 98193156Snwhitehorn phandle_t root, child; 99193156Snwhitehorn device_t cdev; 100193156Snwhitehorn struct ofw_bus_devinfo *dinfo; 101193156Snwhitehorn 102193156Snwhitehorn root = ofw_bus_get_node(dev); 103193156Snwhitehorn 104193156Snwhitehorn for (child = OF_child(root); child != 0; child = OF_peer(child)) { 105193156Snwhitehorn dinfo = malloc(sizeof(*dinfo), M_OFWCPU, M_WAITOK | M_ZERO); 106193156Snwhitehorn 107193156Snwhitehorn if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) { 108193156Snwhitehorn free(dinfo, M_OFWCPU); 109193156Snwhitehorn continue; 110193156Snwhitehorn } 111193156Snwhitehorn cdev = device_add_child(dev, NULL, -1); 112193156Snwhitehorn if (cdev == NULL) { 113193156Snwhitehorn device_printf(dev, "<%s>: device_add_child failed\n", 114193156Snwhitehorn dinfo->obd_name); 115193156Snwhitehorn ofw_bus_gen_destroy_devinfo(dinfo); 116193156Snwhitehorn free(dinfo, M_OFWCPU); 117193156Snwhitehorn continue; 118193156Snwhitehorn } 119193156Snwhitehorn device_set_ivars(cdev, dinfo); 120193156Snwhitehorn } 121193156Snwhitehorn 122193156Snwhitehorn return (bus_generic_attach(dev)); 123193156Snwhitehorn} 124193156Snwhitehorn 125193156Snwhitehornstatic const struct ofw_bus_devinfo * 126193156Snwhitehornofw_cpulist_get_devinfo(device_t dev, device_t child) 127193156Snwhitehorn{ 128193156Snwhitehorn return (device_get_ivars(child)); 129193156Snwhitehorn} 130193156Snwhitehorn 131193156Snwhitehornstatic int ofw_cpu_probe(device_t); 132193156Snwhitehornstatic int ofw_cpu_attach(device_t); 133193156Snwhitehornstatic int ofw_cpu_read_ivar(device_t dev, device_t child, int index, 134193156Snwhitehorn uintptr_t *result); 135193156Snwhitehorn 136252115Sjhibbitsstruct ofw_cpu_softc { 137252115Sjhibbits struct pcpu *sc_cpu_pcpu; 138252115Sjhibbits uint32_t sc_nominal_mhz; 139252115Sjhibbits}; 140252115Sjhibbits 141193156Snwhitehornstatic device_method_t ofw_cpu_methods[] = { 142193156Snwhitehorn /* Device interface */ 143193156Snwhitehorn DEVMETHOD(device_probe, ofw_cpu_probe), 144193156Snwhitehorn DEVMETHOD(device_attach, ofw_cpu_attach), 145193156Snwhitehorn 146193156Snwhitehorn /* Bus interface */ 147193156Snwhitehorn DEVMETHOD(bus_add_child, bus_generic_add_child), 148193156Snwhitehorn DEVMETHOD(bus_read_ivar, ofw_cpu_read_ivar), 149193156Snwhitehorn DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 150193156Snwhitehorn DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 151193156Snwhitehorn DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource), 152193156Snwhitehorn DEVMETHOD(bus_release_resource, bus_generic_release_resource), 153193156Snwhitehorn DEVMETHOD(bus_activate_resource,bus_generic_activate_resource), 154193156Snwhitehorn 155227843Smarius DEVMETHOD_END 156193156Snwhitehorn}; 157193156Snwhitehorn 158193156Snwhitehornstatic driver_t ofw_cpu_driver = { 159193156Snwhitehorn "cpu", 160193156Snwhitehorn ofw_cpu_methods, 161255378Snwhitehorn sizeof(struct ofw_cpu_softc) 162193156Snwhitehorn}; 163193156Snwhitehorn 164193156Snwhitehornstatic devclass_t ofw_cpu_devclass; 165193156Snwhitehorn 166193156SnwhitehornDRIVER_MODULE(ofw_cpu, cpulist, ofw_cpu_driver, ofw_cpu_devclass, 0, 0); 167193156Snwhitehorn 168193156Snwhitehornstatic int 169193156Snwhitehornofw_cpu_probe(device_t dev) 170193156Snwhitehorn{ 171193156Snwhitehorn const char *type = ofw_bus_get_type(dev); 172193156Snwhitehorn 173278609Sian if (type == NULL || strcmp(type, "cpu") != 0) 174193156Snwhitehorn return (ENXIO); 175193156Snwhitehorn 176193156Snwhitehorn device_set_desc(dev, "Open Firmware CPU"); 177193156Snwhitehorn return (0); 178193156Snwhitehorn} 179193156Snwhitehorn 180193156Snwhitehornstatic int 181193156Snwhitehornofw_cpu_attach(device_t dev) 182193156Snwhitehorn{ 183252115Sjhibbits struct ofw_cpu_softc *sc; 184278609Sian phandle_t node; 185252115Sjhibbits uint32_t cell; 186252115Sjhibbits 187252115Sjhibbits sc = device_get_softc(dev); 188278609Sian node = ofw_bus_get_node(dev); 189278609Sian if (OF_getencprop(node, "reg", &cell, sizeof(cell)) < 0) { 190278609Sian cell = device_get_unit(dev); 191278609Sian device_printf(dev, "missing 'reg' property, using %u\n", cell); 192278609Sian } 193252115Sjhibbits sc->sc_cpu_pcpu = pcpu_find(cell); 194278609Sian if (OF_getencprop(node, "clock-frequency", &cell, sizeof(cell)) < 0) { 195278609Sian device_printf(dev, "missing 'clock-frequency' property\n"); 196278609Sian return (ENXIO); 197278609Sian } 198252115Sjhibbits sc->sc_nominal_mhz = cell / 1000000; /* convert to MHz */ 199252115Sjhibbits 200193156Snwhitehorn bus_generic_probe(dev); 201193156Snwhitehorn return (bus_generic_attach(dev)); 202193156Snwhitehorn} 203193156Snwhitehorn 204193156Snwhitehornstatic int 205193156Snwhitehornofw_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 206193156Snwhitehorn{ 207252115Sjhibbits struct ofw_cpu_softc *sc; 208193156Snwhitehorn 209252115Sjhibbits sc = device_get_softc(dev); 210252115Sjhibbits 211193156Snwhitehorn switch (index) { 212193156Snwhitehorn case CPU_IVAR_PCPU: 213252115Sjhibbits *result = (uintptr_t)sc->sc_cpu_pcpu; 214193156Snwhitehorn return (0); 215193156Snwhitehorn case CPU_IVAR_NOMINAL_MHZ: 216252115Sjhibbits *result = (uintptr_t)sc->sc_nominal_mhz; 217193156Snwhitehorn return (0); 218193156Snwhitehorn } 219193156Snwhitehorn 220193156Snwhitehorn return (ENOENT); 221193156Snwhitehorn} 222193156Snwhitehorn 223