Skip to contentSkip to conversation

pci: rearrange code for interrupts

PATCH In review Michael S. Tsirkin 2 participants 8 messages Nov 25, 2009 – Nov 26, 2009 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/4 pci: rearrange code for interrupts

Rearranges code slightly in preparation
for interrupt status and interrupt
disable support: split actual irq change
from irq_state management.

---
 hw/pci.c |   26 ++++++++++++++++----------
 1 files changed, 16 insertions(+), 10 deletions(-)
hw/pci.c +16 -10
index 0359f30..5ff0d46 100644
@@ -880,18 +880,9 @@ void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
 /***********************************************************/
 /* generic PCI irq support */
 
-/* 0 <= irq_num <= 3. level must be 0 or 1 */
-static void pci_set_irq(void *opaque, int irq_num, int level)
+static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
 {
-    PCIDevice *pci_dev = opaque;
     PCIBus *bus;
-    int change;
-
-    change = level - pci_dev->irq_state[irq_num];
-    if (!change)
-        return;
-
-    pci_dev->irq_state[irq_num] = level;
     for (;;) {
         bus = pci_dev->bus;
         irq_num = bus->map_irq(pci_dev, irq_num);
@@ -903,6 +894,21 @@ static void pci_set_irq(void *opaque, int irq_num, int level)
     bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
 }
 
+
+/* 0 <= irq_num <= 3. level must be 0 or 1 */
+static void pci_set_irq(void *opaque, int irq_num, int level)
+{
+    PCIDevice *pci_dev = opaque;
+    int change;
+
+    change = level - pci_dev->irq_state[irq_num];
+    if (!change)
+        return;
+
+    pci_dev->irq_state[irq_num] = level;
+    pci_change_irq_level(pci_dev, irq_num, change);
+}
+
 /***********************************************************/
 /* monitor info on PCI */
 
Signature · 1 line
1.6.5.2.143.g8cc62
#2 lore raw

PATCH 2/4 pci: track IRQ status

In reply to a message not in the archive

track IRQ status in preparation for PCI_STATUS_INTERRUPT support

---
 hw/pci.c |   10 +++++++++-
 hw/pci.h |    3 +++
 2 files changed, 12 insertions(+), 1 deletions(-)
hw/pci.c +9 -1
index 5ff0d46..7717461 100644
@@ -108,6 +108,7 @@ static void pci_device_reset(PCIDevice *dev)
     int r;
 
     memset(dev->irq_state, 0, sizeof dev->irq_state);
+    dev->irq_status = 0;
     dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
                                   PCI_COMMAND_MASTER);
     dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
@@ -318,7 +319,12 @@ void pci_device_save(PCIDevice *s, QEMUFile *f)
 
 int pci_device_load(PCIDevice *s, QEMUFile *f)
 {
-    return vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
+    int ret, i;
+    ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
+    for (i = 0; i < ARRAY_SIZE(s->irq_state); ++i) {
+        s->irq_status += s->irq_state[i];
+    }
+    return ret;
 }
 
 static int pci_set_default_subsystem_id(PCIDevice *pci_dev)
@@ -502,6 +508,7 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
     pci_dev->devfn = devfn;
     pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
     memset(pci_dev->irq_state, 0, sizeof(pci_dev->irq_state));
+    pci_dev->irq_status = 0;
     pci_config_alloc(pci_dev);
 
     header_type &= ~PCI_HEADER_TYPE_MULTI_FUNCTION;
@@ -906,6 +913,7 @@ static void pci_set_irq(void *opaque, int irq_num, int level)
         return;
 
     pci_dev->irq_state[irq_num] = level;
+    pci_dev->irq_status += change;
     pci_change_irq_level(pci_dev, irq_num, change);
 }
 
