OpenSBI Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Yu Chien Peter Lin <peterlin@andestech.com>
To: opensbi@lists.infradead.org
Subject: [PATCH v2 04/11] platform: andes: Add Andes custom PMU support
Date: Thu, 19 Oct 2023 19:37:06 +0800	[thread overview]
Message-ID: <20231019113713.3508153-5-peterlin@andestech.com> (raw)
In-Reply-To: <20231019113713.3508153-1-peterlin@andestech.com>

Before the ratification of Sscofpmf, the Andes PMU extension
was designed to support the sampling and filtering with hardware
performance counters, it works with the current SBI PMU extension
and Linux SBI PMU driver.

This patch implements the PMU device callbacks that update the
corresponding bits on custom CSRs.

Signed-off-by: Yu Chien Peter Lin <peterlin@andestech.com>
Reviewed-by: Leo Yu-Chi Liang <ycliang@andestech.com>
---
Changes v1 -> v2:
  - Fix mode filtering in andes_hw_counter_filter_mode()
  - Return early if pmu is not supported in andes_pmu_init() (suggested by Prabhakar)
  - Don't grant write permissions via CSR_MCOUNTERWEN as not needed
---
 platform/generic/andes/Kconfig             |  4 ++
 platform/generic/andes/andes_pmu.c         | 81 ++++++++++++++++++++++
 platform/generic/andes/objects.mk          |  1 +
 platform/generic/include/andes/andes_pmu.h |  8 +++
 4 files changed, 94 insertions(+)
 create mode 100644 platform/generic/andes/andes_pmu.c
 create mode 100644 platform/generic/include/andes/andes_pmu.h

diff --git a/platform/generic/andes/Kconfig b/platform/generic/andes/Kconfig
index a91fb9c..555e4fe 100644
--- a/platform/generic/andes/Kconfig
+++ b/platform/generic/andes/Kconfig
@@ -7,3 +7,7 @@ config ANDES45_PMA
 config ANDES_SBI
 	bool "Andes SBI support"
 	default n
+
+config ANDES_PMU
+	bool "Andes custom PMU extension support"
+	default n
diff --git a/platform/generic/andes/andes_pmu.c b/platform/generic/andes/andes_pmu.c
new file mode 100644
index 0000000..0f6ecc0
--- /dev/null
+++ b/platform/generic/andes/andes_pmu.c
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (C) 2023 Andes Technology Corporation
+ */
+#include <andes/andes45.h>
+#include <andes/andes_pmu.h>
+#include <sbi/riscv_asm.h>
+#include <sbi/sbi_bitops.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_hart.h>
+#include <sbi/sbi_pmu.h>
+#include <sbi/sbi_scratch.h>
+
+static void andes_hw_counter_enable_irq(uint32_t ctr_idx)
+{
+	unsigned long mip_val;
+
+	if (ctr_idx >= SBI_PMU_HW_CTR_MAX)
+		return;
+
+	mip_val = csr_read(CSR_MIP);
+	if (!(mip_val & MIP_PMOVI))
+		csr_clear(CSR_MCOUNTEROVF, BIT(ctr_idx));
+
+	csr_set(CSR_MCOUNTERINTEN, BIT(ctr_idx));
+}
+
+static void andes_hw_counter_disable_irq(uint32_t ctr_idx)
+{
+	csr_clear(CSR_MCOUNTERINTEN, BIT(ctr_idx));
+}
+
+static void andes_hw_counter_filter_mode(unsigned long flags, int ctr_idx)
+{
+	if (flags & SBI_PMU_CFG_FLAG_SET_UINH)
+		csr_set(CSR_MCOUNTERMASK_U, BIT(ctr_idx));
+	else
+		csr_clear(CSR_MCOUNTERMASK_U, BIT(ctr_idx));
+
+	if (flags & SBI_PMU_CFG_FLAG_SET_SINH)
+		csr_set(CSR_MCOUNTERMASK_S, BIT(ctr_idx));
+	else
+		csr_clear(CSR_MCOUNTERMASK_S, BIT(ctr_idx));
+}
+
+static struct sbi_pmu_device andes_pmu = {
+	.name = "andes_pmu",
+	.hw_counter_enable_irq  = andes_hw_counter_enable_irq,
+	.hw_counter_disable_irq = andes_hw_counter_disable_irq,
+	/*
+	 * We set delegation of supervisor local interrupts via
+	 * 18th bit on mslideleg instead of mideleg, so leave
+	 * hw_counter_irq_bit() callback unimplemented.
+	 */
+	.hw_counter_irq_bit     = NULL,
+	.hw_counter_filter_mode = andes_hw_counter_filter_mode
+};
+
+int andes_pmu_init(void)
+{
+	if (!has_andes_pmu())
+		return SBI_ENOTSUPP;
+
+	/*
+	 * It is not reasonable for an Andes CPU to support
+	 * both Andes PMU and standard Sscofpmf, as they
+	 * serve the same purpose.
+	 */
+	if (sbi_hart_has_extension(sbi_scratch_thishart_ptr(),
+				   SBI_HART_EXT_SSCOFPMF))
+		sbi_hart_hang();
+
+	/* Inhibit HPM counter in M-mode */
+	csr_write(CSR_MCOUNTERMASK_M, 0xfffffffd);
+	/* Delegate S-mode local interrupt to S-mode */
+	csr_write(CSR_MSLIDELEG, MIP_PMOVI);
+
+	sbi_pmu_set_device(&andes_pmu);
+
+	return 0;
+}
diff --git a/platform/generic/andes/objects.mk b/platform/generic/andes/objects.mk
index e8f86ea..6a8c66c 100644
--- a/platform/generic/andes/objects.mk
+++ b/platform/generic/andes/objects.mk
@@ -7,3 +7,4 @@ platform-objs-$(CONFIG_PLATFORM_ANDES_AE350) += andes/ae350.o andes/sleep.o
 
 platform-objs-$(CONFIG_ANDES45_PMA) += andes/andes45-pma.o
 platform-objs-$(CONFIG_ANDES_SBI) += andes/andes_sbi.o
