1 /*-
2 * Copyright (C) 2009 Nathan Whitehorn
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD: stable/10/sys/dev/ofw/ofw_cpu.c 278609 2015-02-12 00:35:58Z ian $");
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/malloc.h>
34 #include <sys/bus.h>
35 #include <sys/cpu.h>
36 #include <machine/bus.h>
37
38 #include <dev/ofw/openfirm.h>
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41
42 static int ofw_cpulist_probe(device_t);
43 static int ofw_cpulist_attach(device_t);
44 static const struct ofw_bus_devinfo *ofw_cpulist_get_devinfo(device_t dev,
45 device_t child);
46
47 static MALLOC_DEFINE(M_OFWCPU, "ofwcpu", "OFW CPU device information");
48
49 static device_method_t ofw_cpulist_methods[] = {
50 /* Device interface */
51 DEVMETHOD(device_probe, ofw_cpulist_probe),
52 DEVMETHOD(device_attach, ofw_cpulist_attach),
53
54 /* Bus interface */
55 DEVMETHOD(bus_add_child, bus_generic_add_child),
56 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
57
58 /* ofw_bus interface */
59 DEVMETHOD(ofw_bus_get_devinfo, ofw_cpulist_get_devinfo),
60 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
61 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
62 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
63 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
64 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
65
66 DEVMETHOD_END
67 };
68
69 static driver_t ofw_cpulist_driver = {
70 "cpulist",
71 ofw_cpulist_methods,
72 0
73 };
74
75 static devclass_t ofw_cpulist_devclass;
76
77 DRIVER_MODULE(ofw_cpulist, ofwbus, ofw_cpulist_driver, ofw_cpulist_devclass,
78 0, 0);
79
80 static int
ofw_cpulist_probe(device_t dev)81 ofw_cpulist_probe(device_t dev)
82 {
83 const char *name;
84
85 name = ofw_bus_get_name(dev);
86
87 if (name == NULL || strcmp(name, "cpus") != 0)
88 return (ENXIO);
89
90 device_set_desc(dev, "Open Firmware CPU Group");
91
92 return (0);
93 }
94
95 static int
ofw_cpulist_attach(device_t dev)96 ofw_cpulist_attach(device_t dev)
97 {
98 phandle_t root, child;
99 device_t cdev;
100 struct ofw_bus_devinfo *dinfo;
101
102 root = ofw_bus_get_node(dev);
103
104 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
105 dinfo = malloc(sizeof(*dinfo), M_OFWCPU, M_WAITOK | M_ZERO);
106
107 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
108 free(dinfo, M_OFWCPU);
109 continue;
110 }
111 cdev = device_add_child(dev, NULL, -1);
112 if (cdev == NULL) {
113 device_printf(dev, "<%s>: device_add_child failed\n",
114 dinfo->obd_name);
115 ofw_bus_gen_destroy_devinfo(dinfo);
116 free(dinfo, M_OFWCPU);
117 continue;
118 }
119 device_set_ivars(cdev, dinfo);
120 }
121
122 return (bus_generic_attach(dev));
123 }
124
125 static const struct ofw_bus_devinfo *
ofw_cpulist_get_devinfo(device_t dev,device_t child)126 ofw_cpulist_get_devinfo(device_t dev, device_t child)
127 {
128 return (device_get_ivars(child));
129 }
130
131 static int ofw_cpu_probe(device_t);
132 static int ofw_cpu_attach(device_t);
133 static int ofw_cpu_read_ivar(device_t dev, device_t child, int index,
134 uintptr_t *result);
135
136 struct ofw_cpu_softc {
137 struct pcpu *sc_cpu_pcpu;
138 uint32_t sc_nominal_mhz;
139 };
140
141 static device_method_t ofw_cpu_methods[] = {
142 /* Device interface */
143 DEVMETHOD(device_probe, ofw_cpu_probe),
144 DEVMETHOD(device_attach, ofw_cpu_attach),
145
146 /* Bus interface */
147 DEVMETHOD(bus_add_child, bus_generic_add_child),
148 DEVMETHOD(bus_read_ivar, ofw_cpu_read_ivar),
149 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
150 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
151 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
152 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
153 DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
154
155 DEVMETHOD_END
156 };
157
158 static driver_t ofw_cpu_driver = {
159 "cpu",
160 ofw_cpu_methods,
161 sizeof(struct ofw_cpu_softc)
162 };
163
164 static devclass_t ofw_cpu_devclass;
165
166 DRIVER_MODULE(ofw_cpu, cpulist, ofw_cpu_driver, ofw_cpu_devclass, 0, 0);
167
168 static int
ofw_cpu_probe(device_t dev)169 ofw_cpu_probe(device_t dev)
170 {
171 const char *type = ofw_bus_get_type(dev);
172
173 if (type == NULL || strcmp(type, "cpu") != 0)
174 return (ENXIO);
175
176 device_set_desc(dev, "Open Firmware CPU");
177 return (0);
178 }
179
180 static int
ofw_cpu_attach(device_t dev)181 ofw_cpu_attach(device_t dev)
182 {
183 struct ofw_cpu_softc *sc;
184 phandle_t node;
185 uint32_t cell;
186
187 sc = device_get_softc(dev);
188 node = ofw_bus_get_node(dev);
189 if (OF_getencprop(node, "reg", &cell, sizeof(cell)) < 0) {
190 cell = device_get_unit(dev);
191 device_printf(dev, "missing 'reg' property, using %u\n", cell);
192 }
193 sc->sc_cpu_pcpu = pcpu_find(cell);
194 if (OF_getencprop(node, "clock-frequency", &cell, sizeof(cell)) < 0) {
195 device_printf(dev, "missing 'clock-frequency' property\n");
196 return (ENXIO);
197 }
198 sc->sc_nominal_mhz = cell / 1000000; /* convert to MHz */
199
200 bus_generic_probe(dev);
201 return (bus_generic_attach(dev));
202 }
203
204 static int
ofw_cpu_read_ivar(device_t dev,device_t child,int index,uintptr_t * result)205 ofw_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
206 {
207 struct ofw_cpu_softc *sc;
208
209 sc = device_get_softc(dev);
210
211 switch (index) {
212 case CPU_IVAR_PCPU:
213 *result = (uintptr_t)sc->sc_cpu_pcpu;
214 return (0);
215 case CPU_IVAR_NOMINAL_MHZ:
216 *result = (uintptr_t)sc->sc_nominal_mhz;
217 return (0);
218 }
219
220 return (ENOENT);
221 }
222
223