1 From b253149b843f89cd300cbdbea27ce1f847506f99 Mon Sep 17 00:00:00 2001
2 From: Len Brown <len.brown@intel.com>
3 Date: Wed, 15 Jan 2014 00:37:34 -0500
4 Subject: sched/idle/x86: Restore mwait_idle() to fix boot hangs, to improve power savings and to improve performance
6 From: Len Brown <len.brown@intel.com>
8 commit b253149b843f89cd300cbdbea27ce1f847506f99 upstream.
10 In Linux-3.9 we removed the mwait_idle() loop:
12 69fb3676df33 ("x86 idle: remove mwait_idle() and "idle=mwait" cmdline param")
14 The reasoning was that modern machines should be sufficiently
15 happy during the boot process using the default_idle() HALT
16 loop, until cpuidle loads and either acpi_idle or intel_idle
17 invoke the newer MWAIT-with-hints idle loop.
19 But two machines reported problems:
21 1. Certain Core2-era machines support MWAIT-C1 and HALT only.
22 MWAIT-C1 is preferred for optimal power and performance.
23 But if they support just C1, cpuidle never loads and
24 so they use the boot-time default idle loop forever.
26 2. Some laptops will boot-hang if HALT is used,
27 but will boot successfully if MWAIT is used.
28 This appears to be a hidden assumption in BIOS SMI,
29 that is presumably valid on the proprietary OS
30 where the BIOS was validated.
32 https://bugzilla.kernel.org/show_bug.cgi?id=60770
34 So here we effectively revert the patch above, restoring
35 the mwait_idle() loop. However, we don't bother restoring
36 the idle=mwait cmdline parameter, since it appears to add
41 For 3.9, simply revert 69fb3676df
42 for 3.10, patch -F3 applies, fuzz needed due to __cpuinit use in
43 context For 3.11, 3.12, 3.13, this patch applies cleanly
45 Tested-by: Mike Galbraith <bitbucket@online.de>
46 Signed-off-by: Len Brown <len.brown@intel.com>
47 Acked-by: Mike Galbraith <bitbucket@online.de>
48 Cc: <stable@vger.kernel.org> # 3.9+
49 Cc: Borislav Petkov <bp@alien8.de>
50 Cc: H. Peter Anvin <hpa@zytor.com>
51 Cc: Ian Malone <ibmalone@gmail.com>
52 Cc: Josh Boyer <jwboyer@redhat.com>
53 Cc: Linus Torvalds <torvalds@linux-foundation.org>
54 Cc: Mike Galbraith <efault@gmx.de>
55 Cc: Peter Zijlstra <peterz@infradead.org>
56 Cc: Thomas Gleixner <tglx@linutronix.de>
57 Link: http://lkml.kernel.org/r/345254a551eb5a6a866e048d7ab570fd2193aca4.1389763084.git.len.brown@intel.com
58 [ Ported to recent kernels. ]
59 [ Mike: 3.10 backport ]
60 Signed-off-by: Ingo Molnar <mingo@kernel.org>
61 Signed-off-by: Mike Galbraith <efault@gmx.de>
62 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
65 arch/x86/include/asm/mwait.h | 8 ++++++
66 arch/x86/kernel/process.c | 50 +++++++++++++++++++++++++++++++++++++++++++
67 2 files changed, 58 insertions(+)
69 --- a/arch/x86/include/asm/mwait.h
70 +++ b/arch/x86/include/asm/mwait.h
71 @@ -30,6 +30,14 @@ static inline void __mwait(unsigned long
72 :: "a" (eax), "c" (ecx));
75 +static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
77 + trace_hardirqs_on();
78 + /* "mwait %eax, %ecx;" */
79 + asm volatile("sti; .byte 0x0f, 0x01, 0xc9;"
80 + :: "a" (eax), "c" (ecx));
84 * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
85 * which can obviate IPI to trigger checking of need_resched.
86 --- a/arch/x86/kernel/process.c
87 +++ b/arch/x86/kernel/process.c
89 #include <asm/fpu-internal.h>
90 #include <asm/debugreg.h>
92 +#include <asm/mwait.h>
95 * per-CPU TSS segments. Threads are completely 'soft' on Linux,
96 @@ -398,6 +399,52 @@ static void amd_e400_idle(void)
101 + * Intel Core2 and older machines prefer MWAIT over HALT for C1.
102 + * We can't rely on cpuidle installing MWAIT, because it will not load
103 + * on systems that support only C1 -- so the boot default must be MWAIT.
105 + * Some AMD machines are the opposite, they depend on using HALT.
107 + * So for default C1, which is used during boot until cpuidle loads,
108 + * use MWAIT-C1 on Intel HW that has it, else use HALT.
110 +static int prefer_mwait_c1_over_halt(const struct cpuinfo_x86 *c)
112 + if (c->x86_vendor != X86_VENDOR_INTEL)
115 + if (!cpu_has(c, X86_FEATURE_MWAIT))
122 + * MONITOR/MWAIT with no hints, used for default default C1 state.
123 + * This invokes MWAIT with interrutps enabled and no flags,
124 + * which is backwards compatible with the original MWAIT implementation.
127 +static void mwait_idle(void)
129 + if (!current_set_polling_and_test()) {
130 + if (static_cpu_has(X86_FEATURE_CLFLUSH_MONITOR)) {
132 + clflush((void *)¤t_thread_info()->flags);
136 + __monitor((void *)¤t_thread_info()->flags, 0, 0);
137 + if (!need_resched())
140 + local_irq_enable();
142 + local_irq_enable();
143 + current_clr_polling();
146 void select_idle_routine(const struct cpuinfo_x86 *c)
149 @@ -411,6 +458,9 @@ void select_idle_routine(const struct cp
150 /* E400: APIC timer interrupt does not wake up CPU from C1e */
151 pr_info("using AMD E400 aware idle routine\n");
152 x86_idle = amd_e400_idle;
153 + } else if (prefer_mwait_c1_over_halt(c)) {
154 + pr_info("using mwait in idle threads\n");
155 + x86_idle = mwait_idle;
157 x86_idle = default_idle;