intel_idle.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 <trace/events/power.h>
  55. #include <linux/sched.h>
  56. #include <linux/notifier.h>
  57. #include <linux/cpu.h>
  58. #include <linux/module.h>
  59. #include <asm/cpu_device_id.h>
  60. #include <asm/mwait.h>
  61. #include <asm/msr.h>
  62. #define INTEL_IDLE_VERSION "0.4"
  63. #define PREFIX "intel_idle: "
  64. static struct cpuidle_driver intel_idle_driver = {
  65. .name = "intel_idle",
  66. .owner = THIS_MODULE,
  67. };
  68. /* intel_idle.max_cstate=0 disables driver */
  69. static int max_cstate = CPUIDLE_STATE_MAX - 1;
  70. static unsigned int mwait_substates;
  71. #define LAPIC_TIMER_ALWAYS_RELIABLE 0xFFFFFFFF
  72. /* Reliable LAPIC Timer States, bit 1 for C1 etc. */
  73. static unsigned int lapic_timer_reliable_states = (1 << 1); /* Default to only C1 */
  74. struct idle_cpu {
  75. struct cpuidle_state *state_table;
  76. /*
  77. * Hardware C-state auto-demotion may not always be optimal.
  78. * Indicate which enable bits to clear here.
  79. */
  80. unsigned long auto_demotion_disable_flags;
  81. bool disable_promotion_to_c1e;
  82. };
  83. static const struct idle_cpu *icpu;
  84. static struct cpuidle_device __percpu *intel_idle_cpuidle_devices;
  85. static int intel_idle(struct cpuidle_device *dev,
  86. struct cpuidle_driver *drv, int index);
  87. static int intel_idle_cpu_init(int cpu);
  88. static struct cpuidle_state *cpuidle_state_table;
  89. /*
  90. * Set this flag for states where the HW flushes the TLB for us
  91. * and so we don't need cross-calls to keep it consistent.
  92. * If this flag is set, SW flushes the TLB, so even if the
  93. * HW doesn't do the flushing, this flag is safe to use.
  94. */
  95. #define CPUIDLE_FLAG_TLB_FLUSHED 0x10000
  96. /*
  97. * MWAIT takes an 8-bit "hint" in EAX "suggesting"
  98. * the C-state (top nibble) and sub-state (bottom nibble)
  99. * 0x00 means "MWAIT(C1)", 0x10 means "MWAIT(C2)" etc.
  100. *
  101. * We store the hint at the top of our "flags" for each state.
  102. */
  103. #define flg2MWAIT(flags) (((flags) >> 24) & 0xFF)
  104. #define MWAIT2flg(eax) ((eax & 0xFF) << 24)
  105. /*
  106. * States are indexed by the cstate number,
  107. * which is also the index into the MWAIT hint array.
  108. * Thus C0 is a dummy.
  109. */
  110. static struct cpuidle_state nehalem_cstates[CPUIDLE_STATE_MAX] = {
  111. {
  112. .name = "C1-NHM",
  113. .desc = "MWAIT 0x00",
  114. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  115. .exit_latency = 3,
  116. .target_residency = 6,
  117. .enter = &intel_idle },
  118. {
  119. .name = "C1E-NHM",
  120. .desc = "MWAIT 0x01",
  121. .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID,
  122. .exit_latency = 10,
  123. .target_residency = 20,
  124. .enter = &intel_idle },
  125. {
  126. .name = "C3-NHM",
  127. .desc = "MWAIT 0x10",
  128. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  129. .exit_latency = 20,
  130. .target_residency = 80,
  131. .enter = &intel_idle },
  132. {
  133. .name = "C6-NHM",
  134. .desc = "MWAIT 0x20",
  135. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  136. .exit_latency = 200,
  137. .target_residency = 800,
  138. .enter = &intel_idle },
  139. {
  140. .enter = NULL }
  141. };
  142. static struct cpuidle_state snb_cstates[CPUIDLE_STATE_MAX] = {
  143. {
  144. .name = "C1-SNB",
  145. .desc = "MWAIT 0x00",
  146. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  147. .exit_latency = 2,
  148. .target_residency = 2,
  149. .enter = &intel_idle },
  150. {
  151. .name = "C1E-SNB",
  152. .desc = "MWAIT 0x01",
  153. .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID,
  154. .exit_latency = 10,
  155. .target_residency = 20,
  156. .enter = &intel_idle },
  157. {
  158. .name = "C3-SNB",
  159. .desc = "MWAIT 0x10",
  160. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  161. .exit_latency = 80,
  162. .target_residency = 211,
  163. .enter = &intel_idle },
  164. {
  165. .name = "C6-SNB",
  166. .desc = "MWAIT 0x20",
  167. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  168. .exit_latency = 104,
  169. .target_residency = 345,
  170. .enter = &intel_idle },
  171. {
  172. .name = "C7-SNB",
  173. .desc = "MWAIT 0x30",
  174. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  175. .exit_latency = 109,
  176. .target_residency = 345,
  177. .enter = &intel_idle },
  178. {
  179. .enter = NULL }
  180. };
  181. static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] = {
  182. {
  183. .name = "C1-IVB",
  184. .desc = "MWAIT 0x00",
  185. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  186. .exit_latency = 1,
  187. .target_residency = 1,
  188. .enter = &intel_idle },
  189. {
  190. .name = "C1E-IVB",
  191. .desc = "MWAIT 0x01",
  192. .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID,
  193. .exit_latency = 10,
  194. .target_residency = 20,
  195. .enter = &intel_idle },
  196. {
  197. .name = "C3-IVB",
  198. .desc = "MWAIT 0x10",
  199. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  200. .exit_latency = 59,
  201. .target_residency = 156,
  202. .enter = &intel_idle },
  203. {
  204. .name = "C6-IVB",
  205. .desc = "MWAIT 0x20",
  206. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  207. .exit_latency = 80,
  208. .target_residency = 300,
  209. .enter = &intel_idle },
  210. {
  211. .name = "C7-IVB",
  212. .desc = "MWAIT 0x30",
  213. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  214. .exit_latency = 87,
  215. .target_residency = 300,
  216. .enter = &intel_idle },
  217. {
  218. .enter = NULL }
  219. };
  220. static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] = {
  221. {
  222. .name = "C1-HSW",
  223. .desc = "MWAIT 0x00",
  224. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  225. .exit_latency = 2,
  226. .target_residency = 2,
  227. .enter = &intel_idle },
  228. {
  229. .name = "C1E-HSW",
  230. .desc = "MWAIT 0x01",
  231. .flags = MWAIT2flg(0x01) | CPUIDLE_FLAG_TIME_VALID,
  232. .exit_latency = 10,
  233. .target_residency = 20,
  234. .enter = &intel_idle },
  235. {
  236. .name = "C3-HSW",
  237. .desc = "MWAIT 0x10",
  238. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  239. .exit_latency = 33,
  240. .target_residency = 100,
  241. .enter = &intel_idle },
  242. {
  243. .name = "C6-HSW",
  244. .desc = "MWAIT 0x20",
  245. .flags = MWAIT2flg(0x20) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  246. .exit_latency = 133,
  247. .target_residency = 400,
  248. .enter = &intel_idle },
  249. {
  250. .name = "C7s-HSW",
  251. .desc = "MWAIT 0x32",
  252. .flags = MWAIT2flg(0x32) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  253. .exit_latency = 166,
  254. .target_residency = 500,
  255. .enter = &intel_idle },
  256. {
  257. .enter = NULL }
  258. };
  259. static struct cpuidle_state atom_cstates[CPUIDLE_STATE_MAX] = {
  260. {
  261. .name = "C1E-ATM",
  262. .desc = "MWAIT 0x00",
  263. .flags = MWAIT2flg(0x00) | CPUIDLE_FLAG_TIME_VALID,
  264. .exit_latency = 10,
  265. .target_residency = 20,
  266. .enter = &intel_idle },
  267. {
  268. .name = "C2-ATM",
  269. .desc = "MWAIT 0x10",
  270. .flags = MWAIT2flg(0x10) | CPUIDLE_FLAG_TIME_VALID,
  271. .exit_latency = 20,
  272. .target_residency = 80,
  273. .enter = &intel_idle },
  274. {
  275. .name = "C4-ATM",
  276. .desc = "MWAIT 0x30",
  277. .flags = MWAIT2flg(0x30) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  278. .exit_latency = 100,
  279. .target_residency = 400,
  280. .enter = &intel_idle },
  281. {
  282. .name = "C6-ATM",
  283. .desc = "MWAIT 0x52",
  284. .flags = MWAIT2flg(0x52) | CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TLB_FLUSHED,
  285. .exit_latency = 140,
  286. .target_residency = 560,
  287. .enter = &intel_idle },
  288. {
  289. .enter = NULL }
  290. };
  291. /**
  292. * intel_idle
  293. * @dev: cpuidle_device
  294. * @drv: cpuidle driver
  295. * @index: index of cpuidle state
  296. *
  297. * Must be called under local_irq_disable().
  298. */
  299. static int intel_idle(struct cpuidle_device *dev,
  300. struct cpuidle_driver *drv, int index)
  301. {
  302. unsigned long ecx = 1; /* break on interrupt flag */
  303. struct cpuidle_state *state = &drv->states[index];
  304. unsigned long eax = flg2MWAIT(state->flags);
  305. unsigned int cstate;
  306. int cpu = smp_processor_id();
  307. cstate = (((eax) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) + 1;
  308. /*
  309. * leave_mm() to avoid costly and often unnecessary wakeups
  310. * for flushing the user TLB's associated with the active mm.
  311. */
  312. if (state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
  313. leave_mm(cpu);
  314. if (!(lapic_timer_reliable_states & (1 << (cstate))))
  315. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
  316. if (!need_resched()) {
  317. __monitor((void *)&current_thread_info()->flags, 0, 0);
  318. smp_mb();
  319. if (!need_resched())
  320. __mwait(eax, ecx);
  321. }
  322. if (!(lapic_timer_reliable_states & (1 << (cstate))))
  323. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
  324. return index;
  325. }
  326. static void __setup_broadcast_timer(void *arg)
  327. {
  328. unsigned long reason = (unsigned long)arg;
  329. int cpu = smp_processor_id();
  330. reason = reason ?
  331. CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
  332. clockevents_notify(reason, &cpu);
  333. }
  334. static int cpu_hotplug_notify(struct notifier_block *n,
  335. unsigned long action, void *hcpu)
  336. {
  337. int hotcpu = (unsigned long)hcpu;
  338. struct cpuidle_device *dev;
  339. switch (action & 0xf) {
  340. case CPU_ONLINE:
  341. if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
  342. smp_call_function_single(hotcpu, __setup_broadcast_timer,
  343. (void *)true, 1);
  344. /*
  345. * Some systems can hotplug a cpu at runtime after
  346. * the kernel has booted, we have to initialize the
  347. * driver in this case
  348. */
  349. dev = per_cpu_ptr(intel_idle_cpuidle_devices, hotcpu);
  350. if (!dev->registered)
  351. intel_idle_cpu_init(hotcpu);
  352. break;
  353. }
  354. return NOTIFY_OK;
  355. }
  356. static struct notifier_block cpu_hotplug_notifier = {
  357. .notifier_call = cpu_hotplug_notify,
  358. };
  359. static void auto_demotion_disable(void *dummy)
  360. {
  361. unsigned long long msr_bits;
  362. rdmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
  363. msr_bits &= ~(icpu->auto_demotion_disable_flags);
  364. wrmsrl(MSR_NHM_SNB_PKG_CST_CFG_CTL, msr_bits);
  365. }
  366. static void c1e_promotion_disable(void *dummy)
  367. {
  368. unsigned long long msr_bits;
  369. rdmsrl(MSR_IA32_POWER_CTL, msr_bits);
  370. msr_bits &= ~0x2;
  371. wrmsrl(MSR_IA32_POWER_CTL, msr_bits);
  372. }
  373. static const struct idle_cpu idle_cpu_nehalem = {
  374. .state_table = nehalem_cstates,
  375. .auto_demotion_disable_flags = NHM_C1_AUTO_DEMOTE | NHM_C3_AUTO_DEMOTE,
  376. .disable_promotion_to_c1e = true,
  377. };
  378. static const struct idle_cpu idle_cpu_atom = {
  379. .state_table = atom_cstates,
  380. };
  381. static const struct idle_cpu idle_cpu_lincroft = {
  382. .state_table = atom_cstates,
  383. .auto_demotion_disable_flags = ATM_LNC_C6_AUTO_DEMOTE,
  384. };
  385. static const struct idle_cpu idle_cpu_snb = {
  386. .state_table = snb_cstates,
  387. .disable_promotion_to_c1e = true,
  388. };
  389. static const struct idle_cpu idle_cpu_ivb = {
  390. .state_table = ivb_cstates,
  391. .disable_promotion_to_c1e = true,
  392. };
  393. static const struct idle_cpu idle_cpu_hsw = {
  394. .state_table = hsw_cstates,
  395. .disable_promotion_to_c1e = true,
  396. };
  397. #define ICPU(model, cpu) \
  398. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_MWAIT, (unsigned long)&cpu }
  399. static const struct x86_cpu_id intel_idle_ids[] = {
  400. ICPU(0x1a, idle_cpu_nehalem),
  401. ICPU(0x1e, idle_cpu_nehalem),
  402. ICPU(0x1f, idle_cpu_nehalem),
  403. ICPU(0x25, idle_cpu_nehalem),
  404. ICPU(0x2c, idle_cpu_nehalem),
  405. ICPU(0x2e, idle_cpu_nehalem),
  406. ICPU(0x1c, idle_cpu_atom),
  407. ICPU(0x26, idle_cpu_lincroft),
  408. ICPU(0x2f, idle_cpu_nehalem),
  409. ICPU(0x2a, idle_cpu_snb),
  410. ICPU(0x2d, idle_cpu_snb),
  411. ICPU(0x3a, idle_cpu_ivb),
  412. ICPU(0x3e, idle_cpu_ivb),
  413. ICPU(0x3c, idle_cpu_hsw),
  414. ICPU(0x3f, idle_cpu_hsw),
  415. ICPU(0x45, idle_cpu_hsw),
  416. {}
  417. };
  418. MODULE_DEVICE_TABLE(x86cpu, intel_idle_ids);
  419. /*
  420. * intel_idle_probe()
  421. */
  422. static int intel_idle_probe(void)
  423. {
  424. unsigned int eax, ebx, ecx;
  425. const struct x86_cpu_id *id;
  426. if (max_cstate == 0) {
  427. pr_debug(PREFIX "disabled\n");
  428. return -EPERM;
  429. }
  430. id = x86_match_cpu(intel_idle_ids);
  431. if (!id) {
  432. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  433. boot_cpu_data.x86 == 6)
  434. pr_debug(PREFIX "does not run on family %d model %d\n",
  435. boot_cpu_data.x86, boot_cpu_data.x86_model);
  436. return -ENODEV;
  437. }
  438. if (boot_cpu_data.cpuid_level < CPUID_MWAIT_LEAF)
  439. return -ENODEV;
  440. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &mwait_substates);
  441. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
  442. !(ecx & CPUID5_ECX_INTERRUPT_BREAK) ||
  443. !mwait_substates)
  444. return -ENODEV;
  445. pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
  446. icpu = (const struct idle_cpu *)id->driver_data;
  447. cpuidle_state_table = icpu->state_table;
  448. if (boot_cpu_has(X86_FEATURE_ARAT)) /* Always Reliable APIC Timer */
  449. lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
  450. else
  451. on_each_cpu(__setup_broadcast_timer, (void *)true, 1);
  452. pr_debug(PREFIX "v" INTEL_IDLE_VERSION
  453. " model 0x%X\n", boot_cpu_data.x86_model);
  454. pr_debug(PREFIX "lapic_timer_reliable_states 0x%x\n",
  455. lapic_timer_reliable_states);
  456. return 0;
  457. }
  458. /*
  459. * intel_idle_cpuidle_devices_uninit()
  460. * unregister, free cpuidle_devices
  461. */
  462. static void intel_idle_cpuidle_devices_uninit(void)
  463. {
  464. int i;
  465. struct cpuidle_device *dev;
  466. for_each_online_cpu(i) {
  467. dev = per_cpu_ptr(intel_idle_cpuidle_devices, i);
  468. cpuidle_unregister_device(dev);
  469. }
  470. free_percpu(intel_idle_cpuidle_devices);
  471. return;
  472. }
  473. /*
  474. * intel_idle_cpuidle_driver_init()
  475. * allocate, initialize cpuidle_states
  476. */
  477. static int intel_idle_cpuidle_driver_init(void)
  478. {
  479. int cstate;
  480. struct cpuidle_driver *drv = &intel_idle_driver;
  481. drv->state_count = 1;
  482. for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
  483. int num_substates, mwait_hint, mwait_cstate, mwait_substate;
  484. if (cpuidle_state_table[cstate].enter == NULL)
  485. break;
  486. if (cstate + 1 > max_cstate) {
  487. printk(PREFIX "max_cstate %d reached\n",
  488. max_cstate);
  489. break;
  490. }
  491. mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
  492. mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
  493. mwait_substate = MWAIT_HINT2SUBSTATE(mwait_hint);
  494. /* does the state exist in CPUID.MWAIT? */
  495. num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
  496. & MWAIT_SUBSTATE_MASK;
  497. /* if sub-state in table is not enumerated by CPUID */
  498. if ((mwait_substate + 1) > num_substates)
  499. continue;
  500. if (((mwait_cstate + 1) > 2) &&
  501. !boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  502. mark_tsc_unstable("TSC halts in idle"
  503. " states deeper than C2");
  504. drv->states[drv->state_count] = /* structure copy */
  505. cpuidle_state_table[cstate];
  506. drv->state_count += 1;
  507. }
  508. if (icpu->auto_demotion_disable_flags)
  509. on_each_cpu(auto_demotion_disable, NULL, 1);
  510. if (icpu->disable_promotion_to_c1e) /* each-cpu is redundant */
  511. on_each_cpu(c1e_promotion_disable, NULL, 1);
  512. return 0;
  513. }
  514. /*
  515. * intel_idle_cpu_init()
  516. * allocate, initialize, register cpuidle_devices
  517. * @cpu: cpu/core to initialize
  518. */
  519. static int intel_idle_cpu_init(int cpu)
  520. {
  521. int cstate;
  522. struct cpuidle_device *dev;
  523. dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
  524. dev->state_count = 1;
  525. for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
  526. int num_substates, mwait_hint, mwait_cstate, mwait_substate;
  527. if (cpuidle_state_table[cstate].enter == NULL)
  528. continue;
  529. if (cstate + 1 > max_cstate) {
  530. printk(PREFIX "max_cstate %d reached\n", max_cstate);
  531. break;
  532. }
  533. mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
  534. mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
  535. mwait_substate = MWAIT_HINT2SUBSTATE(mwait_hint);
  536. /* does the state exist in CPUID.MWAIT? */
  537. num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
  538. & MWAIT_SUBSTATE_MASK;
  539. /* if sub-state in table is not enumerated by CPUID */
  540. if ((mwait_substate + 1) > num_substates)
  541. continue;
  542. dev->state_count += 1;
  543. }
  544. dev->cpu = cpu;
  545. if (cpuidle_register_device(dev)) {
  546. pr_debug(PREFIX "cpuidle_register_device %d failed!\n", cpu);
  547. intel_idle_cpuidle_devices_uninit();
  548. return -EIO;
  549. }
  550. if (icpu->auto_demotion_disable_flags)
  551. smp_call_function_single(cpu, auto_demotion_disable, NULL, 1);
  552. return 0;
  553. }
  554. static int __init intel_idle_init(void)
  555. {
  556. int retval, i;
  557. /* Do not load intel_idle at all for now if idle= is passed */
  558. if (boot_option_idle_override != IDLE_NO_OVERRIDE)
  559. return -ENODEV;
  560. retval = intel_idle_probe();
  561. if (retval)
  562. return retval;
  563. intel_idle_cpuidle_driver_init();
  564. retval = cpuidle_register_driver(&intel_idle_driver);
  565. if (retval) {
  566. struct cpuidle_driver *drv = cpuidle_get_driver();
  567. printk(KERN_DEBUG PREFIX "intel_idle yielding to %s",
  568. drv ? drv->name : "none");
  569. return retval;
  570. }
  571. intel_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  572. if (intel_idle_cpuidle_devices == NULL)
  573. return -ENOMEM;
  574. for_each_online_cpu(i) {
  575. retval = intel_idle_cpu_init(i);
  576. if (retval) {
  577. cpuidle_unregister_driver(&intel_idle_driver);
  578. return retval;
  579. }
  580. }
  581. register_cpu_notifier(&cpu_hotplug_notifier);
  582. return 0;
  583. }
  584. static void __exit intel_idle_exit(void)
  585. {
  586. intel_idle_cpuidle_devices_uninit();
  587. cpuidle_unregister_driver(&intel_idle_driver);
  588. if (lapic_timer_reliable_states != LAPIC_TIMER_ALWAYS_RELIABLE)
  589. on_each_cpu(__setup_broadcast_timer, (void *)false, 1);
  590. unregister_cpu_notifier(&cpu_hotplug_notifier);
  591. return;
  592. }
  593. module_init(intel_idle_init);
  594. module_exit(intel_idle_exit);
  595. module_param(max_cstate, int, 0444);
  596. MODULE_AUTHOR("Len Brown <len.brown@intel.com>");
  597. MODULE_DESCRIPTION("Cpuidle driver for Intel Hardware v" INTEL_IDLE_VERSION);
  598. MODULE_LICENSE("GPL");