Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

firmware: arm_scmi: Add atomic mode support to smc transport

Add a Kernel configuration option to enable SCMI SMC transport atomic
mode operation for selected SCMI transactions and leave it as default
disabled.

Substitute mutex usages with busy-waiting and declare smc transport as
.atomic_enabled if such Kernel configuration option is enabled.

Link: https://lore.kernel.org/r/20211220195646.44498-8-cristian.marussi@arm.com
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>

authored by

Cristian Marussi and committed by
Sudeep Holla
0bfdca8a 69255e74

+64 -6
+14
drivers/firmware/arm_scmi/Kconfig
··· 78 78 If you want the ARM SCMI PROTOCOL stack to include support for a 79 79 transport based on SMC, answer Y. 80 80 81 + config ARM_SCMI_TRANSPORT_SMC_ATOMIC_ENABLE 82 + bool "Enable atomic mode support for SCMI SMC transport" 83 + depends on ARM_SCMI_TRANSPORT_SMC 84 + help 85 + Enable support of atomic operation for SCMI SMC based transport. 86 + 87 + If you want the SCMI SMC based transport to operate in atomic 88 + mode, avoiding any kind of sleeping behaviour for selected 89 + transactions on the TX path, answer Y. 90 + Enabling atomic mode operations allows any SCMI driver using this 91 + transport to optionally ask for atomic SCMI transactions and operate 92 + in atomic context too, at the price of using a number of busy-waiting 93 + primitives all over instead. If unsure say N. 94 + 81 95 config ARM_SCMI_TRANSPORT_VIRTIO 82 96 bool "SCMI transport based on VirtIO" 83 97 depends on VIRTIO=y || VIRTIO=ARM_SCMI_PROTOCOL
+50 -6
drivers/firmware/arm_scmi/smc.c
··· 7 7 */ 8 8 9 9 #include <linux/arm-smccc.h> 10 + #include <linux/atomic.h> 10 11 #include <linux/device.h> 11 12 #include <linux/err.h> 12 13 #include <linux/interrupt.h> ··· 15 14 #include <linux/of.h> 16 15 #include <linux/of_address.h> 17 16 #include <linux/of_irq.h> 17 + #include <linux/processor.h> 18 18 #include <linux/slab.h> 19 19 20 20 #include "common.h" ··· 25 23 * 26 24 * @cinfo: SCMI channel info 27 25 * @shmem: Transmit/Receive shared memory area 28 - * @shmem_lock: Lock to protect access to Tx/Rx shared memory area 26 + * @shmem_lock: Lock to protect access to Tx/Rx shared memory area. 27 + * Used when NOT operating in atomic mode. 28 + * @inflight: Atomic flag to protect access to Tx/Rx shared memory area. 29 + * Used when operating in atomic mode. 29 30 * @func_id: smc/hvc call function id 30 31 */ 31 32 32 33 struct scmi_smc { 33 34 struct scmi_chan_info *cinfo; 34 35 struct scmi_shared_mem __iomem *shmem; 36 + /* Protect access to shmem area */ 35 37 struct mutex shmem_lock; 38 + #define INFLIGHT_NONE MSG_TOKEN_MAX 39 + atomic_t inflight; 36 40 u32 func_id; 37 41 }; 38 42 ··· 60 52 61 53 of_node_put(np); 62 54 return true; 55 + } 56 + 57 + static inline void smc_channel_lock_init(struct scmi_smc *scmi_info) 58 + { 59 + if (IS_ENABLED(CONFIG_ARM_SCMI_TRANSPORT_SMC_ATOMIC_ENABLE)) 60 + atomic_set(&scmi_info->inflight, INFLIGHT_NONE); 61 + else 62 + mutex_init(&scmi_info->shmem_lock); 63 + } 64 + 65 + static bool smc_xfer_inflight(struct scmi_xfer *xfer, atomic_t *inflight) 66 + { 67 + int ret; 68 + 69 + ret = atomic_cmpxchg(inflight, INFLIGHT_NONE, xfer->hdr.seq); 70 + 71 + return ret == INFLIGHT_NONE; 72 + } 73 + 74 + static inline void 75 + smc_channel_lock_acquire(struct scmi_smc *scmi_info, 76 + struct scmi_xfer *xfer __maybe_unused) 77 + { 78 + if (IS_ENABLED(CONFIG_ARM_SCMI_TRANSPORT_SMC_ATOMIC_ENABLE)) 79 + spin_until_cond(smc_xfer_inflight(xfer, &scmi_info->inflight)); 80 + else 81 + mutex_lock(&scmi_info->shmem_lock); 82 + } 83 + 84 + static inline void smc_channel_lock_release(struct scmi_smc *scmi_info) 85 + { 86 + if (IS_ENABLED(CONFIG_ARM_SCMI_TRANSPORT_SMC_ATOMIC_ENABLE)) 87 + atomic_set(&scmi_info->inflight, INFLIGHT_NONE); 88 + else 89 + mutex_unlock(&scmi_info->shmem_lock); 63 90 } 64 91 65 92 static int smc_chan_setup(struct scmi_chan_info *cinfo, struct device *dev, ··· 157 114 158 115 scmi_info->func_id = func_id; 159 116 scmi_info->cinfo = cinfo; 160 - mutex_init(&scmi_info->shmem_lock); 117 + smc_channel_lock_init(scmi_info); 161 118 cinfo->transport_info = scmi_info; 162 119 163 120 return 0; ··· 183 140 struct arm_smccc_res res; 184 141 185 142 /* 186 - * Channel lock will be released only once response has been 143 + * Channel will be released only once response has been 187 144 * surely fully retrieved, so after .mark_txdone() 188 145 */ 189 - mutex_lock(&scmi_info->shmem_lock); 146 + smc_channel_lock_acquire(scmi_info, xfer); 190 147 191 148 shmem_tx_prepare(scmi_info->shmem, xfer); 192 149 ··· 194 151 195 152 /* Only SMCCC_RET_NOT_SUPPORTED is valid error code */ 196 153 if (res.a0) { 197 - mutex_unlock(&scmi_info->shmem_lock); 154 + smc_channel_lock_release(scmi_info); 198 155 return -EOPNOTSUPP; 199 156 } 200 157 ··· 213 170 { 214 171 struct scmi_smc *scmi_info = cinfo->transport_info; 215 172 216 - mutex_unlock(&scmi_info->shmem_lock); 173 + smc_channel_lock_release(scmi_info); 217 174 } 218 175 219 176 static const struct scmi_transport_ops scmi_smc_ops = { ··· 239 196 * from the shared memory area. 240 197 */ 241 198 .sync_cmds_completed_on_ret = true, 199 + .atomic_enabled = IS_ENABLED(CONFIG_ARM_SCMI_TRANSPORT_SMC_ATOMIC_ENABLE), 242 200 };