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