Skip to content

Commit

Permalink
virtio.h: add memory operation for virtio device
Browse files Browse the repository at this point in the history
Buffer management is different for different transport layer:

For MMIO transport layer, the buffer can direclty malloced from
the gust os heap beacase the hypervisor can access all the memmory own
by guest os.

For remoteproc transpor layer, the buffer should be malloced from
the share memory region to make sure the remote core can access this
buffer too.

So add memory ops in virtio device to make different transport/device can
implement their own share memory management.

Signed-off-by: Bowen Wang <[email protected]>
  • Loading branch information
CV-Bowen committed Oct 18, 2024
1 parent 3541b57 commit 64009ec
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions lib/include/openamp/virtio.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ struct virtio_device_id {
typedef void (*virtio_dev_reset_cb)(struct virtio_device *vdev);

struct virtio_dispatch;
struct virtio_memory_ops;

/** @brief Device features. */
struct virtio_feature_desc {
Expand Down Expand Up @@ -197,6 +198,9 @@ struct virtio_device {
/** Virtio dispatch table */
const struct virtio_dispatch *func;

/**< Virtio device memory operations */
const struct virtio_memory_ops *mmops;

/** Private data */
void *priv;

Expand Down Expand Up @@ -282,6 +286,14 @@ struct virtio_dispatch {
void (*notify)(struct virtqueue *vq);
};

struct virtio_memory_ops {
/** Allocate memory from the virtio device. */
void *(*alloc)(struct virtio_device *dev, size_t size, size_t align);

/** Free memory allocated from the virtio device. */
void (*free)(struct virtio_device *dev, void *buf);
};

/**
* @brief Create the virtio device virtqueue.
*
Expand Down Expand Up @@ -499,6 +511,51 @@ static inline int virtio_reset_device(struct virtio_device *vdev)
return 0;
}

/**
* @brief Allocate buffer from the virtio device
*
* @param vdev Pointer to virtio device structure.
* @param buf Pointer to the allocated buffer.
* @param size Allocated buffer size.
* @param align Allocated buffer alignment.
*
* @return 0 on success, otherwise error code.
*/
static inline int virtio_alloc_buf(struct virtio_device *vdev, void **buf,
size_t size, size_t align)
{
if (!vdev || !buf)
return -EINVAL;

if (!vdev->mmops || !vdev->mmops->alloc)
return -ENXIO;

*buf = vdev->mmops->alloc(vdev, size, align);
if (!*buf)
return -ENOMEM;
return 0;
}

/**
* @brief Free buffer to the virtio device
*
* @param vdev Pointer to virtio device structure.
* @param buf Buffer need to be freed.
*
* @return 0 on success, otherwise error code.
*/
static inline int virtio_free_buf(struct virtio_device *vdev, void *buf)
{
if (!vdev)
return -EINVAL;

if (!vdev->mmops || !vdev->mmops->free)
return -ENXIO;

vdev->mmops->free(vdev, buf);
return 0;
}

#if defined __cplusplus
}
#endif
Expand Down

0 comments on commit 64009ec

Please sign in to comment.