Skip to contentSkip to conversation

vpci: PCI config space emulation

PATCH Superseded Roger Pau Monne 4 participants 24 messages Jan 23, 2018 – Mar 2, 2018

v8 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v8 cover vpci: PCI config space emulation

Hello,

The following series contain an implementation of handlers for the PCI configuration space inside of Xen. This allows Xen to detect accesses to the PCI configuration space and react accordingly.

Why is this needed? IMHO, there are two main points of doing all this emulation inside of Xen, the first one is to prevent adding a bunch of duplicated Xen PV specific code to each OS we want to support in PVH mode. This just promotes Xen code duplication amongst OSes, which leads to a higher maintainership burden.

The second reason would be that this code (or it's functionality to be more precise) already exists in QEMU (and pciback to a degree), and it's code that we already support and maintain. By moving it into the hypervisor itself every guest type can make use of it, and should be shared between them all. I know that the code in this series is not yet suitable for DomU HVM guests in it's current state, but it should be in due time.

As usual, each patch contains a changeset summary between versions, I'm not going to copy the list of changes here.

Patch 1 is a pre-patch to allow internal PIO handlers return RETRY. Patch 2 modifies a function to decode a PCI IO port access into pci_sbdf_t and register (which is shared with the ioreq code). Patch 3 implements the generic handlers for accesses to the PCI configuration space together with a minimal user-space test harness that I've used during development. Currently a per-device linked list is used in order to store the list of handlers, and they are sorted based on their offset inside of the configuration space. Patch 2 also adds the x86 port IO traps and wires them into the newly introduced vPCI dispatchers. Patch 4 and 5 adds handlers for the MMCFG areas (as found on the MMCFG ACPI table). Patches 6, 7 and 8 are mostly code moment/refactoring in order to implement support for BAR mapping in patch 9. Finally patches 10 and 12 add support for trapping accesses to the MSI and MSI-X capabilities respectively, so that interrupts are properly setup on behalf of Dom0.

The branch containing the patches can be found at:

git://xenbits.xen.org/people/royger/xen.git vpci_v8

Note that this is only safe to use for the hardware domain (that's trusted), any non-trusted domain will need a lot more of traps before it can freely access the PCI configuration space.

Thanks, Roger.

Roger Pau Monne (11):
vpci: introduce basic handlers to trap accesses to the PCI config
space
x86/mmcfg: add handlers for the PVH Dom0 MMCFG areas x86/physdev: enable PHYSDEVOP_pci_mmcfg_reserved for PVH Dom0
pci: split code to size BARs from pci_add_device
pci: add support to size ROM BARs to pci_size_mem_bar
xen: introduce rangeset_consume_ranges
vpci/bars: add handlers to map the BARs
x86/pt: mask MSI vectors on unbind
vpci/msi: add MSI handlers
vpci: add a priority parameter to the vPCI register initializer vpci/msix: add MSI-X handlers

 .gitignore                        |   3 +
 tools/libxl/libxl_x86.c           |   2 +-
 tools/tests/Makefile              |   1 +
 tools/tests/vpci/Makefile         |  37 +++
 tools/tests/vpci/emul.h           | 134 ++++++++++
 tools/tests/vpci/main.c           | 309 ++++++++++++++++++++++
 xen/arch/arm/xen.lds.S            |  14 +
 xen/arch/x86/domain.c             |  19 +-
 xen/arch/x86/hvm/dom0_build.c     |  23 +-
 xen/arch/x86/hvm/hvm.c            |   7 +
 xen/arch/x86/hvm/hypercall.c      |   5 +
 xen/arch/x86/hvm/io.c             | 293 +++++++++++++++++++++
 xen/arch/x86/hvm/ioreq.c          |   4 +
 xen/arch/x86/hvm/vmsi.c           | 228 ++++++++++++++++
 xen/arch/x86/msi.c                |   3 +
 xen/arch/x86/physdev.c            |  11 +
 xen/arch/x86/setup.c              |   3 +-
 xen/arch/x86/x86_64/mmconfig.h    |   4 -
 xen/arch/x86/xen.lds.S            |  14 +
 xen/common/rangeset.c             |  28 ++
 xen/drivers/Makefile              |   2 +-
 xen/drivers/passthrough/io.c      |  15 ++
 xen/drivers/passthrough/pci.c     | 107 +++++---
 xen/drivers/vpci/Makefile         |   1 +
 xen/drivers/vpci/header.c         | 539 ++++++++++++++++++++++++++++++++++++++
 xen/drivers/vpci/msi.c            | 338 ++++++++++++++++++++++++
 xen/drivers/vpci/msix.c           | 464 ++++++++++++++++++++++++++++++++
 xen/drivers/vpci/vpci.c           | 473 +++++++++++++++++++++++++++++++++
 xen/include/asm-x86/domain.h      |   1 +
 xen/include/asm-x86/hvm/domain.h  |   7 +
 xen/include/asm-x86/hvm/io.h      |  20 ++
 xen/include/asm-x86/msi.h         |   3 +
 xen/include/asm-x86/pci.h         |   6 +
 xen/include/public/arch-x86/xen.h |   5 +-
 xen/include/xen/irq.h             |   1 +
 xen/include/xen/pci.h             |   9 +
 xen/include/xen/pci_regs.h        |   8 +
 xen/include/xen/rangeset.h        |  10 +
 xen/include/xen/sched.h           |   4 +
 xen/include/xen/vpci.h            | 219 ++++++++++++++++
 40 files changed, 3325 insertions(+), 49 deletions(-)
 create mode 100644 tools/tests/vpci/Makefile
 create mode 100644 tools/tests/vpci/emul.h
 create mode 100644 tools/tests/vpci/main.c
 create mode 100644 xen/drivers/vpci/Makefile
 create mode 100644 xen/drivers/vpci/header.c
 create mode 100644 xen/drivers/vpci/msi.c
 create mode 100644 xen/drivers/vpci/msix.c
 create mode 100644 xen/drivers/vpci/vpci.c
 create mode 100644 xen/include/xen/vpci.h
Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH v8 1/11 vpci: introduce basic handlers to trap accesses to the PCI config space

This functionality is going to reside in vpci.c (and the corresponding vpci.h header), and should be arch-agnostic. The handlers introduced in this patch setup the basic functionality required in order to trap accesses to the PCI config space, and allow decoding the address and finding the corresponding handler that should handle the access (although no handlers are implemented).

Note that the traps to the PCI IO ports registers (0xcf8/0xcfc) are setup inside of a x86 HVM file, since that's not shared with other arches.

A new XEN_X86_EMU_VPCI x86 domain flag is added in order to signal Xen whether a domain should use the newly introduced vPCI handlers, this is only enabled for PVH Dom0 at the moment.

A very simple user-space test is also provided, so that the basic functionality of the vPCI traps can be asserted. This has been proven quite helpful during development, since the logic to handle partial accesses or accesses that expand across multiple registers is not trivial.

The handlers for the registers are added to a linked list that's keep sorted at all times. Both the read and write handlers support accesses that expand across multiple emulated registers and contain gaps not emulated.

---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Paul Durrant <[email protected]>
---
Changes since v7:
- Constify d in vpci_portio_read.
- ASSERT the correctness of the address in the read/write handlers.
- Add newlines between non-fallthrough case statements.

Changes since v6:
- Align the vpci handlers in the linker script.
- Switch add/remove register functions to take a vpci parameter instead of a pci_dev.
- Expand comment of merge_result.
- Return X86EMUL_UNHANDLEABLE if accessing cfc and cf8 is disabled.

Changes since v5:
- Use a spinlock per pci device.
- Use the recently introduced pci_sbdf_t type.
- Fix test harness to use the right handler type and the newly introduced lock.
- Move the position of the vpci sections in the linker scripts.
- Constify domain and pci_dev in vpci_{read/write}.
- Fix typos in comments.
- Use _XEN_VPCI_H_ as header guard.

Changes since v4:
* User-space test harness:
- Do not redirect the output of the test.
- Add main.c and emul.h as dependencies of the Makefile target.
- Use the same rule to modify the vpci and list headers.
- Remove underscores from local macro variables.
- Add _check suffix to the test harness multiread function.
- Change the value written by every different size in the multiwrite test.
- Use { } to initialize the r16 and r20 arrays (instead of { 0 }).
- Perform some of the read checks with the local variable directly.
- Expand some comments.
- Implement a dummy rwlock.
* Hypervisor code:
- Guard the linker script changes with CONFIG_HAS_PCI.
- Rename vpci_access_check to vpci_access_allowed and make it return bool.
- Make hvm_pci_decode_addr return the register as return value.
- Use ~3 instead of 0xfffc to remove the register offset when checking accesses to IO ports.
- s/head/prev in vpci_add_register.
- Add parentheses around & in vpci_add_register.
- Fix register removal.
- Change the BUGs in vpci_{read/write}_hw helpers to ASSERT_UNREACHABLE.
- Make merge_result static and change the computation of the mask to avoid using a uint64_t.
- Modify vpci_read to only read from hardware the not-emulated gaps.
- Remove the vpci_val union and use a uint32_t instead.
- Change handler read type to return a uint32_t instead of modifying a variable passed by reference.
- Constify the data opaque parameter of read handlers.
- Change the size parameter of the vpci_{read/write} functions to unsigned int.
- Place the array of initialization handlers in init.rodata or .rodata depending on whether late-hwdom is enabled.
- Remove the pci_devs lock, assume the Dom0 is well behaved and won't remove the device while trying to access it.
- Change the recursive spinlock into a rw lock for performance reasons.

Changes since v3:
* User-space test harness:
- Fix spaces in container_of macro.
- Implement a dummy locking functions.
- Remove 'current' macro make current a pointer to the statically allocated vpcu.
- Remove unneeded parentheses in the pci_conf_readX macros.
- Fix the name of the write test macro.
- Remove the dummy EXPORT_SYMBOL macro (this was needed by the RB code only).
- Import the max macro.
- Test all possible read/write size combinations with all possible emulated register sizes.
- Introduce a test for register removal.
* Hypervisor code:
- Use a sorted list in order to store the config space handlers.
- Remove some unneeded 'else' branches.
- Make the IO port handlers always return X86EMUL_OKAY, and set the data to all 1's in case of read failure (write are simply ignored).
- In hvm_select_ioreq_server reuse local variables when calling XEN_DMOP_PCI_SBDF.
- Store the pointers to the initialization functions in the .rodata section.
- Do not ignore the return value of xen_vpci_add_handlers in setup_one_hwdom_device.
- Remove the vpci_init macro.
- Do not hide the pointers inside of the vpci_{read/write}_t typedefs.
- Rename priv_data to private in vpci_register.
- Simplify checking for register overlap in vpci_register_cmp.
- Check that the offset and the length match before removing a register in xen_vpci_remove_register.
- Make vpci_read_hw return a value rather than storing it in a pointer passed by parameter.
- Handler dispatcher functions vpci_{read/write} no longer return an error code, errors on reads/writes should be treated like hardware (writes ignored, reads return all 1's or garbage).
- Make sure pcidevs is locked before calling pci_get_pdev_by_domain.
- Use a recursive spinlock for the vpci lock, so that spin_is_locked checks that the current CPU is holding the lock.
- Make the code less error-chatty by removing some of the printk's.
- Pass the slot and the function as separate parameters to the handler dispatchers (instead of passing devfn).
- Allow handlers to be registered with either a read or write function only, the missing handler will be replaced by a dummy handler (writes ignored, reads return 1's).
- Introduce PCI_CFG_SPACE_* defines from Linux.
- Simplify the handler dispatchers by removing the recursion, now the dispatchers iterate over the list of sorted handlers and call them in order.
- Remove the GENMASK_BYTES, SHIFT_RIGHT_BYTES and ADD_RESULT macros, and instead provide a merge_result function in order to merge a register output into a partial result.
- Rename the fields of the vpci_val union to u8/u16/u32.
- Remove the return values from the read/write handlers, errors should be handled internally and signaled as would be done on native hardware.
- Remove the usage of the GENMASK macro.

Changes since v2:
- Generalize the PCI address decoding and use it for IOREQ code also.

Changes since v1:
- Allow access to cross a word-boundary.
- Add locking.
- Add cleanup to xen_vpci_add_handlers in case of failure.

---
 .gitignore                        |   3 +
 tools/libxl/libxl_x86.c           |   2 +-
 tools/tests/Makefile              |   1 +
 tools/tests/vpci/Makefile         |  37 +++
 tools/tests/vpci/emul.h           | 133 +++++++++++
 tools/tests/vpci/main.c           | 309 +++++++++++++++++++++++++
 xen/arch/arm/xen.lds.S            |  14 ++
 xen/arch/x86/domain.c             |  19 +-
 xen/arch/x86/hvm/hvm.c            |   2 +
 xen/arch/x86/hvm/io.c             | 105 +++++++++
 xen/arch/x86/setup.c              |   3 +-
 xen/arch/x86/xen.lds.S            |  14 ++
 xen/drivers/Makefile              |   2 +-
 xen/drivers/passthrough/pci.c     |  10 +-
 xen/drivers/vpci/Makefile         |   1 +
 xen/drivers/vpci/vpci.c           | 459 ++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/domain.h      |   1 +
 xen/include/asm-x86/hvm/io.h      |   3 +
 xen/include/public/arch-x86/xen.h |   5 +-
 xen/include/xen/pci.h             |   3 +
 xen/include/xen/pci_regs.h        |   8 +
 xen/include/xen/vpci.h            |  53 +++++
 22 files changed, 1177 insertions(+), 10 deletions(-)
 create mode 100644 tools/tests/vpci/Makefile
 create mode 100644 tools/tests/vpci/emul.h
 create mode 100644 tools/tests/vpci/main.c
 create mode 100644 xen/drivers/vpci/Makefile
 create mode 100644 xen/drivers/vpci/vpci.c
 create mode 100644 xen/include/xen/vpci.h
.gitignore +3 -0
index f6cc61a701..f7c8f821b8 100644
@@ -249,6 +249,9 @@ tools/tests/regression/build/*
 tools/tests/regression/downloads/*
 tools/tests/mem-sharing/memshrtool
 tools/tests/mce-test/tools/xen-mceinj
+tools/tests/vpci/list.h
+tools/tests/vpci/vpci.[hc]
+tools/tests/vpci/test_vpci
 tools/xcutils/lsevtchn
 tools/xcutils/readnotes
 tools/xenbackendd/_paths.h
tools/libxl/libxl_x86.c +1 -1
index 5f91fe4f92..8f6a5bc6f2 100644
@@ -9,7 +9,7 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
 {
     switch(d_config->c_info.type) {
     case LIBXL_DOMAIN_TYPE_HVM:
-        xc_config->emulation_flags = XEN_X86_EMU_ALL;
+        xc_config->emulation_flags = (XEN_X86_EMU_ALL & ~XEN_X86_EMU_VPCI);
         break;
     case LIBXL_DOMAIN_TYPE_PVH:
         if (libxl_defbool_val(d_config->b_info.apic))
tools/tests/Makefile +1 -0
index 7162945121..f6942a93fb 100644
@@ -13,6 +13,7 @@ endif
 SUBDIRS-$(CONFIG_X86) += x86_emulator
 SUBDIRS-y += xen-access
 SUBDIRS-y += xenstore
+SUBDIRS-$(CONFIG_HAS_PCI) += vpci
 
 .PHONY: all clean install distclean uninstall
 all clean distclean: %: subdirs-%
tools/tests/vpci/Makefile new file +37 -0
new file mode 100644
index 0000000000..e45fcb5cd9
@@ -0,0 +1,37 @@
+XEN_ROOT=$(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+TARGET := test_vpci
+
+.PHONY: all
+all: $(TARGET)
+
+.PHONY: run
+run: $(TARGET)
+	./$(TARGET)
+
+$(TARGET): vpci.c vpci.h list.h main.c emul.h
+	$(HOSTCC) -g -o $@ vpci.c main.c
+
+.PHONY: clean
+clean:
+	rm -rf $(TARGET) *.o *~ vpci.h vpci.c list.h
+
+.PHONY: distclean
+distclean: clean
+
+.PHONY: install
+install:
+
+vpci.c: $(XEN_ROOT)/xen/drivers/vpci/vpci.c
+	# Trick the compiler so it doesn't complain about missing symbols
+	sed -e '/#include/d' \
+	    -e '1s;^;#include "emul.h"\
+	             vpci_register_init_t *const __start_vpci_array[1]\;\
+	             vpci_register_init_t *const __end_vpci_array[1]\;\
+	             ;' <$< >$@
+
+list.h: $(XEN_ROOT)/xen/include/xen/list.h
+vpci.h: $(XEN_ROOT)/xen/include/xen/vpci.h
+list.h vpci.h:
+	sed -e '/#include/d' <$< >$@
tools/tests/vpci/emul.h new file +133 -0

133 lines not on this page · raw message

tools/tests/vpci/main.c new file +309 -0

309 lines not on this page · raw message

xen/arch/arm/xen.lds.S +14 -0
index c9b9546435..98b82680c6 100644
@@ -65,6 +65,13 @@ SECTIONS
        __param_start = .;
        *(.data.param)
        __param_end = .;
+
+#if defined(CONFIG_HAS_PCI) && defined(CONFIG_LATE_HWDOM)
+       . = ALIGN(POINTER_ALIGN);
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
   } :text
 
 #if defined(BUILD_ID)
@@ -173,6 +180,13 @@ SECTIONS
        *(.init_array)
        *(SORT(.init_array.*))
        __ctors_end = .;
+
+#if defined(CONFIG_HAS_PCI) && !defined(CONFIG_LATE_HWDOM)
+       . = ALIGN(POINTER_ALIGN);
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
   } :text
   __init_end_efi = .;
   . = ALIGN(STACK_SIZE);
xen/arch/x86/domain.c +15 -4
index 643628c651..7d056cdae6 100644
@@ -402,11 +402,22 @@ static bool emulation_flags_ok(const struct domain *d, uint32_t emflags)
     if ( is_hvm_domain(d) )
     {
         if ( is_hardware_domain(d) &&
-             emflags != (XEN_X86_EMU_LAPIC|XEN_X86_EMU_IOAPIC) )
-            return false;
-        if ( !is_hardware_domain(d) && emflags &&
-             emflags != XEN_X86_EMU_ALL && emflags != XEN_X86_EMU_LAPIC )
+             emflags != (XEN_X86_EMU_LAPIC|XEN_X86_EMU_IOAPIC|
+                         XEN_X86_EMU_VPCI) )
             return false;
+        if ( !is_hardware_domain(d) )
+        {
+            switch ( emflags )
+            {
+            case XEN_X86_EMU_ALL & ~XEN_X86_EMU_VPCI:
+            case XEN_X86_EMU_LAPIC:
+            case 0:
+                break;
+
+            default:
+                return false;
+            }
+        }
     }
     else if ( emflags != 0 && emflags != XEN_X86_EMU_PIT )
     {
xen/arch/x86/hvm/hvm.c +2 -0
index cee2385a60..41b49038ac 100644
@@ -36,6 +36,7 @@
 #include <xen/rangeset.h>
 #include <xen/monitor.h>
 #include <xen/warning.h>
+#include <xen/vpci.h>
 #include <asm/shadow.h>
 #include <asm/hap.h>
 #include <asm/current.h>
@@ -633,6 +634,7 @@ int hvm_domain_initialise(struct domain *d, unsigned long domcr_flags,
         d->arch.hvm_domain.io_bitmap = hvm_io_bitmap;
 
     register_g2m_portio_handler(d);
+    register_vpci_portio_handler(d);
 
     hvm_ioreq_init(d);
 
xen/arch/x86/hvm/io.c +105 -0

117 lines not on this page · raw message

xen/arch/x86/setup.c +2 -1
index 9407247955..f9e46c6de0 100644
@@ -1634,7 +1634,8 @@ void __init noreturn __start_xen(unsigned long mbi_p)
         domcr_flags |= DOMCRF_hvm |
                        ((hvm_funcs.hap_supported && !opt_dom0_shadow) ?
                          DOMCRF_hap : 0);
-        config.emulation_flags = XEN_X86_EMU_LAPIC|XEN_X86_EMU_IOAPIC;
+        config.emulation_flags = XEN_X86_EMU_LAPIC|XEN_X86_EMU_IOAPIC|
+                                 XEN_X86_EMU_VPCI;
     }
 
     /* Create initial domain 0. */
xen/arch/x86/xen.lds.S +14 -0
index 095298048f..96e53f68a8 100644
@@ -128,6 +128,13 @@ SECTIONS
        __param_start = .;
        *(.data.param)
        __param_end = .;
+
+#if defined(CONFIG_HAS_PCI) && defined(CONFIG_LATE_HWDOM)
+       . = ALIGN(POINTER_ALIGN);
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
   } :text
 
 #if defined(CONFIG_PVH_GUEST) && !defined(EFI)
@@ -219,6 +226,13 @@ SECTIONS
        *(.init_array)
        *(SORT(.init_array.*))
        __ctors_end = .;
+
+#if defined(CONFIG_HAS_PCI) && !defined(CONFIG_LATE_HWDOM)
+       . = ALIGN(POINTER_ALIGN);
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
   } :text
 
   . = ALIGN(SECTION_ALIGN);
xen/drivers/Makefile +1 -1
index 19391802a8..d51c766453 100644
@@ -1,6 +1,6 @@
 subdir-y += char
 subdir-$(CONFIG_HAS_CPUFREQ) += cpufreq
-subdir-$(CONFIG_HAS_PCI) += pci
+subdir-$(CONFIG_HAS_PCI) += pci vpci
 subdir-$(CONFIG_HAS_PASSTHROUGH) += passthrough
 subdir-$(CONFIG_ACPI) += acpi
 subdir-$(CONFIG_VIDEO) += video
xen/drivers/passthrough/pci.c +8 -2
index 469dfc6c3d..519993d536 100644
@@ -31,6 +31,7 @@
 #include <xen/radix-tree.h>
 #include <xen/softirq.h>
 #include <xen/tasklet.h>
+#include <xen/vpci.h>
 #include <xsm/xsm.h>
 #include <asm/msi.h>
 #include "ats.h"
@@ -1052,10 +1053,10 @@ static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt,
                                                 struct pci_dev *pdev)
 {
     u8 devfn = pdev->devfn;
+    int err;
 
     do {
-        int err = ctxt->handler(devfn, pdev);
-
+        err = ctxt->handler(devfn, pdev);
         if ( err )
         {
             printk(XENLOG_ERR "setup %04x:%02x:%02x.%u for d%d failed (%d)\n",
@@ -1067,6 +1068,11 @@ static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt,
         devfn += pdev->phantom_stride;
     } while ( devfn != pdev->devfn &&
               PCI_SLOT(devfn) == PCI_SLOT(pdev->devfn) );
+
+    err = vpci_add_handlers(pdev);
+    if ( err )
+        printk(XENLOG_ERR "setup of vPCI for d%d failed: %d\n",
+               ctxt->d->domain_id, err);
 }
 
 static int __hwdom_init _setup_hwdom_pci_devices(struct pci_seg *pseg, void *arg)
xen/drivers/vpci/Makefile new file +1 -0
new file mode 100644
index 0000000000..840a906470
@@ -0,0 +1 @@
+obj-y += vpci.o
xen/drivers/vpci/vpci.c new file +459 -0

459 lines not on this page · raw message

xen/include/asm-x86/domain.h +1 -0
index 4679d5477d..10f1db5a21 100644
@@ -434,6 +434,7 @@ struct arch_domain
 #define has_vpit(d)        (!!((d)->arch.emulation_flags & XEN_X86_EMU_PIT))
 #define has_pirq(d)        (!!((d)->arch.emulation_flags & \
                             XEN_X86_EMU_USE_PIRQ))
+#define has_vpci(d)        (!!((d)->arch.emulation_flags & XEN_X86_EMU_VPCI))
 
 #define has_arch_pdevs(d)    (!list_empty(&(d)->arch.pdev_list))
 
xen/include/asm-x86/hvm/io.h +3 -0
index 707665fbba..ff0bea5d53 100644
@@ -160,6 +160,9 @@ unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr,
  */
 void register_g2m_portio_handler(struct domain *d);
 
+/* HVM port IO handler for vPCI accesses. */
+void register_vpci_portio_handler(struct domain *d);
+
 #endif /* __ASM_X86_HVM_IO_H__ */
 
 
xen/include/public/arch-x86/xen.h +4 -1
index 3b0b1d6073..69ee4bc40d 100644
@@ -294,12 +294,15 @@ struct xen_arch_domainconfig {
 #define XEN_X86_EMU_PIT             (1U<<_XEN_X86_EMU_PIT)
 #define _XEN_X86_EMU_USE_PIRQ       9
 #define XEN_X86_EMU_USE_PIRQ        (1U<<_XEN_X86_EMU_USE_PIRQ)
+#define _XEN_X86_EMU_VPCI           10
+#define XEN_X86_EMU_VPCI            (1U<<_XEN_X86_EMU_VPCI)
 
 #define XEN_X86_EMU_ALL             (XEN_X86_EMU_LAPIC | XEN_X86_EMU_HPET |  \
                                      XEN_X86_EMU_PM | XEN_X86_EMU_RTC |      \
                                      XEN_X86_EMU_IOAPIC | XEN_X86_EMU_PIC |  \
                                      XEN_X86_EMU_VGA | XEN_X86_EMU_IOMMU |   \
-                                     XEN_X86_EMU_PIT | XEN_X86_EMU_USE_PIRQ)
+                                     XEN_X86_EMU_PIT | XEN_X86_EMU_USE_PIRQ |\
+                                     XEN_X86_EMU_VPCI)
     uint32_t emulation_flags;
 };
 
