Skip to contentSkip to conversation

Support for I/O width within ARM SCMI SHMEM

PATCH Superseded Florian Fainelli 3 participants 8 messages Aug 16, 2024 – Aug 22, 2024

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Support for I/O width within ARM SCMI SHMEM

We just got our hands on hardware that only supports 32-bit access width to the SRAM being used. This patch series adds support for the 'reg-io-width' property and allows us to specify the exact access width that the SRAM supports.

Changes in v3:

- added missing documentation for the structure members being added
- removed the use of a macro for the 32-bit only operation, this gets rid of a number of checkpatch warnings
- added missing trailing barriers
- corrected binding indentation

Changes in v2:

- fixed typo in the binding and added reviewed-by tag from Krzysztof

- determine the correct I/O operation at the time we parse the 'reg-io-width' property rather than for each tx_prepare/fetch_response/fetch_notification call

- dropped support for 1 and 2 bytes 'reg-io-width' as they do not quite make sense, if we can support such smaller access size, then we can support the larger 4 byte access width, too, and there are many places within the SCMI code where ioread32/iowrite32 are used

Florian Fainelli (2):
dt-bindings: sram: Document reg-io-width property
firmware: arm_scmi: Support 'reg-io-width' property for shared memory

 .../devicetree/bindings/sram/sram.yaml        |  6 ++
 drivers/firmware/arm_scmi/common.h            | 32 ++++++-
 .../arm_scmi/scmi_transport_mailbox.c         | 13 ++-
 .../firmware/arm_scmi/scmi_transport_optee.c  | 11 ++-
 .../firmware/arm_scmi/scmi_transport_smc.c    | 11 ++-
 drivers/firmware/arm_scmi/shmem.c             | 86 +++++++++++++++++--
 6 files changed, 138 insertions(+), 21 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/2 dt-bindings: sram: Document reg-io-width property

Some SRAMs need to be accessed with a specific access width, define the 'reg-io-width' property specifying such access sizes.

---
 Documentation/devicetree/bindings/sram/sram.yaml | 6 ++++++
 1 file changed, 6 insertions(+)
Documentation/devicetree/bindings/sram/sram.yaml +6 -0
index 0922d1f71ba8..7c1337e159f2 100644
@@ -101,6 +101,12 @@ patternProperties:
           IO mem address range, relative to the SRAM range.
         maxItems: 1
 
+      reg-io-width:
+        description:
+          The size (in bytes) of the IO accesses that should be performed on the
+          SRAM.
+        enum: [1, 2, 4, 8]
+
       pool:
         description:
           Indicates that the particular reserved SRAM area is addressable
Signature · 1 line
2.34.1
#2 lore raw

PATCH v3 2/2 firmware: arm_scmi: Support 'reg-io-width' property for shared memory

In reply to Florian Fainelli

Some shared memory areas might only support a certain access width, such as 32-bit, which memcpy_{from,to}_io() does not adhere to at least on ARM64 by making both 8-bit and 64-bit accesses to such memory.

Update the shmem layer to support reading from and writing to such shared memory area using the specified I/O width in the Device Tree. The various transport layers making use of the shmem.c code are updated accordingly to pass the I/O accessors that they store.

---
 drivers/firmware/arm_scmi/common.h            | 32 ++++++-
 .../arm_scmi/scmi_transport_mailbox.c         | 13 ++-
 .../firmware/arm_scmi/scmi_transport_optee.c  | 11 ++-
 .../firmware/arm_scmi/scmi_transport_smc.c    | 11 ++-
 drivers/firmware/arm_scmi/shmem.c             | 86 +++++++++++++++++--
 5 files changed, 132 insertions(+), 21 deletions(-)
drivers/firmware/arm_scmi/common.h +28 -4
index 69928bbd01c2..73bb496fac01 100644
@@ -316,6 +316,26 @@ enum scmi_bad_msg {
 	MSG_MBOX_SPURIOUS = -5,
 };
 
+/* Used for compactness and signature validation of the function pointers being
+ * passed.
+ */
+typedef void (*shmem_copy_toio_t)(volatile void __iomem *to, const void *from,
+				  size_t count);
+typedef void (*shmem_copy_fromio_t)(void *to, const volatile void __iomem *from,
+				    size_t count);
+
+/**
+ * struct scmi_shmem_io_ops  - I/O operations to read from/write to
+ * Shared Memory
+ *
+ * @toio: Copy data to the shared memory area
+ * @fromio: Copy data from the shared memory area
+ */
+struct scmi_shmem_io_ops {
+	shmem_copy_fromio_t fromio;
+	shmem_copy_toio_t toio;
+};
+
 /* shmem related declarations */
 struct scmi_shared_mem;
 
@@ -336,13 +356,16 @@ struct scmi_shared_mem;
 struct scmi_shared_mem_operations {
 	void (*tx_prepare)(struct scmi_shared_mem __iomem *shmem,
 			   struct scmi_xfer *xfer,
-			   struct scmi_chan_info *cinfo);
+			   struct scmi_chan_info *cinfo,
+			   shmem_copy_toio_t toio);
 	u32 (*read_header)(struct scmi_shared_mem __iomem *shmem);
 
 	void (*fetch_response)(struct scmi_shared_mem __iomem *shmem,
-			       struct scmi_xfer *xfer);
+			       struct scmi_xfer *xfer,
+			       shmem_copy_fromio_t fromio);
 	void (*fetch_notification)(struct scmi_shared_mem __iomem *shmem,
-				   size_t max_len, struct scmi_xfer *xfer);
+				   size_t max_len, struct scmi_xfer *xfer,
+				   shmem_copy_fromio_t fromio);
 	void (*clear_channel)(struct scmi_shared_mem __iomem *shmem);
 	bool (*poll_done)(struct scmi_shared_mem __iomem *shmem,
 			  struct scmi_xfer *xfer);
@@ -350,7 +373,8 @@ struct scmi_shared_mem_operations {
 	bool (*channel_intr_enabled)(struct scmi_shared_mem __iomem *shmem);
 	void __iomem *(*setup_iomap)(struct scmi_chan_info *cinfo,
 				     struct device *dev,
-				     bool tx, struct resource *res);
+				     bool tx, struct resource *res,
+				     struct scmi_shmem_io_ops **ops);
 };
 
 const struct scmi_shared_mem_operations *scmi_shared_mem_operations_get(void);
drivers/firmware/arm_scmi/scmi_transport_mailbox.c +9 -4
index dc5ca894d5eb..1a2e90e5c765 100644
@@ -25,6 +25,7 @@
  * @chan_platform_receiver: Optional Platform Receiver mailbox unidirectional channel
  * @cinfo: SCMI channel info
  * @shmem: Transmit/Receive shared memory area
+ * @io_ops: Transport specific I/O operations
  */
 struct scmi_mailbox {
 	struct mbox_client cl;
@@ -33,6 +34,7 @@ struct scmi_mailbox {
 	struct mbox_chan *chan_platform_receiver;
 	struct scmi_chan_info *cinfo;
 	struct scmi_shared_mem __iomem *shmem;
+	struct scmi_shmem_io_ops *io_ops;
 };
 
 #define client_to_scmi_mailbox(c) container_of(c, struct scmi_mailbox, cl)
@@ -43,7 +45,8 @@ static void tx_prepare(struct mbox_client *cl, void *m)
 {
 	struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl);
 
-	core->shmem->tx_prepare(smbox->shmem, m, smbox->cinfo);
+	core->shmem->tx_prepare(smbox->shmem, m, smbox->cinfo,
+				smbox->io_ops->toio);
 }
 
 static void rx_callback(struct mbox_client *cl, void *m)
@@ -197,7 +200,8 @@ static int mailbox_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
 	if (!smbox)
 		return -ENOMEM;
 
-	smbox->shmem = core->shmem->setup_iomap(cinfo, dev, tx, NULL);
+	smbox->shmem = core->shmem->setup_iomap(cinfo, dev, tx, NULL,
+						&smbox->io_ops);
 	if (IS_ERR(smbox->shmem))
 		return PTR_ERR(smbox->shmem);
 
