perf_event.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. And the callchain
  10. * support references the code of MIPS stacktrace.c.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/cpumask.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/smp.h>
  19. #include <linux/kernel.h>
  20. #include <linux/perf_event.h>
  21. #include <linux/uaccess.h>
  22. #include <asm/irq.h>
  23. #include <asm/irq_regs.h>
  24. #include <asm/stacktrace.h>
  25. #include <asm/time.h> /* For perf_irq */
  26. /* These are for 32bit counters. For 64bit ones, define them accordingly. */
  27. #define MAX_PERIOD ((1ULL << 32) - 1)
  28. #define VALID_COUNT 0x7fffffff
  29. #define TOTAL_BITS 32
  30. #define HIGHEST_BIT 31
  31. #define MIPS_MAX_HWEVENTS 4
  32. struct cpu_hw_events {
  33. /* Array of events on this cpu. */
  34. struct perf_event *events[MIPS_MAX_HWEVENTS];
  35. /*
  36. * Set the bit (indexed by the counter number) when the counter
  37. * is used for an event.
  38. */
  39. unsigned long used_mask[BITS_TO_LONGS(MIPS_MAX_HWEVENTS)];
  40. /*
  41. * The borrowed MSB for the performance counter. A MIPS performance
  42. * counter uses its bit 31 (for 32bit counters) or bit 63 (for 64bit
  43. * counters) as a factor of determining whether a counter overflow
  44. * should be signaled. So here we use a separate MSB for each
  45. * counter to make things easy.
  46. */
  47. unsigned long msbs[BITS_TO_LONGS(MIPS_MAX_HWEVENTS)];
  48. /*
  49. * Software copy of the control register for each performance counter.
  50. * MIPS CPUs vary in performance counters. They use this differently,
  51. * and even may not use it.
  52. */
  53. unsigned int saved_ctrl[MIPS_MAX_HWEVENTS];
  54. };
  55. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  56. .saved_ctrl = {0},
  57. };
  58. /* The description of MIPS performance events. */
  59. struct mips_perf_event {
  60. unsigned int event_id;
  61. /*
  62. * MIPS performance counters are indexed starting from 0.
  63. * CNTR_EVEN indicates the indexes of the counters to be used are
  64. * even numbers.
  65. */
  66. unsigned int cntr_mask;
  67. #define CNTR_EVEN 0x55555555
  68. #define CNTR_ODD 0xaaaaaaaa
  69. #ifdef CONFIG_MIPS_MT_SMP
  70. enum {
  71. T = 0,
  72. V = 1,
  73. P = 2,
  74. } range;
  75. #else
  76. #define T
  77. #define V
  78. #define P
  79. #endif
  80. };
  81. static struct mips_perf_event raw_event;
  82. static DEFINE_MUTEX(raw_event_mutex);
  83. #define UNSUPPORTED_PERF_EVENT_ID 0xffffffff
  84. #define C(x) PERF_COUNT_HW_CACHE_##x
  85. struct mips_pmu {
  86. const char *name;
  87. int irq;
  88. irqreturn_t (*handle_irq)(int irq, void *dev);
  89. int (*handle_shared_irq)(void);
  90. void (*start)(void);
  91. void (*stop)(void);
  92. int (*alloc_counter)(struct cpu_hw_events *cpuc,
  93. struct hw_perf_event *hwc);
  94. u64 (*read_counter)(unsigned int idx);
  95. void (*write_counter)(unsigned int idx, u64 val);
  96. void (*enable_event)(struct hw_perf_event *evt, int idx);
  97. void (*disable_event)(int idx);
  98. const struct mips_perf_event *(*map_raw_event)(u64 config);
  99. const struct mips_perf_event (*general_event_map)[PERF_COUNT_HW_MAX];
  100. const struct mips_perf_event (*cache_event_map)
  101. [PERF_COUNT_HW_CACHE_MAX]
  102. [PERF_COUNT_HW_CACHE_OP_MAX]
  103. [PERF_COUNT_HW_CACHE_RESULT_MAX];
  104. unsigned int num_counters;
  105. };
  106. static const struct mips_pmu *mipspmu;
  107. static int
  108. mipspmu_event_set_period(struct perf_event *event,
  109. struct hw_perf_event *hwc,
  110. int idx)
  111. {
  112. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  113. s64 left = local64_read(&hwc->period_left);
  114. s64 period = hwc->sample_period;
  115. int ret = 0;
  116. u64 uleft;
  117. unsigned long flags;
  118. if (unlikely(left <= -period)) {
  119. left = period;
  120. local64_set(&hwc->period_left, left);
  121. hwc->last_period = period;
  122. ret = 1;
  123. }
  124. if (unlikely(left <= 0)) {
  125. left += period;
  126. local64_set(&hwc->period_left, left);
  127. hwc->last_period = period;
  128. ret = 1;
  129. }
  130. if (left > (s64)MAX_PERIOD)
  131. left = MAX_PERIOD;
  132. local64_set(&hwc->prev_count, (u64)-left);
  133. local_irq_save(flags);
  134. uleft = (u64)(-left) & MAX_PERIOD;
  135. uleft > VALID_COUNT ?
  136. set_bit(idx, cpuc->msbs) : clear_bit(idx, cpuc->msbs);
  137. mipspmu->write_counter(idx, (u64)(-left) & VALID_COUNT);
  138. local_irq_restore(flags);
  139. perf_event_update_userpage(event);
  140. return ret;
  141. }
  142. static void mipspmu_event_update(struct perf_event *event,
  143. struct hw_perf_event *hwc,
  144. int idx)
  145. {
  146. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  147. unsigned long flags;
  148. int shift = 64 - TOTAL_BITS;
  149. s64 prev_raw_count, new_raw_count;
  150. u64 delta;
  151. again:
  152. prev_raw_count = local64_read(&hwc->prev_count);
  153. local_irq_save(flags);
  154. /* Make the counter value be a "real" one. */
  155. new_raw_count = mipspmu->read_counter(idx);
  156. if (new_raw_count & (test_bit(idx, cpuc->msbs) << HIGHEST_BIT)) {
  157. new_raw_count &= VALID_COUNT;
  158. clear_bit(idx, cpuc->msbs);
  159. } else
  160. new_raw_count |= (test_bit(idx, cpuc->msbs) << HIGHEST_BIT);
  161. local_irq_restore(flags);
  162. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  163. new_raw_count) != prev_raw_count)
  164. goto again;
  165. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  166. delta >>= shift;
  167. local64_add(delta, &event->count);
  168. local64_sub(delta, &hwc->period_left);
  169. }
  170. static void mipspmu_start(struct perf_event *event, int flags)
  171. {
  172. struct hw_perf_event *hwc = &event->hw;
  173. if (!mipspmu)
  174. return;
  175. if (flags & PERF_EF_RELOAD)
  176. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  177. hwc->state = 0;
  178. /* Set the period for the event. */
  179. mipspmu_event_set_period(event, hwc, hwc->idx);
  180. /* Enable the event. */
  181. mipspmu->enable_event(hwc, hwc->idx);
  182. }
  183. static void mipspmu_stop(struct perf_event *event, int flags)
  184. {
  185. struct hw_perf_event *hwc = &event->hw;
  186. if (!mipspmu)
  187. return;
  188. if (!(hwc->state & PERF_HES_STOPPED)) {
  189. /* We are working on a local event. */
  190. mipspmu->disable_event(hwc->idx);
  191. barrier();
  192. mipspmu_event_update(event, hwc, hwc->idx);
  193. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  194. }
  195. }
  196. static int mipspmu_add(struct perf_event *event, int flags)
  197. {
  198. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  199. struct hw_perf_event *hwc = &event->hw;
  200. int idx;
  201. int err = 0;
  202. perf_pmu_disable(event->pmu);
  203. /* To look for a free counter for this event. */
  204. idx = mipspmu->alloc_counter(cpuc, hwc);
  205. if (idx < 0) {
  206. err = idx;
  207. goto out;
  208. }
  209. /*
  210. * If there is an event in the counter we are going to use then
  211. * make sure it is disabled.
  212. */
  213. event->hw.idx = idx;
  214. mipspmu->disable_event(idx);
  215. cpuc->events[idx] = event;
  216. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  217. if (flags & PERF_EF_START)
  218. mipspmu_start(event, PERF_EF_RELOAD);
  219. /* Propagate our changes to the userspace mapping. */
  220. perf_event_update_userpage(event);
  221. out:
  222. perf_pmu_enable(event->pmu);
  223. return err;
  224. }
  225. static void mipspmu_del(struct perf_event *event, int flags)
  226. {
  227. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  228. struct hw_perf_event *hwc = &event->hw;
  229. int idx = hwc->idx;
  230. WARN_ON(idx < 0 || idx >= mipspmu->num_counters);
  231. mipspmu_stop(event, PERF_EF_UPDATE);
  232. cpuc->events[idx] = NULL;
  233. clear_bit(idx, cpuc->used_mask);
  234. perf_event_update_userpage(event);
  235. }
  236. static void mipspmu_read(struct perf_event *event)
  237. {
  238. struct hw_perf_event *hwc = &event->hw;
  239. /* Don't read disabled counters! */
  240. if (hwc->idx < 0)
  241. return;
  242. mipspmu_event_update(event, hwc, hwc->idx);
  243. }
  244. static void mipspmu_enable(struct pmu *pmu)
  245. {
  246. if (mipspmu)
  247. mipspmu->start();
  248. }
  249. static void mipspmu_disable(struct pmu *pmu)
  250. {
  251. if (mipspmu)
  252. mipspmu->stop();
  253. }
  254. static atomic_t active_events = ATOMIC_INIT(0);
  255. static DEFINE_MUTEX(pmu_reserve_mutex);
  256. static int (*save_perf_irq)(void);
  257. static int mipspmu_get_irq(void)
  258. {
  259. int err;
  260. if (mipspmu->irq >= 0) {
  261. /* Request my own irq handler. */
  262. err = request_irq(mipspmu->irq, mipspmu->handle_irq,
  263. IRQF_DISABLED | IRQF_NOBALANCING,
  264. "mips_perf_pmu", NULL);
  265. if (err) {
  266. pr_warning("Unable to request IRQ%d for MIPS "
  267. "performance counters!\n", mipspmu->irq);
  268. }
  269. } else if (cp0_perfcount_irq < 0) {
  270. /*
  271. * We are sharing the irq number with the timer interrupt.
  272. */
  273. save_perf_irq = perf_irq;
  274. perf_irq = mipspmu->handle_shared_irq;
  275. err = 0;
  276. } else {
  277. pr_warning("The platform hasn't properly defined its "
  278. "interrupt controller.\n");
  279. err = -ENOENT;
  280. }
  281. return err;
  282. }
  283. static void mipspmu_free_irq(void)
  284. {
  285. if (mipspmu->irq >= 0)
  286. free_irq(mipspmu->irq, NULL);
  287. else if (cp0_perfcount_irq < 0)
  288. perf_irq = save_perf_irq;
  289. }
  290. /*
  291. * mipsxx/rm9000/loongson2 have different performance counters, they have
  292. * specific low-level init routines.
  293. */
  294. static void reset_counters(void *arg);
  295. static int __hw_perf_event_init(struct perf_event *event);
  296. static void hw_perf_event_destroy(struct perf_event *event)
  297. {
  298. if (atomic_dec_and_mutex_lock(&active_events,
  299. &pmu_reserve_mutex)) {
  300. /*
  301. * We must not call the destroy function with interrupts
  302. * disabled.
  303. */
  304. on_each_cpu(reset_counters,
  305. (void *)(long)mipspmu->num_counters, 1);
  306. mipspmu_free_irq();
  307. mutex_unlock(&pmu_reserve_mutex);
  308. }
  309. }
  310. static int mipspmu_event_init(struct perf_event *event)
  311. {
  312. int err = 0;
  313. switch (event->attr.type) {
  314. case PERF_TYPE_RAW:
  315. case PERF_TYPE_HARDWARE:
  316. case PERF_TYPE_HW_CACHE:
  317. break;
  318. default:
  319. return -ENOENT;
  320. }
  321. if (!mipspmu || event->cpu >= nr_cpumask_bits ||
  322. (event->cpu >= 0 && !cpu_online(event->cpu)))
  323. return -ENODEV;
  324. if (!atomic_inc_not_zero(&active_events)) {
  325. if (atomic_read(&active_events) > MIPS_MAX_HWEVENTS) {
  326. atomic_dec(&active_events);
  327. return -ENOSPC;
  328. }
  329. mutex_lock(&pmu_reserve_mutex);
  330. if (atomic_read(&active_events) == 0)
  331. err = mipspmu_get_irq();
  332. if (!err)
  333. atomic_inc(&active_events);
  334. mutex_unlock(&pmu_reserve_mutex);
  335. }
  336. if (err)
  337. return err;
  338. err = __hw_perf_event_init(event);
  339. if (err)
  340. hw_perf_event_destroy(event);
  341. return err;
  342. }
  343. static struct pmu pmu = {
  344. .pmu_enable = mipspmu_enable,
  345. .pmu_disable = mipspmu_disable,
  346. .event_init = mipspmu_event_init,
  347. .add = mipspmu_add,
  348. .del = mipspmu_del,
  349. .start = mipspmu_start,
  350. .stop = mipspmu_stop,
  351. .read = mipspmu_read,
  352. };
  353. static inline unsigned int
  354. mipspmu_perf_event_encode(const struct mips_perf_event *pev)
  355. {
  356. /*
  357. * Top 8 bits for range, next 16 bits for cntr_mask, lowest 8 bits for
  358. * event_id.
  359. */
  360. #ifdef CONFIG_MIPS_MT_SMP
  361. return ((unsigned int)pev->range << 24) |
  362. (pev->cntr_mask & 0xffff00) |
  363. (pev->event_id & 0xff);
  364. #else
  365. return (pev->cntr_mask & 0xffff00) |
  366. (pev->event_id & 0xff);
  367. #endif
  368. }
  369. static const struct mips_perf_event *
  370. mipspmu_map_general_event(int idx)
  371. {
  372. const struct mips_perf_event *pev;
  373. pev = ((*mipspmu->general_event_map)[idx].event_id ==
  374. UNSUPPORTED_PERF_EVENT_ID ? ERR_PTR(-EOPNOTSUPP) :
  375. &(*mipspmu->general_event_map)[idx]);
  376. return pev;
  377. }
  378. static const struct mips_perf_event *
  379. mipspmu_map_cache_event(u64 config)
  380. {
  381. unsigned int cache_type, cache_op, cache_result;
  382. const struct mips_perf_event *pev;
  383. cache_type = (config >> 0) & 0xff;
  384. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  385. return ERR_PTR(-EINVAL);
  386. cache_op = (config >> 8) & 0xff;
  387. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  388. return ERR_PTR(-EINVAL);
  389. cache_result = (config >> 16) & 0xff;
  390. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  391. return ERR_PTR(-EINVAL);
  392. pev = &((*mipspmu->cache_event_map)
  393. [cache_type]
  394. [cache_op]
  395. [cache_result]);
  396. if (pev->event_id == UNSUPPORTED_PERF_EVENT_ID)
  397. return ERR_PTR(-EOPNOTSUPP);
  398. return pev;
  399. }
  400. static int validate_event(struct cpu_hw_events *cpuc,
  401. struct perf_event *event)
  402. {
  403. struct hw_perf_event fake_hwc = event->hw;
  404. /* Allow mixed event group. So return 1 to pass validation. */
  405. if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF)
  406. return 1;
  407. return mipspmu->alloc_counter(cpuc, &fake_hwc) >= 0;
  408. }
  409. static int validate_group(struct perf_event *event)
  410. {
  411. struct perf_event *sibling, *leader = event->group_leader;
  412. struct cpu_hw_events fake_cpuc;
  413. memset(&fake_cpuc, 0, sizeof(fake_cpuc));
  414. if (!validate_event(&fake_cpuc, leader))
  415. return -ENOSPC;
  416. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  417. if (!validate_event(&fake_cpuc, sibling))
  418. return -ENOSPC;
  419. }
  420. if (!validate_event(&fake_cpuc, event))
  421. return -ENOSPC;
  422. return 0;
  423. }
  424. /* This is needed by specific irq handlers in perf_event_*.c */
  425. static void
  426. handle_associated_event(struct cpu_hw_events *cpuc,
  427. int idx, struct perf_sample_data *data, struct pt_regs *regs)
  428. {
  429. struct perf_event *event = cpuc->events[idx];
  430. struct hw_perf_event *hwc = &event->hw;
  431. mipspmu_event_update(event, hwc, idx);
  432. data->period = event->hw.last_period;
  433. if (!mipspmu_event_set_period(event, hwc, idx))
  434. return;
  435. if (perf_event_overflow(event, data, regs))
  436. mipspmu->disable_event(idx);
  437. }
  438. #include "perf_event_mipsxx.c"
  439. /* Callchain handling code. */
  440. /*
  441. * Leave userspace callchain empty for now. When we find a way to trace
  442. * the user stack callchains, we add here.
  443. */
  444. void perf_callchain_user(struct perf_callchain_entry *entry,
  445. struct pt_regs *regs)
  446. {
  447. }
  448. static void save_raw_perf_callchain(struct perf_callchain_entry *entry,
  449. unsigned long reg29)
  450. {
  451. unsigned long *sp = (unsigned long *)reg29;
  452. unsigned long addr;
  453. while (!kstack_end(sp)) {
  454. addr = *sp++;
  455. if (__kernel_text_address(addr)) {
  456. perf_callchain_store(entry, addr);
  457. if (entry->nr >= PERF_MAX_STACK_DEPTH)
  458. break;
  459. }
  460. }
  461. }
  462. void perf_callchain_kernel(struct perf_callchain_entry *entry,
  463. struct pt_regs *regs)
  464. {
  465. unsigned long sp = regs->regs[29];
  466. #ifdef CONFIG_KALLSYMS
  467. unsigned long ra = regs->regs[31];
  468. unsigned long pc = regs->cp0_epc;
  469. if (raw_show_trace || !__kernel_text_address(pc)) {
  470. unsigned long stack_page =
  471. (unsigned long)task_stack_page(current);
  472. if (stack_page && sp >= stack_page &&
  473. sp <= stack_page + THREAD_SIZE - 32)
  474. save_raw_perf_callchain(entry, sp);
  475. return;
  476. }
  477. do {
  478. perf_callchain_store(entry, pc);
  479. if (entry->nr >= PERF_MAX_STACK_DEPTH)
  480. break;
  481. pc = unwind_stack(current, &sp, pc, &ra);
  482. } while (pc);
  483. #else
  484. save_raw_perf_callchain(entry, sp);
  485. #endif
  486. }