init.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * @file init.c
  3. *
  4. * @remark Copyright 2002 OProfile authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author John Levon <levon@movementarian.org>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/oprofile.h>
  11. #include <linux/errno.h>
  12. #include <linux/init.h>
  13. #ifdef CONFIG_SPARC64
  14. #include <asm/hypervisor.h>
  15. #include <asm/spitfire.h>
  16. #include <asm/cpudata.h>
  17. #include <asm/irq.h>
  18. static int nmi_enabled;
  19. struct pcr_ops {
  20. u64 (*read)(void);
  21. void (*write)(u64);
  22. };
  23. static const struct pcr_ops *pcr_ops;
  24. static u64 direct_pcr_read(void)
  25. {
  26. u64 val;
  27. read_pcr(val);
  28. return val;
  29. }
  30. static void direct_pcr_write(u64 val)
  31. {
  32. write_pcr(val);
  33. }
  34. static const struct pcr_ops direct_pcr_ops = {
  35. .read = direct_pcr_read,
  36. .write = direct_pcr_write,
  37. };
  38. static void n2_pcr_write(u64 val)
  39. {
  40. unsigned long ret;
  41. ret = sun4v_niagara2_setperf(HV_N2_PERF_SPARC_CTL, val);
  42. if (val != HV_EOK)
  43. write_pcr(val);
  44. }
  45. static const struct pcr_ops n2_pcr_ops = {
  46. .read = direct_pcr_read,
  47. .write = n2_pcr_write,
  48. };
  49. /* In order to commonize as much of the implementation as
  50. * possible, we use PICH as our counter. Mostly this is
  51. * to accomodate Niagara-1 which can only count insn cycles
  52. * in PICH.
  53. */
  54. static u64 picl_value(void)
  55. {
  56. u32 delta = local_cpu_data().clock_tick / HZ;
  57. return ((u64)((0 - delta) & 0xffffffff)) << 32;
  58. }
  59. #define PCR_PIC_PRIV 0x00000001 /* PIC access is privileged */
  60. #define PCR_STRACE 0x00000002 /* Trace supervisor events */
  61. #define PCR_UTRACE 0x00000004 /* Trace user events */
  62. #define PCR_N2_HTRACE 0x00000008 /* Trace hypervisor events */
  63. #define PCR_N2_TOE_OV0 0x00000010 /* Trap if PIC 0 overflows */
  64. #define PCR_N2_TOE_OV1 0x00000020 /* Trap if PIC 1 overflows */
  65. #define PCR_N2_MASK0 0x00003fc0
  66. #define PCR_N2_MASK0_SHIFT 6
  67. #define PCR_N2_SL0 0x0003c000
  68. #define PCR_N2_SL0_SHIFT 14
  69. #define PCR_N2_OV0 0x00040000
  70. #define PCR_N2_MASK1 0x07f80000
  71. #define PCR_N2_MASK1_SHIFT 19
  72. #define PCR_N2_SL1 0x78000000
  73. #define PCR_N2_SL1_SHIFT 27
  74. #define PCR_N2_OV1 0x80000000
  75. #define PCR_SUN4U_ENABLE (PCR_PIC_PRIV | PCR_STRACE | PCR_UTRACE)
  76. #define PCR_N2_ENABLE (PCR_PIC_PRIV | PCR_STRACE | PCR_UTRACE | \
  77. PCR_N2_TOE_OV1 | \
  78. (2 << PCR_N2_SL1_SHIFT) | \
  79. (0xff << PCR_N2_MASK1_SHIFT))
  80. static u64 pcr_enable = PCR_SUN4U_ENABLE;
  81. static void nmi_handler(struct pt_regs *regs)
  82. {
  83. pcr_ops->write(PCR_PIC_PRIV);
  84. if (nmi_enabled) {
  85. oprofile_add_sample(regs, 0);
  86. write_pic(picl_value());
  87. pcr_ops->write(pcr_enable);
  88. }
  89. }
  90. /* We count "clock cycle" events in the lower 32-bit PIC.
  91. * Then configure it such that it overflows every HZ, and thus
  92. * generates a level 15 interrupt at that frequency.
  93. */
  94. static void cpu_nmi_start(void *_unused)
  95. {
  96. pcr_ops->write(PCR_PIC_PRIV);
  97. write_pic(picl_value());
  98. pcr_ops->write(pcr_enable);
  99. }
  100. static void cpu_nmi_stop(void *_unused)
  101. {
  102. pcr_ops->write(PCR_PIC_PRIV);
  103. }
  104. static int nmi_start(void)
  105. {
  106. int err = register_perfctr_intr(nmi_handler);
  107. if (!err) {
  108. nmi_enabled = 1;
  109. wmb();
  110. err = on_each_cpu(cpu_nmi_start, NULL, 1);
  111. if (err) {
  112. nmi_enabled = 0;
  113. wmb();
  114. on_each_cpu(cpu_nmi_stop, NULL, 1);
  115. release_perfctr_intr(nmi_handler);
  116. }
  117. }
  118. return err;
  119. }
  120. static void nmi_stop(void)
  121. {
  122. nmi_enabled = 0;
  123. wmb();
  124. on_each_cpu(cpu_nmi_stop, NULL, 1);
  125. release_perfctr_intr(nmi_handler);
  126. synchronize_sched();
  127. }
  128. static unsigned long perf_hsvc_group;
  129. static unsigned long perf_hsvc_major;
  130. static unsigned long perf_hsvc_minor;
  131. static int __init register_perf_hsvc(void)
  132. {
  133. if (tlb_type == hypervisor) {
  134. switch (sun4v_chip_type) {
  135. case SUN4V_CHIP_NIAGARA1:
  136. perf_hsvc_group = HV_GRP_NIAG_PERF;
  137. break;
  138. case SUN4V_CHIP_NIAGARA2:
  139. perf_hsvc_group = HV_GRP_N2_CPU;
  140. break;
  141. default:
  142. return -ENODEV;
  143. }
  144. perf_hsvc_major = 1;
  145. perf_hsvc_minor = 0;
  146. if (sun4v_hvapi_register(perf_hsvc_group,
  147. perf_hsvc_major,
  148. &perf_hsvc_minor)) {
  149. printk("perfmon: Could not register N2 hvapi.\n");
  150. return -ENODEV;
  151. }
  152. }
  153. return 0;
  154. }
  155. static void unregister_perf_hsvc(void)
  156. {
  157. if (tlb_type != hypervisor)
  158. return;
  159. sun4v_hvapi_unregister(perf_hsvc_group);
  160. }
  161. static int oprofile_nmi_init(struct oprofile_operations *ops)
  162. {
  163. int err = register_perf_hsvc();
  164. if (err)
  165. return err;
  166. switch (tlb_type) {
  167. case hypervisor:
  168. pcr_ops = &n2_pcr_ops;
  169. pcr_enable = PCR_N2_ENABLE;
  170. break;
  171. case cheetah:
  172. case cheetah_plus:
  173. pcr_ops = &direct_pcr_ops;
  174. break;
  175. default:
  176. return -ENODEV;
  177. }
  178. ops->create_files = NULL;
  179. ops->setup = NULL;
  180. ops->shutdown = NULL;
  181. ops->start = nmi_start;
  182. ops->stop = nmi_stop;
  183. ops->cpu_type = "timer";
  184. printk(KERN_INFO "oprofile: Using perfctr based NMI timer interrupt.\n");
  185. return 0;
  186. }
  187. #endif
  188. int __init oprofile_arch_init(struct oprofile_operations *ops)
  189. {
  190. int ret = -ENODEV;
  191. #ifdef CONFIG_SPARC64
  192. ret = oprofile_nmi_init(ops);
  193. if (!ret)
  194. return ret;
  195. #endif
  196. return ret;
  197. }
  198. void oprofile_arch_exit(void)
  199. {
  200. #ifdef CONFIG_SPARC64
  201. unregister_perf_hsvc();
  202. #endif
  203. }