xref: /freebsd/sys/dev/cpufreq/cpufreq_dt.c (revision 6419bb52)
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 	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 	DPRINTF(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 	DPRINTF(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 	DPRINTF(dev, "Working on cpu %d\n", sc->cpu);
174 	DPRINTF(dev, "We have %d cpu on this dev\n", CPU_COUNT(&sc->cpus));
175 	if (!CPU_ISSET(sc->cpu, &sc->cpus)) {
176 		DPRINTF(dev, "Not for this CPU\n");
177 		return (0);
178 	}
179 
180 	if (clk_get_freq(sc->clk, &freq) != 0) {
181 		device_printf(dev, "Can't get current clk freq\n");
182 		return (ENXIO);
183 	}
184 	/* Try to get current valtage by using regulator first. */
185 	error = regulator_get_voltage(sc->reg, &uvolt);
186 	if (error != 0) {
187 		/*
188 		 * Try oppoints table as backup way. However,
189 		 * this is insufficient because the actual processor
190 		 * frequency may not be in the table. PLL frequency
191 		 * granularity can be different that granularity of
192 		 * oppoint table.
193 		 */
194 		copp = cpufreq_dt_find_opp(sc->dev, freq);
195 		if (copp == NULL) {
196 			device_printf(dev,
197 			    "Can't find the current freq in opp\n");
198 			return (ENOENT);
199 		}
200 		uvolt = copp->uvolt_target;
201 	}
202 
203 	opp = cpufreq_dt_find_opp(sc->dev, set->freq * 1000000);
204 	if (opp == NULL) {
205 		device_printf(dev, "Couldn't find an opp for this freq\n");
206 		return (EINVAL);
207 	}
208 	DPRINTF(sc->dev, "Current freq %ju, uvolt: %d\n", freq, uvolt);
209 	DPRINTF(sc->dev, "Target freq %ju, , uvolt: %d\n",
210 	    opp->freq, opp->uvolt_target);
211 
212 	if (uvolt < opp->uvolt_target) {
213 		DPRINTF(dev, "Changing regulator from %u to %u\n",
214 		    uvolt, opp->uvolt_target);
215 		error = regulator_set_voltage(sc->reg,
216 		    opp->uvolt_min,
217 		    opp->uvolt_max);
218 		if (error != 0) {
219 			DPRINTF(dev, "Failed, backout\n");
220 			return (ENXIO);
221 		}
222 	}
223 
224 	DPRINTF(dev, "Setting clk to %ju\n", opp->freq);
225 	error = clk_set_freq(sc->clk, opp->freq, CLK_SET_ROUND_DOWN);
226 	if (error != 0) {
227 		DPRINTF(dev, "Failed, backout\n");
228 		/* Restore previous voltage (best effort) */
229 		error = regulator_set_voltage(sc->reg,
230 		    copp->uvolt_min,
231 		    copp->uvolt_max);
232 		return (ENXIO);
233 	}
234 
235 	if (uvolt > opp->uvolt_target) {
236 		DPRINTF(dev, "Changing regulator from %u to %u\n",
237 		    uvolt, opp->uvolt_target);
238 		error = regulator_set_voltage(sc->reg,
239 		    opp->uvolt_min,
240 		    opp->uvolt_max);
241 		if (error != 0) {
242 			DPRINTF(dev, "Failed to switch regulator to %d\n",
243 			    opp->uvolt_target);
244 			/* Restore previous CPU frequency (best effort) */
245 			(void)clk_set_freq(sc->clk, copp->freq, 0);
246 			return (ENXIO);
247 		}
248 	}
249 
250 	if (clk_get_freq(sc->clk, &freq) == 0)
251 		cpufreq_dt_notify(dev, freq);
252 
253 	return (0);
254 }
255 
256 
257 static int
258 cpufreq_dt_type(device_t dev, int *type)
259 {
260 	if (type == NULL)
261 		return (EINVAL);
262 
263 	*type = CPUFREQ_TYPE_ABSOLUTE;
264 	return (0);
265 }
266 
267 static int
268 cpufreq_dt_settings(device_t dev, struct cf_setting *sets, int *count)
269 {
270 	struct cpufreq_dt_softc *sc;
271 	ssize_t n;
272 
273 	DPRINTF(dev, "cpufreq_dt_settings\n");
274 	if (sets == NULL || count == NULL)
275 		return (EINVAL);
276 
277 	sc = device_get_softc(dev);
278 
279 	if (*count < sc->nopp) {
280 		*count = (int)sc->nopp;
281 		return (E2BIG);
282 	}
283 
284 	for (n = 0; n < sc->nopp; n++)
285 		cpufreq_dt_opp_to_setting(dev, &sc->opp[n], &sets[n]);
286 
287 	*count = (int)sc->nopp;
288 
289 	return (0);
290 }
291 
292 static void
293 cpufreq_dt_identify(driver_t *driver, device_t parent)
294 {
295 	phandle_t node;
296 
297 	/* Properties must be listed under node /cpus/cpu@0 */
298 	node = ofw_bus_get_node(parent);
299 
300 	/* The cpu@0 node must have the following properties */
301 	if (!OF_hasprop(node, "clocks") ||
302 	    (!OF_hasprop(node, "cpu-supply") &&
303 	    !OF_hasprop(node, "cpu0-supply")))
304 		return;
305 
306 	if (!OF_hasprop(node, "operating-points") &&
307 	    !OF_hasprop(node, "operating-points-v2"))
308 		return;
309 
310 	if (device_find_child(parent, "cpufreq_dt", -1) != NULL)
311 		return;
312 
313 	if (BUS_ADD_CHILD(parent, 0, "cpufreq_dt", -1) == NULL)
314 		device_printf(parent, "add cpufreq_dt child failed\n");
315 }
316 
317 static int
318 cpufreq_dt_probe(device_t dev)
319 {
320 	phandle_t node;
321 
322 	node = ofw_bus_get_node(device_get_parent(dev));
323 
324 	if (!OF_hasprop(node, "clocks") ||
325 	    (!OF_hasprop(node, "cpu-supply") &&
326 	    !OF_hasprop(node, "cpu0-supply")))
327 
328 		return (ENXIO);
329 
330 	if (!OF_hasprop(node, "operating-points") &&
331 	  !OF_hasprop(node, "operating-points-v2"))
332 		return (ENXIO);
333 
334 	device_set_desc(dev, "Generic cpufreq driver");
335 	return (BUS_PROBE_GENERIC);
336 }
337 
338 static int
339 cpufreq_dt_oppv1_parse(struct cpufreq_dt_softc *sc, phandle_t node)
340 {
341 	uint32_t *opp, lat;
342 	ssize_t n;
343 
344 	sc->nopp = OF_getencprop_alloc_multi(node, "operating-points",
345 	    sizeof(uint32_t) * 2, (void **)&opp);
346 	if (sc->nopp == -1)
347 		return (ENXIO);
348 
349 	if (OF_getencprop(node, "clock-latency", &lat, sizeof(lat)) == -1)
350 		lat = CPUFREQ_VAL_UNKNOWN;
351 
352 	sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
353 
354 	for (n = 0; n < sc->nopp; n++) {
355 		sc->opp[n].freq = opp[n * 2 + 0] * 1000;
356 		sc->opp[n].uvolt_min = opp[n * 2 + 1];
357 		sc->opp[n].uvolt_max = sc->opp[n].uvolt_min;
358 		sc->opp[n].uvolt_target = sc->opp[n].uvolt_min;
359 		sc->opp[n].clk_latency = lat;
360 
361 		if (bootverbose)
362 			device_printf(sc->dev, "%ju.%03ju MHz, %u uV\n",
363 			    sc->opp[n].freq / 1000000,
364 			    sc->opp[n].freq % 1000000,
365 			    sc->opp[n].uvolt_target);
366 	}
367 	free(opp, M_OFWPROP);
368 
369 	return (0);
370 }
371 
372 static int
373 cpufreq_dt_oppv2_parse(struct cpufreq_dt_softc *sc, phandle_t node)
374 {
375 	phandle_t opp, opp_table, opp_xref;
376 	pcell_t cell[2];
377 	uint32_t *volts, lat;
378 	int nvolt, i;
379 
380 	if (OF_getencprop(node, "operating-points-v2", &opp_xref,
381 	    sizeof(opp_xref)) == -1) {
382 		device_printf(sc->dev, "Cannot get xref to oppv2 table\n");
383 		return (ENXIO);
384 	}
385 
386 	opp_table = OF_node_from_xref(opp_xref);
387 	if (opp_table == opp_xref)
388 		return (ENXIO);
389 
390 	if (!OF_hasprop(opp_table, "opp-shared")) {
391 		device_printf(sc->dev, "Only opp-shared is supported\n");
392 		return (ENXIO);
393 	}
394 
395 	for (opp = OF_child(opp_table); opp > 0; opp = OF_peer(opp))
396 		sc->nopp += 1;
397 
398 	sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
399 
400 	for (i = 0, opp_table = OF_child(opp_table); opp_table > 0;
401 	     opp_table = OF_peer(opp_table), i++) {
402 		/* opp-hz is a required property */
403 		if (OF_getencprop(opp_table, "opp-hz", cell,
404 		    sizeof(cell)) == -1)
405 			continue;
406 
407 		sc->opp[i].freq = cell[0];
408 		sc->opp[i].freq <<= 32;
409 		sc->opp[i].freq |= cell[1];
410 
411 		if (OF_getencprop(opp_table, "clock-latency", &lat,
412 		    sizeof(lat)) == -1)
413 			sc->opp[i].clk_latency = CPUFREQ_VAL_UNKNOWN;
414 		else
415 			sc->opp[i].clk_latency = (int)lat;
416 
417 		if (OF_hasprop(opp_table, "turbo-mode"))
418 			sc->opp[i].turbo_mode = true;
419 		if (OF_hasprop(opp_table, "opp-suspend"))
420 			sc->opp[i].opp_suspend = true;
421 
422 		nvolt = OF_getencprop_alloc_multi(opp_table, "opp-microvolt",
423 		  sizeof(*volts), (void **)&volts);
424 		if (nvolt == 1) {
425 			sc->opp[i].uvolt_target = volts[0];
426 			sc->opp[i].uvolt_min = volts[0];
427 			sc->opp[i].uvolt_max = volts[0];
428 		} else if (nvolt == 3) {
429 			sc->opp[i].uvolt_target = volts[0];
430 			sc->opp[i].uvolt_min = volts[1];
431 			sc->opp[i].uvolt_max = volts[2];
432 		} else {
433 			device_printf(sc->dev,
434 			    "Wrong count of opp-microvolt property\n");
435 			OF_prop_free(volts);
436 			free(sc->opp, M_DEVBUF);
437 			return (ENXIO);
438 		}
439 		OF_prop_free(volts);
440 
441 		if (bootverbose)
442 			device_printf(sc->dev, "%ju.%03ju Mhz (%u uV)\n",
443 			    sc->opp[i].freq / 1000000,
444 			    sc->opp[i].freq % 1000000,
445 			    sc->opp[i].uvolt_target);
446 	}
447 	return (0);
448 }
449 
450 static int
451 cpufreq_dt_attach(device_t dev)
452 {
453 	struct cpufreq_dt_softc *sc;
454 	phandle_t node;
455 	phandle_t cnode, opp, copp;
456 	int cpu;
457 	uint64_t freq;
458 	int rv = 0;
459 	char device_type[16];
460 	enum opp_version version;
461 
462 	sc = device_get_softc(dev);
463 	sc->dev = dev;
464 	node = ofw_bus_get_node(device_get_parent(dev));
465 	sc->cpu = device_get_unit(device_get_parent(dev));
466 
467 	DPRINTF(dev, "cpu=%d\n", sc->cpu);
468 	if (sc->cpu >= mp_ncpus) {
469 		device_printf(dev, "Not attaching as cpu is not present\n");
470 		return (ENXIO);
471 	}
472 
473 	if (regulator_get_by_ofw_property(dev, node,
474 	    "cpu-supply", &sc->reg) != 0) {
475 		if (regulator_get_by_ofw_property(dev, node,
476 		    "cpu0-supply", &sc->reg) != 0) {
477 			device_printf(dev, "no regulator for %s\n",
478 			    ofw_bus_get_name(device_get_parent(dev)));
479 			return (ENXIO);
480 		}
481 	}
482 
483 	if (clk_get_by_ofw_index(dev, node, 0, &sc->clk) != 0) {
484 		device_printf(dev, "no clock for %s\n",
485 		    ofw_bus_get_name(device_get_parent(dev)));
486 		regulator_release(sc->reg);
487 		return (ENXIO);
488 	}
489 
490 	if (OF_hasprop(node, "operating-points")) {
491 		version = OPP_V1;
492 		rv = cpufreq_dt_oppv1_parse(sc, node);
493 		if (rv != 0) {
494 			device_printf(dev, "Failed to parse opp-v1 table\n");
495 			return (rv);
496 		}
497 		OF_getencprop(node, "operating-points", &opp,
498 		    sizeof(opp));
499 	} else {
500 		version = OPP_V2;
501 		rv = cpufreq_dt_oppv2_parse(sc, node);
502 		if (rv != 0) {
503 			device_printf(dev, "Failed to parse opp-v2 table\n");
504 			return (rv);
505 		}
506 		OF_getencprop(node, "operating-points-v2", &opp,
507 		    sizeof(opp));
508 	}
509 
510 	/*
511 	 * Find all CPUs that share the same opp table
512 	 */
513 	CPU_ZERO(&sc->cpus);
514 	cnode = OF_parent(node);
515 	for (cpu = 0, cnode = OF_child(cnode); cnode > 0; cnode = OF_peer(cnode)) {
516 		if (OF_getprop(cnode, "device_type", device_type, sizeof(device_type)) <= 0)
517 			continue;
518 		if (strcmp(device_type, "cpu") != 0)
519 			continue;
520 		if (cpu == sc->cpu) {
521 			DPRINTF(dev, "Skipping our cpu\n");
522 			CPU_SET(cpu, &sc->cpus);
523 			cpu++;
524 			continue;
525 		}
526 		DPRINTF(dev, "Testing CPU %d\n", cpu);
527 		copp = -1;
528 		if (version == OPP_V1)
529 			OF_getencprop(cnode, "operating-points", &copp,
530 			    sizeof(copp));
531 		else if (version == OPP_V2)
532 			OF_getencprop(cnode, "operating-points-v2",
533 			    &copp, sizeof(copp));
534 		if (opp == copp) {
535 			DPRINTF(dev, "CPU %d is using the same opp as this one (%d)\n",
536 			    cpu, sc->cpu);
537 			CPU_SET(cpu, &sc->cpus);
538 		}
539 		cpu++;
540 	}
541 
542 	if (clk_get_freq(sc->clk, &freq) == 0)
543 		cpufreq_dt_notify(dev, freq);
544 
545 	cpufreq_register(dev);
546 
547 	return (0);
548 }
549 
550 
551 static device_method_t cpufreq_dt_methods[] = {
552 	/* Device interface */
553 	DEVMETHOD(device_identify,	cpufreq_dt_identify),
554 	DEVMETHOD(device_probe,		cpufreq_dt_probe),
555 	DEVMETHOD(device_attach,	cpufreq_dt_attach),
556 
557 	/* cpufreq interface */
558 	DEVMETHOD(cpufreq_drv_get,	cpufreq_dt_get),
559 	DEVMETHOD(cpufreq_drv_set,	cpufreq_dt_set),
560 	DEVMETHOD(cpufreq_drv_type,	cpufreq_dt_type),
561 	DEVMETHOD(cpufreq_drv_settings,	cpufreq_dt_settings),
562 
563 	DEVMETHOD_END
564 };
565 
566 static driver_t cpufreq_dt_driver = {
567 	"cpufreq_dt",
568 	cpufreq_dt_methods,
569 	sizeof(struct cpufreq_dt_softc),
570 };
571 
572 static devclass_t cpufreq_dt_devclass;
573 
574 DRIVER_MODULE(cpufreq_dt, cpu, cpufreq_dt_driver, cpufreq_dt_devclass, 0, 0);
575 MODULE_VERSION(cpufreq_dt, 1);
576