riscv_pmu_sbi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * RISC-V performance counter support.
  4. *
  5. * Copyright (C) 2021 Western Digital Corporation or its affiliates.
  6. *
  7. * This code is based on ARM perf event code which is in turn based on
  8. * sparc64 and x86 code.
  9. */
  10. #define pr_fmt(fmt) "riscv-pmu-sbi: " fmt
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/perf/riscv_pmu.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/irq.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/of.h>
  18. #include <linux/cpu_pm.h>
  19. #include <linux/sched/clock.h>
  20. #include <asm/sbi.h>
  21. #include <asm/hwcap.h>
  22. PMU_FORMAT_ATTR(event, "config:0-47");
  23. PMU_FORMAT_ATTR(firmware, "config:63");
  24. static struct attribute *riscv_arch_formats_attr[] = {
  25. &format_attr_event.attr,
  26. &format_attr_firmware.attr,
  27. NULL,
  28. };
  29. static struct attribute_group riscv_pmu_format_group = {
  30. .name = "format",
  31. .attrs = riscv_arch_formats_attr,
  32. };
  33. static const struct attribute_group *riscv_pmu_attr_groups[] = {
  34. &riscv_pmu_format_group,
  35. NULL,
  36. };
  37. /*
  38. * RISC-V doesn't have hetergenous harts yet. This need to be part of
  39. * per_cpu in case of harts with different pmu counters
  40. */
  41. static union sbi_pmu_ctr_info *pmu_ctr_list;
  42. static unsigned int riscv_pmu_irq;
  43. struct sbi_pmu_event_data {
  44. union {
  45. union {
  46. struct hw_gen_event {
  47. uint32_t event_code:16;
  48. uint32_t event_type:4;
  49. uint32_t reserved:12;
  50. } hw_gen_event;
  51. struct hw_cache_event {
  52. uint32_t result_id:1;
  53. uint32_t op_id:2;
  54. uint32_t cache_id:13;
  55. uint32_t event_type:4;
  56. uint32_t reserved:12;
  57. } hw_cache_event;
  58. };
  59. uint32_t event_idx;
  60. };
  61. };
  62. static const struct sbi_pmu_event_data pmu_hw_event_map[] = {
  63. [PERF_COUNT_HW_CPU_CYCLES] = {.hw_gen_event = {
  64. SBI_PMU_HW_CPU_CYCLES,
  65. SBI_PMU_EVENT_TYPE_HW, 0}},
  66. [PERF_COUNT_HW_INSTRUCTIONS] = {.hw_gen_event = {
  67. SBI_PMU_HW_INSTRUCTIONS,
  68. SBI_PMU_EVENT_TYPE_HW, 0}},
  69. [PERF_COUNT_HW_CACHE_REFERENCES] = {.hw_gen_event = {
  70. SBI_PMU_HW_CACHE_REFERENCES,
  71. SBI_PMU_EVENT_TYPE_HW, 0}},
  72. [PERF_COUNT_HW_CACHE_MISSES] = {.hw_gen_event = {
  73. SBI_PMU_HW_CACHE_MISSES,
  74. SBI_PMU_EVENT_TYPE_HW, 0}},
  75. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {.hw_gen_event = {
  76. SBI_PMU_HW_BRANCH_INSTRUCTIONS,
  77. SBI_PMU_EVENT_TYPE_HW, 0}},
  78. [PERF_COUNT_HW_BRANCH_MISSES] = {.hw_gen_event = {
  79. SBI_PMU_HW_BRANCH_MISSES,
  80. SBI_PMU_EVENT_TYPE_HW, 0}},
  81. [PERF_COUNT_HW_BUS_CYCLES] = {.hw_gen_event = {
  82. SBI_PMU_HW_BUS_CYCLES,
  83. SBI_PMU_EVENT_TYPE_HW, 0}},
  84. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {.hw_gen_event = {
  85. SBI_PMU_HW_STALLED_CYCLES_FRONTEND,
  86. SBI_PMU_EVENT_TYPE_HW, 0}},
  87. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {.hw_gen_event = {
  88. SBI_PMU_HW_STALLED_CYCLES_BACKEND,
  89. SBI_PMU_EVENT_TYPE_HW, 0}},
  90. [PERF_COUNT_HW_REF_CPU_CYCLES] = {.hw_gen_event = {
  91. SBI_PMU_HW_REF_CPU_CYCLES,
  92. SBI_PMU_EVENT_TYPE_HW, 0}},
  93. };
  94. #define C(x) PERF_COUNT_HW_CACHE_##x
  95. static const struct sbi_pmu_event_data pmu_cache_event_map[PERF_COUNT_HW_CACHE_MAX]
  96. [PERF_COUNT_HW_CACHE_OP_MAX]
  97. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  98. [C(L1D)] = {
  99. [C(OP_READ)] = {
  100. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  101. C(OP_READ), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  102. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  103. C(OP_READ), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  104. },
  105. [C(OP_WRITE)] = {
  106. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  107. C(OP_WRITE), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  108. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  109. C(OP_WRITE), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  110. },
  111. [C(OP_PREFETCH)] = {
  112. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  113. C(OP_PREFETCH), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  114. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  115. C(OP_PREFETCH), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  116. },
  117. },
  118. [C(L1I)] = {
  119. [C(OP_READ)] = {
  120. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  121. C(OP_READ), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  122. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS), C(OP_READ),
  123. C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  124. },
  125. [C(OP_WRITE)] = {
  126. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  127. C(OP_WRITE), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  128. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  129. C(OP_WRITE), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  130. },
  131. [C(OP_PREFETCH)] = {
  132. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  133. C(OP_PREFETCH), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  134. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  135. C(OP_PREFETCH), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  136. },
  137. },
  138. [C(LL)] = {
  139. [C(OP_READ)] = {
  140. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  141. C(OP_READ), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  142. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  143. C(OP_READ), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  144. },
  145. [C(OP_WRITE)] = {
  146. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  147. C(OP_WRITE), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  148. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  149. C(OP_WRITE), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  150. },
  151. [C(OP_PREFETCH)] = {
  152. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  153. C(OP_PREFETCH), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  154. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  155. C(OP_PREFETCH), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  156. },
  157. },
  158. [C(DTLB)] = {
  159. [C(OP_READ)] = {
  160. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  161. C(OP_READ), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  162. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  163. C(OP_READ), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  164. },
  165. [C(OP_WRITE)] = {
  166. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  167. C(OP_WRITE), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  168. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  169. C(OP_WRITE), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  170. },
  171. [C(OP_PREFETCH)] = {
  172. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  173. C(OP_PREFETCH), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  174. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  175. C(OP_PREFETCH), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  176. },
  177. },
  178. [C(ITLB)] = {
  179. [C(OP_READ)] = {
  180. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  181. C(OP_READ), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  182. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  183. C(OP_READ), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  184. },
  185. [C(OP_WRITE)] = {
  186. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  187. C(OP_WRITE), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  188. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  189. C(OP_WRITE), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  190. },
  191. [C(OP_PREFETCH)] = {
  192. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  193. C(OP_PREFETCH), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  194. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  195. C(OP_PREFETCH), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  196. },
  197. },
  198. [C(BPU)] = {
  199. [C(OP_READ)] = {
  200. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  201. C(OP_READ), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  202. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  203. C(OP_READ), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  204. },
  205. [C(OP_WRITE)] = {
  206. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  207. C(OP_WRITE), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  208. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  209. C(OP_WRITE), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  210. },
  211. [C(OP_PREFETCH)] = {
  212. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  213. C(OP_PREFETCH), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  214. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  215. C(OP_PREFETCH), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  216. },
  217. },
  218. [C(NODE)] = {
  219. [C(OP_READ)] = {
  220. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  221. C(OP_READ), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  222. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  223. C(OP_READ), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  224. },
  225. [C(OP_WRITE)] = {
  226. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  227. C(OP_WRITE), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  228. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  229. C(OP_WRITE), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  230. },
  231. [C(OP_PREFETCH)] = {
  232. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  233. C(OP_PREFETCH), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  234. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  235. C(OP_PREFETCH), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  236. },
  237. },
  238. };
  239. static int pmu_sbi_ctr_get_width(int idx)
  240. {
  241. return pmu_ctr_list[idx].width;
  242. }
  243. static bool pmu_sbi_ctr_is_fw(int cidx)
  244. {
  245. union sbi_pmu_ctr_info *info;
  246. info = &pmu_ctr_list[cidx];
  247. if (!info)
  248. return false;
  249. return (info->type == SBI_PMU_CTR_TYPE_FW) ? true : false;
  250. }
  251. static int pmu_sbi_ctr_get_idx(struct perf_event *event)
  252. {
  253. struct hw_perf_event *hwc = &event->hw;
  254. struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
  255. struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
  256. struct sbiret ret;
  257. int idx;
  258. uint64_t cbase = 0;
  259. unsigned long cflags = 0;
  260. if (event->attr.exclude_kernel)
  261. cflags |= SBI_PMU_CFG_FLAG_SET_SINH;
  262. if (event->attr.exclude_user)
  263. cflags |= SBI_PMU_CFG_FLAG_SET_UINH;
  264. /* retrieve the available counter index */
  265. #if defined(CONFIG_32BIT)
  266. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase,
  267. rvpmu->cmask, cflags, hwc->event_base, hwc->config,
  268. hwc->config >> 32);
  269. #else
  270. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase,
  271. rvpmu->cmask, cflags, hwc->event_base, hwc->config, 0);
  272. #endif
  273. if (ret.error) {
  274. pr_debug("Not able to find a counter for event %lx config %llx\n",
  275. hwc->event_base, hwc->config);
  276. return sbi_err_map_linux_errno(ret.error);
  277. }
  278. idx = ret.value;
  279. if (!test_bit(idx, &rvpmu->cmask) || !pmu_ctr_list[idx].value)
  280. return -ENOENT;
  281. /* Additional sanity check for the counter id */
  282. if (pmu_sbi_ctr_is_fw(idx)) {
  283. if (!test_and_set_bit(idx, cpuc->used_fw_ctrs))
  284. return idx;
  285. } else {
  286. if (!test_and_set_bit(idx, cpuc->used_hw_ctrs))
  287. return idx;
  288. }
  289. return -ENOENT;
  290. }
  291. static void pmu_sbi_ctr_clear_idx(struct perf_event *event)
  292. {
  293. struct hw_perf_event *hwc = &event->hw;
  294. struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
  295. struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
  296. int idx = hwc->idx;
  297. if (pmu_sbi_ctr_is_fw(idx))
  298. clear_bit(idx, cpuc->used_fw_ctrs);
  299. else
  300. clear_bit(idx, cpuc->used_hw_ctrs);
  301. }
  302. static int pmu_event_find_cache(u64 config)
  303. {
  304. unsigned int cache_type, cache_op, cache_result, ret;
  305. cache_type = (config >> 0) & 0xff;
  306. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  307. return -EINVAL;
  308. cache_op = (config >> 8) & 0xff;
  309. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  310. return -EINVAL;
  311. cache_result = (config >> 16) & 0xff;
  312. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  313. return -EINVAL;
  314. ret = pmu_cache_event_map[cache_type][cache_op][cache_result].event_idx;
  315. return ret;
  316. }
  317. static bool pmu_sbi_is_fw_event(struct perf_event *event)
  318. {
  319. u32 type = event->attr.type;
  320. u64 config = event->attr.config;
  321. if ((type == PERF_TYPE_RAW) && ((config >> 63) == 1))
  322. return true;
  323. else
  324. return false;
  325. }
  326. static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
  327. {
  328. u32 type = event->attr.type;
  329. u64 config = event->attr.config;
  330. int bSoftware;
  331. u64 raw_config_val;
  332. int ret;
  333. switch (type) {
  334. case PERF_TYPE_HARDWARE:
  335. if (config >= PERF_COUNT_HW_MAX)
  336. return -EINVAL;
  337. ret = pmu_hw_event_map[event->attr.config].event_idx;
  338. break;
  339. case PERF_TYPE_HW_CACHE:
  340. ret = pmu_event_find_cache(config);
  341. break;
  342. case PERF_TYPE_RAW:
  343. /*
  344. * As per SBI specification, the upper 16 bits must be unused for
  345. * a raw event. Use the MSB (63b) to distinguish between hardware
  346. * raw event and firmware events.
  347. */
  348. bSoftware = config >> 63;
  349. raw_config_val = config & RISCV_PMU_RAW_EVENT_MASK;
  350. if (bSoftware) {
  351. if (raw_config_val < SBI_PMU_FW_MAX)
  352. ret = (raw_config_val & 0xFFFF) |
  353. (SBI_PMU_EVENT_TYPE_FW << 16);
  354. else
  355. return -EINVAL;
  356. } else {
  357. ret = RISCV_PMU_RAW_EVENT_IDX;
  358. *econfig = raw_config_val;
  359. }
  360. break;
  361. default:
  362. ret = -EINVAL;
  363. break;
  364. }
  365. return ret;
  366. }
  367. static u64 pmu_sbi_ctr_read(struct perf_event *event)
  368. {
  369. struct hw_perf_event *hwc = &event->hw;
  370. int idx = hwc->idx;
  371. struct sbiret ret;
  372. union sbi_pmu_ctr_info info;
  373. u64 val = 0;
  374. if (pmu_sbi_is_fw_event(event)) {
  375. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_FW_READ,
  376. hwc->idx, 0, 0, 0, 0, 0);
  377. if (!ret.error)
  378. val = ret.value;
  379. } else {
  380. info = pmu_ctr_list[idx];
  381. val = riscv_pmu_ctr_read_csr(info.csr);
  382. if (IS_ENABLED(CONFIG_32BIT))
  383. val = ((u64)riscv_pmu_ctr_read_csr(info.csr + 0x80)) << 31 | val;
  384. }
  385. return val;
  386. }
  387. static void pmu_sbi_ctr_start(struct perf_event *event, u64 ival)
  388. {
  389. struct sbiret ret;
  390. struct hw_perf_event *hwc = &event->hw;
  391. unsigned long flag = SBI_PMU_START_FLAG_SET_INIT_VALUE;
  392. #if defined(CONFIG_32BIT)
  393. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
  394. 1, flag, ival, ival >> 32, 0);
  395. #else
  396. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
  397. 1, flag, ival, 0, 0);
  398. #endif
  399. if (ret.error && (ret.error != SBI_ERR_ALREADY_STARTED))
  400. pr_err("Starting counter idx %d failed with error %d\n",
  401. hwc->idx, sbi_err_map_linux_errno(ret.error));
  402. }
  403. static void pmu_sbi_ctr_stop(struct perf_event *event, unsigned long flag)
  404. {
  405. struct sbiret ret;
  406. struct hw_perf_event *hwc = &event->hw;
  407. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, hwc->idx, 1, flag, 0, 0, 0);
  408. if (ret.error && (ret.error != SBI_ERR_ALREADY_STOPPED) &&
  409. flag != SBI_PMU_STOP_FLAG_RESET)
  410. pr_err("Stopping counter idx %d failed with error %d\n",
  411. hwc->idx, sbi_err_map_linux_errno(ret.error));
  412. }
  413. static int pmu_sbi_find_num_ctrs(void)
  414. {
  415. struct sbiret ret;
  416. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_NUM_COUNTERS, 0, 0, 0, 0, 0, 0);
  417. if (!ret.error)
  418. return ret.value;
  419. else
  420. return sbi_err_map_linux_errno(ret.error);
  421. }
  422. static int pmu_sbi_get_ctrinfo(int nctr, unsigned long *mask)
  423. {
  424. struct sbiret ret;
  425. int i, num_hw_ctr = 0, num_fw_ctr = 0;
  426. union sbi_pmu_ctr_info cinfo;
  427. pmu_ctr_list = kcalloc(nctr, sizeof(*pmu_ctr_list), GFP_KERNEL);
  428. if (!pmu_ctr_list)
  429. return -ENOMEM;
  430. for (i = 0; i < nctr; i++) {
  431. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_GET_INFO, i, 0, 0, 0, 0, 0);
  432. if (ret.error)
  433. /* The logical counter ids are not expected to be contiguous */
  434. continue;
  435. *mask |= BIT(i);
  436. cinfo.value = ret.value;
  437. if (cinfo.type == SBI_PMU_CTR_TYPE_FW)
  438. num_fw_ctr++;
  439. else
  440. num_hw_ctr++;
  441. pmu_ctr_list[i].value = cinfo.value;
  442. }
  443. pr_info("%d firmware and %d hardware counters\n", num_fw_ctr, num_hw_ctr);
  444. return 0;
  445. }
  446. static inline void pmu_sbi_stop_all(struct riscv_pmu *pmu)
  447. {
  448. /*
  449. * No need to check the error because we are disabling all the counters
  450. * which may include counters that are not enabled yet.
  451. */
  452. sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP,
  453. 0, pmu->cmask, 0, 0, 0, 0);
  454. }
  455. static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu)
  456. {
  457. struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
  458. /* No need to check the error here as we can't do anything about the error */
  459. sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, 0,
  460. cpu_hw_evt->used_hw_ctrs[0], 0, 0, 0, 0);
  461. }
  462. /*
  463. * This function starts all the used counters in two step approach.
  464. * Any counter that did not overflow can be start in a single step
  465. * while the overflowed counters need to be started with updated initialization
  466. * value.
  467. */
  468. static inline void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
  469. unsigned long ctr_ovf_mask)
  470. {
  471. int idx = 0;
  472. struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
  473. struct perf_event *event;
  474. unsigned long flag = SBI_PMU_START_FLAG_SET_INIT_VALUE;
  475. unsigned long ctr_start_mask = 0;
  476. uint64_t max_period;
  477. struct hw_perf_event *hwc;
  478. u64 init_val = 0;
  479. ctr_start_mask = cpu_hw_evt->used_hw_ctrs[0] & ~ctr_ovf_mask;
  480. /* Start all the counters that did not overflow in a single shot */
  481. sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, 0, ctr_start_mask,
  482. 0, 0, 0, 0);
  483. /* Reinitialize and start all the counter that overflowed */
  484. while (ctr_ovf_mask) {
  485. if (ctr_ovf_mask & 0x01) {
  486. event = cpu_hw_evt->events[idx];
  487. hwc = &event->hw;
  488. max_period = riscv_pmu_ctr_get_width_mask(event);
  489. init_val = local64_read(&hwc->prev_count) & max_period;
  490. #if defined(CONFIG_32BIT)
  491. sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
  492. flag, init_val, init_val >> 32, 0);
  493. #else
  494. sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
  495. flag, init_val, 0, 0);
  496. #endif
  497. perf_event_update_userpage(event);
  498. }
  499. ctr_ovf_mask = ctr_ovf_mask >> 1;
  500. idx++;
  501. }
  502. }
  503. static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
  504. {
  505. struct perf_sample_data data;
  506. struct pt_regs *regs;
  507. struct hw_perf_event *hw_evt;
  508. union sbi_pmu_ctr_info *info;
  509. int lidx, hidx, fidx;
  510. struct riscv_pmu *pmu;
  511. struct perf_event *event;
  512. unsigned long overflow;
  513. unsigned long overflowed_ctrs = 0;
  514. struct cpu_hw_events *cpu_hw_evt = dev;
  515. u64 start_clock = sched_clock();
  516. if (WARN_ON_ONCE(!cpu_hw_evt))
  517. return IRQ_NONE;
  518. /* Firmware counter don't support overflow yet */
  519. fidx = find_first_bit(cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS);
  520. event = cpu_hw_evt->events[fidx];
  521. if (!event) {
  522. csr_clear(CSR_SIP, SIP_LCOFIP);
  523. return IRQ_NONE;
  524. }
  525. pmu = to_riscv_pmu(event->pmu);
  526. pmu_sbi_stop_hw_ctrs(pmu);
  527. /* Overflow status register should only be read after counter are stopped */
  528. overflow = csr_read(CSR_SSCOUNTOVF);
  529. /*
  530. * Overflow interrupt pending bit should only be cleared after stopping
  531. * all the counters to avoid any race condition.
  532. */
  533. csr_clear(CSR_SIP, SIP_LCOFIP);
  534. /* No overflow bit is set */
  535. if (!overflow)
  536. return IRQ_NONE;
  537. regs = get_irq_regs();
  538. for_each_set_bit(lidx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS) {
  539. struct perf_event *event = cpu_hw_evt->events[lidx];
  540. /* Skip if invalid event or user did not request a sampling */
  541. if (!event || !is_sampling_event(event))
  542. continue;
  543. info = &pmu_ctr_list[lidx];
  544. /* Do a sanity check */
  545. if (!info || info->type != SBI_PMU_CTR_TYPE_HW)
  546. continue;
  547. /* compute hardware counter index */
  548. hidx = info->csr - CSR_CYCLE;
  549. /* check if the corresponding bit is set in sscountovf */
  550. if (!(overflow & (1 << hidx)))
  551. continue;
  552. /*
  553. * Keep a track of overflowed counters so that they can be started
  554. * with updated initial value.
  555. */
  556. overflowed_ctrs |= 1 << lidx;
  557. hw_evt = &event->hw;
  558. riscv_pmu_event_update(event);
  559. perf_sample_data_init(&data, 0, hw_evt->last_period);
  560. if (riscv_pmu_event_set_period(event)) {
  561. /*
  562. * Unlike other ISAs, RISC-V don't have to disable interrupts
  563. * to avoid throttling here. As per the specification, the
  564. * interrupt remains disabled until the OF bit is set.
  565. * Interrupts are enabled again only during the start.
  566. * TODO: We will need to stop the guest counters once
  567. * virtualization support is added.
  568. */
  569. perf_event_overflow(event, &data, regs);
  570. }
  571. }
  572. pmu_sbi_start_overflow_mask(pmu, overflowed_ctrs);
  573. perf_sample_event_took(sched_clock() - start_clock);
  574. return IRQ_HANDLED;
  575. }
  576. static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
  577. {
  578. struct riscv_pmu *pmu = hlist_entry_safe(node, struct riscv_pmu, node);
  579. struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
  580. /*
  581. * Enable the access for CYCLE, TIME, and INSTRET CSRs from userspace,
  582. * as is necessary to maintain uABI compatibility.
  583. */
  584. csr_write(CSR_SCOUNTEREN, 0x7);
  585. /* Stop all the counters so that they can be enabled from perf */
  586. pmu_sbi_stop_all(pmu);
  587. if (riscv_isa_extension_available(NULL, SSCOFPMF)) {
  588. cpu_hw_evt->irq = riscv_pmu_irq;
  589. csr_clear(CSR_IP, BIT(RV_IRQ_PMU));
  590. csr_set(CSR_IE, BIT(RV_IRQ_PMU));
  591. enable_percpu_irq(riscv_pmu_irq, IRQ_TYPE_NONE);
  592. }
  593. return 0;
  594. }
  595. static int pmu_sbi_dying_cpu(unsigned int cpu, struct hlist_node *node)
  596. {
  597. if (riscv_isa_extension_available(NULL, SSCOFPMF)) {
  598. disable_percpu_irq(riscv_pmu_irq);
  599. csr_clear(CSR_IE, BIT(RV_IRQ_PMU));
  600. }
  601. /* Disable all counters access for user mode now */
  602. csr_write(CSR_SCOUNTEREN, 0x0);
  603. return 0;
  604. }
  605. static int pmu_sbi_setup_irqs(struct riscv_pmu *pmu, struct platform_device *pdev)
  606. {
  607. int ret;
  608. struct cpu_hw_events __percpu *hw_events = pmu->hw_events;
  609. struct device_node *cpu, *child;
  610. struct irq_domain *domain = NULL;
  611. if (!riscv_isa_extension_available(NULL, SSCOFPMF))
  612. return -EOPNOTSUPP;
  613. for_each_of_cpu_node(cpu) {
  614. child = of_get_compatible_child(cpu, "riscv,cpu-intc");
  615. if (!child) {
  616. pr_err("Failed to find INTC node\n");
  617. of_node_put(cpu);
  618. return -ENODEV;
  619. }
  620. domain = irq_find_host(child);
  621. of_node_put(child);
  622. if (domain) {
  623. of_node_put(cpu);
  624. break;
  625. }
  626. }
  627. if (!domain) {
  628. pr_err("Failed to find INTC IRQ root domain\n");
  629. return -ENODEV;
  630. }
  631. riscv_pmu_irq = irq_create_mapping(domain, RV_IRQ_PMU);
  632. if (!riscv_pmu_irq) {
  633. pr_err("Failed to map PMU interrupt for node\n");
  634. return -ENODEV;
  635. }
  636. ret = request_percpu_irq(riscv_pmu_irq, pmu_sbi_ovf_handler, "riscv-pmu", hw_events);
  637. if (ret) {
  638. pr_err("registering percpu irq failed [%d]\n", ret);
  639. return ret;
  640. }
  641. return 0;
  642. }
  643. #ifdef CONFIG_CPU_PM
  644. static int riscv_pm_pmu_notify(struct notifier_block *b, unsigned long cmd,
  645. void *v)
  646. {
  647. struct riscv_pmu *rvpmu = container_of(b, struct riscv_pmu, riscv_pm_nb);
  648. struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
  649. int enabled = bitmap_weight(cpuc->used_hw_ctrs, RISCV_MAX_COUNTERS);
  650. struct perf_event *event;
  651. int idx;
  652. if (!enabled)
  653. return NOTIFY_OK;
  654. for (idx = 0; idx < RISCV_MAX_COUNTERS; idx++) {
  655. event = cpuc->events[idx];
  656. if (!event)
  657. continue;
  658. switch (cmd) {
  659. case CPU_PM_ENTER:
  660. /*
  661. * Stop and update the counter
  662. */
  663. riscv_pmu_stop(event, PERF_EF_UPDATE);
  664. break;
  665. case CPU_PM_EXIT:
  666. case CPU_PM_ENTER_FAILED:
  667. /*
  668. * Restore and enable the counter.
  669. *
  670. * Requires RCU read locking to be functional,
  671. * wrap the call within RCU_NONIDLE to make the
  672. * RCU subsystem aware this cpu is not idle from
  673. * an RCU perspective for the riscv_pmu_start() call
  674. * duration.
  675. */
  676. RCU_NONIDLE(riscv_pmu_start(event, PERF_EF_RELOAD));
  677. break;
  678. default:
  679. break;
  680. }
  681. }
  682. return NOTIFY_OK;
  683. }
  684. static int riscv_pm_pmu_register(struct riscv_pmu *pmu)
  685. {
  686. pmu->riscv_pm_nb.notifier_call = riscv_pm_pmu_notify;
  687. return cpu_pm_register_notifier(&pmu->riscv_pm_nb);
  688. }
  689. static void riscv_pm_pmu_unregister(struct riscv_pmu *pmu)
  690. {
  691. cpu_pm_unregister_notifier(&pmu->riscv_pm_nb);
  692. }
  693. #else
  694. static inline int riscv_pm_pmu_register(struct riscv_pmu *pmu) { return 0; }
  695. static inline void riscv_pm_pmu_unregister(struct riscv_pmu *pmu) { }
  696. #endif
  697. static void riscv_pmu_destroy(struct riscv_pmu *pmu)
  698. {
  699. riscv_pm_pmu_unregister(pmu);
  700. cpuhp_state_remove_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
  701. }
  702. static int pmu_sbi_device_probe(struct platform_device *pdev)
  703. {
  704. struct riscv_pmu *pmu = NULL;
  705. unsigned long cmask = 0;
  706. int ret = -ENODEV;
  707. int num_counters;
  708. pr_info("SBI PMU extension is available\n");
  709. pmu = riscv_pmu_alloc();
  710. if (!pmu)
  711. return -ENOMEM;
  712. num_counters = pmu_sbi_find_num_ctrs();
  713. if (num_counters < 0) {
  714. pr_err("SBI PMU extension doesn't provide any counters\n");
  715. goto out_free;
  716. }
  717. /* cache all the information about counters now */
  718. if (pmu_sbi_get_ctrinfo(num_counters, &cmask))
  719. goto out_free;
  720. ret = pmu_sbi_setup_irqs(pmu, pdev);
  721. if (ret < 0) {
  722. pr_info("Perf sampling/filtering is not supported as sscof extension is not available\n");
  723. pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  724. pmu->pmu.capabilities |= PERF_PMU_CAP_NO_EXCLUDE;
  725. }
  726. pmu->pmu.attr_groups = riscv_pmu_attr_groups;
  727. pmu->cmask = cmask;
  728. pmu->ctr_start = pmu_sbi_ctr_start;
  729. pmu->ctr_stop = pmu_sbi_ctr_stop;
  730. pmu->event_map = pmu_sbi_event_map;
  731. pmu->ctr_get_idx = pmu_sbi_ctr_get_idx;
  732. pmu->ctr_get_width = pmu_sbi_ctr_get_width;
  733. pmu->ctr_clear_idx = pmu_sbi_ctr_clear_idx;
  734. pmu->ctr_read = pmu_sbi_ctr_read;
  735. ret = cpuhp_state_add_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
  736. if (ret)
  737. return ret;
  738. ret = riscv_pm_pmu_register(pmu);
  739. if (ret)
  740. goto out_unregister;
  741. ret = perf_pmu_register(&pmu->pmu, "cpu", PERF_TYPE_RAW);
  742. if (ret)
  743. goto out_unregister;
  744. return 0;
  745. out_unregister:
  746. riscv_pmu_destroy(pmu);
  747. out_free:
  748. kfree(pmu);
  749. return ret;
  750. }
  751. static struct platform_driver pmu_sbi_driver = {
  752. .probe = pmu_sbi_device_probe,
  753. .driver = {
  754. .name = RISCV_PMU_PDEV_NAME,
  755. },
  756. };
  757. static int __init pmu_sbi_devinit(void)
  758. {
  759. int ret;
  760. struct platform_device *pdev;
  761. if (sbi_spec_version < sbi_mk_version(0, 3) ||
  762. !sbi_probe_extension(SBI_EXT_PMU)) {
  763. return 0;
  764. }
  765. ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_RISCV_STARTING,
  766. "perf/riscv/pmu:starting",
  767. pmu_sbi_starting_cpu, pmu_sbi_dying_cpu);
  768. if (ret) {
  769. pr_err("CPU hotplug notifier could not be registered: %d\n",
  770. ret);
  771. return ret;
  772. }
  773. ret = platform_driver_register(&pmu_sbi_driver);
  774. if (ret)
  775. return ret;
  776. pdev = platform_device_register_simple(RISCV_PMU_PDEV_NAME, -1, NULL, 0);
  777. if (IS_ERR(pdev)) {
  778. platform_driver_unregister(&pmu_sbi_driver);
  779. return PTR_ERR(pdev);
  780. }
  781. /* Notify legacy implementation that SBI pmu is available*/
  782. riscv_pmu_legacy_skip_init();
  783. return ret;
  784. }
  785. device_initcall(pmu_sbi_devinit)