intel_idle.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * intel_idle.c - native hardware idle loop for modern Intel processors
  3. *
  4. * Copyright (c) 2010, Intel Corporation.
  5. * Len Brown <len.brown@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. /*
  21. * intel_idle is a cpuidle driver that loads on specific Intel processors
  22. * in lieu of the legacy ACPI processor_idle driver. The intent is to
  23. * make Linux more efficient on these processors, as intel_idle knows
  24. * more than ACPI, as well as make Linux more immune to ACPI BIOS bugs.
  25. */
  26. /*
  27. * Design Assumptions
  28. *
  29. * All CPUs have same idle states as boot CPU
  30. *
  31. * Chipset BM_STS (bus master status) bit is a NOP
  32. * for preventing entry into deep C-stats
  33. */
  34. /*
  35. * Known limitations
  36. *
  37. * The driver currently initializes for_each_online_cpu() upon modprobe.
  38. * It it unaware of subsequent processors hot-added to the system.
  39. * This means that if you boot with maxcpus=n and later online
  40. * processors above n, those processors will use C1 only.
  41. *
  42. * ACPI has a .suspend hack to turn off deep c-statees during suspend
  43. * to avoid complications with the lapic timer workaround.
  44. * Have not seen issues with suspend, but may need same workaround here.
  45. *
  46. * There is currently no kernel-based automatic probing/loading mechanism
  47. * if the driver is built as a module.
  48. */
  49. /* un-comment DEBUG to enable pr_debug() statements */
  50. #define DEBUG
  51. #include <linux/kernel.h>
  52. #include <linux/cpuidle.h>
  53. #include <linux/clockchips.h>
  54. #include <linux/hrtimer.h> /* ktime_get_real() */
  55. #include <trace/events/power.h>
  56. #include <linux/sched.h>
  57. #include <linux/notifier.h>
  58. #include <linux/cpu.h>
  59. #include <linux/module.h>
  60. #include <asm/mwait.h>
  61. #include <asm/msr.h>
  62. #define INTEL_IDLE_VERSION "0.4"
  63. #define PREFIX "intel_idle: "
  64. static struct cpuidle_driver intel_idle_driver = {
  65. .name = "intel_idle",
  66. .owner = THIS_MODULE,
  67. };
  68. /* intel_idle.max_cstate=0 disables driver */
  69. static int max_cstate = MWAIT_MAX_NUM_CSTATES - 1;
  70. static unsigned int mwait_substates;
  71. #define LAPIC_TIMER_ALWAYS_RELIABLE 0xFFFFFFFF
  72. /* Reliable LAPIC Timer States, bit 1 for C1 etc. */
  73. static unsigned int lapic_timer_reliable_states = (1 << 1); /* Default to only C1 */
  74. static struct cpuidle_device __percpu *intel_idle_cpuidle_devices;
  75. static int intel_idle(struct cpuidle_device *dev,
  76. struct cpuidle_driver *drv, int index);
  77. static struct cpuidle_state *cpuidle_state_table;
  78. /*
  79. * Hardware C-state auto-demotion may not always be optimal.
  80. * Indicate which enable bits to clear here.
  81. */
  82. static unsigned long long auto_demotion_disable_flags;
  83. /*
  84. * Set this flag for states where the HW flushes the TLB for us
  85. * and so we don't need cross-calls to keep it consistent.
  86. * If this flag is set, SW flushes the TLB, so even if the
  87. * HW doesn't do the flushing, this flag is safe to use.
  88. */
  89. #define CPUIDLE_FLAG_TLB_FLUSHED 0x10000
  90. /*
  91. * States are indexed by the cstate number,
  92. * which is also the index into the MWAIT hint array.
  93. * Thus C0 is a dummy.
  94. */
  95. static struct cpuidle_state nehalem_cstates[MWAIT_MAX_NUM_CSTATES] = {
  96. { /* MWAIT C0 */ },
  97. { /* MWAIT C1 */
  98. .name = "C1-NHM",
  99. .desc = "MWAIT 0x00",
  100. .flags = CPUIDLE_FLAG_TIME_VALID,
  101. .exit_latency = 3,
  102. .target_residency = 6,
  103. .enter = &intel_idle },
  104. { /* MWAIT C2 */
  105. .name = "C3-NHM",
  106. .desc = "MWAIT 0x10",
  107. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  108. .exit_latency = 20,
  109. .target_residency = 80,
  110. .enter = &intel_idle },
  111. { /* MWAIT C3 */
  112. .name = "C6-NHM",
  113. .desc = "MWAIT 0x20",
  114. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  115. .exit_latency = 200,
  116. .target_residency = 800,
  117. .enter = &intel_idle },
  118. };
  119. static struct cpuidle_state snb_cstates[MWAIT_MAX_NUM_CSTATES] = {
  120. { /* MWAIT C0 */ },
  121. { /* MWAIT C1 */
  122. .name = "C1-SNB",
  123. .desc = "MWAIT 0x00",
  124. .flags = CPUIDLE_FLAG_TIME_VALID,
  125. .exit_latency = 1,
  126. .target_residency = 1,
  127. .enter = &intel_idle },
  128. { /* MWAIT C2 */
  129. .name = "C3-SNB",
  130. .desc = "MWAIT 0x10",
  131. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  132. .exit_latency = 80,
  133. .target_residency = 211,
  134. .enter = &intel_idle },
  135. { /* MWAIT C3 */
  136. .name = "C6-SNB",
  137. .desc = "MWAIT 0x20",
  138. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  139. .exit_latency = 104,
  140. .target_residency = 345,
  141. .enter = &intel_idle },
  142. { /* MWAIT C4 */
  143. .name = "C7-SNB",
  144. .desc = "MWAIT 0x30",
  145. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  146. .exit_latency = 109,
  147. .target_residency = 345,
  148. .enter = &intel_idle },
  149. };
  150. static struct cpuidle_state atom_cstates[MWAIT_MAX_NUM_CSTATES] = {
  151. { /* MWAIT C0 */ },
  152. { /* MWAIT C1 */
  153. .name = "C1-ATM",
  154. .desc = "MWAIT 0x00",
  155. .flags = CPUIDLE_FLAG_TIME_VALID,
  156. .exit_latency = 1,
  157. .target_residency = 4,
  158. .enter = &intel_idle },
  159. { /* MWAIT C2 */
  160. .name = "C2-ATM",
  161. .desc = "MWAIT 0x10",
  162. .flags = CPUIDLE_FLAG_TIME_VALID,
  163. .exit_latency = 20,
  164. .target_residency = 80,
  165. .enter = &intel_idle },
  166. { /* MWAIT C3 */ },
  167. { /* MWAIT C4 */
  168. .name = "C4-ATM",
  169. .desc = "MWAIT 0x30",
  170. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  171. .exit_latency = 100,
  172. .target_residency = 400,
  173. .enter = &intel_idle },
  174. { /* MWAIT C5 */ },
  175. { /* MWAIT C6 */
  176. .name = "C6-ATM",
  177. .desc = "MWAIT 0x52",
  178. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  179. .exit_latency = 140,
  180. .target_residency = 560,
  181. .enter = &intel_idle },
  182. };
  183. static int get_driver_data(int cstate)
  184. {
  185. int driver_data;
  186. switch (cstate) {
  187. case 1: /* MWAIT C1 */
  188. driver_data = 0x00;
  189. break;
  190. case 2: /* MWAIT C2 */
  191. driver_data = 0x10;
  192. break;
  193. case 3: /* MWAIT C3 */
  194. driver_data = 0x20;
  195. break;
  196. case 4: /* MWAIT C4 */
  197. driver_data = 0x30;
  198. break;
  199. case 5: /* MWAIT C5 */
  200. driver_data = 0x40;
  201. break;
  202. case 6: /* MWAIT C6 */
  203. driver_data = 0x52;
  204. break;
  205. default:
  206. driver_data = 0x00;
  207. }
  208. return driver_data;
  209. }
  210. /**
  211. * intel_idle
  212. * @dev: cpuidle_device
  213. * @drv: cpuidle driver
  214. * @index: index of cpuidle state
  215. *
  216. */
  217. static int intel_idle(struct cpuidle_device *dev,
  218. struct cpuidle_driver *drv, int index)
  219. {
  220. unsigned long ecx = 1; /* break on interrupt flag */
  221. struct cpuidle_state *state = &drv->states[index];
  222. struct cpuidle_state_usage *state_usage = &dev->states_usage[index];
  223. unsigned long eax = (unsigned long)cpuidle_get_statedata(state_usage);
  224. unsigned int cstate;
  225. ktime_t kt_before, kt_after;
  226. s64 usec_delta;
  227. int cpu = smp_processor_id();
  228. cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1;
  229. local_irq_disable();
  230. /*
  231. * leave_mm() to avoid costly and often unnecessary wakeups
  232. * for flushing the user TLB's associated with the active mm.
  233. */
  234. if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
  235. leave_mm(cpu);
  236. if (!(lapic_timer_reliable_states & (1 << (cstate))))
  237. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
  238. kt_before = ktime_get_real();
  239. stop_critical_timings();
  240. if (!need_resched()) {
  241. __monitor((void *)&current_thread_info()->flags, 0, 0);
  242. smp_mb();
  243. if (!need_resched())
  244. __mwait(eax, ecx);
  245. }
  246. start_critical_timings();
  247. kt_after = ktime_get_real();
  248. usec_delta = ktime_to_us(ktime_sub(kt_after, kt_before));
  249. local_irq_enable();
  250. if (!(lapic_timer_reliable_states & (1 << (cstate))))
  251. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
  252. /* Update cpuidle counters */
  253. dev->last_residency = (int)usec_delta;
  254. return index;
  255. }
  256. static void __setup_broadcast_timer(void *arg)
  257. {
  258. unsigned long reason = (unsigned long)arg;
  259. int cpu = smp_processor_id();
  260. reason = reason ?
  261. CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
  262. clockevents_notify(reason, &cpu);
  263. }
  264. static int setup_broadcast_cpuhp_notify(struct notifier_block *n,
  265. unsigned long action, void *hcpu)
  266. {
  267. int hotcpu = (unsigned long)hcpu;
  268. switch (action & 0xf) {
  269. case CPU_ONLINE:
  270. smp_call_function_single(hotcpu, __setup_broadcast_timer,
  271. (void *)true, 1);
  272. break;
  273. }
  274. return NOTIFY_OK;
  275. }
  276. static struct notifier_block setup_broadcast_notifier = {
  277. .notifier_call = setup_broadcast_cpuhp_notify,
  278. };
  279. static void auto_demotion_disable(void *dummy)
  280. {
  281. unsigned long long msr_bits;
  282. rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
  283. msr_bits &= ~auto_demotion_disable_flags;
  284. wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
  285. }
  286. /*
  287. * intel_idle_probe()
  288. */
  289. static int intel_idle_probe(void)
  290. {
  291. unsigned int eax, ebx, ecx;
  292. if (max_cstate == 0) {
  293. pr_debug(PREFIX "disabled\n");
  294. return -EPERM;
  295. }
  296. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  297. return -ENODEV;
  298. if (!boot_cpu_has(X86_FEATURE_MWAIT))
  299. return -ENODEV;
  300. if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
  301. return -ENODEV;
  302. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
  303. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  304. !(ecx & CPUID5_ECX_INTERRUPT_BREAK))
  305. return -ENODEV;
  306. pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
  307. if (boot_cpu_data.x86 != 6) /* family 6 */
  308. return -ENODEV;
  309. switch (boot_cpu_data.x86_model) {
  310. case 0x1A: /* Core i7, Xeon 5500 series */
  311. case 0x1E: /* Core i7 and i5 Processor - Lynnfield Jasper Forest */
  312. case 0x1F: /* Core i7 and i5 Processor - Nehalem */
  313. case 0x2E: /* Nehalem-EX Xeon */
  314. case 0x2F: /* Westmere-EX Xeon */
  315. case 0x25: /* Westmere */
  316. case 0x2C: /* Westmere */
  317. cpuidle_state_table = nehalem_cstates;
  318. auto_demotion_disable_flags =
  319. (NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE);
  320. break;
  321. case 0x1C: /* 28 - Atom Processor */
  322. cpuidle_state_table = atom_cstates;
  323. break;
  324. case 0x26: /* 38 - Lincroft Atom Processor */
  325. cpuidle_state_table = atom_cstates;
  326. auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE;
  327. break;
  328. case 0x2A: /* SNB */
  329. case 0x2D: /* SNB Xeon */
  330. cpuidle_state_table = snb_cstates;
  331. break;
  332. default:
  333. pr_debug(PREFIX "does not run on family %d model %d\n",
  334. boot_cpu_data.x86, boot_cpu_data.x86_model);
  335. return -ENODEV;
  336. }
  337. if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
  338. lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
  339. else {
  340. smp_call_function(__setup_broadcast_timer, (void *)true, 1);
  341. register_cpu_notifier(&setup_broadcast_notifier);
  342. }
  343. pr_debug(PREFIX "v" INTEL_IDLE_VERSION
  344. " model 0x%X\n", boot_cpu_data.x86_model);
  345. pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n",
  346. lapic_timer_reliable_states);
  347. return 0;
  348. }
  349. /*
  350. * intel_idle_cpuidle_devices_uninit()
  351. * unregister, free cpuidle_devices
  352. */
  353. static void intel_idle_cpuidle_devices_uninit(void)
  354. {
  355. int i;
  356. struct cpuidle_device *dev;
  357. for_each_online_cpu(i) {
  358. dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
  359. cpuidle_unregister_device(dev);
  360. }
  361. free_percpu(intel_idle_cpuidle_devices);
  362. return;
  363. }
  364. /*
  365. * intel_idle_cpuidle_driver_init()
  366. * allocate, initialize cpuidle_states
  367. */
  368. static int intel_idle_cpuidle_driver_init(void)
  369. {
  370. int cstate;
  371. struct cpuidle_driver *drv = &intel_idle_driver;
  372. drv->state_count = 1;
  373. for (cstate = 1; cstate < MWAIT_MAX_NUM_CSTATES; ++cstate) {
  374. int num_substates;
  375. if (cstate > max_cstate) {
  376. printk(PREFIX "max_cstate %d reached\n",
  377. max_cstate);
  378. break;
  379. }
  380. /* does the state exist in CPUID.MWAIT? */
  381. num_substates = (mwait_substates >> ((cstate) * 4))
  382. & MWAIT_SUBSTATE_MASK;
  383. if (num_substates == 0)
  384. continue;
  385. /* is the state not enabled? */
  386. if (cpuidle_state_table[cstate].enter == NULL) {
  387. /* does the driver not know about the state? */
  388. if (*cpuidle_state_table[cstate].name == '\0')
  389. pr_debug(PREFIX "unaware of model 0x%x"
  390. " MWAIT %d please"
  391. " contact lenb@kernel.org",
  392. boot_cpu_data.x86_model, cstate);
  393. continue;
  394. }
  395. if ((cstate > 2) &&
  396. !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  397. mark_tsc_unstable("TSC halts in idle"
  398. " states deeper than C2");
  399. drv->states[drv->state_count] = /* structure copy */
  400. cpuidle_state_table[cstate];
  401. drv->state_count += 1;
  402. }
  403. if (auto_demotion_disable_flags)
  404. smp_call_function(auto_demotion_disable, NULL, 1);
  405. return 0;
  406. }
  407. /*
  408. * intel_idle_cpuidle_devices_init()
  409. * allocate, initialize, register cpuidle_devices
  410. */
  411. static int intel_idle_cpuidle_devices_init(void)
  412. {
  413. int i, cstate;
  414. struct cpuidle_device *dev;
  415. intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  416. if (intel_idle_cpuidle_devices == NULL)
  417. return -ENOMEM;
  418. for_each_online_cpu(i) {
  419. dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
  420. dev->state_count = 1;
  421. for (cstate = 1; cstate < MWAIT_MAX_NUM_CSTATES; ++cstate) {
  422. int num_substates;
  423. if (cstate > max_cstate) {
  424. printk(PREFIX "max_cstate %d reached\n",
  425. max_cstate);
  426. break;
  427. }
  428. /* does the state exist in CPUID.MWAIT? */
  429. num_substates = (mwait_substates >> ((cstate) * 4))
  430. & MWAIT_SUBSTATE_MASK;
  431. if (num_substates == 0)
  432. continue;
  433. /* is the state not enabled? */
  434. if (cpuidle_state_table[cstate].enter == NULL) {
  435. continue;
  436. }
  437. dev->states_usage[dev->state_count].driver_data =
  438. (void *)get_driver_data(cstate);
  439. dev->state_count += 1;
  440. }
  441. dev->cpu = i;
  442. if (cpuidle_register_device(dev)) {
  443. pr_debug(PREFIX "cpuidle_register_device %d failed!\n",
  444. i);
  445. intel_idle_cpuidle_devices_uninit();
  446. return -EIO;
  447. }
  448. }
  449. return 0;
  450. }
  451. static int __init intel_idle_init(void)
  452. {
  453. int retval;
  454. /* Do not load intel_idle at all for now if idle= is passed */
  455. if (boot_option_idle_override != IDLE_NO_OVERRIDE)
  456. return -ENODEV;
  457. retval = intel_idle_probe();
  458. if (retval)
  459. return retval;
  460. intel_idle_cpuidle_driver_init();
  461. retval = cpuidle_register_driver(&intel_idle_driver);
  462. if (retval) {
  463. printk(KERN_DEBUG PREFIX "intel_idle yielding to %s",
  464. cpuidle_get_driver()->name);
  465. return retval;
  466. }
  467. retval = intel_idle_cpuidle_devices_init();
  468. if (retval) {
  469. cpuidle_unregister_driver(&intel_idle_driver);
  470. return retval;
  471. }
  472. return 0;
  473. }
  474. static void __exit intel_idle_exit(void)
  475. {
  476. intel_idle_cpuidle_devices_uninit();
  477. cpuidle_unregister_driver(&intel_idle_driver);
  478. if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE) {
  479. smp_call_function(__setup_broadcast_timer, (void *)false, 1);
  480. unregister_cpu_notifier(&setup_broadcast_notifier);
  481. }
  482. return;
  483. }
  484. module_init(intel_idle_init);
  485. module_exit(intel_idle_exit);
  486. module_param(max_cstate, int, 0444);
  487. MODULE_AUTHOR("Len Brown <len.brown@intel.com>");
  488. MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION);
  489. MODULE_LICENSE("GPL");