perf_event.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Linux performance counter support for ARC700 series
  3. *
  4. * Copyright (C) 2013-2015 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/interrupt.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/perf_event.h>
  18. #include <linux/platform_device.h>
  19. #include <asm/arcregs.h>
  20. #include <asm/stacktrace.h>
  21. struct arc_pmu {
  22. struct pmu pmu;
  23. unsigned int irq;
  24. int n_counters;
  25. u64 max_period;
  26. int ev_hw_idx[PERF_COUNT_ARC_HW_MAX];
  27. };
  28. struct arc_pmu_cpu {
  29. /*
  30. * A 1 bit for an index indicates that the counter is being used for
  31. * an event. A 0 means that the counter can be used.
  32. */
  33. unsigned long used_mask[BITS_TO_LONGS(ARC_PERF_MAX_COUNTERS)];
  34. /*
  35. * The events that are active on the PMU for the given index.
  36. */
  37. struct perf_event *act_counter[ARC_PERF_MAX_COUNTERS];
  38. };
  39. struct arc_callchain_trace {
  40. int depth;
  41. void *perf_stuff;
  42. };
  43. static int callchain_trace(unsigned int addr, void *data)
  44. {
  45. struct arc_callchain_trace *ctrl = data;
  46. struct perf_callchain_entry_ctx *entry = ctrl->perf_stuff;
  47. perf_callchain_store(entry, addr);
  48. if (ctrl->depth++ < 3)
  49. return 0;
  50. return -1;
  51. }
  52. void
  53. perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
  54. {
  55. struct arc_callchain_trace ctrl = {
  56. .depth = 0,
  57. .perf_stuff = entry,
  58. };
  59. arc_unwind_core(NULL, regs, callchain_trace, &ctrl);
  60. }
  61. void
  62. perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
  63. {
  64. /*
  65. * User stack can't be unwound trivially with kernel dwarf unwinder
  66. * So for now just record the user PC
  67. */
  68. perf_callchain_store(entry, instruction_pointer(regs));
  69. }
  70. static struct arc_pmu *arc_pmu;
  71. static DEFINE_PER_CPU(struct arc_pmu_cpu, arc_pmu_cpu);
  72. /* read counter #idx; note that counter# != event# on ARC! */
  73. static uint64_t arc_pmu_read_counter(int idx)
  74. {
  75. uint32_t tmp;
  76. uint64_t result;
  77. /*
  78. * ARC supports making 'snapshots' of the counters, so we don't
  79. * need to care about counters wrapping to 0 underneath our feet
  80. */
  81. write_aux_reg(ARC_REG_PCT_INDEX, idx);
  82. tmp = read_aux_reg(ARC_REG_PCT_CONTROL);
  83. write_aux_reg(ARC_REG_PCT_CONTROL, tmp | ARC_REG_PCT_CONTROL_SN);
  84. result = (uint64_t) (read_aux_reg(ARC_REG_PCT_SNAPH)) << 32;
  85. result |= read_aux_reg(ARC_REG_PCT_SNAPL);
  86. return result;
  87. }
  88. static void arc_perf_event_update(struct perf_event *event,
  89. struct hw_perf_event *hwc, int idx)
  90. {
  91. uint64_t prev_raw_count = local64_read(&hwc->prev_count);
  92. uint64_t new_raw_count = arc_pmu_read_counter(idx);
  93. int64_t delta = new_raw_count - prev_raw_count;
  94. /*
  95. * We aren't afraid of hwc->prev_count changing beneath our feet
  96. * because there's no way for us to re-enter this function anytime.
  97. */
  98. local64_set(&hwc->prev_count, new_raw_count);
  99. local64_add(delta, &event->count);
  100. local64_sub(delta, &hwc->period_left);
  101. }
  102. static void arc_pmu_read(struct perf_event *event)
  103. {
  104. arc_perf_event_update(event, &event->hw, event->hw.idx);
  105. }
  106. static int arc_pmu_cache_event(u64 config)
  107. {
  108. unsigned int cache_type, cache_op, cache_result;
  109. int ret;
  110. cache_type = (config >> 0) & 0xff;
  111. cache_op = (config >> 8) & 0xff;
  112. cache_result = (config >> 16) & 0xff;
  113. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  114. return -EINVAL;
  115. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  116. return -EINVAL;
  117. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  118. return -EINVAL;
  119. ret = arc_pmu_cache_map[cache_type][cache_op][cache_result];
  120. if (ret == CACHE_OP_UNSUPPORTED)
  121. return -ENOENT;
  122. pr_debug("init cache event: type/op/result %d/%d/%d with h/w %d \'%s\'\n",
  123. cache_type, cache_op, cache_result, ret,
  124. arc_pmu_ev_hw_map[ret]);
  125. return ret;
  126. }
  127. /* initializes hw_perf_event structure if event is supported */
  128. static int arc_pmu_event_init(struct perf_event *event)
  129. {
  130. struct hw_perf_event *hwc = &event->hw;
  131. int ret;
  132. if (!is_sampling_event(event)) {
  133. hwc->sample_period = arc_pmu->max_period;
  134. hwc->last_period = hwc->sample_period;
  135. local64_set(&hwc->period_left, hwc->sample_period);
  136. }
  137. hwc->config = 0;
  138. if (is_isa_arcv2()) {
  139. /* "exclude user" means "count only kernel" */
  140. if (event->attr.exclude_user)
  141. hwc->config |= ARC_REG_PCT_CONFIG_KERN;
  142. /* "exclude kernel" means "count only user" */
  143. if (event->attr.exclude_kernel)
  144. hwc->config |= ARC_REG_PCT_CONFIG_USER;
  145. }
  146. switch (event->attr.type) {
  147. case PERF_TYPE_HARDWARE:
  148. if (event->attr.config >= PERF_COUNT_HW_MAX)
  149. return -ENOENT;
  150. if (arc_pmu->ev_hw_idx[event->attr.config] < 0)
  151. return -ENOENT;
  152. hwc->config |= arc_pmu->ev_hw_idx[event->attr.config];
  153. pr_debug("init event %d with h/w %08x \'%s\'\n",
  154. (int)event->attr.config, (int)hwc->config,
  155. arc_pmu_ev_hw_map[event->attr.config]);
  156. return 0;
  157. case PERF_TYPE_HW_CACHE:
  158. ret = arc_pmu_cache_event(event->attr.config);
  159. if (ret < 0)
  160. return ret;
  161. hwc->config |= arc_pmu->ev_hw_idx[ret];
  162. pr_debug("init cache event with h/w %08x \'%s\'\n",
  163. (int)hwc->config, arc_pmu_ev_hw_map[ret]);
  164. return 0;
  165. default:
  166. return -ENOENT;
  167. }
  168. }
  169. /* starts all counters */
  170. static void arc_pmu_enable(struct pmu *pmu)
  171. {
  172. uint32_t tmp;
  173. tmp = read_aux_reg(ARC_REG_PCT_CONTROL);
  174. write_aux_reg(ARC_REG_PCT_CONTROL, (tmp & 0xffff0000) | 0x1);
  175. }
  176. /* stops all counters */
  177. static void arc_pmu_disable(struct pmu *pmu)
  178. {
  179. uint32_t tmp;
  180. tmp = read_aux_reg(ARC_REG_PCT_CONTROL);
  181. write_aux_reg(ARC_REG_PCT_CONTROL, (tmp & 0xffff0000) | 0x0);
  182. }
  183. static int arc_pmu_event_set_period(struct perf_event *event)
  184. {
  185. struct hw_perf_event *hwc = &event->hw;
  186. s64 left = local64_read(&hwc->period_left);
  187. s64 period = hwc->sample_period;
  188. int idx = hwc->idx;
  189. int overflow = 0;
  190. u64 value;
  191. if (unlikely(left <= -period)) {
  192. /* left underflowed by more than period. */
  193. left = period;
  194. local64_set(&hwc->period_left, left);
  195. hwc->last_period = period;
  196. overflow = 1;
  197. } else if (unlikely(left <= 0)) {
  198. /* left underflowed by less than period. */
  199. left += period;
  200. local64_set(&hwc->period_left, left);
  201. hwc->last_period = period;
  202. overflow = 1;
  203. }
  204. if (left > arc_pmu->max_period)
  205. left = arc_pmu->max_period;
  206. value = arc_pmu->max_period - left;
  207. local64_set(&hwc->prev_count, value);
  208. /* Select counter */
  209. write_aux_reg(ARC_REG_PCT_INDEX, idx);
  210. /* Write value */
  211. write_aux_reg(ARC_REG_PCT_COUNTL, (u32)value);
  212. write_aux_reg(ARC_REG_PCT_COUNTH, (value >> 32));
  213. perf_event_update_userpage(event);
  214. return overflow;
  215. }
  216. /*
  217. * Assigns hardware counter to hardware condition.
  218. * Note that there is no separate start/stop mechanism;
  219. * stopping is achieved by assigning the 'never' condition
  220. */
  221. static void arc_pmu_start(struct perf_event *event, int flags)
  222. {
  223. struct hw_perf_event *hwc = &event->hw;
  224. int idx = hwc->idx;
  225. if (WARN_ON_ONCE(idx == -1))
  226. return;
  227. if (flags & PERF_EF_RELOAD)
  228. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  229. hwc->state = 0;
  230. arc_pmu_event_set_period(event);
  231. /* Enable interrupt for this counter */
  232. if (is_sampling_event(event))
  233. write_aux_reg(ARC_REG_PCT_INT_CTRL,
  234. read_aux_reg(ARC_REG_PCT_INT_CTRL) | (1 << idx));
  235. /* enable ARC pmu here */
  236. write_aux_reg(ARC_REG_PCT_INDEX, idx); /* counter # */
  237. write_aux_reg(ARC_REG_PCT_CONFIG, hwc->config); /* condition */
  238. }
  239. static void arc_pmu_stop(struct perf_event *event, int flags)
  240. {
  241. struct hw_perf_event *hwc = &event->hw;
  242. int idx = hwc->idx;
  243. /* Disable interrupt for this counter */
  244. if (is_sampling_event(event)) {
  245. /*
  246. * Reset interrupt flag by writing of 1. This is required
  247. * to make sure pending interrupt was not left.
  248. */
  249. write_aux_reg(ARC_REG_PCT_INT_ACT, 1 << idx);
  250. write_aux_reg(ARC_REG_PCT_INT_CTRL,
  251. read_aux_reg(ARC_REG_PCT_INT_CTRL) & ~(1 << idx));
  252. }
  253. if (!(event->hw.state & PERF_HES_STOPPED)) {
  254. /* stop ARC pmu here */
  255. write_aux_reg(ARC_REG_PCT_INDEX, idx);
  256. /* condition code #0 is always "never" */
  257. write_aux_reg(ARC_REG_PCT_CONFIG, 0);
  258. event->hw.state |= PERF_HES_STOPPED;
  259. }
  260. if ((flags & PERF_EF_UPDATE) &&
  261. !(event->hw.state & PERF_HES_UPTODATE)) {
  262. arc_perf_event_update(event, &event->hw, idx);
  263. event->hw.state |= PERF_HES_UPTODATE;
  264. }
  265. }
  266. static void arc_pmu_del(struct perf_event *event, int flags)
  267. {
  268. struct arc_pmu_cpu *pmu_cpu = this_cpu_ptr(&arc_pmu_cpu);
  269. arc_pmu_stop(event, PERF_EF_UPDATE);
  270. __clear_bit(event->hw.idx, pmu_cpu->used_mask);
  271. pmu_cpu->act_counter[event->hw.idx] = 0;
  272. perf_event_update_userpage(event);
  273. }
  274. /* allocate hardware counter and optionally start counting */
  275. static int arc_pmu_add(struct perf_event *event, int flags)
  276. {
  277. struct arc_pmu_cpu *pmu_cpu = this_cpu_ptr(&arc_pmu_cpu);
  278. struct hw_perf_event *hwc = &event->hw;
  279. int idx = hwc->idx;
  280. if (__test_and_set_bit(idx, pmu_cpu->used_mask)) {
  281. idx = find_first_zero_bit(pmu_cpu->used_mask,
  282. arc_pmu->n_counters);
  283. if (idx == arc_pmu->n_counters)
  284. return -EAGAIN;
  285. __set_bit(idx, pmu_cpu->used_mask);
  286. hwc->idx = idx;
  287. }
  288. write_aux_reg(ARC_REG_PCT_INDEX, idx);
  289. pmu_cpu->act_counter[idx] = event;
  290. if (is_sampling_event(event)) {
  291. /* Mimic full counter overflow as other arches do */
  292. write_aux_reg(ARC_REG_PCT_INT_CNTL, (u32)arc_pmu->max_period);
  293. write_aux_reg(ARC_REG_PCT_INT_CNTH,
  294. (arc_pmu->max_period >> 32));
  295. }
  296. write_aux_reg(ARC_REG_PCT_CONFIG, 0);
  297. write_aux_reg(ARC_REG_PCT_COUNTL, 0);
  298. write_aux_reg(ARC_REG_PCT_COUNTH, 0);
  299. local64_set(&hwc->prev_count, 0);
  300. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  301. if (flags & PERF_EF_START)
  302. arc_pmu_start(event, PERF_EF_RELOAD);
  303. perf_event_update_userpage(event);
  304. return 0;
  305. }
  306. #ifdef CONFIG_ISA_ARCV2
  307. static irqreturn_t arc_pmu_intr(int irq, void *dev)
  308. {
  309. struct perf_sample_data data;
  310. struct arc_pmu_cpu *pmu_cpu = this_cpu_ptr(&arc_pmu_cpu);
  311. struct pt_regs *regs;
  312. int active_ints;
  313. int idx;
  314. arc_pmu_disable(&arc_pmu->pmu);
  315. active_ints = read_aux_reg(ARC_REG_PCT_INT_ACT);
  316. regs = get_irq_regs();
  317. for (idx = 0; idx < arc_pmu->n_counters; idx++) {
  318. struct perf_event *event = pmu_cpu->act_counter[idx];
  319. struct hw_perf_event *hwc;
  320. if (!(active_ints & (1 << idx)))
  321. continue;
  322. /* Reset interrupt flag by writing of 1 */
  323. write_aux_reg(ARC_REG_PCT_INT_ACT, 1 << idx);
  324. /*
  325. * On reset of "interrupt active" bit corresponding
  326. * "interrupt enable" bit gets automatically reset as well.
  327. * Now we need to re-enable interrupt for the counter.
  328. */
  329. write_aux_reg(ARC_REG_PCT_INT_CTRL,
  330. read_aux_reg(ARC_REG_PCT_INT_CTRL) | (1 << idx));
  331. hwc = &event->hw;
  332. WARN_ON_ONCE(hwc->idx != idx);
  333. arc_perf_event_update(event, &event->hw, event->hw.idx);
  334. perf_sample_data_init(&data, 0, hwc->last_period);
  335. if (!arc_pmu_event_set_period(event))
  336. continue;
  337. if (perf_event_overflow(event, &data, regs))
  338. arc_pmu_stop(event, 0);
  339. }
  340. arc_pmu_enable(&arc_pmu->pmu);
  341. return IRQ_HANDLED;
  342. }
  343. #else
  344. static irqreturn_t arc_pmu_intr(int irq, void *dev)
  345. {
  346. return IRQ_NONE;
  347. }
  348. #endif /* CONFIG_ISA_ARCV2 */
  349. static void arc_cpu_pmu_irq_init(void *data)
  350. {
  351. int irq = *(int *)data;
  352. enable_percpu_irq(irq, IRQ_TYPE_NONE);
  353. /* Clear all pending interrupt flags */
  354. write_aux_reg(ARC_REG_PCT_INT_ACT, 0xffffffff);
  355. }
  356. static int arc_pmu_device_probe(struct platform_device *pdev)
  357. {
  358. struct arc_reg_pct_build pct_bcr;
  359. struct arc_reg_cc_build cc_bcr;
  360. int i, j, has_interrupts;
  361. int counter_size; /* in bits */
  362. union cc_name {
  363. struct {
  364. uint32_t word0, word1;
  365. char sentinel;
  366. } indiv;
  367. char str[9];
  368. } cc_name;
  369. READ_BCR(ARC_REG_PCT_BUILD, pct_bcr);
  370. if (!pct_bcr.v) {
  371. pr_err("This core does not have performance counters!\n");
  372. return -ENODEV;
  373. }
  374. BUG_ON(pct_bcr.c > ARC_PERF_MAX_COUNTERS);
  375. READ_BCR(ARC_REG_CC_BUILD, cc_bcr);
  376. BUG_ON(!cc_bcr.v); /* Counters exist but No countable conditions ? */
  377. arc_pmu = devm_kzalloc(&pdev->dev, sizeof(struct arc_pmu), GFP_KERNEL);
  378. if (!arc_pmu)
  379. return -ENOMEM;
  380. has_interrupts = is_isa_arcv2() ? pct_bcr.i : 0;
  381. arc_pmu->n_counters = pct_bcr.c;
  382. counter_size = 32 + (pct_bcr.s << 4);
  383. arc_pmu->max_period = (1ULL << counter_size) / 2 - 1ULL;
  384. pr_info("ARC perf\t: %d counters (%d bits), %d conditions%s\n",
  385. arc_pmu->n_counters, counter_size, cc_bcr.c,
  386. has_interrupts ? ", [overflow IRQ support]":"");
  387. cc_name.str[8] = 0;
  388. for (i = 0; i < PERF_COUNT_ARC_HW_MAX; i++)
  389. arc_pmu->ev_hw_idx[i] = -1;
  390. /* loop thru all available h/w condition indexes */
  391. for (j = 0; j < cc_bcr.c; j++) {
  392. write_aux_reg(ARC_REG_CC_INDEX, j);
  393. cc_name.indiv.word0 = read_aux_reg(ARC_REG_CC_NAME0);
  394. cc_name.indiv.word1 = read_aux_reg(ARC_REG_CC_NAME1);
  395. /* See if it has been mapped to a perf event_id */
  396. for (i = 0; i < ARRAY_SIZE(arc_pmu_ev_hw_map); i++) {
  397. if (arc_pmu_ev_hw_map[i] &&
  398. !strcmp(arc_pmu_ev_hw_map[i], cc_name.str) &&
  399. strlen(arc_pmu_ev_hw_map[i])) {
  400. pr_debug("mapping perf event %2d to h/w event \'%8s\' (idx %d)\n",
  401. i, cc_name.str, j);
  402. arc_pmu->ev_hw_idx[i] = j;
  403. }
  404. }
  405. }
  406. arc_pmu->pmu = (struct pmu) {
  407. .pmu_enable = arc_pmu_enable,
  408. .pmu_disable = arc_pmu_disable,
  409. .event_init = arc_pmu_event_init,
  410. .add = arc_pmu_add,
  411. .del = arc_pmu_del,
  412. .start = arc_pmu_start,
  413. .stop = arc_pmu_stop,
  414. .read = arc_pmu_read,
  415. };
  416. if (has_interrupts) {
  417. int irq = platform_get_irq(pdev, 0);
  418. if (irq < 0) {
  419. pr_err("Cannot get IRQ number for the platform\n");
  420. return -ENODEV;
  421. }
  422. arc_pmu->irq = irq;
  423. /* intc map function ensures irq_set_percpu_devid() called */
  424. request_percpu_irq(irq, arc_pmu_intr, "ARC perf counters",
  425. this_cpu_ptr(&arc_pmu_cpu));
  426. on_each_cpu(arc_cpu_pmu_irq_init, &irq, 1);
  427. } else
  428. arc_pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  429. return perf_pmu_register(&arc_pmu->pmu, pdev->name, PERF_TYPE_RAW);
  430. }
  431. #ifdef CONFIG_OF
  432. static const struct of_device_id arc_pmu_match[] = {
  433. { .compatible = "snps,arc700-pct" },
  434. { .compatible = "snps,archs-pct" },
  435. {},
  436. };
  437. MODULE_DEVICE_TABLE(of, arc_pmu_match);
  438. #endif
  439. static struct platform_driver arc_pmu_driver = {
  440. .driver = {
  441. .name = "arc-pct",
  442. .of_match_table = of_match_ptr(arc_pmu_match),
  443. },
  444. .probe = arc_pmu_device_probe,
  445. };
  446. module_platform_driver(arc_pmu_driver);
  447. MODULE_LICENSE("GPL");
  448. MODULE_AUTHOR("Mischa Jonker <mjonker@synopsys.com>");
  449. MODULE_DESCRIPTION("ARC PMU driver");