hw/pci.h +3 -0
index 3e8abad..2e172f6 100644
@@ -222,6 +222,9 @@ struct PCIDevice {
     /* Current IRQ levels.  Used internally by the generic PCI code.  */
     int irq_state[PCI_NUM_PINS];
 
+    /* Sum of all irq levels. Used to implement irq status register. */
+    int irq_status;
+
     /* Capability bits */
     uint32_t cap_present;
 
Signature · 1 line
1.6.5.2.143.g8cc62
#3 lore raw

PATCH 3/4 pci: interrupt status bit implementation

In reply to a message not in the archive

interrupt status is a mandatory feature in PCI spec, so devices must implement it to be spec compliant.

---
 hw/pci.c |   22 ++++++++++++++++++++++
 hw/pci.h |    1 +
 2 files changed, 23 insertions(+), 0 deletions(-)
hw/pci.c +22 -0
index 7717461..844664b 100644
@@ -103,12 +103,24 @@ static int pci_bar(PCIDevice *d, int reg)
     return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
 }
 
+/* Update interrupt status bit in config space on interrupt
+ * state change. */
+static void pci_update_irq_status(PCIDevice *dev)
+{
+    if (dev->irq_status) {
+        dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
+    } else {
+        dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
+    }
+}
+
 static void pci_device_reset(PCIDevice *dev)
 {
     int r;
 
     memset(dev->irq_state, 0, sizeof dev->irq_state);
     dev->irq_status = 0;
+    pci_update_irq_status(dev);
     dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
                                   PCI_COMMAND_MASTER);
     dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
@@ -314,7 +326,14 @@ static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s)
 
 void pci_device_save(PCIDevice *s, QEMUFile *f)
 {
+    /* Clear interrupt status bit: it is implicit
+     * in irq_state which we are saving.
+     * This makes us compatible with old devices
+     * which never set or clear this bit. */
+    s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
     vmstate_save_state(f, pci_get_vmstate(s), s);
+    /* Restore the interrupt status bit. */
+    pci_update_irq_status(s);
 }
 
 int pci_device_load(PCIDevice *s, QEMUFile *f)
@@ -324,6 +343,8 @@ int pci_device_load(PCIDevice *s, QEMUFile *f)
     for (i = 0; i < ARRAY_SIZE(s->irq_state); ++i) {
         s->irq_status += s->irq_state[i];
     }
+    /* Restore the interrupt status bit. */
+    pci_update_irq_status(s);
     return ret;
 }
 
@@ -914,6 +935,7 @@ static void pci_set_irq(void *opaque, int irq_num, int level)
 
     pci_dev->irq_state[irq_num] = level;
     pci_dev->irq_status += change;
+    pci_update_irq_status(s);
     pci_change_irq_level(pci_dev, irq_num, change);
 }
 
hw/pci.h +1 -0
index 2e172f6..c3f2c3f 100644
@@ -102,6 +102,7 @@ typedef struct PCIIORegion {
 #define  PCI_COMMAND_MEMORY	0x2	/* Enable response in Memory space */
 #define  PCI_COMMAND_MASTER	0x4	/* Enable bus master */
 #define PCI_STATUS              0x06    /* 16 bits */
+#define  PCI_STATUS_INTERRUPT   0x08
 #define PCI_REVISION_ID         0x08    /* 8 bits  */
 #define PCI_CLASS_PROG		0x09	/* Reg. Level Programming Interface */
 #define PCI_CLASS_DEVICE        0x0a    /* Device class */
Signature · 1 line
1.6.5.2.143.g8cc62
#4 lore raw

PATCH 4/4 pci: interrupt disable bit support

In reply to a message not in the archive

interrupt disable bit is mandatory in PCI spec,
so we must implement it to be spec compliant.

---
 hw/pci.c |   57 ++++++++++++++++++++++++++++++++++++++++-----------------
 hw/pci.h |    4 ++++
 2 files changed, 44 insertions(+), 17 deletions(-)
hw/pci.c +40 -17
index 844664b..3daae46 100644
@@ -114,12 +114,43 @@ static void pci_update_irq_status(PCIDevice *dev)
     }
 }
 
