Skip to contentSkip to conversation

Add SiFive TMC0 and SMC0 driver

PATCH Superseded Nick Hu 3 participants 19 messages Aug 20, 2025 – Sep 1, 2025 SiFive drivers

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Nick Hu

author
#0 lore raw

v4 cover Add SiFive TMC0 and SMC0 driver

SiFive TMC0 and SMC0 are the power controllers that controls the Tile and CoreComplex power domain. Add the drivers to support the power management features such as HSM STOP and System Suspend.

Features such as power management may require flushing the entire cache before entering a non-retention power state. To support this, a simple cache flush framework is introduced to allow the external CMO to perform a full cache flush.

Changes in v4:
- Call fdt_cmo_init() directly as part of generic_early_init()
- Rename SBI_HART_EXT_XSF* to SBI_HART_EXT_XSIFIVE*
- Move the TMC0 driver to the lib/utils/hsm/
- Move the SMC0 driver to the lib/utils/suspend/

Changes in v3:
- Add the SiFive TMC and SMC driver

Changes in v2:
- Since the platform override hooks was deprecated, use the fdt_driver for initializing the sifive_dev_platform.

Nick Hu (10):
lib: utils: Add cache flush library
lib: utils: Add FDT cache library
lib: utils/cache: Add fdt cmo helpers
lib: sbi: Add SiFive proprietary xsfcflushdlone
lib: sbi: Add SiFive proprietary xsfcease
lib: utils/ipi: Exposing the ACLINT IPI device
lib: utils/irqchip: Add APLIC restore function
platform: sifive: Add SiFive TMC0 driver
lib: utils/timer: Expose timer update function
platform: sifive: Add SiFive SMC0 driver

Vincent Chen (1):
utils: cache: Add SiFive ccache controller

 include/sbi/sbi_hart.h                        |   4 +
 include/sbi_utils/cache/cache.h               |  69 +++
 include/sbi_utils/cache/fdt_cache.h           |  34 ++
 include/sbi_utils/cache/fdt_cmo_helper.h      |  34 ++
 include/sbi_utils/hsm/fdt_hsm_sifive_inst.h   |  20 +
 include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h   |  19 +
 include/sbi_utils/ipi/aclint_mswi.h           |   1 +
 include/sbi_utils/irqchip/aplic.h             |   3 +
 .../suspend/fdt_suspend_sifive_smc0.h         |  18 +
 include/sbi_utils/timer/aclint_mtimer.h       |   5 +
 lib/sbi/sbi_hart.c                            |   2 +
 lib/utils/Kconfig                             |   2 +
 lib/utils/cache/Kconfig                       |  23 +
 lib/utils/cache/cache.c                       |  46 ++
 lib/utils/cache/fdt_cache.c                   |  87 ++++
 lib/utils/cache/fdt_cache_drivers.carray      |   3 +
 lib/utils/cache/fdt_cmo_helper.c              | 112 +++++
 lib/utils/cache/fdt_sifive_ccache.c           | 175 +++++++
 lib/utils/cache/objects.mk                    |  14 +
 lib/utils/hsm/Kconfig                         |   5 +
 lib/utils/hsm/fdt_hsm_sifive_tmc0.c           | 468 ++++++++++++++++++
 lib/utils/hsm/objects.mk                      |   2 +
 lib/utils/ipi/aclint_mswi.c                   |   5 +
 lib/utils/irqchip/aplic.c                     | 160 +++---
 lib/utils/suspend/Kconfig                     |   4 +
 lib/utils/suspend/fdt_suspend_sifive_smc0.c   | 323 ++++++++++++
 lib/utils/suspend/objects.mk                  |   3 +
 lib/utils/timer/aclint_mtimer.c               |  28 +-
 platform/generic/Kconfig                      |   5 +
 platform/generic/configs/defconfig            |   5 +
 platform/generic/platform.c                   |   3 +-
 platform/generic/sifive/objects.mk            |   3 +
 platform/generic/sifive/sifive_dev_platform.c |  57 +++
 33 files changed, 1667 insertions(+), 75 deletions(-)
 create mode 100644 include/sbi_utils/cache/cache.h
 create mode 100644 include/sbi_utils/cache/fdt_cache.h
 create mode 100644 include/sbi_utils/cache/fdt_cmo_helper.h
 create mode 100644 include/sbi_utils/hsm/fdt_hsm_sifive_inst.h
 create mode 100644 include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h
 create mode 100644 include/sbi_utils/suspend/fdt_suspend_sifive_smc0.h
 create mode 100644 lib/utils/cache/Kconfig
 create mode 100644 lib/utils/cache/cache.c
 create mode 100644 lib/utils/cache/fdt_cache.c
 create mode 100644 lib/utils/cache/fdt_cache_drivers.carray
 create mode 100644 lib/utils/cache/fdt_cmo_helper.c
 create mode 100644 lib/utils/cache/fdt_sifive_ccache.c
 create mode 100644 lib/utils/cache/objects.mk
 create mode 100644 lib/utils/hsm/fdt_hsm_sifive_tmc0.c
 create mode 100644 lib/utils/suspend/fdt_suspend_sifive_smc0.c
 create mode 100644 platform/generic/sifive/sifive_dev_platform.c
Signature · 7 lines
2.17.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#1 lore raw

PATCH v4 1/11 lib: utils: Add cache flush library

The current RISC-V CMO only defines how to flush a cache block. However, certain use cases, such as power management, may require flushing the entire cache. Therefore, a framework is being introduced to allow vendors to flush the entire cache using their own methods.

---
 include/sbi_utils/cache/cache.h | 69 +++++++++++++++++++++++++++++++++
 lib/utils/Kconfig               |  2 +
 lib/utils/cache/Kconfig         |  9 +++++
 lib/utils/cache/cache.c         | 46 ++++++++++++++++++++++
 lib/utils/cache/objects.mk      |  7 ++++
 5 files changed, 133 insertions(+)
 create mode 100644 include/sbi_utils/cache/cache.h
 create mode 100644 lib/utils/cache/Kconfig
 create mode 100644 lib/utils/cache/cache.c
 create mode 100644 lib/utils/cache/objects.mk
include/sbi_utils/cache/cache.h new file +69 -0
new file mode 100644
index 00000000..70d9286f
@@ -0,0 +1,69 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __CACHE_H__
+#define __CACHE_H__
+
+#include <sbi/sbi_list.h>
+#include <sbi/sbi_types.h>
+
+#define CACHE_NAME_LEN	32
+
+struct cache_device;
+
+struct cache_ops {
+	/** Warm init **/
+	int (*warm_init)(struct cache_device *dev);
+	/** Flush entire cache **/
+	int (*cache_flush_all)(struct cache_device *dev);
+};
+
+struct cache_device {
+	/** Name of the device **/
+	char name[CACHE_NAME_LEN];
+	/** List node for search **/
+	struct sbi_dlist node;
+	/** Point to the next level cache **/
+	struct cache_device *next;
+	/** Cache Management Operations **/
+	struct cache_ops *ops;
+	/** CPU private cache **/
+	bool cpu_private;
+	/** The unique id of this cache device **/
+	u32 id;
+};
+
+/**
+ * Find a registered cache device
+ *
+ * @param id unique ID of the cache device
+ *
+ * @return the cache device or NULL
+ */
+struct cache_device *cache_find(u32 id);
+
+/**
+ * Register a cache device
+ *
+ * cache_device->id must be initialized already and must not change during the life
+ * of the cache_device object.
+ *
+ * @param dev the cache device to register
+ *
+ * @return 0 on success, or a negative error code on failure
+ */
+int cache_add(struct cache_device *dev);
+
+/**
+ * Flush the entire cache
+ *
+ * @param dev the cache to flush
+ *
+ * @return 0 on success, or a negative error code on failure
+ */
+int cache_flush_all(struct cache_device *dev);
+
+#endif
lib/utils/Kconfig +2 -0
index 901ba564..5a5b3b1e 100644
@@ -2,6 +2,8 @@
 
 menu "Utils and Drivers Support"
 
+source "$(OPENSBI_SRC_DIR)/lib/utils/cache/Kconfig"
+
 source "$(OPENSBI_SRC_DIR)/lib/utils/cppc/Kconfig"
 
 source "$(OPENSBI_SRC_DIR)/lib/utils/fdt/Kconfig"
lib/utils/cache/Kconfig new file +9 -0
new file mode 100644
index 00000000..24aa41bc
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+menu "Cache Support"
+
+config CACHE
+	bool "Cache support"
+	default n
+
+endmenu
lib/utils/cache/cache.c new file +46 -0

46 lines not on this page · raw message

lib/utils/cache/objects.mk new file +7 -0
new file mode 100644
index 00000000..21d30ce8
@@ -0,0 +1,7 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2025 SiFive
+#
+
+libsbiutils-objs-$(CONFIG_CACHE) += cache/cache.o
Signature · 7 lines
2.17.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#2 lore raw

PATCH v4 2/11 lib: utils: Add FDT cache library

In reply to Nick Hu

Add the FDT cache library so we can build up the cache topology via the 'next-level-cache' DT property.

---
 include/sbi_utils/cache/fdt_cache.h      | 34 +++++++++
 lib/utils/cache/Kconfig                  |  6 ++
 lib/utils/cache/fdt_cache.c              | 87 ++++++++++++++++++++++++
 lib/utils/cache/fdt_cache_drivers.carray |  3 +
 lib/utils/cache/objects.mk               |  3 +
 platform/generic/configs/defconfig       |  1 +
 6 files changed, 134 insertions(+)
 create mode 100644 include/sbi_utils/cache/fdt_cache.h
 create mode 100644 lib/utils/cache/fdt_cache.c
 create mode 100644 lib/utils/cache/fdt_cache_drivers.carray
include/sbi_utils/cache/fdt_cache.h new file +34 -0
new file mode 100644
index 00000000..b377d6f5
@@ -0,0 +1,34 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __FDT_CACHE_H__
+#define __FDT_CACHE_H__
+
+#include <sbi_utils/cache/cache.h>
+
+/**
+ * Register a cache device using information from the DT
+ *
+ * @param fdt devicetree blob
+ * @param noff offset of a node in the devicetree blob
+ * @param dev cache device to register for this devicetree node
+ *
+ * @return 0 on success, or a negative error code on failure
+ */
+int fdt_cache_add(const void *fdt, int noff, struct cache_device *dev);
+
+/**
+ * Get the cache device referencd by the "next-level-cache" property of a DT node
+ *
+ * @param fdt devicetree blob
+ * @param noff offset of a node in the devicetree blob
+ * @param out_dev location to return the cache device
+ *
+ * @return 0 on success, or a negative error code on failure
+ */
+int fdt_next_cache_get(const void *fdt, int noff, struct cache_device **out_dev);
+
+#endif
lib/utils/cache/Kconfig +6 -0
index 24aa41bc..10602176 100644
@@ -2,6 +2,12 @@
 
 menu "Cache Support"
 
+config FDT_CACHE
+	bool "FDT based cache drivers"
+	depends on FDT
+	select CACHE
+	default n
+
 config CACHE
 	bool "Cache support"
 	default n
lib/utils/cache/fdt_cache.c new file +87 -0

87 lines not on this page · raw message

lib/utils/cache/fdt_cache_drivers.carray new file +3 -0
new file mode 100644
index 00000000..c1b1791c
@@ -0,0 +1,3 @@
+HEADER: sbi_utils/cache/fdt_cache.h
+TYPE: const struct fdt_driver
+NAME: fdt_cache_drivers
lib/utils/cache/objects.mk +3 -0
index 21d30ce8..2fcf9666 100644
@@ -4,4 +4,7 @@
 # Copyright (c) 2025 SiFive
 #
 
+libsbiutils-objs-$(CONFIG_FDT_CACHE) += cache/fdt_cache.o
+libsbiutils-objs-$(CONFIG_FDT_CACHE) += cache/fdt_cache_drivers.carray.o
+
 libsbiutils-objs-$(CONFIG_CACHE) += cache/cache.o
platform/generic/configs/defconfig +1 -0
index c7a6531e..b5a7993d 100644
@@ -7,6 +7,7 @@ CONFIG_PLATFORM_SOPHGO_SG2042=y
 CONFIG_PLATFORM_STARFIVE_JH7110=y
 CONFIG_PLATFORM_THEAD=y
 CONFIG_PLATFORM_MIPS_P8700=y
+CONFIG_FDT_CACHE=y
 CONFIG_FDT_CPPC=y
 CONFIG_FDT_CPPC_RPMI=y
 CONFIG_FDT_GPIO=y
Signature · 7 lines
2.17.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#3 lore raw

PATCH v4 3/11 utils: cache: Add SiFive ccache controller

In reply to Nick Hu

From: Vincent Chen <[email protected]>

SiFive Composable cache is a L3 share cache of the core complex. Add this driver to support the share cache maintenance operations via the MMIO registers.

---
 lib/utils/cache/Kconfig             |   8 ++
 lib/utils/cache/fdt_sifive_ccache.c | 175 ++++++++++++++++++++++++++++
 lib/utils/cache/objects.mk          |   3 +
 platform/generic/configs/defconfig  |   1 +
 4 files changed, 187 insertions(+)
 create mode 100644 lib/utils/cache/fdt_sifive_ccache.c
lib/utils/cache/Kconfig +8 -0
index 10602176..1c7abdc9 100644
@@ -8,6 +8,14 @@ config FDT_CACHE
 	select CACHE
 	default n
 
+if FDT_CACHE
+
+config FDT_CACHE_SIFIVE_CCACHE
+	bool "SiFive CCACHE FDT cache driver"
+	default n
+
+endif
+
 config CACHE
 	bool "Cache support"
 	default n
lib/utils/cache/fdt_sifive_ccache.c new file +175 -0

175 lines not on this page · raw message

lib/utils/cache/objects.mk +3 -0
index 2fcf9666..a343eb8c 100644
@@ -7,4 +7,7 @@
 libsbiutils-objs-$(CONFIG_FDT_CACHE) += cache/fdt_cache.o
 libsbiutils-objs-$(CONFIG_FDT_CACHE) += cache/fdt_cache_drivers.carray.o
 
+carray-fdt_cache_drivers-$(CONFIG_FDT_CACHE_SIFIVE_CCACHE) += fdt_sifive_ccache
+libsbiutils-objs-$(CONFIG_FDT_CACHE_SIFIVE_CCACHE) += cache/fdt_sifive_ccache.o
+
 libsbiutils-objs-$(CONFIG_CACHE) += cache/cache.o
platform/generic/configs/defconfig +1 -0
index b5a7993d..6b219cfb 100644
@@ -8,6 +8,7 @@ CONFIG_PLATFORM_STARFIVE_JH7110=y
 CONFIG_PLATFORM_THEAD=y
 CONFIG_PLATFORM_MIPS_P8700=y
 CONFIG_FDT_CACHE=y
+CONFIG_FDT_CACHE_SIFIVE_CCACHE=y
 CONFIG_FDT_CPPC=y
 CONFIG_FDT_CPPC_RPMI=y
 CONFIG_FDT_GPIO=y
Signature · 7 lines
2.17.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#4 lore raw

PATCH v4 4/11 lib: utils/cache: Add fdt cmo helpers

In reply to Nick Hu

Add the helpers to build up the cache hierarchy via FDT and provide some cmo functions for the user who want to flush the entire cache.

---
 include/sbi_utils/cache/fdt_cmo_helper.h |  34 +++++++
 lib/utils/cache/fdt_cmo_helper.c         | 112 +++++++++++++++++++++++
 lib/utils/cache/objects.mk               |   1 +
 platform/generic/platform.c              |   3 +-
 4 files changed, 149 insertions(+), 1 deletion(-)
 create mode 100644 include/sbi_utils/cache/fdt_cmo_helper.h
 create mode 100644 lib/utils/cache/fdt_cmo_helper.c
include/sbi_utils/cache/fdt_cmo_helper.h new file +34 -0
new file mode 100644
index 00000000..4275725e
@@ -0,0 +1,34 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __FDT_CMO_HELPER_H__
+#define __FDT_CMO_HELPER_H__
+
+/**
+ * Flush the private first level cache of the current hart
+ *
+ * @return 0 on success, or a negative error code on failure
+ */
+int fdt_cmo_private_flc_flush_all(void);
+
+/**
+ * Flush the last level cache of the current hart
+ *
+ * @return 0 on success, or a negative error code on failure
+ */
+int fdt_cmo_llc_flush_all(void);
+
+/**
+ * Initialize the cache devices for each hart
+ *
+ * @param fdt devicetree blob
+ * @param cold_boot cold init or warm init
+ *
+ * @return 0 on success, or a negative error code on failure
+ */
+int fdt_cmo_init(bool cold_boot);
+
+#endif
lib/utils/cache/fdt_cmo_helper.c new file +112 -0

112 lines not on this page · raw message

lib/utils/cache/objects.mk +1 -0
index a343eb8c..6829a966 100644
@@ -6,6 +6,7 @@
 
 libsbiutils-objs-$(CONFIG_FDT_CACHE) += cache/fdt_cache.o
 libsbiutils-objs-$(CONFIG_FDT_CACHE) += cache/fdt_cache_drivers.carray.o
+libsbiutils-objs-$(CONFIG_FDT_CACHE) += cache/fdt_cmo_helper.o
 
 carray-fdt_cache_drivers-$(CONFIG_FDT_CACHE_SIFIVE_CCACHE) += fdt_sifive_ccache
 libsbiutils-objs-$(CONFIG_FDT_CACHE_SIFIVE_CCACHE) += cache/fdt_sifive_ccache.o
