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 * @chan_platform_receiver: Optional Platform Receiver mailbox unidirectional channel
25 * @cinfo: SCMI channel info
26 * @shmem: Transmit/Receive shared memory area
27 */
28 struct scmi_mailbox {
29 struct mbox_client cl;
30 struct mbox_chan *chan;
31 struct mbox_chan *chan_receiver;
32 struct mbox_chan *chan_platform_receiver;
33 struct scmi_chan_info *cinfo;
34 struct scmi_shared_mem __iomem *shmem;
35 };
36
37 #define client_to_scmi_mailbox(c) container_of(c, struct scmi_mailbox, cl)
38
tx_prepare(struct mbox_client * cl,void * m)39 static void tx_prepare(struct mbox_client *cl, void *m)
40 {
41 struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl);
42
43 shmem_tx_prepare(smbox->shmem, m, smbox->cinfo);
44 }
45
rx_callback(struct mbox_client * cl,void * m)46 static void rx_callback(struct mbox_client *cl, void *m)
47 {
48 struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl);
49
50 /*
51 * An A2P IRQ is NOT valid when received while the platform still has
52 * the ownership of the channel, because the platform at first releases
53 * the SMT channel and then sends the completion interrupt.
54 *
55 * This addresses a possible race condition in which a spurious IRQ from
56 * a previous timed-out reply which arrived late could be wrongly
57 * associated with the next pending transaction.
58 */
59 if (cl->knows_txdone && !shmem_channel_free(smbox->shmem)) {
60 dev_warn(smbox->cinfo->dev, "Ignoring spurious A2P IRQ !\n");
61 return;
62 }
63
64 scmi_rx_callback(smbox->cinfo, shmem_read_header(smbox->shmem), NULL);
65 }
66
mailbox_chan_available(struct device_node * of_node,int idx)67 static bool mailbox_chan_available(struct device_node *of_node, int idx)
68 {
69 int num_mb;
70
71 /*
72 * Just check if bidirrectional channels are involved, and check the
73 * index accordingly; proper full validation will be made later
74 * in mailbox_chan_setup().
75 */
76 num_mb = of_count_phandle_with_args(of_node, "mboxes", "#mbox-cells");
77 if (num_mb == 3 && idx == 1)
78 idx = 2;
79
80 return !of_parse_phandle_with_args(of_node, "mboxes",
81 "#mbox-cells", idx, NULL);
82 }
83
84 /**
85 * mailbox_chan_validate - Validate transport configuration and map channels
86 *
87 * @cdev: Reference to the underlying transport device carrying the
88 * of_node descriptor to analyze.
89 * @a2p_rx_chan: A reference to an optional unidirectional channel to use
90 * for replies on the a2p channel. Set as zero if not present.
91 * @p2a_chan: A reference to the optional p2a channel.
92 * Set as zero if not present.
93 * @p2a_rx_chan: A reference to the optional p2a completion channel.
94 * Set as zero if not present.
95 *
96 * At first, validate the transport configuration as described in terms of
97 * 'mboxes' and 'shmem', then determin which mailbox channel indexes are
98 * appropriate to be use in the current configuration.
99 *
100 * Return: 0 on Success or error
101 */
mailbox_chan_validate(struct device * cdev,int * a2p_rx_chan,int * p2a_chan,int * p2a_rx_chan)102 static int mailbox_chan_validate(struct device *cdev, int *a2p_rx_chan,
103 int *p2a_chan, int *p2a_rx_chan)
104 {
105 int num_mb, num_sh, ret = 0;
106 struct device_node *np = cdev->of_node;
107
108 num_mb = of_count_phandle_with_args(np, "mboxes", "#mbox-cells");
109 num_sh = of_count_phandle_with_args(np, "shmem", NULL);
110 dev_dbg(cdev, "Found %d mboxes and %d shmems !\n", num_mb, num_sh);
111
112 /* Bail out if mboxes and shmem descriptors are inconsistent */
113 if (num_mb <= 0 || num_sh <= 0 || num_sh > 2 || num_mb > 4 ||
114 (num_mb == 1 && num_sh != 1) || (num_mb == 3 && num_sh != 2) ||
115 (num_mb == 4 && num_sh != 2)) {
116 dev_warn(cdev,
117 "Invalid channel descriptor for '%s' - mbs:%d shm:%d\n",
118 of_node_full_name(np), num_mb, num_sh);
119 return -EINVAL;
120 }
121
122 /* Bail out if provided shmem descriptors do not refer distinct areas */
123 if (num_sh > 1) {
124 struct device_node *np_tx, *np_rx;
125
126 np_tx = of_parse_phandle(np, "shmem", 0);
127 np_rx = of_parse_phandle(np, "shmem", 1);
128 if (!np_tx || !np_rx || np_tx == np_rx) {
129 dev_warn(cdev, "Invalid shmem descriptor for '%s'\n",
130 of_node_full_name(np));
131 ret = -EINVAL;
132 }
133
134 of_node_put(np_tx);
135 of_node_put(np_rx);
136 }
137
138 /* Calculate channels IDs to use depending on mboxes/shmem layout */
139 if (!ret) {
140 switch (num_mb) {
141 case 1:
142 *a2p_rx_chan = 0;
143 *p2a_chan = 0;
144 *p2a_rx_chan = 0;
145 break;
146 case 2:
147 if (num_sh == 2) {
148 *a2p_rx_chan = 0;
149 *p2a_chan = 1;
150 } else {
151 *a2p_rx_chan = 1;
152 *p2a_chan = 0;
153 }
154 *p2a_rx_chan = 0;
155 break;
156 case 3:
157 *a2p_rx_chan = 1;
158 *p2a_chan = 2;
159 *p2a_rx_chan = 0;
160 break;
161 case 4:
162 *a2p_rx_chan = 1;
163 *p2a_chan = 2;
164 *p2a_rx_chan = 3;
165 break;
166 }
167 }
168
169 return ret;
170 }
171
mailbox_chan_setup(struct scmi_chan_info * cinfo,struct device * dev,bool tx)172 static int mailbox_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
173 bool tx)
174 {
175 const char *desc = tx ? "Tx" : "Rx";
176 struct device *cdev = cinfo->dev;
177 struct scmi_mailbox *smbox;
178 struct device_node *shmem;
179 int ret, a2p_rx_chan, p2a_chan, p2a_rx_chan, idx = tx ? 0 : 1;
180 struct mbox_client *cl;
181 resource_size_t size;
182 struct resource res;
183
184 ret = mailbox_chan_validate(cdev, &a2p_rx_chan, &p2a_chan, &p2a_rx_chan);
185 if (ret)
186 return ret;
187
188 if (!tx && !p2a_chan)
189 return -ENODEV;
190
191 smbox = devm_kzalloc(dev, sizeof(*smbox), GFP_KERNEL);
192 if (!smbox)
193 return -ENOMEM;
194
195 shmem = of_parse_phandle(cdev->of_node, "shmem", idx);
196 if (!of_device_is_compatible(shmem, "arm,scmi-shmem")) {
197 of_node_put(shmem);
198 return -ENXIO;
199 }
200
201 ret = of_address_to_resource(shmem, 0, &res);
202 of_node_put(shmem);
203 if (ret) {
204 dev_err(cdev, "failed to get SCMI %s shared memory\n", desc);
205 return ret;
206 }
207
208 size = resource_size(&res);
209 smbox->shmem = devm_ioremap(dev, res.start, size);
210 if (!smbox->shmem) {
211 dev_err(dev, "failed to ioremap SCMI %s shared memory\n", desc);
212 return -EADDRNOTAVAIL;
213 }
214
215 cl = &smbox->cl;
216 cl->dev = cdev;
217 cl->tx_prepare = tx ? tx_prepare : NULL;
218 cl->rx_callback = rx_callback;
219 cl->tx_block = false;
220 cl->knows_txdone = tx;
221
222 smbox->chan = mbox_request_channel(cl, tx ? 0 : p2a_chan);
223 if (IS_ERR(smbox->chan)) {
224 ret = PTR_ERR(smbox->chan);
225 if (ret != -EPROBE_DEFER)
226 dev_err(cdev,
227 "failed to request SCMI %s mailbox\n", desc);
228 return ret;
229 }
230
231 /* Additional unidirectional channel for TX if needed */
232 if (tx && a2p_rx_chan) {
233 smbox->chan_receiver = mbox_request_channel(cl, a2p_rx_chan);
234 if (IS_ERR(smbox->chan_receiver)) {
235 ret = PTR_ERR(smbox->chan_receiver);
236 if (ret != -EPROBE_DEFER)
237 dev_err(cdev, "failed to request SCMI Tx Receiver mailbox\n");
238 return ret;
239 }
240 }
241
242 if (!tx && p2a_rx_chan) {
243 smbox->chan_platform_receiver = mbox_request_channel(cl, p2a_rx_chan);
244 if (IS_ERR(smbox->chan_platform_receiver)) {
245 ret = PTR_ERR(smbox->chan_platform_receiver);
246 if (ret != -EPROBE_DEFER)
247 dev_err(cdev, "failed to request SCMI P2A Receiver mailbox\n");
248 return ret;
249 }
250 }
251
252
253 cinfo->transport_info = smbox;
254 smbox->cinfo = cinfo;
255
256 return 0;
257 }
258
mailbox_chan_free(int id,void * p,void * data)259 static int mailbox_chan_free(int id, void *p, void *data)
260 {
261 struct scmi_chan_info *cinfo = p;
262 struct scmi_mailbox *smbox = cinfo->transport_info;
263
264 if (smbox && !IS_ERR(smbox->chan)) {
265 mbox_free_channel(smbox->chan);
266 mbox_free_channel(smbox->chan_receiver);
267 mbox_free_channel(smbox->chan_platform_receiver);
268 cinfo->transport_info = NULL;
269 smbox->chan = NULL;
270 smbox->chan_receiver = NULL;
271 smbox->chan_platform_receiver = NULL;
272 smbox->cinfo = NULL;
273 }
274
275 return 0;
276 }
277
mailbox_send_message(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)278 static int mailbox_send_message(struct scmi_chan_info *cinfo,
279 struct scmi_xfer *xfer)
280 {
281 struct scmi_mailbox *smbox = cinfo->transport_info;
282 int ret;
283
284 ret = mbox_send_message(smbox->chan, xfer);
285
286 /* mbox_send_message returns non-negative value on success, so reset */
287 if (ret > 0)
288 ret = 0;
289
290 return ret;
291 }
292
mailbox_mark_txdone(struct scmi_chan_info * cinfo,int ret,struct scmi_xfer * __unused)293 static void mailbox_mark_txdone(struct scmi_chan_info *cinfo, int ret,
294 struct scmi_xfer *__unused)
295 {
296 struct scmi_mailbox *smbox = cinfo->transport_info;
297
298 /*
299 * NOTE: we might prefer not to need the mailbox ticker to manage the
300 * transfer queueing since the protocol layer queues things by itself.
301 * Unfortunately, we have to kick the mailbox framework after we have
302 * received our message.
303 */
304 mbox_client_txdone(smbox->chan, ret);
305 }
306
mailbox_fetch_response(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)307 static void mailbox_fetch_response(struct scmi_chan_info *cinfo,
308 struct scmi_xfer *xfer)
309 {
310 struct scmi_mailbox *smbox = cinfo->transport_info;
311
312 shmem_fetch_response(smbox->shmem, xfer);
313 }
314
mailbox_fetch_notification(struct scmi_chan_info * cinfo,size_t max_len,struct scmi_xfer * xfer)315 static void mailbox_fetch_notification(struct scmi_chan_info *cinfo,
316 size_t max_len, struct scmi_xfer *xfer)
317 {
318 struct scmi_mailbox *smbox = cinfo->transport_info;
319
320 shmem_fetch_notification(smbox->shmem, max_len, xfer);
321 }
322
mailbox_clear_channel(struct scmi_chan_info * cinfo)323 static void mailbox_clear_channel(struct scmi_chan_info *cinfo)
324 {
325 struct scmi_mailbox *smbox = cinfo->transport_info;
326 struct device *cdev = cinfo->dev;
327 struct mbox_chan *intr;
328 int ret;
329
330 shmem_clear_channel(smbox->shmem);
331
332 if (!shmem_channel_intr_enabled(smbox->shmem))
333 return;
334
335 if (smbox->chan_platform_receiver)
336 intr = smbox->chan_platform_receiver;
337 else if (smbox->chan)
338 intr = smbox->chan;
339 else {
340 dev_err(cdev, "Channel INTR wrongly set?\n");
341 return;
342 }
343
344 ret = mbox_send_message(intr, NULL);
345 /* mbox_send_message returns non-negative value on success, so reset */
346 if (ret > 0)
347 ret = 0;
348
349 mbox_client_txdone(intr, ret);
350 }
351
352 static bool
mailbox_poll_done(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)353 mailbox_poll_done(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
354 {
355 struct scmi_mailbox *smbox = cinfo->transport_info;
356
357 return shmem_poll_done(smbox->shmem, xfer);
358 }
359
360 static const struct scmi_transport_ops scmi_mailbox_ops = {
361 .chan_available = mailbox_chan_available,
362 .chan_setup = mailbox_chan_setup,
363 .chan_free = mailbox_chan_free,
364 .send_message = mailbox_send_message,
365 .mark_txdone = mailbox_mark_txdone,
366 .fetch_response = mailbox_fetch_response,
367 .fetch_notification = mailbox_fetch_notification,
368 .clear_channel = mailbox_clear_channel,
369 .poll_done = mailbox_poll_done,
370 };
371
372 const struct scmi_desc scmi_mailbox_desc = {
373 .ops = &scmi_mailbox_ops,
374 .max_rx_timeout_ms = 30, /* We may increase this if required */
375 .max_msg = 20, /* Limited by MBOX_TX_QUEUE_LEN */
376 .max_msg_size = 128,
377 };
378