blob: 22ac732b1f89b58f822dae512334523bcbee4140 [file] [log] [blame]
Kevin Corryd8bf96e2006-10-24 18:31:22 +02001/*
2 * Cell Broadband Engine Performance Monitor
3 *
4 * (C) Copyright IBM Corporation 2001,2006
5 *
6 * Author:
7 * David Erb (djerb@us.ibm.com)
8 * Kevin Corry (kevcorry@us.ibm.com)
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/types.h>
26#include <asm/io.h>
27#include <asm/machdep.h>
28#include <asm/reg.h>
29#include <asm/spu.h>
30
31#include "cbe_regs.h"
32#include "interrupt.h"
33#include "pmu.h"
34
35/*
36 * When writing to write-only mmio addresses, save a shadow copy. All of the
37 * registers are 32-bit, but stored in the upper-half of a 64-bit field in
38 * pmd_regs.
39 */
40
41#define WRITE_WO_MMIO(reg, x) \
42 do { \
43 u32 _x = (x); \
44 struct cbe_pmd_regs __iomem *pmd_regs; \
45 struct cbe_pmd_shadow_regs *shadow_regs; \
46 pmd_regs = cbe_get_cpu_pmd_regs(cpu); \
47 shadow_regs = cbe_get_cpu_pmd_shadow_regs(cpu); \
48 out_be64(&(pmd_regs->reg), (((u64)_x) << 32)); \
49 shadow_regs->reg = _x; \
50 } while (0)
51
52#define READ_SHADOW_REG(val, reg) \
53 do { \
54 struct cbe_pmd_shadow_regs *shadow_regs; \
55 shadow_regs = cbe_get_cpu_pmd_shadow_regs(cpu); \
56 (val) = shadow_regs->reg; \
57 } while (0)
58
59#define READ_MMIO_UPPER32(val, reg) \
60 do { \
61 struct cbe_pmd_regs __iomem *pmd_regs; \
62 pmd_regs = cbe_get_cpu_pmd_regs(cpu); \
63 (val) = (u32)(in_be64(&pmd_regs->reg) >> 32); \
64 } while (0)
65
66/*
67 * Physical counter registers.
68 * Each physical counter can act as one 32-bit counter or two 16-bit counters.
69 */
70
71u32 cbe_read_phys_ctr(u32 cpu, u32 phys_ctr)
72{
73 u32 val_in_latch, val = 0;
74
75 if (phys_ctr < NR_PHYS_CTRS) {
76 READ_SHADOW_REG(val_in_latch, counter_value_in_latch);
77
78 /* Read the latch or the actual counter, whichever is newer. */
79 if (val_in_latch & (1 << phys_ctr)) {
80 READ_SHADOW_REG(val, pm_ctr[phys_ctr]);
81 } else {
82 READ_MMIO_UPPER32(val, pm_ctr[phys_ctr]);
83 }
84 }
85
86 return val;
87}
Arnd Bergmann5231800c2006-11-20 18:45:12 +010088EXPORT_SYMBOL_GPL(cbe_read_phys_ctr);
Kevin Corryd8bf96e2006-10-24 18:31:22 +020089
90void cbe_write_phys_ctr(u32 cpu, u32 phys_ctr, u32 val)
91{
92 struct cbe_pmd_shadow_regs *shadow_regs;
93 u32 pm_ctrl;
94
95 if (phys_ctr < NR_PHYS_CTRS) {
96 /* Writing to a counter only writes to a hardware latch.
97 * The new value is not propagated to the actual counter
98 * until the performance monitor is enabled.
99 */
100 WRITE_WO_MMIO(pm_ctr[phys_ctr], val);
101
102 pm_ctrl = cbe_read_pm(cpu, pm_control);
103 if (pm_ctrl & CBE_PM_ENABLE_PERF_MON) {
104 /* The counters are already active, so we need to
105 * rewrite the pm_control register to "re-enable"
106 * the PMU.
107 */
108 cbe_write_pm(cpu, pm_control, pm_ctrl);
109 } else {
110 shadow_regs = cbe_get_cpu_pmd_shadow_regs(cpu);
111 shadow_regs->counter_value_in_latch |= (1 << phys_ctr);
112 }
113 }
114}
Arnd Bergmann5231800c2006-11-20 18:45:12 +0100115EXPORT_SYMBOL_GPL(cbe_write_phys_ctr);
Kevin Corryd8bf96e2006-10-24 18:31:22 +0200116
117/*
118 * "Logical" counter registers.
119 * These will read/write 16-bits or 32-bits depending on the
120 * current size of the counter. Counters 4 - 7 are always 16-bit.
121 */
122
123u32 cbe_read_ctr(u32 cpu, u32 ctr)
124{
125 u32 val;
126 u32 phys_ctr = ctr & (NR_PHYS_CTRS - 1);
127
128 val = cbe_read_phys_ctr(cpu, phys_ctr);
129
130 if (cbe_get_ctr_size(cpu, phys_ctr) == 16)
131 val = (ctr < NR_PHYS_CTRS) ? (val >> 16) : (val & 0xffff);
132
133 return val;
134}
Arnd Bergmann5231800c2006-11-20 18:45:12 +0100135EXPORT_SYMBOL_GPL(cbe_read_ctr);
Kevin Corryd8bf96e2006-10-24 18:31:22 +0200136
137void cbe_write_ctr(u32 cpu, u32 ctr, u32 val)
138{
139 u32 phys_ctr;
140 u32 phys_val;
141
142 phys_ctr = ctr & (NR_PHYS_CTRS - 1);
143
144 if (cbe_get_ctr_size(cpu, phys_ctr) == 16) {
145 phys_val = cbe_read_phys_ctr(cpu, phys_ctr);
146
147 if (ctr < NR_PHYS_CTRS)
148 val = (val << 16) | (phys_val & 0xffff);
149 else
150 val = (val & 0xffff) | (phys_val & 0xffff0000);
151 }
152
153 cbe_write_phys_ctr(cpu, phys_ctr, val);
154}
Arnd Bergmann5231800c2006-11-20 18:45:12 +0100155EXPORT_SYMBOL_GPL(cbe_write_ctr);
Kevin Corryd8bf96e2006-10-24 18:31:22 +0200156
157/*
158 * Counter-control registers.
159 * Each "logical" counter has a corresponding control register.
160 */
161
162u32 cbe_read_pm07_control(u32 cpu, u32 ctr)
163{
164 u32 pm07_control = 0;
165
166 if (ctr < NR_CTRS)
167 READ_SHADOW_REG(pm07_control, pm07_control[ctr]);
168
169 return pm07_control;
170}
Arnd Bergmann5231800c2006-11-20 18:45:12 +0100171EXPORT_SYMBOL_GPL(cbe_read_pm07_control);
Kevin Corryd8bf96e2006-10-24 18:31:22 +0200172
173void cbe_write_pm07_control(u32 cpu, u32 ctr, u32 val)
174{
175 if (ctr < NR_CTRS)
176 WRITE_WO_MMIO(pm07_control[ctr], val);
177}
Arnd Bergmann5231800c2006-11-20 18:45:12 +0100178EXPORT_SYMBOL_GPL(cbe_write_pm07_control);
Kevin Corryd8bf96e2006-10-24 18:31:22 +0200179
180/*
181 * Other PMU control registers. Most of these are write-only.
182 */
183
184u32 cbe_read_pm(u32 cpu, enum pm_reg_name reg)
185{
186 u32 val = 0;
187
188 switch (reg) {
189 case group_control:
190 READ_SHADOW_REG(val, group_control);
191 break;
192
193 case debug_bus_control:
194 READ_SHADOW_REG(val, debug_bus_control);
195 break;
196
197 case trace_address:
198 READ_MMIO_UPPER32(val, trace_address);
199 break;
200
201 case ext_tr_timer:
202 READ_SHADOW_REG(val, ext_tr_timer);
203 break;
204
205 case pm_status:
206 READ_MMIO_UPPER32(val, pm_status);
207 break;
208
209 case pm_control:
210 READ_SHADOW_REG(val, pm_control);
211 break;
212
213 case pm_interval:
214 READ_SHADOW_REG(val, pm_interval);
215 break;
216
217 case pm_start_stop:
218 READ_SHADOW_REG(val, pm_start_stop);
219 break;
220 }
221
222 return val;
223}
Arnd Bergmann5231800c2006-11-20 18:45:12 +0100224EXPORT_SYMBOL_GPL(cbe_read_pm);
Kevin Corryd8bf96e2006-10-24 18:31:22 +0200225
226void cbe_write_pm(u32 cpu, enum pm_reg_name reg, u32 val)
227{
228 switch (reg) {
229 case group_control:
230 WRITE_WO_MMIO(group_control, val);
231 break;
232
233 case debug_bus_control:
234 WRITE_WO_MMIO(debug_bus_control, val);
235 break;
236
237 case trace_address:
238 WRITE_WO_MMIO(trace_address, val);
239 break;
240
241 case ext_tr_timer:
242 WRITE_WO_MMIO(ext_tr_timer, val);
243 break;
244
245 case pm_status:
246 WRITE_WO_MMIO(pm_status, val);
247 break;
248
249 case pm_control:
250 WRITE_WO_MMIO(pm_control, val);
251 break;
252
253 case pm_interval:
254 WRITE_WO_MMIO(pm_interval, val);
255 break;
256
257 case pm_start_stop:
258 WRITE_WO_MMIO(pm_start_stop, val);
259 break;
260 }
261}
Arnd Bergmann5231800c2006-11-20 18:45:12 +0100262EXPORT_SYMBOL_GPL(cbe_write_pm);
Kevin Corryd8bf96e2006-10-24 18:31:22 +0200263
264/*
265 * Get/set the size of a physical counter to either 16 or 32 bits.
266 */
267
268u32 cbe_get_ctr_size(u32 cpu, u32 phys_ctr)
269{
270 u32 pm_ctrl, size = 0;
271
272 if (phys_ctr < NR_PHYS_CTRS) {
273 pm_ctrl = cbe_read_pm(cpu, pm_control);
274 size = (pm_ctrl & CBE_PM_16BIT_CTR(phys_ctr)) ? 16 : 32;
275 }
276
277 return size;
278}
Arnd Bergmann5231800c2006-11-20 18:45:12 +0100279EXPORT_SYMBOL_GPL(cbe_get_ctr_size);
Kevin Corryd8bf96e2006-10-24 18:31:22 +0200280
281void cbe_set_ctr_size(u32 cpu, u32 phys_ctr, u32 ctr_size)
282{
283 u32 pm_ctrl;
284
285 if (phys_ctr < NR_PHYS_CTRS) {
286 pm_ctrl = cbe_read_pm(cpu, pm_control);
287 switch (ctr_size) {
288 case 16:
289 pm_ctrl |= CBE_PM_16BIT_CTR(phys_ctr);
290 break;
291
292 case 32:
293 pm_ctrl &= ~CBE_PM_16BIT_CTR(phys_ctr);
294 break;
295 }
296 cbe_write_pm(cpu, pm_control, pm_ctrl);
297 }
298}
Arnd Bergmann5231800c2006-11-20 18:45:12 +0100299EXPORT_SYMBOL_GPL(cbe_set_ctr_size);
Kevin Corryd8bf96e2006-10-24 18:31:22 +0200300
301/*
302 * Enable/disable the entire performance monitoring unit.
303 * When we enable the PMU, all pending writes to counters get committed.
304 */
305
306void cbe_enable_pm(u32 cpu)
307{
308 struct cbe_pmd_shadow_regs *shadow_regs;
309 u32 pm_ctrl;
310
311 shadow_regs = cbe_get_cpu_pmd_shadow_regs(cpu);
312 shadow_regs->counter_value_in_latch = 0;
313
314 pm_ctrl = cbe_read_pm(cpu, pm_control) | CBE_PM_ENABLE_PERF_MON;
315 cbe_write_pm(cpu, pm_control, pm_ctrl);
316}
Arnd Bergmann5231800c2006-11-20 18:45:12 +0100317EXPORT_SYMBOL_GPL(cbe_enable_pm);
Kevin Corryd8bf96e2006-10-24 18:31:22 +0200318
319void cbe_disable_pm(u32 cpu)
320{
321 u32 pm_ctrl;
322 pm_ctrl = cbe_read_pm(cpu, pm_control) & ~CBE_PM_ENABLE_PERF_MON;
323 cbe_write_pm(cpu, pm_control, pm_ctrl);
324}
Arnd Bergmann5231800c2006-11-20 18:45:12 +0100325EXPORT_SYMBOL_GPL(cbe_disable_pm);
Kevin Corryd8bf96e2006-10-24 18:31:22 +0200326
327/*
328 * Reading from the trace_buffer.
329 * The trace buffer is two 64-bit registers. Reading from
330 * the second half automatically increments the trace_address.
331 */
332
333void cbe_read_trace_buffer(u32 cpu, u64 *buf)
334{
335 struct cbe_pmd_regs __iomem *pmd_regs = cbe_get_cpu_pmd_regs(cpu);
336
337 *buf++ = in_be64(&pmd_regs->trace_buffer_0_63);
338 *buf++ = in_be64(&pmd_regs->trace_buffer_64_127);
339}
Arnd Bergmann5231800c2006-11-20 18:45:12 +0100340EXPORT_SYMBOL_GPL(cbe_read_trace_buffer);
Kevin Corryd8bf96e2006-10-24 18:31:22 +0200341