pcie: Add support for Single Root I/O Virtualization
v4 cover pcie: Add support for Single Root I/O Virtualization
This patch set implements generic support for SR/IOV as an extension to the core PCIe functionality, similar to the way other capabilities such as AER is implemented.
There is no implementation of any device that provides SR/IOV support included, but I have implemented a test example which can be found together with this patch set here:
git://github.com/knuto/qemu.git sriov_patches_v4
Testing with the example device was documented here:
http://lists.nongnu.org/archive/html/qemu-devel/2014-08/msg05110.html
Changes since v3:
- Reworked 'pci: Update pci_regs header' to merge kernel version improvements with the current qemu version instead of copying from the kernel version.
Changes since v2:
- Rebased onto 090d0bfd
- Un-qdev'ified - avoids issues when resetting NUM_VFS
- Fixed handling of vf_offset/vf_stride
Changes since v1:
- Rebased on top of latest master, eliminating prereqs.
- Implement proper support for VF_STRIDE, VF_OFFSET and SUP_PGSIZE Time better spent fixing it than explaining what the previous limitations were.
- Added new first patch to fix pci bug related to this
- Split out patch to pci_default_config_write to a separate patch 2 to highlight bug fix.
- Refactored out logic into new source files hw/pci/pcie_sriov.c include/hw/pci/pcie_sriov.h similar to pcie_aer.c/h.
- Rename functions and introduce structs to better separate pf and vf functionality.
- Replaced is_vf member with pci_is_vf() function abstraction
- Fix numerous syntax, whitespace and comment issues according to Michael's review.
- Fix memory leaks.
- Removed igb example device - a rebased version available on github instead.
Knut Omang (3):
pci: Make use of the devfn property when registering new devices
pci: Update pci_regs header
pcie: Add support for Single Root I/O Virtualization (SR/IOV)
hw/pci/Makefile.objs | 2 +- hw/pci/pci.c | 101 ++++++++--- hw/pci/pcie.c | 9 +- hw/pci/pcie_sriov.c | 271 ++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 11 +- include/hw/pci/pcie.h | 6 + include/hw/pci/pcie_sriov.h | 55 ++++++ include/qemu/typedefs.h | 2 + include/standard-headers/linux/pci_regs.h | 142 +++++++++++++--- 9 files changed, 545 insertions(+), 54 deletions(-) create mode 100644 hw/pci/pcie_sriov.c create mode 100644 include/hw/pci/pcie_sriov.h
--
2.4.3
PATCH v4 1/3 pci: Make use of the devfn property when registering new devices
Without this, the devfn argument to pci_create_*() does not affect the assigned devfn.
Needed to support (VF_STRIDE,VF_OFFSET) values other than (1,1) for SR/IOV.
--- hw/pci/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/pci/pci.c +1 -1
@@ -1840,7 +1840,7 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp) bus =PCI_BUS (qdev_get_parent_bus (qdev)); pci_dev =do_pci_register_device (pci_dev, bus,object_get_typename (OBJECT (qdev)), - pci_dev->devfn, errp); +object_property_get_int (OBJECT (qdev),"addr" ,NULL ), errp);if (pci_dev ==NULL )return ;
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH v4 1/3] pci: Make use of the devfn property when registering new devices
Quoted 16 lines · Knut Omang
Without this, the devfn argument to pci_create_*() does not affect the assigned devfn.
Needed to support (VF_STRIDE,VF_OFFSET) values other than (1,1) for SR/IOV.
Signed-off-by Knut Omang <[email protected]>--- hw/pci/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/pci/pci.c +0
-0
hw/pci/pci.c static void pci_qdev_realize(DeviceState *qdev, Error **errp) · line 1843
Quoted 5 lines
hw/pci/pci.c +1
-1@@ -1840,7 +1840,7 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp) bus =PCI_BUS (qdev_get_parent_bus (qdev)); pci_dev =do_pci_register_device (pci_dev, bus,object_get_typename (OBJECT (qdev)), - pci_dev->devfn, errp); +object_property_get_int (OBJECT (qdev),"addr" ,NULL ), errp);
Hi,
I don't get this, using object_property_get_int on "addr" should return the value of pci_dev->devfn, can you please tell what exactly is not working?
Thanks,
Marcel
Quoted 4 lines
hw/pci/pci.c +0
-0if (pci_dev ==NULL )return ;
Re: [Qemu-devel] [PATCH v4 1/3] pci: Make use of the devfn property when registering new devices
Quoted 27 lines · Marcel Apfelbaum
Quoted 16 lines · Knut Omang
Without this, the devfn argument to pci_create_*() does not affect the assigned devfn.
Needed to support (VF_STRIDE,VF_OFFSET) values other than (1,1) for SR/IOV.
Signed-off-by Knut Omang <[email protected]>--- hw/pci/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/pci/pci.c +0
-0hw/pci/pci.c static void pci_qdev_realize(DeviceState · line 1846
Quoted 8 lines
hw/pci/pci.c +1
-1@@ -1840,7 +1840,7 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp) bus =PCI_BUS (qdev_get_parent_bus (qdev)); pci_dev =do_pci_register_device (pci_dev, bus,object_get_typename (OBJECT (qdev)), - pci_dev->devfn, errp); +object_property_get_int (OBJECT (qdev),"addr" ,NULL ), errp);Hi,
Quoted 3 lines
I don't get this, using
object_property_get_inton "addr" should return the value ofpci_dev->devfn,
can you please tell what exactly is not working?
The problem is that at that point pci_dev->devfn has not been set yet - have commented on this before somewhere..
Knut
Quoted 10 lines
Thanks,
MarcelQuoted 4 lines
if (pci_dev == NULL) return;
Marcel Apfelbaum
Reviewed-byRe: [Qemu-devel] [PATCH v4 1/3] pci: Make use of the devfn property when registering new devices
Quoted 28 lines · Knut Omang
Quoted 27 lines · Marcel Apfelbaum
Quoted 16 lines · Knut Omang
Without this, the devfn argument to pci_create_*() does not affect the assigned devfn.
Needed to support (VF_STRIDE,VF_OFFSET) values other than (1,1) for SR/IOV.
Signed-off-by Knut Omang <[email protected]>--- hw/pci/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/pci/pci.c +0
-0hw/pci/pci.c static void pci_qdev_realize(DeviceState · line 1846
Quoted 8 lines
hw/pci/pci.c +1
-1@@ -1840,7 +1840,7 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp) bus =PCI_BUS (qdev_get_parent_bus (qdev)); pci_dev =do_pci_register_device (pci_dev, bus,object_get_typename (OBJECT (qdev)), - pci_dev->devfn, errp); +object_property_get_int (OBJECT (qdev),"addr" ,NULL ), errp);Hi,
Quoted 7 lines
Quoted 3 lines
I don't get this, using
object_property_get_inton "addr" should return the value ofpci_dev->devfn,
can you please tell what exactly is not working?The problem is that at that point
pci_dev->devfnhas not been set yet - have commented on this before somewhere..
But "addr" property has the right value? Is indeed strange because it should get the value from pci_dev->devfn.
Don't get me wrong, this patch is OK.
I just want to understand if we have a hidden bug somewhere.
Anyway,
Quoted 12 lines
Knut
Quoted 10 lines
Thanks,
MarcelQuoted 4 lines
if (pci_dev == NULL) return;
Re: [Qemu-devel] [PATCH v4 1/3] pci: Make use of the devfn property when registering new devices
Quoted 33 lines · Marcel Apfelbaum
Quoted 32 lines · Knut Omang
Quoted 30 lines · Marcel Apfelbaum
Quoted 16 lines · Knut Omang
Without this, the devfn argument to pci_create_*() does not affect the assigned devfn.
Needed to support (VF_STRIDE,VF_OFFSET) values other than (1,1) for SR/IOV.
Signed-off-by Knut Omang <[email protected]>--- hw/pci/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/pci/pci.c +0
-0hw/pci/pci.c static void pci_qdev_realize(DeviceState · line 1846
Quoted 8 lines
hw/pci/pci.c +1
-1@@ -1840,7 +1840,7 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp) bus =PCI_BUS (qdev_get_parent_bus (qdev)); pci_dev =do_pci_register_device (pci_dev, bus,object_get_typename (OBJECT (qdev)), - pci_dev->devfn, errp); +object_property_get_int (OBJECT (qdev),"addr" ,NULL ), errp);Hi,
I don't get this, using
object_property_get_inton "addr" should return the value ofpci_dev->devfn,
can you please tell what exactly is not working?
Quoted 8 lines
Quoted 4 lines
The problem is that at that point
pci_dev->devfnhas not been set yet -
have commented on this before somewhere..But "addr" property has the right value? Is indeed strange because it should
get the value frompci_dev->devfn.
In the current version, in pci_qdev_realize()
the devfn argument to do_pci_register_device() is set to pci_dev->devfn. Then inside do_pci_register_device that devfn argument is again used to set pci_dev->devfn.
When the SR/IOV code tries to add a second device (the first VF) at (devfn + offset + stride) to the bus, it fails because devfn is 0 and that hits the PF in the devices[] array.
Quoted 2 lines
Don't get me wrong, this patch is OK.
I just want to understand if we have a hidden bug somewhere.
Yes, havent dived into the details for a while but it probably works because the pci_dev->devfn argument is often -1 (don't care) and then the code will just search for an available function number.
Quoted 2 lines
Anyway,
Reviewed-by Marcel Apfelbaum <[email protected]>
Thanks,
Knut
Quoted 14 lines
Quoted 12 lines
Knut
Quoted 10 lines
Thanks,
MarcelQuoted 4 lines
if (pci_dev == NULL) return;
PATCH v4 2/3 pci: Update pci_regs header
In reply to Knut OmangMerge in new definitions from kernel v4.2 Adds definition necessary to support emulated SR/IOV.
--- include/standard-headers/linux/pci_regs.h | 142 +++++++++++++++++++++++++----- 1 file changed, 118 insertions(+), 24 deletions(-)
include/standard-headers/linux/pci_regs.h +118 -24
@@ -304,13 +304,14 @@#define PCI_MSI_PENDING_64 20/* Pending bits register for 32-bit devices */ /* MSI-X registers */ -#define PCI_MSIX_FLAGS 2 -#define PCI_MSIX_FLAGS_QSIZE 0x7FF -#define PCI_MSIX_FLAGS_ENABLE (1 << 15) -#define PCI_MSIX_FLAGS_MASKALL (1 << 14) -#define PCI_MSIX_TABLE 4 -#define PCI_MSIX_PBA 8 +#define PCI_MSIX_FLAGS 2/* Message Control */ +#define PCI_MSIX_FLAGS_QSIZE 0x7FF/* Table size */ +#define PCI_MSIX_FLAGS_ENABLE (1 << 15)/* MSI-X enable */ +#define PCI_MSIX_FLAGS_MASKALL (1 << 14)/* Mask all vectors for this function */ +#define PCI_MSIX_TABLE 4/* Table offset */ +#define PCI_MSIX_PBA 8/* Pending Bit Array offset */ #define PCI_MSIX_FLAGS_BIRMASK (7 << 0) +#define PCI_CAP_MSIX_SIZEOF 12/* size of MSIX capability structure */ /* MSI-X entry's format */ #define PCI_MSIX_ENTRY_SIZE 16
@@ -517,31 +518,63 @@#define PCI_EXP_OBFF_MSG 0x40000/* New message signaling */ #define PCI_EXP_OBFF_WAKE 0x80000/* Re-use WAKE# for OBFF */ #define PCI_EXP_DEVCTL2 40/* Device Control 2 */ -#define PCI_EXP_DEVCTL2_ARI 0x20/* Alternative Routing-ID */ -#define PCI_EXP_IDO_REQ_EN 0x100/* ID-based ordering request enable */ -#define PCI_EXP_IDO_CMP_EN 0x200/* ID-based ordering completion enable */ -#define PCI_EXP_LTR_EN 0x400/* Latency tolerance reporting */ -#define PCI_EXP_OBFF_MSGA_EN 0x2000/* OBFF enable with Message type A */ -#define PCI_EXP_OBFF_MSGB_EN 0x4000/* OBFF enable with Message type B */ -#define PCI_EXP_OBFF_WAKE_EN 0x6000/* OBFF using WAKE# signaling */ +#define PCI_EXP_DEVCTL2_COMP_TIMEOUT 0x000f/* Completion Timeout Value */ +#define PCI_EXP_DEVCTL2_ARI 0x0020/* Alternative Routing-ID */ +#define PCI_EXP_DEVCTL2_IDO_REQ_EN 0x0100/* Allow IDO for requests */ +#define PCI_EXP_DEVCTL2_IDO_CMP_EN 0x0200/* Allow IDO for completions */ +#define PCI_EXP_DEVCTL2_LTR_EN 0x0400/* Enable LTR mechanism */ +#define PCI_EXP_DEVCTL2_OBFF_MSGA_EN 0x2000/* Enable OBFF Message type A */ +#define PCI_EXP_DEVCTL2_OBFF_MSGB_EN 0x4000/* Enable OBFF Message type B */ +#define PCI_EXP_DEVCTL2_OBFF_WAKE_EN 0x6000/* OBFF using WAKE# signaling */ +#define PCI_EXP_DEVSTA2 42/* Device Status 2 */ +#define PCI_CAP_EXP_ENDPOINT_SIZEOF_V2 44/* v2 endpoints end here */ +#define PCI_EXP_LNKCAP2 44/* Link Capabilities 2 */ +#define PCI_EXP_LNKCAP2_SLS_2_5GB 0x00000002/* Supported Speed 2.5GT/s */ +#define PCI_EXP_LNKCAP2_SLS_5_0GB 0x00000004/* Supported Speed 5.0GT/s */ +#define PCI_EXP_LNKCAP2_SLS_8_0GB 0x00000008/* Supported Speed 8.0GT/s */ +#define PCI_EXP_LNKCAP2_CROSSLINK 0x00000100/* Crosslink supported */ #define PCI_EXP_LNKCTL2 48/* Link Control 2 */ -#define PCI_EXP_SLTCTL2 56/* Slot Control 2 */ +#define PCI_EXP_LNKSTA2 50/* Link Status 2 */ +#define PCI_EXP_SLTCAP2 52/* Slot Capabilities 2 */ +#define PCI_EXP_SLTCTL2 56/* Slot Control 2 */ +#define PCI_EXP_SLTSTA2 58/* Slot Status 2 */ /* Extended Capabilities (PCI-X 2.0 and Express) */ #define PCI_EXT_CAP_ID (header) (header & 0x0000ffff)#define PCI_EXT_CAP_VER (header) ((header >> 16) & 0xf)#define PCI_EXT_CAP_NEXT (header) ((header >> 20) & 0xffc) -#define PCI_EXT_CAP_ID_ERR 1 -#define PCI_EXT_CAP_ID_VC 2 -#define PCI_EXT_CAP_ID_DSN 3 -#define PCI_EXT_CAP_ID_PWR 4 -#define PCI_EXT_CAP_ID_VNDR 11 -#define PCI_EXT_CAP_ID_ACS 13 -#define PCI_EXT_CAP_ID_ARI 14 -#define PCI_EXT_CAP_ID_ATS 15 -#define PCI_EXT_CAP_ID_SRIOV 16 -#define PCI_EXT_CAP_ID_LTR 24 +#define PCI_EXT_CAP_ID_ERR 0x01/* Advanced Error Reporting */ +#define PCI_EXT_CAP_ID_VC 0x02/* Virtual Channel Capability */ +#define PCI_EXT_CAP_ID_DSN 0x03/* Device Serial Number */ +#define PCI_EXT_CAP_ID_PWR 0x04/* Power Budgeting */ +#define PCI_EXT_CAP_ID_RCLD 0x05/* Root Complex Link Declaration */ +#define PCI_EXT_CAP_ID_RCILC 0x06/* Root Complex Internal Link Control */ +#define PCI_EXT_CAP_ID_RCEC 0x07/* Root Complex Event Collector */ +#define PCI_EXT_CAP_ID_MFVC 0x08/* Multi-Function VC Capability */ +#define PCI_EXT_CAP_ID_VC9 0x09/* same as _VC */ +#define PCI_EXT_CAP_ID_RCRB 0x0A/* Root Complex RB? */ +#define PCI_EXT_CAP_ID_VNDR 0x0B/* Vendor-Specific */ +#define PCI_EXT_CAP_ID_CAC 0x0C/* Config Access - obsolete */ +#define PCI_EXT_CAP_ID_ACS 0x0D/* Access Control Services */ +#define PCI_EXT_CAP_ID_ARI 0x0E/* Alternate Routing ID */ +#define PCI_EXT_CAP_ID_ATS 0x0F/* Address Translation Services */ +#define PCI_EXT_CAP_ID_SRIOV 0x10/* Single Root I/O Virtualization */ +#define PCI_EXT_CAP_ID_MRIOV 0x11/* Multi Root I/O Virtualization */ +#define PCI_EXT_CAP_ID_MCAST 0x12/* Multicast */ +#define PCI_EXT_CAP_ID_PRI 0x13/* Page Request Interface */ +#define PCI_EXT_CAP_ID_AMD_XXX 0x14/* Reserved for AMD */ +#define PCI_EXT_CAP_ID_REBAR 0x15/* Resizable BAR */ +#define PCI_EXT_CAP_ID_DPA 0x16/* Dynamic Power Allocation */ +#define PCI_EXT_CAP_ID_TPH 0x17/* TPH Requester */ +#define PCI_EXT_CAP_ID_LTR 0x18/* Latency Tolerance Reporting */ +#define PCI_EXT_CAP_ID_SECPCI 0x19/* Secondary PCIe Capability */ +#define PCI_EXT_CAP_ID_PMUX 0x1A/* Protocol Multiplexing */ +#define PCI_EXT_CAP_ID_PASID 0x1B/* Process Address Space ID */ +#define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_PASID + +#define PCI_EXT_CAP_DSN_SIZEOF 12 +#define PCI_EXT_CAP_MCAST_ENDPOINT_SIZEOF 40/* Advanced Error Reporting */ #define PCI_ERR_UNCOR_STATUS 4/* Uncorrectable Error Status */
@@ -556,6 +589,11 @@#define PCI_ERR_UNC_MALF_TLP 0x00040000/* Malformed TLP */ #define PCI_ERR_UNC_ECRC 0x00080000/* ECRC Error Status */ #define PCI_ERR_UNC_UNSUP 0x00100000/* Unsupported Request */ +#define PCI_ERR_UNC_ACSV 0x00200000/* ACS Violation */ +#define PCI_ERR_UNC_INTN 0x00400000/* internal error */ +#define PCI_ERR_UNC_MCBTLP 0x00800000/* MC blocked TLP */ +#define PCI_ERR_UNC_ATOMEG 0x01000000/* Atomic egress blocked */ +#define PCI_ERR_UNC_TLPPRE 0x02000000/* TLP prefix blocked */ #define PCI_ERR_UNCOR_MASK 8/* Uncorrectable Error Mask */ /* Same bits as above */ #define PCI_ERR_UNCOR_SEVER 12/* Uncorrectable Error Severity */
@@ -657,6 +695,7 @@#define PCI_ARI_CTRL_MFVC 0x0001/* MFVC Function Groups Enable */ #define PCI_ARI_CTRL_ACS 0x0002/* ACS Function Groups Enable */ #define PCI_ARI_CTRL_FG (x) (((x) >> 4) & 7)/* Function Group */ +#define PCI_EXT_CAP_ARI_SIZEOF 8/* Address Translation Service */ #define PCI_ATS_CAP 0x04/* ATS Capability Register */
@@ -666,6 +705,29 @@#define PCI_ATS_CTRL_ENABLE 0x8000/* ATS Enable */ #define PCI_ATS_CTRL_STU (x) ((x) & 0x1f)/* Smallest Translation Unit */ #define PCI_ATS_MIN_STU 12/* shift of minimum STU block */ +#define PCI_EXT_CAP_ATS_SIZEOF 8 + +/* Page Request Interface */ +#define PCI_PRI_CTRL 0x04/* PRI control register */ +#define PCI_PRI_CTRL_ENABLE 0x01/* Enable */ +#define PCI_PRI_CTRL_RESET 0x02/* Reset */ +#define PCI_PRI_STATUS 0x06/* PRI status register */ +#define PCI_PRI_STATUS_RF 0x001/* Response Failure */ +#define PCI_PRI_STATUS_UPRGI 0x002/* Unexpected PRG index */ +#define PCI_PRI_STATUS_STOPPED 0x100/* PRI Stopped */ +#define PCI_PRI_MAX_REQ 0x08/* PRI max reqs supported */ +#define PCI_PRI_ALLOC_REQ 0x0c/* PRI max reqs allowed */ +#define PCI_EXT_CAP_PRI_SIZEOF 16 + +/* Process Address Space ID */ +#define PCI_PASID_CAP 0x04/* PASID feature register */ +#define PCI_PASID_CAP_EXEC 0x02/* Exec permissions Supported */ +#define PCI_PASID_CAP_PRIV 0x04/* Privilege Mode Supported */ +#define PCI_PASID_CTRL 0x06/* PASID control register */ +#define PCI_PASID_CTRL_ENABLE 0x01/* Enable bit */ +#define PCI_PASID_CTRL_EXEC 0x02/* Exec permissions Enable */ +#define PCI_PASID_CTRL_PRIV 0x04/* Privilege Mode Enable */ +#define PCI_EXT_CAP_PASID_SIZEOF 8/* Single Root I/O Virtualization */ #define PCI_SRIOV_CAP 0x04/* SR-IOV Capabilities */
@@ -697,6 +759,7 @@#define PCI_SRIOV_VFM_MI 0x1/* Dormant.MigrateIn */ #define PCI_SRIOV_VFM_MO 0x2/* Active.MigrateOut */ #define PCI_SRIOV_VFM_AV 0x3/* Active.Available */ +#define PCI_EXT_CAP_SRIOV_SIZEOF 64#define PCI_LTR_MAX_SNOOP_LAT 0x4#define PCI_LTR_MAX_NOSNOOP_LAT 0x6
@@ -713,7 +776,38 @@#define PCI_ACS_UF 0x10/* Upstream Forwarding */ #define PCI_ACS_EC 0x20/* P2P Egress Control */ #define PCI_ACS_DT 0x40/* Direct Translated P2P */ +#define PCI_ACS_EGRESS_BITS 0x05/* ACS Egress Control Vector Size */ #define PCI_ACS_CTRL 0x06/* ACS Control Register */ #define PCI_ACS_EGRESS_CTL_V 0x08/* ACS Egress Control Vector */ +#define PCI_VSEC_HDR 4/* extended cap - vendor-specific */ +#define PCI_VSEC_HDR_LEN_SHIFT 20/* shift for length field */ + +/* SATA capability */ +#define PCI_SATA_REGS 4/* SATA REGs specifier */ +#define PCI_SATA_REGS_MASK 0xF/* location - BAR#/inline */ +#define PCI_SATA_REGS_INLINE 0xF/* REGS in config space */ +#define PCI_SATA_SIZEOF_SHORT 8 +#define PCI_SATA_SIZEOF_LONG 16 + +/* Resizable BARs */ +#define PCI_REBAR_CTRL 8/* control register */ +#define PCI_REBAR_CTRL_NBAR_MASK (7 << 5)/* mask for # bars */ +#define PCI_REBAR_CTRL_NBAR_SHIFT 5/* shift for # bars */ + +/* Dynamic Power Allocation */ +#define PCI_DPA_CAP 4/* capability register */ +#define PCI_DPA_CAP_SUBSTATE_MASK 0x1F/* # substates - 1 */ +#define PCI_DPA_BASE_SIZEOF 16/* size with 0 substates */ + +/* TPH Requester */ +#define PCI_TPH_CAP 4/* capability register */ +#define PCI_TPH_CAP_LOC_MASK 0x600/* location mask */ +#define PCI_TPH_LOC_NONE 0x000/* no location */ +#define PCI_TPH_LOC_CAP 0x200/* in capability */ +#define PCI_TPH_LOC_MSIX 0x400/* in MSI-X */ +#define PCI_TPH_CAP_ST_MASK 0x07FF0000/* st table mask */ +#define PCI_TPH_CAP_ST_SHIFT 16/* st table shift */ +#define PCI_TPH_BASE_SIZEOF 12/* size with no st table */ +#endif /* LINUX_PCI_REGS_H */
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH v4 2/3] pci: Update pci_regs header
Quoted 2 lines · Knut Omang
Merge in new definitions from kernel v4.2 Adds definition necessary to support emulated SR/IOV.
In the meantime Paolo updated this header to 4.3-RC1, is this patch still needed?
Thanks,
Marcel
Quoted 211 lines
Signed-off-by Knut Omang <[email protected]>--- include/standard-headers/linux/pci_regs.h | 142 +++++++++++++++++++++++++----- 1 file changed, 118 insertions(+), 24 deletions(-)include/standard-headers/linux/pci_regs.h +118
-24@@ -304,13 +304,14 @@#define PCI_MSI_PENDING_64 20/* Pending bits register for 32-bit devices */ /* MSI-X registers */ -#define PCI_MSIX_FLAGS 2 -#define PCI_MSIX_FLAGS_QSIZE 0x7FF -#define PCI_MSIX_FLAGS_ENABLE (1 << 15) -#define PCI_MSIX_FLAGS_MASKALL (1 << 14) -#define PCI_MSIX_TABLE 4 -#define PCI_MSIX_PBA 8 +#define PCI_MSIX_FLAGS 2/* Message Control */ +#define PCI_MSIX_FLAGS_QSIZE 0x7FF/* Table size */ +#define PCI_MSIX_FLAGS_ENABLE (1 << 15)/* MSI-X enable */ +#define PCI_MSIX_FLAGS_MASKALL (1 << 14)/* Mask all vectors for this function */ +#define PCI_MSIX_TABLE 4/* Table offset */ +#define PCI_MSIX_PBA 8/* Pending Bit Array offset */ #define PCI_MSIX_FLAGS_BIRMASK (7 << 0) +#define PCI_CAP_MSIX_SIZEOF 12/* size of MSIX capability structure */ /* MSI-X entry's format */ #define PCI_MSIX_ENTRY_SIZE 16@@ -517,31 +518,63 @@#define PCI_EXP_OBFF_MSG 0x40000/* New message signaling */ #define PCI_EXP_OBFF_WAKE 0x80000/* Re-use WAKE# for OBFF */ #define PCI_EXP_DEVCTL2 40/* Device Control 2 */ -#define PCI_EXP_DEVCTL2_ARI 0x20/* Alternative Routing-ID */ -#define PCI_EXP_IDO_REQ_EN 0x100/* ID-based ordering request enable */ -#define PCI_EXP_IDO_CMP_EN 0x200/* ID-based ordering completion enable */ -#define PCI_EXP_LTR_EN 0x400/* Latency tolerance reporting */ -#define PCI_EXP_OBFF_MSGA_EN 0x2000/* OBFF enable with Message type A */ -#define PCI_EXP_OBFF_MSGB_EN 0x4000/* OBFF enable with Message type B */ -#define PCI_EXP_OBFF_WAKE_EN 0x6000/* OBFF using WAKE# signaling */ +#define PCI_EXP_DEVCTL2_COMP_TIMEOUT 0x000f/* Completion Timeout Value */ +#define PCI_EXP_DEVCTL2_ARI 0x0020/* Alternative Routing-ID */ +#define PCI_EXP_DEVCTL2_IDO_REQ_EN 0x0100/* Allow IDO for requests */ +#define PCI_EXP_DEVCTL2_IDO_CMP_EN 0x0200/* Allow IDO for completions */ +#define PCI_EXP_DEVCTL2_LTR_EN 0x0400/* Enable LTR mechanism */ +#define PCI_EXP_DEVCTL2_OBFF_MSGA_EN 0x2000/* Enable OBFF Message type A */ +#define PCI_EXP_DEVCTL2_OBFF_MSGB_EN 0x4000/* Enable OBFF Message type B */ +#define PCI_EXP_DEVCTL2_OBFF_WAKE_EN 0x6000/* OBFF using WAKE# signaling */ +#define PCI_EXP_DEVSTA2 42/* Device Status 2 */ +#define PCI_CAP_EXP_ENDPOINT_SIZEOF_V2 44/* v2 endpoints end here */ +#define PCI_EXP_LNKCAP2 44/* Link Capabilities 2 */ +#define PCI_EXP_LNKCAP2_SLS_2_5GB 0x00000002/* Supported Speed 2.5GT/s */ +#define PCI_EXP_LNKCAP2_SLS_5_0GB 0x00000004/* Supported Speed 5.0GT/s */ +#define PCI_EXP_LNKCAP2_SLS_8_0GB 0x00000008/* Supported Speed 8.0GT/s */ +#define PCI_EXP_LNKCAP2_CROSSLINK 0x00000100/* Crosslink supported */ #define PCI_EXP_LNKCTL2 48/* Link Control 2 */ -#define PCI_EXP_SLTCTL2 56/* Slot Control 2 */ +#define PCI_EXP_LNKSTA2 50/* Link Status 2 */ +#define PCI_EXP_SLTCAP2 52/* Slot Capabilities 2 */ +#define PCI_EXP_SLTCTL2 56/* Slot Control 2 */ +#define PCI_EXP_SLTSTA2 58/* Slot Status 2 */ /* Extended Capabilities (PCI-X 2.0 and Express) */ #define PCI_EXT_CAP_ID (header) (header & 0x0000ffff)#define PCI_EXT_CAP_VER (header) ((header >> 16) & 0xf)#define PCI_EXT_CAP_NEXT (header) ((header >> 20) & 0xffc) -#define PCI_EXT_CAP_ID_ERR 1 -#define PCI_EXT_CAP_ID_VC 2 -#define PCI_EXT_CAP_ID_DSN 3 -#define PCI_EXT_CAP_ID_PWR 4 -#define PCI_EXT_CAP_ID_VNDR 11 -#define PCI_EXT_CAP_ID_ACS 13 -#define PCI_EXT_CAP_ID_ARI 14 -#define PCI_EXT_CAP_ID_ATS 15 -#define PCI_EXT_CAP_ID_SRIOV 16 -#define PCI_EXT_CAP_ID_LTR 24 +#define PCI_EXT_CAP_ID_ERR 0x01/* Advanced Error Reporting */ +#define PCI_EXT_CAP_ID_VC 0x02/* Virtual Channel Capability */ +#define PCI_EXT_CAP_ID_DSN 0x03/* Device Serial Number */ +#define PCI_EXT_CAP_ID_PWR 0x04/* Power Budgeting */ +#define PCI_EXT_CAP_ID_RCLD 0x05/* Root Complex Link Declaration */ +#define PCI_EXT_CAP_ID_RCILC 0x06/* Root Complex Internal Link Control */ +#define PCI_EXT_CAP_ID_RCEC 0x07/* Root Complex Event Collector */ +#define PCI_EXT_CAP_ID_MFVC 0x08/* Multi-Function VC Capability */ +#define PCI_EXT_CAP_ID_VC9 0x09/* same as _VC */ +#define PCI_EXT_CAP_ID_RCRB 0x0A/* Root Complex RB? */ +#define PCI_EXT_CAP_ID_VNDR 0x0B/* Vendor-Specific */ +#define PCI_EXT_CAP_ID_CAC 0x0C/* Config Access - obsolete */ +#define PCI_EXT_CAP_ID_ACS 0x0D/* Access Control Services */ +#define PCI_EXT_CAP_ID_ARI 0x0E/* Alternate Routing ID */ +#define PCI_EXT_CAP_ID_ATS 0x0F/* Address Translation Services */ +#define PCI_EXT_CAP_ID_SRIOV 0x10/* Single Root I/O Virtualization */ +#define PCI_EXT_CAP_ID_MRIOV 0x11/* Multi Root I/O Virtualization */ +#define PCI_EXT_CAP_ID_MCAST 0x12/* Multicast */ +#define PCI_EXT_CAP_ID_PRI 0x13/* Page Request Interface */ +#define PCI_EXT_CAP_ID_AMD_XXX 0x14/* Reserved for AMD */ +#define PCI_EXT_CAP_ID_REBAR 0x15/* Resizable BAR */ +#define PCI_EXT_CAP_ID_DPA 0x16/* Dynamic Power Allocation */ +#define PCI_EXT_CAP_ID_TPH 0x17/* TPH Requester */ +#define PCI_EXT_CAP_ID_LTR 0x18/* Latency Tolerance Reporting */ +#define PCI_EXT_CAP_ID_SECPCI 0x19/* Secondary PCIe Capability */ +#define PCI_EXT_CAP_ID_PMUX 0x1A/* Protocol Multiplexing */ +#define PCI_EXT_CAP_ID_PASID 0x1B/* Process Address Space ID */ +#define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_PASID + +#define PCI_EXT_CAP_DSN_SIZEOF 12 +#define PCI_EXT_CAP_MCAST_ENDPOINT_SIZEOF 40/* Advanced Error Reporting */ #define PCI_ERR_UNCOR_STATUS 4/* Uncorrectable Error Status */ @@ -556,6 +589,11 @@#define PCI_ERR_UNC_MALF_TLP 0x00040000/* Malformed TLP */ #define PCI_ERR_UNC_ECRC 0x00080000/* ECRC Error Status */ #define PCI_ERR_UNC_UNSUP 0x00100000/* Unsupported Request */ +#define PCI_ERR_UNC_ACSV 0x00200000/* ACS Violation */ +#define PCI_ERR_UNC_INTN 0x00400000/* internal error */ +#define PCI_ERR_UNC_MCBTLP 0x00800000/* MC blocked TLP */ +#define PCI_ERR_UNC_ATOMEG 0x01000000/* Atomic egress blocked */ +#define PCI_ERR_UNC_TLPPRE 0x02000000/* TLP prefix blocked */ #define PCI_ERR_UNCOR_MASK 8/* Uncorrectable Error Mask */ /* Same bits as above */ #define PCI_ERR_UNCOR_SEVER 12/* Uncorrectable Error Severity */ @@ -657,6 +695,7 @@#define PCI_ARI_CTRL_MFVC 0x0001/* MFVC Function Groups Enable */ #define PCI_ARI_CTRL_ACS 0x0002/* ACS Function Groups Enable */ #define PCI_ARI_CTRL_FG (x) (((x) >> 4) & 7)/* Function Group */ +#define PCI_EXT_CAP_ARI_SIZEOF 8/* Address Translation Service */ #define PCI_ATS_CAP 0x04/* ATS Capability Register */ @@ -666,6 +705,29 @@#define PCI_ATS_CTRL_ENABLE 0x8000/* ATS Enable */ #define PCI_ATS_CTRL_STU (x) ((x) & 0x1f)/* Smallest Translation Unit */ #define PCI_ATS_MIN_STU 12/* shift of minimum STU block */ +#define PCI_EXT_CAP_ATS_SIZEOF 8 + +/* Page Request Interface */ +#define PCI_PRI_CTRL 0x04/* PRI control register */ +#define PCI_PRI_CTRL_ENABLE 0x01/* Enable */ +#define PCI_PRI_CTRL_RESET 0x02/* Reset */ +#define PCI_PRI_STATUS 0x06/* PRI status register */ +#define PCI_PRI_STATUS_RF 0x001/* Response Failure */ +#define PCI_PRI_STATUS_UPRGI 0x002/* Unexpected PRG index */ +#define PCI_PRI_STATUS_STOPPED 0x100/* PRI Stopped */ +#define PCI_PRI_MAX_REQ 0x08/* PRI max reqs supported */ +#define PCI_PRI_ALLOC_REQ 0x0c/* PRI max reqs allowed */ +#define PCI_EXT_CAP_PRI_SIZEOF 16 + +/* Process Address Space ID */ +#define PCI_PASID_CAP 0x04/* PASID feature register */ +#define PCI_PASID_CAP_EXEC 0x02/* Exec permissions Supported */ +#define PCI_PASID_CAP_PRIV 0x04/* Privilege Mode Supported */ +#define PCI_PASID_CTRL 0x06/* PASID control register */ +#define PCI_PASID_CTRL_ENABLE 0x01/* Enable bit */ +#define PCI_PASID_CTRL_EXEC 0x02/* Exec permissions Enable */ +#define PCI_PASID_CTRL_PRIV 0x04/* Privilege Mode Enable */ +#define PCI_EXT_CAP_PASID_SIZEOF 8/* Single Root I/O Virtualization */ #define PCI_SRIOV_CAP 0x04/* SR-IOV Capabilities */ @@ -697,6 +759,7 @@#define PCI_SRIOV_VFM_MI 0x1/* Dormant.MigrateIn */ #define PCI_SRIOV_VFM_MO 0x2/* Active.MigrateOut */ #define PCI_SRIOV_VFM_AV 0x3/* Active.Available */ +#define PCI_EXT_CAP_SRIOV_SIZEOF 64#define PCI_LTR_MAX_SNOOP_LAT 0x4#define PCI_LTR_MAX_NOSNOOP_LAT 0x6@@ -713,7 +776,38 @@#define PCI_ACS_UF 0x10/* Upstream Forwarding */ #define PCI_ACS_EC 0x20/* P2P Egress Control */ #define PCI_ACS_DT 0x40/* Direct Translated P2P */ +#define PCI_ACS_EGRESS_BITS 0x05/* ACS Egress Control Vector Size */ #define PCI_ACS_CTRL 0x06/* ACS Control Register */ #define PCI_ACS_EGRESS_CTL_V 0x08/* ACS Egress Control Vector */ +#define PCI_VSEC_HDR 4/* extended cap - vendor-specific */ +#define PCI_VSEC_HDR_LEN_SHIFT 20/* shift for length field */ + +/* SATA capability */ +#define PCI_SATA_REGS 4/* SATA REGs specifier */ +#define PCI_SATA_REGS_MASK 0xF/* location - BAR#/inline */ +#define PCI_SATA_REGS_INLINE 0xF/* REGS in config space */ +#define PCI_SATA_SIZEOF_SHORT 8 +#define PCI_SATA_SIZEOF_LONG 16 + +/* Resizable BARs */ +#define PCI_REBAR_CTRL 8/* control register */ +#define PCI_REBAR_CTRL_NBAR_MASK (7 << 5)/* mask for # bars */ +#define PCI_REBAR_CTRL_NBAR_SHIFT 5/* shift for # bars */ + +/* Dynamic Power Allocation */ +#define PCI_DPA_CAP 4/* capability register */ +#define PCI_DPA_CAP_SUBSTATE_MASK 0x1F/* # substates - 1 */ +#define PCI_DPA_BASE_SIZEOF 16/* size with 0 substates */ + +/* TPH Requester */ +#define PCI_TPH_CAP 4/* capability register */ +#define PCI_TPH_CAP_LOC_MASK 0x600/* location mask */ +#define PCI_TPH_LOC_NONE 0x000/* no location */ +#define PCI_TPH_LOC_CAP 0x200/* in capability */ +#define PCI_TPH_LOC_MSIX 0x400/* in MSI-X */ +#define PCI_TPH_CAP_ST_MASK 0x07FF0000/* st table mask */ +#define PCI_TPH_CAP_ST_SHIFT 16/* st table shift */ +#define PCI_TPH_BASE_SIZEOF 12/* size with no st table */ +#endif /* LINUX_PCI_REGS_H */
Re: [Qemu-devel] [PATCH v4 2/3] pci: Update pci_regs header
Quoted 9 lines · Marcel Apfelbaum
Quoted 2 lines · Knut Omang
Merge in new definitions from kernel v4.2 Adds definition necessary to support emulated SR/IOV.
In the meantime Paolo updated this header to 4.3-RC1, is this patch still needed?
Thanks,
Marcel
Quite right - it is gone after rebase.
Thanks,
Knut
Quoted 331 lines
Quoted 330 lines
Signed-off-by Knut Omang <[email protected]>--- include/standard-headers/linux/pci_regs.h | 142 +++++++++++++++++++++++++----- 1 file changed, 118 insertions(+), 24 deletions(-)include/standard-headers/linux/pci_regs.h +0
-0@@ -304,13 +304,14 @@#define PCI_MSI_PENDING_64 20/* Pending bits register for 32-bit devices */
/* MSI-X registers */
include/standard-headers/linux/pci_regs.h +1
-6-#define PCI_MSIX_FLAGS 2 -#define PCI_MSIX_FLAGS_QSIZE 0x7FF -#define PCI_MSIX_FLAGS_ENABLE (1 << 15) -#define PCI_MSIX_FLAGS_MASKALL (1 << 14) -#define PCI_MSIX_TABLE 4 -#define PCI_MSIX_PBA 8 +#define PCI_MSIX_FLAGS 2/* Message Control */
include/standard-headers/linux/pci_regs.h +3
-0+#define PCI_MSIX_FLAGS_QSIZE 0x7FF/* Table size */ +#define PCI_MSIX_FLAGS_ENABLE (1 << 15)/* MSI-X enable */ +#define PCI_MSIX_FLAGS_MASKALL (1 << 14)/* Mask all vectors for this function */
include/standard-headers/linux/pci_regs.h +2
-0+#define PCI_MSIX_TABLE 4/* Table offset */ +#define PCI_MSIX_PBA 8/* Pending Bit Array offset */
#definePCI_MSIX_FLAGS_BIRMASK(7 << 0)
+#definePCI_CAP_MSIX_SIZEOF12 /* size of MSIX
capability structure *//* MSI-X entry's format */ #define PCI_MSIX_ENTRY_SIZE 16include/standard-headers/linux/pci_regs.h +0
-0@@ -517,31 +518,63 @@#define PCI_EXP_OBFF_MSG 0x40000/* New message signaling */ #define PCI_EXP_OBFF_WAKE 0x80000/* Re-use WAKE# for OBFF */
#definePCI_EXP_DEVCTL240 /* Device
Control 2 */
-#definePCI_EXP_DEVCTL2_ARI0x20/* Alternative
Routing-ID */
-#definePCI_EXP_IDO_REQ_EN0x100/* ID-based
ordering request enable */
-#definePCI_EXP_IDO_CMP_EN0x200/* ID-based
ordering completion enable */
-#definePCI_EXP_LTR_EN0x400/* Latency
tolerance reporting */
-#definePCI_EXP_OBFF_MSGA_EN0x2000/* OBFF enable
with Message type A */
-#definePCI_EXP_OBFF_MSGB_EN0x4000/* OBFF enable
with Message type B */
-#definePCI_EXP_OBFF_WAKE_EN0x6000/* OBFF using
WAKE# signaling */
+#definePCI_EXP_DEVCTL2_COMP_TIMEOUT0x000f/*
Completion Timeout Value */
+#definePCI_EXP_DEVCTL2_ARI0x0020/*
Alternative Routing-ID */
+#definePCI_EXP_DEVCTL2_IDO_REQ_EN0x0100/* Allow
IDO for requests */
+#definePCI_EXP_DEVCTL2_IDO_CMP_EN0x0200/* Allow
IDO for completions */
+#definePCI_EXP_DEVCTL2_LTR_EN0x0400/*
Enable LTR mechanism */
+#definePCI_EXP_DEVCTL2_OBFF_MSGA_EN0x2000/*
Enable OBFF Message type A */
+#definePCI_EXP_DEVCTL2_OBFF_MSGB_EN0x4000/*
Enable OBFF Message type B */
+#definePCI_EXP_DEVCTL2_OBFF_WAKE_EN0x6000/* OBFF
using WAKE# signaling */
+#definePCI_EXP_DEVSTA242 /* Device Status
2 */
+#definePCI_CAP_EXP_ENDPOINT_SIZEOF_V244 /* v2
endpoints end here */
+#definePCI_EXP_LNKCAP244 /* Link
Capabilities 2 */
+#definePCI_EXP_LNKCAP2_SLS_2_5GB0x00000002/* Supported
Speed 2.5GT/s */
+#definePCI_EXP_LNKCAP2_SLS_5_0GB0x00000004/* Supported
Speed 5.0GT/s */
+#definePCI_EXP_LNKCAP2_SLS_8_0GB0x00000008/* Supported
Speed 8.0GT/s */
+#definePCI_EXP_LNKCAP2_CROSSLINK0x00000100/* Crosslink
supported */
#definePCI_EXP_LNKCTL248 /* Link Control
2 */
-#definePCI_EXP_SLTCTL256 /* Slot Control 2
*/
+#definePCI_EXP_LNKSTA250 /* Link Status 2
*/
+#definePCI_EXP_SLTCAP252 /* Slot
Capabilities 2 */include/standard-headers/linux/pci_regs.h +2
-0+#define PCI_EXP_SLTCTL2 56/* Slot Control 2 */ +#define PCI_EXP_SLTSTA2 58/* Slot Status 2 */
/* Extended Capabilities (PCI-X 2.0 and Express) */
#definePCI_EXT_CAP_ID(header)(header &0x0000ffff)
#definePCI_EXT_CAP_VER(header)((header >> 16) &0xf)
#definePCI_EXT_CAP_NEXT(header)((header >> 20) &0xffc)include/standard-headers/linux/pci_regs.h +1
-10-#define PCI_EXT_CAP_ID_ERR 1 -#define PCI_EXT_CAP_ID_VC 2 -#define PCI_EXT_CAP_ID_DSN 3 -#define PCI_EXT_CAP_ID_PWR 4 -#define PCI_EXT_CAP_ID_VNDR 11 -#define PCI_EXT_CAP_ID_ACS 13 -#define PCI_EXT_CAP_ID_ARI 14 -#define PCI_EXT_CAP_ID_ATS 15 -#define PCI_EXT_CAP_ID_SRIOV 16 -#define PCI_EXT_CAP_ID_LTR 24 +#define PCI_EXT_CAP_ID_ERR 0x01/* Advanced Error Reporting */
+#definePCI_EXT_CAP_ID_VC0x02/* Virtual Channel
Capability */
+#definePCI_EXT_CAP_ID_DSN0x03/* Device Serial
Number */
+#definePCI_EXT_CAP_ID_PWR0x04/* Power Budgeting
*/
+#definePCI_EXT_CAP_ID_RCLD0x05/* Root Complex
Link Declaration */
+#definePCI_EXT_CAP_ID_RCILC0x06/* Root Complex
Internal Link Control */
+#definePCI_EXT_CAP_ID_RCEC0x07/* Root Complex
Event Collector */
+#definePCI_EXT_CAP_ID_MFVC0x08/* Multi-Function
VC Capability */include/standard-headers/linux/pci_regs.h +2
-0+#define PCI_EXT_CAP_ID_VC9 0x09/* same as _VC */ +#define PCI_EXT_CAP_ID_RCRB 0x0A/* Root Complex RB? */
+#definePCI_EXT_CAP_ID_VNDR0x0B/* Vendor-Specific
*/
+#definePCI_EXT_CAP_ID_CAC0x0C/* Config Access -
obsolete */
+#definePCI_EXT_CAP_ID_ACS0x0D/* Access Control
Services */
+#definePCI_EXT_CAP_ID_ARI0x0E/* Alternate Routing
ID */
+#definePCI_EXT_CAP_ID_ATS0x0F/* Address
Translation Services */
+#definePCI_EXT_CAP_ID_SRIOV0x10/* Single Root I/O
Virtualization */
+#definePCI_EXT_CAP_ID_MRIOV0x11/* Multi Root I/O
Virtualization */include/standard-headers/linux/pci_regs.h +2
-0+#define PCI_EXT_CAP_ID_MCAST 0x12/* Multicast */ +#define PCI_EXT_CAP_ID_PRI 0x13/* Page Request Interface */
+#definePCI_EXT_CAP_ID_AMD_XXX0x14/* Reserved for
AMD */
+#definePCI_EXT_CAP_ID_REBAR0x15/* Resizable BAR
*/
+#definePCI_EXT_CAP_ID_DPA0x16/* Dynamic Power
Allocation */include/standard-headers/linux/pci_regs.h +2
-0+#define PCI_EXT_CAP_ID_TPH 0x17/* TPH Requester */ +#define PCI_EXT_CAP_ID_LTR 0x18/* Latency Tolerance Reporting */
+#definePCI_EXT_CAP_ID_SECPCI0x19/* Secondary PCIe
Capability */
+#definePCI_EXT_CAP_ID_PMUX0x1A/* Protocol
Multiplexing */
+#definePCI_EXT_CAP_ID_PASID0x1B/* Process Address
Space ID */include/standard-headers/linux/pci_regs.h +4
-0+#define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_PASID + +#define PCI_EXT_CAP_DSN_SIZEOF 12 +#define PCI_EXT_CAP_MCAST_ENDPOINT_SIZEOF 40/* Advanced Error Reporting */ #define PCI_ERR_UNCOR_STATUS 4/* Uncorrectable Error Status */
include/standard-headers/linux/pci_regs.h +0
-0@@ -556,6 +589,11 @@#define PCI_ERR_UNC_MALF_TLP 0x00040000/* Malformed TLP */
#definePCI_ERR_UNC_ECRC0x00080000/* ECRC Error
Status */
#definePCI_ERR_UNC_UNSUP0x00100000/*
Unsupported Request */
+#definePCI_ERR_UNC_ACSV0x00200000/* ACS
Violation */
+#definePCI_ERR_UNC_INTN0x00400000/* internal
error */
+#definePCI_ERR_UNC_MCBTLP0x00800000/* MC blocked
TLP */
+#definePCI_ERR_UNC_ATOMEG0x01000000/* Atomic
egress blocked */
+#definePCI_ERR_UNC_TLPPRE0x02000000/* TLP prefix
blocked */
#definePCI_ERR_UNCOR_MASK8 /* Uncorrectable Error
Mask */
/* Same bits as above */
#definePCI_ERR_UNCOR_SEVER12 /* Uncorrectable
Error Severity */include/standard-headers/linux/pci_regs.h +0
-0@@ -657,6 +695,7 @@#define PCI_ARI_CTRL_MFVC 0x0001/* MFVC Function Groups Enable */
#definePCI_ARI_CTRL_ACS0x0002/* ACS Function
Groups Enable */
#definePCI_ARI_CTRL_FG(x)(((x) >> 4) & 7) /* Function
Group */
+#definePCI_EXT_CAP_ARI_SIZEOF8/* Address Translation Service */
#definePCI_ATS_CAP0x04/* ATS Capability
Register */include/standard-headers/linux/pci_regs.h +0
-0@@ -666,6 +705,29 @@#define PCI_ATS_CTRL_ENABLE 0x8000/* ATS Enable */ #define PCI_ATS_CTRL_STU (x) ((x) & 0x1f)/* Smallest Translation Unit */
#definePCI_ATS_MIN_STU12 /* shift of minimum STU
block */include/standard-headers/linux/pci_regs.h +4
-0+#define PCI_EXT_CAP_ATS_SIZEOF 8 + +/* Page Request Interface */ +#define PCI_PRI_CTRL 0x04/* PRI control register */
include/standard-headers/linux/pci_regs.h +3
-0+#define PCI_PRI_CTRL_ENABLE 0x01/* Enable */ +#define PCI_PRI_CTRL_RESET 0x02/* Reset */ +#define PCI_PRI_STATUS 0x06/* PRI status register */
+#definePCI_PRI_STATUS_RF0x001/* Response Failure
*/
+#definePCI_PRI_STATUS_UPRGI0x002/* Unexpected
PRG index */
+#definePCI_PRI_STATUS_STOPPED0x100/* PRI Stopped
*/
+#definePCI_PRI_MAX_REQ0x08/* PRI max reqs
supported */
+#definePCI_PRI_ALLOC_REQ0x0c/* PRI max reqs
allowed */include/standard-headers/linux/pci_regs.h +4
-0+#define PCI_EXT_CAP_PRI_SIZEOF 16 + +/* Process Address Space ID */ +#define PCI_PASID_CAP 0x04/* PASID feature register */
+#definePCI_PASID_CAP_EXEC0x02/* Exec permissions
Supported */
+#definePCI_PASID_CAP_PRIV0x04/* Privilege Mode
Supported */
+#definePCI_PASID_CTRL0x06/* PASID control
register */include/standard-headers/linux/pci_regs.h +2
-0+#define PCI_PASID_CTRL_ENABLE 0x01/* Enable bit */ +#define PCI_PASID_CTRL_EXEC 0x02/* Exec permissions Enable */
+#definePCI_PASID_CTRL_PRIV0x04/* Privilege Mode
Enable */
+#definePCI_EXT_CAP_PASID_SIZEOF8/* Single Root I/O Virtualization */
#definePCI_SRIOV_CAP0x04/* SR-IOV
Capabilities */include/standard-headers/linux/pci_regs.h +1
-0@@ -697,6 +759,7 @@#define PCI_SRIOV_VFM_MI 0x1/* Dormant.MigrateIn */ #define PCI_SRIOV_VFM_MO 0x2/* Active.MigrateOut */ #define PCI_SRIOV_VFM_AV 0x3/* Active.Available */ +#define PCI_EXT_CAP_SRIOV_SIZEOF 64#define PCI_LTR_MAX_SNOOP_LAT 0x4 #define PCI_LTR_MAX_NOSNOOP_LAT 0x6include/standard-headers/linux/pci_regs.h +0
-0@@ -713,7 +776,38 @@#define PCI_ACS_UF 0x10/* Upstream Forwarding */
#definePCI_ACS_EC0x20/* P2P Egress
Control */
#definePCI_ACS_DT0x40/* Direct
Translated P2P */
+#definePCI_ACS_EGRESS_BITS0x05/* ACS Egress
Control Vector Size */
#definePCI_ACS_CTRL0x06/* ACS Control
Register */
#definePCI_ACS_EGRESS_CTL_V0x08/* ACS Egress
Control Vector */+#define
PCI_VSEC_HDR4 /* extended cap -
vendor-specific */
+#definePCI_VSEC_HDR_LEN_SHIFT20 /* shift for
length field */include/standard-headers/linux/pci_regs.h +3
-0+ +/* SATA capability */ +#define PCI_SATA_REGS 4/* SATA REGs specifier */
+#definePCI_SATA_REGS_MASK0xF/* location -
BAR#/inline */
+#definePCI_SATA_REGS_INLINE0xF/* REGS in config
space */include/standard-headers/linux/pci_regs.h +5
-0+#define PCI_SATA_SIZEOF_SHORT 8 +#define PCI_SATA_SIZEOF_LONG 16 + +/* Resizable BARs */ +#define PCI_REBAR_CTRL 8/* control register */
+#definePCI_REBAR_CTRL_NBAR_MASK(7 << 5) /* mask
for # bars */
+#definePCI_REBAR_CTRL_NBAR_SHIFT5 /* shift for #
bars */include/standard-headers/linux/pci_regs.h +3
-0+ +/* Dynamic Power Allocation */ +#define PCI_DPA_CAP 4/* capability register */
+#definePCI_DPA_CAP_SUBSTATE_MASK0x1F/* #
substates - 1 */
+#definePCI_DPA_BASE_SIZEOF16 /* size with 0
substates */include/standard-headers/linux/pci_regs.h +3
-0+ +/* TPH Requester */ +#define PCI_TPH_CAP 4/* capability register */
+#definePCI_TPH_CAP_LOC_MASK0x600/* location mask
*/include/standard-headers/linux/pci_regs.h +4
-0+#define PCI_TPH_LOC_NONE 0x000/* no location */ +#define PCI_TPH_LOC_CAP 0x200/* in capability */ +#define PCI_TPH_LOC_MSIX 0x400/* in MSI-X */ +#define PCI_TPH_CAP_ST_MASK 0x07FF0000/* st table mask */
include/standard-headers/linux/pci_regs.h +2
-0+#define PCI_TPH_CAP_ST_SHIFT 16/* st table shift */ +#define PCI_TPH_BASE_SIZEOF 12/* size with no st table */
+
#endif /*LINUX_PCI_REGS_H*/
PATCH v4 3/3 pcie: Add support for Single Root I/O Virtualization (SR/IOV)
In reply to Knut OmangThis patch provides the building blocks for creating an SR/IOV PCIe Extended Capability header and register/unregister SR/IOV Virtual Functions.
--- hw/pci/Makefile.objs | 2 +- hw/pci/pci.c | 99 ++++++++++++---- hw/pci/pcie.c | 9 +- hw/pci/pcie_sriov.c | 271 ++++++++++++++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 11 +- include/hw/pci/pcie.h | 6 + include/hw/pci/pcie_sriov.h | 55 +++++++++ include/qemu/typedefs.h | 2 + 8 files changed, 426 insertions(+), 29 deletions(-) create mode 100644 hw/pci/pcie_sriov.c create mode 100644 include/hw/pci/pcie_sriov.h
hw/pci/Makefile.objs +1 -1
@@ -3,7 +3,7 @@ common-obj-$(CONFIG_PCI) += msix.o msi.o common-obj-$(CONFIG_PCI) += shpc.o common-obj-$(CONFIG_PCI) += slotid_cap.o common-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o -common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o +common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o pcie_sriov.o common-obj-$(call lnot,$(CONFIG_PCI)) += pci-stub.o common-obj-$(CONFIG_ALL) += pci-stub.o
hw/pci/pci.c +77 -22
158 lines not on this page · raw message
hw/pci/pcie.c +5 -4
@@ -253,7 +253,7 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,* Right now, only a device of function = 0 is allowed to be * hot plugged/unplugged. */ -assert (PCI_FUNC (pci_dev->devfn) ==0 ); +assert (PCI_FUNC (pci_dev->devfn) ==0 ||pci_is_vf (pci_dev));pci_word_test_and_set_mask (exp_cap +PCI_EXP_SLTSTA ,PCI_EXP_SLTSTA_PDS );
@@ -265,10 +265,11 @@ void pcie_cap_slot_hot_unplug_request_cb(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp) {uint8_t *exp_cap; +PCIDevice *pdev =PCI_DEVICE (hotplug_dev); -pcie_cap_slot_hotplug_common (PCI_DEVICE (hotplug_dev), dev, &exp_cap, errp); +pcie_cap_slot_hotplug_common (pdev, dev, &exp_cap, errp); -pcie_cap_slot_push_attention_button (PCI_DEVICE (hotplug_dev)); +pcie_cap_slot_push_attention_button (pdev); }/* pci express slot for pci express root/downstream port
@@ -408,7 +409,7 @@ void pcie_cap_slot_write_config(PCIDevice *dev, }/* -* If the slot is polulated, power indicator is off and power +* If the slot is populated, power indicator is off and power * controller is off, it is safe to detach the devices. */ if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val & PCI_EXP_SLTCTL_PCC) &&
hw/pci/pcie_sriov.c new file +271 -0
271 lines not on this page · raw message
include/hw/pci/pci.h +9 -2
@@ -11,8 +11,6 @@/* PCI includes legacy ISA access. */ #include "hw/isa/isa.h" -#include "hw/pci/pcie.h" -/* PCI bus */ #define PCI_DEVFN (slot, func) ((((slot) & 0x1f) << 3) | ((func) & 0x07))
@@ -132,6 +130,7 @@ enum {#define QEMU_PCI_VGA_IO_HI_SIZE 0x20#include "hw/pci/pci_regs.h" +#include "hw/pci/pcie.h" /* PCI HEADER_TYPE */ #define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80
@@ -421,6 +420,9 @@ typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int);AddressSpace *pci_device_iommu_address_space (PCIDevice *dev);void pci_setup_iommu (PCIBus *bus,PCIIOMMUFunc fn,void *opaque); +pcibus_t pci_bar_address (PCIDevice *d, +int reg,uint8_t type,pcibus_t size); +static inline void pci_set_byte (uint8_t *config,uint8_t val) {
@@ -672,6 +674,11 @@ static inline int pci_is_express(const PCIDevice *d)return d->cap_present &QEMU_PCI_CAP_EXPRESS ; } +static inline int pci_is_vf (const PCIDevice *d) +{ +return d->exp.sriov_vf.pf !=NULL ; +} +static inline uint32_t pci_config_size (const PCIDevice *d) {return pci_is_express (d) ?PCIE_CONFIG_SPACE_SIZE :PCI_CONFIG_SPACE_SIZE ;
include/hw/pci/pcie.h +6 -0
@@ -25,6 +25,7 @@#include "hw/pci/pci_regs.h" #include "hw/pci/pcie_regs.h" #include "hw/pci/pcie_aer.h" +#include "hw/pci/pcie_sriov.h" #include "hw/hotplug.h" typedef enum {
@@ -74,6 +75,11 @@ struct PCIExpressDevice {/* AER */ uint16_t aer_cap;PCIEAERLog aer_log; + +/* SR/IOV */ +uint16_t sriov_cap; +PCIESriovPF sriov_pf; +PCIESriovVF sriov_vf; };#define COMPAT_PROP_PCP "power_controller_present"
include/hw/pci/pcie_sriov.h new file +55 -0
55 lines not on this page · raw message
include/qemu/typedefs.h +2 -0
@@ -53,6 +53,8 @@ typedef struct PCIDevice PCIDevice;typedef struct PCIEAERErr PCIEAERErr ;typedef struct PCIEAERLog PCIEAERLog ;typedef struct PCIEAERMsg PCIEAERMsg ; +typedef struct PCIESriovPF PCIESriovPF ; +typedef struct PCIESriovVF PCIESriovVF ;typedef struct PCIEPort PCIEPort ;typedef struct PCIESlot PCIESlot ;typedef struct PCIExpressDevice PCIExpressDevice ;
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH v4 3/3] pcie: Add support for Single Root I/O Virtualization (SR/IOV)
Quoted 73 lines · Knut Omang
This patch provides the building blocks for creating an SR/IOV PCIe Extended Capability header and register/unregister SR/IOV Virtual Functions.
Signed-off-by Knut Omang <[email protected]>--- hw/pci/Makefile.objs | 2 +- hw/pci/pci.c | 99 ++++++++++++---- hw/pci/pcie.c | 9 +- hw/pci/pcie_sriov.c | 271 ++++++++++++++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 11 +- include/hw/pci/pcie.h | 6 + include/hw/pci/pcie_sriov.h | 55 +++++++++ include/qemu/typedefs.h | 2 + 8 files changed, 426 insertions(+), 29 deletions(-)create mode 100644
hw/pci/pcie_sriov.c
create mode 100644include/hw/pci/pcie_sriov.hhw/pci/Makefile.objs +1
-1@@ -3,7 +3,7 @@ common-obj-$(CONFIG_PCI) += msix.o msi.o common-obj-$(CONFIG_PCI) += shpc.o common-obj-$(CONFIG_PCI) += slotid_cap.o common-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o -common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o +common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o pcie_sriov.o common-obj-$(call lnot,$(CONFIG_PCI)) += pci-stub.o common-obj-$(CONFIG_ALL) += pci-stub.ohw/pci/pci.c +7
-13@@ -153,6 +153,9 @@ int pci_bar(PCIDevice *d, int reg) {uint8_t type; +/* PCIe virtual functions do not have their own BARs */ +assert (!pci_is_vf (d)); +if (reg !=PCI_ROM_SLOT )return PCI_BASE_ADDRESS_0 + reg *4 ;@@ -211,22 +214,13 @@ void pci_device_deassert_intx(PCIDevice *dev) } } -static void pci_do_device_reset (PCIDevice *dev) +static void pci_reset_regions (PCIDevice *dev) {int r; +if (pci_is_vf (dev)) { +return ; + } -pci_device_deassert_intx (dev); -assert (dev->irq_state ==0 ); - -/* Clear all writable bits */ -pci_word_test_and_clear_mask (dev->config +PCI_COMMAND , -pci_get_word (dev->wmask +PCI_COMMAND ) | -pci_get_word (dev->w1cmask +PCI_COMMAND )); -pci_word_test_and_clear_mask (dev->config +PCI_STATUS , -pci_get_word (dev->wmask +PCI_STATUS ) | -pci_get_word (dev->w1cmask +PCI_STATUS )); - dev->config[PCI_CACHE_LINE_SIZE ] =0x0 ; - dev->config[PCI_INTERRUPT_LINE ] =0x0 ;for (r =0 ; r <PCI_NUM_REGIONS ; ++r) {PCIIORegion *region = &dev->io_regions[r];if (!region->size) {
hw/pci/pci.c static void pci_do_device_reset(PCIDevice *dev) · line 241
Quoted 8 lines
hw/pci/pci.c +5
-0@@ -240,6 +234,27 @@ static void pci_do_device_reset(PCIDevice *dev)pci_set_long (dev->config +pci_bar (dev, r), region->type); } } +} + +static void pci_do_device_reset (PCIDevice *dev) +{ +qdev_reset_all (&dev->qdev);
Hi,
Thank you for resubmitting this series!
This is only a quick look, I hope I'll have more time next week to go over it again.
hw/pci/pci.c static void pci_do_device_reset(PCIDevice *dev) · line 243
Quoted 2 lines
hw/pci/pci.c +2
-0+ + dev->irq_state =0 ;
Are you sure we need the assignment above? It seems that the irq_state should be modified only by the intx wrappers as pci_device_deassert_intx and so.
hw/pci/pci.c static void pci_do_device_reset(PCIDevice *dev) · line 244
Quoted 1 line
hw/pci/pci.c +1
-0+pci_update_irq_status (dev);
Why do we have to update the irq status on reset?
Quoted 133 lines
hw/pci/pci.c +62
-9+pci_device_deassert_intx (dev); +assert (dev->irq_state ==0 ); + +/* Clear all writable bits */ +pci_word_test_and_clear_mask (dev->config +PCI_COMMAND , +pci_get_word (dev->wmask +PCI_COMMAND ) | +pci_get_word (dev->w1cmask +PCI_COMMAND )); +pci_word_test_and_clear_mask (dev->config +PCI_STATUS , +pci_get_word (dev->wmask +PCI_STATUS ) | +pci_get_word (dev->w1cmask +PCI_STATUS )); + dev->config[PCI_CACHE_LINE_SIZE ] =0x0 ; + dev->config[PCI_INTERRUPT_LINE ] =0x0 ; +pci_reset_regions (dev);pci_update_mappings (dev);msi_reset (dev);@@ -771,6 +786,15 @@ static void pci_init_multifunction(PCIBus *bus, PCIDevice *dev, Error **errp) dev->config[PCI_HEADER_TYPE ] |=PCI_HEADER_TYPE_MULTI_FUNCTION ; } +/* With SR/IOV and ARI, a device at function 0 need not be a multifunction +* device, as it may just be a VF that ended up with function 0 in +* the legacy PCI interpretation. Avoid failing in such cases: +*/ +if (pci_is_vf (dev) && + dev->exp.sriov_vf.pf->cap_present &QEMU_PCI_CAP_MULTIFUNCTION ) { +return ; + } +/* * multifunction bit is interpreted in two ways as follows. * - all functions must set the bit to 1. @@ -962,6 +986,7 @@ void pci_register_bar(PCIDevice *pci_dev, int region_num,uint64_t wmask;pcibus_t size =memory_region_size (memory); +assert (!pci_is_vf (pci_dev));/* VFs must use pcie_sriov_vf_register_bar */ assert (region_num >=0 );assert (region_num <PCI_NUM_REGIONS );if (size & (size-1 )) {@@ -1060,11 +1085,44 @@ pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)return pci_dev->io_regions[region_num].addr; } -static pcibus_t pci_bar_address (PCIDevice *d, -int reg,uint8_t type,pcibus_t size) + +static pcibus_t pci_config_get_bar_addr (PCIDevice *d,int reg, +uint8_t type,pcibus_t size) +{ +pcibus_t new_addr; +if (!pci_is_vf (d)) { +int bar =pci_bar (d, reg); +if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { + new_addr =pci_get_quad (d->config + bar); + }else { + new_addr =pci_get_long (d->config + bar); + } + }else { +PCIDevice *pf = d->exp.sriov_vf.pf; +uint16_t sriov_cap = pf->exp.sriov_cap; +int bar = sriov_cap +PCI_SRIOV_BAR + reg *4 ; +uint16_t vf_offset =pci_get_word (pf->config + sriov_cap +PCI_SRIOV_VF_OFFSET ); +uint16_t vf_stride =pci_get_word (pf->config + sriov_cap +PCI_SRIOV_VF_STRIDE ); +uint32_t vf_num = (d->devfn - (pf->devfn + vf_offset)) / vf_stride; + +if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { + new_addr =pci_get_quad (pf->config + bar); + }else { + new_addr =pci_get_long (pf->config + bar); + } + new_addr += vf_num * size; + } +if (reg !=PCI_ROM_SLOT ) { +/* Preserve the rom enable bit */ + new_addr &= ~(size -1 ); + } +return new_addr; +} + +pcibus_t pci_bar_address (PCIDevice *d, +int reg,uint8_t type,pcibus_t size) {pcibus_t new_addr, last_addr; -int bar =pci_bar (d, reg);uint16_t cmd =pci_get_word (d->config +PCI_COMMAND );Object *machine =qdev_get_machine ();ObjectClass *oc =object_get_class (machine);@@ -1075,7 +1133,7 @@ static pcibus_t pci_bar_address(PCIDevice *d,if (!(cmd &PCI_COMMAND_IO )) {return PCI_BAR_UNMAPPED ; } - new_addr =pci_get_long (d->config + bar) & ~(size -1 ); + new_addr =pci_config_get_bar_addr (d, reg, type, size); last_addr = new_addr + size -1 ;/* Check if 32 bit BAR wraps around explicitly. * TODO: make priorities correct and remove this work around. @@ -1090,11 +1148,7 @@ static pcibus_t pci_bar_address(PCIDevice *d,if (!(cmd &PCI_COMMAND_MEMORY )) {return PCI_BAR_UNMAPPED ; } -if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { - new_addr =pci_get_quad (d->config + bar); - }else { - new_addr =pci_get_long (d->config + bar); - } + new_addr =pci_config_get_bar_addr (d, reg, type, size);/* the ROM slot has a specific enable bit */ if (reg ==PCI_ROM_SLOT && !(new_addr &PCI_ROM_ADDRESS_ENABLE )) {return PCI_BAR_UNMAPPED ;@@ -1228,6 +1282,7 @@ void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val_in, intmsi_write_config (d, addr, val_in, l);msix_write_config (d, addr, val_in, l); +pcie_sriov_config_write (d, addr, val_in, l); }/***********************************************************/ hw/pci/pcie.c +1
-1@@ -253,7 +253,7 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,* Right now, only a device of function = 0 is allowed to be * hot plugged/unplugged. */ -assert (PCI_FUNC (pci_dev->devfn) ==0 ); +assert (PCI_FUNC (pci_dev->devfn) ==0 ||pci_is_vf (pci_dev));pci_word_test_and_set_mask (exp_cap +PCI_EXP_SLTSTA ,PCI_EXP_SLTSTA_PDS );@@ -265,10 +265,11 @@ void pcie_cap_slot_hot_unplug_request_cb(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp)
hw/pci/pcie.c void pcie_cap_slot_hot_unplug_request_cb(HotplugHandler *hotplug_dev, · line 273
Quoted 10 lines
hw/pci/pcie.c +3
-2{uint8_t *exp_cap; +PCIDevice *pdev =PCI_DEVICE (hotplug_dev); -pcie_cap_slot_hotplug_common (PCI_DEVICE (hotplug_dev), dev, &exp_cap, errp); +pcie_cap_slot_hotplug_common (pdev, dev, &exp_cap, errp); -pcie_cap_slot_push_attention_button (PCI_DEVICE (hotplug_dev)); +pcie_cap_slot_push_attention_button (pdev); }
This chunk is not directly liked to the patch, I would put it in a different patch.
hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 415
Quoted 11 lines
hw/pci/pcie.c +1
-1/* pci express slot for pci express root/downstream port @@ -408,7 +409,7 @@ void pcie_cap_slot_write_config(PCIDevice *dev, }/* -* If the slot is polulated, power indicator is off and power +* If the slot is populated, power indicator is off and power * controller is off, it is safe to detach the devices. */ if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val & PCI_EXP_SLTCTL_PCC) &&
Same here. I am always happy to have this kind of types taken care of, but I think a separate patch would be cleaner.
hw/pci/pcie_sriov.c line 6
Quoted 12 lines
hw/pci/pcie_sriov.c new file +6
-0@@ -0,0 +1,271 @@ +/* +* pcie_sriov.c: +* +* Implementation of SR/IOV emulation support. +* +* Copyright (c) 2014 Knut Omang <[email protected]>
2015 :)
Quoted 36 lines
hw/pci/pcie_sriov.c new file +36
-0+* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#include "hw/pci/pci.h" +#include "hw/pci/pcie.h" +#include "hw/pci/pci_bus.h" +#include "qemu/range.h" + +//#define DEBUG_SRIOV +#ifdef DEBUG_SRIOV +# define SRIOV_DPRINTF (fmt, ...) \ + fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__) +#else +# define SRIOV_DPRINTF (fmt, ...) do {} while (0) +#endif +#define SRIOV_DEV_PRINTF (dev, fmt, ...) \ + SRIOV_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__) + +static PCIDevice *register_vf (PCIDevice *pf,int devfn,const char *name); +static void unregister_vfs (PCIDevice *dev); + +void pcie_sriov_pf_init (PCIDevice *dev,uint16_t offset, +const char *vfname,uint16_t vf_dev_id, +uint16_t init_vfs,uint16_t total_vfs, +uint16_t vf_offset,uint16_t vf_stride) +{ +uint8_t *cfg = dev->config + offset; +uint8_t *wmask; + +pcie_add_capability (dev,PCI_EXT_CAP_ID_SRIOV ,1 , + offset,PCI_EXT_CAP_SRIOV_SIZEOF ); + dev->exp.sriov_cap = offset; + dev->exp.sriov_pf.num_vfs =0 ;
hw/pci/pcie_sriov.c line 52
Quoted 10 lines
hw/pci/pcie_sriov.c new file +10
-0+ dev->exp.sriov_pf.vfname =g_strdup (vfname); + dev->exp.sriov_pf.vf =NULL ; + +pci_set_word (cfg +PCI_SRIOV_VF_OFFSET , vf_offset); +pci_set_word (cfg +PCI_SRIOV_VF_STRIDE , vf_stride); + +/* Minimal page size support values required by the SR/IOV standard: +* 0x553 << 12 = 0x553000 = 4K + 8K + 64K + 256K + 1M + 4M +* Hard coded for simplicity in this version +*/
A define (with the above very good explanation) would be preferred.
Quoted 61 lines
hw/pci/pcie_sriov.c new file +61
-0+pci_set_word (cfg +PCI_SRIOV_SUP_PGSIZE ,0x553 ); + +/* Default is to use 4K pages, software can modify it +* to any of the supported bits +*/ +pci_set_word (cfg +PCI_SRIOV_SYS_PGSIZE ,0x1 ); + +/* Set up device ID and initial/total number of VFs available */ +pci_set_word (cfg +PCI_SRIOV_VF_DID , vf_dev_id); +pci_set_word (cfg +PCI_SRIOV_INITIAL_VF , init_vfs); +pci_set_word (cfg +PCI_SRIOV_TOTAL_VF , total_vfs); +pci_set_word (cfg +PCI_SRIOV_NUM_VF ,0 ); + +/* Write enable control bits */ + wmask = dev->wmask + offset; +pci_set_word (wmask +PCI_SRIOV_CTRL , +PCI_SRIOV_CTRL_VFE |PCI_SRIOV_CTRL_MSE |PCI_SRIOV_CTRL_ARI ); +pci_set_word (wmask +PCI_SRIOV_NUM_VF ,0xffff ); +pci_set_word (wmask +PCI_SRIOV_SYS_PGSIZE ,0x553 ); + +qdev_prop_set_bit (&dev->qdev,"multifunction" , true); +} + +void pcie_sriov_pf_exit (PCIDevice *dev) +{ +SRIOV_DPRINTF ("\n" ); +unregister_vfs (dev); +g_free ((char *)dev->exp.sriov_pf.vfname); + dev->exp.sriov_pf.vfname =NULL ; +} + +void pcie_sriov_pf_init_vf_bar (PCIDevice *dev,int region_num, +uint8_t type,dma_addr_t size) +{ +uint32_t addr; +uint64_t wmask; +uint16_t sriov_cap = dev->exp.sriov_cap; + +assert (sriov_cap >0 ); +assert (region_num >=0 ); +assert (region_num <PCI_NUM_REGIONS ); +assert (region_num !=PCI_ROM_SLOT ); + + wmask = ~(size -1 ); + addr = sriov_cap +PCI_SRIOV_BAR + region_num *4 ; + +pci_set_long (dev->config + addr, type); +if (!(type &PCI_BASE_ADDRESS_SPACE_IO ) && + type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { +pci_set_quad (dev->wmask + addr, wmask); +pci_set_quad (dev->cmask + addr, ~0ULL ); + }else { +pci_set_long (dev->wmask + addr, wmask &0xffffffff ); +pci_set_long (dev->cmask + addr,0xffffffff ); + } + dev->exp.sriov_pf.vf_bar_type[region_num] = type; +} + +void pcie_sriov_vf_register_bar (PCIDevice *dev,int region_num, +MemoryRegion *memory) +{
hw/pci/pcie_sriov.c line 123
Quoted 10 lines
hw/pci/pcie_sriov.c new file +10
-0+PCIIORegion *r; +uint8_t type; +pcibus_t size =memory_region_size (memory); + +assert (pci_is_vf (dev));/* PFs must use pci_register_bar */ +assert (region_num >=0 ); +assert (region_num <PCI_NUM_REGIONS ); + type = dev->exp.sriov_vf.pf->exp.sriov_pf.vf_bar_type[region_num]; + +if (size & (size-1 )) {
We already have a is_power_of_2 func defined in include/qemu/host-utils.h
hw/pci/pcie_sriov.c line 125
Quoted 2 lines
hw/pci/pcie_sriov.c new file +2
-0+fprintf (stderr,"ERROR: PCI region size must be a power" +" of two - type=0x%x, size=0x%" FMT_PCIBUS "\n" , type, size);
Maybe we should error_report instead of fprintf
Quoted 20 lines
hw/pci/pcie_sriov.c new file +20
-0+exit (1 ); + } + + r = &dev->io_regions[region_num]; + r->memory = memory; + r->address_space = + type &PCI_BASE_ADDRESS_SPACE_IO + ? dev->bus->address_space_io + : dev->bus->address_space_mem; + r->size = size; + r->type = type; + + r->addr =pci_bar_address (dev, region_num, r->type, r->size); +if (r->addr !=PCI_BAR_UNMAPPED ) { +memory_region_add_subregion_overlap (r->address_space, + r->addr, r->memory,1 ); + } +} + +static PCIDevice *register_vf (PCIDevice *pf,int devfn,const char *name)
hw/pci/pcie_sriov.c line 155
Quoted 10 lines
hw/pci/pcie_sriov.c new file +10
-0+{ +PCIDevice *dev =pci_create (pf->bus, devfn, name); + dev->exp.sriov_vf.pf = pf; +Error *local_err =NULL ; + +object_property_set_bool (OBJECT (&dev->qdev), true,"realized" , &local_err); +if (local_err) { +fprintf (stderr,"Failed to enable: %s\n" , +error_get_pretty (local_err)); +error_free (local_err);
and error_report_err here
Quoted 40 lines
hw/pci/pcie_sriov.c new file +40
-0+return NULL ; + } + +/* set vid/did according to sr/iov spec - they are not used */ +pci_config_set_vendor_id (dev->config,0xffff ); +pci_config_set_device_id (dev->config,0xffff ); +return dev; +} + +static void register_vfs (PCIDevice *dev) +{ +uint16_t num_vfs; +uint16_t i; +uint16_t sriov_cap = dev->exp.sriov_cap; +uint16_t vf_offset =pci_get_word (dev->config + sriov_cap +PCI_SRIOV_VF_OFFSET ); +uint16_t vf_stride =pci_get_word (dev->config + sriov_cap +PCI_SRIOV_VF_STRIDE ); +int32_t devfn = dev->devfn + vf_offset; + +assert (sriov_cap >0 ); + num_vfs =pci_get_word (dev->config + sriov_cap +PCI_SRIOV_NUM_VF ); + + dev->exp.sriov_pf.vf =g_malloc (sizeof (PCIDevice *) * num_vfs); +assert (dev->exp.sriov_pf.vf); + +SRIOV_DEV_PRINTF (dev,"creating %d vf devs\n" , num_vfs); +for (i =0 ; i < num_vfs; i++) { + dev->exp.sriov_pf.vf[i] =register_vf (dev, devfn, dev->exp.sriov_pf.vfname); +if (!dev->exp.sriov_pf.vf[i]) { +SRIOV_DEV_PRINTF (dev,"Failed to create VF %d at devfn %x.%x\n" , i, +PCI_SLOT (devfn),PCI_FUNC (devfn)); + num_vfs = i; +break ; + } + devfn += vf_stride; + } + dev->exp.sriov_pf.num_vfs = num_vfs; +} + +static void unregister_vfs (PCIDevice *dev) +{
hw/pci/pcie_sriov.c line 205
Quoted 10 lines
hw/pci/pcie_sriov.c new file +10
-0+Error *local_err =NULL ; +uint16_t num_vfs = dev->exp.sriov_pf.num_vfs; +uint16_t i; +SRIOV_DEV_PRINTF (dev,"Unregistering %d vf devs\n" , num_vfs); +for (i =0 ; i < num_vfs; i++) { +object_property_set_bool (OBJECT (&dev->exp.sriov_pf.vf[i]->qdev), false,"realized" , &local_err); +if (local_err) { +fprintf (stderr,"Failed to unplug: %s\n" , +error_get_pretty (local_err)); +error_free (local_err);
error_report_err
Quoted 80 lines
hw/pci/pcie_sriov.c new file +66
-0+ } + } +g_free (dev->exp.sriov_pf.vf); + dev->exp.sriov_pf.vf =NULL ; + dev->exp.sriov_pf.num_vfs =0 ; +} + +void pcie_sriov_config_write (PCIDevice *dev,uint32_t address,uint32_t val,int len) +{ +uint32_t off; +uint16_t sriov_cap = dev->exp.sriov_cap; + +if (!sriov_cap || address < sriov_cap) { +return ; + } + off = address - sriov_cap; +if (off >=PCI_EXT_CAP_SRIOV_SIZEOF ) { +return ; + } + +SRIOV_DEV_PRINTF (dev,"cap at 0x%x sriov offset 0x%x val 0x%x len %d\n" , + sriov_cap, off, val, len); + +if (range_covers_byte (off, len,PCI_SRIOV_CTRL )) { +if (dev->exp.sriov_pf.num_vfs) { +if (!(val &PCI_SRIOV_CTRL_VFE )) { +unregister_vfs (dev); + } + }else { +if (val &PCI_SRIOV_CTRL_VFE ) { +register_vfs (dev); + } + } + } +} + + +/* Reset SR/IOV VF Enable bit to trigger an unregister of all VFs */ +void pcie_sriov_pf_disable_vfs (PCIDevice *dev) +{ +uint16_t sriov_cap = dev->exp.sriov_cap; +if (sriov_cap) { +uint32_t val =pci_get_byte (dev->config + sriov_cap +PCI_SRIOV_CTRL ); +if (val &PCI_SRIOV_CTRL_VFE ) { + val &= ~PCI_SRIOV_CTRL_VFE ; +pcie_sriov_config_write (dev, sriov_cap +PCI_SRIOV_CTRL , val,1 ); + } + } +} + +/* Add optional supported page sizes to the mask of supported page sizes */ +void pcie_sriov_pf_add_sup_pgsize (PCIDevice *dev,uint16_t opt_sup_pgsize) +{ +uint8_t *cfg = dev->config + dev->exp.sriov_cap; +uint8_t *wmask = dev->wmask + dev->exp.sriov_cap; + +uint16_t sup_pgsize =pci_get_word (cfg +PCI_SRIOV_SUP_PGSIZE ); + + sup_pgsize |= opt_sup_pgsize; + +/* Make sure the new bits are set, and that system page size +* also can be set to any of the new values according to spec: +*/ +pci_set_word (cfg +PCI_SRIOV_SUP_PGSIZE , sup_pgsize); +pci_set_word (wmask +PCI_SRIOV_SYS_PGSIZE , sup_pgsize); +}include/hw/pci/pci.h +0
-2@@ -11,8 +11,6 @@/* PCI includes legacy ISA access. */ #include "hw/isa/isa.h" -#include "hw/pci/pcie.h" -/* PCI bus */ #define PCI_DEVFN (slot, func) ((((slot) & 0x1f) << 3) | ((func) & 0x07))
include/hw/pci/pci.h enum { · line 133
Quoted 4 lines
include/hw/pci/pci.h +1
-0@@ -132,6 +130,7 @@ enum {#define QEMU_PCI_VGA_IO_HI_SIZE 0x20#include "hw/pci/pci_regs.h" +#include "hw/pci/pcie.h"
Why is it moved here?
Quoted 55 lines
include/hw/pci/pci.h +8
-0/* PCI HEADER_TYPE */ #define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80@@ -421,6 +420,9 @@ typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int);AddressSpace *pci_device_iommu_address_space (PCIDevice *dev);void pci_setup_iommu (PCIBus *bus,PCIIOMMUFunc fn,void *opaque); +pcibus_t pci_bar_address (PCIDevice *d, +int reg,uint8_t type,pcibus_t size); +static inline void pci_set_byte (uint8_t *config,uint8_t val) {@@ -672,6 +674,11 @@ static inline int pci_is_express(const PCIDevice *d)return d->cap_present &QEMU_PCI_CAP_EXPRESS ; } +static inline int pci_is_vf (const PCIDevice *d) +{ +return d->exp.sriov_vf.pf !=NULL ; +} +static inline uint32_t pci_config_size (const PCIDevice *d) {return pci_is_express (d) ?PCIE_CONFIG_SPACE_SIZE :PCI_CONFIG_SPACE_SIZE ;include/hw/pci/pcie.h +6
-0@@ -25,6 +25,7 @@#include "hw/pci/pci_regs.h" #include "hw/pci/pcie_regs.h" #include "hw/pci/pcie_aer.h" +#include "hw/pci/pcie_sriov.h" #include "hw/hotplug.h" typedef enum {@@ -74,6 +75,11 @@ struct PCIExpressDevice {/* AER */ uint16_t aer_cap;PCIEAERLog aer_log; + +/* SR/IOV */ +uint16_t sriov_cap; +PCIESriovPF sriov_pf; +PCIESriovVF sriov_vf; };#define COMPAT_PROP_PCP "power_controller_present"include/hw/pci/pcie_sriov.h new file +0
-0
include/hw/pci/pcie_sriov.h line 6
Quoted 6 lines
include/hw/pci/pcie_sriov.h new file +6
-0@@ -0,0 +1,55 @@ +/* +* pcie_sriov.h: +* +* Implementation of SR/IOV emulation support. +* +* Copyright (c) 2014 Knut Omang <[email protected]>
2015
Quoted 55 lines
include/hw/pci/pcie_sriov.h new file +49
-0+* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef QEMU_PCIE_SRIOV_H +#define QEMU_PCIE_SRIOV_H + + + +struct PCIESriovPF { +uint16_t num_vfs;/* Number of virtual functions created */ +uint8_t vf_bar_type[PCI_NUM_REGIONS ];/* Store type for each VF bar */ +const char *vfname;/* Reference to the device type used for the VFs */ +PCIDevice **vf;/* Pointer to an array of num_vfs VF devices */ +}; + +struct PCIESriovVF { +PCIDevice *pf;/* Pointer back to owner physical function */ +}; + +void pcie_sriov_pf_init (PCIDevice *dev,uint16_t offset, +const char *vfname,uint16_t vf_dev_id, +uint16_t init_vfs,uint16_t total_vfs, +uint16_t vf_offset,uint16_t vf_stride); +void pcie_sriov_pf_exit (PCIDevice *dev); + +/* Set up a VF bar in the SR/IOV bar area */ +void pcie_sriov_pf_init_vf_bar (PCIDevice *dev,int region_num, +uint8_t type,dma_addr_t size); + +/* Instantiate a bar for a VF */ +void pcie_sriov_vf_register_bar (PCIDevice *dev,int region_num, +MemoryRegion *memory); + +/* Add optional supported page sizes to the mask of supported page sizes +* Page size values are interpreted as opt_sup_pgsize << 12. +*/ +void pcie_sriov_pf_add_sup_pgsize (PCIDevice *dev,uint16_t opt_sup_pgsize); + +/* SR/IOV capability config write handler */ +void pcie_sriov_config_write (PCIDevice *dev,uint32_t address, +uint32_t val,int len); + +/* Reset SR/IOV VF Enable bit to unregister all VFs */ +void pcie_sriov_pf_disable_vfs (PCIDevice *dev); + +#endif /* QEMU_PCIE_SRIOV_H */ include/qemu/typedefs.h +0
-0
include/qemu/typedefs.h typedef struct PCIDevice PCIDevice; · line 60
Quoted 8 lines
include/qemu/typedefs.h +2
-0@@ -53,6 +53,8 @@ typedef struct PCIDevice PCIDevice;typedef struct PCIEAERErr PCIEAERErr ;typedef struct PCIEAERLog PCIEAERLog ;typedef struct PCIEAERMsg PCIEAERMsg ; +typedef struct PCIESriovPF PCIESriovPF ; +typedef struct PCIESriovVF PCIESriovVF ;typedef struct PCIEPort PCIEPort ;typedef struct PCIESlot PCIESlot ;typedef struct PCIExpressDevice PCIExpressDevice ;
I hope I helped,
Thanks,
Marcel
Re: [Qemu-devel] [PATCH v4 3/3] pcie: Add support for Single Root I/O Virtualization (SR/IOV)
Quoted 107 lines · Marcel Apfelbaum
Quoted 79 lines · Knut Omang
This patch provides the building blocks for creating an SR/IOV PCIe Extended Capability header and register/unregister SR/IOV Virtual Functions.
Signed-off-by Knut Omang <[email protected]>--- hw/pci/Makefile.objs | 2 +- hw/pci/pci.c | 99 ++++++++++++---- hw/pci/pcie.c | 9 +- hw/pci/pcie_sriov.c | 271 ++++++++++++++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 11 +- include/hw/pci/pcie.h | 6 + include/hw/pci/pcie_sriov.h | 55 +++++++++ include/qemu/typedefs.h | 2 + 8 files changed, 426 insertions(+), 29 deletions(-)create mode 100644
hw/pci/pcie_sriov.c
create mode 100644include/hw/pci/pcie_sriov.hhw/pci/Makefile.objs +1
-1@@ -3,7 +3,7 @@ common-obj-$(CONFIG_PCI) += msix.o msi.o common-obj-$(CONFIG_PCI) += shpc.o common-obj-$(CONFIG_PCI) += slotid_cap.o common-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o -common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o +common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.opcie_sriov.o
common-obj-$(call lnot,$(CONFIG_PCI)) += pci-stub.o common-obj-$(CONFIG_ALL) += pci-stub.ohw/pci/pci.c +7
-7@@ -153,6 +153,9 @@ int pci_bar(PCIDevice *d, int reg) {uint8_t type; +/* PCIe virtual functions do not have their own BARs */ +assert (!pci_is_vf (d)); +if (reg !=PCI_ROM_SLOT )return PCI_BASE_ADDRESS_0 + reg *4 ;@@ -211,22 +214,13 @@ void pci_device_deassert_intx(PCIDevice *dev) } } -static void pci_do_device_reset (PCIDevice *dev) +static void pci_reset_regions (PCIDevice *dev) {int r; +if (pci_is_vf (dev)) { +return ; + } -pci_device_deassert_intx (dev); -assert (dev->irq_state ==0 ); - -/* Clear all writable bits */ - pci_word_test_and_clear_mask(dev->config + PCI_COMMAND, - pci_get_word(dev->wmask +
PCI_COMMAND) |
- pci_get_word(dev->w1cmask +PCI_COMMAND));hw/pci/pci.c +0
-2- pci_word_test_and_clear_mask(dev->config + PCI_STATUS, - pci_get_word(dev->wmask +
PCI_STATUS) |
- pci_get_word(dev->w1cmask +PCI_STATUS));hw/pci/pci.c +0
-2- dev->config[PCI_CACHE_LINE_SIZE ] =0x0 ; - dev->config[PCI_INTERRUPT_LINE ] =0x0 ;for (r =0 ; r <PCI_NUM_REGIONS ; ++r) {PCIIORegion *region = &dev->io_regions[r];if (!region->size) {hw/pci/pci.c static void pci_do_device_reset(PCIDevice · line 242
Quoted 10 lines
hw/pci/pci.c +5
-1@@ -240,6 +234,27 @@ static void pci_do_device_reset(PCIDevice *dev) pci_set_long(dev->config + pci_bar(dev, r), region ->type); } } +} + +static void pci_do_device_reset (PCIDevice *dev) +{ +qdev_reset_all (&dev->qdev);Hi,
Thank you for resubmitting this series!This is only a quick look, I hope I'll have more time next week to go over it again.
hw/pci/pci.c static void pci_do_device_reset(PCIDevice · line 244
Quoted 2 lines
hw/pci/pci.c +2
-0+ + dev->irq_state =0 ;Are you sure we need the assignment above? It seems that the
irq_state
should be modified only by the intx wrappers as
pci_device_deassert_intxand so.
Quoted 3 lines
hw/pci/pci.c static void pci_do_device_reset(PCIDevice *dev) · line 244
Quoted 1 line
hw/pci/pci.c +1
-0+pci_update_irq_status (dev);Why do we have to update the irq status on reset?
I struggled a lot with interrupts and PF disapperance in earlier versions, this may be unintended leftovers from rebase.
The intention was to avoid the qdev removal which caused problems with unintended "hot unplug of the PF" at VF removal earlier, but after some of the more recent QOM'ification all those problems disappeared.
I tried without these lines and the do_device_reset refactor and it seems to work just fine, will fix that in v5, thanks!
Quoted 165 lines
Quoted 165 lines
hw/pci/pci.c +6
-0+pci_device_deassert_intx (dev); +assert (dev->irq_state ==0 ); + +/* Clear all writable bits */ + pci_word_test_and_clear_mask(dev->config + PCI_COMMAND, + pci_get_word(dev->wmask +
PCI_COMMAND) |
+ pci_get_word(dev->w1cmask +PCI_COMMAND));hw/pci/pci.c +2
-0+ pci_word_test_and_clear_mask(dev->config + PCI_STATUS, + pci_get_word(dev->wmask +
PCI_STATUS) |
+ pci_get_word(dev->w1cmask +PCI_STATUS));hw/pci/pci.c +4
-0+ dev->config[PCI_CACHE_LINE_SIZE ] =0x0 ; + dev->config[PCI_INTERRUPT_LINE ] =0x0 ; +pci_reset_regions (dev);pci_update_mappings (dev);msi_reset (dev);@@ -771,6 +786,15 @@ static void pci_init_multifunction(PCIBus *bus, PCIDevice *dev, Error **errp) dev->config[PCI_HEADER_TYPE ] |=PCI_HEADER_TYPE_MULTI_FUNCTION ; } +/* With SR/IOV and ARI, a device at function 0 need not be a multifunction
+ * device, as it may just be a VF that ended up with function
0 inhw/pci/pci.c +4
-0+* the legacy PCI interpretation. Avoid failing in such cases: +*/ +if (pci_is_vf (dev) && + dev->exp.sriov_vf.pf->cap_present &
QEMU_PCI_CAP_MULTIFUNCTION) {hw/pci/pci.c +4
-0+return ; + } +/* * multifunction bit is interpreted in two ways as follows. * - all functions must set the bit to 1. @@ -962,6 +986,7 @@ void pci_register_bar(PCIDevice *pci_dev, int region_num, uint64_t wmask;pcibus_t size =memory_region_size (memory); +assert (!pci_is_vf (pci_dev));/* VFs must use
pcie_sriov_vf_register_bar*/assert(region_num >= 0); assert(region_num < PCI_NUM_REGIONS); if (size & (size-1)) {hw/pci/pci.c +37
-4@@ -1060,11 +1085,44 @@ pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)return pci_dev->io_regions[region_num].addr; } -static pcibus_t pci_bar_address(PCIDevice *d, - int reg, uint8_t type, pcibus_t size) + +static pcibus_t pci_config_get_bar_addr(PCIDevice *d, int reg, +uint8_t type,pcibus_t size) +{ +pcibus_t new_addr; +if (!pci_is_vf (d)) { +int bar =pci_bar (d, reg); +if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { + new_addr =pci_get_quad (d->config + bar); + }else { + new_addr =pci_get_long (d->config + bar); + } + }else { +PCIDevice *pf = d->exp.sriov_vf.pf; +uint16_t sriov_cap = pf->exp.sriov_cap; +int bar = sriov_cap +PCI_SRIOV_BAR + reg *4 ; +uint16_t vf_offset =pci_get_word (pf->config + sriov_cap +PCI_SRIOV_VF_OFFSET ); +uint16_t vf_stride =pci_get_word (pf->config + sriov_cap +PCI_SRIOV_VF_STRIDE ); +uint32_t vf_num = (d->devfn - (pf->devfn + vf_offset)) / vf_stride; + +if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { + new_addr =pci_get_quad (pf->config + bar); + }else { + new_addr =pci_get_long (pf->config + bar); + } + new_addr += vf_num * size; + } +if (reg !=PCI_ROM_SLOT ) { +/* Preserve the rom enable bit */ + new_addr &= ~(size -1 ); + } +return new_addr; +} + +pcibus_t pci_bar_address (PCIDevice *d, +int reg,uint8_t type,pcibus_t size) {pcibus_t new_addr, last_addr; -int bar =pci_bar (d, reg);uint16_t cmd =pci_get_word (d->config +PCI_COMMAND );Object *machine =qdev_get_machine ();ObjectClass *oc =object_get_class (machine);@@ -1075,7 +1133,7 @@ static pcibus_t pci_bar_address(PCIDevice *d,if (!(cmd &PCI_COMMAND_IO )) {return PCI_BAR_UNMAPPED ; } - new_addr =pci_get_long (d->config + bar) & ~(size -1 ); + new_addr =pci_config_get_bar_addr (d, reg, type, size); last_addr = new_addr + size -1 ;/* Check if 32 bit BAR wraps around explicitly. * TODO: make priorities correct and remove this work around.
hw/pci/pci.c +1
-5@@ -1090,11 +1148,7 @@ static pcibus_t pci_bar_address(PCIDevice *d, if (!(cmd & PCI_COMMAND_MEMORY)) {return PCI_BAR_UNMAPPED ; } -if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { - new_addr =pci_get_quad (d->config + bar); - }else { - new_addr =pci_get_long (d->config + bar); - } + new_addr =pci_config_get_bar_addr (d, reg, type, size);/* the ROM slot has a specific enable bit */ if (reg == PCI_ROM_SLOT && !(new_addr &PCI_ROM_ADDRESS_ENABLE)) { return PCI_BAR_UNMAPPED;hw/pci/pci.c +0
-0@@ -1228,6 +1282,7 @@ void pci_default_write_config(PCIDevice *d,
uint32_taddr,uint32_tval_in, intmsi_write_config(d, addr, val_in, l); msix_write_config(d, addr, val_in, l);hw/pci/pci.c +1
-0+pcie_sriov_config_write (d, addr, val_in, l); }/***********************************************************/ hw/pci/pcie.c +1
-1@@ -253,7 +253,7 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler*hotplug_dev, DeviceState *dev, * Right now, only a device of function = 0 is allowed to be * hot plugged/unplugged. */ -assert (PCI_FUNC (pci_dev->devfn) ==0 ); +assert (PCI_FUNC (pci_dev->devfn) ==0 ||pci_is_vf (pci_dev)); pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,PCI_EXP_SLTSTA_PDS);hw/pci/pcie.c +2
-1@@ -265,10 +265,11 @@ voidpcie_cap_slot_hot_unplug_request_cb (HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp) {uint8_t *exp_cap; +PCIDevice *pdev =PCI_DEVICE (hotplug_dev); -pcie_cap_slot_hotplug_common (PCI_DEVICE (hotplug_dev), dev, &exp_cap, errp); +pcie_cap_slot_hotplug_common (pdev, dev, &exp_cap, errp);
Quoted 6 lines
Quoted 3 lines
hw/pci/pcie.c +1
-1-pcie_cap_slot_push_attention_button (PCI_DEVICE (hotplug_dev)); +pcie_cap_slot_push_attention_button (pdev);}This chunk is not directly liked to the patch, I would put it in a different patch.
ok
Quoted 4 lines
Quoted 4 lines
/* pci express slot for pci express root/downstream port
hw/pci/pcie.c +0
-0@@ -408,7 +409,7 @@ void pcie_cap_slot_write_config(PCIDevice *dev, }
Quoted 12 lines
Quoted 7 lines
hw/pci/pcie.c +1
-1/* -* If the slot is polulated, power indicator is off and power +* If the slot is populated, power indicator is off and power * controller is off, it is safe to detach the devices. */ if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val &
PCI_EXP_SLTCTL_PCC) &&Same here. I am always happy to have this kind of types taken care of,
but I think a separate patch would be cleaner.
Would you like it in the patch set as a separate patch or should I schedule it for a trivial patches set instead, maybe together with the
PCI_DEVICE thing?
Quoted 6 lines
Quoted 6 lines
hw/pci/pcie_sriov.c new file +0
-0@@ -0,0 +1,271 @@
Quoted 8 lines
hw/pci/pcie_sriov.c line 6
Quoted 6 lines
hw/pci/pcie_sriov.c new file +6
-0+/* +* pcie_sriov.c: +* +* Implementation of SR/IOV emulation support. +* +* Copyright (c) 2014 Knut Omang <[email protected]> 2015 :)
thanks,
Quoted 47 lines
Quoted 47 lines
hw/pci/pcie_sriov.c new file +2
-0+* +* This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */hw/pci/pcie_sriov.c new file +10
-0+ +#include "hw/pci/pci.h" +#include "hw/pci/pcie.h" +#include "hw/pci/pci_bus.h" +#include "qemu/range.h" + +//#define DEBUG_SRIOV +#ifdef DEBUG_SRIOV +# define SRIOV_DPRINTF (fmt, ...) \ + fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ##
__VA_ARGS__)hw/pci/pcie_sriov.c new file +5
-0+#else +# define SRIOV_DPRINTF (fmt, ...) do {} while (0) +#endif +#define SRIOV_DEV_PRINTF (dev, fmt, ...) \ + SRIOV_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ##
__VA_ARGS__)hw/pci/pcie_sriov.c new file +2
-0+ +static PCIDevice *register_vf(PCIDevice *pf, int devfn, const char*name);
hw/pci/pcie_sriov.c new file +19
-0+static void unregister_vfs (PCIDevice *dev); + +void pcie_sriov_pf_init (PCIDevice *dev,uint16_t offset, +const char *vfname,uint16_t vf_dev_id, +uint16_t init_vfs,uint16_t total_vfs, +uint16_t vf_offset,uint16_t vf_stride) +{ +uint8_t *cfg = dev->config + offset; +uint8_t *wmask; + +pcie_add_capability (dev,PCI_EXT_CAP_ID_SRIOV ,1 , + offset,PCI_EXT_CAP_SRIOV_SIZEOF ); + dev->exp.sriov_cap = offset; + dev->exp.sriov_pf.num_vfs =0 ; + dev->exp.sriov_pf.vfname =g_strdup (vfname); + dev->exp.sriov_pf.vf =NULL ; + +pci_set_word (cfg +PCI_SRIOV_VF_OFFSET , vf_offset); +pci_set_word (cfg +PCI_SRIOV_VF_STRIDE , vf_stride);
Quoted 8 lines
hw/pci/pcie_sriov.c line 52
Quoted 6 lines
hw/pci/pcie_sriov.c new file +2
-0+ +/* Minimal page size support values required by the SR/IOV standard:
hw/pci/pcie_sriov.c new file +3
-0+* 0x553 << 12 = 0x553000 = 4K + 8K + 64K + 256K + 1M + 4M +* Hard coded for simplicity in this version +*/ A define (with the above very good explanation) would be preferred.
Will do
Quoted 69 lines
Quoted 69 lines
hw/pci/pcie_sriov.c new file +8
-0+pci_set_word (cfg +PCI_SRIOV_SUP_PGSIZE ,0x553 ); + +/* Default is to use 4K pages, software can modify it +* to any of the supported bits +*/ +pci_set_word (cfg +PCI_SRIOV_SYS_PGSIZE ,0x1 ); + +/* Set up device ID and initial/total number of VFs available */
hw/pci/pcie_sriov.c new file +9
-0+pci_set_word (cfg +PCI_SRIOV_VF_DID , vf_dev_id); +pci_set_word (cfg +PCI_SRIOV_INITIAL_VF , init_vfs); +pci_set_word (cfg +PCI_SRIOV_TOTAL_VF , total_vfs); +pci_set_word (cfg +PCI_SRIOV_NUM_VF ,0 ); + +/* Write enable control bits */ + wmask = dev->wmask + offset; + pci_set_word(wmask + PCI_SRIOV_CTRL, + PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE |
PCI_SRIOV_CTRL_ARI);hw/pci/pcie_sriov.c new file +50
-0+pci_set_word (wmask +PCI_SRIOV_NUM_VF ,0xffff ); +pci_set_word (wmask +PCI_SRIOV_SYS_PGSIZE ,0x553 ); + +qdev_prop_set_bit (&dev->qdev,"multifunction" , true); +} + +void pcie_sriov_pf_exit (PCIDevice *dev) +{ +SRIOV_DPRINTF ("\n" ); +unregister_vfs (dev); +g_free ((char *)dev->exp.sriov_pf.vfname); + dev->exp.sriov_pf.vfname =NULL ; +} + +void pcie_sriov_pf_init_vf_bar (PCIDevice *dev,int region_num, +uint8_t type,dma_addr_t size) +{ +uint32_t addr; +uint64_t wmask; +uint16_t sriov_cap = dev->exp.sriov_cap; + +assert (sriov_cap >0 ); +assert (region_num >=0 ); +assert (region_num <PCI_NUM_REGIONS ); +assert (region_num !=PCI_ROM_SLOT ); + + wmask = ~(size -1 ); + addr = sriov_cap +PCI_SRIOV_BAR + region_num *4 ; + +pci_set_long (dev->config + addr, type); +if (!(type &PCI_BASE_ADDRESS_SPACE_IO ) && + type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { +pci_set_quad (dev->wmask + addr, wmask); +pci_set_quad (dev->cmask + addr, ~0ULL ); + }else { +pci_set_long (dev->wmask + addr, wmask &0xffffffff ); +pci_set_long (dev->cmask + addr,0xffffffff ); + } + dev->exp.sriov_pf.vf_bar_type[region_num] = type; +} + +void pcie_sriov_vf_register_bar (PCIDevice *dev,int region_num, +MemoryRegion *memory) +{ +PCIIORegion *r; +uint8_t type; +pcibus_t size =memory_region_size (memory); + +assert (pci_is_vf (dev));/* PFs must use pci_register_bar */ +assert (region_num >=0 );
Quoted 8 lines
hw/pci/pcie_sriov.c line 123
Quoted 5 lines
hw/pci/pcie_sriov.c new file +4
-1+assert (region_num <PCI_NUM_REGIONS ); + type = dev->exp.sriov_vf.pf ->exp.sriov_pf.vf_bar_type[region_num]; + +if (size & (size-1 )) {We already have a
is_power_of_2func defined ininclude/qemu/host-utils.h
will fix,
Quoted 5 lines
Quoted 3 lines
hw/pci/pcie_sriov.c new file +2
-0+ fprintf(stderr, "ERROR: PCI region size must be a power" + " of two - type=0x%x, size=0x%"FMT_PCIBUS"\n",type, size);
Maybe we should
error_reportinstead of fprintf
Leftover from far too many rebases, will fix,
Quoted 27 lines
Quoted 27 lines
hw/pci/pcie_sriov.c new file +16
-0+exit (1 ); + } + + r = &dev->io_regions[region_num]; + r->memory = memory; + r->address_space = + type &PCI_BASE_ADDRESS_SPACE_IO + ? dev->bus->address_space_io + : dev->bus->address_space_mem; + r->size = size; + r->type = type; + + r->addr =pci_bar_address (dev, region_num, r->type, r->size); +if (r->addr !=PCI_BAR_UNMAPPED ) { + memory_region_add_subregion_overlap(r->address_space, + r->addr, r->memory,1);
hw/pci/pcie_sriov.c new file +4
-0+ } +} + +static PCIDevice *register_vf(PCIDevice *pf, int devfn, const char*name)
hw/pci/pcie_sriov.c new file +5
-0+{ +PCIDevice *dev =pci_create (pf->bus, devfn, name); + dev->exp.sriov_vf.pf = pf; +Error *local_err =NULL ; +
Quoted 8 lines
hw/pci/pcie_sriov.c line 155
Quoted 6 lines
+ object_property_set_bool(OBJECT(&dev->qdev), true, "realized",
&local_err);hw/pci/pcie_sriov.c new file +4
-0+if (local_err) { +fprintf (stderr,"Failed to enable: %s\n" , +error_get_pretty (local_err)); +error_free (local_err);and
error_report_errhere
ok
Quoted 151 lines
Quoted 47 lines
hw/pci/pcie_sriov.c new file +15
-0+return NULL ; + } + +/* set vid/did according to sr/iov spec - they are not used */ +pci_config_set_vendor_id (dev->config,0xffff ); +pci_config_set_device_id (dev->config,0xffff ); +return dev; +} + +static void register_vfs (PCIDevice *dev) +{ +uint16_t num_vfs; +uint16_t i; +uint16_t sriov_cap = dev->exp.sriov_cap; + uint16_t vf_offset = pci_get_word(dev->config + sriov_cap +
PCI_SRIOV_VF_OFFSET);
+uint16_tvf_stride= pci_get_word(dev->config +sriov_cap+PCI_SRIOV_VF_STRIDE);hw/pci/pcie_sriov.c new file +4
-0+int32_t devfn = dev->devfn + vf_offset; + +assert (sriov_cap >0 ); + num_vfs = pci_get_word(dev->config + sriov_cap +
PCI_SRIOV_NUM_VF);hw/pci/pcie_sriov.c new file +2
-0+ + dev->exp.sriov_pf.vf = g_malloc(sizeof(PCIDevice *) *
num_vfs);hw/pci/pcie_sriov.c new file +7
-1+ assert(dev->exp.sriov_pf.vf); + + SRIOV_DEV_PRINTF(dev, "creating %d vf devs\n", num_vfs); + for (i = 0; i < num_vfs; i++) { + dev->exp.sriov_pf.vf[i] = register_vf(dev, devfn, dev ->exp.sriov_pf.vfname); + if (!dev->exp.sriov_pf.vf[i]) { + SRIOV_DEV_PRINTF(dev, "Failed to create VF %d at devfn%x.%x\n", i,
hw/pci/pcie_sriov.c new file +12
-0+ PCI_SLOT(devfn), PCI_FUNC(devfn)); + num_vfs = i; +break ; + } + devfn += vf_stride; + } + dev->exp.sriov_pf.num_vfs = num_vfs; +} + +static void unregister_vfs (PCIDevice *dev) +{ +Error *local_err =NULL ;hw/pci/pcie_sriov.c line 205
Quoted 10 lines
hw/pci/pcie_sriov.c new file +9
-1+uint16_t num_vfs = dev->exp.sriov_pf.num_vfs; +uint16_t i; +SRIOV_DEV_PRINTF (dev,"Unregistering %d vf devs\n" , num_vfs); +for (i =0 ; i < num_vfs; i++) { + object_property_set_bool(OBJECT(&dev->exp.sriov_pf.vf[i] ->qdev), false, "realized", &local_err); + if (local_err) { + fprintf(stderr, "Failed to unplug: %s\n", + error_get_pretty(local_err)); + error_free(local_err);
error_report_errQuoted 91 lines
hw/pci/pcie_sriov.c new file +8
-0+ } + } +g_free (dev->exp.sriov_pf.vf); + dev->exp.sriov_pf.vf =NULL ; + dev->exp.sriov_pf.num_vfs =0 ; +} + +void pcie_sriov_config_write(PCIDevice *dev, uint32_t address,
uint32_tval, int len)hw/pci/pcie_sriov.c new file +13
-0+{ +uint32_t off; +uint16_t sriov_cap = dev->exp.sriov_cap; + +if (!sriov_cap || address < sriov_cap) { +return ; + } + off = address - sriov_cap; +if (off >=PCI_EXT_CAP_SRIOV_SIZEOF ) { +return ; + } + + SRIOV_DEV_PRINTF(dev, "cap at 0x%x sriov offset 0x%x val 0x%xlen %d\n",
hw/pci/pcie_sriov.c new file +17
-0+ sriov_cap, off, val, len); + +if (range_covers_byte (off, len,PCI_SRIOV_CTRL )) { +if (dev->exp.sriov_pf.num_vfs) { +if (!(val &PCI_SRIOV_CTRL_VFE )) { +unregister_vfs (dev); + } + }else { +if (val &PCI_SRIOV_CTRL_VFE ) { +register_vfs (dev); + } + } + } +} + + +/* Reset SR/IOV VF Enable bit to trigger an unregister of all VFs */
hw/pci/pcie_sriov.c new file +5
-0+void pcie_sriov_pf_disable_vfs (PCIDevice *dev) +{ +uint16_t sriov_cap = dev->exp.sriov_cap; +if (sriov_cap) { + uint32_t val = pci_get_byte(dev->config + sriov_cap +
PCI_SRIOV_CTRL);hw/pci/pcie_sriov.c new file +3
-0+if (val &PCI_SRIOV_CTRL_VFE ) { + val &= ~PCI_SRIOV_CTRL_VFE ; + pcie_sriov_config_write(dev, sriov_cap +
PCI_SRIOV_CTRL, val, 1);hw/pci/pcie_sriov.c new file +5
-0+ } + } +} + +/* Add optional supported page sizes to the mask of supported page sizes */
+voidpcie_sriov_pf_add_sup_pgsize(PCIDevice *dev, uint16_t opt_sup_pgsize)hw/pci/pcie_sriov.c new file +5
-0+{ +uint8_t *cfg = dev->config + dev->exp.sriov_cap; +uint8_t *wmask = dev->wmask + dev->exp.sriov_cap; + + uint16_t sup_pgsize = pci_get_word(cfg +
PCI_SRIOV_SUP_PGSIZE);hw/pci/pcie_sriov.c new file +9
-0+ + sup_pgsize |= opt_sup_pgsize; + +/* Make sure the new bits are set, and that system page size +* also can be set to any of the new values according to spec: +*/ +pci_set_word (cfg +PCI_SRIOV_SUP_PGSIZE , sup_pgsize); +pci_set_word (wmask +PCI_SRIOV_SYS_PGSIZE , sup_pgsize); +}include/hw/pci/pci.h +0
-2@@ -11,8 +11,6 @@/* PCI includes legacy ISA access. */ #include "hw/isa/isa.h" -#include "hw/pci/pcie.h" -/* PCI bus */ #define PCI_DEVFN (slot, func) ((((slot) & 0x1f) << 3) | ((func)&
0x07))include/hw/pci/pci.h +0
-0@@ -132,6 +130,7 @@ enum {#define QEMU_PCI_VGA_IO_HI_SIZE 0x20
Quoted 4 lines
include/hw/pci/pci.h enum { · line 133
Quoted 2 lines
include/hw/pci/pci.h +1
-0#include "hw/pci/pci_regs.h" +#include "hw/pci/pcie.h" Why is it moved here?
Probably an unintended side effect of rebasing..
Quoted 133 lines
Quoted 58 lines
include/hw/pci/pci.h +3
-0/* PCI HEADER_TYPE */ #define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80@@ -421,6 +420,9 @@ typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int);AddressSpace *pci_device_iommu_address_space (PCIDevice *dev);void pci_setup_iommu (PCIBus *bus,PCIIOMMUFunc fn,void *opaque); +pcibus_t pci_bar_address (PCIDevice *d, +int reg,uint8_t type,pcibus_t size); +static inline void pci_set_byte (uint8_t *config,uint8_t val){include/hw/pci/pci.h +5
-0@@ -672,6 +674,11 @@ static inline int pci_is_express(const PCIDevice *d)return d->cap_present &QEMU_PCI_CAP_EXPRESS ; } +static inline int pci_is_vf (const PCIDevice *d) +{ +return d->exp.sriov_vf.pf !=NULL ; +} +static inline uint32_t pci_config_size (const PCIDevice *d) {return
pci_is_express(d)?PCIE_CONFIG_SPACE_SIZE:PCI_CONFIG_SPACE_SIZE;include/hw/pci/pcie.h +6
-0@@ -25,6 +25,7 @@#include "hw/pci/pci_regs.h" #include "hw/pci/pcie_regs.h" #include "hw/pci/pcie_aer.h" +#include "hw/pci/pcie_sriov.h" #include "hw/hotplug.h" typedef enum {@@ -74,6 +75,11 @@ struct PCIExpressDevice {/* AER */ uint16_t aer_cap;PCIEAERLog aer_log; + +/* SR/IOV */ +uint16_t sriov_cap; +PCIESriovPF sriov_pf; +PCIESriovVF sriov_vf; };#define COMPAT_PROP_PCP "power_controller_present"include/hw/pci/pcie_sriov.h new file +0
-0include/hw/pci/pcie_sriov.h line 6
Quoted 6 lines
include/hw/pci/pcie_sriov.h new file +6
-0@@ -0,0 +1,55 @@ +/* +* pcie_sriov.h: +* +* Implementation of SR/IOV emulation support. +* +* Copyright (c) 2014 Knut Omang <[email protected]> 2015
Quoted 66 lines
include/hw/pci/pcie_sriov.h new file +2
-0+* +* This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */include/hw/pci/pcie_sriov.h new file +8
-0+ +#ifndef QEMU_PCIE_SRIOV_H +#define QEMU_PCIE_SRIOV_H + + + +struct PCIESriovPF { + uint16_t num_vfs; /* Number of virtual functionscreated */
+uint8_tvf_bar_type[PCI_NUM_REGIONS]; /* Store type for each
VF bar */
+ const char *vfname; /* Reference to the device type
used for the VFs */
+ PCIDevice **vf; /* Pointer to an array ofnum_vfs
VF devices */include/hw/pci/pcie_sriov.h new file +4
-0+}; + +struct PCIESriovVF { +PCIDevice *pf;/* Pointer back to owner physical function */
include/hw/pci/pcie_sriov.h new file +17
-0+}; + +void pcie_sriov_pf_init (PCIDevice *dev,uint16_t offset, +const char *vfname,uint16_t vf_dev_id, +uint16_t init_vfs,uint16_t total_vfs, +uint16_t vf_offset,uint16_t vf_stride); +void pcie_sriov_pf_exit (PCIDevice *dev); + +/* Set up a VF bar in the SR/IOV bar area */ +void pcie_sriov_pf_init_vf_bar (PCIDevice *dev,int region_num, +uint8_t type,dma_addr_t size); + +/* Instantiate a bar for a VF */ +void pcie_sriov_vf_register_bar (PCIDevice *dev,int region_num, +MemoryRegion *memory); + +/* Add optional supported page sizes to the mask of supported page sizes
+ * Page size values are interpreted asopt_sup_pgsize<< 12.
+ */
+voidpcie_sriov_pf_add_sup_pgsize(PCIDevice *dev, uint16_t opt_sup_pgsize);include/hw/pci/pcie_sriov.h new file +9
-0+ +/* SR/IOV capability config write handler */ +void pcie_sriov_config_write (PCIDevice *dev,uint32_t address, +uint32_t val,int len); + +/* Reset SR/IOV VF Enable bit to unregister all VFs */ +void pcie_sriov_pf_disable_vfs (PCIDevice *dev); + +#endif /* QEMU_PCIE_SRIOV_H */ include/qemu/typedefs.h +1
-0@@ -53,6 +53,8 @@ typedef struct PCIDevice PCIDevice;typedef struct PCIEAERErr PCIEAERErr ;typedef struct PCIEAERLog PCIEAERLog ;typedef struct PCIEAERMsg PCIEAERMsg ; +typedef struct PCIESriovPF PCIESriovPF ;
Quoted 8 lines
include/qemu/typedefs.h typedef struct PCIDevice PCIDevice; · line 60
Quoted 5 lines
include/qemu/typedefs.h +1
-0+typedef struct PCIESriovVF PCIESriovVF ;typedef struct PCIEPort PCIEPort ;typedef struct PCIESlot PCIESlot ;typedef struct PCIExpressDevice PCIExpressDevice ;I hope I helped,
Definitely,
thanks for the review!
Knut
Quoted 3 lines
Thanks,
Marcel
Re: [Qemu-devel] [PATCH v4 3/3] pcie: Add support for Single Root I/O Virtualization (SR/IOV)
Quoted 313 lines · Knut Omang
Quoted 107 lines · Marcel Apfelbaum
Quoted 79 lines · Knut Omang
This patch provides the building blocks for creating an SR/IOV PCIe Extended Capability header and register/unregister SR/IOV Virtual Functions.
Signed-off-by Knut Omang <[email protected]>--- hw/pci/Makefile.objs | 2 +- hw/pci/pci.c | 99 ++++++++++++---- hw/pci/pcie.c | 9 +- hw/pci/pcie_sriov.c | 271 ++++++++++++++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 11 +- include/hw/pci/pcie.h | 6 + include/hw/pci/pcie_sriov.h | 55 +++++++++ include/qemu/typedefs.h | 2 + 8 files changed, 426 insertions(+), 29 deletions(-)create mode 100644
hw/pci/pcie_sriov.c
create mode 100644include/hw/pci/pcie_sriov.hhw/pci/Makefile.objs +1
-1@@ -3,7 +3,7 @@ common-obj-$(CONFIG_PCI) += msix.o msi.o common-obj-$(CONFIG_PCI) += shpc.o common-obj-$(CONFIG_PCI) += slotid_cap.o common-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o -common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o +common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.opcie_sriov.o
common-obj-$(call lnot,$(CONFIG_PCI)) += pci-stub.o common-obj-$(CONFIG_ALL) += pci-stub.ohw/pci/pci.c +7
-7@@ -153,6 +153,9 @@ int pci_bar(PCIDevice *d, int reg) {uint8_t type; +/* PCIe virtual functions do not have their own BARs */ +assert (!pci_is_vf (d)); +if (reg !=PCI_ROM_SLOT )return PCI_BASE_ADDRESS_0 + reg *4 ;@@ -211,22 +214,13 @@ void pci_device_deassert_intx(PCIDevice *dev) } } -static void pci_do_device_reset (PCIDevice *dev) +static void pci_reset_regions (PCIDevice *dev) {int r; +if (pci_is_vf (dev)) { +return ; + } -pci_device_deassert_intx (dev); -assert (dev->irq_state ==0 ); - -/* Clear all writable bits */ - pci_word_test_and_clear_mask(dev->config + PCI_COMMAND, - pci_get_word(dev->wmask +
PCI_COMMAND) |
- pci_get_word(dev->w1cmask +PCI_COMMAND));hw/pci/pci.c +0
-2- pci_word_test_and_clear_mask(dev->config + PCI_STATUS, - pci_get_word(dev->wmask +
PCI_STATUS) |
- pci_get_word(dev->w1cmask +PCI_STATUS));hw/pci/pci.c +0
-2- dev->config[PCI_CACHE_LINE_SIZE ] =0x0 ; - dev->config[PCI_INTERRUPT_LINE ] =0x0 ;for (r =0 ; r <PCI_NUM_REGIONS ; ++r) {PCIIORegion *region = &dev->io_regions[r];if (!region->size) {hw/pci/pci.c static void pci_do_device_reset(PCIDevice · line 242
Quoted 10 lines
hw/pci/pci.c +5
-1@@ -240,6 +234,27 @@ static void pci_do_device_reset(PCIDevice *dev) pci_set_long(dev->config + pci_bar(dev, r), region ->type); } } +} + +static void pci_do_device_reset (PCIDevice *dev) +{ +qdev_reset_all (&dev->qdev);Hi,
Thank you for resubmitting this series!This is only a quick look, I hope I'll have more time next week to go over it again.
hw/pci/pci.c static void pci_do_device_reset(PCIDevice · line 244
Quoted 2 lines
hw/pci/pci.c +2
-0+ + dev->irq_state =0 ;Are you sure we need the assignment above? It seems that the
irq_state
should be modified only by the intx wrappers as
pci_device_deassert_intxand so.Quoted 3 lines
hw/pci/pci.c static void pci_do_device_reset(PCIDevice *dev) · line 244
Quoted 1 line
hw/pci/pci.c +1
-0+pci_update_irq_status (dev);Why do we have to update the irq status on reset?
I struggled a lot with interrupts and PF disapperance in earlier versions, this may be unintended leftovers from rebase.
The intention was to avoid the qdev removal which caused problems with unintended "hot unplug of the PF" at VF removal earlier, but after some of the more recent QOM'ification all those problems disappeared.
I tried without these lines and the
do_device_resetrefactor and it seems to work just fine, will fix that in v5, thanks!Quoted 165 lines
Quoted 165 lines
hw/pci/pci.c +6
-0+pci_device_deassert_intx (dev); +assert (dev->irq_state ==0 ); + +/* Clear all writable bits */ + pci_word_test_and_clear_mask(dev->config + PCI_COMMAND, + pci_get_word(dev->wmask +
PCI_COMMAND) |
+ pci_get_word(dev->w1cmask +PCI_COMMAND));hw/pci/pci.c +2
-0+ pci_word_test_and_clear_mask(dev->config + PCI_STATUS, + pci_get_word(dev->wmask +
PCI_STATUS) |
+ pci_get_word(dev->w1cmask +PCI_STATUS));hw/pci/pci.c +4
-0+ dev->config[PCI_CACHE_LINE_SIZE ] =0x0 ; + dev->config[PCI_INTERRUPT_LINE ] =0x0 ; +pci_reset_regions (dev);pci_update_mappings (dev);msi_reset (dev);@@ -771,6 +786,15 @@ static void pci_init_multifunction(PCIBus *bus, PCIDevice *dev, Error **errp) dev->config[PCI_HEADER_TYPE ] |=PCI_HEADER_TYPE_MULTI_FUNCTION ; } +/* With SR/IOV and ARI, a device at function 0 need not be a multifunction
+ * device, as it may just be a VF that ended up with function
0 inhw/pci/pci.c +4
-0+* the legacy PCI interpretation. Avoid failing in such cases: +*/ +if (pci_is_vf (dev) && + dev->exp.sriov_vf.pf->cap_present &
QEMU_PCI_CAP_MULTIFUNCTION) {hw/pci/pci.c +4
-0+return ; + } +/* * multifunction bit is interpreted in two ways as follows. * - all functions must set the bit to 1. @@ -962,6 +986,7 @@ void pci_register_bar(PCIDevice *pci_dev, int region_num, uint64_t wmask;pcibus_t size =memory_region_size (memory); +assert (!pci_is_vf (pci_dev));/* VFs must use
pcie_sriov_vf_register_bar*/assert(region_num >= 0); assert(region_num < PCI_NUM_REGIONS); if (size & (size-1)) {hw/pci/pci.c +37
-4@@ -1060,11 +1085,44 @@ pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)return pci_dev->io_regions[region_num].addr; } -static pcibus_t pci_bar_address(PCIDevice *d, - int reg, uint8_t type, pcibus_t size) + +static pcibus_t pci_config_get_bar_addr(PCIDevice *d, int reg, +uint8_t type,pcibus_t size) +{ +pcibus_t new_addr; +if (!pci_is_vf (d)) { +int bar =pci_bar (d, reg); +if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { + new_addr =pci_get_quad (d->config + bar); + }else { + new_addr =pci_get_long (d->config + bar); + } + }else { +PCIDevice *pf = d->exp.sriov_vf.pf; +uint16_t sriov_cap = pf->exp.sriov_cap; +int bar = sriov_cap +PCI_SRIOV_BAR + reg *4 ; +uint16_t vf_offset =pci_get_word (pf->config + sriov_cap +PCI_SRIOV_VF_OFFSET ); +uint16_t vf_stride =pci_get_word (pf->config + sriov_cap +PCI_SRIOV_VF_STRIDE ); +uint32_t vf_num = (d->devfn - (pf->devfn + vf_offset)) / vf_stride; + +if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { + new_addr =pci_get_quad (pf->config + bar); + }else { + new_addr =pci_get_long (pf->config + bar); + } + new_addr += vf_num * size; + } +if (reg !=PCI_ROM_SLOT ) { +/* Preserve the rom enable bit */ + new_addr &= ~(size -1 ); + } +return new_addr; +} + +pcibus_t pci_bar_address (PCIDevice *d, +int reg,uint8_t type,pcibus_t size) {pcibus_t new_addr, last_addr; -int bar =pci_bar (d, reg);uint16_t cmd =pci_get_word (d->config +PCI_COMMAND );Object *machine =qdev_get_machine ();ObjectClass *oc =object_get_class (machine);@@ -1075,7 +1133,7 @@ static pcibus_t pci_bar_address(PCIDevice *d,if (!(cmd &PCI_COMMAND_IO )) {return PCI_BAR_UNMAPPED ; } - new_addr =pci_get_long (d->config + bar) & ~(size -1 ); + new_addr =pci_config_get_bar_addr (d, reg, type, size); last_addr = new_addr + size -1 ;/* Check if 32 bit BAR wraps around explicitly. * TODO: make priorities correct and remove this work around.
hw/pci/pci.c +1
-5@@ -1090,11 +1148,7 @@ static pcibus_t pci_bar_address(PCIDevice *d, if (!(cmd & PCI_COMMAND_MEMORY)) {return PCI_BAR_UNMAPPED ; } -if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { - new_addr =pci_get_quad (d->config + bar); - }else { - new_addr =pci_get_long (d->config + bar); - } + new_addr =pci_config_get_bar_addr (d, reg, type, size);/* the ROM slot has a specific enable bit */ if (reg == PCI_ROM_SLOT && !(new_addr &PCI_ROM_ADDRESS_ENABLE)) { return PCI_BAR_UNMAPPED;hw/pci/pci.c +0
-0@@ -1228,6 +1282,7 @@ void pci_default_write_config(PCIDevice *d,
uint32_taddr,uint32_tval_in, intmsi_write_config(d, addr, val_in, l); msix_write_config(d, addr, val_in, l);hw/pci/pci.c +1
-0+pcie_sriov_config_write (d, addr, val_in, l); }/***********************************************************/ hw/pci/pcie.c +1
-1@@ -253,7 +253,7 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler*hotplug_dev, DeviceState *dev, * Right now, only a device of function = 0 is allowed to be * hot plugged/unplugged. */ -assert (PCI_FUNC (pci_dev->devfn) ==0 ); +assert (PCI_FUNC (pci_dev->devfn) ==0 ||pci_is_vf (pci_dev)); pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,PCI_EXP_SLTSTA_PDS);hw/pci/pcie.c +2
-1@@ -265,10 +265,11 @@ voidpcie_cap_slot_hot_unplug_request_cb (HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp) {uint8_t *exp_cap; +PCIDevice *pdev =PCI_DEVICE (hotplug_dev); -pcie_cap_slot_hotplug_common (PCI_DEVICE (hotplug_dev), dev, &exp_cap, errp); +pcie_cap_slot_hotplug_common (pdev, dev, &exp_cap, errp);Quoted 6 lines
Quoted 3 lines
hw/pci/pcie.c +1
-1-pcie_cap_slot_push_attention_button (PCI_DEVICE (hotplug_dev)); +pcie_cap_slot_push_attention_button (pdev);}This chunk is not directly liked to the patch, I would put it in a different patch.
ok
Quoted 4 lines
Quoted 4 lines
/* pci express slot for pci express root/downstream port
hw/pci/pcie.c +0
-0@@ -408,7 +409,7 @@ void pcie_cap_slot_write_config(PCIDevice *dev, }Quoted 12 lines
Quoted 7 lines
hw/pci/pcie.c +1
-1/* -* If the slot is polulated, power indicator is off and power +* If the slot is populated, power indicator is off and power * controller is off, it is safe to detach the devices. */ if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val &
PCI_EXP_SLTCTL_PCC) &&Same here. I am always happy to have this kind of types taken care of,
but I think a separate patch would be cleaner.
Quoted 3 lines
Would you like it in the patch set as a separate patch or should I schedule it for a trivial patches set instead, maybe together with the
PCI_DEVICEthing?
Hi,
I think in the same patch set would be just fine.
Thanks,
Marcel
Quoted 1 line
[...]
Re: [Qemu-devel] [PATCH v4 3/3] pcie: Add support for Single Root I/O Virtualization (SR/IOV)
In reply to Knut OmangQuoted 701 lines · Knut Omang
Quoted 109 lines · Marcel Apfelbaum
Quoted 80 lines · Knut Omang
This patch provides the building blocks for creating an SR/IOV PCIe Extended Capability header and register/unregister SR/IOV Virtual Functions.
Signed-off-by Knut Omang <[email protected]>--- hw/pci/Makefile.objs | 2 +- hw/pci/pci.c | 99 ++++++++++++---- hw/pci/pcie.c | 9 +- hw/pci/pcie_sriov.c | 271 ++++++++++++++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 11 +- include/hw/pci/pcie.h | 6 + include/hw/pci/pcie_sriov.h | 55 +++++++++ include/qemu/typedefs.h | 2 + 8 files changed, 426 insertions(+), 29 deletions(-)create mode 100644
hw/pci/pcie_sriov.c
create mode 100644include/hw/pci/pcie_sriov.hhw/pci/Makefile.objs +1
-1@@ -3,7 +3,7 @@ common-obj-$(CONFIG_PCI) += msix.o msi.o common-obj-$(CONFIG_PCI) += shpc.o common-obj-$(CONFIG_PCI) += slotid_cap.o common-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o -common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o +common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.opcie_sriov.o
common-obj-$(call lnot,$(CONFIG_PCI)) += pci-stub.o common-obj-$(CONFIG_ALL) += pci-stub.ohw/pci/pci.c +7
-7@@ -153,6 +153,9 @@ int pci_bar(PCIDevice *d, int reg) {uint8_t type; +/* PCIe virtual functions do not have their own BARs */ +assert (!pci_is_vf (d)); +if (reg !=PCI_ROM_SLOT )return PCI_BASE_ADDRESS_0 + reg *4 ;@@ -211,22 +214,13 @@ void pci_device_deassert_intx(PCIDevice *dev) } } -static void pci_do_device_reset (PCIDevice *dev) +static void pci_reset_regions (PCIDevice *dev) {int r; +if (pci_is_vf (dev)) { +return ; + } -pci_device_deassert_intx (dev); -assert (dev->irq_state ==0 ); - -/* Clear all writable bits */ - pci_word_test_and_clear_mask(dev->config + PCI_COMMAND, - pci_get_word(dev->wmask +
PCI_COMMAND) |
- pci_get_word(dev->w1cmask +PCI_COMMAND));hw/pci/pci.c +0
-2- pci_word_test_and_clear_mask(dev->config + PCI_STATUS, - pci_get_word(dev->wmask +
PCI_STATUS) |
- pci_get_word(dev->w1cmask +PCI_STATUS));hw/pci/pci.c +0
-2- dev->config[PCI_CACHE_LINE_SIZE ] =0x0 ; - dev->config[PCI_INTERRUPT_LINE ] =0x0 ;for (r =0 ; r <PCI_NUM_REGIONS ; ++r) {PCIIORegion *region = &dev->io_regions[r];if (!region->size) {hw/pci/pci.c static void pci_do_device_reset(PCIDevice · line 242
Quoted 10 lines
hw/pci/pci.c +5
-1@@ -240,6 +234,27 @@ static void pci_do_device_reset(PCIDevice *dev) pci_set_long(dev->config + pci_bar(dev, r), region ->type); } } +} + +static void pci_do_device_reset (PCIDevice *dev) +{ +qdev_reset_all (&dev->qdev);Hi,
Thank you for resubmitting this series!This is only a quick look, I hope I'll have more time next week to go
over it again.hw/pci/pci.c static void pci_do_device_reset(PCIDevice · line 244
Quoted 2 lines
hw/pci/pci.c +2
-0+ + dev->irq_state =0 ;Are you sure we need the assignment above? It seems that the
irq_state
should be modified only by the intx wrappers as
pci_device_deassert_intxand so.Quoted 3 lines
hw/pci/pci.c static void pci_do_device_reset(PCIDevice *dev) · line 244
Quoted 1 line
hw/pci/pci.c +1
-0+pci_update_irq_status (dev);Why do we have to update the irq status on reset?
I struggled a lot with interrupts and PF disapperance in earlier versions, this may be unintended leftovers from rebase.
The intention was to avoid the qdev removal which caused problems with
unintended "hot unplug of the PF" at VF removal earlier, but after some
of the more recent QOM'ification all those problems disappeared.I tried without these lines and the
do_device_resetrefactor and it seems to work just fine, will fix that in v5, thanks!Quoted 172 lines
Quoted 172 lines
hw/pci/pci.c +6
-0+pci_device_deassert_intx (dev); +assert (dev->irq_state ==0 ); + +/* Clear all writable bits */ + pci_word_test_and_clear_mask(dev->config + PCI_COMMAND, + pci_get_word(dev->wmask +
PCI_COMMAND) |
+ pci_get_word(dev->w1cmask +PCI_COMMAND));hw/pci/pci.c +2
-0+ pci_word_test_and_clear_mask(dev->config + PCI_STATUS, + pci_get_word(dev->wmask +
PCI_STATUS) |
+ pci_get_word(dev->w1cmask +PCI_STATUS));hw/pci/pci.c +4
-0+ dev->config[PCI_CACHE_LINE_SIZE ] =0x0 ; + dev->config[PCI_INTERRUPT_LINE ] =0x0 ; +pci_reset_regions (dev);pci_update_mappings (dev);msi_reset (dev);@@ -771,6 +786,15 @@ static void pci_init_multifunction(PCIBus *bus, PCIDevice *dev, Error **errp) dev->config[PCI_HEADER_TYPE ] |=PCI_HEADER_TYPE_MULTI_FUNCTION ; } +/* With SR/IOV and ARI, a device at function 0 need not be a multifunction
+ * device, as it may just be a VF that ended up with
function
0 in
+ * the legacy PCI interpretation. Avoid failing in such
cases:hw/pci/pci.c +3
-0+*/ +if (pci_is_vf (dev) && + dev->exp.sriov_vf.pf->cap_present &
QEMU_PCI_CAP_MULTIFUNCTION) {hw/pci/pci.c +4
-0+return ; + } +/* * multifunction bit is interpreted in two ways as follows. * - all functions must set the bit to 1. @@ -962,6 +986,7 @@ void pci_register_bar(PCIDevice *pci_dev, int region_num, uint64_t wmask;pcibus_t size =memory_region_size (memory); +assert (!pci_is_vf (pci_dev));/* VFs must use
pcie_sriov_vf_register_bar*/assert(region_num >= 0); assert(region_num < PCI_NUM_REGIONS); if (size & (size-1)) {hw/pci/pci.c +39
-4@@ -1060,11 +1085,44 @@ pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)return pci_dev->io_regions[region_num].addr; } -static pcibus_t pci_bar_address(PCIDevice *d, - int reg, uint8_t type, pcibus_t size) + +static pcibus_t pci_config_get_bar_addr(PCIDevice *d, int reg, +uint8_t type,pcibus_t size) +{ +pcibus_t new_addr; +if (!pci_is_vf (d)) { +int bar =pci_bar (d, reg); +if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { + new_addr =pci_get_quad (d->config + bar); + }else { + new_addr =pci_get_long (d->config + bar); + } + }else { +PCIDevice *pf = d->exp.sriov_vf.pf; +uint16_t sriov_cap = pf->exp.sriov_cap; +int bar = sriov_cap +PCI_SRIOV_BAR + reg *4 ; + uint16_t vf_offset = pci_get_word(pf->config + sriov_cap +PCI_SRIOV_VF_OFFSET ); + uint16_t vf_stride = pci_get_word(pf->config + sriov_cap +PCI_SRIOV_VF_STRIDE ); +uint32_t vf_num = (d->devfn - (pf->devfn + vf_offset)) / vf_stride; + +if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { + new_addr =pci_get_quad (pf->config + bar); + }else { + new_addr =pci_get_long (pf->config + bar); + } + new_addr += vf_num * size; + } +if (reg !=PCI_ROM_SLOT ) { +/* Preserve the rom enable bit */ + new_addr &= ~(size -1 ); + } +return new_addr; +} + +pcibus_t pci_bar_address (PCIDevice *d, +int reg,uint8_t type,pcibus_t size) {pcibus_t new_addr, last_addr; -int bar =pci_bar (d, reg);uint16_t cmd =pci_get_word (d->config +PCI_COMMAND );Object *machine =qdev_get_machine ();ObjectClass *oc =object_get_class (machine);@@ -1075,7 +1133,7 @@ static pcibus_t pci_bar_address(PCIDevice *d, if (!(cmd & PCI_COMMAND_IO)) {return PCI_BAR_UNMAPPED ; } - new_addr =pci_get_long (d->config + bar) & ~(size -1 ); + new_addr =pci_config_get_bar_addr (d, reg, type, size); last_addr = new_addr + size -1 ;/* Check if 32 bit BAR wraps around explicitly. * TODO: make priorities correct and remove this work
around.hw/pci/pci.c +1
-5@@ -1090,11 +1148,7 @@ static pcibus_t pci_bar_address(PCIDevice *d, if (!(cmd & PCI_COMMAND_MEMORY)) {return PCI_BAR_UNMAPPED ; } -if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { - new_addr =pci_get_quad (d->config + bar); - }else { - new_addr =pci_get_long (d->config + bar); - } + new_addr =pci_config_get_bar_addr (d, reg, type, size);/* the ROM slot has a specific enable bit */ if (reg == PCI_ROM_SLOT && !(new_addr &PCI_ROM_ADDRESS_ENABLE)) { return PCI_BAR_UNMAPPED;hw/pci/pci.c +0
-0@@ -1228,6 +1282,7 @@ void pci_default_write_config(PCIDevice *d,
uint32_taddr,uint32_tval_in, intmsi_write_config(d, addr, val_in, l); msix_write_config(d, addr, val_in, l);hw/pci/pci.c +1
-0+pcie_sriov_config_write (d, addr, val_in, l); }/***********************************************************/ hw/pci/pcie.c +0
-0@@ -253,7 +253,7 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev, * Right now, only a device of function = 0 is allowed tobe
* hot plugged/unplugged.
*/hw/pci/pcie.c +1
-1-assert (PCI_FUNC (pci_dev->devfn) ==0 ); +assert (PCI_FUNC (pci_dev->devfn) ==0 ||pci_is_vf (pci_dev));pci_word_test_and_set_mask (exp_cap +PCI_EXP_SLTSTA ,PCI_EXP_SLTSTA_PDS );@@ -265,10 +265,11 @@ void pcie_cap_slot_hot_unplug_request_cb(HotplugHandler *hotplug_dev, DeviceState *dev,Error
**errp){ uint8_t *exp_cap;hw/pci/pcie.c +1
-1+PCIDevice *pdev =PCI_DEVICE (hotplug_dev); - pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev), dev,
&exp_cap, errp);
+pcie_cap_slot_hotplug_common(pdev, dev, &exp_cap, errp);Quoted 7 lines
hw/pci/pcie.c void pcie_cap_slot_hot_unplug_request_cb(HotplugHandler *hotplug_dev, · line 273
Quoted 4 lines
hw/pci/pcie.c +1
-1-pcie_cap_slot_push_attention_button (PCI_DEVICE (hotplug_dev)); +pcie_cap_slot_push_attention_button (pdev); }This chunk is not directly liked to the patch, I would put it in a different patch.
ok
Quoted 5 lines
Quoted 5 lines
/* pci express slot for pci express root/downstream port
hw/pci/pcie.c +0
-0@@ -408,7 +409,7 @@ void pcie_cap_slot_write_config(PCIDevice *dev, }Quoted 14 lines
Quoted 9 lines
hw/pci/pcie.c +0
-1/* -* If the slot is polulated, power indicator is off and power
+ * If the slot is populated, power indicator is off and
power* controller is off, it is safe to detach the devices. */ if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val &
PCI_EXP_SLTCTL_PCC) &&Same here. I am always happy to have this kind of types taken care of,
but I think a separate patch would be cleaner.Would you like it in the patch set as a separate patch or should I schedule it for a trivial patches set instead, maybe together with the
PCI_DEVICEthing?Quoted 6 lines
Quoted 6 lines
hw/pci/pcie_sriov.c new file +0
-0@@ -0,0 +1,271 @@Quoted 8 lines
hw/pci/pcie_sriov.c line 6
Quoted 6 lines
hw/pci/pcie_sriov.c new file +6
-0+/* +* pcie_sriov.c: +* +* Implementation of SR/IOV emulation support. +* +* Copyright (c) 2014 Knut Omang <[email protected]> 2015 :)
thanks,
Quoted 51 lines
Quoted 51 lines
hw/pci/pcie_sriov.c new file +2
-0+* +* This work is licensed under the terms of the GNU GPL, version 2
or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */hw/pci/pcie_sriov.c new file +10
-0+ +#include "hw/pci/pci.h" +#include "hw/pci/pcie.h" +#include "hw/pci/pci_bus.h" +#include "qemu/range.h" + +//#define DEBUG_SRIOV +#ifdef DEBUG_SRIOV +# define SRIOV_DPRINTF (fmt, ...) \ + fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ##
__VA_ARGS__)hw/pci/pcie_sriov.c new file +5
-0+#else +# define SRIOV_DPRINTF (fmt, ...) do {} while (0) +#endif +#define SRIOV_DEV_PRINTF (dev, fmt, ...) \ + SRIOV_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ##
__VA_ARGS__)hw/pci/pcie_sriov.c new file +2
-0+ +static PCIDevice *register_vf(PCIDevice *pf, int devfn, constchar
*name);hw/pci/pcie_sriov.c new file +19
-0+static void unregister_vfs (PCIDevice *dev); + +void pcie_sriov_pf_init (PCIDevice *dev,uint16_t offset, +const char *vfname,uint16_t vf_dev_id, +uint16_t init_vfs,uint16_t total_vfs, +uint16_t vf_offset,uint16_t vf_stride) +{ +uint8_t *cfg = dev->config + offset; +uint8_t *wmask; + +pcie_add_capability (dev,PCI_EXT_CAP_ID_SRIOV ,1 , + offset,PCI_EXT_CAP_SRIOV_SIZEOF ); + dev->exp.sriov_cap = offset; + dev->exp.sriov_pf.num_vfs =0 ; + dev->exp.sriov_pf.vfname =g_strdup (vfname); + dev->exp.sriov_pf.vf =NULL ; + +pci_set_word (cfg +PCI_SRIOV_VF_OFFSET , vf_offset); +pci_set_word (cfg +PCI_SRIOV_VF_STRIDE , vf_stride);Quoted 8 lines
hw/pci/pcie_sriov.c line 52
Quoted 6 lines
hw/pci/pcie_sriov.c new file +2
-0+ +/* Minimal page size support values required by the SR/IOV standard:
hw/pci/pcie_sriov.c new file +3
-0+* 0x553 << 12 = 0x553000 = 4K + 8K + 64K + 256K + 1M + 4M +* Hard coded for simplicity in this version +*/ A define (with the above very good explanation) would be preferred.
Will do
Quoted 70 lines
Quoted 70 lines
hw/pci/pcie_sriov.c new file +8
-0+pci_set_word (cfg +PCI_SRIOV_SUP_PGSIZE ,0x553 ); + +/* Default is to use 4K pages, software can modify it +* to any of the supported bits +*/ +pci_set_word (cfg +PCI_SRIOV_SYS_PGSIZE ,0x1 ); + +/* Set up device ID and initial/total number of VFs available
*/hw/pci/pcie_sriov.c new file +9
-0+pci_set_word (cfg +PCI_SRIOV_VF_DID , vf_dev_id); +pci_set_word (cfg +PCI_SRIOV_INITIAL_VF , init_vfs); +pci_set_word (cfg +PCI_SRIOV_TOTAL_VF , total_vfs); +pci_set_word (cfg +PCI_SRIOV_NUM_VF ,0 ); + +/* Write enable control bits */ + wmask = dev->wmask + offset; + pci_set_word(wmask + PCI_SRIOV_CTRL, + PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE |
PCI_SRIOV_CTRL_ARI);hw/pci/pcie_sriov.c new file +50
-0+pci_set_word (wmask +PCI_SRIOV_NUM_VF ,0xffff ); +pci_set_word (wmask +PCI_SRIOV_SYS_PGSIZE ,0x553 ); + +qdev_prop_set_bit (&dev->qdev,"multifunction" , true); +} + +void pcie_sriov_pf_exit (PCIDevice *dev) +{ +SRIOV_DPRINTF ("\n" ); +unregister_vfs (dev); +g_free ((char *)dev->exp.sriov_pf.vfname); + dev->exp.sriov_pf.vfname =NULL ; +} + +void pcie_sriov_pf_init_vf_bar (PCIDevice *dev,int region_num, +uint8_t type,dma_addr_t size) +{ +uint32_t addr; +uint64_t wmask; +uint16_t sriov_cap = dev->exp.sriov_cap; + +assert (sriov_cap >0 ); +assert (region_num >=0 ); +assert (region_num <PCI_NUM_REGIONS ); +assert (region_num !=PCI_ROM_SLOT ); + + wmask = ~(size -1 ); + addr = sriov_cap +PCI_SRIOV_BAR + region_num *4 ; + +pci_set_long (dev->config + addr, type); +if (!(type &PCI_BASE_ADDRESS_SPACE_IO ) && + type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { +pci_set_quad (dev->wmask + addr, wmask); +pci_set_quad (dev->cmask + addr, ~0ULL ); + }else { +pci_set_long (dev->wmask + addr, wmask &0xffffffff ); +pci_set_long (dev->cmask + addr,0xffffffff ); + } + dev->exp.sriov_pf.vf_bar_type[region_num] = type; +} + +void pcie_sriov_vf_register_bar (PCIDevice *dev,int region_num, +MemoryRegion *memory) +{ +PCIIORegion *r; +uint8_t type; +pcibus_t size =memory_region_size (memory); + +assert (pci_is_vf (dev));/* PFs must use pci_register_bar */ +assert (region_num >=0 );Quoted 8 lines
hw/pci/pcie_sriov.c line 123
Quoted 5 lines
hw/pci/pcie_sriov.c new file +4
-1+assert (region_num <PCI_NUM_REGIONS ); + type = dev->exp.sriov_vf.pf ->exp.sriov_pf.vf_bar_type[region_num]; + +if (size & (size-1 )) {We already have a
is_power_of_2func defined ininclude/qemu/host-utils.hwill fix,
Quoted 5 lines
Quoted 3 lines
hw/pci/pcie_sriov.c new file +2
-0+ fprintf(stderr, "ERROR: PCI region size must be a power" + " of two - type=0x%x, size=0x%"FMT_PCIBUS"\n",type, size);
Maybe we should
error_reportinstead of fprintfLeftover from far too many rebases, will fix,
Quoted 30 lines
Quoted 30 lines
hw/pci/pcie_sriov.c new file +16
-1+exit (1 ); + } + + r = &dev->io_regions[region_num]; + r->memory = memory; + r->address_space = + type &PCI_BASE_ADDRESS_SPACE_IO + ? dev->bus->address_space_io + : dev->bus->address_space_mem; + r->size = size; + r->type = type; + + r->addr = pci_bar_address(dev, region_num, r->type, r ->size); + if (r->addr != PCI_BAR_UNMAPPED) { + memory_region_add_subregion_overlap(r->address_space, + r->addr, r->memory,1);
hw/pci/pcie_sriov.c new file +4
-0+ } +} + +static PCIDevice *register_vf(PCIDevice *pf, int devfn, constchar
*name)hw/pci/pcie_sriov.c new file +6
-0+{ +PCIDevice *dev =pci_create (pf->bus, devfn, name); + dev->exp.sriov_vf.pf = pf; +Error *local_err =NULL ; + + object_property_set_bool(OBJECT(&dev->qdev), true,Quoted 8 lines
hw/pci/pcie_sriov.c line 155
Quoted 6 lines
"realized",
&local_err);hw/pci/pcie_sriov.c new file +4
-0+if (local_err) { +fprintf (stderr,"Failed to enable: %s\n" , +error_get_pretty (local_err)); +error_free (local_err);and
error_report_errhereok
Quoted 159 lines
Quoted 53 lines
hw/pci/pcie_sriov.c new file +4
-0+return NULL ; + } + +/* set vid/did according to sr/iov spec - they are not used */
hw/pci/pcie_sriov.c new file +11
-0+pci_config_set_vendor_id (dev->config,0xffff ); +pci_config_set_device_id (dev->config,0xffff ); +return dev; +} + +static void register_vfs (PCIDevice *dev) +{ +uint16_t num_vfs; +uint16_t i; +uint16_t sriov_cap = dev->exp.sriov_cap; + uint16_t vf_offset = pci_get_word(dev->config + sriov_cap +
PCI_SRIOV_VF_OFFSET);
+uint16_tvf_stride= pci_get_word(dev->config +sriov_cap+PCI_SRIOV_VF_STRIDE);hw/pci/pcie_sriov.c new file +4
-0+int32_t devfn = dev->devfn + vf_offset; + +assert (sriov_cap >0 ); + num_vfs = pci_get_word(dev->config + sriov_cap +
PCI_SRIOV_NUM_VF);hw/pci/pcie_sriov.c new file +2
-0+ + dev->exp.sriov_pf.vf = g_malloc(sizeof(PCIDevice *) *
num_vfs);hw/pci/pcie_sriov.c new file +7
-1+ assert(dev->exp.sriov_pf.vf); + + SRIOV_DEV_PRINTF(dev, "creating %d vf devs\n", num_vfs); + for (i = 0; i < num_vfs; i++) { + dev->exp.sriov_pf.vf[i] = register_vf(dev, devfn, dev ->exp.sriov_pf.vfname); + if (!dev->exp.sriov_pf.vf[i]) { + SRIOV_DEV_PRINTF(dev, "Failed to create VF %d atdevfn
%x.%x\n", i,hw/pci/pcie_sriov.c new file +15
-0+ PCI_SLOT(devfn), PCI_FUNC(devfn)); + num_vfs = i; +break ; + } + devfn += vf_stride; + } + dev->exp.sriov_pf.num_vfs = num_vfs; +} + +static void unregister_vfs (PCIDevice *dev) +{ +Error *local_err =NULL ; +uint16_t num_vfs = dev->exp.sriov_pf.num_vfs; +uint16_t i; + SRIOV_DEV_PRINTF(dev, "Unregistering %d vf devs\n",
num_vfs);hw/pci/pcie_sriov.c line 205
Quoted 7 lines
hw/pci/pcie_sriov.c new file +6
-1+for (i =0 ; i < num_vfs; i++) { + object_property_set_bool(OBJECT(&dev->exp.sriov_pf.vf[i] ->qdev), false, "realized", &local_err); + if (local_err) { + fprintf(stderr, "Failed to unplug: %s\n", + error_get_pretty(local_err)); + error_free(local_err);
error_report_errQuoted 96 lines
hw/pci/pcie_sriov.c new file +8
-0+ } + } +g_free (dev->exp.sriov_pf.vf); + dev->exp.sriov_pf.vf =NULL ; + dev->exp.sriov_pf.num_vfs =0 ; +} + +void pcie_sriov_config_write(PCIDevice *dev, uint32_t address,
uint32_tval, int len)hw/pci/pcie_sriov.c new file +13
-0+{ +uint32_t off; +uint16_t sriov_cap = dev->exp.sriov_cap; + +if (!sriov_cap || address < sriov_cap) { +return ; + } + off = address - sriov_cap; +if (off >=PCI_EXT_CAP_SRIOV_SIZEOF ) { +return ; + } + + SRIOV_DEV_PRINTF(dev, "cap at 0x%x sriov offset 0x%x val0x%x
len %d\n",hw/pci/pcie_sriov.c new file +17
-0+ sriov_cap, off, val, len); + +if (range_covers_byte (off, len,PCI_SRIOV_CTRL )) { +if (dev->exp.sriov_pf.num_vfs) { +if (!(val &PCI_SRIOV_CTRL_VFE )) { +unregister_vfs (dev); + } + }else { +if (val &PCI_SRIOV_CTRL_VFE ) { +register_vfs (dev); + } + } + } +} + + +/* Reset SR/IOV VF Enable bit to trigger an unregister of all VFs
*/hw/pci/pcie_sriov.c new file +5
-0+void pcie_sriov_pf_disable_vfs (PCIDevice *dev) +{ +uint16_t sriov_cap = dev->exp.sriov_cap; +if (sriov_cap) { + uint32_t val = pci_get_byte(dev->config + sriov_cap +
PCI_SRIOV_CTRL);hw/pci/pcie_sriov.c new file +3
-0+if (val &PCI_SRIOV_CTRL_VFE ) { + val &= ~PCI_SRIOV_CTRL_VFE ; + pcie_sriov_config_write(dev, sriov_cap +
PCI_SRIOV_CTRL, val, 1);hw/pci/pcie_sriov.c new file +5
-0+ } + } +} + +/* Add optional supported page sizes to the mask of supported page
sizes */
+voidpcie_sriov_pf_add_sup_pgsize(PCIDevice *dev, uint16_t opt_sup_pgsize)hw/pci/pcie_sriov.c new file +5
-0+{ +uint8_t *cfg = dev->config + dev->exp.sriov_cap; +uint8_t *wmask = dev->wmask + dev->exp.sriov_cap; + + uint16_t sup_pgsize = pci_get_word(cfg +
PCI_SRIOV_SUP_PGSIZE);hw/pci/pcie_sriov.c new file +5
-0+ + sup_pgsize |= opt_sup_pgsize; + +/* Make sure the new bits are set, and that system page size +* also can be set to any of the new values according to spec:
hw/pci/pcie_sriov.c new file +4
-0+*/ +pci_set_word (cfg +PCI_SRIOV_SUP_PGSIZE , sup_pgsize); +pci_set_word (wmask +PCI_SRIOV_SYS_PGSIZE , sup_pgsize); +}include/hw/pci/pci.h +0
-2@@ -11,8 +11,6 @@/* PCI includes legacy ISA access. */ #include "hw/isa/isa.h" -#include "hw/pci/pcie.h" -/* PCI bus */ #define PCI_DEVFN (slot, func) ((((slot) & 0x1f) << 3) |((func)
&0x07))include/hw/pci/pci.h +0
-0@@ -132,6 +130,7 @@ enum {#define QEMU_PCI_VGA_IO_HI_SIZE 0x20
Quoted 6 lines
Quoted 4 lines
include/hw/pci/pci.h enum { · line 133
Quoted 2 lines
include/hw/pci/pci.h +1
-0#include "hw/pci/pci_regs.h" +#include "hw/pci/pcie.h" Why is it moved here?
Probably an unintended side effect of rebasing..
Actually it is due to the need for PCI_NUM_REGIONS (declared above in pci.h now) to declare the PCIESriovPF data type in pcie_sriov.h, which I think should naturally be included by pcie.h similar to pcie_aer.h
Knut
Quoted 159 lines
Quoted 141 lines
Quoted 61 lines
include/hw/pci/pci.h +3
-0/* PCI HEADER_TYPE */ #define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80@@ -421,6 +420,9 @@ typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int);AddressSpace *pci_device_iommu_address_space (PCIDevice *dev);void pci_setup_iommu (PCIBus *bus,PCIIOMMUFunc fn,void *opaque); +pcibus_t pci_bar_address (PCIDevice *d, +int reg,uint8_t type,pcibus_t size); +static inline void pci_set_byte(uint8_t *config, uint8_t val) {include/hw/pci/pci.h +5
-0@@ -672,6 +674,11 @@ static inline int pci_is_express(const PCIDevice *d)return d->cap_present &QEMU_PCI_CAP_EXPRESS ; } +static inline int pci_is_vf (const PCIDevice *d) +{ +return d->exp.sriov_vf.pf !=NULL ; +} +static inline uint32_t pci_config_size (const PCIDevice *d) {return
pci_is_express(d)?PCIE_CONFIG_SPACE_SIZE:PCI_CONFIG_SPACE_SIZE;include/hw/pci/pcie.h +6
-0@@ -25,6 +25,7 @@#include "hw/pci/pci_regs.h" #include "hw/pci/pcie_regs.h" #include "hw/pci/pcie_aer.h" +#include "hw/pci/pcie_sriov.h" #include "hw/hotplug.h" typedef enum {@@ -74,6 +75,11 @@ struct PCIExpressDevice {/* AER */ uint16_t aer_cap;PCIEAERLog aer_log; + +/* SR/IOV */ +uint16_t sriov_cap; +PCIESriovPF sriov_pf; +PCIESriovVF sriov_vf; };#define COMPAT_PROP_PCP "power_controller_present"include/hw/pci/pcie_sriov.h new file +0
-0include/hw/pci/pcie_sriov.h line 6
Quoted 6 lines
include/hw/pci/pcie_sriov.h new file +6
-0@@ -0,0 +1,55 @@ +/* +* pcie_sriov.h: +* +* Implementation of SR/IOV emulation support. +* +* Copyright (c) 2014 Knut Omang <[email protected]> 2015
Quoted 71 lines
include/hw/pci/pcie_sriov.h new file +2
-0+* +* This work is licensed under the terms of the GNU GPL, version 2
or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */include/hw/pci/pcie_sriov.h new file +8
-0+ +#ifndef QEMU_PCIE_SRIOV_H +#define QEMU_PCIE_SRIOV_H + + + +struct PCIESriovPF { + uint16_t num_vfs; /* Number of virtual functionscreated */
+uint8_tvf_bar_type[PCI_NUM_REGIONS]; /* Store type for
each
VF bar */
+ const char *vfname; /* Reference to the device type
used for the VFs */
+ PCIDevice **vf; /* Pointer to an array ofnum_vfs
VF devices */include/hw/pci/pcie_sriov.h new file +4
-0+}; + +struct PCIESriovVF { +PCIDevice *pf;/* Pointer back to owner physical
function */include/hw/pci/pcie_sriov.h new file +17
-0+}; + +void pcie_sriov_pf_init (PCIDevice *dev,uint16_t offset, +const char *vfname,uint16_t vf_dev_id, +uint16_t init_vfs,uint16_t total_vfs, +uint16_t vf_offset,uint16_t vf_stride); +void pcie_sriov_pf_exit (PCIDevice *dev); + +/* Set up a VF bar in the SR/IOV bar area */ +void pcie_sriov_pf_init_vf_bar (PCIDevice *dev,int region_num, +uint8_t type,dma_addr_t size); + +/* Instantiate a bar for a VF */ +void pcie_sriov_vf_register_bar (PCIDevice *dev,int region_num, +MemoryRegion *memory); + +/* Add optional supported page sizes to the mask of supported page
sizes
+ * Page size values are interpreted asopt_sup_pgsize<< 12.
+ */
+voidpcie_sriov_pf_add_sup_pgsize(PCIDevice *dev, uint16_t opt_sup_pgsize);include/hw/pci/pcie_sriov.h new file +9
-0+ +/* SR/IOV capability config write handler */ +void pcie_sriov_config_write (PCIDevice *dev,uint32_t address, +uint32_t val,int len); + +/* Reset SR/IOV VF Enable bit to unregister all VFs */ +void pcie_sriov_pf_disable_vfs (PCIDevice *dev); + +#endif /* QEMU_PCIE_SRIOV_H */ include/qemu/typedefs.h +1
-0@@ -53,6 +53,8 @@ typedef struct PCIDevice PCIDevice;typedef struct PCIEAERErr PCIEAERErr ;typedef struct PCIEAERLog PCIEAERLog ;typedef struct PCIEAERMsg PCIEAERMsg ; +typedef struct PCIESriovPF PCIESriovPF ;Quoted 8 lines
include/qemu/typedefs.h typedef struct PCIDevice PCIDevice; · line 60
Quoted 5 lines
include/qemu/typedefs.h +1
-0+typedef struct PCIESriovVF PCIESriovVF ;typedef struct PCIEPort PCIEPort ;typedef struct PCIESlot PCIESlot ;typedef struct PCIExpressDevice PCIExpressDevice ;I hope I helped,
Definitely,
thanks for the review!Knut
Quoted 3 lines
Thanks,
Marcel
Re: [Qemu-devel] [PATCH v4 3/3] pcie: Add support for Single Root I/O Virtualization (SR/IOV)
In reply to Knut OmangQuoted 649 lines · Knut Omang
This patch provides the building blocks for creating an SR/IOV PCIe Extended Capability header and register/unregister SR/IOV Virtual Functions.
Signed-off-by Knut Omang <[email protected]>--- hw/pci/Makefile.objs | 2 +- hw/pci/pci.c | 99 ++++++++++++---- hw/pci/pcie.c | 9 +- hw/pci/pcie_sriov.c | 271 ++++++++++++++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 11 +- include/hw/pci/pcie.h | 6 + include/hw/pci/pcie_sriov.h | 55 +++++++++ include/qemu/typedefs.h | 2 + 8 files changed, 426 insertions(+), 29 deletions(-)create mode 100644
hw/pci/pcie_sriov.c
create mode 100644include/hw/pci/pcie_sriov.hhw/pci/Makefile.objs +1
-1@@ -3,7 +3,7 @@ common-obj-$(CONFIG_PCI) += msix.o msi.o common-obj-$(CONFIG_PCI) += shpc.o common-obj-$(CONFIG_PCI) += slotid_cap.o common-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o -common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o +common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o pcie_sriov.o common-obj-$(call lnot,$(CONFIG_PCI)) += pci-stub.o common-obj-$(CONFIG_ALL) += pci-stub.ohw/pci/pci.c +77
-22@@ -153,6 +153,9 @@ int pci_bar(PCIDevice *d, int reg) {uint8_t type; +/* PCIe virtual functions do not have their own BARs */ +assert (!pci_is_vf (d)); +if (reg !=PCI_ROM_SLOT )return PCI_BASE_ADDRESS_0 + reg *4 ;@@ -211,22 +214,13 @@ void pci_device_deassert_intx(PCIDevice *dev) } } -static void pci_do_device_reset (PCIDevice *dev) +static void pci_reset_regions (PCIDevice *dev) {int r; +if (pci_is_vf (dev)) { +return ; + } -pci_device_deassert_intx (dev); -assert (dev->irq_state ==0 ); - -/* Clear all writable bits */ -pci_word_test_and_clear_mask (dev->config +PCI_COMMAND , -pci_get_word (dev->wmask +PCI_COMMAND ) | -pci_get_word (dev->w1cmask +PCI_COMMAND )); -pci_word_test_and_clear_mask (dev->config +PCI_STATUS , -pci_get_word (dev->wmask +PCI_STATUS ) | -pci_get_word (dev->w1cmask +PCI_STATUS )); - dev->config[PCI_CACHE_LINE_SIZE ] =0x0 ; - dev->config[PCI_INTERRUPT_LINE ] =0x0 ;for (r =0 ; r <PCI_NUM_REGIONS ; ++r) {PCIIORegion *region = &dev->io_regions[r];if (!region->size) {@@ -240,6 +234,27 @@ static void pci_do_device_reset(PCIDevice *dev)pci_set_long (dev->config +pci_bar (dev, r), region->type); } } +} + +static void pci_do_device_reset (PCIDevice *dev) +{ +qdev_reset_all (&dev->qdev); + + dev->irq_state =0 ; +pci_update_irq_status (dev); +pci_device_deassert_intx (dev); +assert (dev->irq_state ==0 ); + +/* Clear all writable bits */ +pci_word_test_and_clear_mask (dev->config +PCI_COMMAND , +pci_get_word (dev->wmask +PCI_COMMAND ) | +pci_get_word (dev->w1cmask +PCI_COMMAND )); +pci_word_test_and_clear_mask (dev->config +PCI_STATUS , +pci_get_word (dev->wmask +PCI_STATUS ) | +pci_get_word (dev->w1cmask +PCI_STATUS )); + dev->config[PCI_CACHE_LINE_SIZE ] =0x0 ; + dev->config[PCI_INTERRUPT_LINE ] =0x0 ; +pci_reset_regions (dev);pci_update_mappings (dev);msi_reset (dev);@@ -771,6 +786,15 @@ static void pci_init_multifunction(PCIBus *bus, PCIDevice *dev, Error **errp) dev->config[PCI_HEADER_TYPE ] |=PCI_HEADER_TYPE_MULTI_FUNCTION ; } +/* With SR/IOV and ARI, a device at function 0 need not be a multifunction +* device, as it may just be a VF that ended up with function 0 in +* the legacy PCI interpretation. Avoid failing in such cases: +*/ +if (pci_is_vf (dev) && + dev->exp.sriov_vf.pf->cap_present &QEMU_PCI_CAP_MULTIFUNCTION ) { +return ; + } +/* * multifunction bit is interpreted in two ways as follows. * - all functions must set the bit to 1. @@ -962,6 +986,7 @@ void pci_register_bar(PCIDevice *pci_dev, int region_num,uint64_t wmask;pcibus_t size =memory_region_size (memory); +assert (!pci_is_vf (pci_dev));/* VFs must use pcie_sriov_vf_register_bar */ assert (region_num >=0 );assert (region_num <PCI_NUM_REGIONS );if (size & (size-1 )) {@@ -1060,11 +1085,44 @@ pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)return pci_dev->io_regions[region_num].addr; } -static pcibus_t pci_bar_address (PCIDevice *d, -int reg,uint8_t type,pcibus_t size) + +static pcibus_t pci_config_get_bar_addr (PCIDevice *d,int reg, +uint8_t type,pcibus_t size) +{ +pcibus_t new_addr; +if (!pci_is_vf (d)) { +int bar =pci_bar (d, reg); +if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { + new_addr =pci_get_quad (d->config + bar); + }else { + new_addr =pci_get_long (d->config + bar); + } + }else { +PCIDevice *pf = d->exp.sriov_vf.pf; +uint16_t sriov_cap = pf->exp.sriov_cap; +int bar = sriov_cap +PCI_SRIOV_BAR + reg *4 ; +uint16_t vf_offset =pci_get_word (pf->config + sriov_cap +PCI_SRIOV_VF_OFFSET ); +uint16_t vf_stride =pci_get_word (pf->config + sriov_cap +PCI_SRIOV_VF_STRIDE ); +uint32_t vf_num = (d->devfn - (pf->devfn + vf_offset)) / vf_stride; + +if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { + new_addr =pci_get_quad (pf->config + bar); + }else { + new_addr =pci_get_long (pf->config + bar); + } + new_addr += vf_num * size; + } +if (reg !=PCI_ROM_SLOT ) { +/* Preserve the rom enable bit */ + new_addr &= ~(size -1 ); + } +return new_addr; +} + +pcibus_t pci_bar_address (PCIDevice *d, +int reg,uint8_t type,pcibus_t size) {pcibus_t new_addr, last_addr; -int bar =pci_bar (d, reg);uint16_t cmd =pci_get_word (d->config +PCI_COMMAND );Object *machine =qdev_get_machine ();ObjectClass *oc =object_get_class (machine);@@ -1075,7 +1133,7 @@ static pcibus_t pci_bar_address(PCIDevice *d,if (!(cmd &PCI_COMMAND_IO )) {return PCI_BAR_UNMAPPED ; } - new_addr =pci_get_long (d->config + bar) & ~(size -1 ); + new_addr =pci_config_get_bar_addr (d, reg, type, size); last_addr = new_addr + size -1 ;/* Check if 32 bit BAR wraps around explicitly. * TODO: make priorities correct and remove this work around. @@ -1090,11 +1148,7 @@ static pcibus_t pci_bar_address(PCIDevice *d,if (!(cmd &PCI_COMMAND_MEMORY )) {return PCI_BAR_UNMAPPED ; } -if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { - new_addr =pci_get_quad (d->config + bar); - }else { - new_addr =pci_get_long (d->config + bar); - } + new_addr =pci_config_get_bar_addr (d, reg, type, size);/* the ROM slot has a specific enable bit */ if (reg ==PCI_ROM_SLOT && !(new_addr &PCI_ROM_ADDRESS_ENABLE )) {return PCI_BAR_UNMAPPED ;@@ -1228,6 +1282,7 @@ void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val_in, intmsi_write_config (d, addr, val_in, l);msix_write_config (d, addr, val_in, l); +pcie_sriov_config_write (d, addr, val_in, l); }/***********************************************************/ hw/pci/pcie.c +5
-4@@ -253,7 +253,7 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,* Right now, only a device of function = 0 is allowed to be * hot plugged/unplugged. */ -assert (PCI_FUNC (pci_dev->devfn) ==0 ); +assert (PCI_FUNC (pci_dev->devfn) ==0 ||pci_is_vf (pci_dev));pci_word_test_and_set_mask (exp_cap +PCI_EXP_SLTSTA ,PCI_EXP_SLTSTA_PDS );@@ -265,10 +265,11 @@ void pcie_cap_slot_hot_unplug_request_cb(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp) {uint8_t *exp_cap; +PCIDevice *pdev =PCI_DEVICE (hotplug_dev); -pcie_cap_slot_hotplug_common (PCI_DEVICE (hotplug_dev), dev, &exp_cap, errp); +pcie_cap_slot_hotplug_common (pdev, dev, &exp_cap, errp); -pcie_cap_slot_push_attention_button (PCI_DEVICE (hotplug_dev)); +pcie_cap_slot_push_attention_button (pdev); }/* pci express slot for pci express root/downstream port @@ -408,7 +409,7 @@ void pcie_cap_slot_write_config(PCIDevice *dev, }/* -* If the slot is polulated, power indicator is off and power +* If the slot is populated, power indicator is off and power * controller is off, it is safe to detach the devices. */ if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val & PCI_EXP_SLTCTL_PCC) &&hw/pci/pcie_sriov.c new file +271
-0@@ -0,0 +1,271 @@ +/* +* pcie_sriov.c: +* +* Implementation of SR/IOV emulation support. +* +* Copyright (c) 2014 Knut Omang <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#include "hw/pci/pci.h" +#include "hw/pci/pcie.h" +#include "hw/pci/pci_bus.h" +#include "qemu/range.h" + +//#define DEBUG_SRIOV +#ifdef DEBUG_SRIOV +# define SRIOV_DPRINTF (fmt, ...) \ + fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__) +#else +# define SRIOV_DPRINTF (fmt, ...) do {} while (0) +#endif +#define SRIOV_DEV_PRINTF (dev, fmt, ...) \ + SRIOV_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__) + +static PCIDevice *register_vf (PCIDevice *pf,int devfn,const char *name); +static void unregister_vfs (PCIDevice *dev); + +void pcie_sriov_pf_init (PCIDevice *dev,uint16_t offset, +const char *vfname,uint16_t vf_dev_id, +uint16_t init_vfs,uint16_t total_vfs, +uint16_t vf_offset,uint16_t vf_stride) +{ +uint8_t *cfg = dev->config + offset; +uint8_t *wmask; + +pcie_add_capability (dev,PCI_EXT_CAP_ID_SRIOV ,1 , + offset,PCI_EXT_CAP_SRIOV_SIZEOF ); + dev->exp.sriov_cap = offset; + dev->exp.sriov_pf.num_vfs =0 ; + dev->exp.sriov_pf.vfname =g_strdup (vfname); + dev->exp.sriov_pf.vf =NULL ; + +pci_set_word (cfg +PCI_SRIOV_VF_OFFSET , vf_offset); +pci_set_word (cfg +PCI_SRIOV_VF_STRIDE , vf_stride); + +/* Minimal page size support values required by the SR/IOV standard: +* 0x553 << 12 = 0x553000 = 4K + 8K + 64K + 256K + 1M + 4M +* Hard coded for simplicity in this version +*/ +pci_set_word (cfg +PCI_SRIOV_SUP_PGSIZE ,0x553 ); + +/* Default is to use 4K pages, software can modify it +* to any of the supported bits +*/ +pci_set_word (cfg +PCI_SRIOV_SYS_PGSIZE ,0x1 ); + +/* Set up device ID and initial/total number of VFs available */ +pci_set_word (cfg +PCI_SRIOV_VF_DID , vf_dev_id); +pci_set_word (cfg +PCI_SRIOV_INITIAL_VF , init_vfs); +pci_set_word (cfg +PCI_SRIOV_TOTAL_VF , total_vfs); +pci_set_word (cfg +PCI_SRIOV_NUM_VF ,0 ); + +/* Write enable control bits */ + wmask = dev->wmask + offset; +pci_set_word (wmask +PCI_SRIOV_CTRL , +PCI_SRIOV_CTRL_VFE |PCI_SRIOV_CTRL_MSE |PCI_SRIOV_CTRL_ARI ); +pci_set_word (wmask +PCI_SRIOV_NUM_VF ,0xffff ); +pci_set_word (wmask +PCI_SRIOV_SYS_PGSIZE ,0x553 ); + +qdev_prop_set_bit (&dev->qdev,"multifunction" , true); +} + +void pcie_sriov_pf_exit (PCIDevice *dev) +{ +SRIOV_DPRINTF ("\n" ); +unregister_vfs (dev); +g_free ((char *)dev->exp.sriov_pf.vfname); + dev->exp.sriov_pf.vfname =NULL ; +} + +void pcie_sriov_pf_init_vf_bar (PCIDevice *dev,int region_num, +uint8_t type,dma_addr_t size) +{ +uint32_t addr; +uint64_t wmask; +uint16_t sriov_cap = dev->exp.sriov_cap; + +assert (sriov_cap >0 ); +assert (region_num >=0 ); +assert (region_num <PCI_NUM_REGIONS ); +assert (region_num !=PCI_ROM_SLOT ); + + wmask = ~(size -1 ); + addr = sriov_cap +PCI_SRIOV_BAR + region_num *4 ; + +pci_set_long (dev->config + addr, type); +if (!(type &PCI_BASE_ADDRESS_SPACE_IO ) && + type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { +pci_set_quad (dev->wmask + addr, wmask); +pci_set_quad (dev->cmask + addr, ~0ULL ); + }else { +pci_set_long (dev->wmask + addr, wmask &0xffffffff ); +pci_set_long (dev->cmask + addr,0xffffffff ); + } + dev->exp.sriov_pf.vf_bar_type[region_num] = type; +} + +void pcie_sriov_vf_register_bar (PCIDevice *dev,int region_num, +MemoryRegion *memory) +{ +PCIIORegion *r; +uint8_t type; +pcibus_t size =memory_region_size (memory); + +assert (pci_is_vf (dev));/* PFs must use pci_register_bar */ +assert (region_num >=0 ); +assert (region_num <PCI_NUM_REGIONS ); + type = dev->exp.sriov_vf.pf->exp.sriov_pf.vf_bar_type[region_num]; + +if (size & (size-1 )) { +fprintf (stderr,"ERROR: PCI region size must be a power" +" of two - type=0x%x, size=0x%" FMT_PCIBUS "\n" , type, size); +exit (1 ); + } + + r = &dev->io_regions[region_num]; + r->memory = memory; + r->address_space = + type &PCI_BASE_ADDRESS_SPACE_IO + ? dev->bus->address_space_io + : dev->bus->address_space_mem; + r->size = size; + r->type = type; + + r->addr =pci_bar_address (dev, region_num, r->type, r->size); +if (r->addr !=PCI_BAR_UNMAPPED ) { +memory_region_add_subregion_overlap (r->address_space, + r->addr, r->memory,1 ); + } +} + +static PCIDevice *register_vf (PCIDevice *pf,int devfn,const char *name) +{ +PCIDevice *dev =pci_create (pf->bus, devfn, name); + dev->exp.sriov_vf.pf = pf; +Error *local_err =NULL ; + +object_property_set_bool (OBJECT (&dev->qdev), true,"realized" , &local_err); +if (local_err) { +fprintf (stderr,"Failed to enable: %s\n" , +error_get_pretty (local_err)); +error_free (local_err); +return NULL ; + } + +/* set vid/did according to sr/iov spec - they are not used */ +pci_config_set_vendor_id (dev->config,0xffff ); +pci_config_set_device_id (dev->config,0xffff ); +return dev; +} + +static void register_vfs (PCIDevice *dev) +{ +uint16_t num_vfs; +uint16_t i; +uint16_t sriov_cap = dev->exp.sriov_cap; +uint16_t vf_offset =pci_get_word (dev->config + sriov_cap +PCI_SRIOV_VF_OFFSET ); +uint16_t vf_stride =pci_get_word (dev->config + sriov_cap +PCI_SRIOV_VF_STRIDE ); +int32_t devfn = dev->devfn + vf_offset; + +assert (sriov_cap >0 ); + num_vfs =pci_get_word (dev->config + sriov_cap +PCI_SRIOV_NUM_VF ); + + dev->exp.sriov_pf.vf =g_malloc (sizeof (PCIDevice *) * num_vfs); +assert (dev->exp.sriov_pf.vf); + +SRIOV_DEV_PRINTF (dev,"creating %d vf devs\n" , num_vfs); +for (i =0 ; i < num_vfs; i++) { + dev->exp.sriov_pf.vf[i] =register_vf (dev, devfn, dev->exp.sriov_pf.vfname); +if (!dev->exp.sriov_pf.vf[i]) { +SRIOV_DEV_PRINTF (dev,"Failed to create VF %d at devfn %x.%x\n" , i, +PCI_SLOT (devfn),PCI_FUNC (devfn)); + num_vfs = i; +break ; + } + devfn += vf_stride; + } + dev->exp.sriov_pf.num_vfs = num_vfs; +} + +static void unregister_vfs (PCIDevice *dev) +{ +Error *local_err =NULL ; +uint16_t num_vfs = dev->exp.sriov_pf.num_vfs; +uint16_t i; +SRIOV_DEV_PRINTF (dev,"Unregistering %d vf devs\n" , num_vfs); +for (i =0 ; i < num_vfs; i++) { +object_property_set_bool (OBJECT (&dev->exp.sriov_pf.vf[i]->qdev), false,"realized" , &local_err); +if (local_err) { +fprintf (stderr,"Failed to unplug: %s\n" , +error_get_pretty (local_err)); +error_free (local_err); + } + } +g_free (dev->exp.sriov_pf.vf); + dev->exp.sriov_pf.vf =NULL ; + dev->exp.sriov_pf.num_vfs =0 ; +} + +void pcie_sriov_config_write (PCIDevice *dev,uint32_t address,uint32_t val,int len) +{ +uint32_t off; +uint16_t sriov_cap = dev->exp.sriov_cap; + +if (!sriov_cap || address < sriov_cap) { +return ; + } + off = address - sriov_cap; +if (off >=PCI_EXT_CAP_SRIOV_SIZEOF ) { +return ; + } + +SRIOV_DEV_PRINTF (dev,"cap at 0x%x sriov offset 0x%x val 0x%x len %d\n" , + sriov_cap, off, val, len); + +if (range_covers_byte (off, len,PCI_SRIOV_CTRL )) { +if (dev->exp.sriov_pf.num_vfs) { +if (!(val &PCI_SRIOV_CTRL_VFE )) { +unregister_vfs (dev); + } + }else { +if (val &PCI_SRIOV_CTRL_VFE ) { +register_vfs (dev); + } + } + } +} + + +/* Reset SR/IOV VF Enable bit to trigger an unregister of all VFs */ +void pcie_sriov_pf_disable_vfs (PCIDevice *dev) +{ +uint16_t sriov_cap = dev->exp.sriov_cap; +if (sriov_cap) { +uint32_t val =pci_get_byte (dev->config + sriov_cap +PCI_SRIOV_CTRL ); +if (val &PCI_SRIOV_CTRL_VFE ) { + val &= ~PCI_SRIOV_CTRL_VFE ; +pcie_sriov_config_write (dev, sriov_cap +PCI_SRIOV_CTRL , val,1 ); + } + } +} + +/* Add optional supported page sizes to the mask of supported page sizes */ +void pcie_sriov_pf_add_sup_pgsize (PCIDevice *dev,uint16_t opt_sup_pgsize) +{ +uint8_t *cfg = dev->config + dev->exp.sriov_cap; +uint8_t *wmask = dev->wmask + dev->exp.sriov_cap; + +uint16_t sup_pgsize =pci_get_word (cfg +PCI_SRIOV_SUP_PGSIZE ); + + sup_pgsize |= opt_sup_pgsize; + +/* Make sure the new bits are set, and that system page size +* also can be set to any of the new values according to spec: +*/ +pci_set_word (cfg +PCI_SRIOV_SUP_PGSIZE , sup_pgsize); +pci_set_word (wmask +PCI_SRIOV_SYS_PGSIZE , sup_pgsize); +}include/hw/pci/pci.h +9
-2@@ -11,8 +11,6 @@/* PCI includes legacy ISA access. */ #include "hw/isa/isa.h" -#include "hw/pci/pcie.h" -/* PCI bus */ #define PCI_DEVFN (slot, func) ((((slot) & 0x1f) << 3) | ((func) & 0x07))@@ -132,6 +130,7 @@ enum {#define QEMU_PCI_VGA_IO_HI_SIZE 0x20#include "hw/pci/pci_regs.h" +#include "hw/pci/pcie.h" /* PCI HEADER_TYPE */ #define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80@@ -421,6 +420,9 @@ typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int);AddressSpace *pci_device_iommu_address_space (PCIDevice *dev);void pci_setup_iommu (PCIBus *bus,PCIIOMMUFunc fn,void *opaque); +pcibus_t pci_bar_address (PCIDevice *d, +int reg,uint8_t type,pcibus_t size); +static inline void pci_set_byte (uint8_t *config,uint8_t val) {@@ -672,6 +674,11 @@ static inline int pci_is_express(const PCIDevice *d)return d->cap_present &QEMU_PCI_CAP_EXPRESS ; } +static inline int pci_is_vf (const PCIDevice *d) +{ +return d->exp.sriov_vf.pf !=NULL ; +} +static inline uint32_t pci_config_size (const PCIDevice *d) {return pci_is_express (d) ?PCIE_CONFIG_SPACE_SIZE :PCI_CONFIG_SPACE_SIZE ;include/hw/pci/pcie.h +6
-0@@ -25,6 +25,7 @@#include "hw/pci/pci_regs.h" #include "hw/pci/pcie_regs.h" #include "hw/pci/pcie_aer.h" +#include "hw/pci/pcie_sriov.h" #include "hw/hotplug.h" typedef enum {@@ -74,6 +75,11 @@ struct PCIExpressDevice {/* AER */ uint16_t aer_cap;PCIEAERLog aer_log; + +/* SR/IOV */ +uint16_t sriov_cap; +PCIESriovPF sriov_pf; +PCIESriovVF sriov_vf; };#define COMPAT_PROP_PCP "power_controller_present"include/hw/pci/pcie_sriov.h new file +55
-0@@ -0,0 +1,55 @@ +/* +* pcie_sriov.h: +* +* Implementation of SR/IOV emulation support. +* +* Copyright (c) 2014 Knut Omang <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef QEMU_PCIE_SRIOV_H +#define QEMU_PCIE_SRIOV_H + + + +struct PCIESriovPF { +uint16_t num_vfs;/* Number of virtual functions created */ +uint8_t vf_bar_type[PCI_NUM_REGIONS ];/* Store type for each VF bar */ +const char *vfname;/* Reference to the device type used for the VFs */ +PCIDevice **vf;/* Pointer to an array of num_vfs VF devices */ +}; + +struct PCIESriovVF { +PCIDevice *pf;/* Pointer back to owner physical function */ +}; + +void pcie_sriov_pf_init (PCIDevice *dev,uint16_t offset, +const char *vfname,uint16_t vf_dev_id, +uint16_t init_vfs,uint16_t total_vfs, +uint16_t vf_offset,uint16_t vf_stride); +void pcie_sriov_pf_exit (PCIDevice *dev); + +/* Set up a VF bar in the SR/IOV bar area */ +void pcie_sriov_pf_init_vf_bar (PCIDevice *dev,int region_num, +uint8_t type,dma_addr_t size); + +/* Instantiate a bar for a VF */ +void pcie_sriov_vf_register_bar (PCIDevice *dev,int region_num, +MemoryRegion *memory); + +/* Add optional supported page sizes to the mask of supported page sizes +* Page size values are interpreted as opt_sup_pgsize << 12. +*/ +void pcie_sriov_pf_add_sup_pgsize (PCIDevice *dev,uint16_t opt_sup_pgsize); + +/* SR/IOV capability config write handler */ +void pcie_sriov_config_write (PCIDevice *dev,uint32_t address, +uint32_t val,int len); + +/* Reset SR/IOV VF Enable bit to unregister all VFs */ +void pcie_sriov_pf_disable_vfs (PCIDevice *dev); + +#endif /* QEMU_PCIE_SRIOV_H */ include/qemu/typedefs.h +0
-0
include/qemu/typedefs.h typedef struct PCIDevice PCIDevice; · line 60
Quoted 8 lines
include/qemu/typedefs.h +2
-0@@ -53,6 +53,8 @@ typedef struct PCIDevice PCIDevice;typedef struct PCIEAERErr PCIEAERErr ;typedef struct PCIEAERLog PCIEAERLog ;o typedef struct PCIEAERMsg PCIEAERMsg; +typedef struct PCIESriovPF PCIESriovPF ; +typedef struct PCIESriovVF PCIESriovVF ;typedef struct PCIEPort PCIEPort ;typedef struct PCIESlot PCIESlot ;typedef struct PCIExpressDevice PCIExpressDevice ;
I reviewed the patch and it looks OK to me (besides the other comments in the thread). Since we don't have code using it yet and the tests are only an RFC, I suggest adding to the series a document file explaining how to use it (template code from the RFC) and the current limitations/issues.
Thanks,
Marcel
Re: [Qemu-devel] [PATCH v4 3/3] pcie: Add support for Single Root I/O Virtualization (SR/IOV)
Quoted 735 lines · Marcel Apfelbaum
Quoted 725 lines · Knut Omang
This patch provides the building blocks for creating an SR/IOV PCIe Extended Capability header and register/unregister SR/IOV Virtual Functions.
Signed-off-by Knut Omang <[email protected]>--- hw/pci/Makefile.objs | 2 +- hw/pci/pci.c | 99 ++++++++++++---- hw/pci/pcie.c | 9 +- hw/pci/pcie_sriov.c | 271 ++++++++++++++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 11 +- include/hw/pci/pcie.h | 6 + include/hw/pci/pcie_sriov.h | 55 +++++++++ include/qemu/typedefs.h | 2 + 8 files changed, 426 insertions(+), 29 deletions(-)create mode 100644
hw/pci/pcie_sriov.c
create mode 100644include/hw/pci/pcie_sriov.hhw/pci/Makefile.objs +1
-1@@ -3,7 +3,7 @@ common-obj-$(CONFIG_PCI) += msix.o msi.o common-obj-$(CONFIG_PCI) += shpc.o common-obj-$(CONFIG_PCI) += slotid_cap.o common-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o -common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o +common-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.opcie_sriov.o
common-obj-$(call lnot,$(CONFIG_PCI)) += pci-stub.o common-obj-$(CONFIG_ALL) += pci-stub.ohw/pci/pci.c +7
-7@@ -153,6 +153,9 @@ int pci_bar(PCIDevice *d, int reg) {uint8_t type; +/* PCIe virtual functions do not have their own BARs */ +assert (!pci_is_vf (d)); +if (reg !=PCI_ROM_SLOT )return PCI_BASE_ADDRESS_0 + reg *4 ;@@ -211,22 +214,13 @@ void pci_device_deassert_intx(PCIDevice *dev) } } -static void pci_do_device_reset (PCIDevice *dev) +static void pci_reset_regions (PCIDevice *dev) {int r; +if (pci_is_vf (dev)) { +return ; + } -pci_device_deassert_intx (dev); -assert (dev->irq_state ==0 ); - -/* Clear all writable bits */ - pci_word_test_and_clear_mask(dev->config + PCI_COMMAND, - pci_get_word(dev->wmask +
PCI_COMMAND) |
- pci_get_word(dev->w1cmask +PCI_COMMAND));hw/pci/pci.c +0
-2- pci_word_test_and_clear_mask(dev->config + PCI_STATUS, - pci_get_word(dev->wmask +
PCI_STATUS) |
- pci_get_word(dev->w1cmask +PCI_STATUS));hw/pci/pci.c +14
-3- dev->config[PCI_CACHE_LINE_SIZE ] =0x0 ; - dev->config[PCI_INTERRUPT_LINE ] =0x0 ;for (r =0 ; r <PCI_NUM_REGIONS ; ++r) {PCIIORegion *region = &dev->io_regions[r];if (!region->size) {@@ -240,6 +234,27 @@ static void pci_do_device_reset(PCIDevice *dev) pci_set_long(dev->config + pci_bar(dev, r), region ->type); } } +} + +static void pci_do_device_reset (PCIDevice *dev) +{ +qdev_reset_all (&dev->qdev); + + dev->irq_state =0 ; +pci_update_irq_status (dev); +pci_device_deassert_intx (dev); +assert (dev->irq_state ==0 ); + +/* Clear all writable bits */ + pci_word_test_and_clear_mask(dev->config + PCI_COMMAND, + pci_get_word(dev->wmask +
PCI_COMMAND) |
+ pci_get_word(dev->w1cmask +PCI_COMMAND));hw/pci/pci.c +2
-0+ pci_word_test_and_clear_mask(dev->config + PCI_STATUS, + pci_get_word(dev->wmask +
PCI_STATUS) |
+ pci_get_word(dev->w1cmask +PCI_STATUS));hw/pci/pci.c +4
-0+ dev->config[PCI_CACHE_LINE_SIZE ] =0x0 ; + dev->config[PCI_INTERRUPT_LINE ] =0x0 ; +pci_reset_regions (dev);pci_update_mappings (dev);msi_reset (dev);@@ -771,6 +786,15 @@ static void pci_init_multifunction(PCIBus *bus, PCIDevice *dev, Error **errp) dev->config[PCI_HEADER_TYPE ] |=PCI_HEADER_TYPE_MULTI_FUNCTION ; } +/* With SR/IOV and ARI, a device at function 0 need not be a multifunction
+ * device, as it may just be a VF that ended up with function
0 inhw/pci/pci.c +4
-0+* the legacy PCI interpretation. Avoid failing in such cases: +*/ +if (pci_is_vf (dev) && + dev->exp.sriov_vf.pf->cap_present &
QEMU_PCI_CAP_MULTIFUNCTION) {hw/pci/pci.c +4
-0+return ; + } +/* * multifunction bit is interpreted in two ways as follows. * - all functions must set the bit to 1. @@ -962,6 +986,7 @@ void pci_register_bar(PCIDevice *pci_dev, int region_num, uint64_t wmask;pcibus_t size =memory_region_size (memory); +assert (!pci_is_vf (pci_dev));/* VFs must use
pcie_sriov_vf_register_bar*/assert(region_num >= 0); assert(region_num < PCI_NUM_REGIONS); if (size & (size-1)) {hw/pci/pci.c +37
-4@@ -1060,11 +1085,44 @@ pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)return pci_dev->io_regions[region_num].addr; } -static pcibus_t pci_bar_address(PCIDevice *d, - int reg, uint8_t type, pcibus_t size) + +static pcibus_t pci_config_get_bar_addr(PCIDevice *d, int reg, +uint8_t type,pcibus_t size) +{ +pcibus_t new_addr; +if (!pci_is_vf (d)) { +int bar =pci_bar (d, reg); +if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { + new_addr =pci_get_quad (d->config + bar); + }else { + new_addr =pci_get_long (d->config + bar); + } + }else { +PCIDevice *pf = d->exp.sriov_vf.pf; +uint16_t sriov_cap = pf->exp.sriov_cap; +int bar = sriov_cap +PCI_SRIOV_BAR + reg *4 ; +uint16_t vf_offset =pci_get_word (pf->config + sriov_cap +PCI_SRIOV_VF_OFFSET ); +uint16_t vf_stride =pci_get_word (pf->config + sriov_cap +PCI_SRIOV_VF_STRIDE ); +uint32_t vf_num = (d->devfn - (pf->devfn + vf_offset)) / vf_stride; + +if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { + new_addr =pci_get_quad (pf->config + bar); + }else { + new_addr =pci_get_long (pf->config + bar); + } + new_addr += vf_num * size; + } +if (reg !=PCI_ROM_SLOT ) { +/* Preserve the rom enable bit */ + new_addr &= ~(size -1 ); + } +return new_addr; +} + +pcibus_t pci_bar_address (PCIDevice *d, +int reg,uint8_t type,pcibus_t size) {pcibus_t new_addr, last_addr; -int bar =pci_bar (d, reg);uint16_t cmd =pci_get_word (d->config +PCI_COMMAND );Object *machine =qdev_get_machine ();ObjectClass *oc =object_get_class (machine);@@ -1075,7 +1133,7 @@ static pcibus_t pci_bar_address(PCIDevice *d,if (!(cmd &PCI_COMMAND_IO )) {return PCI_BAR_UNMAPPED ; } - new_addr =pci_get_long (d->config + bar) & ~(size -1 ); + new_addr =pci_config_get_bar_addr (d, reg, type, size); last_addr = new_addr + size -1 ;/* Check if 32 bit BAR wraps around explicitly. * TODO: make priorities correct and remove this work around.
hw/pci/pci.c +1
-5@@ -1090,11 +1148,7 @@ static pcibus_t pci_bar_address(PCIDevice *d, if (!(cmd & PCI_COMMAND_MEMORY)) {return PCI_BAR_UNMAPPED ; } -if (type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { - new_addr =pci_get_quad (d->config + bar); - }else { - new_addr =pci_get_long (d->config + bar); - } + new_addr =pci_config_get_bar_addr (d, reg, type, size);/* the ROM slot has a specific enable bit */ if (reg == PCI_ROM_SLOT && !(new_addr &PCI_ROM_ADDRESS_ENABLE)) { return PCI_BAR_UNMAPPED;hw/pci/pci.c +0
-0@@ -1228,6 +1282,7 @@ void pci_default_write_config(PCIDevice *d,
uint32_taddr,uint32_tval_in, intmsi_write_config(d, addr, val_in, l); msix_write_config(d, addr, val_in, l);hw/pci/pci.c +1
-0+pcie_sriov_config_write (d, addr, val_in, l); }/***********************************************************/ hw/pci/pcie.c +1
-1@@ -253,7 +253,7 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler*hotplug_dev, DeviceState *dev, * Right now, only a device of function = 0 is allowed to be * hot plugged/unplugged. */ -assert (PCI_FUNC (pci_dev->devfn) ==0 ); +assert (PCI_FUNC (pci_dev->devfn) ==0 ||pci_is_vf (pci_dev)); pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,PCI_EXP_SLTSTA_PDS);hw/pci/pcie.c +3
-2@@ -265,10 +265,11 @@ voidpcie_cap_slot_hot_unplug_request_cb (HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp) {uint8_t *exp_cap; +PCIDevice *pdev =PCI_DEVICE (hotplug_dev); -pcie_cap_slot_hotplug_common (PCI_DEVICE (hotplug_dev), dev, &exp_cap, errp); +pcie_cap_slot_hotplug_common (pdev, dev, &exp_cap, errp); -pcie_cap_slot_push_attention_button (PCI_DEVICE (hotplug_dev)); +pcie_cap_slot_push_attention_button (pdev);}/* pci express slot for pci express root/downstream port
hw/pci/pcie.c +1
-1@@ -408,7 +409,7 @@ void pcie_cap_slot_write_config(PCIDevice *dev, }/* -* If the slot is polulated, power indicator is off and power +* If the slot is populated, power indicator is off and power * controller is off, it is safe to detach the devices. */ if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val &
PCI_EXP_SLTCTL_PCC) &&hw/pci/pcie_sriov.c new file +8
-0@@ -0,0 +1,271 @@ +/* +* pcie_sriov.c: +* +* Implementation of SR/IOV emulation support. +* +* Copyright (c) 2014 Knut Omang <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */hw/pci/pcie_sriov.c new file +10
-0+ +#include "hw/pci/pci.h" +#include "hw/pci/pcie.h" +#include "hw/pci/pci_bus.h" +#include "qemu/range.h" + +//#define DEBUG_SRIOV +#ifdef DEBUG_SRIOV +# define SRIOV_DPRINTF (fmt, ...) \ + fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ##
__VA_ARGS__)hw/pci/pcie_sriov.c new file +5
-0+#else +# define SRIOV_DPRINTF (fmt, ...) do {} while (0) +#endif +#define SRIOV_DEV_PRINTF (dev, fmt, ...) \ + SRIOV_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ##
__VA_ARGS__)hw/pci/pcie_sriov.c new file +2
-0+ +static PCIDevice *register_vf(PCIDevice *pf, int devfn, const char*name);
hw/pci/pcie_sriov.c new file +21
-0+static void unregister_vfs (PCIDevice *dev); + +void pcie_sriov_pf_init (PCIDevice *dev,uint16_t offset, +const char *vfname,uint16_t vf_dev_id, +uint16_t init_vfs,uint16_t total_vfs, +uint16_t vf_offset,uint16_t vf_stride) +{ +uint8_t *cfg = dev->config + offset; +uint8_t *wmask; + +pcie_add_capability (dev,PCI_EXT_CAP_ID_SRIOV ,1 , + offset,PCI_EXT_CAP_SRIOV_SIZEOF ); + dev->exp.sriov_cap = offset; + dev->exp.sriov_pf.num_vfs =0 ; + dev->exp.sriov_pf.vfname =g_strdup (vfname); + dev->exp.sriov_pf.vf =NULL ; + +pci_set_word (cfg +PCI_SRIOV_VF_OFFSET , vf_offset); +pci_set_word (cfg +PCI_SRIOV_VF_STRIDE , vf_stride); + +/* Minimal page size support values required by the SR/IOV standard:
hw/pci/pcie_sriov.c new file +11
-0+* 0x553 << 12 = 0x553000 = 4K + 8K + 64K + 256K + 1M + 4M +* Hard coded for simplicity in this version +*/ +pci_set_word (cfg +PCI_SRIOV_SUP_PGSIZE ,0x553 ); + +/* Default is to use 4K pages, software can modify it +* to any of the supported bits +*/ +pci_set_word (cfg +PCI_SRIOV_SYS_PGSIZE ,0x1 ); + +/* Set up device ID and initial/total number of VFs available */
hw/pci/pcie_sriov.c new file +9
-0+pci_set_word (cfg +PCI_SRIOV_VF_DID , vf_dev_id); +pci_set_word (cfg +PCI_SRIOV_INITIAL_VF , init_vfs); +pci_set_word (cfg +PCI_SRIOV_TOTAL_VF , total_vfs); +pci_set_word (cfg +PCI_SRIOV_NUM_VF ,0 ); + +/* Write enable control bits */ + wmask = dev->wmask + offset; + pci_set_word(wmask + PCI_SRIOV_CTRL, + PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE |
PCI_SRIOV_CTRL_ARI);hw/pci/pcie_sriov.c new file +56
-1+pci_set_word (wmask +PCI_SRIOV_NUM_VF ,0xffff ); +pci_set_word (wmask +PCI_SRIOV_SYS_PGSIZE ,0x553 ); + +qdev_prop_set_bit (&dev->qdev,"multifunction" , true); +} + +void pcie_sriov_pf_exit (PCIDevice *dev) +{ +SRIOV_DPRINTF ("\n" ); +unregister_vfs (dev); +g_free ((char *)dev->exp.sriov_pf.vfname); + dev->exp.sriov_pf.vfname =NULL ; +} + +void pcie_sriov_pf_init_vf_bar (PCIDevice *dev,int region_num, +uint8_t type,dma_addr_t size) +{ +uint32_t addr; +uint64_t wmask; +uint16_t sriov_cap = dev->exp.sriov_cap; + +assert (sriov_cap >0 ); +assert (region_num >=0 ); +assert (region_num <PCI_NUM_REGIONS ); +assert (region_num !=PCI_ROM_SLOT ); + + wmask = ~(size -1 ); + addr = sriov_cap +PCI_SRIOV_BAR + region_num *4 ; + +pci_set_long (dev->config + addr, type); +if (!(type &PCI_BASE_ADDRESS_SPACE_IO ) && + type &PCI_BASE_ADDRESS_MEM_TYPE_64 ) { +pci_set_quad (dev->wmask + addr, wmask); +pci_set_quad (dev->cmask + addr, ~0ULL ); + }else { +pci_set_long (dev->wmask + addr, wmask &0xffffffff ); +pci_set_long (dev->cmask + addr,0xffffffff ); + } + dev->exp.sriov_pf.vf_bar_type[region_num] = type; +} + +void pcie_sriov_vf_register_bar (PCIDevice *dev,int region_num, +MemoryRegion *memory) +{ +PCIIORegion *r; +uint8_t type; +pcibus_t size =memory_region_size (memory); + +assert (pci_is_vf (dev));/* PFs must use pci_register_bar */ +assert (region_num >=0 ); +assert (region_num <PCI_NUM_REGIONS ); + type = dev->exp.sriov_vf.pf ->exp.sriov_pf.vf_bar_type[region_num]; + +if (size & (size-1 )) { + fprintf(stderr, "ERROR: PCI region size must be a power" + " of two - type=0x%x, size=0x%"FMT_PCIBUS"\n",type, size);
hw/pci/pcie_sriov.c new file +16
-0+exit (1 ); + } + + r = &dev->io_regions[region_num]; + r->memory = memory; + r->address_space = + type &PCI_BASE_ADDRESS_SPACE_IO + ? dev->bus->address_space_io + : dev->bus->address_space_mem; + r->size = size; + r->type = type; + + r->addr =pci_bar_address (dev, region_num, r->type, r->size); +if (r->addr !=PCI_BAR_UNMAPPED ) { + memory_region_add_subregion_overlap(r->address_space, + r->addr, r->memory,1);
hw/pci/pcie_sriov.c new file +4
-0+ } +} + +static PCIDevice *register_vf(PCIDevice *pf, int devfn, const char*name)
hw/pci/pcie_sriov.c new file +6
-0+{ +PCIDevice *dev =pci_create (pf->bus, devfn, name); + dev->exp.sriov_vf.pf = pf; +Error *local_err =NULL ; + + object_property_set_bool(OBJECT(&dev->qdev), true, "realized",
&local_err);hw/pci/pcie_sriov.c new file +19
-0+if (local_err) { +fprintf (stderr,"Failed to enable: %s\n" , +error_get_pretty (local_err)); +error_free (local_err); +return NULL ; + } + +/* set vid/did according to sr/iov spec - they are not used */ +pci_config_set_vendor_id (dev->config,0xffff ); +pci_config_set_device_id (dev->config,0xffff ); +return dev; +} + +static void register_vfs (PCIDevice *dev) +{ +uint16_t num_vfs; +uint16_t i; +uint16_t sriov_cap = dev->exp.sriov_cap; + uint16_t vf_offset = pci_get_word(dev->config + sriov_cap +
PCI_SRIOV_VF_OFFSET);
+uint16_tvf_stride= pci_get_word(dev->config +sriov_cap+PCI_SRIOV_VF_STRIDE);hw/pci/pcie_sriov.c new file +4
-0+int32_t devfn = dev->devfn + vf_offset; + +assert (sriov_cap >0 ); + num_vfs = pci_get_word(dev->config + sriov_cap +
PCI_SRIOV_NUM_VF);hw/pci/pcie_sriov.c new file +2
-0+ + dev->exp.sriov_pf.vf = g_malloc(sizeof(PCIDevice *) *
num_vfs);hw/pci/pcie_sriov.c new file +7
-1+ assert(dev->exp.sriov_pf.vf); + + SRIOV_DEV_PRINTF(dev, "creating %d vf devs\n", num_vfs); + for (i = 0; i < num_vfs; i++) { + dev->exp.sriov_pf.vf[i] = register_vf(dev, devfn, dev ->exp.sriov_pf.vfname); + if (!dev->exp.sriov_pf.vf[i]) { + SRIOV_DEV_PRINTF(dev, "Failed to create VF %d at devfn%x.%x\n", i,
hw/pci/pcie_sriov.c new file +29
-1+ PCI_SLOT(devfn), PCI_FUNC(devfn)); + num_vfs = i; +break ; + } + devfn += vf_stride; + } + dev->exp.sriov_pf.num_vfs = num_vfs; +} + +static void unregister_vfs(PCIDevice *dev) +{ + Error *local_err = NULL; + uint16_t num_vfs = dev->exp.sriov_pf.num_vfs; + uint16_t i; + SRIOV_DEV_PRINTF(dev, "Unregistering %d vf devs\n", num_vfs); + for (i = 0; i < num_vfs; i++) { + object_property_set_bool(OBJECT(&dev->exp.sriov_pf.vf[i] ->qdev), false, "realized", &local_err); + if (local_err) { + fprintf(stderr, "Failed to unplug: %s\n", + error_get_pretty(local_err)); + error_free(local_err); + } + } + g_free(dev->exp.sriov_pf.vf); + dev->exp.sriov_pf.vf = NULL; + dev->exp.sriov_pf.num_vfs = 0; +} + +void pcie_sriov_config_write(PCIDevice *dev, uint32_t address,
uint32_tval, int len)hw/pci/pcie_sriov.c new file +13
-0+{ +uint32_t off; +uint16_t sriov_cap = dev->exp.sriov_cap; + +if (!sriov_cap || address < sriov_cap) { +return ; + } + off = address - sriov_cap; +if (off >=PCI_EXT_CAP_SRIOV_SIZEOF ) { +return ; + } + + SRIOV_DEV_PRINTF(dev, "cap at 0x%x sriov offset 0x%x val 0x%xlen %d\n",
hw/pci/pcie_sriov.c new file +17
-0+ sriov_cap, off, val, len); + +if (range_covers_byte (off, len,PCI_SRIOV_CTRL )) { +if (dev->exp.sriov_pf.num_vfs) { +if (!(val &PCI_SRIOV_CTRL_VFE )) { +unregister_vfs (dev); + } + }else { +if (val &PCI_SRIOV_CTRL_VFE ) { +register_vfs (dev); + } + } + } +} + + +/* Reset SR/IOV VF Enable bit to trigger an unregister of all VFs */
hw/pci/pcie_sriov.c new file +5
-0+void pcie_sriov_pf_disable_vfs (PCIDevice *dev) +{ +uint16_t sriov_cap = dev->exp.sriov_cap; +if (sriov_cap) { + uint32_t val = pci_get_byte(dev->config + sriov_cap +
PCI_SRIOV_CTRL);hw/pci/pcie_sriov.c new file +3
-0+if (val &PCI_SRIOV_CTRL_VFE ) { + val &= ~PCI_SRIOV_CTRL_VFE ; + pcie_sriov_config_write(dev, sriov_cap +
PCI_SRIOV_CTRL, val, 1);hw/pci/pcie_sriov.c new file +5
-0+ } + } +} + +/* Add optional supported page sizes to the mask of supported page sizes */
+voidpcie_sriov_pf_add_sup_pgsize(PCIDevice *dev, uint16_t opt_sup_pgsize)hw/pci/pcie_sriov.c new file +5
-0+{ +uint8_t *cfg = dev->config + dev->exp.sriov_cap; +uint8_t *wmask = dev->wmask + dev->exp.sriov_cap; + + uint16_t sup_pgsize = pci_get_word(cfg +
PCI_SRIOV_SUP_PGSIZE);hw/pci/pcie_sriov.c new file +9
-0+ + sup_pgsize |= opt_sup_pgsize; + +/* Make sure the new bits are set, and that system page size +* also can be set to any of the new values according to spec: +*/ +pci_set_word (cfg +PCI_SRIOV_SUP_PGSIZE , sup_pgsize); +pci_set_word (wmask +PCI_SRIOV_SYS_PGSIZE , sup_pgsize); +}include/hw/pci/pci.h +0
-2@@ -11,8 +11,6 @@/* PCI includes legacy ISA access. */ #include "hw/isa/isa.h" -#include "hw/pci/pcie.h" -/* PCI bus */ #define PCI_DEVFN (slot, func) ((((slot) & 0x1f) << 3) | ((func)&
0x07))include/hw/pci/pci.h +4
-0@@ -132,6 +130,7 @@ enum {#define QEMU_PCI_VGA_IO_HI_SIZE 0x20#include "hw/pci/pci_regs.h" +#include "hw/pci/pcie.h" /* PCI HEADER_TYPE */ #define PCI_HEADER_TYPE_MULTI_FUNCTION 0x80@@ -421,6 +420,9 @@ typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int);AddressSpace *pci_device_iommu_address_space (PCIDevice *dev);void pci_setup_iommu (PCIBus *bus,PCIIOMMUFunc fn,void *opaque); +pcibus_t pci_bar_address (PCIDevice *d, +int reg,uint8_t type,pcibus_t size); +static inline void pci_set_byte (uint8_t *config,uint8_t val){include/hw/pci/pci.h +5
-0@@ -672,6 +674,11 @@ static inline int pci_is_express(const PCIDevice *d)return d->cap_present &QEMU_PCI_CAP_EXPRESS ; } +static inline int pci_is_vf (const PCIDevice *d) +{ +return d->exp.sriov_vf.pf !=NULL ; +} +static inline uint32_t pci_config_size (const PCIDevice *d) {return
pci_is_express(d)?PCIE_CONFIG_SPACE_SIZE:PCI_CONFIG_SPACE_SIZE;include/hw/pci/pcie.h +6
-0@@ -25,6 +25,7 @@#include "hw/pci/pci_regs.h" #include "hw/pci/pcie_regs.h" #include "hw/pci/pcie_aer.h" +#include "hw/pci/pcie_sriov.h" #include "hw/hotplug.h" typedef enum {@@ -74,6 +75,11 @@ struct PCIExpressDevice {/* AER */ uint16_t aer_cap;PCIEAERLog aer_log; + +/* SR/IOV */ +uint16_t sriov_cap; +PCIESriovPF sriov_pf; +PCIESriovVF sriov_vf; };#define COMPAT_PROP_PCP "power_controller_present"include/hw/pci/pcie_sriov.h new file +8
-0@@ -0,0 +1,55 @@ +/* +* pcie_sriov.h: +* +* Implementation of SR/IOV emulation support. +* +* Copyright (c) 2014 Knut Omang <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */include/hw/pci/pcie_sriov.h new file +8
-0+ +#ifndef QEMU_PCIE_SRIOV_H +#define QEMU_PCIE_SRIOV_H + + + +struct PCIESriovPF { + uint16_t num_vfs; /* Number of virtual functionscreated */
+uint8_tvf_bar_type[PCI_NUM_REGIONS]; /* Store type for each
VF bar */
+ const char *vfname; /* Reference to the device type
used for the VFs */
+ PCIDevice **vf; /* Pointer to an array ofnum_vfs
VF devices */include/hw/pci/pcie_sriov.h new file +4
-0+}; + +struct PCIESriovVF { +PCIDevice *pf;/* Pointer back to owner physical function */
include/hw/pci/pcie_sriov.h new file +17
-0+}; + +void pcie_sriov_pf_init (PCIDevice *dev,uint16_t offset, +const char *vfname,uint16_t vf_dev_id, +uint16_t init_vfs,uint16_t total_vfs, +uint16_t vf_offset,uint16_t vf_stride); +void pcie_sriov_pf_exit (PCIDevice *dev); + +/* Set up a VF bar in the SR/IOV bar area */ +void pcie_sriov_pf_init_vf_bar (PCIDevice *dev,int region_num, +uint8_t type,dma_addr_t size); + +/* Instantiate a bar for a VF */ +void pcie_sriov_vf_register_bar (PCIDevice *dev,int region_num, +MemoryRegion *memory); + +/* Add optional supported page sizes to the mask of supported page sizes
+ * Page size values are interpreted asopt_sup_pgsize<< 12.
+ */
+voidpcie_sriov_pf_add_sup_pgsize(PCIDevice *dev, uint16_t opt_sup_pgsize);include/hw/pci/pcie_sriov.h new file +9
-0+ +/* SR/IOV capability config write handler */ +void pcie_sriov_config_write (PCIDevice *dev,uint32_t address, +uint32_t val,int len); + +/* Reset SR/IOV VF Enable bit to unregister all VFs */ +void pcie_sriov_pf_disable_vfs (PCIDevice *dev); + +#endif /* QEMU_PCIE_SRIOV_H */ include/qemu/typedefs.h +0
-0include/qemu/typedefs.h typedef struct PCIDevice PCIDevice; · line 60
Quoted 8 lines
include/qemu/typedefs.h +2
-0@@ -53,6 +53,8 @@ typedef struct PCIDevice PCIDevice;typedef struct PCIEAERErr PCIEAERErr ;typedef struct PCIEAERLog PCIEAERLog ;o typedef struct PCIEAERMsg PCIEAERMsg; +typedef struct PCIESriovPF PCIESriovPF ; +typedef struct PCIESriovVF PCIESriovVF ;typedef struct PCIEPort PCIEPort ;typedef struct PCIESlot PCIESlot ;typedef struct PCIExpressDevice PCIExpressDevice ;
Quoted 7 lines
I reviewed the patch and it looks OK to me (besides the other comments in the thread).
Since we don't have code using it yet and the tests are only an RFC, I suggest adding
to the series a document file explaining how to use it (template code from the RFC)
and the current limitations/issues.
Good idea, the
docs/pci_expander_bridge.txt
file seems like a good template,
Thanks
Knut
Quoted 5 lines
Thanks,
Marcel