intel_idle.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 <asm/mwait.h>
  60. #include <asm/msr.h>
  61. #define INTEL_IDLE_VERSION "0.4"
  62. #define PREFIX "intel_idle: "
  63. static struct cpuidle_driver intel_idle_driver = {
  64. .name = "intel_idle",
  65. .owner = THIS_MODULE,
  66. };
  67. /* intel_idle.max_cstate=0 disables driver */
  68. static int max_cstate = MWAIT_MAX_NUM_CSTATES - 1;
  69. static unsigned int mwait_substates;
  70. #define LAPIC_TIMER_ALWAYS_RELIABLE 0xFFFFFFFF
  71. /* Reliable LAPIC Timer States, bit 1 for C1 etc. */
  72. static unsigned int lapic_timer_reliable_states = (1 << 1); /* Default to only C1 */
  73. static struct cpuidle_device __percpu *intel_idle_cpuidle_devices;
  74. static int intel_idle(struct cpuidle_device *dev, struct cpuidle_state *state);
  75. static struct cpuidle_state *cpuidle_state_table;
  76. /*
  77. * Hardware C-state auto-demotion may not always be optimal.
  78. * Indicate which enable bits to clear here.
  79. */
  80. static unsigned long long auto_demotion_disable_flags;
  81. /*
  82. * Set this flag for states where the HW flushes the TLB for us
  83. * and so we don't need cross-calls to keep it consistent.
  84. * If this flag is set, SW flushes the TLB, so even if the
  85. * HW doesn't do the flushing, this flag is safe to use.
  86. */
  87. #define CPUIDLE_FLAG_TLB_FLUSHED 0x10000
  88. /*
  89. * States are indexed by the cstate number,
  90. * which is also the index into the MWAIT hint array.
  91. * Thus C0 is a dummy.
  92. */
  93. static struct cpuidle_state nehalem_cstates[MWAIT_MAX_NUM_CSTATES] = {
  94. { /* MWAIT C0 */ },
  95. { /* MWAIT C1 */
  96. .name = "C1-NHM",
  97. .desc = "MWAIT 0x00",
  98. .driver_data = (void *) 0x00,
  99. .flags = CPUIDLE_FLAG_TIME_VALID,
  100. .exit_latency = 3,
  101. .target_residency = 6,
  102. .enter = &intel_idle },
  103. { /* MWAIT C2 */
  104. .name = "C3-NHM",
  105. .desc = "MWAIT 0x10",
  106. .driver_data = (void *) 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. .driver_data = (void *) 0x20,
  115. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  116. .exit_latency = 200,
  117. .target_residency = 800,
  118. .enter = &intel_idle },
  119. };
  120. static struct cpuidle_state snb_cstates[MWAIT_MAX_NUM_CSTATES] = {
  121. { /* MWAIT C0 */ },
  122. { /* MWAIT C1 */
  123. .name = "C1-SNB",
  124. .desc = "MWAIT 0x00",
  125. .driver_data = (void *) 0x00,
  126. .flags = CPUIDLE_FLAG_TIME_VALID,
  127. .exit_latency = 1,
  128. .target_residency = 1,
  129. .enter = &intel_idle },
  130. { /* MWAIT C2 */
  131. .name = "C3-SNB",
  132. .desc = "MWAIT 0x10",
  133. .driver_data = (void *) 0x10,
  134. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  135. .exit_latency = 80,
  136. .target_residency = 211,
  137. .enter = &intel_idle },
  138. { /* MWAIT C3 */
  139. .name = "C6-SNB",
  140. .desc = "MWAIT 0x20",
  141. .driver_data = (void *) 0x20,
  142. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  143. .exit_latency = 104,
  144. .target_residency = 345,
  145. .enter = &intel_idle },
  146. { /* MWAIT C4 */
  147. .name = "C7-SNB",
  148. .desc = "MWAIT 0x30",
  149. .driver_data = (void *) 0x30,
  150. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  151. .exit_latency = 109,
  152. .target_residency = 345,
  153. .enter = &intel_idle },
  154. };
  155. static struct cpuidle_state atom_cstates[MWAIT_MAX_NUM_CSTATES] = {
  156. { /* MWAIT C0 */ },
  157. { /* MWAIT C1 */
  158. .name = "C1-ATM",
  159. .desc = "MWAIT 0x00",
  160. .driver_data = (void *) 0x00,
  161. .flags = CPUIDLE_FLAG_TIME_VALID,
  162. .exit_latency = 1,
  163. .target_residency = 4,
  164. .enter = &intel_idle },
  165. { /* MWAIT C2 */
  166. .name = "C2-ATM",
  167. .desc = "MWAIT 0x10",
  168. .driver_data = (void *) 0x10,
  169. .flags = CPUIDLE_FLAG_TIME_VALID,
  170. .exit_latency = 20,
  171. .target_residency = 80,
  172. .enter = &intel_idle },
  173. { /* MWAIT C3 */ },
  174. { /* MWAIT C4 */
  175. .name = "C4-ATM",
  176. .desc = "MWAIT 0x30",
  177. .driver_data = (void *) 0x30,
  178. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  179. .exit_latency = 100,
  180. .target_residency = 400,
  181. .enter = &intel_idle },
  182. { /* MWAIT C5 */ },
  183. { /* MWAIT C6 */
  184. .name = "C6-ATM",
  185. .desc = "MWAIT 0x52",
  186. .driver_data = (void *) 0x52,
  187. .flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  188. .exit_latency = 140,
  189. .target_residency = 560,
  190. .enter = &intel_idle },
  191. };
  192. /**
  193. * intel_idle
  194. * @dev: cpuidle_device
  195. * @state: cpuidle state
  196. *
  197. */
  198. static int intel_idle(struct cpuidle_device *dev, struct cpuidle_state *state)
  199. {
  200. unsigned long ecx = 1; /* break on interrupt flag */
  201. unsigned long eax = (unsigned long)cpuidle_get_statedata(state);
  202. unsigned int cstate;
  203. ktime_t kt_before, kt_after;
  204. s64 usec_delta;
  205. int cpu = smp_processor_id();
  206. cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1;
  207. local_irq_disable();
  208. /*
  209. * leave_mm() to avoid costly and often unnecessary wakeups
  210. * for flushing the user TLB's associated with the active mm.
  211. */
  212. if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
  213. leave_mm(cpu);
  214. if (!(lapic_timer_reliable_states & (1 << (cstate))))
  215. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
  216. kt_before = ktime_get_real();
  217. stop_critical_timings();
  218. if (!need_resched()) {
  219. __monitor((void *)&current_thread_info()->flags, 0, 0);
  220. smp_mb();
  221. if (!need_resched())
  222. __mwait(eax, ecx);
  223. }
  224. start_critical_timings();
  225. kt_after = ktime_get_real();
  226. usec_delta = ktime_to_us(ktime_sub(kt_after, kt_before));
  227. local_irq_enable();
  228. if (!(lapic_timer_reliable_states & (1 << (cstate))))
  229. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
  230. return usec_delta;
  231. }
  232. static void __setup_broadcast_timer(void *arg)
  233. {
  234. unsigned long reason = (unsigned long)arg;
  235. int cpu = smp_processor_id();
  236. reason = reason ?
  237. CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
  238. clockevents_notify(reason, &cpu);
  239. }
  240. static int setup_broadcast_cpuhp_notify(struct notifier_block *n,
  241. unsigned long action, void *hcpu)
  242. {
  243. int hotcpu = (unsigned long)hcpu;
  244. switch (action & 0xf) {
  245. case CPU_ONLINE:
  246. smp_call_function_single(hotcpu, __setup_broadcast_timer,
  247. (void *)true, 1);
  248. break;
  249. }
  250. return NOTIFY_OK;
  251. }
  252. static struct notifier_block setup_broadcast_notifier = {
  253. .notifier_call = setup_broadcast_cpuhp_notify,
  254. };
  255. static void auto_demotion_disable(void *dummy)
  256. {
  257. unsigned long long msr_bits;
  258. rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
  259. msr_bits &= ~auto_demotion_disable_flags;
  260. wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
  261. }
  262. /*
  263. * intel_idle_probe()
  264. */
  265. static int intel_idle_probe(void)
  266. {
  267. unsigned int eax, ebx, ecx;
  268. if (max_cstate == 0) {
  269. pr_debug(PREFIX "disabled\n");
  270. return -EPERM;
  271. }
  272. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  273. return -ENODEV;
  274. if (!boot_cpu_has(X86_FEATURE_MWAIT))
  275. return -ENODEV;
  276. if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
  277. return -ENODEV;
  278. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
  279. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  280. !(ecx & CPUID5_ECX_INTERRUPT_BREAK))
  281. return -ENODEV;
  282. pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
  283. if (boot_cpu_data.x86 != 6) /* family 6 */
  284. return -ENODEV;
  285. switch (boot_cpu_data.x86_model) {
  286. case 0x1A: /* Core i7, Xeon 5500 series */
  287. case 0x1E: /* Core i7 and i5 Processor - Lynnfield Jasper Forest */
  288. case 0x1F: /* Core i7 and i5 Processor - Nehalem */
  289. case 0x2E: /* Nehalem-EX Xeon */
  290. case 0x2F: /* Westmere-EX Xeon */
  291. case 0x25: /* Westmere */
  292. case 0x2C: /* Westmere */
  293. cpuidle_state_table = nehalem_cstates;
  294. auto_demotion_disable_flags =
  295. (NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE);
  296. break;
  297. case 0x1C: /* 28 - Atom Processor */
  298. cpuidle_state_table = atom_cstates;
  299. break;
  300. case 0x26: /* 38 - Lincroft Atom Processor */
  301. cpuidle_state_table = atom_cstates;
  302. auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE;
  303. break;
  304. case 0x2A: /* SNB */
  305. case 0x2D: /* SNB Xeon */
  306. cpuidle_state_table = snb_cstates;
  307. break;
  308. default:
  309. pr_debug(PREFIX "does not run on family %d model %d\n",
  310. boot_cpu_data.x86, boot_cpu_data.x86_model);
  311. return -ENODEV;
  312. }
  313. if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
  314. lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
  315. else {
  316. smp_call_function(__setup_broadcast_timer, (void *)true, 1);
  317. register_cpu_notifier(&setup_broadcast_notifier);
  318. }
  319. pr_debug(PREFIX "v" INTEL_IDLE_VERSION
  320. " model 0x%X\n", boot_cpu_data.x86_model);
  321. pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n",
  322. lapic_timer_reliable_states);
  323. return 0;
  324. }
  325. /*
  326. * intel_idle_cpuidle_devices_uninit()
  327. * unregister, free cpuidle_devices
  328. */
  329. static void intel_idle_cpuidle_devices_uninit(void)
  330. {
  331. int i;
  332. struct cpuidle_device *dev;
  333. for_each_online_cpu(i) {
  334. dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
  335. cpuidle_unregister_device(dev);
  336. }
  337. free_percpu(intel_idle_cpuidle_devices);
  338. return;
  339. }
  340. /*
  341. * intel_idle_cpuidle_devices_init()
  342. * allocate, initialize, register cpuidle_devices
  343. */
  344. static int intel_idle_cpuidle_devices_init(void)
  345. {
  346. int i, cstate;
  347. struct cpuidle_device *dev;
  348. intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  349. if (intel_idle_cpuidle_devices == NULL)
  350. return -ENOMEM;
  351. for_each_online_cpu(i) {
  352. dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
  353. dev->state_count = 1;
  354. for (cstate = 1; cstate < MWAIT_MAX_NUM_CSTATES; ++cstate) {
  355. int num_substates;
  356. if (cstate > max_cstate) {
  357. printk(PREFIX "max_cstate %d reached\n",
  358. max_cstate);
  359. break;
  360. }
  361. /* does the state exist in CPUID.MWAIT? */
  362. num_substates = (mwait_substates >> ((cstate) * 4))
  363. & MWAIT_SUBSTATE_MASK;
  364. if (num_substates == 0)
  365. continue;
  366. /* is the state not enabled? */
  367. if (cpuidle_state_table[cstate].enter == NULL) {
  368. /* does the driver not know about the state? */
  369. if (*cpuidle_state_table[cstate].name == '\0')
  370. pr_debug(PREFIX "unaware of model 0x%x"
  371. " MWAIT %d please"
  372. " contact lenb@kernel.org",
  373. boot_cpu_data.x86_model, cstate);
  374. continue;
  375. }
  376. if ((cstate > 2) &&
  377. !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  378. mark_tsc_unstable("TSC halts in idle"
  379. " states deeper than C2");
  380. dev->states[dev->state_count] = /* structure copy */
  381. cpuidle_state_table[cstate];
  382. dev->state_count += 1;
  383. }
  384. dev->cpu = i;
  385. if (cpuidle_register_device(dev)) {
  386. pr_debug(PREFIX "cpuidle_register_device %d failed!\n",
  387. i);
  388. intel_idle_cpuidle_devices_uninit();
  389. return -EIO;
  390. }
  391. }
  392. if (auto_demotion_disable_flags)
  393. smp_call_function(auto_demotion_disable, NULL, 1);
  394. return 0;
  395. }
  396. static int __init intel_idle_init(void)
  397. {
  398. int retval;
  399. /* Do not load intel_idle at all for now if idle= is passed */
  400. if (boot_option_idle_override != IDLE_NO_OVERRIDE)
  401. return -ENODEV;
  402. retval = intel_idle_probe();
  403. if (retval)
  404. return retval;
  405. retval = cpuidle_register_driver(&intel_idle_driver);
  406. if (retval) {
  407. printk(KERN_DEBUG PREFIX "intel_idle yielding to %s",
  408. cpuidle_get_driver()->name);
  409. return retval;
  410. }
  411. retval = intel_idle_cpuidle_devices_init();
  412. if (retval) {
  413. cpuidle_unregister_driver(&intel_idle_driver);
  414. return retval;
  415. }
  416. return 0;
  417. }
  418. static void __exit intel_idle_exit(void)
  419. {
  420. intel_idle_cpuidle_devices_uninit();
  421. cpuidle_unregister_driver(&intel_idle_driver);
  422. if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE) {
  423. smp_call_function(__setup_broadcast_timer, (void *)false, 1);
  424. unregister_cpu_notifier(&setup_broadcast_notifier);
  425. }
  426. return;
  427. }
  428. module_init(intel_idle_init);
  429. module_exit(intel_idle_exit);
  430. module_param(max_cstate, int, 0444);
  431. MODULE_AUTHOR("Len Brown <len.brown@intel.com>");
  432. MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION);
  433. MODULE_LICENSE("GPL");