iommu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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/init.h>
  15. #include <linux/cpumask.h>
  16. #include <linux/slab.h>
  17. #include "../perf_event.h"
  18. #include "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. AMD_IOMMU_EVENT_DESC(ign_rd_wr_mmio_1ff8h, "csource=0x14"),
  107. AMD_IOMMU_EVENT_DESC(vapic_int_non_guest, "csource=0x15"),
  108. AMD_IOMMU_EVENT_DESC(vapic_int_guest, "csource=0x16"),
  109. AMD_IOMMU_EVENT_DESC(smi_recv, "csource=0x17"),
  110. AMD_IOMMU_EVENT_DESC(smi_blk, "csource=0x18"),
  111. { /* end: all zeroes */ },
  112. };
  113. /*---------------------------------------------
  114. * sysfs cpumask attributes
  115. *---------------------------------------------*/
  116. static cpumask_t iommu_cpumask;
  117. static ssize_t _iommu_cpumask_show(struct device *dev,
  118. struct device_attribute *attr,
  119. char *buf)
  120. {
  121. return cpumap_print_to_pagebuf(true, buf, &iommu_cpumask);
  122. }
  123. static DEVICE_ATTR(cpumask, S_IRUGO, _iommu_cpumask_show, NULL);
  124. static struct attribute *iommu_cpumask_attrs[] = {
  125. &dev_attr_cpumask.attr,
  126. NULL,
  127. };
  128. static struct attribute_group amd_iommu_cpumask_group = {
  129. .attrs = iommu_cpumask_attrs,
  130. };
  131. /*---------------------------------------------*/
  132. static int get_next_avail_iommu_bnk_cntr(struct perf_amd_iommu *perf_iommu)
  133. {
  134. unsigned long flags;
  135. int shift, bank, cntr, retval;
  136. int max_banks = perf_iommu->max_banks;
  137. int max_cntrs = perf_iommu->max_counters;
  138. raw_spin_lock_irqsave(&perf_iommu->lock, flags);
  139. for (bank = 0, shift = 0; bank < max_banks; bank++) {
  140. for (cntr = 0; cntr < max_cntrs; cntr++) {
  141. shift = bank + (bank*3) + cntr;
  142. if (perf_iommu->cntr_assign_mask & (1ULL<<shift)) {
  143. continue;
  144. } else {
  145. perf_iommu->cntr_assign_mask |= (1ULL<<shift);
  146. retval = ((u16)((u16)bank<<8) | (u8)(cntr));
  147. goto out;
  148. }
  149. }
  150. }
  151. retval = -ENOSPC;
  152. out:
  153. raw_spin_unlock_irqrestore(&perf_iommu->lock, flags);
  154. return retval;
  155. }
  156. static int clear_avail_iommu_bnk_cntr(struct perf_amd_iommu *perf_iommu,
  157. u8 bank, u8 cntr)
  158. {
  159. unsigned long flags;
  160. int max_banks, max_cntrs;
  161. int shift = 0;
  162. max_banks = perf_iommu->max_banks;
  163. max_cntrs = perf_iommu->max_counters;
  164. if ((bank > max_banks) || (cntr > max_cntrs))
  165. return -EINVAL;
  166. shift = bank + cntr + (bank*3);
  167. raw_spin_lock_irqsave(&perf_iommu->lock, flags);
  168. perf_iommu->cntr_assign_mask &= ~(1ULL<<shift);
  169. raw_spin_unlock_irqrestore(&perf_iommu->lock, flags);
  170. return 0;
  171. }
  172. static int perf_iommu_event_init(struct perf_event *event)
  173. {
  174. struct hw_perf_event *hwc = &event->hw;
  175. struct perf_amd_iommu *perf_iommu;
  176. u64 config, config1;
  177. /* test the event attr type check for PMU enumeration */
  178. if (event->attr.type != event->pmu->type)
  179. return -ENOENT;
  180. /*
  181. * IOMMU counters are shared across all cores.
  182. * Therefore, it does not support per-process mode.
  183. * Also, it does not support event sampling mode.
  184. */
  185. if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
  186. return -EINVAL;
  187. /* IOMMU counters do not have usr/os/guest/host bits */
  188. if (event->attr.exclude_user || event->attr.exclude_kernel ||
  189. event->attr.exclude_host || event->attr.exclude_guest)
  190. return -EINVAL;
  191. if (event->cpu < 0)
  192. return -EINVAL;
  193. perf_iommu = &__perf_iommu;
  194. if (event->pmu != &perf_iommu->pmu)
  195. return -ENOENT;
  196. if (perf_iommu) {
  197. config = event->attr.config;
  198. config1 = event->attr.config1;
  199. } else {
  200. return -EINVAL;
  201. }
  202. /* integrate with iommu base devid (0000), assume one iommu */
  203. perf_iommu->max_banks =
  204. amd_iommu_pc_get_max_banks(IOMMU_BASE_DEVID);
  205. perf_iommu->max_counters =
  206. amd_iommu_pc_get_max_counters(IOMMU_BASE_DEVID);
  207. if ((perf_iommu->max_banks == 0) || (perf_iommu->max_counters == 0))
  208. return -EINVAL;
  209. /* update the hw_perf_event struct with the iommu config data */
  210. hwc->config = config;
  211. hwc->extra_reg.config = config1;
  212. return 0;
  213. }
  214. static void perf_iommu_enable_event(struct perf_event *ev)
  215. {
  216. u8 csource = _GET_CSOURCE(ev);
  217. u16 devid = _GET_DEVID(ev);
  218. u64 reg = 0ULL;
  219. reg = csource;
  220. amd_iommu_pc_get_set_reg_val(devid,
  221. _GET_BANK(ev), _GET_CNTR(ev) ,
  222. IOMMU_PC_COUNTER_SRC_REG, &reg, true);
  223. reg = 0ULL | devid | (_GET_DEVID_MASK(ev) << 32);
  224. if (reg)
  225. reg |= (1UL << 31);
  226. amd_iommu_pc_get_set_reg_val(devid,
  227. _GET_BANK(ev), _GET_CNTR(ev) ,
  228. IOMMU_PC_DEVID_MATCH_REG, &reg, true);
  229. reg = 0ULL | _GET_PASID(ev) | (_GET_PASID_MASK(ev) << 32);
  230. if (reg)
  231. reg |= (1UL << 31);
  232. amd_iommu_pc_get_set_reg_val(devid,
  233. _GET_BANK(ev), _GET_CNTR(ev) ,
  234. IOMMU_PC_PASID_MATCH_REG, &reg, true);
  235. reg = 0ULL | _GET_DOMID(ev) | (_GET_DOMID_MASK(ev) << 32);
  236. if (reg)
  237. reg |= (1UL << 31);
  238. amd_iommu_pc_get_set_reg_val(devid,
  239. _GET_BANK(ev), _GET_CNTR(ev) ,
  240. IOMMU_PC_DOMID_MATCH_REG, &reg, true);
  241. }
  242. static void perf_iommu_disable_event(struct perf_event *event)
  243. {
  244. u64 reg = 0ULL;
  245. amd_iommu_pc_get_set_reg_val(_GET_DEVID(event),
  246. _GET_BANK(event), _GET_CNTR(event),
  247. IOMMU_PC_COUNTER_SRC_REG, &reg, true);
  248. }
  249. static void perf_iommu_start(struct perf_event *event, int flags)
  250. {
  251. struct hw_perf_event *hwc = &event->hw;
  252. pr_debug("perf: amd_iommu:perf_iommu_start\n");
  253. if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
  254. return;
  255. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  256. hwc->state = 0;
  257. if (flags & PERF_EF_RELOAD) {
  258. u64 prev_raw_count = local64_read(&hwc->prev_count);
  259. amd_iommu_pc_get_set_reg_val(_GET_DEVID(event),
  260. _GET_BANK(event), _GET_CNTR(event),
  261. IOMMU_PC_COUNTER_REG, &prev_raw_count, true);
  262. }
  263. perf_iommu_enable_event(event);
  264. perf_event_update_userpage(event);
  265. }
  266. static void perf_iommu_read(struct perf_event *event)
  267. {
  268. u64 count = 0ULL;
  269. u64 prev_raw_count = 0ULL;
  270. u64 delta = 0ULL;
  271. struct hw_perf_event *hwc = &event->hw;
  272. pr_debug("perf: amd_iommu:perf_iommu_read\n");
  273. amd_iommu_pc_get_set_reg_val(_GET_DEVID(event),
  274. _GET_BANK(event), _GET_CNTR(event),
  275. IOMMU_PC_COUNTER_REG, &count, false);
  276. /* IOMMU pc counter register is only 48 bits */
  277. count &= 0xFFFFFFFFFFFFULL;
  278. prev_raw_count = local64_read(&hwc->prev_count);
  279. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  280. count) != prev_raw_count)
  281. return;
  282. /* Handling 48-bit counter overflowing */
  283. delta = (count << COUNTER_SHIFT) - (prev_raw_count << COUNTER_SHIFT);
  284. delta >>= COUNTER_SHIFT;
  285. local64_add(delta, &event->count);
  286. }
  287. static void perf_iommu_stop(struct perf_event *event, int flags)
  288. {
  289. struct hw_perf_event *hwc = &event->hw;
  290. u64 config;
  291. pr_debug("perf: amd_iommu:perf_iommu_stop\n");
  292. if (hwc->state & PERF_HES_UPTODATE)
  293. return;
  294. perf_iommu_disable_event(event);
  295. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  296. hwc->state |= PERF_HES_STOPPED;
  297. if (hwc->state & PERF_HES_UPTODATE)
  298. return;
  299. config = hwc->config;
  300. perf_iommu_read(event);
  301. hwc->state |= PERF_HES_UPTODATE;
  302. }
  303. static int perf_iommu_add(struct perf_event *event, int flags)
  304. {
  305. int retval;
  306. struct perf_amd_iommu *perf_iommu =
  307. container_of(event->pmu, struct perf_amd_iommu, pmu);
  308. pr_debug("perf: amd_iommu:perf_iommu_add\n");
  309. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  310. /* request an iommu bank/counter */
  311. retval = get_next_avail_iommu_bnk_cntr(perf_iommu);
  312. if (retval != -ENOSPC)
  313. event->hw.extra_reg.reg = (u16)retval;
  314. else
  315. return retval;
  316. if (flags & PERF_EF_START)
  317. perf_iommu_start(event, PERF_EF_RELOAD);
  318. return 0;
  319. }
  320. static void perf_iommu_del(struct perf_event *event, int flags)
  321. {
  322. struct perf_amd_iommu *perf_iommu =
  323. container_of(event->pmu, struct perf_amd_iommu, pmu);
  324. pr_debug("perf: amd_iommu:perf_iommu_del\n");
  325. perf_iommu_stop(event, PERF_EF_UPDATE);
  326. /* clear the assigned iommu bank/counter */
  327. clear_avail_iommu_bnk_cntr(perf_iommu,
  328. _GET_BANK(event),
  329. _GET_CNTR(event));
  330. perf_event_update_userpage(event);
  331. }
  332. static __init int _init_events_attrs(struct perf_amd_iommu *perf_iommu)
  333. {
  334. struct attribute **attrs;
  335. struct attribute_group *attr_group;
  336. int i = 0, j;
  337. while (amd_iommu_v2_event_descs[i].attr.attr.name)
  338. i++;
  339. attr_group = kzalloc(sizeof(struct attribute *)
  340. * (i + 1) + sizeof(*attr_group), GFP_KERNEL);
  341. if (!attr_group)
  342. return -ENOMEM;
  343. attrs = (struct attribute **)(attr_group + 1);
  344. for (j = 0; j < i; j++)
  345. attrs[j] = &amd_iommu_v2_event_descs[j].attr.attr;
  346. attr_group->name = "events";
  347. attr_group->attrs = attrs;
  348. perf_iommu->events_group = attr_group;
  349. return 0;
  350. }
  351. static __init void amd_iommu_pc_exit(void)
  352. {
  353. if (__perf_iommu.events_group != NULL) {
  354. kfree(__perf_iommu.events_group);
  355. __perf_iommu.events_group = NULL;
  356. }
  357. }
  358. static __init int _init_perf_amd_iommu(
  359. struct perf_amd_iommu *perf_iommu, char *name)
  360. {
  361. int ret;
  362. raw_spin_lock_init(&perf_iommu->lock);
  363. /* Init format attributes */
  364. perf_iommu->format_group = &amd_iommu_format_group;
  365. /* Init cpumask attributes to only core 0 */
  366. cpumask_set_cpu(0, &iommu_cpumask);
  367. perf_iommu->cpumask_group = &amd_iommu_cpumask_group;
  368. /* Init events attributes */
  369. if (_init_events_attrs(perf_iommu) != 0)
  370. pr_err("perf: amd_iommu: Only support raw events.\n");
  371. /* Init null attributes */
  372. perf_iommu->null_group = NULL;
  373. perf_iommu->pmu.attr_groups = perf_iommu->attr_groups;
  374. ret = perf_pmu_register(&perf_iommu->pmu, name, -1);
  375. if (ret) {
  376. pr_err("perf: amd_iommu: Failed to initialized.\n");
  377. amd_iommu_pc_exit();
  378. } else {
  379. pr_info("perf: amd_iommu: Detected. (%d banks, %d counters/bank)\n",
  380. amd_iommu_pc_get_max_banks(IOMMU_BASE_DEVID),
  381. amd_iommu_pc_get_max_counters(IOMMU_BASE_DEVID));
  382. }
  383. return ret;
  384. }
  385. static struct perf_amd_iommu __perf_iommu = {
  386. .pmu = {
  387. .task_ctx_nr = perf_invalid_context,
  388. .event_init = perf_iommu_event_init,
  389. .add = perf_iommu_add,
  390. .del = perf_iommu_del,
  391. .start = perf_iommu_start,
  392. .stop = perf_iommu_stop,
  393. .read = perf_iommu_read,
  394. },
  395. .max_banks = 0x00,
  396. .max_counters = 0x00,
  397. .cntr_assign_mask = 0ULL,
  398. .format_group = NULL,
  399. .cpumask_group = NULL,
  400. .events_group = NULL,
  401. .null_group = NULL,
  402. };
  403. static __init int amd_iommu_pc_init(void)
  404. {
  405. /* Make sure the IOMMU PC resource is available */
  406. if (!amd_iommu_pc_supported())
  407. return -ENODEV;
  408. _init_perf_amd_iommu(&__perf_iommu, "amd_iommu");
  409. return 0;
  410. }
  411. device_initcall(amd_iommu_pc_init);