Skip to content

Commit 703fb76

Browse files
sandip4nPeter Zijlstra
authored andcommitted
perf/x86/amd/lbr: Detect LbrExtV2 support
AMD Last Branch Record Extension Version 2 (LbrExtV2) is driven by Core PMC overflows. It records recently taken branches up to the moment when the PMC overflow occurs. Detect the feature during PMU initialization and set the branch stack depth using CPUID leaf 0x80000022 EBX. Signed-off-by: Sandipan Das <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/fc6e45378ada258f1bab79b0de6e05c393a8f1dd.1660211399.git.sandipan.das@amd.com
1 parent 257449c commit 703fb76

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

arch/x86/events/amd/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-License-Identifier: GPL-2.0
2-
obj-$(CONFIG_CPU_SUP_AMD) += core.o
2+
obj-$(CONFIG_CPU_SUP_AMD) += core.o lbr.o
33
obj-$(CONFIG_PERF_EVENTS_AMD_BRS) += brs.o
44
obj-$(CONFIG_PERF_EVENTS_AMD_POWER) += power.o
55
obj-$(CONFIG_X86_LOCAL_APIC) += ibs.o

arch/x86/events/amd/core.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,10 +1374,11 @@ static int __init amd_core_pmu_init(void)
13741374
x86_pmu.flags |= PMU_FL_PAIR;
13751375
}
13761376

1377-
/*
1378-
* BRS requires special event constraints and flushing on ctxsw.
1379-
*/
1380-
if (boot_cpu_data.x86 >= 0x19 && !amd_brs_init()) {
1377+
/* LBR and BRS are mutually exclusive features */
1378+
if (amd_pmu_lbr_init() && !amd_brs_init()) {
1379+
/*
1380+
* BRS requires special event constraints and flushing on ctxsw.
1381+
*/
13811382
x86_pmu.get_event_constraints = amd_get_event_constraints_f19h;
13821383
x86_pmu.sched_task = amd_pmu_brs_sched_task;
13831384
x86_pmu.limit_period = amd_pmu_limit_period;

arch/x86/events/amd/lbr.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/perf_event.h>
3+
#include <asm/perf_event.h>
4+
5+
#include "../perf_event.h"
6+
7+
__init int amd_pmu_lbr_init(void)
8+
{
9+
union cpuid_0x80000022_ebx ebx;
10+
11+
if (x86_pmu.version < 2 || !boot_cpu_has(X86_FEATURE_AMD_LBR_V2))
12+
return -EOPNOTSUPP;
13+
14+
/* Set number of entries */
15+
ebx.full = cpuid_ebx(EXT_PERFMON_DEBUG_FEATURES);
16+
x86_pmu.lbr_nr = ebx.split.lbr_v2_stack_sz;
17+
18+
pr_cont("%d-deep LBR, ", x86_pmu.lbr_nr);
19+
20+
return 0;
21+
}

arch/x86/events/perf_event.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,8 @@ static inline bool fixed_counter_disabled(int i, struct pmu *pmu)
12321232

12331233
int amd_pmu_init(void);
12341234

1235+
int amd_pmu_lbr_init(void);
1236+
12351237
#ifdef CONFIG_PERF_EVENTS_AMD_BRS
12361238

12371239
#define AMD_FAM19H_BRS_EVENT 0xc4 /* RETIRED_TAKEN_BRANCH_INSTRUCTIONS */

arch/x86/include/asm/perf_event.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ union cpuid_0x80000022_ebx {
207207
struct {
208208
/* Number of Core Performance Counters */
209209
unsigned int num_core_pmc:4;
210-
unsigned int reserved:6;
210+
/* Number of available LBR Stack Entries */
211+
unsigned int lbr_v2_stack_sz:6;
211212
/* Number of Data Fabric Counters */
212213
unsigned int num_df_pmc:6;
213214
} split;

0 commit comments

Comments
 (0)