xref: /OK3568_Linux_fs/kernel/arch/powerpc/oprofile/common.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * PPC 64 oprofile support:
4*4882a593Smuzhiyun  * Copyright (C) 2004 Anton Blanchard <anton@au.ibm.com>, IBM
5*4882a593Smuzhiyun  * PPC 32 oprofile support: (based on PPC 64 support)
6*4882a593Smuzhiyun  * Copyright (C) Freescale Semiconductor, Inc 2004
7*4882a593Smuzhiyun  *	Author: Andy Fleming
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Based on alpha version.
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/oprofile.h>
13*4882a593Smuzhiyun #include <linux/init.h>
14*4882a593Smuzhiyun #include <linux/smp.h>
15*4882a593Smuzhiyun #include <linux/errno.h>
16*4882a593Smuzhiyun #include <asm/ptrace.h>
17*4882a593Smuzhiyun #include <asm/pmc.h>
18*4882a593Smuzhiyun #include <asm/cputable.h>
19*4882a593Smuzhiyun #include <asm/oprofile_impl.h>
20*4882a593Smuzhiyun #include <asm/firmware.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun static struct op_powerpc_model *model;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun static struct op_counter_config ctr[OP_MAX_COUNTER];
25*4882a593Smuzhiyun static struct op_system_config sys;
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun static int op_per_cpu_rc;
28*4882a593Smuzhiyun 
op_handle_interrupt(struct pt_regs * regs)29*4882a593Smuzhiyun static void op_handle_interrupt(struct pt_regs *regs)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	model->handle_interrupt(regs, ctr);
32*4882a593Smuzhiyun }
33*4882a593Smuzhiyun 
op_powerpc_cpu_setup(void * dummy)34*4882a593Smuzhiyun static void op_powerpc_cpu_setup(void *dummy)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	int ret;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	ret = model->cpu_setup(ctr);
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	if (ret != 0)
41*4882a593Smuzhiyun 		op_per_cpu_rc = ret;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
op_powerpc_setup(void)44*4882a593Smuzhiyun static int op_powerpc_setup(void)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	int err;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	op_per_cpu_rc = 0;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	/* Grab the hardware */
51*4882a593Smuzhiyun 	err = reserve_pmc_hardware(op_handle_interrupt);
52*4882a593Smuzhiyun 	if (err)
53*4882a593Smuzhiyun 		return err;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	/* Pre-compute the values to stuff in the hardware registers.  */
56*4882a593Smuzhiyun 	op_per_cpu_rc = model->reg_setup(ctr, &sys, model->num_counters);
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	if (op_per_cpu_rc)
59*4882a593Smuzhiyun 		goto out;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	/* Configure the registers on all cpus.	 If an error occurs on one
62*4882a593Smuzhiyun 	 * of the cpus, op_per_cpu_rc will be set to the error */
63*4882a593Smuzhiyun 	on_each_cpu(op_powerpc_cpu_setup, NULL, 1);
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun out:	if (op_per_cpu_rc) {
66*4882a593Smuzhiyun 		/* error on setup release the performance counter hardware */
67*4882a593Smuzhiyun 		release_pmc_hardware();
68*4882a593Smuzhiyun 	}
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	return op_per_cpu_rc;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
op_powerpc_shutdown(void)73*4882a593Smuzhiyun static void op_powerpc_shutdown(void)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	release_pmc_hardware();
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
op_powerpc_cpu_start(void * dummy)78*4882a593Smuzhiyun static void op_powerpc_cpu_start(void *dummy)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	/* If any of the cpus have return an error, set the
81*4882a593Smuzhiyun 	 * global flag to the error so it can be returned
82*4882a593Smuzhiyun 	 * to the generic OProfile caller.
83*4882a593Smuzhiyun 	 */
84*4882a593Smuzhiyun 	int ret;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	ret = model->start(ctr);
87*4882a593Smuzhiyun 	if (ret != 0)
88*4882a593Smuzhiyun 		op_per_cpu_rc = ret;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
op_powerpc_start(void)91*4882a593Smuzhiyun static int op_powerpc_start(void)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	op_per_cpu_rc = 0;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	if (model->global_start)
96*4882a593Smuzhiyun 		return model->global_start(ctr);
97*4882a593Smuzhiyun 	if (model->start) {
98*4882a593Smuzhiyun 		on_each_cpu(op_powerpc_cpu_start, NULL, 1);
99*4882a593Smuzhiyun 		return op_per_cpu_rc;
100*4882a593Smuzhiyun 	}
101*4882a593Smuzhiyun 	return -EIO; /* No start function is defined for this
102*4882a593Smuzhiyun 			power architecture */
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
op_powerpc_cpu_stop(void * dummy)105*4882a593Smuzhiyun static inline void op_powerpc_cpu_stop(void *dummy)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun 	model->stop();
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun 
op_powerpc_stop(void)110*4882a593Smuzhiyun static void op_powerpc_stop(void)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	if (model->stop)
113*4882a593Smuzhiyun 		on_each_cpu(op_powerpc_cpu_stop, NULL, 1);
114*4882a593Smuzhiyun         if (model->global_stop)
115*4882a593Smuzhiyun                 model->global_stop();
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
op_powerpc_create_files(struct dentry * root)118*4882a593Smuzhiyun static int op_powerpc_create_files(struct dentry *root)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	int i;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun #ifdef CONFIG_PPC64
123*4882a593Smuzhiyun 	/*
124*4882a593Smuzhiyun 	 * There is one mmcr0, mmcr1 and mmcra for setting the events for
125*4882a593Smuzhiyun 	 * all of the counters.
126*4882a593Smuzhiyun 	 */
127*4882a593Smuzhiyun 	oprofilefs_create_ulong(root, "mmcr0", &sys.mmcr0);
128*4882a593Smuzhiyun 	oprofilefs_create_ulong(root, "mmcr1", &sys.mmcr1);
129*4882a593Smuzhiyun 	oprofilefs_create_ulong(root, "mmcra", &sys.mmcra);
130*4882a593Smuzhiyun #ifdef CONFIG_OPROFILE_CELL
131*4882a593Smuzhiyun 	/* create a file the user tool can check to see what level of profiling
132*4882a593Smuzhiyun 	 * support exits with this kernel. Initialize bit mask to indicate
133*4882a593Smuzhiyun 	 * what support the kernel has:
134*4882a593Smuzhiyun 	 * bit 0      -  Supports SPU event profiling in addition to PPU
135*4882a593Smuzhiyun 	 *               event and cycles; and SPU cycle profiling
136*4882a593Smuzhiyun 	 * bits 1-31  -  Currently unused.
137*4882a593Smuzhiyun 	 *
138*4882a593Smuzhiyun 	 * If the file does not exist, then the kernel only supports SPU
139*4882a593Smuzhiyun 	 * cycle profiling, PPU event and cycle profiling.
140*4882a593Smuzhiyun 	 */
141*4882a593Smuzhiyun 	oprofilefs_create_ulong(root, "cell_support", &sys.cell_support);
142*4882a593Smuzhiyun 	sys.cell_support = 0x1; /* Note, the user OProfile tool must check
143*4882a593Smuzhiyun 				 * that this bit is set before attempting to
144*4882a593Smuzhiyun 				 * user SPU event profiling.  Older kernels
145*4882a593Smuzhiyun 				 * will not have this file, hence the user
146*4882a593Smuzhiyun 				 * tool is not allowed to do SPU event
147*4882a593Smuzhiyun 				 * profiling on older kernels.  Older kernels
148*4882a593Smuzhiyun 				 * will accept SPU events but collected data
149*4882a593Smuzhiyun 				 * is garbage.
150*4882a593Smuzhiyun 				 */
151*4882a593Smuzhiyun #endif
152*4882a593Smuzhiyun #endif
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	for (i = 0; i < model->num_counters; ++i) {
155*4882a593Smuzhiyun 		struct dentry *dir;
156*4882a593Smuzhiyun 		char buf[4];
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 		snprintf(buf, sizeof buf, "%d", i);
159*4882a593Smuzhiyun 		dir = oprofilefs_mkdir(root, buf);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 		oprofilefs_create_ulong(dir, "enabled", &ctr[i].enabled);
162*4882a593Smuzhiyun 		oprofilefs_create_ulong(dir, "event", &ctr[i].event);
163*4882a593Smuzhiyun 		oprofilefs_create_ulong(dir, "count", &ctr[i].count);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 		/*
166*4882a593Smuzhiyun 		 * Classic PowerPC doesn't support per-counter
167*4882a593Smuzhiyun 		 * control like this, but the options are
168*4882a593Smuzhiyun 		 * expected, so they remain.  For Freescale
169*4882a593Smuzhiyun 		 * Book-E style performance monitors, we do
170*4882a593Smuzhiyun 		 * support them.
171*4882a593Smuzhiyun 		 */
172*4882a593Smuzhiyun 		oprofilefs_create_ulong(dir, "kernel", &ctr[i].kernel);
173*4882a593Smuzhiyun 		oprofilefs_create_ulong(dir, "user", &ctr[i].user);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 		oprofilefs_create_ulong(dir, "unit_mask", &ctr[i].unit_mask);
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	oprofilefs_create_ulong(root, "enable_kernel", &sys.enable_kernel);
179*4882a593Smuzhiyun 	oprofilefs_create_ulong(root, "enable_user", &sys.enable_user);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	/* Default to tracing both kernel and user */
182*4882a593Smuzhiyun 	sys.enable_kernel = 1;
183*4882a593Smuzhiyun 	sys.enable_user = 1;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	return 0;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
oprofile_arch_init(struct oprofile_operations * ops)188*4882a593Smuzhiyun int __init oprofile_arch_init(struct oprofile_operations *ops)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun 	if (!cur_cpu_spec->oprofile_cpu_type)
191*4882a593Smuzhiyun 		return -ENODEV;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	switch (cur_cpu_spec->oprofile_type) {
194*4882a593Smuzhiyun #ifdef CONFIG_PPC_BOOK3S_64
195*4882a593Smuzhiyun #ifdef CONFIG_OPROFILE_CELL
196*4882a593Smuzhiyun 		case PPC_OPROFILE_CELL:
197*4882a593Smuzhiyun 			if (firmware_has_feature(FW_FEATURE_LPAR))
198*4882a593Smuzhiyun 				return -ENODEV;
199*4882a593Smuzhiyun 			model = &op_model_cell;
200*4882a593Smuzhiyun 			ops->sync_start = model->sync_start;
201*4882a593Smuzhiyun 			ops->sync_stop = model->sync_stop;
202*4882a593Smuzhiyun 			break;
203*4882a593Smuzhiyun #endif
204*4882a593Smuzhiyun 		case PPC_OPROFILE_POWER4:
205*4882a593Smuzhiyun 			model = &op_model_power4;
206*4882a593Smuzhiyun 			break;
207*4882a593Smuzhiyun 		case PPC_OPROFILE_PA6T:
208*4882a593Smuzhiyun 			model = &op_model_pa6t;
209*4882a593Smuzhiyun 			break;
210*4882a593Smuzhiyun #endif
211*4882a593Smuzhiyun #ifdef CONFIG_PPC_BOOK3S_32
212*4882a593Smuzhiyun 		case PPC_OPROFILE_G4:
213*4882a593Smuzhiyun 			model = &op_model_7450;
214*4882a593Smuzhiyun 			break;
215*4882a593Smuzhiyun #endif
216*4882a593Smuzhiyun #if defined(CONFIG_FSL_EMB_PERFMON)
217*4882a593Smuzhiyun 		case PPC_OPROFILE_FSL_EMB:
218*4882a593Smuzhiyun 			model = &op_model_fsl_emb;
219*4882a593Smuzhiyun 			break;
220*4882a593Smuzhiyun #endif
221*4882a593Smuzhiyun 		default:
222*4882a593Smuzhiyun 			return -ENODEV;
223*4882a593Smuzhiyun 	}
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	model->num_counters = cur_cpu_spec->num_pmcs;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	ops->cpu_type = cur_cpu_spec->oprofile_cpu_type;
228*4882a593Smuzhiyun 	ops->create_files = op_powerpc_create_files;
229*4882a593Smuzhiyun 	ops->setup = op_powerpc_setup;
230*4882a593Smuzhiyun 	ops->shutdown = op_powerpc_shutdown;
231*4882a593Smuzhiyun 	ops->start = op_powerpc_start;
232*4882a593Smuzhiyun 	ops->stop = op_powerpc_stop;
233*4882a593Smuzhiyun 	ops->backtrace = op_powerpc_backtrace;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	printk(KERN_DEBUG "oprofile: using %s performance monitoring.\n",
236*4882a593Smuzhiyun 	       ops->cpu_type);
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	return 0;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun 
oprofile_arch_exit(void)241*4882a593Smuzhiyun void oprofile_arch_exit(void)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun }
244