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>
23 #include <linux/irq.h>
25 #include <asm/cputype.h>
27 #include "op_counter.h"
28 #include "op_arm_model.h"
30 #define PMU_ENABLE 0x001 /* Enable counters */
31 #define PMN_RESET 0x002 /* Reset event counters */
32 #define CCNT_RESET 0x004 /* Reset clock counter */
33 #define PMU_RESET (CCNT_RESET | PMN_RESET)
34 #define PMU_CNT64 0x008 /* Make CCNT count every 64th cycle */
36 /* TODO do runtime detection */
37 #ifdef CONFIG_ARCH_IOP32X
38 #define XSCALE_PMU_IRQ IRQ_IOP32X_CORE_PMU
40 #ifdef CONFIG_ARCH_IOP33X
41 #define XSCALE_PMU_IRQ IRQ_IOP33X_CORE_PMU
43 #ifdef CONFIG_ARCH_PXA
44 #define XSCALE_PMU_IRQ IRQ_PMU
48 * Different types of events that can be counted by the XScale PMU
49 * as used by Oprofile userspace. Here primarily for documentation
53 #define EVT_ICACHE_MISS 0x00
54 #define EVT_ICACHE_NO_DELIVER 0x01
55 #define EVT_DATA_STALL 0x02
56 #define EVT_ITLB_MISS 0x03
57 #define EVT_DTLB_MISS 0x04
58 #define EVT_BRANCH 0x05
59 #define EVT_BRANCH_MISS 0x06
60 #define EVT_INSTRUCTION 0x07
61 #define EVT_DCACHE_FULL_STALL 0x08
62 #define EVT_DCACHE_FULL_STALL_CONTIG 0x09
63 #define EVT_DCACHE_ACCESS 0x0A
64 #define EVT_DCACHE_MISS 0x0B
65 #define EVT_DCACE_WRITE_BACK 0x0C
66 #define EVT_PC_CHANGED 0x0D
67 #define EVT_BCU_REQUEST 0x10
68 #define EVT_BCU_FULL 0x11
69 #define EVT_BCU_DRAIN 0x12
70 #define EVT_BCU_ECC_NO_ELOG 0x14
71 #define EVT_BCU_1_BIT_ERR 0x15
73 /* EVT_CCNT is not hardware defined */
75 #define EVT_UNUSED 0xFF
78 volatile unsigned long ovf;
79 unsigned long reset_counter;
82 enum { CCNT, PMN0, PMN1, PMN2, PMN3, MAX_COUNTERS };
84 static struct pmu_counter results[MAX_COUNTERS];
87 * There are two versions of the PMU in current XScale processors
88 * with differing register layouts and number of performance counters.
89 * e.g. IOP32x is xsc1 whilst IOP33x is xsc2.
90 * We detect which register layout to use in xscale_detect_pmu()
92 enum { PMU_XSC1, PMU_XSC2 };
98 unsigned int int_enable;
99 unsigned int cnt_ovf[MAX_COUNTERS];
100 unsigned int int_mask[MAX_COUNTERS];
103 static struct pmu_type pmu_parms[] = {
106 .name = "arm/xscale1",
108 .int_mask = { [PMN0] = 0x10, [PMN1] = 0x20,
110 .cnt_ovf = { [CCNT] = 0x400, [PMN0] = 0x100,
115 .name = "arm/xscale2",
117 .int_mask = { [CCNT] = 0x01, [PMN0] = 0x02,
118 [PMN1] = 0x04, [PMN2] = 0x08,
120 .cnt_ovf = { [CCNT] = 0x01, [PMN0] = 0x02,
121 [PMN1] = 0x04, [PMN2] = 0x08,
126 static struct pmu_type *pmu;
128 static void write_pmnc(u32 val)
130 if (pmu->id == PMU_XSC1) {
131 /* upper 4bits and 7, 11 are write-as-0 */
133 __asm__ __volatile__ ("mcr p14, 0, %0, c0, c0, 0" : : "r" (val));
135 /* bits 4-23 are write-as-0, 24-31 are write ignored */
137 __asm__ __volatile__ ("mcr p14, 0, %0, c0, c1, 0" : : "r" (val));
141 static u32 read_pmnc(void)
145 if (pmu->id == PMU_XSC1)
146 __asm__ __volatile__ ("mrc p14, 0, %0, c0, c0, 0" : "=r" (val));
148 __asm__ __volatile__ ("mrc p14, 0, %0, c0, c1, 0" : "=r" (val));
149 /* bits 1-2 and 4-23 are read-unpredictable */
156 static u32 __xsc1_read_counter(int counter)
162 __asm__ __volatile__ ("mrc p14, 0, %0, c1, c0, 0" : "=r" (val));
165 __asm__ __volatile__ ("mrc p14, 0, %0, c2, c0, 0" : "=r" (val));
168 __asm__ __volatile__ ("mrc p14, 0, %0, c3, c0, 0" : "=r" (val));
174 static u32 __xsc2_read_counter(int counter)
180 __asm__ __volatile__ ("mrc p14, 0, %0, c1, c1, 0" : "=r" (val));
183 __asm__ __volatile__ ("mrc p14, 0, %0, c0, c2, 0" : "=r" (val));
186 __asm__ __volatile__ ("mrc p14, 0, %0, c1, c2, 0" : "=r" (val));
189 __asm__ __volatile__ ("mrc p14, 0, %0, c2, c2, 0" : "=r" (val));
192 __asm__ __volatile__ ("mrc p14, 0, %0, c3, c2, 0" : "=r" (val));
198 static u32 read_counter(int counter)
202 if (pmu->id == PMU_XSC1)
203 val = __xsc1_read_counter(counter);
205 val = __xsc2_read_counter(counter);
210 static void __xsc1_write_counter(int counter, u32 val)
214 __asm__ __volatile__ ("mcr p14, 0, %0, c1, c0, 0" : : "r" (val));
217 __asm__ __volatile__ ("mcr p14, 0, %0, c2, c0, 0" : : "r" (val));
220 __asm__ __volatile__ ("mcr p14, 0, %0, c3, c0, 0" : : "r" (val));
225 static void __xsc2_write_counter(int counter, u32 val)
229 __asm__ __volatile__ ("mcr p14, 0, %0, c1, c1, 0" : : "r" (val));
232 __asm__ __volatile__ ("mcr p14, 0, %0, c0, c2, 0" : : "r" (val));
235 __asm__ __volatile__ ("mcr p14, 0, %0, c1, c2, 0" : : "r" (val));
238 __asm__ __volatile__ ("mcr p14, 0, %0, c2, c2, 0" : : "r" (val));
241 __asm__ __volatile__ ("mcr p14, 0, %0, c3, c2, 0" : : "r" (val));
246 static void write_counter(int counter, u32 val)
248 if (pmu->id == PMU_XSC1)
249 __xsc1_write_counter(counter, val);
251 __xsc2_write_counter(counter, val);
254 static int xscale_setup_ctrs(void)
259 for (i = CCNT; i < MAX_COUNTERS; i++) {
260 if (counter_config[i].enabled)
263 counter_config[i].event = EVT_UNUSED;
268 pmnc = (counter_config[PMN1].event << 20) | (counter_config[PMN0].event << 12);
269 pr_debug("xscale_setup_ctrs: pmnc: %#08x\n", pmnc);
274 evtsel = counter_config[PMN0].event | (counter_config[PMN1].event << 8) |
275 (counter_config[PMN2].event << 16) | (counter_config[PMN3].event << 24);
277 pr_debug("xscale_setup_ctrs: evtsel %#08x\n", evtsel);
278 __asm__ __volatile__ ("mcr p14, 0, %0, c8, c1, 0" : : "r" (evtsel));
282 for (i = CCNT; i < MAX_COUNTERS; i++) {
283 if (counter_config[i].event == EVT_UNUSED) {
284 counter_config[i].event = 0;
285 pmu->int_enable &= ~pmu->int_mask[i];
289 results[i].reset_counter = counter_config[i].count;
290 write_counter(i, -(u32)counter_config[i].count);
291 pmu->int_enable |= pmu->int_mask[i];
292 pr_debug("xscale_setup_ctrs: counter%d %#08x from %#08lx\n", i,
293 read_counter(i), counter_config[i].count);
299 static void inline __xsc1_check_ctrs(void)
302 u32 pmnc = read_pmnc();
304 /* NOTE: there's an A stepping errata that states if an overflow */
305 /* bit already exists and another occurs, the previous */
306 /* Overflow bit gets cleared. There's no workaround. */
307 /* Fixed in B stepping or later */
309 /* Write the value back to clear the overflow flags. Overflow */
310 /* flags remain in pmnc for use below */
311 write_pmnc(pmnc & ~PMU_ENABLE);
313 for (i = CCNT; i <= PMN1; i++) {
314 if (!(pmu->int_mask[i] & pmu->int_enable))
317 if (pmnc & pmu->cnt_ovf[i])
322 static void inline __xsc2_check_ctrs(void)
325 u32 flag = 0, pmnc = read_pmnc();
330 /* read overflow flag register */
331 __asm__ __volatile__ ("mrc p14, 0, %0, c5, c1, 0" : "=r" (flag));
333 for (i = CCNT; i <= PMN3; i++) {
334 if (!(pmu->int_mask[i] & pmu->int_enable))
337 if (flag & pmu->cnt_ovf[i])
341 /* writeback clears overflow bits */
342 __asm__ __volatile__ ("mcr p14, 0, %0, c5, c1, 0" : : "r" (flag));
345 static irqreturn_t xscale_pmu_interrupt(int irq, void *arg)
350 if (pmu->id == PMU_XSC1)
355 for (i = CCNT; i < MAX_COUNTERS; i++) {
359 write_counter(i, -(u32)results[i].reset_counter);
360 oprofile_add_sample(get_irq_regs(), i);
364 pmnc = read_pmnc() | PMU_ENABLE;
370 static void xscale_pmu_stop(void)
372 u32 pmnc = read_pmnc();
377 free_irq(XSCALE_PMU_IRQ, results);
380 static int xscale_pmu_start(void)
383 u32 pmnc = read_pmnc();
385 ret = request_irq(XSCALE_PMU_IRQ, xscale_pmu_interrupt, IRQF_DISABLED,
386 "XScale PMU", (void *)results);
389 printk(KERN_ERR "oprofile: unable to request IRQ%d for XScale PMU\n",
394 if (pmu->id == PMU_XSC1)
395 pmnc |= pmu->int_enable;
397 __asm__ __volatile__ ("mcr p14, 0, %0, c4, c1, 0" : : "r" (pmu->int_enable));
403 pr_debug("xscale_pmu_start: pmnc: %#08x mask: %08x\n", pmnc, pmu->int_enable);
407 static int xscale_detect_pmu(void)
412 id = (read_cpuid(CPUID_ID) >> 13) & 0x7;
416 pmu = &pmu_parms[PMU_XSC1];
419 pmu = &pmu_parms[PMU_XSC2];
427 op_xscale_spec.name = pmu->name;
428 op_xscale_spec.num_counters = pmu->num_counters;
429 pr_debug("xscale_detect_pmu: detected %s PMU\n", pmu->name);
435 struct op_arm_model_spec op_xscale_spec = {
436 .init = xscale_detect_pmu,
437 .setup_ctrs = xscale_setup_ctrs,
438 .start = xscale_pmu_start,
439 .stop = xscale_pmu_stop,