4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
7 * @author John Levon <levon@movementarian.org>
10 #include <linux/kernel.h>
11 #include <linux/oprofile.h>
12 #include <linux/errno.h>
13 #include <linux/init.h>
16 #include <asm/hypervisor.h>
17 #include <asm/spitfire.h>
18 #include <asm/cpudata.h>
21 static int nmi_enabled;
27 static const struct pcr_ops *pcr_ops;
29 static u64 direct_pcr_read(void)
37 static void direct_pcr_write(u64 val)
42 static const struct pcr_ops direct_pcr_ops = {
43 .read = direct_pcr_read,
44 .write = direct_pcr_write,
47 static void n2_pcr_write(u64 val)
51 ret = sun4v_niagara2_setperf(HV_N2_PERF_SPARC_CTL, val);
56 static const struct pcr_ops n2_pcr_ops = {
57 .read = direct_pcr_read,
58 .write = n2_pcr_write,
61 /* In order to commonize as much of the implementation as
62 * possible, we use PICH as our counter. Mostly this is
63 * to accomodate Niagara-1 which can only count insn cycles
66 static u64 picl_value(void)
68 u32 delta = local_cpu_data().clock_tick / HZ;
70 return ((u64)((0 - delta) & 0xffffffff)) << 32;
73 #define PCR_PIC_PRIV 0x00000001 /* PIC access is privileged */
74 #define PCR_STRACE 0x00000002 /* Trace supervisor events */
75 #define PCR_UTRACE 0x00000004 /* Trace user events */
76 #define PCR_N2_HTRACE 0x00000008 /* Trace hypervisor events */
77 #define PCR_N2_TOE_OV0 0x00000010 /* Trap if PIC 0 overflows */
78 #define PCR_N2_TOE_OV1 0x00000020 /* Trap if PIC 1 overflows */
79 #define PCR_N2_MASK0 0x00003fc0
80 #define PCR_N2_MASK0_SHIFT 6
81 #define PCR_N2_SL0 0x0003c000
82 #define PCR_N2_SL0_SHIFT 14
83 #define PCR_N2_OV0 0x00040000
84 #define PCR_N2_MASK1 0x07f80000
85 #define PCR_N2_MASK1_SHIFT 19
86 #define PCR_N2_SL1 0x78000000
87 #define PCR_N2_SL1_SHIFT 27
88 #define PCR_N2_OV1 0x80000000
90 #define PCR_SUN4U_ENABLE (PCR_PIC_PRIV | PCR_STRACE | PCR_UTRACE)
91 #define PCR_N2_ENABLE (PCR_PIC_PRIV | PCR_STRACE | PCR_UTRACE | \
93 (2 << PCR_N2_SL1_SHIFT) | \
94 (0xff << PCR_N2_MASK1_SHIFT))
96 static u64 pcr_enable = PCR_SUN4U_ENABLE;
98 static void nmi_handler(struct pt_regs *regs)
100 pcr_ops->write(PCR_PIC_PRIV);
103 oprofile_add_sample(regs, 0);
105 write_pic(picl_value());
106 pcr_ops->write(pcr_enable);
110 /* We count "clock cycle" events in the lower 32-bit PIC.
111 * Then configure it such that it overflows every HZ, and thus
112 * generates a level 15 interrupt at that frequency.
114 static void cpu_nmi_start(void *_unused)
116 pcr_ops->write(PCR_PIC_PRIV);
117 write_pic(picl_value());
119 pcr_ops->write(pcr_enable);
122 static void cpu_nmi_stop(void *_unused)
124 pcr_ops->write(PCR_PIC_PRIV);
127 static int nmi_start(void)
129 int err = register_perfctr_intr(nmi_handler);
134 err = on_each_cpu(cpu_nmi_start, NULL, 1);
138 on_each_cpu(cpu_nmi_stop, NULL, 1);
139 release_perfctr_intr(nmi_handler);
146 static void nmi_stop(void)
151 on_each_cpu(cpu_nmi_stop, NULL, 1);
152 release_perfctr_intr(nmi_handler);
156 static unsigned long perf_hsvc_group;
157 static unsigned long perf_hsvc_major;
158 static unsigned long perf_hsvc_minor;
160 static int __init register_perf_hsvc(void)
162 if (tlb_type == hypervisor) {
163 switch (sun4v_chip_type) {
164 case SUN4V_CHIP_NIAGARA1:
165 perf_hsvc_group = HV_GRP_NIAG_PERF;
168 case SUN4V_CHIP_NIAGARA2:
169 perf_hsvc_group = HV_GRP_N2_CPU;
179 if (sun4v_hvapi_register(perf_hsvc_group,
182 printk("perfmon: Could not register N2 hvapi.\n");
189 static void unregister_perf_hsvc(void)
191 if (tlb_type != hypervisor)
193 sun4v_hvapi_unregister(perf_hsvc_group);
196 static int oprofile_nmi_init(struct oprofile_operations *ops)
198 int err = register_perf_hsvc();
205 pcr_ops = &n2_pcr_ops;
206 pcr_enable = PCR_N2_ENABLE;
211 pcr_ops = &direct_pcr_ops;
218 ops->create_files = NULL;
220 ops->shutdown = NULL;
221 ops->start = nmi_start;
222 ops->stop = nmi_stop;
223 ops->cpu_type = "timer";
225 printk(KERN_INFO "oprofile: Using perfctr based NMI timer interrupt.\n");
231 int __init oprofile_arch_init(struct oprofile_operations *ops)
235 #ifdef CONFIG_SPARC64
236 ret = oprofile_nmi_init(ops);
245 void oprofile_arch_exit(void)
247 #ifdef CONFIG_SPARC64
248 unregister_perf_hsvc();