op_model_mipsxx.c 9.9 KB

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