uncore.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * Copyright (C) 2013 Advanced Micro Devices, Inc.
  3. *
  4. * Author: Jacob Shin <jacob.shin@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/perf_event.h>
  11. #include <linux/percpu.h>
  12. #include <linux/types.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/cpu.h>
  16. #include <linux/cpumask.h>
  17. #include <asm/cpufeature.h>
  18. #include <asm/perf_event.h>
  19. #include <asm/msr.h>
  20. #include <asm/smp.h>
  21. #define NUM_COUNTERS_NB 4
  22. #define NUM_COUNTERS_L2 4
  23. #define NUM_COUNTERS_L3 6
  24. #define MAX_COUNTERS 6
  25. #define RDPMC_BASE_NB 6
  26. #define RDPMC_BASE_LLC 10
  27. #define COUNTER_SHIFT 16
  28. #undef pr_fmt
  29. #define pr_fmt(fmt) "amd_uncore: " fmt
  30. static int num_counters_llc;
  31. static int num_counters_nb;
  32. static bool l3_mask;
  33. static HLIST_HEAD(uncore_unused_list);
  34. struct amd_uncore {
  35. int id;
  36. int refcnt;
  37. int cpu;
  38. int num_counters;
  39. int rdpmc_base;
  40. u32 msr_base;
  41. cpumask_t *active_mask;
  42. struct pmu *pmu;
  43. struct perf_event *events[MAX_COUNTERS];
  44. struct hlist_node node;
  45. };
  46. static struct amd_uncore * __percpu *amd_uncore_nb;
  47. static struct amd_uncore * __percpu *amd_uncore_llc;
  48. static struct pmu amd_nb_pmu;
  49. static struct pmu amd_llc_pmu;
  50. static cpumask_t amd_nb_active_mask;
  51. static cpumask_t amd_llc_active_mask;
  52. static bool is_nb_event(struct perf_event *event)
  53. {
  54. return event->pmu->type == amd_nb_pmu.type;
  55. }
  56. static bool is_llc_event(struct perf_event *event)
  57. {
  58. return event->pmu->type == amd_llc_pmu.type;
  59. }
  60. static struct amd_uncore *event_to_amd_uncore(struct perf_event *event)
  61. {
  62. if (is_nb_event(event) && amd_uncore_nb)
  63. return *per_cpu_ptr(amd_uncore_nb, event->cpu);
  64. else if (is_llc_event(event) && amd_uncore_llc)
  65. return *per_cpu_ptr(amd_uncore_llc, event->cpu);
  66. return NULL;
  67. }
  68. static void amd_uncore_read(struct perf_event *event)
  69. {
  70. struct hw_perf_event *hwc = &event->hw;
  71. u64 prev, new;
  72. s64 delta;
  73. /*
  74. * since we do not enable counter overflow interrupts,
  75. * we do not have to worry about prev_count changing on us
  76. */
  77. prev = local64_read(&hwc->prev_count);
  78. rdpmcl(hwc->event_base_rdpmc, new);
  79. local64_set(&hwc->prev_count, new);
  80. delta = (new << COUNTER_SHIFT) - (prev << COUNTER_SHIFT);
  81. delta >>= COUNTER_SHIFT;
  82. local64_add(delta, &event->count);
  83. }
  84. static void amd_uncore_start(struct perf_event *event, int flags)
  85. {
  86. struct hw_perf_event *hwc = &event->hw;
  87. if (flags & PERF_EF_RELOAD)
  88. wrmsrl(hwc->event_base, (u64)local64_read(&hwc->prev_count));
  89. hwc->state = 0;
  90. wrmsrl(hwc->config_base, (hwc->config | ARCH_PERFMON_EVENTSEL_ENABLE));
  91. perf_event_update_userpage(event);
  92. }
  93. static void amd_uncore_stop(struct perf_event *event, int flags)
  94. {
  95. struct hw_perf_event *hwc = &event->hw;
  96. wrmsrl(hwc->config_base, hwc->config);
  97. hwc->state |= PERF_HES_STOPPED;
  98. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  99. amd_uncore_read(event);
  100. hwc->state |= PERF_HES_UPTODATE;
  101. }
  102. }
  103. static int amd_uncore_add(struct perf_event *event, int flags)
  104. {
  105. int i;
  106. struct amd_uncore *uncore = event_to_amd_uncore(event);
  107. struct hw_perf_event *hwc = &event->hw;
  108. /* are we already assigned? */
  109. if (hwc->idx != -1 && uncore->events[hwc->idx] == event)
  110. goto out;
  111. for (i = 0; i < uncore->num_counters; i++) {
  112. if (uncore->events[i] == event) {
  113. hwc->idx = i;
  114. goto out;
  115. }
  116. }
  117. /* if not, take the first available counter */
  118. hwc->idx = -1;
  119. for (i = 0; i < uncore->num_counters; i++) {
  120. if (cmpxchg(&uncore->events[i], NULL, event) == NULL) {
  121. hwc->idx = i;
  122. break;
  123. }
  124. }
  125. out:
  126. if (hwc->idx == -1)
  127. return -EBUSY;
  128. hwc->config_base = uncore->msr_base + (2 * hwc->idx);
  129. hwc->event_base = uncore->msr_base + 1 + (2 * hwc->idx);
  130. hwc->event_base_rdpmc = uncore->rdpmc_base + hwc->idx;
  131. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  132. if (flags & PERF_EF_START)
  133. amd_uncore_start(event, PERF_EF_RELOAD);
  134. return 0;
  135. }
  136. static void amd_uncore_del(struct perf_event *event, int flags)
  137. {
  138. int i;
  139. struct amd_uncore *uncore = event_to_amd_uncore(event);
  140. struct hw_perf_event *hwc = &event->hw;
  141. amd_uncore_stop(event, PERF_EF_UPDATE);
  142. for (i = 0; i < uncore->num_counters; i++) {
  143. if (cmpxchg(&uncore->events[i], event, NULL) == event)
  144. break;
  145. }
  146. hwc->idx = -1;
  147. }
  148. static int amd_uncore_event_init(struct perf_event *event)
  149. {
  150. struct amd_uncore *uncore;
  151. struct hw_perf_event *hwc = &event->hw;
  152. if (event->attr.type != event->pmu->type)
  153. return -ENOENT;
  154. /*
  155. * NB and Last level cache counters (MSRs) are shared across all cores
  156. * that share the same NB / Last level cache. On family 16h and below,
  157. * Interrupts can be directed to a single target core, however, event
  158. * counts generated by processes running on other cores cannot be masked
  159. * out. So we do not support sampling and per-thread events via
  160. * CAP_NO_INTERRUPT, and we do not enable counter overflow interrupts:
  161. */
  162. /* NB and Last level cache counters do not have usr/os/guest/host bits */
  163. if (event->attr.exclude_user || event->attr.exclude_kernel ||
  164. event->attr.exclude_host || event->attr.exclude_guest)
  165. return -EINVAL;
  166. hwc->config = event->attr.config & AMD64_RAW_EVENT_MASK_NB;
  167. hwc->idx = -1;
  168. if (event->cpu < 0)
  169. return -EINVAL;
  170. /*
  171. * SliceMask and ThreadMask need to be set for certain L3 events in
  172. * Family 17h. For other events, the two fields do not affect the count.
  173. */
  174. if (l3_mask && is_llc_event(event)) {
  175. int thread = 2 * (cpu_data(event->cpu).cpu_core_id % 4);
  176. if (smp_num_siblings > 1)
  177. thread += cpu_data(event->cpu).apicid & 1;
  178. hwc->config |= (1ULL << (AMD64_L3_THREAD_SHIFT + thread) &
  179. AMD64_L3_THREAD_MASK) | AMD64_L3_SLICE_MASK;
  180. }
  181. uncore = event_to_amd_uncore(event);
  182. if (!uncore)
  183. return -ENODEV;
  184. /*
  185. * since request can come in to any of the shared cores, we will remap
  186. * to a single common cpu.
  187. */
  188. event->cpu = uncore->cpu;
  189. return 0;
  190. }
  191. static ssize_t amd_uncore_attr_show_cpumask(struct device *dev,
  192. struct device_attribute *attr,
  193. char *buf)
  194. {
  195. cpumask_t *active_mask;
  196. struct pmu *pmu = dev_get_drvdata(dev);
  197. if (pmu->type == amd_nb_pmu.type)
  198. active_mask = &amd_nb_active_mask;
  199. else if (pmu->type == amd_llc_pmu.type)
  200. active_mask = &amd_llc_active_mask;
  201. else
  202. return 0;
  203. return cpumap_print_to_pagebuf(true, buf, active_mask);
  204. }
  205. static DEVICE_ATTR(cpumask, S_IRUGO, amd_uncore_attr_show_cpumask, NULL);
  206. static struct attribute *amd_uncore_attrs[] = {
  207. &dev_attr_cpumask.attr,
  208. NULL,
  209. };
  210. static struct attribute_group amd_uncore_attr_group = {
  211. .attrs = amd_uncore_attrs,
  212. };
  213. /*
  214. * Similar to PMU_FORMAT_ATTR but allowing for format_attr to be assigned based
  215. * on family
  216. */
  217. #define AMD_FORMAT_ATTR(_dev, _name, _format) \
  218. static ssize_t \
  219. _dev##_show##_name(struct device *dev, \
  220. struct device_attribute *attr, \
  221. char *page) \
  222. { \
  223. BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
  224. return sprintf(page, _format "\n"); \
  225. } \
  226. static struct device_attribute format_attr_##_dev##_name = __ATTR_RO(_dev);
  227. /* Used for each uncore counter type */
  228. #define AMD_ATTRIBUTE(_name) \
  229. static struct attribute *amd_uncore_format_attr_##_name[] = { \
  230. &format_attr_event_##_name.attr, \
  231. &format_attr_umask.attr, \
  232. NULL, \
  233. }; \
  234. static struct attribute_group amd_uncore_format_group_##_name = { \
  235. .name = "format", \
  236. .attrs = amd_uncore_format_attr_##_name, \
  237. }; \
  238. static const struct attribute_group *amd_uncore_attr_groups_##_name[] = { \
  239. &amd_uncore_attr_group, \
  240. &amd_uncore_format_group_##_name, \
  241. NULL, \
  242. };
  243. AMD_FORMAT_ATTR(event, , "config:0-7,32-35");
  244. AMD_FORMAT_ATTR(umask, , "config:8-15");
  245. AMD_FORMAT_ATTR(event, _df, "config:0-7,32-35,59-60");
  246. AMD_FORMAT_ATTR(event, _l3, "config:0-7");
  247. AMD_ATTRIBUTE(df);
  248. AMD_ATTRIBUTE(l3);
  249. static struct pmu amd_nb_pmu = {
  250. .task_ctx_nr = perf_invalid_context,
  251. .event_init = amd_uncore_event_init,
  252. .add = amd_uncore_add,
  253. .del = amd_uncore_del,
  254. .start = amd_uncore_start,
  255. .stop = amd_uncore_stop,
  256. .read = amd_uncore_read,
  257. .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
  258. };
  259. static struct pmu amd_llc_pmu = {
  260. .task_ctx_nr = perf_invalid_context,
  261. .event_init = amd_uncore_event_init,
  262. .add = amd_uncore_add,
  263. .del = amd_uncore_del,
  264. .start = amd_uncore_start,
  265. .stop = amd_uncore_stop,
  266. .read = amd_uncore_read,
  267. .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
  268. };
  269. static struct amd_uncore *amd_uncore_alloc(unsigned int cpu)
  270. {
  271. return kzalloc_node(sizeof(struct amd_uncore), GFP_KERNEL,
  272. cpu_to_node(cpu));
  273. }
  274. static int amd_uncore_cpu_up_prepare(unsigned int cpu)
  275. {
  276. struct amd_uncore *uncore_nb = NULL, *uncore_llc;
  277. if (amd_uncore_nb) {
  278. uncore_nb = amd_uncore_alloc(cpu);
  279. if (!uncore_nb)
  280. goto fail;
  281. uncore_nb->cpu = cpu;
  282. uncore_nb->num_counters = num_counters_nb;
  283. uncore_nb->rdpmc_base = RDPMC_BASE_NB;
  284. uncore_nb->msr_base = MSR_F15H_NB_PERF_CTL;
  285. uncore_nb->active_mask = &amd_nb_active_mask;
  286. uncore_nb->pmu = &amd_nb_pmu;
  287. uncore_nb->id = -1;
  288. *per_cpu_ptr(amd_uncore_nb, cpu) = uncore_nb;
  289. }
  290. if (amd_uncore_llc) {
  291. uncore_llc = amd_uncore_alloc(cpu);
  292. if (!uncore_llc)
  293. goto fail;
  294. uncore_llc->cpu = cpu;
  295. uncore_llc->num_counters = num_counters_llc;
  296. uncore_llc->rdpmc_base = RDPMC_BASE_LLC;
  297. uncore_llc->msr_base = MSR_F16H_L2I_PERF_CTL;
  298. uncore_llc->active_mask = &amd_llc_active_mask;
  299. uncore_llc->pmu = &amd_llc_pmu;
  300. uncore_llc->id = -1;
  301. *per_cpu_ptr(amd_uncore_llc, cpu) = uncore_llc;
  302. }
  303. return 0;
  304. fail:
  305. if (amd_uncore_nb)
  306. *per_cpu_ptr(amd_uncore_nb, cpu) = NULL;
  307. kfree(uncore_nb);
  308. return -ENOMEM;
  309. }
  310. static struct amd_uncore *
  311. amd_uncore_find_online_sibling(struct amd_uncore *this,
  312. struct amd_uncore * __percpu *uncores)
  313. {
  314. unsigned int cpu;
  315. struct amd_uncore *that;
  316. for_each_online_cpu(cpu) {
  317. that = *per_cpu_ptr(uncores, cpu);
  318. if (!that)
  319. continue;
  320. if (this == that)
  321. continue;
  322. if (this->id == that->id) {
  323. hlist_add_head(&this->node, &uncore_unused_list);
  324. this = that;
  325. break;
  326. }
  327. }
  328. this->refcnt++;
  329. return this;
  330. }
  331. static int amd_uncore_cpu_starting(unsigned int cpu)
  332. {
  333. unsigned int eax, ebx, ecx, edx;
  334. struct amd_uncore *uncore;
  335. if (amd_uncore_nb) {
  336. uncore = *per_cpu_ptr(amd_uncore_nb, cpu);
  337. cpuid(0x8000001e, &eax, &ebx, &ecx, &edx);
  338. uncore->id = ecx & 0xff;
  339. uncore = amd_uncore_find_online_sibling(uncore, amd_uncore_nb);
  340. *per_cpu_ptr(amd_uncore_nb, cpu) = uncore;
  341. }
  342. if (amd_uncore_llc) {
  343. uncore = *per_cpu_ptr(amd_uncore_llc, cpu);
  344. uncore->id = per_cpu(cpu_llc_id, cpu);
  345. uncore = amd_uncore_find_online_sibling(uncore, amd_uncore_llc);
  346. *per_cpu_ptr(amd_uncore_llc, cpu) = uncore;
  347. }
  348. return 0;
  349. }
  350. static void uncore_clean_online(void)
  351. {
  352. struct amd_uncore *uncore;
  353. struct hlist_node *n;
  354. hlist_for_each_entry_safe(uncore, n, &uncore_unused_list, node) {
  355. hlist_del(&uncore->node);
  356. kfree(uncore);
  357. }
  358. }
  359. static void uncore_online(unsigned int cpu,
  360. struct amd_uncore * __percpu *uncores)
  361. {
  362. struct amd_uncore *uncore = *per_cpu_ptr(uncores, cpu);
  363. uncore_clean_online();
  364. if (cpu == uncore->cpu)
  365. cpumask_set_cpu(cpu, uncore->active_mask);
  366. }
  367. static int amd_uncore_cpu_online(unsigned int cpu)
  368. {
  369. if (amd_uncore_nb)
  370. uncore_online(cpu, amd_uncore_nb);
  371. if (amd_uncore_llc)
  372. uncore_online(cpu, amd_uncore_llc);
  373. return 0;
  374. }
  375. static void uncore_down_prepare(unsigned int cpu,
  376. struct amd_uncore * __percpu *uncores)
  377. {
  378. unsigned int i;
  379. struct amd_uncore *this = *per_cpu_ptr(uncores, cpu);
  380. if (this->cpu != cpu)
  381. return;
  382. /* this cpu is going down, migrate to a shared sibling if possible */
  383. for_each_online_cpu(i) {
  384. struct amd_uncore *that = *per_cpu_ptr(uncores, i);
  385. if (cpu == i)
  386. continue;
  387. if (this == that) {
  388. perf_pmu_migrate_context(this->pmu, cpu, i);
  389. cpumask_clear_cpu(cpu, that->active_mask);
  390. cpumask_set_cpu(i, that->active_mask);
  391. that->cpu = i;
  392. break;
  393. }
  394. }
  395. }
  396. static int amd_uncore_cpu_down_prepare(unsigned int cpu)
  397. {
  398. if (amd_uncore_nb)
  399. uncore_down_prepare(cpu, amd_uncore_nb);
  400. if (amd_uncore_llc)
  401. uncore_down_prepare(cpu, amd_uncore_llc);
  402. return 0;
  403. }
  404. static void uncore_dead(unsigned int cpu, struct amd_uncore * __percpu *uncores)
  405. {
  406. struct amd_uncore *uncore = *per_cpu_ptr(uncores, cpu);
  407. if (cpu == uncore->cpu)
  408. cpumask_clear_cpu(cpu, uncore->active_mask);
  409. if (!--uncore->refcnt)
  410. kfree(uncore);
  411. *per_cpu_ptr(uncores, cpu) = NULL;
  412. }
  413. static int amd_uncore_cpu_dead(unsigned int cpu)
  414. {
  415. if (amd_uncore_nb)
  416. uncore_dead(cpu, amd_uncore_nb);
  417. if (amd_uncore_llc)
  418. uncore_dead(cpu, amd_uncore_llc);
  419. return 0;
  420. }
  421. static int __init amd_uncore_init(void)
  422. {
  423. int ret = -ENODEV;
  424. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  425. return -ENODEV;
  426. if (!boot_cpu_has(X86_FEATURE_TOPOEXT))
  427. return -ENODEV;
  428. if (boot_cpu_data.x86 == 0x17) {
  429. /*
  430. * For F17h, the Northbridge counters are repurposed as Data
  431. * Fabric counters. Also, L3 counters are supported too. The PMUs
  432. * are exported based on family as either L2 or L3 and NB or DF.
  433. */
  434. num_counters_nb = NUM_COUNTERS_NB;
  435. num_counters_llc = NUM_COUNTERS_L3;
  436. amd_nb_pmu.name = "amd_df";
  437. amd_llc_pmu.name = "amd_l3";
  438. format_attr_event_df.show = &event_show_df;
  439. format_attr_event_l3.show = &event_show_l3;
  440. l3_mask = true;
  441. } else {
  442. num_counters_nb = NUM_COUNTERS_NB;
  443. num_counters_llc = NUM_COUNTERS_L2;
  444. amd_nb_pmu.name = "amd_nb";
  445. amd_llc_pmu.name = "amd_l2";
  446. format_attr_event_df = format_attr_event;
  447. format_attr_event_l3 = format_attr_event;
  448. l3_mask = false;
  449. }
  450. amd_nb_pmu.attr_groups = amd_uncore_attr_groups_df;
  451. amd_llc_pmu.attr_groups = amd_uncore_attr_groups_l3;
  452. if (boot_cpu_has(X86_FEATURE_PERFCTR_NB)) {
  453. amd_uncore_nb = alloc_percpu(struct amd_uncore *);
  454. if (!amd_uncore_nb) {
  455. ret = -ENOMEM;
  456. goto fail_nb;
  457. }
  458. ret = perf_pmu_register(&amd_nb_pmu, amd_nb_pmu.name, -1);
  459. if (ret)
  460. goto fail_nb;
  461. pr_info("AMD NB counters detected\n");
  462. ret = 0;
  463. }
  464. if (boot_cpu_has(X86_FEATURE_PERFCTR_LLC)) {
  465. amd_uncore_llc = alloc_percpu(struct amd_uncore *);
  466. if (!amd_uncore_llc) {
  467. ret = -ENOMEM;
  468. goto fail_llc;
  469. }
  470. ret = perf_pmu_register(&amd_llc_pmu, amd_llc_pmu.name, -1);
  471. if (ret)
  472. goto fail_llc;
  473. pr_info("AMD LLC counters detected\n");
  474. ret = 0;
  475. }
  476. /*
  477. * Install callbacks. Core will call them for each online cpu.
  478. */
  479. if (cpuhp_setup_state(CPUHP_PERF_X86_AMD_UNCORE_PREP,
  480. "perf/x86/amd/uncore:prepare",
  481. amd_uncore_cpu_up_prepare, amd_uncore_cpu_dead))
  482. goto fail_llc;
  483. if (cpuhp_setup_state(CPUHP_AP_PERF_X86_AMD_UNCORE_STARTING,
  484. "perf/x86/amd/uncore:starting",
  485. amd_uncore_cpu_starting, NULL))
  486. goto fail_prep;
  487. if (cpuhp_setup_state(CPUHP_AP_PERF_X86_AMD_UNCORE_ONLINE,
  488. "perf/x86/amd/uncore:online",
  489. amd_uncore_cpu_online,
  490. amd_uncore_cpu_down_prepare))
  491. goto fail_start;
  492. return 0;
  493. fail_start:
  494. cpuhp_remove_state(CPUHP_AP_PERF_X86_AMD_UNCORE_STARTING);
  495. fail_prep:
  496. cpuhp_remove_state(CPUHP_PERF_X86_AMD_UNCORE_PREP);
  497. fail_llc:
  498. if (boot_cpu_has(X86_FEATURE_PERFCTR_NB))
  499. perf_pmu_unregister(&amd_nb_pmu);
  500. if (amd_uncore_llc)
  501. free_percpu(amd_uncore_llc);
  502. fail_nb:
  503. if (amd_uncore_nb)
  504. free_percpu(amd_uncore_nb);
  505. return ret;
  506. }
  507. device_initcall(amd_uncore_init);