ofw_cpu.c revision 275815
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 * 14275815Semaste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15275815Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16275815Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17275815Semaste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18275815Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19275815Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20275815Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21275815Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22275815Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23275815Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24275815Semaste * SUCH DAMAGE. 25193156Snwhitehorn */ 26193156Snwhitehorn 27227843Smarius#include <sys/cdefs.h> 28227843Smarius__FBSDID("$FreeBSD: head/sys/dev/ofw/ofw_cpu.c 275815 2014-12-15 22:20:14Z emaste $"); 29227843Smarius 30193156Snwhitehorn#include <sys/param.h> 31193156Snwhitehorn#include <sys/systm.h> 32193156Snwhitehorn#include <sys/kernel.h> 33193156Snwhitehorn#include <sys/module.h> 34193156Snwhitehorn#include <sys/malloc.h> 35193156Snwhitehorn#include <sys/bus.h> 36193156Snwhitehorn#include <sys/cpu.h> 37193156Snwhitehorn#include <machine/bus.h> 38193156Snwhitehorn 39193156Snwhitehorn#include <dev/ofw/openfirm.h> 40193156Snwhitehorn#include <dev/ofw/ofw_bus.h> 41193156Snwhitehorn#include <dev/ofw/ofw_bus_subr.h> 42193156Snwhitehorn 43193156Snwhitehornstatic int ofw_cpulist_probe(device_t); 44193156Snwhitehornstatic int ofw_cpulist_attach(device_t); 45193156Snwhitehornstatic const struct ofw_bus_devinfo *ofw_cpulist_get_devinfo(device_t dev, 46193156Snwhitehorn device_t child); 47193156Snwhitehorn 48193156Snwhitehornstatic MALLOC_DEFINE(M_OFWCPU, "ofwcpu", "OFW CPU device information"); 49193156Snwhitehorn 50193156Snwhitehornstatic device_method_t ofw_cpulist_methods[] = { 51193156Snwhitehorn /* Device interface */ 52193156Snwhitehorn DEVMETHOD(device_probe, ofw_cpulist_probe), 53193156Snwhitehorn DEVMETHOD(device_attach, ofw_cpulist_attach), 54193156Snwhitehorn 55193156Snwhitehorn /* Bus interface */ 56193156Snwhitehorn DEVMETHOD(bus_add_child, bus_generic_add_child), 57193156Snwhitehorn DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 58193156Snwhitehorn 59193156Snwhitehorn /* ofw_bus interface */ 60193156Snwhitehorn DEVMETHOD(ofw_bus_get_devinfo, ofw_cpulist_get_devinfo), 61193156Snwhitehorn DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 62193156Snwhitehorn DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 63193156Snwhitehorn DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 64193156Snwhitehorn DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 65193156Snwhitehorn DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 66193156Snwhitehorn 67227843Smarius DEVMETHOD_END 68193156Snwhitehorn}; 69193156Snwhitehorn 70193156Snwhitehornstatic driver_t ofw_cpulist_driver = { 71193156Snwhitehorn "cpulist", 72193156Snwhitehorn ofw_cpulist_methods, 73193156Snwhitehorn 0 74193156Snwhitehorn}; 75193156Snwhitehorn 76193156Snwhitehornstatic devclass_t ofw_cpulist_devclass; 77193156Snwhitehorn 78261513SnwhitehornDRIVER_MODULE(ofw_cpulist, ofwbus, ofw_cpulist_driver, ofw_cpulist_devclass, 79193156Snwhitehorn 0, 0); 80193156Snwhitehorn 81193156Snwhitehornstatic int 82193156Snwhitehornofw_cpulist_probe(device_t dev) 83193156Snwhitehorn{ 84193156Snwhitehorn const char *name; 85193156Snwhitehorn 86193156Snwhitehorn name = ofw_bus_get_name(dev); 87193156Snwhitehorn 88193156Snwhitehorn if (name == NULL || strcmp(name, "cpus") != 0) 89193156Snwhitehorn return (ENXIO); 90193156Snwhitehorn 91193156Snwhitehorn device_set_desc(dev, "Open Firmware CPU Group"); 92193156Snwhitehorn 93193156Snwhitehorn return (0); 94193156Snwhitehorn} 95193156Snwhitehorn 96193156Snwhitehornstatic int 97193156Snwhitehornofw_cpulist_attach(device_t dev) 98193156Snwhitehorn{ 99193156Snwhitehorn phandle_t root, child; 100193156Snwhitehorn device_t cdev; 101193156Snwhitehorn struct ofw_bus_devinfo *dinfo; 102193156Snwhitehorn 103193156Snwhitehorn root = ofw_bus_get_node(dev); 104193156Snwhitehorn 105193156Snwhitehorn for (child = OF_child(root); child != 0; child = OF_peer(child)) { 106193156Snwhitehorn dinfo = malloc(sizeof(*dinfo), M_OFWCPU, M_WAITOK | M_ZERO); 107193156Snwhitehorn 108193156Snwhitehorn if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) { 109193156Snwhitehorn free(dinfo, M_OFWCPU); 110193156Snwhitehorn continue; 111193156Snwhitehorn } 112193156Snwhitehorn cdev = device_add_child(dev, NULL, -1); 113193156Snwhitehorn if (cdev == NULL) { 114193156Snwhitehorn device_printf(dev, "<%s>: device_add_child failed\n", 115193156Snwhitehorn dinfo->obd_name); 116193156Snwhitehorn ofw_bus_gen_destroy_devinfo(dinfo); 117193156Snwhitehorn free(dinfo, M_OFWCPU); 118193156Snwhitehorn continue; 119193156Snwhitehorn } 120193156Snwhitehorn device_set_ivars(cdev, dinfo); 121193156Snwhitehorn } 122193156Snwhitehorn 123193156Snwhitehorn return (bus_generic_attach(dev)); 124193156Snwhitehorn} 125193156Snwhitehorn 126193156Snwhitehornstatic const struct ofw_bus_devinfo * 127193156Snwhitehornofw_cpulist_get_devinfo(device_t dev, device_t child) 128193156Snwhitehorn{ 129193156Snwhitehorn return (device_get_ivars(child)); 130193156Snwhitehorn} 131193156Snwhitehorn 132193156Snwhitehornstatic int ofw_cpu_probe(device_t); 133193156Snwhitehornstatic int ofw_cpu_attach(device_t); 134193156Snwhitehornstatic int ofw_cpu_read_ivar(device_t dev, device_t child, int index, 135193156Snwhitehorn uintptr_t *result); 136193156Snwhitehorn 137252115Sjhibbitsstruct ofw_cpu_softc { 138252115Sjhibbits struct pcpu *sc_cpu_pcpu; 139252115Sjhibbits uint32_t sc_nominal_mhz; 140252115Sjhibbits}; 141252115Sjhibbits 142193156Snwhitehornstatic device_method_t ofw_cpu_methods[] = { 143193156Snwhitehorn /* Device interface */ 144193156Snwhitehorn DEVMETHOD(device_probe, ofw_cpu_probe), 145193156Snwhitehorn DEVMETHOD(device_attach, ofw_cpu_attach), 146193156Snwhitehorn 147193156Snwhitehorn /* Bus interface */ 148193156Snwhitehorn DEVMETHOD(bus_add_child, bus_generic_add_child), 149193156Snwhitehorn DEVMETHOD(bus_read_ivar, ofw_cpu_read_ivar), 150193156Snwhitehorn DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 151193156Snwhitehorn DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 152193156Snwhitehorn DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource), 153193156Snwhitehorn DEVMETHOD(bus_release_resource, bus_generic_release_resource), 154193156Snwhitehorn DEVMETHOD(bus_activate_resource,bus_generic_activate_resource), 155193156Snwhitehorn 156227843Smarius DEVMETHOD_END 157193156Snwhitehorn}; 158193156Snwhitehorn 159193156Snwhitehornstatic driver_t ofw_cpu_driver = { 160193156Snwhitehorn "cpu", 161193156Snwhitehorn ofw_cpu_methods, 162255378Snwhitehorn sizeof(struct ofw_cpu_softc) 163193156Snwhitehorn}; 164193156Snwhitehorn 165193156Snwhitehornstatic devclass_t ofw_cpu_devclass; 166193156Snwhitehorn 167193156SnwhitehornDRIVER_MODULE(ofw_cpu, cpulist, ofw_cpu_driver, ofw_cpu_devclass, 0, 0); 168193156Snwhitehorn 169193156Snwhitehornstatic int 170193156Snwhitehornofw_cpu_probe(device_t dev) 171193156Snwhitehorn{ 172193156Snwhitehorn const char *type = ofw_bus_get_type(dev); 173193156Snwhitehorn 174193156Snwhitehorn if (strcmp(type, "cpu") != 0) 175193156Snwhitehorn return (ENXIO); 176193156Snwhitehorn 177193156Snwhitehorn device_set_desc(dev, "Open Firmware CPU"); 178193156Snwhitehorn return (0); 179193156Snwhitehorn} 180193156Snwhitehorn 181193156Snwhitehornstatic int 182193156Snwhitehornofw_cpu_attach(device_t dev) 183193156Snwhitehorn{ 184252115Sjhibbits struct ofw_cpu_softc *sc; 185252115Sjhibbits uint32_t cell; 186252115Sjhibbits 187252115Sjhibbits sc = device_get_softc(dev); 188252115Sjhibbits OF_getprop(ofw_bus_get_node(dev), "reg", &cell, sizeof(cell)); 189252115Sjhibbits sc->sc_cpu_pcpu = pcpu_find(cell); 190252115Sjhibbits OF_getprop(ofw_bus_get_node(dev), "clock-frequency", &cell, sizeof(cell)); 191252115Sjhibbits sc->sc_nominal_mhz = cell / 1000000; /* convert to MHz */ 192252115Sjhibbits 193193156Snwhitehorn bus_generic_probe(dev); 194193156Snwhitehorn return (bus_generic_attach(dev)); 195193156Snwhitehorn} 196193156Snwhitehorn 197193156Snwhitehornstatic int 198193156Snwhitehornofw_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 199193156Snwhitehorn{ 200252115Sjhibbits struct ofw_cpu_softc *sc; 201193156Snwhitehorn 202252115Sjhibbits sc = device_get_softc(dev); 203252115Sjhibbits 204193156Snwhitehorn switch (index) { 205193156Snwhitehorn case CPU_IVAR_PCPU: 206252115Sjhibbits *result = (uintptr_t)sc->sc_cpu_pcpu; 207193156Snwhitehorn return (0); 208193156Snwhitehorn case CPU_IVAR_NOMINAL_MHZ: 209252115Sjhibbits *result = (uintptr_t)sc->sc_nominal_mhz; 210193156Snwhitehorn return (0); 211193156Snwhitehorn } 212193156Snwhitehorn 213193156Snwhitehorn return (ENOENT); 214193156Snwhitehorn} 215193156Snwhitehorn 216