brs.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 <linux/jump_label.h>
  11. #include <asm/msr.h>
  12. #include <asm/cpufeature.h>
  13. #include "../perf_event.h"
  14. #define BRS_POISON 0xFFFFFFFFFFFFFFFEULL /* mark limit of valid entries */
  15. /* Debug Extension Configuration register layout */
  16. union amd_debug_extn_cfg {
  17. __u64 val;
  18. struct {
  19. __u64 rsvd0:2, /* reserved */
  20. brsmen:1, /* branch sample enable */
  21. rsvd4_3:2,/* reserved - must be 0x3 */
  22. vb:1, /* valid branches recorded */
  23. rsvd2:10, /* reserved */
  24. msroff:4, /* index of next entry to write */
  25. rsvd3:4, /* reserved */
  26. pmc:3, /* #PMC holding the sampling event */
  27. rsvd4:37; /* reserved */
  28. };
  29. };
  30. static inline unsigned int brs_from(int idx)
  31. {
  32. return MSR_AMD_SAMP_BR_FROM + 2 * idx;
  33. }
  34. static inline unsigned int brs_to(int idx)
  35. {
  36. return MSR_AMD_SAMP_BR_FROM + 2 * idx + 1;
  37. }
  38. static inline void set_debug_extn_cfg(u64 val)
  39. {
  40. /* bits[4:3] must always be set to 11b */
  41. wrmsrl(MSR_AMD_DBG_EXTN_CFG, val | 3ULL << 3);
  42. }
  43. static inline u64 get_debug_extn_cfg(void)
  44. {
  45. u64 val;
  46. rdmsrl(MSR_AMD_DBG_EXTN_CFG, val);
  47. return val;
  48. }
  49. static bool __init amd_brs_detect(void)
  50. {
  51. if (!cpu_feature_enabled(X86_FEATURE_BRS))
  52. return false;
  53. switch (boot_cpu_data.x86) {
  54. case 0x19: /* AMD Fam19h (Zen3) */
  55. x86_pmu.lbr_nr = 16;
  56. /* No hardware filtering supported */
  57. x86_pmu.lbr_sel_map = NULL;
  58. x86_pmu.lbr_sel_mask = 0;
  59. break;
  60. default:
  61. return false;
  62. }
  63. return true;
  64. }
  65. /*
  66. * Current BRS implementation does not support branch type or privilege level
  67. * filtering. Therefore, this function simply enforces these limitations. No need for
  68. * a br_sel_map. Software filtering is not supported because it would not correlate well
  69. * with a sampling period.
  70. */
  71. static int amd_brs_setup_filter(struct perf_event *event)
  72. {
  73. u64 type = event->attr.branch_sample_type;
  74. /* No BRS support */
  75. if (!x86_pmu.lbr_nr)
  76. return -EOPNOTSUPP;
  77. /* Can only capture all branches, i.e., no filtering */
  78. if ((type & ~PERF_SAMPLE_BRANCH_PLM_ALL) != PERF_SAMPLE_BRANCH_ANY)
  79. return -EINVAL;
  80. return 0;
  81. }
  82. static inline int amd_is_brs_event(struct perf_event *e)
  83. {
  84. return (e->hw.config & AMD64_RAW_EVENT_MASK) == AMD_FAM19H_BRS_EVENT;
  85. }
  86. int amd_brs_hw_config(struct perf_event *event)
  87. {
  88. int ret = 0;
  89. /*
  90. * Due to interrupt holding, BRS is not recommended in
  91. * counting mode.
  92. */
  93. if (!is_sampling_event(event))
  94. return -EINVAL;
  95. /*
  96. * Due to the way BRS operates by holding the interrupt until
  97. * lbr_nr entries have been captured, it does not make sense
  98. * to allow sampling on BRS with an event that does not match
  99. * what BRS is capturing, i.e., retired taken branches.
  100. * Otherwise the correlation with the event's period is even
  101. * more loose:
  102. *
  103. * With retired taken branch:
  104. * Effective P = P + 16 + X
  105. * With any other event:
  106. * Effective P = P + Y + X
  107. *
  108. * Where X is the number of taken branches due to interrupt
  109. * skid. Skid is large.
  110. *
  111. * Where Y is the occurences of the event while BRS is
  112. * capturing the lbr_nr entries.
  113. *
  114. * By using retired taken branches, we limit the impact on the
  115. * Y variable. We know it cannot be more than the depth of
  116. * BRS.
  117. */
  118. if (!amd_is_brs_event(event))
  119. return -EINVAL;
  120. /*
  121. * BRS implementation does not work with frequency mode
  122. * reprogramming of the period.
  123. */
  124. if (event->attr.freq)
  125. return -EINVAL;
  126. /*
  127. * The kernel subtracts BRS depth from period, so it must
  128. * be big enough.
  129. */
  130. if (event->attr.sample_period <= x86_pmu.lbr_nr)
  131. return -EINVAL;
  132. /*
  133. * Check if we can allow PERF_SAMPLE_BRANCH_STACK
  134. */
  135. ret = amd_brs_setup_filter(event);
  136. /* only set in case of success */
  137. if (!ret)
  138. event->hw.flags |= PERF_X86_EVENT_AMD_BRS;
  139. return ret;
  140. }
  141. /* tos = top of stack, i.e., last valid entry written */
  142. static inline int amd_brs_get_tos(union amd_debug_extn_cfg *cfg)
  143. {
  144. /*
  145. * msroff: index of next entry to write so top-of-stack is one off
  146. * if BRS is full then msroff is set back to 0.
  147. */
  148. return (cfg->msroff ? cfg->msroff : x86_pmu.lbr_nr) - 1;
  149. }
  150. /*
  151. * make sure we have a sane BRS offset to begin with
  152. * especially with kexec
  153. */
  154. void amd_brs_reset(void)
  155. {
  156. if (!cpu_feature_enabled(X86_FEATURE_BRS))
  157. return;
  158. /*
  159. * Reset config
  160. */
  161. set_debug_extn_cfg(0);
  162. /*
  163. * Mark first entry as poisoned
  164. */
  165. wrmsrl(brs_to(0), BRS_POISON);
  166. }
  167. int __init amd_brs_init(void)
  168. {
  169. if (!amd_brs_detect())
  170. return -EOPNOTSUPP;
  171. pr_cont("%d-deep BRS, ", x86_pmu.lbr_nr);
  172. return 0;
  173. }
  174. void amd_brs_enable(void)
  175. {
  176. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  177. union amd_debug_extn_cfg cfg;
  178. /* Activate only on first user */
  179. if (++cpuc->brs_active > 1)
  180. return;
  181. cfg.val = 0; /* reset all fields */
  182. cfg.brsmen = 1; /* enable branch sampling */
  183. /* Set enable bit */
  184. set_debug_extn_cfg(cfg.val);
  185. }
  186. void amd_brs_enable_all(void)
  187. {
  188. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  189. if (cpuc->lbr_users)
  190. amd_brs_enable();
  191. }
  192. void amd_brs_disable(void)
  193. {
  194. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  195. union amd_debug_extn_cfg cfg;
  196. /* Check if active (could be disabled via x86_pmu_disable_all()) */
  197. if (!cpuc->brs_active)
  198. return;
  199. /* Only disable for last user */
  200. if (--cpuc->brs_active)
  201. return;
  202. /*
  203. * Clear the brsmen bit but preserve the others as they contain
  204. * useful state such as vb and msroff
  205. */
  206. cfg.val = get_debug_extn_cfg();
  207. /*
  208. * When coming in on interrupt and BRS is full, then hw will have
  209. * already stopped BRS, no need to issue wrmsr again
  210. */
  211. if (cfg.brsmen) {
  212. cfg.brsmen = 0;
  213. set_debug_extn_cfg(cfg.val);
  214. }
  215. }
  216. void amd_brs_disable_all(void)
  217. {
  218. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  219. if (cpuc->lbr_users)
  220. amd_brs_disable();
  221. }
  222. static bool amd_brs_match_plm(struct perf_event *event, u64 to)
  223. {
  224. int type = event->attr.branch_sample_type;
  225. int plm_k = PERF_SAMPLE_BRANCH_KERNEL | PERF_SAMPLE_BRANCH_HV;
  226. int plm_u = PERF_SAMPLE_BRANCH_USER;
  227. if (!(type & plm_k) && kernel_ip(to))
  228. return 0;
  229. if (!(type & plm_u) && !kernel_ip(to))
  230. return 0;
  231. return 1;
  232. }
  233. /*
  234. * Caller must ensure amd_brs_inuse() is true before calling
  235. * return:
  236. */
  237. void amd_brs_drain(void)
  238. {
  239. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  240. struct perf_event *event = cpuc->events[0];
  241. struct perf_branch_entry *br = cpuc->lbr_entries;
  242. union amd_debug_extn_cfg cfg;
  243. u32 i, nr = 0, num, tos, start;
  244. u32 shift = 64 - boot_cpu_data.x86_virt_bits;
  245. /*
  246. * BRS event forced on PMC0,
  247. * so check if there is an event.
  248. * It is possible to have lbr_users > 0 but the event
  249. * not yet scheduled due to long latency PMU irq
  250. */
  251. if (!event)
  252. goto empty;
  253. cfg.val = get_debug_extn_cfg();
  254. /* Sanity check [0-x86_pmu.lbr_nr] */
  255. if (WARN_ON_ONCE(cfg.msroff >= x86_pmu.lbr_nr))
  256. goto empty;
  257. /* No valid branch */
  258. if (cfg.vb == 0)
  259. goto empty;
  260. /*
  261. * msr.off points to next entry to be written
  262. * tos = most recent entry index = msr.off - 1
  263. * BRS register buffer saturates, so we know we have
  264. * start < tos and that we have to read from start to tos
  265. */
  266. start = 0;
  267. tos = amd_brs_get_tos(&cfg);
  268. num = tos - start + 1;
  269. /*
  270. * BRS is only one pass (saturation) from MSROFF to depth-1
  271. * MSROFF wraps to zero when buffer is full
  272. */
  273. for (i = 0; i < num; i++) {
  274. u32 brs_idx = tos - i;
  275. u64 from, to;
  276. rdmsrl(brs_to(brs_idx), to);
  277. /* Entry does not belong to us (as marked by kernel) */
  278. if (to == BRS_POISON)
  279. break;
  280. /*
  281. * Sign-extend SAMP_BR_TO to 64 bits, bits 61-63 are reserved.
  282. * Necessary to generate proper virtual addresses suitable for
  283. * symbolization
  284. */
  285. to = (u64)(((s64)to << shift) >> shift);
  286. if (!amd_brs_match_plm(event, to))
  287. continue;
  288. rdmsrl(brs_from(brs_idx), from);
  289. perf_clear_branch_entry_bitfields(br+nr);
  290. br[nr].from = from;
  291. br[nr].to = to;
  292. nr++;
  293. }
  294. empty:
  295. /* Record number of sampled branches */
  296. cpuc->lbr_stack.nr = nr;
  297. }
  298. /*
  299. * Poison most recent entry to prevent reuse by next task
  300. * required because BRS entry are not tagged by PID
  301. */
  302. static void amd_brs_poison_buffer(void)
  303. {
  304. union amd_debug_extn_cfg cfg;
  305. unsigned int idx;
  306. /* Get current state */
  307. cfg.val = get_debug_extn_cfg();
  308. /* idx is most recently written entry */
  309. idx = amd_brs_get_tos(&cfg);
  310. /* Poison target of entry */
  311. wrmsrl(brs_to(idx), BRS_POISON);
  312. }
  313. /*
  314. * On context switch in, we need to make sure no samples from previous user
  315. * are left in the BRS.
  316. *
  317. * On ctxswin, sched_in = true, called after the PMU has started
  318. * On ctxswout, sched_in = false, called before the PMU is stopped
  319. */
  320. void amd_pmu_brs_sched_task(struct perf_event_context *ctx, bool sched_in)
  321. {
  322. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  323. /* no active users */
  324. if (!cpuc->lbr_users)
  325. return;
  326. /*
  327. * On context switch in, we need to ensure we do not use entries
  328. * from previous BRS user on that CPU, so we poison the buffer as
  329. * a faster way compared to resetting all entries.
  330. */
  331. if (sched_in)
  332. amd_brs_poison_buffer();
  333. }
  334. /*
  335. * called from ACPI processor_idle.c or acpi_pad.c
  336. * with interrupts disabled
  337. */
  338. void perf_amd_brs_lopwr_cb(bool lopwr_in)
  339. {
  340. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  341. union amd_debug_extn_cfg cfg;
  342. /*
  343. * on mwait in, we may end up in non C0 state.
  344. * we must disable branch sampling to avoid holding the NMI
  345. * for too long. We disable it in hardware but we
  346. * keep the state in cpuc, so we can re-enable.
  347. *
  348. * The hardware will deliver the NMI if needed when brsmen cleared
  349. */
  350. if (cpuc->brs_active) {
  351. cfg.val = get_debug_extn_cfg();
  352. cfg.brsmen = !lopwr_in;
  353. set_debug_extn_cfg(cfg.val);
  354. }
  355. }
  356. DEFINE_STATIC_CALL_NULL(perf_lopwr_cb, perf_amd_brs_lopwr_cb);
  357. EXPORT_STATIC_CALL_TRAMP_GPL(perf_lopwr_cb);
  358. void __init amd_brs_lopwr_init(void)
  359. {
  360. static_call_update(perf_lopwr_cb, perf_amd_brs_lopwr_cb);
  361. }