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 27 /* 28 * Generic DT based cpufreq driver 29 */ 30 31 #include <sys/cdefs.h> 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 #include <sys/rman.h> 36 #include <sys/kernel.h> 37 #include <sys/module.h> 38 #include <sys/cpu.h> 39 #include <sys/cpuset.h> 40 #include <sys/smp.h> 41 42 #include <dev/ofw/ofw_bus.h> 43 #include <dev/ofw/ofw_bus_subr.h> 44 45 #include <dev/extres/clk/clk.h> 46 #include <dev/extres/regulator/regulator.h> 47 48 #include "cpufreq_if.h" 49 50 #if 0 51 #define DPRINTF(dev, msg...) device_printf(dev, "cpufreq_dt: " msg); 52 #else 53 #define DPRINTF(dev, msg...) 54 #endif 55 56 enum opp_version { 57 OPP_V1 = 1, 58 OPP_V2, 59 }; 60 61 struct cpufreq_dt_opp { 62 uint64_t freq; 63 uint32_t uvolt_target; 64 uint32_t uvolt_min; 65 uint32_t uvolt_max; 66 uint32_t uamps; 67 uint32_t clk_latency; 68 bool turbo_mode; 69 bool opp_suspend; 70 }; 71 72 #define CPUFREQ_DT_HAVE_REGULATOR(sc) ((sc)->reg != NULL) 73 74 struct cpufreq_dt_softc { 75 device_t dev; 76 clk_t clk; 77 regulator_t reg; 78 79 struct cpufreq_dt_opp *opp; 80 ssize_t nopp; 81 82 int cpu; 83 cpuset_t cpus; 84 }; 85 86 static void 87 cpufreq_dt_notify(device_t dev, uint64_t freq) 88 { 89 struct cpufreq_dt_softc *sc; 90 struct pcpu *pc; 91 int cpu; 92 93 sc = device_get_softc(dev); 94 95 CPU_FOREACH(cpu) { 96 if (CPU_ISSET(cpu, &sc->cpus)) { 97 pc = pcpu_find(cpu); 98 pc->pc_clock = freq; 99 } 100 } 101 } 102 103 static const struct cpufreq_dt_opp * 104 cpufreq_dt_find_opp(device_t dev, uint64_t freq) 105 { 106 struct cpufreq_dt_softc *sc; 107 uint64_t diff, best_diff; 108 ssize_t n, best_n; 109 110 sc = device_get_softc(dev); 111 112 diff = 0; 113 best_diff = ~0; 114 DPRINTF(dev, "Looking for freq %ju\n", freq); 115 for (n = 0; n < sc->nopp; n++) { 116 diff = abs64((int64_t)sc->opp[n].freq - (int64_t)freq); 117 DPRINTF(dev, "Testing %ju, diff is %ju\n", sc->opp[n].freq, diff); 118 if (diff < best_diff) { 119 best_diff = diff; 120 best_n = n; 121 DPRINTF(dev, "%ju is best for now\n", sc->opp[n].freq); 122 } 123 } 124 125 DPRINTF(dev, "Will use %ju\n", sc->opp[best_n].freq); 126 return (&sc->opp[best_n]); 127 } 128 129 static void 130 cpufreq_dt_opp_to_setting(device_t dev, const struct cpufreq_dt_opp *opp, 131 struct cf_setting *set) 132 { 133 134 memset(set, 0, sizeof(*set)); 135 set->freq = opp->freq / 1000000; 136 set->volts = opp->uvolt_target / 1000; 137 set->power = CPUFREQ_VAL_UNKNOWN; 138 set->lat = opp->clk_latency; 139 set->dev = dev; 140 } 141 142 static int 143 cpufreq_dt_get(device_t dev, struct cf_setting *set) 144 { 145 struct cpufreq_dt_softc *sc; 146 const struct cpufreq_dt_opp *opp; 147 uint64_t freq; 148 149 sc = device_get_softc(dev); 150 151 DPRINTF(dev, "cpufreq_dt_get\n"); 152 if (clk_get_freq(sc->clk, &freq) != 0) 153 return (ENXIO); 154 155 opp = cpufreq_dt_find_opp(dev, freq); 156 if (opp == NULL) { 157 device_printf(dev, "Can't find the current freq in opp\n"); 158 return (ENOENT); 159 } 160 161 cpufreq_dt_opp_to_setting(dev, opp, set); 162 163 DPRINTF(dev, "Current freq %dMhz\n", set->freq); 164 return (0); 165 } 166 167 static int 168 cpufreq_dt_set(device_t dev, const struct cf_setting *set) 169 { 170 struct cpufreq_dt_softc *sc; 171 const struct cpufreq_dt_opp *opp, *copp; 172 uint64_t freq; 173 int uvolt, error; 174 175 sc = device_get_softc(dev); 176 177 DPRINTF(dev, "Working on cpu %d\n", sc->cpu); 178 DPRINTF(dev, "We have %d cpu on this dev\n", CPU_COUNT(&sc->cpus)); 179 if (!CPU_ISSET(sc->cpu, &sc->cpus)) { 180 DPRINTF(dev, "Not for this CPU\n"); 181 return (0); 182 } 183 184 if (clk_get_freq(sc->clk, &freq) != 0) { 185 device_printf(dev, "Can't get current clk freq\n"); 186 return (ENXIO); 187 } 188 189 /* 190 * Only do the regulator work if it's required. 191 */ 192 if (CPUFREQ_DT_HAVE_REGULATOR(sc)) { 193 /* Try to get current valtage by using regulator first. */ 194 error = regulator_get_voltage(sc->reg, &uvolt); 195 if (error != 0) { 196 /* 197 * Try oppoints table as backup way. However, 198 * this is insufficient because the actual processor 199 * frequency may not be in the table. PLL frequency 200 * granularity can be different that granularity of 201 * oppoint table. 202 */ 203 copp = cpufreq_dt_find_opp(sc->dev, freq); 204 if (copp == NULL) { 205 device_printf(dev, 206 "Can't find the current freq in opp\n"); 207 return (ENOENT); 208 } 209 uvolt = copp->uvolt_target; 210 } 211 } else 212 uvolt = 0; 213 214 opp = cpufreq_dt_find_opp(sc->dev, set->freq * 1000000); 215 if (opp == NULL) { 216 device_printf(dev, "Couldn't find an opp for this freq\n"); 217 return (EINVAL); 218 } 219 DPRINTF(sc->dev, "Current freq %ju, uvolt: %d\n", freq, uvolt); 220 DPRINTF(sc->dev, "Target freq %ju, , uvolt: %d\n", 221 opp->freq, opp->uvolt_target); 222 223 if (CPUFREQ_DT_HAVE_REGULATOR(sc) && (uvolt < opp->uvolt_target)) { 224 DPRINTF(dev, "Changing regulator from %u to %u\n", 225 uvolt, opp->uvolt_target); 226 error = regulator_set_voltage(sc->reg, 227 opp->uvolt_min, 228 opp->uvolt_max); 229 if (error != 0) { 230 DPRINTF(dev, "Failed, backout\n"); 231 return (ENXIO); 232 } 233 } 234 235 DPRINTF(dev, "Setting clk to %ju\n", opp->freq); 236 error = clk_set_freq(sc->clk, opp->freq, CLK_SET_ROUND_DOWN); 237 if (error != 0) { 238 DPRINTF(dev, "Failed, backout\n"); 239 /* Restore previous voltage (best effort) */ 240 if (CPUFREQ_DT_HAVE_REGULATOR(sc)) 241 error = regulator_set_voltage(sc->reg, 242 copp->uvolt_min, 243 copp->uvolt_max); 244 return (ENXIO); 245 } 246 247 if (CPUFREQ_DT_HAVE_REGULATOR(sc) && (uvolt > opp->uvolt_target)) { 248 DPRINTF(dev, "Changing regulator from %u to %u\n", 249 uvolt, opp->uvolt_target); 250 error = regulator_set_voltage(sc->reg, 251 opp->uvolt_min, 252 opp->uvolt_max); 253 if (error != 0) { 254 DPRINTF(dev, "Failed to switch regulator to %d\n", 255 opp->uvolt_target); 256 /* Restore previous CPU frequency (best effort) */ 257 (void)clk_set_freq(sc->clk, copp->freq, 0); 258 return (ENXIO); 259 } 260 } 261 262 if (clk_get_freq(sc->clk, &freq) == 0) 263 cpufreq_dt_notify(dev, freq); 264 265 return (0); 266 } 267 268 static int 269 cpufreq_dt_type(device_t dev, int *type) 270 { 271 if (type == NULL) 272 return (EINVAL); 273 274 *type = CPUFREQ_TYPE_ABSOLUTE; 275 return (0); 276 } 277 278 static int 279 cpufreq_dt_settings(device_t dev, struct cf_setting *sets, int *count) 280 { 281 struct cpufreq_dt_softc *sc; 282 ssize_t n; 283 284 DPRINTF(dev, "cpufreq_dt_settings\n"); 285 if (sets == NULL || count == NULL) 286 return (EINVAL); 287 288 sc = device_get_softc(dev); 289 290 if (*count < sc->nopp) { 291 *count = (int)sc->nopp; 292 return (E2BIG); 293 } 294 295 for (n = 0; n < sc->nopp; n++) 296 cpufreq_dt_opp_to_setting(dev, &sc->opp[n], &sets[n]); 297 298 *count = (int)sc->nopp; 299 300 return (0); 301 } 302 303 static void 304 cpufreq_dt_identify(driver_t *driver, device_t parent) 305 { 306 phandle_t node; 307 308 /* Properties must be listed under node /cpus/cpu@0 */ 309 node = ofw_bus_get_node(parent); 310 311 /* The cpu@0 node must have the following properties */ 312 if (!OF_hasprop(node, "clocks")) 313 return; 314 315 if (!OF_hasprop(node, "operating-points") && 316 !OF_hasprop(node, "operating-points-v2")) 317 return; 318 319 if (device_find_child(parent, "cpufreq_dt", -1) != NULL) 320 return; 321 322 if (BUS_ADD_CHILD(parent, 0, "cpufreq_dt", device_get_unit(parent)) 323 == NULL) 324 device_printf(parent, "add cpufreq_dt child failed\n"); 325 } 326 327 static int 328 cpufreq_dt_probe(device_t dev) 329 { 330 phandle_t node; 331 332 node = ofw_bus_get_node(device_get_parent(dev)); 333 334 /* 335 * Note - supply isn't required here for probe; we'll check 336 * it out in more detail during attach. 337 */ 338 if (!OF_hasprop(node, "clocks")) 339 return (ENXIO); 340 341 if (!OF_hasprop(node, "operating-points") && 342 !OF_hasprop(node, "operating-points-v2")) 343 return (ENXIO); 344 345 device_set_desc(dev, "Generic cpufreq driver"); 346 return (BUS_PROBE_GENERIC); 347 } 348 349 static int 350 cpufreq_dt_oppv1_parse(struct cpufreq_dt_softc *sc, phandle_t node) 351 { 352 uint32_t *opp, lat; 353 ssize_t n; 354 355 sc->nopp = OF_getencprop_alloc_multi(node, "operating-points", 356 sizeof(uint32_t) * 2, (void **)&opp); 357 if (sc->nopp == -1) 358 return (ENXIO); 359 360 if (OF_getencprop(node, "clock-latency", &lat, sizeof(lat)) == -1) 361 lat = CPUFREQ_VAL_UNKNOWN; 362 363 sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK); 364 365 for (n = 0; n < sc->nopp; n++) { 366 sc->opp[n].freq = opp[n * 2 + 0] * 1000; 367 sc->opp[n].uvolt_min = opp[n * 2 + 1]; 368 sc->opp[n].uvolt_max = sc->opp[n].uvolt_min; 369 sc->opp[n].uvolt_target = sc->opp[n].uvolt_min; 370 sc->opp[n].clk_latency = lat; 371 372 if (bootverbose) 373 device_printf(sc->dev, "%ju.%03ju MHz, %u uV\n", 374 sc->opp[n].freq / 1000000, 375 sc->opp[n].freq % 1000000, 376 sc->opp[n].uvolt_target); 377 } 378 free(opp, M_OFWPROP); 379 380 return (0); 381 } 382 383 static int 384 cpufreq_dt_oppv2_parse(struct cpufreq_dt_softc *sc, phandle_t node) 385 { 386 phandle_t opp, opp_table, opp_xref; 387 pcell_t cell[2]; 388 uint32_t *volts, lat; 389 int nvolt, i; 390 391 /* 392 * operating-points-v2 does not require the voltage entries 393 * and a regulator. So, it's OK if they're not there. 394 */ 395 if (OF_getencprop(node, "operating-points-v2", &opp_xref, 396 sizeof(opp_xref)) == -1) { 397 device_printf(sc->dev, "Cannot get xref to oppv2 table\n"); 398 return (ENXIO); 399 } 400 401 opp_table = OF_node_from_xref(opp_xref); 402 if (opp_table == opp_xref) 403 return (ENXIO); 404 405 if (!OF_hasprop(opp_table, "opp-shared")) { 406 device_printf(sc->dev, "Only opp-shared is supported\n"); 407 return (ENXIO); 408 } 409 410 for (opp = OF_child(opp_table); opp > 0; opp = OF_peer(opp)) 411 sc->nopp += 1; 412 413 sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK); 414 415 for (i = 0, opp_table = OF_child(opp_table); opp_table > 0; 416 opp_table = OF_peer(opp_table), i++) { 417 /* opp-hz is a required property */ 418 if (OF_getencprop(opp_table, "opp-hz", cell, 419 sizeof(cell)) == -1) 420 continue; 421 422 sc->opp[i].freq = cell[0]; 423 sc->opp[i].freq <<= 32; 424 sc->opp[i].freq |= cell[1]; 425 426 if (OF_getencprop(opp_table, "clock-latency", &lat, 427 sizeof(lat)) == -1) 428 sc->opp[i].clk_latency = CPUFREQ_VAL_UNKNOWN; 429 else 430 sc->opp[i].clk_latency = (int)lat; 431 432 if (OF_hasprop(opp_table, "turbo-mode")) 433 sc->opp[i].turbo_mode = true; 434 if (OF_hasprop(opp_table, "opp-suspend")) 435 sc->opp[i].opp_suspend = true; 436 437 if (CPUFREQ_DT_HAVE_REGULATOR(sc)) { 438 nvolt = OF_getencprop_alloc_multi(opp_table, 439 "opp-microvolt", sizeof(*volts), (void **)&volts); 440 if (nvolt == 1) { 441 sc->opp[i].uvolt_target = volts[0]; 442 sc->opp[i].uvolt_min = volts[0]; 443 sc->opp[i].uvolt_max = volts[0]; 444 } else if (nvolt == 3) { 445 sc->opp[i].uvolt_target = volts[0]; 446 sc->opp[i].uvolt_min = volts[1]; 447 sc->opp[i].uvolt_max = volts[2]; 448 } else { 449 device_printf(sc->dev, 450 "Wrong count of opp-microvolt property\n"); 451 OF_prop_free(volts); 452 free(sc->opp, M_DEVBUF); 453 return (ENXIO); 454 } 455 OF_prop_free(volts); 456 } else { 457 /* No regulator required; don't add anything */ 458 sc->opp[i].uvolt_target = 0; 459 sc->opp[i].uvolt_min = 0; 460 sc->opp[i].uvolt_max = 0; 461 } 462 463 if (bootverbose) 464 device_printf(sc->dev, "%ju.%03ju Mhz (%u uV)\n", 465 sc->opp[i].freq / 1000000, 466 sc->opp[i].freq % 1000000, 467 sc->opp[i].uvolt_target); 468 } 469 return (0); 470 } 471 472 static int 473 cpufreq_dt_attach(device_t dev) 474 { 475 struct cpufreq_dt_softc *sc; 476 phandle_t node; 477 phandle_t cnode, opp, copp; 478 int cpu; 479 uint64_t freq; 480 int rv = 0; 481 char device_type[16]; 482 enum opp_version version; 483 484 sc = device_get_softc(dev); 485 sc->dev = dev; 486 node = ofw_bus_get_node(device_get_parent(dev)); 487 sc->cpu = device_get_unit(device_get_parent(dev)); 488 sc->reg = NULL; 489 490 DPRINTF(dev, "cpu=%d\n", sc->cpu); 491 if (sc->cpu >= mp_ncpus) { 492 device_printf(dev, "Not attaching as cpu is not present\n"); 493 rv = ENXIO; 494 goto error; 495 } 496 497 /* 498 * Cache if we have the regulator supply but don't error out 499 * quite yet. If it's operating-points-v2 then regulator 500 * and voltage entries are optional. 501 */ 502 if (regulator_get_by_ofw_property(dev, node, "cpu-supply", 503 &sc->reg) == 0) 504 device_printf(dev, "Found cpu-supply\n"); 505 else if (regulator_get_by_ofw_property(dev, node, "cpu0-supply", 506 &sc->reg) == 0) 507 device_printf(dev, "Found cpu0-supply\n"); 508 509 /* 510 * Determine which operating mode we're in. Error out if we expect 511 * a regulator but we're not getting it. 512 */ 513 if (OF_hasprop(node, "operating-points")) 514 version = OPP_V1; 515 else if (OF_hasprop(node, "operating-points-v2")) 516 version = OPP_V2; 517 else { 518 device_printf(dev, 519 "didn't find a valid operating-points or v2 node\n"); 520 rv = ENXIO; 521 goto error; 522 } 523 524 /* 525 * Now, we only enforce needing a regulator for v1. 526 */ 527 if ((version == OPP_V1) && !CPUFREQ_DT_HAVE_REGULATOR(sc)) { 528 device_printf(dev, "no regulator for %s\n", 529 ofw_bus_get_name(device_get_parent(dev))); 530 rv = ENXIO; 531 goto error; 532 } 533 534 if (clk_get_by_ofw_index(dev, node, 0, &sc->clk) != 0) { 535 device_printf(dev, "no clock for %s\n", 536 ofw_bus_get_name(device_get_parent(dev))); 537 rv = ENXIO; 538 goto error; 539 } 540 541 if (version == OPP_V1) { 542 rv = cpufreq_dt_oppv1_parse(sc, node); 543 if (rv != 0) { 544 device_printf(dev, "Failed to parse opp-v1 table\n"); 545 goto error; 546 } 547 OF_getencprop(node, "operating-points", &opp, 548 sizeof(opp)); 549 } else if (version == OPP_V2) { 550 rv = cpufreq_dt_oppv2_parse(sc, node); 551 if (rv != 0) { 552 device_printf(dev, "Failed to parse opp-v2 table\n"); 553 goto error; 554 } 555 OF_getencprop(node, "operating-points-v2", &opp, 556 sizeof(opp)); 557 } else { 558 device_printf(dev, "operating points version is incorrect\n"); 559 goto error; 560 } 561 562 /* 563 * Find all CPUs that share the same opp table 564 */ 565 CPU_ZERO(&sc->cpus); 566 cnode = OF_parent(node); 567 for (cpu = 0, cnode = OF_child(cnode); cnode > 0; cnode = OF_peer(cnode)) { 568 if (OF_getprop(cnode, "device_type", device_type, sizeof(device_type)) <= 0) 569 continue; 570 if (strcmp(device_type, "cpu") != 0) 571 continue; 572 if (cpu == sc->cpu) { 573 DPRINTF(dev, "Skipping our cpu\n"); 574 CPU_SET(cpu, &sc->cpus); 575 cpu++; 576 continue; 577 } 578 DPRINTF(dev, "Testing CPU %d\n", cpu); 579 copp = -1; 580 if (version == OPP_V1) 581 OF_getencprop(cnode, "operating-points", &copp, 582 sizeof(copp)); 583 else if (version == OPP_V2) 584 OF_getencprop(cnode, "operating-points-v2", 585 &copp, sizeof(copp)); 586 if (opp == copp) { 587 DPRINTF(dev, "CPU %d is using the same opp as this one (%d)\n", 588 cpu, sc->cpu); 589 CPU_SET(cpu, &sc->cpus); 590 } 591 cpu++; 592 } 593 594 if (clk_get_freq(sc->clk, &freq) == 0) 595 cpufreq_dt_notify(dev, freq); 596 597 cpufreq_register(dev); 598 599 return (0); 600 error: 601 if (CPUFREQ_DT_HAVE_REGULATOR(sc)) 602 regulator_release(sc->reg); 603 return (rv); 604 } 605 606 static device_method_t cpufreq_dt_methods[] = { 607 /* Device interface */ 608 DEVMETHOD(device_identify, cpufreq_dt_identify), 609 DEVMETHOD(device_probe, cpufreq_dt_probe), 610 DEVMETHOD(device_attach, cpufreq_dt_attach), 611 612 /* cpufreq interface */ 613 DEVMETHOD(cpufreq_drv_get, cpufreq_dt_get), 614 DEVMETHOD(cpufreq_drv_set, cpufreq_dt_set), 615 DEVMETHOD(cpufreq_drv_type, cpufreq_dt_type), 616 DEVMETHOD(cpufreq_drv_settings, cpufreq_dt_settings), 617 618 DEVMETHOD_END 619 }; 620 621 static driver_t cpufreq_dt_driver = { 622 "cpufreq_dt", 623 cpufreq_dt_methods, 624 sizeof(struct cpufreq_dt_softc), 625 }; 626 627 DRIVER_MODULE(cpufreq_dt, cpu, cpufreq_dt_driver, 0, 0); 628 MODULE_VERSION(cpufreq_dt, 1); 629