op_model_loongson3.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. */
  7. #include <linux/init.h>
  8. #include <linux/cpu.h>
  9. #include <linux/smp.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/oprofile.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/interrupt.h>
  14. #include <asm/uaccess.h>
  15. #include <irq.h>
  16. #include <loongson.h>
  17. #include "op_impl.h"
  18. #define LOONGSON3_PERFCNT_OVERFLOW (1ULL << 63)
  19. #define LOONGSON3_PERFCTRL_EXL (1UL << 0)
  20. #define LOONGSON3_PERFCTRL_KERNEL (1UL << 1)
  21. #define LOONGSON3_PERFCTRL_SUPERVISOR (1UL << 2)
  22. #define LOONGSON3_PERFCTRL_USER (1UL << 3)
  23. #define LOONGSON3_PERFCTRL_ENABLE (1UL << 4)
  24. #define LOONGSON3_PERFCTRL_W (1UL << 30)
  25. #define LOONGSON3_PERFCTRL_M (1UL << 31)
  26. #define LOONGSON3_PERFCTRL_EVENT(idx, event) \
  27. (((event) & (idx ? 0x0f : 0x3f)) << 5)
  28. /* Loongson-3 PerfCount performance counter1 register */
  29. #define read_c0_perflo1() __read_64bit_c0_register($25, 0)
  30. #define write_c0_perflo1(val) __write_64bit_c0_register($25, 0, val)
  31. #define read_c0_perfhi1() __read_64bit_c0_register($25, 1)
  32. #define write_c0_perfhi1(val) __write_64bit_c0_register($25, 1, val)
  33. /* Loongson-3 PerfCount performance counter2 register */
  34. #define read_c0_perflo2() __read_64bit_c0_register($25, 2)
  35. #define write_c0_perflo2(val) __write_64bit_c0_register($25, 2, val)
  36. #define read_c0_perfhi2() __read_64bit_c0_register($25, 3)
  37. #define write_c0_perfhi2(val) __write_64bit_c0_register($25, 3, val)
  38. static int (*save_perf_irq)(void);
  39. static struct loongson3_register_config {
  40. unsigned int control1;
  41. unsigned int control2;
  42. unsigned long long reset_counter1;
  43. unsigned long long reset_counter2;
  44. int ctr1_enable, ctr2_enable;
  45. } reg;
  46. static void reset_counters(void *arg)
  47. {
  48. write_c0_perfhi1(0);
  49. write_c0_perfhi2(0);
  50. write_c0_perflo1(0xc0000000);
  51. write_c0_perflo2(0x40000000);
  52. }
  53. /* Compute all of the registers in preparation for enabling profiling. */
  54. static void loongson3_reg_setup(struct op_counter_config *ctr)
  55. {
  56. unsigned int control1 = 0;
  57. unsigned int control2 = 0;
  58. reg.reset_counter1 = 0;
  59. reg.reset_counter2 = 0;
  60. /* Compute the performance counter control word. */
  61. /* For now count kernel and user mode */
  62. if (ctr[0].enabled) {
  63. control1 |= LOONGSON3_PERFCTRL_EVENT(0, ctr[0].event) |
  64. LOONGSON3_PERFCTRL_ENABLE;
  65. if (ctr[0].kernel)
  66. control1 |= LOONGSON3_PERFCTRL_KERNEL;
  67. if (ctr[0].user)
  68. control1 |= LOONGSON3_PERFCTRL_USER;
  69. reg.reset_counter1 = 0x8000000000000000ULL - ctr[0].count;
  70. }
  71. if (ctr[1].enabled) {
  72. control2 |= LOONGSON3_PERFCTRL_EVENT(1, ctr[1].event) |
  73. LOONGSON3_PERFCTRL_ENABLE;
  74. if (ctr[1].kernel)
  75. control2 |= LOONGSON3_PERFCTRL_KERNEL;
  76. if (ctr[1].user)
  77. control2 |= LOONGSON3_PERFCTRL_USER;
  78. reg.reset_counter2 = 0x8000000000000000ULL - ctr[1].count;
  79. }
  80. if (ctr[0].enabled)
  81. control1 |= LOONGSON3_PERFCTRL_EXL;
  82. if (ctr[1].enabled)
  83. control2 |= LOONGSON3_PERFCTRL_EXL;
  84. reg.control1 = control1;
  85. reg.control2 = control2;
  86. reg.ctr1_enable = ctr[0].enabled;
  87. reg.ctr2_enable = ctr[1].enabled;
  88. }
  89. /* Program all of the registers in preparation for enabling profiling. */
  90. static void loongson3_cpu_setup(void *args)
  91. {
  92. uint64_t perfcount1, perfcount2;
  93. perfcount1 = reg.reset_counter1;
  94. perfcount2 = reg.reset_counter2;
  95. write_c0_perfhi1(perfcount1);
  96. write_c0_perfhi2(perfcount2);
  97. }
  98. static void loongson3_cpu_start(void *args)
  99. {
  100. /* Start all counters on current CPU */
  101. reg.control1 |= (LOONGSON3_PERFCTRL_W|LOONGSON3_PERFCTRL_M);
  102. reg.control2 |= (LOONGSON3_PERFCTRL_W|LOONGSON3_PERFCTRL_M);
  103. if (reg.ctr1_enable)
  104. write_c0_perflo1(reg.control1);
  105. if (reg.ctr2_enable)
  106. write_c0_perflo2(reg.control2);
  107. }
  108. static void loongson3_cpu_stop(void *args)
  109. {
  110. /* Stop all counters on current CPU */
  111. write_c0_perflo1(0xc0000000);
  112. write_c0_perflo2(0x40000000);
  113. memset(&reg, 0, sizeof(reg));
  114. }
  115. static int loongson3_perfcount_handler(void)
  116. {
  117. unsigned long flags;
  118. uint64_t counter1, counter2;
  119. uint32_t cause, handled = IRQ_NONE;
  120. struct pt_regs *regs = get_irq_regs();
  121. cause = read_c0_cause();
  122. if (!(cause & CAUSEF_PCI))
  123. return handled;
  124. counter1 = read_c0_perfhi1();
  125. counter2 = read_c0_perfhi2();
  126. local_irq_save(flags);
  127. if (counter1 & LOONGSON3_PERFCNT_OVERFLOW) {
  128. if (reg.ctr1_enable)
  129. oprofile_add_sample(regs, 0);
  130. counter1 = reg.reset_counter1;
  131. }
  132. if (counter2 & LOONGSON3_PERFCNT_OVERFLOW) {
  133. if (reg.ctr2_enable)
  134. oprofile_add_sample(regs, 1);
  135. counter2 = reg.reset_counter2;
  136. }
  137. local_irq_restore(flags);
  138. write_c0_perfhi1(counter1);
  139. write_c0_perfhi2(counter2);
  140. if (!(cause & CAUSEF_TI))
  141. handled = IRQ_HANDLED;
  142. return handled;
  143. }
  144. static int loongson3_cpu_callback(struct notifier_block *nfb,
  145. unsigned long action, void *hcpu)
  146. {
  147. switch (action) {
  148. case CPU_STARTING:
  149. case CPU_STARTING_FROZEN:
  150. write_c0_perflo1(reg.control1);
  151. write_c0_perflo2(reg.control2);
  152. break;
  153. case CPU_DYING:
  154. case CPU_DYING_FROZEN:
  155. write_c0_perflo1(0xc0000000);
  156. write_c0_perflo2(0x40000000);
  157. break;
  158. }
  159. return NOTIFY_OK;
  160. }
  161. static struct notifier_block loongson3_notifier_block = {
  162. .notifier_call = loongson3_cpu_callback
  163. };
  164. static int __init loongson3_init(void)
  165. {
  166. on_each_cpu(reset_counters, NULL, 1);
  167. register_hotcpu_notifier(&loongson3_notifier_block);
  168. save_perf_irq = perf_irq;
  169. perf_irq = loongson3_perfcount_handler;
  170. return 0;
  171. }
  172. static void loongson3_exit(void)
  173. {
  174. on_each_cpu(reset_counters, NULL, 1);
  175. unregister_hotcpu_notifier(&loongson3_notifier_block);
  176. perf_irq = save_perf_irq;
  177. }
  178. struct op_mips_model op_model_loongson3_ops = {
  179. .reg_setup = loongson3_reg_setup,
  180. .cpu_setup = loongson3_cpu_setup,
  181. .init = loongson3_init,
  182. .exit = loongson3_exit,
  183. .cpu_start = loongson3_cpu_start,
  184. .cpu_stop = loongson3_cpu_stop,
  185. .cpu_type = "mips/loongson3",
  186. .num_counters = 2
  187. };