blob: e9fe904b653804e33ec15a0a71f2510a5431f62e [file] [log] [blame]
Scott Wooda1110652010-02-25 18:09:45 -06001/*
2 * Performance event support - Freescale Embedded Performance Monitor
3 *
4 * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
5 * Copyright 2010 Freescale Semiconductor, Inc.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12#include <linux/kernel.h>
13#include <linux/sched.h>
14#include <linux/perf_event.h>
15#include <linux/percpu.h>
16#include <linux/hardirq.h>
17#include <asm/reg_fsl_emb.h>
18#include <asm/pmc.h>
19#include <asm/machdep.h>
20#include <asm/firmware.h>
21#include <asm/ptrace.h>
22
23struct cpu_hw_events {
24 int n_events;
25 int disabled;
26 u8 pmcs_enabled;
27 struct perf_event *event[MAX_HWEVENTS];
28};
29static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
30
31static struct fsl_emb_pmu *ppmu;
32
33/* Number of perf_events counting hardware events */
34static atomic_t num_events;
35/* Used to avoid races in calling reserve/release_pmc_hardware */
36static DEFINE_MUTEX(pmc_reserve_mutex);
37
38/*
39 * If interrupts were soft-disabled when a PMU interrupt occurs, treat
40 * it as an NMI.
41 */
42static inline int perf_intr_is_nmi(struct pt_regs *regs)
43{
44#ifdef __powerpc64__
45 return !regs->softe;
46#else
47 return 0;
48#endif
49}
50
51static void perf_event_interrupt(struct pt_regs *regs);
52
53/*
54 * Read one performance monitor counter (PMC).
55 */
56static unsigned long read_pmc(int idx)
57{
58 unsigned long val;
59
60 switch (idx) {
61 case 0:
62 val = mfpmr(PMRN_PMC0);
63 break;
64 case 1:
65 val = mfpmr(PMRN_PMC1);
66 break;
67 case 2:
68 val = mfpmr(PMRN_PMC2);
69 break;
70 case 3:
71 val = mfpmr(PMRN_PMC3);
72 break;
Lijun Pan5815c432013-06-05 15:22:09 -050073 case 4:
74 val = mfpmr(PMRN_PMC4);
75 break;
76 case 5:
77 val = mfpmr(PMRN_PMC5);
78 break;
Scott Wooda1110652010-02-25 18:09:45 -060079 default:
80 printk(KERN_ERR "oops trying to read PMC%d\n", idx);
81 val = 0;
82 }
83 return val;
84}
85
86/*
87 * Write one PMC.
88 */
89static void write_pmc(int idx, unsigned long val)
90{
91 switch (idx) {
92 case 0:
93 mtpmr(PMRN_PMC0, val);
94 break;
95 case 1:
96 mtpmr(PMRN_PMC1, val);
97 break;
98 case 2:
99 mtpmr(PMRN_PMC2, val);
100 break;
101 case 3:
102 mtpmr(PMRN_PMC3, val);
103 break;
Lijun Pan5815c432013-06-05 15:22:09 -0500104 case 4:
105 mtpmr(PMRN_PMC4, val);
106 break;
107 case 5:
108 mtpmr(PMRN_PMC5, val);
109 break;
Scott Wooda1110652010-02-25 18:09:45 -0600110 default:
111 printk(KERN_ERR "oops trying to write PMC%d\n", idx);
112 }
113
114 isync();
115}
116
117/*
118 * Write one local control A register
119 */
120static void write_pmlca(int idx, unsigned long val)
121{
122 switch (idx) {
123 case 0:
124 mtpmr(PMRN_PMLCA0, val);
125 break;
126 case 1:
127 mtpmr(PMRN_PMLCA1, val);
128 break;
129 case 2:
130 mtpmr(PMRN_PMLCA2, val);
131 break;
132 case 3:
133 mtpmr(PMRN_PMLCA3, val);
134 break;
Lijun Pan5815c432013-06-05 15:22:09 -0500135 case 4:
136 mtpmr(PMRN_PMLCA4, val);
137 break;
138 case 5:
139 mtpmr(PMRN_PMLCA5, val);
140 break;
Scott Wooda1110652010-02-25 18:09:45 -0600141 default:
142 printk(KERN_ERR "oops trying to write PMLCA%d\n", idx);
143 }
144
145 isync();
146}
147
148/*
149 * Write one local control B register
150 */
151static void write_pmlcb(int idx, unsigned long val)
152{
153 switch (idx) {
154 case 0:
155 mtpmr(PMRN_PMLCB0, val);
156 break;
157 case 1:
158 mtpmr(PMRN_PMLCB1, val);
159 break;
160 case 2:
161 mtpmr(PMRN_PMLCB2, val);
162 break;
163 case 3:
164 mtpmr(PMRN_PMLCB3, val);
165 break;
Lijun Pan5815c432013-06-05 15:22:09 -0500166 case 4:
167 mtpmr(PMRN_PMLCB4, val);
168 break;
169 case 5:
170 mtpmr(PMRN_PMLCB5, val);
171 break;
Scott Wooda1110652010-02-25 18:09:45 -0600172 default:
173 printk(KERN_ERR "oops trying to write PMLCB%d\n", idx);
174 }
175
176 isync();
177}
178
179static void fsl_emb_pmu_read(struct perf_event *event)
180{
181 s64 val, delta, prev;
182
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200183 if (event->hw.state & PERF_HES_STOPPED)
184 return;
185
Scott Wooda1110652010-02-25 18:09:45 -0600186 /*
187 * Performance monitor interrupts come even when interrupts
188 * are soft-disabled, as long as interrupts are hard-enabled.
189 * Therefore we treat them like NMIs.
190 */
191 do {
Peter Zijlstra09f86cd2010-07-09 10:21:22 +0200192 prev = local64_read(&event->hw.prev_count);
Scott Wooda1110652010-02-25 18:09:45 -0600193 barrier();
194 val = read_pmc(event->hw.idx);
Peter Zijlstra09f86cd2010-07-09 10:21:22 +0200195 } while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
Scott Wooda1110652010-02-25 18:09:45 -0600196
197 /* The counters are only 32 bits wide */
198 delta = (val - prev) & 0xfffffffful;
Peter Zijlstra09f86cd2010-07-09 10:21:22 +0200199 local64_add(delta, &event->count);
200 local64_sub(delta, &event->hw.period_left);
Scott Wooda1110652010-02-25 18:09:45 -0600201}
202
203/*
204 * Disable all events to prevent PMU interrupts and to allow
205 * events to be added or removed.
206 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200207static void fsl_emb_pmu_disable(struct pmu *pmu)
Scott Wooda1110652010-02-25 18:09:45 -0600208{
209 struct cpu_hw_events *cpuhw;
210 unsigned long flags;
211
212 local_irq_save(flags);
Christoph Lameter69111ba2014-10-21 15:23:25 -0500213 cpuhw = this_cpu_ptr(&cpu_hw_events);
Scott Wooda1110652010-02-25 18:09:45 -0600214
215 if (!cpuhw->disabled) {
216 cpuhw->disabled = 1;
217
218 /*
219 * Check if we ever enabled the PMU on this cpu.
220 */
221 if (!cpuhw->pmcs_enabled) {
222 ppc_enable_pmcs();
223 cpuhw->pmcs_enabled = 1;
224 }
225
226 if (atomic_read(&num_events)) {
227 /*
228 * Set the 'freeze all counters' bit, and disable
229 * interrupts. The barrier is to make sure the
230 * mtpmr has been executed and the PMU has frozen
231 * the events before we return.
232 */
233
234 mtpmr(PMRN_PMGC0, PMGC0_FAC);
235 isync();
236 }
237 }
238 local_irq_restore(flags);
239}
240
241/*
242 * Re-enable all events if disable == 0.
243 * If we were previously disabled and events were added, then
244 * put the new config on the PMU.
245 */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200246static void fsl_emb_pmu_enable(struct pmu *pmu)
Scott Wooda1110652010-02-25 18:09:45 -0600247{
248 struct cpu_hw_events *cpuhw;
249 unsigned long flags;
250
251 local_irq_save(flags);
Christoph Lameter69111ba2014-10-21 15:23:25 -0500252 cpuhw = this_cpu_ptr(&cpu_hw_events);
Scott Wooda1110652010-02-25 18:09:45 -0600253 if (!cpuhw->disabled)
254 goto out;
255
256 cpuhw->disabled = 0;
257 ppc_set_pmu_inuse(cpuhw->n_events != 0);
258
259 if (cpuhw->n_events > 0) {
260 mtpmr(PMRN_PMGC0, PMGC0_PMIE | PMGC0_FCECE);
261 isync();
262 }
263
264 out:
265 local_irq_restore(flags);
266}
267
268static int collect_events(struct perf_event *group, int max_count,
269 struct perf_event *ctrs[])
270{
271 int n = 0;
272 struct perf_event *event;
273
274 if (!is_software_event(group)) {
275 if (n >= max_count)
276 return -1;
277 ctrs[n] = group;
278 n++;
279 }
280 list_for_each_entry(event, &group->sibling_list, group_entry) {
281 if (!is_software_event(event) &&
282 event->state != PERF_EVENT_STATE_OFF) {
283 if (n >= max_count)
284 return -1;
285 ctrs[n] = event;
286 n++;
287 }
288 }
289 return n;
290}
291
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200292/* context locked on entry */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200293static int fsl_emb_pmu_add(struct perf_event *event, int flags)
Scott Wooda1110652010-02-25 18:09:45 -0600294{
295 struct cpu_hw_events *cpuhw;
296 int ret = -EAGAIN;
297 int num_counters = ppmu->n_counter;
298 u64 val;
299 int i;
300
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200301 perf_pmu_disable(event->pmu);
Scott Wooda1110652010-02-25 18:09:45 -0600302 cpuhw = &get_cpu_var(cpu_hw_events);
303
304 if (event->hw.config & FSL_EMB_EVENT_RESTRICTED)
305 num_counters = ppmu->n_restricted;
306
307 /*
308 * Allocate counters from top-down, so that restricted-capable
309 * counters are kept free as long as possible.
310 */
311 for (i = num_counters - 1; i >= 0; i--) {
312 if (cpuhw->event[i])
313 continue;
314
315 break;
316 }
317
318 if (i < 0)
319 goto out;
320
321 event->hw.idx = i;
322 cpuhw->event[i] = event;
323 ++cpuhw->n_events;
324
325 val = 0;
326 if (event->hw.sample_period) {
Peter Zijlstra09f86cd2010-07-09 10:21:22 +0200327 s64 left = local64_read(&event->hw.period_left);
Scott Wooda1110652010-02-25 18:09:45 -0600328 if (left < 0x80000000L)
329 val = 0x80000000L - left;
330 }
Peter Zijlstra09f86cd2010-07-09 10:21:22 +0200331 local64_set(&event->hw.prev_count, val);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200332
333 if (!(flags & PERF_EF_START)) {
334 event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
335 val = 0;
336 }
337
Scott Wooda1110652010-02-25 18:09:45 -0600338 write_pmc(i, val);
339 perf_event_update_userpage(event);
340
341 write_pmlcb(i, event->hw.config >> 32);
342 write_pmlca(i, event->hw.config_base);
343
344 ret = 0;
345 out:
346 put_cpu_var(cpu_hw_events);
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200347 perf_pmu_enable(event->pmu);
Scott Wooda1110652010-02-25 18:09:45 -0600348 return ret;
349}
350
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200351/* context locked on entry */
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200352static void fsl_emb_pmu_del(struct perf_event *event, int flags)
Scott Wooda1110652010-02-25 18:09:45 -0600353{
354 struct cpu_hw_events *cpuhw;
355 int i = event->hw.idx;
356
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200357 perf_pmu_disable(event->pmu);
Scott Wooda1110652010-02-25 18:09:45 -0600358 if (i < 0)
359 goto out;
360
361 fsl_emb_pmu_read(event);
362
363 cpuhw = &get_cpu_var(cpu_hw_events);
364
365 WARN_ON(event != cpuhw->event[event->hw.idx]);
366
367 write_pmlca(i, 0);
368 write_pmlcb(i, 0);
369 write_pmc(i, 0);
370
371 cpuhw->event[i] = NULL;
372 event->hw.idx = -1;
373
374 /*
375 * TODO: if at least one restricted event exists, and we
376 * just freed up a non-restricted-capable counter, and
377 * there is a restricted-capable counter occupied by
378 * a non-restricted event, migrate that event to the
379 * vacated counter.
380 */
381
382 cpuhw->n_events--;
383
384 out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200385 perf_pmu_enable(event->pmu);
Scott Wooda1110652010-02-25 18:09:45 -0600386 put_cpu_var(cpu_hw_events);
387}
388
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200389static void fsl_emb_pmu_start(struct perf_event *event, int ef_flags)
Scott Wooda1110652010-02-25 18:09:45 -0600390{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200391 unsigned long flags;
Tom Huynhd2caa3ce2015-01-20 16:19:50 -0600392 unsigned long val;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200393 s64 left;
394
395 if (event->hw.idx < 0 || !event->hw.sample_period)
396 return;
397
398 if (!(event->hw.state & PERF_HES_STOPPED))
399 return;
400
401 if (ef_flags & PERF_EF_RELOAD)
402 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
403
404 local_irq_save(flags);
405 perf_pmu_disable(event->pmu);
406
407 event->hw.state = 0;
408 left = local64_read(&event->hw.period_left);
Tom Huynhd2caa3ce2015-01-20 16:19:50 -0600409 val = 0;
410 if (left < 0x80000000L)
411 val = 0x80000000L - left;
412 write_pmc(event->hw.idx, val);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200413
414 perf_event_update_userpage(event);
415 perf_pmu_enable(event->pmu);
416 local_irq_restore(flags);
417}
418
419static void fsl_emb_pmu_stop(struct perf_event *event, int ef_flags)
420{
Scott Wooda1110652010-02-25 18:09:45 -0600421 unsigned long flags;
422
423 if (event->hw.idx < 0 || !event->hw.sample_period)
424 return;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200425
426 if (event->hw.state & PERF_HES_STOPPED)
427 return;
428
Scott Wooda1110652010-02-25 18:09:45 -0600429 local_irq_save(flags);
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200430 perf_pmu_disable(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200431
Scott Wooda1110652010-02-25 18:09:45 -0600432 fsl_emb_pmu_read(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200433 event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
434 write_pmc(event->hw.idx, 0);
435
Scott Wooda1110652010-02-25 18:09:45 -0600436 perf_event_update_userpage(event);
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200437 perf_pmu_enable(event->pmu);
Scott Wooda1110652010-02-25 18:09:45 -0600438 local_irq_restore(flags);
439}
440
Scott Wooda1110652010-02-25 18:09:45 -0600441/*
442 * Release the PMU if this is the last perf_event.
443 */
444static void hw_perf_event_destroy(struct perf_event *event)
445{
446 if (!atomic_add_unless(&num_events, -1, 1)) {
447 mutex_lock(&pmc_reserve_mutex);
448 if (atomic_dec_return(&num_events) == 0)
449 release_pmc_hardware();
450 mutex_unlock(&pmc_reserve_mutex);
451 }
452}
453
454/*
455 * Translate a generic cache event_id config to a raw event_id code.
456 */
457static int hw_perf_cache_event(u64 config, u64 *eventp)
458{
459 unsigned long type, op, result;
460 int ev;
461
462 if (!ppmu->cache_events)
463 return -EINVAL;
464
465 /* unpack config */
466 type = config & 0xff;
467 op = (config >> 8) & 0xff;
468 result = (config >> 16) & 0xff;
469
470 if (type >= PERF_COUNT_HW_CACHE_MAX ||
471 op >= PERF_COUNT_HW_CACHE_OP_MAX ||
472 result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
473 return -EINVAL;
474
475 ev = (*ppmu->cache_events)[type][op][result];
476 if (ev == 0)
477 return -EOPNOTSUPP;
478 if (ev == -1)
479 return -EINVAL;
480 *eventp = ev;
481 return 0;
482}
483
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200484static int fsl_emb_pmu_event_init(struct perf_event *event)
Scott Wooda1110652010-02-25 18:09:45 -0600485{
486 u64 ev;
487 struct perf_event *events[MAX_HWEVENTS];
488 int n;
489 int err;
490 int num_restricted;
491 int i;
492
Catalin Udma96c3c9e2013-06-05 15:22:08 -0500493 if (ppmu->n_counter > MAX_HWEVENTS) {
494 WARN(1, "No. of perf counters (%d) is higher than max array size(%d)\n",
495 ppmu->n_counter, MAX_HWEVENTS);
496 ppmu->n_counter = MAX_HWEVENTS;
497 }
498
Scott Wooda1110652010-02-25 18:09:45 -0600499 switch (event->attr.type) {
500 case PERF_TYPE_HARDWARE:
501 ev = event->attr.config;
502 if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200503 return -EOPNOTSUPP;
Scott Wooda1110652010-02-25 18:09:45 -0600504 ev = ppmu->generic_events[ev];
505 break;
506
507 case PERF_TYPE_HW_CACHE:
508 err = hw_perf_cache_event(event->attr.config, &ev);
509 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200510 return err;
Scott Wooda1110652010-02-25 18:09:45 -0600511 break;
512
513 case PERF_TYPE_RAW:
514 ev = event->attr.config;
515 break;
516
517 default:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200518 return -ENOENT;
Scott Wooda1110652010-02-25 18:09:45 -0600519 }
520
521 event->hw.config = ppmu->xlate_event(ev);
522 if (!(event->hw.config & FSL_EMB_EVENT_VALID))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200523 return -EINVAL;
Scott Wooda1110652010-02-25 18:09:45 -0600524
525 /*
526 * If this is in a group, check if it can go on with all the
527 * other hardware events in the group. We assume the event
528 * hasn't been linked into its leader's sibling list at this point.
529 */
530 n = 0;
531 if (event->group_leader != event) {
532 n = collect_events(event->group_leader,
533 ppmu->n_counter - 1, events);
534 if (n < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200535 return -EINVAL;
Scott Wooda1110652010-02-25 18:09:45 -0600536 }
537
538 if (event->hw.config & FSL_EMB_EVENT_RESTRICTED) {
539 num_restricted = 0;
540 for (i = 0; i < n; i++) {
541 if (events[i]->hw.config & FSL_EMB_EVENT_RESTRICTED)
542 num_restricted++;
543 }
544
545 if (num_restricted >= ppmu->n_restricted)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200546 return -EINVAL;
Scott Wooda1110652010-02-25 18:09:45 -0600547 }
548
549 event->hw.idx = -1;
550
551 event->hw.config_base = PMLCA_CE | PMLCA_FCM1 |
552 (u32)((ev << 16) & PMLCA_EVENT_MASK);
553
554 if (event->attr.exclude_user)
555 event->hw.config_base |= PMLCA_FCU;
556 if (event->attr.exclude_kernel)
557 event->hw.config_base |= PMLCA_FCS;
558 if (event->attr.exclude_idle)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200559 return -ENOTSUPP;
Scott Wooda1110652010-02-25 18:09:45 -0600560
561 event->hw.last_period = event->hw.sample_period;
Peter Zijlstra09f86cd2010-07-09 10:21:22 +0200562 local64_set(&event->hw.period_left, event->hw.last_period);
Scott Wooda1110652010-02-25 18:09:45 -0600563
564 /*
565 * See if we need to reserve the PMU.
566 * If no events are currently in use, then we have to take a
567 * mutex to ensure that we don't race with another task doing
568 * reserve_pmc_hardware or release_pmc_hardware.
569 */
570 err = 0;
571 if (!atomic_inc_not_zero(&num_events)) {
572 mutex_lock(&pmc_reserve_mutex);
573 if (atomic_read(&num_events) == 0 &&
574 reserve_pmc_hardware(perf_event_interrupt))
575 err = -EBUSY;
576 else
577 atomic_inc(&num_events);
578 mutex_unlock(&pmc_reserve_mutex);
579
580 mtpmr(PMRN_PMGC0, PMGC0_FAC);
581 isync();
582 }
583 event->destroy = hw_perf_event_destroy;
584
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200585 return err;
Scott Wooda1110652010-02-25 18:09:45 -0600586}
587
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200588static struct pmu fsl_emb_pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200589 .pmu_enable = fsl_emb_pmu_enable,
590 .pmu_disable = fsl_emb_pmu_disable,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200591 .event_init = fsl_emb_pmu_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200592 .add = fsl_emb_pmu_add,
593 .del = fsl_emb_pmu_del,
594 .start = fsl_emb_pmu_start,
595 .stop = fsl_emb_pmu_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200596 .read = fsl_emb_pmu_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200597};
598
Scott Wooda1110652010-02-25 18:09:45 -0600599/*
600 * A counter has overflowed; update its count and record
601 * things if requested. Note that interrupts are hard-disabled
602 * here so there is no possibility of being interrupted.
603 */
604static void record_and_restart(struct perf_event *event, unsigned long val,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200605 struct pt_regs *regs)
Scott Wooda1110652010-02-25 18:09:45 -0600606{
607 u64 period = event->hw.sample_period;
608 s64 prev, delta, left;
609 int record = 0;
610
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200611 if (event->hw.state & PERF_HES_STOPPED) {
612 write_pmc(event->hw.idx, 0);
613 return;
614 }
615
Scott Wooda1110652010-02-25 18:09:45 -0600616 /* we don't have to worry about interrupts here */
Peter Zijlstra09f86cd2010-07-09 10:21:22 +0200617 prev = local64_read(&event->hw.prev_count);
Scott Wooda1110652010-02-25 18:09:45 -0600618 delta = (val - prev) & 0xfffffffful;
Peter Zijlstra09f86cd2010-07-09 10:21:22 +0200619 local64_add(delta, &event->count);
Scott Wooda1110652010-02-25 18:09:45 -0600620
621 /*
622 * See if the total period for this event has expired,
623 * and update for the next period.
624 */
625 val = 0;
Peter Zijlstra09f86cd2010-07-09 10:21:22 +0200626 left = local64_read(&event->hw.period_left) - delta;
Scott Wooda1110652010-02-25 18:09:45 -0600627 if (period) {
628 if (left <= 0) {
629 left += period;
630 if (left <= 0)
631 left = period;
632 record = 1;
Anton Blanchard8c8a9b22011-01-18 21:44:04 +1100633 event->hw.last_period = event->hw.sample_period;
Scott Wooda1110652010-02-25 18:09:45 -0600634 }
635 if (left < 0x80000000LL)
636 val = 0x80000000LL - left;
637 }
638
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200639 write_pmc(event->hw.idx, val);
640 local64_set(&event->hw.prev_count, val);
641 local64_set(&event->hw.period_left, left);
642 perf_event_update_userpage(event);
643
Scott Wooda1110652010-02-25 18:09:45 -0600644 /*
645 * Finally record data if requested.
646 */
647 if (record) {
Peter Zijlstra6b95ed32010-07-09 10:21:21 +0200648 struct perf_sample_data data;
649
Robert Richterfd0d0002012-04-02 20:19:08 +0200650 perf_sample_data_init(&data, 0, event->hw.last_period);
Scott Wooda1110652010-02-25 18:09:45 -0600651
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200652 if (perf_event_overflow(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200653 fsl_emb_pmu_stop(event, 0);
Scott Wooda1110652010-02-25 18:09:45 -0600654 }
Scott Wooda1110652010-02-25 18:09:45 -0600655}
656
657static void perf_event_interrupt(struct pt_regs *regs)
658{
659 int i;
Christoph Lameter69111ba2014-10-21 15:23:25 -0500660 struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
Scott Wooda1110652010-02-25 18:09:45 -0600661 struct perf_event *event;
662 unsigned long val;
663 int found = 0;
664 int nmi;
665
666 nmi = perf_intr_is_nmi(regs);
667 if (nmi)
668 nmi_enter();
669 else
670 irq_enter();
671
672 for (i = 0; i < ppmu->n_counter; ++i) {
673 event = cpuhw->event[i];
674
675 val = read_pmc(i);
676 if ((int)val < 0) {
677 if (event) {
678 /* event has overflowed */
679 found = 1;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200680 record_and_restart(event, val, regs);
Scott Wooda1110652010-02-25 18:09:45 -0600681 } else {
682 /*
683 * Disabled counter is negative,
684 * reset it just in case.
685 */
686 write_pmc(i, 0);
687 }
688 }
689 }
690
691 /* PMM will keep counters frozen until we return from the interrupt. */
692 mtmsr(mfmsr() | MSR_PMM);
693 mtpmr(PMRN_PMGC0, PMGC0_PMIE | PMGC0_FCECE);
694 isync();
695
696 if (nmi)
697 nmi_exit();
698 else
699 irq_exit();
700}
701
702void hw_perf_event_setup(int cpu)
703{
704 struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
705
706 memset(cpuhw, 0, sizeof(*cpuhw));
707}
708
709int register_fsl_emb_pmu(struct fsl_emb_pmu *pmu)
710{
711 if (ppmu)
712 return -EBUSY; /* something's already registered */
713
714 ppmu = pmu;
715 pr_info("%s performance monitor hardware support registered\n",
716 pmu->name);
717
Peter Zijlstra2e80a822010-11-17 23:17:36 +0100718 perf_pmu_register(&fsl_emb_pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200719
Scott Wooda1110652010-02-25 18:09:45 -0600720 return 0;
721}