From patchwork Tue May 3 14:16:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jag Raman X-Patchwork-Id: 12835862 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.gnu.org (lists.gnu.org [209.51.188.17]) (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 C191EC433EF for ; Tue, 3 May 2022 14:38:16 +0000 (UTC) Received: from localhost ([::1]:55294 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nltfL-0004SK-RI for qemu-devel@archiver.kernel.org; Tue, 03 May 2022 10:38:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42494) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nltLY-0000TI-Kj for qemu-devel@nongnu.org; Tue, 03 May 2022 10:17:48 -0400 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:12830) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nltLV-0001QG-Kr for qemu-devel@nongnu.org; Tue, 03 May 2022 10:17:48 -0400 Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 243DEwCx004092; Tue, 3 May 2022 14:17:41 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : content-transfer-encoding : mime-version; s=corp-2021-07-09; bh=3pNimpuQZqiwrCvFuPMHL0mRMAp6gHodfV5qtEbdfa0=; b=QXqT32uSINYtcjFpCi75h7UyX/x924/OKneoQhZyiyau8xeTCq4HXfUtxykjm0orr2B4 pJy4mpfttt7HcdP0/AxYZJUZj2MYmOl5kWWRr++rBfMAqlw3kLetSY4kh6q4LPW/UBnk kh6lut4Ej5kjyrrS1sl314FsK/CGE070Mmn76JKVMMOtE1Ev3FZWkEzPJNQd/9GA4Ry6 tpTBMKqEHK/ka4sKmvDSqQj74H0ox667dCjL1HhXVfQpF+pdWx0nCshdCVFjd0WhWFSS +O4ZQdXfpZ25wuPuYZIxzcmzkHgw5riGackAuXov8DpjmlrtnrSZwg+5P2whJGUijjE/ CA== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3frw0annj7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 May 2022 14:17:41 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 243EFJLw032022; Tue, 3 May 2022 14:17:40 GMT Received: from nam02-sn1-obe.outbound.protection.outlook.com (mail-sn1anam02lp2044.outbound.protection.outlook.com [104.47.57.44]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3fs1a4r3ww-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 03 May 2022 14:17:40 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ffjC+wmDzaQZ1QnHM0H3xjYX+mWaazCG3KeFrgcV4xE5PJq0XN+ASC64XsKJMHxCbS3gqLsTFgwZSCQnVS4rEq9CwLf/BP+vbEBqHB2JZ7uhGptr3zwouODj3TWGF4kLnXUJ69hAEU7ohtkVdPRYoMsQX2ES5d5btCm/UjM+BGZnf/oAbb2cgog7l5A1oITRurASnuNtrHTXD65BN1eXCwGNnQEo86ukjkC9phiLFo+Mab4leQ6xZJRJMPuNj85+mVTlsv1EZAYmt6Uv7zFQ5d4Ju9PYWewo9atMCxa0zVUqAzfUctvg9Oy8dIB1kDmom6N4dkZQw+4cPZtYW31QRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=3pNimpuQZqiwrCvFuPMHL0mRMAp6gHodfV5qtEbdfa0=; b=L6iyMvTIPuQozVfAc7EZC4biIATdfVBtkNVAjOh3etXWNzGcch8qh4bGvcYDtTbOp0rVr1qzDYKuuiWwaRIZLy1niXsM+SZJoAPg6Q4gTJhI1hovsjryTkY+66Fo1sD4GSNHi0N80xaMS9iFyL9zosWCOGCv332MPBIBiSRug2Aa5i5WzmkNq4boP1pNiJecLPIkC1p7lx3CgMgmVHxMdsM4dXy83RvPp3ydrkaHeXFZRME7TNhtYgfJYx/+tKz9PB7FHI7ABbUCzMuGZq0ElpizavYjXPxmTxwE7KZD1Rfz2n98ChuC4DgbmnnxH3nF2XeJvmdgjLeeMxdKGhyCBQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3pNimpuQZqiwrCvFuPMHL0mRMAp6gHodfV5qtEbdfa0=; b=vP+fIVpJymuvUdzt1J4Oqc1iiwcFsA09RY3kBNVVLK8XMjFv/BQNsIiYjYViTfU6UbmJZ9d6U+c7awl1PQZavViQSOFreMpQe7T1mVWs8EEifx1FEc+PBzBh1OFrmJNuLEWr2Dxg7P6XZu1FgTHMoDSzuTWrfjJZcTdPQ/yjk/Q= Received: from MN2PR10MB4013.namprd10.prod.outlook.com (2603:10b6:208:185::25) by MN2PR10MB3358.namprd10.prod.outlook.com (2603:10b6:208:12f::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5206.24; Tue, 3 May 2022 14:17:35 +0000 Received: from MN2PR10MB4013.namprd10.prod.outlook.com ([fe80::e9cb:e1ca:8189:f191]) by MN2PR10MB4013.namprd10.prod.outlook.com ([fe80::e9cb:e1ca:8189:f191%7]) with mapi id 15.20.5206.013; Tue, 3 May 2022 14:17:35 +0000 From: Jagannathan Raman To: qemu-devel@nongnu.org Cc: stefanha@redhat.com, mst@redhat.com, f4bug@amsat.org, pbonzini@redhat.com, marcandre.lureau@redhat.com, thuth@redhat.com, bleal@redhat.com, berrange@redhat.com, eduardo@habkost.net, marcel.apfelbaum@gmail.com, eblake@redhat.com, armbru@redhat.com, quintela@redhat.com, dgilbert@redhat.com, imammedo@redhat.com, peterx@redhat.com, john.levon@nutanix.com, thanos.makatos@nutanix.com, elena.ufimtseva@oracle.com, john.g.johnson@oracle.com, kanth.ghatraju@oracle.com, jag.raman@oracle.com Subject: [PATCH v9 12/17] vfio-user: IOMMU support for remote device Date: Tue, 3 May 2022 10:16:53 -0400 Message-Id: <29948a10c3f2db09686fb6d257e31e4f873cb7d5.1651586203.git.jag.raman@oracle.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: X-ClientProxiedBy: SA9PR11CA0005.namprd11.prod.outlook.com (2603:10b6:806:6e::10) To MN2PR10MB4013.namprd10.prod.outlook.com (2603:10b6:208:185::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: af69b0b7-78fe-4020-1eaf-08da2d0fabb4 X-MS-TrafficTypeDiagnostic: MN2PR10MB3358:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: voQ0cPQczoTjhRLhzWhqyQwwOVS4vYMQkiGtY4QbClD1V3FWYVaSTBFpcRiec5b6K4lOosqnkQAEu0Ya0cjQWGXwmB7WMPKZFEaqKYGkJGyfPW0P+pD9/t72JoIDPrnGh/1K2O16GqKjnCUUQLJZ3wGnJtJ0ZhVjCPgjcE2ehHhu2VgZ1gRTvpzzoXnm9EncIjtfjSJ1I6LfSiYVvoGQsxnJtMkbnGCqJAzLxKHoFGUAk5SL5JYxZWIJjpQL0GAqdsUCxmsyCvcjU1lwkvIdmWoVIaORjeOiUaNk0OiSrhYRo/E/ho30UDkQDb3IKnHRtMshCAoEPVhL1l9DxomE9ItuSgRSqf+5UHP4t7cuxX7OMDR+fgiuXNUo7zCjW35u5Ml2BPa3qUFPWCEB66u9+POOsMIr70bH3k/YWoXGRB+esRA6wvgkQRwF8GLjYr1DmQ7nBrtEp11mYCGKY7D8/u71X6xpUk5d0wLwACdfp4hGfc9nKPgmB8wZHMlY4pjJC4RqZqfisd7VdbOhA3Q3WPsr3XastPpj18Y31qo9GuMrsnUpqesAh+pJTyYEieVyMFAacPgT0IdK0JQN39Fd/JrBk1H+eW8axKOKgbqjDyH/2rD6wwlZ3nXncviaVnlr1rYtV4kfLJAzGOraYB5LdtqyEfIdKGcFA5ES0BzF4jGnUuq8/aCcQS1DhYYEuBUo X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:MN2PR10MB4013.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(366004)(36756003)(83380400001)(66946007)(2616005)(6506007)(186003)(107886003)(2906002)(6486002)(6916009)(316002)(38350700002)(38100700002)(7416002)(5660300002)(8936002)(6512007)(86362001)(4326008)(6666004)(52116002)(66476007)(508600001)(8676002)(26005)(66556008); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?hTx/mI7W41k3jSQYd3M5rkc8W4eQ?= =?utf-8?q?RK9GnzQXUQ1MM0eSehDzaYSZ82mi5z8y+3dZoki9In8B1spEZzzsJecXhfKeXchjj?= =?utf-8?q?sWUjh/wJRDNjz8AI/b8QLkqrVSf7csmsJcvgj10x+3bFvbrJkQkKpGzZ2wSJGxa1D?= =?utf-8?q?L74mtoDchJ0iBrUS62yREIQvJGpCZks0hvlEd5+tn3xVS4Ydh6Drb5/l+HS8qy7Jd?= =?utf-8?q?HcED4mM5vDLeVfdelZ70PIrGH7cRqu7QJNs1QJBuV3r+52PtXW+/v+HY9i23Q18fP?= =?utf-8?q?yWa5ySQLq7YJWU7lysS0IP8T+hGzrU08zzmTX+3TwicPlVn4+30Dk4Ev5pRTOPsQK?= =?utf-8?q?0bwCaUR6K1uDtC+P6+MrHGn2TZbatcqj2Tn1y3ClzPtqJurk0kslPXgVn7+0UWV57?= =?utf-8?q?7T8j1Q59soQu8b2q//ozshPcuTtMFYX1cxAGU9UpUpISOelN3qWTNp18NxJXc741r?= =?utf-8?q?FQeaSzrqeqtVbn5kR5+PVeDrqtNq1NnemvUZiGYwmvpQ4wgBPJKTbfvNcC8g5cKki?= =?utf-8?q?vT6hYFeitTtlUsyhknPa1+XB6Ar3l6WySPXDOdlge+DvwMk+uw/hxhqrCF7LB1RoA?= =?utf-8?q?k6fM2OTKrrjwZg7RDRIWFRFrf8ZumQAs3UkVZRy1j40KUWO/1ONvC+3QwLURyTUJ3?= =?utf-8?q?bK3QLC07ImwK7stgWV2WbOdRI/EJSBBzLoD4epfjJeW/gU0j8IsonA+xz+N4WJ+7F?= =?utf-8?q?hxIgJNbP+Vn3G0UcVP1x7gP9imwfPInq0v+zoHJqdpBusNNX9Z6ISIY5INvVKRc2M?= =?utf-8?q?oa+HiZusaJSpPayWG3j0BT8DUPy3iyo2HM/suVsyjUr5rQ2xmU9T6+gEcLmnsa9If?= =?utf-8?q?NxIGocQY2u/sOp6DaGoQKzRlHApjksUiBnsqYQ6N5hWbSShq478FD4um0QYUFaL27?= =?utf-8?q?hEBCwXwGZG1Ke6z2TmrMSuF7Mv4oyvzqtatWRgavcjn1BgHjpTgmA63LId2qvUvhJ?= =?utf-8?q?0TmTwhdUnrKoFokFcEVeXPifO5KyfE2Nf6kerd0G8tAkT4PlZQu9QjAuoGqaNqzvV?= =?utf-8?q?Sjq9LJtmOu6UFj6dxmY3HEt66pxQxI2axojv9CliFv7IvA01AWzsjuGY2ZEt/Dto8?= =?utf-8?q?asdx96MTM2t1sQ19I3zpMdM1oSbkPnMuY2KWZkzA+VyxUZH7zHOTsnK3HYncYEBcl?= =?utf-8?q?mk9KuvDpqfUuvlW5JnpbM3zuEfZaNlkhniPYhoTLxAGRHKq3SumKOGqzxlGbuJVLq?= =?utf-8?q?lheoPQc24y/fAmU37NIk0L6YRrwVfKjZ/U97lXXpZ3f33T7RRjE4UUWNCOZ3yuC8L?= =?utf-8?q?Vu6AqkGlNKQMxbMINO+N7g9HBhn+H7rORHaDY0lC7vJBFGC6EjAvLZOSz3VVFFXV5?= =?utf-8?q?HlAaXXNWvF9gL33D/fupSFMQjVtWOUNtiHf96Y8ApYk7jyHCDTKA2BXOrWqOU80iw?= =?utf-8?q?fyPJhH8f5ElETbSWCrKMIfBG+y7ftO2MvKW9XbD1VGWnH6Il98Z49N4lztWsUp5wp?= =?utf-8?q?+WU+HhaoR8RFiXy84Ugr2wYFLhADc4XWwb9deeyLPg8owej1zkcAKgAO0Jn7QJJYU?= =?utf-8?q?cSRPZQjs4PiyOGzh3fcDW3IsalRWRmudcxZstJOFqofEUv8GOAv5Es9Nz6xgQzg5F?= =?utf-8?q?k1oJrRLpQLhXkpYTvEVjCy9VB3eIwQGDjj3nKe5SbahLneCvOm3Qd2W4woPiLqie7?= =?utf-8?q?nUwvPlGEpkFzIfV+SwurYR2FcpS7SHDw=3D=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: af69b0b7-78fe-4020-1eaf-08da2d0fabb4 X-MS-Exchange-CrossTenant-AuthSource: MN2PR10MB4013.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 May 2022 14:17:35.7626 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: xkxNBtUYfPjxu2gdIuvXAXahGnWwUdyFZs3dOvAxbej2i6bogxprvnDxvr4ehSQPgHBCqEA9uAWPjHwgLwvwxQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR10MB3358 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486, 18.0.858 definitions=2022-05-03_05:2022-05-02, 2022-05-03 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 bulkscore=0 malwarescore=0 spamscore=0 mlxlogscore=999 mlxscore=0 suspectscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205030103 X-Proofpoint-GUID: T2rHEvokVdlIloDLQRrr5mvCIjRj9LPT X-Proofpoint-ORIG-GUID: T2rHEvokVdlIloDLQRrr5mvCIjRj9LPT Received-SPF: pass client-ip=205.220.165.32; envelope-from=jag.raman@oracle.com; helo=mx0a-00069f02.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Assign separate address space for each device in the remote processes. Signed-off-by: Elena Ufimtseva Signed-off-by: John G Johnson Signed-off-by: Jagannathan Raman Reviewed-by: Stefan Hajnoczi --- include/hw/remote/iommu.h | 40 ++++++++++++ hw/remote/iommu.c | 131 ++++++++++++++++++++++++++++++++++++++ hw/remote/machine.c | 13 +++- MAINTAINERS | 2 + hw/remote/meson.build | 1 + 5 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 include/hw/remote/iommu.h create mode 100644 hw/remote/iommu.c diff --git a/include/hw/remote/iommu.h b/include/hw/remote/iommu.h new file mode 100644 index 0000000000..33b68a8f4b --- /dev/null +++ b/include/hw/remote/iommu.h @@ -0,0 +1,40 @@ +/** + * Copyright © 2022 Oracle and/or its affiliates. + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#ifndef REMOTE_IOMMU_H +#define REMOTE_IOMMU_H + +#include "hw/pci/pci_bus.h" +#include "hw/pci/pci.h" + +#ifndef INT2VOIDP +#define INT2VOIDP(i) (void *)(uintptr_t)(i) +#endif + +typedef struct RemoteIommuElem { + MemoryRegion *mr; + + AddressSpace as; +} RemoteIommuElem; + +#define TYPE_REMOTE_IOMMU "x-remote-iommu" +OBJECT_DECLARE_SIMPLE_TYPE(RemoteIommu, REMOTE_IOMMU) + +struct RemoteIommu { + Object parent; + + GHashTable *elem_by_devfn; + + QemuMutex lock; +}; + +void remote_iommu_setup(PCIBus *pci_bus); + +void remote_iommu_unplug_dev(PCIDevice *pci_dev); + +#endif diff --git a/hw/remote/iommu.c b/hw/remote/iommu.c new file mode 100644 index 0000000000..fd723d91f3 --- /dev/null +++ b/hw/remote/iommu.c @@ -0,0 +1,131 @@ +/** + * IOMMU for remote device + * + * Copyright © 2022 Oracle and/or its affiliates. + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#include "qemu/osdep.h" + +#include "hw/remote/iommu.h" +#include "hw/pci/pci_bus.h" +#include "hw/pci/pci.h" +#include "exec/memory.h" +#include "exec/address-spaces.h" +#include "trace.h" + +/** + * IOMMU for TYPE_REMOTE_MACHINE - manages DMA address space isolation + * for remote machine. It is used by TYPE_VFIO_USER_SERVER. + * + * - Each TYPE_VFIO_USER_SERVER instance handles one PCIDevice on a PCIBus. + * There is one RemoteIommu per PCIBus, so the RemoteIommu tracks multiple + * PCIDevices by maintaining a ->elem_by_devfn mapping. + * + * - memory_region_init_iommu() is not used because vfio-user MemoryRegions + * will be added to the elem->mr container instead. This is more natural + * than implementing the IOMMUMemoryRegionClass APIs since vfio-user + * provides something that is close to a full-fledged MemoryRegion and + * not like an IOMMU mapping. + * + * - When a device is hot unplugged, the elem->mr reference is dropped so + * all vfio-user MemoryRegions associated with this vfio-user server are + * destroyed. + */ + +static AddressSpace *remote_iommu_find_add_as(PCIBus *pci_bus, + void *opaque, int devfn) +{ + RemoteIommu *iommu = opaque; + RemoteIommuElem *elem = NULL; + + qemu_mutex_lock(&iommu->lock); + + elem = g_hash_table_lookup(iommu->elem_by_devfn, INT2VOIDP(devfn)); + + if (!elem) { + elem = g_malloc0(sizeof(RemoteIommuElem)); + g_hash_table_insert(iommu->elem_by_devfn, INT2VOIDP(devfn), elem); + } + + if (!elem->mr) { + elem->mr = MEMORY_REGION(object_new(TYPE_MEMORY_REGION)); + memory_region_set_size(elem->mr, UINT64_MAX); + address_space_init(&elem->as, elem->mr, NULL); + } + + qemu_mutex_unlock(&iommu->lock); + + return &elem->as; +} + +void remote_iommu_unplug_dev(PCIDevice *pci_dev) +{ + AddressSpace *as = pci_device_iommu_address_space(pci_dev); + RemoteIommuElem *elem = NULL; + + if (as == &address_space_memory) { + return; + } + + elem = container_of(as, RemoteIommuElem, as); + + address_space_destroy(&elem->as); + + object_unref(elem->mr); + + elem->mr = NULL; +} + +static void remote_iommu_init(Object *obj) +{ + RemoteIommu *iommu = REMOTE_IOMMU(obj); + + iommu->elem_by_devfn = g_hash_table_new_full(NULL, NULL, NULL, g_free); + + qemu_mutex_init(&iommu->lock); +} + +static void remote_iommu_finalize(Object *obj) +{ + RemoteIommu *iommu = REMOTE_IOMMU(obj); + + qemu_mutex_destroy(&iommu->lock); + + g_hash_table_destroy(iommu->elem_by_devfn); + + iommu->elem_by_devfn = NULL; +} + +void remote_iommu_setup(PCIBus *pci_bus) +{ + RemoteIommu *iommu = NULL; + + g_assert(pci_bus); + + iommu = REMOTE_IOMMU(object_new(TYPE_REMOTE_IOMMU)); + + pci_setup_iommu(pci_bus, remote_iommu_find_add_as, iommu); + + object_property_add_child(OBJECT(pci_bus), "remote-iommu", OBJECT(iommu)); + + object_unref(OBJECT(iommu)); +} + +static const TypeInfo remote_iommu_info = { + .name = TYPE_REMOTE_IOMMU, + .parent = TYPE_OBJECT, + .instance_size = sizeof(RemoteIommu), + .instance_init = remote_iommu_init, + .instance_finalize = remote_iommu_finalize, +}; + +static void remote_iommu_register_types(void) +{ + type_register_static(&remote_iommu_info); +} + +type_init(remote_iommu_register_types) diff --git a/hw/remote/machine.c b/hw/remote/machine.c index 4d008ed721..cbb2add291 100644 --- a/hw/remote/machine.c +++ b/hw/remote/machine.c @@ -20,6 +20,7 @@ #include "qapi/error.h" #include "hw/pci/pci_host.h" #include "hw/remote/iohub.h" +#include "hw/remote/iommu.h" #include "hw/qdev-core.h" static void remote_machine_init(MachineState *machine) @@ -99,6 +100,16 @@ static void remote_machine_instance_init(Object *obj) s->auto_shutdown = true; } +static void remote_machine_dev_unplug_cb(HotplugHandler *hotplug_dev, + DeviceState *dev, Error **errp) +{ + qdev_unrealize(dev); + + if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) { + remote_iommu_unplug_dev(PCI_DEVICE(dev)); + } +} + static void remote_machine_class_init(ObjectClass *oc, void *data) { MachineClass *mc = MACHINE_CLASS(oc); @@ -107,7 +118,7 @@ static void remote_machine_class_init(ObjectClass *oc, void *data) mc->init = remote_machine_init; mc->desc = "Experimental remote machine"; - hc->unplug = qdev_simple_device_unplug_cb; + hc->unplug = remote_machine_dev_unplug_cb; object_class_property_add_bool(oc, "vfio-user", remote_machine_get_vfio_user, diff --git a/MAINTAINERS b/MAINTAINERS index 1b5719cd89..f257afcf8a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3600,6 +3600,8 @@ F: hw/remote/iohub.c F: include/hw/remote/iohub.h F: subprojects/libvfio-user F: hw/remote/vfio-user-obj.c +F: hw/remote/iommu.c +F: include/hw/remote/iommu.h EBPF: M: Jason Wang diff --git a/hw/remote/meson.build b/hw/remote/meson.build index 534ac5df79..bcef83c8cc 100644 --- a/hw/remote/meson.build +++ b/hw/remote/meson.build @@ -6,6 +6,7 @@ remote_ss.add(when: 'CONFIG_MULTIPROCESS', if_true: files('message.c')) remote_ss.add(when: 'CONFIG_MULTIPROCESS', if_true: files('remote-obj.c')) remote_ss.add(when: 'CONFIG_MULTIPROCESS', if_true: files('proxy.c')) remote_ss.add(when: 'CONFIG_MULTIPROCESS', if_true: files('iohub.c')) +remote_ss.add(when: 'CONFIG_MULTIPROCESS', if_true: files('iommu.c')) remote_ss.add(when: 'CONFIG_VFIO_USER_SERVER', if_true: files('vfio-user-obj.c')) remote_ss.add(when: 'CONFIG_VFIO_USER_SERVER', if_true: vfiouser)