op_model_loongson2.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Loongson2 performance counter driver for oprofile
  3. *
  4. * Copyright (C) 2009 Lemote Inc.
  5. * Author: Yanhua <yanh@lemote.com>
  6. * Author: Wu Zhangjin <wuzhangjin@gmail.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. static char *oprofid = "LoongsonPerf";
  44. static irqreturn_t loongson2_perfcount_handler(int irq, void *dev_id);
  45. /* Compute all of the registers in preparation for enabling profiling. */
  46. static void loongson2_reg_setup(struct op_counter_config *cfg)
  47. {
  48. unsigned int ctrl = 0;
  49. reg.reset_counter1 = 0;
  50. reg.reset_counter2 = 0;
  51. /* Compute the performance counter ctrl word. */
  52. /* For now count kernel and user mode */
  53. if (cfg[0].enabled) {
  54. ctrl |= LOONGSON2_COUNTER1_EVENT(cfg[0].event);
  55. reg.reset_counter1 = 0x80000000ULL - cfg[0].count;
  56. }
  57. if (cfg[1].enabled) {
  58. ctrl |= LOONGSON2_COUNTER2_EVENT(cfg[1].event);
  59. reg.reset_counter2 = (0x80000000ULL - cfg[1].count);
  60. }
  61. if (cfg[0].enabled || cfg[1].enabled) {
  62. ctrl |= LOONGSON2_PERFCNT_EXL | LOONGSON2_PERFCNT_INT_EN;
  63. if (cfg[0].kernel || cfg[1].kernel)
  64. ctrl |= LOONGSON2_PERFCNT_KERNEL;
  65. if (cfg[0].user || cfg[1].user)
  66. ctrl |= LOONGSON2_PERFCNT_USER;
  67. }
  68. reg.ctrl = ctrl;
  69. reg.cnt1_enabled = cfg[0].enabled;
  70. reg.cnt2_enabled = cfg[1].enabled;
  71. }
  72. /* Program all of the registers in preparation for enabling profiling. */
  73. static void loongson2_cpu_setup(void *args)
  74. {
  75. uint64_t perfcount;
  76. perfcount = (reg.reset_counter2 << 32) | reg.reset_counter1;
  77. write_c0_perfcnt(perfcount);
  78. }
  79. static void loongson2_cpu_start(void *args)
  80. {
  81. /* Start all counters on current CPU */
  82. if (reg.cnt1_enabled || reg.cnt2_enabled)
  83. write_c0_perfctrl(reg.ctrl);
  84. }
  85. static void loongson2_cpu_stop(void *args)
  86. {
  87. /* Stop all counters on current CPU */
  88. write_c0_perfctrl(0);
  89. memset(&reg, 0, sizeof(reg));
  90. }
  91. static irqreturn_t loongson2_perfcount_handler(int irq, void *dev_id)
  92. {
  93. uint64_t counter, counter1, counter2;
  94. struct pt_regs *regs = get_irq_regs();
  95. int enabled;
  96. /*
  97. * LOONGSON2 defines two 32-bit performance counters.
  98. * To avoid a race updating the registers we need to stop the counters
  99. * while we're messing with
  100. * them ...
  101. */
  102. /* Check whether the irq belongs to me */
  103. enabled = read_c0_perfcnt() & LOONGSON2_PERFCNT_INT_EN;
  104. if (!enabled)
  105. return IRQ_NONE;
  106. enabled = reg.cnt1_enabled | reg.cnt2_enabled;
  107. if (!enabled)
  108. return IRQ_NONE;
  109. counter = read_c0_perfcnt();
  110. counter1 = counter & 0xffffffff;
  111. counter2 = counter >> 32;
  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. write_c0_perfcnt((counter2 << 32) | counter1);
  123. return IRQ_HANDLED;
  124. }
  125. static int __init loongson2_init(void)
  126. {
  127. return request_irq(LOONGSON2_PERFCNT_IRQ, loongson2_perfcount_handler,
  128. IRQF_SHARED, "Perfcounter", oprofid);
  129. }
  130. static void loongson2_exit(void)
  131. {
  132. write_c0_perfctrl(0);
  133. free_irq(LOONGSON2_PERFCNT_IRQ, oprofid);
  134. }
  135. struct op_mips_model op_model_loongson2_ops = {
  136. .reg_setup = loongson2_reg_setup,
  137. .cpu_setup = loongson2_cpu_setup,
  138. .init = loongson2_init,
  139. .exit = loongson2_exit,
  140. .cpu_start = loongson2_cpu_start,
  141. .cpu_stop = loongson2_cpu_stop,
  142. .cpu_type = LOONGSON2_CPU_TYPE,
  143. .num_counters = 2
  144. };