xen/include/xen/pci.h +3 -0
index dd5ec43a70..b7a6abfc53 100644
@@ -112,6 +112,9 @@ struct pci_dev {
 #define PT_FAULT_THRESHOLD 10
     } fault;
     u64 vf_rlen[6];
+
+    /* Data for vPCI. */
+    struct vpci *vpci;
 };
 
 #define for_each_pdev(domain, pdev) \
xen/include/xen/pci_regs.h +8 -0
index ecd6124d91..cc4ee3b83e 100644
@@ -22,6 +22,14 @@
 #ifndef LINUX_PCI_REGS_H
 #define LINUX_PCI_REGS_H
 
+/*
+ * Conventional PCI and PCI-X Mode 1 devices have 256 bytes of
+ * configuration space.  PCI-X Mode 2 and PCIe devices have 4096 bytes of
+ * configuration space.
+ */
+#define PCI_CFG_SPACE_SIZE	256
+#define PCI_CFG_SPACE_EXP_SIZE	4096
+
 /*
  * Under PCI, each device has 256 bytes of configuration address space,
  * of which the first 64 bytes are standardized as follows:
xen/include/xen/vpci.h new file +53 -0

53 lines not on this page · raw message

Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

Re: [PATCH v8 01/11] vpci: introduce basic handlers to trap accesses to the PCI config space

Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:
xen/arch/arm/xen.lds.S +7 -0
index c9b9546435..98b82680c6 100644
@@ -65,6 +65,13 @@ SECTIONS
        __param_start = .;
        *(.data.param)
        __param_end = .;
+
+#if defined(CONFIG_HAS_PCI) && defined(CONFIG_LATE_HWDOM)
+       . = ALIGN(POINTER_ALIGN);
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
   } :text
 
 #if defined(BUILD_ID)
@@ -173,6 +180,13 @@ SECTIONS
        *(.init_array)
        *(SORT(.init_array.*))
        __ctors_end = .;

xen/arch/arm/xen.lds.S SECTIONS · line 192

Quoted 10 lines
xen/arch/arm/xen.lds.S +7 -0
+
+#if defined(CONFIG_HAS_PCI) && !defined(CONFIG_LATE_HWDOM)
+       . = ALIGN(POINTER_ALIGN);
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
   } :text
   __init_end_efi = .;
   . = ALIGN(STACK_SIZE);

Sadly you forgot to Cc the ARM maintainers. Stefano, Julien, the above would require your ack.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw

Re: [PATCH v8 01/11] vpci: introduce basic handlers to trap accesses to the PCI config space

Hi,

Quoted 11 lines · Jan Beulich

xen/arch/arm/xen.lds.S SECTIONS · line 69

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:
xen/arch/arm/xen.lds.S +2 -0
index c9b9546435..98b82680c6 100644
@@ -65,6 +65,13 @@ SECTIONS
         __param_start = .;
         *(.data.param)
         __param_end = .;
+
+#if defined(CONFIG_HAS_PCI) && defined(CONFIG_LATE_HWDOM)

Why this is conditional to CONFIG_LATE_HWDOM?

Also, any reason to impose VPCI when CONFIG_HAS_PCI is set and not introduce another config? The new config would help to add support for PCI in steps in other architectures.

Quoted 28 lines
Quoted 12 lines
xen/arch/arm/xen.lds.S +5 -0
+       . = ALIGN(POINTER_ALIGN);
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
    } :text
 
  #if defined(BUILD_ID)
@@ -173,6 +180,13 @@ SECTIONS
         *(.init_array)
         *(SORT(.init_array.*))
         __ctors_end = .;

xen/arch/arm/xen.lds.S SECTIONS · line 192

Quoted 10 lines
xen/arch/arm/xen.lds.S +7 -0
+
+#if defined(CONFIG_HAS_PCI) && !defined(CONFIG_LATE_HWDOM)
+       . = ALIGN(POINTER_ALIGN);
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
    } :text
    __init_end_efi = .;
    . = ALIGN(STACK_SIZE);

Sadly you forgot to Cc the ARM maintainers. Stefano, Julien, the above would require your ack.

Jan

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw

Re: [PATCH v8 01/11] vpci: introduce basic handlers to trap accesses to the PCI config space

Quoted 4 lines · Julien Grall

Hi,

Quoted 1 line · Jan Beulich
Quoted 1 line
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:
Quoted 12 lines
Quoted 10 lines

xen/arch/arm/xen.lds.S SECTIONS · line 69

Quoted 10 lines
xen/arch/arm/xen.lds.S +2 -0
index c9b9546435..98b82680c6 100644
@@ -65,6 +65,13 @@ SECTIONS
         __param_start = .;
         *(.data.param)
         __param_end = .;
+
+#if defined(CONFIG_HAS_PCI) && defined(CONFIG_LATE_HWDOM)

Why this is conditional to CONFIG_LATE_HWDOM?

Currently PCI config space emulation is limited to Dom0 usage, hence the setup part of it can live in the init section if not using a late hardware domain.

Quoted 4 lines

Also, any reason to impose VPCI when CONFIG_HAS_PCI is set and not introduce another config? The new config would help to add support for PCI in steps in other architectures.

I could do that I guess, unless anyone objects I will add a CONFIG_VPCI option.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw

Re: [PATCH v8 01/11] vpci: introduce basic handlers to trap accesses to the PCI config space

Hi Roger,

Quoted 18 lines · Roger Pau Monné
Quoted 4 lines · Julien Grall

Hi,

Quoted 1 line · Jan Beulich
Quoted 1 line
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:
Quoted 12 lines
Quoted 10 lines

xen/arch/arm/xen.lds.S SECTIONS · line 69

Quoted 10 lines
xen/arch/arm/xen.lds.S +2 -0
index c9b9546435..98b82680c6 100644
@@ -65,6 +65,13 @@ SECTIONS
          __param_start = .;
          *(.data.param)
          __param_end = .;
+
+#if defined(CONFIG_HAS_PCI) && defined(CONFIG_LATE_HWDOM)

Why this is conditional to CONFIG_LATE_HWDOM?

Quoted 3 lines

Currently PCI config space emulation is limited to Dom0 usage, hence the setup part of it can live in the init section if not using a late hardware domain.

Oh, I missed that bits. Make sense then.

Cheers,

Quoted 10 lines
Quoted 4 lines

Also, any reason to impose VPCI when CONFIG_HAS_PCI is set and not introduce another config? The new config would help to add support for PCI in steps in other architectures.

I could do that I guess, unless anyone objects I will add a CONFIG_VPCI option.

Roger.

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#6 lore raw

PATCH v8 2/11 x86/mmcfg: add handlers for the PVH Dom0 MMCFG areas

In reply to Roger Pau Monne

Introduce a set of handlers for the accesses to the MMCFG areas. Those areas are setup based on the contents of the hardware MMCFG tables, and the list of handled MMCFG areas is stored inside of the hvm_domain struct.

The read/writes are forwarded to the generic vpci handlers once the address is decoded in order to obtain the device and register the guest is trying to access.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Paul Durrant <[email protected]>
---
Changes since v7:
- Add check for end_bus >= start_bus to register_vpci_mmcfg_handler.
- Protect destroy_vpci_mmcfg with the mmcfg_lock.

Changes since v6:
- Move allocation of mmcfg outside of the locked region.
- Do proper overlap checks when adding mmcfg regions.
- Return _RETRY if the mcfg region cannot be found in the read/write handlers. This means the mcfg area has been removed between the accept and the read/write calls.

Changes since v5:
- Switch to use pci_sbdf_t.
- Switch to the new per vpci locks.
- Move the mmcfg related external definitions to asm-x86/pci.h.

Changes since v4:
- Change the attribute of pvh_setup_mmcfg to __hwdom_init.
- Try to add as many MMCFG regions as possible, even if one fails to add.
- Change some fields of the hvm_mmcfg struct: turn size into a unsigned int, segment into uint16_t and bus into uint8_t.
- Convert some address parameters from unsigned long to paddr_t for consistency.
- Make vpci_mmcfg_decode_addr return the decoded register in the return of the function.
- Introduce a new macro to convert a MMCFG address into a BDF, and use it in vpci_mmcfg_decode_addr to clarify the logic.
- In vpci_mmcfg_{read/write} unify the logic for 8B accesses and smaller ones.
- Add the __hwdom_init attribute to register_vpci_mmcfg_handler.
- Test that reg + size doesn't cross a device boundary.

Changes since v3:
- Propagate changes from previous patches: drop xen_ prefix for vpci functions, pass slot and func instead of devfn and fix the error paths of the MMCFG handlers.
- s/ecam/mmcfg/.
- Move the destroy code to a separate function, so the hvm_mmcfg struct can be private to hvm/io.c.
- Constify the return of vpci_mmcfg_find.
- Use d instead of v->domain in vpci_mmcfg_accept.
- Allow 8byte accesses to the mmcfg.

Changes since v1:
- Added locking.

---
 xen/arch/x86/hvm/dom0_build.c    |  21 +++++
 xen/arch/x86/hvm/hvm.c           |   4 +
 xen/arch/x86/hvm/io.c            | 184 ++++++++++++++++++++++++++++++++++++++-
 xen/arch/x86/x86_64/mmconfig.h   |   4 -
 xen/include/asm-x86/hvm/domain.h |   4 +
 xen/include/asm-x86/hvm/io.h     |   7 ++
 xen/include/asm-x86/pci.h        |   6 ++
 7 files changed, 225 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/dom0_build.c +21 -0
index 7d177a1d7d..e511809766 100644
@@ -22,6 +22,7 @@
 #include <xen/init.h>
 #include <xen/libelf.h>
 #include <xen/multiboot.h>
+#include <xen/pci.h>
 #include <xen/softirq.h>
 
 #include <acpi/actables.h>
@@ -1056,6 +1057,24 @@ static int __init pvh_setup_acpi(struct domain *d, paddr_t start_info)
     return 0;
 }
 
+static void __hwdom_init pvh_setup_mmcfg(struct domain *d)
+{
+    unsigned int i;
+    int rc;
+
+    for ( i = 0; i < pci_mmcfg_config_num; i++ )
+    {
+        rc = register_vpci_mmcfg_handler(d, pci_mmcfg_config[i].address,
+                                         pci_mmcfg_config[i].start_bus_number,
+                                         pci_mmcfg_config[i].end_bus_number,
+                                         pci_mmcfg_config[i].pci_segment);
+        if ( rc )
+            printk("Unable to setup MMCFG handler at %#lx for segment %u\n",
+                   pci_mmcfg_config[i].address,
+                   pci_mmcfg_config[i].pci_segment);
+    }
+}
+
 int __init dom0_construct_pvh(struct domain *d, const module_t *image,
                               unsigned long image_headroom,
                               module_t *initrd,
@@ -1097,6 +1116,8 @@ int __init dom0_construct_pvh(struct domain *d, const module_t *image,
         return rc;
     }
 
+    pvh_setup_mmcfg(d);
+
     panic("Building a PVHv2 Dom0 is not yet supported.");
     return 0;
 }
xen/arch/x86/hvm/hvm.c +4 -0
index 41b49038ac..42369d34cb 100644
@@ -585,8 +585,10 @@ int hvm_domain_initialise(struct domain *d, unsigned long domcr_flags,
     spin_lock_init(&d->arch.hvm_domain.irq_lock);
     spin_lock_init(&d->arch.hvm_domain.uc_lock);
     spin_lock_init(&d->arch.hvm_domain.write_map.lock);
+    rwlock_init(&d->arch.hvm_domain.mmcfg_lock);
     INIT_LIST_HEAD(&d->arch.hvm_domain.write_map.list);
     INIT_LIST_HEAD(&d->arch.hvm_domain.g2m_ioport_list);
+    INIT_LIST_HEAD(&d->arch.hvm_domain.mmcfg_regions);
 
     rc = create_perdomain_mapping(d, PERDOMAIN_VIRT_START, 0, NULL, NULL);
     if ( rc )
@@ -732,6 +734,8 @@ void hvm_domain_destroy(struct domain *d)
         list_del(&ioport->list);
         xfree(ioport);
     }
+
+    destroy_vpci_mmcfg(d);
 }
 
 static int hvm_save_tsc_adjust(struct domain *d, hvm_domain_context_t *h)
xen/arch/x86/hvm/io.c +183 -1

196 lines not on this page · raw message

xen/arch/x86/x86_64/mmconfig.h +0 -4
index 7537519414..2e836848ad 100644
@@ -74,10 +74,6 @@ static inline void mmio_config_writel(void __iomem *pos, u32 val)
     asm volatile("movl %%eax,(%1)" :: "a" (val), "r" (pos) : "memory");
 }
 
-/* external variable defines */
-extern int pci_mmcfg_config_num;
-extern struct acpi_mcfg_allocation *pci_mmcfg_config;
-
 /* function prototypes */
 int acpi_parse_mcfg(struct acpi_table_header *header);
 int pci_mmcfg_reserved(uint64_t address, unsigned int segment,
xen/include/asm-x86/hvm/domain.h +4 -0
index 7f128c05ff..d1d933d791 100644
@@ -184,6 +184,10 @@ struct hvm_domain {
     /* List of guest to machine IO ports mapping. */
     struct list_head g2m_ioport_list;
 
+    /* List of MMCFG regions trapped by Xen. */
+    struct list_head mmcfg_regions;
+    rwlock_t mmcfg_lock;
+
     /* List of permanently write-mapped pages. */
     struct {
         spinlock_t lock;
xen/include/asm-x86/hvm/io.h +7 -0
index ff0bea5d53..16465ceb30 100644
@@ -163,6 +163,13 @@ void register_g2m_portio_handler(struct domain *d);
 /* HVM port IO handler for vPCI accesses. */
 void register_vpci_portio_handler(struct domain *d);
 
+/* HVM MMIO handler for PCI MMCFG accesses. */
+int register_vpci_mmcfg_handler(struct domain *d, paddr_t addr,
+                                unsigned int start_bus, unsigned int end_bus,
+                                unsigned int seg);
+/* Destroy tracked MMCFG areas. */
+void destroy_vpci_mmcfg(struct domain *d);
+
 #endif /* __ASM_X86_HVM_IO_H__ */
 
 
xen/include/asm-x86/pci.h +6 -0
index 36801d317b..cc05045e9c 100644
@@ -6,6 +6,8 @@
 #define CF8_ADDR_HI(cf8) (  ((cf8) & 0x0f000000) >> 16)
 #define CF8_ENABLED(cf8) (!!((cf8) & 0x80000000))
 
+#define MMCFG_BDF(addr)  ( ((addr) & 0x0ffff000) >> 12)
+
 #define IS_SNB_GFX(id) (id == 0x01068086 || id == 0x01168086 \
                         || id == 0x01268086 || id == 0x01028086 \
                         || id == 0x01128086 || id == 0x01228086 \
@@ -26,4 +28,8 @@ bool_t pci_mmcfg_decode(unsigned long mfn, unsigned int *seg,
 bool_t pci_ro_mmcfg_decode(unsigned long mfn, unsigned int *seg,
                            unsigned int *bdf);
 
+/* MMCFG external variable defines */
+extern int pci_mmcfg_config_num;
+extern struct acpi_mcfg_allocation *pci_mmcfg_config;
+
 #endif /* __X86_PCI_H__ */
Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#7 lore raw

PATCH v8 3/11 x86/physdev: enable PHYSDEVOP_pci_mmcfg_reserved for PVH Dom0

In reply to Roger Pau Monne

So that MMCFG regions not present in the MCFG ACPI table can be added at run time by the hardware domain.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
---
Changes since v7:
- Add newline in hvm_physdev_op for non-fallthrough case.

Changes since v6:
- Do not return EEXIST if the same exact region is already tracked by Xen.

Changes since v5:
- Check for has_vpci before calling register_vpci_mmcfg_handler instead of checking for is_hvm_domain.

Changes since v4:
- Change the hardware_domain check in hvm_physdev_op to a vpci check.
- Only register the MMCFG area, but don't scan it.

Changes since v3:
- New in this version.

---
 xen/arch/x86/hvm/hypercall.c |  5 +++++
 xen/arch/x86/hvm/io.c        | 16 +++++++++++-----
 xen/arch/x86/physdev.c       | 11 +++++++++++
 3 files changed, 27 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/hypercall.c +5 -0
index 5742dd1797..85eacd7d33 100644
@@ -89,6 +89,11 @@ static long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( !has_pirq(curr->domain) )
             return -ENOSYS;
         break;
+
+    case PHYSDEVOP_pci_mmcfg_reserved:
+        if ( !has_vpci(curr->domain) )
+            return -ENOSYS;
+        break;
     }
 
     if ( !curr->hcall_compat )
xen/arch/x86/hvm/io.c +11 -5
index 04425c064b..556810c126 100644
@@ -507,10 +507,9 @@ static const struct hvm_mmio_ops vpci_mmcfg_ops = {
     .write = vpci_mmcfg_write,
 };
 
-int __hwdom_init register_vpci_mmcfg_handler(struct domain *d, paddr_t addr,
-                                             unsigned int start_bus,
-                                             unsigned int end_bus,
-                                             unsigned int seg)
+int register_vpci_mmcfg_handler(struct domain *d, paddr_t addr,
+                                unsigned int start_bus, unsigned int end_bus,
+                                unsigned int seg)
 {
     struct hvm_mmcfg *mmcfg, *new = xmalloc(struct hvm_mmcfg);
 
@@ -535,9 +534,16 @@ int __hwdom_init register_vpci_mmcfg_handler(struct domain *d, paddr_t addr,
         if ( new->addr < mmcfg->addr + mmcfg->size &&
              mmcfg->addr < new->addr + new->size )
         {
+            int ret = -EEXIST;
+
+            if ( new->addr == mmcfg->addr &&
+                 new->start_bus == mmcfg->start_bus &&
+                 new->segment == mmcfg->segment &&
+                 new->size == mmcfg->size )
+                ret = 0;
             write_unlock(&d->arch.hvm_domain.mmcfg_lock);
             xfree(new);
-            return -EEXIST;
+            return ret;
         }
 
     if ( list_empty(&d->arch.hvm_domain.mmcfg_regions) )
xen/arch/x86/physdev.c +11 -0
index 380d36f6b9..984491c3dc 100644
@@ -557,6 +557,17 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         ret = pci_mmcfg_reserved(info.address, info.segment,
                                  info.start_bus, info.end_bus, info.flags);
+        if ( !ret && has_vpci(currd) )
+        {
+            /*
+             * For HVM (PVH) domains try to add the newly found MMCFG to the
+             * domain.
+             */
+            ret = register_vpci_mmcfg_handler(currd, info.address,
+                                              info.start_bus, info.end_bus,
+                                              info.segment);
+        }
+
         break;
     }
 
Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#8 lore raw

Re: [PATCH v8 03/11] x86/physdev: enable PHYSDEVOP_pci_mmcfg_reserved for PVH Dom0

Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:

So that MMCFG regions not present in the MCFG ACPI table can be added at run time by the hardware domain.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
---
Changes since v7:
- Add newline in hvm_physdev_op for non-fallthrough case.

Changes since v6:
- Do not return EEXIST if the same exact region is already tracked by Xen.

Changes since v5:
- Check for has_vpci before calling register_vpci_mmcfg_handler instead of checking for is_hvm_domain.

Changes since v4:
- Change the hardware_domain check in hvm_physdev_op to a vpci check.
- Only register the MMCFG area, but don't scan it.

Quoted 5 lines

Changes since v3:
- New in this version.

---
 xen/arch/x86/hvm/hypercall.c |  5 +++++
 xen/arch/x86/hvm/io.c        | 16 +++++++++++-----

Sadly you forgot to Cc Paul for this one. Paul - any chance you could take a look?

Jan

Quoted 85 lines
 xen/arch/x86/physdev.c       | 11 +++++++++++
 3 files changed, 27 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/hypercall.c +5 -0
index 5742dd1797..85eacd7d33 100644
@@ -89,6 +89,11 @@ static long hvm_physdev_op(int cmd, 
 XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( !has_pirq(curr->domain) )
             return -ENOSYS;
         break;
+
+    case PHYSDEVOP_pci_mmcfg_reserved:
+        if ( !has_vpci(curr->domain) )
+            return -ENOSYS;
+        break;
     }
 
     if ( !curr->hcall_compat )
xen/arch/x86/hvm/io.c +3 -4
index 04425c064b..556810c126 100644
@@ -507,10 +507,9 @@ static const struct hvm_mmio_ops vpci_mmcfg_ops = {
     .write = vpci_mmcfg_write,
 };
 
-int __hwdom_init register_vpci_mmcfg_handler(struct domain *d, paddr_t 
 addr,
-                                             unsigned int start_bus,
-                                             unsigned int end_bus,
-                                             unsigned int seg)
+int register_vpci_mmcfg_handler(struct domain *d, paddr_t addr,
+                                unsigned int start_bus, unsigned int 
 end_bus,
+                                unsigned int seg)
 {
     struct hvm_mmcfg *mmcfg, *new = xmalloc(struct hvm_mmcfg);
xen/arch/x86/hvm/io.c +8 -1
@@ -535,9 +534,16 @@ int __hwdom_init register_vpci_mmcfg_handler(struct 
 domain *d, paddr_t addr,
         if ( new->addr < mmcfg->addr + mmcfg->size &&
              mmcfg->addr < new->addr + new->size )
         {
+            int ret = -EEXIST;
+
+            if ( new->addr == mmcfg->addr &&
+                 new->start_bus == mmcfg->start_bus &&
+                 new->segment == mmcfg->segment &&
+                 new->size == mmcfg->size )
+                ret = 0;
             write_unlock(&d->arch.hvm_domain.mmcfg_lock);
             xfree(new);
-            return -EEXIST;
+            return ret;
         }
 
     if ( list_empty(&d->arch.hvm_domain.mmcfg_regions) )
xen/arch/x86/physdev.c +4 -0
index 380d36f6b9..984491c3dc 100644
@@ -557,6 +557,17 @@ ret_t do_physdev_op(int cmd, 
 XEN_GUEST_HANDLE_PARAM(void) arg)
 
         ret = pci_mmcfg_reserved(info.address, info.segment,
                                  info.start_bus, info.end_bus, info.flags);
+        if ( !ret && has_vpci(currd) )
+        {
+            /*
+             * For HVM (PVH) domains try to add the newly found MMCFG to 

the

xen/arch/x86/physdev.c +7 -0
+             * domain.
+             */
+            ret = register_vpci_mmcfg_handler(currd, info.address,
+                                              info.start_bus, info.end_bus,
+                                              info.segment);
+        }
+
         break;
     }
Signature · 2 lines
-- 
2.15.1

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by
#9 lore raw

Re: [PATCH v8 03/11] x86/physdev: enable PHYSDEVOP_pci_mmcfg_reserved for PVH Dom0

Quoted 36 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 23 February 2018 15:57
To: Paul Durrant <[email protected]>; Roger Pau Monne <[email protected]>
Cc: Andrew Cooper <[email protected]>; xen- [email protected]; Boris Ostrovsky <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>
Subject: Re: [PATCH v8 03/11] x86/physdev: enable PHYSDEVOP_pci_mmcfg_reserved for PVH Dom0

Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:

So that MMCFG regions not present in the MCFG ACPI table can be added at run time by the hardware domain.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
---
Changes since v7:
- Add newline in hvm_physdev_op for non-fallthrough case.

Changes since v6:
- Do not return EEXIST if the same exact region is already tracked by Xen.

Changes since v5:
- Check for has_vpci before calling register_vpci_mmcfg_handler instead of checking for is_hvm_domain.

Changes since v4:
- Change the hardware_domain check in hvm_physdev_op to a vpci check.
- Only register the MMCFG area, but don't scan it.

Quoted 9 lines
Quoted 5 lines

Changes since v3:
- New in this version.

---
 xen/arch/x86/hvm/hypercall.c |  5 +++++
 xen/arch/x86/hvm/io.c        | 16 +++++++++++-----

Sadly you forgot to Cc Paul for this one. Paul - any chance you could take a look?

Sure. LGTM.

Quoted 88 lines

Jan

xen/arch/x86/hvm/io.c static const struct hvm_mmio_ops

Quoted 25 lines
 xen/arch/x86/physdev.c       | 11 +++++++++++
 3 files changed, 27 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/hypercall.c +5 -0
index 5742dd1797..85eacd7d33 100644
@@ -89,6 +89,11 @@ static long hvm_physdev_op(int cmd,
 XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( !has_pirq(curr->domain) )
             return -ENOSYS;
         break;
+
+    case PHYSDEVOP_pci_mmcfg_reserved:
+        if ( !has_vpci(curr->domain) )
+            return -ENOSYS;
+        break;
     }
 
     if ( !curr->hcall_compat )
xen/arch/x86/hvm/io.c +0 -0
index 04425c064b..556810c126 100644
@@ -507,10 +507,9 @@ static const struct hvm_mmio_ops

vpci_mmcfg_ops = {

xen/arch/x86/hvm/io.c static const struct hvm_mmio_ops vpci_mmcfg_ops = {

Quoted 4 lines
xen/arch/x86/hvm/io.c +0 -1
     .write = vpci_mmcfg_write,
 };
 
-int __hwdom_init register_vpci_mmcfg_handler(struct domain *d,

paddr_t

xen/arch/x86/hvm/io.c int __hwdom_init

Quoted 12 lines
xen/arch/x86/hvm/io.c +3 -3
 addr,
-                                             unsigned int start_bus,
-                                             unsigned int end_bus,
-                                             unsigned int seg)
+int register_vpci_mmcfg_handler(struct domain *d, paddr_t addr,
+                                unsigned int start_bus, unsigned int
 end_bus,
+                                unsigned int seg)
 {
     struct hvm_mmcfg *mmcfg, *new = xmalloc(struct hvm_mmcfg);
xen/arch/x86/hvm/io.c +0 -0
@@ -535,9 +534,16 @@ int __hwdom_init

register_vpci_mmcfg_handler(struct

Quoted 42 lines
xen/arch/x86/hvm/io.c +8 -1
 domain *d, paddr_t addr,
         if ( new->addr < mmcfg->addr + mmcfg->size &&
              mmcfg->addr < new->addr + new->size )
         {
+            int ret = -EEXIST;
+
+            if ( new->addr == mmcfg->addr &&
+                 new->start_bus == mmcfg->start_bus &&
+                 new->segment == mmcfg->segment &&
+                 new->size == mmcfg->size )
+                ret = 0;
             write_unlock(&d->arch.hvm_domain.mmcfg_lock);
             xfree(new);
-            return -EEXIST;
+            return ret;
         }
 
     if ( list_empty(&d->arch.hvm_domain.mmcfg_regions) )
xen/arch/x86/physdev.c +4 -0
index 380d36f6b9..984491c3dc 100644
@@ -557,6 +557,17 @@ ret_t do_physdev_op(int cmd,
 XEN_GUEST_HANDLE_PARAM(void) arg)
 
         ret = pci_mmcfg_reserved(info.address, info.segment,
                                  info.start_bus, info.end_bus, info.flags);
+        if ( !ret && has_vpci(currd) )
+        {
+            /*
+             * For HVM (PVH) domains try to add the newly found MMCFG to

the

xen/arch/x86/physdev.c +7 -0
+             * domain.
+             */
+            ret = register_vpci_mmcfg_handler(currd, info.address,
+                                              info.start_bus, info.end_bus,
+                                              info.segment);
+        }
+
         break;
     }
Quoted 3 lines
Quoted 2 lines

--
2.15.1

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw

PATCH v8 4/11 pci: split code to size BARs from pci_add_device

In reply to Roger Pau Monne

So that it can be called from outside in order to get the size of regular PCI BARs. This will be required in order to map the BARs from PCI devices into PVH Dom0 p2m.

---
Cc: Jan Beulich <[email protected]>
---
Changes since v7:
- Do not return error from pci_size_mem_bar in order to keep previous behavior.

Changes since v6:
- Remove the vf and addr local variables.
- Change the way flags are declared.
- Move the last bool parameter to the flags field.

Changes since v5:
- Introduce a flags field for pci_size_mem_bar.
- Use pci_sbdf_t.

Changes since v4:
- Restore printing whether the BAR is from a vf.
- Make the psize pointer parameter not optional.
- s/u64/uint64_t.
- Remove some unneeded parentheses.
- Assert the return value is never 0.
- Use the newly introduced pci_sbdf_t type.

Changes since v3:
- Rename function to size BARs to pci_size_mem_bar.
- Change the parameters passed to the function. Pass the position and whether the BAR is the last one, instead of the (base, max_bars, *index) tuple.
- Make the function return the number of BARs consumed (1 for 32b, 2 for 64b BARs).
- Change the dprintk back to printk.
- Do not log another error message in pci_add_device in case pci_size_mem_bar fails.

---
 xen/drivers/passthrough/pci.c | 97 ++++++++++++++++++++++++++++---------------
 xen/include/xen/pci.h         |  5 +++
 2 files changed, 68 insertions(+), 34 deletions(-)
xen/drivers/passthrough/pci.c +63 -34
index 519993d536..fb36b20834 100644
@@ -603,6 +603,56 @@ static int iommu_add_device(struct pci_dev *pdev);
 static int iommu_enable_device(struct pci_dev *pdev);
 static int iommu_remove_device(struct pci_dev *pdev);
 
+unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos,
+                              uint64_t *paddr, uint64_t *psize,
+                              unsigned int flags)
+{
+    uint32_t hi = 0, bar = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev,
+                                           sbdf.func, pos);
+    uint64_t size;
+
+    ASSERT((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY);
+    pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos, ~0);
+    if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
+         PCI_BASE_ADDRESS_MEM_TYPE_64 )
+    {
+        if ( flags & PCI_BAR_LAST )
+        {
+            printk(XENLOG_WARNING
+                   "%sdevice %04x:%02x:%02x.%u with 64-bit %sBAR in last slot\n",
+                   (flags & PCI_BAR_VF) ? "SR-IOV " : "", sbdf.seg, sbdf.bus,
+                   sbdf.dev, sbdf.func, (flags & PCI_BAR_VF) ? "vf " : "");
+            *psize = 0;
+            return 1;
+        }
+        hi = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos + 4);
+        pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos + 4, ~0);
+    }
+    size = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos) &
+           PCI_BASE_ADDRESS_MEM_MASK;
+    if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
+         PCI_BASE_ADDRESS_MEM_TYPE_64 )
+    {
+        size |= (uint64_t)pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev,
+                                          sbdf.func, pos + 4) << 32;
+        pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos + 4, hi);
+    }
+    else if ( size )
+        size |= (uint64_t)~0 << 32;
+    pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos, bar);
+    size = -size;
+
+    if ( paddr )
+        *paddr = (bar & PCI_BASE_ADDRESS_MEM_MASK) | ((uint64_t)hi << 32);
+    *psize = size;
+
+    if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
+         PCI_BASE_ADDRESS_MEM_TYPE_64 )
+        return 2;
+
+    return 1;
+}
+
 int pci_add_device(u16 seg, u8 bus, u8 devfn,
                    const struct pci_dev_info *info, nodeid_t node)
 {
@@ -674,11 +724,16 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
             unsigned int i;
 
             BUILD_BUG_ON(ARRAY_SIZE(pdev->vf_rlen) != PCI_SRIOV_NUM_BARS);
-            for ( i = 0; i < PCI_SRIOV_NUM_BARS; ++i )
+            for ( i = 0; i < PCI_SRIOV_NUM_BARS; )
             {
                 unsigned int idx = pos + PCI_SRIOV_BAR + i * 4;
                 u32 bar = pci_conf_read32(seg, bus, slot, func, idx);
-                u32 hi = 0;
+                pci_sbdf_t sbdf = {
+                    .seg = seg,
+                    .bus = bus,
+                    .dev = slot,
+                    .func = func,
+                };
 
                 if ( (bar & PCI_BASE_ADDRESS_SPACE) ==
                      PCI_BASE_ADDRESS_SPACE_IO )
@@ -689,38 +744,12 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
                            seg, bus, slot, func, i);
                     continue;
                 }
