op_model_7450.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * arch/powerpc/oprofile/op_model_7450.c
  3. *
  4. * Freescale 745x/744x oprofile support, based on fsl_booke 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/smp.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/processor.h>
  21. #include <asm/cputable.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 oprofile_running;
  27. static u32 mmcr0_val, mmcr1_val, mmcr2_val, num_pmcs;
  28. #define MMCR0_PMC1_SHIFT 6
  29. #define MMCR0_PMC2_SHIFT 0
  30. #define MMCR1_PMC3_SHIFT 27
  31. #define MMCR1_PMC4_SHIFT 22
  32. #define MMCR1_PMC5_SHIFT 17
  33. #define MMCR1_PMC6_SHIFT 11
  34. #define mmcr0_event1(event) \
  35. ((event << MMCR0_PMC1_SHIFT) & MMCR0_PMC1SEL)
  36. #define mmcr0_event2(event) \
  37. ((event << MMCR0_PMC2_SHIFT) & MMCR0_PMC2SEL)
  38. #define mmcr1_event3(event) \
  39. ((event << MMCR1_PMC3_SHIFT) & MMCR1_PMC3SEL)
  40. #define mmcr1_event4(event) \
  41. ((event << MMCR1_PMC4_SHIFT) & MMCR1_PMC4SEL)
  42. #define mmcr1_event5(event) \
  43. ((event << MMCR1_PMC5_SHIFT) & MMCR1_PMC5SEL)
  44. #define mmcr1_event6(event) \
  45. ((event << MMCR1_PMC6_SHIFT) & MMCR1_PMC6SEL)
  46. #define MMCR0_INIT (MMCR0_FC | MMCR0_FCS | MMCR0_FCP | MMCR0_FCM1 | MMCR0_FCM0)
  47. /* Unfreezes the counters on this CPU, enables the interrupt,
  48. * enables the counters to trigger the interrupt, and sets the
  49. * counters to only count when the mark bit is not set.
  50. */
  51. static void pmc_start_ctrs(void)
  52. {
  53. u32 mmcr0 = mfspr(SPRN_MMCR0);
  54. mmcr0 &= ~(MMCR0_FC | MMCR0_FCM0);
  55. mmcr0 |= (MMCR0_FCECE | MMCR0_PMC1CE | MMCR0_PMCnCE | MMCR0_PMXE);
  56. mtspr(SPRN_MMCR0, mmcr0);
  57. }
  58. /* Disables the counters on this CPU, and freezes them */
  59. static void pmc_stop_ctrs(void)
  60. {
  61. u32 mmcr0 = mfspr(SPRN_MMCR0);
  62. mmcr0 |= MMCR0_FC;
  63. mmcr0 &= ~(MMCR0_FCECE | MMCR0_PMC1CE | MMCR0_PMCnCE | MMCR0_PMXE);
  64. mtspr(SPRN_MMCR0, mmcr0);
  65. }
  66. /* Configures the counters on this CPU based on the global
  67. * settings */
  68. static int fsl7450_cpu_setup(struct op_counter_config *ctr)
  69. {
  70. /* freeze all counters */
  71. pmc_stop_ctrs();
  72. mtspr(SPRN_MMCR0, mmcr0_val);
  73. mtspr(SPRN_MMCR1, mmcr1_val);
  74. if (num_pmcs > 4)
  75. mtspr(SPRN_MMCR2, mmcr2_val);
  76. return 0;
  77. }
  78. /* Configures the global settings for the countes on all CPUs. */
  79. static int fsl7450_reg_setup(struct op_counter_config *ctr,
  80. struct op_system_config *sys,
  81. int num_ctrs)
  82. {
  83. int i;
  84. num_pmcs = num_ctrs;
  85. /* Our counters count up, and "count" refers to
  86. * how much before the next interrupt, and we interrupt
  87. * on overflow. So we calculate the starting value
  88. * which will give us "count" until overflow.
  89. * Then we set the events on the enabled counters */
  90. for (i = 0; i < num_ctrs; ++i)
  91. reset_value[i] = 0x80000000UL - ctr[i].count;
  92. /* Set events for Counters 1 & 2 */
  93. mmcr0_val = MMCR0_INIT | mmcr0_event1(ctr[0].event)
  94. | mmcr0_event2(ctr[1].event);
  95. /* Setup user/kernel bits */
  96. if (sys->enable_kernel)
  97. mmcr0_val &= ~(MMCR0_FCS);
  98. if (sys->enable_user)
  99. mmcr0_val &= ~(MMCR0_FCP);
  100. /* Set events for Counters 3-6 */
  101. mmcr1_val = mmcr1_event3(ctr[2].event)
  102. | mmcr1_event4(ctr[3].event);
  103. if (num_ctrs > 4)
  104. mmcr1_val |= mmcr1_event5(ctr[4].event)
  105. | mmcr1_event6(ctr[5].event);
  106. mmcr2_val = 0;
  107. return 0;
  108. }
  109. /* Sets the counters on this CPU to the chosen values, and starts them */
  110. static int fsl7450_start(struct op_counter_config *ctr)
  111. {
  112. int i;
  113. mtmsr(mfmsr() | MSR_PMM);
  114. for (i = 0; i < num_pmcs; ++i) {
  115. if (ctr[i].enabled)
  116. classic_ctr_write(i, reset_value[i]);
  117. else
  118. classic_ctr_write(i, 0);
  119. }
  120. /* Clear the freeze bit, and enable the interrupt.
  121. * The counters won't actually start until the rfi clears
  122. * the PMM bit */
  123. pmc_start_ctrs();
  124. oprofile_running = 1;
  125. return 0;
  126. }
  127. /* Stop the counters on this CPU */
  128. static void fsl7450_stop(void)
  129. {
  130. /* freeze counters */
  131. pmc_stop_ctrs();
  132. oprofile_running = 0;
  133. mb();
  134. }
  135. /* Handle the interrupt on this CPU, and log a sample for each
  136. * event that triggered the interrupt */
  137. static void fsl7450_handle_interrupt(struct pt_regs *regs,
  138. struct op_counter_config *ctr)
  139. {
  140. unsigned long pc;
  141. int is_kernel;
  142. int val;
  143. int i;
  144. /* set the PMM bit (see comment below) */
  145. mtmsr(mfmsr() | MSR_PMM);
  146. pc = mfspr(SPRN_SIAR);
  147. is_kernel = is_kernel_addr(pc);
  148. for (i = 0; i < num_pmcs; ++i) {
  149. val = classic_ctr_read(i);
  150. if (val < 0) {
  151. if (oprofile_running && ctr[i].enabled) {
  152. oprofile_add_ext_sample(pc, regs, i, is_kernel);
  153. classic_ctr_write(i, reset_value[i]);
  154. } else {
  155. classic_ctr_write(i, 0);
  156. }
  157. }
  158. }
  159. /* The freeze bit was set by the interrupt. */
  160. /* Clear the freeze bit, and reenable the interrupt.
  161. * The counters won't actually start until the rfi clears
  162. * the PM/M bit */
  163. pmc_start_ctrs();
  164. }
  165. struct op_powerpc_model op_model_7450= {
  166. .reg_setup = fsl7450_reg_setup,
  167. .cpu_setup = fsl7450_cpu_setup,
  168. .start = fsl7450_start,
  169. .stop = fsl7450_stop,
  170. .handle_interrupt = fsl7450_handle_interrupt,
  171. };