1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Message Mailbox Transport
6 * Copyright (C) 2019 ARM Ltd.
10 #include <linux/device.h>
11 #include <linux/mailbox_client.h>
13 #include <linux/of_address.h>
14 #include <linux/slab.h>
19 * struct scmi_mailbox - Structure representing a SCMI mailbox transport
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
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;
35 #define client_to_scmi_mailbox(c) container_of(c, struct scmi_mailbox, cl)
37 static void tx_prepare(struct mbox_client *cl, void *m)
39 struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl);
41 shmem_tx_prepare(smbox->shmem, m, smbox->cinfo);
44 static void rx_callback(struct mbox_client *cl, void *m)
46 struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl);
48 scmi_rx_callback(smbox->cinfo, shmem_read_header(smbox->shmem), NULL);
51 static bool mailbox_chan_available(struct device_node *of_node, int idx)
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().
60 num_mb = of_count_phandle_with_args(of_node, "mboxes", "#mbox-cells");
61 if (num_mb == 3 && idx == 1)
64 return !of_parse_phandle_with_args(of_node, "mboxes",
65 "#mbox-cells", idx, NULL);
69 * mailbox_chan_validate - Validate transport configuration and map channels
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.
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.
82 * Return: 0 on Success or error
84 static int mailbox_chan_validate(struct device *cdev,
85 int *a2p_rx_chan, int *p2a_chan)
87 int num_mb, num_sh, ret = 0;
88 struct device_node *np = cdev->of_node;
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);
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)) {
98 "Invalid channel descriptor for '%s' - mbs:%d shm:%d\n",
99 of_node_full_name(np), num_mb, num_sh);
103 /* Bail out if provided shmem descriptors do not refer distinct areas */
105 struct device_node *np_tx, *np_rx;
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));
119 /* Calculate channels IDs to use depending on mboxes/shmem layout */
145 static int mailbox_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
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;
157 ret = mailbox_chan_validate(cdev, &a2p_rx_chan, &p2a_chan);
161 if (!tx && !p2a_chan)
164 smbox = devm_kzalloc(dev, sizeof(*smbox), GFP_KERNEL);
168 shmem = of_parse_phandle(cdev->of_node, "shmem", idx);
169 if (!of_device_is_compatible(shmem, "arm,scmi-shmem"))
172 ret = of_address_to_resource(shmem, 0, &res);
175 dev_err(cdev, "failed to get SCMI %s shared memory\n", desc);
179 size = resource_size(&res);
180 smbox->shmem = devm_ioremap(dev, res.start, size);
182 dev_err(dev, "failed to ioremap SCMI %s shared memory\n", desc);
183 return -EADDRNOTAVAIL;
188 cl->tx_prepare = tx ? tx_prepare : NULL;
189 cl->rx_callback = rx_callback;
190 cl->tx_block = false;
191 cl->knows_txdone = tx;
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)
198 "failed to request SCMI %s mailbox\n", desc);
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");
213 cinfo->transport_info = smbox;
214 smbox->cinfo = cinfo;
219 static int mailbox_chan_free(int id, void *p, void *data)
221 struct scmi_chan_info *cinfo = p;
222 struct scmi_mailbox *smbox = cinfo->transport_info;
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;
229 smbox->chan_receiver = NULL;
236 static int mailbox_send_message(struct scmi_chan_info *cinfo,
237 struct scmi_xfer *xfer)
239 struct scmi_mailbox *smbox = cinfo->transport_info;
242 ret = mbox_send_message(smbox->chan, xfer);
244 /* mbox_send_message returns non-negative value on success, so reset */
251 static void mailbox_mark_txdone(struct scmi_chan_info *cinfo, int ret,
252 struct scmi_xfer *__unused)
254 struct scmi_mailbox *smbox = cinfo->transport_info;
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.
262 mbox_client_txdone(smbox->chan, ret);
265 static void mailbox_fetch_response(struct scmi_chan_info *cinfo,
266 struct scmi_xfer *xfer)
268 struct scmi_mailbox *smbox = cinfo->transport_info;
270 shmem_fetch_response(smbox->shmem, xfer);
273 static void mailbox_fetch_notification(struct scmi_chan_info *cinfo,
274 size_t max_len, struct scmi_xfer *xfer)
276 struct scmi_mailbox *smbox = cinfo->transport_info;
278 shmem_fetch_notification(smbox->shmem, max_len, xfer);
281 static void mailbox_clear_channel(struct scmi_chan_info *cinfo)
283 struct scmi_mailbox *smbox = cinfo->transport_info;
285 shmem_clear_channel(smbox->shmem);
289 mailbox_poll_done(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
291 struct scmi_mailbox *smbox = cinfo->transport_info;
293 return shmem_poll_done(smbox->shmem, xfer);
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,
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 */