perf_event.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Linux performance counter support for ARC700 series
  3. *
  4. * Copyright (C) 2013 Synopsys, Inc. (www.synopsys.com)
  5. *
  6. * This code is inspired by the perf support of various other architectures.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/errno.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/perf_event.h>
  17. #include <linux/platform_device.h>
  18. #include <asm/arcregs.h>
  19. #include <asm/stacktrace.h>
  20. struct arc_pmu {
  21. struct pmu pmu;
  22. int counter_size; /* in bits */
  23. int n_counters;
  24. unsigned long used_mask[BITS_TO_LONGS(ARC_PMU_MAX_HWEVENTS)];
  25. int ev_hw_idx[PERF_COUNT_ARC_HW_MAX];
  26. };
  27. struct arc_callchain_trace {
  28. int depth;
  29. void *perf_stuff;
  30. };
  31. static int callchain_trace(unsigned int addr, void *data)
  32. {
  33. struct arc_callchain_trace *ctrl = data;
  34. struct perf_callchain_entry *entry = ctrl->perf_stuff;
  35. perf_callchain_store(entry, addr);
  36. if (ctrl->depth++ < 3)
  37. return 0;
  38. return -1;
  39. }
  40. void
  41. perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
  42. {
  43. struct arc_callchain_trace ctrl = {
  44. .depth = 0,
  45. .perf_stuff = entry,
  46. };
  47. arc_unwind_core(NULL, regs, callchain_trace, &ctrl);
  48. }
  49. void
  50. perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
  51. {
  52. /*
  53. * User stack can't be unwound trivially with kernel dwarf unwinder
  54. * So for now just record the user PC
  55. */
  56. perf_callchain_store(entry, instruction_pointer(regs));
  57. }
  58. static struct arc_pmu *arc_pmu;
  59. /* read counter #idx; note that counter# != event# on ARC! */
  60. static uint64_t arc_pmu_read_counter(int idx)
  61. {
  62. uint32_t tmp;
  63. uint64_t result;
  64. /*
  65. * ARC supports making 'snapshots' of the counters, so we don't
  66. * need to care about counters wrapping to 0 underneath our feet
  67. */
  68. write_aux_reg(ARC_REG_PCT_INDEX, idx);
  69. tmp = read_aux_reg(ARC_REG_PCT_CONTROL);
  70. write_aux_reg(ARC_REG_PCT_CONTROL, tmp | ARC_REG_PCT_CONTROL_SN);
  71. result = (uint64_t) (read_aux_reg(ARC_REG_PCT_SNAPH)) << 32;
  72. result |= read_aux_reg(ARC_REG_PCT_SNAPL);
  73. return result;
  74. }
  75. static void arc_perf_event_update(struct perf_event *event,
  76. struct hw_perf_event *hwc, int idx)
  77. {
  78. uint64_t prev_raw_count, new_raw_count;
  79. int64_t delta;
  80. do {
  81. prev_raw_count = local64_read(&hwc->prev_count);
  82. new_raw_count = arc_pmu_read_counter(idx);
  83. } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  84. new_raw_count) != prev_raw_count);
  85. delta = (new_raw_count - prev_raw_count) &
  86. ((1ULL << arc_pmu->counter_size) - 1ULL);
  87. local64_add(delta, &event->count);
  88. local64_sub(delta, &hwc->period_left);
  89. }
  90. static void arc_pmu_read(struct perf_event *event)
  91. {
  92. arc_perf_event_update(event, &event->hw, event->hw.idx);
  93. }
  94. static int arc_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 >= PERF_COUNT_HW_CACHE_MAX)
  102. return -EINVAL;
  103. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  104. return -EINVAL;
  105. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  106. return -EINVAL;
  107. ret = arc_pmu_cache_map[cache_type][cache_op][cache_result];
  108. if (ret == CACHE_OP_UNSUPPORTED)
  109. return -ENOENT;
  110. pr_debug("init cache event: type/op/result %d/%d/%d with h/w %d \'%s\'\n",
  111. cache_type, cache_op, cache_result, ret,
  112. arc_pmu_ev_hw_map[ret]);
  113. return ret;
  114. }
  115. /* initializes hw_perf_event structure if event is supported */
  116. static int arc_pmu_event_init(struct perf_event *event)
  117. {
  118. struct hw_perf_event *hwc = &event->hw;
  119. int ret;
  120. switch (event->attr.type) {
  121. case PERF_TYPE_HARDWARE:
  122. if (event->attr.config >= PERF_COUNT_HW_MAX)
  123. return -ENOENT;
  124. if (arc_pmu->ev_hw_idx[event->attr.config] < 0)
  125. return -ENOENT;
  126. hwc->config = arc_pmu->ev_hw_idx[event->attr.config];
  127. pr_debug("init event %d with h/w %d \'%s\'\n",
  128. (int) event->attr.config, (int) hwc->config,
  129. arc_pmu_ev_hw_map[event->attr.config]);
  130. return 0;
  131. case PERF_TYPE_HW_CACHE:
  132. ret = arc_pmu_cache_event(event->attr.config);
  133. if (ret < 0)
  134. return ret;
  135. hwc->config = arc_pmu->ev_hw_idx[ret];
  136. return 0;
  137. default:
  138. return -ENOENT;
  139. }
  140. }
  141. /* starts all counters */
  142. static void arc_pmu_enable(struct pmu *pmu)
  143. {
  144. uint32_t tmp;
  145. tmp = read_aux_reg(ARC_REG_PCT_CONTROL);
  146. write_aux_reg(ARC_REG_PCT_CONTROL, (tmp & 0xffff0000) | 0x1);
  147. }
  148. /* stops all counters */
  149. static void arc_pmu_disable(struct pmu *pmu)
  150. {
  151. uint32_t tmp;
  152. tmp = read_aux_reg(ARC_REG_PCT_CONTROL);
  153. write_aux_reg(ARC_REG_PCT_CONTROL, (tmp & 0xffff0000) | 0x0);
  154. }
  155. /*
  156. * Assigns hardware counter to hardware condition.
  157. * Note that there is no separate start/stop mechanism;
  158. * stopping is achieved by assigning the 'never' condition
  159. */
  160. static void arc_pmu_start(struct perf_event *event, int flags)
  161. {
  162. struct hw_perf_event *hwc = &event->hw;
  163. int idx = hwc->idx;
  164. if (WARN_ON_ONCE(idx == -1))
  165. return;
  166. if (flags & PERF_EF_RELOAD)
  167. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  168. event->hw.state = 0;
  169. /* enable ARC pmu here */
  170. write_aux_reg(ARC_REG_PCT_INDEX, idx);
  171. write_aux_reg(ARC_REG_PCT_CONFIG, hwc->config);
  172. }
  173. static void arc_pmu_stop(struct perf_event *event, int flags)
  174. {
  175. struct hw_perf_event *hwc = &event->hw;
  176. int idx = hwc->idx;
  177. if (!(event->hw.state & PERF_HES_STOPPED)) {
  178. /* stop ARC pmu here */
  179. write_aux_reg(ARC_REG_PCT_INDEX, idx);
  180. /* condition code #0 is always "never" */
  181. write_aux_reg(ARC_REG_PCT_CONFIG, 0);
  182. event->hw.state |= PERF_HES_STOPPED;
  183. }
  184. if ((flags & PERF_EF_UPDATE) &&
  185. !(event->hw.state & PERF_HES_UPTODATE)) {
  186. arc_perf_event_update(event, &event->hw, idx);
  187. event->hw.state |= PERF_HES_UPTODATE;
  188. }
  189. }
  190. static void arc_pmu_del(struct perf_event *event, int flags)
  191. {
  192. arc_pmu_stop(event, PERF_EF_UPDATE);
  193. __clear_bit(event->hw.idx, arc_pmu->used_mask);
  194. perf_event_update_userpage(event);
  195. }
  196. /* allocate hardware counter and optionally start counting */
  197. static int arc_pmu_add(struct perf_event *event, int flags)
  198. {
  199. struct hw_perf_event *hwc = &event->hw;
  200. int idx = hwc->idx;
  201. if (__test_and_set_bit(idx, arc_pmu->used_mask)) {
  202. idx = find_first_zero_bit(arc_pmu->used_mask,
  203. arc_pmu->n_counters);
  204. if (idx == arc_pmu->n_counters)
  205. return -EAGAIN;
  206. __set_bit(idx, arc_pmu->used_mask);
  207. hwc->idx = idx;
  208. }
  209. write_aux_reg(ARC_REG_PCT_INDEX, idx);
  210. write_aux_reg(ARC_REG_PCT_CONFIG, 0);
  211. write_aux_reg(ARC_REG_PCT_COUNTL, 0);
  212. write_aux_reg(ARC_REG_PCT_COUNTH, 0);
  213. local64_set(&hwc->prev_count, 0);
  214. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  215. if (flags & PERF_EF_START)
  216. arc_pmu_start(event, PERF_EF_RELOAD);
  217. perf_event_update_userpage(event);
  218. return 0;
  219. }
  220. static int arc_pmu_device_probe(struct platform_device *pdev)
  221. {
  222. struct arc_reg_pct_build pct_bcr;
  223. struct arc_reg_cc_build cc_bcr;
  224. int i, j;
  225. union cc_name {
  226. struct {
  227. uint32_t word0, word1;
  228. char sentinel;
  229. } indiv;
  230. char str[9];
  231. } cc_name;
  232. READ_BCR(ARC_REG_PCT_BUILD, pct_bcr);
  233. if (!pct_bcr.v) {
  234. pr_err("This core does not have performance counters!\n");
  235. return -ENODEV;
  236. }
  237. BUG_ON(pct_bcr.c > ARC_PMU_MAX_HWEVENTS);
  238. READ_BCR(ARC_REG_CC_BUILD, cc_bcr);
  239. BUG_ON(!cc_bcr.v); /* Counters exist but No countable conditions ? */
  240. arc_pmu = devm_kzalloc(&pdev->dev, sizeof(struct arc_pmu), GFP_KERNEL);
  241. if (!arc_pmu)
  242. return -ENOMEM;
  243. arc_pmu->n_counters = pct_bcr.c;
  244. arc_pmu->counter_size = 32 + (pct_bcr.s << 4);
  245. pr_info("ARC perf\t: %d counters (%d bits), %d countable conditions\n",
  246. arc_pmu->n_counters, arc_pmu->counter_size, cc_bcr.c);
  247. cc_name.str[8] = 0;
  248. for (i = 0; i < PERF_COUNT_ARC_HW_MAX; i++)
  249. arc_pmu->ev_hw_idx[i] = -1;
  250. /* loop thru all available h/w condition indexes */
  251. for (j = 0; j < cc_bcr.c; j++) {
  252. write_aux_reg(ARC_REG_CC_INDEX, j);
  253. cc_name.indiv.word0 = read_aux_reg(ARC_REG_CC_NAME0);
  254. cc_name.indiv.word1 = read_aux_reg(ARC_REG_CC_NAME1);
  255. /* See if it has been mapped to a perf event_id */
  256. for (i = 0; i < ARRAY_SIZE(arc_pmu_ev_hw_map); i++) {
  257. if (arc_pmu_ev_hw_map[i] &&
  258. !strcmp(arc_pmu_ev_hw_map[i], cc_name.str) &&
  259. strlen(arc_pmu_ev_hw_map[i])) {
  260. pr_debug("mapping perf event %2d to h/w event \'%8s\' (idx %d)\n",
  261. i, cc_name.str, j);
  262. arc_pmu->ev_hw_idx[i] = j;
  263. }
  264. }
  265. }
  266. arc_pmu->pmu = (struct pmu) {
  267. .pmu_enable = arc_pmu_enable,
  268. .pmu_disable = arc_pmu_disable,
  269. .event_init = arc_pmu_event_init,
  270. .add = arc_pmu_add,
  271. .del = arc_pmu_del,
  272. .start = arc_pmu_start,
  273. .stop = arc_pmu_stop,
  274. .read = arc_pmu_read,
  275. };
  276. /* ARC 700 PMU does not support sampling events */
  277. arc_pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  278. return perf_pmu_register(&arc_pmu->pmu, pdev->name, PERF_TYPE_RAW);
  279. }
  280. #ifdef CONFIG_OF
  281. static const struct of_device_id arc_pmu_match[] = {
  282. { .compatible = "snps,arc700-pct" },
  283. {},
  284. };
  285. MODULE_DEVICE_TABLE(of, arc_pmu_match);
  286. #endif
  287. static struct platform_driver arc_pmu_driver = {
  288. .driver = {
  289. .name = "arc700-pct",
  290. .of_match_table = of_match_ptr(arc_pmu_match),
  291. },
  292. .probe = arc_pmu_device_probe,
  293. };
  294. module_platform_driver(arc_pmu_driver);
  295. MODULE_LICENSE("GPL");
  296. MODULE_AUTHOR("Mischa Jonker <mjonker@synopsys.com>");
  297. MODULE_DESCRIPTION("ARC PMU driver");