platform/generic/platform.c +2 -1
index 47e771ad..de40791b 100644
@@ -17,6 +17,7 @@
 #include <sbi/sbi_string.h>
 #include <sbi/sbi_system.h>
 #include <sbi/sbi_tlb.h>
+#include <sbi_utils/cache/fdt_cmo_helper.h>
 #include <sbi_utils/fdt/fdt_domain.h>
 #include <sbi_utils/fdt/fdt_driver.h>
 #include <sbi_utils/fdt/fdt_fixup.h>
@@ -231,7 +232,7 @@ int generic_early_init(bool cold_boot)
 		fdt_driver_init_all(fdt, fdt_early_drivers);
 	}
 
-	return 0;
+	return fdt_cmo_init(cold_boot);
 }
 
 int generic_final_init(bool cold_boot)
Signature · 7 lines
2.17.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#5 lore raw

PATCH v4 5/11 lib: sbi: Add SiFive proprietary xsfcflushdlone

In reply to Nick Hu

Using ISA string "xsfcflushdlone" to detect the support of the SiFive L1D cache flush custom instruction.

---
 include/sbi/sbi_hart.h | 2 ++
 lib/sbi/sbi_hart.c     | 1 +
 2 files changed, 3 insertions(+)
include/sbi/sbi_hart.h +2 -0
index 82b19dcf..fdcf400a 100644
@@ -81,6 +81,8 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_SSCTR,
 	/** HART has Ssstateen extension **/
 	SBI_HART_EXT_SSSTATEEN,
+	/** Hart has Xsfcflushdlone extension */
+	SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1,
 
 	/** Maximum index of Hart extension */
 	SBI_HART_EXT_MAX,
lib/sbi/sbi_hart.c +1 -0
index 6a2d7d6f..4160ad6c 100644
@@ -715,6 +715,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
 	__SBI_HART_EXT_DATA(smctr, SBI_HART_EXT_SMCTR),
 	__SBI_HART_EXT_DATA(ssctr, SBI_HART_EXT_SSCTR),
 	__SBI_HART_EXT_DATA(ssstateen, SBI_HART_EXT_SSSTATEEN),
+	__SBI_HART_EXT_DATA(xsfcflushdlone, SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1),
 };
 
 _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
Signature · 7 lines
2.17.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v4 5/11
#6 lore raw

Re: [PATCH v4 05/11] lib: sbi: Add SiFive proprietary xsfcflushdlone

Quoted 6 lines · Nick Hu

Using ISA string "xsfcflushdlone" to detect the support of the SiFive L1D cache flush custom instruction.

LGTM.

Regards,
Anup

Quoted 33 lines
---
 include/sbi/sbi_hart.h | 2 ++
 lib/sbi/sbi_hart.c     | 1 +
 2 files changed, 3 insertions(+)
include/sbi/sbi_hart.h +2 -0
index 82b19dcf..fdcf400a 100644
@@ -81,6 +81,8 @@ enum sbi_hart_extensions {
        SBI_HART_EXT_SSCTR,
        /** HART has Ssstateen extension **/
        SBI_HART_EXT_SSSTATEEN,
+       /** Hart has Xsfcflushdlone extension */
+       SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1,
 
        /** Maximum index of Hart extension */
        SBI_HART_EXT_MAX,
lib/sbi/sbi_hart.c +1 -1
index 6a2d7d6f..4160ad6c 100644
@@ -715,6 +715,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
        __SBI_HART_EXT_DATA(smctr, SBI_HART_EXT_SMCTR),
        __SBI_HART_EXT_DATA(ssctr, SBI_HART_EXT_SSCTR),
        __SBI_HART_EXT_DATA(ssstateen, SBI_HART_EXT_SSSTATEEN),
+       __SBI_HART_EXT_DATA(xsfcflushdlone, SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1),
 };
 
 _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
--

2.17.1

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#7 lore raw

PATCH v4 6/11 lib: sbi: Add SiFive proprietary xsfcease

In reply to Nick Hu

Using ISA string "xsfcease" to detect the support of the custom instruction "CEASE".

---
 include/sbi/sbi_hart.h | 2 ++
 lib/sbi/sbi_hart.c     | 1 +
 2 files changed, 3 insertions(+)
include/sbi/sbi_hart.h +2 -0
index fdcf400a..ff682fb4 100644
@@ -83,6 +83,8 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_SSSTATEEN,
 	/** Hart has Xsfcflushdlone extension */
 	SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1,
+	/** Hart has Xsfcease extension */
+	SBI_HART_EXT_XSIFIVE_CEASE,
 
 	/** Maximum index of Hart extension */
 	SBI_HART_EXT_MAX,
lib/sbi/sbi_hart.c +1 -0
index 4160ad6c..2519f3f0 100644
@@ -716,6 +716,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
 	__SBI_HART_EXT_DATA(ssctr, SBI_HART_EXT_SSCTR),
 	__SBI_HART_EXT_DATA(ssstateen, SBI_HART_EXT_SSSTATEEN),
 	__SBI_HART_EXT_DATA(xsfcflushdlone, SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1),
+	__SBI_HART_EXT_DATA(xsfcease, SBI_HART_EXT_XSIFIVE_CEASE),
 };
 
 _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
Signature · 7 lines
2.17.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v4 6/11
#8 lore raw

Re: [PATCH v4 06/11] lib: sbi: Add SiFive proprietary xsfcease

Quoted 6 lines · Nick Hu

Using ISA string "xsfcease" to detect the support of the custom instruction "CEASE".

LGTM.

Regards,
Anup

Quoted 33 lines
---
 include/sbi/sbi_hart.h | 2 ++
 lib/sbi/sbi_hart.c     | 1 +
 2 files changed, 3 insertions(+)
include/sbi/sbi_hart.h +2 -0
index fdcf400a..ff682fb4 100644
@@ -83,6 +83,8 @@ enum sbi_hart_extensions {
        SBI_HART_EXT_SSSTATEEN,
        /** Hart has Xsfcflushdlone extension */
        SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1,
+       /** Hart has Xsfcease extension */
+       SBI_HART_EXT_XSIFIVE_CEASE,
 
        /** Maximum index of Hart extension */
        SBI_HART_EXT_MAX,
lib/sbi/sbi_hart.c +1 -1
index 4160ad6c..2519f3f0 100644
@@ -716,6 +716,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
        __SBI_HART_EXT_DATA(ssctr, SBI_HART_EXT_SSCTR),
        __SBI_HART_EXT_DATA(ssstateen, SBI_HART_EXT_SSSTATEEN),
        __SBI_HART_EXT_DATA(xsfcflushdlone, SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1),
+       __SBI_HART_EXT_DATA(xsfcease, SBI_HART_EXT_XSIFIVE_CEASE),
 };
 
 _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
--

2.17.1

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#9 lore raw

PATCH v4 7/11 lib: utils/ipi: Exposing the ACLINT IPI device

In reply to Nick Hu

Exposing the ACLINT IPI device so the other driver can send the CLINT IPI on the platform with IMSIC.

---
 include/sbi_utils/ipi/aclint_mswi.h | 1 +
 lib/utils/ipi/aclint_mswi.c         | 5 +++++
 2 files changed, 6 insertions(+)
include/sbi_utils/ipi/aclint_mswi.h +1 -0
index 4b8249f2..311228eb 100644
@@ -26,6 +26,7 @@ struct aclint_mswi_data {
 	u32 hart_count;
 };
 
+struct sbi_ipi_device *aclint_mswi_get(void);
 int aclint_mswi_cold_init(struct aclint_mswi_data *mswi);
 
 #endif
lib/utils/ipi/aclint_mswi.c +5 -0
index 9e55078a..a4173b7c 100644
@@ -66,6 +66,11 @@ static struct sbi_ipi_device aclint_mswi = {
 	.ipi_clear = mswi_ipi_clear
 };
 
