cpuamu.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <cpuamu.h>
  7. #include <lib/el3_runtime/pubsub_events.h>
  8. #include <plat/common/platform.h>
  9. #define CPUAMU_NR_COUNTERS 5U
  10. struct cpuamu_ctx {
  11. uint64_t cnts[CPUAMU_NR_COUNTERS];
  12. unsigned int mask;
  13. };
  14. static struct cpuamu_ctx cpuamu_ctxs[PLATFORM_CORE_COUNT];
  15. int midr_match(unsigned int cpu_midr)
  16. {
  17. unsigned int midr, midr_mask;
  18. midr = (unsigned int)read_midr();
  19. midr_mask = (MIDR_IMPL_MASK << MIDR_IMPL_SHIFT) |
  20. (MIDR_PN_MASK << MIDR_PN_SHIFT);
  21. return ((midr & midr_mask) == (cpu_midr & midr_mask));
  22. }
  23. void cpuamu_context_save(unsigned int nr_counters)
  24. {
  25. struct cpuamu_ctx *ctx = &cpuamu_ctxs[plat_my_core_pos()];
  26. unsigned int i;
  27. assert(nr_counters <= CPUAMU_NR_COUNTERS);
  28. /* Save counter configuration */
  29. ctx->mask = cpuamu_read_cpuamcntenset_el0();
  30. /* Disable counters */
  31. cpuamu_write_cpuamcntenclr_el0(ctx->mask);
  32. isb();
  33. /* Save counters */
  34. for (i = 0; i < nr_counters; i++)
  35. ctx->cnts[i] = cpuamu_cnt_read(i);
  36. }
  37. void cpuamu_context_restore(unsigned int nr_counters)
  38. {
  39. struct cpuamu_ctx *ctx = &cpuamu_ctxs[plat_my_core_pos()];
  40. unsigned int i;
  41. assert(nr_counters <= CPUAMU_NR_COUNTERS);
  42. /*
  43. * Disable counters. They were enabled early in the
  44. * CPU reset function.
  45. */
  46. cpuamu_write_cpuamcntenclr_el0(ctx->mask);
  47. isb();
  48. /* Restore counters */
  49. for (i = 0; i < nr_counters; i++)
  50. cpuamu_cnt_write(i, ctx->cnts[i]);
  51. isb();
  52. /* Restore counter configuration */
  53. cpuamu_write_cpuamcntenset_el0(ctx->mask);
  54. }