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
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 <linux/export.h>
29 #include <asm/processor.h>
31 struct cpu_hw_events
{
32 struct perf_event
*events
[MAX_HWEVENTS
];
33 unsigned long used_mask
[BITS_TO_LONGS(MAX_HWEVENTS
)];
34 unsigned long active_mask
[BITS_TO_LONGS(MAX_HWEVENTS
)];
37 DEFINE_PER_CPU(struct cpu_hw_events
, cpu_hw_events
);
39 static struct sh_pmu
*sh_pmu __read_mostly
;
41 /* Number of perf_events counting hardware events */
42 static atomic_t num_events
;
43 /* Used to avoid races in calling reserve/release_pmc_hardware */
44 static DEFINE_MUTEX(pmc_reserve_mutex
);
47 * Stub these out for now, do something more profound later.
49 int reserve_pmc_hardware(void)
54 void release_pmc_hardware(void)
58 static inline int sh_pmu_initialized(void)
63 const char *perf_pmu_name(void)
70 EXPORT_SYMBOL_GPL(perf_pmu_name
);
72 int perf_num_counters(void)
77 return sh_pmu
->num_events
;
79 EXPORT_SYMBOL_GPL(perf_num_counters
);
82 * Release the PMU if this is the last perf_event.
84 static void hw_perf_event_destroy(struct perf_event
*event
)
86 if (!atomic_add_unless(&num_events
, -1, 1)) {
87 mutex_lock(&pmc_reserve_mutex
);
88 if (atomic_dec_return(&num_events
) == 0)
89 release_pmc_hardware();
90 mutex_unlock(&pmc_reserve_mutex
);
94 static int hw_perf_cache_event(int config
, int *evp
)
96 unsigned long type
, op
, result
;
99 if (!sh_pmu
->cache_events
)
103 type
= config
& 0xff;
104 op
= (config
>> 8) & 0xff;
105 result
= (config
>> 16) & 0xff;
107 if (type
>= PERF_COUNT_HW_CACHE_MAX
||
108 op
>= PERF_COUNT_HW_CACHE_OP_MAX
||
109 result
>= PERF_COUNT_HW_CACHE_RESULT_MAX
)
112 ev
= (*sh_pmu
->cache_events
)[type
][op
][result
];
121 static int __hw_perf_event_init(struct perf_event
*event
)
123 struct perf_event_attr
*attr
= &event
->attr
;
124 struct hw_perf_event
*hwc
= &event
->hw
;
128 if (!sh_pmu_initialized())
132 * See if we need to reserve the counter.
134 * If no events are currently in use, then we have to take a
135 * mutex to ensure that we don't race with another task doing
136 * reserve_pmc_hardware or release_pmc_hardware.
139 if (!atomic_inc_not_zero(&num_events
)) {
140 mutex_lock(&pmc_reserve_mutex
);
141 if (atomic_read(&num_events
) == 0 &&
142 reserve_pmc_hardware())
145 atomic_inc(&num_events
);
146 mutex_unlock(&pmc_reserve_mutex
);
152 event
->destroy
= hw_perf_event_destroy
;
154 switch (attr
->type
) {
156 config
= attr
->config
& sh_pmu
->raw_event_mask
;
158 case PERF_TYPE_HW_CACHE
:
159 err
= hw_perf_cache_event(attr
->config
, &config
);
163 case PERF_TYPE_HARDWARE
:
164 if (attr
->config
>= sh_pmu
->max_events
)
167 config
= sh_pmu
->event_map(attr
->config
);
174 hwc
->config
|= config
;
179 static void sh_perf_event_update(struct perf_event
*event
,
180 struct hw_perf_event
*hwc
, int idx
)
182 u64 prev_raw_count
, new_raw_count
;
187 * Depending on the counter configuration, they may or may not
188 * be chained, in which case the previous counter value can be
189 * updated underneath us if the lower-half overflows.
191 * Our tactic to handle this is to first atomically read and
192 * exchange a new raw count - then add that new-prev delta
193 * count to the generic counter atomically.
195 * As there is no interrupt associated with the overflow events,
196 * this is the simplest approach for maintaining consistency.
199 prev_raw_count
= local64_read(&hwc
->prev_count
);
200 new_raw_count
= sh_pmu
->read(idx
);
202 if (local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
203 new_raw_count
) != prev_raw_count
)
207 * Now we have the new raw value and have updated the prev
208 * timestamp already. We can now calculate the elapsed delta
209 * (counter-)time and add that to the generic counter.
211 * Careful, not all hw sign-extends above the physical width
214 delta
= (new_raw_count
<< shift
) - (prev_raw_count
<< shift
);
217 local64_add(delta
, &event
->count
);
220 static void sh_pmu_stop(struct perf_event
*event
, int flags
)
222 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
223 struct hw_perf_event
*hwc
= &event
->hw
;
226 if (!(event
->hw
.state
& PERF_HES_STOPPED
)) {
227 sh_pmu
->disable(hwc
, idx
);
228 cpuc
->events
[idx
] = NULL
;
229 event
->hw
.state
|= PERF_HES_STOPPED
;
232 if ((flags
& PERF_EF_UPDATE
) && !(event
->hw
.state
& PERF_HES_UPTODATE
)) {
233 sh_perf_event_update(event
, &event
->hw
, idx
);
234 event
->hw
.state
|= PERF_HES_UPTODATE
;
238 static void sh_pmu_start(struct perf_event
*event
, int flags
)
240 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
241 struct hw_perf_event
*hwc
= &event
->hw
;
244 if (WARN_ON_ONCE(idx
== -1))
247 if (flags
& PERF_EF_RELOAD
)
248 WARN_ON_ONCE(!(event
->hw
.state
& PERF_HES_UPTODATE
));
250 cpuc
->events
[idx
] = event
;
252 sh_pmu
->enable(hwc
, idx
);
255 static void sh_pmu_del(struct perf_event
*event
, int flags
)
257 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
259 sh_pmu_stop(event
, PERF_EF_UPDATE
);
260 __clear_bit(event
->hw
.idx
, cpuc
->used_mask
);
262 perf_event_update_userpage(event
);
265 static int sh_pmu_add(struct perf_event
*event
, int flags
)
267 struct cpu_hw_events
*cpuc
= this_cpu_ptr(&cpu_hw_events
);
268 struct hw_perf_event
*hwc
= &event
->hw
;
272 perf_pmu_disable(event
->pmu
);
274 if (__test_and_set_bit(idx
, cpuc
->used_mask
)) {
275 idx
= find_first_zero_bit(cpuc
->used_mask
, sh_pmu
->num_events
);
276 if (idx
== sh_pmu
->num_events
)
279 __set_bit(idx
, cpuc
->used_mask
);
283 sh_pmu
->disable(hwc
, idx
);
285 event
->hw
.state
= PERF_HES_UPTODATE
| PERF_HES_STOPPED
;
286 if (flags
& PERF_EF_START
)
287 sh_pmu_start(event
, PERF_EF_RELOAD
);
289 perf_event_update_userpage(event
);
292 perf_pmu_enable(event
->pmu
);
296 static void sh_pmu_read(struct perf_event
*event
)
298 sh_perf_event_update(event
, &event
->hw
, event
->hw
.idx
);
301 static int sh_pmu_event_init(struct perf_event
*event
)
305 /* does not support taken branch sampling */
306 if (has_branch_stack(event
))
309 switch (event
->attr
.type
) {
311 case PERF_TYPE_HW_CACHE
:
312 case PERF_TYPE_HARDWARE
:
313 err
= __hw_perf_event_init(event
);
322 event
->destroy(event
);
328 static void sh_pmu_enable(struct pmu
*pmu
)
330 if (!sh_pmu_initialized())
333 sh_pmu
->enable_all();
336 static void sh_pmu_disable(struct pmu
*pmu
)
338 if (!sh_pmu_initialized())
341 sh_pmu
->disable_all();
344 static struct pmu pmu
= {
345 .pmu_enable
= sh_pmu_enable
,
346 .pmu_disable
= sh_pmu_disable
,
347 .event_init
= sh_pmu_event_init
,
350 .start
= sh_pmu_start
,
355 static int sh_pmu_prepare_cpu(unsigned int cpu
)
357 struct cpu_hw_events
*cpuhw
= &per_cpu(cpu_hw_events
, cpu
);
359 memset(cpuhw
, 0, sizeof(struct cpu_hw_events
));
363 int register_sh_pmu(struct sh_pmu
*_pmu
)
369 pr_info("Performance Events: %s support registered\n", _pmu
->name
);
372 * All of the on-chip counters are "limited", in that they have
373 * no interrupts, and are therefore unable to do sampling without
374 * further work and timer assistance.
376 pmu
.capabilities
|= PERF_PMU_CAP_NO_INTERRUPT
;
378 WARN_ON(_pmu
->num_events
> MAX_HWEVENTS
);
380 perf_pmu_register(&pmu
, "cpu", PERF_TYPE_RAW
);
381 cpuhp_setup_state(CPUHP_PERF_SUPERH
, "PERF_SUPERH", sh_pmu_prepare_cpu
,