+static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
+{
+    PCIBus *bus;
+    for (;;) {
+        bus = pci_dev->bus;
+        irq_num = bus->map_irq(pci_dev, irq_num);
+        if (bus->set_irq)
+            break;
+        pci_dev = bus->parent_dev;
+    }
+    bus->irq_count[irq_num] += change;
+    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
+}
+
+/* Update irq disabled field after config space change,
+ * assert/deassert interrupts if necessary. */
+static void pci_update_irq_disabled(PCIDevice *d)
+{
+    int i;
+    int disabled = !!(pci_get_word(d->config + PCI_COMMAND) &
+                      PCI_COMMAND_INTX_DISABLE);
+    if (disabled == d->irq_disabled)
+        return;
+    d->irq_disabled = disabled;
+    for (i = 0; i < ARRAY_SIZE(d->irq_state); ++i) {
+        int state = d->irq_state[i];
+        pci_change_irq_level(d, i, disabled ? -state : state);
+    }
+}
+
 static void pci_device_reset(PCIDevice *dev)
 {
     int r;
 
     memset(dev->irq_state, 0, sizeof dev->irq_state);
     dev->irq_status = 0;
+    dev->irq_disabled = 0;
     pci_update_irq_status(dev);
     dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
                                   PCI_COMMAND_MASTER);
@@ -345,6 +376,7 @@ int pci_device_load(PCIDevice *s, QEMUFile *f)
     }
     /* Restore the interrupt status bit. */
     pci_update_irq_status(s);
+    pci_update_irq_disabled(s);
     return ret;
 }
 
@@ -458,7 +490,8 @@ static void pci_init_wmask(PCIDevice *dev)
     dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
     dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
     pci_set_word(dev->wmask + PCI_COMMAND,
-                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
+                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
+                 PCI_COMMAND_INTX_DISABLE);
 
     memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
            config_size - PCI_CONFIG_HEADER_SIZE);
@@ -903,26 +936,14 @@ void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
         ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
         range_covers_byte(addr, l, PCI_COMMAND))
         pci_update_mappings(d);
+
+    if (range_covers_byte(addr, l, PCI_COMMAND))
+        pci_update_irq_disabled(d);
 }
 
 /***********************************************************/
 /* generic PCI irq support */
 
-static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
-{
-    PCIBus *bus;
-    for (;;) {
-        bus = pci_dev->bus;
-        irq_num = bus->map_irq(pci_dev, irq_num);
-        if (bus->set_irq)
-            break;
-        pci_dev = bus->parent_dev;
-    }
-    bus->irq_count[irq_num] += change;
-    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
-}
-
-
 /* 0 <= irq_num <= 3. level must be 0 or 1 */
 static void pci_set_irq(void *opaque, int irq_num, int level)
 {
@@ -935,7 +956,9 @@ static void pci_set_irq(void *opaque, int irq_num, int level)
 
     pci_dev->irq_state[irq_num] = level;
     pci_dev->irq_status += change;
-    pci_update_irq_status(s);
+    pci_update_irq_status(pci_dev);
+    if (pci_dev->irq_disabled)
+        return;
     pci_change_irq_level(pci_dev, irq_num, change);
 }
 
