Skip to contentSkip to conversation

tools/hvmloader: implement Intel IGD extended VBT support

PATCH In review Chuck Zmudzinski 2 participants 4 messages Sep 13 – Sep 23 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

v4 cover tools: Intel IGD fixes

The goal of this series is to provide improved support for Intel graphics device (IGD) passthrough to Xen HVM guests which is currently not good. It started with a 3-patch series for the Qemu device model and has grown to a seven-patch series for the Qemu device model and this 2-patch series for the Xen tools.

Most of the fixes needed for Intel IGD passthrough are in the device model but help is also needed from the Xen tools to provide proper support for newer Intel IGD devices with an extended video bios table (VBT).

For the purpose of reviewing the two patches in this series, by far the most relevant patch in the patch series for the Qemu device model is Patch 6. At the least, reviewers of these two patches should also be familiar with Patch 6 of the companion patch series to the Qemu device model that is available here:

https://lore.kernel.org/qemu-devel/[email protected]/

For further reference, the link to the cover letter of the latest version (v6) of the companion Qemu device model patch series is available here:

https://lore.kernel.org/qemu-devel/[email protected]/

Please refer to each patch in this series and in the companion patch series for the Qemu device model for further details.

Chuck Zmudzinski (2):
tools/hvmloader: implement Intel IGD extended VBT support
libxl: add OpRegion and VBT to firmware when assigning IGD

 tools/firmware/hvmloader/config.h |  6 +-
 tools/firmware/hvmloader/e820.c   |  4 +-
 tools/firmware/hvmloader/pci.c    | 96 ++++++++++++++++++++++++++++++-
 tools/libs/light/libxl_pci.c      | 82 ++++++++++++++++++++++++++
 4 files changed, 182 insertions(+), 6 deletions(-)
Signature · 1 line
2.52.0
#2 lore raw

PATCH v4 1/2 tools/hvmloader: implement Intel IGD extended VBT support

Modern Intel IGD devices do not work well with the current implementation of support for the Intel IGD in hvmloader because it lacks support for an extended video bios table (VBT).

Code 43 errors in Windows guests and failure of the guest screen to light up are some of the problems that occur with the current implementation.

To address this problem, this patch implements support for Intel IGD devices with an extended VBT and OpRegion version 2+ which is required for most modern Intel IGD devices, as noted in the Linux kernel vfio commits referenced in the Link tags below. This patch ports support for devices with an extended VBT and OpRegion 2+ which was added in those commits for KVM/vfio, but adapted for Xen HVM guests with PCI passthrough.

This patch depends on compatible support in the device model. If hvmloader detects the device model lacks such support, it will fall back to the currently implemented protocol for configuring the OpRegion to provide backward compatibiltiy for systems that lack a device model with support for an extended VBT.

The primary reason the OpRegion needs to be patched in some cases is that with the addition of the RVDA and RVDS fields to the OpRegion, the OpRegion is not position-independent and may need to be patched if it is moved to a different address in the guest. This means the current protocol of having the device model directly map the unmodified host OpRegion to the guest is not compatible with the requiremnts of the newer devices that in some cases require that the OpRegion be modified for it to be compatible with the guest address space.

In this implementation, the device model has the responsibility to read the host OpRegion and patch it as needed before exposing it to the guest. Since an extended VBT means more pages are needed for the OpRegion, depending on the size of the extended VBT, hvmloader has the responsibility to edit the E820 map to accomodate the additional pages needed to contain the OpRegion + VBT.

To implement this in hvmloader, use a variable, igd_opregion_e820_pages, instead of the constant, IGD_OPREGION_PAGES, to represent the number of pages to reserve in the E820 map for the OpRegion + VBT. Also, to remove the confusion introduced by setting IGD_OPREGION_PAGES to 3 in an earlier patch to account for the fact that the OpRegion is not guaranteed to be aligned on a page boundary, reset IGD_OPREGION_PAGES to 2 so it matches the actual size of the OpRegion.

