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