Skip to contentSkip to conversation

Add virtio-mmio and use it in vexpress

PATCH Superseded Peter Maydell 3 participants 23 messages Jul 12, 2013 – Jul 25, 2013 Arm/versatile express platform

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add virtio-mmio and use it in vexpress

This patch series adds an implementation of the virtio-mmio transport, and uses it in the vexpress-a9 and vexpress-a15 board models.

The basic idea is that the board instantiates some transports, the user can create backends which automatically plug into them (via -device virtio-blk-backend and the like), and we tell the guest kernel about them using a new arm/boot hook that lets the board modify the user's device tree blob.

Changes v1->v2:
* move the 'is guest provided queue size too large?' check from virtio-mmio into the core virtio 'set_num' function
* rebase (including adding owner parameter to memory region init call in virtio-mmio.c)

Peter Maydell (8):
device_tree: Add qemu_devtree_setprop_sized_cells() utility functions arm/boot: Use qemu_devtree_setprop_sized_cells()
virtio: Add support for guest setting of queue size
virtio: Support transports which can specify the vring alignment
virtio: Implement MMIO based virtio transport
arm/boot: Allow boards to modify the FDT blob
vexpress: Make VEDBoardInfo extend arm_boot_info
vexpress: Add virtio-mmio transports

 device_tree.c                  |   31 +++
 hw/arm/boot.c                  |   33 ++--
 hw/arm/vexpress.c              |  128 ++++++++++--
 hw/virtio/Makefile.objs        |    1 +
 hw/virtio/virtio-mmio.c        |  421 ++++++++++++++++++++++++++++++++++++++++
 hw/virtio/virtio.c             |   40 +++-
 include/hw/arm/arm.h           |    4 +
 include/hw/virtio/virtio-bus.h |    6 +
 include/hw/virtio/virtio.h     |    2 +
 include/sysemu/device_tree.h   |   62 ++++++
 10 files changed, 689 insertions(+), 39 deletions(-)
 create mode 100644 hw/virtio/virtio-mmio.c
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v2 1/8 device_tree: Add qemu_devtree_setprop_sized_cells() utility functions

We already have a qemu_devtree_setprop_cells() which sets a dtb property to an array of cells whose values are specified by varargs. However for the fairly common case of setting a property to a list of addresses or of address,size pairs the number of cells used by each element in the list depends on the parent's #address-cells and #size-cells properties. To make this easier we provide an analogous qemu_devtree_setprop_sized_cells() macro which allows the number of cells used by each element to be specified. This is implemented using an underlying qemu_devtree_setprop_sized_cells_from_array() function which takes the values and sizes as an array; this may also be directly useful for cases where the cell contents are constructed programmatically.

---
 device_tree.c                |   31 +++++++++++++++++++++
 include/sysemu/device_tree.h |   62 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)
device_tree.c +31 -0
index 10cf3d0..308205c 100644
@@ -308,3 +308,34 @@ void qemu_devtree_dumpdtb(void *fdt, int size)
         exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
     }
 }
+
+int qemu_devtree_setprop_sized_cells_from_array(void *fdt,
+                                                const char *node_path,
+                                                const char *property,
+                                                int numvalues,
+                                                uint64_t *values)
+{
+    uint32_t *propcells;
+    uint64_t value;
+    int cellnum, vnum, ncells;
+    uint32_t hival;
+
+    propcells = g_new0(uint32_t, numvalues * 2);
+
+    cellnum = 0;
+    for (vnum = 0; vnum < numvalues; vnum++) {
+        ncells = values[vnum * 2];
+        assert(ncells == 1 || ncells == 2);
+        value = values[vnum * 2 + 1];
+        hival = cpu_to_be32(value >> 32);
+        if (ncells > 1) {
+            propcells[cellnum++] = hival;
+        } else if (hival != 0) {
+            return vnum + 1;
+        }
+        propcells[cellnum++] = cpu_to_be32(value);
+    }
+
+    return qemu_devtree_setprop(fdt, node_path, property, propcells,
+                                cellnum * sizeof(uint32_t));
+}
include/sysemu/device_tree.h +62 -0

66 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/8] device_tree: Add qemu_devtree_setprop_sized_cells() utility functions

Hi Peter,

Quoted 84 lines · Peter Maydell

We already have a qemu_devtree_setprop_cells() which sets a dtb property to an array of cells whose values are specified by varargs. However for the fairly common case of setting a property to a list of addresses or of address,size pairs the number of cells used by each element in the list depends on the parent's #address-cells and #size-cells properties. To make this easier we provide an analogous qemu_devtree_setprop_sized_cells() macro which allows the number of cells used by each element to be specified. This is implemented using an underlying qemu_devtree_setprop_sized_cells_from_array() function which takes the values and sizes as an array; this may also be directly useful for cases where the cell contents are constructed programmatically.

---
 device_tree.c                |   31 +++++++++++++++++++++
 include/sysemu/device_tree.h |   62 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)
device_tree.c +31 -0
index 10cf3d0..308205c 100644
@@ -308,3 +308,34 @@ void qemu_devtree_dumpdtb(void *fdt, int size)
         exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
     }
 }
+
+int qemu_devtree_setprop_sized_cells_from_array(void *fdt,
+                                                const char *node_path,
+                                                const char *property,
+                                                int numvalues,
+                                                uint64_t *values)
+{
+    uint32_t *propcells;
+    uint64_t value;
+    int cellnum, vnum, ncells;
+    uint32_t hival;
+
+    propcells = g_new0(uint32_t, numvalues * 2);
+
+    cellnum = 0;
+    for (vnum = 0; vnum < numvalues; vnum++) {
+        ncells = values[vnum * 2];
+        assert(ncells == 1 || ncells == 2);
+        value = values[vnum * 2 + 1];
+        hival = cpu_to_be32(value >> 32);
+        if (ncells > 1) {
+            propcells[cellnum++] = hival;
+        } else if (hival != 0) {
+            return vnum + 1;
+        }
+        propcells[cellnum++] = cpu_to_be32(value);
+    }
+
+    return qemu_devtree_setprop(fdt, node_path, property, propcells,
+                                cellnum * sizeof(uint32_t));
+}
include/sysemu/device_tree.h +18 -0
index f0b3f35..8189ebc 100644
@@ -51,4 +51,66 @@ int qemu_devtree_add_subnode(void *fdt, const char *name);
 
 void qemu_devtree_dumpdtb(void *fdt, int size);
 
