Skip to contentSkip to conversation

ARMv6 performance counters v3

PATCH In review Jamie Iles 8 participants 46 messages Dec 15, 2009 – Jan 20, 2010

Subsystems (4)
lore
Share & tools
lore

Jamie Iles

author
#0 lore raw

Okay, here's the 3rd attempt at support for hardware perf events support on ARMv6. After feedback from Jean and Will, I've added an arm_pmu structure that allows other CPU versions to be supported in the future. At the moment only ARMv6 is supported in hardware.

Thanks to Will and Jean for their comments.

author
#1 lore raw

PATCH 1/5 arm: provide a mechanism to reserve performance counters

To add support for perf events and to allow the hardware counters to be shared with oprofile, we need a way to reserve access to the pmu (performance monitor unit).

---
 arch/arm/include/asm/pmu.h |   74 ++++++++++++++++++++++++++++++
 arch/arm/kernel/Makefile   |    1 +
 arch/arm/kernel/pmu.c      |  108 ++++++++++++++++++++++++++++++++++++++++++++
 arch/arm/mm/Kconfig        |    5 ++
 4 files changed, 188 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/include/asm/pmu.h
 create mode 100644 arch/arm/kernel/pmu.c
arch/arm/include/asm/pmu.h new file +74 -0
new file mode 100644
index 0000000..e7cc264
@@ -0,0 +1,74 @@
+/*
+ *  linux/arch/arm/include/asm/pmu.h
+ *
+ *  Copyright (C) 2009 picoChip Designs Ltd, Jamie Iles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#ifndef __ARM_PMU_H__
+#define __ARM_PMU_H__
+
+#ifdef CONFIG_CPU_HAS_PMU
+
+struct pmu_irqs {
+	const int   *irqs;
+	unsigned    num_irqs;
+};
+
+/**
+ * reserve_pmu() - reserve the hardware performance counters
+ *
+ * Reserve the hardware performance counters in the system for exclusive use.
+ * The 'struct pmu_irqs' for the system is returned on success, ERR_PTR()
+ * encoded error on failure.
+ */
+extern const struct pmu_irqs *
+reserve_pmu(void);
+
+/**
+ * release_pmu() - Relinquish control of the performance counters
+ *
+ * Release the performance counters and allow someone else to use them.
+ * Callers must have disabled the counters and released IRQs before calling
+ * this. The 'struct pmu_irqs' returned from reserve_pmu() must be passed as
+ * a cookie.
+ */
+extern int
+release_pmu(const struct pmu_irqs *irqs);
+
+/**
+ * init_pmu() - Initialise the PMU.
+ *
+ * Initialise the system ready for PMU enabling. This should typically set the
+ * IRQ affinity and nothing else. The users (oprofile/perf events etc) will do
+ * the actual hardware initialisation.
+ */
+extern int
+init_pmu(void);
+
+#else /* CONFIG_CPU_HAS_PMU */
+
+static inline const struct pmu_irqs *
+reserve_pmu(void)
+{
+	ERR_PTR(-ENODEV);
+}
+
+static inline int
+release_pmu(const struct pmu_irqs *irqs)
+{
+}
+
+static inline int
+init_pmu(void)
+{
+	return -ENODEV;
+}
+
+#endif /* CONFIG_CPU_HAS_PMU */
+
+#endif /* __ARM_PMU_H__ */
arch/arm/kernel/Makefile +1 -0
index dd00f74..216890d 100644
@@ -46,6 +46,7 @@ obj-$(CONFIG_CPU_XSCALE)	+= xscale-cp0.o
 obj-$(CONFIG_CPU_XSC3)		+= xscale-cp0.o
 obj-$(CONFIG_CPU_MOHAWK)	+= xscale-cp0.o
 obj-$(CONFIG_IWMMXT)		+= iwmmxt.o
+obj-$(CONFIG_CPU_HAS_PMU)	+= pmu.o
 AFLAGS_iwmmxt.o			:= -Wa,-mcpu=iwmmxt
 
 ifneq ($(CONFIG_ARCH_EBSA110),y)
arch/arm/kernel/pmu.c new file +108 -0

108 lines not on this page · raw message

arch/arm/mm/Kconfig +5 -0
index dd4698c..5cd0ec4 100644
@@ -536,6 +536,11 @@ config CPU_COPY_FA
 config CPU_COPY_V6
 	bool
 
+config CPU_HAS_PMU
+	depends on CPU_V6 || CPU_V7 || CPU_XSCALE
+	default y
+	bool
+
 # This selects the TLB model
 config CPU_TLB_V3
 	bool
Signature · 1 line
1.6.5.4
author
#2 lore raw

PATCH 2/5 arm/oprofile: reserve the PMU when starting

Make sure that we have access to the performance counters and that they aren't being used by perf events or anything else.

---
 arch/arm/oprofile/op_model_arm11_core.c |    4 +-
 arch/arm/oprofile/op_model_arm11_core.h |    4 +-
 arch/arm/oprofile/op_model_mpcore.c     |   42 ++++++++++++++++--------------
 arch/arm/oprofile/op_model_v6.c         |   30 ++++++++++++++--------
 arch/arm/oprofile/op_model_v7.c         |   30 ++++++++++++++--------
 arch/arm/oprofile/op_model_v7.h         |    4 +-
 arch/arm/oprofile/op_model_xscale.c     |   35 ++++++++++++++-----------
 7 files changed, 85 insertions(+), 64 deletions(-)
arch/arm/oprofile/op_model_arm11_core.c +2 -2
index ad80752..ef3e265 100644
@@ -132,7 +132,7 @@ static irqreturn_t arm11_pmu_interrupt(int irq, void *arg)
 	return IRQ_HANDLED;
 }
 
-int arm11_request_interrupts(int *irqs, int nr)
+int arm11_request_interrupts(const int *irqs, int nr)
 {
 	unsigned int i;
 	int ret = 0;
@@ -153,7 +153,7 @@ int arm11_request_interrupts(int *irqs, int nr)
 	return ret;
 }
 
-void arm11_release_interrupts(int *irqs, int nr)
+void arm11_release_interrupts(const int *irqs, int nr)
 {
 	unsigned int i;
 
arch/arm/oprofile/op_model_arm11_core.h +2 -2
index 6f8538e..1902b99 100644
@@ -39,7 +39,7 @@
 int arm11_setup_pmu(void);
 int arm11_start_pmu(void);
 int arm11_stop_pmu(void);
-int arm11_request_interrupts(int *, int);
-void arm11_release_interrupts(int *, int);
+int arm11_request_interrupts(const int *, int);
+void arm11_release_interrupts(const int *, int);
 
 #endif
arch/arm/oprofile/op_model_mpcore.c +22 -20

88 lines not on this page · raw message

arch/arm/oprofile/op_model_v6.c +19 -11
index f7d2ec5..a22357a 100644
@@ -19,39 +19,47 @@
 /* #define DEBUG */
 #include <linux/types.h>
 #include <linux/errno.h>
+#include <linux/err.h>
 #include <linux/sched.h>
 #include <linux/oprofile.h>
 #include <linux/interrupt.h>
 #include <asm/irq.h>
 #include <asm/system.h>
+#include <asm/pmu.h>
 
 #include "op_counter.h"
 #include "op_arm_model.h"
 #include "op_model_arm11_core.h"
 
-static int irqs[] = {
-#ifdef CONFIG_ARCH_OMAP2
-	3,
-#endif
-#ifdef CONFIG_ARCH_BCMRING
-	IRQ_PMUIRQ, /* for BCMRING, ARM PMU interrupt is 43 */
-#endif
-};
+static const struct pmu_irqs *pmu_irqs;
 
 static void armv6_pmu_stop(void)
 {
 	arm11_stop_pmu();
-	arm11_release_interrupts(irqs, ARRAY_SIZE(irqs));
+	arm11_release_interrupts(pmu_irqs->irqs, pmu_irqs->num_irqs);
+	release_pmu(pmu_irqs);
+	pmu_irqs = NULL;
 }
 
 static int armv6_pmu_start(void)
 {
 	int ret;
 
-	ret = arm11_request_interrupts(irqs, ARRAY_SIZE(irqs));
-	if (ret >= 0)
+	pmu_irqs = reserve_pmu();
+	if (IS_ERR(pmu_irqs)) {
+		ret = PTR_ERR(pmu_irqs);
+		goto out;
+	}
+
+	ret = arm11_request_interrupts(pmu_irqs->irqs, pmu_irqs->num_irqs);
+	if (ret >= 0) {
 		ret = arm11_start_pmu();
+	} else {
+		release_pmu(pmu_irqs);
+		pmu_irqs = NULL;
+	}
 
+out:
 	return ret;
 }
 
arch/arm/oprofile/op_model_v7.c +19 -11
index f20295f..9258fca 100644
@@ -11,11 +11,14 @@
  */
 #include <linux/types.h>
 #include <linux/errno.h>
+#include <linux/err.h>
 #include <linux/oprofile.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/smp.h>
 
+#include <asm/pmu.h>
+
 #include "op_counter.h"
 #include "op_arm_model.h"
 #include "op_model_v7.h"
@@ -299,7 +302,7 @@ static irqreturn_t armv7_pmnc_interrupt(int irq, void *arg)
 	return IRQ_HANDLED;
 }
 
-int armv7_request_interrupts(int *irqs, int nr)
+int armv7_request_interrupts(const int *irqs, int nr)
 {
 	unsigned int i;
 	int ret = 0;
@@ -322,7 +325,7 @@ int armv7_request_interrupts(int *irqs, int nr)
 	return ret;
 }
 
-void armv7_release_interrupts(int *irqs, int nr)
+void armv7_release_interrupts(const int *irqs, int nr)
 {
 	unsigned int i;
 
@@ -366,12 +369,7 @@ static void armv7_pmnc_dump_regs(void)
 }
 #endif
 
-
-static int irqs[] = {
-#ifdef CONFIG_ARCH_OMAP3
-	INT_34XX_BENCH_MPU_EMUL,
-#endif
-};
+static const struct pmu_irqs *pmu_irqs;
 
 static void armv7_pmnc_stop(void)
 {
@@ -379,19 +377,29 @@ static void armv7_pmnc_stop(void)
 	armv7_pmnc_dump_regs();
 #endif
 	armv7_stop_pmnc();
-	armv7_release_interrupts(irqs, ARRAY_SIZE(irqs));
+	armv7_release_interrupts(pmu_irqs->irqs, pmu_irqs->num_irqs);
+	release_pmu(pmu_irqs);
+	pmu_irqs = NULL;
 }
 
 static int armv7_pmnc_start(void)
 {
 	int ret;
 
+	pmu_irqs = reserve_pmu();
+	if (IS_ERR(pmu_irqs))
+		return PTR_ERR(pmu_irqs);
+
 #ifdef DEBUG
 	armv7_pmnc_dump_regs();
 #endif
-	ret = armv7_request_interrupts(irqs, ARRAY_SIZE(irqs));
-	if (ret >= 0)
+	ret = armv7_request_interrupts(pmu_irqs->irqs, pmu_irqs->num_irqs);
+	if (ret >= 0) {
 		armv7_start_pmnc();
+	} else {
+		release_pmu(pmu_irqs);
+		pmu_irqs = NULL;
+	}
 
 	return ret;
 }
arch/arm/oprofile/op_model_v7.h +2 -2
index 0e19bcc..9ca334b 100644
@@ -97,7 +97,7 @@
 int armv7_setup_pmu(void);
 int armv7_start_pmu(void);
 int armv7_stop_pmu(void);
-int armv7_request_interrupts(int *, int);
-void armv7_release_interrupts(int *, int);
+int armv7_request_interrupts(const int *, int);
+void armv7_release_interrupts(const int *, int);
 
 #endif
arch/arm/oprofile/op_model_xscale.c +19 -16
index 724ab9c..1d34a02 100644
@@ -17,12 +17,14 @@
 /* #define DEBUG */
 #include <linux/types.h>
 #include <linux/errno.h>
+#include <linux/err.h>
 #include <linux/sched.h>
 #include <linux/oprofile.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 
 #include <asm/cputype.h>
+#include <asm/pmu.h>
 
 #include "op_counter.h"
 #include "op_arm_model.h"
@@ -33,17 +35,6 @@
 #define	PMU_RESET	(CCNT_RESET | PMN_RESET)
 #define PMU_CNT64	0x008	/* Make CCNT count every 64th cycle */
 
-/* TODO do runtime detection */
-#ifdef CONFIG_ARCH_IOP32X
-#define XSCALE_PMU_IRQ  IRQ_IOP32X_CORE_PMU
-#endif
-#ifdef CONFIG_ARCH_IOP33X
-#define XSCALE_PMU_IRQ  IRQ_IOP33X_CORE_PMU
-#endif
-#ifdef CONFIG_ARCH_PXA
-#define XSCALE_PMU_IRQ  IRQ_PMU
-#endif
-
 /*
  * Different types of events that can be counted by the XScale PMU
  * as used by Oprofile userspace. Here primarily for documentation
@@ -367,6 +358,8 @@ static irqreturn_t xscale_pmu_interrupt(int irq, void *arg)
 	return IRQ_HANDLED;
 }
 
+static const struct pmu_irqs *pmu_irqs;
+
 static void xscale_pmu_stop(void)
 {
 	u32 pmnc = read_pmnc();
@@ -374,20 +367,30 @@ static void xscale_pmu_stop(void)
 	pmnc &= ~PMU_ENABLE;
 	write_pmnc(pmnc);
 
-	free_irq(XSCALE_PMU_IRQ, results);
+	free_irq(pmu_irqs->irqs[0], results);
+	release_pmu(pmu_irqs);
+	pmu_irqs = NULL;
 }
 
 static int xscale_pmu_start(void)
 {
 	int ret;
-	u32 pmnc = read_pmnc();
+	u32 pmnc;
+
+	pmu_irqs = reserve_pmu();
+	if (IS_ERR(pmu_irqs))
+		return PTR_ERR(pmu_irqs);
+
+	pmnc = read_pmnc();
 
-	ret = request_irq(XSCALE_PMU_IRQ, xscale_pmu_interrupt, IRQF_DISABLED,
-			"XScale PMU", (void *)results);
+	ret = request_irq(pmu_irqs->irqs[0], xscale_pmu_interrupt,
+			  IRQF_DISABLED, "XScale PMU", (void *)results);
 
 	if (ret < 0) {
 		printk(KERN_ERR "oprofile: unable to request IRQ%d for XScale PMU\n",
-			XSCALE_PMU_IRQ);
+		       pmu_irqs->irqs[0]);
+		release_pmu(pmu_irqs);
+		pmu_irqs = NULL;
 		return ret;
 	}
 
Signature · 1 line
1.6.5.4
author
#3 lore raw

PATCH 3/5 arm: use the spinlocked, generic atomic64 support

perf events require that we can support atomic64's. There is a generic, spinlocked version that we can use until we have proper hardware support.

---
 arch/arm/Kconfig              |    1 +
 arch/arm/include/asm/atomic.h |    4 ++++
 2 files changed, 5 insertions(+), 0 deletions(-)
arch/arm/Kconfig +1 -0
index 233a222..9580418 100644
@@ -18,6 +18,7 @@ config ARM
 	select HAVE_KRETPROBES if (HAVE_KPROBES)
 	select HAVE_FUNCTION_TRACER if (!XIP_KERNEL)
 	select HAVE_GENERIC_DMA_COHERENT
+	select GENERIC_ATOMIC64
 	help
 	  The ARM series is a line of low-power-consumption RISC chip designs
 	  licensed by ARM Ltd and targeted at embedded applications and
arch/arm/include/asm/atomic.h +4 -0
index d0daeab..ff286a8 100644
@@ -15,6 +15,10 @@
 #include <linux/types.h>
 #include <asm/system.h>
 
+#ifdef CONFIG_GENERIC_ATOMIC64
+#include <asm-generic/atomic64.h>
+#endif
+
 #define ATOMIC_INIT(i)	{ (i) }
 
 #ifdef __KERNEL__
Signature · 1 line
1.6.5.4
author
#4 lore raw

PATCH 4/5 arm: enable support for software perf events

The perf events subsystem allows counting of both hardware and software events. This patch implements the bare minimum for software performance events.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 arch/arm/Kconfig                  |    2 +
 arch/arm/include/asm/perf_event.h |   38 +++++++++++++++++++++++++++++++++++++
 arch/arm/mm/fault.c               |    7 ++++++
 3 files changed, 47 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/include/asm/perf_event.h
arch/arm/Kconfig +2 -0
index 9580418..fe4ce95 100644
@@ -19,6 +19,8 @@ config ARM
 	select HAVE_FUNCTION_TRACER if (!XIP_KERNEL)
 	select HAVE_GENERIC_DMA_COHERENT
 	select GENERIC_ATOMIC64
+	select HAVE_PERF_EVENTS
+	select PERF_USE_VMALLOC
 	help
 	  The ARM series is a line of low-power-consumption RISC chip designs
 	  licensed by ARM Ltd and targeted at embedded applications and
arch/arm/include/asm/perf_event.h new file +38 -0
new file mode 100644
index 0000000..32a66ac
@@ -0,0 +1,38 @@
+/*
+ *  linux/arch/arm/include/asm/perf_event.h
+ *
+ *  Copyright (C) 2009 picoChip Designs Ltd, Jamie Iles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#ifndef __ARM_PERF_EVENT_H__
+#define __ARM_PERF_EVENT_H__
+
+/*
+ * NOP: on *most* (read: all supported) ARM platforms, the performance
+ * counter interrupts are regular interrupts and not an NMI. This
+ * means that when we receive the interrupt we can call
+ * perf_event_do_pending() that handles all of the work with
+ * interrupts enabled.
+ */
+static inline void
+set_perf_event_pending(void)
+{
+}
+
+/* Get the PC. Make sure that we have a 64bit value with the upper 32 cleared.
+ */
+#define perf_instruction_pointer(_regs) \
+	((u64)instruction_pointer(regs) & 0xFFFFFFFFLU)
+#define perf_misc_flags(regs)   (user_mode(regs) ? PERF_RECORD_MISC_USER : \
+                                 PERF_RECORD_MISC_KERNEL)
+
+/* ARM performance counters start from 1 (in the cp15 accesses) so use the
+ * same indexes here for consistency. */
+#define PERF_EVENT_INDEX_OFFSET 1
+
+#endif /* __ARM_PERF_EVENT_H__ */
arch/arm/mm/fault.c +7 -0
index 10e0680..9d40c34 100644
@@ -18,6 +18,7 @@
 #include <linux/page-flags.h>
 #include <linux/sched.h>
 #include <linux/highmem.h>
+#include <linux/perf_event.h>
 
 #include <asm/system.h>
 #include <asm/pgtable.h>
@@ -302,6 +303,12 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
 	fault = __do_page_fault(mm, addr, fsr, tsk);
 	up_read(&mm->mmap_sem);
 
+	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, addr);
+	if (fault & VM_FAULT_MAJOR)
+		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0, regs, addr);
+	else if (fault & VM_FAULT_MINOR)
+		perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0, regs, addr);
+
 	/*
 	 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
 	 */
Signature · 1 line
1.6.5.4
author
#5 lore raw

PATCH 5/5 arm/perfevents: implement perf event support for ARMv6

This patch implements support for ARMv6 performance counters in the Linux performance events subsystem. ARMv6 architectures that have the performance counters should enable HW_PERF_EVENTS and define the interrupts for the counters in arch/arm/kernel/perf_event.c

This implementation also provides an ARM PMU abstraction layer to allow ARMv7 and others to be supported in the future by adding new a 'struct arm_pmu'.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 arch/arm/Kconfig             |    8 +
 arch/arm/kernel/Makefile     |    1 +
 arch/arm/kernel/perf_event.c | 1125 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 1134 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/kernel/perf_event.c
arch/arm/Kconfig +8 -0
index fe4ce95..ec26a1f 100644
@@ -1170,6 +1170,14 @@ config HIGHPTE
 	depends on HIGHMEM
 	depends on !OUTER_CACHE
 
+config HW_PERF_EVENTS
+	bool "Enable hardware performance counter support for perf events"
+	depends on PERF_EVENTS && CPU_HAS_PMU && CPU_V6
+	default y
+	help
+	  Enable hardware performance counter support for perf events. If
+	  disabled, perf events will use software events only.
+
 source "mm/Kconfig"
 
 config LEDS
arch/arm/kernel/Makefile +1 -0
index 216890d..c76e6d2 100644
@@ -47,6 +47,7 @@ obj-$(CONFIG_CPU_XSC3)		+= xscale-cp0.o
 obj-$(CONFIG_CPU_MOHAWK)	+= xscale-cp0.o
 obj-$(CONFIG_IWMMXT)		+= iwmmxt.o
 obj-$(CONFIG_CPU_HAS_PMU)	+= pmu.o
+obj-$(CONFIG_HW_PERF_EVENTS)	+= perf_event.o
 AFLAGS_iwmmxt.o			:= -Wa,-mcpu=iwmmxt
 
 ifneq ($(CONFIG_ARCH_EBSA110),y)
arch/arm/kernel/perf_event.c new file +1125 -0

1,125 lines not on this page · raw message

Signature · 1 line
1.6.5.4
#6 lore raw

PATCH 5/5 arm/perfevents: implement perf event support for ARMv6

Hi Jamie,

I've not looked at the entire patch in depth, but I did spot a couple of things:

Quoted 8 lines

This patch implements support for ARMv6 performance counters in the Linux performance events subsystem. ARMv6 architectures that have the performance counters should enable HW_PERF_EVENTS and define the interrupts for the counters in arch/arm/kernel/perf_event.c

This implementation also provides an ARM PMU abstraction layer to allow ARMv7 and others to be supported in the future by adding new a 'struct arm_pmu'.

<snip>

Quoted 5 lines
arch/arm/kernel/perf_event.c new file +5 -0
+static struct arm_pmu armv6pmu = {
+	.name			= "v6",
+	.handle_irq		= armv6pmu_handle_irq,
+	.enable			= armv6pmu_enable_event,
+	.disable		= armv6pmu_disable_event,

arch/arm/kernel/perf_event.c line 966

Quoted 10 lines
arch/arm/kernel/perf_event.c new file +10 -0
+	.event_map		= armv6pmu_event_map,
+	.raw_event		= armv6pmu_raw_event,
+        .read_counter           = armv6pmu_read_counter,
+        .write_counter          = armv6pmu_write_counter,
+	.get_event_idx		= armv6pmu_get_event_idx,
+        .start                  = armv6pmu_start,
+	.stop		        = armv6pmu_stop,
+	.num_events		= 3,
+	.max_period		= (1LLU << 32) - 1,
+};

