From patchwork Tue Oct 17 13:42:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 13425186 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3FD6FCDB483 for ; Tue, 17 Oct 2023 13:43:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343954AbjJQNn2 (ORCPT ); Tue, 17 Oct 2023 09:43:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343910AbjJQNn1 (ORCPT ); Tue, 17 Oct 2023 09:43:27 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2088.outbound.protection.outlook.com [40.107.220.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 895FBED for ; Tue, 17 Oct 2023 06:43:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QMF/bdv8/Bc+TfcXYRqR9dF/eJ/pJSFSKHMq/17Bsc7akfyMEaaxETZzlHSDLqZb/LokX/gnOXbP1Ba9Pc6Uscd1f01wI/o+w1REDz6NLsG8mfB+ACE3QTanxDh1npK7tklc0axUTbOt7TtWniKmkNWbfTVw3G1V5t1vgqgPCwSpeXi1CdOwNfpd9NGeD1Jp6lUyNsetqy/JZmOIl0qtBS0nfpr/bWKi0/3f86RnfevC1CHJCbFSov6UWg2DP4TByHSQ+cwlcm2I2YynuY04Hu3b+X+rNiyi6vuBkKoNIjobKTgZkbOuvlCyWV+74SVxEFTgcTM6qTxW4KEePSQqwg== 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=P1eFzxJ/LjJHaZZ59rqE75yCcH0bNXHQfZmDKNhUSPI=; b=G6pGwx7GgOo9uWqF2cSVkBfltgdCe9Z45U1HtMS5UiMXeVGKh33PMU0hMyPuhbzMI7vfTqU31xwUNTbb9Cm2l5FCanu9wetnZx6iJWRykotzi6EBm5SD8SUju/ZtIgCuKGACMn2HY5+/R+XmhhLHsqQcDXoOSJEWL4PTt669ST5onQKQ+0wi5ouqtd+ZEHEsgPuNQITB57l40YAKzPFbZRYrZdrJSksGSQNDubWreLnvO/wl69R8tLDVZxXhjJYTORCpKNd6/T8x3Pm6hdLx381QkXOq390wFy5XC7ULWBV/2K4tq6DGs9uNTwBaqLiPyobZlpiqQ4IF13MtPHarbA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=redhat.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=P1eFzxJ/LjJHaZZ59rqE75yCcH0bNXHQfZmDKNhUSPI=; b=WsRlKLx0FJ/2F3Bk10XcrSrjKbC/Ls736gNa5zBobVYJOve/ZYcjRljkzd6CchO3FQmEOyOLhGd/N1QJ4jmyBcdiGwagspPu8z7NftnT2JFNsptmvyLh9ux/k9i5rydQfrXiNrkkQ12vCSpw7EvBRKznD2339Zp7URj3uH3J8cGMdcpcbEJ9svbiHGxYw6eRx5WKwfD92o5Izt60NsmfxlJ1RqOYr5iJKQnJ1wqPCWe+QZpJfVjS8VQU2FI2bPRnOKlWRTXvN9h2+fx08bIpFBjdEeNHq3PiixPJWSHYsHEP5Ivh0e2Zj/fDuZX5tI84i3TgWqitLTq8ymzuYFMBKg== Received: from MN2PR20CA0062.namprd20.prod.outlook.com (2603:10b6:208:235::31) by CY8PR12MB7361.namprd12.prod.outlook.com (2603:10b6:930:53::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6886.36; Tue, 17 Oct 2023 13:43:23 +0000 Received: from BL02EPF0001A108.namprd05.prod.outlook.com (2603:10b6:208:235:cafe::91) by MN2PR20CA0062.outlook.office365.com (2603:10b6:208:235::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6886.36 via Frontend Transport; Tue, 17 Oct 2023 13:43:22 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BL02EPF0001A108.mail.protection.outlook.com (10.167.241.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6907.20 via Frontend Transport; Tue, 17 Oct 2023 13:43:22 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Tue, 17 Oct 2023 06:43:10 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Tue, 17 Oct 2023 06:43:10 -0700 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Tue, 17 Oct 2023 06:43:06 -0700 From: Yishai Hadas To: , , , CC: , , , , , , , , , , Subject: [PATCH V1 vfio 7/9] vfio/pci: Expose vfio_pci_core_setup_barmap() Date: Tue, 17 Oct 2023 16:42:15 +0300 Message-ID: <20231017134217.82497-8-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20231017134217.82497-1-yishaih@nvidia.com> References: <20231017134217.82497-1-yishaih@nvidia.com> MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0001A108:EE_|CY8PR12MB7361:EE_ X-MS-Office365-Filtering-Correlation-Id: 6dc55722-fd0f-4f8f-cd1a-08dbcf170805 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GDtwXyRo0VtVmdwGjEnbf24p+LzE5xgxYYlkbXMhl8o60HZHHqsWU8YiXDsEEYKbOLDKzessAnVWYL4bgqlimsrhDlYAS/DKjBNosu0A5VKt8MXvlTMtQnlRS1T9lnIaE8A9bA66FH4HtkBbEsxL/IrX5YV7d+nRf+WVKHEV0rr6lPgxctxvDfBSlXtN2BIZN2DaIry2UcKjrOJt8nSqbRtQvJD0F7UtO1a4rYcs56FPCcWtqqGNMMs+oWqKBPvPL3sKiZn4qBbXrTwHybZJ9knUOciYcPjms9TsgqVSMoIL8Itc6E0NSbUVHGBgKNtgRTw7+uNS0fxy+ENwQY9A+D0qAJmks3yQmkHvtiMfs34JjHqMa6ZXNaWzn3QyAisZNvWmv9Rhl1IESOodlPLtCoH6iTe+oSEqX0hJYTAAxCIzy93HMfPYFzzCTekqB5yWRvs9/EyETkv47nVA78XvIzSSpMKXkFiHg0tYOC0LWxjrjReYG62YlgDM6WyBUfbB58YCezrmPg91MUHGC2zbjU5SO3hOuu6xT0+AYsm4Bk4q09+fJKaGP/HNl/w7bu44h0DBHvtPG5TFKWwnwGrRje1b1vllqN5Le8RHyfX/ccBXHGWEc3qh1xvIRKMKOxBw4n73ma1lYUu+2U32n/n8vsU6cC9Dej269+HVyf4PVbmKuk/1uGNDHgVc0v1mjoSWdbrS1FyjP9Pu2SqYB0K86+q2h8LSFB5DzQNxc9upWqKOA15LFkgj0mGW1EfHCrmODIxgYTRttWZ3I5J95mFYUw== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(376002)(396003)(136003)(39860400002)(346002)(230922051799003)(1800799009)(82310400011)(186009)(451199024)(64100799003)(40470700004)(36840700001)(46966006)(36756003)(40480700001)(40460700003)(110136005)(54906003)(6636002)(316002)(70586007)(70206006)(86362001)(82740400003)(7636003)(356005)(36860700001)(83380400001)(426003)(336012)(107886003)(26005)(1076003)(2616005)(6666004)(7696005)(8936002)(2906002)(478600001)(8676002)(41300700001)(5660300002)(47076005)(4326008)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Oct 2023 13:43:22.6821 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6dc55722-fd0f-4f8f-cd1a-08dbcf170805 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0001A108.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7361 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Expose vfio_pci_core_setup_barmap() to be used by drivers. This will let drivers to mmap a BAR and re-use it from both vfio and the driver when it's applicable. This API will be used in the next patches by the vfio/virtio coming driver. Signed-off-by: Yishai Hadas --- drivers/vfio/pci/vfio_pci_core.c | 25 +++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_rdwr.c | 28 ++-------------------------- include/linux/vfio_pci_core.h | 1 + 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 1929103ee59a..ebea39836dd9 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -684,6 +684,31 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) } EXPORT_SYMBOL_GPL(vfio_pci_core_disable); +int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar) +{ + struct pci_dev *pdev = vdev->pdev; + void __iomem *io; + int ret; + + if (vdev->barmap[bar]) + return 0; + + ret = pci_request_selected_regions(pdev, 1 << bar, "vfio"); + if (ret) + return ret; + + io = pci_iomap(pdev, bar, 0); + if (!io) { + pci_release_selected_regions(pdev, 1 << bar); + return -ENOMEM; + } + + vdev->barmap[bar] = io; + + return 0; +} +EXPORT_SYMBOL_GPL(vfio_pci_core_setup_barmap); + void vfio_pci_core_close_device(struct vfio_device *core_vdev) { struct vfio_pci_core_device *vdev = diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c index e27de61ac9fe..6f08b3ecbb89 100644 --- a/drivers/vfio/pci/vfio_pci_rdwr.c +++ b/drivers/vfio/pci/vfio_pci_rdwr.c @@ -200,30 +200,6 @@ static ssize_t do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem, return done; } -static int vfio_pci_setup_barmap(struct vfio_pci_core_device *vdev, int bar) -{ - struct pci_dev *pdev = vdev->pdev; - int ret; - void __iomem *io; - - if (vdev->barmap[bar]) - return 0; - - ret = pci_request_selected_regions(pdev, 1 << bar, "vfio"); - if (ret) - return ret; - - io = pci_iomap(pdev, bar, 0); - if (!io) { - pci_release_selected_regions(pdev, 1 << bar); - return -ENOMEM; - } - - vdev->barmap[bar] = io; - - return 0; -} - ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf, size_t count, loff_t *ppos, bool iswrite) { @@ -262,7 +238,7 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf, } x_end = end; } else { - int ret = vfio_pci_setup_barmap(vdev, bar); + int ret = vfio_pci_core_setup_barmap(vdev, bar); if (ret) { done = ret; goto out; @@ -438,7 +414,7 @@ int vfio_pci_ioeventfd(struct vfio_pci_core_device *vdev, loff_t offset, return -EINVAL; #endif - ret = vfio_pci_setup_barmap(vdev, bar); + ret = vfio_pci_core_setup_barmap(vdev, bar); if (ret) return ret; diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index 562e8754869d..67ac58e20e1d 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -127,6 +127,7 @@ int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf); int vfio_pci_core_enable(struct vfio_pci_core_device *vdev); void vfio_pci_core_disable(struct vfio_pci_core_device *vdev); void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev); +int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar); pci_ers_result_t vfio_pci_core_aer_err_detected(struct pci_dev *pdev, pci_channel_state_t state);