2 * Copyright (C) 2009 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/module.h>
32 #include <sys/malloc.h>
35 #include <machine/bus.h>
37 #include <dev/ofw/openfirm.h>
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
41 static int ofw_cpulist_probe(device_t);
42 static int ofw_cpulist_attach(device_t);
43 static const struct ofw_bus_devinfo *ofw_cpulist_get_devinfo(device_t dev,
46 static MALLOC_DEFINE(M_OFWCPU, "ofwcpu", "OFW CPU device information");
48 static device_method_t ofw_cpulist_methods[] = {
49 /* Device interface */
50 DEVMETHOD(device_probe, ofw_cpulist_probe),
51 DEVMETHOD(device_attach, ofw_cpulist_attach),
54 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
55 DEVMETHOD(bus_add_child, bus_generic_add_child),
56 DEVMETHOD(bus_print_child, bus_generic_print_child),
57 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
59 /* ofw_bus interface */
60 DEVMETHOD(ofw_bus_get_devinfo, ofw_cpulist_get_devinfo),
61 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
62 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
63 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
64 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
65 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
70 static driver_t ofw_cpulist_driver = {
76 static devclass_t ofw_cpulist_devclass;
78 DRIVER_MODULE(ofw_cpulist, nexus, ofw_cpulist_driver, ofw_cpulist_devclass,
82 ofw_cpulist_probe(device_t dev)
86 name = ofw_bus_get_name(dev);
88 if (name == NULL || strcmp(name, "cpus") != 0)
91 device_set_desc(dev, "Open Firmware CPU Group");
97 ofw_cpulist_attach(device_t dev)
99 phandle_t root, child;
101 struct ofw_bus_devinfo *dinfo;
103 root = ofw_bus_get_node(dev);
105 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
106 dinfo = malloc(sizeof(*dinfo), M_OFWCPU, M_WAITOK | M_ZERO);
108 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
109 free(dinfo, M_OFWCPU);
112 cdev = device_add_child(dev, NULL, -1);
114 device_printf(dev, "<%s>: device_add_child failed\n",
116 ofw_bus_gen_destroy_devinfo(dinfo);
117 free(dinfo, M_OFWCPU);
120 device_set_ivars(cdev, dinfo);
123 return (bus_generic_attach(dev));
126 static const struct ofw_bus_devinfo *
127 ofw_cpulist_get_devinfo(device_t dev, device_t child)
129 return (device_get_ivars(child));
132 static int ofw_cpu_probe(device_t);
133 static int ofw_cpu_attach(device_t);
134 static int ofw_cpu_read_ivar(device_t dev, device_t child, int index,
137 static device_method_t ofw_cpu_methods[] = {
138 /* Device interface */
139 DEVMETHOD(device_probe, ofw_cpu_probe),
140 DEVMETHOD(device_attach, ofw_cpu_attach),
143 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
144 DEVMETHOD(bus_add_child, bus_generic_add_child),
145 DEVMETHOD(bus_print_child, bus_generic_print_child),
146 DEVMETHOD(bus_read_ivar, ofw_cpu_read_ivar),
147 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
148 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
149 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
150 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
151 DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
156 static driver_t ofw_cpu_driver = {
162 static devclass_t ofw_cpu_devclass;
164 DRIVER_MODULE(ofw_cpu, cpulist, ofw_cpu_driver, ofw_cpu_devclass, 0, 0);
167 ofw_cpu_probe(device_t dev)
169 const char *type = ofw_bus_get_type(dev);
171 if (strcmp(type, "cpu") != 0)
174 device_set_desc(dev, "Open Firmware CPU");
179 ofw_cpu_attach(device_t dev)
181 bus_generic_probe(dev);
182 return (bus_generic_attach(dev));
186 ofw_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
192 OF_getprop(ofw_bus_get_node(dev), "reg", &cell, sizeof(cell));
193 *result = (uintptr_t)(pcpu_find(cell));
195 case CPU_IVAR_NOMINAL_MHZ:
197 OF_getprop(ofw_bus_get_node(dev), "clock-frequency",
198 &cell, sizeof(cell));
199 cell /= 1000000; /* convert to MHz */
200 *result = (uintptr_t)(cell);