+/**
+ * qemu_devtree_setprop_sized_cells_from_array:
+ * @fdt: device tree blob
+ * @node_path: node to set property on
+ * @property: property to set
+ * @numvalues: number of values
+ * @values: array of number-of-cells, value pairs
+ *
+ * Set the specified property on the specified node in the device tree
+ * to be an array of cells. The values of the cells are specified via
+ * the values list, which alternates between "number of cells used by
+ * this value" and "value".
+ * number-of-cells must be either 1 or 2 (other values will assert()).
+ *
+ * This function is useful because device tree nodes often have cell arrays
+ * which are either lists of addresses or lists of address,size tuples, but
+ * the number of cells used for each element vary depending on the
+ * #address-cells and #size-cells properties of their parent node.

include/sysemu/device_tree.h int qemu_devtree_add_subnode(void *fdt, const char *name); · line 81

Quoted 10 lines
include/sysemu/device_tree.h +10 -0
+ * If you know all your cell elements are one cell wide you can use the
+ * simpler qemu_devtree_setprop_cells(). If you're not setting up the
+ * array programmatically, qemu_devtree_setprop_sized_cells may be more
+ * convenient.
+ *
+ * Return value: 0 on success, <0 if setting the property failed,
+ * n (for n>0) if value n wouldn't fit in the required number of cells.
+ * (This slightly odd convention is for the benefit of callers who might
+ * wish to print different error messages depending on which value
+ * was too large to fit, since values might be user-specified.)

I have patches on list converting the other "setprop" APIs to Error ** based reporting (as per your suggestion). Once that change happens we will have two separate error schemes for the one fn.

For sanity checking user specified values (e.g. command line args) cant it just be done explicitly as required? It seems to me that this will be the exception not the rule, as for DTB generation from machine model a fail here will be a fatal anyway (better handled by Error **).

if (acells < 2 && addr >= (1ull << 32)) {
fprintf("your address is too big for this dtb");
}

LOC looks to be similar and this API gets a little cleaner.

Regards,
Peter

#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/8] device_tree: Add qemu_devtree_setprop_sized_cells() utility functions

Quoted 9 lines · Peter Crosthwaite

I have patches on list converting the other "setprop" APIs to Error ** based reporting (as per your suggestion). Once that change happens we will have two separate error schemes for the one fn.

For sanity checking user specified values (e.g. command line args) cant it just be done explicitly as required? It seems to me that this will be the exception not the rule, as for DTB generation from machine model a fail here will be a fatal anyway (better handled by Error **).

Quoted 5 lines
if (acells < 2 && addr >= (1ull << 32)) {
   fprintf("your address is too big for this dtb");
}

LOC looks to be similar and this API gets a little cleaner.

It's trying to allow the arm/boot code to continue to distinguish the two error cases it does currently:
* RAM start address doesn't fit [a board model error]
* RAM size doesn't fit [a user error for which a specific error message is more friendly and helpful]

It's a fatal error in both cases, but the text is different.

I guess I could put a special purpose "if scells is 1 and ram_size > 4GB then fail early" check in boot.c...

I wanted to catch "user is using a DTB with only 32 bit addresses but tried a big fat RAM size", because some earlier versions of vexpress DTBs did only use 32 bit cells.

-- PMM

#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/8] device_tree: Add qemu_devtree_setprop_sized_cells() utility functions

Hi Peter,

Quoted 22 lines · Peter Maydell
Quoted 9 lines · Peter Crosthwaite

I have patches on list converting the other "setprop" APIs to Error ** based reporting (as per your suggestion). Once that change happens we will have two separate error schemes for the one fn.

For sanity checking user specified values (e.g. command line args) cant it just be done explicitly as required? It seems to me that this will be the exception not the rule, as for DTB generation from machine model a fail here will be a fatal anyway (better handled by Error **).

Quoted 5 lines
if (acells < 2 && addr >= (1ull << 32)) {
   fprintf("your address is too big for this dtb");
}

LOC looks to be similar and this API gets a little cleaner.

It's trying to allow the arm/boot code to continue to distinguish the two error cases it does currently:
* RAM start address doesn't fit [a board model error]
* RAM size doesn't fit [a user error for which a specific error message is more friendly and helpful]

Quoted 5 lines

It's a fatal error in both cases, but the text is different.

I guess I could put a special purpose "if scells is 1 and ram_size > 4GB then fail early" check in boot.c...

That was what I thinking with the above snippet. It goes with the caller in this case arm/boot.c. You still need to switch on the return code in the caller for your multiple error messages so I dont think you save too much save over just sanity checking args beforehand in boot.c

Regards,
Peter

Quoted 6 lines

I wanted to catch "user is using a DTB with only 32 bit addresses but tried a big fat RAM size", because some earlier versions of vexpress DTBs did only use 32 bit cells.

-- PMM

#5 lore raw

Re: [Qemu-devel] [PATCH v2 1/8] device_tree: Add qemu_devtree_setprop_sized_cells() utility functions

Quoted 9 lines · Peter Crosthwaite
Quoted 8 lines · Peter Maydell

It's trying to allow the arm/boot code to continue to distinguish the two error cases it does currently:
* RAM start address doesn't fit [a board model error]
* RAM size doesn't fit [a user error for which a specific error message is more friendly and helpful]

It's a fatal error in both cases, but the text is different.

Quoted 8 lines
Quoted 2 lines

I guess I could put a special purpose "if scells is 1 and ram_size > 4GB then fail early" check in boot.c...

That was what I thinking with the above snippet. It goes with the caller in this case arm/boot.c. You still need to switch on the return code in the caller for your multiple error messages so I dont think you save too much save over just sanity checking args beforehand in boot.c

Yeah, you've convinced me -- it's not worth complicating the heck out of the API for the generic function; I'll put an explicit check in boot.c

-- PMM

#6 lore raw

PATCH v2 2/8 arm/boot: Use qemu_devtree_setprop_sized_cells()

In reply to Peter Maydell

Replace the opencoded assembly of the reg property array for the /memory node with a call to qemu_devtree_setprop_sized_cells().

---
 hw/arm/boot.c |   28 +++++++---------------------
 1 file changed, 7 insertions(+), 21 deletions(-)
hw/arm/boot.c +7 -21
index a2e4032..1166632 100644
@@ -227,12 +227,10 @@ static void set_kernel_args_old(const struct arm_boot_info *info)
 
 static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo)
 {
-    uint32_t *mem_reg_property;
-    uint32_t mem_reg_propsize;
     void *fdt = NULL;
     char *filename;
     int size, rc;
-    uint32_t acells, scells, hival;
+    uint32_t acells, scells;
 
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
     if (!filename) {
@@ -255,30 +253,18 @@ static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo)
         goto fail;
     }
 
-    mem_reg_propsize = acells + scells;
-    mem_reg_property = g_new0(uint32_t, mem_reg_propsize);
-    mem_reg_property[acells - 1] = cpu_to_be32(binfo->loader_start);
-    hival = cpu_to_be32(binfo->loader_start >> 32);
-    if (acells > 1) {
-        mem_reg_property[acells - 2] = hival;
-    } else if (hival != 0) {
+    rc = qemu_devtree_setprop_sized_cells(fdt, "/memory", "reg",
+                                          acells, binfo->loader_start,
+                                          scells, binfo->ram_size);
+    if (rc == 1) {
         fprintf(stderr, "qemu: dtb file not compatible with "
                 "RAM start address > 4GB\n");
         goto fail;
-    }
-    mem_reg_property[acells + scells - 1] = cpu_to_be32(binfo->ram_size);
-    hival = cpu_to_be32(binfo->ram_size >> 32);
-    if (scells > 1) {
-        mem_reg_property[acells + scells - 2] = hival;
-    } else if (hival != 0) {
+    } else if (rc == 2) {
         fprintf(stderr, "qemu: dtb file not compatible with "
                 "RAM size > 4GB\n");
         goto fail;
-    }
-
-    rc = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
-                              mem_reg_propsize * sizeof(uint32_t));
-    if (rc < 0) {
+    } else if (rc != 0) {
         fprintf(stderr, "couldn't set /memory/reg\n");
         goto fail;
     }
Signature · 1 line
1.7.9.5
#7 lore raw

PATCH v2 3/8 virtio: Add support for guest setting of queue size

In reply to Peter Maydell

The MMIO virtio transport spec allows the guest to tell the host how large the queue size is. Add virtio_queue_set_num() function which implements this in the QEMU common virtio support code.

---
 hw/virtio/virtio.c         |    8 ++++++++
 include/hw/virtio/virtio.h |    1 +
 2 files changed, 9 insertions(+)
hw/virtio/virtio.c +8 -0
index 8176c14..01b05f3 100644
@@ -667,6 +667,14 @@ hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
     return vdev->vq[n].pa;
 }
 
+void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
+{
+    if (num <= VIRTQUEUE_MAX_SIZE) {
+        vdev->vq[n].vring.num = num;
+        virtqueue_init(&vdev->vq[n]);
+    }
+}
+
 int virtio_queue_get_num(VirtIODevice *vdev, int n)
 {
     return vdev->vq[n].vring.num;
include/hw/virtio/virtio.h +1 -0
index a6c5c53..95c4772 100644
@@ -198,6 +198,7 @@ void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
 void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr);
 hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n);
+void virtio_queue_set_num(VirtIODevice *vdev, int n, int num);
 int virtio_queue_get_num(VirtIODevice *vdev, int n);
 void virtio_queue_notify(VirtIODevice *vdev, int n);
 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
Signature · 1 line
1.7.9.5
#8 lore raw

Re: [Qemu-devel] [PATCH v2 3/8] virtio: Add support for guest setting of queue size

Quoted 5 lines · Peter Maydell

The MMIO virtio transport spec allows the guest to tell the host how large the queue size is. Add virtio_queue_set_num() function which implements this in the QEMU common virtio support code.

Probably needs to go back to default value on reset? Need to migrate?
Is the default value a max legal value? If yes probably a good idea to enforce this.

Quoted 39 lines
---
 hw/virtio/virtio.c         |    8 ++++++++
 include/hw/virtio/virtio.h |    1 +
 2 files changed, 9 insertions(+)
hw/virtio/virtio.c +8 -0
index 8176c14..01b05f3 100644
@@ -667,6 +667,14 @@ hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
     return vdev->vq[n].pa;
 }
 
+void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
+{
+    if (num <= VIRTQUEUE_MAX_SIZE) {
+        vdev->vq[n].vring.num = num;
+        virtqueue_init(&vdev->vq[n]);
+    }
+}
+
 int virtio_queue_get_num(VirtIODevice *vdev, int n)
 {
     return vdev->vq[n].vring.num;
include/hw/virtio/virtio.h +1 -0
index a6c5c53..95c4772 100644
@@ -198,6 +198,7 @@ void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
 void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr);
 hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n);
+void virtio_queue_set_num(VirtIODevice *vdev, int n, int num);
 int virtio_queue_get_num(VirtIODevice *vdev, int n);
 void virtio_queue_notify(VirtIODevice *vdev, int n);
 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
Signature · 1 line
1.7.9.5
#9 lore raw

Re: [Qemu-devel] [PATCH v2 3/8] virtio: Add support for guest setting of queue size

Quoted 8 lines · Michael S. Tsirkin
Quoted 5 lines · Peter Maydell

The MMIO virtio transport spec allows the guest to tell the host how large the queue size is. Add virtio_queue_set_num() function which implements this in the QEMU common virtio support code.

Probably needs to go back to default value on reset?

Tricky, since the default value is "whatever was passed to virtio_add_queue()" and we don't save that anywhere.

For virtio-mmio it is a guest bug to fail to write to the QueueNum register, so the current behaviour is not out of specification (and not harmful either AFAICT).

I guess we could add a vring.defaultnum, which would be set by virtio_add_queue/virtio_del_queue, and have reset copy defaultnum into num. No migration needed for defaultnum because it's always the same for a particular qemu config.

Quoted 1 line

Need to migrate?

It's already migrated (though I'm not entirely sure why).

Quoted 2 lines

Is the default value a max legal value? If yes probably a good idea to enforce this.

virtio_add_queue() already enforces this -- it will abort() if you try to set up a queue with a default size greater than VIRTQUEUE_MAX_SIZE.

thanks
-- PMM

#10 lore raw

Re: [Qemu-devel] [PATCH v2 3/8] virtio: Add support for guest setting of queue size

Quoted 8 lines · Peter Maydell
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Peter Maydell

The MMIO virtio transport spec allows the guest to tell the host how large the queue size is. Add virtio_queue_set_num() function which implements this in the QEMU common virtio support code.

Quoted 8 lines
Quoted 1 line

Probably needs to go back to default value on reset?

Tricky, since the default value is "whatever was passed to virtio_add_queue()" and we don't save that anywhere.

For virtio-mmio it is a guest bug to fail to write to the QueueNum register, so the current behaviour is not out of specification (and not harmful either AFAICT).

Best not to leak info across reboots.
Also if guest sets num = 0 it will cause all kind of harm, no?

Quoted 4 lines

I guess we could add a vring.defaultnum, which would be set by virtio_add_queue/virtio_del_queue, and have reset copy defaultnum into num. No migration needed for defaultnum because it's always the same for a particular qemu config.

Sounds good.

Quoted 10 lines
Quoted 1 line

Need to migrate?

It's already migrated (though I'm not entirely sure why).

Quoted 2 lines

Is the default value a max legal value? If yes probably a good idea to enforce this.

virtio_add_queue() already enforces this -- it will abort() if you try to set up a queue with a default size greater than VIRTQUEUE_MAX_SIZE.

No I mean does default value have some meaning?
Does host supply a hint of a max value
besides VIRTQUEUE_MAX_SIZE?

Quoted 3 lines

thanks
-- PMM

#11 lore raw

Re: [Qemu-devel] [PATCH v2 3/8] virtio: Add support for guest setting of queue size

Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Peter Maydell
Quoted 8 lines · Michael S. Tsirkin
Quoted 5 lines · Peter Maydell

The MMIO virtio transport spec allows the guest to tell the host how large the queue size is. Add virtio_queue_set_num() function which implements this in the QEMU common virtio support code.

Probably needs to go back to default value on reset?

Tricky, since the default value is "whatever was passed to virtio_add_queue()" and we don't save that anywhere.

Quoted 7 lines
Quoted 3 lines

For virtio-mmio it is a guest bug to fail to write to the QueueNum register, so the current behaviour is not out of specification (and not harmful either AFAICT).

Best not to leak info across reboots.
Also if guest sets num = 0 it will cause all kind of harm, no?

Yes, I had the thought on the way into work that we shouldn't allow the guest to switch between 0 and non-zero.

Quoted 11 lines
Quoted 4 lines

I guess we could add a vring.defaultnum, which would be set by virtio_add_queue/virtio_del_queue, and have reset copy defaultnum into num. No migration needed for defaultnum because it's always the same for a particular qemu config.

Sounds good.

Quoted 4 lines
Quoted 1 line

Need to migrate?

It's already migrated (though I'm not entirely sure why).

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Is the default value a max legal value? If yes probably a good idea to enforce this.

virtio_add_queue() already enforces this -- it will abort() if you try to set up a queue with a default size greater than VIRTQUEUE_MAX_SIZE.

No I mean does default value have some meaning?

It's not clear to me -- virtio_add_queue() doesn't document the semantics of its queue_size parameter.

Quoted 2 lines

Does host supply a hint of a max value
besides VIRTQUEUE_MAX_SIZE?

The spec says there's a QueueNumMax register which returns the maximum for the selected queue. The current implementation of virtio-mmio always returns VIRTQUEUE_MAX_SIZE. We could return the default size as the QueueNumMax instead but that would seem to be limiting the guest unnecessarily.

-- PMM

#12 lore raw

Re: [Qemu-devel] [PATCH v2 3/8] virtio: Add support for guest setting of queue size

In reply to Michael S. Tsirkin
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Peter Maydell
Quoted 1 line · Michael S. Tsirkin

Probably needs to go back to default value on reset?

Tricky, since the default value is "whatever was passed to virtio_add_queue()" and we don't save that anywhere.

Quoted 12 lines
Quoted 3 lines

For virtio-mmio it is a guest bug to fail to write to the QueueNum register, so the current behaviour is not out of specification (and not harmful either AFAICT).

Best not to leak info across reboots.
Also if guest sets num = 0 it will cause all kind of harm, no?

Quoted 4 lines

I guess we could add a vring.defaultnum, which would be set by virtio_add_queue/virtio_del_queue, and have reset copy defaultnum into num. No migration needed for defaultnum because it's always the same for a particular qemu config.

So I had a look at implementing this, and I noticed that we already have some odd behaviour on reset. Specifically, virtio backends like net can create virtio queues based on guest behaviour (ie setting feature bits). But on reset, these queues aren't deleted, so a post-reset QEMU will look different from a from-scratch restarted QEMU...

This in turn makes 'save defaultnum and have reset copy it into num' awkward, because defaultnum now needs to be migrated (otherwise it would do the wrong thing on a reset after a VM migration).

-- PMM

#13 lore raw

Re: [Qemu-devel] [PATCH v2 3/8] virtio: Add support for guest setting of queue size

Quoted 21 lines · Peter Maydell
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Peter Maydell
Quoted 1 line · Michael S. Tsirkin

Probably needs to go back to default value on reset?

Tricky, since the default value is "whatever was passed to virtio_add_queue()" and we don't save that anywhere.

Quoted 12 lines
Quoted 3 lines

For virtio-mmio it is a guest bug to fail to write to the QueueNum register, so the current behaviour is not out of specification (and not harmful either AFAICT).

Best not to leak info across reboots.
Also if guest sets num = 0 it will cause all kind of harm, no?

Quoted 4 lines

I guess we could add a vring.defaultnum, which would be set by virtio_add_queue/virtio_del_queue, and have reset copy defaultnum into num. No migration needed for defaultnum because it's always the same for a particular qemu config.

Quoted 6 lines

So I had a look at implementing this, and I noticed that we already have some odd behaviour on reset. Specifically, virtio backends like net can create virtio queues based on guest behaviour (ie setting feature bits). But on reset, these queues aren't deleted, so a post-reset QEMU will look different from a from-scratch restarted QEMU...

That's a bug. Thanks for the report. :)

Quoted 6 lines

This in turn makes 'save defaultnum and have reset copy it into num' awkward, because defaultnum now needs to be migrated (otherwise it would do the wrong thing on a reset after a VM migration).

-- PMM

Looks like we'll have to fix the bug first :(

#14 lore raw

PATCH v2 4/8 virtio: Support transports which can specify the vring alignment

In reply to Peter Maydell

Support virtio transports which can specify the vring alignment (ie where the guest communicates this to the host) by providing a new virtio_queue_set_align() function. (The default alignment remains as before.)

Transports which wish to make use of this must set the has_variable_vring_alignment field in their VirtioBusClass struct to true; they can then change the alignment via virtio_queue_set_align().

---
 hw/virtio/virtio.c             |   32 +++++++++++++++++++++++++++++---
 include/hw/virtio/virtio-bus.h |    6 ++++++
 include/hw/virtio/virtio.h     |    1 +
 3 files changed, 36 insertions(+), 3 deletions(-)
hw/virtio/virtio.c +29 -3
index 01b05f3..09f62c6 100644
@@ -19,8 +19,11 @@
 #include "qemu/atomic.h"
 #include "hw/virtio/virtio-bus.h"
 
-/* The alignment to use between consumer and producer parts of vring.
- * x86 pagesize again. */
+/*
+ * The alignment to use between consumer and producer parts of vring.
+ * x86 pagesize again. This is the default, used by transports like PCI
+ * which don't provide a means for the guest to tell the host the alignment.
+ */
 #define VIRTIO_PCI_VRING_ALIGN         4096
 
 typedef struct VRingDesc