Instead of only writing to the PCI_INTEL_OPREGION register, first read from it to provide a way for both hvmloader and the device model to discover if both components have support for an extended VBT and more than 3 pages reserved for the OpRegion + VBT. The device model detects the read of the register before the write to learn that hvmloader has support, and hvmoader detects that the device model returns the number of pages to reserve for the OpRegion + VBT instead of 0 when it first reads the register to learn that that the device model has support. When this new protocol is supported by both hvmloader and the device model, the device model will not expose the host OpRegion directly to the guest via a direct mapping as the old protocol does but instead exposes an emulated copy of the OpRegion and VBT using its ioreq server. This has the added benefit of preventing extraneous host memory in the regions before or after a non-page-aligned OpRegion that should be confidential to the host from being exposed to the guest.

Testing reveals that when the device model exposes the OpRegion to the guest by mapping it to the device model's ioreq server, the Windows Intel IGD graphics dirvers are unable to access the OpRegion and report Code 43 errors with the result being that the guest screen never lights up. So after the device model exposes the OpRegion to the guest using its ioreq server, make a copy of it and use a copy of the OpRegion and VBT which is backed by RAM allocated to the guest instead of mapped via the ioreq server. This fixes the Code 43 errors reported by the Windows IGD graphics drivers.

---
The companion patch for the Qemu device model (DM) is available here:

https://lore.kernel.org/qemu-devel/[email protected]/

That patch is part of a larger patch series available here:

https://lore.kernel.org/qemu-devel/[email protected]/

Up-to-date specifications for the Intel IGD are not available to the public but an older version is available from Intel here:

https://www.intel.com/content/www/us/en/docs/graphics-for-linux/developer-reference/1-0/opregion-specification.html

This patch derives the specifications for the OpRegion that are needed to add support for an extended VBT from the patches to the Linux kernel vfio driver. This mainly consists of the RVDA and RVDS fieds of the OpRegion which store the address and size of the extended VBT, respectively. See the links in the commit message which provide links to the vfio patches that added support for this feature to KVM/VFIO guests for more details.

There is an undocumented setting that works in the xl.cfg(5) domain configuration file, firmware_override, that makes it possible to use a patched version of hvmloader alongside an installation of unpatched upstream Xen or a version of Xen packaged by a distro. So one can download the source for one's installed version of Xen, apply this patch and build just hvmloader and then install the patched version of hvmloader with a different filename, such as hvmloader-igd-testing, into the same directory where hvmloader is installed (usually something like /usr/libexec/xen/boot) and then one can configure a guest to use the patched version of hvmloader with one's installed version of Xen by adding a line like this to the domain xl.cfg file:

firmware_override = 'hvmloader-igd-testing'

The compatible patch for the DM is part of a larger patchset that fixes many of the problems that currently affect the feature of Intel IGD passthrough to Xen HVM guests. This patch should be considered as a companion patch to that patchset for the DM. Do not try to test this patch with a real Intel IGD device without also applying the patchset for the DM because without those patches, the guest will most likely fail to start if an Intel IGD is passed through to the guest.

Changes in v4:
- Now this patch is the first patch of a 2-patch series
- Correct a spelling mistake in the commit message: beneift -> benefit
- Added a link to the cover letter of the latest version of the companion Qemu device model patch series.

Changes in v3:
- The patch has been substantially re-worked. Most of the implementation of support for Intel IGD in v2 that could be implemented in the DM instead of in hvmloader has been moved to the DM. Specifically, the responsibility to read the OpRegion and patch it if necessary is done in the DM instead of in hvmloader. This change is in response to the comments that were made on v2 of this patch.

- In contrast to both the current implementation and the implementation in v2, the host OpRegion is never directly exposed to the guest. Instead, the DM exposes an emulated copy of the OpRegion to the guest, patched appropriately for the guest, using the DM's ioreq server.

- Hvmloader's main responsibility is to allocate enough pages in the E820 map to accomodate both the OpRegion and the extended VBT. In v3, hvmloader relies on the DM to communicate the number of pages that are needed for the OpRegion + VBT, and this change means all the code in v2 related to discovering the size of the extended VBT has been removed from hvmloader in v3 and moved to the DM.

- Backward compatibility with versions of the DM that do not support an extended VBT has been simplified. There is no need for a bitmask setting to indicate support for extended VBT and OpRegion 2 and higher. Instead, the DM learns that hvmloader has support by detecting a read of the OpRegion register before a write to it, and hvmloader learns that the DM has support if the DM returns a non-zero value, the number of pages needed for the OpRegion + VBT, in response to the first read by hvmloader.

