Skip to content

Commit

Permalink
openamp: add notify_wait_cb to avoid looping in tx buffer get
Browse files Browse the repository at this point in the history
Give users a chance to handle the no tx buffer situation when get tx
buffer, with this patch, user can set the notify_wait_cb in driver
and this callback function will be called to handle the wait when no
tx buffer in tx virtqueue.

Signed-off-by: Yongrong Wang <wangyongrong@xiaomi.com>
  • Loading branch information
ExplorerWyr authored and arnopo committed Mar 18, 2024
1 parent 198e2e6 commit 15b4a8b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/include/openamp/rpmsg.h
Expand Up @@ -43,6 +43,7 @@ extern "C" {
#define RPMSG_ERR_INIT (RPMSG_ERROR_BASE - 6)
#define RPMSG_ERR_ADDR (RPMSG_ERROR_BASE - 7)
#define RPMSG_ERR_PERM (RPMSG_ERROR_BASE - 8)
#define RPMSG_EOPNOTSUPP (RPMSG_ERROR_BASE - 9)

struct rpmsg_endpoint;
struct rpmsg_device;
Expand Down
9 changes: 9 additions & 0 deletions lib/include/openamp/rpmsg_virtio.h
Expand Up @@ -41,6 +41,9 @@ extern "C" {
#define BUFFER_INVALIDATE(x, s) do { } while (0)
#endif /* VIRTIO_CACHED_BUFFERS || VIRTIO_USE_DCACHE */

/* Callback handler for rpmsg virtio service */
typedef int (*rpmsg_virtio_notify_wait_cb)(struct rpmsg_device *rdev, uint32_t id);

/** @brief Shared memory pool used for RPMsg buffers */
struct rpmsg_virtio_shm_pool {
/** Base address of the memory pool */
Expand Down Expand Up @@ -98,6 +101,12 @@ struct rpmsg_virtio_device {
* \ref rpmsg_virtio_release_tx_buffer function
*/
struct metal_list reclaimer;

/**
* Callback handler for rpmsg virtio service, called when service
* can't get tx buffer
*/
rpmsg_virtio_notify_wait_cb notify_wait_cb;
};

#define RPMSG_REMOTE VIRTIO_DEV_DEVICE
Expand Down
26 changes: 24 additions & 2 deletions lib/rpmsg/rpmsg_virtio.c
Expand Up @@ -358,6 +358,18 @@ static void rpmsg_virtio_release_rx_buffer(struct rpmsg_device *rdev,
metal_mutex_release(&rdev->lock);
}

static int rpmsg_virtio_notify_wait(struct rpmsg_virtio_device *rvdev, struct virtqueue *vq)
{
struct virtio_vring_info *vring_info;

vring_info = &rvdev->vdev->vrings_info[vq->vq_queue_index];

if (!rvdev->notify_wait_cb)
return RPMSG_EOPNOTSUPP;

return rvdev->notify_wait_cb(&rvdev->rdev, vring_info->notifyid);
}

static void *rpmsg_virtio_get_tx_payload_buffer(struct rpmsg_device *rdev,
uint32_t *len, int wait)
{
Expand Down Expand Up @@ -387,8 +399,18 @@ static void *rpmsg_virtio_get_tx_payload_buffer(struct rpmsg_device *rdev,
metal_mutex_release(&rdev->lock);
if (rp_hdr || !tick_count)
break;
metal_sleep_usec(RPMSG_TICKS_PER_INTERVAL);
tick_count--;

/*
* Try to use wait loop implemented in the virtio dispatcher and
* use metal_sleep_usec() method by default.
*/
status = rpmsg_virtio_notify_wait(rvdev, rvdev->rvq);
if (status == RPMSG_EOPNOTSUPP) {
metal_sleep_usec(RPMSG_TICKS_PER_INTERVAL);
tick_count--;
} else if (status == RPMSG_SUCCESS) {
break;
}
}

if (!rp_hdr)
Expand Down

0 comments on commit 15b4a8b

Please sign in to comment.