1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel_powerclamp.c - package c-state idle injection
5 * Copyright (c) 2012-2023, Intel Corporation.
8 * Arjan van de Ven <arjan@linux.intel.com>
9 * Jacob Pan <jacob.jun.pan@linux.intel.com>
12 * 1. better handle wakeup from external interrupts, currently a fixed
13 * compensation is added to clamping duration when excessive amount
14 * of wakeups are observed during idle time. the reason is that in
15 * case of external interrupts without need for ack, clamping down
16 * cpu in non-irq context does not reduce irq. for majority of the
17 * cases, clamping down cpu does help reduce irq as well, we should
18 * be able to differentiate the two cases and give a quantitative
19 * solution for the irqs that we can control. perhaps based on
20 * get_cpu_iowait_time_us()
22 * 2. synchronization with other hw blocks
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/delay.h>
30 #include <linux/cpu.h>
31 #include <linux/thermal.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
34 #include <linux/idle_inject.h>
37 #include <asm/mwait.h>
38 #include <asm/cpu_device_id.h>
40 #define MAX_TARGET_RATIO (100U)
41 /* For each undisturbed clamping period (no extra wake ups during idle time),
42 * we increment the confidence counter for the given target ratio.
43 * CONFIDENCE_OK defines the level where runtime calibration results are
46 #define CONFIDENCE_OK (3)
47 /* Default idle injection duration, driver adjust sleep time to meet target
48 * idle ratio. Similar to frequency modulation.
50 #define DEFAULT_DURATION_JIFFIES (6)
52 static struct dentry
*debug_dir
;
53 static bool poll_pkg_cstate_enable
;
55 /* Idle ratio observed using package C-state counters */
56 static unsigned int current_ratio
;
58 /* Skip the idle injection till set to true */
59 static bool should_skip
;
61 struct powerclamp_data
{
65 unsigned int window_size_now
;
66 unsigned int target_ratio
;
70 static struct powerclamp_data powerclamp_data
;
72 static struct thermal_cooling_device
*cooling_dev
;
74 static DEFINE_MUTEX(powerclamp_lock
);
76 /* This duration is in microseconds */
77 static unsigned int duration
;
78 static unsigned int pkg_cstate_ratio_cur
;
79 static unsigned int window_size
;
81 static int duration_set(const char *arg
, const struct kernel_param
*kp
)
84 unsigned long new_duration
;
86 ret
= kstrtoul(arg
, 10, &new_duration
);
89 if (new_duration
> 25 || new_duration
< 6) {
90 pr_err("Out of recommended range %lu, between 6-25ms\n",
96 mutex_lock(&powerclamp_lock
);
97 duration
= clamp(new_duration
, 6ul, 25ul) * 1000;
98 mutex_unlock(&powerclamp_lock
);
104 static int duration_get(char *buf
, const struct kernel_param
*kp
)
108 mutex_lock(&powerclamp_lock
);
109 ret
= sysfs_emit(buf
, "%d\n", duration
/ 1000);
110 mutex_unlock(&powerclamp_lock
);
115 static const struct kernel_param_ops duration_ops
= {
120 module_param_cb(duration
, &duration_ops
, NULL
, 0644);
121 MODULE_PARM_DESC(duration
, "forced idle time for each attempt in msec.");
123 #define DEFAULT_MAX_IDLE 50
124 #define MAX_ALL_CPU_IDLE 75
126 static u8 max_idle
= DEFAULT_MAX_IDLE
;
128 static cpumask_var_t idle_injection_cpu_mask
;
130 static int allocate_copy_idle_injection_mask(const struct cpumask
*copy_mask
)
132 if (cpumask_available(idle_injection_cpu_mask
))
135 /* This mask is allocated only one time and freed during module exit */
136 if (!alloc_cpumask_var(&idle_injection_cpu_mask
, GFP_KERNEL
))
140 cpumask_copy(idle_injection_cpu_mask
, copy_mask
);
145 /* Return true if the cpumask and idle percent combination is invalid */
146 static bool check_invalid(cpumask_var_t mask
, u8 idle
)
148 if (cpumask_equal(cpu_present_mask
, mask
) && idle
> MAX_ALL_CPU_IDLE
)
154 static int cpumask_set(const char *arg
, const struct kernel_param
*kp
)
156 cpumask_var_t new_mask
;
159 mutex_lock(&powerclamp_lock
);
161 /* Can't set mask when cooling device is in use */
162 if (powerclamp_data
.clamping
) {
164 goto skip_cpumask_set
;
167 ret
= alloc_cpumask_var(&new_mask
, GFP_KERNEL
);
169 goto skip_cpumask_set
;
171 ret
= bitmap_parse(arg
, strlen(arg
), cpumask_bits(new_mask
),
174 goto free_cpumask_set
;
176 if (cpumask_empty(new_mask
) || check_invalid(new_mask
, max_idle
)) {
178 goto free_cpumask_set
;
182 * When module parameters are passed from kernel command line
183 * during insmod, the module parameter callback is called
184 * before powerclamp_init(), so we can't assume that some
185 * cpumask can be allocated and copied before here. Also
186 * in this case this cpumask is used as the default mask.
188 ret
= allocate_copy_idle_injection_mask(new_mask
);
191 free_cpumask_var(new_mask
);
193 mutex_unlock(&powerclamp_lock
);
198 static int cpumask_get(char *buf
, const struct kernel_param
*kp
)
200 if (!cpumask_available(idle_injection_cpu_mask
))
203 return bitmap_print_to_pagebuf(false, buf
, cpumask_bits(idle_injection_cpu_mask
),
207 static const struct kernel_param_ops cpumask_ops
= {
212 module_param_cb(cpumask
, &cpumask_ops
, NULL
, 0644);
213 MODULE_PARM_DESC(cpumask
, "Mask of CPUs to use for idle injection.");
215 static int max_idle_set(const char *arg
, const struct kernel_param
*kp
)
220 mutex_lock(&powerclamp_lock
);
222 /* Can't set mask when cooling device is in use */
223 if (powerclamp_data
.clamping
) {
228 ret
= kstrtou8(arg
, 10, &new_max_idle
);
232 if (new_max_idle
> MAX_TARGET_RATIO
) {
237 if (!cpumask_available(idle_injection_cpu_mask
)) {
238 ret
= allocate_copy_idle_injection_mask(cpu_present_mask
);
243 if (check_invalid(idle_injection_cpu_mask
, new_max_idle
)) {
248 max_idle
= new_max_idle
;
251 mutex_unlock(&powerclamp_lock
);
256 static const struct kernel_param_ops max_idle_ops
= {
258 .get
= param_get_byte
,
261 module_param_cb(max_idle
, &max_idle_ops
, &max_idle
, 0644);
262 MODULE_PARM_DESC(max_idle
, "maximum injected idle time to the total CPU time ratio in percent range:1-100");
264 struct powerclamp_calibration_data
{
265 unsigned long confidence
; /* used for calibration, basically a counter
266 * gets incremented each time a clamping
267 * period is completed without extra wakeups
268 * once that counter is reached given level,
269 * compensation is deemed usable.
271 unsigned long steady_comp
; /* steady state compensation used when
272 * no extra wakeups occurred.
274 unsigned long dynamic_comp
; /* compensate excessive wakeup from idle
275 * mostly from external interrupts.
279 static struct powerclamp_calibration_data cal_data
[MAX_TARGET_RATIO
];
281 static int window_size_set(const char *arg
, const struct kernel_param
*kp
)
284 unsigned long new_window_size
;
286 ret
= kstrtoul(arg
, 10, &new_window_size
);
289 if (new_window_size
> 10 || new_window_size
< 2) {
290 pr_err("Out of recommended window size %lu, between 2-10\n",
295 window_size
= clamp(new_window_size
, 2ul, 10ul);
303 static const struct kernel_param_ops window_size_ops
= {
304 .set
= window_size_set
,
305 .get
= param_get_int
,
308 module_param_cb(window_size
, &window_size_ops
, &window_size
, 0644);
309 MODULE_PARM_DESC(window_size
, "sliding window in number of clamping cycles\n"
310 "\tpowerclamp controls idle ratio within this window. larger\n"
311 "\twindow size results in slower response time but more smooth\n"
312 "\tclamping results. default to 2.");
314 struct pkg_cstate_info
{
320 #define PKG_CSTATE_INIT(id) { \
321 .msr_index = MSR_PKG_C##id##_RESIDENCY, \
325 static struct pkg_cstate_info pkg_cstates
[] = {
336 static bool has_pkg_state_counter(void)
339 struct pkg_cstate_info
*info
= pkg_cstates
;
341 /* check if any one of the counter msrs exists */
342 while (info
->msr_index
) {
343 if (!rdmsrl_safe(info
->msr_index
, &val
))
351 static u64
pkg_state_counter(void)
355 struct pkg_cstate_info
*info
= pkg_cstates
;
357 while (info
->msr_index
) {
359 if (!rdmsrl_safe(info
->msr_index
, &val
))
370 static unsigned int get_compensation(int ratio
)
372 unsigned int comp
= 0;
374 if (!poll_pkg_cstate_enable
)
377 /* we only use compensation if all adjacent ones are good */
379 cal_data
[ratio
].confidence
>= CONFIDENCE_OK
&&
380 cal_data
[ratio
+ 1].confidence
>= CONFIDENCE_OK
&&
381 cal_data
[ratio
+ 2].confidence
>= CONFIDENCE_OK
) {
382 comp
= (cal_data
[ratio
].steady_comp
+
383 cal_data
[ratio
+ 1].steady_comp
+
384 cal_data
[ratio
+ 2].steady_comp
) / 3;
385 } else if (ratio
== MAX_TARGET_RATIO
- 1 &&
386 cal_data
[ratio
].confidence
>= CONFIDENCE_OK
&&
387 cal_data
[ratio
- 1].confidence
>= CONFIDENCE_OK
&&
388 cal_data
[ratio
- 2].confidence
>= CONFIDENCE_OK
) {
389 comp
= (cal_data
[ratio
].steady_comp
+
390 cal_data
[ratio
- 1].steady_comp
+
391 cal_data
[ratio
- 2].steady_comp
) / 3;
392 } else if (cal_data
[ratio
].confidence
>= CONFIDENCE_OK
&&
393 cal_data
[ratio
- 1].confidence
>= CONFIDENCE_OK
&&
394 cal_data
[ratio
+ 1].confidence
>= CONFIDENCE_OK
) {
395 comp
= (cal_data
[ratio
].steady_comp
+
396 cal_data
[ratio
- 1].steady_comp
+
397 cal_data
[ratio
+ 1].steady_comp
) / 3;
400 /* do not exceed limit */
401 if (comp
+ ratio
>= MAX_TARGET_RATIO
)
402 comp
= MAX_TARGET_RATIO
- ratio
- 1;
407 static void adjust_compensation(int target_ratio
, unsigned int win
)
410 struct powerclamp_calibration_data
*d
= &cal_data
[target_ratio
];
413 * adjust compensations if confidence level has not been reached.
415 if (d
->confidence
>= CONFIDENCE_OK
)
418 delta
= powerclamp_data
.target_ratio
- current_ratio
;
419 /* filter out bad data */
420 if (delta
>= 0 && delta
<= (1+target_ratio
/10)) {
423 roundup(delta
+d
->steady_comp
, 2)/2;
425 d
->steady_comp
= delta
;
430 static bool powerclamp_adjust_controls(unsigned int target_ratio
,
431 unsigned int guard
, unsigned int win
)
433 static u64 msr_last
, tsc_last
;
434 u64 msr_now
, tsc_now
;
437 /* check result for the last window */
438 msr_now
= pkg_state_counter();
441 /* calculate pkg cstate vs tsc ratio */
442 if (!msr_last
|| !tsc_last
)
444 else if (tsc_now
-tsc_last
) {
445 val64
= 100*(msr_now
-msr_last
);
446 do_div(val64
, (tsc_now
-tsc_last
));
447 current_ratio
= val64
;
454 adjust_compensation(target_ratio
, win
);
456 /* if we are above target+guard, skip */
457 return powerclamp_data
.target_ratio
+ guard
<= current_ratio
;
461 * This function calculates runtime from the current target ratio.
462 * This function gets called under powerclamp_lock.
464 static unsigned int get_run_time(void)
466 unsigned int compensated_ratio
;
467 unsigned int runtime
;
470 * make sure user selected ratio does not take effect until
471 * the next round. adjust target_ratio if user has changed
472 * target such that we can converge quickly.
474 powerclamp_data
.guard
= 1 + powerclamp_data
.target_ratio
/ 20;
475 powerclamp_data
.window_size_now
= window_size
;
478 * systems may have different ability to enter package level
479 * c-states, thus we need to compensate the injected idle ratio
480 * to achieve the actual target reported by the HW.
482 compensated_ratio
= powerclamp_data
.target_ratio
+
483 get_compensation(powerclamp_data
.target_ratio
);
484 if (compensated_ratio
<= 0)
485 compensated_ratio
= 1;
487 runtime
= duration
* 100 / compensated_ratio
- duration
;
493 * 1 HZ polling while clamping is active, useful for userspace
494 * to monitor actual idle ratio.
496 static void poll_pkg_cstate(struct work_struct
*dummy
);
497 static DECLARE_DELAYED_WORK(poll_pkg_cstate_work
, poll_pkg_cstate
);
498 static void poll_pkg_cstate(struct work_struct
*dummy
)
507 msr_now
= pkg_state_counter();
510 /* calculate pkg cstate vs tsc ratio */
511 if (!msr_last
|| !tsc_last
)
512 pkg_cstate_ratio_cur
= 1;
514 if (tsc_now
- tsc_last
) {
515 val64
= 100 * (msr_now
- msr_last
);
516 do_div(val64
, (tsc_now
- tsc_last
));
517 pkg_cstate_ratio_cur
= val64
;
525 mutex_lock(&powerclamp_lock
);
526 if (powerclamp_data
.clamping
)
527 schedule_delayed_work(&poll_pkg_cstate_work
, HZ
);
528 mutex_unlock(&powerclamp_lock
);
531 static struct idle_inject_device
*ii_dev
;
534 * This function is called from idle injection core on timer expiry
535 * for the run duration. This allows powerclamp to readjust or skip
536 * injecting idle for this cycle.
538 static bool idle_inject_update(void)
542 /* We can't sleep in this callback */
543 if (!mutex_trylock(&powerclamp_lock
))
546 if (!(powerclamp_data
.count
% powerclamp_data
.window_size_now
)) {
548 should_skip
= powerclamp_adjust_controls(powerclamp_data
.target_ratio
,
549 powerclamp_data
.guard
,
550 powerclamp_data
.window_size_now
);
555 unsigned int runtime
= get_run_time();
557 idle_inject_set_duration(ii_dev
, runtime
, duration
);
560 powerclamp_data
.count
++;
562 mutex_unlock(&powerclamp_lock
);
570 /* This function starts idle injection by calling idle_inject_start() */
571 static void trigger_idle_injection(void)
573 unsigned int runtime
= get_run_time();
575 idle_inject_set_duration(ii_dev
, runtime
, duration
);
576 idle_inject_start(ii_dev
);
577 powerclamp_data
.clamping
= true;
581 * This function is called from start_power_clamp() to register
582 * CPUS with powercap idle injection register and set default
583 * idle duration and latency.
585 static int powerclamp_idle_injection_register(void)
587 poll_pkg_cstate_enable
= false;
588 if (cpumask_equal(cpu_present_mask
, idle_injection_cpu_mask
)) {
589 ii_dev
= idle_inject_register_full(idle_injection_cpu_mask
, idle_inject_update
);
590 if (topology_max_packages() == 1 && topology_max_dies_per_package() == 1)
591 poll_pkg_cstate_enable
= true;
593 ii_dev
= idle_inject_register(idle_injection_cpu_mask
);
597 pr_err("powerclamp: idle_inject_register failed\n");
601 idle_inject_set_duration(ii_dev
, TICK_USEC
, duration
);
602 idle_inject_set_latency(ii_dev
, UINT_MAX
);
608 * This function is called from end_power_clamp() to stop idle injection
609 * and unregister CPUS from powercap idle injection core.
611 static void remove_idle_injection(void)
613 if (!powerclamp_data
.clamping
)
616 powerclamp_data
.clamping
= false;
617 idle_inject_stop(ii_dev
);
621 * This function is called when user change the cooling device
622 * state from zero to some other value.
624 static int start_power_clamp(void)
628 ret
= powerclamp_idle_injection_register();
630 trigger_idle_injection();
631 if (poll_pkg_cstate_enable
)
632 schedule_delayed_work(&poll_pkg_cstate_work
, 0);
639 * This function is called when user change the cooling device
640 * state from non zero value zero.
642 static void end_power_clamp(void)
644 if (powerclamp_data
.clamping
) {
645 remove_idle_injection();
646 idle_inject_unregister(ii_dev
);
650 static int powerclamp_get_max_state(struct thermal_cooling_device
*cdev
,
651 unsigned long *state
)
653 *state
= MAX_TARGET_RATIO
;
658 static int powerclamp_get_cur_state(struct thermal_cooling_device
*cdev
,
659 unsigned long *state
)
661 mutex_lock(&powerclamp_lock
);
662 *state
= powerclamp_data
.target_ratio
;
663 mutex_unlock(&powerclamp_lock
);
668 static int powerclamp_set_cur_state(struct thermal_cooling_device
*cdev
,
669 unsigned long new_target_ratio
)
673 mutex_lock(&powerclamp_lock
);
675 new_target_ratio
= clamp(new_target_ratio
, 0UL,
676 (unsigned long) (max_idle
- 1));
678 if (powerclamp_data
.target_ratio
== new_target_ratio
)
681 if (!powerclamp_data
.target_ratio
&& new_target_ratio
> 0) {
682 pr_info("Start idle injection to reduce power\n");
683 powerclamp_data
.target_ratio
= new_target_ratio
;
684 ret
= start_power_clamp();
686 powerclamp_data
.target_ratio
= 0;
688 } else if (powerclamp_data
.target_ratio
> 0 && new_target_ratio
== 0) {
689 pr_info("Stop forced idle injection\n");
691 powerclamp_data
.target_ratio
= 0;
692 } else /* adjust currently running */ {
693 unsigned int runtime
;
695 powerclamp_data
.target_ratio
= new_target_ratio
;
696 runtime
= get_run_time();
697 idle_inject_set_duration(ii_dev
, runtime
, duration
);
701 mutex_unlock(&powerclamp_lock
);
706 /* bind to generic thermal layer as cooling device*/
707 static const struct thermal_cooling_device_ops powerclamp_cooling_ops
= {
708 .get_max_state
= powerclamp_get_max_state
,
709 .get_cur_state
= powerclamp_get_cur_state
,
710 .set_cur_state
= powerclamp_set_cur_state
,
713 static const struct x86_cpu_id __initconst intel_powerclamp_ids
[] = {
714 X86_MATCH_VENDOR_FEATURE(INTEL
, X86_FEATURE_MWAIT
, NULL
),
717 MODULE_DEVICE_TABLE(x86cpu
, intel_powerclamp_ids
);
719 static int __init
powerclamp_probe(void)
722 if (!x86_match_cpu(intel_powerclamp_ids
)) {
723 pr_err("CPU does not support MWAIT\n");
727 /* The goal for idle time alignment is to achieve package cstate. */
728 if (!has_pkg_state_counter()) {
729 pr_info("No package C-state available\n");
736 static int powerclamp_debug_show(struct seq_file
*m
, void *unused
)
740 seq_printf(m
, "pct confidence steady dynamic (compensation)\n");
741 for (i
= 0; i
< MAX_TARGET_RATIO
; i
++) {
742 seq_printf(m
, "%d\t%lu\t%lu\t%lu\n",
744 cal_data
[i
].confidence
,
745 cal_data
[i
].steady_comp
,
746 cal_data
[i
].dynamic_comp
);
752 DEFINE_SHOW_ATTRIBUTE(powerclamp_debug
);
754 static inline void powerclamp_create_debug_files(void)
756 debug_dir
= debugfs_create_dir("intel_powerclamp", NULL
);
758 debugfs_create_file("powerclamp_calib", S_IRUGO
, debug_dir
, cal_data
,
759 &powerclamp_debug_fops
);
762 static int __init
powerclamp_init(void)
766 /* probe cpu features and ids here */
767 retval
= powerclamp_probe();
771 mutex_lock(&powerclamp_lock
);
772 if (!cpumask_available(idle_injection_cpu_mask
))
773 retval
= allocate_copy_idle_injection_mask(cpu_present_mask
);
774 mutex_unlock(&powerclamp_lock
);
779 /* set default limit, maybe adjusted during runtime based on feedback */
782 cooling_dev
= thermal_cooling_device_register("intel_powerclamp", NULL
,
783 &powerclamp_cooling_ops
);
784 if (IS_ERR(cooling_dev
))
788 duration
= jiffies_to_usecs(DEFAULT_DURATION_JIFFIES
);
790 powerclamp_create_debug_files();
794 module_init(powerclamp_init
);
796 static void __exit
powerclamp_exit(void)
798 mutex_lock(&powerclamp_lock
);
800 mutex_unlock(&powerclamp_lock
);
802 thermal_cooling_device_unregister(cooling_dev
);
804 cancel_delayed_work_sync(&poll_pkg_cstate_work
);
805 debugfs_remove_recursive(debug_dir
);
807 if (cpumask_available(idle_injection_cpu_mask
))
808 free_cpumask_var(idle_injection_cpu_mask
);
810 module_exit(powerclamp_exit
);
812 MODULE_IMPORT_NS("IDLE_INJECT");
814 MODULE_LICENSE("GPL");
815 MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");
816 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@linux.intel.com>");
817 MODULE_DESCRIPTION("Package Level C-state Idle Injection for Intel CPUs");