forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/drivers/rpmsg/virtio_rpmsg_bus.c
....@@ -11,21 +11,22 @@
1111
1212 #define pr_fmt(fmt) "%s: " fmt, __func__
1313
14
-#include <linux/kernel.h>
15
-#include <linux/module.h>
16
-#include <linux/virtio.h>
17
-#include <linux/virtio_ids.h>
18
-#include <linux/virtio_config.h>
19
-#include <linux/scatterlist.h>
2014 #include <linux/dma-mapping.h>
21
-#include <linux/slab.h>
2215 #include <linux/idr.h>
2316 #include <linux/jiffies.h>
24
-#include <linux/sched.h>
25
-#include <linux/wait.h>
26
-#include <linux/rpmsg.h>
17
+#include <linux/kernel.h>
18
+#include <linux/module.h>
2719 #include <linux/mutex.h>
2820 #include <linux/of_device.h>
21
+#include <linux/rpmsg.h>
22
+#include <linux/scatterlist.h>
23
+#include <linux/slab.h>
24
+#include <linux/sched.h>
25
+#include <linux/virtio.h>
26
+#include <linux/virtio_byteorder.h>
27
+#include <linux/virtio_ids.h>
28
+#include <linux/virtio_config.h>
29
+#include <linux/wait.h>
2930
3031 #include "rpmsg_internal.h"
3132
....@@ -84,12 +85,12 @@
8485 * Every message sent(/received) on the rpmsg bus begins with this header.
8586 */
8687 struct rpmsg_hdr {
87
- u32 src;
88
- u32 dst;
89
- u32 reserved;
90
- u16 len;
91
- u16 flags;
92
- u8 data[0];
88
+ __virtio32 src;
89
+ __virtio32 dst;
90
+ __virtio32 reserved;
91
+ __virtio16 len;
92
+ __virtio16 flags;
93
+ u8 data[];
9394 } __packed;
9495
9596 /**
....@@ -106,8 +107,8 @@
106107 */
107108 struct rpmsg_ns_msg {
108109 char name[RPMSG_NAME_SIZE];
109
- u32 addr;
110
- u32 flags;
110
+ __virtio32 addr;
111
+ __virtio32 flags;
111112 } __packed;
112113
113114 /**
....@@ -122,7 +123,12 @@
122123 };
123124
124125 /**
125
- * @vrp: the remote processor this channel belongs to
126
+ * struct virtio_rpmsg_channel - rpmsg channel descriptor
127
+ * @rpdev: the rpmsg channel device
128
+ * @vrp: the virtio remote processor device this channel belongs to
129
+ *
130
+ * This structure stores the channel that links the rpmsg device to the virtio
131
+ * remote processor device.
126132 */
127133 struct virtio_rpmsg_channel {
128134 struct rpmsg_device rpdev;
....@@ -335,8 +341,8 @@
335341 struct rpmsg_ns_msg nsm;
336342
337343 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
338
- nsm.addr = rpdev->ept->addr;
339
- nsm.flags = RPMSG_NS_CREATE;
344
+ nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
345
+ nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
340346
341347 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
342348 if (err)
....@@ -359,8 +365,8 @@
359365 struct rpmsg_ns_msg nsm;
360366
361367 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
362
- nsm.addr = rpdev->ept->addr;
363
- nsm.flags = RPMSG_NS_DESTROY;
368
+ nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
369
+ nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
364370
365371 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
366372 if (err)
....@@ -612,18 +618,18 @@
612618 }
613619 }
614620
615
- msg->len = len;
621
+ msg->len = cpu_to_virtio16(vrp->vdev, len);
616622 msg->flags = 0;
617
- msg->src = src;
618
- msg->dst = dst;
623
+ msg->src = cpu_to_virtio32(vrp->vdev, src);
624
+ msg->dst = cpu_to_virtio32(vrp->vdev, dst);
619625 msg->reserved = 0;
620626 memcpy(msg->data, data, len);
621627
622628 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
623
- msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
629
+ src, dst, len, msg->flags, msg->reserved);
624630 #if defined(CONFIG_DYNAMIC_DEBUG)
625631 dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
626
- msg, sizeof(*msg) + msg->len, true);
632
+ msg, sizeof(*msg) + len, true);
627633 #endif
628634
629635 rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
....@@ -704,13 +710,17 @@
704710 {
705711 struct rpmsg_endpoint *ept;
706712 struct scatterlist sg;
713
+ unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
707714 int err;
708715
709716 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
710
- msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
717
+ virtio32_to_cpu(vrp->vdev, msg->src),
718
+ virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
719
+ virtio16_to_cpu(vrp->vdev, msg->flags),
720
+ virtio32_to_cpu(vrp->vdev, msg->reserved));
711721 #if defined(CONFIG_DYNAMIC_DEBUG)
712722 dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
713
- msg, sizeof(*msg) + msg->len, true);
723
+ msg, sizeof(*msg) + msg_len, true);
714724 #endif
715725
716726 /*
....@@ -718,15 +728,15 @@
718728 * the reported payload length.
719729 */
720730 if (len > vrp->buf_size ||
721
- msg->len > (len - sizeof(struct rpmsg_hdr))) {
722
- dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
731
+ msg_len > (len - sizeof(struct rpmsg_hdr))) {
732
+ dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
723733 return -EINVAL;
724734 }
725735
726736 /* use the dst addr to fetch the callback of the appropriate user */
727737 mutex_lock(&vrp->endpoints_lock);
728738
729
- ept = idr_find(&vrp->endpoints, msg->dst);
739
+ ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
730740
731741 /* let's make sure no one deallocates ept while we use it */
732742 if (ept)
....@@ -739,8 +749,8 @@
739749 mutex_lock(&ept->cb_lock);
740750
741751 if (ept->cb)
742
- ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
743
- msg->src);
752
+ ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
753
+ virtio32_to_cpu(vrp->vdev, msg->src));
744754
745755 mutex_unlock(&ept->cb_lock);
746756
....@@ -846,15 +856,15 @@
846856 /* don't trust the remote processor for null terminating the name */
847857 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
848858
849
- dev_info(dev, "%sing channel %s addr 0x%x\n",
850
- msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
851
- msg->name, msg->addr);
852
-
853859 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
854860 chinfo.src = RPMSG_ADDR_ANY;
855
- chinfo.dst = msg->addr;
861
+ chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
856862
857
- if (msg->flags & RPMSG_NS_DESTROY) {
863
+ dev_info(dev, "%sing channel %s addr 0x%x\n",
864
+ virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
865
+ "destroy" : "creat", msg->name, chinfo.dst);
866
+
867
+ if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
858868 ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
859869 if (ret)
860870 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
....@@ -920,7 +930,7 @@
920930 goto vqs_del;
921931 }
922932
923
- dev_dbg(&vdev->dev, "buffers: va %p, dma %pad\n",
933
+ dev_dbg(&vdev->dev, "buffers: va %pK, dma %pad\n",
924934 bufs_va, &vrp->bufs_dma);
925935
926936 /* half of the buffers is dedicated for RX */