op_model_mipsxx.c 8.7 KB

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