+struct sbi_ipi_device *aclint_mswi_get(void)
+{
+	return &aclint_mswi;
+}
+
 int aclint_mswi_cold_init(struct aclint_mswi_data *mswi)
 {
 	u32 i;
Signature · 7 lines
2.17.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#10 lore raw

PATCH v4 8/11 lib: utils/irqchip: Add APLIC restore function

In reply to Nick Hu

Since the APLIC may enter a reset state upon system wake-up from a platform low power state, adding a restore function to reinitialize the APLIC.

---
 include/sbi_utils/irqchip/aplic.h |   3 +
 lib/utils/irqchip/aplic.c         | 160 +++++++++++++++++-------------
 2 files changed, 96 insertions(+), 67 deletions(-)
include/sbi_utils/irqchip/aplic.h +3 -0
index e31f48a4..ca606d06 100644
@@ -33,6 +33,7 @@ struct aplic_delegate_data {
 struct aplic_data {
 	/* Private members */
 	struct sbi_irqchip_device irqchip;
+	struct sbi_dlist node;
 	/* Public members */
 	unsigned long addr;
 	unsigned long size;
@@ -48,4 +49,6 @@ struct aplic_data {
 
 int aplic_cold_irqchip_init(struct aplic_data *aplic);
 
+void aplic_restore(void);
+
 #endif
lib/utils/irqchip/aplic.c +93 -67

194 lines not on this page · raw message

Signature · 7 lines
2.17.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#11 lore raw

PATCH v4 9/11 platform: sifive: Add SiFive TMC0 driver

In reply to Nick Hu

The SiFive TMC0 controls the tile power domains on SiFive platform. The CPU enters the low power state via the CEASE instruction after configuring the TMC0. Any devices that inside the tile power domain will be power gated, including the private cache. Therefore flushing the private cache before entering the low power state.

---
 include/sbi_utils/hsm/fdt_hsm_sifive_inst.h   |  20 +
 include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h   |  16 +
 lib/utils/hsm/Kconfig                         |   5 +
 lib/utils/hsm/fdt_hsm_sifive_tmc0.c           | 380 ++++++++++++++++++
 lib/utils/hsm/objects.mk                      |   2 +
 platform/generic/Kconfig                      |   5 +
 platform/generic/configs/defconfig            |   2 +
 platform/generic/sifive/objects.mk            |   3 +
 platform/generic/sifive/sifive_dev_platform.c |  57 +++
 9 files changed, 490 insertions(+)
 create mode 100644 include/sbi_utils/hsm/fdt_hsm_sifive_inst.h
 create mode 100644 include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h
 create mode 100644 lib/utils/hsm/fdt_hsm_sifive_tmc0.c
 create mode 100644 platform/generic/sifive/sifive_dev_platform.c
include/sbi_utils/hsm/fdt_hsm_sifive_inst.h new file +20 -0
new file mode 100644
index 00000000..de8aba19
@@ -0,0 +1,20 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __FDT_HSM_SIFIVE_INST_H__
+#define __FDT_HSM_SIFIVE_INST_H__
+
+static inline void sifive_cease(void)
+{
+	__asm__ __volatile__(".insn 0x30500073" ::: "memory");
+}
+
+static inline void sifive_cflush(void)
+{
+	__asm__ __volatile__(".insn 0xfc000073" ::: "memory");
+}
+
+#endif
include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h new file +16 -0
new file mode 100644
index 00000000..4ba048b9
@@ -0,0 +1,16 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __FDT_HSM_SIFIVE_TMC0_H__
+#define __FDT_HSM_SIFIVE_TMC0_H__
+
+#ifdef CONFIG_FDT_HSM_SIFIVE_TMC0
+int fdt_hsm_sifive_tmc0_cold_init(void);
+#else
+static inline int fdt_hsm_sifive_tmc0_cold_init(void) { return 0; }
+#endif
+
+#endif
lib/utils/hsm/Kconfig +5 -0
index 1ad7958f..3e6f8909 100644
@@ -14,6 +14,11 @@ config FDT_HSM_RPMI
 	depends on FDT_MAILBOX && RPMI_MAILBOX
 	default n
 
+config FDT_HSM_SIFIVE_TMC0
+	bool "FDT SiFive TMC v0 driver"
+	depends on PLATFORM_SIFIVE_DEV
+	default n
+
 endif
 
 endmenu
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +380 -0

380 lines not on this page · raw message

lib/utils/hsm/objects.mk +2 -0
index c13d81f7..04f4a208 100644
@@ -9,3 +9,5 @@
 
 carray-fdt_early_drivers-$(CONFIG_FDT_HSM_RPMI) += fdt_hsm_rpmi
 libsbiutils-objs-$(CONFIG_FDT_HSM_RPMI) += hsm/fdt_hsm_rpmi.o
+
+libsbiutils-objs-$(CONFIG_FDT_HSM_SIFIVE_TMC0) += hsm/fdt_hsm_sifive_tmc0.o
platform/generic/Kconfig +5 -0
index a24d6ab2..20c25a83 100644
@@ -43,6 +43,11 @@ config PLATFORM_RENESAS_RZFIVE
 	select ANDES_PMU
 	default n
 
+config PLATFORM_SIFIVE_DEV
+	bool "SiFive development platform support"
+	depends on FDT_CACHE
+	default n
+
 config PLATFORM_SIFIVE_FU540
 	bool "SiFive FU540 support"
 	default n
platform/generic/configs/defconfig +2 -0
index 6b219cfb..837acd79 100644
@@ -1,6 +1,7 @@
 CONFIG_PLATFORM_ALLWINNER_D1=y
 CONFIG_PLATFORM_ANDES_AE350=y
 CONFIG_PLATFORM_RENESAS_RZFIVE=y
+CONFIG_PLATFORM_SIFIVE_DEV=y
 CONFIG_PLATFORM_SIFIVE_FU540=y
 CONFIG_PLATFORM_SIFIVE_FU740=y
 CONFIG_PLATFORM_SOPHGO_SG2042=y
@@ -17,6 +18,7 @@ CONFIG_FDT_GPIO_SIFIVE=y
 CONFIG_FDT_GPIO_STARFIVE=y
 CONFIG_FDT_HSM=y
 CONFIG_FDT_HSM_RPMI=y
+CONFIG_FDT_HSM_SIFIVE_TMC0=y
 CONFIG_FDT_I2C=y
 CONFIG_FDT_I2C_SIFIVE=y
 CONFIG_FDT_I2C_DW=y
platform/generic/sifive/objects.mk +3 -0
index d75e444a..d32e1273 100644
@@ -2,6 +2,9 @@
 # SPDX-License-Identifier: BSD-2-Clause
 #
 
+carray-platform_override_modules-$(CONFIG_PLATFORM_SIFIVE_DEV) += sifive_dev_platform
+platform-objs-$(CONFIG_PLATFORM_SIFIVE_DEV) += sifive/sifive_dev_platform.o
+
 carray-platform_override_modules-$(CONFIG_PLATFORM_SIFIVE_FU540) += sifive_fu540
 platform-objs-$(CONFIG_PLATFORM_SIFIVE_FU540) += sifive/fu540.o
 
platform/generic/sifive/sifive_dev_platform.c new file +57 -0

57 lines not on this page · raw message

Signature · 7 lines
2.17.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#12 lore raw

Re: [PATCH v4 09/11] platform: sifive: Add SiFive TMC0 driver

Quoted 434 lines · Nick Hu

The SiFive TMC0 controls the tile power domains on SiFive platform. The CPU enters the low power state via the CEASE instruction after configuring the TMC0. Any devices that inside the tile power domain will be power gated, including the private cache. Therefore flushing the private cache before entering the low power state.

---
 include/sbi_utils/hsm/fdt_hsm_sifive_inst.h   |  20 +
 include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h   |  16 +
 lib/utils/hsm/Kconfig                         |   5 +
 lib/utils/hsm/fdt_hsm_sifive_tmc0.c           | 380 ++++++++++++++++++
 lib/utils/hsm/objects.mk                      |   2 +
 platform/generic/Kconfig                      |   5 +
 platform/generic/configs/defconfig            |   2 +
 platform/generic/sifive/objects.mk            |   3 +
 platform/generic/sifive/sifive_dev_platform.c |  57 +++
 9 files changed, 490 insertions(+)
 create mode 100644 include/sbi_utils/hsm/fdt_hsm_sifive_inst.h
 create mode 100644 include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h
 create mode 100644 lib/utils/hsm/fdt_hsm_sifive_tmc0.c
 create mode 100644 platform/generic/sifive/sifive_dev_platform.c
include/sbi_utils/hsm/fdt_hsm_sifive_inst.h new file +20 -0
new file mode 100644
index 00000000..de8aba19
@@ -0,0 +1,20 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __FDT_HSM_SIFIVE_INST_H__
+#define __FDT_HSM_SIFIVE_INST_H__
+
+static inline void sifive_cease(void)
+{
+       __asm__ __volatile__(".insn 0x30500073" ::: "memory");
+}
+
+static inline void sifive_cflush(void)
+{
+       __asm__ __volatile__(".insn 0xfc000073" ::: "memory");
+}
+
+#endif
include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h new file +16 -0
new file mode 100644
index 00000000..4ba048b9
@@ -0,0 +1,16 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __FDT_HSM_SIFIVE_TMC0_H__
+#define __FDT_HSM_SIFIVE_TMC0_H__
+
+#ifdef CONFIG_FDT_HSM_SIFIVE_TMC0
+int fdt_hsm_sifive_tmc0_cold_init(void);
+#else
+static inline int fdt_hsm_sifive_tmc0_cold_init(void) { return 0; }
+#endif
+
+#endif
lib/utils/hsm/Kconfig +5 -0
index 1ad7958f..3e6f8909 100644
@@ -14,6 +14,11 @@ config FDT_HSM_RPMI
        depends on FDT_MAILBOX && RPMI_MAILBOX
        default n
 
+config FDT_HSM_SIFIVE_TMC0
+       bool "FDT SiFive TMC v0 driver"
+       depends on PLATFORM_SIFIVE_DEV
+       default n
+
 endif
 
 endmenu
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +337 -0
new file mode 100644
index 00000000..83f86c88
@@ -0,0 +1,380 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive
+ */
+
+#include <libfdt.h>
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_io.h>
+#include <sbi/sbi_bitops.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_hart.h>
+#include <sbi/sbi_heap.h>
+#include <sbi/sbi_hsm.h>
+#include <sbi/sbi_ipi.h>
+#include <sbi_utils/cache/fdt_cmo_helper.h>
+#include <sbi_utils/fdt/fdt_driver.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/hsm/fdt_hsm_sifive_inst.h>
+#include <sbi_utils/hsm/fdt_hsm_sifive_tmc0.h>
+#include <sbi_utils/ipi/aclint_mswi.h>
+
+struct sifive_tmc0 {
+       unsigned long reg;
+       struct sbi_dlist node;
+       u32 id;
+};
+
+static SBI_LIST_HEAD(tmc0_list);
+static unsigned long tmc0_offset;
+
+#define tmc0_ptr_get(__scratch)                                \
+       sbi_scratch_read_type((__scratch), struct sifive_tmc0 *, tmc0_offset)
+
+#define tmc0_ptr_set(__scratch, __tmc0)                        \
+       sbi_scratch_write_type((__scratch), struct sifive_tmc0 *, tmc0_offset, (__tmc0))
+
+/* TMC.PGPREP */
+#define SIFIVE_TMC_PGPREP_OFF                  0x0
+#define SIFIVE_TMC_PGPREP_ENA_REQ              BIT(31)
+#define SIFIVE_TMC_PGPREP_ENA_ACK              BIT(30)
+#define SIFIVE_TMC_PGPREP_DIS_REQ              BIT(29)
+#define SIFIVE_TMC_PGPREP_DIS_ACK              BIT(28)
+#define SIFIVE_TMC_PGPREP_CLFPNOTQ             BIT(18)
+#define SIFIVE_TMC_PGPREP_PMCENAERR            BIT(17)
+#define SIFIVE_TMC_PGPREP_PMCDENY              BIT(16)
+#define SIFIVE_TMC_PGPREP_BUSERR               BIT(15)
+#define SIFIVE_TMC_PGPREP_WAKE_DETECT          BIT(12)
+#define SIFIVE_TMC_PGPREP_INTERNAL_ABORT       BIT(2)
+#define SIFIVE_TMC_PGPREP_ENARSP               (SIFIVE_TMC_PGPREP_CLFPNOTQ | \
+                                                SIFIVE_TMC_PGPREP_PMCENAERR | \
+                                                SIFIVE_TMC_PGPREP_PMCDENY | \
+                                                SIFIVE_TMC_PGPREP_BUSERR | \
+                                                SIFIVE_TMC_PGPREP_WAKE_DETECT)
+
+/* TMC.PG */
+#define SIFIVE_TMC_PG_OFF                      0x4
+#define SIFIVE_TMC_PG_ENA_REQ                  BIT(31)
+#define SIFIVE_TMC_PG_ENA_ACK                  BIT(30)
+#define SIFIVE_TMC_PG_DIS_REQ                  BIT(29)
+#define SIFIVE_TMC_PG_DIS_ACK                  BIT(28)
+#define SIFIVE_TMC_PG_PMC_ENA_ERR              BIT(17)
+#define SIFIVE_TMC_PG_PMC_DENY                 BIT(16)
+#define SIFIVE_TMC_PG_BUS_ERR                  BIT(15)
+#define SIFIVE_TMC_PG_MASTNOTQ                 BIT(14)
+#define SIFIVE_TMC_PG_WARM_RESET               BIT(1)
+#define SIFIVE_TMC_PG_ENARSP                   (SIFIVE_TMC_PG_PMC_ENA_ERR | \
+                                                SIFIVE_TMC_PG_PMC_DENY | \
+                                                SIFIVE_TMC_PG_BUS_ERR | \
+                                                SIFIVE_TMC_PG_MASTNOTQ)
+
+/* TMC.RESUMEPC */
+#define SIFIVE_TMC_RESUMEPC_LO                 0x10
+#define SIFIVE_TMC_RESUMEPC_HI                 0x14
+
+/* TMC.WAKEMASK */
+#define SIFIVE_TMC_WAKE_MASK_OFF               0x20
+#define SIFIVE_TMC_WAKE_MASK_WREQ              BIT(31)
+#define SIFIVE_TMC_WAKE_MASK_ACK               BIT(30)
+
+static void sifive_tmc0_set_resumepc(physical_addr_t addr)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+
+       writel((u32)addr, (void *)(tmc0->reg + SIFIVE_TMC_RESUMEPC_LO));
+#if __riscv_xlen > 32
+       writel((u32)(addr >> 32), (void *)(tmc0->reg + SIFIVE_TMC_RESUMEPC_HI));
+#endif
+}
+
+static u32 sifive_tmc0_set_pgprep_enareq(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PGPREP_OFF;
+       u32 v = readl((void *)reg);
+
+       writel(v | SIFIVE_TMC_PGPREP_ENA_REQ, (void *)reg);
+       while (!(readl((void *)reg) & SIFIVE_TMC_PGPREP_ENA_ACK));
+
+       v = readl((void *)reg);
+       return v & SIFIVE_TMC_PGPREP_INTERNAL_ABORT;
+}
+
+static void sifive_tmc0_set_pgprep_disreq(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PGPREP_OFF;
+       u32 v = readl((void *)reg);
+
+       writel(v | SIFIVE_TMC_PGPREP_DIS_REQ, (void *)reg);
+       while (!(readl((void *)reg) & SIFIVE_TMC_PGPREP_DIS_ACK));
+}
+
+static u32 sifive_tmc0_get_pgprep_enarsp(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PGPREP_OFF;
+       u32 v = readl((void *)reg);
+
+       return v & SIFIVE_TMC_PGPREP_ENARSP;
+}
+
+static void sifive_tmc0_set_pg_enareq(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PG_OFF;
+       u32 v = readl((void *)reg);
+
+       writel(v | SIFIVE_TMC_PG_ENA_REQ, (void *)reg);
+}
+
+static int sifive_tmc0_prep(void)
+{
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+       u32 rc;
+
+       if (!tmc0_ptr_get(scratch))
+               return SBI_ENODEV;
+
+       rc = sifive_tmc0_set_pgprep_enareq();
+       if (rc) {
+               sbi_printf("TMC0 error: Internal Abort (Wake detect)\n");
+               goto fail;
+       }
+
+       rc = sifive_tmc0_get_pgprep_enarsp();
+       if (rc) {
+               sifive_tmc0_set_pgprep_disreq();
+               sbi_printf("TMC0 error: error response code: 0x%x\n", rc);
+               goto fail;
+       }
+
+       sifive_tmc0_set_resumepc(scratch->warmboot_addr);
+
+       return SBI_OK;
+
+fail:
+       return SBI_EFAIL;
+}
+
+static int sifive_tmc0_enter(void)
+{
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+       u32 rc;
+
+       /* Flush cache and check if there is wake detect or bus error */
+       if (fdt_cmo_private_flc_flush_all() &&
+           sbi_hart_has_extension(scratch, SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1))
+               sifive_cflush();
+
+       rc = sifive_tmc0_get_pgprep_enarsp();
+       if (rc) {
+               sbi_printf("TMC0 error: error response code: 0x%x\n", rc);
+               goto fail;
+       }
+
+       if (sbi_hart_has_extension(scratch, SBI_HART_EXT_XSIFIVE_CEASE)) {
+               sifive_tmc0_set_pg_enareq();
+               while (1)
+                       sifive_cease();
+       }
+
+       rc = SBI_ENOTSUPP;
+fail:
+       sifive_tmc0_set_pgprep_disreq();
+       return rc;
+}
+
+static int sifive_tmc0_tile_pg(void)
+{
+       int rc;
+
+       rc = sifive_tmc0_prep();
+       if (rc)
+               return rc;
+
+       return sifive_tmc0_enter();
+}
+
+static struct sifive_tmc0 *sifive_tmc0_find(u32 id)
+{
+       struct sifive_tmc0 *tmc0;
+
+       sbi_list_for_each_entry(tmc0, &tmc0_list, node) {
+               if (tmc0->id == id)
+                       return tmc0;
+       }
+
+       return NULL;
+}
+
+static int sifive_tmc0_add(struct sifive_tmc0 *tmc0)
+{
+       if (!tmc0)
+               return SBI_ENODEV;
+
+       if (sifive_tmc0_find(tmc0->id))
+               return SBI_EALREADY;
+
+       sbi_list_add(&tmc0->node, &tmc0_list);
+
+       return SBI_OK;
+}
+
+static int sifive_tmc0_probe(const void *fdt, int nodeoff, const struct fdt_match *match)
+{
+       struct sifive_tmc0 *tmc0;
+       int rc;
+       u64 addr;
+
+       tmc0 = sbi_zalloc(sizeof(*tmc0));
+       if (!tmc0)
+               return SBI_ENOMEM;
+
+       rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
+       if (rc)
+               goto free_tmc0;
+
+       tmc0->reg = (unsigned long)addr;
+       tmc0->id = nodeoff;
+
+       rc = sifive_tmc0_add(tmc0);
+       if (rc)
+               goto free_tmc0;
+
+       return SBI_OK;
+
+free_tmc0:
+       sbi_free(tmc0);
+       return rc;
+}
+
+static const struct fdt_match sifive_tmc0_match[] = {
+       { .compatible = "sifive,tmc0" },
+       { },
+};
+
+const struct fdt_driver fdt_hsm_sifive_tmc0 = {
+       .match_table = sifive_tmc0_match,
+       .init = sifive_tmc0_probe,
+};
+
+static const struct fdt_driver *const fdt_hsm_sifive_tmc0_drivers[] = {
+       &fdt_hsm_sifive_tmc0,
+       NULL
+};
+
+static int fdt_sifive_tmc0_get(const void *fdt, int noff, struct sifive_tmc0 **out)
+{
+       struct sifive_tmc0 *tmc0;
+       int rc;
+
+       noff = fdt_node_offset_by_phandle(fdt, noff);
+       if (noff < 0)
+               return noff;
+
+       rc = fdt_driver_init_by_offset(fdt, noff, fdt_hsm_sifive_tmc0_drivers);
+       if (rc)
+               return rc;
+
+       tmc0 = sifive_tmc0_find(noff);
+       if (!tmc0)
+               return SBI_EFAIL;
+
+       if (out)
+               *out = tmc0;
+
+       return SBI_OK;
+}
+
+static int fdt_sifive_tmc0_cold_init(const void *fdt)
+{
+       struct sbi_scratch *scratch;
+       struct sifive_tmc0 *tmc0;
+       const fdt32_t *val;
+       int cpus_off, cpu_off, rc;
+       u32 hartid;
+
+       cpus_off = fdt_path_offset(fdt, "/cpus");
+       if (cpus_off < 0)
+               return SBI_ENOENT;
+
+       fdt_for_each_subnode(cpu_off, fdt, cpus_off) {
+               rc = fdt_parse_hart_id(fdt, cpu_off, &hartid);
+               if (rc)
+                       continue;
+
+               val = fdt_getprop(fdt, cpu_off, "power-domains", NULL);
+               if (!val)
+                       return SBI_ENOENT;
+
+               rc = fdt_sifive_tmc0_get(fdt, fdt32_to_cpu(val[0]), &tmc0);
+               if (rc)
+                       return rc;
+
+               scratch = sbi_hartid_to_scratch(hartid);
+               if (!scratch)
+                       continue;
+
+               tmc0_ptr_set(scratch, tmc0);
+       }
+
+       return SBI_OK;
+}
+
+static int sifive_tmc0_start(u32 hartid, ulong saddr)
+{
+       struct sbi_ipi_device *clint = aclint_mswi_get();
+
+       /*
+        * In system suspend, the IMSIC will be reset in SiFive platform so
+        * we use the CLINT IPI as the wake event.
+        */
+       if (clint && clint->ipi_send)
+               clint->ipi_send(sbi_hartid_to_hartindex(hartid));
+       else

lib/utils/hsm/fdt_hsm_sifive_tmc0.c line 347

Quoted 10 lines
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +10 -0
+               sbi_ipi_raw_send(sbi_hartid_to_hartindex(hartid));
+
+       return SBI_OK;
+}
+
+static int sifive_tmc0_stop(void)
+{
+       unsigned long mie = csr_read(CSR_MIE);
+       int rc;
+       // Set IPI as wake up source

Please don't use c++ style "//" comments.

Quoted 23 lines
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +23 -0
+       csr_set(CSR_MIE, MIP_MEIP | MIP_MSIP);
+
+       rc = sifive_tmc0_tile_pg();
+       if (rc) {
+               csr_write(CSR_MIE, mie);
+               return rc;
+       }
+
+       return SBI_OK;
+}
+
+static struct sbi_hsm_device tmc0_hsm_dev = {
+       .name = "SiFive TMC0",
+       .hart_start = sifive_tmc0_start,
+       .hart_stop = sifive_tmc0_stop,
+};
+
+int fdt_hsm_sifive_tmc0_cold_init(void)
+{
+       int rc;
+
+       tmc0_offset = sbi_scratch_alloc_type_offset(struct sifive_tmc0 *);
+       if (!tmc0_offset)

lib/utils/hsm/fdt_hsm_sifive_tmc0.c line 380

Quoted 10 lines
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +10 -0
+               return SBI_ENOMEM;
+
+       rc = fdt_sifive_tmc0_cold_init(fdt_get_address());
+       if (rc)
+               return rc;
+
+       sbi_hsm_set_device(&tmc0_hsm_dev);
+
+       return SBI_OK;
+}

Why can't the SiFive TMC driver probe as fdt_early_driver ?

Quoted 124 lines
lib/utils/hsm/objects.mk +2 -0
index c13d81f7..04f4a208 100644
@@ -9,3 +9,5 @@
 
 carray-fdt_early_drivers-$(CONFIG_FDT_HSM_RPMI) += fdt_hsm_rpmi
 libsbiutils-objs-$(CONFIG_FDT_HSM_RPMI) += hsm/fdt_hsm_rpmi.o
+
+libsbiutils-objs-$(CONFIG_FDT_HSM_SIFIVE_TMC0) += hsm/fdt_hsm_sifive_tmc0.o
platform/generic/Kconfig +5 -0
index a24d6ab2..20c25a83 100644
@@ -43,6 +43,11 @@ config PLATFORM_RENESAS_RZFIVE
        select ANDES_PMU
        default n
 
+config PLATFORM_SIFIVE_DEV
+       bool "SiFive development platform support"
+       depends on FDT_CACHE
+       default n
+
 config PLATFORM_SIFIVE_FU540
        bool "SiFive FU540 support"
        default n
platform/generic/configs/defconfig +2 -0
index 6b219cfb..837acd79 100644
@@ -1,6 +1,7 @@
 CONFIG_PLATFORM_ALLWINNER_D1=y
 CONFIG_PLATFORM_ANDES_AE350=y
 CONFIG_PLATFORM_RENESAS_RZFIVE=y
+CONFIG_PLATFORM_SIFIVE_DEV=y
 CONFIG_PLATFORM_SIFIVE_FU540=y
 CONFIG_PLATFORM_SIFIVE_FU740=y
 CONFIG_PLATFORM_SOPHGO_SG2042=y
@@ -17,6 +18,7 @@ CONFIG_FDT_GPIO_SIFIVE=y
 CONFIG_FDT_GPIO_STARFIVE=y
 CONFIG_FDT_HSM=y
 CONFIG_FDT_HSM_RPMI=y
+CONFIG_FDT_HSM_SIFIVE_TMC0=y
 CONFIG_FDT_I2C=y
 CONFIG_FDT_I2C_SIFIVE=y
 CONFIG_FDT_I2C_DW=y
platform/generic/sifive/objects.mk +3 -0
index d75e444a..d32e1273 100644
@@ -2,6 +2,9 @@
 # SPDX-License-Identifier: BSD-2-Clause
 #
 
+carray-platform_override_modules-$(CONFIG_PLATFORM_SIFIVE_DEV) += sifive_dev_platform
+platform-objs-$(CONFIG_PLATFORM_SIFIVE_DEV) += sifive/sifive_dev_platform.o
+
 carray-platform_override_modules-$(CONFIG_PLATFORM_SIFIVE_FU540) += sifive_fu540
 platform-objs-$(CONFIG_PLATFORM_SIFIVE_FU540) += sifive/fu540.o
 
platform/generic/sifive/sifive_dev_platform.c new file +57 -1
new file mode 100644
index 00000000..48774492
@@ -0,0 +1,57 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#include <platform_override.h>
+#include <sbi/sbi_ipi.h>
+#include <sbi_utils/cache/fdt_cmo_helper.h>
+#include <sbi_utils/hsm/fdt_hsm_sifive_tmc0.h>
+#include <sbi_utils/ipi/aclint_mswi.h>
+
+static int sifive_early_init(bool cold_boot)
+{
+       int rc;
+
+       rc = generic_early_init(cold_boot);
+       if (rc)
+               return rc;
+
+       if (cold_boot) {
+               rc = fdt_hsm_sifive_tmc0_cold_init();
+               if (rc)
+                       return rc;
+       }
+
+       return 0;
+}
+
+static int sifive_final_init(bool cold_boot)
+{
+       struct sbi_ipi_device *clint = aclint_mswi_get();
+
+       if (clint && clint->ipi_clear)
+               clint->ipi_clear();
+
+       return generic_final_init(cold_boot);
+}
+
+static int sifive_platform_init(const void *fdt, int nodeoff,
+                               const struct fdt_match *match)
+{
+       generic_platform_ops.early_init = sifive_early_init;
+       generic_platform_ops.final_init = sifive_final_init;
+
+       return 0;
+}
+
+static const struct fdt_match sifive_dev_platform_match[] = {
+       { .compatible = "sifive-dev" },
+       { },
+};
+
+const struct fdt_driver sifive_dev_platform = {
+       .match_table = sifive_dev_platform_match,
+       .init = sifive_platform_init,
+};
--
Quoted 2 lines

2.17.1

Regards,
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#13 lore raw

Re: [PATCH v4 09/11] platform: sifive: Add SiFive TMC0 driver

Quoted 440 lines · Anup Patel
Quoted 438 lines · Nick Hu

The SiFive TMC0 controls the tile power domains on SiFive platform. The CPU enters the low power state via the CEASE instruction after configuring the TMC0. Any devices that inside the tile power domain will be power gated, including the private cache. Therefore flushing the private cache before entering the low power state.

---
 include/sbi_utils/hsm/fdt_hsm_sifive_inst.h   |  20 +
 include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h   |  16 +
 lib/utils/hsm/Kconfig                         |   5 +
 lib/utils/hsm/fdt_hsm_sifive_tmc0.c           | 380 ++++++++++++++++++
 lib/utils/hsm/objects.mk                      |   2 +
 platform/generic/Kconfig                      |   5 +
 platform/generic/configs/defconfig            |   2 +
 platform/generic/sifive/objects.mk            |   3 +
 platform/generic/sifive/sifive_dev_platform.c |  57 +++
 9 files changed, 490 insertions(+)
 create mode 100644 include/sbi_utils/hsm/fdt_hsm_sifive_inst.h
 create mode 100644 include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h
 create mode 100644 lib/utils/hsm/fdt_hsm_sifive_tmc0.c
 create mode 100644 platform/generic/sifive/sifive_dev_platform.c
include/sbi_utils/hsm/fdt_hsm_sifive_inst.h new file +20 -0
new file mode 100644
index 00000000..de8aba19
@@ -0,0 +1,20 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __FDT_HSM_SIFIVE_INST_H__
+#define __FDT_HSM_SIFIVE_INST_H__
+
+static inline void sifive_cease(void)
+{
+       __asm__ __volatile__(".insn 0x30500073" ::: "memory");
+}
+
+static inline void sifive_cflush(void)
+{
+       __asm__ __volatile__(".insn 0xfc000073" ::: "memory");
+}
+
+#endif
include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h new file +16 -0
new file mode 100644
index 00000000..4ba048b9
@@ -0,0 +1,16 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __FDT_HSM_SIFIVE_TMC0_H__
+#define __FDT_HSM_SIFIVE_TMC0_H__
+
+#ifdef CONFIG_FDT_HSM_SIFIVE_TMC0
+int fdt_hsm_sifive_tmc0_cold_init(void);
+#else
+static inline int fdt_hsm_sifive_tmc0_cold_init(void) { return 0; }
+#endif
+
+#endif
lib/utils/hsm/Kconfig +5 -0
index 1ad7958f..3e6f8909 100644
@@ -14,6 +14,11 @@ config FDT_HSM_RPMI
        depends on FDT_MAILBOX && RPMI_MAILBOX
        default n
 
+config FDT_HSM_SIFIVE_TMC0
+       bool "FDT SiFive TMC v0 driver"
+       depends on PLATFORM_SIFIVE_DEV
+       default n
+
 endif
 
 endmenu
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +341 -0
new file mode 100644
index 00000000..83f86c88
@@ -0,0 +1,380 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive
+ */
+
+#include <libfdt.h>
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_io.h>
+#include <sbi/sbi_bitops.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_hart.h>
+#include <sbi/sbi_heap.h>
+#include <sbi/sbi_hsm.h>
+#include <sbi/sbi_ipi.h>
+#include <sbi_utils/cache/fdt_cmo_helper.h>
+#include <sbi_utils/fdt/fdt_driver.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/hsm/fdt_hsm_sifive_inst.h>
+#include <sbi_utils/hsm/fdt_hsm_sifive_tmc0.h>
+#include <sbi_utils/ipi/aclint_mswi.h>
+
+struct sifive_tmc0 {
+       unsigned long reg;
+       struct sbi_dlist node;
+       u32 id;
+};
+
+static SBI_LIST_HEAD(tmc0_list);
+static unsigned long tmc0_offset;
+
+#define tmc0_ptr_get(__scratch)                                \
+       sbi_scratch_read_type((__scratch), struct sifive_tmc0 *, tmc0_offset)
+
+#define tmc0_ptr_set(__scratch, __tmc0)                        \
+       sbi_scratch_write_type((__scratch), struct sifive_tmc0 *, tmc0_offset, (__tmc0))
+
+/* TMC.PGPREP */
+#define SIFIVE_TMC_PGPREP_OFF                  0x0
+#define SIFIVE_TMC_PGPREP_ENA_REQ              BIT(31)
+#define SIFIVE_TMC_PGPREP_ENA_ACK              BIT(30)
+#define SIFIVE_TMC_PGPREP_DIS_REQ              BIT(29)
+#define SIFIVE_TMC_PGPREP_DIS_ACK              BIT(28)
+#define SIFIVE_TMC_PGPREP_CLFPNOTQ             BIT(18)
+#define SIFIVE_TMC_PGPREP_PMCENAERR            BIT(17)
+#define SIFIVE_TMC_PGPREP_PMCDENY              BIT(16)
+#define SIFIVE_TMC_PGPREP_BUSERR               BIT(15)
+#define SIFIVE_TMC_PGPREP_WAKE_DETECT          BIT(12)
+#define SIFIVE_TMC_PGPREP_INTERNAL_ABORT       BIT(2)
+#define SIFIVE_TMC_PGPREP_ENARSP               (SIFIVE_TMC_PGPREP_CLFPNOTQ | \
+                                                SIFIVE_TMC_PGPREP_PMCENAERR | \
+                                                SIFIVE_TMC_PGPREP_PMCDENY | \
+                                                SIFIVE_TMC_PGPREP_BUSERR | \
+                                                SIFIVE_TMC_PGPREP_WAKE_DETECT)
+
+/* TMC.PG */
+#define SIFIVE_TMC_PG_OFF                      0x4
+#define SIFIVE_TMC_PG_ENA_REQ                  BIT(31)
+#define SIFIVE_TMC_PG_ENA_ACK                  BIT(30)
+#define SIFIVE_TMC_PG_DIS_REQ                  BIT(29)
+#define SIFIVE_TMC_PG_DIS_ACK                  BIT(28)
+#define SIFIVE_TMC_PG_PMC_ENA_ERR              BIT(17)
+#define SIFIVE_TMC_PG_PMC_DENY                 BIT(16)
+#define SIFIVE_TMC_PG_BUS_ERR                  BIT(15)
+#define SIFIVE_TMC_PG_MASTNOTQ                 BIT(14)
+#define SIFIVE_TMC_PG_WARM_RESET               BIT(1)
+#define SIFIVE_TMC_PG_ENARSP                   (SIFIVE_TMC_PG_PMC_ENA_ERR | \
+                                                SIFIVE_TMC_PG_PMC_DENY | \
+                                                SIFIVE_TMC_PG_BUS_ERR | \
+                                                SIFIVE_TMC_PG_MASTNOTQ)
+
+/* TMC.RESUMEPC */
+#define SIFIVE_TMC_RESUMEPC_LO                 0x10
+#define SIFIVE_TMC_RESUMEPC_HI                 0x14
+
+/* TMC.WAKEMASK */
+#define SIFIVE_TMC_WAKE_MASK_OFF               0x20
+#define SIFIVE_TMC_WAKE_MASK_WREQ              BIT(31)
+#define SIFIVE_TMC_WAKE_MASK_ACK               BIT(30)
+
+static void sifive_tmc0_set_resumepc(physical_addr_t addr)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+
+       writel((u32)addr, (void *)(tmc0->reg + SIFIVE_TMC_RESUMEPC_LO));
+#if __riscv_xlen > 32
+       writel((u32)(addr >> 32), (void *)(tmc0->reg + SIFIVE_TMC_RESUMEPC_HI));
+#endif
+}
+
+static u32 sifive_tmc0_set_pgprep_enareq(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PGPREP_OFF;
+       u32 v = readl((void *)reg);
+
+       writel(v | SIFIVE_TMC_PGPREP_ENA_REQ, (void *)reg);
+       while (!(readl((void *)reg) & SIFIVE_TMC_PGPREP_ENA_ACK));
+
+       v = readl((void *)reg);
+       return v & SIFIVE_TMC_PGPREP_INTERNAL_ABORT;
+}
+
+static void sifive_tmc0_set_pgprep_disreq(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PGPREP_OFF;
+       u32 v = readl((void *)reg);
+
+       writel(v | SIFIVE_TMC_PGPREP_DIS_REQ, (void *)reg);
+       while (!(readl((void *)reg) & SIFIVE_TMC_PGPREP_DIS_ACK));
+}
+
+static u32 sifive_tmc0_get_pgprep_enarsp(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PGPREP_OFF;
+       u32 v = readl((void *)reg);
+
+       return v & SIFIVE_TMC_PGPREP_ENARSP;
+}
+
+static void sifive_tmc0_set_pg_enareq(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PG_OFF;
+       u32 v = readl((void *)reg);
+
+       writel(v | SIFIVE_TMC_PG_ENA_REQ, (void *)reg);
+}
+
+static int sifive_tmc0_prep(void)
+{
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+       u32 rc;
+
+       if (!tmc0_ptr_get(scratch))
+               return SBI_ENODEV;
+
+       rc = sifive_tmc0_set_pgprep_enareq();
+       if (rc) {
+               sbi_printf("TMC0 error: Internal Abort (Wake detect)\n");
+               goto fail;
+       }
+
+       rc = sifive_tmc0_get_pgprep_enarsp();
+       if (rc) {
+               sifive_tmc0_set_pgprep_disreq();
+               sbi_printf("TMC0 error: error response code: 0x%x\n", rc);
+               goto fail;
+       }
+
+       sifive_tmc0_set_resumepc(scratch->warmboot_addr);
+
+       return SBI_OK;
+
+fail:
+       return SBI_EFAIL;
+}
+
+static int sifive_tmc0_enter(void)
+{
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+       u32 rc;
+
+       /* Flush cache and check if there is wake detect or bus error */
+       if (fdt_cmo_private_flc_flush_all() &&
+           sbi_hart_has_extension(scratch, SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1))
+               sifive_cflush();
+
+       rc = sifive_tmc0_get_pgprep_enarsp();
+       if (rc) {
+               sbi_printf("TMC0 error: error response code: 0x%x\n", rc);
+               goto fail;
+       }
+
+       if (sbi_hart_has_extension(scratch, SBI_HART_EXT_XSIFIVE_CEASE)) {
+               sifive_tmc0_set_pg_enareq();
+               while (1)
+                       sifive_cease();
+       }
+
+       rc = SBI_ENOTSUPP;
+fail:
+       sifive_tmc0_set_pgprep_disreq();
+       return rc;
+}
+
+static int sifive_tmc0_tile_pg(void)
+{
+       int rc;
+
+       rc = sifive_tmc0_prep();
+       if (rc)
+               return rc;
+
+       return sifive_tmc0_enter();
+}
+
+static struct sifive_tmc0 *sifive_tmc0_find(u32 id)
+{
+       struct sifive_tmc0 *tmc0;
+
+       sbi_list_for_each_entry(tmc0, &tmc0_list, node) {
+               if (tmc0->id == id)
+                       return tmc0;
+       }
+
+       return NULL;
+}
+
+static int sifive_tmc0_add(struct sifive_tmc0 *tmc0)
+{
+       if (!tmc0)
+               return SBI_ENODEV;
+
+       if (sifive_tmc0_find(tmc0->id))
+               return SBI_EALREADY;
+
+       sbi_list_add(&tmc0->node, &tmc0_list);
+
+       return SBI_OK;
+}
+
+static int sifive_tmc0_probe(const void *fdt, int nodeoff, const struct fdt_match *match)
+{
+       struct sifive_tmc0 *tmc0;
+       int rc;
+       u64 addr;
+
+       tmc0 = sbi_zalloc(sizeof(*tmc0));
+       if (!tmc0)
+               return SBI_ENOMEM;
+
+       rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
+       if (rc)
+               goto free_tmc0;
+
+       tmc0->reg = (unsigned long)addr;
+       tmc0->id = nodeoff;
+
+       rc = sifive_tmc0_add(tmc0);
+       if (rc)
+               goto free_tmc0;
+
+       return SBI_OK;
+
+free_tmc0:
+       sbi_free(tmc0);
+       return rc;
+}
+
+static const struct fdt_match sifive_tmc0_match[] = {
+       { .compatible = "sifive,tmc0" },
+       { },
+};
+
+const struct fdt_driver fdt_hsm_sifive_tmc0 = {
+       .match_table = sifive_tmc0_match,
+       .init = sifive_tmc0_probe,
+};
+
+static const struct fdt_driver *const fdt_hsm_sifive_tmc0_drivers[] = {
+       &fdt_hsm_sifive_tmc0,
+       NULL
+};
+
+static int fdt_sifive_tmc0_get(const void *fdt, int noff, struct sifive_tmc0 **out)
+{
+       struct sifive_tmc0 *tmc0;
+       int rc;
+
+       noff = fdt_node_offset_by_phandle(fdt, noff);
+       if (noff < 0)
+               return noff;
+
+       rc = fdt_driver_init_by_offset(fdt, noff, fdt_hsm_sifive_tmc0_drivers);
+       if (rc)
+               return rc;
+
+       tmc0 = sifive_tmc0_find(noff);
+       if (!tmc0)
+               return SBI_EFAIL;
+
+       if (out)
+               *out = tmc0;
+
+       return SBI_OK;
+}
+
+static int fdt_sifive_tmc0_cold_init(const void *fdt)
+{
+       struct sbi_scratch *scratch;
+       struct sifive_tmc0 *tmc0;
+       const fdt32_t *val;
+       int cpus_off, cpu_off, rc;
+       u32 hartid;
+
+       cpus_off = fdt_path_offset(fdt, "/cpus");
+       if (cpus_off < 0)
+               return SBI_ENOENT;
+
+       fdt_for_each_subnode(cpu_off, fdt, cpus_off) {
+               rc = fdt_parse_hart_id(fdt, cpu_off, &hartid);
+               if (rc)
+                       continue;
+
+               val = fdt_getprop(fdt, cpu_off, "power-domains", NULL);
+               if (!val)
+                       return SBI_ENOENT;
+
+               rc = fdt_sifive_tmc0_get(fdt, fdt32_to_cpu(val[0]), &tmc0);
+               if (rc)
+                       return rc;
+
+               scratch = sbi_hartid_to_scratch(hartid);
+               if (!scratch)
+                       continue;
+
+               tmc0_ptr_set(scratch, tmc0);
+       }
+
+       return SBI_OK;
+}
+
+static int sifive_tmc0_start(u32 hartid, ulong saddr)
+{
+       struct sbi_ipi_device *clint = aclint_mswi_get();
+
+       /*
+        * In system suspend, the IMSIC will be reset in SiFive platform so
+        * we use the CLINT IPI as the wake event.
+        */
+       if (clint && clint->ipi_send)
+               clint->ipi_send(sbi_hartid_to_hartindex(hartid));
+       else
+               sbi_ipi_raw_send(sbi_hartid_to_hartindex(hartid));
+
+       return SBI_OK;
+}
Quoted 9 lines

