Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp2825515rdh; Sun, 26 Nov 2023 22:39:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IFkgFpzZfVZOh2wmqcXbuon8eWPU/KveHTLbIxjmy7cntAnBOEbxzJRl+V2g4lmXHx5roYr X-Received: by 2002:a9d:7493:0:b0:6bd:d14:147a with SMTP id t19-20020a9d7493000000b006bd0d14147amr11858325otk.21.1701067170366; Sun, 26 Nov 2023 22:39:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701067170; cv=none; d=google.com; s=arc-20160816; b=w7zfiy3tSOHI1JorodH3Rq5bgB3jgr3evso0yPlxWrfyb56Vf4URhFmwzo4Rg/FFui A3gZ/fYwrOqEKBMVpAAVYfZyp2Vw37Og2aw0qH0BjW8v/pfc6pfzaAhg/DdPq7/a+Wvv 5HM18PJcJSwCziES1WRH01PgPTjdDZoGO3qflK9Uy8lr7C1ox60jtuGeo/Ol905hE+yx 4j/FggkSou8pRYWFk893IKVcJa1AGpOig5pcPZn3DtybU197bG+f+86E3ak2/R3nema6 JHE75B5rYnCp78xXTJqq+3RsCkfSIznanzdfNmD42CUKm+PDkX5GA4BQDaE3LZQVU69l /Dig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=xqPK+y0ZLw3dcS1Uc1dgxHrSias0B+kCmLL2XPqnaCE=; fh=gm0A6EtasJwovXfz1TpeYfkY+4H0EN5E4rSCpite+Dk=; b=cVBI+2hqxEM+gZaH85+VLmwtucTSeOPp2d6fac/fLE3+tN/nbiSwYx7yGtwQ0ND1vb P4roQT8vKuvXhUR+TQXV71gIrVWznFy85MqvvCFZWjRR/NfhkLg1NEfxNb9B+K/zDkyg EBi5i1+u7J89NHsCIYzPoWwpZEprudM1j7XCTq+jHtRQavig7IYpfBjnkl/o2cRSJzbG N6qC++HYhmEcK+EyY5f9CSorrwGPjXGZPnxCfXpXdzTkdS3nLacW2EobNzdrscUhfRcc TgWgFIlB93ZvHn/Avuzh0PWJqMPSg6Kj8g5M8DrgdMFq4TUkiQUCJKmdAS+GwAAxhmsq r31w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=OOSCoSZT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id p16-20020a63e650000000b005c1b2fdfe6dsi9639746pgj.573.2023.11.26.22.39.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Nov 2023 22:39:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=OOSCoSZT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 42592806135D; Sun, 26 Nov 2023 22:39:29 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232388AbjK0GjP (ORCPT + 99 others); Mon, 27 Nov 2023 01:39:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231443AbjK0GjJ (ORCPT ); Mon, 27 Nov 2023 01:39:09 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BD8F136; Sun, 26 Nov 2023 22:39:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701067156; x=1732603156; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VAys2T6SYXa/EbFErLKF/Vbg/PfRt/6DnDP8hziF19c=; b=OOSCoSZTdDSPEWC49nnn90Ol1aQymW75sry2QEoTTGpXoj/WfGsmwQod ulSGvt9qTHqo7Op5GOEeFzpFqVVtqpXkqPPnj3n1be4RGeYVWYYIJLcSw tKmYTizdgbD72mkWfI51Fw/YKqd+hFFPa8s1y1YjebXik49pX7RhDZJPM IHLT4/vxNzw3pXqnybmwOSAqFVDHkloRDZkP4UwT9evLfER6EU6lJTI8e Xb6nOZ4afzfFL6e8U+d6IUPok+RpgFe6ZhzBhKjc7wCYI2z5EOEXVSMwU KkN3pUQ0nljO4wTo2R5ovSOTNYtjYpQ2NtC+u2tNOwLvDH4YrsnHXbDnB A==; X-IronPort-AV: E=McAfee;i="6600,9927,10906"; a="392397770" X-IronPort-AV: E=Sophos;i="6.04,230,1695711600"; d="scan'208";a="392397770" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Nov 2023 22:39:15 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10906"; a="717940819" X-IronPort-AV: E=Sophos;i="6.04,230,1695711600"; d="scan'208";a="717940819" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by orsmga003.jf.intel.com with ESMTP; 26 Nov 2023 22:39:12 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com, xin.zeng@intel.com, yan.y.zhao@intel.com Subject: [PATCH 3/3] vfio: Report PASID capability via VFIO_DEVICE_FEATURE ioctl Date: Sun, 26 Nov 2023 22:39:09 -0800 Message-Id: <20231127063909.129153-4-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231127063909.129153-1-yi.l.liu@intel.com> References: <20231127063909.129153-1-yi.l.liu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sun, 26 Nov 2023 22:39:29 -0800 (PST) This reports the PASID capability data to userspace via VFIO_DEVICE_FEATURE, hence userspace could probe PASID capability by it. This is a bit different with other capabilities which are reported to userspace when the user reads the device's PCI configuration space. There are two reasons for this. - First, Qemu by default exposes all available PCI capabilities in vfio-pci config space to the guest as read-only, so adding PASID capability in the vfio-pci config space will make it exposed to the guest automatically while an old Qemu doesn't really support it. - Second, PASID capability does not exit on VFs (instead shares the cap of the PF). Creating a virtual PASID capability in vfio-pci config space needs to find a hole to place it, but doing so may require device specific knowledge to avoid potential conflict with device specific registers like hiden bits in VF config space. It's simpler by moving this burden to the VMM instead of maintaining a quirk system in the kernel. Suggested-by: Alex Williamson Signed-off-by: Yi Liu --- drivers/vfio/pci/vfio_pci_core.c | 47 ++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 13 +++++++++ 2 files changed, 60 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 1929103ee59a..8038aa45500e 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -1495,6 +1495,51 @@ static int vfio_pci_core_feature_token(struct vfio_device *device, u32 flags, return 0; } +static int vfio_pci_core_feature_pasid(struct vfio_device *device, u32 flags, + struct vfio_device_feature_pasid __user *arg, + size_t argsz) +{ + struct vfio_pci_core_device *vdev = + container_of(device, struct vfio_pci_core_device, vdev); + struct vfio_device_feature_pasid pasid = { 0 }; + struct pci_dev *pdev = vdev->pdev; + u32 capabilities = 0; + int ret; + + /* We do not support SET of the PASID capability */ + ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_GET, + sizeof(pasid)); + if (ret != 1) + return ret; + + /* + * Needs go to PF if the device is VF as VF shares its PF's + * PASID Capability. + */ + if (pdev->is_virtfn) + pdev = pci_physfn(pdev); + + if (!pdev->pasid_enabled) + goto out; + +#ifdef CONFIG_PCI_PASID + pci_read_config_dword(pdev, pdev->pasid_cap + PCI_PASID_CAP, + &capabilities); +#endif + + if (capabilities & PCI_PASID_CAP_EXEC) + pasid.capabilities |= VFIO_DEVICE_PASID_CAP_EXEC; + if (capabilities & PCI_PASID_CAP_PRIV) + pasid.capabilities |= VFIO_DEVICE_PASID_CAP_PRIV; + + pasid.width = (capabilities >> 8) & 0x1f; + +out: + if (copy_to_user(arg, &pasid, sizeof(pasid))) + return -EFAULT; + return 0; +} + int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags, void __user *arg, size_t argsz) { @@ -1508,6 +1553,8 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags, return vfio_pci_core_pm_exit(device, flags, arg, argsz); case VFIO_DEVICE_FEATURE_PCI_VF_TOKEN: return vfio_pci_core_feature_token(device, flags, arg, argsz); + case VFIO_DEVICE_FEATURE_PASID: + return vfio_pci_core_feature_pasid(device, flags, arg, argsz); default: return -ENOTTY; } diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 495193629029..8326faf8622b 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -1512,6 +1512,19 @@ struct vfio_device_feature_bus_master { }; #define VFIO_DEVICE_FEATURE_BUS_MASTER 10 +/** + * Upon VFIO_DEVICE_FEATURE_GET, return the PASID capability for the device. + * Zero width means no support for PASID. + */ +struct vfio_device_feature_pasid { + __u16 capabilities; +#define VFIO_DEVICE_PASID_CAP_EXEC (1 << 0) +#define VFIO_DEVICE_PASID_CAP_PRIV (1 << 1) + __u8 width; + __u8 __reserved; +}; +#define VFIO_DEVICE_FEATURE_PASID 11 + /* -------- API for Type1 VFIO IOMMU -------- */ /** -- 2.34.1