From nobody Thu Apr 3 23:35:58 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1740422049; cv=none; d=zohomail.com; s=zohoarc; b=QFq8t17L+nGA10U90XTVeCS2kM4q4hI8SIk9htk+T/I4Cb7kJ19kSsmvxyMDUEvE35pm8S/SOnN8eCboYSEG8oYp9ZMXgONawwRJznznTu0IKfjM6R+JIq0ShKp/Gf0pvTq004QvengC4SSx12Z6E7CDgiPLr5VozcuQCkfWg84= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1740422049; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=tXOjZtgFlfgDDzD85XnbBJ3mLhtlWv0xEfuhDPGRItM=; b=fuclqgKBN91o9fRicn4fwwByFCpQumq8iCj2L0h2Wk43skwe5MWUpbeVZTF4pQQddIsV0BYpU5axMf7a4Z8u1mN+hxxGg3qPLlJ0aovkEcXFLgaviyQ+Q1KxXcBmgaO/PTkyqjNnEFgu2QNg4lKUbs7R2ChC8HTJI01lQAck/qI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1740422049711983.1963827867137; Mon, 24 Feb 2025 10:34:09 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tmdH1-00020x-LJ; Mon, 24 Feb 2025 13:33:47 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tmdDQ-0005oW-R2 for qemu-devel@nongnu.org; Mon, 24 Feb 2025 13:30:05 -0500 Received: from mail-pl1-x644.google.com ([2607:f8b0:4864:20::644]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tmdDH-0003Na-1h for qemu-devel@nongnu.org; Mon, 24 Feb 2025 13:30:03 -0500 Received: by mail-pl1-x644.google.com with SMTP id d9443c01a7336-220e83d65e5so90760845ad.1 for ; Mon, 24 Feb 2025 10:29:53 -0800 (PST) Received: from localhost.localdomain ([116.227.8.253]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-220d545cf6bsm183662615ad.123.2025.02.24.10.29.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Feb 2025 10:29:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740421791; x=1741026591; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tXOjZtgFlfgDDzD85XnbBJ3mLhtlWv0xEfuhDPGRItM=; b=k/xWBeuQjXnKTUpFyLGm6Px0PcAoH4cGDpAMMfrjcmKP3aWaTicweKAV3EP5SepoKx XOKO9eIK8nqULxT4xyGVhXOJ5UhcitQ6S8DlhJvGq7fPLBqIvomO4+raMSymBJbmcPX0 Lb0V1KVhuelEdporSIXtWVmtJd0c54bIDAcXQG3ph3CTQQ1Rgi6vnY4VcXLWbZPD5RLp qNjmaykFcamHmUVSWzfquOZZ8IP9L1hfFbZMkSWyJrq1XTTPHXw6PQRIJjqpEvAPu+dG fmEuSXIc7B47nuIKpet2wNurEq1z2TYdDqSY7VjayfDWReQd3JHlgkeWvRkStHoDnG8F V8Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740421791; x=1741026591; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tXOjZtgFlfgDDzD85XnbBJ3mLhtlWv0xEfuhDPGRItM=; b=PPRFbweH4bx+zy9rujhyzNHv15E3gZuMLkj2bydoTi/nfXXIaVzZfgGDf+QimFSmgz 9SVZ5za4L5X39F1W52RdF7S3/56RgwhpyKShbpPrgyY2NTVWPWeohsUZVmJZg1XRW97F khgKziNpsbq5C02dQRUroB1MP+ApaXKEFtwiqFaneP5phduN4aEpxpri8hwW1KmttupF VKAw+cKcA4+rTfrtknVG46pDzsub0t5PdyNd9gBEDFnAxst3cxxvr14T44F1sdGXd29O 5e/qMmSLWi5j1XzsMMYID5r3+hdYVgy07Twb4oBjNA5KwDYbcnSSAm6M697Esao2i87w HCgQ== X-Gm-Message-State: AOJu0Yw7SptFA/N4M4+kh2X0Q0CWN0hwgrYIPxR2CWeL6T/Qy0bj7DFd 3d9so9sjNAVpgVndcLXyio2vLt9KGoaDyI4Q2hsMP7T0JfxVE6Z97EU4OZSpJQ== X-Gm-Gg: ASbGnctspFqWIlGm6azmpjUL3Z9SwvaiLLvkekrbNPNIsEeRD0ZtAzxtEhvAjrAf+UC nucXqRQ4Q1IwZwOUmlMG1DKi67vz+73imtXAcohM7Gy/KIcdVX/dod3FLrvGRQ8CbKPsMuWm8ld aYh+vgcaOFsz+ksedaMEFcYNb4OiQY56C40X4n0zFuYSGoWD+ywXWLdFYvfrsFnD6GlO/V0Skmd fgGkzlad5TjpXRB2R/1oMfL1RWzxhfmakilC1qh2ZkUc8y3k5LF1vhAph1gXZj3uqP53tVi5epg AClR8iio/grN4T5RSfz8iN+G5wHQ9P81A3hk8E8= X-Google-Smtp-Source: AGHT+IGTn3BRGTjR7r7wbP4M2yQxBmC/WB48+TKjpAnD7zwO9xi61IzgvUOtwy2hQ1GnrbZAitLivw== X-Received: by 2002:a17:902:f70d:b0:220:fae9:8a4 with SMTP id d9443c01a7336-2219ff31c0cmr229795855ad.6.1740421791022; Mon, 24 Feb 2025 10:29:51 -0800 (PST) From: Tomita Moeko To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: qemu-devel@nongnu.org, Tomita Moeko Subject: [PATCH 06/10] vfio/igd: Consolidate OpRegion initialization into a single function Date: Tue, 25 Feb 2025 02:29:23 +0800 Message-ID: <20250224182927.31519-7-tomitamoeko@gmail.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250224182927.31519-1-tomitamoeko@gmail.com> References: <20250224182927.31519-1-tomitamoeko@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::644; envelope-from=tomitamoeko@gmail.com; helo=mail-pl1-x644.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1740422050505019000 Content-Type: text/plain; charset="utf-8" Both x-igd-opregion option and legacy mode require identical steps to set up OpRegion for IGD devices. Consolidate these steps into a single vfio_pci_igd_setup_opregion function. The function call in pci.c is wrapped with ifdef temporarily to prevent build error for non-x86 archs, it will be removed later after the removal of legacy mode. Additionally, move vfio_pci_igd_opregion_init to igd.c to prevent it from being compiled in non-x86 builds. Signed-off-by: Tomita Moeko --- hw/vfio/igd.c | 111 +++++++++++++++++++++++++++++++++---------- hw/vfio/pci-quirks.c | 50 ------------------- hw/vfio/pci.c | 22 ++------- hw/vfio/pci.h | 4 +- 4 files changed, 89 insertions(+), 98 deletions(-) diff --git a/hw/vfio/igd.c b/hw/vfio/igd.c index 32ae263c23..7c7461e22d 100644 --- a/hw/vfio/igd.c +++ b/hw/vfio/igd.c @@ -106,6 +106,7 @@ static int igd_gen(VFIOPCIDevice *vdev) return -1; } =20 +#define IGD_ASLS 0xfc /* ASL Storage Register */ #define IGD_GMCH 0x50 /* Graphics Control Register */ #define IGD_BDSM 0x5c /* Base Data of Stolen Memory */ #define IGD_BDSM_GEN11 0xc0 /* Base Data of Stolen Memory of gen 11 and la= ter */ @@ -138,6 +139,82 @@ static uint64_t igd_stolen_memory_size(int gen, uint32= _t gmch) return 0; } =20 +/* + * The OpRegion includes the Video BIOS Table, which seems important for + * telling the driver what sort of outputs it has. Without this, the devi= ce + * may work in the guest, but we may not get output. This also requires B= IOS + * support to reserve and populate a section of guest memory sufficient for + * the table and to write the base address of that memory to the ASLS regi= ster + * of the IGD device. + */ +static bool vfio_pci_igd_opregion_init(VFIOPCIDevice *vdev, + struct vfio_region_info *info, + Error **errp) +{ + int ret; + + vdev->igd_opregion =3D g_malloc0(info->size); + ret =3D pread(vdev->vbasedev.fd, vdev->igd_opregion, + info->size, info->offset); + if (ret !=3D info->size) { + error_setg(errp, "failed to read IGD OpRegion"); + g_free(vdev->igd_opregion); + vdev->igd_opregion =3D NULL; + return false; + } + + /* + * Provide fw_cfg with a copy of the OpRegion which the VM firmware is= to + * allocate 32bit reserved memory for, copy these contents into, and w= rite + * the reserved memory base address to the device ASLS register at 0xF= C. + * Alignment of this reserved region seems flexible, but using a 4k pa= ge + * alignment seems to work well. This interface assumes a single IGD + * device, which may be at VM address 00:02.0 in legacy mode or another + * address in UPT mode. + * + * NB, there may be future use cases discovered where the VM should ha= ve + * direct interaction with the host OpRegion, in which case the write = to + * the ASLS register would trigger MemoryRegion setup to enable that. + */ + fw_cfg_add_file(fw_cfg_find(), "etc/igd-opregion", + vdev->igd_opregion, info->size); + + trace_vfio_pci_igd_opregion_enabled(vdev->vbasedev.name); + + pci_set_long(vdev->pdev.config + IGD_ASLS, 0); + pci_set_long(vdev->pdev.wmask + IGD_ASLS, ~0); + pci_set_long(vdev->emulated_config_bits + IGD_ASLS, ~0); + + return true; +} + +bool vfio_pci_igd_setup_opregion(VFIOPCIDevice *vdev, Error **errp) +{ + g_autofree struct vfio_region_info *opregion =3D NULL; + int ret; + + /* Hotplugging is not supprted for opregion access */ + if (vdev->pdev.qdev.hotplugged) { + error_setg(errp, "IGD OpRegion is not supported on hotplugged devi= ce"); + return false; + } + + ret =3D vfio_get_dev_region_info(&vdev->vbasedev, + VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_INTEL, + VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION, &opregion); + if (ret) { + error_setg_errno(errp, -ret, + "Device does not supports IGD OpRegion feature"); + return false; + } + + if (!vfio_pci_igd_opregion_init(vdev, opregion, errp)) { + return false; + } + + return true; +} + /* * The rather short list of registers that we copy from the host devices. * The LPC/ISA bridge values are definitely needed to support the vBIOS, t= he @@ -339,9 +416,9 @@ void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int= nr) QLIST_INSERT_HEAD(&vdev->bars[nr].quirks, bdsm_quirk, next); } =20 + void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr) { - g_autofree struct vfio_region_info *opregion =3D NULL; g_autofree struct vfio_region_info *host =3D NULL; g_autofree struct vfio_region_info *lpc =3D NULL; PCIDevice *lpc_bridge; @@ -389,29 +466,11 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, i= nt nr) return; } =20 - /* - * Ignore the hotplug corner case, we can't create the devices we - * need for legacy mode in the hotplug scenario. - */ - if (vdev->pdev.qdev.hotplugged) { - error_report("IGD device %s hotplugged, legacy mode disabled", - vdev->vbasedev.name); - return; - } =20 /* * Check whether we have all the vfio device specific regions to * support legacy mode (added in Linux v4.6). If not, bail. */ - ret =3D vfio_get_dev_region_info(&vdev->vbasedev, - VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_I= NTEL, - VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION, &opregion); - if (ret) { - error_report("IGD device %s does not support OpRegion access," - "legacy mode disabled", vdev->vbasedev.name); - return; - } - ret =3D vfio_get_dev_region_info(&vdev->vbasedev, VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_I= NTEL, VFIO_REGION_SUBTYPE_INTEL_IGD_HOST_CFG, &host); @@ -430,6 +489,13 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, in= t nr) return; } =20 + /* Setup OpRegion access */ + if (!vfio_pci_igd_setup_opregion(vdev, &err)) { + error_append_hint(&err, "IGD legacy mode disabled\n"); + error_report_err(err); + return; + } + /* Create our LPC/ISA bridge */ ret =3D vfio_pci_igd_lpc_init(vdev, lpc); if (ret) { @@ -446,13 +512,6 @@ void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, in= t nr) return; } =20 - /* Setup OpRegion access */ - if (!vfio_pci_igd_opregion_init(vdev, opregion, &err)) { - error_append_hint(&err, "IGD legacy mode disabled\n"); - error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); - return; - } - gmch =3D vfio_pci_read_config(&vdev->pdev, IGD_GMCH, 4); =20 /* diff --git a/hw/vfio/pci-quirks.c b/hw/vfio/pci-quirks.c index fbe43b0a79..cac0aa1910 100644 --- a/hw/vfio/pci-quirks.c +++ b/hw/vfio/pci-quirks.c @@ -1114,56 +1114,6 @@ static void vfio_probe_rtl8168_bar2_quirk(VFIOPCIDev= ice *vdev, int nr) trace_vfio_quirk_rtl8168_probe(vdev->vbasedev.name); } =20 -#define IGD_ASLS 0xfc /* ASL Storage Register */ - -/* - * The OpRegion includes the Video BIOS Table, which seems important for - * telling the driver what sort of outputs it has. Without this, the devi= ce - * may work in the guest, but we may not get output. This also requires B= IOS - * support to reserve and populate a section of guest memory sufficient for - * the table and to write the base address of that memory to the ASLS regi= ster - * of the IGD device. - */ -bool vfio_pci_igd_opregion_init(VFIOPCIDevice *vdev, - struct vfio_region_info *info, Error **err= p) -{ - int ret; - - vdev->igd_opregion =3D g_malloc0(info->size); - ret =3D pread(vdev->vbasedev.fd, vdev->igd_opregion, - info->size, info->offset); - if (ret !=3D info->size) { - error_setg(errp, "failed to read IGD OpRegion"); - g_free(vdev->igd_opregion); - vdev->igd_opregion =3D NULL; - return false; - } - - /* - * Provide fw_cfg with a copy of the OpRegion which the VM firmware is= to - * allocate 32bit reserved memory for, copy these contents into, and w= rite - * the reserved memory base address to the device ASLS register at 0xF= C. - * Alignment of this reserved region seems flexible, but using a 4k pa= ge - * alignment seems to work well. This interface assumes a single IGD - * device, which may be at VM address 00:02.0 in legacy mode or another - * address in UPT mode. - * - * NB, there may be future use cases discovered where the VM should ha= ve - * direct interaction with the host OpRegion, in which case the write = to - * the ASLS register would trigger MemoryRegion setup to enable that. - */ - fw_cfg_add_file(fw_cfg_find(), "etc/igd-opregion", - vdev->igd_opregion, info->size); - - trace_vfio_pci_igd_opregion_enabled(vdev->vbasedev.name); - - pci_set_long(vdev->pdev.config + IGD_ASLS, 0); - pci_set_long(vdev->pdev.wmask + IGD_ASLS, ~0); - pci_set_long(vdev->emulated_config_bits + IGD_ASLS, ~0); - - return true; -} - /* * Common quirk probe entry points. */ diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 1c45e3987e..56d7097a7a 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -3132,30 +3132,14 @@ static void vfio_realize(PCIDevice *pdev, Error **e= rrp) vfio_bar_quirk_setup(vdev, i); } =20 +#ifdef CONFIG_VFIO_IGD if (!vdev->igd_opregion && vdev->features & VFIO_FEATURE_ENABLE_IGD_OPREGION) { - g_autofree struct vfio_region_info *opregion =3D NULL; - - if (vdev->pdev.qdev.hotplugged) { - error_setg(errp, - "cannot support IGD OpRegion feature on hotplugged " - "device"); - goto out_unset_idev; - } - - ret =3D vfio_get_dev_region_info(vbasedev, - VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_I= NTEL, - VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION, &opregion); - if (ret) { - error_setg_errno(errp, -ret, - "does not support requested IGD OpRegion feat= ure"); - goto out_unset_idev; - } - - if (!vfio_pci_igd_opregion_init(vdev, opregion, errp)) { + if (!vfio_pci_igd_setup_opregion(vdev, errp)) { goto out_unset_idev; } } +#endif =20 /* QEMU emulates all of MSI & MSIX */ if (pdev->cap_present & QEMU_PCI_CAP_MSIX) { diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h index 75505af6a3..ca7c0415f0 100644 --- a/hw/vfio/pci.h +++ b/hw/vfio/pci.h @@ -226,9 +226,7 @@ bool vfio_pci_host_match(PCIHostDeviceAddress *addr, co= nst char *name); int vfio_pci_get_pci_hot_reset_info(VFIOPCIDevice *vdev, struct vfio_pci_hot_reset_info **info_= p); =20 -bool vfio_pci_igd_opregion_init(VFIOPCIDevice *vdev, - struct vfio_region_info *info, - Error **errp); +bool vfio_pci_igd_setup_opregion(VFIOPCIDevice *vdev, Error **errp); =20 void vfio_display_reset(VFIOPCIDevice *vdev); bool vfio_display_probe(VFIOPCIDevice *vdev, Error **errp); --=20 2.47.2