op_model_mipsxx.c 8.2 KB

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