perf_event_v6.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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 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 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 irqreturn_t
  448. armv6pmu_handle_irq(int irq_num,
  449. void *dev)
  450. {
  451. unsigned long pmcr = armv6_pmcr_read();
  452. struct perf_sample_data data;
  453. struct pmu_hw_events *cpuc;
  454. struct pt_regs *regs;
  455. int idx;
  456. if (!armv6_pmcr_has_overflowed(pmcr))
  457. return IRQ_NONE;
  458. regs = get_irq_regs();
  459. /*
  460. * The interrupts are cleared by writing the overflow flags back to
  461. * the control register. All of the other bits don't have any effect
  462. * if they are rewritten, so write the whole value back.
  463. */
  464. armv6_pmcr_write(pmcr);
  465. perf_sample_data_init(&data, 0);
  466. cpuc = &__get_cpu_var(cpu_hw_events);
  467. for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
  468. struct perf_event *event = cpuc->events[idx];
  469. struct hw_perf_event *hwc;
  470. /* Ignore if we don't have an event. */
  471. if (!event)
  472. continue;
  473. /*
  474. * We have a single interrupt for all counters. Check that
  475. * each counter has overflowed before we process it.
  476. */
  477. if (!armv6_pmcr_counter_has_overflowed(pmcr, idx))
  478. continue;
  479. hwc = &event->hw;
  480. armpmu_event_update(event, hwc, idx);
  481. data.period = event->hw.last_period;
  482. if (!armpmu_event_set_period(event, hwc, idx))
  483. continue;
  484. if (perf_event_overflow(event, &data, regs))
  485. cpu_pmu->disable(hwc, idx);
  486. }
  487. /*
  488. * Handle the pending perf events.
  489. *
  490. * Note: this call *must* be run with interrupts disabled. For
  491. * platforms that can have the PMU interrupts raised as an NMI, this
  492. * will not work.
  493. */
  494. irq_work_run();
  495. return IRQ_HANDLED;
  496. }
  497. static void
  498. armv6pmu_start(void)
  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
  509. armv6pmu_stop(void)
  510. {
  511. unsigned long flags, val;
  512. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  513. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  514. val = armv6_pmcr_read();
  515. val &= ~ARMV6_PMCR_ENABLE;
  516. armv6_pmcr_write(val);
  517. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  518. }
  519. static int
  520. armv6pmu_get_event_idx(struct pmu_hw_events *cpuc,
  521. struct hw_perf_event *event)
  522. {
  523. /* Always place a cycle counter into the cycle counter. */
  524. if (ARMV6_PERFCTR_CPU_CYCLES == event->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
  542. armv6pmu_disable_event(struct hw_perf_event *hwc,
  543. int idx)
  544. {
  545. unsigned long val, mask, evt, flags;
  546. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  547. if (ARMV6_CYCLE_COUNTER == idx) {
  548. mask = ARMV6_PMCR_CCOUNT_IEN;
  549. evt = 0;
  550. } else if (ARMV6_COUNTER0 == idx) {
  551. mask = ARMV6_PMCR_COUNT0_IEN | ARMV6_PMCR_EVT_COUNT0_MASK;
  552. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT0_SHIFT;
  553. } else if (ARMV6_COUNTER1 == idx) {
  554. mask = ARMV6_PMCR_COUNT1_IEN | ARMV6_PMCR_EVT_COUNT1_MASK;
  555. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT1_SHIFT;
  556. } else {
  557. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  558. return;
  559. }
  560. /*
  561. * Mask out the current event and set the counter to count the number
  562. * of ETM bus signal assertion cycles. The external reporting should
  563. * be disabled and so this should never increment.
  564. */
  565. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  566. val = armv6_pmcr_read();
  567. val &= ~mask;
  568. val |= evt;
  569. armv6_pmcr_write(val);
  570. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  571. }
  572. static void
  573. armv6mpcore_pmu_disable_event(struct hw_perf_event *hwc,
  574. int idx)
  575. {
  576. unsigned long val, mask, flags, evt = 0;
  577. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  578. if (ARMV6_CYCLE_COUNTER == idx) {
  579. mask = ARMV6_PMCR_CCOUNT_IEN;
  580. } else if (ARMV6_COUNTER0 == idx) {
  581. mask = ARMV6_PMCR_COUNT0_IEN;
  582. } else if (ARMV6_COUNTER1 == idx) {
  583. mask = ARMV6_PMCR_COUNT1_IEN;
  584. } else {
  585. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  586. return;
  587. }
  588. /*
  589. * Unlike UP ARMv6, we don't have a way of stopping the counters. We
  590. * simply disable the interrupt reporting.
  591. */
  592. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  593. val = armv6_pmcr_read();
  594. val &= ~mask;
  595. val |= evt;
  596. armv6_pmcr_write(val);
  597. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  598. }
  599. static int armv6_map_event(struct perf_event *event)
  600. {
  601. return map_cpu_event(event, &armv6_perf_map,
  602. &armv6_perf_cache_map, 0xFF);
  603. }
  604. static struct arm_pmu armv6pmu = {
  605. .id = ARM_PERF_PMU_ID_V6,
  606. .name = "v6",
  607. .handle_irq = armv6pmu_handle_irq,
  608. .request_pmu_irq = armpmu_generic_request_irq,
  609. .free_pmu_irq = armpmu_generic_free_irq,
  610. .enable = armv6pmu_enable_event,
  611. .disable = armv6pmu_disable_event,
  612. .read_counter = armv6pmu_read_counter,
  613. .write_counter = armv6pmu_write_counter,
  614. .get_event_idx = armv6pmu_get_event_idx,
  615. .start = armv6pmu_start,
  616. .stop = armv6pmu_stop,
  617. .map_event = armv6_map_event,
  618. .num_events = 3,
  619. .max_period = (1LLU << 32) - 1,
  620. };
  621. static struct arm_pmu *__init armv6pmu_init(void)
  622. {
  623. return &armv6pmu;
  624. }
  625. /*
  626. * ARMv6mpcore is almost identical to single core ARMv6 with the exception
  627. * that some of the events have different enumerations and that there is no
  628. * *hack* to stop the programmable counters. To stop the counters we simply
  629. * disable the interrupt reporting and update the event. When unthrottling we
  630. * reset the period and enable the interrupt reporting.
  631. */
  632. static int armv6mpcore_map_event(struct perf_event *event)
  633. {
  634. return map_cpu_event(event, &armv6mpcore_perf_map,
  635. &armv6mpcore_perf_cache_map, 0xFF);
  636. }
  637. static struct arm_pmu armv6mpcore_pmu = {
  638. .id = ARM_PERF_PMU_ID_V6MP,
  639. .name = "v6mpcore",
  640. .handle_irq = armv6pmu_handle_irq,
  641. .request_pmu_irq = armpmu_generic_request_irq,
  642. .free_pmu_irq = armpmu_generic_free_irq,
  643. .enable = armv6pmu_enable_event,
  644. .disable = armv6mpcore_pmu_disable_event,
  645. .read_counter = armv6pmu_read_counter,
  646. .write_counter = armv6pmu_write_counter,
  647. .get_event_idx = armv6pmu_get_event_idx,
  648. .start = armv6pmu_start,
  649. .stop = armv6pmu_stop,
  650. .map_event = armv6mpcore_map_event,
  651. .num_events = 3,
  652. .max_period = (1LLU << 32) - 1,
  653. };
  654. static struct arm_pmu *__init armv6mpcore_pmu_init(void)
  655. {
  656. return &armv6mpcore_pmu;
  657. }
  658. #else
  659. static struct arm_pmu *__init armv6pmu_init(void)
  660. {
  661. return NULL;
  662. }
  663. static struct arm_pmu *__init armv6mpcore_pmu_init(void)
  664. {
  665. return NULL;
  666. }
  667. #endif /* CONFIG_CPU_V6 || CONFIG_CPU_V6K */