• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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));
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 	ret = of_address_to_resource(shmem, 0, &res);
124 	of_node_put(shmem);
125 	if (ret) {
126 		dev_err(cdev, "failed to get SCMI %s shared memory\n", desc);
127 		return ret;
128 	}
129 
130 	size = resource_size(&res);
131 	smbox->shmem = devm_ioremap(dev, res.start, size);
132 	if (!smbox->shmem) {
133 		dev_err(dev, "failed to ioremap SCMI %s shared memory\n", desc);
134 		return -EADDRNOTAVAIL;
135 	}
136 
137 	cl = &smbox->cl;
138 	cl->dev = cdev;
139 	cl->tx_prepare = tx ? tx_prepare : NULL;
140 	cl->rx_callback = rx_callback;
141 	cl->tx_block = false;
142 	cl->knows_txdone = tx;
143 
144 	smbox->chan = mbox_request_channel(cl, tx ? 0 : 1);
145 	if (IS_ERR(smbox->chan)) {
146 		ret = PTR_ERR(smbox->chan);
147 		if (ret != -EPROBE_DEFER)
148 			dev_err(cdev, "failed to request SCMI %s mailbox\n",
149 				tx ? "Tx" : "Rx");
150 		return ret;
151 	}
152 
153 	cinfo->transport_info = smbox;
154 	smbox->cinfo = cinfo;
155 
156 	return 0;
157 }
158 
mailbox_chan_free(int id,void * p,void * data)159 static int mailbox_chan_free(int id, void *p, void *data)
160 {
161 	struct scmi_chan_info *cinfo = p;
162 	struct scmi_mailbox *smbox = cinfo->transport_info;
163 
164 	if (smbox && !IS_ERR(smbox->chan)) {
165 		mbox_free_channel(smbox->chan);
166 		cinfo->transport_info = NULL;
167 		smbox->chan = NULL;
168 		smbox->cinfo = NULL;
169 	}
170 
171 	scmi_free_channel(cinfo, data, id);
172 
173 	return 0;
174 }
175 
mailbox_send_message(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)176 static int mailbox_send_message(struct scmi_chan_info *cinfo,
177 				struct scmi_xfer *xfer)
178 {
179 	struct scmi_mailbox *smbox = cinfo->transport_info;
180 	int ret;
181 
182 	ret = mbox_send_message(smbox->chan, xfer);
183 
184 	/* mbox_send_message returns non-negative value on success, so reset */
185 	if (ret > 0)
186 		ret = 0;
187 
188 	return ret;
189 }
190 
mailbox_mark_txdone(struct scmi_chan_info * cinfo,int ret)191 static void mailbox_mark_txdone(struct scmi_chan_info *cinfo, int ret)
192 {
193 	struct scmi_mailbox *smbox = cinfo->transport_info;
194 
195 	/*
196 	 * NOTE: we might prefer not to need the mailbox ticker to manage the
197 	 * transfer queueing since the protocol layer queues things by itself.
198 	 * Unfortunately, we have to kick the mailbox framework after we have
199 	 * received our message.
200 	 */
201 	mbox_client_txdone(smbox->chan, ret);
202 }
203 
mailbox_fetch_response(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)204 static void mailbox_fetch_response(struct scmi_chan_info *cinfo,
205 				   struct scmi_xfer *xfer)
206 {
207 	struct scmi_mailbox *smbox = cinfo->transport_info;
208 
209 	shmem_fetch_response(smbox->shmem, xfer);
210 }
211 
mailbox_fetch_notification(struct scmi_chan_info * cinfo,size_t max_len,struct scmi_xfer * xfer)212 static void mailbox_fetch_notification(struct scmi_chan_info *cinfo,
213 				       size_t max_len, struct scmi_xfer *xfer)
214 {
215 	struct scmi_mailbox *smbox = cinfo->transport_info;
216 
217 	shmem_fetch_notification(smbox->shmem, max_len, xfer);
218 }
219 
mailbox_clear_channel(struct scmi_chan_info * cinfo)220 static void mailbox_clear_channel(struct scmi_chan_info *cinfo)
221 {
222 	struct scmi_mailbox *smbox = cinfo->transport_info;
223 
224 	shmem_clear_channel(smbox->shmem);
225 }
226 
227 static bool
mailbox_poll_done(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)228 mailbox_poll_done(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
229 {
230 	struct scmi_mailbox *smbox = cinfo->transport_info;
231 
232 	return shmem_poll_done(smbox->shmem, xfer);
233 }
234 
235 static const struct scmi_transport_ops scmi_mailbox_ops = {
236 	.chan_available = mailbox_chan_available,
237 	.chan_setup = mailbox_chan_setup,
238 	.chan_free = mailbox_chan_free,
239 	.send_message = mailbox_send_message,
240 	.mark_txdone = mailbox_mark_txdone,
241 	.fetch_response = mailbox_fetch_response,
242 	.fetch_notification = mailbox_fetch_notification,
243 	.clear_channel = mailbox_clear_channel,
244 	.poll_done = mailbox_poll_done,
245 };
246 
247 const struct scmi_desc scmi_mailbox_desc = {
248 	.ops = &scmi_mailbox_ops,
249 	.max_rx_timeout_ms = 30, /* We may increase this if required */
250 	.max_msg = 20, /* Limited by MBOX_TX_QUEUE_LEN */
251 	.max_msg_size = 128,
252 };
253