hw/pci.h +4 -0
index c3f2c3f..994b8bc 100644
@@ -101,6 +101,7 @@ typedef struct PCIIORegion {
 #define  PCI_COMMAND_IO		0x1	/* Enable response in I/O space */
 #define  PCI_COMMAND_MEMORY	0x2	/* Enable response in Memory space */
 #define  PCI_COMMAND_MASTER	0x4	/* Enable bus master */
+#define  PCI_COMMAND_INTX_DISABLE 0x400 /* INTx Emulation Disable */
 #define PCI_STATUS              0x06    /* 16 bits */
 #define  PCI_STATUS_INTERRUPT   0x08
 #define PCI_REVISION_ID         0x08    /* 8 bits  */
@@ -226,6 +227,9 @@ struct PCIDevice {
     /* Sum of all irq levels. Used to implement irq status register. */
     int irq_status;
 
+    /* Whether interrupts are disabled by command bit. */
+    int irq_disabled;
+
     /* Capability bits */
     uint32_t cap_present;
 
Signature · 1 line
1.6.5.2.143.g8cc62
#5 lore raw

[Qemu-devel] Re: [PATCH 4/4] pci: interrupt disable bit support

Quoted 19 lines · Michael S. Tsirkin

interrupt disable bit is mandatory in PCI spec,
so we must implement it to be spec compliant.

---
 hw/pci.c |   57 ++++++++++++++++++++++++++++++++++++++++-----------------
 hw/pci.h |    4 ++++
 2 files changed, 44 insertions(+), 17 deletions(-)
hw/pci.c +2 -0
index 844664b..3daae46 100644
@@ -114,12 +114,43 @@ static void pci_update_irq_status(PCIDevice *dev)
     }
 }
 
+static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
+{

hw/pci.c static void pci_update_irq_status(PCIDevice *dev) · line 128

Quoted 10 lines
hw/pci.c +10 -0
+    PCIBus *bus;
+    for (;;) {
+        bus = pci_dev->bus;
+        irq_num = bus->map_irq(pci_dev, irq_num);
+        if (bus->set_irq)
+            break;
+        pci_dev = bus->parent_dev;
+    }
+    bus->irq_count[irq_num] += change;
+    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);

Let's use ! instead of != 0 for consistency.

Quoted 8 lines
hw/pci.c +8 -0
+}
+
+/* Update irq disabled field after config space change,
+ * assert/deassert interrupts if necessary. */
+static void pci_update_irq_disabled(PCIDevice *d)
+{
+    int i;
+    int disabled = !!(pci_get_word(d->config + PCI_COMMAND) &

hw/pci.c static void pci_update_irq_status(PCIDevice *dev) · line 146

Quoted 10 lines
hw/pci.c +10 -0
+                      PCI_COMMAND_INTX_DISABLE);
+    if (disabled == d->irq_disabled)
+        return;
+    d->irq_disabled = disabled;
+    for (i = 0; i < ARRAY_SIZE(d->irq_state); ++i) {
+        int state = d->irq_state[i];
+        pci_change_irq_level(d, i, disabled ? -state : state);
+    }
+}
+

"if (!changed) return" check is in pci_set_irq().
So if d->irq_state[i] == 0, bus->set_irq() will be called again.
I suppose this isn't your intention.
I suppose !changed check should be in pci_change_irq_level() instead of pci_set_irq().

Quoted 91 lines
hw/pci.c +10 -17
 static void pci_device_reset(PCIDevice *dev)
 {
     int r;
 
     memset(dev->irq_state, 0, sizeof dev->irq_state);
     dev->irq_status = 0;
+    dev->irq_disabled = 0;
     pci_update_irq_status(dev);
     dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
                                   PCI_COMMAND_MASTER);
@@ -345,6 +376,7 @@ int pci_device_load(PCIDevice *s, QEMUFile *f)
     }
     /* Restore the interrupt status bit. */
     pci_update_irq_status(s);
+    pci_update_irq_disabled(s);
     return ret;
 }
 
@@ -458,7 +490,8 @@ static void pci_init_wmask(PCIDevice *dev)
     dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
     dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
     pci_set_word(dev->wmask + PCI_COMMAND,
-                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
+                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
+                 PCI_COMMAND_INTX_DISABLE);
 
     memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
            config_size - PCI_CONFIG_HEADER_SIZE);
@@ -903,26 +936,14 @@ void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
         ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
         range_covers_byte(addr, l, PCI_COMMAND))
         pci_update_mappings(d);
+
+    if (range_covers_byte(addr, l, PCI_COMMAND))
+        pci_update_irq_disabled(d);
 }
 
 /***********************************************************/
 /* generic PCI irq support */
 