lib/utils/hsm/fdt_hsm_sifive_tmc0.c line 347

Quoted 6 lines
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +6 -0
+
+static int sifive_tmc0_stop(void)
+{
+       unsigned long mie = csr_read(CSR_MIE);
+       int rc;
+       // Set IPI as wake up source

Please don't use c++ style "//" comments.

Will fix it in the next version

Quoted 27 lines
Quoted 27 lines
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +27 -0
+       csr_set(CSR_MIE, MIP_MEIP | MIP_MSIP);
+
+       rc = sifive_tmc0_tile_pg();
+       if (rc) {
+               csr_write(CSR_MIE, mie);
+               return rc;
+       }
+
+       return SBI_OK;
+}
+
+static struct sbi_hsm_device tmc0_hsm_dev = {
+       .name = "SiFive TMC0",
+       .hart_start = sifive_tmc0_start,
+       .hart_stop = sifive_tmc0_stop,
+};
+
+int fdt_hsm_sifive_tmc0_cold_init(void)
+{
+       int rc;
+
+       tmc0_offset = sbi_scratch_alloc_type_offset(struct sifive_tmc0 *);
+       if (!tmc0_offset)
+               return SBI_ENOMEM;
+
+       rc = fdt_sifive_tmc0_cold_init(fdt_get_address());
+       if (rc)
Quoted 9 lines

lib/utils/hsm/fdt_hsm_sifive_tmc0.c line 380

Quoted 6 lines
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +6 -0
+               return rc;
+
+       sbi_hsm_set_device(&tmc0_hsm_dev);
+
+       return SBI_OK;
+}