@@ -298,7 +302,7 @@ static void mailbox_fetch_response(struct scmi_chan_info *cinfo,
 {
 	struct scmi_mailbox *smbox = cinfo->transport_info;
 
-	core->shmem->fetch_response(smbox->shmem, xfer);
+	core->shmem->fetch_response(smbox->shmem, xfer, smbox->io_ops->fromio);
 }
 
 static void mailbox_fetch_notification(struct scmi_chan_info *cinfo,
@@ -306,7 +310,8 @@ static void mailbox_fetch_notification(struct scmi_chan_info *cinfo,
 {
 	struct scmi_mailbox *smbox = cinfo->transport_info;
 
-	core->shmem->fetch_notification(smbox->shmem, max_len, xfer);
+	core->shmem->fetch_notification(smbox->shmem, max_len, xfer,
+					smbox->io_ops->fromio);
 }
 
 static void mailbox_clear_channel(struct scmi_chan_info *cinfo)
drivers/firmware/arm_scmi/scmi_transport_optee.c +8 -3
index 08911f40d1ff..2be4124c6826 100644
@@ -114,6 +114,7 @@ enum scmi_optee_pta_cmd {
  * @req.shmem: Virtual base address of the shared memory
  * @req.msg: Shared memory protocol handle for SCMI request and
  *   synchronous response
+ * @io_ops: Transport specific I/O operations
  * @tee_shm: TEE shared memory handle @req or NULL if using IOMEM shmem
  * @link: Reference in agent's channel list
  */
@@ -128,6 +129,7 @@ struct scmi_optee_channel {
 		struct scmi_shared_mem __iomem *shmem;
 		struct scmi_msg_payld *msg;
 	} req;
+	struct scmi_shmem_io_ops *io_ops;
 	struct tee_shm *tee_shm;
 	struct list_head link;
 };
@@ -350,7 +352,8 @@ static int setup_dynamic_shmem(struct device *dev, struct scmi_optee_channel *ch
 static int setup_static_shmem(struct device *dev, struct scmi_chan_info *cinfo,
 			      struct scmi_optee_channel *channel)
 {
-	channel->req.shmem = core->shmem->setup_iomap(cinfo, dev, true, NULL);
+	channel->req.shmem = core->shmem->setup_iomap(cinfo, dev, true, NULL,
+						      &channel->io_ops);
 	if (IS_ERR(channel->req.shmem))
 		return PTR_ERR(channel->req.shmem);
 
@@ -465,7 +468,8 @@ static int scmi_optee_send_message(struct scmi_chan_info *cinfo,
 		ret = invoke_process_msg_channel(channel,
 						 core->msg->command_size(xfer));
 	} else {
-		core->shmem->tx_prepare(channel->req.shmem, xfer, cinfo);
+		core->shmem->tx_prepare(channel->req.shmem, xfer, cinfo,
+					channel->io_ops->toio);
 		ret = invoke_process_smt_channel(channel);
 	}
 
@@ -484,7 +488,8 @@ static void scmi_optee_fetch_response(struct scmi_chan_info *cinfo,
 		core->msg->fetch_response(channel->req.msg,
 					  channel->rx_len, xfer);
 	else
-		core->shmem->fetch_response(channel->req.shmem, xfer);
+		core->shmem->fetch_response(channel->req.shmem, xfer,
+					    channel->io_ops->fromio);
 }
 
 static void scmi_optee_mark_txdone(struct scmi_chan_info *cinfo, int ret,
drivers/firmware/arm_scmi/scmi_transport_smc.c +8 -3
index c6c69a17a9cc..04e715ec1570 100644
@@ -45,6 +45,7 @@
  * @irq: An optional IRQ for completion
  * @cinfo: SCMI channel info
  * @shmem: Transmit/Receive shared memory area
+ * @io_ops: Transport specific I/O operations
  * @shmem_lock: Lock to protect access to Tx/Rx shared memory area.
  *		Used when NOT operating in atomic mode.
  * @inflight: Atomic flag to protect access to Tx/Rx shared memory area.
@@ -60,6 +61,7 @@ struct scmi_smc {
 	int irq;
 	struct scmi_chan_info *cinfo;
 	struct scmi_shared_mem __iomem *shmem;
+	struct scmi_shmem_io_ops *io_ops;
 	/* Protect access to shmem area */
 	struct mutex shmem_lock;
 #define INFLIGHT_NONE	MSG_TOKEN_MAX
@@ -144,7 +146,8 @@ static int smc_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
 	if (!scmi_info)
 		return -ENOMEM;
 
-	scmi_info->shmem = core->shmem->setup_iomap(cinfo, dev, tx, &res);
+	scmi_info->shmem = core->shmem->setup_iomap(cinfo, dev, tx, &res,
+						    &scmi_info->io_ops);
 	if (IS_ERR(scmi_info->shmem))
 		return PTR_ERR(scmi_info->shmem);
 
@@ -229,7 +232,8 @@ static int smc_send_message(struct scmi_chan_info *cinfo,
 	 */
 	smc_channel_lock_acquire(scmi_info, xfer);
 
-	core->shmem->tx_prepare(scmi_info->shmem, xfer, cinfo);
+	core->shmem->tx_prepare(scmi_info->shmem, xfer, cinfo,
+				scmi_info->io_ops->toio);
 
 	if (scmi_info->cap_id != ULONG_MAX)
 		arm_smccc_1_1_invoke(scmi_info->func_id, scmi_info->cap_id, 0,
@@ -253,7 +257,8 @@ static void smc_fetch_response(struct scmi_chan_info *cinfo,
 {
 	struct scmi_smc *scmi_info = cinfo->transport_info;
 
-	core->shmem->fetch_response(scmi_info->shmem, xfer);
+	core->shmem->fetch_response(scmi_info->shmem, xfer,
+				    scmi_info->io_ops->fromio);
 }
 
 static void smc_mark_txdone(struct scmi_chan_info *cinfo, int ret,
drivers/firmware/arm_scmi/shmem.c +79 -7

139 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

RE: [PATCH v3 2/2] firmware: arm_scmi: Support 'reg-io-width' property for shared memory

Quoted 291 lines

Subject: [PATCH v3 2/2] firmware: arm_scmi: Support 'reg-io-width' property for shared memory

Some shared memory areas might only support a certain access width, such as 32-bit, which memcpy_{from,to}_io() does not adhere to at least on ARM64 by making both 8-bit and 64-bit accesses to such memory.

Update the shmem layer to support reading from and writing to such shared memory area using the specified I/O width in the Device Tree. The various transport layers making use of the shmem.c code are updated accordingly to pass the I/O accessors that they store.

---
 drivers/firmware/arm_scmi/common.h            | 32 ++++++-
 .../arm_scmi/scmi_transport_mailbox.c         | 13 ++-
 .../firmware/arm_scmi/scmi_transport_optee.c  | 11 ++-
 .../firmware/arm_scmi/scmi_transport_smc.c    | 11 ++-
 drivers/firmware/arm_scmi/shmem.c             | 86 +++++++++++++++++-
(quoted diff) +0 -1
-
 5 files changed, 132 insertions(+), 21 deletions(-)
drivers/firmware/arm_scmi/common.h +20 -0
index 69928bbd01c2..73bb496fac01 100644
@@ -316,6 +316,26 @@ enum scmi_bad_msg {
 	MSG_MBOX_SPURIOUS = -5,
 };
 
+/* Used for compactness and signature validation of the function
+pointers being
+ * passed.
+ */
+typedef void (*shmem_copy_toio_t)(volatile void __iomem *to, const
 void *from,
+				  size_t count);
+typedef void (*shmem_copy_fromio_t)(void *to, const volatile void
 __iomem *from,
+				    size_t count);
+
+/**
+ * struct scmi_shmem_io_ops  - I/O operations to read from/write to
+ * Shared Memory
+ *
+ * @toio: Copy data to the shared memory area
+ * @fromio: Copy data from the shared memory area  */ struct
+scmi_shmem_io_ops {
+	shmem_copy_fromio_t fromio;
+	shmem_copy_toio_t toio;
+};
+
 /* shmem related declarations */

struct scmi_shared_mem;

drivers/firmware/arm_scmi/common.h +4 -3
@@ -336,13 +356,16 @@ struct scmi_shared_mem;  struct
 scmi_shared_mem_operations {
 	void (*tx_prepare)(struct scmi_shared_mem __iomem
 *shmem,
 			   struct scmi_xfer *xfer,
-			   struct scmi_chan_info *cinfo);
+			   struct scmi_chan_info *cinfo,
+			   shmem_copy_toio_t toio);
 	u32 (*read_header)(struct scmi_shared_mem __iomem
 *shmem);
 
 	void (*fetch_response)(struct scmi_shared_mem __iomem
 *shmem,
-			       struct scmi_xfer *xfer);
+			       struct scmi_xfer *xfer,
+			       shmem_copy_fromio_t fromio);
 	void (*fetch_notification)(struct scmi_shared_mem __iomem
 *shmem,
-				   size_t max_len, struct scmi_xfer

*xfer);

drivers/firmware/arm_scmi/common.h +1 -0
+				   size_t max_len, struct scmi_xfer

*xfer,

drivers/firmware/arm_scmi/common.h +1 -0
+				   shmem_copy_fromio_t fromio);
 	void (*clear_channel)(struct scmi_shared_mem __iomem

*shmem);

drivers/firmware/arm_scmi/common.h +2 -1
 	bool (*poll_done)(struct scmi_shared_mem __iomem *shmem,
 			  struct scmi_xfer *xfer);
@@ -350,7 +373,8 @@ struct scmi_shared_mem_operations {
 	bool (*channel_intr_enabled)(struct scmi_shared_mem
 __iomem *shmem);
 	void __iomem *(*setup_iomap)(struct scmi_chan_info *cinfo,
 				     struct device *dev,
-				     bool tx, struct resource *res);
+				     bool tx, struct resource *res,
+				     struct scmi_shmem_io_ops **ops);
 };
 

const struct scmi_shared_mem_operations
*scmi_shared_mem_operations_get(void);

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +0 -0
index dc5ca894d5eb..1a2e90e5c765 100644
@@ -25,6 +25,7 @@
  * @chan_platform_receiver: Optional Platform Receiver mailbox

unidirectional channel
* @cinfo: SCMI channel info
* @shmem: Transmit/Receive shared memory area
+ * @io_ops: Transport specific I/O operations
*/
struct scmi_mailbox {

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +1 -0
 	struct mbox_client cl;
@@ -33,6 +34,7 @@ struct scmi_mailbox {
 	struct mbox_chan *chan_platform_receiver;
 	struct scmi_chan_info *cinfo;
 	struct scmi_shared_mem __iomem *shmem;
+	struct scmi_shmem_io_ops *io_ops;
 };
 
 #define client_to_scmi_mailbox(c) container_of(c, struct scmi_mailbox,

cl) @@ -43,7 +45,8 @@ static void tx_prepare(struct mbox_client *cl, void *m) {

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +3 -1
 	struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl);
 
-	core->shmem->tx_prepare(smbox->shmem, m, smbox->cinfo);
+	core->shmem->tx_prepare(smbox->shmem, m, smbox->cinfo,
+				smbox->io_ops->toio);
 }
 
 static void rx_callback(struct mbox_client *cl, void *m) @@ -197,7
+200,8 @@ static int mailbox_chan_setup(struct scmi_chan_info

*cinfo, struct device *dev,

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +0 -1
 	if (!smbox)
 		return -ENOMEM;
 
-	smbox->shmem = core->shmem->setup_iomap(cinfo, dev, tx,

NULL);

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +1 -0
+	smbox->shmem = core->shmem->setup_iomap(cinfo, dev, tx,

NULL,

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +5 -2
+						&smbox->io_ops);
 	if (IS_ERR(smbox->shmem))
 		return PTR_ERR(smbox->shmem);
@@ -298,7 +302,7 @@ static void mailbox_fetch_response(struct
 scmi_chan_info *cinfo,  {
 	struct scmi_mailbox *smbox = cinfo->transport_info;
 
-	core->shmem->fetch_response(smbox->shmem, xfer);
+	core->shmem->fetch_response(smbox->shmem, xfer,
+smbox->io_ops->fromio);
 }
 
 static void mailbox_fetch_notification(struct scmi_chan_info *cinfo,
@@ -306,7 +310,8 @@ static void mailbox_fetch_notification(struct
 scmi_chan_info *cinfo,  {
 	struct scmi_mailbox *smbox = cinfo->transport_info;
 
-	core->shmem->fetch_notification(smbox->shmem, max_len,
 xfer);
+	core->shmem->fetch_notification(smbox->shmem, max_len,
 xfer,
+					smbox->io_ops->fromio);
 }

static void mailbox_clear_channel(struct scmi_chan_info *cinfo) diff --
git a/drivers/firmware/arm_scmi/scmi_transport_optee.c b/drivers/firmware/arm_scmi/scmi_transport_optee.c
index 08911f40d1ff..2be4124c6826 100644

drivers/firmware/arm_scmi/scmi_transport_optee.c +1 -0
@@ -114,6 +114,7 @@ enum scmi_optee_pta_cmd {
  * @req.shmem: Virtual base address of the shared memory
  * @req.msg: Shared memory protocol handle for SCMI request and
  *   synchronous response
+ * @io_ops: Transport specific I/O operations
  * @tee_shm: TEE shared memory handle @req or NULL if using

IOMEM shmem
* @link: Reference in agent's channel list
*/

drivers/firmware/arm_scmi/scmi_transport_optee.c +3 -1
@@ -128,6 +129,7 @@ struct scmi_optee_channel {
 		struct scmi_shared_mem __iomem *shmem;
 		struct scmi_msg_payld *msg;
 	} req;
+	struct scmi_shmem_io_ops *io_ops;
 	struct tee_shm *tee_shm;
 	struct list_head link;
 };
@@ -350,7 +352,8 @@ static int setup_dynamic_shmem(struct device
 *dev, struct scmi_optee_channel *ch  static int
 setup_static_shmem(struct device *dev, struct scmi_chan_info *cinfo,
 			      struct scmi_optee_channel *channel)  {
-	channel->req.shmem = core->shmem->setup_iomap(cinfo, dev,
 true, NULL);
+	channel->req.shmem = core->shmem->setup_iomap(cinfo, dev,
 true, NULL,
+						      &channel-
 >io_ops);
drivers/firmware/arm_scmi/scmi_transport_optee.c +1 -1
 	if (IS_ERR(channel->req.shmem))
 		return PTR_ERR(channel->req.shmem);
@@ -465,7 +468,8 @@ static int scmi_optee_send_message(struct
 scmi_chan_info *cinfo,
 		ret = invoke_process_msg_channel(channel,
 						 core->msg-
 >command_size(xfer));
 	} else {
-		core->shmem->tx_prepare(channel->req.shmem, xfer,
 cinfo);
+		core->shmem->tx_prepare(channel->req.shmem, xfer,

cinfo,

drivers/firmware/arm_scmi/scmi_transport_optee.c +3 -1
+					channel->io_ops->toio);
 		ret = invoke_process_smt_channel(channel);
 	}
@@ -484,7 +488,8 @@ static void scmi_optee_fetch_response(struct
 scmi_chan_info *cinfo,
 		core->msg->fetch_response(channel->req.msg,
 					  channel->rx_len, xfer);
 	else
-		core->shmem->fetch_response(channel->req.shmem,
 xfer);
+		core->shmem->fetch_response(channel->req.shmem,
 xfer,
+					    channel->io_ops->fromio);

}

static void scmi_optee_mark_txdone(struct scmi_chan_info *cinfo, int
ret, diff --git a/drivers/firmware/arm_scmi/scmi_transport_smc.c b/drivers/firmware/arm_scmi/scmi_transport_smc.c
index c6c69a17a9cc..04e715ec1570 100644

drivers/firmware/arm_scmi/scmi_transport_smc.c +1 -0
@@ -45,6 +45,7 @@
  * @irq: An optional IRQ for completion
  * @cinfo: SCMI channel info
  * @shmem: Transmit/Receive shared memory area
+ * @io_ops: Transport specific I/O operations
  * @shmem_lock: Lock to protect access to Tx/Rx shared memory area.
  *		Used when NOT operating in atomic mode.
  * @inflight: Atomic flag to protect access to Tx/Rx shared memory

area.

drivers/firmware/arm_scmi/scmi_transport_smc.c +3 -1
@@ -60,6 +61,7 @@ struct scmi_smc {
 	int irq;
 	struct scmi_chan_info *cinfo;
 	struct scmi_shared_mem __iomem *shmem;
+	struct scmi_shmem_io_ops *io_ops;
 	/* Protect access to shmem area */
 	struct mutex shmem_lock;
 #define INFLIGHT_NONE	MSG_TOKEN_MAX
@@ -144,7 +146,8 @@ static int smc_chan_setup(struct
 scmi_chan_info *cinfo, struct device *dev,
 	if (!scmi_info)
 		return -ENOMEM;
 
-	scmi_info->shmem = core->shmem->setup_iomap(cinfo, dev,
 tx, &res);
+	scmi_info->shmem = core->shmem->setup_iomap(cinfo, dev,
 tx, &res,
+						    &scmi_info-
Quoted 1 line

io_ops);

drivers/firmware/arm_scmi/scmi_transport_smc.c +2 -1
 	if (IS_ERR(scmi_info->shmem))
 		return PTR_ERR(scmi_info->shmem);
@@ -229,7 +232,8 @@ static int smc_send_message(struct
 scmi_chan_info *cinfo,
 	 */
 	smc_channel_lock_acquire(scmi_info, xfer);
 
-	core->shmem->tx_prepare(scmi_info->shmem, xfer, cinfo);
+	core->shmem->tx_prepare(scmi_info->shmem, xfer, cinfo,
+				scmi_info->io_ops->toio);
 
 	if (scmi_info->cap_id != ULONG_MAX)

arm_smccc_1_1_invoke(scmi_info->func_id,
scmi_info->cap_id, 0, @@ -253,7 +257,8 @@ static void smc_fetch_response(struct scmi_chan_info *cinfo, {

drivers/firmware/arm_scmi/scmi_transport_smc.c +2 -2
 	struct scmi_smc *scmi_info = cinfo->transport_info;
 
-	core->shmem->fetch_response(scmi_info->shmem, xfer);
+	core->shmem->fetch_response(scmi_info->shmem, xfer,
+				    scmi_info->io_ops->fromio);
 }
 
 static void smc_mark_txdone(struct scmi_chan_info *cinfo, int ret, diff
--git a/drivers/firmware/arm_scmi/shmem.c

b/drivers/firmware/arm_scmi/shmem.c
index 01d8a9398fe8..aded5f1cd49f 100644

drivers/firmware/arm_scmi/shmem.c +0 -0
@@ -34,9 +34,67 @@ struct scmi_shared_mem {
 	u8 msg_payload[];
 };
 

drivers/firmware/arm_scmi/shmem.c struct scmi_shared_mem { · line 46

Quoted 10 lines
drivers/firmware/arm_scmi/shmem.c +9 -0
+static inline void shmem_memcpy_fromio32(void *to,
+					 const volatile void __iomem
 *from,
+					 size_t count)
+{
+	while (count) {
+		*(u32 *)to = __raw_readl(from);
+		from += 4;
+		to += 4;
+		count -= 4;

This may not have issue, considering the 'count % 4' will be 0 and 'from' is 4 bytes aligned.

But I think it maybe better to add check if 'count' and 'from' are not 4 bytes aligned.

drivers/firmware/arm_scmi/shmem.c struct scmi_shared_mem { · line 49

Quoted 4 lines
drivers/firmware/arm_scmi/shmem.c +4 -0
+	}
+
+	/* Ensure all reads from I/O have completed */
+	rmb();

Is there a need to use rmb here? I am not sure, just wonder.

drivers/firmware/arm_scmi/shmem.c struct scmi_shared_mem { · line 61

Quoted 12 lines
drivers/firmware/arm_scmi/shmem.c +12 -0
+}
+
+static inline void shmem_memcpy_toio32(volatile void __iomem *to,
+				       const void *from,
+				       size_t count)
+{
+	while (count) {
+		__raw_writel(*(u32 *)from, to);
+		from += 4;
+		to += 4;
+		count -= 4;
+	}

Ditto.

drivers/firmware/arm_scmi/shmem.c struct scmi_shared_mem { · line 64

Quoted 3 lines
drivers/firmware/arm_scmi/shmem.c +3 -0
+
+	/* Ensure all writes to I/O have completed */
+	wmb();

This maybe not needed.
The mailbox will use "writel", the SMC will use "smc", the virtio will have "hvc", both will have barrier I think.

Regards,
Peng.

Quoted 142 lines
drivers/firmware/arm_scmi/shmem.c +8 -0
+}
+
+static struct scmi_shmem_io_ops shmem_io_ops32 = {
+	.fromio	= shmem_memcpy_fromio32,
+	.toio	= shmem_memcpy_toio32,
+};
+
+/* Wrappers are needed for proper memcpy_{from,to}_io expansion

by the
+ * pre-processor.
+ */

drivers/firmware/arm_scmi/shmem.c +2 -0
+static inline void shmem_memcpy_fromio(void *to,
+				       const volatile void __iomem

*from,

drivers/firmware/arm_scmi/shmem.c +17 -0
+				       size_t count)
+{
+	memcpy_fromio(to, from, count);
+}
+
+static inline void shmem_memcpy_toio(volatile void __iomem *to,
+				     const void *from,
+				     size_t count)
+{
+	memcpy_toio(to, from, count);
+}
+
+static struct scmi_shmem_io_ops shmem_io_ops_default = {
+	.fromio = shmem_memcpy_fromio,
+	.toio	= shmem_memcpy_toio,
+};
+
 static void shmem_tx_prepare(struct scmi_shared_mem __iomem

*shmem,

drivers/firmware/arm_scmi/shmem.c +2 -2
 			     struct scmi_xfer *xfer,
-			     struct scmi_chan_info *cinfo)
+			     struct scmi_chan_info *cinfo,
+			     shmem_copy_toio_t copy_toio)
 {
 	ktime_t stop;
@@ -73,7 +131,7 @@ static void shmem_tx_prepare(struct
 scmi_shared_mem __iomem *shmem,
 	iowrite32(sizeof(shmem->msg_header) + xfer->tx.len,
 &shmem->length);
 	iowrite32(pack_scmi_header(&xfer->hdr), &shmem-
 >msg_header);
 	if (xfer->tx.buf)
-		memcpy_toio(shmem->msg_payload, xfer->tx.buf,

xfer->tx.len);

drivers/firmware/arm_scmi/shmem.c +1 -0
+		copy_toio(shmem->msg_payload, xfer->tx.buf, xfer-
Quoted 1 line

tx.len);

}

static u32 shmem_read_header(struct scmi_shared_mem __iomem
*shmem) @@ -82,7 +140,8 @@ static u32 shmem_read_header(struct scmi_shared_mem __iomem *shmem) }

static void shmem_fetch_response(struct scmi_shared_mem __iomem
*shmem,

drivers/firmware/arm_scmi/shmem.c +3 -3
-				 struct scmi_xfer *xfer)
+				 struct scmi_xfer *xfer,
+				 shmem_copy_fromio_t copy_fromio)
 {
 	size_t len = ioread32(&shmem->length);
@@ -91,11 +150,12 @@ static void shmem_fetch_response(struct
 scmi_shared_mem __iomem *shmem,
 	xfer->rx.len = min_t(size_t, xfer->rx.len, len > 8 ? len - 8 : 0);
 
 	/* Take a copy to the rx buffer.. */
-	memcpy_fromio(xfer->rx.buf, shmem->msg_payload + 4, xfer-
 >rx.len);
+	copy_fromio(xfer->rx.buf, shmem->msg_payload + 4, xfer-
 >rx.len);
 }
 
 static void shmem_fetch_notification(struct scmi_shared_mem
 __iomem *shmem,
-				     size_t max_len, struct scmi_xfer

*xfer)

drivers/firmware/arm_scmi/shmem.c +1 -0
+				     size_t max_len, struct scmi_xfer

*xfer,

drivers/firmware/arm_scmi/shmem.c +1 -0
+				     shmem_copy_fromio_t

copy_fromio)
{

drivers/firmware/arm_scmi/shmem.c +1 -1
 	size_t len = ioread32(&shmem->length);
@@ -103,7 +163,7 @@ static void shmem_fetch_notification(struct
 scmi_shared_mem __iomem *shmem,
 	xfer->rx.len = min_t(size_t, max_len, len > 4 ? len - 4 : 0);
 
 	/* Take a copy to the rx buffer.. */
-	memcpy_fromio(xfer->rx.buf, shmem->msg_payload, xfer-
 >rx.len);
+	copy_fromio(xfer->rx.buf, shmem->msg_payload, xfer->rx.len);
 }

static void shmem_clear_channel(struct scmi_shared_mem __iomem
*shmem) @@ -139,7 +199,8 @@ static bool shmem_channel_intr_enabled(struct scmi_shared_mem __iomem *shmem)

static void __iomem *shmem_setup_iomap(struct scmi_chan_info
*cinfo,

drivers/firmware/arm_scmi/shmem.c +2 -1
 				       struct device *dev, bool tx,
-				       struct resource *res)
+				       struct resource *res,
+				       struct scmi_shmem_io_ops

**ops)
{

drivers/firmware/arm_scmi/shmem.c +1 -0
 	struct device_node *shmem __free(device_node);
 	const char *desc = tx ? "Tx" : "Rx";
@@ -148,6 +209,7 @@ static void __iomem
 *shmem_setup_iomap(struct scmi_chan_info *cinfo,
 	struct resource lres = {};
 	resource_size_t size;
 	void __iomem *addr;
+	u32 reg_io_width;
 
 	shmem = of_parse_phandle(cdev->of_node, "shmem", idx);
drivers/firmware/arm_scmi/shmem.c +10 -1
 	if (!shmem)
@@ -173,6 +235,16 @@ static void __iomem
 *shmem_setup_iomap(struct scmi_chan_info *cinfo,
 		return IOMEM_ERR_PTR(-EADDRNOTAVAIL);
 	}
 
+	of_property_read_u32(shmem, "reg-io-width", &reg_io_width);
+	switch (reg_io_width) {
+	case 4:
+		*ops = &shmem_io_ops32;
+		break;
+	default:
+		*ops = &shmem_io_ops_default;
+		break;
+	}
+
 	return addr;
 }
 
--

2.34.1

#4 lore raw

Re: [PATCH v3 2/2] firmware: arm_scmi: Support 'reg-io-width' property for shared memory

Quoted 291 lines · Peng Fan
Quoted 291 lines

Subject: [PATCH v3 2/2] firmware: arm_scmi: Support 'reg-io-width' property for shared memory

Some shared memory areas might only support a certain access width, such as 32-bit, which memcpy_{from,to}_io() does not adhere to at least on ARM64 by making both 8-bit and 64-bit accesses to such memory.

Update the shmem layer to support reading from and writing to such shared memory area using the specified I/O width in the Device Tree. The various transport layers making use of the shmem.c code are updated accordingly to pass the I/O accessors that they store.

---
  drivers/firmware/arm_scmi/common.h            | 32 ++++++-
  .../arm_scmi/scmi_transport_mailbox.c         | 13 ++-
  .../firmware/arm_scmi/scmi_transport_optee.c  | 11 ++-
  .../firmware/arm_scmi/scmi_transport_smc.c    | 11 ++-
  drivers/firmware/arm_scmi/shmem.c             | 86 +++++++++++++++++-
(quoted diff) +0 -1
-
  5 files changed, 132 insertions(+), 21 deletions(-)
drivers/firmware/arm_scmi/common.h +20 -0
index 69928bbd01c2..73bb496fac01 100644
@@ -316,6 +316,26 @@ enum scmi_bad_msg {
  	MSG_MBOX_SPURIOUS = -5,
  };
 
+/* Used for compactness and signature validation of the function
+pointers being
+ * passed.
+ */
+typedef void (*shmem_copy_toio_t)(volatile void __iomem *to, const
 void *from,
+				  size_t count);
+typedef void (*shmem_copy_fromio_t)(void *to, const volatile void
 __iomem *from,
+				    size_t count);
+
+/**
+ * struct scmi_shmem_io_ops  - I/O operations to read from/write to
+ * Shared Memory
+ *
+ * @toio: Copy data to the shared memory area
+ * @fromio: Copy data from the shared memory area  */ struct
+scmi_shmem_io_ops {
+	shmem_copy_fromio_t fromio;
+	shmem_copy_toio_t toio;
+};
+
  /* shmem related declarations */

struct scmi_shared_mem;

drivers/firmware/arm_scmi/common.h +4 -3
@@ -336,13 +356,16 @@ struct scmi_shared_mem;  struct
 scmi_shared_mem_operations {
  	void (*tx_prepare)(struct scmi_shared_mem __iomem
 *shmem,
  			   struct scmi_xfer *xfer,
-			   struct scmi_chan_info *cinfo);
+			   struct scmi_chan_info *cinfo,
+			   shmem_copy_toio_t toio);
  	u32 (*read_header)(struct scmi_shared_mem __iomem
 *shmem);
 
  	void (*fetch_response)(struct scmi_shared_mem __iomem
 *shmem,
-			       struct scmi_xfer *xfer);
+			       struct scmi_xfer *xfer,
+			       shmem_copy_fromio_t fromio);
  	void (*fetch_notification)(struct scmi_shared_mem __iomem
 *shmem,
-				   size_t max_len, struct scmi_xfer

*xfer);

drivers/firmware/arm_scmi/common.h +1 -0
+				   size_t max_len, struct scmi_xfer

*xfer,

drivers/firmware/arm_scmi/common.h +1 -0
+				   shmem_copy_fromio_t fromio);
  	void (*clear_channel)(struct scmi_shared_mem __iomem

*shmem);
bool (*poll_done)(struct scmi_shared_mem __iomem *shmem,
struct scmi_xfer *xfer);

drivers/firmware/arm_scmi/common.h +2 -1
@@ -350,7 +373,8 @@ struct scmi_shared_mem_operations {
  	bool (*channel_intr_enabled)(struct scmi_shared_mem
 __iomem *shmem);
  	void __iomem *(*setup_iomap)(struct scmi_chan_info *cinfo,
  				     struct device *dev,
-				     bool tx, struct resource *res);
+				     bool tx, struct resource *res,
+				     struct scmi_shmem_io_ops **ops);
  };
 

const struct scmi_shared_mem_operations
*scmi_shared_mem_operations_get(void);

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +0 -0
index dc5ca894d5eb..1a2e90e5c765 100644
@@ -25,6 +25,7 @@
   * @chan_platform_receiver: Optional Platform Receiver mailbox

unidirectional channel
* @cinfo: SCMI channel info
* @shmem: Transmit/Receive shared memory area
+ * @io_ops: Transport specific I/O operations
*/
struct scmi_mailbox {
struct mbox_client cl;

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +1 -0
@@ -33,6 +34,7 @@ struct scmi_mailbox {
  	struct mbox_chan *chan_platform_receiver;
  	struct scmi_chan_info *cinfo;
  	struct scmi_shared_mem __iomem *shmem;
+	struct scmi_shmem_io_ops *io_ops;
  };
 
  #define client_to_scmi_mailbox(c) container_of(c, struct scmi_mailbox,

cl) @@ -43,7 +45,8 @@ static void tx_prepare(struct mbox_client *cl, void *m) {
struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl);

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +3 -1
-	core->shmem->tx_prepare(smbox->shmem, m, smbox->cinfo);
+	core->shmem->tx_prepare(smbox->shmem, m, smbox->cinfo,
+				smbox->io_ops->toio);
  }
 
  static void rx_callback(struct mbox_client *cl, void *m) @@ -197,7
+200,8 @@ static int mailbox_chan_setup(struct scmi_chan_info

*cinfo, struct device *dev,
if (!smbox)
return -ENOMEM;

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +0 -1
-	smbox->shmem = core->shmem->setup_iomap(cinfo, dev, tx,

NULL);

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +1 -0
+	smbox->shmem = core->shmem->setup_iomap(cinfo, dev, tx,

NULL,

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +5 -2
+						&smbox->io_ops);
  	if (IS_ERR(smbox->shmem))
  		return PTR_ERR(smbox->shmem);
@@ -298,7 +302,7 @@ static void mailbox_fetch_response(struct
 scmi_chan_info *cinfo,  {
  	struct scmi_mailbox *smbox = cinfo->transport_info;
 
-	core->shmem->fetch_response(smbox->shmem, xfer);
+	core->shmem->fetch_response(smbox->shmem, xfer,
+smbox->io_ops->fromio);
  }
 
  static void mailbox_fetch_notification(struct scmi_chan_info *cinfo,
@@ -306,7 +310,8 @@ static void mailbox_fetch_notification(struct
 scmi_chan_info *cinfo,  {
  	struct scmi_mailbox *smbox = cinfo->transport_info;
 
-	core->shmem->fetch_notification(smbox->shmem, max_len,
 xfer);
+	core->shmem->fetch_notification(smbox->shmem, max_len,
 xfer,
+					smbox->io_ops->fromio);
  }

static void mailbox_clear_channel(struct scmi_chan_info *cinfo) diff --
git a/drivers/firmware/arm_scmi/scmi_transport_optee.c b/drivers/firmware/arm_scmi/scmi_transport_optee.c
index 08911f40d1ff..2be4124c6826 100644

drivers/firmware/arm_scmi/scmi_transport_optee.c +1 -0
@@ -114,6 +114,7 @@ enum scmi_optee_pta_cmd {
   * @req.shmem: Virtual base address of the shared memory
   * @req.msg: Shared memory protocol handle for SCMI request and
   *   synchronous response
+ * @io_ops: Transport specific I/O operations
   * @tee_shm: TEE shared memory handle @req or NULL if using

IOMEM shmem
* @link: Reference in agent's channel list
*/

drivers/firmware/arm_scmi/scmi_transport_optee.c +3 -1
@@ -128,6 +129,7 @@ struct scmi_optee_channel {
  		struct scmi_shared_mem __iomem *shmem;
  		struct scmi_msg_payld *msg;
  	} req;
+	struct scmi_shmem_io_ops *io_ops;
  	struct tee_shm *tee_shm;
  	struct list_head link;
  };
@@ -350,7 +352,8 @@ static int setup_dynamic_shmem(struct device
 *dev, struct scmi_optee_channel *ch  static int
 setup_static_shmem(struct device *dev, struct scmi_chan_info *cinfo,
  			      struct scmi_optee_channel *channel)  {
-	channel->req.shmem = core->shmem->setup_iomap(cinfo, dev,
 true, NULL);
+	channel->req.shmem = core->shmem->setup_iomap(cinfo, dev,
 true, NULL,
+						      &channel-
 > io_ops);
  	if (IS_ERR(channel->req.shmem))
  		return PTR_ERR(channel->req.shmem);
drivers/firmware/arm_scmi/scmi_transport_optee.c +1 -1
@@ -465,7 +468,8 @@ static int scmi_optee_send_message(struct
 scmi_chan_info *cinfo,
  		ret = invoke_process_msg_channel(channel,
  						 core->msg-
 > command_size(xfer));
  	} else {
-		core->shmem->tx_prepare(channel->req.shmem, xfer,
 cinfo);
+		core->shmem->tx_prepare(channel->req.shmem, xfer,

cinfo,

drivers/firmware/arm_scmi/scmi_transport_optee.c +3 -1
+					channel->io_ops->toio);
  		ret = invoke_process_smt_channel(channel);
  	}
@@ -484,7 +488,8 @@ static void scmi_optee_fetch_response(struct
 scmi_chan_info *cinfo,
  		core->msg->fetch_response(channel->req.msg,
  					  channel->rx_len, xfer);
  	else
-		core->shmem->fetch_response(channel->req.shmem,
 xfer);
+		core->shmem->fetch_response(channel->req.shmem,
 xfer,
+					    channel->io_ops->fromio);
  }

static void scmi_optee_mark_txdone(struct scmi_chan_info *cinfo, int
ret, diff --git a/drivers/firmware/arm_scmi/scmi_transport_smc.c b/drivers/firmware/arm_scmi/scmi_transport_smc.c
index c6c69a17a9cc..04e715ec1570 100644

drivers/firmware/arm_scmi/scmi_transport_smc.c +1 -0
@@ -45,6 +45,7 @@
   * @irq: An optional IRQ for completion
   * @cinfo: SCMI channel info
   * @shmem: Transmit/Receive shared memory area
+ * @io_ops: Transport specific I/O operations
   * @shmem_lock: Lock to protect access to Tx/Rx shared memory area.
   *		Used when NOT operating in atomic mode.
   * @inflight: Atomic flag to protect access to Tx/Rx shared memory

area.

drivers/firmware/arm_scmi/scmi_transport_smc.c +3 -1
@@ -60,6 +61,7 @@ struct scmi_smc {
  	int irq;
  	struct scmi_chan_info *cinfo;
  	struct scmi_shared_mem __iomem *shmem;
+	struct scmi_shmem_io_ops *io_ops;
  	/* Protect access to shmem area */
  	struct mutex shmem_lock;
  #define INFLIGHT_NONE	MSG_TOKEN_MAX
@@ -144,7 +146,8 @@ static int smc_chan_setup(struct
 scmi_chan_info *cinfo, struct device *dev,
  	if (!scmi_info)
  		return -ENOMEM;
 
-	scmi_info->shmem = core->shmem->setup_iomap(cinfo, dev,
 tx, &res);
+	scmi_info->shmem = core->shmem->setup_iomap(cinfo, dev,
 tx, &res,
+						    &scmi_info-
Quoted 1 line

io_ops);

if (IS_ERR(scmi_info->shmem))
return PTR_ERR(scmi_info->shmem);

drivers/firmware/arm_scmi/scmi_transport_smc.c +2 -1
@@ -229,7 +232,8 @@ static int smc_send_message(struct
 scmi_chan_info *cinfo,
  	 */
  	smc_channel_lock_acquire(scmi_info, xfer);
 
-	core->shmem->tx_prepare(scmi_info->shmem, xfer, cinfo);
+	core->shmem->tx_prepare(scmi_info->shmem, xfer, cinfo,
+				scmi_info->io_ops->toio);
 
  	if (scmi_info->cap_id != ULONG_MAX)

arm_smccc_1_1_invoke(scmi_info->func_id,
scmi_info->cap_id, 0, @@ -253,7 +257,8 @@ static void smc_fetch_response(struct scmi_chan_info *cinfo, {
struct scmi_smc *scmi_info = cinfo->transport_info;

drivers/firmware/arm_scmi/scmi_transport_smc.c +2 -2
-	core->shmem->fetch_response(scmi_info->shmem, xfer);
+	core->shmem->fetch_response(scmi_info->shmem, xfer,
+				    scmi_info->io_ops->fromio);
  }
 
  static void smc_mark_txdone(struct scmi_chan_info *cinfo, int ret, diff
--git a/drivers/firmware/arm_scmi/shmem.c

b/drivers/firmware/arm_scmi/shmem.c
index 01d8a9398fe8..aded5f1cd49f 100644

drivers/firmware/arm_scmi/shmem.c +0 -0
@@ -34,9 +34,67 @@ struct scmi_shared_mem {
  	u8 msg_payload[];
  };
 
Quoted 13 lines

drivers/firmware/arm_scmi/shmem.c struct scmi_shared_mem { · line 45

Quoted 10 lines
drivers/firmware/arm_scmi/shmem.c +9 -0
+static inline void shmem_memcpy_fromio32(void *to,
+					 const volatile void __iomem
 *from,
+					 size_t count)
+{
+	while (count) {
+		*(u32 *)to = __raw_readl(from);
+		from += 4;
+		to += 4;
+		count -= 4;

This may not have issue, considering the 'count % 4' will be 0 and 'from' is 4 bytes aligned.

Correct, this cannot possibly happen today by virtue of msg->payload being naturally aligned on a 4 bytes boundary.

Quoted 3 lines

But I think it maybe better to add check if 'count' and 'from' are not 4 bytes aligned.

Humm, what would be the fallback, or should we just WARN()?

Quoted 7 lines

drivers/firmware/arm_scmi/shmem.c struct scmi_shared_mem { · line 49

Quoted 5 lines
drivers/firmware/arm_scmi/shmem.c +4 -0
+	}
+
+	/* Ensure all reads from I/O have completed */
+	rmb();

Is there a need to use rmb here? I am not sure, just wonder.

I personally do not think there is because there is an implicit data dependency eventually we will be consuming data that was copied into msg->payload and that will ensure that the data is there.

Quoted 16 lines

drivers/firmware/arm_scmi/shmem.c struct scmi_shared_mem { · line 61

Quoted 13 lines
drivers/firmware/arm_scmi/shmem.c +12 -0
+}
+
+static inline void shmem_memcpy_toio32(volatile void __iomem *to,
+				       const void *from,
+				       size_t count)
+{
+	while (count) {
+		__raw_writel(*(u32 *)from, to);
+		from += 4;
+		to += 4;
+		count -= 4;
+	}

Ditto.

Quoted 7 lines

drivers/firmware/arm_scmi/shmem.c struct scmi_shared_mem { · line 64

Quoted 3 lines
drivers/firmware/arm_scmi/shmem.c +3 -0
+
+	/* Ensure all writes to I/O have completed */
+	wmb();

This maybe not needed.
The mailbox will use "writel", the SMC will use "smc", the virtio will have "hvc", both will have barrier I think.

Yes, those are all implicit barriers, I was attempting to address Christian's concerns raised in v2:

https://lore.kernel.org/all/Zr-GJts3Gu6GEkhC@pluto/

Signature · 1 line
Florian
#5 lore raw

RE: [PATCH v3 2/2] firmware: arm_scmi: Support 'reg-io-width' property for shared memory

Quoted 325 lines

Subject: Re: [PATCH v3 2/2] firmware: arm_scmi: Support 'reg-io- width' property for shared memory

Quoted 4 lines · Peng Fan
Quoted 4 lines

Subject: [PATCH v3 2/2] firmware: arm_scmi: Support 'reg-io-width' property for shared memory

Some shared memory areas might only support a certain access

width,

Quoted 5 lines
Quoted 5 lines

such as 32-bit, which memcpy_{from,to}_io() does not adhere to at least on ARM64 by making both 8-bit and 64-bit accesses to such memory.

Update the shmem layer to support reading from and writing to

such

Quoted 11 lines
Quoted 11 lines

shared memory area using the specified I/O width in the Device Tree. The various transport layers making use of the shmem.c code are updated accordingly to pass the I/O accessors that they store.

---
  drivers/firmware/arm_scmi/common.h            | 32 ++++++-
  .../arm_scmi/scmi_transport_mailbox.c         | 13 ++-
  .../firmware/arm_scmi/scmi_transport_optee.c  | 11 ++-
  .../firmware/arm_scmi/scmi_transport_smc.c    | 11 ++-
  drivers/firmware/arm_scmi/shmem.c             | 86

+++++++++++++++++-

Quoted 12 lines
Quoted 12 lines
(quoted diff) +0 -1
-
  5 files changed, 132 insertions(+), 21 deletions(-)
drivers/firmware/arm_scmi/common.h +0 -0
index 69928bbd01c2..73bb496fac01 100644
@@ -316,6 +316,26 @@ enum scmi_bad_msg {
  	MSG_MBOX_SPURIOUS = -5,
  };
 
Quoted 5 lines

drivers/firmware/arm_scmi/common.h enum scmi_bad_msg { · line 323

Quoted 5 lines
drivers/firmware/arm_scmi/common.h +5 -0
+/* Used for compactness and signature validation of the function
+pointers being
+ * passed.
+ */
+typedef void (*shmem_copy_toio_t)(volatile void __iomem *to,

const

Quoted 8 lines

drivers/firmware/arm_scmi/common.h enum scmi_bad_msg {

Quoted 8 lines
drivers/firmware/arm_scmi/common.h +6 -0
 void *from,
+				  size_t count);
+typedef void (*shmem_copy_fromio_t)(void *to, const volatile void
 __iomem *from,
+				    size_t count);
+
+/**
+ * struct scmi_shmem_io_ops  - I/O operations to read from/write

to

Quoted 12 lines
Quoted 12 lines
drivers/firmware/arm_scmi/common.h +9 -0
+ * Shared Memory
+ *
+ * @toio: Copy data to the shared memory area
+ * @fromio: Copy data from the shared memory area  */ struct
+scmi_shmem_io_ops {
+	shmem_copy_fromio_t fromio;
+	shmem_copy_toio_t toio;
+};
+
  /* shmem related declarations */

struct scmi_shared_mem;

Quoted 3 lines

drivers/firmware/arm_scmi/common.h struct scmi_shared_mem; struct · line 357

Quoted 3 lines
drivers/firmware/arm_scmi/common.h +0 -0
@@ -336,13 +356,16 @@ struct scmi_shared_mem;  struct
 scmi_shared_mem_operations {
  	void (*tx_prepare)(struct scmi_shared_mem __iomem

*shmem,

Quoted 5 lines

drivers/firmware/arm_scmi/common.h struct scmi_shared_mem;

Quoted 5 lines
drivers/firmware/arm_scmi/common.h +2 -1
  			   struct scmi_xfer *xfer,
-			   struct scmi_chan_info *cinfo);
+			   struct scmi_chan_info *cinfo,
+			   shmem_copy_toio_t toio);
  	u32 (*read_header)(struct scmi_shared_mem __iomem

*shmem);

Quoted 2 lines

drivers/firmware/arm_scmi/common.h struct scmi_shared_mem; struct · line 362

Quoted 2 lines
drivers/firmware/arm_scmi/common.h +0 -0
  	void (*fetch_response)(struct scmi_shared_mem __iomem

*shmem,

Quoted 4 lines

drivers/firmware/arm_scmi/common.h struct scmi_shared_mem;

Quoted 4 lines
drivers/firmware/arm_scmi/common.h +2 -1
-			       struct scmi_xfer *xfer);
+			       struct scmi_xfer *xfer,
+			       shmem_copy_fromio_t fromio);
  	void (*fetch_notification)(struct scmi_shared_mem __iomem

*shmem,

Quoted 6 lines

drivers/firmware/arm_scmi/common.h struct scmi_shared_mem; struct · line 370

Quoted 6 lines
drivers/firmware/arm_scmi/common.h +2 -1
-				   size_t max_len, struct scmi_xfer
 *xfer);
+				   size_t max_len, struct scmi_xfer
 *xfer,
+				   shmem_copy_fromio_t fromio);
  	void (*clear_channel)(struct scmi_shared_mem __iomem

*shmem);

Quoted 4 lines

drivers/firmware/arm_scmi/common.h struct scmi_shared_mem_operations { · line 373

Quoted 4 lines
drivers/firmware/arm_scmi/common.h +0 -0
  	bool (*poll_done)(struct scmi_shared_mem __iomem *shmem,

struct scmi_xfer *xfer);

drivers/firmware/arm_scmi/common.h +0 -0
@@ -350,7 +373,8 @@ struct scmi_shared_mem_operations {
  	bool (*channel_intr_enabled)(struct scmi_shared_mem

__iomem

Quoted 31 lines
Quoted 31 lines
drivers/firmware/arm_scmi/common.h +2 -1
 *shmem);
  	void __iomem *(*setup_iomap)(struct scmi_chan_info *cinfo,
  				     struct device *dev,
-				     bool tx, struct resource *res);
+				     bool tx, struct resource *res,
+				     struct scmi_shmem_io_ops **ops);
  };
 

const struct scmi_shared_mem_operations
*scmi_shared_mem_operations_get(void);

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +0 -0
index dc5ca894d5eb..1a2e90e5c765 100644
@@ -25,6 +25,7 @@
   * @chan_platform_receiver: Optional Platform Receiver mailbox

unidirectional channel
* @cinfo: SCMI channel info
* @shmem: Transmit/Receive shared memory area
+ * @io_ops: Transport specific I/O operations
*/
struct scmi_mailbox {
struct mbox_client cl;

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +1 -0
@@ -33,6 +34,7 @@ struct scmi_mailbox {
  	struct mbox_chan *chan_platform_receiver;
  	struct scmi_chan_info *cinfo;
  	struct scmi_shared_mem __iomem *shmem;
+	struct scmi_shmem_io_ops *io_ops;
  };
 
Quoted 3 lines
Quoted 3 lines
drivers/firmware/arm_scmi/scmi_transport_mailbox.c +0 -0
  #define client_to_scmi_mailbox(c) container_of(c, struct

scmi_mailbox,
cl) @@ -43,7 +45,8 @@ static void tx_prepare(struct mbox_client

*cl,

Quoted 43 lines
Quoted 43 lines

void *m) {
struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl);

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +3 -1
-	core->shmem->tx_prepare(smbox->shmem, m, smbox->cinfo);
+	core->shmem->tx_prepare(smbox->shmem, m, smbox->cinfo,
+				smbox->io_ops->toio);
  }
 
  static void rx_callback(struct mbox_client *cl, void *m) @@ -197,7
+200,8 @@ static int mailbox_chan_setup(struct scmi_chan_info

*cinfo, struct device *dev,
if (!smbox)
return -ENOMEM;

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +0 -1
-	smbox->shmem = core->shmem->setup_iomap(cinfo, dev, tx,

NULL);

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +1 -0
+	smbox->shmem = core->shmem->setup_iomap(cinfo, dev, tx,

NULL,

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +3 -1
+						&smbox->io_ops);
  	if (IS_ERR(smbox->shmem))
  		return PTR_ERR(smbox->shmem);
@@ -298,7 +302,7 @@ static void mailbox_fetch_response(struct
 scmi_chan_info *cinfo,  {
  	struct scmi_mailbox *smbox = cinfo->transport_info;
 
-	core->shmem->fetch_response(smbox->shmem, xfer);
+	core->shmem->fetch_response(smbox->shmem, xfer,
+smbox->io_ops->fromio);
  }
 
  static void mailbox_fetch_notification(struct scmi_chan_info

*cinfo, @@ -306,7 +310,8 @@ static void mailbox_fetch_notification(struct scmi_chan_info *cinfo, {
struct scmi_mailbox *smbox = cinfo->transport_info;

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +0 -1
-	core->shmem->fetch_notification(smbox->shmem, max_len,

xfer);

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +1 -0
+	core->shmem->fetch_notification(smbox->shmem, max_len,

xfer,

drivers/firmware/arm_scmi/scmi_transport_mailbox.c +1 -0
+					smbox->io_ops->fromio);
  }
Quoted 9 lines

drivers/firmware/arm_scmi/scmi_transport_optee.c enum scmi_optee_pta_cmd { · line 115

Quoted 9 lines

static void mailbox_clear_channel(struct scmi_chan_info *cinfo)
diff -- git a/drivers/firmware/arm_scmi/scmi_transport_optee.c b/drivers/firmware/arm_scmi/scmi_transport_optee.c index 08911f40d1ff..2be4124c6826 100644

drivers/firmware/arm_scmi/scmi_transport_optee.c +0 -0
@@ -114,6 +114,7 @@ enum scmi_optee_pta_cmd {
   * @req.shmem: Virtual base address of the shared memory
   * @req.msg: Shared memory protocol handle for SCMI request

and

Quoted 3 lines

drivers/firmware/arm_scmi/scmi_transport_optee.c enum scmi_optee_pta_cmd {

Quoted 3 lines
drivers/firmware/arm_scmi/scmi_transport_optee.c +1 -0
   *   synchronous response
+ * @io_ops: Transport specific I/O operations
   * @tee_shm: TEE shared memory handle @req or NULL if using

IOMEM

Quoted 4 lines
Quoted 4 lines

shmem
* @link: Reference in agent's channel list
*/

drivers/firmware/arm_scmi/scmi_transport_optee.c +0 -0
@@ -128,6 +129,7 @@ struct scmi_optee_channel {
Quoted 8 lines

drivers/firmware/arm_scmi/scmi_transport_optee.c static int setup_dynamic_shmem(struct

Quoted 8 lines
drivers/firmware/arm_scmi/scmi_transport_optee.c +1 -0
  		struct scmi_shared_mem __iomem *shmem;
  		struct scmi_msg_payld *msg;
  	} req;
+	struct scmi_shmem_io_ops *io_ops;
  	struct tee_shm *tee_shm;
  	struct list_head link;
  };
@@ -350,7 +352,8 @@ static int setup_dynamic_shmem(struct

device

Quoted 2 lines
Quoted 2 lines
drivers/firmware/arm_scmi/scmi_transport_optee.c +0 -0
 *dev, struct scmi_optee_channel *ch  static int

setup_static_shmem(struct device *dev, struct scmi_chan_info

*cinfo,

Quoted 24 lines
Quoted 24 lines
  			      struct scmi_optee_channel *channel)  {
drivers/firmware/arm_scmi/scmi_transport_optee.c +0 -1
-	channel->req.shmem = core->shmem->setup_iomap(cinfo, dev,

true, NULL);

drivers/firmware/arm_scmi/scmi_transport_optee.c +1 -0
+	channel->req.shmem = core->shmem->setup_iomap(cinfo, dev,

true, NULL,

drivers/firmware/arm_scmi/scmi_transport_optee.c +1 -0
+						      &channel-
Quoted 1 line

io_ops);

if (IS_ERR(channel->req.shmem))
return PTR_ERR(channel->req.shmem);

drivers/firmware/arm_scmi/scmi_transport_optee.c +1 -1
@@ -465,7 +468,8 @@ static int scmi_optee_send_message(struct
 scmi_chan_info *cinfo,
  		ret = invoke_process_msg_channel(channel,
  						 core->msg-
 > command_size(xfer));
  	} else {
-		core->shmem->tx_prepare(channel->req.shmem, xfer,
 cinfo);
+		core->shmem->tx_prepare(channel->req.shmem, xfer,

cinfo,

drivers/firmware/arm_scmi/scmi_transport_optee.c +1 -0
+					channel->io_ops->toio);
  		ret = invoke_process_smt_channel(channel);
  	}
Quoted 1 line

drivers/firmware/arm_scmi/scmi_transport_optee.c static void

Quoted 1 line
drivers/firmware/arm_scmi/scmi_transport_optee.c +0 -0
@@ -484,7 +488,8 @@ static void

scmi_optee_fetch_response(struct

Quoted 11 lines
Quoted 11 lines
drivers/firmware/arm_scmi/scmi_transport_optee.c +2 -1
 scmi_chan_info *cinfo,
  		core->msg->fetch_response(channel->req.msg,
  					  channel->rx_len, xfer);
  	else
-		core->shmem->fetch_response(channel->req.shmem,
 xfer);
+		core->shmem->fetch_response(channel->req.shmem,
 xfer,
+					    channel->io_ops->fromio);
  }
Quoted 12 lines

drivers/firmware/arm_scmi/scmi_transport_smc.c line 49

Quoted 12 lines

static void scmi_optee_mark_txdone(struct scmi_chan_info *cinfo,
int ret, diff --git a/drivers/firmware/arm_scmi/scmi_transport_smc.c b/drivers/firmware/arm_scmi/scmi_transport_smc.c
index c6c69a17a9cc..04e715ec1570 100644

drivers/firmware/arm_scmi/scmi_transport_smc.c +1 -0
@@ -45,6 +45,7 @@
   * @irq: An optional IRQ for completion
   * @cinfo: SCMI channel info
   * @shmem: Transmit/Receive shared memory area
+ * @io_ops: Transport specific I/O operations
   * @shmem_lock: Lock to protect access to Tx/Rx shared memory

area.

Quoted 4 lines
Quoted 4 lines
drivers/firmware/arm_scmi/scmi_transport_smc.c +0 -0
   *		Used when NOT operating in atomic mode.
   * @inflight: Atomic flag to protect access to Tx/Rx shared memory

area.

drivers/firmware/arm_scmi/scmi_transport_smc.c +0 -0
@@ -60,6 +61,7 @@ struct scmi_smc {
Quoted 8 lines

drivers/firmware/arm_scmi/scmi_transport_smc.c static int smc_chan_setup(struct

Quoted 8 lines
drivers/firmware/arm_scmi/scmi_transport_smc.c +1 -0
  	int irq;
  	struct scmi_chan_info *cinfo;
  	struct scmi_shared_mem __iomem *shmem;
+	struct scmi_shmem_io_ops *io_ops;
  	/* Protect access to shmem area */
  	struct mutex shmem_lock;
  #define INFLIGHT_NONE	MSG_TOKEN_MAX
@@ -144,7 +146,8 @@ static int smc_chan_setup(struct

scmi_chan_info

Quoted 9 lines
Quoted 9 lines
drivers/firmware/arm_scmi/scmi_transport_smc.c +2 -1
 *cinfo, struct device *dev,
  	if (!scmi_info)
  		return -ENOMEM;
 
-	scmi_info->shmem = core->shmem->setup_iomap(cinfo, dev,
 tx, &res);
+	scmi_info->shmem = core->shmem->setup_iomap(cinfo, dev,
 tx, &res,
+						    &scmi_info-
Quoted 5 lines

drivers/firmware/arm_scmi/scmi_transport_smc.c static int smc_send_message(struct

Quoted 5 lines
Quoted 1 line

io_ops);

if (IS_ERR(scmi_info->shmem))
return PTR_ERR(scmi_info->shmem);

drivers/firmware/arm_scmi/scmi_transport_smc.c +0 -0
@@ -229,7 +232,8 @@ static int smc_send_message(struct

scmi_chan_info

Quoted 29 lines
Quoted 29 lines
drivers/firmware/arm_scmi/scmi_transport_smc.c +2 -1
 *cinfo,
  	 */
  	smc_channel_lock_acquire(scmi_info, xfer);
 
-	core->shmem->tx_prepare(scmi_info->shmem, xfer, cinfo);
+	core->shmem->tx_prepare(scmi_info->shmem, xfer, cinfo,
+				scmi_info->io_ops->toio);
 
  	if (scmi_info->cap_id != ULONG_MAX)

arm_smccc_1_1_invoke(scmi_info->func_id,
scmi_info->cap_id, 0, @@ -253,7 +257,8 @@ static void smc_fetch_response(struct scmi_chan_info *cinfo, {
struct scmi_smc *scmi_info = cinfo->transport_info;

drivers/firmware/arm_scmi/scmi_transport_smc.c +2 -1
-	core->shmem->fetch_response(scmi_info->shmem, xfer);
+	core->shmem->fetch_response(scmi_info->shmem, xfer,
+				    scmi_info->io_ops->fromio);
  }
 
  static void smc_mark_txdone(struct scmi_chan_info *cinfo, int ret,
drivers/firmware/arm_scmi/shmem.c +0 -0
index 01d8a9398fe8..aded5f1cd49f 100644
@@ -34,9 +34,67 @@ struct scmi_shared_mem {
  	u8 msg_payload[];
  };
 
Quoted 13 lines

drivers/firmware/arm_scmi/shmem.c struct scmi_shared_mem { · line 45

Quoted 10 lines
drivers/firmware/arm_scmi/shmem.c +9 -0
+static inline void shmem_memcpy_fromio32(void *to,
+					 const volatile void __iomem
 *from,
+					 size_t count)
+{
+	while (count) {
+		*(u32 *)to = __raw_readl(from);
+		from += 4;
+		to += 4;
+		count -= 4;

This may not have issue, considering the 'count % 4' will be 0 and 'from' is 4 bytes aligned.

Quoted 8 lines

Correct, this cannot possibly happen today by virtue of msg->payload being naturally aligned on a 4 bytes boundary.

Quoted 3 lines

But I think it maybe better to add check if 'count' and 'from' are not 4 bytes aligned.

Humm, what would be the fallback, or should we just WARN()?

This is just to avoid potential issues if there is change in future spec That some entries are not 4 bytes aligned. I maybe over thinking here.

A "WARN" is ok from my view.

Regards,
Peng.

#6 lore raw

Re: [PATCH v3 2/2] firmware: arm_scmi: Support 'reg-io-width' property for shared memory

In reply to Florian Fainelli

Hi Florian,

Sorry for getting late to this party, I wasn't able to review this before. Overall changes look correct. But my main concern is that is SCMI the right place to have such IO accessors. It is better to run it through Arnd if he is happy with it before I send him the pull request containing these.

Quoted 4 lines · Florian Fainelli

Some shared memory areas might only support a certain access width, such as 32-bit, which memcpy_{from,to}_io() does not adhere to at least on ARM64 by making both 8-bit and 64-bit accesses to such memory.

Is this limitation on the hardware for both read and writes ? The reason I ask is I see arm64 does have memcpy_toio_aligned() or __iowrite32_copy_full() for 32 bit aligned writes.

Signature · 2 lines
Regards,
Sudeep
#7 lore raw

Re: [PATCH v3 2/2] firmware: arm_scmi: Support 'reg-io-width' property for shared memory

Quoted 6 lines · Sudeep Holla

Hi Florian,

Sorry for getting late to this party, I wasn't able to review this before. Overall changes look correct. But my main concern is that is SCMI the right place to have such IO accessors. It is better to run it through Arnd if he is happy with it before I send him the pull request containing these.

Sure, would definitively want more eyes to review.

Quoted 8 lines · Florian Fainelli
Quoted 4 lines

Some shared memory areas might only support a certain access width, such as 32-bit, which memcpy_{from,to}_io() does not adhere to at least on ARM64 by making both 8-bit and 64-bit accesses to such memory.

Is this limitation on the hardware for both read and writes ?

This applies to both reads and writes. We have to make accesses on a 4 byte boundary and of exactly 4 bytes in size.

Quoted 3 lines

The reason I ask is I see arm64 does have memcpy_toio_aligned() or __iowrite32_copy_full() for 32 bit aligned writes.

That appears to work nicely on ARM64 and ARM 32-bit, thanks for the suggestion! One needs to be careful that __io{read,write}32_copy takes 32-bit units, not bytes!

FWIW, here is my diff between v3 and v4:

drivers/firmware/arm_scmi/common.h +0 -0
index 73bb496fac01..a13f79b37c99 100644
@@ -319,9 +319,9 @@ enum scmi_bad_msg {
 /* Used for compactness and signature validation of the function 

pointers being
* passed.
*/
-typedef void (*shmem_copy_toio_t)(volatile void __iomem *to, const void *from, +typedef void (*shmem_copy_toio_t)(void __iomem *to, const void *from,

                                  size_t count);
-typedef void (*shmem_copy_fromio_t)(void *to, const volatile void __iomem *from,
+typedef void (*shmem_copy_fromio_t)(void *to, const void __iomem *from,
                                    size_t count);

/**

drivers/firmware/arm_scmi/shmem.c +12 -20
index aded5f1cd49f..e9f30ab671a8 100644
@@ -35,33 +35,25 @@ struct scmi_shared_mem {
 };
 
 static inline void shmem_memcpy_fromio32(void *to,
-                                        const volatile void __iomem *from,
+                                        const void __iomem *from,
                                         size_t count)
 {
-       while (count) {
-               *(u32 *)to = __raw_readl(from);
-               from += 4;
-               to += 4;
-               count -= 4;
-       }
+       WARN_ON(!IS_ALIGNED((unsigned long)from, 4) ||
+               !IS_ALIGNED((unsigned long)to, 4) ||
+               count % 4);
 
-       /* Ensure all reads from I/O have completed */
-       rmb();
+       __ioread32_copy(to, from, count / 4);
 }
 
-static inline void shmem_memcpy_toio32(volatile void __iomem *to,
+static inline void shmem_memcpy_toio32(void __iomem *to,
                                       const void *from,
                                       size_t count)
 {
-       while (count) {
-               __raw_writel(*(u32 *)from, to);
-               from += 4;
-               to += 4;
-               count -= 4;
-       }
+       WARN_ON(!IS_ALIGNED((unsigned long)to, 4) ||
+               !IS_ALIGNED((unsigned long)from, 4) ||
+               count % 4);
 
-       /* Ensure all writes to I/O have completed */
-       wmb();
+       __iowrite32_copy(to, from, count / 4);
 }
 
 static struct scmi_shmem_io_ops shmem_io_ops32 = {
@@ -73,13 +65,13 @@ static struct scmi_shmem_io_ops shmem_io_ops32 = {
  * pre-processor.
  */
 static inline void shmem_memcpy_fromio(void *to,
-                                      const volatile void __iomem *from,
+                                      const void __iomem *from,
                                       size_t count)
 {
        memcpy_fromio(to, from, count);
 }
 
-static inline void shmem_memcpy_toio(volatile void __iomem *to,
+static inline void shmem_memcpy_toio(void __iomem *to,
                                     const void *from,
                                     size_t count)
 {
Signature · 1 line
Florian