intel_idle.c 17 KB

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