Why can't the SiFive TMC driver probe as fdt_early_driver ?

Yes, it can. This requires updating the logic for binding the TMC to the corresponding CPU.
In the current implementation, we iterate over each CPU DT node to probe the TMC driver via its power-domains property and then bind the TMC to that CPU.

If the TMC driver is registered as an fdt_early_driver, we instead need to iterate over each CPU node during every TMC driver probe and bind the TMC to the CPU whose power-domains property references the current TMC node.

Quoted 129 lines
Quoted 124 lines
lib/utils/hsm/objects.mk +2 -0
index c13d81f7..04f4a208 100644
@@ -9,3 +9,5 @@
 
 carray-fdt_early_drivers-$(CONFIG_FDT_HSM_RPMI) += fdt_hsm_rpmi
 libsbiutils-objs-$(CONFIG_FDT_HSM_RPMI) += hsm/fdt_hsm_rpmi.o
+
+libsbiutils-objs-$(CONFIG_FDT_HSM_SIFIVE_TMC0) += hsm/fdt_hsm_sifive_tmc0.o
platform/generic/Kconfig +5 -0
index a24d6ab2..20c25a83 100644
@@ -43,6 +43,11 @@ config PLATFORM_RENESAS_RZFIVE
        select ANDES_PMU
        default n
 
+config PLATFORM_SIFIVE_DEV
+       bool "SiFive development platform support"
+       depends on FDT_CACHE
+       default n
+
 config PLATFORM_SIFIVE_FU540
        bool "SiFive FU540 support"
        default n
platform/generic/configs/defconfig +2 -0
index 6b219cfb..837acd79 100644
@@ -1,6 +1,7 @@
 CONFIG_PLATFORM_ALLWINNER_D1=y
 CONFIG_PLATFORM_ANDES_AE350=y
 CONFIG_PLATFORM_RENESAS_RZFIVE=y
+CONFIG_PLATFORM_SIFIVE_DEV=y
 CONFIG_PLATFORM_SIFIVE_FU540=y
 CONFIG_PLATFORM_SIFIVE_FU740=y
 CONFIG_PLATFORM_SOPHGO_SG2042=y
@@ -17,6 +18,7 @@ CONFIG_FDT_GPIO_SIFIVE=y
 CONFIG_FDT_GPIO_STARFIVE=y
 CONFIG_FDT_HSM=y
 CONFIG_FDT_HSM_RPMI=y
+CONFIG_FDT_HSM_SIFIVE_TMC0=y
 CONFIG_FDT_I2C=y
 CONFIG_FDT_I2C_SIFIVE=y
 CONFIG_FDT_I2C_DW=y
platform/generic/sifive/objects.mk +3 -0
index d75e444a..d32e1273 100644
@@ -2,6 +2,9 @@
 # SPDX-License-Identifier: BSD-2-Clause
 #
 
+carray-platform_override_modules-$(CONFIG_PLATFORM_SIFIVE_DEV) += sifive_dev_platform
+platform-objs-$(CONFIG_PLATFORM_SIFIVE_DEV) += sifive/sifive_dev_platform.o
+
 carray-platform_override_modules-$(CONFIG_PLATFORM_SIFIVE_FU540) += sifive_fu540
 platform-objs-$(CONFIG_PLATFORM_SIFIVE_FU540) += sifive/fu540.o
 
platform/generic/sifive/sifive_dev_platform.c new file +57 -1
new file mode 100644
index 00000000..48774492
@@ -0,0 +1,57 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#include <platform_override.h>
+#include <sbi/sbi_ipi.h>
+#include <sbi_utils/cache/fdt_cmo_helper.h>
+#include <sbi_utils/hsm/fdt_hsm_sifive_tmc0.h>
+#include <sbi_utils/ipi/aclint_mswi.h>
+
+static int sifive_early_init(bool cold_boot)
+{
+       int rc;
+
+       rc = generic_early_init(cold_boot);
+       if (rc)
+               return rc;
+
+       if (cold_boot) {
+               rc = fdt_hsm_sifive_tmc0_cold_init();
+               if (rc)
+                       return rc;
+       }
+
+       return 0;
+}
+
+static int sifive_final_init(bool cold_boot)
+{
+       struct sbi_ipi_device *clint = aclint_mswi_get();
+
+       if (clint && clint->ipi_clear)
+               clint->ipi_clear();
+
+       return generic_final_init(cold_boot);
+}
+
+static int sifive_platform_init(const void *fdt, int nodeoff,
+                               const struct fdt_match *match)
+{
+       generic_platform_ops.early_init = sifive_early_init;
+       generic_platform_ops.final_init = sifive_final_init;
+
+       return 0;
+}
+
+static const struct fdt_match sifive_dev_platform_match[] = {
+       { .compatible = "sifive-dev" },
+       { },
+};
+
+const struct fdt_driver sifive_dev_platform = {
+       .match_table = sifive_dev_platform_match,
+       .init = sifive_platform_init,
+};
--
Quoted 2 lines

2.17.1

Regards,
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#14 lore raw

Re: [PATCH v4 09/11] platform: sifive: Add SiFive TMC0 driver

Quoted 495 lines · Nick Hu
Quoted 440 lines · Anup Patel
Quoted 438 lines · Nick Hu

The SiFive TMC0 controls the tile power domains on SiFive platform. The CPU enters the low power state via the CEASE instruction after configuring the TMC0. Any devices that inside the tile power domain will be power gated, including the private cache. Therefore flushing the private cache before entering the low power state.

---
 include/sbi_utils/hsm/fdt_hsm_sifive_inst.h   |  20 +
 include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h   |  16 +
 lib/utils/hsm/Kconfig                         |   5 +
 lib/utils/hsm/fdt_hsm_sifive_tmc0.c           | 380 ++++++++++++++++++
 lib/utils/hsm/objects.mk                      |   2 +
 platform/generic/Kconfig                      |   5 +
 platform/generic/configs/defconfig            |   2 +
 platform/generic/sifive/objects.mk            |   3 +
 platform/generic/sifive/sifive_dev_platform.c |  57 +++
 9 files changed, 490 insertions(+)
 create mode 100644 include/sbi_utils/hsm/fdt_hsm_sifive_inst.h
 create mode 100644 include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h
 create mode 100644 lib/utils/hsm/fdt_hsm_sifive_tmc0.c
 create mode 100644 platform/generic/sifive/sifive_dev_platform.c
include/sbi_utils/hsm/fdt_hsm_sifive_inst.h new file +20 -0
new file mode 100644
index 00000000..de8aba19
@@ -0,0 +1,20 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __FDT_HSM_SIFIVE_INST_H__
+#define __FDT_HSM_SIFIVE_INST_H__
+
+static inline void sifive_cease(void)
+{
+       __asm__ __volatile__(".insn 0x30500073" ::: "memory");
+}
+
+static inline void sifive_cflush(void)
+{
+       __asm__ __volatile__(".insn 0xfc000073" ::: "memory");
+}
+
+#endif
include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h new file +16 -0
new file mode 100644
index 00000000..4ba048b9
@@ -0,0 +1,16 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __FDT_HSM_SIFIVE_TMC0_H__
+#define __FDT_HSM_SIFIVE_TMC0_H__
+
+#ifdef CONFIG_FDT_HSM_SIFIVE_TMC0
+int fdt_hsm_sifive_tmc0_cold_init(void);
+#else
+static inline int fdt_hsm_sifive_tmc0_cold_init(void) { return 0; }
+#endif
+
+#endif
lib/utils/hsm/Kconfig +5 -0
index 1ad7958f..3e6f8909 100644
@@ -14,6 +14,11 @@ config FDT_HSM_RPMI
        depends on FDT_MAILBOX && RPMI_MAILBOX
        default n
 
+config FDT_HSM_SIFIVE_TMC0
+       bool "FDT SiFive TMC v0 driver"
+       depends on PLATFORM_SIFIVE_DEV
+       default n
+
 endif
 
 endmenu
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +341 -0
new file mode 100644
index 00000000..83f86c88
@@ -0,0 +1,380 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive
+ */
+
+#include <libfdt.h>
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_io.h>
+#include <sbi/sbi_bitops.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_hart.h>
+#include <sbi/sbi_heap.h>
+#include <sbi/sbi_hsm.h>
+#include <sbi/sbi_ipi.h>
+#include <sbi_utils/cache/fdt_cmo_helper.h>
+#include <sbi_utils/fdt/fdt_driver.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/hsm/fdt_hsm_sifive_inst.h>
+#include <sbi_utils/hsm/fdt_hsm_sifive_tmc0.h>
+#include <sbi_utils/ipi/aclint_mswi.h>
+
+struct sifive_tmc0 {
+       unsigned long reg;
+       struct sbi_dlist node;
+       u32 id;
+};
+
+static SBI_LIST_HEAD(tmc0_list);
+static unsigned long tmc0_offset;
+
+#define tmc0_ptr_get(__scratch)                                \
+       sbi_scratch_read_type((__scratch), struct sifive_tmc0 *, tmc0_offset)
+
+#define tmc0_ptr_set(__scratch, __tmc0)                        \
+       sbi_scratch_write_type((__scratch), struct sifive_tmc0 *, tmc0_offset, (__tmc0))
+
+/* TMC.PGPREP */
+#define SIFIVE_TMC_PGPREP_OFF                  0x0
+#define SIFIVE_TMC_PGPREP_ENA_REQ              BIT(31)
+#define SIFIVE_TMC_PGPREP_ENA_ACK              BIT(30)
+#define SIFIVE_TMC_PGPREP_DIS_REQ              BIT(29)
+#define SIFIVE_TMC_PGPREP_DIS_ACK              BIT(28)
+#define SIFIVE_TMC_PGPREP_CLFPNOTQ             BIT(18)
+#define SIFIVE_TMC_PGPREP_PMCENAERR            BIT(17)
+#define SIFIVE_TMC_PGPREP_PMCDENY              BIT(16)
+#define SIFIVE_TMC_PGPREP_BUSERR               BIT(15)
+#define SIFIVE_TMC_PGPREP_WAKE_DETECT          BIT(12)
+#define SIFIVE_TMC_PGPREP_INTERNAL_ABORT       BIT(2)
+#define SIFIVE_TMC_PGPREP_ENARSP               (SIFIVE_TMC_PGPREP_CLFPNOTQ | \
+                                                SIFIVE_TMC_PGPREP_PMCENAERR | \
+                                                SIFIVE_TMC_PGPREP_PMCDENY | \
+                                                SIFIVE_TMC_PGPREP_BUSERR | \
+                                                SIFIVE_TMC_PGPREP_WAKE_DETECT)
+
+/* TMC.PG */
+#define SIFIVE_TMC_PG_OFF                      0x4
+#define SIFIVE_TMC_PG_ENA_REQ                  BIT(31)
+#define SIFIVE_TMC_PG_ENA_ACK                  BIT(30)
+#define SIFIVE_TMC_PG_DIS_REQ                  BIT(29)
+#define SIFIVE_TMC_PG_DIS_ACK                  BIT(28)
+#define SIFIVE_TMC_PG_PMC_ENA_ERR              BIT(17)
+#define SIFIVE_TMC_PG_PMC_DENY                 BIT(16)
+#define SIFIVE_TMC_PG_BUS_ERR                  BIT(15)
+#define SIFIVE_TMC_PG_MASTNOTQ                 BIT(14)
+#define SIFIVE_TMC_PG_WARM_RESET               BIT(1)
+#define SIFIVE_TMC_PG_ENARSP                   (SIFIVE_TMC_PG_PMC_ENA_ERR | \
+                                                SIFIVE_TMC_PG_PMC_DENY | \
+                                                SIFIVE_TMC_PG_BUS_ERR | \
+                                                SIFIVE_TMC_PG_MASTNOTQ)
+
+/* TMC.RESUMEPC */
+#define SIFIVE_TMC_RESUMEPC_LO                 0x10
+#define SIFIVE_TMC_RESUMEPC_HI                 0x14
+
+/* TMC.WAKEMASK */
+#define SIFIVE_TMC_WAKE_MASK_OFF               0x20
+#define SIFIVE_TMC_WAKE_MASK_WREQ              BIT(31)
+#define SIFIVE_TMC_WAKE_MASK_ACK               BIT(30)
+
+static void sifive_tmc0_set_resumepc(physical_addr_t addr)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+
+       writel((u32)addr, (void *)(tmc0->reg + SIFIVE_TMC_RESUMEPC_LO));
+#if __riscv_xlen > 32
+       writel((u32)(addr >> 32), (void *)(tmc0->reg + SIFIVE_TMC_RESUMEPC_HI));
+#endif
+}
+
+static u32 sifive_tmc0_set_pgprep_enareq(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PGPREP_OFF;
+       u32 v = readl((void *)reg);
+
+       writel(v | SIFIVE_TMC_PGPREP_ENA_REQ, (void *)reg);
+       while (!(readl((void *)reg) & SIFIVE_TMC_PGPREP_ENA_ACK));
+
+       v = readl((void *)reg);
+       return v & SIFIVE_TMC_PGPREP_INTERNAL_ABORT;
+}
+
+static void sifive_tmc0_set_pgprep_disreq(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PGPREP_OFF;
+       u32 v = readl((void *)reg);
+
+       writel(v | SIFIVE_TMC_PGPREP_DIS_REQ, (void *)reg);
+       while (!(readl((void *)reg) & SIFIVE_TMC_PGPREP_DIS_ACK));
+}
+
+static u32 sifive_tmc0_get_pgprep_enarsp(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PGPREP_OFF;
+       u32 v = readl((void *)reg);
+
+       return v & SIFIVE_TMC_PGPREP_ENARSP;
+}
+
+static void sifive_tmc0_set_pg_enareq(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PG_OFF;
+       u32 v = readl((void *)reg);
+
+       writel(v | SIFIVE_TMC_PG_ENA_REQ, (void *)reg);
+}
+
+static int sifive_tmc0_prep(void)
+{
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+       u32 rc;
+
+       if (!tmc0_ptr_get(scratch))
+               return SBI_ENODEV;
+
+       rc = sifive_tmc0_set_pgprep_enareq();
+       if (rc) {
+               sbi_printf("TMC0 error: Internal Abort (Wake detect)\n");
+               goto fail;
+       }
+
+       rc = sifive_tmc0_get_pgprep_enarsp();
+       if (rc) {
+               sifive_tmc0_set_pgprep_disreq();
+               sbi_printf("TMC0 error: error response code: 0x%x\n", rc);
+               goto fail;
+       }
+
+       sifive_tmc0_set_resumepc(scratch->warmboot_addr);
+
+       return SBI_OK;
+
+fail:
+       return SBI_EFAIL;
+}
+
+static int sifive_tmc0_enter(void)
+{
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+       u32 rc;
+
+       /* Flush cache and check if there is wake detect or bus error */
+       if (fdt_cmo_private_flc_flush_all() &&
+           sbi_hart_has_extension(scratch, SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1))
+               sifive_cflush();
+
+       rc = sifive_tmc0_get_pgprep_enarsp();
+       if (rc) {
+               sbi_printf("TMC0 error: error response code: 0x%x\n", rc);
+               goto fail;
+       }
+
+       if (sbi_hart_has_extension(scratch, SBI_HART_EXT_XSIFIVE_CEASE)) {
+               sifive_tmc0_set_pg_enareq();
+               while (1)
+                       sifive_cease();
+       }
+
+       rc = SBI_ENOTSUPP;
+fail:
+       sifive_tmc0_set_pgprep_disreq();
+       return rc;
+}
+
+static int sifive_tmc0_tile_pg(void)
+{
+       int rc;
+
+       rc = sifive_tmc0_prep();
+       if (rc)
+               return rc;
+
+       return sifive_tmc0_enter();
+}
+
+static struct sifive_tmc0 *sifive_tmc0_find(u32 id)
+{
+       struct sifive_tmc0 *tmc0;
+
+       sbi_list_for_each_entry(tmc0, &tmc0_list, node) {
+               if (tmc0->id == id)
+                       return tmc0;
+       }
+
+       return NULL;
+}
+
+static int sifive_tmc0_add(struct sifive_tmc0 *tmc0)
+{
+       if (!tmc0)
+               return SBI_ENODEV;
+
+       if (sifive_tmc0_find(tmc0->id))
+               return SBI_EALREADY;
+
+       sbi_list_add(&tmc0->node, &tmc0_list);
+
+       return SBI_OK;
+}
+
+static int sifive_tmc0_probe(const void *fdt, int nodeoff, const struct fdt_match *match)
+{
+       struct sifive_tmc0 *tmc0;
+       int rc;
+       u64 addr;
+
+       tmc0 = sbi_zalloc(sizeof(*tmc0));
+       if (!tmc0)
+               return SBI_ENOMEM;
+
+       rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
+       if (rc)
+               goto free_tmc0;
+
+       tmc0->reg = (unsigned long)addr;
+       tmc0->id = nodeoff;
+
+       rc = sifive_tmc0_add(tmc0);
+       if (rc)
+               goto free_tmc0;
+
+       return SBI_OK;
+
+free_tmc0:
+       sbi_free(tmc0);
+       return rc;
+}
+
+static const struct fdt_match sifive_tmc0_match[] = {
+       { .compatible = "sifive,tmc0" },
+       { },
+};
+
+const struct fdt_driver fdt_hsm_sifive_tmc0 = {
+       .match_table = sifive_tmc0_match,
+       .init = sifive_tmc0_probe,
+};
+
+static const struct fdt_driver *const fdt_hsm_sifive_tmc0_drivers[] = {
+       &fdt_hsm_sifive_tmc0,
+       NULL
+};
+
+static int fdt_sifive_tmc0_get(const void *fdt, int noff, struct sifive_tmc0 **out)
+{
+       struct sifive_tmc0 *tmc0;
+       int rc;
+
+       noff = fdt_node_offset_by_phandle(fdt, noff);
+       if (noff < 0)
+               return noff;
+
+       rc = fdt_driver_init_by_offset(fdt, noff, fdt_hsm_sifive_tmc0_drivers);
+       if (rc)
+               return rc;
+
+       tmc0 = sifive_tmc0_find(noff);
+       if (!tmc0)
+               return SBI_EFAIL;
+
+       if (out)
+               *out = tmc0;
+
+       return SBI_OK;
+}
+
+static int fdt_sifive_tmc0_cold_init(const void *fdt)
+{
+       struct sbi_scratch *scratch;
+       struct sifive_tmc0 *tmc0;
+       const fdt32_t *val;
+       int cpus_off, cpu_off, rc;
+       u32 hartid;
+
+       cpus_off = fdt_path_offset(fdt, "/cpus");
+       if (cpus_off < 0)
+               return SBI_ENOENT;
+
+       fdt_for_each_subnode(cpu_off, fdt, cpus_off) {
+               rc = fdt_parse_hart_id(fdt, cpu_off, &hartid);
+               if (rc)
+                       continue;
+
+               val = fdt_getprop(fdt, cpu_off, "power-domains", NULL);
+               if (!val)
+                       return SBI_ENOENT;
+
+               rc = fdt_sifive_tmc0_get(fdt, fdt32_to_cpu(val[0]), &tmc0);
+               if (rc)
+                       return rc;
+
+               scratch = sbi_hartid_to_scratch(hartid);
+               if (!scratch)
+                       continue;
+
+               tmc0_ptr_set(scratch, tmc0);
+       }
+
+       return SBI_OK;
+}
+
+static int sifive_tmc0_start(u32 hartid, ulong saddr)
+{
+       struct sbi_ipi_device *clint = aclint_mswi_get();
+
+       /*
+        * In system suspend, the IMSIC will be reset in SiFive platform so
+        * we use the CLINT IPI as the wake event.
+        */
+       if (clint && clint->ipi_send)
+               clint->ipi_send(sbi_hartid_to_hartindex(hartid));
+       else
+               sbi_ipi_raw_send(sbi_hartid_to_hartindex(hartid));
+
+       return SBI_OK;
+}
Quoted 9 lines

