Skip to contentSkip to conversation

pcie port switch emulators

PATCH Superseded Isaku Yamahata 4 participants 25 messages Sep 6, 2010 – Sep 8, 2010 Kbuild

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

cover pcie port switch emulators

This patch series implements pcie port switch emulators which is basic part for pcie/q35 support.
This is for mst/pci tree.

some random comments
- pci bus reset
As Anthony is cleaning up qdev reset stuff, so pci bus reset code is commented out. Once the qdev clean up is done, the patch that enabled pci bus reset will be sent.

- vid/did
there are arbitrariness for pcie port switch. I just choose Intel and IT one to model them.

thanks,

Isaku Yamahata (14):
RESEND apb: fix typo.
pci: consolidate pci_add_capability_at_offset() into
pci_add_capability().
pci bridge: add helper function for ssvid capability.
pci: call hotplug callback even when not hotplug case for later use.
pci: make pci_parse_devfn() aware of func.
pci_ids.h: add vendor id of Texus Intesruments.
msi: implemented msi.
pcie: helper functions for pcie extended capability.
pcie port: define struct PCIEPort/PCIESlot and helper functions pcie root port: implement pcie root port.
pcie upstream port: pci express switch upstream port. pcie downstream port: pci express switch downstream port. pcie/hotplug: glue pushing attention button command. pcie_abp pcie/aer: glue aer error injection into qemu monitor.

 Makefile.objs        |    6 +-
 hw/acpi_piix4.c      |    3 +
 hw/apb_pci.c         |    6 +-
 hw/eepro100.c        |    4 +-
 hw/msi.c             |  362 +++++++++++
 hw/msi.h             |   41 ++
 hw/msix.c            |    3 +-
 hw/pci.c             |   70 ++-
 hw/pci.h             |   36 +-
 hw/pci_bridge.c      |   19 +
 hw/pci_bridge.h      |    3 +
 hw/pci_ids.h         |    2 +
 hw/pcie.c            | 1753 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pcie.h            |  186 ++++++
 hw/pcie_downstream.c |  224 +++++++
 hw/pcie_downstream.h |   33 +
 hw/pcie_port.c       |  188 ++++++
 hw/pcie_port.h       |   51 ++
 hw/pcie_root.c       |  247 +++++++
 hw/pcie_root.h       |   32 +
 hw/pcie_upstream.c   |  206 ++++++
 hw/pcie_upstream.h   |   32 +
 qemu-common.h        |    3 +
 qemu-monitor.hx      |   36 +
 sysemu.h             |    9 +
 25 files changed, 3520 insertions(+), 35 deletions(-)
 create mode 100644 hw/msi.c
 create mode 100644 hw/msi.h
 create mode 100644 hw/pcie.c
 create mode 100644 hw/pcie.h
 create mode 100644 hw/pcie_downstream.c
 create mode 100644 hw/pcie_downstream.h
 create mode 100644 hw/pcie_port.c
 create mode 100644 hw/pcie_port.h
 create mode 100644 hw/pcie_root.c
 create mode 100644 hw/pcie_root.h
 create mode 100644 hw/pcie_upstream.c
 create mode 100644 hw/pcie_upstream.h
#1 lore raw

PATCH 1/14 RESEND apb: fix typo.

fix typo.

---
 hw/apb_pci.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
hw/apb_pci.c +3 -3
index 10a5baa..c619112 100644
@@ -362,7 +362,7 @@ PCIBus *pci_apb_init(target_phys_addr_t special_base,
     /* APB secondary busses */
     pci_dev = pci_create_multifunction(d->bus, PCI_DEVFN(1, 0), true,
                                    "pbm-bridge");
-    br = DO_UPCAST(PCIBridge, dev, dev);
+    br = DO_UPCAST(PCIBridge, dev, pci_dev);
     pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 1",
                        pci_apb_map_irq);
     qdev_init_nofail(&pci_dev->qdev);
@@ -370,7 +370,7 @@ PCIBus *pci_apb_init(target_phys_addr_t special_base,
 
     pci_dev = pci_create_multifunction(d->bus, PCI_DEVFN(1, 1), true,
                                    "pbm-bridge");
-    br = DO_UPCAST(PCIBridge, dev, dev);
+    br = DO_UPCAST(PCIBridge, dev, pci_dev);
     pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 2",
                        pci_apb_map_irq);
     qdev_init_nofail(&pci_dev->qdev);
