op_model_rm9000.c 3.6 KB

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