- It was necessary to retain the code that populates the pages allocated for the OpRegion and VBT with guest RAM and copying the OpRegion and VBT to that guest RAM because testing revealed that when the OpRegion and VBT are exposed to the guest by the DM's ioreq server, Windows graphics drivers are unable to access the OpRegion and VBT.

- To remove the confusion with the value of IGD_OPREGION_PAGES that is currently set to 3 to account for the fact that the OpRegion is not always aligned on a page boundary, it has been changed in v3 to 2, the actual number of pages needed for the OpRegion (not including an extended VBT).

- Added a check on the number of pages needed for the OpRegion + VBT to ensure the region does not take up an unreasonably large percentage of the reserved dynamic memory range.

I also provide the following table that hopefully helps illustrate how v3 of this patch differs from v2:

Resource/Description Proposed in v2 Proposed in v3
------------------------------------------------------------------------------
OpRegion register Emulated in DM Emulated in DM
------------------------------------------------------------------------------

OpRegion                      Emulated (hvmloader       Emulated in DM (guest
                              makes a copy from direct  accesses emulated copy
                              mapped host OpRegion      provided by DM and
                              and from then on guest    from then on guest
                              accesses its own copy     accesses its own copy
                              stored in guest memory)   stored in guest memory)
------------------------------------------------------------------------------
Extended VBT                  Emulated (hvmloader       Emulated in DM (guest
                              makes a copy from direct  accesses emulated copy
                              mapped host VBT and       provided by DM and from
                              from then on guest        then on guest accesses
                              accesses its own copy     its own copy stored in
                              stored in guest memory)   guest memory)
------------------------------------------------------------------------------
E820 pages allocated          Depends on extended VBT   Depends on extended VBT
                              size if there is an       size if there is an
                              extended VBT              extended VBT
------------------------------------------------------------------------------
If OpRegion needs patching    hvmloader patches it      DM patches it
------------------------------------------------------------------------------
Setting the OpRegion          Done by DM after complex  Done by DM with hint
register                      communication protocol    from hvmloader which
                              with hvmloader completes  provides DM with page

base address of OpRegion in guest
------------------------------------------------------------------------------

Changes in v2:
- Correct the name of the new function in the commit message

    opregion_setup() -> intel_opregion_setup()

- Add a link to the companion patchset for the device model

- Describe how to use the firmware_override setting in xl.cfg(5) to simplify testing of this patch.

- Correct a logical flaw that in case the size of the extended VBT is <= 2 pages, an extra, unnecessary page would be allocated in the memory hole. This correction is in the intel_opregion.c file.

This code:

/* Update the number of pages we need for the E820 map */ igd_opregion_e820_pages = pages_needed;

/*
* So far we have allocated vbt_pages_needed
* and we will likely need to allocate more
* pages to fully contain OpRegion + VBT.
*/
if ( pages_needed > vbt_pages_needed )
igd_opregion_pgbase = mem_hole_alloc
(pages_needed - vbt_pages_needed);

Is replaced with this code:

/*
* So far we have allocated igd_opregion_e820_pages
* and we will likely need to allocate more
* pages to fully contain OpRegion + VBT.
*/
if ( pages_needed > igd_opregion_e820_pages )
igd_opregion_pgbase = mem_hole_alloc
(pages_needed - igd_opregion_e820_pages);

/* Update the number of pages we need for the E820 map */ igd_opregion_e820_pages = pages_needed;

 tools/firmware/hvmloader/config.h |  6 +-
 tools/firmware/hvmloader/e820.c   |  4 +-
 tools/firmware/hvmloader/pci.c    | 96 ++++++++++++++++++++++++++++++-
 3 files changed, 100 insertions(+), 6 deletions(-)
tools/firmware/hvmloader/config.h +5 -1
index c159db3..edc3a8d 100644
@@ -8,7 +8,8 @@ enum virtual_vga { VGA_none, VGA_std, VGA_cirrus, VGA_pt };
 extern enum virtual_vga virtual_vga;
 
 extern unsigned long igd_opregion_pgbase;
