1
2
3
4
5
6
7#include <linux/kernel.h>
8#include <linux/export.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/mwait.h>
16#include <asm/special_insns.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
90 if (num_cstate_subtype == 0) {
91 pr_warn(FW_BUG "ACPI MWAIT C-state 0x%x not supported by HW (0x%x)\n",
92 cx->address, edx_part);
93 retval = -1;
94 goto out;
95 }
96
97
98 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED) ||
99 !(ecx & CPUID5_ECX_INTERRUPT_BREAK)) {
100 retval = -1;
101 goto out;
102 }
103
104 if (!mwait_supported[cstate_type]) {
105 mwait_supported[cstate_type] = 1;
106 printk(KERN_DEBUG
107 "Monitor-Mwait will be used to enter C-%d state\n",
108 cx->type);
109 }
110 snprintf(cx->desc,
111 ACPI_CX_DESC_LEN, "ACPI FFH INTEL MWAIT 0x%x",
112 cx->address);
113out:
114 return retval;
115}
116
117int acpi_processor_ffh_cstate_probe(unsigned int cpu,
118 struct acpi_processor_cx *cx, struct acpi_power_register *reg)
119{
120 struct cstate_entry *percpu_entry;
121 struct cpuinfo_x86 *c = &cpu_data(cpu);
122 long retval;
123
124 if (!cpu_cstate_entry || c->cpuid_level < CPUID_MWAIT_LEAF)
125 return -1;
126
127 if (reg->bit_offset != NATIVE_CSTATE_BEYOND_HALT)
128 return -1;
129
130 percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
131 percpu_entry->states[cx->index].eax = 0;
132 percpu_entry->states[cx->index].ecx = 0;
133
134
135
136 retval = call_on_cpu(cpu, acpi_processor_ffh_cstate_probe_cpu, cx,
137 false);
138 if (retval == 0) {
139
140 percpu_entry->states[cx->index].eax = cx->address;
141 percpu_entry->states[cx->index].ecx = MWAIT_ECX_INTERRUPT_BREAK;
142 }
143
144
145
146
147
148
149 if ((c->x86_vendor == X86_VENDOR_INTEL) && !(reg->access_size & 0x2))
150 cx->bm_sts_skip = 1;
151
152 return retval;
153}
154EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_probe);
155
156void __cpuidle acpi_processor_ffh_cstate_enter(struct acpi_processor_cx *cx)
157{
158 unsigned int cpu = smp_processor_id();
159 struct cstate_entry *percpu_entry;
160
161 percpu_entry = per_cpu_ptr(cpu_cstate_entry, cpu);
162 mwait_idle_with_hints(percpu_entry->states[cx->index].eax,
163 percpu_entry->states[cx->index].ecx);
164}
165EXPORT_SYMBOL_GPL(acpi_processor_ffh_cstate_enter);
166
167static int __init ffh_cstate_init(void)
168{
169 struct cpuinfo_x86 *c = &boot_cpu_data;
170
171 if (c->x86_vendor != X86_VENDOR_INTEL &&
172 c->x86_vendor != X86_VENDOR_AMD)
173 return -1;
174
175 cpu_cstate_entry = alloc_percpu(struct cstate_entry);
176 return 0;
177}
178
179static void __exit ffh_cstate_exit(void)
180{
181 free_percpu(cpu_cstate_entry);
182 cpu_cstate_entry = NULL;
183}
184
185arch_initcall(ffh_cstate_init);
186__exitcall(ffh_cstate_exit);
187