blob: ff96cbfb89bb9515074701c97118ce3dd821475a [file] [log] [blame]
Maynard Johnson18f21902006-11-20 18:45:16 +01001/*
2 * Cell Broadband Engine OProfile Support
3 *
4 * (C) Copyright IBM Corporation 2006
5 *
6 * Author: David Erb (djerb@us.ibm.com)
7 * Modifications:
Bob Nelson14748552007-07-20 21:39:53 +02008 * Carl Love <carll@us.ibm.com>
9 * Maynard Johnson <maynardj@us.ibm.com>
Maynard Johnson18f21902006-11-20 18:45:16 +010010 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 */
16
17#include <linux/cpufreq.h>
18#include <linux/delay.h>
19#include <linux/init.h>
20#include <linux/jiffies.h>
21#include <linux/kthread.h>
22#include <linux/oprofile.h>
23#include <linux/percpu.h>
24#include <linux/smp.h>
25#include <linux/spinlock.h>
26#include <linux/timer.h>
27#include <asm/cell-pmu.h>
28#include <asm/cputable.h>
29#include <asm/firmware.h>
30#include <asm/io.h>
31#include <asm/oprofile_impl.h>
32#include <asm/processor.h>
33#include <asm/prom.h>
34#include <asm/ptrace.h>
35#include <asm/reg.h>
36#include <asm/rtas.h>
37#include <asm/system.h>
Benjamin Herrenschmidteef686a02007-10-04 15:40:42 +100038#include <asm/cell-regs.h>
Maynard Johnson18f21902006-11-20 18:45:16 +010039
40#include "../platforms/cell/interrupt.h"
Bob Nelson14748552007-07-20 21:39:53 +020041#include "cell/pr_util.h"
42
Carl Love9b934182008-12-01 16:18:34 -080043#define PPU_PROFILING 0
44#define SPU_PROFILING_CYCLES 1
45#define SPU_PROFILING_EVENTS 2
Bob Nelson14748552007-07-20 21:39:53 +020046
Carl Love88382322008-12-01 16:18:36 -080047#define SPU_EVENT_NUM_START 4100
48#define SPU_EVENT_NUM_STOP 4399
49#define SPU_PROFILE_EVENT_ADDR 4363 /* spu, address trace, decimal */
50#define SPU_PROFILE_EVENT_ADDR_MASK_A 0x146 /* sub unit set to zero */
51#define SPU_PROFILE_EVENT_ADDR_MASK_B 0x186 /* sub unit set to zero */
52
Bob Nelson14748552007-07-20 21:39:53 +020053#define NUM_SPUS_PER_NODE 8
54#define SPU_CYCLES_EVENT_NUM 2 /* event number for SPU_CYCLES */
Maynard Johnson18f21902006-11-20 18:45:16 +010055
56#define PPU_CYCLES_EVENT_NUM 1 /* event number for CYCLES */
Bob Nelson14748552007-07-20 21:39:53 +020057#define PPU_CYCLES_GRP_NUM 1 /* special group number for identifying
58 * PPU_CYCLES event
59 */
60#define CBE_COUNT_ALL_CYCLES 0x42800000 /* PPU cycle event specifier */
Maynard Johnson18f21902006-11-20 18:45:16 +010061
Carl Lovebcb63e22007-02-13 22:02:02 +010062#define NUM_THREADS 2 /* number of physical threads in
63 * physical processor
64 */
Bob Nelsona1ef4842007-08-17 11:06:09 -050065#define NUM_DEBUG_BUS_WORDS 4
Carl Lovebcb63e22007-02-13 22:02:02 +010066#define NUM_INPUT_BUS_WORDS 2
67
Bob Nelson14748552007-07-20 21:39:53 +020068#define MAX_SPU_COUNT 0xFFFFFF /* maximum 24 bit LFSR value */
Maynard Johnson18f21902006-11-20 18:45:16 +010069
Carl Love88382322008-12-01 16:18:36 -080070/* Minumum HW interval timer setting to send value to trace buffer is 10 cycle.
71 * To configure counter to send value every N cycles set counter to
72 * 2^32 - 1 - N.
73 */
74#define NUM_INTERVAL_CYC 0xFFFFFFFF - 10
75
Carl Love9b934182008-12-01 16:18:34 -080076/*
77 * spu_cycle_reset is the number of cycles between samples.
78 * This variable is used for SPU profiling and should ONLY be set
79 * at the beginning of cell_reg_setup; otherwise, it's read-only.
80 */
81static unsigned int spu_cycle_reset;
82static unsigned int profiling_mode;
Carl Love88382322008-12-01 16:18:36 -080083static int spu_evnt_phys_spu_indx;
Carl Love9b934182008-12-01 16:18:34 -080084
Maynard Johnson18f21902006-11-20 18:45:16 +010085struct pmc_cntrl_data {
86 unsigned long vcntr;
87 unsigned long evnts;
88 unsigned long masks;
89 unsigned long enabled;
90};
91
92/*
93 * ibm,cbe-perftools rtas parameters
94 */
Maynard Johnson18f21902006-11-20 18:45:16 +010095struct pm_signal {
96 u16 cpu; /* Processor to modify */
Bob Nelson14748552007-07-20 21:39:53 +020097 u16 sub_unit; /* hw subunit this applies to (if applicable)*/
98 short int signal_group; /* Signal Group to Enable/Disable */
Maynard Johnson18f21902006-11-20 18:45:16 +010099 u8 bus_word; /* Enable/Disable on this Trace/Trigger/Event
100 * Bus Word(s) (bitmask)
101 */
102 u8 bit; /* Trigger/Event bit (if applicable) */
103};
104
105/*
106 * rtas call arguments
107 */
108enum {
109 SUBFUNC_RESET = 1,
110 SUBFUNC_ACTIVATE = 2,
111 SUBFUNC_DEACTIVATE = 3,
112
113 PASSTHRU_IGNORE = 0,
114 PASSTHRU_ENABLE = 1,
115 PASSTHRU_DISABLE = 2,
116};
117
118struct pm_cntrl {
119 u16 enable;
120 u16 stop_at_max;
121 u16 trace_mode;
122 u16 freeze;
123 u16 count_mode;
Carl Love88382322008-12-01 16:18:36 -0800124 u16 spu_addr_trace;
125 u8 trace_buf_ovflw;
Maynard Johnson18f21902006-11-20 18:45:16 +0100126};
127
128static struct {
129 u32 group_control;
130 u32 debug_bus_control;
131 struct pm_cntrl pm_cntrl;
132 u32 pm07_cntrl[NR_PHYS_CTRS];
133} pm_regs;
134
Maynard Johnson18f21902006-11-20 18:45:16 +0100135#define GET_SUB_UNIT(x) ((x & 0x0000f000) >> 12)
136#define GET_BUS_WORD(x) ((x & 0x000000f0) >> 4)
137#define GET_BUS_TYPE(x) ((x & 0x00000300) >> 8)
138#define GET_POLARITY(x) ((x & 0x00000002) >> 1)
139#define GET_COUNT_CYCLES(x) (x & 0x00000001)
140#define GET_INPUT_CONTROL(x) ((x & 0x00000004) >> 2)
141
Maynard Johnson18f21902006-11-20 18:45:16 +0100142static DEFINE_PER_CPU(unsigned long[NR_PHYS_CTRS], pmc_values);
Carl Love88382322008-12-01 16:18:36 -0800143static unsigned long spu_pm_cnt[MAX_NUMNODES * NUM_SPUS_PER_NODE];
Maynard Johnson18f21902006-11-20 18:45:16 +0100144static struct pmc_cntrl_data pmc_cntrl[NUM_THREADS][NR_PHYS_CTRS];
145
Bob Nelson14748552007-07-20 21:39:53 +0200146/*
147 * The CELL profiling code makes rtas calls to setup the debug bus to
148 * route the performance signals. Additionally, SPU profiling requires
149 * a second rtas call to setup the hardware to capture the SPU PCs.
150 * The EIO error value is returned if the token lookups or the rtas
151 * call fail. The EIO error number is the best choice of the existing
152 * error numbers. The probability of rtas related error is very low. But
153 * by returning EIO and printing additional information to dmsg the user
154 * will know that OProfile did not start and dmesg will tell them why.
155 * OProfile does not support returning errors on Stop. Not a huge issue
156 * since failure to reset the debug bus or stop the SPU PC collection is
157 * not a fatel issue. Chances are if the Stop failed, Start doesn't work
158 * either.
159 */
160
161/*
162 * Interpetation of hdw_thread:
Maynard Johnson18f21902006-11-20 18:45:16 +0100163 * 0 - even virtual cpus 0, 2, 4,...
164 * 1 - odd virtual cpus 1, 3, 5, ...
Bob Nelson14748552007-07-20 21:39:53 +0200165 *
166 * FIXME: this is strictly wrong, we need to clean this up in a number
167 * of places. It works for now. -arnd
Maynard Johnson18f21902006-11-20 18:45:16 +0100168 */
169static u32 hdw_thread;
170
171static u32 virt_cntr_inter_mask;
172static struct timer_list timer_virt_cntr;
Carl Love88382322008-12-01 16:18:36 -0800173static struct timer_list timer_spu_event_swap;
Maynard Johnson18f21902006-11-20 18:45:16 +0100174
Bob Nelson14748552007-07-20 21:39:53 +0200175/*
176 * pm_signal needs to be global since it is initialized in
Maynard Johnson18f21902006-11-20 18:45:16 +0100177 * cell_reg_setup at the time when the necessary information
178 * is available.
179 */
180static struct pm_signal pm_signal[NR_PHYS_CTRS];
Bob Nelson14748552007-07-20 21:39:53 +0200181static int pm_rtas_token; /* token for debug bus setup call */
182static int spu_rtas_token; /* token for SPU cycle profiling */
Maynard Johnson18f21902006-11-20 18:45:16 +0100183
184static u32 reset_value[NR_PHYS_CTRS];
185static int num_counters;
186static int oprofile_running;
Carl Love9b934182008-12-01 16:18:34 -0800187static DEFINE_SPINLOCK(cntr_lock);
Maynard Johnson18f21902006-11-20 18:45:16 +0100188
189static u32 ctr_enabled;
190
Carl Lovebcb63e22007-02-13 22:02:02 +0100191static unsigned char input_bus[NUM_INPUT_BUS_WORDS];
Maynard Johnson18f21902006-11-20 18:45:16 +0100192
193/*
194 * Firmware interface functions
195 */
196static int
197rtas_ibm_cbe_perftools(int subfunc, int passthru,
198 void *address, unsigned long length)
199{
200 u64 paddr = __pa(address);
201
Bob Nelson14748552007-07-20 21:39:53 +0200202 return rtas_call(pm_rtas_token, 5, 1, NULL, subfunc,
203 passthru, paddr >> 32, paddr & 0xffffffff, length);
Maynard Johnson18f21902006-11-20 18:45:16 +0100204}
205
206static void pm_rtas_reset_signals(u32 node)
207{
208 int ret;
209 struct pm_signal pm_signal_local;
210
Bob Nelson14748552007-07-20 21:39:53 +0200211 /*
212 * The debug bus is being set to the passthru disable state.
213 * However, the FW still expects atleast one legal signal routing
214 * entry or it will return an error on the arguments. If we don't
215 * supply a valid entry, we must ignore all return values. Ignoring
216 * all return values means we might miss an error we should be
217 * concerned about.
Maynard Johnson18f21902006-11-20 18:45:16 +0100218 */
219
220 /* fw expects physical cpu #. */
221 pm_signal_local.cpu = node;
222 pm_signal_local.signal_group = 21;
223 pm_signal_local.bus_word = 1;
224 pm_signal_local.sub_unit = 0;
225 pm_signal_local.bit = 0;
226
227 ret = rtas_ibm_cbe_perftools(SUBFUNC_RESET, PASSTHRU_DISABLE,
228 &pm_signal_local,
229 sizeof(struct pm_signal));
230
Bob Nelson14748552007-07-20 21:39:53 +0200231 if (unlikely(ret))
232 /*
233 * Not a fatal error. For Oprofile stop, the oprofile
234 * functions do not support returning an error for
235 * failure to stop OProfile.
236 */
Maynard Johnson18f21902006-11-20 18:45:16 +0100237 printk(KERN_WARNING "%s: rtas returned: %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100238 __func__, ret);
Maynard Johnson18f21902006-11-20 18:45:16 +0100239}
240
Bob Nelson14748552007-07-20 21:39:53 +0200241static int pm_rtas_activate_signals(u32 node, u32 count)
Maynard Johnson18f21902006-11-20 18:45:16 +0100242{
243 int ret;
Maynard Johnsonc7eb7342007-02-13 22:02:03 +0100244 int i, j;
Maynard Johnson18f21902006-11-20 18:45:16 +0100245 struct pm_signal pm_signal_local[NR_PHYS_CTRS];
246
Bob Nelson14748552007-07-20 21:39:53 +0200247 /*
248 * There is no debug setup required for the cycles event.
Maynard Johnsonc7eb7342007-02-13 22:02:03 +0100249 * Note that only events in the same group can be used.
250 * Otherwise, there will be conflicts in correctly routing
251 * the signals on the debug bus. It is the responsiblity
252 * of the OProfile user tool to check the events are in
253 * the same group.
254 */
255 i = 0;
Maynard Johnson18f21902006-11-20 18:45:16 +0100256 for (j = 0; j < count; j++) {
Maynard Johnsonc7eb7342007-02-13 22:02:03 +0100257 if (pm_signal[j].signal_group != PPU_CYCLES_GRP_NUM) {
258
259 /* fw expects physical cpu # */
260 pm_signal_local[i].cpu = node;
261 pm_signal_local[i].signal_group
262 = pm_signal[j].signal_group;
263 pm_signal_local[i].bus_word = pm_signal[j].bus_word;
264 pm_signal_local[i].sub_unit = pm_signal[j].sub_unit;
265 pm_signal_local[i].bit = pm_signal[j].bit;
266 i++;
267 }
Maynard Johnson18f21902006-11-20 18:45:16 +0100268 }
269
Maynard Johnsonc7eb7342007-02-13 22:02:03 +0100270 if (i != 0) {
271 ret = rtas_ibm_cbe_perftools(SUBFUNC_ACTIVATE, PASSTHRU_ENABLE,
272 pm_signal_local,
273 i * sizeof(struct pm_signal));
Maynard Johnson18f21902006-11-20 18:45:16 +0100274
Bob Nelson14748552007-07-20 21:39:53 +0200275 if (unlikely(ret)) {
Maynard Johnsonc7eb7342007-02-13 22:02:03 +0100276 printk(KERN_WARNING "%s: rtas returned: %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100277 __func__, ret);
Bob Nelson14748552007-07-20 21:39:53 +0200278 return -EIO;
279 }
Maynard Johnsonc7eb7342007-02-13 22:02:03 +0100280 }
Bob Nelson14748552007-07-20 21:39:53 +0200281
282 return 0;
Maynard Johnson18f21902006-11-20 18:45:16 +0100283}
284
285/*
286 * PM Signal functions
287 */
288static void set_pm_event(u32 ctr, int event, u32 unit_mask)
289{
290 struct pm_signal *p;
291 u32 signal_bit;
292 u32 bus_word, bus_type, count_cycles, polarity, input_control;
293 int j, i;
294
295 if (event == PPU_CYCLES_EVENT_NUM) {
296 /* Special Event: Count all cpu cycles */
297 pm_regs.pm07_cntrl[ctr] = CBE_COUNT_ALL_CYCLES;
298 p = &(pm_signal[ctr]);
Maynard Johnsonc7eb7342007-02-13 22:02:03 +0100299 p->signal_group = PPU_CYCLES_GRP_NUM;
Maynard Johnson18f21902006-11-20 18:45:16 +0100300 p->bus_word = 1;
301 p->sub_unit = 0;
302 p->bit = 0;
303 goto out;
304 } else {
305 pm_regs.pm07_cntrl[ctr] = 0;
306 }
307
308 bus_word = GET_BUS_WORD(unit_mask);
309 bus_type = GET_BUS_TYPE(unit_mask);
310 count_cycles = GET_COUNT_CYCLES(unit_mask);
311 polarity = GET_POLARITY(unit_mask);
312 input_control = GET_INPUT_CONTROL(unit_mask);
313 signal_bit = (event % 100);
314
315 p = &(pm_signal[ctr]);
316
317 p->signal_group = event / 100;
318 p->bus_word = bus_word;
Bob Nelsona1ef4842007-08-17 11:06:09 -0500319 p->sub_unit = GET_SUB_UNIT(unit_mask);
Maynard Johnson18f21902006-11-20 18:45:16 +0100320
321 pm_regs.pm07_cntrl[ctr] = 0;
322 pm_regs.pm07_cntrl[ctr] |= PM07_CTR_COUNT_CYCLES(count_cycles);
323 pm_regs.pm07_cntrl[ctr] |= PM07_CTR_POLARITY(polarity);
324 pm_regs.pm07_cntrl[ctr] |= PM07_CTR_INPUT_CONTROL(input_control);
325
Bob Nelson14748552007-07-20 21:39:53 +0200326 /*
327 * Some of the islands signal selection is based on 64 bit words.
Carl Lovebcb63e22007-02-13 22:02:02 +0100328 * The debug bus words are 32 bits, the input words to the performance
329 * counters are defined as 32 bits. Need to convert the 64 bit island
330 * specification to the appropriate 32 input bit and bus word for the
Bob Nelson14748552007-07-20 21:39:53 +0200331 * performance counter event selection. See the CELL Performance
Carl Lovebcb63e22007-02-13 22:02:02 +0100332 * monitoring signals manual and the Perf cntr hardware descriptions
333 * for the details.
334 */
Maynard Johnson18f21902006-11-20 18:45:16 +0100335 if (input_control == 0) {
336 if (signal_bit > 31) {
337 signal_bit -= 32;
338 if (bus_word == 0x3)
339 bus_word = 0x2;
340 else if (bus_word == 0xc)
341 bus_word = 0x8;
342 }
343
344 if ((bus_type == 0) && p->signal_group >= 60)
345 bus_type = 2;
346 if ((bus_type == 1) && p->signal_group >= 50)
347 bus_type = 0;
348
349 pm_regs.pm07_cntrl[ctr] |= PM07_CTR_INPUT_MUX(signal_bit);
350 } else {
351 pm_regs.pm07_cntrl[ctr] = 0;
352 p->bit = signal_bit;
353 }
354
Bob Nelsona1ef4842007-08-17 11:06:09 -0500355 for (i = 0; i < NUM_DEBUG_BUS_WORDS; i++) {
Maynard Johnson18f21902006-11-20 18:45:16 +0100356 if (bus_word & (1 << i)) {
357 pm_regs.debug_bus_control |=
Bob Nelsona1ef4842007-08-17 11:06:09 -0500358 (bus_type << (30 - (2 * i)));
Maynard Johnson18f21902006-11-20 18:45:16 +0100359
Carl Lovebcb63e22007-02-13 22:02:02 +0100360 for (j = 0; j < NUM_INPUT_BUS_WORDS; j++) {
Maynard Johnson18f21902006-11-20 18:45:16 +0100361 if (input_bus[j] == 0xff) {
362 input_bus[j] = i;
363 pm_regs.group_control |=
Bob Nelsona1ef4842007-08-17 11:06:09 -0500364 (i << (30 - (2 * j)));
Bob Nelson14748552007-07-20 21:39:53 +0200365
Maynard Johnson18f21902006-11-20 18:45:16 +0100366 break;
367 }
368 }
369 }
370 }
371out:
372 ;
373}
374
Carl Lovebcb63e22007-02-13 22:02:02 +0100375static void write_pm_cntrl(int cpu)
Maynard Johnson18f21902006-11-20 18:45:16 +0100376{
Bob Nelson14748552007-07-20 21:39:53 +0200377 /*
378 * Oprofile will use 32 bit counters, set bits 7:10 to 0
Carl Lovebcb63e22007-02-13 22:02:02 +0100379 * pmregs.pm_cntrl is a global
380 */
381
Maynard Johnson18f21902006-11-20 18:45:16 +0100382 u32 val = 0;
Carl Lovebcb63e22007-02-13 22:02:02 +0100383 if (pm_regs.pm_cntrl.enable == 1)
Maynard Johnson18f21902006-11-20 18:45:16 +0100384 val |= CBE_PM_ENABLE_PERF_MON;
385
Carl Lovebcb63e22007-02-13 22:02:02 +0100386 if (pm_regs.pm_cntrl.stop_at_max == 1)
Maynard Johnson18f21902006-11-20 18:45:16 +0100387 val |= CBE_PM_STOP_AT_MAX;
388
Carl Love9b934182008-12-01 16:18:34 -0800389 if (pm_regs.pm_cntrl.trace_mode != 0)
Carl Lovebcb63e22007-02-13 22:02:02 +0100390 val |= CBE_PM_TRACE_MODE_SET(pm_regs.pm_cntrl.trace_mode);
Maynard Johnson18f21902006-11-20 18:45:16 +0100391
Carl Love88382322008-12-01 16:18:36 -0800392 if (pm_regs.pm_cntrl.trace_buf_ovflw == 1)
393 val |= CBE_PM_TRACE_BUF_OVFLW(pm_regs.pm_cntrl.trace_buf_ovflw);
Carl Lovebcb63e22007-02-13 22:02:02 +0100394 if (pm_regs.pm_cntrl.freeze == 1)
Maynard Johnson18f21902006-11-20 18:45:16 +0100395 val |= CBE_PM_FREEZE_ALL_CTRS;
396
Carl Love88382322008-12-01 16:18:36 -0800397 val |= CBE_PM_SPU_ADDR_TRACE_SET(pm_regs.pm_cntrl.spu_addr_trace);
398
Bob Nelson14748552007-07-20 21:39:53 +0200399 /*
400 * Routine set_count_mode must be called previously to set
Maynard Johnson18f21902006-11-20 18:45:16 +0100401 * the count mode based on the user selection of user and kernel.
402 */
Carl Lovebcb63e22007-02-13 22:02:02 +0100403 val |= CBE_PM_COUNT_MODE_SET(pm_regs.pm_cntrl.count_mode);
Maynard Johnson18f21902006-11-20 18:45:16 +0100404 cbe_write_pm(cpu, pm_control, val);
405}
406
407static inline void
Carl Lovebcb63e22007-02-13 22:02:02 +0100408set_count_mode(u32 kernel, u32 user)
Maynard Johnson18f21902006-11-20 18:45:16 +0100409{
Bob Nelson14748552007-07-20 21:39:53 +0200410 /*
411 * The user must specify user and kernel if they want them. If
Carl Lovebcb63e22007-02-13 22:02:02 +0100412 * neither is specified, OProfile will count in hypervisor mode.
413 * pm_regs.pm_cntrl is a global
Maynard Johnson18f21902006-11-20 18:45:16 +0100414 */
415 if (kernel) {
416 if (user)
Carl Lovebcb63e22007-02-13 22:02:02 +0100417 pm_regs.pm_cntrl.count_mode = CBE_COUNT_ALL_MODES;
Maynard Johnson18f21902006-11-20 18:45:16 +0100418 else
Carl Lovebcb63e22007-02-13 22:02:02 +0100419 pm_regs.pm_cntrl.count_mode =
420 CBE_COUNT_SUPERVISOR_MODE;
Maynard Johnson18f21902006-11-20 18:45:16 +0100421 } else {
422 if (user)
Carl Lovebcb63e22007-02-13 22:02:02 +0100423 pm_regs.pm_cntrl.count_mode = CBE_COUNT_PROBLEM_MODE;
Maynard Johnson18f21902006-11-20 18:45:16 +0100424 else
Carl Lovebcb63e22007-02-13 22:02:02 +0100425 pm_regs.pm_cntrl.count_mode =
426 CBE_COUNT_HYPERVISOR_MODE;
Maynard Johnson18f21902006-11-20 18:45:16 +0100427 }
428}
429
Robert Richter25ad2912008-09-05 17:12:36 +0200430static inline void enable_ctr(u32 cpu, u32 ctr, u32 *pm07_cntrl)
Maynard Johnson18f21902006-11-20 18:45:16 +0100431{
432
Carl Lovebcb63e22007-02-13 22:02:02 +0100433 pm07_cntrl[ctr] |= CBE_PM_CTR_ENABLE;
Maynard Johnson18f21902006-11-20 18:45:16 +0100434 cbe_write_pm07_control(cpu, ctr, pm07_cntrl[ctr]);
435}
436
437/*
438 * Oprofile is expected to collect data on all CPUs simultaneously.
Bob Nelson14748552007-07-20 21:39:53 +0200439 * However, there is one set of performance counters per node. There are
Maynard Johnson18f21902006-11-20 18:45:16 +0100440 * two hardware threads or virtual CPUs on each node. Hence, OProfile must
441 * multiplex in time the performance counter collection on the two virtual
442 * CPUs. The multiplexing of the performance counters is done by this
443 * virtual counter routine.
444 *
445 * The pmc_values used below is defined as 'per-cpu' but its use is
446 * more akin to 'per-node'. We need to store two sets of counter
447 * values per node -- one for the previous run and one for the next.
448 * The per-cpu[NR_PHYS_CTRS] gives us the storage we need. Each odd/even
449 * pair of per-cpu arrays is used for storing the previous and next
450 * pmc values for a given node.
451 * NOTE: We use the per-cpu variable to improve cache performance.
Bob Nelson14748552007-07-20 21:39:53 +0200452 *
453 * This routine will alternate loading the virtual counters for
454 * virtual CPUs
Maynard Johnson18f21902006-11-20 18:45:16 +0100455 */
456static void cell_virtual_cntr(unsigned long data)
457{
Maynard Johnson18f21902006-11-20 18:45:16 +0100458 int i, prev_hdw_thread, next_hdw_thread;
459 u32 cpu;
460 unsigned long flags;
461
Bob Nelson14748552007-07-20 21:39:53 +0200462 /*
463 * Make sure that the interrupt_hander and the virt counter are
464 * not both playing with the counters on the same node.
Maynard Johnson18f21902006-11-20 18:45:16 +0100465 */
466
Carl Love9b934182008-12-01 16:18:34 -0800467 spin_lock_irqsave(&cntr_lock, flags);
Maynard Johnson18f21902006-11-20 18:45:16 +0100468
469 prev_hdw_thread = hdw_thread;
470
471 /* switch the cpu handling the interrupts */
472 hdw_thread = 1 ^ hdw_thread;
473 next_hdw_thread = hdw_thread;
474
Bob Nelsona1ef4842007-08-17 11:06:09 -0500475 pm_regs.group_control = 0;
476 pm_regs.debug_bus_control = 0;
477
478 for (i = 0; i < NUM_INPUT_BUS_WORDS; i++)
479 input_bus[i] = 0xff;
480
Bob Nelson14748552007-07-20 21:39:53 +0200481 /*
482 * There are some per thread events. Must do the
Carl Lovebcb63e22007-02-13 22:02:02 +0100483 * set event, for the thread that is being started
484 */
Bob Nelson14748552007-07-20 21:39:53 +0200485 for (i = 0; i < num_counters; i++)
Carl Lovebcb63e22007-02-13 22:02:02 +0100486 set_pm_event(i,
487 pmc_cntrl[next_hdw_thread][i].evnts,
488 pmc_cntrl[next_hdw_thread][i].masks);
489
Bob Nelson14748552007-07-20 21:39:53 +0200490 /*
491 * The following is done only once per each node, but
Maynard Johnson18f21902006-11-20 18:45:16 +0100492 * we need cpu #, not node #, to pass to the cbe_xxx functions.
493 */
494 for_each_online_cpu(cpu) {
495 if (cbe_get_hw_thread_id(cpu))
496 continue;
497
Bob Nelson14748552007-07-20 21:39:53 +0200498 /*
499 * stop counters, save counter values, restore counts
Maynard Johnson18f21902006-11-20 18:45:16 +0100500 * for previous thread
501 */
502 cbe_disable_pm(cpu);
503 cbe_disable_pm_interrupts(cpu);
504 for (i = 0; i < num_counters; i++) {
505 per_cpu(pmc_values, cpu + prev_hdw_thread)[i]
506 = cbe_read_ctr(cpu, i);
507
508 if (per_cpu(pmc_values, cpu + next_hdw_thread)[i]
509 == 0xFFFFFFFF)
510 /* If the cntr value is 0xffffffff, we must
511 * reset that to 0xfffffff0 when the current
Bob Nelson14748552007-07-20 21:39:53 +0200512 * thread is restarted. This will generate a
Carl Lovebcb63e22007-02-13 22:02:02 +0100513 * new interrupt and make sure that we never
514 * restore the counters to the max value. If
515 * the counters were restored to the max value,
516 * they do not increment and no interrupts are
517 * generated. Hence no more samples will be
518 * collected on that cpu.
Maynard Johnson18f21902006-11-20 18:45:16 +0100519 */
520 cbe_write_ctr(cpu, i, 0xFFFFFFF0);
521 else
522 cbe_write_ctr(cpu, i,
523 per_cpu(pmc_values,
524 cpu +
525 next_hdw_thread)[i]);
526 }
527
Bob Nelson14748552007-07-20 21:39:53 +0200528 /*
529 * Switch to the other thread. Change the interrupt
Maynard Johnson18f21902006-11-20 18:45:16 +0100530 * and control regs to be scheduled on the CPU
531 * corresponding to the thread to execute.
532 */
533 for (i = 0; i < num_counters; i++) {
534 if (pmc_cntrl[next_hdw_thread][i].enabled) {
Bob Nelson14748552007-07-20 21:39:53 +0200535 /*
536 * There are some per thread events.
Maynard Johnson18f21902006-11-20 18:45:16 +0100537 * Must do the set event, enable_cntr
538 * for each cpu.
539 */
Maynard Johnson18f21902006-11-20 18:45:16 +0100540 enable_ctr(cpu, i,
541 pm_regs.pm07_cntrl);
542 } else {
543 cbe_write_pm07_control(cpu, i, 0);
544 }
545 }
546
547 /* Enable interrupts on the CPU thread that is starting */
548 cbe_enable_pm_interrupts(cpu, next_hdw_thread,
549 virt_cntr_inter_mask);
550 cbe_enable_pm(cpu);
551 }
552
Carl Love9b934182008-12-01 16:18:34 -0800553 spin_unlock_irqrestore(&cntr_lock, flags);
Maynard Johnson18f21902006-11-20 18:45:16 +0100554
555 mod_timer(&timer_virt_cntr, jiffies + HZ / 10);
556}
557
558static void start_virt_cntrs(void)
559{
560 init_timer(&timer_virt_cntr);
561 timer_virt_cntr.function = cell_virtual_cntr;
562 timer_virt_cntr.data = 0UL;
563 timer_virt_cntr.expires = jiffies + HZ / 10;
564 add_timer(&timer_virt_cntr);
565}
566
Carl Love9b934182008-12-01 16:18:34 -0800567static int cell_reg_setup_spu_cycles(struct op_counter_config *ctr,
Bob Nelson14748552007-07-20 21:39:53 +0200568 struct op_system_config *sys, int num_ctrs)
Maynard Johnson18f21902006-11-20 18:45:16 +0100569{
Carl Love9b934182008-12-01 16:18:34 -0800570 spu_cycle_reset = ctr[0].count;
Bob Nelson14748552007-07-20 21:39:53 +0200571
572 /*
Carl Love9b934182008-12-01 16:18:34 -0800573 * Each node will need to make the rtas call to start
574 * and stop SPU profiling. Get the token once and store it.
Bob Nelson14748552007-07-20 21:39:53 +0200575 */
Carl Love9b934182008-12-01 16:18:34 -0800576 spu_rtas_token = rtas_token("ibm,cbe-spu-perftools");
577
578 if (unlikely(spu_rtas_token == RTAS_UNKNOWN_SERVICE)) {
Bob Nelson14748552007-07-20 21:39:53 +0200579 printk(KERN_ERR
Carl Love9b934182008-12-01 16:18:34 -0800580 "%s: rtas token ibm,cbe-spu-perftools unknown\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100581 __func__);
Bob Nelson14748552007-07-20 21:39:53 +0200582 return -EIO;
Maynard Johnson18f21902006-11-20 18:45:16 +0100583 }
Carl Love9b934182008-12-01 16:18:34 -0800584 return 0;
585}
586
Carl Love88382322008-12-01 16:18:36 -0800587/* Unfortunately, the hardware will only support event profiling
588 * on one SPU per node at a time. Therefore, we must time slice
589 * the profiling across all SPUs in the node. Note, we do this
590 * in parallel for each node. The following routine is called
591 * periodically based on kernel timer to switch which SPU is
592 * being monitored in a round robbin fashion.
593 */
594static void spu_evnt_swap(unsigned long data)
595{
596 int node;
597 int cur_phys_spu, nxt_phys_spu, cur_spu_evnt_phys_spu_indx;
598 unsigned long flags;
599 int cpu;
600 int ret;
601 u32 interrupt_mask;
602
603
604 /* enable interrupts on cntr 0 */
605 interrupt_mask = CBE_PM_CTR_OVERFLOW_INTR(0);
606
607 hdw_thread = 0;
608
609 /* Make sure spu event interrupt handler and spu event swap
610 * don't access the counters simultaneously.
611 */
612 spin_lock_irqsave(&cntr_lock, flags);
613
614 cur_spu_evnt_phys_spu_indx = spu_evnt_phys_spu_indx;
615
616 if (++(spu_evnt_phys_spu_indx) == NUM_SPUS_PER_NODE)
617 spu_evnt_phys_spu_indx = 0;
618
619 pm_signal[0].sub_unit = spu_evnt_phys_spu_indx;
620 pm_signal[1].sub_unit = spu_evnt_phys_spu_indx;
621 pm_signal[2].sub_unit = spu_evnt_phys_spu_indx;
622
623 /* switch the SPU being profiled on each node */
624 for_each_online_cpu(cpu) {
625 if (cbe_get_hw_thread_id(cpu))
626 continue;
627
628 node = cbe_cpu_to_node(cpu);
629 cur_phys_spu = (node * NUM_SPUS_PER_NODE)
630 + cur_spu_evnt_phys_spu_indx;
631 nxt_phys_spu = (node * NUM_SPUS_PER_NODE)
632 + spu_evnt_phys_spu_indx;
633
634 /*
635 * stop counters, save counter values, restore counts
636 * for previous physical SPU
637 */
638 cbe_disable_pm(cpu);
639 cbe_disable_pm_interrupts(cpu);
640
641 spu_pm_cnt[cur_phys_spu]
642 = cbe_read_ctr(cpu, 0);
643
644 /* restore previous count for the next spu to sample */
645 /* NOTE, hardware issue, counter will not start if the
646 * counter value is at max (0xFFFFFFFF).
647 */
648 if (spu_pm_cnt[nxt_phys_spu] >= 0xFFFFFFFF)
649 cbe_write_ctr(cpu, 0, 0xFFFFFFF0);
650 else
651 cbe_write_ctr(cpu, 0, spu_pm_cnt[nxt_phys_spu]);
652
653 pm_rtas_reset_signals(cbe_cpu_to_node(cpu));
654
655 /* setup the debug bus measure the one event and
656 * the two events to route the next SPU's PC on
657 * the debug bus
658 */
659 ret = pm_rtas_activate_signals(cbe_cpu_to_node(cpu), 3);
660 if (ret)
661 printk(KERN_ERR
662 "%s: pm_rtas_activate_signals failed, SPU event swap\n",
663 __func__);
664
665 /* clear the trace buffer, don't want to take PC for
666 * previous SPU*/
667 cbe_write_pm(cpu, trace_address, 0);
668
669 enable_ctr(cpu, 0, pm_regs.pm07_cntrl);
670
671 /* Enable interrupts on the CPU thread that is starting */
672 cbe_enable_pm_interrupts(cpu, hdw_thread,
673 interrupt_mask);
674 cbe_enable_pm(cpu);
675 }
676
677 spin_unlock_irqrestore(&cntr_lock, flags);
678
679 /* swap approximately every 0.1 seconds */
680 mod_timer(&timer_spu_event_swap, jiffies + HZ / 25);
681}
682
683static void start_spu_event_swap(void)
684{
685 init_timer(&timer_spu_event_swap);
686 timer_spu_event_swap.function = spu_evnt_swap;
687 timer_spu_event_swap.data = 0UL;
688 timer_spu_event_swap.expires = jiffies + HZ / 25;
689 add_timer(&timer_spu_event_swap);
690}
691
692static int cell_reg_setup_spu_events(struct op_counter_config *ctr,
693 struct op_system_config *sys, int num_ctrs)
694{
695 int i;
696
697 /* routine is called once for all nodes */
698
699 spu_evnt_phys_spu_indx = 0;
700 /*
701 * For all events except PPU CYCLEs, each node will need to make
702 * the rtas cbe-perftools call to setup and reset the debug bus.
703 * Make the token lookup call once and store it in the global
704 * variable pm_rtas_token.
705 */
706 pm_rtas_token = rtas_token("ibm,cbe-perftools");
707
708 if (unlikely(pm_rtas_token == RTAS_UNKNOWN_SERVICE)) {
709 printk(KERN_ERR
710 "%s: rtas token ibm,cbe-perftools unknown\n",
711 __func__);
712 return -EIO;
713 }
714
715 /* setup the pm_control register settings,
716 * settings will be written per node by the
717 * cell_cpu_setup() function.
718 */
719 pm_regs.pm_cntrl.trace_buf_ovflw = 1;
720
721 /* Use the occurrence trace mode to have SPU PC saved
722 * to the trace buffer. Occurrence data in trace buffer
723 * is not used. Bit 2 must be set to store SPU addresses.
724 */
725 pm_regs.pm_cntrl.trace_mode = 2;
726
727 pm_regs.pm_cntrl.spu_addr_trace = 0x1; /* using debug bus
728 event 2 & 3 */
729
730 /* setup the debug bus event array with the SPU PC routing events.
731 * Note, pm_signal[0] will be filled in by set_pm_event() call below.
732 */
733 pm_signal[1].signal_group = SPU_PROFILE_EVENT_ADDR / 100;
734 pm_signal[1].bus_word = GET_BUS_WORD(SPU_PROFILE_EVENT_ADDR_MASK_A);
735 pm_signal[1].bit = SPU_PROFILE_EVENT_ADDR % 100;
736 pm_signal[1].sub_unit = spu_evnt_phys_spu_indx;
737
738 pm_signal[2].signal_group = SPU_PROFILE_EVENT_ADDR / 100;
739 pm_signal[2].bus_word = GET_BUS_WORD(SPU_PROFILE_EVENT_ADDR_MASK_B);
740 pm_signal[2].bit = SPU_PROFILE_EVENT_ADDR % 100;
741 pm_signal[2].sub_unit = spu_evnt_phys_spu_indx;
742
743 /* Set the user selected spu event to profile on,
744 * note, only one SPU profiling event is supported
745 */
746 num_counters = 1; /* Only support one SPU event at a time */
747 set_pm_event(0, ctr[0].event, ctr[0].unit_mask);
748
749 reset_value[0] = 0xFFFFFFFF - ctr[0].count;
750
751 /* global, used by cell_cpu_setup */
752 ctr_enabled |= 1;
753
754 /* Initialize the count for each SPU to the reset value */
755 for (i=0; i < MAX_NUMNODES * NUM_SPUS_PER_NODE; i++)
756 spu_pm_cnt[i] = reset_value[0];
757
758 return 0;
759}
760
Carl Love9b934182008-12-01 16:18:34 -0800761static int cell_reg_setup_ppu(struct op_counter_config *ctr,
762 struct op_system_config *sys, int num_ctrs)
763{
Carl Love88382322008-12-01 16:18:36 -0800764 /* routine is called once for all nodes */
Carl Love9b934182008-12-01 16:18:34 -0800765 int i, j, cpu;
Maynard Johnson18f21902006-11-20 18:45:16 +0100766
767 num_counters = num_ctrs;
768
Carl Love210434d2008-10-29 05:06:45 +0000769 if (unlikely(num_ctrs > NR_PHYS_CTRS)) {
770 printk(KERN_ERR
771 "%s: Oprofile, number of specified events " \
772 "exceeds number of physical counters\n",
773 __func__);
774 return -EIO;
775 }
Maynard Johnson18f21902006-11-20 18:45:16 +0100776
Carl Lovebcb63e22007-02-13 22:02:02 +0100777 set_count_mode(sys->enable_kernel, sys->enable_user);
Maynard Johnson18f21902006-11-20 18:45:16 +0100778
779 /* Setup the thread 0 events */
780 for (i = 0; i < num_ctrs; ++i) {
781
782 pmc_cntrl[0][i].evnts = ctr[i].event;
783 pmc_cntrl[0][i].masks = ctr[i].unit_mask;
784 pmc_cntrl[0][i].enabled = ctr[i].enabled;
785 pmc_cntrl[0][i].vcntr = i;
786
787 for_each_possible_cpu(j)
788 per_cpu(pmc_values, j)[i] = 0;
789 }
790
Bob Nelson14748552007-07-20 21:39:53 +0200791 /*
792 * Setup the thread 1 events, map the thread 0 event to the
Maynard Johnson18f21902006-11-20 18:45:16 +0100793 * equivalent thread 1 event.
794 */
795 for (i = 0; i < num_ctrs; ++i) {
796 if ((ctr[i].event >= 2100) && (ctr[i].event <= 2111))
797 pmc_cntrl[1][i].evnts = ctr[i].event + 19;
798 else if (ctr[i].event == 2203)
799 pmc_cntrl[1][i].evnts = ctr[i].event;
800 else if ((ctr[i].event >= 2200) && (ctr[i].event <= 2215))
801 pmc_cntrl[1][i].evnts = ctr[i].event + 16;
802 else
803 pmc_cntrl[1][i].evnts = ctr[i].event;
804
805 pmc_cntrl[1][i].masks = ctr[i].unit_mask;
806 pmc_cntrl[1][i].enabled = ctr[i].enabled;
807 pmc_cntrl[1][i].vcntr = i;
808 }
809
Carl Lovebcb63e22007-02-13 22:02:02 +0100810 for (i = 0; i < NUM_INPUT_BUS_WORDS; i++)
Maynard Johnson18f21902006-11-20 18:45:16 +0100811 input_bus[i] = 0xff;
812
Bob Nelson14748552007-07-20 21:39:53 +0200813 /*
814 * Our counters count up, and "count" refers to
Maynard Johnson18f21902006-11-20 18:45:16 +0100815 * how much before the next interrupt, and we interrupt
Bob Nelson14748552007-07-20 21:39:53 +0200816 * on overflow. So we calculate the starting value
Maynard Johnson18f21902006-11-20 18:45:16 +0100817 * which will give us "count" until overflow.
818 * Then we set the events on the enabled counters.
819 */
820 for (i = 0; i < num_counters; ++i) {
821 /* start with virtual counter set 0 */
822 if (pmc_cntrl[0][i].enabled) {
823 /* Using 32bit counters, reset max - count */
824 reset_value[i] = 0xFFFFFFFF - ctr[i].count;
825 set_pm_event(i,
826 pmc_cntrl[0][i].evnts,
827 pmc_cntrl[0][i].masks);
828
829 /* global, used by cell_cpu_setup */
830 ctr_enabled |= (1 << i);
831 }
832 }
833
834 /* initialize the previous counts for the virtual cntrs */
835 for_each_online_cpu(cpu)
836 for (i = 0; i < num_counters; ++i) {
837 per_cpu(pmc_values, cpu)[i] = reset_value[i];
838 }
Bob Nelson14748552007-07-20 21:39:53 +0200839
840 return 0;
Maynard Johnson18f21902006-11-20 18:45:16 +0100841}
842
Bob Nelson14748552007-07-20 21:39:53 +0200843
Carl Love9b934182008-12-01 16:18:34 -0800844/* This function is called once for all cpus combined */
845static int cell_reg_setup(struct op_counter_config *ctr,
846 struct op_system_config *sys, int num_ctrs)
847{
Carl Love88382322008-12-01 16:18:36 -0800848 int ret=0;
Carl Love9b934182008-12-01 16:18:34 -0800849 spu_cycle_reset = 0;
850
Carl Love88382322008-12-01 16:18:36 -0800851 /* initialize the spu_arr_trace value, will be reset if
852 * doing spu event profiling.
853 */
854 pm_regs.group_control = 0;
855 pm_regs.debug_bus_control = 0;
856 pm_regs.pm_cntrl.stop_at_max = 1;
857 pm_regs.pm_cntrl.trace_mode = 0;
858 pm_regs.pm_cntrl.freeze = 1;
859 pm_regs.pm_cntrl.trace_buf_ovflw = 0;
860 pm_regs.pm_cntrl.spu_addr_trace = 0;
861
Carl Love9b934182008-12-01 16:18:34 -0800862 /*
863 * For all events except PPU CYCLEs, each node will need to make
864 * the rtas cbe-perftools call to setup and reset the debug bus.
865 * Make the token lookup call once and store it in the global
866 * variable pm_rtas_token.
867 */
868 pm_rtas_token = rtas_token("ibm,cbe-perftools");
869
870 if (unlikely(pm_rtas_token == RTAS_UNKNOWN_SERVICE)) {
871 printk(KERN_ERR
872 "%s: rtas token ibm,cbe-perftools unknown\n",
873 __func__);
874 return -EIO;
875 }
876
877 if (ctr[0].event == SPU_CYCLES_EVENT_NUM) {
878 profiling_mode = SPU_PROFILING_CYCLES;
879 ret = cell_reg_setup_spu_cycles(ctr, sys, num_ctrs);
Carl Love88382322008-12-01 16:18:36 -0800880 } else if ((ctr[0].event >= SPU_EVENT_NUM_START) &&
881 (ctr[0].event <= SPU_EVENT_NUM_STOP)) {
882 profiling_mode = SPU_PROFILING_EVENTS;
883 spu_cycle_reset = ctr[0].count;
884
885 /* for SPU event profiling, need to setup the
886 * pm_signal array with the events to route the
887 * SPU PC before making the FW call. Note, only
888 * one SPU event for profiling can be specified
889 * at a time.
890 */
891 cell_reg_setup_spu_events(ctr, sys, num_ctrs);
Carl Love9b934182008-12-01 16:18:34 -0800892 } else {
893 profiling_mode = PPU_PROFILING;
894 ret = cell_reg_setup_ppu(ctr, sys, num_ctrs);
895 }
896
897 return ret;
898}
899
900
Bob Nelson14748552007-07-20 21:39:53 +0200901
Maynard Johnson18f21902006-11-20 18:45:16 +0100902/* This function is called once for each cpu */
Bob Nelson14748552007-07-20 21:39:53 +0200903static int cell_cpu_setup(struct op_counter_config *cntr)
Maynard Johnson18f21902006-11-20 18:45:16 +0100904{
905 u32 cpu = smp_processor_id();
906 u32 num_enabled = 0;
907 int i;
Carl Love88382322008-12-01 16:18:36 -0800908 int ret;
Maynard Johnson18f21902006-11-20 18:45:16 +0100909
Carl Love9b934182008-12-01 16:18:34 -0800910 /* Cycle based SPU profiling does not use the performance
911 * counters. The trace array is configured to collect
912 * the data.
913 */
914 if (profiling_mode == SPU_PROFILING_CYCLES)
Bob Nelson14748552007-07-20 21:39:53 +0200915 return 0;
916
Maynard Johnson18f21902006-11-20 18:45:16 +0100917 /* There is one performance monitor per processor chip (i.e. node),
918 * so we only need to perform this function once per node.
919 */
920 if (cbe_get_hw_thread_id(cpu))
Bob Nelson14748552007-07-20 21:39:53 +0200921 return 0;
Maynard Johnson18f21902006-11-20 18:45:16 +0100922
923 /* Stop all counters */
924 cbe_disable_pm(cpu);
925 cbe_disable_pm_interrupts(cpu);
926
Maynard Johnson18f21902006-11-20 18:45:16 +0100927 cbe_write_pm(cpu, pm_start_stop, 0);
928 cbe_write_pm(cpu, group_control, pm_regs.group_control);
929 cbe_write_pm(cpu, debug_bus_control, pm_regs.debug_bus_control);
Carl Lovebcb63e22007-02-13 22:02:02 +0100930 write_pm_cntrl(cpu);
Maynard Johnson18f21902006-11-20 18:45:16 +0100931
932 for (i = 0; i < num_counters; ++i) {
933 if (ctr_enabled & (1 << i)) {
934 pm_signal[num_enabled].cpu = cbe_cpu_to_node(cpu);
935 num_enabled++;
936 }
937 }
938
Bob Nelson14748552007-07-20 21:39:53 +0200939 /*
940 * The pm_rtas_activate_signals will return -EIO if the FW
941 * call failed.
942 */
Carl Love88382322008-12-01 16:18:36 -0800943 if (profiling_mode == SPU_PROFILING_EVENTS) {
944 /* For SPU event profiling also need to setup the
945 * pm interval timer
946 */
947 ret = pm_rtas_activate_signals(cbe_cpu_to_node(cpu),
948 num_enabled+2);
949 /* store PC from debug bus to Trace buffer as often
950 * as possible (every 10 cycles)
951 */
952 cbe_write_pm(cpu, pm_interval, NUM_INTERVAL_CYC);
953 return ret;
954 } else
955 return pm_rtas_activate_signals(cbe_cpu_to_node(cpu),
956 num_enabled);
Maynard Johnson18f21902006-11-20 18:45:16 +0100957}
958
Bob Nelson14748552007-07-20 21:39:53 +0200959#define ENTRIES 303
960#define MAXLFSR 0xFFFFFF
961
962/* precomputed table of 24 bit LFSR values */
963static int initial_lfsr[] = {
964 8221349, 12579195, 5379618, 10097839, 7512963, 7519310, 3955098, 10753424,
965 15507573, 7458917, 285419, 2641121, 9780088, 3915503, 6668768, 1548716,
966 4885000, 8774424, 9650099, 2044357, 2304411, 9326253, 10332526, 4421547,
967 3440748, 10179459, 13332843, 10375561, 1313462, 8375100, 5198480, 6071392,
968 9341783, 1526887, 3985002, 1439429, 13923762, 7010104, 11969769, 4547026,
969 2040072, 4025602, 3437678, 7939992, 11444177, 4496094, 9803157, 10745556,
970 3671780, 4257846, 5662259, 13196905, 3237343, 12077182, 16222879, 7587769,
971 14706824, 2184640, 12591135, 10420257, 7406075, 3648978, 11042541, 15906893,
972 11914928, 4732944, 10695697, 12928164, 11980531, 4430912, 11939291, 2917017,
973 6119256, 4172004, 9373765, 8410071, 14788383, 5047459, 5474428, 1737756,
974 15967514, 13351758, 6691285, 8034329, 2856544, 14394753, 11310160, 12149558,
975 7487528, 7542781, 15668898, 12525138, 12790975, 3707933, 9106617, 1965401,
976 16219109, 12801644, 2443203, 4909502, 8762329, 3120803, 6360315, 9309720,
977 15164599, 10844842, 4456529, 6667610, 14924259, 884312, 6234963, 3326042,
978 15973422, 13919464, 5272099, 6414643, 3909029, 2764324, 5237926, 4774955,
979 10445906, 4955302, 5203726, 10798229, 11443419, 2303395, 333836, 9646934,
980 3464726, 4159182, 568492, 995747, 10318756, 13299332, 4836017, 8237783,
981 3878992, 2581665, 11394667, 5672745, 14412947, 3159169, 9094251, 16467278,
982 8671392, 15230076, 4843545, 7009238, 15504095, 1494895, 9627886, 14485051,
983 8304291, 252817, 12421642, 16085736, 4774072, 2456177, 4160695, 15409741,
984 4902868, 5793091, 13162925, 16039714, 782255, 11347835, 14884586, 366972,
985 16308990, 11913488, 13390465, 2958444, 10340278, 1177858, 1319431, 10426302,
986 2868597, 126119, 5784857, 5245324, 10903900, 16436004, 3389013, 1742384,
987 14674502, 10279218, 8536112, 10364279, 6877778, 14051163, 1025130, 6072469,
988 1988305, 8354440, 8216060, 16342977, 13112639, 3976679, 5913576, 8816697,
989 6879995, 14043764, 3339515, 9364420, 15808858, 12261651, 2141560, 5636398,
990 10345425, 10414756, 781725, 6155650, 4746914, 5078683, 7469001, 6799140,
991 10156444, 9667150, 10116470, 4133858, 2121972, 1124204, 1003577, 1611214,
992 14304602, 16221850, 13878465, 13577744, 3629235, 8772583, 10881308, 2410386,
993 7300044, 5378855, 9301235, 12755149, 4977682, 8083074, 10327581, 6395087,
994 9155434, 15501696, 7514362, 14520507, 15808945, 3244584, 4741962, 9658130,
995 14336147, 8654727, 7969093, 15759799, 14029445, 5038459, 9894848, 8659300,
996 13699287, 8834306, 10712885, 14753895, 10410465, 3373251, 309501, 9561475,
997 5526688, 14647426, 14209836, 5339224, 207299, 14069911, 8722990, 2290950,
998 3258216, 12505185, 6007317, 9218111, 14661019, 10537428, 11731949, 9027003,
999 6641507, 9490160, 200241, 9720425, 16277895, 10816638, 1554761, 10431375,
1000 7467528, 6790302, 3429078, 14633753, 14428997, 11463204, 3576212, 2003426,
1001 6123687, 820520, 9992513, 15784513, 5778891, 6428165, 8388607
1002};
1003
1004/*
1005 * The hardware uses an LFSR counting sequence to determine when to capture
1006 * the SPU PCs. An LFSR sequence is like a puesdo random number sequence
1007 * where each number occurs once in the sequence but the sequence is not in
1008 * numerical order. The SPU PC capture is done when the LFSR sequence reaches
1009 * the last value in the sequence. Hence the user specified value N
1010 * corresponds to the LFSR number that is N from the end of the sequence.
1011 *
1012 * To avoid the time to compute the LFSR, a lookup table is used. The 24 bit
1013 * LFSR sequence is broken into four ranges. The spacing of the precomputed
1014 * values is adjusted in each range so the error between the user specifed
1015 * number (N) of events between samples and the actual number of events based
1016 * on the precomputed value will be les then about 6.2%. Note, if the user
1017 * specifies N < 2^16, the LFSR value that is 2^16 from the end will be used.
1018 * This is to prevent the loss of samples because the trace buffer is full.
1019 *
1020 * User specified N Step between Index in
1021 * precomputed values precomputed
1022 * table
1023 * 0 to 2^16-1 ---- 0
1024 * 2^16 to 2^16+2^19-1 2^12 1 to 128
1025 * 2^16+2^19 to 2^16+2^19+2^22-1 2^15 129 to 256
1026 * 2^16+2^19+2^22 to 2^24-1 2^18 257 to 302
1027 *
1028 *
1029 * For example, the LFSR values in the second range are computed for 2^16,
1030 * 2^16+2^12, ... , 2^19-2^16, 2^19 and stored in the table at indicies
1031 * 1, 2,..., 127, 128.
1032 *
1033 * The 24 bit LFSR value for the nth number in the sequence can be
1034 * calculated using the following code:
1035 *
1036 * #define size 24
1037 * int calculate_lfsr(int n)
1038 * {
1039 * int i;
1040 * unsigned int newlfsr0;
1041 * unsigned int lfsr = 0xFFFFFF;
1042 * unsigned int howmany = n;
1043 *
1044 * for (i = 2; i < howmany + 2; i++) {
1045 * newlfsr0 = (((lfsr >> (size - 1 - 0)) & 1) ^
1046 * ((lfsr >> (size - 1 - 1)) & 1) ^
1047 * (((lfsr >> (size - 1 - 6)) & 1) ^
1048 * ((lfsr >> (size - 1 - 23)) & 1)));
1049 *
1050 * lfsr >>= 1;
1051 * lfsr = lfsr | (newlfsr0 << (size - 1));
1052 * }
1053 * return lfsr;
1054 * }
1055 */
1056
1057#define V2_16 (0x1 << 16)
1058#define V2_19 (0x1 << 19)
1059#define V2_22 (0x1 << 22)
1060
1061static int calculate_lfsr(int n)
Maynard Johnson18f21902006-11-20 18:45:16 +01001062{
Bob Nelson14748552007-07-20 21:39:53 +02001063 /*
1064 * The ranges and steps are in powers of 2 so the calculations
1065 * can be done using shifts rather then divide.
1066 */
1067 int index;
1068
1069 if ((n >> 16) == 0)
1070 index = 0;
1071 else if (((n - V2_16) >> 19) == 0)
1072 index = ((n - V2_16) >> 12) + 1;
1073 else if (((n - V2_16 - V2_19) >> 22) == 0)
1074 index = ((n - V2_16 - V2_19) >> 15 ) + 1 + 128;
1075 else if (((n - V2_16 - V2_19 - V2_22) >> 24) == 0)
1076 index = ((n - V2_16 - V2_19 - V2_22) >> 18 ) + 1 + 256;
1077 else
1078 index = ENTRIES-1;
1079
1080 /* make sure index is valid */
1081 if ((index > ENTRIES) || (index < 0))
1082 index = ENTRIES-1;
1083
1084 return initial_lfsr[index];
1085}
1086
1087static int pm_rtas_activate_spu_profiling(u32 node)
1088{
1089 int ret, i;
Carl Love210434d2008-10-29 05:06:45 +00001090 struct pm_signal pm_signal_local[NUM_SPUS_PER_NODE];
Bob Nelson14748552007-07-20 21:39:53 +02001091
1092 /*
1093 * Set up the rtas call to configure the debug bus to
1094 * route the SPU PCs. Setup the pm_signal for each SPU
1095 */
Carl Love210434d2008-10-29 05:06:45 +00001096 for (i = 0; i < ARRAY_SIZE(pm_signal_local); i++) {
Bob Nelson14748552007-07-20 21:39:53 +02001097 pm_signal_local[i].cpu = node;
1098 pm_signal_local[i].signal_group = 41;
1099 /* spu i on word (i/2) */
1100 pm_signal_local[i].bus_word = 1 << i / 2;
1101 /* spu i */
1102 pm_signal_local[i].sub_unit = i;
1103 pm_signal_local[i].bit = 63;
1104 }
1105
1106 ret = rtas_ibm_cbe_perftools(SUBFUNC_ACTIVATE,
1107 PASSTHRU_ENABLE, pm_signal_local,
Carl Love210434d2008-10-29 05:06:45 +00001108 (ARRAY_SIZE(pm_signal_local)
Bob Nelson14748552007-07-20 21:39:53 +02001109 * sizeof(struct pm_signal)));
1110
1111 if (unlikely(ret)) {
1112 printk(KERN_WARNING "%s: rtas returned: %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +11001113 __func__, ret);
Bob Nelson14748552007-07-20 21:39:53 +02001114 return -EIO;
1115 }
1116
1117 return 0;
1118}
1119
1120#ifdef CONFIG_CPU_FREQ
1121static int
1122oprof_cpufreq_notify(struct notifier_block *nb, unsigned long val, void *data)
1123{
1124 int ret = 0;
1125 struct cpufreq_freqs *frq = data;
1126 if ((val == CPUFREQ_PRECHANGE && frq->old < frq->new) ||
1127 (val == CPUFREQ_POSTCHANGE && frq->old > frq->new) ||
1128 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE))
1129 set_spu_profiling_frequency(frq->new, spu_cycle_reset);
1130 return ret;
1131}
1132
1133static struct notifier_block cpu_freq_notifier_block = {
1134 .notifier_call = oprof_cpufreq_notify
1135};
1136#endif
1137
Carl Love9b934182008-12-01 16:18:34 -08001138/*
1139 * Note the generic OProfile stop calls do not support returning
1140 * an error on stop. Hence, will not return an error if the FW
1141 * calls fail on stop. Failure to reset the debug bus is not an issue.
1142 * Failure to disable the SPU profiling is not an issue. The FW calls
1143 * to enable the performance counters and debug bus will work even if
1144 * the hardware was not cleanly reset.
1145 */
1146static void cell_global_stop_spu_cycles(void)
1147{
1148 int subfunc, rtn_value;
1149 unsigned int lfsr_value;
1150 int cpu;
1151
1152 oprofile_running = 0;
Carl Love88382322008-12-01 16:18:36 -08001153 smp_wmb();
Carl Love9b934182008-12-01 16:18:34 -08001154
1155#ifdef CONFIG_CPU_FREQ
1156 cpufreq_unregister_notifier(&cpu_freq_notifier_block,
1157 CPUFREQ_TRANSITION_NOTIFIER);
1158#endif
1159
1160 for_each_online_cpu(cpu) {
1161 if (cbe_get_hw_thread_id(cpu))
1162 continue;
1163
1164 subfunc = 3; /*
1165 * 2 - activate SPU tracing,
1166 * 3 - deactivate
1167 */
1168 lfsr_value = 0x8f100000;
1169
1170 rtn_value = rtas_call(spu_rtas_token, 3, 1, NULL,
1171 subfunc, cbe_cpu_to_node(cpu),
1172 lfsr_value);
1173
1174 if (unlikely(rtn_value != 0)) {
1175 printk(KERN_ERR
1176 "%s: rtas call ibm,cbe-spu-perftools " \
1177 "failed, return = %d\n",
1178 __func__, rtn_value);
1179 }
1180
1181 /* Deactivate the signals */
1182 pm_rtas_reset_signals(cbe_cpu_to_node(cpu));
1183 }
1184
Carl Love88382322008-12-01 16:18:36 -08001185 stop_spu_profiling_cycles();
1186}
1187
1188static void cell_global_stop_spu_events(void)
1189{
1190 int cpu;
1191 oprofile_running = 0;
1192
1193 stop_spu_profiling_events();
1194 smp_wmb();
1195
1196 for_each_online_cpu(cpu) {
1197 if (cbe_get_hw_thread_id(cpu))
1198 continue;
1199
1200 cbe_sync_irq(cbe_cpu_to_node(cpu));
1201 /* Stop the counters */
1202 cbe_disable_pm(cpu);
1203 cbe_write_pm07_control(cpu, 0, 0);
1204
1205 /* Deactivate the signals */
1206 pm_rtas_reset_signals(cbe_cpu_to_node(cpu));
1207
1208 /* Deactivate interrupts */
1209 cbe_disable_pm_interrupts(cpu);
1210 }
1211 del_timer_sync(&timer_spu_event_swap);
Carl Love9b934182008-12-01 16:18:34 -08001212}
1213
1214static void cell_global_stop_ppu(void)
1215{
1216 int cpu;
1217
1218 /*
1219 * This routine will be called once for the system.
1220 * There is one performance monitor per node, so we
1221 * only need to perform this function once per node.
1222 */
1223 del_timer_sync(&timer_virt_cntr);
1224 oprofile_running = 0;
1225 smp_wmb();
1226
1227 for_each_online_cpu(cpu) {
1228 if (cbe_get_hw_thread_id(cpu))
1229 continue;
1230
1231 cbe_sync_irq(cbe_cpu_to_node(cpu));
1232 /* Stop the counters */
1233 cbe_disable_pm(cpu);
1234
1235 /* Deactivate the signals */
1236 pm_rtas_reset_signals(cbe_cpu_to_node(cpu));
1237
1238 /* Deactivate interrupts */
1239 cbe_disable_pm_interrupts(cpu);
1240 }
1241}
1242
1243static void cell_global_stop(void)
1244{
1245 if (profiling_mode == PPU_PROFILING)
1246 cell_global_stop_ppu();
Carl Love88382322008-12-01 16:18:36 -08001247 else if (profiling_mode == SPU_PROFILING_EVENTS)
1248 cell_global_stop_spu_events();
Carl Love9b934182008-12-01 16:18:34 -08001249 else
1250 cell_global_stop_spu_cycles();
1251}
1252
1253static int cell_global_start_spu_cycles(struct op_counter_config *ctr)
Bob Nelson14748552007-07-20 21:39:53 +02001254{
1255 int subfunc;
1256 unsigned int lfsr_value;
1257 int cpu;
1258 int ret;
1259 int rtas_error;
1260 unsigned int cpu_khzfreq = 0;
1261
1262 /* The SPU profiling uses time-based profiling based on
1263 * cpu frequency, so if configured with the CPU_FREQ
1264 * option, we should detect frequency changes and react
1265 * accordingly.
1266 */
1267#ifdef CONFIG_CPU_FREQ
1268 ret = cpufreq_register_notifier(&cpu_freq_notifier_block,
1269 CPUFREQ_TRANSITION_NOTIFIER);
1270 if (ret < 0)
1271 /* this is not a fatal error */
1272 printk(KERN_ERR "CPU freq change registration failed: %d\n",
1273 ret);
1274
1275 else
1276 cpu_khzfreq = cpufreq_quick_get(smp_processor_id());
1277#endif
1278
1279 set_spu_profiling_frequency(cpu_khzfreq, spu_cycle_reset);
1280
1281 for_each_online_cpu(cpu) {
1282 if (cbe_get_hw_thread_id(cpu))
1283 continue;
1284
1285 /*
1286 * Setup SPU cycle-based profiling.
1287 * Set perf_mon_control bit 0 to a zero before
1288 * enabling spu collection hardware.
1289 */
1290 cbe_write_pm(cpu, pm_control, 0);
1291
1292 if (spu_cycle_reset > MAX_SPU_COUNT)
1293 /* use largest possible value */
1294 lfsr_value = calculate_lfsr(MAX_SPU_COUNT-1);
1295 else
1296 lfsr_value = calculate_lfsr(spu_cycle_reset);
1297
1298 /* must use a non zero value. Zero disables data collection. */
1299 if (lfsr_value == 0)
1300 lfsr_value = calculate_lfsr(1);
1301
1302 lfsr_value = lfsr_value << 8; /* shift lfsr to correct
1303 * register location
1304 */
1305
1306 /* debug bus setup */
1307 ret = pm_rtas_activate_spu_profiling(cbe_cpu_to_node(cpu));
1308
1309 if (unlikely(ret)) {
1310 rtas_error = ret;
1311 goto out;
1312 }
1313
1314
1315 subfunc = 2; /* 2 - activate SPU tracing, 3 - deactivate */
1316
1317 /* start profiling */
1318 ret = rtas_call(spu_rtas_token, 3, 1, NULL, subfunc,
1319 cbe_cpu_to_node(cpu), lfsr_value);
1320
1321 if (unlikely(ret != 0)) {
1322 printk(KERN_ERR
Carl Love9b934182008-12-01 16:18:34 -08001323 "%s: rtas call ibm,cbe-spu-perftools failed, " \
1324 "return = %d\n", __func__, ret);
Bob Nelson14748552007-07-20 21:39:53 +02001325 rtas_error = -EIO;
1326 goto out;
1327 }
1328 }
1329
Carl Love9b934182008-12-01 16:18:34 -08001330 rtas_error = start_spu_profiling_cycles(spu_cycle_reset);
Bob Nelson14748552007-07-20 21:39:53 +02001331 if (rtas_error)
1332 goto out_stop;
1333
1334 oprofile_running = 1;
1335 return 0;
1336
1337out_stop:
Carl Love9b934182008-12-01 16:18:34 -08001338 cell_global_stop_spu_cycles(); /* clean up the PMU/debug bus */
Bob Nelson14748552007-07-20 21:39:53 +02001339out:
1340 return rtas_error;
1341}
1342
Carl Love88382322008-12-01 16:18:36 -08001343static int cell_global_start_spu_events(struct op_counter_config *ctr)
1344{
1345 int cpu;
1346 u32 interrupt_mask = 0;
1347 int rtn = 0;
1348
1349 hdw_thread = 0;
1350
1351 /* spu event profiling, uses the performance counters to generate
1352 * an interrupt. The hardware is setup to store the SPU program
1353 * counter into the trace array. The occurrence mode is used to
1354 * enable storing data to the trace buffer. The bits are set
1355 * to send/store the SPU address in the trace buffer. The debug
1356 * bus must be setup to route the SPU program counter onto the
1357 * debug bus. The occurrence data in the trace buffer is not used.
1358 */
1359
1360 /* This routine gets called once for the system.
1361 * There is one performance monitor per node, so we
1362 * only need to perform this function once per node.
1363 */
1364
1365 for_each_online_cpu(cpu) {
1366 if (cbe_get_hw_thread_id(cpu))
1367 continue;
1368
1369 /*
1370 * Setup SPU event-based profiling.
1371 * Set perf_mon_control bit 0 to a zero before
1372 * enabling spu collection hardware.
1373 *
1374 * Only support one SPU event on one SPU per node.
1375 */
1376 if (ctr_enabled & 1) {
1377 cbe_write_ctr(cpu, 0, reset_value[0]);
1378 enable_ctr(cpu, 0, pm_regs.pm07_cntrl);
1379 interrupt_mask |=
1380 CBE_PM_CTR_OVERFLOW_INTR(0);
1381 } else {
1382 /* Disable counter */
1383 cbe_write_pm07_control(cpu, 0, 0);
1384 }
1385
1386 cbe_get_and_clear_pm_interrupts(cpu);
1387 cbe_enable_pm_interrupts(cpu, hdw_thread, interrupt_mask);
1388 cbe_enable_pm(cpu);
1389
1390 /* clear the trace buffer */
1391 cbe_write_pm(cpu, trace_address, 0);
1392 }
1393
1394 /* Start the timer to time slice collecting the event profile
1395 * on each of the SPUs. Note, can collect profile on one SPU
1396 * per node at a time.
1397 */
1398 start_spu_event_swap();
1399 start_spu_profiling_events();
1400 oprofile_running = 1;
1401 smp_wmb();
1402
1403 return rtn;
1404}
1405
Bob Nelson14748552007-07-20 21:39:53 +02001406static int cell_global_start_ppu(struct op_counter_config *ctr)
1407{
1408 u32 cpu, i;
Maynard Johnson18f21902006-11-20 18:45:16 +01001409 u32 interrupt_mask = 0;
Maynard Johnson18f21902006-11-20 18:45:16 +01001410
1411 /* This routine gets called once for the system.
1412 * There is one performance monitor per node, so we
1413 * only need to perform this function once per node.
1414 */
1415 for_each_online_cpu(cpu) {
1416 if (cbe_get_hw_thread_id(cpu))
1417 continue;
1418
1419 interrupt_mask = 0;
1420
1421 for (i = 0; i < num_counters; ++i) {
1422 if (ctr_enabled & (1 << i)) {
1423 cbe_write_ctr(cpu, i, reset_value[i]);
1424 enable_ctr(cpu, i, pm_regs.pm07_cntrl);
1425 interrupt_mask |=
1426 CBE_PM_CTR_OVERFLOW_INTR(i);
1427 } else {
1428 /* Disable counter */
1429 cbe_write_pm07_control(cpu, i, 0);
1430 }
1431 }
1432
Carl Lovebcb63e22007-02-13 22:02:02 +01001433 cbe_get_and_clear_pm_interrupts(cpu);
Maynard Johnson18f21902006-11-20 18:45:16 +01001434 cbe_enable_pm_interrupts(cpu, hdw_thread, interrupt_mask);
1435 cbe_enable_pm(cpu);
1436 }
1437
1438 virt_cntr_inter_mask = interrupt_mask;
1439 oprofile_running = 1;
1440 smp_wmb();
1441
Bob Nelson14748552007-07-20 21:39:53 +02001442 /*
1443 * NOTE: start_virt_cntrs will result in cell_virtual_cntr() being
1444 * executed which manipulates the PMU. We start the "virtual counter"
Maynard Johnson18f21902006-11-20 18:45:16 +01001445 * here so that we do not need to synchronize access to the PMU in
1446 * the above for-loop.
1447 */
1448 start_virt_cntrs();
Bob Nelson14748552007-07-20 21:39:53 +02001449
1450 return 0;
Maynard Johnson18f21902006-11-20 18:45:16 +01001451}
1452
Bob Nelson14748552007-07-20 21:39:53 +02001453static int cell_global_start(struct op_counter_config *ctr)
1454{
Carl Love9b934182008-12-01 16:18:34 -08001455 if (profiling_mode == SPU_PROFILING_CYCLES)
1456 return cell_global_start_spu_cycles(ctr);
Carl Love88382322008-12-01 16:18:36 -08001457 else if (profiling_mode == SPU_PROFILING_EVENTS)
1458 return cell_global_start_spu_events(ctr);
Bob Nelson14748552007-07-20 21:39:53 +02001459 else
1460 return cell_global_start_ppu(ctr);
1461}
1462
Bob Nelson14748552007-07-20 21:39:53 +02001463
Carl Love88382322008-12-01 16:18:36 -08001464/* The SPU interrupt handler
1465 *
1466 * SPU event profiling works as follows:
1467 * The pm_signal[0] holds the one SPU event to be measured. It is routed on
1468 * the debug bus using word 0 or 1. The value of pm_signal[1] and
1469 * pm_signal[2] contain the necessary events to route the SPU program
1470 * counter for the selected SPU onto the debug bus using words 2 and 3.
1471 * The pm_interval register is setup to write the SPU PC value into the
1472 * trace buffer at the maximum rate possible. The trace buffer is configured
1473 * to store the PCs, wrapping when it is full. The performance counter is
1474 * intialized to the max hardware count minus the number of events, N, between
1475 * samples. Once the N events have occured, a HW counter overflow occurs
1476 * causing the generation of a HW counter interrupt which also stops the
1477 * writing of the SPU PC values to the trace buffer. Hence the last PC
1478 * written to the trace buffer is the SPU PC that we want. Unfortunately,
1479 * we have to read from the beginning of the trace buffer to get to the
1480 * last value written. We just hope the PPU has nothing better to do then
1481 * service this interrupt. The PC for the specific SPU being profiled is
1482 * extracted from the trace buffer processed and stored. The trace buffer
1483 * is cleared, interrupts are cleared, the counter is reset to max - N.
1484 * A kernel timer is used to periodically call the routine spu_evnt_swap()
1485 * to switch to the next physical SPU in the node to profile in round robbin
1486 * order. This way data is collected for all SPUs on the node. It does mean
1487 * that we need to use a relatively small value of N to ensure enough samples
1488 * on each SPU are collected each SPU is being profiled 1/8 of the time.
1489 * It may also be necessary to use a longer sample collection period.
1490 */
1491static void cell_handle_interrupt_spu(struct pt_regs *regs,
1492 struct op_counter_config *ctr)
1493{
1494 u32 cpu, cpu_tmp;
1495 u64 trace_entry;
1496 u32 interrupt_mask;
1497 u64 trace_buffer[2];
1498 u64 last_trace_buffer;
1499 u32 sample;
1500 u32 trace_addr;
1501 unsigned long sample_array_lock_flags;
1502 int spu_num;
1503 unsigned long flags;
1504
1505 /* Make sure spu event interrupt handler and spu event swap
1506 * don't access the counters simultaneously.
1507 */
1508 cpu = smp_processor_id();
1509 spin_lock_irqsave(&cntr_lock, flags);
1510
1511 cpu_tmp = cpu;
1512 cbe_disable_pm(cpu);
1513
1514 interrupt_mask = cbe_get_and_clear_pm_interrupts(cpu);
1515
1516 sample = 0xABCDEF;
1517 trace_entry = 0xfedcba;
1518 last_trace_buffer = 0xdeadbeaf;
1519
1520 if ((oprofile_running == 1) && (interrupt_mask != 0)) {
1521 /* disable writes to trace buff */
1522 cbe_write_pm(cpu, pm_interval, 0);
1523
1524 /* only have one perf cntr being used, cntr 0 */
1525 if ((interrupt_mask & CBE_PM_CTR_OVERFLOW_INTR(0))
1526 && ctr[0].enabled)
1527 /* The SPU PC values will be read
1528 * from the trace buffer, reset counter
1529 */
1530
1531 cbe_write_ctr(cpu, 0, reset_value[0]);
1532
1533 trace_addr = cbe_read_pm(cpu, trace_address);
1534
1535 while (!(trace_addr & CBE_PM_TRACE_BUF_EMPTY)) {
1536 /* There is data in the trace buffer to process
1537 * Read the buffer until you get to the last
1538 * entry. This is the value we want.
1539 */
1540
1541 cbe_read_trace_buffer(cpu, trace_buffer);
1542 trace_addr = cbe_read_pm(cpu, trace_address);
1543 }
1544
1545 /* SPU Address 16 bit count format for 128 bit
1546 * HW trace buffer is used for the SPU PC storage
1547 * HDR bits 0:15
1548 * SPU Addr 0 bits 16:31
1549 * SPU Addr 1 bits 32:47
1550 * unused bits 48:127
1551 *
1552 * HDR: bit4 = 1 SPU Address 0 valid
1553 * HDR: bit5 = 1 SPU Address 1 valid
1554 * - unfortunately, the valid bits don't seem to work
1555 *
1556 * Note trace_buffer[0] holds bits 0:63 of the HW
1557 * trace buffer, trace_buffer[1] holds bits 64:127
1558 */
1559
1560 trace_entry = trace_buffer[0]
1561 & 0x00000000FFFF0000;
1562
1563 /* only top 16 of the 18 bit SPU PC address
1564 * is stored in trace buffer, hence shift right
1565 * by 16 -2 bits */
1566 sample = trace_entry >> 14;
1567 last_trace_buffer = trace_buffer[0];
1568
1569 spu_num = spu_evnt_phys_spu_indx
1570 + (cbe_cpu_to_node(cpu) * NUM_SPUS_PER_NODE);
1571
1572 /* make sure only one process at a time is calling
1573 * spu_sync_buffer()
1574 */
1575 spin_lock_irqsave(&oprof_spu_smpl_arry_lck,
1576 sample_array_lock_flags);
1577 spu_sync_buffer(spu_num, &sample, 1);
1578 spin_unlock_irqrestore(&oprof_spu_smpl_arry_lck,
1579 sample_array_lock_flags);
1580
1581 smp_wmb(); /* insure spu event buffer updates are written
1582 * don't want events intermingled... */
1583
1584 /* The counters were frozen by the interrupt.
1585 * Reenable the interrupt and restart the counters.
1586 */
1587 cbe_write_pm(cpu, pm_interval, NUM_INTERVAL_CYC);
1588 cbe_enable_pm_interrupts(cpu, hdw_thread,
1589 virt_cntr_inter_mask);
1590
1591 /* clear the trace buffer, re-enable writes to trace buff */
1592 cbe_write_pm(cpu, trace_address, 0);
1593 cbe_write_pm(cpu, pm_interval, NUM_INTERVAL_CYC);
1594
1595 /* The writes to the various performance counters only writes
1596 * to a latch. The new values (interrupt setting bits, reset
1597 * counter value etc.) are not copied to the actual registers
1598 * until the performance monitor is enabled. In order to get
1599 * this to work as desired, the permormance monitor needs to
1600 * be disabled while writing to the latches. This is a
1601 * HW design issue.
1602 */
1603 write_pm_cntrl(cpu);
1604 cbe_enable_pm(cpu);
1605 }
1606 spin_unlock_irqrestore(&cntr_lock, flags);
1607}
1608
Carl Love9b934182008-12-01 16:18:34 -08001609static void cell_handle_interrupt_ppu(struct pt_regs *regs,
1610 struct op_counter_config *ctr)
Maynard Johnson18f21902006-11-20 18:45:16 +01001611{
1612 u32 cpu;
1613 u64 pc;
1614 int is_kernel;
1615 unsigned long flags = 0;
1616 u32 interrupt_mask;
1617 int i;
1618
1619 cpu = smp_processor_id();
1620
Bob Nelson14748552007-07-20 21:39:53 +02001621 /*
1622 * Need to make sure the interrupt handler and the virt counter
Maynard Johnson18f21902006-11-20 18:45:16 +01001623 * routine are not running at the same time. See the
1624 * cell_virtual_cntr() routine for additional comments.
1625 */
Carl Love9b934182008-12-01 16:18:34 -08001626 spin_lock_irqsave(&cntr_lock, flags);
Maynard Johnson18f21902006-11-20 18:45:16 +01001627
Bob Nelson14748552007-07-20 21:39:53 +02001628 /*
1629 * Need to disable and reenable the performance counters
Maynard Johnson18f21902006-11-20 18:45:16 +01001630 * to get the desired behavior from the hardware. This
1631 * is hardware specific.
1632 */
1633
1634 cbe_disable_pm(cpu);
1635
Carl Lovebcb63e22007-02-13 22:02:02 +01001636 interrupt_mask = cbe_get_and_clear_pm_interrupts(cpu);
Maynard Johnson18f21902006-11-20 18:45:16 +01001637
Bob Nelson14748552007-07-20 21:39:53 +02001638 /*
1639 * If the interrupt mask has been cleared, then the virt cntr
Maynard Johnson18f21902006-11-20 18:45:16 +01001640 * has cleared the interrupt. When the thread that generated
1641 * the interrupt is restored, the data count will be restored to
1642 * 0xffffff0 to cause the interrupt to be regenerated.
1643 */
1644
1645 if ((oprofile_running == 1) && (interrupt_mask != 0)) {
1646 pc = regs->nip;
1647 is_kernel = is_kernel_addr(pc);
1648
1649 for (i = 0; i < num_counters; ++i) {
1650 if ((interrupt_mask & CBE_PM_CTR_OVERFLOW_INTR(i))
1651 && ctr[i].enabled) {
Bob Nelson101fd462008-02-20 05:00:56 +01001652 oprofile_add_ext_sample(pc, regs, i, is_kernel);
Maynard Johnson18f21902006-11-20 18:45:16 +01001653 cbe_write_ctr(cpu, i, reset_value[i]);
1654 }
1655 }
1656
Bob Nelson14748552007-07-20 21:39:53 +02001657 /*
1658 * The counters were frozen by the interrupt.
Maynard Johnson18f21902006-11-20 18:45:16 +01001659 * Reenable the interrupt and restart the counters.
1660 * If there was a race between the interrupt handler and
Bob Nelson14748552007-07-20 21:39:53 +02001661 * the virtual counter routine. The virutal counter
Maynard Johnson18f21902006-11-20 18:45:16 +01001662 * routine may have cleared the interrupts. Hence must
1663 * use the virt_cntr_inter_mask to re-enable the interrupts.
1664 */
1665 cbe_enable_pm_interrupts(cpu, hdw_thread,
1666 virt_cntr_inter_mask);
1667
Bob Nelson14748552007-07-20 21:39:53 +02001668 /*
1669 * The writes to the various performance counters only writes
1670 * to a latch. The new values (interrupt setting bits, reset
Maynard Johnson18f21902006-11-20 18:45:16 +01001671 * counter value etc.) are not copied to the actual registers
1672 * until the performance monitor is enabled. In order to get
1673 * this to work as desired, the permormance monitor needs to
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02001674 * be disabled while writing to the latches. This is a
Maynard Johnson18f21902006-11-20 18:45:16 +01001675 * HW design issue.
1676 */
1677 cbe_enable_pm(cpu);
1678 }
Carl Love9b934182008-12-01 16:18:34 -08001679 spin_unlock_irqrestore(&cntr_lock, flags);
1680}
1681
1682static void cell_handle_interrupt(struct pt_regs *regs,
1683 struct op_counter_config *ctr)
1684{
1685 if (profiling_mode == PPU_PROFILING)
1686 cell_handle_interrupt_ppu(regs, ctr);
Carl Love88382322008-12-01 16:18:36 -08001687 else
1688 cell_handle_interrupt_spu(regs, ctr);
Maynard Johnson18f21902006-11-20 18:45:16 +01001689}
1690
Bob Nelson14748552007-07-20 21:39:53 +02001691/*
1692 * This function is called from the generic OProfile
1693 * driver. When profiling PPUs, we need to do the
1694 * generic sync start; otherwise, do spu_sync_start.
1695 */
1696static int cell_sync_start(void)
1697{
Carl Love9b934182008-12-01 16:18:34 -08001698 if ((profiling_mode == SPU_PROFILING_CYCLES) ||
1699 (profiling_mode == SPU_PROFILING_EVENTS))
Bob Nelson14748552007-07-20 21:39:53 +02001700 return spu_sync_start();
1701 else
1702 return DO_GENERIC_SYNC;
1703}
1704
1705static int cell_sync_stop(void)
1706{
Carl Love9b934182008-12-01 16:18:34 -08001707 if ((profiling_mode == SPU_PROFILING_CYCLES) ||
1708 (profiling_mode == SPU_PROFILING_EVENTS))
Bob Nelson14748552007-07-20 21:39:53 +02001709 return spu_sync_stop();
1710 else
1711 return 1;
1712}
1713
Maynard Johnson18f21902006-11-20 18:45:16 +01001714struct op_powerpc_model op_model_cell = {
1715 .reg_setup = cell_reg_setup,
1716 .cpu_setup = cell_cpu_setup,
1717 .global_start = cell_global_start,
1718 .global_stop = cell_global_stop,
Bob Nelson14748552007-07-20 21:39:53 +02001719 .sync_start = cell_sync_start,
1720 .sync_stop = cell_sync_stop,
Maynard Johnson18f21902006-11-20 18:45:16 +01001721 .handle_interrupt = cell_handle_interrupt,
1722};