marvell_cn10k_tad_pmu.c 13 KB

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