1 /*
2  * Performance event support - powerpc architecture code
3  *
4  * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/perf_event.h>
14 #include <linux/percpu.h>
15 #include <linux/hardirq.h>
16 #include <asm/reg.h>
17 #include <asm/pmc.h>
18 #include <asm/machdep.h>
19 #include <asm/firmware.h>
20 #include <asm/ptrace.h>
21 
22 struct cpu_hw_events {
23 	int n_events;
24 	int n_percpu;
25 	int disabled;
26 	int n_added;
27 	int n_limited;
28 	u8  pmcs_enabled;
29 	struct perf_event *event[MAX_HWEVENTS];
30 	u64 events[MAX_HWEVENTS];
31 	unsigned int flags[MAX_HWEVENTS];
32 	unsigned long mmcr[3];
33 	struct perf_event *limited_counter[MAX_LIMITED_HWCOUNTERS];
34 	u8  limited_hwidx[MAX_LIMITED_HWCOUNTERS];
35 	u64 alternatives[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
36 	unsigned long amasks[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
37 	unsigned long avalues[MAX_HWEVENTS][MAX_EVENT_ALTERNATIVES];
38 
39 	unsigned int group_flag;
40 	int n_txn_start;
41 };
42 DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
43 
44 struct power_pmu *ppmu;
45 
46 /*
47  * Normally, to ignore kernel events we set the FCS (freeze counters
48  * in supervisor mode) bit in MMCR0, but if the kernel runs with the
49  * hypervisor bit set in the MSR, or if we are running on a processor
50  * where the hypervisor bit is forced to 1 (as on Apple G5 processors),
51  * then we need to use the FCHV bit to ignore kernel events.
52  */
53 static unsigned int freeze_events_kernel = MMCR0_FCS;
54 
55 /*
56  * 32-bit doesn't have MMCRA but does have an MMCR2,
57  * and a few other names are different.
58  */
59 #ifdef CONFIG_PPC32
60 
61 #define MMCR0_FCHV		0
62 #define MMCR0_PMCjCE		MMCR0_PMCnCE
63 
64 #define SPRN_MMCRA		SPRN_MMCR2
65 #define MMCRA_SAMPLE_ENABLE	0
66 
perf_ip_adjust(struct pt_regs * regs)67 static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
68 {
69 	return 0;
70 }
perf_get_data_addr(struct pt_regs * regs,u64 * addrp)71 static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
perf_get_misc_flags(struct pt_regs * regs)72 static inline u32 perf_get_misc_flags(struct pt_regs *regs)
73 {
74 	return 0;
75 }
perf_read_regs(struct pt_regs * regs)76 static inline void perf_read_regs(struct pt_regs *regs) { }
perf_intr_is_nmi(struct pt_regs * regs)77 static inline int perf_intr_is_nmi(struct pt_regs *regs)
78 {
79 	return 0;
80 }
81 
82 #endif /* CONFIG_PPC32 */
83 
84 /*
85  * Things that are specific to 64-bit implementations.
86  */
87 #ifdef CONFIG_PPC64
88 
perf_ip_adjust(struct pt_regs * regs)89 static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
90 {
91 	unsigned long mmcra = regs->dsisr;
92 
93 	if ((mmcra & MMCRA_SAMPLE_ENABLE) && !(ppmu->flags & PPMU_ALT_SIPR)) {
94 		unsigned long slot = (mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT;
95 		if (slot > 1)
96 			return 4 * (slot - 1);
97 	}
98 	return 0;
99 }
100 
101 /*
102  * The user wants a data address recorded.
103  * If we're not doing instruction sampling, give them the SDAR
104  * (sampled data address).  If we are doing instruction sampling, then
105  * only give them the SDAR if it corresponds to the instruction
106  * pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC
107  * bit in MMCRA.
108  */
perf_get_data_addr(struct pt_regs * regs,u64 * addrp)109 static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
110 {
111 	unsigned long mmcra = regs->dsisr;
112 	unsigned long sdsync = (ppmu->flags & PPMU_ALT_SIPR) ?
113 		POWER6_MMCRA_SDSYNC : MMCRA_SDSYNC;
114 
115 	if (!(mmcra & MMCRA_SAMPLE_ENABLE) || (mmcra & sdsync))
116 		*addrp = mfspr(SPRN_SDAR);
117 }
118 
perf_flags_from_msr(struct pt_regs * regs)119 static inline u32 perf_flags_from_msr(struct pt_regs *regs)
120 {
121 	if (regs->msr & MSR_PR)
122 		return PERF_RECORD_MISC_USER;
123 	if ((regs->msr & MSR_HV) && freeze_events_kernel != MMCR0_FCHV)
124 		return PERF_RECORD_MISC_HYPERVISOR;
125 	return PERF_RECORD_MISC_KERNEL;
126 }
127 
perf_get_misc_flags(struct pt_regs * regs)128 static inline u32 perf_get_misc_flags(struct pt_regs *regs)
129 {
130 	unsigned long mmcra = regs->dsisr;
131 	unsigned long sihv = MMCRA_SIHV;
132 	unsigned long sipr = MMCRA_SIPR;
133 
134 	/* Not a PMU interrupt: Make up flags from regs->msr */
135 	if (TRAP(regs) != 0xf00)
136 		return perf_flags_from_msr(regs);
137 
138 	/*
139 	 * If we don't support continuous sampling and this
140 	 * is not a marked event, same deal
141 	 */
142 	if ((ppmu->flags & PPMU_NO_CONT_SAMPLING) &&
143 	    !(mmcra & MMCRA_SAMPLE_ENABLE))
144 		return perf_flags_from_msr(regs);
145 
146 	/*
147 	 * If we don't have flags in MMCRA, rather than using
148 	 * the MSR, we intuit the flags from the address in
149 	 * SIAR which should give slightly more reliable
150 	 * results
151 	 */
152 	if (ppmu->flags & PPMU_NO_SIPR) {
153 		unsigned long siar = mfspr(SPRN_SIAR);
154 		if (siar >= PAGE_OFFSET)
155 			return PERF_RECORD_MISC_KERNEL;
156 		return PERF_RECORD_MISC_USER;
157 	}
158 
159 	if (ppmu->flags & PPMU_ALT_SIPR) {
160 		sihv = POWER6_MMCRA_SIHV;
161 		sipr = POWER6_MMCRA_SIPR;
162 	}
163 
164 	/* PR has priority over HV, so order below is important */
165 	if (mmcra & sipr)
166 		return PERF_RECORD_MISC_USER;
167 	if ((mmcra & sihv) && (freeze_events_kernel != MMCR0_FCHV))
168 		return PERF_RECORD_MISC_HYPERVISOR;
169 	return PERF_RECORD_MISC_KERNEL;
170 }
171 
172 /*
173  * Overload regs->dsisr to store MMCRA so we only need to read it once
174  * on each interrupt.
175  */
perf_read_regs(struct pt_regs * regs)176 static inline void perf_read_regs(struct pt_regs *regs)
177 {
178 	regs->dsisr = mfspr(SPRN_MMCRA);
179 }
180 
181 /*
182  * If interrupts were soft-disabled when a PMU interrupt occurs, treat
183  * it as an NMI.
184  */
perf_intr_is_nmi(struct pt_regs * regs)185 static inline int perf_intr_is_nmi(struct pt_regs *regs)
186 {
187 	return !regs->softe;
188 }
189 
190 #endif /* CONFIG_PPC64 */
191 
192 static void perf_event_interrupt(struct pt_regs *regs);
193 
perf_event_print_debug(void)194 void perf_event_print_debug(void)
195 {
196 }
197 
198 /*
199  * Read one performance monitor counter (PMC).
200  */
read_pmc(int idx)201 static unsigned long read_pmc(int idx)
202 {
203 	unsigned long val;
204 
205 	switch (idx) {
206 	case 1:
207 		val = mfspr(SPRN_PMC1);
208 		break;
209 	case 2:
210 		val = mfspr(SPRN_PMC2);
211 		break;
212 	case 3:
213 		val = mfspr(SPRN_PMC3);
214 		break;
215 	case 4:
216 		val = mfspr(SPRN_PMC4);
217 		break;
218 	case 5:
219 		val = mfspr(SPRN_PMC5);
220 		break;
221 	case 6:
222 		val = mfspr(SPRN_PMC6);
223 		break;
224 #ifdef CONFIG_PPC64
225 	case 7:
226 		val = mfspr(SPRN_PMC7);
227 		break;
228 	case 8:
229 		val = mfspr(SPRN_PMC8);
230 		break;
231 #endif /* CONFIG_PPC64 */
232 	default:
233 		printk(KERN_ERR "oops trying to read PMC%d\n", idx);
234 		val = 0;
235 	}
236 	return val;
237 }
238 
239 /*
240  * Write one PMC.
241  */
write_pmc(int idx,unsigned long val)242 static void write_pmc(int idx, unsigned long val)
243 {
244 	switch (idx) {
245 	case 1:
246 		mtspr(SPRN_PMC1, val);
247 		break;
248 	case 2:
249 		mtspr(SPRN_PMC2, val);
250 		break;
251 	case 3:
252 		mtspr(SPRN_PMC3, val);
253 		break;
254 	case 4:
255 		mtspr(SPRN_PMC4, val);
256 		break;
257 	case 5:
258 		mtspr(SPRN_PMC5, val);
259 		break;
260 	case 6:
261 		mtspr(SPRN_PMC6, val);
262 		break;
263 #ifdef CONFIG_PPC64
264 	case 7:
265 		mtspr(SPRN_PMC7, val);
266 		break;
267 	case 8:
268 		mtspr(SPRN_PMC8, val);
269 		break;
270 #endif /* CONFIG_PPC64 */
271 	default:
272 		printk(KERN_ERR "oops trying to write PMC%d\n", idx);
273 	}
274 }
275 
276 /*
277  * Check if a set of events can all go on the PMU at once.
278  * If they can't, this will look at alternative codes for the events
279  * and see if any combination of alternative codes is feasible.
280  * The feasible set is returned in event_id[].
281  */
power_check_constraints(struct cpu_hw_events * cpuhw,u64 event_id[],unsigned int cflags[],int n_ev)282 static int power_check_constraints(struct cpu_hw_events *cpuhw,
283 				   u64 event_id[], unsigned int cflags[],
284 				   int n_ev)
285 {
286 	unsigned long mask, value, nv;
287 	unsigned long smasks[MAX_HWEVENTS], svalues[MAX_HWEVENTS];
288 	int n_alt[MAX_HWEVENTS], choice[MAX_HWEVENTS];
289 	int i, j;
290 	unsigned long addf = ppmu->add_fields;
291 	unsigned long tadd = ppmu->test_adder;
292 
293 	if (n_ev > ppmu->n_counter)
294 		return -1;
295 
296 	/* First see if the events will go on as-is */
297 	for (i = 0; i < n_ev; ++i) {
298 		if ((cflags[i] & PPMU_LIMITED_PMC_REQD)
299 		    && !ppmu->limited_pmc_event(event_id[i])) {
300 			ppmu->get_alternatives(event_id[i], cflags[i],
301 					       cpuhw->alternatives[i]);
302 			event_id[i] = cpuhw->alternatives[i][0];
303 		}
304 		if (ppmu->get_constraint(event_id[i], &cpuhw->amasks[i][0],
305 					 &cpuhw->avalues[i][0]))
306 			return -1;
307 	}
308 	value = mask = 0;
309 	for (i = 0; i < n_ev; ++i) {
310 		nv = (value | cpuhw->avalues[i][0]) +
311 			(value & cpuhw->avalues[i][0] & addf);
312 		if ((((nv + tadd) ^ value) & mask) != 0 ||
313 		    (((nv + tadd) ^ cpuhw->avalues[i][0]) &
314 		     cpuhw->amasks[i][0]) != 0)
315 			break;
316 		value = nv;
317 		mask |= cpuhw->amasks[i][0];
318 	}
319 	if (i == n_ev)
320 		return 0;	/* all OK */
321 
322 	/* doesn't work, gather alternatives... */
323 	if (!ppmu->get_alternatives)
324 		return -1;
325 	for (i = 0; i < n_ev; ++i) {
326 		choice[i] = 0;
327 		n_alt[i] = ppmu->get_alternatives(event_id[i], cflags[i],
328 						  cpuhw->alternatives[i]);
329 		for (j = 1; j < n_alt[i]; ++j)
330 			ppmu->get_constraint(cpuhw->alternatives[i][j],
331 					     &cpuhw->amasks[i][j],
332 					     &cpuhw->avalues[i][j]);
333 	}
334 
335 	/* enumerate all possibilities and see if any will work */
336 	i = 0;
337 	j = -1;
338 	value = mask = nv = 0;
339 	while (i < n_ev) {
340 		if (j >= 0) {
341 			/* we're backtracking, restore context */
342 			value = svalues[i];
343 			mask = smasks[i];
344 			j = choice[i];
345 		}
346 		/*
347 		 * See if any alternative k for event_id i,
348 		 * where k > j, will satisfy the constraints.
349 		 */
350 		while (++j < n_alt[i]) {
351 			nv = (value | cpuhw->avalues[i][j]) +
352 				(value & cpuhw->avalues[i][j] & addf);
353 			if ((((nv + tadd) ^ value) & mask) == 0 &&
354 			    (((nv + tadd) ^ cpuhw->avalues[i][j])
355 			     & cpuhw->amasks[i][j]) == 0)
356 				break;
357 		}
358 		if (j >= n_alt[i]) {
359 			/*
360 			 * No feasible alternative, backtrack
361 			 * to event_id i-1 and continue enumerating its
362 			 * alternatives from where we got up to.
363 			 */
364 			if (--i < 0)
365 				return -1;
366 		} else {
367 			/*
368 			 * Found a feasible alternative for event_id i,
369 			 * remember where we got up to with this event_id,
370 			 * go on to the next event_id, and start with
371 			 * the first alternative for it.
372 			 */
373 			choice[i] = j;
374 			svalues[i] = value;
375 			smasks[i] = mask;
376 			value = nv;
377 			mask |= cpuhw->amasks[i][j];
378 			++i;
379 			j = -1;
380 		}
381 	}
382 
383 	/* OK, we have a feasible combination, tell the caller the solution */
384 	for (i = 0; i < n_ev; ++i)
385 		event_id[i] = cpuhw->alternatives[i][choice[i]];
386 	return 0;
387 }
388 
389 /*
390  * Check if newly-added events have consistent settings for
391  * exclude_{user,kernel,hv} with each other and any previously
392  * added events.
393  */
check_excludes(struct perf_event ** ctrs,unsigned int cflags[],int n_prev,int n_new)394 static int check_excludes(struct perf_event **ctrs, unsigned int cflags[],
395 			  int n_prev, int n_new)
396 {
397 	int eu = 0, ek = 0, eh = 0;
398 	int i, n, first;
399 	struct perf_event *event;
400 
401 	n = n_prev + n_new;
402 	if (n <= 1)
403 		return 0;
404 
405 	first = 1;
406 	for (i = 0; i < n; ++i) {
407 		if (cflags[i] & PPMU_LIMITED_PMC_OK) {
408 			cflags[i] &= ~PPMU_LIMITED_PMC_REQD;
409 			continue;
410 		}
411 		event = ctrs[i];
412 		if (first) {
413 			eu = event->attr.exclude_user;
414 			ek = event->attr.exclude_kernel;
415 			eh = event->attr.exclude_hv;
416 			first = 0;
417 		} else if (event->attr.exclude_user != eu ||
418 			   event->attr.exclude_kernel != ek ||
419 			   event->attr.exclude_hv != eh) {
420 			return -EAGAIN;
421 		}
422 	}
423 
424 	if (eu || ek || eh)
425 		for (i = 0; i < n; ++i)
426 			if (cflags[i] & PPMU_LIMITED_PMC_OK)
427 				cflags[i] |= PPMU_LIMITED_PMC_REQD;
428 
429 	return 0;
430 }
431 
check_and_compute_delta(u64 prev,u64 val)432 static u64 check_and_compute_delta(u64 prev, u64 val)
433 {
434 	u64 delta = (val - prev) & 0xfffffffful;
435 
436 	/*
437 	 * POWER7 can roll back counter values, if the new value is smaller
438 	 * than the previous value it will cause the delta and the counter to
439 	 * have bogus values unless we rolled a counter over.  If a coutner is
440 	 * rolled back, it will be smaller, but within 256, which is the maximum
441 	 * number of events to rollback at once.  If we dectect a rollback
442 	 * return 0.  This can lead to a small lack of precision in the
443 	 * counters.
444 	 */
445 	if (prev > val && (prev - val) < 256)
446 		delta = 0;
447 
448 	return delta;
449 }
450 
power_pmu_read(struct perf_event * event)451 static void power_pmu_read(struct perf_event *event)
452 {
453 	s64 val, delta, prev;
454 
455 	if (event->hw.state & PERF_HES_STOPPED)
456 		return;
457 
458 	if (!event->hw.idx)
459 		return;
460 	/*
461 	 * Performance monitor interrupts come even when interrupts
462 	 * are soft-disabled, as long as interrupts are hard-enabled.
463 	 * Therefore we treat them like NMIs.
464 	 */
465 	do {
466 		prev = local64_read(&event->hw.prev_count);
467 		barrier();
468 		val = read_pmc(event->hw.idx);
469 		delta = check_and_compute_delta(prev, val);
470 		if (!delta)
471 			return;
472 	} while (local64_cmpxchg(&event->hw.prev_count, prev, val) != prev);
473 
474 	local64_add(delta, &event->count);
475 
476 	/*
477 	 * A number of places program the PMC with (0x80000000 - period_left).
478 	 * We never want period_left to be less than 1 because we will program
479 	 * the PMC with a value >= 0x800000000 and an edge detected PMC will
480 	 * roll around to 0 before taking an exception. We have seen this
481 	 * on POWER8.
482 	 *
483 	 * To fix this, clamp the minimum value of period_left to 1.
484 	 */
485 	do {
486 		prev = local64_read(&event->hw.period_left);
487 		val = prev - delta;
488 		if (val < 1)
489 			val = 1;
490 	} while (local64_cmpxchg(&event->hw.period_left, prev, val) != prev);
491 }
492 
493 /*
494  * On some machines, PMC5 and PMC6 can't be written, don't respect
495  * the freeze conditions, and don't generate interrupts.  This tells
496  * us if `event' is using such a PMC.
497  */
is_limited_pmc(int pmcnum)498 static int is_limited_pmc(int pmcnum)
499 {
500 	return (ppmu->flags & PPMU_LIMITED_PMC5_6)
501 		&& (pmcnum == 5 || pmcnum == 6);
502 }
503 
freeze_limited_counters(struct cpu_hw_events * cpuhw,unsigned long pmc5,unsigned long pmc6)504 static void freeze_limited_counters(struct cpu_hw_events *cpuhw,
505 				    unsigned long pmc5, unsigned long pmc6)
506 {
507 	struct perf_event *event;
508 	u64 val, prev, delta;
509 	int i;
510 
511 	for (i = 0; i < cpuhw->n_limited; ++i) {
512 		event = cpuhw->limited_counter[i];
513 		if (!event->hw.idx)
514 			continue;
515 		val = (event->hw.idx == 5) ? pmc5 : pmc6;
516 		prev = local64_read(&event->hw.prev_count);
517 		event->hw.idx = 0;
518 		delta = check_and_compute_delta(prev, val);
519 		if (delta)
520 			local64_add(delta, &event->count);
521 	}
522 }
523 
thaw_limited_counters(struct cpu_hw_events * cpuhw,unsigned long pmc5,unsigned long pmc6)524 static void thaw_limited_counters(struct cpu_hw_events *cpuhw,
525 				  unsigned long pmc5, unsigned long pmc6)
526 {
527 	struct perf_event *event;
528 	u64 val, prev;
529 	int i;
530 
531 	for (i = 0; i < cpuhw->n_limited; ++i) {
532 		event = cpuhw->limited_counter[i];
533 		event->hw.idx = cpuhw->limited_hwidx[i];
534 		val = (event->hw.idx == 5) ? pmc5 : pmc6;
535 		prev = local64_read(&event->hw.prev_count);
536 		if (check_and_compute_delta(prev, val))
537 			local64_set(&event->hw.prev_count, val);
538 		perf_event_update_userpage(event);
539 	}
540 }
541 
542 /*
543  * Since limited events don't respect the freeze conditions, we
544  * have to read them immediately after freezing or unfreezing the
545  * other events.  We try to keep the values from the limited
546  * events as consistent as possible by keeping the delay (in
547  * cycles and instructions) between freezing/unfreezing and reading
548  * the limited events as small and consistent as possible.
549  * Therefore, if any limited events are in use, we read them
550  * both, and always in the same order, to minimize variability,
551  * and do it inside the same asm that writes MMCR0.
552  */
write_mmcr0(struct cpu_hw_events * cpuhw,unsigned long mmcr0)553 static void write_mmcr0(struct cpu_hw_events *cpuhw, unsigned long mmcr0)
554 {
555 	unsigned long pmc5, pmc6;
556 
557 	if (!cpuhw->n_limited) {
558 		mtspr(SPRN_MMCR0, mmcr0);
559 		return;
560 	}
561 
562 	/*
563 	 * Write MMCR0, then read PMC5 and PMC6 immediately.
564 	 * To ensure we don't get a performance monitor interrupt
565 	 * between writing MMCR0 and freezing/thawing the limited
566 	 * events, we first write MMCR0 with the event overflow
567 	 * interrupt enable bits turned off.
568 	 */
569 	asm volatile("mtspr %3,%2; mfspr %0,%4; mfspr %1,%5"
570 		     : "=&r" (pmc5), "=&r" (pmc6)
571 		     : "r" (mmcr0 & ~(MMCR0_PMC1CE | MMCR0_PMCjCE)),
572 		       "i" (SPRN_MMCR0),
573 		       "i" (SPRN_PMC5), "i" (SPRN_PMC6));
574 
575 	if (mmcr0 & MMCR0_FC)
576 		freeze_limited_counters(cpuhw, pmc5, pmc6);
577 	else
578 		thaw_limited_counters(cpuhw, pmc5, pmc6);
579 
580 	/*
581 	 * Write the full MMCR0 including the event overflow interrupt
582 	 * enable bits, if necessary.
583 	 */
584 	if (mmcr0 & (MMCR0_PMC1CE | MMCR0_PMCjCE))
585 		mtspr(SPRN_MMCR0, mmcr0);
586 }
587 
588 /*
589  * Disable all events to prevent PMU interrupts and to allow
590  * events to be added or removed.
591  */
power_pmu_disable(struct pmu * pmu)592 static void power_pmu_disable(struct pmu *pmu)
593 {
594 	struct cpu_hw_events *cpuhw;
595 	unsigned long flags;
596 
597 	if (!ppmu)
598 		return;
599 	local_irq_save(flags);
600 	cpuhw = &__get_cpu_var(cpu_hw_events);
601 
602 	if (!cpuhw->disabled) {
603 		cpuhw->disabled = 1;
604 		cpuhw->n_added = 0;
605 
606 		/*
607 		 * Check if we ever enabled the PMU on this cpu.
608 		 */
609 		if (!cpuhw->pmcs_enabled) {
610 			ppc_enable_pmcs();
611 			cpuhw->pmcs_enabled = 1;
612 		}
613 
614 		/*
615 		 * Disable instruction sampling if it was enabled
616 		 */
617 		if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
618 			mtspr(SPRN_MMCRA,
619 			      cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
620 			mb();
621 		}
622 
623 		/*
624 		 * Set the 'freeze counters' bit.
625 		 * The barrier is to make sure the mtspr has been
626 		 * executed and the PMU has frozen the events
627 		 * before we return.
628 		 */
629 		write_mmcr0(cpuhw, mfspr(SPRN_MMCR0) | MMCR0_FC);
630 		mb();
631 	}
632 	local_irq_restore(flags);
633 }
634 
635 /*
636  * Re-enable all events if disable == 0.
637  * If we were previously disabled and events were added, then
638  * put the new config on the PMU.
639  */
power_pmu_enable(struct pmu * pmu)640 static void power_pmu_enable(struct pmu *pmu)
641 {
642 	struct perf_event *event;
643 	struct cpu_hw_events *cpuhw;
644 	unsigned long flags;
645 	long i;
646 	unsigned long val;
647 	s64 left;
648 	unsigned int hwc_index[MAX_HWEVENTS];
649 	int n_lim;
650 	int idx;
651 
652 	if (!ppmu)
653 		return;
654 	local_irq_save(flags);
655 	cpuhw = &__get_cpu_var(cpu_hw_events);
656 	if (!cpuhw->disabled) {
657 		local_irq_restore(flags);
658 		return;
659 	}
660 	cpuhw->disabled = 0;
661 
662 	/*
663 	 * If we didn't change anything, or only removed events,
664 	 * no need to recalculate MMCR* settings and reset the PMCs.
665 	 * Just reenable the PMU with the current MMCR* settings
666 	 * (possibly updated for removal of events).
667 	 */
668 	if (!cpuhw->n_added) {
669 		mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
670 		mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
671 		if (cpuhw->n_events == 0)
672 			ppc_set_pmu_inuse(0);
673 		goto out_enable;
674 	}
675 
676 	/*
677 	 * Compute MMCR* values for the new set of events
678 	 */
679 	if (ppmu->compute_mmcr(cpuhw->events, cpuhw->n_events, hwc_index,
680 			       cpuhw->mmcr)) {
681 		/* shouldn't ever get here */
682 		printk(KERN_ERR "oops compute_mmcr failed\n");
683 		goto out;
684 	}
685 
686 	/*
687 	 * Add in MMCR0 freeze bits corresponding to the
688 	 * attr.exclude_* bits for the first event.
689 	 * We have already checked that all events have the
690 	 * same values for these bits as the first event.
691 	 */
692 	event = cpuhw->event[0];
693 	if (event->attr.exclude_user)
694 		cpuhw->mmcr[0] |= MMCR0_FCP;
695 	if (event->attr.exclude_kernel)
696 		cpuhw->mmcr[0] |= freeze_events_kernel;
697 	if (event->attr.exclude_hv)
698 		cpuhw->mmcr[0] |= MMCR0_FCHV;
699 
700 	/*
701 	 * Write the new configuration to MMCR* with the freeze
702 	 * bit set and set the hardware events to their initial values.
703 	 * Then unfreeze the events.
704 	 */
705 	ppc_set_pmu_inuse(1);
706 	mtspr(SPRN_MMCRA, cpuhw->mmcr[2] & ~MMCRA_SAMPLE_ENABLE);
707 	mtspr(SPRN_MMCR1, cpuhw->mmcr[1]);
708 	mtspr(SPRN_MMCR0, (cpuhw->mmcr[0] & ~(MMCR0_PMC1CE | MMCR0_PMCjCE))
709 				| MMCR0_FC);
710 
711 	/*
712 	 * Read off any pre-existing events that need to move
713 	 * to another PMC.
714 	 */
715 	for (i = 0; i < cpuhw->n_events; ++i) {
716 		event = cpuhw->event[i];
717 		if (event->hw.idx && event->hw.idx != hwc_index[i] + 1) {
718 			power_pmu_read(event);
719 			write_pmc(event->hw.idx, 0);
720 			event->hw.idx = 0;
721 		}
722 	}
723 
724 	/*
725 	 * Initialize the PMCs for all the new and moved events.
726 	 */
727 	cpuhw->n_limited = n_lim = 0;
728 	for (i = 0; i < cpuhw->n_events; ++i) {
729 		event = cpuhw->event[i];
730 		if (event->hw.idx)
731 			continue;
732 		idx = hwc_index[i] + 1;
733 		if (is_limited_pmc(idx)) {
734 			cpuhw->limited_counter[n_lim] = event;
735 			cpuhw->limited_hwidx[n_lim] = idx;
736 			++n_lim;
737 			continue;
738 		}
739 		val = 0;
740 		if (event->hw.sample_period) {
741 			left = local64_read(&event->hw.period_left);
742 			if (left < 0x80000000L)
743 				val = 0x80000000L - left;
744 		}
745 		local64_set(&event->hw.prev_count, val);
746 		event->hw.idx = idx;
747 		if (event->hw.state & PERF_HES_STOPPED)
748 			val = 0;
749 		write_pmc(idx, val);
750 		perf_event_update_userpage(event);
751 	}
752 	cpuhw->n_limited = n_lim;
753 	cpuhw->mmcr[0] |= MMCR0_PMXE | MMCR0_FCECE;
754 
755  out_enable:
756 	mb();
757 	write_mmcr0(cpuhw, cpuhw->mmcr[0]);
758 
759 	/*
760 	 * Enable instruction sampling if necessary
761 	 */
762 	if (cpuhw->mmcr[2] & MMCRA_SAMPLE_ENABLE) {
763 		mb();
764 		mtspr(SPRN_MMCRA, cpuhw->mmcr[2]);
765 	}
766 
767  out:
768 	local_irq_restore(flags);
769 }
770 
collect_events(struct perf_event * group,int max_count,struct perf_event * ctrs[],u64 * events,unsigned int * flags)771 static int collect_events(struct perf_event *group, int max_count,
772 			  struct perf_event *ctrs[], u64 *events,
773 			  unsigned int *flags)
774 {
775 	int n = 0;
776 	struct perf_event *event;
777 
778 	if (!is_software_event(group)) {
779 		if (n >= max_count)
780 			return -1;
781 		ctrs[n] = group;
782 		flags[n] = group->hw.event_base;
783 		events[n++] = group->hw.config;
784 	}
785 	list_for_each_entry(event, &group->sibling_list, group_entry) {
786 		if (!is_software_event(event) &&
787 		    event->state != PERF_EVENT_STATE_OFF) {
788 			if (n >= max_count)
789 				return -1;
790 			ctrs[n] = event;
791 			flags[n] = event->hw.event_base;
792 			events[n++] = event->hw.config;
793 		}
794 	}
795 	return n;
796 }
797 
798 /*
799  * Add a event to the PMU.
800  * If all events are not already frozen, then we disable and
801  * re-enable the PMU in order to get hw_perf_enable to do the
802  * actual work of reconfiguring the PMU.
803  */
power_pmu_add(struct perf_event * event,int ef_flags)804 static int power_pmu_add(struct perf_event *event, int ef_flags)
805 {
806 	struct cpu_hw_events *cpuhw;
807 	unsigned long flags;
808 	int n0;
809 	int ret = -EAGAIN;
810 
811 	local_irq_save(flags);
812 	perf_pmu_disable(event->pmu);
813 
814 	/*
815 	 * Add the event to the list (if there is room)
816 	 * and check whether the total set is still feasible.
817 	 */
818 	cpuhw = &__get_cpu_var(cpu_hw_events);
819 	n0 = cpuhw->n_events;
820 	if (n0 >= ppmu->n_counter)
821 		goto out;
822 	cpuhw->event[n0] = event;
823 	cpuhw->events[n0] = event->hw.config;
824 	cpuhw->flags[n0] = event->hw.event_base;
825 
826 	if (!(ef_flags & PERF_EF_START))
827 		event->hw.state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
828 
829 	/*
830 	 * If group events scheduling transaction was started,
831 	 * skip the schedulability test here, it will be performed
832 	 * at commit time(->commit_txn) as a whole
833 	 */
834 	if (cpuhw->group_flag & PERF_EVENT_TXN)
835 		goto nocheck;
836 
837 	if (check_excludes(cpuhw->event, cpuhw->flags, n0, 1))
838 		goto out;
839 	if (power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n0 + 1))
840 		goto out;
841 	event->hw.config = cpuhw->events[n0];
842 
843 nocheck:
844 	++cpuhw->n_events;
845 	++cpuhw->n_added;
846 
847 	ret = 0;
848  out:
849 	perf_pmu_enable(event->pmu);
850 	local_irq_restore(flags);
851 	return ret;
852 }
853 
854 /*
855  * Remove a event from the PMU.
856  */
power_pmu_del(struct perf_event * event,int ef_flags)857 static void power_pmu_del(struct perf_event *event, int ef_flags)
858 {
859 	struct cpu_hw_events *cpuhw;
860 	long i;
861 	unsigned long flags;
862 
863 	local_irq_save(flags);
864 	perf_pmu_disable(event->pmu);
865 
866 	power_pmu_read(event);
867 
868 	cpuhw = &__get_cpu_var(cpu_hw_events);
869 	for (i = 0; i < cpuhw->n_events; ++i) {
870 		if (event == cpuhw->event[i]) {
871 			while (++i < cpuhw->n_events) {
872 				cpuhw->event[i-1] = cpuhw->event[i];
873 				cpuhw->events[i-1] = cpuhw->events[i];
874 				cpuhw->flags[i-1] = cpuhw->flags[i];
875 			}
876 			--cpuhw->n_events;
877 			ppmu->disable_pmc(event->hw.idx - 1, cpuhw->mmcr);
878 			if (event->hw.idx) {
879 				write_pmc(event->hw.idx, 0);
880 				event->hw.idx = 0;
881 			}
882 			perf_event_update_userpage(event);
883 			break;
884 		}
885 	}
886 	for (i = 0; i < cpuhw->n_limited; ++i)
887 		if (event == cpuhw->limited_counter[i])
888 			break;
889 	if (i < cpuhw->n_limited) {
890 		while (++i < cpuhw->n_limited) {
891 			cpuhw->limited_counter[i-1] = cpuhw->limited_counter[i];
892 			cpuhw->limited_hwidx[i-1] = cpuhw->limited_hwidx[i];
893 		}
894 		--cpuhw->n_limited;
895 	}
896 	if (cpuhw->n_events == 0) {
897 		/* disable exceptions if no events are running */
898 		cpuhw->mmcr[0] &= ~(MMCR0_PMXE | MMCR0_FCECE);
899 	}
900 
901 	perf_pmu_enable(event->pmu);
902 	local_irq_restore(flags);
903 }
904 
905 /*
906  * POWER-PMU does not support disabling individual counters, hence
907  * program their cycle counter to their max value and ignore the interrupts.
908  */
909 
power_pmu_start(struct perf_event * event,int ef_flags)910 static void power_pmu_start(struct perf_event *event, int ef_flags)
911 {
912 	unsigned long flags;
913 	s64 left;
914 	unsigned long val;
915 
916 	if (!event->hw.idx || !event->hw.sample_period)
917 		return;
918 
919 	if (!(event->hw.state & PERF_HES_STOPPED))
920 		return;
921 
922 	if (ef_flags & PERF_EF_RELOAD)
923 		WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
924 
925 	local_irq_save(flags);
926 	perf_pmu_disable(event->pmu);
927 
928 	event->hw.state = 0;
929 	left = local64_read(&event->hw.period_left);
930 
931 	val = 0;
932 	if (left < 0x80000000L)
933 		val = 0x80000000L - left;
934 
935 	write_pmc(event->hw.idx, val);
936 
937 	perf_event_update_userpage(event);
938 	perf_pmu_enable(event->pmu);
939 	local_irq_restore(flags);
940 }
941 
power_pmu_stop(struct perf_event * event,int ef_flags)942 static void power_pmu_stop(struct perf_event *event, int ef_flags)
943 {
944 	unsigned long flags;
945 
946 	if (!event->hw.idx || !event->hw.sample_period)
947 		return;
948 
949 	if (event->hw.state & PERF_HES_STOPPED)
950 		return;
951 
952 	local_irq_save(flags);
953 	perf_pmu_disable(event->pmu);
954 
955 	power_pmu_read(event);
956 	event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
957 	write_pmc(event->hw.idx, 0);
958 
959 	perf_event_update_userpage(event);
960 	perf_pmu_enable(event->pmu);
961 	local_irq_restore(flags);
962 }
963 
964 /*
965  * Start group events scheduling transaction
966  * Set the flag to make pmu::enable() not perform the
967  * schedulability test, it will be performed at commit time
968  */
power_pmu_start_txn(struct pmu * pmu)969 void power_pmu_start_txn(struct pmu *pmu)
970 {
971 	struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
972 
973 	perf_pmu_disable(pmu);
974 	cpuhw->group_flag |= PERF_EVENT_TXN;
975 	cpuhw->n_txn_start = cpuhw->n_events;
976 }
977 
978 /*
979  * Stop group events scheduling transaction
980  * Clear the flag and pmu::enable() will perform the
981  * schedulability test.
982  */
power_pmu_cancel_txn(struct pmu * pmu)983 void power_pmu_cancel_txn(struct pmu *pmu)
984 {
985 	struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
986 
987 	cpuhw->group_flag &= ~PERF_EVENT_TXN;
988 	perf_pmu_enable(pmu);
989 }
990 
991 /*
992  * Commit group events scheduling transaction
993  * Perform the group schedulability test as a whole
994  * Return 0 if success
995  */
power_pmu_commit_txn(struct pmu * pmu)996 int power_pmu_commit_txn(struct pmu *pmu)
997 {
998 	struct cpu_hw_events *cpuhw;
999 	long i, n;
1000 
1001 	if (!ppmu)
1002 		return -EAGAIN;
1003 	cpuhw = &__get_cpu_var(cpu_hw_events);
1004 	n = cpuhw->n_events;
1005 	if (check_excludes(cpuhw->event, cpuhw->flags, 0, n))
1006 		return -EAGAIN;
1007 	i = power_check_constraints(cpuhw, cpuhw->events, cpuhw->flags, n);
1008 	if (i < 0)
1009 		return -EAGAIN;
1010 
1011 	for (i = cpuhw->n_txn_start; i < n; ++i)
1012 		cpuhw->event[i]->hw.config = cpuhw->events[i];
1013 
1014 	cpuhw->group_flag &= ~PERF_EVENT_TXN;
1015 	perf_pmu_enable(pmu);
1016 	return 0;
1017 }
1018 
1019 /*
1020  * Return 1 if we might be able to put event on a limited PMC,
1021  * or 0 if not.
1022  * A event can only go on a limited PMC if it counts something
1023  * that a limited PMC can count, doesn't require interrupts, and
1024  * doesn't exclude any processor mode.
1025  */
can_go_on_limited_pmc(struct perf_event * event,u64 ev,unsigned int flags)1026 static int can_go_on_limited_pmc(struct perf_event *event, u64 ev,
1027 				 unsigned int flags)
1028 {
1029 	int n;
1030 	u64 alt[MAX_EVENT_ALTERNATIVES];
1031 
1032 	if (event->attr.exclude_user
1033 	    || event->attr.exclude_kernel
1034 	    || event->attr.exclude_hv
1035 	    || event->attr.sample_period)
1036 		return 0;
1037 
1038 	if (ppmu->limited_pmc_event(ev))
1039 		return 1;
1040 
1041 	/*
1042 	 * The requested event_id isn't on a limited PMC already;
1043 	 * see if any alternative code goes on a limited PMC.
1044 	 */
1045 	if (!ppmu->get_alternatives)
1046 		return 0;
1047 
1048 	flags |= PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD;
1049 	n = ppmu->get_alternatives(ev, flags, alt);
1050 
1051 	return n > 0;
1052 }
1053 
1054 /*
1055  * Find an alternative event_id that goes on a normal PMC, if possible,
1056  * and return the event_id code, or 0 if there is no such alternative.
1057  * (Note: event_id code 0 is "don't count" on all machines.)
1058  */
normal_pmc_alternative(u64 ev,unsigned long flags)1059 static u64 normal_pmc_alternative(u64 ev, unsigned long flags)
1060 {
1061 	u64 alt[MAX_EVENT_ALTERNATIVES];
1062 	int n;
1063 
1064 	flags &= ~(PPMU_LIMITED_PMC_OK | PPMU_LIMITED_PMC_REQD);
1065 	n = ppmu->get_alternatives(ev, flags, alt);
1066 	if (!n)
1067 		return 0;
1068 	return alt[0];
1069 }
1070 
1071 /* Number of perf_events counting hardware events */
1072 static atomic_t num_events;
1073 /* Used to avoid races in calling reserve/release_pmc_hardware */
1074 static DEFINE_MUTEX(pmc_reserve_mutex);
1075 
1076 /*
1077  * Release the PMU if this is the last perf_event.
1078  */
hw_perf_event_destroy(struct perf_event * event)1079 static void hw_perf_event_destroy(struct perf_event *event)
1080 {
1081 	if (!atomic_add_unless(&num_events, -1, 1)) {
1082 		mutex_lock(&pmc_reserve_mutex);
1083 		if (atomic_dec_return(&num_events) == 0)
1084 			release_pmc_hardware();
1085 		mutex_unlock(&pmc_reserve_mutex);
1086 	}
1087 }
1088 
1089 /*
1090  * Translate a generic cache event_id config to a raw event_id code.
1091  */
hw_perf_cache_event(u64 config,u64 * eventp)1092 static int hw_perf_cache_event(u64 config, u64 *eventp)
1093 {
1094 	unsigned long type, op, result;
1095 	int ev;
1096 
1097 	if (!ppmu->cache_events)
1098 		return -EINVAL;
1099 
1100 	/* unpack config */
1101 	type = config & 0xff;
1102 	op = (config >> 8) & 0xff;
1103 	result = (config >> 16) & 0xff;
1104 
1105 	if (type >= PERF_COUNT_HW_CACHE_MAX ||
1106 	    op >= PERF_COUNT_HW_CACHE_OP_MAX ||
1107 	    result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1108 		return -EINVAL;
1109 
1110 	ev = (*ppmu->cache_events)[type][op][result];
1111 	if (ev == 0)
1112 		return -EOPNOTSUPP;
1113 	if (ev == -1)
1114 		return -EINVAL;
1115 	*eventp = ev;
1116 	return 0;
1117 }
1118 
power_pmu_event_init(struct perf_event * event)1119 static int power_pmu_event_init(struct perf_event *event)
1120 {
1121 	u64 ev;
1122 	unsigned long flags;
1123 	struct perf_event *ctrs[MAX_HWEVENTS];
1124 	u64 events[MAX_HWEVENTS];
1125 	unsigned int cflags[MAX_HWEVENTS];
1126 	int n;
1127 	int err;
1128 	struct cpu_hw_events *cpuhw;
1129 
1130 	if (!ppmu)
1131 		return -ENOENT;
1132 
1133 	/* does not support taken branch sampling */
1134 	if (has_branch_stack(event))
1135 		return -EOPNOTSUPP;
1136 
1137 	switch (event->attr.type) {
1138 	case PERF_TYPE_HARDWARE:
1139 		ev = event->attr.config;
1140 		if (ev >= ppmu->n_generic || ppmu->generic_events[ev] == 0)
1141 			return -EOPNOTSUPP;
1142 		ev = ppmu->generic_events[ev];
1143 		break;
1144 	case PERF_TYPE_HW_CACHE:
1145 		err = hw_perf_cache_event(event->attr.config, &ev);
1146 		if (err)
1147 			return err;
1148 		break;
1149 	case PERF_TYPE_RAW:
1150 		ev = event->attr.config;
1151 		break;
1152 	default:
1153 		return -ENOENT;
1154 	}
1155 
1156 	event->hw.config_base = ev;
1157 	event->hw.idx = 0;
1158 
1159 	/*
1160 	 * If we are not running on a hypervisor, force the
1161 	 * exclude_hv bit to 0 so that we don't care what
1162 	 * the user set it to.
1163 	 */
1164 	if (!firmware_has_feature(FW_FEATURE_LPAR))
1165 		event->attr.exclude_hv = 0;
1166 
1167 	/*
1168 	 * If this is a per-task event, then we can use
1169 	 * PM_RUN_* events interchangeably with their non RUN_*
1170 	 * equivalents, e.g. PM_RUN_CYC instead of PM_CYC.
1171 	 * XXX we should check if the task is an idle task.
1172 	 */
1173 	flags = 0;
1174 	if (event->attach_state & PERF_ATTACH_TASK)
1175 		flags |= PPMU_ONLY_COUNT_RUN;
1176 
1177 	/*
1178 	 * If this machine has limited events, check whether this
1179 	 * event_id could go on a limited event.
1180 	 */
1181 	if (ppmu->flags & PPMU_LIMITED_PMC5_6) {
1182 		if (can_go_on_limited_pmc(event, ev, flags)) {
1183 			flags |= PPMU_LIMITED_PMC_OK;
1184 		} else if (ppmu->limited_pmc_event(ev)) {
1185 			/*
1186 			 * The requested event_id is on a limited PMC,
1187 			 * but we can't use a limited PMC; see if any
1188 			 * alternative goes on a normal PMC.
1189 			 */
1190 			ev = normal_pmc_alternative(ev, flags);
1191 			if (!ev)
1192 				return -EINVAL;
1193 		}
1194 	}
1195 
1196 	/*
1197 	 * If this is in a group, check if it can go on with all the
1198 	 * other hardware events in the group.  We assume the event
1199 	 * hasn't been linked into its leader's sibling list at this point.
1200 	 */
1201 	n = 0;
1202 	if (event->group_leader != event) {
1203 		n = collect_events(event->group_leader, ppmu->n_counter - 1,
1204 				   ctrs, events, cflags);
1205 		if (n < 0)
1206 			return -EINVAL;
1207 	}
1208 	events[n] = ev;
1209 	ctrs[n] = event;
1210 	cflags[n] = flags;
1211 	if (check_excludes(ctrs, cflags, n, 1))
1212 		return -EINVAL;
1213 
1214 	cpuhw = &get_cpu_var(cpu_hw_events);
1215 	err = power_check_constraints(cpuhw, events, cflags, n + 1);
1216 	put_cpu_var(cpu_hw_events);
1217 	if (err)
1218 		return -EINVAL;
1219 
1220 	event->hw.config = events[n];
1221 	event->hw.event_base = cflags[n];
1222 	event->hw.last_period = event->hw.sample_period;
1223 	local64_set(&event->hw.period_left, event->hw.last_period);
1224 
1225 	/*
1226 	 * See if we need to reserve the PMU.
1227 	 * If no events are currently in use, then we have to take a
1228 	 * mutex to ensure that we don't race with another task doing
1229 	 * reserve_pmc_hardware or release_pmc_hardware.
1230 	 */
1231 	err = 0;
1232 	if (!atomic_inc_not_zero(&num_events)) {
1233 		mutex_lock(&pmc_reserve_mutex);
1234 		if (atomic_read(&num_events) == 0 &&
1235 		    reserve_pmc_hardware(perf_event_interrupt))
1236 			err = -EBUSY;
1237 		else
1238 			atomic_inc(&num_events);
1239 		mutex_unlock(&pmc_reserve_mutex);
1240 	}
1241 	event->destroy = hw_perf_event_destroy;
1242 
1243 	return err;
1244 }
1245 
power_pmu_event_idx(struct perf_event * event)1246 static int power_pmu_event_idx(struct perf_event *event)
1247 {
1248 	return event->hw.idx;
1249 }
1250 
1251 struct pmu power_pmu = {
1252 	.pmu_enable	= power_pmu_enable,
1253 	.pmu_disable	= power_pmu_disable,
1254 	.event_init	= power_pmu_event_init,
1255 	.add		= power_pmu_add,
1256 	.del		= power_pmu_del,
1257 	.start		= power_pmu_start,
1258 	.stop		= power_pmu_stop,
1259 	.read		= power_pmu_read,
1260 	.start_txn	= power_pmu_start_txn,
1261 	.cancel_txn	= power_pmu_cancel_txn,
1262 	.commit_txn	= power_pmu_commit_txn,
1263 	.event_idx	= power_pmu_event_idx,
1264 };
1265 
1266 /*
1267  * A counter has overflowed; update its count and record
1268  * things if requested.  Note that interrupts are hard-disabled
1269  * here so there is no possibility of being interrupted.
1270  */
record_and_restart(struct perf_event * event,unsigned long val,struct pt_regs * regs)1271 static void record_and_restart(struct perf_event *event, unsigned long val,
1272 			       struct pt_regs *regs)
1273 {
1274 	u64 period = event->hw.sample_period;
1275 	s64 prev, delta, left;
1276 	int record = 0;
1277 
1278 	if (event->hw.state & PERF_HES_STOPPED) {
1279 		write_pmc(event->hw.idx, 0);
1280 		return;
1281 	}
1282 
1283 	/* we don't have to worry about interrupts here */
1284 	prev = local64_read(&event->hw.prev_count);
1285 	delta = check_and_compute_delta(prev, val);
1286 	local64_add(delta, &event->count);
1287 
1288 	/*
1289 	 * See if the total period for this event has expired,
1290 	 * and update for the next period.
1291 	 */
1292 	val = 0;
1293 	left = local64_read(&event->hw.period_left) - delta;
1294 	if (period) {
1295 		if (left <= 0) {
1296 			left += period;
1297 			if (left <= 0)
1298 				left = period;
1299 			record = 1;
1300 			event->hw.last_period = event->hw.sample_period;
1301 		}
1302 		if (left < 0x80000000LL)
1303 			val = 0x80000000LL - left;
1304 	}
1305 
1306 	write_pmc(event->hw.idx, val);
1307 	local64_set(&event->hw.prev_count, val);
1308 	local64_set(&event->hw.period_left, left);
1309 	perf_event_update_userpage(event);
1310 
1311 	/*
1312 	 * Finally record data if requested.
1313 	 */
1314 	if (record) {
1315 		struct perf_sample_data data;
1316 
1317 		perf_sample_data_init(&data, ~0ULL);
1318 		data.period = event->hw.last_period;
1319 
1320 		if (event->attr.sample_type & PERF_SAMPLE_ADDR)
1321 			perf_get_data_addr(regs, &data.addr);
1322 
1323 		if (perf_event_overflow(event, &data, regs))
1324 			power_pmu_stop(event, 0);
1325 	}
1326 }
1327 
1328 /*
1329  * Called from generic code to get the misc flags (i.e. processor mode)
1330  * for an event_id.
1331  */
perf_misc_flags(struct pt_regs * regs)1332 unsigned long perf_misc_flags(struct pt_regs *regs)
1333 {
1334 	u32 flags = perf_get_misc_flags(regs);
1335 
1336 	if (flags)
1337 		return flags;
1338 	return user_mode(regs) ? PERF_RECORD_MISC_USER :
1339 		PERF_RECORD_MISC_KERNEL;
1340 }
1341 
1342 /*
1343  * Called from generic code to get the instruction pointer
1344  * for an event_id.
1345  */
perf_instruction_pointer(struct pt_regs * regs)1346 unsigned long perf_instruction_pointer(struct pt_regs *regs)
1347 {
1348 	unsigned long mmcra = regs->dsisr;
1349 
1350 	/* Not a PMU interrupt */
1351 	if (TRAP(regs) != 0xf00)
1352 		return regs->nip;
1353 
1354 	/* Processor doesn't support sampling non marked events */
1355 	if ((ppmu->flags & PPMU_NO_CONT_SAMPLING) &&
1356 	    !(mmcra & MMCRA_SAMPLE_ENABLE))
1357 		return regs->nip;
1358 
1359 	return mfspr(SPRN_SIAR) + perf_ip_adjust(regs);
1360 }
1361 
pmc_overflow(unsigned long val)1362 static bool pmc_overflow(unsigned long val)
1363 {
1364 	if ((int)val < 0)
1365 		return true;
1366 
1367 	/*
1368 	 * Events on POWER7 can roll back if a speculative event doesn't
1369 	 * eventually complete. Unfortunately in some rare cases they will
1370 	 * raise a performance monitor exception. We need to catch this to
1371 	 * ensure we reset the PMC. In all cases the PMC will be 256 or less
1372 	 * cycles from overflow.
1373 	 *
1374 	 * We only do this if the first pass fails to find any overflowing
1375 	 * PMCs because a user might set a period of less than 256 and we
1376 	 * don't want to mistakenly reset them.
1377 	 */
1378 	if (__is_processor(PV_POWER7) && ((0x80000000 - val) <= 256))
1379 		return true;
1380 
1381 	return false;
1382 }
1383 
1384 /*
1385  * Performance monitor interrupt stuff
1386  */
perf_event_interrupt(struct pt_regs * regs)1387 static void perf_event_interrupt(struct pt_regs *regs)
1388 {
1389 	int i;
1390 	struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
1391 	struct perf_event *event;
1392 	unsigned long val;
1393 	int found = 0;
1394 	int nmi;
1395 
1396 	if (cpuhw->n_limited)
1397 		freeze_limited_counters(cpuhw, mfspr(SPRN_PMC5),
1398 					mfspr(SPRN_PMC6));
1399 
1400 	perf_read_regs(regs);
1401 
1402 	nmi = perf_intr_is_nmi(regs);
1403 	if (nmi)
1404 		nmi_enter();
1405 	else
1406 		irq_enter();
1407 
1408 	for (i = 0; i < cpuhw->n_events; ++i) {
1409 		event = cpuhw->event[i];
1410 		if (!event->hw.idx || is_limited_pmc(event->hw.idx))
1411 			continue;
1412 		val = read_pmc(event->hw.idx);
1413 		if ((int)val < 0) {
1414 			/* event has overflowed */
1415 			found = 1;
1416 			record_and_restart(event, val, regs);
1417 		}
1418 	}
1419 
1420 	/*
1421 	 * In case we didn't find and reset the event that caused
1422 	 * the interrupt, scan all events and reset any that are
1423 	 * negative, to avoid getting continual interrupts.
1424 	 * Any that we processed in the previous loop will not be negative.
1425 	 */
1426 	if (!found) {
1427 		for (i = 0; i < ppmu->n_counter; ++i) {
1428 			if (is_limited_pmc(i + 1))
1429 				continue;
1430 			val = read_pmc(i + 1);
1431 			if (pmc_overflow(val))
1432 				write_pmc(i + 1, 0);
1433 		}
1434 	}
1435 
1436 	/*
1437 	 * Reset MMCR0 to its normal value.  This will set PMXE and
1438 	 * clear FC (freeze counters) and PMAO (perf mon alert occurred)
1439 	 * and thus allow interrupts to occur again.
1440 	 * XXX might want to use MSR.PM to keep the events frozen until
1441 	 * we get back out of this interrupt.
1442 	 */
1443 	write_mmcr0(cpuhw, cpuhw->mmcr[0]);
1444 
1445 	if (nmi)
1446 		nmi_exit();
1447 	else
1448 		irq_exit();
1449 }
1450 
power_pmu_setup(int cpu)1451 static void power_pmu_setup(int cpu)
1452 {
1453 	struct cpu_hw_events *cpuhw = &per_cpu(cpu_hw_events, cpu);
1454 
1455 	if (!ppmu)
1456 		return;
1457 	memset(cpuhw, 0, sizeof(*cpuhw));
1458 	cpuhw->mmcr[0] = MMCR0_FC;
1459 }
1460 
1461 static int __cpuinit
power_pmu_notifier(struct notifier_block * self,unsigned long action,void * hcpu)1462 power_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
1463 {
1464 	unsigned int cpu = (long)hcpu;
1465 
1466 	switch (action & ~CPU_TASKS_FROZEN) {
1467 	case CPU_UP_PREPARE:
1468 		power_pmu_setup(cpu);
1469 		break;
1470 
1471 	default:
1472 		break;
1473 	}
1474 
1475 	return NOTIFY_OK;
1476 }
1477 
register_power_pmu(struct power_pmu * pmu)1478 int __cpuinit register_power_pmu(struct power_pmu *pmu)
1479 {
1480 	if (ppmu)
1481 		return -EBUSY;		/* something's already registered */
1482 
1483 	ppmu = pmu;
1484 	pr_info("%s performance monitor hardware support registered\n",
1485 		pmu->name);
1486 
1487 #ifdef MSR_HV
1488 	/*
1489 	 * Use FCHV to ignore kernel events if MSR.HV is set.
1490 	 */
1491 	if (mfmsr() & MSR_HV)
1492 		freeze_events_kernel = MMCR0_FCHV;
1493 #endif /* CONFIG_PPC64 */
1494 
1495 	perf_pmu_register(&power_pmu, "cpu", PERF_TYPE_RAW);
1496 	perf_cpu_notifier(power_pmu_notifier);
1497 
1498 	return 0;
1499 }
1500