intel_idle.c 11 KB

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