xref: /OK3568_Linux_fs/kernel/arch/powerpc/oprofile/cell/spu_profiler.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Cell Broadband Engine OProfile Support
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * (C) Copyright IBM Corporation 2006
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Authors: Maynard Johnson <maynardj@us.ibm.com>
8*4882a593Smuzhiyun  *	    Carl Love <carll@us.ibm.com>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/hrtimer.h>
12*4882a593Smuzhiyun #include <linux/smp.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun #include <asm/cell-pmu.h>
15*4882a593Smuzhiyun #include <asm/time.h>
16*4882a593Smuzhiyun #include "pr_util.h"
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define SCALE_SHIFT 14
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun static u32 *samples;
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /* spu_prof_running is a flag used to indicate if spu profiling is enabled
23*4882a593Smuzhiyun  * or not.  It is set by the routines start_spu_profiling_cycles() and
24*4882a593Smuzhiyun  * start_spu_profiling_events().  The flag is cleared by the routines
25*4882a593Smuzhiyun  * stop_spu_profiling_cycles() and stop_spu_profiling_events().  These
26*4882a593Smuzhiyun  * routines are called via global_start() and global_stop() which are called in
27*4882a593Smuzhiyun  * op_powerpc_start() and op_powerpc_stop().  These routines are called once
28*4882a593Smuzhiyun  * per system as a result of the user starting/stopping oprofile.  Hence, only
29*4882a593Smuzhiyun  * one CPU per user at a time will be changing  the value of spu_prof_running.
30*4882a593Smuzhiyun  * In general, OProfile does not protect against multiple users trying to run
31*4882a593Smuzhiyun  * OProfile at a time.
32*4882a593Smuzhiyun  */
33*4882a593Smuzhiyun int spu_prof_running;
34*4882a593Smuzhiyun static unsigned int profiling_interval;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define NUM_SPU_BITS_TRBUF 16
37*4882a593Smuzhiyun #define SPUS_PER_TB_ENTRY   4
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #define SPU_PC_MASK	     0xFFFF
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun DEFINE_SPINLOCK(oprof_spu_smpl_arry_lck);
42*4882a593Smuzhiyun static unsigned long oprof_spu_smpl_arry_lck_flags;
43*4882a593Smuzhiyun 
set_spu_profiling_frequency(unsigned int freq_khz,unsigned int cycles_reset)44*4882a593Smuzhiyun void set_spu_profiling_frequency(unsigned int freq_khz, unsigned int cycles_reset)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	unsigned long ns_per_cyc;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	if (!freq_khz)
49*4882a593Smuzhiyun 		freq_khz = ppc_proc_freq/1000;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	/* To calculate a timeout in nanoseconds, the basic
52*4882a593Smuzhiyun 	 * formula is ns = cycles_reset * (NSEC_PER_SEC / cpu frequency).
53*4882a593Smuzhiyun 	 * To avoid floating point math, we use the scale math
54*4882a593Smuzhiyun 	 * technique as described in linux/jiffies.h.  We use
55*4882a593Smuzhiyun 	 * a scale factor of SCALE_SHIFT, which provides 4 decimal places
56*4882a593Smuzhiyun 	 * of precision.  This is close enough for the purpose at hand.
57*4882a593Smuzhiyun 	 *
58*4882a593Smuzhiyun 	 * The value of the timeout should be small enough that the hw
59*4882a593Smuzhiyun 	 * trace buffer will not get more than about 1/3 full for the
60*4882a593Smuzhiyun 	 * maximum user specified (the LFSR value) hw sampling frequency.
61*4882a593Smuzhiyun 	 * This is to ensure the trace buffer will never fill even if the
62*4882a593Smuzhiyun 	 * kernel thread scheduling varies under a heavy system load.
63*4882a593Smuzhiyun 	 */
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	ns_per_cyc = (USEC_PER_SEC << SCALE_SHIFT)/freq_khz;
66*4882a593Smuzhiyun 	profiling_interval = (ns_per_cyc * cycles_reset) >> SCALE_SHIFT;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /*
71*4882a593Smuzhiyun  * Extract SPU PC from trace buffer entry
72*4882a593Smuzhiyun  */
spu_pc_extract(int cpu,int entry)73*4882a593Smuzhiyun static void spu_pc_extract(int cpu, int entry)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	/* the trace buffer is 128 bits */
76*4882a593Smuzhiyun 	u64 trace_buffer[2];
77*4882a593Smuzhiyun 	u64 spu_mask;
78*4882a593Smuzhiyun 	int spu;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	spu_mask = SPU_PC_MASK;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	/* Each SPU PC is 16 bits; hence, four spus in each of
83*4882a593Smuzhiyun 	 * the two 64-bit buffer entries that make up the
84*4882a593Smuzhiyun 	 * 128-bit trace_buffer entry.	Process two 64-bit values
85*4882a593Smuzhiyun 	 * simultaneously.
86*4882a593Smuzhiyun 	 * trace[0] SPU PC contents are: 0 1 2 3
87*4882a593Smuzhiyun 	 * trace[1] SPU PC contents are: 4 5 6 7
88*4882a593Smuzhiyun 	 */
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	cbe_read_trace_buffer(cpu, trace_buffer);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	for (spu = SPUS_PER_TB_ENTRY-1; spu >= 0; spu--) {
93*4882a593Smuzhiyun 		/* spu PC trace entry is upper 16 bits of the
94*4882a593Smuzhiyun 		 * 18 bit SPU program counter
95*4882a593Smuzhiyun 		 */
96*4882a593Smuzhiyun 		samples[spu * TRACE_ARRAY_SIZE + entry]
97*4882a593Smuzhiyun 			= (spu_mask & trace_buffer[0]) << 2;
98*4882a593Smuzhiyun 		samples[(spu + SPUS_PER_TB_ENTRY) * TRACE_ARRAY_SIZE + entry]
99*4882a593Smuzhiyun 			= (spu_mask & trace_buffer[1]) << 2;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 		trace_buffer[0] = trace_buffer[0] >> NUM_SPU_BITS_TRBUF;
102*4882a593Smuzhiyun 		trace_buffer[1] = trace_buffer[1] >> NUM_SPU_BITS_TRBUF;
103*4882a593Smuzhiyun 	}
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun 
cell_spu_pc_collection(int cpu)106*4882a593Smuzhiyun static int cell_spu_pc_collection(int cpu)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	u32 trace_addr;
109*4882a593Smuzhiyun 	int entry;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	/* process the collected SPU PC for the node */
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	entry = 0;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	trace_addr = cbe_read_pm(cpu, trace_address);
116*4882a593Smuzhiyun 	while (!(trace_addr & CBE_PM_TRACE_BUF_EMPTY)) {
117*4882a593Smuzhiyun 		/* there is data in the trace buffer to process */
118*4882a593Smuzhiyun 		spu_pc_extract(cpu, entry);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 		entry++;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 		if (entry >= TRACE_ARRAY_SIZE)
123*4882a593Smuzhiyun 			/* spu_samples is full */
124*4882a593Smuzhiyun 			break;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 		trace_addr = cbe_read_pm(cpu, trace_address);
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	return entry;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 
profile_spus(struct hrtimer * timer)133*4882a593Smuzhiyun static enum hrtimer_restart profile_spus(struct hrtimer *timer)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	ktime_t kt;
136*4882a593Smuzhiyun 	int cpu, node, k, num_samples, spu_num;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	if (!spu_prof_running)
139*4882a593Smuzhiyun 		goto stop;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	for_each_online_cpu(cpu) {
142*4882a593Smuzhiyun 		if (cbe_get_hw_thread_id(cpu))
143*4882a593Smuzhiyun 			continue;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 		node = cbe_cpu_to_node(cpu);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 		/* There should only be one kernel thread at a time processing
148*4882a593Smuzhiyun 		 * the samples.	 In the very unlikely case that the processing
149*4882a593Smuzhiyun 		 * is taking a very long time and multiple kernel threads are
150*4882a593Smuzhiyun 		 * started to process the samples.  Make sure only one kernel
151*4882a593Smuzhiyun 		 * thread is working on the samples array at a time.  The
152*4882a593Smuzhiyun 		 * sample array must be loaded and then processed for a given
153*4882a593Smuzhiyun 		 * cpu.	 The sample array is not per cpu.
154*4882a593Smuzhiyun 		 */
155*4882a593Smuzhiyun 		spin_lock_irqsave(&oprof_spu_smpl_arry_lck,
156*4882a593Smuzhiyun 				  oprof_spu_smpl_arry_lck_flags);
157*4882a593Smuzhiyun 		num_samples = cell_spu_pc_collection(cpu);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 		if (num_samples == 0) {
160*4882a593Smuzhiyun 			spin_unlock_irqrestore(&oprof_spu_smpl_arry_lck,
161*4882a593Smuzhiyun 					       oprof_spu_smpl_arry_lck_flags);
162*4882a593Smuzhiyun 			continue;
163*4882a593Smuzhiyun 		}
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 		for (k = 0; k < SPUS_PER_NODE; k++) {
166*4882a593Smuzhiyun 			spu_num = k + (node * SPUS_PER_NODE);
167*4882a593Smuzhiyun 			spu_sync_buffer(spu_num,
168*4882a593Smuzhiyun 					samples + (k * TRACE_ARRAY_SIZE),
169*4882a593Smuzhiyun 					num_samples);
170*4882a593Smuzhiyun 		}
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 		spin_unlock_irqrestore(&oprof_spu_smpl_arry_lck,
173*4882a593Smuzhiyun 				       oprof_spu_smpl_arry_lck_flags);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 	smp_wmb();	/* insure spu event buffer updates are written */
177*4882a593Smuzhiyun 			/* don't want events intermingled... */
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	kt = profiling_interval;
180*4882a593Smuzhiyun 	if (!spu_prof_running)
181*4882a593Smuzhiyun 		goto stop;
182*4882a593Smuzhiyun 	hrtimer_forward(timer, timer->base->get_time(), kt);
183*4882a593Smuzhiyun 	return HRTIMER_RESTART;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun  stop:
186*4882a593Smuzhiyun 	printk(KERN_INFO "SPU_PROF: spu-prof timer ending\n");
187*4882a593Smuzhiyun 	return HRTIMER_NORESTART;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun static struct hrtimer timer;
191*4882a593Smuzhiyun /*
192*4882a593Smuzhiyun  * Entry point for SPU cycle profiling.
193*4882a593Smuzhiyun  * NOTE:  SPU profiling is done system-wide, not per-CPU.
194*4882a593Smuzhiyun  *
195*4882a593Smuzhiyun  * cycles_reset is the count value specified by the user when
196*4882a593Smuzhiyun  * setting up OProfile to count SPU_CYCLES.
197*4882a593Smuzhiyun  */
start_spu_profiling_cycles(unsigned int cycles_reset)198*4882a593Smuzhiyun int start_spu_profiling_cycles(unsigned int cycles_reset)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun 	ktime_t kt;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	pr_debug("timer resolution: %lu\n", TICK_NSEC);
203*4882a593Smuzhiyun 	kt = profiling_interval;
204*4882a593Smuzhiyun 	hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
205*4882a593Smuzhiyun 	hrtimer_set_expires(&timer, kt);
206*4882a593Smuzhiyun 	timer.function = profile_spus;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	/* Allocate arrays for collecting SPU PC samples */
209*4882a593Smuzhiyun 	samples = kcalloc(SPUS_PER_NODE * TRACE_ARRAY_SIZE, sizeof(u32),
210*4882a593Smuzhiyun 			  GFP_KERNEL);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	if (!samples)
213*4882a593Smuzhiyun 		return -ENOMEM;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	spu_prof_running = 1;
216*4882a593Smuzhiyun 	hrtimer_start(&timer, kt, HRTIMER_MODE_REL);
217*4882a593Smuzhiyun 	schedule_delayed_work(&spu_work, DEFAULT_TIMER_EXPIRE);
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	return 0;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun /*
223*4882a593Smuzhiyun  * Entry point for SPU event profiling.
224*4882a593Smuzhiyun  * NOTE:  SPU profiling is done system-wide, not per-CPU.
225*4882a593Smuzhiyun  *
226*4882a593Smuzhiyun  * cycles_reset is the count value specified by the user when
227*4882a593Smuzhiyun  * setting up OProfile to count SPU_CYCLES.
228*4882a593Smuzhiyun  */
start_spu_profiling_events(void)229*4882a593Smuzhiyun void start_spu_profiling_events(void)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	spu_prof_running = 1;
232*4882a593Smuzhiyun 	schedule_delayed_work(&spu_work, DEFAULT_TIMER_EXPIRE);
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	return;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
stop_spu_profiling_cycles(void)237*4882a593Smuzhiyun void stop_spu_profiling_cycles(void)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	spu_prof_running = 0;
240*4882a593Smuzhiyun 	hrtimer_cancel(&timer);
241*4882a593Smuzhiyun 	kfree(samples);
242*4882a593Smuzhiyun 	pr_debug("SPU_PROF: stop_spu_profiling_cycles issued\n");
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun 
stop_spu_profiling_events(void)245*4882a593Smuzhiyun void stop_spu_profiling_events(void)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun 	spu_prof_running = 0;
248*4882a593Smuzhiyun }
249