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