core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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 <asm/apicdef.h>
  7. #include "../perf_event.h"
  8. static __initconst const u64 amd_hw_cache_event_ids
  9. [PERF_COUNT_HW_CACHE_MAX]
  10. [PERF_COUNT_HW_CACHE_OP_MAX]
  11. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  12. {
  13. [ C(L1D) ] = {
  14. [ C(OP_READ) ] = {
  15. [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
  16. [ C(RESULT_MISS) ] = 0x0141, /* Data Cache Misses */
  17. },
  18. [ C(OP_WRITE) ] = {
  19. [ C(RESULT_ACCESS) ] = 0,
  20. [ C(RESULT_MISS) ] = 0,
  21. },
  22. [ C(OP_PREFETCH) ] = {
  23. [ C(RESULT_ACCESS) ] = 0x0267, /* Data Prefetcher :attempts */
  24. [ C(RESULT_MISS) ] = 0x0167, /* Data Prefetcher :cancelled */
  25. },
  26. },
  27. [ C(L1I ) ] = {
  28. [ C(OP_READ) ] = {
  29. [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction cache fetches */
  30. [ C(RESULT_MISS) ] = 0x0081, /* Instruction cache misses */
  31. },
  32. [ C(OP_WRITE) ] = {
  33. [ C(RESULT_ACCESS) ] = -1,
  34. [ C(RESULT_MISS) ] = -1,
  35. },
  36. [ C(OP_PREFETCH) ] = {
  37. [ C(RESULT_ACCESS) ] = 0x014B, /* Prefetch Instructions :Load */
  38. [ C(RESULT_MISS) ] = 0,
  39. },
  40. },
  41. [ C(LL ) ] = {
  42. [ C(OP_READ) ] = {
  43. [ C(RESULT_ACCESS) ] = 0x037D, /* Requests to L2 Cache :IC+DC */
  44. [ C(RESULT_MISS) ] = 0x037E, /* L2 Cache Misses : IC+DC */
  45. },
  46. [ C(OP_WRITE) ] = {
  47. [ C(RESULT_ACCESS) ] = 0x017F, /* L2 Fill/Writeback */
  48. [ C(RESULT_MISS) ] = 0,
  49. },
  50. [ C(OP_PREFETCH) ] = {
  51. [ C(RESULT_ACCESS) ] = 0,
  52. [ C(RESULT_MISS) ] = 0,
  53. },
  54. },
  55. [ C(DTLB) ] = {
  56. [ C(OP_READ) ] = {
  57. [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
  58. [ C(RESULT_MISS) ] = 0x0746, /* L1_DTLB_AND_L2_DLTB_MISS.ALL */
  59. },
  60. [ C(OP_WRITE) ] = {
  61. [ C(RESULT_ACCESS) ] = 0,
  62. [ C(RESULT_MISS) ] = 0,
  63. },
  64. [ C(OP_PREFETCH) ] = {
  65. [ C(RESULT_ACCESS) ] = 0,
  66. [ C(RESULT_MISS) ] = 0,
  67. },
  68. },
  69. [ C(ITLB) ] = {
  70. [ C(OP_READ) ] = {
  71. [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction fecthes */
  72. [ C(RESULT_MISS) ] = 0x0385, /* L1_ITLB_AND_L2_ITLB_MISS.ALL */
  73. },
  74. [ C(OP_WRITE) ] = {
  75. [ C(RESULT_ACCESS) ] = -1,
  76. [ C(RESULT_MISS) ] = -1,
  77. },
  78. [ C(OP_PREFETCH) ] = {
  79. [ C(RESULT_ACCESS) ] = -1,
  80. [ C(RESULT_MISS) ] = -1,
  81. },
  82. },
  83. [ C(BPU ) ] = {
  84. [ C(OP_READ) ] = {
  85. [ C(RESULT_ACCESS) ] = 0x00c2, /* Retired Branch Instr. */
  86. [ C(RESULT_MISS) ] = 0x00c3, /* Retired Mispredicted BI */
  87. },
  88. [ C(OP_WRITE) ] = {
  89. [ C(RESULT_ACCESS) ] = -1,
  90. [ C(RESULT_MISS) ] = -1,
  91. },
  92. [ C(OP_PREFETCH) ] = {
  93. [ C(RESULT_ACCESS) ] = -1,
  94. [ C(RESULT_MISS) ] = -1,
  95. },
  96. },
  97. [ C(NODE) ] = {
  98. [ C(OP_READ) ] = {
  99. [ C(RESULT_ACCESS) ] = 0xb8e9, /* CPU Request to Memory, l+r */
  100. [ C(RESULT_MISS) ] = 0x98e9, /* CPU Request to Memory, r */
  101. },
  102. [ C(OP_WRITE) ] = {
  103. [ C(RESULT_ACCESS) ] = -1,
  104. [ C(RESULT_MISS) ] = -1,
  105. },
  106. [ C(OP_PREFETCH) ] = {
  107. [ C(RESULT_ACCESS) ] = -1,
  108. [ C(RESULT_MISS) ] = -1,
  109. },
  110. },
  111. };
  112. /*
  113. * AMD Performance Monitor K7 and later.
  114. */
  115. static const u64 amd_perfmon_event_map[PERF_COUNT_HW_MAX] =
  116. {
  117. [PERF_COUNT_HW_CPU_CYCLES] = 0x0076,
  118. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  119. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x077d,
  120. [PERF_COUNT_HW_CACHE_MISSES] = 0x077e,
  121. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c2,
  122. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c3,
  123. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x00d0, /* "Decoder empty" event */
  124. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = 0x00d1, /* "Dispatch stalls" event */
  125. };
  126. static u64 amd_pmu_event_map(int hw_event)
  127. {
  128. return amd_perfmon_event_map[hw_event];
  129. }
  130. /*
  131. * Previously calculated offsets
  132. */
  133. static unsigned int event_offsets[X86_PMC_IDX_MAX] __read_mostly;
  134. static unsigned int count_offsets[X86_PMC_IDX_MAX] __read_mostly;
  135. /*
  136. * Legacy CPUs:
  137. * 4 counters starting at 0xc0010000 each offset by 1
  138. *
  139. * CPUs with core performance counter extensions:
  140. * 6 counters starting at 0xc0010200 each offset by 2
  141. */
  142. static inline int amd_pmu_addr_offset(int index, bool eventsel)
  143. {
  144. int offset;
  145. if (!index)
  146. return index;
  147. if (eventsel)
  148. offset = event_offsets[index];
  149. else
  150. offset = count_offsets[index];
  151. if (offset)
  152. return offset;
  153. if (!boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
  154. offset = index;
  155. else
  156. offset = index << 1;
  157. if (eventsel)
  158. event_offsets[index] = offset;
  159. else
  160. count_offsets[index] = offset;
  161. return offset;
  162. }
  163. static int amd_core_hw_config(struct perf_event *event)
  164. {
  165. if (event->attr.exclude_host && event->attr.exclude_guest)
  166. /*
  167. * When HO == GO == 1 the hardware treats that as GO == HO == 0
  168. * and will count in both modes. We don't want to count in that
  169. * case so we emulate no-counting by setting US = OS = 0.
  170. */
  171. event->hw.config &= ~(ARCH_PERFMON_EVENTSEL_USR |
  172. ARCH_PERFMON_EVENTSEL_OS);
  173. else if (event->attr.exclude_host)
  174. event->hw.config |= AMD64_EVENTSEL_GUESTONLY;
  175. else if (event->attr.exclude_guest)
  176. event->hw.config |= AMD64_EVENTSEL_HOSTONLY;
  177. return 0;
  178. }
  179. /*
  180. * AMD64 events are detected based on their event codes.
  181. */
  182. static inline unsigned int amd_get_event_code(struct hw_perf_event *hwc)
  183. {
  184. return ((hwc->config >> 24) & 0x0f00) | (hwc->config & 0x00ff);
  185. }
  186. static inline int amd_is_nb_event(struct hw_perf_event *hwc)
  187. {
  188. return (hwc->config & 0xe0) == 0xe0;
  189. }
  190. static inline int amd_has_nb(struct cpu_hw_events *cpuc)
  191. {
  192. struct amd_nb *nb = cpuc->amd_nb;
  193. return nb && nb->nb_id != -1;
  194. }
  195. static int amd_pmu_hw_config(struct perf_event *event)
  196. {
  197. int ret;
  198. /* pass precise event sampling to ibs: */
  199. if (event->attr.precise_ip && get_ibs_caps())
  200. return -ENOENT;
  201. if (has_branch_stack(event))
  202. return -EOPNOTSUPP;
  203. ret = x86_pmu_hw_config(event);
  204. if (ret)
  205. return ret;
  206. if (event->attr.type == PERF_TYPE_RAW)
  207. event->hw.config |= event->attr.config & AMD64_RAW_EVENT_MASK;
  208. return amd_core_hw_config(event);
  209. }
  210. static void __amd_put_nb_event_constraints(struct cpu_hw_events *cpuc,
  211. struct perf_event *event)
  212. {
  213. struct amd_nb *nb = cpuc->amd_nb;
  214. int i;
  215. /*
  216. * need to scan whole list because event may not have
  217. * been assigned during scheduling
  218. *
  219. * no race condition possible because event can only
  220. * be removed on one CPU at a time AND PMU is disabled
  221. * when we come here
  222. */
  223. for (i = 0; i < x86_pmu.num_counters; i++) {
  224. if (cmpxchg(nb->owners + i, event, NULL) == event)
  225. break;
  226. }
  227. }
  228. /*
  229. * AMD64 NorthBridge events need special treatment because
  230. * counter access needs to be synchronized across all cores
  231. * of a package. Refer to BKDG section 3.12
  232. *
  233. * NB events are events measuring L3 cache, Hypertransport
  234. * traffic. They are identified by an event code >= 0xe00.
  235. * They measure events on the NorthBride which is shared
  236. * by all cores on a package. NB events are counted on a
  237. * shared set of counters. When a NB event is programmed
  238. * in a counter, the data actually comes from a shared
  239. * counter. Thus, access to those counters needs to be
  240. * synchronized.
  241. *
  242. * We implement the synchronization such that no two cores
  243. * can be measuring NB events using the same counters. Thus,
  244. * we maintain a per-NB allocation table. The available slot
  245. * is propagated using the event_constraint structure.
  246. *
  247. * We provide only one choice for each NB event based on
  248. * the fact that only NB events have restrictions. Consequently,
  249. * if a counter is available, there is a guarantee the NB event
  250. * will be assigned to it. If no slot is available, an empty
  251. * constraint is returned and scheduling will eventually fail
  252. * for this event.
  253. *
  254. * Note that all cores attached the same NB compete for the same
  255. * counters to host NB events, this is why we use atomic ops. Some
  256. * multi-chip CPUs may have more than one NB.
  257. *
  258. * Given that resources are allocated (cmpxchg), they must be
  259. * eventually freed for others to use. This is accomplished by
  260. * calling __amd_put_nb_event_constraints()
  261. *
  262. * Non NB events are not impacted by this restriction.
  263. */
  264. static struct event_constraint *
  265. __amd_get_nb_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event,
  266. struct event_constraint *c)
  267. {
  268. struct hw_perf_event *hwc = &event->hw;
  269. struct amd_nb *nb = cpuc->amd_nb;
  270. struct perf_event *old;
  271. int idx, new = -1;
  272. if (!c)
  273. c = &unconstrained;
  274. if (cpuc->is_fake)
  275. return c;
  276. /*
  277. * detect if already present, if so reuse
  278. *
  279. * cannot merge with actual allocation
  280. * because of possible holes
  281. *
  282. * event can already be present yet not assigned (in hwc->idx)
  283. * because of successive calls to x86_schedule_events() from
  284. * hw_perf_group_sched_in() without hw_perf_enable()
  285. */
  286. for_each_set_bit(idx, c->idxmsk, x86_pmu.num_counters) {
  287. if (new == -1 || hwc->idx == idx)
  288. /* assign free slot, prefer hwc->idx */
  289. old = cmpxchg(nb->owners + idx, NULL, event);
  290. else if (nb->owners[idx] == event)
  291. /* event already present */
  292. old = event;
  293. else
  294. continue;
  295. if (old && old != event)
  296. continue;
  297. /* reassign to this slot */
  298. if (new != -1)
  299. cmpxchg(nb->owners + new, event, NULL);
  300. new = idx;
  301. /* already present, reuse */
  302. if (old == event)
  303. break;
  304. }
  305. if (new == -1)
  306. return &emptyconstraint;
  307. return &nb->event_constraints[new];
  308. }
  309. static struct amd_nb *amd_alloc_nb(int cpu)
  310. {
  311. struct amd_nb *nb;
  312. int i;
  313. nb = kzalloc_node(sizeof(struct amd_nb), GFP_KERNEL, cpu_to_node(cpu));
  314. if (!nb)
  315. return NULL;
  316. nb->nb_id = -1;
  317. /*
  318. * initialize all possible NB constraints
  319. */
  320. for (i = 0; i < x86_pmu.num_counters; i++) {
  321. __set_bit(i, nb->event_constraints[i].idxmsk);
  322. nb->event_constraints[i].weight = 1;
  323. }
  324. return nb;
  325. }
  326. static int amd_pmu_cpu_prepare(int cpu)
  327. {
  328. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  329. WARN_ON_ONCE(cpuc->amd_nb);
  330. if (!x86_pmu.amd_nb_constraints)
  331. return 0;
  332. cpuc->amd_nb = amd_alloc_nb(cpu);
  333. if (!cpuc->amd_nb)
  334. return -ENOMEM;
  335. return 0;
  336. }
  337. static void amd_pmu_cpu_starting(int cpu)
  338. {
  339. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  340. void **onln = &cpuc->kfree_on_online[X86_PERF_KFREE_SHARED];
  341. struct amd_nb *nb;
  342. int i, nb_id;
  343. cpuc->perf_ctr_virt_mask = AMD64_EVENTSEL_HOSTONLY;
  344. if (!x86_pmu.amd_nb_constraints)
  345. return;
  346. nb_id = amd_get_nb_id(cpu);
  347. WARN_ON_ONCE(nb_id == BAD_APICID);
  348. for_each_online_cpu(i) {
  349. nb = per_cpu(cpu_hw_events, i).amd_nb;
  350. if (WARN_ON_ONCE(!nb))
  351. continue;
  352. if (nb->nb_id == nb_id) {
  353. *onln = cpuc->amd_nb;
  354. cpuc->amd_nb = nb;
  355. break;
  356. }
  357. }
  358. cpuc->amd_nb->nb_id = nb_id;
  359. cpuc->amd_nb->refcnt++;
  360. }
  361. static void amd_pmu_cpu_dead(int cpu)
  362. {
  363. struct cpu_hw_events *cpuhw;
  364. if (!x86_pmu.amd_nb_constraints)
  365. return;
  366. cpuhw = &per_cpu(cpu_hw_events, cpu);
  367. if (cpuhw->amd_nb) {
  368. struct amd_nb *nb = cpuhw->amd_nb;
  369. if (nb->nb_id == -1 || --nb->refcnt == 0)
  370. kfree(nb);
  371. cpuhw->amd_nb = NULL;
  372. }
  373. }
  374. static struct event_constraint *
  375. amd_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
  376. struct perf_event *event)
  377. {
  378. /*
  379. * if not NB event or no NB, then no constraints
  380. */
  381. if (!(amd_has_nb(cpuc) && amd_is_nb_event(&event->hw)))
  382. return &unconstrained;
  383. return __amd_get_nb_event_constraints(cpuc, event, NULL);
  384. }
  385. static void amd_put_event_constraints(struct cpu_hw_events *cpuc,
  386. struct perf_event *event)
  387. {
  388. if (amd_has_nb(cpuc) && amd_is_nb_event(&event->hw))
  389. __amd_put_nb_event_constraints(cpuc, event);
  390. }
  391. PMU_FORMAT_ATTR(event, "config:0-7,32-35");
  392. PMU_FORMAT_ATTR(umask, "config:8-15" );
  393. PMU_FORMAT_ATTR(edge, "config:18" );
  394. PMU_FORMAT_ATTR(inv, "config:23" );
  395. PMU_FORMAT_ATTR(cmask, "config:24-31" );
  396. static struct attribute *amd_format_attr[] = {
  397. &format_attr_event.attr,
  398. &format_attr_umask.attr,
  399. &format_attr_edge.attr,
  400. &format_attr_inv.attr,
  401. &format_attr_cmask.attr,
  402. NULL,
  403. };
  404. /* AMD Family 15h */
  405. #define AMD_EVENT_TYPE_MASK 0x000000F0ULL
  406. #define AMD_EVENT_FP 0x00000000ULL ... 0x00000010ULL
  407. #define AMD_EVENT_LS 0x00000020ULL ... 0x00000030ULL
  408. #define AMD_EVENT_DC 0x00000040ULL ... 0x00000050ULL
  409. #define AMD_EVENT_CU 0x00000060ULL ... 0x00000070ULL
  410. #define AMD_EVENT_IC_DE 0x00000080ULL ... 0x00000090ULL
  411. #define AMD_EVENT_EX_LS 0x000000C0ULL
  412. #define AMD_EVENT_DE 0x000000D0ULL
  413. #define AMD_EVENT_NB 0x000000E0ULL ... 0x000000F0ULL
  414. /*
  415. * AMD family 15h event code/PMC mappings:
  416. *
  417. * type = event_code & 0x0F0:
  418. *
  419. * 0x000 FP PERF_CTL[5:3]
  420. * 0x010 FP PERF_CTL[5:3]
  421. * 0x020 LS PERF_CTL[5:0]
  422. * 0x030 LS PERF_CTL[5:0]
  423. * 0x040 DC PERF_CTL[5:0]
  424. * 0x050 DC PERF_CTL[5:0]
  425. * 0x060 CU PERF_CTL[2:0]
  426. * 0x070 CU PERF_CTL[2:0]
  427. * 0x080 IC/DE PERF_CTL[2:0]
  428. * 0x090 IC/DE PERF_CTL[2:0]
  429. * 0x0A0 ---
  430. * 0x0B0 ---
  431. * 0x0C0 EX/LS PERF_CTL[5:0]
  432. * 0x0D0 DE PERF_CTL[2:0]
  433. * 0x0E0 NB NB_PERF_CTL[3:0]
  434. * 0x0F0 NB NB_PERF_CTL[3:0]
  435. *
  436. * Exceptions:
  437. *
  438. * 0x000 FP PERF_CTL[3], PERF_CTL[5:3] (*)
  439. * 0x003 FP PERF_CTL[3]
  440. * 0x004 FP PERF_CTL[3], PERF_CTL[5:3] (*)
  441. * 0x00B FP PERF_CTL[3]
  442. * 0x00D FP PERF_CTL[3]
  443. * 0x023 DE PERF_CTL[2:0]
  444. * 0x02D LS PERF_CTL[3]
  445. * 0x02E LS PERF_CTL[3,0]
  446. * 0x031 LS PERF_CTL[2:0] (**)
  447. * 0x043 CU PERF_CTL[2:0]
  448. * 0x045 CU PERF_CTL[2:0]
  449. * 0x046 CU PERF_CTL[2:0]
  450. * 0x054 CU PERF_CTL[2:0]
  451. * 0x055 CU PERF_CTL[2:0]
  452. * 0x08F IC PERF_CTL[0]
  453. * 0x187 DE PERF_CTL[0]
  454. * 0x188 DE PERF_CTL[0]
  455. * 0x0DB EX PERF_CTL[5:0]
  456. * 0x0DC LS PERF_CTL[5:0]
  457. * 0x0DD LS PERF_CTL[5:0]
  458. * 0x0DE LS PERF_CTL[5:0]
  459. * 0x0DF LS PERF_CTL[5:0]
  460. * 0x1C0 EX PERF_CTL[5:3]
  461. * 0x1D6 EX PERF_CTL[5:0]
  462. * 0x1D8 EX PERF_CTL[5:0]
  463. *
  464. * (*) depending on the umask all FPU counters may be used
  465. * (**) only one unitmask enabled at a time
  466. */
  467. static struct event_constraint amd_f15_PMC0 = EVENT_CONSTRAINT(0, 0x01, 0);
  468. static struct event_constraint amd_f15_PMC20 = EVENT_CONSTRAINT(0, 0x07, 0);
  469. static struct event_constraint amd_f15_PMC3 = EVENT_CONSTRAINT(0, 0x08, 0);
  470. static struct event_constraint amd_f15_PMC30 = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
  471. static struct event_constraint amd_f15_PMC50 = EVENT_CONSTRAINT(0, 0x3F, 0);
  472. static struct event_constraint amd_f15_PMC53 = EVENT_CONSTRAINT(0, 0x38, 0);
  473. static struct event_constraint *
  474. amd_get_event_constraints_f15h(struct cpu_hw_events *cpuc, int idx,
  475. struct perf_event *event)
  476. {
  477. struct hw_perf_event *hwc = &event->hw;
  478. unsigned int event_code = amd_get_event_code(hwc);
  479. switch (event_code & AMD_EVENT_TYPE_MASK) {
  480. case AMD_EVENT_FP:
  481. switch (event_code) {
  482. case 0x000:
  483. if (!(hwc->config & 0x0000F000ULL))
  484. break;
  485. if (!(hwc->config & 0x00000F00ULL))
  486. break;
  487. return &amd_f15_PMC3;
  488. case 0x004:
  489. if (hweight_long(hwc->config & ARCH_PERFMON_EVENTSEL_UMASK) <= 1)
  490. break;
  491. return &amd_f15_PMC3;
  492. case 0x003:
  493. case 0x00B:
  494. case 0x00D:
  495. return &amd_f15_PMC3;
  496. }
  497. return &amd_f15_PMC53;
  498. case AMD_EVENT_LS:
  499. case AMD_EVENT_DC:
  500. case AMD_EVENT_EX_LS:
  501. switch (event_code) {
  502. case 0x023:
  503. case 0x043:
  504. case 0x045:
  505. case 0x046:
  506. case 0x054:
  507. case 0x055:
  508. return &amd_f15_PMC20;
  509. case 0x02D:
  510. return &amd_f15_PMC3;
  511. case 0x02E:
  512. return &amd_f15_PMC30;
  513. case 0x031:
  514. if (hweight_long(hwc->config & ARCH_PERFMON_EVENTSEL_UMASK) <= 1)
  515. return &amd_f15_PMC20;
  516. return &emptyconstraint;
  517. case 0x1C0:
  518. return &amd_f15_PMC53;
  519. default:
  520. return &amd_f15_PMC50;
  521. }
  522. case AMD_EVENT_CU:
  523. case AMD_EVENT_IC_DE:
  524. case AMD_EVENT_DE:
  525. switch (event_code) {
  526. case 0x08F:
  527. case 0x187:
  528. case 0x188:
  529. return &amd_f15_PMC0;
  530. case 0x0DB ... 0x0DF:
  531. case 0x1D6:
  532. case 0x1D8:
  533. return &amd_f15_PMC50;
  534. default:
  535. return &amd_f15_PMC20;
  536. }
  537. case AMD_EVENT_NB:
  538. /* moved to perf_event_amd_uncore.c */
  539. return &emptyconstraint;
  540. default:
  541. return &emptyconstraint;
  542. }
  543. }
  544. static ssize_t amd_event_sysfs_show(char *page, u64 config)
  545. {
  546. u64 event = (config & ARCH_PERFMON_EVENTSEL_EVENT) |
  547. (config & AMD64_EVENTSEL_EVENT) >> 24;
  548. return x86_event_sysfs_show(page, config, event);
  549. }
  550. static __initconst const struct x86_pmu amd_pmu = {
  551. .name = "AMD",
  552. .handle_irq = x86_pmu_handle_irq,
  553. .disable_all = x86_pmu_disable_all,
  554. .enable_all = x86_pmu_enable_all,
  555. .enable = x86_pmu_enable_event,
  556. .disable = x86_pmu_disable_event,
  557. .hw_config = amd_pmu_hw_config,
  558. .schedule_events = x86_schedule_events,
  559. .eventsel = MSR_K7_EVNTSEL0,
  560. .perfctr = MSR_K7_PERFCTR0,
  561. .addr_offset = amd_pmu_addr_offset,
  562. .event_map = amd_pmu_event_map,
  563. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  564. .num_counters = AMD64_NUM_COUNTERS,
  565. .cntval_bits = 48,
  566. .cntval_mask = (1ULL << 48) - 1,
  567. .apic = 1,
  568. /* use highest bit to detect overflow */
  569. .max_period = (1ULL << 47) - 1,
  570. .get_event_constraints = amd_get_event_constraints,
  571. .put_event_constraints = amd_put_event_constraints,
  572. .format_attrs = amd_format_attr,
  573. .events_sysfs_show = amd_event_sysfs_show,
  574. .cpu_prepare = amd_pmu_cpu_prepare,
  575. .cpu_starting = amd_pmu_cpu_starting,
  576. .cpu_dead = amd_pmu_cpu_dead,
  577. .amd_nb_constraints = 1,
  578. };
  579. static int __init amd_core_pmu_init(void)
  580. {
  581. if (!boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
  582. return 0;
  583. switch (boot_cpu_data.x86) {
  584. case 0x15:
  585. pr_cont("Fam15h ");
  586. x86_pmu.get_event_constraints = amd_get_event_constraints_f15h;
  587. break;
  588. case 0x17:
  589. pr_cont("Fam17h ");
  590. /*
  591. * In family 17h, there are no event constraints in the PMC hardware.
  592. * We fallback to using default amd_get_event_constraints.
  593. */
  594. break;
  595. default:
  596. pr_err("core perfctr but no constraints; unknown hardware!\n");
  597. return -ENODEV;
  598. }
  599. /*
  600. * If core performance counter extensions exists, we must use
  601. * MSR_F15H_PERF_CTL/MSR_F15H_PERF_CTR msrs. See also
  602. * amd_pmu_addr_offset().
  603. */
  604. x86_pmu.eventsel = MSR_F15H_PERF_CTL;
  605. x86_pmu.perfctr = MSR_F15H_PERF_CTR;
  606. x86_pmu.num_counters = AMD64_NUM_COUNTERS_CORE;
  607. /*
  608. * AMD Core perfctr has separate MSRs for the NB events, see
  609. * the amd/uncore.c driver.
  610. */
  611. x86_pmu.amd_nb_constraints = 0;
  612. pr_cont("core perfctr, ");
  613. return 0;
  614. }
  615. __init int amd_pmu_init(void)
  616. {
  617. int ret;
  618. /* Performance-monitoring supported from K7 and later: */
  619. if (boot_cpu_data.x86 < 6)
  620. return -ENODEV;
  621. x86_pmu = amd_pmu;
  622. ret = amd_core_pmu_init();
  623. if (ret)
  624. return ret;
  625. if (num_possible_cpus() == 1) {
  626. /*
  627. * No point in allocating data structures to serialize
  628. * against other CPUs, when there is only the one CPU.
  629. */
  630. x86_pmu.amd_nb_constraints = 0;
  631. }
  632. /* Events are common for all AMDs */
  633. memcpy(hw_cache_event_ids, amd_hw_cache_event_ids,
  634. sizeof(hw_cache_event_ids));
  635. return 0;
  636. }
  637. void amd_pmu_enable_virt(void)
  638. {
  639. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  640. cpuc->perf_ctr_virt_mask = 0;
  641. /* Reload all events */
  642. x86_pmu_disable_all();
  643. x86_pmu_enable_all(0);
  644. }
  645. EXPORT_SYMBOL_GPL(amd_pmu_enable_virt);
  646. void amd_pmu_disable_virt(void)
  647. {
  648. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  649. /*
  650. * We only mask out the Host-only bit so that host-only counting works
  651. * when SVM is disabled. If someone sets up a guest-only counter when
  652. * SVM is disabled the Guest-only bits still gets set and the counter
  653. * will not count anything.
  654. */
  655. cpuc->perf_ctr_virt_mask = AMD64_EVENTSEL_HOSTONLY;
  656. /* Reload all events */
  657. x86_pmu_disable_all();
  658. x86_pmu_enable_all(0);
  659. }
  660. EXPORT_SYMBOL_GPL(amd_pmu_disable_virt);