From d9c21e102c9a12df24e54a1b4358ac29ffb4f1e2 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Fri, 15 Mar 2024 14:22:38 -0500 Subject: [PATCH 1/3] rpmsg_virtio: Do not make assumptions on struct member order In rpmsg_virtio_ns_callback() we directly case from rpmsg_device* to rpmsg_virtio_device*. This only works if rdev is the first member. Use metal_container_of() to remove this assumption on struct order. Signed-off-by: Andrew Davis --- lib/rpmsg/rpmsg_virtio.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/rpmsg/rpmsg_virtio.c b/lib/rpmsg/rpmsg_virtio.c index 66623e54b..15502cf44 100644 --- a/lib/rpmsg/rpmsg_virtio.c +++ b/lib/rpmsg/rpmsg_virtio.c @@ -639,7 +639,9 @@ static int rpmsg_virtio_ns_callback(struct rpmsg_endpoint *ept, void *data, size_t len, uint32_t src, void *priv) { struct rpmsg_device *rdev = ept->rdev; - struct rpmsg_virtio_device *rvdev = (struct rpmsg_virtio_device *)rdev; + struct rpmsg_virtio_device *rvdev = metal_container_of(rdev, + struct rpmsg_virtio_device, + rdev); struct metal_io_region *io = rvdev->shbuf_io; struct rpmsg_endpoint *_ept; struct rpmsg_ns_msg *ns_msg; From 5173e8943f5eb401d5bdb8ae17381b24ae0b79c5 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Fri, 15 Mar 2024 14:38:17 -0500 Subject: [PATCH 2/3] rpmsg: Allow private endpoint data to be set in rpmsg_register_endpoint There is a private data member in the endpoint struct that is passed into the callback, but there is no way to populate it. Add this to the endpoint register function. Signed-off-by: Andrew Davis --- lib/rpmsg/rpmsg.c | 6 ++++-- lib/rpmsg/rpmsg_internal.h | 3 ++- lib/rpmsg/rpmsg_virtio.c | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/rpmsg/rpmsg.c b/lib/rpmsg/rpmsg.c index 3be947d3c..fa2b9d356 100644 --- a/lib/rpmsg/rpmsg.c +++ b/lib/rpmsg/rpmsg.c @@ -273,7 +273,8 @@ void rpmsg_register_endpoint(struct rpmsg_device *rdev, const char *name, uint32_t src, uint32_t dest, rpmsg_ept_cb cb, - rpmsg_ns_unbind_cb ns_unbind_cb) + rpmsg_ns_unbind_cb ns_unbind_cb, + void *priv) { strncpy(ept->name, name ? name : "", sizeof(ept->name)); ept->refcnt = 1; @@ -281,6 +282,7 @@ void rpmsg_register_endpoint(struct rpmsg_device *rdev, ept->dest_addr = dest; ept->cb = cb; ept->ns_unbind_cb = ns_unbind_cb; + ept->priv = priv; ept->rdev = rdev; metal_list_add_tail(&rdev->endpoints, &ept->node); } @@ -323,7 +325,7 @@ int rpmsg_create_ept(struct rpmsg_endpoint *ept, struct rpmsg_device *rdev, */ } - rpmsg_register_endpoint(rdev, ept, name, addr, dest, cb, unbind_cb); + rpmsg_register_endpoint(rdev, ept, name, addr, dest, cb, unbind_cb, NULL); metal_mutex_release(&rdev->lock); /* Send NS announcement to remote processor */ diff --git a/lib/rpmsg/rpmsg_internal.h b/lib/rpmsg/rpmsg_internal.h index f1c61c5f2..51fb5ed3a 100644 --- a/lib/rpmsg/rpmsg_internal.h +++ b/lib/rpmsg/rpmsg_internal.h @@ -103,7 +103,8 @@ void rpmsg_register_endpoint(struct rpmsg_device *rdev, const char *name, uint32_t src, uint32_t dest, rpmsg_ept_cb cb, - rpmsg_ns_unbind_cb ns_unbind_cb); + rpmsg_ns_unbind_cb ns_unbind_cb, + void *priv); static inline struct rpmsg_endpoint * rpmsg_get_ept_from_addr(struct rpmsg_device *rdev, uint32_t addr) diff --git a/lib/rpmsg/rpmsg_virtio.c b/lib/rpmsg/rpmsg_virtio.c index 15502cf44..3f0a66551 100644 --- a/lib/rpmsg/rpmsg_virtio.c +++ b/lib/rpmsg/rpmsg_virtio.c @@ -966,7 +966,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev, if (rdev->support_ns) { rpmsg_register_endpoint(rdev, &rdev->ns_ept, "NS", RPMSG_NS_EPT_ADDR, RPMSG_NS_EPT_ADDR, - rpmsg_virtio_ns_callback, NULL); + rpmsg_virtio_ns_callback, NULL, NULL); } #ifndef VIRTIO_DEVICE_ONLY From 87651c2e0a49d8a6a0cca6191daa990e4e622fab Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Thu, 14 Mar 2024 16:28:00 -0500 Subject: [PATCH 3/3] rpmsg_virtio: Use priv data in name service callback The callback allows us to add private data. Use that to store our rpmsg_device instance instead of extracting it from the endpoint struct. The contents of that struct are internal to the RPMsg layer. Signed-off-by: Andrew Davis --- lib/rpmsg/rpmsg_virtio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/rpmsg/rpmsg_virtio.c b/lib/rpmsg/rpmsg_virtio.c index 3f0a66551..77231dd47 100644 --- a/lib/rpmsg/rpmsg_virtio.c +++ b/lib/rpmsg/rpmsg_virtio.c @@ -638,7 +638,7 @@ static void rpmsg_virtio_rx_callback(struct virtqueue *vq) static int rpmsg_virtio_ns_callback(struct rpmsg_endpoint *ept, void *data, size_t len, uint32_t src, void *priv) { - struct rpmsg_device *rdev = ept->rdev; + struct rpmsg_device *rdev = priv; struct rpmsg_virtio_device *rvdev = metal_container_of(rdev, struct rpmsg_virtio_device, rdev); @@ -649,7 +649,7 @@ static int rpmsg_virtio_ns_callback(struct rpmsg_endpoint *ept, void *data, bool ept_to_release; char name[RPMSG_NAME_SIZE]; - (void)priv; + (void)ept; (void)src; ns_msg = data; @@ -966,7 +966,7 @@ int rpmsg_init_vdev_with_config(struct rpmsg_virtio_device *rvdev, if (rdev->support_ns) { rpmsg_register_endpoint(rdev, &rdev->ns_ept, "NS", RPMSG_NS_EPT_ADDR, RPMSG_NS_EPT_ADDR, - rpmsg_virtio_ns_callback, NULL, NULL); + rpmsg_virtio_ns_callback, NULL, rvdev); } #ifndef VIRTIO_DEVICE_ONLY