op_model_fsl_booke.c 6.9 KB

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