Your indentation seems to have gone awry here, I think I saw it somewhere else in the file too.

Quoted 14 lines
arch/arm/kernel/perf_event.c new file +14 -0
+static int __init
+init_hw_perf_events(void)
+{
+#define CPUID_V6_MASK   0x7F000
+#define CPUID_V6_BITS   0x7B000
+        unsigned long cpuid = read_cpuid_id();
+
+        if (CPUID_V6_BITS == (cpuid & CPUID_V6_MASK)) {
+                armpmu = &armv6pmu;
+                memcpy(armpmu_perf_cache_map, armv6_perf_cache_map,
+                       sizeof(armv6_perf_cache_map));
+                perf_max_events	= armv6pmu.num_events;
+        } else {
+                pr_info("no hardware support available\n");

arch/arm/kernel/perf_event.c line 991

Quoted 10 lines
arch/arm/kernel/perf_event.c new file +10 -0
+                perf_max_events = -1;
+        }
+
+        if (armpmu)
+                pr_info("enabled with %s PMU driver\n",
+                        armpmu->name);
+
+        return 0;
+}
+arch_initcall(init_hw_perf_events);

Watch out for the 11MPCore CPU. The event numbers are defined *slightly* differently (1136,1156 and 1176 don't have any conflicts with each other, but the 11MPCore does). If you look at the TRM the first 6 events are the same as for other v6 cores, but then event 0x06 becomes a new event: `branch not predicted' which offsets the others by one. Other than that, the PMUs are accessed the same way on each core, so you just need to make sure you select the correct event mappings.

Cheers,

Will

#7 lore raw

PATCH 5/5 arm/perfevents: implement perf event support for ARMv6

Hi Will,

Quoted 10 lines · Will Deacon
Quoted 10 lines
arch/arm/kernel/perf_event.c new file +10 -0
+static struct arm_pmu armv6pmu = {
+	.name			= "v6",
+	.handle_irq		= armv6pmu_handle_irq,
+	.enable			= armv6pmu_enable_event,
+	.disable		= armv6pmu_disable_event,
+	.event_map		= armv6pmu_event_map,
+	.raw_event		= armv6pmu_raw_event,
+        .read_counter           = armv6pmu_read_counter,
+        .write_counter          = armv6pmu_write_counter,
+	.get_event_idx		= armv6pmu_get_event_idx,
Quoted 8 lines

arch/arm/kernel/perf_event.c line 966

Quoted 5 lines
arch/arm/kernel/perf_event.c new file +5 -0
+        .start                  = armv6pmu_start,
+	.stop		        = armv6pmu_stop,
+	.num_events		= 3,
+	.max_period		= (1LLU << 32) - 1,
+};

Your indentation seems to have gone awry here, I think I saw it somewhere else in the file too.

Indeed. That needs fixing.

Quoted 7 lines

Watch out for the 11MPCore CPU. The event numbers are defined *slightly* differently (1136,1156 and 1176 don't have any conflicts with each other, but the 11MPCore does). If you look at the TRM the first 6 events are the same as for other v6 cores, but then event 0x06 becomes a new event: `branch not predicted' which offsets the others by one. Other than that, the PMUs are accessed the same way on each core, so you just need to make sure you select the correct event mappings.

Ok, is it a safe assumption that for ARMv6 if num_possible_cpus() returns >1 we are mpcore and need to use different ID's or is there a better way to test for mpcore?

Thanks,

Jamie

#8 lore raw

PATCH 5/5 arm/perfevents: implement perf event support for ARMv6

Quoted 3 lines · Jamie Iles

Ok, is it a safe assumption that for ARMv6 if num_possible_cpus() returns >1 we are mpcore and need to use different ID's or is there a better way to test for mpcore?

The bottom 16 bits of the cpuid field will read 0xB020 on an 11MPCore as it's a different chip to the other v6 cores.

You could cast the cpuid to a u16 and switch on that: 0xB360, 0xB560 and 0xB760 are the single core ARM11s.

Will

author
#9 lore raw

PATCH 5/5 arm/perfevents: implement perf event support for ARMv6

Hi Will,

Quoted 9 lines · Will Deacon
Quoted 3 lines

Ok, is it a safe assumption that for ARMv6 if num_possible_cpus() returns >1 we are mpcore and need to use different ID's or is there a better way to test for mpcore?

The bottom 16 bits of the cpuid field will read 0xB020 on an 11MPCore as it's a different chip to the other v6 cores.

You could cast the cpuid to a u16 and switch on that: 0xB360, 0xB560 and 0xB760 are the single core ARM11s.

Ok, that sounds like a good idea. Another problem with mpcore support is that with the v6 performance counters, you can't disable a single event counter. If we receive lots of interrupts, the higher perf events layers will disable the counter for a bit before reenabling/unthrottling it.

On the single core, I just disable the interrupt for the counter and tell it to count the ETM cycle events with the event exporting disabled so that the counter doesn't increment. mpcore doesn't have this event so the counter will keep on incrementing. Is there an undocumented event number we can use to stop the counter? If not we'll need to read the counter, disable it and stash the value away for when we unthrottle it and write it back. I guess we'll lose some accuracy so it would be nice not to do that.

Jamie

maintainer
#10 lore raw

PATCH 5/5 arm/perfevents: implement perf event support for ARMv6

Quoted 3 lines · Jamie Iles

Another problem with mpcore support is that
with the v6 performance counters, you can't disable a single event counter.

Can you program them with a non-counting event?

On x86 there's various ways of doing that, either by selecting an event that simply doesn't count (cache-misses with 0 MESI mask), or by telling it to mask both user and kernel event.

#11 lore raw

PATCH 5/5 arm/perfevents: implement perf event support for ARMv6

Quoted 10 lines · Peter Zijlstra
Quoted 3 lines · Jamie Iles

Another problem with mpcore support is that
with the v6 performance counters, you can't disable a single event counter.

Can you program them with a non-counting event?

On x86 there's various ways of doing that, either by selecting an event that simply doesn't count (cache-misses with 0 MESI mask), or by telling it to mask both user and kernel event.

That's exactly what I do for single core ARMv6. However, in the list of events for mpcore I can't see any that wouldn't count. There's plenty of reserved identifiers though so hopefully one of those will do the job. Also, ARM counters can't be set to exclude any modes.

Jamie

#12 lore raw

PATCH 5/5 arm/perfevents: implement perf event support for ARMv6

Quoted 8 lines · Jamie Iles
Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Jamie Iles

Another problem with mpcore support is that
with the v6 performance counters, you can't disable a single event counter.

Can you program them with a non-counting event?

Quoted 7 lines
Quoted 3 lines

On x86 there's various ways of doing that, either by selecting an event that simply doesn't count (cache-misses with 0 MESI mask), or by telling it to mask both user and kernel event.

That's exactly what I do for single core ARMv6. However, in the list of events for mpcore I can't see any that wouldn't count. There's plenty of reserved identifiers though so hopefully one of those will do the job. Also, ARM counters can't be set to exclude any modes.

Thinking about this a bit more, although we can't disable the counters, we can disable their interrupt reporting. So, when the generic perf events layer calls pmu->disable(event), we do the update of the event then turn off the IRQ. When we come to unthrottling, the counter will have carried on counting, but if we set the period again, the counter gets set with the correct restart value and then reenabled.

I think this should work for mpcore and is also required for the cycle counter on all v6 cores. I've given this a go using an artificially low period on a cycle counter and it does appear to do the job.

Thanks,

Jamie

#13 lore raw

PATCH 5/5 arm/perfevents: implement perf event support for ARMv6

Quoted 11 lines · *Jamie Iles
Quoted 4 lines

That's exactly what I do for single core ARMv6. However, in the list of events for mpcore I can't see any that wouldn't count. There's plenty of reserved identifiers though so hopefully one of those will do the job. Also, ARM counters can't be set to exclude any modes.

Thinking about this a bit more, although we can't disable the counters, we can disable their interrupt reporting. So, when the generic perf events layer calls pmu->disable(event), we do the update of the event then turn off the IRQ. When we come to unthrottling, the counter will have carried on counting, but if we set the period again, the counter gets set with the correct restart value and then reenabled.

This was my first thought, but I was concerned about how it would play out with the armpmu_read function. Now that I see we don't read disabled counters, I can't see any reason not to simply disable the interrupt and stash the count value.

Quoted 3 lines

I think this should work for mpcore and is also required for the cycle counter on all v6 cores. I've given this a go using an artificially low period on a cycle counter and it does appear to do the job.

If we do this for mpcore, is it worth doing the same thing for the other v6 cores too [and removing the ETM `hack']?

Cheers,

Will

#14 lore raw

PATCH 5/5 arm/perfevents: implement perf event support for ARMv6

Quoted 14 lines · Will Deacon
Quoted 11 lines · *Jamie Iles
Quoted 4 lines

That's exactly what I do for single core ARMv6. However, in the list of events for mpcore I can't see any that wouldn't count. There's plenty of reserved identifiers though so hopefully one of those will do the job. Also, ARM counters can't be set to exclude any modes.

Thinking about this a bit more, although we can't disable the counters, we can disable their interrupt reporting. So, when the generic perf events layer calls pmu->disable(event), we do the update of the event then turn off the IRQ. When we come to unthrottling, the counter will have carried on counting, but if we set the period again, the counter gets set with the correct restart value and then reenabled.

Quoted 4 lines

This was my first thought, but I was concerned about how it would play out with the armpmu_read function. Now that I see we don't read disabled counters, I can't see any reason not to simply disable the interrupt and stash the count value.

We shouldn't even need to stash the value. When the higher layer disables the event we sample it to record the value so the stashing happens already.

Quoted 6 lines
Quoted 3 lines

I think this should work for mpcore and is also required for the cycle counter on all v6 cores. I've given this a go using an artificially low period on a cycle counter and it does appear to do the job.

If we do this for mpcore, is it worth doing the same thing for the other v6 cores too [and removing the ETM `hack']?

We do need to do this on UP v6 for the cycle counter as that keeps on running, but imho the ETM hack is worth keeping as it really does stop the counter. It doesn't hurt and we're guaranteed not to lose any events.

If I make the change to the unthrottling and the Kconfig and tab changes, and add the mpcore events does anyone have an objection to making a final version of this patch series and merging it?

Jamie

#15 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

In reply to Jamie Iles

Hello,

Here is a patch that adds the support for ARMv7 processors, using the PMNC HW unit.

The code is for review, it has been compiled and boot tested only, the complete testing is in progress. Please let me know if the patch is wrapped or garbled I will send it attached (20KB in size).

Feedback is welcome.

I had a question about the events mapping to user space. Although most of the events are mapped in the kernel code, some of the exotic events are not mapped (e.g. NEON or PMU related events). How to use those events from user space? Is it done using the raw mappings?

Regards,
Jean

---

Quoted 1 line

From d48f736b380b0a05ab74743dcce4e662d71371d9 Mon Sep 17 00:00:00 2001

From: Jean Pihet <[email protected]>
Date: Fri, 18 Dec 2009 17:46:21 +0100
Subject: [PATCH] arm/perfevents: add support for ARMv7

Adds the Performance Events support for ARMv7 processor, using the PMNC unit in HW.

---
 arch/arm/Kconfig             |    2 +-
 arch/arm/kernel/perf_event.c |  708
+++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 708 insertions(+), 2 deletions(-)
arch/arm/Kconfig +1 -1
index 2ac6e8d..9dfc0ee 100644
@@ -1171,7 +1171,7 @@ config HIGHPTE
 
 config HW_PERF_EVENTS
 	bool "Enable hardware performance counter support for perf events"
-	depends on PERF_EVENTS && CPU_HAS_PMU && CPU_V6
+	depends on PERF_EVENTS && CPU_HAS_PMU && (CPU_V6 || CPU_V7)
 	default y
 	help
 	  Enable hardware performance counter support for perf events. If
arch/arm/kernel/perf_event.c +1 -0
index abb5267..79e92ce 100644
@@ -4,6 +4,7 @@
  * ARM performance counter support.
  *
  * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
+ * ARMv7 support: Jean Pihet <[email protected]>
  *
  * This code is based on the sparc64 perf event code, which is in turn

based
* on the x86 code. Callchain code is based on the ARM OProfile
backtrace
@@ -35,8 +36,11 @@ DEFINE_SPINLOCK(pmu_lock);
* ARMv6 supports a maximum of 3 events, starting from index 1. If we
add
* another platform that supports more, we need to increase this to be
the
* largest of all platforms.
+ *
+ * ARMv7 supports up to 5 events:
+ * cycle counter CCNT + 4 events counters CNT0..3
*/
-#define ARMPMU_MAX_HWEVENTS 4
+#define ARMPMU_MAX_HWEVENTS 5

/* The events for a given CPU. */
struct cpu_hw_events {
@@ -965,6 +969,701 @@ static struct arm_pmu armv6pmu = {
.max_period = (1LLU << 32) - 1,
};

+/*
+ * ARMv7 Performance counter handling code.
+ *
+ * Copied from ARMv6 code, with the low level code inspired
+ * by the ARMv7 Oprofile code.
+ *
+ * ARMv7 has 4 configurable performance counters and a single cycle counter.
+ * All counters can be enabled/disabled and IRQ masked separately. The cycle
+ * counter and all 4 performance counters together can be reset separately.
+ */
+
+enum armv7_perf_types {
+ ARMV7_PERFCTR_PMNC_SW_INCR = 0x00,
+ ARMV7_PERFCTR_IFETCH_MISS = 0x01,
+ ARMV7_PERFCTR_ITLB_MISS = 0x02,
+ ARMV7_PERFCTR_DCACHE_REFILL = 0x03,
+ ARMV7_PERFCTR_DCACHE_ACCESS = 0x04,
+ ARMV7_PERFCTR_DTLB_REFILL = 0x05,
+ ARMV7_PERFCTR_DREAD = 0x06,
+ ARMV7_PERFCTR_DWRITE = 0x07,
+ ARMV7_PERFCTR_INSTR_EXECUTED = 0x08,
+ ARMV7_PERFCTR_EXC_TAKEN = 0x09,
+ ARMV7_PERFCTR_EXC_EXECUTED = 0x0A,
+ ARMV7_PERFCTR_CID_WRITE = 0x0B,
+ ARMV7_PERFCTR_PC_WRITE = 0x0C,
+ ARMV7_PERFCTR_PC_IMM_BRANCH = 0x0D,
+ ARMV7_PERFCTR_PC_PROC_RETURN = 0x0E,
+ ARMV7_PERFCTR_UNALIGNED_ACCESS = 0x0F,
+ ARMV7_PERFCTR_PC_BRANCH_MIS_PRED = 0x10,
+
+ ARMV7_PERFCTR_PC_BRANCH_MIS_USED = 0x12,
+
+ ARMV7_PERFCTR_WRITE_BUFFER_FULL = 0x40,
+ ARMV7_PERFCTR_L2_STORE_MERGED = 0x41,
+ ARMV7_PERFCTR_L2_STORE_BUFF = 0x42,
+ ARMV7_PERFCTR_L2_ACCESS = 0x43,
+ ARMV7_PERFCTR_L2_CACH_MISS = 0x44,
+ ARMV7_PERFCTR_AXI_READ_CYCLES = 0x45,
+ ARMV7_PERFCTR_AXI_WRITE_CYCLES = 0x46,
+ ARMV7_PERFCTR_MEMORY_REPLAY = 0x47,
+ ARMV7_PERFCTR_UNALIGNED_ACCESS_REPLAY = 0x48,
+ ARMV7_PERFCTR_L1_DATA_MISS = 0x49,
+ ARMV7_PERFCTR_L1_INST_MISS = 0x4A,
+ ARMV7_PERFCTR_L1_DATA_COLORING = 0x4B,
+ ARMV7_PERFCTR_L1_NEON_DATA = 0x4C,
+ ARMV7_PERFCTR_L1_NEON_CACH_DATA = 0x4D,
+ ARMV7_PERFCTR_L2_NEON = 0x4E,
+ ARMV7_PERFCTR_L2_NEON_HIT = 0x4F,
+ ARMV7_PERFCTR_L1_INST = 0x50,
+ ARMV7_PERFCTR_PC_RETURN_MIS_PRED = 0x51,
+ ARMV7_PERFCTR_PC_BRANCH_FAILED = 0x52,
+ ARMV7_PERFCTR_PC_BRANCH_TAKEN = 0x53,
+ ARMV7_PERFCTR_PC_BRANCH_EXECUTED = 0x54,
+ ARMV7_PERFCTR_OP_EXECUTED = 0x55,
+ ARMV7_PERFCTR_CYCLES_INST_STALL = 0x56,
+ ARMV7_PERFCTR_CYCLES_INST = 0x57,
+ ARMV7_PERFCTR_CYCLES_NEON_DATA_STALL = 0x58,
+ ARMV7_PERFCTR_CYCLES_NEON_INST_STALL = 0x59,
+ ARMV7_PERFCTR_NEON_CYCLES = 0x5A,
+
+ ARMV7_PERFCTR_PMU0_EVENTS = 0x70,
+ ARMV7_PERFCTR_PMU1_EVENTS = 0x71,
+ ARMV7_PERFCTR_PMU_EVENTS = 0x72,
+
+ ARMV7_PERFCTR_CPU_CYCLES = 0xFF
+};
+
+enum armv7_counters {
+ ARMV7_CYCLE_COUNTER = 1,
+ ARMV7_COUNTER0,
+ ARMV7_COUNTER1,
+ ARMV7_COUNTER2,
+ ARMV7_COUNTER3,
+};
+
+/*
+ * The hardware events that we support. We do support cache operations but
+ * we have harvard caches and no way to combine instruction and data
+ * accesses/misses in hardware.
+ */
+static const unsigned armv7_perf_map[PERF_COUNT_HW_MAX] = {
+ [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
+ [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
+ [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
+ [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
+ [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_BRANCH_TAKEN,
+ [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_FAILED,
+ [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
+};
+
+static const unsigned armv7_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
+ [PERF_COUNT_HW_CACHE_OP_MAX]
+ [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
+ [C(L1D)] = {
+ /*
+ * The performance counters don't differentiate between read
+ * and write accesses/misses so this isn't strictly correct,
+ * but it's the best we can do. Writes and reads get
+ * combined.
+ */
+ [C(OP_READ)] = {
+ [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
+ [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DATA_MISS,
+ },
+ [C(OP_WRITE)] = {
+ [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
+ [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_DATA_MISS,
+ },
+ [C(OP_PREFETCH)] = {
+ [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
+ [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
+ },
+ },
+ [C(L1I)] = {
+ [C(OP_READ)] = {
+ [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
+ [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
+ },
+ [C(OP_WRITE)] = {
+ [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
+ [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
+ },
+ [C(OP_PREFETCH)] = {
+ [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
+ [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
+ },
+ },
+ [C(LL)] = {
+ [C(OP_READ)] = {
+ [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
+ [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
+ },
+ [C(OP_WRITE)] = {
+ [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
+ [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
+ },
+ [C(OP_PREFETCH)] = {
+ [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
+ [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
+ },
+ },
+ [C(DTLB)] = {
+ /*
+ * The ARM performance counters can count micro DTLB misses,
+ * micro ITLB misses and main TLB misses. There isn't an event
+ * for TLB misses, so use the micro misses here and if users
+ * want the main TLB misses they can use a raw counter.
+ */
+ [C(OP_READ)] = {
+ [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
+ [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
+ },
+ [C(OP_WRITE)] = {
+ [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
+ [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
+ },
+ [C(OP_PREFETCH)] = {
+ [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
+ [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
+ },
+ },
+ [C(ITLB)] = {
+ [C(OP_READ)] = {
+ [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
+ [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
+ },
+ [C(OP_WRITE)] = {
+ [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
+ [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
+ },
+ [C(OP_PREFETCH)] = {
+ [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
+ [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
+ },
+ },
+ [C(BPU)] = {
+ [C(OP_READ)] = {
+ [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_TAKEN,
+ [C(RESULT_MISS)]
+ = ARMV7_PERFCTR_PC_BRANCH_FAILED,
+ },
+ [C(OP_WRITE)] = {
+ [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_BRANCH_TAKEN,
+ [C(RESULT_MISS)]
+ = ARMV7_PERFCTR_PC_BRANCH_FAILED,
+ },
+ [C(OP_PREFETCH)] = {
+ [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
+ [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
+ },
+ },
+};
+
+/*
+ * ARMv7 low level PMNC access
+ */
+
+/*
+ * Per-CPU PMNC: config reg
+ */
+#define ARMV7_PMNC_E (1 << 0) /* Enable all counters */
+#define ARMV7_PMNC_P (1 << 1) /* Reset all counters */
+#define ARMV7_PMNC_C (1 << 2) /* Cycle counter reset */
+#define ARMV7_PMNC_D (1 << 3) /* CCNT counts every 64th cpu cycle */
+#define ARMV7_PMNC_X (1 << 4) /* Export to ETM */
+#define ARMV7_PMNC_DP (1 << 5) /* Disable CCNT if non-invasive debug*/
+#define ARMV7_PMNC_MASK 0x3f /* Mask for writable bits */
+
+/*
+ * Available counters
+ */
+#define ARMV7_CCNT 0
+#define ARMV7_CNT0 1
+#define ARMV7_CNT1 2
+#define ARMV7_CNT2 3
+#define ARMV7_CNT3 4
+#define ARMV7_CNTMAX 5
+#define ARMV7_COUNTER_TO_CCNT (ARMV7_CYCLE_COUNTER - ARMV7_CCNT)
+
+#define ARMV7_CPU_COUNTER(cpu, counter) ((cpu) * CNTMAX + (counter))
+
+/*
+ * CNTENS: counters enable reg
+ */
+#define ARMV7_CNTENS_P0 (1 << 0)
+#define ARMV7_CNTENS_P1 (1 << 1)
+#define ARMV7_CNTENS_P2 (1 << 2)
+#define ARMV7_CNTENS_P3 (1 << 3)
+#define ARMV7_CNTENS_C (1 << 31)
+#define ARMV7_CNTENS_MASK 0x8000000f /* Mask for writable bits */
+
+/*
+ * CNTENC: counters disable reg
+ */
+#define ARMV7_CNTENC_P0 (1 << 0)
+#define ARMV7_CNTENC_P1 (1 << 1)
+#define ARMV7_CNTENC_P2 (1 << 2)
+#define ARMV7_CNTENC_P3 (1 << 3)
+#define ARMV7_CNTENC_C (1 << 31)
+#define ARMV7_CNTENC_MASK 0x8000000f /* Mask for writable bits */
+
+/*
+ * INTENS: counters overflow interrupt enable reg
+ */
+#define ARMV7_INTENS_P0 (1 << 0)
+#define ARMV7_INTENS_P1 (1 << 1)
+#define ARMV7_INTENS_P2 (1 << 2)
+#define ARMV7_INTENS_P3 (1 << 3)
+#define ARMV7_INTENS_C (1 << 31)
+#define ARMV7_INTENS_MASK 0x8000000f /* Mask for writable bits */
+
+/*
+ * INTENC: counters overflow interrupt disable reg
+ */
+#define ARMV7_INTENC_P0 (1 << 0)
+#define ARMV7_INTENC_P1 (1 << 1)
+#define ARMV7_INTENC_P2 (1 << 2)
+#define ARMV7_INTENC_P3 (1 << 3)
+#define ARMV7_INTENC_C (1 << 31)
+#define ARMV7_INTENC_MASK 0x8000000f /* Mask for writable bits */
+
+/*
+ * EVTSEL: Event selection reg
+ */
+#define ARMV7_EVTSEL_MASK 0x7f /* Mask for writable bits */
+
+/*
+ * SELECT: Counter selection reg
+ */
+#define ARMV7_SELECT_MASK 0x1f /* Mask for writable bits */
+
+/*
+ * FLAG: counters overflow flag status reg
+ */
+#define ARMV7_FLAG_P0 (1 << 0)
+#define ARMV7_FLAG_P1 (1 << 1)
+#define ARMV7_FLAG_P2 (1 << 2)
+#define ARMV7_FLAG_P3 (1 << 3)
+#define ARMV7_FLAG_C (1 << 31)
+#define ARMV7_FLAG_MASK 0x8000000f /* Mask for writable bits */
+#define ARMV7_OVERFLOWED_MASK ARMV7_FLAG_MASK
+
+static inline unsigned long armv7_pmnc_read(void)
+{
+ u32 val;
+ asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r"(val));
+ return val;
+}
+
+static inline void armv7_pmnc_write(unsigned long val)
+{
+ val &= ARMV7_PMNC_MASK;
+ asm volatile("mcr p15, 0, %0, c9, c12, 0" : : "r"(val));
+}
+
+static inline int armv7_pmnc_has_overflowed(unsigned long pmnc) +{
+ return pmnc & ARMV7_OVERFLOWED_MASK;
+}
+
+static inline int armv7_pmnc_counter_has_overflowed(unsigned long pmnc,
+ enum armv7_counters counter)
+{
+ int ret;
+
+ if (ARMV7_CYCLE_COUNTER == counter)
+ ret = pmnc & ARMV7_FLAG_C;
+ else if (ARMV7_COUNTER0 == counter)
+ ret = pmnc & ARMV7_FLAG_P0;
+ else if (ARMV7_COUNTER1 == counter)
+ ret = pmnc & ARMV7_FLAG_P1;
+ else if (ARMV7_COUNTER2 == counter)
+ ret = pmnc & ARMV7_FLAG_P2;
+ else if (ARMV7_COUNTER3 == counter)
+ ret = pmnc & ARMV7_FLAG_P3;
+ else
+ BUG();
+
+ return ret;
+}
+
+static inline int armv7_pmnc_select_counter(unsigned int cnt) +{
+ u32 val;
+
+ cnt -= ARMV7_COUNTER_TO_CCNT;
+
+ if ((cnt == ARMV7_CCNT) || (cnt >= ARMV7_CNTMAX)) {
+ printk(KERN_ERR "oprofile: CPU%u selecting wrong PMNC counter"
+ " %d\n", smp_processor_id(), cnt);
+ return -1;
+ }
+
+ val = (cnt - ARMV7_CNT0) & ARMV7_SELECT_MASK;
+ asm volatile("mcr p15, 0, %0, c9, c12, 5" : : "r" (val));
+
+ return cnt;
+}
+
+static inline u32 armv7pmu_read_counter(int counter)
+{
+ unsigned long value = 0;
+
+ switch (counter) {
+ case ARMV7_CYCLE_COUNTER:
+ asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (value));
+ break;
+ case ARMV7_COUNTER0:
+ case ARMV7_COUNTER1:
+ case ARMV7_COUNTER2:
+ case ARMV7_COUNTER3:
+ if (armv7_pmnc_select_counter(counter) == counter)
+ asm volatile("mrc p15, 0, %0, c9, c13, 2"
+ : "=r" (value));
+ break;
+ default:
+ BUG();
+ }
+
+ return value;
+}
+
+static inline void armv7pmu_write_counter(int counter, u32 value) +{
+ switch (counter) {
+ case ARMV7_CYCLE_COUNTER:
+ asm volatile("mcr p15, 0, %0, c9, c13, 0" : : "r" (value));
+ break;
+ case ARMV7_COUNTER0:
+ case ARMV7_COUNTER1:
+ case ARMV7_COUNTER2:
+ case ARMV7_COUNTER3:
+ if (armv7_pmnc_select_counter(counter) == counter)
+ asm volatile("mcr p15, 0, %0, c9, c13, 2"
+ : : "r" (value));
+ break;
+ default:
+ BUG();
+ }
+}
+
+static inline void armv7_pmnc_write_evtsel(unsigned int cnt, u32 val) +{
+ if (armv7_pmnc_select_counter(cnt) == cnt) {
+ val &= ARMV7_EVTSEL_MASK;
+ asm volatile("mcr p15, 0, %0, c9, c13, 1" : : "r" (val));
+ }
+}
+
+static inline u32 armv7_pmnc_enable_counter(unsigned int cnt) +{
+ u32 val;
+
+ cnt -= ARMV7_COUNTER_TO_CCNT;
+
+ if (cnt >= ARMV7_CNTMAX) {
+ printk(KERN_ERR "oprofile: CPU%u enabling wrong PMNC counter"
+ " %d\n", smp_processor_id(), cnt);
+ return -1;
+ }
+
+ if (cnt == ARMV7_CCNT)
+ val = ARMV7_CNTENS_C;
+ else
+ val = (1 << (cnt - ARMV7_CNT0));
+
+ val &= ARMV7_CNTENS_MASK;
+ asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r" (val));
+
+ return cnt;
+}
+
+static inline u32 armv7_pmnc_disable_counter(unsigned int cnt) +{
+ u32 val;
+
+ cnt -= ARMV7_COUNTER_TO_CCNT;
+
+ if (cnt >= ARMV7_CNTMAX) {
+ printk(KERN_ERR "oprofile: CPU%u disabling wrong PMNC counter"
+ " %d\n", smp_processor_id(), cnt);
+ return -1;
+ }
+
+ if (cnt == ARMV7_CCNT)
+ val = ARMV7_CNTENC_C;
+ else
+ val = (1 << (cnt - ARMV7_CNT0));
+
+ val &= ARMV7_CNTENC_MASK;
+ asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r" (val));
+
+ return cnt;
+}
+
+static inline u32 armv7_pmnc_enable_intens(unsigned int cnt) +{
+ u32 val;
+
+ cnt -= ARMV7_COUNTER_TO_CCNT;
+
+ if (cnt >= ARMV7_CNTMAX) {
+ printk(KERN_ERR "oprofile: CPU%u enabling wrong PMNC counter"
+ " interrupt enable %d\n", smp_processor_id(), cnt);
+ return -1;
+ }
+
+ if (cnt == ARMV7_CCNT)
+ val = ARMV7_INTENS_C;
+ else
+ val = (1 << (cnt - ARMV7_CNT0));
+
+ val &= ARMV7_INTENS_MASK;
+ asm volatile("mcr p15, 0, %0, c9, c14, 1" : : "r" (val));
+
+ return cnt;
+}
+
+static inline u32 armv7_pmnc_disable_intens(unsigned int cnt) +{
+ u32 val;
+
+ cnt -= ARMV7_COUNTER_TO_CCNT;
+
+ if (cnt >= ARMV7_CNTMAX) {
+ printk(KERN_ERR "oprofile: CPU%u disabling wrong PMNC counter"
+ " interrupt enable %d\n", smp_processor_id(), cnt);
+ return -1;
+ }
+
+ if (cnt == ARMV7_CCNT)
+ val = ARMV7_INTENC_C;
+ else
+ val = (1 << (cnt - ARMV7_CNT0));
+
+ val &= ARMV7_INTENC_MASK;
+ asm volatile("mcr p15, 0, %0, c9, c14, 2" : : "r" (val));
+
+ return cnt;
+}
+
+static inline u32 armv7_pmnc_getreset_flags(void)
+{
+ u32 val;
+
+ /* Read */
+ asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
+
+ /* Write to clear flags */
+ val &= ARMV7_FLAG_MASK;
+ asm volatile("mcr p15, 0, %0, c9, c12, 3" : : "r" (val));
+
+ return val;
+}
+
+void armv7pmu_enable_event(struct hw_perf_event *hwc, int idx) +{
+ unsigned long flags;
+
+ /*
+ * Enable counter and interrupt, and set the counter to count
+ * the event that we're interested in.
+ */
+ spin_lock_irqsave(&pmu_lock, flags);
+
+ /*
+ * Disable counter
+ */
+ armv7_pmnc_disable_counter(idx);
+
+ /*
+ * Set event (if destined for PMNx counters)
+ * We don't need to set the event if it's a cycle count
+ */
+ if (idx != ARMV7_CYCLE_COUNTER)
+ armv7_pmnc_write_evtsel(idx, hwc->config_base);
+
+ /*
+ * Enable interrupt for this counter
+ */
+ armv7_pmnc_enable_intens(idx);
+
+ /*
+ * Enable counter
+ */
+ armv7_pmnc_enable_counter(idx);
+
+ spin_unlock_irqrestore(&pmu_lock, flags);
+}
+
+static void armv7pmu_disable_event(struct hw_perf_event *hwc, int idx) +{
+ unsigned long flags;
+
+ /*
+ * Disable counter and interrupt
+ */
+ spin_lock_irqsave(&pmu_lock, flags);
+
+ /*
+ * Disable counter
+ */
+ armv7_pmnc_disable_counter(idx);
+
+ /*
+ * Disable interrupt for this counter
+ */
+ armv7_pmnc_disable_intens(idx);
+
+ spin_unlock_irqrestore(&pmu_lock, flags);
+}
+
+static irqreturn_t armv7pmu_handle_irq(int irq_num, void *dev) +{
+ unsigned long pmnc;
+ struct perf_sample_data data;
+ struct cpu_hw_events *cpuc;
+ struct pt_regs *regs;
+ int idx;
+
+ /*
+ * Get and reset the IRQ flags
+ */
+ pmnc = armv7_pmnc_getreset_flags();
+
+ /*
+ * Did an overflow occur?
+ */
+ if (!armv7_pmnc_has_overflowed(pmnc))
+ return IRQ_NONE;
+
+ /*
+ * Handle the counter(s) overflow(s)
+ */
+ regs = get_irq_regs();
+
+ data.addr = 0;
+
+ cpuc = &__get_cpu_var(cpu_hw_events);
+ for (idx = 0; idx <= armpmu->num_events; ++idx) {
+ struct perf_event *event = cpuc->events[idx];
+ struct hw_perf_event *hwc;
+
+ if (!test_bit(idx, cpuc->active_mask))
+ continue;
+
+ /*
+ * We have a single interrupt for all counters. Check that
+ * each counter has overflowed before we process it.
+ */
+ if (!armv7_pmnc_counter_has_overflowed(pmnc, idx))
+ continue;
+
+ hwc = &event->hw;
+ armpmu_event_update(event, hwc, idx);
+ data.period = event->hw.last_period;
+ if (!armpmu_event_set_period(event, hwc, idx))
+ continue;
+
+ if (perf_event_overflow(event, 0, &data, regs))
+ armpmu->disable(hwc, idx);
+ }
+
+ /*
+ * Handle the pending perf events.
+ *
+ * Note: this call *must* be run with interrupts enabled. For
+ * platforms that can have the PMU interrupts raised as a PMI, this
+ * will not work.
+ */
+ perf_event_do_pending();
+
+ return IRQ_HANDLED;
+}
+
+static void armv7pmu_start(void)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&pmu_lock, flags);
+ /* Enable all counters */
+ armv7_pmnc_write(armv7_pmnc_read() | ARMV7_PMNC_E);
+ spin_unlock_irqrestore(&pmu_lock, flags);
+}
+
+static void armv7pmu_stop(void)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&pmu_lock, flags);
+ /* Disable all counters */
+ armv7_pmnc_write(armv7_pmnc_read() & ~ARMV7_PMNC_E);
+ spin_unlock_irqrestore(&pmu_lock, flags);
+}
+
+static inline int armv7pmu_event_map(int config)
+{
+ int mapping = armv7_perf_map[config];
+ if (HW_OP_UNSUPPORTED == mapping)
+ mapping = -EOPNOTSUPP;
+ return mapping;
+}
+
+static u64 armv7pmu_raw_event(u64 config)
+{
+ return config & 0xff;
+}
+
+static int armv7pmu_get_event_idx(struct cpu_hw_events *cpuc,
+ struct hw_perf_event *event)
+{
+ /* Always place a cycle counter into the cycle counter. */
+ if (ARMV7_PERFCTR_CPU_CYCLES == event->config_base) {
+ if (test_and_set_bit(ARMV7_CYCLE_COUNTER, cpuc->used_mask))
+ return -EAGAIN;
+
+ return ARMV7_CYCLE_COUNTER;
+ } else {
+ /*
+ * For anything other than a cycle counter, try and use
+ * counters 0..3
+ */
+ if (!test_and_set_bit(ARMV7_COUNTER0, cpuc->used_mask))
+ return ARMV7_COUNTER0;
+
+ if (!test_and_set_bit(ARMV7_COUNTER1, cpuc->used_mask))
+ return ARMV7_COUNTER1;
+
+ if (!test_and_set_bit(ARMV7_COUNTER2, cpuc->used_mask))
+ return ARMV7_COUNTER2;
+
+ if (!test_and_set_bit(ARMV7_COUNTER3, cpuc->used_mask))
+ return ARMV7_COUNTER3;
+
+ /* The counters are all in use. */
+ return -EAGAIN;
+ }
+}
+
+static struct arm_pmu armv7pmu = {
+ .name = "v7",
+ .handle_irq = armv7pmu_handle_irq,
+ .enable = armv7pmu_enable_event,
+ .disable = armv7pmu_disable_event,
+ .event_map = armv7pmu_event_map,
+ .raw_event = armv7pmu_raw_event,
+ .read_counter = armv7pmu_read_counter,
+ .write_counter = armv7pmu_write_counter,
+ .get_event_idx = armv7pmu_get_event_idx,
+ .start = armv7pmu_start,
+ .stop = armv7pmu_stop,
+ .num_events = 5,
+ .max_period = (1LLU << 32) - 1,
+};
+
static int __init
init_hw_perf_events(void)
{
@@ -977,6 +1676,13 @@ init_hw_perf_events(void)

                 memcpy(armpmu_perf_cache_map, armv6_perf_cache_map,
                        sizeof(armv6_perf_cache_map));
                 perf_max_events	= armv6pmu.num_events;

+ } else if (cpu_architecture() == CPU_ARCH_ARMv7) {
+ armpmu = &armv7pmu;
+ memcpy(armpmu_perf_cache_map, armv7_perf_cache_map,
+ sizeof(armv7_perf_cache_map));
+ perf_max_events = armv7pmu.num_events;
+ /* Initialize & Reset PMNC: C bit and P bit */

+		armv7_pmnc_write(ARMV7_PMNC_P | ARMV7_PMNC_C);
         } else {
                 pr_info("no hardware support available\n");
                 perf_max_events = -1;
Signature · 4 lines
1.6.0


---
Quoted 19 lines · Jamie Iles

This patch implements support for ARMv6 performance counters in the Linux performance events subsystem. ARMv6 architectures that have the performance counters should enable HW_PERF_EVENTS and define the interrupts for the counters in arch/arm/kernel/perf_event.c

This implementation also provides an ARM PMU abstraction layer to allow ARMv7 and others to be supported in the future by adding new a 'struct arm_pmu'.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 arch/arm/Kconfig             |    8 +
 arch/arm/kernel/Makefile     |    1 +
 arch/arm/kernel/perf_event.c | 1125 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 1134 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/kernel/perf_event.c

...

#16 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Quoted 6 lines · Jean Pihet

Here is a patch that adds the support for ARMv7 processors, using the PMNC HW unit.

The code is for review, it has been compiled and boot tested only, the complete testing is in progress. Please let me know if the patch is wrapped or garbled I will send it attached (20KB in size).

Excellent! It looks good to me, a few minor comments though. I don't know if it's my mail client but some of the longer lines appeared to wrap onto 2 patch lines but it's not difficult to apply.

[snip]

Quoted 4 lines

I had a question about the events mapping to user space. Although most of the events are mapped in the kernel code, some of the exotic events are not mapped (e.g. NEON or PMU related events). How to use those events from user space? Is it done using the raw mappings?

Yes, the raw events should do the trick. 'perf stat -a -e rff -- sleep 1' will do cycle counting on v6 using the raw event number.

Quoted 128 lines
(quoted diff) +66 -0
+enum armv7_perf_types {
+	ARMV7_PERFCTR_PMNC_SW_INCR		= 0x00,
+	ARMV7_PERFCTR_IFETCH_MISS		= 0x01,
+	ARMV7_PERFCTR_ITLB_MISS			= 0x02,
+	ARMV7_PERFCTR_DCACHE_REFILL		= 0x03,
+	ARMV7_PERFCTR_DCACHE_ACCESS		= 0x04,
+	ARMV7_PERFCTR_DTLB_REFILL		= 0x05,
+	ARMV7_PERFCTR_DREAD			= 0x06,
+	ARMV7_PERFCTR_DWRITE			= 0x07,
+	ARMV7_PERFCTR_INSTR_EXECUTED		= 0x08,
+	ARMV7_PERFCTR_EXC_TAKEN			= 0x09,
+	ARMV7_PERFCTR_EXC_EXECUTED		= 0x0A,
+	ARMV7_PERFCTR_CID_WRITE			= 0x0B,
+	ARMV7_PERFCTR_PC_WRITE			= 0x0C,
+	ARMV7_PERFCTR_PC_IMM_BRANCH		= 0x0D,
+	ARMV7_PERFCTR_PC_PROC_RETURN		= 0x0E,
+	ARMV7_PERFCTR_UNALIGNED_ACCESS		= 0x0F,
+	ARMV7_PERFCTR_PC_BRANCH_MIS_PRED	= 0x10,
+
+	ARMV7_PERFCTR_PC_BRANCH_MIS_USED	= 0x12,
+
+	ARMV7_PERFCTR_WRITE_BUFFER_FULL		= 0x40,
+	ARMV7_PERFCTR_L2_STORE_MERGED		= 0x41,
+	ARMV7_PERFCTR_L2_STORE_BUFF		= 0x42,
+	ARMV7_PERFCTR_L2_ACCESS			= 0x43,
+	ARMV7_PERFCTR_L2_CACH_MISS		= 0x44,
+	ARMV7_PERFCTR_AXI_READ_CYCLES		= 0x45,
+	ARMV7_PERFCTR_AXI_WRITE_CYCLES		= 0x46,
+	ARMV7_PERFCTR_MEMORY_REPLAY		= 0x47,
+	ARMV7_PERFCTR_UNALIGNED_ACCESS_REPLAY	= 0x48,
+	ARMV7_PERFCTR_L1_DATA_MISS		= 0x49,
+	ARMV7_PERFCTR_L1_INST_MISS		= 0x4A,
+	ARMV7_PERFCTR_L1_DATA_COLORING		= 0x4B,
+	ARMV7_PERFCTR_L1_NEON_DATA		= 0x4C,
+	ARMV7_PERFCTR_L1_NEON_CACH_DATA		= 0x4D,
+	ARMV7_PERFCTR_L2_NEON			= 0x4E,
+	ARMV7_PERFCTR_L2_NEON_HIT		= 0x4F,
+	ARMV7_PERFCTR_L1_INST			= 0x50,
+	ARMV7_PERFCTR_PC_RETURN_MIS_PRED	= 0x51,
+	ARMV7_PERFCTR_PC_BRANCH_FAILED		= 0x52,
+	ARMV7_PERFCTR_PC_BRANCH_TAKEN		= 0x53,
+	ARMV7_PERFCTR_PC_BRANCH_EXECUTED	= 0x54,
+	ARMV7_PERFCTR_OP_EXECUTED		= 0x55,
+	ARMV7_PERFCTR_CYCLES_INST_STALL		= 0x56,
+	ARMV7_PERFCTR_CYCLES_INST		= 0x57,
+	ARMV7_PERFCTR_CYCLES_NEON_DATA_STALL	= 0x58,
+	ARMV7_PERFCTR_CYCLES_NEON_INST_STALL	= 0x59,
+	ARMV7_PERFCTR_NEON_CYCLES		= 0x5A,
+
+	ARMV7_PERFCTR_PMU0_EVENTS		= 0x70,
+	ARMV7_PERFCTR_PMU1_EVENTS		= 0x71,
+	ARMV7_PERFCTR_PMU_EVENTS		= 0x72,
+
+	ARMV7_PERFCTR_CPU_CYCLES		= 0xFF
+};
+
+enum armv7_counters {
+	ARMV7_CYCLE_COUNTER = 1,
+	ARMV7_COUNTER0,
+	ARMV7_COUNTER1,
+	ARMV7_COUNTER2,
+	ARMV7_COUNTER3,
+};
+
+/*
+ * The hardware events that we support. We do support cache operations

but
+ * we have harvard caches and no way to combine instruction and data
+ * accesses/misses in hardware.
+ */

(quoted diff) +58 -0
+static const unsigned armv7_perf_map[PERF_COUNT_HW_MAX] = {
+	[PERF_COUNT_HW_CPU_CYCLES]	    = ARMV7_PERFCTR_CPU_CYCLES,
+	[PERF_COUNT_HW_INSTRUCTIONS]	    = ARMV7_PERFCTR_INSTR_EXECUTED,
+	[PERF_COUNT_HW_CACHE_REFERENCES]    = HW_OP_UNSUPPORTED,
+	[PERF_COUNT_HW_CACHE_MISSES]	    = HW_OP_UNSUPPORTED,
+	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_BRANCH_TAKEN,
+	[PERF_COUNT_HW_BRANCH_MISSES]	    = ARMV7_PERFCTR_PC_BRANCH_FAILED,
+	[PERF_COUNT_HW_BUS_CYCLES]	    = HW_OP_UNSUPPORTED,
+};
+
+static const unsigned armv7_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
+					  [PERF_COUNT_HW_CACHE_OP_MAX]
+					  [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
+	[C(L1D)] = {
+		/*
+		 * The performance counters don't differentiate between read
+		 * and write accesses/misses so this isn't strictly correct,
+		 * but it's the best we can do. Writes and reads get
+		 * combined.
+		 */
+		[C(OP_READ)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_DCACHE_ACCESS,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_DATA_MISS,
+		},
+		[C(OP_WRITE)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_DCACHE_ACCESS,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_DATA_MISS,
+		},
+		[C(OP_PREFETCH)] = {
+			[C(RESULT_ACCESS)]	= CACHE_OP_UNSUPPORTED,
+			[C(RESULT_MISS)]	= CACHE_OP_UNSUPPORTED,
+		},
+	},
+	[C(L1I)] = {
+		[C(OP_READ)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_L1_INST,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_INST_MISS,
+		},
+		[C(OP_WRITE)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_L1_INST,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_INST_MISS,
+		},
+		[C(OP_PREFETCH)] = {
+			[C(RESULT_ACCESS)]	= CACHE_OP_UNSUPPORTED,
+			[C(RESULT_MISS)]	= CACHE_OP_UNSUPPORTED,
+		},
+	},
+	[C(LL)] = {
+		[C(OP_READ)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_L2_ACCESS,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L2_CACH_MISS,
+		},
+		[C(OP_WRITE)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_L2_ACCESS,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L2_CACH_MISS,
+		},
+		[C(OP_PREFETCH)] = {
+			[C(RESULT_ACCESS)]	= CACHE_OP_UNSUPPORTED,

Review

Quoted 10 lines
(quoted diff) +10 -0
+			[C(RESULT_MISS)]	= CACHE_OP_UNSUPPORTED,
+		},
+	},
+	[C(DTLB)] = {
+		/*
+		 * The ARM performance counters can count micro DTLB misses,
+		 * micro ITLB misses and main TLB misses. There isn't an event
+		 * for TLB misses, so use the micro misses here and if users
+		 * want the main TLB misses they can use a raw counter.
+		 */

I think this comment needs to be changed for v7. From the events enum it doesn't look like v7 has micro tlb events.

Review

Quoted 9 lines
(quoted diff) +9 -0
+static inline int armv7_pmnc_select_counter(unsigned int cnt)
+{
+	u32 val;
+
+	cnt -= ARMV7_COUNTER_TO_CCNT;
+
+	if ((cnt == ARMV7_CCNT) || (cnt >= ARMV7_CNTMAX)) {
+		printk(KERN_ERR "oprofile: CPU%u selecting wrong PMNC counter"
+			" %d\n", smp_processor_id(), cnt);

Most of the printk's refer to oprofile. Could we use pr_err() etc so we get the same prefix for all messages?

[snip]

Review

Quoted 3 lines
(quoted diff) +3 -0
+		if (armv7_pmnc_select_counter(counter) == counter)
+			asm volatile("mrc p15, 0, %0, c9, c13, 2"
+				     : "=r" (value));

Does this sequence need some locking to make sure that we really do read from the counter that we've selected? The same applies to the other places.

Jamie

#17 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Quoted 9 lines · Jamie Iles

[snip]

Quoted 4 lines

I had a question about the events mapping to user space. Although most of the events are mapped in the kernel code, some of the exotic events are not mapped (e.g. NEON or PMU related events). How to use those events from user space? Is it done using the raw mappings?

Yes, the raw events should do the trick. 'perf stat -a -e rff -- sleep 1' will do cycle counting on v6 using the raw event number.

Sidenote: if some of the more exotic events turn out to be useful and are worth generalizing, then we can add them to the generic enumeration and add tooling support (symbols, aliases, listing, etc.) for it.

The current set of generic events are intended to be a 'seed' set, to be extended on an as-needed basis - not cast into stone in any way.

The current generic (hardware) events are (from 'perf list' output):

  cpu-cycles OR cycles                       [Hardware event]
  instructions                               [Hardware event]
  cache-references                           [Hardware event]
  cache-misses                               [Hardware event]
  branch-instructions OR branches            [Hardware event]
  branch-misses                              [Hardware event]
  bus-cycles                                 [Hardware event]
  L1-dcache-loads                            [Hardware cache event]
  L1-dcache-load-misses                      [Hardware cache event]
  L1-dcache-stores                           [Hardware cache event]
  L1-dcache-store-misses                     [Hardware cache event]
  L1-dcache-prefetches                       [Hardware cache event]
  L1-dcache-prefetch-misses                  [Hardware cache event]
  L1-icache-loads                            [Hardware cache event]
  L1-icache-load-misses                      [Hardware cache event]
  L1-icache-prefetches                       [Hardware cache event]
  L1-icache-prefetch-misses                  [Hardware cache event]
  LLC-loads                                  [Hardware cache event]
  LLC-load-misses                            [Hardware cache event]
  LLC-stores                                 [Hardware cache event]
  LLC-store-misses                           [Hardware cache event]
  LLC-prefetches                             [Hardware cache event]
  LLC-prefetch-misses                        [Hardware cache event]
  dTLB-loads                                 [Hardware cache event]
  dTLB-load-misses                           [Hardware cache event]
  dTLB-stores                                [Hardware cache event]
  dTLB-store-misses                          [Hardware cache event]
  dTLB-prefetches                            [Hardware cache event]
  dTLB-prefetch-misses                       [Hardware cache event]
  iTLB-loads                                 [Hardware cache event]
  iTLB-load-misses                           [Hardware cache event]
  branch-loads                               [Hardware cache event]
  branch-load-misses                         [Hardware cache event]
  rNNN                                       [raw hardware event descriptor]
  mem:<addr>[:access]                        [hardware breakpoint]

But i think we might want to capture FPU-alike instructions as well on CPUs that can count/sample them - etc.

Ingo

#18 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Quoted 54 lines · On Saturday 19 December 2009 11:53:43 Ingo Molnar wrote:
Quoted 9 lines · Jamie Iles

[snip]

Quoted 4 lines

I had a question about the events mapping to user space. Although most of the events are mapped in the kernel code, some of the exotic events are not mapped (e.g. NEON or PMU related events). How to use those events from user space? Is it done using the raw mappings?

Yes, the raw events should do the trick. 'perf stat -a -e rff -- sleep 1' will do cycle counting on v6 using the raw event number.

Sidenote: if some of the more exotic events turn out to be useful and are worth generalizing, then we can add them to the generic enumeration and add tooling support (symbols, aliases, listing, etc.) for it.

The current set of generic events are intended to be a 'seed' set, to be extended on an as-needed basis - not cast into stone in any way.

The current generic (hardware) events are (from 'perf list' output):

  cpu-cycles OR cycles                       [Hardware event]
  instructions                               [Hardware event]
  cache-references                           [Hardware event]
  cache-misses                               [Hardware event]
  branch-instructions OR branches            [Hardware event]
  branch-misses                              [Hardware event]
  bus-cycles                                 [Hardware event]
  L1-dcache-loads                            [Hardware cache event]
  L1-dcache-load-misses                      [Hardware cache event]
  L1-dcache-stores                           [Hardware cache event]
  L1-dcache-store-misses                     [Hardware cache event]
  L1-dcache-prefetches                       [Hardware cache event]
  L1-dcache-prefetch-misses                  [Hardware cache event]
  L1-icache-loads                            [Hardware cache event]
  L1-icache-load-misses                      [Hardware cache event]
  L1-icache-prefetches                       [Hardware cache event]
  L1-icache-prefetch-misses                  [Hardware cache event]
  LLC-loads                                  [Hardware cache event]
  LLC-load-misses                            [Hardware cache event]
  LLC-stores                                 [Hardware cache event]
  LLC-store-misses                           [Hardware cache event]
  LLC-prefetches                             [Hardware cache event]
  LLC-prefetch-misses                        [Hardware cache event]
  dTLB-loads                                 [Hardware cache event]
  dTLB-load-misses                           [Hardware cache event]
  dTLB-stores                                [Hardware cache event]
  dTLB-store-misses                          [Hardware cache event]
  dTLB-prefetches                            [Hardware cache event]
  dTLB-prefetch-misses                       [Hardware cache event]
  iTLB-loads                                 [Hardware cache event]
  iTLB-load-misses                           [Hardware cache event]
  branch-loads                               [Hardware cache event]
  branch-load-misses                         [Hardware cache event]
  rNNN                                       [raw hardware event
Quoted 6 lines

descriptor] mem:<addr>[:access] [hardware
breakpoint]

But i think we might want to capture FPU-alike instructions as well on CPUs that can count/sample them - etc.

Ok, thanks for the information!
We might need to generalize the ARMv7 CPUs for the moment the processors are Cortex-A8 (supported by the proposed patch) and Cortex-A9 (not yet supported but should come soon).

Quoted 1 line

Ingo

Jean

#19 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

In reply to Jamie Iles
Quoted 14 lines · On Saturday 19 December 2009 11:29:05 Jamie Iles wrote:
Quoted 6 lines · Jean Pihet

Here is a patch that adds the support for ARMv7 processors, using the PMNC HW unit.

The code is for review, it has been compiled and boot tested only, the complete testing is in progress. Please let me know if the patch is wrapped or garbled I will send it attached (20KB in size).

Excellent! It looks good to me, a few minor comments though. I don't know if it's my mail client but some of the longer lines appeared to wrap onto 2 patch lines but it's not difficult to apply.

[snip]

Quoted 7 lines
Quoted 4 lines

I had a question about the events mapping to user space. Although most of the events are mapped in the kernel code, some of the exotic events are not mapped (e.g. NEON or PMU related events). How to use those events from user space? Is it done using the raw mappings?

Yes, the raw events should do the trick. 'perf stat -a -e rff -- sleep 1' will do cycle counting on v6 using the raw event number.

Ok.

Quoted 134 lines
Quoted 134 lines
(quoted diff) +66 -0
+enum armv7_perf_types {
+	ARMV7_PERFCTR_PMNC_SW_INCR		= 0x00,
+	ARMV7_PERFCTR_IFETCH_MISS		= 0x01,
+	ARMV7_PERFCTR_ITLB_MISS			= 0x02,
+	ARMV7_PERFCTR_DCACHE_REFILL		= 0x03,
+	ARMV7_PERFCTR_DCACHE_ACCESS		= 0x04,
+	ARMV7_PERFCTR_DTLB_REFILL		= 0x05,
+	ARMV7_PERFCTR_DREAD			= 0x06,
+	ARMV7_PERFCTR_DWRITE			= 0x07,
+	ARMV7_PERFCTR_INSTR_EXECUTED		= 0x08,
+	ARMV7_PERFCTR_EXC_TAKEN			= 0x09,
+	ARMV7_PERFCTR_EXC_EXECUTED		= 0x0A,
+	ARMV7_PERFCTR_CID_WRITE			= 0x0B,
+	ARMV7_PERFCTR_PC_WRITE			= 0x0C,
+	ARMV7_PERFCTR_PC_IMM_BRANCH		= 0x0D,
+	ARMV7_PERFCTR_PC_PROC_RETURN		= 0x0E,
+	ARMV7_PERFCTR_UNALIGNED_ACCESS		= 0x0F,
+	ARMV7_PERFCTR_PC_BRANCH_MIS_PRED	= 0x10,
+
+	ARMV7_PERFCTR_PC_BRANCH_MIS_USED	= 0x12,
+
+	ARMV7_PERFCTR_WRITE_BUFFER_FULL		= 0x40,
+	ARMV7_PERFCTR_L2_STORE_MERGED		= 0x41,
+	ARMV7_PERFCTR_L2_STORE_BUFF		= 0x42,
+	ARMV7_PERFCTR_L2_ACCESS			= 0x43,
+	ARMV7_PERFCTR_L2_CACH_MISS		= 0x44,
+	ARMV7_PERFCTR_AXI_READ_CYCLES		= 0x45,
+	ARMV7_PERFCTR_AXI_WRITE_CYCLES		= 0x46,
+	ARMV7_PERFCTR_MEMORY_REPLAY		= 0x47,
+	ARMV7_PERFCTR_UNALIGNED_ACCESS_REPLAY	= 0x48,
+	ARMV7_PERFCTR_L1_DATA_MISS		= 0x49,
+	ARMV7_PERFCTR_L1_INST_MISS		= 0x4A,
+	ARMV7_PERFCTR_L1_DATA_COLORING		= 0x4B,
+	ARMV7_PERFCTR_L1_NEON_DATA		= 0x4C,
+	ARMV7_PERFCTR_L1_NEON_CACH_DATA		= 0x4D,
+	ARMV7_PERFCTR_L2_NEON			= 0x4E,
+	ARMV7_PERFCTR_L2_NEON_HIT		= 0x4F,
+	ARMV7_PERFCTR_L1_INST			= 0x50,
+	ARMV7_PERFCTR_PC_RETURN_MIS_PRED	= 0x51,
+	ARMV7_PERFCTR_PC_BRANCH_FAILED		= 0x52,
+	ARMV7_PERFCTR_PC_BRANCH_TAKEN		= 0x53,
+	ARMV7_PERFCTR_PC_BRANCH_EXECUTED	= 0x54,
+	ARMV7_PERFCTR_OP_EXECUTED		= 0x55,
+	ARMV7_PERFCTR_CYCLES_INST_STALL		= 0x56,
+	ARMV7_PERFCTR_CYCLES_INST		= 0x57,
+	ARMV7_PERFCTR_CYCLES_NEON_DATA_STALL	= 0x58,
+	ARMV7_PERFCTR_CYCLES_NEON_INST_STALL	= 0x59,
+	ARMV7_PERFCTR_NEON_CYCLES		= 0x5A,
+
+	ARMV7_PERFCTR_PMU0_EVENTS		= 0x70,
+	ARMV7_PERFCTR_PMU1_EVENTS		= 0x71,
+	ARMV7_PERFCTR_PMU_EVENTS		= 0x72,
+
+	ARMV7_PERFCTR_CPU_CYCLES		= 0xFF
+};
+
+enum armv7_counters {
+	ARMV7_CYCLE_COUNTER = 1,
+	ARMV7_COUNTER0,
+	ARMV7_COUNTER1,
+	ARMV7_COUNTER2,
+	ARMV7_COUNTER3,
+};
+
+/*
+ * The hardware events that we support. We do support cache operations

but
+ * we have harvard caches and no way to combine instruction and data
+ * accesses/misses in hardware.
+ */

(quoted diff) +63 -0
+static const unsigned armv7_perf_map[PERF_COUNT_HW_MAX] = {
+	[PERF_COUNT_HW_CPU_CYCLES]	    = ARMV7_PERFCTR_CPU_CYCLES,
+	[PERF_COUNT_HW_INSTRUCTIONS]	    = ARMV7_PERFCTR_INSTR_EXECUTED,
+	[PERF_COUNT_HW_CACHE_REFERENCES]    = HW_OP_UNSUPPORTED,
+	[PERF_COUNT_HW_CACHE_MISSES]	    = HW_OP_UNSUPPORTED,
+	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_BRANCH_TAKEN,
+	[PERF_COUNT_HW_BRANCH_MISSES]	    = ARMV7_PERFCTR_PC_BRANCH_FAILED,
+	[PERF_COUNT_HW_BUS_CYCLES]	    = HW_OP_UNSUPPORTED,
+};
+
+static const unsigned armv7_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
+					  [PERF_COUNT_HW_CACHE_OP_MAX]
+					  [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
+	[C(L1D)] = {
+		/*
+		 * The performance counters don't differentiate between read
+		 * and write accesses/misses so this isn't strictly correct,
+		 * but it's the best we can do. Writes and reads get
+		 * combined.
+		 */
+		[C(OP_READ)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_DCACHE_ACCESS,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_DATA_MISS,
+		},
+		[C(OP_WRITE)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_DCACHE_ACCESS,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_DATA_MISS,
+		},
+		[C(OP_PREFETCH)] = {
+			[C(RESULT_ACCESS)]	= CACHE_OP_UNSUPPORTED,
+			[C(RESULT_MISS)]	= CACHE_OP_UNSUPPORTED,
+		},
+	},
+	[C(L1I)] = {
+		[C(OP_READ)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_L1_INST,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_INST_MISS,
+		},
+		[C(OP_WRITE)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_L1_INST,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_INST_MISS,
+		},
+		[C(OP_PREFETCH)] = {
+			[C(RESULT_ACCESS)]	= CACHE_OP_UNSUPPORTED,
+			[C(RESULT_MISS)]	= CACHE_OP_UNSUPPORTED,
+		},
+	},
+	[C(LL)] = {
+		[C(OP_READ)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_L2_ACCESS,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L2_CACH_MISS,
+		},
+		[C(OP_WRITE)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_L2_ACCESS,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L2_CACH_MISS,
+		},
+		[C(OP_PREFETCH)] = {
+			[C(RESULT_ACCESS)]	= CACHE_OP_UNSUPPORTED,
+			[C(RESULT_MISS)]	= CACHE_OP_UNSUPPORTED,
+		},
+	},
+	[C(DTLB)] = {
+		/*
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+		 * The ARM performance counters can count micro DTLB misses,
+		 * micro ITLB misses and main TLB misses. There isn't an event
+		 * for TLB misses, so use the micro misses here and if users
+		 * want the main TLB misses they can use a raw counter.
+		 */

I think this comment needs to be changed for v7. From the events enum it doesn't look like v7 has micro tlb events.

Yes I need to correct this.

Quoted 4 lines
Quoted 4 lines
(quoted diff) +4 -0
+static inline int armv7_pmnc_select_counter(unsigned int cnt)
+{
+	u32 val;
+
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+	cnt -= ARMV7_COUNTER_TO_CCNT;
+
+	if ((cnt == ARMV7_CCNT) || (cnt >= ARMV7_CNTMAX)) {
+		printk(KERN_ERR "oprofile: CPU%u selecting wrong PMNC counter"
+			" %d\n", smp_processor_id(), cnt);

Most of the printk's refer to oprofile. Could we use pr_err() etc so we get the same prefix for all messages?

Oops this is a left over from Oprofile. I will correct the message and use the pr_ macros instead of printk.

Quoted 8 lines

[snip]

Review

Quoted 3 lines
(quoted diff) +3 -0
+		if (armv7_pmnc_select_counter(counter) == counter)
+			asm volatile("mrc p15, 0, %0, c9, c13, 2"
+				     : "=r" (value));

Does this sequence need some locking to make sure that we really do read from the counter that we've selected? The same applies to the other places.

In fact armv7_pmnc_select_counter is used by armv7pmu_read_counter, armv7pmu_write_counter and armv7pmu_enable_event that are called by the perf events generic code. Is that enough of a guarantee for atomic accesses, or we need some extra locking?

I will post a new version as soon as the changes are made and after some testing on a board.

Quoted 1 line

Jamie

Thanks,
Jean

#20 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

In reply to Jean Pihet

Hi Jean,

I've provided some comments inline. Hopefully they're useful.

Quoted 11 lines · Jean Pihet

Hello,

Here is a patch that adds the support for ARMv7 processors, using the PMNC HW unit.

The code is for review, it has been compiled and boot tested only, the complete testing is in progress. Please let me know if the patch is wrapped or garbled I will send it attached (20KB in size).

Feedback is welcome.

<snip>

Quoted 24 lines
arch/arm/kernel/perf_event.c +1 -0
index abb5267..79e92ce 100644
@@ -4,6 +4,7 @@
  * ARM performance counter support.
  *
  * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
+ * ARMv7 support: Jean Pihet <[email protected]>
  *
  * This code is based on the sparc64 perf event code, which is in turn

based
* on the x86 code. Callchain code is based on the ARM OProfile
backtrace

arch/arm/kernel/perf_event.c +0 -0
@@ -35,8 +36,11 @@ DEFINE_SPINLOCK(pmu_lock);
  * ARMv6 supports a maximum of 3 events, starting from index 1. If we

add
* another platform that supports more, we need to increase this to be
the
* largest of all platforms.
+ *
+ * ARMv7 supports up to 5 events:
+ * cycle counter CCNT + 4 events counters CNT0..3
*/

arch/arm/kernel/perf_event.c

Quoted 2 lines
arch/arm/kernel/perf_event.c +1 -1
-#define ARMPMU_MAX_HWEVENTS		4
+#define ARMPMU_MAX_HWEVENTS		5

The maximum number of event counters on ARMv7 is currently 6 [Cortex-A9], plus a cycle counter. Additionally, the number of event counters actually available is implementation defined (the cycle counter is mandatory). You can find out the number of event counters using the PMCR ((PMCR >> 11) & 0x1f).

Quoted 31 lines

/* The events for a given CPU. */
struct cpu_hw_events {

arch/arm/kernel/perf_event.c +7 -0
@@ -965,6 +969,701 @@ static struct arm_pmu armv6pmu = {
 	.max_period		= (1LLU << 32) - 1,
 };
 
+/*
+ * ARMv7 Performance counter handling code.
+ *
+ * Copied from ARMv6 code, with the low level code inspired
+ *  by the ARMv7 Oprofile code.
+ *
+ * ARMv7 has 4 configurable performance counters and a single cycle

counter.
+ * All counters can be enabled/disabled and IRQ masked separately. The cycle
+ * counter and all 4 performance counters together can be reset separately.
+ */

arch/arm/kernel/perf_event.c +11 -0
+
+enum armv7_perf_types {
+	ARMV7_PERFCTR_PMNC_SW_INCR		= 0x00,
+	ARMV7_PERFCTR_IFETCH_MISS		= 0x01,
+	ARMV7_PERFCTR_ITLB_MISS			= 0x02,
+	ARMV7_PERFCTR_DCACHE_REFILL		= 0x03,
+	ARMV7_PERFCTR_DCACHE_ACCESS		= 0x04,
+	ARMV7_PERFCTR_DTLB_REFILL		= 0x05,
+	ARMV7_PERFCTR_DREAD			= 0x06,
+	ARMV7_PERFCTR_DWRITE			= 0x07,
+	ARMV7_PERFCTR_INSTR_EXECUTED		= 0x08,

arch/arm/kernel/perf_event.c

Quoted 10 lines
arch/arm/kernel/perf_event.c +10 -0
+	ARMV7_PERFCTR_EXC_TAKEN			= 0x09,
+	ARMV7_PERFCTR_EXC_EXECUTED		= 0x0A,
+	ARMV7_PERFCTR_CID_WRITE			= 0x0B,
+	ARMV7_PERFCTR_PC_WRITE			= 0x0C,
+	ARMV7_PERFCTR_PC_IMM_BRANCH		= 0x0D,
+	ARMV7_PERFCTR_PC_PROC_RETURN		= 0x0E,
+	ARMV7_PERFCTR_UNALIGNED_ACCESS		= 0x0F,
+	ARMV7_PERFCTR_PC_BRANCH_MIS_PRED	= 0x10,
+
+	ARMV7_PERFCTR_PC_BRANCH_MIS_USED	= 0x12,

Ok - the events so far are defined by the v7 architecture. Note that this doesn't necessarily mean they are all supported by the core.

Quoted 24 lines
arch/arm/kernel/perf_event.c +24 -0
+	ARMV7_PERFCTR_WRITE_BUFFER_FULL		= 0x40,
+	ARMV7_PERFCTR_L2_STORE_MERGED		= 0x41,
+	ARMV7_PERFCTR_L2_STORE_BUFF		= 0x42,
+	ARMV7_PERFCTR_L2_ACCESS			= 0x43,
+	ARMV7_PERFCTR_L2_CACH_MISS		= 0x44,
+	ARMV7_PERFCTR_AXI_READ_CYCLES		= 0x45,
+	ARMV7_PERFCTR_AXI_WRITE_CYCLES		= 0x46,
+	ARMV7_PERFCTR_MEMORY_REPLAY		= 0x47,
+	ARMV7_PERFCTR_UNALIGNED_ACCESS_REPLAY	= 0x48,
+	ARMV7_PERFCTR_L1_DATA_MISS		= 0x49,
+	ARMV7_PERFCTR_L1_INST_MISS		= 0x4A,
+	ARMV7_PERFCTR_L1_DATA_COLORING		= 0x4B,
+	ARMV7_PERFCTR_L1_NEON_DATA		= 0x4C,
+	ARMV7_PERFCTR_L1_NEON_CACH_DATA		= 0x4D,
+	ARMV7_PERFCTR_L2_NEON			= 0x4E,
+	ARMV7_PERFCTR_L2_NEON_HIT		= 0x4F,
+	ARMV7_PERFCTR_L1_INST			= 0x50,
+	ARMV7_PERFCTR_PC_RETURN_MIS_PRED	= 0x51,
+	ARMV7_PERFCTR_PC_BRANCH_FAILED		= 0x52,
+	ARMV7_PERFCTR_PC_BRANCH_TAKEN		= 0x53,
+	ARMV7_PERFCTR_PC_BRANCH_EXECUTED	= 0x54,
+	ARMV7_PERFCTR_OP_EXECUTED		= 0x55,
+	ARMV7_PERFCTR_CYCLES_INST_STALL		= 0x56,
+	ARMV7_PERFCTR_CYCLES_INST		= 0x57,

arch/arm/kernel/perf_event.c

Quoted 10 lines
arch/arm/kernel/perf_event.c +10 -0
+	ARMV7_PERFCTR_CYCLES_NEON_DATA_STALL	= 0x58,
+	ARMV7_PERFCTR_CYCLES_NEON_INST_STALL	= 0x59,
+	ARMV7_PERFCTR_NEON_CYCLES		= 0x5A,
+
+	ARMV7_PERFCTR_PMU0_EVENTS		= 0x70,
+	ARMV7_PERFCTR_PMU1_EVENTS		= 0x71,
+	ARMV7_PERFCTR_PMU_EVENTS		= 0x72,
+
+	ARMV7_PERFCTR_CPU_CYCLES		= 0xFF
+};

These events are specific to the Cortex-A8. Unfortunately, these numbers clash with events specific to the Cortex-A9 [and potentially future v7 cores]. For example, 0x40 on the A8 is WRITE_BUFFER_FULL but on the A9 it is JAVA_BYTECODE_EXEC. This means that you'll need to take a similar approach as was taken for ARM11MP vs ARM11*.

<snip>

arch/arm/kernel/perf_event.c

Quoted 12 lines
arch/arm/kernel/perf_event.c +12 -0
+/*
+ * Available counters
+ */
+#define ARMV7_CCNT 		0
+#define ARMV7_CNT0 		1
+#define ARMV7_CNT1 		2
+#define ARMV7_CNT2 		3
+#define ARMV7_CNT3 		4
+#define ARMV7_CNTMAX 		5
+#define ARMV7_COUNTER_TO_CCNT	(ARMV7_CYCLE_COUNTER - ARMV7_CCNT)
+
+#define ARMV7_CPU_COUNTER(cpu, counter)	((cpu) * CNTMAX + (counter))

You don't use this macro. I imagine there are others which are no longer used too.

<snip>

arch/arm/kernel/perf_event.c

Quoted 11 lines
arch/arm/kernel/perf_event.c +11 -0
+static inline int armv7_pmnc_select_counter(unsigned int cnt)
+{
+	u32 val;
+
+	cnt -= ARMV7_COUNTER_TO_CCNT;
+
+	if ((cnt == ARMV7_CCNT) || (cnt >= ARMV7_CNTMAX)) {
+		printk(KERN_ERR "oprofile: CPU%u selecting wrong PMNC counter"
+			" %d\n", smp_processor_id(), cnt);
+		return -1;
+	}

Nice error message :)

<snip>

Quoted 7 lines

static int __init
init_hw_perf_events(void)
{

arch/arm/kernel/perf_event.c +0 -0
@@ -977,6 +1676,13 @@ init_hw_perf_events(void)
                 memcpy(armpmu_perf_cache_map, armv6_perf_cache_map,
                        sizeof(armv6_perf_cache_map));
                 perf_max_events	= armv6pmu.num_events;

arch/arm/kernel/perf_event.c init_hw_perf_events(void) · line 1688

Quoted 10 lines
arch/arm/kernel/perf_event.c +7 -0
+	} else if (cpu_architecture() == CPU_ARCH_ARMv7) {
+		armpmu = &armv7pmu;
+		memcpy(armpmu_perf_cache_map, armv7_perf_cache_map,
+			sizeof(armv7_perf_cache_map));
+		perf_max_events	= armv7pmu.num_events;
+		/* Initialize & Reset PMNC: C bit and P bit */
+		armv7_pmnc_write(ARMV7_PMNC_P | ARMV7_PMNC_C);
         } else {
                 pr_info("no hardware support available\n");
                 perf_max_events = -1;

You'll need to switch on the cpuid to select the correct event mappings.

I've implemented this for oprofile, I'll post it as an RFC after Christmas as I won't be able to respond in the meantime.

Cheers,

Will

#21 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Hi,

Quoted 3 lines · On Monday 21 December 2009 12:04:55 Will Deacon wrote:

Hi Jean,

I've provided some comments inline. Hopefully they're useful.

Thanks for reviewing the code.

Quoted 38 lines · Jean Pihet
Quoted 10 lines

Hello,

Here is a patch that adds the support for ARMv7 processors, using the PMNC HW unit.

The code is for review, it has been compiled and boot tested only, the complete testing is in progress. Please let me know if the patch is wrapped or garbled I will send it attached (20KB in size).

Feedback is welcome.

<snip>

Quoted 24 lines
arch/arm/kernel/perf_event.c +1 -0
index abb5267..79e92ce 100644
@@ -4,6 +4,7 @@
  * ARM performance counter support.
  *
  * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
+ * ARMv7 support: Jean Pihet <[email protected]>
  *
  * This code is based on the sparc64 perf event code, which is in turn

based
* on the x86 code. Callchain code is based on the ARM OProfile
backtrace

arch/arm/kernel/perf_event.c +0 -0
@@ -35,8 +36,11 @@ DEFINE_SPINLOCK(pmu_lock);
  * ARMv6 supports a maximum of 3 events, starting from index 1. If we

add
* another platform that supports more, we need to increase this to be
the
* largest of all platforms.
+ *
+ * ARMv7 supports up to 5 events:
+ * cycle counter CCNT + 4 events counters CNT0..3
*/

Quoted 8 lines

arch/arm/kernel/perf_event.c

Quoted 2 lines
arch/arm/kernel/perf_event.c +1 -1
-#define ARMPMU_MAX_HWEVENTS		4
+#define ARMPMU_MAX_HWEVENTS		5

The maximum number of event counters on ARMv7 is currently 6 [Cortex-A9], plus a cycle counter. Additionally, the number of event counters actually available is implementation defined (the cycle counter is mandatory). You can find out the number of event counters using the PMCR ((PMCR >> 11) & 0x1f).

I think we should support Cortex-A8 for now and add support for Cortex-A9 on top of it. IIUC a generic ARMV7 support is not possible so I will need separate handling for Cortex-A8 and -A9. Is that correct?

Unfortunately I do not have any -A9 HW for now. I will look at the spec in order to spot the differences between both PMNC units.

Quoted 36 lines
Quoted 36 lines

/* The events for a given CPU. */
struct cpu_hw_events {

arch/arm/kernel/perf_event.c +7 -0
@@ -965,6 +969,701 @@ static struct arm_pmu armv6pmu = {
 	.max_period		= (1LLU << 32) - 1,
 };
 
+/*
+ * ARMv7 Performance counter handling code.
+ *
+ * Copied from ARMv6 code, with the low level code inspired
+ *  by the ARMv7 Oprofile code.
+ *
+ * ARMv7 has 4 configurable performance counters and a single cycle

counter.
+ * All counters can be enabled/disabled and IRQ masked separately. The cycle
+ * counter and all 4 performance counters together can be reset separately.
+ */

arch/arm/kernel/perf_event.c +17 -0
+
+enum armv7_perf_types {
+	ARMV7_PERFCTR_PMNC_SW_INCR		= 0x00,
+	ARMV7_PERFCTR_IFETCH_MISS		= 0x01,
+	ARMV7_PERFCTR_ITLB_MISS			= 0x02,
+	ARMV7_PERFCTR_DCACHE_REFILL		= 0x03,
+	ARMV7_PERFCTR_DCACHE_ACCESS		= 0x04,
+	ARMV7_PERFCTR_DTLB_REFILL		= 0x05,
+	ARMV7_PERFCTR_DREAD			= 0x06,
+	ARMV7_PERFCTR_DWRITE			= 0x07,
+	ARMV7_PERFCTR_INSTR_EXECUTED		= 0x08,
+	ARMV7_PERFCTR_EXC_TAKEN			= 0x09,
+	ARMV7_PERFCTR_EXC_EXECUTED		= 0x0A,
+	ARMV7_PERFCTR_CID_WRITE			= 0x0B,
+	ARMV7_PERFCTR_PC_WRITE			= 0x0C,
+	ARMV7_PERFCTR_PC_IMM_BRANCH		= 0x0D,
+	ARMV7_PERFCTR_PC_PROC_RETURN		= 0x0E,
Quoted 8 lines

arch/arm/kernel/perf_event.c

Quoted 4 lines
arch/arm/kernel/perf_event.c +4 -0
+	ARMV7_PERFCTR_UNALIGNED_ACCESS		= 0x0F,
+	ARMV7_PERFCTR_PC_BRANCH_MIS_PRED	= 0x10,
+
+	ARMV7_PERFCTR_PC_BRANCH_MIS_USED	= 0x12,

Ok - the events so far are defined by the v7 architecture. Note that this doesn't necessarily mean they are all supported by the core.

Is there a way to detect the supported PMU events at run-time? Is it harmful to use unsupported events?

Quoted 33 lines
Quoted 33 lines
arch/arm/kernel/perf_event.c +33 -0
+	ARMV7_PERFCTR_WRITE_BUFFER_FULL		= 0x40,
+	ARMV7_PERFCTR_L2_STORE_MERGED		= 0x41,
+	ARMV7_PERFCTR_L2_STORE_BUFF		= 0x42,
+	ARMV7_PERFCTR_L2_ACCESS			= 0x43,
+	ARMV7_PERFCTR_L2_CACH_MISS		= 0x44,
+	ARMV7_PERFCTR_AXI_READ_CYCLES		= 0x45,
+	ARMV7_PERFCTR_AXI_WRITE_CYCLES		= 0x46,
+	ARMV7_PERFCTR_MEMORY_REPLAY		= 0x47,
+	ARMV7_PERFCTR_UNALIGNED_ACCESS_REPLAY	= 0x48,
+	ARMV7_PERFCTR_L1_DATA_MISS		= 0x49,
+	ARMV7_PERFCTR_L1_INST_MISS		= 0x4A,
+	ARMV7_PERFCTR_L1_DATA_COLORING		= 0x4B,
+	ARMV7_PERFCTR_L1_NEON_DATA		= 0x4C,
+	ARMV7_PERFCTR_L1_NEON_CACH_DATA		= 0x4D,
+	ARMV7_PERFCTR_L2_NEON			= 0x4E,
+	ARMV7_PERFCTR_L2_NEON_HIT		= 0x4F,
+	ARMV7_PERFCTR_L1_INST			= 0x50,
+	ARMV7_PERFCTR_PC_RETURN_MIS_PRED	= 0x51,
+	ARMV7_PERFCTR_PC_BRANCH_FAILED		= 0x52,
+	ARMV7_PERFCTR_PC_BRANCH_TAKEN		= 0x53,
+	ARMV7_PERFCTR_PC_BRANCH_EXECUTED	= 0x54,
+	ARMV7_PERFCTR_OP_EXECUTED		= 0x55,
+	ARMV7_PERFCTR_CYCLES_INST_STALL		= 0x56,
+	ARMV7_PERFCTR_CYCLES_INST		= 0x57,
+	ARMV7_PERFCTR_CYCLES_NEON_DATA_STALL	= 0x58,
+	ARMV7_PERFCTR_CYCLES_NEON_INST_STALL	= 0x59,
+	ARMV7_PERFCTR_NEON_CYCLES		= 0x5A,
+
+	ARMV7_PERFCTR_PMU0_EVENTS		= 0x70,
+	ARMV7_PERFCTR_PMU1_EVENTS		= 0x71,
+	ARMV7_PERFCTR_PMU_EVENTS		= 0x72,
+
+	ARMV7_PERFCTR_CPU_CYCLES		= 0xFF
Quoted 8 lines
Quoted 1 line

+};

These events are specific to the Cortex-A8. Unfortunately, these numbers clash with events specific to the Cortex-A9 [and potentially future v7 cores]. For example, 0x40 on the A8 is WRITE_BUFFER_FULL but on the A9 it is JAVA_BYTECODE_EXEC. This means that you'll need to take a similar approach as was taken for ARM11MP vs ARM11*.

Ok so I will need to separate Cortex-A8 from -A9.

Quoted 17 lines

<snip>

arch/arm/kernel/perf_event.c

Quoted 12 lines
arch/arm/kernel/perf_event.c +12 -0
+/*
+ * Available counters
+ */
+#define ARMV7_CCNT 		0
+#define ARMV7_CNT0 		1
+#define ARMV7_CNT1 		2
+#define ARMV7_CNT2 		3
+#define ARMV7_CNT3 		4
+#define ARMV7_CNTMAX 		5
+#define ARMV7_COUNTER_TO_CCNT	(ARMV7_CYCLE_COUNTER - ARMV7_CCNT)
+
+#define ARMV7_CPU_COUNTER(cpu, counter)	((cpu) * CNTMAX + (counter))

You don't use this macro. I imagine there are others which are no longer used too.

Ok I am checking and cleaning the code.

Quoted 15 lines

<snip>

arch/arm/kernel/perf_event.c

Quoted 11 lines
arch/arm/kernel/perf_event.c +11 -0
+static inline int armv7_pmnc_select_counter(unsigned int cnt)
+{
+	u32 val;
+
+	cnt -= ARMV7_COUNTER_TO_CCNT;
+
+	if ((cnt == ARMV7_CCNT) || (cnt >= ARMV7_CNTMAX)) {
+		printk(KERN_ERR "oprofile: CPU%u selecting wrong PMNC counter"
+			" %d\n", smp_processor_id(), cnt);
+		return -1;
+	}

Nice error message :)

Indeed! Theis is corrected already.

Quoted 22 lines

<snip>

Quoted 7 lines

static int __init
init_hw_perf_events(void)
{

arch/arm/kernel/perf_event.c +0 -0
@@ -977,6 +1676,13 @@ init_hw_perf_events(void)
                 memcpy(armpmu_perf_cache_map, armv6_perf_cache_map,
                        sizeof(armv6_perf_cache_map));
                 perf_max_events	= armv6pmu.num_events;

arch/arm/kernel/perf_event.c init_hw_perf_events(void) · line 1688

Quoted 10 lines
arch/arm/kernel/perf_event.c +7 -0
+	} else if (cpu_architecture() == CPU_ARCH_ARMv7) {
+		armpmu = &armv7pmu;
+		memcpy(armpmu_perf_cache_map, armv7_perf_cache_map,
+			sizeof(armv7_perf_cache_map));
+		perf_max_events	= armv7pmu.num_events;
+		/* Initialize & Reset PMNC: C bit and P bit */
+		armv7_pmnc_write(ARMV7_PMNC_P | ARMV7_PMNC_C);
         } else {
                 pr_info("no hardware support available\n");
                 perf_max_events = -1;

You'll need to switch on the cpuid to select the correct event mappings.

Quoted 2 lines

I've implemented this for oprofile, I'll post it as an RFC after Christmas as I won't be able to respond in the meantime.

Ok. Do you know how I can differentiate Cortex-A8 from -A9?

I will post a new version with the corrections.

Quoted 4 lines

Cheers,

Will

Cheers and a good celebration time,
Jean

#22 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Hello,

Quoted 11 lines · Jean Pihet
Quoted 5 lines

The maximum number of event counters on ARMv7 is currently 6 [Cortex-A9], plus a cycle counter. Additionally, the number of event counters actually available is implementation defined (the cycle counter is mandatory). You can find out the number of event counters using the PMCR ((PMCR >> 11) & 0x1f).

I think we should support Cortex-A8 for now and add support for Cortex-A9 on top of it. IIUC a generic ARMV7 support is not possible so I will need separate handling for Cortex-A8 and -A9. Is that correct?

Unfortunately I do not have any -A9 HW for now. I will look at the spec in order to spot the differences between both PMNC units.

Sorry, I should've mentioned that the PMU hardware interface is the same across all v7 cores. The only difference is the core-specific event numberings. A9 is also available in MP configurations, but that shouldn't cause many problems for perf. I can test on an A9MP once you have something you're happy with.

Quoted 5 lines
Quoted 3 lines

Ok - the events so far are defined by the v7 architecture. Note that this doesn't necessarily mean they are all supported by the core.

Is there a way to detect the supported PMU events at run-time? Is it harmful to use unsupported events?

The unsupported events for a given core are documented in the TRM. For example, A9 doesn't support 0x08 and 0x0E but does support all the other events defined by the architecture. It then has its own set of extensions listed in the TRM [Section 9.2.1].

Quoted 1 line

Ok so I will need to separate Cortex-A8 from -A9.

Yep - but it's an easy thing to do.

Quoted 3 lines
Quoted 2 lines

I've implemented this for oprofile, I'll post it as an RFC after Christmas as I won't be able to respond in the meantime.

Ok. Do you know how I can differentiate Cortex-A8 from -A9?

The bottom two bytes of the main cpuid (read_cpuid_id()) are 0xC080 and 0xC090 for the A8 and A9 respectively.

Quoted 1 line

I will post a new version with the corrections.

Excellent. I can comment on things tomorrow as well, but then I'm off.

Quoted 1 line

Cheers and a good celebration time,

Thanks. I'll post my oprofile patches when I return. They cover these issues already so hopefully you can spot anything I've missed.

Will

#23 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Quoted 8 lines · Will Deacon
Quoted 1 line

I will post a new version with the corrections.

Excellent. I can comment on things tomorrow as well, but then I'm off.

Quoted 1 line

Cheers and a good celebration time,

Thanks. I'll post my oprofile patches when I return. They cover these issues already so hopefully you can spot anything I've missed.

It's my last working day today. When I return I'll post an updated patch series for the generic arm perfcounters and v6 support. Hopefully we can get these signed off then and base the v7 support off of these.

Enjoy the break,

Jamie

#24 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Quoted 5 lines · On Monday 21 December 2009 13:43:18 Jamie Iles wrote:
Quoted 4 lines · Will Deacon
Quoted 1 line

I will post a new version with the corrections.

Excellent. I can comment on things tomorrow as well, but then I'm off.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Cheers and a good celebration time,

Thanks. I'll post my oprofile patches when I return. They cover these issues already so hopefully you can spot anything I've missed.

It's my last working day today. When I return I'll post an updated patch series for the generic arm perfcounters and v6 support. Hopefully we can get these signed off then and base the v7 support off of these.

OK, good! I am continuing on v7 and I will rebase my patches as soon as those are out.

Quoted 1 line

Enjoy the break,

Thx, the same for you!

Quoted 2 lines

Jamie

Jean

#25 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Hi,

Here is the updated patch. It now supports:
- Cortex-A8 and Cortex-A9 processors,
- The low level code has been completely redesigned to allow the dynamic detection of the number of available counters, based on the PMCR value,
- runtime detection of the CPU arch (v6 or v7) and model (Cortex-A8 or Cortex-A9)

The code is for review, it has been checked, compiled and boot tested on OMAP3 (Cortex-A8). Unfortunately I am still facing some cross compilation problems of the tools/perf utility.

Some remarks and questions:

1) The number of available counters can reach 32 on ARMv7, so the macro ARMPMU_MAX_HWEVENTS is now defined as 32. Is that correct?

2) Please note that the Cortex-A9 events do not easily map to the predefined events. Cf. armv7_a9_perf_map and armv7_a9_perf_cache_map in the code.
- the PERF_COUNT_HW_INSTRUCTIONS event is not found. It looks like the number of instructions is calculated by adding events numbers (events from 0x70 till 0x74: MAIN_UNIT_EXECUTED_INST, SECOND_UNIT_EXECUTED_INST, LD_ST_UNIT_EXECUTED_INST, FP_EXECUTED_INST and NEON_EXECUTED_INST),
- the HW_BRANCH events are not found
- the global cache events 0x50 and 0x51 define the COHERENT_LINE_HIT and COHERENT_LINE_MISS events, is that correct?
- L1 and L2 cache events are not found. Those could be available in separate PL310 registers, TBC
- no TLB events excepted the ITLB_MISS event are found.

Any thoughts?

Regards,
Jean

---

Quoted 1 line

From 26fbfd99815abbe1147a191ff759b1197344587f Mon Sep 17 00:00:00 2001

From: Jean Pihet <[email protected]>
Date: Fri, 18 Dec 2009 17:46:21 +0100
Subject: [PATCH] arm/perfevents: add support for ARMv7

Adds the Performance Events support for ARMv7 processor, using the PMNC unit in HW.

Supports the following:
- Cortex-A8 and Cortex-A9 processors,
- dynamic detection of the number of available counters, based on the PMCR value,
- runtime detection of the CPU arch (v6 or v7) and model (Cortex-A8 or Cortex-A9)

---
 arch/arm/Kconfig             |    2 +-
 arch/arm/kernel/perf_event.c |  919 
+++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 903 insertions(+), 18 deletions(-)
arch/arm/Kconfig +1 -1
index e5bd97e..5c6afdb 100644
@@ -1172,7 +1172,7 @@ config HIGHPTE
 
 config HW_PERF_EVENTS
 	bool "Enable hardware performance counter support for perf events"
-	depends on PERF_EVENTS && CPU_HAS_PMU && CPU_V6
+	depends on PERF_EVENTS && CPU_HAS_PMU && (CPU_V6 || CPU_V7)
 	default y
 	help
 	  Enable hardware performance counter support for perf events. If
arch/arm/kernel/perf_event.c +902 -17

949 lines not on this page · raw message

Signature · 1 line
1.6.2.5.168.g3823
Quoted 30 lines · On Monday 21 December 2009 14:35:04 Jean Pihet wrote:
Quoted 5 lines · On Monday 21 December 2009 13:43:18 Jamie Iles wrote:
Quoted 4 lines · Will Deacon
Quoted 1 line

I will post a new version with the corrections.

Excellent. I can comment on things tomorrow as well, but then I'm off.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Cheers and a good celebration time,

Thanks. I'll post my oprofile patches when I return. They cover these issues already so hopefully you can spot anything I've missed.

It's my last working day today. When I return I'll post an updated patch series for the generic arm perfcounters and v6 support. Hopefully we can get these signed off then and base the v7 support off of these.

OK, good! I am continuing on v7 and I will rebase my patches as soon as those are out.

Quoted 1 line

Enjoy the break,

Thx, the same for you!

Quoted 1 line

Jamie

Jean

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#26 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Quoted 3 lines · Jean Pihet

The code is for review, it has been checked, compiled and boot tested on OMAP3 (Cortex-A8). Unfortunately I am still facing some cross compilation problems of the tools/perf utility.

Have you managed to solve these cross-compilation problems? If yes, it would be nice to merge the fixes into upstream perf.

Ingo

#27 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Ingo, Jamie,

Quoted 7 lines · On Monday 28 December 2009 08:57:48 Ingo Molnar wrote:
Quoted 3 lines · Jean Pihet

The code is for review, it has been checked, compiled and boot tested on OMAP3 (Cortex-A8). Unfortunately I am still facing some cross compilation problems of the tools/perf utility.

Have you managed to solve these cross-compilation problems? If yes, it would be nice to merge the fixes into upstream perf.

Yes I got them resolved but I needed to make a few changes:
- -Werror removed from the CFLAGS definition in tools/perf/Makefile. Without the change the compilation stops after a warning about include paths.
- the rmb() macro in tools/perf/perf.h prevents the compilation. I changed it to the definition from arch/arm/include/asm/system.h (asm volatile("":::"memory")). Where is the original definition from? Why is it specific to perf_events?

What do you think about those changes? Should they be submitted separately?

I tested the ARMv7 code on the Cortex-A8 processor. The code looks fine but when I load the CPU I am running into spinlock recursion problems (in perf_ctx_adjust_freq).
Does those problem happen on ARMv6 as well?
Here is the backtrace:

BUG: spinlock recursion on CPU#0, cp/1049
 lock: c7ab4600, .magic: dead4ead, .owner: cp/1049, .owner_cpu: 0
[<c00415e0>] (unwind_backtrace+0x0/0xdc) from [<c0200c7c>] 
(do_raw_spin_lock+0x48/0x14c)
[<c0200c7c>] (do_raw_spin_lock+0x48/0x14c) from [<c00b7ca4>] 
(perf_ctx_adjust_freq+0xc/0x1b4)
[<c00b7ca4>] (perf_ctx_adjust_freq+0xc/0x1b4) from [<c00b7e84>] 
(perf_event_task_tick+0x38/0x9c)
[<c00b7e84>] (perf_event_task_tick+0x38/0x9c) from [<c0078d9c>] 
(update_process_times+0x3c/0x48)
[<c0078d9c>] (update_process_times+0x3c/0x48) from [<c00907fc>] 
(tick_sched_timer+0x80/0xbc)
[<c00907fc>] (tick_sched_timer+0x80/0xbc) from [<c0087f3c>] 
(__run_hrtimer+0xc8/0x158)
[<c0087f3c>] (__run_hrtimer+0xc8/0x158) from [<c008823c>] 
(hrtimer_interrupt+0x130/0x310)
[<c008823c>] (hrtimer_interrupt+0x130/0x310) from [<c0048244>] 
(omap2_gp_timer_interrupt+0x20/0x2c)
[<c0048244>] (omap2_gp_timer_interrupt+0x20/0x2c) from [<c009dc14>] 
(handle_IRQ_event+0x70/0x184)
[<c009dc14>] (handle_IRQ_event+0x70/0x184) from [<c009f7a8>] 
(handle_level_irq+0xa4/0x118)
[<c009f7a8>] (handle_level_irq+0xa4/0x118) from [<c003b070>] 
(asm_do_IRQ+0x70/0x90)
[<c003b070>] (asm_do_IRQ+0x70/0x90) from [<c042d2f0>] (__irq_svc+0x30/0x80)
Exception stack(0xc7a89890 to 0xc7a898d8)

9880: 00000000 c05bc1e0 0000001e 00000010
98a0: 00000004 c05bc1e0 c7a7e8a8 c05bc160 c0606550 c7bccdc0 c05db260 c7a8997c 98c0: 00000000 c7a898d8 c0041f44 c01f6034 40000013 ffffffff [<c042d2f0>] (__irq_svc+0x30/0x80) from [<c01f6034>] (_test_and_set_bit_le+0x20/0x34)

BUG: spinlock lockup on CPU#0, cp/1049, c7ab4600
[<c00415e0>] (unwind_backtrace+0x0/0xdc) from [<c0200d44>] 
(do_raw_spin_lock+0x110/0x14c)
[<c0200d44>] (do_raw_spin_lock+0x110/0x14c) from [<c00b7ca4>] 
(perf_ctx_adjust_freq+0xc/0x1b4)
[<c00b7ca4>] (perf_ctx_adjust_freq+0xc/0x1b4) from [<c00b7e84>] 
(perf_event_task_tick+0x38/0x9c)
[<c00b7e84>] (perf_event_task_tick+0x38/0x9c) from [<c0078d9c>] 
(update_process_times+0x3c/0x48)
[<c0078d9c>] (update_process_times+0x3c/0x48) from [<c00907fc>] 
(tick_sched_timer+0x80/0xbc)
[<c00907fc>] (tick_sched_timer+0x80/0xbc) from [<c0087f3c>] 
(__run_hrtimer+0xc8/0x158)
[<c0087f3c>] (__run_hrtimer+0xc8/0x158) from [<c008823c>] 
(hrtimer_interrupt+0x130/0x310)
[<c008823c>] (hrtimer_interrupt+0x130/0x310) from [<c0048244>] 
(omap2_gp_timer_interrupt+0x20/0x2c)
[<c0048244>] (omap2_gp_timer_interrupt+0x20/0x2c) from [<c009dc14>] 
(handle_IRQ_event+0x70/0x184)
[<c009dc14>] (handle_IRQ_event+0x70/0x184) from [<c009f7a8>] 
(handle_level_irq+0xa4/0x118)
[<c009f7a8>] (handle_level_irq+0xa4/0x118) from [<c003b070>] 
(asm_do_IRQ+0x70/0x90)
[<c003b070>] (asm_do_IRQ+0x70/0x90) from [<c042d2f0>] (__irq_svc+0x30/0x80)
Exception stack(0xc7a89890 to 0xc7a898d8)

9880: 00000000 c05bc1e0 0000001e 00000010
98a0: 00000004 c05bc1e0 c7a7e8a8 c05bc160 c0606550 c7bccdc0 c05db260 c7a8997c 98c0: 00000000 c7a898d8 c0041f44 c01f6034 40000013 ffffffff

Quoted 2 lines

Ingo

Regards,
Jean

#28 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Hi Jean,

Quoted 8 lines · Jean Pihet

Ingo, Jamie,

Quoted 5 lines · On Monday 28 December 2009 08:57:48 Ingo Molnar wrote:
Quoted 3 lines · Jean Pihet

The code is for review, it has been checked, compiled and boot tested on OMAP3 (Cortex-A8). Unfortunately I am still facing some cross compilation problems of the tools/perf utility.

Quoted 5 lines
Quoted 2 lines

Have you managed to solve these cross-compilation problems? If yes, it would be nice to merge the fixes into upstream perf.

Yes I got them resolved but I needed to make a few changes:
- -Werror removed from the CFLAGS definition in tools/perf/Makefile. Without the change the compilation stops after a warning about include paths.

I've found that for the embedded platform I'm working on I need to build with NO_LIBPERL=1 otherwise the local include paths are used for perl. Obviously perl scripting will not be available.

Quoted 4 lines

- the rmb() macro in tools/perf/perf.h prevents the compilation. I changed it to the definition from arch/arm/include/asm/system.h (asm volatile("":::"memory")). Where is the original definition from? Why is it specific to perf_events?

I took this definition of rmb() from arch/arm/kernel/entry-armv.S. This gives a CPU independent way of a real memory barrier. This compiles with the latest codesourcery toolchain and 2009q1.

[snip]

Quoted 4 lines

I tested the ARMv7 code on the Cortex-A8 processor. The code looks fine but when I load the CPU I am running into spinlock recursion problems (in perf_ctx_adjust_freq).
Does those problem happen on ARMv6 as well?

I haven't seen this on ARMv6 but perhaps I've been getting lucky...

Jamie

#29 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Quoted 13 lines
Quoted 7 lines
Quoted 3 lines · Jean Pihet
Quoted 2 lines

I am still facing some cross compilation problems of the tools/perf utility.

Quoted 2 lines · On Monday 28 December 2009 08:57:48 Ingo Molnar wrote:

Have you managed to solve these cross-compilation problems? If yes, it would be nice to merge the fixes into upstream perf.

Quoted 4 lines · Jean Pihet

Yes I got them resolved but I needed to make a few changes:
- -Werror removed from the CFLAGS definition in tools/perf/Makefile. Without the change the compilation stops after a warning about include paths.

Quoted 4 lines · Jamie Iles

I've found that for the embedded platform I'm working on I need to build with NO_LIBPERL=1 otherwise the local include paths are used for perl. Obviously perl scripting will not be available.

Quoted 8 lines
Quoted 4 lines

- the rmb() macro in tools/perf/perf.h prevents the compilation. I changed it to the definition from arch/arm/include/asm/system.h (asm volatile("":::"memory")). Where is the original definition from? Why is it specific to perf_events?

I took this definition of rmb() from arch/arm/kernel/entry-armv.S. This give a CPU independent way of a real memory barrier. This compiles with the latest codesourcery toolchain and 2009q1.

Since I've just started to investigate the performance counter architecture and will probably need to port tools/perf on ARM as well, would you care to post any patches of your work in this area? It will be greatly appreciated. I couldn't find any of such patches posted anywhere or in the tree.

Also, I would like to ask what's the idea behind tools/perf utility anyway. As far as I understand it provides only a few generalized events and all other events are accessed only via critic r### where ### is hardware dependent number. If that's the case the tools might be hard to use since users don't tend to remember such numbers. This means that perf would require some kind of front-end which would provide a nice textual description for those mysterious r###.

Signature · 4 lines
Best regards,                                           _     _
  .o. | Liege of Serenely Enlightened Majesty of       o' \,=./ `o
  ..o | Computer Science,  Micha? "mina86" Nazarewicz     (o o)
  ooo +---<[email protected]>---<[email protected]>---ooO--(_)--Ooo--
#30 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

On Wed, Jan 06, 2010 at 04:16:06PM +0100, Micha? Nazarewicz wrote:
[snip]

Quoted 5 lines

Since I've just started to investigate the performance counter architecture and will probably need to port tools/perf on ARM as well, would you care to post any patches of your work in this area? It will be greatly appreciated. I couldn't find any of such patches posted anywhere or in the tree.

The patches are in tip/master. 58e9f94138c1d9c47f6a63632ca7a78fc6dcc15f and cc835752ae3634acd2d487fdf5152f6075f45aef should do the trick. They should also be in Linus' tree.

Quoted 7 lines

Also, I would like to ask what's the idea behind tools/perf utility anyway. As far as I understand it provides only a few generalized events and all other events are accessed only via critic r### where ### is hardware dependent number. If that's the case the tools might be hard to use since users don't tend to remember such numbers. This means that perf would require some kind of front-end which would provide a nice textual description for those mysterious r###.

True. However, for simple profiling, cycle counts are often enough plus all of the software events are always available. I do wonder if it would be worth having a mechanism for implementations to register a list of supported events with the perf subsystem that could be exported through debugfs. The perf tools could then read this to produce the list of events and would show all events that were available.

Jamie

#31 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Quoted 3 lines · Micha? Nazarewicz
Quoted 2 lines

Since I [...] will probably need to port tools/perf on ARM as well, would you care to post any patches of your work in this area?

Quoted 1 line · Jamie Iles

The patches are in tip/master. [...] They should also be in Linus' tree.

Ah, thank you. Now I don't know why I couldn't find it previously...

BTW. Did anyone got problems with stack protector? My ld complained it could not find ssp_nonshared when checking for libelf. I think the "-c" should be dropped from the -fstack-protector-all support checking in Makefile (diff included at the end of mail).

Quoted 6 lines
Quoted 5 lines

what's the idea behind tools/perf utility. As far as I understand it provides a few generalized events and all other events are accessed via critic r### [...] tool might be hard to use since users don't tend to remember such numbers. [...] perf would require front-end which would provide textual description for those mysterious r###.

Quoted 6 lines

True. However, for simple profiling, cycle counts are often enough plus all of the software events are always available. I do wonder if it would be worth having a mechanism for implementations to register a list of supported events with the perf subsystem that could be exported through debugfs. The perf tools could then read this to produce the list of events and would show all events that were available.

I would also consider using sysfs (/sys/devices/system/cpu/perf-events?) since one may lack debugfs in their kernel. It can be argued that if someone wants to use performance counters we might just require debugfs however personally I'd hate such requirement and prefer this information to be placed in sysfs.

Lets not argue about details though. More important question is whether such information is needed at all and I believe it is.

I agree that in many (if not most) cases the generalized events are enough but imagine this poor fellow who needs to look through 1k-page CPU reference manual to find "raw" event number because he wants to do more in-depth profiling.

In my opinion, if the utility is to be widely used such database will be created at one point or another and it can be either in kernel or user space (some front-ends for pref cold emerge or maybe there'll be some other utility that uses the same API and has its own database). Furthermore, I believe it's better done in kernel because it knows better what CPU system is running on and what it supports.

So if you ask me I say that such list is needed, is needed to be done in kernel and besides raw numbers should provide some textual names for events so for example the same name can be used on two different ARM CPUs which use different event number for the very same event.

And promised diff:
--

tools/perf/Makefile +1 -1
index 652a470..434c5ec 100644
@@ -250,7 +250,7 @@ PTHREAD_LIBS = -lpthread
  # explicitly what architecture to check for. Fix this up for yours..
  SPARSE_FLAGS = -D__BIG_ENDIAN__ -D__powerpc__
 
-ifeq ($(shell sh -c "echo 'int foo(void) {char X[2]; return 3;}' | $(CC) -x c -c -Werror -fstack-protector-all - -o /dev/null "$(QUIET_STDERR)" && echo y"), y)
+ifeq ($(shell sh -c "echo 'int foo(void) {char X[2]; return 3;}' | $(CC) -x c -Werror -fstack-protector-all - -o /dev/null "$(QUIET_STDERR)" && echo y"), y)
    CFLAGS := $(CFLAGS) -fstack-protector-all
  endif
 
Signature · 4 lines
Best regards,                                           _     _
  .o. | Liege of Serenely Enlightened Majesty of       o' \,=./ `o
  ..o | Computer Science,  Micha? "mina86" Nazarewicz     (o o)
  ooo +---<[email protected]>---<[email protected]>---ooO--(_)--Ooo--
#32 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

In reply to Jean Pihet

Hi,

Here is the updated patch after testing on HW.
I will rebase it on Jamie's latest patch set as soon as they are out.

Feedback is welcome!

The comments and remarks are the same as in the previous version:
It now supports:
- Cortex-A8 and Cortex-A9 processors,
- The low level code has been completely redesigned to allow the dynamic detection of the number of available counters, based on the PMCR value,
- runtime detection of the CPU arch (v6 or v7) and model (Cortex-A8 or Cortex-A9)

The code is for review, it has been checked, compiled and tested on OMAP3 (Cortex-A8).

Some remarks and questions:

1) The number of available counters can reach 32 on ARMv7, so the macro ? ARMPMU_MAX_HWEVENTS is now defined as 32. Is that correct?

2) Please note that the Cortex-A9 events do not easily map to the predefined events. Cf. armv7_a9_perf_map and armv7_a9_perf_cache_map in the code.
- the PERF_COUNT_HW_INSTRUCTIONS event is not found. It looks like the number of instructions is calculated by adding events numbers (events from 0x70 till 0x74: MAIN_UNIT_EXECUTED_INST, SECOND_UNIT_EXECUTED_INST, LD_ST_UNIT_EXECUTED_INST, FP_EXECUTED_INST and NEON_EXECUTED_INST),
- the HW_BRANCH events are not found
- the global cache events 0x50 and 0x51 define the COHERENT_LINE_HIT and COHERENT_LINE_MISS events, is that correct?
- L1 and L2 cache events are not found. Those could be available in separate PL310 registers, TBC
- no TLB events excepted the ITLB_MISS event are found.

Any thoughts?

Regards,
Jean

---

Quoted 1 line

From 9d559085417187870be2823e8a10fba6a513aae9 Mon Sep 17 00:00:00 2001

From: Jean Pihet <[email protected]>
Date: Fri, 18 Dec 2009 17:46:21 +0100
Subject: [PATCH] arm/perfevents: add support for ARMv7

Adds the Performance Events support for ARMv7 processor, using the PMNC unit in HW.

Supports the following:
- Cortex-A8 and Cortex-A9 processors,
- dynamic detection of the number of available counters, based on the PMCR value,
- runtime detection of the CPU arch (v6 or v7) and model (Cortex-A8 or Cortex-A9)

Tested on OMAP3530 (Cortex-A8) only.

---
 arch/arm/Kconfig             |    2 +-
 arch/arm/kernel/perf_event.c |  923 
+++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 907 insertions(+), 18 deletions(-)
arch/arm/Kconfig +1 -1
index e5bd97e..5c6afdb 100644
@@ -1172,7 +1172,7 @@ config HIGHPTE
 
 config HW_PERF_EVENTS
 	bool "Enable hardware performance counter support for perf events"
-	depends on PERF_EVENTS && CPU_HAS_PMU && CPU_V6
+	depends on PERF_EVENTS && CPU_HAS_PMU && (CPU_V6 || CPU_V7)
 	default y
 	help
 	  Enable hardware performance counter support for perf events. If
arch/arm/kernel/perf_event.c +906 -17

953 lines not on this page · raw message

Signature · 1 line
1.6.2.5.168.g3823
Quoted 36 lines · On Tuesday 22 December 2009 17:51:39 Jean Pihet wrote:

Hi,

Here is the updated patch. It now supports:
- Cortex-A8 and Cortex-A9 processors,
- The low level code has been completely redesigned to allow the dynamic detection of the number of available counters, based on the PMCR value,
- runtime detection of the CPU arch (v6 or v7) and model (Cortex-A8 or Cortex-A9)

The code is for review, it has been checked, compiled and boot tested on OMAP3 (Cortex-A8). Unfortunately I am still facing some cross compilation problems of the tools/perf utility.

Some remarks and questions:

1) The number of available counters can reach 32 on ARMv7, so the macro ARMPMU_MAX_HWEVENTS is now defined as 32. Is that correct?

2) Please note that the Cortex-A9 events do not easily map to the predefined events. Cf. armv7_a9_perf_map and armv7_a9_perf_cache_map in the code. - the PERF_COUNT_HW_INSTRUCTIONS event is not found. It looks like the number of instructions is calculated by adding events numbers (events from 0x70 till 0x74: MAIN_UNIT_EXECUTED_INST, SECOND_UNIT_EXECUTED_INST, LD_ST_UNIT_EXECUTED_INST, FP_EXECUTED_INST and NEON_EXECUTED_INST),
- the HW_BRANCH events are not found
- the global cache events 0x50 and 0x51 define the COHERENT_LINE_HIT and COHERENT_LINE_MISS events, is that correct?
- L1 and L2 cache events are not found. Those could be available in separate PL310 registers, TBC
- no TLB events excepted the ITLB_MISS event are found.

Any thoughts?

Regards,
Jean

#33 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Hi Jean,

Thanks for this. Feedback inline.

Quoted 9 lines · Jean Pihet

Here is the updated patch after testing on HW.
I will rebase it on Jamie's latest patch set as soon as they are out.

Feedback is welcome!

Some remarks and questions:

1) The number of available counters can reach 32 on ARMv7, so the macro ARMPMU_MAX_HWEVENTS is now defined as 32. Is that correct?

I think index 0 is reserved for the events array, so this should probably be 33. Hopefully that doesn't break any bitmasks.

Quoted 6 lines

2) Please note that the Cortex-A9 events do not easily map to the predefined events. Cf. armv7_a9_perf_map and armv7_a9_perf_cache_map in the code.
- the PERF_COUNT_HW_INSTRUCTIONS event is not found. It looks like the number of instructions is calculated by adding events numbers (events from 0x70 till 0x74: MAIN_UNIT_EXECUTED_INST, SECOND_UNIT_EXECUTED_INST, LD_ST_UNIT_EXECUTED_INST, FP_EXECUTED_INST and NEON_EXECUTED_INST),

Event 0x68 - `Instructions coming out of the core renaming stage' can be used as an approximation for PERF_COUNT_HW_INSTRUCTIONS.

Quoted 1 line

- the HW_BRANCH events are not found

0x0c is HW_BRANCH_INSTRUCTIONS and 0x10 is HW_BRANCH_MISSES.
0x12 is the number of predictable branch instructions executed, so the mispredict rate is 0x10/0x12. These events are defined for v7, so A8 should take these definitions too.

Quoted 2 lines

- the global cache events 0x50 and 0x51 define the COHERENT_LINE_HIT and COHERENT_LINE_MISS events, is that correct?

0x50 is COHERENT_LINE_MISS and 0x51 is COHERENT_LINE_HIT. These are only available on A9 with SMP.

Quoted 2 lines

- L1 and L2 cache events are not found. Those could be available in separate PL310 registers, TBC

We could use 0x01 for icache miss, 0x03 for dcache miss and 0x04 for dcache access.

Quoted 1 line

- no TLB events excepted the ITLB_MISS event are found.

I think 0x05 is DTLB_MISS.

<snip>

Quoted 33 lines
arch/arm/kernel/perf_event.c +33 -0
+/*
+ * ARMv7 Cortex-A8 and Cortex-A9 Performance Events handling code.
+ *
+ * Copied from ARMv6 code, with the low level code inspired
+ *  by the ARMv7 Oprofile code.
+ *
+ * Cortex-A8 has up to 4 configurable performance counters and
+ *  a single cycle counter.
+ * Cortex-A9 has up to 31 configurable performance counters and
+ *  a single cycle counter.
+ *
+ * All counters can be enabled/disabled and IRQ masked separately. The cycle
+ *  counter and all 4 performance counters together can be reset separately.
+ */
+
+#define ARMV7_PMU_CORTEX_A8_NAME		"ARMv7 Cortex-A8"
+
+#define ARMV7_PMU_CORTEX_A9_NAME		"ARMv7 Cortex-A9"
+
+/* Common ARMv7 event types */
+enum armv7_perf_types {
+	ARMV7_PERFCTR_PMNC_SW_INCR		= 0x00,
+	ARMV7_PERFCTR_IFETCH_MISS		= 0x01,
+	ARMV7_PERFCTR_ITLB_MISS			= 0x02,
+	ARMV7_PERFCTR_DCACHE_REFILL		= 0x03,
+	ARMV7_PERFCTR_DCACHE_ACCESS		= 0x04,
+	ARMV7_PERFCTR_DTLB_REFILL		= 0x05,
+	ARMV7_PERFCTR_DREAD			= 0x06,
+	ARMV7_PERFCTR_DWRITE			= 0x07,
+
+	ARMV7_PERFCTR_EXC_TAKEN			= 0x09,
+	ARMV7_PERFCTR_EXC_EXECUTED		= 0x0A,
+	ARMV7_PERFCTR_CID_WRITE			= 0x0B,

arch/arm/kernel/perf_event.c static struct arm_pmu armv6pmu = { · line 1015

Quoted 10 lines
arch/arm/kernel/perf_event.c +10 -0
+	ARMV7_PERFCTR_PC_WRITE			= 0x0C,
+	ARMV7_PERFCTR_PC_IMM_BRANCH		= 0x0D,
+	ARMV7_PERFCTR_UNALIGNED_ACCESS		= 0x0F,
+	ARMV7_PERFCTR_PC_BRANCH_MIS_PRED	= 0x10,
+	ARMV7_PERFCTR_CLOCK_CYCLES		= 0x11,
+
+	ARMV7_PERFCTR_PC_BRANCH_MIS_USED	= 0x12,
+
+	ARMV7_PERFCTR_CPU_CYCLES		= 0xFF
+};

For consistency, I think it's better to stick to either MISS or REFILL. Events 0x01 and 0x03 are the same, but for instructions and data respectively.

<snip>

Quoted 13 lines
arch/arm/kernel/perf_event.c +13 -0
+static const unsigned armv7_a8_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
+					  [PERF_COUNT_HW_CACHE_OP_MAX]
+					  [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
+	[C(L1D)] = {
+		/*
+		 * The performance counters don't differentiate between read
+		 * and write accesses/misses so this isn't strictly correct,
+		 * but it's the best we can do. Writes and reads get
+		 * combined.
+		 */
+		[C(OP_READ)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_DCACHE_ACCESS,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_DATA_MISS,

arch/arm/kernel/perf_event.c static struct arm_pmu armv6pmu = { · line 1146

Quoted 10 lines
arch/arm/kernel/perf_event.c +10 -0
+		},
+		[C(OP_WRITE)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_DCACHE_ACCESS,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_DATA_MISS,
+		},
+		[C(OP_PREFETCH)] = {
+			[C(RESULT_ACCESS)]	= CACHE_OP_UNSUPPORTED,
+			[C(RESULT_MISS)]	= CACHE_OP_UNSUPPORTED,
+		},
+	},

You're using the A8-only event 0x49 [ARMV7_PERFCTR_L1_DATA_MISS]. This also counts hash misses in the address translation during the cache lookup procedure, even if the resulting access is a [slightly slower] hit. I think you're better off using event 0x03. In fact, I'd try to use architecturally defined events whenever you can because that allows for comparisons between v7 cores.

Quoted 4 lines
arch/arm/kernel/perf_event.c +4 -0
+	[C(L1I)] = {
+		[C(OP_READ)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_L1_INST,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_INST_MISS,

arch/arm/kernel/perf_event.c static struct arm_pmu armv6pmu = { · line 1160

Quoted 10 lines
arch/arm/kernel/perf_event.c +10 -0
+		},
+		[C(OP_WRITE)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_L1_INST,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_INST_MISS,
+		},
+		[C(OP_PREFETCH)] = {
+			[C(RESULT_ACCESS)]	= CACHE_OP_UNSUPPORTED,
+			[C(RESULT_MISS)]	= CACHE_OP_UNSUPPORTED,
+		},
+	},

Same thing here. I'd suggest using 0x01 instead of 0x4a.

<snip>

arch/arm/kernel/perf_event.c static struct arm_pmu armv6pmu = { · line 1374

Quoted 8 lines
arch/arm/kernel/perf_event.c +8 -0
+/*
+ * Available counters
+ */
+#define ARMV7_CNT0 		0	/* First event counter */
+#define ARMV7_CCNT 		31	/* Cycle counter */
+
+#define ARMV7_A8_CNTMAX		5	/* Cortex-A8: up to 4 counters + CCNT */
+#define ARMV7_A9_CNTMAX		32	/* Cortex-A9: up to 31 counters + CCNT*/

Actually, A9 has a maximum number of 6 event counters + CCNT.

<snip>

arch/arm/kernel/perf_event.c static struct arm_pmu armv6pmu = { · line 1815

Quoted 8 lines
arch/arm/kernel/perf_event.c +8 -0
+#define CPUID_V6_MASK   	0x7F000
+#define CPUID_V6_BITS   	0x7B000
+
+#define CPUID_CORTEX_A8_BITS	0xC080
+#define CPUID_CORTEX_A8_MASK	0xFFF0
+
+#define CPUID_CORTEX_A9_BITS	0xC090
+#define CPUID_CORTEX_A9_MASK	0xFFF0

Just define CPUID_V7_MASK.

Quoted 28 lines
arch/arm/kernel/perf_event.c +28 -0
+	unsigned long cpuid = read_cpuid_id();
+
+	/*
+	 * ARMv6 detection
+	 */
+	if (CPUID_V6_BITS == (cpuid & CPUID_V6_MASK)) {
+		armpmu = &armv6pmu;
+		memcpy(armpmu_perf_cache_map, armv6_perf_cache_map,
+			sizeof(armv6_perf_cache_map));
+		perf_max_events	= armv6pmu.num_events;
+	}
+	/*
+	 * ARMv7 detection
+	 */
+	else if (cpu_architecture() == CPU_ARCH_ARMv7) {
+		/*
+		 * Cortex-A8 detection
+		 */
+		if ((cpuid & CPUID_CORTEX_A8_MASK) == CPUID_CORTEX_A8_BITS) {
+			armv7pmu.name = ARMV7_PMU_CORTEX_A8_NAME;
+			memcpy(armpmu_perf_cache_map, armv7_a8_perf_cache_map,
+				sizeof(armv7_a8_perf_cache_map));
+			armv7pmu.event_map = armv7_a8_pmu_event_map;
+			armpmu = &armv7pmu;
+		} else
+		/*
+		 * Cortex-A9 detection
+		 */

arch/arm/kernel/perf_event.c static struct arm_pmu armv6pmu = { · line 1854

Quoted 10 lines
arch/arm/kernel/perf_event.c +10 -0
+			if ((cpuid & CPUID_CORTEX_A9_MASK)
+			    == CPUID_CORTEX_A9_BITS) {
+				armv7pmu.name = ARMV7_PMU_CORTEX_A9_NAME;
+				memcpy(armpmu_perf_cache_map,
+					armv7_a9_perf_cache_map,
+					sizeof(armv7_a9_perf_cache_map));
+				armv7pmu.event_map = armv7_a9_pmu_event_map;
+				armpmu = &armv7pmu;
+		} else
+			perf_max_events = -1;

The A9 code indentation is a level too deep I think. It might also be worth adding a cpu_architecture() check to the v6 test just in case a v7 core conflicts with the mask.

I hope that helps. Please let me know what you think about the event numberings.

Cheers,

Will

#34 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Hi Will,

Thanks for the answers. Here are the remarks.

Jamie, there is a remark at the very end about the cpuid detection. Can you check?

Regards,
Jean

Quoted 10 lines · On Monday 04 January 2010 17:52:01 Will Deacon wrote:

Hi Jean,

Thanks for this. Feedback inline.

Quoted 5 lines · Jean Pihet

Here is the updated patch after testing on HW.
I will rebase it on Jamie's latest patch set as soon as they are out.

Feedback is welcome!

Quoted 7 lines
Quoted 4 lines

Some remarks and questions:

1) The number of available counters can reach 32 on ARMv7, so the macro ARMPMU_MAX_HWEVENTS is now defined as 32. Is that correct?

I think index 0 is reserved for the events array, so this should probably be 33. Hopefully that doesn't break any bitmasks.

Ok, changed to 33.
Note that the code is generic enough to support up to 32 counters (CCNT + 31 event counters), even though the Cortex-A8 and Cortex-A9 respectively support up to 1+4 and 1+6 counters.
The actual number of counters (from the PMNC registers) is printed out at Perf Events init.

Quoted 10 lines
Quoted 7 lines

2) Please note that the Cortex-A9 events do not easily map to the predefined events. Cf. armv7_a9_perf_map and armv7_a9_perf_cache_map in the code. - the PERF_COUNT_HW_INSTRUCTIONS event is not found. It looks like the number of instructions is calculated by adding events numbers (events from 0x70 till 0x74: MAIN_UNIT_EXECUTED_INST, SECOND_UNIT_EXECUTED_INST,
LD_ST_UNIT_EXECUTED_INST, FP_EXECUTED_INST and NEON_EXECUTED_INST),

Event 0x68 - `Instructions coming out of the core renaming stage' can be used as an approximation for PERF_COUNT_HW_INSTRUCTIONS.

Ok changed.

Quoted 7 lines
Quoted 1 line

- the HW_BRANCH events are not found

0x0c is HW_BRANCH_INSTRUCTIONS and 0x10 is HW_BRANCH_MISSES. 0x12 is the number of predictable branch instructions executed, so the mispredict rate is 0x10/0x12. These events are defined for v7, so A8 should take these definitions too.
From the spec I read 0x0c is 'SW write of the PC', is that equivalent to

HW_BRANCH_INSTRUCTIONS?

For A8 I am using:
- ARMV7_PERFCTR_PC_BRANCH_TAKEN (0x53),
- ARMV7_PERFCTR_PC_BRANCH_FAILED (0x52)

For A9 it is unsupported for now.

Do you think I should use 0x0c and 0x10 for both A8 and A9? How to get the accesses and misses count directly?

Quoted 5 lines
Quoted 2 lines

- the global cache events 0x50 and 0x51 define the COHERENT_LINE_HIT and COHERENT_LINE_MISS events, is that correct?

0x50 is COHERENT_LINE_MISS and 0x51 is COHERENT_LINE_HIT. These are only available on A9 with SMP.

Ok the code uses 0x50 and 0x51 for A9.

Quoted 5 lines
Quoted 2 lines

- L1 and L2 cache events are not found. Those could be available in separate PL310 registers, TBC

We could use 0x01 for icache miss, 0x03 for dcache miss and 0x04 for dcache access.

Ok changed to the following. Is that correct?
Note that A8 uses specific events for I cache in order to make them comparable to each other. I cache miss could use 0x01 also. Cf. remark below for more.

Cortex-A8:
- D cache access: ARMV7_PERFCTR_DCACHE_ACCESS (0x04),
- D cache miss: ARMV7_PERFCTR_DCACHE_REFILL (0x03) instead of ARMV7_PERFCTR_L1_DATA_MISS (0x49),
- I cache access: ARMV7_PERFCTR_L1_DATA_MISS (0x50),
- I cache miss: ARMV7_PERFCTR_L1_INST_MISS (0x4a).

Cortex-A9:
- D cache access: ARMV7_PERFCTR_DCACHE_ACCESS (0x04),
- D cache miss: ARMV7_PERFCTR_DCACHE_REFILL (0x03),
- I cache access: Not supported,
- I cache miss: ARMV7_PERFCTR_IFETCH_MISS (0x01).

Quoted 3 lines
Quoted 1 line

- no TLB events excepted the ITLB_MISS event are found.

I think 0x05 is DTLB_MISS.

Ok using 0x05 for DTLB misses

Quoted 41 lines

<snip>

Quoted 39 lines

+/*
+ * ARMv7 Cortex-A8 and Cortex-A9 Performance Events handling code.
+ *
+ * Copied from ARMv6 code, with the low level code inspired
+ * by the ARMv7 Oprofile code.
+ *
+ * Cortex-A8 has up to 4 configurable performance counters and
+ * a single cycle counter.
+ * Cortex-A9 has up to 31 configurable performance counters and
+ * a single cycle counter.
+ *
+ * All counters can be enabled/disabled and IRQ masked separately. The cycle + * counter and all 4 performance counters together can be reset separately. + */

arch/arm/kernel/perf_event.c +25 -0
+
+#define ARMV7_PMU_CORTEX_A8_NAME		"ARMv7 Cortex-A8"
+
+#define ARMV7_PMU_CORTEX_A9_NAME		"ARMv7 Cortex-A9"
+
+/* Common ARMv7 event types */
+enum armv7_perf_types {
+	ARMV7_PERFCTR_PMNC_SW_INCR		= 0x00,
+	ARMV7_PERFCTR_IFETCH_MISS		= 0x01,
+	ARMV7_PERFCTR_ITLB_MISS			= 0x02,
+	ARMV7_PERFCTR_DCACHE_REFILL		= 0x03,
+	ARMV7_PERFCTR_DCACHE_ACCESS		= 0x04,
+	ARMV7_PERFCTR_DTLB_REFILL		= 0x05,
+	ARMV7_PERFCTR_DREAD			= 0x06,
+	ARMV7_PERFCTR_DWRITE			= 0x07,
+
+	ARMV7_PERFCTR_EXC_TAKEN			= 0x09,
+	ARMV7_PERFCTR_EXC_EXECUTED		= 0x0A,
+	ARMV7_PERFCTR_CID_WRITE			= 0x0B,
+	ARMV7_PERFCTR_PC_WRITE			= 0x0C,
+	ARMV7_PERFCTR_PC_IMM_BRANCH		= 0x0D,
+	ARMV7_PERFCTR_UNALIGNED_ACCESS		= 0x0F,
+	ARMV7_PERFCTR_PC_BRANCH_MIS_PRED	= 0x10,
+	ARMV7_PERFCTR_CLOCK_CYCLES		= 0x11,
+
Quoted 8 lines

arch/arm/kernel/perf_event.c static struct arm_pmu armv6pmu = { · line 1015

Quoted 4 lines
arch/arm/kernel/perf_event.c +4 -0
+	ARMV7_PERFCTR_PC_BRANCH_MIS_USED	= 0x12,
+
+	ARMV7_PERFCTR_CPU_CYCLES		= 0xFF
+};

For consistency, I think it's better to stick to either MISS or REFILL. Events 0x01 and 0x03 are the same, but for instructions and data respectively.

Good point! I am using the terminology from the TRM but I have to agree that some are cryptic.

Quoted 24 lines

<snip>

Quoted 22 lines
arch/arm/kernel/perf_event.c +22 -0
+static const unsigned armv7_a8_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
+					  [PERF_COUNT_HW_CACHE_OP_MAX]
+					  [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
+	[C(L1D)] = {
+		/*
+		 * The performance counters don't differentiate between read
+		 * and write accesses/misses so this isn't strictly correct,
+		 * but it's the best we can do. Writes and reads get
+		 * combined.
+		 */
+		[C(OP_READ)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_DCACHE_ACCESS,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_DATA_MISS,
+		},
+		[C(OP_WRITE)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_DCACHE_ACCESS,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_DATA_MISS,
+		},
+		[C(OP_PREFETCH)] = {
+			[C(RESULT_ACCESS)]	= CACHE_OP_UNSUPPORTED,
+			[C(RESULT_MISS)]	= CACHE_OP_UNSUPPORTED,
+		},
Quoted 8 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	},

You're using the A8-only event 0x49 [ARMV7_PERFCTR_L1_DATA_MISS]. This also counts hash misses in the address translation during the cache lookup procedure, even if the resulting access is a [slightly slower] hit. I think you're better off using event 0x03. In fact, I'd try to use architecturally defined events whenever you can because that allows for comparisons between v7 cores.

Ok changed that after remark above about L1 and L2.

Quoted 8 lines
Quoted 8 lines
arch/arm/kernel/perf_event.c +8 -0
+	[C(L1I)] = {
+		[C(OP_READ)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_L1_INST,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_INST_MISS,
+		},
+		[C(OP_WRITE)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_L1_INST,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_INST_MISS,
Quoted 8 lines

arch/arm/kernel/perf_event.c static struct arm_pmu armv6pmu = { · line 1146

Quoted 6 lines
arch/arm/kernel/perf_event.c +6 -0
+		},
+		[C(OP_PREFETCH)] = {
+			[C(RESULT_ACCESS)]	= CACHE_OP_UNSUPPORTED,
+			[C(RESULT_MISS)]	= CACHE_OP_UNSUPPORTED,
+		},
+	},

Same thing here. I'd suggest using 0x01 instead of 0x4a.

Ok is it preferred to keep the ARMV7_PERFCTR_L1_ events for both accesses and misses in order to make the events counts comparable to each other? On the other end using 0x01 allows the comparison between A8 and A9.
I am OK to change it, just let me know.

Quoted 12 lines

<snip>

arch/arm/kernel/perf_event.c static struct arm_pmu armv6pmu = { · line 1374

Quoted 8 lines
arch/arm/kernel/perf_event.c +8 -0
+/*
+ * Available counters
+ */
+#define ARMV7_CNT0 		0	/* First event counter */
+#define ARMV7_CCNT 		31	/* Cycle counter */
+
+#define ARMV7_A8_CNTMAX		5	/* Cortex-A8: up to 4 counters + CCNT */
+#define ARMV7_A9_CNTMAX		32	/* Cortex-A9: up to 31 counters + CCNT*/

Actually, A9 has a maximum number of 6 event counters + CCNT.

Cf. remark above. The code is generic enough and supports up to the 1+31 events as defined in the A8 and A9 TRMs. The number of counters is dynamically read from the PMNC registers. Should that be compared against the given maximum (1+4 for A8, 1+6 for A9)? That looks like overkill.

Quoted 12 lines

<snip>

arch/arm/kernel/perf_event.c static struct arm_pmu armv6pmu = { · line 1815

Quoted 8 lines
arch/arm/kernel/perf_event.c +8 -0
+#define CPUID_V6_MASK   	0x7F000
+#define CPUID_V6_BITS   	0x7B000
+
+#define CPUID_CORTEX_A8_BITS	0xC080
+#define CPUID_CORTEX_A8_MASK	0xFFF0
+
+#define CPUID_CORTEX_A9_BITS	0xC090
+#define CPUID_CORTEX_A9_MASK	0xFFF0

Just define CPUID_V7_MASK.

Ok changed.

Quoted 33 lines
Quoted 33 lines
arch/arm/kernel/perf_event.c +32 -0
+	unsigned long cpuid = read_cpuid_id();
+
+	/*
+	 * ARMv6 detection
+	 */
+	if (CPUID_V6_BITS == (cpuid & CPUID_V6_MASK)) {
+		armpmu = &armv6pmu;
+		memcpy(armpmu_perf_cache_map, armv6_perf_cache_map,
+			sizeof(armv6_perf_cache_map));
+		perf_max_events	= armv6pmu.num_events;
+	}
+	/*
+	 * ARMv7 detection
+	 */
+	else if (cpu_architecture() == CPU_ARCH_ARMv7) {
+		/*
+		 * Cortex-A8 detection
+		 */
+		if ((cpuid & CPUID_CORTEX_A8_MASK) == CPUID_CORTEX_A8_BITS) {
+			armv7pmu.name = ARMV7_PMU_CORTEX_A8_NAME;
+			memcpy(armpmu_perf_cache_map, armv7_a8_perf_cache_map,
+				sizeof(armv7_a8_perf_cache_map));
+			armv7pmu.event_map = armv7_a8_pmu_event_map;
+			armpmu = &armv7pmu;
+		} else
+		/*
+		 * Cortex-A9 detection
+		 */
+			if ((cpuid & CPUID_CORTEX_A9_MASK)
+			    == CPUID_CORTEX_A9_BITS) {
+				armv7pmu.name = ARMV7_PMU_CORTEX_A9_NAME;
+				memcpy(armpmu_perf_cache_map,
Quoted 8 lines

arch/arm/kernel/perf_event.c static struct arm_pmu armv6pmu = { · line 1854

Quoted 6 lines
arch/arm/kernel/perf_event.c +6 -0
+					armv7_a9_perf_cache_map,
+					sizeof(armv7_a9_perf_cache_map));
+				armv7pmu.event_map = armv7_a9_pmu_event_map;
+				armpmu = &armv7pmu;
+		} else
+			perf_max_events = -1;

The A9 code indentation is a level too deep I think.

Ok changed. It is beautiful now ;p

Quoted 3 lines

It might also be
worth adding a cpu_architecture() check to the v6 test just in case a v7 core conflicts with the mask.

Jamie, what do you think?

Quoted 3 lines

I hope that helps. Please let me know what you think about the event numberings.

Ok that definitely helps. Please let me know about my remarks.

Quoted 4 lines

Cheers,

Will

Cheers,
Jean

author
#35 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Quoted 6 lines · Jean Pihet

Hi Will,

Thanks for the answers. Here are the remarks.

Jamie, there is a remark at the very end about the cpuid detection. Can you check?

[snip]

Quoted 4 lines
Quoted 3 lines

It might also be
worth adding a cpu_architecture() check to the v6 test just in case a v7 core conflicts with the mask.

Jamie, what do you think?

Hmm, I'm not sure. I thought that cpu_architecture() returned the MMU architecture of the core. See the thread 'question about ARM11MP cpu architecture identification' for more details. Certainly on my ARM1176 Linux reports it as a v7 on bootup so this check would be bad...

Jamie

#36 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Quoted 14 lines · On Friday 15 January 2010 16:39:41 Jamie Iles wrote:
Quoted 6 lines · Jean Pihet

Hi Will,

Thanks for the answers. Here are the remarks.

Jamie, there is a remark at the very end about the cpuid detection. Can you check?

[snip]

Quoted 4 lines
Quoted 3 lines

It might also be
worth adding a cpu_architecture() check to the v6 test just in case a v7 core conflicts with the mask.

Quoted 6 lines
Quoted 1 line

Jamie, what do you think?

Hmm, I'm not sure. I thought that cpu_architecture() returned the MMU architecture of the core. See the thread 'question about ARM11MP cpu architecture identification' for more details. Certainly on my ARM1176 Linux reports it as a v7 on bootup so this check would be bad...

Correct! That is why I was asking. So the tests are working for v6 and v7 because of the code ordering (need to check v6 first, then v7 variants). Is it ok to keep the code as is?

Quoted 2 lines

Jamie

author
#37 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Quoted 17 lines · Jean Pihet
Quoted 16 lines · On Friday 15 January 2010 16:39:41 Jamie Iles wrote:
Quoted 6 lines · Jean Pihet

Hi Will,

Thanks for the answers. Here are the remarks.

Jamie, there is a remark at the very end about the cpuid detection. Can you check?

[snip]

Quoted 5 lines
Quoted 3 lines

It might also be
worth adding a cpu_architecture() check to the v6 test just in case a v7 core conflicts with the mask.

Jamie, what do you think?

Quoted 7 lines
Quoted 4 lines

Hmm, I'm not sure. I thought that cpu_architecture() returned the MMU architecture of the core. See the thread 'question about ARM11MP cpu architecture identification' for more details. Certainly on my ARM1176 Linux reports it as a v7 on bootup so this check would be bad...

Correct! That is why I was asking. So the tests are working for v6 and v7 because of the code ordering (need to check v6 first, then v7 variants). Is it ok to keep the code as is?

That's fine with me.

Jamie

#38 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

In reply to Jean Pihet

Hi Jean,

Sorry for the delay in getting back to you, I've had a few technical problems with my machine. Anyway, here we go:

* Jean Pihet wrote:
<snip>

Quoted 6 lines
Quoted 4 lines

0x0c is HW_BRANCH_INSTRUCTIONS and 0x10 is HW_BRANCH_MISSES. 0x12 is the number of predictable branch instructions executed, so the mispredict rate is 0x10/0x12. These events are defined for v7, so A8 should take these definitions too.

From the spec I read 0x0c is 'SW write of the PC', is that equivalent to HW_BRANCH_INSTRUCTIONS?

This event counts:
- All branch instructions
- Instructions that explicitly write the PC
- Exception generating instructions

I think this is suitable for HW_BRANCH_INSTRUCTIONS, but if anybody feels differently then maybe we should reconsider.

Quoted 8 lines

For A8 I am using:
- ARMV7_PERFCTR_PC_BRANCH_TAKEN (0x53),
- ARMV7_PERFCTR_PC_BRANCH_FAILED (0x52)

For A9 it is unsupported for now.

Do you think I should use 0x0c and 0x10 for both A8 and A9? How to get the accesses and misses count directly?

I think we should define the `standard' set (i.e. those that perf supports by name) using the v7 events, so in this case then use 0x0c and 0x10 for both A8 and A9. The core-specific definitions can then always be accessed as raw events. As I mentioned, I think this is important if people decide to compare the counts between two cores.

Quoted 13 lines
Quoted 2 lines

We could use 0x01 for icache miss, 0x03 for dcache miss and 0x04 for dcache access.

Ok changed to the following. Is that correct?
Note that A8 uses specific events for I cache in order to make them comparable to each other. I cache miss could use 0x01 also. Cf. remark below for more.

Cortex-A8:
- D cache access: ARMV7_PERFCTR_DCACHE_ACCESS (0x04),
- D cache miss: ARMV7_PERFCTR_DCACHE_REFILL (0x03) instead of ARMV7_PERFCTR_L1_DATA_MISS (0x49),
- I cache access: ARMV7_PERFCTR_L1_DATA_MISS (0x50),
- I cache miss: ARMV7_PERFCTR_L1_INST_MISS (0x4a).

Quoted 5 lines

Cortex-A9:
- D cache access: ARMV7_PERFCTR_DCACHE_ACCESS (0x04),
- D cache miss: ARMV7_PERFCTR_DCACHE_REFILL (0x03),
- I cache access: Not supported,
- I cache miss: ARMV7_PERFCTR_IFETCH_MISS (0x01).

Hmm, this is an interesting one. I suppose comparison between events on a given core (i.e. A8) is preferable, so I agree with you here. Due to the lack of I-cache access events on A9, there's nothing we can do to get a fair cross-core comparison. [minor note: You've called the I-cache access event ARMV7_PERFCTR_L1_DATA_MISS!]

Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
arch/arm/kernel/perf_event.c +8 -0
+	[C(L1I)] = {
+		[C(OP_READ)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_L1_INST,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_INST_MISS,
+		},
+		[C(OP_WRITE)] = {
+			[C(RESULT_ACCESS)]	= ARMV7_PERFCTR_L1_INST,
+			[C(RESULT_MISS)]	= ARMV7_PERFCTR_L1_INST_MISS,
Quoted 8 lines

arch/arm/kernel/perf_event.c static struct arm_pmu armv6pmu = { · line 1146

Quoted 6 lines
arch/arm/kernel/perf_event.c +6 -0
+		},
+		[C(OP_PREFETCH)] = {
+			[C(RESULT_ACCESS)]	= CACHE_OP_UNSUPPORTED,
+			[C(RESULT_MISS)]	= CACHE_OP_UNSUPPORTED,
+		},
+	},

Same thing here. I'd suggest using 0x01 instead of 0x4a.

Quoted 4 lines

Ok is it preferred to keep the ARMV7_PERFCTR_L1_ events for both accesses and misses in order to make the events counts comparable to each other? On the other end using 0x01 allows the comparison between A8 and A9.
I am OK to change it, just let me know.

After thinking about this above, I agree with you; let's use the ARMV7_PERFCTR_L1_ events to allow for event comparisons on the A8. Comparing with an A9 is a non-starter because the I-cache accesses can't be counted there.

Quoted 10 lines
Quoted 10 lines

arch/arm/kernel/perf_event.c static struct arm_pmu armv6pmu = { · line 1374

Quoted 8 lines
arch/arm/kernel/perf_event.c +8 -0
+/*
+ * Available counters
+ */
+#define ARMV7_CNT0 		0	/* First event counter */
+#define ARMV7_CCNT 		31	/* Cycle counter */
+
+#define ARMV7_A8_CNTMAX		5	/* Cortex-A8: up to 4 counters + CCNT */
+#define ARMV7_A9_CNTMAX		32	/* Cortex-A9: up to 31 counters + CCNT*/

Actually, A9 has a maximum number of 6 event counters + CCNT.

Quoted 4 lines

Cf. remark above. The code is generic enough and supports up to the 1+31 events as defined in the A8 and A9 TRMs. The number of counters is dynamically read from the PMNC registers. Should that be compared against the given maximum (1+4 for A8, 1+6 for A9)? That looks like overkill.

Sure, I was just referring to ARMV7_A9_CNTMAX being artificially high. You'll never see more than 6 event counters on an A9.

Quoted 4 lines
Quoted 3 lines

It might also be
worth adding a cpu_architecture() check to the v6 test just in case a v7 core conflicts with the mask.

Jamie, what do you think?

I forgot that looked at the MMU. Oh well, the ordering will have to matter.

Cheers,

Will

#39 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

In reply to Jean Pihet
Quoted 5 lines

From: linux-arm-kernel-bounces at lists.infradead.org [mailto:linux-arm-kernel- bounces at lists.infradead.org] On Behalf Of Jean Pihet
Sent: Tuesday, December 22, 2009 10:52 AM

Quoted 11 lines

2) Please note that the Cortex-A9 events do not easily map to the predefined events. Cf. armv7_a9_perf_map and armv7_a9_perf_cache_map in the code.
- the PERF_COUNT_HW_INSTRUCTIONS event is not found. It looks like the number of instructions is calculated by adding events numbers (events from 0x70 till 0x74: MAIN_UNIT_EXECUTED_INST, SECOND_UNIT_EXECUTED_INST, LD_ST_UNIT_EXECUTED_INST, FP_EXECUTED_INST and NEON_EXECUTED_INST),
- the HW_BRANCH events are not found
- the global cache events 0x50 and 0x51 define the COHERENT_LINE_HIT and COHERENT_LINE_MISS events, is that correct?
- L1 and L2 cache events are not found. Those could be available in separate PL310 registers, TBC

Recently I had done a side by side diff of A8 and A9 events for OMAP4.

It is notable that L2 cache events for CortexA8 come up through same PMNC register interface as ARMv7 core events. For CortexA9 + PL310 the L2 events all come up through a _different_ register interface. The interface is still simple but different.

- Several of the registers needed to enable PL310 event bus are Trustzone protected. This will lead to some messiness in getting at them through monitor mode proxies.

- A9 is missing a few events at core level and all l2 events which come up through pl310 regs.

- A9 has a few more event counter instances over A8. This grows some registers in expected way.

One bit I didn't get clear on was if any entity was trying to account for per-core stats at the shared PL310 level. Each core can give stats in familiar manner but association of both cores with common pl310 is not clear.

Regards,
Richard W.

#40 lore raw

Perf Event support for ARMv7 (was: Re: [PATCH 5/5] arm/perfevents: implement perf event support for ARMv6)

Richard,

Quoted 18 lines · Richard
Quoted 5 lines

From: linux-arm-kernel-bounces at lists.infradead.org [mailto:linux-arm-kernel- bounces at lists.infradead.org] On Behalf Of Jean Pihet
Sent: Tuesday, December 22, 2009 10:52 AM

Quoted 12 lines

2) Please note that the Cortex-A9 events do not easily map to the predefined events. Cf. armv7_a9_perf_map and armv7_a9_perf_cache_map in the code. - the PERF_COUNT_HW_INSTRUCTIONS event is not found. It looks like the number of instructions is calculated by adding events numbers (events from 0x70 till 0x74: MAIN_UNIT_EXECUTED_INST, SECOND_UNIT_EXECUTED_INST,
LD_ST_UNIT_EXECUTED_INST, FP_EXECUTED_INST and NEON_EXECUTED_INST),
- the HW_BRANCH events are not found
- the global cache events 0x50 and 0x51 define the COHERENT_LINE_HIT and COHERENT_LINE_MISS events, is that correct?
- L1 and L2 cache events are not found. Those could be available in separate PL310 registers, TBC

Quoted 6 lines

Recently I had done a side by side diff of A8 and A9 events for OMAP4.

It is notable that L2 cache events for CortexA8 come up through same PMNC register interface as ARMv7 core events. For CortexA9 + PL310 the L2 events all come up through a _different_ register interface. The interface is still simple but different.

Ok that could be done after the first ARMv7 support is merged in. Where can I find more info about the PL310 interface?

Quoted 3 lines

- Several of the registers needed to enable PL310 event bus are Trustzone protected. This will lead to some messiness in getting at them through monitor mode proxies.

Mmh security related code might be missing here. More investigation is needed.

Quoted 2 lines

- A9 is missing a few events at core level and all l2 events which come up through pl310 regs.

Ok.

Quoted 2 lines

- A9 has a few more event counter instances over A8. This grows some registers in expected way.

The latest code supports up to 1+31 events as defined in the A8 and A9 specs. The number of counters is dynamically read at Perf Events init.

Quoted 4 lines

One bit I didn't get clear on was if any entity was trying to account for per-core stats at the shared PL310 level. Each core can give stats in familiar manner but association of both cores with common pl310 is not clear.

Ok it wouls be nice to have all the details.

Quoted 2 lines

Regards,
Richard W.

Thanks & regards,
Jean

#41 lore raw

PATCH 1/5 arm: provide a mechanism to reserve performance counters

In reply to Jamie Iles

Hi Jamie,

It's getting there! Minor stylistic suggestions inline.

Quoted 30 lines · Jamie Iles

To add support for perf events and to allow the hardware counters to be shared with oprofile, we need a way to reserve access to the pmu (performance monitor unit).

arch/arm/include/asm/pmu.h new file +20 -0
new file mode 100644
index 0000000..e7cc264
@@ -0,0 +1,74 @@
+/*
+ *  linux/arch/arm/include/asm/pmu.h
+ *
+ *  Copyright (C) 2009 picoChip Designs Ltd, Jamie Iles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#ifndef __ARM_PMU_H__
+#define __ARM_PMU_H__
+
+#ifdef CONFIG_CPU_HAS_PMU
+
+struct pmu_irqs {
+	const int   *irqs;
+	unsigned    num_irqs;
+};

arch/arm/include/asm/pmu.h line 30

Quoted 10 lines
arch/arm/include/asm/pmu.h new file +10 -0
+
+/**
+ * reserve_pmu() - reserve the hardware performance counters
+ *
+ * Reserve the hardware performance counters in the system for exclusive use.
+ * The 'struct pmu_irqs' for the system is returned on success, ERR_PTR()
+ * encoded error on failure.
+ */
+extern const struct pmu_irqs *
+reserve_pmu(void);

I think it's standard Kernel coding style to put the declaration of a function all on one line if it fits. The same goes elsewhere in the patch.

arch/arm/mm/Kconfig config CPU_COPY_FA · line 542

Quoted 12 lines
arch/arm/mm/Kconfig +4 -0
index dd4698c..5cd0ec4 100644
@@ -536,6 +536,11 @@ config CPU_COPY_FA
 config CPU_COPY_V6
 	bool
 
+config CPU_HAS_PMU
+	depends on CPU_V6 || CPU_V7 || CPU_XSCALE
+	default y
+	bool

I think you should use XSCALE_PMU instead of CPU_XSCALE. Also, this should probably be in the top-level ARM Kconfig instead of the mm/ one.

Cheers,

Will

author
#42 lore raw

PATCH 1/5 arm: provide a mechanism to reserve performance counters

Hi Will,

Many thanks for the review again!

On Tue, Dec 15, 2009 at 02:13:25PM -0000, Will Deacon wrote:
[snip]

Quoted 4 lines
Quoted 4 lines

+/**
+ * reserve_pmu() - reserve the hardware performance counters
+ *
+ * Reserve the hardware performance counters in the system for exclusive use.

Quoted 8 lines

arch/arm/include/asm/pmu.h line 30

Quoted 5 lines

+ * The 'struct pmu_irqs' for the system is returned on success, ERR_PTR()
+ * encoded error on failure.
+ */

arch/arm/include/asm/pmu.h new file +2 -0
+extern const struct pmu_irqs *
+reserve_pmu(void);

I think it's standard Kernel coding style to put the declaration of a function all on one line if it fits. The same goes elsewhere in the patch.

I couldn't find anything that has this written down and there are plenty of other places in the perf code that do this. I personally like it because you can grep for "^foo" to find the definition. Unless people have strong objections to this I'll leave it as is.

Quoted 7 lines

arch/arm/mm/Kconfig config CPU_COPY_FA · line 542

Quoted 4 lines
arch/arm/mm/Kconfig +4 -0
+config CPU_HAS_PMU
+	depends on CPU_V6 || CPU_V7 || CPU_XSCALE
+	default y
+	bool

I think you should use XSCALE_PMU instead of CPU_XSCALE. Also, this should probably be in the top-level ARM Kconfig instead of the mm/ one.

Ok, I agree with you about using XSCALE_PMU, but why isn't mm/Kconfig the correct one? It's describing what features the CPU has and the PMU *is* a feature.

Cheers,

Jamie

#43 lore raw

PATCH 1/5 arm: provide a mechanism to reserve performance counters

Quoted 6 lines · Jamie Iles
Quoted 2 lines

I think it's standard Kernel coding style to put the declaration of a function all on one line if it fits. The same goes elsewhere in the patch.

I couldn't find anything that has this written down and there are plenty of other places in the perf code that do this. I personally like it because you can grep for "^foo" to find the definition. Unless people have strong objections to this I'll leave it as is.

I'm just going by the examples in Documentation/CodingStyle.

Quoted 10 lines
Quoted 7 lines

arch/arm/mm/Kconfig config CPU_COPY_FA · line 542

Quoted 4 lines
arch/arm/mm/Kconfig +4 -0
+config CPU_HAS_PMU
+	depends on CPU_V6 || CPU_V7 || CPU_XSCALE
+	default y
+	bool

I think you should use XSCALE_PMU instead of CPU_XSCALE. Also, this should probably be in the top-level ARM Kconfig instead of the mm/ one.

Ok, I agree with you about using XSCALE_PMU, but why isn't mm/Kconfig the correct one? It's describing what features the CPU has and the PMU *is* a feature.

I'd say move it out of mm/Kconfig because the PMU is not related to memory management and almost everything under mm/ is. oprofile may also want to use these bools and that is based in the top-level ARM Kconfig.

Will

#44 lore raw

PATCH 1/5 arm: provide a mechanism to reserve performance counters

In reply to Jamie Iles

Hi Jamie,

Just a small thing I noticed with the PMU reservation:

Quoted 5 lines · *Jamie Iles
arch/arm/kernel/pmu.c new file +0 -0
new file mode 100644
index 0000000..3a178bb

<snip>

Quoted 27 lines
arch/arm/kernel/pmu.c new file +27 -0
+static const int irqs[] = {
+#ifdef CONFIG_ARCH_PC3XX
+	IRQ_NPMUIRQ,
+#elif defined(CONFIG_ARCH_OMAP2)
+	3,
+#elif defined(CONFIG_ARCH_BCMRING)
+	IRQ_PMUIRQ,
+#elif defined(CONFIG_MACH_REALVIEW_EB)
+	IRQ_EB11MP_PMU_CPU0,
+	IRQ_EB11MP_PMU_CPU1,
+	IRQ_EB11MP_PMU_CPU2,
+	IRQ_EB11MP_PMU_CPU3,
+#elif defined(CONFIG_ARCH_OMAP3)
+	INT_34XX_BENCH_MPU_EMUL,
+#elif defined(CONFIG_ARCH_IOP32X)
+	IRQ_IOP32X_CORE_PMU,
+#elif defined(CONFIG_ARCH_IOP33X)
+	IRQ_IOP33X_CORE_PMU,
+#elif defined(CONFIG_ARCH_PXA)
+	IRQ_PMU,
+#endif
+};
+
+static const struct pmu_irqs pmu_irqs = {
+	.irqs	    = irqs,
+	.num_irqs   = ARRAY_SIZE(irqs),
+};

arch/arm/kernel/pmu.c line 66

Quoted 10 lines
arch/arm/kernel/pmu.c new file +10 -0
+
+static DECLARE_MUTEX(pmu_mutex);
+
+const struct pmu_irqs *
+reserve_pmu(void)
+{
+	int ret = down_trylock(&pmu_mutex) ? -EBUSY : 0;
+
+	return ret ? ERR_PTR(ret) : &pmu_irqs;
+}

I think it would be sensible to return an error (-ENODEV) if pmu_irqs.num_irqs == 0. Not doing so can cause applications to fail silently when they are running on unsupported boards.

Will

author
#45 lore raw

PATCH 1/5 arm: provide a mechanism to reserve performance counters

Quoted 11 lines · Will Deacon

arch/arm/kernel/pmu.c line 66

Quoted 7 lines
arch/arm/kernel/pmu.c new file +7 -0
+const struct pmu_irqs *
+reserve_pmu(void)
+{
+	int ret = down_trylock(&pmu_mutex) ? -EBUSY : 0;
+
+	return ret ? ERR_PTR(ret) : &pmu_irqs;
+}

I think it would be sensible to return an error (-ENODEV) if pmu_irqs.num_irqs == 0. Not doing so can cause applications to fail silently when they are running on unsupported boards.

I did think about that, but when the interrupts were in oprofile, it didn't regard this as an error so I kept this the same.

In the perf events code, we check that num_irqs is >= 1. You _could_ use the pmu as free running counters that you just periodically sample and wouldn't need an interrupt so I thought it best to leave this error checking to the user.

Cheers,

Jamie