From patchwork Fri Mar 14 13:34:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mykyta Poturai X-Patchwork-Id: 14016910 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 513DDC35FF9 for ; Fri, 14 Mar 2025 13:35:11 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.914589.1320324 (Exim 4.92) (envelope-from ) id 1tt5Bi-00043c-Ke; Fri, 14 Mar 2025 13:34:58 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 914589.1320324; Fri, 14 Mar 2025 13:34:58 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tt5Bi-00042z-EV; Fri, 14 Mar 2025 13:34:58 +0000 Received: by outflank-mailman (input) for mailman id 914589; Fri, 14 Mar 2025 13:34:57 +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 1tt5Bg-0003qF-T1 for xen-devel@lists.xenproject.org; Fri, 14 Mar 2025 13:34:57 +0000 Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazlp170110001.outbound.protection.outlook.com [2a01:111:f403:c200::1]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 1d65c6c7-00d9-11f0-9ab9-95dc52dad729; Fri, 14 Mar 2025 14:34:55 +0100 (CET) Received: from PAVPR03MB10102.eurprd03.prod.outlook.com (2603:10a6:102:30d::12) by PAXPR03MB7950.eurprd03.prod.outlook.com (2603:10a6:102:219::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8534.28; Fri, 14 Mar 2025 13:34:50 +0000 Received: from PAVPR03MB10102.eurprd03.prod.outlook.com ([fe80::35ac:8893:c31c:b971]) by PAVPR03MB10102.eurprd03.prod.outlook.com ([fe80::35ac:8893:c31c:b971%3]) with mapi id 15.20.8511.026; Fri, 14 Mar 2025 13:34:50 +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: 1d65c6c7-00d9-11f0-9ab9-95dc52dad729 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=A1FSVEcjaVtisi0GGaIOqLmM+KnFLh7DlBWt/TNRjj0DwsDG3L5wDOcrs0wfBY1iftutm/9ZIFVrZmHRJ/fq5/FqAZb4N9cSofAZyCvepUsl2J7S0AWXwcMmxObfE9j7+4qRFzfOUSjsR/UqakoOfX2Q8rXZBVp2bGH1XmQbCYtpgmfcIs4uxt5mTQcRTxuzDvUWFOMlO82M6eZz1Gap1GVZjx4fEeg6EyaZvmAIMeJl4EgmKYrF/pUuyNkTP+euJ+nO5FD2uaBUNIFlzEXs0zvG/780rmJzngzm8/KZcVEwiLg2dxLrDiKChdEAcYT7PIAao1h0XoTYUU6BOzy3gQ== 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=PUsxee2lGto+cKUU/E4hlWyJiIj2nzbj+aTUKYVDphk=; b=CyhZtb8RHEubR5I76lAApHgOtckl5AEO1q0i33pnQYxu3TCSiAYmJ3ajvqruAc8F5IMjxFGUu8AE9HaTm5bZQluYKygd828rugeNr5TwM0EUBcTwV9WWPN2wrEnt3AsgoBT4dsSwsfk+3eYJ1Dodp0THmwd7iNHR+DkGlpPDCmzU8ukkItd4uYnXanudMKHMGZyjKv0R9tphjoNhQAI54YsF3kMOiSWCGlm1JibcESzTi1E7ssMKdfeskU3B05H39DoT7xYPP+tW2jy10A7hVyW+As2LCoJI71mDTtA1fK6w7exhkGITnKl+WqIgSA7SIdqO+ohE/FgugXRieeYyNg== 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=PUsxee2lGto+cKUU/E4hlWyJiIj2nzbj+aTUKYVDphk=; b=uLUoa+RBL4ujinaNIScyxaVb7OsbTTomr3Gfw8ndm9Pw+Ow6uTNTksU8DX6YjksQl1sOkyZ6P5yY8Fg5I2mqAeYs1JBqBaq2tNrILEr85WCoy8t6PAWGO9rutl9JJT1s64375brhI+8OqzfiCrSZBtQPO647XAiCeWkN/taOIoneEyUnYuxh/D253Lu2GnQuxsfpOhoPKC1+aGaBiRjhqtX0CESNmsQYMipWtkVFJTJMMG0WBCQsnUDQ7S+/1PLliRGCiXeqHAeSPqFuF4rrY1wcCHZv3fgM4ihKRdfaFeV9eCqsfvtnbGNDFh/NAuwfy3daRe1NwR109taXx7UbjQ== From: Mykyta Poturai To: "xen-devel@lists.xenproject.org" CC: Oleksandr Tyshchenko , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Jan Beulich , =?iso-8859-1?q?Roger_Pau_Monn=E9?= , Stewart Hildebrand , Mykyta Poturai Subject: [PATCH v9 2/8] iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API Thread-Topic: [PATCH v9 2/8] iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API Thread-Index: AQHblOXcI08sNdwZJ0ah+pJJkNPOtA== Date: Fri, 14 Mar 2025 13:34:50 +0000 Message-ID: <95d779f00127defa37f3c5056c762ea0da47fc53.1741958647.git.mykyta_poturai@epam.com> 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_|PAXPR03MB7950:EE_ x-ms-office365-filtering-correlation-id: 9959f410-01e1-43d2-7612-08dd62fcff08 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?ipgzzjd8il8SYMCvX8cSQiWaiz?= =?iso-8859-1?q?O9PMda5qrn16cCfNKP4wryiCLU7MPItTqYuYtr9nH1jAXUxDCp+L/N9Bx4cK?= =?iso-8859-1?q?0GaPmq3BAWTwxEHyCvKcbAXlNYCfu6jKdiBJ1HBLadDn2Q9ibyu2ugyWr0Rr?= =?iso-8859-1?q?5hTXariHumrjjlNGlQZkkJexkMnXeV6Z/izmNkf8WzKUEDBLMkUfzZWNt4RN?= =?iso-8859-1?q?V1dCDrjH/0FPqNekkCU0alZTR9VPkdRhZhrAXJ7ktOAP6MS97vnsSXy8wFf2?= =?iso-8859-1?q?diTcAyWqQygY5q2ww/yLsrUANrtfR8FvxDomrcBusqrSMz3uDp2pEJxxVM7x?= =?iso-8859-1?q?FL6xtpV062jeHXhzJYqSGE7EtTaq4CmbIPC7wku7FnSgK/nUbzEJpkSUCUDc?= =?iso-8859-1?q?zutppHgNa0eU0M8DF/wYin2IyhvEntjIUFozrzrK5vAKbAg1v8E+rHZ1su16?= =?iso-8859-1?q?FfQv3PV0XM8AMPdkLnSS0cIe5bdhiOSNs7LB/wPa3sRRlD3zuWBBFKBdHwN+?= =?iso-8859-1?q?fLENpprkOY7wdy6HSI5SZF78yp9vInrsHPg9s7wwVqZqLJTi5tdSWYE0GyP3?= =?iso-8859-1?q?3BN0qbb0JOfPD1aIOKVBWWdoaVoMRR02F0oVRKS81QxM/7S3VpMiBAKQ30eE?= =?iso-8859-1?q?GzJp1KHKamzCuSAd9P3NVmN7za7ijW6pKr1AIa0mR71R/vLDNC+ZqGYHtEaB?= =?iso-8859-1?q?CPPj6W7XoLpVkQzADgquo5HvdX1NggBbNxRugWC2nTAgx7gYfoIKBZq/OpkD?= =?iso-8859-1?q?gBbHA8V3eTlkV9NshufmGhSRQivf0tulmIZV2CBeIq9oMBrvTDVyMbRMeK+C?= =?iso-8859-1?q?Qu36E+DlnYa/7AEmabjCzTG7uCGBfXs0GykzCWMKCkHuiZ3rOkNl5cz1/1t+?= =?iso-8859-1?q?IjMVIyB2GXP7+Gk9ZISJStR0xHxiMZFnnOEd/Kt0uXSZ9DqxQPdcz3ShAw7v?= =?iso-8859-1?q?tbEyCT9WaXlwVyKUrUGwBZBs0XnH5vubnpd6b3sJu4y/aqMuOlv0LOpeLVQq?= =?iso-8859-1?q?yEKcc/5gCAvNoCKBe5f7alxGCEMRyJSj/QvnHwDSXntr2xALzw7HgDgcUN+F?= =?iso-8859-1?q?x/dyzV8d5yZbK9ORK72CXjf798t+8wvLkrm0Gg06mvMqWQNLGvz0qFT7CU0Z?= =?iso-8859-1?q?xqhXiJww/go1fsYqin4l8b+hCISVDRASd28nsCXgj9WYiQkloRtAa1hepBFV?= =?iso-8859-1?q?OENtv+FW79QnMf5JLkIGdHt4h3Kgho9pnbX/1L8sK3V+TTo3lb3f1vVWJkfb?= =?iso-8859-1?q?RXukuqeXAGRkKEe3GCWMpPdGGNzZaUnOrPv1UL3SQlS7Xdiw9O5TrHMmf1Rb?= =?iso-8859-1?q?NzrCevf1evSAOUz7F5Ay0+AncpyLe+KRLnmX6d1xqYOPIkPvCsSrCaDC/NxM?= =?iso-8859-1?q?J1Eq98PMh4TlLp3jIDPAt2VJNIOhCHe/bdQuicLPbV2rBmlMQf7hqEnKDHhi?= =?iso-8859-1?q?n9gDqL/jyvvaykUWRYilXgKDDF2mjy8PMlNtxtXRgTyIO7DTZnBmbw/1alTL?= =?iso-8859-1?q?SIk5TZ?= 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?SngHlYyFkqnZdoqILSU5hWX?= =?iso-8859-1?q?M1VkF1ysgBL9i+oka1WxH6/Wmox6WL3n69AL3yqgKnBIkkqvLGMjxtbRfRbz?= =?iso-8859-1?q?7bPrUCxo6evK/rFkpPS5ybRmomZ5BrnESURx7Vuf1t49+YlbOqm4RdsDAlc7?= =?iso-8859-1?q?nAZeKeDx6WXrMbw9RgxfNIy/Q/o/KyYQ4gDBVAHCANeuyx7psg8GnE1+mJJ6?= =?iso-8859-1?q?jNjlh4Zlk9goVL77MSeTLbRSLkMM0TM13m+JN28xet5C65kQIHzE7d/cSEyx?= =?iso-8859-1?q?tHdIDig3Ex8RlXyf4s798eZxaOv8NbDcrRjZDtQ/pqcb/C1Nh6FRNZn2YduF?= =?iso-8859-1?q?sbb6RS/NyH/lYMvSOKKAMdPhYUfiQDXo9AJnVF4sQYN7pu6BXJFg8eKVA44Q?= =?iso-8859-1?q?M3+i9L4nuuTtbDdLN+/T06kCziNo+19jwYDzjBPk8idof6LNNDBLLG7fjzzA?= =?iso-8859-1?q?envyOgAx4RfwHkHq1x9i5VozTimszI9nLS2HjUJUTPENzTfTmTqxWil3ODZg?= =?iso-8859-1?q?KEimxNyWLxWkvLxB7OMiX8siVBT2hMD8zl3MGu1auNqH28pbT5q3gYOcT00U?= =?iso-8859-1?q?YagJykzDsxbZ7petiwxBdyARYln6QfNDZax477i8fXTxjxlw8tRh5gyf2NV4?= =?iso-8859-1?q?nnfuo4wqaFl4ZPQsuVF+3hMYjP/MwI2pqKcfwrqsAUVOmy/qIg2VKwVBe2a8?= =?iso-8859-1?q?xpnTiSJpkyy2SLIkbFUUgBUav027FsVRcobGa7uHc56LHTrgh05Ht0aWS7jx?= =?iso-8859-1?q?rrX63HAlcLFGuF+xS8bgh4qRdcevINLjAj0qWIV0aAN3pyytsfszoHdM9ndf?= =?iso-8859-1?q?bWLqTqBPu9ELngf45hvI/i2A/Sts7wqeSUDAUVbrLKuQ+K7HJ2Yf/HnBIYIg?= =?iso-8859-1?q?Yc6ZRa4mHXpqMnlJ/2jwK9Qqpf7G4o7o0st95AwfV912MIbHsqti55uwrp2O?= =?iso-8859-1?q?xJO/z3pwSrLIxWYQ/GUwA6KdLwS6vp1nPWXLcYMQf6mV9PApeIjaNswAYoHZ?= =?iso-8859-1?q?o9jAbcFH6XX+BpaQHdaNuqFI8CuT1j2Yve5WBJiwqMLaSUUY+7HK9H93OdYV?= =?iso-8859-1?q?WYpJOAgDYUh5T57bpwW9YkatCkeefsVD95nKCKdxgOSV16ryMxZSeRS+XR7I?= =?iso-8859-1?q?T/XFNegvOc4l1lsHvArN0dFLqCPBB6NPrUvq/hUyz61TUSWDBaMmdOx4XBQM?= =?iso-8859-1?q?isYy4fLQ9I5AXcB+5qEcF3ArlCux3XnNy1FN0jZSKpBBbzNPjJFN4UgpbGfn?= =?iso-8859-1?q?b+PomQXHseR81KX0szmLjkuHB7NfHqemSeYsar1AXcdofv3abjFSjjmIqoS4?= =?iso-8859-1?q?+HZlCGGHhMd68DKg9DX+baCccYVrlcRVj/1SC5zAAHXaUhbydbwqxa4ELkzz?= =?iso-8859-1?q?nmlNf3GDto8mXQh2hnAg2DChfjxxzkSBeKoIVq1M7M/z8isQCHxPecbAH4pW?= =?iso-8859-1?q?865dauJ8N2F8sjV5OHp22ByO9p5rNRPJpsbIgTvU73HglgMGtBw4OhzTPnWH?= =?iso-8859-1?q?FY9YXTy1SUBu/hxmDWFYdbOf37VKujUWAKVYJetK9XkJEhHG4C/lr1PiNNf4?= =?iso-8859-1?q?RKRSEbURfSa9giXrOP5LEUU+RHGX2Rq6kuqtrUU1giUVn11F47lbXGNrRfAi?= =?iso-8859-1?q?+/zDqYyHm9lbKcUoIYotWnxDlJST7H7bFZx/Elg=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: 9959f410-01e1-43d2-7612-08dd62fcff08 X-MS-Exchange-CrossTenant-originalarrivaltime: 14 Mar 2025 13:34:50.6048 (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: z0LBuDDp2ZQjQD4Vg23xrZ4s5otsDTd78ghvLS7hA/uLI8zuH3UEV0TT6G3qR2239uFn2MjNRwc/M/CKSXkUSA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR03MB7950 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. v8->v9: * replace DT_NO_IOMMU with 1 * guard iommu_add_pci_sideband_ids with CONFIG_ARM v7->v8: * ENOSYS->EOPNOTSUPP * move iommu_add_pci_sideband_ids to iommu.c to fix x86 build * simplify iommu_add_pci_sideband_ids * add docstrings to iommu_add_{dt_}pci_sideband_ids 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/drivers/passthrough/iommu.c | 15 +++++ xen/include/xen/device_tree.h | 23 +++++++ xen/include/xen/iommu.h | 40 +++++++++++- 5 files changed, 210 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 4a1971c3fc..37e1437b65 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 1; + + 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) ? 1 : 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/drivers/passthrough/iommu.c b/xen/drivers/passthrough/iommu.c index 9e74a1fc72..dfaca67302 100644 --- a/xen/drivers/passthrough/iommu.c +++ b/xen/drivers/passthrough/iommu.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #ifdef CONFIG_X86 @@ -744,6 +745,20 @@ int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func, return 0; } +#ifdef CONFIG_ARM +int iommu_add_pci_sideband_ids(struct pci_dev *pdev) +{ + int ret = -EOPNOTSUPP; + +#ifdef CONFIG_HAS_PCI + if ( acpi_disabled ) + ret = iommu_add_dt_pci_sideband_ids(pdev); +#endif + + return ret; +} +#endif + /* * Local variables: * mode: C 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 b928c67e19..82319016a1 100644 --- a/xen/include/xen/iommu.h +++ b/xen/include/xen/iommu.h @@ -215,7 +215,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 @@ -225,6 +226,19 @@ int iommu_release_dt_devices(struct domain *d); */ int iommu_add_dt_device(struct dt_device_node *np); +/* + * Fills out the device's IOMMU fwspec with IOMMU ids from the DT. + * Ids are specified in the iommu-map property in the host bridge node. + * More information on the iommu-map property format can be found in + * Documentation/devicetree/bindings/pci/pci-iommu.txt from Linux. + * + * Return values: + * 0 : iommu_fwspec is filled out successfully. + * <0 : error while filling out the iommu_fwspec. + * >0 : IOMMU is not enabled/present or device is not connected to it. + */ +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); @@ -238,8 +252,32 @@ int iommu_do_dt_domctl(struct xen_domctl *domctl, struct domain *d, */ int iommu_remove_dt_device(struct dt_device_node *np); +#else /* !HAS_DEVICE_TREE */ +static inline int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev) +{ + return -EOPNOTSUPP; +} + #endif /* HAS_DEVICE_TREE */ +/* + * This function is not strictly ARM-specific, but it is only used by ARM + * as of now. So wrap it with ifdef to avoid creating dead code on other + * architectures. When usage is extended to other architectures, this ifdef + * should be removed. + */ +#ifdef CONFIG_ARM +/* + * Fills out the device's IOMMU fwspec with IOMMU ids. + * + * Return values: + * 0 : iommu_fwspec is filled out successfully. + * <0 : error while filling out the iommu_fwspec. + * >0 : IOMMU is not enabled/present or device is not connected to it. + */ +int iommu_add_pci_sideband_ids(struct pci_dev *pdev); +#endif + struct page_info; /*