perf_event_amd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. #ifdef CONFIG_CPU_SUP_AMD
  2. static __initconst const u64 amd_hw_cache_event_ids
  3. [PERF_COUNT_HW_CACHE_MAX]
  4. [PERF_COUNT_HW_CACHE_OP_MAX]
  5. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  6. {
  7. [ C(L1D) ] = {
  8. [ C(OP_READ) ] = {
  9. [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
  10. [ C(RESULT_MISS) ] = 0x0041, /* Data Cache Misses */
  11. },
  12. [ C(OP_WRITE) ] = {
  13. [ C(RESULT_ACCESS) ] = 0x0142, /* Data Cache Refills :system */
  14. [ C(RESULT_MISS) ] = 0,
  15. },
  16. [ C(OP_PREFETCH) ] = {
  17. [ C(RESULT_ACCESS) ] = 0x0267, /* Data Prefetcher :attempts */
  18. [ C(RESULT_MISS) ] = 0x0167, /* Data Prefetcher :cancelled */
  19. },
  20. },
  21. [ C(L1I ) ] = {
  22. [ C(OP_READ) ] = {
  23. [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction cache fetches */
  24. [ C(RESULT_MISS) ] = 0x0081, /* Instruction cache misses */
  25. },
  26. [ C(OP_WRITE) ] = {
  27. [ C(RESULT_ACCESS) ] = -1,
  28. [ C(RESULT_MISS) ] = -1,
  29. },
  30. [ C(OP_PREFETCH) ] = {
  31. [ C(RESULT_ACCESS) ] = 0x014B, /* Prefetch Instructions :Load */
  32. [ C(RESULT_MISS) ] = 0,
  33. },
  34. },
  35. [ C(LL ) ] = {
  36. [ C(OP_READ) ] = {
  37. [ C(RESULT_ACCESS) ] = 0x037D, /* Requests to L2 Cache :IC+DC */
  38. [ C(RESULT_MISS) ] = 0x037E, /* L2 Cache Misses : IC+DC */
  39. },
  40. [ C(OP_WRITE) ] = {
  41. [ C(RESULT_ACCESS) ] = 0x017F, /* L2 Fill/Writeback */
  42. [ C(RESULT_MISS) ] = 0,
  43. },
  44. [ C(OP_PREFETCH) ] = {
  45. [ C(RESULT_ACCESS) ] = 0,
  46. [ C(RESULT_MISS) ] = 0,
  47. },
  48. },
  49. [ C(DTLB) ] = {
  50. [ C(OP_READ) ] = {
  51. [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
  52. [ C(RESULT_MISS) ] = 0x0746, /* L1_DTLB_AND_L2_DLTB_MISS.ALL */
  53. },
  54. [ C(OP_WRITE) ] = {
  55. [ C(RESULT_ACCESS) ] = 0,
  56. [ C(RESULT_MISS) ] = 0,
  57. },
  58. [ C(OP_PREFETCH) ] = {
  59. [ C(RESULT_ACCESS) ] = 0,
  60. [ C(RESULT_MISS) ] = 0,
  61. },
  62. },
  63. [ C(ITLB) ] = {
  64. [ C(OP_READ) ] = {
  65. [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction fecthes */
  66. [ C(RESULT_MISS) ] = 0x0385, /* L1_ITLB_AND_L2_ITLB_MISS.ALL */
  67. },
  68. [ C(OP_WRITE) ] = {
  69. [ C(RESULT_ACCESS) ] = -1,
  70. [ C(RESULT_MISS) ] = -1,
  71. },
  72. [ C(OP_PREFETCH) ] = {
  73. [ C(RESULT_ACCESS) ] = -1,
  74. [ C(RESULT_MISS) ] = -1,
  75. },
  76. },
  77. [ C(BPU ) ] = {
  78. [ C(OP_READ) ] = {
  79. [ C(RESULT_ACCESS) ] = 0x00c2, /* Retired Branch Instr. */
  80. [ C(RESULT_MISS) ] = 0x00c3, /* Retired Mispredicted BI */
  81. },
  82. [ C(OP_WRITE) ] = {
  83. [ C(RESULT_ACCESS) ] = -1,
  84. [ C(RESULT_MISS) ] = -1,
  85. },
  86. [ C(OP_PREFETCH) ] = {
  87. [ C(RESULT_ACCESS) ] = -1,
  88. [ C(RESULT_MISS) ] = -1,
  89. },
  90. },
  91. };
  92. /*
  93. * AMD Performance Monitor K7 and later.
  94. */
  95. static const u64 amd_perfmon_event_map[] =
  96. {
  97. [PERF_COUNT_HW_CPU_CYCLES] = 0x0076,
  98. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  99. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0080,
  100. [PERF_COUNT_HW_CACHE_MISSES] = 0x0081,
  101. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c2,
  102. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c3,
  103. };
  104. static u64 amd_pmu_event_map(int hw_event)
  105. {
  106. return amd_perfmon_event_map[hw_event];
  107. }
  108. static int amd_pmu_hw_config(struct perf_event *event)
  109. {
  110. int ret = x86_pmu_hw_config(event);
  111. if (ret)
  112. return ret;
  113. if (event->attr.type != PERF_TYPE_RAW)
  114. return 0;
  115. event->hw.config |= event->attr.config & AMD64_RAW_EVENT_MASK;
  116. return 0;
  117. }
  118. /*
  119. * AMD64 events are detected based on their event codes.
  120. */
  121. static inline unsigned int amd_get_event_code(struct hw_perf_event *hwc)
  122. {
  123. return ((hwc->config >> 24) & 0x0f00) | (hwc->config & 0x00ff);
  124. }
  125. static inline int amd_is_nb_event(struct hw_perf_event *hwc)
  126. {
  127. return (hwc->config & 0xe0) == 0xe0;
  128. }
  129. static inline int amd_has_nb(struct cpu_hw_events *cpuc)
  130. {
  131. struct amd_nb *nb = cpuc->amd_nb;
  132. return nb && nb->nb_id != -1;
  133. }
  134. static void amd_put_event_constraints(struct cpu_hw_events *cpuc,
  135. struct perf_event *event)
  136. {
  137. struct hw_perf_event *hwc = &event->hw;
  138. struct amd_nb *nb = cpuc->amd_nb;
  139. int i;
  140. /*
  141. * only care about NB events
  142. */
  143. if (!(amd_has_nb(cpuc) && amd_is_nb_event(hwc)))
  144. return;
  145. /*
  146. * need to scan whole list because event may not have
  147. * been assigned during scheduling
  148. *
  149. * no race condition possible because event can only
  150. * be removed on one CPU at a time AND PMU is disabled
  151. * when we come here
  152. */
  153. for (i = 0; i < x86_pmu.num_counters; i++) {
  154. if (nb->owners[i] == event) {
  155. cmpxchg(nb->owners+i, event, NULL);
  156. break;
  157. }
  158. }
  159. }
  160. /*
  161. * AMD64 NorthBridge events need special treatment because
  162. * counter access needs to be synchronized across all cores
  163. * of a package. Refer to BKDG section 3.12
  164. *
  165. * NB events are events measuring L3 cache, Hypertransport
  166. * traffic. They are identified by an event code >= 0xe00.
  167. * They measure events on the NorthBride which is shared
  168. * by all cores on a package. NB events are counted on a
  169. * shared set of counters. When a NB event is programmed
  170. * in a counter, the data actually comes from a shared
  171. * counter. Thus, access to those counters needs to be
  172. * synchronized.
  173. *
  174. * We implement the synchronization such that no two cores
  175. * can be measuring NB events using the same counters. Thus,
  176. * we maintain a per-NB allocation table. The available slot
  177. * is propagated using the event_constraint structure.
  178. *
  179. * We provide only one choice for each NB event based on
  180. * the fact that only NB events have restrictions. Consequently,
  181. * if a counter is available, there is a guarantee the NB event
  182. * will be assigned to it. If no slot is available, an empty
  183. * constraint is returned and scheduling will eventually fail
  184. * for this event.
  185. *
  186. * Note that all cores attached the same NB compete for the same
  187. * counters to host NB events, this is why we use atomic ops. Some
  188. * multi-chip CPUs may have more than one NB.
  189. *
  190. * Given that resources are allocated (cmpxchg), they must be
  191. * eventually freed for others to use. This is accomplished by
  192. * calling amd_put_event_constraints().
  193. *
  194. * Non NB events are not impacted by this restriction.
  195. */
  196. static struct event_constraint *
  197. amd_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  198. {
  199. struct hw_perf_event *hwc = &event->hw;
  200. struct amd_nb *nb = cpuc->amd_nb;
  201. struct perf_event *old = NULL;
  202. int max = x86_pmu.num_counters;
  203. int i, j, k = -1;
  204. /*
  205. * if not NB event or no NB, then no constraints
  206. */
  207. if (!(amd_has_nb(cpuc) && amd_is_nb_event(hwc)))
  208. return &unconstrained;
  209. /*
  210. * detect if already present, if so reuse
  211. *
  212. * cannot merge with actual allocation
  213. * because of possible holes
  214. *
  215. * event can already be present yet not assigned (in hwc->idx)
  216. * because of successive calls to x86_schedule_events() from
  217. * hw_perf_group_sched_in() without hw_perf_enable()
  218. */
  219. for (i = 0; i < max; i++) {
  220. /*
  221. * keep track of first free slot
  222. */
  223. if (k == -1 && !nb->owners[i])
  224. k = i;
  225. /* already present, reuse */
  226. if (nb->owners[i] == event)
  227. goto done;
  228. }
  229. /*
  230. * not present, so grab a new slot
  231. * starting either at:
  232. */
  233. if (hwc->idx != -1) {
  234. /* previous assignment */
  235. i = hwc->idx;
  236. } else if (k != -1) {
  237. /* start from free slot found */
  238. i = k;
  239. } else {
  240. /*
  241. * event not found, no slot found in
  242. * first pass, try again from the
  243. * beginning
  244. */
  245. i = 0;
  246. }
  247. j = i;
  248. do {
  249. old = cmpxchg(nb->owners+i, NULL, event);
  250. if (!old)
  251. break;
  252. if (++i == max)
  253. i = 0;
  254. } while (i != j);
  255. done:
  256. if (!old)
  257. return &nb->event_constraints[i];
  258. return &emptyconstraint;
  259. }
  260. static struct amd_nb *amd_alloc_nb(int cpu)
  261. {
  262. struct amd_nb *nb;
  263. int i;
  264. nb = kmalloc_node(sizeof(struct amd_nb), GFP_KERNEL | __GFP_ZERO,
  265. cpu_to_node(cpu));
  266. if (!nb)
  267. return NULL;
  268. nb->nb_id = -1;
  269. /*
  270. * initialize all possible NB constraints
  271. */
  272. for (i = 0; i < x86_pmu.num_counters; i++) {
  273. __set_bit(i, nb->event_constraints[i].idxmsk);
  274. nb->event_constraints[i].weight = 1;
  275. }
  276. return nb;
  277. }
  278. static int amd_pmu_cpu_prepare(int cpu)
  279. {
  280. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  281. WARN_ON_ONCE(cpuc->amd_nb);
  282. if (boot_cpu_data.x86_max_cores < 2)
  283. return NOTIFY_OK;
  284. cpuc->amd_nb = amd_alloc_nb(cpu);
  285. if (!cpuc->amd_nb)
  286. return NOTIFY_BAD;
  287. return NOTIFY_OK;
  288. }
  289. static void amd_pmu_cpu_starting(int cpu)
  290. {
  291. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  292. struct amd_nb *nb;
  293. int i, nb_id;
  294. if (boot_cpu_data.x86_max_cores < 2)
  295. return;
  296. nb_id = amd_get_nb_id(cpu);
  297. WARN_ON_ONCE(nb_id == BAD_APICID);
  298. for_each_online_cpu(i) {
  299. nb = per_cpu(cpu_hw_events, i).amd_nb;
  300. if (WARN_ON_ONCE(!nb))
  301. continue;
  302. if (nb->nb_id == nb_id) {
  303. kfree(cpuc->amd_nb);
  304. cpuc->amd_nb = nb;
  305. break;
  306. }
  307. }
  308. cpuc->amd_nb->nb_id = nb_id;
  309. cpuc->amd_nb->refcnt++;
  310. }
  311. static void amd_pmu_cpu_dead(int cpu)
  312. {
  313. struct cpu_hw_events *cpuhw;
  314. if (boot_cpu_data.x86_max_cores < 2)
  315. return;
  316. cpuhw = &per_cpu(cpu_hw_events, cpu);
  317. if (cpuhw->amd_nb) {
  318. struct amd_nb *nb = cpuhw->amd_nb;
  319. if (nb->nb_id == -1 || --nb->refcnt == 0)
  320. kfree(nb);
  321. cpuhw->amd_nb = NULL;
  322. }
  323. }
  324. static __initconst const struct x86_pmu amd_pmu = {
  325. .name = "AMD",
  326. .handle_irq = x86_pmu_handle_irq,
  327. .disable_all = x86_pmu_disable_all,
  328. .enable_all = x86_pmu_enable_all,
  329. .enable = x86_pmu_enable_event,
  330. .disable = x86_pmu_disable_event,
  331. .hw_config = amd_pmu_hw_config,
  332. .schedule_events = x86_schedule_events,
  333. .eventsel = MSR_K7_EVNTSEL0,
  334. .perfctr = MSR_K7_PERFCTR0,
  335. .event_map = amd_pmu_event_map,
  336. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  337. .num_counters = 4,
  338. .cntval_bits = 48,
  339. .cntval_mask = (1ULL << 48) - 1,
  340. .apic = 1,
  341. /* use highest bit to detect overflow */
  342. .max_period = (1ULL << 47) - 1,
  343. .get_event_constraints = amd_get_event_constraints,
  344. .put_event_constraints = amd_put_event_constraints,
  345. .cpu_prepare = amd_pmu_cpu_prepare,
  346. .cpu_starting = amd_pmu_cpu_starting,
  347. .cpu_dead = amd_pmu_cpu_dead,
  348. };
  349. /* AMD Family 15h */
  350. #define AMD_EVENT_TYPE_MASK 0x000000F0ULL
  351. #define AMD_EVENT_FP 0x00000000ULL ... 0x00000010ULL
  352. #define AMD_EVENT_LS 0x00000020ULL ... 0x00000030ULL
  353. #define AMD_EVENT_DC 0x00000040ULL ... 0x00000050ULL
  354. #define AMD_EVENT_CU 0x00000060ULL ... 0x00000070ULL
  355. #define AMD_EVENT_IC_DE 0x00000080ULL ... 0x00000090ULL
  356. #define AMD_EVENT_EX_LS 0x000000C0ULL
  357. #define AMD_EVENT_DE 0x000000D0ULL
  358. #define AMD_EVENT_NB 0x000000E0ULL ... 0x000000F0ULL
  359. /*
  360. * AMD family 15h event code/PMC mappings:
  361. *
  362. * type = event_code & 0x0F0:
  363. *
  364. * 0x000 FP PERF_CTL[5:3]
  365. * 0x010 FP PERF_CTL[5:3]
  366. * 0x020 LS PERF_CTL[5:0]
  367. * 0x030 LS PERF_CTL[5:0]
  368. * 0x040 DC PERF_CTL[5:0]
  369. * 0x050 DC PERF_CTL[5:0]
  370. * 0x060 CU PERF_CTL[2:0]
  371. * 0x070 CU PERF_CTL[2:0]
  372. * 0x080 IC/DE PERF_CTL[2:0]
  373. * 0x090 IC/DE PERF_CTL[2:0]
  374. * 0x0A0 ---
  375. * 0x0B0 ---
  376. * 0x0C0 EX/LS PERF_CTL[5:0]
  377. * 0x0D0 DE PERF_CTL[2:0]
  378. * 0x0E0 NB NB_PERF_CTL[3:0]
  379. * 0x0F0 NB NB_PERF_CTL[3:0]
  380. *
  381. * Exceptions:
  382. *
  383. * 0x003 FP PERF_CTL[3]
  384. * 0x00B FP PERF_CTL[3]
  385. * 0x00D FP PERF_CTL[3]
  386. * 0x023 DE PERF_CTL[2:0]
  387. * 0x02D LS PERF_CTL[3]
  388. * 0x02E LS PERF_CTL[3,0]
  389. * 0x043 CU PERF_CTL[2:0]
  390. * 0x045 CU PERF_CTL[2:0]
  391. * 0x046 CU PERF_CTL[2:0]
  392. * 0x054 CU PERF_CTL[2:0]
  393. * 0x055 CU PERF_CTL[2:0]
  394. * 0x08F IC PERF_CTL[0]
  395. * 0x187 DE PERF_CTL[0]
  396. * 0x188 DE PERF_CTL[0]
  397. * 0x0DB EX PERF_CTL[5:0]
  398. * 0x0DC LS PERF_CTL[5:0]
  399. * 0x0DD LS PERF_CTL[5:0]
  400. * 0x0DE LS PERF_CTL[5:0]
  401. * 0x0DF LS PERF_CTL[5:0]
  402. * 0x1D6 EX PERF_CTL[5:0]
  403. * 0x1D8 EX PERF_CTL[5:0]
  404. */
  405. static struct event_constraint amd_f15_PMC0 = EVENT_CONSTRAINT(0, 0x01, 0);
  406. static struct event_constraint amd_f15_PMC20 = EVENT_CONSTRAINT(0, 0x07, 0);
  407. static struct event_constraint amd_f15_PMC3 = EVENT_CONSTRAINT(0, 0x08, 0);
  408. static struct event_constraint amd_f15_PMC30 = EVENT_CONSTRAINT(0, 0x09, 0);
  409. static struct event_constraint amd_f15_PMC50 = EVENT_CONSTRAINT(0, 0x3F, 0);
  410. static struct event_constraint amd_f15_PMC53 = EVENT_CONSTRAINT(0, 0x38, 0);
  411. static struct event_constraint *
  412. amd_get_event_constraints_f15h(struct cpu_hw_events *cpuc, struct perf_event *event)
  413. {
  414. unsigned int event_code = amd_get_event_code(&event->hw);
  415. switch (event_code & AMD_EVENT_TYPE_MASK) {
  416. case AMD_EVENT_FP:
  417. switch (event_code) {
  418. case 0x003:
  419. case 0x00B:
  420. case 0x00D:
  421. return &amd_f15_PMC3;
  422. default:
  423. return &amd_f15_PMC53;
  424. }
  425. case AMD_EVENT_LS:
  426. case AMD_EVENT_DC:
  427. case AMD_EVENT_EX_LS:
  428. switch (event_code) {
  429. case 0x023:
  430. case 0x043:
  431. case 0x045:
  432. case 0x046:
  433. case 0x054:
  434. case 0x055:
  435. return &amd_f15_PMC20;
  436. case 0x02D:
  437. return &amd_f15_PMC3;
  438. case 0x02E:
  439. return &amd_f15_PMC30;
  440. default:
  441. return &amd_f15_PMC50;
  442. }
  443. case AMD_EVENT_CU:
  444. case AMD_EVENT_IC_DE:
  445. case AMD_EVENT_DE:
  446. switch (event_code) {
  447. case 0x08F:
  448. case 0x187:
  449. case 0x188:
  450. return &amd_f15_PMC0;
  451. case 0x0DB ... 0x0DF:
  452. case 0x1D6:
  453. case 0x1D8:
  454. return &amd_f15_PMC50;
  455. default:
  456. return &amd_f15_PMC20;
  457. }
  458. case AMD_EVENT_NB:
  459. /* not yet implemented */
  460. return &emptyconstraint;
  461. default:
  462. return &emptyconstraint;
  463. }
  464. }
  465. static __initconst const struct x86_pmu amd_pmu_f15h = {
  466. .name = "AMD Family 15h",
  467. .handle_irq = x86_pmu_handle_irq,
  468. .disable_all = x86_pmu_disable_all,
  469. .enable_all = x86_pmu_enable_all,
  470. .enable = x86_pmu_enable_event,
  471. .disable = x86_pmu_disable_event,
  472. .hw_config = amd_pmu_hw_config,
  473. .schedule_events = x86_schedule_events,
  474. .eventsel = MSR_F15H_PERF_CTL,
  475. .perfctr = MSR_F15H_PERF_CTR,
  476. .event_map = amd_pmu_event_map,
  477. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  478. .num_counters = 6,
  479. .cntval_bits = 48,
  480. .cntval_mask = (1ULL << 48) - 1,
  481. .apic = 1,
  482. /* use highest bit to detect overflow */
  483. .max_period = (1ULL << 47) - 1,
  484. .get_event_constraints = amd_get_event_constraints_f15h,
  485. /* nortbridge counters not yet implemented: */
  486. #if 0
  487. .put_event_constraints = amd_put_event_constraints,
  488. .cpu_prepare = amd_pmu_cpu_prepare,
  489. .cpu_starting = amd_pmu_cpu_starting,
  490. .cpu_dead = amd_pmu_cpu_dead,
  491. #endif
  492. };
  493. static __init int amd_pmu_init(void)
  494. {
  495. /* Performance-monitoring supported from K7 and later: */
  496. if (boot_cpu_data.x86 < 6)
  497. return -ENODEV;
  498. /*
  499. * If core performance counter extensions exists, it must be
  500. * family 15h, otherwise fail. See x86_pmu_addr_offset().
  501. */
  502. switch (boot_cpu_data.x86) {
  503. case 0x15:
  504. if (!cpu_has_perfctr_core)
  505. return -ENODEV;
  506. x86_pmu = amd_pmu_f15h;
  507. break;
  508. default:
  509. if (cpu_has_perfctr_core)
  510. return -ENODEV;
  511. x86_pmu = amd_pmu;
  512. break;
  513. }
  514. /* Events are common for all AMDs */
  515. memcpy(hw_cache_event_ids, amd_hw_cache_event_ids,
  516. sizeof(hw_cache_event_ids));
  517. return 0;
  518. }
  519. #else /* CONFIG_CPU_SUP_AMD */
  520. static int amd_pmu_init(void)
  521. {
  522. return 0;
  523. }
  524. #endif