Skip to contentSkip to conversation

RPMI and SBI MPXY support for OpenSBI

PATCH Superseded Anup Patel 4 participants 25 messages Aug 6, 2024 – Oct 11, 2024 Arm/Allwinner sunXi SoC support

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

cover RPMI and SBI MPXY support for OpenSBI

This series adds RPMI and SBI MPXY support for OpenSBI. The RPMI service groups supported include system reset, system suspend, HSM, CPPC, and clock. The RPMI clock serivice group is exposed to supervisor software as an MPXY channel.

These patches can also be found in the rpmi_mpxy_v1 branch at:
https://github.com/avpatel/opensbi.git

To test these patches, use the dev-upstream branch of following repos:
* https://github.com/ventanamicro/qemu.git
* https://github.com/ventanamicro/linux.git

To enable QEMU RPMI emulation (using librpmi), use "virt,rpmi=on" as the QEMU machine name.

Anup Patel (7):
lib: utils/mailbox: Add generic mailbox library
lib: utils/mailbox: Add simple FDT based mailbox framework
lib: utils: Add simple FDT based system suspend driver framework
lib: utils: Add simple FDT based HSM driver framework
lib: utils: Add simple FDT based CPPC driver framework
lib: sbi: Implement SBI MPXY extension
lib: utils: Add simple FDT based MPXY driver framework

Rahul Pathak (5):
lib: Increase ROOT_REGION_MAX to accomodate more memregions lib/utils: Add RPMI messaging protocol and shared memory transport
support
lib/utils: reset: Add RPMI System Reset driver
lib: sbi: Add SBI Message Proxy (MPXY) framework
lib: utils/mpxy: Add RPMI client driver for MPXY

Subrahmanya Lingappa (4):
lib: utils/suspend: Add RPMI system suspend driver
lib: sbi: Add optional resume address to hart suspend
lib: utils/hsm: Add RPMI HSM driver
lib: utils/cppc: Add RPMI CPPC driver

 include/sbi/sbi_ecall_interface.h            |  13 +
 include/sbi/sbi_error.h                      |  14 +-
 include/sbi/sbi_hsm.h                        |   6 +-
 include/sbi/sbi_mpxy.h                       | 181 +++++
 include/sbi/sbi_platform.h                   |  17 +
 include/sbi_utils/cppc/fdt_cppc.h            |  35 +
 include/sbi_utils/hsm/fdt_hsm.h              |  39 ++
 include/sbi_utils/mailbox/fdt_mailbox.h      |  36 +
 include/sbi_utils/mailbox/mailbox.h          | 170 +++++
 include/sbi_utils/mailbox/rpmi_mailbox.h     |  32 +
 include/sbi_utils/mailbox/rpmi_msgprot.h     | 513 ++++++++++++++
 include/sbi_utils/mpxy/fdt_mpxy.h            |  31 +
 include/sbi_utils/suspend/fdt_suspend.h      |  45 ++
 lib/sbi/Kconfig                              |   3 +
 lib/sbi/objects.mk                           |   4 +
 lib/sbi/sbi_domain.c                         |   2 +-
 lib/sbi/sbi_ecall_mpxy.c                     |  68 ++
 lib/sbi/sbi_hsm.c                            |   6 +-
 lib/sbi/sbi_init.c                           |   6 +
 lib/sbi/sbi_mpxy.c                           | 644 ++++++++++++++++++
 lib/utils/Kconfig                            |  10 +
 lib/utils/cppc/Kconfig                       |  19 +
 lib/utils/cppc/fdt_cppc.c                    |  82 +++
 lib/utils/cppc/fdt_cppc_drivers.carray       |   3 +
 lib/utils/cppc/fdt_cppc_rpmi.c               | 287 ++++++++
 lib/utils/cppc/objects.mk                    |  14 +
 lib/utils/hsm/Kconfig                        |  19 +
 lib/utils/hsm/fdt_hsm.c                      |  89 +++
 lib/utils/hsm/fdt_hsm_drivers.carray         |   3 +
 lib/utils/hsm/fdt_hsm_rpmi.c                 | 351 ++++++++++
 lib/utils/hsm/objects.mk                     |  14 +
 lib/utils/mailbox/Kconfig                    |  29 +
 lib/utils/mailbox/fdt_mailbox.c              | 142 ++++
 lib/utils/mailbox/fdt_mailbox_drivers.carray |   3 +
 lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c   | 671 +++++++++++++++++++
 lib/utils/mailbox/mailbox.c                  | 116 ++++
 lib/utils/mailbox/objects.mk                 |  18 +
 lib/utils/mailbox/rpmi_mailbox.c             |  79 +++
 lib/utils/mpxy/Kconfig                       |  19 +
 lib/utils/mpxy/fdt_mpxy.c                    |  50 ++
 lib/utils/mpxy/fdt_mpxy_drivers.carray       |   3 +
 lib/utils/mpxy/fdt_mpxy_rpmi_mbox.c          | 408 +++++++++++
 lib/utils/mpxy/objects.mk                    |  14 +
 lib/utils/reset/Kconfig                      |   5 +
 lib/utils/reset/fdt_reset_rpmi.c             | 141 ++++
 lib/utils/reset/objects.mk                   |   3 +
 lib/utils/suspend/Kconfig                    |  19 +
 lib/utils/suspend/fdt_suspend.c              |  46 ++
 lib/utils/suspend/fdt_suspend_drivers.carray |   3 +
 lib/utils/suspend/fdt_suspend_rpmi.c         | 137 ++++
 lib/utils/suspend/objects.mk                 |  14 +
 platform/generic/allwinner/sun20i-d1.c       |   2 +-
 platform/generic/configs/defconfig           |  12 +
 platform/generic/platform.c                  |  13 +
 54 files changed, 4691 insertions(+), 12 deletions(-)
 create mode 100644 include/sbi/sbi_mpxy.h
 create mode 100644 include/sbi_utils/cppc/fdt_cppc.h
 create mode 100644 include/sbi_utils/hsm/fdt_hsm.h
 create mode 100644 include/sbi_utils/mailbox/fdt_mailbox.h
 create mode 100644 include/sbi_utils/mailbox/mailbox.h
 create mode 100644 include/sbi_utils/mailbox/rpmi_mailbox.h
 create mode 100644 include/sbi_utils/mailbox/rpmi_msgprot.h
 create mode 100644 include/sbi_utils/mpxy/fdt_mpxy.h
 create mode 100644 include/sbi_utils/suspend/fdt_suspend.h
 create mode 100644 lib/sbi/sbi_ecall_mpxy.c
 create mode 100644 lib/sbi/sbi_mpxy.c
 create mode 100644 lib/utils/cppc/Kconfig
 create mode 100644 lib/utils/cppc/fdt_cppc.c
 create mode 100644 lib/utils/cppc/fdt_cppc_drivers.carray
 create mode 100644 lib/utils/cppc/fdt_cppc_rpmi.c
 create mode 100644 lib/utils/cppc/objects.mk
 create mode 100644 lib/utils/hsm/Kconfig
 create mode 100644 lib/utils/hsm/fdt_hsm.c
 create mode 100644 lib/utils/hsm/fdt_hsm_drivers.carray
 create mode 100644 lib/utils/hsm/fdt_hsm_rpmi.c
 create mode 100644 lib/utils/hsm/objects.mk
 create mode 100644 lib/utils/mailbox/Kconfig
 create mode 100644 lib/utils/mailbox/fdt_mailbox.c
 create mode 100644 lib/utils/mailbox/fdt_mailbox_drivers.carray
 create mode 100644 lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c
 create mode 100644 lib/utils/mailbox/mailbox.c
 create mode 100644 lib/utils/mailbox/objects.mk
 create mode 100644 lib/utils/mailbox/rpmi_mailbox.c
 create mode 100644 lib/utils/mpxy/Kconfig
 create mode 100644 lib/utils/mpxy/fdt_mpxy.c
 create mode 100644 lib/utils/mpxy/fdt_mpxy_drivers.carray
 create mode 100644 lib/utils/mpxy/fdt_mpxy_rpmi_mbox.c
 create mode 100644 lib/utils/mpxy/objects.mk
 create mode 100644 lib/utils/reset/fdt_reset_rpmi.c
 create mode 100644 lib/utils/suspend/Kconfig
 create mode 100644 lib/utils/suspend/fdt_suspend.c
 create mode 100644 lib/utils/suspend/fdt_suspend_drivers.carray
 create mode 100644 lib/utils/suspend/fdt_suspend_rpmi.c
 create mode 100644 lib/utils/suspend/objects.mk
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/16 lib: Increase ROOT_REGION_MAX to accomodate more memregions

From: Rahul Pathak <[email protected]>

As more drivers adding memregions into root domain, the current static limit of ROOT_REGION_MAX is not sufficient. Increase the limit to accomodate more memregions.

---
 lib/sbi/sbi_domain.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_domain.c +1 -1
index 374ac36b..65649169 100644
@@ -26,7 +26,7 @@ struct sbi_domain *domidx_to_domain_table[SBI_DOMAIN_MAX_INDEX + 1] = { 0 };
 static u32 domain_count = 0;
 static bool domain_finalized = false;
 
-#define ROOT_REGION_MAX	16
+#define ROOT_REGION_MAX	32
 static u32 root_memregs_count = 0;
 
 struct sbi_domain root = {
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/16 lib: utils/mailbox: Add generic mailbox library

In reply to Anup Patel

Add generic mailbox library which is independent of hardware description format. The OpenSBI platform support or mailbox drivers can register mailbox controller instances which can be discovered and used by different mailbox client drivers. Each mailbox controller instance has a unique ID which can be used by mailbox client drivers for find the mailbox controller instance. The mailbox client drivers will typically request a mailbox channel from the mailbox controller and use it to do data transfer with the remote end of mailbox channel.

---
 include/sbi_utils/mailbox/mailbox.h | 170 ++++++++++++++++++++++++++++
 lib/utils/Kconfig                   |   2 +
 lib/utils/mailbox/Kconfig           |   9 ++
 lib/utils/mailbox/mailbox.c         | 116 +++++++++++++++++++
 lib/utils/mailbox/objects.mk        |  10 ++
 5 files changed, 307 insertions(+)
 create mode 100644 include/sbi_utils/mailbox/mailbox.h
 create mode 100644 lib/utils/mailbox/Kconfig
 create mode 100644 lib/utils/mailbox/mailbox.c
 create mode 100644 lib/utils/mailbox/objects.mk
include/sbi_utils/mailbox/mailbox.h new file +170 -0
new file mode 100644
index 00000000..7206ce81
@@ -0,0 +1,170 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#ifndef __MAILBOX_H__
+#define __MAILBOX_H__
+
+#include <sbi/sbi_types.h>
+#include <sbi/sbi_list.h>
+#include <sbi/riscv_atomic.h>
+
+/** Representation of a mailbox channel */
+struct mbox_chan {
+	/** List head */
+	struct sbi_dlist node;
+	/** Pointer to the mailbox controller */
+	struct mbox_controller *mbox;
+	/**
+	 * Arguments (or parameters) to identify a mailbox channel
+	 * within a mailbox controller.
+	 */
+#define MBOX_CHAN_MAX_ARGS	2
+	u32 chan_args[MBOX_CHAN_MAX_ARGS];
+};
+
+#define to_mbox_chan(__node)	\
+	container_of((__node), struct mbox_chan, node)
+
+/**
+ * Representation of a mailbox data transfer
+ *
+ * NOTE: If both "tx" and "rx" are non-NULL then Tx is done before Rx.
+ */
+struct mbox_xfer {
+#define MBOX_XFER_SEQ			(1UL << 0)
+	/** Transfer flags */
+	unsigned long flags;
+	/** Transfer arguments (or parameters) */
+	void *args;
+	/**
+	 * Sequence number
+	 *
+	 * If MBOX_XFER_SEQ is not set in flags then mbox_chan_xfer()
+	 * will generate a unique sequence number and update this field
+	 * else mbox_chan_xfer() will blindly use the sequence number
+	 * specified by this field.
+	 */
+	long seq;
+	/** Send data pointer */
+	void *tx;
+	/** Send data length (valid only if tx != NULL) */
+	unsigned long tx_len;
+	/**
+	 * Send timeout milliseconds (valid only if tx != NULL)
+	 *
+	 * If this field is non-zero along with tx != NULL then the
+	 * mailbox controller driver will wait specified milliseconds
+	 * for send data transfer to complete else the mailbox controller
+	 * driver will not wait.
+	 */
+	unsigned long tx_timeout;
+	/** Receive data pointer */
+	void *rx;
+	/** Receive data length (valid only if rx != NULL) */
+	unsigned long rx_len;
+	/**
+	 * Receive timeout milliseconds (valid only if rx != NULL)
+	 *
+	 * If this field is non-zero along with rx != NULL then the
+	 * mailbox controller driver will wait specified milliseconds
+	 * for receive data transfer to complete else the mailbox
+	 * controller driver will not wait.
+	 */
+	unsigned long rx_timeout;
+};
+
+#define mbox_xfer_init_tx(__p, __a, __t, __t_len, __t_tim)		\
+do {									\
+	(__p)->flags = 0;						\
+	(__p)->args = (__a);						\
+	(__p)->tx = (__t);						\
+	(__p)->tx_len = (__t_len);					\
+	(__p)->tx_timeout = (__t_tim);					\
+	(__p)->rx = NULL;						\
+	(__p)->rx_len = 0;						\
+	(__p)->rx_timeout = 0;						\
+} while (0)
+
+#define mbox_xfer_init_rx(__p, __a, __r, __r_len, __r_tim)		\
+do {									\
+	(__p)->flags = 0;						\
+	(__p)->args = (__a);						\
+	(__p)->tx = NULL;						\
+	(__p)->tx_len = 0;						\
+	(__p)->tx_timeout = 0;						\
+	(__p)->rx = (__r);						\
+	(__p)->rx_len = (__r_len);					\
+	(__p)->rx_timeout = (__r_tim);					\
+} while (0)
+
+#define mbox_xfer_init_txrx(__p, __a, __t, __t_len, __t_tim, __r, __r_len, __r_tim)\
+do {									\
+	(__p)->flags = 0;						\
+	(__p)->args = (__a);						\
+	(__p)->tx = (__t);						\
+	(__p)->tx_len = (__t_len);					\
+	(__p)->tx_timeout = (__t_tim);					\
+	(__p)->rx = (__r);						\
+	(__p)->rx_len = (__r_len);					\
+	(__p)->rx_timeout = (__r_tim);					\
+} while (0)
+
+#define mbox_xfer_set_sequence(__p, __seq)				\
+do {									\
+	(__p)->flags |= MBOX_XFER_SEQ;					\
+	(__p)->seq = (__seq);						\
+} while (0)
+
+/** Representation of a mailbox controller */
+struct mbox_controller {
+	/** List head */
+	struct sbi_dlist node;
+	/** Next sequence atomic counter */
+	atomic_t xfer_next_seq;
+	/* List of mailbox channels */
+	struct sbi_dlist chan_list;
+	/** Unique ID of the mailbox controller assigned by the driver */
+	unsigned int id;
+	/** Maximum length of transfer supported by the mailbox controller */
+	unsigned int max_xfer_len;
+	/** Pointer to mailbox driver owning this mailbox controller */
+	void *driver;
+	/** Request a mailbox channel from the mailbox controller */
+	struct mbox_chan *(*request_chan)(struct mbox_controller *mbox,
+					  u32 *chan_args);
+	/** Free a mailbox channel from the mailbox controller */
+	void *(*free_chan)(struct mbox_controller *mbox,
+			   struct mbox_chan *chan);
+	/** Transfer data over mailbox channel */
+	int (*xfer)(struct mbox_chan *chan, struct mbox_xfer *xfer);
+};
+
+#define to_mbox_controller(__node)	\
+	container_of((__node), struct mbox_controller, node)
+
+/** Find a registered mailbox controller */
+struct mbox_controller *mbox_controller_find(unsigned int id);
+
+/** Register mailbox controller */
+int mbox_controller_add(struct mbox_controller *mbox);
+
+/** Un-register mailbox controller */
+void mbox_controller_remove(struct mbox_controller *mbox);
+
+/** Request a mailbox channel */
+struct mbox_chan *mbox_controller_request_chan(struct mbox_controller *mbox,
+					       u32 *chan_args);
+
+/** Free a mailbox channel */
+void mbox_controller_free_chan(struct mbox_chan *chan);
+
+/** Data transfer over mailbox channel */
+int mbox_chan_xfer(struct mbox_chan *chan, struct mbox_xfer *xfer);
+
+#endif
lib/utils/Kconfig +2 -0
index de8b4eb9..6aa7843c 100644
@@ -14,6 +14,8 @@ source "$(OPENSBI_SRC_DIR)/lib/utils/irqchip/Kconfig"
 
 source "$(OPENSBI_SRC_DIR)/lib/utils/libfdt/Kconfig"
 
+source "$(OPENSBI_SRC_DIR)/lib/utils/mailbox/Kconfig"
+
 source "$(OPENSBI_SRC_DIR)/lib/utils/regmap/Kconfig"
 
 source "$(OPENSBI_SRC_DIR)/lib/utils/reset/Kconfig"
lib/utils/mailbox/Kconfig new file +9 -0
new file mode 100644
index 00000000..f91a9bb9
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+menu "Mailbox Support"
+
+config MAILBOX
+	bool "Mailbox support"
+	default n
+
+endmenu
lib/utils/mailbox/mailbox.c new file +116 -0

116 lines not on this page · raw message

lib/utils/mailbox/objects.mk new file +10 -0
new file mode 100644
index 00000000..79b27e4c
@@ -0,0 +1,10 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2024 Ventana Micro Systems Inc.
+#
+# Authors:
+#   Anup Patel <[email protected]>
+#
+
+libsbiutils-objs-$(CONFIG_MAILBOX) += mailbox/mailbox.o
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 3/16 lib: utils/mailbox: Add simple FDT based mailbox framework

In reply to Anup Patel

Add a simple FDT based mailbox framework which is built on top of the generic mailbox library. The phandle of FDT mailbox DT node is treated as the unique mailbox controller ID which is required by the generic mailbox library. The FDT based mailbox drivers will be probed on-demand from fdt_mailbox_request_chan() called by the mailbox client drivers.

---
 include/sbi_utils/mailbox/fdt_mailbox.h      |  36 +++++
 lib/utils/mailbox/Kconfig                    |   6 +
 lib/utils/mailbox/fdt_mailbox.c              | 142 +++++++++++++++++++
 lib/utils/mailbox/fdt_mailbox_drivers.carray |   3 +
 lib/utils/mailbox/objects.mk                 |   3 +
 5 files changed, 190 insertions(+)
 create mode 100644 include/sbi_utils/mailbox/fdt_mailbox.h
 create mode 100644 lib/utils/mailbox/fdt_mailbox.c
 create mode 100644 lib/utils/mailbox/fdt_mailbox_drivers.carray
include/sbi_utils/mailbox/fdt_mailbox.h new file +36 -0
new file mode 100644
index 00000000..27f425db
@@ -0,0 +1,36 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#ifndef __FDT_MAILBOX_H__
+#define __FDT_MAILBOX_H__
+
+#include <sbi_utils/mailbox/mailbox.h>
+
+struct fdt_phandle_args;
+
+/** FDT based mailbox driver */
+struct fdt_mailbox {
+	const struct fdt_match *match_table;
+	int (*xlate)(struct mbox_controller *mbox,
+		     const struct fdt_phandle_args *pargs,
+		     u32 *out_chan_args);
+	int (*init)(void *fdt, int nodeoff, u32 phandle,
+		    const struct fdt_match *match);
+};
+
+/** Request a mailbox channel using "mboxes" DT property of client DT node */
+int fdt_mailbox_request_chan(void *fdt, int nodeoff, int index,
+			     struct mbox_chan **out_chan);
+
+/** Simple xlate function to convert one mailbox FDT cell into channel args */
+int fdt_mailbox_simple_xlate(struct mbox_controller *mbox,
+			     const struct fdt_phandle_args *pargs,
+			     u32 *out_chan_args);
+
+#endif
lib/utils/mailbox/Kconfig +6 -0
index f91a9bb9..3957bfba 100644
@@ -2,6 +2,12 @@
 
 menu "Mailbox Support"
 
+config FDT_MAILBOX
+	bool "FDT based mailbox drivers"
+	depends on FDT
+	select MAILBOX
+	default n
+
 config MAILBOX
 	bool "Mailbox support"
 	default n
lib/utils/mailbox/fdt_mailbox.c new file +142 -0

142 lines not on this page · raw message

lib/utils/mailbox/fdt_mailbox_drivers.carray new file +3 -0
new file mode 100644
index 00000000..fd4246df
@@ -0,0 +1,3 @@
+HEADER: sbi_utils/mailbox/fdt_mailbox.h
+TYPE: struct fdt_mailbox
+NAME: fdt_mailbox_drivers
lib/utils/mailbox/objects.mk +3 -0
index 79b27e4c..2135898c 100644
@@ -7,4 +7,7 @@
 #   Anup Patel <[email protected]>
 #
 
+libsbiutils-objs-$(CONFIG_FDT_MAILBOX) += mailbox/fdt_mailbox.o
+libsbiutils-objs-$(CONFIG_FDT_MAILBOX) += mailbox/fdt_mailbox_drivers.carray.o
+
 libsbiutils-objs-$(CONFIG_MAILBOX) += mailbox/mailbox.o
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH 4/16 lib/utils: Add RPMI messaging protocol and shared memory transport support

In reply to Anup Patel

From: Rahul Pathak <[email protected]>

The RISC-V Platform Management Interface (RPMI) defines a messaging protocol and shared memory based transport for bi-directional communication with an on-chip or external microcontroller.

To support RPMI in OpenSBI, add:
1) The RPMI messaging protocol defines and helper macros
2) A FDT mailbox driver for the RPMI shared memory transport

---
 include/sbi_utils/mailbox/rpmi_mailbox.h   |  32 +
 include/sbi_utils/mailbox/rpmi_msgprot.h   | 186 ++++++
 lib/utils/mailbox/Kconfig                  |  14 +
 lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c | 671 +++++++++++++++++++++
 lib/utils/mailbox/objects.mk               |   5 +
 lib/utils/mailbox/rpmi_mailbox.c           |  79 +++
 platform/generic/configs/defconfig         |   3 +
 7 files changed, 990 insertions(+)
 create mode 100644 include/sbi_utils/mailbox/rpmi_mailbox.h
 create mode 100644 include/sbi_utils/mailbox/rpmi_msgprot.h
 create mode 100644 lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c
 create mode 100644 lib/utils/mailbox/rpmi_mailbox.c
include/sbi_utils/mailbox/rpmi_mailbox.h new file +32 -0
new file mode 100644
index 00000000..61af51a8
@@ -0,0 +1,32 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#ifndef __RPMI_MAILBOX_H__
+#define __RPMI_MAILBOX_H__
+
+#include <sbi/sbi_error.h>
+#include <sbi_utils/mailbox/rpmi_msgprot.h>
+
+#define rpmi_u32_count(__var)	(sizeof(__var) / sizeof(u32))
+
+/** Convert RPMI error to SBI error */
+int rpmi_xlate_error(enum rpmi_error error);
+
+/** Typical RPMI normal request with at least status code in response */
+int rpmi_normal_request_with_status(
+			struct mbox_chan *chan, u32 service_id,
+			void *req, u32 req_words, u32 req_endian_words,
+			void *resp, u32 resp_words, u32 resp_endian_words);
+
+/* RPMI posted request which is without any response*/
+int rpmi_posted_request(
+		struct mbox_chan *chan, u32 service_id,
+		void *req, u32 req_words, u32 req_endian_words);
+
+#endif /* !__RPMI_MAILBOX_H__ */
include/sbi_utils/mailbox/rpmi_msgprot.h new file +186 -0

186 lines not on this page · raw message

lib/utils/mailbox/Kconfig +14 -0
index 3957bfba..6e7f2cdd 100644
@@ -8,8 +8,22 @@ config FDT_MAILBOX
 	select MAILBOX
 	default n
 
+config RPMI_MAILBOX
+	bool "RPMI based mailbox drivers"
+	select MAILBOX
+	default n
+
 config MAILBOX
 	bool "Mailbox support"
 	default n
 
+if FDT_MAILBOX
+
+config FDT_MAILBOX_RPMI_SHMEM
+	bool "RPMI Shared Memory Mailbox Controller"
+	depends on RPMI_MAILBOX
+	default n
+
+endif
+
 endmenu
lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c new file +671 -0

671 lines not on this page · raw message

lib/utils/mailbox/objects.mk +5 -0
index 2135898c..746b0313 100644
@@ -11,3 +11,8 @@ libsbiutils-objs-$(CONFIG_FDT_MAILBOX) += mailbox/fdt_mailbox.o
 libsbiutils-objs-$(CONFIG_FDT_MAILBOX) += mailbox/fdt_mailbox_drivers.carray.o
 
 libsbiutils-objs-$(CONFIG_MAILBOX) += mailbox/mailbox.o
+
+libsbiutils-objs-$(CONFIG_RPMI_MAILBOX) += mailbox/rpmi_mailbox.o
+
+carray-fdt_mailbox_drivers-$(CONFIG_FDT_MAILBOX_RPMI_SHMEM) += fdt_mailbox_rpmi_shmem
+libsbiutils-objs-$(CONFIG_FDT_MAILBOX_RPMI_SHMEM) += mailbox/fdt_mailbox_rpmi_shmem.o
lib/utils/mailbox/rpmi_mailbox.c new file +79 -0

79 lines not on this page · raw message

platform/generic/configs/defconfig +3 -0
index 079bc4fe..233a9a89 100644
@@ -20,6 +20,9 @@ CONFIG_FDT_IRQCHIP=y
 CONFIG_FDT_IRQCHIP_APLIC=y
 CONFIG_FDT_IRQCHIP_IMSIC=y
 CONFIG_FDT_IRQCHIP_PLIC=y
+CONFIG_FDT_MAILBOX=y
+CONFIG_RPMI_MAILBOX=y
+CONFIG_FDT_MAILBOX_RPMI_SHMEM=y
 CONFIG_FDT_REGMAP=y
 CONFIG_FDT_REGMAP_SYSCON=y
 CONFIG_FDT_RESET=y
Signature · 1 line
2.34.1
author
#8 lore raw

PATCH 5/16 lib/utils: reset: Add RPMI System Reset driver

In reply to Anup Patel

From: Rahul Pathak <[email protected]>

Add RPMI based driver for system reset and enable it in the generic platform defconfig

---
 include/sbi_utils/mailbox/rpmi_msgprot.h |  26 +++++
 lib/utils/reset/Kconfig                  |   5 +
 lib/utils/reset/fdt_reset_rpmi.c         | 141 +++++++++++++++++++++++
 lib/utils/reset/objects.mk               |   3 +
 platform/generic/configs/defconfig       |   1 +
 5 files changed, 176 insertions(+)
 create mode 100644 lib/utils/reset/fdt_reset_rpmi.c