@@ -54,6 +57,7 @@ typedef struct VRingUsed
 typedef struct VRing
 {
     unsigned int num;
+    unsigned int align;
     hwaddr desc;
     hwaddr avail;
     hwaddr used;
@@ -93,7 +97,7 @@ static void virtqueue_init(VirtQueue *vq)
     vq->vring.avail = pa + vq->vring.num * sizeof(VRingDesc);
     vq->vring.used = vring_align(vq->vring.avail +
                                  offsetof(VRingAvail, ring[vq->vring.num]),
-                                 VIRTIO_PCI_VRING_ALIGN);
+                                 vq->vring.align);
 }
 
 static inline uint64_t vring_desc_addr(hwaddr desc_pa, int i)
@@ -687,6 +691,21 @@ int virtio_queue_get_id(VirtQueue *vq)
     return vq - &vdev->vq[0];
 }
 
+void virtio_queue_set_align(VirtIODevice *vdev, int n, int align)
+{
+    BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
+    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
+
+    /* Check that the transport told us it was going to do this
+     * (so a buggy transport will immediately assert rather than
+     * silently failing to migrate this state)
+     */
+    assert(k->has_variable_vring_alignment);
+
+    vdev->vq[n].vring.align = align;
+    virtqueue_init(&vdev->vq[n]);
+}
+
 void virtio_queue_notify_vq(VirtQueue *vq)
 {
     if (vq->vring.desc) {
@@ -727,6 +746,7 @@ VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
         abort();
 
     vdev->vq[i].vring.num = queue_size;
+    vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN;
     vdev->vq[i].handle_output = handle_output;
 
     return &vdev->vq[i];
@@ -833,6 +853,9 @@ void virtio_save(VirtIODevice *vdev, QEMUFile *f)
             break;
 
         qemu_put_be32(f, vdev->vq[i].vring.num);
+        if (k->has_variable_vring_alignment) {
+            qemu_put_be32(f, vdev->vq[i].vring.align);
+        }
         qemu_put_be64(f, vdev->vq[i].pa);
         qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
         if (k->save_queue) {
@@ -889,6 +912,9 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f)
 
     for (i = 0; i < num; i++) {
         vdev->vq[i].vring.num = qemu_get_be32(f);
+        if (k->has_variable_vring_alignment) {
+            vdev->vq[i].vring.align = qemu_get_be32(f);
+        }
         vdev->vq[i].pa = qemu_get_be64(f);
         qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
         vdev->vq[i].signalled_used_valid = false;
include/hw/virtio/virtio-bus.h +6 -0
index 9ed60f9..9217f85 100644
@@ -62,6 +62,12 @@ typedef struct VirtioBusClass {
      * This is called by virtio-bus just before the device is unplugged.
      */
     void (*device_unplug)(DeviceState *d);
+    /*
+     * Does the transport have variable vring alignment?
+     * (ie can it ever call virtio_queue_set_align()?)
+     * Note that changing this will break migration for this transport.
+     */
+    bool has_variable_vring_alignment;
 } VirtioBusClass;
 
 struct VirtioBusState {
include/hw/virtio/virtio.h +1 -0
index 95c4772..6c2cee6 100644
@@ -200,6 +200,7 @@ void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr);
 hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n);
 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num);
 int virtio_queue_get_num(VirtIODevice *vdev, int n);
+void virtio_queue_set_align(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);
Signature · 1 line
1.7.9.5
#15 lore raw

PATCH v2 5/8 virtio: Implement MMIO based virtio transport

In reply to Peter Maydell

Add support for the generic MMIO based virtio transport.

This patch includes some fixes for bugs spotted by Ying-Shiuan Pan <[email protected]>.

[Fred changes: updated to new virtio-bus mechanisms]

[PMM changes:
* fixed trivial makefile conflict
* removed unused int_enable
* host_features doesn't need migrating
* reset guest accessible state in the reset function
* minor style fixes like extra blank lines
* RAZ/WI if there's no backend
* made transport size 0x200, in line with kvmtool
* set has_variable_vring_alignment
]

---
 hw/virtio/Makefile.objs |    1 +
 hw/virtio/virtio-mmio.c |  421 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 422 insertions(+)
 create mode 100644 hw/virtio/virtio-mmio.c
hw/virtio/Makefile.objs +1 -0
index cbe6d51..1ba53d9 100644
@@ -1,6 +1,7 @@
 common-obj-y += virtio-rng.o
 common-obj-$(CONFIG_VIRTIO_PCI) += virtio-pci.o
 common-obj-y += virtio-bus.o
+common-obj-y += virtio-mmio.o
 common-obj-$(CONFIG_VIRTIO_BLK_DATA_PLANE) += dataplane/
 
 obj-y += virtio.o virtio-balloon.o 
hw/virtio/virtio-mmio.c new file +421 -0

421 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#16 lore raw

PATCH v2 6/8 arm/boot: Allow boards to modify the FDT blob

In reply to Peter Maydell

Add a callback hook in arm_boot_info to allow board models to modify the device tree blob if they need to. (The major expected use case is to add virtio-mmio nodes for virtio-mmio transports that exist in QEMU but not in the hardware.)

---
 hw/arm/boot.c        |    5 +++++
 include/hw/arm/arm.h |    4 ++++
 2 files changed, 9 insertions(+)
hw/arm/boot.c +5 -0
index 1166632..72ae562 100644
@@ -293,6 +293,11 @@ static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo)
             goto fail;
         }
     }
