Received: from mail.onstor.com (66.201.51.107) by exch1.onstor.net
 (10.0.0.225) with Microsoft SMTP Server id 8.1.311.2; Mon, 23 Feb 2009
 08:27:55 -0800
Received: from ausesmta2-3.messageone.com ([64.20.241.45]) by mail.onstor.com
 with Microsoft SMTPSVC(6.0.3790.3959);	 Mon, 23 Feb 2009 08:27:48 -0800
Received: from ftp.linux-mips.org (ftp.linux-mips.org [213.58.128.207])	by
 ausesmta2-3.messageone.com (8.13.8/8.13.8) with ESMTP id n1NGRmug009767	for
 <andy.sharp@onstor.com>; Mon, 23 Feb 2009 10:27:48 -0600
Received: from localhost.localdomain ([127.0.0.1]:64660 "EHLO
	ftp.linux-mips.org") by ftp.linux-mips.org with ESMTP	id S20808053AbZBWQ1D
 (ORCPT <rfc822;andy.sharp@onstor.com>);	Mon, 23 Feb 2009 16:27:03 +0000
Received: with ECARTIS (v1.0.0; list linux-mips); Mon, 23 Feb 2009 16:26:46
 +0000 (GMT)
Received: from mail.windriver.com ([147.11.1.11]:29180 "EHLO mail.wrs.com")	by
 ftp.linux-mips.org with ESMTP id S20808054AbZBWQ0o (ORCPT
	<rfc822;linux-mips@linux-mips.org>); Mon, 23 Feb 2009 16:26:44 +0000
Received: from ALA-MAIL03.corp.ad.wrs.com (ala-mail03 [147.11.57.144])	by
 mail.wrs.com (8.13.6/8.13.6) with ESMTP id n1NGQZAj006483;	Mon, 23 Feb 2009
 08:26:35 -0800 (PST)
Received: from ala-mail06.corp.ad.wrs.com ([147.11.57.147]) by
 ALA-MAIL03.corp.ad.wrs.com with Microsoft SMTPSVC(6.0.3790.1830);	 Mon, 23
 Feb 2009 08:26:35 -0800
Received: from localhost.localdomain ([128.224.146.23]) by
 ala-mail06.corp.ad.wrs.com with Microsoft SMTPSVC(6.0.3790.1830);	 Mon, 23
 Feb 2009 08:26:35 -0800
From: Mark Asselstine <mark.asselstine@windriver.com>
To: "linux-mips@linux-mips.org" <linux-mips@linux-mips.org>,
	"oprofile-list@lists.sf.net" <oprofile-list@lists.sf.net>
Sender: "linux-mips-bounce@linux-mips.org" <linux-mips-bounce@linux-mips.org>
Date: Mon, 23 Feb 2009 08:26:34 -0800
Subject: [PATCH] oprofile: VR5500 performance counter driver
Thread-Topic: [PATCH] oprofile: VR5500 performance counter driver
Thread-Index: AcmV066+rAw+voleSsKk6TuwqWpD7Q==
Message-ID: <1235406394-2650-1-git-send-email-mark.asselstine@windriver.com>
Accept-Language: en-US
Content-Language: en-US
X-MS-Exchange-Organization-AuthAs: Internal
X-MS-Exchange-Organization-AuthMechanism: 0b
X-MS-Exchange-Organization-AuthSource: exch1.onstor.net
X-MS-Has-Attach:
X-Auto-Response-Suppress: All
X-MS-TNEF-Correlator:
x-originalarrivaltime: 23 Feb 2009 16:26:35.0159 (UTC)
 FILETIME=[7E9C7270:01C995D3]
errors-to: linux-mips-bounce@linux-mips.org
x-ems-proccessed: 2K3Xl1OQTInXD6xxuA8z3Q==
x-ems-stamp: nswKNDehL7KvL6aYK5J1qA==
x-messageone-virus-version: vendor=fsecure
 engine=4.65.7400:2.4.4,1.2.40,4.0.164
 definitions=2009-02-22_03:2009-02-10,2009-02-22,2009-02-22 signatures=0
x-messageone-virus-scanned: Clean
x-messageone-envelope-sender: linux-mips-bounce@linux-mips.org
x-messageone-spam-details: rule=m773emszm_notspam policy=m773emszm score=0
 spamscore=0 ipscore=0 phishscore=0 bulkscore=0 adultscore=0 classifier=spam
 adjust=0 reason=mlx engine=3.1.0-0810130000 definitions=main-0902220252
x-messageone-spam-score: 0
x-messageone-spam-bar:
x-list: linux-mips
x-archive-position: 21957
x-ecartis-version: Ecartis v1.0.0
x-original-sender: mark.asselstine@windriver.com
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

This is inspired by op_model_mipsxx.c with some modification
in regards to register layout and overflow handling. This has
been tested on a NEC VR5500 board and shown to produce sane
results.

Signed-off-by: Mark Asselstine <mark.asselstine@windriver.com>

diff --git a/arch/mips/oprofile/Makefile b/arch/mips/oprofile/Makefile
index cfd4b60..977a828 100644
--- a/arch/mips/oprofile/Makefile
+++ b/arch/mips/oprofile/Makefile
@@ -14,4 +14,5 @@ oprofile-$(CONFIG_CPU_MIPS32)		+=3D op_model_mipsxx.o
 oprofile-$(CONFIG_CPU_MIPS64)		+=3D op_model_mipsxx.o
 oprofile-$(CONFIG_CPU_R10000)		+=3D op_model_mipsxx.o
 oprofile-$(CONFIG_CPU_SB1)		+=3D op_model_mipsxx.o
+oprofile-$(CONFIG_CPU_VR5500)		+=3D op_model_vr5500.o
 oprofile-$(CONFIG_CPU_RM9000)		+=3D op_model_rm9000.o
diff --git a/arch/mips/oprofile/common.c b/arch/mips/oprofile/common.c
index e1bffab..68aad99 100644
--- a/arch/mips/oprofile/common.c
+++ b/arch/mips/oprofile/common.c
@@ -17,6 +17,7 @@
=20
 extern struct op_mips_model op_model_mipsxx_ops __attribute__((weak));
 extern struct op_mips_model op_model_rm9000_ops __attribute__((weak));
+extern struct op_mips_model op_model_vr5500_ops __attribute__((weak));
=20
 static struct op_mips_model *model;
=20
@@ -94,6 +95,10 @@ int __init oprofile_arch_init(struct oprofile_operations=
 *ops)
 	case CPU_RM9000:
 		lmodel =3D &op_model_rm9000_ops;
 		break;
+
+	case CPU_R5500:
+		lmodel =3D &op_model_vr5500_ops;
+		break;
 	};
