arm-spe.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Arm Statistical Profiling Extensions (SPE) support
  4. * Copyright (c) 2017-2018, Arm Ltd.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/types.h>
  8. #include <linux/bitops.h>
  9. #include <linux/log2.h>
  10. #include <time.h>
  11. #include "../../util/cpumap.h"
  12. #include "../../util/evsel.h"
  13. #include "../../util/evlist.h"
  14. #include "../../util/session.h"
  15. #include "../../util/util.h"
  16. #include "../../util/pmu.h"
  17. #include "../../util/debug.h"
  18. #include "../../util/auxtrace.h"
  19. #include "../../util/arm-spe.h"
  20. #define KiB(x) ((x) * 1024)
  21. #define MiB(x) ((x) * 1024 * 1024)
  22. struct arm_spe_recording {
  23. struct auxtrace_record itr;
  24. struct perf_pmu *arm_spe_pmu;
  25. struct perf_evlist *evlist;
  26. };
  27. static size_t
  28. arm_spe_info_priv_size(struct auxtrace_record *itr __maybe_unused,
  29. struct perf_evlist *evlist __maybe_unused)
  30. {
  31. return ARM_SPE_AUXTRACE_PRIV_SIZE;
  32. }
  33. static int arm_spe_info_fill(struct auxtrace_record *itr,
  34. struct perf_session *session,
  35. struct auxtrace_info_event *auxtrace_info,
  36. size_t priv_size)
  37. {
  38. struct arm_spe_recording *sper =
  39. container_of(itr, struct arm_spe_recording, itr);
  40. struct perf_pmu *arm_spe_pmu = sper->arm_spe_pmu;
  41. if (priv_size != ARM_SPE_AUXTRACE_PRIV_SIZE)
  42. return -EINVAL;
  43. if (!session->evlist->nr_mmaps)
  44. return -EINVAL;
  45. auxtrace_info->type = PERF_AUXTRACE_ARM_SPE;
  46. auxtrace_info->priv[ARM_SPE_PMU_TYPE] = arm_spe_pmu->type;
  47. return 0;
  48. }
  49. static int arm_spe_recording_options(struct auxtrace_record *itr,
  50. struct perf_evlist *evlist,
  51. struct record_opts *opts)
  52. {
  53. struct arm_spe_recording *sper =
  54. container_of(itr, struct arm_spe_recording, itr);
  55. struct perf_pmu *arm_spe_pmu = sper->arm_spe_pmu;
  56. struct perf_evsel *evsel, *arm_spe_evsel = NULL;
  57. bool privileged = geteuid() == 0 || perf_event_paranoid() < 0;
  58. struct perf_evsel *tracking_evsel;
  59. int err;
  60. sper->evlist = evlist;
  61. evlist__for_each_entry(evlist, evsel) {
  62. if (evsel->attr.type == arm_spe_pmu->type) {
  63. if (arm_spe_evsel) {
  64. pr_err("There may be only one " ARM_SPE_PMU_NAME "x event\n");
  65. return -EINVAL;
  66. }
  67. evsel->attr.freq = 0;
  68. evsel->attr.sample_period = 1;
  69. arm_spe_evsel = evsel;
  70. opts->full_auxtrace = true;
  71. }
  72. }
  73. if (!opts->full_auxtrace)
  74. return 0;
  75. /* We are in full trace mode but '-m,xyz' wasn't specified */
  76. if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
  77. if (privileged) {
  78. opts->auxtrace_mmap_pages = MiB(4) / page_size;
  79. } else {
  80. opts->auxtrace_mmap_pages = KiB(128) / page_size;
  81. if (opts->mmap_pages == UINT_MAX)
  82. opts->mmap_pages = KiB(256) / page_size;
  83. }
  84. }
  85. /* Validate auxtrace_mmap_pages */
  86. if (opts->auxtrace_mmap_pages) {
  87. size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
  88. size_t min_sz = KiB(8);
  89. if (sz < min_sz || !is_power_of_2(sz)) {
  90. pr_err("Invalid mmap size for ARM SPE: must be at least %zuKiB and a power of 2\n",
  91. min_sz / 1024);
  92. return -EINVAL;
  93. }
  94. }
  95. /*
  96. * To obtain the auxtrace buffer file descriptor, the auxtrace event
  97. * must come first.
  98. */
  99. perf_evlist__to_front(evlist, arm_spe_evsel);
  100. perf_evsel__set_sample_bit(arm_spe_evsel, CPU);
  101. perf_evsel__set_sample_bit(arm_spe_evsel, TIME);
  102. perf_evsel__set_sample_bit(arm_spe_evsel, TID);
  103. /* Add dummy event to keep tracking */
  104. err = parse_events(evlist, "dummy:u", NULL);
  105. if (err)
  106. return err;
  107. tracking_evsel = perf_evlist__last(evlist);
  108. perf_evlist__set_tracking_event(evlist, tracking_evsel);
  109. tracking_evsel->attr.freq = 0;
  110. tracking_evsel->attr.sample_period = 1;
  111. perf_evsel__set_sample_bit(tracking_evsel, TIME);
  112. perf_evsel__set_sample_bit(tracking_evsel, CPU);
  113. perf_evsel__reset_sample_bit(tracking_evsel, BRANCH_STACK);
  114. return 0;
  115. }
  116. static u64 arm_spe_reference(struct auxtrace_record *itr __maybe_unused)
  117. {
  118. struct timespec ts;
  119. clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
  120. return ts.tv_sec ^ ts.tv_nsec;
  121. }
  122. static void arm_spe_recording_free(struct auxtrace_record *itr)
  123. {
  124. struct arm_spe_recording *sper =
  125. container_of(itr, struct arm_spe_recording, itr);
  126. free(sper);
  127. }
  128. static int arm_spe_read_finish(struct auxtrace_record *itr, int idx)
  129. {
  130. struct arm_spe_recording *sper =
  131. container_of(itr, struct arm_spe_recording, itr);
  132. struct perf_evsel *evsel;
  133. evlist__for_each_entry(sper->evlist, evsel) {
  134. if (evsel->attr.type == sper->arm_spe_pmu->type)
  135. return perf_evlist__enable_event_idx(sper->evlist,
  136. evsel, idx);
  137. }
  138. return -EINVAL;
  139. }
  140. struct auxtrace_record *arm_spe_recording_init(int *err,
  141. struct perf_pmu *arm_spe_pmu)
  142. {
  143. struct arm_spe_recording *sper;
  144. if (!arm_spe_pmu) {
  145. *err = -ENODEV;
  146. return NULL;
  147. }
  148. sper = zalloc(sizeof(struct arm_spe_recording));
  149. if (!sper) {
  150. *err = -ENOMEM;
  151. return NULL;
  152. }
  153. sper->arm_spe_pmu = arm_spe_pmu;
  154. sper->itr.recording_options = arm_spe_recording_options;
  155. sper->itr.info_priv_size = arm_spe_info_priv_size;
  156. sper->itr.info_fill = arm_spe_info_fill;
  157. sper->itr.free = arm_spe_recording_free;
  158. sper->itr.reference = arm_spe_reference;
  159. sper->itr.read_finish = arm_spe_read_finish;
  160. sper->itr.alignment = 0;
  161. *err = 0;
  162. return &sper->itr;
  163. }
  164. struct perf_event_attr
  165. *arm_spe_pmu_default_config(struct perf_pmu *arm_spe_pmu)
  166. {
  167. struct perf_event_attr *attr;
  168. attr = zalloc(sizeof(struct perf_event_attr));
  169. if (!attr) {
  170. pr_err("arm_spe default config cannot allocate a perf_event_attr\n");
  171. return NULL;
  172. }
  173. /*
  174. * If kernel driver doesn't advertise a minimum,
  175. * use max allowable by PMSIDR_EL1.INTERVAL
  176. */
  177. if (perf_pmu__scan_file(arm_spe_pmu, "caps/min_interval", "%llu",
  178. &attr->sample_period) != 1) {
  179. pr_debug("arm_spe driver doesn't advertise a min. interval. Using 4096\n");
  180. attr->sample_period = 4096;
  181. }
  182. arm_spe_pmu->selectable = true;
  183. arm_spe_pmu->is_uncore = false;
  184. return attr;
  185. }