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