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
34 unsigned long offset_mask
[1];
38 struct perf_ibs_data
{
41 u32 data
[0]; /* data buffer starts here */
44 u64 regs
[MSR_AMD64_IBS_REG_COUNT_MAX
];
47 static struct perf_ibs perf_ibs_fetch
;
48 static struct perf_ibs perf_ibs_op
;
50 static struct perf_ibs
*get_ibs_pmu(int type
)
52 if (perf_ibs_fetch
.pmu
.type
== type
)
53 return &perf_ibs_fetch
;
54 if (perf_ibs_op
.pmu
.type
== type
)
59 static int perf_ibs_init(struct perf_event
*event
)
61 struct hw_perf_event
*hwc
= &event
->hw
;
62 struct perf_ibs
*perf_ibs
;
65 perf_ibs
= get_ibs_pmu(event
->attr
.type
);
69 config
= event
->attr
.config
;
70 if (config
& ~perf_ibs
->config_mask
)
73 if (hwc
->sample_period
) {
74 if (config
& perf_ibs
->cnt_mask
)
75 /* raw max_cnt may not be set */
77 if (hwc
->sample_period
& 0x0f)
78 /* lower 4 bits can not be set in ibs max cnt */
80 max_cnt
= hwc
->sample_period
>> 4;
81 if (max_cnt
& ~perf_ibs
->cnt_mask
)
86 max_cnt
= config
& perf_ibs
->cnt_mask
;
87 event
->attr
.sample_period
= max_cnt
<< 4;
88 hwc
->sample_period
= event
->attr
.sample_period
;
94 hwc
->config_base
= perf_ibs
->msr
;
100 static int perf_ibs_add(struct perf_event
*event
, int flags
)
105 static void perf_ibs_del(struct perf_event
*event
, int flags
)
109 static struct perf_ibs perf_ibs_fetch
= {
111 .task_ctx_nr
= perf_invalid_context
,
113 .event_init
= perf_ibs_init
,
117 .msr
= MSR_AMD64_IBSFETCHCTL
,
118 .config_mask
= IBS_FETCH_CONFIG_MASK
,
119 .cnt_mask
= IBS_FETCH_MAX_CNT
,
120 .enable_mask
= IBS_FETCH_ENABLE
,
121 .valid_mask
= IBS_FETCH_VAL
,
122 .offset_mask
= { MSR_AMD64_IBSFETCH_REG_MASK
},
123 .offset_max
= MSR_AMD64_IBSFETCH_REG_COUNT
,
126 static struct perf_ibs perf_ibs_op
= {
128 .task_ctx_nr
= perf_invalid_context
,
130 .event_init
= perf_ibs_init
,
134 .msr
= MSR_AMD64_IBSOPCTL
,
135 .config_mask
= IBS_OP_CONFIG_MASK
,
136 .cnt_mask
= IBS_OP_MAX_CNT
,
137 .enable_mask
= IBS_OP_ENABLE
,
138 .valid_mask
= IBS_OP_VAL
,
139 .offset_mask
= { MSR_AMD64_IBSOP_REG_MASK
},
140 .offset_max
= MSR_AMD64_IBSOP_REG_COUNT
,
143 static int perf_ibs_handle_irq(struct perf_ibs
*perf_ibs
, struct pt_regs
*iregs
)
145 struct perf_event
*event
= NULL
;
146 struct hw_perf_event
*hwc
= &event
->hw
;
147 struct perf_sample_data data
;
148 struct perf_raw_record raw
;
150 struct perf_ibs_data ibs_data
;
155 msr
= hwc
->config_base
;
158 if (!(*buf
++ & perf_ibs
->valid_mask
))
161 perf_sample_data_init(&data
, 0);
162 if (event
->attr
.sample_type
& PERF_SAMPLE_RAW
) {
163 ibs_data
.caps
= ibs_caps
;
167 rdmsrl(msr
+ offset
, *buf
++);
169 offset
= find_next_bit(perf_ibs
->offset_mask
,
170 perf_ibs
->offset_max
,
172 } while (offset
< perf_ibs
->offset_max
);
173 raw
.size
= sizeof(u32
) + sizeof(u64
) * size
;
174 raw
.data
= ibs_data
.data
;
178 regs
= *iregs
; /* XXX: update ip from ibs sample */
180 if (perf_event_overflow(event
, &data
, ®s
))
184 wrmsrl(hwc
->config_base
, hwc
->config
| perf_ibs
->enable_mask
);
190 perf_ibs_nmi_handler(unsigned int cmd
, struct pt_regs
*regs
)
194 handled
+= perf_ibs_handle_irq(&perf_ibs_fetch
, regs
);
195 handled
+= perf_ibs_handle_irq(&perf_ibs_op
, regs
);
198 inc_irq_stat(apic_perf_irqs
);
203 static __init
int perf_event_ibs_init(void)
206 return -ENODEV
; /* ibs not supported by the cpu */
208 perf_pmu_register(&perf_ibs_fetch
.pmu
, "ibs_fetch", -1);
209 perf_pmu_register(&perf_ibs_op
.pmu
, "ibs_op", -1);
210 register_nmi_handler(NMI_LOCAL
, &perf_ibs_nmi_handler
, 0, "perf_ibs");
211 printk(KERN_INFO
"perf: AMD IBS detected (0x%08x)\n", ibs_caps
);
216 #else /* defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD) */
218 static __init
int perf_event_ibs_init(void) { return 0; }
222 /* IBS - apic initialization, for perf and oprofile */
224 static __init u32
__get_ibs_caps(void)
227 unsigned int max_level
;
229 if (!boot_cpu_has(X86_FEATURE_IBS
))
232 /* check IBS cpuid feature flags */
233 max_level
= cpuid_eax(0x80000000);
234 if (max_level
< IBS_CPUID_FEATURES
)
235 return IBS_CAPS_DEFAULT
;
237 caps
= cpuid_eax(IBS_CPUID_FEATURES
);
238 if (!(caps
& IBS_CAPS_AVAIL
))
239 /* cpuid flags not valid */
240 return IBS_CAPS_DEFAULT
;
245 u32
get_ibs_caps(void)
250 EXPORT_SYMBOL(get_ibs_caps
);
252 static inline int get_eilvt(int offset
)
254 return !setup_APIC_eilvt(offset
, 0, APIC_EILVT_MSG_NMI
, 1);
257 static inline int put_eilvt(int offset
)
259 return !setup_APIC_eilvt(offset
, 0, 0, 1);
263 * Check and reserve APIC extended interrupt LVT offset for IBS if available.
265 static inline int ibs_eilvt_valid(void)
273 rdmsrl(MSR_AMD64_IBSCTL
, val
);
274 offset
= val
& IBSCTL_LVT_OFFSET_MASK
;
276 if (!(val
& IBSCTL_LVT_OFFSET_VALID
)) {
277 pr_err(FW_BUG
"cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
278 smp_processor_id(), offset
, MSR_AMD64_IBSCTL
, val
);
282 if (!get_eilvt(offset
)) {
283 pr_err(FW_BUG
"cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
284 smp_processor_id(), offset
, MSR_AMD64_IBSCTL
, val
);
295 static int setup_ibs_ctl(int ibs_eilvt_off
)
297 struct pci_dev
*cpu_cfg
;
304 cpu_cfg
= pci_get_device(PCI_VENDOR_ID_AMD
,
305 PCI_DEVICE_ID_AMD_10H_NB_MISC
,
310 pci_write_config_dword(cpu_cfg
, IBSCTL
, ibs_eilvt_off
311 | IBSCTL_LVT_OFFSET_VALID
);
312 pci_read_config_dword(cpu_cfg
, IBSCTL
, &value
);
313 if (value
!= (ibs_eilvt_off
| IBSCTL_LVT_OFFSET_VALID
)) {
314 pci_dev_put(cpu_cfg
);
315 printk(KERN_DEBUG
"Failed to setup IBS LVT offset, "
316 "IBSCTL = 0x%08x\n", value
);
322 printk(KERN_DEBUG
"No CPU node configured for IBS\n");
330 * This runs only on the current cpu. We try to find an LVT offset and
331 * setup the local APIC. For this we must disable preemption. On
332 * success we initialize all nodes with this offset. This updates then
333 * the offset in the IBS_CTL per-node msr. The per-core APIC setup of
334 * the IBS interrupt vector is handled by perf_ibs_cpu_notifier that
335 * is using the new offset.
337 static int force_ibs_eilvt_setup(void)
343 /* find the next free available EILVT entry, skip offset 0 */
344 for (offset
= 1; offset
< APIC_EILVT_NR_MAX
; offset
++) {
345 if (get_eilvt(offset
))
350 if (offset
== APIC_EILVT_NR_MAX
) {
351 printk(KERN_DEBUG
"No EILVT entry available\n");
355 ret
= setup_ibs_ctl(offset
);
359 if (!ibs_eilvt_valid()) {
364 pr_info("IBS: LVT offset %d assigned\n", offset
);
374 static inline int get_ibs_lvt_offset(void)
378 rdmsrl(MSR_AMD64_IBSCTL
, val
);
379 if (!(val
& IBSCTL_LVT_OFFSET_VALID
))
382 return val
& IBSCTL_LVT_OFFSET_MASK
;
385 static void setup_APIC_ibs(void *dummy
)
389 offset
= get_ibs_lvt_offset();
393 if (!setup_APIC_eilvt(offset
, 0, APIC_EILVT_MSG_NMI
, 0))
396 pr_warn("perf: IBS APIC setup failed on cpu #%d\n",
400 static void clear_APIC_ibs(void *dummy
)
404 offset
= get_ibs_lvt_offset();
406 setup_APIC_eilvt(offset
, 0, APIC_EILVT_MSG_FIX
, 1);
410 perf_ibs_cpu_notifier(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
412 switch (action
& ~CPU_TASKS_FROZEN
) {
414 setup_APIC_ibs(NULL
);
417 clear_APIC_ibs(NULL
);
426 static __init
int amd_ibs_init(void)
431 caps
= __get_ibs_caps();
433 return -ENODEV
; /* ibs not supported by the cpu */
436 * Force LVT offset assignment for family 10h: The offsets are
437 * not assigned by the BIOS for this family, so the OS is
438 * responsible for doing it. If the OS assignment fails, fall
439 * back to BIOS settings and try to setup this.
441 if (boot_cpu_data
.x86
== 0x10)
442 force_ibs_eilvt_setup();
444 if (!ibs_eilvt_valid())
449 /* make ibs_caps visible to other cpus: */
451 perf_cpu_notifier(perf_ibs_cpu_notifier
);
452 smp_call_function(setup_APIC_ibs
, NULL
, 1);
455 ret
= perf_event_ibs_init();
458 pr_err("Failed to setup IBS, %d\n", ret
);
462 /* Since we need the pci subsystem to init ibs we can't do this earlier: */
463 device_initcall(amd_ibs_init
);