riscv_pmu_sbi.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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 <linux/soc/andes/irq.h>
  21. #include <linux/workqueue.h>
  22. #include <asm/errata_list.h>
  23. #include <asm/sbi.h>
  24. #include <asm/cpufeature.h>
  25. #include <asm/vendor_extensions.h>
  26. #include <asm/vendor_extensions/andes.h>
  27. #define ALT_SBI_PMU_OVERFLOW(__ovl) \
  28. asm volatile(ALTERNATIVE_2( \
  29. "csrr %0, " __stringify(CSR_SCOUNTOVF), \
  30. "csrr %0, " __stringify(THEAD_C9XX_CSR_SCOUNTEROF), \
  31. THEAD_VENDOR_ID, ERRATA_THEAD_PMU, \
  32. CONFIG_ERRATA_THEAD_PMU, \
  33. "csrr %0, " __stringify(ANDES_CSR_SCOUNTEROF), \
  34. ANDES_VENDOR_ID, \
  35. RISCV_ISA_VENDOR_EXT_XANDESPMU + RISCV_VENDOR_EXT_ALTERNATIVES_BASE, \
  36. CONFIG_ANDES_CUSTOM_PMU) \
  37. : "=r" (__ovl) : \
  38. : "memory")
  39. #define ALT_SBI_PMU_OVF_CLEAR_PENDING(__irq_mask) \
  40. asm volatile(ALTERNATIVE( \
  41. "csrc " __stringify(CSR_IP) ", %0\n\t", \
  42. "csrc " __stringify(ANDES_CSR_SLIP) ", %0\n\t", \
  43. ANDES_VENDOR_ID, \
  44. RISCV_ISA_VENDOR_EXT_XANDESPMU + RISCV_VENDOR_EXT_ALTERNATIVES_BASE, \
  45. CONFIG_ANDES_CUSTOM_PMU) \
  46. : : "r"(__irq_mask) \
  47. : "memory")
  48. #define SYSCTL_NO_USER_ACCESS 0
  49. #define SYSCTL_USER_ACCESS 1
  50. #define SYSCTL_LEGACY 2
  51. #define PERF_EVENT_FLAG_NO_USER_ACCESS BIT(SYSCTL_NO_USER_ACCESS)
  52. #define PERF_EVENT_FLAG_USER_ACCESS BIT(SYSCTL_USER_ACCESS)
  53. #define PERF_EVENT_FLAG_LEGACY BIT(SYSCTL_LEGACY)
  54. PMU_FORMAT_ATTR(event, "config:0-55");
  55. PMU_FORMAT_ATTR(firmware, "config:62-63");
  56. static bool sbi_v2_available;
  57. static bool sbi_v3_available;
  58. static DEFINE_STATIC_KEY_FALSE(sbi_pmu_snapshot_available);
  59. #define sbi_pmu_snapshot_available() \
  60. static_branch_unlikely(&sbi_pmu_snapshot_available)
  61. static struct attribute *riscv_arch_formats_attr[] = {
  62. &format_attr_event.attr,
  63. &format_attr_firmware.attr,
  64. NULL,
  65. };
  66. static struct attribute_group riscv_pmu_format_group = {
  67. .name = "format",
  68. .attrs = riscv_arch_formats_attr,
  69. };
  70. static const struct attribute_group *riscv_pmu_attr_groups[] = {
  71. &riscv_pmu_format_group,
  72. NULL,
  73. };
  74. /* Allow user mode access by default */
  75. static int sysctl_perf_user_access __read_mostly = SYSCTL_USER_ACCESS;
  76. /*
  77. * RISC-V doesn't have heterogeneous harts yet. This need to be part of
  78. * per_cpu in case of harts with different pmu counters
  79. */
  80. static union sbi_pmu_ctr_info *pmu_ctr_list;
  81. static bool riscv_pmu_use_irq;
  82. static unsigned int riscv_pmu_irq_num;
  83. static unsigned int riscv_pmu_irq_mask;
  84. static unsigned int riscv_pmu_irq;
  85. /* Cache the available counters in a bitmask */
  86. static unsigned long cmask;
  87. static int pmu_event_find_cache(u64 config);
  88. struct sbi_pmu_event_data {
  89. union {
  90. union {
  91. struct hw_gen_event {
  92. uint32_t event_code:16;
  93. uint32_t event_type:4;
  94. uint32_t reserved:12;
  95. } hw_gen_event;
  96. struct hw_cache_event {
  97. uint32_t result_id:1;
  98. uint32_t op_id:2;
  99. uint32_t cache_id:13;
  100. uint32_t event_type:4;
  101. uint32_t reserved:12;
  102. } hw_cache_event;
  103. };
  104. uint32_t event_idx;
  105. };
  106. };
  107. static struct sbi_pmu_event_data pmu_hw_event_map[] = {
  108. [PERF_COUNT_HW_CPU_CYCLES] = {.hw_gen_event = {
  109. SBI_PMU_HW_CPU_CYCLES,
  110. SBI_PMU_EVENT_TYPE_HW, 0}},
  111. [PERF_COUNT_HW_INSTRUCTIONS] = {.hw_gen_event = {
  112. SBI_PMU_HW_INSTRUCTIONS,
  113. SBI_PMU_EVENT_TYPE_HW, 0}},
  114. [PERF_COUNT_HW_CACHE_REFERENCES] = {.hw_gen_event = {
  115. SBI_PMU_HW_CACHE_REFERENCES,
  116. SBI_PMU_EVENT_TYPE_HW, 0}},
  117. [PERF_COUNT_HW_CACHE_MISSES] = {.hw_gen_event = {
  118. SBI_PMU_HW_CACHE_MISSES,
  119. SBI_PMU_EVENT_TYPE_HW, 0}},
  120. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {.hw_gen_event = {
  121. SBI_PMU_HW_BRANCH_INSTRUCTIONS,
  122. SBI_PMU_EVENT_TYPE_HW, 0}},
  123. [PERF_COUNT_HW_BRANCH_MISSES] = {.hw_gen_event = {
  124. SBI_PMU_HW_BRANCH_MISSES,
  125. SBI_PMU_EVENT_TYPE_HW, 0}},
  126. [PERF_COUNT_HW_BUS_CYCLES] = {.hw_gen_event = {
  127. SBI_PMU_HW_BUS_CYCLES,
  128. SBI_PMU_EVENT_TYPE_HW, 0}},
  129. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {.hw_gen_event = {
  130. SBI_PMU_HW_STALLED_CYCLES_FRONTEND,
  131. SBI_PMU_EVENT_TYPE_HW, 0}},
  132. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {.hw_gen_event = {
  133. SBI_PMU_HW_STALLED_CYCLES_BACKEND,
  134. SBI_PMU_EVENT_TYPE_HW, 0}},
  135. [PERF_COUNT_HW_REF_CPU_CYCLES] = {.hw_gen_event = {
  136. SBI_PMU_HW_REF_CPU_CYCLES,
  137. SBI_PMU_EVENT_TYPE_HW, 0}},
  138. };
  139. #define C(x) PERF_COUNT_HW_CACHE_##x
  140. static struct sbi_pmu_event_data pmu_cache_event_map[PERF_COUNT_HW_CACHE_MAX]
  141. [PERF_COUNT_HW_CACHE_OP_MAX]
  142. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  143. [C(L1D)] = {
  144. [C(OP_READ)] = {
  145. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  146. C(OP_READ), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  147. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  148. C(OP_READ), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  149. },
  150. [C(OP_WRITE)] = {
  151. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  152. C(OP_WRITE), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  153. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  154. C(OP_WRITE), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  155. },
  156. [C(OP_PREFETCH)] = {
  157. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  158. C(OP_PREFETCH), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  159. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  160. C(OP_PREFETCH), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  161. },
  162. },
  163. [C(L1I)] = {
  164. [C(OP_READ)] = {
  165. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  166. C(OP_READ), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  167. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS), C(OP_READ),
  168. C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  169. },
  170. [C(OP_WRITE)] = {
  171. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  172. C(OP_WRITE), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  173. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  174. C(OP_WRITE), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  175. },
  176. [C(OP_PREFETCH)] = {
  177. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  178. C(OP_PREFETCH), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  179. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  180. C(OP_PREFETCH), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  181. },
  182. },
  183. [C(LL)] = {
  184. [C(OP_READ)] = {
  185. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  186. C(OP_READ), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  187. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  188. C(OP_READ), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  189. },
  190. [C(OP_WRITE)] = {
  191. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  192. C(OP_WRITE), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  193. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  194. C(OP_WRITE), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  195. },
  196. [C(OP_PREFETCH)] = {
  197. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  198. C(OP_PREFETCH), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  199. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  200. C(OP_PREFETCH), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  201. },
  202. },
  203. [C(DTLB)] = {
  204. [C(OP_READ)] = {
  205. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  206. C(OP_READ), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  207. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  208. C(OP_READ), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  209. },
  210. [C(OP_WRITE)] = {
  211. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  212. C(OP_WRITE), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  213. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  214. C(OP_WRITE), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  215. },
  216. [C(OP_PREFETCH)] = {
  217. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  218. C(OP_PREFETCH), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  219. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  220. C(OP_PREFETCH), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  221. },
  222. },
  223. [C(ITLB)] = {
  224. [C(OP_READ)] = {
  225. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  226. C(OP_READ), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  227. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  228. C(OP_READ), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  229. },
  230. [C(OP_WRITE)] = {
  231. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  232. C(OP_WRITE), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  233. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  234. C(OP_WRITE), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  235. },
  236. [C(OP_PREFETCH)] = {
  237. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  238. C(OP_PREFETCH), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  239. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  240. C(OP_PREFETCH), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  241. },
  242. },
  243. [C(BPU)] = {
  244. [C(OP_READ)] = {
  245. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  246. C(OP_READ), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  247. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  248. C(OP_READ), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  249. },
  250. [C(OP_WRITE)] = {
  251. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  252. C(OP_WRITE), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  253. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  254. C(OP_WRITE), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  255. },
  256. [C(OP_PREFETCH)] = {
  257. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  258. C(OP_PREFETCH), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  259. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  260. C(OP_PREFETCH), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  261. },
  262. },
  263. [C(NODE)] = {
  264. [C(OP_READ)] = {
  265. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  266. C(OP_READ), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  267. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  268. C(OP_READ), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  269. },
  270. [C(OP_WRITE)] = {
  271. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  272. C(OP_WRITE), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  273. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  274. C(OP_WRITE), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  275. },
  276. [C(OP_PREFETCH)] = {
  277. [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
  278. C(OP_PREFETCH), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  279. [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
  280. C(OP_PREFETCH), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
  281. },
  282. },
  283. };
  284. static int pmu_sbi_check_event_info(void)
  285. {
  286. int num_events = ARRAY_SIZE(pmu_hw_event_map) + PERF_COUNT_HW_CACHE_MAX *
  287. PERF_COUNT_HW_CACHE_OP_MAX * PERF_COUNT_HW_CACHE_RESULT_MAX;
  288. struct riscv_pmu_event_info *event_info_shmem;
  289. phys_addr_t base_addr;
  290. int i, j, k, result = 0, count = 0;
  291. struct sbiret ret;
  292. event_info_shmem = kzalloc_objs(*event_info_shmem, num_events);
  293. if (!event_info_shmem)
  294. return -ENOMEM;
  295. for (i = 0; i < ARRAY_SIZE(pmu_hw_event_map); i++)
  296. event_info_shmem[count++].event_idx = pmu_hw_event_map[i].event_idx;
  297. for (i = 0; i < ARRAY_SIZE(pmu_cache_event_map); i++) {
  298. for (j = 0; j < ARRAY_SIZE(pmu_cache_event_map[i]); j++) {
  299. for (k = 0; k < ARRAY_SIZE(pmu_cache_event_map[i][j]); k++)
  300. event_info_shmem[count++].event_idx =
  301. pmu_cache_event_map[i][j][k].event_idx;
  302. }
  303. }
  304. base_addr = __pa(event_info_shmem);
  305. if (IS_ENABLED(CONFIG_32BIT))
  306. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_EVENT_GET_INFO, lower_32_bits(base_addr),
  307. upper_32_bits(base_addr), count, 0, 0, 0);
  308. else
  309. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_EVENT_GET_INFO, base_addr, 0,
  310. count, 0, 0, 0);
  311. if (ret.error) {
  312. result = -EOPNOTSUPP;
  313. goto free_mem;
  314. }
  315. for (i = 0; i < ARRAY_SIZE(pmu_hw_event_map); i++) {
  316. if (!(event_info_shmem[i].output & RISCV_PMU_EVENT_INFO_OUTPUT_MASK))
  317. pmu_hw_event_map[i].event_idx = -ENOENT;
  318. }
  319. count = ARRAY_SIZE(pmu_hw_event_map);
  320. for (i = 0; i < ARRAY_SIZE(pmu_cache_event_map); i++) {
  321. for (j = 0; j < ARRAY_SIZE(pmu_cache_event_map[i]); j++) {
  322. for (k = 0; k < ARRAY_SIZE(pmu_cache_event_map[i][j]); k++) {
  323. if (!(event_info_shmem[count].output &
  324. RISCV_PMU_EVENT_INFO_OUTPUT_MASK))
  325. pmu_cache_event_map[i][j][k].event_idx = -ENOENT;
  326. count++;
  327. }
  328. }
  329. }
  330. free_mem:
  331. kfree(event_info_shmem);
  332. return result;
  333. }
  334. static void pmu_sbi_check_event(struct sbi_pmu_event_data *edata)
  335. {
  336. struct sbiret ret;
  337. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH,
  338. 0, cmask, 0, edata->event_idx, 0, 0);
  339. if (!ret.error) {
  340. sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP,
  341. ret.value, 0x1, SBI_PMU_STOP_FLAG_RESET, 0, 0, 0);
  342. } else if (ret.error == SBI_ERR_NOT_SUPPORTED) {
  343. /* This event cannot be monitored by any counter */
  344. edata->event_idx = -ENOENT;
  345. }
  346. }
  347. static void pmu_sbi_check_std_events(struct work_struct *work)
  348. {
  349. int ret;
  350. if (sbi_v3_available) {
  351. ret = pmu_sbi_check_event_info();
  352. if (ret)
  353. pr_err("pmu_sbi_check_event_info failed with error %d\n", ret);
  354. return;
  355. }
  356. for (int i = 0; i < ARRAY_SIZE(pmu_hw_event_map); i++)
  357. pmu_sbi_check_event(&pmu_hw_event_map[i]);
  358. for (int i = 0; i < ARRAY_SIZE(pmu_cache_event_map); i++)
  359. for (int j = 0; j < ARRAY_SIZE(pmu_cache_event_map[i]); j++)
  360. for (int k = 0; k < ARRAY_SIZE(pmu_cache_event_map[i][j]); k++)
  361. pmu_sbi_check_event(&pmu_cache_event_map[i][j][k]);
  362. }
  363. static DECLARE_WORK(check_std_events_work, pmu_sbi_check_std_events);
  364. static int pmu_sbi_ctr_get_width(int idx)
  365. {
  366. return pmu_ctr_list[idx].width;
  367. }
  368. static bool pmu_sbi_ctr_is_fw(int cidx)
  369. {
  370. union sbi_pmu_ctr_info *info;
  371. info = &pmu_ctr_list[cidx];
  372. if (!info)
  373. return false;
  374. return info->type == SBI_PMU_CTR_TYPE_FW;
  375. }
  376. int riscv_pmu_get_event_info(u32 type, u64 config, u64 *econfig)
  377. {
  378. int ret = -ENOENT;
  379. switch (type) {
  380. case PERF_TYPE_HARDWARE:
  381. if (config >= PERF_COUNT_HW_MAX)
  382. return -EINVAL;
  383. ret = pmu_hw_event_map[config].event_idx;
  384. break;
  385. case PERF_TYPE_HW_CACHE:
  386. ret = pmu_event_find_cache(config);
  387. break;
  388. case PERF_TYPE_RAW:
  389. /*
  390. * As per SBI v0.3 specification,
  391. * -- the upper 16 bits must be unused for a hardware raw event.
  392. * As per SBI v2.0 specification,
  393. * -- the upper 8 bits must be unused for a hardware raw event.
  394. * Bits 63:62 are used to distinguish between raw events
  395. * 00 - Hardware raw event
  396. * 10 - SBI firmware events
  397. * 11 - Risc-V platform specific firmware event
  398. */
  399. switch (config >> 62) {
  400. case 0:
  401. if (sbi_v3_available) {
  402. /* Return error any bits [56-63] is set as it is not allowed by the spec */
  403. if (!(config & ~RISCV_PMU_RAW_EVENT_V2_MASK)) {
  404. if (econfig)
  405. *econfig = config & RISCV_PMU_RAW_EVENT_V2_MASK;
  406. ret = RISCV_PMU_RAW_EVENT_V2_IDX;
  407. }
  408. /* Return error any bits [48-63] is set as it is not allowed by the spec */
  409. } else if (!(config & ~RISCV_PMU_RAW_EVENT_MASK)) {
  410. if (econfig)
  411. *econfig = config & RISCV_PMU_RAW_EVENT_MASK;
  412. ret = RISCV_PMU_RAW_EVENT_IDX;
  413. }
  414. break;
  415. case 2:
  416. ret = (config & 0xFFFF) | (SBI_PMU_EVENT_TYPE_FW << 16);
  417. break;
  418. case 3:
  419. /*
  420. * For Risc-V platform specific firmware events
  421. * Event code - 0xFFFF
  422. * Event data - raw event encoding
  423. */
  424. ret = SBI_PMU_EVENT_TYPE_FW << 16 | RISCV_PLAT_FW_EVENT;
  425. if (econfig)
  426. *econfig = config & RISCV_PMU_PLAT_FW_EVENT_MASK;
  427. break;
  428. default:
  429. break;
  430. }
  431. break;
  432. default:
  433. break;
  434. }
  435. return ret;
  436. }
  437. EXPORT_SYMBOL_GPL(riscv_pmu_get_event_info);
  438. /*
  439. * Returns the counter width of a programmable counter and number of hardware
  440. * counters. As we don't support heterogeneous CPUs yet, it is okay to just
  441. * return the counter width of the first programmable counter.
  442. */
  443. int riscv_pmu_get_hpm_info(u32 *hw_ctr_width, u32 *num_hw_ctr)
  444. {
  445. int i;
  446. union sbi_pmu_ctr_info *info;
  447. u32 hpm_width = 0, hpm_count = 0;
  448. if (!cmask)
  449. return -EINVAL;
  450. for_each_set_bit(i, &cmask, RISCV_MAX_COUNTERS) {
  451. info = &pmu_ctr_list[i];
  452. if (!info)
  453. continue;
  454. if (!hpm_width && info->csr != CSR_CYCLE && info->csr != CSR_INSTRET)
  455. hpm_width = info->width;
  456. if (info->type == SBI_PMU_CTR_TYPE_HW)
  457. hpm_count++;
  458. }
  459. *hw_ctr_width = hpm_width;
  460. *num_hw_ctr = hpm_count;
  461. return 0;
  462. }
  463. EXPORT_SYMBOL_GPL(riscv_pmu_get_hpm_info);
  464. static uint8_t pmu_sbi_csr_index(struct perf_event *event)
  465. {
  466. return pmu_ctr_list[event->hw.idx].csr - CSR_CYCLE;
  467. }
  468. static unsigned long pmu_sbi_get_filter_flags(struct perf_event *event)
  469. {
  470. unsigned long cflags = 0;
  471. bool guest_events = false;
  472. if (event->attr.config1 & RISCV_PMU_CONFIG1_GUEST_EVENTS)
  473. guest_events = true;
  474. if (event->attr.exclude_kernel)
  475. cflags |= guest_events ? SBI_PMU_CFG_FLAG_SET_VSINH : SBI_PMU_CFG_FLAG_SET_SINH;
  476. if (event->attr.exclude_user)
  477. cflags |= guest_events ? SBI_PMU_CFG_FLAG_SET_VUINH : SBI_PMU_CFG_FLAG_SET_UINH;
  478. if (guest_events && event->attr.exclude_hv)
  479. cflags |= SBI_PMU_CFG_FLAG_SET_SINH;
  480. if (event->attr.exclude_host)
  481. cflags |= SBI_PMU_CFG_FLAG_SET_UINH | SBI_PMU_CFG_FLAG_SET_SINH;
  482. if (event->attr.exclude_guest)
  483. cflags |= SBI_PMU_CFG_FLAG_SET_VSINH | SBI_PMU_CFG_FLAG_SET_VUINH;
  484. return cflags;
  485. }
  486. static int pmu_sbi_ctr_get_idx(struct perf_event *event)
  487. {
  488. struct hw_perf_event *hwc = &event->hw;
  489. struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
  490. struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
  491. struct sbiret ret;
  492. int idx;
  493. uint64_t cbase = 0, cmask = rvpmu->cmask;
  494. unsigned long cflags = 0;
  495. cflags = pmu_sbi_get_filter_flags(event);
  496. /*
  497. * In legacy mode, we have to force the fixed counters for those events
  498. * but not in the user access mode as we want to use the other counters
  499. * that support sampling/filtering.
  500. */
  501. if ((hwc->flags & PERF_EVENT_FLAG_LEGACY) && (event->attr.type == PERF_TYPE_HARDWARE)) {
  502. if (event->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
  503. cflags |= SBI_PMU_CFG_FLAG_SKIP_MATCH;
  504. cmask = 1;
  505. } else if (event->attr.config == PERF_COUNT_HW_INSTRUCTIONS) {
  506. cflags |= SBI_PMU_CFG_FLAG_SKIP_MATCH;
  507. cmask = BIT(CSR_INSTRET - CSR_CYCLE);
  508. }
  509. }
  510. /* retrieve the available counter index */
  511. #if defined(CONFIG_32BIT)
  512. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase,
  513. cmask, cflags, hwc->event_base, hwc->config,
  514. hwc->config >> 32);
  515. #else
  516. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase,
  517. cmask, cflags, hwc->event_base, hwc->config, 0);
  518. #endif
  519. if (ret.error) {
  520. pr_debug("Not able to find a counter for event %lx config %llx\n",
  521. hwc->event_base, hwc->config);
  522. return sbi_err_map_linux_errno(ret.error);
  523. }
  524. idx = ret.value;
  525. if (!test_bit(idx, &rvpmu->cmask) || !pmu_ctr_list[idx].value)
  526. return -ENOENT;
  527. /* Additional sanity check for the counter id */
  528. if (pmu_sbi_ctr_is_fw(idx)) {
  529. if (!test_and_set_bit(idx, cpuc->used_fw_ctrs))
  530. return idx;
  531. } else {
  532. if (!test_and_set_bit(idx, cpuc->used_hw_ctrs))
  533. return idx;
  534. }
  535. return -ENOENT;
  536. }
  537. static void pmu_sbi_ctr_clear_idx(struct perf_event *event)
  538. {
  539. struct hw_perf_event *hwc = &event->hw;
  540. struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
  541. struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
  542. int idx = hwc->idx;
  543. if (pmu_sbi_ctr_is_fw(idx))
  544. clear_bit(idx, cpuc->used_fw_ctrs);
  545. else
  546. clear_bit(idx, cpuc->used_hw_ctrs);
  547. }
  548. static int pmu_event_find_cache(u64 config)
  549. {
  550. unsigned int cache_type, cache_op, cache_result, ret;
  551. cache_type = (config >> 0) & 0xff;
  552. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  553. return -EINVAL;
  554. cache_op = (config >> 8) & 0xff;
  555. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  556. return -EINVAL;
  557. cache_result = (config >> 16) & 0xff;
  558. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  559. return -EINVAL;
  560. ret = pmu_cache_event_map[cache_type][cache_op][cache_result].event_idx;
  561. return ret;
  562. }
  563. static bool pmu_sbi_is_fw_event(struct perf_event *event)
  564. {
  565. u32 type = event->attr.type;
  566. u64 config = event->attr.config;
  567. if ((type == PERF_TYPE_RAW) && ((config >> 63) == 1))
  568. return true;
  569. else
  570. return false;
  571. }
  572. static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
  573. {
  574. u32 type = event->attr.type;
  575. u64 config = event->attr.config;
  576. /*
  577. * Ensure we are finished checking standard hardware events for
  578. * validity before allowing userspace to configure any events.
  579. */
  580. flush_work(&check_std_events_work);
  581. return riscv_pmu_get_event_info(type, config, econfig);
  582. }
  583. static void pmu_sbi_snapshot_free(struct riscv_pmu *pmu)
  584. {
  585. int cpu;
  586. for_each_possible_cpu(cpu) {
  587. struct cpu_hw_events *cpu_hw_evt = per_cpu_ptr(pmu->hw_events, cpu);
  588. if (!cpu_hw_evt->snapshot_addr)
  589. continue;
  590. free_page((unsigned long)cpu_hw_evt->snapshot_addr);
  591. cpu_hw_evt->snapshot_addr = NULL;
  592. cpu_hw_evt->snapshot_addr_phys = 0;
  593. }
  594. }
  595. static int pmu_sbi_snapshot_alloc(struct riscv_pmu *pmu)
  596. {
  597. int cpu;
  598. struct page *snapshot_page;
  599. for_each_possible_cpu(cpu) {
  600. struct cpu_hw_events *cpu_hw_evt = per_cpu_ptr(pmu->hw_events, cpu);
  601. snapshot_page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
  602. if (!snapshot_page) {
  603. pmu_sbi_snapshot_free(pmu);
  604. return -ENOMEM;
  605. }
  606. cpu_hw_evt->snapshot_addr = page_to_virt(snapshot_page);
  607. cpu_hw_evt->snapshot_addr_phys = page_to_phys(snapshot_page);
  608. }
  609. return 0;
  610. }
  611. static int pmu_sbi_snapshot_disable(void)
  612. {
  613. struct sbiret ret;
  614. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_SNAPSHOT_SET_SHMEM, SBI_SHMEM_DISABLE,
  615. SBI_SHMEM_DISABLE, 0, 0, 0, 0);
  616. if (ret.error) {
  617. pr_warn("failed to disable snapshot shared memory\n");
  618. return sbi_err_map_linux_errno(ret.error);
  619. }
  620. return 0;
  621. }
  622. static int pmu_sbi_snapshot_setup(struct riscv_pmu *pmu, int cpu)
  623. {
  624. struct cpu_hw_events *cpu_hw_evt;
  625. struct sbiret ret = {0};
  626. cpu_hw_evt = per_cpu_ptr(pmu->hw_events, cpu);
  627. if (!cpu_hw_evt->snapshot_addr_phys)
  628. return -EINVAL;
  629. if (cpu_hw_evt->snapshot_set_done)
  630. return 0;
  631. if (IS_ENABLED(CONFIG_32BIT))
  632. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_SNAPSHOT_SET_SHMEM,
  633. cpu_hw_evt->snapshot_addr_phys,
  634. (u64)(cpu_hw_evt->snapshot_addr_phys) >> 32, 0, 0, 0, 0);
  635. else
  636. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_SNAPSHOT_SET_SHMEM,
  637. cpu_hw_evt->snapshot_addr_phys, 0, 0, 0, 0, 0);
  638. /* Free up the snapshot area memory and fall back to SBI PMU calls without snapshot */
  639. if (ret.error) {
  640. if (ret.error != SBI_ERR_NOT_SUPPORTED)
  641. pr_warn("pmu snapshot setup failed with error %ld\n", ret.error);
  642. return sbi_err_map_linux_errno(ret.error);
  643. }
  644. memset(cpu_hw_evt->snapshot_cval_shcopy, 0, sizeof(u64) * RISCV_MAX_COUNTERS);
  645. cpu_hw_evt->snapshot_set_done = true;
  646. return 0;
  647. }
  648. static u64 pmu_sbi_ctr_read(struct perf_event *event)
  649. {
  650. struct hw_perf_event *hwc = &event->hw;
  651. int idx = hwc->idx;
  652. struct sbiret ret;
  653. u64 val = 0;
  654. struct riscv_pmu *pmu = to_riscv_pmu(event->pmu);
  655. struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
  656. struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
  657. union sbi_pmu_ctr_info info = pmu_ctr_list[idx];
  658. /* Read the value from the shared memory directly only if counter is stopped */
  659. if (sbi_pmu_snapshot_available() && (hwc->state & PERF_HES_STOPPED)) {
  660. val = sdata->ctr_values[idx];
  661. return val;
  662. }
  663. if (pmu_sbi_is_fw_event(event)) {
  664. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_FW_READ,
  665. hwc->idx, 0, 0, 0, 0, 0);
  666. if (ret.error)
  667. return 0;
  668. val = ret.value;
  669. if (IS_ENABLED(CONFIG_32BIT) && sbi_v2_available && info.width >= 32) {
  670. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_FW_READ_HI,
  671. hwc->idx, 0, 0, 0, 0, 0);
  672. if (!ret.error)
  673. val |= ((u64)ret.value << 32);
  674. else
  675. WARN_ONCE(1, "Unable to read upper 32 bits of firmware counter error: %ld\n",
  676. ret.error);
  677. }
  678. } else {
  679. val = riscv_pmu_ctr_read_csr(info.csr);
  680. if (IS_ENABLED(CONFIG_32BIT))
  681. val |= ((u64)riscv_pmu_ctr_read_csr(info.csr + 0x80)) << 32;
  682. }
  683. return val;
  684. }
  685. static void pmu_sbi_set_scounteren(void *arg)
  686. {
  687. struct perf_event *event = (struct perf_event *)arg;
  688. if (event->hw.idx != -1)
  689. csr_write(CSR_SCOUNTEREN,
  690. csr_read(CSR_SCOUNTEREN) | BIT(pmu_sbi_csr_index(event)));
  691. }
  692. static void pmu_sbi_reset_scounteren(void *arg)
  693. {
  694. struct perf_event *event = (struct perf_event *)arg;
  695. if (event->hw.idx != -1)
  696. csr_write(CSR_SCOUNTEREN,
  697. csr_read(CSR_SCOUNTEREN) & ~BIT(pmu_sbi_csr_index(event)));
  698. }
  699. static void pmu_sbi_ctr_start(struct perf_event *event, u64 ival)
  700. {
  701. struct sbiret ret;
  702. struct hw_perf_event *hwc = &event->hw;
  703. unsigned long flag = SBI_PMU_START_FLAG_SET_INIT_VALUE;
  704. /* There is no benefit setting SNAPSHOT FLAG for a single counter */
  705. #if defined(CONFIG_32BIT)
  706. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
  707. 1, flag, ival, ival >> 32, 0);
  708. #else
  709. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
  710. 1, flag, ival, 0, 0);
  711. #endif
  712. if (ret.error && (ret.error != SBI_ERR_ALREADY_STARTED))
  713. pr_err("Starting counter idx %d failed with error %d\n",
  714. hwc->idx, sbi_err_map_linux_errno(ret.error));
  715. if ((hwc->flags & PERF_EVENT_FLAG_USER_ACCESS) &&
  716. (hwc->flags & PERF_EVENT_FLAG_USER_READ_CNT))
  717. pmu_sbi_set_scounteren((void *)event);
  718. }
  719. static void pmu_sbi_ctr_stop(struct perf_event *event, unsigned long flag)
  720. {
  721. struct sbiret ret;
  722. struct hw_perf_event *hwc = &event->hw;
  723. struct riscv_pmu *pmu = to_riscv_pmu(event->pmu);
  724. struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
  725. struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
  726. if ((hwc->flags & PERF_EVENT_FLAG_USER_ACCESS) &&
  727. (hwc->flags & PERF_EVENT_FLAG_USER_READ_CNT))
  728. pmu_sbi_reset_scounteren((void *)event);
  729. if (sbi_pmu_snapshot_available())
  730. flag |= SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT;
  731. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, hwc->idx, 1, flag, 0, 0, 0);
  732. if (!ret.error && sbi_pmu_snapshot_available()) {
  733. /*
  734. * The counter snapshot is based on the index base specified by hwc->idx.
  735. * The actual counter value is updated in shared memory at index 0 when counter
  736. * mask is 0x01. To ensure accurate counter values, it's necessary to transfer
  737. * the counter value to shared memory. However, if hwc->idx is zero, the counter
  738. * value is already correctly updated in shared memory, requiring no further
  739. * adjustment.
  740. */
  741. if (hwc->idx > 0) {
  742. sdata->ctr_values[hwc->idx] = sdata->ctr_values[0];
  743. sdata->ctr_values[0] = 0;
  744. }
  745. } else if (ret.error && (ret.error != SBI_ERR_ALREADY_STOPPED) &&
  746. flag != SBI_PMU_STOP_FLAG_RESET) {
  747. pr_err("Stopping counter idx %d failed with error %d\n",
  748. hwc->idx, sbi_err_map_linux_errno(ret.error));
  749. }
  750. }
  751. static int pmu_sbi_find_num_ctrs(void)
  752. {
  753. struct sbiret ret;
  754. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_NUM_COUNTERS, 0, 0, 0, 0, 0, 0);
  755. if (!ret.error)
  756. return ret.value;
  757. else
  758. return sbi_err_map_linux_errno(ret.error);
  759. }
  760. static int pmu_sbi_get_ctrinfo(int nctr, unsigned long *mask)
  761. {
  762. struct sbiret ret;
  763. int i, num_hw_ctr = 0, num_fw_ctr = 0;
  764. union sbi_pmu_ctr_info cinfo;
  765. pmu_ctr_list = kzalloc_objs(*pmu_ctr_list, nctr);
  766. if (!pmu_ctr_list)
  767. return -ENOMEM;
  768. for (i = 0; i < nctr; i++) {
  769. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_GET_INFO, i, 0, 0, 0, 0, 0);
  770. if (ret.error)
  771. /* The logical counter ids are not expected to be contiguous */
  772. continue;
  773. *mask |= BIT(i);
  774. cinfo.value = ret.value;
  775. if (cinfo.type == SBI_PMU_CTR_TYPE_FW)
  776. num_fw_ctr++;
  777. else
  778. num_hw_ctr++;
  779. pmu_ctr_list[i].value = cinfo.value;
  780. }
  781. pr_info("%d firmware and %d hardware counters\n", num_fw_ctr, num_hw_ctr);
  782. return 0;
  783. }
  784. static inline void pmu_sbi_stop_all(struct riscv_pmu *pmu)
  785. {
  786. /*
  787. * No need to check the error because we are disabling all the counters
  788. * which may include counters that are not enabled yet.
  789. */
  790. sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP,
  791. 0, pmu->cmask, SBI_PMU_STOP_FLAG_RESET, 0, 0, 0);
  792. }
  793. static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu)
  794. {
  795. struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
  796. struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
  797. unsigned long flag = 0;
  798. int i, idx;
  799. struct sbiret ret;
  800. u64 temp_ctr_overflow_mask = 0;
  801. if (sbi_pmu_snapshot_available())
  802. flag = SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT;
  803. /* Reset the shadow copy to avoid save/restore any value from previous overflow */
  804. memset(cpu_hw_evt->snapshot_cval_shcopy, 0, sizeof(u64) * RISCV_MAX_COUNTERS);
  805. for (i = 0; i < BITS_TO_LONGS(RISCV_MAX_COUNTERS); i++) {
  806. /* No need to check the error here as we can't do anything about the error */
  807. ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, i * BITS_PER_LONG,
  808. cpu_hw_evt->used_hw_ctrs[i], flag, 0, 0, 0);
  809. if (!ret.error && sbi_pmu_snapshot_available()) {
  810. /* Save the counter values to avoid clobbering */
  811. for_each_set_bit(idx, &cpu_hw_evt->used_hw_ctrs[i], BITS_PER_LONG)
  812. cpu_hw_evt->snapshot_cval_shcopy[i * BITS_PER_LONG + idx] =
  813. sdata->ctr_values[idx];
  814. /* Save the overflow mask to avoid clobbering */
  815. temp_ctr_overflow_mask |= sdata->ctr_overflow_mask << (i * BITS_PER_LONG);
  816. }
  817. }
  818. /* Restore the counter values to the shared memory for used hw counters */
  819. if (sbi_pmu_snapshot_available()) {
  820. for_each_set_bit(idx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS)
  821. sdata->ctr_values[idx] = cpu_hw_evt->snapshot_cval_shcopy[idx];
  822. if (temp_ctr_overflow_mask)
  823. sdata->ctr_overflow_mask = temp_ctr_overflow_mask;
  824. }
  825. }
  826. /*
  827. * This function starts all the used counters in two step approach.
  828. * Any counter that did not overflow can be start in a single step
  829. * while the overflowed counters need to be started with updated initialization
  830. * value.
  831. */
  832. static inline void pmu_sbi_start_ovf_ctrs_sbi(struct cpu_hw_events *cpu_hw_evt,
  833. u64 ctr_ovf_mask)
  834. {
  835. int idx = 0, i;
  836. struct perf_event *event;
  837. unsigned long flag = SBI_PMU_START_FLAG_SET_INIT_VALUE;
  838. unsigned long ctr_start_mask = 0;
  839. uint64_t max_period;
  840. struct hw_perf_event *hwc;
  841. u64 init_val = 0;
  842. for (i = 0; i < BITS_TO_LONGS(RISCV_MAX_COUNTERS); i++) {
  843. ctr_start_mask = cpu_hw_evt->used_hw_ctrs[i] & ~ctr_ovf_mask;
  844. /* Start all the counters that did not overflow in a single shot */
  845. if (ctr_start_mask) {
  846. sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, i * BITS_PER_LONG,
  847. ctr_start_mask, 0, 0, 0, 0);
  848. }
  849. }
  850. /* Reinitialize and start all the counter that overflowed */
  851. while (ctr_ovf_mask) {
  852. if (ctr_ovf_mask & 0x01) {
  853. event = cpu_hw_evt->events[idx];
  854. hwc = &event->hw;
  855. max_period = riscv_pmu_ctr_get_width_mask(event);
  856. init_val = local64_read(&hwc->prev_count) & max_period;
  857. #if defined(CONFIG_32BIT)
  858. sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
  859. flag, init_val, init_val >> 32, 0);
  860. #else
  861. sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
  862. flag, init_val, 0, 0);
  863. #endif
  864. perf_event_update_userpage(event);
  865. }
  866. ctr_ovf_mask = ctr_ovf_mask >> 1;
  867. idx++;
  868. }
  869. }
  870. static inline void pmu_sbi_start_ovf_ctrs_snapshot(struct cpu_hw_events *cpu_hw_evt,
  871. u64 ctr_ovf_mask)
  872. {
  873. int i, idx = 0;
  874. struct perf_event *event;
  875. unsigned long flag = SBI_PMU_START_FLAG_INIT_SNAPSHOT;
  876. u64 max_period, init_val = 0;
  877. struct hw_perf_event *hwc;
  878. struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
  879. for_each_set_bit(idx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS) {
  880. if (ctr_ovf_mask & BIT(idx)) {
  881. event = cpu_hw_evt->events[idx];
  882. hwc = &event->hw;
  883. max_period = riscv_pmu_ctr_get_width_mask(event);
  884. init_val = local64_read(&hwc->prev_count) & max_period;
  885. cpu_hw_evt->snapshot_cval_shcopy[idx] = init_val;
  886. }
  887. /*
  888. * We do not need to update the non-overflow counters the previous
  889. * value should have been there already.
  890. */
  891. }
  892. for (i = 0; i < BITS_TO_LONGS(RISCV_MAX_COUNTERS); i++) {
  893. /* Restore the counter values to relative indices for used hw counters */
  894. for_each_set_bit(idx, &cpu_hw_evt->used_hw_ctrs[i], BITS_PER_LONG)
  895. sdata->ctr_values[idx] =
  896. cpu_hw_evt->snapshot_cval_shcopy[idx + i * BITS_PER_LONG];
  897. /* Start all the counters in a single shot */
  898. sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx * BITS_PER_LONG,
  899. cpu_hw_evt->used_hw_ctrs[i], flag, 0, 0, 0);
  900. }
  901. }
  902. static void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
  903. u64 ctr_ovf_mask)
  904. {
  905. struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
  906. if (sbi_pmu_snapshot_available())
  907. pmu_sbi_start_ovf_ctrs_snapshot(cpu_hw_evt, ctr_ovf_mask);
  908. else
  909. pmu_sbi_start_ovf_ctrs_sbi(cpu_hw_evt, ctr_ovf_mask);
  910. }
  911. static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
  912. {
  913. struct perf_sample_data data;
  914. struct pt_regs *regs;
  915. struct hw_perf_event *hw_evt;
  916. union sbi_pmu_ctr_info *info;
  917. int lidx, hidx, fidx;
  918. struct riscv_pmu *pmu;
  919. struct perf_event *event;
  920. u64 overflow;
  921. u64 overflowed_ctrs = 0;
  922. struct cpu_hw_events *cpu_hw_evt = dev;
  923. u64 start_clock = sched_clock();
  924. struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
  925. if (WARN_ON_ONCE(!cpu_hw_evt))
  926. return IRQ_NONE;
  927. /* Firmware counter don't support overflow yet */
  928. fidx = find_first_bit(cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS);
  929. if (fidx == RISCV_MAX_COUNTERS) {
  930. csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));
  931. return IRQ_NONE;
  932. }
  933. event = cpu_hw_evt->events[fidx];
  934. if (!event) {
  935. ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
  936. return IRQ_NONE;
  937. }
  938. pmu = to_riscv_pmu(event->pmu);
  939. pmu_sbi_stop_hw_ctrs(pmu);
  940. /* Overflow status register should only be read after counter are stopped */
  941. if (sbi_pmu_snapshot_available())
  942. overflow = sdata->ctr_overflow_mask;
  943. else
  944. ALT_SBI_PMU_OVERFLOW(overflow);
  945. /*
  946. * Overflow interrupt pending bit should only be cleared after stopping
  947. * all the counters to avoid any race condition.
  948. */
  949. ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
  950. /* No overflow bit is set */
  951. if (!overflow)
  952. return IRQ_NONE;
  953. regs = get_irq_regs();
  954. for_each_set_bit(lidx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS) {
  955. struct perf_event *event = cpu_hw_evt->events[lidx];
  956. /* Skip if invalid event or user did not request a sampling */
  957. if (!event || !is_sampling_event(event))
  958. continue;
  959. info = &pmu_ctr_list[lidx];
  960. /* Do a sanity check */
  961. if (!info || info->type != SBI_PMU_CTR_TYPE_HW)
  962. continue;
  963. if (sbi_pmu_snapshot_available())
  964. /* SBI implementation already updated the logical indicies */
  965. hidx = lidx;
  966. else
  967. /* compute hardware counter index */
  968. hidx = info->csr - CSR_CYCLE;
  969. /* check if the corresponding bit is set in scountovf or overflow mask in shmem */
  970. if (!(overflow & BIT(hidx)))
  971. continue;
  972. /*
  973. * Keep a track of overflowed counters so that they can be started
  974. * with updated initial value.
  975. */
  976. overflowed_ctrs |= BIT(lidx);
  977. hw_evt = &event->hw;
  978. /* Update the event states here so that we know the state while reading */
  979. hw_evt->state |= PERF_HES_STOPPED;
  980. riscv_pmu_event_update(event);
  981. hw_evt->state |= PERF_HES_UPTODATE;
  982. perf_sample_data_init(&data, 0, hw_evt->last_period);
  983. if (riscv_pmu_event_set_period(event)) {
  984. /*
  985. * Unlike other ISAs, RISC-V don't have to disable interrupts
  986. * to avoid throttling here. As per the specification, the
  987. * interrupt remains disabled until the OF bit is set.
  988. * Interrupts are enabled again only during the start.
  989. * TODO: We will need to stop the guest counters once
  990. * virtualization support is added.
  991. */
  992. perf_event_overflow(event, &data, regs);
  993. }
  994. /* Reset the state as we are going to start the counter after the loop */
  995. hw_evt->state = 0;
  996. }
  997. pmu_sbi_start_overflow_mask(pmu, overflowed_ctrs);
  998. perf_sample_event_took(sched_clock() - start_clock);
  999. return IRQ_HANDLED;
  1000. }
  1001. static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
  1002. {
  1003. struct riscv_pmu *pmu = hlist_entry_safe(node, struct riscv_pmu, node);
  1004. struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
  1005. /*
  1006. * We keep enabling userspace access to CYCLE, TIME and INSTRET via the
  1007. * legacy option but that will be removed in the future.
  1008. */
  1009. if (sysctl_perf_user_access == SYSCTL_LEGACY)
  1010. csr_write(CSR_SCOUNTEREN, 0x7);
  1011. else
  1012. csr_write(CSR_SCOUNTEREN, 0x2);
  1013. /* Stop all the counters so that they can be enabled from perf */
  1014. pmu_sbi_stop_all(pmu);
  1015. if (riscv_pmu_use_irq) {
  1016. cpu_hw_evt->irq = riscv_pmu_irq;
  1017. ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
  1018. enable_percpu_irq(riscv_pmu_irq, IRQ_TYPE_NONE);
  1019. }
  1020. if (sbi_pmu_snapshot_available())
  1021. return pmu_sbi_snapshot_setup(pmu, cpu);
  1022. return 0;
  1023. }
  1024. static int pmu_sbi_dying_cpu(unsigned int cpu, struct hlist_node *node)
  1025. {
  1026. if (riscv_pmu_use_irq) {
  1027. disable_percpu_irq(riscv_pmu_irq);
  1028. }
  1029. /* Disable all counters access for user mode now */
  1030. csr_write(CSR_SCOUNTEREN, 0x0);
  1031. if (sbi_pmu_snapshot_available())
  1032. return pmu_sbi_snapshot_disable();
  1033. return 0;
  1034. }
  1035. static int pmu_sbi_setup_irqs(struct riscv_pmu *pmu, struct platform_device *pdev)
  1036. {
  1037. int ret;
  1038. struct cpu_hw_events __percpu *hw_events = pmu->hw_events;
  1039. struct irq_domain *domain = NULL;
  1040. if (riscv_isa_extension_available(NULL, SSCOFPMF)) {
  1041. riscv_pmu_irq_num = RV_IRQ_PMU;
  1042. riscv_pmu_use_irq = true;
  1043. } else if (IS_ENABLED(CONFIG_ERRATA_THEAD_PMU) &&
  1044. riscv_cached_mvendorid(0) == THEAD_VENDOR_ID &&
  1045. riscv_cached_marchid(0) == 0 &&
  1046. riscv_cached_mimpid(0) == 0) {
  1047. riscv_pmu_irq_num = THEAD_C9XX_RV_IRQ_PMU;
  1048. riscv_pmu_use_irq = true;
  1049. } else if (riscv_has_vendor_extension_unlikely(ANDES_VENDOR_ID,
  1050. RISCV_ISA_VENDOR_EXT_XANDESPMU) &&
  1051. IS_ENABLED(CONFIG_ANDES_CUSTOM_PMU)) {
  1052. riscv_pmu_irq_num = ANDES_SLI_CAUSE_BASE + ANDES_RV_IRQ_PMOVI;
  1053. riscv_pmu_use_irq = true;
  1054. }
  1055. riscv_pmu_irq_mask = BIT(riscv_pmu_irq_num % BITS_PER_LONG);
  1056. if (!riscv_pmu_use_irq)
  1057. return -EOPNOTSUPP;
  1058. domain = irq_find_matching_fwnode(riscv_get_intc_hwnode(),
  1059. DOMAIN_BUS_ANY);
  1060. if (!domain) {
  1061. pr_err("Failed to find INTC IRQ root domain\n");
  1062. return -ENODEV;
  1063. }
  1064. riscv_pmu_irq = irq_create_mapping(domain, riscv_pmu_irq_num);
  1065. if (!riscv_pmu_irq) {
  1066. pr_err("Failed to map PMU interrupt for node\n");
  1067. return -ENODEV;
  1068. }
  1069. ret = request_percpu_irq(riscv_pmu_irq, pmu_sbi_ovf_handler, "riscv-pmu", hw_events);
  1070. if (ret) {
  1071. pr_err("registering percpu irq failed [%d]\n", ret);
  1072. return ret;
  1073. }
  1074. return 0;
  1075. }
  1076. #ifdef CONFIG_CPU_PM
  1077. static int riscv_pm_pmu_notify(struct notifier_block *b, unsigned long cmd,
  1078. void *v)
  1079. {
  1080. struct riscv_pmu *rvpmu = container_of(b, struct riscv_pmu, riscv_pm_nb);
  1081. struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
  1082. bool enabled = !bitmap_empty(cpuc->used_hw_ctrs, RISCV_MAX_COUNTERS);
  1083. struct perf_event *event;
  1084. int idx;
  1085. if (!enabled)
  1086. return NOTIFY_OK;
  1087. for (idx = 0; idx < RISCV_MAX_COUNTERS; idx++) {
  1088. event = cpuc->events[idx];
  1089. if (!event)
  1090. continue;
  1091. switch (cmd) {
  1092. case CPU_PM_ENTER:
  1093. /*
  1094. * Stop and update the counter
  1095. */
  1096. riscv_pmu_stop(event, PERF_EF_UPDATE);
  1097. break;
  1098. case CPU_PM_EXIT:
  1099. case CPU_PM_ENTER_FAILED:
  1100. /*
  1101. * Restore and enable the counter.
  1102. */
  1103. riscv_pmu_start(event, PERF_EF_RELOAD);
  1104. break;
  1105. default:
  1106. break;
  1107. }
  1108. }
  1109. return NOTIFY_OK;
  1110. }
  1111. static int riscv_pm_pmu_register(struct riscv_pmu *pmu)
  1112. {
  1113. pmu->riscv_pm_nb.notifier_call = riscv_pm_pmu_notify;
  1114. return cpu_pm_register_notifier(&pmu->riscv_pm_nb);
  1115. }
  1116. static void riscv_pm_pmu_unregister(struct riscv_pmu *pmu)
  1117. {
  1118. cpu_pm_unregister_notifier(&pmu->riscv_pm_nb);
  1119. }
  1120. #else
  1121. static inline int riscv_pm_pmu_register(struct riscv_pmu *pmu) { return 0; }
  1122. static inline void riscv_pm_pmu_unregister(struct riscv_pmu *pmu) { }
  1123. #endif
  1124. static void riscv_pmu_destroy(struct riscv_pmu *pmu)
  1125. {
  1126. if (sbi_v2_available) {
  1127. if (sbi_pmu_snapshot_available()) {
  1128. pmu_sbi_snapshot_disable();
  1129. pmu_sbi_snapshot_free(pmu);
  1130. }
  1131. }
  1132. riscv_pm_pmu_unregister(pmu);
  1133. cpuhp_state_remove_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
  1134. }
  1135. static void pmu_sbi_event_init(struct perf_event *event)
  1136. {
  1137. /*
  1138. * The permissions are set at event_init so that we do not depend
  1139. * on the sysctl value that can change.
  1140. */
  1141. if (sysctl_perf_user_access == SYSCTL_NO_USER_ACCESS)
  1142. event->hw.flags |= PERF_EVENT_FLAG_NO_USER_ACCESS;
  1143. else if (sysctl_perf_user_access == SYSCTL_USER_ACCESS)
  1144. event->hw.flags |= PERF_EVENT_FLAG_USER_ACCESS;
  1145. else
  1146. event->hw.flags |= PERF_EVENT_FLAG_LEGACY;
  1147. }
  1148. static void pmu_sbi_event_mapped(struct perf_event *event, struct mm_struct *mm)
  1149. {
  1150. if (event->hw.flags & PERF_EVENT_FLAG_NO_USER_ACCESS)
  1151. return;
  1152. if (event->hw.flags & PERF_EVENT_FLAG_LEGACY) {
  1153. if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES &&
  1154. event->attr.config != PERF_COUNT_HW_INSTRUCTIONS) {
  1155. return;
  1156. }
  1157. }
  1158. /*
  1159. * The user mmapped the event to directly access it: this is where
  1160. * we determine based on sysctl_perf_user_access if we grant userspace
  1161. * the direct access to this event. That means that within the same
  1162. * task, some events may be directly accessible and some other may not,
  1163. * if the user changes the value of sysctl_perf_user_accesss in the
  1164. * meantime.
  1165. */
  1166. event->hw.flags |= PERF_EVENT_FLAG_USER_READ_CNT;
  1167. /*
  1168. * We must enable userspace access *before* advertising in the user page
  1169. * that it is possible to do so to avoid any race.
  1170. * And we must notify all cpus here because threads that currently run
  1171. * on other cpus will try to directly access the counter too without
  1172. * calling pmu_sbi_ctr_start.
  1173. */
  1174. if (event->hw.flags & PERF_EVENT_FLAG_USER_ACCESS)
  1175. on_each_cpu_mask(mm_cpumask(mm),
  1176. pmu_sbi_set_scounteren, (void *)event, 1);
  1177. }
  1178. static void pmu_sbi_event_unmapped(struct perf_event *event, struct mm_struct *mm)
  1179. {
  1180. if (event->hw.flags & PERF_EVENT_FLAG_NO_USER_ACCESS)
  1181. return;
  1182. if (event->hw.flags & PERF_EVENT_FLAG_LEGACY) {
  1183. if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES &&
  1184. event->attr.config != PERF_COUNT_HW_INSTRUCTIONS) {
  1185. return;
  1186. }
  1187. }
  1188. /*
  1189. * Here we can directly remove user access since the user does not have
  1190. * access to the user page anymore so we avoid the racy window where the
  1191. * user could have read cap_user_rdpmc to true right before we disable
  1192. * it.
  1193. */
  1194. event->hw.flags &= ~PERF_EVENT_FLAG_USER_READ_CNT;
  1195. if (event->hw.flags & PERF_EVENT_FLAG_USER_ACCESS)
  1196. on_each_cpu_mask(mm_cpumask(mm),
  1197. pmu_sbi_reset_scounteren, (void *)event, 1);
  1198. }
  1199. static void riscv_pmu_update_counter_access(void *info)
  1200. {
  1201. if (sysctl_perf_user_access == SYSCTL_LEGACY)
  1202. csr_write(CSR_SCOUNTEREN, 0x7);
  1203. else
  1204. csr_write(CSR_SCOUNTEREN, 0x2);
  1205. }
  1206. static int riscv_pmu_proc_user_access_handler(const struct ctl_table *table,
  1207. int write, void *buffer,
  1208. size_t *lenp, loff_t *ppos)
  1209. {
  1210. int prev = sysctl_perf_user_access;
  1211. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  1212. /*
  1213. * Test against the previous value since we clear SCOUNTEREN when
  1214. * sysctl_perf_user_access is set to SYSCTL_USER_ACCESS, but we should
  1215. * not do that if that was already the case.
  1216. */
  1217. if (ret || !write || prev == sysctl_perf_user_access)
  1218. return ret;
  1219. on_each_cpu(riscv_pmu_update_counter_access, NULL, 1);
  1220. return 0;
  1221. }
  1222. static const struct ctl_table sbi_pmu_sysctl_table[] = {
  1223. {
  1224. .procname = "perf_user_access",
  1225. .data = &sysctl_perf_user_access,
  1226. .maxlen = sizeof(unsigned int),
  1227. .mode = 0644,
  1228. .proc_handler = riscv_pmu_proc_user_access_handler,
  1229. .extra1 = SYSCTL_ZERO,
  1230. .extra2 = SYSCTL_TWO,
  1231. },
  1232. };
  1233. static int pmu_sbi_device_probe(struct platform_device *pdev)
  1234. {
  1235. struct riscv_pmu *pmu = NULL;
  1236. int ret = -ENODEV;
  1237. int num_counters;
  1238. pr_info("SBI PMU extension is available\n");
  1239. pmu = riscv_pmu_alloc();
  1240. if (!pmu)
  1241. return -ENOMEM;
  1242. num_counters = pmu_sbi_find_num_ctrs();
  1243. if (num_counters < 0) {
  1244. pr_err("SBI PMU extension doesn't provide any counters\n");
  1245. goto out_free;
  1246. }
  1247. /* It is possible to get from SBI more than max number of counters */
  1248. if (num_counters > RISCV_MAX_COUNTERS) {
  1249. num_counters = RISCV_MAX_COUNTERS;
  1250. pr_info("SBI returned more than maximum number of counters. Limiting the number of counters to %d\n", num_counters);
  1251. }
  1252. /* cache all the information about counters now */
  1253. if (pmu_sbi_get_ctrinfo(num_counters, &cmask))
  1254. goto out_free;
  1255. ret = pmu_sbi_setup_irqs(pmu, pdev);
  1256. if (ret < 0) {
  1257. pr_info("Perf sampling/filtering is not supported as sscof extension is not available\n");
  1258. pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  1259. pmu->pmu.capabilities |= PERF_PMU_CAP_NO_EXCLUDE;
  1260. }
  1261. pmu->pmu.attr_groups = riscv_pmu_attr_groups;
  1262. pmu->pmu.parent = &pdev->dev;
  1263. pmu->cmask = cmask;
  1264. pmu->ctr_start = pmu_sbi_ctr_start;
  1265. pmu->ctr_stop = pmu_sbi_ctr_stop;
  1266. pmu->event_map = pmu_sbi_event_map;
  1267. pmu->ctr_get_idx = pmu_sbi_ctr_get_idx;
  1268. pmu->ctr_get_width = pmu_sbi_ctr_get_width;
  1269. pmu->ctr_clear_idx = pmu_sbi_ctr_clear_idx;
  1270. pmu->ctr_read = pmu_sbi_ctr_read;
  1271. pmu->event_init = pmu_sbi_event_init;
  1272. pmu->event_mapped = pmu_sbi_event_mapped;
  1273. pmu->event_unmapped = pmu_sbi_event_unmapped;
  1274. pmu->csr_index = pmu_sbi_csr_index;
  1275. ret = riscv_pm_pmu_register(pmu);
  1276. if (ret)
  1277. goto out_unregister;
  1278. ret = perf_pmu_register(&pmu->pmu, "cpu", PERF_TYPE_RAW);
  1279. if (ret)
  1280. goto out_unregister;
  1281. /* SBI PMU Snapsphot is only available in SBI v2.0 */
  1282. if (sbi_v2_available) {
  1283. int cpu;
  1284. ret = pmu_sbi_snapshot_alloc(pmu);
  1285. if (ret)
  1286. goto out_unregister;
  1287. cpu = get_cpu();
  1288. ret = pmu_sbi_snapshot_setup(pmu, cpu);
  1289. put_cpu();
  1290. if (ret) {
  1291. /* Snapshot is an optional feature. Continue if not available */
  1292. pmu_sbi_snapshot_free(pmu);
  1293. } else {
  1294. pr_info("SBI PMU snapshot detected\n");
  1295. /*
  1296. * We enable it once here for the boot cpu. If snapshot shmem setup
  1297. * fails during cpu hotplug process, it will fail to start the cpu
  1298. * as we can not handle hetergenous PMUs with different snapshot
  1299. * capability.
  1300. */
  1301. static_branch_enable(&sbi_pmu_snapshot_available);
  1302. }
  1303. }
  1304. register_sysctl("kernel", sbi_pmu_sysctl_table);
  1305. ret = cpuhp_state_add_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
  1306. if (ret)
  1307. goto out_unregister;
  1308. /* Asynchronously check which standard events are available */
  1309. schedule_work(&check_std_events_work);
  1310. return 0;
  1311. out_unregister:
  1312. riscv_pmu_destroy(pmu);
  1313. out_free:
  1314. kfree(pmu);
  1315. return ret;
  1316. }
  1317. static struct platform_driver pmu_sbi_driver = {
  1318. .probe = pmu_sbi_device_probe,
  1319. .driver = {
  1320. .name = RISCV_PMU_SBI_PDEV_NAME,
  1321. },
  1322. };
  1323. static int __init pmu_sbi_devinit(void)
  1324. {
  1325. int ret;
  1326. struct platform_device *pdev;
  1327. if (sbi_spec_version < sbi_mk_version(0, 3) ||
  1328. !sbi_probe_extension(SBI_EXT_PMU)) {
  1329. return 0;
  1330. }
  1331. if (sbi_spec_version >= sbi_mk_version(2, 0))
  1332. sbi_v2_available = true;
  1333. if (sbi_spec_version >= sbi_mk_version(3, 0))
  1334. sbi_v3_available = true;
  1335. ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_RISCV_STARTING,
  1336. "perf/riscv/pmu:starting",
  1337. pmu_sbi_starting_cpu, pmu_sbi_dying_cpu);
  1338. if (ret) {
  1339. pr_err("CPU hotplug notifier could not be registered: %d\n",
  1340. ret);
  1341. return ret;
  1342. }
  1343. ret = platform_driver_register(&pmu_sbi_driver);
  1344. if (ret)
  1345. return ret;
  1346. pdev = platform_device_register_simple(RISCV_PMU_SBI_PDEV_NAME, -1, NULL, 0);
  1347. if (IS_ERR(pdev)) {
  1348. platform_driver_unregister(&pmu_sbi_driver);
  1349. return PTR_ERR(pdev);
  1350. }
  1351. /* Notify legacy implementation that SBI pmu is available*/
  1352. riscv_pmu_legacy_skip_init();
  1353. return ret;
  1354. }
  1355. device_initcall(pmu_sbi_devinit)