intel_idle.c 14 KB

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