core.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. #include <linux/perf_event.h>
  2. #include <linux/export.h>
  3. #include <linux/types.h>
  4. #include <linux/init.h>
  5. #include <linux/slab.h>
  6. #include <linux/delay.h>
  7. #include <linux/jiffies.h>
  8. #include <asm/apicdef.h>
  9. #include <asm/nmi.h>
  10. #include "../perf_event.h"
  11. static DEFINE_PER_CPU(unsigned long, perf_nmi_tstamp);
  12. static unsigned long perf_nmi_window;
  13. static __initconst const u64 amd_hw_cache_event_ids
  14. [PERF_COUNT_HW_CACHE_MAX]
  15. [PERF_COUNT_HW_CACHE_OP_MAX]
  16. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  17. {
  18. [ C(L1D) ] = {
  19. [ C(OP_READ) ] = {
  20. [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
  21. [ C(RESULT_MISS) ] = 0x0141, /* Data Cache Misses */
  22. },
  23. [ C(OP_WRITE) ] = {
  24. [ C(RESULT_ACCESS) ] = 0,
  25. [ C(RESULT_MISS) ] = 0,
  26. },
  27. [ C(OP_PREFETCH) ] = {
  28. [ C(RESULT_ACCESS) ] = 0x0267, /* Data Prefetcher :attempts */
  29. [ C(RESULT_MISS) ] = 0x0167, /* Data Prefetcher :cancelled */
  30. },
  31. },
  32. [ C(L1I ) ] = {
  33. [ C(OP_READ) ] = {
  34. [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction cache fetches */
  35. [ C(RESULT_MISS) ] = 0x0081, /* Instruction cache misses */
  36. },
  37. [ C(OP_WRITE) ] = {
  38. [ C(RESULT_ACCESS) ] = -1,
  39. [ C(RESULT_MISS) ] = -1,
  40. },
  41. [ C(OP_PREFETCH) ] = {
  42. [ C(RESULT_ACCESS) ] = 0x014B, /* Prefetch Instructions :Load */
  43. [ C(RESULT_MISS) ] = 0,
  44. },
  45. },
  46. [ C(LL ) ] = {
  47. [ C(OP_READ) ] = {
  48. [ C(RESULT_ACCESS) ] = 0x037D, /* Requests to L2 Cache :IC+DC */
  49. [ C(RESULT_MISS) ] = 0x037E, /* L2 Cache Misses : IC+DC */
  50. },
  51. [ C(OP_WRITE) ] = {
  52. [ C(RESULT_ACCESS) ] = 0x017F, /* L2 Fill/Writeback */
  53. [ C(RESULT_MISS) ] = 0,
  54. },
  55. [ C(OP_PREFETCH) ] = {
  56. [ C(RESULT_ACCESS) ] = 0,
  57. [ C(RESULT_MISS) ] = 0,
  58. },
  59. },
  60. [ C(DTLB) ] = {
  61. [ C(OP_READ) ] = {
  62. [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
  63. [ C(RESULT_MISS) ] = 0x0746, /* L1_DTLB_AND_L2_DLTB_MISS.ALL */
  64. },
  65. [ C(OP_WRITE) ] = {
  66. [ C(RESULT_ACCESS) ] = 0,
  67. [ C(RESULT_MISS) ] = 0,
  68. },
  69. [ C(OP_PREFETCH) ] = {
  70. [ C(RESULT_ACCESS) ] = 0,
  71. [ C(RESULT_MISS) ] = 0,
  72. },
  73. },
  74. [ C(ITLB) ] = {
  75. [ C(OP_READ) ] = {
  76. [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction fecthes */
  77. [ C(RESULT_MISS) ] = 0x0385, /* L1_ITLB_AND_L2_ITLB_MISS.ALL */
  78. },
  79. [ C(OP_WRITE) ] = {
  80. [ C(RESULT_ACCESS) ] = -1,
  81. [ C(RESULT_MISS) ] = -1,
  82. },
  83. [ C(OP_PREFETCH) ] = {
  84. [ C(RESULT_ACCESS) ] = -1,
  85. [ C(RESULT_MISS) ] = -1,
  86. },
  87. },
  88. [ C(BPU ) ] = {
  89. [ C(OP_READ) ] = {
  90. [ C(RESULT_ACCESS) ] = 0x00c2, /* Retired Branch Instr. */
  91. [ C(RESULT_MISS) ] = 0x00c3, /* Retired Mispredicted BI */
  92. },
  93. [ C(OP_WRITE) ] = {
  94. [ C(RESULT_ACCESS) ] = -1,
  95. [ C(RESULT_MISS) ] = -1,
  96. },
  97. [ C(OP_PREFETCH) ] = {
  98. [ C(RESULT_ACCESS) ] = -1,
  99. [ C(RESULT_MISS) ] = -1,
  100. },
  101. },
  102. [ C(NODE) ] = {
  103. [ C(OP_READ) ] = {
  104. [ C(RESULT_ACCESS) ] = 0xb8e9, /* CPU Request to Memory, l+r */
  105. [ C(RESULT_MISS) ] = 0x98e9, /* CPU Request to Memory, r */
  106. },
  107. [ C(OP_WRITE) ] = {
  108. [ C(RESULT_ACCESS) ] = -1,
  109. [ C(RESULT_MISS) ] = -1,
  110. },
  111. [ C(OP_PREFETCH) ] = {
  112. [ C(RESULT_ACCESS) ] = -1,
  113. [ C(RESULT_MISS) ] = -1,
  114. },
  115. },
  116. };
  117. static __initconst const u64 amd_hw_cache_event_ids_f17h
  118. [PERF_COUNT_HW_CACHE_MAX]
  119. [PERF_COUNT_HW_CACHE_OP_MAX]
  120. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  121. [C(L1D)] = {
  122. [C(OP_READ)] = {
  123. [C(RESULT_ACCESS)] = 0x0040, /* Data Cache Accesses */
  124. [C(RESULT_MISS)] = 0xc860, /* L2$ access from DC Miss */
  125. },
  126. [C(OP_WRITE)] = {
  127. [C(RESULT_ACCESS)] = 0,
  128. [C(RESULT_MISS)] = 0,
  129. },
  130. [C(OP_PREFETCH)] = {
  131. [C(RESULT_ACCESS)] = 0xff5a, /* h/w prefetch DC Fills */
  132. [C(RESULT_MISS)] = 0,
  133. },
  134. },
  135. [C(L1I)] = {
  136. [C(OP_READ)] = {
  137. [C(RESULT_ACCESS)] = 0x0080, /* Instruction cache fetches */
  138. [C(RESULT_MISS)] = 0x0081, /* Instruction cache misses */
  139. },
  140. [C(OP_WRITE)] = {
  141. [C(RESULT_ACCESS)] = -1,
  142. [C(RESULT_MISS)] = -1,
  143. },
  144. [C(OP_PREFETCH)] = {
  145. [C(RESULT_ACCESS)] = 0,
  146. [C(RESULT_MISS)] = 0,
  147. },
  148. },
  149. [C(LL)] = {
  150. [C(OP_READ)] = {
  151. [C(RESULT_ACCESS)] = 0,
  152. [C(RESULT_MISS)] = 0,
  153. },
  154. [C(OP_WRITE)] = {
  155. [C(RESULT_ACCESS)] = 0,
  156. [C(RESULT_MISS)] = 0,
  157. },
  158. [C(OP_PREFETCH)] = {
  159. [C(RESULT_ACCESS)] = 0,
  160. [C(RESULT_MISS)] = 0,
  161. },
  162. },
  163. [C(DTLB)] = {
  164. [C(OP_READ)] = {
  165. [C(RESULT_ACCESS)] = 0xff45, /* All L2 DTLB accesses */
  166. [C(RESULT_MISS)] = 0xf045, /* L2 DTLB misses (PT walks) */
  167. },
  168. [C(OP_WRITE)] = {
  169. [C(RESULT_ACCESS)] = 0,
  170. [C(RESULT_MISS)] = 0,
  171. },
  172. [C(OP_PREFETCH)] = {
  173. [C(RESULT_ACCESS)] = 0,
  174. [C(RESULT_MISS)] = 0,
  175. },
  176. },
  177. [C(ITLB)] = {
  178. [C(OP_READ)] = {
  179. [C(RESULT_ACCESS)] = 0x0084, /* L1 ITLB misses, L2 ITLB hits */
  180. [C(RESULT_MISS)] = 0xff85, /* L1 ITLB misses, L2 misses */
  181. },
  182. [C(OP_WRITE)] = {
  183. [C(RESULT_ACCESS)] = -1,
  184. [C(RESULT_MISS)] = -1,
  185. },
  186. [C(OP_PREFETCH)] = {
  187. [C(RESULT_ACCESS)] = -1,
  188. [C(RESULT_MISS)] = -1,
  189. },
  190. },
  191. [C(BPU)] = {
  192. [C(OP_READ)] = {
  193. [C(RESULT_ACCESS)] = 0x00c2, /* Retired Branch Instr. */
  194. [C(RESULT_MISS)] = 0x00c3, /* Retired Mispredicted BI */
  195. },
  196. [C(OP_WRITE)] = {
  197. [C(RESULT_ACCESS)] = -1,
  198. [C(RESULT_MISS)] = -1,
  199. },
  200. [C(OP_PREFETCH)] = {
  201. [C(RESULT_ACCESS)] = -1,
  202. [C(RESULT_MISS)] = -1,
  203. },
  204. },
  205. [C(NODE)] = {
  206. [C(OP_READ)] = {
  207. [C(RESULT_ACCESS)] = 0,
  208. [C(RESULT_MISS)] = 0,
  209. },
  210. [C(OP_WRITE)] = {
  211. [C(RESULT_ACCESS)] = -1,
  212. [C(RESULT_MISS)] = -1,
  213. },
  214. [C(OP_PREFETCH)] = {
  215. [C(RESULT_ACCESS)] = -1,
  216. [C(RESULT_MISS)] = -1,
  217. },
  218. },
  219. };
  220. /*
  221. * AMD Performance Monitor K7 and later, up to and including Family 16h:
  222. */
  223. static const u64 amd_perfmon_event_map[PERF_COUNT_HW_MAX] =
  224. {
  225. [PERF_COUNT_HW_CPU_CYCLES] = 0x0076,
  226. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  227. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x077d,
  228. [PERF_COUNT_HW_CACHE_MISSES] = 0x077e,
  229. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c2,
  230. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c3,
  231. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x00d0, /* "Decoder empty" event */
  232. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = 0x00d1, /* "Dispatch stalls" event */
  233. };
  234. /*
  235. * AMD Performance Monitor Family 17h and later:
  236. */
  237. static const u64 amd_f17h_perfmon_event_map[PERF_COUNT_HW_MAX] =
  238. {
  239. [PERF_COUNT_HW_CPU_CYCLES] = 0x0076,
  240. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  241. [PERF_COUNT_HW_CACHE_REFERENCES] = 0xff60,
  242. [PERF_COUNT_HW_CACHE_MISSES] = 0x0964,
  243. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c2,
  244. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c3,
  245. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x0287,
  246. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = 0x0187,
  247. };
  248. static u64 amd_pmu_event_map(int hw_event)
  249. {
  250. if (boot_cpu_data.x86 >= 0x17)
  251. return amd_f17h_perfmon_event_map[hw_event];
  252. return amd_perfmon_event_map[hw_event];
  253. }
  254. /*
  255. * Previously calculated offsets
  256. */
  257. static unsigned int event_offsets[X86_PMC_IDX_MAX] __read_mostly;
  258. static unsigned int count_offsets[X86_PMC_IDX_MAX] __read_mostly;
  259. /*
  260. * Legacy CPUs:
  261. * 4 counters starting at 0xc0010000 each offset by 1
  262. *
  263. * CPUs with core performance counter extensions:
  264. * 6 counters starting at 0xc0010200 each offset by 2
  265. */
  266. static inline int amd_pmu_addr_offset(int index, bool eventsel)
  267. {
  268. int offset;
  269. if (!index)
  270. return index;
  271. if (eventsel)
  272. offset = event_offsets[index];
  273. else
  274. offset = count_offsets[index];
  275. if (offset)
  276. return offset;
  277. if (!boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
  278. offset = index;
  279. else
  280. offset = index << 1;
  281. if (eventsel)
  282. event_offsets[index] = offset;
  283. else
  284. count_offsets[index] = offset;
  285. return offset;
  286. }
  287. static int amd_core_hw_config(struct perf_event *event)
  288. {
  289. if (event->attr.exclude_host && event->attr.exclude_guest)
  290. /*
  291. * When HO == GO == 1 the hardware treats that as GO == HO == 0
  292. * and will count in both modes. We don't want to count in that
  293. * case so we emulate no-counting by setting US = OS = 0.
  294. */
  295. event->hw.config &= ~(ARCH_PERFMON_EVENTSEL_USR |
  296. ARCH_PERFMON_EVENTSEL_OS);
  297. else if (event->attr.exclude_host)
  298. event->hw.config |= AMD64_EVENTSEL_GUESTONLY;
  299. else if (event->attr.exclude_guest)
  300. event->hw.config |= AMD64_EVENTSEL_HOSTONLY;
  301. return 0;
  302. }
  303. /*
  304. * AMD64 events are detected based on their event codes.
  305. */
  306. static inline unsigned int amd_get_event_code(struct hw_perf_event *hwc)
  307. {
  308. return ((hwc->config >> 24) & 0x0f00) | (hwc->config & 0x00ff);
  309. }
  310. static inline int amd_is_nb_event(struct hw_perf_event *hwc)
  311. {
  312. return (hwc->config & 0xe0) == 0xe0;
  313. }
  314. static inline int amd_has_nb(struct cpu_hw_events *cpuc)
  315. {
  316. struct amd_nb *nb = cpuc->amd_nb;
  317. return nb && nb->nb_id != -1;
  318. }
  319. static int amd_pmu_hw_config(struct perf_event *event)
  320. {
  321. int ret;
  322. /* pass precise event sampling to ibs: */
  323. if (event->attr.precise_ip && get_ibs_caps())
  324. return -ENOENT;
  325. if (has_branch_stack(event))
  326. return -EOPNOTSUPP;
  327. ret = x86_pmu_hw_config(event);
  328. if (ret)
  329. return ret;
  330. if (event->attr.type == PERF_TYPE_RAW)
  331. event->hw.config |= event->attr.config & AMD64_RAW_EVENT_MASK;
  332. return amd_core_hw_config(event);
  333. }
  334. static void __amd_put_nb_event_constraints(struct cpu_hw_events *cpuc,
  335. struct perf_event *event)
  336. {
  337. struct amd_nb *nb = cpuc->amd_nb;
  338. int i;
  339. /*
  340. * need to scan whole list because event may not have
  341. * been assigned during scheduling
  342. *
  343. * no race condition possible because event can only
  344. * be removed on one CPU at a time AND PMU is disabled
  345. * when we come here
  346. */
  347. for (i = 0; i < x86_pmu.num_counters; i++) {
  348. if (cmpxchg(nb->owners + i, event, NULL) == event)
  349. break;
  350. }
  351. }
  352. /*
  353. * AMD64 NorthBridge events need special treatment because
  354. * counter access needs to be synchronized across all cores
  355. * of a package. Refer to BKDG section 3.12
  356. *
  357. * NB events are events measuring L3 cache, Hypertransport
  358. * traffic. They are identified by an event code >= 0xe00.
  359. * They measure events on the NorthBride which is shared
  360. * by all cores on a package. NB events are counted on a
  361. * shared set of counters. When a NB event is programmed
  362. * in a counter, the data actually comes from a shared
  363. * counter. Thus, access to those counters needs to be
  364. * synchronized.
  365. *
  366. * We implement the synchronization such that no two cores
  367. * can be measuring NB events using the same counters. Thus,
  368. * we maintain a per-NB allocation table. The available slot
  369. * is propagated using the event_constraint structure.
  370. *
  371. * We provide only one choice for each NB event based on
  372. * the fact that only NB events have restrictions. Consequently,
  373. * if a counter is available, there is a guarantee the NB event
  374. * will be assigned to it. If no slot is available, an empty
  375. * constraint is returned and scheduling will eventually fail
  376. * for this event.
  377. *
  378. * Note that all cores attached the same NB compete for the same
  379. * counters to host NB events, this is why we use atomic ops. Some
  380. * multi-chip CPUs may have more than one NB.
  381. *
  382. * Given that resources are allocated (cmpxchg), they must be
  383. * eventually freed for others to use. This is accomplished by
  384. * calling __amd_put_nb_event_constraints()
  385. *
  386. * Non NB events are not impacted by this restriction.
  387. */
  388. static struct event_constraint *
  389. __amd_get_nb_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event,
  390. struct event_constraint *c)
  391. {
  392. struct hw_perf_event *hwc = &event->hw;
  393. struct amd_nb *nb = cpuc->amd_nb;
  394. struct perf_event *old;
  395. int idx, new = -1;
  396. if (!c)
  397. c = &unconstrained;
  398. if (cpuc->is_fake)
  399. return c;
  400. /*
  401. * detect if already present, if so reuse
  402. *
  403. * cannot merge with actual allocation
  404. * because of possible holes
  405. *
  406. * event can already be present yet not assigned (in hwc->idx)
  407. * because of successive calls to x86_schedule_events() from
  408. * hw_perf_group_sched_in() without hw_perf_enable()
  409. */
  410. for_each_set_bit(idx, c->idxmsk, x86_pmu.num_counters) {
  411. if (new == -1 || hwc->idx == idx)
  412. /* assign free slot, prefer hwc->idx */
  413. old = cmpxchg(nb->owners + idx, NULL, event);
  414. else if (nb->owners[idx] == event)
  415. /* event already present */
  416. old = event;
  417. else
  418. continue;
  419. if (old && old != event)
  420. continue;
  421. /* reassign to this slot */
  422. if (new != -1)
  423. cmpxchg(nb->owners + new, event, NULL);
  424. new = idx;
  425. /* already present, reuse */
  426. if (old == event)
  427. break;
  428. }
  429. if (new == -1)
  430. return &emptyconstraint;
  431. return &nb->event_constraints[new];
  432. }
  433. static struct amd_nb *amd_alloc_nb(int cpu)
  434. {
  435. struct amd_nb *nb;
  436. int i;
  437. nb = kzalloc_node(sizeof(struct amd_nb), GFP_KERNEL, cpu_to_node(cpu));
  438. if (!nb)
  439. return NULL;
  440. nb->nb_id = -1;
  441. /*
  442. * initialize all possible NB constraints
  443. */
  444. for (i = 0; i < x86_pmu.num_counters; i++) {
  445. __set_bit(i, nb->event_constraints[i].idxmsk);
  446. nb->event_constraints[i].weight = 1;
  447. }
  448. return nb;
  449. }
  450. static int amd_pmu_cpu_prepare(int cpu)
  451. {
  452. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  453. WARN_ON_ONCE(cpuc->amd_nb);
  454. if (!x86_pmu.amd_nb_constraints)
  455. return 0;
  456. cpuc->amd_nb = amd_alloc_nb(cpu);
  457. if (!cpuc->amd_nb)
  458. return -ENOMEM;
  459. return 0;
  460. }
  461. static void amd_pmu_cpu_starting(int cpu)
  462. {
  463. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  464. void **onln = &cpuc->kfree_on_online[X86_PERF_KFREE_SHARED];
  465. struct amd_nb *nb;
  466. int i, nb_id;
  467. cpuc->perf_ctr_virt_mask = AMD64_EVENTSEL_HOSTONLY;
  468. if (!x86_pmu.amd_nb_constraints)
  469. return;
  470. nb_id = amd_get_nb_id(cpu);
  471. WARN_ON_ONCE(nb_id == BAD_APICID);
  472. for_each_online_cpu(i) {
  473. nb = per_cpu(cpu_hw_events, i).amd_nb;
  474. if (WARN_ON_ONCE(!nb))
  475. continue;
  476. if (nb->nb_id == nb_id) {
  477. *onln = cpuc->amd_nb;
  478. cpuc->amd_nb = nb;
  479. break;
  480. }
  481. }
  482. cpuc->amd_nb->nb_id = nb_id;
  483. cpuc->amd_nb->refcnt++;
  484. }
  485. static void amd_pmu_cpu_dead(int cpu)
  486. {
  487. struct cpu_hw_events *cpuhw;
  488. if (!x86_pmu.amd_nb_constraints)
  489. return;
  490. cpuhw = &per_cpu(cpu_hw_events, cpu);
  491. if (cpuhw->amd_nb) {
  492. struct amd_nb *nb = cpuhw->amd_nb;
  493. if (nb->nb_id == -1 || --nb->refcnt == 0)
  494. kfree(nb);
  495. cpuhw->amd_nb = NULL;
  496. }
  497. }
  498. /*
  499. * When a PMC counter overflows, an NMI is used to process the event and
  500. * reset the counter. NMI latency can result in the counter being updated
  501. * before the NMI can run, which can result in what appear to be spurious
  502. * NMIs. This function is intended to wait for the NMI to run and reset
  503. * the counter to avoid possible unhandled NMI messages.
  504. */
  505. #define OVERFLOW_WAIT_COUNT 50
  506. static void amd_pmu_wait_on_overflow(int idx)
  507. {
  508. unsigned int i;
  509. u64 counter;
  510. /*
  511. * Wait for the counter to be reset if it has overflowed. This loop
  512. * should exit very, very quickly, but just in case, don't wait
  513. * forever...
  514. */
  515. for (i = 0; i < OVERFLOW_WAIT_COUNT; i++) {
  516. rdmsrl(x86_pmu_event_addr(idx), counter);
  517. if (counter & (1ULL << (x86_pmu.cntval_bits - 1)))
  518. break;
  519. /* Might be in IRQ context, so can't sleep */
  520. udelay(1);
  521. }
  522. }
  523. static void amd_pmu_disable_all(void)
  524. {
  525. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  526. int idx;
  527. x86_pmu_disable_all();
  528. /*
  529. * This shouldn't be called from NMI context, but add a safeguard here
  530. * to return, since if we're in NMI context we can't wait for an NMI
  531. * to reset an overflowed counter value.
  532. */
  533. if (in_nmi())
  534. return;
  535. /*
  536. * Check each counter for overflow and wait for it to be reset by the
  537. * NMI if it has overflowed. This relies on the fact that all active
  538. * counters are always enabled when this function is caled and
  539. * ARCH_PERFMON_EVENTSEL_INT is always set.
  540. */
  541. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  542. if (!test_bit(idx, cpuc->active_mask))
  543. continue;
  544. amd_pmu_wait_on_overflow(idx);
  545. }
  546. }
  547. static void amd_pmu_disable_event(struct perf_event *event)
  548. {
  549. x86_pmu_disable_event(event);
  550. /*
  551. * This can be called from NMI context (via x86_pmu_stop). The counter
  552. * may have overflowed, but either way, we'll never see it get reset
  553. * by the NMI if we're already in the NMI. And the NMI latency support
  554. * below will take care of any pending NMI that might have been
  555. * generated by the overflow.
  556. */
  557. if (in_nmi())
  558. return;
  559. amd_pmu_wait_on_overflow(event->hw.idx);
  560. }
  561. /*
  562. * Because of NMI latency, if multiple PMC counters are active or other sources
  563. * of NMIs are received, the perf NMI handler can handle one or more overflowed
  564. * PMC counters outside of the NMI associated with the PMC overflow. If the NMI
  565. * doesn't arrive at the LAPIC in time to become a pending NMI, then the kernel
  566. * back-to-back NMI support won't be active. This PMC handler needs to take into
  567. * account that this can occur, otherwise this could result in unknown NMI
  568. * messages being issued. Examples of this is PMC overflow while in the NMI
  569. * handler when multiple PMCs are active or PMC overflow while handling some
  570. * other source of an NMI.
  571. *
  572. * Attempt to mitigate this by creating an NMI window in which un-handled NMIs
  573. * received during this window will be claimed. This prevents extending the
  574. * window past when it is possible that latent NMIs should be received. The
  575. * per-CPU perf_nmi_tstamp will be set to the window end time whenever perf has
  576. * handled a counter. When an un-handled NMI is received, it will be claimed
  577. * only if arriving within that window.
  578. */
  579. static int amd_pmu_handle_irq(struct pt_regs *regs)
  580. {
  581. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  582. int active, handled;
  583. /*
  584. * Obtain the active count before calling x86_pmu_handle_irq() since
  585. * it is possible that x86_pmu_handle_irq() may make a counter
  586. * inactive (through x86_pmu_stop).
  587. */
  588. active = __bitmap_weight(cpuc->active_mask, X86_PMC_IDX_MAX);
  589. /* Process any counter overflows */
  590. handled = x86_pmu_handle_irq(regs);
  591. /*
  592. * If a counter was handled, record a timestamp such that un-handled
  593. * NMIs will be claimed if arriving within that window.
  594. */
  595. if (handled) {
  596. this_cpu_write(perf_nmi_tstamp,
  597. jiffies + perf_nmi_window);
  598. return handled;
  599. }
  600. if (time_after(jiffies, this_cpu_read(perf_nmi_tstamp)))
  601. return NMI_DONE;
  602. return NMI_HANDLED;
  603. }
  604. static struct event_constraint *
  605. amd_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
  606. struct perf_event *event)
  607. {
  608. /*
  609. * if not NB event or no NB, then no constraints
  610. */
  611. if (!(amd_has_nb(cpuc) && amd_is_nb_event(&event->hw)))
  612. return &unconstrained;
  613. return __amd_get_nb_event_constraints(cpuc, event, NULL);
  614. }
  615. static void amd_put_event_constraints(struct cpu_hw_events *cpuc,
  616. struct perf_event *event)
  617. {
  618. if (amd_has_nb(cpuc) && amd_is_nb_event(&event->hw))
  619. __amd_put_nb_event_constraints(cpuc, event);
  620. }
  621. PMU_FORMAT_ATTR(event, "config:0-7,32-35");
  622. PMU_FORMAT_ATTR(umask, "config:8-15" );
  623. PMU_FORMAT_ATTR(edge, "config:18" );
  624. PMU_FORMAT_ATTR(inv, "config:23" );
  625. PMU_FORMAT_ATTR(cmask, "config:24-31" );
  626. static struct attribute *amd_format_attr[] = {
  627. &format_attr_event.attr,
  628. &format_attr_umask.attr,
  629. &format_attr_edge.attr,
  630. &format_attr_inv.attr,
  631. &format_attr_cmask.attr,
  632. NULL,
  633. };
  634. /* AMD Family 15h */
  635. #define AMD_EVENT_TYPE_MASK 0x000000F0ULL
  636. #define AMD_EVENT_FP 0x00000000ULL ... 0x00000010ULL
  637. #define AMD_EVENT_LS 0x00000020ULL ... 0x00000030ULL
  638. #define AMD_EVENT_DC 0x00000040ULL ... 0x00000050ULL
  639. #define AMD_EVENT_CU 0x00000060ULL ... 0x00000070ULL
  640. #define AMD_EVENT_IC_DE 0x00000080ULL ... 0x00000090ULL
  641. #define AMD_EVENT_EX_LS 0x000000C0ULL
  642. #define AMD_EVENT_DE 0x000000D0ULL
  643. #define AMD_EVENT_NB 0x000000E0ULL ... 0x000000F0ULL
  644. /*
  645. * AMD family 15h event code/PMC mappings:
  646. *
  647. * type = event_code & 0x0F0:
  648. *
  649. * 0x000 FP PERF_CTL[5:3]
  650. * 0x010 FP PERF_CTL[5:3]
  651. * 0x020 LS PERF_CTL[5:0]
  652. * 0x030 LS PERF_CTL[5:0]
  653. * 0x040 DC PERF_CTL[5:0]
  654. * 0x050 DC PERF_CTL[5:0]
  655. * 0x060 CU PERF_CTL[2:0]
  656. * 0x070 CU PERF_CTL[2:0]
  657. * 0x080 IC/DE PERF_CTL[2:0]
  658. * 0x090 IC/DE PERF_CTL[2:0]
  659. * 0x0A0 ---
  660. * 0x0B0 ---
  661. * 0x0C0 EX/LS PERF_CTL[5:0]
  662. * 0x0D0 DE PERF_CTL[2:0]
  663. * 0x0E0 NB NB_PERF_CTL[3:0]
  664. * 0x0F0 NB NB_PERF_CTL[3:0]
  665. *
  666. * Exceptions:
  667. *
  668. * 0x000 FP PERF_CTL[3], PERF_CTL[5:3] (*)
  669. * 0x003 FP PERF_CTL[3]
  670. * 0x004 FP PERF_CTL[3], PERF_CTL[5:3] (*)
  671. * 0x00B FP PERF_CTL[3]
  672. * 0x00D FP PERF_CTL[3]
  673. * 0x023 DE PERF_CTL[2:0]
  674. * 0x02D LS PERF_CTL[3]
  675. * 0x02E LS PERF_CTL[3,0]
  676. * 0x031 LS PERF_CTL[2:0] (**)
  677. * 0x043 CU PERF_CTL[2:0]
  678. * 0x045 CU PERF_CTL[2:0]
  679. * 0x046 CU PERF_CTL[2:0]
  680. * 0x054 CU PERF_CTL[2:0]
  681. * 0x055 CU PERF_CTL[2:0]
  682. * 0x08F IC PERF_CTL[0]
  683. * 0x187 DE PERF_CTL[0]
  684. * 0x188 DE PERF_CTL[0]
  685. * 0x0DB EX PERF_CTL[5:0]
  686. * 0x0DC LS PERF_CTL[5:0]
  687. * 0x0DD LS PERF_CTL[5:0]
  688. * 0x0DE LS PERF_CTL[5:0]
  689. * 0x0DF LS PERF_CTL[5:0]
  690. * 0x1C0 EX PERF_CTL[5:3]
  691. * 0x1D6 EX PERF_CTL[5:0]
  692. * 0x1D8 EX PERF_CTL[5:0]
  693. *
  694. * (*) depending on the umask all FPU counters may be used
  695. * (**) only one unitmask enabled at a time
  696. */
  697. static struct event_constraint amd_f15_PMC0 = EVENT_CONSTRAINT(0, 0x01, 0);
  698. static struct event_constraint amd_f15_PMC20 = EVENT_CONSTRAINT(0, 0x07, 0);
  699. static struct event_constraint amd_f15_PMC3 = EVENT_CONSTRAINT(0, 0x08, 0);
  700. static struct event_constraint amd_f15_PMC30 = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
  701. static struct event_constraint amd_f15_PMC50 = EVENT_CONSTRAINT(0, 0x3F, 0);
  702. static struct event_constraint amd_f15_PMC53 = EVENT_CONSTRAINT(0, 0x38, 0);
  703. static struct event_constraint *
  704. amd_get_event_constraints_f15h(struct cpu_hw_events *cpuc, int idx,
  705. struct perf_event *event)
  706. {
  707. struct hw_perf_event *hwc = &event->hw;
  708. unsigned int event_code = amd_get_event_code(hwc);
  709. switch (event_code & AMD_EVENT_TYPE_MASK) {
  710. case AMD_EVENT_FP:
  711. switch (event_code) {
  712. case 0x000:
  713. if (!(hwc->config & 0x0000F000ULL))
  714. break;
  715. if (!(hwc->config & 0x00000F00ULL))
  716. break;
  717. return &amd_f15_PMC3;
  718. case 0x004:
  719. if (hweight_long(hwc->config & ARCH_PERFMON_EVENTSEL_UMASK) <= 1)
  720. break;
  721. return &amd_f15_PMC3;
  722. case 0x003:
  723. case 0x00B:
  724. case 0x00D:
  725. return &amd_f15_PMC3;
  726. }
  727. return &amd_f15_PMC53;
  728. case AMD_EVENT_LS:
  729. case AMD_EVENT_DC:
  730. case AMD_EVENT_EX_LS:
  731. switch (event_code) {
  732. case 0x023:
  733. case 0x043:
  734. case 0x045:
  735. case 0x046:
  736. case 0x054:
  737. case 0x055:
  738. return &amd_f15_PMC20;
  739. case 0x02D:
  740. return &amd_f15_PMC3;
  741. case 0x02E:
  742. return &amd_f15_PMC30;
  743. case 0x031:
  744. if (hweight_long(hwc->config & ARCH_PERFMON_EVENTSEL_UMASK) <= 1)
  745. return &amd_f15_PMC20;
  746. return &emptyconstraint;
  747. case 0x1C0:
  748. return &amd_f15_PMC53;
  749. default:
  750. return &amd_f15_PMC50;
  751. }
  752. case AMD_EVENT_CU:
  753. case AMD_EVENT_IC_DE:
  754. case AMD_EVENT_DE:
  755. switch (event_code) {
  756. case 0x08F:
  757. case 0x187:
  758. case 0x188:
  759. return &amd_f15_PMC0;
  760. case 0x0DB ... 0x0DF:
  761. case 0x1D6:
  762. case 0x1D8:
  763. return &amd_f15_PMC50;
  764. default:
  765. return &amd_f15_PMC20;
  766. }
  767. case AMD_EVENT_NB:
  768. /* moved to uncore.c */
  769. return &emptyconstraint;
  770. default:
  771. return &emptyconstraint;
  772. }
  773. }
  774. static ssize_t amd_event_sysfs_show(char *page, u64 config)
  775. {
  776. u64 event = (config & ARCH_PERFMON_EVENTSEL_EVENT) |
  777. (config & AMD64_EVENTSEL_EVENT) >> 24;
  778. return x86_event_sysfs_show(page, config, event);
  779. }
  780. static __initconst const struct x86_pmu amd_pmu = {
  781. .name = "AMD",
  782. .handle_irq = amd_pmu_handle_irq,
  783. .disable_all = amd_pmu_disable_all,
  784. .enable_all = x86_pmu_enable_all,
  785. .enable = x86_pmu_enable_event,
  786. .disable = amd_pmu_disable_event,
  787. .hw_config = amd_pmu_hw_config,
  788. .schedule_events = x86_schedule_events,
  789. .eventsel = MSR_K7_EVNTSEL0,
  790. .perfctr = MSR_K7_PERFCTR0,
  791. .addr_offset = amd_pmu_addr_offset,
  792. .event_map = amd_pmu_event_map,
  793. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  794. .num_counters = AMD64_NUM_COUNTERS,
  795. .cntval_bits = 48,
  796. .cntval_mask = (1ULL << 48) - 1,
  797. .apic = 1,
  798. /* use highest bit to detect overflow */
  799. .max_period = (1ULL << 47) - 1,
  800. .get_event_constraints = amd_get_event_constraints,
  801. .put_event_constraints = amd_put_event_constraints,
  802. .format_attrs = amd_format_attr,
  803. .events_sysfs_show = amd_event_sysfs_show,
  804. .cpu_prepare = amd_pmu_cpu_prepare,
  805. .cpu_starting = amd_pmu_cpu_starting,
  806. .cpu_dead = amd_pmu_cpu_dead,
  807. .amd_nb_constraints = 1,
  808. };
  809. static int __init amd_core_pmu_init(void)
  810. {
  811. if (!boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
  812. return 0;
  813. /* Avoid calulating the value each time in the NMI handler */
  814. perf_nmi_window = msecs_to_jiffies(100);
  815. switch (boot_cpu_data.x86) {
  816. case 0x15:
  817. pr_cont("Fam15h ");
  818. x86_pmu.get_event_constraints = amd_get_event_constraints_f15h;
  819. break;
  820. case 0x17:
  821. pr_cont("Fam17h ");
  822. /*
  823. * In family 17h, there are no event constraints in the PMC hardware.
  824. * We fallback to using default amd_get_event_constraints.
  825. */
  826. break;
  827. default:
  828. pr_err("core perfctr but no constraints; unknown hardware!\n");
  829. return -ENODEV;
  830. }
  831. /*
  832. * If core performance counter extensions exists, we must use
  833. * MSR_F15H_PERF_CTL/MSR_F15H_PERF_CTR msrs. See also
  834. * amd_pmu_addr_offset().
  835. */
  836. x86_pmu.eventsel = MSR_F15H_PERF_CTL;
  837. x86_pmu.perfctr = MSR_F15H_PERF_CTR;
  838. x86_pmu.num_counters = AMD64_NUM_COUNTERS_CORE;
  839. /*
  840. * AMD Core perfctr has separate MSRs for the NB events, see
  841. * the amd/uncore.c driver.
  842. */
  843. x86_pmu.amd_nb_constraints = 0;
  844. pr_cont("core perfctr, ");
  845. return 0;
  846. }
  847. __init int amd_pmu_init(void)
  848. {
  849. int ret;
  850. /* Performance-monitoring supported from K7 and later: */
  851. if (boot_cpu_data.x86 < 6)
  852. return -ENODEV;
  853. x86_pmu = amd_pmu;
  854. ret = amd_core_pmu_init();
  855. if (ret)
  856. return ret;
  857. if (num_possible_cpus() == 1) {
  858. /*
  859. * No point in allocating data structures to serialize
  860. * against other CPUs, when there is only the one CPU.
  861. */
  862. x86_pmu.amd_nb_constraints = 0;
  863. }
  864. if (boot_cpu_data.x86 >= 0x17)
  865. memcpy(hw_cache_event_ids, amd_hw_cache_event_ids_f17h, sizeof(hw_cache_event_ids));
  866. else
  867. memcpy(hw_cache_event_ids, amd_hw_cache_event_ids, sizeof(hw_cache_event_ids));
  868. return 0;
  869. }
  870. void amd_pmu_enable_virt(void)
  871. {
  872. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  873. cpuc->perf_ctr_virt_mask = 0;
  874. /* Reload all events */
  875. amd_pmu_disable_all();
  876. x86_pmu_enable_all(0);
  877. }
  878. EXPORT_SYMBOL_GPL(amd_pmu_enable_virt);
  879. void amd_pmu_disable_virt(void)
  880. {
  881. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  882. /*
  883. * We only mask out the Host-only bit so that host-only counting works
  884. * when SVM is disabled. If someone sets up a guest-only counter when
  885. * SVM is disabled the Guest-only bits still gets set and the counter
  886. * will not count anything.
  887. */
  888. cpuc->perf_ctr_virt_mask = AMD64_EVENTSEL_HOSTONLY;
  889. /* Reload all events */
  890. amd_pmu_disable_all();
  891. x86_pmu_enable_all(0);
  892. }
  893. EXPORT_SYMBOL_GPL(amd_pmu_disable_virt);