intel_powerclamp.c 20 KB

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