=20
=20
diff --git a/arch/mips/oprofile/op_model_vr5500.c b/arch/mips/oprofile/op_m=
odel_vr5500.c
new file mode 100644
index 0000000..75fae6a
--- /dev/null
+++ b/arch/mips/oprofile/op_model_vr5500.c
@@ -0,0 +1,179 @@
+/*
+ * This file is subject to the terms and conditions of the GNU General Pub=
lic
+ * License.  See the file "COPYING" in the main directory of this archive
+ * for more details.
+ *
+ * Copyright (c) 2009 Wind River Systems, Inc.
+ */
+#include <linux/cpumask.h>
+#include <linux/oprofile.h>
+#include <linux/interrupt.h>
+#include <linux/smp.h>
+#include <asm/irq_regs.h>
+
+#include "op_impl.h"
+
+#define M_PERFCTL_EXL			(1UL      <<  0)
+#define M_PERFCTL_KERNEL		(1UL      <<  1)
+#define M_PERFCTL_SUPERVISOR		(1UL      <<  2)
+#define M_PERFCTL_USER			(1UL      <<  3)
+#define M_PERFCTL_INTERRUPT_ENABLE	(1UL      <<  4)
+#define M_PERFCTL_INTERRUPT		(1UL      <<  5)
+#define M_PERFCTL_EVENT(event)		(((event) & 0xf)  << 6)
+#define M_PERFCTL_COUNT_ENABLE		(1UL      <<  10)
+
+#define NUM_COUNTERS                    2
+
+static int (*save_perf_irq) (void);
+
+#define __define_perf_accessors(r, n)    				\
+									\
+	static inline unsigned int r_c0_ ## r ## n(void)		\
+	{								\
+		return read_c0_ ## r ## n();				\
+	}								\
+									\
+	static inline void w_c0_ ## r ## n(unsigned int value)		\
+	{								\
+		write_c0_ ## r ## n(value);				\
+	}								\
+
+__define_perf_accessors(perfcntr, 0)
+__define_perf_accessors(perfcntr, 1)
+
+__define_perf_accessors(perfctrl, 0)
+__define_perf_accessors(perfctrl, 1)
+
+struct op_mips_model op_model_vr5500_ops;
+
+static struct vr5500_register_config {
+	unsigned int control[NUM_COUNTERS];
+	unsigned int counter[NUM_COUNTERS];
+} reg;
+
+/* Compute all of the registers in preparation for enabling profiling.  */
+static void vr5500_reg_setup(struct op_counter_config *ctr)
+{
+	int i;
+	unsigned int counters =3D NUM_COUNTERS;
+
+	/* Compute the performance counter control word.  */
+	for (i =3D 0; i < counters; i++) {
+		reg.control[i] =3D 0;
+		reg.counter[i] =3D 0;
+
+		if (!ctr[i].enabled)
+			continue;
+
+		reg.control[i] =3D M_PERFCTL_EVENT(ctr[i].event) |
+		    M_PERFCTL_INTERRUPT_ENABLE | M_PERFCTL_COUNT_ENABLE;
+		if (ctr[i].kernel)
+			reg.control[i] |=3D M_PERFCTL_KERNEL;
+		if (ctr[i].user)
+			reg.control[i] |=3D M_PERFCTL_USER;
+		if (ctr[i].exl)
+			reg.control[i] |=3D M_PERFCTL_EXL;
+
+		reg.counter[i] =3D 0xffffffff - ctr[i].count + 1;
+	}
+}
+
+/* Program all of the registers in preparation for enabling profiling.  */
+static void vr5500_cpu_setup(void *args)
+{
+	w_c0_perfctrl1(0);
+	w_c0_perfcntr1(reg.counter[1]);
+
+	w_c0_perfctrl0(0);
+	w_c0_perfcntr0(reg.counter[0]);
+}
+
+/* Start all counters on current CPU */
+static void vr5500_cpu_start(void *args)
+{
+	w_c0_perfctrl1(reg.control[1]);
+	w_c0_perfctrl0(reg.control[0]);
+}
+
+/* Stop all counters on current CPU */
+static void vr5500_cpu_stop(void *args)
+{
+	w_c0_perfctrl1(0);
+	w_c0_perfctrl0(0);
+}
+
+static int vr5500_perfcount_handler(void)
+{
+	unsigned int control;
+	unsigned int counter;
+	int handled =3D IRQ_NONE;
+	unsigned int counters =3D NUM_COUNTERS;
+
+	if (cpu_has_mips_r2 && !(read_c0_cause() & (1 << 26)))
+		return handled;
+
+	switch (counters) {
+	#define HANDLE_COUNTER(n) 					\
+	case n + 1:							\
+		control =3D r_c0_perfctrl ## n();				\
+		counter =3D r_c0_perfcntr ## n();				\
+		if ((control & M_PERFCTL_INTERRUPT_ENABLE) &&		\
+			(control & M_PERFCTL_INTERRUPT)) {		\
+			oprofile_add_sample(get_irq_regs(), n);		\
+			w_c0_perfcntr ## n(reg.counter[n]);		\
+			w_c0_perfctrl ## n(control & ~M_PERFCTL_INTERRUPT); \
+			handled =3D IRQ_HANDLED;				\
+		}
+	HANDLE_COUNTER(1)
+	HANDLE_COUNTER(0)
+	}
+
+	return handled;
+}
+
+static void reset_counters(void *arg)
+{
+	w_c0_perfctrl1(0);
+	w_c0_perfcntr1(0);
+
+	w_c0_perfctrl0(0);
+	w_c0_perfcntr0(0);
+}
+
+static int __init vr5500_init(void)
+{
+	on_each_cpu(reset_counters, NULL, 1);
+
+	switch (current_cpu_type()) {
+	case CPU_R5500:
+		op_model_vr5500_ops.cpu_type =3D "mips/vr5500";
+		break;
+
+	default:
+		printk(KERN_ERR "Profiling unsupported for this CPU\n");
+
+		return -ENODEV;
+	}
+
+	save_perf_irq =3D perf_irq;
+	perf_irq =3D vr5500_perfcount_handler;
+
+	return 0;
+}
+
+static void vr5500_exit(void)
+{
+	on_each_cpu(reset_counters, NULL, 1);
+
+	perf_irq =3D save_perf_irq;
+}
+
+struct op_mips_model op_model_vr5500_ops =3D {
+	.reg_setup =3D vr5500_reg_setup,
+	.cpu_setup =3D vr5500_cpu_setup,
+	.init =3D vr5500_init,
+	.exit =3D vr5500_exit,
+	.cpu_start =3D vr5500_cpu_start,
+	.cpu_stop =3D vr5500_cpu_stop,
+	.num_counters =3D NUM_COUNTERS,
+};

