intel_idle.c 18 KB

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