+
+    if (binfo->modify_dtb) {
+        binfo->modify_dtb(binfo, fdt);
+    }
+
     qemu_devtree_dumpdtb(fdt, size);
 
     cpu_physical_memory_write(addr, fdt, size);
include/hw/arm/arm.h +4 -0
index 7b2b02d..bae87c6 100644
@@ -55,6 +55,10 @@ struct arm_boot_info {
                                  const struct arm_boot_info *info);
     void (*secondary_cpu_reset_hook)(ARMCPU *cpu,
                                      const struct arm_boot_info *info);
+    /* if a board needs to be able to modify a device tree provided by
+     * the user it should implement this hook.
+     */
+    void (*modify_dtb)(const struct arm_boot_info *info, void *fdt);
     /* Used internally by arm_boot.c */
     int is_linux;
     hwaddr initrd_start;
Signature · 1 line
1.7.9.5
#17 lore raw

PATCH v2 7/8 vexpress: Make VEDBoardInfo extend arm_boot_info

In reply to Peter Maydell

Make the VEDBoardInfo struct extend arm_boot_info; this will allow us to get at the VEDBoardInfo information inside callbacks from arm/boot code.

---
 hw/arm/vexpress.c |   31 +++++++++++++++----------------
 1 file changed, 15 insertions(+), 16 deletions(-)
