Add macros to read/write registers of virtio-1.0 regions.
Signed-off-by: Gerd Hoffmann kraxel@redhat.com --- src/hw/virtio-pci.h | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+)
diff --git a/src/hw/virtio-pci.h b/src/hw/virtio-pci.h index 893a7dd..e1d8b3e 100644 --- a/src/hw/virtio-pci.h +++ b/src/hw/virtio-pci.h @@ -111,6 +111,82 @@ struct vp_device { struct vp_cap common, notify, isr, device; };
+#define vp_modern_read(_cap, _struct, _field, _var) { \ + u32 addr = _cap.addr; \ + addr += offsetof(_struct, _field); \ + if (_cap.is_io) { \ + switch (sizeof(((_struct *)0)->_field)) { \ + case 8: \ + _var = inl(addr); \ + _var |= (u64)inl(addr+4) << 32; \ + break; \ + case 4: \ + _var = inl(addr); \ + break; \ + case 2: \ + _var = inw(addr); \ + break; \ + case 1: \ + _var = inb(addr); \ + break; \ + default: \ + _var = 0; \ + } \ + } else { \ + switch (sizeof(((_struct *)0)->_field)) { \ + case 8: \ + _var = readl((void*)addr); \ + _var |= (u64)readl((void*)(addr+4)) << 32; \ + break; \ + case 4: \ + _var = readl((void*)addr); \ + break; \ + case 2: \ + _var = readw((void*)addr); \ + break; \ + case 1: \ + _var = readb((void*)addr); \ + break; \ + default: \ + _var = 0; \ + } \ + } \ + dprintf(9, "vp read %x (%d) -> 0x%x\n", \ + addr, sizeof(((_struct *)0)->_field), (u32)_var); \ + } + +#define vp_modern_write(_cap, _struct, _field, _var) { \ + u32 addr = _cap.addr; \ + addr += offsetof(_struct, _field); \ + dprintf(9, "vp write %x (%d) <- 0x%x\n", \ + addr, sizeof(((_struct *)0)->_field), (u32)_var); \ + if (_cap.is_io) { \ + switch (sizeof(((_struct *)0)->_field)) { \ + case 4: \ + outl(_var, addr); \ + break; \ + case 2: \ + outw(_var, addr); \ + break; \ + case 1: \ + outb(_var, addr); \ + break; \ + } \ + } else { \ + switch (sizeof(((_struct *)0)->_field)) { \ + case 4: \ + writel((void*)addr, _var); \ + break; \ + case 2: \ + writew((void*)addr, _var); \ + break; \ + case 1: \ + writeb((void*)addr, _var); \ + break; \ + } \ + } \ + } + static inline u32 vp_get_features(struct vp_device *vp) { return inl(GET_LOWFLAT(vp->ioaddr) + VIRTIO_PCI_HOST_FEATURES);