op_model_mipsxx.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2004, 05, 06 by Ralf Baechle
  7. * Copyright (C) 2005 by MIPS Technologies, Inc.
  8. */
  9. #include <linux/oprofile.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/smp.h>
  12. #include <asm/irq_regs.h>
  13. #include "op_impl.h"
  14. #define M_PERFCTL_EXL (1UL << 0)
  15. #define M_PERFCTL_KERNEL (1UL << 1)
  16. #define M_PERFCTL_SUPERVISOR (1UL << 2)
  17. #define M_PERFCTL_USER (1UL << 3)
  18. #define M_PERFCTL_INTERRUPT_ENABLE (1UL << 4)
  19. #define M_PERFCTL_EVENT(event) (((event) & 0x3f) << 5)
  20. #define M_PERFCTL_VPEID(vpe) ((vpe) << 16)
  21. #define M_PERFCTL_MT_EN(filter) ((filter) << 20)
  22. #define M_TC_EN_ALL M_PERFCTL_MT_EN(0)
  23. #define M_TC_EN_VPE M_PERFCTL_MT_EN(1)
  24. #define M_TC_EN_TC M_PERFCTL_MT_EN(2)
  25. #define M_PERFCTL_TCID(tcid) ((tcid) << 22)
  26. #define M_PERFCTL_WIDE (1UL << 30)
  27. #define M_PERFCTL_MORE (1UL << 31)
  28. #define M_COUNTER_OVERFLOW (1UL << 31)
  29. #ifdef CONFIG_MIPS_MT_SMP
  30. #define WHAT (M_TC_EN_VPE | M_PERFCTL_VPEID(smp_processor_id()))
  31. #define vpe_id() smp_processor_id()
  32. #else
  33. #define WHAT 0
  34. #define vpe_id() 0
  35. #endif
  36. #define __define_perf_accessors(r, n, np) \
  37. \
  38. static inline unsigned int r_c0_ ## r ## n(void) \
  39. { \
  40. unsigned int cpu = vpe_id(); \
  41. \
  42. switch (cpu) { \
  43. case 0: \
  44. return read_c0_ ## r ## n(); \
  45. case 1: \
  46. return read_c0_ ## r ## np(); \
  47. default: \
  48. BUG(); \
  49. } \
  50. return 0; \
  51. } \
  52. \
  53. static inline void w_c0_ ## r ## n(unsigned int value) \
  54. { \
  55. unsigned int cpu = vpe_id(); \
  56. \
  57. switch (cpu) { \
  58. case 0: \
  59. write_c0_ ## r ## n(value); \
  60. return; \
  61. case 1: \
  62. write_c0_ ## r ## np(value); \
  63. return; \
  64. default: \
  65. BUG(); \
  66. } \
  67. return; \
  68. } \
  69. __define_perf_accessors(perfcntr, 0, 2)
  70. __define_perf_accessors(perfcntr, 1, 3)
  71. __define_perf_accessors(perfcntr, 2, 0)
  72. __define_perf_accessors(perfcntr, 3, 1)
  73. __define_perf_accessors(perfctrl, 0, 2)
  74. __define_perf_accessors(perfctrl, 1, 3)
  75. __define_perf_accessors(perfctrl, 2, 0)
  76. __define_perf_accessors(perfctrl, 3, 1)
  77. struct op_mips_model op_model_mipsxx_ops;
  78. static struct mipsxx_register_config {
  79. unsigned int control[4];
  80. unsigned int counter[4];
  81. } reg;
  82. /* Compute all of the registers in preparation for enabling profiling. */
  83. static void mipsxx_reg_setup(struct op_counter_config *ctr)
  84. {
  85. unsigned int counters = op_model_mipsxx_ops.num_counters;
  86. int i;
  87. /* Compute the performance counter control word. */
  88. for (i = 0; i < counters; i++) {
  89. reg.control[i] = 0;
  90. reg.counter[i] = 0;
  91. if (!ctr[i].enabled)
  92. continue;
  93. reg.control[i] = M_PERFCTL_EVENT(ctr[i].event) |
  94. M_PERFCTL_INTERRUPT_ENABLE;
  95. if (ctr[i].kernel)
  96. reg.control[i] |= M_PERFCTL_KERNEL;
  97. if (ctr[i].user)
  98. reg.control[i] |= M_PERFCTL_USER;
  99. if (ctr[i].exl)
  100. reg.control[i] |= M_PERFCTL_EXL;
  101. reg.counter[i] = 0x80000000 - ctr[i].count;
  102. }
  103. }
  104. /* Program all of the registers in preparation for enabling profiling. */
  105. static void mipsxx_cpu_setup (void *args)
  106. {
  107. unsigned int counters = op_model_mipsxx_ops.num_counters;
  108. switch (counters) {
  109. case 4:
  110. w_c0_perfctrl3(0);
  111. w_c0_perfcntr3(reg.counter[3]);
  112. case 3:
  113. w_c0_perfctrl2(0);
  114. w_c0_perfcntr2(reg.counter[2]);
  115. case 2:
  116. w_c0_perfctrl1(0);
  117. w_c0_perfcntr1(reg.counter[1]);
  118. case 1:
  119. w_c0_perfctrl0(0);
  120. w_c0_perfcntr0(reg.counter[0]);
  121. }
  122. }
  123. /* Start all counters on current CPU */
  124. static void mipsxx_cpu_start(void *args)
  125. {
  126. unsigned int counters = op_model_mipsxx_ops.num_counters;
  127. switch (counters) {
  128. case 4:
  129. w_c0_perfctrl3(WHAT | reg.control[3]);
  130. case 3:
  131. w_c0_perfctrl2(WHAT | reg.control[2]);
  132. case 2:
  133. w_c0_perfctrl1(WHAT | reg.control[1]);
  134. case 1:
  135. w_c0_perfctrl0(WHAT | reg.control[0]);
  136. }
  137. }
  138. /* Stop all counters on current CPU */
  139. static void mipsxx_cpu_stop(void *args)
  140. {
  141. unsigned int counters = op_model_mipsxx_ops.num_counters;
  142. switch (counters) {
  143. case 4:
  144. w_c0_perfctrl3(0);
  145. case 3:
  146. w_c0_perfctrl2(0);
  147. case 2:
  148. w_c0_perfctrl1(0);
  149. case 1:
  150. w_c0_perfctrl0(0);
  151. }
  152. }
  153. static int mipsxx_perfcount_handler(void)
  154. {
  155. unsigned int counters = op_model_mipsxx_ops.num_counters;
  156. unsigned int control;
  157. unsigned int counter;
  158. int handled = 0;
  159. switch (counters) {
  160. #define HANDLE_COUNTER(n) \
  161. case n + 1: \
  162. control = r_c0_perfctrl ## n(); \
  163. counter = r_c0_perfcntr ## n(); \
  164. if ((control & M_PERFCTL_INTERRUPT_ENABLE) && \
  165. (counter & M_COUNTER_OVERFLOW)) { \
  166. oprofile_add_sample(get_irq_regs(), n); \
  167. w_c0_perfcntr ## n(reg.counter[n]); \
  168. handled = 1; \
  169. }
  170. HANDLE_COUNTER(3)
  171. HANDLE_COUNTER(2)
  172. HANDLE_COUNTER(1)
  173. HANDLE_COUNTER(0)
  174. }
  175. return handled;
  176. }
  177. #define M_CONFIG1_PC (1 << 4)
  178. static inline int __n_counters(void)
  179. {
  180. if (!(read_c0_config1() & M_CONFIG1_PC))
  181. return 0;
  182. if (!(r_c0_perfctrl0() & M_PERFCTL_MORE))
  183. return 1;
  184. if (!(r_c0_perfctrl1() & M_PERFCTL_MORE))
  185. return 2;
  186. if (!(r_c0_perfctrl2() & M_PERFCTL_MORE))
  187. return 3;
  188. return 4;
  189. }
  190. static inline int n_counters(void)
  191. {
  192. int counters;
  193. switch (current_cpu_data.cputype) {
  194. case CPU_R10000:
  195. counters = 2;
  196. break;
  197. case CPU_R12000:
  198. case CPU_R14000:
  199. counters = 4;
  200. break;
  201. default:
  202. counters = __n_counters();
  203. }
  204. return counters;
  205. }
  206. static inline void reset_counters(int counters)
  207. {
  208. switch (counters) {
  209. case 4:
  210. w_c0_perfctrl3(0);
  211. w_c0_perfcntr3(0);
  212. case 3:
  213. w_c0_perfctrl2(0);
  214. w_c0_perfcntr2(0);
  215. case 2:
  216. w_c0_perfctrl1(0);
  217. w_c0_perfcntr1(0);
  218. case 1:
  219. w_c0_perfctrl0(0);
  220. w_c0_perfcntr0(0);
  221. }
  222. }
  223. static int __init mipsxx_init(void)
  224. {
  225. int counters;
  226. counters = n_counters();
  227. if (counters == 0) {
  228. printk(KERN_ERR "Oprofile: CPU has no performance counters\n");
  229. return -ENODEV;
  230. }
  231. reset_counters(counters);
  232. #ifdef CONFIG_MIPS_MT_SMP
  233. counters >>= 1;
  234. #endif
  235. op_model_mipsxx_ops.num_counters = counters;
  236. switch (current_cpu_data.cputype) {
  237. case CPU_20KC:
  238. op_model_mipsxx_ops.cpu_type = "mips/20K";
  239. break;
  240. case CPU_24K:
  241. op_model_mipsxx_ops.cpu_type = "mips/24K";
  242. break;
  243. case CPU_25KF:
  244. op_model_mipsxx_ops.cpu_type = "mips/25K";
  245. break;
  246. case CPU_34K:
  247. op_model_mipsxx_ops.cpu_type = "mips/34K";
  248. break;
  249. case CPU_74K:
  250. op_model_mipsxx_ops.cpu_type = "mips/74K";
  251. break;
  252. case CPU_5KC:
  253. op_model_mipsxx_ops.cpu_type = "mips/5K";
  254. break;
  255. case CPU_R10000:
  256. if ((current_cpu_data.processor_id & 0xff) == 0x20)
  257. op_model_mipsxx_ops.cpu_type = "mips/r10000-v2.x";
  258. else
  259. op_model_mipsxx_ops.cpu_type = "mips/r10000";
  260. break;
  261. case CPU_R12000:
  262. case CPU_R14000:
  263. op_model_mipsxx_ops.cpu_type = "mips/r12000";
  264. break;
  265. case CPU_SB1:
  266. case CPU_SB1A:
  267. op_model_mipsxx_ops.cpu_type = "mips/sb1";
  268. break;
  269. default:
  270. printk(KERN_ERR "Profiling unsupported for this CPU\n");
  271. return -ENODEV;
  272. }
  273. perf_irq = mipsxx_perfcount_handler;
  274. return 0;
  275. }
  276. static void mipsxx_exit(void)
  277. {
  278. int counters = op_model_mipsxx_ops.num_counters;
  279. #ifdef CONFIG_MIPS_MT_SMP
  280. counters <<= 1;
  281. #endif
  282. reset_counters(counters);
  283. perf_irq = null_perf_irq;
  284. }
  285. struct op_mips_model op_model_mipsxx_ops = {
  286. .reg_setup = mipsxx_reg_setup,
  287. .cpu_setup = mipsxx_cpu_setup,
  288. .init = mipsxx_init,
  289. .exit = mipsxx_exit,
  290. .cpu_start = mipsxx_cpu_start,
  291. .cpu_stop = mipsxx_cpu_stop,
  292. };