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 * @chan_lock: Lock that prevents multiple xfers from being queued
27 */
28 struct scmi_mailbox {
29 struct mbox_client cl;
30 struct mbox_chan *chan;
31 struct mbox_chan *chan_receiver;
32 struct scmi_chan_info *cinfo;
33 struct scmi_shared_mem __iomem *shmem;
34 struct mutex chan_lock;
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 scmi_bad_message_trace(smbox->cinfo,
62 shmem_read_header(smbox->shmem),
63 MSG_MBOX_SPURIOUS);
64 return;
65 }
66
67 scmi_rx_callback(smbox->cinfo, shmem_read_header(smbox->shmem), NULL);
68 }
69
mailbox_chan_available(struct device_node * of_node,int idx)70 static bool mailbox_chan_available(struct device_node *of_node, int idx)
71 {
72 int num_mb;
73
74 /*
75 * Just check if bidirrectional channels are involved, and check the
76 * index accordingly; proper full validation will be made later
77 * in mailbox_chan_setup().
78 */
79 num_mb = of_count_phandle_with_args(of_node, "mboxes", "#mbox-cells");
80 if (num_mb == 3 && idx == 1)
81 idx = 2;
82
83 return !of_parse_phandle_with_args(of_node, "mboxes",
84 "#mbox-cells", idx, NULL);
85 }
86
87 /**
88 * mailbox_chan_validate - Validate transport configuration and map channels
89 *
90 * @cdev: Reference to the underlying transport device carrying the
91 * of_node descriptor to analyze.
92 * @a2p_rx_chan: A reference to an optional unidirectional channel to use
93 * for replies on the a2p channel. Set as zero if not present.
94 * @p2a_chan: A reference to the optional p2a channel.
95 * Set as zero if not present.
96 *
97 * At first, validate the transport configuration as described in terms of
98 * 'mboxes' and 'shmem', then determin which mailbox channel indexes are
99 * appropriate to be use in the current configuration.
100 *
101 * Return: 0 on Success or error
102 */
mailbox_chan_validate(struct device * cdev,int * a2p_rx_chan,int * p2a_chan)103 static int mailbox_chan_validate(struct device *cdev,
104 int *a2p_rx_chan, int *p2a_chan)
105 {
106 int num_mb, num_sh, ret = 0;
107 struct device_node *np = cdev->of_node;
108
109 num_mb = of_count_phandle_with_args(np, "mboxes", "#mbox-cells");
110 num_sh = of_count_phandle_with_args(np, "shmem", NULL);
111 dev_dbg(cdev, "Found %d mboxes and %d shmems !\n", num_mb, num_sh);
112
113 /* Bail out if mboxes and shmem descriptors are inconsistent */
114 if (num_mb <= 0 || num_sh <= 0 || num_sh > 2 || num_mb > 3 ||
115 (num_mb == 1 && num_sh != 1) || (num_mb == 3 && 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 break;
145 case 2:
146 if (num_sh == 2) {
147 *a2p_rx_chan = 0;
148 *p2a_chan = 1;
149 } else {
150 *a2p_rx_chan = 1;
151 *p2a_chan = 0;
152 }
153 break;
154 case 3:
155 *a2p_rx_chan = 1;
156 *p2a_chan = 2;
157 break;
158 }
159 }
160
161 return ret;
162 }
163
mailbox_chan_setup(struct scmi_chan_info * cinfo,struct device * dev,bool tx)164 static int mailbox_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
165 bool tx)
166 {
167 const char *desc = tx ? "Tx" : "Rx";
168 struct device *cdev = cinfo->dev;
169 struct scmi_mailbox *smbox;
170 struct device_node *shmem;
171 int ret, a2p_rx_chan, p2a_chan, idx = tx ? 0 : 1;
172 struct mbox_client *cl;
173 resource_size_t size;
174 struct resource res;
175
176 ret = mailbox_chan_validate(cdev, &a2p_rx_chan, &p2a_chan);
177 if (ret)
178 return ret;
179
180 if (!tx && !p2a_chan)
181 return -ENODEV;
182
183 smbox = devm_kzalloc(dev, sizeof(*smbox), GFP_KERNEL);
184 if (!smbox)
185 return -ENOMEM;
186
187 shmem = of_parse_phandle(cdev->of_node, "shmem", idx);
188 if (!of_device_is_compatible(shmem, "arm,scmi-shmem")) {
189 of_node_put(shmem);
190 return -ENXIO;
191 }
192
193 ret = of_address_to_resource(shmem, 0, &res);
194 of_node_put(shmem);
195 if (ret) {
196 dev_err(cdev, "failed to get SCMI %s shared memory\n", desc);
197 return ret;
198 }
199
200 size = resource_size(&res);
201 smbox->shmem = devm_ioremap(dev, res.start, size);
202 if (!smbox->shmem) {
203 dev_err(dev, "failed to ioremap SCMI %s shared memory\n", desc);
204 return -EADDRNOTAVAIL;
205 }
206
207 cl = &smbox->cl;
208 cl->dev = cdev;
209 cl->tx_prepare = tx ? tx_prepare : NULL;
210 cl->rx_callback = rx_callback;
211 cl->tx_block = false;
212 cl->knows_txdone = tx;
213
214 smbox->chan = mbox_request_channel(cl, tx ? 0 : p2a_chan);
215 if (IS_ERR(smbox->chan)) {
216 ret = PTR_ERR(smbox->chan);
217 if (ret != -EPROBE_DEFER)
218 dev_err(cdev,
219 "failed to request SCMI %s mailbox\n", desc);
220 return ret;
221 }
222
223 /* Additional unidirectional channel for TX if needed */
224 if (tx && a2p_rx_chan) {
225 smbox->chan_receiver = mbox_request_channel(cl, a2p_rx_chan);
226 if (IS_ERR(smbox->chan_receiver)) {
227 ret = PTR_ERR(smbox->chan_receiver);
228 if (ret != -EPROBE_DEFER)
229 dev_err(cdev, "failed to request SCMI Tx Receiver mailbox\n");
230 return ret;
231 }
232 }
233
234 cinfo->transport_info = smbox;
235 smbox->cinfo = cinfo;
236 mutex_init(&smbox->chan_lock);
237
238 return 0;
239 }
240
mailbox_chan_free(int id,void * p,void * data)241 static int mailbox_chan_free(int id, void *p, void *data)
242 {
243 struct scmi_chan_info *cinfo = p;
244 struct scmi_mailbox *smbox = cinfo->transport_info;
245
246 if (smbox && !IS_ERR(smbox->chan)) {
247 mbox_free_channel(smbox->chan);
248 mbox_free_channel(smbox->chan_receiver);
249 cinfo->transport_info = NULL;
250 smbox->chan = NULL;
251 smbox->chan_receiver = NULL;
252 smbox->cinfo = NULL;
253 }
254
255 return 0;
256 }
257
mailbox_send_message(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)258 static int mailbox_send_message(struct scmi_chan_info *cinfo,
259 struct scmi_xfer *xfer)
260 {
261 struct scmi_mailbox *smbox = cinfo->transport_info;
262 int ret;
263
264 /*
265 * The mailbox layer has its own queue. However the mailbox queue
266 * confuses the per message SCMI timeouts since the clock starts when
267 * the message is submitted into the mailbox queue. So when multiple
268 * messages are queued up the clock starts on all messages instead of
269 * only the one inflight.
270 */
271 mutex_lock(&smbox->chan_lock);
272
273 ret = mbox_send_message(smbox->chan, xfer);
274 /* mbox_send_message returns non-negative value on success */
275 if (ret < 0) {
276 mutex_unlock(&smbox->chan_lock);
277 return ret;
278 }
279
280 return 0;
281 }
282
mailbox_mark_txdone(struct scmi_chan_info * cinfo,int ret,struct scmi_xfer * __unused)283 static void mailbox_mark_txdone(struct scmi_chan_info *cinfo, int ret,
284 struct scmi_xfer *__unused)
285 {
286 struct scmi_mailbox *smbox = cinfo->transport_info;
287
288 mbox_client_txdone(smbox->chan, ret);
289
290 /* Release channel */
291 mutex_unlock(&smbox->chan_lock);
292 }
293
mailbox_fetch_response(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)294 static void mailbox_fetch_response(struct scmi_chan_info *cinfo,
295 struct scmi_xfer *xfer)
296 {
297 struct scmi_mailbox *smbox = cinfo->transport_info;
298
299 shmem_fetch_response(smbox->shmem, xfer);
300 }
301
mailbox_fetch_notification(struct scmi_chan_info * cinfo,size_t max_len,struct scmi_xfer * xfer)302 static void mailbox_fetch_notification(struct scmi_chan_info *cinfo,
303 size_t max_len, struct scmi_xfer *xfer)
304 {
305 struct scmi_mailbox *smbox = cinfo->transport_info;
306
307 shmem_fetch_notification(smbox->shmem, max_len, xfer);
308 }
309
mailbox_clear_channel(struct scmi_chan_info * cinfo)310 static void mailbox_clear_channel(struct scmi_chan_info *cinfo)
311 {
312 struct scmi_mailbox *smbox = cinfo->transport_info;
313
314 shmem_clear_channel(smbox->shmem);
315 }
316
317 static bool
mailbox_poll_done(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)318 mailbox_poll_done(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
319 {
320 struct scmi_mailbox *smbox = cinfo->transport_info;
321
322 return shmem_poll_done(smbox->shmem, xfer);
323 }
324
325 static const struct scmi_transport_ops scmi_mailbox_ops = {
326 .chan_available = mailbox_chan_available,
327 .chan_setup = mailbox_chan_setup,
328 .chan_free = mailbox_chan_free,
329 .send_message = mailbox_send_message,
330 .mark_txdone = mailbox_mark_txdone,
331 .fetch_response = mailbox_fetch_response,
332 .fetch_notification = mailbox_fetch_notification,
333 .clear_channel = mailbox_clear_channel,
334 .poll_done = mailbox_poll_done,
335 };
336
337 const struct scmi_desc scmi_mailbox_desc = {
338 .ops = &scmi_mailbox_ops,
339 .max_rx_timeout_ms = 30, /* We may increase this if required */
340 .max_msg = 20, /* Limited by MBOX_TX_QUEUE_LEN */
341 .max_msg_size = 128,
342 };
343