From patchwork Tue Feb 4 13:54:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mykyta Poturai X-Patchwork-Id: 13959279 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2297EC02194 for ; Tue, 4 Feb 2025 13:54:17 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.881481.1291635 (Exim 4.92) (envelope-from ) id 1tfJNQ-0007h7-Cw; Tue, 04 Feb 2025 13:54:08 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 881481.1291635; Tue, 04 Feb 2025 13:54:08 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tfJNQ-0007fP-5f; Tue, 04 Feb 2025 13:54:08 +0000 Received: by outflank-mailman (input) for mailman id 881481; Tue, 04 Feb 2025 13:54:07 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tfJNP-0007P3-7j for xen-devel@lists.xenproject.org; Tue, 04 Feb 2025 13:54:07 +0000 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on20616.outbound.protection.outlook.com [2a01:111:f403:2612::616]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 7f73919b-e2ff-11ef-a0e7-8be0dac302b0; Tue, 04 Feb 2025 14:54:05 +0100 (CET) Received: from PAVPR03MB10102.eurprd03.prod.outlook.com (2603:10a6:102:30d::12) by AS8PR03MB7494.eurprd03.prod.outlook.com (2603:10a6:20b:2e2::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8398.25; Tue, 4 Feb 2025 13:54:03 +0000 Received: from PAVPR03MB10102.eurprd03.prod.outlook.com ([fe80::35ac:8893:c31c:b971]) by PAVPR03MB10102.eurprd03.prod.outlook.com ([fe80::35ac:8893:c31c:b971%7]) with mapi id 15.20.8398.021; Tue, 4 Feb 2025 13:54:03 +0000 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: 7f73919b-e2ff-11ef-a0e7-8be0dac302b0 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=l6NAciO1rGWv2E5zOf7+2zB7TvUY3ULPV7GpH6XuJjeZ0raik34O+c3IhBjIAvEgx5SgAeC3gmfz3fKhnr+2N5DQRuIgIX4Cq0OF8Ui/9BRni3BTTMlLM6M6w0BDYTDma+ClCD9mefc/VL1Jz+IaTN0JH+hbh5FxNy8rKqcQQWUojWRCPO+tv9FepI6oU22PXS36EjRjfvE3BbWYh0yRJ4/6eilvq+OsKjBk/WpfJ1N8+Ao4/hoHbWpchrtOJLGFG75wQYsIsGuWqFwTdfJLKy2tjdHcrJs9LINfqJ9m/Nhc91p2FKPe6J8kh4DQsZdBPzwBR/Itro5bjKArH5AgIg== 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=rcDxDDzV0nXc3Oe/VjJpBckwCTeLIyuU4o2H6BVgetE=; b=PH7tPutksOc2T46sqJ0cGBOGhr8rKfgL2aiYpbPPP4swKpeURNomQOsWSiohgx8Hq9ThLoZekYWU8Jdjmpa/WY8FQoUSiEbh5lMFBOhFxci0KhCk3RDfzvJVXwNbLxp4If6X96DY9GfN2YJkrlf9uP4kHK6PA25yJBESm5e2I/a1szpuWp/5YeBzAixzOOQE4leOcgXnwy9qnbALq2SNrd3BoscATqUut2q5D+/hnka8s20fDDpmUdQ8DJrMvgSW8bGPwe/yX1e2y2+1/TlGC8BmfwUsLpO5KSQGYDHf4cYC/eXl8zHiVwzZMM9aIYo9JBzatSiTX05yks377PcMaA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=epam.com; dmarc=pass action=none header.from=epam.com; dkim=pass header.d=epam.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=epam.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=rcDxDDzV0nXc3Oe/VjJpBckwCTeLIyuU4o2H6BVgetE=; b=L5D1oCMcyCdMU1BLvgC//hSWwp3vE6P+tlRaqCmzxotbNRtl0zSCxb0enIPH4bLQ1ZQT4QWOuG0OopAvuOMDJFH0lJnmXfpQo7p5I1/rcb8GtwuVcaZamT9PBHARxjkAVYtm+t0kHnZJB+3JAyxLpyh02iBYKhNDE2MG1F4P26SlwC0X15giWCMVD8/8G1lyJmv29V7uSwzyD0EXV96djw68pf2U3FVDHOLJcjBgHBpzkvAFz/wrhIBA02tZlVQJJmwf98ViHzppOTThDuB27ecZnanedXNctcuhTC7Cup/XfX/Sa6fIuoxUFvWIajzU2Pfk244oTUhV+WpcYNqKFQ== From: Mykyta Poturai To: "xen-devel@lists.xenproject.org" CC: Stewart Hildebrand , Oleksandr Tyshchenko , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Jan Beulich , =?iso-8859-1?q?Roger_Pau_Monn=E9?= , Mykyta Poturai Subject: [PATCH v7 2/8] iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API Thread-Topic: [PATCH v7 2/8] iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API Thread-Index: AQHbdww/qCT0TCxV3UKquqIUpSdYjQ== Date: Tue, 4 Feb 2025 13:54:02 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=epam.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PAVPR03MB10102:EE_|AS8PR03MB7494:EE_ x-ms-office365-filtering-correlation-id: 1ff38ae0-7632-4e25-0bca-08dd45236231 x-ld-processed: b41b72d0-4e9f-4c26-8a69-f949f367c91d,ExtAddr x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0;ARA:13230040|376014|366016|1800799024|38070700018; x-microsoft-antispam-message-info: =?iso-8859-1?q?tYXa6m0CSuJXAJyea45NstWy/Z?= =?iso-8859-1?q?4RtypWIDejuZrg99Y195vnTmElD7Xdt97MlO0IoN8bpMWlnfLgUnRPxthco0?= =?iso-8859-1?q?9HXd2UAvHa9yCSTzAJJpGypSSYa962Ibt4MztwH91g896O/Gn8yeJxsnz08y?= =?iso-8859-1?q?cyyeWU84cUv4U1cHOq0OPRLqKmHgNVGV10O2fa+zFVLXHGHcQUEn/8hYknMJ?= =?iso-8859-1?q?eziNgGII2nEET5pEBW2/xHTRXNoIhm9mRL9L3zazbW7tVhHpE6YyA3FC02fw?= =?iso-8859-1?q?dICuK198fGuZ3kelwK5FOZlknf0Ss5wyjmBLsqRan3isFgpWyhu18PKl+NQv?= =?iso-8859-1?q?LjTtZWYbSSd+ne4Vefdry27Kk7YEzG+9DXGr1EvroQ5s9e2PC9u1FKyCvAae?= =?iso-8859-1?q?xrRX2JxuW/lyHiaKchmq+bSYh5A+W3apKlNsOZuEmTq+mSv1hDDMnSLWCGSo?= =?iso-8859-1?q?w4IU/ae0S30yJlrWPEMpQsx5Qgs42XWNigNrItNHK/aImed62eCiGXoLKuzL?= =?iso-8859-1?q?JBMUkWHpuvLr76BWMSchGi1HoL8ERHbFtk5jZnyFgG/EuKiBe91S+lMfMnhT?= =?iso-8859-1?q?Yc/vmFHd+Ss8IaFFudEng7IyhQeY//WnY7UVGCz6/JKhyNk/7hJyYVLA77Nb?= =?iso-8859-1?q?XHTvyxQlBrqzXEHlZs/78y79mbfNOHU43/iXP0I2MPeiePOjccr6j1I9S4eE?= =?iso-8859-1?q?s81ycDvMdUapEZ9kNY694irEjnbWCQG2U/K0pds7WNaxcLwzPZ+SUVKsUeX/?= =?iso-8859-1?q?VVzOxYxVzKMMlGpSWbMerGvBre3dBCYTw9rdnPZA1wUltxSTaYllN/llbRjR?= =?iso-8859-1?q?nBGbp0zSFPq5ReVYq4G1FbM8xYp/4JZJmxBGOcDl4h0tJPDQl2OtaSljR5FG?= =?iso-8859-1?q?ErEP/2QLY4qN3/cZWifJd6PRnVnBMcutqsq/8JPibm71OSszjE0niGlb09Jy?= =?iso-8859-1?q?KFBsaexsjbhNV4iTf6d+nrgWHJR3MPQ4Ct/JIxZO77QNi1EqXFm73NJcD/Mw?= =?iso-8859-1?q?wnwcDyzLv3kBw+wX6cy2/bE1PjFTaGKbDYilY4fiO9B4gMZpFrxVLx5WZZm2?= =?iso-8859-1?q?G4hb7CaBSmDpBeiJzDOVuErijd1MVgiJPZ7rJPVNwCEmTxV1Qa31ISmkcbRh?= =?iso-8859-1?q?iylU+iwvovcYQO+7PRzhcwkUsE36jHkdOFWQ2F+dE/E1C/yuyYu4yoNoeEhp?= =?iso-8859-1?q?ZenWN39Xt2V4KWiC2qmZgWzA727N3adD2GAMf+3hyPGlXMlZEdqdb+6nlG0f?= =?iso-8859-1?q?ArpVN0wV61phQ6S4dJkTDouAD8QwfNOllLPjyJfuDZ14An6PbM6px5dAPaYe?= =?iso-8859-1?q?3iG2gP574u80m0Kud3Yq7tirgboTK+hBIjbf94o3eGrBC2HRZrVHXmzjGW1b?= =?iso-8859-1?q?9SSys5AU3Er5rJjo+lKIsEW7Q/FcFBqWjDceMyimg1sSKGXyPVK7kk4yYv3O?= =?iso-8859-1?q?RpmzeJvH0laoA4Xb/1Co6Cu+4qOxvjTk4cpl+uNzWKAwkdVkErGEmXawdcS+?= =?iso-8859-1?q?Z4n68M?= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PAVPR03MB10102.eurprd03.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(366016)(1800799024)(38070700018);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?hpV2+EAZUXS1viqgozEW4uZ?= =?iso-8859-1?q?IP7KftYbwdMd4tGRUA1k4xg9bKxd/OfqVUY9jIMVeovXMcB7rGZTY4N5h7Ub?= =?iso-8859-1?q?FcTuiH92saBX03EhtuSW3lBo/L0gJJ1a9Q1b5Au8o+Asg1o30hGYi6Q/SGLi?= =?iso-8859-1?q?6OujtF6OXDWrkQkzOmd+Yj4ncrei0jq2OA7/K4zokkHa5p89UqzC5p1LrvjG?= =?iso-8859-1?q?YR6ccbJpL1Ljf9DN5Nn6mjM5Q86JsSkxBIj9cywQYUBYXq5Tcw9RX8tirNfW?= =?iso-8859-1?q?eef+15AsKv5VkryjLrZ053gSPocRbAc7sMLE4IB7rDEogNSEmIR2y7CvqFYb?= =?iso-8859-1?q?K/oE6o5VjgNYIZfChKBvN++X9BGm0zP+ly1FbPqf6p3oPil87znj9IQI36LO?= =?iso-8859-1?q?K8hk0V11iP/KDQ/1tPjTb9AzcLy6I64WgTPZ0016793qv2fzcEl/cZOTgxIL?= =?iso-8859-1?q?nseRMbhLc7TVhmvyXYKvQmylbMsfEy923mqEYiMBK3b4hg19G3+zpleD+g0W?= =?iso-8859-1?q?Xb7SggLIGKD5dKV/rdTcyuP9j50kSiRvtxdLUN9X5ZR5a8H+360CxJUB79I5?= =?iso-8859-1?q?8/RnS0vuhwekHkptjcFsVnSq7bRNAlUF/qJsTLTWJXaog5jw1yeGkwXJz0Nd?= =?iso-8859-1?q?IKZCpvtZjUBVhPWTqdzSAAj7YJEgassvcoEJsvC+bD9OGkXrGW2iLNhS/OU4?= =?iso-8859-1?q?ubWIqk6tpHLxFU7HkjtI/bLraulzZa+v4ig3o7nXFn9QA9XZ9NSJGWWEJoqR?= =?iso-8859-1?q?p+ymSSYToUDw9DqNNyhrOyMQldw9ymeqpGvpmjB8JDOwBfPpO4zAHUSl/u4c?= =?iso-8859-1?q?DlbIgEaMSu+lE6bt4ZlMB9hMPPT4ixaSxPLnpL9pqYZo6a77F0zrOsdq4DMw?= =?iso-8859-1?q?ok0WYQ916vN+g58BP5uZbq7hR+vhsapM8udkF4kgVII4hjElpIsGNIsV2UkP?= =?iso-8859-1?q?Uk99W11tQsD3eVgK8C03OKjJXgTTs8HGCQoStPGwZKQkh2GTZYEbUODMjh02?= =?iso-8859-1?q?h90JVcVX4LeElr+45bML2RKC3XlGBn37UO9DRWb3oQqfE8sqhcu4B8OvqGhJ?= =?iso-8859-1?q?PUo/4i6CGPigQilN4/XOWNAhjcNkwSK0j9RTsT0AbvO3fMr1qm8hIr7EyjkJ?= =?iso-8859-1?q?6abmQQ21ZG/NIsIGqExd7tGMl+Co+Hf5hni1u03BqopUgVwTf1J3WE1XNQms?= =?iso-8859-1?q?+BYlIi+tITS0yLnnFYSkyfLYqAw99zOP5y9A/WtqVkurl8XYH3CtjQqTaply?= =?iso-8859-1?q?miU4h07HBeld2RQhkPKZ/vxnsTjntYVDxR+8LBfwDMAb+nUWGDlvAdjY6NJV?= =?iso-8859-1?q?YXqFRJUUEsadmF96WC23i486BXn7sWTdKwQ2ir7ZDaUdmf7zW1OENomAOG1+?= =?iso-8859-1?q?OfXx77zcBmdqcLQccOgFCYATWdkG9oyYIHcsolowKGx2bGYDFRWOS+M002yy?= =?iso-8859-1?q?sqIxChqu03sFDVaKPhFTu2uAVbrIwBpiplTeqJI3fH59cTB0qFwMnk9Ii5JH?= =?iso-8859-1?q?SieDETtLNmYG+is67vIjEvVLXvw4NSTSwfQnAD964BS+ApcJXXsWCQ42jYaO?= =?iso-8859-1?q?a1a/5gTQu7xiECtYON0SC9ovtpSoqY91NrQMH/4h5EXdDNPFxUMP2DzpBa0j?= =?iso-8859-1?q?xnqqg9KHGB+AhGr4bQFYXCIuAFwvKbE3MW4RP/w=3D=3D?= MIME-Version: 1.0 X-OriginatorOrg: epam.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: PAVPR03MB10102.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1ff38ae0-7632-4e25-0bca-08dd45236231 X-MS-Exchange-CrossTenant-originalarrivaltime: 04 Feb 2025 13:54:02.9629 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b41b72d0-4e9f-4c26-8a69-f949f367c91d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: S7EyCONp79Y7SmNSgoEIfPKx155Pu8szRrHkpXqjEwwb+D2VBa6LLA2wVJ1kqVUb6sv0J5kfD30xOmTu7cGOww== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB7494 From: Oleksandr Tyshchenko The main purpose of this patch is to add a way to register PCI device (which is behind the IOMMU) using the generic PCI-IOMMU DT bindings [1] before assigning that device to a domain. This behaves similarly to the existing iommu_add_dt_device API, except it handles PCI devices, and it is to be invoked from the add_device hook in the SMMU driver. The function dt_map_id to translate an ID through a downstream mapping (which is also suitable for mapping Requester ID) was borrowed from Linux (v5.10-rc6) and updated according to the Xen code base. [1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt Signed-off-by: Oleksandr Tyshchenko Signed-off-by: Stewart Hildebrand Signed-off-by: Mykyta Poturai --- Regarding pci_for_each_dma_alias question: getting host bridge node directly seems like a simpler solution with the same result. AFAIU with pci_for_each_dma_alias in linux we would arrive to the host brige node anyway, but also try to call dt_map_id for each device along the way. I am not sure why exactly it is done this way in linux, as according to the pci-iommu.txt, iommu-map node can only be present in the PCI root. v6->v7: * put iommu_add_pci_sideband_ids under ifdef * remove ifdef CONFIG_APCI * style: add newline for symmetry v5->v6: * pass ops to iommu_dt_xlate() v4->v5: * style: add newlines after variable declarations and before return in iommu.h * drop device_is_protected() check in iommu_add_dt_pci_sideband_ids() * rebase on top of ("dynamic node programming using overlay dtbo") series * fix typo in commit message * remove #ifdef around dt_map_id() prototype * move dt_map_id() to xen/common/device_tree.c * add function name in error prints * use dprintk for debug prints * use GENMASK and #include * fix typo in comment * remove unnecessary (int) cast in loop condition * assign *id_out and return success in case of no translation in dt_map_id() * don't initialize local variable unnecessarily * return error in case of ACPI/no DT in iommu_add_{dt_}pci_sideband_ids() v3->v4: * wrap #include and if ( acpi_disabled ) in #ifdef CONFIG_ACPI * fix Michal's remarks about style, parenthesis, and print formats * remove !ops->dt_xlate check since it is already in iommu_dt_xlate helper * rename s/iommu_dt_pci_map_id/dt_map_id/ because it is generic, not specific to iommu * update commit description v2->v3: * new patch title (was: iommu/arm: Introduce iommu_add_dt_pci_device API) * renamed function from: iommu_add_dt_pci_device to: iommu_add_dt_pci_sideband_ids * removed stale ops->add_device check * iommu.h: add empty stub iommu_add_dt_pci_sideband_ids for !HAS_DEVICE_TREE * iommu.h: add iommu_add_pci_sideband_ids helper * iommu.h: don't wrap prototype in #ifdef CONFIG_HAS_PCI * s/iommu_fwspec_free(pci_to_dev(pdev))/iommu_fwspec_free(dev)/ v1->v2: * remove extra devfn parameter since pdev fully describes the device * remove ops->add_device() call from iommu_add_dt_pci_device(). Instead, rely on the existing iommu call in iommu_add_device(). * move the ops->add_device and ops->dt_xlate checks earlier downstream->v1: * rebase * add const qualifier to struct dt_device_node *np arg in dt_map_id() * add const qualifier to struct dt_device_node *np declaration in iommu_add_pci_device() * use stdint.h types instead of u8/u32/etc... * rename functions: s/dt_iommu_xlate/iommu_dt_xlate/ s/dt_map_id/iommu_dt_pci_map_id/ s/iommu_add_pci_device/iommu_add_dt_pci_device/ * add device_is_protected check in iommu_add_dt_pci_device * wrap prototypes in CONFIG_HAS_PCI (cherry picked from commit 734e3bf6ee77e7947667ab8fa96c25b349c2e1da from the downstream branch poc/pci-passthrough from https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc.git) --- xen/common/device-tree/device-tree.c | 91 +++++++++++++++++++++++++++ xen/drivers/passthrough/device_tree.c | 42 +++++++++++++ xen/include/xen/device_tree.h | 23 +++++++ xen/include/xen/iommu.h | 28 ++++++++- 4 files changed, 183 insertions(+), 1 deletion(-) diff --git a/xen/common/device-tree/device-tree.c b/xen/common/device-tree/device-tree.c index d0528c5825..3de7858df6 100644 --- a/xen/common/device-tree/device-tree.c +++ b/xen/common/device-tree/device-tree.c @@ -10,6 +10,7 @@ * published by the Free Software Foundation. */ +#include #include #include #include @@ -2243,6 +2244,96 @@ int dt_get_pci_domain_nr(struct dt_device_node *node) return (u16)domain; } +int dt_map_id(const struct dt_device_node *np, uint32_t id, + const char *map_name, const char *map_mask_name, + struct dt_device_node **target, uint32_t *id_out) +{ + uint32_t map_mask, masked_id, map_len; + const __be32 *map = NULL; + + if ( !np || !map_name || (!target && !id_out) ) + return -EINVAL; + + map = dt_get_property(np, map_name, &map_len); + if ( !map ) + { + if ( target ) + return -ENODEV; + + /* Otherwise, no map implies no translation */ + *id_out = id; + return 0; + } + + if ( !map_len || (map_len % (4 * sizeof(*map))) ) + { + printk(XENLOG_ERR "%s(): %s: Error: Bad %s length: %u\n", __func__, + np->full_name, map_name, map_len); + return -EINVAL; + } + + /* The default is to select all bits. */ + map_mask = GENMASK(31, 0); + + /* + * Can be overridden by "{iommu,msi}-map-mask" property. + * If dt_property_read_u32() fails, the default is used. + */ + if ( map_mask_name ) + dt_property_read_u32(np, map_mask_name, &map_mask); + + masked_id = map_mask & id; + for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4 ) + { + struct dt_device_node *phandle_node; + uint32_t id_base = be32_to_cpup(map + 0); + uint32_t phandle = be32_to_cpup(map + 1); + uint32_t out_base = be32_to_cpup(map + 2); + uint32_t id_len = be32_to_cpup(map + 3); + + if ( id_base & ~map_mask ) + { + printk(XENLOG_ERR "%s(): %s: Invalid %s translation - %s-mask (0x%"PRIx32") ignores id-base (0x%"PRIx32")\n", + __func__, np->full_name, map_name, map_name, map_mask, + id_base); + return -EFAULT; + } + + if ( (masked_id < id_base) || (masked_id >= (id_base + id_len)) ) + continue; + + phandle_node = dt_find_node_by_phandle(phandle); + if ( !phandle_node ) + return -ENODEV; + + if ( target ) + { + if ( !*target ) + *target = phandle_node; + + if ( *target != phandle_node ) + continue; + } + + if ( id_out ) + *id_out = masked_id - id_base + out_base; + + dprintk(XENLOG_DEBUG, "%s: %s, using mask %08"PRIx32", id-base: %08"PRIx32", out-base: %08"PRIx32", length: %08"PRIx32", id: %08"PRIx32" -> %08"PRIx32"\n", + np->full_name, map_name, map_mask, id_base, out_base, id_len, id, + masked_id - id_base + out_base); + return 0; + } + + dprintk(XENLOG_DEBUG, "%s: no %s translation for id 0x%"PRIx32" on %s\n", + np->full_name, map_name, id, + (target && *target) ? (*target)->full_name : NULL); + + if ( id_out ) + *id_out = id; + + return 0; +} + /* * Local variables: * mode: C diff --git a/xen/drivers/passthrough/device_tree.c b/xen/drivers/passthrough/device_tree.c index 4c35281d98..edbd3f17ad 100644 --- a/xen/drivers/passthrough/device_tree.c +++ b/xen/drivers/passthrough/device_tree.c @@ -161,6 +161,48 @@ static int iommu_dt_xlate(struct device *dev, return ops->dt_xlate(dev, iommu_spec); } +#ifdef CONFIG_HAS_PCI +int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev) +{ + const struct iommu_ops *ops = iommu_get_ops(); + struct dt_phandle_args iommu_spec = { .args_count = 1 }; + struct device *dev = pci_to_dev(pdev); + const struct dt_device_node *np; + int rc; + + if ( !iommu_enabled ) + return NO_IOMMU; + + if ( !ops ) + return -EINVAL; + + if ( dev_iommu_fwspec_get(dev) ) + return -EEXIST; + + np = pci_find_host_bridge_node(pdev); + if ( !np ) + return -ENODEV; + + /* + * According to the Documentation/devicetree/bindings/pci/pci-iommu.txt + * from Linux. + */ + rc = dt_map_id(np, PCI_BDF(pdev->bus, pdev->devfn), "iommu-map", + "iommu-map-mask", &iommu_spec.np, iommu_spec.args); + if ( rc ) + return (rc == -ENODEV) ? NO_IOMMU : rc; + + rc = iommu_dt_xlate(dev, &iommu_spec, ops); + if ( rc < 0 ) + { + iommu_fwspec_free(dev); + return -EINVAL; + } + + return rc; +} +#endif /* CONFIG_HAS_PCI */ + int iommu_remove_dt_device(struct dt_device_node *np) { const struct iommu_ops *ops = iommu_get_ops(); diff --git a/xen/include/xen/device_tree.h b/xen/include/xen/device_tree.h index 5ff763bb80..9254204af6 100644 --- a/xen/include/xen/device_tree.h +++ b/xen/include/xen/device_tree.h @@ -946,6 +946,29 @@ int dt_count_phandle_with_args(const struct dt_device_node *np, */ int dt_get_pci_domain_nr(struct dt_device_node *node); +/** + * dt_map_id - Translate an ID through a downstream mapping. + * @np: root complex device node. + * @id: device ID to map. + * @map_name: property name of the map to use. + * @map_mask_name: optional property name of the mask to use. + * @target: optional pointer to a target device node. + * @id_out: optional pointer to receive the translated ID. + * + * Given a device ID, look up the appropriate implementation-defined + * platform ID and/or the target device which receives transactions on that + * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or + * @id_out may be NULL if only the other is required. If @target points to + * a non-NULL device node pointer, only entries targeting that node will be + * matched; if it points to a NULL value, it will receive the device node of + * the first matching target phandle, with a reference held. + * + * Return: 0 on success or a standard error code on failure. + */ +int dt_map_id(const struct dt_device_node *np, uint32_t id, + const char *map_name, const char *map_mask_name, + struct dt_device_node **target, uint32_t *id_out); + struct dt_device_node *dt_find_node_by_phandle(dt_phandle handle); #ifdef CONFIG_DEVICE_TREE_DEBUG diff --git a/xen/include/xen/iommu.h b/xen/include/xen/iommu.h index c3b8df9815..d2b9f04f81 100644 --- a/xen/include/xen/iommu.h +++ b/xen/include/xen/iommu.h @@ -27,6 +27,7 @@ #include #include #include +#include #include TYPE_SAFE(uint64_t, dfn); @@ -215,7 +216,8 @@ int iommu_dt_domain_init(struct domain *d); int iommu_release_dt_devices(struct domain *d); /* - * Helper to add master device to the IOMMU using generic IOMMU DT bindings. + * Helpers to add master device to the IOMMU using generic (PCI-)IOMMU + * DT bindings. * * Return values: * 0 : device is protected by an IOMMU @@ -224,6 +226,7 @@ int iommu_release_dt_devices(struct domain *d); * (IOMMU is not enabled/present or device is not connected to it). */ int iommu_add_dt_device(struct dt_device_node *np); +int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev); int iommu_do_dt_domctl(struct xen_domctl *domctl, struct domain *d, XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl); @@ -241,8 +244,31 @@ int iommu_remove_dt_device(struct dt_device_node *np); /* Error code for reporting no IOMMU is present */ #define NO_IOMMU 1 +#else /* !HAS_DEVICE_TREE */ +static inline int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev) +{ + return -ENOSYS; +} + #endif /* HAS_DEVICE_TREE */ +#ifdef CONFIG_HAS_PCI +static inline int iommu_add_pci_sideband_ids(struct pci_dev *pdev) +{ + int ret = -ENOSYS; + + if ( acpi_disabled ) + ret = iommu_add_dt_pci_sideband_ids(pdev); + + return ret; +} +#else /* !HAS_PCI */ +static inline int iommu_add_pci_sideband_ids(struct pci_dev *pdev) +{ + return -ENOSYS; +} +#endif + struct page_info; /*