From nobody Sat Nov 23 17:57:54 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=amd.com); dmarc=pass(p=quarantine dis=none) header.from=amd.com ARC-Seal: i=2; a=rsa-sha256; t=1730873876; cv=pass; d=zohomail.com; s=zohoarc; b=AYm8amEt+9fS4pDuepuWkd7glsH9vdxoxD4Ok17g6GQiKnrWH2y3/gGEJ4AU3jtKNV3XEFFR48AdcgGaxN5gso8RdrbIntsSSLApY2HBPMuyg/QCWFMHELfOVQMFWo2D5vKbxVKuQ0heb8lp7YG1sSc5w9L6K7o2PBncwMXQFA4= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1730873876; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Y/yFvXqsoiXHrBBPosOdrUbPPJTAhrEOBVR0YQqMyMw=; b=A/t7hLdgBsIKpDjDQ/C7PPaMzRMkZuuXkdy6xSyT/Xyb+4l5Cmpx7iZbffFi/9bEkyrSh+qPKksZBjaGXv2PlCcC9/N7LRG4zcQ/FI6vA0Iom3xFSgGDT6kun/vhEw76Sh5PQ70uC2phW8/LpbxeGjK0AKye1ZMVO6WtmqCt/Eg= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=amd.com); dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 17308738768711012.9699310720399; Tue, 5 Nov 2024 22:17:56 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.830581.1245632 (Exim 4.92) (envelope-from ) id 1t8ZLy-0004VG-Ns; Wed, 06 Nov 2024 06:17:18 +0000 Received: by outflank-mailman (output) from mailman id 830581.1245632; Wed, 06 Nov 2024 06:17:18 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1t8ZLy-0004V9-Kn; Wed, 06 Nov 2024 06:17:18 +0000 Received: by outflank-mailman (input) for mailman id 830581; Wed, 06 Nov 2024 06:17:17 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1t8ZLw-0004Ub-OE for xen-devel@lists.xenproject.org; Wed, 06 Nov 2024 06:17:17 +0000 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2062f.outbound.protection.outlook.com [2a01:111:f403:2405::62f]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id be313dbb-9c06-11ef-99a3-01e77a169b0f; Wed, 06 Nov 2024 07:17:05 +0100 (CET) Received: from SA9PR13CA0086.namprd13.prod.outlook.com (2603:10b6:806:23::31) by MN0PR12MB5859.namprd12.prod.outlook.com (2603:10b6:208:37a::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8137.19; Wed, 6 Nov 2024 06:16:59 +0000 Received: from SA2PEPF00003AE9.namprd02.prod.outlook.com (2603:10b6:806:23:cafe::66) by SA9PR13CA0086.outlook.office365.com (2603:10b6:806:23::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8137.11 via Frontend Transport; Wed, 6 Nov 2024 06:16:58 +0000 Received: from SATLEXMB04.amd.com (165.204.84.17) by SA2PEPF00003AE9.mail.protection.outlook.com (10.167.248.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8137.17 via Frontend Transport; Wed, 6 Nov 2024 06:16:58 +0000 Received: from cjq-desktop.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Wed, 6 Nov 2024 00:15:13 -0600 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: be313dbb-9c06-11ef-99a3-01e77a169b0f X-Custom-Connection: eyJyZW1vdGVpcCI6IjJhMDE6MTExOmY0MDM6MjQwNTo6NjJmIiwiaGVsbyI6Ik5BTTAyLURNMy1vYmUub3V0Ym91bmQucHJvdGVjdGlvbi5vdXRsb29rLmNvbSJ9 X-Custom-Transaction: eyJpZCI6ImJlMzEzZGJiLTljMDYtMTFlZi05OWEzLTAxZTc3YTE2OWIwZiIsInRzIjoxNzMwODczODI1Ljg3NzM1OCwic2VuZGVyIjoiamlxaWFuLmNoZW5AYW1kLmNvbSIsInJlY2lwaWVudCI6Inhlbi1kZXZlbEBsaXN0cy54ZW5wcm9qZWN0Lm9yZyJ9 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=JKw5yUWjj5LnMWGzFUgpz1chd3kSgUdMA28C801bJlWf+B+KClXD72nhtApisZYOMc3S7qoDJmR/PtiK4zYLqLliRye8A5wlFUhmUQ2zIBMb7Oot4tgKr03X+dCGGQ0QLMfQvZZpYN4rUyavWVDoTua0u/sEHM6DElbn6S+tqfAUlaaFj9g0t4n330EELz/1ik5Zy6Oic4kUK9M8UhmbtAsTBPxoB5OywtI3CeyQaa821+iXNc4jCqBA1jXHh/JY0dgCNg043PITRwSkLtX+78lfg75A+6xnZdPsGjfKI52xip2eHJMY7Q9Ua9L6J12aaY+IPtNuMnzvZ8x9WXee3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Y/yFvXqsoiXHrBBPosOdrUbPPJTAhrEOBVR0YQqMyMw=; b=b9Pvg+gOpwaBxBXAYc/DZKpbDb1BCilo0aCptIhG4wfauGaur0bUKgYImvZjOeupmnHRFW6iLgh47WyEOA/KVDHm4akd2Q3cf9iWe0hXg5nSIrJ7s4sXlTP5TEdx/WCF5K/3mgU7Ij5cuWe3AqfyB4gpmxI1S2gq1Hri+nkSLcZjBTvjKiRSM9Bdt9rqHq0dc/M1bbCICcYCDNs1frO/cscGZCQSwX7oM+uBUV/mzlTfFIaAMvQzpKaZzgfOX6g72QxHLOWkEfAJj2X+LfeCVIRqJlElCL3ugDSC1MJarqnJKzEhHIIPw2gUrA49jCBoVvAD9osGZK18v4X+RSDUow== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Y/yFvXqsoiXHrBBPosOdrUbPPJTAhrEOBVR0YQqMyMw=; b=MtUhI5x8E6qJwk2kXe9eEhH0MCWZPQoAPuTV9cgu0KVQPbTy38UV6+xQDJ40b1dhmXg40Pvlo+9rr+ghNq3dV6U1K3V1HUUYVcG3oA6V9LZFeyFW8uUseDGMnun4IE0qLPCpp9Fqauf4gv8HChbnEjACmU3Qre/KcsafR/IneLk= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C From: Jiqian Chen To: Stefano Stabellini , Anthony PERARD , Paul Durrant , "Edgar E . Iglesias" CC: , , "Stewart Hildebrand" , Jiqian Chen , Huang Rui Subject: [QEMU PATCH v10] xen/passthrough: use gsi to map pirq when dom0 is PVH Date: Wed, 6 Nov 2024 14:14:18 +0800 Message-ID: <20241106061418.3655304-1-Jiqian.Chen@amd.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA2PEPF00003AE9:EE_|MN0PR12MB5859:EE_ X-MS-Office365-Filtering-Correlation-Id: 3ea4a54c-d44b-4519-3300-08dcfe2a9ea1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|376014|1800799024|82310400026; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?Evt0MQeRA3d1BcAZs/uSvql9sDZl21PtyYkV+ncMfYP2Z5HeNQVEVARsvaLH?= =?us-ascii?Q?ZhAPQtYHn8q/K0WWuXcIMFGN5z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ascii?Q?wpyxWTngaUWhnZKPSgW9r1QPY4l0Fhwl5unHMl1awhZ98kLrI5+1pkSJt4WU?= =?us-ascii?Q?NyvFHDgQMDixp+yTy6cIZL8RzmDhG/UHrX5QBZ7pYCjMGpH+hZfHnBmEgjmO?= =?us-ascii?Q?zuxpPYeFRb079sa8tldcM7o1STWK3IlBpr1lf7LfU/MiT7nyjReGL6KSldZR?= =?us-ascii?Q?ZKAhoXd6Xva+4NQpMKUnn4CwoxXFD+6MwIH3XQXbts6uW/yULfRNMcF3UnbI?= =?us-ascii?Q?pT9irGacFC9e+jZBGQPto7bYTQlrYduttroPFmXiMqG1AuYD/XFBrarvD2uB?= =?us-ascii?Q?YAHKhrLeTbM45ONTL96+EaK4aTKaPjYpw07hT6Aqy9sKUFdj9x4IMrk1o13O?= =?us-ascii?Q?x7nvI1g94Qnk2mGgB3XZO5poUemzK4x+XWQsvFw/k+heKmvu113aYWq/aXjE?= =?us-ascii?Q?sPL2k4uNYO8OjypOorT02Rhelhrdl8CR06YoTuloZATZeCcDMQ=3D=3D?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700013)(376014)(1800799024)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Nov 2024 06:16:58.2276 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3ea4a54c-d44b-4519-3300-08dcfe2a9ea1 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SA2PEPF00003AE9.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB5859 X-ZohoMail-DKIM: pass (identity @amd.com) X-ZM-MESSAGEID: 1730873878476116600 Content-Type: text/plain; charset="utf-8" In PVH dom0, when passthrough a device to domU, QEMU code xen_pt_realize->xc_physdev_map_pirq wants to use gsi, but in current codes the gsi number is got from file /sys/bus/pci/devices//irq, that is wrong, because irq is not equal with gsi, they are in different spaces, so pirq mapping fails. To solve above problem, use new interface of Xen, xc_pcidev_get_gsi to get gsi and use xc_physdev_map_pirq_gsi to map pirq when dom0 is PVH. Signed-off-by: Jiqian Chen Signed-off-by: Huang Rui Signed-off-by: Jiqian Chen Acked-by: Anthony PERARD Reviewed-by: Stewart Hildebrand --- Hi All, This is v10 to support passthrough on Xen when dom0 is PVH. v9->v10 changes: * Check the return value of function fgets. * Added comment to explain why set the length of array 'type' to be 10. Best regards, Jiqian Chen v8->v9 changes: * Moved the definition of PCI_SBDF from /hw/xen/xen_pt.c to /include/hw/pci= /pci.h. * Renamed xen_run_qemu_on_hvm to xen_pt_need_gsi. * Renamed xen_map_pirq_for_gsi to xen_pt_map_pirq_for_gsi. * Through reading /sys/hypervisor/guest_type to get dom type instead of usi= ng xc_domain_getinfo_single. v7->v8 changes: * Since xc_physdev_gsi_from_dev was renamed to xc_pcidev_get_gsi, changed i= t. * Added xen_run_qemu_on_hvm to check if Qemu run on PV dom0, if not use xc_= physdev_map_pirq_gsi to map pirq. * Used CONFIG_XEN_CTRL_INTERFACE_VERSION to wrap the new part for compatibi= lity. * Added "#define DOMID_RUN_QEMU 0" to represent the id of domain that Qemu = run on. v6->v7 changes: * Because the function of obtaining gsi was changed on the kernel and Xen s= ide. Changed to use xc_physdev_gsi_from_dev, that requires passing in sbdf instead of irq. v5->v6 changes: * Because the function of obtaining gsi was changed on the kernel and Xen s= ide. Changed to use xc_physdev_gsi_from_irq, instead of gsi sysfs. * Since function changed, removed the Review-by of Stefano. v4->v5 changes: * Added Review-by Stefano. v3->v4 changes: * Added gsi into struct XenHostPCIDevice and used gsi number that read from= gsi sysfs if it exists, if there is no gsi sysfs, still use irq. v2->v3 changes: * Due to changes in the implementation of the second patch on kernel side(t= hat adds a new sysfs for gsi instead of a new syscall), so read gsi number from th= e sysfs of gsi. v1 and v2: We can record the relation between gsi and irq, then when userspace(qemu) w= ant to use gsi, we can do a translation. The third patch of kernel(xen/privcmd:= Add new syscall to get gsi from irq) records all the relations in acpi_register_gsi_xen_pvh= () when dom0 initialize pci devices, and provide a syscall for userspace to get the gsi = from irq. The third patch of xen(tools: Add new function to get gsi from irq) add a new f= unction xc_physdev_gsi_from_irq() to call the new syscall added on kernel side. And then userspace can use that function to get gsi. Then xc_physdev_map_pi= rq() will success. Issues we encountered: 1. failed to map pirq for gsi Problem: qemu will call xc_physdev_map_pirq() to map a passthrough device's= gsi to pirq in function xen_pt_realize(). But failed. Reason: According to the implement of xc_physdev_map_pirq(), it needs gsi i= nstead of irq, but qemu pass irq to it and treat irq as gsi, it is got from file /sys/bus/pci/devices/xxxx:xx:xx.x/irq in function xen_host_pci_device_get()= . But actually the gsi number is not equal with irq. They are in different space. --- hw/xen/xen_pt.c | 60 ++++++++++++++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 4 +++ 2 files changed, 64 insertions(+) diff --git a/hw/xen/xen_pt.c b/hw/xen/xen_pt.c index 3635d1b39f79..5e6230c4a60f 100644 --- a/hw/xen/xen_pt.c +++ b/hw/xen/xen_pt.c @@ -766,6 +766,57 @@ static void xen_pt_destroy(PCIDevice *d) { } /* init */ =20 +#if CONFIG_XEN_CTRL_INTERFACE_VERSION >=3D 42000 +static bool xen_pt_need_gsi(void) +{ + FILE *fp; + int len; + /* + * The max length of guest_type is "PVH"+'\n'+'\0', it is 5, + * so here set the length of type to be twice. + */ + char type[10]; + const char *guest_type =3D "/sys/hypervisor/guest_type"; + + fp =3D fopen(guest_type, "r"); + if (!fp) { + error_report("Cannot open %s: %s", guest_type, strerror(errno)); + return false; + } + + if (fgets(type, sizeof(type), fp)) { + len =3D strlen(type); + if (len) { + type[len - 1] =3D '\0'; + if (!strcmp(type, "PVH")) { + fclose(fp); + return true; + } + } + } + + fclose(fp); + return false; +} + +static int xen_pt_map_pirq_for_gsi(PCIDevice *d, int *pirq) +{ + int gsi; + XenPCIPassthroughState *s =3D XEN_PT_DEVICE(d); + + gsi =3D xc_pcidev_get_gsi(xen_xc, + PCI_SBDF(s->real_device.domain, + s->real_device.bus, + s->real_device.dev, + s->real_device.func)); + if (gsi >=3D 0) { + return xc_physdev_map_pirq_gsi(xen_xc, xen_domid, gsi, pirq); + } + + return gsi; +} +#endif + static void xen_pt_realize(PCIDevice *d, Error **errp) { ERRP_GUARD(); @@ -847,7 +898,16 @@ static void xen_pt_realize(PCIDevice *d, Error **errp) goto out; } =20 +#if CONFIG_XEN_CTRL_INTERFACE_VERSION >=3D 42000 + if (xen_pt_need_gsi()) { + rc =3D xen_pt_map_pirq_for_gsi(d, &pirq); + } else { + rc =3D xc_physdev_map_pirq(xen_xc, xen_domid, machine_irq, &pirq); + } +#else rc =3D xc_physdev_map_pirq(xen_xc, xen_domid, machine_irq, &pirq); +#endif + if (rc < 0) { XEN_PT_ERR(d, "Mapping machine irq %u to pirq %i failed, (err: %d)= \n", machine_irq, pirq, errno); diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index eb26cac81098..07805aa8a5f3 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -23,6 +23,10 @@ extern bool pci_available; #define PCI_SLOT_MAX 32 #define PCI_FUNC_MAX 8 =20 +#define PCI_SBDF(seg, bus, dev, func) \ + ((((uint32_t)(seg)) << 16) | \ + (PCI_BUILD_BDF(bus, PCI_DEVFN(dev, func)))) + /* Class, Vendor and Device IDs from Linux's pci_ids.h */ #include "hw/pci/pci_ids.h" =20 --=20 2.34.1