perfmon.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Support Intel IOMMU PerfMon
  4. * Copyright(c) 2023 Intel Corporation.
  5. */
  6. #define pr_fmt(fmt) "DMAR: " fmt
  7. #define dev_fmt(fmt) pr_fmt(fmt)
  8. #include <linux/dmar.h>
  9. #include "iommu.h"
  10. #include "perfmon.h"
  11. PMU_FORMAT_ATTR(event, "config:0-27"); /* ES: Events Select */
  12. PMU_FORMAT_ATTR(event_group, "config:28-31"); /* EGI: Event Group Index */
  13. static struct attribute *iommu_pmu_format_attrs[] = {
  14. &format_attr_event_group.attr,
  15. &format_attr_event.attr,
  16. NULL
  17. };
  18. static struct attribute_group iommu_pmu_format_attr_group = {
  19. .name = "format",
  20. .attrs = iommu_pmu_format_attrs,
  21. };
  22. /* The available events are added in attr_update later */
  23. static struct attribute *attrs_empty[] = {
  24. NULL
  25. };
  26. static struct attribute_group iommu_pmu_events_attr_group = {
  27. .name = "events",
  28. .attrs = attrs_empty,
  29. };
  30. static const struct attribute_group *iommu_pmu_attr_groups[] = {
  31. &iommu_pmu_format_attr_group,
  32. &iommu_pmu_events_attr_group,
  33. NULL
  34. };
  35. static inline struct iommu_pmu *dev_to_iommu_pmu(struct device *dev)
  36. {
  37. /*
  38. * The perf_event creates its own dev for each PMU.
  39. * See pmu_dev_alloc()
  40. */
  41. return container_of(dev_get_drvdata(dev), struct iommu_pmu, pmu);
  42. }
  43. #define IOMMU_PMU_ATTR(_name, _format, _filter) \
  44. PMU_FORMAT_ATTR(_name, _format); \
  45. \
  46. static struct attribute *_name##_attr[] = { \
  47. &format_attr_##_name.attr, \
  48. NULL \
  49. }; \
  50. \
  51. static umode_t \
  52. _name##_is_visible(struct kobject *kobj, struct attribute *attr, int i) \
  53. { \
  54. struct device *dev = kobj_to_dev(kobj); \
  55. struct iommu_pmu *iommu_pmu = dev_to_iommu_pmu(dev); \
  56. \
  57. if (!iommu_pmu) \
  58. return 0; \
  59. return (iommu_pmu->filter & _filter) ? attr->mode : 0; \
  60. } \
  61. \
  62. static struct attribute_group _name = { \
  63. .name = "format", \
  64. .attrs = _name##_attr, \
  65. .is_visible = _name##_is_visible, \
  66. };
  67. IOMMU_PMU_ATTR(filter_requester_id_en, "config1:0", IOMMU_PMU_FILTER_REQUESTER_ID);
  68. IOMMU_PMU_ATTR(filter_domain_en, "config1:1", IOMMU_PMU_FILTER_DOMAIN);
  69. IOMMU_PMU_ATTR(filter_pasid_en, "config1:2", IOMMU_PMU_FILTER_PASID);
  70. IOMMU_PMU_ATTR(filter_ats_en, "config1:3", IOMMU_PMU_FILTER_ATS);
  71. IOMMU_PMU_ATTR(filter_page_table_en, "config1:4", IOMMU_PMU_FILTER_PAGE_TABLE);
  72. IOMMU_PMU_ATTR(filter_requester_id, "config1:16-31", IOMMU_PMU_FILTER_REQUESTER_ID);
  73. IOMMU_PMU_ATTR(filter_domain, "config1:32-47", IOMMU_PMU_FILTER_DOMAIN);
  74. IOMMU_PMU_ATTR(filter_pasid, "config2:0-21", IOMMU_PMU_FILTER_PASID);
  75. IOMMU_PMU_ATTR(filter_ats, "config2:24-28", IOMMU_PMU_FILTER_ATS);
  76. IOMMU_PMU_ATTR(filter_page_table, "config2:32-36", IOMMU_PMU_FILTER_PAGE_TABLE);
  77. #define iommu_pmu_en_requester_id(e) ((e) & 0x1)
  78. #define iommu_pmu_en_domain(e) (((e) >> 1) & 0x1)
  79. #define iommu_pmu_en_pasid(e) (((e) >> 2) & 0x1)
  80. #define iommu_pmu_en_ats(e) (((e) >> 3) & 0x1)
  81. #define iommu_pmu_en_page_table(e) (((e) >> 4) & 0x1)
  82. #define iommu_pmu_get_requester_id(filter) (((filter) >> 16) & 0xffff)
  83. #define iommu_pmu_get_domain(filter) (((filter) >> 32) & 0xffff)
  84. #define iommu_pmu_get_pasid(filter) ((filter) & 0x3fffff)
  85. #define iommu_pmu_get_ats(filter) (((filter) >> 24) & 0x1f)
  86. #define iommu_pmu_get_page_table(filter) (((filter) >> 32) & 0x1f)
  87. #define iommu_pmu_set_filter(_name, _config, _filter, _idx, _econfig) \
  88. { \
  89. if ((iommu_pmu->filter & _filter) && iommu_pmu_en_##_name(_econfig)) { \
  90. dmar_writel(iommu_pmu->cfg_reg + _idx * IOMMU_PMU_CFG_OFFSET + \
  91. IOMMU_PMU_CFG_SIZE + \
  92. (ffs(_filter) - 1) * IOMMU_PMU_CFG_FILTERS_OFFSET, \
  93. iommu_pmu_get_##_name(_config) | IOMMU_PMU_FILTER_EN);\
  94. } \
  95. }
  96. #define iommu_pmu_clear_filter(_filter, _idx) \
  97. { \
  98. if (iommu_pmu->filter & _filter) { \
  99. dmar_writel(iommu_pmu->cfg_reg + _idx * IOMMU_PMU_CFG_OFFSET + \
  100. IOMMU_PMU_CFG_SIZE + \
  101. (ffs(_filter) - 1) * IOMMU_PMU_CFG_FILTERS_OFFSET, \
  102. 0); \
  103. } \
  104. }
  105. /*
  106. * Define the event attr related functions
  107. * Input: _name: event attr name
  108. * _string: string of the event in sysfs
  109. * _g_idx: event group encoding
  110. * _event: event encoding
  111. */
  112. #define IOMMU_PMU_EVENT_ATTR(_name, _string, _g_idx, _event) \
  113. PMU_EVENT_ATTR_STRING(_name, event_attr_##_name, _string) \
  114. \
  115. static struct attribute *_name##_attr[] = { \
  116. &event_attr_##_name.attr.attr, \
  117. NULL \
  118. }; \
  119. \
  120. static umode_t \
  121. _name##_is_visible(struct kobject *kobj, struct attribute *attr, int i) \
  122. { \
  123. struct device *dev = kobj_to_dev(kobj); \
  124. struct iommu_pmu *iommu_pmu = dev_to_iommu_pmu(dev); \
  125. \
  126. if (!iommu_pmu) \
  127. return 0; \
  128. return (iommu_pmu->evcap[_g_idx] & _event) ? attr->mode : 0; \
  129. } \
  130. \
  131. static struct attribute_group _name = { \
  132. .name = "events", \
  133. .attrs = _name##_attr, \
  134. .is_visible = _name##_is_visible, \
  135. };
  136. IOMMU_PMU_EVENT_ATTR(iommu_clocks, "event_group=0x0,event=0x001", 0x0, 0x001)
  137. IOMMU_PMU_EVENT_ATTR(iommu_requests, "event_group=0x0,event=0x002", 0x0, 0x002)
  138. IOMMU_PMU_EVENT_ATTR(pw_occupancy, "event_group=0x0,event=0x004", 0x0, 0x004)
  139. IOMMU_PMU_EVENT_ATTR(ats_blocked, "event_group=0x0,event=0x008", 0x0, 0x008)
  140. IOMMU_PMU_EVENT_ATTR(iommu_mrds, "event_group=0x1,event=0x001", 0x1, 0x001)
  141. IOMMU_PMU_EVENT_ATTR(iommu_mem_blocked, "event_group=0x1,event=0x020", 0x1, 0x020)
  142. IOMMU_PMU_EVENT_ATTR(pg_req_posted, "event_group=0x1,event=0x040", 0x1, 0x040)
  143. IOMMU_PMU_EVENT_ATTR(ctxt_cache_lookup, "event_group=0x2,event=0x001", 0x2, 0x001)
  144. IOMMU_PMU_EVENT_ATTR(ctxt_cache_hit, "event_group=0x2,event=0x002", 0x2, 0x002)
  145. IOMMU_PMU_EVENT_ATTR(pasid_cache_lookup, "event_group=0x2,event=0x004", 0x2, 0x004)
  146. IOMMU_PMU_EVENT_ATTR(pasid_cache_hit, "event_group=0x2,event=0x008", 0x2, 0x008)
  147. IOMMU_PMU_EVENT_ATTR(ss_nonleaf_lookup, "event_group=0x2,event=0x010", 0x2, 0x010)
  148. IOMMU_PMU_EVENT_ATTR(ss_nonleaf_hit, "event_group=0x2,event=0x020", 0x2, 0x020)
  149. IOMMU_PMU_EVENT_ATTR(fs_nonleaf_lookup, "event_group=0x2,event=0x040", 0x2, 0x040)
  150. IOMMU_PMU_EVENT_ATTR(fs_nonleaf_hit, "event_group=0x2,event=0x080", 0x2, 0x080)
  151. IOMMU_PMU_EVENT_ATTR(hpt_nonleaf_lookup, "event_group=0x2,event=0x100", 0x2, 0x100)
  152. IOMMU_PMU_EVENT_ATTR(hpt_nonleaf_hit, "event_group=0x2,event=0x200", 0x2, 0x200)
  153. IOMMU_PMU_EVENT_ATTR(iotlb_lookup, "event_group=0x3,event=0x001", 0x3, 0x001)
  154. IOMMU_PMU_EVENT_ATTR(iotlb_hit, "event_group=0x3,event=0x002", 0x3, 0x002)
  155. IOMMU_PMU_EVENT_ATTR(hpt_leaf_lookup, "event_group=0x3,event=0x004", 0x3, 0x004)
  156. IOMMU_PMU_EVENT_ATTR(hpt_leaf_hit, "event_group=0x3,event=0x008", 0x3, 0x008)
  157. IOMMU_PMU_EVENT_ATTR(int_cache_lookup, "event_group=0x4,event=0x001", 0x4, 0x001)
  158. IOMMU_PMU_EVENT_ATTR(int_cache_hit_nonposted, "event_group=0x4,event=0x002", 0x4, 0x002)
  159. IOMMU_PMU_EVENT_ATTR(int_cache_hit_posted, "event_group=0x4,event=0x004", 0x4, 0x004)
  160. static const struct attribute_group *iommu_pmu_attr_update[] = {
  161. &filter_requester_id_en,
  162. &filter_domain_en,
  163. &filter_pasid_en,
  164. &filter_ats_en,
  165. &filter_page_table_en,
  166. &filter_requester_id,
  167. &filter_domain,
  168. &filter_pasid,
  169. &filter_ats,
  170. &filter_page_table,
  171. &iommu_clocks,
  172. &iommu_requests,
  173. &pw_occupancy,
  174. &ats_blocked,
  175. &iommu_mrds,
  176. &iommu_mem_blocked,
  177. &pg_req_posted,
  178. &ctxt_cache_lookup,
  179. &ctxt_cache_hit,
  180. &pasid_cache_lookup,
  181. &pasid_cache_hit,
  182. &ss_nonleaf_lookup,
  183. &ss_nonleaf_hit,
  184. &fs_nonleaf_lookup,
  185. &fs_nonleaf_hit,
  186. &hpt_nonleaf_lookup,
  187. &hpt_nonleaf_hit,
  188. &iotlb_lookup,
  189. &iotlb_hit,
  190. &hpt_leaf_lookup,
  191. &hpt_leaf_hit,
  192. &int_cache_lookup,
  193. &int_cache_hit_nonposted,
  194. &int_cache_hit_posted,
  195. NULL
  196. };
  197. static inline void __iomem *
  198. iommu_event_base(struct iommu_pmu *iommu_pmu, int idx)
  199. {
  200. return iommu_pmu->cntr_reg + idx * iommu_pmu->cntr_stride;
  201. }
  202. static inline void __iomem *
  203. iommu_config_base(struct iommu_pmu *iommu_pmu, int idx)
  204. {
  205. return iommu_pmu->cfg_reg + idx * IOMMU_PMU_CFG_OFFSET;
  206. }
  207. static inline struct iommu_pmu *iommu_event_to_pmu(struct perf_event *event)
  208. {
  209. return container_of(event->pmu, struct iommu_pmu, pmu);
  210. }
  211. static inline u64 iommu_event_config(struct perf_event *event)
  212. {
  213. u64 config = event->attr.config;
  214. return (iommu_event_select(config) << IOMMU_EVENT_CFG_ES_SHIFT) |
  215. (iommu_event_group(config) << IOMMU_EVENT_CFG_EGI_SHIFT) |
  216. IOMMU_EVENT_CFG_INT;
  217. }
  218. static inline bool is_iommu_pmu_event(struct iommu_pmu *iommu_pmu,
  219. struct perf_event *event)
  220. {
  221. return event->pmu == &iommu_pmu->pmu;
  222. }
  223. static int iommu_pmu_validate_event(struct perf_event *event)
  224. {
  225. struct iommu_pmu *iommu_pmu = iommu_event_to_pmu(event);
  226. u32 event_group = iommu_event_group(event->attr.config);
  227. if (event_group >= iommu_pmu->num_eg)
  228. return -EINVAL;
  229. return 0;
  230. }
  231. static int iommu_pmu_validate_group(struct perf_event *event)
  232. {
  233. struct iommu_pmu *iommu_pmu = iommu_event_to_pmu(event);
  234. struct perf_event *sibling;
  235. int nr = 0;
  236. /*
  237. * All events in a group must be scheduled simultaneously.
  238. * Check whether there is enough counters for all the events.
  239. */
  240. for_each_sibling_event(sibling, event->group_leader) {
  241. if (!is_iommu_pmu_event(iommu_pmu, sibling) ||
  242. sibling->state <= PERF_EVENT_STATE_OFF)
  243. continue;
  244. if (++nr > iommu_pmu->num_cntr)
  245. return -EINVAL;
  246. }
  247. return 0;
  248. }
  249. static int iommu_pmu_event_init(struct perf_event *event)
  250. {
  251. struct hw_perf_event *hwc = &event->hw;
  252. if (event->attr.type != event->pmu->type)
  253. return -ENOENT;
  254. /* sampling not supported */
  255. if (event->attr.sample_period)
  256. return -EINVAL;
  257. if (event->cpu < 0)
  258. return -EINVAL;
  259. if (iommu_pmu_validate_event(event))
  260. return -EINVAL;
  261. hwc->config = iommu_event_config(event);
  262. return iommu_pmu_validate_group(event);
  263. }
  264. static void iommu_pmu_event_update(struct perf_event *event)
  265. {
  266. struct iommu_pmu *iommu_pmu = iommu_event_to_pmu(event);
  267. struct hw_perf_event *hwc = &event->hw;
  268. u64 prev_count, new_count, delta;
  269. int shift = 64 - iommu_pmu->cntr_width;
  270. again:
  271. prev_count = local64_read(&hwc->prev_count);
  272. new_count = dmar_readq(iommu_event_base(iommu_pmu, hwc->idx));
  273. if (local64_xchg(&hwc->prev_count, new_count) != prev_count)
  274. goto again;
  275. /*
  276. * The counter width is enumerated. Always shift the counter
  277. * before using it.
  278. */
  279. delta = (new_count << shift) - (prev_count << shift);
  280. delta >>= shift;
  281. local64_add(delta, &event->count);
  282. }
  283. static void iommu_pmu_start(struct perf_event *event, int flags)
  284. {
  285. struct iommu_pmu *iommu_pmu = iommu_event_to_pmu(event);
  286. struct intel_iommu *iommu = iommu_pmu->iommu;
  287. struct hw_perf_event *hwc = &event->hw;
  288. u64 count;
  289. if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
  290. return;
  291. if (WARN_ON_ONCE(hwc->idx < 0 || hwc->idx >= IOMMU_PMU_IDX_MAX))
  292. return;
  293. if (flags & PERF_EF_RELOAD)
  294. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  295. hwc->state = 0;
  296. /* Always reprogram the period */
  297. count = dmar_readq(iommu_event_base(iommu_pmu, hwc->idx));
  298. local64_set((&hwc->prev_count), count);
  299. /*
  300. * The error of ecmd will be ignored.
  301. * - The existing perf_event subsystem doesn't handle the error.
  302. * Only IOMMU PMU returns runtime HW error. We don't want to
  303. * change the existing generic interfaces for the specific case.
  304. * - It's a corner case caused by HW, which is very unlikely to
  305. * happen. There is nothing SW can do.
  306. * - The worst case is that the user will get <not count> with
  307. * perf command, which can give the user some hints.
  308. */
  309. ecmd_submit_sync(iommu, DMA_ECMD_ENABLE, hwc->idx, 0);
  310. perf_event_update_userpage(event);
  311. }
  312. static void iommu_pmu_stop(struct perf_event *event, int flags)
  313. {
  314. struct iommu_pmu *iommu_pmu = iommu_event_to_pmu(event);
  315. struct intel_iommu *iommu = iommu_pmu->iommu;
  316. struct hw_perf_event *hwc = &event->hw;
  317. if (!(hwc->state & PERF_HES_STOPPED)) {
  318. ecmd_submit_sync(iommu, DMA_ECMD_DISABLE, hwc->idx, 0);
  319. iommu_pmu_event_update(event);
  320. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  321. }
  322. }
  323. static inline int
  324. iommu_pmu_validate_per_cntr_event(struct iommu_pmu *iommu_pmu,
  325. int idx, struct perf_event *event)
  326. {
  327. u32 event_group = iommu_event_group(event->attr.config);
  328. u32 select = iommu_event_select(event->attr.config);
  329. if (!(iommu_pmu->cntr_evcap[idx][event_group] & select))
  330. return -EINVAL;
  331. return 0;
  332. }
  333. static int iommu_pmu_assign_event(struct iommu_pmu *iommu_pmu,
  334. struct perf_event *event)
  335. {
  336. struct hw_perf_event *hwc = &event->hw;
  337. int idx;
  338. /*
  339. * The counters which support limited events are usually at the end.
  340. * Schedule them first to accommodate more events.
  341. */
  342. for (idx = iommu_pmu->num_cntr - 1; idx >= 0; idx--) {
  343. if (test_and_set_bit(idx, iommu_pmu->used_mask))
  344. continue;
  345. /* Check per-counter event capabilities */
  346. if (!iommu_pmu_validate_per_cntr_event(iommu_pmu, idx, event))
  347. break;
  348. clear_bit(idx, iommu_pmu->used_mask);
  349. }
  350. if (idx < 0)
  351. return -EINVAL;
  352. iommu_pmu->event_list[idx] = event;
  353. hwc->idx = idx;
  354. /* config events */
  355. dmar_writeq(iommu_config_base(iommu_pmu, idx), hwc->config);
  356. iommu_pmu_set_filter(requester_id, event->attr.config1,
  357. IOMMU_PMU_FILTER_REQUESTER_ID, idx,
  358. event->attr.config1);
  359. iommu_pmu_set_filter(domain, event->attr.config1,
  360. IOMMU_PMU_FILTER_DOMAIN, idx,
  361. event->attr.config1);
  362. iommu_pmu_set_filter(pasid, event->attr.config2,
  363. IOMMU_PMU_FILTER_PASID, idx,
  364. event->attr.config1);
  365. iommu_pmu_set_filter(ats, event->attr.config2,
  366. IOMMU_PMU_FILTER_ATS, idx,
  367. event->attr.config1);
  368. iommu_pmu_set_filter(page_table, event->attr.config2,
  369. IOMMU_PMU_FILTER_PAGE_TABLE, idx,
  370. event->attr.config1);
  371. return 0;
  372. }
  373. static int iommu_pmu_add(struct perf_event *event, int flags)
  374. {
  375. struct iommu_pmu *iommu_pmu = iommu_event_to_pmu(event);
  376. struct hw_perf_event *hwc = &event->hw;
  377. int ret;
  378. ret = iommu_pmu_assign_event(iommu_pmu, event);
  379. if (ret < 0)
  380. return ret;
  381. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  382. if (flags & PERF_EF_START)
  383. iommu_pmu_start(event, 0);
  384. return 0;
  385. }
  386. static void iommu_pmu_del(struct perf_event *event, int flags)
  387. {
  388. struct iommu_pmu *iommu_pmu = iommu_event_to_pmu(event);
  389. int idx = event->hw.idx;
  390. iommu_pmu_stop(event, PERF_EF_UPDATE);
  391. iommu_pmu_clear_filter(IOMMU_PMU_FILTER_REQUESTER_ID, idx);
  392. iommu_pmu_clear_filter(IOMMU_PMU_FILTER_DOMAIN, idx);
  393. iommu_pmu_clear_filter(IOMMU_PMU_FILTER_PASID, idx);
  394. iommu_pmu_clear_filter(IOMMU_PMU_FILTER_ATS, idx);
  395. iommu_pmu_clear_filter(IOMMU_PMU_FILTER_PAGE_TABLE, idx);
  396. iommu_pmu->event_list[idx] = NULL;
  397. event->hw.idx = -1;
  398. clear_bit(idx, iommu_pmu->used_mask);
  399. perf_event_update_userpage(event);
  400. }
  401. static void iommu_pmu_enable(struct pmu *pmu)
  402. {
  403. struct iommu_pmu *iommu_pmu = container_of(pmu, struct iommu_pmu, pmu);
  404. struct intel_iommu *iommu = iommu_pmu->iommu;
  405. ecmd_submit_sync(iommu, DMA_ECMD_UNFREEZE, 0, 0);
  406. }
  407. static void iommu_pmu_disable(struct pmu *pmu)
  408. {
  409. struct iommu_pmu *iommu_pmu = container_of(pmu, struct iommu_pmu, pmu);
  410. struct intel_iommu *iommu = iommu_pmu->iommu;
  411. ecmd_submit_sync(iommu, DMA_ECMD_FREEZE, 0, 0);
  412. }
  413. static void iommu_pmu_counter_overflow(struct iommu_pmu *iommu_pmu)
  414. {
  415. struct perf_event *event;
  416. u64 status;
  417. int i;
  418. /*
  419. * Two counters may be overflowed very close. Always check
  420. * whether there are more to handle.
  421. */
  422. while ((status = dmar_readq(iommu_pmu->overflow))) {
  423. for_each_set_bit(i, (unsigned long *)&status, iommu_pmu->num_cntr) {
  424. /*
  425. * Find the assigned event of the counter.
  426. * Accumulate the value into the event->count.
  427. */
  428. event = iommu_pmu->event_list[i];
  429. if (!event) {
  430. pr_warn_once("Cannot find the assigned event for counter %d\n", i);
  431. continue;
  432. }
  433. iommu_pmu_event_update(event);
  434. }
  435. dmar_writeq(iommu_pmu->overflow, status);
  436. }
  437. }
  438. static irqreturn_t iommu_pmu_irq_handler(int irq, void *dev_id)
  439. {
  440. struct intel_iommu *iommu = dev_id;
  441. if (!dmar_readl(iommu->reg + DMAR_PERFINTRSTS_REG))
  442. return IRQ_NONE;
  443. iommu_pmu_counter_overflow(iommu->pmu);
  444. /* Clear the status bit */
  445. dmar_writel(iommu->reg + DMAR_PERFINTRSTS_REG, DMA_PERFINTRSTS_PIS);
  446. return IRQ_HANDLED;
  447. }
  448. static int __iommu_pmu_register(struct intel_iommu *iommu)
  449. {
  450. struct iommu_pmu *iommu_pmu = iommu->pmu;
  451. iommu_pmu->pmu.name = iommu->name;
  452. iommu_pmu->pmu.task_ctx_nr = perf_invalid_context;
  453. iommu_pmu->pmu.event_init = iommu_pmu_event_init;
  454. iommu_pmu->pmu.pmu_enable = iommu_pmu_enable;
  455. iommu_pmu->pmu.pmu_disable = iommu_pmu_disable;
  456. iommu_pmu->pmu.add = iommu_pmu_add;
  457. iommu_pmu->pmu.del = iommu_pmu_del;
  458. iommu_pmu->pmu.start = iommu_pmu_start;
  459. iommu_pmu->pmu.stop = iommu_pmu_stop;
  460. iommu_pmu->pmu.read = iommu_pmu_event_update;
  461. iommu_pmu->pmu.attr_groups = iommu_pmu_attr_groups;
  462. iommu_pmu->pmu.attr_update = iommu_pmu_attr_update;
  463. iommu_pmu->pmu.capabilities = PERF_PMU_CAP_NO_EXCLUDE;
  464. iommu_pmu->pmu.scope = PERF_PMU_SCOPE_SYS_WIDE;
  465. iommu_pmu->pmu.module = THIS_MODULE;
  466. return perf_pmu_register(&iommu_pmu->pmu, iommu_pmu->pmu.name, -1);
  467. }
  468. static inline void __iomem *
  469. get_perf_reg_address(struct intel_iommu *iommu, u32 offset)
  470. {
  471. u32 off = dmar_readl(iommu->reg + offset);
  472. return iommu->reg + off;
  473. }
  474. int alloc_iommu_pmu(struct intel_iommu *iommu)
  475. {
  476. struct iommu_pmu *iommu_pmu;
  477. int i, j, ret;
  478. u64 perfcap;
  479. u32 cap;
  480. if (!ecap_pms(iommu->ecap))
  481. return 0;
  482. /* The IOMMU PMU requires the ECMD support as well */
  483. if (!cap_ecmds(iommu->cap))
  484. return -ENODEV;
  485. perfcap = dmar_readq(iommu->reg + DMAR_PERFCAP_REG);
  486. /* The performance monitoring is not supported. */
  487. if (!perfcap)
  488. return -ENODEV;
  489. /* Sanity check for the number of the counters and event groups */
  490. if (!pcap_num_cntr(perfcap) || !pcap_num_event_group(perfcap))
  491. return -ENODEV;
  492. /* The interrupt on overflow is required */
  493. if (!pcap_interrupt(perfcap))
  494. return -ENODEV;
  495. /* Check required Enhanced Command Capability */
  496. if (!ecmd_has_pmu_essential(iommu))
  497. return -ENODEV;
  498. iommu_pmu = kzalloc(sizeof(*iommu_pmu), GFP_KERNEL);
  499. if (!iommu_pmu)
  500. return -ENOMEM;
  501. iommu_pmu->num_cntr = pcap_num_cntr(perfcap);
  502. if (iommu_pmu->num_cntr > IOMMU_PMU_IDX_MAX) {
  503. pr_warn_once("The number of IOMMU counters %d > max(%d), clipping!",
  504. iommu_pmu->num_cntr, IOMMU_PMU_IDX_MAX);
  505. iommu_pmu->num_cntr = IOMMU_PMU_IDX_MAX;
  506. }
  507. iommu_pmu->cntr_width = pcap_cntr_width(perfcap);
  508. iommu_pmu->filter = pcap_filters_mask(perfcap);
  509. iommu_pmu->cntr_stride = pcap_cntr_stride(perfcap);
  510. iommu_pmu->num_eg = pcap_num_event_group(perfcap);
  511. iommu_pmu->evcap = kcalloc(iommu_pmu->num_eg, sizeof(u64), GFP_KERNEL);
  512. if (!iommu_pmu->evcap) {
  513. ret = -ENOMEM;
  514. goto free_pmu;
  515. }
  516. /* Parse event group capabilities */
  517. for (i = 0; i < iommu_pmu->num_eg; i++) {
  518. u64 pcap;
  519. pcap = dmar_readq(iommu->reg + DMAR_PERFEVNTCAP_REG +
  520. i * IOMMU_PMU_CAP_REGS_STEP);
  521. iommu_pmu->evcap[i] = pecap_es(pcap);
  522. }
  523. iommu_pmu->cntr_evcap = kcalloc(iommu_pmu->num_cntr, sizeof(u32 *), GFP_KERNEL);
  524. if (!iommu_pmu->cntr_evcap) {
  525. ret = -ENOMEM;
  526. goto free_pmu_evcap;
  527. }
  528. for (i = 0; i < iommu_pmu->num_cntr; i++) {
  529. iommu_pmu->cntr_evcap[i] = kcalloc(iommu_pmu->num_eg, sizeof(u32), GFP_KERNEL);
  530. if (!iommu_pmu->cntr_evcap[i]) {
  531. ret = -ENOMEM;
  532. goto free_pmu_cntr_evcap;
  533. }
  534. /*
  535. * Set to the global capabilities, will adjust according
  536. * to per-counter capabilities later.
  537. */
  538. for (j = 0; j < iommu_pmu->num_eg; j++)
  539. iommu_pmu->cntr_evcap[i][j] = (u32)iommu_pmu->evcap[j];
  540. }
  541. iommu_pmu->cfg_reg = get_perf_reg_address(iommu, DMAR_PERFCFGOFF_REG);
  542. iommu_pmu->cntr_reg = get_perf_reg_address(iommu, DMAR_PERFCNTROFF_REG);
  543. iommu_pmu->overflow = get_perf_reg_address(iommu, DMAR_PERFOVFOFF_REG);
  544. /*
  545. * Check per-counter capabilities. All counters should have the
  546. * same capabilities on Interrupt on Overflow Support and Counter
  547. * Width.
  548. */
  549. for (i = 0; i < iommu_pmu->num_cntr; i++) {
  550. cap = dmar_readl(iommu_pmu->cfg_reg +
  551. i * IOMMU_PMU_CFG_OFFSET +
  552. IOMMU_PMU_CFG_CNTRCAP_OFFSET);
  553. if (!iommu_cntrcap_pcc(cap))
  554. continue;
  555. /*
  556. * It's possible that some counters have a different
  557. * capability because of e.g., HW bug. Check the corner
  558. * case here and simply drop those counters.
  559. */
  560. if ((iommu_cntrcap_cw(cap) != iommu_pmu->cntr_width) ||
  561. !iommu_cntrcap_ios(cap)) {
  562. iommu_pmu->num_cntr = i;
  563. pr_warn("PMU counter capability inconsistent, counter number reduced to %d\n",
  564. iommu_pmu->num_cntr);
  565. }
  566. /* Clear the pre-defined events group */
  567. for (j = 0; j < iommu_pmu->num_eg; j++)
  568. iommu_pmu->cntr_evcap[i][j] = 0;
  569. /* Override with per-counter event capabilities */
  570. for (j = 0; j < iommu_cntrcap_egcnt(cap); j++) {
  571. cap = dmar_readl(iommu_pmu->cfg_reg + i * IOMMU_PMU_CFG_OFFSET +
  572. IOMMU_PMU_CFG_CNTREVCAP_OFFSET +
  573. (j * IOMMU_PMU_OFF_REGS_STEP));
  574. iommu_pmu->cntr_evcap[i][iommu_event_group(cap)] = iommu_event_select(cap);
  575. /*
  576. * Some events may only be supported by a specific counter.
  577. * Track them in the evcap as well.
  578. */
  579. iommu_pmu->evcap[iommu_event_group(cap)] |= iommu_event_select(cap);
  580. }
  581. }
  582. iommu_pmu->iommu = iommu;
  583. iommu->pmu = iommu_pmu;
  584. return 0;
  585. free_pmu_cntr_evcap:
  586. for (i = 0; i < iommu_pmu->num_cntr; i++)
  587. kfree(iommu_pmu->cntr_evcap[i]);
  588. kfree(iommu_pmu->cntr_evcap);
  589. free_pmu_evcap:
  590. kfree(iommu_pmu->evcap);
  591. free_pmu:
  592. kfree(iommu_pmu);
  593. return ret;
  594. }
  595. void free_iommu_pmu(struct intel_iommu *iommu)
  596. {
  597. struct iommu_pmu *iommu_pmu = iommu->pmu;
  598. if (!iommu_pmu)
  599. return;
  600. if (iommu_pmu->evcap) {
  601. int i;
  602. for (i = 0; i < iommu_pmu->num_cntr; i++)
  603. kfree(iommu_pmu->cntr_evcap[i]);
  604. kfree(iommu_pmu->cntr_evcap);
  605. }
  606. kfree(iommu_pmu->evcap);
  607. kfree(iommu_pmu);
  608. iommu->pmu = NULL;
  609. }
  610. static int iommu_pmu_set_interrupt(struct intel_iommu *iommu)
  611. {
  612. struct iommu_pmu *iommu_pmu = iommu->pmu;
  613. int irq, ret;
  614. irq = dmar_alloc_hwirq(IOMMU_IRQ_ID_OFFSET_PERF + iommu->seq_id, iommu->node, iommu);
  615. if (irq <= 0)
  616. return -EINVAL;
  617. snprintf(iommu_pmu->irq_name, sizeof(iommu_pmu->irq_name), "dmar%d-perf", iommu->seq_id);
  618. iommu->perf_irq = irq;
  619. ret = request_threaded_irq(irq, NULL, iommu_pmu_irq_handler,
  620. IRQF_ONESHOT, iommu_pmu->irq_name, iommu);
  621. if (ret) {
  622. dmar_free_hwirq(irq);
  623. iommu->perf_irq = 0;
  624. return ret;
  625. }
  626. return 0;
  627. }
  628. static void iommu_pmu_unset_interrupt(struct intel_iommu *iommu)
  629. {
  630. if (!iommu->perf_irq)
  631. return;
  632. free_irq(iommu->perf_irq, iommu);
  633. dmar_free_hwirq(iommu->perf_irq);
  634. iommu->perf_irq = 0;
  635. }
  636. void iommu_pmu_register(struct intel_iommu *iommu)
  637. {
  638. struct iommu_pmu *iommu_pmu = iommu->pmu;
  639. if (!iommu_pmu)
  640. return;
  641. if (__iommu_pmu_register(iommu))
  642. goto err;
  643. /* Set interrupt for overflow */
  644. if (iommu_pmu_set_interrupt(iommu))
  645. goto unregister;
  646. return;
  647. unregister:
  648. perf_pmu_unregister(&iommu_pmu->pmu);
  649. err:
  650. pr_err("Failed to register PMU for iommu (seq_id = %d)\n", iommu->seq_id);
  651. free_iommu_pmu(iommu);
  652. }
  653. void iommu_pmu_unregister(struct intel_iommu *iommu)
  654. {
  655. struct iommu_pmu *iommu_pmu = iommu->pmu;
  656. if (!iommu_pmu)
  657. return;
  658. iommu_pmu_unset_interrupt(iommu);
  659. perf_pmu_unregister(&iommu_pmu->pmu);
  660. }