From nobody Tue Dec 16 09:16:26 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=1748447830; cv=none; d=zohomail.com; s=zohoarc; b=JjPUs3QOHX9vgqiVgrFncCg+L1xujZ27N5hymSKe6mcs+g2ViElspQI50g4d2ES8iSNXUyVVfLVX2bXzkRlw8T0e2r3rKylKOWk5raYLp0HpsAZaad/q8p9l9VJKS4Fq8Mz5C1IeHF6PnobNELiP3DnQ/c6FBca7RpJvUI3uofs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748447830; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=NyTwWlT35h762q2znPKQtKWn5WvVU53R3yV7crEc5XY=; b=IOedVbaf2RL7/JBp/h8NRFXeqJIzHrThZLPAtHwezmNIUmjRS3TFTyhdnljLzkkdIh5yvDs3/ejVSp3UAmWWX/fTz4TcGfwmVd0rZUnzhOCbUYN3wohG03ZGNql8sJDlu0+2SfnSJ5bp0eimhoMl5M7IefJ6EBf4DuWbrVc7Ib4= 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 1748447830980560.1327081366846; Wed, 28 May 2025 08:57:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uKJ8d-0005vR-Vf; Wed, 28 May 2025 11:56:20 -0400 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 1uKJ8c-0005v3-Ns for qemu-devel@nongnu.org; Wed, 28 May 2025 11:56:18 -0400 Received: from mail-pj1-x1044.google.com ([2607:f8b0:4864:20::1044]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uKJ8a-0000uW-UE for qemu-devel@nongnu.org; Wed, 28 May 2025 11:56:18 -0400 Received: by mail-pj1-x1044.google.com with SMTP id 98e67ed59e1d1-31118251071so2952852a91.1 for ; Wed, 28 May 2025 08:56:14 -0700 (PDT) Received: from localhost.localdomain ([139.227.17.74]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-311e9b45d8dsm1322079a91.1.2025.05.28.08.56.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 May 2025 08:56:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1748447773; x=1749052573; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=NyTwWlT35h762q2znPKQtKWn5WvVU53R3yV7crEc5XY=; b=YzsZ//pCoEZXPbfXlmCOuOalglSMEiASqjhjo29LWBkQ7qtsW1DHlaoXe+bCIFXCss ShDhMIhmeMk9TWHBwrEe1+J/2UCFj1uuj6+24XFnK+rVoGx/Q/n7v5ZWTRXHfMJHE38s vdOSEfgE8VcbUJpgmj2pkXjb8rl1Xlmiwai5PuPkOm0HSCUZyCtrQv6ayqYJEu1W4Aa+ 5T1A0TBHewXa39/vDwI54kvUWajPXPRrucEIqUie0Et1JiURNzGnu47eRxQYp4lTZW6F 28N8iVF35jJhNboHAK0XkO0+obnbaxVrcJjgjwaYoUoMOjM5xFA8LNBRkppFRICnDG1a OmGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748447773; x=1749052573; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NyTwWlT35h762q2znPKQtKWn5WvVU53R3yV7crEc5XY=; b=aARbMF1cFnWJ+RLSUa65u/P0NhE65j69IAXlFxDqpStMS8QnE+zXLwu4KUkoqBacVI ms0dhPSGndO9bivO1e65QC3P20XcEOcXR/wHEMsRLIwHH9twbp4emTl+FsLUxIutcl4/ 4C/QVKR1kJNBvv0kpoaJapV5KXGshrZo1Y+KYLBHoBa5oI2zSMxA5R858lqHRIYQBh3x yqrJI2bOAMgn82sQUgHbIdugSD+Bf72/32HTFFccR1oWxN/t9HSJwQ8feRlmXl22wNSY q+uXr5/66MfREYUOpyrLEmWIZy1foDGLA4BEf5RYeWAVIf2mroBnFc1+3AeCm/G8RfVi w2Tg== X-Forwarded-Encrypted: i=1; AJvYcCVBYNCfJNTYQKKjHP0G+FTrtAZEdSAW8omTbEyH7R7PL+f8SVh7XkZnzQxnSFG3suoUFA0hwCB1BItB@nongnu.org X-Gm-Message-State: AOJu0Yz9UTcaqc3kj1iT9hHLH5qR6X+hNSlXsVObmWm5fCwiUuajvIqv 9+1Gil58PWay5jyID4BkFHSE2202CHM+sYvjLDM0wCXEz2tV8o3Jqwg= X-Gm-Gg: ASbGncs1FPRQkdB8BPn2b1K9c+Wv4WqbDDzZKMHdj7Z3PsZdWUdAp7ansLSsBuVohqj 3GNk6YhqpEB3TzCSB6YMMIgTJK0rFcgAarVJmifDiSeOzPTX5hbV927aB1JdNBfSI14MzkW8DQu v4LbIKgrgHDrzPgNcbpERvKHGh4mJZ+l8xorreLj4zGucKLdBwJR+lLOObZ0lN85OyYMxXWHQMu sHu6KZc6FWqzbCDvugM+9lKmvyBHWzPKmjcFTALmePvpKT1i3y73SgnQeiQlldSvHWdu+DKNxiu LmIfBmNFzwRNzec5UtmPWLqs48KHqo7spXYJQxORhVZlJ8u90bEsd1WtUqEzG5Hb X-Google-Smtp-Source: AGHT+IEU4Lnn1KXzqOv9DhKN/pYO7xqOm/UTAGs2zALh747qwGK6hN2QSmyWMX4wIFuDLXWxhENHog== X-Received: by 2002:a17:90b:1997:b0:311:a314:c2dc with SMTP id 98e67ed59e1d1-311a314c453mr11729096a91.14.1748447772580; Wed, 28 May 2025 08:56:12 -0700 (PDT) From: Tomita Moeko To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: Tomita Moeko , qemu-devel@nongnu.org Subject: [PATCH v2] vfio/pci: Introduce x-pci-class-code option Date: Wed, 28 May 2025 23:55:48 +0800 Message-ID: <20250528155548.29344-1-tomitamoeko@gmail.com> X-Mailer: git-send-email 2.47.2 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::1044; envelope-from=tomitamoeko@gmail.com; helo=mail-pj1-x1044.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: 1748447833285116600 Content-Type: text/plain; charset="utf-8" Introduce x-pci-class-code option to allow users to override PCI class code of a device, similar to the existing x-pci-vendor-id option. Only the lower 24 bits of this option are used, though a uint32 is used here for determining whether the value is valid and set by user. Additionally, to prevent exposing VGA ranges on non-VGA devices, the x-vga=3Don option requires x-pci-class-code is either unset or set to VGA controller class. This is mainly intended for IGD devices that expose themselves either as VGA controller (primary display) or Display controller (non-primary display). The UEFI GOP driver depends on the device reporting a VGA controller class code (0x030000). Signed-off-by: Tomita Moeko --- v2: * Add vdev class code check in vfio_populate_vga(). * Fix type in trace-events. Link: https://lore.kernel.org/all/20250524153102.19747-1-tomitamoeko@gmail.= com/ hw/vfio/pci.c | 25 +++++++++++++++++++++++++ hw/vfio/pci.h | 1 + hw/vfio/trace-events | 1 + 3 files changed, 27 insertions(+) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index b1250d85bf..d57cb7356e 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -2726,6 +2726,14 @@ bool vfio_populate_vga(VFIOPCIDevice *vdev, Error **= errp) return false; } =20 + /* vdev class should be either unmodified (PCI_ANY_ID), or VGA control= ler */ + if ((vdev->class_code !=3D PCI_ANY_ID) && + (vdev->class_code !=3D (PCI_CLASS_DISPLAY_VGA << 8)) && + (vdev->class_code !=3D (PCI_CLASS_NOT_DEFINED_VGA << 8))) { + error_setg(errp, "vdev is not a VGA device"); + return false; + } + if (!(reg_info->flags & VFIO_REGION_INFO_FLAG_READ) || !(reg_info->flags & VFIO_REGION_INFO_FLAG_WRITE) || reg_info->size < 0xbffff + 1) { @@ -3092,6 +3100,21 @@ static bool vfio_pci_config_setup(VFIOPCIDevice *vde= v, Error **errp) vdev->sub_device_id); } =20 + /* + * Class code is a 24-bit value at config space 0x09. Allow overriding= it + * with any 24-bit value. + */ + if (vdev->class_code !=3D PCI_ANY_ID) { + if (vdev->class_code > 0xffffff) { + error_setg(errp, "invalid PCI class code provided"); + return false; + } + /* Higher 24 bits of PCI_CLASS_REVISION are class code */ + vfio_add_emulated_long(vdev, PCI_CLASS_REVISION, + vdev->class_code << 8, ~0xff); + trace_vfio_pci_emulated_class_code(vbasedev->name, vdev->class_cod= e); + } + /* QEMU can change multi-function devices to single function, or rever= se */ vdev->emulated_config_bits[PCI_HEADER_TYPE] =3D PCI_HEADER_TYPE_MULTI_FUNCTI= ON; @@ -3489,6 +3512,8 @@ static const Property vfio_pci_dev_properties[] =3D { sub_vendor_id, PCI_ANY_ID), DEFINE_PROP_UINT32("x-pci-sub-device-id", VFIOPCIDevice, sub_device_id, PCI_ANY_ID), + DEFINE_PROP_UINT32("x-pci-class-code", VFIOPCIDevice, + class_code, PCI_ANY_ID), DEFINE_PROP_UINT32("x-igd-gms", VFIOPCIDevice, igd_gms, 0), DEFINE_PROP_UNSIGNED_NODEFAULT("x-nv-gpudirect-clique", VFIOPCIDevice, nv_gpudirect_clique, diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h index 5ce0fb916f..587eb8cc9a 100644 --- a/hw/vfio/pci.h +++ b/hw/vfio/pci.h @@ -156,6 +156,7 @@ struct VFIOPCIDevice { uint32_t device_id; uint32_t sub_vendor_id; uint32_t sub_device_id; + uint32_t class_code; uint32_t features; #define VFIO_FEATURE_ENABLE_VGA_BIT 0 #define VFIO_FEATURE_ENABLE_VGA (1 << VFIO_FEATURE_ENABLE_VGA_BIT) diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index e90ec9bff8..e8d585b49a 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -46,6 +46,7 @@ vfio_pci_emulated_vendor_id(const char *name, uint16_t va= l) "%s 0x%04x" vfio_pci_emulated_device_id(const char *name, uint16_t val) "%s 0x%04x" vfio_pci_emulated_sub_vendor_id(const char *name, uint16_t val) "%s 0x%04x" vfio_pci_emulated_sub_device_id(const char *name, uint16_t val) "%s 0x%04x" +vfio_pci_emulated_class_code(const char *name, uint32_t val) "%s 0x%06x" =20 # pci-quirks.c vfio_quirk_rom_in_denylist(const char *name, uint16_t vid, uint16_t did) "= %s %04x:%04x" --=20 2.47.2