- Update to 2.6.25-rc3.
[linux-flexiantxendom0-3.2.10.git] / arch / powerpc / oprofile / cell / spu_profiler.c
1 /*
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 <asm/time.h>
20 #include "pr_util.h"
21
22 #define TRACE_ARRAY_SIZE 1024
23 #define SCALE_SHIFT 14
24
25 static u32 *samples;
26
27 static int spu_prof_running;
28 static unsigned int profiling_interval;
29
30 #define NUM_SPU_BITS_TRBUF 16
31 #define SPUS_PER_TB_ENTRY   4
32 #define SPUS_PER_NODE        8
33
34 #define SPU_PC_MASK          0xFFFF
35
36 static DEFINE_SPINLOCK(sample_array_lock);
37 unsigned long sample_array_lock_flags;
38
39 void set_spu_profiling_frequency(unsigned int freq_khz, unsigned int cycles_reset)
40 {
41         unsigned long ns_per_cyc;
42
43         if (!freq_khz)
44                 freq_khz = ppc_proc_freq/1000;
45
46         /* To calculate a timeout in nanoseconds, the basic
47          * formula is ns = cycles_reset * (NSEC_PER_SEC / cpu frequency).
48          * To avoid floating point math, we use the scale math
49          * technique as described in linux/jiffies.h.  We use
50          * a scale factor of SCALE_SHIFT, which provides 4 decimal places
51          * of precision.  This is close enough for the purpose at hand.
52          *
53          * The value of the timeout should be small enough that the hw
54          * trace buffer will not get more then about 1/3 full for the
55          * maximum user specified (the LFSR value) hw sampling frequency.
56          * This is to ensure the trace buffer will never fill even if the
57          * kernel thread scheduling varies under a heavy system load.
58          */
59
60         ns_per_cyc = (USEC_PER_SEC << SCALE_SHIFT)/freq_khz;
61         profiling_interval = (ns_per_cyc * cycles_reset) >> SCALE_SHIFT;
62
63 }
64
65 /*
66  * Extract SPU PC from trace buffer entry
67  */
68 static void spu_pc_extract(int cpu, int entry)
69 {
70         /* the trace buffer is 128 bits */
71         u64 trace_buffer[2];
72         u64 spu_mask;
73         int spu;
74
75         spu_mask = SPU_PC_MASK;
76
77         /* Each SPU PC is 16 bits; hence, four spus in each of
78          * the two 64-bit buffer entries that make up the
79          * 128-bit trace_buffer entry.  Process two 64-bit values
80          * simultaneously.
81          * trace[0] SPU PC contents are: 0 1 2 3
82          * trace[1] SPU PC contents are: 4 5 6 7
83          */
84
85         cbe_read_trace_buffer(cpu, trace_buffer);
86
87         for (spu = SPUS_PER_TB_ENTRY-1; spu >= 0; spu--) {
88                 /* spu PC trace entry is upper 16 bits of the
89                  * 18 bit SPU program counter
90                  */
91                 samples[spu * TRACE_ARRAY_SIZE + entry]
92                         = (spu_mask & trace_buffer[0]) << 2;
93                 samples[(spu + SPUS_PER_TB_ENTRY) * TRACE_ARRAY_SIZE + entry]
94                         = (spu_mask & trace_buffer[1]) << 2;
95
96                 trace_buffer[0] = trace_buffer[0] >> NUM_SPU_BITS_TRBUF;
97                 trace_buffer[1] = trace_buffer[1] >> NUM_SPU_BITS_TRBUF;
98         }
99 }
100
101 static int cell_spu_pc_collection(int cpu)
102 {
103         u32 trace_addr;
104         int entry;
105
106         /* process the collected SPU PC for the node */
107
108         entry = 0;
109
110         trace_addr = cbe_read_pm(cpu, trace_address);
111         while (!(trace_addr & CBE_PM_TRACE_BUF_EMPTY)) {
112                 /* there is data in the trace buffer to process */
113                 spu_pc_extract(cpu, entry);
114
115                 entry++;
116
117                 if (entry >= TRACE_ARRAY_SIZE)
118                         /* spu_samples is full */
119                         break;
120
121                 trace_addr = cbe_read_pm(cpu, trace_address);
122         }
123
124         return entry;
125 }
126
127
128 static enum hrtimer_restart profile_spus(struct hrtimer *timer)
129 {
130         ktime_t kt;
131         int cpu, node, k, num_samples, spu_num;
132
133         if (!spu_prof_running)
134                 goto stop;
135
136         for_each_online_cpu(cpu) {
137                 if (cbe_get_hw_thread_id(cpu))
138                         continue;
139
140                 node = cbe_cpu_to_node(cpu);
141
142                 /* There should only be one kernel thread at a time processing
143                  * the samples.  In the very unlikely case that the processing
144                  * is taking a very long time and multiple kernel threads are
145                  * started to process the samples.  Make sure only one kernel
146                  * thread is working on the samples array at a time.  The
147                  * sample array must be loaded and then processed for a given
148                  * cpu.  The sample array is not per cpu.
149                  */
150                 spin_lock_irqsave(&sample_array_lock,
151                                   sample_array_lock_flags);
152                 num_samples = cell_spu_pc_collection(cpu);
153
154                 if (num_samples == 0) {
155                         spin_unlock_irqrestore(&sample_array_lock,
156                                                sample_array_lock_flags);
157                         continue;
158                 }
159
160                 for (k = 0; k < SPUS_PER_NODE; k++) {
161                         spu_num = k + (node * SPUS_PER_NODE);
162                         spu_sync_buffer(spu_num,
163                                         samples + (k * TRACE_ARRAY_SIZE),
164                                         num_samples);
165                 }
166
167                 spin_unlock_irqrestore(&sample_array_lock,
168                                        sample_array_lock_flags);
169
170         }
171         smp_wmb();      /* insure spu event buffer updates are written */
172                         /* don't want events intermingled... */
173
174         kt = ktime_set(0, profiling_interval);
175         if (!spu_prof_running)
176                 goto stop;
177         hrtimer_forward(timer, timer->base->get_time(), kt);
178         return HRTIMER_RESTART;
179
180  stop:
181         printk(KERN_INFO "SPU_PROF: spu-prof timer ending\n");
182         return HRTIMER_NORESTART;
183 }
184
185 static struct hrtimer timer;
186 /*
187  * Entry point for SPU profiling.
188  * NOTE:  SPU profiling is done system-wide, not per-CPU.
189  *
190  * cycles_reset is the count value specified by the user when
191  * setting up OProfile to count SPU_CYCLES.
192  */
193 int start_spu_profiling(unsigned int cycles_reset)
194 {
195         ktime_t kt;
196
197         pr_debug("timer resolution: %lu\n", TICK_NSEC);
198         kt = ktime_set(0, profiling_interval);
199         hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
200         timer.expires = kt;
201         timer.function = profile_spus;
202
203         /* Allocate arrays for collecting SPU PC samples */
204         samples = kzalloc(SPUS_PER_NODE *
205                           TRACE_ARRAY_SIZE * sizeof(u32), GFP_KERNEL);
206
207         if (!samples)
208                 return -ENOMEM;
209
210         spu_prof_running = 1;
211         hrtimer_start(&timer, kt, HRTIMER_MODE_REL);
212
213         return 0;
214 }
215
216 void stop_spu_profiling(void)
217 {
218         spu_prof_running = 0;
219         hrtimer_cancel(&timer);
220         kfree(samples);
221         pr_debug("SPU_PROF: stop_spu_profiling issued\n");
222 }