include/sbi_utils/mailbox/rpmi_msgprot.h +26 -0
index e0c7cba0..4ede28dd 100644
@@ -148,6 +148,7 @@ struct rpmi_message_args {
 enum rpmi_servicegroup_id {
 	RPMI_SRVGRP_ID_MIN = 0,
 	RPMI_SRVGRP_BASE = 0x00001,
+	RPMI_SRVGRP_SYSTEM_RESET = 0x00002,
 	RPMI_SRVGRP_ID_MAX_COUNT,
 };
 
@@ -183,4 +184,29 @@ struct rpmi_base_get_attributes_resp {
 	u32 f3;
 };
 
+/** RPMI System Reset ServiceGroup Service IDs */
+enum rpmi_system_reset_service_id {
+	RPMI_SYSRST_SRV_ENABLE_NOTIFICATION = 0x01,
+	RPMI_SYSRST_SRV_GET_SYSTEM_RESET_ATTRIBUTES = 0x02,
+	RPMI_SYSRST_SRV_SYSTEM_RESET = 0x03,
+	RPMI_SYSRST_SRV_ID_MAX_COUNT,
+};
+
+/** RPMI System Reset types */
+enum rpmi_sysrst_reset_type {
+	RPMI_SYSRST_SHUTDOWN = 0,
+	RPMI_SYSRST_COLD_RESET = 1,
+	RPMI_SYSRST_WARM_RESET = 2,
+	RPMI_SYSRST_MAX_IDN_COUNT,
+};
+
+/** Response for system reset attributes */
+struct rpmi_sysrst_get_reset_attributes_resp {
+	s32 status;
+#define RPMI_SYSRST_FLAGS_SUPPORTED_POS		(31)
+#define RPMI_SYSRST_FLAGS_SUPPORTED_MASK		\
+			(1U << RPMI_SYSRST_FLAGS_SUPPORTED_POS)
+	u32 flags;
+};
+
 #endif /* !__RPMI_MSGPROT_H__ */
lib/utils/reset/Kconfig +5 -0
index 6c077fe7..68e66716 100644
@@ -24,6 +24,11 @@ config FDT_RESET_HTIF
 	select SYS_HTIF
 	default n
 
+config FDT_RESET_RPMI
+	bool "RPMI FDT reset driver"
+	depends on FDT_MAILBOX && RPMI_MAILBOX
+	default n
+
 config FDT_RESET_SG2042_HWMON_MCU
 	bool "Sophgo SG2042 hwmon MCU FDT reset driver"
 	default n
lib/utils/reset/fdt_reset_rpmi.c new file +141 -0

141 lines not on this page · raw message

lib/utils/reset/objects.mk +3 -0
index a84336cf..8f3774c5 100644
@@ -29,3 +29,6 @@ libsbiutils-objs-$(CONFIG_FDT_RESET_SUNXI_WDT) += reset/fdt_reset_sunxi_wdt.o
 carray-fdt_reset_drivers-$(CONFIG_FDT_RESET_SYSCON) += fdt_syscon_poweroff
 carray-fdt_reset_drivers-$(CONFIG_FDT_RESET_SYSCON) += fdt_syscon_reboot
 libsbiutils-objs-$(CONFIG_FDT_RESET_SYSCON) += reset/fdt_reset_syscon.o
+
+carray-fdt_reset_drivers-$(CONFIG_FDT_RESET_RPMI) += fdt_reset_rpmi
+libsbiutils-objs-$(CONFIG_FDT_RESET_RPMI) += reset/fdt_reset_rpmi.o
platform/generic/configs/defconfig +1 -0
index 233a9a89..48f8df4c 100644
@@ -29,6 +29,7 @@ CONFIG_FDT_RESET=y
 CONFIG_FDT_RESET_ATCWDT200=y
 CONFIG_FDT_RESET_GPIO=y
 CONFIG_FDT_RESET_HTIF=y
+CONFIG_FDT_RESET_RPMI=y
 CONFIG_FDT_RESET_SUNXI_WDT=y
 CONFIG_FDT_RESET_SG2042_HWMON_MCU=y
 CONFIG_FDT_RESET_SYSCON=y
Signature · 1 line
2.34.1
author
#9 lore raw

PATCH 6/16 lib: utils: Add simple FDT based system suspend driver framework

In reply to Anup Patel

The generic platform can have multiple system suspend drivers so add a simple FDT based system suspend driver framework.

---
 include/sbi_utils/suspend/fdt_suspend.h      | 45 +++++++++++++++++++
 lib/utils/Kconfig                            |  2 +
 lib/utils/suspend/Kconfig                    | 10 +++++
 lib/utils/suspend/fdt_suspend.c              | 46 ++++++++++++++++++++
 lib/utils/suspend/fdt_suspend_drivers.carray |  3 ++
 lib/utils/suspend/objects.mk                 | 11 +++++
 platform/generic/configs/defconfig           |  1 +
 platform/generic/platform.c                  |  2 +
 8 files changed, 120 insertions(+)
 create mode 100644 include/sbi_utils/suspend/fdt_suspend.h
 create mode 100644 lib/utils/suspend/Kconfig
 create mode 100644 lib/utils/suspend/fdt_suspend.c
 create mode 100644 lib/utils/suspend/fdt_suspend_drivers.carray
 create mode 100644 lib/utils/suspend/objects.mk
include/sbi_utils/suspend/fdt_suspend.h new file +45 -0
new file mode 100644
index 00000000..cb6fb719
@@ -0,0 +1,45 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#ifndef __FDT_SUSPEND_H__
+#define __FDT_SUSPEND_H__
+
+#include <sbi/sbi_types.h>
+
+struct fdt_suspend {
+	const struct fdt_match *match_table;
+	int (*init)(void *fdt, int nodeoff, const struct fdt_match *match);
+};
+
+#ifdef CONFIG_FDT_SUSPEND
+
+/**
+ * fdt_suspend_driver_init() - initialize suspend driver based on the device-tree
+ */
+int fdt_suspend_driver_init(void *fdt, struct fdt_suspend *drv);
+
+/**
+ * fdt_suspend_init() - initialize reset drivers based on the device-tree
+ *
+ * This function shall be invoked in final init.
+ */
+void fdt_suspend_init(void);
+
+#else
+
+static inline int fdt_suspend_driver_init(void *fdt, struct fdt_suspend *drv)
+{
+	return 0;
+}
+
+static inline void fdt_suspend_init(void) { }
+
+#endif
+
+#endif
lib/utils/Kconfig +2 -0
index 6aa7843c..3f32c1ca 100644
@@ -22,6 +22,8 @@ source "$(OPENSBI_SRC_DIR)/lib/utils/reset/Kconfig"
 
 source "$(OPENSBI_SRC_DIR)/lib/utils/serial/Kconfig"
 
+source "$(OPENSBI_SRC_DIR)/lib/utils/suspend/Kconfig"
+
 source "$(OPENSBI_SRC_DIR)/lib/utils/sys/Kconfig"
 
 source "$(OPENSBI_SRC_DIR)/lib/utils/timer/Kconfig"
lib/utils/suspend/Kconfig new file +10 -0
new file mode 100644
index 00000000..416ae795
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+menu "System Suspend Support"
+
+config FDT_SUSPEND
+	bool "FDT based suspend drivers"
+	depends on FDT
+	default n
+
+endmenu
lib/utils/suspend/fdt_suspend.c new file +46 -0

46 lines not on this page · raw message

lib/utils/suspend/fdt_suspend_drivers.carray new file +3 -0
new file mode 100644
index 00000000..8793e4cf
@@ -0,0 +1,3 @@
+HEADER: sbi_utils/suspend/fdt_suspend.h
+TYPE: struct fdt_suspend
+NAME: fdt_suspend_drivers
lib/utils/suspend/objects.mk new file +11 -0
new file mode 100644
index 00000000..30d897d1
@@ -0,0 +1,11 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2024 Ventana Micro Systems Inc.
+#
+# Authors:
+#   Anup Patel <[email protected]>
+#
+
+libsbiutils-objs-$(CONFIG_FDT_SUSPEND) += suspend/fdt_suspend.o
+libsbiutils-objs-$(CONFIG_FDT_SUSPEND) += suspend/fdt_suspend_drivers.carray.o
platform/generic/configs/defconfig +1 -0
index 48f8df4c..ec439d74 100644
@@ -44,6 +44,7 @@ CONFIG_FDT_SERIAL_LITEX=y
 CONFIG_FDT_SERIAL_UART8250=y
 CONFIG_FDT_SERIAL_XILINX_UARTLITE=y
 CONFIG_SERIAL_SEMIHOSTING=y
+CONFIG_FDT_SUSPEND=y
 CONFIG_FDT_TIMER=y
 CONFIG_FDT_TIMER_MTIMER=y
 CONFIG_FDT_TIMER_PLMT=y
platform/generic/platform.c +2 -0
index 3470474a..138a54c2 100644
@@ -24,6 +24,7 @@
 #include <sbi_utils/irqchip/fdt_irqchip.h>
 #include <sbi_utils/irqchip/imsic.h>
 #include <sbi_utils/serial/fdt_serial.h>
+#include <sbi_utils/suspend/fdt_suspend.h>
 #include <sbi_utils/timer/fdt_timer.h>
 #include <sbi_utils/ipi/fdt_ipi.h>
 #include <sbi_utils/reset/fdt_reset.h>
@@ -225,6 +226,7 @@ static int generic_early_init(bool cold_boot)
 
 	if (cold_boot) {
 		fdt_reset_init();
+		fdt_suspend_init();
 
 		if (semihosting_enabled())
 			rc = semihosting_init();
Signature · 1 line
2.34.1
author
#10 lore raw

PATCH 7/16 lib: utils/suspend: Add RPMI system suspend driver

In reply to Anup Patel

From: Subrahmanya Lingappa <[email protected]>

Add RPMI based system suspend driver.

To test this, execute the follwoing in Linux:

 $ echo mem > /sys/power/state

To wake up, execute the following command on qemu monitor terminal:
(qemu) system_wakeup

---
 include/sbi_utils/mailbox/rpmi_msgprot.h |  34 ++++++
 lib/utils/suspend/Kconfig                |   9 ++
 lib/utils/suspend/fdt_suspend_rpmi.c     | 137 +++++++++++++++++++++++
 lib/utils/suspend/objects.mk             |   3 +
 platform/generic/configs/defconfig       |   1 +
 5 files changed, 184 insertions(+)
 create mode 100644 lib/utils/suspend/fdt_suspend_rpmi.c
include/sbi_utils/mailbox/rpmi_msgprot.h +34 -0
index 4ede28dd..a2403c90 100644
@@ -5,6 +5,7 @@
  *
  * Authors:
  *   Rahul Pathak <[email protected]>
+ *   Subrahmanya Lingappa <[email protected]>
  */
 
 #ifndef __RPMI_MSGPROT_H__
@@ -149,6 +150,7 @@ enum rpmi_servicegroup_id {
 	RPMI_SRVGRP_ID_MIN = 0,
 	RPMI_SRVGRP_BASE = 0x00001,
 	RPMI_SRVGRP_SYSTEM_RESET = 0x00002,
+	RPMI_SRVGRP_SYSTEM_SUSPEND = 0x00003,
 	RPMI_SRVGRP_ID_MAX_COUNT,
 };
 
@@ -209,4 +211,36 @@ struct rpmi_sysrst_get_reset_attributes_resp {
 	u32 flags;
 };
 
+/** RPMI System Suspend ServiceGroup Service IDs */
+enum rpmi_system_suspend_service_id {
+	RPMI_SYSSUSP_SRV_ENABLE_NOTIFICATION = 0x01,
+	RPMI_SYSSUSP_SRV_GET_SYSTEM_SUSPEND_ATTRIBUTES = 0x02,
+	RPMI_SYSSUSP_SRV_SYSTEM_SUSPEND = 0x03,
+	RPMI_SYSSUSP_SRV_ID_MAX_COUNT,
+};
+
+/** Request for system suspend attributes */
+struct rpmi_syssusp_get_attr_req {
+	u32 susp_type;
+};
+
+/** Response for system suspend attributes */
+struct rpmi_syssusp_get_attr_resp {
+	s32 status;
+#define RPMI_SYSSUSP_FLAGS_CUSTOM_RESUME_ADDR_SUPPORTED	(1U << 31)
+#define RPMI_SYSSUSP_FLAGS_SUPPORTED			(1U << 30)
+	u32 flags;
+};
+
+struct rpmi_syssusp_suspend_req {
+	u32 hartid;
+	u32 suspend_type;
+	u32 resume_addr_lo;
+	u32 resume_addr_hi;
+};
+
+struct rpmi_syssusp_suspend_resp {
+	s32 status;
+};
+
 #endif /* !__RPMI_MSGPROT_H__ */
lib/utils/suspend/Kconfig +9 -0
index 416ae795..2cbea75c 100644
@@ -7,4 +7,13 @@ config FDT_SUSPEND
 	depends on FDT
 	default n
 
+if FDT_SUSPEND
+
+config FDT_SUSPEND_RPMI
+	bool "FDT RPMI suspend driver"
+	depends on FDT_MAILBOX && RPMI_MAILBOX
+	default n
+
+endif
+
 endmenu
lib/utils/suspend/fdt_suspend_rpmi.c new file +137 -0

137 lines not on this page · raw message

lib/utils/suspend/objects.mk +3 -0
index 30d897d1..657670a8 100644
@@ -9,3 +9,6 @@
 
 libsbiutils-objs-$(CONFIG_FDT_SUSPEND) += suspend/fdt_suspend.o
 libsbiutils-objs-$(CONFIG_FDT_SUSPEND) += suspend/fdt_suspend_drivers.carray.o
+
+carray-fdt_suspend_drivers-$(CONFIG_FDT_SUSPEND_RPMI) += fdt_suspend_rpmi
+libsbiutils-objs-$(CONFIG_FDT_SUSPEND_RPMI) += suspend/fdt_suspend_rpmi.o
platform/generic/configs/defconfig +1 -0
index ec439d74..54300fb5 100644
@@ -45,6 +45,7 @@ CONFIG_FDT_SERIAL_UART8250=y
 CONFIG_FDT_SERIAL_XILINX_UARTLITE=y
 CONFIG_SERIAL_SEMIHOSTING=y
 CONFIG_FDT_SUSPEND=y
+CONFIG_FDT_SUSPEND_RPMI=y
 CONFIG_FDT_TIMER=y
 CONFIG_FDT_TIMER_MTIMER=y
 CONFIG_FDT_TIMER_PLMT=y
Signature · 1 line
2.34.1
author
#11 lore raw

PATCH 8/16 lib: utils: Add simple FDT based HSM driver framework

In reply to Anup Patel

The generic platform can have multiple HSM drivers so add a simple FDT based HSM driver framework.

---
 include/sbi_utils/hsm/fdt_hsm.h      | 39 ++++++++++++
 lib/utils/Kconfig                    |  2 +
 lib/utils/hsm/Kconfig                | 10 ++++
 lib/utils/hsm/fdt_hsm.c              | 89 ++++++++++++++++++++++++++++
 lib/utils/hsm/fdt_hsm_drivers.carray |  3 +
 lib/utils/hsm/objects.mk             | 11 ++++
 platform/generic/configs/defconfig   |  1 +
 platform/generic/platform.c          |  6 ++
 8 files changed, 161 insertions(+)
 create mode 100644 include/sbi_utils/hsm/fdt_hsm.h
 create mode 100644 lib/utils/hsm/Kconfig
 create mode 100644 lib/utils/hsm/fdt_hsm.c
 create mode 100644 lib/utils/hsm/fdt_hsm_drivers.carray
 create mode 100644 lib/utils/hsm/objects.mk
include/sbi_utils/hsm/fdt_hsm.h new file +39 -0
new file mode 100644
index 00000000..7076873c
@@ -0,0 +1,39 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#ifndef __FDT_HSM_H__
+#define __FDT_HSM_H__
+
+#include <sbi/sbi_types.h>
+
+#ifdef CONFIG_FDT_HSM
+
+struct fdt_hsm {
+	const struct fdt_match *match_table;
+	int (*fdt_fixup)(void *fdt);
+	int (*cold_init)(void *fdt, int nodeoff, const struct fdt_match *match);
+	int (*warm_init)(void);
+	void (*exit)(void);
+};
+
+int fdt_hsm_fixup(void *fdt);
+
+void fdt_hsm_exit(void);
+
+int fdt_hsm_init(bool cold_boot);
+
+#else
+
+static inline int fdt_hsm_fixup(void *fdt) { return 0; }
+static inline void fdt_hsm_exit(void) { }
+static inline int fdt_hsm_init(bool cold_boot) { return 0; }
+
+#endif
+
+#endif
lib/utils/Kconfig +2 -0
index 3f32c1ca..c860a185 100644
@@ -6,6 +6,8 @@ source "$(OPENSBI_SRC_DIR)/lib/utils/fdt/Kconfig"
 
 source "$(OPENSBI_SRC_DIR)/lib/utils/gpio/Kconfig"
 
+source "$(OPENSBI_SRC_DIR)/lib/utils/hsm/Kconfig"
+
 source "$(OPENSBI_SRC_DIR)/lib/utils/i2c/Kconfig"
 
 source "$(OPENSBI_SRC_DIR)/lib/utils/ipi/Kconfig"
lib/utils/hsm/Kconfig new file +10 -0
new file mode 100644
index 00000000..31506116
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+menu "HSM Device Support"
+
+config FDT_HSM
+	bool "FDT based HSM drivers"
+	depends on FDT
+	default n
+
+endmenu
lib/utils/hsm/fdt_hsm.c new file +89 -0

89 lines not on this page · raw message

lib/utils/hsm/fdt_hsm_drivers.carray new file +3 -0
new file mode 100644
index 00000000..21396db2
@@ -0,0 +1,3 @@
+HEADER: sbi_utils/hsm/fdt_hsm.h
+TYPE: struct fdt_hsm
+NAME: fdt_hsm_drivers
lib/utils/hsm/objects.mk new file +11 -0
new file mode 100644
index 00000000..49337bf5
@@ -0,0 +1,11 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2024 Ventana Micro Systems Inc.
+#
+# Authors:
+#   Anup Patel <[email protected]>
+#
+
+libsbiutils-objs-$(CONFIG_FDT_HSM) += hsm/fdt_hsm.o
+libsbiutils-objs-$(CONFIG_FDT_HSM) += hsm/fdt_hsm_drivers.carray.o
platform/generic/configs/defconfig +1 -0
index 54300fb5..2efc7136 100644
@@ -10,6 +10,7 @@ CONFIG_FDT_GPIO=y
 CONFIG_FDT_GPIO_DESIGNWARE=y
 CONFIG_FDT_GPIO_SIFIVE=y
 CONFIG_FDT_GPIO_STARFIVE=y
+CONFIG_FDT_HSM=y
 CONFIG_FDT_I2C=y
 CONFIG_FDT_I2C_SIFIVE=y
 CONFIG_FDT_I2C_DW=y
platform/generic/platform.c +6 -0
index 138a54c2..bcd7133b 100644
@@ -21,6 +21,7 @@
 #include <sbi_utils/fdt/fdt_fixup.h>
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/fdt/fdt_pmu.h>
+#include <sbi_utils/hsm/fdt_hsm.h>
 #include <sbi_utils/irqchip/fdt_irqchip.h>
 #include <sbi_utils/irqchip/imsic.h>
 #include <sbi_utils/serial/fdt_serial.h>
@@ -236,6 +237,8 @@ static int generic_early_init(bool cold_boot)
 			return rc;
 	}
 
+	fdt_hsm_init(cold_boot);
+
 	if (!generic_plat || !generic_plat->early_init)
 		return 0;
 
@@ -261,6 +264,7 @@ static int generic_final_init(bool cold_boot)
 	fdt_cpu_fixup(fdt);
 	fdt_fixups(fdt);
 	fdt_domain_fixup(fdt);
+	fdt_hsm_fixup(fdt);
 
 	if (generic_plat && generic_plat->fdt_fixup) {
 		rc = generic_plat->fdt_fixup(fdt, generic_plat_match);
@@ -287,6 +291,8 @@ static int generic_vendor_ext_provider(long funcid,
 
 static void generic_early_exit(void)
 {
+	fdt_hsm_exit();
+
 	if (generic_plat && generic_plat->early_exit)
 		generic_plat->early_exit(generic_plat_match);
 }
Signature · 1 line
2.34.1
author
#12 lore raw

PATCH 9/16 lib: sbi: Add optional resume address to hart suspend

In reply to Anup Patel

From: Subrahmanya Lingappa <[email protected]>

Add an optional resume address to the platform specific hart suspend call.

---
 include/sbi/sbi_hsm.h                  | 6 +++++-
 lib/sbi/sbi_hsm.c                      | 6 +++---
 platform/generic/allwinner/sun20i-d1.c | 2 +-
 3 files changed, 9 insertions(+), 5 deletions(-)
include/sbi/sbi_hsm.h +5 -1
index 4b5601ba..aa692dfe 100644
@@ -39,8 +39,12 @@ struct sbi_hsm_device {
 	 *
 	 * For successful non-retentive suspend, the hart will resume from
 	 * the warm boot entry point.
+	 *
+	 * NOTE: mmode_resume_addr(resume address) is optional hence it
+	 * may or may not be honored by the platform. If its not honored
+	 * then platform must ensure to resume from the warmboot address.
 	 */
-	int (*hart_suspend)(u32 suspend_type);
+	int (*hart_suspend)(u32 suspend_type, ulong mmode_resume_addr);
 
 	/**
 	 * Perform platform-specific actions to resume from a suspended state.
lib/sbi/sbi_hsm.c +3 -3
index 3706acfb..2f518d44 100644
@@ -228,10 +228,10 @@ static int hsm_device_hart_stop(void)
 	return SBI_ENOTSUPP;
 }
 
-static int hsm_device_hart_suspend(u32 suspend_type)
+static int hsm_device_hart_suspend(u32 suspend_type, ulong mmode_resume_addr)
 {
 	if (hsm_dev && hsm_dev->hart_suspend)
-		return hsm_dev->hart_suspend(suspend_type);
+		return hsm_dev->hart_suspend(suspend_type, mmode_resume_addr);
 	return SBI_ENOTSUPP;
 }
 
@@ -536,7 +536,7 @@ int sbi_hsm_hart_suspend(struct sbi_scratch *scratch, u32 suspend_type,
 		__sbi_hsm_suspend_non_ret_save(scratch);
 
 	/* Try platform specific suspend */
-	ret = hsm_device_hart_suspend(suspend_type);
+	ret = hsm_device_hart_suspend(suspend_type, scratch->warmboot_addr);
 	if (ret == SBI_ENOTSUPP) {
 		/* Try generic implementation of default suspend types */
 		if (suspend_type == SBI_HSM_SUSPEND_RET_DEFAULT ||
platform/generic/allwinner/sun20i-d1.c +1 -1
index ab2eee55..33312e0b 100644
@@ -152,7 +152,7 @@ static void sun20i_d1_riscv_cfg_init(void)
 	writel_relaxed(entry >> 32, SUN20I_D1_RISCV_CFG_BASE + RESET_ENTRY_HI_REG);
 }
 
-static int sun20i_d1_hart_suspend(u32 suspend_type)
+static int sun20i_d1_hart_suspend(u32 suspend_type, ulong mmode_resume_addr)
 {
 	/* Use the generic code for retentive suspend. */
 	if (!(suspend_type & SBI_HSM_SUSP_NON_RET_BIT))
Signature · 1 line
2.34.1
author
#13 lore raw

PATCH 10/16 lib: utils/hsm: Add RPMI HSM driver

In reply to Anup Patel

From: Subrahmanya Lingappa <[email protected]>

The RPMI HSM service group provides set of routine to query and control power states of a Hart. Add RPMI based Hart State Management (HSM) driver.

---
 include/sbi_utils/mailbox/rpmi_msgprot.h |  91 ++++++
 lib/utils/hsm/Kconfig                    |   9 +
 lib/utils/hsm/fdt_hsm_rpmi.c             | 351 +++++++++++++++++++++++
 lib/utils/hsm/objects.mk                 |   3 +
 platform/generic/configs/defconfig       |   1 +
 5 files changed, 455 insertions(+)
 create mode 100644 lib/utils/hsm/fdt_hsm_rpmi.c
include/sbi_utils/mailbox/rpmi_msgprot.h +91 -0
index a2403c90..efe35aee 100644
@@ -151,6 +151,7 @@ enum rpmi_servicegroup_id {
 	RPMI_SRVGRP_BASE = 0x00001,
 	RPMI_SRVGRP_SYSTEM_RESET = 0x00002,
 	RPMI_SRVGRP_SYSTEM_SUSPEND = 0x00003,
+	RPMI_SRVGRP_HSM = 0x00004,
 	RPMI_SRVGRP_ID_MAX_COUNT,
 };
 
@@ -243,4 +244,94 @@ struct rpmi_syssusp_suspend_resp {
 	s32 status;
 };
 
+/** RPMI HSM State Management ServiceGroup Service IDs */
+enum rpmi_cpu_hsm_service_id {
+	RPMI_HSM_SRV_ENABLE_NOTIFICATION = 0x01,
+	RPMI_HSM_SRV_HART_START = 0x02,
+	RPMI_HSM_SRV_HART_STOP = 0x03,
+	RPMI_HSM_SRV_HART_SUSPEND = 0x04,
+	RPMI_HSM_SRV_GET_HART_STATUS = 0x05,
+	RPMI_HSM_SRV_GET_HART_LIST = 0x06,
+	RPMI_HSM_SRV_GET_SUSPEND_TYPES = 0x07,
+	RPMI_HSM_SRV_GET_SUSPEND_INFO = 0x08,
+	RPMI_HSM_SRV_ID_MAX_COUNT,
+};
+
+/* HSM service group request and response structs */
+struct rpmi_hsm_hart_start_req {
+	u32 hartid;
+	u32 start_addr_lo;
+	u32 start_addr_hi;
+};
+
+struct rpmi_hsm_hart_start_resp {
+	s32 status;
+};
+
+struct rpmi_hsm_hart_stop_req {
+	u32 hartid;
+};
+
+struct rpmi_hsm_hart_stop_resp {
+	s32 status;
+};
+
+struct rpmi_hsm_hart_susp_req {
+	u32 hartid;
+	u32 suspend_type;
+	u32 resume_addr_lo;
+	u32 resume_addr_hi;
+};
+
+struct rpmi_hsm_hart_susp_resp {
+	s32 status;
+};
+
+struct rpmi_hsm_get_hart_status_req {
+	u32 hartid;
+};
+
+struct rpmi_hsm_get_hart_status_resp {
+	s32 status;
+	u32 hart_status;
+};
+
+struct rpmi_hsm_get_hart_list_req {
+	u32 start_index;
+};
+
+struct rpmi_hsm_get_hart_list_resp {
+	s32 status;
+	u32 remaining;
+	u32 returned;
+	/* remaining space need to be adjusted for the above 3 u32's */
+	u32 hartid[(RPMI_MSG_DATA_SIZE(RPMI_SLOT_SIZE_MIN) - (sizeof(u32) * 3)) / sizeof(u32)];
+};
+
+struct rpmi_hsm_get_susp_types_req {
+	u32 start_index;
+};
+
+struct rpmi_hsm_get_susp_types_resp {
+	s32 status;
+	u32 remaining;
+	u32 returned;
+	/* remaining space need to be adjusted for the above 3 u32's */
+	u32 types[(RPMI_MSG_DATA_SIZE(RPMI_SLOT_SIZE_MIN) - (sizeof(u32) * 3)) / sizeof(u32)];
+};
+
+struct rpmi_hsm_get_susp_info_req {
+	u32 suspend_type;
+};
+
+struct rpmi_hsm_get_susp_info_resp {
+	s32 status;
+	u32 flags;
+#define RPMI_HSM_FLAGS_LOCAL_TIME_STOP     (1U << 31)
+	u32 entry_latency_us;
+	u32 exit_latency_us;
+	u32 wakeup_latency_us;
+	u32 min_residency_us;
+};
+
 #endif /* !__RPMI_MSGPROT_H__ */
lib/utils/hsm/Kconfig +9 -0
index 31506116..1ad7958f 100644
@@ -7,4 +7,13 @@ config FDT_HSM
 	depends on FDT
 	default n
 
+if FDT_HSM
+
+config FDT_HSM_RPMI
+	bool "FDT RPMI HSM driver"
+	depends on FDT_MAILBOX && RPMI_MAILBOX
+	default n
+
+endif
+
 endmenu
lib/utils/hsm/fdt_hsm_rpmi.c new file +351 -0

351 lines not on this page · raw message

lib/utils/hsm/objects.mk +3 -0
index 49337bf5..b54b6f6c 100644
@@ -9,3 +9,6 @@
 
 libsbiutils-objs-$(CONFIG_FDT_HSM) += hsm/fdt_hsm.o
 libsbiutils-objs-$(CONFIG_FDT_HSM) += hsm/fdt_hsm_drivers.carray.o
+
+carray-fdt_hsm_drivers-$(CONFIG_FDT_HSM_RPMI) += fdt_hsm_rpmi
+libsbiutils-objs-$(CONFIG_FDT_HSM_RPMI) += hsm/fdt_hsm_rpmi.o
platform/generic/configs/defconfig +1 -0
index 2efc7136..78fc96b6 100644
@@ -11,6 +11,7 @@ CONFIG_FDT_GPIO_DESIGNWARE=y
 CONFIG_FDT_GPIO_SIFIVE=y
 CONFIG_FDT_GPIO_STARFIVE=y
 CONFIG_FDT_HSM=y
+CONFIG_FDT_HSM_RPMI=y
 CONFIG_FDT_I2C=y
 CONFIG_FDT_I2C_SIFIVE=y
 CONFIG_FDT_I2C_DW=y
Signature · 1 line
2.34.1
author
#14 lore raw

PATCH 11/16 lib: utils: Add simple FDT based CPPC driver framework

In reply to Anup Patel

The generic platform can have multiple CPPC drivers so add a simple FDT based CPPC driver framework.

---
 include/sbi_utils/cppc/fdt_cppc.h      | 35 +++++++++++
 lib/utils/Kconfig                      |  2 +
 lib/utils/cppc/Kconfig                 | 10 ++++
 lib/utils/cppc/fdt_cppc.c              | 82 ++++++++++++++++++++++++++
 lib/utils/cppc/fdt_cppc_drivers.carray |  3 +
 lib/utils/cppc/objects.mk              | 11 ++++
 platform/generic/configs/defconfig     |  1 +
 platform/generic/platform.c            |  3 +
 8 files changed, 147 insertions(+)
 create mode 100644 include/sbi_utils/cppc/fdt_cppc.h
 create mode 100644 lib/utils/cppc/Kconfig
 create mode 100644 lib/utils/cppc/fdt_cppc.c
 create mode 100644 lib/utils/cppc/fdt_cppc_drivers.carray
 create mode 100644 lib/utils/cppc/objects.mk
include/sbi_utils/cppc/fdt_cppc.h new file +35 -0
new file mode 100644
index 00000000..af0cdc41
@@ -0,0 +1,35 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#ifndef __FDT_CPPC_H__
+#define __FDT_CPPC_H__
+
+#include <sbi/sbi_types.h>
+
+#ifdef CONFIG_FDT_CPPC
+
+struct fdt_cppc {
+	const struct fdt_match *match_table;
+	int (*cold_init)(void *fdt, int nodeoff, const struct fdt_match *match);
+	int (*warm_init)(void);
+	void (*exit)(void);
+};
+
+void fdt_cppc_exit(void);
+
+int fdt_cppc_init(bool cold_boot);
+
+#else
+
+static inline void fdt_cppc_exit(void) { }
+static inline int fdt_cppc_init(bool cold_boot) { return 0; }
+
+#endif
+
+#endif
lib/utils/Kconfig +2 -0
index c860a185..002d6f8f 100644
@@ -2,6 +2,8 @@
 
 menu "Utils and Drivers Support"
 
+source "$(OPENSBI_SRC_DIR)/lib/utils/cppc/Kconfig"
+
 source "$(OPENSBI_SRC_DIR)/lib/utils/fdt/Kconfig"
 
 source "$(OPENSBI_SRC_DIR)/lib/utils/gpio/Kconfig"
lib/utils/cppc/Kconfig new file +10 -0
new file mode 100644
index 00000000..08d1c97f
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+menu "CPPC Device Support"
+
+config FDT_CPPC
+	bool "FDT based CPPC drivers"
+	depends on FDT
+	default n
+
+endmenu
lib/utils/cppc/fdt_cppc.c new file +82 -0

82 lines not on this page · raw message

lib/utils/cppc/fdt_cppc_drivers.carray new file +3 -0
new file mode 100644
index 00000000..c2a9af2c
@@ -0,0 +1,3 @@
+HEADER: sbi_utils/cppc/fdt_cppc.h
+TYPE: struct fdt_cppc
+NAME: fdt_cppc_drivers
lib/utils/cppc/objects.mk new file +11 -0
new file mode 100644
index 00000000..fb37478a
@@ -0,0 +1,11 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2024 Ventana Micro Systems Inc.
+#
+# Authors:
+#   Anup Patel <[email protected]>
+#
+
+libsbiutils-objs-$(CONFIG_FDT_CPPC) += cppc/fdt_cppc.o
+libsbiutils-objs-$(CONFIG_FDT_CPPC) += cppc/fdt_cppc_drivers.carray.o
platform/generic/configs/defconfig +1 -0
index 78fc96b6..bd18c3c1 100644
@@ -6,6 +6,7 @@ CONFIG_PLATFORM_SIFIVE_FU740=y
 CONFIG_PLATFORM_SOPHGO_SG2042=y
 CONFIG_PLATFORM_STARFIVE_JH7110=y
 CONFIG_PLATFORM_THEAD=y
+CONFIG_FDT_CPPC=y
 CONFIG_FDT_GPIO=y
 CONFIG_FDT_GPIO_DESIGNWARE=y
 CONFIG_FDT_GPIO_SIFIVE=y
platform/generic/platform.c +3 -0
index bcd7133b..d085647d 100644
@@ -17,6 +17,7 @@
 #include <sbi/sbi_string.h>
 #include <sbi/sbi_system.h>
 #include <sbi/sbi_tlb.h>
+#include <sbi_utils/cppc/fdt_cppc.h>
 #include <sbi_utils/fdt/fdt_domain.h>
 #include <sbi_utils/fdt/fdt_fixup.h>
 #include <sbi_utils/fdt/fdt_helper.h>
@@ -238,6 +239,7 @@ static int generic_early_init(bool cold_boot)
 	}
 
 	fdt_hsm_init(cold_boot);
+	fdt_cppc_init(cold_boot);
 
 	if (!generic_plat || !generic_plat->early_init)
 		return 0;
@@ -291,6 +293,7 @@ static int generic_vendor_ext_provider(long funcid,
 
 static void generic_early_exit(void)
 {
+	fdt_cppc_exit();
 	fdt_hsm_exit();
 
 	if (generic_plat && generic_plat->early_exit)
Signature · 1 line
2.34.1
author
#15 lore raw

PATCH 12/16 lib: utils/cppc: Add RPMI CPPC driver

In reply to Anup Patel

From: Subrahmanya Lingappa <[email protected]>

Add RPMI based driver for CPPC register read, write and probe.

---
 include/sbi_utils/mailbox/rpmi_msgprot.h |  83 +++++++
 lib/utils/cppc/Kconfig                   |   9 +
 lib/utils/cppc/fdt_cppc_rpmi.c           | 287 +++++++++++++++++++++++
 lib/utils/cppc/objects.mk                |   3 +
 platform/generic/configs/defconfig       |   1 +
 5 files changed, 383 insertions(+)
 create mode 100644 lib/utils/cppc/fdt_cppc_rpmi.c
include/sbi_utils/mailbox/rpmi_msgprot.h +83 -0
index efe35aee..f7913ab1 100644
@@ -152,6 +152,7 @@ enum rpmi_servicegroup_id {
 	RPMI_SRVGRP_SYSTEM_RESET = 0x00002,
 	RPMI_SRVGRP_SYSTEM_SUSPEND = 0x00003,
 	RPMI_SRVGRP_HSM = 0x00004,
+	RPMI_SRVGRP_CPPC = 0x00005,
 	RPMI_SRVGRP_ID_MAX_COUNT,
 };
 
@@ -334,4 +335,86 @@ struct rpmi_hsm_get_susp_info_resp {
 	u32 min_residency_us;
 };
 
+/** RPMI CPPC ServiceGroup Service IDs */
+enum rpmi_cppc_service_id {
+	RPMI_CPPC_SRV_ENABLE_NOTIFICATION = 0x01,
+	RPMI_CPPC_SRV_PROBE_REG = 0x02,
+	RPMI_CPPC_SRV_READ_REG = 0x03,
+	RPMI_CPPC_SRV_WRITE_REG = 0x04,
+	RPMI_CPPC_SRV_GET_FAST_CHANNEL_ADDR = 0x05,
+	RPMI_CPPC_SRV_POKE_FAST_CHANNEL = 0x06,
+	RPMI_CPPC_SRV_GET_HART_LIST = 0x07,
+	RPMI_CPPC_SRV_MAX_COUNT,
+};
+
+struct rpmi_cppc_probe_req {
+	u32 hart_id;
+	u32 reg_id;
+};
+
+struct rpmi_cppc_probe_resp {
+	s32 status;
+	u32 reg_len;
+};
+
+struct rpmi_cppc_read_reg_req {
+	u32 hart_id;
+	u32 reg_id;
+};
+
+struct rpmi_cppc_read_reg_resp {
+	s32 status;
+	u32 data_lo;
+	u32 data_hi;
+};
+
+struct rpmi_cppc_write_reg_req {
+	u32 hart_id;
+	u32 reg_id;
+	u32 data_lo;
+	u32 data_hi;
+};
+
+struct rpmi_cppc_write_reg_resp {
+	s32 status;
+};
+
+struct rpmi_cppc_get_fast_channel_addr_req {
+	u32 hart_id;
+};
+
+struct rpmi_cppc_get_fast_channel_addr_resp {
+	s32 status;
+#define RPMI_CPPC_FAST_CHANNEL_FLAGS_DB_WIDTH_POS	1
+#define RPMI_CPPC_FAST_CHANNEL_FLAGS_DB_WIDTH_MASK	\
+			(3U << RPMI_CPPC_FAST_CHANNEL_FLAGS_DB_WIDTH_POS)
+#define RPMI_CPPC_FAST_CHANNEL_FLAGS_DB_SUPPORTED	(1U << 0)
+	u32 flags;
+	u32 addr_lo;
+	u32 addr_hi;
+	u32 db_addr_lo;
+	u32 db_addr_hi;
+	u32 db_id_lo;
+	u32 db_id_hi;
+};
+
+enum rpmi_cppc_fast_channel_db_width {
+	RPMI_CPPC_FAST_CHANNEL_DB_WIDTH_8 = 0x0,
+	RPMI_CPPC_FAST_CHANNEL_DB_WIDTH_16 = 0x1,
+	RPMI_CPPC_FAST_CHANNEL_DB_WIDTH_32 = 0x2,
+	RPMI_CPPC_FAST_CHANNEL_DB_WIDTH_64 = 0x3,
+};
+
+struct rpmi_cppc_hart_list_req {
+	u32 start_index;
+};
+
+struct rpmi_cppc_hart_list_resp {
+	s32 status;
+	u32 remaining;
+	u32 returned;
+	/* remaining space need to be adjusted for the above 3 u32's */
+	u32 hartid[(RPMI_MSG_DATA_SIZE(RPMI_SLOT_SIZE_MIN) - (sizeof(u32) * 3)) / sizeof(u32)];
+};
+
 #endif /* !__RPMI_MSGPROT_H__ */
lib/utils/cppc/Kconfig +9 -0
index 08d1c97f..494f6894 100644
@@ -7,4 +7,13 @@ config FDT_CPPC
 	depends on FDT
 	default n
 
+if FDT_CPPC
+
+config FDT_CPPC_RPMI
+	bool "FDT RPMI CPPC driver"
+	depends on FDT_MAILBOX && RPMI_MAILBOX
+	default n
+
+endif
+
 endmenu
lib/utils/cppc/fdt_cppc_rpmi.c new file +287 -0

287 lines not on this page · raw message

lib/utils/cppc/objects.mk +3 -0
index fb37478a..07dc7d89 100644
@@ -9,3 +9,6 @@
 
 libsbiutils-objs-$(CONFIG_FDT_CPPC) += cppc/fdt_cppc.o
 libsbiutils-objs-$(CONFIG_FDT_CPPC) += cppc/fdt_cppc_drivers.carray.o
+
+carray-fdt_cppc_drivers-$(CONFIG_FDT_CPPC_RPMI) += fdt_cppc_rpmi
+libsbiutils-objs-$(CONFIG_FDT_CPPC_RPMI) += cppc/fdt_cppc_rpmi.o
platform/generic/configs/defconfig +1 -0
index bd18c3c1..384918f9 100644
@@ -7,6 +7,7 @@ CONFIG_PLATFORM_SOPHGO_SG2042=y
 CONFIG_PLATFORM_STARFIVE_JH7110=y
 CONFIG_PLATFORM_THEAD=y
 CONFIG_FDT_CPPC=y
+CONFIG_FDT_CPPC_RPMI=y
 CONFIG_FDT_GPIO=y
 CONFIG_FDT_GPIO_DESIGNWARE=y
 CONFIG_FDT_GPIO_SIFIVE=y
Signature · 1 line
2.34.1
author
#16 lore raw

PATCH 13/16 lib: sbi: Add SBI Message Proxy (MPXY) framework

In reply to Anup Patel

From: Rahul Pathak <[email protected]>

Introduce SBI Message Proxy (MPXY) framework which allows platform specific code or drivers to register message protocol specific channels.

This framework enables the supervisor software to send messages belonging to different message protocols via OpenSBI firmware.

---
 include/sbi/sbi_ecall_interface.h |   3 +
 include/sbi/sbi_error.h           |  14 +-
 include/sbi/sbi_mpxy.h            | 181 +++++++++
 include/sbi/sbi_platform.h        |  17 +
 lib/sbi/objects.mk                |   1 +
 lib/sbi/sbi_init.c                |   6 +
 lib/sbi/sbi_mpxy.c                | 644 ++++++++++++++++++++++++++++++
 7 files changed, 860 insertions(+), 6 deletions(-)
 create mode 100644 include/sbi/sbi_mpxy.h
 create mode 100644 lib/sbi/sbi_mpxy.c
include/sbi/sbi_ecall_interface.h +3 -0
index e9a81677..6b993b18 100644
@@ -428,6 +428,9 @@ enum sbi_sse_state {
 #define SBI_ERR_NO_SHMEM			-9
 #define SBI_ERR_INVALID_STATE			-10
 #define SBI_ERR_BAD_RANGE			-11
+#define SBI_ERR_NOT_IMPLEMENTED			-12
+#define SBI_ERR_TIMEOUT				-13
+#define SBI_ERR_IO				-14
 
 #define SBI_LAST_ERR				SBI_ERR_BAD_RANGE
 
include/sbi/sbi_error.h +8 -6
index fb78bf62..173923fb 100644
@@ -26,16 +26,18 @@
 #define SBI_ENO_SHMEM		SBI_ERR_NO_SHMEM
 #define SBI_EINVALID_STATE	SBI_ERR_INVALID_STATE
 #define SBI_EBAD_RANGE		SBI_ERR_BAD_RANGE
+#define SBI_ENOTIMPL		SBI_ERR_NOT_IMPLEMENTED
+#define SBI_ETIMEOUT		SBI_ERR_TIMEOUT
+#define SBI_EIO			SBI_ERR_IO
 
 #define SBI_ENODEV		-1000
 #define SBI_ENOSYS		-1001
 #define SBI_ETIMEDOUT		-1002
-#define SBI_EIO			-1003
-#define SBI_EILL		-1004
-#define SBI_ENOSPC		-1005
-#define SBI_ENOMEM		-1006
-#define SBI_EUNKNOWN		-1007
-#define SBI_ENOENT		-1008
+#define SBI_EILL		-1003
+#define SBI_ENOSPC		-1004
+#define SBI_ENOMEM		-1005
+#define SBI_EUNKNOWN		-1006
+#define SBI_ENOENT		-1007
 
 /* clang-format on */
 
include/sbi/sbi_mpxy.h new file +181 -0

181 lines not on this page · raw message

include/sbi/sbi_platform.h +17 -0
index 7b3ac4bf..91996888 100644
@@ -132,6 +132,9 @@ struct sbi_platform_operations {
 	/** Exit platform timer for current HART */
 	void (*timer_exit)(void);
 
+	/** Initialize the platform Message Proxy(MPXY) driver */
+	int (*mpxy_init)(void);
+
 	/** Check if SBI vendor extension is implemented or not */
 	bool (*vendor_ext_check)(void);
 	/** platform specific SBI extension implementation provider */
@@ -627,6 +630,20 @@ static inline void sbi_platform_timer_exit(const struct sbi_platform *plat)
 		sbi_platform_ops(plat)->timer_exit();
 }
 
+/**
+ * Initialize the platform Message Proxy drivers
+ *
+ * @param plat pointer to struct sbi_platform
+ *
+ * @return 0 on success and negative error code on failure
+ */
+static inline int sbi_platform_mpxy_init(const struct sbi_platform *plat)
+{
+	if (plat && sbi_platform_ops(plat)->mpxy_init)
+		return sbi_platform_ops(plat)->mpxy_init();
+	return 0;
+}
+
 /**
  * Check if SBI vendor extension is implemented or not.
  *
lib/sbi/objects.mk +1 -0
index 535aa709..2cea93b9 100644
@@ -81,6 +81,7 @@ libsbi-objs-y += sbi_irqchip.o
 libsbi-objs-y += sbi_platform.o
 libsbi-objs-y += sbi_pmu.o
 libsbi-objs-y += sbi_dbtr.o
+libsbi-objs-y += sbi_mpxy.o
 libsbi-objs-y += sbi_scratch.o
 libsbi-objs-y += sbi_sse.o
 libsbi-objs-y += sbi_string.o
lib/sbi/sbi_init.c +6 -0
index d80efe97..bac55e79 100644
@@ -24,6 +24,7 @@
 #include <sbi/sbi_platform.h>
 #include <sbi/sbi_pmu.h>
 #include <sbi/sbi_dbtr.h>
+#include <sbi/sbi_mpxy.h>
 #include <sbi/sbi_sse.h>
 #include <sbi/sbi_system.h>
 #include <sbi/sbi_string.h>
@@ -311,6 +312,11 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 		sbi_hart_hang();
 	}
 
+	rc = sbi_mpxy_init(scratch);
+	if (rc) {
+		sbi_printf("%s: mpxy init failed (error %d)\n", __func__, rc);
+		sbi_hart_hang();
+	}
 	/*
 	 * Note: Finalize domains after HSM initialization so that we
 	 * can startup non-root domains.
lib/sbi/sbi_mpxy.c new file +644 -0

644 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#19 lore raw

PATCH 14/16 lib: sbi: Implement SBI MPXY extension

In reply to Anup Patel

Implement the SBI MPXY extension which provides an SBI interface to the supervisor software for send messages via MPXY framework.

---
 include/sbi/sbi_ecall_interface.h | 10 +++++
 lib/sbi/Kconfig                   |  3 ++
 lib/sbi/objects.mk                |  3 ++
 lib/sbi/sbi_ecall_mpxy.c          | 68 +++++++++++++++++++++++++++++++
 4 files changed, 84 insertions(+)
 create mode 100644 lib/sbi/sbi_ecall_mpxy.c
include/sbi/sbi_ecall_interface.h +10 -0
index 6b993b18..085b33e7 100644
@@ -35,6 +35,7 @@
 #define SBI_EXT_DBTR				0x44425452
 #define SBI_EXT_SSE				0x535345
 #define SBI_EXT_FWFT				0x46574654
+#define SBI_EXT_MPXY				0x4D505859
 
 /* SBI function IDs for BASE extension*/
 #define SBI_EXT_BASE_GET_SPEC_VERSION		0x0
@@ -406,6 +407,15 @@ enum sbi_sse_state {
 #define SBI_SSE_EVENT_GLOBAL_BIT		(1 << 15)
 #define SBI_SSE_EVENT_PLATFORM_BIT		(1 << 14)
 
+/* SBI function IDs for MPXY extension */
+#define SBI_EXT_MPXY_SET_SHMEM			0x0
+#define SBI_EXT_MPXY_GET_CHANNEL_IDS		0x1
+#define SBI_EXT_MPXY_READ_ATTRS			0x2
+#define SBI_EXT_MPXY_WRITE_ATTRS		0x3
+#define SBI_EXT_MPXY_SEND_MSG_WITH_RESP		0x4
+#define SBI_EXT_MPXY_SEND_MSG_NO_RESP		0x5
+#define SBI_EXT_MPXY_GET_NOTIFICATION_EVENTS	0x6
+
 /* SBI base specification related macros */
 #define SBI_SPEC_VERSION_MAJOR_OFFSET		24
 #define SBI_SPEC_VERSION_MAJOR_MASK		0x7f
lib/sbi/Kconfig +3 -0
index bd8ba2b6..c6cc04bc 100644
@@ -66,4 +66,7 @@ config SBI_ECALL_SSE
 	bool "SSE extension"
 	default y
 
+config SBI_ECALL_MPXY
+	bool "MPXY extension"
+	default y
 endmenu
lib/sbi/objects.mk +3 -0
index 2cea93b9..c199b834 100644
@@ -61,6 +61,9 @@ libsbi-objs-$(CONFIG_SBI_ECALL_DBTR) += sbi_ecall_dbtr.o
 carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_SSE) += ecall_sse
 libsbi-objs-$(CONFIG_SBI_ECALL_SSE) += sbi_ecall_sse.o
 
+carray-sbi_ecall_exts-$(CONFIG_SBI_ECALL_MPXY) += ecall_mpxy
+libsbi-objs-$(CONFIG_SBI_ECALL_MPXY) += sbi_ecall_mpxy.o
+
 libsbi-objs-y += sbi_bitmap.o
 libsbi-objs-y += sbi_bitops.o
 libsbi-objs-y += sbi_console.o
lib/sbi/sbi_ecall_mpxy.c new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#23 lore raw

PATCH 15/16 lib: utils: Add simple FDT based MPXY driver framework

In reply to Anup Patel

The generic platform can have multiple MPXY drivers so add a simple FDT based MPXY driver framework.

---
 include/sbi_utils/mpxy/fdt_mpxy.h      | 31 ++++++++++++++++
 lib/utils/Kconfig                      |  2 ++
 lib/utils/mpxy/Kconfig                 | 10 ++++++
 lib/utils/mpxy/fdt_mpxy.c              | 50 ++++++++++++++++++++++++++
 lib/utils/mpxy/fdt_mpxy_drivers.carray |  3 ++
 lib/utils/mpxy/objects.mk              | 11 ++++++
 platform/generic/configs/defconfig     |  1 +
 platform/generic/platform.c            |  2 ++
 8 files changed, 110 insertions(+)
 create mode 100644 include/sbi_utils/mpxy/fdt_mpxy.h
 create mode 100644 lib/utils/mpxy/Kconfig
 create mode 100644 lib/utils/mpxy/fdt_mpxy.c
 create mode 100644 lib/utils/mpxy/fdt_mpxy_drivers.carray
 create mode 100644 lib/utils/mpxy/objects.mk
include/sbi_utils/mpxy/fdt_mpxy.h new file +31 -0
new file mode 100644
index 00000000..7ea26055
@@ -0,0 +1,31 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#ifndef __FDT_MPXY_H__
+#define __FDT_MPXY_H__
+
+#include <sbi/sbi_types.h>
+
+#ifdef CONFIG_FDT_MPXY
+
+struct fdt_mpxy {
+	const struct fdt_match *match_table;
+	int (*init)(void *fdt, int nodeoff, const struct fdt_match *match);
+	void (*exit)(void);
+};
+
+int fdt_mpxy_init(void);
+
+#else
+
+static inline int fdt_mpxy_init(void) { return 0; }
+
+#endif
+
+#endif
lib/utils/Kconfig +2 -0
index 002d6f8f..901ba564 100644
@@ -32,4 +32,6 @@ source "$(OPENSBI_SRC_DIR)/lib/utils/sys/Kconfig"
 
 source "$(OPENSBI_SRC_DIR)/lib/utils/timer/Kconfig"
 
+source "$(OPENSBI_SRC_DIR)/lib/utils/mpxy/Kconfig"
+
 endmenu
lib/utils/mpxy/Kconfig new file +10 -0
new file mode 100644
index 00000000..d084b09a
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+menu "MPXY Device Support"
+
+config FDT_MPXY
+	bool "FDT based MPXY drivers"
+	depends on FDT
+	default n
+
+endmenu
lib/utils/mpxy/fdt_mpxy.c new file +50 -0

50 lines not on this page · raw message

lib/utils/mpxy/fdt_mpxy_drivers.carray new file +3 -0
new file mode 100644
index 00000000..d930753a
@@ -0,0 +1,3 @@
+HEADER: sbi_utils/mpxy/fdt_mpxy.h
+TYPE: struct fdt_mpxy
+NAME: fdt_mpxy_drivers
lib/utils/mpxy/objects.mk new file +11 -0
new file mode 100644
index 00000000..43e73c94
@@ -0,0 +1,11 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2024 Ventana Micro Systems Inc.
+#
+# Authors:
+#   Anup Patel <[email protected]>
+#
+
+libsbiutils-objs-$(CONFIG_FDT_MPXY) += mpxy/fdt_mpxy.o
+libsbiutils-objs-$(CONFIG_FDT_MPXY) += mpxy/fdt_mpxy_drivers.carray.o
platform/generic/configs/defconfig +1 -0
index 384918f9..1f0880ee 100644
@@ -53,3 +53,4 @@ CONFIG_FDT_SUSPEND_RPMI=y
 CONFIG_FDT_TIMER=y
 CONFIG_FDT_TIMER_MTIMER=y
 CONFIG_FDT_TIMER_PLMT=y
+CONFIG_FDT_MPXY=y
platform/generic/platform.c +2 -0
index d085647d..89bd3af9 100644
@@ -17,6 +17,7 @@
 #include <sbi/sbi_string.h>
 #include <sbi/sbi_system.h>
 #include <sbi/sbi_tlb.h>
+#include <sbi_utils/mpxy/fdt_mpxy.h>
 #include <sbi_utils/cppc/fdt_cppc.h>
 #include <sbi_utils/fdt/fdt_domain.h>
 #include <sbi_utils/fdt/fdt_fixup.h>
@@ -418,6 +419,7 @@ const struct sbi_platform_operations platform_ops = {
 	.get_tlb_num_entries	= generic_tlb_num_entries,
 	.timer_init		= fdt_timer_init,
 	.timer_exit		= fdt_timer_exit,
+	.mpxy_init		= fdt_mpxy_init,
 	.vendor_ext_check	= generic_vendor_ext_check,
 	.vendor_ext_provider	= generic_vendor_ext_provider,
 };
Signature · 1 line
2.34.1
author
#24 lore raw

PATCH 16/16 lib: utils/mpxy: Add RPMI client driver for MPXY

In reply to Anup Patel

From: Rahul Pathak <[email protected]>

Add a generic RPMI mailbox client driver which provides a MPXY channel. Initially, this driver only supports RPMI clock service group but can be extended to support multiple RPMI service groups.

---
 include/sbi_utils/mailbox/rpmi_msgprot.h |  93 ++++++
 lib/utils/mpxy/Kconfig                   |   9 +
 lib/utils/mpxy/fdt_mpxy_rpmi_mbox.c      | 408 +++++++++++++++++++++++
 lib/utils/mpxy/objects.mk                |   3 +
 platform/generic/configs/defconfig       |   1 +
 5 files changed, 514 insertions(+)
 create mode 100644 lib/utils/mpxy/fdt_mpxy_rpmi_mbox.c
include/sbi_utils/mailbox/rpmi_msgprot.h +93 -0
index f7913ab1..50d34017 100644
@@ -153,6 +153,7 @@ enum rpmi_servicegroup_id {
 	RPMI_SRVGRP_SYSTEM_SUSPEND = 0x00003,
 	RPMI_SRVGRP_HSM = 0x00004,
 	RPMI_SRVGRP_CPPC = 0x00005,
+	RPMI_SRVGRP_CLOCK = 0x00007,
 	RPMI_SRVGRP_ID_MAX_COUNT,
 };
 
@@ -417,4 +418,96 @@ struct rpmi_cppc_hart_list_resp {
 	u32 hartid[(RPMI_MSG_DATA_SIZE(RPMI_SLOT_SIZE_MIN) - (sizeof(u32) * 3)) / sizeof(u32)];
 };
 
+/** RPMI Clock ServiceGroup Service IDs */
+enum rpmi_clock_service_id {
+	RPMI_CLOCK_SRV_ENABLE_NOTIFICATION = 0x01,
+	RPMI_CLOCK_SRV_GET_NUM_CLOCKS = 0x02,
+	RPMI_CLOCK_SRV_GET_ATTRIBUTES = 0x03,
+	RPMI_CLOCK_SRV_GET_SUPPORTED_RATES = 0x04,
+	RPMI_CLOCK_SRV_SET_CONFIG = 0x05,
+	RPMI_CLOCK_SRV_GET_CONFIG = 0x06,
+	RPMI_CLOCK_SRV_SET_RATE = 0x07,
+	RPMI_CLOCK_SRV_GET_RATE = 0x08,
+	RPMI_CLOCK_SRV_MAX_COUNT,
+};
+
+struct rpmi_clock_get_num_clocks_resp {
+	s32 status;
+	u32 num_clocks;
+};
+
+struct rpmi_clock_get_attributes_req {
+	u32 clock_id;
+};
+
+struct rpmi_clock_get_attributes_resp {
+	s32 status;
+#define RPMI_CLOCK_FLAGS_FORMAT_POS		30
+#define RPMI_CLOCK_FLAGS_FORMAT_MASK		\
+			(3U << RPMI_CLOCK_FLAGS_CLOCK_FORMAT_POS)
+#define RPMI_CLOCK_FLAGS_FORMAT_DISCRETE	0
+#define RPMI_CLOCK_FLAGS_FORMAT_LINEAR		1
+	u32 flags;
+	u32 num_rates;
+	u32 transition_latency;
+	u8 name[16];
+};
+
+struct rpmi_clock_get_supported_rates_req {
+	u32 clock_id;
+	u32 clock_rate_index;
+};
+
+struct rpmi_clock_get_supported_rates_resp {
+	s32 status;
+	u32 flags;
+	u32 remaining;
+	u32 returned;
+	u32 clock_rate[0];
+};
+
+struct rpmi_clock_set_config_req {
+	u32 clock_id;
+#define RPMI_CLOCK_CONFIG_ENABLE		(1U << 0)
+	u32 config;
+};
+
+struct rpmi_clock_set_config_resp {
+	s32 status;
+};
+
+struct rpmi_clock_get_config_req {
+	u32 clock_id;
+};
+
+struct rpmi_clock_get_config_resp {
+	s32 status;
+	u32 config;
+};
+
+struct rpmi_clock_set_rate_req {
+	u32 clock_id;
+#define RPMI_CLOCK_SET_RATE_FLAGS_MASK		(3U << 0)
+#define RPMI_CLOCK_SET_RATE_FLAGS_ROUND_DOWN	0
+#define RPMI_CLOCK_SET_RATE_FLAGS_ROUND_UP	1
+#define RPMI_CLOCK_SET_RATE_FLAGS_ROUND_PLAT	2
+	u32 flags;
+	u32 clock_rate_low;
+	u32 clock_rate_high;
+};
+
+struct rpmi_clock_set_rate_resp {
+	s32 status;
+};
+
+struct rpmi_clock_get_rate_req {
+	u32 clock_id;
+};
+
+struct rpmi_clock_get_rate_resp {
+	s32 status;
+	u32 clock_rate_low;
+	u32 clock_rate_high;
+};
+
 #endif /* !__RPMI_MSGPROT_H__ */
lib/utils/mpxy/Kconfig +9 -0
index d084b09a..131fb91a 100644
@@ -7,4 +7,13 @@ config FDT_MPXY
 	depends on FDT
 	default n
 
+if FDT_MPXY
+
+config FDT_MPXY_RPMI_MBOX
+	bool "FDT MPXY mailbox client driver"
+	depends on FDT_MAILBOX
+	default n
+
+endif
+
 endmenu
lib/utils/mpxy/fdt_mpxy_rpmi_mbox.c new file +408 -0

408 lines not on this page · raw message

lib/utils/mpxy/objects.mk +3 -0
index 43e73c94..ccb28b55 100644
@@ -9,3 +9,6 @@
 
 libsbiutils-objs-$(CONFIG_FDT_MPXY) += mpxy/fdt_mpxy.o
 libsbiutils-objs-$(CONFIG_FDT_MPXY) += mpxy/fdt_mpxy_drivers.carray.o
+
+carray-fdt_mpxy_drivers-$(CONFIG_FDT_MPXY_RPMI_MBOX) += fdt_mpxy_rpmi_mbox
+libsbiutils-objs-$(CONFIG_FDT_MPXY_RPMI_MBOX) += mpxy/fdt_mpxy_rpmi_mbox.o
platform/generic/configs/defconfig +1 -0
index 1f0880ee..e23b38b2 100644
@@ -54,3 +54,4 @@ CONFIG_FDT_TIMER=y
 CONFIG_FDT_TIMER_MTIMER=y
 CONFIG_FDT_TIMER_PLMT=y
 CONFIG_FDT_MPXY=y
+CONFIG_FDT_MPXY_RPMI_MBOX=y
Signature · 1 line
2.34.1
#20 lore raw

PATCH 14/16 lib: sbi: Implement SBI MPXY extension

In reply to Anup Patel

Hi Anup,

Quoted 29 lines · Anup Patel

[EXTERNAL MAIL]

Implement the SBI MPXY extension which provides an SBI interface to the supervisor software for send messages via MPXY framework.

---
 include/sbi/sbi_ecall_interface.h | 10 +++++
 lib/sbi/Kconfig                   |  3 ++
 lib/sbi/objects.mk                |  3 ++
 lib/sbi/sbi_ecall_mpxy.c          | 68 +++++++++++++++++++++++++++++++
 4 files changed, 84 insertions(+)
 create mode 100644 lib/sbi/sbi_ecall_mpxy.c
include/sbi/sbi_ecall_interface.h +1 -0
index 6b993b18..085b33e7 100644
@@ -35,6 +35,7 @@
 #define SBI_EXT_DBTR                           0x44425452
 #define SBI_EXT_SSE                            0x535345
 #define SBI_EXT_FWFT                           0x46574654
+#define SBI_EXT_MPXY                           0x4D505859
 
 /* SBI function IDs for BASE extension*/
 #define SBI_EXT_BASE_GET_SPEC_VERSION          0x0

include/sbi/sbi_ecall_interface.h enum sbi_sse_state { · line 412

Quoted 6 lines
include/sbi/sbi_ecall_interface.h +3 -0
@@ -406,6 +407,15 @@ enum sbi_sse_state {
 #define SBI_SSE_EVENT_GLOBAL_BIT               (1 << 15)
 #define SBI_SSE_EVENT_PLATFORM_BIT             (1 << 14)
 
+/* SBI function IDs for MPXY extension */
+#define SBI_EXT_MPXY_SET_SHMEM                 0x0
+#define SBI_EXT_MPXY_GET_CHANNEL_IDS           0x1

I am unable to find this FID from the thread:
https://lists.riscv.org/g/tech-rpmi/message/89

Could you please let me know if there's an updated version of the MPXY Chapter?

Thanks,
Peter Lin

Quoted 5 lines
include/sbi/sbi_ecall_interface.h +5 -0
+#define SBI_EXT_MPXY_READ_ATTRS                        0x2
+#define SBI_EXT_MPXY_WRITE_ATTRS               0x3
+#define SBI_EXT_MPXY_SEND_MSG_WITH_RESP                0x4
+#define SBI_EXT_MPXY_SEND_MSG_NO_RESP          0x5
+#define SBI_EXT_MPXY_GET_NOTIFICATION_EVENTS   0x6
#21 lore raw

PATCH 14/16 lib: sbi: Implement SBI MPXY extension

Quoted 43 lines · On Wed, Aug 7, 2024 at 2:55?PM Yu-Chien Peter Lin <[email protected]>…

Hi Anup,

Quoted 29 lines · Anup Patel

[EXTERNAL MAIL]

Implement the SBI MPXY extension which provides an SBI interface to the supervisor software for send messages via MPXY framework.

---
 include/sbi/sbi_ecall_interface.h | 10 +++++
 lib/sbi/Kconfig                   |  3 ++
 lib/sbi/objects.mk                |  3 ++
 lib/sbi/sbi_ecall_mpxy.c          | 68 +++++++++++++++++++++++++++++++
 4 files changed, 84 insertions(+)
 create mode 100644 lib/sbi/sbi_ecall_mpxy.c
include/sbi/sbi_ecall_interface.h +1 -0
index 6b993b18..085b33e7 100644
@@ -35,6 +35,7 @@
 #define SBI_EXT_DBTR                           0x44425452
 #define SBI_EXT_SSE                            0x535345
 #define SBI_EXT_FWFT                           0x46574654
+#define SBI_EXT_MPXY                           0x4D505859
 
 /* SBI function IDs for BASE extension*/
 #define SBI_EXT_BASE_GET_SPEC_VERSION          0x0

include/sbi/sbi_ecall_interface.h enum sbi_sse_state { · line 412

Quoted 6 lines
include/sbi/sbi_ecall_interface.h +3 -0
@@ -406,6 +407,15 @@ enum sbi_sse_state {
 #define SBI_SSE_EVENT_GLOBAL_BIT               (1 << 15)
 #define SBI_SSE_EVENT_PLATFORM_BIT             (1 << 14)
 
+/* SBI function IDs for MPXY extension */
+#define SBI_EXT_MPXY_SET_SHMEM                 0x0
+#define SBI_EXT_MPXY_GET_CHANNEL_IDS           0x1

I am unable to find this FID from the thread:
https://lists.riscv.org/g/tech-rpmi/message/89

Quoted 2 lines

Could you please let me know if there's an updated version of the MPXY Chapter?

This function was added in the V3 version of MPXY spec. Here is the link https://lists.riscv.org/g/tech-prs/message/974

Quoted 8 lines

Thanks,
Peter Lin

Quoted 5 lines
include/sbi/sbi_ecall_interface.h +5 -0
+#define SBI_EXT_MPXY_READ_ATTRS                        0x2
+#define SBI_EXT_MPXY_WRITE_ATTRS               0x3
+#define SBI_EXT_MPXY_SEND_MSG_WITH_RESP                0x4
+#define SBI_EXT_MPXY_SEND_MSG_NO_RESP          0x5
+#define SBI_EXT_MPXY_GET_NOTIFICATION_EVENTS   0x6
Signature · 2 lines
Thanks
Rahul Pathak
#22 lore raw

PATCH 14/16 lib: sbi: Implement SBI MPXY extension

Hi Rahul,

Quoted 47 lines · Rahul Pathak

[EXTERNAL MAIL]

Quoted 43 lines · On Wed, Aug 7, 2024 at 2:55?PM Yu-Chien Peter Lin <[email protected]>…

Hi Anup,

Quoted 29 lines · Anup Patel

[EXTERNAL MAIL]

Implement the SBI MPXY extension which provides an SBI interface to the supervisor software for send messages via MPXY framework.

---
 include/sbi/sbi_ecall_interface.h | 10 +++++
 lib/sbi/Kconfig                   |  3 ++
 lib/sbi/objects.mk                |  3 ++
 lib/sbi/sbi_ecall_mpxy.c          | 68 +++++++++++++++++++++++++++++++
 4 files changed, 84 insertions(+)
 create mode 100644 lib/sbi/sbi_ecall_mpxy.c
include/sbi/sbi_ecall_interface.h +1 -0
index 6b993b18..085b33e7 100644
@@ -35,6 +35,7 @@
 #define SBI_EXT_DBTR                           0x44425452
 #define SBI_EXT_SSE                            0x535345
 #define SBI_EXT_FWFT                           0x46574654
+#define SBI_EXT_MPXY                           0x4D505859
 
 /* SBI function IDs for BASE extension*/
 #define SBI_EXT_BASE_GET_SPEC_VERSION          0x0

include/sbi/sbi_ecall_interface.h enum sbi_sse_state { · line 412

Quoted 6 lines
include/sbi/sbi_ecall_interface.h +3 -0
@@ -406,6 +407,15 @@ enum sbi_sse_state {
 #define SBI_SSE_EVENT_GLOBAL_BIT               (1 << 15)
 #define SBI_SSE_EVENT_PLATFORM_BIT             (1 << 14)
 
+/* SBI function IDs for MPXY extension */
+#define SBI_EXT_MPXY_SET_SHMEM                 0x0
+#define SBI_EXT_MPXY_GET_CHANNEL_IDS           0x1

I am unable to find this FID from the thread:
https://lists.riscv.org/g/tech-rpmi/message/89

Quoted 5 lines
Quoted 2 lines

Could you please let me know if there's an updated version of the MPXY Chapter?

This function was added in the V3 version of MPXY spec. Here is the link https://lists.riscv.org/g/tech-prs/message/974

Thanks for the information.

Best regards,
Peter Lin

Quoted 15 lines
Quoted 8 lines

Thanks,
Peter Lin

include/sbi/sbi_ecall_interface.h enum sbi_sse_state { · line 417

Quoted 5 lines
include/sbi/sbi_ecall_interface.h +5 -0
+#define SBI_EXT_MPXY_READ_ATTRS                        0x2
+#define SBI_EXT_MPXY_WRITE_ATTRS               0x3
+#define SBI_EXT_MPXY_SEND_MSG_WITH_RESP                0x4
+#define SBI_EXT_MPXY_SEND_MSG_NO_RESP          0x5
+#define SBI_EXT_MPXY_GET_NOTIFICATION_EVENTS   0x6

--

Thanks
Rahul Pathak

#5 lore raw

PATCH 4/16 lib/utils: Add RPMI messaging protocol and shared memory transport support

In reply to Anup Patel

Hi All,

I have some high level questions.

Given that the RPMI shared memory transport works on HWs described as:
https://github.com/riscv-software-src/librpmi/blob/main/docs/librpmi-arch.png

I'm wondering maybe we should also define the memory/cache architecture we are targeting in this AP-PuC design. E.g., one problem I can think of is that AP and PuC are often not cache-coherent. Thus, when doing Tx/Rx, we might need cache flush/invalidation. The memory order involving head/ tailptr, and read/write queue elements is also important. Even if we are dealing with a cache-coherent design, the usage of smp_wmb() after updating both the queue element and head/tailptr doesn't seems to be right. I think it should reflect a load-acquire semantic on headptr and a store-release semantic on tailptr. I'm sure there're very good references on how to handle memory order on this classic headptr/tailptr pattern, and we probably need to rework this part a little bit. For non-cache-coherent HW, I'm really not sure how to code this correctly, but my instinct tells me we need to be even more careful, make use of cache flushes, and ensure PuC observes the memory in the correct order.

The librpmi project might be the place where we can document AP-PuC memory architectures for both cache-coherent/non-coherent cases, and provide some guidance to PuC firmware writers. Excuse me if I'm raising an already solved issue, as I'm not following RPMI discussion closely. Thanks!

Bo

Quoted 1,075 lines · Anup Patel

From: Rahul Pathak <[email protected]>

The RISC-V Platform Management Interface (RPMI) defines a messaging protocol and shared memory based transport for bi-directional communication with an on-chip or external microcontroller.

To support RPMI in OpenSBI, add:
1) The RPMI messaging protocol defines and helper macros
2) A FDT mailbox driver for the RPMI shared memory transport

---
  include/sbi_utils/mailbox/rpmi_mailbox.h   |  32 +
  include/sbi_utils/mailbox/rpmi_msgprot.h   | 186 ++++++
  lib/utils/mailbox/Kconfig                  |  14 +
  lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c | 671 +++++++++++++++++++++
  lib/utils/mailbox/objects.mk               |   5 +
  lib/utils/mailbox/rpmi_mailbox.c           |  79 +++
  platform/generic/configs/defconfig         |   3 +
  7 files changed, 990 insertions(+)

create mode 100644 include/sbi_utils/mailbox/rpmi_mailbox.h create mode 100644 include/sbi_utils/mailbox/rpmi_msgprot.h create mode 100644 lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c create mode 100644 lib/utils/mailbox/rpmi_mailbox.c

include/sbi_utils/mailbox/rpmi_mailbox.h new file +32 -0
new file mode 100644
index 00000000..61af51a8
@@ -0,0 +1,32 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#ifndef __RPMI_MAILBOX_H__
+#define __RPMI_MAILBOX_H__
+
+#include <sbi/sbi_error.h>
+#include <sbi_utils/mailbox/rpmi_msgprot.h>
+
+#define rpmi_u32_count(__var)	(sizeof(__var) / sizeof(u32))
+
+/** Convert RPMI error to SBI error */
+int rpmi_xlate_error(enum rpmi_error error);
+
+/** Typical RPMI normal request with at least status code in response */
+int rpmi_normal_request_with_status(
+			struct mbox_chan *chan, u32 service_id,
+			void *req, u32 req_words, u32 req_endian_words,
+			void *resp, u32 resp_words, u32 resp_endian_words);
+
+/* RPMI posted request which is without any response*/
+int rpmi_posted_request(
+		struct mbox_chan *chan, u32 service_id,
+		void *req, u32 req_words, u32 req_endian_words);
+
+#endif /* !__RPMI_MAILBOX_H__ */
include/sbi_utils/mailbox/rpmi_msgprot.h new file +186 -0
new file mode 100644
index 00000000..e0c7cba0
@@ -0,0 +1,186 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Rahul Pathak <[email protected]>
+ */
+
+#ifndef __RPMI_MSGPROT_H__
+#define __RPMI_MSGPROT_H__
+
+#include <sbi/sbi_byteorder.h>
+#include <sbi/sbi_error.h>
+
+/*
+ * 31                                            0
+ * +---------------------+-----------------------+
+ * | FLAGS | SERVICE_ID  |   SERVICEGROUP_ID     |
+ * +---------------------+-----------------------+
+ * |        TOKEN        |     DATA LENGTH       |
+ * +---------------------+-----------------------+
+ * |                 DATA/PAYLOAD                |
+ * +---------------------------------------------+
+ */
+
+/** Message Header byte offset */
+#define RPMI_MSG_HDR_OFFSET			(0x0)
+/** Message Header Size in bytes */
+#define RPMI_MSG_HDR_SIZE			(8)
+
+/** ServiceGroup ID field byte offset */
+#define RPMI_MSG_SERVICEGROUP_ID_OFFSET		(0x0)
+/** ServiceGroup ID field size in bytes */
+#define RPMI_MSG_SERVICEGROUP_ID_SIZE		(2)
+
+/** Service ID field byte offset */
+#define RPMI_MSG_SERVICE_ID_OFFSET		(0x2)
+/** Service ID field size in bytes */
+#define RPMI_MSG_SERVICE_ID_SIZE		(1)
+
+/** Flags field byte offset */
+#define RPMI_MSG_FLAGS_OFFSET			(0x3)
+/** Flags field size in bytes */
+#define RPMI_MSG_FLAGS_SIZE			(1)
+
+#define RPMI_MSG_FLAGS_TYPE_POS			(0U)
+#define RPMI_MSG_FLAGS_TYPE_MASK		0x7
+#define RPMI_MSG_FLAGS_TYPE			\
+	((0x7) << RPMI_MSG_FLAGS_TYPE_POS)
+
+#define RPMI_MSG_FLAGS_DOORBELL_POS		(3U)
+#define RPMI_MSG_FLAGS_DOORBELL_MASK		0x1
+#define RPMI_MSG_FLAGS_DOORBELL			\
+	((0x1) << RPMI_MSG_FLAGS_DOORBELL_POS)
+
+/** Data length field byte offset */
+#define RPMI_MSG_DATALEN_OFFSET			(0x4)
+/** Data length field size in bytes */
+#define RPMI_MSG_DATALEN_SIZE			(2)
+
+/** Token field byte offset */
+#define RPMI_MSG_TOKEN_OFFSET			(0x6)
+/** Token field size in bytes */
+#define RPMI_MSG_TOKEN_SIZE			(2)
+/** Token field mask */
+#define RPMI_MSG_TOKEN_MASK			(0xffffU)
+
+/** Data field byte offset */
+#define RPMI_MSG_DATA_OFFSET			(RPMI_MSG_HDR_SIZE)
+/** Data field size in bytes */
+#define RPMI_MSG_DATA_SIZE(__slot_size)		((__slot_size) - RPMI_MSG_HDR_SIZE)
+
+/** Minimum slot size in bytes */
+#define RPMI_SLOT_SIZE_MIN			(64)
+
+/** Name length of 16 characters */
+#define RPMI_NAME_CHARS_MAX			(16)
+
+/** Queue layout */
+#define RPMI_QUEUE_HEAD_SLOT		0
+#define RPMI_QUEUE_TAIL_SLOT		1
+#define RPMI_QUEUE_HEADER_SLOTS		2
+
+/** Default timeout values */
+#define RPMI_DEF_TX_TIMEOUT			20
+#define RPMI_DEF_RX_TIMEOUT			20
+
+/** RPMI Message Header */
+struct rpmi_message_header {
+	le16_t servicegroup_id;
+	uint8_t service_id;
+	uint8_t flags;
+	le16_t datalen;
+	le16_t token;
+} __packed;
+
+/** RPMI Message */
+struct rpmi_message {
+	struct rpmi_message_header header;
+	u8 data[0];
+} __packed;
+
+/** RPMI Messages Types */
+enum rpmi_message_type {
+	/* Normal request backed with ack */
+	RPMI_MSG_NORMAL_REQUEST = 0x0,
+	/* Request without any ack */
+	RPMI_MSG_POSTED_REQUEST = 0x1,
+	/* Acknowledgment for normal request message */
+	RPMI_MSG_ACKNOWLDGEMENT = 0x2,
+	/* Notification message */
+	RPMI_MSG_NOTIFICATION = 0x3,
+};
+
+/** RPMI Error Types */
+enum rpmi_error {
+	RPMI_SUCCESS = 0,
+	RPMI_ERR_FAILED = -1,
+	RPMI_ERR_NOTSUPP = -2,
+	RPMI_ERR_INVAL = -3,
+	RPMI_ERR_DENIED = -4,
+	RPMI_ERR_NOTFOUND = -5,
+	RPMI_ERR_OUTOFRANGE = -6,
+	RPMI_ERR_OUTOFRES = -7,
+	RPMI_ERR_HWFAULT = -8,
+};
+
+/** RPMI Message Arguments */
+struct rpmi_message_args {
+	u32 flags;
+#define RPMI_MSG_FLAGS_NO_TX		(1U << 0)
+#define RPMI_MSG_FLAGS_NO_RX		(1U << 1)
+#define RPMI_MSG_FLAGS_NO_RX_TOKEN	(1U << 2)
+	enum rpmi_message_type type;
+	u8 service_id;
+	u32 tx_endian_words;
+	u32 rx_endian_words;
+	u16 rx_token;
+	u32 rx_data_len;
+};
+
+/*
+ * RPMI SERVICEGROUPS AND SERVICES
+ */
+
+/** RPMI ServiceGroups IDs */
+enum rpmi_servicegroup_id {
+	RPMI_SRVGRP_ID_MIN = 0,
+	RPMI_SRVGRP_BASE = 0x00001,
+	RPMI_SRVGRP_ID_MAX_COUNT,
+};
+
+/** RPMI enable notification request */
+struct rpmi_enable_notification_req {
+	u32 eventid;
+};
+
+/** RPMI enable notification response */
+struct rpmi_enable_notification_resp {
+	s32 status;
+};
+
+/** RPMI Base ServiceGroup Service IDs */
+enum rpmi_base_service_id {
+	RPMI_BASE_SRV_ENABLE_NOTIFICATION = 0x01,
+	RPMI_BASE_SRV_GET_IMPLEMENTATION_VERSION = 0x02,
+	RPMI_BASE_SRV_GET_IMPLEMENTATION_IDN = 0x03,
+	RPMI_BASE_SRV_GET_SPEC_VERSION = 0x04,
+	RPMI_BASE_SRV_GET_HW_INFO = 0x05,
+	RPMI_BASE_SRV_PROBE_SERVICE_GROUP = 0x06,
+	RPMI_BASE_SRV_GET_ATTRIBUTES = 0x07,
+	RPMI_BASE_SRV_SET_MSI = 0x08,
+};
+
+struct rpmi_base_get_attributes_resp {
+	s32 status_code;
+#define RPMI_BASE_FLAGS_F0_EV_NOTIFY		(1U << 31)
+#define RPMI_BASE_FLAGS_F0_MSI_EN			(1U << 30)
+	u32 f0;
+	u32 f1;
+	u32 f2;
+	u32 f3;
+};
+
+#endif /* !__RPMI_MSGPROT_H__ */
lib/utils/mailbox/Kconfig +14 -0
index 3957bfba..6e7f2cdd 100644
@@ -8,8 +8,22 @@ config FDT_MAILBOX
  	select MAILBOX
  	default n
 
+config RPMI_MAILBOX
+	bool "RPMI based mailbox drivers"
+	select MAILBOX
+	default n
+
  config MAILBOX
  	bool "Mailbox support"
  	default n
 
+if FDT_MAILBOX
+
+config FDT_MAILBOX_RPMI_SHMEM
+	bool "RPMI Shared Memory Mailbox Controller"
+	depends on RPMI_MAILBOX
+	default n
+
+endif
+
  endmenu
lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c new file +671 -0
new file mode 100644
index 00000000..9705507c
@@ -0,0 +1,671 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Rahul Pathak <[email protected]>
+ *   Subrahmanya Lingappa <[email protected]>
+ *   Anup Patel <[email protected]>
+ */
+
+#include <libfdt.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_heap.h>
+#include <sbi/sbi_timer.h>
+#include <sbi/riscv_io.h>
+#include <sbi/riscv_locks.h>
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_barrier.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/mailbox/mailbox.h>
+#include <sbi_utils/mailbox/fdt_mailbox.h>
+#include <sbi_utils/mailbox/rpmi_mailbox.h>
+
+/**************** RPMI Transport Structures and Macros ***********/
+
+#define RPMI_MAILBOX_CHANNELS_MAX	(16)
+
+#define GET_SERVICEGROUP_ID(msg)		\
+({						\
+	struct rpmi_message *mbuf = msg;	\
+	le16_to_cpu(mbuf->header.servicegroup_id);\
+})
+
+#define GET_SERVICE_ID(msg)			\
+({						\
+	struct rpmi_message *mbuf = msg;	\
+	mbuf->header.service_id;		\
+})
+
+#define GET_FLAGS(msg)				\
+({						\
+	struct rpmi_message *mbuf = msg;	\
+	mbuf->header.flags;			\
+})
+
+#define GET_MESSAGE_ID(msg)			\
+({						\
+	struct rpmi_message *mbuf = msg;	\
+	((u32)mbuf->header.flags << (RPMI_MSG_FLAGS_OFFSET * 8)) | \
+	((u32)mbuf->header.service_id << (RPMI_MSG_SERVICE_ID_OFFSET * 8)) | \
+	((u32)le16_to_cpu(mbuf->header.servicegroup_id)); \
+})
+
+#define MAKE_MESSAGE_ID(__group_id, __service_id, __flags)	\
+({						\
+	u32 __ret = 0;				\
+	__ret |= (u32)(__group_id) << (RPMI_MSG_SERVICEGROUP_ID_OFFSET * 8); \
+	__ret |= (u32)(__service_id) << (RPMI_MSG_SERVICE_ID_OFFSET * 8); \
+	__ret |= (u32)(__flags) << (RPMI_MSG_FLAGS_OFFSET * 8); \
+	__ret;					\
+})
+
+#define GET_DLEN(msg)				\
+({						\
+	struct rpmi_message *mbuf = msg;	\
+	le16_to_cpu(mbuf->header.datalen);	\
+})
+
+#define GET_TOKEN(msg)				\
+({						\
+	struct rpmi_message *mbuf = msg;	\
+	le16_to_cpu(mbuf->header.token);	\
+})
+
+#define GET_MESSAGE_TYPE(msg)						\
+({									\
+	uint8_t flags = *((uint8_t *)msg + RPMI_MSG_FLAGS_OFFSET);	\
+	((flags & RPMI_MSG_FLAGS_TYPE) >> RPMI_MSG_FLAGS_TYPE_POS));	\
+})
+
+enum rpmi_queue_type {
+	RPMI_QUEUE_TYPE_REQ = 0,
+	RPMI_QUEUE_TYPE_ACK = 1,
+};
+
+enum rpmi_queue_idx {
+	RPMI_QUEUE_IDX_A2P_REQ = 0,
+	RPMI_QUEUE_IDX_P2A_ACK = 1,
+	RPMI_QUEUE_IDX_P2A_REQ = 2,
+	RPMI_QUEUE_IDX_A2P_ACK = 3,
+	RPMI_QUEUE_IDX_MAX_COUNT,
+};
+
+enum rpmi_reg_idx {
+	RPMI_REG_IDX_DB_REG = 0, /* Doorbell register */
+	RPMI_REG_IDX_MAX_COUNT,
+};
+
+/** Mailbox registers */
+struct rpmi_mb_regs {
+	/* doorbell from AP -> PuC*/
+	volatile le32_t db_reg;
+} __packed;
+
+/** Single Queue Context Structure */
+struct smq_queue_ctx {
+	u32 queue_id;
+	u32 num_slots;
+	spinlock_t queue_lock;
+	/* Type of queue - REQ or ACK */
+	enum rpmi_queue_type queue_type;
+	/* Pointers to the queue shared memory */
+	volatile le32_t *headptr;
+	volatile le32_t *tailptr;
+	volatile uint8_t *buffer;
+	/* Name of the queue */
+	char name[RPMI_NAME_CHARS_MAX];
+};
+
+struct rpmi_shmem_mbox_controller {
+	/* Driver specific members */
+	u32 slot_size;
+	u32 queue_count;
+	struct rpmi_mb_regs *mb_regs;
+	struct smq_queue_ctx queue_ctx_tbl[RPMI_QUEUE_IDX_MAX_COUNT];
+	/* Mailbox framework related members */
+	struct mbox_controller controller;
+	struct mbox_chan channels[RPMI_MAILBOX_CHANNELS_MAX];
+	struct mbox_chan *base_chan;
+	u32 impl_version;
+	u32 impl_id;
+	u32 spec_version;
+	struct {
+		bool f0_ev_notif_en;
+		bool f0_msi_en;
+	} base_flags;
+};
+
+/**************** Shared Memory Queues Helpers **************/
+
+static bool __smq_queue_full(struct smq_queue_ctx *qctx)
+{
+	return ((le32_to_cpu(*qctx->tailptr) + 1) % qctx->num_slots ==
+			le32_to_cpu(*qctx->headptr)) ? true : false;
+}
+
+static bool __smq_queue_empty(struct smq_queue_ctx *qctx)
+{
+	return (le32_to_cpu(*qctx->headptr) ==
+		le32_to_cpu(*qctx->tailptr)) ? true : false;
+}
+
+static int __smq_rx(struct smq_queue_ctx *qctx, u32 slot_size,
+		    u32 service_group_id, struct mbox_xfer *xfer)
+{
+	void *dst, *src;
+	struct rpmi_message *msg;
+	u32 i, tmp, pos, dlen, msgidn, headidx, tailidx;
+	struct rpmi_message_args *args = xfer->args;
+	bool no_rx_token = (args->flags & RPMI_MSG_FLAGS_NO_RX_TOKEN) ?
+			   true : false;
+
+	/* Rx sanity checks */
+	if ((sizeof(u32) * args->rx_endian_words) >
+	    (slot_size - sizeof(struct rpmi_message_header)))
+		return SBI_EINVAL;
+	if ((sizeof(u32) * args->rx_endian_words) > xfer->rx_len)
+		return SBI_EINVAL;
+
+	/* There should be some message in the queue */
+	if (__smq_queue_empty(qctx))
+		return SBI_ENOENT;
+
+	/* Get the head/read index and tail/write index */
+	headidx = le32_to_cpu(*qctx->headptr);
+	tailidx = le32_to_cpu(*qctx->tailptr);
+
+	/*
+	 * Compute msgidn expected in the incoming message
+	 * NOTE: DOORBELL bit is not expected to be set.
+	 */
+	msgidn = MAKE_MESSAGE_ID(service_group_id, args->service_id, args->type);
+
+	/* Find the Rx message with matching token */
+	pos = headidx;
+	while (pos != tailidx) {
+		src = (void *)qctx->buffer + (pos * slot_size);
+		if ((no_rx_token && GET_MESSAGE_ID(src) == msgidn) ||
+		    (GET_TOKEN(src) == (xfer->seq & RPMI_MSG_TOKEN_MASK)))
+			break;
+		pos = (pos + 1) % qctx->num_slots;
+	}
+	if (pos == tailidx)
+		return SBI_ENOENT;
+
+	/* If Rx message is not first message then make it first message */
+	if (pos != headidx) {
+		src = (void *)qctx->buffer + (pos * slot_size);
+		dst = (void *)qctx->buffer + (headidx * slot_size);
+		for (i = 0; i < slot_size / sizeof(u32); i++) {
+			tmp = ((u32 *)dst)[i];
+			((u32 *)dst)[i] = ((u32 *)src)[i];
+			((u32 *)src)[i] = tmp;
+		}
+	}
+
+	/* Update rx_token if not available */
+	msg = (void *)qctx->buffer + (headidx * slot_size);
+	if (no_rx_token)
+		args->rx_token = GET_TOKEN(msg);
+
+	/* Extract data from the first message */
+	if (xfer->rx) {
+		args->rx_data_len = dlen = GET_DLEN(msg);
+		if (dlen > xfer->rx_len)
+			dlen = xfer->rx_len;
+		src = (void *)msg + sizeof(struct rpmi_message_header);
+		dst = xfer->rx;
+		for (i = 0; i < args->rx_endian_words; i++)
+			((u32 *)dst)[i] = le32_to_cpu(((u32 *)src)[i]);
+		dst += sizeof(u32) * args->rx_endian_words;
+		src += sizeof(u32) * args->rx_endian_words;
+		sbi_memcpy(dst, src,
+			xfer->rx_len - (sizeof(u32) * args->rx_endian_words));
+	}
+
+	/* Update the head/read index */
+	*qctx->headptr = cpu_to_le32(headidx + 1) % qctx->num_slots;
+	smp_wmb();
+
+	return SBI_OK;
+}
+
+static int __smq_tx(struct smq_queue_ctx *qctx, struct rpmi_mb_regs *mb_regs,
+		    u32 slot_size, u32 service_group_id, struct mbox_xfer *xfer)
+{
+	u32 i, tailidx;
+	void *dst, *src;
+	struct rpmi_message_header header = { 0 };
+	struct rpmi_message_args *args = xfer->args;
+
+	/* Tx sanity checks */
+	if ((sizeof(u32) * args->tx_endian_words) >
+	    (slot_size - sizeof(struct rpmi_message_header)))
+		return SBI_EINVAL;
+	if ((sizeof(u32) * args->tx_endian_words) > xfer->tx_len)
+		return SBI_EINVAL;
+
+	/* There should be some room in the queue */
+	if (__smq_queue_full(qctx))
+		return SBI_ENOMEM;
+
+	/* Get the tail/write index */
+	tailidx = le32_to_cpu(*qctx->tailptr);
+
+	/* Prepare the header to be written into the slot */
+	header.servicegroup_id = cpu_to_le16(service_group_id);
+	header.service_id = args->service_id;
+	header.flags = args->type;
+	header.datalen = cpu_to_le16((u16)xfer->tx_len);
+	header.token = cpu_to_le16((u16)xfer->seq);
+
+	/* Write header into the slot */
+	dst = (char *)qctx->buffer + (tailidx * slot_size);
+	sbi_memcpy(dst, &header, sizeof(header));
+	dst += sizeof(header);
+
+	/* Write data into the slot */
+	if (xfer->tx) {
+		src = xfer->tx;
+		for (i = 0; i < args->tx_endian_words; i++)
+			((u32 *)dst)[i] = cpu_to_le32(((u32 *)src)[i]);
+		dst += sizeof(u32) * args->tx_endian_words;
+		src += sizeof(u32) * args->tx_endian_words;
+		sbi_memcpy(dst, src,
+			xfer->tx_len - (sizeof(u32) * args->tx_endian_words));
+	}
+
+	/* Update the tail/write index */
+	*qctx->tailptr = cpu_to_le32(tailidx + 1) % qctx->num_slots;
+	smp_wmb();
+
+	/* Ring the RPMI doorbell if present */
+	if (mb_regs)
+		writel(cpu_to_le32(1), &mb_regs->db_reg);
+
+	return SBI_OK;
+}
+
+static int smq_rx(struct rpmi_shmem_mbox_controller *mctl,
+		  u32 queue_id, u32 service_group_id, struct mbox_xfer *xfer)
+{
+	int ret, rxretry = 0;
+	struct smq_queue_ctx *qctx;
+
+	if (mctl->queue_count < queue_id ||
+	    RPMI_MAILBOX_CHANNELS_MAX <= service_group_id) {
+		sbi_printf("%s: invalid queue_id or service_group_id\n",
+			   __func__);
+		return SBI_EINVAL;
+	}
+	qctx = &mctl->queue_ctx_tbl[queue_id];
+
+	/*
+	 * Once the timeout happens and call this function is returned
+	 * to the client then there is no way to deliver the response
+	 * message after that if it comes later.
+	 *
+	 * REVISIT: In complete timeout duration how much duration
+	 * it should wait(delay) before recv retry. udelay or mdelay
+	 */
+	do {
+		spin_lock(&qctx->queue_lock);
+		ret = __smq_rx(qctx, mctl->slot_size, service_group_id, xfer);
+		spin_unlock(&qctx->queue_lock);
+		if (!ret)
+			return 0;
+
+		sbi_timer_mdelay(1);
+		rxretry += 1;
+	} while (rxretry < xfer->rx_timeout);
+
+	return SBI_ETIMEDOUT;
+}
+
+static int smq_tx(struct rpmi_shmem_mbox_controller *mctl,
+		  u32 queue_id, u32 service_group_id, struct mbox_xfer *xfer)
+{
+	int ret, txretry = 0;
+	struct smq_queue_ctx *qctx;
+
+	if (mctl->queue_count < queue_id ||
+	    RPMI_MAILBOX_CHANNELS_MAX <= service_group_id) {
+		sbi_printf("%s: invalid queue_id or service_group_id\n",
+			   __func__);
+		return SBI_EINVAL;
+	}
+	qctx = &mctl->queue_ctx_tbl[queue_id];
+
+	/*
+	 * Ignoring the tx timeout since in RPMI has no mechanism
+	 * with which other side can let know about the reception of
+	 * message which marks as tx complete. For RPMI tx complete is
+	 * marked as done when message in successfully copied in queue.
+	 *
+	 * REVISIT: In complete timeout duration how much duration
+	 * it should wait(delay) before send retry. udelay or mdelay
+	 */
+	do {
+		spin_lock(&qctx->queue_lock);
+		ret = __smq_tx(qctx, mctl->mb_regs, mctl->slot_size,
+				service_group_id, xfer);
+		spin_unlock(&qctx->queue_lock);
+		if (!ret)
+			return 0;
+
+		sbi_timer_mdelay(1);
+		txretry += 1;
+	} while (txretry < xfer->tx_timeout);
+
+	return SBI_ETIMEDOUT;
+}
+
+static int smq_base_get_two_u32(struct rpmi_shmem_mbox_controller *mctl,
+				u32 service_id, u32 *inarg, u32 *outvals)
+{
+	return rpmi_normal_request_with_status(
+			mctl->base_chan, service_id,
+			inarg, (inarg) ? 1 : 0, (inarg) ? 1 : 0,
+			outvals, 2, 2);
+}
+
+/**************** Mailbox Controller Functions **************/
+
+static int rpmi_shmem_mbox_xfer(struct mbox_chan *chan, struct mbox_xfer *xfer)
+{
+	int ret;
+	u32 tx_qid = 0, rx_qid = 0;
+	struct rpmi_shmem_mbox_controller *mctl =
+			container_of(chan->mbox,
+				     struct rpmi_shmem_mbox_controller,
+				     controller);
+	struct rpmi_message_args *args = xfer->args;
+	bool do_tx = (args->flags & RPMI_MSG_FLAGS_NO_TX) ? false : true;
+	bool do_rx = (args->flags & RPMI_MSG_FLAGS_NO_RX) ? false : true;
+
+	if (!do_tx && !do_rx)
+		return SBI_EINVAL;
+
+	switch (args->type) {
+	case RPMI_MSG_NORMAL_REQUEST:
+		if (do_tx && do_rx) {
+			tx_qid = RPMI_QUEUE_IDX_A2P_REQ;
+			rx_qid = RPMI_QUEUE_IDX_P2A_ACK;
+		} else if (do_tx) {
+			tx_qid = RPMI_QUEUE_IDX_A2P_REQ;
+		} else if (do_rx) {
+			rx_qid = RPMI_QUEUE_IDX_P2A_REQ;
+		}
+		break;
+	case RPMI_MSG_POSTED_REQUEST:
+		if (do_tx && do_rx)
+			return SBI_EINVAL;
+		if (do_tx) {
+			tx_qid = RPMI_QUEUE_IDX_A2P_REQ;
+		} else {
+			rx_qid = RPMI_QUEUE_IDX_P2A_REQ;
+		}
+		break;
+	case RPMI_MSG_ACKNOWLDGEMENT:
+		if (do_tx && do_rx)
+			return SBI_EINVAL;
+		if (do_tx) {
+			tx_qid = RPMI_QUEUE_IDX_A2P_ACK;
+		} else {
+			rx_qid = RPMI_QUEUE_IDX_P2A_ACK;
+		}
+		break;
+	default:
+		return SBI_ENOTSUPP;
+	}
+
+	if (do_tx) {
+		ret = smq_tx(mctl, tx_qid, chan - mctl->channels, xfer);
+		if (ret)
+			return ret;
+	}
+
+	if (do_rx) {
+		ret = smq_rx(mctl, rx_qid, chan - mctl->channels, xfer);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static struct mbox_chan *rpmi_shmem_mbox_request_chan(
+						struct mbox_controller *mbox,
+						u32 *chan_args)
+{
+	int ret;
+	u32 tval[2] = { 0 };
+	struct rpmi_shmem_mbox_controller *mctl =
+			container_of(mbox,
+				     struct rpmi_shmem_mbox_controller,
+				     controller);
+
+	if (chan_args[0] >= RPMI_MAILBOX_CHANNELS_MAX)
+		return NULL;
+
+	/* Base serivce group is always present so probe other groups */
+	if (chan_args[0] != RPMI_SRVGRP_BASE) {
+		/* Probe service group */
+		ret = smq_base_get_two_u32(mctl,
+					   RPMI_BASE_SRV_PROBE_SERVICE_GROUP,
+					   chan_args, tval);
+		if (ret || !tval[1])
+			return NULL;
+	}
+
+	return &mctl->channels[chan_args[0]];
+}
+
+static void *rpmi_shmem_mbox_free_chan(struct mbox_controller *mbox,
+					struct mbox_chan *chan)
+{
+	/* Nothing to do here */
+	return NULL;
+}
+
+extern struct fdt_mailbox fdt_mailbox_rpmi_shmem;
+
+static int rpmi_shmem_transport_init(struct rpmi_shmem_mbox_controller *mctl,
+				     void *fdt, int nodeoff)
+{
+	const char *name;
+	int count, len, ret, qid;
+	uint64_t reg_addr, reg_size;
+	const fdt32_t *prop_slotsz;
+	struct smq_queue_ctx *qctx;
+
+	ret = fdt_node_check_compatible(fdt, nodeoff,
+					"riscv,rpmi-shmem-mbox");
+	if (ret)
+		return ret;
+
+	/* get queue slot size in bytes */
+	prop_slotsz = fdt_getprop(fdt, nodeoff, "riscv,slot-size", &len);
+	if (!prop_slotsz)
+		return SBI_ENOENT;
+
+	mctl->slot_size = fdt32_to_cpu(*prop_slotsz);
+	if (mctl->slot_size < RPMI_SLOT_SIZE_MIN) {
+		sbi_printf("%s: slot_size < mimnum required message size\n",
+			   __func__);
+		mctl->slot_size = RPMI_SLOT_SIZE_MIN;
+	}
+
+	/*
+	 * queue names count is taken as the number of queues
+	 * supported which make it mandatory to provide the
+	 * name of the queue.
+	 */
+	count = fdt_stringlist_count(fdt, nodeoff, "reg-names");
+	if (count < 0 ||
+	    count > (RPMI_QUEUE_IDX_MAX_COUNT + RPMI_REG_IDX_MAX_COUNT))
+		return SBI_EINVAL;
+
+	mctl->queue_count = count - RPMI_REG_IDX_MAX_COUNT;
+
+	/* parse all queues and populate queues context structure */
+	for (qid = 0; qid < mctl->queue_count; qid++) {
+		qctx = &mctl->queue_ctx_tbl[qid];
+
+		/* get each queue share-memory base address and size*/
+		ret = fdt_get_node_addr_size(fdt, nodeoff, qid,
+					     &reg_addr, &reg_size);
+		if (ret < 0 || !reg_addr || !reg_size)
+			return SBI_ENOENT;
+
+		ret = sbi_domain_root_add_memrange(reg_addr, reg_size, reg_size,
+						   (SBI_DOMAIN_MEMREGION_MMIO |
+						    SBI_DOMAIN_MEMREGION_M_READABLE |
+						    SBI_DOMAIN_MEMREGION_M_WRITABLE));
+		if (ret)
+			return ret;
+
+		/* calculate number of slots in each queue */
+		qctx->num_slots =
+			(reg_size - (mctl->slot_size * RPMI_QUEUE_HEADER_SLOTS)) / mctl->slot_size;
+
+		/* setup queue pointers */
+		qctx->headptr = ((void *)(unsigned long)reg_addr) +
+				RPMI_QUEUE_HEAD_SLOT * mctl->slot_size;
+		qctx->tailptr = ((void *)(unsigned long)reg_addr) +
+				RPMI_QUEUE_TAIL_SLOT * mctl->slot_size;
+		qctx->buffer = ((void *)(unsigned long)reg_addr) +
+				RPMI_QUEUE_HEADER_SLOTS * mctl->slot_size;
+
+		/* get the queue name */
+		name = fdt_stringlist_get(fdt, nodeoff, "reg-names",
+					  qid, &len);
+		if (!name || (name && len < 0))
+			return len;
+
+		sbi_memcpy(qctx->name, name, len);
+
+		/* store the index as queue_id */
+		qctx->queue_id = qid;
+
+		SPIN_LOCK_INIT(qctx->queue_lock);
+	}
+
+	/* get the db-reg property name */
+	name = fdt_stringlist_get(fdt, nodeoff, "reg-names", qid, &len);
+	if (!name || (name && len < 0))
+		return len;
+
+	/* fetch doorbell register address*/
+	ret = fdt_get_node_addr_size(fdt, nodeoff, qid, &reg_addr,
+				       &reg_size);
+	if (!ret && !(strncmp(name, "db-reg", strlen("db-reg")))) {
+		mctl->mb_regs = (void *)(unsigned long)reg_addr;
+		ret = sbi_domain_root_add_memrange(reg_addr, reg_size, reg_size,
+						   (SBI_DOMAIN_MEMREGION_MMIO |
+						    SBI_DOMAIN_MEMREGION_M_READABLE |
+						    SBI_DOMAIN_MEMREGION_M_WRITABLE));
+		if (ret)
+			return ret;
+	}
+
+	return SBI_SUCCESS;
+}
+
+static int rpmi_shmem_mbox_init(void *fdt, int nodeoff, u32 phandle,
+				const struct fdt_match *match)
+{
+	int ret = 0;
+	u32 tval[2];
+	struct rpmi_base_get_attributes_resp resp;
+	struct rpmi_shmem_mbox_controller *mctl;
+
+	mctl = sbi_zalloc(sizeof(*mctl));
+	if (!mctl)
+		return SBI_ENOMEM;
+
+	/* Initialization transport from device tree */
+	ret = rpmi_shmem_transport_init(mctl, fdt, nodeoff);
+	if (ret)
+		goto fail_free_controller;
+
+	/* Register mailbox controller */
+	mctl->controller.id = phandle;
+	mctl->controller.max_xfer_len =
+			mctl->slot_size - sizeof(struct rpmi_message_header);
+	mctl->controller.driver = &fdt_mailbox_rpmi_shmem;
+	mctl->controller.request_chan = rpmi_shmem_mbox_request_chan;
+	mctl->controller.free_chan = rpmi_shmem_mbox_free_chan;
+	mctl->controller.xfer = rpmi_shmem_mbox_xfer;
+	ret = mbox_controller_add(&mctl->controller);
+	if (ret)
+		goto fail_free_controller;
+
+	/* Request base service group channel */
+	tval[0] = RPMI_SRVGRP_BASE;
+	mctl->base_chan = mbox_controller_request_chan(&mctl->controller,
+							tval);
+	if (!mctl->base_chan) {
+		ret = SBI_ENOENT;
+		goto fail_remove_controller;
+	}
+
+	/* Get implementation id */
+	ret = smq_base_get_two_u32(mctl,
+				   RPMI_BASE_SRV_GET_IMPLEMENTATION_VERSION,
+				   NULL, tval);
+	if (ret)
+		goto fail_free_chan;
+	mctl->impl_version = tval[1];
+
+	/* Get implementation version */
+	ret = smq_base_get_two_u32(mctl, RPMI_BASE_SRV_GET_IMPLEMENTATION_IDN,
+				   NULL, tval);
+	if (ret)
+		goto fail_free_chan;
+	mctl->impl_id = tval[1];
+
+	/* Get specification version */
+	ret = smq_base_get_two_u32(mctl, RPMI_BASE_SRV_GET_SPEC_VERSION,
+				   NULL, tval);
+	if (ret)
+		goto fail_free_chan;
+	mctl->spec_version = tval[1];
+
+	/* Get optional features implementation flags */
+	ret = rpmi_normal_request_with_status(
+			mctl->base_chan, RPMI_BASE_SRV_GET_ATTRIBUTES,
+			NULL, 0, 0,
+			&resp, rpmi_u32_count(resp), rpmi_u32_count(resp));
+	if (ret)
+		goto fail_free_chan;
+
+	mctl->base_flags.f0_ev_notif_en =
+			resp.f0 & RPMI_BASE_FLAGS_F0_EV_NOTIFY ? 1 : 0;
+	mctl->base_flags.f0_msi_en =
+			resp.f0 & RPMI_BASE_FLAGS_F0_MSI_EN ? 1 : 0;
+
+	return 0;
+
+fail_free_chan:
+	mbox_controller_free_chan(mctl->base_chan);
+fail_remove_controller:
+	mbox_controller_remove(&mctl->controller);
+fail_free_controller:
+	sbi_free(mctl);
+	return ret;
+}
+
+static const struct fdt_match rpmi_shmem_mbox_match[] = {
+	{ .compatible = "riscv,rpmi-shmem-mbox" },
+	{ },
+};
+
+struct fdt_mailbox fdt_mailbox_rpmi_shmem = {
+	.match_table = rpmi_shmem_mbox_match,
+	.init = rpmi_shmem_mbox_init,
+	.xlate = fdt_mailbox_simple_xlate,
+};
lib/utils/mailbox/objects.mk +5 -0
index 2135898c..746b0313 100644
@@ -11,3 +11,8 @@ libsbiutils-objs-$(CONFIG_FDT_MAILBOX) += mailbox/fdt_mailbox.o
  libsbiutils-objs-$(CONFIG_FDT_MAILBOX) += mailbox/fdt_mailbox_drivers.carray.o
 
  libsbiutils-objs-$(CONFIG_MAILBOX) += mailbox/mailbox.o
+
+libsbiutils-objs-$(CONFIG_RPMI_MAILBOX) += mailbox/rpmi_mailbox.o
+
+carray-fdt_mailbox_drivers-$(CONFIG_FDT_MAILBOX_RPMI_SHMEM) += fdt_mailbox_rpmi_shmem
+libsbiutils-objs-$(CONFIG_FDT_MAILBOX_RPMI_SHMEM) += mailbox/fdt_mailbox_rpmi_shmem.o
lib/utils/mailbox/rpmi_mailbox.c new file +79 -0
new file mode 100644
index 00000000..58c64e56
@@ -0,0 +1,79 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#include <sbi/sbi_error.h>
+#include <sbi_utils/mailbox/mailbox.h>
+#include <sbi_utils/mailbox/rpmi_mailbox.h>
+
+int rpmi_xlate_error(enum rpmi_error error)
+{
+	switch (error) {
+	case RPMI_SUCCESS:
+		return SBI_OK;
+	case RPMI_ERR_FAILED:
+		return SBI_EFAIL;
+	case RPMI_ERR_NOTSUPP:
+		return SBI_ENOTSUPP;
+	case RPMI_ERR_INVAL:
+		return SBI_EINVAL;
+	case RPMI_ERR_DENIED:
+		return SBI_EDENIED;
+	case RPMI_ERR_NOTFOUND:
+		return SBI_ENOENT;
+	case RPMI_ERR_OUTOFRANGE:
+		return SBI_EINVAL;
+	case RPMI_ERR_OUTOFRES:
+		return SBI_ENOSPC;
+	case RPMI_ERR_HWFAULT:
+		return SBI_EIO;
+	default:
+		return SBI_EUNKNOWN;
+	}
+}
+
+int rpmi_normal_request_with_status(
+			struct mbox_chan *chan, u32 service_id,
+			void *req, u32 req_words, u32 req_endian_words,
+			void *resp, u32 resp_words, u32 resp_endian_words)
+{
+	int ret;
+	struct mbox_xfer xfer;
+	struct rpmi_message_args args = { 0 };
+
+	args.type = RPMI_MSG_NORMAL_REQUEST;
+	args.service_id = service_id;
+	args.tx_endian_words = req_endian_words;
+	args.rx_endian_words = resp_endian_words;
+	mbox_xfer_init_txrx(&xfer, &args,
+			req, sizeof(u32) * req_words, RPMI_DEF_TX_TIMEOUT,
+			resp, sizeof(u32) * resp_words, RPMI_DEF_RX_TIMEOUT);
+
+	ret = mbox_chan_xfer(chan, &xfer);
+	if (ret)
+		return ret;
+
+	return rpmi_xlate_error(((u32 *)resp)[0]);
+}
+
+int rpmi_posted_request(
+		struct mbox_chan *chan, u32 service_id,
+		void *req, u32 req_words, u32 req_endian_words)
+{
+	struct mbox_xfer xfer;
+	struct rpmi_message_args args = { 0 };
+
+	args.type = RPMI_MSG_POSTED_REQUEST;
+	args.flags = RPMI_MSG_FLAGS_NO_RX;
+	args.service_id = service_id;
+	args.tx_endian_words = req_endian_words;
+	mbox_xfer_init_tx(&xfer, &args,
+			  req, sizeof(u32) * req_words, RPMI_DEF_TX_TIMEOUT);
+
+	return mbox_chan_xfer(chan, &xfer);
+}
platform/generic/configs/defconfig +3 -0
index 079bc4fe..233a9a89 100644
@@ -20,6 +20,9 @@ CONFIG_FDT_IRQCHIP=y
  CONFIG_FDT_IRQCHIP_APLIC=y
  CONFIG_FDT_IRQCHIP_IMSIC=y
  CONFIG_FDT_IRQCHIP_PLIC=y
+CONFIG_FDT_MAILBOX=y
+CONFIG_RPMI_MAILBOX=y
+CONFIG_FDT_MAILBOX_RPMI_SHMEM=y
  CONFIG_FDT_REGMAP=y
  CONFIG_FDT_REGMAP_SYSCON=y
  CONFIG_FDT_RESET=y
author
#6 lore raw

PATCH 4/16 lib/utils: Add RPMI messaging protocol and shared memory transport support

Quoted 8 lines · On Fri, Aug 16, 2024 at 6:31?AM Bo Gan <[email protected]> wrote:

Hi All,

I have some high level questions.

Given that the RPMI shared memory transport works on HWs described as:
https://github.com/riscv-software-src/librpmi/blob/main/docs/librpmi-arch.png

Quoted 14 lines

I'm wondering maybe we should also define the memory/cache architecture we are targeting in this AP-PuC design. E.g., one problem I can think of is that AP and PuC are often not cache-coherent. Thus, when doing Tx/Rx, we might need cache flush/invalidation. The memory order involving head/ tailptr, and read/write queue elements is also important. Even if we are dealing with a cache-coherent design, the usage of smp_wmb() after updating both the queue element and head/tailptr doesn't seems to be right. I think it should reflect a load-acquire semantic on headptr and a store-release semantic on tailptr. I'm sure there're very good references on how to handle memory order on this classic headptr/tailptr pattern, and we probably need to rework this part a little bit. For non-cache-coherent HW, I'm really not sure how to code this correctly, but my instinct tells me we need to be even more careful, make use of cache flushes, and ensure PuC observes the memory in the correct order.

There are 4 possible scenarios for coherent view of RPMI shared memory between PuC and AP:
1) RPMI shared memory is cacheable for both PuC and AP
2) RPMI shared memory is cacheable for PuC but non-cacheable for AP
3) RPMI shared memory is non-cacheable for PuC and cacheable for AP
4) RPMI shared memory is non-cacheable for both PuC and AP

For #1 and #4, nothing special is required on the PuC and AP side to ensure a coherent view of the RPMI shared memory.

For #2, PuC will have to use cache-clean operation after writing and cache-invalidate operation before reading the RPMI shared memory whereas AP need not do anything. To handle this case in librpmi, PuC firmware can either use rpmi_shmem_simple_noncoherent_ops or provide custom platform operations when creating RPMI shared memory instances.
(Refer, https://github.com/riscv-software-src/librpmi/pull/23)

For #3, AP will have to use cache-clean operation after writing and cache-invalidate operation before reading the RPMI shared memory whereas PuC need not do anything. To handle this case in OpenSBI (running on AP), we can use optional "dma-non-coherent" DT property of the RPMI shared memory DT node which will allow the RPMI shared memory transport driver to do cache-clean after writing to the shared memory and cache-invalidate before reading the shared memory.

The #1 and #4 scenarios will be very common in the real world. The #2 scenario is also possible if PuC does not have any mechanism to mark shared memory as non-cacheable in HW. The #3 is very unlikely because most APs (RISC-V CPUs) have PMAs to mark shared memory as non-cacheable for OpenSBI (M-mode).

Due to the above reasons, the current OpenSBI RPMI patches (posted recently) don't implement the optional "dma-non-coherent" DT property in RPMI shared memory mailbox driver.

Regards,
Anup

Quoted 1,084 lines

The librpmi project might be the place where we can document AP-PuC memory architectures for both cache-coherent/non-coherent cases, and provide some guidance to PuC firmware writers. Excuse me if I'm raising an already solved issue, as I'm not following RPMI discussion closely. Thanks!

Bo

Quoted 1,075 lines · Anup Patel

From: Rahul Pathak <[email protected]>

The RISC-V Platform Management Interface (RPMI) defines a messaging protocol and shared memory based transport for bi-directional communication with an on-chip or external microcontroller.

To support RPMI in OpenSBI, add:
1) The RPMI messaging protocol defines and helper macros
2) A FDT mailbox driver for the RPMI shared memory transport

---
  include/sbi_utils/mailbox/rpmi_mailbox.h   |  32 +
  include/sbi_utils/mailbox/rpmi_msgprot.h   | 186 ++++++
  lib/utils/mailbox/Kconfig                  |  14 +
  lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c | 671 +++++++++++++++++++++
  lib/utils/mailbox/objects.mk               |   5 +
  lib/utils/mailbox/rpmi_mailbox.c           |  79 +++
  platform/generic/configs/defconfig         |   3 +
  7 files changed, 990 insertions(+)

create mode 100644 include/sbi_utils/mailbox/rpmi_mailbox.h create mode 100644 include/sbi_utils/mailbox/rpmi_msgprot.h create mode 100644 lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c create mode 100644 lib/utils/mailbox/rpmi_mailbox.c

include/sbi_utils/mailbox/rpmi_mailbox.h new file +32 -0
new file mode 100644
index 00000000..61af51a8
@@ -0,0 +1,32 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#ifndef __RPMI_MAILBOX_H__
+#define __RPMI_MAILBOX_H__
+
+#include <sbi/sbi_error.h>
+#include <sbi_utils/mailbox/rpmi_msgprot.h>
+
+#define rpmi_u32_count(__var)        (sizeof(__var) / sizeof(u32))
+
+/** Convert RPMI error to SBI error */
+int rpmi_xlate_error(enum rpmi_error error);
+
+/** Typical RPMI normal request with at least status code in response */
+int rpmi_normal_request_with_status(
+                     struct mbox_chan *chan, u32 service_id,
+                     void *req, u32 req_words, u32 req_endian_words,
+                     void *resp, u32 resp_words, u32 resp_endian_words);
+
+/* RPMI posted request which is without any response*/
+int rpmi_posted_request(
+             struct mbox_chan *chan, u32 service_id,
+             void *req, u32 req_words, u32 req_endian_words);
+
+#endif /* !__RPMI_MAILBOX_H__ */
include/sbi_utils/mailbox/rpmi_msgprot.h new file +186 -0
new file mode 100644
index 00000000..e0c7cba0
@@ -0,0 +1,186 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Rahul Pathak <[email protected]>
+ */
+
+#ifndef __RPMI_MSGPROT_H__
+#define __RPMI_MSGPROT_H__
+
+#include <sbi/sbi_byteorder.h>
+#include <sbi/sbi_error.h>
+
+/*
+ * 31                                            0
+ * +---------------------+-----------------------+
+ * | FLAGS | SERVICE_ID  |   SERVICEGROUP_ID     |
+ * +---------------------+-----------------------+
+ * |        TOKEN        |     DATA LENGTH       |
+ * +---------------------+-----------------------+
+ * |                 DATA/PAYLOAD                |
+ * +---------------------------------------------+
+ */
+
+/** Message Header byte offset */
+#define RPMI_MSG_HDR_OFFSET                  (0x0)
+/** Message Header Size in bytes */
+#define RPMI_MSG_HDR_SIZE                    (8)
+
+/** ServiceGroup ID field byte offset */
+#define RPMI_MSG_SERVICEGROUP_ID_OFFSET              (0x0)
+/** ServiceGroup ID field size in bytes */
+#define RPMI_MSG_SERVICEGROUP_ID_SIZE                (2)
+
+/** Service ID field byte offset */
+#define RPMI_MSG_SERVICE_ID_OFFSET           (0x2)
+/** Service ID field size in bytes */
+#define RPMI_MSG_SERVICE_ID_SIZE             (1)
+
+/** Flags field byte offset */
+#define RPMI_MSG_FLAGS_OFFSET                        (0x3)
+/** Flags field size in bytes */
+#define RPMI_MSG_FLAGS_SIZE                  (1)
+
+#define RPMI_MSG_FLAGS_TYPE_POS                      (0U)
+#define RPMI_MSG_FLAGS_TYPE_MASK             0x7
+#define RPMI_MSG_FLAGS_TYPE                  \
+     ((0x7) << RPMI_MSG_FLAGS_TYPE_POS)
+
+#define RPMI_MSG_FLAGS_DOORBELL_POS          (3U)
+#define RPMI_MSG_FLAGS_DOORBELL_MASK         0x1
+#define RPMI_MSG_FLAGS_DOORBELL                      \
+     ((0x1) << RPMI_MSG_FLAGS_DOORBELL_POS)
+
+/** Data length field byte offset */
+#define RPMI_MSG_DATALEN_OFFSET                      (0x4)
+/** Data length field size in bytes */
+#define RPMI_MSG_DATALEN_SIZE                        (2)
+
+/** Token field byte offset */
+#define RPMI_MSG_TOKEN_OFFSET                        (0x6)
+/** Token field size in bytes */
+#define RPMI_MSG_TOKEN_SIZE                  (2)
+/** Token field mask */
+#define RPMI_MSG_TOKEN_MASK                  (0xffffU)
+
+/** Data field byte offset */
+#define RPMI_MSG_DATA_OFFSET                 (RPMI_MSG_HDR_SIZE)
+/** Data field size in bytes */
+#define RPMI_MSG_DATA_SIZE(__slot_size)              ((__slot_size) - RPMI_MSG_HDR_SIZE)
+
+/** Minimum slot size in bytes */
+#define RPMI_SLOT_SIZE_MIN                   (64)
+
+/** Name length of 16 characters */
+#define RPMI_NAME_CHARS_MAX                  (16)
+
+/** Queue layout */
+#define RPMI_QUEUE_HEAD_SLOT         0
+#define RPMI_QUEUE_TAIL_SLOT         1
+#define RPMI_QUEUE_HEADER_SLOTS              2
+
+/** Default timeout values */
+#define RPMI_DEF_TX_TIMEOUT                  20
+#define RPMI_DEF_RX_TIMEOUT                  20
+
+/** RPMI Message Header */
+struct rpmi_message_header {
+     le16_t servicegroup_id;
+     uint8_t service_id;
+     uint8_t flags;
+     le16_t datalen;
+     le16_t token;
+} __packed;
+
+/** RPMI Message */
+struct rpmi_message {
+     struct rpmi_message_header header;
+     u8 data[0];
+} __packed;
+
+/** RPMI Messages Types */
+enum rpmi_message_type {
+     /* Normal request backed with ack */
+     RPMI_MSG_NORMAL_REQUEST = 0x0,
+     /* Request without any ack */
+     RPMI_MSG_POSTED_REQUEST = 0x1,
+     /* Acknowledgment for normal request message */
+     RPMI_MSG_ACKNOWLDGEMENT = 0x2,
+     /* Notification message */
+     RPMI_MSG_NOTIFICATION = 0x3,
+};
+
+/** RPMI Error Types */
+enum rpmi_error {
+     RPMI_SUCCESS = 0,
+     RPMI_ERR_FAILED = -1,
+     RPMI_ERR_NOTSUPP = -2,
+     RPMI_ERR_INVAL = -3,
+     RPMI_ERR_DENIED = -4,
+     RPMI_ERR_NOTFOUND = -5,
+     RPMI_ERR_OUTOFRANGE = -6,
+     RPMI_ERR_OUTOFRES = -7,
+     RPMI_ERR_HWFAULT = -8,
+};
+
+/** RPMI Message Arguments */
+struct rpmi_message_args {
+     u32 flags;
+#define RPMI_MSG_FLAGS_NO_TX         (1U << 0)
+#define RPMI_MSG_FLAGS_NO_RX         (1U << 1)
+#define RPMI_MSG_FLAGS_NO_RX_TOKEN   (1U << 2)
+     enum rpmi_message_type type;
+     u8 service_id;
+     u32 tx_endian_words;
+     u32 rx_endian_words;
+     u16 rx_token;
+     u32 rx_data_len;
+};
+
+/*
+ * RPMI SERVICEGROUPS AND SERVICES
+ */
+
+/** RPMI ServiceGroups IDs */
+enum rpmi_servicegroup_id {
+     RPMI_SRVGRP_ID_MIN = 0,
+     RPMI_SRVGRP_BASE = 0x00001,
+     RPMI_SRVGRP_ID_MAX_COUNT,
+};
+
+/** RPMI enable notification request */
+struct rpmi_enable_notification_req {
+     u32 eventid;
+};
+
+/** RPMI enable notification response */
+struct rpmi_enable_notification_resp {
+     s32 status;
+};
+
+/** RPMI Base ServiceGroup Service IDs */
+enum rpmi_base_service_id {
+     RPMI_BASE_SRV_ENABLE_NOTIFICATION = 0x01,
+     RPMI_BASE_SRV_GET_IMPLEMENTATION_VERSION = 0x02,
+     RPMI_BASE_SRV_GET_IMPLEMENTATION_IDN = 0x03,
+     RPMI_BASE_SRV_GET_SPEC_VERSION = 0x04,
+     RPMI_BASE_SRV_GET_HW_INFO = 0x05,
+     RPMI_BASE_SRV_PROBE_SERVICE_GROUP = 0x06,
+     RPMI_BASE_SRV_GET_ATTRIBUTES = 0x07,
+     RPMI_BASE_SRV_SET_MSI = 0x08,
+};
+
+struct rpmi_base_get_attributes_resp {
+     s32 status_code;
+#define RPMI_BASE_FLAGS_F0_EV_NOTIFY         (1U << 31)
+#define RPMI_BASE_FLAGS_F0_MSI_EN                    (1U << 30)
+     u32 f0;
+     u32 f1;
+     u32 f2;
+     u32 f3;
+};
+
+#endif /* !__RPMI_MSGPROT_H__ */
lib/utils/mailbox/Kconfig +14 -0
index 3957bfba..6e7f2cdd 100644
@@ -8,8 +8,22 @@ config FDT_MAILBOX
      select MAILBOX
      default n
 
+config RPMI_MAILBOX
+     bool "RPMI based mailbox drivers"
+     select MAILBOX
+     default n
+
  config MAILBOX
      bool "Mailbox support"
      default n
 
+if FDT_MAILBOX
+
+config FDT_MAILBOX_RPMI_SHMEM
+     bool "RPMI Shared Memory Mailbox Controller"
+     depends on RPMI_MAILBOX
+     default n
+
+endif
+
  endmenu
lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c new file +671 -0
new file mode 100644
index 00000000..9705507c
@@ -0,0 +1,671 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Rahul Pathak <[email protected]>
+ *   Subrahmanya Lingappa <[email protected]>
+ *   Anup Patel <[email protected]>
+ */
+
+#include <libfdt.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_heap.h>
+#include <sbi/sbi_timer.h>
+#include <sbi/riscv_io.h>
+#include <sbi/riscv_locks.h>
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_barrier.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/mailbox/mailbox.h>
+#include <sbi_utils/mailbox/fdt_mailbox.h>
+#include <sbi_utils/mailbox/rpmi_mailbox.h>
+
+/**************** RPMI Transport Structures and Macros ***********/
+
+#define RPMI_MAILBOX_CHANNELS_MAX    (16)
+
+#define GET_SERVICEGROUP_ID(msg)             \
+({                                           \
+     struct rpmi_message *mbuf = msg;        \
+     le16_to_cpu(mbuf->header.servicegroup_id);\
+})
+
+#define GET_SERVICE_ID(msg)                  \
+({                                           \
+     struct rpmi_message *mbuf = msg;        \
+     mbuf->header.service_id;                \
+})
+
+#define GET_FLAGS(msg)                               \
+({                                           \
+     struct rpmi_message *mbuf = msg;        \
+     mbuf->header.flags;                     \
+})
+
+#define GET_MESSAGE_ID(msg)                  \
+({                                           \
+     struct rpmi_message *mbuf = msg;        \
+     ((u32)mbuf->header.flags << (RPMI_MSG_FLAGS_OFFSET * 8)) | \
+     ((u32)mbuf->header.service_id << (RPMI_MSG_SERVICE_ID_OFFSET * 8)) | \
+     ((u32)le16_to_cpu(mbuf->header.servicegroup_id)); \
+})
+
+#define MAKE_MESSAGE_ID(__group_id, __service_id, __flags)   \
+({                                           \
+     u32 __ret = 0;                          \
+     __ret |= (u32)(__group_id) << (RPMI_MSG_SERVICEGROUP_ID_OFFSET * 8); \
+     __ret |= (u32)(__service_id) << (RPMI_MSG_SERVICE_ID_OFFSET * 8); \
+     __ret |= (u32)(__flags) << (RPMI_MSG_FLAGS_OFFSET * 8); \
+     __ret;                                  \
+})
+
+#define GET_DLEN(msg)                                \
+({                                           \
+     struct rpmi_message *mbuf = msg;        \
+     le16_to_cpu(mbuf->header.datalen);      \
+})
+
+#define GET_TOKEN(msg)                               \
+({                                           \
+     struct rpmi_message *mbuf = msg;        \
+     le16_to_cpu(mbuf->header.token);        \
+})
+
+#define GET_MESSAGE_TYPE(msg)                                                \
+({                                                                   \
+     uint8_t flags = *((uint8_t *)msg + RPMI_MSG_FLAGS_OFFSET);      \
+     ((flags & RPMI_MSG_FLAGS_TYPE) >> RPMI_MSG_FLAGS_TYPE_POS));    \
+})
+
+enum rpmi_queue_type {
+     RPMI_QUEUE_TYPE_REQ = 0,
+     RPMI_QUEUE_TYPE_ACK = 1,
+};
+
+enum rpmi_queue_idx {
+     RPMI_QUEUE_IDX_A2P_REQ = 0,
+     RPMI_QUEUE_IDX_P2A_ACK = 1,
+     RPMI_QUEUE_IDX_P2A_REQ = 2,
+     RPMI_QUEUE_IDX_A2P_ACK = 3,
+     RPMI_QUEUE_IDX_MAX_COUNT,
+};
+
+enum rpmi_reg_idx {
+     RPMI_REG_IDX_DB_REG = 0, /* Doorbell register */
+     RPMI_REG_IDX_MAX_COUNT,
+};
+
+/** Mailbox registers */
+struct rpmi_mb_regs {
+     /* doorbell from AP -> PuC*/
+     volatile le32_t db_reg;
+} __packed;
+
+/** Single Queue Context Structure */
+struct smq_queue_ctx {
+     u32 queue_id;
+     u32 num_slots;
+     spinlock_t queue_lock;
+     /* Type of queue - REQ or ACK */
+     enum rpmi_queue_type queue_type;
+     /* Pointers to the queue shared memory */
+     volatile le32_t *headptr;
+     volatile le32_t *tailptr;
+     volatile uint8_t *buffer;
+     /* Name of the queue */
+     char name[RPMI_NAME_CHARS_MAX];
+};
+
+struct rpmi_shmem_mbox_controller {
+     /* Driver specific members */
+     u32 slot_size;
+     u32 queue_count;
+     struct rpmi_mb_regs *mb_regs;
+     struct smq_queue_ctx queue_ctx_tbl[RPMI_QUEUE_IDX_MAX_COUNT];
+     /* Mailbox framework related members */
+     struct mbox_controller controller;
+     struct mbox_chan channels[RPMI_MAILBOX_CHANNELS_MAX];
+     struct mbox_chan *base_chan;
+     u32 impl_version;
+     u32 impl_id;
+     u32 spec_version;
+     struct {
+             bool f0_ev_notif_en;
+             bool f0_msi_en;
+     } base_flags;
+};
+
+/**************** Shared Memory Queues Helpers **************/
+
+static bool __smq_queue_full(struct smq_queue_ctx *qctx)
+{
+     return ((le32_to_cpu(*qctx->tailptr) + 1) % qctx->num_slots ==
+                     le32_to_cpu(*qctx->headptr)) ? true : false;
+}
+
+static bool __smq_queue_empty(struct smq_queue_ctx *qctx)
+{
+     return (le32_to_cpu(*qctx->headptr) ==
+             le32_to_cpu(*qctx->tailptr)) ? true : false;
+}
+
+static int __smq_rx(struct smq_queue_ctx *qctx, u32 slot_size,
+                 u32 service_group_id, struct mbox_xfer *xfer)
+{
+     void *dst, *src;
+     struct rpmi_message *msg;
+     u32 i, tmp, pos, dlen, msgidn, headidx, tailidx;
+     struct rpmi_message_args *args = xfer->args;
+     bool no_rx_token = (args->flags & RPMI_MSG_FLAGS_NO_RX_TOKEN) ?
+                        true : false;
+
+     /* Rx sanity checks */
+     if ((sizeof(u32) * args->rx_endian_words) >
+         (slot_size - sizeof(struct rpmi_message_header)))
+             return SBI_EINVAL;
+     if ((sizeof(u32) * args->rx_endian_words) > xfer->rx_len)
+             return SBI_EINVAL;
+
+     /* There should be some message in the queue */
+     if (__smq_queue_empty(qctx))
+             return SBI_ENOENT;
+
+     /* Get the head/read index and tail/write index */
+     headidx = le32_to_cpu(*qctx->headptr);
+     tailidx = le32_to_cpu(*qctx->tailptr);
+
+     /*
+      * Compute msgidn expected in the incoming message
+      * NOTE: DOORBELL bit is not expected to be set.
+      */
+     msgidn = MAKE_MESSAGE_ID(service_group_id, args->service_id, args->type);
+
+     /* Find the Rx message with matching token */
+     pos = headidx;
+     while (pos != tailidx) {
+             src = (void *)qctx->buffer + (pos * slot_size);
+             if ((no_rx_token && GET_MESSAGE_ID(src) == msgidn) ||
+                 (GET_TOKEN(src) == (xfer->seq & RPMI_MSG_TOKEN_MASK)))
+                     break;
+             pos = (pos + 1) % qctx->num_slots;
+     }
+     if (pos == tailidx)
+             return SBI_ENOENT;
+
+     /* If Rx message is not first message then make it first message */
+     if (pos != headidx) {
+             src = (void *)qctx->buffer + (pos * slot_size);
+             dst = (void *)qctx->buffer + (headidx * slot_size);
+             for (i = 0; i < slot_size / sizeof(u32); i++) {
+                     tmp = ((u32 *)dst)[i];
+                     ((u32 *)dst)[i] = ((u32 *)src)[i];
+                     ((u32 *)src)[i] = tmp;
+             }
+     }
+
+     /* Update rx_token if not available */
+     msg = (void *)qctx->buffer + (headidx * slot_size);
+     if (no_rx_token)
+             args->rx_token = GET_TOKEN(msg);
+
+     /* Extract data from the first message */
+     if (xfer->rx) {
+             args->rx_data_len = dlen = GET_DLEN(msg);
+             if (dlen > xfer->rx_len)
+                     dlen = xfer->rx_len;
+             src = (void *)msg + sizeof(struct rpmi_message_header);
+             dst = xfer->rx;
+             for (i = 0; i < args->rx_endian_words; i++)
+                     ((u32 *)dst)[i] = le32_to_cpu(((u32 *)src)[i]);
+             dst += sizeof(u32) * args->rx_endian_words;
+             src += sizeof(u32) * args->rx_endian_words;
+             sbi_memcpy(dst, src,
+                     xfer->rx_len - (sizeof(u32) * args->rx_endian_words));
+     }
+
+     /* Update the head/read index */
+     *qctx->headptr = cpu_to_le32(headidx + 1) % qctx->num_slots;
+     smp_wmb();
+
+     return SBI_OK;
+}
+
+static int __smq_tx(struct smq_queue_ctx *qctx, struct rpmi_mb_regs *mb_regs,
+                 u32 slot_size, u32 service_group_id, struct mbox_xfer *xfer)
+{
+     u32 i, tailidx;
+     void *dst, *src;
+     struct rpmi_message_header header = { 0 };
+     struct rpmi_message_args *args = xfer->args;
+
+     /* Tx sanity checks */
+     if ((sizeof(u32) * args->tx_endian_words) >
+         (slot_size - sizeof(struct rpmi_message_header)))
+             return SBI_EINVAL;
+     if ((sizeof(u32) * args->tx_endian_words) > xfer->tx_len)
+             return SBI_EINVAL;
+
+     /* There should be some room in the queue */
+     if (__smq_queue_full(qctx))
+             return SBI_ENOMEM;
+
+     /* Get the tail/write index */
+     tailidx = le32_to_cpu(*qctx->tailptr);
+
+     /* Prepare the header to be written into the slot */
+     header.servicegroup_id = cpu_to_le16(service_group_id);
+     header.service_id = args->service_id;
+     header.flags = args->type;
+     header.datalen = cpu_to_le16((u16)xfer->tx_len);
+     header.token = cpu_to_le16((u16)xfer->seq);
+
+     /* Write header into the slot */
+     dst = (char *)qctx->buffer + (tailidx * slot_size);
+     sbi_memcpy(dst, &header, sizeof(header));
+     dst += sizeof(header);
+
+     /* Write data into the slot */
+     if (xfer->tx) {
+             src = xfer->tx;
+             for (i = 0; i < args->tx_endian_words; i++)
+                     ((u32 *)dst)[i] = cpu_to_le32(((u32 *)src)[i]);
+             dst += sizeof(u32) * args->tx_endian_words;
+             src += sizeof(u32) * args->tx_endian_words;
+             sbi_memcpy(dst, src,
+                     xfer->tx_len - (sizeof(u32) * args->tx_endian_words));
+     }
+
+     /* Update the tail/write index */
+     *qctx->tailptr = cpu_to_le32(tailidx + 1) % qctx->num_slots;
+     smp_wmb();
+
+     /* Ring the RPMI doorbell if present */
+     if (mb_regs)
+             writel(cpu_to_le32(1), &mb_regs->db_reg);
+
+     return SBI_OK;
+}
+
+static int smq_rx(struct rpmi_shmem_mbox_controller *mctl,
+               u32 queue_id, u32 service_group_id, struct mbox_xfer *xfer)
+{
+     int ret, rxretry = 0;
+     struct smq_queue_ctx *qctx;
+
+     if (mctl->queue_count < queue_id ||
+         RPMI_MAILBOX_CHANNELS_MAX <= service_group_id) {
+             sbi_printf("%s: invalid queue_id or service_group_id\n",
+                        __func__);
+             return SBI_EINVAL;
+     }
+     qctx = &mctl->queue_ctx_tbl[queue_id];
+
+     /*
+      * Once the timeout happens and call this function is returned
+      * to the client then there is no way to deliver the response
+      * message after that if it comes later.
+      *
+      * REVISIT: In complete timeout duration how much duration
+      * it should wait(delay) before recv retry. udelay or mdelay
+      */
+     do {
+             spin_lock(&qctx->queue_lock);
+             ret = __smq_rx(qctx, mctl->slot_size, service_group_id, xfer);
+             spin_unlock(&qctx->queue_lock);
+             if (!ret)
+                     return 0;
+
+             sbi_timer_mdelay(1);
+             rxretry += 1;
+     } while (rxretry < xfer->rx_timeout);
+
+     return SBI_ETIMEDOUT;
+}
+
+static int smq_tx(struct rpmi_shmem_mbox_controller *mctl,
+               u32 queue_id, u32 service_group_id, struct mbox_xfer *xfer)
+{
+     int ret, txretry = 0;
+     struct smq_queue_ctx *qctx;
+
+     if (mctl->queue_count < queue_id ||
+         RPMI_MAILBOX_CHANNELS_MAX <= service_group_id) {
+             sbi_printf("%s: invalid queue_id or service_group_id\n",
+                        __func__);
+             return SBI_EINVAL;
+     }
+     qctx = &mctl->queue_ctx_tbl[queue_id];
+
+     /*
+      * Ignoring the tx timeout since in RPMI has no mechanism
+      * with which other side can let know about the reception of
+      * message which marks as tx complete. For RPMI tx complete is
+      * marked as done when message in successfully copied in queue.
+      *
+      * REVISIT: In complete timeout duration how much duration
+      * it should wait(delay) before send retry. udelay or mdelay
+      */
+     do {
+             spin_lock(&qctx->queue_lock);
+             ret = __smq_tx(qctx, mctl->mb_regs, mctl->slot_size,
+                             service_group_id, xfer);
+             spin_unlock(&qctx->queue_lock);
+             if (!ret)
+                     return 0;
+
+             sbi_timer_mdelay(1);
+             txretry += 1;
+     } while (txretry < xfer->tx_timeout);
+
+     return SBI_ETIMEDOUT;
+}
+
+static int smq_base_get_two_u32(struct rpmi_shmem_mbox_controller *mctl,
+                             u32 service_id, u32 *inarg, u32 *outvals)
+{
+     return rpmi_normal_request_with_status(
+                     mctl->base_chan, service_id,
+                     inarg, (inarg) ? 1 : 0, (inarg) ? 1 : 0,
+                     outvals, 2, 2);
+}
+
+/**************** Mailbox Controller Functions **************/
+
+static int rpmi_shmem_mbox_xfer(struct mbox_chan *chan, struct mbox_xfer *xfer)
+{
+     int ret;
+     u32 tx_qid = 0, rx_qid = 0;
+     struct rpmi_shmem_mbox_controller *mctl =
+                     container_of(chan->mbox,
+                                  struct rpmi_shmem_mbox_controller,
+                                  controller);
+     struct rpmi_message_args *args = xfer->args;
+     bool do_tx = (args->flags & RPMI_MSG_FLAGS_NO_TX) ? false : true;
+     bool do_rx = (args->flags & RPMI_MSG_FLAGS_NO_RX) ? false : true;
+
+     if (!do_tx && !do_rx)
+             return SBI_EINVAL;
+
+     switch (args->type) {
+     case RPMI_MSG_NORMAL_REQUEST:
+             if (do_tx && do_rx) {
+                     tx_qid = RPMI_QUEUE_IDX_A2P_REQ;
+                     rx_qid = RPMI_QUEUE_IDX_P2A_ACK;
+             } else if (do_tx) {
+                     tx_qid = RPMI_QUEUE_IDX_A2P_REQ;
+             } else if (do_rx) {
+                     rx_qid = RPMI_QUEUE_IDX_P2A_REQ;
+             }
+             break;
+     case RPMI_MSG_POSTED_REQUEST:
+             if (do_tx && do_rx)
+                     return SBI_EINVAL;
+             if (do_tx) {
+                     tx_qid = RPMI_QUEUE_IDX_A2P_REQ;
+             } else {
+                     rx_qid = RPMI_QUEUE_IDX_P2A_REQ;
+             }
+             break;
+     case RPMI_MSG_ACKNOWLDGEMENT:
+             if (do_tx && do_rx)
+                     return SBI_EINVAL;
+             if (do_tx) {
+                     tx_qid = RPMI_QUEUE_IDX_A2P_ACK;
+             } else {
+                     rx_qid = RPMI_QUEUE_IDX_P2A_ACK;
+             }
+             break;
+     default:
+             return SBI_ENOTSUPP;
+     }
+
+     if (do_tx) {
+             ret = smq_tx(mctl, tx_qid, chan - mctl->channels, xfer);
+             if (ret)
+                     return ret;
+     }
+
+     if (do_rx) {
+             ret = smq_rx(mctl, rx_qid, chan - mctl->channels, xfer);
+             if (ret)
+                     return ret;
+     }
+
+     return 0;
+}
+
+static struct mbox_chan *rpmi_shmem_mbox_request_chan(
+                                             struct mbox_controller *mbox,
+                                             u32 *chan_args)
+{
+     int ret;
+     u32 tval[2] = { 0 };
+     struct rpmi_shmem_mbox_controller *mctl =
+                     container_of(mbox,
+                                  struct rpmi_shmem_mbox_controller,
+                                  controller);
+
+     if (chan_args[0] >= RPMI_MAILBOX_CHANNELS_MAX)
+             return NULL;
+
+     /* Base serivce group is always present so probe other groups */
+     if (chan_args[0] != RPMI_SRVGRP_BASE) {
+             /* Probe service group */
+             ret = smq_base_get_two_u32(mctl,
+                                        RPMI_BASE_SRV_PROBE_SERVICE_GROUP,
+                                        chan_args, tval);
+             if (ret || !tval[1])
+                     return NULL;
+     }
+
+     return &mctl->channels[chan_args[0]];
+}
+
+static void *rpmi_shmem_mbox_free_chan(struct mbox_controller *mbox,
+                                     struct mbox_chan *chan)
+{
+     /* Nothing to do here */
+     return NULL;
+}
+
+extern struct fdt_mailbox fdt_mailbox_rpmi_shmem;
+
+static int rpmi_shmem_transport_init(struct rpmi_shmem_mbox_controller *mctl,
+                                  void *fdt, int nodeoff)
+{
+     const char *name;
+     int count, len, ret, qid;
+     uint64_t reg_addr, reg_size;
+     const fdt32_t *prop_slotsz;
+     struct smq_queue_ctx *qctx;
+
+     ret = fdt_node_check_compatible(fdt, nodeoff,
+                                     "riscv,rpmi-shmem-mbox");
+     if (ret)
+             return ret;
+
+     /* get queue slot size in bytes */
+     prop_slotsz = fdt_getprop(fdt, nodeoff, "riscv,slot-size", &len);
+     if (!prop_slotsz)
+             return SBI_ENOENT;
+
+     mctl->slot_size = fdt32_to_cpu(*prop_slotsz);
+     if (mctl->slot_size < RPMI_SLOT_SIZE_MIN) {
+             sbi_printf("%s: slot_size < mimnum required message size\n",
+                        __func__);
+             mctl->slot_size = RPMI_SLOT_SIZE_MIN;
+     }
+
+     /*
+      * queue names count is taken as the number of queues
+      * supported which make it mandatory to provide the
+      * name of the queue.
+      */
+     count = fdt_stringlist_count(fdt, nodeoff, "reg-names");
+     if (count < 0 ||
+         count > (RPMI_QUEUE_IDX_MAX_COUNT + RPMI_REG_IDX_MAX_COUNT))
+             return SBI_EINVAL;
+
+     mctl->queue_count = count - RPMI_REG_IDX_MAX_COUNT;
+
+     /* parse all queues and populate queues context structure */
+     for (qid = 0; qid < mctl->queue_count; qid++) {
+             qctx = &mctl->queue_ctx_tbl[qid];
+
+             /* get each queue share-memory base address and size*/
+             ret = fdt_get_node_addr_size(fdt, nodeoff, qid,
+                                          &reg_addr, &reg_size);
+             if (ret < 0 || !reg_addr || !reg_size)
+                     return SBI_ENOENT;
+
+             ret = sbi_domain_root_add_memrange(reg_addr, reg_size, reg_size,
+                                                (SBI_DOMAIN_MEMREGION_MMIO |
+                                                 SBI_DOMAIN_MEMREGION_M_READABLE |
+                                                 SBI_DOMAIN_MEMREGION_M_WRITABLE));
+             if (ret)
+                     return ret;
+
+             /* calculate number of slots in each queue */
+             qctx->num_slots =
+                     (reg_size - (mctl->slot_size * RPMI_QUEUE_HEADER_SLOTS)) / mctl->slot_size;
+
+             /* setup queue pointers */
+             qctx->headptr = ((void *)(unsigned long)reg_addr) +
+                             RPMI_QUEUE_HEAD_SLOT * mctl->slot_size;
+             qctx->tailptr = ((void *)(unsigned long)reg_addr) +
+                             RPMI_QUEUE_TAIL_SLOT * mctl->slot_size;
+             qctx->buffer = ((void *)(unsigned long)reg_addr) +
+                             RPMI_QUEUE_HEADER_SLOTS * mctl->slot_size;
+
+             /* get the queue name */
+             name = fdt_stringlist_get(fdt, nodeoff, "reg-names",
+                                       qid, &len);
+             if (!name || (name && len < 0))
+                     return len;
+
+             sbi_memcpy(qctx->name, name, len);
+
+             /* store the index as queue_id */
+             qctx->queue_id = qid;
+
+             SPIN_LOCK_INIT(qctx->queue_lock);
+     }
+
+     /* get the db-reg property name */
+     name = fdt_stringlist_get(fdt, nodeoff, "reg-names", qid, &len);
+     if (!name || (name && len < 0))
+             return len;
+
+     /* fetch doorbell register address*/
+     ret = fdt_get_node_addr_size(fdt, nodeoff, qid, &reg_addr,
+                                    &reg_size);
+     if (!ret && !(strncmp(name, "db-reg", strlen("db-reg")))) {
+             mctl->mb_regs = (void *)(unsigned long)reg_addr;
+             ret = sbi_domain_root_add_memrange(reg_addr, reg_size, reg_size,
+                                                (SBI_DOMAIN_MEMREGION_MMIO |
+                                                 SBI_DOMAIN_MEMREGION_M_READABLE |
+                                                 SBI_DOMAIN_MEMREGION_M_WRITABLE));
+             if (ret)
+                     return ret;
+     }
+
+     return SBI_SUCCESS;
+}
+
+static int rpmi_shmem_mbox_init(void *fdt, int nodeoff, u32 phandle,
+                             const struct fdt_match *match)
+{
+     int ret = 0;
+     u32 tval[2];
+     struct rpmi_base_get_attributes_resp resp;
+     struct rpmi_shmem_mbox_controller *mctl;
+
+     mctl = sbi_zalloc(sizeof(*mctl));
+     if (!mctl)
+             return SBI_ENOMEM;
+
+     /* Initialization transport from device tree */
+     ret = rpmi_shmem_transport_init(mctl, fdt, nodeoff);
+     if (ret)
+             goto fail_free_controller;
+
+     /* Register mailbox controller */
+     mctl->controller.id = phandle;
+     mctl->controller.max_xfer_len =
+                     mctl->slot_size - sizeof(struct rpmi_message_header);
+     mctl->controller.driver = &fdt_mailbox_rpmi_shmem;
+     mctl->controller.request_chan = rpmi_shmem_mbox_request_chan;
+     mctl->controller.free_chan = rpmi_shmem_mbox_free_chan;
+     mctl->controller.xfer = rpmi_shmem_mbox_xfer;
+     ret = mbox_controller_add(&mctl->controller);
+     if (ret)
+             goto fail_free_controller;
+
+     /* Request base service group channel */
+     tval[0] = RPMI_SRVGRP_BASE;
+     mctl->base_chan = mbox_controller_request_chan(&mctl->controller,
+                                                     tval);
+     if (!mctl->base_chan) {
+             ret = SBI_ENOENT;
+             goto fail_remove_controller;
+     }
+
+     /* Get implementation id */
+     ret = smq_base_get_two_u32(mctl,
+                                RPMI_BASE_SRV_GET_IMPLEMENTATION_VERSION,
+                                NULL, tval);
+     if (ret)
+             goto fail_free_chan;
+     mctl->impl_version = tval[1];
+
+     /* Get implementation version */
+     ret = smq_base_get_two_u32(mctl, RPMI_BASE_SRV_GET_IMPLEMENTATION_IDN,
+                                NULL, tval);
+     if (ret)
+             goto fail_free_chan;
+     mctl->impl_id = tval[1];
+
+     /* Get specification version */
+     ret = smq_base_get_two_u32(mctl, RPMI_BASE_SRV_GET_SPEC_VERSION,
+                                NULL, tval);
+     if (ret)
+             goto fail_free_chan;
+     mctl->spec_version = tval[1];
+
+     /* Get optional features implementation flags */
+     ret = rpmi_normal_request_with_status(
+                     mctl->base_chan, RPMI_BASE_SRV_GET_ATTRIBUTES,
+                     NULL, 0, 0,
+                     &resp, rpmi_u32_count(resp), rpmi_u32_count(resp));
+     if (ret)
+             goto fail_free_chan;
+
+     mctl->base_flags.f0_ev_notif_en =
+                     resp.f0 & RPMI_BASE_FLAGS_F0_EV_NOTIFY ? 1 : 0;
+     mctl->base_flags.f0_msi_en =
+                     resp.f0 & RPMI_BASE_FLAGS_F0_MSI_EN ? 1 : 0;
+
+     return 0;
+
+fail_free_chan:
+     mbox_controller_free_chan(mctl->base_chan);
+fail_remove_controller:
+     mbox_controller_remove(&mctl->controller);
+fail_free_controller:
+     sbi_free(mctl);
+     return ret;
+}
+
+static const struct fdt_match rpmi_shmem_mbox_match[] = {
+     { .compatible = "riscv,rpmi-shmem-mbox" },
+     { },
+};
+
+struct fdt_mailbox fdt_mailbox_rpmi_shmem = {
+     .match_table = rpmi_shmem_mbox_match,
+     .init = rpmi_shmem_mbox_init,
+     .xlate = fdt_mailbox_simple_xlate,
+};
lib/utils/mailbox/objects.mk +5 -0
index 2135898c..746b0313 100644
@@ -11,3 +11,8 @@ libsbiutils-objs-$(CONFIG_FDT_MAILBOX) += mailbox/fdt_mailbox.o
  libsbiutils-objs-$(CONFIG_FDT_MAILBOX) += mailbox/fdt_mailbox_drivers.carray.o
 
  libsbiutils-objs-$(CONFIG_MAILBOX) += mailbox/mailbox.o
+
+libsbiutils-objs-$(CONFIG_RPMI_MAILBOX) += mailbox/rpmi_mailbox.o
+
+carray-fdt_mailbox_drivers-$(CONFIG_FDT_MAILBOX_RPMI_SHMEM) += fdt_mailbox_rpmi_shmem
+libsbiutils-objs-$(CONFIG_FDT_MAILBOX_RPMI_SHMEM) += mailbox/fdt_mailbox_rpmi_shmem.o
lib/utils/mailbox/rpmi_mailbox.c new file +79 -0
new file mode 100644
index 00000000..58c64e56
@@ -0,0 +1,79 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#include <sbi/sbi_error.h>
+#include <sbi_utils/mailbox/mailbox.h>
+#include <sbi_utils/mailbox/rpmi_mailbox.h>
+
+int rpmi_xlate_error(enum rpmi_error error)
+{
+     switch (error) {
+     case RPMI_SUCCESS:
+             return SBI_OK;
+     case RPMI_ERR_FAILED:
+             return SBI_EFAIL;
+     case RPMI_ERR_NOTSUPP:
+             return SBI_ENOTSUPP;
+     case RPMI_ERR_INVAL:
+             return SBI_EINVAL;
+     case RPMI_ERR_DENIED:
+             return SBI_EDENIED;
+     case RPMI_ERR_NOTFOUND:
+             return SBI_ENOENT;
+     case RPMI_ERR_OUTOFRANGE:
+             return SBI_EINVAL;
+     case RPMI_ERR_OUTOFRES:
+             return SBI_ENOSPC;
+     case RPMI_ERR_HWFAULT:
+             return SBI_EIO;
+     default:
+             return SBI_EUNKNOWN;
+     }
+}
+
+int rpmi_normal_request_with_status(
+                     struct mbox_chan *chan, u32 service_id,
+                     void *req, u32 req_words, u32 req_endian_words,
+                     void *resp, u32 resp_words, u32 resp_endian_words)
+{
+     int ret;
+     struct mbox_xfer xfer;
+     struct rpmi_message_args args = { 0 };
+
+     args.type = RPMI_MSG_NORMAL_REQUEST;
+     args.service_id = service_id;
+     args.tx_endian_words = req_endian_words;
+     args.rx_endian_words = resp_endian_words;
+     mbox_xfer_init_txrx(&xfer, &args,
+                     req, sizeof(u32) * req_words, RPMI_DEF_TX_TIMEOUT,
+                     resp, sizeof(u32) * resp_words, RPMI_DEF_RX_TIMEOUT);
+
+     ret = mbox_chan_xfer(chan, &xfer);
+     if (ret)
+             return ret;
+
+     return rpmi_xlate_error(((u32 *)resp)[0]);
+}
+
+int rpmi_posted_request(
+             struct mbox_chan *chan, u32 service_id,
+             void *req, u32 req_words, u32 req_endian_words)
+{
+     struct mbox_xfer xfer;
+     struct rpmi_message_args args = { 0 };
+
+     args.type = RPMI_MSG_POSTED_REQUEST;
+     args.flags = RPMI_MSG_FLAGS_NO_RX;
+     args.service_id = service_id;
+     args.tx_endian_words = req_endian_words;
+     mbox_xfer_init_tx(&xfer, &args,
+                       req, sizeof(u32) * req_words, RPMI_DEF_TX_TIMEOUT);
+
+     return mbox_chan_xfer(chan, &xfer);
+}
platform/generic/configs/defconfig +3 -0
index 079bc4fe..233a9a89 100644
@@ -20,6 +20,9 @@ CONFIG_FDT_IRQCHIP=y
  CONFIG_FDT_IRQCHIP_APLIC=y
  CONFIG_FDT_IRQCHIP_IMSIC=y
  CONFIG_FDT_IRQCHIP_PLIC=y
+CONFIG_FDT_MAILBOX=y
+CONFIG_RPMI_MAILBOX=y
+CONFIG_FDT_MAILBOX_RPMI_SHMEM=y
  CONFIG_FDT_REGMAP=y
  CONFIG_FDT_REGMAP_SYSCON=y
  CONFIG_FDT_RESET=y
#7 lore raw

PATCH 4/16 lib/utils: Add RPMI messaging protocol and shared memory transport support

In reply to Anup Patel

Hi Anup,

Based on the previous discussion (4 scenarios of cache coherency),I have the following comments (see inline) mainly focused on memory ordering.

Quoted 453 lines · Anup Patel

From: Rahul Pathak <[email protected]>

The RISC-V Platform Management Interface (RPMI) defines a messaging protocol and shared memory based transport for bi-directional communication with an on-chip or external microcontroller.

To support RPMI in OpenSBI, add:
1) The RPMI messaging protocol defines and helper macros
2) A FDT mailbox driver for the RPMI shared memory transport

---
  include/sbi_utils/mailbox/rpmi_mailbox.h   |  32 +
  include/sbi_utils/mailbox/rpmi_msgprot.h   | 186 ++++++
  lib/utils/mailbox/Kconfig                  |  14 +
  lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c | 671 +++++++++++++++++++++
  lib/utils/mailbox/objects.mk               |   5 +
  lib/utils/mailbox/rpmi_mailbox.c           |  79 +++
  platform/generic/configs/defconfig         |   3 +
  7 files changed, 990 insertions(+)

create mode 100644 include/sbi_utils/mailbox/rpmi_mailbox.h create mode 100644 include/sbi_utils/mailbox/rpmi_msgprot.h create mode 100644 lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c create mode 100644 lib/utils/mailbox/rpmi_mailbox.c

include/sbi_utils/mailbox/rpmi_mailbox.h new file +32 -0
new file mode 100644
index 00000000..61af51a8
@@ -0,0 +1,32 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#ifndef __RPMI_MAILBOX_H__
+#define __RPMI_MAILBOX_H__
+
+#include <sbi/sbi_error.h>
+#include <sbi_utils/mailbox/rpmi_msgprot.h>
+
+#define rpmi_u32_count(__var)	(sizeof(__var) / sizeof(u32))
+
+/** Convert RPMI error to SBI error */
+int rpmi_xlate_error(enum rpmi_error error);
+
+/** Typical RPMI normal request with at least status code in response */
+int rpmi_normal_request_with_status(
+			struct mbox_chan *chan, u32 service_id,
+			void *req, u32 req_words, u32 req_endian_words,
+			void *resp, u32 resp_words, u32 resp_endian_words);
+
+/* RPMI posted request which is without any response*/
+int rpmi_posted_request(
+		struct mbox_chan *chan, u32 service_id,
+		void *req, u32 req_words, u32 req_endian_words);
+
+#endif /* !__RPMI_MAILBOX_H__ */
include/sbi_utils/mailbox/rpmi_msgprot.h new file +186 -0
new file mode 100644
index 00000000..e0c7cba0
@@ -0,0 +1,186 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Rahul Pathak <[email protected]>
+ */
+
+#ifndef __RPMI_MSGPROT_H__
+#define __RPMI_MSGPROT_H__
+
+#include <sbi/sbi_byteorder.h>
+#include <sbi/sbi_error.h>
+
+/*
+ * 31                                            0
+ * +---------------------+-----------------------+
+ * | FLAGS | SERVICE_ID  |   SERVICEGROUP_ID     |
+ * +---------------------+-----------------------+
+ * |        TOKEN        |     DATA LENGTH       |
+ * +---------------------+-----------------------+
+ * |                 DATA/PAYLOAD                |
+ * +---------------------------------------------+
+ */
+
+/** Message Header byte offset */
+#define RPMI_MSG_HDR_OFFSET			(0x0)
+/** Message Header Size in bytes */
+#define RPMI_MSG_HDR_SIZE			(8)
+
+/** ServiceGroup ID field byte offset */
+#define RPMI_MSG_SERVICEGROUP_ID_OFFSET		(0x0)
+/** ServiceGroup ID field size in bytes */
+#define RPMI_MSG_SERVICEGROUP_ID_SIZE		(2)
+
+/** Service ID field byte offset */
+#define RPMI_MSG_SERVICE_ID_OFFSET		(0x2)
+/** Service ID field size in bytes */
+#define RPMI_MSG_SERVICE_ID_SIZE		(1)
+
+/** Flags field byte offset */
+#define RPMI_MSG_FLAGS_OFFSET			(0x3)
+/** Flags field size in bytes */
+#define RPMI_MSG_FLAGS_SIZE			(1)
+
+#define RPMI_MSG_FLAGS_TYPE_POS			(0U)
+#define RPMI_MSG_FLAGS_TYPE_MASK		0x7
+#define RPMI_MSG_FLAGS_TYPE			\
+	((0x7) << RPMI_MSG_FLAGS_TYPE_POS)
+
+#define RPMI_MSG_FLAGS_DOORBELL_POS		(3U)
+#define RPMI_MSG_FLAGS_DOORBELL_MASK		0x1
+#define RPMI_MSG_FLAGS_DOORBELL			\
+	((0x1) << RPMI_MSG_FLAGS_DOORBELL_POS)
+
+/** Data length field byte offset */
+#define RPMI_MSG_DATALEN_OFFSET			(0x4)
+/** Data length field size in bytes */
+#define RPMI_MSG_DATALEN_SIZE			(2)
+
+/** Token field byte offset */
+#define RPMI_MSG_TOKEN_OFFSET			(0x6)
+/** Token field size in bytes */
+#define RPMI_MSG_TOKEN_SIZE			(2)
+/** Token field mask */
+#define RPMI_MSG_TOKEN_MASK			(0xffffU)
+
+/** Data field byte offset */
+#define RPMI_MSG_DATA_OFFSET			(RPMI_MSG_HDR_SIZE)
+/** Data field size in bytes */
+#define RPMI_MSG_DATA_SIZE(__slot_size)		((__slot_size) - RPMI_MSG_HDR_SIZE)
+
+/** Minimum slot size in bytes */
+#define RPMI_SLOT_SIZE_MIN			(64)
+
+/** Name length of 16 characters */
+#define RPMI_NAME_CHARS_MAX			(16)
+
+/** Queue layout */
+#define RPMI_QUEUE_HEAD_SLOT		0
+#define RPMI_QUEUE_TAIL_SLOT		1
+#define RPMI_QUEUE_HEADER_SLOTS		2
+
+/** Default timeout values */
+#define RPMI_DEF_TX_TIMEOUT			20
+#define RPMI_DEF_RX_TIMEOUT			20
+
+/** RPMI Message Header */
+struct rpmi_message_header {
+	le16_t servicegroup_id;
+	uint8_t service_id;
+	uint8_t flags;
+	le16_t datalen;
+	le16_t token;
+} __packed;
+
+/** RPMI Message */
+struct rpmi_message {
+	struct rpmi_message_header header;
+	u8 data[0];
+} __packed;
+
+/** RPMI Messages Types */
+enum rpmi_message_type {
+	/* Normal request backed with ack */
+	RPMI_MSG_NORMAL_REQUEST = 0x0,
+	/* Request without any ack */
+	RPMI_MSG_POSTED_REQUEST = 0x1,
+	/* Acknowledgment for normal request message */
+	RPMI_MSG_ACKNOWLDGEMENT = 0x2,
+	/* Notification message */
+	RPMI_MSG_NOTIFICATION = 0x3,
+};
+
+/** RPMI Error Types */
+enum rpmi_error {
+	RPMI_SUCCESS = 0,
+	RPMI_ERR_FAILED = -1,
+	RPMI_ERR_NOTSUPP = -2,
+	RPMI_ERR_INVAL = -3,
+	RPMI_ERR_DENIED = -4,
+	RPMI_ERR_NOTFOUND = -5,
+	RPMI_ERR_OUTOFRANGE = -6,
+	RPMI_ERR_OUTOFRES = -7,
+	RPMI_ERR_HWFAULT = -8,
+};
+
+/** RPMI Message Arguments */
+struct rpmi_message_args {
+	u32 flags;
+#define RPMI_MSG_FLAGS_NO_TX		(1U << 0)
+#define RPMI_MSG_FLAGS_NO_RX		(1U << 1)
+#define RPMI_MSG_FLAGS_NO_RX_TOKEN	(1U << 2)
+	enum rpmi_message_type type;
+	u8 service_id;
+	u32 tx_endian_words;
+	u32 rx_endian_words;
+	u16 rx_token;
+	u32 rx_data_len;
+};
+
+/*
+ * RPMI SERVICEGROUPS AND SERVICES
+ */
+
+/** RPMI ServiceGroups IDs */
+enum rpmi_servicegroup_id {
+	RPMI_SRVGRP_ID_MIN = 0,
+	RPMI_SRVGRP_BASE = 0x00001,
+	RPMI_SRVGRP_ID_MAX_COUNT,
+};
+
+/** RPMI enable notification request */
+struct rpmi_enable_notification_req {
+	u32 eventid;
+};
+
+/** RPMI enable notification response */
+struct rpmi_enable_notification_resp {
+	s32 status;
+};
+
+/** RPMI Base ServiceGroup Service IDs */
+enum rpmi_base_service_id {
+	RPMI_BASE_SRV_ENABLE_NOTIFICATION = 0x01,
+	RPMI_BASE_SRV_GET_IMPLEMENTATION_VERSION = 0x02,
+	RPMI_BASE_SRV_GET_IMPLEMENTATION_IDN = 0x03,
+	RPMI_BASE_SRV_GET_SPEC_VERSION = 0x04,
+	RPMI_BASE_SRV_GET_HW_INFO = 0x05,
+	RPMI_BASE_SRV_PROBE_SERVICE_GROUP = 0x06,
+	RPMI_BASE_SRV_GET_ATTRIBUTES = 0x07,
+	RPMI_BASE_SRV_SET_MSI = 0x08,
+};
+
+struct rpmi_base_get_attributes_resp {
+	s32 status_code;
+#define RPMI_BASE_FLAGS_F0_EV_NOTIFY		(1U << 31)
+#define RPMI_BASE_FLAGS_F0_MSI_EN			(1U << 30)
+	u32 f0;
+	u32 f1;
+	u32 f2;
+	u32 f3;
+};
+
+#endif /* !__RPMI_MSGPROT_H__ */
lib/utils/mailbox/Kconfig +14 -0
index 3957bfba..6e7f2cdd 100644
@@ -8,8 +8,22 @@ config FDT_MAILBOX
  	select MAILBOX
  	default n
 
+config RPMI_MAILBOX
+	bool "RPMI based mailbox drivers"
+	select MAILBOX
+	default n
+
  config MAILBOX
  	bool "Mailbox support"
  	default n
 
+if FDT_MAILBOX
+
+config FDT_MAILBOX_RPMI_SHMEM
+	bool "RPMI Shared Memory Mailbox Controller"
+	depends on RPMI_MAILBOX
+	default n
+
+endif
+
  endmenu
lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c new file +161 -0
new file mode 100644
index 00000000..9705507c
@@ -0,0 +1,671 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Rahul Pathak <[email protected]>
+ *   Subrahmanya Lingappa <[email protected]>
+ *   Anup Patel <[email protected]>
+ */
+
+#include <libfdt.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_heap.h>
+#include <sbi/sbi_timer.h>
+#include <sbi/riscv_io.h>
+#include <sbi/riscv_locks.h>
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_barrier.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/mailbox/mailbox.h>
+#include <sbi_utils/mailbox/fdt_mailbox.h>
+#include <sbi_utils/mailbox/rpmi_mailbox.h>
+
+/**************** RPMI Transport Structures and Macros ***********/
+
+#define RPMI_MAILBOX_CHANNELS_MAX	(16)
+
+#define GET_SERVICEGROUP_ID(msg)		\
+({						\
+	struct rpmi_message *mbuf = msg;	\
+	le16_to_cpu(mbuf->header.servicegroup_id);\
+})
+
+#define GET_SERVICE_ID(msg)			\
+({						\
+	struct rpmi_message *mbuf = msg;	\
+	mbuf->header.service_id;		\
+})
+
+#define GET_FLAGS(msg)				\
+({						\
+	struct rpmi_message *mbuf = msg;	\
+	mbuf->header.flags;			\
+})
+
+#define GET_MESSAGE_ID(msg)			\
+({						\
+	struct rpmi_message *mbuf = msg;	\
+	((u32)mbuf->header.flags << (RPMI_MSG_FLAGS_OFFSET * 8)) | \
+	((u32)mbuf->header.service_id << (RPMI_MSG_SERVICE_ID_OFFSET * 8)) | \
+	((u32)le16_to_cpu(mbuf->header.servicegroup_id)); \
+})
+
+#define MAKE_MESSAGE_ID(__group_id, __service_id, __flags)	\
+({						\
+	u32 __ret = 0;				\
+	__ret |= (u32)(__group_id) << (RPMI_MSG_SERVICEGROUP_ID_OFFSET * 8); \
+	__ret |= (u32)(__service_id) << (RPMI_MSG_SERVICE_ID_OFFSET * 8); \
+	__ret |= (u32)(__flags) << (RPMI_MSG_FLAGS_OFFSET * 8); \
+	__ret;					\
+})
+
+#define GET_DLEN(msg)				\
+({						\
+	struct rpmi_message *mbuf = msg;	\
+	le16_to_cpu(mbuf->header.datalen);	\
+})
+
+#define GET_TOKEN(msg)				\
+({						\
+	struct rpmi_message *mbuf = msg;	\
+	le16_to_cpu(mbuf->header.token);	\
+})
+
+#define GET_MESSAGE_TYPE(msg)						\
+({									\
+	uint8_t flags = *((uint8_t *)msg + RPMI_MSG_FLAGS_OFFSET);	\
+	((flags & RPMI_MSG_FLAGS_TYPE) >> RPMI_MSG_FLAGS_TYPE_POS));	\
+})
+
+enum rpmi_queue_type {
+	RPMI_QUEUE_TYPE_REQ = 0,
+	RPMI_QUEUE_TYPE_ACK = 1,
+};
+
+enum rpmi_queue_idx {
+	RPMI_QUEUE_IDX_A2P_REQ = 0,
+	RPMI_QUEUE_IDX_P2A_ACK = 1,
+	RPMI_QUEUE_IDX_P2A_REQ = 2,
+	RPMI_QUEUE_IDX_A2P_ACK = 3,
+	RPMI_QUEUE_IDX_MAX_COUNT,
+};
+
+enum rpmi_reg_idx {
+	RPMI_REG_IDX_DB_REG = 0, /* Doorbell register */
+	RPMI_REG_IDX_MAX_COUNT,
+};
+
+/** Mailbox registers */
+struct rpmi_mb_regs {
+	/* doorbell from AP -> PuC*/
+	volatile le32_t db_reg;
+} __packed;
+
+/** Single Queue Context Structure */
+struct smq_queue_ctx {
+	u32 queue_id;
+	u32 num_slots;
+	spinlock_t queue_lock;
+	/* Type of queue - REQ or ACK */
+	enum rpmi_queue_type queue_type;
+	/* Pointers to the queue shared memory */
+	volatile le32_t *headptr;
+	volatile le32_t *tailptr;
+	volatile uint8_t *buffer;
+	/* Name of the queue */
+	char name[RPMI_NAME_CHARS_MAX];
+};
+
+struct rpmi_shmem_mbox_controller {
+	/* Driver specific members */
+	u32 slot_size;
+	u32 queue_count;
+	struct rpmi_mb_regs *mb_regs;
+	struct smq_queue_ctx queue_ctx_tbl[RPMI_QUEUE_IDX_MAX_COUNT];
+	/* Mailbox framework related members */
+	struct mbox_controller controller;
+	struct mbox_chan channels[RPMI_MAILBOX_CHANNELS_MAX];
+	struct mbox_chan *base_chan;
+	u32 impl_version;
+	u32 impl_id;
+	u32 spec_version;
+	struct {
+		bool f0_ev_notif_en;
+		bool f0_msi_en;
+	} base_flags;
+};
+
+/**************** Shared Memory Queues Helpers **************/
+
+static bool __smq_queue_full(struct smq_queue_ctx *qctx)
+{
+	return ((le32_to_cpu(*qctx->tailptr) + 1) % qctx->num_slots ==
+			le32_to_cpu(*qctx->headptr)) ? true : false;
+}
+
+static bool __smq_queue_empty(struct smq_queue_ctx *qctx)
+{
+	return (le32_to_cpu(*qctx->headptr) ==
+		le32_to_cpu(*qctx->tailptr)) ? true : false;
+}
+
+static int __smq_rx(struct smq_queue_ctx *qctx, u32 slot_size,
+		    u32 service_group_id, struct mbox_xfer *xfer)
+{
+	void *dst, *src;
+	struct rpmi_message *msg;
+	u32 i, tmp, pos, dlen, msgidn, headidx, tailidx;
+	struct rpmi_message_args *args = xfer->args;

lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c line 171

Quoted 10 lines
lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c new file +10 -0
+	bool no_rx_token = (args->flags & RPMI_MSG_FLAGS_NO_RX_TOKEN) ?
+			   true : false;
+
+	/* Rx sanity checks */
+	if ((sizeof(u32) * args->rx_endian_words) >
+	    (slot_size - sizeof(struct rpmi_message_header)))
+		return SBI_EINVAL;
+	if ((sizeof(u32) * args->rx_endian_words) > xfer->rx_len)
+		return SBI_EINVAL;
+

No need to have __smq_queue_empty(qctx)
The empty case is already handled later in if (pos == tailidx)

lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c line 179

Quoted 8 lines
lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c new file +8 -0
+	/* There should be some message in the queue */
+	if (__smq_queue_empty(qctx))
+		return SBI_ENOENT;
+
+	/* Get the head/read index and tail/write index */
+	headidx = le32_to_cpu(*qctx->headptr);
+	tailidx = le32_to_cpu(*qctx->tailptr);
+

There should be a load-acquire style of fence here. Such as

	RISCV_FENCE(r, rw);

Or we can __atomic_load_n(tailptr, __ATOMIC_ACQUIRE) to sync with PuC's update on tailptr. No need to do the same for headptr, as only AP can update headptr, and it's already protected by spin_lock(&qctx->queue_lock).

Reason: I think technically we might rely on address-dependency-ordering and argue that we must have known the value of tailidx before we start fetching "msg" (correction needed, can speculation happen if CPU guesses the correct value of "tailidx"?). But for code readability and pairing with PuC's store-release, I'd still prefer we do a fence/barrier here.

Quoted 39 lines
lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c new file +39 -0
+	/*
+	 * Compute msgidn expected in the incoming message
+	 * NOTE: DOORBELL bit is not expected to be set.
+	 */
+	msgidn = MAKE_MESSAGE_ID(service_group_id, args->service_id, args->type);
+
+	/* Find the Rx message with matching token */
+	pos = headidx;
+	while (pos != tailidx) {
+		src = (void *)qctx->buffer + (pos * slot_size);
+		if ((no_rx_token && GET_MESSAGE_ID(src) == msgidn) ||
+		    (GET_TOKEN(src) == (xfer->seq & RPMI_MSG_TOKEN_MASK)))
+			break;
+		pos = (pos + 1) % qctx->num_slots;
+	}
+	if (pos == tailidx)
+		return SBI_ENOENT;
+
+	/* If Rx message is not first message then make it first message */
+	if (pos != headidx) {
+		src = (void *)qctx->buffer + (pos * slot_size);
+		dst = (void *)qctx->buffer + (headidx * slot_size);
+		for (i = 0; i < slot_size / sizeof(u32); i++) {
+			tmp = ((u32 *)dst)[i];
+			((u32 *)dst)[i] = ((u32 *)src)[i];
+			((u32 *)src)[i] = tmp;
+		}
+	}
+
+	/* Update rx_token if not available */
+	msg = (void *)qctx->buffer + (headidx * slot_size);
+	if (no_rx_token)
+		args->rx_token = GET_TOKEN(msg);
+
+	/* Extract data from the first message */
+	if (xfer->rx) {
+		args->rx_data_len = dlen = GET_DLEN(msg);
+		if (dlen > xfer->rx_len)
+			dlen = xfer->rx_len;

lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c line 228

Quoted 10 lines
lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c new file +10 -0
+		src = (void *)msg + sizeof(struct rpmi_message_header);
+		dst = xfer->rx;
+		for (i = 0; i < args->rx_endian_words; i++)
+			((u32 *)dst)[i] = le32_to_cpu(((u32 *)src)[i]);
+		dst += sizeof(u32) * args->rx_endian_words;
+		src += sizeof(u32) * args->rx_endian_words;
+		sbi_memcpy(dst, src,
+			xfer->rx_len - (sizeof(u32) * args->rx_endian_words));
+	}
+

There should be a store-release style of fence here. Such as

	RISCV_FENCE(rw, w);

Or we can __atomic_store_n(headptr, __ATOMIC_RELEASE) to publish it to PuC. smp_wmb() after the store to headptr is not needed. (Incorrect)

Reason: when PuC observed the free slots by reading head/tailptr, AP must finish touching the previously in-use slot(s). Otherwise, there might be data-corruption

Quoted 15 lines
lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c new file +15 -0
+	/* Update the head/read index */
+	*qctx->headptr = cpu_to_le32(headidx + 1) % qctx->num_slots;
+	smp_wmb();
+
+	return SBI_OK;
+}
+
+static int __smq_tx(struct smq_queue_ctx *qctx, struct rpmi_mb_regs *mb_regs,
+		    u32 slot_size, u32 service_group_id, struct mbox_xfer *xfer)
+{
+	u32 i, tailidx;
+	void *dst, *src;
+	struct rpmi_message_header header = { 0 };
+	struct rpmi_message_args *args = xfer->args;
+

lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c line 253

Quoted 10 lines
lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c new file +10 -0
+	/* Tx sanity checks */
+	if ((sizeof(u32) * args->tx_endian_words) >
+	    (slot_size - sizeof(struct rpmi_message_header)))
+		return SBI_EINVAL;
+	if ((sizeof(u32) * args->tx_endian_words) > xfer->tx_len)
+		return SBI_EINVAL;
+
+	/* There should be some room in the queue */
+	if (__smq_queue_full(qctx))
+		return SBI_ENOMEM;

Probably no need for fence/barrier here due to control-dependency. (reading from tailptr is the dependency of "if(__smq_queue_full(qctx))" which guards the write to msg below). I'm not 100% sure. We might need a second look.

Quoted 16 lines
lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c new file +16 -0
+
+	/* Get the tail/write index */
+	tailidx = le32_to_cpu(*qctx->tailptr);> +
+	/* Prepare the header to be written into the slot */
+	header.servicegroup_id = cpu_to_le16(service_group_id);
+	header.service_id = args->service_id;
+	header.flags = args->type;
+	header.datalen = cpu_to_le16((u16)xfer->tx_len);
+	header.token = cpu_to_le16((u16)xfer->seq);
+
+	/* Write header into the slot */
+	dst = (char *)qctx->buffer + (tailidx * slot_size);
+	sbi_memcpy(dst, &header, sizeof(header));
+	dst += sizeof(header);
+
+	/* Write data into the slot */

lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c line 280

Quoted 10 lines
lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c new file +10 -0
+	if (xfer->tx) {
+		src = xfer->tx;
+		for (i = 0; i < args->tx_endian_words; i++)
+			((u32 *)dst)[i] = cpu_to_le32(((u32 *)src)[i]);
+		dst += sizeof(u32) * args->tx_endian_words;
+		src += sizeof(u32) * args->tx_endian_words;
+		sbi_memcpy(dst, src,
+			xfer->tx_len - (sizeof(u32) * args->tx_endian_words));
+	}
+

Remove smp_wmb() below and use store-relase on tailptr. See the comment on headptr in __smq_rx above.

Quoted 494 lines
lib/utils/mailbox/fdt_mailbox_rpmi_shmem.c new file +391 -0
+	/* Update the tail/write index */
+	*qctx->tailptr = cpu_to_le32(tailidx + 1) % qctx->num_slots;
+	smp_wmb();
+
+	/* Ring the RPMI doorbell if present */
+	if (mb_regs)
+		writel(cpu_to_le32(1), &mb_regs->db_reg);
+
+	return SBI_OK;
+}
+
+static int smq_rx(struct rpmi_shmem_mbox_controller *mctl,
+		  u32 queue_id, u32 service_group_id, struct mbox_xfer *xfer)
+{
+	int ret, rxretry = 0;
+	struct smq_queue_ctx *qctx;
+
+	if (mctl->queue_count < queue_id ||
+	    RPMI_MAILBOX_CHANNELS_MAX <= service_group_id) {
+		sbi_printf("%s: invalid queue_id or service_group_id\n",
+			   __func__);
+		return SBI_EINVAL;
+	}
+	qctx = &mctl->queue_ctx_tbl[queue_id];
+
+	/*
+	 * Once the timeout happens and call this function is returned
+	 * to the client then there is no way to deliver the response
+	 * message after that if it comes later.
+	 *
+	 * REVISIT: In complete timeout duration how much duration
+	 * it should wait(delay) before recv retry. udelay or mdelay
+	 */
+	do {
+		spin_lock(&qctx->queue_lock);
+		ret = __smq_rx(qctx, mctl->slot_size, service_group_id, xfer);
+		spin_unlock(&qctx->queue_lock);
+		if (!ret)
+			return 0;
+
+		sbi_timer_mdelay(1);
+		rxretry += 1;
+	} while (rxretry < xfer->rx_timeout);
+
+	return SBI_ETIMEDOUT;
+}
+
+static int smq_tx(struct rpmi_shmem_mbox_controller *mctl,
+		  u32 queue_id, u32 service_group_id, struct mbox_xfer *xfer)
+{
+	int ret, txretry = 0;
+	struct smq_queue_ctx *qctx;
+
+	if (mctl->queue_count < queue_id ||
+	    RPMI_MAILBOX_CHANNELS_MAX <= service_group_id) {
+		sbi_printf("%s: invalid queue_id or service_group_id\n",
+			   __func__);
+		return SBI_EINVAL;
+	}
+	qctx = &mctl->queue_ctx_tbl[queue_id];
+
+	/*
+	 * Ignoring the tx timeout since in RPMI has no mechanism
+	 * with which other side can let know about the reception of
+	 * message which marks as tx complete. For RPMI tx complete is
+	 * marked as done when message in successfully copied in queue.
+	 *
+	 * REVISIT: In complete timeout duration how much duration
+	 * it should wait(delay) before send retry. udelay or mdelay
+	 */
+	do {
+		spin_lock(&qctx->queue_lock);
+		ret = __smq_tx(qctx, mctl->mb_regs, mctl->slot_size,
+				service_group_id, xfer);
+		spin_unlock(&qctx->queue_lock);
+		if (!ret)
+			return 0;
+
+		sbi_timer_mdelay(1);
+		txretry += 1;
+	} while (txretry < xfer->tx_timeout);
+
+	return SBI_ETIMEDOUT;
+}
+
+static int smq_base_get_two_u32(struct rpmi_shmem_mbox_controller *mctl,
+				u32 service_id, u32 *inarg, u32 *outvals)
+{
+	return rpmi_normal_request_with_status(
+			mctl->base_chan, service_id,
+			inarg, (inarg) ? 1 : 0, (inarg) ? 1 : 0,
+			outvals, 2, 2);
+}
+
+/**************** Mailbox Controller Functions **************/
+
+static int rpmi_shmem_mbox_xfer(struct mbox_chan *chan, struct mbox_xfer *xfer)
+{
+	int ret;
+	u32 tx_qid = 0, rx_qid = 0;
+	struct rpmi_shmem_mbox_controller *mctl =
+			container_of(chan->mbox,
+				     struct rpmi_shmem_mbox_controller,
+				     controller);
+	struct rpmi_message_args *args = xfer->args;
+	bool do_tx = (args->flags & RPMI_MSG_FLAGS_NO_TX) ? false : true;
+	bool do_rx = (args->flags & RPMI_MSG_FLAGS_NO_RX) ? false : true;
+
+	if (!do_tx && !do_rx)
+		return SBI_EINVAL;
+
+	switch (args->type) {
+	case RPMI_MSG_NORMAL_REQUEST:
+		if (do_tx && do_rx) {
+			tx_qid = RPMI_QUEUE_IDX_A2P_REQ;
+			rx_qid = RPMI_QUEUE_IDX_P2A_ACK;
+		} else if (do_tx) {
+			tx_qid = RPMI_QUEUE_IDX_A2P_REQ;
+		} else if (do_rx) {
+			rx_qid = RPMI_QUEUE_IDX_P2A_REQ;
+		}
+		break;
+	case RPMI_MSG_POSTED_REQUEST:
+		if (do_tx && do_rx)
+			return SBI_EINVAL;
+		if (do_tx) {
+			tx_qid = RPMI_QUEUE_IDX_A2P_REQ;
+		} else {
+			rx_qid = RPMI_QUEUE_IDX_P2A_REQ;
+		}
+		break;
+	case RPMI_MSG_ACKNOWLDGEMENT:
+		if (do_tx && do_rx)
+			return SBI_EINVAL;
+		if (do_tx) {
+			tx_qid = RPMI_QUEUE_IDX_A2P_ACK;
+		} else {
+			rx_qid = RPMI_QUEUE_IDX_P2A_ACK;
+		}
+		break;
+	default:
+		return SBI_ENOTSUPP;
+	}
+
+	if (do_tx) {
+		ret = smq_tx(mctl, tx_qid, chan - mctl->channels, xfer);
+		if (ret)
+			return ret;
+	}
+
+	if (do_rx) {
+		ret = smq_rx(mctl, rx_qid, chan - mctl->channels, xfer);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static struct mbox_chan *rpmi_shmem_mbox_request_chan(
+						struct mbox_controller *mbox,
+						u32 *chan_args)
+{
+	int ret;
+	u32 tval[2] = { 0 };
+	struct rpmi_shmem_mbox_controller *mctl =
+			container_of(mbox,
+				     struct rpmi_shmem_mbox_controller,
+				     controller);
+
+	if (chan_args[0] >= RPMI_MAILBOX_CHANNELS_MAX)
+		return NULL;
+
+	/* Base serivce group is always present so probe other groups */
+	if (chan_args[0] != RPMI_SRVGRP_BASE) {
+		/* Probe service group */
+		ret = smq_base_get_two_u32(mctl,
+					   RPMI_BASE_SRV_PROBE_SERVICE_GROUP,
+					   chan_args, tval);
+		if (ret || !tval[1])
+			return NULL;
+	}
+
+	return &mctl->channels[chan_args[0]];
+}
+
+static void *rpmi_shmem_mbox_free_chan(struct mbox_controller *mbox,
+					struct mbox_chan *chan)
+{
+	/* Nothing to do here */
+	return NULL;
+}
+
+extern struct fdt_mailbox fdt_mailbox_rpmi_shmem;
+
+static int rpmi_shmem_transport_init(struct rpmi_shmem_mbox_controller *mctl,
+				     void *fdt, int nodeoff)
+{
+	const char *name;
+	int count, len, ret, qid;
+	uint64_t reg_addr, reg_size;
+	const fdt32_t *prop_slotsz;
+	struct smq_queue_ctx *qctx;
+
+	ret = fdt_node_check_compatible(fdt, nodeoff,
+					"riscv,rpmi-shmem-mbox");
+	if (ret)
+		return ret;
+
+	/* get queue slot size in bytes */
+	prop_slotsz = fdt_getprop(fdt, nodeoff, "riscv,slot-size", &len);
+	if (!prop_slotsz)
+		return SBI_ENOENT;
+
+	mctl->slot_size = fdt32_to_cpu(*prop_slotsz);
+	if (mctl->slot_size < RPMI_SLOT_SIZE_MIN) {
+		sbi_printf("%s: slot_size < mimnum required message size\n",
+			   __func__);
+		mctl->slot_size = RPMI_SLOT_SIZE_MIN;
+	}
+
+	/*
+	 * queue names count is taken as the number of queues
+	 * supported which make it mandatory to provide the
+	 * name of the queue.
+	 */
+	count = fdt_stringlist_count(fdt, nodeoff, "reg-names");
+	if (count < 0 ||
+	    count > (RPMI_QUEUE_IDX_MAX_COUNT + RPMI_REG_IDX_MAX_COUNT))
+		return SBI_EINVAL;
+
+	mctl->queue_count = count - RPMI_REG_IDX_MAX_COUNT;
+
+	/* parse all queues and populate queues context structure */
+	for (qid = 0; qid < mctl->queue_count; qid++) {
+		qctx = &mctl->queue_ctx_tbl[qid];
+
+		/* get each queue share-memory base address and size*/
+		ret = fdt_get_node_addr_size(fdt, nodeoff, qid,
+					     &reg_addr, &reg_size);
+		if (ret < 0 || !reg_addr || !reg_size)
+			return SBI_ENOENT;
+
+		ret = sbi_domain_root_add_memrange(reg_addr, reg_size, reg_size,
+						   (SBI_DOMAIN_MEMREGION_MMIO |
+						    SBI_DOMAIN_MEMREGION_M_READABLE |
+						    SBI_DOMAIN_MEMREGION_M_WRITABLE));
+		if (ret)
+			return ret;
+
+		/* calculate number of slots in each queue */
+		qctx->num_slots =
+			(reg_size - (mctl->slot_size * RPMI_QUEUE_HEADER_SLOTS)) / mctl->slot_size;
+
+		/* setup queue pointers */
+		qctx->headptr = ((void *)(unsigned long)reg_addr) +
+				RPMI_QUEUE_HEAD_SLOT * mctl->slot_size;
+		qctx->tailptr = ((void *)(unsigned long)reg_addr) +
+				RPMI_QUEUE_TAIL_SLOT * mctl->slot_size;
+		qctx->buffer = ((void *)(unsigned long)reg_addr) +
+				RPMI_QUEUE_HEADER_SLOTS * mctl->slot_size;
+
+		/* get the queue name */
+		name = fdt_stringlist_get(fdt, nodeoff, "reg-names",
+					  qid, &len);
+		if (!name || (name && len < 0))
+			return len;
+
+		sbi_memcpy(qctx->name, name, len);
+
+		/* store the index as queue_id */
+		qctx->queue_id = qid;
+
+		SPIN_LOCK_INIT(qctx->queue_lock);
+	}
+
+	/* get the db-reg property name */
+	name = fdt_stringlist_get(fdt, nodeoff, "reg-names", qid, &len);
+	if (!name || (name && len < 0))
+		return len;
+
+	/* fetch doorbell register address*/
+	ret = fdt_get_node_addr_size(fdt, nodeoff, qid, &reg_addr,
+				       &reg_size);
+	if (!ret && !(strncmp(name, "db-reg", strlen("db-reg")))) {
+		mctl->mb_regs = (void *)(unsigned long)reg_addr;
+		ret = sbi_domain_root_add_memrange(reg_addr, reg_size, reg_size,
+						   (SBI_DOMAIN_MEMREGION_MMIO |
+						    SBI_DOMAIN_MEMREGION_M_READABLE |
+						    SBI_DOMAIN_MEMREGION_M_WRITABLE));
+		if (ret)
+			return ret;
+	}
+
+	return SBI_SUCCESS;
+}
+
+static int rpmi_shmem_mbox_init(void *fdt, int nodeoff, u32 phandle,
+				const struct fdt_match *match)
+{
+	int ret = 0;
+	u32 tval[2];
+	struct rpmi_base_get_attributes_resp resp;
+	struct rpmi_shmem_mbox_controller *mctl;
+
+	mctl = sbi_zalloc(sizeof(*mctl));
+	if (!mctl)
+		return SBI_ENOMEM;
+
+	/* Initialization transport from device tree */
+	ret = rpmi_shmem_transport_init(mctl, fdt, nodeoff);
+	if (ret)
+		goto fail_free_controller;
+
+	/* Register mailbox controller */
+	mctl->controller.id = phandle;
+	mctl->controller.max_xfer_len =
+			mctl->slot_size - sizeof(struct rpmi_message_header);
+	mctl->controller.driver = &fdt_mailbox_rpmi_shmem;
+	mctl->controller.request_chan = rpmi_shmem_mbox_request_chan;
+	mctl->controller.free_chan = rpmi_shmem_mbox_free_chan;
+	mctl->controller.xfer = rpmi_shmem_mbox_xfer;
+	ret = mbox_controller_add(&mctl->controller);
+	if (ret)
+		goto fail_free_controller;
+
+	/* Request base service group channel */
+	tval[0] = RPMI_SRVGRP_BASE;
+	mctl->base_chan = mbox_controller_request_chan(&mctl->controller,
+							tval);
+	if (!mctl->base_chan) {
+		ret = SBI_ENOENT;
+		goto fail_remove_controller;
+	}
+
+	/* Get implementation id */
+	ret = smq_base_get_two_u32(mctl,
+				   RPMI_BASE_SRV_GET_IMPLEMENTATION_VERSION,
+				   NULL, tval);
+	if (ret)
+		goto fail_free_chan;
+	mctl->impl_version = tval[1];
+
+	/* Get implementation version */
+	ret = smq_base_get_two_u32(mctl, RPMI_BASE_SRV_GET_IMPLEMENTATION_IDN,
+				   NULL, tval);
+	if (ret)
+		goto fail_free_chan;
+	mctl->impl_id = tval[1];
+
+	/* Get specification version */
+	ret = smq_base_get_two_u32(mctl, RPMI_BASE_SRV_GET_SPEC_VERSION,
+				   NULL, tval);
+	if (ret)
+		goto fail_free_chan;
+	mctl->spec_version = tval[1];
+
+	/* Get optional features implementation flags */
+	ret = rpmi_normal_request_with_status(
+			mctl->base_chan, RPMI_BASE_SRV_GET_ATTRIBUTES,
+			NULL, 0, 0,
+			&resp, rpmi_u32_count(resp), rpmi_u32_count(resp));
+	if (ret)
+		goto fail_free_chan;
+
+	mctl->base_flags.f0_ev_notif_en =
+			resp.f0 & RPMI_BASE_FLAGS_F0_EV_NOTIFY ? 1 : 0;
+	mctl->base_flags.f0_msi_en =
+			resp.f0 & RPMI_BASE_FLAGS_F0_MSI_EN ? 1 : 0;
+
+	return 0;
+
+fail_free_chan:
+	mbox_controller_free_chan(mctl->base_chan);
+fail_remove_controller:
+	mbox_controller_remove(&mctl->controller);
+fail_free_controller:
+	sbi_free(mctl);
+	return ret;
+}
+
+static const struct fdt_match rpmi_shmem_mbox_match[] = {
+	{ .compatible = "riscv,rpmi-shmem-mbox" },
+	{ },
+};
+
+struct fdt_mailbox fdt_mailbox_rpmi_shmem = {
+	.match_table = rpmi_shmem_mbox_match,
+	.init = rpmi_shmem_mbox_init,
+	.xlate = fdt_mailbox_simple_xlate,
+};
lib/utils/mailbox/objects.mk +5 -0
index 2135898c..746b0313 100644
@@ -11,3 +11,8 @@ libsbiutils-objs-$(CONFIG_FDT_MAILBOX) += mailbox/fdt_mailbox.o
  libsbiutils-objs-$(CONFIG_FDT_MAILBOX) += mailbox/fdt_mailbox_drivers.carray.o
 
  libsbiutils-objs-$(CONFIG_MAILBOX) += mailbox/mailbox.o
+
+libsbiutils-objs-$(CONFIG_RPMI_MAILBOX) += mailbox/rpmi_mailbox.o
+
+carray-fdt_mailbox_drivers-$(CONFIG_FDT_MAILBOX_RPMI_SHMEM) += fdt_mailbox_rpmi_shmem
+libsbiutils-objs-$(CONFIG_FDT_MAILBOX_RPMI_SHMEM) += mailbox/fdt_mailbox_rpmi_shmem.o
lib/utils/mailbox/rpmi_mailbox.c new file +79 -0
new file mode 100644
index 00000000..58c64e56
@@ -0,0 +1,79 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#include <sbi/sbi_error.h>
+#include <sbi_utils/mailbox/mailbox.h>
+#include <sbi_utils/mailbox/rpmi_mailbox.h>
+
+int rpmi_xlate_error(enum rpmi_error error)
+{
+	switch (error) {
+	case RPMI_SUCCESS:
+		return SBI_OK;
+	case RPMI_ERR_FAILED:
+		return SBI_EFAIL;
+	case RPMI_ERR_NOTSUPP:
+		return SBI_ENOTSUPP;
+	case RPMI_ERR_INVAL:
+		return SBI_EINVAL;
+	case RPMI_ERR_DENIED:
+		return SBI_EDENIED;
+	case RPMI_ERR_NOTFOUND:
+		return SBI_ENOENT;
+	case RPMI_ERR_OUTOFRANGE:
+		return SBI_EINVAL;
+	case RPMI_ERR_OUTOFRES:
+		return SBI_ENOSPC;
+	case RPMI_ERR_HWFAULT:
+		return SBI_EIO;
+	default:
+		return SBI_EUNKNOWN;
+	}
+}
+
+int rpmi_normal_request_with_status(
+			struct mbox_chan *chan, u32 service_id,
+			void *req, u32 req_words, u32 req_endian_words,
+			void *resp, u32 resp_words, u32 resp_endian_words)
+{
+	int ret;
+	struct mbox_xfer xfer;
+	struct rpmi_message_args args = { 0 };
+
+	args.type = RPMI_MSG_NORMAL_REQUEST;
+	args.service_id = service_id;
+	args.tx_endian_words = req_endian_words;
+	args.rx_endian_words = resp_endian_words;
+	mbox_xfer_init_txrx(&xfer, &args,
+			req, sizeof(u32) * req_words, RPMI_DEF_TX_TIMEOUT,
+			resp, sizeof(u32) * resp_words, RPMI_DEF_RX_TIMEOUT);
+
+	ret = mbox_chan_xfer(chan, &xfer);
+	if (ret)
+		return ret;
+
+	return rpmi_xlate_error(((u32 *)resp)[0]);
+}
+
+int rpmi_posted_request(
+		struct mbox_chan *chan, u32 service_id,
+		void *req, u32 req_words, u32 req_endian_words)
+{
+	struct mbox_xfer xfer;
+	struct rpmi_message_args args = { 0 };
+
+	args.type = RPMI_MSG_POSTED_REQUEST;
+	args.flags = RPMI_MSG_FLAGS_NO_RX;
+	args.service_id = service_id;
+	args.tx_endian_words = req_endian_words;
+	mbox_xfer_init_tx(&xfer, &args,
+			  req, sizeof(u32) * req_words, RPMI_DEF_TX_TIMEOUT);
+
+	return mbox_chan_xfer(chan, &xfer);
+}
platform/generic/configs/defconfig +0 -0
index 079bc4fe..233a9a89 100644

platform/generic/configs/defconfig CONFIG_FDT_IRQCHIP=y · line 28

Quoted 9 lines
platform/generic/configs/defconfig +3 -0
@@ -20,6 +20,9 @@ CONFIG_FDT_IRQCHIP=y
  CONFIG_FDT_IRQCHIP_APLIC=y
  CONFIG_FDT_IRQCHIP_IMSIC=y
  CONFIG_FDT_IRQCHIP_PLIC=y
+CONFIG_FDT_MAILBOX=y
+CONFIG_RPMI_MAILBOX=y
+CONFIG_FDT_MAILBOX_RPMI_SHMEM=y
  CONFIG_FDT_REGMAP=y
  CONFIG_FDT_REGMAP_SYSCON=y
  CONFIG_FDT_RESET=y

Thanks!
Bo

#17 lore raw

PATCH 13/16 lib: sbi: Add SBI Message Proxy (MPXY) framework

In reply to Anup Patel

Hi Anup,

Quoted 13 lines · Anup Patel

From: Rahul Pathak <[email protected]>

Introduce SBI Message Proxy (MPXY) framework which allows platform specific code or drivers to register message protocol specific channels.

This framework enables the supervisor software to send messages belonging to different message protocols via OpenSBI firmware.

<...>

lib/sbi/sbi_mpxy.c line 70

Quoted 6 lines
lib/sbi/sbi_mpxy.c new file +6 -0
+/** Disable hart shared memory */
+static inline void sbi_mpxy_shmem_disable(struct mpxy_state *rs)
+{
+	rs->shmem.shmem_size = 0;
+	rs->shmem.shmem_addr_lo = INVALID_ADDR;
+	rs->shmem.shmem_addr_hi = INVALID_ADDR;

nitpicking: if the abbreviation "rs" representing the RPXY state, would it be renamed as "ms"?

Best regards,
Peter Lin

#18 lore raw

PATCH 13/16 lib: sbi: Add SBI Message Proxy (MPXY) framework

Quoted 18 lines · On Fri, Oct 11, 2024 at 4:57?PM Yu-Chien Peter Lin <[email protected]>…

Hi Anup,

Quoted 13 lines · Anup Patel

From: Rahul Pathak <[email protected]>

Introduce SBI Message Proxy (MPXY) framework which allows platform specific code or drivers to register message protocol specific channels.

This framework enables the supervisor software to send messages belonging to different message protocols via OpenSBI firmware.

<...>

Quoted 8 lines

lib/sbi/sbi_mpxy.c line 70

Quoted 6 lines
lib/sbi/sbi_mpxy.c new file +6 -0
+/** Disable hart shared memory */
+static inline void sbi_mpxy_shmem_disable(struct mpxy_state *rs)
+{
+     rs->shmem.shmem_size = 0;
+     rs->shmem.shmem_addr_lo = INVALID_ADDR;
+     rs->shmem.shmem_addr_hi = INVALID_ADDR;

nitpicking: if the abbreviation "rs" representing the RPXY state, would it be renamed as "ms"?

It's the remnant of that only. And noted. If another version of the series happens, I will change it.

Quoted 3 lines

Best regards,
Peter Lin

Thanks
Rahul Pathak