-                pci_conf_write32(seg, bus, slot, func, idx, ~0);
-                if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
-                     PCI_BASE_ADDRESS_MEM_TYPE_64 )
-                {
-                    if ( i >= PCI_SRIOV_NUM_BARS )
-                    {
-                        printk(XENLOG_WARNING
-                               "SR-IOV device %04x:%02x:%02x.%u with 64-bit"
-                               " vf BAR in last slot\n",
-                               seg, bus, slot, func);
-                        break;
-                    }
-                    hi = pci_conf_read32(seg, bus, slot, func, idx + 4);
-                    pci_conf_write32(seg, bus, slot, func, idx + 4, ~0);
-                }
-                pdev->vf_rlen[i] = pci_conf_read32(seg, bus, slot, func, idx) &
-                                   PCI_BASE_ADDRESS_MEM_MASK;
-                if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
-                     PCI_BASE_ADDRESS_MEM_TYPE_64 )
-                {
-                    pdev->vf_rlen[i] |= (u64)pci_conf_read32(seg, bus,
-                                                             slot, func,
-                                                             idx + 4) << 32;
-                    pci_conf_write32(seg, bus, slot, func, idx + 4, hi);
-                }
-                else if ( pdev->vf_rlen[i] )
-                    pdev->vf_rlen[i] |= (u64)~0 << 32;
-                pci_conf_write32(seg, bus, slot, func, idx, bar);
-                pdev->vf_rlen[i] = -pdev->vf_rlen[i];
-                if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
-                     PCI_BASE_ADDRESS_MEM_TYPE_64 )
-                    ++i;
+                ret = pci_size_mem_bar(sbdf, idx, NULL, &pdev->vf_rlen[i],
+                                       PCI_BAR_VF |
+                                       ((i == PCI_SRIOV_NUM_BARS - 1) ?
+                                        PCI_BAR_LAST : 0));
+                ASSERT(ret);
+                i += ret;
             }
         }
         else
