ofw_cpu.c revision 193156
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 * $FreeBSD: head/sys/powerpc/ofw/ofw_cpu.c 193156 2009-05-31 09:01:23Z nwhitehorn $
26193156Snwhitehorn */
27193156Snwhitehorn
28193156Snwhitehorn#include <sys/param.h>
29193156Snwhitehorn#include <sys/systm.h>
30193156Snwhitehorn#include <sys/kernel.h>
31193156Snwhitehorn#include <sys/module.h>
32193156Snwhitehorn#include <sys/malloc.h>
33193156Snwhitehorn#include <sys/bus.h>
34193156Snwhitehorn#include <sys/cpu.h>
35193156Snwhitehorn#include <machine/bus.h>
36193156Snwhitehorn
37193156Snwhitehorn#include <dev/ofw/openfirm.h>
38193156Snwhitehorn#include <dev/ofw/ofw_bus.h>
39193156Snwhitehorn#include <dev/ofw/ofw_bus_subr.h>
40193156Snwhitehorn
41193156Snwhitehornstatic int	ofw_cpulist_probe(device_t);
42193156Snwhitehornstatic int	ofw_cpulist_attach(device_t);
43193156Snwhitehornstatic const struct ofw_bus_devinfo *ofw_cpulist_get_devinfo(device_t dev,
44193156Snwhitehorn    device_t child);
45193156Snwhitehorn
46193156Snwhitehornstatic MALLOC_DEFINE(M_OFWCPU, "ofwcpu", "OFW CPU device information");
47193156Snwhitehorn
48193156Snwhitehornstatic device_method_t ofw_cpulist_methods[] = {
49193156Snwhitehorn	/* Device interface */
50193156Snwhitehorn	DEVMETHOD(device_probe,		ofw_cpulist_probe),
51193156Snwhitehorn	DEVMETHOD(device_attach,	ofw_cpulist_attach),
52193156Snwhitehorn
53193156Snwhitehorn	/* Bus interface */
54193156Snwhitehorn	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
55193156Snwhitehorn	DEVMETHOD(bus_add_child,	bus_generic_add_child),
56193156Snwhitehorn	DEVMETHOD(bus_print_child,	bus_generic_print_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
67193156Snwhitehorn	{ 0, 0 }
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
78193156SnwhitehornDRIVER_MODULE(ofw_cpulist, nexus, 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
137193156Snwhitehornstatic device_method_t ofw_cpu_methods[] = {
138193156Snwhitehorn	/* Device interface */
139193156Snwhitehorn	DEVMETHOD(device_probe,		ofw_cpu_probe),
140193156Snwhitehorn	DEVMETHOD(device_attach,	ofw_cpu_attach),
141193156Snwhitehorn
142193156Snwhitehorn	/* Bus interface */
143193156Snwhitehorn	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
144193156Snwhitehorn	DEVMETHOD(bus_add_child,	bus_generic_add_child),
145193156Snwhitehorn	DEVMETHOD(bus_print_child,	bus_generic_print_child),
146193156Snwhitehorn	DEVMETHOD(bus_read_ivar,	ofw_cpu_read_ivar),
147193156Snwhitehorn	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
148193156Snwhitehorn	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
149193156Snwhitehorn	DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
150193156Snwhitehorn	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
151193156Snwhitehorn	DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
152193156Snwhitehorn
153193156Snwhitehorn	{ 0, 0 }
154193156Snwhitehorn};
155193156Snwhitehorn
156193156Snwhitehornstatic driver_t ofw_cpu_driver = {
157193156Snwhitehorn	"cpu",
158193156Snwhitehorn	ofw_cpu_methods,
159193156Snwhitehorn	0
160193156Snwhitehorn};
161193156Snwhitehorn
162193156Snwhitehornstatic devclass_t ofw_cpu_devclass;
163193156Snwhitehorn
164193156SnwhitehornDRIVER_MODULE(ofw_cpu, cpulist, ofw_cpu_driver, ofw_cpu_devclass, 0, 0);
165193156Snwhitehorn
166193156Snwhitehornstatic int
167193156Snwhitehornofw_cpu_probe(device_t dev)
168193156Snwhitehorn{
169193156Snwhitehorn	const char *type = ofw_bus_get_type(dev);
170193156Snwhitehorn
171193156Snwhitehorn	if (strcmp(type, "cpu") != 0)
172193156Snwhitehorn		return (ENXIO);
173193156Snwhitehorn
174193156Snwhitehorn	device_set_desc(dev, "Open Firmware CPU");
175193156Snwhitehorn	return (0);
176193156Snwhitehorn}
177193156Snwhitehorn
178193156Snwhitehornstatic int
179193156Snwhitehornofw_cpu_attach(device_t dev)
180193156Snwhitehorn{
181193156Snwhitehorn	bus_generic_probe(dev);
182193156Snwhitehorn	return (bus_generic_attach(dev));
183193156Snwhitehorn}
184193156Snwhitehorn
185193156Snwhitehornstatic int
186193156Snwhitehornofw_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
187193156Snwhitehorn{
188193156Snwhitehorn	uint32_t cell;
189193156Snwhitehorn
190193156Snwhitehorn	switch (index) {
191193156Snwhitehorn	case CPU_IVAR_PCPU:
192193156Snwhitehorn		OF_getprop(ofw_bus_get_node(dev), "reg", &cell, sizeof(cell));
193193156Snwhitehorn		*result = (uintptr_t)(pcpu_find(cell));
194193156Snwhitehorn		return (0);
195193156Snwhitehorn	case CPU_IVAR_NOMINAL_MHZ:
196193156Snwhitehorn		cell = 0;
197193156Snwhitehorn		OF_getprop(ofw_bus_get_node(dev), "clock-frequency",
198193156Snwhitehorn		    &cell, sizeof(cell));
199193156Snwhitehorn		cell /= 1000000; /* convert to MHz */
200193156Snwhitehorn		*result = (uintptr_t)(cell);
201193156Snwhitehorn		return (0);
202193156Snwhitehorn	}
203193156Snwhitehorn
204193156Snwhitehorn	return (ENOENT);
205193156Snwhitehorn}
206193156Snwhitehorn
207