@@ -462,7 +462,7 @@ static PCIDeviceInfo pbm_pci_bridge_info = {
     .qdev.name = "pbm-bridge",
     .qdev.size = sizeof(PCIBridge),
     .qdev.vmsd = &vmstate_pci_device,
-    .qdev.reset = pci_brdige_reset,
+    .qdev.reset = pci_bridge_reset,
     .init = apb_pci_bridge_initfn,
     .exit = pci_bridge_exitfn,
     .config_write = pci_bridge_write_config,
Signature · 1 line
1.7.1.1
#2 lore raw

[Qemu-devel] Re: [PATCH 01/14] RESEND apb: fix typo.

Quoted 3 lines · Isaku Yamahata

fix typo.

This is separate from the express patches, right?
I'll appply this. Thanks!

Quoted 37 lines
---
 hw/apb_pci.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
hw/apb_pci.c +3 -3
index 10a5baa..c619112 100644
@@ -362,7 +362,7 @@ PCIBus *pci_apb_init(target_phys_addr_t special_base,
     /* APB secondary busses */
     pci_dev = pci_create_multifunction(d->bus, PCI_DEVFN(1, 0), true,
                                    "pbm-bridge");
-    br = DO_UPCAST(PCIBridge, dev, dev);
+    br = DO_UPCAST(PCIBridge, dev, pci_dev);
     pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 1",
                        pci_apb_map_irq);
     qdev_init_nofail(&pci_dev->qdev);
@@ -370,7 +370,7 @@ PCIBus *pci_apb_init(target_phys_addr_t special_base,
 
     pci_dev = pci_create_multifunction(d->bus, PCI_DEVFN(1, 1), true,
                                    "pbm-bridge");
-    br = DO_UPCAST(PCIBridge, dev, dev);
+    br = DO_UPCAST(PCIBridge, dev, pci_dev);
     pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 2",
                        pci_apb_map_irq);
     qdev_init_nofail(&pci_dev->qdev);
@@ -462,7 +462,7 @@ static PCIDeviceInfo pbm_pci_bridge_info = {
     .qdev.name = "pbm-bridge",
     .qdev.size = sizeof(PCIBridge),
     .qdev.vmsd = &vmstate_pci_device,
-    .qdev.reset = pci_brdige_reset,
+    .qdev.reset = pci_bridge_reset,
     .init = apb_pci_bridge_initfn,
     .exit = pci_bridge_exitfn,
     .config_write = pci_bridge_write_config,
Signature · 1 line
1.7.1.1
#3 lore raw

Re: [Qemu-devel] Re: [PATCH 01/14] RESEND apb: fix typo.

Quoted 7 lines · Michael S. Tsirkin
Quoted 3 lines · Isaku Yamahata

fix typo.

This is separate from the express patches, right?
I'll appply this. Thanks!

Since the patch that introduces the build breakage (pci_brdige_reset) has not been committed to master yet, can you instead drop that commit and introduce a fixed version? This will avoid problems with git bisect.

Quoted 40 lines
Quoted 38 lines

---
 hw/apb_pci.c |    6 +++---

 1 files changed, 3 insertions(+), 3 deletions(-)
hw/apb_pci.c +3 -4
index 10a5baa..c619112 100644
@@ -362,7 +362,7 @@ PCIBus *pci_apb_init(target_phys_addr_t special_base,
      /* APB secondary busses */
      pci_dev = pci_create_multifunction(d->bus, PCI_DEVFN(1, 0), true,
                                     "pbm-bridge");
-    br = DO_UPCAST(PCIBridge, dev, dev);
+    br = DO_UPCAST(PCIBridge, dev, pci_dev);
      pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 1",
                         pci_apb_map_irq);
      qdev_init_nofail(&pci_dev->qdev);
@@ -370,7 +370,7 @@ PCIBus *pci_apb_init(target_phys_addr_t special_base,
 
      pci_dev = pci_create_multifunction(d->bus, PCI_DEVFN(1, 1), true,
                                     "pbm-bridge");
-    br = DO_UPCAST(PCIBridge, dev, dev);
+    br = DO_UPCAST(PCIBridge, dev, pci_dev);
      pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 2",
                         pci_apb_map_irq);
      qdev_init_nofail(&pci_dev->qdev);
@@ -462,7 +462,7 @@ static PCIDeviceInfo pbm_pci_bridge_info = {
      .qdev.name = "pbm-bridge",
      .qdev.size = sizeof(PCIBridge),
      .qdev.vmsd = &vmstate_pci_device,
-    .qdev.reset = pci_brdige_reset,
+    .qdev.reset = pci_bridge_reset,
      .init = apb_pci_bridge_initfn,
      .exit = pci_bridge_exitfn,
      .config_write = pci_bridge_write_config,
--

1.7.1.1

#4 lore raw

Re: [Qemu-devel] Re: [PATCH 01/14] RESEND apb: fix typo.

Quoted 6 lines · Blue Swirl
Quoted 5 lines · Michael S. Tsirkin
Quoted 3 lines · Isaku Yamahata

fix typo.

Quoted 7 lines
Quoted 2 lines

This is separate from the express patches, right?
I'll appply this. Thanks!

Since the patch that introduces the build breakage (pci_brdige_reset) has not been committed to master yet, can you instead drop that commit and introduce a fixed version? This will avoid problems with git bisect.

Sure, thanks for pointing this out. Isaku, could
you just repost a fixed up patchset? Working git bisect is nice to have.

Quoted 40 lines
Quoted 40 lines
Quoted 38 lines

---
 hw/apb_pci.c |    6 +++---

 1 files changed, 3 insertions(+), 3 deletions(-)
hw/apb_pci.c +3 -4
index 10a5baa..c619112 100644
@@ -362,7 +362,7 @@ PCIBus *pci_apb_init(target_phys_addr_t special_base,
      /* APB secondary busses */
      pci_dev = pci_create_multifunction(d->bus, PCI_DEVFN(1, 0), true,
                                     "pbm-bridge");
-    br = DO_UPCAST(PCIBridge, dev, dev);
+    br = DO_UPCAST(PCIBridge, dev, pci_dev);
      pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 1",
                         pci_apb_map_irq);
      qdev_init_nofail(&pci_dev->qdev);
@@ -370,7 +370,7 @@ PCIBus *pci_apb_init(target_phys_addr_t special_base,
 
      pci_dev = pci_create_multifunction(d->bus, PCI_DEVFN(1, 1), true,
                                     "pbm-bridge");
-    br = DO_UPCAST(PCIBridge, dev, dev);
+    br = DO_UPCAST(PCIBridge, dev, pci_dev);
      pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 2",
                         pci_apb_map_irq);
      qdev_init_nofail(&pci_dev->qdev);
@@ -462,7 +462,7 @@ static PCIDeviceInfo pbm_pci_bridge_info = {
      .qdev.name = "pbm-bridge",
      .qdev.size = sizeof(PCIBridge),
      .qdev.vmsd = &vmstate_pci_device,
-    .qdev.reset = pci_brdige_reset,
+    .qdev.reset = pci_bridge_reset,
      .init = apb_pci_bridge_initfn,
      .exit = pci_bridge_exitfn,
      .config_write = pci_bridge_write_config,
--

1.7.1.1

#5 lore raw

Re: [Qemu-devel] Re: [PATCH 01/14] RESEND apb: fix typo.

Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Blue Swirl
Quoted 7 lines · Michael S. Tsirkin
Quoted 3 lines · Isaku Yamahata

fix typo.

This is separate from the express patches, right?
I'll appply this. Thanks!

Quoted 7 lines
Quoted 4 lines

Since the patch that introduces the build breakage (pci_brdige_reset) has not been committed to master yet, can you instead drop that commit and introduce a fixed version? This will avoid problems with git bisect.

Sure, thanks for pointing this out. Isaku, could
you just repost a fixed up patchset? Working git bisect is nice to have.

I should have included it in the commit log.

12bc74dfc1fe8cc57060a9eb94d0e9884c5c445e
Fortunately it's the head of pci branch.

thanks,

Quoted 42 lines
Quoted 41 lines
Quoted 41 lines
Quoted 39 lines

---
?hw/apb_pci.c | ? ?6 +++---
?1 files changed, 3 insertions(+), 3 deletions(-)

hw/apb_pci.c +0 -0
index 10a5baa..c619112 100644
@@ -362,7 +362,7 @@ PCIBus *pci_apb_init(target_phys_addr_t special_base,

? ? ?/* APB secondary busses */
? ? ?pci_dev = pci_create_multifunction(d->bus, PCI_DEVFN(1, 0), true, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "pbm-bridge");
- ? ?br = DO_UPCAST(PCIBridge, dev, dev);
+ ? ?br = DO_UPCAST(PCIBridge, dev, pci_dev);
? ? ?pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 1", ? ? ? ? ? ? ? ? ? ? ? ? pci_apb_map_irq);
? ? ?qdev_init_nofail(&pci_dev->qdev);

hw/apb_pci.c +2 -3
@@ -370,7 +370,7 @@ PCIBus *pci_apb_init(target_phys_addr_t special_base,
 
 ? ? ?pci_dev = pci_create_multifunction(d->bus, PCI_DEVFN(1, 1), true,
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "pbm-bridge");
- ? ?br = DO_UPCAST(PCIBridge, dev, dev);
+ ? ?br = DO_UPCAST(PCIBridge, dev, pci_dev);
 ? ? ?pci_bridge_map_irq(br, "Advanced PCI Bus secondary bridge 2",
 ? ? ? ? ? ? ? ? ? ? ? ? pci_apb_map_irq);
 ? ? ?qdev_init_nofail(&pci_dev->qdev);
@@ -462,7 +462,7 @@ static PCIDeviceInfo pbm_pci_bridge_info = {
 ? ? ?.qdev.name = "pbm-bridge",
 ? ? ?.qdev.size = sizeof(PCIBridge),
 ? ? ?.qdev.vmsd = &vmstate_pci_device,
- ? ?.qdev.reset = pci_brdige_reset,
+ ? ?.qdev.reset = pci_bridge_reset,
 ? ? ?.init = apb_pci_bridge_initfn,
 ? ? ?.exit = pci_bridge_exitfn,
 ? ? ?.config_write = pci_bridge_write_config,
--

1.7.1.1

Signature · 1 line
yamahata
#6 lore raw

PATCH 2/14 pci: consolidate pci_add_capability_at_offset() into pci_add_capability().

In reply to Isaku Yamahata

By making pci_add_capability() the special case of pci_add_capability_at_offset() of offset = 0,
consolidate pci_add_capability_at_offset() into pci_add_capability().

Cc Stefan Weil <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
---
 hw/eepro100.c |    4 ++--
 hw/msix.c     |    3 ++-
 hw/pci.c      |   33 ++++++++++++++++++---------------
 hw/pci.h      |    5 ++---
 4 files changed, 24 insertions(+), 21 deletions(-)
hw/eepro100.c +2 -2
index 2b75c8f..8cbc3aa 100644
@@ -539,8 +539,8 @@ static void e100_pci_reset(EEPRO100State * s, E100PCIDeviceInfo *e100_device)
     if (e100_device->power_management) {
         /* Power Management Capabilities */
         int cfg_offset = 0xdc;
-        int r = pci_add_capability_at_offset(&s->dev, PCI_CAP_ID_PM,
-                                             cfg_offset, PCI_PM_SIZEOF);
+        int r = pci_add_capability(&s->dev, PCI_CAP_ID_PM,
+                                   cfg_offset, PCI_PM_SIZEOF);
         assert(r >= 0);
         pci_set_word(pci_conf + cfg_offset + PCI_PM_PMC, 0x7e21);
 #if 0 /* TODO: replace dummy code for power management emulation. */
hw/msix.c +2 -1
index d99403a..7ce63eb 100644
@@ -73,7 +73,8 @@ static int msix_add_config(struct PCIDevice *pdev, unsigned short nentries,
     }
 
     pdev->msix_bar_size = new_size;
-    config_offset = pci_add_capability(pdev, PCI_CAP_ID_MSIX, MSIX_CAP_LENGTH);
+    config_offset = pci_add_capability(pdev, PCI_CAP_ID_MSIX,
+                                       0, MSIX_CAP_LENGTH);
     if (config_offset < 0)
         return config_offset;
     config = pdev->config + config_offset;
hw/pci.c +18 -15
index 2dc1577..754ffb3 100644
@@ -1682,11 +1682,25 @@ static void pci_del_option_rom(PCIDevice *pdev)
     pdev->rom_offset = 0;
 }
 
-/* Reserve space and add capability to the linked list in pci config space */
-int pci_add_capability_at_offset(PCIDevice *pdev, uint8_t cap_id,
-                                 uint8_t offset, uint8_t size)
+/*
+ * if !offset
+ * Reserve space and add capability to the linked list in pci config space
+ *
+ * if offset = 0,
+ * Find and reserve space and add capability to the linked list
+ * in pci config space */
+int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
+                       uint8_t offset, uint8_t size)
 {
-    uint8_t *config = pdev->config + offset;
+    uint8_t *config;
+    if (!offset) {
+        offset = pci_find_space(pdev, size);
+        if (!offset) {
+            return -ENOSPC;
+        }
+    }
+
+    config = pdev->config + offset;
     config[PCI_CAP_LIST_ID] = cap_id;
     config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
     pdev->config[PCI_CAPABILITY_LIST] = offset;
@@ -1699,17 +1713,6 @@ int pci_add_capability_at_offset(PCIDevice *pdev, uint8_t cap_id,
     return offset;
 }
 
-/* Find and reserve space and add capability to the linked list
- * in pci config space */
-int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
-{
-    uint8_t offset = pci_find_space(pdev, size);
-    if (!offset) {
-        return -ENOSPC;
-    }
-    return pci_add_capability_at_offset(pdev, cap_id, offset, size);
-}
-
 /* Unlink capability from the pci config space. */
 void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
 {
hw/pci.h +2 -3
index c551f96..2ddba59 100644
@@ -183,9 +183,8 @@ void pci_register_bar(PCIDevice *pci_dev, int region_num,
                             pcibus_t size, int type,
                             PCIMapIORegionFunc *map_func);
 
-int pci_add_capability(PCIDevice *pci_dev, uint8_t cap_id, uint8_t cap_size);
-int pci_add_capability_at_offset(PCIDevice *pci_dev, uint8_t cap_id,
-                                 uint8_t cap_offset, uint8_t cap_size);
+int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
+                       uint8_t offset, uint8_t size);
 
 void pci_del_capability(PCIDevice *pci_dev, uint8_t cap_id, uint8_t cap_size);
 
Signature · 1 line
1.7.1.1
#7 lore raw

[Qemu-devel] Re: [PATCH 02/14] pci: consolidate pci_add_capability_at_offset() into pci_add_capability().

Quoted 7 lines · Isaku Yamahata

By making pci_add_capability() the special case of pci_add_capability_at_offset() of offset = 0,
consolidate pci_add_capability_at_offset() into pci_add_capability().

Cc Stefan Weil <[email protected]>
Cc Michael S. Tsirkin <[email protected]>

Good stuff.

Here's an idea: Long term I think we should just give up on the ability to allocate capabilities dynamically, and always pass a valid offset. In particular, this would help us get rid of the used mask, and we won't have to pass in capability size.

virtio would then have
#define VIRTIO_PCI_MSIX_CAP_OFF 0x40
and msix.h would be changed to get the offset.

The reason is that we need to preserve the offsets for migration to work, anyway.

Have said all this, this is just an idea, it is not a must and can be a separate patch, or I might do this change myself.

Quoted 106 lines
---
 hw/eepro100.c |    4 ++--
 hw/msix.c     |    3 ++-
 hw/pci.c      |   33 ++++++++++++++++++---------------
 hw/pci.h      |    5 ++---
 4 files changed, 24 insertions(+), 21 deletions(-)
hw/eepro100.c +2 -2
index 2b75c8f..8cbc3aa 100644
@@ -539,8 +539,8 @@ static void e100_pci_reset(EEPRO100State * s, E100PCIDeviceInfo *e100_device)
     if (e100_device->power_management) {
         /* Power Management Capabilities */
         int cfg_offset = 0xdc;
-        int r = pci_add_capability_at_offset(&s->dev, PCI_CAP_ID_PM,
-                                             cfg_offset, PCI_PM_SIZEOF);
+        int r = pci_add_capability(&s->dev, PCI_CAP_ID_PM,
+                                   cfg_offset, PCI_PM_SIZEOF);
         assert(r >= 0);
         pci_set_word(pci_conf + cfg_offset + PCI_PM_PMC, 0x7e21);
 #if 0 /* TODO: replace dummy code for power management emulation. */
hw/msix.c +2 -1
index d99403a..7ce63eb 100644
@@ -73,7 +73,8 @@ static int msix_add_config(struct PCIDevice *pdev, unsigned short nentries,
     }
 
     pdev->msix_bar_size = new_size;
-    config_offset = pci_add_capability(pdev, PCI_CAP_ID_MSIX, MSIX_CAP_LENGTH);
+    config_offset = pci_add_capability(pdev, PCI_CAP_ID_MSIX,
+                                       0, MSIX_CAP_LENGTH);
     if (config_offset < 0)
         return config_offset;
     config = pdev->config + config_offset;
hw/pci.c +18 -15
index 2dc1577..754ffb3 100644
@@ -1682,11 +1682,25 @@ static void pci_del_option_rom(PCIDevice *pdev)
     pdev->rom_offset = 0;
 }
 
-/* Reserve space and add capability to the linked list in pci config space */
-int pci_add_capability_at_offset(PCIDevice *pdev, uint8_t cap_id,
-                                 uint8_t offset, uint8_t size)
+/*
+ * if !offset
+ * Reserve space and add capability to the linked list in pci config space
+ *
+ * if offset = 0,
+ * Find and reserve space and add capability to the linked list
+ * in pci config space */
+int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
+                       uint8_t offset, uint8_t size)
 {
-    uint8_t *config = pdev->config + offset;
+    uint8_t *config;
+    if (!offset) {
+        offset = pci_find_space(pdev, size);
+        if (!offset) {
+            return -ENOSPC;
+        }
+    }
+
+    config = pdev->config + offset;
     config[PCI_CAP_LIST_ID] = cap_id;
     config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
     pdev->config[PCI_CAPABILITY_LIST] = offset;
@@ -1699,17 +1713,6 @@ int pci_add_capability_at_offset(PCIDevice *pdev, uint8_t cap_id,
     return offset;
 }
 
-/* Find and reserve space and add capability to the linked list
- * in pci config space */
-int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
-{
-    uint8_t offset = pci_find_space(pdev, size);
-    if (!offset) {
-        return -ENOSPC;
-    }
-    return pci_add_capability_at_offset(pdev, cap_id, offset, size);
-}
-
 /* Unlink capability from the pci config space. */
 void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
 {
hw/pci.h +2 -3
index c551f96..2ddba59 100644
@@ -183,9 +183,8 @@ void pci_register_bar(PCIDevice *pci_dev, int region_num,
                             pcibus_t size, int type,
                             PCIMapIORegionFunc *map_func);
 
-int pci_add_capability(PCIDevice *pci_dev, uint8_t cap_id, uint8_t cap_size);
-int pci_add_capability_at_offset(PCIDevice *pci_dev, uint8_t cap_id,
-                                 uint8_t cap_offset, uint8_t cap_size);
+int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
+                       uint8_t offset, uint8_t size);
 
 void pci_del_capability(PCIDevice *pci_dev, uint8_t cap_id, uint8_t cap_size);
 
Signature · 1 line
1.7.1.1
#8 lore raw

PATCH 3/14 pci bridge: add helper function for ssvid capability.

In reply to Isaku Yamahata

helper function to add ssvid capability.

---
 hw/pci_bridge.c |   19 +++++++++++++++++++
 hw/pci_bridge.h |    3 +++
 2 files changed, 22 insertions(+), 0 deletions(-)
hw/pci_bridge.c +19 -0
index 198c3c7..638e3b3 100644
@@ -32,6 +32,25 @@
 #include "pci_bridge.h"
 #include "pci_internals.h"
 
+/* PCI bridge subsystem vendor ID helper functions */
+#define PCI_SSVID_SIZEOF        8
+#define PCI_SSVID_SVID          4
+#define PCI_SSVID_SSID          6
+
+int pci_bridge_ssvid_init(PCIDevice *dev, uint8_t offset,
+                          uint16_t svid, uint16_t ssid)
+{
+    int pos;
+    pos = pci_add_capability(dev, PCI_CAP_ID_SSVID, offset, PCI_SSVID_SIZEOF);
+    if (pos < 0) {
+        return pos;
+    }
+
+    pci_set_word(dev->config + pos + PCI_SSVID_SVID, svid);
+    pci_set_word(dev->config + pos + PCI_SSVID_SSID, ssid);
+    return pos;
+}
+
 /* Accessor function to get parent bridge device from pci bus. */
 PCIDevice *pci_bridge_get_device(PCIBus *bus)
 {
hw/pci_bridge.h +3 -0
index 63ada19..f6fade0 100644
@@ -28,6 +28,9 @@
 
 #include "pci.h"
 
+int pci_bridge_ssvid_init(PCIDevice *dev, uint8_t offset,
+                          uint16_t svid, uint16_t ssid);
+
 PCIDevice *pci_bridge_get_device(PCIBus *bus);
 PCIBus *pci_bridge_get_sec_bus(PCIBridge *br);
 
Signature · 1 line
1.7.1.1
#9 lore raw

PATCH 4/14 pci: call hotplug callback even when not hotplug case for later use.

In reply to Isaku Yamahata

call hotplug callback even when not hotplug case for later use. And move hotplug check into hotplug callback.
PCIE slot needs this for card presence detection.

---
 hw/acpi_piix4.c |    3 +++
 hw/pci.c        |    3 ++-
 2 files changed, 5 insertions(+), 1 deletions(-)
hw/acpi_piix4.c +3 -0
index bfa1d9a..24dfcf2 100644
@@ -611,6 +611,9 @@ static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev, int state)
     PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev,
                                 DO_UPCAST(PCIDevice, qdev, qdev));
 
+    if (!dev->qdev.hotplugged)
+        return 0;
+
     s->pci0_status.up = 0;
     s->pci0_status.down = 0;
     if (state) {
hw/pci.c +2 -1
index 754ffb3..bb9ddea 100644
@@ -1514,7 +1514,8 @@ static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
         pci_dev->romfile = qemu_strdup(info->romfile);
     pci_add_option_rom(pci_dev);
 
-    if (qdev->hotplugged) {
+    if (bus->hotplug) {
+        /* lower layer must check qdev->hotplugged */
         rc = bus->hotplug(bus->hotplug_qdev, pci_dev, 1);
         if (rc != 0) {
             int r = pci_unregister_device(&pci_dev->qdev);
Signature · 1 line
1.7.1.1
#10 lore raw

PATCH 5/14 pci: make pci_parse_devfn() aware of func.

In reply to Isaku Yamahata

make pci_parse_devfn() aware of func. With func = NULL it behave as before. This will be used later.

---
 hw/pci.c |   34 ++++++++++++++++++++++++++--------
 hw/pci.h |    2 ++
 2 files changed, 28 insertions(+), 8 deletions(-)
hw/pci.c +26 -8
index bb9ddea..f03b83e 100644
@@ -424,15 +424,18 @@ static void pci_set_default_subsystem_id(PCIDevice *pci_dev)
 }
 
 /*
- * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
+ * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL
+ *       [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
  */
-static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp)
+int pci_parse_devaddr(const char *addr, int *domp, int *busp,
+                      unsigned int *slotp, unsigned int *funcp)
 {
     const char *p;
     char *e;
     unsigned long val;
     unsigned long dom = 0, bus = 0;
-    unsigned slot = 0;
+    unsigned int slot = 0;
+    unsigned int func = 0;
 
     p = addr;
     val = strtoul(p, &e, 16);
@@ -454,11 +457,24 @@ static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *s
 	}
     }
 
-    if (dom > 0xffff || bus > 0xff || val > 0x1f)
-	return -1;
-
     slot = val;
 
+    if (funcp != NULL) {
+        if (*e != '.')
+            return -1;
+
+        p = e + 1;
+        val = strtoul(p, &e, 16);
+        if (e == p)
+            return -1;
+
+        func = val;
+    }
+
+    /* if funcp == NULL func is 0 */
+    if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7)
+	return -1;
+
     if (*e)
 	return -1;
 
@@ -469,6 +485,8 @@ static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *s
     *domp = dom;
     *busp = bus;
     *slotp = slot;
+    if (funcp != NULL)
+        *funcp = func;
     return 0;
 }
 
@@ -479,7 +497,7 @@ int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
     if (!strncmp(addr, "pci_addr=", 9)) {
         addr += 9;
     }
-    if (pci_parse_devaddr(addr, domp, busp, slotp)) {
+    if (pci_parse_devaddr(addr, domp, busp, slotp, NULL)) {
         monitor_printf(mon, "Invalid pci address\n");
         return -1;
     }
@@ -496,7 +514,7 @@ PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr)
         return pci_find_bus(pci_find_root_bus(0), 0);
     }
 
-    if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) {
+    if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) {
         return NULL;
     }
 
hw/pci.h +2 -0
index 2ddba59..2b4c318 100644
@@ -227,6 +227,8 @@ PCIBus *pci_find_bus(PCIBus *bus, int bus_num);
 PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function);
 PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr);
 
+int pci_parse_devaddr(const char *addr, int *domp, int *busp,
+                      unsigned int *slotp, unsigned int *funcp);
 int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
                      unsigned *slotp);
 