xen/include/xen/pci.h +5 -0
index b7a6abfc53..2f171a8dcc 100644
@@ -189,6 +189,11 @@ const char *parse_pci(const char *, unsigned int *seg, unsigned int *bus,
 const char *parse_pci_seg(const char *, unsigned int *seg, unsigned int *bus,
                           unsigned int *dev, unsigned int *func, bool *def_seg);
 
+#define PCI_BAR_VF      (1u << 0)
+#define PCI_BAR_LAST    (1u << 1)
+unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos,
+                              uint64_t *paddr, uint64_t *psize,
+                              unsigned int flags);
 
 bool_t pcie_aer_get_firmware_first(const struct pci_dev *);
 
Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v8 4/11
#11 lore raw

Re: [PATCH v8 04/11] pci: split code to size BARs from pci_add_device

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:

So that it can be called from outside in order to get the size of regular PCI BARs. This will be required in order to map the BARs from PCI devices into PVH Dom0 p2m.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#12 lore raw

PATCH v8 5/11 pci: add support to size ROM BARs to pci_size_mem_bar

In reply to Roger Pau Monne

---
Cc: Jan Beulich <[email protected]>
---
Changes since v6:
- Remove the rom local variable.

Changes since v5:
- Use the flags field.
- Introduce a mask local variable.
- Simplify return.

Changes since v4:
- New in this version.

---
 xen/drivers/passthrough/pci.c | 28 ++++++++++++++--------------
 xen/include/xen/pci.h         |  1 +
 2 files changed, 15 insertions(+), 14 deletions(-)
xen/drivers/passthrough/pci.c +14 -14
index fb36b20834..bf26027f4c 100644
@@ -610,11 +610,16 @@ unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos,
     uint32_t hi = 0, bar = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev,
                                            sbdf.func, pos);
     uint64_t size;
