]> Git Repo - linux.git/blob - drivers/firmware/arm_scmi/mailbox.c
x86/kaslr: Expose and use the end of the physical memory address space
[linux.git] / drivers / firmware / arm_scmi / mailbox.c
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
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
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
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  * @p2a_rx_chan: A reference to the optional p2a completion channel.
97  *            Set as zero if not present.
98  *
99  * At first, validate the transport configuration as described in terms of
100  * 'mboxes' and 'shmem', then determin which mailbox channel indexes are
101  * appropriate to be use in the current configuration.
102  *
103  * Return: 0 on Success or error
104  */
105 static int mailbox_chan_validate(struct device *cdev, int *a2p_rx_chan,
106                                  int *p2a_chan, int *p2a_rx_chan)
107 {
108         int num_mb, num_sh, ret = 0;
109         struct device_node *np = cdev->of_node;
110
111         num_mb = of_count_phandle_with_args(np, "mboxes", "#mbox-cells");
112         num_sh = of_count_phandle_with_args(np, "shmem", NULL);
113         dev_dbg(cdev, "Found %d mboxes and %d shmems !\n", num_mb, num_sh);
114
115         /* Bail out if mboxes and shmem descriptors are inconsistent */
116         if (num_mb <= 0 || num_sh <= 0 || num_sh > 2 || num_mb > 4 ||
117             (num_mb == 1 && num_sh != 1) || (num_mb == 3 && num_sh != 2) ||
118             (num_mb == 4 && num_sh != 2)) {
119                 dev_warn(cdev,
120                          "Invalid channel descriptor for '%s' - mbs:%d  shm:%d\n",
121                          of_node_full_name(np), num_mb, num_sh);
122                 return -EINVAL;
123         }
124
125         /* Bail out if provided shmem descriptors do not refer distinct areas  */
126         if (num_sh > 1) {
127                 struct device_node *np_tx, *np_rx;
128
129                 np_tx = of_parse_phandle(np, "shmem", 0);
130                 np_rx = of_parse_phandle(np, "shmem", 1);
131                 if (!np_tx || !np_rx || np_tx == np_rx) {
132                         dev_warn(cdev, "Invalid shmem descriptor for '%s'\n",
133                                  of_node_full_name(np));
134                         ret = -EINVAL;
135                 }
136
137                 of_node_put(np_tx);
138                 of_node_put(np_rx);
139         }
140
141         /* Calculate channels IDs to use depending on mboxes/shmem layout */
142         if (!ret) {
143                 switch (num_mb) {
144                 case 1:
145                         *a2p_rx_chan = 0;
146                         *p2a_chan = 0;
147                         *p2a_rx_chan = 0;
148                         break;
149                 case 2:
150                         if (num_sh == 2) {
151                                 *a2p_rx_chan = 0;
152                                 *p2a_chan = 1;
153                         } else {
154                                 *a2p_rx_chan = 1;
155                                 *p2a_chan = 0;
156                         }
157                         *p2a_rx_chan = 0;
158                         break;
159                 case 3:
160                         *a2p_rx_chan = 1;
161                         *p2a_chan = 2;
162                         *p2a_rx_chan = 0;
163                         break;
164                 case 4:
165                         *a2p_rx_chan = 1;
166                         *p2a_chan = 2;
167                         *p2a_rx_chan = 3;
168                         break;
169                 }
170         }
171
172         return ret;
173 }
174
175 static int mailbox_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
176                               bool tx)
177 {
178         const char *desc = tx ? "Tx" : "Rx";
179         struct device *cdev = cinfo->dev;
180         struct scmi_mailbox *smbox;
181         struct device_node *shmem;
182         int ret, a2p_rx_chan, p2a_chan, p2a_rx_chan, idx = tx ? 0 : 1;
183         struct mbox_client *cl;
184         resource_size_t size;
185         struct resource res;
186
187         ret = mailbox_chan_validate(cdev, &a2p_rx_chan, &p2a_chan, &p2a_rx_chan);
188         if (ret)
189                 return ret;
190
191         if (!tx && !p2a_chan)
192                 return -ENODEV;
193
194         smbox = devm_kzalloc(dev, sizeof(*smbox), GFP_KERNEL);
195         if (!smbox)
196                 return -ENOMEM;
197
198         shmem = of_parse_phandle(cdev->of_node, "shmem", idx);
199         if (!of_device_is_compatible(shmem, "arm,scmi-shmem")) {
200                 of_node_put(shmem);
201                 return -ENXIO;
202         }
203
204         ret = of_address_to_resource(shmem, 0, &res);
205         of_node_put(shmem);
206         if (ret) {
207                 dev_err(cdev, "failed to get SCMI %s shared memory\n", desc);
208                 return ret;
209         }
210
211         size = resource_size(&res);
212         smbox->shmem = devm_ioremap(dev, res.start, size);
213         if (!smbox->shmem) {
214                 dev_err(dev, "failed to ioremap SCMI %s shared memory\n", desc);
215                 return -EADDRNOTAVAIL;
216         }
217
218         cl = &smbox->cl;
219         cl->dev = cdev;
220         cl->tx_prepare = tx ? tx_prepare : NULL;
221         cl->rx_callback = rx_callback;
222         cl->tx_block = false;
223         cl->knows_txdone = tx;
224
225         smbox->chan = mbox_request_channel(cl, tx ? 0 : p2a_chan);
226         if (IS_ERR(smbox->chan)) {
227                 ret = PTR_ERR(smbox->chan);
228                 if (ret != -EPROBE_DEFER)
229                         dev_err(cdev,
230                                 "failed to request SCMI %s mailbox\n", desc);
231                 return ret;
232         }
233
234         /* Additional unidirectional channel for TX if needed */
235         if (tx && a2p_rx_chan) {
236                 smbox->chan_receiver = mbox_request_channel(cl, a2p_rx_chan);
237                 if (IS_ERR(smbox->chan_receiver)) {
238                         ret = PTR_ERR(smbox->chan_receiver);
239                         if (ret != -EPROBE_DEFER)
240                                 dev_err(cdev, "failed to request SCMI Tx Receiver mailbox\n");
241                         return ret;
242                 }
243         }
244
245         if (!tx && p2a_rx_chan) {
246                 smbox->chan_platform_receiver = mbox_request_channel(cl, p2a_rx_chan);
247                 if (IS_ERR(smbox->chan_platform_receiver)) {
248                         ret = PTR_ERR(smbox->chan_platform_receiver);
249                         if (ret != -EPROBE_DEFER)
250                                 dev_err(cdev, "failed to request SCMI P2A Receiver mailbox\n");
251                         return ret;
252                 }
253         }
254
255
256         cinfo->transport_info = smbox;
257         smbox->cinfo = cinfo;
258
259         return 0;
260 }
261
262 static int mailbox_chan_free(int id, void *p, void *data)
263 {
264         struct scmi_chan_info *cinfo = p;
265         struct scmi_mailbox *smbox = cinfo->transport_info;
266
267         if (smbox && !IS_ERR(smbox->chan)) {
268                 mbox_free_channel(smbox->chan);
269                 mbox_free_channel(smbox->chan_receiver);
270                 mbox_free_channel(smbox->chan_platform_receiver);
271                 cinfo->transport_info = NULL;
272                 smbox->chan = NULL;
273                 smbox->chan_receiver = NULL;
274                 smbox->chan_platform_receiver = NULL;
275                 smbox->cinfo = NULL;
276         }
277
278         return 0;
279 }
280
281 static int mailbox_send_message(struct scmi_chan_info *cinfo,
282                                 struct scmi_xfer *xfer)
283 {
284         struct scmi_mailbox *smbox = cinfo->transport_info;
285         int ret;
286
287         ret = mbox_send_message(smbox->chan, xfer);
288
289         /* mbox_send_message returns non-negative value on success, so reset */
290         if (ret > 0)
291                 ret = 0;
292
293         return ret;
294 }
295
296 static void mailbox_mark_txdone(struct scmi_chan_info *cinfo, int ret,
297                                 struct scmi_xfer *__unused)
298 {
299         struct scmi_mailbox *smbox = cinfo->transport_info;
300
301         /*
302          * NOTE: we might prefer not to need the mailbox ticker to manage the
303          * transfer queueing since the protocol layer queues things by itself.
304          * Unfortunately, we have to kick the mailbox framework after we have
305          * received our message.
306          */
307         mbox_client_txdone(smbox->chan, ret);
308 }
309
310 static void mailbox_fetch_response(struct scmi_chan_info *cinfo,
311                                    struct scmi_xfer *xfer)
312 {
313         struct scmi_mailbox *smbox = cinfo->transport_info;
314
315         shmem_fetch_response(smbox->shmem, xfer);
316 }
317
318 static void mailbox_fetch_notification(struct scmi_chan_info *cinfo,
319                                        size_t max_len, struct scmi_xfer *xfer)
320 {
321         struct scmi_mailbox *smbox = cinfo->transport_info;
322
323         shmem_fetch_notification(smbox->shmem, max_len, xfer);
324 }
325
326 static void mailbox_clear_channel(struct scmi_chan_info *cinfo)
327 {
328         struct scmi_mailbox *smbox = cinfo->transport_info;
329         struct mbox_chan *intr_chan;
330         int ret;
331
332         shmem_clear_channel(smbox->shmem);
333
334         if (!shmem_channel_intr_enabled(smbox->shmem))
335                 return;
336
337         if (smbox->chan_platform_receiver)
338                 intr_chan = smbox->chan_platform_receiver;
339         else if (smbox->chan)
340                 intr_chan = smbox->chan;
341         else
342                 return;
343
344         ret = mbox_send_message(intr_chan, 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_chan, ret);
350 }
351
352 static bool
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 };
This page took 0.060474 seconds and 4 git commands to generate.