amu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Copyright (c) 2017-2023, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <cdefs.h>
  8. #include <inttypes.h>
  9. #include <stdbool.h>
  10. #include <stdint.h>
  11. #include "../amu_private.h"
  12. #include <arch.h>
  13. #include <arch_features.h>
  14. #include <arch_helpers.h>
  15. #include <common/debug.h>
  16. #include <lib/el3_runtime/pubsub_events.h>
  17. #include <lib/extensions/amu.h>
  18. #include <plat/common/platform.h>
  19. #if ENABLE_AMU_FCONF
  20. # include <lib/fconf/fconf.h>
  21. # include <lib/fconf/fconf_amu_getter.h>
  22. #endif
  23. #if ENABLE_MPMM
  24. # include <lib/mpmm/mpmm.h>
  25. #endif
  26. struct amu_ctx {
  27. uint64_t group0_cnts[AMU_GROUP0_MAX_COUNTERS];
  28. #if ENABLE_AMU_AUXILIARY_COUNTERS
  29. uint64_t group1_cnts[AMU_GROUP1_MAX_COUNTERS];
  30. #endif
  31. /* Architected event counter 1 does not have an offset register */
  32. uint64_t group0_voffsets[AMU_GROUP0_MAX_COUNTERS - 1U];
  33. #if ENABLE_AMU_AUXILIARY_COUNTERS
  34. uint64_t group1_voffsets[AMU_GROUP1_MAX_COUNTERS];
  35. #endif
  36. uint16_t group0_enable;
  37. #if ENABLE_AMU_AUXILIARY_COUNTERS
  38. uint16_t group1_enable;
  39. #endif
  40. };
  41. static struct amu_ctx amu_ctxs_[PLATFORM_CORE_COUNT];
  42. CASSERT((sizeof(amu_ctxs_[0].group0_enable) * CHAR_BIT) <= AMU_GROUP0_MAX_COUNTERS,
  43. amu_ctx_group0_enable_cannot_represent_all_group0_counters);
  44. #if ENABLE_AMU_AUXILIARY_COUNTERS
  45. CASSERT((sizeof(amu_ctxs_[0].group1_enable) * CHAR_BIT) <= AMU_GROUP1_MAX_COUNTERS,
  46. amu_ctx_group1_enable_cannot_represent_all_group1_counters);
  47. #endif
  48. static inline __unused uint64_t read_hcr_el2_amvoffen(void)
  49. {
  50. return (read_hcr_el2() & HCR_AMVOFFEN_BIT) >>
  51. HCR_AMVOFFEN_SHIFT;
  52. }
  53. static inline __unused void write_cptr_el2_tam(uint64_t value)
  54. {
  55. write_cptr_el2((read_cptr_el2() & ~CPTR_EL2_TAM_BIT) |
  56. ((value << CPTR_EL2_TAM_SHIFT) & CPTR_EL2_TAM_BIT));
  57. }
  58. static inline __unused void ctx_write_scr_el3_amvoffen(cpu_context_t *ctx, uint64_t amvoffen)
  59. {
  60. uint64_t value = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
  61. value &= ~SCR_AMVOFFEN_BIT;
  62. value |= (amvoffen << SCR_AMVOFFEN_SHIFT) & SCR_AMVOFFEN_BIT;
  63. write_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3, value);
  64. }
  65. static inline __unused void write_hcr_el2_amvoffen(uint64_t value)
  66. {
  67. write_hcr_el2((read_hcr_el2() & ~HCR_AMVOFFEN_BIT) |
  68. ((value << HCR_AMVOFFEN_SHIFT) & HCR_AMVOFFEN_BIT));
  69. }
  70. static inline __unused void write_amcr_el0_cg1rz(uint64_t value)
  71. {
  72. write_amcr_el0((read_amcr_el0() & ~AMCR_CG1RZ_BIT) |
  73. ((value << AMCR_CG1RZ_SHIFT) & AMCR_CG1RZ_BIT));
  74. }
  75. static inline __unused uint64_t read_amcfgr_el0_ncg(void)
  76. {
  77. return (read_amcfgr_el0() >> AMCFGR_EL0_NCG_SHIFT) &
  78. AMCFGR_EL0_NCG_MASK;
  79. }
  80. static inline __unused uint64_t read_amcgcr_el0_cg0nc(void)
  81. {
  82. return (read_amcgcr_el0() >> AMCGCR_EL0_CG0NC_SHIFT) &
  83. AMCGCR_EL0_CG0NC_MASK;
  84. }
  85. static inline __unused uint64_t read_amcg1idr_el0_voff(void)
  86. {
  87. return (read_amcg1idr_el0() >> AMCG1IDR_VOFF_SHIFT) &
  88. AMCG1IDR_VOFF_MASK;
  89. }
  90. static inline __unused uint64_t read_amcgcr_el0_cg1nc(void)
  91. {
  92. return (read_amcgcr_el0() >> AMCGCR_EL0_CG1NC_SHIFT) &
  93. AMCGCR_EL0_CG1NC_MASK;
  94. }
  95. static inline __unused uint64_t read_amcntenset0_el0_px(void)
  96. {
  97. return (read_amcntenset0_el0() >> AMCNTENSET0_EL0_Pn_SHIFT) &
  98. AMCNTENSET0_EL0_Pn_MASK;
  99. }
  100. static inline __unused uint64_t read_amcntenset1_el0_px(void)
  101. {
  102. return (read_amcntenset1_el0() >> AMCNTENSET1_EL0_Pn_SHIFT) &
  103. AMCNTENSET1_EL0_Pn_MASK;
  104. }
  105. static inline __unused void write_amcntenset0_el0_px(uint64_t px)
  106. {
  107. uint64_t value = read_amcntenset0_el0();
  108. value &= ~AMCNTENSET0_EL0_Pn_MASK;
  109. value |= (px << AMCNTENSET0_EL0_Pn_SHIFT) & AMCNTENSET0_EL0_Pn_MASK;
  110. write_amcntenset0_el0(value);
  111. }
  112. static inline __unused void write_amcntenset1_el0_px(uint64_t px)
  113. {
  114. uint64_t value = read_amcntenset1_el0();
  115. value &= ~AMCNTENSET1_EL0_Pn_MASK;
  116. value |= (px << AMCNTENSET1_EL0_Pn_SHIFT) & AMCNTENSET1_EL0_Pn_MASK;
  117. write_amcntenset1_el0(value);
  118. }
  119. static inline __unused void write_amcntenclr0_el0_px(uint64_t px)
  120. {
  121. uint64_t value = read_amcntenclr0_el0();
  122. value &= ~AMCNTENCLR0_EL0_Pn_MASK;
  123. value |= (px << AMCNTENCLR0_EL0_Pn_SHIFT) & AMCNTENCLR0_EL0_Pn_MASK;
  124. write_amcntenclr0_el0(value);
  125. }
  126. static inline __unused void write_amcntenclr1_el0_px(uint64_t px)
  127. {
  128. uint64_t value = read_amcntenclr1_el0();
  129. value &= ~AMCNTENCLR1_EL0_Pn_MASK;
  130. value |= (px << AMCNTENCLR1_EL0_Pn_SHIFT) & AMCNTENCLR1_EL0_Pn_MASK;
  131. write_amcntenclr1_el0(value);
  132. }
  133. #if ENABLE_AMU_AUXILIARY_COUNTERS
  134. static __unused bool amu_group1_supported(void)
  135. {
  136. return read_amcfgr_el0_ncg() > 0U;
  137. }
  138. #endif
  139. /*
  140. * Enable counters. This function is meant to be invoked by the context
  141. * management library before exiting from EL3.
  142. */
  143. void amu_enable(cpu_context_t *ctx)
  144. {
  145. /* Initialize FEAT_AMUv1p1 features if present. */
  146. if (is_feat_amuv1p1_supported()) {
  147. /*
  148. * Set SCR_EL3.AMVOFFEN to one so that accesses to virtual
  149. * offset registers at EL2 do not trap to EL3
  150. */
  151. ctx_write_scr_el3_amvoffen(ctx, 1U);
  152. }
  153. }
  154. void amu_enable_per_world(per_world_context_t *per_world_ctx)
  155. {
  156. /*
  157. * Set CPTR_EL3.TAM to zero so that any accesses to the Activity Monitor
  158. * registers do not trap to EL3.
  159. */
  160. uint64_t cptr_el3 = per_world_ctx->ctx_cptr_el3;
  161. cptr_el3 &= ~TAM_BIT;
  162. per_world_ctx->ctx_cptr_el3 = cptr_el3;
  163. }
  164. void amu_init_el3(void)
  165. {
  166. uint64_t group0_impl_ctr = read_amcgcr_el0_cg0nc();
  167. uint64_t group0_en_mask = (1 << (group0_impl_ctr)) - 1U;
  168. uint64_t num_ctr_groups = read_amcfgr_el0_ncg();
  169. /* Enable all architected counters by default */
  170. write_amcntenset0_el0_px(group0_en_mask);
  171. #if ENABLE_AMU_AUXILIARY_COUNTERS
  172. if (num_ctr_groups > 0U) {
  173. uint64_t amcntenset1_el0_px = 0x0; /* Group 1 enable mask */
  174. const struct amu_topology *topology;
  175. /*
  176. * The platform may opt to enable specific auxiliary counters.
  177. * This can be done via the common FCONF getter, or via the
  178. * platform-implemented function.
  179. */
  180. #if ENABLE_AMU_FCONF
  181. topology = FCONF_GET_PROPERTY(amu, config, topology);
  182. #else
  183. topology = plat_amu_topology();
  184. #endif /* ENABLE_AMU_FCONF */
  185. if (topology != NULL) {
  186. unsigned int core_pos = plat_my_core_pos();
  187. amcntenset1_el0_px = topology->cores[core_pos].enable;
  188. } else {
  189. ERROR("AMU: failed to generate AMU topology\n");
  190. }
  191. write_amcntenset1_el0_px(amcntenset1_el0_px);
  192. }
  193. #else /* ENABLE_AMU_AUXILIARY_COUNTERS */
  194. if (num_ctr_groups > 0U) {
  195. VERBOSE("AMU: auxiliary counters detected but support is disabled\n");
  196. }
  197. #endif /* ENABLE_AMU_AUXILIARY_COUNTERS */
  198. if (is_feat_amuv1p1_supported()) {
  199. #if AMU_RESTRICT_COUNTERS
  200. /*
  201. * FEAT_AMUv1p1 adds a register field to restrict access to
  202. * group 1 counters at all but the highest implemented EL. This
  203. * is controlled with the `AMU_RESTRICT_COUNTERS` compile time
  204. * flag, when set, system register reads at lower ELs return
  205. * zero. Reads from the memory mapped view are unaffected.
  206. */
  207. VERBOSE("AMU group 1 counter access restricted.\n");
  208. write_amcr_el0_cg1rz(1U);
  209. #else
  210. write_amcr_el0_cg1rz(0U);
  211. #endif
  212. }
  213. #if ENABLE_MPMM
  214. mpmm_enable();
  215. #endif
  216. }
  217. void amu_init_el2_unused(void)
  218. {
  219. /*
  220. * CPTR_EL2.TAM: Set to zero so any accesses to the Activity Monitor
  221. * registers do not trap to EL2.
  222. */
  223. write_cptr_el2_tam(0U);
  224. /* Initialize FEAT_AMUv1p1 features if present. */
  225. if (is_feat_amuv1p1_supported()) {
  226. /* Make sure virtual offsets are disabled if EL2 not used. */
  227. write_hcr_el2_amvoffen(0U);
  228. }
  229. }
  230. /* Read the group 0 counter identified by the given `idx`. */
  231. static uint64_t amu_group0_cnt_read(unsigned int idx)
  232. {
  233. assert(is_feat_amu_supported());
  234. assert(idx < read_amcgcr_el0_cg0nc());
  235. return amu_group0_cnt_read_internal(idx);
  236. }
  237. /* Write the group 0 counter identified by the given `idx` with `val` */
  238. static void amu_group0_cnt_write(unsigned int idx, uint64_t val)
  239. {
  240. assert(is_feat_amu_supported());
  241. assert(idx < read_amcgcr_el0_cg0nc());
  242. amu_group0_cnt_write_internal(idx, val);
  243. isb();
  244. }
  245. /*
  246. * Unlike with auxiliary counters, we cannot detect at runtime whether an
  247. * architected counter supports a virtual offset. These are instead fixed
  248. * according to FEAT_AMUv1p1, but this switch will need to be updated if later
  249. * revisions of FEAT_AMU add additional architected counters.
  250. */
  251. static bool amu_group0_voffset_supported(uint64_t idx)
  252. {
  253. switch (idx) {
  254. case 0U:
  255. case 2U:
  256. case 3U:
  257. return true;
  258. case 1U:
  259. return false;
  260. default:
  261. ERROR("AMU: can't set up virtual offset for unknown "
  262. "architected counter %" PRIu64 "!\n", idx);
  263. panic();
  264. }
  265. }
  266. /*
  267. * Read the group 0 offset register for a given index. Index must be 0, 2,
  268. * or 3, the register for 1 does not exist.
  269. *
  270. * Using this function requires FEAT_AMUv1p1 support.
  271. */
  272. static uint64_t amu_group0_voffset_read(unsigned int idx)
  273. {
  274. assert(is_feat_amuv1p1_supported());
  275. assert(idx < read_amcgcr_el0_cg0nc());
  276. assert(idx != 1U);
  277. return amu_group0_voffset_read_internal(idx);
  278. }
  279. /*
  280. * Write the group 0 offset register for a given index. Index must be 0, 2, or
  281. * 3, the register for 1 does not exist.
  282. *
  283. * Using this function requires FEAT_AMUv1p1 support.
  284. */
  285. static void amu_group0_voffset_write(unsigned int idx, uint64_t val)
  286. {
  287. assert(is_feat_amuv1p1_supported());
  288. assert(idx < read_amcgcr_el0_cg0nc());
  289. assert(idx != 1U);
  290. amu_group0_voffset_write_internal(idx, val);
  291. isb();
  292. }
  293. #if ENABLE_AMU_AUXILIARY_COUNTERS
  294. /* Read the group 1 counter identified by the given `idx` */
  295. static uint64_t amu_group1_cnt_read(unsigned int idx)
  296. {
  297. assert(is_feat_amu_supported());
  298. assert(amu_group1_supported());
  299. assert(idx < read_amcgcr_el0_cg1nc());
  300. return amu_group1_cnt_read_internal(idx);
  301. }
  302. /* Write the group 1 counter identified by the given `idx` with `val` */
  303. static void amu_group1_cnt_write(unsigned int idx, uint64_t val)
  304. {
  305. assert(is_feat_amu_supported());
  306. assert(amu_group1_supported());
  307. assert(idx < read_amcgcr_el0_cg1nc());
  308. amu_group1_cnt_write_internal(idx, val);
  309. isb();
  310. }
  311. /*
  312. * Read the group 1 offset register for a given index.
  313. *
  314. * Using this function requires FEAT_AMUv1p1 support.
  315. */
  316. static uint64_t amu_group1_voffset_read(unsigned int idx)
  317. {
  318. assert(is_feat_amuv1p1_supported());
  319. assert(amu_group1_supported());
  320. assert(idx < read_amcgcr_el0_cg1nc());
  321. assert((read_amcg1idr_el0_voff() & (UINT64_C(1) << idx)) != 0U);
  322. return amu_group1_voffset_read_internal(idx);
  323. }
  324. /*
  325. * Write the group 1 offset register for a given index.
  326. *
  327. * Using this function requires FEAT_AMUv1p1 support.
  328. */
  329. static void amu_group1_voffset_write(unsigned int idx, uint64_t val)
  330. {
  331. assert(is_feat_amuv1p1_supported());
  332. assert(amu_group1_supported());
  333. assert(idx < read_amcgcr_el0_cg1nc());
  334. assert((read_amcg1idr_el0_voff() & (UINT64_C(1) << idx)) != 0U);
  335. amu_group1_voffset_write_internal(idx, val);
  336. isb();
  337. }
  338. #endif
  339. static void *amu_context_save(const void *arg)
  340. {
  341. uint64_t i, j;
  342. unsigned int core_pos;
  343. struct amu_ctx *ctx;
  344. uint64_t hcr_el2_amvoffen = 0; /* AMU virtual offsets enabled */
  345. uint64_t amcgcr_el0_cg0nc; /* Number of group 0 counters */
  346. #if ENABLE_AMU_AUXILIARY_COUNTERS
  347. uint64_t amcg1idr_el0_voff; /* Auxiliary counters with virtual offsets */
  348. uint64_t amcfgr_el0_ncg; /* Number of counter groups */
  349. uint64_t amcgcr_el0_cg1nc; /* Number of group 1 counters */
  350. #endif
  351. if (!is_feat_amu_supported()) {
  352. return (void *)0;
  353. }
  354. core_pos = plat_my_core_pos();
  355. ctx = &amu_ctxs_[core_pos];
  356. amcgcr_el0_cg0nc = read_amcgcr_el0_cg0nc();
  357. if (is_feat_amuv1p1_supported()) {
  358. hcr_el2_amvoffen = read_hcr_el2_amvoffen();
  359. }
  360. #if ENABLE_AMU_AUXILIARY_COUNTERS
  361. amcfgr_el0_ncg = read_amcfgr_el0_ncg();
  362. amcgcr_el0_cg1nc = (amcfgr_el0_ncg > 0U) ? read_amcgcr_el0_cg1nc() : 0U;
  363. amcg1idr_el0_voff = (hcr_el2_amvoffen != 0U) ? read_amcg1idr_el0_voff() : 0U;
  364. #endif
  365. /*
  366. * Disable all AMU counters.
  367. */
  368. ctx->group0_enable = read_amcntenset0_el0_px();
  369. write_amcntenclr0_el0_px(ctx->group0_enable);
  370. #if ENABLE_AMU_AUXILIARY_COUNTERS
  371. if (amcfgr_el0_ncg > 0U) {
  372. ctx->group1_enable = read_amcntenset1_el0_px();
  373. write_amcntenclr1_el0_px(ctx->group1_enable);
  374. }
  375. #endif
  376. /*
  377. * Save the counters to the local context.
  378. */
  379. isb(); /* Ensure counters have been stopped */
  380. for (i = 0U; i < amcgcr_el0_cg0nc; i++) {
  381. ctx->group0_cnts[i] = amu_group0_cnt_read(i);
  382. }
  383. #if ENABLE_AMU_AUXILIARY_COUNTERS
  384. for (i = 0U; i < amcgcr_el0_cg1nc; i++) {
  385. ctx->group1_cnts[i] = amu_group1_cnt_read(i);
  386. }
  387. #endif
  388. /*
  389. * Save virtual offsets for counters that offer them.
  390. */
  391. if (hcr_el2_amvoffen != 0U) {
  392. for (i = 0U, j = 0U; i < amcgcr_el0_cg0nc; i++) {
  393. if (!amu_group0_voffset_supported(i)) {
  394. continue; /* No virtual offset */
  395. }
  396. ctx->group0_voffsets[j++] = amu_group0_voffset_read(i);
  397. }
  398. #if ENABLE_AMU_AUXILIARY_COUNTERS
  399. for (i = 0U, j = 0U; i < amcgcr_el0_cg1nc; i++) {
  400. if ((amcg1idr_el0_voff >> i) & 1U) {
  401. continue; /* No virtual offset */
  402. }
  403. ctx->group1_voffsets[j++] = amu_group1_voffset_read(i);
  404. }
  405. #endif
  406. }
  407. return (void *)0;
  408. }
  409. static void *amu_context_restore(const void *arg)
  410. {
  411. uint64_t i, j;
  412. unsigned int core_pos;
  413. struct amu_ctx *ctx;
  414. uint64_t hcr_el2_amvoffen = 0; /* AMU virtual offsets enabled */
  415. uint64_t amcgcr_el0_cg0nc; /* Number of group 0 counters */
  416. #if ENABLE_AMU_AUXILIARY_COUNTERS
  417. uint64_t amcfgr_el0_ncg; /* Number of counter groups */
  418. uint64_t amcgcr_el0_cg1nc; /* Number of group 1 counters */
  419. uint64_t amcg1idr_el0_voff; /* Auxiliary counters with virtual offsets */
  420. #endif
  421. if (!is_feat_amu_supported()) {
  422. return (void *)0;
  423. }
  424. core_pos = plat_my_core_pos();
  425. ctx = &amu_ctxs_[core_pos];
  426. amcgcr_el0_cg0nc = read_amcgcr_el0_cg0nc();
  427. if (is_feat_amuv1p1_supported()) {
  428. hcr_el2_amvoffen = read_hcr_el2_amvoffen();
  429. }
  430. #if ENABLE_AMU_AUXILIARY_COUNTERS
  431. amcfgr_el0_ncg = read_amcfgr_el0_ncg();
  432. amcgcr_el0_cg1nc = (amcfgr_el0_ncg > 0U) ? read_amcgcr_el0_cg1nc() : 0U;
  433. amcg1idr_el0_voff = (hcr_el2_amvoffen != 0U) ? read_amcg1idr_el0_voff() : 0U;
  434. #endif
  435. /*
  436. * Restore the counter values from the local context.
  437. */
  438. for (i = 0U; i < amcgcr_el0_cg0nc; i++) {
  439. amu_group0_cnt_write(i, ctx->group0_cnts[i]);
  440. }
  441. #if ENABLE_AMU_AUXILIARY_COUNTERS
  442. for (i = 0U; i < amcgcr_el0_cg1nc; i++) {
  443. amu_group1_cnt_write(i, ctx->group1_cnts[i]);
  444. }
  445. #endif
  446. /*
  447. * Restore virtual offsets for counters that offer them.
  448. */
  449. if (hcr_el2_amvoffen != 0U) {
  450. for (i = 0U, j = 0U; i < amcgcr_el0_cg0nc; i++) {
  451. if (!amu_group0_voffset_supported(i)) {
  452. continue; /* No virtual offset */
  453. }
  454. amu_group0_voffset_write(i, ctx->group0_voffsets[j++]);
  455. }
  456. #if ENABLE_AMU_AUXILIARY_COUNTERS
  457. for (i = 0U, j = 0U; i < amcgcr_el0_cg1nc; i++) {
  458. if ((amcg1idr_el0_voff >> i) & 1U) {
  459. continue; /* No virtual offset */
  460. }
  461. amu_group1_voffset_write(i, ctx->group1_voffsets[j++]);
  462. }
  463. #endif
  464. }
  465. /*
  466. * Re-enable counters that were disabled during context save.
  467. */
  468. write_amcntenset0_el0_px(ctx->group0_enable);
  469. #if ENABLE_AMU_AUXILIARY_COUNTERS
  470. if (amcfgr_el0_ncg > 0) {
  471. write_amcntenset1_el0_px(ctx->group1_enable);
  472. }
  473. #endif
  474. #if ENABLE_MPMM
  475. mpmm_enable();
  476. #endif
  477. return (void *)0;
  478. }
  479. SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_start, amu_context_save);
  480. SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_finish, amu_context_restore);