1 /* Performance counter support for sparc64.
3 * Copyright (C) 2009 David S. Miller <davem@davemloft.net>
5 * This code is based almost entirely upon the x86 perf counter
8 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
9 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
10 * Copyright (C) 2009 Jaswinder Singh Rajput
11 * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
12 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
15 #include <linux/perf_counter.h>
16 #include <linux/kprobes.h>
17 #include <linux/kernel.h>
18 #include <linux/kdebug.h>
19 #include <linux/mutex.h>
21 #include <asm/cpudata.h>
22 #include <asm/atomic.h>
26 /* Sparc64 chips have two performance counters, 32-bits each, with
27 * overflow interrupts generated on transition from 0xffffffff to 0.
28 * The counters are accessed in one go using a 64-bit register.
30 * Both counters are controlled using a single control register. The
31 * only way to stop all sampling is to clear all of the context (user,
32 * supervisor, hypervisor) sampling enable bits. But these bits apply
33 * to both counters, thus the two counters can't be enabled/disabled
36 * The control register has two event fields, one for each of the two
37 * counters. It's thus nearly impossible to have one counter going
38 * while keeping the other one stopped. Therefore it is possible to
39 * get overflow interrupts for counters not currently "in use" and
40 * that condition must be checked in the overflow interrupt handler.
42 * So we use a hack, in that we program inactive counters with the
43 * "sw_count0" and "sw_count1" events. These count how many times
44 * the instruction "sethi %hi(0xfc000), %g0" is executed. It's an
45 * unusual way to encode a NOP and therefore will not trigger in
49 #define MAX_HWCOUNTERS 2
50 #define MAX_PERIOD ((1UL << 32) - 1)
52 #define PIC_UPPER_INDEX 0
53 #define PIC_LOWER_INDEX 1
55 #define PIC_UPPER_NOP 0x1c
56 #define PIC_LOWER_NOP 0x14
58 struct cpu_hw_counters
{
59 struct perf_counter
*counters
[MAX_HWCOUNTERS
];
60 unsigned long used_mask
[BITS_TO_LONGS(MAX_HWCOUNTERS
)];
61 unsigned long active_mask
[BITS_TO_LONGS(MAX_HWCOUNTERS
)];
64 DEFINE_PER_CPU(struct cpu_hw_counters
, cpu_hw_counters
) = { .enabled
= 1, };
66 struct perf_event_map
{
70 #define PIC_UPPER 0x01
71 #define PIC_LOWER 0x02
75 const struct perf_event_map
*(*event_map
)(int);
82 static const struct perf_event_map ultra3i_perfmon_event_map
[] = {
83 [PERF_COUNT_HW_CPU_CYCLES
] = { 0x0000, PIC_UPPER
| PIC_LOWER
},
84 [PERF_COUNT_HW_INSTRUCTIONS
] = { 0x0001, PIC_UPPER
| PIC_LOWER
},
85 [PERF_COUNT_HW_CACHE_REFERENCES
] = { 0x0009, PIC_LOWER
},
86 [PERF_COUNT_HW_CACHE_MISSES
] = { 0x0009, PIC_UPPER
},
89 static const struct perf_event_map
*ultra3i_event_map(int event
)
91 return &ultra3i_perfmon_event_map
[event
];
94 static const struct sparc_pmu ultra3i_pmu
= {
95 .event_map
= ultra3i_event_map
,
96 .max_events
= ARRAY_SIZE(ultra3i_perfmon_event_map
),
102 static const struct sparc_pmu
*sparc_pmu __read_mostly
;
104 static u64
event_encoding(u64 event
, int idx
)
106 if (idx
== PIC_UPPER_INDEX
)
107 event
<<= sparc_pmu
->upper_shift
;
109 event
<<= sparc_pmu
->lower_shift
;
113 static u64
mask_for_index(int idx
)
115 return event_encoding(sparc_pmu
->event_mask
, idx
);
118 static u64
nop_for_index(int idx
)
120 return event_encoding(idx
== PIC_UPPER_INDEX
?
121 PIC_UPPER_NOP
: PIC_LOWER_NOP
, idx
);
124 static inline void sparc_pmu_enable_counter(struct hw_perf_counter
*hwc
,
127 u64 val
, mask
= mask_for_index(idx
);
129 val
= pcr_ops
->read();
130 pcr_ops
->write((val
& ~mask
) | hwc
->config
);
133 static inline void sparc_pmu_disable_counter(struct hw_perf_counter
*hwc
,
136 u64 mask
= mask_for_index(idx
);
137 u64 nop
= nop_for_index(idx
);
138 u64 val
= pcr_ops
->read();
140 pcr_ops
->write((val
& ~mask
) | nop
);
143 void hw_perf_enable(void)
145 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
155 val
= pcr_ops
->read();
157 for (i
= 0; i
< MAX_HWCOUNTERS
; i
++) {
158 struct perf_counter
*cp
= cpuc
->counters
[i
];
159 struct hw_perf_counter
*hwc
;
164 val
|= hwc
->config_base
;
170 void hw_perf_disable(void)
172 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
180 val
= pcr_ops
->read();
181 val
&= ~(PCR_UTRACE
| PCR_STRACE
);
185 static u32
read_pmc(int idx
)
190 if (idx
== PIC_UPPER_INDEX
)
193 return val
& 0xffffffff;
196 static void write_pmc(int idx
, u64 val
)
198 u64 shift
, mask
, pic
;
201 if (idx
== PIC_UPPER_INDEX
)
204 mask
= ((u64
) 0xffffffff) << shift
;
213 static int sparc_perf_counter_set_period(struct perf_counter
*counter
,
214 struct hw_perf_counter
*hwc
, int idx
)
216 s64 left
= atomic64_read(&hwc
->period_left
);
217 s64 period
= hwc
->sample_period
;
220 if (unlikely(left
<= -period
)) {
222 atomic64_set(&hwc
->period_left
, left
);
223 hwc
->last_period
= period
;
227 if (unlikely(left
<= 0)) {
229 atomic64_set(&hwc
->period_left
, left
);
230 hwc
->last_period
= period
;
233 if (left
> MAX_PERIOD
)
236 atomic64_set(&hwc
->prev_count
, (u64
)-left
);
238 write_pmc(idx
, (u64
)(-left
) & 0xffffffff);
240 perf_counter_update_userpage(counter
);
245 static int sparc_pmu_enable(struct perf_counter
*counter
)
247 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
248 struct hw_perf_counter
*hwc
= &counter
->hw
;
251 if (test_and_set_bit(idx
, cpuc
->used_mask
))
254 sparc_pmu_disable_counter(hwc
, idx
);
256 cpuc
->counters
[idx
] = counter
;
257 set_bit(idx
, cpuc
->active_mask
);
259 sparc_perf_counter_set_period(counter
, hwc
, idx
);
260 sparc_pmu_enable_counter(hwc
, idx
);
261 perf_counter_update_userpage(counter
);
265 static u64
sparc_perf_counter_update(struct perf_counter
*counter
,
266 struct hw_perf_counter
*hwc
, int idx
)
269 u64 prev_raw_count
, new_raw_count
;
273 prev_raw_count
= atomic64_read(&hwc
->prev_count
);
274 new_raw_count
= read_pmc(idx
);
276 if (atomic64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
277 new_raw_count
) != prev_raw_count
)
280 delta
= (new_raw_count
<< shift
) - (prev_raw_count
<< shift
);
283 atomic64_add(delta
, &counter
->count
);
284 atomic64_sub(delta
, &hwc
->period_left
);
286 return new_raw_count
;
289 static void sparc_pmu_disable(struct perf_counter
*counter
)
291 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
292 struct hw_perf_counter
*hwc
= &counter
->hw
;
295 clear_bit(idx
, cpuc
->active_mask
);
296 sparc_pmu_disable_counter(hwc
, idx
);
300 sparc_perf_counter_update(counter
, hwc
, idx
);
301 cpuc
->counters
[idx
] = NULL
;
302 clear_bit(idx
, cpuc
->used_mask
);
304 perf_counter_update_userpage(counter
);
307 static void sparc_pmu_read(struct perf_counter
*counter
)
309 struct hw_perf_counter
*hwc
= &counter
->hw
;
310 sparc_perf_counter_update(counter
, hwc
, hwc
->idx
);
313 static void sparc_pmu_unthrottle(struct perf_counter
*counter
)
315 struct hw_perf_counter
*hwc
= &counter
->hw
;
316 sparc_pmu_enable_counter(hwc
, hwc
->idx
);
319 static atomic_t active_counters
= ATOMIC_INIT(0);
320 static DEFINE_MUTEX(pmc_grab_mutex
);
322 void perf_counter_grab_pmc(void)
324 if (atomic_inc_not_zero(&active_counters
))
327 mutex_lock(&pmc_grab_mutex
);
328 if (atomic_read(&active_counters
) == 0) {
329 if (atomic_read(&nmi_active
) > 0) {
330 on_each_cpu(stop_nmi_watchdog
, NULL
, 1);
331 BUG_ON(atomic_read(&nmi_active
) != 0);
333 atomic_inc(&active_counters
);
335 mutex_unlock(&pmc_grab_mutex
);
338 void perf_counter_release_pmc(void)
340 if (atomic_dec_and_mutex_lock(&active_counters
, &pmc_grab_mutex
)) {
341 if (atomic_read(&nmi_active
) == 0)
342 on_each_cpu(start_nmi_watchdog
, NULL
, 1);
343 mutex_unlock(&pmc_grab_mutex
);
347 static void hw_perf_counter_destroy(struct perf_counter
*counter
)
349 perf_counter_release_pmc();
352 static int __hw_perf_counter_init(struct perf_counter
*counter
)
354 struct perf_counter_attr
*attr
= &counter
->attr
;
355 struct hw_perf_counter
*hwc
= &counter
->hw
;
356 const struct perf_event_map
*pmap
;
359 if (atomic_read(&nmi_active
) < 0)
362 if (attr
->type
!= PERF_TYPE_HARDWARE
)
365 if (attr
->config
>= sparc_pmu
->max_events
)
368 perf_counter_grab_pmc();
369 counter
->destroy
= hw_perf_counter_destroy
;
371 /* We save the enable bits in the config_base. So to
372 * turn off sampling just write 'config', and to enable
373 * things write 'config | config_base'.
375 hwc
->config_base
= 0;
376 if (!attr
->exclude_user
)
377 hwc
->config_base
|= PCR_UTRACE
;
378 if (!attr
->exclude_kernel
)
379 hwc
->config_base
|= PCR_STRACE
;
381 if (!hwc
->sample_period
) {
382 hwc
->sample_period
= MAX_PERIOD
;
383 hwc
->last_period
= hwc
->sample_period
;
384 atomic64_set(&hwc
->period_left
, hwc
->sample_period
);
387 pmap
= sparc_pmu
->event_map(attr
->config
);
389 enc
= pmap
->encoding
;
390 if (pmap
->pic_mask
& PIC_UPPER
) {
391 hwc
->idx
= PIC_UPPER_INDEX
;
392 enc
<<= sparc_pmu
->upper_shift
;
394 hwc
->idx
= PIC_LOWER_INDEX
;
395 enc
<<= sparc_pmu
->lower_shift
;
402 static const struct pmu pmu
= {
403 .enable
= sparc_pmu_enable
,
404 .disable
= sparc_pmu_disable
,
405 .read
= sparc_pmu_read
,
406 .unthrottle
= sparc_pmu_unthrottle
,
409 const struct pmu
*hw_perf_counter_init(struct perf_counter
*counter
)
411 int err
= __hw_perf_counter_init(counter
);
418 void perf_counter_print_debug(void)
427 local_irq_save(flags
);
429 cpu
= smp_processor_id();
431 pcr
= pcr_ops
->read();
435 pr_info("CPU#%d: PCR[%016llx] PIC[%016llx]\n",
438 local_irq_restore(flags
);
441 static int __kprobes
perf_counter_nmi_handler(struct notifier_block
*self
,
442 unsigned long cmd
, void *__args
)
444 struct die_args
*args
= __args
;
445 struct perf_sample_data data
;
446 struct cpu_hw_counters
*cpuc
;
447 struct pt_regs
*regs
;
450 if (!atomic_read(&active_counters
))
466 cpuc
= &__get_cpu_var(cpu_hw_counters
);
467 for (idx
= 0; idx
< MAX_HWCOUNTERS
; idx
++) {
468 struct perf_counter
*counter
= cpuc
->counters
[idx
];
469 struct hw_perf_counter
*hwc
;
472 if (!test_bit(idx
, cpuc
->active_mask
))
475 val
= sparc_perf_counter_update(counter
, hwc
, idx
);
476 if (val
& (1ULL << 31))
479 data
.period
= counter
->hw
.last_period
;
480 if (!sparc_perf_counter_set_period(counter
, hwc
, idx
))
483 if (perf_counter_overflow(counter
, 1, &data
))
484 sparc_pmu_disable_counter(hwc
, idx
);
490 static __read_mostly
struct notifier_block perf_counter_nmi_notifier
= {
491 .notifier_call
= perf_counter_nmi_handler
,
494 static bool __init
supported_pmu(void)
496 if (!strcmp(sparc_pmu_type
, "ultra3i")) {
497 sparc_pmu
= &ultra3i_pmu
;
503 void __init
init_hw_perf_counters(void)
505 pr_info("Performance counters: ");
507 if (!supported_pmu()) {
508 pr_cont("No support for PMU type '%s'\n", sparc_pmu_type
);
512 pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type
);
514 /* All sparc64 PMUs currently have 2 counters. But this simple
515 * driver only supports one active counter at a time.
517 perf_max_counters
= 1;
519 register_die_notifier(&perf_counter_nmi_notifier
);