-static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
-{
-    PCIBus *bus;
-    for (;;) {
-        bus = pci_dev->bus;
-        irq_num = bus->map_irq(pci_dev, irq_num);
-        if (bus->set_irq)
-            break;
-        pci_dev = bus->parent_dev;
-    }
-    bus->irq_count[irq_num] += change;
-    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
-}
-
-
 /* 0 <= irq_num <= 3. level must be 0 or 1 */
 static void pci_set_irq(void *opaque, int irq_num, int level)
 {
@@ -935,7 +956,9 @@ static void pci_set_irq(void *opaque, int irq_num, int level)
 
     pci_dev->irq_state[irq_num] = level;
     pci_dev->irq_status += change;
-    pci_update_irq_status(s);
+    pci_update_irq_status(pci_dev);
+    if (pci_dev->irq_disabled)
+        return;
     pci_change_irq_level(pci_dev, irq_num, change);
 }
 
hw/pci.h +4 -0
index c3f2c3f..994b8bc 100644
@@ -101,6 +101,7 @@ typedef struct PCIIORegion {
 #define  PCI_COMMAND_IO		0x1	/* Enable response in I/O space */
 #define  PCI_COMMAND_MEMORY	0x2	/* Enable response in Memory space */
 #define  PCI_COMMAND_MASTER	0x4	/* Enable bus master */
+#define  PCI_COMMAND_INTX_DISABLE 0x400 /* INTx Emulation Disable */
 #define PCI_STATUS              0x06    /* 16 bits */
 #define  PCI_STATUS_INTERRUPT   0x08
 #define PCI_REVISION_ID         0x08    /* 8 bits  */
@@ -226,6 +227,9 @@ struct PCIDevice {
     /* Sum of all irq levels. Used to implement irq status register. */
     int irq_status;
 
+    /* Whether interrupts are disabled by command bit. */
+    int irq_disabled;
+
     /* Capability bits */
     uint32_t cap_present;
 
Signature · 1 line
yamahata
#6 lore raw

[Qemu-devel] Re: [PATCH 4/4] pci: interrupt disable bit support

Quoted 18 lines · Isaku Yamahata
Quoted 17 lines · Michael S. Tsirkin

interrupt disable bit is mandatory in PCI spec,
so we must implement it to be spec compliant.

---
 hw/pci.c |   57 ++++++++++++++++++++++++++++++++++++++++-----------------
 hw/pci.h |    4 ++++
 2 files changed, 44 insertions(+), 17 deletions(-)
hw/pci.c +0 -0
index 844664b..3daae46 100644
@@ -114,12 +114,43 @@ static void pci_update_irq_status(PCIDevice *dev)
     }
 }
 
Quoted 15 lines

hw/pci.c static void pci_update_irq_status(PCIDevice *dev) · line 128

Quoted 12 lines
hw/pci.c +12 -0
+static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
+{
+    PCIBus *bus;
+    for (;;) {
+        bus = pci_dev->bus;
+        irq_num = bus->map_irq(pci_dev, irq_num);
+        if (bus->set_irq)
+            break;
+        pci_dev = bus->parent_dev;
+    }
+    bus->irq_count[irq_num] += change;
+    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);

Let's use ! instead of != 0 for consistency.

This patch simply moves code, so it's better to keep it the same - this way git blame can find it.
A separate patch to clean this might be a good idea.

Quoted 14 lines
Quoted 14 lines
hw/pci.c +14 -0
+}
+
+/* Update irq disabled field after config space change,
+ * assert/deassert interrupts if necessary. */
+static void pci_update_irq_disabled(PCIDevice *d)
+{
+    int i;
+    int disabled = !!(pci_get_word(d->config + PCI_COMMAND) &
+                      PCI_COMMAND_INTX_DISABLE);
+    if (disabled == d->irq_disabled)
+        return;
+    d->irq_disabled = disabled;
+    for (i = 0; i < ARRAY_SIZE(d->irq_state); ++i) {
+        int state = d->irq_state[i];
Quoted 8 lines

hw/pci.c static void pci_update_irq_status(PCIDevice *dev) · line 146

Quoted 4 lines
hw/pci.c +4 -0
+        pci_change_irq_level(d, i, disabled ? -state : state);
+    }
+}
+

