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 664 /* 665 * Set cpuinfo.min_freq to Lowest to make the full range of performance 666 * available if userspace wants to use any perf between lowest & lowest 667 * nonlinear perf 668 */ 669 policy->cpuinfo.min_freq = cppc_perf_to_khz(caps, caps->lowest_perf); 670 policy->cpuinfo.max_freq = cppc_perf_to_khz(caps, policy->boost_enabled ? 671 caps->highest_perf : caps->nominal_perf); 672 673 policy->transition_delay_us = cppc_cpufreq_get_transition_delay_us(cpu); 674 policy->shared_type = cpu_data->shared_type; 675 676 switch (policy->shared_type) { 677 case CPUFREQ_SHARED_TYPE_HW: 678 case CPUFREQ_SHARED_TYPE_NONE: 679 /* Nothing to be done - we'll have a policy for each CPU */ 680 break; 681 case CPUFREQ_SHARED_TYPE_ANY: 682 /* 683 * All CPUs in the domain will share a policy and all cpufreq 684 * operations will use a single cppc_cpudata structure stored 685 * in policy->driver_data. 686 */ 687 cpumask_copy(policy->cpus, cpu_data->shared_cpu_map); 688 break; 689 default: 690 pr_debug("Unsupported CPU co-ord type: %d\n", 691 policy->shared_type); 692 ret = -EFAULT; 693 goto out; 694 } 695 696 policy->fast_switch_possible = cppc_allow_fast_switch(); 697 policy->dvfs_possible_from_any_cpu = true; 698 699 /* 700 * If 'highest_perf' is greater than 'nominal_perf', we assume CPU Boost 701 * is supported. 702 */ 703 if (caps->highest_perf > caps->nominal_perf) 704 policy->boost_supported = true; 705 706 /* Set policy->cur to max now. The governors will adjust later. */ 707 policy->cur = cppc_perf_to_khz(caps, caps->highest_perf); 708 cpu_data->perf_ctrls.desired_perf = caps->highest_perf; 709 710 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls); 711 if (ret) { 712 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n", 713 caps->highest_perf, cpu, ret); 714 goto out; 715 } 716 717 cppc_cpufreq_cpu_fie_init(policy); 718 return 0; 719 720 out: 721 cppc_cpufreq_put_cpu_data(policy); 722 return ret; 723 } 724 725 static void cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy) 726 { 727 struct cppc_cpudata *cpu_data = policy->driver_data; 728 struct cppc_perf_caps *caps = &cpu_data->perf_caps; 729 unsigned int cpu = policy->cpu; 730 int ret; 731 732 cppc_cpufreq_cpu_fie_exit(policy); 733 734 cpu_data->perf_ctrls.desired_perf = caps->lowest_perf; 735 736 ret = cppc_set_perf(cpu, &cpu_data->perf_ctrls); 737 if (ret) 738 pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n", 739 caps->lowest_perf, cpu, ret); 740 741 cppc_cpufreq_put_cpu_data(policy); 742 } 743 744 static inline u64 get_delta(u64 t1, u64 t0) 745 { 746 if (t1 > t0 || t0 > ~(u32)0) 747 return t1 - t0; 748 749 return (u32)t1 - (u32)t0; 750 } 751 752 static int cppc_perf_from_fbctrs(u64 reference_perf, 753 struct cppc_perf_fb_ctrs *fb_ctrs_t0, 754 struct cppc_perf_fb_ctrs *fb_ctrs_t1) 755 { 756 u64 delta_reference, delta_delivered; 757 758 delta_reference = get_delta(fb_ctrs_t1->reference, 759 fb_ctrs_t0->reference); 760 delta_delivered = get_delta(fb_ctrs_t1->delivered, 761 fb_ctrs_t0->delivered); 762 763 /* 764 * Avoid divide-by zero and unchanged feedback counters. 765 * Leave it for callers to handle. 766 */ 767 if (!delta_reference || !delta_delivered) 768 return 0; 769 770 return (reference_perf * delta_delivered) / delta_reference; 771 } 772 773 static int cppc_get_perf_ctrs_sample(int cpu, 774 struct cppc_perf_fb_ctrs *fb_ctrs_t0, 775 struct cppc_perf_fb_ctrs *fb_ctrs_t1) 776 { 777 int ret; 778 779 ret = cppc_get_perf_ctrs(cpu, fb_ctrs_t0); 780 if (ret) 781 return ret; 782 783 udelay(2); /* 2usec delay between sampling */ 784 785 return cppc_get_perf_ctrs(cpu, fb_ctrs_t1); 786 } 787 788 static unsigned int cppc_cpufreq_get_rate(unsigned int cpu) 789 { 790 struct cpufreq_policy *policy __free(put_cpufreq_policy) = cpufreq_cpu_get(cpu); 791 struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0}; 792 struct cppc_cpudata *cpu_data; 793 u64 delivered_perf, reference_perf; 794 int ret; 795 796 if (!policy) 797 return 0; 798 799 cpu_data = policy->driver_data; 800 801 ret = cppc_get_perf_ctrs_sample(cpu, &fb_ctrs_t0, &fb_ctrs_t1); 802 if (ret) { 803 if (ret == -EFAULT) 804 /* Any of the associated CPPC regs is 0. */ 805 goto out_invalid_counters; 806 else 807 return 0; 808 } 809 810 reference_perf = cpu_data->perf_caps.reference_perf; 811 delivered_perf = cppc_perf_from_fbctrs(reference_perf, 812 &fb_ctrs_t0, &fb_ctrs_t1); 813 if (!delivered_perf) 814 goto out_invalid_counters; 815 816 return cppc_perf_to_khz(&cpu_data->perf_caps, delivered_perf); 817 818 out_invalid_counters: 819 /* 820 * Feedback counters could be unchanged or 0 when a cpu enters a 821 * low-power idle state, e.g. clock-gated or power-gated. 822 * Use desired perf for reflecting frequency. Get the latest register 823 * value first as some platforms may update the actual delivered perf 824 * there; if failed, resort to the cached desired perf. 825 */ 826 if (cppc_get_desired_perf(cpu, &delivered_perf)) 827 delivered_perf = cpu_data->perf_ctrls.desired_perf; 828 829 return cppc_perf_to_khz(&cpu_data->perf_caps, delivered_perf); 830 } 831 832 static int cppc_cpufreq_set_boost(struct cpufreq_policy *policy, int state) 833 { 834 struct cppc_cpudata *cpu_data = policy->driver_data; 835 struct cppc_perf_caps *caps = &cpu_data->perf_caps; 836 837 if (state) 838 policy->cpuinfo.max_freq = cppc_perf_to_khz(caps, caps->highest_perf); 839 else 840 policy->cpuinfo.max_freq = cppc_perf_to_khz(caps, caps->nominal_perf); 841 842 return 0; 843 } 844 845 static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf) 846 { 847 struct cppc_cpudata *cpu_data = policy->driver_data; 848 849 return cpufreq_show_cpus(cpu_data->shared_cpu_map, buf); 850 } 851 852 static ssize_t show_auto_select(struct cpufreq_policy *policy, char *buf) 853 { 854 bool val; 855 int ret; 856 857 ret = cppc_get_auto_sel(policy->cpu, &val); 858 859 /* show "<unsupported>" when this register is not supported by cpc */ 860 if (ret == -EOPNOTSUPP) 861 return sysfs_emit(buf, "<unsupported>\n"); 862 863 if (ret) 864 return ret; 865 866 return sysfs_emit(buf, "%d\n", val); 867 } 868 869 static ssize_t store_auto_select(struct cpufreq_policy *policy, 870 const char *buf, size_t count) 871 { 872 struct cppc_cpudata *cpu_data = policy->driver_data; 873 bool val; 874 int ret; 875 876 ret = kstrtobool(buf, &val); 877 if (ret) 878 return ret; 879 880 ret = cppc_set_auto_sel(policy->cpu, val); 881 if (ret) 882 return ret; 883 884 cpu_data->perf_ctrls.auto_sel = val; 885 886 if (val) { 887 u32 old_min_perf = cpu_data->perf_ctrls.min_perf; 888 u32 old_max_perf = cpu_data->perf_ctrls.max_perf; 889 890 /* 891 * When enabling autonomous selection, program MIN_PERF and 892 * MAX_PERF from current policy limits so that the platform 893 * uses the correct performance bounds immediately. 894 */ 895 cppc_cpufreq_update_perf_limits(cpu_data, policy); 896 897 ret = cppc_set_perf(policy->cpu, &cpu_data->perf_ctrls); 898 if (ret) { 899 cpu_data->perf_ctrls.min_perf = old_min_perf; 900 cpu_data->perf_ctrls.max_perf = old_max_perf; 901 cppc_set_auto_sel(policy->cpu, false); 902 cpu_data->perf_ctrls.auto_sel = false; 903 return ret; 904 } 905 } 906 907 return count; 908 } 909 910 static ssize_t cppc_cpufreq_sysfs_show_u64(unsigned int cpu, 911 int (*get_func)(int, u64 *), 912 char *buf) 913 { 914 u64 val; 915 int ret = get_func((int)cpu, &val); 916 917 if (ret == -EOPNOTSUPP) 918 return sysfs_emit(buf, "<unsupported>\n"); 919 920 if (ret) 921 return ret; 922 923 return sysfs_emit(buf, "%llu\n", val); 924 } 925 926 static ssize_t cppc_cpufreq_sysfs_store_u64(unsigned int cpu, 927 int (*set_func)(int, u64), 928 const char *buf, size_t count) 929 { 930 u64 val; 931 int ret; 932 933 ret = kstrtou64(buf, 0, &val); 934 if (ret) 935 return ret; 936 937 ret = set_func((int)cpu, val); 938 939 return ret ? ret : count; 940 } 941 942 #define CPPC_CPUFREQ_ATTR_RW_U64(_name, _get_func, _set_func) \ 943 static ssize_t show_##_name(struct cpufreq_policy *policy, char *buf) \ 944 { \ 945 return cppc_cpufreq_sysfs_show_u64(policy->cpu, _get_func, buf);\ 946 } \ 947 static ssize_t store_##_name(struct cpufreq_policy *policy, \ 948 const char *buf, size_t count) \ 949 { \ 950 return cppc_cpufreq_sysfs_store_u64(policy->cpu, _set_func, \ 951 buf, count); \ 952 } 953 954 CPPC_CPUFREQ_ATTR_RW_U64(auto_act_window, cppc_get_auto_act_window, 955 cppc_set_auto_act_window) 956 957 static ssize_t 958 show_energy_performance_preference_val(struct cpufreq_policy *policy, char *buf) 959 { 960 return cppc_cpufreq_sysfs_show_u64(policy->cpu, cppc_get_epp_perf, buf); 961 } 962 963 static ssize_t 964 store_energy_performance_preference_val(struct cpufreq_policy *policy, 965 const char *buf, size_t count) 966 { 967 struct cppc_cpudata *cpu_data = policy->driver_data; 968 u64 val; 969 int ret; 970 971 ret = kstrtou64(buf, 0, &val); 972 if (ret) 973 return ret; 974 975 ret = cppc_set_epp(policy->cpu, val); 976 if (ret) 977 return ret; 978 979 cpu_data->perf_ctrls.energy_perf = val; 980 981 return count; 982 } 983 984 static int cppc_get_perf_limited_filtered(int cpu, u64 *perf_limited) 985 { 986 struct cpufreq_policy *policy; 987 struct cppc_cpudata *cpu_data; 988 int ret; 989 990 ret = cppc_get_perf_limited(cpu, perf_limited); 991 if (ret) 992 return ret; 993 994 policy = cpufreq_cpu_get_raw(cpu); 995 if (!policy) 996 return -EINVAL; 997 998 cpu_data = policy->driver_data; 999 1000 /* 1001 * Desired Excursion is ignored when autonomous selection is 1002 * enabled. Clear the bit to avoid exposing meaningless state 1003 * to userspace. 1004 */ 1005 if (cpu_data && cpu_data->perf_ctrls.auto_sel) 1006 *perf_limited &= ~CPPC_PERF_LIMITED_DESIRED_EXCURSION; 1007 1008 return 0; 1009 } 1010 1011 CPPC_CPUFREQ_ATTR_RW_U64(perf_limited, cppc_get_perf_limited_filtered, 1012 cppc_set_perf_limited) 1013 1014 cpufreq_freq_attr_ro(freqdomain_cpus); 1015 cpufreq_freq_attr_rw(auto_select); 1016 cpufreq_freq_attr_rw(auto_act_window); 1017 cpufreq_freq_attr_rw(energy_performance_preference_val); 1018 cpufreq_freq_attr_rw(perf_limited); 1019 1020 static struct freq_attr *cppc_cpufreq_attr[] = { 1021 &freqdomain_cpus, 1022 &auto_select, 1023 &auto_act_window, 1024 &energy_performance_preference_val, 1025 &perf_limited, 1026 NULL, 1027 }; 1028 1029 static struct cpufreq_driver cppc_cpufreq_driver = { 1030 .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS, 1031 .verify = cppc_verify_policy, 1032 .target = cppc_cpufreq_set_target, 1033 .get = cppc_cpufreq_get_rate, 1034 .fast_switch = cppc_cpufreq_fast_switch, 1035 .init = cppc_cpufreq_cpu_init, 1036 .exit = cppc_cpufreq_cpu_exit, 1037 .set_boost = cppc_cpufreq_set_boost, 1038 .attr = cppc_cpufreq_attr, 1039 .name = "cppc_cpufreq", 1040 }; 1041 1042 static int __init cppc_cpufreq_init(void) 1043 { 1044 int ret; 1045 1046 if (!acpi_cpc_valid()) 1047 return -ENODEV; 1048 1049 cppc_freq_invariance_init(); 1050 populate_efficiency_class(); 1051 1052 ret = cpufreq_register_driver(&cppc_cpufreq_driver); 1053 if (ret) 1054 cppc_freq_invariance_exit(); 1055 1056 return ret; 1057 } 1058 1059 static void __exit cppc_cpufreq_exit(void) 1060 { 1061 cpufreq_unregister_driver(&cppc_cpufreq_driver); 1062 cppc_freq_invariance_exit(); 1063 } 1064 1065 module_exit(cppc_cpufreq_exit); 1066 MODULE_AUTHOR("Ashwin Chaugule"); 1067 MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec"); 1068 MODULE_LICENSE("GPL"); 1069 1070 late_initcall(cppc_cpufreq_init); 1071 1072 static const struct acpi_device_id cppc_acpi_ids[] __used = { 1073 {ACPI_PROCESSOR_DEVICE_HID, }, 1074 {} 1075 }; 1076 1077 MODULE_DEVICE_TABLE(acpi, cppc_acpi_ids); 1078