1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * System Control and Management Interface (SCMI) Message Mailbox Transport 4 * driver. 5 * 6 * Copyright (C) 2019 ARM Ltd. 7 */ 8 9 #include <linux/err.h> 10 #include <linux/device.h> 11 #include <linux/mailbox_client.h> 12 #include <linux/of.h> 13 #include <linux/of_address.h> 14 #include <linux/slab.h> 15 16 #include "common.h" 17 18 /** 19 * struct scmi_mailbox - Structure representing a SCMI mailbox transport 20 * 21 * @cl: Mailbox Client 22 * @chan: Transmit/Receive mailbox uni/bi-directional channel 23 * @chan_receiver: Optional Receiver mailbox unidirectional channel 24 * @cinfo: SCMI channel info 25 * @shmem: Transmit/Receive shared memory area 26 */ 27 struct scmi_mailbox { 28 struct mbox_client cl; 29 struct mbox_chan *chan; 30 struct mbox_chan *chan_receiver; 31 struct scmi_chan_info *cinfo; 32 struct scmi_shared_mem __iomem *shmem; 33 }; 34 35 #define client_to_scmi_mailbox(c) container_of(c, struct scmi_mailbox, cl) 36 37 static void tx_prepare(struct mbox_client *cl, void *m) 38 { 39 struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl); 40 41 shmem_tx_prepare(smbox->shmem, m, smbox->cinfo); 42 } 43 44 static void rx_callback(struct mbox_client *cl, void *m) 45 { 46 struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl); 47 48 scmi_rx_callback(smbox->cinfo, shmem_read_header(smbox->shmem), NULL); 49 } 50 51 static bool mailbox_chan_available(struct device_node *of_node, int idx) 52 { 53 int num_mb; 54 55 /* 56 * Just check if bidirrectional channels are involved, and check the 57 * index accordingly; proper full validation will be made later 58 * in mailbox_chan_setup(). 59 */ 60 num_mb = of_count_phandle_with_args(of_node, "mboxes", "#mbox-cells"); 61 if (num_mb == 3 && idx == 1) 62 idx = 2; 63 64 return !of_parse_phandle_with_args(of_node, "mboxes", 65 "#mbox-cells", idx, NULL); 66 } 67 68 /** 69 * mailbox_chan_validate - Validate transport configuration and map channels 70 * 71 * @cdev: Reference to the underlying transport device carrying the 72 * of_node descriptor to analyze. 73 * @a2p_rx_chan: A reference to an optional unidirectional channel to use 74 * for replies on the a2p channel. Set as zero if not present. 75 * @p2a_chan: A reference to the optional p2a channel. 76 * Set as zero if not present. 77 * 78 * At first, validate the transport configuration as described in terms of 79 * 'mboxes' and 'shmem', then determin which mailbox channel indexes are 80 * appropriate to be use in the current configuration. 81 * 82 * Return: 0 on Success or error 83 */ 84 static int mailbox_chan_validate(struct device *cdev, 85 int *a2p_rx_chan, int *p2a_chan) 86 { 87 int num_mb, num_sh, ret = 0; 88 struct device_node *np = cdev->of_node; 89 90 num_mb = of_count_phandle_with_args(np, "mboxes", "#mbox-cells"); 91 num_sh = of_count_phandle_with_args(np, "shmem", NULL); 92 dev_dbg(cdev, "Found %d mboxes and %d shmems !\n", num_mb, num_sh); 93 94 /* Bail out if mboxes and shmem descriptors are inconsistent */ 95 if (num_mb <= 0 || num_sh <= 0 || num_sh > 2 || num_mb > 3 || 96 (num_mb == 1 && num_sh != 1) || (num_mb == 3 && num_sh != 2)) { 97 dev_warn(cdev, 98 "Invalid channel descriptor for '%s' - mbs:%d shm:%d\n", 99 of_node_full_name(np), num_mb, num_sh); 100 return -EINVAL; 101 } 102 103 /* Bail out if provided shmem descriptors do not refer distinct areas */ 104 if (num_sh > 1) { 105 struct device_node *np_tx, *np_rx; 106 107 np_tx = of_parse_phandle(np, "shmem", 0); 108 np_rx = of_parse_phandle(np, "shmem", 1); 109 if (!np_tx || !np_rx || np_tx == np_rx) { 110 dev_warn(cdev, "Invalid shmem descriptor for '%s'\n", 111 of_node_full_name(np)); 112 ret = -EINVAL; 113 } 114 115 of_node_put(np_tx); 116 of_node_put(np_rx); 117 } 118 119 /* Calculate channels IDs to use depending on mboxes/shmem layout */ 120 if (!ret) { 121 switch (num_mb) { 122 case 1: 123 *a2p_rx_chan = 0; 124 *p2a_chan = 0; 125 break; 126 case 2: 127 if (num_sh == 2) { 128 *a2p_rx_chan = 0; 129 *p2a_chan = 1; 130 } else { 131 *a2p_rx_chan = 1; 132 *p2a_chan = 0; 133 } 134 break; 135 case 3: 136 *a2p_rx_chan = 1; 137 *p2a_chan = 2; 138 break; 139 } 140 } 141 142 return ret; 143 } 144 145 static int mailbox_chan_setup(struct scmi_chan_info *cinfo, struct device *dev, 146 bool tx) 147 { 148 const char *desc = tx ? "Tx" : "Rx"; 149 struct device *cdev = cinfo->dev; 150 struct scmi_mailbox *smbox; 151 struct device_node *shmem; 152 int ret, a2p_rx_chan, p2a_chan, idx = tx ? 0 : 1; 153 struct mbox_client *cl; 154 resource_size_t size; 155 struct resource res; 156 157 ret = mailbox_chan_validate(cdev, &a2p_rx_chan, &p2a_chan); 158 if (ret) 159 return ret; 160 161 if (!tx && !p2a_chan) 162 return -ENODEV; 163 164 smbox = devm_kzalloc(dev, sizeof(*smbox), GFP_KERNEL); 165 if (!smbox) 166 return -ENOMEM; 167 168 shmem = of_parse_phandle(cdev->of_node, "shmem", idx); 169 if (!of_device_is_compatible(shmem, "arm,scmi-shmem")) 170 return -ENXIO; 171 172 ret = of_address_to_resource(shmem, 0, &res); 173 of_node_put(shmem); 174 if (ret) { 175 dev_err(cdev, "failed to get SCMI %s shared memory\n", desc); 176 return ret; 177 } 178 179 size = resource_size(&res); 180 smbox->shmem = devm_ioremap(dev, res.start, size); 181 if (!smbox->shmem) { 182 dev_err(dev, "failed to ioremap SCMI %s shared memory\n", desc); 183 return -EADDRNOTAVAIL; 184 } 185 186 cl = &smbox->cl; 187 cl->dev = cdev; 188 cl->tx_prepare = tx ? tx_prepare : NULL; 189 cl->rx_callback = rx_callback; 190 cl->tx_block = false; 191 cl->knows_txdone = tx; 192 193 smbox->chan = mbox_request_channel(cl, tx ? 0 : p2a_chan); 194 if (IS_ERR(smbox->chan)) { 195 ret = PTR_ERR(smbox->chan); 196 if (ret != -EPROBE_DEFER) 197 dev_err(cdev, 198 "failed to request SCMI %s mailbox\n", desc); 199 return ret; 200 } 201 202 /* Additional unidirectional channel for TX if needed */ 203 if (tx && a2p_rx_chan) { 204 smbox->chan_receiver = mbox_request_channel(cl, a2p_rx_chan); 205 if (IS_ERR(smbox->chan_receiver)) { 206 ret = PTR_ERR(smbox->chan_receiver); 207 if (ret != -EPROBE_DEFER) 208 dev_err(cdev, "failed to request SCMI Tx Receiver mailbox\n"); 209 return ret; 210 } 211 } 212 213 cinfo->transport_info = smbox; 214 smbox->cinfo = cinfo; 215 216 return 0; 217 } 218 219 static int mailbox_chan_free(int id, void *p, void *data) 220 { 221 struct scmi_chan_info *cinfo = p; 222 struct scmi_mailbox *smbox = cinfo->transport_info; 223 224 if (smbox && !IS_ERR(smbox->chan)) { 225 mbox_free_channel(smbox->chan); 226 mbox_free_channel(smbox->chan_receiver); 227 cinfo->transport_info = NULL; 228 smbox->chan = NULL; 229 smbox->chan_receiver = NULL; 230 smbox->cinfo = NULL; 231 } 232 233 return 0; 234 } 235 236 static int mailbox_send_message(struct scmi_chan_info *cinfo, 237 struct scmi_xfer *xfer) 238 { 239 struct scmi_mailbox *smbox = cinfo->transport_info; 240 int ret; 241 242 ret = mbox_send_message(smbox->chan, xfer); 243 244 /* mbox_send_message returns non-negative value on success, so reset */ 245 if (ret > 0) 246 ret = 0; 247 248 return ret; 249 } 250 251 static void mailbox_mark_txdone(struct scmi_chan_info *cinfo, int ret, 252 struct scmi_xfer *__unused) 253 { 254 struct scmi_mailbox *smbox = cinfo->transport_info; 255 256 /* 257 * NOTE: we might prefer not to need the mailbox ticker to manage the 258 * transfer queueing since the protocol layer queues things by itself. 259 * Unfortunately, we have to kick the mailbox framework after we have 260 * received our message. 261 */ 262 mbox_client_txdone(smbox->chan, ret); 263 } 264 265 static void mailbox_fetch_response(struct scmi_chan_info *cinfo, 266 struct scmi_xfer *xfer) 267 { 268 struct scmi_mailbox *smbox = cinfo->transport_info; 269 270 shmem_fetch_response(smbox->shmem, xfer); 271 } 272 273 static void mailbox_fetch_notification(struct scmi_chan_info *cinfo, 274 size_t max_len, struct scmi_xfer *xfer) 275 { 276 struct scmi_mailbox *smbox = cinfo->transport_info; 277 278 shmem_fetch_notification(smbox->shmem, max_len, xfer); 279 } 280 281 static void mailbox_clear_channel(struct scmi_chan_info *cinfo) 282 { 283 struct scmi_mailbox *smbox = cinfo->transport_info; 284 285 shmem_clear_channel(smbox->shmem); 286 } 287 288 static bool 289 mailbox_poll_done(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer) 290 { 291 struct scmi_mailbox *smbox = cinfo->transport_info; 292 293 return shmem_poll_done(smbox->shmem, xfer); 294 } 295 296 static const struct scmi_transport_ops scmi_mailbox_ops = { 297 .chan_available = mailbox_chan_available, 298 .chan_setup = mailbox_chan_setup, 299 .chan_free = mailbox_chan_free, 300 .send_message = mailbox_send_message, 301 .mark_txdone = mailbox_mark_txdone, 302 .fetch_response = mailbox_fetch_response, 303 .fetch_notification = mailbox_fetch_notification, 304 .clear_channel = mailbox_clear_channel, 305 .poll_done = mailbox_poll_done, 306 }; 307 308 const struct scmi_desc scmi_mailbox_desc = { 309 .ops = &scmi_mailbox_ops, 310 .max_rx_timeout_ms = 30, /* We may increase this if required */ 311 .max_msg = 20, /* Limited by MBOX_TX_QUEUE_LEN */ 312 .max_msg_size = 128, 313 }; 314