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