-#define IGD_OPREGION_PAGES 3
+extern unsigned int igd_opregion_e820_pages;
+#define IGD_OPREGION_PAGES 2
 
 struct bios_config {
     const char *name;
@@ -75,6 +76,9 @@ extern bool acpi_enabled;
 #define ACPI_MEMORY_DYNAMIC_START     0xFC001000
 #define RESERVED_MEMORY_DYNAMIC_START 0xFC100000
 #define RESERVED_MEMORY_DYNAMIC_END   0xFE000000
+#define RESERVED_MEMORY_DYNAMIC_PAGES (RESERVED_MEMORY_DYNAMIC_END - \
+                                       RESERVED_MEMORY_DYNAMIC_START) >> \
+                                       PAGE_SHIFT
 /*
  * GUEST_RESERVED: Physical address space reserved for guest use.
  * This is not dynamically advertised to guests, so this range must *never*
tools/firmware/hvmloader/e820.c +2 -2
index 86d3954..97a234e 100644
@@ -243,11 +243,11 @@ int build_e820_table(struct e820entry *e820,
         nr++;
 
         e820[nr].addr = igd_opregion_base;
-        e820[nr].size = IGD_OPREGION_PAGES * PAGE_SIZE;
+        e820[nr].size = igd_opregion_e820_pages * PAGE_SIZE;
         e820[nr].type = E820_NVS;
         nr++;
 
-        e820[nr].addr = igd_opregion_base + IGD_OPREGION_PAGES * PAGE_SIZE;
+        e820[nr].addr = igd_opregion_base + igd_opregion_e820_pages * PAGE_SIZE;
         e820[nr].size = (uint32_t)-e820[nr].addr;
         e820[nr].type = E820_RESERVED;
         nr++;
tools/firmware/hvmloader/pci.c +93 -3

117 lines not on this page · raw message

Signature · 1 line
2.52.0
#3 lore raw
Quoted 66 lines · Chuck Zmudzinski

Modern Intel IGD devices do not work well with the current implementation of support for the Intel IGD in hvmloader because it lacks support for an extended video bios table (VBT).

Code 43 errors in Windows guests and failure of the guest screen to light up are some of the problems that occur with the current implementation.

To address this problem, this patch implements support for Intel IGD devices with an extended VBT and OpRegion version 2+ which is required for most modern Intel IGD devices, as noted in the Linux kernel vfio commits referenced in the Link tags below. This patch ports support for devices with an extended VBT and OpRegion 2+ which was added in those commits for KVM/vfio, but adapted for Xen HVM guests with PCI passthrough.

This patch depends on compatible support in the device model. If hvmloader detects the device model lacks such support, it will fall back to the currently implemented protocol for configuring the OpRegion to provide backward compatibiltiy for systems that lack a device model with support for an extended VBT.

The primary reason the OpRegion needs to be patched in some cases is that with the addition of the RVDA and RVDS fields to the OpRegion, the OpRegion is not position-independent and may need to be patched if it is moved to a different address in the guest. This means the current protocol of having the device model directly map the unmodified host OpRegion to the guest is not compatible with the requiremnts of the newer devices that in some cases require that the OpRegion be modified for it to be compatible with the guest address space.

In this implementation, the device model has the responsibility to read the host OpRegion and patch it as needed before exposing it to the guest. Since an extended VBT means more pages are needed for the OpRegion, depending on the size of the extended VBT, hvmloader has the responsibility to edit the E820 map to accomodate the additional pages needed to contain the OpRegion + VBT.

To implement this in hvmloader, use a variable, igd_opregion_e820_pages, instead of the constant, IGD_OPREGION_PAGES, to represent the number of pages to reserve in the E820 map for the OpRegion + VBT. Also, to remove the confusion introduced by setting IGD_OPREGION_PAGES to 3 in an earlier patch to account for the fact that the OpRegion is not guaranteed to be aligned on a page boundary, reset IGD_OPREGION_PAGES to 2 so it matches the actual size of the OpRegion.

Instead of only writing to the PCI_INTEL_OPREGION register, first read from it to provide a way for both hvmloader and the device model to discover if both components have support for an extended VBT and more than 3 pages reserved for the OpRegion + VBT. The device model detects the read of the register before the write to learn that hvmloader has support, and hvmoader detects that the device model returns the number of pages to reserve for the OpRegion + VBT instead of 0 when it first reads the register to learn that that the device model has support. When this new protocol is supported by both hvmloader and the device model, the device model will not expose the host OpRegion directly to the guest via a direct mapping as the old protocol does but instead exposes an emulated copy of the OpRegion and VBT using its ioreq server. This has the added benefit of preventing extraneous host memory in the regions before or after a non-page-aligned OpRegion that should be confidential to the host from being exposed to the guest.

Testing reveals that when the device model exposes the OpRegion to the guest by mapping it to the device model's ioreq server, the Windows Intel IGD graphics dirvers are unable to access the OpRegion and report Code 43 errors with the result being that the guest screen never lights up. So after the device model exposes the OpRegion to the guest using its ioreq server, make a copy of it and use a copy of the OpRegion and VBT which is backed by RAM allocated to the guest instead of mapped via the ioreq server. This fixes the Code 43 errors reported by the Windows IGD graphics drivers.

Quoted 3 lines

Despite the non-negligible number of comments below, I think this is in a much better shape now.

tools/firmware/hvmloader/config.h enum virtual_vga { VGA_none, VGA_std, VGA_cirrus, VGA_pt }; · line 12

Quoted 9 lines
tools/firmware/hvmloader/config.h +2 -1
@@ -8,7 +8,8 @@ enum virtual_vga { VGA_none, VGA_std, VGA_cirrus, VGA_pt };
 extern enum virtual_vga virtual_vga;
 
 extern unsigned long igd_opregion_pgbase;
-#define IGD_OPREGION_PAGES 3
+extern unsigned int igd_opregion_e820_pages;
+#define IGD_OPREGION_PAGES 2

I wonder if this is a good way of expressing things. Specifying a page count here kind of implies page alignment. Imo specifying a size in bytes here would be better, as that then makes more natural that an extra page needs adding in the legacy case (to account for the area not starting at a page boundary). See also a comment further down, likely leading to elimination of most of the users of this constant.

Quoted 11 lines
tools/firmware/hvmloader/pci.c +1 -0
@@ -44,6 +44,7 @@ uint64_t pci_hi_mem_start = 0, pci_hi_mem_end = 0;
 
 enum virtual_vga virtual_vga = VGA_none;
 unsigned long igd_opregion_pgbase = 0;
+unsigned int igd_opregion_e820_pages = 0;
 
 /* Check if the specified range conflicts with any reserved device memory. */
 static bool check_overlap_all(uint64_t start, uint64_t size)

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 99

