xref: /freebsd/sys/dev/cpufreq/cpufreq_dt.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*-
2  * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.Org>
3  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
4  * All rights reserved.
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  * $FreeBSD$
28  */
29 
30 /*
31  * Generic DT based cpufreq driver
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/rman.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/cpu.h>
44 #include <sys/cpuset.h>
45 #include <sys/smp.h>
46 
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 
50 #include <dev/extres/clk/clk.h>
51 #include <dev/extres/regulator/regulator.h>
52 
53 #include "cpufreq_if.h"
54 
55 #if 0
56 #define DEBUG(dev, msg...) device_printf(dev, "cpufreq_dt: " msg);
57 #else
58 #define DEBUG(dev, msg...)
59 #endif
60 
61 enum opp_version {
62 	OPP_V1 = 1,
63 	OPP_V2,
64 };
65 
66 struct cpufreq_dt_opp {
67 	uint64_t	freq;
68 	uint32_t	uvolt_target;
69 	uint32_t	uvolt_min;
70 	uint32_t	uvolt_max;
71 	uint32_t	uamps;
72 	uint32_t	clk_latency;
73 	bool		turbo_mode;
74 	bool		opp_suspend;
75 };
76 
77 struct cpufreq_dt_softc {
78 	device_t dev;
79 	clk_t clk;
80 	regulator_t reg;
81 
82 	struct cpufreq_dt_opp *opp;
83 	ssize_t nopp;
84 
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 	DEBUG(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 	DEBUG(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 	struct cpufreq_dt_softc *sc;
127 
128 	sc = device_get_softc(dev);
129 
130 	memset(set, 0, sizeof(*set));
131 	set->freq = opp->freq / 1000000;
132 	set->volts = opp->uvolt_target / 1000;
133 	set->power = CPUFREQ_VAL_UNKNOWN;
134 	set->lat = opp->clk_latency;
135 	set->dev = dev;
136 }
137 
138 static int
139 cpufreq_dt_get(device_t dev, struct cf_setting *set)
140 {
141 	struct cpufreq_dt_softc *sc;
142 	const struct cpufreq_dt_opp *opp;
143 	uint64_t freq;
144 
145 	sc = device_get_softc(dev);
146 
147 	DEBUG(dev, "cpufreq_dt_get\n");
148 	if (clk_get_freq(sc->clk, &freq) != 0)
149 		return (ENXIO);
150 
151 	opp = cpufreq_dt_find_opp(dev, freq);
152 	if (opp == NULL) {
153 		device_printf(dev, "Can't find the current freq in opp\n");
154 		return (ENOENT);
155 	}
156 
157 	cpufreq_dt_opp_to_setting(dev, opp, set);
158 
159 	DEBUG(dev, "Current freq %dMhz\n", set->freq);
160 	return (0);
161 }
162 
163 static int
164 cpufreq_dt_set(device_t dev, const struct cf_setting *set)
165 {
166 	struct cpufreq_dt_softc *sc;
167 	const struct cpufreq_dt_opp *opp, *copp;
168 	uint64_t freq;
169 	int uvolt, error;
170 
171 	sc = device_get_softc(dev);
172 
173 	if (clk_get_freq(sc->clk, &freq) != 0) {
174 		device_printf(dev, "Can't get current clk freq\n");
175 		return (ENXIO);
176 	}
177 	/* Try to get current valtage by using regulator first. */
178 	error = regulator_get_voltage(sc->reg, &uvolt);
179 	if (error != 0) {
180 		/*
181 		 * Try oppoints table as backup way. However,
182 		 * this is insufficient because the actual processor
183 		 * frequency may not be in the table. PLL frequency
184 		 * granularity can be different that granularity of
185 		 * oppoint table.
186 		 */
187 		copp = cpufreq_dt_find_opp(sc->dev, freq);
188 		if (copp == NULL) {
189 			device_printf(dev,
190 			    "Can't find the current freq in opp\n");
191 			return (ENOENT);
192 		}
193 		uvolt = copp->uvolt_target;
194 
195 	}
196 
197 	opp = cpufreq_dt_find_opp(sc->dev, set->freq * 1000000);
198 	if (opp == NULL) {
199 		device_printf(dev, "Couldn't find an opp for this freq\n");
200 		return (EINVAL);
201 	}
202 	DEBUG(sc->dev, "Current freq %ju, uvolt: %d\n", freq, uvolt);
203 	DEBUG(sc->dev, "Target freq %ju, , uvolt: %d\n",
204 	    opp->freq, opp->uvolt_target);
205 
206 	if (uvolt < opp->uvolt_target) {
207 		DEBUG(dev, "Changing regulator from %u to %u\n",
208 		    uvolt, opp->uvolt_target);
209 		error = regulator_set_voltage(sc->reg,
210 		    opp->uvolt_min,
211 		    opp->uvolt_max);
212 		if (error != 0) {
213 			DEBUG(dev, "Failed, backout\n");
214 			return (ENXIO);
215 		}
216 	}
217 
218 	DEBUG(dev, "Setting clk to %ju\n", opp->freq);
219 	error = clk_set_freq(sc->clk, opp->freq, CLK_SET_ROUND_DOWN);
220 	if (error != 0) {
221 		DEBUG(dev, "Failed, backout\n");
222 		/* Restore previous voltage (best effort) */
223 		error = regulator_set_voltage(sc->reg,
224 		    copp->uvolt_min,
225 		    copp->uvolt_max);
226 		return (ENXIO);
227 	}
228 
229 	if (uvolt > opp->uvolt_target) {
230 		DEBUG(dev, "Changing regulator from %u to %u\n",
231 		    uvolt, opp->uvolt_target);
232 		error = regulator_set_voltage(sc->reg,
233 		    opp->uvolt_min,
234 		    opp->uvolt_max);
235 		if (error != 0) {
236 			DEBUG(dev, "Failed to switch regulator to %d\n",
237 			    opp->uvolt_target);
238 			/* Restore previous CPU frequency (best effort) */
239 			(void)clk_set_freq(sc->clk, copp->freq, 0);
240 			return (ENXIO);
241 		}
242 	}
243 
244 	if (clk_get_freq(sc->clk, &freq) == 0)
245 		cpufreq_dt_notify(dev, freq);
246 
247 	return (0);
248 }
249 
250 
251 static int
252 cpufreq_dt_type(device_t dev, int *type)
253 {
254 	if (type == NULL)
255 		return (EINVAL);
256 
257 	*type = CPUFREQ_TYPE_ABSOLUTE;
258 	return (0);
259 }
260 
261 static int
262 cpufreq_dt_settings(device_t dev, struct cf_setting *sets, int *count)
263 {
264 	struct cpufreq_dt_softc *sc;
265 	ssize_t n;
266 
267 	DEBUG(dev, "cpufreq_dt_settings\n");
268 	if (sets == NULL || count == NULL)
269 		return (EINVAL);
270 
271 	sc = device_get_softc(dev);
272 
273 	if (*count < sc->nopp) {
274 		*count = (int)sc->nopp;
275 		return (E2BIG);
276 	}
277 
278 	for (n = 0; n < sc->nopp; n++)
279 		cpufreq_dt_opp_to_setting(dev, &sc->opp[n], &sets[n]);
280 
281 	*count = (int)sc->nopp;
282 
283 	return (0);
284 }
285 
286 static void
287 cpufreq_dt_identify(driver_t *driver, device_t parent)
288 {
289 	phandle_t node;
290 
291 	/* Properties must be listed under node /cpus/cpu@0 */
292 	node = ofw_bus_get_node(parent);
293 
294 	/* The cpu@0 node must have the following properties */
295 	if (!OF_hasprop(node, "clocks") ||
296 	    (!OF_hasprop(node, "cpu-supply") &&
297 	    !OF_hasprop(node, "cpu0-supply")))
298 		return;
299 
300 	if (!OF_hasprop(node, "operating-points") &&
301 	    !OF_hasprop(node, "operating-points-v2"))
302 		return;
303 
304 	if (device_find_child(parent, "cpufreq_dt", -1) != NULL)
305 		return;
306 
307 	if (BUS_ADD_CHILD(parent, 0, "cpufreq_dt", -1) == NULL)
308 		device_printf(parent, "add cpufreq_dt child failed\n");
309 }
310 
311 static int
312 cpufreq_dt_probe(device_t dev)
313 {
314 	phandle_t node;
315 
316 	node = ofw_bus_get_node(device_get_parent(dev));
317 
318 	if (!OF_hasprop(node, "clocks") ||
319 	    (!OF_hasprop(node, "cpu-supply") &&
320 	    !OF_hasprop(node, "cpu0-supply")))
321 
322 		return (ENXIO);
323 
324 	if (!OF_hasprop(node, "operating-points") &&
325 	  !OF_hasprop(node, "operating-points-v2"))
326 		return (ENXIO);
327 
328 	device_set_desc(dev, "Generic cpufreq driver");
329 	return (BUS_PROBE_GENERIC);
330 }
331 
332 static int
333 cpufreq_dt_oppv1_parse(struct cpufreq_dt_softc *sc, phandle_t node)
334 {
335 	uint32_t *opp, lat;
336 	ssize_t n;
337 
338 	sc->nopp = OF_getencprop_alloc_multi(node, "operating-points",
339 	    sizeof(uint32_t) * 2, (void **)&opp);
340 	if (sc->nopp == -1)
341 		return (ENXIO);
342 
343 	if (OF_getencprop(node, "clock-latency", &lat, sizeof(lat)) == -1)
344 		lat = CPUFREQ_VAL_UNKNOWN;
345 
346 	sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
347 
348 	for (n = 0; n < sc->nopp; n++) {
349 		sc->opp[n].freq = opp[n * 2 + 0] * 1000;
350 		sc->opp[n].uvolt_min = opp[n * 2 + 1];
351 		sc->opp[n].uvolt_max = sc->opp[n].uvolt_min;
352 		sc->opp[n].uvolt_target = sc->opp[n].uvolt_min;
353 		sc->opp[n].clk_latency = lat;
354 
355 		if (bootverbose)
356 			device_printf(sc->dev, "%ju.%03ju MHz, %u uV\n",
357 			    sc->opp[n].freq / 1000000,
358 			    sc->opp[n].freq % 1000000,
359 			    sc->opp[n].uvolt_target);
360 	}
361 	free(opp, M_OFWPROP);
362 
363 	return (0);
364 }
365 
366 static int
367 cpufreq_dt_oppv2_parse(struct cpufreq_dt_softc *sc, phandle_t node)
368 {
369 	phandle_t opp, opp_table, opp_xref;
370 	pcell_t cell[2];
371 	uint32_t *volts, lat;
372 	int nvolt, i;
373 
374 	if (OF_getencprop(node, "operating-points-v2", &opp_xref,
375 	    sizeof(opp_xref)) == -1) {
376 		device_printf(sc->dev, "Cannot get xref to oppv2 table\n");
377 		return (ENXIO);
378 	}
379 
380 	opp_table = OF_node_from_xref(opp_xref);
381 	if (opp_table == opp_xref)
382 		return (ENXIO);
383 
384 	if (!OF_hasprop(opp_table, "opp-shared")) {
385 		device_printf(sc->dev, "Only opp-shared is supported\n");
386 		return (ENXIO);
387 	}
388 
389 	for (opp = OF_child(opp_table); opp > 0; opp = OF_peer(opp))
390 		sc->nopp += 1;
391 
392 	sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
393 
394 	for (i = 0, opp_table = OF_child(opp_table); opp_table > 0;
395 	     opp_table = OF_peer(opp_table), i++) {
396 		/* opp-hz is a required property */
397 		if (OF_getencprop(opp_table, "opp-hz", cell,
398 		    sizeof(cell)) == -1)
399 			continue;
400 
401 		sc->opp[i].freq = cell[0];
402 		sc->opp[i].freq <<= 32;
403 		sc->opp[i].freq |= cell[1];
404 
405 		if (OF_getencprop(opp_table, "clock-latency", &lat,
406 		    sizeof(lat)) == -1)
407 			sc->opp[i].clk_latency = CPUFREQ_VAL_UNKNOWN;
408 		else
409 			sc->opp[i].clk_latency = (int)lat;
410 
411 		if (OF_hasprop(opp_table, "turbo-mode"))
412 			sc->opp[i].turbo_mode = true;
413 		if (OF_hasprop(opp_table, "opp-suspend"))
414 			sc->opp[i].opp_suspend = true;
415 
416 		nvolt = OF_getencprop_alloc_multi(opp_table, "opp-microvolt",
417 		  sizeof(*volts), (void **)&volts);
418 		if (nvolt == 1) {
419 			sc->opp[i].uvolt_target = volts[0];
420 			sc->opp[i].uvolt_min = volts[0];
421 			sc->opp[i].uvolt_max = volts[0];
422 		} else if (nvolt == 3) {
423 			sc->opp[i].uvolt_target = volts[0];
424 			sc->opp[i].uvolt_min = volts[1];
425 			sc->opp[i].uvolt_max = volts[2];
426 		} else {
427 			device_printf(sc->dev,
428 			    "Wrong count of opp-microvolt property\n");
429 			OF_prop_free(volts);
430 			free(sc->opp, M_DEVBUF);
431 			return (ENXIO);
432 		}
433 		OF_prop_free(volts);
434 
435 		if (bootverbose)
436 			device_printf(sc->dev, "%ju.%03ju Mhz (%u uV)\n",
437 			    sc->opp[i].freq / 1000000,
438 			    sc->opp[i].freq % 1000000,
439 			    sc->opp[i].uvolt_target);
440 	}
441 	return (0);
442 }
443 
444 static int
445 cpufreq_dt_attach(device_t dev)
446 {
447 	struct cpufreq_dt_softc *sc;
448 	phandle_t node;
449 	phandle_t cnode, opp, copp;
450 	int cpu;
451 	uint64_t freq;
452 	int rv = 0;
453 	enum opp_version version;
454 
455 	sc = device_get_softc(dev);
456 	sc->dev = dev;
457 	node = ofw_bus_get_node(device_get_parent(dev));
458 
459 	if (regulator_get_by_ofw_property(dev, node,
460 	    "cpu-supply", &sc->reg) != 0) {
461 		if (regulator_get_by_ofw_property(dev, node,
462 		    "cpu0-supply", &sc->reg) != 0) {
463 			device_printf(dev, "no regulator for %s\n",
464 			    ofw_bus_get_name(device_get_parent(dev)));
465 			return (ENXIO);
466 		}
467 	}
468 
469 	if (clk_get_by_ofw_index(dev, node, 0, &sc->clk) != 0) {
470 		device_printf(dev, "no clock for %s\n",
471 		    ofw_bus_get_name(device_get_parent(dev)));
472 		regulator_release(sc->reg);
473 		return (ENXIO);
474 	}
475 
476 	if (OF_hasprop(node, "operating-points")) {
477 		version = OPP_V1;
478 		rv = cpufreq_dt_oppv1_parse(sc, node);
479 		if (rv != 0) {
480 			device_printf(dev, "Failed to parse opp-v1 table\n");
481 			return (rv);
482 		}
483 		OF_getencprop(node, "operating-points", &opp,
484 		    sizeof(opp));
485 	} else {
486 		version = OPP_V2;
487 		rv = cpufreq_dt_oppv2_parse(sc, node);
488 		if (rv != 0) {
489 			device_printf(dev, "Failed to parse opp-v2 table\n");
490 			return (rv);
491 		}
492 		OF_getencprop(node, "operating-points-v2", &opp,
493 		    sizeof(opp));
494 	}
495 
496 	/*
497 	 * Find all CPUs that share the same opp table
498 	 */
499 	CPU_ZERO(&sc->cpus);
500 	cpu = device_get_unit(device_get_parent(dev));
501 	for (cnode = node; cnode > 0; cnode = OF_peer(cnode), cpu++) {
502 		copp = -1;
503 		if (version == OPP_V1)
504 			OF_getencprop(cnode, "operating-points", &copp,
505 			    sizeof(copp));
506 		else if (version == OPP_V2)
507 			OF_getencprop(cnode, "operating-points-v2",
508 			    &copp, sizeof(copp));
509 		if (opp == copp)
510 			CPU_SET(cpu, &sc->cpus);
511 	}
512 
513 	if (clk_get_freq(sc->clk, &freq) == 0)
514 		cpufreq_dt_notify(dev, freq);
515 
516 	cpufreq_register(dev);
517 
518 	return (0);
519 }
520 
521 
522 static device_method_t cpufreq_dt_methods[] = {
523 	/* Device interface */
524 	DEVMETHOD(device_identify,	cpufreq_dt_identify),
525 	DEVMETHOD(device_probe,		cpufreq_dt_probe),
526 	DEVMETHOD(device_attach,	cpufreq_dt_attach),
527 
528 	/* cpufreq interface */
529 	DEVMETHOD(cpufreq_drv_get,	cpufreq_dt_get),
530 	DEVMETHOD(cpufreq_drv_set,	cpufreq_dt_set),
531 	DEVMETHOD(cpufreq_drv_type,	cpufreq_dt_type),
532 	DEVMETHOD(cpufreq_drv_settings,	cpufreq_dt_settings),
533 
534 	DEVMETHOD_END
535 };
536 
537 static driver_t cpufreq_dt_driver = {
538 	"cpufreq_dt",
539 	cpufreq_dt_methods,
540 	sizeof(struct cpufreq_dt_softc),
541 };
542 
543 static devclass_t cpufreq_dt_devclass;
544 
545 DRIVER_MODULE(cpufreq_dt, cpu, cpufreq_dt_driver, cpufreq_dt_devclass, 0, 0);
546 MODULE_VERSION(cpufreq_dt, 1);
547