op_model_fsl_booke.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 void init_pmc_stop(int ctr)
  32. {
  33. u32 pmlca = (PMLCA_FC | PMLCA_FCS | PMLCA_FCU |
  34. PMLCA_FCM1 | PMLCA_FCM0);
  35. u32 pmlcb = 0;
  36. switch (ctr) {
  37. case 0:
  38. mtpmr(PMRN_PMLCA0, pmlca);
  39. mtpmr(PMRN_PMLCB0, pmlcb);
  40. break;
  41. case 1:
  42. mtpmr(PMRN_PMLCA1, pmlca);
  43. mtpmr(PMRN_PMLCB1, pmlcb);
  44. break;
  45. case 2:
  46. mtpmr(PMRN_PMLCA2, pmlca);
  47. mtpmr(PMRN_PMLCB2, pmlcb);
  48. break;
  49. case 3:
  50. mtpmr(PMRN_PMLCA3, pmlca);
  51. mtpmr(PMRN_PMLCB3, pmlcb);
  52. break;
  53. default:
  54. panic("Bad ctr number!\n");
  55. }
  56. }
  57. static void set_pmc_event(int ctr, int event)
  58. {
  59. u32 pmlca;
  60. pmlca = get_pmlca(ctr);
  61. pmlca = (pmlca & ~PMLCA_EVENT_MASK) |
  62. ((event << PMLCA_EVENT_SHIFT) &
  63. PMLCA_EVENT_MASK);
  64. set_pmlca(ctr, pmlca);
  65. }
  66. static void set_pmc_user_kernel(int ctr, int user, int kernel)
  67. {
  68. u32 pmlca;
  69. pmlca = get_pmlca(ctr);
  70. if(user)
  71. pmlca &= ~PMLCA_FCU;
  72. else
  73. pmlca |= PMLCA_FCU;
  74. if(kernel)
  75. pmlca &= ~PMLCA_FCS;
  76. else
  77. pmlca |= PMLCA_FCS;
  78. set_pmlca(ctr, pmlca);
  79. }
  80. static void set_pmc_marked(int ctr, int mark0, int mark1)
  81. {
  82. u32 pmlca = get_pmlca(ctr);
  83. if(mark0)
  84. pmlca &= ~PMLCA_FCM0;
  85. else
  86. pmlca |= PMLCA_FCM0;
  87. if(mark1)
  88. pmlca &= ~PMLCA_FCM1;
  89. else
  90. pmlca |= PMLCA_FCM1;
  91. set_pmlca(ctr, pmlca);
  92. }
  93. static void pmc_start_ctr(int ctr, int enable)
  94. {
  95. u32 pmlca = get_pmlca(ctr);
  96. pmlca &= ~PMLCA_FC;
  97. if (enable)
  98. pmlca |= PMLCA_CE;
  99. else
  100. pmlca &= ~PMLCA_CE;
  101. set_pmlca(ctr, pmlca);
  102. }
  103. static void pmc_start_ctrs(int enable)
  104. {
  105. u32 pmgc0 = mfpmr(PMRN_PMGC0);
  106. pmgc0 &= ~PMGC0_FAC;
  107. pmgc0 |= PMGC0_FCECE;
  108. if (enable)
  109. pmgc0 |= PMGC0_PMIE;
  110. else
  111. pmgc0 &= ~PMGC0_PMIE;
  112. mtpmr(PMRN_PMGC0, pmgc0);
  113. }
  114. static void pmc_stop_ctrs(void)
  115. {
  116. u32 pmgc0 = mfpmr(PMRN_PMGC0);
  117. pmgc0 |= PMGC0_FAC;
  118. pmgc0 &= ~(PMGC0_PMIE | PMGC0_FCECE);
  119. mtpmr(PMRN_PMGC0, pmgc0);
  120. }
  121. static void dump_pmcs(void)
  122. {
  123. printk("pmgc0: %x\n", mfpmr(PMRN_PMGC0));
  124. printk("pmc\t\tpmlca\t\tpmlcb\n");
  125. printk("%8x\t%8x\t%8x\n", mfpmr(PMRN_PMC0),
  126. mfpmr(PMRN_PMLCA0), mfpmr(PMRN_PMLCB0));
  127. printk("%8x\t%8x\t%8x\n", mfpmr(PMRN_PMC1),
  128. mfpmr(PMRN_PMLCA1), mfpmr(PMRN_PMLCB1));
  129. printk("%8x\t%8x\t%8x\n", mfpmr(PMRN_PMC2),
  130. mfpmr(PMRN_PMLCA2), mfpmr(PMRN_PMLCB2));
  131. printk("%8x\t%8x\t%8x\n", mfpmr(PMRN_PMC3),
  132. mfpmr(PMRN_PMLCA3), mfpmr(PMRN_PMLCB3));
  133. }
  134. static void fsl_booke_cpu_setup(struct op_counter_config *ctr)
  135. {
  136. int i;
  137. /* freeze all counters */
  138. pmc_stop_ctrs();
  139. for (i = 0;i < num_counters;i++) {
  140. init_pmc_stop(i);
  141. set_pmc_event(i, ctr[i].event);
  142. set_pmc_user_kernel(i, ctr[i].user, ctr[i].kernel);
  143. }
  144. }
  145. static void fsl_booke_reg_setup(struct op_counter_config *ctr,
  146. struct op_system_config *sys,
  147. int num_ctrs)
  148. {
  149. int i;
  150. num_counters = num_ctrs;
  151. /* Our counters count up, and "count" refers to
  152. * how much before the next interrupt, and we interrupt
  153. * on overflow. So we calculate the starting value
  154. * which will give us "count" until overflow.
  155. * Then we set the events on the enabled counters */
  156. for (i = 0; i < num_counters; ++i)
  157. reset_value[i] = 0x80000000UL - ctr[i].count;
  158. }
  159. static void fsl_booke_start(struct op_counter_config *ctr)
  160. {
  161. int i;
  162. mtmsr(mfmsr() | MSR_PMM);
  163. for (i = 0; i < num_counters; ++i) {
  164. if (ctr[i].enabled) {
  165. ctr_write(i, reset_value[i]);
  166. /* Set each enabled counter to only
  167. * count when the Mark bit is *not* set */
  168. set_pmc_marked(i, 1, 0);
  169. pmc_start_ctr(i, 1);
  170. } else {
  171. ctr_write(i, 0);
  172. /* Set the ctr to be stopped */
  173. pmc_start_ctr(i, 0);
  174. }
  175. }
  176. /* Clear the freeze bit, and enable the interrupt.
  177. * The counters won't actually start until the rfi clears
  178. * the PMM bit */
  179. pmc_start_ctrs(1);
  180. oprofile_running = 1;
  181. pr_debug("start on cpu %d, pmgc0 %x\n", smp_processor_id(),
  182. mfpmr(PMRN_PMGC0));
  183. }
  184. static void fsl_booke_stop(void)
  185. {
  186. /* freeze counters */
  187. pmc_stop_ctrs();
  188. oprofile_running = 0;
  189. pr_debug("stop on cpu %d, pmgc0 %x\n", smp_processor_id(),
  190. mfpmr(PMRN_PMGC0));
  191. mb();
  192. }
  193. static void fsl_booke_handle_interrupt(struct pt_regs *regs,
  194. struct op_counter_config *ctr)
  195. {
  196. unsigned long pc;
  197. int is_kernel;
  198. int val;
  199. int i;
  200. /* set the PMM bit (see comment below) */
  201. mtmsr(mfmsr() | MSR_PMM);
  202. pc = regs->nip;
  203. is_kernel = is_kernel_addr(pc);
  204. for (i = 0; i < num_counters; ++i) {
  205. val = ctr_read(i);
  206. if (val < 0) {
  207. if (oprofile_running && ctr[i].enabled) {
  208. oprofile_add_ext_sample(pc, regs, i, is_kernel);
  209. ctr_write(i, reset_value[i]);
  210. } else {
  211. ctr_write(i, 0);
  212. }
  213. }
  214. }
  215. /* The freeze bit was set by the interrupt. */
  216. /* Clear the freeze bit, and reenable the interrupt.
  217. * The counters won't actually start until the rfi clears
  218. * the PMM bit */
  219. pmc_start_ctrs(1);
  220. }
  221. struct op_powerpc_model op_model_fsl_booke = {
  222. .reg_setup = fsl_booke_reg_setup,
  223. .cpu_setup = fsl_booke_cpu_setup,
  224. .start = fsl_booke_start,
  225. .stop = fsl_booke_stop,
  226. .handle_interrupt = fsl_booke_handle_interrupt,
  227. };