perf_event.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Performance event support framework for SuperH hardware counters.
  3. *
  4. * Copyright (C) 2009 Paul Mundt
  5. *
  6. * Heavily based on the x86 and PowerPC implementations.
  7. *
  8. * x86:
  9. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  10. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  11. * Copyright (C) 2009 Jaswinder Singh Rajput
  12. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  13. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  14. * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
  15. *
  16. * ppc:
  17. * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
  18. *
  19. * This file is subject to the terms and conditions of the GNU General Public
  20. * License. See the file "COPYING" in the main directory of this archive
  21. * for more details.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/io.h>
  26. #include <linux/irq.h>
  27. #include <linux/perf_event.h>
  28. #include <asm/processor.h>
  29. struct cpu_hw_events {
  30. struct perf_event *events[MAX_HWEVENTS];
  31. unsigned long used_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
  32. unsigned long active_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
  33. };
  34. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  35. static struct sh_pmu *sh_pmu __read_mostly;
  36. /* Number of perf_events counting hardware events */
  37. static atomic_t num_events;
  38. /* Used to avoid races in calling reserve/release_pmc_hardware */
  39. static DEFINE_MUTEX(pmc_reserve_mutex);
  40. /*
  41. * Stub these out for now, do something more profound later.
  42. */
  43. int reserve_pmc_hardware(void)
  44. {
  45. return 0;
  46. }
  47. void release_pmc_hardware(void)
  48. {
  49. }
  50. static inline int sh_pmu_initialized(void)
  51. {
  52. return !!sh_pmu;
  53. }
  54. const char *perf_pmu_name(void)
  55. {
  56. if (!sh_pmu)
  57. return NULL;
  58. return sh_pmu->name;
  59. }
  60. EXPORT_SYMBOL_GPL(perf_pmu_name);
  61. int perf_num_counters(void)
  62. {
  63. if (!sh_pmu)
  64. return 0;
  65. return sh_pmu->num_events;
  66. }
  67. EXPORT_SYMBOL_GPL(perf_num_counters);
  68. /*
  69. * Release the PMU if this is the last perf_event.
  70. */
  71. static void hw_perf_event_destroy(struct perf_event *event)
  72. {
  73. if (!atomic_add_unless(&num_events, -1, 1)) {
  74. mutex_lock(&pmc_reserve_mutex);
  75. if (atomic_dec_return(&num_events) == 0)
  76. release_pmc_hardware();
  77. mutex_unlock(&pmc_reserve_mutex);
  78. }
  79. }
  80. static int hw_perf_cache_event(int config, int *evp)
  81. {
  82. unsigned long type, op, result;
  83. int ev;
  84. if (!sh_pmu->cache_events)
  85. return -EINVAL;
  86. /* unpack config */
  87. type = config & 0xff;
  88. op = (config >> 8) & 0xff;
  89. result = (config >> 16) & 0xff;
  90. if (type >= PERF_COUNT_HW_CACHE_MAX ||
  91. op >= PERF_COUNT_HW_CACHE_OP_MAX ||
  92. result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  93. return -EINVAL;
  94. ev = (*sh_pmu->cache_events)[type][op][result];
  95. if (ev == 0)
  96. return -EOPNOTSUPP;
  97. if (ev == -1)
  98. return -EINVAL;
  99. *evp = ev;
  100. return 0;
  101. }
  102. static int __hw_perf_event_init(struct perf_event *event)
  103. {
  104. struct perf_event_attr *attr = &event->attr;
  105. struct hw_perf_event *hwc = &event->hw;
  106. int config = -1;
  107. int err;
  108. if (!sh_pmu_initialized())
  109. return -ENODEV;
  110. /*
  111. * All of the on-chip counters are "limited", in that they have
  112. * no interrupts, and are therefore unable to do sampling without
  113. * further work and timer assistance.
  114. */
  115. if (hwc->sample_period)
  116. return -EINVAL;
  117. /*
  118. * See if we need to reserve the counter.
  119. *
  120. * If no events are currently in use, then we have to take a
  121. * mutex to ensure that we don't race with another task doing
  122. * reserve_pmc_hardware or release_pmc_hardware.
  123. */
  124. err = 0;
  125. if (!atomic_inc_not_zero(&num_events)) {
  126. mutex_lock(&pmc_reserve_mutex);
  127. if (atomic_read(&num_events) == 0 &&
  128. reserve_pmc_hardware())
  129. err = -EBUSY;
  130. else
  131. atomic_inc(&num_events);
  132. mutex_unlock(&pmc_reserve_mutex);
  133. }
  134. if (err)
  135. return err;
  136. event->destroy = hw_perf_event_destroy;
  137. switch (attr->type) {
  138. case PERF_TYPE_RAW:
  139. config = attr->config & sh_pmu->raw_event_mask;
  140. break;
  141. case PERF_TYPE_HW_CACHE:
  142. err = hw_perf_cache_event(attr->config, &config);
  143. if (err)
  144. return err;
  145. break;
  146. case PERF_TYPE_HARDWARE:
  147. if (attr->config >= sh_pmu->max_events)
  148. return -EINVAL;
  149. config = sh_pmu->event_map(attr->config);
  150. break;
  151. }
  152. if (config == -1)
  153. return -EINVAL;
  154. hwc->config |= config;
  155. return 0;
  156. }
  157. static void sh_perf_event_update(struct perf_event *event,
  158. struct hw_perf_event *hwc, int idx)
  159. {
  160. u64 prev_raw_count, new_raw_count;
  161. s64 delta;
  162. int shift = 0;
  163. /*
  164. * Depending on the counter configuration, they may or may not
  165. * be chained, in which case the previous counter value can be
  166. * updated underneath us if the lower-half overflows.
  167. *
  168. * Our tactic to handle this is to first atomically read and
  169. * exchange a new raw count - then add that new-prev delta
  170. * count to the generic counter atomically.
  171. *
  172. * As there is no interrupt associated with the overflow events,
  173. * this is the simplest approach for maintaining consistency.
  174. */
  175. again:
  176. prev_raw_count = local64_read(&hwc->prev_count);
  177. new_raw_count = sh_pmu->read(idx);
  178. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  179. new_raw_count) != prev_raw_count)
  180. goto again;
  181. /*
  182. * Now we have the new raw value and have updated the prev
  183. * timestamp already. We can now calculate the elapsed delta
  184. * (counter-)time and add that to the generic counter.
  185. *
  186. * Careful, not all hw sign-extends above the physical width
  187. * of the count.
  188. */
  189. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  190. delta >>= shift;
  191. local64_add(delta, &event->count);
  192. }
  193. static void sh_pmu_disable(struct perf_event *event)
  194. {
  195. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  196. struct hw_perf_event *hwc = &event->hw;
  197. int idx = hwc->idx;
  198. clear_bit(idx, cpuc->active_mask);
  199. sh_pmu->disable(hwc, idx);
  200. barrier();
  201. sh_perf_event_update(event, &event->hw, idx);
  202. cpuc->events[idx] = NULL;
  203. clear_bit(idx, cpuc->used_mask);
  204. perf_event_update_userpage(event);
  205. }
  206. static int sh_pmu_enable(struct perf_event *event)
  207. {
  208. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  209. struct hw_perf_event *hwc = &event->hw;
  210. int idx = hwc->idx;
  211. if (test_and_set_bit(idx, cpuc->used_mask)) {
  212. idx = find_first_zero_bit(cpuc->used_mask, sh_pmu->num_events);
  213. if (idx == sh_pmu->num_events)
  214. return -EAGAIN;
  215. set_bit(idx, cpuc->used_mask);
  216. hwc->idx = idx;
  217. }
  218. sh_pmu->disable(hwc, idx);
  219. cpuc->events[idx] = event;
  220. set_bit(idx, cpuc->active_mask);
  221. sh_pmu->enable(hwc, idx);
  222. perf_event_update_userpage(event);
  223. return 0;
  224. }
  225. static void sh_pmu_read(struct perf_event *event)
  226. {
  227. sh_perf_event_update(event, &event->hw, event->hw.idx);
  228. }
  229. static const struct pmu pmu = {
  230. .enable = sh_pmu_enable,
  231. .disable = sh_pmu_disable,
  232. .read = sh_pmu_read,
  233. };
  234. const struct pmu *hw_perf_event_init(struct perf_event *event)
  235. {
  236. int err = __hw_perf_event_init(event);
  237. if (unlikely(err)) {
  238. if (event->destroy)
  239. event->destroy(event);
  240. return ERR_PTR(err);
  241. }
  242. return &pmu;
  243. }
  244. static void sh_pmu_setup(int cpu)
  245. {
  246. struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
  247. memset(cpuhw, 0, sizeof(struct cpu_hw_events));
  248. }
  249. static int __cpuinit
  250. sh_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  251. {
  252. unsigned int cpu = (long)hcpu;
  253. switch (action & ~CPU_TASKS_FROZEN) {
  254. case CPU_UP_PREPARE:
  255. sh_pmu_setup(cpu);
  256. break;
  257. default:
  258. break;
  259. }
  260. return NOTIFY_OK;
  261. }
  262. void hw_perf_enable(void)
  263. {
  264. if (!sh_pmu_initialized())
  265. return;
  266. sh_pmu->enable_all();
  267. }
  268. void hw_perf_disable(void)
  269. {
  270. if (!sh_pmu_initialized())
  271. return;
  272. sh_pmu->disable_all();
  273. }
  274. int __cpuinit register_sh_pmu(struct sh_pmu *pmu)
  275. {
  276. if (sh_pmu)
  277. return -EBUSY;
  278. sh_pmu = pmu;
  279. pr_info("Performance Events: %s support registered\n", pmu->name);
  280. WARN_ON(pmu->num_events > MAX_HWEVENTS);
  281. perf_cpu_notifier(sh_pmu_notifier);
  282. return 0;
  283. }