perf_event.c 6.2 KB

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