qemu-kvm: MSI layer rework for in-kernel irqchip support
RFC PATCH 45/45 pci-assign: Fix coding style issues
In reply to Jan KiszkaAlso remove the dead get_assigned_device at this chance. No functional changes.
--- hw/device-assignment.c | 199 ++++++++++++++++++++++++------------------------ hw/device-assignment.h | 14 ++-- 2 files changed, 107 insertions(+), 106 deletions(-)
hw/device-assignment.c +100 -99
@@ -58,10 +58,10 @@#ifdef DEVICE_ASSIGNMENT_DEBUG #define DEBUG (fmt, ...) \ do { \ - fprintf(stderr, "%s: " fmt, __func__ , __VA_ARGS__); \ + fprintf(stderr, "%s: " fmt, __func__ , __VA_ARGS__); \ } while (0)#else -#define DEBUG (fmt, ...) do { } while(0) +#define DEBUG (fmt, ...) do { } while (0)#endif static void assigned_dev_load_option_rom (AssignedDevice *dev);
@@ -97,27 +97,27 @@ static uint32_t assigned_dev_ioport_rw(AssignedDevRegion *dev_region,DEBUG ("out val=%x, len=%d, e_phys=%x, host=%x\n" , *val, len, addr, port);switch (len) { -case 1 : -outb (*val, port); -break ; -case 2 : -outw (*val, port); -break ; -case 4 : -outl (*val, port); -break ; +case 1 : +outb (*val, port); +break ; +case 2 : +outw (*val, port); +break ; +case 4 : +outl (*val, port); +break ; } }else {switch (len) { -case 1 : - ret =inb (port); -break ; -case 2 : - ret =inw (port); -break ; -case 4 : - ret =inl (port); -break ; +case 1 : + ret =inb (port); +break ; +case 2 : + ret =inw (port); +break ; +case 4 : + ret =inl (port); +break ; }DEBUG ("in val=%x, len=%d, e_phys=%x, host=%x\n" , ret, len, addr, port);
@@ -130,21 +130,18 @@ static void assigned_dev_ioport_writeb(void *opaque, uint32_t addr, uint32_t value) {assigned_dev_ioport_rw (opaque, addr,1 , &value); -return ; }static void assigned_dev_ioport_writew (void *opaque,uint32_t addr,uint32_t value) {assigned_dev_ioport_rw (opaque, addr,2 , &value); -return ; }static void assigned_dev_ioport_writel (void *opaque,uint32_t addr,uint32_t value) {assigned_dev_ioport_rw (opaque, addr,4 , &value); -return ; }static uint32_t assigned_dev_ioport_readb (void *opaque,uint32_t addr)
@@ -295,13 +292,13 @@ static uint32_t assigned_dev_pci_read(PCIDevice *d, int pos, int len) again: ret =pread (fd, &val, len, pos);if (ret != len) { -if ((ret <0 ) && (errno ==EINTR || errno ==EAGAIN )) - goto again; - -fprintf (stderr,"%s: pread failed, ret = %zd errno = %d\n" , - __func__, ret, errno); +if ((ret <0 ) && (errno ==EINTR || errno ==EAGAIN )) { + goto again; + } +fprintf (stderr,"%s: pread failed, ret = %zd errno = %d\n" , + __func__, ret, errno); -exit (1 ); +exit (1 ); }return val;
@@ -321,16 +318,14 @@ static void assigned_dev_pci_write(PCIDevice *d, int pos, uint32_t val, int len) again: ret =pwrite (fd, &val, len, pos);if (ret != len) { -if ((ret <0 ) && (errno ==EINTR || errno ==EAGAIN )) - goto again; - -fprintf (stderr,"%s: pwrite failed, ret = %zd errno = %d\n" , - __func__, ret, errno); +if ((ret <0 ) && (errno ==EINTR || errno ==EAGAIN )) { + goto again; + } +fprintf (stderr,"%s: pwrite failed, ret = %zd errno = %d\n" , + __func__, ret, errno); -exit (1 ); +exit (1 ); } - -return ; } static void assigned_dev_emulate_config_read(AssignedDevice *dev,
@@ -359,22 +354,24 @@ static uint8_t pci_find_cap_offset(PCIDevice *d, uint8_t cap, uint8_t start)int status; status =assigned_dev_pci_read_byte (d,PCI_STATUS ); -if ((status &PCI_STATUS_CAP_LIST ) ==0 ) +if ((status &PCI_STATUS_CAP_LIST ) ==0 ) {return 0 ; + }while (max_cap--) { pos =assigned_dev_pci_read_byte (d, pos); -if (pos <0x40 ) +if (pos <0x40 ) {break ; - + } pos &= ~3 ; id =assigned_dev_pci_read_byte (d, pos +PCI_CAP_LIST_ID ); -if (id ==0xff ) +if (id ==0xff ) {break ; -if (id == cap) + } +if (id == cap) {return pos; - + } pos +=PCI_CAP_LIST_NEXT ; }return 0 ;
@@ -388,8 +385,9 @@ static int assigned_dev_register_regions(PCIRegion *io_regions,PCIRegion *cur_region = io_regions;for (i =0 ; i < regions_num; i++, cur_region++) { -if (!cur_region->valid) +if (!cur_region->valid) {continue ; + } pci_dev->v_addrs[i].num = i;/* handle memory io regions */
@@ -527,7 +525,7 @@ static int get_real_device(AssignedDevice *pci_dev, uint16_t r_seg, dev->region_number =0 ;snprintf (dir,sizeof (dir),"/sys/bus/pci/devices/%04x:%02x:%02x.%x/" , - r_seg, r_bus, r_dev, r_func); + r_seg, r_bus, r_dev, r_func);snprintf (name,sizeof (name),"%sconfig" , dir);
@@ -554,8 +552,9 @@ again: r =read (dev->config_fd, pci_dev->dev.config,pci_config_size (&pci_dev->dev));if (r <0 ) { -if (errno ==EINTR || errno ==EAGAIN ) +if (errno ==EINTR || errno ==EAGAIN ) { goto again; + }fprintf (stderr,"%s: read failed, errno = %d\n" , __func__, errno); }
@@ -574,16 +573,17 @@ again: }for (r =0 ; r <PCI_ROM_SLOT ; r++) { -if (fscanf (f,"%lli %lli %lli\n" , &start, &end, &flags) !=3 ) -break ; - +if (fscanf (f,"%lli %lli %lli\n" , &start, &end, &flags) !=3 ) { +break ; + } rp = dev->regions + r; rp->valid =0 ; rp->resource_fd =-1 ; size = end - start +1 ; flags &=IORESOURCE_IO |IORESOURCE_MEM |IORESOURCE_PREFETCH ; -if (size ==0 || (flags & ~IORESOURCE_PREFETCH ) ==0 ) +if (size ==0 || (flags & ~IORESOURCE_PREFETCH ) ==0 ) {continue ; + }if (flags &IORESOURCE_MEM ) { flags &= ~IORESOURCE_IO ; }else {
@@ -591,8 +591,9 @@ again: }snprintf (name,sizeof (name),"%sresource%d" , dir, r); fd =open (name,O_RDWR ); -if (fd ==-1 ) +if (fd ==-1 ) {continue ; + } rp->resource_fd = fd; rp->type = flags;
@@ -704,7 +705,8 @@ static void assign_failed_examine(AssignedDevice *dev)sprintf (name,"%sdriver" , dir); r =readlink (name, driver,sizeof (driver)); -if ((r <=0 ) || r >=sizeof (driver) || !(ns =strrchr (driver,'/' ))) { + ns =strrchr (driver,'/' ); +if (r <=0 || r >=sizeof (driver) || ns ==NULL ) { goto fail; }
@@ -780,11 +782,11 @@ static int assign_device(AssignedDevice *dev) dev->dev.qdev.id, strerror(-r));switch (r) { -case -EBUSY : -assign_failed_examine (dev); -break ; -default : -break ; +case -EBUSY : +assign_failed_examine (dev); +break ; +default : +break ; } }return r;
@@ -812,9 +814,9 @@ static int assign_intx(AssignedDevice *dev)int irq, r;/* Interrupt PIN 0 means don't use INTx */ -if (assigned_dev_pci_read_byte (&dev->dev,PCI_INTERRUPT_PIN ) ==0 ) +if (assigned_dev_pci_read_byte (&dev->dev,PCI_INTERRUPT_PIN ) ==0 ) {return 0 ; - + } irq =pci_map_irq (&dev->dev, dev->intpin); irq =piix_get_irq (irq);
@@ -856,27 +858,11 @@ static void deassign_device(AssignedDevice *dev) assigned_dev_data.assigned_dev_id =calc_assigned_dev_id (dev); r =kvm_deassign_pci_device (kvm_state, &assigned_dev_data); -if (r <0 ) -fprintf (stderr,"Failed to deassign device \"%s\" : %s\n" , +if (r <0 ) { +fprintf (stderr,"Failed to deassign device \"%s\" : %s\n" , dev->dev.qdev.id,strerror (-r)); -} - -#if 0 -AssignedDevInfo *get_assigned_device (int pcibus,int slot) -{ -AssignedDevice *assigned_dev =NULL ; -AssignedDevInfo *adev =NULL ; - - QLIST_FOREACH(adev, &adev_head, next) { - assigned_dev = adev->assigned_dev; -if (pci_bus_num (assigned_dev->dev.bus) == pcibus && -PCI_SLOT (assigned_dev->dev.devfn) == slot) -return adev; } - -return NULL ; } -#endif /* The pci config space got updated. Check if irq numbers have changed * for our devices
@@ -1103,10 +1089,12 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev) }/* Minimal PM support, nothing writable, device appears to NAK changes */ -if ((pos =pci_find_cap_offset (pci_dev,PCI_CAP_ID_PM ,0 ))) { + pos =pci_find_cap_offset (pci_dev,PCI_CAP_ID_PM ,0 ); +if (pos !=0 ) {uint16_t pmc; -if ((ret =pci_add_capability (pci_dev,PCI_CAP_ID_PM , pos, -PCI_PM_SIZEOF )) <0 ) { + + ret =pci_add_capability (pci_dev,PCI_CAP_ID_PM , pos,PCI_PM_SIZEOF ); +if (ret <0 ) {return ret; }
@@ -1125,7 +1113,8 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)pci_set_byte (pci_dev->config + pos +PCI_PM_DATA_REGISTER ,0 ); } -if ((pos =pci_find_cap_offset (pci_dev,PCI_CAP_ID_EXP ,0 ))) { + pos =pci_find_cap_offset (pci_dev,PCI_CAP_ID_EXP ,0 ); +if (pos !=0 ) {uint8_t version, size =0 ;uint16_t type, devctl, lnkcap, lnksta;uint32_t devcap;
@@ -1144,13 +1133,13 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev) size =MIN (0x3c ,PCI_CONFIG_SPACE_SIZE - pos);if (size <0x34 ) {fprintf (stderr, -"%s: Invalid size PCIe cap-id 0x%x \n" , +"%s: Invalid size PCIe cap-id 0x%x\n" , __func__,PCI_CAP_ID_EXP );return -EINVAL ; }else if (size !=0x3c ) {fprintf (stderr,"WARNING, %s: PCIe cap-id 0x%x has " -"non-standard size 0x%x; std size should be 0x3c \n" , +"non-standard size 0x%x; std size should be 0x3c\n" , __func__,PCI_CAP_ID_EXP , size); } }else if (version ==0 ) {
@@ -1173,8 +1162,8 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)return -EINVAL ; } -if ((ret =pci_add_capability (pci_dev,PCI_CAP_ID_EXP , - pos, size)) <0 ) { + ret =pci_add_capability (pci_dev,PCI_CAP_ID_EXP , pos, size); +if (ret <0 ) {return ret; }
@@ -1246,12 +1235,14 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev) } } -if ((pos =pci_find_cap_offset (pci_dev,PCI_CAP_ID_PCIX ,0 ))) { + pos =pci_find_cap_offset (pci_dev,PCI_CAP_ID_PCIX ,0 ); +if (pos !=0 ) {uint16_t cmd;uint32_t status;/* Only expose the minimum, 8 byte capability */ -if ((ret =pci_add_capability (pci_dev,PCI_CAP_ID_PCIX , pos,8 )) <0 ) { + ret =pci_add_capability (pci_dev,PCI_CAP_ID_PCIX , pos,8 ); +if (ret <0 ) {return ret; }
@@ -1273,9 +1264,11 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)pci_set_long (pci_dev->config + pos +PCI_X_STATUS , status); } -if ((pos =pci_find_cap_offset (pci_dev,PCI_CAP_ID_VPD ,0 ))) { + pos =pci_find_cap_offset (pci_dev,PCI_CAP_ID_VPD ,0 ); +if (pos !=0 ) {/* Direct R/W passthrough */ -if ((ret =pci_add_capability (pci_dev,PCI_CAP_ID_VPD , pos,8 )) <0 ) { + ret =pci_add_capability (pci_dev,PCI_CAP_ID_VPD , pos,8 ); +if (ret <0 ) {return ret; }
@@ -1290,8 +1283,8 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev) pos += PCI_CAP_LIST_NEXT) {uint8_t len =pci_get_byte (pci_dev->config + pos +PCI_CAP_FLAGS );/* Direct R/W passthrough */ -if ((ret =pci_add_capability (pci_dev,PCI_CAP_ID_VNDR , - pos, len)) <0 ) { + ret =pci_add_capability (pci_dev,PCI_CAP_ID_VNDR , pos, len); +if (ret <0 ) {return ret; }
@@ -1397,8 +1390,9 @@ static int assigned_initfn(struct PCIDevice *pci_dev)/* handle real device's MMIO/PIO BARs */ if (assigned_dev_register_regions (dev->real_device.regions, dev->real_device.region_number, - dev)) + dev)) { goto out; + }if (assigned_device_pci_cap_init (pci_dev) <0 ) { goto out;
@@ -1415,13 +1409,15 @@ static int assigned_initfn(struct PCIDevice *pci_dev)/* assign device to guest */ r =assign_device (dev); -if (r <0 ) +if (r <0 ) { goto out; + }/* assign legacy INTx to the device */ r =assign_intx (dev); -if (r <0 ) +if (r <0 ) { goto assigned_out; + }assigned_dev_load_option_rom (dev);QLIST_INSERT_HEAD (&devs, dev, next);
@@ -1452,13 +1448,16 @@ static int parse_hostaddr(DeviceState *dev, Property *prop, const char *str)PCIHostDevice *ptr =qdev_get_prop_ptr (dev, prop);int rc; - rc =pci_parse_host_devaddr (str, &ptr->seg, &ptr->bus, &ptr->dev, &ptr->func); -if (rc !=0 ) + rc =pci_parse_host_devaddr (str, &ptr->seg, &ptr->bus, &ptr->dev, + &ptr->func); +if (rc !=0 ) {return -1 ; + }return 0 ; } -static int print_hostaddr (DeviceState *dev,Property *prop,char *dest,size_t len) +static int print_hostaddr (DeviceState *dev,Property *prop,char *dest, +size_t len) {PCIHostDevice *ptr =qdev_get_prop_ptr (dev, prop);
@@ -1484,7 +1483,8 @@ static PCIDeviceInfo assign_info = { .config_read = assigned_dev_pci_read_config, .config_write = assigned_dev_pci_write_config, .qdev.props = (Property []) { -DEFINE_PROP ("host" , AssignedDevice, host, qdev_prop_hostaddr, PCIHostDevice), +DEFINE_PROP ("host" , AssignedDevice, host, qdev_prop_hostaddr, + PCIHostDevice),DEFINE_PROP_BIT ("iommu" , AssignedDevice, features,ASSIGNED_DEVICE_USE_IOMMU_BIT , true), DEFINE_PROP_BIT("prefer_msi", AssignedDevice, features,
@@ -1516,8 +1516,9 @@ static void assigned_dev_load_option_rom(AssignedDevice *dev)void *ptr;/* If loading ROM from file, pci handles it */ -if (dev->dev.romfile || !dev->dev.rom_bar) +if (dev->dev.romfile || !dev->dev.rom_bar) {return ; + } snprintf(rom_file, sizeof(rom_file), "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/rom",
hw/device-assignment.h +7 -7
@@ -25,8 +25,8 @@* Copyright (C) 2008, Red Hat, Amit Shah ([email protected]) */ -#ifndef __DEVICE_ASSIGNMENT_H__ -#define __DEVICE_ASSIGNMENT_H__ +#ifndef QEMU_DEVICE_ASSIGNMENT_H +#define QEMU_DEVICE_ASSIGNMENT_H #include <sys/mman.h> #include "qemu-common.h"
@@ -74,11 +74,11 @@ typedef struct {PCIRegion *region; } AssignedDevRegion; -#define ASSIGNED_DEVICE_USE_IOMMU_BIT 0 -#define ASSIGNED_DEVICE_PREFER_MSI_BIT 1 +#define ASSIGNED_DEVICE_USE_IOMMU_BIT 0 +#define ASSIGNED_DEVICE_PREFER_MSI_BIT 1 -#define ASSIGNED_DEVICE_USE_IOMMU_MASK (1 << ASSIGNED_DEVICE_USE_IOMMU_BIT) -#define ASSIGNED_DEVICE_PREFER_MSI_MASK (1 << ASSIGNED_DEVICE_PREFER_MSI_BIT) +#define ASSIGNED_DEVICE_USE_IOMMU_MASK (1 << ASSIGNED_DEVICE_USE_IOMMU_BIT) +#define ASSIGNED_DEVICE_PREFER_MSI_MASK (1 << ASSIGNED_DEVICE_PREFER_MSI_BIT) typedef struct AssignedDevice { PCIDevice dev;
@@ -105,4 +105,4 @@ typedef struct AssignedDevice {void assigned_dev_update_irqs (void ); -#endif /* __DEVICE_ASSIGNMENT_H__ */ +#endif /* QEMU_DEVICE_ASSIGNMENT_H */
Signature · 1 line
1.7.3.4
Quoted 14 lines · Jan Kiszka
As previously indicated, I was working for quite a while on a major refactoring of the MSI "additions" we have in qemu-kvm to support in-kernel irqchip, vhost and device assignment. This is now the outcome.
I'm quite happy with it, things are still working (apparently), and the invasiveness of KVM hooks into the MSI layer is significantly reduced. Moreover, I was able to port the device assignment code over generic MSI support, reducing the size of that file a bit further.
Some further highlights:
- fix for HPET MSI support with in-kernel irqchip
- fully configurable MSI-X (allows 1:1 mapping for assigned devices)
- refactored KVM core API for device assignment and IRQ routing
Quoted 5 lines
I'm sending the whole series in one chunk so that you can see what the result will be. It's RFC as I bet that there are regressions included and maybe still room left for improvements. Once all is fine (can be broken up into multiple chunks for the merge), I would suggest patching qemu-kvm first and then start with porting things over to upstream.
Impressive patchset, let's merge this as quickly as possible.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 20 lines · Jan Kiszka
As previously indicated, I was working for quite a while on a major refactoring of the MSI "additions" we have in qemu-kvm to support in-kernel irqchip, vhost and device assignment. This is now the outcome.
I'm quite happy with it, things are still working (apparently), and the invasiveness of KVM hooks into the MSI layer is significantly reduced. Moreover, I was able to port the device assignment code over generic MSI support, reducing the size of that file a bit further.
Some further highlights:
- fix for HPET MSI support with in-kernel irqchip
- fully configurable MSI-X (allows 1:1 mapping for assigned devices)
- refactored KVM core API for device assignment and IRQ routingI'm sending the whole series in one chunk so that you can see what the result will be. It's RFC as I bet that there are regressions included and maybe still room left for improvements. Once all is fine (can be broken up into multiple chunks for the merge), I would suggest patching qemu-kvm first and then start with porting things over to upstream.
Quoted 5 lines
Comments & review welcome.
CC Alexander Graf <[email protected]>CC Gerd Hoffmann <[email protected]>CC Isaku Yamahata <[email protected]>
So the scheme where we lazily update kvm gsi table on interrupts is interesting. There seems to be very little point in it for virtio, and it does seem to make it impossible to detect lack or resources (at the moment we let guest know if we run out of GSIs and linux guests can fall back to regular interrupts).
I am guessing the idea is to use it for device assignment where it does make sense as there is no standard way
to track which vectors are actually used?
But how does it work there? kvm does not
propage unmapped interrupts from an assigned device to qemu, does it?
Signature · 1 line
MST
Quoted 28 lines · Michael S. Tsirkin
Quoted 20 lines · Jan Kiszka
As previously indicated, I was working for quite a while on a major refactoring of the MSI "additions" we have in qemu-kvm to support in-kernel irqchip, vhost and device assignment. This is now the outcome.
I'm quite happy with it, things are still working (apparently), and the invasiveness of KVM hooks into the MSI layer is significantly reduced. Moreover, I was able to port the device assignment code over generic MSI support, reducing the size of that file a bit further.
Some further highlights:
- fix for HPET MSI support with in-kernel irqchip
- fully configurable MSI-X (allows 1:1 mapping for assigned devices)
- refactored KVM core API for device assignment and IRQ routingI'm sending the whole series in one chunk so that you can see what the result will be. It's RFC as I bet that there are regressions included and maybe still room left for improvements. Once all is fine (can be broken up into multiple chunks for the merge), I would suggest patching qemu-kvm first and then start with porting things over to upstream.
Quoted 5 lines
Comments & review welcome.
CC Alexander Graf <[email protected]>CC Gerd Hoffmann <[email protected]>CC Isaku Yamahata <[email protected]>
Quoted 6 lines
So the scheme where we lazily update kvm gsi table on interrupts is interesting. There seems to be very little point in it for virtio, and it does seem to make it impossible to detect lack or resources (at the moment we let guest know if we run out of GSIs and linux guests can fall back to regular interrupts).
Are we really at that limit already? Then I think it's rather time to lift it at the kernel side.
Quoted 6 lines
I am guessing the idea is to use it for device assignment where it does make sense as there is no standard way
to track which vectors are actually used?
But how does it work there? kvm does not
propage unmapped interrupts from an assigned device to qemu, does it?
No, device assignment and irqfd (vhost, vfio, whatever-will-come) belong to the static group. They need to hand out a static gsi to the irq source, thus they cannot participate in lazy updating.
But they can benefit from the generic config notifiers: whenever the guest changes some route (or disables it), they just need to inform the core about this. So there is no need to track used vectors separately anymore.
Jan