From nobody Tue Oct 22 22:18:06 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=1729060165; cv=pass; d=zohomail.com; s=zohoarc; b=RqgqUHxBIumgCz+bAJTFk3BRlGYeGizP+dBkLiXMLg/8npu4agb5JZ6yVfLemPkjBjPcQaHk37+3yhLyDj3L1PMFrGPebHKEVyJW6atIlUfcbMeRuWuLY0QLrYcX1221vegIWGAwM4oP6eidLiaIbZ+EGRYy2OEWXpSInmuxv9s= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1729060165; 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=+Qa+/Vrr5vYV8N9Hqm6E9aPfl5zi0iD1R8OkMk0hb40=; b=LaRajWSUPhspq9nYtnoczMuHFyXPwJ9swJDUlTJDyuXKgZs2fFezIaxfskoeC2wTH1N7nDeBO3OsUsNEijSN5huIgUOOBFKDAg48hRMNjtsBSKGBVYWEt1DqdhedDzmqPNjfKVd9cwVSQyuiiUpMw7aifsDgX6wCOe1qDBF8WBg= 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 1729060165396388.77994839714586; Tue, 15 Oct 2024 23:29:25 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.819552.1232924 (Exim 4.92) (envelope-from ) id 1t0xWh-0006nT-30; Wed, 16 Oct 2024 06:28:55 +0000 Received: by outflank-mailman (output) from mailman id 819552.1232924; Wed, 16 Oct 2024 06:28:55 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1t0xWg-0006nM-W4; Wed, 16 Oct 2024 06:28:54 +0000 Received: by outflank-mailman (input) for mailman id 819552; Wed, 16 Oct 2024 06:28:53 +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 1t0xWf-0006nG-LQ for xen-devel@lists.xenproject.org; Wed, 16 Oct 2024 06:28:53 +0000 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on20610.outbound.protection.outlook.com [2a01:111:f403:2414::610]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id e8194e4d-8b87-11ef-99a2-01e77a169b0f; Wed, 16 Oct 2024 08:28:51 +0200 (CEST) Received: from SJ0PR03CA0090.namprd03.prod.outlook.com (2603:10b6:a03:331::35) by PH0PR12MB8797.namprd12.prod.outlook.com (2603:10b6:510:28d::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.19; Wed, 16 Oct 2024 06:28:43 +0000 Received: from SJ5PEPF00000207.namprd05.prod.outlook.com (2603:10b6:a03:331:cafe::68) by SJ0PR03CA0090.outlook.office365.com (2603:10b6:a03:331::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.18 via Frontend Transport; Wed, 16 Oct 2024 06:28:43 +0000 Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ5PEPF00000207.mail.protection.outlook.com (10.167.244.40) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8069.17 via Frontend Transport; Wed, 16 Oct 2024 06:28:43 +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, 16 Oct 2024 01:28:40 -0500 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: e8194e4d-8b87-11ef-99a2-01e77a169b0f ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=qO7E1qqV6uQ0YAsF7C5wMyvq7qyhuN7qBOL0mYExYZPSV4ZdCOT05yXiI0MgmhHwVcITACChkUeYA/kJKuM0bPHS8uOT4GFqh9q55oqM0kk/x6bKGy58ldsSyZp/yWzp2cJKz51C7vLh/gFnvUu67s1plDOsW4R7+2LftafJCqOu4A90xb5Yk9C/xeM4OWRAG8eAPwRDKTm+6TDv7QS/HHBzKA7JQCTdYhZaz28pA+N8HeoUc8SLuosZCi0gMXerrDheBu2FvYUMSDoYw31+K/B7VAFVRSxPiBW0L5csHyh69Ygw7XwlrFTlULaOwXsa9pI2NnI23xv5qyxevAktYw== 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=+Qa+/Vrr5vYV8N9Hqm6E9aPfl5zi0iD1R8OkMk0hb40=; b=ofaIzaWoXsprLXAqBAIFrVie0Evko9XiuASyuxwzsHB9wH51A7rGXrs7dqAiXvgeABo/QjXqrYZ3pYinW56UE0CVv2k5ktUtlcgDdvBlIxzTB4j1LwXfea7QkrDSfewpUr3SLaO7eI1pvW1Y4paEUi2YzNyN2LHHpaAMjTSe5HPleEynUkRy8ycqN6Z/d196+Pns1cnngfAk0ytfnewA8I0Im9U8ig9RJ2Dbcadt6YcMI+9mrOpTOzS7jb8Gudz5fMVOmwJRFUpsZl7v9FC//9ZTHHCZvw4Wg3aYLY4c+Ywd2q4UIDfXtZjQC+xqLUR4BapbHH7sITPB2qnHyq+zYg== 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=+Qa+/Vrr5vYV8N9Hqm6E9aPfl5zi0iD1R8OkMk0hb40=; b=XG1Ly6/2nCr09Ej5oKJcsJG3d6P2J3vq/udR0dCJL7+zYK2jl60NQ1KY89Gxq3j1dTSn77ootzmTBcLPZhYwsDfCDL23f5pt8P+lzFbYRnM+fM4Ay7ZO5Ipkc+anfjPqBB0A0g8QP45RkHKp/MnF5w6gVHeOCrOpaHRG57g7QJI= 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: , , Huang Rui , Jiqian Chen , Huang Rui Subject: [QEMU PATCH v8] xen/passthrough: use gsi to map pirq when dom0 is PVH Date: Wed, 16 Oct 2024 14:28:27 +0800 Message-ID: <20241016062827.2301004-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: SJ5PEPF00000207:EE_|PH0PR12MB8797:EE_ X-MS-Office365-Filtering-Correlation-Id: 9348a513-3acd-4696-da8e-08dcedabc815 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|36860700013|376014|82310400026; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?PE3p22gpaIpl7KxYAt6w7HoXWIp/AsCVFvkKFmHG4bXdjeQ6OMSi4ze2G0wJ?= =?us-ascii?Q?zCg/BNz9KrG2CIXLFWunrCo9EQza9dI/am5ctwk88WbJ/Y5kyzQpPJUNhhUB?= =?us-ascii?Q?eiPkoP31zW02CosRHdApmri3aS1vd/dBhH3EuEuq5SC1P8njaQiKl9rSFfXC?= =?us-ascii?Q?grWIo1bAzo1nYxx9ZtIPY0+uzc07jZhGzbaEQoazkUWSFY3oE1FxLc4RxLay?= =?us-ascii?Q?d488+JFfG4hEO098a6ccmCrGI0HacnRW7tUtLX2AI87gf0ciRM5UA+EeY+Kq?= =?us-ascii?Q?O6ggfG9xW+4pz61pnBHZ/08zzOVu3LEJq7qHc030e0qXaiRsieSOnHRXCOiO?= =?us-ascii?Q?4OztvwaaSXH7nk1E/rR/MrhK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?us-ascii?Q?rzdhla4iMLaBXYrxqLTzcVjYijYcemT/aA7onBZwDChiiBp9ZGd/R2igp9Ex?= =?us-ascii?Q?7ym7FZi/p7u63iRmTEaT7YEjYhbU4eZamgro81FRSF3VSMsZnCtA3QuOE2GX?= =?us-ascii?Q?4LQxXRd8jJASkqs1ONk1dHJ5pnKJ?= 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)(1800799024)(36860700013)(376014)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Oct 2024 06:28:43.0427 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9348a513-3acd-4696-da8e-08dcedabc815 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: SJ5PEPF00000207.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB8797 X-ZohoMail-DKIM: pass (identity @amd.com) X-ZM-MESSAGEID: 1729060167340116600 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 --- Hi All, This is v8 to support passthrough on Xen when dom0 is PVH. v7->v8 change: * 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. Best regards, Jiqian Chen 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 | 44 ++++++++++++++++++++++++++++++++++++++++++++ hw/xen/xen_pt.h | 1 + 2 files changed, 45 insertions(+) diff --git a/hw/xen/xen_pt.c b/hw/xen/xen_pt.c index 3635d1b39f79..7f8139d20915 100644 --- a/hw/xen/xen_pt.c +++ b/hw/xen/xen_pt.c @@ -766,6 +766,41 @@ static void xen_pt_destroy(PCIDevice *d) { } /* init */ =20 +#define PCI_SBDF(seg, bus, dev, func) \ + ((((uint32_t)(seg)) << 16) | \ + (PCI_BUILD_BDF(bus, PCI_DEVFN(dev, func)))) + +#if CONFIG_XEN_CTRL_INTERFACE_VERSION >=3D 42000 +static bool xen_run_qemu_on_hvm(void) +{ + xc_domaininfo_t info; + + if (!xc_domain_getinfo_single(xen_xc, DOMID_RUN_QEMU, &info) && + (info.flags & XEN_DOMINF_hvm_guest)) { + return true; + } + + return false; +} + +static int xen_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 +882,16 @@ static void xen_pt_realize(PCIDevice *d, Error **errp) goto out; } =20 +#if CONFIG_XEN_CTRL_INTERFACE_VERSION >=3D 42000 + if (xen_run_qemu_on_hvm()) { + rc =3D xen_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/hw/xen/xen_pt.h b/hw/xen/xen_pt.h index 095a0f0365d4..a08b45b7fbae 100644 --- a/hw/xen/xen_pt.h +++ b/hw/xen/xen_pt.h @@ -36,6 +36,7 @@ void xen_pt_log(const PCIDevice *d, const char *f, ...) G= _GNUC_PRINTF(2, 3); # define XEN_PT_LOG_CONFIG(d, addr, val, len) #endif =20 +#define DOMID_RUN_QEMU 0 =20 /* Helper */ #define XEN_PFN(x) ((x) >> XC_PAGE_SHIFT) --=20 2.34.1