Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

virtio.h: add memory operation for virtio device #541

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 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,54 @@ 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 (virtual address).
* @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);
tnmysh marked this conversation as resolved.
Show resolved Hide resolved
if (!*buf)
return -ENOMEM;

return 0;
CV-Bowen marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @brief Free the buffer allocated by \ref virtio_alloc_buf from 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;
CV-Bowen marked this conversation as resolved.
Show resolved Hide resolved
}

#if defined __cplusplus
}
#endif
Expand Down