1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * CPPC (Collaborative Processor Performance Control) driver for 4 * interfacing with the CPUfreq layer and governors. See 5 * cppc_acpi.c for CPPC specific methods. 6 * 7 * (C) Copyright 2014, 2015 Linaro Ltd. 8 * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org> 9 */ 10 11 #define pr_fmt(fmt) "CPPC Cpufreq:" fmt 12 13 #include <linux/arch_topology.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/delay.h> 17 #include <linux/cpu.h> 18 #include <linux/cpufreq.h> 19 #include <linux/irq_work.h> 20 #include <linux/kthread.h> 21 #include <linux/time.h> 22 #include <linux/vmalloc.h> 23 #include <uapi/linux/sched/types.h> 24 25 #include <linux/unaligned.h> 26 27 #include <acpi/cppc_acpi.h> 28 29 static struct cpufreq_driver cppc_cpufreq_driver; 30 31 #ifdef CONFIG_ACPI_CPPC_CPUFREQ_FIE 32 static enum { 33 FIE_UNSET = -1, 34 FIE_ENABLED, 35 FIE_DISABLED 36 } fie_disabled = FIE_UNSET; 37 38 module_param(fie_disabled, int, 0444); 39 MODULE_PARM_DESC(fie_disabled, "Disable Frequency Invariance Engine (FIE)"); 40 41 /* Frequency invariance support */ 42 struct cppc_freq_invariance { 43 int cpu; 44 struct irq_work irq_work; 45 struct kthread_work work; 46 struct cppc_perf_fb_ctrs prev_perf_fb_ctrs; 47 struct cppc_cpudata *cpu_data; 48 }; 49 50 static DEFINE_PER_CPU(struct cppc_freq_invariance, cppc_freq_inv); 51 static struct kthread_worker *kworker_fie; 52 53 static int cppc_perf_from_fbctrs(u64 reference_perf, 54 struct cppc_perf_fb_ctrs *fb_ctrs_t0, 55 struct cppc_perf_fb_ctrs *fb_ctrs_t1); 56 57 /** 58 * __cppc_scale_freq_tick - CPPC arch_freq_scale updater for frequency invariance 59 * @cppc_fi: per-cpu CPPC FIE data. 60 * 61 * The CPPC driver registers itself with the topology core to provide its own 62 * implementation (cppc_scale_freq_tick()) of topology_scale_freq_tick() which 63 * gets called by the scheduler on every tick. 64 * 65 * Note that the arch specific counters have higher priority than CPPC counters, 66 * if available, though the CPPC driver doesn't need to have any special 67 * handling for that. 68 */ 69 static void __cppc_scale_freq_tick(struct cppc_freq_invariance *cppc_fi) 70 { 71 struct cppc_perf_fb_ctrs fb_ctrs = {0}; 72 struct cppc_cpudata *cpu_data; 73 unsigned long local_freq_scale; 74 u64 perf, ref_perf; 75 76 cpu_data = cppc_fi->cpu_data; 77 78 if (cppc_get_perf_ctrs(cppc_fi->cpu, &fb_ctrs)) { 79 pr_warn("%s: failed to read perf counters\n", __func__); 80 return; 81 } 82 83 ref_perf = cpu_data->perf_caps.reference_perf; 84 perf = cppc_perf_from_fbctrs(ref_perf, 85 &cppc_fi->prev_perf_fb_ctrs, &fb_ctrs); 86 if (!perf) 87 return; 88 89 cppc_fi->prev_perf_fb_ctrs = fb_ctrs; 90 91 perf <<= SCHED_CAPACITY_SHIFT; 92 local_freq_scale = div64_u64(perf, cpu_data->perf_caps.highest_perf); 93 94 /* This can happen due to counter's overflow */ 95 if (unlikely(local_freq_scale > 1024)) 96 local_freq_scale = 1024; 97 98 per_cpu(arch_freq_scale, cppc_fi->cpu) = local_freq_scale; 99 } 100 101 static void cppc_scale_freq_tick(void) 102 { 103 __cppc_scale_freq_tick(&per_cpu(cppc_freq_inv, smp_processor_id())); 104 } 105 106 static struct scale_freq_data cppc_sftd = { 107 .source = SCALE_FREQ_SOURCE_CPPC, 108 .set_freq_scale = cppc_scale_freq_tick, 109 }; 110 111 static void cppc_scale_freq_workfn(struct kthread_work *work) 112 { 113 struct cppc_freq_invariance *cppc_fi; 114 115 cppc_fi = container_of(work, struct cppc_freq_invariance, work); 116 __cppc_scale_freq_tick(cppc_fi); 117 } 118 119 static void cppc_irq_work(struct irq_work *irq_work) 120 { 121 struct cppc_freq_invariance *cppc_fi; 122 123 cppc_fi = container_of(irq_work, struct cppc_freq_invariance, irq_work); 124 kthread_queue_work(kworker_fie, &cppc_fi->work); 125 } 126 127 /* 128 * Reading perf counters may sleep if the CPC regs are in PCC. Thus, we 129 * schedule an irq work in scale_freq_tick (since we reach here from hard-irq 130 * context), which then schedules a normal work item cppc_scale_freq_workfn() 131 * that updates the per_cpu arch_freq_scale variable based on the counter 132 * updates since the last tick. 133 */ 134 static void cppc_scale_freq_tick_pcc(void) 135 { 136 struct cppc_freq_invariance *cppc_fi = &per_cpu(cppc_freq_inv, smp_processor_id()); 137 138 /* 139 * cppc_get_perf_ctrs() can potentially sleep, call that from the right 140 * context. 141 */ 142 irq_work_queue(&cppc_fi->irq_work); 143 } 144 145 static struct scale_freq_data cppc_sftd_pcc = { 146 .source = SCALE_FREQ_SOURCE_CPPC, 147 .set_freq_scale = cppc_scale_freq_tick_pcc, 148 }; 149 150 static void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy) 151 { 152 struct scale_freq_data *sftd = &cppc_sftd; 153 struct cppc_freq_invariance *cppc_fi; 154 int cpu, ret; 155 156 if (fie_disabled) 157 return; 158 159 for_each_cpu(cpu, policy->cpus) { 160 cppc_fi = &per_cpu(cppc_freq_inv, cpu); 161 cppc_fi->cpu = cpu; 162 cppc_fi->cpu_data = policy->driver_data; 163 if (cppc_perf_ctrs_in_pcc_cpu(cpu)) { 164 kthread_init_work(&cppc_fi->work, cppc_scale_freq_workfn); 165 init_irq_work(&cppc_fi->irq_work, cppc_irq_work); 166 sftd = &cppc_sftd_pcc; 167 } 168 169 ret = cppc_get_perf_ctrs(cpu, &cppc_fi->prev_perf_fb_ctrs); 170 171 /* 172 * Don't abort as the CPU was offline while the driver was 173 * getting registered. 174 */ 175 if (ret && cpu_online(cpu)) { 176 pr_debug("%s: failed to read perf counters for cpu:%d: %d\n", 177 __func__, cpu, ret); 178 return; 179 } 180 } 181 182 /* Register for freq-invariance */ 183 topology_set_scale_freq_source(sftd, policy->cpus); 184 } 185 186 /* 187 * We free all the resources on policy's removal and not on CPU removal as the 188 * irq-work are per-cpu and the hotplug core takes care of flushing the pending 189 * irq-works (hint: smpcfd_dying_cpu()) on CPU hotplug. Even if the kthread-work 190 * fires on another CPU after the concerned CPU is removed, it won't harm. 191 * 192 * We just need to make sure to remove them all on policy->exit(). 193 */ 194 static void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy) 195 { 196 struct cppc_freq_invariance *cppc_fi; 197 int cpu; 198 199 if (fie_disabled) 200 return; 201 202 /* policy->cpus will be empty here, use related_cpus instead */ 203 topology_clear_scale_freq_source(SCALE_FREQ_SOURCE_CPPC, policy->related_cpus); 204 205 for_each_cpu(cpu, policy->related_cpus) { 206 if (!cppc_perf_ctrs_in_pcc_cpu(cpu)) 207 continue; 208 cppc_fi = &per_cpu(cppc_freq_inv, cpu); 209 irq_work_sync(&cppc_fi->irq_work); 210 kthread_cancel_work_sync(&cppc_fi->work); 211 } 212 } 213 214 static void cppc_fie_kworker_init(void) 215 { 216 struct sched_attr attr = { 217 .size = sizeof(struct sched_attr), 218 .sched_policy = SCHED_DEADLINE, 219 .sched_nice = 0, 220 .sched_priority = 0, 221 /* 222 * Fake (unused) bandwidth; workaround to "fix" 223 * priority inheritance. 224 */ 225 .sched_runtime = NSEC_PER_MSEC, 226 .sched_deadline = 10 * NSEC_PER_MSEC, 227 .sched_period = 10 * NSEC_PER_MSEC, 228 }; 229 int ret; 230 231 kworker_fie = kthread_run_worker(0, "cppc_fie"); 232 if (IS_ERR(kworker_fie)) { 233 pr_warn("%s: failed to create kworker_fie: %ld\n", __func__, 234 PTR_ERR(kworker_fie)); 235 fie_disabled = FIE_DISABLED; 236 kworker_fie = NULL; 237 return; 238 } 239 240 ret = sched_setattr_nocheck(kworker_fie->task, &attr); 241 if (ret) { 242 pr_warn("%s: failed to set SCHED_DEADLINE: %d\n", __func__, 243 ret); 244 kthread_destroy_worker(kworker_fie); 245 fie_disabled = FIE_DISABLED; 246 kworker_fie = NULL; 247 } 248 } 249 250 static void __init cppc_freq_invariance_init(void) 251 { 252 bool perf_ctrs_in_pcc = cppc_perf_ctrs_in_pcc(); 253 254 if (fie_disabled == FIE_UNSET) { 255 if (perf_ctrs_in_pcc) { 256 pr_info("FIE not enabled on systems with registers in PCC\n"); 257 fie_disabled = FIE_DISABLED; 258 } else { 259 fie_disabled = FIE_ENABLED; 260 } 261 } 262 263 if (fie_disabled || !perf_ctrs_in_pcc) 264 return; 265 266 cppc_fie_kworker_init(); 267 } 268 269 static void cppc_freq_invariance_exit(void) 270 { 271 if (kworker_fie) 272 kthread_destroy_worker(kworker_fie); 273 } 274 275 #else 276 static inline void cppc_cpufreq_cpu_fie_init(struct cpufreq_policy *policy) 277 { 278 } 279 280 static inline void cppc_cpufreq_cpu_fie_exit(struct cpufreq_policy *policy) 281 { 282 } 283 284 static inline void cppc_freq_invariance_init(void) 285 { 286 } 287 288 static inline void cppc_freq_invariance_exit(void) 289 { 290 } 291 #endif /* CONFIG_ACPI_CPPC_CPUFREQ_FIE */ 292 293 static void cppc_cpufreq_update_perf_limits(struct cppc_cpudata *cpu_data, 294 struct cpufreq_policy *policy) 295 { 296 struct cppc_perf_caps *caps = &cpu_data->perf_caps; 297 u32 min_perf, max_perf; 298 299 min_perf = cppc_khz_to_perf(caps, policy->min); 300 max_perf = cppc_khz_to_perf(caps, policy->max); 301 302 cpu_data->perf_ctrls.min_perf = 303 clamp_t(u32, min_perf, caps->lowest_perf, caps->highest_perf); 304 cpu_data->perf_ctrls.max_perf = 305 clamp_t(u32, max_perf, caps->lowest_perf, caps->highest_perf); 306 } 307 308 static int cppc_cpufreq_set_target(struct cpufreq_policy *policy, 309 unsigned int target_freq, 310 unsigned int relation) 311 { 312 struct cppc_cpudata *cpu_data = policy->driver_data; 313 unsigned int cpu = policy->cpu; 314 struct cpufreq_freqs freqs; 315 int ret = 0; 316 317 cpu_data->perf_ctrls.desired_perf = 318 cppc_khz_to_perf(&cpu_data->perf_caps, target_freq); 319 cppc_cpufreq_update_perf_limits(cpu_data, policy); 320 321 freqs.old = policy->cur; 322 freqs.new = target_freq; 323 324 cpufreq_freq_transition_begin(policy, &freqs); 325 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls); 326 cpufreq_freq_transition_end(policy, &freqs, ret != 0); 327 328 if (ret) 329 pr_debug("Failed to set target on CPU:%d. ret:%d\n", 330 cpu, ret); 331 332 return ret; 333 } 334 335 static unsigned int cppc_cpufreq_fast_switch(struct cpufreq_policy *policy, 336 unsigned int target_freq) 337 { 338 struct cppc_cpudata *cpu_data = policy->driver_data; 339 unsigned int cpu = policy->cpu; 340 u32 desired_perf; 341 int ret; 342 343 desired_perf = cppc_khz_to_perf(&cpu_data->perf_caps, target_freq); 344 cpu_data->perf_ctrls.desired_perf = desired_perf; 345 cppc_cpufreq_update_perf_limits(cpu_data, policy); 346 347 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls); 348 if (ret) { 349 pr_debug("Failed to set target on CPU:%d. ret:%d\n", 350 cpu, ret); 351 return 0; 352 } 353 354 return target_freq; 355 } 356 357 static int cppc_verify_policy(struct cpufreq_policy_data *policy) 358 { 359 cpufreq_verify_within_cpu_limits(policy); 360 return 0; 361 } 362 363 static unsigned int __cppc_cpufreq_get_transition_delay_us(unsigned int cpu) 364 { 365 int transition_latency_ns = cppc_get_transition_latency(cpu); 366 367 if (transition_latency_ns < 0) 368 return CPUFREQ_DEFAULT_TRANSITION_LATENCY_NS / NSEC_PER_USEC; 369 370 return transition_latency_ns / NSEC_PER_USEC; 371 } 372 373 /* 374 * The PCC subspace describes the rate at which platform can accept commands 375 * on the shared PCC channel (including READs which do not count towards freq 376 * transition requests), so ideally we need to use the PCC values as a fallback 377 * if we don't have a platform specific transition_delay_us 378 */ 379 #ifdef CONFIG_ARM64 380 #include <asm/cputype.h> 381 382 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu) 383 { 384 unsigned long implementor = read_cpuid_implementor(); 385 unsigned long part_num = read_cpuid_part_number(); 386 387 switch (implementor) { 388 case ARM_CPU_IMP_QCOM: 389 switch (part_num) { 390 case QCOM_CPU_PART_FALKOR_V1: 391 case QCOM_CPU_PART_FALKOR: 392 return 10000; 393 } 394 } 395 return __cppc_cpufreq_get_transition_delay_us(cpu); 396 } 397 #else 398 static unsigned int cppc_cpufreq_get_transition_delay_us(unsigned int cpu) 399 { 400 return __cppc_cpufreq_get_transition_delay_us(cpu); 401 } 402 #endif 403 404 #if defined(CONFIG_ARM64) && defined(CONFIG_ENERGY_MODEL) 405 406 static DEFINE_PER_CPU(unsigned int, efficiency_class); 407 408 /* Create an artificial performance state every CPPC_EM_CAP_STEP capacity unit. */ 409 #define CPPC_EM_CAP_STEP (20) 410 /* Increase the cost value by CPPC_EM_COST_STEP every performance state. */ 411 #define CPPC_EM_COST_STEP (1) 412 /* Add a cost gap correspnding to the energy of 4 CPUs. */ 413 #define CPPC_EM_COST_GAP (4 * SCHED_CAPACITY_SCALE * CPPC_EM_COST_STEP \ 414 / CPPC_EM_CAP_STEP) 415 416 static unsigned int get_perf_level_count(struct cpufreq_policy *policy) 417 { 418 struct cppc_perf_caps *perf_caps; 419 unsigned int min_cap, max_cap; 420 struct cppc_cpudata *cpu_data; 421 int cpu = policy->cpu; 422 423 cpu_data = policy->driver_data; 424 perf_caps = &cpu_data->perf_caps; 425 max_cap = arch_scale_cpu_capacity(cpu); 426 min_cap = div_u64((u64)max_cap * perf_caps->lowest_perf, 427 perf_caps->highest_perf); 428 if ((min_cap == 0) || (max_cap < min_cap)) 429 return 0; 430 return 1 + max_cap / CPPC_EM_CAP_STEP - min_cap / CPPC_EM_CAP_STEP; 431 } 432 433 /* 434 * The cost is defined as: 435 * cost = power * max_frequency / frequency 436 */ 437 static inline unsigned long compute_cost(int cpu, int step) 438 { 439 return CPPC_EM_COST_GAP * per_cpu(efficiency_class, cpu) + 440 step * CPPC_EM_COST_STEP; 441 } 442 443 static int cppc_get_cpu_power(struct device *cpu_dev, 444 unsigned long *power, unsigned long *KHz) 445 { 446 unsigned long perf_step, perf_prev, perf, perf_check; 447 unsigned int min_step, max_step, step, step_check; 448 unsigned long prev_freq = *KHz; 449 unsigned int min_cap, max_cap; 450 struct cpufreq_policy *policy; 451 452 struct cppc_perf_caps *perf_caps; 453 struct cppc_cpudata *cpu_data; 454 455 policy = cpufreq_cpu_get_raw(cpu_dev->id); 456 if (!policy) 457 return -EINVAL; 458 459 cpu_data = policy->driver_data; 460 perf_caps = &cpu_data->perf_caps; 461 max_cap = arch_scale_cpu_capacity(cpu_dev->id); 462 min_cap = div_u64((u64)max_cap * perf_caps->lowest_perf, 463 perf_caps->highest_perf); 464 perf_step = div_u64((u64)CPPC_EM_CAP_STEP * perf_caps->highest_perf, 465 max_cap); 466 min_step = min_cap / CPPC_EM_CAP_STEP; 467 max_step = max_cap / CPPC_EM_CAP_STEP; 468 469 perf_prev = cppc_khz_to_perf(perf_caps, *KHz); 470 step = perf_prev / perf_step; 471 472 if (step > max_step) 473 return -EINVAL; 474 475 if (min_step == max_step) { 476 step = max_step; 477 perf = perf_caps->highest_perf; 478 } else if (step < min_step) { 479 step = min_step; 480 perf = perf_caps->lowest_perf; 481 } else { 482 step++; 483 if (step == max_step) 484 perf = perf_caps->highest_perf; 485 else 486 perf = step * perf_step; 487 } 488 489 *KHz = cppc_perf_to_khz(perf_caps, perf); 490 perf_check = cppc_khz_to_perf(perf_caps, *KHz); 491 step_check = perf_check / perf_step; 492 493 /* 494 * To avoid bad integer approximation, check that new frequency value 495 * increased and that the new frequency will be converted to the 496 * desired step value. 497 */ 498 while ((*KHz == prev_freq) || (step_check != step)) { 499 perf++; 500 *KHz = cppc_perf_to_khz(perf_caps, perf); 501 perf_check = cppc_khz_to_perf(perf_caps, *KHz); 502 step_check = perf_check / perf_step; 503 } 504 505 /* 506 * With an artificial EM, only the cost value is used. Still the power 507 * is populated such as 0 < power < EM_MAX_POWER. This allows to add 508 * more sense to the artificial performance states. 509 */ 510 *power = compute_cost(cpu_dev->id, step); 511 512 return 0; 513 } 514 515 static int cppc_get_cpu_cost(struct device *cpu_dev, unsigned long KHz, 516 unsigned long *cost) 517 { 518 unsigned long perf_step, perf_prev; 519 struct cppc_perf_caps *perf_caps; 520 struct cpufreq_policy *policy; 521 struct cppc_cpudata *cpu_data; 522 unsigned int max_cap; 523 int step; 524 525 policy = cpufreq_cpu_get_raw(cpu_dev->id); 526 if (!policy) 527 return -EINVAL; 528 529 cpu_data = policy->driver_data; 530 perf_caps = &cpu_data->perf_caps; 531 max_cap = arch_scale_cpu_capacity(cpu_dev->id); 532 533 perf_prev = cppc_khz_to_perf(perf_caps, KHz); 534 perf_step = CPPC_EM_CAP_STEP * perf_caps->highest_perf / max_cap; 535 step = perf_prev / perf_step; 536 537 *cost = compute_cost(cpu_dev->id, step); 538 539 return 0; 540 } 541 542 static void cppc_cpufreq_register_em(struct cpufreq_policy *policy) 543 { 544 struct cppc_cpudata *cpu_data; 545 struct em_data_callback em_cb = 546 EM_ADV_DATA_CB(cppc_get_cpu_power, cppc_get_cpu_cost); 547 548 cpu_data = policy->driver_data; 549 em_dev_register_perf_domain(get_cpu_device(policy->cpu), 550 get_perf_level_count(policy), &em_cb, 551 cpu_data->shared_cpu_map, 0); 552 } 553 554 static void populate_efficiency_class(void) 555 { 556 struct acpi_madt_generic_interrupt *gicc; 557 DECLARE_BITMAP(used_classes, 256) = {}; 558 int class, cpu, index; 559 560 for_each_possible_cpu(cpu) { 561 gicc = acpi_cpu_get_madt_gicc(cpu); 562 class = gicc->efficiency_class; 563 bitmap_set(used_classes, class, 1); 564 } 565 566 if (bitmap_weight(used_classes, 256) <= 1) { 567 pr_debug("Efficiency classes are all equal (=%d). " 568 "No EM registered", class); 569 return; 570 } 571 572 /* 573 * Squeeze efficiency class values on [0:#efficiency_class-1]. 574 * Values are per spec in [0:255]. 575 */ 576 index = 0; 577 for_each_set_bit(class, used_classes, 256) { 578 for_each_possible_cpu(cpu) { 579 gicc = acpi_cpu_get_madt_gicc(cpu); 580 if (gicc->efficiency_class == class) 581 per_cpu(efficiency_class, cpu) = index; 582 } 583 index++; 584 } 585 cppc_cpufreq_driver.register_em = cppc_cpufreq_register_em; 586 } 587 588 #else 589 static void populate_efficiency_class(void) 590 { 591 } 592 #endif 593 594 static struct cppc_cpudata *cppc_cpufreq_get_cpu_data(unsigned int cpu) 595 { 596 struct cppc_cpudata *cpu_data; 597 int ret; 598 599 cpu_data = kzalloc_obj(struct cppc_cpudata); 600 if (!cpu_data) 601 goto out; 602 603 if (!zalloc_cpumask_var(&cpu_data->shared_cpu_map, GFP_KERNEL)) 604 goto free_cpu; 605 606 ret = acpi_get_psd_map(cpu, cpu_data); 607 if (ret) { 608 pr_debug("Err parsing CPU%d PSD data: ret:%d\n", cpu, ret); 609 goto free_mask; 610 } 611 612 ret = cppc_get_perf_caps(cpu, &cpu_data->perf_caps); 613 if (ret) { 614 pr_debug("Err reading CPU%d perf caps: ret:%d\n", cpu, ret); 615 goto free_mask; 616 } 617 618 ret = cppc_get_perf(cpu, &cpu_data->perf_ctrls); 619 if (ret) { 620 pr_debug("Err reading CPU%d perf ctrls: ret:%d\n", cpu, ret); 621 goto free_mask; 622 } 623 624 return cpu_data; 625 626 free_mask: 627 free_cpumask_var(cpu_data->shared_cpu_map); 628 free_cpu: 629 kfree(cpu_data); 630 out: 631 return NULL; 632 } 633 634 static void cppc_cpufreq_put_cpu_data(struct cpufreq_policy *policy) 635 { 636 struct cppc_cpudata *cpu_data = policy->driver_data; 637 638 free_cpumask_var(cpu_data->shared_cpu_map); 639 kfree(cpu_data); 640 policy->driver_data = NULL; 641 } 642 643 static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) 644 { 645 unsigned int cpu = policy->cpu; 646 struct cppc_cpudata *cpu_data; 647 struct cppc_perf_caps *caps; 648 int ret; 649 650 cpu_data = cppc_cpufreq_get_cpu_data(cpu); 651 if (!cpu_data) { 652 pr_err("Error in acquiring _CPC/_PSD data for CPU%d.\n", cpu); 653 return -ENODEV; 654 } 655 caps = &cpu_data->perf_caps; 656 policy->driver_data = cpu_data; 657 658 /* 659 * Set min to lowest nonlinear perf to avoid any efficiency penalty (see 660 * Section 8.4.7.1.1.5 of ACPI 6.1 spec) 661 */ 662 policy->min = cppc_perf_to_khz(caps, caps->lowest_nonlinear_perf); 663 policy->max = cppc_perf_to_khz(caps, policy->boost_enabled ? 664 caps->highest_perf : caps->nominal_perf); 665 666 /* 667 * Set cpuinfo.min_freq to Lowest to make the full range of performance 668 * available if userspace wants to use any perf between lowest & lowest 669 * nonlinear perf 670 */ 671 policy->cpuinfo.min_freq = cppc_perf_to_khz(caps, caps->lowest_perf); 672 policy->cpuinfo.max_freq = policy->max; 673 674 policy->transition_delay_us = cppc_cpufreq_get_transition_delay_us(cpu); 675 policy->shared_type = cpu_data->shared_type; 676 677 switch (policy->shared_type) { 678 case CPUFREQ_SHARED_TYPE_HW: 679 case CPUFREQ_SHARED_TYPE_NONE: 680 /* Nothing to be done - we'll have a policy for each CPU */ 681 break; 682 case CPUFREQ_SHARED_TYPE_ANY: 683 /* 684 * All CPUs in the domain will share a policy and all cpufreq 685 * operations will use a single cppc_cpudata structure stored 686 * in policy->driver_data. 687 */ 688 cpumask_copy(policy->cpus, cpu_data->shared_cpu_map); 689 break; 690 default: 691 pr_debug("Unsupported CPU co-ord type: %d\n", 692 policy->shared_type); 693 ret = -EFAULT; 694 goto out; 695 } 696 697 policy->fast_switch_possible = cppc_allow_fast_switch(); 698 policy->dvfs_possible_from_any_cpu = true; 699 700 /* 701 * If 'highest_perf' is greater than 'nominal_perf', we assume CPU Boost 702 * is supported. 703 */ 704 if (caps->highest_perf > caps->nominal_perf) 705 policy->boost_supported = true; 706 707 /* Set policy->cur to max now. The governors will adjust later. */ 708 policy->cur = cppc_perf_to_khz(caps, caps->highest_perf); 709 cpu_data->perf_ctrls.desired_perf = caps->highest_perf; 710 711 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls); 712 if (ret) { 713 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n", 714 caps->highest_perf, cpu, ret); 715 goto out; 716 } 717 718 cppc_cpufreq_cpu_fie_init(policy); 719 return 0; 720 721 out: 722 cppc_cpufreq_put_cpu_data(policy); 723 return ret; 724 } 725 726 static void cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy) 727 { 728 struct cppc_cpudata *cpu_data = policy->driver_data; 729 struct cppc_perf_caps *caps = &cpu_data->perf_caps; 730 unsigned int cpu = policy->cpu; 731 int ret; 732 733 cppc_cpufreq_cpu_fie_exit(policy); 734 735 cpu_data->perf_ctrls.desired_perf = caps->lowest_perf; 736 737 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls); 738 if (ret) 739 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n", 740 caps->lowest_perf, cpu, ret); 741 742 cppc_cpufreq_put_cpu_data(policy); 743 } 744 745 static inline u64 get_delta(u64 t1, u64 t0) 746 { 747 if (t1 > t0 || t0 > ~(u32)0) 748 return t1 - t0; 749 750 return (u32)t1 - (u32)t0; 751 } 752 753 static int cppc_perf_from_fbctrs(u64 reference_perf, 754 struct cppc_perf_fb_ctrs *fb_ctrs_t0, 755 struct cppc_perf_fb_ctrs *fb_ctrs_t1) 756 { 757 u64 delta_reference, delta_delivered; 758 759 delta_reference = get_delta(fb_ctrs_t1->reference, 760 fb_ctrs_t0->reference); 761 delta_delivered = get_delta(fb_ctrs_t1->delivered, 762 fb_ctrs_t0->delivered); 763 764 /* 765 * Avoid divide-by zero and unchanged feedback counters. 766 * Leave it for callers to handle. 767 */ 768 if (!delta_reference || !delta_delivered) 769 return 0; 770 771 return (reference_perf * delta_delivered) / delta_reference; 772 } 773 774 static int cppc_get_perf_ctrs_sample(int cpu, 775 struct cppc_perf_fb_ctrs *fb_ctrs_t0, 776 struct cppc_perf_fb_ctrs *fb_ctrs_t1) 777 { 778 int ret; 779 780 ret = cppc_get_perf_ctrs(cpu, fb_ctrs_t0); 781 if (ret) 782 return ret; 783 784 udelay(2); /* 2usec delay between sampling */ 785 786 return cppc_get_perf_ctrs(cpu, fb_ctrs_t1); 787 } 788 789 static unsigned int cppc_cpufreq_get_rate(unsigned int cpu) 790 { 791 struct cpufreq_policy *policy __free(put_cpufreq_policy) = cpufreq_cpu_get(cpu); 792 struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0}; 793 struct cppc_cpudata *cpu_data; 794 u64 delivered_perf, reference_perf; 795 int ret; 796 797 if (!policy) 798 return 0; 799 800 cpu_data = policy->driver_data; 801 802 ret = cppc_get_perf_ctrs_sample(cpu, &fb_ctrs_t0, &fb_ctrs_t1); 803 if (ret) { 804 if (ret == -EFAULT) 805 /* Any of the associated CPPC regs is 0. */ 806 goto out_invalid_counters; 807 else 808 return 0; 809 } 810 811 reference_perf = cpu_data->perf_caps.reference_perf; 812 delivered_perf = cppc_perf_from_fbctrs(reference_perf, 813 &fb_ctrs_t0, &fb_ctrs_t1); 814 if (!delivered_perf) 815 goto out_invalid_counters; 816 817 return cppc_perf_to_khz(&cpu_data->perf_caps, delivered_perf); 818 819 out_invalid_counters: 820 /* 821 * Feedback counters could be unchanged or 0 when a cpu enters a 822 * low-power idle state, e.g. clock-gated or power-gated. 823 * Use desired perf for reflecting frequency. Get the latest register 824 * value first as some platforms may update the actual delivered perf 825 * there; if failed, resort to the cached desired perf. 826 */ 827 if (cppc_get_desired_perf(cpu, &delivered_perf)) 828 delivered_perf = cpu_data->perf_ctrls.desired_perf; 829 830 return cppc_perf_to_khz(&cpu_data->perf_caps, delivered_perf); 831 } 832 833 static int cppc_cpufreq_set_boost(struct cpufreq_policy *policy, int state) 834 { 835 struct cppc_cpudata *cpu_data = policy->driver_data; 836 struct cppc_perf_caps *caps = &cpu_data->perf_caps; 837 838 if (state) 839 policy->cpuinfo.max_freq = cppc_perf_to_khz(caps, caps->highest_perf); 840 else 841 policy->cpuinfo.max_freq = cppc_perf_to_khz(caps, caps->nominal_perf); 842 843 return 0; 844 } 845 846 static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf) 847 { 848 struct cppc_cpudata *cpu_data = policy->driver_data; 849 850 return cpufreq_show_cpus(cpu_data->shared_cpu_map, buf); 851 } 852 853 static ssize_t show_auto_select(struct cpufreq_policy *policy, char *buf) 854 { 855 bool val; 856 int ret; 857 858 ret = cppc_get_auto_sel(policy->cpu, &val); 859 860 /* show "<unsupported>" when this register is not supported by cpc */ 861 if (ret == -EOPNOTSUPP) 862 return sysfs_emit(buf, "<unsupported>\n"); 863 864 if (ret) 865 return ret; 866 867 return sysfs_emit(buf, "%d\n", val); 868 } 869 870 static ssize_t store_auto_select(struct cpufreq_policy *policy, 871 const char *buf, size_t count) 872 { 873 struct cppc_cpudata *cpu_data = policy->driver_data; 874 bool val; 875 int ret; 876 877 ret = kstrtobool(buf, &val); 878 if (ret) 879 return ret; 880 881 ret = cppc_set_auto_sel(policy->cpu, val); 882 if (ret) 883 return ret; 884 885 cpu_data->perf_ctrls.auto_sel = val; 886 887 if (val) { 888 u32 old_min_perf = cpu_data->perf_ctrls.min_perf; 889 u32 old_max_perf = cpu_data->perf_ctrls.max_perf; 890 891 /* 892 * When enabling autonomous selection, program MIN_PERF and 893 * MAX_PERF from current policy limits so that the platform 894 * uses the correct performance bounds immediately. 895 */ 896 cppc_cpufreq_update_perf_limits(cpu_data, policy); 897 898 ret = cppc_set_perf(policy->cpu, &cpu_data->perf_ctrls); 899 if (ret) { 900 cpu_data->perf_ctrls.min_perf = old_min_perf; 901 cpu_data->perf_ctrls.max_perf = old_max_perf; 902 cppc_set_auto_sel(policy->cpu, false); 903 cpu_data->perf_ctrls.auto_sel = false; 904 return ret; 905 } 906 } 907 908 return count; 909 } 910 911 static ssize_t cppc_cpufreq_sysfs_show_u64(unsigned int cpu, 912 int (*get_func)(int, u64 *), 913 char *buf) 914 { 915 u64 val; 916 int ret = get_func((int)cpu, &val); 917 918 if (ret == -EOPNOTSUPP) 919 return sysfs_emit(buf, "<unsupported>\n"); 920 921 if (ret) 922 return ret; 923 924 return sysfs_emit(buf, "%llu\n", val); 925 } 926 927 static ssize_t cppc_cpufreq_sysfs_store_u64(unsigned int cpu, 928 int (*set_func)(int, u64), 929 const char *buf, size_t count) 930 { 931 u64 val; 932 int ret; 933 934 ret = kstrtou64(buf, 0, &val); 935 if (ret) 936 return ret; 937 938 ret = set_func((int)cpu, val); 939 940 return ret ? ret : count; 941 } 942 943 #define CPPC_CPUFREQ_ATTR_RW_U64(_name, _get_func, _set_func) \ 944 static ssize_t show_##_name(struct cpufreq_policy *policy, char *buf) \ 945 { \ 946 return cppc_cpufreq_sysfs_show_u64(policy->cpu, _get_func, buf);\ 947 } \ 948 static ssize_t store_##_name(struct cpufreq_policy *policy, \ 949 const char *buf, size_t count) \ 950 { \ 951 return cppc_cpufreq_sysfs_store_u64(policy->cpu, _set_func, \ 952 buf, count); \ 953 } 954 955 CPPC_CPUFREQ_ATTR_RW_U64(auto_act_window, cppc_get_auto_act_window, 956 cppc_set_auto_act_window) 957 958 static ssize_t 959 show_energy_performance_preference_val(struct cpufreq_policy *policy, char *buf) 960 { 961 return cppc_cpufreq_sysfs_show_u64(policy->cpu, cppc_get_epp_perf, buf); 962 } 963 964 static ssize_t 965 store_energy_performance_preference_val(struct cpufreq_policy *policy, 966 const char *buf, size_t count) 967 { 968 struct cppc_cpudata *cpu_data = policy->driver_data; 969 u64 val; 970 int ret; 971 972 ret = kstrtou64(buf, 0, &val); 973 if (ret) 974 return ret; 975 976 ret = cppc_set_epp(policy->cpu, val); 977 if (ret) 978 return ret; 979 980 cpu_data->perf_ctrls.energy_perf = val; 981 982 return count; 983 } 984 985 CPPC_CPUFREQ_ATTR_RW_U64(perf_limited, cppc_get_perf_limited, 986 cppc_set_perf_limited) 987 988 cpufreq_freq_attr_ro(freqdomain_cpus); 989 cpufreq_freq_attr_rw(auto_select); 990 cpufreq_freq_attr_rw(auto_act_window); 991 cpufreq_freq_attr_rw(energy_performance_preference_val); 992 cpufreq_freq_attr_rw(perf_limited); 993 994 static struct freq_attr *cppc_cpufreq_attr[] = { 995 &freqdomain_cpus, 996 &auto_select, 997 &auto_act_window, 998 &energy_performance_preference_val, 999 &perf_limited, 1000 NULL, 1001 }; 1002 1003 static struct cpufreq_driver cppc_cpufreq_driver = { 1004 .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS, 1005 .verify = cppc_verify_policy, 1006 .target = cppc_cpufreq_set_target, 1007 .get = cppc_cpufreq_get_rate, 1008 .fast_switch = cppc_cpufreq_fast_switch, 1009 .init = cppc_cpufreq_cpu_init, 1010 .exit = cppc_cpufreq_cpu_exit, 1011 .set_boost = cppc_cpufreq_set_boost, 1012 .attr = cppc_cpufreq_attr, 1013 .name = "cppc_cpufreq", 1014 }; 1015 1016 static int __init cppc_cpufreq_init(void) 1017 { 1018 int ret; 1019 1020 if (!acpi_cpc_valid()) 1021 return -ENODEV; 1022 1023 cppc_freq_invariance_init(); 1024 populate_efficiency_class(); 1025 1026 ret = cpufreq_register_driver(&cppc_cpufreq_driver); 1027 if (ret) 1028 cppc_freq_invariance_exit(); 1029 1030 return ret; 1031 } 1032 1033 static void __exit cppc_cpufreq_exit(void) 1034 { 1035 cpufreq_unregister_driver(&cppc_cpufreq_driver); 1036 cppc_freq_invariance_exit(); 1037 } 1038 1039 module_exit(cppc_cpufreq_exit); 1040 MODULE_AUTHOR("Ashwin Chaugule"); 1041 MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec"); 1042 MODULE_LICENSE("GPL"); 1043 1044 late_initcall(cppc_cpufreq_init); 1045 1046 static const struct acpi_device_id cppc_acpi_ids[] __used = { 1047 {ACPI_PROCESSOR_DEVICE_HID, }, 1048 {} 1049 }; 1050 1051 MODULE_DEVICE_TABLE(acpi, cppc_acpi_ids); 1052