perf_event_v6.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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)] = ARMV6_PERFCTR_ICACHE_MISS,
  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)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
  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
  389. armv6pmu_read_counter(int counter)
  390. {
  391. unsigned long value = 0;
  392. if (ARMV6_CYCLE_COUNTER == counter)
  393. asm volatile("mrc p15, 0, %0, c15, c12, 1" : "=r"(value));
  394. else if (ARMV6_COUNTER0 == counter)
  395. asm volatile("mrc p15, 0, %0, c15, c12, 2" : "=r"(value));
  396. else if (ARMV6_COUNTER1 == counter)
  397. asm volatile("mrc p15, 0, %0, c15, c12, 3" : "=r"(value));
  398. else
  399. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  400. return value;
  401. }
  402. static inline void
  403. armv6pmu_write_counter(int counter,
  404. u32 value)
  405. {
  406. if (ARMV6_CYCLE_COUNTER == counter)
  407. asm volatile("mcr p15, 0, %0, c15, c12, 1" : : "r"(value));
  408. else if (ARMV6_COUNTER0 == counter)
  409. asm volatile("mcr p15, 0, %0, c15, c12, 2" : : "r"(value));
  410. else if (ARMV6_COUNTER1 == counter)
  411. asm volatile("mcr p15, 0, %0, c15, c12, 3" : : "r"(value));
  412. else
  413. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  414. }
  415. static void
  416. armv6pmu_enable_event(struct hw_perf_event *hwc,
  417. int idx)
  418. {
  419. unsigned long val, mask, evt, flags;
  420. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  421. if (ARMV6_CYCLE_COUNTER == idx) {
  422. mask = 0;
  423. evt = ARMV6_PMCR_CCOUNT_IEN;
  424. } else if (ARMV6_COUNTER0 == idx) {
  425. mask = ARMV6_PMCR_EVT_COUNT0_MASK;
  426. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT0_SHIFT) |
  427. ARMV6_PMCR_COUNT0_IEN;
  428. } else if (ARMV6_COUNTER1 == idx) {
  429. mask = ARMV6_PMCR_EVT_COUNT1_MASK;
  430. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT1_SHIFT) |
  431. ARMV6_PMCR_COUNT1_IEN;
  432. } else {
  433. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  434. return;
  435. }
  436. /*
  437. * Mask out the current event and set the counter to count the event
  438. * that we're interested in.
  439. */
  440. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  441. val = armv6_pmcr_read();
  442. val &= ~mask;
  443. val |= evt;
  444. armv6_pmcr_write(val);
  445. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  446. }
  447. static int counter_is_active(unsigned long pmcr, int idx)
  448. {
  449. unsigned long mask = 0;
  450. if (idx == ARMV6_CYCLE_COUNTER)
  451. mask = ARMV6_PMCR_CCOUNT_IEN;
  452. else if (idx == ARMV6_COUNTER0)
  453. mask = ARMV6_PMCR_COUNT0_IEN;
  454. else if (idx == ARMV6_COUNTER1)
  455. mask = ARMV6_PMCR_COUNT1_IEN;
  456. if (mask)
  457. return pmcr & mask;
  458. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  459. return 0;
  460. }
  461. static irqreturn_t
  462. armv6pmu_handle_irq(int irq_num,
  463. void *dev)
  464. {
  465. unsigned long pmcr = armv6_pmcr_read();
  466. struct perf_sample_data data;
  467. struct pmu_hw_events *cpuc;
  468. struct pt_regs *regs;
  469. int idx;
  470. if (!armv6_pmcr_has_overflowed(pmcr))
  471. return IRQ_NONE;
  472. regs = get_irq_regs();
  473. /*
  474. * The interrupts are cleared by writing the overflow flags back to
  475. * the control register. All of the other bits don't have any effect
  476. * if they are rewritten, so write the whole value back.
  477. */
  478. armv6_pmcr_write(pmcr);
  479. perf_sample_data_init(&data, 0);
  480. cpuc = &__get_cpu_var(cpu_hw_events);
  481. for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
  482. struct perf_event *event = cpuc->events[idx];
  483. struct hw_perf_event *hwc;
  484. if (!counter_is_active(pmcr, idx))
  485. continue;
  486. /*
  487. * We have a single interrupt for all counters. Check that
  488. * each counter has overflowed before we process it.
  489. */
  490. if (!armv6_pmcr_counter_has_overflowed(pmcr, idx))
  491. continue;
  492. hwc = &event->hw;
  493. armpmu_event_update(event, hwc, idx, 1);
  494. data.period = event->hw.last_period;
  495. if (!armpmu_event_set_period(event, hwc, idx))
  496. continue;
  497. if (perf_event_overflow(event, &data, regs))
  498. cpu_pmu->disable(hwc, idx);
  499. }
  500. /*
  501. * Handle the pending perf events.
  502. *
  503. * Note: this call *must* be run with interrupts disabled. For
  504. * platforms that can have the PMU interrupts raised as an NMI, this
  505. * will not work.
  506. */
  507. irq_work_run();
  508. return IRQ_HANDLED;
  509. }
  510. static void
  511. armv6pmu_start(void)
  512. {
  513. unsigned long flags, val;
  514. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  515. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  516. val = armv6_pmcr_read();
  517. val |= ARMV6_PMCR_ENABLE;
  518. armv6_pmcr_write(val);
  519. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  520. }
  521. static void
  522. armv6pmu_stop(void)
  523. {
  524. unsigned long flags, val;
  525. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  526. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  527. val = armv6_pmcr_read();
  528. val &= ~ARMV6_PMCR_ENABLE;
  529. armv6_pmcr_write(val);
  530. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  531. }
  532. static int
  533. armv6pmu_get_event_idx(struct pmu_hw_events *cpuc,
  534. struct hw_perf_event *event)
  535. {
  536. /* Always place a cycle counter into the cycle counter. */
  537. if (ARMV6_PERFCTR_CPU_CYCLES == event->config_base) {
  538. if (test_and_set_bit(ARMV6_CYCLE_COUNTER, cpuc->used_mask))
  539. return -EAGAIN;
  540. return ARMV6_CYCLE_COUNTER;
  541. } else {
  542. /*
  543. * For anything other than a cycle counter, try and use
  544. * counter0 and counter1.
  545. */
  546. if (!test_and_set_bit(ARMV6_COUNTER1, cpuc->used_mask))
  547. return ARMV6_COUNTER1;
  548. if (!test_and_set_bit(ARMV6_COUNTER0, cpuc->used_mask))
  549. return ARMV6_COUNTER0;
  550. /* The counters are all in use. */
  551. return -EAGAIN;
  552. }
  553. }
  554. static void
  555. armv6pmu_disable_event(struct hw_perf_event *hwc,
  556. int idx)
  557. {
  558. unsigned long val, mask, evt, flags;
  559. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  560. if (ARMV6_CYCLE_COUNTER == idx) {
  561. mask = ARMV6_PMCR_CCOUNT_IEN;
  562. evt = 0;
  563. } else if (ARMV6_COUNTER0 == idx) {
  564. mask = ARMV6_PMCR_COUNT0_IEN | ARMV6_PMCR_EVT_COUNT0_MASK;
  565. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT0_SHIFT;
  566. } else if (ARMV6_COUNTER1 == idx) {
  567. mask = ARMV6_PMCR_COUNT1_IEN | ARMV6_PMCR_EVT_COUNT1_MASK;
  568. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT1_SHIFT;
  569. } else {
  570. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  571. return;
  572. }
  573. /*
  574. * Mask out the current event and set the counter to count the number
  575. * of ETM bus signal assertion cycles. The external reporting should
  576. * be disabled and so this should never increment.
  577. */
  578. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  579. val = armv6_pmcr_read();
  580. val &= ~mask;
  581. val |= evt;
  582. armv6_pmcr_write(val);
  583. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  584. }
  585. static void
  586. armv6mpcore_pmu_disable_event(struct hw_perf_event *hwc,
  587. int idx)
  588. {
  589. unsigned long val, mask, flags, evt = 0;
  590. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  591. if (ARMV6_CYCLE_COUNTER == idx) {
  592. mask = ARMV6_PMCR_CCOUNT_IEN;
  593. } else if (ARMV6_COUNTER0 == idx) {
  594. mask = ARMV6_PMCR_COUNT0_IEN;
  595. } else if (ARMV6_COUNTER1 == idx) {
  596. mask = ARMV6_PMCR_COUNT1_IEN;
  597. } else {
  598. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  599. return;
  600. }
  601. /*
  602. * Unlike UP ARMv6, we don't have a way of stopping the counters. We
  603. * simply disable the interrupt reporting.
  604. */
  605. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  606. val = armv6_pmcr_read();
  607. val &= ~mask;
  608. val |= evt;
  609. armv6_pmcr_write(val);
  610. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  611. }
  612. static int armv6_map_event(struct perf_event *event)
  613. {
  614. return map_cpu_event(event, &armv6_perf_map,
  615. &armv6_perf_cache_map, 0xFF);
  616. }
  617. static struct arm_pmu armv6pmu = {
  618. .id = ARM_PERF_PMU_ID_V6,
  619. .name = "v6",
  620. .handle_irq = armv6pmu_handle_irq,
  621. .enable = armv6pmu_enable_event,
  622. .disable = armv6pmu_disable_event,
  623. .read_counter = armv6pmu_read_counter,
  624. .write_counter = armv6pmu_write_counter,
  625. .get_event_idx = armv6pmu_get_event_idx,
  626. .start = armv6pmu_start,
  627. .stop = armv6pmu_stop,
  628. .map_event = armv6_map_event,
  629. .num_events = 3,
  630. .max_period = (1LLU << 32) - 1,
  631. };
  632. static struct arm_pmu *__init armv6pmu_init(void)
  633. {
  634. return &armv6pmu;
  635. }
  636. /*
  637. * ARMv6mpcore is almost identical to single core ARMv6 with the exception
  638. * that some of the events have different enumerations and that there is no
  639. * *hack* to stop the programmable counters. To stop the counters we simply
  640. * disable the interrupt reporting and update the event. When unthrottling we
  641. * reset the period and enable the interrupt reporting.
  642. */
  643. static int armv6mpcore_map_event(struct perf_event *event)
  644. {
  645. return map_cpu_event(event, &armv6mpcore_perf_map,
  646. &armv6mpcore_perf_cache_map, 0xFF);
  647. }
  648. static struct arm_pmu armv6mpcore_pmu = {
  649. .id = ARM_PERF_PMU_ID_V6MP,
  650. .name = "v6mpcore",
  651. .handle_irq = armv6pmu_handle_irq,
  652. .enable = armv6pmu_enable_event,
  653. .disable = armv6mpcore_pmu_disable_event,
  654. .read_counter = armv6pmu_read_counter,
  655. .write_counter = armv6pmu_write_counter,
  656. .get_event_idx = armv6pmu_get_event_idx,
  657. .start = armv6pmu_start,
  658. .stop = armv6pmu_stop,
  659. .map_event = armv6mpcore_map_event,
  660. .num_events = 3,
  661. .max_period = (1LLU << 32) - 1,
  662. };
  663. static struct arm_pmu *__init armv6mpcore_pmu_init(void)
  664. {
  665. return &armv6mpcore_pmu;
  666. }
  667. #else
  668. static struct arm_pmu *__init armv6pmu_init(void)
  669. {
  670. return NULL;
  671. }
  672. static struct arm_pmu *__init armv6mpcore_pmu_init(void)
  673. {
  674. return NULL;
  675. }
  676. #endif /* CONFIG_CPU_V6 || CONFIG_CPU_V6K */