xref: /freebsd/sys/powerpc/cpufreq/pmcr.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018 Justin Hibbits
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/module.h>
37 
38 #include <dev/ofw/openfirm.h>
39 
40 #include "cpufreq_if.h"
41 
42 static int pstate_ids[256];
43 static int pstate_freqs[256];
44 static int npstates;
45 
46 static void parse_pstates(void)
47 {
48 	phandle_t node;
49 
50 	node = OF_finddevice("/ibm,opal/power-mgt");
51 
52 	/* If this fails, npstates will remain 0, and any attachment will bail. */
53 	if (node == -1)
54 		return;
55 
56 	npstates = OF_getencprop(node, "ibm,pstate-ids", pstate_ids,
57 	    sizeof(pstate_ids));
58 	if (npstates < 0) {
59 		npstates = 0;
60 		return;
61 	}
62 
63 	if (OF_getencprop(node, "ibm,pstate-frequencies-mhz", pstate_freqs,
64 	    sizeof(pstate_freqs)) != npstates) {
65 		npstates = 0;
66 		return;
67 	}
68 	npstates /= sizeof(cell_t);
69 
70 }
71 
72 /* Make this a sysinit so it runs before the cpufreq driver attaches. */
73 SYSINIT(parse_pstates, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, parse_pstates, NULL);
74 
75 #define	PMCR_UPPERPS_MASK	0xff00000000000000UL
76 #define	PMCR_UPPERPS_SHIFT	56
77 #define	PMCR_LOWERPS_MASK	0x00ff000000000000UL
78 #define	PMCR_LOWERPS_SHIFT	48
79 #define	PMCR_VERSION_MASK	0x0000000f
80 #define	  PMCR_VERSION_1	  1
81 
82 struct pmcr_softc {
83 	device_t dev;
84 };
85 
86 static void	pmcr_identify(driver_t *driver, device_t parent);
87 static int	pmcr_probe(device_t dev);
88 static int	pmcr_attach(device_t dev);
89 static int	pmcr_settings(device_t dev, struct cf_setting *sets, int *count);
90 static int	pmcr_set(device_t dev, const struct cf_setting *set);
91 static int	pmcr_get(device_t dev, struct cf_setting *set);
92 static int	pmcr_type(device_t dev, int *type);
93 
94 static device_method_t pmcr_methods[] = {
95 	/* Device interface */
96 	DEVMETHOD(device_identify,	pmcr_identify),
97 	DEVMETHOD(device_probe,		pmcr_probe),
98 	DEVMETHOD(device_attach,	pmcr_attach),
99 
100 	/* cpufreq interface */
101 	DEVMETHOD(cpufreq_drv_set,	pmcr_set),
102 	DEVMETHOD(cpufreq_drv_get,	pmcr_get),
103 	DEVMETHOD(cpufreq_drv_type,	pmcr_type),
104 	DEVMETHOD(cpufreq_drv_settings,	pmcr_settings),
105 	{0, 0}
106 };
107 
108 static driver_t pmcr_driver = {
109 	"pmcr",
110 	pmcr_methods,
111 	sizeof(struct pmcr_softc)
112 };
113 
114 static devclass_t pmcr_devclass;
115 DRIVER_MODULE(pmcr, cpu, pmcr_driver, pmcr_devclass, 0, 0);
116 
117 static void
118 pmcr_identify(driver_t *driver, device_t parent)
119 {
120 
121 	/* Make sure we're not being doubly invoked. */
122 	if (device_find_child(parent, "pmcr", -1) != NULL)
123 		return;
124 
125 	/*
126 	 * We attach a child for every CPU since settings need to
127 	 * be performed on every CPU in the SMP case.
128 	 */
129 	if (BUS_ADD_CHILD(parent, 10, "pmcr", -1) == NULL)
130 		device_printf(parent, "add pmcr child failed\n");
131 }
132 
133 static int
134 pmcr_probe(device_t dev)
135 {
136 	if (resource_disabled("pmcr", 0))
137 		return (ENXIO);
138 
139 	if (npstates == 0)
140 		return (ENXIO);
141 
142 	device_set_desc(dev, "Power Management Control Register");
143 	return (0);
144 }
145 
146 static int
147 pmcr_attach(device_t dev)
148 {
149 	struct pmcr_softc *sc;
150 
151 	sc = device_get_softc(dev);
152 	sc->dev = dev;
153 
154 	cpufreq_register(dev);
155 	return (0);
156 }
157 
158 static int
159 pmcr_settings(device_t dev, struct cf_setting *sets, int *count)
160 {
161 	struct pmcr_softc *sc;
162 	int i;
163 
164 	sc = device_get_softc(dev);
165 	if (sets == NULL || count == NULL)
166 		return (EINVAL);
167 	if (*count < npstates)
168 		return (E2BIG);
169 
170 	/* Return a list of valid settings for this driver. */
171 	memset(sets, CPUFREQ_VAL_UNKNOWN, sizeof(*sets) * npstates);
172 
173 	for (i = 0; i < npstates; i++) {
174 		sets[i].freq = pstate_freqs[i];
175 		sets[i].spec[0] = pstate_ids[i];
176 		sets[i].spec[1] = i;
177 		sets[i].dev = dev;
178 	}
179 	*count = npstates;
180 
181 	return (0);
182 }
183 
184 static int
185 pmcr_set(device_t dev, const struct cf_setting *set)
186 {
187 	register_t pmcr;
188 
189 	if (set == NULL)
190 		return (EINVAL);
191 
192 	if (set->spec[1] < 0 || set->spec[1] >= npstates)
193 		return (EINVAL);
194 
195 	pmcr = ((long)set->spec[0] << PMCR_LOWERPS_SHIFT) & PMCR_LOWERPS_MASK;
196 	pmcr |= ((long)set->spec[0] << PMCR_UPPERPS_SHIFT) & PMCR_UPPERPS_MASK;
197 	pmcr |= PMCR_VERSION_1;
198 
199 	mtspr(SPR_PMCR, pmcr);
200 	powerpc_sync(); isync();
201 
202 	return (0);
203 }
204 
205 static int
206 pmcr_get(device_t dev, struct cf_setting *set)
207 {
208 	struct pmcr_softc *sc;
209 	register_t pmcr;
210 	int i, pstate;
211 
212 	if (set == NULL)
213 		return (EINVAL);
214 	sc = device_get_softc(dev);
215 
216 	memset(set, CPUFREQ_VAL_UNKNOWN, sizeof(*set));
217 
218 	pmcr = mfspr(SPR_PMCR);
219 
220 	pstate = (pmcr & PMCR_LOWERPS_MASK) >> PMCR_LOWERPS_SHIFT;
221 
222 	for (i = 0; i < npstates && pstate_ids[i] != pstate; i++)
223 		;
224 
225 	if (i == npstates)
226 		return (EINVAL);
227 
228 	set->spec[0] = pstate;
229 	set->spec[1] = i;
230 	set->freq = pstate_freqs[i];
231 
232 	set->dev = dev;
233 
234 	return (0);
235 }
236 
237 static int
238 pmcr_type(device_t dev, int *type)
239 {
240 
241 	if (type == NULL)
242 		return (EINVAL);
243 
244 	*type = CPUFREQ_TYPE_ABSOLUTE;
245 	return (0);
246 }
247