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