xref: /linux/drivers/thermal/intel/intel_powerclamp.c (revision 5d7f3375cd574fc83f0cd30839b21455c0e79ae2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * intel_powerclamp.c - package c-state idle injection
4  *
5  * Copyright (c) 2012-2023, Intel Corporation.
6  *
7  * Authors:
8  *     Arjan van de Ven <arjan@linux.intel.com>
9  *     Jacob Pan <jacob.jun.pan@linux.intel.com>
10  *
11  *	TODO:
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()
21  *
22  *	     2. synchronization with other hw blocks
23  */
24 
25 #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
26 
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>
35 
36 #include <asm/msr.h>
37 #include <asm/mwait.h>
38 #include <asm/cpu_device_id.h>
39 
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
44  * valid.
45  */
46 #define CONFIDENCE_OK (3)
47 /* Default idle injection duration, driver adjust sleep time to meet target
48  * idle ratio. Similar to frequency modulation.
49  */
50 #define DEFAULT_DURATION_JIFFIES (6)
51 
52 static struct dentry *debug_dir;
53 static bool poll_pkg_cstate_enable;
54 
55 /* Idle ratio observed using package C-state counters */
56 static unsigned int current_ratio;
57 
58 /* Skip the idle injection till set to true */
59 static bool should_skip;
60 
61 struct powerclamp_data {
62 	unsigned int cpu;
63 	unsigned int count;
64 	unsigned int guard;
65 	unsigned int window_size_now;
66 	unsigned int target_ratio;
67 	bool clamping;
68 };
69 
70 static struct powerclamp_data powerclamp_data;
71 
72 static struct thermal_cooling_device *cooling_dev;
73 
74 static DEFINE_MUTEX(powerclamp_lock);
75 
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;
80 
81 static int duration_set(const char *arg, const struct kernel_param *kp)
82 {
83 	int ret = 0;
84 	unsigned long new_duration;
85 
86 	ret = kstrtoul(arg, 10, &new_duration);
87 	if (ret)
88 		goto exit;
89 	if (new_duration > 25 || new_duration < 6) {
90 		pr_err("Out of recommended range %lu, between 6-25ms\n",
91 			new_duration);
92 		ret = -EINVAL;
93 		goto exit;
94 	}
95 
96 	mutex_lock(&powerclamp_lock);
97 	duration = new_duration * 1000;
98 	mutex_unlock(&powerclamp_lock);
99 exit:
100 
101 	return ret;
102 }
103 
104 static int duration_get(char *buf, const struct kernel_param *kp)
105 {
106 	guard(mutex)(&powerclamp_lock);
107 
108 	return sysfs_emit(buf, "%u\n", duration / 1000);
109 }
110 
111 static const struct kernel_param_ops duration_ops = {
112 	.set = duration_set,
113 	.get = duration_get,
114 };
115 
116 module_param_cb(duration, &duration_ops, NULL, 0644);
117 MODULE_PARM_DESC(duration, "forced idle time for each attempt in msec.");
118 
119 #define DEFAULT_MAX_IDLE	50
120 #define MAX_ALL_CPU_IDLE	75
121 
122 static u8 max_idle = DEFAULT_MAX_IDLE;
123 
124 static cpumask_var_t idle_injection_cpu_mask;
125 
126 static int allocate_copy_idle_injection_mask(const struct cpumask *copy_mask)
127 {
128 	if (cpumask_available(idle_injection_cpu_mask))
129 		goto copy_mask;
130 
131 	/* This mask is allocated only one time and freed during module exit */
132 	if (!alloc_cpumask_var(&idle_injection_cpu_mask, GFP_KERNEL))
133 		return -ENOMEM;
134 
135 copy_mask:
136 	cpumask_copy(idle_injection_cpu_mask, copy_mask);
137 
138 	return 0;
139 }
140 
141 /* Return true if the cpumask and idle percent combination is invalid */
142 static bool check_invalid(const struct cpumask *mask, u8 idle)
143 {
144 	return cpumask_equal(cpu_present_mask, mask) && idle > MAX_ALL_CPU_IDLE;
145 }
146 
147 static int cpumask_set(const char *arg, const struct kernel_param *kp)
148 {
149 	cpumask_var_t new_mask;
150 	int ret;
151 
152 	mutex_lock(&powerclamp_lock);
153 
154 	/* Can't set mask when cooling device is in use */
155 	if (powerclamp_data.clamping) {
156 		ret = -EAGAIN;
157 		goto skip_cpumask_set;
158 	}
159 
160 	ret = alloc_cpumask_var(&new_mask, GFP_KERNEL);
161 	if (!ret)
162 		goto skip_cpumask_set;
163 
164 	ret = bitmap_parse(arg, strlen(arg), cpumask_bits(new_mask),
165 			   nr_cpumask_bits);
166 	if (ret)
167 		goto free_cpumask_set;
168 
169 	if (cpumask_empty(new_mask) || check_invalid(new_mask, max_idle)) {
170 		ret = -EINVAL;
171 		goto free_cpumask_set;
172 	}
173 
174 	/*
175 	 * When module parameters are passed from kernel command line
176 	 * during insmod, the module parameter callback is called
177 	 * before powerclamp_init(), so we can't assume that some
178 	 * cpumask can be allocated and copied before here. Also
179 	 * in this case this cpumask is used as the default mask.
180 	 */
181 	ret = allocate_copy_idle_injection_mask(new_mask);
182 
183 free_cpumask_set:
184 	free_cpumask_var(new_mask);
185 skip_cpumask_set:
186 	mutex_unlock(&powerclamp_lock);
187 
188 	return ret;
189 }
190 
191 static int cpumask_get(char *buf, const struct kernel_param *kp)
192 {
193 	if (!cpumask_available(idle_injection_cpu_mask))
194 		return -ENODEV;
195 
196 	return sysfs_emit(buf, "%*pb\n", cpumask_pr_args(idle_injection_cpu_mask));
197 }
198 
199 static const struct kernel_param_ops cpumask_ops = {
200 	.set = cpumask_set,
201 	.get = cpumask_get,
202 };
203 
204 module_param_cb(cpumask, &cpumask_ops, NULL, 0644);
205 MODULE_PARM_DESC(cpumask, "Mask of CPUs to use for idle injection.");
206 
207 static int max_idle_set(const char *arg, const struct kernel_param *kp)
208 {
209 	u8 new_max_idle;
210 	int ret;
211 
212 	guard(mutex)(&powerclamp_lock);
213 
214 	/* Can't set mask when cooling device is in use */
215 	if (powerclamp_data.clamping)
216 		return -EAGAIN;
217 
218 	ret = kstrtou8(arg, 10, &new_max_idle);
219 	if (ret)
220 		return ret;
221 
222 	if (new_max_idle > MAX_TARGET_RATIO)
223 		return -EINVAL;
224 
225 	if (!cpumask_available(idle_injection_cpu_mask)) {
226 		ret = allocate_copy_idle_injection_mask(cpu_present_mask);
227 		if (ret)
228 			return ret;
229 	}
230 
231 	if (check_invalid(idle_injection_cpu_mask, new_max_idle))
232 		return -EINVAL;
233 
234 	max_idle = new_max_idle;
235 
236 	return ret;
237 }
238 
239 static const struct kernel_param_ops max_idle_ops = {
240 	.set = max_idle_set,
241 	.get = param_get_byte,
242 };
243 
244 module_param_cb(max_idle, &max_idle_ops, &max_idle, 0644);
245 MODULE_PARM_DESC(max_idle, "maximum injected idle time to the total CPU time ratio in percent range:1-100");
246 
247 struct powerclamp_calibration_data {
248 	unsigned long confidence;  /* used for calibration, basically a counter
249 				    * gets incremented each time a clamping
250 				    * period is completed without extra wakeups
251 				    * once that counter is reached given level,
252 				    * compensation is deemed usable.
253 				    */
254 	unsigned long steady_comp; /* steady state compensation used when
255 				    * no extra wakeups occurred.
256 				    */
257 	unsigned long dynamic_comp; /* compensate excessive wakeup from idle
258 				     * mostly from external interrupts.
259 				     */
260 };
261 
262 static struct powerclamp_calibration_data cal_data[MAX_TARGET_RATIO];
263 
264 static int window_size_set(const char *arg, const struct kernel_param *kp)
265 {
266 	int ret = 0;
267 	unsigned long new_window_size;
268 
269 	ret = kstrtoul(arg, 10, &new_window_size);
270 	if (ret)
271 		goto exit_win;
272 	if (new_window_size > 10 || new_window_size < 2) {
273 		pr_err("Out of recommended window size %lu, between 2-10\n",
274 			new_window_size);
275 		ret = -EINVAL;
276 		goto exit_win;
277 	}
278 
279 	window_size = new_window_size;
280 	smp_mb();
281 
282 exit_win:
283 
284 	return ret;
285 }
286 
287 static const struct kernel_param_ops window_size_ops = {
288 	.set = window_size_set,
289 	.get = param_get_int,
290 };
291 
292 module_param_cb(window_size, &window_size_ops, &window_size, 0644);
293 MODULE_PARM_DESC(window_size, "sliding window in number of clamping cycles\n"
294 	"\tpowerclamp controls idle ratio within this window. larger\n"
295 	"\twindow size results in slower response time but more smooth\n"
296 	"\tclamping results. default to 2.");
297 
298 struct pkg_cstate_info {
299 	bool skip;
300 	int msr_index;
301 	int cstate_id;
302 };
303 
304 #define PKG_CSTATE_INIT(id) {				\
305 		.msr_index = MSR_PKG_C##id##_RESIDENCY, \
306 		.cstate_id = id				\
307 			}
308 
309 static struct pkg_cstate_info pkg_cstates[] = {
310 	PKG_CSTATE_INIT(2),
311 	PKG_CSTATE_INIT(3),
312 	PKG_CSTATE_INIT(6),
313 	PKG_CSTATE_INIT(7),
314 	PKG_CSTATE_INIT(8),
315 	PKG_CSTATE_INIT(9),
316 	PKG_CSTATE_INIT(10),
317 	{NULL},
318 };
319 
320 static bool has_pkg_state_counter(void)
321 {
322 	u64 val;
323 	struct pkg_cstate_info *info = pkg_cstates;
324 
325 	/* check if any one of the counter msrs exists */
326 	while (info->msr_index) {
327 		if (!rdmsrq_safe(info->msr_index, &val))
328 			return true;
329 		info++;
330 	}
331 
332 	return false;
333 }
334 
335 static u64 pkg_state_counter(void)
336 {
337 	u64 val;
338 	u64 count = 0;
339 	struct pkg_cstate_info *info = pkg_cstates;
340 
341 	while (info->msr_index) {
342 		if (!info->skip) {
343 			if (!rdmsrq_safe(info->msr_index, &val))
344 				count += val;
345 			else
346 				info->skip = true;
347 		}
348 		info++;
349 	}
350 
351 	return count;
352 }
353 
354 static unsigned int get_compensation(int ratio)
355 {
356 	unsigned int comp = 0;
357 
358 	if (!poll_pkg_cstate_enable)
359 		return 0;
360 
361 	/* we only use compensation if all adjacent ones are good */
362 	if (ratio == 1 &&
363 		cal_data[ratio].confidence >= CONFIDENCE_OK &&
364 		cal_data[ratio + 1].confidence >= CONFIDENCE_OK &&
365 		cal_data[ratio + 2].confidence >= CONFIDENCE_OK) {
366 		comp = (cal_data[ratio].steady_comp +
367 			cal_data[ratio + 1].steady_comp +
368 			cal_data[ratio + 2].steady_comp) / 3;
369 	} else if (ratio == MAX_TARGET_RATIO - 1 &&
370 		cal_data[ratio].confidence >= CONFIDENCE_OK &&
371 		cal_data[ratio - 1].confidence >= CONFIDENCE_OK &&
372 		cal_data[ratio - 2].confidence >= CONFIDENCE_OK) {
373 		comp = (cal_data[ratio].steady_comp +
374 			cal_data[ratio - 1].steady_comp +
375 			cal_data[ratio - 2].steady_comp) / 3;
376 	} else if (cal_data[ratio].confidence >= CONFIDENCE_OK &&
377 		cal_data[ratio - 1].confidence >= CONFIDENCE_OK &&
378 		cal_data[ratio + 1].confidence >= CONFIDENCE_OK) {
379 		comp = (cal_data[ratio].steady_comp +
380 			cal_data[ratio - 1].steady_comp +
381 			cal_data[ratio + 1].steady_comp) / 3;
382 	}
383 
384 	/* do not exceed limit */
385 	if (comp + ratio >= MAX_TARGET_RATIO)
386 		comp = MAX_TARGET_RATIO - ratio - 1;
387 
388 	return comp;
389 }
390 
391 static void adjust_compensation(int target_ratio, unsigned int win)
392 {
393 	int delta;
394 	struct powerclamp_calibration_data *d = &cal_data[target_ratio];
395 
396 	/*
397 	 * adjust compensations if confidence level has not been reached.
398 	 */
399 	if (d->confidence >= CONFIDENCE_OK)
400 		return;
401 
402 	delta = powerclamp_data.target_ratio - current_ratio;
403 	/* filter out bad data */
404 	if (delta >= 0 && delta <= (1+target_ratio/10)) {
405 		if (d->steady_comp)
406 			d->steady_comp =
407 				roundup(delta+d->steady_comp, 2)/2;
408 		else
409 			d->steady_comp = delta;
410 		d->confidence++;
411 	}
412 }
413 
414 static bool powerclamp_adjust_controls(unsigned int target_ratio,
415 				unsigned int guard, unsigned int win)
416 {
417 	static u64 msr_last, tsc_last;
418 	u64 msr_now, tsc_now;
419 	u64 val64;
420 
421 	/* check result for the last window */
422 	msr_now = pkg_state_counter();
423 	tsc_now = rdtsc();
424 
425 	/* calculate pkg cstate vs tsc ratio */
426 	if (!msr_last || !tsc_last)
427 		current_ratio = 1;
428 	else if (tsc_now-tsc_last) {
429 		val64 = 100*(msr_now-msr_last);
430 		do_div(val64, (tsc_now-tsc_last));
431 		current_ratio = val64;
432 	}
433 
434 	/* update record */
435 	msr_last = msr_now;
436 	tsc_last = tsc_now;
437 
438 	adjust_compensation(target_ratio, win);
439 
440 	/* if we are above target+guard, skip */
441 	return powerclamp_data.target_ratio + guard <= current_ratio;
442 }
443 
444 /*
445  * This function calculates runtime from the current target ratio.
446  * This function gets called under powerclamp_lock.
447  */
448 static unsigned int get_run_time(void)
449 {
450 	unsigned int compensated_ratio;
451 	unsigned int runtime;
452 
453 	/*
454 	 * make sure user selected ratio does not take effect until
455 	 * the next round. adjust target_ratio if user has changed
456 	 * target such that we can converge quickly.
457 	 */
458 	powerclamp_data.guard = 1 + powerclamp_data.target_ratio / 20;
459 	powerclamp_data.window_size_now = window_size;
460 
461 	/*
462 	 * systems may have different ability to enter package level
463 	 * c-states, thus we need to compensate the injected idle ratio
464 	 * to achieve the actual target reported by the HW.
465 	 */
466 	compensated_ratio = powerclamp_data.target_ratio +
467 		get_compensation(powerclamp_data.target_ratio);
468 	if (compensated_ratio <= 0)
469 		compensated_ratio = 1;
470 
471 	runtime = duration * 100 / compensated_ratio - duration;
472 
473 	return runtime;
474 }
475 
476 /*
477  * 1 HZ polling while clamping is active, useful for userspace
478  * to monitor actual idle ratio.
479  */
480 static void poll_pkg_cstate(struct work_struct *dummy);
481 static DECLARE_DELAYED_WORK(poll_pkg_cstate_work, poll_pkg_cstate);
482 static void poll_pkg_cstate(struct work_struct *dummy)
483 {
484 	static u64 msr_last;
485 	static u64 tsc_last;
486 
487 	u64 msr_now;
488 	u64 tsc_now;
489 	u64 val64;
490 
491 	msr_now = pkg_state_counter();
492 	tsc_now = rdtsc();
493 
494 	/* calculate pkg cstate vs tsc ratio */
495 	if (!msr_last || !tsc_last)
496 		pkg_cstate_ratio_cur = 1;
497 	else {
498 		if (tsc_now - tsc_last) {
499 			val64 = 100 * (msr_now - msr_last);
500 			do_div(val64, (tsc_now - tsc_last));
501 			pkg_cstate_ratio_cur = val64;
502 		}
503 	}
504 
505 	/* update record */
506 	msr_last = msr_now;
507 	tsc_last = tsc_now;
508 
509 	mutex_lock(&powerclamp_lock);
510 	if (powerclamp_data.clamping)
511 		schedule_delayed_work(&poll_pkg_cstate_work, HZ);
512 	mutex_unlock(&powerclamp_lock);
513 }
514 
515 static struct idle_inject_device *ii_dev;
516 
517 /*
518  * This function is called from idle injection core on timer expiry
519  * for the run duration. This allows powerclamp to readjust or skip
520  * injecting idle for this cycle.
521  */
522 static bool idle_inject_update(void)
523 {
524 	/* We can't sleep in this callback */
525 	if (!mutex_trylock(&powerclamp_lock))
526 		return true;
527 
528 	if (!(powerclamp_data.count % powerclamp_data.window_size_now)) {
529 		unsigned int runtime;
530 
531 		should_skip = powerclamp_adjust_controls(powerclamp_data.target_ratio,
532 							 powerclamp_data.guard,
533 							 powerclamp_data.window_size_now);
534 		runtime = get_run_time();
535 		idle_inject_set_duration(ii_dev, runtime, duration);
536 	}
537 
538 	powerclamp_data.count++;
539 
540 	mutex_unlock(&powerclamp_lock);
541 
542 	return !should_skip;
543 }
544 
545 /* This function starts idle injection by calling idle_inject_start() */
546 static void trigger_idle_injection(void)
547 {
548 	unsigned int runtime = get_run_time();
549 
550 	idle_inject_set_duration(ii_dev, runtime, duration);
551 	idle_inject_start(ii_dev);
552 	powerclamp_data.clamping = true;
553 }
554 
555 /*
556  * This function is called from start_power_clamp() to register
557  * CPUS with powercap idle injection register and set default
558  * idle duration and latency.
559  */
560 static int powerclamp_idle_injection_register(void)
561 {
562 	poll_pkg_cstate_enable = false;
563 	if (cpumask_equal(cpu_present_mask, idle_injection_cpu_mask)) {
564 		ii_dev = idle_inject_register_full(idle_injection_cpu_mask, idle_inject_update);
565 		if (topology_max_packages() == 1 && topology_max_dies_per_package() == 1)
566 			poll_pkg_cstate_enable = true;
567 	} else {
568 		ii_dev = idle_inject_register(idle_injection_cpu_mask);
569 	}
570 
571 	if (!ii_dev) {
572 		pr_err("powerclamp: idle_inject_register failed\n");
573 		return -EAGAIN;
574 	}
575 
576 	idle_inject_set_duration(ii_dev, TICK_USEC, duration);
577 	idle_inject_set_latency(ii_dev, UINT_MAX);
578 
579 	return 0;
580 }
581 
582 /*
583  * This function is called from end_power_clamp() to stop idle injection
584  * and unregister CPUS from powercap idle injection core.
585  */
586 static void remove_idle_injection(void)
587 {
588 	if (!powerclamp_data.clamping)
589 		return;
590 
591 	powerclamp_data.clamping = false;
592 	idle_inject_stop(ii_dev);
593 }
594 
595 /*
596  * This function is called when user change the cooling device
597  * state from zero to some other value.
598  */
599 static int start_power_clamp(void)
600 {
601 	int ret;
602 
603 	ret = powerclamp_idle_injection_register();
604 	if (!ret) {
605 		trigger_idle_injection();
606 		if (poll_pkg_cstate_enable)
607 			schedule_delayed_work(&poll_pkg_cstate_work, 0);
608 	}
609 
610 	return ret;
611 }
612 
613 /*
614  * This function is called when user change the cooling device
615  * state from non zero value zero.
616  */
617 static void end_power_clamp(void)
618 {
619 	if (powerclamp_data.clamping) {
620 		remove_idle_injection();
621 		idle_inject_unregister(ii_dev);
622 	}
623 }
624 
625 static int powerclamp_get_max_state(struct thermal_cooling_device *cdev,
626 				 unsigned long *state)
627 {
628 	*state = MAX_TARGET_RATIO;
629 
630 	return 0;
631 }
632 
633 static int powerclamp_get_cur_state(struct thermal_cooling_device *cdev,
634 				 unsigned long *state)
635 {
636 	mutex_lock(&powerclamp_lock);
637 	*state = powerclamp_data.target_ratio;
638 	mutex_unlock(&powerclamp_lock);
639 
640 	return 0;
641 }
642 
643 static int powerclamp_set_cur_state(struct thermal_cooling_device *cdev,
644 				 unsigned long new_target_ratio)
645 {
646 	int ret = 0;
647 
648 	mutex_lock(&powerclamp_lock);
649 
650 	new_target_ratio = clamp(new_target_ratio, 0UL,
651 				(unsigned long) (max_idle - 1));
652 
653 	if (powerclamp_data.target_ratio == new_target_ratio)
654 		goto exit_set;
655 
656 	if (!powerclamp_data.target_ratio && new_target_ratio > 0) {
657 		pr_info("Start idle injection to reduce power\n");
658 		powerclamp_data.target_ratio = new_target_ratio;
659 		ret = start_power_clamp();
660 		if (ret)
661 			powerclamp_data.target_ratio = 0;
662 		goto exit_set;
663 	} else	if (powerclamp_data.target_ratio > 0 && new_target_ratio == 0) {
664 		pr_info("Stop forced idle injection\n");
665 		end_power_clamp();
666 		powerclamp_data.target_ratio = 0;
667 	} else	/* adjust currently running */ {
668 		unsigned int runtime;
669 
670 		powerclamp_data.target_ratio = new_target_ratio;
671 		runtime = get_run_time();
672 		idle_inject_set_duration(ii_dev, runtime, duration);
673 	}
674 
675 exit_set:
676 	mutex_unlock(&powerclamp_lock);
677 
678 	return ret;
679 }
680 
681 /* bind to generic thermal layer as cooling device*/
682 static const struct thermal_cooling_device_ops powerclamp_cooling_ops = {
683 	.get_max_state = powerclamp_get_max_state,
684 	.get_cur_state = powerclamp_get_cur_state,
685 	.set_cur_state = powerclamp_set_cur_state,
686 };
687 
688 static const struct x86_cpu_id __initconst intel_powerclamp_ids[] = {
689 	X86_MATCH_VENDOR_FEATURE(INTEL, X86_FEATURE_MWAIT, NULL),
690 	{}
691 };
692 MODULE_DEVICE_TABLE(x86cpu, intel_powerclamp_ids);
693 
694 static int __init powerclamp_probe(void)
695 {
696 
697 	if (!x86_match_cpu(intel_powerclamp_ids)) {
698 		pr_err("CPU does not support MWAIT\n");
699 		return -ENODEV;
700 	}
701 
702 	/* The goal for idle time alignment is to achieve package cstate. */
703 	if (!has_pkg_state_counter()) {
704 		pr_info("No package C-state available\n");
705 		return -ENODEV;
706 	}
707 
708 	return 0;
709 }
710 
711 static int powerclamp_debug_show(struct seq_file *m, void *unused)
712 {
713 	int i = 0;
714 
715 	seq_printf(m, "pct confidence steady dynamic (compensation)\n");
716 	for (i = 0; i < MAX_TARGET_RATIO; i++) {
717 		seq_printf(m, "%d\t%lu\t%lu\t%lu\n",
718 			i,
719 			cal_data[i].confidence,
720 			cal_data[i].steady_comp,
721 			cal_data[i].dynamic_comp);
722 	}
723 
724 	return 0;
725 }
726 
727 DEFINE_SHOW_ATTRIBUTE(powerclamp_debug);
728 
729 static inline void powerclamp_create_debug_files(void)
730 {
731 	debug_dir = debugfs_create_dir("intel_powerclamp", NULL);
732 
733 	debugfs_create_file("powerclamp_calib", S_IRUGO, debug_dir, cal_data,
734 			    &powerclamp_debug_fops);
735 }
736 
737 static int __init powerclamp_init(void)
738 {
739 	int retval;
740 
741 	/* probe cpu features and ids here */
742 	retval = powerclamp_probe();
743 	if (retval)
744 		return retval;
745 
746 	mutex_lock(&powerclamp_lock);
747 	if (!cpumask_available(idle_injection_cpu_mask))
748 		retval = allocate_copy_idle_injection_mask(cpu_present_mask);
749 	mutex_unlock(&powerclamp_lock);
750 
751 	if (retval)
752 		return retval;
753 
754 	/* set default limit, maybe adjusted during runtime based on feedback */
755 	window_size = 2;
756 
757 	cooling_dev = thermal_cooling_device_register("intel_powerclamp", NULL,
758 						      &powerclamp_cooling_ops);
759 	if (IS_ERR(cooling_dev))
760 		return -ENODEV;
761 
762 	if (!duration)
763 		duration = jiffies_to_usecs(DEFAULT_DURATION_JIFFIES);
764 
765 	powerclamp_create_debug_files();
766 
767 	return 0;
768 }
769 module_init(powerclamp_init);
770 
771 static void __exit powerclamp_exit(void)
772 {
773 	mutex_lock(&powerclamp_lock);
774 	end_power_clamp();
775 	mutex_unlock(&powerclamp_lock);
776 
777 	thermal_cooling_device_unregister(cooling_dev);
778 
779 	cancel_delayed_work_sync(&poll_pkg_cstate_work);
780 	debugfs_remove_recursive(debug_dir);
781 
782 	if (cpumask_available(idle_injection_cpu_mask))
783 		free_cpumask_var(idle_injection_cpu_mask);
784 }
785 module_exit(powerclamp_exit);
786 
787 MODULE_IMPORT_NS("IDLE_INJECT");
788 
789 MODULE_LICENSE("GPL");
790 MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");
791 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@linux.intel.com>");
792 MODULE_DESCRIPTION("Package Level C-state Idle Injection for Intel CPUs");
793