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