xen/igd: get PCH info from host sysfs
A message that is not in the archive (sent off-list or not yet received): [email protected]
v5 cover xen/igd: fixes for Intel IGD passthrough
Please note that Patch 5 of this series also requires a patch to hvmloader of Xen for the new support to take effect that is available here:
https://lore.kernel.org/qemu-devel/[email protected]/
This patch series aims to fix long-standing bugs that need to be backported to all currently supported stable versions in order to support passthrough of both older and modern Intel IGD devices to Xen HVM guests. Note that previous versions of this patch series only fixed the bugs affecting older devices and the patches needed for newer devices were not included, so this is the first version of this patch series that is suitable for use and testing with newer devices that, for example, are designed by the manufacturer to only work with UEFI firmware.
These patches fix several bugs that cause problems ranging from a dark screen in the guest until the guest OS graphics are loaded to an assert failure or other failures in newer devices that in many cases prevent guest creation from succeeding or cause other problems such as code 43 errors in Windows guests.
To test the the third patch and verify it fixes the bug of no video output from Seabios, it is necessary to test with older Intel IGD devices that have support for legacy VGA bios (Seabios) because the newer devices, as far as I can tell, are not compatible with legacy VGA BIOS.
The first three patches have been tested using Xen 4.21 and Seabios 1.17 on Fedora 44 using an older Intel NUC7i5BNK with an i5-7260U processor and have been verified to fix the bugs described in the individual patches. This device is compatible with legacy VGA BIOS and Seabios.
With the fourth through sixth patches applied, it is possible to use/test with newer devices since, with those patches applied, the bugs that prevent guest creation from succeeding with many newer devices are fixed. All six patches have been tested on both the older Intel NUC7i5BNK mentioned above and a newer 14th Gen i5-14500 Raptor Lake processor in an ASRock board that, as far as I can tell, is not compatible with legacy VGA BIOS and Seabios, which means that, although one can successfully create a guest with Seabios for the guest with the newer device, there will be no graphics output from the guest until the guest OS loads the graphics drivers when using the newer device with Seabios.
The sixth patch is provided for use with newer devices that require an EFI graphics output protocol (GOP) driver for graphics output during early boot. For more details, read the commit message and the accompanying notes in Patch 6.
Two hints to help configuring Xen HVM guests with Intel IGD passthrough:
- Increase the value of mmio_hole from its default value of 256 to at least 512 using the mmio_hole setting in the guest config. The value of 512 works well when the stolen memory is 256 MiB. If more stolen memory is used, the mmio_hole may need to be increased even more.
- If there are rdm conflicts, lower the rdm_mem_boundary setting in the guest config from its default value of 2048 to a value at which all rdm conflicts are resolved. I have discovered that lowering it in 128 MiB increments works well. With the older device I needed to lower it to 1792 and with the newer one I needed to lower it to 1664. See the description of the rdm_mem_boundary setting in the xl.cfg(5) man page for more details.
Changes in v2:
- close open files before setting errp
- improvements to readability and style
- small corrections to the commit messages
- add stable to Cc list
Changes in v3:
- whitespace fix in first patch
- fix Cc address for qemu-stable
Changes in v4:
- add three patches to provide support for newer Intel IGD devices
- edit the commit messages for better consistency of style
- use 12 digits instead of 7 when referencing commit hashes
- re-write xen_pt_get_host_pch_info() in patch 1 using the functions declared in xen-host-pci-device.h
- handle errors in patch 1 that in previous versions of that patch were not handled
- add a Fixes tag to the third patch
- add two hints in the cover letter to help those who are trying to configure Xen HVM guests with an Intel IGD passed through
Changes in v5:
- fix style problems reported by checkpatch (see each patch for details)
- update the link to the companion patch for Xen hvmloader
Chuck Zmudzinski (6):
xen/igd: get PCH info from host sysfs xen/igd: don't register rom bar twice xen/igd: fixup device id before registering rom xen/igd: enable guest creation when ROM read fails xen/igd: implement support for extended VBT xen/igd: use custom option ROM if provided
hw/xen/xen_pt.c | 13 +- hw/xen/xen_pt_graphics.c | 282 +++++++++++++++++++++++++++++++++++++-- hw/xen/xen_pt_load_rom.c | 64 ++++++--- include/hw/xen/xen_igd.h | 3 +- 4 files changed, 327 insertions(+), 35 deletions(-)
Signature · 1 line
2.52.0
PATCH v5 1/6 xen/igd: get PCH info from host sysfs
The igd_combo_id_infos[] data is out of date with many devices missing from igd_combo_id_infos[]. For newer devices not in igd_combo_id_infos[], get the infos from the host sysfs. If logging is configured, print log messages displaying the PCH info used for the guest.
Introduce helper function xen_pt_get_host_pch_info() to facilitate getting the necessary information from sysfs. Treat failure to get the host PCH device id as an unrecoverable error that causes guest creation to fail. If access to the host PCH device revision id fails, print a warning message and use a default value of 0x1 in that case.
Also, use errp in xen_igd_passthrough_isa_bridge_create() to set errors from xen_pt_get_host_pch_info() and cleanup on error path with xen_host_pci_device_put(&s->real_device) and object_unparent(OBJECT(&d->rom)) for errors when creating creating the IGD PCH bridge.
Add cleanup with object_unparent(OBJECT(&d->rom)) for errors when setting up VGA BIOS for GFX passthrough.
---
Changes in v4:
- re-wrote xen_pt_get_host_pch_info() using functions from xen-host-pci-device.h
- add more error handling to clean up better after if errors occur
- don't consider failure to get the PCH device revision id a fatal error but instead print a warning message and use a default value of 0x1
Changes in v5:
- Shorten warn_report message to resolve checkpatch line length warning
hw/xen/xen_pt.c | 10 +++++++++- hw/xen/xen_pt_graphics.c | 39 +++++++++++++++++++++++++++++++++++++-- include/hw/xen/xen_igd.h | 3 ++- 3 files changed, 48 insertions(+), 4 deletions(-)
hw/xen/xen_pt.c +9 -1
@@ -862,12 +862,20 @@ static void xen_pt_realize(PCIDevice *d, Error **errp)if (*errp) {error_append_hint (errp,"Setup VGA BIOS of passthrough" " GFX failed" ); +object_unparent (OBJECT (&d->rom));xen_host_pci_device_put (&s->real_device);return ; }/* Register ISA bridge for passthrough GFX. */ -xen_igd_passthrough_isa_bridge_create (s, &s->real_device); +xen_igd_passthrough_isa_bridge_create (s, &s->real_device, errp); +if (*errp) { +error_append_hint (errp,"Failed to create PCH bridge" +" for passthrough GFX" ); +object_unparent (OBJECT (&d->rom)); +xen_host_pci_device_put (&s->real_device); +return ; + } }/* Handle real device's MMIO/PIO BARs */
hw/xen/xen_pt_graphics.c +37 -2
@@ -2,6 +2,7 @@* graphics passthrough */ #include "qemu/osdep.h" +#include "qemu/error-report.h" #include "qapi/error.h" #include "hw/xen/xen_pt.h" #include "hw/xen/xen_igd.h"
@@ -376,8 +377,33 @@ static void pt_graphics_register_types(void) }type_init (pt_graphics_register_types) +static void xen_pt_get_host_pch_info(uint16_t *pch_dev_id, uint8_t *pch_rev_id, +Error **errp) +{ + g_autofree XenHostPCIDevice *pch_dev = g_new(XenHostPCIDevice, 1); + +xen_host_pci_device_get (pch_dev,0 ,0 ,0x1f ,0 , errp); +if (*errp) { + goto error; + } + + *pch_dev_id = pch_dev->device_id; + +if (xen_host_pci_get_byte (pch_dev,PCI_REVISION_ID , pch_rev_id)) { + *pch_rev_id =0x1 ; +warn_report ("IGD: failed to get host PCH revision, setting it to 0x1" ); + } + +xen_host_pci_device_put (pch_dev); +return ; + +error: +error_append_hint (errp,"failed to get host PCH device for Intel IGD" ); +} +void xen_igd_passthrough_isa_bridge_create (XenPCIPassthroughState *s, -XenHostPCIDevice *dev) +XenHostPCIDevice *dev, +Error **errp) {PCIBus *bus =pci_get_bus (&s->dev);struct PCIDevice *bridge_dev;
@@ -394,7 +420,16 @@ void xen_igd_passthrough_isa_bridge_create(XenPCIPassthroughState *s, } } +/* Newer devices get PCH infos from host sysfs */ +if ((pch_dev_id ==0xffff ) || !pch_rev_id) { +xen_pt_get_host_pch_info (&pch_dev_id, &pch_rev_id, errp); + } + +XEN_PT_LOG (&s->dev,"PCH device id: 0x%x\n" , pch_dev_id); +XEN_PT_LOG (&s->dev,"PCH revision: 0x%x\n" , pch_rev_id); +if (pch_dev_id ==0xffff ) { +error_setg (errp,"failed to get PCH device id" );return ; }
@@ -406,7 +441,7 @@ void xen_igd_passthrough_isa_bridge_create(XenPCIPassthroughState *s,* Note that vendor id is always PCI_VENDOR_ID_INTEL. */ if (!bridge_dev) { -fprintf (stderr,"set igd-passthrough-isa-bridge failed!\n" ); +error_setg (errp,"set igd-passthrough-isa-bridge failed!" );return ; }pci_config_set_device_id (bridge_dev->config, pch_dev_id);
include/hw/xen/xen_igd.h +2 -1
@@ -22,7 +22,8 @@ uint32_t igd_read_opregion(XenPCIPassthroughState *s);void xen_igd_reserve_slot (PCIBus *pci_bus);void igd_write_opregion (XenPCIPassthroughState *s,uint32_t val);void xen_igd_passthrough_isa_bridge_create (XenPCIPassthroughState *s, -XenHostPCIDevice *dev); +XenHostPCIDevice *dev, +Error **errp);static inline bool is_igd_vga_passthrough (XenHostPCIDevice *dev) {
Signature · 1 line
2.52.0
PATCH v5 2/6 xen/igd: don't register rom bar twice
In reply to Chuck ZmudzinskiThis also fixes a failed assertion in pci [1] for Qemu version 10 and higher when passing through an Intel IGD with an option ROM to the guest.
[1] f6fc01c78666 ("hw/pci: Assert a bar is not registered multiple times")
---
Changes in v4:
- Use 12 digits for commit hashes
Changes in v5:
- No changes to this patch
hw/xen/xen_pt.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/xen/xen_pt.c +2 -1
@@ -459,6 +459,7 @@ static int xen_pt_register_regions(XenPCIPassthroughState *s, uint16_t *cmd) {int i =0 ;XenHostPCIDevice *d = &s->real_device; +const pcibus_t romsize = s->dev.io_regions[PCI_ROM_SLOT ].size;/* Register PIO/MMIO BARs */ for (i =0 ; i <PCI_ROM_SLOT ; i++) {
@@ -495,7 +496,7 @@ static int xen_pt_register_regions(XenPCIPassthroughState *s, uint16_t *cmd) }/* Register expansion ROM address */ -if (d->rom.base_addr && d->rom.size) { +if (!romsize && d->rom.base_addr && d->rom.size) {uint32_t bar_data =0 ;/* Re-set BAR reported by OS, otherwise ROM can't be read. */
Signature · 1 line
2.52.0
PATCH v5 3/6 xen/igd: fixup device id before registering rom
In reply to Chuck ZmudzinskiWith the current implementation, Seabios does not see the fixup of the device id done here and consequently Seabios does not load the VGA bios and the guest screen does not light up until the guest OS graphics driver is loaded. So there is no VGA output from the passed through Intel IGD from either Seabios or the guest bootloader with the current implementation in cases when the device id needs fixing.
Fix this by waiting until after doing fixup of the device id before registering the option ROM. With this patch, Seabios sees the fixup done here and loads the VGA bios, and both Seabios and the guest bootloader light up the guest screen in cases when fixup of the device id is needed.
Also, remove unused header hw/core/loader.h.
---
Changes in v4:
- Add a Fixes tag
Changes in v5:
- No changes to this patch
hw/xen/xen_pt_graphics.c | 3 +++ hw/xen/xen_pt_load_rom.c | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-)
hw/xen/xen_pt_graphics.c +3 -0
@@ -223,6 +223,9 @@ void xen_pt_setup_vga(XenPCIPassthroughState *s, XenHostPCIDevice *dev, } } +pci_register_bar (&s->dev,PCI_ROM_SLOT ,0 , &s->dev.rom); + s->dev.has_rom = true; +/* Currently we fixed this address as a primary for legacy BIOS. */ physical_memory_write (0xc0000 , bios, bios_size); }
hw/xen/xen_pt_load_rom.c +12 -6
@@ -4,14 +4,22 @@#include "qemu/osdep.h" #include "qapi/error.h" #include "qemu/error-report.h" -#include "hw/core/loader.h" #include "hw/pci/pci.h" #include "xen_pt.h" /* -* Scan the assigned devices for the devices that have an option ROM, and then -* load the corresponding ROM data to RAM. If an error occurs while loading an -* option ROM, we just ignore that option ROM and continue with the next one. +* Normally xen_pt_register_regions will handle loading the option ROM, +* but in some cases, such as for the Intel IGD, the option ROM might +* need to be modified. +* +* For such cases, use this function to get a pointer to the option ROM +* from sysfs. Caller has the responsibility to edit the option ROM as +* needed, call pci_register_bar to register the modified option ROM, +* and set has_rom to true for the PCI device. +* +* This function must be called before xen_pt_register_regions is called +* because if xen_pt_register_regions is called first, it will register +* the option ROM and any attempt to register it again will fail. */ void *pci_assign_dev_load_option_rom(PCIDevice *dev, int *size, unsigned int domain,
@@ -76,8 +84,6 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev, goto close_rom; } -pci_register_bar (dev,PCI_ROM_SLOT ,0 , &dev->rom); - dev->has_rom = true; *size = st.st_size; close_rom:/* Write "0" to disable ROM */
Signature · 1 line
2.52.0
PATCH v5 4/6 xen/igd: enable guest creation when ROM read fails
In reply to Chuck ZmudzinskiFor newer IGD devices, the host option ROM is not readable from sysfs and this results in a call to error_fail() that causes Qemu to exit(1) so guest creation fails with the current implementation for many newer IGD devices. But this read failure need not be a fatal error causing guest creation to fail because the guest does not need the option ROM to successfully boot and run. The guest only needs the option ROM for getting graphics output from the guest during early boot before the guest OS loads the Intel IGD graphics drivers.
To fix this, allow guest creation to continue by avoiding setting errp if the attempt to read the host ROM file from sysfs fails. In this case, the memory for the guest option ROM has been allocated so free that memory by calling object_unparent(OBJECT(&s->dev.rom)) before continuing.
Replace the error_report() and error_printf() messages for this case when the option ROM cannot be read via sysfs with a suitable info_report() message.
In the case when the host option ROM cannot be read via the sysfs interface, xen_pt_register_regions() will attempt to setup the option ROM for the guest the same way it would for any other Xen passthrough PCI device that has an option ROM.
---
Changes in v4:
- v4 is the first version of the series that has this patch
Changes in v5:
- Shorten info_report message to resolve checkpatch line length warning
This patch provides initial support for many newer Intel IGD devices so, at least, guest creation will not fail if such newer Intel IGD devices are passed through to a Xen HVM guest. But this patch alone is not sufficient for proper operation of the Intel IGD for many, if not all, of the newer Intel IGD devices when passed through to a Xen HVM guest.
There are two main problems with more recent, modern devices:
1. The newer divices might require patches to the Intel OpRegion and also an extended video bios table (VBT). Without support for these aspects of the newer devices, the experience will not be great and in many cases the Intel IGD still will not function properly in the guest.
2. The newer devices only work with UEFI AFAICT, and the Ovmf* platforms provided by the upsream edk2 project do not provide support for the Intel IGD. It appears the problem is that the ekd2 project deems the fact that the hardware manufacturer does not provide the necessary firmware, the EFI graphics output protocol (GOP) driver, in the ordinary way by making the EFI GOP driver accessible in virtual environments via the option ROM of the real PCI device, to be a reason to reject patches that add support for the Intel IGD. This, however, is not a fatal problem since it only affects the guest during early boot when OVMF or the bootloader is running and the guest OS graphics drivers have not yet been loaded. Lack of support for the Intel IGD in OVMF does not seem to affect the experience negatively once the guest OS graphics drivers have been loaded. So efforts to address this problem are only important in cases when it is necessary to get graphics output from OVMF and/or the guest bootloader.
The next two patches in this patchset address these two problems. Of those two patches, the first one is more necessary, and the second of those two patches is only needed to provide graphics output from the guest during early boot.
hw/xen/xen_pt_graphics.c | 7 +++++++ hw/xen/xen_pt_load_rom.c | 5 +---- 2 files changed, 8 insertions(+), 4 deletions(-)
hw/xen/xen_pt_graphics.c +7 -0
@@ -187,6 +187,13 @@ void xen_pt_setup_vga(XenPCIPassthroughState *s, XenHostPCIDevice *dev,return ; } +/* Case when the host ROM file from sysfs could not be read */ +if (!bios_size) { +object_unparent (OBJECT (&s->dev.rom)); + bios =NULL ; +return ; + } +if (bios_size <sizeof (struct rom_header )) {error_setg (errp,"VGA: VBIOS image corrupt (too small)" );return ;
hw/xen/xen_pt_load_rom.c +1 -4
@@ -77,10 +77,7 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev,memset (ptr,0xff , dev->romsize);if (!fread (ptr,1 , st.st_size, fp)) { -error_report ("pci-assign: Cannot read from host %s" , rom_file); -error_printf ("Device option ROM contents are probably invalid " -"(check dmesg).\nSkip option ROM probe with rombar=0, " -"or load from file with romfile=\n" ); +info_report ("pci-assign: Can't read Option ROM %s from host" , rom_file); goto close_rom; }
Signature · 1 line
2.52.0
PATCH v5 5/6 xen/igd: implement support for extended VBT
In reply to Chuck ZmudzinskiNewer devices with versions of the OpRegion >= 2 require an extended bios table (VBT) in some cases and also in some cases require modifications to the OpRegion for proper operation in the guest. This is in contrast to legacy devices in which the VBT is always embedded within the OpRegion.
This makes the current approach of providing only the unmodified host OpRegion to the guest with no guest access to the VBT insufficient for proper support of devices with OpRegion version 2 or higher and an extended VBT.
Support for extended VBT also depends on compatible support in hvmloader. If such support is lacking in hvmloader, fall back to the current protocol that does not provide support for extended VBT.
To implement support for extended VBT:
Instead of configuring the guest with access to the unmodified host OpRegion via hypervisor mapping of the OpRegion from the host to the guest, temporarily map the host OpRegion and VBT into the guest, allowing the guest (hvmloader) to get copies of the host OpRegion and VBT which hvmloader can modify as needed to support cases that require modifications to the OpRegion.
In xen_pt_unregister_vga_regions(), do not try to unmap the OpRegion in cases when the OpRegion is not mapped during normal operation of the guest, and replace the constant '3' with the macro XEN_PCI_INTEL_OPREGION_PAGES which is defined to be 3.
To implement this:
Use 'done = true' to end further processing when the OpRegion does not need to be unmapped in xen_pt_unregister_vga_regions(), and use 'guest_supports_opregion2 = false' to end further processing when the OpRegion does need to be unmapped in xen_pt_unregister_vga_regions().
The OpRegion 2+ support that can be provided by this patch and a compatible patch to hvmloader is required to fix code 43 errors in Windows guests that have an Intel IGD with extended VBT passed through to the guest.
---
Changes in v4:
- v4 is the first version of the series that has this patch
Changes in v5:
- fix style by adding braces to two if blocks and not initializing two static boolean variables to false
- update the link to the companion patch for Xen hvmloader
The companion patch to hvmloader that is needed to make this patch take effect is available here:
https://lore.kernel.org/qemu-devel/[email protected]/
hw/xen/xen_pt_graphics.c | 233 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 225 insertions(+), 8 deletions(-)
hw/xen/xen_pt_graphics.c +225 -8
@@ -12,7 +12,26 @@static unsigned long igd_guest_opregion;static unsigned long igd_host_opregion; +/* +* These are true until they are set to false when the guest first +* accesses the OpRegion address register for a read or write, +* respectively. +*/ +static bool first_guest_opregion_read = true; +static bool first_guest_opregion_write = true; + +static uint32_t guest_opregion_extra_writes; +static bool guest_supports_opregion2; +static bool done; +static unsigned long rvda;/* absolute host VBT address */ +static unsigned long vbt_guest_pgbase; +static uint32_t vbt_nr_pages; +#define XEN_PCI_INTEL_OPREGION_MASK 0xfff +#define XEN_PCI_INTEL_OPREGION_PAGES 0x3 +#define XEN_PCI_INTEL_OPREGION_ENABLE_ACCESSED 0x1 +#define XEN_PCI_INTEL_OPREGION_DISABLE_ACCESS 0x0 +#define XEN_PCI_INTEL_OPREGION2_SUPPORT_MASK 0x1 typedef struct VGARegion { int type; /* Memory or port I/O */
@@ -117,11 +136,11 @@ int xen_pt_unregister_vga_regions(XenHostPCIDevice *dev) } } -if (igd_guest_opregion) { +if (!guest_supports_opregion2 && igd_guest_opregion) { ret =xc_domain_memory_mapping (xen_xc, xen_domid, (unsigned long )(igd_guest_opregion >>XC_PAGE_SHIFT ), (unsigned long )(igd_host_opregion >>XC_PAGE_SHIFT ), -3 , +XEN_PCI_INTEL_OPREGION_PAGES ,DPCI_REMOVE_MAPPING );if (ret) {return ret;
@@ -239,7 +258,31 @@ void xen_pt_setup_vga(XenPCIPassthroughState *s, XenHostPCIDevice *dev,uint32_t igd_read_opregion (XenPCIPassthroughState *s) { +if (!igd_host_opregion) { +/* We just work with LE. */ +xen_host_pci_get_block (&s->real_device,XEN_PCI_INTEL_OPREGION , + (uint8_t *)&igd_host_opregion,4 ); + } + +/* +* By returning igd_host_opregion here instead of 0, we can +* indicate to hvmloader that we support OpRegion 2. +* +* The conditions are there to prevent returning igd_host_opregion +* to guests that have a version of hvmloader that lacks support +* for OpRegion 2. We do this to maintain backward compatibility for +* guests with earlier versions of hvmloader that always expect us +* to return 0 instead of igd_host_opregion when igd_guest_opregion +* is not yet set to a non-zero value. +*/ +if (first_guest_opregion_read && !igd_guest_opregion && + first_guest_opregion_write) { + first_guest_opregion_read = false; +return igd_host_opregion; + } +uint32_t val =0 ; + first_guest_opregion_read = false;if (!igd_guest_opregion) {return val;
@@ -251,21 +294,195 @@ uint32_t igd_read_opregion(XenPCIPassthroughState *s)return val; } -#define XEN_PCI_INTEL_OPREGION_PAGES 0x3 -#define XEN_PCI_INTEL_OPREGION_ENABLE_ACCESSED 0x1void igd_write_opregion (XenPCIPassthroughState *s,uint32_t val) {int ret; -if (igd_guest_opregion) { +/* hvmloader with OpRegion 2 support uses lsb of val to indicate support */ +if ((val &XEN_PCI_INTEL_OPREGION2_SUPPORT_MASK ) && + first_guest_opregion_write) { + guest_supports_opregion2 = true; + }else if (first_guest_opregion_write) { +XEN_PT_LOG (&s->dev,"hvmloader lacks extended VBT support, " +"continuing with legacy support only\n" ); + } + +if ((!guest_supports_opregion2 && igd_guest_opregion) || done) {XEN_PT_LOG (&s->dev,"opregion register already been set, ignoring %x\n" , val);return ; } -/* We just work with LE. */ -xen_host_pci_get_block (&s->real_device,XEN_PCI_INTEL_OPREGION , - (uint8_t *)&igd_host_opregion,4 ); +if (guest_supports_opregion2 && !first_guest_opregion_write) { +/* +* OpRegion 2 is supported and we are processing +* additional writes that the legacy protocol ignores. +* +* We should always return from this if block to prevent +* executing code below which is only for the first write +* when we map the host OpRegion into the guest. +*/ + guest_opregion_extra_writes++; +switch (guest_opregion_extra_writes) { +case 1 : +/* +* Hvmloader expects us to store the value as the least +* significant DWORD of rvda. +*/ + rvda = (unsigned long )val; +break ; +case 2 : +/* +* Hvmloader expects us to store the value as the most +* significant DWORD of rvda and unmap the OpRegion if +* rvda is not equal to zero. +* +* If the unmapping fails, hvmloader will fall back to the +* behavior of older versions which simply map the OpRegion +* from the host to the guest without trying to configure +* the guest with OpRegion 2 with extended VBT support. +*/ + rvda |= (unsigned long )(val) <<32 ; +if (rvda) { + ret =xc_domain_memory_mapping (xen_xc, xen_domid, + (unsigned long ) + (igd_guest_opregion >>XC_PAGE_SHIFT ), + (unsigned long ) + (igd_host_opregion >>XC_PAGE_SHIFT ), +XEN_PCI_INTEL_OPREGION_PAGES , +DPCI_REMOVE_MAPPING ); +if (ret) { +XEN_PT_ERR (&s->dev,"[%d]:Can't unmap IGD host opregion:0x%lx" +" from guest opregion:0x%lx.\n" , ret, + (unsigned long )(igd_host_opregion >>XC_PAGE_SHIFT ), + (unsigned long )(igd_guest_opregion >>XC_PAGE_SHIFT )); + rvda =0 ; + guest_supports_opregion2 = false; + } + ret =xc_domain_iomem_permission (xen_xc, xen_domid, + (unsigned long ) + (igd_host_opregion >>XC_PAGE_SHIFT ), +XEN_PCI_INTEL_OPREGION_PAGES , +XEN_PCI_INTEL_OPREGION_DISABLE_ACCESS ); +if (ret) { +XEN_PT_WARN (&s->dev,"[%d]:Can't disable access to IGD host" +" OpRegion: 0x%x.\n" , ret, + (unsigned long )(igd_host_opregion >>XC_PAGE_SHIFT )); + } + }else { + guest_supports_opregion2 = false; + } +break ; +case 3 : +/* +* Hvmloader expects us to store the value as the address +* to map the VBT to in the guest and to map the VBT at the +* provided address in the guest. Hvmloader encodes the number +* of pages to map in the least significant 12 bits of the +* provided address. +* +* If VBT verification fails, hvmloader can't determine if the +* VBT is mapped but corrupted or unmapped, so it crashes the +* guest as an unrecoverable error. +*/ + +/* address (gfn) to map VBT to in the guest */ + vbt_guest_pgbase = val >>XC_PAGE_SHIFT ; + vbt_nr_pages = val &XEN_PCI_INTEL_OPREGION_MASK ; + ret =xc_domain_iomem_permission (xen_xc, xen_domid, + (unsigned long )(rvda >>XC_PAGE_SHIFT ), + vbt_nr_pages, +XEN_PCI_INTEL_OPREGION_ENABLE_ACCESSED ); +if (ret) { +XEN_PT_ERR (&s->dev,"[%d]:Can't enable access to IGD host VBT:" +" 0x%lx.\n" , ret, + (unsigned long )(rvda >>XC_PAGE_SHIFT )), + rvda =0 ; + vbt_guest_pgbase =0 ; + vbt_nr_pages =0 ; + done = true; +break ; + } + ret =xc_domain_memory_mapping (xen_xc, xen_domid, + (unsigned long )vbt_guest_pgbase, + (unsigned long )(rvda >>XC_PAGE_SHIFT ), + vbt_nr_pages,DPCI_ADD_MAPPING ); +if (ret) { +XEN_PT_ERR (&s->dev,"[%d]:Can't map IGD host VBT:0x%lx to" +" guest VBT:0x%lx.\n" , ret, + (unsigned long )(rvda >>XC_PAGE_SHIFT ), + (unsigned long )vbt_guest_pgbase); + rvda =0 ; + vbt_guest_pgbase =0 ; + vbt_nr_pages =0 ; + done = true; +break ; + } +XEN_PT_LOG (&s->dev,"Map VBT: 0x%lx -> 0x%lx\n" , + (unsigned long )(rvda >>XC_PAGE_SHIFT ), + (unsigned long )vbt_guest_pgbase); +XEN_PT_LOG (&s->dev,"VBT host address: 0x%lx\n" , rvda); +break ; +case 4 : +/* +* Hvmloader expects us to store the given value as the +* final value for the register that stores the OpRegion +* address in the guest. We also unmap the VBT since the +* guest now has its own copy of both it and the OpRegion. +* +* If the unmapping fails the VBT will be mapped where +* hvmloader needs to place the OpRegion plus VBT in the +* guest E820 map. In this case, hvmloader will crash with +* BUG() rather than try to use the mapped VBT with the +* guest's copy of the OpRegion. +*/ + igd_guest_opregion = val; + ret =xc_domain_memory_mapping (xen_xc, xen_domid, + (unsigned long )vbt_guest_pgbase, + (unsigned long )(rvda >>XC_PAGE_SHIFT ), + vbt_nr_pages,DPCI_REMOVE_MAPPING ); +if (ret) { +XEN_PT_ERR (&s->dev,"[%d]:Can't unmap IGD host VBT:0x%lx from" +" guest VBT:0x%lx.\n" , ret, + (unsigned long )(rvda >>XC_PAGE_SHIFT ), + (unsigned long )vbt_guest_pgbase); + rvda =0 ; + done = true; +break ; + } + + ret =xc_domain_iomem_permission (xen_xc, xen_domid, + (unsigned long )(rvda >>XC_PAGE_SHIFT ), + vbt_nr_pages, +XEN_PCI_INTEL_OPREGION_DISABLE_ACCESS ); +if (ret) { +XEN_PT_WARN (&s->dev,"[%d]:Can't disable access to IGD host" +" VBT: 0x%x.\n" , ret, + (unsigned long )(rvda >>XC_PAGE_SHIFT )); + } + + done = true; +break ; +default : +break ; + } +return ; + } + +/* +* This code handles the first write to the register from the guest. +* It maps the host OpRegion into the guest. +* +* Set first_guest_opregion_write to false to enable more writes +* if OpRegion 2 is supported. +*/ + first_guest_opregion_write = false; + +if (!igd_host_opregion) { +/* We just work with LE. */ +xen_host_pci_get_block (&s->real_device,XEN_PCI_INTEL_OPREGION , + (uint8_t *)&igd_host_opregion,4 ); + } igd_guest_opregion = (unsigned long )(val & ~XEN_PCI_INTEL_OPREGION_MASK ) | (igd_host_opregion &XEN_PCI_INTEL_OPREGION_MASK );
Signature · 1 line
2.52.0
PATCH v5 6/6 xen/igd: use custom option ROM if provided
In reply to Chuck ZmudzinskiSince in some cases the option ROM is not readable from sysfs on the host, provide the option to use a custom option ROM file instead that, for example, could be extracted from BIOS or UEFI firmware and modified as needed for use with a particular Intel IGD device.
The file must be named "igd.rom" and be located in a directory configured at build time as a Qemu firmware directory and its size should be a power of two, and it must be compatible with the particular Intel IGD device being passed through.
If provided, the "igd.rom" file will be used as the option ROM instead of the option ROM file epxosed in the host sysfs.
If no "igd.rom" file is provided, this patch has no effect.
---
Changes in v4:
- v4 is the first version of the series that has this patch
Changes in v5:
- Fix wrong whitespace in three places in a conditional block
Sorry for the length of these notes but there are many things to say about this patch that are not obvious to persons without some experience of actually trying to use the option ROM of an Intel IGD when it is passed through to a Xen HVM guest.
This patch is primarily for providing a way to add Intel IGD support for the OvmfXen platform to get graphics output during early boot from modern Intel IGD devices that are only compatible with UEFI for graphics output during early boot.
Note this patch is not necessary for successful operation of the Intel IGD in the guest once the guest OS drivers have loaded. It is only needed as part of the patchset necessary to provide graphics output from the Intel IGD in the guest during early boot when using newer devices that are only compatible with UEFI for graphics output during early boot. Most older devices that are compatible with legacy VGA BIOS will work with Seabios without this patch, but they will need Patch 3 of this patchset to work with Seabios.
Some notes on adding Intel IGD support for the OvmfXen platform:
It is necessary to provide an EFI graphics output protocol (GOP) driver to the guest to get output from the Intel IGD before the guest OS loads the graphics drivers when the guest uses UEFI. This GOP driver is essentially the replacement of the VBIOS driver that applied to older devices that use legacy bios, as described here:
https://www.intel.com/content/www/us/en/support/articles/000005749/graphics.html
Unfortunately, with modern Intel IGD devices, the EFI GOP driver is not provided to the guest in the usual way of providing firmware for a PCI device in the option ROM of the real PCI device. So I included this patch in this patchset to provide a way to expose the EFI GOP driver to the guest. I was able to extract the GOP driver for my device using the UEFI bios update file from the motherboard manufacturer and the UEFITool available here:
https://github.com/longsoft/uefitool
That EFI driver can be wrapped into an option ROM using the EfiRom bin wrapper that is part of the edk2 project:
https://github.com/tianocore/edk2/blob/master/BaseTools/BinWrappers/PosixLike/EfiRom
I tried setting the 'romfile' member of the PCIDevice struct that is used by KVM/VFIO Qemu devices and emulated Qemu PCI devices, but that did not work with Xen PCI passthrough devices. Neither Seabios nor the OvmfXen platform could detect the option ROM in the guest with that method of exposing an option ROM to the guest. So I implemented this approach of substituting the 'rom' file exposed by sysfs with an administrator-provided file instead of using 'romfile'.
In the commit message I mentioned the size of the rom file "should" be a power of two. I mentioned this because the code in pci.c that handles the 'romfile' setting for PCI devices enforces this requirement strictly on the romfile that Qemu emulated or VFIO devices use. However, I do not know for sure whether or not the rom file is strictly required to have a size of a power of two, so that is why I say it should be a power of two. In my testing, I zero pad the "igd.rom" file so it has a size of a power of two. I will accept the suggestions of experts on this question about the appropriate size of the option ROM file (I am not such an expert!).
As mentioned in the message accompanying Patch 4 of this patchset, the official edk2 project does not provide support for the Intel IGD, but some OVMF patches for Intel IGD support are available online for KVM/VFIO guests, such as at the links below (they apply to the OvmfPkgX64 platform):
https://github.com/cmd2001/build-edk2-gvtd
https://eci.intel.com/docs/3.3/components/kvm-hypervisor.html#build-ovmf-fd-for-kvm
https://github.com/LongQT-sea/intel-igpu-passthru
With such patches it is reported that the passed through Intel IGD device lights up the display during early boot from OVMF and the guest bootloader in KVM/VFIO guests provided that the administrator provides the correct ROM file via the 'romfile' setting for the passed thorugh Intel iGD device and applies appropriate patches to the OvmfPkgX64 platform.
It should also be possible to add Intel IGD support for the OvmfXen platform also but I have not seen any such patches online for OvmfXen and if anyone knows of such patches online I would be interested to be informed about them. I am also working on my own patches to add Intel IGD support to the OvmfXen platform, in private for now. If anyone is interested, I can make the work I have done so far toward this goal avalable online.
hw/xen/xen_pt_load_rom.c | 47 +++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 15 deletions(-)
hw/xen/xen_pt_load_rom.c +32 -15
@@ -2,6 +2,7 @@* This is splited from hw/i386/kvm/pci-assign.c */ #include "qemu/osdep.h" +#include "qemu/datadir.h" #include "qapi/error.h" #include "qemu/error-report.h" #include "hw/pci/pci.h"
@@ -13,9 +14,9 @@* need to be modified. * * For such cases, use this function to get a pointer to the option ROM -* from sysfs. Caller has the responsibility to edit the option ROM as -* needed, call pci_register_bar to register the modified option ROM, -* and set has_rom to true for the PCI device. +* from a user provided romfile or sysfs. Caller has the responsibility +* to edit the option ROM as needed, call pci_register_bar to register +* the modified option ROM, and set has_rom to true for the PCI device. * * This function must be called before xen_pt_register_regions is called * because if xen_pt_register_regions is called first, it will register
@@ -32,17 +33,27 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev,struct stat st;void *ptr =NULL ;Object *owner =OBJECT (dev); + g_autofree const char *fname = g_strdup("igd.rom"); + g_autofree const char *path = qemu_find_file(QEMU_FILE_TYPE_BIOS, fname); +bool sysfs = false;/* If loading ROM from file, pci handles it */ if (dev->romfile || !dev->rom_bar) {return NULL ; } -snprintf (rom_file,sizeof (rom_file), -"/sys/bus/pci/devices/%04x:%02x:%02x.%01x/rom" , - domain, bus, slot, function); +if (path) { +snprintf (rom_file,sizeof (rom_file),"%s" , path); +XEN_PT_LOG (dev,"Using Intel IGD romfile %s " +"(administratior provided)\n" , path); + }else { +snprintf (rom_file,sizeof (rom_file), +"/sys/bus/pci/devices/%04x:%02x:%02x.%01x/rom" , + domain, bus, slot, function); + sysfs = true; +XEN_PT_LOG (dev,"Using Intel IGD romfile from host sysfs\n" ); + } -/* Write "1" to the ROM file to enable it */ fp =fopen (rom_file,"r+" );if (fp ==NULL ) {if (errno !=ENOENT ) {
@@ -55,10 +66,14 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev, goto close_rom; } - val =1 ; -if (fwrite (&val,1 ,1 , fp) !=1 ) { - goto close_rom; +/* Write "1" to the ROM file to enable it if using ROM from sysfs */ +if (sysfs) { + val =1 ; +if (fwrite (&val,1 ,1 , fp) !=1 ) { + goto close_rom; + } } +fseek (fp,0 ,SEEK_SET );if (dev->romsize !=UINT_MAX ) {
@@ -83,11 +98,13 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev, *size = st.st_size; close_rom: -/* Write "0" to disable ROM */ -fseek (fp,0 ,SEEK_SET ); - val =0 ; -if (!fwrite (&val,1 ,1 , fp)) { -XEN_PT_WARN (dev,"%s\n" ,"Failed to disable pci-sysfs rom file" ); +/* Write "0" to disable ROM if using ROM from sysfs */ +if (sysfs) { +fseek (fp,0 ,SEEK_SET ); + val =0 ; +if (!fwrite (&val,1 ,1 , fp)) { +XEN_PT_WARN (dev,"%s\n" ,"Failed to disable pci-sysfs rom file" ); + } }fclose (fp);
Signature · 1 line
2.52.0