op_model_rm9000.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * Copyright (C) 2004 by Ralf Baechle
  7. */
  8. #include <linux/init.h>
  9. #include <linux/oprofile.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/smp.h>
  12. #include "op_impl.h"
  13. #define RM9K_COUNTER1_EVENT(event) ((event) << 0)
  14. #define RM9K_COUNTER1_SUPERVISOR (1ULL << 7)
  15. #define RM9K_COUNTER1_KERNEL (1ULL << 8)
  16. #define RM9K_COUNTER1_USER (1ULL << 9)
  17. #define RM9K_COUNTER1_ENABLE (1ULL << 10)
  18. #define RM9K_COUNTER1_OVERFLOW (1ULL << 15)
  19. #define RM9K_COUNTER2_EVENT(event) ((event) << 16)
  20. #define RM9K_COUNTER2_SUPERVISOR (1ULL << 23)
  21. #define RM9K_COUNTER2_KERNEL (1ULL << 24)
  22. #define RM9K_COUNTER2_USER (1ULL << 25)
  23. #define RM9K_COUNTER2_ENABLE (1ULL << 26)
  24. #define RM9K_COUNTER2_OVERFLOW (1ULL << 31)
  25. extern unsigned int rm9000_perfcount_irq;
  26. static struct rm9k_register_config {
  27. unsigned int control;
  28. unsigned int reset_counter1;
  29. unsigned int reset_counter2;
  30. } reg;
  31. /* Compute all of the registers in preparation for enabling profiling. */
  32. static void rm9000_reg_setup(struct op_counter_config *ctr)
  33. {
  34. unsigned int control = 0;
  35. /* Compute the performance counter control word. */
  36. /* For now count kernel and user mode */
  37. if (ctr[0].enabled)
  38. control |= RM9K_COUNTER1_EVENT(ctr[0].event) |
  39. RM9K_COUNTER1_KERNEL |
  40. RM9K_COUNTER1_USER |
  41. RM9K_COUNTER1_ENABLE;
  42. if (ctr[1].enabled)
  43. control |= RM9K_COUNTER2_EVENT(ctr[1].event) |
  44. RM9K_COUNTER2_KERNEL |
  45. RM9K_COUNTER2_USER |
  46. RM9K_COUNTER2_ENABLE;
  47. reg.control = control;
  48. reg.reset_counter1 = 0x80000000 - ctr[0].count;
  49. reg.reset_counter2 = 0x80000000 - ctr[1].count;
  50. }
  51. /* Program all of the registers in preparation for enabling profiling. */
  52. static void rm9000_cpu_setup(void *args)
  53. {
  54. uint64_t perfcount;
  55. perfcount = ((uint64_t) reg.reset_counter2 << 32) | reg.reset_counter1;
  56. write_c0_perfcount(perfcount);
  57. }
  58. static void rm9000_cpu_start(void *args)
  59. {
  60. /* Start all counters on current CPU */
  61. write_c0_perfcontrol(reg.control);
  62. }
  63. static void rm9000_cpu_stop(void *args)
  64. {
  65. /* Stop all counters on current CPU */
  66. write_c0_perfcontrol(0);
  67. }
  68. static irqreturn_t rm9000_perfcount_handler(int irq, void *dev_id)
  69. {
  70. unsigned int control = read_c0_perfcontrol();
  71. struct pt_regs *regs = get_irq_regs();
  72. uint32_t counter1, counter2;
  73. uint64_t counters;
  74. /*
  75. * RM9000 combines two 32-bit performance counters into a single
  76. * 64-bit coprocessor zero register. To avoid a race updating the
  77. * registers we need to stop the counters while we're messing with
  78. * them ...
  79. */
  80. write_c0_perfcontrol(0);
  81. counters = read_c0_perfcount();
  82. counter1 = counters;
  83. counter2 = counters >> 32;
  84. if (control & RM9K_COUNTER1_OVERFLOW) {
  85. oprofile_add_sample(regs, 0);
  86. counter1 = reg.reset_counter1;
  87. }
  88. if (control & RM9K_COUNTER2_OVERFLOW) {
  89. oprofile_add_sample(regs, 1);
  90. counter2 = reg.reset_counter2;
  91. }
  92. counters = ((uint64_t)counter2 << 32) | counter1;
  93. write_c0_perfcount(counters);
  94. write_c0_perfcontrol(reg.control);
  95. return IRQ_HANDLED;
  96. }
  97. static int __init rm9000_init(void)
  98. {
  99. return request_irq(rm9000_perfcount_irq, rm9000_perfcount_handler,
  100. 0, "Perfcounter", NULL);
  101. }
  102. static void rm9000_exit(void)
  103. {
  104. free_irq(rm9000_perfcount_irq, NULL);
  105. }
  106. struct op_mips_model op_model_rm9000_ops = {
  107. .reg_setup = rm9000_reg_setup,
  108. .cpu_setup = rm9000_cpu_setup,
  109. .init = rm9000_init,
  110. .exit = rm9000_exit,
  111. .cpu_start = rm9000_cpu_start,
  112. .cpu_stop = rm9000_cpu_stop,
  113. .cpu_type = "mips/rm9000",
  114. .num_counters = 2
  115. };