marvell_cn10k_tad_pmu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Marvell CN10K LLC-TAD perf driver
  3. *
  4. * Copyright (C) 2021 Marvell
  5. */
  6. #define pr_fmt(fmt) "tad_pmu: " fmt
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/of_address.h>
  10. #include <linux/of_device.h>
  11. #include <linux/cpuhotplug.h>
  12. #include <linux/perf_event.h>
  13. #include <linux/platform_device.h>
  14. #define TAD_PFC_OFFSET 0x800
  15. #define TAD_PFC(counter) (TAD_PFC_OFFSET | (counter << 3))
  16. #define TAD_PRF_OFFSET 0x900
  17. #define TAD_PRF(counter) (TAD_PRF_OFFSET | (counter << 3))
  18. #define TAD_PRF_CNTSEL_MASK 0xFF
  19. #define TAD_MAX_COUNTERS 8
  20. #define to_tad_pmu(p) (container_of(p, struct tad_pmu, pmu))
  21. struct tad_region {
  22. void __iomem *base;
  23. };
  24. struct tad_pmu {
  25. struct pmu pmu;
  26. struct tad_region *regions;
  27. u32 region_cnt;
  28. unsigned int cpu;
  29. struct hlist_node node;
  30. struct perf_event *events[TAD_MAX_COUNTERS];
  31. DECLARE_BITMAP(counters_map, TAD_MAX_COUNTERS);
  32. };
  33. static int tad_pmu_cpuhp_state;
  34. static void tad_pmu_event_counter_read(struct perf_event *event)
  35. {
  36. struct tad_pmu *tad_pmu = to_tad_pmu(event->pmu);
  37. struct hw_perf_event *hwc = &event->hw;
  38. u32 counter_idx = hwc->idx;
  39. u64 prev, new;
  40. int i;
  41. do {
  42. prev = local64_read(&hwc->prev_count);
  43. for (i = 0, new = 0; i < tad_pmu->region_cnt; i++)
  44. new += readq(tad_pmu->regions[i].base +
  45. TAD_PFC(counter_idx));
  46. } while (local64_cmpxchg(&hwc->prev_count, prev, new) != prev);
  47. local64_add(new - prev, &event->count);
  48. }
  49. static void tad_pmu_event_counter_stop(struct perf_event *event, int flags)
  50. {
  51. struct tad_pmu *tad_pmu = to_tad_pmu(event->pmu);
  52. struct hw_perf_event *hwc = &event->hw;
  53. u32 counter_idx = hwc->idx;
  54. int i;
  55. /* TAD()_PFC() stop counting on the write
  56. * which sets TAD()_PRF()[CNTSEL] == 0
  57. */
  58. for (i = 0; i < tad_pmu->region_cnt; i++) {
  59. writeq_relaxed(0, tad_pmu->regions[i].base +
  60. TAD_PRF(counter_idx));
  61. }
  62. tad_pmu_event_counter_read(event);
  63. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  64. }
  65. static void tad_pmu_event_counter_start(struct perf_event *event, int flags)
  66. {
  67. struct tad_pmu *tad_pmu = to_tad_pmu(event->pmu);
  68. struct hw_perf_event *hwc = &event->hw;
  69. u32 event_idx = event->attr.config;
  70. u32 counter_idx = hwc->idx;
  71. u64 reg_val;
  72. int i;
  73. hwc->state = 0;
  74. /* Typically TAD_PFC() are zeroed to start counting */
  75. for (i = 0; i < tad_pmu->region_cnt; i++)
  76. writeq_relaxed(0, tad_pmu->regions[i].base +
  77. TAD_PFC(counter_idx));
  78. /* TAD()_PFC() start counting on the write
  79. * which sets TAD()_PRF()[CNTSEL] != 0
  80. */
  81. for (i = 0; i < tad_pmu->region_cnt; i++) {
  82. reg_val = event_idx & 0xFF;
  83. writeq_relaxed(reg_val, tad_pmu->regions[i].base +
  84. TAD_PRF(counter_idx));
  85. }
  86. }
  87. static void tad_pmu_event_counter_del(struct perf_event *event, int flags)
  88. {
  89. struct tad_pmu *tad_pmu = to_tad_pmu(event->pmu);
  90. struct hw_perf_event *hwc = &event->hw;
  91. int idx = hwc->idx;
  92. tad_pmu_event_counter_stop(event, flags | PERF_EF_UPDATE);
  93. tad_pmu->events[idx] = NULL;
  94. clear_bit(idx, tad_pmu->counters_map);
  95. }
  96. static int tad_pmu_event_counter_add(struct perf_event *event, int flags)
  97. {
  98. struct tad_pmu *tad_pmu = to_tad_pmu(event->pmu);
  99. struct hw_perf_event *hwc = &event->hw;
  100. int idx;
  101. /* Get a free counter for this event */
  102. idx = find_first_zero_bit(tad_pmu->counters_map, TAD_MAX_COUNTERS);
  103. if (idx == TAD_MAX_COUNTERS)
  104. return -EAGAIN;
  105. set_bit(idx, tad_pmu->counters_map);
  106. hwc->idx = idx;
  107. hwc->state = PERF_HES_STOPPED;
  108. tad_pmu->events[idx] = event;
  109. if (flags & PERF_EF_START)
  110. tad_pmu_event_counter_start(event, flags);
  111. return 0;
  112. }
  113. static int tad_pmu_event_init(struct perf_event *event)
  114. {
  115. struct tad_pmu *tad_pmu = to_tad_pmu(event->pmu);
  116. if (event->attr.type != event->pmu->type)
  117. return -ENOENT;
  118. if (!event->attr.disabled)
  119. return -EINVAL;
  120. if (event->state != PERF_EVENT_STATE_OFF)
  121. return -EINVAL;
  122. event->cpu = tad_pmu->cpu;
  123. event->hw.idx = -1;
  124. event->hw.config_base = event->attr.config;
  125. return 0;
  126. }
  127. static ssize_t tad_pmu_event_show(struct device *dev,
  128. struct device_attribute *attr, char *page)
  129. {
  130. struct perf_pmu_events_attr *pmu_attr;
  131. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
  132. return sysfs_emit(page, "event=0x%02llx\n", pmu_attr->id);
  133. }
  134. #define TAD_PMU_EVENT_ATTR(name, config) \
  135. PMU_EVENT_ATTR_ID(name, tad_pmu_event_show, config)
  136. static struct attribute *tad_pmu_event_attrs[] = {
  137. TAD_PMU_EVENT_ATTR(tad_none, 0x0),
  138. TAD_PMU_EVENT_ATTR(tad_req_msh_in_any, 0x1),
  139. TAD_PMU_EVENT_ATTR(tad_req_msh_in_mn, 0x2),
  140. TAD_PMU_EVENT_ATTR(tad_req_msh_in_exlmn, 0x3),
  141. TAD_PMU_EVENT_ATTR(tad_rsp_msh_in_any, 0x4),
  142. TAD_PMU_EVENT_ATTR(tad_rsp_msh_in_mn, 0x5),
  143. TAD_PMU_EVENT_ATTR(tad_rsp_msh_in_exlmn, 0x6),
  144. TAD_PMU_EVENT_ATTR(tad_rsp_msh_in_dss, 0x7),
  145. TAD_PMU_EVENT_ATTR(tad_rsp_msh_in_retry_dss, 0x8),
  146. TAD_PMU_EVENT_ATTR(tad_dat_msh_in_any, 0x9),
  147. TAD_PMU_EVENT_ATTR(tad_dat_msh_in_dss, 0xa),
  148. TAD_PMU_EVENT_ATTR(tad_req_msh_out_any, 0xb),
  149. TAD_PMU_EVENT_ATTR(tad_req_msh_out_dss_rd, 0xc),
  150. TAD_PMU_EVENT_ATTR(tad_req_msh_out_dss_wr, 0xd),
  151. TAD_PMU_EVENT_ATTR(tad_req_msh_out_evict, 0xe),
  152. TAD_PMU_EVENT_ATTR(tad_rsp_msh_out_any, 0xf),
  153. TAD_PMU_EVENT_ATTR(tad_rsp_msh_out_retry_exlmn, 0x10),
  154. TAD_PMU_EVENT_ATTR(tad_rsp_msh_out_retry_mn, 0x11),
  155. TAD_PMU_EVENT_ATTR(tad_rsp_msh_out_exlmn, 0x12),
  156. TAD_PMU_EVENT_ATTR(tad_rsp_msh_out_mn, 0x13),
  157. TAD_PMU_EVENT_ATTR(tad_snp_msh_out_any, 0x14),
  158. TAD_PMU_EVENT_ATTR(tad_snp_msh_out_mn, 0x15),
  159. TAD_PMU_EVENT_ATTR(tad_snp_msh_out_exlmn, 0x16),
  160. TAD_PMU_EVENT_ATTR(tad_dat_msh_out_any, 0x17),
  161. TAD_PMU_EVENT_ATTR(tad_dat_msh_out_fill, 0x18),
  162. TAD_PMU_EVENT_ATTR(tad_dat_msh_out_dss, 0x19),
  163. TAD_PMU_EVENT_ATTR(tad_alloc_dtg, 0x1a),
  164. TAD_PMU_EVENT_ATTR(tad_alloc_ltg, 0x1b),
  165. TAD_PMU_EVENT_ATTR(tad_alloc_any, 0x1c),
  166. TAD_PMU_EVENT_ATTR(tad_hit_dtg, 0x1d),
  167. TAD_PMU_EVENT_ATTR(tad_hit_ltg, 0x1e),
  168. TAD_PMU_EVENT_ATTR(tad_hit_any, 0x1f),
  169. TAD_PMU_EVENT_ATTR(tad_tag_rd, 0x20),
  170. TAD_PMU_EVENT_ATTR(tad_dat_rd, 0x21),
  171. TAD_PMU_EVENT_ATTR(tad_dat_rd_byp, 0x22),
  172. TAD_PMU_EVENT_ATTR(tad_ifb_occ, 0x23),
  173. TAD_PMU_EVENT_ATTR(tad_req_occ, 0x24),
  174. NULL
  175. };
  176. static const struct attribute_group tad_pmu_events_attr_group = {
  177. .name = "events",
  178. .attrs = tad_pmu_event_attrs,
  179. };
  180. PMU_FORMAT_ATTR(event, "config:0-7");
  181. static struct attribute *tad_pmu_format_attrs[] = {
  182. &format_attr_event.attr,
  183. NULL
  184. };
  185. static struct attribute_group tad_pmu_format_attr_group = {
  186. .name = "format",
  187. .attrs = tad_pmu_format_attrs,
  188. };
  189. static ssize_t tad_pmu_cpumask_show(struct device *dev,
  190. struct device_attribute *attr, char *buf)
  191. {
  192. struct tad_pmu *tad_pmu = to_tad_pmu(dev_get_drvdata(dev));
  193. return cpumap_print_to_pagebuf(true, buf, cpumask_of(tad_pmu->cpu));
  194. }
  195. static DEVICE_ATTR(cpumask, 0444, tad_pmu_cpumask_show, NULL);
  196. static struct attribute *tad_pmu_cpumask_attrs[] = {
  197. &dev_attr_cpumask.attr,
  198. NULL
  199. };
  200. static struct attribute_group tad_pmu_cpumask_attr_group = {
  201. .attrs = tad_pmu_cpumask_attrs,
  202. };
  203. static const struct attribute_group *tad_pmu_attr_groups[] = {
  204. &tad_pmu_events_attr_group,
  205. &tad_pmu_format_attr_group,
  206. &tad_pmu_cpumask_attr_group,
  207. NULL
  208. };
  209. static int tad_pmu_probe(struct platform_device *pdev)
  210. {
  211. struct device_node *node = pdev->dev.of_node;
  212. struct tad_region *regions;
  213. struct tad_pmu *tad_pmu;
  214. struct resource *res;
  215. u32 tad_pmu_page_size;
  216. u32 tad_page_size;
  217. u32 tad_cnt;
  218. int i, ret;
  219. char *name;
  220. tad_pmu = devm_kzalloc(&pdev->dev, sizeof(*tad_pmu), GFP_KERNEL);
  221. if (!tad_pmu)
  222. return -ENOMEM;
  223. platform_set_drvdata(pdev, tad_pmu);
  224. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  225. if (!res) {
  226. dev_err(&pdev->dev, "Mem resource not found\n");
  227. return -ENODEV;
  228. }
  229. ret = of_property_read_u32(node, "marvell,tad-page-size",
  230. &tad_page_size);
  231. if (ret) {
  232. dev_err(&pdev->dev, "Can't find tad-page-size property\n");
  233. return ret;
  234. }
  235. ret = of_property_read_u32(node, "marvell,tad-pmu-page-size",
  236. &tad_pmu_page_size);
  237. if (ret) {
  238. dev_err(&pdev->dev, "Can't find tad-pmu-page-size property\n");
  239. return ret;
  240. }
  241. ret = of_property_read_u32(node, "marvell,tad-cnt", &tad_cnt);
  242. if (ret) {
  243. dev_err(&pdev->dev, "Can't find tad-cnt property\n");
  244. return ret;
  245. }
  246. regions = devm_kcalloc(&pdev->dev, tad_cnt,
  247. sizeof(*regions), GFP_KERNEL);
  248. if (!regions)
  249. return -ENOMEM;
  250. /* ioremap the distributed TAD pmu regions */
  251. for (i = 0; i < tad_cnt && res->start < res->end; i++) {
  252. regions[i].base = devm_ioremap(&pdev->dev,
  253. res->start,
  254. tad_pmu_page_size);
  255. if (!regions[i].base) {
  256. dev_err(&pdev->dev, "TAD%d ioremap fail\n", i);
  257. return -ENOMEM;
  258. }
  259. res->start += tad_page_size;
  260. }
  261. tad_pmu->regions = regions;
  262. tad_pmu->region_cnt = tad_cnt;
  263. tad_pmu->pmu = (struct pmu) {
  264. .module = THIS_MODULE,
  265. .attr_groups = tad_pmu_attr_groups,
  266. .capabilities = PERF_PMU_CAP_NO_EXCLUDE |
  267. PERF_PMU_CAP_NO_INTERRUPT,
  268. .task_ctx_nr = perf_invalid_context,
  269. .event_init = tad_pmu_event_init,
  270. .add = tad_pmu_event_counter_add,
  271. .del = tad_pmu_event_counter_del,
  272. .start = tad_pmu_event_counter_start,
  273. .stop = tad_pmu_event_counter_stop,
  274. .read = tad_pmu_event_counter_read,
  275. };
  276. tad_pmu->cpu = raw_smp_processor_id();
  277. /* Register pmu instance for cpu hotplug */
  278. ret = cpuhp_state_add_instance_nocalls(tad_pmu_cpuhp_state,
  279. &tad_pmu->node);
  280. if (ret) {
  281. dev_err(&pdev->dev, "Error %d registering hotplug\n", ret);
  282. return ret;
  283. }
  284. name = "tad";
  285. ret = perf_pmu_register(&tad_pmu->pmu, name, -1);
  286. if (ret)
  287. cpuhp_state_remove_instance_nocalls(tad_pmu_cpuhp_state,
  288. &tad_pmu->node);
  289. return ret;
  290. }
  291. static int tad_pmu_remove(struct platform_device *pdev)
  292. {
  293. struct tad_pmu *pmu = platform_get_drvdata(pdev);
  294. cpuhp_state_remove_instance_nocalls(tad_pmu_cpuhp_state,
  295. &pmu->node);
  296. perf_pmu_unregister(&pmu->pmu);
  297. return 0;
  298. }
  299. #ifdef CONFIG_OF
  300. static const struct of_device_id tad_pmu_of_match[] = {
  301. { .compatible = "marvell,cn10k-tad-pmu", },
  302. {},
  303. };
  304. #endif
  305. static struct platform_driver tad_pmu_driver = {
  306. .driver = {
  307. .name = "cn10k_tad_pmu",
  308. .of_match_table = of_match_ptr(tad_pmu_of_match),
  309. .suppress_bind_attrs = true,
  310. },
  311. .probe = tad_pmu_probe,
  312. .remove = tad_pmu_remove,
  313. };
  314. static int tad_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
  315. {
  316. struct tad_pmu *pmu = hlist_entry_safe(node, struct tad_pmu, node);
  317. unsigned int target;
  318. if (cpu != pmu->cpu)
  319. return 0;
  320. target = cpumask_any_but(cpu_online_mask, cpu);
  321. if (target >= nr_cpu_ids)
  322. return 0;
  323. perf_pmu_migrate_context(&pmu->pmu, cpu, target);
  324. pmu->cpu = target;
  325. return 0;
  326. }
  327. static int __init tad_pmu_init(void)
  328. {
  329. int ret;
  330. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
  331. "perf/cn10k/tadpmu:online",
  332. NULL,
  333. tad_pmu_offline_cpu);
  334. if (ret < 0)
  335. return ret;
  336. tad_pmu_cpuhp_state = ret;
  337. ret = platform_driver_register(&tad_pmu_driver);
  338. if (ret)
  339. cpuhp_remove_multi_state(tad_pmu_cpuhp_state);
  340. return ret;
  341. }
  342. static void __exit tad_pmu_exit(void)
  343. {
  344. platform_driver_unregister(&tad_pmu_driver);
  345. cpuhp_remove_multi_state(tad_pmu_cpuhp_state);
  346. }
  347. module_init(tad_pmu_init);
  348. module_exit(tad_pmu_exit);
  349. MODULE_DESCRIPTION("Marvell CN10K LLC-TAD Perf driver");
  350. MODULE_AUTHOR("Bhaskara Budiredla <[email protected]>");
  351. MODULE_LICENSE("GPL v2");