2 * Performance event support framework for SuperH hardware counters.
4 * Copyright (C) 2009 Paul Mundt
6 * Heavily based on the x86 and PowerPC implementations.
9 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
10 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
11 * Copyright (C) 2009 Jaswinder Singh Rajput
12 * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
13 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
14 * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
17 * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
19 * This file is subject to the terms and conditions of the GNU General Public
20 * License. See the file "COPYING" in the main directory of this archive
23 #include <linux/kernel.h>
24 #include <linux/init.h>
26 #include <linux/irq.h>
27 #include <linux/perf_event.h>
28 #include <asm/processor.h>
30 struct cpu_hw_events {
31 struct perf_event *events[MAX_HWEVENTS];
32 unsigned long used_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
33 unsigned long active_mask[BITS_TO_LONGS(MAX_HWEVENTS)];
36 DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
38 static struct sh_pmu *sh_pmu __read_mostly;
40 /* Number of perf_events counting hardware events */
41 static atomic_t num_events;
42 /* Used to avoid races in calling reserve/release_pmc_hardware */
43 static DEFINE_MUTEX(pmc_reserve_mutex);
46 * Stub these out for now, do something more profound later.
48 int reserve_pmc_hardware(void)
53 void release_pmc_hardware(void)
57 static inline int sh_pmu_initialized(void)
63 * Release the PMU if this is the last perf_event.
65 static void hw_perf_event_destroy(struct perf_event *event)
67 if (!atomic_add_unless(&num_events, -1, 1)) {
68 mutex_lock(&pmc_reserve_mutex);
69 if (atomic_dec_return(&num_events) == 0)
70 release_pmc_hardware();
71 mutex_unlock(&pmc_reserve_mutex);
75 static int hw_perf_cache_event(int config, int *evp)
77 unsigned long type, op, result;
80 if (!sh_pmu->cache_events)
85 op = (config >> 8) & 0xff;
86 result = (config >> 16) & 0xff;
88 if (type >= PERF_COUNT_HW_CACHE_MAX ||
89 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
90 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
93 ev = (*sh_pmu->cache_events)[type][op][result];
102 static int __hw_perf_event_init(struct perf_event *event)
104 struct perf_event_attr *attr = &event->attr;
105 struct hw_perf_event *hwc = &event->hw;
109 if (!sh_pmu_initialized())
113 * All of the on-chip counters are "limited", in that they have
114 * no interrupts, and are therefore unable to do sampling without
115 * further work and timer assistance.
117 if (hwc->sample_period)
121 * See if we need to reserve the counter.
123 * If no events are currently in use, then we have to take a
124 * mutex to ensure that we don't race with another task doing
125 * reserve_pmc_hardware or release_pmc_hardware.
128 if (!atomic_inc_not_zero(&num_events)) {
129 mutex_lock(&pmc_reserve_mutex);
130 if (atomic_read(&num_events) == 0 &&
131 reserve_pmc_hardware())
134 atomic_inc(&num_events);
135 mutex_unlock(&pmc_reserve_mutex);
141 event->destroy = hw_perf_event_destroy;
143 switch (attr->type) {
145 config = attr->config & sh_pmu->raw_event_mask;
147 case PERF_TYPE_HW_CACHE:
148 err = hw_perf_cache_event(attr->config, &config);
152 case PERF_TYPE_HARDWARE:
153 if (attr->config >= sh_pmu->max_events)
156 config = sh_pmu->event_map(attr->config);
165 hwc->config |= config;
170 static void sh_perf_event_update(struct perf_event *event,
171 struct hw_perf_event *hwc, int idx)
173 u64 prev_raw_count, new_raw_count;
178 * Depending on the counter configuration, they may or may not
179 * be chained, in which case the previous counter value can be
180 * updated underneath us if the lower-half overflows.
182 * Our tactic to handle this is to first atomically read and
183 * exchange a new raw count - then add that new-prev delta
184 * count to the generic counter atomically.
186 * As there is no interrupt associated with the overflow events,
187 * this is the simplest approach for maintaining consistency.
190 prev_raw_count = atomic64_read(&hwc->prev_count);
191 new_raw_count = sh_pmu->read(idx);
193 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
194 new_raw_count) != prev_raw_count)
198 * Now we have the new raw value and have updated the prev
199 * timestamp already. We can now calculate the elapsed delta
200 * (counter-)time and add that to the generic counter.
202 * Careful, not all hw sign-extends above the physical width
205 delta = (new_raw_count << shift) - (prev_raw_count << shift);
208 atomic64_add(delta, &event->count);
211 static void sh_pmu_disable(struct perf_event *event)
213 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
214 struct hw_perf_event *hwc = &event->hw;
217 clear_bit(idx, cpuc->active_mask);
218 sh_pmu->disable(hwc, idx);
222 sh_perf_event_update(event, &event->hw, idx);
224 cpuc->events[idx] = NULL;
225 clear_bit(idx, cpuc->used_mask);
227 perf_event_update_userpage(event);
230 static int sh_pmu_enable(struct perf_event *event)
232 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
233 struct hw_perf_event *hwc = &event->hw;
236 if (test_and_set_bit(idx, cpuc->used_mask)) {
237 idx = find_first_zero_bit(cpuc->used_mask, sh_pmu->num_events);
238 if (idx == sh_pmu->num_events)
241 set_bit(idx, cpuc->used_mask);
245 sh_pmu->disable(hwc, idx);
247 cpuc->events[idx] = event;
248 set_bit(idx, cpuc->active_mask);
250 sh_pmu->enable(hwc, idx);
252 perf_event_update_userpage(event);
257 static void sh_pmu_read(struct perf_event *event)
259 sh_perf_event_update(event, &event->hw, event->hw.idx);
262 static const struct pmu pmu = {
263 .enable = sh_pmu_enable,
264 .disable = sh_pmu_disable,
268 const struct pmu *hw_perf_event_init(struct perf_event *event)
270 int err = __hw_perf_event_init(event);
273 event->destroy(event);
280 void hw_perf_event_setup(int cpu)
282 struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
284 memset(cpuhw, 0, sizeof(struct cpu_hw_events));
287 void hw_perf_enable(void)
289 if (!sh_pmu_initialized())
292 sh_pmu->enable_all();
295 void hw_perf_disable(void)
297 if (!sh_pmu_initialized())
300 sh_pmu->disable_all();
303 int register_sh_pmu(struct sh_pmu *pmu)
309 pr_info("Performance Events: %s support registered\n", pmu->name);
311 WARN_ON(pmu->num_events > MAX_HWEVENTS);