op_model_mipsxx.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 = IRQ_NONE;
  159. if (cpu_has_mips_r2 && !(read_c0_cause() & (1 << 26)))
  160. return handled;
  161. switch (counters) {
  162. #define HANDLE_COUNTER(n) \
  163. case n + 1: \
  164. control = r_c0_perfctrl ## n(); \
  165. counter = r_c0_perfcntr ## n(); \
  166. if ((control & M_PERFCTL_INTERRUPT_ENABLE) && \
  167. (counter & M_COUNTER_OVERFLOW)) { \
  168. oprofile_add_sample(get_irq_regs(), n); \
  169. w_c0_perfcntr ## n(reg.counter[n]); \
  170. handled = IRQ_HANDLED; \
  171. }
  172. HANDLE_COUNTER(3)
  173. HANDLE_COUNTER(2)
  174. HANDLE_COUNTER(1)
  175. HANDLE_COUNTER(0)
  176. }
  177. return handled;
  178. }
  179. #define M_CONFIG1_PC (1 << 4)
  180. static inline int __n_counters(void)
  181. {
  182. if (!(read_c0_config1() & M_CONFIG1_PC))
  183. return 0;
  184. if (!(r_c0_perfctrl0() & M_PERFCTL_MORE))
  185. return 1;
  186. if (!(r_c0_perfctrl1() & M_PERFCTL_MORE))
  187. return 2;
  188. if (!(r_c0_perfctrl2() & M_PERFCTL_MORE))
  189. return 3;
  190. return 4;
  191. }
  192. static inline int n_counters(void)
  193. {
  194. int counters;
  195. switch (current_cpu_type()) {
  196. case CPU_R10000:
  197. counters = 2;
  198. break;
  199. case CPU_R12000:
  200. case CPU_R14000:
  201. counters = 4;
  202. break;
  203. default:
  204. counters = __n_counters();
  205. }
  206. return counters;
  207. }
  208. static inline void reset_counters(int counters)
  209. {
  210. switch (counters) {
  211. case 4:
  212. w_c0_perfctrl3(0);
  213. w_c0_perfcntr3(0);
  214. case 3:
  215. w_c0_perfctrl2(0);
  216. w_c0_perfcntr2(0);
  217. case 2:
  218. w_c0_perfctrl1(0);
  219. w_c0_perfcntr1(0);
  220. case 1:
  221. w_c0_perfctrl0(0);
  222. w_c0_perfcntr0(0);
  223. }
  224. }
  225. static int __init mipsxx_init(void)
  226. {
  227. int counters;
  228. counters = n_counters();
  229. if (counters == 0) {
  230. printk(KERN_ERR "Oprofile: CPU has no performance counters\n");
  231. return -ENODEV;
  232. }
  233. reset_counters(counters);
  234. #ifdef CONFIG_MIPS_MT_SMP
  235. counters >>= 1;
  236. #endif
  237. op_model_mipsxx_ops.num_counters = counters;
  238. switch (current_cpu_type()) {
  239. case CPU_20KC:
  240. op_model_mipsxx_ops.cpu_type = "mips/20K";
  241. break;
  242. case CPU_24K:
  243. op_model_mipsxx_ops.cpu_type = "mips/24K";
  244. break;
  245. case CPU_25KF:
  246. op_model_mipsxx_ops.cpu_type = "mips/25K";
  247. break;
  248. case CPU_34K:
  249. op_model_mipsxx_ops.cpu_type = "mips/34K";
  250. break;
  251. case CPU_74K:
  252. op_model_mipsxx_ops.cpu_type = "mips/74K";
  253. break;
  254. case CPU_5KC:
  255. op_model_mipsxx_ops.cpu_type = "mips/5K";
  256. break;
  257. case CPU_R10000:
  258. if ((current_cpu_data.processor_id & 0xff) == 0x20)
  259. op_model_mipsxx_ops.cpu_type = "mips/r10000-v2.x";
  260. else
  261. op_model_mipsxx_ops.cpu_type = "mips/r10000";
  262. break;
  263. case CPU_R12000:
  264. case CPU_R14000:
  265. op_model_mipsxx_ops.cpu_type = "mips/r12000";
  266. break;
  267. case CPU_SB1:
  268. case CPU_SB1A:
  269. op_model_mipsxx_ops.cpu_type = "mips/sb1";
  270. break;
  271. default:
  272. printk(KERN_ERR "Profiling unsupported for this CPU\n");
  273. return -ENODEV;
  274. }
  275. perf_irq = mipsxx_perfcount_handler;
  276. return 0;
  277. }
  278. static void mipsxx_exit(void)
  279. {
  280. int counters = op_model_mipsxx_ops.num_counters;
  281. #ifdef CONFIG_MIPS_MT_SMP
  282. counters <<= 1;
  283. #endif
  284. reset_counters(counters);
  285. perf_irq = null_perf_irq;
  286. }
  287. struct op_mips_model op_model_mipsxx_ops = {
  288. .reg_setup = mipsxx_reg_setup,
  289. .cpu_setup = mipsxx_cpu_setup,
  290. .init = mipsxx_init,
  291. .exit = mipsxx_exit,
  292. .cpu_start = mipsxx_cpu_start,
  293. .cpu_stop = mipsxx_cpu_stop,
  294. };