perf_event.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 *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 *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 *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 don't afaraid 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 %d \'%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. return 0;
  163. default:
  164. return -ENOENT;
  165. }
  166. }
  167. /* starts all counters */
  168. static void arc_pmu_enable(struct pmu *pmu)
  169. {
  170. uint32_t tmp;
  171. tmp = read_aux_reg(ARC_REG_PCT_CONTROL);
  172. write_aux_reg(ARC_REG_PCT_CONTROL, (tmp & 0xffff0000) | 0x1);
  173. }
  174. /* stops all counters */
  175. static void arc_pmu_disable(struct pmu *pmu)
  176. {
  177. uint32_t tmp;
  178. tmp = read_aux_reg(ARC_REG_PCT_CONTROL);
  179. write_aux_reg(ARC_REG_PCT_CONTROL, (tmp & 0xffff0000) | 0x0);
  180. }
  181. static int arc_pmu_event_set_period(struct perf_event *event)
  182. {
  183. struct hw_perf_event *hwc = &event->hw;
  184. s64 left = local64_read(&hwc->period_left);
  185. s64 period = hwc->sample_period;
  186. int idx = hwc->idx;
  187. int overflow = 0;
  188. u64 value;
  189. if (unlikely(left <= -period)) {
  190. /* left underflowed by more than period. */
  191. left = period;
  192. local64_set(&hwc->period_left, left);
  193. hwc->last_period = period;
  194. overflow = 1;
  195. } else if (unlikely(left <= 0)) {
  196. /* left underflowed by less than period. */
  197. left += period;
  198. local64_set(&hwc->period_left, left);
  199. hwc->last_period = period;
  200. overflow = 1;
  201. }
  202. if (left > arc_pmu->max_period)
  203. left = arc_pmu->max_period;
  204. value = arc_pmu->max_period - left;
  205. local64_set(&hwc->prev_count, value);
  206. /* Select counter */
  207. write_aux_reg(ARC_REG_PCT_INDEX, idx);
  208. /* Write value */
  209. write_aux_reg(ARC_REG_PCT_COUNTL, (u32)value);
  210. write_aux_reg(ARC_REG_PCT_COUNTH, (value >> 32));
  211. perf_event_update_userpage(event);
  212. return overflow;
  213. }
  214. /*
  215. * Assigns hardware counter to hardware condition.
  216. * Note that there is no separate start/stop mechanism;
  217. * stopping is achieved by assigning the 'never' condition
  218. */
  219. static void arc_pmu_start(struct perf_event *event, int flags)
  220. {
  221. struct hw_perf_event *hwc = &event->hw;
  222. int idx = hwc->idx;
  223. if (WARN_ON_ONCE(idx == -1))
  224. return;
  225. if (flags & PERF_EF_RELOAD)
  226. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  227. hwc->state = 0;
  228. arc_pmu_event_set_period(event);
  229. /* Enable interrupt for this counter */
  230. if (is_sampling_event(event))
  231. write_aux_reg(ARC_REG_PCT_INT_CTRL,
  232. read_aux_reg(ARC_REG_PCT_INT_CTRL) | (1 << idx));
  233. /* enable ARC pmu here */
  234. write_aux_reg(ARC_REG_PCT_INDEX, idx); /* counter # */
  235. write_aux_reg(ARC_REG_PCT_CONFIG, hwc->config); /* condition */
  236. }
  237. static void arc_pmu_stop(struct perf_event *event, int flags)
  238. {
  239. struct hw_perf_event *hwc = &event->hw;
  240. int idx = hwc->idx;
  241. /* Disable interrupt for this counter */
  242. if (is_sampling_event(event)) {
  243. /*
  244. * Reset interrupt flag by writing of 1. This is required
  245. * to make sure pending interrupt was not left.
  246. */
  247. write_aux_reg(ARC_REG_PCT_INT_ACT, 1 << idx);
  248. write_aux_reg(ARC_REG_PCT_INT_CTRL,
  249. read_aux_reg(ARC_REG_PCT_INT_CTRL) & ~(1 << idx));
  250. }
  251. if (!(event->hw.state & PERF_HES_STOPPED)) {
  252. /* stop ARC pmu here */
  253. write_aux_reg(ARC_REG_PCT_INDEX, idx);
  254. /* condition code #0 is always "never" */
  255. write_aux_reg(ARC_REG_PCT_CONFIG, 0);
  256. event->hw.state |= PERF_HES_STOPPED;
  257. }
  258. if ((flags & PERF_EF_UPDATE) &&
  259. !(event->hw.state & PERF_HES_UPTODATE)) {
  260. arc_perf_event_update(event, &event->hw, idx);
  261. event->hw.state |= PERF_HES_UPTODATE;
  262. }
  263. }
  264. static void arc_pmu_del(struct perf_event *event, int flags)
  265. {
  266. struct arc_pmu_cpu *pmu_cpu = this_cpu_ptr(&arc_pmu_cpu);
  267. arc_pmu_stop(event, PERF_EF_UPDATE);
  268. __clear_bit(event->hw.idx, pmu_cpu->used_mask);
  269. pmu_cpu->act_counter[event->hw.idx] = 0;
  270. perf_event_update_userpage(event);
  271. }
  272. /* allocate hardware counter and optionally start counting */
  273. static int arc_pmu_add(struct perf_event *event, int flags)
  274. {
  275. struct arc_pmu_cpu *pmu_cpu = this_cpu_ptr(&arc_pmu_cpu);
  276. struct hw_perf_event *hwc = &event->hw;
  277. int idx = hwc->idx;
  278. if (__test_and_set_bit(idx, pmu_cpu->used_mask)) {
  279. idx = find_first_zero_bit(pmu_cpu->used_mask,
  280. arc_pmu->n_counters);
  281. if (idx == arc_pmu->n_counters)
  282. return -EAGAIN;
  283. __set_bit(idx, pmu_cpu->used_mask);
  284. hwc->idx = idx;
  285. }
  286. write_aux_reg(ARC_REG_PCT_INDEX, idx);
  287. pmu_cpu->act_counter[idx] = event;
  288. if (is_sampling_event(event)) {
  289. /* Mimic full counter overflow as other arches do */
  290. write_aux_reg(ARC_REG_PCT_INT_CNTL, (u32)arc_pmu->max_period);
  291. write_aux_reg(ARC_REG_PCT_INT_CNTH,
  292. (arc_pmu->max_period >> 32));
  293. }
  294. write_aux_reg(ARC_REG_PCT_CONFIG, 0);
  295. write_aux_reg(ARC_REG_PCT_COUNTL, 0);
  296. write_aux_reg(ARC_REG_PCT_COUNTH, 0);
  297. local64_set(&hwc->prev_count, 0);
  298. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  299. if (flags & PERF_EF_START)
  300. arc_pmu_start(event, PERF_EF_RELOAD);
  301. perf_event_update_userpage(event);
  302. return 0;
  303. }
  304. #ifdef CONFIG_ISA_ARCV2
  305. static irqreturn_t arc_pmu_intr(int irq, void *dev)
  306. {
  307. struct perf_sample_data data;
  308. struct arc_pmu_cpu *pmu_cpu = this_cpu_ptr(&arc_pmu_cpu);
  309. struct pt_regs *regs;
  310. int active_ints;
  311. int idx;
  312. arc_pmu_disable(&arc_pmu->pmu);
  313. active_ints = read_aux_reg(ARC_REG_PCT_INT_ACT);
  314. regs = get_irq_regs();
  315. for (idx = 0; idx < arc_pmu->n_counters; idx++) {
  316. struct perf_event *event = pmu_cpu->act_counter[idx];
  317. struct hw_perf_event *hwc;
  318. if (!(active_ints & (1 << idx)))
  319. continue;
  320. /* Reset interrupt flag by writing of 1 */
  321. write_aux_reg(ARC_REG_PCT_INT_ACT, 1 << idx);
  322. /*
  323. * On reset of "interrupt active" bit corresponding
  324. * "interrupt enable" bit gets automatically reset as well.
  325. * Now we need to re-enable interrupt for the counter.
  326. */
  327. write_aux_reg(ARC_REG_PCT_INT_CTRL,
  328. read_aux_reg(ARC_REG_PCT_INT_CTRL) | (1 << idx));
  329. hwc = &event->hw;
  330. WARN_ON_ONCE(hwc->idx != idx);
  331. arc_perf_event_update(event, &event->hw, event->hw.idx);
  332. perf_sample_data_init(&data, 0, hwc->last_period);
  333. if (!arc_pmu_event_set_period(event))
  334. continue;
  335. if (perf_event_overflow(event, &data, regs))
  336. arc_pmu_stop(event, 0);
  337. }
  338. arc_pmu_enable(&arc_pmu->pmu);
  339. return IRQ_HANDLED;
  340. }
  341. #else
  342. static irqreturn_t arc_pmu_intr(int irq, void *dev)
  343. {
  344. return IRQ_NONE;
  345. }
  346. #endif /* CONFIG_ISA_ARCV2 */
  347. static void arc_cpu_pmu_irq_init(void *data)
  348. {
  349. int irq = *(int *)data;
  350. enable_percpu_irq(irq, IRQ_TYPE_NONE);
  351. /* Clear all pending interrupt flags */
  352. write_aux_reg(ARC_REG_PCT_INT_ACT, 0xffffffff);
  353. }
  354. static int arc_pmu_device_probe(struct platform_device *pdev)
  355. {
  356. struct arc_reg_pct_build pct_bcr;
  357. struct arc_reg_cc_build cc_bcr;
  358. int i, j, has_interrupts;
  359. int counter_size; /* in bits */
  360. union cc_name {
  361. struct {
  362. uint32_t word0, word1;
  363. char sentinel;
  364. } indiv;
  365. char str[9];
  366. } cc_name;
  367. READ_BCR(ARC_REG_PCT_BUILD, pct_bcr);
  368. if (!pct_bcr.v) {
  369. pr_err("This core does not have performance counters!\n");
  370. return -ENODEV;
  371. }
  372. BUG_ON(pct_bcr.c > ARC_PERF_MAX_COUNTERS);
  373. READ_BCR(ARC_REG_CC_BUILD, cc_bcr);
  374. BUG_ON(!cc_bcr.v); /* Counters exist but No countable conditions ? */
  375. arc_pmu = devm_kzalloc(&pdev->dev, sizeof(struct arc_pmu), GFP_KERNEL);
  376. if (!arc_pmu)
  377. return -ENOMEM;
  378. has_interrupts = is_isa_arcv2() ? pct_bcr.i : 0;
  379. arc_pmu->n_counters = pct_bcr.c;
  380. counter_size = 32 + (pct_bcr.s << 4);
  381. arc_pmu->max_period = (1ULL << counter_size) / 2 - 1ULL;
  382. pr_info("ARC perf\t: %d counters (%d bits), %d conditions%s\n",
  383. arc_pmu->n_counters, counter_size, cc_bcr.c,
  384. has_interrupts ? ", [overflow IRQ support]":"");
  385. cc_name.str[8] = 0;
  386. for (i = 0; i < PERF_COUNT_ARC_HW_MAX; i++)
  387. arc_pmu->ev_hw_idx[i] = -1;
  388. /* loop thru all available h/w condition indexes */
  389. for (j = 0; j < cc_bcr.c; j++) {
  390. write_aux_reg(ARC_REG_CC_INDEX, j);
  391. cc_name.indiv.word0 = read_aux_reg(ARC_REG_CC_NAME0);
  392. cc_name.indiv.word1 = read_aux_reg(ARC_REG_CC_NAME1);
  393. /* See if it has been mapped to a perf event_id */
  394. for (i = 0; i < ARRAY_SIZE(arc_pmu_ev_hw_map); i++) {
  395. if (arc_pmu_ev_hw_map[i] &&
  396. !strcmp(arc_pmu_ev_hw_map[i], cc_name.str) &&
  397. strlen(arc_pmu_ev_hw_map[i])) {
  398. pr_debug("mapping perf event %2d to h/w event \'%8s\' (idx %d)\n",
  399. i, cc_name.str, j);
  400. arc_pmu->ev_hw_idx[i] = j;
  401. }
  402. }
  403. }
  404. arc_pmu->pmu = (struct pmu) {
  405. .pmu_enable = arc_pmu_enable,
  406. .pmu_disable = arc_pmu_disable,
  407. .event_init = arc_pmu_event_init,
  408. .add = arc_pmu_add,
  409. .del = arc_pmu_del,
  410. .start = arc_pmu_start,
  411. .stop = arc_pmu_stop,
  412. .read = arc_pmu_read,
  413. };
  414. if (has_interrupts) {
  415. int irq = platform_get_irq(pdev, 0);
  416. if (irq < 0) {
  417. pr_err("Cannot get IRQ number for the platform\n");
  418. return -ENODEV;
  419. }
  420. arc_pmu->irq = irq;
  421. /* intc map function ensures irq_set_percpu_devid() called */
  422. request_percpu_irq(irq, arc_pmu_intr, "ARC perf counters",
  423. this_cpu_ptr(&arc_pmu_cpu));
  424. on_each_cpu(arc_cpu_pmu_irq_init, &irq, 1);
  425. } else
  426. arc_pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  427. return perf_pmu_register(&arc_pmu->pmu, pdev->name, PERF_TYPE_RAW);
  428. }
  429. #ifdef CONFIG_OF
  430. static const struct of_device_id arc_pmu_match[] = {
  431. { .compatible = "snps,arc700-pct" },
  432. { .compatible = "snps,archs-pct" },
  433. {},
  434. };
  435. MODULE_DEVICE_TABLE(of, arc_pmu_match);
  436. #endif
  437. static struct platform_driver arc_pmu_driver = {
  438. .driver = {
  439. .name = "arc-pct",
  440. .of_match_table = of_match_ptr(arc_pmu_match),
  441. },
  442. .probe = arc_pmu_device_probe,
  443. };
  444. module_platform_driver(arc_pmu_driver);
  445. MODULE_LICENSE("GPL");
  446. MODULE_AUTHOR("Mischa Jonker <mjonker@synopsys.com>");
  447. MODULE_DESCRIPTION("ARC PMU driver");