op_model_7450.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * 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/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/page.h>
  25. #include <asm/pmc.h>
  26. #include <asm/oprofile_impl.h>
  27. static unsigned long reset_value[OP_MAX_COUNTER];
  28. static int oprofile_running;
  29. static u32 mmcr0_val, mmcr1_val, mmcr2_val;
  30. #define MMCR0_PMC1_SHIFT 6
  31. #define MMCR0_PMC2_SHIFT 0
  32. #define MMCR1_PMC3_SHIFT 27
  33. #define MMCR1_PMC4_SHIFT 22
  34. #define MMCR1_PMC5_SHIFT 17
  35. #define MMCR1_PMC6_SHIFT 11
  36. #define mmcr0_event1(event) \
  37. ((event << MMCR0_PMC1_SHIFT) & MMCR0_PMC1SEL)
  38. #define mmcr0_event2(event) \
  39. ((event << MMCR0_PMC2_SHIFT) & MMCR0_PMC2SEL)
  40. #define mmcr1_event3(event) \
  41. ((event << MMCR1_PMC3_SHIFT) & MMCR1_PMC3SEL)
  42. #define mmcr1_event4(event) \
  43. ((event << MMCR1_PMC4_SHIFT) & MMCR1_PMC4SEL)
  44. #define mmcr1_event5(event) \
  45. ((event << MMCR1_PMC5_SHIFT) & MMCR1_PMC5SEL)
  46. #define mmcr1_event6(event) \
  47. ((event << MMCR1_PMC6_SHIFT) & MMCR1_PMC6SEL)
  48. #define MMCR0_INIT (MMCR0_FC | MMCR0_FCS | MMCR0_FCP | MMCR0_FCM1 | MMCR0_FCM0)
  49. /* Unfreezes the counters on this CPU, enables the interrupt,
  50. * enables the counters to trigger the interrupt, and sets the
  51. * counters to only count when the mark bit is not set.
  52. */
  53. static void pmc_start_ctrs(void)
  54. {
  55. u32 mmcr0 = mfspr(SPRN_MMCR0);
  56. mmcr0 &= ~(MMCR0_FC | MMCR0_FCM0);
  57. mmcr0 |= (MMCR0_FCECE | MMCR0_PMC1CE | MMCR0_PMCnCE | MMCR0_PMXE);
  58. mtspr(SPRN_MMCR0, mmcr0);
  59. }
  60. /* Disables the counters on this CPU, and freezes them */
  61. static void pmc_stop_ctrs(void)
  62. {
  63. u32 mmcr0 = mfspr(SPRN_MMCR0);
  64. mmcr0 |= MMCR0_FC;
  65. mmcr0 &= ~(MMCR0_FCECE | MMCR0_PMC1CE | MMCR0_PMCnCE | MMCR0_PMXE);
  66. mtspr(SPRN_MMCR0, mmcr0);
  67. }
  68. /* Configures the counters on this CPU based on the global
  69. * settings */
  70. static void fsl7450_cpu_setup(void *unused)
  71. {
  72. /* freeze all counters */
  73. pmc_stop_ctrs();
  74. mtspr(SPRN_MMCR0, mmcr0_val);
  75. mtspr(SPRN_MMCR1, mmcr1_val);
  76. mtspr(SPRN_MMCR2, mmcr2_val);
  77. }
  78. #define NUM_CTRS 6
  79. /* Configures the global settings for the countes on all CPUs. */
  80. static void fsl7450_reg_setup(struct op_counter_config *ctr,
  81. struct op_system_config *sys,
  82. int num_ctrs)
  83. {
  84. int i;
  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. | mmcr1_event5(ctr[4].event)
  104. | mmcr1_event6(ctr[5].event);
  105. mmcr2_val = 0;
  106. }
  107. /* Sets the counters on this CPU to the chosen values, and starts them */
  108. static void fsl7450_start(struct op_counter_config *ctr)
  109. {
  110. int i;
  111. mtmsr(mfmsr() | MSR_PMM);
  112. for (i = 0; i < NUM_CTRS; ++i) {
  113. if (ctr[i].enabled)
  114. ctr_write(i, reset_value[i]);
  115. else
  116. ctr_write(i, 0);
  117. }
  118. /* Clear the freeze bit, and enable the interrupt.
  119. * The counters won't actually start until the rfi clears
  120. * the PMM bit */
  121. pmc_start_ctrs();
  122. oprofile_running = 1;
  123. }
  124. /* Stop the counters on this CPU */
  125. static void fsl7450_stop(void)
  126. {
  127. /* freeze counters */
  128. pmc_stop_ctrs();
  129. oprofile_running = 0;
  130. mb();
  131. }
  132. /* Handle the interrupt on this CPU, and log a sample for each
  133. * event that triggered the interrupt */
  134. static void fsl7450_handle_interrupt(struct pt_regs *regs,
  135. struct op_counter_config *ctr)
  136. {
  137. unsigned long pc;
  138. int is_kernel;
  139. int val;
  140. int i;
  141. /* set the PMM bit (see comment below) */
  142. mtmsr(mfmsr() | MSR_PMM);
  143. pc = mfspr(SPRN_SIAR);
  144. is_kernel = is_kernel_addr(pc);
  145. for (i = 0; i < NUM_CTRS; ++i) {
  146. val = ctr_read(i);
  147. if (val < 0) {
  148. if (oprofile_running && ctr[i].enabled) {
  149. oprofile_add_ext_sample(pc, regs, i, is_kernel);
  150. ctr_write(i, reset_value[i]);
  151. } else {
  152. ctr_write(i, 0);
  153. }
  154. }
  155. }
  156. /* The freeze bit was set by the interrupt. */
  157. /* Clear the freeze bit, and reenable the interrupt.
  158. * The counters won't actually start until the rfi clears
  159. * the PMM bit */
  160. pmc_start_ctrs();
  161. }
  162. struct op_powerpc_model op_model_7450= {
  163. .reg_setup = fsl7450_reg_setup,
  164. .cpu_setup = fsl7450_cpu_setup,
  165. .start = fsl7450_start,
  166. .stop = fsl7450_stop,
  167. .handle_interrupt = fsl7450_handle_interrupt,
  168. };