op_model_fsl_emb.c 6.5 KB

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