hw/arm/vexpress.c +15 -16
index fd18b60..d1c28aa 100644
@@ -36,8 +36,6 @@
 #define VEXPRESS_FLASH_SIZE (64 * 1024 * 1024)
 #define VEXPRESS_FLASH_SECT_SIZE (256 * 1024)
 
-static struct arm_boot_info vexpress_binfo;
-
 /* Address maps for peripherals:
  * the Versatile Express motherboard has two possible maps,
  * the "legacy" one (used for A9) and the "Cortex-A Series"
@@ -150,6 +148,7 @@ typedef void DBoardInitFn(const VEDBoardInfo *daughterboard,
                           qemu_irq *pic);
 
 struct VEDBoardInfo {
+    struct arm_boot_info bootinfo;
     const hwaddr *motherboard_map;
     hwaddr loader_start;
     const hwaddr gic_cpu_if_addr;
@@ -269,7 +268,7 @@ static const uint32_t a9_clocks[] = {
     66670000, /* Test chip reference clock: 66.67MHz */
 };
 
-static const VEDBoardInfo a9_daughterboard = {
+static VEDBoardInfo a9_daughterboard = {
     .motherboard_map = motherboard_legacy_map,
     .loader_start = 0x60000000,
     .gic_cpu_if_addr = 0x1e000100,
@@ -381,7 +380,7 @@ static const uint32_t a15_clocks[] = {
     40000000, /* OSCCLK8: 40MHz : DDR2 PLL reference */
 };
 
-static const VEDBoardInfo a15_daughterboard = {
+static VEDBoardInfo a15_daughterboard = {
     .motherboard_map = motherboard_aseries_map,
     .loader_start = 0x80000000,
     .gic_cpu_if_addr = 0x2c002000,
@@ -393,7 +392,7 @@ static const VEDBoardInfo a15_daughterboard = {
     .init = a15_daughterboard_init,
 };
 
-static void vexpress_common_init(const VEDBoardInfo *daughterboard,
+static void vexpress_common_init(VEDBoardInfo *daughterboard,
                                  QEMUMachineInitArgs *args)
 {
     DeviceState *dev, *sysctl, *pl041;
@@ -509,17 +508,17 @@ static void vexpress_common_init(const VEDBoardInfo *daughterboard,
 
     /* VE_DAPROM: not modelled */
 
-    vexpress_binfo.ram_size = args->ram_size;
-    vexpress_binfo.kernel_filename = args->kernel_filename;
-    vexpress_binfo.kernel_cmdline = args->kernel_cmdline;
-    vexpress_binfo.initrd_filename = args->initrd_filename;
-    vexpress_binfo.nb_cpus = smp_cpus;
-    vexpress_binfo.board_id = VEXPRESS_BOARD_ID;
-    vexpress_binfo.loader_start = daughterboard->loader_start;
-    vexpress_binfo.smp_loader_start = map[VE_SRAM];
-    vexpress_binfo.smp_bootreg_addr = map[VE_SYSREGS] + 0x30;
-    vexpress_binfo.gic_cpu_if_addr = daughterboard->gic_cpu_if_addr;
-    arm_load_kernel(ARM_CPU(first_cpu), &vexpress_binfo);
+    daughterboard->bootinfo.ram_size = args->ram_size;
+    daughterboard->bootinfo.kernel_filename = args->kernel_filename;
+    daughterboard->bootinfo.kernel_cmdline = args->kernel_cmdline;
+    daughterboard->bootinfo.initrd_filename = args->initrd_filename;
+    daughterboard->bootinfo.nb_cpus = smp_cpus;
+    daughterboard->bootinfo.board_id = VEXPRESS_BOARD_ID;
+    daughterboard->bootinfo.loader_start = daughterboard->loader_start;
+    daughterboard->bootinfo.smp_loader_start = map[VE_SRAM];
+    daughterboard->bootinfo.smp_bootreg_addr = map[VE_SYSREGS] + 0x30;
+    daughterboard->bootinfo.gic_cpu_if_addr = daughterboard->gic_cpu_if_addr;
+    arm_load_kernel(ARM_CPU(first_cpu), &daughterboard->bootinfo);
 }
 
 static void vexpress_a9_init(QEMUMachineInitArgs *args)
Signature · 1 line
1.7.9.5
#18 lore raw

Re: [Qemu-devel] [PATCH v2 7/8] vexpress: Make VEDBoardInfo extend arm_boot_info

Hi Peter,

Quoted 31 lines · Peter Maydell

Make the VEDBoardInfo struct extend arm_boot_info; this will allow us to get at the VEDBoardInfo information inside callbacks from arm/boot code.

---
 hw/arm/vexpress.c |   31 +++++++++++++++----------------
 1 file changed, 15 insertions(+), 16 deletions(-)
hw/arm/vexpress.c +1 -2
index fd18b60..d1c28aa 100644
@@ -36,8 +36,6 @@
 #define VEXPRESS_FLASH_SIZE (64 * 1024 * 1024)
 #define VEXPRESS_FLASH_SECT_SIZE (256 * 1024)
 
-static struct arm_boot_info vexpress_binfo;
-
 /* Address maps for peripherals:
  * the Versatile Express motherboard has two possible maps,
  * the "legacy" one (used for A9) and the "Cortex-A Series"
@@ -150,6 +148,7 @@ typedef void DBoardInitFn(const VEDBoardInfo *daughterboard,
                           qemu_irq *pic);
 
 struct VEDBoardInfo {
+    struct arm_boot_info bootinfo;
     const hwaddr *motherboard_map;
     hwaddr loader_start;
     const hwaddr gic_cpu_if_addr;

hw/arm/vexpress.c static const uint32_t a9_clocks[] = { · line 271

Quoted 5 lines
hw/arm/vexpress.c +1 -1
@@ -269,7 +268,7 @@ static const uint32_t a9_clocks[] = {
     66670000, /* Test chip reference clock: 66.67MHz */
 };
 
-static const VEDBoardInfo a9_daughterboard = {
+static VEDBoardInfo a9_daughterboard = {

Wouldn't it be cleaner to just add opaque data to arm_boot_info?, then you don't have to promote this previously const struct to global state.

Regards,
Peter

#19 lore raw

Re: [Qemu-devel] [PATCH v2 7/8] vexpress: Make VEDBoardInfo extend arm_boot_info

Quoted 37 lines · Peter Crosthwaite

Hi Peter,

Quoted 34 lines · Peter Maydell

Make the VEDBoardInfo struct extend arm_boot_info; this will allow us to get at the VEDBoardInfo information inside callbacks from arm/boot code.

---
 hw/arm/vexpress.c |   31 +++++++++++++++----------------
 1 file changed, 15 insertions(+), 16 deletions(-)
hw/arm/vexpress.c +1 -2
index fd18b60..d1c28aa 100644
@@ -36,8 +36,6 @@
 #define VEXPRESS_FLASH_SIZE (64 * 1024 * 1024)
 #define VEXPRESS_FLASH_SECT_SIZE (256 * 1024)
 
-static struct arm_boot_info vexpress_binfo;
-
 /* Address maps for peripherals:
  * the Versatile Express motherboard has two possible maps,
  * the "legacy" one (used for A9) and the "Cortex-A Series"
@@ -150,6 +148,7 @@ typedef void DBoardInitFn(const VEDBoardInfo *daughterboard,
                           qemu_irq *pic);
 
 struct VEDBoardInfo {
+    struct arm_boot_info bootinfo;
     const hwaddr *motherboard_map;
     hwaddr loader_start;
     const hwaddr gic_cpu_if_addr;
@@ -269,7 +268,7 @@ static const uint32_t a9_clocks[] = {
     66670000, /* Test chip reference clock: 66.67MHz */
 };
 
Quoted 5 lines

hw/arm/vexpress.c static const uint32_t a9_clocks[] = { · line 271

Quoted 2 lines
hw/arm/vexpress.c +1 -1
-static const VEDBoardInfo a9_daughterboard = {
+static VEDBoardInfo a9_daughterboard = {

Wouldn't it be cleaner to just add opaque data to arm_boot_info?, then you don't have to promote this previously const struct to global state.

I thought about that but decided I didn't really want to throw an opaque into the boot_info structure when the callbacks already get a pointer to state.

-- PMM

#20 lore raw

PATCH v2 8/8 vexpress: Add virtio-mmio transports

In reply to Peter Maydell

Add some virtio-mmio transports to the vexpress board model, together with a modify_dtb hook which adds them to the device tree so that the kernel will probe for them. We put them in a reserved area of the address map.

---
 hw/arm/vexpress.c |   99 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)
hw/arm/vexpress.c +99 -0
index d1c28aa..dcc86b9 100644
@@ -31,11 +31,18 @@
 #include "exec/address-spaces.h"
 #include "sysemu/blockdev.h"
 #include "hw/block/flash.h"
+#include "sysemu/device_tree.h"
+#include <libfdt.h>
 
 #define VEXPRESS_BOARD_ID 0x8e0
 #define VEXPRESS_FLASH_SIZE (64 * 1024 * 1024)
 #define VEXPRESS_FLASH_SECT_SIZE (256 * 1024)
 
+/* Number of virtio transports to create (0..8; limited by
+ * number of available IRQ lines).
+ */
+#define NUM_VIRTIO_TRANSPORTS 4
+
 /* Address maps for peripherals:
  * the Versatile Express motherboard has two possible maps,
  * the "legacy" one (used for A9) and the "Cortex-A Series"
@@ -70,6 +77,7 @@ enum {
     VE_ETHERNET,
     VE_USB,
     VE_DAPROM,
+    VE_VIRTIO,
 };
 
 static hwaddr motherboard_legacy_map[] = {
@@ -88,6 +96,7 @@ static hwaddr motherboard_legacy_map[] = {
     [VE_WDT] = 0x1000f000,
     [VE_TIMER01] = 0x10011000,
     [VE_TIMER23] = 0x10012000,
+    [VE_VIRTIO] = 0x10013000,
     [VE_SERIALDVI] = 0x10016000,
     [VE_RTC] = 0x10017000,
     [VE_COMPACTFLASH] = 0x1001a000,
@@ -132,6 +141,7 @@ static hwaddr motherboard_aseries_map[] = {
     [VE_WDT] = 0x1c0f0000,
     [VE_TIMER01] = 0x1c110000,
     [VE_TIMER23] = 0x1c120000,
+    [VE_VIRTIO] = 0x1c130000,
     [VE_SERIALDVI] = 0x1c160000,
     [VE_RTC] = 0x1c170000,
     [VE_COMPACTFLASH] = 0x1c1a0000,
@@ -392,6 +402,85 @@ static VEDBoardInfo a15_daughterboard = {
     .init = a15_daughterboard_init,
 };
 
+static int add_virtio_mmio_node(void *fdt, uint32_t acells, uint32_t scells,
+                                hwaddr addr, hwaddr size, uint32_t intc,
+                                int irq)
+{
+    /* Add a virtio_mmio node to the device tree blob:
+     *   virtio_mmio@ADDRESS {
+     *       compatible = "virtio,mmio";
+     *       reg = <ADDRESS, SIZE>;
+     *       interrupt-parent = <&intc>;
+     *       interrupts = <0, irq, 1>;
+     *   }
+     * (Note that the format of the interrupts property is dependent on the
+     * interrupt controller that interrupt-parent points to; these are for
+     * the ARM GIC and indicate an SPI interrupt, rising-edge-triggered.)
+     */
+    int rc;
+    char *nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, addr);
+
+    rc = qemu_devtree_add_subnode(fdt, nodename);
+    rc |= qemu_devtree_setprop_string(fdt, nodename,
+                                      "compatible", "virtio,mmio");
+    rc |= qemu_devtree_setprop_sized_cells(fdt, nodename, "reg",
+                                           acells, addr, scells, size);
+    qemu_devtree_setprop_cells(fdt, nodename, "interrupt-parent", intc);
+    qemu_devtree_setprop_cells(fdt, nodename, "interrupts", 0, irq, 1);
+    g_free(nodename);
+    if (rc) {
+        return -1;
+    }
+    return 0;
+}
+
+static uint32_t find_int_controller(void *fdt)
+{
+    /* Find the FDT node corresponding to the interrupt controller
+     * for virtio-mmio devices. We do this by scanning the fdt for
+     * a node with the right compatibility, since we know there is
+     * only one GIC on a vexpress board.
+     * We return the phandle of the node, or 0 if none was found.
+     */
+    const char *compat = "arm,cortex-a9-gic";
+    int offset;
+
+    offset = fdt_node_offset_by_compatible(fdt, -1, compat);
+    if (offset >= 0) {
+        return fdt_get_phandle(fdt, offset);
+    }
+    return 0;
+}
+
+static void vexpress_modify_dtb(const struct arm_boot_info *info, void *fdt)
+{
+    uint32_t acells, scells, intc;
+    const VEDBoardInfo *daughterboard = (const VEDBoardInfo *)info;
+
+    acells = qemu_devtree_getprop_cell(fdt, "/", "#address-cells");
+    scells = qemu_devtree_getprop_cell(fdt, "/", "#size-cells");
+    intc = find_int_controller(fdt);
+    if (!intc) {
+        /* Not fatal, we just won't provide virtio. This will
+         * happen with older device tree blobs.
+         */
+        fprintf(stderr, "QEMU: warning: couldn't find interrupt controller in "
+                "dtb; will not include virtio-mmio devices in the dtb.\n");
+    } else {
+        int i;
+        const hwaddr *map = daughterboard->motherboard_map;
+
+        /* We iterate backwards here because adding nodes
+         * to the dtb puts them in last-first.
+         */
+        for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) {
+            add_virtio_mmio_node(fdt, acells, scells,
+                                 map[VE_VIRTIO] + 0x200 * i,
+                                 0x200, intc, 40 + i);
+        }
+    }
+}
+
 static void vexpress_common_init(VEDBoardInfo *daughterboard,
                                  QEMUMachineInitArgs *args)
 {
@@ -508,6 +597,15 @@ static void vexpress_common_init(VEDBoardInfo *daughterboard,
 
     /* VE_DAPROM: not modelled */
 
+    /* Create mmio transports, so the user can create virtio backends
+     * (which will be automatically plugged in to the transports). If
+     * no backend is created the transport will just sit harmlessly idle.
+     */
+    for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) {
+        sysbus_create_simple("virtio-mmio", map[VE_VIRTIO] + 0x200 * i,
+                             pic[40 + i]);
+    }
+
     daughterboard->bootinfo.ram_size = args->ram_size;
     daughterboard->bootinfo.kernel_filename = args->kernel_filename;
     daughterboard->bootinfo.kernel_cmdline = args->kernel_cmdline;
@@ -518,6 +616,7 @@ static void vexpress_common_init(VEDBoardInfo *daughterboard,
     daughterboard->bootinfo.smp_loader_start = map[VE_SRAM];
     daughterboard->bootinfo.smp_bootreg_addr = map[VE_SYSREGS] + 0x30;
     daughterboard->bootinfo.gic_cpu_if_addr = daughterboard->gic_cpu_if_addr;
+    daughterboard->bootinfo.modify_dtb = vexpress_modify_dtb;
     arm_load_kernel(ARM_CPU(first_cpu), &daughterboard->bootinfo);
 }
 
Signature · 1 line
1.7.9.5
#21 lore raw

Re: [Qemu-devel] [PATCH v2 8/8] vexpress: Add virtio-mmio transports

Hi,

Quoted 58 lines · Peter Maydell

Add some virtio-mmio transports to the vexpress board model, together with a modify_dtb hook which adds them to the device tree so that the kernel will probe for them. We put them in a reserved area of the address map.

---
 hw/arm/vexpress.c |   99 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)
hw/arm/vexpress.c +10 -0
index d1c28aa..dcc86b9 100644
@@ -31,11 +31,18 @@
 #include "exec/address-spaces.h"
 #include "sysemu/blockdev.h"
 #include "hw/block/flash.h"
+#include "sysemu/device_tree.h"
+#include <libfdt.h>
 
 #define VEXPRESS_BOARD_ID 0x8e0
 #define VEXPRESS_FLASH_SIZE (64 * 1024 * 1024)
 #define VEXPRESS_FLASH_SECT_SIZE (256 * 1024)
 
+/* Number of virtio transports to create (0..8; limited by
+ * number of available IRQ lines).
+ */
+#define NUM_VIRTIO_TRANSPORTS 4
+
 /* Address maps for peripherals:
  * the Versatile Express motherboard has two possible maps,
  * the "legacy" one (used for A9) and the "Cortex-A Series"
@@ -70,6 +77,7 @@ enum {
     VE_ETHERNET,
     VE_USB,
     VE_DAPROM,
+    VE_VIRTIO,
 };
 
 static hwaddr motherboard_legacy_map[] = {
@@ -88,6 +96,7 @@ static hwaddr motherboard_legacy_map[] = {
     [VE_WDT] = 0x1000f000,
     [VE_TIMER01] = 0x10011000,
     [VE_TIMER23] = 0x10012000,
+    [VE_VIRTIO] = 0x10013000,
     [VE_SERIALDVI] = 0x10016000,
     [VE_RTC] = 0x10017000,
     [VE_COMPACTFLASH] = 0x1001a000,
@@ -132,6 +141,7 @@ static hwaddr motherboard_aseries_map[] = {
     [VE_WDT] = 0x1c0f0000,
     [VE_TIMER01] = 0x1c110000,
     [VE_TIMER23] = 0x1c120000,
+    [VE_VIRTIO] = 0x1c130000,
     [VE_SERIALDVI] = 0x1c160000,
     [VE_RTC] = 0x1c170000,
     [VE_COMPACTFLASH] = 0x1c1a0000,

hw/arm/vexpress.c static VEDBoardInfo a15_daughterboard = { · line 407

Quoted 6 lines
hw/arm/vexpress.c +3 -0
@@ -392,6 +402,85 @@ static VEDBoardInfo a15_daughterboard = {
     .init = a15_daughterboard_init,
 };
 
+static int add_virtio_mmio_node(void *fdt, uint32_t acells, uint32_t scells,
+                                hwaddr addr, hwaddr size, uint32_t intc,
+                                int irq)

This seems pretty general to me and maybe belongs in the device tree API. Some of the PPC machines could make good use of this. From e500:

snprintf(mpic, sizeof(mpic), "%s/pic@%llx", soc, MPC8544_MPIC_REGS_OFFSET);
    qemu_devtree_add_subnode(fdt, mpic);
    qemu_devtree_setprop_string(fdt, mpic, "device_type", "open-pic");
    qemu_devtree_setprop_string(fdt, mpic, "compatible", "fsl,mpic");
    qemu_devtree_setprop_cells(fdt, mpic, "reg", MPC8544_MPIC_REGS_OFFSET,
                               0x40000);
    qemu_devtree_setprop_cell(fdt, mpic, "#address-cells", 0);
    qemu_devtree_setprop_cell(fdt, mpic, "#interrupt-cells", 2);

The nasty intc specific stuff is a problem, but perhaps at least the boiler plate
creation+reg+compatibile can be factored out for all to use?

Regards,
Peter

#22 lore raw

Re: [Qemu-devel] [PATCH v2 8/8] vexpress: Add virtio-mmio transports

Quoted 17 lines · Peter Crosthwaite

hw/arm/vexpress.c static VEDBoardInfo a15_daughterboard = { · line 407

Quoted 3 lines · Peter Maydell
hw/arm/vexpress.c +3 -0
+static int add_virtio_mmio_node(void *fdt, uint32_t acells, uint32_t scells,
+                                hwaddr addr, hwaddr size, uint32_t intc,
+                                int irq)

This seems pretty general to me and maybe belongs in the device tree API. Some of the PPC machines could make good use of this. From e500:

snprintf(mpic, sizeof(mpic), "%s/pic@%llx", soc, MPC8544_MPIC_REGS_OFFSET);
    qemu_devtree_add_subnode(fdt, mpic);
    qemu_devtree_setprop_string(fdt, mpic, "device_type", "open-pic");
    qemu_devtree_setprop_string(fdt, mpic, "compatible", "fsl,mpic");
    qemu_devtree_setprop_cells(fdt, mpic, "reg", MPC8544_MPIC_REGS_OFFSET,
                               0x40000);
    qemu_devtree_setprop_cell(fdt, mpic, "#address-cells", 0);
    qemu_devtree_setprop_cell(fdt, mpic, "#interrupt-cells", 2);
Quoted 3 lines

The nasty intc specific stuff is a problem, but perhaps at least the boiler plate
creation+reg+compatibile can be factored out for all to use?

Doesn't really seem worth doing to me -- all we're doing is setting some string properties, and the reg property is tricky because it's only the right thing in some cases (ie where you know you're not putting the node inside some other container that messes with ranges). The setprop_sized_cells is the part I felt really merited being factored out.

thanks
-- PMM