-
-    ASSERT((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY);
+    bool is64bits = !(flags & PCI_BAR_ROM) &&
+        (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64;
+    uint32_t mask = (flags & PCI_BAR_ROM) ? (uint32_t)PCI_ROM_ADDRESS_MASK
+                                          : (uint32_t)PCI_BASE_ADDRESS_MEM_MASK;
+
+    ASSERT(!((flags & PCI_BAR_VF) && (flags & PCI_BAR_ROM)));
+    ASSERT((flags & PCI_BAR_ROM) ||
+           (bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY);
     pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos, ~0);
-    if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
-         PCI_BASE_ADDRESS_MEM_TYPE_64 )
+    if ( is64bits )
     {
         if ( flags & PCI_BAR_LAST )
         {
@@ -628,10 +633,9 @@ unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos,
         hi = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos + 4);
         pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos + 4, ~0);
     }
-    size = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos) &
-           PCI_BASE_ADDRESS_MEM_MASK;
-    if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
-         PCI_BASE_ADDRESS_MEM_TYPE_64 )
+    size = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func,
+                           pos) & mask;
+    if ( is64bits )
     {
         size |= (uint64_t)pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev,
                                           sbdf.func, pos + 4) << 32;
@@ -643,14 +647,10 @@ unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos,
     size = -size;
 
     if ( paddr )
-        *paddr = (bar & PCI_BASE_ADDRESS_MEM_MASK) | ((uint64_t)hi << 32);
+        *paddr = (bar & mask) | ((uint64_t)hi << 32);
     *psize = size;
 
-    if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
-         PCI_BASE_ADDRESS_MEM_TYPE_64 )
-        return 2;
-
-    return 1;
+    return is64bits ? 2 : 1;
 }
 
 int pci_add_device(u16 seg, u8 bus, u8 devfn,
xen/include/xen/pci.h +1 -0
index 2f171a8dcc..4cfa774615 100644
@@ -191,6 +191,7 @@ const char *parse_pci_seg(const char *, unsigned int *seg, unsigned int *bus,
 
 #define PCI_BAR_VF      (1u << 0)
 #define PCI_BAR_LAST    (1u << 1)
+#define PCI_BAR_ROM     (1u << 2)
 unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos,
                               uint64_t *paddr, uint64_t *psize,
                               unsigned int flags);
Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#13 lore raw

PATCH v8 6/11 xen: introduce rangeset_consume_ranges

In reply to Roger Pau Monne

This function allows to iterate over a rangeset while removing the processed regions.

This will be used in order to split processing of large memory areas when mapping them into the guest p2m.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v6:
- Expand commit message.
- Add a comment to describe the expected function behavior.
- Fix indentation.

Changes since v5:
- New in this version.

---
 xen/common/rangeset.c      | 28 ++++++++++++++++++++++++++++
 xen/include/xen/rangeset.h | 10 ++++++++++
 2 files changed, 38 insertions(+)
xen/common/rangeset.c +28 -0
index ade34f6a50..bb68ce62e4 100644
@@ -350,6 +350,34 @@ int rangeset_claim_range(struct rangeset *r, unsigned long size,
     return 0;
 }
 
+int rangeset_consume_ranges(struct rangeset *r,
+                            int (*cb)(unsigned long s, unsigned long e, void *,
+                                      unsigned long *c),
+                            void *ctxt)
+{
+    int rc = 0;
+
+    write_lock(&r->lock);
+    while ( !rangeset_is_empty(r) )
+    {
+        unsigned long consumed = 0;
+        struct range *x = first_range(r);
+
+        rc = cb(x->s, x->e, ctxt, &consumed);
+
+        ASSERT(consumed <= x->e - x->s + 1);
+        x->s += consumed;
+        if ( x->s > x->e )
+            destroy_range(r, x);
+
+        if ( rc )
+            break;
+    }
+    write_unlock(&r->lock);
+
+    return rc;
+}
+
 int rangeset_add_singleton(
     struct rangeset *r, unsigned long s)
 {
xen/include/xen/rangeset.h +10 -0
index 1f83b1f44b..583b72bb0c 100644
@@ -70,6 +70,16 @@ int rangeset_report_ranges(
     struct rangeset *r, unsigned long s, unsigned long e,
     int (*cb)(unsigned long s, unsigned long e, void *), void *ctxt);
 
+/*
+ * Note that the consume function can return an error value apart from
+ * -ERESTART, and that no cleanup is performed (ie: the user should call
+ * rangeset_destroy if needed).
+ */
+int rangeset_consume_ranges(struct rangeset *r,
+                            int (*cb)(unsigned long s, unsigned long e,
+                                      void *, unsigned long *c),
+                            void *ctxt);
+
 /* Add/remove/query a single number. */
 int __must_check rangeset_add_singleton(
     struct rangeset *r, unsigned long s);
Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#14 lore raw

PATCH v8 7/11 vpci/bars: add handlers to map the BARs

In reply to Roger Pau Monne

Introduce a set of handlers that trap accesses to the PCI BARs and the command register, in order to snoop BAR sizing and BAR relocation.

The command handler is used to detect changes to bit 2 (response to memory space accesses), and maps/unmaps the BARs of the device into the guest p2m. A rangeset is used in order to figure out which memory to map/unmap. This makes it easier to keep track of the possible overlaps with other BARs, and will also simplify MSI-X support, where certain regions of a BAR might be used for the MSI-X table or PBA.

The BAR register handlers are used to detect attempts by the guest to size or relocate the BARs.

Note that the long running BAR mapping and unmapping operations are deferred to be performed by hvm_io_pending, so that they can be safely preempted.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v7:
- Order includes.
- Add newline between switch cases.
- Fix typo in comment (hopping).
- Wrap ternary conditional in parentheses.
- Remove CONFIG_HAS_PCI gueard from sched.h vpci_vcpu usage.
- Add comment regarding vpci_vcpu usage.
- Move rom_enabled from BAR struct to header.
- Do not protect vpci_vcpu with __XEN__ guards.

Changes since v6:
- s/vpci_check_pending/vpci_process_pending/.
- Improve error handling in vpci_process_pending.
- Add a comment that explains how vpci_check_bar_overlap works.
- Add error messages to vpci_modify_bars and vpci_modify_rom.
- Introduce vpci_hw_read16/32, in order to passthrough reads to the underlying hw.
- Print BAR number on error in vpci_bar_write.
- Place the CONFIG_HAS_PCI guards inside the vpci.h header and provide an empty vpci_vcpu structure for the !CONFIG_HAS_PCI case.
- Define CONFIG_HAS_PCI in the test harness emul.h header before including vpci.h
- Add ARM TODOs and an ARM-specific bodge to vpci_map_range due to the lack of preemption in {un}map_mmio_regions.
- Make vpci_maybe_defer_map void.
- Set rom_enabled in vpci_init_bars.
- Defer enabling/disabling the memory decoding (or the ROM enable bit) until the memory has been mapped/unmapped.
- Remove vpci_ prefix from static functions.
- Use the same code in order to map the general BARs and the ROM BARs.
- Remove the seg/bus local variables and use pdev->{seg,bus} instead.
- Convert the bools in the BAR related structs into bool bitfields.
- Add the must_check attribute to vpci_process_pending.
- Open code check_bar_overlap inside modify_bars, which was it's only user.

Changes since v5:
- Switch to the new handler type.
- Use pci_sbdf_t to size the BARs.
- Use a single return for vpci_modify_bar.
- Do not return an error code from vpci_modify_bars, just log the failure.
- Remove the 'sizing' parameter. Instead just let the guest write directly to the BAR, and read the value back. This simplifies the BAR register handlers, specially the read one.
- Ignore ROM BAR writes with memory decoding enabled and ROM enabled.
- Do not propagate failures to setup the ROM BAR in vpci_init_bars.
- Add preemption support to the BAR mapping/unmapping operations.

Changes since v4:
- Expand commit message to mention the reason behind the usage of rangesets.
- Fix comment related to the inclusiveness of rangesets.
- Fix off-by-one error in the calculation of the end of memory regions.
- Store the state of the BAR (mapped/unmapped) in the vpci_bar enabled field, previously was only used by ROMs.
- Fix double negation of return code.
- Modify vpci_cmd_write so it has a single call to pci_conf_write16.
- Print a warning when trying to write to the BAR with memory decoding enabled (and ignore the write).
- Remove header_type local variable, it's used only once.
- Move the read of the command register.
- Restore previous command register value in the exit paths.
- Only set address to INVALID_PADDR if the initial BAR value matches

    ~0 & PCI_BASE_ADDRESS_MEM_MASK.

- Don't disable the enabled bit in the expansion ROM register, memory decoding is already disabled and takes precedence.
- Don't use INVALID_PADDR, just set the initial BAR address to the value found in the hardware.
- Introduce rom_enabled to store the status of the PCI_ROM_ADDRESS_ENABLE bit.
- Reorder fields of the structure to prevent holes.

Changes since v3:
- Propagate previous changes: drop xen_ prefix and use u8/u16/u32 instead of the previous half_word/word/double_word.
- Constify some of the paramerters.
- s/VPCI_BAR_MEM/VPCI_BAR_MEM32/.
- Simplify the number of fields stored for each BAR, a single address field is stored and contains the address of the BAR both on Xen and in the guest.
- Allow the guest to move the BARs around in the physical memory map.
- Add support for expansion ROM BARs.
- Do not cache the value of the command register.
- Remove a label used in vpci_cmd_write.
- Fix the calculation of the sizing mask in vpci_bar_write.
- Check the memory decode bit in order to decide if a BAR is positioned or not.
- Disable memory decoding before sizing the BARs in Xen.
- When mapping/unmapping BARs check if there's overlap between BARs, in order to avoid unmapping memory required by another BAR.
- Introduce a macro to check whether a BAR is mappable or not.
- Add a comment regarding the lack of support for SR-IOV.
- Remove the usage of the GENMASK macro.

Changes since v2:
- Detect unset BARs and allow the hardware domain to position them.

---
 tools/tests/vpci/emul.h   |   1 +
 xen/arch/x86/hvm/ioreq.c  |   4 +
 xen/drivers/vpci/Makefile |   2 +-
 xen/drivers/vpci/header.c | 521 ++++++++++++++++++++++++++++++++++++++++++++++
 xen/drivers/vpci/vpci.c   |  14 ++
 xen/include/xen/sched.h   |   4 +
 xen/include/xen/vpci.h    |  55 +++++
 7 files changed, 600 insertions(+), 1 deletion(-)
 create mode 100644 xen/drivers/vpci/header.c
tools/tests/vpci/emul.h +1 -0
index fd0317995a..f6f9358b74 100644
@@ -80,6 +80,7 @@ typedef union {
     };
 } pci_sbdf_t;
 
+#define CONFIG_HAS_PCI
 #include "vpci.h"
 
 #define __hwdom_init
xen/arch/x86/hvm/ioreq.c +4 -0
index 5aeaaaccd9..03995cdaef 100644
@@ -26,6 +26,7 @@
 #include <xen/domain.h>
 #include <xen/event.h>
 #include <xen/paging.h>
+#include <xen/vpci.h>
 
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/ioreq.h>
@@ -48,6 +49,9 @@ bool hvm_io_pending(struct vcpu *v)
     struct domain *d = v->domain;
     struct hvm_ioreq_server *s;
 
+    if ( has_vpci(d) && vpci_process_pending(v) )
+        return true;
+
     list_for_each_entry ( s,
                           &d->arch.hvm_domain.ioreq_server.list,
                           list_entry )
xen/drivers/vpci/Makefile +1 -1
index 840a906470..241467212f 100644
@@ -1 +1 @@
-obj-y += vpci.o
+obj-y += vpci.o header.o
xen/drivers/vpci/header.c new file +521 -0

521 lines not on this page · raw message

xen/drivers/vpci/vpci.c +14 -0
index 4740d02edf..3da1822901 100644
@@ -102,6 +102,20 @@ static void vpci_ignored_write(const struct pci_dev *pdev, unsigned int reg,
 {
 }
 
+uint32_t vpci_hw_read16(const struct pci_dev *pdev, unsigned int reg,
+                        void *data)
+{
+    return pci_conf_read16(pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
+                           PCI_FUNC(pdev->devfn), reg);
+}
+
+uint32_t vpci_hw_read32(const struct pci_dev *pdev, unsigned int reg,
+                        void *data)
+{
+    return pci_conf_read32(pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
+                           PCI_FUNC(pdev->devfn), reg);
+}
+
 int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler,
                       vpci_write_t *write_handler, unsigned int offset,
                       unsigned int size, void *data)
xen/include/xen/sched.h +4 -0
index 2541ecb04f..b6509e8532 100644
@@ -20,6 +20,7 @@
 #include <xen/smp.h>
 #include <xen/perfc.h>
 #include <asm/atomic.h>
+#include <xen/vpci.h>
 #include <xen/wait.h>
 #include <public/xen.h>
 #include <public/domctl.h>
@@ -264,6 +265,9 @@ struct vcpu
 
     struct evtchn_fifo_vcpu *evtchn_fifo;
 
+    /* vPCI per-vCPU area, used to store data for long running operations. */
+    struct vpci_vcpu vpci;
+
     struct arch_vcpu arch;
 };
 
xen/include/xen/vpci.h +55 -0

73 lines not on this page · raw message

Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#15 lore raw

Re: [PATCH v8 07/11] vpci/bars: add handlers to map the BARs

Quoted 4 lines · Roger Pau Monne
xen/drivers/vpci/header.c new file +4 -0
+static void bar_write(const struct pci_dev *pdev, unsigned int reg,
+                      uint32_t val, void *data)
+{
+    struct vpci_bar *bar = data;

xen/drivers/vpci/header.c line 316

Quoted 10 lines
xen/drivers/vpci/header.c new file +10 -0
+    uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn);
+    bool hi = false;
+
+    if ( pci_conf_read16(pdev->seg, pdev->bus, slot, func, PCI_COMMAND) &
+         PCI_COMMAND_MEMORY )
+    {
+        gprintk(XENLOG_WARNING,
+                "%04x:%02x:%02x.%u: ignored BAR %lu write with memory decoding enabled\n",
+                pdev->seg, pdev->bus, slot, func,
+                (pdev->vpci->header.bars - bar) / 4);

This should be bar - pdev->vpci->header.bars. Will fix it in next version.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#16 lore raw

PATCH v8 8/11 x86/pt: mask MSI vectors on unbind

In reply to Roger Pau Monne

When a MSI device with per-vector masking capabilities is detected or added to Xen all the vectors are masked when initializing it. This implies that the first time the interrupt is bound to a domain it's masked.

This however only applies to the first time the interrupt is bound because neither the unbind nor the pirq unmap will mask the vector again. In order to fix this re-mask the interrupt when unbinding it from a guest. This makes sure that pairs of bind/unbind will always get the same masking state.

Note that no issues have been reported regarding this behavior because QEMU always uses the newly introduced XEN_PT_GFLAGSSHIFT_UNMASKED when binding interrupts, so it's always unmasked.

---
Cc: Jan Beulich <[email protected]>
---
Changes since v7:
- New in this version.

---
 xen/drivers/passthrough/io.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
xen/drivers/passthrough/io.c +15 -0
index 8f16e6c0a5..bab3aa349a 100644
@@ -645,7 +645,22 @@ int pt_irq_destroy_bind(
         }
         break;
     case PT_IRQ_TYPE_MSI:
+    {
+        unsigned long flags;
+        struct irq_desc *desc = domain_spin_lock_irq_desc(d, machine_gsi,
+                                                          &flags);
+
+        if ( !desc )
+            return -EINVAL;
+        /*
+         * Leave the MSI masked, so that the state when calling
+         * pt_irq_create_bind is consistent across bind/unbinds.
+         */
+        guest_mask_msi_irq(desc, true);
+        spin_unlock_irqrestore(&desc->lock, flags);
         break;
+    }
+
     default:
         return -EOPNOTSUPP;
     }
Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v8 8/11
#17 lore raw

