intel_idle.c 13 KB

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