lib/utils/hsm/fdt_hsm_sifive_tmc0.c line 347

Quoted 6 lines
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +6 -0
+
+static int sifive_tmc0_stop(void)
+{
+       unsigned long mie = csr_read(CSR_MIE);
+       int rc;
+       // Set IPI as wake up source

Please don't use c++ style "//" comments.

Will fix it in the next version

Quoted 27 lines
Quoted 27 lines
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +27 -0
+       csr_set(CSR_MIE, MIP_MEIP | MIP_MSIP);
+
+       rc = sifive_tmc0_tile_pg();
+       if (rc) {
+               csr_write(CSR_MIE, mie);
+               return rc;
+       }
+
+       return SBI_OK;
+}
+
+static struct sbi_hsm_device tmc0_hsm_dev = {
+       .name = "SiFive TMC0",
+       .hart_start = sifive_tmc0_start,
+       .hart_stop = sifive_tmc0_stop,
+};
+
+int fdt_hsm_sifive_tmc0_cold_init(void)
+{
+       int rc;
+
+       tmc0_offset = sbi_scratch_alloc_type_offset(struct sifive_tmc0 *);
+       if (!tmc0_offset)
+               return SBI_ENOMEM;
+
+       rc = fdt_sifive_tmc0_cold_init(fdt_get_address());
+       if (rc)
Quoted 9 lines

lib/utils/hsm/fdt_hsm_sifive_tmc0.c line 380

Quoted 6 lines
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +6 -0
+               return rc;
+
+       sbi_hsm_set_device(&tmc0_hsm_dev);
+
+       return SBI_OK;
+}

Why can't the SiFive TMC driver probe as fdt_early_driver ?

Yes, it can. This requires updating the logic for binding the TMC to the corresponding CPU.
In the current implementation, we iterate over each CPU DT node to probe the TMC driver via its power-domains property and then bind the TMC to that CPU.

Quoted 4 lines

If the TMC driver is registered as an fdt_early_driver, we instead need to iterate over each CPU node during every TMC driver probe and bind the TMC to the CPU whose power-domains property references the current TMC node.

I think it's better to have the TMC driver as an fdt_early_driver. If required you can add helper routines to parse power-domain from the CPU DT node.

Regards,
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#15 lore raw

Re: [PATCH v4 09/11] platform: sifive: Add SiFive TMC0 driver

Quoted 497 lines · Anup Patel
Quoted 495 lines · Nick Hu
Quoted 440 lines · Anup Patel
Quoted 438 lines · Nick Hu

The SiFive TMC0 controls the tile power domains on SiFive platform. The CPU enters the low power state via the CEASE instruction after configuring the TMC0. Any devices that inside the tile power domain will be power gated, including the private cache. Therefore flushing the private cache before entering the low power state.

---
 include/sbi_utils/hsm/fdt_hsm_sifive_inst.h   |  20 +
 include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h   |  16 +
 lib/utils/hsm/Kconfig                         |   5 +
 lib/utils/hsm/fdt_hsm_sifive_tmc0.c           | 380 ++++++++++++++++++
 lib/utils/hsm/objects.mk                      |   2 +
 platform/generic/Kconfig                      |   5 +
 platform/generic/configs/defconfig            |   2 +
 platform/generic/sifive/objects.mk            |   3 +
 platform/generic/sifive/sifive_dev_platform.c |  57 +++
 9 files changed, 490 insertions(+)
 create mode 100644 include/sbi_utils/hsm/fdt_hsm_sifive_inst.h
 create mode 100644 include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h
 create mode 100644 lib/utils/hsm/fdt_hsm_sifive_tmc0.c
 create mode 100644 platform/generic/sifive/sifive_dev_platform.c
include/sbi_utils/hsm/fdt_hsm_sifive_inst.h new file +20 -0
new file mode 100644
index 00000000..de8aba19
@@ -0,0 +1,20 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __FDT_HSM_SIFIVE_INST_H__
+#define __FDT_HSM_SIFIVE_INST_H__
+
+static inline void sifive_cease(void)
+{
+       __asm__ __volatile__(".insn 0x30500073" ::: "memory");
+}
+
+static inline void sifive_cflush(void)
+{
+       __asm__ __volatile__(".insn 0xfc000073" ::: "memory");
+}
+
+#endif
include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h new file +16 -0
new file mode 100644
index 00000000..4ba048b9
@@ -0,0 +1,16 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __FDT_HSM_SIFIVE_TMC0_H__
+#define __FDT_HSM_SIFIVE_TMC0_H__
+
+#ifdef CONFIG_FDT_HSM_SIFIVE_TMC0
+int fdt_hsm_sifive_tmc0_cold_init(void);
+#else
+static inline int fdt_hsm_sifive_tmc0_cold_init(void) { return 0; }
+#endif
+
+#endif
lib/utils/hsm/Kconfig +5 -0
index 1ad7958f..3e6f8909 100644
@@ -14,6 +14,11 @@ config FDT_HSM_RPMI
        depends on FDT_MAILBOX && RPMI_MAILBOX
        default n
 
+config FDT_HSM_SIFIVE_TMC0
+       bool "FDT SiFive TMC v0 driver"
+       depends on PLATFORM_SIFIVE_DEV
+       default n
+
 endif
 
 endmenu
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +341 -0
new file mode 100644
index 00000000..83f86c88
@@ -0,0 +1,380 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive
+ */
+
+#include <libfdt.h>
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_io.h>
+#include <sbi/sbi_bitops.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_hart.h>
+#include <sbi/sbi_heap.h>
+#include <sbi/sbi_hsm.h>
+#include <sbi/sbi_ipi.h>
+#include <sbi_utils/cache/fdt_cmo_helper.h>
+#include <sbi_utils/fdt/fdt_driver.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/hsm/fdt_hsm_sifive_inst.h>
+#include <sbi_utils/hsm/fdt_hsm_sifive_tmc0.h>
+#include <sbi_utils/ipi/aclint_mswi.h>
+
+struct sifive_tmc0 {
+       unsigned long reg;
+       struct sbi_dlist node;
+       u32 id;
+};
+
+static SBI_LIST_HEAD(tmc0_list);
+static unsigned long tmc0_offset;
+
+#define tmc0_ptr_get(__scratch)                                \
+       sbi_scratch_read_type((__scratch), struct sifive_tmc0 *, tmc0_offset)
+
+#define tmc0_ptr_set(__scratch, __tmc0)                        \
+       sbi_scratch_write_type((__scratch), struct sifive_tmc0 *, tmc0_offset, (__tmc0))
+
+/* TMC.PGPREP */
+#define SIFIVE_TMC_PGPREP_OFF                  0x0
+#define SIFIVE_TMC_PGPREP_ENA_REQ              BIT(31)
+#define SIFIVE_TMC_PGPREP_ENA_ACK              BIT(30)
+#define SIFIVE_TMC_PGPREP_DIS_REQ              BIT(29)
+#define SIFIVE_TMC_PGPREP_DIS_ACK              BIT(28)
+#define SIFIVE_TMC_PGPREP_CLFPNOTQ             BIT(18)
+#define SIFIVE_TMC_PGPREP_PMCENAERR            BIT(17)
+#define SIFIVE_TMC_PGPREP_PMCDENY              BIT(16)
+#define SIFIVE_TMC_PGPREP_BUSERR               BIT(15)
+#define SIFIVE_TMC_PGPREP_WAKE_DETECT          BIT(12)
+#define SIFIVE_TMC_PGPREP_INTERNAL_ABORT       BIT(2)
+#define SIFIVE_TMC_PGPREP_ENARSP               (SIFIVE_TMC_PGPREP_CLFPNOTQ | \
+                                                SIFIVE_TMC_PGPREP_PMCENAERR | \
+                                                SIFIVE_TMC_PGPREP_PMCDENY | \
+                                                SIFIVE_TMC_PGPREP_BUSERR | \
+                                                SIFIVE_TMC_PGPREP_WAKE_DETECT)
+
+/* TMC.PG */
+#define SIFIVE_TMC_PG_OFF                      0x4
+#define SIFIVE_TMC_PG_ENA_REQ                  BIT(31)
+#define SIFIVE_TMC_PG_ENA_ACK                  BIT(30)
+#define SIFIVE_TMC_PG_DIS_REQ                  BIT(29)
+#define SIFIVE_TMC_PG_DIS_ACK                  BIT(28)
+#define SIFIVE_TMC_PG_PMC_ENA_ERR              BIT(17)
+#define SIFIVE_TMC_PG_PMC_DENY                 BIT(16)
+#define SIFIVE_TMC_PG_BUS_ERR                  BIT(15)
+#define SIFIVE_TMC_PG_MASTNOTQ                 BIT(14)
+#define SIFIVE_TMC_PG_WARM_RESET               BIT(1)
+#define SIFIVE_TMC_PG_ENARSP                   (SIFIVE_TMC_PG_PMC_ENA_ERR | \
+                                                SIFIVE_TMC_PG_PMC_DENY | \
+                                                SIFIVE_TMC_PG_BUS_ERR | \
+                                                SIFIVE_TMC_PG_MASTNOTQ)
+
+/* TMC.RESUMEPC */
+#define SIFIVE_TMC_RESUMEPC_LO                 0x10
+#define SIFIVE_TMC_RESUMEPC_HI                 0x14
+
+/* TMC.WAKEMASK */
+#define SIFIVE_TMC_WAKE_MASK_OFF               0x20
+#define SIFIVE_TMC_WAKE_MASK_WREQ              BIT(31)
+#define SIFIVE_TMC_WAKE_MASK_ACK               BIT(30)
+
+static void sifive_tmc0_set_resumepc(physical_addr_t addr)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+
+       writel((u32)addr, (void *)(tmc0->reg + SIFIVE_TMC_RESUMEPC_LO));
+#if __riscv_xlen > 32
+       writel((u32)(addr >> 32), (void *)(tmc0->reg + SIFIVE_TMC_RESUMEPC_HI));
+#endif
+}
+
+static u32 sifive_tmc0_set_pgprep_enareq(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PGPREP_OFF;
+       u32 v = readl((void *)reg);
+
+       writel(v | SIFIVE_TMC_PGPREP_ENA_REQ, (void *)reg);
+       while (!(readl((void *)reg) & SIFIVE_TMC_PGPREP_ENA_ACK));
+
+       v = readl((void *)reg);
+       return v & SIFIVE_TMC_PGPREP_INTERNAL_ABORT;
+}
+
+static void sifive_tmc0_set_pgprep_disreq(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PGPREP_OFF;
+       u32 v = readl((void *)reg);
+
+       writel(v | SIFIVE_TMC_PGPREP_DIS_REQ, (void *)reg);
+       while (!(readl((void *)reg) & SIFIVE_TMC_PGPREP_DIS_ACK));
+}
+
+static u32 sifive_tmc0_get_pgprep_enarsp(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PGPREP_OFF;
+       u32 v = readl((void *)reg);
+
+       return v & SIFIVE_TMC_PGPREP_ENARSP;
+}
+
+static void sifive_tmc0_set_pg_enareq(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long reg = tmc0->reg + SIFIVE_TMC_PG_OFF;
+       u32 v = readl((void *)reg);
+
+       writel(v | SIFIVE_TMC_PG_ENA_REQ, (void *)reg);
+}
+
+static int sifive_tmc0_prep(void)
+{
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+       u32 rc;
+
+       if (!tmc0_ptr_get(scratch))
+               return SBI_ENODEV;
+
+       rc = sifive_tmc0_set_pgprep_enareq();
+       if (rc) {
+               sbi_printf("TMC0 error: Internal Abort (Wake detect)\n");
+               goto fail;
+       }
+
+       rc = sifive_tmc0_get_pgprep_enarsp();
+       if (rc) {
+               sifive_tmc0_set_pgprep_disreq();
+               sbi_printf("TMC0 error: error response code: 0x%x\n", rc);
+               goto fail;
+       }
+
+       sifive_tmc0_set_resumepc(scratch->warmboot_addr);
+
+       return SBI_OK;
+
+fail:
+       return SBI_EFAIL;
+}
+
+static int sifive_tmc0_enter(void)
+{
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+       u32 rc;
+
+       /* Flush cache and check if there is wake detect or bus error */
+       if (fdt_cmo_private_flc_flush_all() &&
+           sbi_hart_has_extension(scratch, SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1))
+               sifive_cflush();
+
+       rc = sifive_tmc0_get_pgprep_enarsp();
+       if (rc) {
+               sbi_printf("TMC0 error: error response code: 0x%x\n", rc);
+               goto fail;
+       }
+
+       if (sbi_hart_has_extension(scratch, SBI_HART_EXT_XSIFIVE_CEASE)) {
+               sifive_tmc0_set_pg_enareq();
+               while (1)
+                       sifive_cease();
+       }
+
+       rc = SBI_ENOTSUPP;
+fail:
+       sifive_tmc0_set_pgprep_disreq();
+       return rc;
+}
+
+static int sifive_tmc0_tile_pg(void)
+{
+       int rc;
+
+       rc = sifive_tmc0_prep();
+       if (rc)
+               return rc;
+
+       return sifive_tmc0_enter();
+}
+
+static struct sifive_tmc0 *sifive_tmc0_find(u32 id)
+{
+       struct sifive_tmc0 *tmc0;
+
+       sbi_list_for_each_entry(tmc0, &tmc0_list, node) {
+               if (tmc0->id == id)
+                       return tmc0;
+       }
+
+       return NULL;
+}
+
+static int sifive_tmc0_add(struct sifive_tmc0 *tmc0)
+{
+       if (!tmc0)
+               return SBI_ENODEV;
+
+       if (sifive_tmc0_find(tmc0->id))
+               return SBI_EALREADY;
+
+       sbi_list_add(&tmc0->node, &tmc0_list);
+
+       return SBI_OK;
+}
+
+static int sifive_tmc0_probe(const void *fdt, int nodeoff, const struct fdt_match *match)
+{
+       struct sifive_tmc0 *tmc0;
+       int rc;
+       u64 addr;
+
+       tmc0 = sbi_zalloc(sizeof(*tmc0));
+       if (!tmc0)
+               return SBI_ENOMEM;
+
+       rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
+       if (rc)
+               goto free_tmc0;
+
+       tmc0->reg = (unsigned long)addr;
+       tmc0->id = nodeoff;
+
+       rc = sifive_tmc0_add(tmc0);
+       if (rc)
+               goto free_tmc0;
+
+       return SBI_OK;
+
+free_tmc0:
+       sbi_free(tmc0);
+       return rc;
+}
+
+static const struct fdt_match sifive_tmc0_match[] = {
+       { .compatible = "sifive,tmc0" },
+       { },
+};
+
+const struct fdt_driver fdt_hsm_sifive_tmc0 = {
+       .match_table = sifive_tmc0_match,
+       .init = sifive_tmc0_probe,
+};
+
+static const struct fdt_driver *const fdt_hsm_sifive_tmc0_drivers[] = {
+       &fdt_hsm_sifive_tmc0,
+       NULL
+};
+
+static int fdt_sifive_tmc0_get(const void *fdt, int noff, struct sifive_tmc0 **out)
+{
+       struct sifive_tmc0 *tmc0;
+       int rc;
+
+       noff = fdt_node_offset_by_phandle(fdt, noff);
+       if (noff < 0)
+               return noff;
+
+       rc = fdt_driver_init_by_offset(fdt, noff, fdt_hsm_sifive_tmc0_drivers);
+       if (rc)
+               return rc;
+
+       tmc0 = sifive_tmc0_find(noff);
+       if (!tmc0)
+               return SBI_EFAIL;
+
+       if (out)
+               *out = tmc0;
+
+       return SBI_OK;
+}
+
+static int fdt_sifive_tmc0_cold_init(const void *fdt)
+{
+       struct sbi_scratch *scratch;
+       struct sifive_tmc0 *tmc0;
+       const fdt32_t *val;
+       int cpus_off, cpu_off, rc;
+       u32 hartid;
+
+       cpus_off = fdt_path_offset(fdt, "/cpus");
+       if (cpus_off < 0)
+               return SBI_ENOENT;
+
+       fdt_for_each_subnode(cpu_off, fdt, cpus_off) {
+               rc = fdt_parse_hart_id(fdt, cpu_off, &hartid);
+               if (rc)
+                       continue;
+
+               val = fdt_getprop(fdt, cpu_off, "power-domains", NULL);
+               if (!val)
+                       return SBI_ENOENT;
+
+               rc = fdt_sifive_tmc0_get(fdt, fdt32_to_cpu(val[0]), &tmc0);
+               if (rc)
+                       return rc;
+
+               scratch = sbi_hartid_to_scratch(hartid);
+               if (!scratch)
+                       continue;
+
+               tmc0_ptr_set(scratch, tmc0);
+       }
+
+       return SBI_OK;
+}
+
+static int sifive_tmc0_start(u32 hartid, ulong saddr)
+{
+       struct sbi_ipi_device *clint = aclint_mswi_get();
+
+       /*
+        * In system suspend, the IMSIC will be reset in SiFive platform so
+        * we use the CLINT IPI as the wake event.
+        */
+       if (clint && clint->ipi_send)
+               clint->ipi_send(sbi_hartid_to_hartindex(hartid));
+       else
+               sbi_ipi_raw_send(sbi_hartid_to_hartindex(hartid));
+
+       return SBI_OK;
+}
Quoted 9 lines

