2 * Copyright (c) 2016, Linaro Ltd
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <linux/module.h>
17 #include <linux/of_address.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/slab.h>
22 #include <linux/rpmsg.h>
23 #include <linux/idr.h>
24 #include <linux/circ_buf.h>
25 #include <linux/soc/qcom/smem.h>
26 #include <linux/sizes.h>
27 #include <linux/delay.h>
28 #include <linux/regmap.h>
29 #include <linux/workqueue.h>
30 #include <linux/list.h>
32 #include <linux/rpmsg/qcom_glink.h>
34 #include "qcom_glink_native.h"
36 #define FIFO_FULL_RESERVE 8
37 #define FIFO_ALIGNMENT 8
38 #define TX_BLOCKED_CMD_RESERVE 8 /* size of struct read_notif_request */
40 #define SMEM_GLINK_NATIVE_XPRT_DESCRIPTOR 478
41 #define SMEM_GLINK_NATIVE_XPRT_FIFO_0 479
42 #define SMEM_GLINK_NATIVE_XPRT_FIFO_1 480
44 struct glink_smem_pipe
{
45 struct qcom_glink_pipe native
;
55 #define to_smem_pipe(p) container_of(p, struct glink_smem_pipe, native)
57 static size_t glink_smem_rx_avail(struct qcom_glink_pipe
*np
)
59 struct glink_smem_pipe
*pipe
= to_smem_pipe(np
);
66 fifo
= qcom_smem_get(pipe
->remote_pid
,
67 SMEM_GLINK_NATIVE_XPRT_FIFO_1
, &len
);
69 pr_err("failed to acquire RX fifo handle: %ld\n",
75 pipe
->native
.length
= len
;
78 head
= le32_to_cpu(*pipe
->head
);
79 tail
= le32_to_cpu(*pipe
->tail
);
82 return pipe
->native
.length
- tail
+ head
;
87 static void glink_smem_rx_peak(struct qcom_glink_pipe
*np
,
88 void *data
, unsigned int offset
, size_t count
)
90 struct glink_smem_pipe
*pipe
= to_smem_pipe(np
);
94 tail
= le32_to_cpu(*pipe
->tail
);
96 if (tail
>= pipe
->native
.length
)
97 tail
-= pipe
->native
.length
;
99 len
= min_t(size_t, count
, pipe
->native
.length
- tail
);
101 __ioread32_copy(data
, pipe
->fifo
+ tail
,
106 __ioread32_copy(data
+ len
, pipe
->fifo
,
107 (count
- len
) / sizeof(u32
));
111 static void glink_smem_rx_advance(struct qcom_glink_pipe
*np
,
114 struct glink_smem_pipe
*pipe
= to_smem_pipe(np
);
117 tail
= le32_to_cpu(*pipe
->tail
);
120 if (tail
> pipe
->native
.length
)
121 tail
-= pipe
->native
.length
;
123 *pipe
->tail
= cpu_to_le32(tail
);
126 static size_t glink_smem_tx_avail(struct qcom_glink_pipe
*np
)
128 struct glink_smem_pipe
*pipe
= to_smem_pipe(np
);
133 head
= le32_to_cpu(*pipe
->head
);
134 tail
= le32_to_cpu(*pipe
->tail
);
137 avail
= pipe
->native
.length
- head
+ tail
;
141 if (avail
< (FIFO_FULL_RESERVE
+ TX_BLOCKED_CMD_RESERVE
))
144 avail
-= FIFO_FULL_RESERVE
+ TX_BLOCKED_CMD_RESERVE
;
149 static unsigned int glink_smem_tx_write_one(struct glink_smem_pipe
*pipe
,
151 const void *data
, size_t count
)
155 len
= min_t(size_t, count
, pipe
->native
.length
- head
);
157 memcpy(pipe
->fifo
+ head
, data
, len
);
160 memcpy(pipe
->fifo
, data
+ len
, count
- len
);
163 if (head
>= pipe
->native
.length
)
164 head
-= pipe
->native
.length
;
169 static void glink_smem_tx_write(struct qcom_glink_pipe
*glink_pipe
,
170 const void *hdr
, size_t hlen
,
171 const void *data
, size_t dlen
)
173 struct glink_smem_pipe
*pipe
= to_smem_pipe(glink_pipe
);
176 head
= le32_to_cpu(*pipe
->head
);
178 head
= glink_smem_tx_write_one(pipe
, head
, hdr
, hlen
);
179 head
= glink_smem_tx_write_one(pipe
, head
, data
, dlen
);
181 /* Ensure head is always aligned to 8 bytes */
182 head
= ALIGN(head
, 8);
183 if (head
>= pipe
->native
.length
)
184 head
-= pipe
->native
.length
;
186 /* Ensure ordering of fifo and head update */
189 *pipe
->head
= cpu_to_le32(head
);
192 static void qcom_glink_smem_release(struct device
*dev
)
197 struct qcom_glink
*qcom_glink_smem_register(struct device
*parent
,
198 struct device_node
*node
)
200 struct glink_smem_pipe
*rx_pipe
;
201 struct glink_smem_pipe
*tx_pipe
;
202 struct qcom_glink
*glink
;
209 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
211 return ERR_PTR(-ENOMEM
);
213 dev
->parent
= parent
;
215 dev
->release
= qcom_glink_smem_release
;
216 dev_set_name(dev
, "%s:%s", node
->parent
->name
, node
->name
);
217 ret
= device_register(dev
);
219 pr_err("failed to register glink edge\n");
223 ret
= of_property_read_u32(dev
->of_node
, "qcom,remote-pid",
226 dev_err(dev
, "failed to parse qcom,remote-pid\n");
230 rx_pipe
= devm_kzalloc(dev
, sizeof(*rx_pipe
), GFP_KERNEL
);
231 tx_pipe
= devm_kzalloc(dev
, sizeof(*tx_pipe
), GFP_KERNEL
);
232 if (!rx_pipe
|| !tx_pipe
) {
237 ret
= qcom_smem_alloc(remote_pid
,
238 SMEM_GLINK_NATIVE_XPRT_DESCRIPTOR
, 32);
239 if (ret
&& ret
!= -EEXIST
) {
240 dev_err(dev
, "failed to allocate glink descriptors\n");
244 descs
= qcom_smem_get(remote_pid
,
245 SMEM_GLINK_NATIVE_XPRT_DESCRIPTOR
, &size
);
247 dev_err(dev
, "failed to acquire xprt descriptor\n");
248 ret
= PTR_ERR(descs
);
253 dev_err(dev
, "glink descriptor of invalid size\n");
258 tx_pipe
->tail
= &descs
[0];
259 tx_pipe
->head
= &descs
[1];
260 rx_pipe
->tail
= &descs
[2];
261 rx_pipe
->head
= &descs
[3];
263 ret
= qcom_smem_alloc(remote_pid
, SMEM_GLINK_NATIVE_XPRT_FIFO_0
,
265 if (ret
&& ret
!= -EEXIST
) {
266 dev_err(dev
, "failed to allocate TX fifo\n");
270 tx_pipe
->fifo
= qcom_smem_get(remote_pid
, SMEM_GLINK_NATIVE_XPRT_FIFO_0
,
271 &tx_pipe
->native
.length
);
272 if (IS_ERR(tx_pipe
->fifo
)) {
273 dev_err(dev
, "failed to acquire TX fifo\n");
274 ret
= PTR_ERR(tx_pipe
->fifo
);
278 rx_pipe
->native
.avail
= glink_smem_rx_avail
;
279 rx_pipe
->native
.peak
= glink_smem_rx_peak
;
280 rx_pipe
->native
.advance
= glink_smem_rx_advance
;
281 rx_pipe
->remote_pid
= remote_pid
;
283 tx_pipe
->native
.avail
= glink_smem_tx_avail
;
284 tx_pipe
->native
.write
= glink_smem_tx_write
;
285 tx_pipe
->remote_pid
= remote_pid
;
290 glink
= qcom_glink_native_probe(dev
,
291 GLINK_FEATURE_INTENT_REUSE
,
292 &rx_pipe
->native
, &tx_pipe
->native
,
295 ret
= PTR_ERR(glink
);
306 EXPORT_SYMBOL_GPL(qcom_glink_smem_register
);
308 void qcom_glink_smem_unregister(struct qcom_glink
*glink
)
310 qcom_glink_native_remove(glink
);
311 qcom_glink_native_unregister(glink
);
313 EXPORT_SYMBOL_GPL(qcom_glink_smem_unregister
);
315 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@linaro.org>");
316 MODULE_DESCRIPTION("Qualcomm GLINK SMEM driver");
317 MODULE_LICENSE("GPL v2");