perf_event_amd.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #ifdef CONFIG_CPU_SUP_AMD
  2. static DEFINE_RAW_SPINLOCK(amd_nb_lock);
  3. static __initconst const u64 amd_hw_cache_event_ids
  4. [PERF_COUNT_HW_CACHE_MAX]
  5. [PERF_COUNT_HW_CACHE_OP_MAX]
  6. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  7. {
  8. [ C(L1D) ] = {
  9. [ C(OP_READ) ] = {
  10. [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
  11. [ C(RESULT_MISS) ] = 0x0041, /* Data Cache Misses */
  12. },
  13. [ C(OP_WRITE) ] = {
  14. [ C(RESULT_ACCESS) ] = 0x0142, /* Data Cache Refills :system */
  15. [ C(RESULT_MISS) ] = 0,
  16. },
  17. [ C(OP_PREFETCH) ] = {
  18. [ C(RESULT_ACCESS) ] = 0x0267, /* Data Prefetcher :attempts */
  19. [ C(RESULT_MISS) ] = 0x0167, /* Data Prefetcher :cancelled */
  20. },
  21. },
  22. [ C(L1I ) ] = {
  23. [ C(OP_READ) ] = {
  24. [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction cache fetches */
  25. [ C(RESULT_MISS) ] = 0x0081, /* Instruction cache misses */
  26. },
  27. [ C(OP_WRITE) ] = {
  28. [ C(RESULT_ACCESS) ] = -1,
  29. [ C(RESULT_MISS) ] = -1,
  30. },
  31. [ C(OP_PREFETCH) ] = {
  32. [ C(RESULT_ACCESS) ] = 0x014B, /* Prefetch Instructions :Load */
  33. [ C(RESULT_MISS) ] = 0,
  34. },
  35. },
  36. [ C(LL ) ] = {
  37. [ C(OP_READ) ] = {
  38. [ C(RESULT_ACCESS) ] = 0x037D, /* Requests to L2 Cache :IC+DC */
  39. [ C(RESULT_MISS) ] = 0x037E, /* L2 Cache Misses : IC+DC */
  40. },
  41. [ C(OP_WRITE) ] = {
  42. [ C(RESULT_ACCESS) ] = 0x017F, /* L2 Fill/Writeback */
  43. [ C(RESULT_MISS) ] = 0,
  44. },
  45. [ C(OP_PREFETCH) ] = {
  46. [ C(RESULT_ACCESS) ] = 0,
  47. [ C(RESULT_MISS) ] = 0,
  48. },
  49. },
  50. [ C(DTLB) ] = {
  51. [ C(OP_READ) ] = {
  52. [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
  53. [ C(RESULT_MISS) ] = 0x0046, /* L1 DTLB and L2 DLTB Miss */
  54. },
  55. [ C(OP_WRITE) ] = {
  56. [ C(RESULT_ACCESS) ] = 0,
  57. [ C(RESULT_MISS) ] = 0,
  58. },
  59. [ C(OP_PREFETCH) ] = {
  60. [ C(RESULT_ACCESS) ] = 0,
  61. [ C(RESULT_MISS) ] = 0,
  62. },
  63. },
  64. [ C(ITLB) ] = {
  65. [ C(OP_READ) ] = {
  66. [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction fecthes */
  67. [ C(RESULT_MISS) ] = 0x0085, /* Instr. fetch ITLB misses */
  68. },
  69. [ C(OP_WRITE) ] = {
  70. [ C(RESULT_ACCESS) ] = -1,
  71. [ C(RESULT_MISS) ] = -1,
  72. },
  73. [ C(OP_PREFETCH) ] = {
  74. [ C(RESULT_ACCESS) ] = -1,
  75. [ C(RESULT_MISS) ] = -1,
  76. },
  77. },
  78. [ C(BPU ) ] = {
  79. [ C(OP_READ) ] = {
  80. [ C(RESULT_ACCESS) ] = 0x00c2, /* Retired Branch Instr. */
  81. [ C(RESULT_MISS) ] = 0x00c3, /* Retired Mispredicted BI */
  82. },
  83. [ C(OP_WRITE) ] = {
  84. [ C(RESULT_ACCESS) ] = -1,
  85. [ C(RESULT_MISS) ] = -1,
  86. },
  87. [ C(OP_PREFETCH) ] = {
  88. [ C(RESULT_ACCESS) ] = -1,
  89. [ C(RESULT_MISS) ] = -1,
  90. },
  91. },
  92. };
  93. /*
  94. * AMD Performance Monitor K7 and later.
  95. */
  96. static const u64 amd_perfmon_event_map[] =
  97. {
  98. [PERF_COUNT_HW_CPU_CYCLES] = 0x0076,
  99. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  100. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0080,
  101. [PERF_COUNT_HW_CACHE_MISSES] = 0x0081,
  102. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c2,
  103. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c3,
  104. };
  105. static u64 amd_pmu_event_map(int hw_event)
  106. {
  107. return amd_perfmon_event_map[hw_event];
  108. }
  109. static int amd_pmu_hw_config(struct perf_event *event)
  110. {
  111. int ret = x86_pmu_hw_config(event);
  112. if (ret)
  113. return ret;
  114. if (event->attr.type != PERF_TYPE_RAW)
  115. return 0;
  116. event->hw.config |= event->attr.config & AMD64_RAW_EVENT_MASK;
  117. return 0;
  118. }
  119. /*
  120. * AMD64 events are detected based on their event codes.
  121. */
  122. static inline int amd_is_nb_event(struct hw_perf_event *hwc)
  123. {
  124. return (hwc->config & 0xe0) == 0xe0;
  125. }
  126. static inline int amd_has_nb(struct cpu_hw_events *cpuc)
  127. {
  128. struct amd_nb *nb = cpuc->amd_nb;
  129. return nb && nb->nb_id != -1;
  130. }
  131. static void amd_put_event_constraints(struct cpu_hw_events *cpuc,
  132. struct perf_event *event)
  133. {
  134. struct hw_perf_event *hwc = &event->hw;
  135. struct amd_nb *nb = cpuc->amd_nb;
  136. int i;
  137. /*
  138. * only care about NB events
  139. */
  140. if (!(amd_has_nb(cpuc) && amd_is_nb_event(hwc)))
  141. return;
  142. /*
  143. * need to scan whole list because event may not have
  144. * been assigned during scheduling
  145. *
  146. * no race condition possible because event can only
  147. * be removed on one CPU at a time AND PMU is disabled
  148. * when we come here
  149. */
  150. for (i = 0; i < x86_pmu.num_counters; i++) {
  151. if (nb->owners[i] == event) {
  152. cmpxchg(nb->owners+i, event, NULL);
  153. break;
  154. }
  155. }
  156. }
  157. /*
  158. * AMD64 NorthBridge events need special treatment because
  159. * counter access needs to be synchronized across all cores
  160. * of a package. Refer to BKDG section 3.12
  161. *
  162. * NB events are events measuring L3 cache, Hypertransport
  163. * traffic. They are identified by an event code >= 0xe00.
  164. * They measure events on the NorthBride which is shared
  165. * by all cores on a package. NB events are counted on a
  166. * shared set of counters. When a NB event is programmed
  167. * in a counter, the data actually comes from a shared
  168. * counter. Thus, access to those counters needs to be
  169. * synchronized.
  170. *
  171. * We implement the synchronization such that no two cores
  172. * can be measuring NB events using the same counters. Thus,
  173. * we maintain a per-NB allocation table. The available slot
  174. * is propagated using the event_constraint structure.
  175. *
  176. * We provide only one choice for each NB event based on
  177. * the fact that only NB events have restrictions. Consequently,
  178. * if a counter is available, there is a guarantee the NB event
  179. * will be assigned to it. If no slot is available, an empty
  180. * constraint is returned and scheduling will eventually fail
  181. * for this event.
  182. *
  183. * Note that all cores attached the same NB compete for the same
  184. * counters to host NB events, this is why we use atomic ops. Some
  185. * multi-chip CPUs may have more than one NB.
  186. *
  187. * Given that resources are allocated (cmpxchg), they must be
  188. * eventually freed for others to use. This is accomplished by
  189. * calling amd_put_event_constraints().
  190. *
  191. * Non NB events are not impacted by this restriction.
  192. */
  193. static struct event_constraint *
  194. amd_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  195. {
  196. struct hw_perf_event *hwc = &event->hw;
  197. struct amd_nb *nb = cpuc->amd_nb;
  198. struct perf_event *old = NULL;
  199. int max = x86_pmu.num_counters;
  200. int i, j, k = -1;
  201. /*
  202. * if not NB event or no NB, then no constraints
  203. */
  204. if (!(amd_has_nb(cpuc) && amd_is_nb_event(hwc)))
  205. return &unconstrained;
  206. /*
  207. * detect if already present, if so reuse
  208. *
  209. * cannot merge with actual allocation
  210. * because of possible holes
  211. *
  212. * event can already be present yet not assigned (in hwc->idx)
  213. * because of successive calls to x86_schedule_events() from
  214. * hw_perf_group_sched_in() without hw_perf_enable()
  215. */
  216. for (i = 0; i < max; i++) {
  217. /*
  218. * keep track of first free slot
  219. */
  220. if (k == -1 && !nb->owners[i])
  221. k = i;
  222. /* already present, reuse */
  223. if (nb->owners[i] == event)
  224. goto done;
  225. }
  226. /*
  227. * not present, so grab a new slot
  228. * starting either at:
  229. */
  230. if (hwc->idx != -1) {
  231. /* previous assignment */
  232. i = hwc->idx;
  233. } else if (k != -1) {
  234. /* start from free slot found */
  235. i = k;
  236. } else {
  237. /*
  238. * event not found, no slot found in
  239. * first pass, try again from the
  240. * beginning
  241. */
  242. i = 0;
  243. }
  244. j = i;
  245. do {
  246. old = cmpxchg(nb->owners+i, NULL, event);
  247. if (!old)
  248. break;
  249. if (++i == max)
  250. i = 0;
  251. } while (i != j);
  252. done:
  253. if (!old)
  254. return &nb->event_constraints[i];
  255. return &emptyconstraint;
  256. }
  257. static struct amd_nb *amd_alloc_nb(int cpu, int nb_id)
  258. {
  259. struct amd_nb *nb;
  260. int i;
  261. nb = kmalloc(sizeof(struct amd_nb), GFP_KERNEL);
  262. if (!nb)
  263. return NULL;
  264. memset(nb, 0, sizeof(*nb));
  265. nb->nb_id = nb_id;
  266. /*
  267. * initialize all possible NB constraints
  268. */
  269. for (i = 0; i < x86_pmu.num_counters; i++) {
  270. __set_bit(i, nb->event_constraints[i].idxmsk);
  271. nb->event_constraints[i].weight = 1;
  272. }
  273. return nb;
  274. }
  275. static int amd_pmu_cpu_prepare(int cpu)
  276. {
  277. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  278. WARN_ON_ONCE(cpuc->amd_nb);
  279. if (boot_cpu_data.x86_max_cores < 2)
  280. return NOTIFY_OK;
  281. cpuc->amd_nb = amd_alloc_nb(cpu, -1);
  282. if (!cpuc->amd_nb)
  283. return NOTIFY_BAD;
  284. return NOTIFY_OK;
  285. }
  286. static void amd_pmu_cpu_starting(int cpu)
  287. {
  288. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  289. struct amd_nb *nb;
  290. int i, nb_id;
  291. if (boot_cpu_data.x86_max_cores < 2)
  292. return;
  293. nb_id = amd_get_nb_id(cpu);
  294. WARN_ON_ONCE(nb_id == BAD_APICID);
  295. raw_spin_lock(&amd_nb_lock);
  296. for_each_online_cpu(i) {
  297. nb = per_cpu(cpu_hw_events, i).amd_nb;
  298. if (WARN_ON_ONCE(!nb))
  299. continue;
  300. if (nb->nb_id == nb_id) {
  301. kfree(cpuc->amd_nb);
  302. cpuc->amd_nb = nb;
  303. break;
  304. }
  305. }
  306. cpuc->amd_nb->nb_id = nb_id;
  307. cpuc->amd_nb->refcnt++;
  308. raw_spin_unlock(&amd_nb_lock);
  309. }
  310. static void amd_pmu_cpu_dead(int cpu)
  311. {
  312. struct cpu_hw_events *cpuhw;
  313. if (boot_cpu_data.x86_max_cores < 2)
  314. return;
  315. cpuhw = &per_cpu(cpu_hw_events, cpu);
  316. raw_spin_lock(&amd_nb_lock);
  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. raw_spin_unlock(&amd_nb_lock);
  324. }
  325. static __initconst const struct x86_pmu amd_pmu = {
  326. .name = "AMD",
  327. .handle_irq = x86_pmu_handle_irq,
  328. .disable_all = x86_pmu_disable_all,
  329. .enable_all = x86_pmu_enable_all,
  330. .enable = x86_pmu_enable_event,
  331. .disable = x86_pmu_disable_event,
  332. .hw_config = amd_pmu_hw_config,
  333. .schedule_events = x86_schedule_events,
  334. .eventsel = MSR_K7_EVNTSEL0,
  335. .perfctr = MSR_K7_PERFCTR0,
  336. .event_map = amd_pmu_event_map,
  337. .max_events = ARRAY_SIZE(amd_perfmon_event_map),
  338. .num_counters = 4,
  339. .cntval_bits = 48,
  340. .cntval_mask = (1ULL << 48) - 1,
  341. .apic = 1,
  342. /* use highest bit to detect overflow */
  343. .max_period = (1ULL << 47) - 1,
  344. .get_event_constraints = amd_get_event_constraints,
  345. .put_event_constraints = amd_put_event_constraints,
  346. .cpu_prepare = amd_pmu_cpu_prepare,
  347. .cpu_starting = amd_pmu_cpu_starting,
  348. .cpu_dead = amd_pmu_cpu_dead,
  349. };
  350. static __init int amd_pmu_init(void)
  351. {
  352. /* Performance-monitoring supported from K7 and later: */
  353. if (boot_cpu_data.x86 < 6)
  354. return -ENODEV;
  355. x86_pmu = amd_pmu;
  356. /* Events are common for all AMDs */
  357. memcpy(hw_cache_event_ids, amd_hw_cache_event_ids,
  358. sizeof(hw_cache_event_ids));
  359. return 0;
  360. }
  361. #else /* CONFIG_CPU_SUP_AMD */
  362. static int amd_pmu_init(void)
  363. {
  364. return 0;
  365. }
  366. #endif