diff --git a/lib/include/openamp/rpmsg_virtio.h b/lib/include/openamp/rpmsg_virtio.h index aea2edf56..ae2446638 100644 --- a/lib/include/openamp/rpmsg_virtio.h +++ b/lib/include/openamp/rpmsg_virtio.h @@ -166,13 +166,37 @@ rpmsg_virtio_create_virtqueues(struct rpmsg_virtio_device *rvdev, } /** - * @brief Get rpmsg virtio buffer size + * @brief Get rpmsg virtio Tx buffer size * * @param rdev Pointer to the rpmsg device * * @return Next available buffer size for text, negative value for failure */ -int rpmsg_virtio_get_buffer_size(struct rpmsg_device *rdev); +int rpmsg_virtio_get_tx_buffer_size(struct rpmsg_device *rdev); + +/** + * @brief Get rpmsg virtio Rx buffer size + * + * @param rdev Pointer to the rpmsg device + * + * @return Next available buffer size for text, negative value for failure + */ +int rpmsg_virtio_get_rx_buffer_size(struct rpmsg_device *rdev); + +/** + * @brief Get rpmsg virtio Tx buffer size + * + * This function is same as rpmsg_virtio_get_tx_buffer_size(), keep it here + * to maintain the forward compatibility. + * + * @param rdev Pointer to the rpmsg device. + * + * @return Next available buffer size for text, negative value for failure. + */ +static inline int rpmsg_virtio_get_buffer_size(struct rpmsg_device *rdev) +{ + return rpmsg_virtio_get_tx_buffer_size(rdev); +} /** * @brief Initialize rpmsg virtio device diff --git a/lib/rpmsg/rpmsg_internal.h b/lib/rpmsg/rpmsg_internal.h index 6721ecf88..825050bd1 100644 --- a/lib/rpmsg/rpmsg_internal.h +++ b/lib/rpmsg/rpmsg_internal.h @@ -34,6 +34,17 @@ extern "C" { ((struct rpmsg_hdr *)((unsigned char *)(p) - sizeof(struct rpmsg_hdr))) #define RPMSG_LOCATE_DATA(p) ((unsigned char *)(p) + sizeof(struct rpmsg_hdr)) +/** + * enum rpmsg_buffer_type - rpmsg buffer type + * + * @RPMSG_BUFFER_RX: rpmsg rx buffer type + * @RPMSG_BUFFER_TX: rpmsg tx buffer type + */ +enum rpmsg_buffer_type { + RPMSG_BUFFER_RX = 0, + RPMSG_BUFFER_TX = 1, +}; + /** * enum rpmsg_ns_flags - dynamic name service announcement flags * diff --git a/lib/rpmsg/rpmsg_virtio.c b/lib/rpmsg/rpmsg_virtio.c index ea4cc0d9e..69d53f31c 100644 --- a/lib/rpmsg/rpmsg_virtio.c +++ b/lib/rpmsg/rpmsg_virtio.c @@ -273,13 +273,15 @@ static int rpmsg_virtio_wait_remote_ready(struct rpmsg_virtio_device *rvdev) /** * @internal * - * @brief Returns buffer size available for sending messages. + * @brief Returns buffer size available for sending/receiving messages. * * @param rvdev Pointer to rpmsg device + * @param tx Tx or Rx buffer * * @return Buffer size */ -static int _rpmsg_virtio_get_buffer_size(struct rpmsg_virtio_device *rvdev) +static int _rpmsg_virtio_get_buffer_size(struct rpmsg_virtio_device *rvdev, + enum rpmsg_buffer_type type) { unsigned int role = rpmsg_virtio_get_role(rvdev); int length = 0; @@ -290,7 +292,10 @@ static int _rpmsg_virtio_get_buffer_size(struct rpmsg_virtio_device *rvdev) * If device role is host then buffers are provided by us, * so just provide the macro. */ - length = rvdev->config.h2r_buf_size - sizeof(struct rpmsg_hdr); + if (type == RPMSG_BUFFER_TX) + length = rvdev->config.h2r_buf_size; + else if (type == RPMSG_BUFFER_RX) + length = rvdev->config.r2h_buf_size - sizeof(struct rpmsg_hdr); } #endif /*!VIRTIO_DEVICE_ONLY*/ @@ -300,9 +305,12 @@ static int _rpmsg_virtio_get_buffer_size(struct rpmsg_virtio_device *rvdev) * If other core is host then buffers are provided by it, * so get the buffer size from the virtqueue. */ - length = - (int)virtqueue_get_desc_size(rvdev->svq) - - sizeof(struct rpmsg_hdr); + if (type == RPMSG_BUFFER_TX) + length = (int)virtqueue_get_desc_size(rvdev->svq) - + sizeof(struct rpmsg_hdr); + else if (type == RPMSG_BUFFER_RX) + length = (int)virtqueue_get_desc_size(rvdev->rvq) - + sizeof(struct rpmsg_hdr); } #endif /*!VIRTIO_DRIVER_ONLY*/ @@ -661,7 +669,7 @@ static int rpmsg_virtio_ns_callback(struct rpmsg_endpoint *ept, void *data, return RPMSG_SUCCESS; } -int rpmsg_virtio_get_buffer_size(struct rpmsg_device *rdev) +int rpmsg_virtio_get_tx_buffer_size(struct rpmsg_device *rdev) { int size; struct rpmsg_virtio_device *rvdev; @@ -670,7 +678,21 @@ int rpmsg_virtio_get_buffer_size(struct rpmsg_device *rdev) return RPMSG_ERR_PARAM; metal_mutex_acquire(&rdev->lock); rvdev = (struct rpmsg_virtio_device *)rdev; - size = _rpmsg_virtio_get_buffer_size(rvdev); + size = _rpmsg_virtio_get_buffer_size(rvdev, RPMSG_BUFFER_TX); + metal_mutex_release(&rdev->lock); + return size; +} + +int rpmsg_virtio_get_rx_buffer_size(struct rpmsg_device *rdev) +{ + int size; + struct rpmsg_virtio_device *rvdev; + + if (!rdev) + return RPMSG_ERR_PARAM; + metal_mutex_acquire(&rdev->lock); + rvdev = (struct rpmsg_virtio_device *)rdev; + size = _rpmsg_virtio_get_buffer_size(rvdev, RPMSG_BUFFER_RX); metal_mutex_release(&rdev->lock); return size; }