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)
62 int perf_num_counters(void)
67 return sh_pmu
->num_events
;
69 EXPORT_SYMBOL_GPL(perf_num_counters
);
72 * Release the PMU if this is the last perf_event.
74 static void hw_perf_event_destroy(struct perf_event
*event
)
76 if (!atomic_add_unless(&num_events
, -1, 1)) {
77 mutex_lock(&pmc_reserve_mutex
);
78 if (atomic_dec_return(&num_events
) == 0)
79 release_pmc_hardware();
80 mutex_unlock(&pmc_reserve_mutex
);
84 static int hw_perf_cache_event(int config
, int *evp
)
86 unsigned long type
, op
, result
;
89 if (!sh_pmu
->cache_events
)
94 op
= (config
>> 8) & 0xff;
95 result
= (config
>> 16) & 0xff;
97 if (type
>= PERF_COUNT_HW_CACHE_MAX
||
98 op
>= PERF_COUNT_HW_CACHE_OP_MAX
||
99 result
>= PERF_COUNT_HW_CACHE_RESULT_MAX
)
102 ev
= (*sh_pmu
->cache_events
)[type
][op
][result
];
111 static int __hw_perf_event_init(struct perf_event
*event
)
113 struct perf_event_attr
*attr
= &event
->attr
;
114 struct hw_perf_event
*hwc
= &event
->hw
;
118 if (!sh_pmu_initialized())
122 * All of the on-chip counters are "limited", in that they have
123 * no interrupts, and are therefore unable to do sampling without
124 * further work and timer assistance.
126 if (hwc
->sample_period
)
130 * See if we need to reserve the counter.
132 * If no events are currently in use, then we have to take a
133 * mutex to ensure that we don't race with another task doing
134 * reserve_pmc_hardware or release_pmc_hardware.
137 if (!atomic_inc_not_zero(&num_events
)) {
138 mutex_lock(&pmc_reserve_mutex
);
139 if (atomic_read(&num_events
) == 0 &&
140 reserve_pmc_hardware())
143 atomic_inc(&num_events
);
144 mutex_unlock(&pmc_reserve_mutex
);
150 event
->destroy
= hw_perf_event_destroy
;
152 switch (attr
->type
) {
154 config
= attr
->config
& sh_pmu
->raw_event_mask
;
156 case PERF_TYPE_HW_CACHE
:
157 err
= hw_perf_cache_event(attr
->config
, &config
);
161 case PERF_TYPE_HARDWARE
:
162 if (attr
->config
>= sh_pmu
->max_events
)
165 config
= sh_pmu
->event_map(attr
->config
);
172 hwc
->config
|= config
;
177 static void sh_perf_event_update(struct perf_event
*event
,
178 struct hw_perf_event
*hwc
, int idx
)
180 u64 prev_raw_count
, new_raw_count
;
185 * Depending on the counter configuration, they may or may not
186 * be chained, in which case the previous counter value can be
187 * updated underneath us if the lower-half overflows.
189 * Our tactic to handle this is to first atomically read and
190 * exchange a new raw count - then add that new-prev delta
191 * count to the generic counter atomically.
193 * As there is no interrupt associated with the overflow events,
194 * this is the simplest approach for maintaining consistency.
197 prev_raw_count
= local64_read(&hwc
->prev_count
);
198 new_raw_count
= sh_pmu
->read(idx
);
200 if (local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
201 new_raw_count
) != prev_raw_count
)
205 * Now we have the new raw value and have updated the prev
206 * timestamp already. We can now calculate the elapsed delta
207 * (counter-)time and add that to the generic counter.
209 * Careful, not all hw sign-extends above the physical width
212 delta
= (new_raw_count
<< shift
) - (prev_raw_count
<< shift
);
215 local64_add(delta
, &event
->count
);
218 static void sh_pmu_disable(struct perf_event
*event
)
220 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
221 struct hw_perf_event
*hwc
= &event
->hw
;
224 clear_bit(idx
, cpuc
->active_mask
);
225 sh_pmu
->disable(hwc
, idx
);
229 sh_perf_event_update(event
, &event
->hw
, idx
);
231 cpuc
->events
[idx
] = NULL
;
232 clear_bit(idx
, cpuc
->used_mask
);
234 perf_event_update_userpage(event
);
237 static int sh_pmu_enable(struct perf_event
*event
)
239 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
240 struct hw_perf_event
*hwc
= &event
->hw
;
243 if (test_and_set_bit(idx
, cpuc
->used_mask
)) {
244 idx
= find_first_zero_bit(cpuc
->used_mask
, sh_pmu
->num_events
);
245 if (idx
== sh_pmu
->num_events
)
248 set_bit(idx
, cpuc
->used_mask
);
252 sh_pmu
->disable(hwc
, idx
);
254 cpuc
->events
[idx
] = event
;
255 set_bit(idx
, cpuc
->active_mask
);
257 sh_pmu
->enable(hwc
, idx
);
259 perf_event_update_userpage(event
);
264 static void sh_pmu_read(struct perf_event
*event
)
266 sh_perf_event_update(event
, &event
->hw
, event
->hw
.idx
);
269 static const struct pmu pmu
= {
270 .enable
= sh_pmu_enable
,
271 .disable
= sh_pmu_disable
,
275 const struct pmu
*hw_perf_event_init(struct perf_event
*event
)
277 int err
= __hw_perf_event_init(event
);
280 event
->destroy(event
);
287 static void sh_pmu_setup(int cpu
)
289 struct cpu_hw_events
*cpuhw
= &per_cpu(cpu_hw_events
, cpu
);
291 memset(cpuhw
, 0, sizeof(struct cpu_hw_events
));
295 sh_pmu_notifier(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
297 unsigned int cpu
= (long)hcpu
;
299 switch (action
& ~CPU_TASKS_FROZEN
) {
311 void hw_perf_enable(void)
313 if (!sh_pmu_initialized())
316 sh_pmu
->enable_all();
319 void hw_perf_disable(void)
321 if (!sh_pmu_initialized())
324 sh_pmu
->disable_all();
327 int __cpuinit
register_sh_pmu(struct sh_pmu
*pmu
)
333 pr_info("Performance Events: %s support registered\n", pmu
->name
);
335 WARN_ON(pmu
->num_events
> MAX_HWEVENTS
);
337 perf_cpu_notifier(sh_pmu_notifier
);