Skip to content

Commit

Permalink
openamp: add new ops wait_notified() 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 call rproc_virtio_set_wait_notified() to set the
wait_notified() callback and this callback function will be called
when no tx buffer in tx virtqueue.

Signed-off-by: Guiding Li <[email protected]>
Signed-off-by: Bowen Wang <[email protected]>
  • Loading branch information
GUIDINGLI authored and CV-Bowen committed Oct 17, 2023
1 parent cd88238 commit 8fb21ee
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/include/openamp/remoteproc.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,17 @@ struct remoteproc_ops {
/** Notify the remote */
int (*notify)(struct remoteproc *rproc, uint32_t id);

/**
* @brief Wait for remote notified, when there is no TX buffer anymore.
* Set to NULL means use usleep to wait TX buffer available.
*
* @param rproc pointer to remoteproc instance
* @param id the notifyid
*
* return 0 means there is notify available, otherwise negative value.
*/
int (*wait_notified)(struct remoteproc *rproc, uint32_t id);

/**
* @brief Get remoteproc memory I/O region by either name, virtual
* address, physical address or device address.
Expand Down Expand Up @@ -497,6 +508,7 @@ struct remoteproc_ops {
#define RPROC_ERR_RSC_TAB_NP (RPROC_EBASE + 10)
#define RPROC_ERR_RSC_TAB_NS (RPROC_EBASE + 11)
#define RPROC_ERR_LOADER_STATE (RPROC_EBASE + 12)
#define RPROC_EXIO (RPROC_EBASE + 13)
#define RPROC_EMAX (RPROC_EBASE + 16)
#define RPROC_EPTR (void *)(-1)
#define RPROC_EOF (void *)(-1)
Expand Down
22 changes: 22 additions & 0 deletions lib/include/openamp/remoteproc_virtio.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ extern "C" {
/* define vdev notification function user should implement */
typedef int (*rpvdev_notify_func)(void *priv, uint32_t id);

/* define vdev wait notified function, user can implement this
* function to wait peer return the tx buffer when no tx buffer found,
* and this is optional
*/
typedef int (*rpvdev_wait_notified_func)(void *priv, uint32_t id);

/** @brief Virtio structure for remoteproc instance */
struct remoteproc_virtio {
/** Pointer to private data */
Expand All @@ -53,6 +59,9 @@ struct remoteproc_virtio {
/** Notification function */
rpvdev_notify_func notify;

/** Wait notification function */
rpvdev_wait_notified_func wait_notified;

/** Virtio device */
struct virtio_device vdev;

Expand Down Expand Up @@ -126,6 +135,19 @@ int rproc_virtio_notified(struct virtio_device *vdev, uint32_t notifyid);
*/
void rproc_virtio_wait_remote_ready(struct virtio_device *vdev);

/**
* @brief set the remoteproc virtio notified function, this function will be
* called when no tx buffer to let user to handle the customisze buffer wait
* function.
*
* @param vdev Pointer to the virtio device
* @param wait_notified The wait notified callback function
*
* @return 0 for successful, negative value for failure
*/
int rproc_virtio_set_wait_notified(struct virtio_device *vdev,
rpvdev_wait_notified_func wait_notified);

#if defined __cplusplus
}
#endif
Expand Down
9 changes: 9 additions & 0 deletions lib/include/openamp/rpmsg_virtio.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ rpmsg_virtio_create_virtqueues(struct rpmsg_virtio_device *rvdev,
callbacks, NULL);
}

static inline int
rpmsg_virtio_wait_notified(struct rpmsg_virtio_device *rvdev,
struct virtqueue *vq)
{
return rvdev->vdev->func->wait_notified ?
rvdev->vdev->func->wait_notified(rvdev->vdev, vq) :
RPMSG_ERR_PERM;
}

/**
* @brief Get rpmsg virtio buffer size
*
Expand Down
1 change: 1 addition & 0 deletions lib/include/openamp/virtio.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ struct virtio_dispatch {

/** Notify the other side that a virtio vring as been updated. */
void (*notify)(struct virtqueue *vq);
int (*wait_notified)(struct virtio_device *dev, struct virtqueue *vq);
};

