intel_powerclamp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * intel_powerclamp.c - package c-state idle injection
  3. *
  4. * Copyright (c) 2012, Intel Corporation.
  5. *
  6. * Authors:
  7. * Arjan van de Ven <arjan@linux.intel.com>
  8. * Jacob Pan <jacob.jun.pan@linux.intel.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. *
  24. * TODO:
  25. * 1. better handle wakeup from external interrupts, currently a fixed
  26. * compensation is added to clamping duration when excessive amount
  27. * of wakeups are observed during idle time. the reason is that in
  28. * case of external interrupts without need for ack, clamping down
  29. * cpu in non-irq context does not reduce irq. for majority of the
  30. * cases, clamping down cpu does help reduce irq as well, we should
  31. * be able to differenciate the two cases and give a quantitative
  32. * solution for the irqs that we can control. perhaps based on
  33. * get_cpu_iowait_time_us()
  34. *
  35. * 2. synchronization with other hw blocks
  36. *
  37. *
  38. */
  39. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  40. #include <linux/module.h>
  41. #include <linux/kernel.h>
  42. #include <linux/delay.h>
  43. #include <linux/kthread.h>
  44. #include <linux/freezer.h>
  45. #include <linux/cpu.h>
  46. #include <linux/thermal.h>
  47. #include <linux/slab.h>
  48. #include <linux/tick.h>
  49. #include <linux/debugfs.h>
  50. #include <linux/seq_file.h>
  51. #include <linux/sched/rt.h>
  52. #include <asm/nmi.h>
  53. #include <asm/msr.h>
  54. #include <asm/mwait.h>
  55. #include <asm/cpu_device_id.h>
  56. #include <asm/idle.h>
  57. #include <asm/hardirq.h>
  58. #define MAX_TARGET_RATIO (50U)
  59. /* For each undisturbed clamping period (no extra wake ups during idle time),
  60. * we increment the confidence counter for the given target ratio.
  61. * CONFIDENCE_OK defines the level where runtime calibration results are
  62. * valid.
  63. */
  64. #define CONFIDENCE_OK (3)
  65. /* Default idle injection duration, driver adjust sleep time to meet target
  66. * idle ratio. Similar to frequency modulation.
  67. */
  68. #define DEFAULT_DURATION_JIFFIES (6)
  69. static unsigned int target_mwait;
  70. static struct dentry *debug_dir;
  71. /* user selected target */
  72. static unsigned int set_target_ratio;
  73. static unsigned int current_ratio;
  74. static bool should_skip;
  75. static bool reduce_irq;
  76. static atomic_t idle_wakeup_counter;
  77. static unsigned int control_cpu; /* The cpu assigned to collect stat and update
  78. * control parameters. default to BSP but BSP
  79. * can be offlined.
  80. */
  81. static bool clamping;
  82. static struct task_struct * __percpu *powerclamp_thread;
  83. static struct thermal_cooling_device *cooling_dev;
  84. static unsigned long *cpu_clamping_mask; /* bit map for tracking per cpu
  85. * clamping thread
  86. */
  87. static unsigned int duration;
  88. static unsigned int pkg_cstate_ratio_cur;
  89. static unsigned int window_size;
  90. static int duration_set(const char *arg, const struct kernel_param *kp)
  91. {
  92. int ret = 0;
  93. unsigned long new_duration;
  94. ret = kstrtoul(arg, 10, &new_duration);
  95. if (ret)
  96. goto exit;
  97. if (new_duration > 25 || new_duration < 6) {
  98. pr_err("Out of recommended range %lu, between 6-25ms\n",
  99. new_duration);
  100. ret = -EINVAL;
  101. }
  102. duration = clamp(new_duration, 6ul, 25ul);
  103. smp_mb();
  104. exit:
  105. return ret;
  106. }
  107. static const struct kernel_param_ops duration_ops = {
  108. .set = duration_set,
  109. .get = param_get_int,
  110. };
  111. module_param_cb(duration, &duration_ops, &duration, 0644);
  112. MODULE_PARM_DESC(duration, "forced idle time for each attempt in msec.");
  113. struct powerclamp_calibration_data {
  114. unsigned long confidence; /* used for calibration, basically a counter
  115. * gets incremented each time a clamping
  116. * period is completed without extra wakeups
  117. * once that counter is reached given level,
  118. * compensation is deemed usable.
  119. */
  120. unsigned long steady_comp; /* steady state compensation used when
  121. * no extra wakeups occurred.
  122. */
  123. unsigned long dynamic_comp; /* compensate excessive wakeup from idle
  124. * mostly from external interrupts.
  125. */
  126. };
  127. static struct powerclamp_calibration_data cal_data[MAX_TARGET_RATIO];
  128. static int window_size_set(const char *arg, const struct kernel_param *kp)
  129. {
  130. int ret = 0;
  131. unsigned long new_window_size;
  132. ret = kstrtoul(arg, 10, &new_window_size);
  133. if (ret)
  134. goto exit_win;
  135. if (new_window_size > 10 || new_window_size < 2) {
  136. pr_err("Out of recommended window size %lu, between 2-10\n",
  137. new_window_size);
  138. ret = -EINVAL;
  139. }
  140. window_size = clamp(new_window_size, 2ul, 10ul);
  141. smp_mb();
  142. exit_win:
  143. return ret;
  144. }
  145. static const struct kernel_param_ops window_size_ops = {
  146. .set = window_size_set,
  147. .get = param_get_int,
  148. };
  149. module_param_cb(window_size, &window_size_ops, &window_size, 0644);
  150. MODULE_PARM_DESC(window_size, "sliding window in number of clamping cycles\n"
  151. "\tpowerclamp controls idle ratio within this window. larger\n"
  152. "\twindow size results in slower response time but more smooth\n"
  153. "\tclamping results. default to 2.");
  154. static void find_target_mwait(void)
  155. {
  156. unsigned int eax, ebx, ecx, edx;
  157. unsigned int highest_cstate = 0;
  158. unsigned int highest_subcstate = 0;
  159. int i;
  160. if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
  161. return;
  162. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
  163. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  164. !(ecx & CPUID5_ECX_INTERRUPT_BREAK))
  165. return;
  166. edx >>= MWAIT_SUBSTATE_SIZE;
  167. for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
  168. if (edx & MWAIT_SUBSTATE_MASK) {
  169. highest_cstate = i;
  170. highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
  171. }
  172. }
  173. target_mwait = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
  174. (highest_subcstate - 1);
  175. }
  176. struct pkg_cstate_info {
  177. bool skip;
  178. int msr_index;
  179. int cstate_id;
  180. };
  181. #define PKG_CSTATE_INIT(id) { \
  182. .msr_index = MSR_PKG_C##id##_RESIDENCY, \
  183. .cstate_id = id \
  184. }
  185. static struct pkg_cstate_info pkg_cstates[] = {
  186. PKG_CSTATE_INIT(2),
  187. PKG_CSTATE_INIT(3),
  188. PKG_CSTATE_INIT(6),
  189. PKG_CSTATE_INIT(7),
  190. PKG_CSTATE_INIT(8),
  191. PKG_CSTATE_INIT(9),
  192. PKG_CSTATE_INIT(10),
  193. {NULL},
  194. };
  195. static bool has_pkg_state_counter(void)
  196. {
  197. u64 val;
  198. struct pkg_cstate_info *info = pkg_cstates;
  199. /* check if any one of the counter msrs exists */
  200. while (info->msr_index) {
  201. if (!rdmsrl_safe(info->msr_index, &val))
  202. return true;
  203. info++;
  204. }
  205. return false;
  206. }
  207. static u64 pkg_state_counter(void)
  208. {
  209. u64 val;
  210. u64 count = 0;
  211. struct pkg_cstate_info *info = pkg_cstates;
  212. while (info->msr_index) {
  213. if (!info->skip) {
  214. if (!rdmsrl_safe(info->msr_index, &val))
  215. count += val;
  216. else
  217. info->skip = true;
  218. }
  219. info++;
  220. }
  221. return count;
  222. }
  223. static void noop_timer(unsigned long foo)
  224. {
  225. /* empty... just the fact that we get the interrupt wakes us up */
  226. }
  227. static unsigned int get_compensation(int ratio)
  228. {
  229. unsigned int comp = 0;
  230. /* we only use compensation if all adjacent ones are good */
  231. if (ratio == 1 &&
  232. cal_data[ratio].confidence >= CONFIDENCE_OK &&
  233. cal_data[ratio + 1].confidence >= CONFIDENCE_OK &&
  234. cal_data[ratio + 2].confidence >= CONFIDENCE_OK) {
  235. comp = (cal_data[ratio].steady_comp +
  236. cal_data[ratio + 1].steady_comp +
  237. cal_data[ratio + 2].steady_comp) / 3;
  238. } else if (ratio == MAX_TARGET_RATIO - 1 &&
  239. cal_data[ratio].confidence >= CONFIDENCE_OK &&
  240. cal_data[ratio - 1].confidence >= CONFIDENCE_OK &&
  241. cal_data[ratio - 2].confidence >= CONFIDENCE_OK) {
  242. comp = (cal_data[ratio].steady_comp +
  243. cal_data[ratio - 1].steady_comp +
  244. cal_data[ratio - 2].steady_comp) / 3;
  245. } else if (cal_data[ratio].confidence >= CONFIDENCE_OK &&
  246. cal_data[ratio - 1].confidence >= CONFIDENCE_OK &&
  247. cal_data[ratio + 1].confidence >= CONFIDENCE_OK) {
  248. comp = (cal_data[ratio].steady_comp +
  249. cal_data[ratio - 1].steady_comp +
  250. cal_data[ratio + 1].steady_comp) / 3;
  251. }
  252. /* REVISIT: simple penalty of double idle injection */
  253. if (reduce_irq)
  254. comp = ratio;
  255. /* do not exceed limit */
  256. if (comp + ratio >= MAX_TARGET_RATIO)
  257. comp = MAX_TARGET_RATIO - ratio - 1;
  258. return comp;
  259. }
  260. static void adjust_compensation(int target_ratio, unsigned int win)
  261. {
  262. int delta;
  263. struct powerclamp_calibration_data *d = &cal_data[target_ratio];
  264. /*
  265. * adjust compensations if confidence level has not been reached or
  266. * there are too many wakeups during the last idle injection period, we
  267. * cannot trust the data for compensation.
  268. */
  269. if (d->confidence >= CONFIDENCE_OK ||
  270. atomic_read(&idle_wakeup_counter) >
  271. win * num_online_cpus())
  272. return;
  273. delta = set_target_ratio - current_ratio;
  274. /* filter out bad data */
  275. if (delta >= 0 && delta <= (1+target_ratio/10)) {
  276. if (d->steady_comp)
  277. d->steady_comp =
  278. roundup(delta+d->steady_comp, 2)/2;
  279. else
  280. d->steady_comp = delta;
  281. d->confidence++;
  282. }
  283. }
  284. static bool powerclamp_adjust_controls(unsigned int target_ratio,
  285. unsigned int guard, unsigned int win)
  286. {
  287. static u64 msr_last, tsc_last;
  288. u64 msr_now, tsc_now;
  289. u64 val64;
  290. /* check result for the last window */
  291. msr_now = pkg_state_counter();
  292. rdtscll(tsc_now);
  293. /* calculate pkg cstate vs tsc ratio */
  294. if (!msr_last || !tsc_last)
  295. current_ratio = 1;
  296. else if (tsc_now-tsc_last) {
  297. val64 = 100*(msr_now-msr_last);
  298. do_div(val64, (tsc_now-tsc_last));
  299. current_ratio = val64;
  300. }
  301. /* update record */
  302. msr_last = msr_now;
  303. tsc_last = tsc_now;
  304. adjust_compensation(target_ratio, win);
  305. /*
  306. * too many external interrupts, set flag such
  307. * that we can take measure later.
  308. */
  309. reduce_irq = atomic_read(&idle_wakeup_counter) >=
  310. 2 * win * num_online_cpus();
  311. atomic_set(&idle_wakeup_counter, 0);
  312. /* if we are above target+guard, skip */
  313. return set_target_ratio + guard <= current_ratio;
  314. }
  315. static int clamp_thread(void *arg)
  316. {
  317. int cpunr = (unsigned long)arg;
  318. DEFINE_TIMER(wakeup_timer, noop_timer, 0, 0);
  319. static const struct sched_param param = {
  320. .sched_priority = MAX_USER_RT_PRIO/2,
  321. };
  322. unsigned int count = 0;
  323. unsigned int target_ratio;
  324. set_bit(cpunr, cpu_clamping_mask);
  325. set_freezable();
  326. init_timer_on_stack(&wakeup_timer);
  327. sched_setscheduler(current, SCHED_FIFO, &param);
  328. while (true == clamping && !kthread_should_stop() &&
  329. cpu_online(cpunr)) {
  330. int sleeptime;
  331. unsigned long target_jiffies;
  332. unsigned int guard;
  333. unsigned int compensation = 0;
  334. int interval; /* jiffies to sleep for each attempt */
  335. unsigned int duration_jiffies = msecs_to_jiffies(duration);
  336. unsigned int window_size_now;
  337. try_to_freeze();
  338. /*
  339. * make sure user selected ratio does not take effect until
  340. * the next round. adjust target_ratio if user has changed
  341. * target such that we can converge quickly.
  342. */
  343. target_ratio = set_target_ratio;
  344. guard = 1 + target_ratio/20;
  345. window_size_now = window_size;
  346. count++;
  347. /*
  348. * systems may have different ability to enter package level
  349. * c-states, thus we need to compensate the injected idle ratio
  350. * to achieve the actual target reported by the HW.
  351. */
  352. compensation = get_compensation(target_ratio);
  353. interval = duration_jiffies*100/(target_ratio+compensation);
  354. /* align idle time */
  355. target_jiffies = roundup(jiffies, interval);
  356. sleeptime = target_jiffies - jiffies;
  357. if (sleeptime <= 0)
  358. sleeptime = 1;
  359. schedule_timeout_interruptible(sleeptime);
  360. /*
  361. * only elected controlling cpu can collect stats and update
  362. * control parameters.
  363. */
  364. if (cpunr == control_cpu && !(count%window_size_now)) {
  365. should_skip =
  366. powerclamp_adjust_controls(target_ratio,
  367. guard, window_size_now);
  368. smp_mb();
  369. }
  370. if (should_skip)
  371. continue;
  372. target_jiffies = jiffies + duration_jiffies;
  373. mod_timer(&wakeup_timer, target_jiffies);
  374. if (unlikely(local_softirq_pending()))
  375. continue;
  376. /*
  377. * stop tick sched during idle time, interrupts are still
  378. * allowed. thus jiffies are updated properly.
  379. */
  380. preempt_disable();
  381. /* mwait until target jiffies is reached */
  382. while (time_before(jiffies, target_jiffies)) {
  383. unsigned long ecx = 1;
  384. unsigned long eax = target_mwait;
  385. /*
  386. * REVISIT: may call enter_idle() to notify drivers who
  387. * can save power during cpu idle. same for exit_idle()
  388. */
  389. local_touch_nmi();
  390. stop_critical_timings();
  391. mwait_idle_with_hints(eax, ecx);
  392. start_critical_timings();
  393. atomic_inc(&idle_wakeup_counter);
  394. }
  395. preempt_enable();
  396. }
  397. del_timer_sync(&wakeup_timer);
  398. clear_bit(cpunr, cpu_clamping_mask);
  399. return 0;
  400. }
  401. /*
  402. * 1 HZ polling while clamping is active, useful for userspace
  403. * to monitor actual idle ratio.
  404. */
  405. static void poll_pkg_cstate(struct work_struct *dummy);
  406. static DECLARE_DELAYED_WORK(poll_pkg_cstate_work, poll_pkg_cstate);
  407. static void poll_pkg_cstate(struct work_struct *dummy)
  408. {
  409. static u64 msr_last;
  410. static u64 tsc_last;
  411. static unsigned long jiffies_last;
  412. u64 msr_now;
  413. unsigned long jiffies_now;
  414. u64 tsc_now;
  415. u64 val64;
  416. msr_now = pkg_state_counter();
  417. rdtscll(tsc_now);
  418. jiffies_now = jiffies;
  419. /* calculate pkg cstate vs tsc ratio */
  420. if (!msr_last || !tsc_last)
  421. pkg_cstate_ratio_cur = 1;
  422. else {
  423. if (tsc_now - tsc_last) {
  424. val64 = 100 * (msr_now - msr_last);
  425. do_div(val64, (tsc_now - tsc_last));
  426. pkg_cstate_ratio_cur = val64;
  427. }
  428. }
  429. /* update record */
  430. msr_last = msr_now;
  431. jiffies_last = jiffies_now;
  432. tsc_last = tsc_now;
  433. if (true == clamping)
  434. schedule_delayed_work(&poll_pkg_cstate_work, HZ);
  435. }
  436. static int start_power_clamp(void)
  437. {
  438. unsigned long cpu;
  439. struct task_struct *thread;
  440. /* check if pkg cstate counter is completely 0, abort in this case */
  441. if (!has_pkg_state_counter()) {
  442. pr_err("pkg cstate counter not functional, abort\n");
  443. return -EINVAL;
  444. }
  445. set_target_ratio = clamp(set_target_ratio, 0U, MAX_TARGET_RATIO - 1);
  446. /* prevent cpu hotplug */
  447. get_online_cpus();
  448. /* prefer BSP */
  449. control_cpu = 0;
  450. if (!cpu_online(control_cpu))
  451. control_cpu = smp_processor_id();
  452. clamping = true;
  453. schedule_delayed_work(&poll_pkg_cstate_work, 0);
  454. /* start one thread per online cpu */
  455. for_each_online_cpu(cpu) {
  456. struct task_struct **p =
  457. per_cpu_ptr(powerclamp_thread, cpu);
  458. thread = kthread_create_on_node(clamp_thread,
  459. (void *) cpu,
  460. cpu_to_node(cpu),
  461. "kidle_inject/%ld", cpu);
  462. /* bind to cpu here */
  463. if (likely(!IS_ERR(thread))) {
  464. kthread_bind(thread, cpu);
  465. wake_up_process(thread);
  466. *p = thread;
  467. }
  468. }
  469. put_online_cpus();
  470. return 0;
  471. }
  472. static void end_power_clamp(void)
  473. {
  474. int i;
  475. struct task_struct *thread;
  476. clamping = false;
  477. /*
  478. * make clamping visible to other cpus and give per cpu clamping threads
  479. * sometime to exit, or gets killed later.
  480. */
  481. smp_mb();
  482. msleep(20);
  483. if (bitmap_weight(cpu_clamping_mask, num_possible_cpus())) {
  484. for_each_set_bit(i, cpu_clamping_mask, num_possible_cpus()) {
  485. pr_debug("clamping thread for cpu %d alive, kill\n", i);
  486. thread = *per_cpu_ptr(powerclamp_thread, i);
  487. kthread_stop(thread);
  488. }
  489. }
  490. }
  491. static int powerclamp_cpu_callback(struct notifier_block *nfb,
  492. unsigned long action, void *hcpu)
  493. {
  494. unsigned long cpu = (unsigned long)hcpu;
  495. struct task_struct *thread;
  496. struct task_struct **percpu_thread =
  497. per_cpu_ptr(powerclamp_thread, cpu);
  498. if (false == clamping)
  499. goto exit_ok;
  500. switch (action) {
  501. case CPU_ONLINE:
  502. thread = kthread_create_on_node(clamp_thread,
  503. (void *) cpu,
  504. cpu_to_node(cpu),
  505. "kidle_inject/%lu", cpu);
  506. if (likely(!IS_ERR(thread))) {
  507. kthread_bind(thread, cpu);
  508. wake_up_process(thread);
  509. *percpu_thread = thread;
  510. }
  511. /* prefer BSP as controlling CPU */
  512. if (cpu == 0) {
  513. control_cpu = 0;
  514. smp_mb();
  515. }
  516. break;
  517. case CPU_DEAD:
  518. if (test_bit(cpu, cpu_clamping_mask)) {
  519. pr_err("cpu %lu dead but powerclamping thread is not\n",
  520. cpu);
  521. kthread_stop(*percpu_thread);
  522. }
  523. if (cpu == control_cpu) {
  524. control_cpu = smp_processor_id();
  525. smp_mb();
  526. }
  527. }
  528. exit_ok:
  529. return NOTIFY_OK;
  530. }
  531. static struct notifier_block powerclamp_cpu_notifier = {
  532. .notifier_call = powerclamp_cpu_callback,
  533. };
  534. static int powerclamp_get_max_state(struct thermal_cooling_device *cdev,
  535. unsigned long *state)
  536. {
  537. *state = MAX_TARGET_RATIO;
  538. return 0;
  539. }
  540. static int powerclamp_get_cur_state(struct thermal_cooling_device *cdev,
  541. unsigned long *state)
  542. {
  543. if (true == clamping)
  544. *state = pkg_cstate_ratio_cur;
  545. else
  546. /* to save power, do not poll idle ratio while not clamping */
  547. *state = -1; /* indicates invalid state */
  548. return 0;
  549. }
  550. static int powerclamp_set_cur_state(struct thermal_cooling_device *cdev,
  551. unsigned long new_target_ratio)
  552. {
  553. int ret = 0;
  554. new_target_ratio = clamp(new_target_ratio, 0UL,
  555. (unsigned long) (MAX_TARGET_RATIO-1));
  556. if (set_target_ratio == 0 && new_target_ratio > 0) {
  557. pr_info("Start idle injection to reduce power\n");
  558. set_target_ratio = new_target_ratio;
  559. ret = start_power_clamp();
  560. goto exit_set;
  561. } else if (set_target_ratio > 0 && new_target_ratio == 0) {
  562. pr_info("Stop forced idle injection\n");
  563. set_target_ratio = 0;
  564. end_power_clamp();
  565. } else /* adjust currently running */ {
  566. set_target_ratio = new_target_ratio;
  567. /* make new set_target_ratio visible to other cpus */
  568. smp_mb();
  569. }
  570. exit_set:
  571. return ret;
  572. }
  573. /* bind to generic thermal layer as cooling device*/
  574. static struct thermal_cooling_device_ops powerclamp_cooling_ops = {
  575. .get_max_state = powerclamp_get_max_state,
  576. .get_cur_state = powerclamp_get_cur_state,
  577. .set_cur_state = powerclamp_set_cur_state,
  578. };
  579. /* runs on Nehalem and later */
  580. static const struct x86_cpu_id intel_powerclamp_ids[] __initconst = {
  581. { X86_VENDOR_INTEL, 6, 0x1a},
  582. { X86_VENDOR_INTEL, 6, 0x1c},
  583. { X86_VENDOR_INTEL, 6, 0x1e},
  584. { X86_VENDOR_INTEL, 6, 0x1f},
  585. { X86_VENDOR_INTEL, 6, 0x25},
  586. { X86_VENDOR_INTEL, 6, 0x26},
  587. { X86_VENDOR_INTEL, 6, 0x2a},
  588. { X86_VENDOR_INTEL, 6, 0x2c},
  589. { X86_VENDOR_INTEL, 6, 0x2d},
  590. { X86_VENDOR_INTEL, 6, 0x2e},
  591. { X86_VENDOR_INTEL, 6, 0x2f},
  592. { X86_VENDOR_INTEL, 6, 0x37},
  593. { X86_VENDOR_INTEL, 6, 0x3a},
  594. { X86_VENDOR_INTEL, 6, 0x3c},
  595. { X86_VENDOR_INTEL, 6, 0x3d},
  596. { X86_VENDOR_INTEL, 6, 0x3e},
  597. { X86_VENDOR_INTEL, 6, 0x3f},
  598. { X86_VENDOR_INTEL, 6, 0x45},
  599. { X86_VENDOR_INTEL, 6, 0x46},
  600. { X86_VENDOR_INTEL, 6, 0x4c},
  601. { X86_VENDOR_INTEL, 6, 0x4d},
  602. { X86_VENDOR_INTEL, 6, 0x4f},
  603. { X86_VENDOR_INTEL, 6, 0x56},
  604. { X86_VENDOR_INTEL, 6, 0x57},
  605. {}
  606. };
  607. MODULE_DEVICE_TABLE(x86cpu, intel_powerclamp_ids);
  608. static int __init powerclamp_probe(void)
  609. {
  610. if (!x86_match_cpu(intel_powerclamp_ids)) {
  611. pr_err("Intel powerclamp does not run on family %d model %d\n",
  612. boot_cpu_data.x86, boot_cpu_data.x86_model);
  613. return -ENODEV;
  614. }
  615. if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC) ||
  616. !boot_cpu_has(X86_FEATURE_CONSTANT_TSC) ||
  617. !boot_cpu_has(X86_FEATURE_MWAIT) ||
  618. !boot_cpu_has(X86_FEATURE_ARAT))
  619. return -ENODEV;
  620. /* find the deepest mwait value */
  621. find_target_mwait();
  622. return 0;
  623. }
  624. static int powerclamp_debug_show(struct seq_file *m, void *unused)
  625. {
  626. int i = 0;
  627. seq_printf(m, "controlling cpu: %d\n", control_cpu);
  628. seq_printf(m, "pct confidence steady dynamic (compensation)\n");
  629. for (i = 0; i < MAX_TARGET_RATIO; i++) {
  630. seq_printf(m, "%d\t%lu\t%lu\t%lu\n",
  631. i,
  632. cal_data[i].confidence,
  633. cal_data[i].steady_comp,
  634. cal_data[i].dynamic_comp);
  635. }
  636. return 0;
  637. }
  638. static int powerclamp_debug_open(struct inode *inode,
  639. struct file *file)
  640. {
  641. return single_open(file, powerclamp_debug_show, inode->i_private);
  642. }
  643. static const struct file_operations powerclamp_debug_fops = {
  644. .open = powerclamp_debug_open,
  645. .read = seq_read,
  646. .llseek = seq_lseek,
  647. .release = single_release,
  648. .owner = THIS_MODULE,
  649. };
  650. static inline void powerclamp_create_debug_files(void)
  651. {
  652. debug_dir = debugfs_create_dir("intel_powerclamp", NULL);
  653. if (!debug_dir)
  654. return;
  655. if (!debugfs_create_file("powerclamp_calib", S_IRUGO, debug_dir,
  656. cal_data, &powerclamp_debug_fops))
  657. goto file_error;
  658. return;
  659. file_error:
  660. debugfs_remove_recursive(debug_dir);
  661. }
  662. static int __init powerclamp_init(void)
  663. {
  664. int retval;
  665. int bitmap_size;
  666. bitmap_size = BITS_TO_LONGS(num_possible_cpus()) * sizeof(long);
  667. cpu_clamping_mask = kzalloc(bitmap_size, GFP_KERNEL);
  668. if (!cpu_clamping_mask)
  669. return -ENOMEM;
  670. /* probe cpu features and ids here */
  671. retval = powerclamp_probe();
  672. if (retval)
  673. goto exit_free;
  674. /* set default limit, maybe adjusted during runtime based on feedback */
  675. window_size = 2;
  676. register_hotcpu_notifier(&powerclamp_cpu_notifier);
  677. powerclamp_thread = alloc_percpu(struct task_struct *);
  678. if (!powerclamp_thread) {
  679. retval = -ENOMEM;
  680. goto exit_unregister;
  681. }
  682. cooling_dev = thermal_cooling_device_register("intel_powerclamp", NULL,
  683. &powerclamp_cooling_ops);
  684. if (IS_ERR(cooling_dev)) {
  685. retval = -ENODEV;
  686. goto exit_free_thread;
  687. }
  688. if (!duration)
  689. duration = jiffies_to_msecs(DEFAULT_DURATION_JIFFIES);
  690. powerclamp_create_debug_files();
  691. return 0;
  692. exit_free_thread:
  693. free_percpu(powerclamp_thread);
  694. exit_unregister:
  695. unregister_hotcpu_notifier(&powerclamp_cpu_notifier);
  696. exit_free:
  697. kfree(cpu_clamping_mask);
  698. return retval;
  699. }
  700. module_init(powerclamp_init);
  701. static void __exit powerclamp_exit(void)
  702. {
  703. unregister_hotcpu_notifier(&powerclamp_cpu_notifier);
  704. end_power_clamp();
  705. free_percpu(powerclamp_thread);
  706. thermal_cooling_device_unregister(cooling_dev);
  707. kfree(cpu_clamping_mask);
  708. cancel_delayed_work_sync(&poll_pkg_cstate_work);
  709. debugfs_remove_recursive(debug_dir);
  710. }
  711. module_exit(powerclamp_exit);
  712. MODULE_LICENSE("GPL");
  713. MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");
  714. MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@linux.intel.com>");
  715. MODULE_DESCRIPTION("Package Level C-state Idle Injection for Intel CPUs");