Skip to content

Commit

Permalink
openamp: add new API rpmsg_virtio_get_rx_buffer_size()
Browse files Browse the repository at this point in the history
This API can be used by the endpoint callback to limit the rx
buffer usage range.

Signed-off-by: Guiding Li <liguiding1@xiaomi.com>
Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
  • Loading branch information
GUIDINGLI authored and CV-Bowen committed Oct 26, 2023
1 parent 4e13c09 commit 9c88fce
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 48 deletions.
28 changes: 26 additions & 2 deletions lib/include/openamp/rpmsg_virtio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
119 changes: 73 additions & 46 deletions lib/rpmsg/rpmsg_virtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,49 +270,6 @@ static int rpmsg_virtio_wait_remote_ready(struct rpmsg_virtio_device *rvdev)
}
#endif /*!VIRTIO_DRIVER_ONLY*/

/**
* @internal
*
* @brief Returns buffer size available for sending messages.
*
* @param rvdev Pointer to rpmsg device
*
* @return Buffer size
*/
static int _rpmsg_virtio_get_buffer_size(struct rpmsg_virtio_device *rvdev)
{
unsigned int role = rpmsg_virtio_get_role(rvdev);
int length = 0;

#ifndef VIRTIO_DEVICE_ONLY
if (role == RPMSG_HOST) {
/*
* 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);
}
#endif /*!VIRTIO_DEVICE_ONLY*/

#ifndef VIRTIO_DRIVER_ONLY
if (role == RPMSG_REMOTE) {
/*
* 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);
}
#endif /*!VIRTIO_DRIVER_ONLY*/

if (length <= 0) {
length = RPMSG_ERR_NO_BUFF;
}

return length;
}

static void rpmsg_virtio_hold_rx_buffer(struct rpmsg_device *rdev, void *rxbuf)
{
struct rpmsg_hdr *rp_hdr;
Expand Down Expand Up @@ -661,17 +618,87 @@ 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;
unsigned int role;
int size = 0;

if (!rdev)
return RPMSG_ERR_PARAM;

metal_mutex_acquire(&rdev->lock);
rvdev = (struct rpmsg_virtio_device *)rdev;
size = _rpmsg_virtio_get_buffer_size(rvdev);
role = rpmsg_virtio_get_role(rvdev);

#ifndef VIRTIO_DEVICE_ONLY
if (role == RPMSG_HOST) {
/*
* If device role is host then buffers are provided by us,
* so just provide the macro.
*/
size = rvdev->config.h2r_buf_size - sizeof(struct rpmsg_hdr);
}
#endif /*!VIRTIO_DEVICE_ONLY*/

#ifndef VIRTIO_DRIVER_ONLY
if (role == RPMSG_REMOTE) {
/*
* If other core is host then buffers are provided by it,
* so get the buffer size from the virtqueue.
*/
size = (int)virtqueue_get_desc_size(rvdev->svq) -
sizeof(struct rpmsg_hdr);
}
#endif /*!VIRTIO_DRIVER_ONLY*/

if (size <= 0)
size = RPMSG_ERR_NO_BUFF;

metal_mutex_release(&rdev->lock);

return size;
}

int rpmsg_virtio_get_rx_buffer_size(struct rpmsg_device *rdev)
{
struct rpmsg_virtio_device *rvdev;
unsigned int role;
int size = 0;

if (!rdev)
return RPMSG_ERR_PARAM;

metal_mutex_acquire(&rdev->lock);
rvdev = (struct rpmsg_virtio_device *)rdev;
role = rpmsg_virtio_get_role(rvdev);

#ifndef VIRTIO_DEVICE_ONLY
if (role == RPMSG_HOST) {
/*
* If device role is host then buffers are provided by us,
* so just provide the macro.
*/
size = rvdev->config.r2h_buf_size - sizeof(struct rpmsg_hdr);
}
#endif /*!VIRTIO_DEVICE_ONLY*/

#ifndef VIRTIO_DRIVER_ONLY
if (role == RPMSG_REMOTE) {
/*
* If other core is host then buffers are provided by it,
* so get the buffer size from the virtqueue.
*/
size = (int)virtqueue_get_desc_size(rvdev->rvq) -
sizeof(struct rpmsg_hdr);
}
#endif /*!VIRTIO_DRIVER_ONLY*/

if (size <= 0)
size = RPMSG_ERR_NO_BUFF;

metal_mutex_release(&rdev->lock);

return size;
}

Expand Down

0 comments on commit 9c88fce

Please sign in to comment.