/**
Expand Down
11 changes: 11 additions & 0 deletions lib/remoteproc/remoteproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,16 @@ static int remoteproc_virtio_notify(void *priv, uint32_t id)
return 0;
}

static int remoteproc_virtio_wait_notified(void *priv, uint32_t id)
{
struct remoteproc *rproc = priv;

if (rproc->ops->wait_notified)
return rproc->ops->wait_notified(rproc, id);

return 0;
}

struct virtio_device *
remoteproc_create_virtio(struct remoteproc *rproc,
int vdev_id, unsigned int role,
Expand Down Expand Up @@ -958,6 +968,7 @@ remoteproc_create_virtio(struct remoteproc *rproc,
rproc_virtio_wait_remote_ready(vdev);

rpvdev = metal_container_of(vdev, struct remoteproc_virtio, vdev);
rpvdev->wait_notified = remoteproc_virtio_wait_notified;
metal_list_add_tail(&rproc->vdevs, &rpvdev->node);
num_vrings = vdev_rsc->num_of_vrings;

Expand Down
28 changes: 28 additions & 0 deletions lib/remoteproc/remoteproc_virtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ static void rproc_virtio_virtqueue_notify(struct virtqueue *vq)
rpvdev->notify(rpvdev->priv, vring_info->notifyid);
}

static int rproc_virtio_wait_notified(struct virtio_device *vdev,
struct virtqueue *vq)
{
struct remoteproc_virtio *rpvdev;
struct virtio_vring_info *vring_info;
unsigned int vq_id = vq->vq_queue_index;

rpvdev = metal_container_of(vdev, struct remoteproc_virtio, vdev);
vring_info = &vdev->vrings_info[vq_id];

return rpvdev->wait_notified ?
rpvdev->wait_notified(rpvdev->priv, vring_info->notifyid) :
-RPROC_EXIO;
}

static unsigned char rproc_virtio_get_status(struct virtio_device *vdev)
{
struct remoteproc_virtio *rpvdev;
Expand Down Expand Up @@ -187,6 +202,7 @@ static const struct virtio_dispatch remoteproc_virtio_dispatch_funcs = {
.get_features = rproc_virtio_get_features,
.read_config = rproc_virtio_read_config,
.notify = rproc_virtio_virtqueue_notify,
.wait_notified = rproc_virtio_wait_notified,
#ifndef VIRTIO_DEVICE_ONLY
/*
* We suppose here that the vdev is in a shared memory so that can
Expand Down Expand Up @@ -364,3 +380,15 @@ void rproc_virtio_wait_remote_ready(struct virtio_device *vdev)
metal_cpu_yield();
}
}

int rproc_virtio_set_wait_notified(struct virtio_device *vdev,
rpvdev_wait_notified_func wait_notified)
{
struct remoteproc_virtio *rpvdev;

if (!vdev || !wait_notified)
return -RPROC_EINVAL;
rpvdev = metal_container_of(vdev, struct remoteproc_virtio, vdev);
rpvdev->wait_notified = wait_notified;
return 0;
}
5 changes: 5 additions & 0 deletions lib/rpmsg/rpmsg_virtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ static void *rpmsg_virtio_get_tx_payload_buffer(struct rpmsg_device *rdev,
metal_mutex_release(&rdev->lock);
if (rp_hdr || !tick_count)
break;

status = rpmsg_virtio_wait_notified(rvdev, rvdev->rvq);
if (status == RPMSG_SUCCESS)
continue;

metal_sleep_usec(RPMSG_TICKS_PER_INTERVAL);
tick_count--;
}
Expand Down

0 comments on commit 8fb21ee

Please sign in to comment.