Re: [PATCH v8 08/11] x86/pt: mask MSI vectors on unbind

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:

When a MSI device with per-vector masking capabilities is detected or added to Xen all the vectors are masked when initializing it. This implies that the first time the interrupt is bound to a domain it's masked.

This however only applies to the first time the interrupt is bound because neither the unbind nor the pirq unmap will mask the vector again. In order to fix this re-mask the interrupt when unbinding it from a guest. This makes sure that pairs of bind/unbind will always get the same masking state.

Quoted 5 lines

Note that no issues have been reported regarding this behavior because QEMU always uses the newly introduced XEN_PT_GFLAGSSHIFT_UNMASKED when binding interrupts, so it's always unmasked.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#18 lore raw

PATCH v8 9/11 vpci/msi: add MSI handlers

In reply to Roger Pau Monne

Add handlers for the MSI control, address, data and mask fields in order to detect accesses to them and setup the interrupts as requested by the guest.

Note that the pending register is not trapped, and the guest can freely read/write to it.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Paul Durrant <[email protected]>
---
Changes since v7:
- Don't store pci segment/bus on local variables.
- Add an error label to init_msi.
- Don't trap accesses to the PBA.
- Fix msi_pending_bits_reg macro so it matches coding style.
- Move the position of vectors in the vpci_msi struct.
- Add a comment to clarify the expected state of vectors after pt_irq_create_bind and use XEN_DOMCTL_VMSI_X86_UNMASKED.

Changes since v6:
- Use domain_spin_lock_irq_desc instead of open coding it.
- Reduce the size of printed debug messages.
- Constify domain in vpci_dump_msi.
- Lock domlist_read_lock before iterating over the list of domains.
- Make max_vectors and vectors uint8_t.
- Drop the vpci_ prefix from the static functions in msi.c.
- Turn the booleans in vpci_msi into bitfields.
- Apply the mask bits to all vectors when enabling msi.
- Remove the pos field.
- Remove the usage of __msi_set_{enable/disable}.
- Update the bindings when the message or data fields are updated.
- Make vpci_msi_arch_disable return void, it wasn't returning any error.
- Prevent the guest from writing to the pending bits field, it's read only as defined in the spec.
- Add the must_check attribute to vpci_msi_arch_enable.

Changes since v5:
- Update to new lock usage.
- Change handlers to match the new type.
- s/msi_flags/msi_gflags/, remove the local variables and use the new DOMCTL_VMSI_* defines.
- Change the MSI arch function to take a vpci_msi instead of a vpci_arch_msi as parameter.
- Fix the calculation of the guest vector for MSI injection to take into account the number of bits that can be modified.
- Use INVALID_PIRQ everywhere.
- Simplify exit path of vpci_msi_disable.
- Remove the conditional when setting address64 and masking fields.
- Add a process_pending_softirqs to the MSI dump loop.
- Place the prototypes for the MSI arch-specific functions in xen/vpci.h.
- Add parentheses around the INVALID_PIRQ definition.

Changes since v4:
- Fix commit message.
- Change the ASSERTs in vpci_msi_arch_mask into ifs.
- Introduce INVALID_PIRQ.
- Destroy the partially created bindings in case of failure in vpci_msi_arch_enable.
- Just take the pcidevs lock once in vpci_msi_arch_disable.
- Print an error message in case of failure of pt_irq_destroy_bind.
- Make vpci_msi_arch_init return void.
- Constify the arch parameter of vpci_msi_arch_print.
- Use fixed instead of cpu for msi redirection.
- Separate the header includes in vpci/msi.c between xen and asm.
- Store the number of configured vectors even if MSI is not enabled and always return it in vpci_msi_control_read.
- Fix/add comments in vpci_msi_control_write to clarify intended behavior.
- Simplify usage of masks in vpci_msi_address_{upper_}write.
- Add comment to vpci_msi_mask_{read/write}.
- Don't use MASK_EXTR in vpci_msi_mask_write.
- s/msi_offset/pos/ in vpci_init_msi.
- Move control variable setup closer to it's usage.
- Use d%d in vpci_dump_msi.
- Fix printing of bitfield mask in vpci_dump_msi.
- Fix definition of MSI_ADDR_REDIRECTION_MASK.
- Shuffle the layout of vpci_msi to minimize gaps.
- Remove the error label in vpci_init_msi.

Changes since v3:
- Propagate changes from previous versions: drop xen_ prefix, drop return value from handlers, use the new vpci_val fields.
- Use MASK_EXTR.
- Remove the usage of GENMASK.
- Add GFLAGS_SHIFT_DEST_ID and use it in msi_flags.
- Add "arch" to the MSI arch specific functions.
- Move the dumping of vPCI MSI information to dump_msi (key 'M').
- Remove the guest_vectors field.
- Allow the guest to change the number of active vectors without having to disable and enable MSI.
- Check the number of active vectors when parsing the disable mask.
- Remove the debug messages from vpci_init_msi.
- Move the arch-specific part of the dump handler to x86/hvm/vmsi.c.
- Use trylock in the dump handler to get the vpci lock.

Changes since v2:
- Add an arch-specific abstraction layer. Note that this is only implemented for x86 currently.
- Add a wrapper to detect MSI enabling for vPCI.

NB: I've only been able to test this with devices using a single MSI interrupt and no mask register. I will try to find hardware that supports the mask register and more than one vector, but I cannot make any promises.

If there are doubts about the untested parts we could always force Xen to report no per-vector masking support and only 1 available vector, but I would rather avoid doing it.

---
 xen/arch/x86/hvm/vmsi.c      | 142 +++++++++++++++++++
 xen/arch/x86/msi.c           |   3 +
 xen/drivers/vpci/Makefile    |   2 +-
 xen/drivers/vpci/msi.c       | 324 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/hvm/io.h |   5 +
 xen/include/asm-x86/msi.h    |   3 +
 xen/include/xen/irq.h        |   1 +
 xen/include/xen/vpci.h       |  36 +++++
 8 files changed, 515 insertions(+), 1 deletion(-)
 create mode 100644 xen/drivers/vpci/msi.c
xen/arch/x86/hvm/vmsi.c +142 -0
index 7126de7841..be59c56d43 100644
@@ -31,6 +31,7 @@
 #include <xen/errno.h>
 #include <xen/sched.h>
 #include <xen/irq.h>
+#include <xen/vpci.h>
 #include <public/hvm/ioreq.h>
 #include <asm/hvm/io.h>
 #include <asm/hvm/vpic.h>
@@ -621,3 +622,144 @@ void msix_write_completion(struct vcpu *v)
     if ( msixtbl_write(v, ctrl_address, 4, 0) != X86EMUL_OKAY )
         gdprintk(XENLOG_WARNING, "MSI-X write completion failure\n");
 }
+
+static unsigned int msi_gflags(uint16_t data, uint64_t addr, bool masked)
+{
+    /*
+     * We need to use the DOMCTL constants here because the output of this
+     * function is used as input to pt_irq_create_bind, which also takes the
+     * input from the DOMCTL itself.
+     */
+    return MASK_INSR(MASK_EXTR(addr, MSI_ADDR_DEST_ID_MASK),
+                     XEN_DOMCTL_VMSI_X86_DEST_ID_MASK) |
+           MASK_INSR(MASK_EXTR(addr, MSI_ADDR_REDIRECTION_MASK),
+                     XEN_DOMCTL_VMSI_X86_RH_MASK) |
+           MASK_INSR(MASK_EXTR(addr, MSI_ADDR_DESTMODE_MASK),
+                     XEN_DOMCTL_VMSI_X86_DM_MASK) |
+           MASK_INSR(MASK_EXTR(data, MSI_DATA_DELIVERY_MODE_MASK),
+                     XEN_DOMCTL_VMSI_X86_DELIV_MASK) |
+           MASK_INSR(MASK_EXTR(data, MSI_DATA_TRIGGER_MASK),
+                     XEN_DOMCTL_VMSI_X86_TRIG_MASK) |
+           /* NB: by default MSI vectors are bound masked. */
+           (masked ? 0 : XEN_DOMCTL_VMSI_X86_UNMASKED);
+}
+
+void vpci_msi_arch_mask(struct vpci_msi *msi, const struct pci_dev *pdev,
+                        unsigned int entry, bool mask)
+{
+    unsigned long flags;
+    struct irq_desc *desc = domain_spin_lock_irq_desc(pdev->domain,
+                                                      msi->arch.pirq + entry,
+                                                      &flags);
+
+    if ( !desc )
+        return;
+    guest_mask_msi_irq(desc, mask);
+    spin_unlock_irqrestore(&desc->lock, flags);
+}
+
+int vpci_msi_arch_enable(struct vpci_msi *msi, const struct pci_dev *pdev,
+                         unsigned int vectors)
+{
+    struct msi_info msi_info = {
+        .seg = pdev->seg,
+        .bus = pdev->bus,
+        .devfn = pdev->devfn,
+        .entry_nr = vectors,
+    };
+    unsigned int i;
+    int rc;
+
+    ASSERT(msi->arch.pirq == INVALID_PIRQ);
+
+    /* Get a PIRQ. */
+    rc = allocate_and_map_msi_pirq(pdev->domain, -1, &msi->arch.pirq,
+                                   MAP_PIRQ_TYPE_MULTI_MSI, &msi_info);
+    if ( rc )
+    {
+        gdprintk(XENLOG_ERR, "%04x:%02x:%02x.%u: failed to map PIRQ: %d\n",
+                 pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
+                 PCI_FUNC(pdev->devfn), rc);
+        return rc;
+    }
+
+    for ( i = 0; i < vectors; i++ )
+    {
+        uint8_t vector = MASK_EXTR(msi->data, MSI_DATA_VECTOR_MASK);
+        uint8_t vector_mask = 0xff >> (8 - fls(msi->vectors) + 1);
+        struct xen_domctl_bind_pt_irq bind = {
+            .machine_irq = msi->arch.pirq + i,
+            .irq_type = PT_IRQ_TYPE_MSI,
+            .u.msi.gvec = (vector & ~vector_mask) |
+                          ((vector + i) & vector_mask),
+            .u.msi.gflags = msi_gflags(msi->data, msi->address,
+                                       (msi->mask >> i) & 1),
+        };
+
+        pcidevs_lock();
+        rc = pt_irq_create_bind(pdev->domain, &bind);
+        if ( rc )
+        {
+            gdprintk(XENLOG_ERR,
+                     "%04x:%02x:%02x.%u: failed to bind PIRQ %u: %d\n",
+                     pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
+                     PCI_FUNC(pdev->devfn), msi->arch.pirq + i, rc);
+            while ( bind.machine_irq-- )
+                pt_irq_destroy_bind(pdev->domain, &bind);
+            spin_lock(&pdev->domain->event_lock);
+            unmap_domain_pirq(pdev->domain, msi->arch.pirq);
+            spin_unlock(&pdev->domain->event_lock);
+            pcidevs_unlock();
+            msi->arch.pirq = INVALID_PIRQ;
+            return rc;
+        }
+        pcidevs_unlock();
+    }
+
+    return 0;
+}
+
+void vpci_msi_arch_disable(struct vpci_msi *msi, const struct pci_dev *pdev)
+{
+    unsigned int i;
+
+    ASSERT(msi->arch.pirq != INVALID_PIRQ);
+
+    pcidevs_lock();
+    for ( i = 0; i < msi->vectors; i++ )
+    {
+        struct xen_domctl_bind_pt_irq bind = {
+            .machine_irq = msi->arch.pirq + i,
+            .irq_type = PT_IRQ_TYPE_MSI,
+        };
+        int rc;
+
+        rc = pt_irq_destroy_bind(pdev->domain, &bind);
+        ASSERT(!rc);
+    }
+
+    spin_lock(&pdev->domain->event_lock);
+    unmap_domain_pirq(pdev->domain, msi->arch.pirq);
+    spin_unlock(&pdev->domain->event_lock);
+    pcidevs_unlock();
+
+    msi->arch.pirq = INVALID_PIRQ;
+}
+
+void vpci_msi_arch_init(struct vpci_msi *msi)
+{
+    msi->arch.pirq = INVALID_PIRQ;
+}
+
+void vpci_msi_arch_print(const struct vpci_msi *msi)
+{
+    printk("vec=%#02x%7s%6s%3sassert%5s%7s dest_id=%lu pirq: %d\n",
+           MASK_EXTR(msi->data, MSI_DATA_VECTOR_MASK),
+           msi->data & MSI_DATA_DELIVERY_LOWPRI ? "lowest" : "fixed",
+           msi->data & MSI_DATA_TRIGGER_LEVEL ? "level" : "edge",
+           msi->data & MSI_DATA_LEVEL_ASSERT ? "" : "de",
+           msi->address & MSI_ADDR_DESTMODE_LOGIC ? "log" : "phys",
+           msi->address & MSI_ADDR_REDIRECTION_LOWPRI ? "lowest" : "fixed",
+           MASK_EXTR(msi->address, MSI_ADDR_DEST_ID_MASK),
+           msi->arch.pirq);
+}
xen/arch/x86/msi.c +3 -0
index 8c89f072a8..5567990fbd 100644
@@ -30,6 +30,7 @@
 #include <public/physdev.h>
 #include <xen/iommu.h>
 #include <xsm/xsm.h>
+#include <xen/vpci.h>
 
 static s8 __read_mostly use_msi = -1;
 boolean_param("msi", use_msi);
@@ -1527,6 +1528,8 @@ static void dump_msi(unsigned char key)
                attr.guest_masked ? 'G' : ' ',
                mask);
     }
+
+    vpci_dump_msi();
 }
 
 static int __init msi_setup_keyhandler(void)
xen/drivers/vpci/Makefile +1 -1
index 241467212f..62cec9e82b 100644
@@ -1 +1 @@
-obj-y += vpci.o header.o
+obj-y += vpci.o header.o msi.o
xen/drivers/vpci/msi.c new file +324 -0

324 lines not on this page · raw message

