perf_event.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Xtensa Performance Monitor Module driver
  3. * See Tensilica Debug User's Guide for PMU registers documentation.
  4. *
  5. * Copyright (C) 2015 Cadence Design Systems Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/perf_event.h>
  16. #include <linux/platform_device.h>
  17. #include <asm/processor.h>
  18. #include <asm/stacktrace.h>
  19. /* Global control/status for all perf counters */
  20. #define XTENSA_PMU_PMG 0x1000
  21. /* Perf counter values */
  22. #define XTENSA_PMU_PM(i) (0x1080 + (i) * 4)
  23. /* Perf counter control registers */
  24. #define XTENSA_PMU_PMCTRL(i) (0x1100 + (i) * 4)
  25. /* Perf counter status registers */
  26. #define XTENSA_PMU_PMSTAT(i) (0x1180 + (i) * 4)
  27. #define XTENSA_PMU_PMG_PMEN 0x1
  28. #define XTENSA_PMU_COUNTER_MASK 0xffffffffULL
  29. #define XTENSA_PMU_COUNTER_MAX 0x7fffffff
  30. #define XTENSA_PMU_PMCTRL_INTEN 0x00000001
  31. #define XTENSA_PMU_PMCTRL_KRNLCNT 0x00000008
  32. #define XTENSA_PMU_PMCTRL_TRACELEVEL 0x000000f0
  33. #define XTENSA_PMU_PMCTRL_SELECT_SHIFT 8
  34. #define XTENSA_PMU_PMCTRL_SELECT 0x00001f00
  35. #define XTENSA_PMU_PMCTRL_MASK_SHIFT 16
  36. #define XTENSA_PMU_PMCTRL_MASK 0xffff0000
  37. #define XTENSA_PMU_MASK(select, mask) \
  38. (((select) << XTENSA_PMU_PMCTRL_SELECT_SHIFT) | \
  39. ((mask) << XTENSA_PMU_PMCTRL_MASK_SHIFT) | \
  40. XTENSA_PMU_PMCTRL_TRACELEVEL | \
  41. XTENSA_PMU_PMCTRL_INTEN)
  42. #define XTENSA_PMU_PMSTAT_OVFL 0x00000001
  43. #define XTENSA_PMU_PMSTAT_INTASRT 0x00000010
  44. struct xtensa_pmu_events {
  45. /* Array of events currently on this core */
  46. struct perf_event *event[XCHAL_NUM_PERF_COUNTERS];
  47. /* Bitmap of used hardware counters */
  48. unsigned long used_mask[BITS_TO_LONGS(XCHAL_NUM_PERF_COUNTERS)];
  49. };
  50. static DEFINE_PER_CPU(struct xtensa_pmu_events, xtensa_pmu_events);
  51. static const u32 xtensa_hw_ctl[] = {
  52. [PERF_COUNT_HW_CPU_CYCLES] = XTENSA_PMU_MASK(0, 0x1),
  53. [PERF_COUNT_HW_INSTRUCTIONS] = XTENSA_PMU_MASK(2, 0xffff),
  54. [PERF_COUNT_HW_CACHE_REFERENCES] = XTENSA_PMU_MASK(10, 0x1),
  55. [PERF_COUNT_HW_CACHE_MISSES] = XTENSA_PMU_MASK(12, 0x1),
  56. /* Taken and non-taken branches + taken loop ends */
  57. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = XTENSA_PMU_MASK(2, 0x490),
  58. /* Instruction-related + other global stall cycles */
  59. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = XTENSA_PMU_MASK(4, 0x1ff),
  60. /* Data-related global stall cycles */
  61. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = XTENSA_PMU_MASK(3, 0x1ff),
  62. };
  63. #define C(_x) PERF_COUNT_HW_CACHE_##_x
  64. static const u32 xtensa_cache_ctl[][C(OP_MAX)][C(RESULT_MAX)] = {
  65. [C(L1D)] = {
  66. [C(OP_READ)] = {
  67. [C(RESULT_ACCESS)] = XTENSA_PMU_MASK(10, 0x1),
  68. [C(RESULT_MISS)] = XTENSA_PMU_MASK(10, 0x2),
  69. },
  70. [C(OP_WRITE)] = {
  71. [C(RESULT_ACCESS)] = XTENSA_PMU_MASK(11, 0x1),
  72. [C(RESULT_MISS)] = XTENSA_PMU_MASK(11, 0x2),
  73. },
  74. },
  75. [C(L1I)] = {
  76. [C(OP_READ)] = {
  77. [C(RESULT_ACCESS)] = XTENSA_PMU_MASK(8, 0x1),
  78. [C(RESULT_MISS)] = XTENSA_PMU_MASK(8, 0x2),
  79. },
  80. },
  81. [C(DTLB)] = {
  82. [C(OP_READ)] = {
  83. [C(RESULT_ACCESS)] = XTENSA_PMU_MASK(9, 0x1),
  84. [C(RESULT_MISS)] = XTENSA_PMU_MASK(9, 0x8),
  85. },
  86. },
  87. [C(ITLB)] = {
  88. [C(OP_READ)] = {
  89. [C(RESULT_ACCESS)] = XTENSA_PMU_MASK(7, 0x1),
  90. [C(RESULT_MISS)] = XTENSA_PMU_MASK(7, 0x8),
  91. },
  92. },
  93. };
  94. static int xtensa_pmu_cache_event(u64 config)
  95. {
  96. unsigned int cache_type, cache_op, cache_result;
  97. int ret;
  98. cache_type = (config >> 0) & 0xff;
  99. cache_op = (config >> 8) & 0xff;
  100. cache_result = (config >> 16) & 0xff;
  101. if (cache_type >= ARRAY_SIZE(xtensa_cache_ctl) ||
  102. cache_op >= C(OP_MAX) ||
  103. cache_result >= C(RESULT_MAX))
  104. return -EINVAL;
  105. ret = xtensa_cache_ctl[cache_type][cache_op][cache_result];
  106. if (ret == 0)
  107. return -EINVAL;
  108. return ret;
  109. }
  110. static inline uint32_t xtensa_pmu_read_counter(int idx)
  111. {
  112. return get_er(XTENSA_PMU_PM(idx));
  113. }
  114. static inline void xtensa_pmu_write_counter(int idx, uint32_t v)
  115. {
  116. set_er(v, XTENSA_PMU_PM(idx));
  117. }
  118. static void xtensa_perf_event_update(struct perf_event *event,
  119. struct hw_perf_event *hwc, int idx)
  120. {
  121. uint64_t prev_raw_count, new_raw_count;
  122. int64_t delta;
  123. do {
  124. prev_raw_count = local64_read(&hwc->prev_count);
  125. new_raw_count = xtensa_pmu_read_counter(event->hw.idx);
  126. } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  127. new_raw_count) != prev_raw_count);
  128. delta = (new_raw_count - prev_raw_count) & XTENSA_PMU_COUNTER_MASK;
  129. local64_add(delta, &event->count);
  130. local64_sub(delta, &hwc->period_left);
  131. }
  132. static bool xtensa_perf_event_set_period(struct perf_event *event,
  133. struct hw_perf_event *hwc, int idx)
  134. {
  135. bool rc = false;
  136. s64 left;
  137. if (!is_sampling_event(event)) {
  138. left = XTENSA_PMU_COUNTER_MAX;
  139. } else {
  140. s64 period = hwc->sample_period;
  141. left = local64_read(&hwc->period_left);
  142. if (left <= -period) {
  143. left = period;
  144. local64_set(&hwc->period_left, left);
  145. hwc->last_period = period;
  146. rc = true;
  147. } else if (left <= 0) {
  148. left += period;
  149. local64_set(&hwc->period_left, left);
  150. hwc->last_period = period;
  151. rc = true;
  152. }
  153. if (left > XTENSA_PMU_COUNTER_MAX)
  154. left = XTENSA_PMU_COUNTER_MAX;
  155. }
  156. local64_set(&hwc->prev_count, -left);
  157. xtensa_pmu_write_counter(idx, -left);
  158. perf_event_update_userpage(event);
  159. return rc;
  160. }
  161. static void xtensa_pmu_enable(struct pmu *pmu)
  162. {
  163. set_er(get_er(XTENSA_PMU_PMG) | XTENSA_PMU_PMG_PMEN, XTENSA_PMU_PMG);
  164. }
  165. static void xtensa_pmu_disable(struct pmu *pmu)
  166. {
  167. set_er(get_er(XTENSA_PMU_PMG) & ~XTENSA_PMU_PMG_PMEN, XTENSA_PMU_PMG);
  168. }
  169. static int xtensa_pmu_event_init(struct perf_event *event)
  170. {
  171. int ret;
  172. switch (event->attr.type) {
  173. case PERF_TYPE_HARDWARE:
  174. if (event->attr.config >= ARRAY_SIZE(xtensa_hw_ctl) ||
  175. xtensa_hw_ctl[event->attr.config] == 0)
  176. return -EINVAL;
  177. event->hw.config = xtensa_hw_ctl[event->attr.config];
  178. return 0;
  179. case PERF_TYPE_HW_CACHE:
  180. ret = xtensa_pmu_cache_event(event->attr.config);
  181. if (ret < 0)
  182. return ret;
  183. event->hw.config = ret;
  184. return 0;
  185. case PERF_TYPE_RAW:
  186. /* Not 'previous counter' select */
  187. if ((event->attr.config & XTENSA_PMU_PMCTRL_SELECT) ==
  188. (1 << XTENSA_PMU_PMCTRL_SELECT_SHIFT))
  189. return -EINVAL;
  190. event->hw.config = (event->attr.config &
  191. (XTENSA_PMU_PMCTRL_KRNLCNT |
  192. XTENSA_PMU_PMCTRL_TRACELEVEL |
  193. XTENSA_PMU_PMCTRL_SELECT |
  194. XTENSA_PMU_PMCTRL_MASK)) |
  195. XTENSA_PMU_PMCTRL_INTEN;
  196. return 0;
  197. default:
  198. return -ENOENT;
  199. }
  200. }
  201. /*
  202. * Starts/Stops a counter present on the PMU. The PMI handler
  203. * should stop the counter when perf_event_overflow() returns
  204. * !0. ->start() will be used to continue.
  205. */
  206. static void xtensa_pmu_start(struct perf_event *event, int flags)
  207. {
  208. struct hw_perf_event *hwc = &event->hw;
  209. int idx = hwc->idx;
  210. if (WARN_ON_ONCE(idx == -1))
  211. return;
  212. if (flags & PERF_EF_RELOAD) {
  213. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  214. xtensa_perf_event_set_period(event, hwc, idx);
  215. }
  216. hwc->state = 0;
  217. set_er(hwc->config, XTENSA_PMU_PMCTRL(idx));
  218. }
  219. static void xtensa_pmu_stop(struct perf_event *event, int flags)
  220. {
  221. struct hw_perf_event *hwc = &event->hw;
  222. int idx = hwc->idx;
  223. if (!(hwc->state & PERF_HES_STOPPED)) {
  224. set_er(0, XTENSA_PMU_PMCTRL(idx));
  225. set_er(get_er(XTENSA_PMU_PMSTAT(idx)),
  226. XTENSA_PMU_PMSTAT(idx));
  227. hwc->state |= PERF_HES_STOPPED;
  228. }
  229. if ((flags & PERF_EF_UPDATE) &&
  230. !(event->hw.state & PERF_HES_UPTODATE)) {
  231. xtensa_perf_event_update(event, &event->hw, idx);
  232. event->hw.state |= PERF_HES_UPTODATE;
  233. }
  234. }
  235. /*
  236. * Adds/Removes a counter to/from the PMU, can be done inside
  237. * a transaction, see the ->*_txn() methods.
  238. */
  239. static int xtensa_pmu_add(struct perf_event *event, int flags)
  240. {
  241. struct xtensa_pmu_events *ev = this_cpu_ptr(&xtensa_pmu_events);
  242. struct hw_perf_event *hwc = &event->hw;
  243. int idx = hwc->idx;
  244. if (__test_and_set_bit(idx, ev->used_mask)) {
  245. idx = find_first_zero_bit(ev->used_mask,
  246. XCHAL_NUM_PERF_COUNTERS);
  247. if (idx == XCHAL_NUM_PERF_COUNTERS)
  248. return -EAGAIN;
  249. __set_bit(idx, ev->used_mask);
  250. hwc->idx = idx;
  251. }
  252. ev->event[idx] = event;
  253. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  254. if (flags & PERF_EF_START)
  255. xtensa_pmu_start(event, PERF_EF_RELOAD);
  256. perf_event_update_userpage(event);
  257. return 0;
  258. }
  259. static void xtensa_pmu_del(struct perf_event *event, int flags)
  260. {
  261. struct xtensa_pmu_events *ev = this_cpu_ptr(&xtensa_pmu_events);
  262. xtensa_pmu_stop(event, PERF_EF_UPDATE);
  263. __clear_bit(event->hw.idx, ev->used_mask);
  264. perf_event_update_userpage(event);
  265. }
  266. static void xtensa_pmu_read(struct perf_event *event)
  267. {
  268. xtensa_perf_event_update(event, &event->hw, event->hw.idx);
  269. }
  270. static int callchain_trace(struct stackframe *frame, void *data)
  271. {
  272. struct perf_callchain_entry_ctx *entry = data;
  273. perf_callchain_store(entry, frame->pc);
  274. return 0;
  275. }
  276. void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
  277. struct pt_regs *regs)
  278. {
  279. xtensa_backtrace_kernel(regs, entry->max_stack,
  280. callchain_trace, NULL, entry);
  281. }
  282. void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
  283. struct pt_regs *regs)
  284. {
  285. xtensa_backtrace_user(regs, entry->max_stack,
  286. callchain_trace, entry);
  287. }
  288. void perf_event_print_debug(void)
  289. {
  290. unsigned long flags;
  291. unsigned i;
  292. local_irq_save(flags);
  293. pr_info("CPU#%d: PMG: 0x%08lx\n", smp_processor_id(),
  294. get_er(XTENSA_PMU_PMG));
  295. for (i = 0; i < XCHAL_NUM_PERF_COUNTERS; ++i)
  296. pr_info("PM%d: 0x%08lx, PMCTRL%d: 0x%08lx, PMSTAT%d: 0x%08lx\n",
  297. i, get_er(XTENSA_PMU_PM(i)),
  298. i, get_er(XTENSA_PMU_PMCTRL(i)),
  299. i, get_er(XTENSA_PMU_PMSTAT(i)));
  300. local_irq_restore(flags);
  301. }
  302. irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id)
  303. {
  304. irqreturn_t rc = IRQ_NONE;
  305. struct xtensa_pmu_events *ev = this_cpu_ptr(&xtensa_pmu_events);
  306. unsigned i;
  307. for (i = find_first_bit(ev->used_mask, XCHAL_NUM_PERF_COUNTERS);
  308. i < XCHAL_NUM_PERF_COUNTERS;
  309. i = find_next_bit(ev->used_mask, XCHAL_NUM_PERF_COUNTERS, i + 1)) {
  310. uint32_t v = get_er(XTENSA_PMU_PMSTAT(i));
  311. struct perf_event *event = ev->event[i];
  312. struct hw_perf_event *hwc = &event->hw;
  313. u64 last_period;
  314. if (!(v & XTENSA_PMU_PMSTAT_OVFL))
  315. continue;
  316. set_er(v, XTENSA_PMU_PMSTAT(i));
  317. xtensa_perf_event_update(event, hwc, i);
  318. last_period = hwc->last_period;
  319. if (xtensa_perf_event_set_period(event, hwc, i)) {
  320. struct perf_sample_data data;
  321. struct pt_regs *regs = get_irq_regs();
  322. perf_sample_data_init(&data, 0, last_period);
  323. if (perf_event_overflow(event, &data, regs))
  324. xtensa_pmu_stop(event, 0);
  325. }
  326. rc = IRQ_HANDLED;
  327. }
  328. return rc;
  329. }
  330. static struct pmu xtensa_pmu = {
  331. .pmu_enable = xtensa_pmu_enable,
  332. .pmu_disable = xtensa_pmu_disable,
  333. .event_init = xtensa_pmu_event_init,
  334. .add = xtensa_pmu_add,
  335. .del = xtensa_pmu_del,
  336. .start = xtensa_pmu_start,
  337. .stop = xtensa_pmu_stop,
  338. .read = xtensa_pmu_read,
  339. };
  340. static int xtensa_pmu_setup(int cpu)
  341. {
  342. unsigned i;
  343. set_er(0, XTENSA_PMU_PMG);
  344. for (i = 0; i < XCHAL_NUM_PERF_COUNTERS; ++i) {
  345. set_er(0, XTENSA_PMU_PMCTRL(i));
  346. set_er(get_er(XTENSA_PMU_PMSTAT(i)), XTENSA_PMU_PMSTAT(i));
  347. }
  348. return 0;
  349. }
  350. static int __init xtensa_pmu_init(void)
  351. {
  352. int ret;
  353. int irq = irq_create_mapping(NULL, XCHAL_PROFILING_INTERRUPT);
  354. ret = cpuhp_setup_state(CPUHP_AP_PERF_XTENSA_STARTING,
  355. "AP_PERF_XTENSA_STARTING", xtensa_pmu_setup,
  356. NULL);
  357. if (ret) {
  358. pr_err("xtensa_pmu: failed to register CPU-hotplug.\n");
  359. return ret;
  360. }
  361. #if XTENSA_FAKE_NMI
  362. enable_irq(irq);
  363. #else
  364. ret = request_irq(irq, xtensa_pmu_irq_handler, IRQF_PERCPU,
  365. "pmu", NULL);
  366. if (ret < 0)
  367. return ret;
  368. #endif
  369. ret = perf_pmu_register(&xtensa_pmu, "cpu", PERF_TYPE_RAW);
  370. if (ret)
  371. free_irq(irq, NULL);
  372. return ret;
  373. }
  374. early_initcall(xtensa_pmu_init);