"if (!changed) return" check is in pci_set_irq().
So if d->irq_state[i] == 0, bus->set_irq() will be called again. I suppose this isn't your intention.

Good catch, thanks!

Quoted 2 lines

I suppose !changed check should be in pci_change_irq_level() instead of pci_set_irq().

The fix is different: in fact pci_update_irq_disabled should never call pci_change_irq_level with change value 0. that -state should have been 1 - state.
I'll fix this. Thanks!

Quoted 94 lines
Quoted 91 lines
hw/pci.c +10 -17
 static void pci_device_reset(PCIDevice *dev)
 {
     int r;
 
     memset(dev->irq_state, 0, sizeof dev->irq_state);
     dev->irq_status = 0;
+    dev->irq_disabled = 0;
     pci_update_irq_status(dev);
     dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
                                   PCI_COMMAND_MASTER);
@@ -345,6 +376,7 @@ int pci_device_load(PCIDevice *s, QEMUFile *f)
     }
     /* Restore the interrupt status bit. */
     pci_update_irq_status(s);
+    pci_update_irq_disabled(s);
     return ret;
 }
 
@@ -458,7 +490,8 @@ static void pci_init_wmask(PCIDevice *dev)
     dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
     dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
     pci_set_word(dev->wmask + PCI_COMMAND,
-                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
+                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
+                 PCI_COMMAND_INTX_DISABLE);
 
     memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
            config_size - PCI_CONFIG_HEADER_SIZE);
@@ -903,26 +936,14 @@ void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
         ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
         range_covers_byte(addr, l, PCI_COMMAND))
         pci_update_mappings(d);
+
+    if (range_covers_byte(addr, l, PCI_COMMAND))
+        pci_update_irq_disabled(d);
 }
 
 /***********************************************************/
 /* generic PCI irq support */
 
-static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
-{
-    PCIBus *bus;
-    for (;;) {
-        bus = pci_dev->bus;
-        irq_num = bus->map_irq(pci_dev, irq_num);
-        if (bus->set_irq)
-            break;
-        pci_dev = bus->parent_dev;
-    }
-    bus->irq_count[irq_num] += change;
-    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
-}
-
-
 /* 0 <= irq_num <= 3. level must be 0 or 1 */
 static void pci_set_irq(void *opaque, int irq_num, int level)
 {
@@ -935,7 +956,9 @@ static void pci_set_irq(void *opaque, int irq_num, int level)
 
     pci_dev->irq_state[irq_num] = level;
     pci_dev->irq_status += change;
-    pci_update_irq_status(s);
+    pci_update_irq_status(pci_dev);
+    if (pci_dev->irq_disabled)
+        return;
     pci_change_irq_level(pci_dev, irq_num, change);
 }
 
hw/pci.h +4 -0
index c3f2c3f..994b8bc 100644
@@ -101,6 +101,7 @@ typedef struct PCIIORegion {
 #define  PCI_COMMAND_IO		0x1	/* Enable response in I/O space */
 #define  PCI_COMMAND_MEMORY	0x2	/* Enable response in Memory space */
 #define  PCI_COMMAND_MASTER	0x4	/* Enable bus master */
+#define  PCI_COMMAND_INTX_DISABLE 0x400 /* INTx Emulation Disable */
 #define PCI_STATUS              0x06    /* 16 bits */
 #define  PCI_STATUS_INTERRUPT   0x08
 #define PCI_REVISION_ID         0x08    /* 8 bits  */
@@ -226,6 +227,9 @@ struct PCIDevice {
     /* Sum of all irq levels. Used to implement irq status register. */
     int irq_status;
 
+    /* Whether interrupts are disabled by command bit. */
+    int irq_disabled;
+
     /* Capability bits */
     uint32_t cap_present;
 
Signature · 1 line
yamahata
#7 lore raw

[Qemu-devel] Re: [PATCH 4/4] pci: interrupt disable bit support

Quoted 62 lines · Michael S. Tsirkin
Quoted 18 lines · Isaku Yamahata
Quoted 17 lines · Michael S. Tsirkin

interrupt disable bit is mandatory in PCI spec,
so we must implement it to be spec compliant.

---
 hw/pci.c |   57 ++++++++++++++++++++++++++++++++++++++++-----------------
 hw/pci.h |    4 ++++
 2 files changed, 44 insertions(+), 17 deletions(-)
hw/pci.c +0 -0
index 844664b..3daae46 100644
@@ -114,12 +114,43 @@ static void pci_update_irq_status(PCIDevice *dev)
     }
 }
 