Signature · 1 line
1.7.1.1
#11 lore raw

PATCH 6/14 pci_ids.h: add vendor id of Texus Intesruments.

In reply to Isaku Yamahata

add vendor id of Texus Intesruments.

---
 hw/pci_ids.h |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
hw/pci_ids.h +2 -0
index 39e9f1d..82cba7e 100644
@@ -57,6 +57,8 @@
 #define PCI_VENDOR_ID_AMD                0x1022
 #define PCI_DEVICE_ID_AMD_LANCE          0x2000
 
+#define PCI_VENDOR_ID_TI                 0x104c
+
 #define PCI_VENDOR_ID_MOTOROLA           0x1057
 #define PCI_DEVICE_ID_MOTOROLA_MPC106    0x0002
 #define PCI_DEVICE_ID_MOTOROLA_RAVEN     0x4801
Signature · 1 line
1.7.1.1
#12 lore raw

PATCH 7/14 msi: implemented msi.

In reply to Isaku Yamahata

implemented msi support functions.

---
 Makefile.objs |    2 +-
 hw/msi.c      |  362 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/msi.h      |   41 +++++++
 hw/pci.h      |    5 +
 4 files changed, 409 insertions(+), 1 deletions(-)
 create mode 100644 hw/msi.c
 create mode 100644 hw/msi.h
Makefile.objs +1 -1
index 594894b..5f5a4c5 100644
@@ -186,7 +186,7 @@ hw-obj-$(CONFIG_PIIX4) += piix4.o
 # PCI watchdog devices
 hw-obj-y += wdt_i6300esb.o
 
-hw-obj-y += msix.o
+hw-obj-y += msix.o msi.o
 
 # PCI network cards
 hw-obj-y += ne2000.o
hw/msi.c new file +362 -0

362 lines not on this page · raw message

hw/msi.h new file +41 -0

41 lines not on this page · raw message