+platform-objs-$(CONFIG_ANDES_PMU) += andes/andes_pmu.o
diff --git a/platform/generic/include/andes/andes_pmu.h b/platform/generic/include/andes/andes_pmu.h
new file mode 100644
index 0000000..70b3a12
--- /dev/null
+++ b/platform/generic/include/andes/andes_pmu.h
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: BSD-2-Clause
+
+#ifndef _RISCV_ANDES_PMU_H
+#define _RISCV_ANDES_PMU_H
+
+int andes_pmu_init(void);
+
+#endif /* _RISCV_ANDES_PMU_H */
-- 
2.34.1



  parent reply	other threads:[~2023-10-19 11:37 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-19 11:37 [PATCH v2 00/11] Add Andes PMU extension support Yu Chien Peter Lin
2023-10-19 11:37 ` [PATCH v2 01/11] sbi: sbi_pmu: Improve sbi_pmu_init() error handling Yu Chien Peter Lin
2023-11-16  6:32   ` Anup Patel
2023-10-19 11:37 ` [PATCH v2 02/11] sbi: sbi_pmu: Add hw_counter_filter_mode() to pmu device Yu Chien Peter Lin
2023-11-16  6:36   ` Anup Patel
2023-10-19 11:37 ` [PATCH v2 03/11] platform: include: andes45: Add PMU related CSR defines Yu Chien Peter Lin
2023-11-16  6:39   ` Anup Patel
2023-10-19 11:37 ` Yu Chien Peter Lin [this message]
2023-11-16  6:41   ` [PATCH v2 04/11] platform: andes: Add Andes custom PMU support Anup Patel
2023-10-19 11:37 ` [PATCH v2 05/11] platform: andes: Enable Andes PMU for AE350 Yu Chien Peter Lin
2023-11-16  6:45   ` Anup Patel
2023-10-19 11:37 ` [PATCH v2 06/11] platform: rzfive: Enable Andes PMU for RZ/Five Yu Chien Peter Lin
2023-11-16  6:45   ` Anup Patel
2023-10-19 11:37 ` [PATCH v2 07/11] lib: utils: fdt_fixup: Add fdt_add_pmu_mappings() helper function Yu Chien Peter Lin
2023-10-21 12:25   ` [PATCH v2 10/11] platform: andes: Implement andes_fdt_add_pmu_mappings platform override Inochi Amaoto
2023-10-22  6:50     ` Yu-Chien Peter Lin
2023-11-16  6:59   ` [PATCH v2 07/11] lib: utils: fdt_fixup: Add fdt_add_pmu_mappings() helper function Anup Patel
2023-10-19 11:37 ` [PATCH v2 08/11] lib: utils: fdt_fixup: Allow preserving PMU properties Yu Chien Peter Lin
2023-11-16  6:48   ` Anup Patel
2023-10-19 11:37 ` [PATCH v2 09/11] platform: andes: Factor out is_andes() helper Yu Chien Peter Lin
2023-11-16  6:49   ` Anup Patel
2023-10-19 11:37 ` [PATCH v2 10/11] platform: andes: Implement andes_fdt_add_pmu_mappings platform override Yu Chien Peter Lin
2023-11-16  6:58   ` Anup Patel
2023-10-19 11:37 ` [PATCH v2 11/11] docs: pmu: Add Andes PMU node example Yu Chien Peter Lin
2023-11-16  7:00   ` Anup Patel
2023-11-16  7:01 ` [PATCH v2 00/11] Add Andes PMU extension support Anup Patel
2023-11-21  7:44   ` Yu-Chien Peter Lin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231019113713.3508153-5-peterlin@andestech.com \
    --to=peterlin@andestech.com \
    --cc=opensbi@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox