riscv_pmu_legacy.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 implementation is based on old RISC-V perf and ARM perf event code
  8. * which are in turn based on sparc64 and x86 code.
  9. */
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/perf/riscv_pmu.h>
  12. #include <linux/platform_device.h>
  13. #define RISCV_PMU_LEGACY_CYCLE 0
  14. #define RISCV_PMU_LEGACY_INSTRET 1
  15. static bool pmu_init_done;
  16. static int pmu_legacy_ctr_get_idx(struct perf_event *event)
  17. {
  18. struct perf_event_attr *attr = &event->attr;
  19. if (event->attr.type != PERF_TYPE_HARDWARE)
  20. return -EOPNOTSUPP;
  21. if (attr->config == PERF_COUNT_HW_CPU_CYCLES)
  22. return RISCV_PMU_LEGACY_CYCLE;
  23. else if (attr->config == PERF_COUNT_HW_INSTRUCTIONS)
  24. return RISCV_PMU_LEGACY_INSTRET;
  25. else
  26. return -EOPNOTSUPP;
  27. }
  28. /* For legacy config & counter index are same */
  29. static int pmu_legacy_event_map(struct perf_event *event, u64 *config)
  30. {
  31. return pmu_legacy_ctr_get_idx(event);
  32. }
  33. static u64 pmu_legacy_read_ctr(struct perf_event *event)
  34. {
  35. struct hw_perf_event *hwc = &event->hw;
  36. int idx = hwc->idx;
  37. u64 val;
  38. if (idx == RISCV_PMU_LEGACY_CYCLE) {
  39. val = riscv_pmu_ctr_read_csr(CSR_CYCLE);
  40. if (IS_ENABLED(CONFIG_32BIT))
  41. val = (u64)riscv_pmu_ctr_read_csr(CSR_CYCLEH) << 32 | val;
  42. } else if (idx == RISCV_PMU_LEGACY_INSTRET) {
  43. val = riscv_pmu_ctr_read_csr(CSR_INSTRET);
  44. if (IS_ENABLED(CONFIG_32BIT))
  45. val = ((u64)riscv_pmu_ctr_read_csr(CSR_INSTRETH)) << 32 | val;
  46. } else
  47. return 0;
  48. return val;
  49. }
  50. static void pmu_legacy_ctr_start(struct perf_event *event, u64 ival)
  51. {
  52. struct hw_perf_event *hwc = &event->hw;
  53. u64 initial_val = pmu_legacy_read_ctr(event);
  54. /**
  55. * The legacy method doesn't really have a start/stop method.
  56. * It also can not update the counter with a initial value.
  57. * But we still need to set the prev_count so that read() can compute
  58. * the delta. Just use the current counter value to set the prev_count.
  59. */
  60. local64_set(&hwc->prev_count, initial_val);
  61. }
  62. /*
  63. * This is just a simple implementation to allow legacy implementations
  64. * compatible with new RISC-V PMU driver framework.
  65. * This driver only allows reading two counters i.e CYCLE & INSTRET.
  66. * However, it can not start or stop the counter. Thus, it is not very useful
  67. * will be removed in future.
  68. */
  69. static void pmu_legacy_init(struct riscv_pmu *pmu)
  70. {
  71. pr_info("Legacy PMU implementation is available\n");
  72. pmu->cmask = BIT(RISCV_PMU_LEGACY_CYCLE) |
  73. BIT(RISCV_PMU_LEGACY_INSTRET);
  74. pmu->ctr_start = pmu_legacy_ctr_start;
  75. pmu->ctr_stop = NULL;
  76. pmu->event_map = pmu_legacy_event_map;
  77. pmu->ctr_get_idx = pmu_legacy_ctr_get_idx;
  78. pmu->ctr_get_width = NULL;
  79. pmu->ctr_clear_idx = NULL;
  80. pmu->ctr_read = pmu_legacy_read_ctr;
  81. perf_pmu_register(&pmu->pmu, "cpu", PERF_TYPE_RAW);
  82. }
  83. static int pmu_legacy_device_probe(struct platform_device *pdev)
  84. {
  85. struct riscv_pmu *pmu = NULL;
  86. pmu = riscv_pmu_alloc();
  87. if (!pmu)
  88. return -ENOMEM;
  89. pmu_legacy_init(pmu);
  90. return 0;
  91. }
  92. static struct platform_driver pmu_legacy_driver = {
  93. .probe = pmu_legacy_device_probe,
  94. .driver = {
  95. .name = RISCV_PMU_LEGACY_PDEV_NAME,
  96. },
  97. };
  98. static int __init riscv_pmu_legacy_devinit(void)
  99. {
  100. int ret;
  101. struct platform_device *pdev;
  102. if (likely(pmu_init_done))
  103. return 0;
  104. ret = platform_driver_register(&pmu_legacy_driver);
  105. if (ret)
  106. return ret;
  107. pdev = platform_device_register_simple(RISCV_PMU_LEGACY_PDEV_NAME, -1, NULL, 0);
  108. if (IS_ERR(pdev)) {
  109. platform_driver_unregister(&pmu_legacy_driver);
  110. return PTR_ERR(pdev);
  111. }
  112. return ret;
  113. }
  114. late_initcall(riscv_pmu_legacy_devinit);
  115. void riscv_pmu_legacy_skip_init(void)
  116. {
  117. pmu_init_done = true;
  118. }