perf_event.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * Blackfin performance counters
  3. *
  4. * Copyright 2011 Analog Devices Inc.
  5. *
  6. * Ripped from SuperH version:
  7. *
  8. * Copyright (C) 2009 Paul Mundt
  9. *
  10. * Heavily based on the x86 and PowerPC implementations.
  11. *
  12. * x86:
  13. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  14. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  15. * Copyright (C) 2009 Jaswinder Singh Rajput
  16. * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
  17. * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  18. * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
  19. *
  20. * ppc:
  21. * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
  22. *
  23. * Licensed under the GPL-2 or later.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/perf_event.h>
  28. #include <asm/bfin_pfmon.h>
  29. /*
  30. * We have two counters, and each counter can support an event type.
  31. * The 'o' is PFCNTx=1 and 's' is PFCNTx=0
  32. *
  33. * 0x04 o pc invariant branches
  34. * 0x06 o mispredicted branches
  35. * 0x09 o predicted branches taken
  36. * 0x0B o EXCPT insn
  37. * 0x0C o CSYNC/SSYNC insn
  38. * 0x0D o Insns committed
  39. * 0x0E o Interrupts taken
  40. * 0x0F o Misaligned address exceptions
  41. * 0x80 o Code memory fetches stalled due to DMA
  42. * 0x83 o 64bit insn fetches delivered
  43. * 0x9A o data cache fills (bank a)
  44. * 0x9B o data cache fills (bank b)
  45. * 0x9C o data cache lines evicted (bank a)
  46. * 0x9D o data cache lines evicted (bank b)
  47. * 0x9E o data cache high priority fills
  48. * 0x9F o data cache low priority fills
  49. * 0x00 s loop 0 iterations
  50. * 0x01 s loop 1 iterations
  51. * 0x0A s CSYNC/SSYNC stalls
  52. * 0x10 s DAG read/after write hazards
  53. * 0x13 s RAW data hazards
  54. * 0x81 s code TAG stalls
  55. * 0x82 s code fill stalls
  56. * 0x90 s processor to memory stalls
  57. * 0x91 s data memory stalls not hidden by 0x90
  58. * 0x92 s data store buffer full stalls
  59. * 0x93 s data memory write buffer full stalls due to high->low priority
  60. * 0x95 s data memory fill buffer stalls
  61. * 0x96 s data TAG collision stalls
  62. * 0x97 s data collision stalls
  63. * 0x98 s data stalls
  64. * 0x99 s data stalls sent to processor
  65. */
  66. static const int event_map[] = {
  67. /* use CYCLES cpu register */
  68. [PERF_COUNT_HW_CPU_CYCLES] = -1,
  69. [PERF_COUNT_HW_INSTRUCTIONS] = 0x0D,
  70. [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
  71. [PERF_COUNT_HW_CACHE_MISSES] = 0x83,
  72. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x09,
  73. [PERF_COUNT_HW_BRANCH_MISSES] = 0x06,
  74. [PERF_COUNT_HW_BUS_CYCLES] = -1,
  75. };
  76. #define C(x) PERF_COUNT_HW_CACHE_##x
  77. static const int cache_events[PERF_COUNT_HW_CACHE_MAX]
  78. [PERF_COUNT_HW_CACHE_OP_MAX]
  79. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  80. {
  81. [C(L1D)] = { /* Data bank A */
  82. [C(OP_READ)] = {
  83. [C(RESULT_ACCESS)] = 0,
  84. [C(RESULT_MISS) ] = 0x9A,
  85. },
  86. [C(OP_WRITE)] = {
  87. [C(RESULT_ACCESS)] = 0,
  88. [C(RESULT_MISS) ] = 0,
  89. },
  90. [C(OP_PREFETCH)] = {
  91. [C(RESULT_ACCESS)] = 0,
  92. [C(RESULT_MISS) ] = 0,
  93. },
  94. },
  95. [C(L1I)] = {
  96. [C(OP_READ)] = {
  97. [C(RESULT_ACCESS)] = 0,
  98. [C(RESULT_MISS) ] = 0x83,
  99. },
  100. [C(OP_WRITE)] = {
  101. [C(RESULT_ACCESS)] = -1,
  102. [C(RESULT_MISS) ] = -1,
  103. },
  104. [C(OP_PREFETCH)] = {
  105. [C(RESULT_ACCESS)] = 0,
  106. [C(RESULT_MISS) ] = 0,
  107. },
  108. },
  109. [C(LL)] = {
  110. [C(OP_READ)] = {
  111. [C(RESULT_ACCESS)] = -1,
  112. [C(RESULT_MISS) ] = -1,
  113. },
  114. [C(OP_WRITE)] = {
  115. [C(RESULT_ACCESS)] = -1,
  116. [C(RESULT_MISS) ] = -1,
  117. },
  118. [C(OP_PREFETCH)] = {
  119. [C(RESULT_ACCESS)] = -1,
  120. [C(RESULT_MISS) ] = -1,
  121. },
  122. },
  123. [C(DTLB)] = {
  124. [C(OP_READ)] = {
  125. [C(RESULT_ACCESS)] = -1,
  126. [C(RESULT_MISS) ] = -1,
  127. },
  128. [C(OP_WRITE)] = {
  129. [C(RESULT_ACCESS)] = -1,
  130. [C(RESULT_MISS) ] = -1,
  131. },
  132. [C(OP_PREFETCH)] = {
  133. [C(RESULT_ACCESS)] = -1,
  134. [C(RESULT_MISS) ] = -1,
  135. },
  136. },
  137. [C(ITLB)] = {
  138. [C(OP_READ)] = {
  139. [C(RESULT_ACCESS)] = -1,
  140. [C(RESULT_MISS) ] = -1,
  141. },
  142. [C(OP_WRITE)] = {
  143. [C(RESULT_ACCESS)] = -1,
  144. [C(RESULT_MISS) ] = -1,
  145. },
  146. [C(OP_PREFETCH)] = {
  147. [C(RESULT_ACCESS)] = -1,
  148. [C(RESULT_MISS) ] = -1,
  149. },
  150. },
  151. [C(BPU)] = {
  152. [C(OP_READ)] = {
  153. [C(RESULT_ACCESS)] = -1,
  154. [C(RESULT_MISS) ] = -1,
  155. },
  156. [C(OP_WRITE)] = {
  157. [C(RESULT_ACCESS)] = -1,
  158. [C(RESULT_MISS) ] = -1,
  159. },
  160. [C(OP_PREFETCH)] = {
  161. [C(RESULT_ACCESS)] = -1,
  162. [C(RESULT_MISS) ] = -1,
  163. },
  164. },
  165. };
  166. const char *perf_pmu_name(void)
  167. {
  168. return "bfin";
  169. }
  170. EXPORT_SYMBOL(perf_pmu_name);
  171. int perf_num_counters(void)
  172. {
  173. return ARRAY_SIZE(event_map);
  174. }
  175. EXPORT_SYMBOL(perf_num_counters);
  176. static u64 bfin_pfmon_read(int idx)
  177. {
  178. return bfin_read32(PFCNTR0 + (idx * 4));
  179. }
  180. static void bfin_pfmon_disable(struct hw_perf_event *hwc, int idx)
  181. {
  182. bfin_write_PFCTL(bfin_read_PFCTL() & ~PFCEN(idx, PFCEN_MASK));
  183. }
  184. static void bfin_pfmon_enable(struct hw_perf_event *hwc, int idx)
  185. {
  186. u32 val, mask;
  187. val = PFPWR;
  188. if (idx) {
  189. mask = ~(PFCNT1 | PFMON1 | PFCEN1 | PEMUSW1);
  190. /* The packed config is for event0, so shift it to event1 slots */
  191. val |= (hwc->config << (PFMON1_P - PFMON0_P));
  192. val |= (hwc->config & PFCNT0) << (PFCNT1_P - PFCNT0_P);
  193. bfin_write_PFCNTR1(0);
  194. } else {
  195. mask = ~(PFCNT0 | PFMON0 | PFCEN0 | PEMUSW0);
  196. val |= hwc->config;
  197. bfin_write_PFCNTR0(0);
  198. }
  199. bfin_write_PFCTL((bfin_read_PFCTL() & mask) | val);
  200. }
  201. static void bfin_pfmon_disable_all(void)
  202. {
  203. bfin_write_PFCTL(bfin_read_PFCTL() & ~PFPWR);
  204. }
  205. static void bfin_pfmon_enable_all(void)
  206. {
  207. bfin_write_PFCTL(bfin_read_PFCTL() | PFPWR);
  208. }
  209. struct cpu_hw_events {
  210. struct perf_event *events[MAX_HWEVENTS];
  211. unsigned long used_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
  212. };
  213. DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
  214. static int hw_perf_cache_event(int config, int *evp)
  215. {
  216. unsigned long type, op, result;
  217. int ev;
  218. /* unpack config */
  219. type = config & 0xff;
  220. op = (config >> 8) & 0xff;
  221. result = (config >> 16) & 0xff;
  222. if (type >= PERF_COUNT_HW_CACHE_MAX ||
  223. op >= PERF_COUNT_HW_CACHE_OP_MAX ||
  224. result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  225. return -EINVAL;
  226. ev = cache_events[type][op][result];
  227. if (ev == 0)
  228. return -EOPNOTSUPP;
  229. if (ev == -1)
  230. return -EINVAL;
  231. *evp = ev;
  232. return 0;
  233. }
  234. static void bfin_perf_event_update(struct perf_event *event,
  235. struct hw_perf_event *hwc, int idx)
  236. {
  237. u64 prev_raw_count, new_raw_count;
  238. s64 delta;
  239. int shift = 0;
  240. /*
  241. * Depending on the counter configuration, they may or may not
  242. * be chained, in which case the previous counter value can be
  243. * updated underneath us if the lower-half overflows.
  244. *
  245. * Our tactic to handle this is to first atomically read and
  246. * exchange a new raw count - then add that new-prev delta
  247. * count to the generic counter atomically.
  248. *
  249. * As there is no interrupt associated with the overflow events,
  250. * this is the simplest approach for maintaining consistency.
  251. */
  252. again:
  253. prev_raw_count = local64_read(&hwc->prev_count);
  254. new_raw_count = bfin_pfmon_read(idx);
  255. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  256. new_raw_count) != prev_raw_count)
  257. goto again;
  258. /*
  259. * Now we have the new raw value and have updated the prev
  260. * timestamp already. We can now calculate the elapsed delta
  261. * (counter-)time and add that to the generic counter.
  262. *
  263. * Careful, not all hw sign-extends above the physical width
  264. * of the count.
  265. */
  266. delta = (new_raw_count << shift) - (prev_raw_count << shift);
  267. delta >>= shift;
  268. local64_add(delta, &event->count);
  269. }
  270. static void bfin_pmu_stop(struct perf_event *event, int flags)
  271. {
  272. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  273. struct hw_perf_event *hwc = &event->hw;
  274. int idx = hwc->idx;
  275. if (!(event->hw.state & PERF_HES_STOPPED)) {
  276. bfin_pfmon_disable(hwc, idx);
  277. cpuc->events[idx] = NULL;
  278. event->hw.state |= PERF_HES_STOPPED;
  279. }
  280. if ((flags & PERF_EF_UPDATE) && !(event->hw.state & PERF_HES_UPTODATE)) {
  281. bfin_perf_event_update(event, &event->hw, idx);
  282. event->hw.state |= PERF_HES_UPTODATE;
  283. }
  284. }
  285. static void bfin_pmu_start(struct perf_event *event, int flags)
  286. {
  287. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  288. struct hw_perf_event *hwc = &event->hw;
  289. int idx = hwc->idx;
  290. if (WARN_ON_ONCE(idx == -1))
  291. return;
  292. if (flags & PERF_EF_RELOAD)
  293. WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
  294. cpuc->events[idx] = event;
  295. event->hw.state = 0;
  296. bfin_pfmon_enable(hwc, idx);
  297. }
  298. static void bfin_pmu_del(struct perf_event *event, int flags)
  299. {
  300. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  301. bfin_pmu_stop(event, PERF_EF_UPDATE);
  302. __clear_bit(event->hw.idx, cpuc->used_mask);
  303. perf_event_update_userpage(event);
  304. }
  305. static int bfin_pmu_add(struct perf_event *event, int flags)
  306. {
  307. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  308. struct hw_perf_event *hwc = &event->hw;
  309. int idx = hwc->idx;
  310. int ret = -EAGAIN;
  311. perf_pmu_disable(event->pmu);
  312. if (__test_and_set_bit(idx, cpuc->used_mask)) {
  313. idx = find_first_zero_bit(cpuc->used_mask, MAX_HWEVENTS);
  314. if (idx == MAX_HWEVENTS)
  315. goto out;
  316. __set_bit(idx, cpuc->used_mask);
  317. hwc->idx = idx;
  318. }
  319. bfin_pfmon_disable(hwc, idx);
  320. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  321. if (flags & PERF_EF_START)
  322. bfin_pmu_start(event, PERF_EF_RELOAD);
  323. perf_event_update_userpage(event);
  324. ret = 0;
  325. out:
  326. perf_pmu_enable(event->pmu);
  327. return ret;
  328. }
  329. static void bfin_pmu_read(struct perf_event *event)
  330. {
  331. bfin_perf_event_update(event, &event->hw, event->hw.idx);
  332. }
  333. static int bfin_pmu_event_init(struct perf_event *event)
  334. {
  335. struct perf_event_attr *attr = &event->attr;
  336. struct hw_perf_event *hwc = &event->hw;
  337. int config = -1;
  338. int ret;
  339. if (attr->exclude_hv || attr->exclude_idle)
  340. return -EPERM;
  341. /*
  342. * All of the on-chip counters are "limited", in that they have
  343. * no interrupts, and are therefore unable to do sampling without
  344. * further work and timer assistance.
  345. */
  346. if (hwc->sample_period)
  347. return -EINVAL;
  348. ret = 0;
  349. switch (attr->type) {
  350. case PERF_TYPE_RAW:
  351. config = PFMON(0, attr->config & PFMON_MASK) |
  352. PFCNT(0, !(attr->config & 0x100));
  353. break;
  354. case PERF_TYPE_HW_CACHE:
  355. ret = hw_perf_cache_event(attr->config, &config);
  356. break;
  357. case PERF_TYPE_HARDWARE:
  358. if (attr->config >= ARRAY_SIZE(event_map))
  359. return -EINVAL;
  360. config = event_map[attr->config];
  361. break;
  362. }
  363. if (config == -1)
  364. return -EINVAL;
  365. if (!attr->exclude_kernel)
  366. config |= PFCEN(0, PFCEN_ENABLE_SUPV);
  367. if (!attr->exclude_user)
  368. config |= PFCEN(0, PFCEN_ENABLE_USER);
  369. hwc->config |= config;
  370. return ret;
  371. }
  372. static void bfin_pmu_enable(struct pmu *pmu)
  373. {
  374. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  375. struct perf_event *event;
  376. struct hw_perf_event *hwc;
  377. int i;
  378. for (i = 0; i < MAX_HWEVENTS; ++i) {
  379. event = cpuc->events[i];
  380. if (!event)
  381. continue;
  382. hwc = &event->hw;
  383. bfin_pfmon_enable(hwc, hwc->idx);
  384. }
  385. bfin_pfmon_enable_all();
  386. }
  387. static void bfin_pmu_disable(struct pmu *pmu)
  388. {
  389. bfin_pfmon_disable_all();
  390. }
  391. static struct pmu pmu = {
  392. .pmu_enable = bfin_pmu_enable,
  393. .pmu_disable = bfin_pmu_disable,
  394. .event_init = bfin_pmu_event_init,
  395. .add = bfin_pmu_add,
  396. .del = bfin_pmu_del,
  397. .start = bfin_pmu_start,
  398. .stop = bfin_pmu_stop,
  399. .read = bfin_pmu_read,
  400. };
  401. static void bfin_pmu_setup(int cpu)
  402. {
  403. struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
  404. memset(cpuhw, 0, sizeof(struct cpu_hw_events));
  405. }
  406. static int __cpuinit
  407. bfin_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
  408. {
  409. unsigned int cpu = (long)hcpu;
  410. switch (action & ~CPU_TASKS_FROZEN) {
  411. case CPU_UP_PREPARE:
  412. bfin_write_PFCTL(0);
  413. bfin_pmu_setup(cpu);
  414. break;
  415. default:
  416. break;
  417. }
  418. return NOTIFY_OK;
  419. }
  420. static int __init bfin_pmu_init(void)
  421. {
  422. int ret;
  423. ret = perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
  424. if (!ret)
  425. perf_cpu_notifier(bfin_pmu_notifier);
  426. return ret;
  427. }
  428. early_initcall(bfin_pmu_init);