perf_event_v6.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. * ARMv6 Performance counter handling code.
  3. *
  4. * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
  5. *
  6. * ARMv6 has 2 configurable performance counters and a single cycle counter.
  7. * They all share a single reset bit but can be written to zero so we can use
  8. * that for a reset.
  9. *
  10. * The counters can't be individually enabled or disabled so when we remove
  11. * one event and replace it with another we could get spurious counts from the
  12. * wrong event. However, we can take advantage of the fact that the
  13. * performance counters can export events to the event bus, and the event bus
  14. * itself can be monitored. This requires that we *don't* export the events to
  15. * the event bus. The procedure for disabling a configurable counter is:
  16. * - change the counter to count the ETMEXTOUT[0] signal (0x20). This
  17. * effectively stops the counter from counting.
  18. * - disable the counter's interrupt generation (each counter has it's
  19. * own interrupt enable bit).
  20. * Once stopped, the counter value can be written as 0 to reset.
  21. *
  22. * To enable a counter:
  23. * - enable the counter's interrupt generation.
  24. * - set the new event type.
  25. *
  26. * Note: the dedicated cycle counter only counts cycles and can't be
  27. * enabled/disabled independently of the others. When we want to disable the
  28. * cycle counter, we have to just disable the interrupt reporting and start
  29. * ignoring that counter. When re-enabling, we have to reset the value and
  30. * enable the interrupt.
  31. */
  32. #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K)
  33. enum armv6_perf_types {
  34. ARMV6_PERFCTR_ICACHE_MISS = 0x0,
  35. ARMV6_PERFCTR_IBUF_STALL = 0x1,
  36. ARMV6_PERFCTR_DDEP_STALL = 0x2,
  37. ARMV6_PERFCTR_ITLB_MISS = 0x3,
  38. ARMV6_PERFCTR_DTLB_MISS = 0x4,
  39. ARMV6_PERFCTR_BR_EXEC = 0x5,
  40. ARMV6_PERFCTR_BR_MISPREDICT = 0x6,
  41. ARMV6_PERFCTR_INSTR_EXEC = 0x7,
  42. ARMV6_PERFCTR_DCACHE_HIT = 0x9,
  43. ARMV6_PERFCTR_DCACHE_ACCESS = 0xA,
  44. ARMV6_PERFCTR_DCACHE_MISS = 0xB,
  45. ARMV6_PERFCTR_DCACHE_WBACK = 0xC,
  46. ARMV6_PERFCTR_SW_PC_CHANGE = 0xD,
  47. ARMV6_PERFCTR_MAIN_TLB_MISS = 0xF,
  48. ARMV6_PERFCTR_EXPL_D_ACCESS = 0x10,
  49. ARMV6_PERFCTR_LSU_FULL_STALL = 0x11,
  50. ARMV6_PERFCTR_WBUF_DRAINED = 0x12,
  51. ARMV6_PERFCTR_CPU_CYCLES = 0xFF,
  52. ARMV6_PERFCTR_NOP = 0x20,
  53. };
  54. enum armv6_counters {
  55. ARMV6_CYCLE_COUNTER = 0,
  56. ARMV6_COUNTER0,
  57. ARMV6_COUNTER1,
  58. };
  59. /*
  60. * The hardware events that we support. We do support cache operations but
  61. * we have harvard caches and no way to combine instruction and data
  62. * accesses/misses in hardware.
  63. */
  64. static const unsigned armv6_perf_map[PERF_COUNT_HW_MAX] = {
  65. [PERF_COUNT_HW_CPU_CYCLES] = ARMV6_PERFCTR_CPU_CYCLES,
  66. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6_PERFCTR_INSTR_EXEC,
  67. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  68. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  69. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6_PERFCTR_BR_EXEC,
  70. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6_PERFCTR_BR_MISPREDICT,
  71. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  72. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV6_PERFCTR_IBUF_STALL,
  73. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV6_PERFCTR_LSU_FULL_STALL,
  74. };
  75. static const unsigned armv6_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  76. [PERF_COUNT_HW_CACHE_OP_MAX]
  77. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  78. [C(L1D)] = {
  79. /*
  80. * The performance counters don't differentiate between read
  81. * and write accesses/misses so this isn't strictly correct,
  82. * but it's the best we can do. Writes and reads get
  83. * combined.
  84. */
  85. [C(OP_READ)] = {
  86. [C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
  87. [C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
  88. },
  89. [C(OP_WRITE)] = {
  90. [C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
  91. [C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
  92. },
  93. [C(OP_PREFETCH)] = {
  94. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  95. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  96. },
  97. },
  98. [C(L1I)] = {
  99. [C(OP_READ)] = {
  100. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  101. [C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
  102. },
  103. [C(OP_WRITE)] = {
  104. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  105. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  106. },
  107. [C(OP_PREFETCH)] = {
  108. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  109. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  110. },
  111. },
  112. [C(LL)] = {
  113. [C(OP_READ)] = {
  114. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  115. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  116. },
  117. [C(OP_WRITE)] = {
  118. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  119. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  120. },
  121. [C(OP_PREFETCH)] = {
  122. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  123. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  124. },
  125. },
  126. [C(DTLB)] = {
  127. /*
  128. * The ARM performance counters can count micro DTLB misses,
  129. * micro ITLB misses and main TLB misses. There isn't an event
  130. * for TLB misses, so use the micro misses here and if users
  131. * want the main TLB misses they can use a raw counter.
  132. */
  133. [C(OP_READ)] = {
  134. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  135. [C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
  136. },
  137. [C(OP_WRITE)] = {
  138. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  139. [C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
  140. },
  141. [C(OP_PREFETCH)] = {
  142. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  143. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  144. },
  145. },
  146. [C(ITLB)] = {
  147. [C(OP_READ)] = {
  148. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  149. [C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
  150. },
  151. [C(OP_WRITE)] = {
  152. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  153. [C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
  154. },
  155. [C(OP_PREFETCH)] = {
  156. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  157. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  158. },
  159. },
  160. [C(BPU)] = {
  161. [C(OP_READ)] = {
  162. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  163. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  164. },
  165. [C(OP_WRITE)] = {
  166. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  167. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  168. },
  169. [C(OP_PREFETCH)] = {
  170. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  171. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  172. },
  173. },
  174. [C(NODE)] = {
  175. [C(OP_READ)] = {
  176. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  177. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  178. },
  179. [C(OP_WRITE)] = {
  180. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  181. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  182. },
  183. [C(OP_PREFETCH)] = {
  184. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  185. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  186. },
  187. },
  188. };
  189. enum armv6mpcore_perf_types {
  190. ARMV6MPCORE_PERFCTR_ICACHE_MISS = 0x0,
  191. ARMV6MPCORE_PERFCTR_IBUF_STALL = 0x1,
  192. ARMV6MPCORE_PERFCTR_DDEP_STALL = 0x2,
  193. ARMV6MPCORE_PERFCTR_ITLB_MISS = 0x3,
  194. ARMV6MPCORE_PERFCTR_DTLB_MISS = 0x4,
  195. ARMV6MPCORE_PERFCTR_BR_EXEC = 0x5,
  196. ARMV6MPCORE_PERFCTR_BR_NOTPREDICT = 0x6,
  197. ARMV6MPCORE_PERFCTR_BR_MISPREDICT = 0x7,
  198. ARMV6MPCORE_PERFCTR_INSTR_EXEC = 0x8,
  199. ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS = 0xA,
  200. ARMV6MPCORE_PERFCTR_DCACHE_RDMISS = 0xB,
  201. ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS = 0xC,
  202. ARMV6MPCORE_PERFCTR_DCACHE_WRMISS = 0xD,
  203. ARMV6MPCORE_PERFCTR_DCACHE_EVICTION = 0xE,
  204. ARMV6MPCORE_PERFCTR_SW_PC_CHANGE = 0xF,
  205. ARMV6MPCORE_PERFCTR_MAIN_TLB_MISS = 0x10,
  206. ARMV6MPCORE_PERFCTR_EXPL_MEM_ACCESS = 0x11,
  207. ARMV6MPCORE_PERFCTR_LSU_FULL_STALL = 0x12,
  208. ARMV6MPCORE_PERFCTR_WBUF_DRAINED = 0x13,
  209. ARMV6MPCORE_PERFCTR_CPU_CYCLES = 0xFF,
  210. };
  211. /*
  212. * The hardware events that we support. We do support cache operations but
  213. * we have harvard caches and no way to combine instruction and data
  214. * accesses/misses in hardware.
  215. */
  216. static const unsigned armv6mpcore_perf_map[PERF_COUNT_HW_MAX] = {
  217. [PERF_COUNT_HW_CPU_CYCLES] = ARMV6MPCORE_PERFCTR_CPU_CYCLES,
  218. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_INSTR_EXEC,
  219. [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
  220. [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
  221. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_BR_EXEC,
  222. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6MPCORE_PERFCTR_BR_MISPREDICT,
  223. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  224. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV6MPCORE_PERFCTR_IBUF_STALL,
  225. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV6MPCORE_PERFCTR_LSU_FULL_STALL,
  226. };
  227. static const unsigned armv6mpcore_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  228. [PERF_COUNT_HW_CACHE_OP_MAX]
  229. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  230. [C(L1D)] = {
  231. [C(OP_READ)] = {
  232. [C(RESULT_ACCESS)] =
  233. ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS,
  234. [C(RESULT_MISS)] =
  235. ARMV6MPCORE_PERFCTR_DCACHE_RDMISS,
  236. },
  237. [C(OP_WRITE)] = {
  238. [C(RESULT_ACCESS)] =
  239. ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS,
  240. [C(RESULT_MISS)] =
  241. ARMV6MPCORE_PERFCTR_DCACHE_WRMISS,
  242. },
  243. [C(OP_PREFETCH)] = {
  244. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  245. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  246. },
  247. },
  248. [C(L1I)] = {
  249. [C(OP_READ)] = {
  250. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  251. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
  252. },
  253. [C(OP_WRITE)] = {
  254. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  255. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  256. },
  257. [C(OP_PREFETCH)] = {
  258. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  259. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  260. },
  261. },
  262. [C(LL)] = {
  263. [C(OP_READ)] = {
  264. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  265. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  266. },
  267. [C(OP_WRITE)] = {
  268. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  269. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  270. },
  271. [C(OP_PREFETCH)] = {
  272. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  273. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  274. },
  275. },
  276. [C(DTLB)] = {
  277. /*
  278. * The ARM performance counters can count micro DTLB misses,
  279. * micro ITLB misses and main TLB misses. There isn't an event
  280. * for TLB misses, so use the micro misses here and if users
  281. * want the main TLB misses they can use a raw counter.
  282. */
  283. [C(OP_READ)] = {
  284. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  285. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
  286. },
  287. [C(OP_WRITE)] = {
  288. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  289. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
  290. },
  291. [C(OP_PREFETCH)] = {
  292. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  293. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  294. },
  295. },
  296. [C(ITLB)] = {
  297. [C(OP_READ)] = {
  298. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  299. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
  300. },
  301. [C(OP_WRITE)] = {
  302. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  303. [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
  304. },
  305. [C(OP_PREFETCH)] = {
  306. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  307. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  308. },
  309. },
  310. [C(BPU)] = {
  311. [C(OP_READ)] = {
  312. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  313. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  314. },
  315. [C(OP_WRITE)] = {
  316. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  317. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  318. },
  319. [C(OP_PREFETCH)] = {
  320. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  321. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  322. },
  323. },
  324. [C(NODE)] = {
  325. [C(OP_READ)] = {
  326. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  327. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  328. },
  329. [C(OP_WRITE)] = {
  330. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  331. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  332. },
  333. [C(OP_PREFETCH)] = {
  334. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  335. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  336. },
  337. },
  338. };
  339. static inline unsigned long
  340. armv6_pmcr_read(void)
  341. {
  342. u32 val;
  343. asm volatile("mrc p15, 0, %0, c15, c12, 0" : "=r"(val));
  344. return val;
  345. }
  346. static inline void
  347. armv6_pmcr_write(unsigned long val)
  348. {
  349. asm volatile("mcr p15, 0, %0, c15, c12, 0" : : "r"(val));
  350. }
  351. #define ARMV6_PMCR_ENABLE (1 << 0)
  352. #define ARMV6_PMCR_CTR01_RESET (1 << 1)
  353. #define ARMV6_PMCR_CCOUNT_RESET (1 << 2)
  354. #define ARMV6_PMCR_CCOUNT_DIV (1 << 3)
  355. #define ARMV6_PMCR_COUNT0_IEN (1 << 4)
  356. #define ARMV6_PMCR_COUNT1_IEN (1 << 5)
  357. #define ARMV6_PMCR_CCOUNT_IEN (1 << 6)
  358. #define ARMV6_PMCR_COUNT0_OVERFLOW (1 << 8)
  359. #define ARMV6_PMCR_COUNT1_OVERFLOW (1 << 9)
  360. #define ARMV6_PMCR_CCOUNT_OVERFLOW (1 << 10)
  361. #define ARMV6_PMCR_EVT_COUNT0_SHIFT 20
  362. #define ARMV6_PMCR_EVT_COUNT0_MASK (0xFF << ARMV6_PMCR_EVT_COUNT0_SHIFT)
  363. #define ARMV6_PMCR_EVT_COUNT1_SHIFT 12
  364. #define ARMV6_PMCR_EVT_COUNT1_MASK (0xFF << ARMV6_PMCR_EVT_COUNT1_SHIFT)
  365. #define ARMV6_PMCR_OVERFLOWED_MASK \
  366. (ARMV6_PMCR_COUNT0_OVERFLOW | ARMV6_PMCR_COUNT1_OVERFLOW | \
  367. ARMV6_PMCR_CCOUNT_OVERFLOW)
  368. static inline int
  369. armv6_pmcr_has_overflowed(unsigned long pmcr)
  370. {
  371. return pmcr & ARMV6_PMCR_OVERFLOWED_MASK;
  372. }
  373. static inline int
  374. armv6_pmcr_counter_has_overflowed(unsigned long pmcr,
  375. enum armv6_counters counter)
  376. {
  377. int ret = 0;
  378. if (ARMV6_CYCLE_COUNTER == counter)
  379. ret = pmcr & ARMV6_PMCR_CCOUNT_OVERFLOW;
  380. else if (ARMV6_COUNTER0 == counter)
  381. ret = pmcr & ARMV6_PMCR_COUNT0_OVERFLOW;
  382. else if (ARMV6_COUNTER1 == counter)
  383. ret = pmcr & ARMV6_PMCR_COUNT1_OVERFLOW;
  384. else
  385. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  386. return ret;
  387. }
  388. static inline u32 armv6pmu_read_counter(struct perf_event *event)
  389. {
  390. struct hw_perf_event *hwc = &event->hw;
  391. int counter = hwc->idx;
  392. unsigned long value = 0;
  393. if (ARMV6_CYCLE_COUNTER == counter)
  394. asm volatile("mrc p15, 0, %0, c15, c12, 1" : "=r"(value));
  395. else if (ARMV6_COUNTER0 == counter)
  396. asm volatile("mrc p15, 0, %0, c15, c12, 2" : "=r"(value));
  397. else if (ARMV6_COUNTER1 == counter)
  398. asm volatile("mrc p15, 0, %0, c15, c12, 3" : "=r"(value));
  399. else
  400. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  401. return value;
  402. }
  403. static inline void armv6pmu_write_counter(struct perf_event *event, u32 value)
  404. {
  405. struct hw_perf_event *hwc = &event->hw;
  406. int counter = hwc->idx;
  407. if (ARMV6_CYCLE_COUNTER == counter)
  408. asm volatile("mcr p15, 0, %0, c15, c12, 1" : : "r"(value));
  409. else if (ARMV6_COUNTER0 == counter)
  410. asm volatile("mcr p15, 0, %0, c15, c12, 2" : : "r"(value));
  411. else if (ARMV6_COUNTER1 == counter)
  412. asm volatile("mcr p15, 0, %0, c15, c12, 3" : : "r"(value));
  413. else
  414. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  415. }
  416. static void armv6pmu_enable_event(struct perf_event *event)
  417. {
  418. unsigned long val, mask, evt, flags;
  419. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  420. struct hw_perf_event *hwc = &event->hw;
  421. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  422. int idx = hwc->idx;
  423. if (ARMV6_CYCLE_COUNTER == idx) {
  424. mask = 0;
  425. evt = ARMV6_PMCR_CCOUNT_IEN;
  426. } else if (ARMV6_COUNTER0 == idx) {
  427. mask = ARMV6_PMCR_EVT_COUNT0_MASK;
  428. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT0_SHIFT) |
  429. ARMV6_PMCR_COUNT0_IEN;
  430. } else if (ARMV6_COUNTER1 == idx) {
  431. mask = ARMV6_PMCR_EVT_COUNT1_MASK;
  432. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT1_SHIFT) |
  433. ARMV6_PMCR_COUNT1_IEN;
  434. } else {
  435. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  436. return;
  437. }
  438. /*
  439. * Mask out the current event and set the counter to count the event
  440. * that we're interested in.
  441. */
  442. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  443. val = armv6_pmcr_read();
  444. val &= ~mask;
  445. val |= evt;
  446. armv6_pmcr_write(val);
  447. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  448. }
  449. static irqreturn_t
  450. armv6pmu_handle_irq(int irq_num,
  451. void *dev)
  452. {
  453. unsigned long pmcr = armv6_pmcr_read();
  454. struct perf_sample_data data;
  455. struct arm_pmu *cpu_pmu = (struct arm_pmu *)dev;
  456. struct pmu_hw_events *cpuc = cpu_pmu->get_hw_events();
  457. struct pt_regs *regs;
  458. int idx;
  459. if (!armv6_pmcr_has_overflowed(pmcr))
  460. return IRQ_NONE;
  461. regs = get_irq_regs();
  462. /*
  463. * The interrupts are cleared by writing the overflow flags back to
  464. * the control register. All of the other bits don't have any effect
  465. * if they are rewritten, so write the whole value back.
  466. */
  467. armv6_pmcr_write(pmcr);
  468. for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
  469. struct perf_event *event = cpuc->events[idx];
  470. struct hw_perf_event *hwc;
  471. /* Ignore if we don't have an event. */
  472. if (!event)
  473. continue;
  474. /*
  475. * We have a single interrupt for all counters. Check that
  476. * each counter has overflowed before we process it.
  477. */
  478. if (!armv6_pmcr_counter_has_overflowed(pmcr, idx))
  479. continue;
  480. hwc = &event->hw;
  481. armpmu_event_update(event);
  482. perf_sample_data_init(&data, 0, hwc->last_period);
  483. if (!armpmu_event_set_period(event))
  484. continue;
  485. if (perf_event_overflow(event, &data, regs))
  486. cpu_pmu->disable(event);
  487. }
  488. /*
  489. * Handle the pending perf events.
  490. *
  491. * Note: this call *must* be run with interrupts disabled. For
  492. * platforms that can have the PMU interrupts raised as an NMI, this
  493. * will not work.
  494. */
  495. irq_work_run();
  496. return IRQ_HANDLED;
  497. }
  498. static void armv6pmu_start(struct arm_pmu *cpu_pmu)
  499. {
  500. unsigned long flags, val;
  501. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  502. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  503. val = armv6_pmcr_read();
  504. val |= ARMV6_PMCR_ENABLE;
  505. armv6_pmcr_write(val);
  506. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  507. }
  508. static void armv6pmu_stop(struct arm_pmu *cpu_pmu)
  509. {
  510. unsigned long flags, val;
  511. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  512. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  513. val = armv6_pmcr_read();
  514. val &= ~ARMV6_PMCR_ENABLE;
  515. armv6_pmcr_write(val);
  516. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  517. }
  518. static int
  519. armv6pmu_get_event_idx(struct pmu_hw_events *cpuc,
  520. struct perf_event *event)
  521. {
  522. struct hw_perf_event *hwc = &event->hw;
  523. /* Always place a cycle counter into the cycle counter. */
  524. if (ARMV6_PERFCTR_CPU_CYCLES == hwc->config_base) {
  525. if (test_and_set_bit(ARMV6_CYCLE_COUNTER, cpuc->used_mask))
  526. return -EAGAIN;
  527. return ARMV6_CYCLE_COUNTER;
  528. } else {
  529. /*
  530. * For anything other than a cycle counter, try and use
  531. * counter0 and counter1.
  532. */
  533. if (!test_and_set_bit(ARMV6_COUNTER1, cpuc->used_mask))
  534. return ARMV6_COUNTER1;
  535. if (!test_and_set_bit(ARMV6_COUNTER0, cpuc->used_mask))
  536. return ARMV6_COUNTER0;
  537. /* The counters are all in use. */
  538. return -EAGAIN;
  539. }
  540. }
  541. static void armv6pmu_disable_event(struct perf_event *event)
  542. {
  543. unsigned long val, mask, evt, flags;
  544. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  545. struct hw_perf_event *hwc = &event->hw;
  546. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  547. int idx = hwc->idx;
  548. if (ARMV6_CYCLE_COUNTER == idx) {
  549. mask = ARMV6_PMCR_CCOUNT_IEN;
  550. evt = 0;
  551. } else if (ARMV6_COUNTER0 == idx) {
  552. mask = ARMV6_PMCR_COUNT0_IEN | ARMV6_PMCR_EVT_COUNT0_MASK;
  553. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT0_SHIFT;
  554. } else if (ARMV6_COUNTER1 == idx) {
  555. mask = ARMV6_PMCR_COUNT1_IEN | ARMV6_PMCR_EVT_COUNT1_MASK;
  556. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT1_SHIFT;
  557. } else {
  558. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  559. return;
  560. }
  561. /*
  562. * Mask out the current event and set the counter to count the number
  563. * of ETM bus signal assertion cycles. The external reporting should
  564. * be disabled and so this should never increment.
  565. */
  566. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  567. val = armv6_pmcr_read();
  568. val &= ~mask;
  569. val |= evt;
  570. armv6_pmcr_write(val);
  571. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  572. }
  573. static void armv6mpcore_pmu_disable_event(struct perf_event *event)
  574. {
  575. unsigned long val, mask, flags, evt = 0;
  576. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  577. struct hw_perf_event *hwc = &event->hw;
  578. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  579. int idx = hwc->idx;
  580. if (ARMV6_CYCLE_COUNTER == idx) {
  581. mask = ARMV6_PMCR_CCOUNT_IEN;
  582. } else if (ARMV6_COUNTER0 == idx) {
  583. mask = ARMV6_PMCR_COUNT0_IEN;
  584. } else if (ARMV6_COUNTER1 == idx) {
  585. mask = ARMV6_PMCR_COUNT1_IEN;
  586. } else {
  587. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  588. return;
  589. }
  590. /*
  591. * Unlike UP ARMv6, we don't have a way of stopping the counters. We
  592. * simply disable the interrupt reporting.
  593. */
  594. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  595. val = armv6_pmcr_read();
  596. val &= ~mask;
  597. val |= evt;
  598. armv6_pmcr_write(val);
  599. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  600. }
  601. static int armv6_map_event(struct perf_event *event)
  602. {
  603. return armpmu_map_event(event, &armv6_perf_map,
  604. &armv6_perf_cache_map, 0xFF);
  605. }
  606. static int armv6pmu_init(struct arm_pmu *cpu_pmu)
  607. {
  608. cpu_pmu->name = "v6";
  609. cpu_pmu->handle_irq = armv6pmu_handle_irq;
  610. cpu_pmu->enable = armv6pmu_enable_event;
  611. cpu_pmu->disable = armv6pmu_disable_event;
  612. cpu_pmu->read_counter = armv6pmu_read_counter;
  613. cpu_pmu->write_counter = armv6pmu_write_counter;
  614. cpu_pmu->get_event_idx = armv6pmu_get_event_idx;
  615. cpu_pmu->start = armv6pmu_start;
  616. cpu_pmu->stop = armv6pmu_stop;
  617. cpu_pmu->map_event = armv6_map_event;
  618. cpu_pmu->num_events = 3;
  619. cpu_pmu->max_period = (1LLU << 32) - 1;
  620. return 0;
  621. }
  622. /*
  623. * ARMv6mpcore is almost identical to single core ARMv6 with the exception
  624. * that some of the events have different enumerations and that there is no
  625. * *hack* to stop the programmable counters. To stop the counters we simply
  626. * disable the interrupt reporting and update the event. When unthrottling we
  627. * reset the period and enable the interrupt reporting.
  628. */
  629. static int armv6mpcore_map_event(struct perf_event *event)
  630. {
  631. return armpmu_map_event(event, &armv6mpcore_perf_map,
  632. &armv6mpcore_perf_cache_map, 0xFF);
  633. }
  634. static int armv6mpcore_pmu_init(struct arm_pmu *cpu_pmu)
  635. {
  636. cpu_pmu->name = "v6mpcore";
  637. cpu_pmu->handle_irq = armv6pmu_handle_irq;
  638. cpu_pmu->enable = armv6pmu_enable_event;
  639. cpu_pmu->disable = armv6mpcore_pmu_disable_event;
  640. cpu_pmu->read_counter = armv6pmu_read_counter;
  641. cpu_pmu->write_counter = armv6pmu_write_counter;
  642. cpu_pmu->get_event_idx = armv6pmu_get_event_idx;
  643. cpu_pmu->start = armv6pmu_start;
  644. cpu_pmu->stop = armv6pmu_stop;
  645. cpu_pmu->map_event = armv6mpcore_map_event;
  646. cpu_pmu->num_events = 3;
  647. cpu_pmu->max_period = (1LLU << 32) - 1;
  648. return 0;
  649. }
  650. #else
  651. static int armv6pmu_init(struct arm_pmu *cpu_pmu)
  652. {
  653. return -ENODEV;
  654. }
  655. static int armv6mpcore_pmu_init(struct arm_pmu *cpu_pmu)
  656. {
  657. return -ENODEV;
  658. }
  659. #endif /* CONFIG_CPU_V6 || CONFIG_CPU_V6K */