hw/pci.h +5 -0
index 2b4c318..9387a03 100644
@@ -115,6 +115,8 @@ enum {
     /* multifunction capable device */
 #define QEMU_PCI_CAP_MULTIFUNCTION_BITNR        2
     QEMU_PCI_CAP_MULTIFUNCTION = (1 << QEMU_PCI_CAP_MULTIFUNCTION_BITNR),
+
+    QEMU_PCI_CAP_MSI = 0x4,
 };
 
 struct PCIDevice {
@@ -168,6 +170,9 @@ struct PCIDevice {
     /* Version id needed for VMState */
     int32_t version_id;
 
+    /* Offset of MSI capability in config space */
+    uint8_t msi_cap;
+
     /* Location of option rom */
     char *romfile;
     ram_addr_t rom_offset;
Signature · 1 line
1.7.1.1
#13 lore raw

[Qemu-devel] Re: [PATCH 07/14] msi: implemented msi.

Quoted 3 lines · Isaku Yamahata

implemented msi support functions.

Good stuff. Below I suggest a couple of minor correctness issues, and a couple of API changes. Below are also some suggestions to improve readability a bit: mostly there are too many one-liners used only once or twice, with names not informative enough to guess what they do, so I suggest we opencode them as one ends up reading the source anyway.

Thanks!

Quoted 43 lines
---
 Makefile.objs |    2 +-
 hw/msi.c      |  362 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/msi.h      |   41 +++++++
 hw/pci.h      |    5 +
 4 files changed, 409 insertions(+), 1 deletions(-)
 create mode 100644 hw/msi.c
 create mode 100644 hw/msi.h
Makefile.objs +1 -1
index 594894b..5f5a4c5 100644
@@ -186,7 +186,7 @@ hw-obj-$(CONFIG_PIIX4) += piix4.o
 # PCI watchdog devices
 hw-obj-y += wdt_i6300esb.o
 
-hw-obj-y += msix.o
+hw-obj-y += msix.o msi.o
 
 # PCI network cards
 hw-obj-y += ne2000.o
hw/msi.c new file +15 -0
new file mode 100644
index 0000000..dbe89ee
@@ -0,0 +1,362 @@
+/*
+ * msi.c
+ *
+ * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
+ *                    VA Linux Systems Japan K.K.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.

hw/msi.c line 25

Quoted 10 lines
hw/msi.c new file +10 -0
+
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "msi.h"
+
+#define PCI_MSI_PENDING_32      0x10
+#define PCI_MSI_PENDING_64      0x14
+

Longer term, pls try to add this to linux, btw. If you manage to we'll then move this to pci_regs.h

hw/msi.c line 28

Quoted 3 lines
hw/msi.c new file +3 -0
+/* PCI_MSI_FLAGS */
+#define PCI_MSI_FLAGS_QSIZE_SHIFT       4
+#define PCI_MSI_FLAGS_QMASK_SHIFT       1

Use ffs on macros from pci_regs.h :
PCI_MSI_FLAGS_QSIZE_SHIFT -> (ffs(PCI_MSI_FLAGS_QSIZE) - 1)
as there's a single user, we don't need a macro then.

hw/msi.c line 31

Quoted 3 lines
hw/msi.c new file +3 -0
+
+/* PCI_MSI_ADDRESS_LO */
+#define PCI_MSI_ADDRESS_LO_RESERVED_MASK        0x3

Better to have the valid bits than invalid bits:
#define PCI_MSI_ADDRESS_LO_MASK (~0x3)
And then use directly.
again, we can try adding this to linux long term.

hw/msi.c line 36

Quoted 5 lines
hw/msi.c new file +5 -0
+
+#define PCI_MSI_32_SIZEOF       0x0a
+#define PCI_MSI_64_SIZEOF       0x0e
+#define PCI_MSI_32M_SIZEOF      0x14
+#define PCI_MSI_64M_SIZEOF      0x18

Note to self: if we get rid of cap allocator, we won't need the above.

Quoted 15 lines
hw/msi.c new file +15 -0
+
+#define MSI_DEBUG
+#ifdef MSI_DEBUG
+# define MSI_DPRINTF(fmt, ...)                                          \
+    fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
+#else
+# define MSI_DPRINTF(fmt, ...)  do { } while (0)
+#endif
+#define MSI_DEV_PRINTF(dev, fmt, ...)                                   \
+    MSI_DPRINTF("%s:%x " fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
+
+static inline bool msi_test_bit(uint32_t bitmap, uint8_t bit)
+{
+    return bitmap & (1 << bit);
+}

hw/msi.c line 61

Quoted 10 lines
hw/msi.c new file +10 -0
+
+static inline void msi_set_bit(uint32_t *bitmap, uint8_t bit)
+{
+    *bitmap |= (1 << bit);
+}
+
+static inline void msi_clear_bit(uint32_t *bitmap, uint8_t bit)
+{
+    *bitmap &= ~(1 << bit);
+}

The above 3 are much better open-coded - all they do is simple math. Also, I think we should use 1U << - the above is signed and so it will cause integer overflow when bit is 31.

hw/msi.c line 63

Quoted 2 lines
hw/msi.c new file +2 -0
+
+

Two empty lines -> one

Quoted 8 lines
hw/msi.c new file +8 -0
+/* multiple vector only suport power of 2 and up to 32 */
+static inline uint8_t ilog2(uint8_t nr_vector)
+{
+    switch (nr_vector) {
+    case 1:
+        return 0;
+    case 2:
+        return 1;

hw/msi.c line 81

Quoted 10 lines
hw/msi.c new file +10 -0
+    case 4:
+        return 2;
+    case 8:
+        return 3;
+    case 16:
+        return 4;
+    case 32:
+        return 5;
+    default:
+        abort();

Not intuitive to abort in a mathematical function IMO.

hw/msi.c line 86

Quoted 5 lines
hw/msi.c new file +5 -0
+        break;
+    }
+    return 0;
+}
+

Please just use ffs opencoded, there's a single user. ilog -> ffs(n) - 1;
and do this after range-checking as suggested below.

hw/msi.c line 96

Quoted 10 lines
hw/msi.c new file +10 -0
+static inline bool is_mask_bit_support(uint16_t control)
+{
+    return control & PCI_MSI_FLAGS_MASKBIT;
+}
+
+static inline bool is_64bit_address(uint16_t control)
+{
+    return control & PCI_MSI_FLAGS_64BIT;
+}
+

These 2 function names are especially confusing.
The above 2 are better open-coded.

hw/msi.c line 101

Quoted 5 lines
hw/msi.c new file +5 -0
+static inline uint8_t msi_vector_order(uint16_t control)
+{
+    return (control & PCI_MSI_FLAGS_QSIZE) >> PCI_MSI_FLAGS_QSIZE_SHIFT;
+}
+

above only used once? better opencoded

hw/msi.c line 106

Quoted 5 lines
hw/msi.c new file +5 -0
+static inline uint8_t msi_nr_vector(uint16_t control)
+{
+    return 1 << msi_vector_order(control);
+}
+

Rename msi_nr_vectors.

hw/msi.c line 111

Quoted 5 lines
hw/msi.c new file +5 -0
+static inline bool msi_control_enabled(uint16_t control)
+{
+    return control & PCI_MSI_FLAGS_ENABLE;
+}
+

above is better opencoded.

Quoted 8 lines
hw/msi.c new file +8 -0
+static inline uint8_t msi_cap_sizeof(uint16_t control)
+{
+    switch (control & (PCI_MSI_FLAGS_MASKBIT | PCI_MSI_FLAGS_64BIT)) {
+    case PCI_MSI_FLAGS_MASKBIT | PCI_MSI_FLAGS_64BIT:
+        return PCI_MSI_64M_SIZEOF;
+    case PCI_MSI_FLAGS_64BIT:
+        return PCI_MSI_64_SIZEOF;
+    case PCI_MSI_FLAGS_MASKBIT:

hw/msi.c line 129

Quoted 10 lines
hw/msi.c new file +10 -0
+        return PCI_MSI_32M_SIZEOF;
+    case 0:
+        return PCI_MSI_32_SIZEOF;
+    default:
+        abort();
+        break;
+    }
+    return 0;
+}
+

Note to self: this will go away too if we get rid of cap allocator.

Quoted 5 lines
hw/msi.c new file +5 -0
+static inline uint16_t msi_control_reg(const PCIDevice* dev)
+{
+    return dev->msi_cap + PCI_MSI_FLAGS;
+}
+

hw/msi.c line 144

Quoted 10 lines
hw/msi.c new file +10 -0
+static inline uint32_t msi_lower_address_reg(const PCIDevice* dev)
+{
+    return dev->msi_cap + PCI_MSI_ADDRESS_LO;
+}
+
+static inline uint32_t msi_upper_address_reg(const PCIDevice* dev)
+{
+    return dev->msi_cap + PCI_MSI_ADDRESS_HI;
+}
+

I think above are better open-coded - a bit less sure here as I understand you want consistency between registers.
In any case, I think names should be consistent with macros and be explicit in that they return an offset:
e.g. msi_lower_address_reg -> msi_address_lo_off Also, return uint8_t and not uint32_t: these registers always live in a traditional config space.

hw/msi.c line 145

Quoted 1 line
hw/msi.c new file +1 -0
+static inline uint16_t msi_data_reg(const PCIDevice* dev, bool is64bit)

msi64bit would be a better name than is64bit

Quoted 12 lines
hw/msi.c new file +12 -0
+{
+    return dev->msi_cap + (is64bit ?  PCI_MSI_DATA_64 : PCI_MSI_DATA_32);
+}
+
+static inline uint32_t msi_mask_reg(const PCIDevice* dev, bool is64bit)
+{
+    return dev->msi_cap + (is64bit ? PCI_MSI_MASK_64 : PCI_MSI_MASK_32);
+}
+
+static inline uint32_t msi_pending_reg(const PCIDevice* dev, bool is64bit)
+{
+    return dev->msi_cap + (is64bit ? PCI_MSI_PENDING_64 : PCI_MSI_PENDING_32);

hw/msi.c line 167

Quoted 10 lines
hw/msi.c new file +10 -0
+}
+
+bool msi_enabled(const PCIDevice *dev)
+{
+    return msi_present(dev) &&
+        msi_control_enabled(pci_get_word(dev->config + msi_control_reg(dev)));
+}
+
+int msi_init(struct PCIDevice *dev, uint8_t offset,
+             uint8_t nr_vector, uint16_t flags)

rename nr_vector -> nr_vectors here and elsewhere

hw/msi.c line 172

Quoted 5 lines
hw/msi.c new file +5 -0
+{
+    uint8_t vector_order = ilog2(nr_vector);
+    bool is64bit = is_64bit_address(flags);
+    uint8_t cap_size;
+    int config_offset;

maybe add assert on nr_vector: should be

	assert(!((nr_vector - 1) & nr_vector));
	assert(nr_vector > 0);
	assert(nr_vector <= 32);
Quoted 19 lines
hw/msi.c new file +19 -0
+    MSI_DEV_PRINTF(dev,
+                   "init offset: 0x%"PRIx8" vector: %"PRId8
+                   " flags 0x%"PRIx16"\n", offset, nr_vector, flags);
+
+    flags &= PCI_MSI_FLAGS_MASKBIT | PCI_MSI_FLAGS_64BIT;
+    flags |= (vector_order << PCI_MSI_FLAGS_QMASK_SHIFT) & PCI_MSI_FLAGS_QMASK;
+
+    cap_size = msi_cap_sizeof(flags);
+    config_offset = pci_add_capability(dev, PCI_CAP_ID_MSI, offset, cap_size);
+    if (config_offset < 0) {
+        return config_offset;
+    }
+
+    dev->msi_cap = config_offset;
+    dev->cap_present |= QEMU_PCI_CAP_MSI;
+
+    pci_set_word(dev->config + msi_control_reg(dev), flags);
+    pci_set_word(dev->wmask + msi_control_reg(dev),
+                 PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);

hw/msi.c line 201

Quoted 10 lines
hw/msi.c new file +10 -0
+    pci_set_long(dev->wmask + msi_lower_address_reg(dev),
+                 ~PCI_MSI_ADDRESS_LO_RESERVED_MASK);
+    if (is64bit) {
+        pci_set_long(dev->wmask + msi_upper_address_reg(dev), 0xffffffff);
+    }
+    pci_set_word(dev->wmask + msi_data_reg(dev, is64bit), 0xffff);
+
+    if (is_mask_bit_support(flags)) {
+        pci_set_long(dev->wmask + msi_mask_reg(dev, is64bit),
+                     (1 << nr_vector) - 1);

1 is signed. I think this should be 1U << nr_vector, otherwise you get signed integer overflow for nr >= 31.

Quoted 118 lines
hw/msi.c new file +118 -0
+    }
+    return config_offset;
+}
+
+void msi_uninit(struct PCIDevice *dev)
+{
+    uint16_t control = pci_get_word(dev->config + msi_control_reg(dev));
+    uint8_t cap_size = msi_cap_sizeof(control);
+    pci_del_capability(dev, PCI_CAP_ID_MSIX, cap_size);
+    MSI_DEV_PRINTF(dev, "uninit\n");
+}
+
+void msi_reset(PCIDevice *dev)
+{
+    uint16_t control;
+    bool is64bit;
+
+    control = pci_get_word(dev->config + msi_control_reg(dev));
+    control &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
+    is64bit = is_64bit_address(control);
+
+    pci_set_word(dev->config + msi_control_reg(dev), control);
+    pci_set_long(dev->config + msi_lower_address_reg(dev), 0);
+    if (is64bit) {
+        pci_set_long(dev->config + msi_upper_address_reg(dev), 0);
+    }
+    pci_set_word(dev->config + msi_data_reg(dev, is64bit), 0);
+    if (is_mask_bit_support(control)) {
+        pci_set_long(dev->config + msi_mask_reg(dev, is64bit), 0);
+        pci_set_long(dev->config + msi_pending_reg(dev, is64bit), 0);
+    }
+    MSI_DEV_PRINTF(dev, "reset\n");
+}
+
+static bool msi_is_masked(const PCIDevice *dev, uint8_t vector)
+{
+    uint16_t control = pci_get_word(dev->config + msi_control_reg(dev));
+    uint32_t mask;
+
+    if (!is_mask_bit_support(control)) {
+        return false;
+    }
+
+    mask = pci_get_long(dev->config +
+                        msi_mask_reg(dev, is_64bit_address(control)));
+    return msi_test_bit(mask, vector);
+}
+
+static void msi_set_pending(PCIDevice *dev, uint8_t vector)
+{
+    uint16_t control = pci_get_word(dev->config + msi_control_reg(dev));
+    bool is64bit = is_64bit_address(control);
+    uint32_t pending;
+
+    assert(is_mask_bit_support(control));
+
+    pending = pci_get_long(dev->config + msi_pending_reg(dev, is64bit));
+    msi_set_bit(&pending, vector);
+    pci_set_long(dev->config + msi_pending_reg(dev, is64bit), pending);
+    MSI_DEV_PRINTF(dev, "pending vector 0x%"PRIx8"\n", vector);
+}
+
+void msi_notify(PCIDevice *dev, uint8_t vector)
+{
+    uint16_t control = pci_get_word(dev->config + msi_control_reg(dev));
+    bool is64bit = is_64bit_address(control);
+    uint8_t nr_vector = msi_nr_vector(control);
+    uint64_t address;
+    uint32_t data;
+
+    assert(vector < nr_vector);
+    if (msi_is_masked(dev, vector)) {
+        msi_set_pending(dev, vector);
+        return;
+    }
+
+    if (is64bit){
+        address = pci_get_quad(dev->config + msi_lower_address_reg(dev));
+    } else {
+        address = pci_get_long(dev->config + msi_lower_address_reg(dev));
+    }
+
+    /* upper bit 31:16 is zero */
+    data = pci_get_word(dev->config + msi_data_reg(dev, is64bit));
+    if (nr_vector > 1) {
+        data &= ~(nr_vector - 1);
+        data |= vector;
+    }
+
+    MSI_DEV_PRINTF(dev,
+                   "notify vector 0x%"PRIx8
+                   " address: 0x%"PRIx64" data: 0x%"PRIx32"\n",
+                   vector, address, data);
+    stl_phys(address, data);
+}
+
+/* call this function after updating configs by pci_default_write_config() */
+void msi_write_config(PCIDevice *dev, uint32_t addr, uint32_t val, int len)
+{
+    uint16_t control = pci_get_word(dev->config + msi_control_reg(dev));
+    bool is64bit = is_64bit_address(control);
+    uint8_t nr_vector = msi_nr_vector(control);
+
+#ifdef MSI_DEBUG
+    uint8_t cap_size = msi_cap_sizeof(control & (PCI_MSI_FLAGS_MASKBIT |
+                                                 PCI_MSI_FLAGS_64BIT));
+    if (ranges_overlap(addr, len, dev->msi_cap, cap_size)) {
+        MSI_DEV_PRINTF(dev, "addr 0x%"PRIx32" val 0x%"PRIx32" len %d\n",
+                       addr, val, len);
+        MSI_DEV_PRINTF(dev, "ctrl: 0x%"PRIx16" address: 0x%"PRIx32,
+                       control,
+                       pci_get_long(dev->config + msi_lower_address_reg(dev)));
+        if (is64bit) {
+            fprintf(stderr, " addrss-hi: 0x%"PRIx32,
+                    pci_get_long(dev->config + msi_upper_address_reg(dev)));
+        }
+        fprintf(stderr, " data: 0x%"PRIx16,
+                pci_get_word(dev->config + msi_data_reg(dev, is64bit)));

hw/msi.c line 329

Quoted 10 lines
hw/msi.c new file +10 -0
+        if (is_mask_bit_support(control)) {
+            fprintf(stderr, " mask 0x%"PRIx32" pending 0x%"PRIx32,
+                    pci_get_long(dev->config + msi_mask_reg(dev, is64bit)),
+                    pci_get_long(dev->config + msi_pending_reg(dev, is64bit)));
+        }
+        fprintf(stderr, "\n");
+    }
+#endif
+
+    if (!msi_control_enabled(control)) {

Should not we clear pending bits here?

hw/msi.c line 331

Quoted 2 lines
hw/msi.c new file +2 -0
+        return;
+    }

We must also clear INTx# interrupts when msi is enabled.

hw/msi.c line 338

Quoted 7 lines
hw/msi.c new file +7 -0
+
+    if (!is_mask_bit_support(control)) {
+        /* if per vector masking isn't support,
+           there is no pending interrupt. */
+        return;
+    }
+

I think we also need to clear pending bits if # of vectors is reduced.

We need to also decide what to do if software tries to enable more vectors than supported. I suggest setting allocated vectors to nr_vectors in this case.

hw/msi.c line 340

Quoted 2 lines
hw/msi.c new file +2 -0
+    if (range_covers_byte(addr, len, msi_control_reg(dev)) || /* MSI enable */
+        ranges_overlap(addr, len, msi_mask_reg(dev, is64bit), 4)) {

I think it is better to reduce nesting.
Reverse the condition and return if this is not an msi write.

hw/msi.c line 347

Quoted 7 lines
hw/msi.c new file +7 -0
+        uint32_t pending =
+            pci_get_long(dev->config + msi_pending_reg(dev, is64bit));
+        uint8_t vector;
+
+        /* deliver pending interrupts which are unmasked */
+        for (vector = 0; vector < nr_vector; ++vector) {
+            if (msi_is_masked(dev, vector) || !msi_test_bit(pending, vector)) {

I am confused. This is called after mask is updated, right? So msi_is_masked means vector was masked, not unmasked? I think the logic is reversed here.

hw/msi.c line 358

Quoted 11 lines
hw/msi.c new file +11 -0
+                continue;
+            }
+
+            msi_clear_bit(&pending, vector);
+            pci_set_long(dev->config + msi_pending_reg(dev, is64bit), pending);
+            msi_notify(dev, vector);
+        }
+    }
+}
+
+uint8_t msi_nr_allocated_vector(const PCIDevice *dev)

rename msi_nr_vectors_allocated to match spec.

Quoted 29 lines
hw/msi.c new file +4 -0
+{
+    uint16_t control = pci_get_word(dev->config + msi_control_reg(dev));
+    return msi_nr_vector(control);
+}
hw/msi.h new file +19 -0
new file mode 100644
index 0000000..1131c6e
@@ -0,0 +1,41 @@
+/*
+ * msi.h
+ *
+ * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
+ *                    VA Linux Systems Japan K.K.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */

hw/msi.h line 29

Quoted 10 lines
hw/msi.h new file +10 -0
+
+#ifndef QEMU_MSI_H
+#define QEMU_MSI_H
+
+#include "qemu-common.h"
+#include "pci.h"
+
+bool msi_enabled(const PCIDevice *dev);
+int msi_init(struct PCIDevice *dev, uint8_t offset,
+             uint8_t nr_vector, uint16_t flags);

So users need to encode flags? Where do they get them? And to encode, we need registers internal to msi.c or duplicate ... Would not it be better to pass msi_64_bit and msi_per_vector_mask?

Quoted 17 lines
hw/msi.h new file +12 -0
+void msi_uninit(struct PCIDevice *dev);
+void msi_reset(PCIDevice *dev);
+void msi_notify(PCIDevice *dev, uint8_t vector);
+void msi_write_config(PCIDevice *dev, uint32_t addr, uint32_t val, int len);
+uint8_t msi_nr_allocated_vector(const PCIDevice *dev);
+
+static inline bool msi_present(const PCIDevice *dev)
+{
+    return dev->cap_present & QEMU_PCI_CAP_MSI;
+}
+
+#endif /* QEMU_MSI_H */
hw/pci.h +0 -0
index 2b4c318..9387a03 100644

hw/pci.h enum { · line 119

Quoted 5 lines
hw/pci.h +2 -0
@@ -115,6 +115,8 @@ enum {
     /* multifunction capable device */
 #define QEMU_PCI_CAP_MULTIFUNCTION_BITNR        2
     QEMU_PCI_CAP_MULTIFUNCTION = (1 << QEMU_PCI_CAP_MULTIFUNCTION_BITNR),
+
+    QEMU_PCI_CAP_MSI = 0x4,

These are internal bits so we do not need to preserve any values. Put this cap right before or right after MSIX please.

Quoted 15 lines
hw/pci.h +3 -0
 };
 
 struct PCIDevice {
@@ -168,6 +170,9 @@ struct PCIDevice {
     /* Version id needed for VMState */
     int32_t version_id;
 
+    /* Offset of MSI capability in config space */
+    uint8_t msi_cap;
+
     /* Location of option rom */
     char *romfile;
     ram_addr_t rom_offset;
Signature · 1 line
1.7.1.1
#14 lore raw

[Qemu-devel] Re: [PATCH 07/14] msi: implemented msi.

Thank you for through review.

Quoted 11 lines · Michael S. Tsirkin

hw/msi.c line 347

Quoted 7 lines
hw/msi.c new file +7 -0
+        uint32_t pending =
+            pci_get_long(dev->config + msi_pending_reg(dev, is64bit));
+        uint8_t vector;
+
+        /* deliver pending interrupts which are unmasked */
+        for (vector = 0; vector < nr_vector; ++vector) {
+            if (msi_is_masked(dev, vector) || !msi_test_bit(pending, vector)) {

I am confused. This is called after mask is updated, right? So msi_is_masked means vector was masked, not unmasked? I think the logic is reversed here.

I suppose you were missing the following continue.

Quoted 1 line
Quoted 1 line

+ continue;

                     ^^^^^^^^^ Here
Quoted 10 lines
Quoted 10 lines
hw/msi.c new file +10 -0
+            }
+
+            msi_clear_bit(&pending, vector);
+            pci_set_long(dev->config + msi_pending_reg(dev, is64bit), pending);
+            msi_notify(dev, vector);
+        }
+    }
+}
+
+uint8_t msi_nr_allocated_vector(const PCIDevice *dev)
Signature · 1 line
yamahata
#15 lore raw

[Qemu-devel] Re: [PATCH 07/14] msi: implemented msi.

Quoted 15 lines · Isaku Yamahata

Thank you for through review.

Quoted 11 lines · Michael S. Tsirkin

hw/msi.c line 347

Quoted 7 lines
hw/msi.c new file +7 -0
+        uint32_t pending =
+            pci_get_long(dev->config + msi_pending_reg(dev, is64bit));
+        uint8_t vector;
+
+        /* deliver pending interrupts which are unmasked */
+        for (vector = 0; vector < nr_vector; ++vector) {
+            if (msi_is_masked(dev, vector) || !msi_test_bit(pending, vector)) {

I am confused. This is called after mask is updated, right? So msi_is_masked means vector was masked, not unmasked? I think the logic is reversed here.

Quoted 5 lines

I suppose you were missing the following continue.

Quoted 1 line
Quoted 1 line

+ continue;

                     ^^^^^^^^^ Here

I see. You are right. The block is small enough to maybe make it worthwhile to revert the logic and avoid continue. It's up to you though, there's no bug here.

Quoted 13 lines
Quoted 10 lines
Quoted 10 lines
hw/msi.c new file +10 -0
+            }
+
+            msi_clear_bit(&pending, vector);
+            pci_set_long(dev->config + msi_pending_reg(dev, is64bit), pending);
+            msi_notify(dev, vector);
+        }
+    }
+}
+
+uint8_t msi_nr_allocated_vector(const PCIDevice *dev)
Signature · 1 line
yamahata
#16 lore raw

PATCH 8/14 pcie: helper functions for pcie extended capability.

In reply to Isaku Yamahata

This patch implements helper functions for pci express extended capability.
NOTE: presence detection depends on pci_qdev_init() change.
PCIExpressDevice::aer_log_max is in PCIDevice for device property.

---
 Makefile.objs |    1 +
 hw/pci.h      |   24 +
 hw/pcie.c     | 1668 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pcie.h     |  186 +++++++
 qemu-common.h |    1 +
 5 files changed, 1880 insertions(+), 0 deletions(-)
 create mode 100644 hw/pcie.c
 create mode 100644 hw/pcie.h
Makefile.objs +1 -0
index 5f5a4c5..eeb5134 100644
@@ -186,6 +186,7 @@ hw-obj-$(CONFIG_PIIX4) += piix4.o
 # PCI watchdog devices
 hw-obj-y += wdt_i6300esb.o
 
+hw-obj-y += pcie.o
 hw-obj-y += msix.o msi.o
 
 # PCI network cards
hw/pci.h +24 -0
index 9387a03..3f912b8 100644
@@ -9,6 +9,8 @@
 /* PCI includes legacy ISA access.  */
 #include "isa.h"
 
+#include "pcie.h"
+
 /* PCI bus */
 
 #define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))
@@ -173,6 +175,12 @@ struct PCIDevice {
     /* Offset of MSI capability in config space */
     uint8_t msi_cap;
 
+    /* PCI Express */
+    PCIExpressDevice *exp;
+    /* Theoretically this belongs to  PCIExpressDevice.
+       However it is here for property and save/load */
+    struct pcie_aer_log aer_log;
+
     /* Location of option rom */
     char *romfile;
     ram_addr_t rom_offset;
@@ -368,6 +376,22 @@ static inline uint32_t pci_config_size(const PCIDevice *d)
     return pci_is_express(d) ? PCIE_CONFIG_SPACE_SIZE : PCI_CONFIG_SPACE_SIZE;
 }
 
+
+/* These are pci express specific, so should belong to pcie.h.
+   they're here to avoid header inclusion error. */
+static inline uint8_t pci_pcie_cap(const PCIDevice *d)
+{
+    return d->exp ? d->exp->exp_cap : 0;
+}
+
+/* AER */
+static inline uint16_t pcie_aer_cap(const PCIDevice *d)
+{
+    assert(d->exp);
+    return d->exp->aer_cap;
+}
+
+
 /* These are not pci specific. Should move into a separate header.
  * Only pci.c uses them, so keep them here for now.
  */
hw/pcie.c new file +1668 -0

1,668 lines not on this page · raw message

hw/pcie.h new file +186 -0

186 lines not on this page · raw message

qemu-common.h +1 -0
index d735235..6d9ee26 100644
@@ -219,6 +219,7 @@ typedef struct PCIHostState PCIHostState;
 typedef struct PCIExpressHost PCIExpressHost;
 typedef struct PCIBus PCIBus;
 typedef struct PCIDevice PCIDevice;
+typedef struct PCIExpressDevice PCIExpressDevice;
 typedef struct PCIBridge PCIBridge;
 typedef struct SerialState SerialState;
 typedef struct IRQState *qemu_irq;
Signature · 1 line
1.7.1.1
#17 lore raw

PATCH 9/14 pcie port: define struct PCIEPort/PCIESlot and helper functions

In reply to Isaku Yamahata

define struct PCIEPort which represents common part
of pci express port.(root, upstream and downstream.) add a helper function for pcie port which can be used commonly by root/upstream/downstream port.
define struct PCIESlot which represents common part of pcie slot.(root and downstream.) and helper functions for it. helper functions for chassis, slot -> PCIESlot conversion.

---
 Makefile.objs  |    2 +-
 hw/pcie_port.c |  106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pcie_port.h |   51 +++++++++++++++++++++++++++
 qemu-common.h  |    2 +
 4 files changed, 160 insertions(+), 1 deletions(-)
 create mode 100644 hw/pcie_port.c
 create mode 100644 hw/pcie_port.h
Makefile.objs +1 -1
index eeb5134..c73d12b 100644
@@ -186,7 +186,7 @@ hw-obj-$(CONFIG_PIIX4) += piix4.o
 # PCI watchdog devices
 hw-obj-y += wdt_i6300esb.o
 
-hw-obj-y += pcie.o
+hw-obj-y += pcie.o pcie_port.o
 hw-obj-y += msix.o msi.o
 
 # PCI network cards
hw/pcie_port.c new file +106 -0

106 lines not on this page · raw message

hw/pcie_port.h new file +51 -0

51 lines not on this page · raw message

qemu-common.h +2 -0
index 6d9ee26..b97b16e 100644
@@ -221,6 +221,8 @@ typedef struct PCIBus PCIBus;
 typedef struct PCIDevice PCIDevice;
 typedef struct PCIExpressDevice PCIExpressDevice;
 typedef struct PCIBridge PCIBridge;
+typedef struct PCIEPort PCIEPort;
+typedef struct PCIESlot PCIESlot;
 typedef struct SerialState SerialState;
 typedef struct IRQState *qemu_irq;
 typedef struct PCMCIACardState PCMCIACardState;
Signature · 1 line
1.7.1.1
#18 lore raw

PATCH 10/14 pcie root port: implement pcie root port.

In reply to Isaku Yamahata

pcie root port.

---
 Makefile.objs  |    2 +-
 hw/pcie_root.c |  247 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pcie_root.h |   32 +++++++
 3 files changed, 280 insertions(+), 1 deletions(-)
 create mode 100644 hw/pcie_root.c
 create mode 100644 hw/pcie_root.h
Makefile.objs +1 -1
index c73d12b..658c399 100644
@@ -186,7 +186,7 @@ hw-obj-$(CONFIG_PIIX4) += piix4.o
 # PCI watchdog devices
 hw-obj-y += wdt_i6300esb.o
 
-hw-obj-y += pcie.o pcie_port.o
+hw-obj-y += pcie.o pcie_port.o pcie_root.o
 hw-obj-y += msix.o msi.o
 
 # PCI network cards
hw/pcie_root.c new file +247 -0

247 lines not on this page · raw message

hw/pcie_root.h new file +32 -0
new file mode 100644
index 0000000..9c5d4d0
@@ -0,0 +1,32 @@
+/*
+ * pcie_root.h
+ *
+ * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
+ *                    VA Linux Systems Japan K.K.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_PCIE_ROOT_H
+#define QEMU_PCIE_ROOT_H
+
+#include "pcie_port.h"
+
+#define PCIE_ROOT_PORT    "pcie-root-port"
+
+PCIESlot *pcie_root_init(PCIBus *bus, int devfn, bool multifunction,
+                         const char *bus_name, pci_map_irq_fn map_irq,
+                         uint8_t port, uint8_t chassis, uint16_t slot);
+
+#endif /* QEMU_PCIE_ROOT_H */
Signature · 1 line
1.7.1.1
#19 lore raw

PATCH 11/14 pcie upstream port: pci express switch upstream port.

In reply to Isaku Yamahata

pci express switch upstream port.

---
 Makefile.objs      |    2 +-
 hw/pcie_upstream.c |  206 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pcie_upstream.h |   32 ++++++++
 3 files changed, 239 insertions(+), 1 deletions(-)
 create mode 100644 hw/pcie_upstream.c
 create mode 100644 hw/pcie_upstream.h
Makefile.objs +1 -1
index 658c399..e7a08dc 100644
@@ -139,7 +139,7 @@ user-obj-y += cutils.o cache-utils.o
 hw-obj-y =
 hw-obj-y += vl.o loader.o
 hw-obj-y += virtio.o virtio-console.o
-hw-obj-y += fw_cfg.o pci.o pci_host.o pcie_host.o pci_bridge.o
+hw-obj-y += fw_cfg.o pci.o pci_host.o pcie_host.o pci_bridge.o pcie_upstream.o
 hw-obj-y += watchdog.o
 hw-obj-$(CONFIG_ISA_MMIO) += isa_mmio.o
 hw-obj-$(CONFIG_ECC) += ecc.o
hw/pcie_upstream.c new file +206 -0

206 lines not on this page · raw message

hw/pcie_upstream.h new file +32 -0
new file mode 100644
index 0000000..1d36317
@@ -0,0 +1,32 @@
+/*
+ * pcie_upstream.h
+ *
+ * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
+ *                    VA Linux Systems Japan K.K.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_PCIE_UPSTREAM_H
+#define QEMU_PCIE_UPSTREAM_H
+
+#include "pcie_port.h"
+
+#define PCIE_UPSTREAM_PORT      "pcie-upstream-port"
+
+PCIEPort *pcie_upstream_init(PCIBus *bus, int devfn, bool multifunction,
+                             const char *bus_name, pci_map_irq_fn map_irq,
+                             uint8_t port);
+
+#endif /* QEMU_PCIE_UPSTREAM_H */
Signature · 1 line
1.7.1.1
#20 lore raw

PATCH 12/14 pcie downstream port: pci express switch downstream port.

In reply to Isaku Yamahata

pcie switch downstream port.

---
 Makefile.objs        |    1 +
 hw/pcie_downstream.c |  224 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pcie_downstream.h |   33 ++++++++
 3 files changed, 258 insertions(+), 0 deletions(-)
 create mode 100644 hw/pcie_downstream.c
 create mode 100644 hw/pcie_downstream.h
Makefile.objs +1 -0
index e7a08dc..6904b02 100644
@@ -140,6 +140,7 @@ hw-obj-y =
 hw-obj-y += vl.o loader.o
 hw-obj-y += virtio.o virtio-console.o
 hw-obj-y += fw_cfg.o pci.o pci_host.o pcie_host.o pci_bridge.o pcie_upstream.o
+hw-obj-y += pcie_downstream.o
 hw-obj-y += watchdog.o
 hw-obj-$(CONFIG_ISA_MMIO) += isa_mmio.o
 hw-obj-$(CONFIG_ECC) += ecc.o
hw/pcie_downstream.c new file +224 -0

224 lines not on this page · raw message

hw/pcie_downstream.h new file +33 -0
new file mode 100644
index 0000000..686fdac
@@ -0,0 +1,33 @@
+/*
+ * pcie_downstream.h
+ *
+ * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
+ *                    VA Linux Systems Japan K.K.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_PCIE_DOWNSTREAM_H
+#define QEMU_PCIE_DOWNSTREAM_H
+
+#include "pcie_port.h"
+
+#define PCIE_DOWNSTREAM_PORT    "pcie-downstream-port"
+
+PCIESlot *pcie_downstream_init(PCIBus *bus,
+                               int devfn, bool multifunction,
+                               const char *bus_name, pci_map_irq_fn map_irq,
+                               uint8_t port, uint8_t chassis, uint16_t slot);
+
+#endif /* QEMU_PCIE_DOWNSTREAM_H */
Signature · 1 line
1.7.1.1
#21 lore raw

PATCH 13/14 pcie/hotplug: glue pushing attention button command. pcie_abp

In reply to Isaku Yamahata

glue to pcie_abp monitor command.

---
 hw/pcie_port.c  |   82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-monitor.hx |   14 +++++++++
 sysemu.h        |    4 +++
 3 files changed, 100 insertions(+), 0 deletions(-)
hw/pcie_port.c +82 -0
index d2b1f38..0aa8c53 100644
@@ -18,6 +18,10 @@
  * with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "qemu-objects.h"
+#include "sysemu.h"
+#include "monitor.h"
+#include "pcie.h"
 #include "pcie_port.h"
 
 void pcie_port_init_reg(PCIDevice *d)
@@ -104,3 +108,81 @@ void pcie_chassis_del_slot(PCIESlot *s)
 {
     QLIST_REMOVE(s, next);
 }
+
+/**************************************************************************
+ * glue for qemu monitor
+ */
+
+/* Parse [<chassis>.]<slot>, return -1 on error */
+static int pcie_parse_slot_addr(const char* slot_addr,
+                                uint8_t *chassisp, uint16_t *slotp)
+{
+    const char *p;
+    char *e;
+    unsigned long val;
+    unsigned long chassis = 0;
+    unsigned long slot;
+
+    p = slot_addr;
+    val = strtoul(p, &e, 0);
+    if (e == p) {
+        return -1;
+    }
+    if (*e == '.') {
+        chassis = val;
+        p = e + 1;
+        val = strtoul(p, &e, 0);
+        if (e == p) {
+            return -1;
+        }
+    }
+    slot = val;
+
+    if (*e) {
+        return -1;
+    }
+
+    if (chassis > 0xff || slot > 0xffff) {
+        return -1;
+    }
+
+    *chassisp = chassis;
+    *slotp = slot;
+    return 0;
+}
+
+void pcie_attention_button_push_print(Monitor *mon, const QObject *data)
+{
+    QDict *qdict;
+
+    assert(qobject_type(data) == QTYPE_QDICT);
+    qdict = qobject_to_qdict(data);
+
+    monitor_printf(mon, "OK chassis %d, slot %d\n",
+                   (int) qdict_get_int(qdict, "chassis"),
+                   (int) qdict_get_int(qdict, "slot"));
+}
+
+int pcie_attention_button_push(Monitor *mon, const QDict *qdict,
+                               QObject **ret_data)
+{
+    const char* pcie_slot = qdict_get_str(qdict, "pcie_slot");
+    uint8_t chassis;
+    uint16_t slot;
+    PCIESlot *s;
+
+    if (pcie_parse_slot_addr(pcie_slot, &chassis, &slot) < 0) {
+        monitor_printf(mon, "invalid pcie slot address %s\n", pcie_slot);
+        return -1;
+    }
+    s = pcie_chassis_find_slot(chassis, slot);
+    if (!s) {
+        monitor_printf(mon, "slot is not found. %s\n", pcie_slot);
+        return -1;
+    }
+    pcie_cap_slot_push_attention_button(&s->port.br.dev);
+    *ret_data = qobject_from_jsonf("{ 'chassis': %d, 'slot': %d}",
+                                   chassis, slot);
+    assert(*ret_data);
+    return 0;
+}
qemu-monitor.hx +14 -0
index 2af3de6..02fbda1 100644
@@ -1154,6 +1154,20 @@ Hot remove PCI device.
 ETEXI
 
     {
+        .name       = "pcie_abp",
+        .args_type  = "pcie_slot:s",
+        .params     = "[<chassis>.]<slot>",
+        .help       = "push pci express attention button",
+        .user_print  = pcie_attention_button_push_print,
+        .mhandler.cmd_new = pcie_attention_button_push,
+    },
+
+STEXI
+@item pcie_abp
+Push PCI express attention button
+ETEXI
+
+    {
         .name       = "host_net_add",
         .args_type  = "device:s,opts:s?",
         .params     = "tap|user|socket|vde|dump [options]",
sysemu.h +4 -0
index 9c988bb..cca411d 100644
@@ -150,6 +150,10 @@ extern unsigned int nb_prom_envs;
 void pci_device_hot_add(Monitor *mon, const QDict *qdict);
 void drive_hot_add(Monitor *mon, const QDict *qdict);
 void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict);
+/* pcie hotplug */
+void pcie_attention_button_push_print(Monitor *mon, const QObject *data);
+int pcie_attention_button_push(Monitor *mon, const QDict *qdict,
+                               QObject **ret_data);
 
 /* serial ports */
 
Signature · 1 line
1.7.1.1
#22 lore raw

PATCH 14/14 pcie/aer: glue aer error injection into qemu monitor.

In reply to Isaku Yamahata

glue aer error injection into qemu monitor.

Conflicts:

hw/pcie.c

---
 hw/pcie.c       |   85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-monitor.hx |   22 ++++++++++++++
 sysemu.h        |    5 +++
 3 files changed, 112 insertions(+), 0 deletions(-)
hw/pcie.c +85 -0
index 1f24c2a..f02a868 100644
@@ -19,6 +19,8 @@
  */
 
 #include "sysemu.h"
+#include "qemu-objects.h"
+#include "monitor.h"
 #include "pci_bridge.h"
 #include "pcie.h"
 #include "msix.h"
@@ -1666,3 +1668,86 @@ const VMStateDescription vmstate_pcie_aer_log = {
         VMSTATE_END_OF_LIST()
     }
 };
+
+void pcie_aer_inject_error_print(Monitor *mon, const QObject *data)
+{
+    QDict *qdict;
+    int devfn;
+    assert(qobject_type(data) == QTYPE_QDICT);
+    qdict = qobject_to_qdict(data);
+
+    devfn = (int)qdict_get_int(qdict, "devfn");
+    monitor_printf(mon, "OK domain: %x, bus: %x devfn: %x.%x\n",
+                   (int) qdict_get_int(qdict, "domain"),
+                   (int) qdict_get_int(qdict, "bus"),
+                   PCI_SLOT(devfn), PCI_FUNC(devfn));
+}
+
+int do_pcie_aer_inejct_error(Monitor *mon,
+                             const QDict *qdict, QObject **ret_data)
+{
+    const char *pci_addr = qdict_get_str(qdict, "pci_addr");
+    int dom;
+    int bus;
+    unsigned int slot;
+    unsigned int func;
+    PCIDevice *dev;
+    struct pcie_aer_err err;
+
+    /* Ideally qdev device path should be used.
+     * However at the moment there is no reliable way to determine
+     * wheher a given qdev is pci device or not.
+     * so pci_addr is used.
+     */
+    if (pci_parse_devaddr(pci_addr, &dom, &bus, &slot, &func)) {
+        monitor_printf(mon, "invalid pci address %s\n", pci_addr);
+        return -1;
+    }
+    dev = pci_find_device(pci_find_root_bus(dom), bus, slot, func);
+    if (!dev) {
+        monitor_printf(mon, "device is not found. 0x%x:0x%x.0x%x\n",
+                       bus, slot, func);
+        return -1;
+    }
+    if (!pci_is_express(dev)) {
+        monitor_printf(mon, "the device doesn't support pci express. "
+                       "0x%x:0x%x.0x%x\n",
+                       bus, slot, func);
+        return -1;
+    }
+
+    err.status = qdict_get_int(qdict, "error_status");
+    err.source_id = (pci_bus_num(dev->bus) << 8) | dev->devfn;
+
+    err.flags = 0;
+    if (qdict_get_int(qdict, "is_correctable")) {
+        err.flags |= PCIE_AER_ERR_IS_CORRECTABLE;
+    }
+    if (qdict_get_int(qdict, "advisory_non_fatal")) {
+        err.flags |= PCIE_AER_ERR_MAYBE_ADVISORY;
+    }
+    if (qdict_haskey(qdict, "tlph0")) {
+        err.flags |= PCIE_AER_ERR_HEADER_VALID;
+    }
+    if (qdict_haskey(qdict, "hpfx0")) {
+        err.flags |= PCIE_AER_ERR_TLP_PRESENT;
+    }
+
+    err.header[0] = qdict_get_try_int(qdict, "tlph0", 0);
+    err.header[1] = qdict_get_try_int(qdict, "tlph1", 0);
+    err.header[2] = qdict_get_try_int(qdict, "tlph2", 0);
+    err.header[3] = qdict_get_try_int(qdict, "tlph3", 0);
+
+    err.prefix[0] = qdict_get_try_int(qdict, "hpfx0", 0);
+    err.prefix[1] = qdict_get_try_int(qdict, "hpfx1", 0);
+    err.prefix[2] = qdict_get_try_int(qdict, "hpfx2", 0);
+    err.prefix[3] = qdict_get_try_int(qdict, "hpfx3", 0);
+
+    pcie_aer_inject_error(dev, &err);
+    *ret_data = qobject_from_jsonf("{ 'domain': %d, 'bus': %d, 'devfn': %d }",
+                                   pci_find_domain(dev->bus),
+                                   pci_bus_num(dev->bus), dev->devfn);
+    assert(*ret_data);
+
+    return 0;
+}
qemu-monitor.hx +22 -0
index 02fbda1..080c90e 100644
@@ -1168,6 +1168,28 @@ Push PCI express attention button
 ETEXI
 
     {
+        .name       = "pcie_aer_inject_error",
+        .args_type  = "advisory_non_fatal:-a,is_correctable:-c,"
+	              "pci_addr:s,error_status:i,"
+	              "tlph0:i?,tlph1:i?,tlph2:i?,tlph3:i?,"
+	              "hpfx0:i?,hpfx1:i?,hpfx2:i?,hpfx3:i?",
+        .params     = "[-a] [-c] [[<domain>:]<bus>:]<slot>.<func> "
+	              "<error status:32bit> "
+	              "[<tlp header:(32bit x 4)>] "
+	              "[<tlp header prefix:(32bit x 4)>]",
+        .help       = "inject pcie aer error "
+	               "(use -a for advisory non fatal error) "
+	               "(use -c for correctrable error)",
+        .user_print  = pcie_aer_inject_error_print,
+        .mhandler.cmd_new = do_pcie_aer_inejct_error,
+    },
+
+STEXI
+@item pcie_abp
+Push PCI express attention button
+ETEXI
+
+    {
         .name       = "host_net_add",
         .args_type  = "device:s,opts:s?",
         .params     = "tap|user|socket|vde|dump [options]",
sysemu.h +5 -0
index cca411d..2f7157c 100644
@@ -155,6 +155,11 @@ void pcie_attention_button_push_print(Monitor *mon, const QObject *data);
 int pcie_attention_button_push(Monitor *mon, const QDict *qdict,
                                QObject **ret_data);
 
+/* pcie aer error injection */
+void pcie_aer_inject_error_print(Monitor *mon, const QObject *data);
+int do_pcie_aer_inejct_error(Monitor *mon,
+                             const QDict *qdict, QObject **ret_data);
+
 /* serial ports */
 
 #define MAX_SERIAL_PORTS 4
Signature · 1 line
1.7.1.1
#23 lore raw
In reply to Isaku Yamahata

Isaku,

Thanks! Last week I did some merging to qemu-kvm and found similar issues (qdev reset) and also ioapic/apic needs many changes. I may ask your help this week...

Wei

Quoted 73 lines · Isaku Yamahata

This patch series implements pcie port switch emulators which is basic part for pcie/q35 support.
This is for mst/pci tree.

some random comments
- pci bus reset
As Anthony is cleaning up qdev reset stuff, so pci bus reset code is commented out. Once the qdev clean up is done, the patch that enabled pci bus reset will be sent.

- vid/did
there are arbitrariness for pcie port switch. I just choose Intel and IT one to model them.

thanks,

Isaku Yamahata (14):
RESEND apb: fix typo.
pci: consolidate pci_add_capability_at_offset() into
pci_add_capability().
pci bridge: add helper function for ssvid capability.
pci: call hotplug callback even when not hotplug case for later use.
pci: make pci_parse_devfn() aware of func.
pci_ids.h: add vendor id of Texus Intesruments.
msi: implemented msi.
pcie: helper functions for pcie extended capability.
pcie port: define struct PCIEPort/PCIESlot and helper functions pcie root port: implement pcie root port.
pcie upstream port: pci express switch upstream port. pcie downstream port: pci express switch downstream port. pcie/hotplug: glue pushing attention button command. pcie_abp pcie/aer: glue aer error injection into qemu monitor.

 Makefile.objs        |    6 +-
 hw/acpi_piix4.c      |    3 +
 hw/apb_pci.c         |    6 +-
 hw/eepro100.c        |    4 +-
 hw/msi.c             |  362 +++++++++++
 hw/msi.h             |   41 ++
 hw/msix.c            |    3 +-
 hw/pci.c             |   70 ++-
 hw/pci.h             |   36 +-
 hw/pci_bridge.c      |   19 +
 hw/pci_bridge.h      |    3 +
 hw/pci_ids.h         |    2 +
 hw/pcie.c            | 1753
++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pcie.h            |  186 ++++++
 hw/pcie_downstream.c |  224 +++++++
 hw/pcie_downstream.h |   33 +
 hw/pcie_port.c       |  188 ++++++
 hw/pcie_port.h       |   51 ++
 hw/pcie_root.c       |  247 +++++++
 hw/pcie_root.h       |   32 +
 hw/pcie_upstream.c   |  206 ++++++
 hw/pcie_upstream.h   |   32 +
 qemu-common.h        |    3 +
 qemu-monitor.hx      |   36 +
 sysemu.h             |    9 +
 25 files changed, 3520 insertions(+), 35 deletions(-)
 create mode 100644 hw/msi.c
 create mode 100644 hw/msi.h
 create mode 100644 hw/pcie.c
 create mode 100644 hw/pcie.h
 create mode 100644 hw/pcie_downstream.c
 create mode 100644 hw/pcie_downstream.h
 create mode 100644 hw/pcie_port.c
 create mode 100644 hw/pcie_port.h
 create mode 100644 hw/pcie_root.c
 create mode 100644 hw/pcie_root.h
 create mode 100644 hw/pcie_upstream.c
 create mode 100644 hw/pcie_upstream.h
#24 lore raw
In reply to Isaku Yamahata
Quoted 16 lines · Isaku Yamahata

This patch series implements pcie port switch emulators which is basic part for pcie/q35 support.
This is for mst/pci tree.

some random comments
- pci bus reset
As Anthony is cleaning up qdev reset stuff, so pci bus reset code is commented out. Once the qdev clean up is done, the patch that enabled pci bus reset will be sent.

- vid/did
there are arbitrariness for pcie port switch. I just choose Intel and IT one to model them.

thanks,

Quoted 8 lines

Isaku Yamahata (14):
RESEND apb: fix typo.
pci: consolidate pci_add_capability_at_offset() into
pci_add_capability().
pci bridge: add helper function for ssvid capability.
pci: call hotplug callback even when not hotplug case for later use.
pci: make pci_parse_devfn() aware of func.
pci_ids.h: add vendor id of Texus Intesruments.

OK, I pushed the above to the pci branch, and folded
the first patch into the previous one.
Didn't have time to test it yet, but I thought I'd push it out already to reduce mailing-list churn.

Quoted 1 line

msi: implemented msi.

I went over this and posted some comments.

Quoted 7 lines

pcie: helper functions for pcie extended capability. pcie port: define struct PCIEPort/PCIESlot and helper functions pcie root port: implement pcie root port.
pcie upstream port: pci express switch upstream port. pcie downstream port: pci express switch downstream port. pcie/hotplug: glue pushing attention button command. pcie_abp pcie/aer: glue aer error injection into qemu monitor.

Haven't reviewed the above yet. Are there bits that don't depend on msi?

Quoted 38 lines
 Makefile.objs        |    6 +-
 hw/acpi_piix4.c      |    3 +
 hw/apb_pci.c         |    6 +-
 hw/eepro100.c        |    4 +-
 hw/msi.c             |  362 +++++++++++
 hw/msi.h             |   41 ++
 hw/msix.c            |    3 +-
 hw/pci.c             |   70 ++-
 hw/pci.h             |   36 +-
 hw/pci_bridge.c      |   19 +
 hw/pci_bridge.h      |    3 +
 hw/pci_ids.h         |    2 +
 hw/pcie.c            | 1753 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pcie.h            |  186 ++++++
 hw/pcie_downstream.c |  224 +++++++
 hw/pcie_downstream.h |   33 +
 hw/pcie_port.c       |  188 ++++++
 hw/pcie_port.h       |   51 ++
 hw/pcie_root.c       |  247 +++++++
 hw/pcie_root.h       |   32 +
 hw/pcie_upstream.c   |  206 ++++++
 hw/pcie_upstream.h   |   32 +
 qemu-common.h        |    3 +
 qemu-monitor.hx      |   36 +
 sysemu.h             |    9 +
 25 files changed, 3520 insertions(+), 35 deletions(-)
 create mode 100644 hw/msi.c
 create mode 100644 hw/msi.h
 create mode 100644 hw/pcie.c
 create mode 100644 hw/pcie.h
 create mode 100644 hw/pcie_downstream.c
 create mode 100644 hw/pcie_downstream.h
 create mode 100644 hw/pcie_port.c
 create mode 100644 hw/pcie_port.h
 create mode 100644 hw/pcie_root.c
 create mode 100644 hw/pcie_root.h
 create mode 100644 hw/pcie_upstream.c
 create mode 100644 hw/pcie_upstream.h