Patchwork [2/3] virtio: Added function to calculate number of bytes required to allocate a VRing

login
register
mail settings
Submitter David Gibson
Date May 17, 2011, 6:47 a.m.
Message ID <1305614826-1815-3-git-send-email-david@gibson.dropbear.id.au>
Download mbox | patch
Permalink /patch/95857/
State New
Headers show

Comments

David Gibson - May 17, 2011, 6:47 a.m.
From: Alexey Kardashevskiy <aik@ozlabs.ru>

The existing function virtio_queue_get_ring_size returns number of bytes
for vring only when it is already initialized.

In order to know how much memory new vring requires, new function
virtio_queue_get_mem_size has been introduced. It is a copy
of the vring_size function from the linux kernel (include/linux/virtio_ring.h).

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 hw/virtio.c |    8 ++++++++
 hw/virtio.h |    1 +
 2 files changed, 9 insertions(+), 0 deletions(-)
Alexander Graf - May 17, 2011, 6:58 a.m.
On 17.05.2011, at 08:47, David Gibson wrote:

> From: Alexey Kardashevskiy <aik@ozlabs.ru>
> 
> The existing function virtio_queue_get_ring_size returns number of bytes
> for vring only when it is already initialized.
> 
> In order to know how much memory new vring requires, new function
> virtio_queue_get_mem_size has been introduced. It is a copy
> of the vring_size function from the linux kernel (include/linux/virtio_ring.h).
> 
> Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>

Michael, could I please get your signed-off-by here?


Alex

Patch

diff --git a/hw/virtio.c b/hw/virtio.c
index 6e8814c..ff05b25 100644
--- a/hw/virtio.c
+++ b/hw/virtio.c
@@ -574,6 +574,14 @@  int virtio_queue_get_num(VirtIODevice *vdev, int n)
     return vdev->vq[n].vring.num;
 }
 
+int virtio_queue_get_mem_size(VirtIODevice *vdev, int n, int align)
+{
+    int num = virtio_queue_get_num(vdev, n);
+    return ((sizeof(VRingDesc)*num + sizeof(uint16_t)*(2 + num)
+            + align - 1) & ~(align - 1))
+            + sizeof(uint16_t)*2 + sizeof(VRingUsedElem)*num;
+}
+
 void virtio_queue_notify_vq(VirtQueue *vq)
 {
     if (vq->vring.desc) {
diff --git a/hw/virtio.h b/hw/virtio.h
index bc72289..114a877 100644
--- a/hw/virtio.h
+++ b/hw/virtio.h
@@ -182,6 +182,7 @@  void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
 void virtio_queue_set_addr(VirtIODevice *vdev, int n, target_phys_addr_t addr);
 target_phys_addr_t virtio_queue_get_addr(VirtIODevice *vdev, int n);
 int virtio_queue_get_num(VirtIODevice *vdev, int n);
+int virtio_queue_get_mem_size(VirtIODevice *vdev, int n, int align);
 void virtio_queue_notify(VirtIODevice *vdev, int n);
 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);