ofw_cpu.c revision 227843
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: head/sys/powerpc/ofw/ofw_cpu.c 227843 2011-11-22 21:28:20Z marius $");
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
77193156SnwhitehornDRIVER_MODULE(ofw_cpulist, nexus, 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
136193156Snwhitehornstatic device_method_t ofw_cpu_methods[] = {
137193156Snwhitehorn	/* Device interface */
138193156Snwhitehorn	DEVMETHOD(device_probe,		ofw_cpu_probe),
139193156Snwhitehorn	DEVMETHOD(device_attach,	ofw_cpu_attach),
140193156Snwhitehorn
141193156Snwhitehorn	/* Bus interface */
142193156Snwhitehorn	DEVMETHOD(bus_add_child,	bus_generic_add_child),
143193156Snwhitehorn	DEVMETHOD(bus_read_ivar,	ofw_cpu_read_ivar),
144193156Snwhitehorn	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
145193156Snwhitehorn	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
146193156Snwhitehorn	DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
147193156Snwhitehorn	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
148193156Snwhitehorn	DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
149193156Snwhitehorn
150227843Smarius	DEVMETHOD_END
151193156Snwhitehorn};
152193156Snwhitehorn
153193156Snwhitehornstatic driver_t ofw_cpu_driver = {
154193156Snwhitehorn	"cpu",
155193156Snwhitehorn	ofw_cpu_methods,
156193156Snwhitehorn	0
157193156Snwhitehorn};
158193156Snwhitehorn
159193156Snwhitehornstatic devclass_t ofw_cpu_devclass;
160193156Snwhitehorn
161193156SnwhitehornDRIVER_MODULE(ofw_cpu, cpulist, ofw_cpu_driver, ofw_cpu_devclass, 0, 0);
162193156Snwhitehorn
163193156Snwhitehornstatic int
164193156Snwhitehornofw_cpu_probe(device_t dev)
165193156Snwhitehorn{
166193156Snwhitehorn	const char *type = ofw_bus_get_type(dev);
167193156Snwhitehorn
168193156Snwhitehorn	if (strcmp(type, "cpu") != 0)
169193156Snwhitehorn		return (ENXIO);
170193156Snwhitehorn
171193156Snwhitehorn	device_set_desc(dev, "Open Firmware CPU");
172193156Snwhitehorn	return (0);
173193156Snwhitehorn}
174193156Snwhitehorn
175193156Snwhitehornstatic int
176193156Snwhitehornofw_cpu_attach(device_t dev)
177193156Snwhitehorn{
178193156Snwhitehorn	bus_generic_probe(dev);
179193156Snwhitehorn	return (bus_generic_attach(dev));
180193156Snwhitehorn}
181193156Snwhitehorn
182193156Snwhitehornstatic int
183193156Snwhitehornofw_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
184193156Snwhitehorn{
185193156Snwhitehorn	uint32_t cell;
186193156Snwhitehorn
187193156Snwhitehorn	switch (index) {
188193156Snwhitehorn	case CPU_IVAR_PCPU:
189193156Snwhitehorn		OF_getprop(ofw_bus_get_node(dev), "reg", &cell, sizeof(cell));
190193156Snwhitehorn		*result = (uintptr_t)(pcpu_find(cell));
191193156Snwhitehorn		return (0);
192193156Snwhitehorn	case CPU_IVAR_NOMINAL_MHZ:
193193156Snwhitehorn		cell = 0;
194193156Snwhitehorn		OF_getprop(ofw_bus_get_node(dev), "clock-frequency",
195193156Snwhitehorn		    &cell, sizeof(cell));
196193156Snwhitehorn		cell /= 1000000; /* convert to MHz */
197193156Snwhitehorn		*result = (uintptr_t)(cell);
198193156Snwhitehorn		return (0);
199193156Snwhitehorn	}
200193156Snwhitehorn
201193156Snwhitehorn	return (ENOENT);
202193156Snwhitehorn}
203193156Snwhitehorn
204