perf_event.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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
  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 <linux/export.h>
  29. #include <asm/processor.h>
  30. struct cpu_hw_events {
  31. struct perf_event *events[MAX_HWEVENTS];
  32. unsigned long used_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
  33. unsigned long active_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
  34. };
  35. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  36. static struct sh_pmu *sh_pmu __read_mostly;
  37. /* Number of perf_events counting hardware events */
  38. static atomic_t num_events;
  39. /* Used to avoid races in calling reserve/release_pmc_hardware */
  40. static DEFINE_MUTEX(pmc_reserve_mutex);
  41. /*
  42. * Stub these out for now, do something more profound later.
  43. */
  44. int reserve_pmc_hardware(void)
  45. {
  46. return 0;
  47. }
  48. void release_pmc_hardware(void)
  49. {
  50. }
  51. static inline int sh_pmu_initialized(void)
  52. {
  53. return !!sh_pmu;
  54. }
  55. const char *perf_pmu_name(void)
  56. {
  57. if (!sh_pmu)
  58. return NULL;
  59. return sh_pmu->name;
  60. }
  61. EXPORT_SYMBOL_GPL(perf_pmu_name);
  62. int perf_num_counters(void)
  63. {
  64. if (!sh_pmu)
  65. return 0;
  66. return sh_pmu->num_events;
  67. }
  68. EXPORT_SYMBOL_GPL(perf_num_counters);
  69. /*
  70. * Release the PMU if this is the last perf_event.
  71. */
  72. static void hw_perf_event_destroy(struct perf_event *event)
  73. {
  74. if (!atomic_add_unless(&num_events, -1, 1)) {
  75. mutex_lock(&pmc_reserve_mutex);
  76. if (atomic_dec_return(&num_events) == 0)
  77. release_pmc_hardware();
  78. mutex_unlock(&pmc_reserve_mutex);
  79. }
  80. }
  81. static int hw_perf_cache_event(int config, int *evp)
  82. {
  83. unsigned long type, op, result;
  84. int ev;
  85. if (!sh_pmu->cache_events)
  86. return -EINVAL;
  87. /* unpack config */
  88. type = config & 0xff;
  89. op = (config >> 8) & 0xff;
  90. result = (config >> 16) & 0xff;
  91. if (type >= PERF_COUNT_HW_CACHE_MAX ||
  92. op >= PERF_COUNT_HW_CACHE_OP_MAX ||
  93. result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  94. return -EINVAL;
  95. ev = (*sh_pmu->cache_events)[type][op][result];
  96. if (ev == 0)
  97. return -EOPNOTSUPP;
  98. if (ev == -1)
  99. return -EINVAL;
  100. *evp = ev;
  101. return 0;
  102. }
  103. static int __hw_perf_event_init(struct perf_event *event)
  104. {
  105. struct perf_event_attr *attr = &event->attr;
  106. struct hw_perf_event *hwc = &event->hw;
  107. int config = -1;
  108. int err;
  109. if (!sh_pmu_initialized())
  110. return -ENODEV;
  111. /*
  112. * See if we need to reserve the counter.
  113. *
  114. * If no events are currently in use, then we have to take a
  115. * mutex to ensure that we don't race with another task doing
  116. * reserve_pmc_hardware or release_pmc_hardware.
  117. */
  118. err = 0;
  119. if (!atomic_inc_not_zero(&num_events)) {
  120. mutex_lock(&pmc_reserve_mutex);
  121. if (atomic_read(&num_events) == 0 &&
  122. reserve_pmc_hardware())
  123. err = -EBUSY;
  124. else
  125. atomic_inc(&num_events);
  126. mutex_unlock(&pmc_reserve_mutex);
  127. }
  128. if (err)
  129. return err;
  130. event->destroy = hw_perf_event_destroy;
  131. switch (attr->type) {
  132. case PERF_TYPE_RAW:
  133. config = attr->config & sh_pmu->raw_event_mask;
  134. break;
  135. case PERF_TYPE_HW_CACHE:
  136. err = hw_perf_cache_event(attr->config, &config);
  137. if (err)
  138. return err;
  139. break;
  140. case PERF_TYPE_HARDWARE:
  141. if (attr->config >= sh_pmu->max_events)
  142. return -EINVAL;
  143. config = sh_pmu->event_map(attr->config);
  144. break;
  145. }
  146. if (config == -1)
  147. return -EINVAL;
  148. hwc->config |= config;
  149. return 0;
  150. }
  151. static void sh_perf_event_update(struct perf_event *event,
  152. struct hw_perf_event *hwc, int idx)
  153. {
  154. u64 prev_raw_count, new_raw_count;
  155. s64 delta;
  156. int shift = 0;
  157. /*
  158. * Depending on the counter configuration, they may or may not
  159. * be chained, in which case the previous counter value can be
  160. * updated underneath us if the lower-half overflows.
  161. *
  162. * Our tactic to handle this is to first atomically read and
  163. * exchange a new raw count - then add that new-prev delta
  164. * count to the generic counter atomically.
  165. *
  166. * As there is no interrupt associated with the overflow events,
  167. * this is the simplest approach for maintaining consistency.
  168. */
  169. again:
  170. prev_raw_count = local64_read(&hwc->prev_count);
  171. new_raw_count = sh_pmu->read(idx);
  172. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  173. new_raw_count) != prev_raw_count)
  174. goto again;
  175. /*
  176. * Now we have the new raw value and have updated the prev
  177. * timestamp already. We can now calculate the elapsed delta
  178. * (counter-)time and add that to the generic counter.
  179. *
  180. * Careful, not all hw sign-extends above the physical width
  181. * of the count.
  182. */
  183. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  184. delta >>= shift;
  185. local64_add(delta, &event->count);
  186. }
  187. static void sh_pmu_stop(struct perf_event *event, int flags)
  188. {
  189. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  190. struct hw_perf_event *hwc = &event->hw;
  191. int idx = hwc->idx;
  192. if (!(event->hw.state & PERF_HES_STOPPED)) {
  193. sh_pmu->disable(hwc, idx);
  194. cpuc->events[idx] = NULL;
  195. event->hw.state |= PERF_HES_STOPPED;
  196. }
  197. if ((flags & PERF_EF_UPDATE) && !(event->hw.state & PERF_HES_UPTODATE)) {
  198. sh_perf_event_update(event, &event->hw, idx);
  199. event->hw.state |= PERF_HES_UPTODATE;
  200. }
  201. }
  202. static void sh_pmu_start(struct perf_event *event, int flags)
  203. {
  204. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  205. struct hw_perf_event *hwc = &event->hw;
  206. int idx = hwc->idx;
  207. if (WARN_ON_ONCE(idx == -1))
  208. return;
  209. if (flags & PERF_EF_RELOAD)
  210. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  211. cpuc->events[idx] = event;
  212. event->hw.state = 0;
  213. sh_pmu->enable(hwc, idx);
  214. }
  215. static void sh_pmu_del(struct perf_event *event, int flags)
  216. {
  217. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  218. sh_pmu_stop(event, PERF_EF_UPDATE);
  219. __clear_bit(event->hw.idx, cpuc->used_mask);
  220. perf_event_update_userpage(event);
  221. }
  222. static int sh_pmu_add(struct perf_event *event, int flags)
  223. {
  224. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  225. struct hw_perf_event *hwc = &event->hw;
  226. int idx = hwc->idx;
  227. int ret = -EAGAIN;
  228. perf_pmu_disable(event->pmu);
  229. if (__test_and_set_bit(idx, cpuc->used_mask)) {
  230. idx = find_first_zero_bit(cpuc->used_mask, sh_pmu->num_events);
  231. if (idx == sh_pmu->num_events)
  232. goto out;
  233. __set_bit(idx, cpuc->used_mask);
  234. hwc->idx = idx;
  235. }
  236. sh_pmu->disable(hwc, idx);
  237. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  238. if (flags & PERF_EF_START)
  239. sh_pmu_start(event, PERF_EF_RELOAD);
  240. perf_event_update_userpage(event);
  241. ret = 0;
  242. out:
  243. perf_pmu_enable(event->pmu);
  244. return ret;
  245. }
  246. static void sh_pmu_read(struct perf_event *event)
  247. {
  248. sh_perf_event_update(event, &event->hw, event->hw.idx);
  249. }
  250. static int sh_pmu_event_init(struct perf_event *event)
  251. {
  252. int err;
  253. /* does not support taken branch sampling */
  254. if (has_branch_stack(event))
  255. return -EOPNOTSUPP;
  256. switch (event->attr.type) {
  257. case PERF_TYPE_RAW:
  258. case PERF_TYPE_HW_CACHE:
  259. case PERF_TYPE_HARDWARE:
  260. err = __hw_perf_event_init(event);
  261. break;
  262. default:
  263. return -ENOENT;
  264. }
  265. if (unlikely(err)) {
  266. if (event->destroy)
  267. event->destroy(event);
  268. }
  269. return err;
  270. }
  271. static void sh_pmu_enable(struct pmu *pmu)
  272. {
  273. if (!sh_pmu_initialized())
  274. return;
  275. sh_pmu->enable_all();
  276. }
  277. static void sh_pmu_disable(struct pmu *pmu)
  278. {
  279. if (!sh_pmu_initialized())
  280. return;
  281. sh_pmu->disable_all();
  282. }
  283. static struct pmu pmu = {
  284. .pmu_enable = sh_pmu_enable,
  285. .pmu_disable = sh_pmu_disable,
  286. .event_init = sh_pmu_event_init,
  287. .add = sh_pmu_add,
  288. .del = sh_pmu_del,
  289. .start = sh_pmu_start,
  290. .stop = sh_pmu_stop,
  291. .read = sh_pmu_read,
  292. };
  293. static void sh_pmu_setup(int cpu)
  294. {
  295. struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
  296. memset(cpuhw, 0, sizeof(struct cpu_hw_events));
  297. }
  298. static int
  299. sh_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  300. {
  301. unsigned int cpu = (long)hcpu;
  302. switch (action & ~CPU_TASKS_FROZEN) {
  303. case CPU_UP_PREPARE:
  304. sh_pmu_setup(cpu);
  305. break;
  306. default:
  307. break;
  308. }
  309. return NOTIFY_OK;
  310. }
  311. int register_sh_pmu(struct sh_pmu *_pmu)
  312. {
  313. if (sh_pmu)
  314. return -EBUSY;
  315. sh_pmu = _pmu;
  316. pr_info("Performance Events: %s support registered\n", _pmu->name);
  317. /*
  318. * All of the on-chip counters are "limited", in that they have
  319. * no interrupts, and are therefore unable to do sampling without
  320. * further work and timer assistance.
  321. */
  322. pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  323. WARN_ON(_pmu->num_events > MAX_HWEVENTS);
  324. perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  325. perf_cpu_notifier(sh_pmu_notifier);
  326. return 0;
  327. }