perf_event_amd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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) ] = 0x0142, /* Data Cache Refills :system */
  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[] =
  116. {
  117. [PERF_COUNT_HW_CPU_CYCLES] = 0x0076,
  118. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  119. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0080,
  120. [PERF_COUNT_HW_CACHE_MISSES] = 0x0081,
  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. static int amd_pmu_hw_config(struct perf_event *event)
  131. {
  132. int ret;
  133. /* pass precise event sampling to ibs: */
  134. if (event->attr.precise_ip && get_ibs_caps())
  135. return -ENOENT;
  136. ret = x86_pmu_hw_config(event);
  137. if (ret)
  138. return ret;
  139. if (has_branch_stack(event))
  140. return -EOPNOTSUPP;
  141. if (event->attr.exclude_host && event->attr.exclude_guest)
  142. /*
  143. * When HO == GO == 1 the hardware treats that as GO == HO == 0
  144. * and will count in both modes. We don't want to count in that
  145. * case so we emulate no-counting by setting US = OS = 0.
  146. */
  147. event->hw.config &= ~(ARCH_PERFMON_EVENTSEL_USR |
  148. ARCH_PERFMON_EVENTSEL_OS);
  149. else if (event->attr.exclude_host)
  150. event->hw.config |= AMD_PERFMON_EVENTSEL_GUESTONLY;
  151. else if (event->attr.exclude_guest)
  152. event->hw.config |= AMD_PERFMON_EVENTSEL_HOSTONLY;
  153. if (event->attr.type != PERF_TYPE_RAW)
  154. return 0;
  155. event->hw.config |= event->attr.config & AMD64_RAW_EVENT_MASK;
  156. return 0;
  157. }
  158. /*
  159. * AMD64 events are detected based on their event codes.
  160. */
  161. static inline unsigned int amd_get_event_code(struct hw_perf_event *hwc)
  162. {
  163. return ((hwc->config >> 24) & 0x0f00) | (hwc->config & 0x00ff);
  164. }
  165. static inline int amd_is_nb_event(struct hw_perf_event *hwc)
  166. {
  167. return (hwc->config & 0xe0) == 0xe0;
  168. }
  169. static inline int amd_has_nb(struct cpu_hw_events *cpuc)
  170. {
  171. struct amd_nb *nb = cpuc->amd_nb;
  172. return nb && nb->nb_id != -1;
  173. }
  174. static void amd_put_event_constraints(struct cpu_hw_events *cpuc,
  175. struct perf_event *event)
  176. {
  177. struct hw_perf_event *hwc = &event->hw;
  178. struct amd_nb *nb = cpuc->amd_nb;
  179. int i;
  180. /*
  181. * only care about NB events
  182. */
  183. if (!(amd_has_nb(cpuc) && amd_is_nb_event(hwc)))
  184. return;
  185. /*
  186. * need to scan whole list because event may not have
  187. * been assigned during scheduling
  188. *
  189. * no race condition possible because event can only
  190. * be removed on one CPU at a time AND PMU is disabled
  191. * when we come here
  192. */
  193. for (i = 0; i < x86_pmu.num_counters; i++) {
  194. if (cmpxchg(nb->owners + i, event, NULL) == event)
  195. break;
  196. }
  197. }
  198. /*
  199. * AMD64 NorthBridge events need special treatment because
  200. * counter access needs to be synchronized across all cores
  201. * of a package. Refer to BKDG section 3.12
  202. *
  203. * NB events are events measuring L3 cache, Hypertransport
  204. * traffic. They are identified by an event code >= 0xe00.
  205. * They measure events on the NorthBride which is shared
  206. * by all cores on a package. NB events are counted on a
  207. * shared set of counters. When a NB event is programmed
  208. * in a counter, the data actually comes from a shared
  209. * counter. Thus, access to those counters needs to be
  210. * synchronized.
  211. *
  212. * We implement the synchronization such that no two cores
  213. * can be measuring NB events using the same counters. Thus,
  214. * we maintain a per-NB allocation table. The available slot
  215. * is propagated using the event_constraint structure.
  216. *
  217. * We provide only one choice for each NB event based on
  218. * the fact that only NB events have restrictions. Consequently,
  219. * if a counter is available, there is a guarantee the NB event
  220. * will be assigned to it. If no slot is available, an empty
  221. * constraint is returned and scheduling will eventually fail
  222. * for this event.
  223. *
  224. * Note that all cores attached the same NB compete for the same
  225. * counters to host NB events, this is why we use atomic ops. Some
  226. * multi-chip CPUs may have more than one NB.
  227. *
  228. * Given that resources are allocated (cmpxchg), they must be
  229. * eventually freed for others to use. This is accomplished by
  230. * calling amd_put_event_constraints().
  231. *
  232. * Non NB events are not impacted by this restriction.
  233. */
  234. static struct event_constraint *
  235. amd_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  236. {
  237. struct hw_perf_event *hwc = &event->hw;
  238. struct amd_nb *nb = cpuc->amd_nb;
  239. struct perf_event *old = NULL;
  240. int max = x86_pmu.num_counters;
  241. int i, j, k = -1;
  242. /*
  243. * if not NB event or no NB, then no constraints
  244. */
  245. if (!(amd_has_nb(cpuc) && amd_is_nb_event(hwc)))
  246. return &unconstrained;
  247. /*
  248. * detect if already present, if so reuse
  249. *
  250. * cannot merge with actual allocation
  251. * because of possible holes
  252. *
  253. * event can already be present yet not assigned (in hwc->idx)
  254. * because of successive calls to x86_schedule_events() from
  255. * hw_perf_group_sched_in() without hw_perf_enable()
  256. */
  257. for (i = 0; i < max; i++) {
  258. /*
  259. * keep track of first free slot
  260. */
  261. if (k == -1 && !nb->owners[i])
  262. k = i;
  263. /* already present, reuse */
  264. if (nb->owners[i] == event)
  265. goto done;
  266. }
  267. /*
  268. * not present, so grab a new slot
  269. * starting either at:
  270. */
  271. if (hwc->idx != -1) {
  272. /* previous assignment */
  273. i = hwc->idx;
  274. } else if (k != -1) {
  275. /* start from free slot found */
  276. i = k;
  277. } else {
  278. /*
  279. * event not found, no slot found in
  280. * first pass, try again from the
  281. * beginning
  282. */
  283. i = 0;
  284. }
  285. j = i;
  286. do {
  287. old = cmpxchg(nb->owners+i, NULL, event);
  288. if (!old)
  289. break;
  290. if (++i == max)
  291. i = 0;
  292. } while (i != j);
  293. done:
  294. if (!old)
  295. return &nb->event_constraints[i];
  296. return &emptyconstraint;
  297. }
  298. static struct amd_nb *amd_alloc_nb(int cpu)
  299. {
  300. struct amd_nb *nb;
  301. int i;
  302. nb = kmalloc_node(sizeof(struct amd_nb), GFP_KERNEL | __GFP_ZERO,
  303. cpu_to_node(cpu));
  304. if (!nb)
  305. return NULL;
  306. nb->nb_id = -1;
  307. /*
  308. * initialize all possible NB constraints
  309. */
  310. for (i = 0; i < x86_pmu.num_counters; i++) {
  311. __set_bit(i, nb->event_constraints[i].idxmsk);
  312. nb->event_constraints[i].weight = 1;
  313. }
  314. return nb;
  315. }
  316. static int amd_pmu_cpu_prepare(int cpu)
  317. {
  318. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  319. WARN_ON_ONCE(cpuc->amd_nb);
  320. if (boot_cpu_data.x86_max_cores < 2)
  321. return NOTIFY_OK;
  322. cpuc->amd_nb = amd_alloc_nb(cpu);
  323. if (!cpuc->amd_nb)
  324. return NOTIFY_BAD;
  325. return NOTIFY_OK;
  326. }
  327. static void amd_pmu_cpu_starting(int cpu)
  328. {
  329. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  330. struct amd_nb *nb;
  331. int i, nb_id;
  332. cpuc->perf_ctr_virt_mask = AMD_PERFMON_EVENTSEL_HOSTONLY;
  333. if (boot_cpu_data.x86_max_cores < 2)
  334. return;
  335. nb_id = amd_get_nb_id(cpu);
  336. WARN_ON_ONCE(nb_id == BAD_APICID);
  337. for_each_online_cpu(i) {
  338. nb = per_cpu(cpu_hw_events, i).amd_nb;
  339. if (WARN_ON_ONCE(!nb))
  340. continue;
  341. if (nb->nb_id == nb_id) {
  342. cpuc->kfree_on_online = cpuc->amd_nb;
  343. cpuc->amd_nb = nb;
  344. break;
  345. }
  346. }
  347. cpuc->amd_nb->nb_id = nb_id;
  348. cpuc->amd_nb->refcnt++;
  349. }
  350. static void amd_pmu_cpu_dead(int cpu)
  351. {
  352. struct cpu_hw_events *cpuhw;
  353. if (boot_cpu_data.x86_max_cores < 2)
  354. return;
  355. cpuhw = &per_cpu(cpu_hw_events, cpu);
  356. if (cpuhw->amd_nb) {
  357. struct amd_nb *nb = cpuhw->amd_nb;
  358. if (nb->nb_id == -1 || --nb->refcnt == 0)
  359. kfree(nb);
  360. cpuhw->amd_nb = NULL;
  361. }
  362. }
  363. PMU_FORMAT_ATTR(event, "config:0-7,32-35");
  364. PMU_FORMAT_ATTR(umask, "config:8-15" );
  365. PMU_FORMAT_ATTR(edge, "config:18" );
  366. PMU_FORMAT_ATTR(inv, "config:23" );
  367. PMU_FORMAT_ATTR(cmask, "config:24-31" );
  368. static struct attribute *amd_format_attr[] = {
  369. &format_attr_event.attr,
  370. &format_attr_umask.attr,
  371. &format_attr_edge.attr,
  372. &format_attr_inv.attr,
  373. &format_attr_cmask.attr,
  374. NULL,
  375. };
  376. /* AMD Family 15h */
  377. #define AMD_EVENT_TYPE_MASK 0x000000F0ULL
  378. #define AMD_EVENT_FP 0x00000000ULL ... 0x00000010ULL
  379. #define AMD_EVENT_LS 0x00000020ULL ... 0x00000030ULL
  380. #define AMD_EVENT_DC 0x00000040ULL ... 0x00000050ULL
  381. #define AMD_EVENT_CU 0x00000060ULL ... 0x00000070ULL
  382. #define AMD_EVENT_IC_DE 0x00000080ULL ... 0x00000090ULL
  383. #define AMD_EVENT_EX_LS 0x000000C0ULL
  384. #define AMD_EVENT_DE 0x000000D0ULL
  385. #define AMD_EVENT_NB 0x000000E0ULL ... 0x000000F0ULL
  386. /*
  387. * AMD family 15h event code/PMC mappings:
  388. *
  389. * type = event_code & 0x0F0:
  390. *
  391. * 0x000 FP PERF_CTL[5:3]
  392. * 0x010 FP PERF_CTL[5:3]
  393. * 0x020 LS PERF_CTL[5:0]
  394. * 0x030 LS PERF_CTL[5:0]
  395. * 0x040 DC PERF_CTL[5:0]
  396. * 0x050 DC PERF_CTL[5:0]
  397. * 0x060 CU PERF_CTL[2:0]
  398. * 0x070 CU PERF_CTL[2:0]
  399. * 0x080 IC/DE PERF_CTL[2:0]
  400. * 0x090 IC/DE PERF_CTL[2:0]
  401. * 0x0A0 ---
  402. * 0x0B0 ---
  403. * 0x0C0 EX/LS PERF_CTL[5:0]
  404. * 0x0D0 DE PERF_CTL[2:0]
  405. * 0x0E0 NB NB_PERF_CTL[3:0]
  406. * 0x0F0 NB NB_PERF_CTL[3:0]
  407. *
  408. * Exceptions:
  409. *
  410. * 0x000 FP PERF_CTL[3], PERF_CTL[5:3] (*)
  411. * 0x003 FP PERF_CTL[3]
  412. * 0x004 FP PERF_CTL[3], PERF_CTL[5:3] (*)
  413. * 0x00B FP PERF_CTL[3]
  414. * 0x00D FP PERF_CTL[3]
  415. * 0x023 DE PERF_CTL[2:0]
  416. * 0x02D LS PERF_CTL[3]
  417. * 0x02E LS PERF_CTL[3,0]
  418. * 0x031 LS PERF_CTL[2:0] (**)
  419. * 0x043 CU PERF_CTL[2:0]
  420. * 0x045 CU PERF_CTL[2:0]
  421. * 0x046 CU PERF_CTL[2:0]
  422. * 0x054 CU PERF_CTL[2:0]
  423. * 0x055 CU PERF_CTL[2:0]
  424. * 0x08F IC PERF_CTL[0]
  425. * 0x187 DE PERF_CTL[0]
  426. * 0x188 DE PERF_CTL[0]
  427. * 0x0DB EX PERF_CTL[5:0]
  428. * 0x0DC LS PERF_CTL[5:0]
  429. * 0x0DD LS PERF_CTL[5:0]
  430. * 0x0DE LS PERF_CTL[5:0]
  431. * 0x0DF LS PERF_CTL[5:0]
  432. * 0x1C0 EX PERF_CTL[5:3]
  433. * 0x1D6 EX PERF_CTL[5:0]
  434. * 0x1D8 EX PERF_CTL[5:0]
  435. *
  436. * (*) depending on the umask all FPU counters may be used
  437. * (**) only one unitmask enabled at a time
  438. */
  439. static struct event_constraint amd_f15_PMC0 = EVENT_CONSTRAINT(0, 0x01, 0);
  440. static struct event_constraint amd_f15_PMC20 = EVENT_CONSTRAINT(0, 0x07, 0);
  441. static struct event_constraint amd_f15_PMC3 = EVENT_CONSTRAINT(0, 0x08, 0);
  442. static struct event_constraint amd_f15_PMC30 = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
  443. static struct event_constraint amd_f15_PMC50 = EVENT_CONSTRAINT(0, 0x3F, 0);
  444. static struct event_constraint amd_f15_PMC53 = EVENT_CONSTRAINT(0, 0x38, 0);
  445. static struct event_constraint *
  446. amd_get_event_constraints_f15h(struct cpu_hw_events *cpuc, struct perf_event *event)
  447. {
  448. struct hw_perf_event *hwc = &event->hw;
  449. unsigned int event_code = amd_get_event_code(hwc);
  450. switch (event_code & AMD_EVENT_TYPE_MASK) {
  451. case AMD_EVENT_FP:
  452. switch (event_code) {
  453. case 0x000:
  454. if (!(hwc->config & 0x0000F000ULL))
  455. break;
  456. if (!(hwc->config & 0x00000F00ULL))
  457. break;
  458. return &amd_f15_PMC3;
  459. case 0x004:
  460. if (hweight_long(hwc->config & ARCH_PERFMON_EVENTSEL_UMASK) <= 1)
  461. break;
  462. return &amd_f15_PMC3;
  463. case 0x003:
  464. case 0x00B:
  465. case 0x00D:
  466. return &amd_f15_PMC3;
  467. }
  468. return &amd_f15_PMC53;
  469. case AMD_EVENT_LS:
  470. case AMD_EVENT_DC:
  471. case AMD_EVENT_EX_LS:
  472. switch (event_code) {
  473. case 0x023:
  474. case 0x043:
  475. case 0x045:
  476. case 0x046:
  477. case 0x054:
  478. case 0x055:
  479. return &amd_f15_PMC20;
  480. case 0x02D:
  481. return &amd_f15_PMC3;
  482. case 0x02E:
  483. return &amd_f15_PMC30;
  484. case 0x031:
  485. if (hweight_long(hwc->config & ARCH_PERFMON_EVENTSEL_UMASK) <= 1)
  486. return &amd_f15_PMC20;
  487. return &emptyconstraint;
  488. case 0x1C0:
  489. return &amd_f15_PMC53;
  490. default:
  491. return &amd_f15_PMC50;
  492. }
  493. case AMD_EVENT_CU:
  494. case AMD_EVENT_IC_DE:
  495. case AMD_EVENT_DE:
  496. switch (event_code) {
  497. case 0x08F:
  498. case 0x187:
  499. case 0x188:
  500. return &amd_f15_PMC0;
  501. case 0x0DB ... 0x0DF:
  502. case 0x1D6:
  503. case 0x1D8:
  504. return &amd_f15_PMC50;
  505. default:
  506. return &amd_f15_PMC20;
  507. }
  508. case AMD_EVENT_NB:
  509. /* not yet implemented */
  510. return &emptyconstraint;
  511. default:
  512. return &emptyconstraint;
  513. }
  514. }
  515. static __initconst const struct x86_pmu amd_pmu = {
  516. .name = "AMD",
  517. .handle_irq = x86_pmu_handle_irq,
  518. .disable_all = x86_pmu_disable_all,
  519. .enable_all = x86_pmu_enable_all,
  520. .enable = x86_pmu_enable_event,
  521. .disable = x86_pmu_disable_event,
  522. .hw_config = amd_pmu_hw_config,
  523. .schedule_events = x86_schedule_events,
  524. .eventsel = MSR_K7_EVNTSEL0,
  525. .perfctr = MSR_K7_PERFCTR0,
  526. .event_map = amd_pmu_event_map,
  527. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  528. .num_counters = AMD64_NUM_COUNTERS,
  529. .cntval_bits = 48,
  530. .cntval_mask = (1ULL << 48) - 1,
  531. .apic = 1,
  532. /* use highest bit to detect overflow */
  533. .max_period = (1ULL << 47) - 1,
  534. .get_event_constraints = amd_get_event_constraints,
  535. .put_event_constraints = amd_put_event_constraints,
  536. .format_attrs = amd_format_attr,
  537. .cpu_prepare = amd_pmu_cpu_prepare,
  538. .cpu_starting = amd_pmu_cpu_starting,
  539. .cpu_dead = amd_pmu_cpu_dead,
  540. };
  541. static int setup_event_constraints(void)
  542. {
  543. if (boot_cpu_data.x86 >= 0x15)
  544. x86_pmu.get_event_constraints = amd_get_event_constraints_f15h;
  545. return 0;
  546. }
  547. static int setup_perfctr_core(void)
  548. {
  549. if (!cpu_has_perfctr_core) {
  550. WARN(x86_pmu.get_event_constraints == amd_get_event_constraints_f15h,
  551. KERN_ERR "Odd, counter constraints enabled but no core perfctrs detected!");
  552. return -ENODEV;
  553. }
  554. WARN(x86_pmu.get_event_constraints == amd_get_event_constraints,
  555. KERN_ERR "hw perf events core counters need constraints handler!");
  556. /*
  557. * If core performance counter extensions exists, we must use
  558. * MSR_F15H_PERF_CTL/MSR_F15H_PERF_CTR msrs. See also
  559. * x86_pmu_addr_offset().
  560. */
  561. x86_pmu.eventsel = MSR_F15H_PERF_CTL;
  562. x86_pmu.perfctr = MSR_F15H_PERF_CTR;
  563. x86_pmu.num_counters = AMD64_NUM_COUNTERS_CORE;
  564. printk(KERN_INFO "perf: AMD core performance counters detected\n");
  565. return 0;
  566. }
  567. __init int amd_pmu_init(void)
  568. {
  569. /* Performance-monitoring supported from K7 and later: */
  570. if (boot_cpu_data.x86 < 6)
  571. return -ENODEV;
  572. x86_pmu = amd_pmu;
  573. setup_event_constraints();
  574. setup_perfctr_core();
  575. /* Events are common for all AMDs */
  576. memcpy(hw_cache_event_ids, amd_hw_cache_event_ids,
  577. sizeof(hw_cache_event_ids));
  578. return 0;
  579. }
  580. void amd_pmu_enable_virt(void)
  581. {
  582. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  583. cpuc->perf_ctr_virt_mask = 0;
  584. /* Reload all events */
  585. x86_pmu_disable_all();
  586. x86_pmu_enable_all(0);
  587. }
  588. EXPORT_SYMBOL_GPL(amd_pmu_enable_virt);
  589. void amd_pmu_disable_virt(void)
  590. {
  591. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  592. /*
  593. * We only mask out the Host-only bit so that host-only counting works
  594. * when SVM is disabled. If someone sets up a guest-only counter when
  595. * SVM is disabled the Guest-only bits still gets set and the counter
  596. * will not count anything.
  597. */
  598. cpuc->perf_ctr_virt_mask = AMD_PERFMON_EVENTSEL_HOSTONLY;
  599. /* Reload all events */
  600. x86_pmu_disable_all();
  601. x86_pmu_enable_all(0);
  602. }
  603. EXPORT_SYMBOL_GPL(amd_pmu_disable_virt);