intel_idle.c 13 KB

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