op_model_loongson2.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Loongson2 performance counter driver for oprofile
  3. *
  4. * Copyright (C) 2009 Lemote Inc. & Insititute of Computing Technology
  5. * Author: Yanhua <yanh@lemote.com>
  6. * Author: Wu Zhangjin <wuzj@lemote.com>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. *
  12. */
  13. #include <linux/init.h>
  14. #include <linux/oprofile.h>
  15. #include <linux/interrupt.h>
  16. #include <loongson.h> /* LOONGSON2_PERFCNT_IRQ */
  17. #include "op_impl.h"
  18. /*
  19. * a patch should be sent to oprofile with the loongson-specific support.
  20. * otherwise, the oprofile tool will not recognize this and complain about
  21. * "cpu_type 'unset' is not valid".
  22. */
  23. #define LOONGSON2_CPU_TYPE "mips/loongson2"
  24. #define LOONGSON2_COUNTER1_EVENT(event) ((event & 0x0f) << 5)
  25. #define LOONGSON2_COUNTER2_EVENT(event) ((event & 0x0f) << 9)
  26. #define LOONGSON2_PERFCNT_EXL (1UL << 0)
  27. #define LOONGSON2_PERFCNT_KERNEL (1UL << 1)
  28. #define LOONGSON2_PERFCNT_SUPERVISOR (1UL << 2)
  29. #define LOONGSON2_PERFCNT_USER (1UL << 3)
  30. #define LOONGSON2_PERFCNT_INT_EN (1UL << 4)
  31. #define LOONGSON2_PERFCNT_OVERFLOW (1ULL << 31)
  32. /* Loongson2 performance counter register */
  33. #define read_c0_perfctrl() __read_64bit_c0_register($24, 0)
  34. #define write_c0_perfctrl(val) __write_64bit_c0_register($24, 0, val)
  35. #define read_c0_perfcnt() __read_64bit_c0_register($25, 0)
  36. #define write_c0_perfcnt(val) __write_64bit_c0_register($25, 0, val)
  37. static struct loongson2_register_config {
  38. unsigned int ctrl;
  39. unsigned long long reset_counter1;
  40. unsigned long long reset_counter2;
  41. int cnt1_enabled, cnt2_enabled;
  42. } reg;
  43. DEFINE_SPINLOCK(sample_lock);
  44. static char *oprofid = "LoongsonPerf";
  45. static irqreturn_t loongson2_perfcount_handler(int irq, void *dev_id);
  46. /* Compute all of the registers in preparation for enabling profiling. */
  47. static void loongson2_reg_setup(struct op_counter_config *cfg)
  48. {
  49. unsigned int ctrl = 0;
  50. reg.reset_counter1 = 0;
  51. reg.reset_counter2 = 0;
  52. /* Compute the performance counter ctrl word. */
  53. /* For now count kernel and user mode */
  54. if (cfg[0].enabled) {
  55. ctrl |= LOONGSON2_COUNTER1_EVENT(cfg[0].event);
  56. reg.reset_counter1 = 0x80000000ULL - cfg[0].count;
  57. }
  58. if (cfg[1].enabled) {
  59. ctrl |= LOONGSON2_COUNTER2_EVENT(cfg[1].event);
  60. reg.reset_counter2 = (0x80000000ULL - cfg[1].count);
  61. }
  62. if (cfg[0].enabled || cfg[1].enabled) {
  63. ctrl |= LOONGSON2_PERFCNT_EXL | LOONGSON2_PERFCNT_INT_EN;
  64. if (cfg[0].kernel || cfg[1].kernel)
  65. ctrl |= LOONGSON2_PERFCNT_KERNEL;
  66. if (cfg[0].user || cfg[1].user)
  67. ctrl |= LOONGSON2_PERFCNT_USER;
  68. }
  69. reg.ctrl = ctrl;
  70. reg.cnt1_enabled = cfg[0].enabled;
  71. reg.cnt2_enabled = cfg[1].enabled;
  72. }
  73. /* Program all of the registers in preparation for enabling profiling. */
  74. static void loongson2_cpu_setup(void *args)
  75. {
  76. uint64_t perfcount;
  77. perfcount = (reg.reset_counter2 << 32) | reg.reset_counter1;
  78. write_c0_perfcnt(perfcount);
  79. }
  80. static void loongson2_cpu_start(void *args)
  81. {
  82. /* Start all counters on current CPU */
  83. if (reg.cnt1_enabled || reg.cnt2_enabled)
  84. write_c0_perfctrl(reg.ctrl);
  85. }
  86. static void loongson2_cpu_stop(void *args)
  87. {
  88. /* Stop all counters on current CPU */
  89. write_c0_perfctrl(0);
  90. memset(&reg, 0, sizeof(reg));
  91. }
  92. static irqreturn_t loongson2_perfcount_handler(int irq, void *dev_id)
  93. {
  94. uint64_t counter, counter1, counter2;
  95. struct pt_regs *regs = get_irq_regs();
  96. int enabled;
  97. unsigned long flags;
  98. /*
  99. * LOONGSON2 defines two 32-bit performance counters.
  100. * To avoid a race updating the registers we need to stop the counters
  101. * while we're messing with
  102. * them ...
  103. */
  104. /* Check whether the irq belongs to me */
  105. enabled = reg.cnt1_enabled | reg.cnt2_enabled;
  106. if (!enabled)
  107. return IRQ_NONE;
  108. counter = read_c0_perfcnt();
  109. counter1 = counter & 0xffffffff;
  110. counter2 = counter >> 32;
  111. spin_lock_irqsave(&sample_lock, flags);
  112. if (counter1 & LOONGSON2_PERFCNT_OVERFLOW) {
  113. if (reg.cnt1_enabled)
  114. oprofile_add_sample(regs, 0);
  115. counter1 = reg.reset_counter1;
  116. }
  117. if (counter2 & LOONGSON2_PERFCNT_OVERFLOW) {
  118. if (reg.cnt2_enabled)
  119. oprofile_add_sample(regs, 1);
  120. counter2 = reg.reset_counter2;
  121. }
  122. spin_unlock_irqrestore(&sample_lock, flags);
  123. write_c0_perfcnt((counter2 << 32) | counter1);
  124. return IRQ_HANDLED;
  125. }
  126. static int __init loongson2_init(void)
  127. {
  128. return request_irq(LOONGSON2_PERFCNT_IRQ, loongson2_perfcount_handler,
  129. IRQF_SHARED, "Perfcounter", oprofid);
  130. }
  131. static void loongson2_exit(void)
  132. {
  133. write_c0_perfctrl(0);
  134. free_irq(LOONGSON2_PERFCNT_IRQ, oprofid);
  135. }
  136. struct op_mips_model op_model_loongson2_ops = {
  137. .reg_setup = loongson2_reg_setup,
  138. .cpu_setup = loongson2_cpu_setup,
  139. .init = loongson2_init,
  140. .exit = loongson2_exit,
  141. .cpu_start = loongson2_cpu_start,
  142. .cpu_stop = loongson2_cpu_stop,
  143. .cpu_type = LOONGSON2_CPU_TYPE,
  144. .num_counters = 2
  145. };