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, smbox->cinfo);
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 scmi_rx_callback(smbox->cinfo, shmem_read_header(smbox->shmem), NULL);
47 }
48
mailbox_chan_available(struct device_node * of_node,int idx)49 static bool mailbox_chan_available(struct device_node *of_node, int idx)
50 {
51 return !of_parse_phandle_with_args(of_node, "mboxes",
52 "#mbox-cells", idx, NULL);
53 }
54
mailbox_chan_setup(struct scmi_chan_info * cinfo,struct device * dev,bool tx)55 static int mailbox_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
56 bool tx)
57 {
58 const char *desc = tx ? "Tx" : "Rx";
59 struct device *cdev = cinfo->dev;
60 struct scmi_mailbox *smbox;
61 struct device_node *shmem;
62 int ret, idx = tx ? 0 : 1;
63 struct mbox_client *cl;
64 resource_size_t size;
65 struct resource res;
66
67 smbox = devm_kzalloc(dev, sizeof(*smbox), GFP_KERNEL);
68 if (!smbox)
69 return -ENOMEM;
70
71 shmem = of_parse_phandle(cdev->of_node, "shmem", idx);
72 if (!of_device_is_compatible(shmem, "arm,scmi-shmem"))
73 return -ENXIO;
74
75 ret = of_address_to_resource(shmem, 0, &res);
76 of_node_put(shmem);
77 if (ret) {
78 dev_err(cdev, "failed to get SCMI %s shared memory\n", desc);
79 return ret;
80 }
81
82 size = resource_size(&res);
83 smbox->shmem = devm_ioremap(dev, res.start, size);
84 if (!smbox->shmem) {
85 dev_err(dev, "failed to ioremap SCMI %s shared memory\n", desc);
86 return -EADDRNOTAVAIL;
87 }
88
89 cl = &smbox->cl;
90 cl->dev = cdev;
91 cl->tx_prepare = tx ? tx_prepare : NULL;
92 cl->rx_callback = rx_callback;
93 cl->tx_block = false;
94 cl->knows_txdone = tx;
95
96 smbox->chan = mbox_request_channel(cl, tx ? 0 : 1);
97 if (IS_ERR(smbox->chan)) {
98 ret = PTR_ERR(smbox->chan);
99 if (ret != -EPROBE_DEFER)
100 dev_err(cdev, "failed to request SCMI %s mailbox\n",
101 tx ? "Tx" : "Rx");
102 return ret;
103 }
104
105 cinfo->transport_info = smbox;
106 smbox->cinfo = cinfo;
107
108 return 0;
109 }
110
mailbox_chan_free(int id,void * p,void * data)111 static int mailbox_chan_free(int id, void *p, void *data)
112 {
113 struct scmi_chan_info *cinfo = p;
114 struct scmi_mailbox *smbox = cinfo->transport_info;
115
116 if (smbox && !IS_ERR(smbox->chan)) {
117 mbox_free_channel(smbox->chan);
118 cinfo->transport_info = NULL;
119 smbox->chan = NULL;
120 smbox->cinfo = NULL;
121 }
122
123 return 0;
124 }
125
mailbox_send_message(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)126 static int mailbox_send_message(struct scmi_chan_info *cinfo,
127 struct scmi_xfer *xfer)
128 {
129 struct scmi_mailbox *smbox = cinfo->transport_info;
130 int ret;
131
132 ret = mbox_send_message(smbox->chan, xfer);
133
134 /* mbox_send_message returns non-negative value on success, so reset */
135 if (ret > 0)
136 ret = 0;
137
138 return ret;
139 }
140
mailbox_mark_txdone(struct scmi_chan_info * cinfo,int ret,struct scmi_xfer * __unused)141 static void mailbox_mark_txdone(struct scmi_chan_info *cinfo, int ret,
142 struct scmi_xfer *__unused)
143 {
144 struct scmi_mailbox *smbox = cinfo->transport_info;
145
146 /*
147 * NOTE: we might prefer not to need the mailbox ticker to manage the
148 * transfer queueing since the protocol layer queues things by itself.
149 * Unfortunately, we have to kick the mailbox framework after we have
150 * received our message.
151 */
152 mbox_client_txdone(smbox->chan, ret);
153 }
154
mailbox_fetch_response(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)155 static void mailbox_fetch_response(struct scmi_chan_info *cinfo,
156 struct scmi_xfer *xfer)
157 {
158 struct scmi_mailbox *smbox = cinfo->transport_info;
159
160 shmem_fetch_response(smbox->shmem, xfer);
161 }
162
mailbox_fetch_notification(struct scmi_chan_info * cinfo,size_t max_len,struct scmi_xfer * xfer)163 static void mailbox_fetch_notification(struct scmi_chan_info *cinfo,
164 size_t max_len, struct scmi_xfer *xfer)
165 {
166 struct scmi_mailbox *smbox = cinfo->transport_info;
167
168 shmem_fetch_notification(smbox->shmem, max_len, xfer);
169 }
170
mailbox_clear_channel(struct scmi_chan_info * cinfo)171 static void mailbox_clear_channel(struct scmi_chan_info *cinfo)
172 {
173 struct scmi_mailbox *smbox = cinfo->transport_info;
174
175 shmem_clear_channel(smbox->shmem);
176 }
177
178 static bool
mailbox_poll_done(struct scmi_chan_info * cinfo,struct scmi_xfer * xfer)179 mailbox_poll_done(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
180 {
181 struct scmi_mailbox *smbox = cinfo->transport_info;
182
183 return shmem_poll_done(smbox->shmem, xfer);
184 }
185
186 static const struct scmi_transport_ops scmi_mailbox_ops = {
187 .chan_available = mailbox_chan_available,
188 .chan_setup = mailbox_chan_setup,
189 .chan_free = mailbox_chan_free,
190 .send_message = mailbox_send_message,
191 .mark_txdone = mailbox_mark_txdone,
192 .fetch_response = mailbox_fetch_response,
193 .fetch_notification = mailbox_fetch_notification,
194 .clear_channel = mailbox_clear_channel,
195 .poll_done = mailbox_poll_done,
196 };
197
198 const struct scmi_desc scmi_mailbox_desc = {
199 .ops = &scmi_mailbox_ops,
200 .max_rx_timeout_ms = 30, /* We may increase this if required */
201 .max_msg = 20, /* Limited by MBOX_TX_QUEUE_LEN */
202 .max_msg_size = 128,
203 };
204