This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 11c71e16384 drivers/rpmsg: support rpmsg_alloc_buf() on the virtio
device side
11c71e16384 is described below
commit 11c71e16384f08db940acde2838494bd07827200
Author: yintao <[email protected]>
AuthorDate: Wed Nov 26 16:54:20 2025 +0800
drivers/rpmsg: support rpmsg_alloc_buf() on the virtio device side
rpmsg_alloc_buf() on rpmsg_virtio only worked on the driver side, which
owns the shared memory: the device side has no heap of its own and got
NULL.
Add CONFIG_RPMSG_HEAP. Each rpmsg device then creates an "rpmsg-heap"
endpoint. The device side forwards rpmsg_alloc_buf() and
rpmsg_free_buf() to the driver side over this endpoint, which
allocates from or frees to the shared memory and returns the physical
address. Both sides of a link must enable the option.
Signed-off-by: yintao <[email protected]>
Signed-off-by: Bowen Wang <[email protected]>
---
drivers/rpmsg/CMakeLists.txt | 4 +
drivers/rpmsg/Kconfig | 7 ++
drivers/rpmsg/Make.defs | 4 +
drivers/rpmsg/rpmsg.c | 7 ++
drivers/rpmsg/rpmsg.h | 15 +++
drivers/rpmsg/rpmsg_heap.c | 258 +++++++++++++++++++++++++++++++++++++++++++
drivers/rpmsg/rpmsg_virtio.c | 13 ++-
7 files changed, 306 insertions(+), 2 deletions(-)
diff --git a/drivers/rpmsg/CMakeLists.txt b/drivers/rpmsg/CMakeLists.txt
index 5b5bb163515..46c99e0777b 100644
--- a/drivers/rpmsg/CMakeLists.txt
+++ b/drivers/rpmsg/CMakeLists.txt
@@ -41,6 +41,10 @@ if(CONFIG_RPMSG)
list(APPEND SRCS rpmsg_ping.c)
endif()
+ if(CONFIG_RPMSG_HEAP)
+ list(APPEND SRCS rpmsg_heap.c)
+ endif()
+
if(CONFIG_RPMSG_PORT)
list(APPEND SRCS rpmsg_port.c)
endif()
diff --git a/drivers/rpmsg/Kconfig b/drivers/rpmsg/Kconfig
index 615d4b38d35..601902be67a 100644
--- a/drivers/rpmsg/Kconfig
+++ b/drivers/rpmsg/Kconfig
@@ -117,6 +117,13 @@ config RPMSG_WAKELOCK
RPMSGIOC_ACQUIRE_WAKELOCK and RPMSGIOC_RELEASE_WAKELOCK ioctls.
Both sides of the link must enable it.
+config RPMSG_HEAP
+ bool "rpmsg heap manage support"
+ default n
+ ---help---
+ This is for rpmsg heap manage, it can be used to allocate/free
+ shm for slave.
+
config RPMSG_PANIC_DUMP_LEVEL
int "rpmsg panic dump level"
default 1 if DEFAULT_SMALL
diff --git a/drivers/rpmsg/Make.defs b/drivers/rpmsg/Make.defs
index 397f1bc64ed..f61a6dfa643 100644
--- a/drivers/rpmsg/Make.defs
+++ b/drivers/rpmsg/Make.defs
@@ -46,6 +46,10 @@ ifeq ($(CONFIG_RPMSG_TEST),y)
CSRCS += rpmsg_test.c
endif
+ifeq ($(CONFIG_RPMSG_HEAP),y)
+CSRCS += rpmsg_heap.c
+endif
+
ifeq ($(CONFIG_RPMSG_WAKELOCK),y)
CSRCS += rpmsg_wakelock.c
endif
diff --git a/drivers/rpmsg/rpmsg.c b/drivers/rpmsg/rpmsg.c
index ea7fcd1b24e..d1dff03b5c1 100644
--- a/drivers/rpmsg/rpmsg.c
+++ b/drivers/rpmsg/rpmsg.c
@@ -724,6 +724,9 @@ void rpmsg_device_created(FAR struct rpmsg_s *rpmsg)
#ifdef CONFIG_RPMSG_WAKELOCK
rpmsg_wakelock_init(rdev, &rpmsg->wakelock);
#endif
+#ifdef CONFIG_RPMSG_HEAP
+ rpmsg_heap_init(rdev, &rpmsg->heap);
+#endif
}
void rpmsg_device_destroy(FAR struct rpmsg_s *rpmsg)
@@ -749,6 +752,10 @@ void rpmsg_device_destroy(FAR struct rpmsg_s *rpmsg)
rpmsg_wakelock_deinit(&rpmsg->wakelock);
#endif
+#ifdef CONFIG_RPMSG_HEAP
+ rpmsg_heap_deinit(&rpmsg->heap);
+#endif
+
nxrmutex_lock(&rpmsg->lock);
list_for_every_entry_safe(&rpmsg->bind, bind, bind_tmp,
struct rpmsg_bind_s, node)
diff --git a/drivers/rpmsg/rpmsg.h b/drivers/rpmsg/rpmsg.h
index 4c8876a5431..63ba916a760 100644
--- a/drivers/rpmsg/rpmsg.h
+++ b/drivers/rpmsg/rpmsg.h
@@ -74,6 +74,9 @@ struct rpmsg_s
#ifdef CONFIG_RPMSG_WAKELOCK
struct rpmsg_endpoint wakelock;
#endif
+#ifdef CONFIG_RPMSG_HEAP
+ struct rpmsg_endpoint heap;
+#endif
#ifdef CONFIG_RPMSG_CHAR
struct rpmsg_char_s chr;
#endif
@@ -156,5 +159,17 @@ void rpmsg_queue_rx_work(FAR struct rpmsg_s *rpmsg,
uint8_t priority,
int rpmsg_foreach(rpmsg_foreach_t handler, FAR void *args);
void rpmsg_dump_epts(FAR struct rpmsg_device *rdev);
+#ifdef CONFIG_RPMSG_HEAP
+int rpmsg_heap_init(FAR struct rpmsg_device *rdev,
+ FAR struct rpmsg_endpoint *ept);
+void rpmsg_heap_deinit(FAR struct rpmsg_endpoint *ept);
+FAR void *rpmsg_heap_alloc(FAR struct rpmsg_endpoint *ept,
+ size_t size, size_t align);
+void rpmsg_heap_free(FAR struct rpmsg_endpoint *ept, FAR void *addr);
+#else
+# define rpmsg_heap_alloc(ept, size, align) (NULL)
+# define rpmsg_heap_free(ept, addr)
+#endif
+
#endif /* CONFIG_RPMSG */
#endif /* __DRIVERS_RPMSG_RPMSG_H */
diff --git a/drivers/rpmsg/rpmsg_heap.c b/drivers/rpmsg/rpmsg_heap.c
new file mode 100644
index 00000000000..8ec325a1488
--- /dev/null
+++ b/drivers/rpmsg/rpmsg_heap.c
@@ -0,0 +1,258 @@
+/****************************************************************************
+ * drivers/rpmsg/rpmsg_heap.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/rpmsg/rpmsg.h>
+
+/****************************************************************************
+ * Pre-processor definitions
+ ****************************************************************************/
+
+#define RPMSG_MM_ALLOC_REQ 1
+#define RPMSG_MM_ALLOC_RESP 2
+#define RPMSG_MM_FREE_REQ 3
+#define RPMSG_MM_FREE_RESP 4
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct rpmsg_heap_cookie_s
+{
+ sem_t sem;
+ int32_t result;
+ FAR void *data;
+};
+
+begin_packed_struct struct rpmsg_heap_msg_s
+{
+ uint32_t cmd;
+ int32_t result;
+ uint32_t size;
+ uint32_t align;
+ uint64_t paddr;
+ uint64_t cookie;
+} end_packed_struct;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int rpmsg_heap_ept_cb(FAR struct rpmsg_endpoint *ept,
+ FAR void *data, size_t len, uint32_t src,
+ FAR void *priv)
+{
+ FAR struct rpmsg_heap_msg_s *msg = data;
+ FAR struct rpmsg_heap_cookie_s *cookie =
+ (FAR struct rpmsg_heap_cookie_s *)(uintptr_t)msg->cookie;
+ int ret = -EINVAL;
+
+ if (msg->cmd == RPMSG_MM_ALLOC_RESP)
+ {
+ cookie->data = up_addrenv_pa_to_va(msg->paddr);
+ cookie->result = msg->result;
+ ret = rpmsg_post(ept, &cookie->sem);
+ }
+ else if (msg->cmd == RPMSG_MM_FREE_RESP)
+ {
+ cookie->result = msg->result;
+ ret = rpmsg_post(ept, &cookie->sem);
+ }
+ else if (msg->cmd == RPMSG_MM_ALLOC_REQ)
+ {
+ msg->paddr = (uintptr_t)up_addrenv_va_to_pa(
+ rpmsg_alloc_buf(ept->rdev, msg->size, msg->align));
+ msg->result = msg->paddr ? 0 : -ENOMEM;
+ msg->cmd = RPMSG_MM_ALLOC_RESP;
+ ret = rpmsg_send(ept, msg, sizeof(struct rpmsg_heap_msg_s));
+ }
+ else if (msg->cmd == RPMSG_MM_FREE_REQ)
+ {
+ rpmsg_free_buf(ept->rdev, up_addrenv_pa_to_va(msg->paddr));
+ msg->cmd = RPMSG_MM_FREE_RESP;
+ msg->result = 0;
+ ret = rpmsg_send(ept, msg, sizeof(struct rpmsg_heap_msg_s));
+ }
+
+ if (ret < 0)
+ {
+ rpmsgerr("rpmsg_heap_ept_cb failed %d cmd %" PRIu32 "\n",
+ ret, msg->cmd);
+ }
+
+ return ret;
+}
+
+static FAR void *rpmsg_heap_get_tx_payload_buffer(
+ FAR struct rpmsg_endpoint *ept, FAR uint32_t *len)
+{
+ FAR sem_t *wait = ept->priv;
+ int sval;
+
+ nxsem_get_value(wait, &sval);
+ if (sval <= 0)
+ {
+ rpmsg_wait(ept, wait);
+ rpmsg_post(ept, wait);
+ }
+
+ return rpmsg_get_tx_payload_buffer(ept, len, true);
+}
+
+static FAR void *rpmsg_heap_send_recv(FAR struct rpmsg_endpoint *ept,
+ uint32_t command, size_t size,
+ size_t align, uint64_t paddr)
+{
+ FAR struct rpmsg_heap_cookie_s *cookie;
+ FAR struct rpmsg_heap_msg_s *msg;
+ FAR void *data = NULL;
+ uint32_t len;
+ int ret;
+
+ msg = rpmsg_heap_get_tx_payload_buffer(ept, &len);
+ if (!msg)
+ {
+ return NULL;
+ }
+
+ DEBUGASSERT(sizeof(struct rpmsg_heap_msg_s) <= len);
+
+ msg->cmd = command;
+ msg->result = -EINVAL;
+ msg->size = size;
+ msg->align = align;
+ msg->paddr = paddr;
+
+ cookie = rpmsg_pool_alloc(sizeof(*cookie));
+ memset(cookie, 0, sizeof(*cookie));
+ nxsem_init(&cookie->sem, 0, 0);
+ msg->cookie = (uintptr_t)cookie;
+
+ ret = rpmsg_send_nocopy(ept, msg, sizeof(struct rpmsg_heap_msg_s));
+ if (ret >= 0)
+ {
+ rpmsg_wait(ept, &cookie->sem);
+ }
+ else
+ {
+ rpmsgerr("rpmsg_heap_send_recv send failed: %d\n", ret);
+ rpmsg_release_tx_buffer(ept, msg);
+ goto out;
+ }
+
+ if (cookie->result != 0)
+ {
+ rpmsgerr("rpmsg_heap_send_recv failed: %" PRIi32 "\n", cookie->result);
+ }
+ else
+ {
+ data = cookie->data;
+ }
+
+out:
+ nxsem_destroy(&cookie->sem);
+ rpmsg_pool_free(cookie);
+ return data;
+}
+
+static void rpmsg_heap_ns_bound(struct rpmsg_endpoint *ept)
+{
+ FAR sem_t *wait = ept->priv;
+
+ rpmsg_post(ept, wait);
+}
+
+static void rpmsg_heap_ns_unbind(FAR struct rpmsg_endpoint *ept)
+{
+ FAR sem_t *wait = ept->priv;
+
+ rpmsg_wait(ept, wait);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+FAR void *rpmsg_heap_alloc(FAR struct rpmsg_endpoint *ept,
+ size_t size, size_t align)
+{
+ return rpmsg_heap_send_recv(ept, RPMSG_MM_ALLOC_REQ, size, align, 0);
+}
+
+void rpmsg_heap_free(FAR struct rpmsg_endpoint *ept, FAR void *addr)
+{
+ rpmsg_heap_send_recv(ept, RPMSG_MM_FREE_REQ, 0, 0,
+ up_addrenv_va_to_pa(addr));
+}
+
+int rpmsg_heap_init(FAR struct rpmsg_device *rdev,
+ FAR struct rpmsg_endpoint *ept)
+{
+ FAR sem_t *wait;
+ int ret = OK;
+
+ wait = kmm_zalloc(sizeof(*wait));
+ if (!wait)
+ {
+ rpmsgerr("rpmsg_heap_init alloc failed: %d\n", ret);
+ return -ENOMEM;
+ }
+
+ nxsem_init(wait, 0, 0);
+ ept->priv = wait;
+ ept->ns_bound_cb = rpmsg_heap_ns_bound;
+ ret = rpmsg_create_ept(ept, rdev, "rpmsg-heap",
+ RPMSG_ADDR_ANY, RPMSG_ADDR_ANY,
+ rpmsg_heap_ept_cb, rpmsg_heap_ns_unbind);
+ if (ret < 0)
+ {
+ rpmsgerr("rpmsg_heap_init failed: %d\n", ret);
+ nxsem_destroy(wait);
+ }
+
+ return ret;
+}
+
+void rpmsg_heap_deinit(FAR struct rpmsg_endpoint *ept)
+{
+ FAR sem_t *wait = ept->priv;
+ int sval;
+
+ nxsem_get_value(wait, &sval);
+ if (sval > 0)
+ {
+ rpmsg_wait(ept, wait);
+ }
+
+ rpmsg_destroy_ept(ept);
+ nxsem_destroy(wait);
+ kmm_free(wait);
+}
diff --git a/drivers/rpmsg/rpmsg_virtio.c b/drivers/rpmsg/rpmsg_virtio.c
index eb3dd2a804b..d3594ce5cb3 100644
--- a/drivers/rpmsg/rpmsg_virtio.c
+++ b/drivers/rpmsg/rpmsg_virtio.c
@@ -487,7 +487,9 @@ static FAR void *rpmsg_virtio_alloc_buf(FAR struct rpmsg_s
*rpmsg,
FAR struct rpmsg_virtio_priv_s *priv =
(FAR struct rpmsg_virtio_priv_s *)rpmsg;
- return virtio_malloc_buf(priv->vdev, size, align);
+ return priv->vdev->role == VIRTIO_DEV_DRIVER ?
+ virtio_malloc_buf(priv->vdev, size, align):
+ rpmsg_heap_alloc(&rpmsg->heap, size, align);
}
/****************************************************************************
@@ -500,7 +502,14 @@ static void rpmsg_virtio_free_buf(FAR struct rpmsg_s
*rpmsg,
FAR struct rpmsg_virtio_priv_s *priv =
(FAR struct rpmsg_virtio_priv_s *)rpmsg;
- virtio_free_buf(priv->vdev, addr);
+ if (priv->vdev->role == VIRTIO_DEV_DRIVER)
+ {
+ virtio_free_buf(priv->vdev, addr);
+ }
+ else
+ {
+ rpmsg_heap_free(&rpmsg->heap, addr);
+ }
}
/****************************************************************************