xen/include/asm-x86/hvm/io.h +5 -0
index 16465ceb30..0fedb3473c 100644
@@ -127,6 +127,11 @@ void hvm_dpci_eoi(struct domain *d, unsigned int guest_irq,
 void msix_write_completion(struct vcpu *);
 void msixtbl_init(struct domain *d);
 
+/* Arch-specific MSI data for vPCI. */
+struct vpci_arch_msi {
+    int pirq;
+};
+
 enum stdvga_cache_state {
     STDVGA_CACHE_UNINITIALIZED,
     STDVGA_CACHE_ENABLED,
xen/include/asm-x86/msi.h +3 -0
index 37d37b820e..10387dce2e 100644
@@ -48,6 +48,7 @@
 #define MSI_ADDR_REDIRECTION_SHIFT  3
 #define MSI_ADDR_REDIRECTION_CPU    (0 << MSI_ADDR_REDIRECTION_SHIFT)
 #define MSI_ADDR_REDIRECTION_LOWPRI (1 << MSI_ADDR_REDIRECTION_SHIFT)
+#define MSI_ADDR_REDIRECTION_MASK   (1 << MSI_ADDR_REDIRECTION_SHIFT)
 
 #define MSI_ADDR_DEST_ID_SHIFT		12
 #define	 MSI_ADDR_DEST_ID_MASK		0x00ff000
@@ -152,6 +153,8 @@ int msi_free_irq(struct msi_desc *entry);
 	( (is64bit == 1) ? base+PCI_MSI_DATA_64 : base+PCI_MSI_DATA_32 )
 #define msi_mask_bits_reg(base, is64bit) \
 	( (is64bit == 1) ? base+PCI_MSI_MASK_BIT : base+PCI_MSI_MASK_BIT-4)
+#define msi_pending_bits_reg(base, is64bit) \
+	((base) + PCI_MSI_MASK_BIT + ((is64bit) ? 4 : 0))
 #define msi_disable(control)		control &= ~PCI_MSI_FLAGS_ENABLE
 #define multi_msi_capable(control) \
 	(1 << ((control & PCI_MSI_FLAGS_QMASK) >> 1))
xen/include/xen/irq.h +1 -0
index 0aa817e266..586b78393a 100644
@@ -133,6 +133,7 @@ struct pirq {
     struct arch_pirq arch;
 };
 
+#define INVALID_PIRQ (-1)
 #define pirq_info(d, p) ((struct pirq *)radix_tree_lookup(&(d)->pirq_tree, p))
 
 /* Use this instead of pirq_info() if the structure may need allocating. */
xen/include/xen/vpci.h +36 -0
index 6ae254407f..5afd54b2e8 100644
@@ -79,6 +79,28 @@ struct vpci {
         bool rom_enabled      : 1;
         /* FIXME: currently there's no support for SR-IOV. */
     } header;
+
+    /* MSI data. */
+    struct vpci_msi {
+        /* Address. */
+        uint64_t address;
+        /* Mask bitfield. */
+        uint32_t mask;
+        /* Data. */
+        uint16_t data;
+        /* Maximum number of vectors supported by the device. */
+        uint8_t max_vectors : 5;
+        /* Enabled? */
+        bool enabled        : 1;
+        /* Supports per-vector masking? */
+        bool masking        : 1;
+        /* 64-bit address capable? */
+        bool address64      : 1;
+        /* Number of vectors configured. */
+        uint8_t vectors     : 5;
+        /* Arch-specific data. */
+        struct vpci_arch_msi arch;
+    } *msi;
 #endif
 };
 
@@ -90,6 +112,20 @@ struct vpci_vcpu {
     bool rom : 1;
 };
 
+#ifdef __XEN__
+void vpci_dump_msi(void);
+
+/* Arch-specific vPCI MSI helpers. */
+void vpci_msi_arch_mask(struct vpci_msi *msi, const struct pci_dev *pdev,
+                        unsigned int entry, bool mask);
+int __must_check vpci_msi_arch_enable(struct vpci_msi *msi,
+                                      const struct pci_dev *pdev,
+                                      unsigned int vectors);
+void vpci_msi_arch_disable(struct vpci_msi *msi, const struct pci_dev *pdev);
+void vpci_msi_arch_init(struct vpci_msi *msi);
+void vpci_msi_arch_print(const struct vpci_msi *msi);
+#endif /* __XEN__ */
+
 #else /* !CONFIG_HAS_PCI */
 struct vpci_vpcu {
 };
Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v8 9/11
#19 lore raw

Re: [PATCH v8 09/11] vpci/msi: add MSI handlers

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:

Add handlers for the MSI control, address, data and mask fields in order to detect accesses to them and setup the interrupts as requested by the guest.

Note that the pending register is not trapped, and the guest can freely read/write to it.

Quoted 13 lines

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Paul Durrant <[email protected]>
---
Changes since v7:
- Don't store pci segment/bus on local variables.
- Add an error label to init_msi.
- Don't trap accesses to the PBA.
- Fix msi_pending_bits_reg macro so it matches coding style.
- Move the position of vectors in the vpci_msi struct.
- Add a comment to clarify the expected state of vectors after pt_irq_create_bind and use XEN_DOMCTL_VMSI_X86_UNMASKED.

And this long list did not invalidate Paul's R-b?

Quoted 7 lines

NB: I've only been able to test this with devices using a single MSI interrupt and no mask register. I will try to find hardware that supports the mask register and more than one vector, but I cannot make any promises.

If there are doubts about the untested parts we could always force Xen to report no per-vector masking support and only 1 available vector, but I would rather avoid doing it.

Has this become stale meanwhile, or is this still untested? In any event, I agree we shouldn't add hacks, but there should be a record somewhere of things needing to be done (here: tested) before the whole Dom0 thing can be marked supported. Perhaps just a fixme-like comment in the code.

Quoted 68 lines
xen/drivers/vpci/msi.c new file +68 -0
+static int init_msi(struct pci_dev *pdev)
+{
+    uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn);
+    struct vpci_msi *msi;
+    unsigned int pos = pci_find_cap_offset(pdev->seg, pdev->bus, slot, func,
+                                           PCI_CAP_ID_MSI);
+    uint16_t control;
+    int ret;
+
+    if ( !pos )
+        return 0;
+
+    msi = xzalloc(struct vpci_msi);
+    if ( !msi )
+        return -ENOMEM;
+
+    ret = vpci_add_register(pdev->vpci, control_read, control_write,
+                            msi_control_reg(pos), 2, msi);
+    if ( ret )
+        goto error;
+
+    /* Get the maximum number of vectors the device supports. */
+    control = pci_conf_read16(pdev->seg, pdev->bus, slot, func,
+                              msi_control_reg(pos));
+    msi->max_vectors = multi_msi_capable(control);
+    ASSERT(msi->max_vectors <= 32);
+
+    /* The multiple message enable is 0 after reset (1 message enabled). */
+    msi->vectors = 1;
+
+    /* No PIRQ bound yet. */
+    vpci_msi_arch_init(msi);
+
+    msi->address64 = is_64bit_address(control);
+    msi->masking = is_mask_bit_support(control);
+
+    ret = vpci_add_register(pdev->vpci, address_read, address_write,
+                            msi_lower_address_reg(pos), 4, msi);
+    if ( ret )
+        goto error;
+
+    ret = vpci_add_register(pdev->vpci, data_read, data_write,
+                            msi_data_reg(pos, msi->address64), 2,
+                            msi);
+    if ( ret )
+        goto error;
+
+    if ( msi->address64 )
+    {
+        ret = vpci_add_register(pdev->vpci, address_hi_read, address_hi_write,
+                                msi_upper_address_reg(pos), 4, msi);
+        if ( ret )
+            goto error;
+    }
+
+    if ( msi->masking )
+    {
+        ret = vpci_add_register(pdev->vpci, mask_read, mask_write,
+                                msi_mask_bits_reg(pos, msi->address64), 4,
+                                msi);
+        if ( ret )
+            goto error;
+        /*
+         * NB: do not add any handler for the pending bits for the hardware
+         * domain, which means direct access. This will be revisited when
+         * adding unprivileged domain support.
+         */
+    }

xen/drivers/vpci/msi.c line 266

Quoted 10 lines
xen/drivers/vpci/msi.c new file +10 -0
+
+    pdev->vpci->msi = msi;
+
+    return 0;
+
+ error:
+    ASSERT(ret);
+    xfree(msi);
+
+    return ret;

Can you please add a short comment here making clear why all the vpci_add_register() above don't need undoing here? Otherwise, just like me every time I come across this, readers will get the impression that there's something being leaked here.

With both of the comment additions taken care of

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#20 lore raw

PATCH v8 10/11 vpci: add a priority parameter to the vPCI register initializer

In reply to Roger Pau Monne

This is needed for MSI-X, since MSI-X will need to be initialized before parsing the BARs, so that the header BAR handlers are aware of the MSI-X related holes and make sure they are not mapped in order for the trap handlers to work properly.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
---
Changes since v4:
- Add a middle priority and add the PCI header to it.

Changes since v3:
- Add a numerial suffix to the section used to store the pointer to each initializer function, and sort them at link time.

---
 xen/arch/arm/xen.lds.S    | 4 ++--
 xen/arch/x86/xen.lds.S    | 4 ++--
 xen/drivers/vpci/header.c | 2 +-
 xen/drivers/vpci/msi.c    | 2 +-
 xen/include/xen/vpci.h    | 8 ++++++--
 5 files changed, 12 insertions(+), 8 deletions(-)
xen/arch/arm/xen.lds.S +2 -2
index 98b82680c6..f2e213b3c2 100644
@@ -69,7 +69,7 @@ SECTIONS
 #if defined(CONFIG_HAS_PCI) && defined(CONFIG_LATE_HWDOM)
        . = ALIGN(POINTER_ALIGN);
        __start_vpci_array = .;
-       *(.data.vpci)
+       *(SORT(.data.vpci.*))
        __end_vpci_array = .;
 #endif
   } :text
@@ -184,7 +184,7 @@ SECTIONS
 #if defined(CONFIG_HAS_PCI) && !defined(CONFIG_LATE_HWDOM)
        . = ALIGN(POINTER_ALIGN);
        __start_vpci_array = .;
-       *(.data.vpci)
+       *(SORT(.data.vpci.*))
        __end_vpci_array = .;
 #endif
   } :text
xen/arch/x86/xen.lds.S +2 -2
index 96e53f68a8..19a912245a 100644
@@ -132,7 +132,7 @@ SECTIONS
 #if defined(CONFIG_HAS_PCI) && defined(CONFIG_LATE_HWDOM)
        . = ALIGN(POINTER_ALIGN);
        __start_vpci_array = .;
-       *(.data.vpci)
+       *(SORT(.data.vpci.*))
        __end_vpci_array = .;
 #endif
   } :text
@@ -230,7 +230,7 @@ SECTIONS
 #if defined(CONFIG_HAS_PCI) && !defined(CONFIG_LATE_HWDOM)
        . = ALIGN(POINTER_ALIGN);
        __start_vpci_array = .;
-       *(.data.vpci)
+       *(SORT(.data.vpci.*))
        __end_vpci_array = .;
 #endif
   } :text
xen/drivers/vpci/header.c +1 -1
index 994cfbb8f7..98ed53a126 100644
@@ -508,7 +508,7 @@ static int init_bars(struct pci_dev *pdev)
 
     return 0;
 }
-REGISTER_VPCI_INIT(init_bars);
+REGISTER_VPCI_INIT(init_bars, VPCI_PRIORITY_MIDDLE);
 
 /*
  * Local variables:
xen/drivers/vpci/msi.c +1 -1
index 9da687a5d7..f8f08cb515 100644
@@ -265,7 +265,7 @@ static int init_msi(struct pci_dev *pdev)
 
     return ret;
 }
-REGISTER_VPCI_INIT(init_msi);
+REGISTER_VPCI_INIT(init_msi, VPCI_PRIORITY_LOW);
 
 void vpci_dump_msi(void)
 {
xen/include/xen/vpci.h +6 -2
index 5afd54b2e8..6e810801d6 100644
@@ -15,9 +15,13 @@ typedef void vpci_write_t(const struct pci_dev *pdev, unsigned int reg,
 
 typedef int vpci_register_init_t(struct pci_dev *dev);
 
-#define REGISTER_VPCI_INIT(x)                   \
+#define VPCI_PRIORITY_HIGH      "1"
+#define VPCI_PRIORITY_MIDDLE    "5"
+#define VPCI_PRIORITY_LOW       "9"
+
+#define REGISTER_VPCI_INIT(x, p)                \
   static vpci_register_init_t *const x##_entry  \
-               __used_section(".data.vpci") = x
+               __used_section(".data.vpci." p) = x
 
 /* Add vPCI handlers to device. */
 int __must_check vpci_add_handlers(struct pci_dev *dev);
Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#21 lore raw

PATCH v8 11/11 vpci/msix: add MSI-X handlers

In reply to Roger Pau Monne

Add handlers for accesses to the MSI-X message control field on the PCI configuration space, and traps for accesses to the memory region that contains the MSI-X table and PBA. This traps detect attempts from the guest to configure MSI-X interrupts and properly sets them up.

Note that accesses to the Table Offset, Table BIR, PBA Offset and PBA BIR are not trapped by Xen at the moment.

Finally, turn the panic in the Dom0 PVH builder into a warning.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
---
Changes since v7:
- Switch vpci.h macros to inline functions.
- Change vpci_msix_arch_print_entry into vpci_msix_arch_print and make it print all the entries.
- Add a log message if rangeset_remove_range fails to remove the BAR MSI-related range.
- Introduce a new update_entry to disable and enable a MSIX entry in order to either update or set it up. This removes open coding it in two different places.
- Unify access checks in access_allowed.
- Add newlines between switch cases.
- Expand max_entries to 12 bits.

Changes since v6:
- Reduce the output of the debug keys.
- Fix comments and code to match in vpci_msix_control_write.
- Optimize size of the MSIX structure.
- Convert 'tables[]' to a uint32_t in order to reduce the size of vpci_msix. Introduce some macros to make it easier to get the MSIX tables related data.
- Limit size of the bool fields to 1 bit.
- Remove the 'nr' field of vpci_msix_entry. The position can be calculated from the base of the entries array.
- Drop the 'vpci_' prefix from the functions in msix.c, they are all static.
- Remove the val local variable in control_read.
- Initialize new_masked and new_enabled at declaration.
- Recalculate the msix control value before writing it.
- Remove the seg and bus local variables and use pdev->seg and pdev->bus instead.
- Initialize msix at declaration in msix_{write/read}.
- Add the must_check attribute to vpci_msix_arch_{enable/disable}_entry.

Changes since v5:
- Update lock usage.
- Unbind/unmap PIRQs when MSIX is disabled.
- Share the arch-specific MSIX code with the MSI functions.
- Do not reference the MSIX memory areas from the PCI BARs fields, instead fetch the BIR and offset each time needed.
- Add the '_entry' suffix to the MSIX arch functions.
- Prefix the vMSIX macros with 'V'.
- s/gdprintk/gprintk/ in msix.c
- Make vpci_msix_access_check return bool, and change it's name to vpci_msix_access_allowed.
- Join the first two ifs in vpci_msix_{read/write} into a single one.
- Allow Dom0 to write to the PBA area.
- Add a note that reads from the PBA area will need to be translated if the PBA it's not identity mapped.

Changes since v4:
- Remove parentheses around offsetof.
- Add "being" to MSI-X enabling comment.
- Use INVALID_PIRQ.
- Add a simple sanity check to vpci_msix_arch_enable in order to detect wrong MSI-X entries more quickly.
- Constify vpci_msix_arch_print entry argument.
- s/cpu/fixed/ in vpci_msix_arch_print.
- Dump the MSI-X info together with the MSI info.
- Fix vpci_msix_control_write to take into account changes to the address and data fields when switching the function mask bit.
- Only disable/enable the entries if the address or data fields have been updated.
- Usew the BAR enable field to check if a BAR is mapped or not (instead of reading the command register for each device).
- Fix error path in vpci_msix_read to set the return data to ~0.
- Simplify mask usage in vpci_msix_write.
- Cast data to uint64_t when shifting it 32 bits.
- Fix writes to the table entry control register to take into account if the mask-all bit is set.
- Add some comments to clarify the intended behavior of the code.
- Align the PBA size to 64-bits.
- Remove the error label in vpci_init_msix.
- Try to compact the layout of the vpci_msix structure.
- Remove the local table_bar and pba_bar variables from vpci_init_msix, they are used only once.

