xref: /OK3568_Linux_fs/kernel/arch/powerpc/oprofile/op_model_power4.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2004 Anton Blanchard <anton@au.ibm.com>, IBM
4*4882a593Smuzhiyun  * Added mmcra[slot] support:
5*4882a593Smuzhiyun  * Copyright (C) 2006-2007 Will Schmidt <willschm@us.ibm.com>, IBM
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/oprofile.h>
9*4882a593Smuzhiyun #include <linux/smp.h>
10*4882a593Smuzhiyun #include <asm/firmware.h>
11*4882a593Smuzhiyun #include <asm/ptrace.h>
12*4882a593Smuzhiyun #include <asm/processor.h>
13*4882a593Smuzhiyun #include <asm/cputable.h>
14*4882a593Smuzhiyun #include <asm/rtas.h>
15*4882a593Smuzhiyun #include <asm/oprofile_impl.h>
16*4882a593Smuzhiyun #include <asm/reg.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define dbg(args...)
19*4882a593Smuzhiyun #define OPROFILE_PM_PMCSEL_MSK      0xffULL
20*4882a593Smuzhiyun #define OPROFILE_PM_UNIT_SHIFT      60
21*4882a593Smuzhiyun #define OPROFILE_PM_UNIT_MSK        0xfULL
22*4882a593Smuzhiyun #define OPROFILE_MAX_PMC_NUM        3
23*4882a593Smuzhiyun #define OPROFILE_PMSEL_FIELD_WIDTH  8
24*4882a593Smuzhiyun #define OPROFILE_UNIT_FIELD_WIDTH   4
25*4882a593Smuzhiyun #define MMCRA_SIAR_VALID_MASK       0x10000000ULL
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun static unsigned long reset_value[OP_MAX_COUNTER];
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun static int oprofile_running;
30*4882a593Smuzhiyun static int use_slot_nums;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /* mmcr values are set in power4_reg_setup, used in power4_cpu_setup */
33*4882a593Smuzhiyun static u32 mmcr0_val;
34*4882a593Smuzhiyun static u64 mmcr1_val;
35*4882a593Smuzhiyun static u64 mmcra_val;
36*4882a593Smuzhiyun static u32 cntr_marked_events;
37*4882a593Smuzhiyun 
power7_marked_instr_event(u64 mmcr1)38*4882a593Smuzhiyun static int power7_marked_instr_event(u64 mmcr1)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	u64 psel, unit;
41*4882a593Smuzhiyun 	int pmc, cntr_marked_events = 0;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	/* Given the MMCR1 value, look at the field for each counter to
44*4882a593Smuzhiyun 	 * determine if it is a marked event.  Code based on the function
45*4882a593Smuzhiyun 	 * power7_marked_instr_event() in file arch/powerpc/perf/power7-pmu.c.
46*4882a593Smuzhiyun 	 */
47*4882a593Smuzhiyun 	for (pmc = 0; pmc < 4; pmc++) {
48*4882a593Smuzhiyun 		psel = mmcr1 & (OPROFILE_PM_PMCSEL_MSK
49*4882a593Smuzhiyun 				<< (OPROFILE_MAX_PMC_NUM - pmc)
50*4882a593Smuzhiyun 				* OPROFILE_PMSEL_FIELD_WIDTH);
51*4882a593Smuzhiyun 		psel = (psel >> ((OPROFILE_MAX_PMC_NUM - pmc)
52*4882a593Smuzhiyun 				 * OPROFILE_PMSEL_FIELD_WIDTH)) & ~1ULL;
53*4882a593Smuzhiyun 		unit = mmcr1 & (OPROFILE_PM_UNIT_MSK
54*4882a593Smuzhiyun 				<< (OPROFILE_PM_UNIT_SHIFT
55*4882a593Smuzhiyun 				    - (pmc * OPROFILE_PMSEL_FIELD_WIDTH )));
56*4882a593Smuzhiyun 		unit = unit >> (OPROFILE_PM_UNIT_SHIFT
57*4882a593Smuzhiyun 				- (pmc * OPROFILE_PMSEL_FIELD_WIDTH));
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 		switch (psel >> 4) {
60*4882a593Smuzhiyun 		case 2:
61*4882a593Smuzhiyun 			cntr_marked_events |= (pmc == 1 || pmc == 3) << pmc;
62*4882a593Smuzhiyun 			break;
63*4882a593Smuzhiyun 		case 3:
64*4882a593Smuzhiyun 			if (psel == 0x3c) {
65*4882a593Smuzhiyun 				cntr_marked_events |= (pmc == 0) << pmc;
66*4882a593Smuzhiyun 				break;
67*4882a593Smuzhiyun 			}
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 			if (psel == 0x3e) {
70*4882a593Smuzhiyun 				cntr_marked_events |= (pmc != 1) << pmc;
71*4882a593Smuzhiyun 				break;
72*4882a593Smuzhiyun 			}
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 			cntr_marked_events |= 1 << pmc;
75*4882a593Smuzhiyun 			break;
76*4882a593Smuzhiyun 		case 4:
77*4882a593Smuzhiyun 		case 5:
78*4882a593Smuzhiyun 			cntr_marked_events |= (unit == 0xd) << pmc;
79*4882a593Smuzhiyun 			break;
80*4882a593Smuzhiyun 		case 6:
81*4882a593Smuzhiyun 			if (psel == 0x64)
82*4882a593Smuzhiyun 				cntr_marked_events |= (pmc >= 2) << pmc;
83*4882a593Smuzhiyun 			break;
84*4882a593Smuzhiyun 		case 8:
85*4882a593Smuzhiyun 			cntr_marked_events |= (unit == 0xd) << pmc;
86*4882a593Smuzhiyun 			break;
87*4882a593Smuzhiyun 		}
88*4882a593Smuzhiyun 	}
89*4882a593Smuzhiyun 	return cntr_marked_events;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
power4_reg_setup(struct op_counter_config * ctr,struct op_system_config * sys,int num_ctrs)92*4882a593Smuzhiyun static int power4_reg_setup(struct op_counter_config *ctr,
93*4882a593Smuzhiyun 			     struct op_system_config *sys,
94*4882a593Smuzhiyun 			     int num_ctrs)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	int i;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	/*
99*4882a593Smuzhiyun 	 * The performance counter event settings are given in the mmcr0,
100*4882a593Smuzhiyun 	 * mmcr1 and mmcra values passed from the user in the
101*4882a593Smuzhiyun 	 * op_system_config structure (sys variable).
102*4882a593Smuzhiyun 	 */
103*4882a593Smuzhiyun 	mmcr0_val = sys->mmcr0;
104*4882a593Smuzhiyun 	mmcr1_val = sys->mmcr1;
105*4882a593Smuzhiyun 	mmcra_val = sys->mmcra;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	/* Power 7+ and newer architectures:
108*4882a593Smuzhiyun 	 * Determine which counter events in the group (the group of events is
109*4882a593Smuzhiyun 	 * specified by the bit settings in the MMCR1 register) are marked
110*4882a593Smuzhiyun 	 * events for use in the interrupt handler.  Do the calculation once
111*4882a593Smuzhiyun 	 * before OProfile starts.  Information is used in the interrupt
112*4882a593Smuzhiyun 	 * handler.  Starting with Power 7+ we only record the sample for
113*4882a593Smuzhiyun 	 * marked events if the SIAR valid bit is set.  For non marked events
114*4882a593Smuzhiyun 	 * the sample is always recorded.
115*4882a593Smuzhiyun 	 */
116*4882a593Smuzhiyun 	if (pvr_version_is(PVR_POWER7p))
117*4882a593Smuzhiyun 		cntr_marked_events = power7_marked_instr_event(mmcr1_val);
118*4882a593Smuzhiyun 	else
119*4882a593Smuzhiyun 		cntr_marked_events = 0; /* For older processors, set the bit map
120*4882a593Smuzhiyun 					 * to zero so the sample will always be
121*4882a593Smuzhiyun 					 * be recorded.
122*4882a593Smuzhiyun 					 */
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	for (i = 0; i < cur_cpu_spec->num_pmcs; ++i)
125*4882a593Smuzhiyun 		reset_value[i] = 0x80000000UL - ctr[i].count;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	/* setup user and kernel profiling */
128*4882a593Smuzhiyun 	if (sys->enable_kernel)
129*4882a593Smuzhiyun 		mmcr0_val &= ~MMCR0_KERNEL_DISABLE;
130*4882a593Smuzhiyun 	else
131*4882a593Smuzhiyun 		mmcr0_val |= MMCR0_KERNEL_DISABLE;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	if (sys->enable_user)
134*4882a593Smuzhiyun 		mmcr0_val &= ~MMCR0_PROBLEM_DISABLE;
135*4882a593Smuzhiyun 	else
136*4882a593Smuzhiyun 		mmcr0_val |= MMCR0_PROBLEM_DISABLE;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	if (pvr_version_is(PVR_POWER4) || pvr_version_is(PVR_POWER4p) ||
139*4882a593Smuzhiyun 	    pvr_version_is(PVR_970) || pvr_version_is(PVR_970FX) ||
140*4882a593Smuzhiyun 	    pvr_version_is(PVR_970MP) || pvr_version_is(PVR_970GX) ||
141*4882a593Smuzhiyun 	    pvr_version_is(PVR_POWER5) || pvr_version_is(PVR_POWER5p))
142*4882a593Smuzhiyun 		use_slot_nums = 1;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	return 0;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun extern void ppc_enable_pmcs(void);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun /*
150*4882a593Smuzhiyun  * Older CPUs require the MMCRA sample bit to be always set, but newer
151*4882a593Smuzhiyun  * CPUs only want it set for some groups. Eventually we will remove all
152*4882a593Smuzhiyun  * knowledge of this bit in the kernel, oprofile userspace should be
153*4882a593Smuzhiyun  * setting it when required.
154*4882a593Smuzhiyun  *
155*4882a593Smuzhiyun  * In order to keep current installations working we force the bit for
156*4882a593Smuzhiyun  * those older CPUs. Once everyone has updated their oprofile userspace we
157*4882a593Smuzhiyun  * can remove this hack.
158*4882a593Smuzhiyun  */
mmcra_must_set_sample(void)159*4882a593Smuzhiyun static inline int mmcra_must_set_sample(void)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun 	if (pvr_version_is(PVR_POWER4) || pvr_version_is(PVR_POWER4p) ||
162*4882a593Smuzhiyun 	    pvr_version_is(PVR_970) || pvr_version_is(PVR_970FX) ||
163*4882a593Smuzhiyun 	    pvr_version_is(PVR_970MP) || pvr_version_is(PVR_970GX))
164*4882a593Smuzhiyun 		return 1;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	return 0;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
power4_cpu_setup(struct op_counter_config * ctr)169*4882a593Smuzhiyun static int power4_cpu_setup(struct op_counter_config *ctr)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	unsigned int mmcr0 = mmcr0_val;
172*4882a593Smuzhiyun 	unsigned long mmcra = mmcra_val;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	ppc_enable_pmcs();
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	/* set the freeze bit */
177*4882a593Smuzhiyun 	mmcr0 |= MMCR0_FC;
178*4882a593Smuzhiyun 	mtspr(SPRN_MMCR0, mmcr0);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	mmcr0 |= MMCR0_FCM1|MMCR0_PMXE|MMCR0_FCECE;
181*4882a593Smuzhiyun 	mmcr0 |= MMCR0_PMC1CE|MMCR0_PMCjCE;
182*4882a593Smuzhiyun 	mtspr(SPRN_MMCR0, mmcr0);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	mtspr(SPRN_MMCR1, mmcr1_val);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	if (mmcra_must_set_sample())
187*4882a593Smuzhiyun 		mmcra |= MMCRA_SAMPLE_ENABLE;
188*4882a593Smuzhiyun 	mtspr(SPRN_MMCRA, mmcra);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	dbg("setup on cpu %d, mmcr0 %lx\n", smp_processor_id(),
191*4882a593Smuzhiyun 	    mfspr(SPRN_MMCR0));
192*4882a593Smuzhiyun 	dbg("setup on cpu %d, mmcr1 %lx\n", smp_processor_id(),
193*4882a593Smuzhiyun 	    mfspr(SPRN_MMCR1));
194*4882a593Smuzhiyun 	dbg("setup on cpu %d, mmcra %lx\n", smp_processor_id(),
195*4882a593Smuzhiyun 	    mfspr(SPRN_MMCRA));
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	return 0;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
power4_start(struct op_counter_config * ctr)200*4882a593Smuzhiyun static int power4_start(struct op_counter_config *ctr)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	int i;
203*4882a593Smuzhiyun 	unsigned int mmcr0;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	/* set the PMM bit (see comment below) */
206*4882a593Smuzhiyun 	mtmsr(mfmsr() | MSR_PMM);
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
209*4882a593Smuzhiyun 		if (ctr[i].enabled) {
210*4882a593Smuzhiyun 			classic_ctr_write(i, reset_value[i]);
211*4882a593Smuzhiyun 		} else {
212*4882a593Smuzhiyun 			classic_ctr_write(i, 0);
213*4882a593Smuzhiyun 		}
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	mmcr0 = mfspr(SPRN_MMCR0);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	/*
219*4882a593Smuzhiyun 	 * We must clear the PMAO bit on some (GQ) chips. Just do it
220*4882a593Smuzhiyun 	 * all the time
221*4882a593Smuzhiyun 	 */
222*4882a593Smuzhiyun 	mmcr0 &= ~MMCR0_PMAO;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	/*
225*4882a593Smuzhiyun 	 * now clear the freeze bit, counting will not start until we
226*4882a593Smuzhiyun 	 * rfid from this excetion, because only at that point will
227*4882a593Smuzhiyun 	 * the PMM bit be cleared
228*4882a593Smuzhiyun 	 */
229*4882a593Smuzhiyun 	mmcr0 &= ~MMCR0_FC;
230*4882a593Smuzhiyun 	mtspr(SPRN_MMCR0, mmcr0);
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	oprofile_running = 1;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	dbg("start on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
235*4882a593Smuzhiyun 	return 0;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun 
power4_stop(void)238*4882a593Smuzhiyun static void power4_stop(void)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	unsigned int mmcr0;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	/* freeze counters */
243*4882a593Smuzhiyun 	mmcr0 = mfspr(SPRN_MMCR0);
244*4882a593Smuzhiyun 	mmcr0 |= MMCR0_FC;
245*4882a593Smuzhiyun 	mtspr(SPRN_MMCR0, mmcr0);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	oprofile_running = 0;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	dbg("stop on cpu %d, mmcr0 %x\n", smp_processor_id(), mmcr0);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	mb();
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun /* Fake functions used by canonicalize_pc */
hypervisor_bucket(void)255*4882a593Smuzhiyun static void __used hypervisor_bucket(void)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun 
rtas_bucket(void)259*4882a593Smuzhiyun static void __used rtas_bucket(void)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun 
kernel_unknown_bucket(void)263*4882a593Smuzhiyun static void __used kernel_unknown_bucket(void)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun /*
268*4882a593Smuzhiyun  * On GQ and newer the MMCRA stores the HV and PR bits at the time
269*4882a593Smuzhiyun  * the SIAR was sampled. We use that to work out if the SIAR was sampled in
270*4882a593Smuzhiyun  * the hypervisor, our exception vectors or RTAS.
271*4882a593Smuzhiyun  * If the MMCRA_SAMPLE_ENABLE bit is set, we can use the MMCRA[slot] bits
272*4882a593Smuzhiyun  * to more accurately identify the address of the sampled instruction. The
273*4882a593Smuzhiyun  * mmcra[slot] bits represent the slot number of a sampled instruction
274*4882a593Smuzhiyun  * within an instruction group.  The slot will contain a value between 1
275*4882a593Smuzhiyun  * and 5 if MMCRA_SAMPLE_ENABLE is set, otherwise 0.
276*4882a593Smuzhiyun  */
get_pc(struct pt_regs * regs)277*4882a593Smuzhiyun static unsigned long get_pc(struct pt_regs *regs)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	unsigned long pc = mfspr(SPRN_SIAR);
280*4882a593Smuzhiyun 	unsigned long mmcra;
281*4882a593Smuzhiyun 	unsigned long slot;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	/* Can't do much about it */
284*4882a593Smuzhiyun 	if (!cur_cpu_spec->oprofile_mmcra_sihv)
285*4882a593Smuzhiyun 		return pc;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	mmcra = mfspr(SPRN_MMCRA);
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	if (use_slot_nums && (mmcra & MMCRA_SAMPLE_ENABLE)) {
290*4882a593Smuzhiyun 		slot = ((mmcra & MMCRA_SLOT) >> MMCRA_SLOT_SHIFT);
291*4882a593Smuzhiyun 		if (slot > 1)
292*4882a593Smuzhiyun 			pc += 4 * (slot - 1);
293*4882a593Smuzhiyun 	}
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	/* Were we in the hypervisor? */
296*4882a593Smuzhiyun 	if (firmware_has_feature(FW_FEATURE_LPAR) &&
297*4882a593Smuzhiyun 	    (mmcra & cur_cpu_spec->oprofile_mmcra_sihv))
298*4882a593Smuzhiyun 		/* function descriptor madness */
299*4882a593Smuzhiyun 		return *((unsigned long *)hypervisor_bucket);
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	/* We were in userspace, nothing to do */
302*4882a593Smuzhiyun 	if (mmcra & cur_cpu_spec->oprofile_mmcra_sipr)
303*4882a593Smuzhiyun 		return pc;
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun #ifdef CONFIG_PPC_RTAS
306*4882a593Smuzhiyun 	/* Were we in RTAS? */
307*4882a593Smuzhiyun 	if (pc >= rtas.base && pc < (rtas.base + rtas.size))
308*4882a593Smuzhiyun 		/* function descriptor madness */
309*4882a593Smuzhiyun 		return *((unsigned long *)rtas_bucket);
310*4882a593Smuzhiyun #endif
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	/* Were we in our exception vectors or SLB real mode miss handler? */
313*4882a593Smuzhiyun 	if (pc < 0x1000000UL)
314*4882a593Smuzhiyun 		return (unsigned long)__va(pc);
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	/* Not sure where we were */
317*4882a593Smuzhiyun 	if (!is_kernel_addr(pc))
318*4882a593Smuzhiyun 		/* function descriptor madness */
319*4882a593Smuzhiyun 		return *((unsigned long *)kernel_unknown_bucket);
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	return pc;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun 
get_kernel(unsigned long pc,unsigned long mmcra)324*4882a593Smuzhiyun static int get_kernel(unsigned long pc, unsigned long mmcra)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	int is_kernel;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	if (!cur_cpu_spec->oprofile_mmcra_sihv) {
329*4882a593Smuzhiyun 		is_kernel = is_kernel_addr(pc);
330*4882a593Smuzhiyun 	} else {
331*4882a593Smuzhiyun 		is_kernel = ((mmcra & cur_cpu_spec->oprofile_mmcra_sipr) == 0);
332*4882a593Smuzhiyun 	}
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	return is_kernel;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun 
pmc_overflow(unsigned long val)337*4882a593Smuzhiyun static bool pmc_overflow(unsigned long val)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun 	if ((int)val < 0)
340*4882a593Smuzhiyun 		return true;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	/*
343*4882a593Smuzhiyun 	 * Events on POWER7 can roll back if a speculative event doesn't
344*4882a593Smuzhiyun 	 * eventually complete. Unfortunately in some rare cases they will
345*4882a593Smuzhiyun 	 * raise a performance monitor exception. We need to catch this to
346*4882a593Smuzhiyun 	 * ensure we reset the PMC. In all cases the PMC will be 256 or less
347*4882a593Smuzhiyun 	 * cycles from overflow.
348*4882a593Smuzhiyun 	 *
349*4882a593Smuzhiyun 	 * We only do this if the first pass fails to find any overflowing
350*4882a593Smuzhiyun 	 * PMCs because a user might set a period of less than 256 and we
351*4882a593Smuzhiyun 	 * don't want to mistakenly reset them.
352*4882a593Smuzhiyun 	 */
353*4882a593Smuzhiyun 	if (pvr_version_is(PVR_POWER7) && ((0x80000000 - val) <= 256))
354*4882a593Smuzhiyun 		return true;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	return false;
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun 
power4_handle_interrupt(struct pt_regs * regs,struct op_counter_config * ctr)359*4882a593Smuzhiyun static void power4_handle_interrupt(struct pt_regs *regs,
360*4882a593Smuzhiyun 				    struct op_counter_config *ctr)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun 	unsigned long pc;
363*4882a593Smuzhiyun 	int is_kernel;
364*4882a593Smuzhiyun 	int val;
365*4882a593Smuzhiyun 	int i;
366*4882a593Smuzhiyun 	unsigned int mmcr0;
367*4882a593Smuzhiyun 	unsigned long mmcra;
368*4882a593Smuzhiyun 	bool siar_valid = false;
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	mmcra = mfspr(SPRN_MMCRA);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	pc = get_pc(regs);
373*4882a593Smuzhiyun 	is_kernel = get_kernel(pc, mmcra);
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	/* set the PMM bit (see comment below) */
376*4882a593Smuzhiyun 	mtmsr(mfmsr() | MSR_PMM);
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	/* Check that the SIAR  valid bit in MMCRA is set to 1. */
379*4882a593Smuzhiyun 	if ((mmcra & MMCRA_SIAR_VALID_MASK) == MMCRA_SIAR_VALID_MASK)
380*4882a593Smuzhiyun 		siar_valid = true;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	for (i = 0; i < cur_cpu_spec->num_pmcs; ++i) {
383*4882a593Smuzhiyun 		val = classic_ctr_read(i);
384*4882a593Smuzhiyun 		if (pmc_overflow(val)) {
385*4882a593Smuzhiyun 			if (oprofile_running && ctr[i].enabled) {
386*4882a593Smuzhiyun 				/* Power 7+ and newer architectures:
387*4882a593Smuzhiyun 				 * If the event is a marked event, then only
388*4882a593Smuzhiyun 				 * save the sample if the SIAR valid bit is
389*4882a593Smuzhiyun 				 * set.  If the event is not marked, then
390*4882a593Smuzhiyun 				 * always save the sample.
391*4882a593Smuzhiyun 				 * Note, the Sample enable bit in the MMCRA
392*4882a593Smuzhiyun 				 * register must be set to 1 if the group
393*4882a593Smuzhiyun 				 * contains a marked event.
394*4882a593Smuzhiyun 				 */
395*4882a593Smuzhiyun 				if ((siar_valid &&
396*4882a593Smuzhiyun 				     (cntr_marked_events & (1 << i)))
397*4882a593Smuzhiyun 				    || !(cntr_marked_events & (1 << i)))
398*4882a593Smuzhiyun 					oprofile_add_ext_sample(pc, regs, i,
399*4882a593Smuzhiyun 								is_kernel);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 				classic_ctr_write(i, reset_value[i]);
402*4882a593Smuzhiyun 			} else {
403*4882a593Smuzhiyun 				classic_ctr_write(i, 0);
404*4882a593Smuzhiyun 			}
405*4882a593Smuzhiyun 		}
406*4882a593Smuzhiyun 	}
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	mmcr0 = mfspr(SPRN_MMCR0);
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	/* reset the perfmon trigger */
411*4882a593Smuzhiyun 	mmcr0 |= MMCR0_PMXE;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	/*
414*4882a593Smuzhiyun 	 * We must clear the PMAO bit on some (GQ) chips. Just do it
415*4882a593Smuzhiyun 	 * all the time
416*4882a593Smuzhiyun 	 */
417*4882a593Smuzhiyun 	mmcr0 &= ~MMCR0_PMAO;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	/* Clear the appropriate bits in the MMCRA */
420*4882a593Smuzhiyun 	mmcra &= ~cur_cpu_spec->oprofile_mmcra_clear;
421*4882a593Smuzhiyun 	mtspr(SPRN_MMCRA, mmcra);
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	/*
424*4882a593Smuzhiyun 	 * now clear the freeze bit, counting will not start until we
425*4882a593Smuzhiyun 	 * rfid from this exception, because only at that point will
426*4882a593Smuzhiyun 	 * the PMM bit be cleared
427*4882a593Smuzhiyun 	 */
428*4882a593Smuzhiyun 	mmcr0 &= ~MMCR0_FC;
429*4882a593Smuzhiyun 	mtspr(SPRN_MMCR0, mmcr0);
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun struct op_powerpc_model op_model_power4 = {
433*4882a593Smuzhiyun 	.reg_setup		= power4_reg_setup,
434*4882a593Smuzhiyun 	.cpu_setup		= power4_cpu_setup,
435*4882a593Smuzhiyun 	.start			= power4_start,
436*4882a593Smuzhiyun 	.stop			= power4_stop,
437*4882a593Smuzhiyun 	.handle_interrupt	= power4_handle_interrupt,
438*4882a593Smuzhiyun };
439