perf_event.c 14 KB

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