op_model_mipsxx.c 9.9 KB

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