perf_event_amd_iommu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Copyright (C) 2013 Advanced Micro Devices, Inc.
  3. *
  4. * Author: Steven Kinney <Steven.Kinney@amd.com>
  5. * Author: Suravee Suthikulpanit <Suraveee.Suthikulpanit@amd.com>
  6. *
  7. * Perf: amd_iommu - AMD IOMMU Performance Counter PMU implementation
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/perf_event.h>
  14. #include <linux/module.h>
  15. #include <linux/cpumask.h>
  16. #include <linux/slab.h>
  17. #include "perf_event.h"
  18. #include "perf_event_amd_iommu.h"
  19. #define COUNTER_SHIFT 16
  20. #define _GET_BANK(ev) ((u8)(ev->hw.extra_reg.reg >> 8))
  21. #define _GET_CNTR(ev) ((u8)(ev->hw.extra_reg.reg))
  22. /* iommu pmu config masks */
  23. #define _GET_CSOURCE(ev) ((ev->hw.config & 0xFFULL))
  24. #define _GET_DEVID(ev) ((ev->hw.config >> 8) & 0xFFFFULL)
  25. #define _GET_PASID(ev) ((ev->hw.config >> 24) & 0xFFFFULL)
  26. #define _GET_DOMID(ev) ((ev->hw.config >> 40) & 0xFFFFULL)
  27. #define _GET_DEVID_MASK(ev) ((ev->hw.extra_reg.config) & 0xFFFFULL)
  28. #define _GET_PASID_MASK(ev) ((ev->hw.extra_reg.config >> 16) & 0xFFFFULL)
  29. #define _GET_DOMID_MASK(ev) ((ev->hw.extra_reg.config >> 32) & 0xFFFFULL)
  30. static struct perf_amd_iommu __perf_iommu;
  31. struct perf_amd_iommu {
  32. struct pmu pmu;
  33. u8 max_banks;
  34. u8 max_counters;
  35. u64 cntr_assign_mask;
  36. raw_spinlock_t lock;
  37. const struct attribute_group *attr_groups[4];
  38. };
  39. #define format_group attr_groups[0]
  40. #define cpumask_group attr_groups[1]
  41. #define events_group attr_groups[2]
  42. #define null_group attr_groups[3]
  43. /*---------------------------------------------
  44. * sysfs format attributes
  45. *---------------------------------------------*/
  46. PMU_FORMAT_ATTR(csource, "config:0-7");
  47. PMU_FORMAT_ATTR(devid, "config:8-23");
  48. PMU_FORMAT_ATTR(pasid, "config:24-39");
  49. PMU_FORMAT_ATTR(domid, "config:40-55");
  50. PMU_FORMAT_ATTR(devid_mask, "config1:0-15");
  51. PMU_FORMAT_ATTR(pasid_mask, "config1:16-31");
  52. PMU_FORMAT_ATTR(domid_mask, "config1:32-47");
  53. static struct attribute *iommu_format_attrs[] = {
  54. &format_attr_csource.attr,
  55. &format_attr_devid.attr,
  56. &format_attr_pasid.attr,
  57. &format_attr_domid.attr,
  58. &format_attr_devid_mask.attr,
  59. &format_attr_pasid_mask.attr,
  60. &format_attr_domid_mask.attr,
  61. NULL,
  62. };
  63. static struct attribute_group amd_iommu_format_group = {
  64. .name = "format",
  65. .attrs = iommu_format_attrs,
  66. };
  67. /*---------------------------------------------
  68. * sysfs events attributes
  69. *---------------------------------------------*/
  70. struct amd_iommu_event_desc {
  71. struct kobj_attribute attr;
  72. const char *event;
  73. };
  74. static ssize_t _iommu_event_show(struct kobject *kobj,
  75. struct kobj_attribute *attr, char *buf)
  76. {
  77. struct amd_iommu_event_desc *event =
  78. container_of(attr, struct amd_iommu_event_desc, attr);
  79. return sprintf(buf, "%s\n", event->event);
  80. }
  81. #define AMD_IOMMU_EVENT_DESC(_name, _event) \
  82. { \
  83. .attr = __ATTR(_name, 0444, _iommu_event_show, NULL), \
  84. .event = _event, \
  85. }
  86. static struct amd_iommu_event_desc amd_iommu_v2_event_descs[] = {
  87. AMD_IOMMU_EVENT_DESC(mem_pass_untrans, "csource=0x01"),
  88. AMD_IOMMU_EVENT_DESC(mem_pass_pretrans, "csource=0x02"),
  89. AMD_IOMMU_EVENT_DESC(mem_pass_excl, "csource=0x03"),
  90. AMD_IOMMU_EVENT_DESC(mem_target_abort, "csource=0x04"),
  91. AMD_IOMMU_EVENT_DESC(mem_trans_total, "csource=0x05"),
  92. AMD_IOMMU_EVENT_DESC(mem_iommu_tlb_pte_hit, "csource=0x06"),
  93. AMD_IOMMU_EVENT_DESC(mem_iommu_tlb_pte_mis, "csource=0x07"),
  94. AMD_IOMMU_EVENT_DESC(mem_iommu_tlb_pde_hit, "csource=0x08"),
  95. AMD_IOMMU_EVENT_DESC(mem_iommu_tlb_pde_mis, "csource=0x09"),
  96. AMD_IOMMU_EVENT_DESC(mem_dte_hit, "csource=0x0a"),
  97. AMD_IOMMU_EVENT_DESC(mem_dte_mis, "csource=0x0b"),
  98. AMD_IOMMU_EVENT_DESC(page_tbl_read_tot, "csource=0x0c"),
  99. AMD_IOMMU_EVENT_DESC(page_tbl_read_nst, "csource=0x0d"),
  100. AMD_IOMMU_EVENT_DESC(page_tbl_read_gst, "csource=0x0e"),
  101. AMD_IOMMU_EVENT_DESC(int_dte_hit, "csource=0x0f"),
  102. AMD_IOMMU_EVENT_DESC(int_dte_mis, "csource=0x10"),
  103. AMD_IOMMU_EVENT_DESC(cmd_processed, "csource=0x11"),
  104. AMD_IOMMU_EVENT_DESC(cmd_processed_inv, "csource=0x12"),
  105. AMD_IOMMU_EVENT_DESC(tlb_inv, "csource=0x13"),
  106. { /* end: all zeroes */ },
  107. };
  108. /*---------------------------------------------
  109. * sysfs cpumask attributes
  110. *---------------------------------------------*/
  111. static cpumask_t iommu_cpumask;
  112. static ssize_t _iommu_cpumask_show(struct device *dev,
  113. struct device_attribute *attr,
  114. char *buf)
  115. {
  116. return cpumap_print_to_pagebuf(true, buf, &iommu_cpumask);
  117. }
  118. static DEVICE_ATTR(cpumask, S_IRUGO, _iommu_cpumask_show, NULL);
  119. static struct attribute *iommu_cpumask_attrs[] = {
  120. &dev_attr_cpumask.attr,
  121. NULL,
  122. };
  123. static struct attribute_group amd_iommu_cpumask_group = {
  124. .attrs = iommu_cpumask_attrs,
  125. };
  126. /*---------------------------------------------*/
  127. static int get_next_avail_iommu_bnk_cntr(struct perf_amd_iommu *perf_iommu)
  128. {
  129. unsigned long flags;
  130. int shift, bank, cntr, retval;
  131. int max_banks = perf_iommu->max_banks;
  132. int max_cntrs = perf_iommu->max_counters;
  133. raw_spin_lock_irqsave(&perf_iommu->lock, flags);
  134. for (bank = 0, shift = 0; bank < max_banks; bank++) {
  135. for (cntr = 0; cntr < max_cntrs; cntr++) {
  136. shift = bank + (bank*3) + cntr;
  137. if (perf_iommu->cntr_assign_mask & (1ULL<<shift)) {
  138. continue;
  139. } else {
  140. perf_iommu->cntr_assign_mask |= (1ULL<<shift);
  141. retval = ((u16)((u16)bank<<8) | (u8)(cntr));
  142. goto out;
  143. }
  144. }
  145. }
  146. retval = -ENOSPC;
  147. out:
  148. raw_spin_unlock_irqrestore(&perf_iommu->lock, flags);
  149. return retval;
  150. }
  151. static int clear_avail_iommu_bnk_cntr(struct perf_amd_iommu *perf_iommu,
  152. u8 bank, u8 cntr)
  153. {
  154. unsigned long flags;
  155. int max_banks, max_cntrs;
  156. int shift = 0;
  157. max_banks = perf_iommu->max_banks;
  158. max_cntrs = perf_iommu->max_counters;
  159. if ((bank > max_banks) || (cntr > max_cntrs))
  160. return -EINVAL;
  161. shift = bank + cntr + (bank*3);
  162. raw_spin_lock_irqsave(&perf_iommu->lock, flags);
  163. perf_iommu->cntr_assign_mask &= ~(1ULL<<shift);
  164. raw_spin_unlock_irqrestore(&perf_iommu->lock, flags);
  165. return 0;
  166. }
  167. static int perf_iommu_event_init(struct perf_event *event)
  168. {
  169. struct hw_perf_event *hwc = &event->hw;
  170. struct perf_amd_iommu *perf_iommu;
  171. u64 config, config1;
  172. /* test the event attr type check for PMU enumeration */
  173. if (event->attr.type != event->pmu->type)
  174. return -ENOENT;
  175. /*
  176. * IOMMU counters are shared across all cores.
  177. * Therefore, it does not support per-process mode.
  178. * Also, it does not support event sampling mode.
  179. */
  180. if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
  181. return -EINVAL;
  182. /* IOMMU counters do not have usr/os/guest/host bits */
  183. if (event->attr.exclude_user || event->attr.exclude_kernel ||
  184. event->attr.exclude_host || event->attr.exclude_guest)
  185. return -EINVAL;
  186. if (event->cpu < 0)
  187. return -EINVAL;
  188. perf_iommu = &__perf_iommu;
  189. if (event->pmu != &perf_iommu->pmu)
  190. return -ENOENT;
  191. if (perf_iommu) {
  192. config = event->attr.config;
  193. config1 = event->attr.config1;
  194. } else {
  195. return -EINVAL;
  196. }
  197. /* integrate with iommu base devid (0000), assume one iommu */
  198. perf_iommu->max_banks =
  199. amd_iommu_pc_get_max_banks(IOMMU_BASE_DEVID);
  200. perf_iommu->max_counters =
  201. amd_iommu_pc_get_max_counters(IOMMU_BASE_DEVID);
  202. if ((perf_iommu->max_banks == 0) || (perf_iommu->max_counters == 0))
  203. return -EINVAL;
  204. /* update the hw_perf_event struct with the iommu config data */
  205. hwc->config = config;
  206. hwc->extra_reg.config = config1;
  207. return 0;
  208. }
  209. static void perf_iommu_enable_event(struct perf_event *ev)
  210. {
  211. u8 csource = _GET_CSOURCE(ev);
  212. u16 devid = _GET_DEVID(ev);
  213. u64 reg = 0ULL;
  214. reg = csource;
  215. amd_iommu_pc_get_set_reg_val(devid,
  216. _GET_BANK(ev), _GET_CNTR(ev) ,
  217. IOMMU_PC_COUNTER_SRC_REG, &reg, true);
  218. reg = 0ULL | devid | (_GET_DEVID_MASK(ev) << 32);
  219. if (reg)
  220. reg |= (1UL << 31);
  221. amd_iommu_pc_get_set_reg_val(devid,
  222. _GET_BANK(ev), _GET_CNTR(ev) ,
  223. IOMMU_PC_DEVID_MATCH_REG, &reg, true);
  224. reg = 0ULL | _GET_PASID(ev) | (_GET_PASID_MASK(ev) << 32);
  225. if (reg)
  226. reg |= (1UL << 31);
  227. amd_iommu_pc_get_set_reg_val(devid,
  228. _GET_BANK(ev), _GET_CNTR(ev) ,
  229. IOMMU_PC_PASID_MATCH_REG, &reg, true);
  230. reg = 0ULL | _GET_DOMID(ev) | (_GET_DOMID_MASK(ev) << 32);
  231. if (reg)
  232. reg |= (1UL << 31);
  233. amd_iommu_pc_get_set_reg_val(devid,
  234. _GET_BANK(ev), _GET_CNTR(ev) ,
  235. IOMMU_PC_DOMID_MATCH_REG, &reg, true);
  236. }
  237. static void perf_iommu_disable_event(struct perf_event *event)
  238. {
  239. u64 reg = 0ULL;
  240. amd_iommu_pc_get_set_reg_val(_GET_DEVID(event),
  241. _GET_BANK(event), _GET_CNTR(event),
  242. IOMMU_PC_COUNTER_SRC_REG, &reg, true);
  243. }
  244. static void perf_iommu_start(struct perf_event *event, int flags)
  245. {
  246. struct hw_perf_event *hwc = &event->hw;
  247. pr_debug("perf: amd_iommu:perf_iommu_start\n");
  248. if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
  249. return;
  250. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  251. hwc->state = 0;
  252. if (flags & PERF_EF_RELOAD) {
  253. u64 prev_raw_count = local64_read(&hwc->prev_count);
  254. amd_iommu_pc_get_set_reg_val(_GET_DEVID(event),
  255. _GET_BANK(event), _GET_CNTR(event),
  256. IOMMU_PC_COUNTER_REG, &prev_raw_count, true);
  257. }
  258. perf_iommu_enable_event(event);
  259. perf_event_update_userpage(event);
  260. }
  261. static void perf_iommu_read(struct perf_event *event)
  262. {
  263. u64 count = 0ULL;
  264. u64 prev_raw_count = 0ULL;
  265. u64 delta = 0ULL;
  266. struct hw_perf_event *hwc = &event->hw;
  267. pr_debug("perf: amd_iommu:perf_iommu_read\n");
  268. amd_iommu_pc_get_set_reg_val(_GET_DEVID(event),
  269. _GET_BANK(event), _GET_CNTR(event),
  270. IOMMU_PC_COUNTER_REG, &count, false);
  271. /* IOMMU pc counter register is only 48 bits */
  272. count &= 0xFFFFFFFFFFFFULL;
  273. prev_raw_count = local64_read(&hwc->prev_count);
  274. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  275. count) != prev_raw_count)
  276. return;
  277. /* Handling 48-bit counter overflowing */
  278. delta = (count << COUNTER_SHIFT) - (prev_raw_count << COUNTER_SHIFT);
  279. delta >>= COUNTER_SHIFT;
  280. local64_add(delta, &event->count);
  281. }
  282. static void perf_iommu_stop(struct perf_event *event, int flags)
  283. {
  284. struct hw_perf_event *hwc = &event->hw;
  285. u64 config;
  286. pr_debug("perf: amd_iommu:perf_iommu_stop\n");
  287. if (hwc->state & PERF_HES_UPTODATE)
  288. return;
  289. perf_iommu_disable_event(event);
  290. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  291. hwc->state |= PERF_HES_STOPPED;
  292. if (hwc->state & PERF_HES_UPTODATE)
  293. return;
  294. config = hwc->config;
  295. perf_iommu_read(event);
  296. hwc->state |= PERF_HES_UPTODATE;
  297. }
  298. static int perf_iommu_add(struct perf_event *event, int flags)
  299. {
  300. int retval;
  301. struct perf_amd_iommu *perf_iommu =
  302. container_of(event->pmu, struct perf_amd_iommu, pmu);
  303. pr_debug("perf: amd_iommu:perf_iommu_add\n");
  304. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  305. /* request an iommu bank/counter */
  306. retval = get_next_avail_iommu_bnk_cntr(perf_iommu);
  307. if (retval != -ENOSPC)
  308. event->hw.extra_reg.reg = (u16)retval;
  309. else
  310. return retval;
  311. if (flags & PERF_EF_START)
  312. perf_iommu_start(event, PERF_EF_RELOAD);
  313. return 0;
  314. }
  315. static void perf_iommu_del(struct perf_event *event, int flags)
  316. {
  317. struct perf_amd_iommu *perf_iommu =
  318. container_of(event->pmu, struct perf_amd_iommu, pmu);
  319. pr_debug("perf: amd_iommu:perf_iommu_del\n");
  320. perf_iommu_stop(event, PERF_EF_UPDATE);
  321. /* clear the assigned iommu bank/counter */
  322. clear_avail_iommu_bnk_cntr(perf_iommu,
  323. _GET_BANK(event),
  324. _GET_CNTR(event));
  325. perf_event_update_userpage(event);
  326. }
  327. static __init int _init_events_attrs(struct perf_amd_iommu *perf_iommu)
  328. {
  329. struct attribute **attrs;
  330. struct attribute_group *attr_group;
  331. int i = 0, j;
  332. while (amd_iommu_v2_event_descs[i].attr.attr.name)
  333. i++;
  334. attr_group = kzalloc(sizeof(struct attribute *)
  335. * (i + 1) + sizeof(*attr_group), GFP_KERNEL);
  336. if (!attr_group)
  337. return -ENOMEM;
  338. attrs = (struct attribute **)(attr_group + 1);
  339. for (j = 0; j < i; j++)
  340. attrs[j] = &amd_iommu_v2_event_descs[j].attr.attr;
  341. attr_group->name = "events";
  342. attr_group->attrs = attrs;
  343. perf_iommu->events_group = attr_group;
  344. return 0;
  345. }
  346. static __init void amd_iommu_pc_exit(void)
  347. {
  348. if (__perf_iommu.events_group != NULL) {
  349. kfree(__perf_iommu.events_group);
  350. __perf_iommu.events_group = NULL;
  351. }
  352. }
  353. static __init int _init_perf_amd_iommu(
  354. struct perf_amd_iommu *perf_iommu, char *name)
  355. {
  356. int ret;
  357. raw_spin_lock_init(&perf_iommu->lock);
  358. /* Init format attributes */
  359. perf_iommu->format_group = &amd_iommu_format_group;
  360. /* Init cpumask attributes to only core 0 */
  361. cpumask_set_cpu(0, &iommu_cpumask);
  362. perf_iommu->cpumask_group = &amd_iommu_cpumask_group;
  363. /* Init events attributes */
  364. if (_init_events_attrs(perf_iommu) != 0)
  365. pr_err("perf: amd_iommu: Only support raw events.\n");
  366. /* Init null attributes */
  367. perf_iommu->null_group = NULL;
  368. perf_iommu->pmu.attr_groups = perf_iommu->attr_groups;
  369. ret = perf_pmu_register(&perf_iommu->pmu, name, -1);
  370. if (ret) {
  371. pr_err("perf: amd_iommu: Failed to initialized.\n");
  372. amd_iommu_pc_exit();
  373. } else {
  374. pr_info("perf: amd_iommu: Detected. (%d banks, %d counters/bank)\n",
  375. amd_iommu_pc_get_max_banks(IOMMU_BASE_DEVID),
  376. amd_iommu_pc_get_max_counters(IOMMU_BASE_DEVID));
  377. }
  378. return ret;
  379. }
  380. static struct perf_amd_iommu __perf_iommu = {
  381. .pmu = {
  382. .event_init = perf_iommu_event_init,
  383. .add = perf_iommu_add,
  384. .del = perf_iommu_del,
  385. .start = perf_iommu_start,
  386. .stop = perf_iommu_stop,
  387. .read = perf_iommu_read,
  388. },
  389. .max_banks = 0x00,
  390. .max_counters = 0x00,
  391. .cntr_assign_mask = 0ULL,
  392. .format_group = NULL,
  393. .cpumask_group = NULL,
  394. .events_group = NULL,
  395. .null_group = NULL,
  396. };
  397. static __init int amd_iommu_pc_init(void)
  398. {
  399. /* Make sure the IOMMU PC resource is available */
  400. if (!amd_iommu_pc_supported())
  401. return -ENODEV;
  402. _init_perf_amd_iommu(&__perf_iommu, "amd_iommu");
  403. return 0;
  404. }
  405. device_initcall(amd_iommu_pc_init);