sbi_pmu.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2021 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Atish Patra <atish.patra@wdc.com>
  8. */
  9. #include <sbi/riscv_asm.h>
  10. #include <sbi/sbi_bitops.h>
  11. #include <sbi/sbi_console.h>
  12. #include <sbi/sbi_ecall_interface.h>
  13. #include <sbi/sbi_hart.h>
  14. #include <sbi/sbi_hartmask.h>
  15. #include <sbi/sbi_platform.h>
  16. #include <sbi/sbi_pmu.h>
  17. #include <sbi/sbi_scratch.h>
  18. #include <sbi/sbi_string.h>
  19. /** Information about hardware counters */
  20. struct sbi_pmu_hw_event {
  21. uint32_t counters;
  22. uint32_t start_idx;
  23. uint32_t end_idx;
  24. /* Event selector value used only for raw events. The event select value
  25. * can be a even id or a selector value for set of events encoded in few
  26. * bits. In case latter, the bits used for encoding of the events should
  27. * be zeroed out in the select value.
  28. */
  29. uint64_t select;
  30. /**
  31. * The select_mask indicates which bits are encoded for the event(s).
  32. */
  33. uint64_t select_mask;
  34. };
  35. /* Information about PMU counters as per SBI specification */
  36. union sbi_pmu_ctr_info {
  37. unsigned long value;
  38. struct {
  39. unsigned long csr:12;
  40. unsigned long width:6;
  41. #if __riscv_xlen == 32
  42. unsigned long reserved:13;
  43. #else
  44. unsigned long reserved:45;
  45. #endif
  46. unsigned long type:1;
  47. };
  48. };
  49. /* Platform specific PMU device */
  50. static const struct sbi_pmu_device *pmu_dev = NULL;
  51. /* Mapping between event range and possible counters */
  52. static struct sbi_pmu_hw_event hw_event_map[SBI_PMU_HW_EVENT_MAX] = {0};
  53. /* counter to enabled event mapping */
  54. static uint32_t active_events[SBI_HARTMASK_MAX_BITS][SBI_PMU_HW_CTR_MAX + SBI_PMU_FW_CTR_MAX];
  55. /* Bitmap of firmware counters started on each HART */
  56. #if SBI_PMU_FW_CTR_MAX >= BITS_PER_LONG
  57. #error "Can't handle firmware counters beyond BITS_PER_LONG"
  58. #endif
  59. static unsigned long fw_counters_started[SBI_HARTMASK_MAX_BITS];
  60. /*
  61. * Counter values for SBI firmware events and event codes for platform
  62. * firmware events. Both are mutually exclusive and hence can optimally share
  63. * the same memory.
  64. */
  65. static uint64_t fw_counters_data[SBI_HARTMASK_MAX_BITS][SBI_PMU_FW_CTR_MAX] = {0};
  66. /* Maximum number of hardware events available */
  67. static uint32_t num_hw_events;
  68. /* Maximum number of hardware counters available */
  69. static uint32_t num_hw_ctrs;
  70. /* Maximum number of counters available */
  71. static uint32_t total_ctrs;
  72. /* Helper macros to retrieve event idx and code type */
  73. #define get_cidx_type(x) ((x & SBI_PMU_EVENT_IDX_TYPE_MASK) >> 16)
  74. #define get_cidx_code(x) (x & SBI_PMU_EVENT_IDX_CODE_MASK)
  75. /**
  76. * Perform a sanity check on event & counter mappings with event range overlap check
  77. * @param evtA Pointer to the existing hw event structure
  78. * @param evtB Pointer to the new hw event structure
  79. *
  80. * Return false if the range doesn't overlap, true otherwise
  81. */
  82. static bool pmu_event_range_overlap(struct sbi_pmu_hw_event *evtA,
  83. struct sbi_pmu_hw_event *evtB)
  84. {
  85. /* check if the range of events overlap with a previous entry */
  86. if (((evtA->end_idx < evtB->start_idx) && (evtA->end_idx < evtB->end_idx)) ||
  87. ((evtA->start_idx > evtB->start_idx) && (evtA->start_idx > evtB->end_idx)))
  88. return false;
  89. return true;
  90. }
  91. static bool pmu_event_select_overlap(struct sbi_pmu_hw_event *evt,
  92. uint64_t select_val, uint64_t select_mask)
  93. {
  94. if ((evt->select == select_val) && (evt->select_mask == select_mask))
  95. return true;
  96. return false;
  97. }
  98. static int pmu_event_validate(unsigned long event_idx)
  99. {
  100. uint32_t event_idx_type = get_cidx_type(event_idx);
  101. uint32_t event_idx_code = get_cidx_code(event_idx);
  102. uint32_t event_idx_code_max = -1;
  103. uint32_t cache_ops_result, cache_ops_id, cache_id;
  104. switch(event_idx_type) {
  105. case SBI_PMU_EVENT_TYPE_HW:
  106. event_idx_code_max = SBI_PMU_HW_GENERAL_MAX;
  107. break;
  108. case SBI_PMU_EVENT_TYPE_FW:
  109. if (SBI_PMU_FW_MAX <= event_idx_code &&
  110. pmu_dev && pmu_dev->fw_event_validate_code)
  111. return pmu_dev->fw_event_validate_code(event_idx_code);
  112. else
  113. event_idx_code_max = SBI_PMU_FW_MAX;
  114. break;
  115. case SBI_PMU_EVENT_TYPE_HW_CACHE:
  116. cache_ops_result = event_idx_code &
  117. SBI_PMU_EVENT_HW_CACHE_OPS_RESULT;
  118. cache_ops_id = (event_idx_code &
  119. SBI_PMU_EVENT_HW_CACHE_OPS_ID_MASK) >>
  120. SBI_PMU_EVENT_HW_CACHE_OPS_ID_OFFSET;
  121. cache_id = (event_idx_code &
  122. SBI_PMU_EVENT_HW_CACHE_ID_MASK) >>
  123. SBI_PMU_EVENT_HW_CACHE_ID_OFFSET;
  124. if ((cache_ops_result < SBI_PMU_HW_CACHE_RESULT_MAX) &&
  125. (cache_ops_id < SBI_PMU_HW_CACHE_OP_MAX) &&
  126. (cache_id < SBI_PMU_HW_CACHE_MAX))
  127. return event_idx_type;
  128. else
  129. return SBI_EINVAL;
  130. break;
  131. case SBI_PMU_EVENT_TYPE_HW_RAW:
  132. event_idx_code_max = 1; // event_idx.code should be zero
  133. break;
  134. default:
  135. return SBI_EINVAL;
  136. }
  137. if (event_idx_code < event_idx_code_max)
  138. return event_idx_type;
  139. return SBI_EINVAL;
  140. }
  141. static int pmu_ctr_validate(uint32_t cidx, uint32_t *event_idx_code)
  142. {
  143. uint32_t event_idx_val;
  144. uint32_t event_idx_type;
  145. u32 hartid = current_hartid();
  146. if (cidx >= total_ctrs)
  147. return SBI_EINVAL;
  148. event_idx_val = active_events[hartid][cidx];
  149. event_idx_type = get_cidx_type(event_idx_val);
  150. if (event_idx_val == SBI_PMU_EVENT_IDX_INVALID ||
  151. event_idx_type >= SBI_PMU_EVENT_TYPE_MAX)
  152. return SBI_EINVAL;
  153. *event_idx_code = get_cidx_code(event_idx_val);
  154. return event_idx_type;
  155. }
  156. int sbi_pmu_ctr_fw_read(uint32_t cidx, uint64_t *cval)
  157. {
  158. int event_idx_type;
  159. uint32_t event_code;
  160. u32 hartid = current_hartid();
  161. event_idx_type = pmu_ctr_validate(cidx, &event_code);
  162. if (event_idx_type != SBI_PMU_EVENT_TYPE_FW)
  163. return SBI_EINVAL;
  164. if (SBI_PMU_FW_MAX <= event_code &&
  165. pmu_dev && pmu_dev->fw_counter_read_value)
  166. fw_counters_data[hartid][cidx - num_hw_ctrs] =
  167. pmu_dev->fw_counter_read_value(cidx - num_hw_ctrs);
  168. *cval = fw_counters_data[hartid][cidx - num_hw_ctrs];
  169. return 0;
  170. }
  171. static int pmu_add_hw_event_map(u32 eidx_start, u32 eidx_end, u32 cmap,
  172. uint64_t select, uint64_t select_mask)
  173. {
  174. int i = 0;
  175. bool is_overlap;
  176. struct sbi_pmu_hw_event *event = &hw_event_map[num_hw_events];
  177. struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
  178. int hw_ctr_avail = sbi_hart_mhpm_count(scratch);
  179. uint32_t ctr_avail_mask = ((uint32_t)(~0) >> (32 - (hw_ctr_avail + 3)));
  180. /* The first two counters are reserved by priv spec */
  181. if (eidx_start > SBI_PMU_HW_INSTRUCTIONS && (cmap & SBI_PMU_FIXED_CTR_MASK))
  182. return SBI_EDENIED;
  183. if (num_hw_events >= SBI_PMU_HW_EVENT_MAX - 1) {
  184. sbi_printf("Can not handle more than %d perf events\n",
  185. SBI_PMU_HW_EVENT_MAX);
  186. return SBI_EFAIL;
  187. }
  188. event->start_idx = eidx_start;
  189. event->end_idx = eidx_end;
  190. /* Sanity check */
  191. for (i = 0; i < num_hw_events; i++) {
  192. if (eidx_start == SBI_PMU_EVENT_RAW_IDX)
  193. /* All raw events have same event idx. Just do sanity check on select */
  194. is_overlap = pmu_event_select_overlap(&hw_event_map[i],
  195. select, select_mask);
  196. else
  197. is_overlap = pmu_event_range_overlap(&hw_event_map[i], event);
  198. if (is_overlap)
  199. goto reset_event;
  200. }
  201. event->select_mask = select_mask;
  202. /* Map the only the counters that are available in the hardware */
  203. event->counters = cmap & ctr_avail_mask;
  204. event->select = select;
  205. num_hw_events++;
  206. return 0;
  207. reset_event:
  208. event->start_idx = 0;
  209. event->end_idx = 0;
  210. return SBI_EINVAL;
  211. }
  212. /**
  213. * Logical counter ids are assigned to hardware counters are assigned consecutively.
  214. * E.g. counter0 must count MCYCLE where counter2 must count minstret. Similarly,
  215. * counterX will mhpmcounterX.
  216. */
  217. int sbi_pmu_add_hw_event_counter_map(u32 eidx_start, u32 eidx_end, u32 cmap)
  218. {
  219. if ((eidx_start > eidx_end) || eidx_start == SBI_PMU_EVENT_RAW_IDX ||
  220. eidx_end == SBI_PMU_EVENT_RAW_IDX)
  221. return SBI_EINVAL;
  222. return pmu_add_hw_event_map(eidx_start, eidx_end, cmap, 0, 0);
  223. }
  224. int sbi_pmu_add_raw_event_counter_map(uint64_t select, uint64_t select_mask, u32 cmap)
  225. {
  226. return pmu_add_hw_event_map(SBI_PMU_EVENT_RAW_IDX,
  227. SBI_PMU_EVENT_RAW_IDX, cmap, select, select_mask);
  228. }
  229. static int pmu_ctr_enable_irq_hw(int ctr_idx)
  230. {
  231. unsigned long mhpmevent_csr;
  232. unsigned long mhpmevent_curr;
  233. unsigned long mip_val;
  234. unsigned long of_mask;
  235. if (ctr_idx < 3 || ctr_idx >= SBI_PMU_HW_CTR_MAX)
  236. return SBI_EFAIL;
  237. #if __riscv_xlen == 32
  238. mhpmevent_csr = CSR_MHPMEVENT3H + ctr_idx - 3;
  239. of_mask = (uint32_t)~MHPMEVENTH_OF;
  240. #else
  241. mhpmevent_csr = CSR_MHPMEVENT3 + ctr_idx - 3;
  242. of_mask = ~MHPMEVENT_OF;
  243. #endif
  244. mhpmevent_curr = csr_read_num(mhpmevent_csr);
  245. mip_val = csr_read(CSR_MIP);
  246. /**
  247. * Clear out the OF bit so that next interrupt can be enabled.
  248. * This should be done only when the corresponding overflow interrupt
  249. * bit is cleared. That indicates that software has already handled the
  250. * previous interrupts or the hardware yet to set an overflow interrupt.
  251. * Otherwise, there will be race conditions where we may clear the bit
  252. * the software is yet to handle the interrupt.
  253. */
  254. if (!(mip_val & MIP_LCOFIP)) {
  255. mhpmevent_curr &= of_mask;
  256. csr_write_num(mhpmevent_csr, mhpmevent_curr);
  257. }
  258. return 0;
  259. }
  260. static void pmu_ctr_write_hw(uint32_t cidx, uint64_t ival)
  261. {
  262. #if __riscv_xlen == 32
  263. csr_write_num(CSR_MCYCLE + cidx, 0);
  264. csr_write_num(CSR_MCYCLE + cidx, ival & 0xFFFFFFFF);
  265. csr_write_num(CSR_MCYCLEH + cidx, ival >> BITS_PER_LONG);
  266. #else
  267. csr_write_num(CSR_MCYCLE + cidx, ival);
  268. #endif
  269. }
  270. static int pmu_ctr_start_hw(uint32_t cidx, uint64_t ival, bool ival_update)
  271. {
  272. struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
  273. unsigned long mctr_inhbt;
  274. /* Make sure the counter index lies within the range and is not TM bit */
  275. if (cidx >= num_hw_ctrs || cidx == 1)
  276. return SBI_EINVAL;
  277. if (sbi_hart_priv_version(scratch) < SBI_HART_PRIV_VER_1_11)
  278. goto skip_inhibit_update;
  279. /*
  280. * Some of the hardware may not support mcountinhibit but perf stat
  281. * still can work if supervisor mode programs the initial value.
  282. */
  283. mctr_inhbt = csr_read(CSR_MCOUNTINHIBIT);
  284. if (!__test_bit(cidx, &mctr_inhbt))
  285. return SBI_EALREADY_STARTED;
  286. __clear_bit(cidx, &mctr_inhbt);
  287. if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCOFPMF))
  288. pmu_ctr_enable_irq_hw(cidx);
  289. if (pmu_dev && pmu_dev->hw_counter_enable_irq)
  290. pmu_dev->hw_counter_enable_irq(cidx);
  291. csr_write(CSR_MCOUNTINHIBIT, mctr_inhbt);
  292. skip_inhibit_update:
  293. if (ival_update)
  294. pmu_ctr_write_hw(cidx, ival);
  295. return 0;
  296. }
  297. int sbi_pmu_irq_bit(void)
  298. {
  299. struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
  300. if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCOFPMF))
  301. return MIP_LCOFIP;
  302. if (pmu_dev && pmu_dev->hw_counter_irq_bit)
  303. return pmu_dev->hw_counter_irq_bit();
  304. return 0;
  305. }
  306. static int pmu_ctr_start_fw(uint32_t cidx, uint32_t event_code,
  307. uint64_t ival, bool ival_update)
  308. {
  309. int ret;
  310. u32 hartid = current_hartid();
  311. if (SBI_PMU_FW_MAX <= event_code &&
  312. pmu_dev && pmu_dev->fw_counter_start) {
  313. ret = pmu_dev->fw_counter_start(cidx - num_hw_ctrs,
  314. event_code,
  315. ival, ival_update);
  316. if (ret)
  317. return ret;
  318. }
  319. if (ival_update)
  320. fw_counters_data[hartid][cidx - num_hw_ctrs] = ival;
  321. fw_counters_started[hartid] |= BIT(cidx - num_hw_ctrs);
  322. return 0;
  323. }
  324. int sbi_pmu_ctr_start(unsigned long cbase, unsigned long cmask,
  325. unsigned long flags, uint64_t ival)
  326. {
  327. int event_idx_type;
  328. uint32_t event_code;
  329. int ret = SBI_EINVAL;
  330. bool bUpdate = false;
  331. int i, cidx;
  332. if ((cbase + sbi_fls(cmask)) >= total_ctrs)
  333. return ret;
  334. if (flags & SBI_PMU_START_FLAG_SET_INIT_VALUE)
  335. bUpdate = true;
  336. for_each_set_bit(i, &cmask, total_ctrs) {
  337. cidx = i + cbase;
  338. event_idx_type = pmu_ctr_validate(cidx, &event_code);
  339. if (event_idx_type < 0)
  340. /* Continue the start operation for other counters */
  341. continue;
  342. else if (event_idx_type == SBI_PMU_EVENT_TYPE_FW)
  343. ret = pmu_ctr_start_fw(cidx, event_code, ival, bUpdate);
  344. else
  345. ret = pmu_ctr_start_hw(cidx, ival, bUpdate);
  346. }
  347. return ret;
  348. }
  349. static int pmu_ctr_stop_hw(uint32_t cidx)
  350. {
  351. struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
  352. unsigned long mctr_inhbt;
  353. if (sbi_hart_priv_version(scratch) < SBI_HART_PRIV_VER_1_11)
  354. return 0;
  355. mctr_inhbt = csr_read(CSR_MCOUNTINHIBIT);
  356. /* Make sure the counter index lies within the range and is not TM bit */
  357. if (cidx >= num_hw_ctrs || cidx == 1)
  358. return SBI_EINVAL;
  359. if (!__test_bit(cidx, &mctr_inhbt)) {
  360. __set_bit(cidx, &mctr_inhbt);
  361. csr_write(CSR_MCOUNTINHIBIT, mctr_inhbt);
  362. return 0;
  363. } else
  364. return SBI_EALREADY_STOPPED;
  365. }
  366. static int pmu_ctr_stop_fw(uint32_t cidx, uint32_t event_code)
  367. {
  368. int ret;
  369. if (SBI_PMU_FW_MAX <= event_code &&
  370. pmu_dev && pmu_dev->fw_counter_stop) {
  371. ret = pmu_dev->fw_counter_stop(cidx - num_hw_ctrs);
  372. if (ret)
  373. return ret;
  374. }
  375. fw_counters_started[current_hartid()] &= ~BIT(cidx - num_hw_ctrs);
  376. return 0;
  377. }
  378. static int pmu_reset_hw_mhpmevent(int ctr_idx)
  379. {
  380. if (ctr_idx < 3 || ctr_idx >= SBI_PMU_HW_CTR_MAX)
  381. return SBI_EFAIL;
  382. #if __riscv_xlen == 32
  383. csr_write_num(CSR_MHPMEVENT3 + ctr_idx - 3, 0);
  384. if (sbi_hart_has_extension(sbi_scratch_thishart_ptr(),
  385. SBI_HART_EXT_SSCOFPMF))
  386. csr_write_num(CSR_MHPMEVENT3H + ctr_idx - 3, 0);
  387. #else
  388. csr_write_num(CSR_MHPMEVENT3 + ctr_idx - 3, 0);
  389. #endif
  390. return 0;
  391. }
  392. int sbi_pmu_ctr_stop(unsigned long cbase, unsigned long cmask,
  393. unsigned long flag)
  394. {
  395. u32 hartid = current_hartid();
  396. int ret = SBI_EINVAL;
  397. int event_idx_type;
  398. uint32_t event_code;
  399. int i, cidx;
  400. if ((cbase + sbi_fls(cmask)) >= total_ctrs)
  401. return SBI_EINVAL;
  402. for_each_set_bit(i, &cmask, total_ctrs) {
  403. cidx = i + cbase;
  404. event_idx_type = pmu_ctr_validate(cidx, &event_code);
  405. if (event_idx_type < 0)
  406. /* Continue the stop operation for other counters */
  407. continue;
  408. else if (event_idx_type == SBI_PMU_EVENT_TYPE_FW)
  409. ret = pmu_ctr_stop_fw(cidx, event_code);
  410. else
  411. ret = pmu_ctr_stop_hw(cidx);
  412. if (flag & SBI_PMU_STOP_FLAG_RESET) {
  413. active_events[hartid][cidx] = SBI_PMU_EVENT_IDX_INVALID;
  414. pmu_reset_hw_mhpmevent(cidx);
  415. }
  416. }
  417. return ret;
  418. }
  419. static void pmu_update_inhibit_flags(unsigned long flags, uint64_t *mhpmevent_val)
  420. {
  421. if (flags & SBI_PMU_CFG_FLAG_SET_VUINH)
  422. *mhpmevent_val |= MHPMEVENT_VUINH;
  423. if (flags & SBI_PMU_CFG_FLAG_SET_VSINH)
  424. *mhpmevent_val |= MHPMEVENT_VSINH;
  425. if (flags & SBI_PMU_CFG_FLAG_SET_UINH)
  426. *mhpmevent_val |= MHPMEVENT_UINH;
  427. if (flags & SBI_PMU_CFG_FLAG_SET_SINH)
  428. *mhpmevent_val |= MHPMEVENT_SINH;
  429. }
  430. static int pmu_update_hw_mhpmevent(struct sbi_pmu_hw_event *hw_evt, int ctr_idx,
  431. unsigned long flags, unsigned long eindex,
  432. uint64_t data)
  433. {
  434. struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
  435. const struct sbi_platform *plat = sbi_platform_ptr(scratch);
  436. uint64_t mhpmevent_val;
  437. /* Get the final mhpmevent value to be written from platform */
  438. mhpmevent_val = sbi_platform_pmu_xlate_to_mhpmevent(plat, eindex, data);
  439. if (!mhpmevent_val || ctr_idx < 3 || ctr_idx >= SBI_PMU_HW_CTR_MAX)
  440. return SBI_EFAIL;
  441. /**
  442. * Always set the OVF bit(disable interrupts) and inhibit counting of
  443. * events in M-mode. The OVF bit should be enabled during the start call.
  444. */
  445. if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCOFPMF))
  446. mhpmevent_val = (mhpmevent_val & ~MHPMEVENT_SSCOF_MASK) |
  447. MHPMEVENT_MINH | MHPMEVENT_OF;
  448. if (pmu_dev && pmu_dev->hw_counter_disable_irq)
  449. pmu_dev->hw_counter_disable_irq(ctr_idx);
  450. /* Update the inhibit flags based on inhibit flags received from supervisor */
  451. pmu_update_inhibit_flags(flags, &mhpmevent_val);
  452. #if __riscv_xlen == 32
  453. csr_write_num(CSR_MHPMEVENT3 + ctr_idx - 3, mhpmevent_val & 0xFFFFFFFF);
  454. if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCOFPMF))
  455. csr_write_num(CSR_MHPMEVENT3H + ctr_idx - 3,
  456. mhpmevent_val >> BITS_PER_LONG);
  457. #else
  458. csr_write_num(CSR_MHPMEVENT3 + ctr_idx - 3, mhpmevent_val);
  459. #endif
  460. return 0;
  461. }
  462. static int pmu_ctr_find_fixed_fw(unsigned long evt_idx_code)
  463. {
  464. /* Non-programmables counters are enabled always. No need to do lookup */
  465. if (evt_idx_code == SBI_PMU_HW_CPU_CYCLES)
  466. return 0;
  467. else if (evt_idx_code == SBI_PMU_HW_INSTRUCTIONS)
  468. return 2;
  469. else
  470. return SBI_EINVAL;
  471. }
  472. static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned long flags,
  473. unsigned long event_idx, uint64_t data)
  474. {
  475. unsigned long ctr_mask;
  476. int i, ret = 0, fixed_ctr, ctr_idx = SBI_ENOTSUPP;
  477. struct sbi_pmu_hw_event *temp;
  478. unsigned long mctr_inhbt = 0;
  479. u32 hartid = current_hartid();
  480. struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
  481. if (cbase >= num_hw_ctrs)
  482. return SBI_EINVAL;
  483. /**
  484. * If Sscof is present try to find the programmable counter for
  485. * cycle/instret as well.
  486. */
  487. fixed_ctr = pmu_ctr_find_fixed_fw(event_idx);
  488. if (fixed_ctr >= 0 &&
  489. !sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCOFPMF))
  490. return fixed_ctr;
  491. if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_11)
  492. mctr_inhbt = csr_read(CSR_MCOUNTINHIBIT);
  493. for (i = 0; i < num_hw_events; i++) {
  494. temp = &hw_event_map[i];
  495. if ((temp->start_idx > event_idx && event_idx < temp->end_idx) ||
  496. (temp->start_idx < event_idx && event_idx > temp->end_idx))
  497. continue;
  498. /* For raw events, event data is used as the select value */
  499. if (event_idx == SBI_PMU_EVENT_RAW_IDX) {
  500. uint64_t select_mask = temp->select_mask;
  501. /* The non-event map bits of data should match the selector */
  502. if (temp->select != (data & select_mask))
  503. continue;
  504. }
  505. /* Fixed counters should not be part of the search */
  506. ctr_mask = temp->counters & (cmask << cbase) &
  507. (~SBI_PMU_FIXED_CTR_MASK);
  508. for_each_set_bit_from(cbase, &ctr_mask, SBI_PMU_HW_CTR_MAX) {
  509. /**
  510. * Some of the platform may not support mcountinhibit.
  511. * Checking the active_events is enough for them
  512. */
  513. if (active_events[hartid][cbase] != SBI_PMU_EVENT_IDX_INVALID)
  514. continue;
  515. /* If mcountinhibit is supported, the bit must be enabled */
  516. if ((sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_11) &&
  517. !__test_bit(cbase, &mctr_inhbt))
  518. continue;
  519. /* We found a valid counter that is not started yet */
  520. ctr_idx = cbase;
  521. }
  522. }
  523. if (ctr_idx == SBI_ENOTSUPP) {
  524. /**
  525. * We can't find any programmable counters for cycle/instret.
  526. * Return the fixed counter as they are mandatory anyways.
  527. */
  528. if (fixed_ctr >= 0)
  529. return fixed_ctr;
  530. else
  531. return SBI_EFAIL;
  532. }
  533. ret = pmu_update_hw_mhpmevent(temp, ctr_idx, flags, event_idx, data);
  534. if (!ret)
  535. ret = ctr_idx;
  536. return ret;
  537. }
  538. /**
  539. * Any firmware counter can map to any firmware event.
  540. * Thus, select the first available fw counter after sanity
  541. * check.
  542. */
  543. static int pmu_ctr_find_fw(unsigned long cbase, unsigned long cmask,
  544. uint32_t event_code, u32 hartid)
  545. {
  546. int i, cidx;
  547. for_each_set_bit(i, &cmask, BITS_PER_LONG) {
  548. cidx = i + cbase;
  549. if (cidx < num_hw_ctrs || total_ctrs <= cidx)
  550. continue;
  551. if (active_events[hartid][i] != SBI_PMU_EVENT_IDX_INVALID)
  552. continue;
  553. if (SBI_PMU_FW_MAX <= event_code &&
  554. pmu_dev && pmu_dev->fw_counter_match_code) {
  555. if (!pmu_dev->fw_counter_match_code(cidx - num_hw_ctrs,
  556. event_code))
  557. continue;
  558. }
  559. return i;
  560. }
  561. return SBI_ENOTSUPP;
  562. }
  563. int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask,
  564. unsigned long flags, unsigned long event_idx,
  565. uint64_t event_data)
  566. {
  567. int ret, ctr_idx = SBI_ENOTSUPP;
  568. u32 event_code, hartid = current_hartid();
  569. int event_type;
  570. /* Do a basic sanity check of counter base & mask */
  571. if ((cidx_base + sbi_fls(cidx_mask)) >= total_ctrs)
  572. return SBI_EINVAL;
  573. event_type = pmu_event_validate(event_idx);
  574. if (event_type < 0)
  575. return SBI_EINVAL;
  576. event_code = get_cidx_code(event_idx);
  577. if (flags & SBI_PMU_CFG_FLAG_SKIP_MATCH) {
  578. /* The caller wants to skip the match because it already knows the
  579. * counter idx for the given event. Verify that the counter idx
  580. * is still valid.
  581. */
  582. if (active_events[hartid][cidx_base] == SBI_PMU_EVENT_IDX_INVALID)
  583. return SBI_EINVAL;
  584. ctr_idx = cidx_base;
  585. goto skip_match;
  586. }
  587. if (event_type == SBI_PMU_EVENT_TYPE_FW) {
  588. /* Any firmware counter can be used track any firmware event */
  589. ctr_idx = pmu_ctr_find_fw(cidx_base, cidx_mask, event_code, hartid);
  590. } else {
  591. ctr_idx = pmu_ctr_find_hw(cidx_base, cidx_mask, flags, event_idx,
  592. event_data);
  593. }
  594. if (ctr_idx < 0)
  595. return SBI_ENOTSUPP;
  596. active_events[hartid][ctr_idx] = event_idx;
  597. skip_match:
  598. if (event_type == SBI_PMU_EVENT_TYPE_HW) {
  599. if (flags & SBI_PMU_CFG_FLAG_CLEAR_VALUE)
  600. pmu_ctr_write_hw(ctr_idx, 0);
  601. if (flags & SBI_PMU_CFG_FLAG_AUTO_START)
  602. pmu_ctr_start_hw(ctr_idx, 0, false);
  603. } else if (event_type == SBI_PMU_EVENT_TYPE_FW) {
  604. if (flags & SBI_PMU_CFG_FLAG_CLEAR_VALUE)
  605. fw_counters_data[hartid][ctr_idx - num_hw_ctrs] = 0;
  606. if (flags & SBI_PMU_CFG_FLAG_AUTO_START) {
  607. if (SBI_PMU_FW_MAX <= event_code &&
  608. pmu_dev && pmu_dev->fw_counter_start) {
  609. ret = pmu_dev->fw_counter_start(
  610. ctr_idx - num_hw_ctrs, event_code,
  611. fw_counters_data[hartid][ctr_idx - num_hw_ctrs],
  612. true);
  613. if (ret)
  614. return ret;
  615. }
  616. fw_counters_started[hartid] |= BIT(ctr_idx - num_hw_ctrs);
  617. }
  618. }
  619. return ctr_idx;
  620. }
  621. int sbi_pmu_ctr_incr_fw(enum sbi_pmu_fw_event_code_id fw_id)
  622. {
  623. u32 cidx, hartid = current_hartid();
  624. uint64_t *fcounter = NULL;
  625. if (likely(!fw_counters_started[hartid]))
  626. return 0;
  627. if (unlikely(fw_id >= SBI_PMU_FW_MAX))
  628. return SBI_EINVAL;
  629. for (cidx = num_hw_ctrs; cidx < total_ctrs; cidx++) {
  630. if (get_cidx_code(active_events[hartid][cidx]) == fw_id &&
  631. (fw_counters_started[hartid] & BIT(cidx - num_hw_ctrs))) {
  632. fcounter = &fw_counters_data[hartid][cidx - num_hw_ctrs];
  633. break;
  634. }
  635. }
  636. if (fcounter)
  637. (*fcounter)++;
  638. return 0;
  639. }
  640. unsigned long sbi_pmu_num_ctr(void)
  641. {
  642. return (num_hw_ctrs + SBI_PMU_FW_CTR_MAX);
  643. }
  644. int sbi_pmu_ctr_get_info(uint32_t cidx, unsigned long *ctr_info)
  645. {
  646. int width;
  647. union sbi_pmu_ctr_info cinfo = {0};
  648. struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
  649. /* Sanity check. Counter1 is not mapped at all */
  650. if (cidx >= total_ctrs || cidx == 1)
  651. return SBI_EINVAL;
  652. /* We have 31 HW counters with 31 being the last index(MHPMCOUNTER31) */
  653. if (cidx < num_hw_ctrs) {
  654. cinfo.type = SBI_PMU_CTR_TYPE_HW;
  655. cinfo.csr = CSR_CYCLE + cidx;
  656. /* mcycle & minstret are always 64 bit */
  657. if (cidx == 0 || cidx == 2)
  658. cinfo.width = 63;
  659. else
  660. cinfo.width = sbi_hart_mhpm_bits(scratch) - 1;
  661. } else {
  662. /* it's a firmware counter */
  663. cinfo.type = SBI_PMU_CTR_TYPE_FW;
  664. /* Firmware counters are always 64 bits wide */
  665. cinfo.width = 63;
  666. if (pmu_dev && pmu_dev->fw_counter_width) {
  667. width = pmu_dev->fw_counter_width();
  668. if (width)
  669. cinfo.width = width - 1;
  670. }
  671. }
  672. *ctr_info = cinfo.value;
  673. return 0;
  674. }
  675. static void pmu_reset_event_map(u32 hartid)
  676. {
  677. int j;
  678. /* Initialize the counter to event mapping table */
  679. for (j = 3; j < total_ctrs; j++)
  680. active_events[hartid][j] = SBI_PMU_EVENT_IDX_INVALID;
  681. for (j = 0; j < SBI_PMU_FW_CTR_MAX; j++)
  682. fw_counters_data[hartid][j] = 0;
  683. fw_counters_started[hartid] = 0;
  684. }
  685. const struct sbi_pmu_device *sbi_pmu_get_device(void)
  686. {
  687. return pmu_dev;
  688. }
  689. void sbi_pmu_set_device(const struct sbi_pmu_device *dev)
  690. {
  691. if (!dev || pmu_dev)
  692. return;
  693. pmu_dev = dev;
  694. }
  695. void sbi_pmu_exit(struct sbi_scratch *scratch)
  696. {
  697. u32 hartid = current_hartid();
  698. if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_11)
  699. csr_write(CSR_MCOUNTINHIBIT, 0xFFFFFFF8);
  700. if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_10)
  701. csr_write(CSR_MCOUNTEREN, -1);
  702. pmu_reset_event_map(hartid);
  703. }
  704. int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot)
  705. {
  706. const struct sbi_platform *plat;
  707. u32 hartid = current_hartid();
  708. if (cold_boot) {
  709. plat = sbi_platform_ptr(scratch);
  710. /* Initialize hw pmu events */
  711. sbi_platform_pmu_init(plat);
  712. /* mcycle & minstret is available always */
  713. num_hw_ctrs = sbi_hart_mhpm_count(scratch) + 3;
  714. total_ctrs = num_hw_ctrs + SBI_PMU_FW_CTR_MAX;
  715. }
  716. pmu_reset_event_map(hartid);
  717. /* First three counters are fixed by the priv spec and we enable it by default */
  718. active_events[hartid][0] = SBI_PMU_EVENT_TYPE_HW << SBI_PMU_EVENT_IDX_OFFSET |
  719. SBI_PMU_HW_CPU_CYCLES;
  720. active_events[hartid][1] = SBI_PMU_EVENT_IDX_INVALID;
  721. active_events[hartid][2] = SBI_PMU_EVENT_TYPE_HW << SBI_PMU_EVENT_IDX_OFFSET |
  722. SBI_PMU_HW_INSTRUCTIONS;
  723. return 0;
  724. }