lib/utils/hsm/fdt_hsm_sifive_tmc0.c line 347

Quoted 6 lines
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +6 -0
+
+static int sifive_tmc0_stop(void)
+{
+       unsigned long mie = csr_read(CSR_MIE);
+       int rc;
+       // Set IPI as wake up source

Please don't use c++ style "//" comments.

Will fix it in the next version

Quoted 27 lines
Quoted 27 lines
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +27 -0
+       csr_set(CSR_MIE, MIP_MEIP | MIP_MSIP);
+
+       rc = sifive_tmc0_tile_pg();
+       if (rc) {
+               csr_write(CSR_MIE, mie);
+               return rc;
+       }
+
+       return SBI_OK;
+}
+
+static struct sbi_hsm_device tmc0_hsm_dev = {
+       .name = "SiFive TMC0",
+       .hart_start = sifive_tmc0_start,
+       .hart_stop = sifive_tmc0_stop,
+};
+
+int fdt_hsm_sifive_tmc0_cold_init(void)
+{
+       int rc;
+
+       tmc0_offset = sbi_scratch_alloc_type_offset(struct sifive_tmc0 *);
+       if (!tmc0_offset)
+               return SBI_ENOMEM;
+
+       rc = fdt_sifive_tmc0_cold_init(fdt_get_address());
+       if (rc)
Quoted 9 lines

lib/utils/hsm/fdt_hsm_sifive_tmc0.c line 380

Quoted 6 lines
lib/utils/hsm/fdt_hsm_sifive_tmc0.c new file +6 -0
+               return rc;
+
+       sbi_hsm_set_device(&tmc0_hsm_dev);
+
+       return SBI_OK;
+}

Why can't the SiFive TMC driver probe as fdt_early_driver ?

Yes, it can. This requires updating the logic for binding the TMC to the corresponding CPU.
In the current implementation, we iterate over each CPU DT node to probe the TMC driver via its power-domains property and then bind the TMC to that CPU.

Quoted 9 lines
Quoted 4 lines

If the TMC driver is registered as an fdt_early_driver, we instead need to iterate over each CPU node during every TMC driver probe and bind the TMC to the CPU whose power-domains property references the current TMC node.

I think it's better to have the TMC driver as an fdt_early_driver. If required you can add helper routines to parse power-domain from the CPU DT node.

I will update it in the next version. Thanks.

Quoted 2 lines

Regards,
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#16 lore raw

PATCH v4 10/11 lib: utils/timer: Expose timer update function

In reply to Nick Hu

Exposing the ACLINT timer update APIs so the user can update the mtimer after waking up from the non-retentive suspend.

---
 include/sbi_utils/timer/aclint_mtimer.h |  5 +++++
 lib/utils/timer/aclint_mtimer.c         | 28 ++++++++++++++++++-------
 2 files changed, 26 insertions(+), 7 deletions(-)
include/sbi_utils/timer/aclint_mtimer.h +5 -0
index a245e13e..f8d2e5b4 100644
@@ -42,6 +42,11 @@ struct aclint_mtimer_data {
 	void (*time_wr)(bool timecmp, u64 value, volatile u64 *addr);
 };
 
