2 * Performance events - AMD IBS
4 * Copyright (C) 2011 Advanced Micro Devices, Inc., Robert Richter
6 * For licencing details see kernel-base/COPYING
9 #include <linux/perf_event.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
17 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD)
19 #include <linux/kprobes.h>
20 #include <linux/hardirq.h>
24 #define IBS_FETCH_CONFIG_MASK (IBS_FETCH_RAND_EN | IBS_FETCH_MAX_CNT)
25 #define IBS_OP_CONFIG_MASK IBS_OP_MAX_CNT
36 struct perf_event
*event
;
37 unsigned long state
[BITS_TO_LONGS(IBS_MAX_STATES
)];
47 unsigned long offset_mask
[1];
49 struct cpu_perf_ibs __percpu
*pcpu
;
52 struct perf_ibs_data
{
55 u32 data
[0]; /* data buffer starts here */
58 u64 regs
[MSR_AMD64_IBS_REG_COUNT_MAX
];
61 static struct perf_ibs perf_ibs_fetch
;
62 static struct perf_ibs perf_ibs_op
;
64 static struct perf_ibs
*get_ibs_pmu(int type
)
66 if (perf_ibs_fetch
.pmu
.type
== type
)
67 return &perf_ibs_fetch
;
68 if (perf_ibs_op
.pmu
.type
== type
)
73 static int perf_ibs_init(struct perf_event
*event
)
75 struct hw_perf_event
*hwc
= &event
->hw
;
76 struct perf_ibs
*perf_ibs
;
79 perf_ibs
= get_ibs_pmu(event
->attr
.type
);
83 config
= event
->attr
.config
;
84 if (config
& ~perf_ibs
->config_mask
)
87 if (hwc
->sample_period
) {
88 if (config
& perf_ibs
->cnt_mask
)
89 /* raw max_cnt may not be set */
91 if (hwc
->sample_period
& 0x0f)
92 /* lower 4 bits can not be set in ibs max cnt */
94 max_cnt
= hwc
->sample_period
>> 4;
95 if (max_cnt
& ~perf_ibs
->cnt_mask
)
100 max_cnt
= config
& perf_ibs
->cnt_mask
;
101 event
->attr
.sample_period
= max_cnt
<< 4;
102 hwc
->sample_period
= event
->attr
.sample_period
;
108 hwc
->config_base
= perf_ibs
->msr
;
109 hwc
->config
= config
;
114 static void perf_ibs_start(struct perf_event
*event
, int flags
)
116 struct hw_perf_event
*hwc
= &event
->hw
;
117 struct perf_ibs
*perf_ibs
= container_of(event
->pmu
, struct perf_ibs
, pmu
);
118 struct cpu_perf_ibs
*pcpu
= this_cpu_ptr(perf_ibs
->pcpu
);
120 if (test_and_set_bit(IBS_STARTED
, pcpu
->state
))
123 wrmsrl(hwc
->config_base
, hwc
->config
| perf_ibs
->enable_mask
);
126 static void perf_ibs_stop(struct perf_event
*event
, int flags
)
128 struct hw_perf_event
*hwc
= &event
->hw
;
129 struct perf_ibs
*perf_ibs
= container_of(event
->pmu
, struct perf_ibs
, pmu
);
130 struct cpu_perf_ibs
*pcpu
= this_cpu_ptr(perf_ibs
->pcpu
);
133 if (!test_and_clear_bit(IBS_STARTED
, pcpu
->state
))
136 set_bit(IBS_STOPPING
, pcpu
->state
);
138 rdmsrl(hwc
->config_base
, val
);
139 val
&= ~perf_ibs
->enable_mask
;
140 wrmsrl(hwc
->config_base
, val
);
143 static int perf_ibs_add(struct perf_event
*event
, int flags
)
145 struct perf_ibs
*perf_ibs
= container_of(event
->pmu
, struct perf_ibs
, pmu
);
146 struct cpu_perf_ibs
*pcpu
= this_cpu_ptr(perf_ibs
->pcpu
);
148 if (test_and_set_bit(IBS_ENABLED
, pcpu
->state
))
153 if (flags
& PERF_EF_START
)
154 perf_ibs_start(event
, PERF_EF_RELOAD
);
159 static void perf_ibs_del(struct perf_event
*event
, int flags
)
161 struct perf_ibs
*perf_ibs
= container_of(event
->pmu
, struct perf_ibs
, pmu
);
162 struct cpu_perf_ibs
*pcpu
= this_cpu_ptr(perf_ibs
->pcpu
);
164 if (!test_and_clear_bit(IBS_ENABLED
, pcpu
->state
))
167 perf_ibs_stop(event
, 0);
172 static void perf_ibs_read(struct perf_event
*event
) { }
174 static struct perf_ibs perf_ibs_fetch
= {
176 .task_ctx_nr
= perf_invalid_context
,
178 .event_init
= perf_ibs_init
,
181 .start
= perf_ibs_start
,
182 .stop
= perf_ibs_stop
,
183 .read
= perf_ibs_read
,
185 .msr
= MSR_AMD64_IBSFETCHCTL
,
186 .config_mask
= IBS_FETCH_CONFIG_MASK
,
187 .cnt_mask
= IBS_FETCH_MAX_CNT
,
188 .enable_mask
= IBS_FETCH_ENABLE
,
189 .valid_mask
= IBS_FETCH_VAL
,
190 .offset_mask
= { MSR_AMD64_IBSFETCH_REG_MASK
},
191 .offset_max
= MSR_AMD64_IBSFETCH_REG_COUNT
,
194 static struct perf_ibs perf_ibs_op
= {
196 .task_ctx_nr
= perf_invalid_context
,
198 .event_init
= perf_ibs_init
,
201 .start
= perf_ibs_start
,
202 .stop
= perf_ibs_stop
,
203 .read
= perf_ibs_read
,
205 .msr
= MSR_AMD64_IBSOPCTL
,
206 .config_mask
= IBS_OP_CONFIG_MASK
,
207 .cnt_mask
= IBS_OP_MAX_CNT
,
208 .enable_mask
= IBS_OP_ENABLE
,
209 .valid_mask
= IBS_OP_VAL
,
210 .offset_mask
= { MSR_AMD64_IBSOP_REG_MASK
},
211 .offset_max
= MSR_AMD64_IBSOP_REG_COUNT
,
214 static int perf_ibs_handle_irq(struct perf_ibs
*perf_ibs
, struct pt_regs
*iregs
)
216 struct cpu_perf_ibs
*pcpu
= this_cpu_ptr(perf_ibs
->pcpu
);
217 struct perf_event
*event
= pcpu
->event
;
218 struct hw_perf_event
*hwc
= &event
->hw
;
219 struct perf_sample_data data
;
220 struct perf_raw_record raw
;
222 struct perf_ibs_data ibs_data
;
227 if (!test_bit(IBS_STARTED
, pcpu
->state
)) {
228 /* Catch spurious interrupts after stopping IBS: */
229 if (!test_and_clear_bit(IBS_STOPPING
, pcpu
->state
))
231 rdmsrl(perf_ibs
->msr
, *ibs_data
.regs
);
232 return (*ibs_data
.regs
& perf_ibs
->valid_mask
) ? 1 : 0;
235 msr
= hwc
->config_base
;
238 if (!(*buf
++ & perf_ibs
->valid_mask
))
241 perf_sample_data_init(&data
, 0);
242 if (event
->attr
.sample_type
& PERF_SAMPLE_RAW
) {
243 ibs_data
.caps
= ibs_caps
;
247 rdmsrl(msr
+ offset
, *buf
++);
249 offset
= find_next_bit(perf_ibs
->offset_mask
,
250 perf_ibs
->offset_max
,
252 } while (offset
< perf_ibs
->offset_max
);
253 raw
.size
= sizeof(u32
) + sizeof(u64
) * size
;
254 raw
.data
= ibs_data
.data
;
258 regs
= *iregs
; /* XXX: update ip from ibs sample */
260 if (perf_event_overflow(event
, &data
, ®s
))
264 wrmsrl(hwc
->config_base
, hwc
->config
| perf_ibs
->enable_mask
);
270 perf_ibs_nmi_handler(unsigned int cmd
, struct pt_regs
*regs
)
274 handled
+= perf_ibs_handle_irq(&perf_ibs_fetch
, regs
);
275 handled
+= perf_ibs_handle_irq(&perf_ibs_op
, regs
);
278 inc_irq_stat(apic_perf_irqs
);
283 static __init
int perf_ibs_pmu_init(struct perf_ibs
*perf_ibs
, char *name
)
285 struct cpu_perf_ibs __percpu
*pcpu
;
288 pcpu
= alloc_percpu(struct cpu_perf_ibs
);
292 perf_ibs
->pcpu
= pcpu
;
294 ret
= perf_pmu_register(&perf_ibs
->pmu
, name
, -1);
296 perf_ibs
->pcpu
= NULL
;
303 static __init
int perf_event_ibs_init(void)
306 return -ENODEV
; /* ibs not supported by the cpu */
308 perf_ibs_pmu_init(&perf_ibs_fetch
, "ibs_fetch");
309 perf_ibs_pmu_init(&perf_ibs_op
, "ibs_op");
310 register_nmi_handler(NMI_LOCAL
, &perf_ibs_nmi_handler
, 0, "perf_ibs");
311 printk(KERN_INFO
"perf: AMD IBS detected (0x%08x)\n", ibs_caps
);
316 #else /* defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD) */
318 static __init
int perf_event_ibs_init(void) { return 0; }
322 /* IBS - apic initialization, for perf and oprofile */
324 static __init u32
__get_ibs_caps(void)
327 unsigned int max_level
;
329 if (!boot_cpu_has(X86_FEATURE_IBS
))
332 /* check IBS cpuid feature flags */
333 max_level
= cpuid_eax(0x80000000);
334 if (max_level
< IBS_CPUID_FEATURES
)
335 return IBS_CAPS_DEFAULT
;
337 caps
= cpuid_eax(IBS_CPUID_FEATURES
);
338 if (!(caps
& IBS_CAPS_AVAIL
))
339 /* cpuid flags not valid */
340 return IBS_CAPS_DEFAULT
;
345 u32
get_ibs_caps(void)
350 EXPORT_SYMBOL(get_ibs_caps
);
352 static inline int get_eilvt(int offset
)
354 return !setup_APIC_eilvt(offset
, 0, APIC_EILVT_MSG_NMI
, 1);
357 static inline int put_eilvt(int offset
)
359 return !setup_APIC_eilvt(offset
, 0, 0, 1);
363 * Check and reserve APIC extended interrupt LVT offset for IBS if available.
365 static inline int ibs_eilvt_valid(void)
373 rdmsrl(MSR_AMD64_IBSCTL
, val
);
374 offset
= val
& IBSCTL_LVT_OFFSET_MASK
;
376 if (!(val
& IBSCTL_LVT_OFFSET_VALID
)) {
377 pr_err(FW_BUG
"cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
378 smp_processor_id(), offset
, MSR_AMD64_IBSCTL
, val
);
382 if (!get_eilvt(offset
)) {
383 pr_err(FW_BUG
"cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
384 smp_processor_id(), offset
, MSR_AMD64_IBSCTL
, val
);
395 static int setup_ibs_ctl(int ibs_eilvt_off
)
397 struct pci_dev
*cpu_cfg
;
404 cpu_cfg
= pci_get_device(PCI_VENDOR_ID_AMD
,
405 PCI_DEVICE_ID_AMD_10H_NB_MISC
,
410 pci_write_config_dword(cpu_cfg
, IBSCTL
, ibs_eilvt_off
411 | IBSCTL_LVT_OFFSET_VALID
);
412 pci_read_config_dword(cpu_cfg
, IBSCTL
, &value
);
413 if (value
!= (ibs_eilvt_off
| IBSCTL_LVT_OFFSET_VALID
)) {
414 pci_dev_put(cpu_cfg
);
415 printk(KERN_DEBUG
"Failed to setup IBS LVT offset, "
416 "IBSCTL = 0x%08x\n", value
);
422 printk(KERN_DEBUG
"No CPU node configured for IBS\n");
430 * This runs only on the current cpu. We try to find an LVT offset and
431 * setup the local APIC. For this we must disable preemption. On
432 * success we initialize all nodes with this offset. This updates then
433 * the offset in the IBS_CTL per-node msr. The per-core APIC setup of
434 * the IBS interrupt vector is handled by perf_ibs_cpu_notifier that
435 * is using the new offset.
437 static int force_ibs_eilvt_setup(void)
443 /* find the next free available EILVT entry, skip offset 0 */
444 for (offset
= 1; offset
< APIC_EILVT_NR_MAX
; offset
++) {
445 if (get_eilvt(offset
))
450 if (offset
== APIC_EILVT_NR_MAX
) {
451 printk(KERN_DEBUG
"No EILVT entry available\n");
455 ret
= setup_ibs_ctl(offset
);
459 if (!ibs_eilvt_valid()) {
464 pr_info("IBS: LVT offset %d assigned\n", offset
);
474 static inline int get_ibs_lvt_offset(void)
478 rdmsrl(MSR_AMD64_IBSCTL
, val
);
479 if (!(val
& IBSCTL_LVT_OFFSET_VALID
))
482 return val
& IBSCTL_LVT_OFFSET_MASK
;
485 static void setup_APIC_ibs(void *dummy
)
489 offset
= get_ibs_lvt_offset();
493 if (!setup_APIC_eilvt(offset
, 0, APIC_EILVT_MSG_NMI
, 0))
496 pr_warn("perf: IBS APIC setup failed on cpu #%d\n",
500 static void clear_APIC_ibs(void *dummy
)
504 offset
= get_ibs_lvt_offset();
506 setup_APIC_eilvt(offset
, 0, APIC_EILVT_MSG_FIX
, 1);
510 perf_ibs_cpu_notifier(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
512 switch (action
& ~CPU_TASKS_FROZEN
) {
514 setup_APIC_ibs(NULL
);
517 clear_APIC_ibs(NULL
);
526 static __init
int amd_ibs_init(void)
531 caps
= __get_ibs_caps();
533 return -ENODEV
; /* ibs not supported by the cpu */
536 * Force LVT offset assignment for family 10h: The offsets are
537 * not assigned by the BIOS for this family, so the OS is
538 * responsible for doing it. If the OS assignment fails, fall
539 * back to BIOS settings and try to setup this.
541 if (boot_cpu_data
.x86
== 0x10)
542 force_ibs_eilvt_setup();
544 if (!ibs_eilvt_valid())
549 /* make ibs_caps visible to other cpus: */
551 perf_cpu_notifier(perf_ibs_cpu_notifier
);
552 smp_call_function(setup_APIC_ibs
, NULL
, 1);
555 ret
= perf_event_ibs_init();
558 pr_err("Failed to setup IBS, %d\n", ret
);
562 /* Since we need the pci subsystem to init ibs we can't do this earlier: */
563 device_initcall(amd_ibs_init
);