Quoted 15 lines

hw/pci.c static void pci_update_irq_status(PCIDevice *dev) · line 128

Quoted 12 lines
hw/pci.c +12 -0
+static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
+{
+    PCIBus *bus;
+    for (;;) {
+        bus = pci_dev->bus;
+        irq_num = bus->map_irq(pci_dev, irq_num);
+        if (bus->set_irq)
+            break;
+        pci_dev = bus->parent_dev;
+    }
+    bus->irq_count[irq_num] += change;
+    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);

Let's use ! instead of != 0 for consistency.

This patch simply moves code, so it's better to keep it the same - this way git blame can find it.
A separate patch to clean this might be a good idea.

Quoted 14 lines
Quoted 14 lines
hw/pci.c +14 -0
+}
+
+/* Update irq disabled field after config space change,
+ * assert/deassert interrupts if necessary. */
+static void pci_update_irq_disabled(PCIDevice *d)
+{
+    int i;
+    int disabled = !!(pci_get_word(d->config + PCI_COMMAND) &
+                      PCI_COMMAND_INTX_DISABLE);
+    if (disabled == d->irq_disabled)
+        return;
+    d->irq_disabled = disabled;
+    for (i = 0; i < ARRAY_SIZE(d->irq_state); ++i) {
+        int state = d->irq_state[i];
Quoted 8 lines

hw/pci.c static void pci_update_irq_status(PCIDevice *dev) · line 146

Quoted 4 lines
hw/pci.c +4 -0
+        pci_change_irq_level(d, i, disabled ? -state : state);
+    }
+}
+

"if (!changed) return" check is in pci_set_irq().
So if d->irq_state[i] == 0, bus->set_irq() will be called again. I suppose this isn't your intention.

Quoted 1 line

Good catch, thanks!

Thinking about this more, in fact, it is harmless to call set_irq with the same level as th old one, and this
is not on datapath.
So yes, this is the intention.

Quoted 7 lines
Quoted 2 lines

I suppose !changed check should be in pci_change_irq_level() instead of pci_set_irq().

The fix is different: in fact pci_update_irq_disabled should never call pci_change_irq_level with change value 0. that -state should have been 1 - state.
I'll fix this. Thanks!

In fact, you were right.
While calling pci_change_irq_level with change 0 is harmless, it's cleaner to move the check within that function. I'll do that.

Quoted 94 lines
Quoted 94 lines
Quoted 91 lines
hw/pci.c +10 -17
 static void pci_device_reset(PCIDevice *dev)
 {
     int r;
 
     memset(dev->irq_state, 0, sizeof dev->irq_state);
     dev->irq_status = 0;
+    dev->irq_disabled = 0;
     pci_update_irq_status(dev);
     dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
                                   PCI_COMMAND_MASTER);
@@ -345,6 +376,7 @@ int pci_device_load(PCIDevice *s, QEMUFile *f)
     }
     /* Restore the interrupt status bit. */
     pci_update_irq_status(s);
+    pci_update_irq_disabled(s);
     return ret;
 }
 
