perfmon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2020 Intel Corporation. All rights rsvd. */
  3. #include <linux/sched/task.h>
  4. #include <linux/io-64-nonatomic-lo-hi.h>
  5. #include "idxd.h"
  6. #include "perfmon.h"
  7. /*
  8. * These attributes specify the bits in the config word that the perf
  9. * syscall uses to pass the event ids and categories to perfmon.
  10. */
  11. DEFINE_PERFMON_FORMAT_ATTR(event_category, "config:0-3");
  12. DEFINE_PERFMON_FORMAT_ATTR(event, "config:4-31");
  13. /*
  14. * These attributes specify the bits in the config1 word that the perf
  15. * syscall uses to pass filter data to perfmon.
  16. */
  17. DEFINE_PERFMON_FORMAT_ATTR(filter_wq, "config1:0-31");
  18. DEFINE_PERFMON_FORMAT_ATTR(filter_tc, "config1:32-39");
  19. DEFINE_PERFMON_FORMAT_ATTR(filter_pgsz, "config1:40-43");
  20. DEFINE_PERFMON_FORMAT_ATTR(filter_sz, "config1:44-51");
  21. DEFINE_PERFMON_FORMAT_ATTR(filter_eng, "config1:52-59");
  22. #define PERFMON_FILTERS_START 2
  23. #define PERFMON_FILTERS_MAX 5
  24. static struct attribute *perfmon_format_attrs[] = {
  25. &format_attr_idxd_event_category.attr,
  26. &format_attr_idxd_event.attr,
  27. &format_attr_idxd_filter_wq.attr,
  28. &format_attr_idxd_filter_tc.attr,
  29. &format_attr_idxd_filter_pgsz.attr,
  30. &format_attr_idxd_filter_sz.attr,
  31. &format_attr_idxd_filter_eng.attr,
  32. NULL,
  33. };
  34. static struct attribute_group perfmon_format_attr_group = {
  35. .name = "format",
  36. .attrs = perfmon_format_attrs,
  37. };
  38. static const struct attribute_group *perfmon_attr_groups[] = {
  39. &perfmon_format_attr_group,
  40. NULL,
  41. };
  42. static bool is_idxd_event(struct idxd_pmu *idxd_pmu, struct perf_event *event)
  43. {
  44. return &idxd_pmu->pmu == event->pmu;
  45. }
  46. static int perfmon_collect_events(struct idxd_pmu *idxd_pmu,
  47. struct perf_event *leader,
  48. bool do_grp)
  49. {
  50. struct perf_event *event;
  51. int n, max_count;
  52. max_count = idxd_pmu->n_counters;
  53. n = idxd_pmu->n_events;
  54. if (n >= max_count)
  55. return -EINVAL;
  56. if (is_idxd_event(idxd_pmu, leader)) {
  57. idxd_pmu->event_list[n] = leader;
  58. idxd_pmu->event_list[n]->hw.idx = n;
  59. n++;
  60. }
  61. if (!do_grp)
  62. return n;
  63. for_each_sibling_event(event, leader) {
  64. if (!is_idxd_event(idxd_pmu, event) ||
  65. event->state <= PERF_EVENT_STATE_OFF)
  66. continue;
  67. if (n >= max_count)
  68. return -EINVAL;
  69. idxd_pmu->event_list[n] = event;
  70. idxd_pmu->event_list[n]->hw.idx = n;
  71. n++;
  72. }
  73. return n;
  74. }
  75. static void perfmon_assign_hw_event(struct idxd_pmu *idxd_pmu,
  76. struct perf_event *event, int idx)
  77. {
  78. struct idxd_device *idxd = idxd_pmu->idxd;
  79. struct hw_perf_event *hwc = &event->hw;
  80. hwc->idx = idx;
  81. hwc->config_base = ioread64(CNTRCFG_REG(idxd, idx));
  82. hwc->event_base = ioread64(CNTRCFG_REG(idxd, idx));
  83. }
  84. static int perfmon_assign_event(struct idxd_pmu *idxd_pmu,
  85. struct perf_event *event)
  86. {
  87. int i;
  88. for (i = 0; i < IDXD_PMU_EVENT_MAX; i++)
  89. if (!test_and_set_bit(i, idxd_pmu->used_mask))
  90. return i;
  91. return -EINVAL;
  92. }
  93. /*
  94. * Check whether there are enough counters to satisfy that all the
  95. * events in the group can actually be scheduled at the same time.
  96. *
  97. * To do this, create a fake idxd_pmu object so the event collection
  98. * and assignment functions can be used without affecting the internal
  99. * state of the real idxd_pmu object.
  100. */
  101. static int perfmon_validate_group(struct idxd_pmu *pmu,
  102. struct perf_event *event)
  103. {
  104. struct perf_event *leader = event->group_leader;
  105. struct idxd_pmu *fake_pmu;
  106. int i, ret = 0, n, idx;
  107. fake_pmu = kzalloc(sizeof(*fake_pmu), GFP_KERNEL);
  108. if (!fake_pmu)
  109. return -ENOMEM;
  110. fake_pmu->pmu.name = pmu->pmu.name;
  111. fake_pmu->n_counters = pmu->n_counters;
  112. n = perfmon_collect_events(fake_pmu, leader, true);
  113. if (n < 0) {
  114. ret = n;
  115. goto out;
  116. }
  117. fake_pmu->n_events = n;
  118. n = perfmon_collect_events(fake_pmu, event, false);
  119. if (n < 0) {
  120. ret = n;
  121. goto out;
  122. }
  123. fake_pmu->n_events = n;
  124. for (i = 0; i < n; i++) {
  125. event = fake_pmu->event_list[i];
  126. idx = perfmon_assign_event(fake_pmu, event);
  127. if (idx < 0) {
  128. ret = idx;
  129. goto out;
  130. }
  131. }
  132. out:
  133. kfree(fake_pmu);
  134. return ret;
  135. }
  136. static int perfmon_pmu_event_init(struct perf_event *event)
  137. {
  138. struct idxd_device *idxd;
  139. int ret = 0;
  140. idxd = event_to_idxd(event);
  141. event->hw.idx = -1;
  142. if (event->attr.type != event->pmu->type)
  143. return -ENOENT;
  144. /* sampling not supported */
  145. if (event->attr.sample_period)
  146. return -EINVAL;
  147. if (event->cpu < 0)
  148. return -EINVAL;
  149. if (event->pmu != &idxd->idxd_pmu->pmu)
  150. return -EINVAL;
  151. event->hw.event_base = ioread64(PERFMON_TABLE_OFFSET(idxd));
  152. event->hw.config = event->attr.config;
  153. if (event->group_leader != event)
  154. /* non-group events have themselves as leader */
  155. ret = perfmon_validate_group(idxd->idxd_pmu, event);
  156. return ret;
  157. }
  158. static inline u64 perfmon_pmu_read_counter(struct perf_event *event)
  159. {
  160. struct hw_perf_event *hwc = &event->hw;
  161. struct idxd_device *idxd;
  162. int cntr = hwc->idx;
  163. idxd = event_to_idxd(event);
  164. return ioread64(CNTRDATA_REG(idxd, cntr));
  165. }
  166. static void perfmon_pmu_event_update(struct perf_event *event)
  167. {
  168. struct idxd_device *idxd = event_to_idxd(event);
  169. u64 prev_raw_count, new_raw_count, delta, p, n;
  170. int shift = 64 - idxd->idxd_pmu->counter_width;
  171. struct hw_perf_event *hwc = &event->hw;
  172. prev_raw_count = local64_read(&hwc->prev_count);
  173. do {
  174. new_raw_count = perfmon_pmu_read_counter(event);
  175. } while (!local64_try_cmpxchg(&hwc->prev_count,
  176. &prev_raw_count, new_raw_count));
  177. n = (new_raw_count << shift);
  178. p = (prev_raw_count << shift);
  179. delta = ((n - p) >> shift);
  180. local64_add(delta, &event->count);
  181. }
  182. void perfmon_counter_overflow(struct idxd_device *idxd)
  183. {
  184. int i, n_counters, max_loop = OVERFLOW_SIZE;
  185. struct perf_event *event;
  186. unsigned long ovfstatus;
  187. n_counters = min(idxd->idxd_pmu->n_counters, OVERFLOW_SIZE);
  188. ovfstatus = ioread32(OVFSTATUS_REG(idxd));
  189. /*
  190. * While updating overflowed counters, other counters behind
  191. * them could overflow and be missed in a given pass.
  192. * Normally this could happen at most n_counters times, but in
  193. * theory a tiny counter width could result in continual
  194. * overflows and endless looping. max_loop provides a
  195. * failsafe in that highly unlikely case.
  196. */
  197. while (ovfstatus && max_loop--) {
  198. /* Figure out which counter(s) overflowed */
  199. for_each_set_bit(i, &ovfstatus, n_counters) {
  200. unsigned long ovfstatus_clear = 0;
  201. /* Update event->count for overflowed counter */
  202. event = idxd->idxd_pmu->event_list[i];
  203. perfmon_pmu_event_update(event);
  204. /* Writing 1 to OVFSTATUS bit clears it */
  205. set_bit(i, &ovfstatus_clear);
  206. iowrite32(ovfstatus_clear, OVFSTATUS_REG(idxd));
  207. }
  208. ovfstatus = ioread32(OVFSTATUS_REG(idxd));
  209. }
  210. /*
  211. * Should never happen. If so, it means a counter(s) looped
  212. * around twice while this handler was running.
  213. */
  214. WARN_ON_ONCE(ovfstatus);
  215. }
  216. static inline void perfmon_reset_config(struct idxd_device *idxd)
  217. {
  218. iowrite32(CONFIG_RESET, PERFRST_REG(idxd));
  219. iowrite32(0, OVFSTATUS_REG(idxd));
  220. iowrite32(0, PERFFRZ_REG(idxd));
  221. }
  222. static inline void perfmon_reset_counters(struct idxd_device *idxd)
  223. {
  224. iowrite32(CNTR_RESET, PERFRST_REG(idxd));
  225. }
  226. static inline void perfmon_reset(struct idxd_device *idxd)
  227. {
  228. perfmon_reset_config(idxd);
  229. perfmon_reset_counters(idxd);
  230. }
  231. static void perfmon_pmu_event_start(struct perf_event *event, int mode)
  232. {
  233. u32 flt_wq, flt_tc, flt_pg_sz, flt_xfer_sz, flt_eng = 0;
  234. u64 cntr_cfg, cntrdata, event_enc, event_cat = 0;
  235. struct hw_perf_event *hwc = &event->hw;
  236. union filter_cfg flt_cfg;
  237. union event_cfg event_cfg;
  238. struct idxd_device *idxd;
  239. int cntr;
  240. idxd = event_to_idxd(event);
  241. event->hw.idx = hwc->idx;
  242. cntr = hwc->idx;
  243. /* Obtain event category and event value from user space */
  244. event_cfg.val = event->attr.config;
  245. flt_cfg.val = event->attr.config1;
  246. event_cat = event_cfg.event_cat;
  247. event_enc = event_cfg.event_enc;
  248. /* Obtain filter configuration from user space */
  249. flt_wq = flt_cfg.wq;
  250. flt_tc = flt_cfg.tc;
  251. flt_pg_sz = flt_cfg.pg_sz;
  252. flt_xfer_sz = flt_cfg.xfer_sz;
  253. flt_eng = flt_cfg.eng;
  254. if (flt_wq && test_bit(FLT_WQ, &idxd->idxd_pmu->supported_filters))
  255. iowrite32(flt_wq, FLTCFG_REG(idxd, cntr, FLT_WQ));
  256. if (flt_tc && test_bit(FLT_TC, &idxd->idxd_pmu->supported_filters))
  257. iowrite32(flt_tc, FLTCFG_REG(idxd, cntr, FLT_TC));
  258. if (flt_pg_sz && test_bit(FLT_PG_SZ, &idxd->idxd_pmu->supported_filters))
  259. iowrite32(flt_pg_sz, FLTCFG_REG(idxd, cntr, FLT_PG_SZ));
  260. if (flt_xfer_sz && test_bit(FLT_XFER_SZ, &idxd->idxd_pmu->supported_filters))
  261. iowrite32(flt_xfer_sz, FLTCFG_REG(idxd, cntr, FLT_XFER_SZ));
  262. if (flt_eng && test_bit(FLT_ENG, &idxd->idxd_pmu->supported_filters))
  263. iowrite32(flt_eng, FLTCFG_REG(idxd, cntr, FLT_ENG));
  264. /* Read the start value */
  265. cntrdata = ioread64(CNTRDATA_REG(idxd, cntr));
  266. local64_set(&event->hw.prev_count, cntrdata);
  267. /* Set counter to event/category */
  268. cntr_cfg = event_cat << CNTRCFG_CATEGORY_SHIFT;
  269. cntr_cfg |= event_enc << CNTRCFG_EVENT_SHIFT;
  270. /* Set interrupt on overflow and counter enable bits */
  271. cntr_cfg |= (CNTRCFG_IRQ_OVERFLOW | CNTRCFG_ENABLE);
  272. iowrite64(cntr_cfg, CNTRCFG_REG(idxd, cntr));
  273. }
  274. static void perfmon_pmu_event_stop(struct perf_event *event, int mode)
  275. {
  276. struct hw_perf_event *hwc = &event->hw;
  277. struct idxd_device *idxd;
  278. int i, cntr = hwc->idx;
  279. u64 cntr_cfg;
  280. idxd = event_to_idxd(event);
  281. /* remove this event from event list */
  282. for (i = 0; i < idxd->idxd_pmu->n_events; i++) {
  283. if (event != idxd->idxd_pmu->event_list[i])
  284. continue;
  285. for (++i; i < idxd->idxd_pmu->n_events; i++)
  286. idxd->idxd_pmu->event_list[i - 1] = idxd->idxd_pmu->event_list[i];
  287. --idxd->idxd_pmu->n_events;
  288. break;
  289. }
  290. cntr_cfg = ioread64(CNTRCFG_REG(idxd, cntr));
  291. cntr_cfg &= ~CNTRCFG_ENABLE;
  292. iowrite64(cntr_cfg, CNTRCFG_REG(idxd, cntr));
  293. if (mode == PERF_EF_UPDATE)
  294. perfmon_pmu_event_update(event);
  295. event->hw.idx = -1;
  296. clear_bit(cntr, idxd->idxd_pmu->used_mask);
  297. }
  298. static void perfmon_pmu_event_del(struct perf_event *event, int mode)
  299. {
  300. perfmon_pmu_event_stop(event, PERF_EF_UPDATE);
  301. }
  302. static int perfmon_pmu_event_add(struct perf_event *event, int flags)
  303. {
  304. struct idxd_device *idxd = event_to_idxd(event);
  305. struct idxd_pmu *idxd_pmu = idxd->idxd_pmu;
  306. struct hw_perf_event *hwc = &event->hw;
  307. int idx, n;
  308. n = perfmon_collect_events(idxd_pmu, event, false);
  309. if (n < 0)
  310. return n;
  311. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  312. if (!(flags & PERF_EF_START))
  313. hwc->state |= PERF_HES_ARCH;
  314. idx = perfmon_assign_event(idxd_pmu, event);
  315. if (idx < 0)
  316. return idx;
  317. perfmon_assign_hw_event(idxd_pmu, event, idx);
  318. if (flags & PERF_EF_START)
  319. perfmon_pmu_event_start(event, 0);
  320. idxd_pmu->n_events = n;
  321. return 0;
  322. }
  323. static void enable_perfmon_pmu(struct idxd_device *idxd)
  324. {
  325. iowrite32(COUNTER_UNFREEZE, PERFFRZ_REG(idxd));
  326. }
  327. static void disable_perfmon_pmu(struct idxd_device *idxd)
  328. {
  329. iowrite32(COUNTER_FREEZE, PERFFRZ_REG(idxd));
  330. }
  331. static void perfmon_pmu_enable(struct pmu *pmu)
  332. {
  333. struct idxd_device *idxd = pmu_to_idxd(pmu);
  334. enable_perfmon_pmu(idxd);
  335. }
  336. static void perfmon_pmu_disable(struct pmu *pmu)
  337. {
  338. struct idxd_device *idxd = pmu_to_idxd(pmu);
  339. disable_perfmon_pmu(idxd);
  340. }
  341. static void skip_filter(int i)
  342. {
  343. int j;
  344. for (j = i; j < PERFMON_FILTERS_MAX; j++)
  345. perfmon_format_attrs[PERFMON_FILTERS_START + j] =
  346. perfmon_format_attrs[PERFMON_FILTERS_START + j + 1];
  347. }
  348. static void idxd_pmu_init(struct idxd_pmu *idxd_pmu)
  349. {
  350. int i;
  351. for (i = 0 ; i < PERFMON_FILTERS_MAX; i++) {
  352. if (!test_bit(i, &idxd_pmu->supported_filters))
  353. skip_filter(i);
  354. }
  355. idxd_pmu->pmu.name = idxd_pmu->name;
  356. idxd_pmu->pmu.attr_groups = perfmon_attr_groups;
  357. idxd_pmu->pmu.task_ctx_nr = perf_invalid_context;
  358. idxd_pmu->pmu.event_init = perfmon_pmu_event_init;
  359. idxd_pmu->pmu.pmu_enable = perfmon_pmu_enable;
  360. idxd_pmu->pmu.pmu_disable = perfmon_pmu_disable;
  361. idxd_pmu->pmu.add = perfmon_pmu_event_add;
  362. idxd_pmu->pmu.del = perfmon_pmu_event_del;
  363. idxd_pmu->pmu.start = perfmon_pmu_event_start;
  364. idxd_pmu->pmu.stop = perfmon_pmu_event_stop;
  365. idxd_pmu->pmu.read = perfmon_pmu_event_update;
  366. idxd_pmu->pmu.capabilities = PERF_PMU_CAP_NO_EXCLUDE;
  367. idxd_pmu->pmu.scope = PERF_PMU_SCOPE_SYS_WIDE;
  368. idxd_pmu->pmu.module = THIS_MODULE;
  369. }
  370. void perfmon_pmu_remove(struct idxd_device *idxd)
  371. {
  372. if (!idxd->idxd_pmu)
  373. return;
  374. perf_pmu_unregister(&idxd->idxd_pmu->pmu);
  375. kfree(idxd->idxd_pmu);
  376. idxd->idxd_pmu = NULL;
  377. }
  378. int perfmon_pmu_init(struct idxd_device *idxd)
  379. {
  380. union idxd_perfcap perfcap;
  381. struct idxd_pmu *idxd_pmu;
  382. int rc = -ENODEV;
  383. /*
  384. * If perfmon_offset or num_counters is 0, it means perfmon is
  385. * not supported on this hardware.
  386. */
  387. if (idxd->perfmon_offset == 0)
  388. return -ENODEV;
  389. idxd_pmu = kzalloc(sizeof(*idxd_pmu), GFP_KERNEL);
  390. if (!idxd_pmu)
  391. return -ENOMEM;
  392. idxd_pmu->idxd = idxd;
  393. idxd->idxd_pmu = idxd_pmu;
  394. if (idxd->data->type == IDXD_TYPE_DSA) {
  395. rc = sprintf(idxd_pmu->name, "dsa%d", idxd->id);
  396. if (rc < 0)
  397. goto free;
  398. } else if (idxd->data->type == IDXD_TYPE_IAX) {
  399. rc = sprintf(idxd_pmu->name, "iax%d", idxd->id);
  400. if (rc < 0)
  401. goto free;
  402. } else {
  403. goto free;
  404. }
  405. perfmon_reset(idxd);
  406. perfcap.bits = ioread64(PERFCAP_REG(idxd));
  407. /*
  408. * If total perf counter is 0, stop further registration.
  409. * This is necessary in order to support driver running on
  410. * guest which does not have pmon support.
  411. */
  412. if (perfcap.num_perf_counter == 0)
  413. goto free;
  414. /* A counter width of 0 means it can't count */
  415. if (perfcap.counter_width == 0)
  416. goto free;
  417. /* Overflow interrupt and counter freeze support must be available */
  418. if (!perfcap.overflow_interrupt || !perfcap.counter_freeze)
  419. goto free;
  420. /* Number of event categories cannot be 0 */
  421. if (perfcap.num_event_category == 0)
  422. goto free;
  423. /*
  424. * We don't support per-counter capabilities for now.
  425. */
  426. if (perfcap.cap_per_counter)
  427. goto free;
  428. idxd_pmu->n_event_categories = perfcap.num_event_category;
  429. idxd_pmu->supported_event_categories = perfcap.global_event_category;
  430. idxd_pmu->per_counter_caps_supported = perfcap.cap_per_counter;
  431. /* check filter capability. If 0, then filters are not supported */
  432. idxd_pmu->supported_filters = perfcap.filter;
  433. if (perfcap.filter)
  434. idxd_pmu->n_filters = hweight8(perfcap.filter);
  435. /* Store the total number of counters categories, and counter width */
  436. idxd_pmu->n_counters = perfcap.num_perf_counter;
  437. idxd_pmu->counter_width = perfcap.counter_width;
  438. idxd_pmu_init(idxd_pmu);
  439. rc = perf_pmu_register(&idxd_pmu->pmu, idxd_pmu->name, -1);
  440. if (rc)
  441. goto free;
  442. out:
  443. return rc;
  444. free:
  445. kfree(idxd_pmu);
  446. idxd->idxd_pmu = NULL;
  447. goto out;
  448. }