8xx-pmu.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Performance event support - PPC 8xx
  3. *
  4. * Copyright 2016 Christophe Leroy, CS Systemes d'Information
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/perf_event.h>
  14. #include <linux/percpu.h>
  15. #include <linux/hardirq.h>
  16. #include <asm/pmc.h>
  17. #include <asm/machdep.h>
  18. #include <asm/firmware.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/code-patching.h>
  21. #define PERF_8xx_ID_CPU_CYCLES 1
  22. #define PERF_8xx_ID_HW_INSTRUCTIONS 2
  23. #define PERF_8xx_ID_ITLB_LOAD_MISS 3
  24. #define PERF_8xx_ID_DTLB_LOAD_MISS 4
  25. #define C(x) PERF_COUNT_HW_CACHE_##x
  26. #define DTLB_LOAD_MISS (C(DTLB) | (C(OP_READ) << 8) | (C(RESULT_MISS) << 16))
  27. #define ITLB_LOAD_MISS (C(ITLB) | (C(OP_READ) << 8) | (C(RESULT_MISS) << 16))
  28. extern unsigned long itlb_miss_counter, dtlb_miss_counter;
  29. extern atomic_t instruction_counter;
  30. extern unsigned int itlb_miss_perf, dtlb_miss_perf;
  31. extern unsigned int itlb_miss_exit_1, itlb_miss_exit_2;
  32. extern unsigned int dtlb_miss_exit_1, dtlb_miss_exit_2, dtlb_miss_exit_3;
  33. static atomic_t insn_ctr_ref;
  34. static atomic_t itlb_miss_ref;
  35. static atomic_t dtlb_miss_ref;
  36. static s64 get_insn_ctr(void)
  37. {
  38. int ctr;
  39. unsigned long counta;
  40. do {
  41. ctr = atomic_read(&instruction_counter);
  42. counta = mfspr(SPRN_COUNTA);
  43. } while (ctr != atomic_read(&instruction_counter));
  44. return ((s64)ctr << 16) | (counta >> 16);
  45. }
  46. static int event_type(struct perf_event *event)
  47. {
  48. switch (event->attr.type) {
  49. case PERF_TYPE_HARDWARE:
  50. if (event->attr.config == PERF_COUNT_HW_CPU_CYCLES)
  51. return PERF_8xx_ID_CPU_CYCLES;
  52. if (event->attr.config == PERF_COUNT_HW_INSTRUCTIONS)
  53. return PERF_8xx_ID_HW_INSTRUCTIONS;
  54. break;
  55. case PERF_TYPE_HW_CACHE:
  56. if (event->attr.config == ITLB_LOAD_MISS)
  57. return PERF_8xx_ID_ITLB_LOAD_MISS;
  58. if (event->attr.config == DTLB_LOAD_MISS)
  59. return PERF_8xx_ID_DTLB_LOAD_MISS;
  60. break;
  61. case PERF_TYPE_RAW:
  62. break;
  63. default:
  64. return -ENOENT;
  65. }
  66. return -EOPNOTSUPP;
  67. }
  68. static int mpc8xx_pmu_event_init(struct perf_event *event)
  69. {
  70. int type = event_type(event);
  71. if (type < 0)
  72. return type;
  73. return 0;
  74. }
  75. static int mpc8xx_pmu_add(struct perf_event *event, int flags)
  76. {
  77. int type = event_type(event);
  78. s64 val = 0;
  79. if (type < 0)
  80. return type;
  81. switch (type) {
  82. case PERF_8xx_ID_CPU_CYCLES:
  83. val = get_tb();
  84. break;
  85. case PERF_8xx_ID_HW_INSTRUCTIONS:
  86. if (atomic_inc_return(&insn_ctr_ref) == 1)
  87. mtspr(SPRN_ICTRL, 0xc0080007);
  88. val = get_insn_ctr();
  89. break;
  90. case PERF_8xx_ID_ITLB_LOAD_MISS:
  91. if (atomic_inc_return(&itlb_miss_ref) == 1) {
  92. unsigned long target = (unsigned long)&itlb_miss_perf;
  93. patch_branch(&itlb_miss_exit_1, target, 0);
  94. #ifndef CONFIG_PIN_TLB_TEXT
  95. patch_branch(&itlb_miss_exit_2, target, 0);
  96. #endif
  97. }
  98. val = itlb_miss_counter;
  99. break;
  100. case PERF_8xx_ID_DTLB_LOAD_MISS:
  101. if (atomic_inc_return(&dtlb_miss_ref) == 1) {
  102. unsigned long target = (unsigned long)&dtlb_miss_perf;
  103. patch_branch(&dtlb_miss_exit_1, target, 0);
  104. patch_branch(&dtlb_miss_exit_2, target, 0);
  105. patch_branch(&dtlb_miss_exit_3, target, 0);
  106. }
  107. val = dtlb_miss_counter;
  108. break;
  109. }
  110. local64_set(&event->hw.prev_count, val);
  111. return 0;
  112. }
  113. static void mpc8xx_pmu_read(struct perf_event *event)
  114. {
  115. int type = event_type(event);
  116. s64 prev, val = 0, delta = 0;
  117. if (type < 0)
  118. return;
  119. do {
  120. prev = local64_read(&event->hw.prev_count);
  121. switch (type) {
  122. case PERF_8xx_ID_CPU_CYCLES:
  123. val = get_tb();
  124. delta = 16 * (val - prev);
  125. break;
  126. case PERF_8xx_ID_HW_INSTRUCTIONS:
  127. val = get_insn_ctr();
  128. delta = prev - val;
  129. if (delta < 0)
  130. delta += 0x1000000000000LL;
  131. break;
  132. case PERF_8xx_ID_ITLB_LOAD_MISS:
  133. val = itlb_miss_counter;
  134. delta = (s64)((s32)val - (s32)prev);
  135. break;
  136. case PERF_8xx_ID_DTLB_LOAD_MISS:
  137. val = dtlb_miss_counter;
  138. delta = (s64)((s32)val - (s32)prev);
  139. break;
  140. }
  141. } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
  142. local64_add(delta, &event->count);
  143. }
  144. static void mpc8xx_pmu_del(struct perf_event *event, int flags)
  145. {
  146. /* mfspr r10, SPRN_SPRG_SCRATCH0 */
  147. unsigned int insn = PPC_INST_MFSPR | __PPC_RS(R10) |
  148. __PPC_SPR(SPRN_SPRG_SCRATCH0);
  149. mpc8xx_pmu_read(event);
  150. /* If it was the last user, stop counting to avoid useles overhead */
  151. switch (event_type(event)) {
  152. case PERF_8xx_ID_CPU_CYCLES:
  153. break;
  154. case PERF_8xx_ID_HW_INSTRUCTIONS:
  155. if (atomic_dec_return(&insn_ctr_ref) == 0)
  156. mtspr(SPRN_ICTRL, 7);
  157. break;
  158. case PERF_8xx_ID_ITLB_LOAD_MISS:
  159. if (atomic_dec_return(&itlb_miss_ref) == 0) {
  160. patch_instruction(&itlb_miss_exit_1, insn);
  161. #ifndef CONFIG_PIN_TLB_TEXT
  162. patch_instruction(&itlb_miss_exit_2, insn);
  163. #endif
  164. }
  165. break;
  166. case PERF_8xx_ID_DTLB_LOAD_MISS:
  167. if (atomic_dec_return(&dtlb_miss_ref) == 0) {
  168. patch_instruction(&dtlb_miss_exit_1, insn);
  169. patch_instruction(&dtlb_miss_exit_2, insn);
  170. patch_instruction(&dtlb_miss_exit_3, insn);
  171. }
  172. break;
  173. }
  174. }
  175. static struct pmu mpc8xx_pmu = {
  176. .event_init = mpc8xx_pmu_event_init,
  177. .add = mpc8xx_pmu_add,
  178. .del = mpc8xx_pmu_del,
  179. .read = mpc8xx_pmu_read,
  180. .capabilities = PERF_PMU_CAP_NO_INTERRUPT |
  181. PERF_PMU_CAP_NO_NMI,
  182. };
  183. static int init_mpc8xx_pmu(void)
  184. {
  185. mtspr(SPRN_ICTRL, 7);
  186. mtspr(SPRN_CMPA, 0);
  187. mtspr(SPRN_COUNTA, 0xffff);
  188. return perf_pmu_register(&mpc8xx_pmu, "cpu", PERF_TYPE_RAW);
  189. }
  190. early_initcall(init_mpc8xx_pmu);