op_model_fsl_emb.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Freescale Embedded oprofile support, based on ppc64 oprofile support
  3. * Copyright (C) 2004 Anton Blanchard <anton@au.ibm.com>, IBM
  4. *
  5. * Copyright (c) 2004 Freescale Semiconductor, Inc
  6. *
  7. * Author: Andy Fleming
  8. * Maintainer: Kumar Gala <galak@kernel.crashing.org>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/oprofile.h>
  16. #include <linux/init.h>
  17. #include <linux/smp.h>
  18. #include <asm/ptrace.h>
  19. #include <asm/system.h>
  20. #include <asm/processor.h>
  21. #include <asm/cputable.h>
  22. #include <asm/reg_fsl_emb.h>
  23. #include <asm/page.h>
  24. #include <asm/pmc.h>
  25. #include <asm/oprofile_impl.h>
  26. static unsigned long reset_value[OP_MAX_COUNTER];
  27. static int num_counters;
  28. static int oprofile_running;
  29. static inline u32 get_pmlca(int ctr)
  30. {
  31. u32 pmlca;
  32. switch (ctr) {
  33. case 0:
  34. pmlca = mfpmr(PMRN_PMLCA0);
  35. break;
  36. case 1:
  37. pmlca = mfpmr(PMRN_PMLCA1);
  38. break;
  39. case 2:
  40. pmlca = mfpmr(PMRN_PMLCA2);
  41. break;
  42. case 3:
  43. pmlca = mfpmr(PMRN_PMLCA3);
  44. break;
  45. default:
  46. panic("Bad ctr number\n");
  47. }
  48. return pmlca;
  49. }
  50. static inline void set_pmlca(int ctr, u32 pmlca)
  51. {
  52. switch (ctr) {
  53. case 0:
  54. mtpmr(PMRN_PMLCA0, pmlca);
  55. break;
  56. case 1:
  57. mtpmr(PMRN_PMLCA1, pmlca);
  58. break;
  59. case 2:
  60. mtpmr(PMRN_PMLCA2, pmlca);
  61. break;
  62. case 3:
  63. mtpmr(PMRN_PMLCA3, pmlca);
  64. break;
  65. default:
  66. panic("Bad ctr number\n");
  67. }
  68. }
  69. static inline unsigned int ctr_read(unsigned int i)
  70. {
  71. switch(i) {
  72. case 0:
  73. return mfpmr(PMRN_PMC0);
  74. case 1:
  75. return mfpmr(PMRN_PMC1);
  76. case 2:
  77. return mfpmr(PMRN_PMC2);
  78. case 3:
  79. return mfpmr(PMRN_PMC3);
  80. default:
  81. return 0;
  82. }
  83. }
  84. static inline void ctr_write(unsigned int i, unsigned int val)
  85. {
  86. switch(i) {
  87. case 0:
  88. mtpmr(PMRN_PMC0, val);
  89. break;
  90. case 1:
  91. mtpmr(PMRN_PMC1, val);
  92. break;
  93. case 2:
  94. mtpmr(PMRN_PMC2, val);
  95. break;
  96. case 3:
  97. mtpmr(PMRN_PMC3, val);
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. static void init_pmc_stop(int ctr)
  104. {
  105. u32 pmlca = (PMLCA_FC | PMLCA_FCS | PMLCA_FCU |
  106. PMLCA_FCM1 | PMLCA_FCM0);
  107. u32 pmlcb = 0;
  108. switch (ctr) {
  109. case 0:
  110. mtpmr(PMRN_PMLCA0, pmlca);
  111. mtpmr(PMRN_PMLCB0, pmlcb);
  112. break;
  113. case 1:
  114. mtpmr(PMRN_PMLCA1, pmlca);
  115. mtpmr(PMRN_PMLCB1, pmlcb);
  116. break;
  117. case 2:
  118. mtpmr(PMRN_PMLCA2, pmlca);
  119. mtpmr(PMRN_PMLCB2, pmlcb);
  120. break;
  121. case 3:
  122. mtpmr(PMRN_PMLCA3, pmlca);
  123. mtpmr(PMRN_PMLCB3, pmlcb);
  124. break;
  125. default:
  126. panic("Bad ctr number!\n");
  127. }
  128. }
  129. static void set_pmc_event(int ctr, int event)
  130. {
  131. u32 pmlca;
  132. pmlca = get_pmlca(ctr);
  133. pmlca = (pmlca & ~PMLCA_EVENT_MASK) |
  134. ((event << PMLCA_EVENT_SHIFT) &
  135. PMLCA_EVENT_MASK);
  136. set_pmlca(ctr, pmlca);
  137. }
  138. static void set_pmc_user_kernel(int ctr, int user, int kernel)
  139. {
  140. u32 pmlca;
  141. pmlca = get_pmlca(ctr);
  142. if(user)
  143. pmlca &= ~PMLCA_FCU;
  144. else
  145. pmlca |= PMLCA_FCU;
  146. if(kernel)
  147. pmlca &= ~PMLCA_FCS;
  148. else
  149. pmlca |= PMLCA_FCS;
  150. set_pmlca(ctr, pmlca);
  151. }
  152. static void set_pmc_marked(int ctr, int mark0, int mark1)
  153. {
  154. u32 pmlca = get_pmlca(ctr);
  155. if(mark0)
  156. pmlca &= ~PMLCA_FCM0;
  157. else
  158. pmlca |= PMLCA_FCM0;
  159. if(mark1)
  160. pmlca &= ~PMLCA_FCM1;
  161. else
  162. pmlca |= PMLCA_FCM1;
  163. set_pmlca(ctr, pmlca);
  164. }
  165. static void pmc_start_ctr(int ctr, int enable)
  166. {
  167. u32 pmlca = get_pmlca(ctr);
  168. pmlca &= ~PMLCA_FC;
  169. if (enable)
  170. pmlca |= PMLCA_CE;
  171. else
  172. pmlca &= ~PMLCA_CE;
  173. set_pmlca(ctr, pmlca);
  174. }
  175. static void pmc_start_ctrs(int enable)
  176. {
  177. u32 pmgc0 = mfpmr(PMRN_PMGC0);
  178. pmgc0 &= ~PMGC0_FAC;
  179. pmgc0 |= PMGC0_FCECE;
  180. if (enable)
  181. pmgc0 |= PMGC0_PMIE;
  182. else
  183. pmgc0 &= ~PMGC0_PMIE;
  184. mtpmr(PMRN_PMGC0, pmgc0);
  185. }
  186. static void pmc_stop_ctrs(void)
  187. {
  188. u32 pmgc0 = mfpmr(PMRN_PMGC0);
  189. pmgc0 |= PMGC0_FAC;
  190. pmgc0 &= ~(PMGC0_PMIE | PMGC0_FCECE);
  191. mtpmr(PMRN_PMGC0, pmgc0);
  192. }
  193. static void dump_pmcs(void)
  194. {
  195. printk("pmgc0: %x\n", mfpmr(PMRN_PMGC0));
  196. printk("pmc\t\tpmlca\t\tpmlcb\n");
  197. printk("%8x\t%8x\t%8x\n", mfpmr(PMRN_PMC0),
  198. mfpmr(PMRN_PMLCA0), mfpmr(PMRN_PMLCB0));
  199. printk("%8x\t%8x\t%8x\n", mfpmr(PMRN_PMC1),
  200. mfpmr(PMRN_PMLCA1), mfpmr(PMRN_PMLCB1));
  201. printk("%8x\t%8x\t%8x\n", mfpmr(PMRN_PMC2),
  202. mfpmr(PMRN_PMLCA2), mfpmr(PMRN_PMLCB2));
  203. printk("%8x\t%8x\t%8x\n", mfpmr(PMRN_PMC3),
  204. mfpmr(PMRN_PMLCA3), mfpmr(PMRN_PMLCB3));
  205. }
  206. static int fsl_emb_cpu_setup(struct op_counter_config *ctr)
  207. {
  208. int i;
  209. /* freeze all counters */
  210. pmc_stop_ctrs();
  211. for (i = 0;i < num_counters;i++) {
  212. init_pmc_stop(i);
  213. set_pmc_event(i, ctr[i].event);
  214. set_pmc_user_kernel(i, ctr[i].user, ctr[i].kernel);
  215. }
  216. return 0;
  217. }
  218. static int fsl_emb_reg_setup(struct op_counter_config *ctr,
  219. struct op_system_config *sys,
  220. int num_ctrs)
  221. {
  222. int i;
  223. num_counters = num_ctrs;
  224. /* Our counters count up, and "count" refers to
  225. * how much before the next interrupt, and we interrupt
  226. * on overflow. So we calculate the starting value
  227. * which will give us "count" until overflow.
  228. * Then we set the events on the enabled counters */
  229. for (i = 0; i < num_counters; ++i)
  230. reset_value[i] = 0x80000000UL - ctr[i].count;
  231. return 0;
  232. }
  233. static int fsl_emb_start(struct op_counter_config *ctr)
  234. {
  235. int i;
  236. mtmsr(mfmsr() | MSR_PMM);
  237. for (i = 0; i < num_counters; ++i) {
  238. if (ctr[i].enabled) {
  239. ctr_write(i, reset_value[i]);
  240. /* Set each enabled counter to only
  241. * count when the Mark bit is *not* set */
  242. set_pmc_marked(i, 1, 0);
  243. pmc_start_ctr(i, 1);
  244. } else {
  245. ctr_write(i, 0);
  246. /* Set the ctr to be stopped */
  247. pmc_start_ctr(i, 0);
  248. }
  249. }
  250. /* Clear the freeze bit, and enable the interrupt.
  251. * The counters won't actually start until the rfi clears
  252. * the PMM bit */
  253. pmc_start_ctrs(1);
  254. oprofile_running = 1;
  255. pr_debug("start on cpu %d, pmgc0 %x\n", smp_processor_id(),
  256. mfpmr(PMRN_PMGC0));
  257. return 0;
  258. }
  259. static void fsl_emb_stop(void)
  260. {
  261. /* freeze counters */
  262. pmc_stop_ctrs();
  263. oprofile_running = 0;
  264. pr_debug("stop on cpu %d, pmgc0 %x\n", smp_processor_id(),
  265. mfpmr(PMRN_PMGC0));
  266. mb();
  267. }
  268. static void fsl_emb_handle_interrupt(struct pt_regs *regs,
  269. struct op_counter_config *ctr)
  270. {
  271. unsigned long pc;
  272. int is_kernel;
  273. int val;
  274. int i;
  275. /* set the PMM bit (see comment below) */
  276. mtmsr(mfmsr() | MSR_PMM);
  277. pc = regs->nip;
  278. is_kernel = is_kernel_addr(pc);
  279. for (i = 0; i < num_counters; ++i) {
  280. val = ctr_read(i);
  281. if (val < 0) {
  282. if (oprofile_running && ctr[i].enabled) {
  283. oprofile_add_ext_sample(pc, regs, i, is_kernel);
  284. ctr_write(i, reset_value[i]);
  285. } else {
  286. ctr_write(i, 0);
  287. }
  288. }
  289. }
  290. /* The freeze bit was set by the interrupt. */
  291. /* Clear the freeze bit, and reenable the interrupt.
  292. * The counters won't actually start until the rfi clears
  293. * the PMM bit */
  294. pmc_start_ctrs(1);
  295. }
  296. struct op_powerpc_model op_model_fsl_emb = {
  297. .reg_setup = fsl_emb_reg_setup,
  298. .cpu_setup = fsl_emb_cpu_setup,
  299. .start = fsl_emb_start,
  300. .stop = fsl_emb_stop,
  301. .handle_interrupt = fsl_emb_handle_interrupt,
  302. };