1
2
3
4
5
6
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/acpi.h>
11#include <linux/cpu.h>
12#include <linux/sched.h>
13
14#include <acpi/processor.h>
15#include <asm/acpi.h>
16#include <asm/mwait.h>
17
18
19
20
21
22
23
24
25
26
27
28void acpi_processor_power_init_bm_check(struct acpi_processor_flags *flags,
29 unsigned int cpu)
30{
31 struct cpuinfo_x86 *c = &cpu_data(cpu);
32
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
38
39
40
41
42 flags->bm_check = 1;
43 }
44
45
46
47
48
49
50
51 if (c->x86_vendor == X86_VENDOR_INTEL &&
52 (c->x86 > 0xf || (c->x86 == 6 && c->x86_model >= 0x0f)))
53 flags->bm_control = 0;
54}
55EXPORT_SYMBOL(acpi_processor_power_init_bm_check);
56
57
58
59struct cstate_entry {
60 struct {
61 unsigned int eax;
62 unsigned int ecx;
63 } states[ACPI_PROCESSOR_MAX_POWER];
64};
65static struct cstate_entry __percpu *cpu_cstate_entry;
66
67static short mwait_supported[ACPI_PROCESSOR_MAX_POWER];
68
69#define NATIVE_CSTATE_BEYOND_HALT (2)
70
71static long acpi_processor_ffh_cstate_probe_cpu(void *_cx)
72{
73 struct acpi_processor_cx *cx = _cx;
74 long retval;
75 unsigned int eax, ebx, ecx, edx;
76 unsigned int edx_part;
77 unsigned int cstate_type;
78 unsigned int num_cstate_subtype;
79
80 cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
81
82
83 cstate_type = ((cx->address >> MWAIT_SUBSTATE_SIZE) &
84 MWAIT_CSTATE_MASK) + 1;
85 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
95 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
96 !(ecx & CPUID5_ECX_INTERRUPT_BREAK)) {
97 retval = -1;
98 goto out;
99 }
100
101 if (!mwait_supported[cstate_type]) {
102 mwait_supported[cstate_type] = 1;
103 printk(KERN_DEBUG
104 "Monitor-Mwait will be used to enter C-%d "
105 "state\n", cx->type);
106 }
107 snprintf(cx->desc,
108 ACPI_CX_DESC_LEN, "ACPI FFH INTEL MWAIT 0x%x",
109 cx->address);
110out:
111 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
132
133 retval = work_on_cpu(cpu, acpi_processor_ffh_cstate_probe_cpu, cx);
134 if (retval == 0) {
135
136 percpu_entry->states[cx->index].eax = cx->address;
137 percpu_entry->states[cx->index].ecx = MWAIT_ECX_INTERRUPT_BREAK;
138 }
139
140
141
142
143
144
145 if ((c->x86_vendor == X86_VENDOR_INTEL) && !(reg->access_size & 0x2))
146 cx->bm_sts_skip = 1;
147
148 return retval;
149}
150EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_probe);
151
152
153
154
155
156
157
158
159
160
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 *)¤t_thread_info()->flags);
167
168 __monitor((void *)¤t_thread_info()->flags, 0, 0);
169 smp_mb();
170 if (!need_resched())
171 __mwait(ax, cx);
172 }
173}
174
175void acpi_processor_ffh_cstate_enter(struct acpi_processor_cx *cx)
176{
177 unsigned int cpu = smp_processor_id();
178 struct cstate_entry *percpu_entry;
179
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
192 cpu_cstate_entry = alloc_percpu(struct cstate_entry);
193 return 0;
194}
195
196static void __exit ffh_cstate_exit(void)
197{
198 free_percpu(cpu_cstate_entry);
199 cpu_cstate_entry = NULL;
200}
201
202arch_initcall(ffh_cstate_init);
203__exitcall(ffh_cstate_exit);
204