blob: 494af3227f3b3fafec489bd39ef763da2689e7a5 [file] [log] [blame]
David S. Miller3eb80572009-01-21 21:30:23 -08001/* pcr.c: Generic sparc64 performance counter infrastructure.
2 *
3 * Copyright (C) 2009 David S. Miller (davem@davemloft.net)
4 */
5#include <linux/kernel.h>
Paul Gortmaker066bcac2011-07-22 13:18:16 -04006#include <linux/export.h>
David S. Miller3eb80572009-01-21 21:30:23 -08007#include <linux/init.h>
8#include <linux/irq.h>
9
Peter Zijlstrae360adb2010-10-14 14:01:34 +080010#include <linux/irq_work.h>
David S. Miller9960e9e2010-04-07 04:41:33 -070011#include <linux/ftrace.h>
David S. Miller5686f9c2009-09-10 05:59:24 -070012
David S. Miller3eb80572009-01-21 21:30:23 -080013#include <asm/pil.h>
14#include <asm/pcr.h>
David S. Millere5553a62009-01-29 21:22:47 -080015#include <asm/nmi.h>
Paul Gortmakerc2068da2011-08-01 13:42:48 -040016#include <asm/spitfire.h>
David S. Miller3eb80572009-01-21 21:30:23 -080017
18/* This code is shared between various users of the performance
19 * counters. Users will be oprofile, pseudo-NMI watchdog, and the
Ingo Molnarcdd6c482009-09-21 12:02:48 +020020 * perf_event support layer.
David S. Miller3eb80572009-01-21 21:30:23 -080021 */
22
David S. Millere5553a62009-01-29 21:22:47 -080023#define PCR_SUN4U_ENABLE (PCR_PIC_PRIV | PCR_STRACE | PCR_UTRACE)
24#define PCR_N2_ENABLE (PCR_PIC_PRIV | PCR_STRACE | PCR_UTRACE | \
25 PCR_N2_TOE_OV1 | \
26 (2 << PCR_N2_SL1_SHIFT) | \
27 (0xff << PCR_N2_MASK1_SHIFT))
28
29u64 pcr_enable;
David S. Millere5553a62009-01-29 21:22:47 -080030
David S. Miller3eb80572009-01-21 21:30:23 -080031/* Performance counter interrupts run unmasked at PIL level 15.
32 * Therefore we can't do things like wakeups and other work
33 * that expects IRQ disabling to be adhered to in locking etc.
34 *
35 * Therefore in such situations we defer the work by signalling
36 * a lower level cpu IRQ.
37 */
David S. Miller9960e9e2010-04-07 04:41:33 -070038void __irq_entry deferred_pcr_work_irq(int irq, struct pt_regs *regs)
David S. Miller3eb80572009-01-21 21:30:23 -080039{
David S. Miller5686f9c2009-09-10 05:59:24 -070040 struct pt_regs *old_regs;
41
David S. Miller3eb80572009-01-21 21:30:23 -080042 clear_softint(1 << PIL_DEFERRED_PCR_WORK);
David S. Miller5686f9c2009-09-10 05:59:24 -070043
44 old_regs = set_irq_regs(regs);
45 irq_enter();
Peter Zijlstrae360adb2010-10-14 14:01:34 +080046#ifdef CONFIG_IRQ_WORK
47 irq_work_run();
David S. Miller5686f9c2009-09-10 05:59:24 -070048#endif
49 irq_exit();
50 set_irq_regs(old_regs);
David S. Miller3eb80572009-01-21 21:30:23 -080051}
52
Peter Zijlstrae360adb2010-10-14 14:01:34 +080053void arch_irq_work_raise(void)
David S. Miller3eb80572009-01-21 21:30:23 -080054{
55 set_softint(1 << PIL_DEFERRED_PCR_WORK);
56}
57
58const struct pcr_ops *pcr_ops;
59EXPORT_SYMBOL_GPL(pcr_ops);
60
David S. Miller0bab20b2012-08-16 21:16:22 -070061static u64 direct_pcr_read(unsigned long reg_num)
David S. Miller3eb80572009-01-21 21:30:23 -080062{
63 u64 val;
64
David S. Miller0bab20b2012-08-16 21:16:22 -070065 WARN_ON_ONCE(reg_num != 0);
David S. Miller09d053c2012-08-16 23:19:32 -070066 __asm__ __volatile__("rd %%pcr, %0" : "=r" (val));
David S. Miller3eb80572009-01-21 21:30:23 -080067 return val;
68}
69
David S. Miller0bab20b2012-08-16 21:16:22 -070070static void direct_pcr_write(unsigned long reg_num, u64 val)
David S. Miller3eb80572009-01-21 21:30:23 -080071{
David S. Miller0bab20b2012-08-16 21:16:22 -070072 WARN_ON_ONCE(reg_num != 0);
David S. Miller09d053c2012-08-16 23:19:32 -070073 __asm__ __volatile__("wr %0, 0x0, %%pcr" : : "r" (val));
74}
75
76static u64 direct_pic_read(unsigned long reg_num)
77{
78 u64 val;
79
80 WARN_ON_ONCE(reg_num != 0);
81 __asm__ __volatile__("rd %%pic, %0" : "=r" (val));
82 return val;
83}
84
85static void direct_pic_write(unsigned long reg_num, u64 val)
86{
87 WARN_ON_ONCE(reg_num != 0);
88
89 /* Blackbird errata workaround. See commentary in
90 * arch/sparc64/kernel/smp.c:smp_percpu_timer_interrupt()
91 * for more information.
92 */
93 __asm__ __volatile__("ba,pt %%xcc, 99f\n\t"
94 " nop\n\t"
95 ".align 64\n"
96 "99:wr %0, 0x0, %%pic\n\t"
97 "rd %%pic, %%g0" : : "r" (val));
David S. Miller3eb80572009-01-21 21:30:23 -080098}
99
David S. Miller73a6b052012-08-16 23:26:01 -0700100static u64 direct_picl_value(unsigned int nmi_hz)
101{
102 u32 delta = local_cpu_data().clock_tick / nmi_hz;
103
104 return ((u64)((0 - delta) & 0xffffffff)) << 32;
105}
106
David S. Miller3eb80572009-01-21 21:30:23 -0800107static const struct pcr_ops direct_pcr_ops = {
David S. Miller09d053c2012-08-16 23:19:32 -0700108 .read_pcr = direct_pcr_read,
109 .write_pcr = direct_pcr_write,
110 .read_pic = direct_pic_read,
111 .write_pic = direct_pic_write,
David S. Miller73a6b052012-08-16 23:26:01 -0700112 .nmi_picl_value = direct_picl_value,
David S. Miller3eb80572009-01-21 21:30:23 -0800113};
114
David S. Miller0bab20b2012-08-16 21:16:22 -0700115static void n2_pcr_write(unsigned long reg_num, u64 val)
David S. Miller3eb80572009-01-21 21:30:23 -0800116{
117 unsigned long ret;
118
David S. Miller0bab20b2012-08-16 21:16:22 -0700119 WARN_ON_ONCE(reg_num != 0);
David S. Miller314ff522011-07-27 20:46:25 -0700120 if (val & PCR_N2_HTRACE) {
121 ret = sun4v_niagara2_setperf(HV_N2_PERF_SPARC_CTL, val);
122 if (ret != HV_EOK)
David S. Miller09d053c2012-08-16 23:19:32 -0700123 direct_pcr_write(reg_num, val);
David S. Miller314ff522011-07-27 20:46:25 -0700124 } else
David S. Miller09d053c2012-08-16 23:19:32 -0700125 direct_pcr_write(reg_num, val);
David S. Miller3eb80572009-01-21 21:30:23 -0800126}
127
David S. Miller73a6b052012-08-16 23:26:01 -0700128static u64 n2_picl_value(unsigned int nmi_hz)
129{
130 u32 delta = local_cpu_data().clock_tick / (nmi_hz << 2);
131
132 return ((u64)((0 - delta) & 0xffffffff)) << 32;
133}
134
David S. Miller3eb80572009-01-21 21:30:23 -0800135static const struct pcr_ops n2_pcr_ops = {
David S. Miller09d053c2012-08-16 23:19:32 -0700136 .read_pcr = direct_pcr_read,
137 .write_pcr = n2_pcr_write,
138 .read_pic = direct_pic_read,
139 .write_pic = direct_pic_write,
David S. Miller73a6b052012-08-16 23:26:01 -0700140 .nmi_picl_value = n2_picl_value,
David S. Miller3eb80572009-01-21 21:30:23 -0800141};
142
143static unsigned long perf_hsvc_group;
144static unsigned long perf_hsvc_major;
145static unsigned long perf_hsvc_minor;
146
147static int __init register_perf_hsvc(void)
148{
149 if (tlb_type == hypervisor) {
150 switch (sun4v_chip_type) {
151 case SUN4V_CHIP_NIAGARA1:
152 perf_hsvc_group = HV_GRP_NIAG_PERF;
153 break;
154
155 case SUN4V_CHIP_NIAGARA2:
156 perf_hsvc_group = HV_GRP_N2_CPU;
157 break;
158
David S. Miller4ba991d2011-07-27 21:06:16 -0700159 case SUN4V_CHIP_NIAGARA3:
160 perf_hsvc_group = HV_GRP_KT_CPU;
161 break;
162
David S. Miller3eb80572009-01-21 21:30:23 -0800163 default:
164 return -ENODEV;
165 }
166
167
168 perf_hsvc_major = 1;
169 perf_hsvc_minor = 0;
170 if (sun4v_hvapi_register(perf_hsvc_group,
171 perf_hsvc_major,
172 &perf_hsvc_minor)) {
173 printk("perfmon: Could not register hvapi.\n");
174 return -ENODEV;
175 }
176 }
177 return 0;
178}
179
180static void __init unregister_perf_hsvc(void)
181{
182 if (tlb_type != hypervisor)
183 return;
184 sun4v_hvapi_unregister(perf_hsvc_group);
185}
186
187int __init pcr_arch_init(void)
188{
189 int err = register_perf_hsvc();
190
191 if (err)
192 return err;
193
194 switch (tlb_type) {
195 case hypervisor:
196 pcr_ops = &n2_pcr_ops;
David S. Millere5553a62009-01-29 21:22:47 -0800197 pcr_enable = PCR_N2_ENABLE;
David S. Miller3eb80572009-01-21 21:30:23 -0800198 break;
199
David S. Miller3eb80572009-01-21 21:30:23 -0800200 case cheetah:
201 case cheetah_plus:
202 pcr_ops = &direct_pcr_ops;
David S. Millere5553a62009-01-29 21:22:47 -0800203 pcr_enable = PCR_SUN4U_ENABLE;
David S. Miller3eb80572009-01-21 21:30:23 -0800204 break;
205
David S. Miller1c2f61d2009-02-05 23:59:04 -0800206 case spitfire:
207 /* UltraSPARC-I/II and derivatives lack a profile
208 * counter overflow interrupt so we can't make use of
209 * their hardware currently.
210 */
211 /* fallthrough */
David S. Miller3eb80572009-01-21 21:30:23 -0800212 default:
213 err = -ENODEV;
214 goto out_unregister;
215 }
216
David S. Millere5553a62009-01-29 21:22:47 -0800217 return nmi_init();
David S. Miller3eb80572009-01-21 21:30:23 -0800218
219out_unregister:
220 unregister_perf_hsvc();
221 return err;
222}