1 /*-
2 * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.Org>
3 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29 /*
30 * Generic DT based cpufreq driver
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/rman.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/cpu.h>
43 #include <sys/cpuset.h>
44 #include <sys/smp.h>
45
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48
49 #include <dev/extres/clk/clk.h>
50 #include <dev/extres/regulator/regulator.h>
51
52 #include "cpufreq_if.h"
53
54 #if 0
55 #define DPRINTF(dev, msg...) device_printf(dev, "cpufreq_dt: " msg);
56 #else
57 #define DPRINTF(dev, msg...)
58 #endif
59
60 enum opp_version {
61 OPP_V1 = 1,
62 OPP_V2,
63 };
64
65 struct cpufreq_dt_opp {
66 uint64_t freq;
67 uint32_t uvolt_target;
68 uint32_t uvolt_min;
69 uint32_t uvolt_max;
70 uint32_t uamps;
71 uint32_t clk_latency;
72 bool turbo_mode;
73 bool opp_suspend;
74 };
75
76 struct cpufreq_dt_softc {
77 device_t dev;
78 clk_t clk;
79 regulator_t reg;
80
81 struct cpufreq_dt_opp *opp;
82 ssize_t nopp;
83
84 int cpu;
85 cpuset_t cpus;
86 };
87
88 static void
89 cpufreq_dt_notify(device_t dev, uint64_t freq)
90 {
91 struct cpufreq_dt_softc *sc;
92 struct pcpu *pc;
93 int cpu;
94
95 sc = device_get_softc(dev);
96
97 CPU_FOREACH(cpu) {
98 if (CPU_ISSET(cpu, &sc->cpus)) {
99 pc = pcpu_find(cpu);
100 pc->pc_clock = freq;
101 }
102 }
103 }
104
105 static const struct cpufreq_dt_opp *
106 cpufreq_dt_find_opp(device_t dev, uint64_t freq)
107 {
108 struct cpufreq_dt_softc *sc;
109 ssize_t n;
110
111 sc = device_get_softc(dev);
112
113 DPRINTF(dev, "Looking for freq %ju\n", freq);
114 for (n = 0; n < sc->nopp; n++)
115 if (CPUFREQ_CMP(sc->opp[n].freq, freq))
116 return (&sc->opp[n]);
117
118 DPRINTF(dev, "Couldn't find one\n");
119 return (NULL);
120 }
121
122 static void
123 cpufreq_dt_opp_to_setting(device_t dev, const struct cpufreq_dt_opp *opp,
124 struct cf_setting *set)
125 {
126
127 memset(set, 0, sizeof(*set));
128 set->freq = opp->freq / 1000000;
129 set->volts = opp->uvolt_target / 1000;
130 set->power = CPUFREQ_VAL_UNKNOWN;
131 set->lat = opp->clk_latency;
132 set->dev = dev;
133 }
134
135 static int
136 cpufreq_dt_get(device_t dev, struct cf_setting *set)
137 {
138 struct cpufreq_dt_softc *sc;
139 const struct cpufreq_dt_opp *opp;
140 uint64_t freq;
141
142 sc = device_get_softc(dev);
143
144 DPRINTF(dev, "cpufreq_dt_get\n");
145 if (clk_get_freq(sc->clk, &freq) != 0)
146 return (ENXIO);
147
148 opp = cpufreq_dt_find_opp(dev, freq);
149 if (opp == NULL) {
150 device_printf(dev, "Can't find the current freq in opp\n");
151 return (ENOENT);
152 }
153
154 cpufreq_dt_opp_to_setting(dev, opp, set);
155
156 DPRINTF(dev, "Current freq %dMhz\n", set->freq);
157 return (0);
158 }
159
160 static int
161 cpufreq_dt_set(device_t dev, const struct cf_setting *set)
162 {
163 struct cpufreq_dt_softc *sc;
164 const struct cpufreq_dt_opp *opp, *copp;
165 uint64_t freq;
166 int uvolt, error;
167
168 sc = device_get_softc(dev);
169
170 DPRINTF(dev, "Working on cpu %d\n", sc->cpu);
171 DPRINTF(dev, "We have %d cpu on this dev\n", CPU_COUNT(&sc->cpus));
172 if (!CPU_ISSET(sc->cpu, &sc->cpus)) {
173 DPRINTF(dev, "Not for this CPU\n");
174 return (0);
175 }
176
177 if (clk_get_freq(sc->clk, &freq) != 0) {
178 device_printf(dev, "Can't get current clk freq\n");
179 return (ENXIO);
180 }
181 /* Try to get current valtage by using regulator first. */
182 error = regulator_get_voltage(sc->reg, &uvolt);
183 if (error != 0) {
184 /*
185 * Try oppoints table as backup way. However,
186 * this is insufficient because the actual processor
187 * frequency may not be in the table. PLL frequency
188 * granularity can be different that granularity of
189 * oppoint table.
190 */
191 copp = cpufreq_dt_find_opp(sc->dev, freq);
192 if (copp == NULL) {
193 device_printf(dev,
194 "Can't find the current freq in opp\n");
195 return (ENOENT);
196 }
197 uvolt = copp->uvolt_target;
198 }
199
200 opp = cpufreq_dt_find_opp(sc->dev, set->freq * 1000000);
201 if (opp == NULL) {
202 device_printf(dev, "Couldn't find an opp for this freq\n");
203 return (EINVAL);
204 }
205 DPRINTF(sc->dev, "Current freq %ju, uvolt: %d\n", freq, uvolt);
206 DPRINTF(sc->dev, "Target freq %ju, , uvolt: %d\n",
207 opp->freq, opp->uvolt_target);
208
209 if (uvolt < opp->uvolt_target) {
210 DPRINTF(dev, "Changing regulator from %u to %u\n",
211 uvolt, opp->uvolt_target);
212 error = regulator_set_voltage(sc->reg,
213 opp->uvolt_min,
214 opp->uvolt_max);
215 if (error != 0) {
216 DPRINTF(dev, "Failed, backout\n");
217 return (ENXIO);
218 }
219 }
220
221 DPRINTF(dev, "Setting clk to %ju\n", opp->freq);
222 error = clk_set_freq(sc->clk, opp->freq, CLK_SET_ROUND_DOWN);
223 if (error != 0) {
224 DPRINTF(dev, "Failed, backout\n");
225 /* Restore previous voltage (best effort) */
226 error = regulator_set_voltage(sc->reg,
227 copp->uvolt_min,
228 copp->uvolt_max);
229 return (ENXIO);
230 }
231
232 if (uvolt > opp->uvolt_target) {
233 DPRINTF(dev, "Changing regulator from %u to %u\n",
234 uvolt, opp->uvolt_target);
235 error = regulator_set_voltage(sc->reg,
236 opp->uvolt_min,
237 opp->uvolt_max);
238 if (error != 0) {
239 DPRINTF(dev, "Failed to switch regulator to %d\n",
240 opp->uvolt_target);
241 /* Restore previous CPU frequency (best effort) */
242 (void)clk_set_freq(sc->clk, copp->freq, 0);
243 return (ENXIO);
244 }
245 }
246
247 if (clk_get_freq(sc->clk, &freq) == 0)
248 cpufreq_dt_notify(dev, freq);
249
250 return (0);
251 }
252
253 static int
254 cpufreq_dt_type(device_t dev, int *type)
255 {
256 if (type == NULL)
257 return (EINVAL);
258
259 *type = CPUFREQ_TYPE_ABSOLUTE;
260 return (0);
261 }
262
263 static int
264 cpufreq_dt_settings(device_t dev, struct cf_setting *sets, int *count)
265 {
266 struct cpufreq_dt_softc *sc;
267 ssize_t n;
268
269 DPRINTF(dev, "cpufreq_dt_settings\n");
270 if (sets == NULL || count == NULL)
271 return (EINVAL);
272
273 sc = device_get_softc(dev);
274
275 if (*count < sc->nopp) {
276 *count = (int)sc->nopp;
277 return (E2BIG);
278 }
279
280 for (n = 0; n < sc->nopp; n++)
281 cpufreq_dt_opp_to_setting(dev, &sc->opp[n], &sets[n]);
282
283 *count = (int)sc->nopp;
284
285 return (0);
286 }
287
288 static void
289 cpufreq_dt_identify(driver_t *driver, device_t parent)
290 {
291 phandle_t node;
292
293 /* Properties must be listed under node /cpus/cpu@0 */
294 node = ofw_bus_get_node(parent);
295
296 /* The cpu@0 node must have the following properties */
297 if (!OF_hasprop(node, "clocks") ||
298 (!OF_hasprop(node, "cpu-supply") &&
299 !OF_hasprop(node, "cpu0-supply")))
300 return;
301
302 if (!OF_hasprop(node, "operating-points") &&
303 !OF_hasprop(node, "operating-points-v2"))
304 return;
305
306 if (device_find_child(parent, "cpufreq_dt", -1) != NULL)
307 return;
308
309 if (BUS_ADD_CHILD(parent, 0, "cpufreq_dt", device_get_unit(parent))
310 == NULL)
311 device_printf(parent, "add cpufreq_dt child failed\n");
312 }
313
314 static int
315 cpufreq_dt_probe(device_t dev)
316 {
317 phandle_t node;
318
319 node = ofw_bus_get_node(device_get_parent(dev));
320
321 if (!OF_hasprop(node, "clocks") ||
322 (!OF_hasprop(node, "cpu-supply") &&
323 !OF_hasprop(node, "cpu0-supply")))
324
325 return (ENXIO);
326
327 if (!OF_hasprop(node, "operating-points") &&
328 !OF_hasprop(node, "operating-points-v2"))
329 return (ENXIO);
330
331 device_set_desc(dev, "Generic cpufreq driver");
332 return (BUS_PROBE_GENERIC);
333 }
334
335 static int
336 cpufreq_dt_oppv1_parse(struct cpufreq_dt_softc *sc, phandle_t node)
337 {
338 uint32_t *opp, lat;
339 ssize_t n;
340
341 sc->nopp = OF_getencprop_alloc_multi(node, "operating-points",
342 sizeof(uint32_t) * 2, (void **)&opp);
343 if (sc->nopp == -1)
344 return (ENXIO);
345
346 if (OF_getencprop(node, "clock-latency", &lat, sizeof(lat)) == -1)
347 lat = CPUFREQ_VAL_UNKNOWN;
348
349 sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
350
351 for (n = 0; n < sc->nopp; n++) {
352 sc->opp[n].freq = opp[n * 2 + 0] * 1000;
353 sc->opp[n].uvolt_min = opp[n * 2 + 1];
354 sc->opp[n].uvolt_max = sc->opp[n].uvolt_min;
355 sc->opp[n].uvolt_target = sc->opp[n].uvolt_min;
356 sc->opp[n].clk_latency = lat;
357
358 if (bootverbose)
359 device_printf(sc->dev, "%ju.%03ju MHz, %u uV\n",
360 sc->opp[n].freq / 1000000,
361 sc->opp[n].freq % 1000000,
362 sc->opp[n].uvolt_target);
363 }
364 free(opp, M_OFWPROP);
365
366 return (0);
367 }
368
369 static int
370 cpufreq_dt_oppv2_parse(struct cpufreq_dt_softc *sc, phandle_t node)
371 {
372 phandle_t opp, opp_table, opp_xref;
373 pcell_t cell[2];
374 uint32_t *volts, lat;
375 int nvolt, i;
376
377 if (OF_getencprop(node, "operating-points-v2", &opp_xref,
378 sizeof(opp_xref)) == -1) {
379 device_printf(sc->dev, "Cannot get xref to oppv2 table\n");
380 return (ENXIO);
381 }
382
383 opp_table = OF_node_from_xref(opp_xref);
384 if (opp_table == opp_xref)
385 return (ENXIO);
386
387 if (!OF_hasprop(opp_table, "opp-shared")) {
388 device_printf(sc->dev, "Only opp-shared is supported\n");
389 return (ENXIO);
390 }
391
392 for (opp = OF_child(opp_table); opp > 0; opp = OF_peer(opp))
393 sc->nopp += 1;
394
395 sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
396
397 for (i = 0, opp_table = OF_child(opp_table); opp_table > 0;
398 opp_table = OF_peer(opp_table), i++) {
399 /* opp-hz is a required property */
400 if (OF_getencprop(opp_table, "opp-hz", cell,
401 sizeof(cell)) == -1)
402 continue;
403
404 sc->opp[i].freq = cell[0];
405 sc->opp[i].freq <<= 32;
406 sc->opp[i].freq |= cell[1];
407
408 if (OF_getencprop(opp_table, "clock-latency", &lat,
409 sizeof(lat)) == -1)
410 sc->opp[i].clk_latency = CPUFREQ_VAL_UNKNOWN;
411 else
412 sc->opp[i].clk_latency = (int)lat;
413
414 if (OF_hasprop(opp_table, "turbo-mode"))
415 sc->opp[i].turbo_mode = true;
416 if (OF_hasprop(opp_table, "opp-suspend"))
417 sc->opp[i].opp_suspend = true;
418
419 nvolt = OF_getencprop_alloc_multi(opp_table, "opp-microvolt",
420 sizeof(*volts), (void **)&volts);
421 if (nvolt == 1) {
422 sc->opp[i].uvolt_target = volts[0];
423 sc->opp[i].uvolt_min = volts[0];
424 sc->opp[i].uvolt_max = volts[0];
425 } else if (nvolt == 3) {
426 sc->opp[i].uvolt_target = volts[0];
427 sc->opp[i].uvolt_min = volts[1];
428 sc->opp[i].uvolt_max = volts[2];
429 } else {
430 device_printf(sc->dev,
431 "Wrong count of opp-microvolt property\n");
432 OF_prop_free(volts);
433 free(sc->opp, M_DEVBUF);
434 return (ENXIO);
435 }
436 OF_prop_free(volts);
437
438 if (bootverbose)
439 device_printf(sc->dev, "%ju.%03ju Mhz (%u uV)\n",
440 sc->opp[i].freq / 1000000,
441 sc->opp[i].freq % 1000000,
442 sc->opp[i].uvolt_target);
443 }
444 return (0);
445 }
446
447 static int
448 cpufreq_dt_attach(device_t dev)
449 {
450 struct cpufreq_dt_softc *sc;
451 phandle_t node;
452 phandle_t cnode, opp, copp;
453 int cpu;
454 uint64_t freq;
455 int rv = 0;
456 char device_type[16];
457 enum opp_version version;
458
459 sc = device_get_softc(dev);
460 sc->dev = dev;
461 node = ofw_bus_get_node(device_get_parent(dev));
462 sc->cpu = device_get_unit(device_get_parent(dev));
463
464 DPRINTF(dev, "cpu=%d\n", sc->cpu);
465 if (sc->cpu >= mp_ncpus) {
466 device_printf(dev, "Not attaching as cpu is not present\n");
467 return (ENXIO);
468 }
469
470 if (regulator_get_by_ofw_property(dev, node,
471 "cpu-supply", &sc->reg) != 0) {
472 if (regulator_get_by_ofw_property(dev, node,
473 "cpu0-supply", &sc->reg) != 0) {
474 device_printf(dev, "no regulator for %s\n",
475 ofw_bus_get_name(device_get_parent(dev)));
476 return (ENXIO);
477 }
478 }
479
480 if (clk_get_by_ofw_index(dev, node, 0, &sc->clk) != 0) {
481 device_printf(dev, "no clock for %s\n",
482 ofw_bus_get_name(device_get_parent(dev)));
483 regulator_release(sc->reg);
484 return (ENXIO);
485 }
486
487 if (OF_hasprop(node, "operating-points")) {
488 version = OPP_V1;
489 rv = cpufreq_dt_oppv1_parse(sc, node);
490 if (rv != 0) {
491 device_printf(dev, "Failed to parse opp-v1 table\n");
492 return (rv);
493 }
494 OF_getencprop(node, "operating-points", &opp,
495 sizeof(opp));
496 } else {
497 version = OPP_V2;
498 rv = cpufreq_dt_oppv2_parse(sc, node);
499 if (rv != 0) {
500 device_printf(dev, "Failed to parse opp-v2 table\n");
501 return (rv);
502 }
503 OF_getencprop(node, "operating-points-v2", &opp,
504 sizeof(opp));
505 }
506
507 /*
508 * Find all CPUs that share the same opp table
509 */
510 CPU_ZERO(&sc->cpus);
511 cnode = OF_parent(node);
512 for (cpu = 0, cnode = OF_child(cnode); cnode > 0; cnode = OF_peer(cnode)) {
513 if (OF_getprop(cnode, "device_type", device_type, sizeof(device_type)) <= 0)
514 continue;
515 if (strcmp(device_type, "cpu") != 0)
516 continue;
517 if (cpu == sc->cpu) {
518 DPRINTF(dev, "Skipping our cpu\n");
519 CPU_SET(cpu, &sc->cpus);
520 cpu++;
521 continue;
522 }
523 DPRINTF(dev, "Testing CPU %d\n", cpu);
524 copp = -1;
525 if (version == OPP_V1)
526 OF_getencprop(cnode, "operating-points", &copp,
527 sizeof(copp));
528 else if (version == OPP_V2)
529 OF_getencprop(cnode, "operating-points-v2",
530 &copp, sizeof(copp));
531 if (opp == copp) {
532 DPRINTF(dev, "CPU %d is using the same opp as this one (%d)\n",
533 cpu, sc->cpu);
534 CPU_SET(cpu, &sc->cpus);
535 }
536 cpu++;
537 }
538
539 if (clk_get_freq(sc->clk, &freq) == 0)
540 cpufreq_dt_notify(dev, freq);
541
542 cpufreq_register(dev);
543
544 return (0);
545 }
546
547 static device_method_t cpufreq_dt_methods[] = {
548 /* Device interface */
549 DEVMETHOD(device_identify, cpufreq_dt_identify),
550 DEVMETHOD(device_probe, cpufreq_dt_probe),
551 DEVMETHOD(device_attach, cpufreq_dt_attach),
552
553 /* cpufreq interface */
554 DEVMETHOD(cpufreq_drv_get, cpufreq_dt_get),
555 DEVMETHOD(cpufreq_drv_set, cpufreq_dt_set),
556 DEVMETHOD(cpufreq_drv_type, cpufreq_dt_type),
557 DEVMETHOD(cpufreq_drv_settings, cpufreq_dt_settings),
558
559 DEVMETHOD_END
560 };
561
562 static driver_t cpufreq_dt_driver = {
563 "cpufreq_dt",
564 cpufreq_dt_methods,
565 sizeof(struct cpufreq_dt_softc),
566 };
567
568 static devclass_t cpufreq_dt_devclass;
569
570 DRIVER_MODULE(cpufreq_dt, cpu, cpufreq_dt_driver, cpufreq_dt_devclass, 0, 0);
571 MODULE_VERSION(cpufreq_dt, 1);
Cache object: aabbf3154c6e12f5ecfeb985f22c74cb
|