intel_idle.c 11 KB

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