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 		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  *
94  * At first, validate the transport configuration as described in terms of
95  * 'mboxes' and 'shmem', then determin which mailbox channel indexes are
96  * appropriate to be use in the current configuration.
97  *
98  * Return: 0 on Success or error
99  */
mailbox_chan_validate(struct device * cdev,int * a2p_rx_chan,int * p2a_chan)100 static int mailbox_chan_validate(struct device *cdev,
101 				 int *a2p_rx_chan, int *p2a_chan)
102 {
103 	int num_mb, num_sh, ret = 0;
104 	struct device_node *np = cdev->of_node;
105 
106 	num_mb = of_count_phandle_with_args(np, "mboxes", "#mbox-cells");
107 	num_sh = of_count_phandle_with_args(np, "shmem", NULL);
108 	dev_dbg(cdev, "Found %d mboxes and %d shmems !\n", num_mb, num_sh);
109 
110 	/* Bail out if mboxes and shmem descriptors are inconsistent */
111 	if (num_mb <= 0 || num_sh <= 0 || num_sh > 2 || num_mb > 3 ||
112 	    (num_mb == 1 && num_sh != 1) || (num_mb == 3 && num_sh != 2)) {
113 		dev_warn(cdev,
114 			 "Invalid channel descriptor for '%s' - mbs:%d  shm:%d\n",
115 			 of_node_full_name(np), num_mb, num_sh);
116 		return -EINVAL;
117 	}
118 
119 	/* Bail out if provided shmem descriptors do not refer distinct areas  */
120 	if (num_sh > 1) {
121 		struct device_node *np_tx, *np_rx;
122 
123 		np_tx = of_parse_phandle(np, "shmem", 0);
124 		np_rx = of_parse_phandle(np, "shmem", 1);
125 		if (!np_tx || !np_rx || np_tx == np_rx) {
126 			dev_warn(cdev, "Invalid shmem descriptor for '%s'\n",
127 				 of_node_full_name(np));
128 			ret = -EINVAL;
129 		}
130 
131 		of_node_put(np_tx);
132 		of_node_put(np_rx);
133 	}
134 
135 	/* Calculate channels IDs to use depending on mboxes/shmem layout */
136 	if (!ret) {
137 		switch (num_mb) {
138 		case 1:
139 			*a2p_rx_chan = 0;
140 			*p2a_chan = 0;
141 			break;
142 		case 2:
143 			if (num_sh == 2) {
144 				*a2p_rx_chan = 0;
145 				*p2a_chan = 1;
146 			} else {
147 				*a2p_rx_chan = 1;
148 				*p2a_chan = 0;
149 			}
150 			break;
151 		case 3:
152 			*a2p_rx_chan = 1;
153 			*p2a_chan = 2;
154 			break;
155 		}
156 	}
157 
158 	return ret;
159 }
160 
mailbox_chan_setup(struct scmi_chan_info * cinfo,struct device * dev,bool tx)161 static int mailbox_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
162 			      bool tx)
163 {
164 	const char *desc = tx ? "Tx" : "Rx";
165 	struct device *cdev = cinfo->dev;
166 	struct scmi_mailbox *smbox;
167 	struct device_node *shmem;
168 	int ret, a2p_rx_chan, p2a_chan, idx = tx ? 0 : 1;
169 	struct mbox_client *cl;
170 	resource_size_t size;
171 	struct resource res;
172 
173 	ret = mailbox_chan_validate(cdev, &a2p_rx_chan, &p2a_chan);
174 	if (ret)
175 		return ret;
176 
177 	if (!tx && !p2a_chan)
178 		return -ENODEV;
179 
180 	smbox = devm_kzalloc(dev, sizeof(*smbox), GFP_KERNEL);
181 	if (!smbox)
182 		return -ENOMEM;
183 
184 	shmem = of_parse_phandle(cdev->of_node, "shmem", idx);
185 	if (!of_device_is_compatible(shmem, "arm,scmi-shmem")) {
186 		of_node_put(shmem);
187 		return -ENXIO;
188 	}
189 
190 	ret = of_address_to_resource(shmem, 0, &res);
191 	of_node_put(shmem);
192 	if (ret) {
193 		dev_err(cdev, "failed to get SCMI %s shared memory\n", desc);
194 		return ret;
195 	}
196 
197 	size = resource_size(&res);
198 	smbox->shmem = devm_ioremap(dev, res.start, size);
199 	if (!smbox->shmem) {
200 		dev_err(dev, "failed to ioremap SCMI %s shared memory\n", desc);
201 		return -EADDRNOTAVAIL;
202 	}
203 
204 	cl = &smbox->cl;
205 	cl->dev = cdev;
206 	cl->tx_prepare = tx ? tx_prepare : NULL;
207 	cl->rx_callback = rx_callback;
208 	cl->tx_block = false;
209 	cl->knows_txdone = tx;
210 
211 	smbox->chan = mbox_request_channel(cl, tx ? 0 : p2a_chan);
212 	if (IS_ERR(smbox->chan)) {
213 		ret = PTR_ERR(smbox->chan);
214 		if (ret != -EPROBE_DEFER)
215 			dev_err(cdev,
216 				"failed to request SCMI %s mailbox\n", desc);
217 		return ret;
218 	}
219 
220 	/* Additional unidirectional channel for TX if needed */
221 	if (tx && a2p_rx_chan) {
222 		smbox->chan_receiver = mbox_request_channel(cl, a2p_rx_chan);
223 		if (IS_ERR(smbox->chan_receiver)) {
224 			ret = PTR_ERR(smbox->chan_receiver);
225 			if (ret != -EPROBE_DEFER)
226 				dev_err(cdev, "failed to request SCMI Tx Receiver mailbox\n");
227 			return ret;
228 		}
229 	}
230 
231 	cinfo->transport_info = smbox;
232 	smbox->cinfo = cinfo;
233 	mutex_init(&smbox->chan_lock);
234 
235 	return 0;
236 }
237 
mailbox_chan_free(int id,void * p,void * data)238 static int mailbox_chan_free(int id, void *p, void *data)
239 {
240 	struct scmi_chan_info *cinfo = p;
241 	struct scmi_mailbox *smbox = cinfo->transport_info;
242 
243 	if (smbox && !IS_ERR(smbox->chan)) {
244 		mbox_free_channel(smbox->chan);
245 		mbox_free_channel(smbox->chan_receiver);
246 		cinfo->transport_info = NULL;
247 		smbox->chan = NULL;
248 		smbox->chan_receiver = NULL;
249 		smbox->cinfo = NULL;
250 	}
251 
252 	return 0;
253 }
254 
mailbox_send_message(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)255 static int mailbox_send_message(struct scmi_chan_info *cinfo,
256 				struct scmi_xfer *xfer)
257 {
258 	struct scmi_mailbox *smbox = cinfo->transport_info;
259 	int ret;
260 
261 	/*
262 	 * The mailbox layer has its own queue. However the mailbox queue
263 	 * confuses the per message SCMI timeouts since the clock starts when
264 	 * the message is submitted into the mailbox queue. So when multiple
265 	 * messages are queued up the clock starts on all messages instead of
266 	 * only the one inflight.
267 	 */
268 	mutex_lock(&smbox->chan_lock);
269 
270 	ret = mbox_send_message(smbox->chan, xfer);
271 	/* mbox_send_message returns non-negative value on success */
272 	if (ret < 0) {
273 		mutex_unlock(&smbox->chan_lock);
274 		return ret;
275 	}
276 
277 	return 0;
278 }
279 
mailbox_mark_txdone(struct scmi_chan_info * cinfo,int ret,struct scmi_xfer * __unused)280 static void mailbox_mark_txdone(struct scmi_chan_info *cinfo, int ret,
281 				struct scmi_xfer *__unused)
282 {
283 	struct scmi_mailbox *smbox = cinfo->transport_info;
284 
285 	mbox_client_txdone(smbox->chan, ret);
286 
287 	/* Release channel */
288 	mutex_unlock(&smbox->chan_lock);
289 }
290 
mailbox_fetch_response(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)291 static void mailbox_fetch_response(struct scmi_chan_info *cinfo,
292 				   struct scmi_xfer *xfer)
293 {
294 	struct scmi_mailbox *smbox = cinfo->transport_info;
295 
296 	shmem_fetch_response(smbox->shmem, xfer);
297 }
298 
mailbox_fetch_notification(struct scmi_chan_info * cinfo,size_t max_len,struct scmi_xfer * xfer)299 static void mailbox_fetch_notification(struct scmi_chan_info *cinfo,
300 				       size_t max_len, struct scmi_xfer *xfer)
301 {
302 	struct scmi_mailbox *smbox = cinfo->transport_info;
303 
304 	shmem_fetch_notification(smbox->shmem, max_len, xfer);
305 }
306 
mailbox_clear_channel(struct scmi_chan_info * cinfo)307 static void mailbox_clear_channel(struct scmi_chan_info *cinfo)
308 {
309 	struct scmi_mailbox *smbox = cinfo->transport_info;
310 
311 	shmem_clear_channel(smbox->shmem);
312 }
313 
314 static bool
mailbox_poll_done(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)315 mailbox_poll_done(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
316 {
317 	struct scmi_mailbox *smbox = cinfo->transport_info;
318 
319 	return shmem_poll_done(smbox->shmem, xfer);
320 }
321 
322 static const struct scmi_transport_ops scmi_mailbox_ops = {
323 	.chan_available = mailbox_chan_available,
324 	.chan_setup = mailbox_chan_setup,
325 	.chan_free = mailbox_chan_free,
326 	.send_message = mailbox_send_message,
327 	.mark_txdone = mailbox_mark_txdone,
328 	.fetch_response = mailbox_fetch_response,
329 	.fetch_notification = mailbox_fetch_notification,
330 	.clear_channel = mailbox_clear_channel,
331 	.poll_done = mailbox_poll_done,
332 };
333 
334 const struct scmi_desc scmi_mailbox_desc = {
335 	.ops = &scmi_mailbox_ops,
336 	.max_rx_timeout_ms = 30, /* We may increase this if required */
337 	.max_msg = 20, /* Limited by MBOX_TX_QUEUE_LEN */
338 	.max_msg_size = 128,
339 };
340