blob: f50e7fb2a201b752c5c007dc9da41397ca9c6c46 [file] [log] [blame]
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001/*
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04002 * Copyright (C) 2005 Intel Corporation
3 * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
4 * - Added _PDC for SMP C-states on Intel CPUs
5 */
6
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/acpi.h>
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -070011#include <linux/cpu.h>
Al Viro914e2632006-10-18 13:55:46 -040012#include <linux/sched.h>
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -040013
14#include <acpi/processor.h>
15#include <asm/acpi.h>
H. Peter Anvinbc83ccc2010-09-17 15:36:40 -070016#include <asm/mwait.h>
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -040017
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -040018/*
19 * Initialize bm_flags based on the CPU cache properties
20 * On SMP it depends on cache configuration
21 * - When cache is not shared among all CPUs, we flush cache
22 * before entering C3.
23 * - When cache is shared among all CPUs, we use bm_check
24 * mechanism as in UP case
25 *
26 * This routine is called only after all the CPUs are online
27 */
28void acpi_processor_power_init_bm_check(struct acpi_processor_flags *flags,
29 unsigned int cpu)
30{
Mike Travis92cb7612007-10-19 20:35:04 +020031 struct cpuinfo_x86 *c = &cpu_data(cpu);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -040032
33 flags->bm_check = 0;
34 if (num_online_cpus() == 1)
35 flags->bm_check = 1;
36 else if (c->x86_vendor == X86_VENDOR_INTEL) {
37 /*
Pallipadi, Venkateshee1ca482009-05-21 17:09:10 -070038 * Today all MP CPUs that support C3 share cache.
39 * And caches should not be flushed by software while
40 * entering C3 type state.
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -040041 */
42 flags->bm_check = 1;
43 }
Pallipadi, Venkateshee1ca482009-05-21 17:09:10 -070044
45 /*
46 * On all recent Intel platforms, ARB_DISABLE is a nop.
47 * So, set bm_control to zero to indicate that ARB_DISABLE
48 * is not required while entering C3 type state on
49 * P4, Core and beyond CPUs
50 */
51 if (c->x86_vendor == X86_VENDOR_INTEL &&
Zhao Yakui03a05ed2009-12-11 15:17:20 +080052 (c->x86 > 0xf || (c->x86 == 6 && c->x86_model >= 0x0f)))
Pallipadi, Venkateshee1ca482009-05-21 17:09:10 -070053 flags->bm_control = 0;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -040054}
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -040055EXPORT_SYMBOL(acpi_processor_power_init_bm_check);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -070056
57/* The code below handles cstate entry with monitor-mwait pair on Intel*/
58
Venkatesh Pallipadi5d651312007-01-10 23:08:38 -050059struct cstate_entry {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -070060 struct {
61 unsigned int eax;
62 unsigned int ecx;
63 } states[ACPI_PROCESSOR_MAX_POWER];
64};
Namhyung Kimbd126b22010-08-08 02:17:29 +090065static struct cstate_entry __percpu *cpu_cstate_entry; /* per CPU ptr */
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -070066
67static short mwait_supported[ACPI_PROCESSOR_MAX_POWER];
68
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -070069#define NATIVE_CSTATE_BEYOND_HALT (2)
70
Mike Travisc74f31c2009-01-04 05:18:07 -080071static long acpi_processor_ffh_cstate_probe_cpu(void *_cx)
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -070072{
Mike Travisc74f31c2009-01-04 05:18:07 -080073 struct acpi_processor_cx *cx = _cx;
74 long retval;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -070075 unsigned int eax, ebx, ecx, edx;
76 unsigned int edx_part;
77 unsigned int cstate_type; /* C-state type and not ACPI C-state type */
78 unsigned int num_cstate_subtype;
79
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -070080 cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
81
82 /* Check whether this particular cx_type (in CST) is supported or not */
Zhao Yakui13b40a12009-01-04 12:04:21 +080083 cstate_type = ((cx->address >> MWAIT_SUBSTATE_SIZE) &
84 MWAIT_CSTATE_MASK) + 1;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -070085 edx_part = edx >> (cstate_type * MWAIT_SUBSTATE_SIZE);
86 num_cstate_subtype = edx_part & MWAIT_SUBSTATE_MASK;
87
88 retval = 0;
89 if (num_cstate_subtype < (cx->address & MWAIT_SUBSTATE_MASK)) {
90 retval = -1;
91 goto out;
92 }
93
94 /* mwait ecx extensions INTERRUPT_BREAK should be supported for C2/C3 */
95 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
96 !(ecx & CPUID5_ECX_INTERRUPT_BREAK)) {
97 retval = -1;
98 goto out;
99 }
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700100
101 if (!mwait_supported[cstate_type]) {
102 mwait_supported[cstate_type] = 1;
Mike Travisc74f31c2009-01-04 05:18:07 -0800103 printk(KERN_DEBUG
104 "Monitor-Mwait will be used to enter C-%d "
105 "state\n", cx->type);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700106 }
Mike Travisc74f31c2009-01-04 05:18:07 -0800107 snprintf(cx->desc,
108 ACPI_CX_DESC_LEN, "ACPI FFH INTEL MWAIT 0x%x",
109 cx->address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700110out:
Mike Travisc74f31c2009-01-04 05:18:07 -0800111 return retval;
112}
113
114int acpi_processor_ffh_cstate_probe(unsigned int cpu,
115 struct acpi_processor_cx *cx, struct acpi_power_register *reg)
116{
117 struct cstate_entry *percpu_entry;
118 struct cpuinfo_x86 *c = &cpu_data(cpu);
119 long retval;
120
121 if (!cpu_cstate_entry || c->cpuid_level < CPUID_MWAIT_LEAF)
122 return -1;
123
124 if (reg->bit_offset != NATIVE_CSTATE_BEYOND_HALT)
125 return -1;
126
127 percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
128 percpu_entry->states[cx->index].eax = 0;
129 percpu_entry->states[cx->index].ecx = 0;
130
131 /* Make sure we are running on right CPU */
132
133 retval = work_on_cpu(cpu, acpi_processor_ffh_cstate_probe_cpu, cx);
134 if (retval == 0) {
135 /* Use the hint in CST */
136 percpu_entry->states[cx->index].eax = cx->address;
137 percpu_entry->states[cx->index].ecx = MWAIT_ECX_INTERRUPT_BREAK;
138 }
Len Brown718be4a2010-07-22 16:54:27 -0400139
140 /*
141 * For _CST FFH on Intel, if GAS.access_size bit 1 is cleared,
142 * then we should skip checking BM_STS for this C-state.
143 * ref: "Intel Processor Vendor-Specific ACPI Interface Specification"
144 */
145 if ((c->x86_vendor == X86_VENDOR_INTEL) && !(reg->access_size & 0x2))
146 cx->bm_sts_skip = 1;
147
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700148 return retval;
149}
150EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_probe);
151
Len Brown4bfc8282011-03-30 23:52:29 -0400152/*
153 * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
154 * which can obviate IPI to trigger checking of need_resched.
155 * We execute MONITOR against need_resched and enter optimized wait state
156 * through MWAIT. Whenever someone changes need_resched, we would be woken
157 * up from MWAIT (without an IPI).
158 *
159 * New with Core Duo processors, MWAIT can take some hints based on CPU
160 * capability.
161 */
162void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
163{
164 if (!need_resched()) {
165 if (this_cpu_has(X86_FEATURE_CLFLUSH_MONITOR))
166 clflush((void *)&current_thread_info()->flags);
167
168 __monitor((void *)&current_thread_info()->flags, 0, 0);
169 smp_mb();
170 if (!need_resched())
171 __mwait(ax, cx);
172 }
173}
174
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700175void acpi_processor_ffh_cstate_enter(struct acpi_processor_cx *cx)
176{
177 unsigned int cpu = smp_processor_id();
Venkatesh Pallipadi5d651312007-01-10 23:08:38 -0500178 struct cstate_entry *percpu_entry;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700179
180 percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
181 mwait_idle_with_hints(percpu_entry->states[cx->index].eax,
182 percpu_entry->states[cx->index].ecx);
183}
184EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_enter);
185
186static int __init ffh_cstate_init(void)
187{
188 struct cpuinfo_x86 *c = &boot_cpu_data;
189 if (c->x86_vendor != X86_VENDOR_INTEL)
190 return -1;
191
Venkatesh Pallipadi5d651312007-01-10 23:08:38 -0500192 cpu_cstate_entry = alloc_percpu(struct cstate_entry);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700193 return 0;
194}
195
196static void __exit ffh_cstate_exit(void)
197{
Alan Sterna1205862006-12-06 20:32:37 -0800198 free_percpu(cpu_cstate_entry);
199 cpu_cstate_entry = NULL;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700200}
201
202arch_initcall(ffh_cstate_init);
203__exitcall(ffh_cstate_exit);