virtqueue: add writable buffer helper

An application at device side might need to know if a buffer has been
marked as device-writable by the driver.
An example is the virtio SPI protocol, in which the direction of an
half-duplex communication can be determined by the device only by
checking if the buffer containing the data is writable or not.

Add a simple helper to test if a buffer is device-writable, that is,
if the VRING_DESC_F_WRITE flag is present on it.

Signed-off-by: Francesco Valla <francesco@valla.it>
This commit is contained in:
Francesco Valla
2026-09-21 09:51:59 +02:00
committed by Arnaud Pouliquen
parent 3ea751021e
commit 80284e3ac0
2 changed files with 18 additions and 0 deletions
+10
View File
@@ -401,6 +401,16 @@ uint32_t virtqueue_get_desc_size(struct virtqueue *vq);
uint32_t virtqueue_get_buffer_length(struct virtqueue *vq, uint16_t idx);
void *virtqueue_get_buffer_addr(struct virtqueue *vq, uint16_t idx);
/**
* @brief Test if buffer is marked as device-writable
*
* @param vq Pointer to VirtIO queue control block
* @param idx Index used in vring desc table
*
* @return true if the buffer is marked as device-writable, false otherwise
*/
bool virtqueue_is_buffer_device_writable(struct virtqueue *vq, uint16_t idx);
/**
* @brief Test if virtqueue is empty
*
+8
View File
@@ -187,6 +187,14 @@ void *virtqueue_get_buffer_addr(struct virtqueue *vq, uint16_t idx)
return virtqueue_phys_to_virt(vq, vq->vq_ring.desc[idx].addr);
}
bool virtqueue_is_buffer_device_writable(struct virtqueue *vq, uint16_t idx)
{
/* Invalidate the desc entry written by driver before accessing it */
VRING_INVALIDATE(&vq->vq_ring.desc[idx].flags,
sizeof(vq->vq_ring.desc[idx].flags));
return !!(vq->vq_ring.desc[idx].flags & VRING_DESC_F_WRITE);
}
void virtqueue_free(struct virtqueue *vq)
{
if (vq) {