perf_event.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. return;
  170. }
  171. static void mipspmu_start(struct perf_event *event, int flags)
  172. {
  173. struct hw_perf_event *hwc = &event->hw;
  174. if (!mipspmu)
  175. return;
  176. if (flags & PERF_EF_RELOAD)
  177. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  178. hwc->state = 0;
  179. /* Set the period for the event. */
  180. mipspmu_event_set_period(event, hwc, hwc->idx);
  181. /* Enable the event. */
  182. mipspmu->enable_event(hwc, hwc->idx);
  183. }
  184. static void mipspmu_stop(struct perf_event *event, int flags)
  185. {
  186. struct hw_perf_event *hwc = &event->hw;
  187. if (!mipspmu)
  188. return;
  189. if (!(hwc->state & PERF_HES_STOPPED)) {
  190. /* We are working on a local event. */
  191. mipspmu->disable_event(hwc->idx);
  192. barrier();
  193. mipspmu_event_update(event, hwc, hwc->idx);
  194. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  195. }
  196. }
  197. static int mipspmu_add(struct perf_event *event, int flags)
  198. {
  199. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  200. struct hw_perf_event *hwc = &event->hw;
  201. int idx;
  202. int err = 0;
  203. perf_pmu_disable(event->pmu);
  204. /* To look for a free counter for this event. */
  205. idx = mipspmu->alloc_counter(cpuc, hwc);
  206. if (idx < 0) {
  207. err = idx;
  208. goto out;
  209. }
  210. /*
  211. * If there is an event in the counter we are going to use then
  212. * make sure it is disabled.
  213. */
  214. event->hw.idx = idx;
  215. mipspmu->disable_event(idx);
  216. cpuc->events[idx] = event;
  217. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  218. if (flags & PERF_EF_START)
  219. mipspmu_start(event, PERF_EF_RELOAD);
  220. /* Propagate our changes to the userspace mapping. */
  221. perf_event_update_userpage(event);
  222. out:
  223. perf_pmu_enable(event->pmu);
  224. return err;
  225. }
  226. static void mipspmu_del(struct perf_event *event, int flags)
  227. {
  228. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  229. struct hw_perf_event *hwc = &event->hw;
  230. int idx = hwc->idx;
  231. WARN_ON(idx < 0 || idx >= mipspmu->num_counters);
  232. mipspmu_stop(event, PERF_EF_UPDATE);
  233. cpuc->events[idx] = NULL;
  234. clear_bit(idx, cpuc->used_mask);
  235. perf_event_update_userpage(event);
  236. }
  237. static void mipspmu_read(struct perf_event *event)
  238. {
  239. struct hw_perf_event *hwc = &event->hw;
  240. /* Don't read disabled counters! */
  241. if (hwc->idx < 0)
  242. return;
  243. mipspmu_event_update(event, hwc, hwc->idx);
  244. }
  245. static void mipspmu_enable(struct pmu *pmu)
  246. {
  247. if (mipspmu)
  248. mipspmu->start();
  249. }
  250. static void mipspmu_disable(struct pmu *pmu)
  251. {
  252. if (mipspmu)
  253. mipspmu->stop();
  254. }
  255. static atomic_t active_events = ATOMIC_INIT(0);
  256. static DEFINE_MUTEX(pmu_reserve_mutex);
  257. static int (*save_perf_irq)(void);
  258. static int mipspmu_get_irq(void)
  259. {
  260. int err;
  261. if (mipspmu->irq >= 0) {
  262. /* Request my own irq handler. */
  263. err = request_irq(mipspmu->irq, mipspmu->handle_irq,
  264. IRQF_DISABLED | IRQF_NOBALANCING,
  265. "mips_perf_pmu", NULL);
  266. if (err) {
  267. pr_warning("Unable to request IRQ%d for MIPS "
  268. "performance counters!\n", mipspmu->irq);
  269. }
  270. } else if (cp0_perfcount_irq < 0) {
  271. /*
  272. * We are sharing the irq number with the timer interrupt.
  273. */
  274. save_perf_irq = perf_irq;
  275. perf_irq = mipspmu->handle_shared_irq;
  276. err = 0;
  277. } else {
  278. pr_warning("The platform hasn't properly defined its "
  279. "interrupt controller.\n");
  280. err = -ENOENT;
  281. }
  282. return err;
  283. }
  284. static void mipspmu_free_irq(void)
  285. {
  286. if (mipspmu->irq >= 0)
  287. free_irq(mipspmu->irq, NULL);
  288. else if (cp0_perfcount_irq < 0)
  289. perf_irq = save_perf_irq;
  290. }
  291. /*
  292. * mipsxx/rm9000/loongson2 have different performance counters, they have
  293. * specific low-level init routines.
  294. */
  295. static void reset_counters(void *arg);
  296. static int __hw_perf_event_init(struct perf_event *event);
  297. static void hw_perf_event_destroy(struct perf_event *event)
  298. {
  299. if (atomic_dec_and_mutex_lock(&active_events,
  300. &pmu_reserve_mutex)) {
  301. /*
  302. * We must not call the destroy function with interrupts
  303. * disabled.
  304. */
  305. on_each_cpu(reset_counters,
  306. (void *)(long)mipspmu->num_counters, 1);
  307. mipspmu_free_irq();
  308. mutex_unlock(&pmu_reserve_mutex);
  309. }
  310. }
  311. static int mipspmu_event_init(struct perf_event *event)
  312. {
  313. int err = 0;
  314. switch (event->attr.type) {
  315. case PERF_TYPE_RAW:
  316. case PERF_TYPE_HARDWARE:
  317. case PERF_TYPE_HW_CACHE:
  318. break;
  319. default:
  320. return -ENOENT;
  321. }
  322. if (!mipspmu || event->cpu >= nr_cpumask_bits ||
  323. (event->cpu >= 0 && !cpu_online(event->cpu)))
  324. return -ENODEV;
  325. if (!atomic_inc_not_zero(&active_events)) {
  326. if (atomic_read(&active_events) > MIPS_MAX_HWEVENTS) {
  327. atomic_dec(&active_events);
  328. return -ENOSPC;
  329. }
  330. mutex_lock(&pmu_reserve_mutex);
  331. if (atomic_read(&active_events) == 0)
  332. err = mipspmu_get_irq();
  333. if (!err)
  334. atomic_inc(&active_events);
  335. mutex_unlock(&pmu_reserve_mutex);
  336. }
  337. if (err)
  338. return err;
  339. err = __hw_perf_event_init(event);
  340. if (err)
  341. hw_perf_event_destroy(event);
  342. return err;
  343. }
  344. static struct pmu pmu = {
  345. .pmu_enable = mipspmu_enable,
  346. .pmu_disable = mipspmu_disable,
  347. .event_init = mipspmu_event_init,
  348. .add = mipspmu_add,
  349. .del = mipspmu_del,
  350. .start = mipspmu_start,
  351. .stop = mipspmu_stop,
  352. .read = mipspmu_read,
  353. };
  354. static inline unsigned int
  355. mipspmu_perf_event_encode(const struct mips_perf_event *pev)
  356. {
  357. /*
  358. * Top 8 bits for range, next 16 bits for cntr_mask, lowest 8 bits for
  359. * event_id.
  360. */
  361. #ifdef CONFIG_MIPS_MT_SMP
  362. return ((unsigned int)pev->range << 24) |
  363. (pev->cntr_mask & 0xffff00) |
  364. (pev->event_id & 0xff);
  365. #else
  366. return (pev->cntr_mask & 0xffff00) |
  367. (pev->event_id & 0xff);
  368. #endif
  369. }
  370. static const struct mips_perf_event *
  371. mipspmu_map_general_event(int idx)
  372. {
  373. const struct mips_perf_event *pev;
  374. pev = ((*mipspmu->general_event_map)[idx].event_id ==
  375. UNSUPPORTED_PERF_EVENT_ID ? ERR_PTR(-EOPNOTSUPP) :
  376. &(*mipspmu->general_event_map)[idx]);
  377. return pev;
  378. }
  379. static const struct mips_perf_event *
  380. mipspmu_map_cache_event(u64 config)
  381. {
  382. unsigned int cache_type, cache_op, cache_result;
  383. const struct mips_perf_event *pev;
  384. cache_type = (config >> 0) & 0xff;
  385. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  386. return ERR_PTR(-EINVAL);
  387. cache_op = (config >> 8) & 0xff;
  388. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  389. return ERR_PTR(-EINVAL);
  390. cache_result = (config >> 16) & 0xff;
  391. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  392. return ERR_PTR(-EINVAL);
  393. pev = &((*mipspmu->cache_event_map)
  394. [cache_type]
  395. [cache_op]
  396. [cache_result]);
  397. if (pev->event_id == UNSUPPORTED_PERF_EVENT_ID)
  398. return ERR_PTR(-EOPNOTSUPP);
  399. return pev;
  400. }
  401. static int validate_event(struct cpu_hw_events *cpuc,
  402. struct perf_event *event)
  403. {
  404. struct hw_perf_event fake_hwc = event->hw;
  405. /* Allow mixed event group. So return 1 to pass validation. */
  406. if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF)
  407. return 1;
  408. return mipspmu->alloc_counter(cpuc, &fake_hwc) >= 0;
  409. }
  410. static int validate_group(struct perf_event *event)
  411. {
  412. struct perf_event *sibling, *leader = event->group_leader;
  413. struct cpu_hw_events fake_cpuc;
  414. memset(&fake_cpuc, 0, sizeof(fake_cpuc));
  415. if (!validate_event(&fake_cpuc, leader))
  416. return -ENOSPC;
  417. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  418. if (!validate_event(&fake_cpuc, sibling))
  419. return -ENOSPC;
  420. }
  421. if (!validate_event(&fake_cpuc, event))
  422. return -ENOSPC;
  423. return 0;
  424. }
  425. /* This is needed by specific irq handlers in perf_event_*.c */
  426. static void
  427. handle_associated_event(struct cpu_hw_events *cpuc,
  428. int idx, struct perf_sample_data *data, struct pt_regs *regs)
  429. {
  430. struct perf_event *event = cpuc->events[idx];
  431. struct hw_perf_event *hwc = &event->hw;
  432. mipspmu_event_update(event, hwc, idx);
  433. data->period = event->hw.last_period;
  434. if (!mipspmu_event_set_period(event, hwc, idx))
  435. return;
  436. if (perf_event_overflow(event, 0, data, regs))
  437. mipspmu->disable_event(idx);
  438. }
  439. #include "perf_event_mipsxx.c"
  440. /* Callchain handling code. */
  441. /*
  442. * Leave userspace callchain empty for now. When we find a way to trace
  443. * the user stack callchains, we add here.
  444. */
  445. void perf_callchain_user(struct perf_callchain_entry *entry,
  446. struct pt_regs *regs)
  447. {
  448. }
  449. static void save_raw_perf_callchain(struct perf_callchain_entry *entry,
  450. unsigned long reg29)
  451. {
  452. unsigned long *sp = (unsigned long *)reg29;
  453. unsigned long addr;
  454. while (!kstack_end(sp)) {
  455. addr = *sp++;
  456. if (__kernel_text_address(addr)) {
  457. perf_callchain_store(entry, addr);
  458. if (entry->nr >= PERF_MAX_STACK_DEPTH)
  459. break;
  460. }
  461. }
  462. }
  463. void perf_callchain_kernel(struct perf_callchain_entry *entry,
  464. struct pt_regs *regs)
  465. {
  466. unsigned long sp = regs->regs[29];
  467. #ifdef CONFIG_KALLSYMS
  468. unsigned long ra = regs->regs[31];
  469. unsigned long pc = regs->cp0_epc;
  470. if (raw_show_trace || !__kernel_text_address(pc)) {
  471. unsigned long stack_page =
  472. (unsigned long)task_stack_page(current);
  473. if (stack_page && sp >= stack_page &&
  474. sp <= stack_page + THREAD_SIZE - 32)
  475. save_raw_perf_callchain(entry, sp);
  476. return;
  477. }
  478. do {
  479. perf_callchain_store(entry, pc);
  480. if (entry->nr >= PERF_MAX_STACK_DEPTH)
  481. break;
  482. pc = unwind_stack(current, &sp, pc, &ra);
  483. } while (pc);
  484. #else
  485. save_raw_perf_callchain(entry, sp);
  486. #endif
  487. }