2 * @file op_model_xscale.c
3 * XScale Performance Monitor Driver
5 * @remark Copyright 2000-2004 Deepak Saxena <dsaxena@mvista.com>
6 * @remark Copyright 2000-2004 MontaVista Software Inc
7 * @remark Copyright 2004 Dave Jiang <dave.jiang@intel.com>
8 * @remark Copyright 2004 Intel Corporation
9 * @remark Copyright 2004 Zwane Mwaikambo <zwane@arm.linux.org.uk>
10 * @remark Copyright 2004 OProfile Authors
12 * @remark Read the file COPYING
14 * @author Zwane Mwaikambo
18 #include <linux/types.h>
19 #include <linux/errno.h>
20 #include <linux/sched.h>
21 #include <linux/oprofile.h>
22 #include <linux/interrupt.h>
24 #include <asm/system.h>
26 #include "op_counter.h"
27 #include "op_arm_model.h"
29 #define PMU_ENABLE 0x001 /* Enable counters */
30 #define PMN_RESET 0x002 /* Reset event counters */
31 #define CCNT_RESET 0x004 /* Reset clock counter */
32 #define PMU_RESET (CCNT_RESET | PMN_RESET)
33 #define PMU_CNT64 0x008 /* Make CCNT count every 64th cycle */
35 /* TODO do runtime detection */
36 #ifdef CONFIG_ARCH_IOP310
37 #define XSCALE_PMU_IRQ IRQ_XS80200_PMU
39 #ifdef CONFIG_ARCH_IOP321
40 #define XSCALE_PMU_IRQ IRQ_IOP321_CORE_PMU
42 #ifdef CONFIG_ARCH_IOP331
43 #define XSCALE_PMU_IRQ IRQ_IOP331_CORE_PMU
45 #ifdef CONFIG_ARCH_PXA
46 #define XSCALE_PMU_IRQ IRQ_PMU
50 * Different types of events that can be counted by the XScale PMU
51 * as used by Oprofile userspace. Here primarily for documentation
55 #define EVT_ICACHE_MISS 0x00
56 #define EVT_ICACHE_NO_DELIVER 0x01
57 #define EVT_DATA_STALL 0x02
58 #define EVT_ITLB_MISS 0x03
59 #define EVT_DTLB_MISS 0x04
60 #define EVT_BRANCH 0x05
61 #define EVT_BRANCH_MISS 0x06
62 #define EVT_INSTRUCTION 0x07
63 #define EVT_DCACHE_FULL_STALL 0x08
64 #define EVT_DCACHE_FULL_STALL_CONTIG 0x09
65 #define EVT_DCACHE_ACCESS 0x0A
66 #define EVT_DCACHE_MISS 0x0B
67 #define EVT_DCACE_WRITE_BACK 0x0C
68 #define EVT_PC_CHANGED 0x0D
69 #define EVT_BCU_REQUEST 0x10
70 #define EVT_BCU_FULL 0x11
71 #define EVT_BCU_DRAIN 0x12
72 #define EVT_BCU_ECC_NO_ELOG 0x14
73 #define EVT_BCU_1_BIT_ERR 0x15
75 /* EVT_CCNT is not hardware defined */
77 #define EVT_UNUSED 0xFF
80 volatile unsigned long ovf;
81 unsigned long reset_counter;
84 enum { CCNT, PMN0, PMN1, PMN2, PMN3, MAX_COUNTERS };
86 static struct pmu_counter results[MAX_COUNTERS];
89 * There are two versions of the PMU in current XScale processors
90 * with differing register layouts and number of performance counters.
91 * e.g. IOP321 is xsc1 whilst IOP331 is xsc2.
92 * We detect which register layout to use in xscale_detect_pmu()
94 enum { PMU_XSC1, PMU_XSC2 };
100 unsigned int int_enable;
101 unsigned int cnt_ovf[MAX_COUNTERS];
102 unsigned int int_mask[MAX_COUNTERS];
105 static struct pmu_type pmu_parms[] = {
108 .name = "arm/xscale1",
110 .int_mask = { [PMN0] = 0x10, [PMN1] = 0x20,
112 .cnt_ovf = { [CCNT] = 0x400, [PMN0] = 0x100,
117 .name = "arm/xscale2",
119 .int_mask = { [CCNT] = 0x01, [PMN0] = 0x02,
120 [PMN1] = 0x04, [PMN2] = 0x08,
122 .cnt_ovf = { [CCNT] = 0x01, [PMN0] = 0x02,
123 [PMN1] = 0x04, [PMN2] = 0x08,
128 static struct pmu_type *pmu;
130 static void write_pmnc(u32 val)
132 if (pmu->id == PMU_XSC1) {
133 /* upper 4bits and 7, 11 are write-as-0 */
135 __asm__ __volatile__ ("mcr p14, 0, %0, c0, c0, 0" : : "r" (val));
137 /* bits 4-23 are write-as-0, 24-31 are write ignored */
139 __asm__ __volatile__ ("mcr p14, 0, %0, c0, c1, 0" : : "r" (val));
143 static u32 read_pmnc(void)
147 if (pmu->id == PMU_XSC1)
148 __asm__ __volatile__ ("mrc p14, 0, %0, c0, c0, 0" : "=r" (val));
150 __asm__ __volatile__ ("mrc p14, 0, %0, c0, c1, 0" : "=r" (val));
151 /* bits 1-2 and 4-23 are read-unpredictable */
158 static u32 __xsc1_read_counter(int counter)
164 __asm__ __volatile__ ("mrc p14, 0, %0, c1, c0, 0" : "=r" (val));
167 __asm__ __volatile__ ("mrc p14, 0, %0, c2, c0, 0" : "=r" (val));
170 __asm__ __volatile__ ("mrc p14, 0, %0, c3, c0, 0" : "=r" (val));
176 static u32 __xsc2_read_counter(int counter)
182 __asm__ __volatile__ ("mrc p14, 0, %0, c1, c1, 0" : "=r" (val));
185 __asm__ __volatile__ ("mrc p14, 0, %0, c0, c2, 0" : "=r" (val));
188 __asm__ __volatile__ ("mrc p14, 0, %0, c1, c2, 0" : "=r" (val));
191 __asm__ __volatile__ ("mrc p14, 0, %0, c2, c2, 0" : "=r" (val));
194 __asm__ __volatile__ ("mrc p14, 0, %0, c3, c2, 0" : "=r" (val));
200 static u32 read_counter(int counter)
204 if (pmu->id == PMU_XSC1)
205 val = __xsc1_read_counter(counter);
207 val = __xsc2_read_counter(counter);
212 static void __xsc1_write_counter(int counter, u32 val)
216 __asm__ __volatile__ ("mcr p14, 0, %0, c1, c0, 0" : : "r" (val));
219 __asm__ __volatile__ ("mcr p14, 0, %0, c2, c0, 0" : : "r" (val));
222 __asm__ __volatile__ ("mcr p14, 0, %0, c3, c0, 0" : : "r" (val));
227 static void __xsc2_write_counter(int counter, u32 val)
231 __asm__ __volatile__ ("mcr p14, 0, %0, c1, c1, 0" : : "r" (val));
234 __asm__ __volatile__ ("mcr p14, 0, %0, c0, c2, 0" : : "r" (val));
237 __asm__ __volatile__ ("mcr p14, 0, %0, c1, c2, 0" : : "r" (val));
240 __asm__ __volatile__ ("mcr p14, 0, %0, c2, c2, 0" : : "r" (val));
243 __asm__ __volatile__ ("mcr p14, 0, %0, c3, c2, 0" : : "r" (val));
248 static void write_counter(int counter, u32 val)
250 if (pmu->id == PMU_XSC1)
251 __xsc1_write_counter(counter, val);
253 __xsc2_write_counter(counter, val);
256 static int xscale_setup_ctrs(void)
261 for (i = CCNT; i < MAX_COUNTERS; i++) {
262 if (counter_config[i].enabled)
265 counter_config[i].event = EVT_UNUSED;
270 pmnc = (counter_config[PMN1].event << 20) | (counter_config[PMN0].event << 12);
271 pr_debug("xscale_setup_ctrs: pmnc: %#08x\n", pmnc);
276 evtsel = counter_config[PMN0].event | (counter_config[PMN1].event << 8) |
277 (counter_config[PMN2].event << 16) | (counter_config[PMN3].event << 24);
279 pr_debug("xscale_setup_ctrs: evtsel %#08x\n", evtsel);
280 __asm__ __volatile__ ("mcr p14, 0, %0, c8, c1, 0" : : "r" (evtsel));
284 for (i = CCNT; i < MAX_COUNTERS; i++) {
285 if (counter_config[i].event == EVT_UNUSED) {
286 counter_config[i].event = 0;
287 pmu->int_enable &= ~pmu->int_mask[i];
291 results[i].reset_counter = counter_config[i].count;
292 write_counter(i, -(u32)counter_config[i].count);
293 pmu->int_enable |= pmu->int_mask[i];
294 pr_debug("xscale_setup_ctrs: counter%d %#08x from %#08lx\n", i,
295 read_counter(i), counter_config[i].count);
301 static void inline __xsc1_check_ctrs(void)
304 u32 pmnc = read_pmnc();
306 /* NOTE: there's an A stepping errata that states if an overflow */
307 /* bit already exists and another occurs, the previous */
308 /* Overflow bit gets cleared. There's no workaround. */
309 /* Fixed in B stepping or later */
311 /* Write the value back to clear the overflow flags. Overflow */
312 /* flags remain in pmnc for use below */
313 write_pmnc(pmnc & ~PMU_ENABLE);
315 for (i = CCNT; i <= PMN1; i++) {
316 if (!(pmu->int_mask[i] & pmu->int_enable))
319 if (pmnc & pmu->cnt_ovf[i])
324 static void inline __xsc2_check_ctrs(void)
327 u32 flag = 0, pmnc = read_pmnc();
332 /* read overflow flag register */
333 __asm__ __volatile__ ("mrc p14, 0, %0, c5, c1, 0" : "=r" (flag));
335 for (i = CCNT; i <= PMN3; i++) {
336 if (!(pmu->int_mask[i] & pmu->int_enable))
339 if (flag & pmu->cnt_ovf[i])
343 /* writeback clears overflow bits */
344 __asm__ __volatile__ ("mcr p14, 0, %0, c5, c1, 0" : : "r" (flag));
347 static irqreturn_t xscale_pmu_interrupt(int irq, void *arg, struct pt_regs *regs)
352 if (pmu->id == PMU_XSC1)
357 for (i = CCNT; i < MAX_COUNTERS; i++) {
361 write_counter(i, -(u32)results[i].reset_counter);
362 oprofile_add_sample(regs, i);
366 pmnc = read_pmnc() | PMU_ENABLE;
372 static void xscale_pmu_stop(void)
374 u32 pmnc = read_pmnc();
379 free_irq(XSCALE_PMU_IRQ, results);
382 static int xscale_pmu_start(void)
385 u32 pmnc = read_pmnc();
387 ret = request_irq(XSCALE_PMU_IRQ, xscale_pmu_interrupt, SA_INTERRUPT,
388 "XScale PMU", (void *)results);
391 printk(KERN_ERR "oprofile: unable to request IRQ%d for XScale PMU\n",
396 if (pmu->id == PMU_XSC1)
397 pmnc |= pmu->int_enable;
399 __asm__ __volatile__ ("mcr p14, 0, %0, c4, c1, 0" : : "r" (pmu->int_enable));
405 pr_debug("xscale_pmu_start: pmnc: %#08x mask: %08x\n", pmnc, pmu->int_enable);
409 static int xscale_detect_pmu(void)
414 id = (read_cpuid(CPUID_ID) >> 13) & 0x7;
418 pmu = &pmu_parms[PMU_XSC1];
421 pmu = &pmu_parms[PMU_XSC2];
429 op_xscale_spec.name = pmu->name;
430 op_xscale_spec.num_counters = pmu->num_counters;
431 pr_debug("xscale_detect_pmu: detected %s PMU\n", pmu->name);
437 struct op_arm_model_spec op_xscale_spec = {
438 .init = xscale_detect_pmu,
439 .setup_ctrs = xscale_setup_ctrs,
440 .start = xscale_pmu_start,
441 .stop = xscale_pmu_stop,