1
2
3
4
5
6
7
8
9
10
11
12
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 <asm/time.h>
20#include "pr_util.h"
21
22#define SCALE_SHIFT 14
23
24static u32 *samples;
25
26
27
28
29
30
31
32
33
34
35
36
37int spu_prof_running;
38static unsigned int profiling_interval;
39
40#define NUM_SPU_BITS_TRBUF 16
41#define SPUS_PER_TB_ENTRY 4
42
43#define SPU_PC_MASK 0xFFFF
44
45DEFINE_SPINLOCK(oprof_spu_smpl_arry_lck);
46unsigned long oprof_spu_smpl_arry_lck_flags;
47
48void set_spu_profiling_frequency(unsigned int freq_khz, unsigned int cycles_reset)
49{
50 unsigned long ns_per_cyc;
51
52 if (!freq_khz)
53 freq_khz = ppc_proc_freq/1000;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 ns_per_cyc = (USEC_PER_SEC << SCALE_SHIFT)/freq_khz;
70 profiling_interval = (ns_per_cyc * cycles_reset) >> SCALE_SHIFT;
71
72}
73
74
75
76
77static void spu_pc_extract(int cpu, int entry)
78{
79
80 u64 trace_buffer[2];
81 u64 spu_mask;
82 int spu;
83
84 spu_mask = SPU_PC_MASK;
85
86
87
88
89
90
91
92
93
94 cbe_read_trace_buffer(cpu, trace_buffer);
95
96 for (spu = SPUS_PER_TB_ENTRY-1; spu >= 0; spu--) {
97
98
99
100 samples[spu * TRACE_ARRAY_SIZE + entry]
101 = (spu_mask & trace_buffer[0]) << 2;
102 samples[(spu + SPUS_PER_TB_ENTRY) * TRACE_ARRAY_SIZE + entry]
103 = (spu_mask & trace_buffer[1]) << 2;
104
105 trace_buffer[0] = trace_buffer[0] >> NUM_SPU_BITS_TRBUF;
106 trace_buffer[1] = trace_buffer[1] >> NUM_SPU_BITS_TRBUF;
107 }
108}
109
110static int cell_spu_pc_collection(int cpu)
111{
112 u32 trace_addr;
113 int entry;
114
115
116
117 entry = 0;
118
119 trace_addr = cbe_read_pm(cpu, trace_address);
120 while (!(trace_addr & CBE_PM_TRACE_BUF_EMPTY)) {
121
122 spu_pc_extract(cpu, entry);
123
124 entry++;
125
126 if (entry >= TRACE_ARRAY_SIZE)
127
128 break;
129
130 trace_addr = cbe_read_pm(cpu, trace_address);
131 }
132
133 return entry;
134}
135
136
137static enum hrtimer_restart profile_spus(struct hrtimer *timer)
138{
139 ktime_t kt;
140 int cpu, node, k, num_samples, spu_num;
141
142 if (!spu_prof_running)
143 goto stop;
144
145 for_each_online_cpu(cpu) {
146 if (cbe_get_hw_thread_id(cpu))
147 continue;
148
149 node = cbe_cpu_to_node(cpu);
150
151
152
153
154
155
156
157
158
159 spin_lock_irqsave(&oprof_spu_smpl_arry_lck,
160 oprof_spu_smpl_arry_lck_flags);
161 num_samples = cell_spu_pc_collection(cpu);
162
163 if (num_samples == 0) {
164 spin_unlock_irqrestore(&oprof_spu_smpl_arry_lck,
165 oprof_spu_smpl_arry_lck_flags);
166 continue;
167 }
168
169 for (k = 0; k < SPUS_PER_NODE; k++) {
170 spu_num = k + (node * SPUS_PER_NODE);
171 spu_sync_buffer(spu_num,
172 samples + (k * TRACE_ARRAY_SIZE),
173 num_samples);
174 }
175
176 spin_unlock_irqrestore(&oprof_spu_smpl_arry_lck,
177 oprof_spu_smpl_arry_lck_flags);
178
179 }
180 smp_wmb();
181
182
183 kt = ktime_set(0, profiling_interval);
184 if (!spu_prof_running)
185 goto stop;
186 hrtimer_forward(timer, timer->base->get_time(), kt);
187 return HRTIMER_RESTART;
188
189 stop:
190 printk(KERN_INFO "SPU_PROF: spu-prof timer ending\n");
191 return HRTIMER_NORESTART;
192}
193
194static struct hrtimer timer;
195
196
197
198
199
200
201
202int start_spu_profiling_cycles(unsigned int cycles_reset)
203{
204 ktime_t kt;
205
206 pr_debug("timer resolution: %lu\n", TICK_NSEC);
207 kt = ktime_set(0, profiling_interval);
208 hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
209 hrtimer_set_expires(&timer, kt);
210 timer.function = profile_spus;
211
212
213 samples = kzalloc(SPUS_PER_NODE *
214 TRACE_ARRAY_SIZE * sizeof(u32), GFP_KERNEL);
215
216 if (!samples)
217 return -ENOMEM;
218
219 spu_prof_running = 1;
220 hrtimer_start(&timer, kt, HRTIMER_MODE_REL);
221 schedule_delayed_work(&spu_work, DEFAULT_TIMER_EXPIRE);
222
223 return 0;
224}
225
226
227
228
229
230
231
232
233void start_spu_profiling_events(void)
234{
235 spu_prof_running = 1;
236 schedule_delayed_work(&spu_work, DEFAULT_TIMER_EXPIRE);
237
238 return;
239}
240
241void stop_spu_profiling_cycles(void)
242{
243 spu_prof_running = 0;
244 hrtimer_cancel(&timer);
245 kfree(samples);
246 pr_debug("SPU_PROF: stop_spu_profiling_cycles issued\n");
247}
248
249void stop_spu_profiling_events(void)
250{
251 spu_prof_running = 0;
252}
253