Changes since v3:
- Propagate changes from previous versions: remove xen_ prefix, use the new fields in vpci_val and remove the return value from handlers.
- Remove the usage of GENMASK.
- Mave the arch-specific parts of the dump routine to the x86/hvm/vmsi.c dump handler.
- Chain the MSI-X dump handler to the 'M' debug key.
- Fix the header BAR mappings so that the MSI-X regions inside of BARs are unmapped from the domain p2m in order for the handlers to work properly.
- Unconditionally trap and forward accesses to the PBA MSI-X area.
- Simplify the conditionals in vpci_msix_control_write.
- Fix vpci_msix_accept to use a bool type.
- Allow all supported accesses as described in the spec to the MSI-X table.
- Truncate the returned address when the access is a 32b read.
- Always return X86EMUL_OKAY from the handlers, returning ~0 in the read case if the access is not supported, or ignoring writes.
- Do not check that max_entries is != 0 in the init handler.
- Use trylock in the dump handler.

Changes since v2:
- Split out arch-specific code.

This patch has been tested with devices using both a single MSI-X entry and multiple ones.

---
 xen/arch/x86/hvm/dom0_build.c    |   2 +-
 xen/arch/x86/hvm/hvm.c           |   1 +
 xen/arch/x86/hvm/vmsi.c          | 142 +++++++++---
 xen/drivers/vpci/Makefile        |   2 +-
 xen/drivers/vpci/header.c        |  18 ++
 xen/drivers/vpci/msi.c           |  20 +-
 xen/drivers/vpci/msix.c          | 464 +++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/hvm/domain.h |   3 +
 xen/include/asm-x86/hvm/io.h     |   5 +
 xen/include/xen/vpci.h           |  71 ++++++
 10 files changed, 695 insertions(+), 33 deletions(-)
 create mode 100644 xen/drivers/vpci/msix.c
xen/arch/x86/hvm/dom0_build.c +1 -1
index e511809766..72f6baf601 100644
@@ -1118,7 +1118,7 @@ int __init dom0_construct_pvh(struct domain *d, const module_t *image,
 
     pvh_setup_mmcfg(d);
 
-    panic("Building a PVHv2 Dom0 is not yet supported.");
+    printk("WARNING: PVH is an experimental mode with limited functionality\n");
     return 0;
 }
 
xen/arch/x86/hvm/hvm.c +1 -0
index 42369d34cb..961ff442d1 100644
@@ -589,6 +589,7 @@ int hvm_domain_initialise(struct domain *d, unsigned long domcr_flags,
     INIT_LIST_HEAD(&d->arch.hvm_domain.write_map.list);
     INIT_LIST_HEAD(&d->arch.hvm_domain.g2m_ioport_list);
     INIT_LIST_HEAD(&d->arch.hvm_domain.mmcfg_regions);
+    INIT_LIST_HEAD(&d->arch.hvm_domain.msix_tables);
 
     rc = create_perdomain_mapping(d, PERDOMAIN_VIRT_START, 0, NULL, NULL);
     if ( rc )
xen/arch/x86/hvm/vmsi.c +114 -28

211 lines not on this page · raw message

xen/drivers/vpci/Makefile +1 -1
index 62cec9e82b..55d1bdfda0 100644
@@ -1 +1 @@
-obj-y += vpci.o header.o msi.o
+obj-y += vpci.o header.o msi.o msix.o
xen/drivers/vpci/header.c +18 -0
index 98ed53a126..5c5e91d8eb 100644
@@ -210,6 +210,7 @@ static void modify_bars(const struct pci_dev *pdev, bool map, bool rom)
     struct vpci_header *header = &pdev->vpci->header;
     struct rangeset *mem = rangeset_new(NULL, NULL, 0);
     const struct pci_dev *tmp;
+    const struct vpci_msix *msix = pdev->vpci->msix;
     unsigned int i;
     int rc;
 
@@ -255,6 +256,23 @@ static void modify_bars(const struct pci_dev *pdev, bool map, bool rom)
         }
     }
 
+    /* Remove any MSIX regions if present. */
+    for ( i = 0; msix && i < ARRAY_SIZE(msix->tables); i++ )
+    {
+        paddr_t start = vmsix_table_addr(pdev->vpci, i);
+        paddr_t end = start + vmsix_table_size(pdev->vpci, i) - 1;
+
+        rc = rangeset_remove_range(mem, PFN_DOWN(start), PFN_UP(end));
+        if ( rc )
+        {
+            printk(XENLOG_G_WARNING
+                   "Failed to remove MSIX table [%" PRI_gfn ", %" PRI_gfn "): %d\n",
+                   PFN_DOWN(start), PFN_UP(end), rc);
+            rangeset_destroy(mem);
+            return;
+        }
+    }
+
     /*
      * Check for overlaps with other BARs. Note that only BARs that are
      * currently mapped (enabled) are checked for overlaps.
xen/drivers/vpci/msi.c +17 -3
index f8f08cb515..3a81fd170c 100644
@@ -279,11 +279,16 @@ void vpci_dump_msi(void)
         if ( !has_vpci(d) )
             continue;
 
-        printk("vPCI MSI d%d\n", d->domain_id);
+        printk("vPCI MSI/MSI-X d%d\n", d->domain_id);
 
         list_for_each_entry ( pdev, &d->arch.pdev_list, domain_list )
         {
             const struct vpci_msi *msi = pdev->vpci->msi;
+            const struct vpci_msix *msix = pdev->vpci->msix;
+
+            if ( msi || msix )
+                printk("%04x:%02x:%02x.%u\n", pdev->seg, pdev->bus,
+                       PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
 
             if ( !spin_trylock(&pdev->vpci->lock) )
             {
@@ -293,8 +298,7 @@ void vpci_dump_msi(void)
 
             if ( msi )
             {
-                printk("%04x:%02x:%02x.%u\n", pdev->seg, pdev->bus,
-                       PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
+                printk(" MSI\n");
 
                 printk("  enabled: %d 64-bit: %d",
                        msi->enabled, msi->address64);
@@ -306,6 +310,16 @@ void vpci_dump_msi(void)
                 vpci_msi_arch_print(msi);
             }
 
+            if ( msix )
+            {
+                printk(" MSI-X\n");
+
+                printk("  entries: %u maskall: %d enabled: %d\n",
+                       msix->max_entries, msix->masked, msix->enabled);
+
+                vpci_msix_arch_print(msix);
+            }
+
             spin_unlock(&pdev->vpci->lock);
             process_pending_softirqs();
         }
xen/drivers/vpci/msix.c new file +464 -0

464 lines not on this page · raw message

xen/include/asm-x86/hvm/domain.h +3 -0
index d1d933d791..020ceacd81 100644
@@ -188,6 +188,9 @@ struct hvm_domain {
     struct list_head mmcfg_regions;
     rwlock_t mmcfg_lock;
 
+    /* List of MSI-X tables. */
+    struct list_head msix_tables;
+
     /* List of permanently write-mapped pages. */
     struct {
         spinlock_t lock;
xen/include/asm-x86/hvm/io.h +5 -0
index 0fedb3473c..e6b6ed0b92 100644
@@ -132,6 +132,11 @@ struct vpci_arch_msi {
     int pirq;
 };
 
+/* Arch-specific MSI-X entry data for vPCI. */
+struct vpci_arch_msix_entry {
+    int pirq;
+};
+
 enum stdvga_cache_state {
     STDVGA_CACHE_UNINITIALIZED,
     STDVGA_CACHE_ENABLED,
xen/include/xen/vpci.h +71 -0

83 lines not on this page · raw message

Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#22 lore raw

Re: [PATCH v8 11/11] vpci/msix: add MSI-X handlers

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:
xen/arch/x86/hvm/vmsi.c +8 -0
+void vpci_msix_arch_print(const struct vpci_msix *msix)
+{
+    unsigned int i;
+
+    for ( i = 0; i < msix->max_entries; i++ )
+    {
+        const struct vpci_msix_entry *entry = &msix->entries[i];
+

xen/arch/x86/hvm/vmsi.c void vpci_msi_arch_print(const struct vpci_msi *msi) · line 850

Quoted 10 lines
xen/arch/x86/hvm/vmsi.c +10 -0
+        printk("%6u vec=%02x%7s%6s%3sassert%5s%7s dest_id=%lu mask=%u pirq: %d\n",
+               i, MASK_EXTR(entry->data, MSI_DATA_VECTOR_MASK),
+               entry->data & MSI_DATA_DELIVERY_LOWPRI ? "lowest" : "fixed",
+               entry->data & MSI_DATA_TRIGGER_LEVEL ? "level" : "edge",
+               entry->data & MSI_DATA_LEVEL_ASSERT ? "" : "de",
+               entry->addr & MSI_ADDR_DESTMODE_LOGIC ? "log" : "phys",
+               entry->addr & MSI_ADDR_REDIRECTION_LOWPRI ? "lowest" : "fixed",
+               MASK_EXTR(entry->addr, MSI_ADDR_DEST_ID_MASK),
+               entry->masked, entry->arch.pirq);
+    }

In case you have 2k entries to print, you surely want to make sure events get processed every once in a while.

Quoted 8 lines
xen/drivers/vpci/header.c +4 -0
@@ -255,6 +256,23 @@ static void modify_bars(const struct pci_dev *pdev, bool map, bool rom)
         }
     }
 
+    /* Remove any MSIX regions if present. */
+    for ( i = 0; msix && i < ARRAY_SIZE(msix->tables); i++ )
+    {
+        paddr_t start = vmsix_table_addr(pdev->vpci, i);

xen/drivers/vpci/header.c static void modify_bars(const struct pci_dev *pdev, bool map, bool rom) · line 272

Quoted 10 lines
xen/drivers/vpci/header.c +10 -0
+        paddr_t end = start + vmsix_table_size(pdev->vpci, i) - 1;
+
+        rc = rangeset_remove_range(mem, PFN_DOWN(start), PFN_UP(end));
+        if ( rc )
+        {
+            printk(XENLOG_G_WARNING
+                   "Failed to remove MSIX table [%" PRI_gfn ", %" PRI_gfn "): %d\n",
+                   PFN_DOWN(start), PFN_UP(end), rc);
+            rangeset_destroy(mem);
+            return;

On v7 I had said "Silent discarding of an error also needs an explanation in a comment." You've added a printk() instead of a comment; the discarding of the error remains silent that way from the perspective of the caller (it's only verbose as far as the admin goes), and still provides no explanation.

xen/drivers/vpci/msi.c void vpci_dump_msi(void) · line 301

Quoted 7 lines
xen/drivers/vpci/msi.c +1 -2
@@ -293,8 +298,7 @@ void vpci_dump_msi(void)
 
             if ( msi )
             {
-                printk("%04x:%02x:%02x.%u\n", pdev->seg, pdev->bus,
-                       PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
+                printk(" MSI\n");

For this as well as ...

xen/drivers/vpci/msi.c void vpci_dump_msi(void) · line 315

Quoted 7 lines
xen/drivers/vpci/msi.c +3 -0
@@ -306,6 +310,16 @@ void vpci_dump_msi(void)
                 vpci_msi_arch_print(msi);
             }
 
+            if ( msix )
+            {
+                printk(" MSI-X\n");

... this: Can we please avoid logging such short (and meaningless on their own) lines? MSI and MSI-X shouldn't be enabled at the same time, so what's wrong with adding this to the line with the device SBDF?

Quoted 15 lines
xen/drivers/vpci/msix.c new file +15 -0
+static int update_entry(struct vpci_msix_entry *entry,
+                        const struct pci_dev *pdev, unsigned int nr)
+{
+    uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn);
+    int rc = vpci_msix_arch_disable_entry(entry, pdev);
+
+    /* Ignore ENOENT, it means the entry wasn't setup. */
+    if ( rc && rc != -ENOENT )
+    {
+        gprintk(XENLOG_WARNING,
+                "%04x:%02x:%02x.%u: unable to disable entry %u for update: %d\n",
+                pdev->seg, pdev->bus, slot, func, nr, rc);
+        return rc;
+    }
+

xen/drivers/vpci/msix.c line 65

Quoted 10 lines
xen/drivers/vpci/msix.c new file +10 -0
+    rc = vpci_msix_arch_enable_entry(entry, pdev,
+                                     vmsix_table_base(pdev->vpci,
+                                                      VPCI_MSIX_TABLE));
+    if ( rc )
+    {
+        gprintk(XENLOG_WARNING,
+                "%04x:%02x:%02x.%u: unable to enable entry %u: %d\n",
+                pdev->seg, pdev->bus, slot, func, nr, rc);
+        /* Entry is likely not properly configured, skip it. */
+        return rc;

The "skip" part of the comment isn't really applicable here.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#23 lore raw

Re: [PATCH v8 11/11] vpci/msix: add MSI-X handlers

Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 23.01.18 at 16:07, <[email protected]> wrote:
xen/drivers/vpci/header.c +12 -0
@@ -255,6 +256,23 @@ static void modify_bars(const struct pci_dev *pdev, bool map, bool rom)
         }
     }
 
+    /* Remove any MSIX regions if present. */
+    for ( i = 0; msix && i < ARRAY_SIZE(msix->tables); i++ )
+    {
+        paddr_t start = vmsix_table_addr(pdev->vpci, i);
+        paddr_t end = start + vmsix_table_size(pdev->vpci, i) - 1;
+
+        rc = rangeset_remove_range(mem, PFN_DOWN(start), PFN_UP(end));
+        if ( rc )
+        {
+            printk(XENLOG_G_WARNING
+                   "Failed to remove MSIX table [%" PRI_gfn ", %" PRI_gfn "): %d\n",
+                   PFN_DOWN(start), PFN_UP(end), rc);
Quoted 8 lines

xen/drivers/vpci/header.c static void modify_bars(const struct pci_dev *pdev, bool map, bool rom) · line 272

Quoted 2 lines
xen/drivers/vpci/header.c +2 -0
+            rangeset_destroy(mem);
+            return;

On v7 I had said "Silent discarding of an error also needs an explanation in a comment." You've added a printk() instead of a comment; the discarding of the error remains silent that way from the perspective of the caller (it's only verbose as far as the admin goes), and still provides no explanation.

Right, cmd_write and rom_write don't really care about the error because there's nothing those functions can do about it.

However init_bars cares about the error, in which case this function should return and the error should be propagated to the caller of init_bars.

Quoted 19 lines
Quoted 19 lines
xen/drivers/vpci/msix.c new file +19 -0
+static int update_entry(struct vpci_msix_entry *entry,
+                        const struct pci_dev *pdev, unsigned int nr)
+{
+    uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn);
+    int rc = vpci_msix_arch_disable_entry(entry, pdev);
+
+    /* Ignore ENOENT, it means the entry wasn't setup. */
+    if ( rc && rc != -ENOENT )
+    {
+        gprintk(XENLOG_WARNING,
+                "%04x:%02x:%02x.%u: unable to disable entry %u for update: %d\n",
+                pdev->seg, pdev->bus, slot, func, nr, rc);
+        return rc;
+    }
+
+    rc = vpci_msix_arch_enable_entry(entry, pdev,
+                                     vmsix_table_base(pdev->vpci,
+                                                      VPCI_MSIX_TABLE));
+    if ( rc )
Quoted 8 lines

xen/drivers/vpci/msix.c line 65

Quoted 6 lines
xen/drivers/vpci/msix.c new file +6 -0
+    {
+        gprintk(XENLOG_WARNING,
+                "%04x:%02x:%02x.%u: unable to enable entry %u: %d\n",
+                pdev->seg, pdev->bus, slot, func, nr, rc);
+        /* Entry is likely not properly configured, skip it. */
+        return rc;

The "skip" part of the comment isn't really applicable here.

Correct. This code was probably moved and the comment is now stall.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel