perf/x86/amd/ibs: Fix waking up from S3 for AMD family 10h
[pandora-kernel.git] / arch / x86 / kernel / cpu / perf_event_amd_ibs.c
1 /*
2  * Performance events - AMD IBS
3  *
4  *  Copyright (C) 2011 Advanced Micro Devices, Inc., Robert Richter
5  *
6  *  For licencing details see kernel-base/COPYING
7  */
8
9 #include <linux/perf_event.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/syscore_ops.h>
13
14 #include <asm/apic.h>
15
16 static u32 ibs_caps;
17
18 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD)
19
20 static struct pmu perf_ibs;
21
22 static int perf_ibs_init(struct perf_event *event)
23 {
24         if (perf_ibs.type != event->attr.type)
25                 return -ENOENT;
26         return 0;
27 }
28
29 static int perf_ibs_add(struct perf_event *event, int flags)
30 {
31         return 0;
32 }
33
34 static void perf_ibs_del(struct perf_event *event, int flags)
35 {
36 }
37
38 static struct pmu perf_ibs = {
39         .event_init= perf_ibs_init,
40         .add= perf_ibs_add,
41         .del= perf_ibs_del,
42 };
43
44 static __init int perf_event_ibs_init(void)
45 {
46         if (!ibs_caps)
47                 return -ENODEV; /* ibs not supported by the cpu */
48
49         perf_pmu_register(&perf_ibs, "ibs", -1);
50         printk(KERN_INFO "perf: AMD IBS detected (0x%08x)\n", ibs_caps);
51
52         return 0;
53 }
54
55 #else /* defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD) */
56
57 static __init int perf_event_ibs_init(void) { return 0; }
58
59 #endif
60
61 /* IBS - apic initialization, for perf and oprofile */
62
63 static __init u32 __get_ibs_caps(void)
64 {
65         u32 caps;
66         unsigned int max_level;
67
68         if (!boot_cpu_has(X86_FEATURE_IBS))
69                 return 0;
70
71         /* check IBS cpuid feature flags */
72         max_level = cpuid_eax(0x80000000);
73         if (max_level < IBS_CPUID_FEATURES)
74                 return IBS_CAPS_DEFAULT;
75
76         caps = cpuid_eax(IBS_CPUID_FEATURES);
77         if (!(caps & IBS_CAPS_AVAIL))
78                 /* cpuid flags not valid */
79                 return IBS_CAPS_DEFAULT;
80
81         return caps;
82 }
83
84 u32 get_ibs_caps(void)
85 {
86         return ibs_caps;
87 }
88
89 EXPORT_SYMBOL(get_ibs_caps);
90
91 static inline int get_eilvt(int offset)
92 {
93         return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1);
94 }
95
96 static inline int put_eilvt(int offset)
97 {
98         return !setup_APIC_eilvt(offset, 0, 0, 1);
99 }
100
101 /*
102  * Check and reserve APIC extended interrupt LVT offset for IBS if available.
103  */
104 static inline int ibs_eilvt_valid(void)
105 {
106         int offset;
107         u64 val;
108         int valid = 0;
109
110         preempt_disable();
111
112         rdmsrl(MSR_AMD64_IBSCTL, val);
113         offset = val & IBSCTL_LVT_OFFSET_MASK;
114
115         if (!(val & IBSCTL_LVT_OFFSET_VALID)) {
116                 pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
117                        smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
118                 goto out;
119         }
120
121         if (!get_eilvt(offset)) {
122                 pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
123                        smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
124                 goto out;
125         }
126
127         valid = 1;
128 out:
129         preempt_enable();
130
131         return valid;
132 }
133
134 static int setup_ibs_ctl(int ibs_eilvt_off)
135 {
136         struct pci_dev *cpu_cfg;
137         int nodes;
138         u32 value = 0;
139
140         nodes = 0;
141         cpu_cfg = NULL;
142         do {
143                 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
144                                          PCI_DEVICE_ID_AMD_10H_NB_MISC,
145                                          cpu_cfg);
146                 if (!cpu_cfg)
147                         break;
148                 ++nodes;
149                 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
150                                        | IBSCTL_LVT_OFFSET_VALID);
151                 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
152                 if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) {
153                         pci_dev_put(cpu_cfg);
154                         printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
155                                "IBSCTL = 0x%08x\n", value);
156                         return -EINVAL;
157                 }
158         } while (1);
159
160         if (!nodes) {
161                 printk(KERN_DEBUG "No CPU node configured for IBS\n");
162                 return -ENODEV;
163         }
164
165         return 0;
166 }
167
168 /*
169  * This runs only on the current cpu. We try to find an LVT offset and
170  * setup the local APIC. For this we must disable preemption. On
171  * success we initialize all nodes with this offset. This updates then
172  * the offset in the IBS_CTL per-node msr. The per-core APIC setup of
173  * the IBS interrupt vector is handled by perf_ibs_cpu_notifier that
174  * is using the new offset.
175  */
176 static int force_ibs_eilvt_setup(void)
177 {
178         int offset;
179         int ret;
180
181         preempt_disable();
182         /* find the next free available EILVT entry, skip offset 0 */
183         for (offset = 1; offset < APIC_EILVT_NR_MAX; offset++) {
184                 if (get_eilvt(offset))
185                         break;
186         }
187         preempt_enable();
188
189         if (offset == APIC_EILVT_NR_MAX) {
190                 printk(KERN_DEBUG "No EILVT entry available\n");
191                 return -EBUSY;
192         }
193
194         ret = setup_ibs_ctl(offset);
195         if (ret)
196                 goto out;
197
198         if (!ibs_eilvt_valid()) {
199                 ret = -EFAULT;
200                 goto out;
201         }
202
203         pr_info("IBS: LVT offset %d assigned\n", offset);
204
205         return 0;
206 out:
207         preempt_disable();
208         put_eilvt(offset);
209         preempt_enable();
210         return ret;
211 }
212
213 static void ibs_eilvt_setup(void)
214 {
215         /*
216          * Force LVT offset assignment for family 10h: The offsets are
217          * not assigned by the BIOS for this family, so the OS is
218          * responsible for doing it. If the OS assignment fails, fall
219          * back to BIOS settings and try to setup this.
220          */
221         if (boot_cpu_data.x86 == 0x10)
222                 force_ibs_eilvt_setup();
223 }
224
225 static inline int get_ibs_lvt_offset(void)
226 {
227         u64 val;
228
229         rdmsrl(MSR_AMD64_IBSCTL, val);
230         if (!(val & IBSCTL_LVT_OFFSET_VALID))
231                 return -EINVAL;
232
233         return val & IBSCTL_LVT_OFFSET_MASK;
234 }
235
236 static void setup_APIC_ibs(void *dummy)
237 {
238         int offset;
239
240         offset = get_ibs_lvt_offset();
241         if (offset < 0)
242                 goto failed;
243
244         if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0))
245                 return;
246 failed:
247         pr_warn("perf: IBS APIC setup failed on cpu #%d\n",
248                 smp_processor_id());
249 }
250
251 static void clear_APIC_ibs(void *dummy)
252 {
253         int offset;
254
255         offset = get_ibs_lvt_offset();
256         if (offset >= 0)
257                 setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1);
258 }
259
260 #ifdef CONFIG_PM
261
262 static int perf_ibs_suspend(void)
263 {
264         clear_APIC_ibs(NULL);
265         return 0;
266 }
267
268 static void perf_ibs_resume(void)
269 {
270         ibs_eilvt_setup();
271         setup_APIC_ibs(NULL);
272 }
273
274 static struct syscore_ops perf_ibs_syscore_ops = {
275         .resume         = perf_ibs_resume,
276         .suspend        = perf_ibs_suspend,
277 };
278
279 static void perf_ibs_pm_init(void)
280 {
281         register_syscore_ops(&perf_ibs_syscore_ops);
282 }
283
284 #else
285
286 static inline void perf_ibs_pm_init(void) { }
287
288 #endif
289
290 static int __cpuinit
291 perf_ibs_cpu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
292 {
293         switch (action & ~CPU_TASKS_FROZEN) {
294         case CPU_STARTING:
295                 setup_APIC_ibs(NULL);
296                 break;
297         case CPU_DYING:
298                 clear_APIC_ibs(NULL);
299                 break;
300         default:
301                 break;
302         }
303
304         return NOTIFY_OK;
305 }
306
307 static __init int amd_ibs_init(void)
308 {
309         u32 caps;
310         int ret = -EINVAL;
311
312         caps = __get_ibs_caps();
313         if (!caps)
314                 return -ENODEV; /* ibs not supported by the cpu */
315
316         ibs_eilvt_setup();
317
318         if (!ibs_eilvt_valid())
319                 goto out;
320
321         perf_ibs_pm_init();
322         get_online_cpus();
323         ibs_caps = caps;
324         /* make ibs_caps visible to other cpus: */
325         smp_mb();
326         perf_cpu_notifier(perf_ibs_cpu_notifier);
327         smp_call_function(setup_APIC_ibs, NULL, 1);
328         put_online_cpus();
329
330         ret = perf_event_ibs_init();
331 out:
332         if (ret)
333                 pr_err("Failed to setup IBS, %d\n", ret);
334         return ret;
335 }
336
337 /* Since we need the pci subsystem to init ibs we can't do this earlier: */
338 device_initcall(amd_ibs_init);