Deleted Added
full compact
ofw_cpu.c (276162) ofw_cpu.c (277378)
1/*-
2 * Copyright (C) 2009 Nathan Whitehorn
1/*-
2 * Copyright (C) 2009 Nathan Whitehorn
3 * Copyright (C) 2015 The FreeBSD Foundation
3 * All rights reserved.
4 *
4 * All rights reserved.
5 *
6 * Portions of this software were developed by Andrew Turner
7 * under sponsorship from the FreeBSD Foundation.
8 *
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 THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/ofw/ofw_cpu.c 276162 2014-12-24 01:19:11Z ian $");
32__FBSDID("$FreeBSD: head/sys/dev/ofw/ofw_cpu.c 277378 2015-01-19 11:06:56Z andrew $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/module.h>
34#include <sys/malloc.h>
35#include <sys/bus.h>
36#include <sys/cpu.h>
37#include <machine/bus.h>
38
39#include <dev/ofw/openfirm.h>
40#include <dev/ofw/ofw_bus.h>
41#include <dev/ofw/ofw_bus_subr.h>
42
43static int ofw_cpulist_probe(device_t);
44static int ofw_cpulist_attach(device_t);
45static const struct ofw_bus_devinfo *ofw_cpulist_get_devinfo(device_t dev,
46 device_t child);
47
48static MALLOC_DEFINE(M_OFWCPU, "ofwcpu", "OFW CPU device information");
49
50static device_method_t ofw_cpulist_methods[] = {
51 /* Device interface */
52 DEVMETHOD(device_probe, ofw_cpulist_probe),
53 DEVMETHOD(device_attach, ofw_cpulist_attach),
54
55 /* Bus interface */
56 DEVMETHOD(bus_add_child, bus_generic_add_child),
57 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
58
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),
66
67 DEVMETHOD_END
68};
69
70static driver_t ofw_cpulist_driver = {
71 "cpulist",
72 ofw_cpulist_methods,
73 0
74};
75
76static devclass_t ofw_cpulist_devclass;
77
78DRIVER_MODULE(ofw_cpulist, ofwbus, ofw_cpulist_driver, ofw_cpulist_devclass,
79 0, 0);
80
81static int
82ofw_cpulist_probe(device_t dev)
83{
84 const char *name;
85
86 name = ofw_bus_get_name(dev);
87
88 if (name == NULL || strcmp(name, "cpus") != 0)
89 return (ENXIO);
90
91 device_set_desc(dev, "Open Firmware CPU Group");
92
93 return (0);
94}
95
96static int
97ofw_cpulist_attach(device_t dev)
98{
99 phandle_t root, child;
100 device_t cdev;
101 struct ofw_bus_devinfo *dinfo;
102
103 root = ofw_bus_get_node(dev);
104
105 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
106 dinfo = malloc(sizeof(*dinfo), M_OFWCPU, M_WAITOK | M_ZERO);
107
108 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
109 free(dinfo, M_OFWCPU);
110 continue;
111 }
112 cdev = device_add_child(dev, NULL, -1);
113 if (cdev == NULL) {
114 device_printf(dev, "<%s>: device_add_child failed\n",
115 dinfo->obd_name);
116 ofw_bus_gen_destroy_devinfo(dinfo);
117 free(dinfo, M_OFWCPU);
118 continue;
119 }
120 device_set_ivars(cdev, dinfo);
121 }
122
123 return (bus_generic_attach(dev));
124}
125
126static const struct ofw_bus_devinfo *
127ofw_cpulist_get_devinfo(device_t dev, device_t child)
128{
129 return (device_get_ivars(child));
130}
131
132static int ofw_cpu_probe(device_t);
133static int ofw_cpu_attach(device_t);
134static int ofw_cpu_read_ivar(device_t dev, device_t child, int index,
135 uintptr_t *result);
136
137struct ofw_cpu_softc {
138 struct pcpu *sc_cpu_pcpu;
139 uint32_t sc_nominal_mhz;
140};
141
142static device_method_t ofw_cpu_methods[] = {
143 /* Device interface */
144 DEVMETHOD(device_probe, ofw_cpu_probe),
145 DEVMETHOD(device_attach, ofw_cpu_attach),
146
147 /* Bus interface */
148 DEVMETHOD(bus_add_child, bus_generic_add_child),
149 DEVMETHOD(bus_read_ivar, ofw_cpu_read_ivar),
150 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
151 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
152 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
153 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
154 DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
155
156 DEVMETHOD_END
157};
158
159static driver_t ofw_cpu_driver = {
160 "cpu",
161 ofw_cpu_methods,
162 sizeof(struct ofw_cpu_softc)
163};
164
165static devclass_t ofw_cpu_devclass;
166
167DRIVER_MODULE(ofw_cpu, cpulist, ofw_cpu_driver, ofw_cpu_devclass, 0, 0);
168
169static int
170ofw_cpu_probe(device_t dev)
171{
172 const char *type = ofw_bus_get_type(dev);
173
174 if (type == NULL || strcmp(type, "cpu") != 0)
175 return (ENXIO);
176
177 device_set_desc(dev, "Open Firmware CPU");
178 return (0);
179}
180
181static int
182ofw_cpu_attach(device_t dev)
183{
184 struct ofw_cpu_softc *sc;
185 phandle_t node;
186 uint32_t cell;
187
188 sc = device_get_softc(dev);
189 node = ofw_bus_get_node(dev);
190 if (OF_getencprop(node, "reg", &cell, sizeof(cell)) < 0) {
191 cell = device_get_unit(dev);
192 device_printf(dev, "missing 'reg' property, using %u\n", cell);
193 }
194 sc->sc_cpu_pcpu = pcpu_find(cell);
195 if (OF_getencprop(node, "clock-frequency", &cell, sizeof(cell)) < 0) {
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#include <sys/module.h>
38#include <sys/malloc.h>
39#include <sys/bus.h>
40#include <sys/cpu.h>
41#include <machine/bus.h>
42
43#include <dev/ofw/openfirm.h>
44#include <dev/ofw/ofw_bus.h>
45#include <dev/ofw/ofw_bus_subr.h>
46
47static int ofw_cpulist_probe(device_t);
48static int ofw_cpulist_attach(device_t);
49static const struct ofw_bus_devinfo *ofw_cpulist_get_devinfo(device_t dev,
50 device_t child);
51
52static MALLOC_DEFINE(M_OFWCPU, "ofwcpu", "OFW CPU device information");
53
54static device_method_t ofw_cpulist_methods[] = {
55 /* Device interface */
56 DEVMETHOD(device_probe, ofw_cpulist_probe),
57 DEVMETHOD(device_attach, ofw_cpulist_attach),
58
59 /* Bus interface */
60 DEVMETHOD(bus_add_child, bus_generic_add_child),
61 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
62
63 /* ofw_bus interface */
64 DEVMETHOD(ofw_bus_get_devinfo, ofw_cpulist_get_devinfo),
65 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
66 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
67 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
68 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
69 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
70
71 DEVMETHOD_END
72};
73
74static driver_t ofw_cpulist_driver = {
75 "cpulist",
76 ofw_cpulist_methods,
77 0
78};
79
80static devclass_t ofw_cpulist_devclass;
81
82DRIVER_MODULE(ofw_cpulist, ofwbus, ofw_cpulist_driver, ofw_cpulist_devclass,
83 0, 0);
84
85static int
86ofw_cpulist_probe(device_t dev)
87{
88 const char *name;
89
90 name = ofw_bus_get_name(dev);
91
92 if (name == NULL || strcmp(name, "cpus") != 0)
93 return (ENXIO);
94
95 device_set_desc(dev, "Open Firmware CPU Group");
96
97 return (0);
98}
99
100static int
101ofw_cpulist_attach(device_t dev)
102{
103 phandle_t root, child;
104 device_t cdev;
105 struct ofw_bus_devinfo *dinfo;
106
107 root = ofw_bus_get_node(dev);
108
109 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
110 dinfo = malloc(sizeof(*dinfo), M_OFWCPU, M_WAITOK | M_ZERO);
111
112 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
113 free(dinfo, M_OFWCPU);
114 continue;
115 }
116 cdev = device_add_child(dev, NULL, -1);
117 if (cdev == NULL) {
118 device_printf(dev, "<%s>: device_add_child failed\n",
119 dinfo->obd_name);
120 ofw_bus_gen_destroy_devinfo(dinfo);
121 free(dinfo, M_OFWCPU);
122 continue;
123 }
124 device_set_ivars(cdev, dinfo);
125 }
126
127 return (bus_generic_attach(dev));
128}
129
130static const struct ofw_bus_devinfo *
131ofw_cpulist_get_devinfo(device_t dev, device_t child)
132{
133 return (device_get_ivars(child));
134}
135
136static int ofw_cpu_probe(device_t);
137static int ofw_cpu_attach(device_t);
138static int ofw_cpu_read_ivar(device_t dev, device_t child, int index,
139 uintptr_t *result);
140
141struct ofw_cpu_softc {
142 struct pcpu *sc_cpu_pcpu;
143 uint32_t sc_nominal_mhz;
144};
145
146static device_method_t ofw_cpu_methods[] = {
147 /* Device interface */
148 DEVMETHOD(device_probe, ofw_cpu_probe),
149 DEVMETHOD(device_attach, ofw_cpu_attach),
150
151 /* Bus interface */
152 DEVMETHOD(bus_add_child, bus_generic_add_child),
153 DEVMETHOD(bus_read_ivar, ofw_cpu_read_ivar),
154 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
155 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
156 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
157 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
158 DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
159
160 DEVMETHOD_END
161};
162
163static driver_t ofw_cpu_driver = {
164 "cpu",
165 ofw_cpu_methods,
166 sizeof(struct ofw_cpu_softc)
167};
168
169static devclass_t ofw_cpu_devclass;
170
171DRIVER_MODULE(ofw_cpu, cpulist, ofw_cpu_driver, ofw_cpu_devclass, 0, 0);
172
173static int
174ofw_cpu_probe(device_t dev)
175{
176 const char *type = ofw_bus_get_type(dev);
177
178 if (type == NULL || strcmp(type, "cpu") != 0)
179 return (ENXIO);
180
181 device_set_desc(dev, "Open Firmware CPU");
182 return (0);
183}
184
185static int
186ofw_cpu_attach(device_t dev)
187{
188 struct ofw_cpu_softc *sc;
189 phandle_t node;
190 uint32_t cell;
191
192 sc = device_get_softc(dev);
193 node = ofw_bus_get_node(dev);
194 if (OF_getencprop(node, "reg", &cell, sizeof(cell)) < 0) {
195 cell = device_get_unit(dev);
196 device_printf(dev, "missing 'reg' property, using %u\n", cell);
197 }
198 sc->sc_cpu_pcpu = pcpu_find(cell);
199 if (OF_getencprop(node, "clock-frequency", &cell, sizeof(cell)) < 0) {
196 device_printf(dev, "missing 'clock-frequency' property\n");
197 return (ENXIO);
198 }
199 sc->sc_nominal_mhz = cell / 1000000; /* convert to MHz */
200 if (bootverbose)
201 device_printf(dev,
202 "missing 'clock-frequency' property\n");
203 } else
204 sc->sc_nominal_mhz = cell / 1000000; /* convert to MHz */
200
201 bus_generic_probe(dev);
202 return (bus_generic_attach(dev));
203}
204
205static int
206ofw_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
207{
208 struct ofw_cpu_softc *sc;
209
210 sc = device_get_softc(dev);
211
212 switch (index) {
213 case CPU_IVAR_PCPU:
214 *result = (uintptr_t)sc->sc_cpu_pcpu;
215 return (0);
216 case CPU_IVAR_NOMINAL_MHZ:
205
206 bus_generic_probe(dev);
207 return (bus_generic_attach(dev));
208}
209
210static int
211ofw_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
212{
213 struct ofw_cpu_softc *sc;
214
215 sc = device_get_softc(dev);
216
217 switch (index) {
218 case CPU_IVAR_PCPU:
219 *result = (uintptr_t)sc->sc_cpu_pcpu;
220 return (0);
221 case CPU_IVAR_NOMINAL_MHZ:
217 *result = (uintptr_t)sc->sc_nominal_mhz;
218 return (0);
222 if (sc->sc_nominal_mhz > 0) {
223 *result = (uintptr_t)sc->sc_nominal_mhz;
224 return (0);
225 }
226 break;
219 }
220
221 return (ENOENT);
222}
223
227 }
228
229 return (ENOENT);
230}
231