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 struct cpu_hw_counters {
56 struct perf_counter *counters[MAX_HWCOUNTERS];
57 unsigned long used_mask[BITS_TO_LONGS(MAX_HWCOUNTERS)];
58 unsigned long active_mask[BITS_TO_LONGS(MAX_HWCOUNTERS)];
61 DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = { .enabled = 1, };
63 struct perf_event_map {
67 #define PIC_UPPER 0x01
68 #define PIC_LOWER 0x02
72 const struct perf_event_map *(*event_map)(int);
83 static const struct perf_event_map ultra3i_perfmon_event_map[] = {
84 [PERF_COUNT_HW_CPU_CYCLES] = { 0x0000, PIC_UPPER | PIC_LOWER },
85 [PERF_COUNT_HW_INSTRUCTIONS] = { 0x0001, PIC_UPPER | PIC_LOWER },
86 [PERF_COUNT_HW_CACHE_REFERENCES] = { 0x0009, PIC_LOWER },
87 [PERF_COUNT_HW_CACHE_MISSES] = { 0x0009, PIC_UPPER },
90 static const struct perf_event_map *ultra3i_event_map(int event)
92 return &ultra3i_perfmon_event_map[event];
95 static const struct sparc_pmu ultra3i_pmu = {
96 .event_map = ultra3i_event_map,
97 .max_events = ARRAY_SIZE(ultra3i_perfmon_event_map),
105 static const struct sparc_pmu *sparc_pmu __read_mostly;
107 static u64 event_encoding(u64 event, int idx)
109 if (idx == PIC_UPPER_INDEX)
110 event <<= sparc_pmu->upper_shift;
112 event <<= sparc_pmu->lower_shift;
116 static u64 mask_for_index(int idx)
118 return event_encoding(sparc_pmu->event_mask, idx);
121 static u64 nop_for_index(int idx)
123 return event_encoding(idx == PIC_UPPER_INDEX ?
124 sparc_pmu->upper_nop :
125 sparc_pmu->lower_nop, idx);
128 static inline void sparc_pmu_enable_counter(struct hw_perf_counter *hwc,
131 u64 val, mask = mask_for_index(idx);
133 val = pcr_ops->read();
134 pcr_ops->write((val & ~mask) | hwc->config);
137 static inline void sparc_pmu_disable_counter(struct hw_perf_counter *hwc,
140 u64 mask = mask_for_index(idx);
141 u64 nop = nop_for_index(idx);
142 u64 val = pcr_ops->read();
144 pcr_ops->write((val & ~mask) | nop);
147 void hw_perf_enable(void)
149 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
159 val = pcr_ops->read();
161 for (i = 0; i < MAX_HWCOUNTERS; i++) {
162 struct perf_counter *cp = cpuc->counters[i];
163 struct hw_perf_counter *hwc;
168 val |= hwc->config_base;
174 void hw_perf_disable(void)
176 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
184 val = pcr_ops->read();
185 val &= ~(PCR_UTRACE | PCR_STRACE |
186 sparc_pmu->hv_bit | sparc_pmu->irq_bit);
190 static u32 read_pmc(int idx)
195 if (idx == PIC_UPPER_INDEX)
198 return val & 0xffffffff;
201 static void write_pmc(int idx, u64 val)
203 u64 shift, mask, pic;
206 if (idx == PIC_UPPER_INDEX)
209 mask = ((u64) 0xffffffff) << shift;
218 static int sparc_perf_counter_set_period(struct perf_counter *counter,
219 struct hw_perf_counter *hwc, int idx)
221 s64 left = atomic64_read(&hwc->period_left);
222 s64 period = hwc->sample_period;
225 if (unlikely(left <= -period)) {
227 atomic64_set(&hwc->period_left, left);
228 hwc->last_period = period;
232 if (unlikely(left <= 0)) {
234 atomic64_set(&hwc->period_left, left);
235 hwc->last_period = period;
238 if (left > MAX_PERIOD)
241 atomic64_set(&hwc->prev_count, (u64)-left);
243 write_pmc(idx, (u64)(-left) & 0xffffffff);
245 perf_counter_update_userpage(counter);
250 static int sparc_pmu_enable(struct perf_counter *counter)
252 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
253 struct hw_perf_counter *hwc = &counter->hw;
256 if (test_and_set_bit(idx, cpuc->used_mask))
259 sparc_pmu_disable_counter(hwc, idx);
261 cpuc->counters[idx] = counter;
262 set_bit(idx, cpuc->active_mask);
264 sparc_perf_counter_set_period(counter, hwc, idx);
265 sparc_pmu_enable_counter(hwc, idx);
266 perf_counter_update_userpage(counter);
270 static u64 sparc_perf_counter_update(struct perf_counter *counter,
271 struct hw_perf_counter *hwc, int idx)
274 u64 prev_raw_count, new_raw_count;
278 prev_raw_count = atomic64_read(&hwc->prev_count);
279 new_raw_count = read_pmc(idx);
281 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
282 new_raw_count) != prev_raw_count)
285 delta = (new_raw_count << shift) - (prev_raw_count << shift);
288 atomic64_add(delta, &counter->count);
289 atomic64_sub(delta, &hwc->period_left);
291 return new_raw_count;
294 static void sparc_pmu_disable(struct perf_counter *counter)
296 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
297 struct hw_perf_counter *hwc = &counter->hw;
300 clear_bit(idx, cpuc->active_mask);
301 sparc_pmu_disable_counter(hwc, idx);
305 sparc_perf_counter_update(counter, hwc, idx);
306 cpuc->counters[idx] = NULL;
307 clear_bit(idx, cpuc->used_mask);
309 perf_counter_update_userpage(counter);
312 static void sparc_pmu_read(struct perf_counter *counter)
314 struct hw_perf_counter *hwc = &counter->hw;
315 sparc_perf_counter_update(counter, hwc, hwc->idx);
318 static void sparc_pmu_unthrottle(struct perf_counter *counter)
320 struct hw_perf_counter *hwc = &counter->hw;
321 sparc_pmu_enable_counter(hwc, hwc->idx);
324 static atomic_t active_counters = ATOMIC_INIT(0);
325 static DEFINE_MUTEX(pmc_grab_mutex);
327 void perf_counter_grab_pmc(void)
329 if (atomic_inc_not_zero(&active_counters))
332 mutex_lock(&pmc_grab_mutex);
333 if (atomic_read(&active_counters) == 0) {
334 if (atomic_read(&nmi_active) > 0) {
335 on_each_cpu(stop_nmi_watchdog, NULL, 1);
336 BUG_ON(atomic_read(&nmi_active) != 0);
338 atomic_inc(&active_counters);
340 mutex_unlock(&pmc_grab_mutex);
343 void perf_counter_release_pmc(void)
345 if (atomic_dec_and_mutex_lock(&active_counters, &pmc_grab_mutex)) {
346 if (atomic_read(&nmi_active) == 0)
347 on_each_cpu(start_nmi_watchdog, NULL, 1);
348 mutex_unlock(&pmc_grab_mutex);
352 static void hw_perf_counter_destroy(struct perf_counter *counter)
354 perf_counter_release_pmc();
357 static int __hw_perf_counter_init(struct perf_counter *counter)
359 struct perf_counter_attr *attr = &counter->attr;
360 struct hw_perf_counter *hwc = &counter->hw;
361 const struct perf_event_map *pmap;
364 if (atomic_read(&nmi_active) < 0)
367 if (attr->type != PERF_TYPE_HARDWARE)
370 if (attr->config >= sparc_pmu->max_events)
373 perf_counter_grab_pmc();
374 counter->destroy = hw_perf_counter_destroy;
376 /* We save the enable bits in the config_base. So to
377 * turn off sampling just write 'config', and to enable
378 * things write 'config | config_base'.
380 hwc->config_base = sparc_pmu->irq_bit;
381 if (!attr->exclude_user)
382 hwc->config_base |= PCR_UTRACE;
383 if (!attr->exclude_kernel)
384 hwc->config_base |= PCR_STRACE;
385 if (!attr->exclude_hv)
386 hwc->config_base |= sparc_pmu->hv_bit;
388 if (!hwc->sample_period) {
389 hwc->sample_period = MAX_PERIOD;
390 hwc->last_period = hwc->sample_period;
391 atomic64_set(&hwc->period_left, hwc->sample_period);
394 pmap = sparc_pmu->event_map(attr->config);
396 enc = pmap->encoding;
397 if (pmap->pic_mask & PIC_UPPER) {
398 hwc->idx = PIC_UPPER_INDEX;
399 enc <<= sparc_pmu->upper_shift;
401 hwc->idx = PIC_LOWER_INDEX;
402 enc <<= sparc_pmu->lower_shift;
409 static const struct pmu pmu = {
410 .enable = sparc_pmu_enable,
411 .disable = sparc_pmu_disable,
412 .read = sparc_pmu_read,
413 .unthrottle = sparc_pmu_unthrottle,
416 const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
418 int err = __hw_perf_counter_init(counter);
425 void perf_counter_print_debug(void)
434 local_irq_save(flags);
436 cpu = smp_processor_id();
438 pcr = pcr_ops->read();
442 pr_info("CPU#%d: PCR[%016llx] PIC[%016llx]\n",
445 local_irq_restore(flags);
448 static int __kprobes perf_counter_nmi_handler(struct notifier_block *self,
449 unsigned long cmd, void *__args)
451 struct die_args *args = __args;
452 struct perf_sample_data data;
453 struct cpu_hw_counters *cpuc;
454 struct pt_regs *regs;
457 if (!atomic_read(&active_counters))
473 cpuc = &__get_cpu_var(cpu_hw_counters);
474 for (idx = 0; idx < MAX_HWCOUNTERS; idx++) {
475 struct perf_counter *counter = cpuc->counters[idx];
476 struct hw_perf_counter *hwc;
479 if (!test_bit(idx, cpuc->active_mask))
482 val = sparc_perf_counter_update(counter, hwc, idx);
483 if (val & (1ULL << 31))
486 data.period = counter->hw.last_period;
487 if (!sparc_perf_counter_set_period(counter, hwc, idx))
490 if (perf_counter_overflow(counter, 1, &data))
491 sparc_pmu_disable_counter(hwc, idx);
497 static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
498 .notifier_call = perf_counter_nmi_handler,
501 static bool __init supported_pmu(void)
503 if (!strcmp(sparc_pmu_type, "ultra3i")) {
504 sparc_pmu = &ultra3i_pmu;
510 void __init init_hw_perf_counters(void)
512 pr_info("Performance counters: ");
514 if (!supported_pmu()) {
515 pr_cont("No support for PMU type '%s'\n", sparc_pmu_type);
519 pr_cont("Supported PMU type is '%s'\n", sparc_pmu_type);
521 /* All sparc64 PMUs currently have 2 counters. But this simple
522 * driver only supports one active counter at a time.
524 perf_max_counters = 1;
526 register_die_notifier(&perf_counter_nmi_notifier);