Some small cleanups and corrections to PCI-E handling
cover Some small cleanups and corrections to PCI-E handling
Here are a couple of cleanups for PCI-E handling which arose out of some other work I have in progress. At least the first of these patches has been posted before as part of a different series, but wasn't ready to go at the time.
David Gibson (2):
pci: Simplify pci_bus_is_root()
pcie: Don't allow extended config space access via conventional PCI
bridges
hw/pci-bridge/pci_expander_bridge.c | 6 ----- hw/pci/pci.c | 40 ++++++++++++++++++++++------- hw/virtio/virtio-pci.c | 1 + include/hw/pci/pci.h | 5 ++-- include/hw/pci/pci_bus.h | 13 ++++++++++ 5 files changed, 48 insertions(+), 17 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/2 pci: Simplify pci_bus_is_root()
pci_bus_is_root() currently relies on a method in the PCIBusClass. But it's always known if a PCI bus is a root bus when we create it, so using a dynamic method is overkill.
This replaces it with an IS_ROOT bit in a new flags field, which is set on root buses and otherwise clear. As a bonus this removes the special is_root logic from pci_expander_bridge, since it already creates its bus as a root bus.
--- hw/pci-bridge/pci_expander_bridge.c | 6 ------ hw/pci/pci.c | 14 ++------------ hw/virtio/virtio-pci.c | 1 + include/hw/pci/pci.h | 1 - include/hw/pci/pci_bus.h | 11 +++++++++++ 5 files changed, 14 insertions(+), 19 deletions(-)
hw/pci-bridge/pci_expander_bridge.c +0 -6
@@ -66,11 +66,6 @@ static int pxb_bus_num(PCIBus *bus)return pxb->bus_nr; } -static bool pxb_is_root (PCIBus *bus) -{ -return true;/* by definition */ -} -static uint16_t pxb_bus_numa_node (PCIBus *bus) {PXBDev *pxb =convert_to_pxb (bus->parent_dev);
@@ -83,7 +78,6 @@ static void pxb_bus_class_init(ObjectClass *class, void *data)PCIBusClass *pbc =PCI_BUS_CLASS (class); pbc->bus_num = pxb_bus_num; - pbc->is_root = pxb_is_root; pbc->numa_node = pxb_bus_numa_node; }
hw/pci/pci.c +2 -12
@@ -129,14 +129,9 @@ static void pci_bus_unrealize(BusState *qbus, Error **errp)vmstate_unregister (NULL , &vmstate_pcibus, bus); } -static bool pcibus_is_root (PCIBus *bus) -{ -return !bus->parent_dev; -} -static int pcibus_num (PCIBus *bus) { -if (pcibus_is_root (bus)) { +if (pci_bus_is_root (bus)) {return 0 ;/* pci host bridge */ }return bus->parent_dev->config[PCI_SECONDARY_BUS ];
@@ -159,7 +154,6 @@ static void pci_bus_class_init(ObjectClass *klass, void *data) k->unrealize = pci_bus_unrealize; k->reset = pcibus_reset; - pbc->is_root = pcibus_is_root; pbc->bus_num = pcibus_num; pbc->numa_node = pcibus_numa_node; }
@@ -379,6 +373,7 @@ static void pci_root_bus_init(PCIBus *bus, DeviceState *parent, bus->slot_reserved_mask =0x0 ; bus->address_space_mem = address_space_mem; bus->address_space_io = address_space_io; + bus->flags |=PCI_BUS_IS_ROOT ;/* host bridge */ QLIST_INIT (&bus->child);
@@ -396,11 +391,6 @@ bool pci_bus_is_express(PCIBus *bus)return object_dynamic_cast (OBJECT (bus),TYPE_PCIE_BUS ); } -bool pci_bus_is_root (PCIBus *bus) -{ -return PCI_BUS_GET_CLASS (bus)->is_root (bus); -} - void pci_root_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent, const char *name, MemoryRegion *address_space_mem,
hw/virtio/virtio-pci.c +1 -0
@@ -20,6 +20,7 @@#include "standard-headers/linux/virtio_pci.h" #include "hw/virtio/virtio.h" #include "hw/pci/pci.h" +#include "hw/pci/pci_bus.h" #include "qapi/error.h" #include "qemu/error-report.h" #include "hw/pci/msi.h"
include/hw/pci/pci.h +0 -1
@@ -395,7 +395,6 @@ typedef PCIINTxRoute (*pci_route_irq_fn)(void *opaque, int pin);#define TYPE_PCIE_BUS "PCIE"bool pci_bus_is_express (PCIBus *bus); -bool pci_bus_is_root (PCIBus *bus); void pci_root_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent, const char *name, MemoryRegion *address_space_mem,
include/hw/pci/pci_bus.h +11 -0
@@ -20,8 +20,14 @@ typedef struct PCIBusClass {uint16_t (*numa_node)(PCIBus *bus); } PCIBusClass; +enum PCIBusFlags { +/* This bus is the root of a PCI domain */ +PCI_BUS_IS_ROOT =0x0001 , +}; +struct PCIBus {BusState qbus; +enum PCIBusFlags flags;PCIIOMMUFunc iommu_fn;void *iommu_opaque;uint8_t devfn_min;
@@ -46,4 +52,9 @@ struct PCIBus { Notifier machine_done; }; +static inline bool pci_bus_is_root (PCIBus *bus) +{ +return !!(bus->flags &PCI_BUS_IS_ROOT ); +} +#endif /* QEMU_PCI_BUS_H */
Signature · 1 line
2.20.1
PATCH 2/2 pcie: Don't allow extended config space access via conventional PCI bridges
In reply to David GibsonIn hardware it's possible, if odd, to have a configuration like:
PCIe host bridge
\- PCIe to PCI bridge
\- PCI to PCIe bridge
\- PCIe device
The PCIe extended configuration space on the device won't be accessible to the host, because the cycles can't traverse the conventional PCI bus on the way there.
However, if we attempt to model that configuration under qemu, extended config access on the device *will* work, because pci_config_size() depends only on whether the device itself is PCIe capable.
This patch fixes that modelling error by adding a flag to each PCI/PCIe bus instance indicating whether extended config space accesses are possible on it. It will always be false for conventional PCI buses, for PCIe buses it will be true if and only if the parent bus also has the flag set.
AIUI earlier attempts to correct this have been rejected, because they involved expensively traversing the whole bus hierarchy on each config access. This approach avoids that by computing the value as the bus hierarchy is constructed, meaning we only need a single bit check when we actually attempt the config access.
--- hw/pci/pci.c | 32 ++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 4 +++- include/hw/pci/pci_bus.h | 2 ++ 3 files changed, 37 insertions(+), 1 deletion(-)
hw/pci/pci.c +32 -0
@@ -120,6 +120,25 @@ static void pci_bus_realize(BusState *qbus, Error **errp)vmstate_register (NULL ,-1 , &vmstate_pcibus, bus); } +static void pcie_bus_realize (BusState *qbus,Error **errp) +{ +PCIBus *bus =PCI_BUS (qbus); + +pci_bus_realize (qbus, errp); + +/* a PCI-E bus can supported extended config space if it's the +* root bus, or if the bus/bridge above it does as well */ +if (pci_bus_is_root (bus)) { + bus->flags |=PCI_BUS_EXTENDED_CONFIG_SPACE ; + }else { +PCIBus *parent_bus =pci_get_bus (bus->parent_dev); + +if (pci_bus_extended_config_space (parent_bus)) { + bus->flags |=PCI_BUS_EXTENDED_CONFIG_SPACE ; + } + } +} +static void pci_bus_unrealize (BusState *qbus,Error **errp) {PCIBus *bus =PCI_BUS (qbus);
@@ -166,6 +185,13 @@ static const TypeInfo pci_bus_info = { .class_init = pci_bus_class_init, }; +static void pcie_bus_class_init (ObjectClass *klass,void *data) +{ +BusClass *k =BUS_CLASS (klass); + + k->realize = pcie_bus_realize; +} +static const TypeInfo pcie_interface_info = { .name =INTERFACE_PCIE_DEVICE , .parent =TYPE_INTERFACE ,
@@ -174,6 +200,7 @@ static const TypeInfo pcie_interface_info = {static const TypeInfo conventional_pci_interface_info = { .name =INTERFACE_CONVENTIONAL_PCI_DEVICE , .parent =TYPE_INTERFACE , + .class_init = pcie_bus_class_init, };static const TypeInfo pcie_bus_info = {
@@ -391,6 +418,11 @@ bool pci_bus_is_express(PCIBus *bus)return object_dynamic_cast (OBJECT (bus),TYPE_PCIE_BUS ); } +bool pci_bus_extended_config_space (PCIBus *bus) +{ +return !!(bus->flags &PCI_BUS_EXTENDED_CONFIG_SPACE ); +} + void pci_root_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent, const char *name, MemoryRegion *address_space_mem,
include/hw/pci/pci.h +3 -1
@@ -395,6 +395,7 @@ typedef PCIINTxRoute (*pci_route_irq_fn)(void *opaque, int pin);#define TYPE_PCIE_BUS "PCIE"bool pci_bus_is_express (PCIBus *bus); +bool pci_bus_extended_config_space (PCIBus *bus); void pci_root_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent, const char *name, MemoryRegion *address_space_mem,
@@ -754,7 +755,8 @@ static inline int pci_is_express_downstream_port(const PCIDevice *d)static inline uint32_t pci_config_size (const PCIDevice *d) { -return pci_is_express (d) ?PCIE_CONFIG_SPACE_SIZE :PCI_CONFIG_SPACE_SIZE ; +return (pci_is_express (d) &&pci_bus_extended_config_space (pci_get_bus (d))) + ?PCIE_CONFIG_SPACE_SIZE :PCI_CONFIG_SPACE_SIZE ; }static inline uint16_t pci_get_bdf (PCIDevice *dev)
include/hw/pci/pci_bus.h +2 -0
@@ -23,6 +23,8 @@ typedef struct PCIBusClass {enum PCIBusFlags {/* This bus is the root of a PCI domain */ PCI_BUS_IS_ROOT =0x0001 , +/* PCIe extended configuration space is accessible on this bus */ +PCI_BUS_EXTENDED_CONFIG_SPACE =0x0002 , };struct PCIBus {
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH 2/2] pcie: Don't allow extended config space access via conventional PCI bridges
Quoted 112 lines · David Gibson
In hardware it's possible, if odd, to have a configuration like:
PCIe host bridge
\- PCIe to PCI bridge
\- PCI to PCIe bridge
\- PCIe deviceThe PCIe extended configuration space on the device won't be accessible to the host, because the cycles can't traverse the conventional PCI bus on the way there.
However, if we attempt to model that configuration under qemu, extended config access on the device *will* work, because
pci_config_size()depends only on whether the device itself is PCIe capable.This patch fixes that modelling error by adding a flag to each PCI/PCIe bus instance indicating whether extended config space accesses are possible on it. It will always be false for conventional PCI buses, for PCIe buses it will be true if and only if the parent bus also has the flag set.
AIUI earlier attempts to correct this have been rejected, because they involved expensively traversing the whole bus hierarchy on each config access. This approach avoids that by computing the value as the bus hierarchy is constructed, meaning we only need a single bit check when we actually attempt the config access.
Signed-off-by David Gibson <[email protected]>--- hw/pci/pci.c | 32 ++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 4 +++- include/hw/pci/pci_bus.h | 2 ++ 3 files changed, 37 insertions(+), 1 deletion(-)hw/pci/pci.c +32
-0@@ -120,6 +120,25 @@ static void pci_bus_realize(BusState *qbus, Error **errp)vmstate_register (NULL ,-1 , &vmstate_pcibus, bus); } +static void pcie_bus_realize (BusState *qbus,Error **errp) +{ +PCIBus *bus =PCI_BUS (qbus); + +pci_bus_realize (qbus, errp); + +/* a PCI-E bus can supported extended config space if it's the +* root bus, or if the bus/bridge above it does as well */ +if (pci_bus_is_root (bus)) { + bus->flags |=PCI_BUS_EXTENDED_CONFIG_SPACE ; + }else { +PCIBus *parent_bus =pci_get_bus (bus->parent_dev); + +if (pci_bus_extended_config_space (parent_bus)) { + bus->flags |=PCI_BUS_EXTENDED_CONFIG_SPACE ; + } + } +} +static void pci_bus_unrealize (BusState *qbus,Error **errp) {PCIBus *bus =PCI_BUS (qbus);@@ -166,6 +185,13 @@ static const TypeInfo pci_bus_info = { .class_init = pci_bus_class_init, }; +static void pcie_bus_class_init (ObjectClass *klass,void *data) +{ +BusClass *k =BUS_CLASS (klass); + + k->realize = pcie_bus_realize; +} +static const TypeInfo pcie_interface_info = { .name =INTERFACE_PCIE_DEVICE , .parent =TYPE_INTERFACE ,@@ -174,6 +200,7 @@ static const TypeInfo pcie_interface_info = {static const TypeInfo conventional_pci_interface_info = { .name =INTERFACE_CONVENTIONAL_PCI_DEVICE , .parent =TYPE_INTERFACE , + .class_init = pcie_bus_class_init, };static const TypeInfo pcie_bus_info = {@@ -391,6 +418,11 @@ bool pci_bus_is_express(PCIBus *bus)return object_dynamic_cast (OBJECT (bus),TYPE_PCIE_BUS ); } +bool pci_bus_extended_config_space (PCIBus *bus) +{ +return !!(bus->flags &PCI_BUS_EXTENDED_CONFIG_SPACE ); +} + void pci_root_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent, const char *name, MemoryRegion *address_space_mem,include/hw/pci/pci.h +1
-0@@ -395,6 +395,7 @@ typedef PCIINTxRoute (*pci_route_irq_fn)(void *opaque, int pin);#define TYPE_PCIE_BUS "PCIE"bool pci_bus_is_express (PCIBus *bus); +bool pci_bus_extended_config_space (PCIBus *bus); void pci_root_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent, const char *name, MemoryRegion *address_space_mem,
include/hw/pci/pci.h static inline int pci_is_express_downstream_port(const PCIDevice *d) · line 759
Quoted 6 lines
include/hw/pci/pci.h +2
-1@@ -754,7 +755,8 @@ static inline int pci_is_express_downstream_port(const PCIDevice *d)static inline uint32_t pci_config_size (const PCIDevice *d) { -return pci_is_express (d) ?PCIE_CONFIG_SPACE_SIZE :PCI_CONFIG_SPACE_SIZE ; +return (pci_is_express (d) &&pci_bus_extended_config_space (pci_get_bus (d))) + ?PCIE_CONFIG_SPACE_SIZE :PCI_CONFIG_SPACE_SIZE ;
Since there is a selfnack anyway, I'll ask out of curiosity - can a device sit on PCIe bus and not be PCIe itself?
The pci_is_express(d) check above just seems a little redundant, g_assert() could probably do just that.
Quoted 17 lines
include/hw/pci/pci.h +0
-0}static inline uint16_t pci_get_bdf (PCIDevice *dev)include/hw/pci/pci_bus.h +2
-0@@ -23,6 +23,8 @@ typedef struct PCIBusClass {enum PCIBusFlags {/* This bus is the root of a PCI domain */ PCI_BUS_IS_ROOT =0x0001 , +/* PCIe extended configuration space is accessible on this bus */ +PCI_BUS_EXTENDED_CONFIG_SPACE =0x0002 , };struct PCIBus {
Signature · 1 line
Alexey
Re: [Qemu-devel] [PATCH 2/2] pcie: Don't allow extended config space access via conventional PCI bridges
Quoted 116 lines · Alexey Kardashevskiy
Quoted 113 lines · David Gibson
In hardware it's possible, if odd, to have a configuration like:
PCIe host bridge
\- PCIe to PCI bridge
\- PCI to PCIe bridge
\- PCIe deviceThe PCIe extended configuration space on the device won't be accessible to the host, because the cycles can't traverse the conventional PCI bus on the way there.
However, if we attempt to model that configuration under qemu, extended config access on the device *will* work, because
pci_config_size()depends only on whether the device itself is PCIe capable.This patch fixes that modelling error by adding a flag to each PCI/PCIe bus instance indicating whether extended config space accesses are possible on it. It will always be false for conventional PCI buses, for PCIe buses it will be true if and only if the parent bus also has the flag set.
AIUI earlier attempts to correct this have been rejected, because they involved expensively traversing the whole bus hierarchy on each config access. This approach avoids that by computing the value as the bus hierarchy is constructed, meaning we only need a single bit check when we actually attempt the config access.
Signed-off-by David Gibson <[email protected]>--- hw/pci/pci.c | 32 ++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 4 +++- include/hw/pci/pci_bus.h | 2 ++ 3 files changed, 37 insertions(+), 1 deletion(-)hw/pci/pci.c +32
-0@@ -120,6 +120,25 @@ static void pci_bus_realize(BusState *qbus, Error **errp)vmstate_register (NULL ,-1 , &vmstate_pcibus, bus); } +static void pcie_bus_realize (BusState *qbus,Error **errp) +{ +PCIBus *bus =PCI_BUS (qbus); + +pci_bus_realize (qbus, errp); + +/* a PCI-E bus can supported extended config space if it's the +* root bus, or if the bus/bridge above it does as well */ +if (pci_bus_is_root (bus)) { + bus->flags |=PCI_BUS_EXTENDED_CONFIG_SPACE ; + }else { +PCIBus *parent_bus =pci_get_bus (bus->parent_dev); + +if (pci_bus_extended_config_space (parent_bus)) { + bus->flags |=PCI_BUS_EXTENDED_CONFIG_SPACE ; + } + } +} +static void pci_bus_unrealize (BusState *qbus,Error **errp) {PCIBus *bus =PCI_BUS (qbus);@@ -166,6 +185,13 @@ static const TypeInfo pci_bus_info = { .class_init = pci_bus_class_init, }; +static void pcie_bus_class_init (ObjectClass *klass,void *data) +{ +BusClass *k =BUS_CLASS (klass); + + k->realize = pcie_bus_realize; +} +static const TypeInfo pcie_interface_info = { .name =INTERFACE_PCIE_DEVICE , .parent =TYPE_INTERFACE ,@@ -174,6 +200,7 @@ static const TypeInfo pcie_interface_info = {static const TypeInfo conventional_pci_interface_info = { .name =INTERFACE_CONVENTIONAL_PCI_DEVICE , .parent =TYPE_INTERFACE , + .class_init = pcie_bus_class_init, };static const TypeInfo pcie_bus_info = {@@ -391,6 +418,11 @@ bool pci_bus_is_express(PCIBus *bus)return object_dynamic_cast (OBJECT (bus),TYPE_PCIE_BUS ); } +bool pci_bus_extended_config_space (PCIBus *bus) +{ +return !!(bus->flags &PCI_BUS_EXTENDED_CONFIG_SPACE ); +} + void pci_root_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent, const char *name, MemoryRegion *address_space_mem,include/hw/pci/pci.h +1
-0@@ -395,6 +395,7 @@ typedef PCIINTxRoute (*pci_route_irq_fn)(void *opaque, int pin);#define TYPE_PCIE_BUS "PCIE"bool pci_bus_is_express (PCIBus *bus); +bool pci_bus_extended_config_space (PCIBus *bus); void pci_root_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent, const char *name, MemoryRegion *address_space_mem,@@ -754,7 +755,8 @@ static inline int pci_is_express_downstream_port(const PCIDevice *d)
Quoted 9 lines
include/hw/pci/pci.h static inline int pci_is_express_downstream_port(const PCIDevice *d) · line 759
Quoted 5 lines
include/hw/pci/pci.h +2
-1static inline uint32_t pci_config_size (const PCIDevice *d) { -return pci_is_express (d) ?PCIE_CONFIG_SPACE_SIZE :PCI_CONFIG_SPACE_SIZE ; +return (pci_is_express (d) &&pci_bus_extended_config_space (pci_get_bus (d))) + ?PCIE_CONFIG_SPACE_SIZE :PCI_CONFIG_SPACE_SIZE ;Since there is a selfnack anyway, I'll ask out of curiosity - can a device sit on PCIe bus and not be PCIe itself?
I believe so. I think the most common case is plain-PCI integrated devices in an otherwise PCI-E host bridge / root complex.
Quoted 24 lines
The
pci_is_express(d)check above just seems a little redundant,g_assert()could probably do just that.Quoted 17 lines
include/hw/pci/pci.h +0
-0}static inline uint16_t pci_get_bdf (PCIDevice *dev)include/hw/pci/pci_bus.h +2
-0@@ -23,6 +23,8 @@ typedef struct PCIBusClass {enum PCIBusFlags {/* This bus is the root of a PCI domain */ PCI_BUS_IS_ROOT =0x0001 , +/* PCIe extended configuration space is accessible on this bus */ +PCI_BUS_EXTENDED_CONFIG_SPACE =0x0002 , };struct PCIBus {
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 4 lines · David Gibson
Here are a couple of cleanups for PCI-E handling which arose out of some other work I have in progress. At least the first of these patches has been posted before as part of a different series, but wasn't ready to go at the time.
Self NACK.
Sorry, made a sloppy error in my testing and this breaks make check. I'll repost once I've fixed it.
Quoted 13 lines
David Gibson (2):
pci: Simplifypci_bus_is_root()
pcie: Don't allow extended config space access via conventional PCI
bridgeshw/pci-bridge/pci_expander_bridge.c | 6 ----- hw/pci/pci.c | 40 ++++++++++++++++++++++------- hw/virtio/virtio-pci.c | 1 + include/hw/pci/pci.h | 5 ++-- include/hw/pci/pci_bus.h | 13 ++++++++++ 5 files changed, 48 insertions(+), 17 deletions(-)
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson