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 channel 23 * @cinfo: SCMI channel info 24 * @shmem: Transmit/Receive shared memory area 25 */ 26 struct scmi_mailbox { 27 struct mbox_client cl; 28 struct mbox_chan *chan; 29 struct scmi_chan_info *cinfo; 30 struct scmi_shared_mem __iomem *shmem; 31 }; 32 33 #define client_to_scmi_mailbox(c) container_of(c, struct scmi_mailbox, cl) 34 35 static void tx_prepare(struct mbox_client *cl, void *m) 36 { 37 struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl); 38 39 shmem_tx_prepare(smbox->shmem, m, smbox->cinfo); 40 } 41 42 static void rx_callback(struct mbox_client *cl, void *m) 43 { 44 struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl); 45 46 scmi_rx_callback(smbox->cinfo, shmem_read_header(smbox->shmem), NULL); 47 } 48 49 static bool mailbox_chan_available(struct device_node *of_node, int idx) 50 { 51 return !of_parse_phandle_with_args(of_node, "mboxes", 52 "#mbox-cells", idx, NULL); 53 } 54 55 static int mailbox_chan_validate(struct device *cdev) 56 { 57 int num_mb, num_sh, ret = 0; 58 struct device_node *np = cdev->of_node; 59 60 num_mb = of_count_phandle_with_args(np, "mboxes", "#mbox-cells"); 61 num_sh = of_count_phandle_with_args(np, "shmem", NULL); 62 /* Bail out if mboxes and shmem descriptors are inconsistent */ 63 if (num_mb <= 0 || num_sh > 2 || num_mb != num_sh) { 64 dev_warn(cdev, "Invalid channel descriptor for '%s'\n", 65 of_node_full_name(np)); 66 return -EINVAL; 67 } 68 69 if (num_sh > 1) { 70 struct device_node *np_tx, *np_rx; 71 72 np_tx = of_parse_phandle(np, "shmem", 0); 73 np_rx = of_parse_phandle(np, "shmem", 1); 74 /* SCMI Tx and Rx shared mem areas have to be distinct */ 75 if (!np_tx || !np_rx || np_tx == np_rx) { 76 dev_warn(cdev, "Invalid shmem descriptor for '%s'\n", 77 of_node_full_name(np)); 78 ret = -EINVAL; 79 } 80 81 of_node_put(np_tx); 82 of_node_put(np_rx); 83 } 84 85 return ret; 86 } 87 88 static int mailbox_chan_setup(struct scmi_chan_info *cinfo, struct device *dev, 89 bool tx) 90 { 91 const char *desc = tx ? "Tx" : "Rx"; 92 struct device *cdev = cinfo->dev; 93 struct scmi_mailbox *smbox; 94 struct device_node *shmem; 95 int ret, idx = tx ? 0 : 1; 96 struct mbox_client *cl; 97 resource_size_t size; 98 struct resource res; 99 100 ret = mailbox_chan_validate(cdev); 101 if (ret) 102 return ret; 103 104 smbox = devm_kzalloc(dev, sizeof(*smbox), GFP_KERNEL); 105 if (!smbox) 106 return -ENOMEM; 107 108 shmem = of_parse_phandle(cdev->of_node, "shmem", idx); 109 if (!of_device_is_compatible(shmem, "arm,scmi-shmem")) 110 return -ENXIO; 111 112 ret = of_address_to_resource(shmem, 0, &res); 113 of_node_put(shmem); 114 if (ret) { 115 dev_err(cdev, "failed to get SCMI %s shared memory\n", desc); 116 return ret; 117 } 118 119 size = resource_size(&res); 120 smbox->shmem = devm_ioremap(dev, res.start, size); 121 if (!smbox->shmem) { 122 dev_err(dev, "failed to ioremap SCMI %s shared memory\n", desc); 123 return -EADDRNOTAVAIL; 124 } 125 126 cl = &smbox->cl; 127 cl->dev = cdev; 128 cl->tx_prepare = tx ? tx_prepare : NULL; 129 cl->rx_callback = rx_callback; 130 cl->tx_block = false; 131 cl->knows_txdone = tx; 132 133 smbox->chan = mbox_request_channel(cl, tx ? 0 : 1); 134 if (IS_ERR(smbox->chan)) { 135 ret = PTR_ERR(smbox->chan); 136 if (ret != -EPROBE_DEFER) 137 dev_err(cdev, "failed to request SCMI %s mailbox\n", 138 tx ? "Tx" : "Rx"); 139 return ret; 140 } 141 142 cinfo->transport_info = smbox; 143 smbox->cinfo = cinfo; 144 145 return 0; 146 } 147 148 static int mailbox_chan_free(int id, void *p, void *data) 149 { 150 struct scmi_chan_info *cinfo = p; 151 struct scmi_mailbox *smbox = cinfo->transport_info; 152 153 if (smbox && !IS_ERR(smbox->chan)) { 154 mbox_free_channel(smbox->chan); 155 cinfo->transport_info = NULL; 156 smbox->chan = NULL; 157 smbox->cinfo = NULL; 158 } 159 160 return 0; 161 } 162 163 static int mailbox_send_message(struct scmi_chan_info *cinfo, 164 struct scmi_xfer *xfer) 165 { 166 struct scmi_mailbox *smbox = cinfo->transport_info; 167 int ret; 168 169 ret = mbox_send_message(smbox->chan, xfer); 170 171 /* mbox_send_message returns non-negative value on success, so reset */ 172 if (ret > 0) 173 ret = 0; 174 175 return ret; 176 } 177 178 static void mailbox_mark_txdone(struct scmi_chan_info *cinfo, int ret, 179 struct scmi_xfer *__unused) 180 { 181 struct scmi_mailbox *smbox = cinfo->transport_info; 182 183 /* 184 * NOTE: we might prefer not to need the mailbox ticker to manage the 185 * transfer queueing since the protocol layer queues things by itself. 186 * Unfortunately, we have to kick the mailbox framework after we have 187 * received our message. 188 */ 189 mbox_client_txdone(smbox->chan, ret); 190 } 191 192 static void mailbox_fetch_response(struct scmi_chan_info *cinfo, 193 struct scmi_xfer *xfer) 194 { 195 struct scmi_mailbox *smbox = cinfo->transport_info; 196 197 shmem_fetch_response(smbox->shmem, xfer); 198 } 199 200 static void mailbox_fetch_notification(struct scmi_chan_info *cinfo, 201 size_t max_len, struct scmi_xfer *xfer) 202 { 203 struct scmi_mailbox *smbox = cinfo->transport_info; 204 205 shmem_fetch_notification(smbox->shmem, max_len, xfer); 206 } 207 208 static void mailbox_clear_channel(struct scmi_chan_info *cinfo) 209 { 210 struct scmi_mailbox *smbox = cinfo->transport_info; 211 212 shmem_clear_channel(smbox->shmem); 213 } 214 215 static bool 216 mailbox_poll_done(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer) 217 { 218 struct scmi_mailbox *smbox = cinfo->transport_info; 219 220 return shmem_poll_done(smbox->shmem, xfer); 221 } 222 223 static const struct scmi_transport_ops scmi_mailbox_ops = { 224 .chan_available = mailbox_chan_available, 225 .chan_setup = mailbox_chan_setup, 226 .chan_free = mailbox_chan_free, 227 .send_message = mailbox_send_message, 228 .mark_txdone = mailbox_mark_txdone, 229 .fetch_response = mailbox_fetch_response, 230 .fetch_notification = mailbox_fetch_notification, 231 .clear_channel = mailbox_clear_channel, 232 .poll_done = mailbox_poll_done, 233 }; 234 235 const struct scmi_desc scmi_mailbox_desc = { 236 .ops = &scmi_mailbox_ops, 237 .max_rx_timeout_ms = 30, /* We may increase this if required */ 238 .max_msg = 20, /* Limited by MBOX_TX_QUEUE_LEN */ 239 .max_msg_size = 128, 240 }; 241