perf_event.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Performance events support for SH7750-style performance counters
  4. *
  5. * Copyright (C) 2009 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 PM_CR_BASE 0xff000084 /* 16-bit */
  14. #define PM_CTR_BASE 0xff100004 /* 32-bit */
  15. #define PMCR(n) (PM_CR_BASE + ((n) * 0x04))
  16. #define PMCTRH(n) (PM_CTR_BASE + 0x00 + ((n) * 0x08))
  17. #define PMCTRL(n) (PM_CTR_BASE + 0x04 + ((n) * 0x08))
  18. #define PMCR_PMM_MASK 0x0000003f
  19. #define PMCR_CLKF 0x00000100
  20. #define PMCR_PMCLR 0x00002000
  21. #define PMCR_PMST 0x00004000
  22. #define PMCR_PMEN 0x00008000
  23. static struct sh_pmu sh7750_pmu;
  24. /*
  25. * There are a number of events supported by each counter (33 in total).
  26. * Since we have 2 counters, each counter will take the event code as it
  27. * corresponds to the PMCR PMM setting. Each counter can be configured
  28. * independently.
  29. *
  30. * Event Code Description
  31. * ---------- -----------
  32. *
  33. * 0x01 Operand read access
  34. * 0x02 Operand write access
  35. * 0x03 UTLB miss
  36. * 0x04 Operand cache read miss
  37. * 0x05 Operand cache write miss
  38. * 0x06 Instruction fetch (w/ cache)
  39. * 0x07 Instruction TLB miss
  40. * 0x08 Instruction cache miss
  41. * 0x09 All operand accesses
  42. * 0x0a All instruction accesses
  43. * 0x0b OC RAM operand access
  44. * 0x0d On-chip I/O space access
  45. * 0x0e Operand access (r/w)
  46. * 0x0f Operand cache miss (r/w)
  47. * 0x10 Branch instruction
  48. * 0x11 Branch taken
  49. * 0x12 BSR/BSRF/JSR
  50. * 0x13 Instruction execution
  51. * 0x14 Instruction execution in parallel
  52. * 0x15 FPU Instruction execution
  53. * 0x16 Interrupt
  54. * 0x17 NMI
  55. * 0x18 trapa instruction execution
  56. * 0x19 UBCA match
  57. * 0x1a UBCB match
  58. * 0x21 Instruction cache fill
  59. * 0x22 Operand cache fill
  60. * 0x23 Elapsed time
  61. * 0x24 Pipeline freeze by I-cache miss
  62. * 0x25 Pipeline freeze by D-cache miss
  63. * 0x27 Pipeline freeze by branch instruction
  64. * 0x28 Pipeline freeze by CPU register
  65. * 0x29 Pipeline freeze by FPU
  66. */
  67. static const int sh7750_general_events[] = {
  68. [PERF_COUNT_HW_CPU_CYCLES] = 0x0023,
  69. [PERF_COUNT_HW_INSTRUCTIONS] = 0x000a,
  70. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0006, /* I-cache */
  71. [PERF_COUNT_HW_CACHE_MISSES] = 0x0008, /* I-cache */
  72. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x0010,
  73. [PERF_COUNT_HW_BRANCH_MISSES] = -1,
  74. [PERF_COUNT_HW_BUS_CYCLES] = -1,
  75. };
  76. #define C(x) PERF_COUNT_HW_CACHE_##x
  77. static const int sh7750_cache_events
  78. [PERF_COUNT_HW_CACHE_MAX]
  79. [PERF_COUNT_HW_CACHE_OP_MAX]
  80. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  81. {
  82. [ C(L1D) ] = {
  83. [ C(OP_READ) ] = {
  84. [ C(RESULT_ACCESS) ] = 0x0001,
  85. [ C(RESULT_MISS) ] = 0x0004,
  86. },
  87. [ C(OP_WRITE) ] = {
  88. [ C(RESULT_ACCESS) ] = 0x0002,
  89. [ C(RESULT_MISS) ] = 0x0005,
  90. },
  91. [ C(OP_PREFETCH) ] = {
  92. [ C(RESULT_ACCESS) ] = 0,
  93. [ C(RESULT_MISS) ] = 0,
  94. },
  95. },
  96. [ C(L1I) ] = {
  97. [ C(OP_READ) ] = {
  98. [ C(RESULT_ACCESS) ] = 0x0006,
  99. [ C(RESULT_MISS) ] = 0x0008,
  100. },
  101. [ C(OP_WRITE) ] = {
  102. [ C(RESULT_ACCESS) ] = -1,
  103. [ C(RESULT_MISS) ] = -1,
  104. },
  105. [ C(OP_PREFETCH) ] = {
  106. [ C(RESULT_ACCESS) ] = 0,
  107. [ C(RESULT_MISS) ] = 0,
  108. },
  109. },
  110. [ C(LL) ] = {
  111. [ C(OP_READ) ] = {
  112. [ C(RESULT_ACCESS) ] = 0,
  113. [ C(RESULT_MISS) ] = 0,
  114. },
  115. [ C(OP_WRITE) ] = {
  116. [ C(RESULT_ACCESS) ] = 0,
  117. [ C(RESULT_MISS) ] = 0,
  118. },
  119. [ C(OP_PREFETCH) ] = {
  120. [ C(RESULT_ACCESS) ] = 0,
  121. [ C(RESULT_MISS) ] = 0,
  122. },
  123. },
  124. [ C(DTLB) ] = {
  125. [ C(OP_READ) ] = {
  126. [ C(RESULT_ACCESS) ] = 0,
  127. [ C(RESULT_MISS) ] = 0x0003,
  128. },
  129. [ C(OP_WRITE) ] = {
  130. [ C(RESULT_ACCESS) ] = 0,
  131. [ C(RESULT_MISS) ] = 0,
  132. },
  133. [ C(OP_PREFETCH) ] = {
  134. [ C(RESULT_ACCESS) ] = 0,
  135. [ C(RESULT_MISS) ] = 0,
  136. },
  137. },
  138. [ C(ITLB) ] = {
  139. [ C(OP_READ) ] = {
  140. [ C(RESULT_ACCESS) ] = 0,
  141. [ C(RESULT_MISS) ] = 0x0007,
  142. },
  143. [ C(OP_WRITE) ] = {
  144. [ C(RESULT_ACCESS) ] = -1,
  145. [ C(RESULT_MISS) ] = -1,
  146. },
  147. [ C(OP_PREFETCH) ] = {
  148. [ C(RESULT_ACCESS) ] = -1,
  149. [ C(RESULT_MISS) ] = -1,
  150. },
  151. },
  152. [ C(BPU) ] = {
  153. [ C(OP_READ) ] = {
  154. [ C(RESULT_ACCESS) ] = -1,
  155. [ C(RESULT_MISS) ] = -1,
  156. },
  157. [ C(OP_WRITE) ] = {
  158. [ C(RESULT_ACCESS) ] = -1,
  159. [ C(RESULT_MISS) ] = -1,
  160. },
  161. [ C(OP_PREFETCH) ] = {
  162. [ C(RESULT_ACCESS) ] = -1,
  163. [ C(RESULT_MISS) ] = -1,
  164. },
  165. },
  166. [ C(NODE) ] = {
  167. [ C(OP_READ) ] = {
  168. [ C(RESULT_ACCESS) ] = -1,
  169. [ C(RESULT_MISS) ] = -1,
  170. },
  171. [ C(OP_WRITE) ] = {
  172. [ C(RESULT_ACCESS) ] = -1,
  173. [ C(RESULT_MISS) ] = -1,
  174. },
  175. [ C(OP_PREFETCH) ] = {
  176. [ C(RESULT_ACCESS) ] = -1,
  177. [ C(RESULT_MISS) ] = -1,
  178. },
  179. },
  180. };
  181. static int sh7750_event_map(int event)
  182. {
  183. return sh7750_general_events[event];
  184. }
  185. static u64 sh7750_pmu_read(int idx)
  186. {
  187. return (u64)((u64)(__raw_readl(PMCTRH(idx)) & 0xffff) << 32) |
  188. __raw_readl(PMCTRL(idx));
  189. }
  190. static void sh7750_pmu_disable(struct hw_perf_event *hwc, int idx)
  191. {
  192. unsigned int tmp;
  193. tmp = __raw_readw(PMCR(idx));
  194. tmp &= ~(PMCR_PMM_MASK | PMCR_PMEN);
  195. __raw_writew(tmp, PMCR(idx));
  196. }
  197. static void sh7750_pmu_enable(struct hw_perf_event *hwc, int idx)
  198. {
  199. __raw_writew(__raw_readw(PMCR(idx)) | PMCR_PMCLR, PMCR(idx));
  200. __raw_writew(hwc->config | PMCR_PMEN | PMCR_PMST, PMCR(idx));
  201. }
  202. static void sh7750_pmu_disable_all(void)
  203. {
  204. int i;
  205. for (i = 0; i < sh7750_pmu.num_events; i++)
  206. __raw_writew(__raw_readw(PMCR(i)) & ~PMCR_PMEN, PMCR(i));
  207. }
  208. static void sh7750_pmu_enable_all(void)
  209. {
  210. int i;
  211. for (i = 0; i < sh7750_pmu.num_events; i++)
  212. __raw_writew(__raw_readw(PMCR(i)) | PMCR_PMEN, PMCR(i));
  213. }
  214. static struct sh_pmu sh7750_pmu = {
  215. .name = "sh7750",
  216. .num_events = 2,
  217. .event_map = sh7750_event_map,
  218. .max_events = ARRAY_SIZE(sh7750_general_events),
  219. .raw_event_mask = PMCR_PMM_MASK,
  220. .cache_events = &sh7750_cache_events,
  221. .read = sh7750_pmu_read,
  222. .disable = sh7750_pmu_disable,
  223. .enable = sh7750_pmu_enable,
  224. .disable_all = sh7750_pmu_disable_all,
  225. .enable_all = sh7750_pmu_enable_all,
  226. };
  227. static int __init sh7750_pmu_init(void)
  228. {
  229. /*
  230. * Make sure this CPU actually has perf counters.
  231. */
  232. if (!(boot_cpu_data.flags & CPU_HAS_PERF_COUNTER)) {
  233. pr_notice("HW perf events unsupported, software events only.\n");
  234. return -ENODEV;
  235. }
  236. return register_sh_pmu(&sh7750_pmu);
  237. }
  238. early_initcall(sh7750_pmu_init);