perf_event.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Performance events support for SH-4A performance counters
  4. *
  5. * Copyright (C) 2009, 2010 Paul Mundt
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/io.h>
  10. #include <linux/irq.h>
  11. #include <linux/perf_event.h>
  12. #include <asm/processor.h>
  13. #define PPC_CCBR(idx) (0xff200800 + (sizeof(u32) * idx))
  14. #define PPC_PMCTR(idx) (0xfc100000 + (sizeof(u32) * idx))
  15. #define CCBR_CIT_MASK (0x7ff << 6)
  16. #define CCBR_DUC (1 << 3)
  17. #define CCBR_CMDS (1 << 1)
  18. #define CCBR_PPCE (1 << 0)
  19. #ifdef CONFIG_CPU_SHX3
  20. /*
  21. * The PMCAT location for SH-X3 CPUs was quietly moved, while the CCBR
  22. * and PMCTR locations remains tentatively constant. This change remains
  23. * wholly undocumented, and was simply found through trial and error.
  24. *
  25. * Early cuts of SH-X3 still appear to use the SH-X/SH-X2 locations, and
  26. * it's unclear when this ceased to be the case. For now we always use
  27. * the new location (if future parts keep up with this trend then
  28. * scanning for them at runtime also remains a viable option.)
  29. *
  30. * The gap in the register space also suggests that there are other
  31. * undocumented counters, so this will need to be revisited at a later
  32. * point in time.
  33. */
  34. #define PPC_PMCAT 0xfc100240
  35. #else
  36. #define PPC_PMCAT 0xfc100080
  37. #endif
  38. #define PMCAT_OVF3 (1 << 27)
  39. #define PMCAT_CNN3 (1 << 26)
  40. #define PMCAT_CLR3 (1 << 25)
  41. #define PMCAT_OVF2 (1 << 19)
  42. #define PMCAT_CLR2 (1 << 17)
  43. #define PMCAT_OVF1 (1 << 11)
  44. #define PMCAT_CNN1 (1 << 10)
  45. #define PMCAT_CLR1 (1 << 9)
  46. #define PMCAT_OVF0 (1 << 3)
  47. #define PMCAT_CLR0 (1 << 1)
  48. static struct sh_pmu sh4a_pmu;
  49. /*
  50. * Supported raw event codes:
  51. *
  52. * Event Code Description
  53. * ---------- -----------
  54. *
  55. * 0x0000 number of elapsed cycles
  56. * 0x0200 number of elapsed cycles in privileged mode
  57. * 0x0280 number of elapsed cycles while SR.BL is asserted
  58. * 0x0202 instruction execution
  59. * 0x0203 instruction execution in parallel
  60. * 0x0204 number of unconditional branches
  61. * 0x0208 number of exceptions
  62. * 0x0209 number of interrupts
  63. * 0x0220 UTLB miss caused by instruction fetch
  64. * 0x0222 UTLB miss caused by operand access
  65. * 0x02a0 number of ITLB misses
  66. * 0x0028 number of accesses to instruction memories
  67. * 0x0029 number of accesses to instruction cache
  68. * 0x002a instruction cache miss
  69. * 0x022e number of access to instruction X/Y memory
  70. * 0x0030 number of reads to operand memories
  71. * 0x0038 number of writes to operand memories
  72. * 0x0031 number of operand cache read accesses
  73. * 0x0039 number of operand cache write accesses
  74. * 0x0032 operand cache read miss
  75. * 0x003a operand cache write miss
  76. * 0x0236 number of reads to operand X/Y memory
  77. * 0x023e number of writes to operand X/Y memory
  78. * 0x0237 number of reads to operand U memory
  79. * 0x023f number of writes to operand U memory
  80. * 0x0337 number of U memory read buffer misses
  81. * 0x02b4 number of wait cycles due to operand read access
  82. * 0x02bc number of wait cycles due to operand write access
  83. * 0x0033 number of wait cycles due to operand cache read miss
  84. * 0x003b number of wait cycles due to operand cache write miss
  85. */
  86. /*
  87. * Special reserved bits used by hardware emulators, read values will
  88. * vary, but writes must always be 0.
  89. */
  90. #define PMCAT_EMU_CLR_MASK ((1 << 24) | (1 << 16) | (1 << 8) | (1 << 0))
  91. static const int sh4a_general_events[] = {
  92. [PERF_COUNT_HW_CPU_CYCLES] = 0x0000,
  93. [PERF_COUNT_HW_INSTRUCTIONS] = 0x0202,
  94. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0029, /* I-cache */
  95. [PERF_COUNT_HW_CACHE_MISSES] = 0x002a, /* I-cache */
  96. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x0204,
  97. [PERF_COUNT_HW_BRANCH_MISSES] = -1,
  98. [PERF_COUNT_HW_BUS_CYCLES] = -1,
  99. };
  100. #define C(x) PERF_COUNT_HW_CACHE_##x
  101. static const int sh4a_cache_events
  102. [PERF_COUNT_HW_CACHE_MAX]
  103. [PERF_COUNT_HW_CACHE_OP_MAX]
  104. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  105. {
  106. [ C(L1D) ] = {
  107. [ C(OP_READ) ] = {
  108. [ C(RESULT_ACCESS) ] = 0x0031,
  109. [ C(RESULT_MISS) ] = 0x0032,
  110. },
  111. [ C(OP_WRITE) ] = {
  112. [ C(RESULT_ACCESS) ] = 0x0039,
  113. [ C(RESULT_MISS) ] = 0x003a,
  114. },
  115. [ C(OP_PREFETCH) ] = {
  116. [ C(RESULT_ACCESS) ] = 0,
  117. [ C(RESULT_MISS) ] = 0,
  118. },
  119. },
  120. [ C(L1I) ] = {
  121. [ C(OP_READ) ] = {
  122. [ C(RESULT_ACCESS) ] = 0x0029,
  123. [ C(RESULT_MISS) ] = 0x002a,
  124. },
  125. [ C(OP_WRITE) ] = {
  126. [ C(RESULT_ACCESS) ] = -1,
  127. [ C(RESULT_MISS) ] = -1,
  128. },
  129. [ C(OP_PREFETCH) ] = {
  130. [ C(RESULT_ACCESS) ] = 0,
  131. [ C(RESULT_MISS) ] = 0,
  132. },
  133. },
  134. [ C(LL) ] = {
  135. [ C(OP_READ) ] = {
  136. [ C(RESULT_ACCESS) ] = 0x0030,
  137. [ C(RESULT_MISS) ] = 0,
  138. },
  139. [ C(OP_WRITE) ] = {
  140. [ C(RESULT_ACCESS) ] = 0x0038,
  141. [ C(RESULT_MISS) ] = 0,
  142. },
  143. [ C(OP_PREFETCH) ] = {
  144. [ C(RESULT_ACCESS) ] = 0,
  145. [ C(RESULT_MISS) ] = 0,
  146. },
  147. },
  148. [ C(DTLB) ] = {
  149. [ C(OP_READ) ] = {
  150. [ C(RESULT_ACCESS) ] = 0x0222,
  151. [ C(RESULT_MISS) ] = 0x0220,
  152. },
  153. [ C(OP_WRITE) ] = {
  154. [ C(RESULT_ACCESS) ] = 0,
  155. [ C(RESULT_MISS) ] = 0,
  156. },
  157. [ C(OP_PREFETCH) ] = {
  158. [ C(RESULT_ACCESS) ] = 0,
  159. [ C(RESULT_MISS) ] = 0,
  160. },
  161. },
  162. [ C(ITLB) ] = {
  163. [ C(OP_READ) ] = {
  164. [ C(RESULT_ACCESS) ] = 0,
  165. [ C(RESULT_MISS) ] = 0x02a0,
  166. },
  167. [ C(OP_WRITE) ] = {
  168. [ C(RESULT_ACCESS) ] = -1,
  169. [ C(RESULT_MISS) ] = -1,
  170. },
  171. [ C(OP_PREFETCH) ] = {
  172. [ C(RESULT_ACCESS) ] = -1,
  173. [ C(RESULT_MISS) ] = -1,
  174. },
  175. },
  176. [ C(BPU) ] = {
  177. [ C(OP_READ) ] = {
  178. [ C(RESULT_ACCESS) ] = -1,
  179. [ C(RESULT_MISS) ] = -1,
  180. },
  181. [ C(OP_WRITE) ] = {
  182. [ C(RESULT_ACCESS) ] = -1,
  183. [ C(RESULT_MISS) ] = -1,
  184. },
  185. [ C(OP_PREFETCH) ] = {
  186. [ C(RESULT_ACCESS) ] = -1,
  187. [ C(RESULT_MISS) ] = -1,
  188. },
  189. },
  190. [ C(NODE) ] = {
  191. [ C(OP_READ) ] = {
  192. [ C(RESULT_ACCESS) ] = -1,
  193. [ C(RESULT_MISS) ] = -1,
  194. },
  195. [ C(OP_WRITE) ] = {
  196. [ C(RESULT_ACCESS) ] = -1,
  197. [ C(RESULT_MISS) ] = -1,
  198. },
  199. [ C(OP_PREFETCH) ] = {
  200. [ C(RESULT_ACCESS) ] = -1,
  201. [ C(RESULT_MISS) ] = -1,
  202. },
  203. },
  204. };
  205. static int sh4a_event_map(int event)
  206. {
  207. return sh4a_general_events[event];
  208. }
  209. static u64 sh4a_pmu_read(int idx)
  210. {
  211. return __raw_readl(PPC_PMCTR(idx));
  212. }
  213. static void sh4a_pmu_disable(struct hw_perf_event *hwc, int idx)
  214. {
  215. unsigned int tmp;
  216. tmp = __raw_readl(PPC_CCBR(idx));
  217. tmp &= ~(CCBR_CIT_MASK | CCBR_DUC);
  218. __raw_writel(tmp, PPC_CCBR(idx));
  219. }
  220. static void sh4a_pmu_enable(struct hw_perf_event *hwc, int idx)
  221. {
  222. unsigned int tmp;
  223. tmp = __raw_readl(PPC_PMCAT);
  224. tmp &= ~PMCAT_EMU_CLR_MASK;
  225. tmp |= idx ? PMCAT_CLR1 : PMCAT_CLR0;
  226. __raw_writel(tmp, PPC_PMCAT);
  227. tmp = __raw_readl(PPC_CCBR(idx));
  228. tmp |= (hwc->config << 6) | CCBR_CMDS | CCBR_PPCE;
  229. __raw_writel(tmp, PPC_CCBR(idx));
  230. __raw_writel(__raw_readl(PPC_CCBR(idx)) | CCBR_DUC, PPC_CCBR(idx));
  231. }
  232. static void sh4a_pmu_disable_all(void)
  233. {
  234. int i;
  235. for (i = 0; i < sh4a_pmu.num_events; i++)
  236. __raw_writel(__raw_readl(PPC_CCBR(i)) & ~CCBR_DUC, PPC_CCBR(i));
  237. }
  238. static void sh4a_pmu_enable_all(void)
  239. {
  240. int i;
  241. for (i = 0; i < sh4a_pmu.num_events; i++)
  242. __raw_writel(__raw_readl(PPC_CCBR(i)) | CCBR_DUC, PPC_CCBR(i));
  243. }
  244. static struct sh_pmu sh4a_pmu = {
  245. .name = "sh4a",
  246. .num_events = 2,
  247. .event_map = sh4a_event_map,
  248. .max_events = ARRAY_SIZE(sh4a_general_events),
  249. .raw_event_mask = 0x3ff,
  250. .cache_events = &sh4a_cache_events,
  251. .read = sh4a_pmu_read,
  252. .disable = sh4a_pmu_disable,
  253. .enable = sh4a_pmu_enable,
  254. .disable_all = sh4a_pmu_disable_all,
  255. .enable_all = sh4a_pmu_enable_all,
  256. };
  257. static int __init sh4a_pmu_init(void)
  258. {
  259. /*
  260. * Make sure this CPU actually has perf counters.
  261. */
  262. if (!(boot_cpu_data.flags & CPU_HAS_PERF_COUNTER)) {
  263. pr_notice("HW perf events unsupported, software events only.\n");
  264. return -ENODEV;
  265. }
  266. return register_sh_pmu(&sh4a_pmu);
  267. }
  268. early_initcall(sh4a_pmu_init);