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