* [Qemu-devel] [PATCH 1/4] pci: rearrange code for interrupts
[not found] <cover.1259168040.git.mst@redhat.com>
@ 2009-11-25 16:58 ` Michael S. Tsirkin
2009-11-25 16:58 ` [Qemu-devel] [PATCH 2/4] pci: track IRQ status Michael S. Tsirkin
` (2 subsequent siblings)
3 siblings, 0 replies; 8+ messages in thread
From: Michael S. Tsirkin @ 2009-11-25 16:58 UTC (permalink / raw)
To: qemu-devel, anthony, yamahata
Rearranges code slightly in preparation
for interrupt status and interrupt
disable support: split actual irq change
from irq_state management.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
hw/pci.c | 26 ++++++++++++++++----------
1 files changed, 16 insertions(+), 10 deletions(-)
diff --git a/hw/pci.c b/hw/pci.c
index 0359f30..5ff0d46 100644
--- a/hw/pci.c
+++ b/hw/pci.c
@@ -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 */
--
1.6.5.2.143.g8cc62
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [Qemu-devel] [PATCH 2/4] pci: track IRQ status
[not found] <cover.1259168040.git.mst@redhat.com>
2009-11-25 16:58 ` [Qemu-devel] [PATCH 1/4] pci: rearrange code for interrupts Michael S. Tsirkin
@ 2009-11-25 16:58 ` Michael S. Tsirkin
2009-11-25 16:58 ` [Qemu-devel] [PATCH 3/4] pci: interrupt status bit implementation Michael S. Tsirkin
2009-11-25 16:59 ` [Qemu-devel] [PATCH 4/4] pci: interrupt disable bit support Michael S. Tsirkin
3 siblings, 0 replies; 8+ messages in thread
From: Michael S. Tsirkin @ 2009-11-25 16:58 UTC (permalink / raw)
To: qemu-devel, anthony, yamahata
track IRQ status in preparation for PCI_STATUS_INTERRUPT support
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
hw/pci.c | 10 +++++++++-
hw/pci.h | 3 +++
2 files changed, 12 insertions(+), 1 deletions(-)
diff --git a/hw/pci.c b/hw/pci.c
index 5ff0d46..7717461 100644
--- a/hw/pci.c
+++ b/hw/pci.c
@@ -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);
}
diff --git a/hw/pci.h b/hw/pci.h
index 3e8abad..2e172f6 100644
--- a/hw/pci.h
+++ b/hw/pci.h
@@ -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;
--
1.6.5.2.143.g8cc62
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [Qemu-devel] [PATCH 3/4] pci: interrupt status bit implementation
[not found] <cover.1259168040.git.mst@redhat.com>
2009-11-25 16:58 ` [Qemu-devel] [PATCH 1/4] pci: rearrange code for interrupts Michael S. Tsirkin
2009-11-25 16:58 ` [Qemu-devel] [PATCH 2/4] pci: track IRQ status Michael S. Tsirkin
@ 2009-11-25 16:58 ` Michael S. Tsirkin
2009-11-25 16:59 ` [Qemu-devel] [PATCH 4/4] pci: interrupt disable bit support Michael S. Tsirkin
3 siblings, 0 replies; 8+ messages in thread
From: Michael S. Tsirkin @ 2009-11-25 16:58 UTC (permalink / raw)
To: qemu-devel, anthony, yamahata
interrupt status is a mandatory feature in PCI spec,
so devices must implement it to be spec compliant.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
hw/pci.c | 22 ++++++++++++++++++++++
hw/pci.h | 1 +
2 files changed, 23 insertions(+), 0 deletions(-)
diff --git a/hw/pci.c b/hw/pci.c
index 7717461..844664b 100644
--- a/hw/pci.c
+++ b/hw/pci.c
@@ -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);
}
diff --git a/hw/pci.h b/hw/pci.h
index 2e172f6..c3f2c3f 100644
--- a/hw/pci.h
+++ b/hw/pci.h
@@ -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 */
--
1.6.5.2.143.g8cc62
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [Qemu-devel] [PATCH 4/4] pci: interrupt disable bit support
[not found] <cover.1259168040.git.mst@redhat.com>
` (2 preceding siblings ...)
2009-11-25 16:58 ` [Qemu-devel] [PATCH 3/4] pci: interrupt status bit implementation Michael S. Tsirkin
@ 2009-11-25 16:59 ` Michael S. Tsirkin
2009-11-26 3:04 ` [Qemu-devel] " Isaku Yamahata
3 siblings, 1 reply; 8+ messages in thread
From: Michael S. Tsirkin @ 2009-11-25 16:59 UTC (permalink / raw)
To: qemu-devel, anthony, yamahata
interrupt disable bit is mandatory in PCI spec,
so we must implement it to be spec compliant.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
hw/pci.c | 57 ++++++++++++++++++++++++++++++++++++++++-----------------
hw/pci.h | 4 ++++
2 files changed, 44 insertions(+), 17 deletions(-)
diff --git a/hw/pci.c b/hw/pci.c
index 844664b..3daae46 100644
--- a/hw/pci.c
+++ b/hw/pci.c
@@ -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);
}
diff --git a/hw/pci.h b/hw/pci.h
index c3f2c3f..994b8bc 100644
--- a/hw/pci.h
+++ b/hw/pci.h
@@ -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;
--
1.6.5.2.143.g8cc62
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [Qemu-devel] Re: [PATCH 4/4] pci: interrupt disable bit support
2009-11-25 16:59 ` [Qemu-devel] [PATCH 4/4] pci: interrupt disable bit support Michael S. Tsirkin
@ 2009-11-26 3:04 ` Isaku Yamahata
2009-11-26 9:53 ` Michael S. Tsirkin
2009-11-26 13:21 ` Michael S. Tsirkin
0 siblings, 2 replies; 8+ messages in thread
From: Isaku Yamahata @ 2009-11-26 3:04 UTC (permalink / raw)
To: Michael S. Tsirkin; +Cc: qemu-devel
On Wed, Nov 25, 2009 at 06:59:06PM +0200, Michael S. Tsirkin wrote:
> interrupt disable bit is mandatory in PCI spec,
> so we must implement it to be spec compliant.
>
> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> ---
> hw/pci.c | 57 ++++++++++++++++++++++++++++++++++++++++-----------------
> hw/pci.h | 4 ++++
> 2 files changed, 44 insertions(+), 17 deletions(-)
>
> diff --git a/hw/pci.c b/hw/pci.c
> index 844664b..3daae46 100644
> --- a/hw/pci.c
> +++ b/hw/pci.c
> @@ -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);
Let's use ! instead of != 0 for consistency.
> +}
> +
> +/* 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);
> + }
> +}
> +
"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().
> 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);
> }
>
> diff --git a/hw/pci.h b/hw/pci.h
> index c3f2c3f..994b8bc 100644
> --- a/hw/pci.h
> +++ b/hw/pci.h
> @@ -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;
>
--
yamahata
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Qemu-devel] Re: [PATCH 4/4] pci: interrupt disable bit support
2009-11-26 3:04 ` [Qemu-devel] " Isaku Yamahata
@ 2009-11-26 9:53 ` Michael S. Tsirkin
2009-11-26 12:38 ` Michael S. Tsirkin
2009-11-26 13:21 ` Michael S. Tsirkin
1 sibling, 1 reply; 8+ messages in thread
From: Michael S. Tsirkin @ 2009-11-26 9:53 UTC (permalink / raw)
To: Isaku Yamahata; +Cc: qemu-devel
On Thu, Nov 26, 2009 at 12:04:51PM +0900, Isaku Yamahata wrote:
> On Wed, Nov 25, 2009 at 06:59:06PM +0200, Michael S. Tsirkin wrote:
> > interrupt disable bit is mandatory in PCI spec,
> > so we must implement it to be spec compliant.
> >
> > Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> > ---
> > hw/pci.c | 57 ++++++++++++++++++++++++++++++++++++++++-----------------
> > hw/pci.h | 4 ++++
> > 2 files changed, 44 insertions(+), 17 deletions(-)
> >
> > diff --git a/hw/pci.c b/hw/pci.c
> > index 844664b..3daae46 100644
> > --- a/hw/pci.c
> > +++ b/hw/pci.c
> > @@ -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);
>
> 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.
> > +}
> > +
> > +/* 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);
> > + }
> > +}
> > +
>
> "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!
> 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!
> > 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);
> > }
> >
> > diff --git a/hw/pci.h b/hw/pci.h
> > index c3f2c3f..994b8bc 100644
> > --- a/hw/pci.h
> > +++ b/hw/pci.h
> > @@ -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;
> >
>
> --
> yamahata
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Qemu-devel] Re: [PATCH 4/4] pci: interrupt disable bit support
2009-11-26 9:53 ` Michael S. Tsirkin
@ 2009-11-26 12:38 ` Michael S. Tsirkin
0 siblings, 0 replies; 8+ messages in thread
From: Michael S. Tsirkin @ 2009-11-26 12:38 UTC (permalink / raw)
To: Isaku Yamahata; +Cc: qemu-devel
On Thu, Nov 26, 2009 at 11:53:16AM +0200, Michael S. Tsirkin wrote:
> On Thu, Nov 26, 2009 at 12:04:51PM +0900, Isaku Yamahata wrote:
> > On Wed, Nov 25, 2009 at 06:59:06PM +0200, Michael S. Tsirkin wrote:
> > > interrupt disable bit is mandatory in PCI spec,
> > > so we must implement it to be spec compliant.
> > >
> > > Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> > > ---
> > > hw/pci.c | 57 ++++++++++++++++++++++++++++++++++++++++-----------------
> > > hw/pci.h | 4 ++++
> > > 2 files changed, 44 insertions(+), 17 deletions(-)
> > >
> > > diff --git a/hw/pci.c b/hw/pci.c
> > > index 844664b..3daae46 100644
> > > --- a/hw/pci.c
> > > +++ b/hw/pci.c
> > > @@ -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);
> >
> > 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.
>
> > > +}
> > > +
> > > +/* 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);
> > > + }
> > > +}
> > > +
> >
> > "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!
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.
> > 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.
> > > 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);
> > > }
> > >
> > > diff --git a/hw/pci.h b/hw/pci.h
> > > index c3f2c3f..994b8bc 100644
> > > --- a/hw/pci.h
> > > +++ b/hw/pci.h
> > > @@ -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;
> > >
> >
> > --
> > yamahata
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Qemu-devel] Re: [PATCH 4/4] pci: interrupt disable bit support
2009-11-26 3:04 ` [Qemu-devel] " Isaku Yamahata
2009-11-26 9:53 ` Michael S. Tsirkin
@ 2009-11-26 13:21 ` Michael S. Tsirkin
1 sibling, 0 replies; 8+ messages in thread
From: Michael S. Tsirkin @ 2009-11-26 13:21 UTC (permalink / raw)
To: Isaku Yamahata; +Cc: qemu-devel
On Thu, Nov 26, 2009 at 12:04:51PM +0900, Isaku Yamahata wrote:
> On Wed, Nov 25, 2009 at 06:59:06PM +0200, Michael S. Tsirkin wrote:
> > interrupt disable bit is mandatory in PCI spec,
> > so we must implement it to be spec compliant.
> >
> > Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> > ---
> > hw/pci.c | 57 ++++++++++++++++++++++++++++++++++++++++-----------------
> > hw/pci.h | 4 ++++
> > 2 files changed, 44 insertions(+), 17 deletions(-)
> >
> > diff --git a/hw/pci.c b/hw/pci.c
> > index 844664b..3daae46 100644
> > --- a/hw/pci.c
> > +++ b/hw/pci.c
> > @@ -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);
>
> 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 !!.
--
MST
^ permalink raw reply [flat|nested] 8+ messages in thread