xref: /freebsd/sys/powerpc/cpufreq/pmufreq.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*-
2  * Copyright (c) 2011 Justin Hibbits
3  * Copyright (c) 2009 Nathan Whitehorn
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/cpu.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
37 #include <sys/module.h>
38 
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/openfirm.h>
41 
42 #include "cpufreq_if.h"
43 #include "powerpc/powermac/pmuvar.h"
44 
45 struct pmufreq_softc {
46 	device_t dev;
47 	uint32_t minfreq;
48 	uint32_t maxfreq;
49 	uint32_t curfreq;
50 };
51 
52 static void	pmufreq_identify(driver_t *driver, device_t parent);
53 static int	pmufreq_probe(device_t dev);
54 static int	pmufreq_attach(device_t dev);
55 static int	pmufreq_settings(device_t dev, struct cf_setting *sets, int *count);
56 static int	pmufreq_set(device_t dev, const struct cf_setting *set);
57 static int	pmufreq_get(device_t dev, struct cf_setting *set);
58 static int	pmufreq_type(device_t dev, int *type);
59 
60 static device_method_t pmufreq_methods[] = {
61 	/* Device interface */
62 	DEVMETHOD(device_identify,	pmufreq_identify),
63 	DEVMETHOD(device_probe,		pmufreq_probe),
64 	DEVMETHOD(device_attach,	pmufreq_attach),
65 
66 	/* cpufreq interface */
67 	DEVMETHOD(cpufreq_drv_set,	pmufreq_set),
68 	DEVMETHOD(cpufreq_drv_get,	pmufreq_get),
69 	DEVMETHOD(cpufreq_drv_type,	pmufreq_type),
70 	DEVMETHOD(cpufreq_drv_settings,	pmufreq_settings),
71 
72 	{0, 0}
73 };
74 
75 static driver_t pmufreq_driver = {
76 	"pmufreq",
77 	pmufreq_methods,
78 	sizeof(struct pmufreq_softc)
79 };
80 
81 static devclass_t pmufreq_devclass;
82 DRIVER_MODULE(pmufreq, cpu, pmufreq_driver, pmufreq_devclass, 0, 0);
83 
84 static void
85 pmufreq_identify(driver_t *driver, device_t parent)
86 {
87 	phandle_t node;
88 	uint32_t min_freq;
89 
90 	node = ofw_bus_get_node(parent);
91 	if (OF_getprop(node, "min-clock-frequency", &min_freq, sizeof(min_freq)) == -1)
92 		return;
93 
94 	/* Make sure we're not being doubly invoked. */
95 	if (device_find_child(parent, "pmufreq", -1) != NULL)
96 		return;
97 
98 	/*
99 	 * We attach a child for every CPU since settings need to
100 	 * be performed on every CPU in the SMP case.
101 	 */
102 	if (BUS_ADD_CHILD(parent, 10, "pmufreq", -1) == NULL)
103 		device_printf(parent, "add pmufreq child failed\n");
104 }
105 
106 static int
107 pmufreq_probe(device_t dev)
108 {
109 	struct pmufreq_softc *sc;
110 	phandle_t node;
111 	uint32_t min_freq;
112 
113 	if (resource_disabled("pmufreq", 0))
114 		return (ENXIO);
115 
116 	sc = device_get_softc(dev);
117 	node = ofw_bus_get_node(device_get_parent(dev));
118 	/*
119 	 * A scalable MPC7455 has min-clock-frequency/max-clock-frequency as OFW
120 	 * properties of the 'cpu' node.
121 	 */
122 	if (OF_getprop(node, "min-clock-frequency", &min_freq, sizeof(min_freq)) == -1)
123 		return (ENXIO);
124 	device_set_desc(dev, "PMU-based frequency scaling");
125 	return (0);
126 }
127 
128 static int
129 pmufreq_attach(device_t dev)
130 {
131 	struct pmufreq_softc *sc;
132 	phandle_t node;
133 
134 	sc = device_get_softc(dev);
135 	sc->dev = dev;
136 
137 	node = ofw_bus_get_node(device_get_parent(dev));
138 	OF_getprop(node, "min-clock-frequency", &sc->minfreq, sizeof(sc->minfreq));
139 	OF_getprop(node, "max-clock-frequency", &sc->maxfreq, sizeof(sc->maxfreq));
140 	OF_getprop(node, "rounded-clock-frequency", &sc->curfreq, sizeof(sc->curfreq));
141 	sc->minfreq /= 1000000;
142 	sc->maxfreq /= 1000000;
143 	sc->curfreq /= 1000000;
144 
145 	cpufreq_register(dev);
146 	return (0);
147 }
148 
149 static int
150 pmufreq_settings(device_t dev, struct cf_setting *sets, int *count)
151 {
152 	struct pmufreq_softc *sc;
153 
154 	sc = device_get_softc(dev);
155 	if (sets == NULL || count == NULL)
156 		return (EINVAL);
157 	if (*count < 2)
158 		return (E2BIG);
159 
160 	/* Return a list of valid settings for this driver. */
161 	memset(sets, CPUFREQ_VAL_UNKNOWN, sizeof(*sets) * 2);
162 
163 	sets[0].freq = sc->maxfreq; sets[0].dev = dev;
164 	sets[1].freq = sc->minfreq; sets[1].dev = dev;
165 	/* Set high latency for CPU frequency changes, it's a tedious process. */
166 	sets[0].lat = INT_MAX;
167 	sets[1].lat = INT_MAX;
168 	*count = 2;
169 
170 	return (0);
171 }
172 
173 static int
174 pmufreq_set(device_t dev, const struct cf_setting *set)
175 {
176 	struct pmufreq_softc *sc;
177 	int error, speed_sel;
178 
179 	if (set == NULL)
180 		return (EINVAL);
181 
182 	sc = device_get_softc(dev);
183 
184 	if (set->freq == sc->maxfreq)
185 		speed_sel = 0;
186 	else
187 		speed_sel = 1;
188 
189 	error = pmu_set_speed(speed_sel);
190 	if (error == 0)
191 		sc->curfreq = set->freq;
192 
193 	return (error);
194 }
195 
196 static int
197 pmufreq_get(device_t dev, struct cf_setting *set)
198 {
199 	struct pmufreq_softc *sc;
200 
201 	if (set == NULL)
202 		return (EINVAL);
203 	sc = device_get_softc(dev);
204 
205 	set->freq = sc->curfreq;
206 	set->dev = dev;
207 
208 	return (0);
209 }
210 
211 static int
212 pmufreq_type(device_t dev, int *type)
213 {
214 
215 	if (type == NULL)
216 		return (EINVAL);
217 
218 	*type = CPUFREQ_TYPE_ABSOLUTE;
219 	return (0);
220 }
221 
222