+struct aclint_mtimer_data *aclint_get_mtimer_data(void);
+
+void aclint_mtimer_update(struct aclint_mtimer_data *mt,
+			  struct aclint_mtimer_data *ref);
+
 void aclint_mtimer_sync(struct aclint_mtimer_data *mt);
 
 void aclint_mtimer_set_reference(struct aclint_mtimer_data *mt,
lib/utils/timer/aclint_mtimer.c +21 -7
index 3db3c3be..fd6189aa 100644
@@ -109,28 +109,42 @@ static struct sbi_timer_device mtimer = {
 	.timer_event_stop = mtimer_event_stop
 };
 
-void aclint_mtimer_sync(struct aclint_mtimer_data *mt)
+struct aclint_mtimer_data *aclint_get_mtimer_data(void)
+{
+	return mtimer_get_hart_data_ptr(sbi_scratch_thishart_ptr());
+}
+
+void aclint_mtimer_update(struct aclint_mtimer_data *mt,
+			  struct aclint_mtimer_data *ref)
 {
 	u64 v1, v2, mv, delta;
 	u64 *mt_time_val, *ref_time_val;
-	struct aclint_mtimer_data *reference;
 
-	/* Sync-up non-shared MTIME if reference is available */
-	if (mt->has_shared_mtime || !mt->time_delta_reference)
+	if (!mt || !ref || !mt->time_rd || !mt->time_wr || !ref->time_rd)
 		return;
 
-	reference = mt->time_delta_reference;
 	mt_time_val = (void *)mt->mtime_addr;
-	ref_time_val = (void *)reference->mtime_addr;
+	ref_time_val = (void *)ref->mtime_addr;
 	if (!atomic_raw_xchg_ulong(&mt->time_delta_computed, 1)) {
 		v1 = mt->time_rd(mt_time_val);
-		mv = reference->time_rd(ref_time_val);
+		mv = ref->time_rd(ref_time_val);
 		v2 = mt->time_rd(mt_time_val);
 		delta = mv - ((v1 / 2) + (v2 / 2));
 		mt->time_wr(false, mt->time_rd(mt_time_val) + delta,
 			    mt_time_val);
 	}
+}
 
+void aclint_mtimer_sync(struct aclint_mtimer_data *mt)
+{
+	struct aclint_mtimer_data *reference;
+
+	/* Sync-up non-shared MTIME if reference is available */
+	if (mt->has_shared_mtime || !mt->time_delta_reference)
+		return;
+
+	reference = mt->time_delta_reference;
+	aclint_mtimer_update(mt, reference);
 }
 
 void aclint_mtimer_set_reference(struct aclint_mtimer_data *mt,
Signature · 7 lines
2.17.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#17 lore raw

PATCH v4 11/11 platform: sifive: Add SiFive SMC0 driver

In reply to Nick Hu

The SiFive SMC0 controls the clock and power domain of the core complex on the SiFive platform. The core complex enters the low power state after the secondary cores enter the tile power gating and last core execute the CEASE instruction with the corresponding SMC0 configurations. The devices that inside both tile power domain and core complex power domain will be off, including caches and timer. Therefore we need to flush the last level cache before entering the core complex power gating and update the timer after waking up.

---
 include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h   |   3 +
 .../suspend/fdt_suspend_sifive_smc0.h         |  18 +
 lib/utils/hsm/fdt_hsm_sifive_tmc0.c           |  88 +++++
 lib/utils/suspend/Kconfig                     |   4 +
 lib/utils/suspend/fdt_suspend_sifive_smc0.c   | 323 ++++++++++++++++++
 lib/utils/suspend/objects.mk                  |   3 +
 platform/generic/configs/defconfig            |   1 +
 7 files changed, 440 insertions(+)
 create mode 100644 include/sbi_utils/suspend/fdt_suspend_sifive_smc0.h
 create mode 100644 lib/utils/suspend/fdt_suspend_sifive_smc0.c
include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h +3 -0
index 4ba048b9..d370158a 100644
@@ -9,6 +9,9 @@
 
 #ifdef CONFIG_FDT_HSM_SIFIVE_TMC0
 int fdt_hsm_sifive_tmc0_cold_init(void);
+int sifive_tmc0_set_wakemask_enareq(u32 hartid);
+void sifive_tmc0_set_wakemask_disreq(u32 hartid);
+bool sifive_tmc0_is_pg(u32 hartid);
 #else
 static inline int fdt_hsm_sifive_tmc0_cold_init(void) { return 0; }
 #endif
include/sbi_utils/suspend/fdt_suspend_sifive_smc0.h new file +18 -0
new file mode 100644
index 00000000..f092be52
@@ -0,0 +1,18 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __FDT_SUSPEND_SIFIVE_SMC0_H__
+#define __FDT_SUSPEND_SIFIVE_SMC0_H__
+
+#ifdef CONFIG_FDT_SUSPEND_SIFIVE_SMC0
+bool sifive_smc0_check_warm_reset(void);
+void sifive_smc0_mtime_update(void);
+#else
+static inline bool sifive_smc0_check_warm_reset(void) { return false; }
+static inline void sifive_smc0_mtime_update(void) { return; }
+#endif
+
+#endif
lib/utils/hsm/fdt_hsm_sifive_tmc0.c +88 -0

110 lines not on this page · raw message

lib/utils/suspend/Kconfig +4 -0
index 2cbea75c..623e053f 100644
@@ -14,6 +14,10 @@ config FDT_SUSPEND_RPMI
 	depends on FDT_MAILBOX && RPMI_MAILBOX
 	default n
 
+config FDT_SUSPEND_SIFIVE_SMC0
+	bool "FDT SIFIVE SMC0 suspend driver"
+	depends on FDT_HSM_SIFIVE_TMC0
+	default n
 endif
 
 endmenu
lib/utils/suspend/fdt_suspend_sifive_smc0.c new file +323 -0

323 lines not on this page · raw message

lib/utils/suspend/objects.mk +3 -0
index 9c386248..1fb29b5e 100644
@@ -9,3 +9,6 @@
 
 carray-fdt_early_drivers-$(CONFIG_FDT_SUSPEND_RPMI) += fdt_suspend_rpmi
 libsbiutils-objs-$(CONFIG_FDT_SUSPEND_RPMI) += suspend/fdt_suspend_rpmi.o
+
+carray-fdt_early_drivers-$(CONFIG_FDT_SUSPEND_SIFIVE_SMC0) += fdt_suspend_sifive_smc0
+libsbiutils-objs-$(CONFIG_FDT_SUSPEND_SIFIVE_SMC0) += suspend/fdt_suspend_sifive_smc0.o
platform/generic/configs/defconfig +1 -0
index 837acd79..258988cc 100644
@@ -55,6 +55,7 @@ CONFIG_FDT_SERIAL_XILINX_UARTLITE=y
 CONFIG_SERIAL_SEMIHOSTING=y
 CONFIG_FDT_SUSPEND=y
 CONFIG_FDT_SUSPEND_RPMI=y
+CONFIG_FDT_SUSPEND_SIFIVE_SMC0=y
 CONFIG_FDT_TIMER=y
 CONFIG_FDT_TIMER_MTIMER=y
 CONFIG_FDT_TIMER_PLMT=y
Signature · 7 lines
2.17.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v4 11/11
#18 lore raw

Re: [PATCH v4 11/11] platform: sifive: Add SiFive SMC0 driver

Quoted 10 lines · Nick Hu

The SiFive SMC0 controls the clock and power domain of the core complex on the SiFive platform. The core complex enters the low power state after the secondary cores enter the tile power gating and last core execute the CEASE instruction with the corresponding SMC0 configurations. The devices that inside both tile power domain and core complex power domain will be off, including caches and timer. Therefore we need to flush the last level cache before entering the core complex power gating and update the timer after waking up.

Quoted 2 lines

LGTM.

Regards,
Anup

Quoted 537 lines
---
 include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h   |   3 +
 .../suspend/fdt_suspend_sifive_smc0.h         |  18 +
 lib/utils/hsm/fdt_hsm_sifive_tmc0.c           |  88 +++++
 lib/utils/suspend/Kconfig                     |   4 +
 lib/utils/suspend/fdt_suspend_sifive_smc0.c   | 323 ++++++++++++++++++
 lib/utils/suspend/objects.mk                  |   3 +
 platform/generic/configs/defconfig            |   1 +
 7 files changed, 440 insertions(+)
 create mode 100644 include/sbi_utils/suspend/fdt_suspend_sifive_smc0.h
 create mode 100644 lib/utils/suspend/fdt_suspend_sifive_smc0.c
include/sbi_utils/hsm/fdt_hsm_sifive_tmc0.h +3 -0
index 4ba048b9..d370158a 100644
@@ -9,6 +9,9 @@
 
 #ifdef CONFIG_FDT_HSM_SIFIVE_TMC0
 int fdt_hsm_sifive_tmc0_cold_init(void);
+int sifive_tmc0_set_wakemask_enareq(u32 hartid);
+void sifive_tmc0_set_wakemask_disreq(u32 hartid);
+bool sifive_tmc0_is_pg(u32 hartid);
 #else
 static inline int fdt_hsm_sifive_tmc0_cold_init(void) { return 0; }
 #endif
include/sbi_utils/suspend/fdt_suspend_sifive_smc0.h new file +18 -0
new file mode 100644
index 00000000..f092be52
@@ -0,0 +1,18 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#ifndef __FDT_SUSPEND_SIFIVE_SMC0_H__
+#define __FDT_SUSPEND_SIFIVE_SMC0_H__
+
+#ifdef CONFIG_FDT_SUSPEND_SIFIVE_SMC0
+bool sifive_smc0_check_warm_reset(void);
+void sifive_smc0_mtime_update(void);
+#else
+static inline bool sifive_smc0_check_warm_reset(void) { return false; }
+static inline void sifive_smc0_mtime_update(void) { return; }
+#endif
+
+#endif
lib/utils/hsm/fdt_hsm_sifive_tmc0.c +88 -0
index 83f86c88..4a13fd4d 100644
@@ -20,6 +20,8 @@
 #include <sbi_utils/hsm/fdt_hsm_sifive_inst.h>
 #include <sbi_utils/hsm/fdt_hsm_sifive_tmc0.h>
 #include <sbi_utils/ipi/aclint_mswi.h>
+#include <sbi_utils/irqchip/aplic.h>
+#include <sbi_utils/suspend/fdt_suspend_sifive_smc0.h>
 
 struct sifive_tmc0 {
        unsigned long reg;
@@ -79,6 +81,73 @@ static unsigned long tmc0_offset;
 #define SIFIVE_TMC_WAKE_MASK_WREQ              BIT(31)
 #define SIFIVE_TMC_WAKE_MASK_ACK               BIT(30)
 
+int sifive_tmc0_set_wakemask_enareq(u32 hartid)
+{
+       struct sbi_scratch *scratch = sbi_hartid_to_scratch(hartid);
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(scratch);
+       unsigned long addr;
+       u32 v;
+
+       if (!tmc0)
+               return SBI_ENODEV;
+
+       addr = tmc0->reg + SIFIVE_TMC_WAKE_MASK_OFF;
+       v = readl((void *)addr);
+       writel(v | SIFIVE_TMC_WAKE_MASK_WREQ, (void *)addr);
+
+       while (!(readl((void *)addr) & SIFIVE_TMC_WAKE_MASK_ACK));
+
+       return SBI_OK;
+}
+
+void sifive_tmc0_set_wakemask_disreq(u32 hartid)
+{
+       struct sbi_scratch *scratch = sbi_hartid_to_scratch(hartid);
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(scratch);
+       unsigned long addr;
+       u32 v;
+
+       if (!tmc0)
+               return;
+
+       addr = tmc0->reg + SIFIVE_TMC_WAKE_MASK_OFF;
+       v = readl((void *)addr);
+       writel(v & ~SIFIVE_TMC_WAKE_MASK_WREQ, (void *)addr);
+
+       while (readl((void *)addr) & SIFIVE_TMC_WAKE_MASK_ACK);
+}
+
+bool sifive_tmc0_is_pg(u32 hartid)
+{
+       struct sbi_scratch *scratch = sbi_hartid_to_scratch(hartid);
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(scratch);
+       unsigned long addr;
+       u32 v;
+
+       if (!tmc0)
+               return false;
+
+       addr = tmc0->reg + SIFIVE_TMC_PG_OFF;
+       v = readl((void *)addr);
+       if (!(v & SIFIVE_TMC_PG_ENA_ACK) ||
+           (v & SIFIVE_TMC_PG_ENARSP) ||
+           (v & SIFIVE_TMC_PG_DIS_REQ))
+               return false;
+
+       return true;
+}
+
+static bool sifive_tmc0_check_warm_reset(void)
+{
+       struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
+       unsigned long addr = tmc0->reg + SIFIVE_TMC_PG_OFF;
+
+       if (!tmc0)
+               return false;
+
+       return readl((void *)addr) & SIFIVE_TMC_PG_WARM_RESET ? true : false;
+}
+
 static void sifive_tmc0_set_resumepc(physical_addr_t addr)
 {
        struct sifive_tmc0 *tmc0 = tmc0_ptr_get(sbi_scratch_thishart_ptr());
@@ -356,10 +425,29 @@ static int sifive_tmc0_stop(void)
        return SBI_OK;
 }
 
+static void sifive_pg_resume(void)
+{
+       /*
+        * To simplify the implementation, the SW won't clear the warm
+        * reset bit. If the system woken up from the Core Complex power
+        * gating, the warm reset bit of the TMC0 will be clear. Therefore
+        * we need to check the warm reset bit of the TMC0 first as the
+        * Tile power gating won't clear the warm reset bit of the SMC0.
+        */
+       if (sifive_tmc0_check_warm_reset())
+               return;
+
+       if (sifive_smc0_check_warm_reset()) {
+               aplic_restore();
+               sifive_smc0_mtime_update();
+       }
+}
+
 static struct sbi_hsm_device tmc0_hsm_dev = {
        .name = "SiFive TMC0",
        .hart_start = sifive_tmc0_start,
        .hart_stop = sifive_tmc0_stop,
+       .hart_resume = sifive_pg_resume,
 };
 
 int fdt_hsm_sifive_tmc0_cold_init(void)
lib/utils/suspend/Kconfig +4 -0
index 2cbea75c..623e053f 100644
@@ -14,6 +14,10 @@ config FDT_SUSPEND_RPMI
        depends on FDT_MAILBOX && RPMI_MAILBOX
        default n
 
+config FDT_SUSPEND_SIFIVE_SMC0
+       bool "FDT SIFIVE SMC0 suspend driver"
+       depends on FDT_HSM_SIFIVE_TMC0
+       default n
 endif
 
 endmenu
lib/utils/suspend/fdt_suspend_sifive_smc0.c new file +323 -0
new file mode 100644
index 00000000..005486b4
@@ -0,0 +1,323 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive
+ */
+
+#include <libfdt.h>
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_io.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_domain.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_hart.h>
+#include <sbi/sbi_hsm.h>
+#include <sbi/sbi_system.h>
+#include <sbi/sbi_timer.h>
+#include <sbi_utils/cache/fdt_cmo_helper.h>
+#include <sbi_utils/fdt/fdt_driver.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/hsm/fdt_hsm_sifive_inst.h>
+#include <sbi_utils/hsm/fdt_hsm_sifive_tmc0.h>
+#include <sbi_utils/suspend/fdt_suspend_sifive_smc0.h>
+#include <sbi_utils/timer/aclint_mtimer.h>
+
+#define SIFIVE_SMC_PGPREP_OFF                  0x0
+#define SIFIVE_SMC_PG_OFF                      0x4
+#define SIFIVE_SMC_CCTIMER_OFF                 0xc
+#define SIFIVE_SMC_RESUMEPC_LO_OFF             0x10
+#define SIFIVE_SMC_RESUMEPC_HI_OFF             0x14
+#define SIFIVE_SMC_SYNC_PMC_OFF                        0x24
+#define SIFIVE_SMC_CYCLECOUNT_LO_OFF           0x28
+#define SIFIVE_SMC_CYCLECOUNT_HI_OFF           0x2c
+#define SIFIVE_SMC_WFI_UNCORE_CG_OFF           0x50
+
+#define SIFIVE_SMC_PGPREP_ENA_REQ              BIT(31)
+#define SIFIVE_SMC_PGPREP_ENA_ACK              BIT(30)
+#define SIFIVE_SMC_PGPREP_DIS_REQ              BIT(29)
+#define SIFIVE_SMC_PGPREP_DIS_ACK              BIT(29)
+#define SIFIVE_SMC_PGPREP_FRONTNOTQ            BIT(19)
+#define SIFIVE_SMC_PGPREP_CLFPNOTQ             BIT(18)
+#define SIFIVE_SMC_PGPREP_PMCENAERR            BIT(17)
+#define SIFIVE_SMC_PGPREP_WAKE_DETECT          BIT(16)
+#define SIFIVE_SMC_PGPREP_BUSERR               BIT(15)
+#define SIFIVE_SMC_PGPREP_EARLY_ABORT          BIT(3)
+#define SIFIVE_SMC_PGPREP_INTERNAL_ABORT       BIT(2)
+#define SIFIVE_SMC_PGPREP_ENARSP               (SIFIVE_SMC_PGPREP_FRONTNOTQ | \
+                                                SIFIVE_SMC_PGPREP_CLFPNOTQ | \
+                                                SIFIVE_SMC_PGPREP_PMCENAERR | \
+                                                SIFIVE_SMC_PGPREP_WAKE_DETECT | \
+                                                SIFIVE_SMC_PGPREP_BUSERR)
+
+#define SIFIVE_SMC_PGPREP_ABORT                        (SIFIVE_SMC_PGPREP_EARLY_ABORT | \
+                                                SIFIVE_SMC_PGPREP_INTERNAL_ABORT)
+
+#define SIFIVE_SMC_PG_ENA_REQ                  BIT(31)
+#define SIFIVE_SMC_PG_WARM_RESET               BIT(1)
+
+#define SIFIVE_SMC_SYNCPMC_SYNC_REQ            BIT(31)
+#define SIFIVE_SMC_SYNCPMC_SYNC_WREQ           BIT(30)
+#define SIFIVE_SMC_SYNCPMC_SYNC_ACK            BIT(29)
+
+static struct aclint_mtimer_data smc_sync_timer;
+static unsigned long smc0_base;
+
+static void sifive_smc0_set_pmcsync(char regid, bool write_mode)
+{
+       unsigned long addr = smc0_base + SIFIVE_SMC_SYNC_PMC_OFF;
+       u32 v = regid | SIFIVE_SMC_SYNCPMC_SYNC_REQ;
+
+       if (write_mode)
+               v |= SIFIVE_SMC_SYNCPMC_SYNC_WREQ;
+
+       writel(v, (void *)addr);
+       while (!(readl((void *)addr) & SIFIVE_SMC_SYNCPMC_SYNC_ACK));
+}
+
+static u64 sifive_smc0_time_read(volatile u64 *addr)
+{
+       u32 lo, hi;
+
+       do {
+               sifive_smc0_set_pmcsync(SIFIVE_SMC_CYCLECOUNT_LO_OFF, false);
+               sifive_smc0_set_pmcsync(SIFIVE_SMC_CYCLECOUNT_HI_OFF, false);
+               hi = readl_relaxed((u32 *)addr + 1);
+               lo = readl_relaxed((u32 *)addr);
+       } while (hi != readl_relaxed((u32 *)addr + 1));
+
+       return ((u64)hi << 32) | (u64)lo;
+}
+
+static void sifive_smc0_set_resumepc(physical_addr_t raddr)
+{
+       /* Set resumepc_lo */
+       writel((u32)raddr, (void *)(smc0_base + SIFIVE_SMC_RESUMEPC_LO_OFF));
+       /* copy resumepc_lo from SMC to PMC */
+       sifive_smc0_set_pmcsync(SIFIVE_SMC_RESUMEPC_LO_OFF, true);
+#if __riscv_xlen > 32
+       /* Set resumepc_hi */
+       writel((u32)(raddr >> 32), (void *)(smc0_base + SIFIVE_SMC_RESUMEPC_HI_OFF));
+       /* copy resumepc_hi from SMC to PMC */
+       sifive_smc0_set_pmcsync(SIFIVE_SMC_RESUMEPC_HI_OFF, true);
+#endif
+}
+
+static u32 sifive_smc0_get_pgprep_enarsp(void)
+{
+       u32 v = readl((void *)(smc0_base + SIFIVE_SMC_PGPREP_OFF));
+
+       return v & SIFIVE_SMC_PGPREP_ENARSP;
+}
+
+static void sifive_smc0_set_pgprep_disreq(void)
+{
+       unsigned long addr = smc0_base + SIFIVE_SMC_PGPREP_OFF;
+       u32 v = readl((void *)addr);
+
+       writel(v | SIFIVE_SMC_PGPREP_DIS_REQ, (void *)addr);
+       while (!(readl((void *)addr) & SIFIVE_SMC_PGPREP_DIS_ACK));
+}
+
+static u32 sifive_smc0_set_pgprep_enareq(void)
+{
+       unsigned long addr = smc0_base + SIFIVE_SMC_PGPREP_OFF;
+       u32 v = readl((void *)addr);
+
+       writel(v | SIFIVE_SMC_PGPREP_ENA_REQ, (void *)addr);
+       while (!(readl((void *)addr) & SIFIVE_SMC_PGPREP_ENA_ACK));
+
+       v = readl((void *)addr);
+
+       return v & SIFIVE_SMC_PGPREP_ABORT;
+}
+
+static void sifive_smc0_set_pg_enareq(void)
+{
+       unsigned long addr = smc0_base + SIFIVE_SMC_PG_OFF;
+       u32 v = readl((void *)addr);
+
+       writel(v | SIFIVE_SMC_PG_ENA_REQ, (void *)addr);
+}
+
+bool sifive_smc0_check_warm_reset(void)
+{
+       unsigned long addr = smc0_base + SIFIVE_SMC_PG_OFF;
+
+       if (!smc0_base)
+               return false;
+
+       sifive_smc0_set_pmcsync(SIFIVE_SMC_PG_OFF, false);
+
+       return readl((void *)addr) & SIFIVE_SMC_PG_WARM_RESET ? true : false;
+}
+
+static inline void sifive_smc0_set_cg(bool enable)
+{
+       unsigned long addr = smc0_base + SIFIVE_SMC_WFI_UNCORE_CG_OFF;
+
+       if (enable)
+               writel(0, (void *)addr);
+       else
+               writel(1, (void *)addr);
+}
+
+static int sifive_smc0_prep(void)
+{
+       const struct sbi_domain *dom = &root;
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+       unsigned long i;
+       int rc;
+       u32 target;
+
+       if (!smc0_base)
+               return SBI_ENODEV;
+
+       /* Prevent all secondary tiles from waking up from PG state */
+       sbi_hartmask_for_each_hartindex(i, dom->possible_harts) {
+               target = sbi_hartindex_to_hartid(i);
+               if (target != current_hartid()) {
+                       rc = sifive_tmc0_set_wakemask_enareq(target);
+                       if (rc) {
+                               sbi_printf("Fail to enable wakemask for hart %d\n",
+                                          target);
+                               goto fail;
+                       }
+               }
+       }
+
+       /* Check if all secondary tiles enter PG state */
+       sbi_hartmask_for_each_hartindex(i, dom->possible_harts) {
+               target = sbi_hartindex_to_hartid(i);
+               if (target != current_hartid() &&
+                   !sifive_tmc0_is_pg(target)) {
+                       sbi_printf("Hart %d not in the PG state\n", target);
+                       goto fail;
+               }
+       }
+
+       rc = sifive_smc0_set_pgprep_enareq();
+       if (rc) {
+               sbi_printf("SMC0 error: abort code: 0x%x\n", rc);
+               goto fail;
+       }
+
+       rc = sifive_smc0_get_pgprep_enarsp();
+       if (rc) {
+               sifive_smc0_set_pgprep_disreq();
+               sbi_printf("SMC0 error: error response code: 0x%x\n", rc);
+               goto fail;
+       }
+
+       sifive_smc0_set_resumepc(scratch->warmboot_addr);
+       return SBI_OK;
+fail:
+       sbi_hartmask_for_each_hartindex(i, dom->possible_harts) {
+               target = sbi_hartindex_to_hartid(i);
+               if (target != current_hartid())
+                       sifive_tmc0_set_wakemask_disreq(target);
+       }
+
+       return SBI_EFAIL;
+}
+
+static int sifive_smc0_enter(void)
+{
+       const struct sbi_domain *dom = &root;
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+       unsigned long i;
+       u32 target, rc;
+
+       /* Flush cache and check if there is wake detect or bus error */
+       if (fdt_cmo_llc_flush_all() &&
+           sbi_hart_has_extension(scratch, SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1))
+               sifive_cflush();
+
+       rc = sifive_smc0_get_pgprep_enarsp();
+       if (rc) {
+               sbi_printf("SMC0 error: error response code: 0x%x\n", rc);
+               rc = SBI_EFAIL;
+               goto fail;
+       }
+
+       if (sbi_hart_has_extension(scratch, SBI_HART_EXT_XSIFIVE_CEASE)) {
+               sifive_smc0_set_pg_enareq();
+               while (1)
+                       sifive_cease();
+       }
+
+       rc = SBI_ENOTSUPP;
+fail:
+       sifive_smc0_set_pgprep_disreq();
+       sbi_hartmask_for_each_hartindex(i, dom->possible_harts) {
+               target = sbi_hartindex_to_hartid(i);
+               if (target != current_hartid())
+                       sifive_tmc0_set_wakemask_disreq(target);
+       }
+       return rc;
+}
+
+static int sifive_smc0_pg(void)
+{
+       int rc;
+
+       rc = sifive_smc0_prep();
+       if (rc)
+               return rc;
+
+       return sifive_smc0_enter();
+}
+
+static int sifive_smc0_system_suspend(u32 sleep_type, unsigned long addr)
+{
+       /* Disable the timer interrupt */
+       sbi_timer_exit(sbi_scratch_thishart_ptr());
+
+       return sifive_smc0_pg();
+}
+
+static int sifive_smc0_system_suspend_check(u32 sleep_type)
+{
+       return sleep_type == SBI_SUSP_SLEEP_TYPE_SUSPEND ? SBI_OK : SBI_EINVAL;
+}
+
+static struct sbi_system_suspend_device smc0_sys_susp = {
+       .name = "Sifive SMC0",
+       .system_suspend_check = sifive_smc0_system_suspend_check,
+       .system_suspend = sifive_smc0_system_suspend,
+};
+
+void sifive_smc0_mtime_update(void)
+{
+       struct aclint_mtimer_data *mt = aclint_get_mtimer_data();
+
+       aclint_mtimer_update(mt, &smc_sync_timer);
+}
+
+static int sifive_smc0_probe(const void *fdt, int nodeoff, const struct fdt_match *match)
+{
+       int rc;
+       u64 addr;
+
+       rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
+       if (rc)
+               return rc;
+
+       smc0_base = (unsigned long)addr;
+       smc_sync_timer.time_rd = sifive_smc0_time_read;
+       smc_sync_timer.mtime_addr = smc0_base + SIFIVE_SMC_CYCLECOUNT_LO_OFF;
+
+       sbi_system_suspend_set_device(&smc0_sys_susp);
+       sifive_smc0_set_cg(true);
+
+       return SBI_OK;
+}
+
+static const struct fdt_match sifive_smc0_match[] = {
+       { .compatible = "sifive,smc0" },
+       { },
+};
+
+const struct fdt_driver fdt_suspend_sifive_smc0 = {
+       .match_table = sifive_smc0_match,
+       .init = sifive_smc0_probe,
+};
lib/utils/suspend/objects.mk +3 -0
index 9c386248..1fb29b5e 100644
@@ -9,3 +9,6 @@
 
 carray-fdt_early_drivers-$(CONFIG_FDT_SUSPEND_RPMI) += fdt_suspend_rpmi
 libsbiutils-objs-$(CONFIG_FDT_SUSPEND_RPMI) += suspend/fdt_suspend_rpmi.o
+
+carray-fdt_early_drivers-$(CONFIG_FDT_SUSPEND_SIFIVE_SMC0) += fdt_suspend_sifive_smc0
+libsbiutils-objs-$(CONFIG_FDT_SUSPEND_SIFIVE_SMC0) += suspend/fdt_suspend_sifive_smc0.o
platform/generic/configs/defconfig +1 -1
index 837acd79..258988cc 100644
@@ -55,6 +55,7 @@ CONFIG_FDT_SERIAL_XILINX_UARTLITE=y
 CONFIG_SERIAL_SEMIHOSTING=y
 CONFIG_FDT_SUSPEND=y
 CONFIG_FDT_SUSPEND_RPMI=y
+CONFIG_FDT_SUSPEND_SIFIVE_SMC0=y
 CONFIG_FDT_TIMER=y
 CONFIG_FDT_TIMER_MTIMER=y
 CONFIG_FDT_TIMER_PLMT=y
--

2.17.1

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi