op_model_fsl_emb.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 int fsl_emb_cpu_setup(struct op_counter_config *ctr)
  194. {
  195. int i;
  196. /* freeze all counters */
  197. pmc_stop_ctrs();
  198. for (i = 0;i < num_counters;i++) {
  199. init_pmc_stop(i);
  200. set_pmc_event(i, ctr[i].event);
  201. set_pmc_user_kernel(i, ctr[i].user, ctr[i].kernel);
  202. }
  203. return 0;
  204. }
  205. static int fsl_emb_reg_setup(struct op_counter_config *ctr,
  206. struct op_system_config *sys,
  207. int num_ctrs)
  208. {
  209. int i;
  210. num_counters = num_ctrs;
  211. /* Our counters count up, and "count" refers to
  212. * how much before the next interrupt, and we interrupt
  213. * on overflow. So we calculate the starting value
  214. * which will give us "count" until overflow.
  215. * Then we set the events on the enabled counters */
  216. for (i = 0; i < num_counters; ++i)
  217. reset_value[i] = 0x80000000UL - ctr[i].count;
  218. return 0;
  219. }
  220. static int fsl_emb_start(struct op_counter_config *ctr)
  221. {
  222. int i;
  223. mtmsr(mfmsr() | MSR_PMM);
  224. for (i = 0; i < num_counters; ++i) {
  225. if (ctr[i].enabled) {
  226. ctr_write(i, reset_value[i]);
  227. /* Set each enabled counter to only
  228. * count when the Mark bit is *not* set */
  229. set_pmc_marked(i, 1, 0);
  230. pmc_start_ctr(i, 1);
  231. } else {
  232. ctr_write(i, 0);
  233. /* Set the ctr to be stopped */
  234. pmc_start_ctr(i, 0);
  235. }
  236. }
  237. /* Clear the freeze bit, and enable the interrupt.
  238. * The counters won't actually start until the rfi clears
  239. * the PMM bit */
  240. pmc_start_ctrs(1);
  241. oprofile_running = 1;
  242. pr_debug("start on cpu %d, pmgc0 %x\n", smp_processor_id(),
  243. mfpmr(PMRN_PMGC0));
  244. return 0;
  245. }
  246. static void fsl_emb_stop(void)
  247. {
  248. /* freeze counters */
  249. pmc_stop_ctrs();
  250. oprofile_running = 0;
  251. pr_debug("stop on cpu %d, pmgc0 %x\n", smp_processor_id(),
  252. mfpmr(PMRN_PMGC0));
  253. mb();
  254. }
  255. static void fsl_emb_handle_interrupt(struct pt_regs *regs,
  256. struct op_counter_config *ctr)
  257. {
  258. unsigned long pc;
  259. int is_kernel;
  260. int val;
  261. int i;
  262. /* set the PMM bit (see comment below) */
  263. mtmsr(mfmsr() | MSR_PMM);
  264. pc = regs->nip;
  265. is_kernel = is_kernel_addr(pc);
  266. for (i = 0; i < num_counters; ++i) {
  267. val = ctr_read(i);
  268. if (val < 0) {
  269. if (oprofile_running && ctr[i].enabled) {
  270. oprofile_add_ext_sample(pc, regs, i, is_kernel);
  271. ctr_write(i, reset_value[i]);
  272. } else {
  273. ctr_write(i, 0);
  274. }
  275. }
  276. }
  277. /* The freeze bit was set by the interrupt. */
  278. /* Clear the freeze bit, and reenable the interrupt.
  279. * The counters won't actually start until the rfi clears
  280. * the PMM bit */
  281. pmc_start_ctrs(1);
  282. }
  283. struct op_powerpc_model op_model_fsl_emb = {
  284. .reg_setup = fsl_emb_reg_setup,
  285. .cpu_setup = fsl_emb_cpu_setup,
  286. .start = fsl_emb_start,
  287. .stop = fsl_emb_stop,
  288. .handle_interrupt = fsl_emb_handle_interrupt,
  289. };