perf_event.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Linux performance counter support for MIPS.
  3. *
  4. * Copyright (C) 2010 MIPS Technologies, Inc.
  5. * Author: Deng-Cheng Zhu
  6. *
  7. * This code is based on the implementation for ARM, which is in turn
  8. * based on the sparc64 perf event code and the x86 code. Performance
  9. * counter access is based on the MIPS Oprofile code.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/cpumask.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/smp.h>
  18. #include <linux/kernel.h>
  19. #include <linux/perf_event.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/irq.h>
  22. #include <asm/irq_regs.h>
  23. #include <asm/stacktrace.h>
  24. #include <asm/time.h> /* For perf_irq */
  25. /* These are for 32bit counters. For 64bit ones, define them accordingly. */
  26. #define MAX_PERIOD ((1ULL << 32) - 1)
  27. #define VALID_COUNT 0x7fffffff
  28. #define TOTAL_BITS 32
  29. #define HIGHEST_BIT 31
  30. #define MIPS_MAX_HWEVENTS 4
  31. struct cpu_hw_events {
  32. /* Array of events on this cpu. */
  33. struct perf_event *events[MIPS_MAX_HWEVENTS];
  34. /*
  35. * Set the bit (indexed by the counter number) when the counter
  36. * is used for an event.
  37. */
  38. unsigned long used_mask[BITS_TO_LONGS(MIPS_MAX_HWEVENTS)];
  39. /*
  40. * The borrowed MSB for the performance counter. A MIPS performance
  41. * counter uses its bit 31 (for 32bit counters) or bit 63 (for 64bit
  42. * counters) as a factor of determining whether a counter overflow
  43. * should be signaled. So here we use a separate MSB for each
  44. * counter to make things easy.
  45. */
  46. unsigned long msbs[BITS_TO_LONGS(MIPS_MAX_HWEVENTS)];
  47. /*
  48. * Software copy of the control register for each performance counter.
  49. * MIPS CPUs vary in performance counters. They use this differently,
  50. * and even may not use it.
  51. */
  52. unsigned int saved_ctrl[MIPS_MAX_HWEVENTS];
  53. };
  54. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  55. .saved_ctrl = {0},
  56. };
  57. /* The description of MIPS performance events. */
  58. struct mips_perf_event {
  59. unsigned int event_id;
  60. /*
  61. * MIPS performance counters are indexed starting from 0.
  62. * CNTR_EVEN indicates the indexes of the counters to be used are
  63. * even numbers.
  64. */
  65. unsigned int cntr_mask;
  66. #define CNTR_EVEN 0x55555555
  67. #define CNTR_ODD 0xaaaaaaaa
  68. #ifdef CONFIG_MIPS_MT_SMP
  69. enum {
  70. T = 0,
  71. V = 1,
  72. P = 2,
  73. } range;
  74. #else
  75. #define T
  76. #define V
  77. #define P
  78. #endif
  79. };
  80. #define UNSUPPORTED_PERF_EVENT_ID 0xffffffff
  81. #define C(x) PERF_COUNT_HW_CACHE_##x
  82. struct mips_pmu {
  83. const char *name;
  84. int irq;
  85. irqreturn_t (*handle_irq)(int irq, void *dev);
  86. int (*handle_shared_irq)(void);
  87. void (*start)(void);
  88. void (*stop)(void);
  89. int (*alloc_counter)(struct cpu_hw_events *cpuc,
  90. struct hw_perf_event *hwc);
  91. u64 (*read_counter)(unsigned int idx);
  92. void (*write_counter)(unsigned int idx, u64 val);
  93. void (*enable_event)(struct hw_perf_event *evt, int idx);
  94. void (*disable_event)(int idx);
  95. const struct mips_perf_event (*general_event_map)[PERF_COUNT_HW_MAX];
  96. const struct mips_perf_event (*cache_event_map)
  97. [PERF_COUNT_HW_CACHE_MAX]
  98. [PERF_COUNT_HW_CACHE_OP_MAX]
  99. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  100. unsigned int num_counters;
  101. };
  102. static const struct mips_pmu *mipspmu;
  103. static int
  104. mipspmu_event_set_period(struct perf_event *event,
  105. struct hw_perf_event *hwc,
  106. int idx)
  107. {
  108. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  109. s64 left = local64_read(&hwc->period_left);
  110. s64 period = hwc->sample_period;
  111. int ret = 0;
  112. u64 uleft;
  113. unsigned long flags;
  114. if (unlikely(left <= -period)) {
  115. left = period;
  116. local64_set(&hwc->period_left, left);
  117. hwc->last_period = period;
  118. ret = 1;
  119. }
  120. if (unlikely(left <= 0)) {
  121. left += period;
  122. local64_set(&hwc->period_left, left);
  123. hwc->last_period = period;
  124. ret = 1;
  125. }
  126. if (left > (s64)MAX_PERIOD)
  127. left = MAX_PERIOD;
  128. local64_set(&hwc->prev_count, (u64)-left);
  129. local_irq_save(flags);
  130. uleft = (u64)(-left) & MAX_PERIOD;
  131. uleft > VALID_COUNT ?
  132. set_bit(idx, cpuc->msbs) : clear_bit(idx, cpuc->msbs);
  133. mipspmu->write_counter(idx, (u64)(-left) & VALID_COUNT);
  134. local_irq_restore(flags);
  135. perf_event_update_userpage(event);
  136. return ret;
  137. }
  138. static int mipspmu_enable(struct perf_event *event)
  139. {
  140. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  141. struct hw_perf_event *hwc = &event->hw;
  142. int idx;
  143. int err = 0;
  144. /* To look for a free counter for this event. */
  145. idx = mipspmu->alloc_counter(cpuc, hwc);
  146. if (idx < 0) {
  147. err = idx;
  148. goto out;
  149. }
  150. /*
  151. * If there is an event in the counter we are going to use then
  152. * make sure it is disabled.
  153. */
  154. event->hw.idx = idx;
  155. mipspmu->disable_event(idx);
  156. cpuc->events[idx] = event;
  157. /* Set the period for the event. */
  158. mipspmu_event_set_period(event, hwc, idx);
  159. /* Enable the event. */
  160. mipspmu->enable_event(hwc, idx);
  161. /* Propagate our changes to the userspace mapping. */
  162. perf_event_update_userpage(event);
  163. out:
  164. return err;
  165. }
  166. static void mipspmu_event_update(struct perf_event *event,
  167. struct hw_perf_event *hwc,
  168. int idx)
  169. {
  170. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  171. unsigned long flags;
  172. int shift = 64 - TOTAL_BITS;
  173. s64 prev_raw_count, new_raw_count;
  174. s64 delta;
  175. again:
  176. prev_raw_count = local64_read(&hwc->prev_count);
  177. local_irq_save(flags);
  178. /* Make the counter value be a "real" one. */
  179. new_raw_count = mipspmu->read_counter(idx);
  180. if (new_raw_count & (test_bit(idx, cpuc->msbs) << HIGHEST_BIT)) {
  181. new_raw_count &= VALID_COUNT;
  182. clear_bit(idx, cpuc->msbs);
  183. } else
  184. new_raw_count |= (test_bit(idx, cpuc->msbs) << HIGHEST_BIT);
  185. local_irq_restore(flags);
  186. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  187. new_raw_count) != prev_raw_count)
  188. goto again;
  189. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  190. delta >>= shift;
  191. local64_add(delta, &event->count);
  192. local64_sub(delta, &hwc->period_left);
  193. return;
  194. }
  195. static void mipspmu_disable(struct perf_event *event)
  196. {
  197. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  198. struct hw_perf_event *hwc = &event->hw;
  199. int idx = hwc->idx;
  200. WARN_ON(idx < 0 || idx >= mipspmu->num_counters);
  201. /* We are working on a local event. */
  202. mipspmu->disable_event(idx);
  203. barrier();
  204. mipspmu_event_update(event, hwc, idx);
  205. cpuc->events[idx] = NULL;
  206. clear_bit(idx, cpuc->used_mask);
  207. perf_event_update_userpage(event);
  208. }
  209. static void mipspmu_unthrottle(struct perf_event *event)
  210. {
  211. struct hw_perf_event *hwc = &event->hw;
  212. mipspmu->enable_event(hwc, hwc->idx);
  213. }
  214. static void mipspmu_read(struct perf_event *event)
  215. {
  216. struct hw_perf_event *hwc = &event->hw;
  217. /* Don't read disabled counters! */
  218. if (hwc->idx < 0)
  219. return;
  220. mipspmu_event_update(event, hwc, hwc->idx);
  221. }
  222. static struct pmu pmu = {
  223. .enable = mipspmu_enable,
  224. .disable = mipspmu_disable,
  225. .unthrottle = mipspmu_unthrottle,
  226. .read = mipspmu_read,
  227. };
  228. static atomic_t active_events = ATOMIC_INIT(0);
  229. static DEFINE_MUTEX(pmu_reserve_mutex);
  230. static int (*save_perf_irq)(void);
  231. static int mipspmu_get_irq(void)
  232. {
  233. int err;
  234. if (mipspmu->irq >= 0) {
  235. /* Request my own irq handler. */
  236. err = request_irq(mipspmu->irq, mipspmu->handle_irq,
  237. IRQF_DISABLED | IRQF_NOBALANCING,
  238. "mips_perf_pmu", NULL);
  239. if (err) {
  240. pr_warning("Unable to request IRQ%d for MIPS "
  241. "performance counters!\n", mipspmu->irq);
  242. }
  243. } else if (cp0_perfcount_irq < 0) {
  244. /*
  245. * We are sharing the irq number with the timer interrupt.
  246. */
  247. save_perf_irq = perf_irq;
  248. perf_irq = mipspmu->handle_shared_irq;
  249. err = 0;
  250. } else {
  251. pr_warning("The platform hasn't properly defined its "
  252. "interrupt controller.\n");
  253. err = -ENOENT;
  254. }
  255. return err;
  256. }
  257. static void mipspmu_free_irq(void)
  258. {
  259. if (mipspmu->irq >= 0)
  260. free_irq(mipspmu->irq, NULL);
  261. else if (cp0_perfcount_irq < 0)
  262. perf_irq = save_perf_irq;
  263. }
  264. static inline unsigned int
  265. mipspmu_perf_event_encode(const struct mips_perf_event *pev)
  266. {
  267. /*
  268. * Top 8 bits for range, next 16 bits for cntr_mask, lowest 8 bits for
  269. * event_id.
  270. */
  271. #ifdef CONFIG_MIPS_MT_SMP
  272. return ((unsigned int)pev->range << 24) |
  273. (pev->cntr_mask & 0xffff00) |
  274. (pev->event_id & 0xff);
  275. #else
  276. return (pev->cntr_mask & 0xffff00) |
  277. (pev->event_id & 0xff);
  278. #endif
  279. }
  280. static const struct mips_perf_event *
  281. mipspmu_map_general_event(int idx)
  282. {
  283. const struct mips_perf_event *pev;
  284. pev = ((*mipspmu->general_event_map)[idx].event_id ==
  285. UNSUPPORTED_PERF_EVENT_ID ? ERR_PTR(-EOPNOTSUPP) :
  286. &(*mipspmu->general_event_map)[idx]);
  287. return pev;
  288. }
  289. static const struct mips_perf_event *
  290. mipspmu_map_cache_event(u64 config)
  291. {
  292. unsigned int cache_type, cache_op, cache_result;
  293. const struct mips_perf_event *pev;
  294. cache_type = (config >> 0) & 0xff;
  295. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  296. return ERR_PTR(-EINVAL);
  297. cache_op = (config >> 8) & 0xff;
  298. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  299. return ERR_PTR(-EINVAL);
  300. cache_result = (config >> 16) & 0xff;
  301. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  302. return ERR_PTR(-EINVAL);
  303. pev = &((*mipspmu->cache_event_map)
  304. [cache_type]
  305. [cache_op]
  306. [cache_result]);
  307. if (pev->event_id == UNSUPPORTED_PERF_EVENT_ID)
  308. return ERR_PTR(-EOPNOTSUPP);
  309. return pev;
  310. }
  311. static int validate_event(struct cpu_hw_events *cpuc,
  312. struct perf_event *event)
  313. {
  314. struct hw_perf_event fake_hwc = event->hw;
  315. if (event->pmu && event->pmu != &pmu)
  316. return 0;
  317. return mipspmu->alloc_counter(cpuc, &fake_hwc) >= 0;
  318. }
  319. static int validate_group(struct perf_event *event)
  320. {
  321. struct perf_event *sibling, *leader = event->group_leader;
  322. struct cpu_hw_events fake_cpuc;
  323. memset(&fake_cpuc, 0, sizeof(fake_cpuc));
  324. if (!validate_event(&fake_cpuc, leader))
  325. return -ENOSPC;
  326. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  327. if (!validate_event(&fake_cpuc, sibling))
  328. return -ENOSPC;
  329. }
  330. if (!validate_event(&fake_cpuc, event))
  331. return -ENOSPC;
  332. return 0;
  333. }
  334. /*
  335. * mipsxx/rm9000/loongson2 have different performance counters, they have
  336. * specific low-level init routines.
  337. */
  338. static int __hw_perf_event_init(struct perf_event *event);
  339. static void hw_perf_event_destroy(struct perf_event *event)
  340. {
  341. if (atomic_dec_and_mutex_lock(&active_events,
  342. &pmu_reserve_mutex)) {
  343. /*
  344. * We must not call the destroy function with interrupts
  345. * disabled.
  346. */
  347. on_each_cpu(reset_counters,
  348. (void *)(long)mipspmu->num_counters, 1);
  349. mipspmu_free_irq();
  350. mutex_unlock(&pmu_reserve_mutex);
  351. }
  352. }
  353. const struct pmu *hw_perf_event_init(struct perf_event *event)
  354. {
  355. int err = 0;
  356. if (!mipspmu || event->cpu >= nr_cpumask_bits ||
  357. (event->cpu >= 0 && !cpu_online(event->cpu)))
  358. return ERR_PTR(-ENODEV);
  359. if (!atomic_inc_not_zero(&active_events)) {
  360. if (atomic_read(&active_events) > MIPS_MAX_HWEVENTS) {
  361. atomic_dec(&active_events);
  362. return ERR_PTR(-ENOSPC);
  363. }
  364. mutex_lock(&pmu_reserve_mutex);
  365. if (atomic_read(&active_events) == 0)
  366. err = mipspmu_get_irq();
  367. if (!err)
  368. atomic_inc(&active_events);
  369. mutex_unlock(&pmu_reserve_mutex);
  370. }
  371. if (err)
  372. return ERR_PTR(err);
  373. err = __hw_perf_event_init(event);
  374. if (err)
  375. hw_perf_event_destroy(event);
  376. return err ? ERR_PTR(err) : &pmu;
  377. }
  378. void hw_perf_enable(void)
  379. {
  380. if (mipspmu)
  381. mipspmu->start();
  382. }
  383. void hw_perf_disable(void)
  384. {
  385. if (mipspmu)
  386. mipspmu->stop();
  387. }
  388. /* This is needed by specific irq handlers in perf_event_*.c */
  389. static void
  390. handle_associated_event(struct cpu_hw_events *cpuc,
  391. int idx, struct perf_sample_data *data, struct pt_regs *regs)
  392. {
  393. struct perf_event *event = cpuc->events[idx];
  394. struct hw_perf_event *hwc = &event->hw;
  395. mipspmu_event_update(event, hwc, idx);
  396. data->period = event->hw.last_period;
  397. if (!mipspmu_event_set_period(event, hwc, idx))
  398. return;
  399. if (perf_event_overflow(event, 0, data, regs))
  400. mipspmu->disable_event(idx);
  401. }