@@ -458,7 +490,8 @@ static void pci_init_wmask(PCIDevice *dev)
     dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
     dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
     pci_set_word(dev->wmask + PCI_COMMAND,
-                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
+                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
+                 PCI_COMMAND_INTX_DISABLE);
 
     memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
            config_size - PCI_CONFIG_HEADER_SIZE);
@@ -903,26 +936,14 @@ void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
         ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
         range_covers_byte(addr, l, PCI_COMMAND))
         pci_update_mappings(d);
+
+    if (range_covers_byte(addr, l, PCI_COMMAND))
+        pci_update_irq_disabled(d);
 }
 
 /***********************************************************/
 /* generic PCI irq support */
 
-static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
-{
-    PCIBus *bus;
-    for (;;) {
-        bus = pci_dev->bus;
-        irq_num = bus->map_irq(pci_dev, irq_num);
-        if (bus->set_irq)
-            break;
-        pci_dev = bus->parent_dev;
-    }
-    bus->irq_count[irq_num] += change;
-    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
-}
-
-
 /* 0 <= irq_num <= 3. level must be 0 or 1 */
 static void pci_set_irq(void *opaque, int irq_num, int level)
 {
@@ -935,7 +956,9 @@ static void pci_set_irq(void *opaque, int irq_num, int level)
 
     pci_dev->irq_state[irq_num] = level;
     pci_dev->irq_status += change;
-    pci_update_irq_status(s);
+    pci_update_irq_status(pci_dev);
+    if (pci_dev->irq_disabled)
+        return;
     pci_change_irq_level(pci_dev, irq_num, change);
 }
 
hw/pci.h +4 -0
index c3f2c3f..994b8bc 100644
@@ -101,6 +101,7 @@ typedef struct PCIIORegion {
 #define  PCI_COMMAND_IO		0x1	/* Enable response in I/O space */
 #define  PCI_COMMAND_MEMORY	0x2	/* Enable response in Memory space */
 #define  PCI_COMMAND_MASTER	0x4	/* Enable bus master */
+#define  PCI_COMMAND_INTX_DISABLE 0x400 /* INTx Emulation Disable */
 #define PCI_STATUS              0x06    /* 16 bits */
 #define  PCI_STATUS_INTERRUPT   0x08
 #define PCI_REVISION_ID         0x08    /* 8 bits  */
@@ -226,6 +227,9 @@ struct PCIDevice {
     /* Sum of all irq levels. Used to implement irq status register. */
     int irq_status;
 
+    /* Whether interrupts are disabled by command bit. */
+    int irq_disabled;
+
     /* Capability bits */
     uint32_t cap_present;
 
Signature · 1 line
yamahata
#8 lore raw

[Qemu-devel] Re: [PATCH 4/4] pci: interrupt disable bit support

In reply to Isaku Yamahata
Quoted 18 lines · Isaku Yamahata
Quoted 17 lines · Michael S. Tsirkin

interrupt disable bit is mandatory in PCI spec,
so we must implement it to be spec compliant.

---
 hw/pci.c |   57 ++++++++++++++++++++++++++++++++++++++++-----------------
 hw/pci.h |    4 ++++
 2 files changed, 44 insertions(+), 17 deletions(-)
hw/pci.c +0 -0
index 844664b..3daae46 100644
@@ -114,12 +114,43 @@ static void pci_update_irq_status(PCIDevice *dev)
     }
 }
 
Quoted 14 lines

hw/pci.c static void pci_update_irq_status(PCIDevice *dev) · line 128

Quoted 12 lines
hw/pci.c +12 -0
+static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
+{
+    PCIBus *bus;
+    for (;;) {
+        bus = pci_dev->bus;
+        irq_num = bus->map_irq(pci_dev, irq_num);
+        if (bus->set_irq)
+            break;
+        pci_dev = bus->parent_dev;
+    }
+    bus->irq_count[irq_num] += change;
+    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);

Let's use ! instead of != 0 for consistency.

By the way, this would have to be !!, not !. So not sure there's much consistency to be gained:
!= appears in pci.c more times than !!.

Signature · 1 line
MST