perf_event_v6.c 20 KB

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