|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Implement support for AMD Fam19h Branch Sampling feature |
| 4 | + * Based on specifications published in AMD PPR Fam19 Model 01 |
| 5 | + * |
| 6 | + * Copyright 2021 Google LLC |
| 7 | + * Contributed by Stephane Eranian <[email protected]> |
| 8 | + */ |
| 9 | +#include <linux/kernel.h> |
| 10 | +#include <asm/msr.h> |
| 11 | +#include <asm/cpufeature.h> |
| 12 | + |
| 13 | +#include "../perf_event.h" |
| 14 | + |
| 15 | +#define BRS_POISON 0xFFFFFFFFFFFFFFFEULL /* mark limit of valid entries */ |
| 16 | + |
| 17 | +/* Debug Extension Configuration register layout */ |
| 18 | +union amd_debug_extn_cfg { |
| 19 | + __u64 val; |
| 20 | + struct { |
| 21 | + __u64 rsvd0:2, /* reserved */ |
| 22 | + brsmen:1, /* branch sample enable */ |
| 23 | + rsvd4_3:2,/* reserved - must be 0x3 */ |
| 24 | + vb:1, /* valid branches recorded */ |
| 25 | + rsvd2:10, /* reserved */ |
| 26 | + msroff:4, /* index of next entry to write */ |
| 27 | + rsvd3:4, /* reserved */ |
| 28 | + pmc:3, /* #PMC holding the sampling event */ |
| 29 | + rsvd4:37; /* reserved */ |
| 30 | + }; |
| 31 | +}; |
| 32 | + |
| 33 | +static inline unsigned int brs_from(int idx) |
| 34 | +{ |
| 35 | + return MSR_AMD_SAMP_BR_FROM + 2 * idx; |
| 36 | +} |
| 37 | + |
| 38 | +static inline unsigned int brs_to(int idx) |
| 39 | +{ |
| 40 | + return MSR_AMD_SAMP_BR_FROM + 2 * idx + 1; |
| 41 | +} |
| 42 | + |
| 43 | +static inline void set_debug_extn_cfg(u64 val) |
| 44 | +{ |
| 45 | + /* bits[4:3] must always be set to 11b */ |
| 46 | + wrmsrl(MSR_AMD_DBG_EXTN_CFG, val | 3ULL << 3); |
| 47 | +} |
| 48 | + |
| 49 | +static inline u64 get_debug_extn_cfg(void) |
| 50 | +{ |
| 51 | + u64 val; |
| 52 | + |
| 53 | + rdmsrl(MSR_AMD_DBG_EXTN_CFG, val); |
| 54 | + return val; |
| 55 | +} |
| 56 | + |
| 57 | +static bool __init amd_brs_detect(void) |
| 58 | +{ |
| 59 | + if (!boot_cpu_has(X86_FEATURE_BRS)) |
| 60 | + return false; |
| 61 | + |
| 62 | + switch (boot_cpu_data.x86) { |
| 63 | + case 0x19: /* AMD Fam19h (Zen3) */ |
| 64 | + x86_pmu.lbr_nr = 16; |
| 65 | + |
| 66 | + /* No hardware filtering supported */ |
| 67 | + x86_pmu.lbr_sel_map = NULL; |
| 68 | + x86_pmu.lbr_sel_mask = 0; |
| 69 | + break; |
| 70 | + default: |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + return true; |
| 75 | +} |
| 76 | + |
| 77 | +/* |
| 78 | + * Current BRS implementation does not support branch type or privilege level |
| 79 | + * filtering. Therefore, this function simply enforces these limitations. No need for |
| 80 | + * a br_sel_map. Software filtering is not supported because it would not correlate well |
| 81 | + * with a sampling period. |
| 82 | + */ |
| 83 | +int amd_brs_setup_filter(struct perf_event *event) |
| 84 | +{ |
| 85 | + u64 type = event->attr.branch_sample_type; |
| 86 | + |
| 87 | + /* No BRS support */ |
| 88 | + if (!x86_pmu.lbr_nr) |
| 89 | + return -EOPNOTSUPP; |
| 90 | + |
| 91 | + /* Can only capture all branches, i.e., no filtering */ |
| 92 | + if ((type & ~PERF_SAMPLE_BRANCH_PLM_ALL) != PERF_SAMPLE_BRANCH_ANY) |
| 93 | + return -EINVAL; |
| 94 | + |
| 95 | + /* can only capture at all priv levels due to the way BRS works */ |
| 96 | + if ((type & PERF_SAMPLE_BRANCH_PLM_ALL) != PERF_SAMPLE_BRANCH_PLM_ALL) |
| 97 | + return -EINVAL; |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
| 101 | + |
| 102 | +/* tos = top of stack, i.e., last valid entry written */ |
| 103 | +static inline int amd_brs_get_tos(union amd_debug_extn_cfg *cfg) |
| 104 | +{ |
| 105 | + /* |
| 106 | + * msroff: index of next entry to write so top-of-stack is one off |
| 107 | + * if BRS is full then msroff is set back to 0. |
| 108 | + */ |
| 109 | + return (cfg->msroff ? cfg->msroff : x86_pmu.lbr_nr) - 1; |
| 110 | +} |
| 111 | + |
| 112 | +/* |
| 113 | + * make sure we have a sane BRS offset to begin with |
| 114 | + * especially with kexec |
| 115 | + */ |
| 116 | +void amd_brs_reset(void) |
| 117 | +{ |
| 118 | + /* |
| 119 | + * Reset config |
| 120 | + */ |
| 121 | + set_debug_extn_cfg(0); |
| 122 | + |
| 123 | + /* |
| 124 | + * Mark first entry as poisoned |
| 125 | + */ |
| 126 | + wrmsrl(brs_to(0), BRS_POISON); |
| 127 | +} |
| 128 | + |
| 129 | +int __init amd_brs_init(void) |
| 130 | +{ |
| 131 | + if (!amd_brs_detect()) |
| 132 | + return -EOPNOTSUPP; |
| 133 | + |
| 134 | + pr_cont("%d-deep BRS, ", x86_pmu.lbr_nr); |
| 135 | + |
| 136 | + return 0; |
| 137 | +} |
| 138 | + |
| 139 | +void amd_brs_enable(void) |
| 140 | +{ |
| 141 | + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 142 | + union amd_debug_extn_cfg cfg; |
| 143 | + |
| 144 | + /* Activate only on first user */ |
| 145 | + if (++cpuc->brs_active > 1) |
| 146 | + return; |
| 147 | + |
| 148 | + cfg.val = 0; /* reset all fields */ |
| 149 | + cfg.brsmen = 1; /* enable branch sampling */ |
| 150 | + |
| 151 | + /* Set enable bit */ |
| 152 | + set_debug_extn_cfg(cfg.val); |
| 153 | +} |
| 154 | + |
| 155 | +void amd_brs_enable_all(void) |
| 156 | +{ |
| 157 | + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 158 | + if (cpuc->lbr_users) |
| 159 | + amd_brs_enable(); |
| 160 | +} |
| 161 | + |
| 162 | +void amd_brs_disable(void) |
| 163 | +{ |
| 164 | + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 165 | + union amd_debug_extn_cfg cfg; |
| 166 | + |
| 167 | + /* Check if active (could be disabled via x86_pmu_disable_all()) */ |
| 168 | + if (!cpuc->brs_active) |
| 169 | + return; |
| 170 | + |
| 171 | + /* Only disable for last user */ |
| 172 | + if (--cpuc->brs_active) |
| 173 | + return; |
| 174 | + |
| 175 | + /* |
| 176 | + * Clear the brsmen bit but preserve the others as they contain |
| 177 | + * useful state such as vb and msroff |
| 178 | + */ |
| 179 | + cfg.val = get_debug_extn_cfg(); |
| 180 | + |
| 181 | + /* |
| 182 | + * When coming in on interrupt and BRS is full, then hw will have |
| 183 | + * already stopped BRS, no need to issue wrmsr again |
| 184 | + */ |
| 185 | + if (cfg.brsmen) { |
| 186 | + cfg.brsmen = 0; |
| 187 | + set_debug_extn_cfg(cfg.val); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +void amd_brs_disable_all(void) |
| 192 | +{ |
| 193 | + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 194 | + if (cpuc->lbr_users) |
| 195 | + amd_brs_disable(); |
| 196 | +} |
| 197 | + |
| 198 | +/* |
| 199 | + * Caller must ensure amd_brs_inuse() is true before calling |
| 200 | + * return: |
| 201 | + */ |
| 202 | +void amd_brs_drain(void) |
| 203 | +{ |
| 204 | + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 205 | + struct perf_event *event = cpuc->events[0]; |
| 206 | + struct perf_branch_entry *br = cpuc->lbr_entries; |
| 207 | + union amd_debug_extn_cfg cfg; |
| 208 | + u32 i, nr = 0, num, tos, start; |
| 209 | + u32 shift = 64 - boot_cpu_data.x86_virt_bits; |
| 210 | + |
| 211 | + /* |
| 212 | + * BRS event forced on PMC0, |
| 213 | + * so check if there is an event. |
| 214 | + * It is possible to have lbr_users > 0 but the event |
| 215 | + * not yet scheduled due to long latency PMU irq |
| 216 | + */ |
| 217 | + if (!event) |
| 218 | + goto empty; |
| 219 | + |
| 220 | + cfg.val = get_debug_extn_cfg(); |
| 221 | + |
| 222 | + /* Sanity check [0-x86_pmu.lbr_nr] */ |
| 223 | + if (WARN_ON_ONCE(cfg.msroff >= x86_pmu.lbr_nr)) |
| 224 | + goto empty; |
| 225 | + |
| 226 | + /* No valid branch */ |
| 227 | + if (cfg.vb == 0) |
| 228 | + goto empty; |
| 229 | + |
| 230 | + /* |
| 231 | + * msr.off points to next entry to be written |
| 232 | + * tos = most recent entry index = msr.off - 1 |
| 233 | + * BRS register buffer saturates, so we know we have |
| 234 | + * start < tos and that we have to read from start to tos |
| 235 | + */ |
| 236 | + start = 0; |
| 237 | + tos = amd_brs_get_tos(&cfg); |
| 238 | + |
| 239 | + num = tos - start + 1; |
| 240 | + |
| 241 | + /* |
| 242 | + * BRS is only one pass (saturation) from MSROFF to depth-1 |
| 243 | + * MSROFF wraps to zero when buffer is full |
| 244 | + */ |
| 245 | + for (i = 0; i < num; i++) { |
| 246 | + u32 brs_idx = tos - i; |
| 247 | + u64 from, to; |
| 248 | + |
| 249 | + rdmsrl(brs_to(brs_idx), to); |
| 250 | + |
| 251 | + /* Entry does not belong to us (as marked by kernel) */ |
| 252 | + if (to == BRS_POISON) |
| 253 | + break; |
| 254 | + |
| 255 | + rdmsrl(brs_from(brs_idx), from); |
| 256 | + |
| 257 | + /* |
| 258 | + * Sign-extend SAMP_BR_TO to 64 bits, bits 61-63 are reserved. |
| 259 | + * Necessary to generate proper virtual addresses suitable for |
| 260 | + * symbolization |
| 261 | + */ |
| 262 | + to = (u64)(((s64)to << shift) >> shift); |
| 263 | + |
| 264 | + perf_clear_branch_entry_bitfields(br+nr); |
| 265 | + |
| 266 | + br[nr].from = from; |
| 267 | + br[nr].to = to; |
| 268 | + |
| 269 | + nr++; |
| 270 | + } |
| 271 | +empty: |
| 272 | + /* Record number of sampled branches */ |
| 273 | + cpuc->lbr_stack.nr = nr; |
| 274 | +} |
| 275 | + |
| 276 | +/* |
| 277 | + * Poison most recent entry to prevent reuse by next task |
| 278 | + * required because BRS entry are not tagged by PID |
| 279 | + */ |
| 280 | +static void amd_brs_poison_buffer(void) |
| 281 | +{ |
| 282 | + union amd_debug_extn_cfg cfg; |
| 283 | + unsigned int idx; |
| 284 | + |
| 285 | + /* Get current state */ |
| 286 | + cfg.val = get_debug_extn_cfg(); |
| 287 | + |
| 288 | + /* idx is most recently written entry */ |
| 289 | + idx = amd_brs_get_tos(&cfg); |
| 290 | + |
| 291 | + /* Poison target of entry */ |
| 292 | + wrmsrl(brs_to(idx), BRS_POISON); |
| 293 | +} |
| 294 | + |
| 295 | +/* |
| 296 | + * On context switch in, we need to make sure no samples from previous user |
| 297 | + * are left in the BRS. |
| 298 | + * |
| 299 | + * On ctxswin, sched_in = true, called after the PMU has started |
| 300 | + * On ctxswout, sched_in = false, called before the PMU is stopped |
| 301 | + */ |
| 302 | +void amd_pmu_brs_sched_task(struct perf_event_context *ctx, bool sched_in) |
| 303 | +{ |
| 304 | + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 305 | + |
| 306 | + /* no active users */ |
| 307 | + if (!cpuc->lbr_users) |
| 308 | + return; |
| 309 | + |
| 310 | + /* |
| 311 | + * On context switch in, we need to ensure we do not use entries |
| 312 | + * from previous BRS user on that CPU, so we poison the buffer as |
| 313 | + * a faster way compared to resetting all entries. |
| 314 | + */ |
| 315 | + if (sched_in) |
| 316 | + amd_brs_poison_buffer(); |
| 317 | +} |
0 commit comments