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