xref: /freebsd/sys/powerpc/cpufreq/pmufreq.c (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #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 	{0, 0}
72 };
73 
74 static driver_t pmufreq_driver = {
75 	"pmufreq",
76 	pmufreq_methods,
77 	sizeof(struct pmufreq_softc)
78 };
79 
80 DRIVER_MODULE(pmufreq, cpu, pmufreq_driver, 0, 0);
81 
82 static void
83 pmufreq_identify(driver_t *driver, device_t parent)
84 {
85 	phandle_t node;
86 	uint32_t min_freq;
87 
88 	node = ofw_bus_get_node(parent);
89 	if (OF_getprop(node, "min-clock-frequency", &min_freq, sizeof(min_freq)) == -1)
90 		return;
91 
92 	/* Make sure we're not being doubly invoked. */
93 	if (device_find_child(parent, "pmufreq", -1) != NULL)
94 		return;
95 
96 	/*
97 	 * We attach a child for every CPU since settings need to
98 	 * be performed on every CPU in the SMP case.
99 	 */
100 	if (BUS_ADD_CHILD(parent, 10, "pmufreq", -1) == NULL)
101 		device_printf(parent, "add pmufreq child failed\n");
102 }
103 
104 static int
105 pmufreq_probe(device_t dev)
106 {
107 	phandle_t node;
108 	uint32_t min_freq;
109 
110 	if (resource_disabled("pmufreq", 0))
111 		return (ENXIO);
112 
113 	node = ofw_bus_get_node(device_get_parent(dev));
114 	/*
115 	 * A scalable MPC7455 has min-clock-frequency/max-clock-frequency as OFW
116 	 * properties of the 'cpu' node.
117 	 */
118 	if (OF_getprop(node, "min-clock-frequency", &min_freq, sizeof(min_freq)) == -1)
119 		return (ENXIO);
120 	device_set_desc(dev, "PMU-based frequency scaling");
121 	return (0);
122 }
123 
124 static int
125 pmufreq_attach(device_t dev)
126 {
127 	struct pmufreq_softc *sc;
128 	phandle_t node;
129 
130 	sc = device_get_softc(dev);
131 	sc->dev = dev;
132 
133 	node = ofw_bus_get_node(device_get_parent(dev));
134 	OF_getprop(node, "min-clock-frequency", &sc->minfreq, sizeof(sc->minfreq));
135 	OF_getprop(node, "max-clock-frequency", &sc->maxfreq, sizeof(sc->maxfreq));
136 	OF_getprop(node, "rounded-clock-frequency", &sc->curfreq, sizeof(sc->curfreq));
137 	sc->minfreq /= 1000000;
138 	sc->maxfreq /= 1000000;
139 	sc->curfreq /= 1000000;
140 
141 	cpufreq_register(dev);
142 	return (0);
143 }
144 
145 static int
146 pmufreq_settings(device_t dev, struct cf_setting *sets, int *count)
147 {
148 	struct pmufreq_softc *sc;
149 
150 	sc = device_get_softc(dev);
151 	if (sets == NULL || count == NULL)
152 		return (EINVAL);
153 	if (*count < 2)
154 		return (E2BIG);
155 
156 	/* Return a list of valid settings for this driver. */
157 	memset(sets, CPUFREQ_VAL_UNKNOWN, sizeof(*sets) * 2);
158 
159 	sets[0].freq = sc->maxfreq; sets[0].dev = dev;
160 	sets[1].freq = sc->minfreq; sets[1].dev = dev;
161 	/* Set high latency for CPU frequency changes, it's a tedious process. */
162 	sets[0].lat = INT_MAX;
163 	sets[1].lat = INT_MAX;
164 	*count = 2;
165 
166 	return (0);
167 }
168 
169 static int
170 pmufreq_set(device_t dev, const struct cf_setting *set)
171 {
172 	struct pmufreq_softc *sc;
173 	int error, speed_sel;
174 
175 	if (set == NULL)
176 		return (EINVAL);
177 
178 	sc = device_get_softc(dev);
179 
180 	if (set->freq == sc->maxfreq)
181 		speed_sel = 0;
182 	else
183 		speed_sel = 1;
184 
185 	error = pmu_set_speed(speed_sel);
186 	if (error == 0)
187 		sc->curfreq = set->freq;
188 
189 	return (error);
190 }
191 
192 static int
193 pmufreq_get(device_t dev, struct cf_setting *set)
194 {
195 	struct pmufreq_softc *sc;
196 
197 	if (set == NULL)
198 		return (EINVAL);
199 	sc = device_get_softc(dev);
200 
201 	set->freq = sc->curfreq;
202 	set->dev = dev;
203 
204 	return (0);
205 }
206 
207 static int
208 pmufreq_type(device_t dev, int *type)
209 {
210 
211 	if (type == NULL)
212 		return (EINVAL);
213 
214 	*type = CPUFREQ_TYPE_ABSOLUTE;
215 	return (0);
216 }
217