1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * System Control and Management Interface (SCMI) Message SMC/HVC 4 * Transport driver 5 * 6 * Copyright 2020 NXP 7 */ 8 9 #include <linux/arm-smccc.h> 10 #include <linux/atomic.h> 11 #include <linux/device.h> 12 #include <linux/err.h> 13 #include <linux/interrupt.h> 14 #include <linux/mutex.h> 15 #include <linux/of.h> 16 #include <linux/of_address.h> 17 #include <linux/of_irq.h> 18 #include <linux/processor.h> 19 #include <linux/slab.h> 20 21 #include "common.h" 22 23 /* 24 * The shmem address is split into 4K page and offset. 25 * This is to make sure the parameters fit in 32bit arguments of the 26 * smc/hvc call to keep it uniform across smc32/smc64 conventions. 27 * This however limits the shmem address to 44 bit. 28 * 29 * These optional parameters can be used to distinguish among multiple 30 * scmi instances that are using the same smc-id. 31 * The page parameter is passed in r1/x1/w1 register and the offset parameter 32 * is passed in r2/x2/w2 register. 33 */ 34 35 #define SHMEM_SIZE (SZ_4K) 36 #define SHMEM_SHIFT 12 37 #define SHMEM_PAGE(x) (_UL((x) >> SHMEM_SHIFT)) 38 #define SHMEM_OFFSET(x) ((x) & (SHMEM_SIZE - 1)) 39 40 /** 41 * struct scmi_smc - Structure representing a SCMI smc transport 42 * 43 * @irq: An optional IRQ for completion 44 * @cinfo: SCMI channel info 45 * @shmem: Transmit/Receive shared memory area 46 * @shmem_lock: Lock to protect access to Tx/Rx shared memory area. 47 * Used when NOT operating in atomic mode. 48 * @inflight: Atomic flag to protect access to Tx/Rx shared memory area. 49 * Used when operating in atomic mode. 50 * @func_id: smc/hvc call function id 51 * @param_page: 4K page number of the shmem channel 52 * @param_offset: Offset within the 4K page of the shmem channel 53 */ 54 55 struct scmi_smc { 56 int irq; 57 struct scmi_chan_info *cinfo; 58 struct scmi_shared_mem __iomem *shmem; 59 /* Protect access to shmem area */ 60 struct mutex shmem_lock; 61 #define INFLIGHT_NONE MSG_TOKEN_MAX 62 atomic_t inflight; 63 u32 func_id; 64 u32 param_page; 65 u32 param_offset; 66 }; 67 68 static irqreturn_t smc_msg_done_isr(int irq, void *data) 69 { 70 struct scmi_smc *scmi_info = data; 71 72 scmi_rx_callback(scmi_info->cinfo, 73 shmem_read_header(scmi_info->shmem), NULL); 74 75 return IRQ_HANDLED; 76 } 77 78 static bool smc_chan_available(struct device_node *of_node, int idx) 79 { 80 struct device_node *np = of_parse_phandle(of_node, "shmem", 0); 81 if (!np) 82 return false; 83 84 of_node_put(np); 85 return true; 86 } 87 88 static inline void smc_channel_lock_init(struct scmi_smc *scmi_info) 89 { 90 if (IS_ENABLED(CONFIG_ARM_SCMI_TRANSPORT_SMC_ATOMIC_ENABLE)) 91 atomic_set(&scmi_info->inflight, INFLIGHT_NONE); 92 else 93 mutex_init(&scmi_info->shmem_lock); 94 } 95 96 static bool smc_xfer_inflight(struct scmi_xfer *xfer, atomic_t *inflight) 97 { 98 int ret; 99 100 ret = atomic_cmpxchg(inflight, INFLIGHT_NONE, xfer->hdr.seq); 101 102 return ret == INFLIGHT_NONE; 103 } 104 105 static inline void 106 smc_channel_lock_acquire(struct scmi_smc *scmi_info, 107 struct scmi_xfer *xfer __maybe_unused) 108 { 109 if (IS_ENABLED(CONFIG_ARM_SCMI_TRANSPORT_SMC_ATOMIC_ENABLE)) 110 spin_until_cond(smc_xfer_inflight(xfer, &scmi_info->inflight)); 111 else 112 mutex_lock(&scmi_info->shmem_lock); 113 } 114 115 static inline void smc_channel_lock_release(struct scmi_smc *scmi_info) 116 { 117 if (IS_ENABLED(CONFIG_ARM_SCMI_TRANSPORT_SMC_ATOMIC_ENABLE)) 118 atomic_set(&scmi_info->inflight, INFLIGHT_NONE); 119 else 120 mutex_unlock(&scmi_info->shmem_lock); 121 } 122 123 static int smc_chan_setup(struct scmi_chan_info *cinfo, struct device *dev, 124 bool tx) 125 { 126 struct device *cdev = cinfo->dev; 127 struct scmi_smc *scmi_info; 128 resource_size_t size; 129 struct resource res; 130 struct device_node *np; 131 u32 func_id; 132 int ret; 133 134 if (!tx) 135 return -ENODEV; 136 137 scmi_info = devm_kzalloc(dev, sizeof(*scmi_info), GFP_KERNEL); 138 if (!scmi_info) 139 return -ENOMEM; 140 141 np = of_parse_phandle(cdev->of_node, "shmem", 0); 142 if (!of_device_is_compatible(np, "arm,scmi-shmem")) { 143 of_node_put(np); 144 return -ENXIO; 145 } 146 147 ret = of_address_to_resource(np, 0, &res); 148 of_node_put(np); 149 if (ret) { 150 dev_err(cdev, "failed to get SCMI Tx shared memory\n"); 151 return ret; 152 } 153 154 size = resource_size(&res); 155 scmi_info->shmem = devm_ioremap(dev, res.start, size); 156 if (!scmi_info->shmem) { 157 dev_err(dev, "failed to ioremap SCMI Tx shared memory\n"); 158 return -EADDRNOTAVAIL; 159 } 160 161 ret = of_property_read_u32(dev->of_node, "arm,smc-id", &func_id); 162 if (ret < 0) 163 return ret; 164 165 if (of_device_is_compatible(dev->of_node, "arm,scmi-smc-param")) { 166 scmi_info->param_page = SHMEM_PAGE(res.start); 167 scmi_info->param_offset = SHMEM_OFFSET(res.start); 168 } 169 /* 170 * If there is an interrupt named "a2p", then the service and 171 * completion of a message is signaled by an interrupt rather than by 172 * the return of the SMC call. 173 */ 174 scmi_info->irq = of_irq_get_byname(cdev->of_node, "a2p"); 175 if (scmi_info->irq > 0) { 176 ret = request_irq(scmi_info->irq, smc_msg_done_isr, 177 IRQF_NO_SUSPEND, dev_name(dev), scmi_info); 178 if (ret) { 179 dev_err(dev, "failed to setup SCMI smc irq\n"); 180 return ret; 181 } 182 } else { 183 cinfo->no_completion_irq = true; 184 } 185 186 scmi_info->func_id = func_id; 187 scmi_info->cinfo = cinfo; 188 smc_channel_lock_init(scmi_info); 189 cinfo->transport_info = scmi_info; 190 191 return 0; 192 } 193 194 static int smc_chan_free(int id, void *p, void *data) 195 { 196 struct scmi_chan_info *cinfo = p; 197 struct scmi_smc *scmi_info = cinfo->transport_info; 198 199 /* Ignore any possible further reception on the IRQ path */ 200 if (scmi_info->irq > 0) 201 free_irq(scmi_info->irq, scmi_info); 202 203 cinfo->transport_info = NULL; 204 scmi_info->cinfo = NULL; 205 206 return 0; 207 } 208 209 static int smc_send_message(struct scmi_chan_info *cinfo, 210 struct scmi_xfer *xfer) 211 { 212 struct scmi_smc *scmi_info = cinfo->transport_info; 213 struct arm_smccc_res res; 214 unsigned long page = scmi_info->param_page; 215 unsigned long offset = scmi_info->param_offset; 216 217 /* 218 * Channel will be released only once response has been 219 * surely fully retrieved, so after .mark_txdone() 220 */ 221 smc_channel_lock_acquire(scmi_info, xfer); 222 223 shmem_tx_prepare(scmi_info->shmem, xfer, cinfo); 224 225 arm_smccc_1_1_invoke(scmi_info->func_id, page, offset, 0, 0, 0, 0, 0, 226 &res); 227 228 /* Only SMCCC_RET_NOT_SUPPORTED is valid error code */ 229 if (res.a0) { 230 smc_channel_lock_release(scmi_info); 231 return -EOPNOTSUPP; 232 } 233 234 return 0; 235 } 236 237 static void smc_fetch_response(struct scmi_chan_info *cinfo, 238 struct scmi_xfer *xfer) 239 { 240 struct scmi_smc *scmi_info = cinfo->transport_info; 241 242 shmem_fetch_response(scmi_info->shmem, xfer); 243 } 244 245 static void smc_mark_txdone(struct scmi_chan_info *cinfo, int ret, 246 struct scmi_xfer *__unused) 247 { 248 struct scmi_smc *scmi_info = cinfo->transport_info; 249 250 smc_channel_lock_release(scmi_info); 251 } 252 253 static const struct scmi_transport_ops scmi_smc_ops = { 254 .chan_available = smc_chan_available, 255 .chan_setup = smc_chan_setup, 256 .chan_free = smc_chan_free, 257 .send_message = smc_send_message, 258 .mark_txdone = smc_mark_txdone, 259 .fetch_response = smc_fetch_response, 260 }; 261 262 const struct scmi_desc scmi_smc_desc = { 263 .ops = &scmi_smc_ops, 264 .max_rx_timeout_ms = 30, 265 .max_msg = 20, 266 .max_msg_size = 128, 267 /* 268 * Setting .sync_cmds_atomic_replies to true for SMC assumes that, 269 * once the SMC instruction has completed successfully, the issued 270 * SCMI command would have been already fully processed by the SCMI 271 * platform firmware and so any possible response value expected 272 * for the issued command will be immmediately ready to be fetched 273 * from the shared memory area. 274 */ 275 .sync_cmds_completed_on_ret = true, 276 .atomic_enabled = IS_ENABLED(CONFIG_ARM_SCMI_TRANSPORT_SMC_ATOMIC_ENABLE), 277 }; 278