Implement SR-IOV support for PVH
v5 cover Implement SR-IOV support for PVH
This series enables support for PCI SR-IOV capability for PVH domains. It allows Dom0 to enable and use SR-IOV virtual functions and for these functions to be passed to guests.
To achieve this, add handlers for SRIOV_CONTROL registers and simplified handlers for VF headers. Dom0 enables SR-IOV; Xen then discovers the VFs itself, terminating config space accesses to them for 100ms as the spec permits, and adding each VF on the first access past that window. No hypercall from dom0 is involved.
Core functionality is based on previous work[1].
Tested on R-Car Spider board with Samsung NVMe SSD Controller 980 and Intel X550T ethernet card.
[1]: https://lore.kernel.org/xen-devel/[email protected]/
v4->v5:
* re-check pending VFs under pcidevs_lock()
* keep PHYSDEVOP_pci_device_add working for VFs on PV dom0
* gate the SR-IOV capability on the hardware domain instead of XSM
* drop the class/revision handler from the DomU patch
* see individual patches
v3->v4:
* rework the series for VF discovery in Xen
* squash export patches
* drop "vpci: Use pervcpu ranges for BAR mapping"; rangesets are now allocated per mapping task
* split the non-functional churn into "vpci: use a local variable for the BAR rangeset"
* fold the documentation patch into "add SR-IOV support for PVH Dom0"
* see individual patches
v2->v3:
* rework the series for VF discovery by Dom0
* drop pci/iommu: Check that IOMMU supports removing devices, see [2]
* drop vpci: add a wait operation to the vpci vcpu pending actions
* add vpci: allow queueing of mapping operations
* minor changes in individual patches
[2]: https://patchew.org/Xen/a59c2da0d4c72deb42950e9a8e3982fbdee60668.1775555766.git.mykyta._5Fpoturai@epam.com/v1->v2:
* rework the series for VF discovery in Xen
* separate doc changes into the last patch
Mykyta Poturai (1):
vpci: use a local variable for the BAR rangeset
Stewart Hildebrand (4):
vpci: Export functions required for SR-IOV support
vpci: allow queueing of mapping operations
vpci: add SR-IOV support for PVH Dom0
vpci: add SR-IOV support for DomUs
CHANGELOG.md | 2 + SUPPORT.md | 2 - tools/tests/vpci/emul.h | 6 + xen/common/domain.c | 2 + xen/drivers/passthrough/pci.c | 46 +++ xen/drivers/pci/physdev.c | 3 + xen/drivers/vpci/Makefile | 1 + xen/drivers/vpci/header.c | 306 ++++++++++++------- xen/drivers/vpci/private.h | 36 +++ xen/drivers/vpci/sriov.c | 560 ++++++++++++++++++++++++++++++++++ xen/drivers/vpci/vpci.c | 44 ++- xen/include/xen/pci.h | 3 + xen/include/xen/vpci.h | 40 ++- 13 files changed, 923 insertions(+), 128 deletions(-) create mode 100644 xen/drivers/vpci/sriov.c
Signature · 1 line
2.56.1
PATCH v5 2/5 vpci: use a local variable for the BAR rangeset
From: Mykyta Poturai <[email protected]>
In the BAR (un)mapping paths latch the rangeset of the BAR being processed into a local variable, instead of dereferencing struct vpci_bar on every use. Preparatory work for moving the rangesets out of struct vpci_bar.
No functional change.
---v4->v5:
* change SOB order
* remove unrelated change
* collect RBv3->v4:
* new patch, split out of "vpci: Use pervcpu ranges for BAR mapping" so that the non-functional churn is separate from the functional change
--- xen/drivers/vpci/header.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-)
xen/drivers/vpci/header.c +21 -17
@@ -196,6 +196,7 @@ bool vpci_process_pending(struct vcpu *v)for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) {struct vpci_bar *bar = &header->bars[i]; +struct rangeset *mem = bar->mem;struct map_data data = { .d = v->domain, .map = v->vpci.cmd &PCI_COMMAND_MEMORY ,
@@ -203,10 +204,10 @@ bool vpci_process_pending(struct vcpu *v) };int rc; -if (rangeset_is_empty (bar->mem) ) +if (rangeset_is_empty (mem) )continue ; - rc =rangeset_consume_ranges (bar->mem, map_range, &data); + rc =rangeset_consume_ranges (mem, map_range, &data);if ( rc == -ERESTART ) {
@@ -260,13 +261,14 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev,for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) {struct vpci_bar *bar = &header->bars[i]; +struct rangeset *mem = bar->mem;struct map_data data = { .d = d, .map = true, .bar = bar }; -if (rangeset_is_empty (bar->mem) ) +if (rangeset_is_empty (mem) )continue ; -while ( (rc =rangeset_consume_ranges (bar->mem, map_range, - &data)) == -ERESTART ) +while ( (rc =rangeset_consume_ranges (mem, map_range, &data)) == + -ERESTART ) {/* * It's safe to drop and reacquire the lock in this context
@@ -331,12 +333,13 @@ int vpci_modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)for ( i =0 ; i <ARRAY_SIZE (header->bars); i++ ) {struct vpci_bar *bar = &header->bars[i]; +struct rangeset *mem = bar->mem;unsigned long start =PFN_DOWN (bar->addr);unsigned long end =PFN_DOWN (bar->addr + bar->size -1 );unsigned long start_guest =PFN_DOWN (bar->guest_addr);unsigned long end_guest =PFN_DOWN (bar->guest_addr + bar->size -1 ); -if ( !bar->mem ) +if ( !mem )continue ; if ( !MAPPABLE_BAR(bar) ||
@@ -354,7 +357,7 @@ int vpci_modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)continue ; } -ASSERT (rangeset_is_empty (bar->mem)); +ASSERT (rangeset_is_empty (mem));/* * Make sure that the guest set address has the same page offset
@@ -369,7 +372,7 @@ int vpci_modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)return -EINVAL ; } - rc =rangeset_add_range (bar->mem, start_guest, end_guest); + rc =rangeset_add_range (mem, start_guest, end_guest);if ( rc ) { printk(XENLOG_G_WARNING "Failed to add [%lx, %lx]: %d\n",
@@ -380,12 +383,12 @@ int vpci_modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)/* Check for overlap with the already setup BAR ranges. */ for ( j =0 ; j < i; j++ ) { -struct vpci_bar *prev_bar = &header->bars[j]; +struct rangeset *prev_mem = header->bars[j].mem; -if (rangeset_is_empty (prev_bar->mem) ) +if (rangeset_is_empty (prev_mem) )continue ; - rc =rangeset_remove_range (prev_bar->mem, start_guest, end_guest); + rc =rangeset_remove_range (prev_mem, start_guest, end_guest);if ( rc ) { gprintk(XENLOG_WARNING,
@@ -395,7 +398,7 @@ int vpci_modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) } } - rc =pci_sanitize_bar_memory (bar->mem); + rc =pci_sanitize_bar_memory (mem);if ( rc ) { gprintk(XENLOG_WARNING,
@@ -414,12 +417,12 @@ int vpci_modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)for ( j =0 ; j <ARRAY_SIZE (header->bars); j++ ) { -const struct vpci_bar *bar = &header->bars[j]; +struct rangeset *mem = header->bars[j].mem; -if (rangeset_is_empty (bar->mem) ) +if (rangeset_is_empty (mem) )continue ; - rc =rangeset_remove_range (bar->mem, start, end); + rc =rangeset_remove_range (mem, start, end);if ( rc ) { gprintk(XENLOG_WARNING,
@@ -469,8 +472,9 @@ int vpci_modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)for ( j =0 ; j <ARRAY_SIZE (header->bars); j++) {const struct vpci_bar *bar = &header->bars[j]; +struct rangeset *mem = bar->mem; - if ( !rangeset_overlaps_range(bar->mem, start, end) || + if ( !rangeset_overlaps_range(mem, start, end) ||/* * If only the ROM enable bit is toggled check against * other BARs in the same device for overlaps, but not
@@ -481,7 +485,7 @@ int vpci_modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) bar->type == VPCI_BAR_ROM) )continue ; - rc =rangeset_remove_range (bar->mem, start, end); + rc =rangeset_remove_range (mem, start, end);if ( rc ) { gprintk(XENLOG_WARNING,
Signature · 1 line
2.56.1
PATCH v5 1/5 vpci: Export functions required for SR-IOV support
In reply to Mykyta PoturaiFrom: Stewart Hildebrand <[email protected]>
Rename and export vpci_modify_bars, vpci_guest_mem_bar_{read,write}
---v4->v5:
* collect RBsv3->v4:
* squash "vpci: rename and export vpci_guest_mem_bar_{read,write}" with "vpci: rename and export vpci_modify_bars"v2->v3:
* move declaration to private.hv1->v2
* Collect RBs
--- xen/drivers/vpci/header.c | 36 ++++++++++++++++++++---------------- xen/drivers/vpci/private.h | 9 +++++++++ 2 files changed, 29 insertions(+), 16 deletions(-)
xen/drivers/vpci/header.c +20 -16
@@ -305,7 +305,7 @@ static void defer_map(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)raise_softirq (SCHEDULE_SOFTIRQ ); } -static int modify_bars (const struct pci_dev *pdev,uint16_t cmd,bool rom_only) +int vpci_modify_bars (const struct pci_dev *pdev,uint16_t cmd,bool rom_only) {struct vpci_header *header = &pdev->vpci->header;struct pci_dev *tmp;
@@ -546,7 +546,7 @@ static void cf_check cmd_write(* memory decoding bit has not been changed, so leave everything as-is, * hoping the guest will realize and try again. */ -modify_bars (pdev, cmd, false); +vpci_modify_bars (pdev, cmd, false); else pci_conf_write16(pdev->sbdf, reg, cmd); }
@@ -611,9 +611,9 @@ static void cf_check bar_write(pci_conf_write32 (pdev->sbdf, reg, val); } -static void cf_check guest_mem_bar_write(const struct pci_dev *pdev, -unsigned int reg,uint32_t val, -void *data) +void cf_check vpci_guest_mem_bar_write(const struct pci_dev *pdev, +unsigned int reg,uint32_t val, +void *data) {struct vpci_bar *bar = data;bool hi = false;
@@ -653,8 +653,8 @@ static void cf_check guest_mem_bar_write(const struct pci_dev *pdev, bar->guest_addr = guest_addr; } -static uint32_t cf_check guest_mem_bar_read(const struct pci_dev *pdev, -unsigned int reg,void *data) +uint32_t cf_check vpci_guest_mem_bar_read(const struct pci_dev *pdev, +unsigned int reg,void *data) {const struct vpci_bar *bar = data;uint32_t reg_val;
@@ -714,13 +714,15 @@ static void cf_check rom_write(* Pass PCI_COMMAND_MEMORY or 0 to signal a map/unmap request, note that * this fabricated command is never going to be written to the register. */ - else if ( modify_bars(pdev, new_enabled ? PCI_COMMAND_MEMORY : 0, true) ) + else if ( vpci_modify_bars(pdev, + new_enabled ? PCI_COMMAND_MEMORY : 0, + true) ) /* * No memory has been added or removed from the p2m (because the actual * p2m changes are deferred in defer_map) and the ROM enable bit has * not been changed, so leave everything as-is, hoping the guest will * realize and try again. It's important to not update rom->addr in the - * unmap case if modify_bars has failed, or future attempts would + * unmap case if vpci_modify_bars has failed, or future attempts would * attempt to unmap the wrong address. */ return;
@@ -800,8 +802,8 @@ int vpci_init_header(struct pci_dev *pdev)/* * For DomUs, clear PCI_COMMAND_{MASTER,MEMORY,IO} and other * DomU-controllable bits in PCI_COMMAND. Devices assigned to DomUs will -* start with memory decoding disabled, and modify_bars() will not be called -* at the end of this function. +* start with memory decoding disabled, and vpci_modify_bars() will not be +* called at the end of this function. */ if ( !is_hwdom ) cmd &= ~(PCI_COMMAND_VGA_PALETTE | PCI_COMMAND_INVALIDATE |
@@ -824,8 +826,9 @@ int vpci_init_header(struct pci_dev *pdev) bars[i].type =VPCI_BAR_MEM64_HI ; rc =vpci_add_register (pdev->vpci, is_hwdom ? vpci_hw_read32 - : guest_mem_bar_read, - is_hwdom ? bar_write : guest_mem_bar_write, + : vpci_guest_mem_bar_read, + is_hwdom ? bar_write + : vpci_guest_mem_bar_write, reg,4 , &bars[i]);if ( rc ) goto fail;
@@ -883,8 +886,9 @@ int vpci_init_header(struct pci_dev *pdev) bars[i].prefetchable = val &PCI_BASE_ADDRESS_MEM_PREFETCH ; rc =vpci_add_register (pdev->vpci, - is_hwdom ? vpci_hw_read32 : guest_mem_bar_read, - is_hwdom ? bar_write : guest_mem_bar_write, + is_hwdom ? vpci_hw_read32 + : vpci_guest_mem_bar_read, + is_hwdom ? bar_write : vpci_guest_mem_bar_write, reg,4 , &bars[i]);if ( rc ) goto fail;
@@ -926,7 +930,7 @@ int vpci_init_header(struct pci_dev *pdev) goto fail; } -return (cmd &PCI_COMMAND_MEMORY ) ?modify_bars (pdev, cmd, false) :0 ; +return (cmd &PCI_COMMAND_MEMORY ) ?vpci_modify_bars (pdev, cmd, false) :0 ; fail:pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd);
xen/drivers/vpci/private.h +9 -0
@@ -138,6 +138,15 @@ static inline size_t vmsix_table_size(const struct vpci *vpci, unsigned int nr) 8), 8); } +/* Map/unmap the BARs of a vPCI device. */ +int vpci_modify_bars (const struct pci_dev *pdev,uint16_t cmd,bool rom_only); + +void cf_check vpci_guest_mem_bar_write(const struct pci_dev *pdev, +unsigned int reg,uint32_t val, +void *data); + +uint32_t cf_check vpci_guest_mem_bar_read(const struct pci_dev *pdev, +unsigned int reg,void *data);#endif /* __XEN__ */ #endif /* VPCI_PRIVATE_H */
Signature · 1 line
2.56.1
PATCH v5 5/5 vpci: add SR-IOV support for DomUs
In reply to Mykyta PoturaiFrom: Stewart Hildebrand <[email protected]>
Emulate guest BAR register values based on PF BAR values for VFs. This allows creating a guest view of the normal BAR registers and emulates the size and properties as it is done during PCI device enumeration by the guest.
Expose VID/DID to the guest.
Note, that VFs ROM BAR is read-only and is all zeros, but VF may provide access to the PFs ROM via emulation and is not implemented.
Note this only removes one of the gaps on the way to vPCI DomU support; VFs are not usable in DomUs until the rest of it is upstream. The DomU testing behind this patch used out-of-tree vPCI DomU patches.
---v4->v5:
* collect RB
* use _p() for the constant register values
* load sriov_pos after the vf_init_bars() error check
* drop the class/revision handler, as it isn't SR-IOV specific
v3->v4:
* emulate VID/DID again
* add a note to commit message
v2->v3:
* don't emulate VID/DID registers
* replace ifdef with IS_ENABLED
v1->v2:
* remove VF register handlers covered by init_header
* set guest addr unconditionally
--- xen/drivers/vpci/sriov.c | 58 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-)
xen/drivers/vpci/sriov.c +56 -2
@@ -76,6 +76,7 @@ int vpci_vf_init_header(struct pci_dev *vf_pdev)const struct pci_dev *pf_pdev;int rc =0 ;uint16_t ctrl; +unsigned int sriov_pos;ASSERT (rw_is_write_locked (&vf_pdev->domain->pci_lock));ASSERT (vf_pdev->info.is_virtfn);
@@ -87,8 +88,61 @@ int vpci_vf_init_header(struct pci_dev *vf_pdev)if ( rc )return rc; - ctrl =pci_conf_read16 (pf_pdev->sbdf, - pf_pdev->vpci->sriov->pos +PCI_SRIOV_CTRL ); + sriov_pos = pf_pdev->vpci->sriov->pos; + ctrl =pci_conf_read16 (pf_pdev->sbdf, sriov_pos +PCI_SRIOV_CTRL ); + +if (IS_ENABLED (CONFIG_HAS_VPCI_GUEST_SUPPORT ) && + pf_pdev->domain != vf_pdev->domain ) + { +struct vpci_bar *bars = vf_pdev->vpci->header.bars; +/* +* A VF's Vendor and Device ID registers read as 0xffff. The guest has +* no PF to synthesise them from, so take the vendor from the PF and +* the device from its SR-IOV capability. +*/ +uint16_t vid =pci_conf_read16 (pf_pdev->sbdf,PCI_VENDOR_ID ); +uint16_t did =pci_conf_read16 (pf_pdev->sbdf, + sriov_pos +PCI_SRIOV_VF_DID ); + + rc =vpci_add_register (vf_pdev->vpci, vpci_read_val,NULL , +PCI_VENDOR_ID ,2 ,_p (vid)); +if ( rc ) +return rc; + + rc =vpci_add_register (vf_pdev->vpci, vpci_read_val,NULL , +PCI_DEVICE_ID ,2 ,_p (did)); +if ( rc ) +return rc; + +/* Hardcode multi-function device bit to 0 */ + rc =vpci_add_register (vf_pdev->vpci, vpci_read_val,NULL , +PCI_HEADER_TYPE ,1 ,_p (PCI_HEADER_TYPE_NORMAL )); +if ( rc ) +return rc; + +for (unsigned int i =0 ; i <PCI_SRIOV_NUM_BARS ; i++ ) + { +switch ( pf_pdev->vpci->sriov->vf_bars[i].type ) + { +case VPCI_BAR_MEM32 : +case VPCI_BAR_MEM64_LO : +case VPCI_BAR_MEM64_HI : + rc =vpci_add_register (vf_pdev->vpci, vpci_guest_mem_bar_read, + vpci_guest_mem_bar_write, +PCI_BASE_ADDRESS_0 + i *4 ,4 , &bars[i]); +if ( rc ) +return rc; +break ; + +default : + rc =vpci_add_register (vf_pdev->vpci, vpci_read_val,NULL , +PCI_BASE_ADDRESS_0 + i *4 ,4 ,_p (0 )); +if ( rc ) +return rc; +break ; + } + } + }if ( (pf_pdev->domain == vf_pdev->domain) && (ctrl &PCI_SRIOV_CTRL_MSE ) ) {
Signature · 1 line
2.56.1
PATCH v5 3/5 vpci: allow queueing of mapping operations
In reply to Mykyta PoturaiFrom: Stewart Hildebrand <[email protected]>
Introduce vPCI BAR mapping task queue. Store information needed to map/unmap BARs in struct vpci_map_task. Allow queueing of BAR map/unmap operations in a list, thus making it possible to perform multiple p2m operations associated with single PCI config space register access.
The rangesets describing the ranges to be (un)mapped move from struct vpci_bar into the task. They are only live for the duration of the operation, and a single per-device rangeset cannot describe more than one queued operation. Allocate them along with the task and free them once the task has been processed.
This is preparatory work for further changes that need to perform multiple unmap/map operations before returning to guest.
At the moment, only a single operation will be queued. However, when multiple operations are queued, there is a check in modify_bars() to skip BARs already in the requested state that will no longer be accurate because it doesn't take pending operations into account. Remove this check in preparation of upcoming changes.
Amends: 622bdd962822 ("vpci/header: handle p2m range sets per BAR")
Message-ID: <[email protected]>
---v4->v5:
* make vpci_vcpu_init inline
* move vpci_map_task to private
* fix vpi_map_task hole
* fix formatting
* clarify commit message
v3->v4:
* drop "vpci: Use pervcpu ranges for BAR mapping"; the rangesets are now allocated per task here rather than per vcpu, so that patch only added code this one removed again
* non-functional churn split out into "vpci: use a local variable for the BAR rangeset"
v2->v3:
* new patch in this series, borrowed from [1]
[1]: https://patchew.org/Xen/[email protected]/[email protected]/
--- xen/common/domain.c | 2 + xen/drivers/vpci/header.c | 230 +++++++++++++++++++++++-------------- xen/drivers/vpci/private.h | 13 +++ xen/drivers/vpci/vpci.c | 5 - xen/include/xen/vpci.h | 15 ++- 5 files changed, 170 insertions(+), 95 deletions(-)
xen/common/domain.c +2 -0
@@ -508,6 +508,8 @@ struct vcpu *vcpu_create(struct domain *d, unsigned int vcpu_id)if (arch_vcpu_create (v) !=0 ) goto fail_sched; +vpci_vcpu_init (&v->vpci); + d->vcpu[vcpu_id] = v;if ( vcpu_id !=0 ) {
xen/drivers/vpci/header.c +146 -84
411 lines not on this page · raw message
xen/drivers/vpci/private.h +13 -0
@@ -44,6 +44,19 @@ typedef struct {#define REGISTER_VPCI_EXTCAP (name, finit, fclean) \ REGISTER_VPCI_CAPABILITY(PCI_EXT_CAP_ID_##name, name, finit, fclean, true) +struct vpci_map_task { +/* Per-vcpu structure to store state while {un}mapping of PCI BARs. */ +const struct pci_dev *pdev; +struct list_head next; +struct vpci_bar_map { +uint64_t addr; +uint64_t guest_addr; +struct rangeset *mem; + } bars[ARRAY_SIZE (((struct vpci_header *)NULL )->bars)]; +uint16_t cmd; +bool rom_only :1 ; +}; +int __must_check vpci_init_header (struct pci_dev *pdev);int vpci_init_capabilities (struct pci_dev *pdev,bool ext_only);
xen/drivers/vpci/vpci.c +0 -5
@@ -89,8 +89,6 @@ struct vpci_register *vpci_get_register(const struct vpci *vpci,void vpci_deassign_device (struct pci_dev *pdev) { -unsigned int i; -ASSERT (rw_is_write_locked (&pdev->domain->pci_lock));if ( !has_vpci (pdev->domain) || !pdev->vpci )
@@ -116,9 +114,6 @@ void vpci_deassign_device(struct pci_dev *pdev) }spin_unlock (&pdev->vpci->lock); -for ( i =0 ; i <ARRAY_SIZE (pdev->vpci->header.bars); i++ ) -rangeset_destroy (pdev->vpci->header.bars[i].mem); -xfree (pdev->vpci); pdev->vpci =NULL ; }
xen/include/xen/vpci.h +9 -6
@@ -54,7 +54,6 @@ struct vpci {uint64_t guest_addr;uint64_t size;uint64_t resizable_sizes; -struct rangeset *mem;enum {VPCI_BAR_EMPTY ,VPCI_BAR_IO ,
@@ -152,14 +151,16 @@ struct vpci {#endif }; +#ifdef __XEN__struct vpci_vcpu { -/* Per-vcpu structure to store state while {un}mapping of PCI BARs. */ -const struct pci_dev *pdev; -uint16_t cmd; -bool rom_only :1 ; +struct list_head task_queue; }; -#ifdef __XEN__ +static inline void vpci_vcpu_init (struct vpci_vcpu *v) +{ +INIT_LIST_HEAD (&v->task_queue); +} +void vpci_dump_msi (void );/* Arch-specific vPCI MSI helpers. */
@@ -204,6 +205,8 @@ bool vpci_ecam_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int len,#else /* !CONFIG_HAS_VPCI */ struct vpci_vcpu {}; +static inline void vpci_vcpu_init (struct vpci_vcpu *v) { } +static inline int vpci_reinit_ext_capabilities (struct pci_dev *pdev) {return 0 ;
Signature · 1 line
2.56.1
PATCH v5 4/5 vpci: add SR-IOV support for PVH Dom0
In reply to Mykyta PoturaiFrom: Stewart Hildebrand <[email protected]>
This code is expected to only be used by privileged domains, unprivileged domains should not get access to the SR-IOV capability.
Implement a write handler for the SR-IOV control register, so Xen can discover the VFs a PF enables and map their BARs. VF BARs live in the PF's capability and are re-sized on every VF Enable, as the System Page Size register can change them. They decode only while both VF Enable and VF MSE are set.
We need to modify vf_rlen in control_write but we can't do it cleanly from pdev because write callback only accepts const pdev. Moving vf_rlen inside of struct vpci_sriov is also not possible because it is used before vpci init. So pass it to control_write as additional data to not require dropping const inside.
Xen adds the VFs itself: on VF Enable their routing IDs are recorded and config space accesses to them terminated for 100ms, as the spec permits; the first access past that adds the VF. Clearing VF Enable removes them again.
---v4->v5:
* re-check pending VFs under pcidevs_lock to prevent races
* make VF marking all-or-nothing, reject 0 VF stride
* replace ~0 SBDF sentinel with a dedicated atomic flag
* only skip PHYSDEVOP_pci_device_add for VFs when the caller has vPCI
* check is_hardware_domain(pdev->domain) instead of XSM
* drop unlikely(), physfn_vf_bars and a redundant ASSERT()
* limit the CHANGELOG entry to x86 and sort sriov.o in the Makefile
* message, comment and style fixes
v3->v4:
* add VFs in Xen instead of requiring the hardware domain to report them
* drop the emulated command register read for VFs; PCI_COMMAND_MEMORY is read-only 0 on a VF and must not control mapping
* require VF Enable in addition to MSE before mapping
* cache the SR-IOV capability position at init
* address review comments on vf_init_bars(), init_sriov(), cleanup_sriov() and size_vf_bars()
* change SOB order
v2->v3:
* rework adding VFs with multiple mapping operations support
* switch back to no longer adding VFs withing Xen
* use private.h
* style fixes
* fixup cleanup_sriov
* emulate command register read for VFs
v1->v2:
* switch to VF discovery by Xen
* fix sequential vpci_modify_bars calls
* move documentation changes to a separate commit
--- CHANGELOG.md | 2 + SUPPORT.md | 2 - tools/tests/vpci/emul.h | 6 + xen/drivers/passthrough/pci.c | 46 ++++ xen/drivers/pci/physdev.c | 3 + xen/drivers/vpci/Makefile | 1 + xen/drivers/vpci/header.c | 10 +- xen/drivers/vpci/private.h | 14 + xen/drivers/vpci/sriov.c | 506 ++++++++++++++++++++++++++++++++++ xen/drivers/vpci/vpci.c | 39 +++ xen/include/xen/pci.h | 3 + xen/include/xen/vpci.h | 25 +- 12 files changed, 653 insertions(+), 4 deletions(-) create mode 100644 xen/drivers/vpci/sriov.c
CHANGELOG.md +2 -0
@@ -16,6 +16,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) afterwards. ### Added + - On x86: + - PCI SR-IOV is supported for PVH dom0. ### Removed - On Arm:
SUPPORT.md +0 -2
@@ -170,8 +170,6 @@ unexpected behavior or issues on some hardware. At least the following features are missing on a PVH dom0: - * PCI SR-IOV. - * Native NMI forwarding (nmi=dom0 command line option). * MCE handling.
tools/tests/vpci/emul.h +6 -0
@@ -63,6 +63,8 @@ typedef bool spinlock_t;#define read_unlock (l) (*(l) = false)#define write_lock (l) (*(l) = true)#define write_unlock (l) (*(l) = false) +#define pcidevs_lock () +#define pcidevs_unlock () typedef union { uint32_t sbdf;
@@ -101,6 +103,10 @@ typedef union {#define pci_get_pdev (...) (&test_pdev)#define pci_get_ro_map (...) NULL +#define pci_vf_setup_pending (...) false +#define vpci_sriov_add_vf (...) false +#define vpci_sriov_drop_vfs (...) +#define test_bit (...) false/* Dummy native helpers. Writes are ignored, reads return 1's. */
xen/drivers/passthrough/pci.c +46 -0
64 lines not on this page · raw message
xen/drivers/pci/physdev.c +3 -0
@@ -26,6 +26,9 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if (copy_from_guest (&add, arg,1 ) !=0 )break ; +if ( add.flags &XEN_PCI_DEV_VIRTFN &&has_vpci (current->domain) ) +return 0 ; + pdev_info.is_extfn = (add.flags &XEN_PCI_DEV_EXTFN );if ( add.flags &XEN_PCI_DEV_VIRTFN ) {
xen/drivers/vpci/Makefile +1 -0
@@ -1,3 +1,4 @@ obj-y += cap.o obj-y += vpci.o header.o rebar.o obj-$(CONFIG_HAS_PCI_MSI) += msi.o msix.o +obj-y += sriov.o
xen/drivers/vpci/header.c +9 -1
@@ -626,8 +626,13 @@ static void cf_check cmd_write(* Let Dom0 play with all the bits directly except for the memory * decoding one. Bits that are not allowed for DomU are already * handled above and by the rsvdp_mask. +* +* PCI_COMMAND_MEMORY is hardwired to 0 for VFs, so for the hardware domain +* it must not control the mapping: VF memory space is enabled by MSE in the +* PF's SR-IOV control register. */ -if ( header->bars_mapped != !!(cmd &PCI_COMMAND_MEMORY ) ) +if ( !(pdev->info.is_virtfn &&is_hardware_domain (pdev->domain)) && + header->bars_mapped != !!(cmd &PCI_COMMAND_MEMORY ) )/* * Ignore the error. No memory has been added or removed from the p2m * (because the actual p2m changes are deferred in defer_map) and the
@@ -888,6 +893,9 @@ int vpci_init_header(struct pci_dev *pdev) header->guest_cmd = cmd; +if ( pdev->info.is_virtfn ) +return vpci_vf_init_header (pdev); +/* Disable memory decoding before sizing. */ if ( !is_hwdom || (cmd &PCI_COMMAND_MEMORY ) )pci_conf_write16 (pdev->sbdf,PCI_COMMAND , cmd & ~PCI_COMMAND_MEMORY );
xen/drivers/vpci/private.h +14 -0
@@ -58,6 +58,7 @@ struct vpci_map_task { };int __must_check vpci_init_header (struct pci_dev *pdev); +int __must_check vpci_vf_init_header (struct pci_dev *pdev);int vpci_init_capabilities (struct pci_dev *pdev,bool ext_only);void vpci_cleanup_capabilities (struct pci_dev *pdev,bool ext_only);
@@ -154,6 +155,19 @@ static inline size_t vmsix_table_size(const struct vpci *vpci, unsigned int nr)/* Map/unmap the BARs of a vPCI device. */ int vpci_modify_bars (const struct pci_dev *pdev,uint16_t cmd,bool rom_only); +/* +* Handle a config space access to a VF which is enabled in hardware but not +* known to Xen yet. Returns false while the VF need not respond, in which +* case the access has to be terminated. +*/ +bool vpci_sriov_add_vf (pci_sbdf_t sbdf); + +/* +* Remove the VFs of the PF whose VF Enable the config space write in progress +* has cleared. Same locking requirement as above. +*/ +void vpci_sriov_drop_vfs (void ); + void cf_check vpci_guest_mem_bar_write(const struct pci_dev *pdev,unsigned int reg,uint32_t val,void *data);
xen/drivers/vpci/sriov.c new file +506 -0
506 lines not on this page · raw message
xen/drivers/vpci/vpci.c +39 -0
@@ -481,6 +481,24 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size)return data; } +/* +* Check whether a VF is still to be added before the guest accesses it. +* pcidevs_lock() protects against parallel additions of the same VF; the +* outer test avoids taking it on every vPCI access. The lock is +* recursive, so pci_add_device() acquiring it again is fine. +*/ +if (is_hardware_domain (d) &&pci_vf_setup_pending (sbdf) ) + { +bool done; + +pcidevs_lock (); + done =pci_vf_setup_pending (sbdf) && !vpci_sriov_add_vf (sbdf); +pcidevs_unlock (); + +if ( done ) +return data; + } +/* * Find the PCI dev matching the address, which for hwdom also requires * consulting DomXEN. Passthrough everything that's not trapped.
@@ -602,6 +620,24 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,return ; } +/* +* Check whether a VF is still to be added before the guest accesses it. +* pcidevs_lock() protects against parallel additions of the same VF; the +* outer test avoids taking it on every vPCI access. The lock is +* recursive, so pci_add_device() acquiring it again is fine. +*/ +if (is_hardware_domain (d) &&pci_vf_setup_pending (sbdf) ) + { +bool done; + +pcidevs_lock (); + done =pci_vf_setup_pending (sbdf) && !vpci_sriov_add_vf (sbdf); +pcidevs_unlock (); + +if ( done ) +return ; + } +/* * Find the PCI dev matching the address, which for hwdom also requires * consulting DomXEN. Passthrough everything that's not trapped.
@@ -675,6 +711,9 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,/* Tailing gap, write the remaining. */ vpci_write_hw (sbdf, reg + data_offset, size - data_offset, data >> (data_offset *8 )); + +/* Only once every lock has been released, as removal acquires them. */ +vpci_sriov_drop_vfs (); }/* Helper function to check an access size and alignment on vpci space. */
xen/include/xen/pci.h +3 -0
@@ -229,6 +229,9 @@ void setup_hwdom_pci_devices(struct domain *d,int pci_release_devices (struct domain *d);int pci_add_segment (u16 seg);const unsigned long *pci_get_ro_map (u16 seg); +int pci_vf_setup_begin (pci_sbdf_t sbdf); +void pci_vf_setup_end (pci_sbdf_t sbdf); +bool pci_vf_setup_pending (pci_sbdf_t sbdf);int pci_add_device (u16 seg,u8 bus,u8 devfn,const struct pci_dev_info *info,nodeid_t node);int pci_remove_device (u16 seg,u8 bus,u8 devfn);
xen/include/xen/vpci.h +24 -1
@@ -4,6 +4,7 @@#ifdef CONFIG_HAS_VPCI #include <xen/pci.h> +#include <xen/time.h> #include <xen/types.h> #include <xen/list.h>
@@ -82,7 +83,6 @@ struct vpci {* upon to know whether BARs are mapped into the guest p2m. */ bool bars_mapped : 1; -/* FIXME: currently there's no support for SR-IOV. */ } header;/* MSI data. */
@@ -143,6 +143,22 @@ struct vpci { unsigned int nbars:3; } rebar; +struct vpci_sriov { +/* PF only */ +struct vpci_bar vf_bars[PCI_SRIOV_NUM_BARS ]; +/* +* When the VFs which are enabled but not added to Xen yet are +* guaranteed to respond to config space accesses. Zero if there are +* none. +*/ +s_time_t vfs_ready; +/* Routing ID offset of the first VF, and stride between VFs. */ +unsigned int offset; +unsigned int stride; +unsigned int num_vfs; +unsigned int pos; + } *sriov; +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT /* Guest SBDF of the device. */ #define INVALID_GUEST_SBDF ((pci_sbdf_t){ .sbdf = ~0U })
@@ -154,6 +170,13 @@ struct vpci {#ifdef __XEN__struct vpci_vcpu {struct list_head task_queue; +/* +* PF whose VFs have been disabled by the config space write in progress, +* and which Xen has to remove once the PCI locks have been dropped. Only +* valid while drop_vfs is set. +*/ +pci_sbdf_t drop_vfs_pf; +bool drop_vfs; };static inline void vpci_vcpu_init (struct vpci_vcpu *v)
Signature · 1 line
2.56.1
Re: [PATCH v5 2/5] vpci: use a local variable for the BAR rangeset
In reply to Mykyta PoturaiQuoted 7 lines · Mykyta Poturai
From: Mykyta Poturai <[email protected]>
In the BAR (un)mapping paths latch the rangeset of the BAR being processed into a local variable, instead of dereferencing struct
vpci_baron every use. Preparatory work for moving the rangesets out of structvpci_bar.
Quoted 8 lines
No functional change.
Signed-off-by Stewart Hildebrand <[email protected]>Signed-off-by Mykyta Poturai <[email protected]>Reviewed-by Jason Andryuk <[email protected]>---
v4->v5:
* change SOB order
And now From: and first S-o-b: don't match. Who's the original author?
Jan