perf_event.c 14 KB

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