perf_event_v6.c 19 KB

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