perf_event_v6.c 21 KB

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