Quoted 6 lines
tools/firmware/hvmloader/pci.c +3 -0
@@ -93,6 +94,9 @@ void pci_setup(void)
     uint16_t class, vendor_id, device_id;
     unsigned int bar, pin, link, isa_irq;
     uint8_t pci_devfn_decode_type[256] = {};
+    uint32_t igd_opregion;
+    void *opregion_vbt_scratch;
+    bool opregion_is_direct_mapped;

Please declare these in the narrowest possible scope, i.e. ...

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 196

Quoted 2 lines
tools/firmware/hvmloader/pci.c +0 -0
@@ -192,12 +196,98 @@ void pci_setup(void)
                 {

... apparently here.

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 197

Quoted 1 line
tools/firmware/hvmloader/pci.c +0 -0
                     igd_opregion_pgbase = mem_hole_alloc(IGD_OPREGION_PAGES);

Why do you leave this untouched? By splitting the mem_hole_alloc() calls, you rely on implementation details of that function without there actually being a need.

Quoted 5 lines
tools/firmware/hvmloader/pci.c +1 -3
                     /*
-                     * Write the the OpRegion offset to give the opregion
-                     * address to the device model. The device model will trap 
-                     * and map the OpRegion at the give address.
+                     * To be compatible with this interface for programming the

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 209

Quoted 10 lines
tools/firmware/hvmloader/pci.c +10 -0
+                     * the PCI_INTEL_OPREGION register, the device model must
+                     * check if the guest reads the register before it writes
+                     * to the register. To indicate to the device model that
+                     * we have support for an extended VBT, we read the
+                     * PCI_INTEL_OPREGION register before writing to it. If the
+                     * device model supports an extended VBT, it will return
+                     * the number of pages needed for the OpRegion + VBT. If
+                     * not, it will return 0 which indicates that it does not
+                     * implement this interface for supporting an extended VBT.
+                     */

This approach will need buyoff by qemu folks before it can be acked here. (Buyoff doesn't necessarily mean the change must have gone in there first, yet of course it would be best if things went in that order.)

Quoted 25 lines
tools/firmware/hvmloader/pci.c +25 -0
+                    igd_opregion_e820_pages = pci_readl(vga_devfn,
+                                                        PCI_INTEL_OPREGION);
+                    if ( !igd_opregion_e820_pages )
+                    {
+                        /*
+                         * This case provides backward compatibility with
+                         * device model versions that lack support for an
+                         * extended VBT. In this case the device model
+                         * expects us to allocate an extra page in case the
+                         * OpRegion is not aligned on a page boundary. Also,
+                         * in this case, the host OpRegion is direct mapped
+                         * into the guest.
+                         */
+                        igd_opregion_pgbase = mem_hole_alloc(1);
+                        igd_opregion_e820_pages = IGD_OPREGION_PAGES + 1;
+                        opregion_is_direct_mapped = true;
+                    }
+                    else
+                    {
+                        /* Allocate extra pages for an extended VBT */
+                        if ( igd_opregion_e820_pages > IGD_OPREGION_PAGES )
+                        {
+                            igd_opregion_pgbase =
+                                mem_hole_alloc(igd_opregion_e820_pages -
+                                               IGD_OPREGION_PAGES);

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 244

Quoted 10 lines
tools/firmware/hvmloader/pci.c +10 -0
+                        }
+                        opregion_is_direct_mapped = false;
+                    }
+                    /*
+                     * This ensures the OpRegion + VBT does not take up more
+                     * than 1/32 of the reserved region. Also, we must reject
+                     * a value of 1 for igd_opregion_e820_pages.
+                     */
+                    if ( igd_opregion_e820_pages >
+                        RESERVED_MEMORY_DYNAMIC_PAGES >> 5 ||

Parentheses please around the shift expression.

The 1/32th also looks to be entirely arbitrary. If so, the comment also wants to state this fact.

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 250

Quoted 6 lines
tools/firmware/hvmloader/pci.c +6 -0
+                        igd_opregion_e820_pages == 1 )
+                    {
+                        printf("too many or too few pages (%u) for OpRegion\n",
+                               igd_opregion_e820_pages);
+                        BUG();
+                    }

Why does this entire if() not live in the "else" branch of the earlier if()? It's applicable only there afaict.

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 252

Quoted 2 lines
tools/firmware/hvmloader/pci.c +2 -0
+                    /*
+                     * Write the the OpRegion offset to give the OpRegion

Nit: As you move the comment, please also drop the stray extra "the".

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 258

Quoted 6 lines
tools/firmware/hvmloader/pci.c +5 -0
+                     * address to the device model. The device model will trap
+                     * and make the OpRegion accessible at the given address.
+                     * The device model is also expected to verify that the
+                     * OpRegion is compatible with the guest address space and
+                     * patch it if necessary to make it compatible.
                      */

The last sentence of the comment is new, and I fear I don't understand what it is trying to describe. How can one "patch a region" to "make it compatible"?

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 271

Quoted 13 lines
tools/firmware/hvmloader/pci.c +11 -0
                     pci_writel(vga_devfn, PCI_INTEL_OPREGION,
                                igd_opregion_pgbase << PAGE_SHIFT);
+
+                    /* Don't use our own copy if OpRegion is direct mapped */
+                    if ( opregion_is_direct_mapped )
+                        break;
+
+                    /*
+                     * Windows IGD drivers do not work properly when the
+                     * OpRegion is exposed by the device model's ioreq server,
+                     * so make a copy of the OpRegion and use that copy which
+                     * will be backed by RAM allocated to the guest.
+                     */

I'm struggling with this comment: What does "do not work properly" mean here? How the region is backed should be of no interest to the driver?

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 279

Quoted 8 lines
tools/firmware/hvmloader/pci.c +8 -0
+                    opregion_vbt_scratch =
+                        scratch_alloc(igd_opregion_e820_pages <<
+                                      PAGE_SHIFT, 0);
+                    memcpy(opregion_vbt_scratch,
+                           (void *)(igd_opregion_pgbase << PAGE_SHIFT),
+                           igd_opregion_e820_pages << PAGE_SHIFT);
+
+                    igd_opregion = pci_readl(vga_devfn, PCI_INTEL_OPREGION);

Aren't you reading back here what you wrote above? Why the read then?

tools/firmware/hvmloader/pci.c void pci_setup(void) · line 284

Quoted 5 lines
tools/firmware/hvmloader/pci.c +5 -0
+                    /*
+                     * The device model will unmap the OpRegion from the ioreq
+                     * server so we can use our own copy of the OpRegion.
+                     */
+                    pci_writel(vga_devfn, PCI_INTEL_OPREGION, igd_opregion);

Despite the comment this looks like black magic: You write back the value you read. That's hardly expected behavior, and hardly how real hardware would behave. I think the comment will need extending, and I think this is another behavioral aspect which needs buyoff by qemu folks.

One further request: As you're already tidying things, would you mind also correcting the comment on PCI_INTEL_OPREGION's definition (which I think means "bytes" when it says "bits")?

Jan

#4 lore raw

PATCH v4 2/2 libxl: add OpRegion and VBT to firmware when assigning IGD

In reply to Chuck Zmudzinski

To provide support for newer IGD devices with an extended video bios table (VBT), the device model needs to access the host OpRegion and VBT when the IGD is bound to the xen-pciback driver but the Linux kernel only exposes them in the debugfs when the IGD is bound to the i915 driver. Although the OpRegion and VBT can be manually copied to a firmware directory from the debugfs to a directory where the device model can access them when the IGD is bound to the xen-pciback driver, libxl can do this automatically by copying the OpRegion and VBT from the Linux debugfs to the Xen firmware directory before unbinding the IGD from the i915 driver. Since the copying of the OpRegion and VBT are not required for the device to be made assignable, don't return an error if the only error is that the attempt to copy the OpRegion and/or the VBT to the Xen firmware directory failed.

It is necessary to read the RVDS field of the OpRegion which stores the size of the extended VBT. We cannot rely on stat(2) for this which returns a value of 0 for the size of files stored in the Linux debugfs. This patch relies on lstat(2) only to determine if the OpRegion and VBT files already exist in the Xen firmware directory. If the value stored in the RVDS field is 0, then there is no extended VBT and the VBT is embedded within mailbox # 4 of the OpRegion, in which case we can assume the size of the VBT is 6 KiB.

For users of the Qemu device model, if the Xen firmware directory is not configured as one of the Qemu firmware directories, the "intel-opregion" and "intel-vbt" files must be moved or copied to a Qemu firmware directory to provide proper support for an IGD with an extended VBT that is passed through to a Xen HVM guest.

---
This patch also depends on the companion patch for the Qemu device model available here:

https://lore.kernel.org/qemu-devel/[email protected]/

There is not an up-to-date specification for the OpRegion and VBT available to the public online but an older version is available here:

https://www.intel.com/content/www/us/en/docs/graphics-for-linux/developer-reference/1-0/opregion-specification.html

Despite the fact that we do not have an up-to-date specification, patches to the Linux kernel i915 driver and the igd-related files of the Linux kernel vfio driver provide enough information about the specification to provide support for the Intel IGD in Xen HVM guests. These patches to the Linux kernel vfio driver were particularly helpful for this series of patches to fix support for IGD passthrough for Xen HVM guests:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/vfio/pci/vfio_pci_igd.c?id=bab2c1990b78
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/vfio/pci/vfio_pci_igd.c?id=49ba1a2976c8

Many more details about the support for Intel IGD passthrough to Xen guests is available in the other patch in this series and the 7 patches of the companion patch series for the device model. Please refer to those patches before asking questions that might be answered by reading through those other patches.

Changes in v4:
- This is the first version of this series of IGD fixes with this patch

 tools/libs/light/libxl_pci.c | 82 ++++++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)
tools/libs/light/libxl_pci.c +82 -0
index 49d272d..ff28387 100644
@@ -25,6 +25,9 @@
 #define PCI_OPTIONS            "msitranslate=%d,power_mgmt=%d"
 #define PCI_BDF_XSPATH         "%04x-%02x-%02x-%01x"
 #define PCI_PT_QDEV_ID         "pci-pt-%02x_%02x.%01x"
+#define PCI_OPREGION_SIZE      0x2000
+#define PCI_OPREGION_RVDS      0x3c2 /* offset of RVDS in OpRegion */
+#define PCI_VBT_MBOX4_SIZE     0x1800
 
 /* PCI Interrupt Line is an 8-bit value, 0xff means disconnected. */
 #define PCI_IRQ_LINE_LIMIT     0xff
@@ -765,6 +768,17 @@ static int libxl__device_pci_assignable_add(libxl__gc *gc,
     const char *name;
     int rc;
     struct stat st;
+    uint32_t rvds = 0; /* Intel VBT size */
+    int fd = -1;
+    char *spath1 = NULL;
+    char *dpath1 = NULL;
+    char *spath2 = NULL;
+    char *dpath2 = NULL;
+    uint8_t *buf1 = NULL;
+    uint8_t *buf2 = NULL;
+    uint16_t pt_vendor = 0xffff;
+    uint16_t pt_device = 0xffff;
+    unsigned long class = 0;
 
     /* Local copy for convenience */
     dom = pci->domain;
@@ -797,6 +811,74 @@ static int libxl__device_pci_assignable_add(libxl__gc *gc,
         return ERROR_FAIL;
     }
 
+    pt_vendor = sysfs_dev_get_vendor(gc, pci);
+    pt_device = sysfs_dev_get_device(gc, pci);
+
+    /* Check if the device is an Intel IGD */
+    if ( pt_vendor != 0x8086 || pt_device == 0xffff ||
+        sysfs_dev_get_class(gc, pci, &class) ||
+        (class != 0x030000 && class != 0x038000) )
+        goto skipigd;
+
+    /* These filenames match the filenames used in the device model */
+    dpath1 = libxl__abs_path(gc, "intel-opregion",
+                             libxl__xenfirmwaredir_path());
+    dpath2 = libxl__abs_path(gc, "intel-vbt",
+                             libxl__xenfirmwaredir_path());
+
+    /* Check if the OpRegion and VBT files are already present */
+    if ( !lstat(dpath1, &st) && !lstat(dpath2, &st) )
+        goto skipigd;
+
+    spath1 = GCSPRINTF("/sys/kernel/debug/dri/"PCI_BDF"/i915_opregion",
+                       dom, bus, dev, func);
+    spath2 = GCSPRINTF("/sys/kernel/debug/dri/"PCI_BDF"/i915_vbt",
+                       dom, bus, dev, func);
+
+    /*
+     * Try to copy the OpRegion and VBT while IGD is bound to i915 driver
+     * but don't return an error if one or both of the copies fail.
+     */
+    if ( !lstat(spath1, &st) && !lstat(spath2, &st) ) {
+        fd = open(spath1, O_RDONLY);
+        GCNEW_ARRAY(buf1, PCI_OPREGION_SIZE);
+        rc = libxl_read_exactly(ctx, fd, (void *)buf1, PCI_OPREGION_SIZE,
+                                spath1, NULL);
+        close(fd);
+        if ( !rc ) {
+            fd = open(dpath1, O_CREAT|O_WRONLY|O_TRUNC, 0644);
+            rc = libxl_write_exactly(ctx, fd, (void *)buf1,
+                                     PCI_OPREGION_SIZE, dpath1, NULL);
+            close(fd);
+            if ( !rc ) {
+                rvds = *(uint32_t *)(buf1 + PCI_OPREGION_RVDS);
+                if ( !rvds ) /* VBT is embedded in the OpRegion */
+                    rvds = PCI_VBT_MBOX4_SIZE;
+                fd = open(spath2, O_RDONLY);
+                GCNEW_ARRAY(buf2, rvds);
+                rc = libxl_read_exactly(ctx, fd, (void *)buf2, rvds,
+                                        spath2, NULL);
+                close(fd);
+                if ( !rc ) {
+                    fd = open(dpath2, O_CREAT|O_WRONLY|O_TRUNC, 0644);
+                    rc = libxl_write_exactly(ctx, fd, (void *)buf2,
+                                             rvds, dpath2, NULL);
+                    close(fd);
+                    if ( rc ) {
+                        LOG(INFO, "Failed to copy VBT");
+                    }
+                } else {
+                    LOG(INFO, "Failed to read VBT from debugfs");
+                }
+            } else {
+                LOG(INFO, "Failed to copy OpRegion");
+            }
+        } else {
+            LOG(INFO, "Failed to read OpRegion from debugfs");
+        }
+    }
+
+skipigd:
     /* Check to see if it's already assigned to pciback */
     rc = pciback_dev_is_assigned(gc, pci);
     if ( rc < 0 ) {
Signature · 1 line
2.52.0