perf_event_amd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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 struct event_constraint *amd_nb_event_constraint;
  131. /*
  132. * Previously calculated offsets
  133. */
  134. static unsigned int event_offsets[X86_PMC_IDX_MAX] __read_mostly;
  135. static unsigned int count_offsets[X86_PMC_IDX_MAX] __read_mostly;
  136. static unsigned int rdpmc_indexes[X86_PMC_IDX_MAX] __read_mostly;
  137. /*
  138. * Legacy CPUs:
  139. * 4 counters starting at 0xc0010000 each offset by 1
  140. *
  141. * CPUs with core performance counter extensions:
  142. * 6 counters starting at 0xc0010200 each offset by 2
  143. *
  144. * CPUs with north bridge performance counter extensions:
  145. * 4 additional counters starting at 0xc0010240 each offset by 2
  146. * (indexed right above either one of the above core counters)
  147. */
  148. static inline int amd_pmu_addr_offset(int index, bool eventsel)
  149. {
  150. int offset, first, base;
  151. if (!index)
  152. return index;
  153. if (eventsel)
  154. offset = event_offsets[index];
  155. else
  156. offset = count_offsets[index];
  157. if (offset)
  158. return offset;
  159. if (amd_nb_event_constraint &&
  160. test_bit(index, amd_nb_event_constraint->idxmsk)) {
  161. /*
  162. * calculate the offset of NB counters with respect to
  163. * base eventsel or perfctr
  164. */
  165. first = find_first_bit(amd_nb_event_constraint->idxmsk,
  166. X86_PMC_IDX_MAX);
  167. if (eventsel)
  168. base = MSR_F15H_NB_PERF_CTL - x86_pmu.eventsel;
  169. else
  170. base = MSR_F15H_NB_PERF_CTR - x86_pmu.perfctr;
  171. offset = base + ((index - first) << 1);
  172. } else if (!cpu_has_perfctr_core)
  173. offset = index;
  174. else
  175. offset = index << 1;
  176. if (eventsel)
  177. event_offsets[index] = offset;
  178. else
  179. count_offsets[index] = offset;
  180. return offset;
  181. }
  182. static inline int amd_pmu_rdpmc_index(int index)
  183. {
  184. int ret, first;
  185. if (!index)
  186. return index;
  187. ret = rdpmc_indexes[index];
  188. if (ret)
  189. return ret;
  190. if (amd_nb_event_constraint &&
  191. test_bit(index, amd_nb_event_constraint->idxmsk)) {
  192. /*
  193. * according to the mnual, ECX value of the NB counters is
  194. * the index of the NB counter (0, 1, 2 or 3) plus 6
  195. */
  196. first = find_first_bit(amd_nb_event_constraint->idxmsk,
  197. X86_PMC_IDX_MAX);
  198. ret = index - first + 6;
  199. } else
  200. ret = index;
  201. rdpmc_indexes[index] = ret;
  202. return ret;
  203. }
  204. static int amd_core_hw_config(struct perf_event *event)
  205. {
  206. if (event->attr.exclude_host && event->attr.exclude_guest)
  207. /*
  208. * When HO == GO == 1 the hardware treats that as GO == HO == 0
  209. * and will count in both modes. We don't want to count in that
  210. * case so we emulate no-counting by setting US = OS = 0.
  211. */
  212. event->hw.config &= ~(ARCH_PERFMON_EVENTSEL_USR |
  213. ARCH_PERFMON_EVENTSEL_OS);
  214. else if (event->attr.exclude_host)
  215. event->hw.config |= AMD64_EVENTSEL_GUESTONLY;
  216. else if (event->attr.exclude_guest)
  217. event->hw.config |= AMD64_EVENTSEL_HOSTONLY;
  218. return 0;
  219. }
  220. /*
  221. * NB counters do not support the following event select bits:
  222. * Host/Guest only
  223. * Counter mask
  224. * Invert counter mask
  225. * Edge detect
  226. * OS/User mode
  227. */
  228. static int amd_nb_hw_config(struct perf_event *event)
  229. {
  230. /* for NB, we only allow system wide counting mode */
  231. if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
  232. return -EINVAL;
  233. if (event->attr.exclude_user || event->attr.exclude_kernel ||
  234. event->attr.exclude_host || event->attr.exclude_guest)
  235. return -EINVAL;
  236. event->hw.config &= ~(ARCH_PERFMON_EVENTSEL_USR |
  237. ARCH_PERFMON_EVENTSEL_OS);
  238. if (event->hw.config & ~(AMD64_RAW_EVENT_MASK_NB |
  239. ARCH_PERFMON_EVENTSEL_INT))
  240. return -EINVAL;
  241. return 0;
  242. }
  243. /*
  244. * AMD64 events are detected based on their event codes.
  245. */
  246. static inline unsigned int amd_get_event_code(struct hw_perf_event *hwc)
  247. {
  248. return ((hwc->config >> 24) & 0x0f00) | (hwc->config & 0x00ff);
  249. }
  250. static inline int amd_is_nb_event(struct hw_perf_event *hwc)
  251. {
  252. return (hwc->config & 0xe0) == 0xe0;
  253. }
  254. static inline int amd_is_perfctr_nb_event(struct hw_perf_event *hwc)
  255. {
  256. return amd_nb_event_constraint && amd_is_nb_event(hwc);
  257. }
  258. static inline int amd_has_nb(struct cpu_hw_events *cpuc)
  259. {
  260. struct amd_nb *nb = cpuc->amd_nb;
  261. return nb && nb->nb_id != -1;
  262. }
  263. static int amd_pmu_hw_config(struct perf_event *event)
  264. {
  265. int ret;
  266. /* pass precise event sampling to ibs: */
  267. if (event->attr.precise_ip && get_ibs_caps())
  268. return -ENOENT;
  269. if (has_branch_stack(event))
  270. return -EOPNOTSUPP;
  271. ret = x86_pmu_hw_config(event);
  272. if (ret)
  273. return ret;
  274. if (event->attr.type == PERF_TYPE_RAW)
  275. event->hw.config |= event->attr.config & AMD64_RAW_EVENT_MASK;
  276. if (amd_is_perfctr_nb_event(&event->hw))
  277. return amd_nb_hw_config(event);
  278. return amd_core_hw_config(event);
  279. }
  280. static void __amd_put_nb_event_constraints(struct cpu_hw_events *cpuc,
  281. struct perf_event *event)
  282. {
  283. struct amd_nb *nb = cpuc->amd_nb;
  284. int i;
  285. /*
  286. * need to scan whole list because event may not have
  287. * been assigned during scheduling
  288. *
  289. * no race condition possible because event can only
  290. * be removed on one CPU at a time AND PMU is disabled
  291. * when we come here
  292. */
  293. for (i = 0; i < x86_pmu.num_counters; i++) {
  294. if (cmpxchg(nb->owners + i, event, NULL) == event)
  295. break;
  296. }
  297. }
  298. static void amd_nb_interrupt_hw_config(struct hw_perf_event *hwc)
  299. {
  300. int core_id = cpu_data(smp_processor_id()).cpu_core_id;
  301. /* deliver interrupts only to this core */
  302. if (hwc->config & ARCH_PERFMON_EVENTSEL_INT) {
  303. hwc->config |= AMD64_EVENTSEL_INT_CORE_ENABLE;
  304. hwc->config &= ~AMD64_EVENTSEL_INT_CORE_SEL_MASK;
  305. hwc->config |= (u64)(core_id) <<
  306. AMD64_EVENTSEL_INT_CORE_SEL_SHIFT;
  307. }
  308. }
  309. /*
  310. * AMD64 NorthBridge events need special treatment because
  311. * counter access needs to be synchronized across all cores
  312. * of a package. Refer to BKDG section 3.12
  313. *
  314. * NB events are events measuring L3 cache, Hypertransport
  315. * traffic. They are identified by an event code >= 0xe00.
  316. * They measure events on the NorthBride which is shared
  317. * by all cores on a package. NB events are counted on a
  318. * shared set of counters. When a NB event is programmed
  319. * in a counter, the data actually comes from a shared
  320. * counter. Thus, access to those counters needs to be
  321. * synchronized.
  322. *
  323. * We implement the synchronization such that no two cores
  324. * can be measuring NB events using the same counters. Thus,
  325. * we maintain a per-NB allocation table. The available slot
  326. * is propagated using the event_constraint structure.
  327. *
  328. * We provide only one choice for each NB event based on
  329. * the fact that only NB events have restrictions. Consequently,
  330. * if a counter is available, there is a guarantee the NB event
  331. * will be assigned to it. If no slot is available, an empty
  332. * constraint is returned and scheduling will eventually fail
  333. * for this event.
  334. *
  335. * Note that all cores attached the same NB compete for the same
  336. * counters to host NB events, this is why we use atomic ops. Some
  337. * multi-chip CPUs may have more than one NB.
  338. *
  339. * Given that resources are allocated (cmpxchg), they must be
  340. * eventually freed for others to use. This is accomplished by
  341. * calling __amd_put_nb_event_constraints()
  342. *
  343. * Non NB events are not impacted by this restriction.
  344. */
  345. static struct event_constraint *
  346. __amd_get_nb_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event,
  347. struct event_constraint *c)
  348. {
  349. struct hw_perf_event *hwc = &event->hw;
  350. struct amd_nb *nb = cpuc->amd_nb;
  351. struct perf_event *old;
  352. int idx, new = -1;
  353. if (!c)
  354. c = &unconstrained;
  355. if (cpuc->is_fake)
  356. return c;
  357. /*
  358. * detect if already present, if so reuse
  359. *
  360. * cannot merge with actual allocation
  361. * because of possible holes
  362. *
  363. * event can already be present yet not assigned (in hwc->idx)
  364. * because of successive calls to x86_schedule_events() from
  365. * hw_perf_group_sched_in() without hw_perf_enable()
  366. */
  367. for_each_set_bit(idx, c->idxmsk, x86_pmu.num_counters) {
  368. if (new == -1 || hwc->idx == idx)
  369. /* assign free slot, prefer hwc->idx */
  370. old = cmpxchg(nb->owners + idx, NULL, event);
  371. else if (nb->owners[idx] == event)
  372. /* event already present */
  373. old = event;
  374. else
  375. continue;
  376. if (old && old != event)
  377. continue;
  378. /* reassign to this slot */
  379. if (new != -1)
  380. cmpxchg(nb->owners + new, event, NULL);
  381. new = idx;
  382. /* already present, reuse */
  383. if (old == event)
  384. break;
  385. }
  386. if (new == -1)
  387. return &emptyconstraint;
  388. if (amd_is_perfctr_nb_event(hwc))
  389. amd_nb_interrupt_hw_config(hwc);
  390. return &nb->event_constraints[new];
  391. }
  392. static struct amd_nb *amd_alloc_nb(int cpu)
  393. {
  394. struct amd_nb *nb;
  395. int i;
  396. nb = kmalloc_node(sizeof(struct amd_nb), GFP_KERNEL | __GFP_ZERO,
  397. cpu_to_node(cpu));
  398. if (!nb)
  399. return NULL;
  400. nb->nb_id = -1;
  401. /*
  402. * initialize all possible NB constraints
  403. */
  404. for (i = 0; i < x86_pmu.num_counters; i++) {
  405. __set_bit(i, nb->event_constraints[i].idxmsk);
  406. nb->event_constraints[i].weight = 1;
  407. }
  408. return nb;
  409. }
  410. static int amd_pmu_cpu_prepare(int cpu)
  411. {
  412. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  413. WARN_ON_ONCE(cpuc->amd_nb);
  414. if (boot_cpu_data.x86_max_cores < 2)
  415. return NOTIFY_OK;
  416. cpuc->amd_nb = amd_alloc_nb(cpu);
  417. if (!cpuc->amd_nb)
  418. return NOTIFY_BAD;
  419. return NOTIFY_OK;
  420. }
  421. static void amd_pmu_cpu_starting(int cpu)
  422. {
  423. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  424. struct amd_nb *nb;
  425. int i, nb_id;
  426. cpuc->perf_ctr_virt_mask = AMD64_EVENTSEL_HOSTONLY;
  427. if (boot_cpu_data.x86_max_cores < 2)
  428. return;
  429. nb_id = amd_get_nb_id(cpu);
  430. WARN_ON_ONCE(nb_id == BAD_APICID);
  431. for_each_online_cpu(i) {
  432. nb = per_cpu(cpu_hw_events, i).amd_nb;
  433. if (WARN_ON_ONCE(!nb))
  434. continue;
  435. if (nb->nb_id == nb_id) {
  436. cpuc->kfree_on_online = cpuc->amd_nb;
  437. cpuc->amd_nb = nb;
  438. break;
  439. }
  440. }
  441. cpuc->amd_nb->nb_id = nb_id;
  442. cpuc->amd_nb->refcnt++;
  443. }
  444. static void amd_pmu_cpu_dead(int cpu)
  445. {
  446. struct cpu_hw_events *cpuhw;
  447. if (boot_cpu_data.x86_max_cores < 2)
  448. return;
  449. cpuhw = &per_cpu(cpu_hw_events, cpu);
  450. if (cpuhw->amd_nb) {
  451. struct amd_nb *nb = cpuhw->amd_nb;
  452. if (nb->nb_id == -1 || --nb->refcnt == 0)
  453. kfree(nb);
  454. cpuhw->amd_nb = NULL;
  455. }
  456. }
  457. static struct event_constraint *
  458. amd_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  459. {
  460. /*
  461. * if not NB event or no NB, then no constraints
  462. */
  463. if (!(amd_has_nb(cpuc) && amd_is_nb_event(&event->hw)))
  464. return &unconstrained;
  465. return __amd_get_nb_event_constraints(cpuc, event,
  466. amd_nb_event_constraint);
  467. }
  468. static void amd_put_event_constraints(struct cpu_hw_events *cpuc,
  469. struct perf_event *event)
  470. {
  471. if (amd_has_nb(cpuc) && amd_is_nb_event(&event->hw))
  472. __amd_put_nb_event_constraints(cpuc, event);
  473. }
  474. PMU_FORMAT_ATTR(event, "config:0-7,32-35");
  475. PMU_FORMAT_ATTR(umask, "config:8-15" );
  476. PMU_FORMAT_ATTR(edge, "config:18" );
  477. PMU_FORMAT_ATTR(inv, "config:23" );
  478. PMU_FORMAT_ATTR(cmask, "config:24-31" );
  479. static struct attribute *amd_format_attr[] = {
  480. &format_attr_event.attr,
  481. &format_attr_umask.attr,
  482. &format_attr_edge.attr,
  483. &format_attr_inv.attr,
  484. &format_attr_cmask.attr,
  485. NULL,
  486. };
  487. /* AMD Family 15h */
  488. #define AMD_EVENT_TYPE_MASK 0x000000F0ULL
  489. #define AMD_EVENT_FP 0x00000000ULL ... 0x00000010ULL
  490. #define AMD_EVENT_LS 0x00000020ULL ... 0x00000030ULL
  491. #define AMD_EVENT_DC 0x00000040ULL ... 0x00000050ULL
  492. #define AMD_EVENT_CU 0x00000060ULL ... 0x00000070ULL
  493. #define AMD_EVENT_IC_DE 0x00000080ULL ... 0x00000090ULL
  494. #define AMD_EVENT_EX_LS 0x000000C0ULL
  495. #define AMD_EVENT_DE 0x000000D0ULL
  496. #define AMD_EVENT_NB 0x000000E0ULL ... 0x000000F0ULL
  497. /*
  498. * AMD family 15h event code/PMC mappings:
  499. *
  500. * type = event_code & 0x0F0:
  501. *
  502. * 0x000 FP PERF_CTL[5:3]
  503. * 0x010 FP PERF_CTL[5:3]
  504. * 0x020 LS PERF_CTL[5:0]
  505. * 0x030 LS PERF_CTL[5:0]
  506. * 0x040 DC PERF_CTL[5:0]
  507. * 0x050 DC PERF_CTL[5:0]
  508. * 0x060 CU PERF_CTL[2:0]
  509. * 0x070 CU PERF_CTL[2:0]
  510. * 0x080 IC/DE PERF_CTL[2:0]
  511. * 0x090 IC/DE PERF_CTL[2:0]
  512. * 0x0A0 ---
  513. * 0x0B0 ---
  514. * 0x0C0 EX/LS PERF_CTL[5:0]
  515. * 0x0D0 DE PERF_CTL[2:0]
  516. * 0x0E0 NB NB_PERF_CTL[3:0]
  517. * 0x0F0 NB NB_PERF_CTL[3:0]
  518. *
  519. * Exceptions:
  520. *
  521. * 0x000 FP PERF_CTL[3], PERF_CTL[5:3] (*)
  522. * 0x003 FP PERF_CTL[3]
  523. * 0x004 FP PERF_CTL[3], PERF_CTL[5:3] (*)
  524. * 0x00B FP PERF_CTL[3]
  525. * 0x00D FP PERF_CTL[3]
  526. * 0x023 DE PERF_CTL[2:0]
  527. * 0x02D LS PERF_CTL[3]
  528. * 0x02E LS PERF_CTL[3,0]
  529. * 0x031 LS PERF_CTL[2:0] (**)
  530. * 0x043 CU PERF_CTL[2:0]
  531. * 0x045 CU PERF_CTL[2:0]
  532. * 0x046 CU PERF_CTL[2:0]
  533. * 0x054 CU PERF_CTL[2:0]
  534. * 0x055 CU PERF_CTL[2:0]
  535. * 0x08F IC PERF_CTL[0]
  536. * 0x187 DE PERF_CTL[0]
  537. * 0x188 DE PERF_CTL[0]
  538. * 0x0DB EX PERF_CTL[5:0]
  539. * 0x0DC LS PERF_CTL[5:0]
  540. * 0x0DD LS PERF_CTL[5:0]
  541. * 0x0DE LS PERF_CTL[5:0]
  542. * 0x0DF LS PERF_CTL[5:0]
  543. * 0x1C0 EX PERF_CTL[5:3]
  544. * 0x1D6 EX PERF_CTL[5:0]
  545. * 0x1D8 EX PERF_CTL[5:0]
  546. *
  547. * (*) depending on the umask all FPU counters may be used
  548. * (**) only one unitmask enabled at a time
  549. */
  550. static struct event_constraint amd_f15_PMC0 = EVENT_CONSTRAINT(0, 0x01, 0);
  551. static struct event_constraint amd_f15_PMC20 = EVENT_CONSTRAINT(0, 0x07, 0);
  552. static struct event_constraint amd_f15_PMC3 = EVENT_CONSTRAINT(0, 0x08, 0);
  553. static struct event_constraint amd_f15_PMC30 = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0);
  554. static struct event_constraint amd_f15_PMC50 = EVENT_CONSTRAINT(0, 0x3F, 0);
  555. static struct event_constraint amd_f15_PMC53 = EVENT_CONSTRAINT(0, 0x38, 0);
  556. static struct event_constraint amd_NBPMC96 = EVENT_CONSTRAINT(0, 0x3C0, 0);
  557. static struct event_constraint amd_NBPMC74 = EVENT_CONSTRAINT(0, 0xF0, 0);
  558. static struct event_constraint *
  559. amd_get_event_constraints_f15h(struct cpu_hw_events *cpuc, struct perf_event *event)
  560. {
  561. struct hw_perf_event *hwc = &event->hw;
  562. unsigned int event_code = amd_get_event_code(hwc);
  563. switch (event_code & AMD_EVENT_TYPE_MASK) {
  564. case AMD_EVENT_FP:
  565. switch (event_code) {
  566. case 0x000:
  567. if (!(hwc->config & 0x0000F000ULL))
  568. break;
  569. if (!(hwc->config & 0x00000F00ULL))
  570. break;
  571. return &amd_f15_PMC3;
  572. case 0x004:
  573. if (hweight_long(hwc->config & ARCH_PERFMON_EVENTSEL_UMASK) <= 1)
  574. break;
  575. return &amd_f15_PMC3;
  576. case 0x003:
  577. case 0x00B:
  578. case 0x00D:
  579. return &amd_f15_PMC3;
  580. }
  581. return &amd_f15_PMC53;
  582. case AMD_EVENT_LS:
  583. case AMD_EVENT_DC:
  584. case AMD_EVENT_EX_LS:
  585. switch (event_code) {
  586. case 0x023:
  587. case 0x043:
  588. case 0x045:
  589. case 0x046:
  590. case 0x054:
  591. case 0x055:
  592. return &amd_f15_PMC20;
  593. case 0x02D:
  594. return &amd_f15_PMC3;
  595. case 0x02E:
  596. return &amd_f15_PMC30;
  597. case 0x031:
  598. if (hweight_long(hwc->config & ARCH_PERFMON_EVENTSEL_UMASK) <= 1)
  599. return &amd_f15_PMC20;
  600. return &emptyconstraint;
  601. case 0x1C0:
  602. return &amd_f15_PMC53;
  603. default:
  604. return &amd_f15_PMC50;
  605. }
  606. case AMD_EVENT_CU:
  607. case AMD_EVENT_IC_DE:
  608. case AMD_EVENT_DE:
  609. switch (event_code) {
  610. case 0x08F:
  611. case 0x187:
  612. case 0x188:
  613. return &amd_f15_PMC0;
  614. case 0x0DB ... 0x0DF:
  615. case 0x1D6:
  616. case 0x1D8:
  617. return &amd_f15_PMC50;
  618. default:
  619. return &amd_f15_PMC20;
  620. }
  621. case AMD_EVENT_NB:
  622. return __amd_get_nb_event_constraints(cpuc, event,
  623. amd_nb_event_constraint);
  624. default:
  625. return &emptyconstraint;
  626. }
  627. }
  628. static ssize_t amd_event_sysfs_show(char *page, u64 config)
  629. {
  630. u64 event = (config & ARCH_PERFMON_EVENTSEL_EVENT) |
  631. (config & AMD64_EVENTSEL_EVENT) >> 24;
  632. return x86_event_sysfs_show(page, config, event);
  633. }
  634. static __initconst const struct x86_pmu amd_pmu = {
  635. .name = "AMD",
  636. .handle_irq = x86_pmu_handle_irq,
  637. .disable_all = x86_pmu_disable_all,
  638. .enable_all = x86_pmu_enable_all,
  639. .enable = x86_pmu_enable_event,
  640. .disable = x86_pmu_disable_event,
  641. .hw_config = amd_pmu_hw_config,
  642. .schedule_events = x86_schedule_events,
  643. .eventsel = MSR_K7_EVNTSEL0,
  644. .perfctr = MSR_K7_PERFCTR0,
  645. .addr_offset = amd_pmu_addr_offset,
  646. .rdpmc_index = amd_pmu_rdpmc_index,
  647. .event_map = amd_pmu_event_map,
  648. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  649. .num_counters = AMD64_NUM_COUNTERS,
  650. .cntval_bits = 48,
  651. .cntval_mask = (1ULL << 48) - 1,
  652. .apic = 1,
  653. /* use highest bit to detect overflow */
  654. .max_period = (1ULL << 47) - 1,
  655. .get_event_constraints = amd_get_event_constraints,
  656. .put_event_constraints = amd_put_event_constraints,
  657. .format_attrs = amd_format_attr,
  658. .events_sysfs_show = amd_event_sysfs_show,
  659. .cpu_prepare = amd_pmu_cpu_prepare,
  660. .cpu_starting = amd_pmu_cpu_starting,
  661. .cpu_dead = amd_pmu_cpu_dead,
  662. };
  663. static int setup_event_constraints(void)
  664. {
  665. if (boot_cpu_data.x86 == 0x15)
  666. x86_pmu.get_event_constraints = amd_get_event_constraints_f15h;
  667. return 0;
  668. }
  669. static int setup_perfctr_core(void)
  670. {
  671. if (!cpu_has_perfctr_core) {
  672. WARN(x86_pmu.get_event_constraints == amd_get_event_constraints_f15h,
  673. KERN_ERR "Odd, counter constraints enabled but no core perfctrs detected!");
  674. return -ENODEV;
  675. }
  676. WARN(x86_pmu.get_event_constraints == amd_get_event_constraints,
  677. KERN_ERR "hw perf events core counters need constraints handler!");
  678. /*
  679. * If core performance counter extensions exists, we must use
  680. * MSR_F15H_PERF_CTL/MSR_F15H_PERF_CTR msrs. See also
  681. * x86_pmu_addr_offset().
  682. */
  683. x86_pmu.eventsel = MSR_F15H_PERF_CTL;
  684. x86_pmu.perfctr = MSR_F15H_PERF_CTR;
  685. x86_pmu.num_counters = AMD64_NUM_COUNTERS_CORE;
  686. printk(KERN_INFO "perf: AMD core performance counters detected\n");
  687. return 0;
  688. }
  689. static int setup_perfctr_nb(void)
  690. {
  691. if (!cpu_has_perfctr_nb)
  692. return -ENODEV;
  693. x86_pmu.num_counters += AMD64_NUM_COUNTERS_NB;
  694. if (cpu_has_perfctr_core)
  695. amd_nb_event_constraint = &amd_NBPMC96;
  696. else
  697. amd_nb_event_constraint = &amd_NBPMC74;
  698. printk(KERN_INFO "perf: AMD northbridge performance counters detected\n");
  699. return 0;
  700. }
  701. __init int amd_pmu_init(void)
  702. {
  703. /* Performance-monitoring supported from K7 and later: */
  704. if (boot_cpu_data.x86 < 6)
  705. return -ENODEV;
  706. x86_pmu = amd_pmu;
  707. setup_event_constraints();
  708. setup_perfctr_core();
  709. setup_perfctr_nb();
  710. /* Events are common for all AMDs */
  711. memcpy(hw_cache_event_ids, amd_hw_cache_event_ids,
  712. sizeof(hw_cache_event_ids));
  713. return 0;
  714. }
  715. void amd_pmu_enable_virt(void)
  716. {
  717. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  718. cpuc->perf_ctr_virt_mask = 0;
  719. /* Reload all events */
  720. x86_pmu_disable_all();
  721. x86_pmu_enable_all(0);
  722. }
  723. EXPORT_SYMBOL_GPL(amd_pmu_enable_virt);
  724. void amd_pmu_disable_virt(void)
  725. {
  726. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  727. /*
  728. * We only mask out the Host-only bit so that host-only counting works
  729. * when SVM is disabled. If someone sets up a guest-only counter when
  730. * SVM is disabled the Guest-only bits still gets set and the counter
  731. * will not count anything.
  732. */
  733. cpuc->perf_ctr_virt_mask = AMD64_EVENTSEL_HOSTONLY;
  734. /* Reload all events */
  735. x86_pmu_disable_all();
  736. x86_pmu_enable_all(0);
  737. }
  738. EXPORT_SYMBOL_GPL(amd_pmu_disable_virt);