sbi_pmu.c 25 KB

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