From nobody Sun Nov 24 01:19:00 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89591199BC for ; Fri, 8 Nov 2024 02:15:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032103; cv=none; b=l3k0ym5cJZHwpjQgxW13BpEA/FWZuIdmCGc63fbfHZQwzBsfajy6Gzj6L8XqBE1UGAsqIDPpPyeat42XQG9iad0Fs7rh6Iu7MNpgCVFttTdccn/O0JZTDyz4MASZ+LC+Av2IJcPx6joZbzql1GWEUP7lGj2S2EDbt/hXyPi4ibM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032103; c=relaxed/simple; bh=w+eyi5SDR+GEiDrCO9seIHx/z9DNYStl43G2IPYxsVg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XzFKAv3//dpZc+fB3AbF9p2zzbiYz2Ke3hGZmGmEHfXDYm/X7NYqfCjpyYpz0fWSKwjd84IPEIPyDocp5vL6cSXy2f6KdzRt/fKyyES3A+fJ5dtokPIPvwr6BpwAWmxFEbVfVoooQI+/xPYiy9wdRgHGCTyYIanoSrnRvwM3U4I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=QwfyJbzk; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="QwfyJbzk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731032101; x=1762568101; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=w+eyi5SDR+GEiDrCO9seIHx/z9DNYStl43G2IPYxsVg=; b=QwfyJbzk+/8avhB56A9LgDQ775++nmMw+yO1crJHuDHKwEcVMoq/v6kG xcmXemrFqTqAuXlCvoEBFN6xdo0/N/QmKU1KybPXX7Im3yQzwnanl2ZdO yirjGrSR2QeNnOgwtfr4Ki8EzJuahia1hoc68S9BWRDSNYKyJuavvBXle cABpHay5+muYEq5woKgVKuYKBHL8UWP+WlNoAyyTPBodPPDt3qUQxtZp4 PxsIsyTf1oXOvYoMlLO4sudZxw7POxIzmD80kawH490Ltz/DtNV3CuxFC 57hCcQyZP5LhKZDgD6PeBWE51CtM8INQ7OPTpbqUgwF8i8qK4v4OFPAcF Q==; X-CSE-ConnectionGUID: SadK35k3QKCeulCvkH0xpQ== X-CSE-MsgGUID: BPWgsj/ZS2aCQDx1xaP3Dw== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="31007729" X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="31007729" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 18:15:00 -0800 X-CSE-ConnectionGUID: ZVs0WjrcTcWn2v1kQ8TINw== X-CSE-MsgGUID: 7ObZAuxrRQKG1ai+fT0d5w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="116213720" Received: from allen-sbox.sh.intel.com ([10.239.159.30]) by fmviesa001.fm.intel.com with ESMTP; 07 Nov 2024 18:14:58 -0800 From: Lu Baolu To: Joerg Roedel Cc: Jason Gunthorpe , Kevin Tian , Yi Liu , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 01/13] iommu: Pass old domain to set_dev_pasid op Date: Fri, 8 Nov 2024 10:13:52 +0800 Message-ID: <20241108021406.173972-2-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241108021406.173972-1-baolu.lu@linux.intel.com> References: <20241108021406.173972-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Yi Liu To support domain replacement for pasid, the underlying iommu driver needs to know the old domain hence be able to clean up the existing attachment. It would be much convenient for iommu layer to pass down the old domain. Otherwise, iommu drivers would need to track domain for pasids by themselves, this would duplicate code among the iommu drivers. Or iommu drivers would rely group->pasid_array to get domain, which may not always the correct one. Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Reviewed-by: Nicolin Chen Reviewed-by: Vasant Hegde Signed-off-by: Yi Liu Link: https://lore.kernel.org/r/20241107122234.7424-2-yi.l.liu@intel.com Signed-off-by: Lu Baolu --- drivers/iommu/amd/amd_iommu.h | 3 ++- drivers/iommu/amd/pasid.c | 3 ++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 3 ++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 3 ++- drivers/iommu/intel/iommu.c | 6 ++++-- drivers/iommu/intel/svm.c | 3 ++- drivers/iommu/iommu.c | 3 ++- include/linux/iommu.h | 2 +- 8 files changed, 17 insertions(+), 9 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h index 6386fa4556d9..b11b014fa82d 100644 --- a/drivers/iommu/amd/amd_iommu.h +++ b/drivers/iommu/amd/amd_iommu.h @@ -52,7 +52,8 @@ struct iommu_domain *amd_iommu_domain_alloc_sva(struct de= vice *dev, struct mm_struct *mm); void amd_iommu_domain_free(struct iommu_domain *dom); int iommu_sva_set_dev_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t pasid); + struct device *dev, ioasid_t pasid, + struct iommu_domain *old); void amd_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid, struct iommu_domain *domain); =20 diff --git a/drivers/iommu/amd/pasid.c b/drivers/iommu/amd/pasid.c index 0657b9373be5..d1dfc745f55e 100644 --- a/drivers/iommu/amd/pasid.c +++ b/drivers/iommu/amd/pasid.c @@ -100,7 +100,8 @@ static const struct mmu_notifier_ops sva_mn =3D { }; =20 int iommu_sva_set_dev_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t pasid) + struct device *dev, ioasid_t pasid, + struct iommu_domain *old) { struct pdom_dev_data *pdom_dev_data; struct protection_domain *sva_pdom =3D to_pdomain(domain); diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iomm= u/arm/arm-smmu-v3/arm-smmu-v3-sva.c index a7c36654dee5..645da7b69bed 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -332,7 +332,8 @@ void arm_smmu_sva_notifier_synchronize(void) } =20 static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t id) + struct device *dev, ioasid_t id, + struct iommu_domain *old) { struct arm_smmu_domain *smmu_domain =3D to_smmu_domain(domain); struct arm_smmu_master *master =3D dev_iommu_priv_get(dev); diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/ar= m/arm-smmu-v3/arm-smmu-v3.c index 737c5b882355..1517fe2c356e 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2856,7 +2856,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *d= omain, struct device *dev) } =20 static int arm_smmu_s1_set_dev_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t id) + struct device *dev, ioasid_t id, + struct iommu_domain *old) { struct arm_smmu_domain *smmu_domain =3D to_smmu_domain(domain); struct arm_smmu_master *master =3D dev_iommu_priv_get(dev); diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index e2b3aa601191..2d1d208d5125 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -4070,7 +4070,8 @@ static void intel_iommu_remove_dev_pasid(struct devic= e *dev, ioasid_t pasid, } =20 static int intel_iommu_set_dev_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t pasid) + struct device *dev, ioasid_t pasid, + struct iommu_domain *old) { struct device_domain_info *info =3D dev_iommu_priv_get(dev); struct dmar_domain *dmar_domain =3D to_dmar_domain(domain); @@ -4356,7 +4357,8 @@ static int identity_domain_attach_dev(struct iommu_do= main *domain, struct device } =20 static int identity_domain_set_dev_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t pasid) + struct device *dev, ioasid_t pasid, + struct iommu_domain *old) { struct device_domain_info *info =3D dev_iommu_priv_get(dev); struct intel_iommu *iommu =3D info->iommu; diff --git a/drivers/iommu/intel/svm.c b/drivers/iommu/intel/svm.c index 3cc43a958b4d..4a2bd65614ad 100644 --- a/drivers/iommu/intel/svm.c +++ b/drivers/iommu/intel/svm.c @@ -111,7 +111,8 @@ static const struct mmu_notifier_ops intel_mmuops =3D { }; =20 static int intel_svm_set_dev_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t pasid) + struct device *dev, ioasid_t pasid, + struct iommu_domain *old) { struct device_domain_info *info =3D dev_iommu_priv_get(dev); struct dmar_domain *dmar_domain =3D to_dmar_domain(domain); diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 83c8e617a2c5..f3f81c04b8fb 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3331,7 +3331,8 @@ static int __iommu_set_group_pasid(struct iommu_domai= n *domain, int ret; =20 for_each_group_device(group, device) { - ret =3D domain->ops->set_dev_pasid(domain, device->dev, pasid); + ret =3D domain->ops->set_dev_pasid(domain, device->dev, + pasid, NULL); if (ret) goto err_revert; } diff --git a/include/linux/iommu.h b/include/linux/iommu.h index bd722f473635..32dce80aa7fd 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -642,7 +642,7 @@ struct iommu_ops { struct iommu_domain_ops { int (*attach_dev)(struct iommu_domain *domain, struct device *dev); int (*set_dev_pasid)(struct iommu_domain *domain, struct device *dev, - ioasid_t pasid); + ioasid_t pasid, struct iommu_domain *old); =20 int (*map_pages)(struct iommu_domain *domain, unsigned long iova, phys_addr_t paddr, size_t pgsize, size_t pgcount, --=20 2.43.0 From nobody Sun Nov 24 01:19:00 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E96E2481DD for ; Fri, 8 Nov 2024 02:15:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032104; cv=none; b=G7TcnvMT5QdMCzE4j1pJTmt2/4HxzYC3asZpBNphXivvwHviuSI++ZOlZqDuCxhblhAa1rR2eECrQbQRG6VaIZ20M2tBoV/bMZrVLnvrbIm5ExBHJD7bPA5VXY2O3Z/7N/eZZoaSmI/qAhKj3NKcZ1p1ZRfpiwhRgOAvDcx0RoU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032104; c=relaxed/simple; bh=rnccXpvvV6E9nMU3XuiFVBN1Y54SbbFLjDOdujr0Vg8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Q2PtsYnbeXjmu3V+tDkPgBYLwLDM0bFMoLBpm50+ZqEe2iIIOq3weXL8GZOmVDUdEMfxBmLLKJ/YdZoJCYapcDhA5d3FKdP0qM/g1otFrUHElcWQVXN/TqSv506OGY/V7sSNTEpBVOK84gS7nvZZEIXjM13eigzoesWbJrmQdWk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=HiAmt215; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="HiAmt215" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731032103; x=1762568103; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rnccXpvvV6E9nMU3XuiFVBN1Y54SbbFLjDOdujr0Vg8=; b=HiAmt215SjtizK76DvFLsmHLE6my9rFkBLR8fwGT0BjJOemDoAGB+KtF CZDUIwprSu7bApMgB4QYuwM+Jz4PLq9wx42g0qA+j2daVU5XcyXowhtO+ CeFTxoa/BW4la/eB0sgw8Lx84g/olzaMtXuIMqIzihuYBRmpjRS2UPCOC dsHKWmB91aYQrwyyA3gkVmaPkKFYvC6ONjSSlk9NUvimbOUu9912YkW17 xKKXlmp6RPOk5uK2N418IgvI/9MbLTEFxuGEIxuxNYMjBl+hq/eMhp1KQ 9TpdiyGPYPbXhRUsF+2ookmNaTGaBuA3u2JB+gtpiw/IgeTiF4u2ccSYm A==; X-CSE-ConnectionGUID: 9rgvER+6QJezQVyhyAJUOw== X-CSE-MsgGUID: xagFqE15QH+ahh7EIBJTdQ== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="31007738" X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="31007738" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 18:15:02 -0800 X-CSE-ConnectionGUID: /KE6/FgbRHSla2uvbYEdsA== X-CSE-MsgGUID: GG5RL0UeQySIT6/UAl6HLQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="116213732" Received: from allen-sbox.sh.intel.com ([10.239.159.30]) by fmviesa001.fm.intel.com with ESMTP; 07 Nov 2024 18:15:00 -0800 From: Lu Baolu To: Joerg Roedel Cc: Jason Gunthorpe , Kevin Tian , Yi Liu , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 02/13] iommu/vt-d: Add a helper to flush cache for updating present pasid entry Date: Fri, 8 Nov 2024 10:13:53 +0800 Message-ID: <20241108021406.173972-3-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241108021406.173972-1-baolu.lu@linux.intel.com> References: <20241108021406.173972-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Yi Liu Generalize the logic for flushing pasid-related cache upon changes to bits other than SSADE and P which requires a different flow according to VT-d spec. No functional change is intended. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Yi Liu Link: https://lore.kernel.org/r/20241107122234.7424-3-yi.l.liu@intel.com Signed-off-by: Lu Baolu --- drivers/iommu/intel/pasid.c | 52 ++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/drivers/iommu/intel/pasid.c b/drivers/iommu/intel/pasid.c index 31665fb62e1c..8d11701c2e76 100644 --- a/drivers/iommu/intel/pasid.c +++ b/drivers/iommu/intel/pasid.c @@ -287,6 +287,39 @@ static void pasid_flush_caches(struct intel_iommu *iom= mu, } } =20 +/* + * This function is supposed to be used after caller updates the fields + * except for the SSADE and P bit of a pasid table entry. It does the + * below: + * - Flush cacheline if needed + * - Flush the caches per Table 28 =E2=80=9DGuidance to Software for Inval= idations=E2=80=9C + * of VT-d spec 5.0. + */ +static void intel_pasid_flush_present(struct intel_iommu *iommu, + struct device *dev, + u32 pasid, u16 did, + struct pasid_entry *pte) +{ + if (!ecap_coherent(iommu->ecap)) + clflush_cache_range(pte, sizeof(*pte)); + + /* + * VT-d spec 5.0 table28 states guides for cache invalidation: + * + * - PASID-selective-within-Domain PASID-cache invalidation + * - PASID-selective PASID-based IOTLB invalidation + * - If (pasid is RID_PASID) + * - Global Device-TLB invalidation to affected functions + * Else + * - PASID-based Device-TLB invalidation (with S=3D1 and + * Addr[63:12]=3D0x7FFFFFFF_FFFFF) to affected functions + */ + pasid_cache_invalidation_with_pasid(iommu, did, pasid); + qi_flush_piotlb(iommu, did, pasid, 0, -1, 0); + + devtlb_invalidation_with_pasid(iommu, dev, pasid); +} + /* * Set up the scalable mode pasid table entry for first only * translation type. @@ -526,24 +559,7 @@ void intel_pasid_setup_page_snoop_control(struct intel= _iommu *iommu, did =3D pasid_get_domain_id(pte); spin_unlock(&iommu->lock); =20 - if (!ecap_coherent(iommu->ecap)) - clflush_cache_range(pte, sizeof(*pte)); - - /* - * VT-d spec 3.4 table23 states guides for cache invalidation: - * - * - PASID-selective-within-Domain PASID-cache invalidation - * - PASID-selective PASID-based IOTLB invalidation - * - If (pasid is RID_PASID) - * - Global Device-TLB invalidation to affected functions - * Else - * - PASID-based Device-TLB invalidation (with S=3D1 and - * Addr[63:12]=3D0x7FFFFFFF_FFFFF) to affected functions - */ - pasid_cache_invalidation_with_pasid(iommu, did, pasid); - qi_flush_piotlb(iommu, did, pasid, 0, -1, 0); - - devtlb_invalidation_with_pasid(iommu, dev, pasid); + intel_pasid_flush_present(iommu, dev, pasid, did, pte); } =20 /** --=20 2.43.0 From nobody Sun Nov 24 01:19:00 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB740433AD for ; Fri, 8 Nov 2024 02:15:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032107; cv=none; b=ZSUF3GywhuQKiPx4S4te+OTNUhIcw+XoitBhhf8FAws/vMlFGtmOOrMDCYPfJMbC8Py4ViFteKYyjm0DsqXd6fDd7S5mrx+0bMx+0Akqn9bZPC5Vei3r5+AUfZ6kJKVT/tszakuEbON1hKPf4csdc+2Rw/y6iYHMztKcdp78yhQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032107; c=relaxed/simple; bh=bjsWhVMSbd813w0DjLkjDWXvfKtoiu2Kwm/FhWjSqAc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RO/FvnslC8UAX/4UNMnsr87Dn5ng4Y3cO74sbxieA7tIQ2B069aEQoTjlds063zQJYw+rAetGzhbVcnzOc0IC1PdmUf0XeOCIDIjPrlSe+4z33GPyGrGrvGuYoFp/Xb0t9CqIPe/wOrJT9yvjUNbL2TqFc/7haLAkDU3qVKjBFU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Z6Difwp8; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Z6Difwp8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731032106; x=1762568106; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bjsWhVMSbd813w0DjLkjDWXvfKtoiu2Kwm/FhWjSqAc=; b=Z6Difwp8DLLhMHX2AXK5mwN3RtNd+DhzQCWMo6AZshzUjKyivjxfrTYg JpOYpi0Tbuc2BB7SXIF3vc6HimgG7NLc5RnNKJ89pk1d2gwv+XcBRka+v IAg6w7SCJU98ZQiFcDfdcmt+06qXJ4xde9dAaR59n8+sT2NGF5FZykhzP lTv/ddrayhkGgQlWsBqDN2hzTrJwdrE+s7zaGx631p5IrRCShhshvPEMA oJe9nISGq44UoEVZI4ez0e/c41VBpvZ7ZXIoUG88bf0DawIYj6zm9e/P+ fHvfZSmR/c2BjEyDELT6NT1ky2fWkqi47BK7EFJ2yVgEk7T8KgzdbsxZ/ Q==; X-CSE-ConnectionGUID: Lc9S4hWuSzyRr89GroVo4A== X-CSE-MsgGUID: W/Q6KU0ORQmPggnFsRTXqQ== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="31007752" X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="31007752" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 18:15:05 -0800 X-CSE-ConnectionGUID: Ppe7ING7S5y8mB5UDs+Ifw== X-CSE-MsgGUID: nI76q8J9QjG9SJ8ehuyTLQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="116213750" Received: from allen-sbox.sh.intel.com ([10.239.159.30]) by fmviesa001.fm.intel.com with ESMTP; 07 Nov 2024 18:15:02 -0800 From: Lu Baolu To: Joerg Roedel Cc: Jason Gunthorpe , Kevin Tian , Yi Liu , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 03/13] iommu/vt-d: Refactor the pasid setup helpers Date: Fri, 8 Nov 2024 10:13:54 +0800 Message-ID: <20241108021406.173972-4-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241108021406.173972-1-baolu.lu@linux.intel.com> References: <20241108021406.173972-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Yi Liu It is clearer to have a new set of pasid replacement helpers other than extending the existing ones to cover both initial setup and replacement. Then abstract out the common code for manipulating the pasid entry as preparation. No functional change is intended. Suggested-by: Lu Baolu Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Yi Liu Link: https://lore.kernel.org/r/20241107122234.7424-4-yi.l.liu@intel.com Signed-off-by: Lu Baolu --- drivers/iommu/intel/pasid.c | 169 ++++++++++++++++++++++-------------- 1 file changed, 105 insertions(+), 64 deletions(-) diff --git a/drivers/iommu/intel/pasid.c b/drivers/iommu/intel/pasid.c index 8d11701c2e76..6841b9892d55 100644 --- a/drivers/iommu/intel/pasid.c +++ b/drivers/iommu/intel/pasid.c @@ -324,6 +324,32 @@ static void intel_pasid_flush_present(struct intel_iom= mu *iommu, * Set up the scalable mode pasid table entry for first only * translation type. */ +static void pasid_pte_config_first_level(struct intel_iommu *iommu, + struct pasid_entry *pte, + pgd_t *pgd, u16 did, int flags) +{ + lockdep_assert_held(&iommu->lock); + + pasid_clear_entry(pte); + + /* Setup the first level page table pointer: */ + pasid_set_flptr(pte, (u64)__pa(pgd)); + + if (flags & PASID_FLAG_FL5LP) + pasid_set_flpm(pte, 1); + + if (flags & PASID_FLAG_PAGE_SNOOP) + pasid_set_pgsnp(pte); + + pasid_set_domain_id(pte, did); + pasid_set_address_width(pte, iommu->agaw); + pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); + + /* Setup Present and PASID Granular Transfer Type: */ + pasid_set_translation_type(pte, PASID_ENTRY_PGTT_FL_ONLY); + pasid_set_present(pte); +} + int intel_pasid_setup_first_level(struct intel_iommu *iommu, struct device *dev, pgd_t *pgd, u32 pasid, u16 did, int flags) @@ -354,24 +380,8 @@ int intel_pasid_setup_first_level(struct intel_iommu *= iommu, return -EBUSY; } =20 - pasid_clear_entry(pte); + pasid_pte_config_first_level(iommu, pte, pgd, did, flags); =20 - /* Setup the first level page table pointer: */ - pasid_set_flptr(pte, (u64)__pa(pgd)); - - if (flags & PASID_FLAG_FL5LP) - pasid_set_flpm(pte, 1); - - if (flags & PASID_FLAG_PAGE_SNOOP) - pasid_set_pgsnp(pte); - - pasid_set_domain_id(pte, did); - pasid_set_address_width(pte, iommu->agaw); - pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); - - /* Setup Present and PASID Granular Transfer Type: */ - pasid_set_translation_type(pte, PASID_ENTRY_PGTT_FL_ONLY); - pasid_set_present(pte); spin_unlock(&iommu->lock); =20 pasid_flush_caches(iommu, pte, pasid, did); @@ -382,6 +392,26 @@ int intel_pasid_setup_first_level(struct intel_iommu *= iommu, /* * Set up the scalable mode pasid entry for second only translation type. */ +static void pasid_pte_config_second_level(struct intel_iommu *iommu, + struct pasid_entry *pte, + u64 pgd_val, int agaw, u16 did, + bool dirty_tracking) +{ + lockdep_assert_held(&iommu->lock); + + pasid_clear_entry(pte); + pasid_set_domain_id(pte, did); + pasid_set_slptr(pte, pgd_val); + pasid_set_address_width(pte, agaw); + pasid_set_translation_type(pte, PASID_ENTRY_PGTT_SL_ONLY); + pasid_set_fault_enable(pte); + pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); + if (dirty_tracking) + pasid_set_ssade(pte); + + pasid_set_present(pte); +} + int intel_pasid_setup_second_level(struct intel_iommu *iommu, struct dmar_domain *domain, struct device *dev, u32 pasid) @@ -417,17 +447,8 @@ int intel_pasid_setup_second_level(struct intel_iommu = *iommu, return -EBUSY; } =20 - pasid_clear_entry(pte); - pasid_set_domain_id(pte, did); - pasid_set_slptr(pte, pgd_val); - pasid_set_address_width(pte, domain->agaw); - pasid_set_translation_type(pte, PASID_ENTRY_PGTT_SL_ONLY); - pasid_set_fault_enable(pte); - pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); - if (domain->dirty_tracking) - pasid_set_ssade(pte); - - pasid_set_present(pte); + pasid_pte_config_second_level(iommu, pte, pgd_val, domain->agaw, + did, domain->dirty_tracking); spin_unlock(&iommu->lock); =20 pasid_flush_caches(iommu, pte, pasid, did); @@ -507,6 +528,20 @@ int intel_pasid_setup_dirty_tracking(struct intel_iomm= u *iommu, /* * Set up the scalable mode pasid entry for passthrough translation type. */ +static void pasid_pte_config_pass_through(struct intel_iommu *iommu, + struct pasid_entry *pte, u16 did) +{ + lockdep_assert_held(&iommu->lock); + + pasid_clear_entry(pte); + pasid_set_domain_id(pte, did); + pasid_set_address_width(pte, iommu->agaw); + pasid_set_translation_type(pte, PASID_ENTRY_PGTT_PT); + pasid_set_fault_enable(pte); + pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); + pasid_set_present(pte); +} + int intel_pasid_setup_pass_through(struct intel_iommu *iommu, struct device *dev, u32 pasid) { @@ -525,13 +560,7 @@ int intel_pasid_setup_pass_through(struct intel_iommu = *iommu, return -EBUSY; } =20 - pasid_clear_entry(pte); - pasid_set_domain_id(pte, did); - pasid_set_address_width(pte, iommu->agaw); - pasid_set_translation_type(pte, PASID_ENTRY_PGTT_PT); - pasid_set_fault_enable(pte); - pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); - pasid_set_present(pte); + pasid_pte_config_pass_through(iommu, pte, did); spin_unlock(&iommu->lock); =20 pasid_flush_caches(iommu, pte, pasid, did); @@ -562,6 +591,46 @@ void intel_pasid_setup_page_snoop_control(struct intel= _iommu *iommu, intel_pasid_flush_present(iommu, dev, pasid, did, pte); } =20 +static void pasid_pte_config_nestd(struct intel_iommu *iommu, + struct pasid_entry *pte, + struct iommu_hwpt_vtd_s1 *s1_cfg, + struct dmar_domain *s2_domain, + u16 did) +{ + struct dma_pte *pgd =3D s2_domain->pgd; + + lockdep_assert_held(&iommu->lock); + + pasid_clear_entry(pte); + + if (s1_cfg->addr_width =3D=3D ADDR_WIDTH_5LEVEL) + pasid_set_flpm(pte, 1); + + pasid_set_flptr(pte, s1_cfg->pgtbl_addr); + + if (s1_cfg->flags & IOMMU_VTD_S1_SRE) { + pasid_set_sre(pte); + if (s1_cfg->flags & IOMMU_VTD_S1_WPE) + pasid_set_wpe(pte); + } + + if (s1_cfg->flags & IOMMU_VTD_S1_EAFE) + pasid_set_eafe(pte); + + if (s2_domain->force_snooping) + pasid_set_pgsnp(pte); + + pasid_set_slptr(pte, virt_to_phys(pgd)); + pasid_set_fault_enable(pte); + pasid_set_domain_id(pte, did); + pasid_set_address_width(pte, s2_domain->agaw); + pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); + if (s2_domain->dirty_tracking) + pasid_set_ssade(pte); + pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED); + pasid_set_present(pte); +} + /** * intel_pasid_setup_nested() - Set up PASID entry for nested translation. * @iommu: IOMMU which the device belong to @@ -579,7 +648,6 @@ int intel_pasid_setup_nested(struct intel_iommu *iommu,= struct device *dev, struct iommu_hwpt_vtd_s1 *s1_cfg =3D &domain->s1_cfg; struct dmar_domain *s2_domain =3D domain->s2_domain; u16 did =3D domain_id_iommu(domain, iommu); - struct dma_pte *pgd =3D s2_domain->pgd; struct pasid_entry *pte; =20 /* Address width should match the address width supported by hardware */ @@ -622,34 +690,7 @@ int intel_pasid_setup_nested(struct intel_iommu *iommu= , struct device *dev, return -EBUSY; } =20 - pasid_clear_entry(pte); - - if (s1_cfg->addr_width =3D=3D ADDR_WIDTH_5LEVEL) - pasid_set_flpm(pte, 1); - - pasid_set_flptr(pte, s1_cfg->pgtbl_addr); - - if (s1_cfg->flags & IOMMU_VTD_S1_SRE) { - pasid_set_sre(pte); - if (s1_cfg->flags & IOMMU_VTD_S1_WPE) - pasid_set_wpe(pte); - } - - if (s1_cfg->flags & IOMMU_VTD_S1_EAFE) - pasid_set_eafe(pte); - - if (s2_domain->force_snooping) - pasid_set_pgsnp(pte); - - pasid_set_slptr(pte, virt_to_phys(pgd)); - pasid_set_fault_enable(pte); - pasid_set_domain_id(pte, did); - pasid_set_address_width(pte, s2_domain->agaw); - pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); - if (s2_domain->dirty_tracking) - pasid_set_ssade(pte); - pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED); - pasid_set_present(pte); + pasid_pte_config_nestd(iommu, pte, s1_cfg, s2_domain, did); spin_unlock(&iommu->lock); =20 pasid_flush_caches(iommu, pte, pasid, did); --=20 2.43.0 From nobody Sun Nov 24 01:19:00 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 945A313B29F for ; Fri, 8 Nov 2024 02:15:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032110; cv=none; b=l2tYe7n4Yut4RSd6lVLB6L3ITB6/nRkLQANfEXMeQ/AZSb8Tuj3iI1ngNcdQZj3t5r8CQ4IIiwsWgUz2TeGGZ+WQQTC4pd0oBtgSUxjaJhjjwWwv7z4RoaxHIrGtuMrZY+dWZbKaKBF1UyEivOlSD58hWiDi+n9fORd1w9ZRdPI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032110; c=relaxed/simple; bh=Zr0PGgREOMxb9hFtNVo5tDPtF6olnwqmeazDou4TFVI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iTn6/F+cwc3Rg4Wxa86cORTkqJlz2by9aZlwva4wOubHsW1fgzJscPtDejs/ppVMV7XpO4mMq6sDLzhAypJ+VFwKgRC1IDralVy8tux87SXZmXQ1a+x3dNzfWS/u5ASIVnqqgSWY8QLMjZgWGKSpPR8WPNVKd/UfxRGku71GZ3I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=CUUIO+sZ; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="CUUIO+sZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731032109; x=1762568109; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Zr0PGgREOMxb9hFtNVo5tDPtF6olnwqmeazDou4TFVI=; b=CUUIO+sZUNiSmI0Mjx1JiaXOEvs1x0gwe2VTdOHF0FzDaXdMKdnge5bY Xw+kzk8greSGROuo3DDmCdQmOzBm2Ls+wkjg4qfyfAN0l+83XzoC385lt P6cUmLhXmvwhKxb8Qt07yguph26T7AAKiZftg0A8BKLLGzp00oQvujHQr SYIHjcgG+snP0faC71aiagetnpcVP98YgrMcWf2FnNyD+QsguBiApuq/4 uzmpkc90K3MwHRPGkvUhWSrguRRqU8KX9EHS8IjJ1wkW8YqPsAav0pCsY LU1rsqePKCU4y4PYAYdUVfTVkYGaTQP21Z/TlflEIbJMXIZEYa/I41G/4 A==; X-CSE-ConnectionGUID: bZkA7DXARB6H/InM6/9Ljw== X-CSE-MsgGUID: OEwjlSQxSDiGK9WZvcArEQ== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="31007770" X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="31007770" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 18:15:07 -0800 X-CSE-ConnectionGUID: pBxeondQQ4a2MsjaeJ6CAA== X-CSE-MsgGUID: OUGQDDkCRsyghUheQs2ztA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="116213780" Received: from allen-sbox.sh.intel.com ([10.239.159.30]) by fmviesa001.fm.intel.com with ESMTP; 07 Nov 2024 18:15:05 -0800 From: Lu Baolu To: Joerg Roedel Cc: Jason Gunthorpe , Kevin Tian , Yi Liu , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 04/13] iommu/vt-d: Add pasid replace helpers Date: Fri, 8 Nov 2024 10:13:55 +0800 Message-ID: <20241108021406.173972-5-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241108021406.173972-1-baolu.lu@linux.intel.com> References: <20241108021406.173972-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Yi Liu pasid replacement allows converting a present pasid entry to be FS, SS, PT or nested, hence add helpers for such operations. Suggested-by: Lu Baolu Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Yi Liu Link: https://lore.kernel.org/r/20241107122234.7424-5-yi.l.liu@intel.com Signed-off-by: Lu Baolu --- drivers/iommu/intel/pasid.c | 190 ++++++++++++++++++++++++++++++++++++ drivers/iommu/intel/pasid.h | 15 +++ 2 files changed, 205 insertions(+) diff --git a/drivers/iommu/intel/pasid.c b/drivers/iommu/intel/pasid.c index 6841b9892d55..0f2a926d3bd5 100644 --- a/drivers/iommu/intel/pasid.c +++ b/drivers/iommu/intel/pasid.c @@ -389,6 +389,50 @@ int intel_pasid_setup_first_level(struct intel_iommu *= iommu, return 0; } =20 +int intel_pasid_replace_first_level(struct intel_iommu *iommu, + struct device *dev, pgd_t *pgd, + u32 pasid, u16 did, u16 old_did, + int flags) +{ + struct pasid_entry *pte, new_pte; + + if (!ecap_flts(iommu->ecap)) { + pr_err("No first level translation support on %s\n", + iommu->name); + return -EINVAL; + } + + if ((flags & PASID_FLAG_FL5LP) && !cap_fl5lp_support(iommu->cap)) { + pr_err("No 5-level paging support for first-level on %s\n", + iommu->name); + return -EINVAL; + } + + pasid_pte_config_first_level(iommu, &new_pte, pgd, did, flags); + + spin_lock(&iommu->lock); + pte =3D intel_pasid_get_entry(dev, pasid); + if (!pte) { + spin_unlock(&iommu->lock); + return -ENODEV; + } + + if (!pasid_pte_is_present(pte)) { + spin_unlock(&iommu->lock); + return -EINVAL; + } + + WARN_ON(old_did !=3D pasid_get_domain_id(pte)); + + *pte =3D new_pte; + spin_unlock(&iommu->lock); + + intel_pasid_flush_present(iommu, dev, pasid, old_did, pte); + intel_iommu_drain_pasid_prq(dev, pasid); + + return 0; +} + /* * Set up the scalable mode pasid entry for second only translation type. */ @@ -456,6 +500,57 @@ int intel_pasid_setup_second_level(struct intel_iommu = *iommu, return 0; } =20 +int intel_pasid_replace_second_level(struct intel_iommu *iommu, + struct dmar_domain *domain, + struct device *dev, u16 old_did, + u32 pasid) +{ + struct pasid_entry *pte, new_pte; + struct dma_pte *pgd; + u64 pgd_val; + u16 did; + + /* + * If hardware advertises no support for second level + * translation, return directly. + */ + if (!ecap_slts(iommu->ecap)) { + pr_err("No second level translation support on %s\n", + iommu->name); + return -EINVAL; + } + + pgd =3D domain->pgd; + pgd_val =3D virt_to_phys(pgd); + did =3D domain_id_iommu(domain, iommu); + + pasid_pte_config_second_level(iommu, &new_pte, pgd_val, + domain->agaw, did, + domain->dirty_tracking); + + spin_lock(&iommu->lock); + pte =3D intel_pasid_get_entry(dev, pasid); + if (!pte) { + spin_unlock(&iommu->lock); + return -ENODEV; + } + + if (!pasid_pte_is_present(pte)) { + spin_unlock(&iommu->lock); + return -EINVAL; + } + + WARN_ON(old_did !=3D pasid_get_domain_id(pte)); + + *pte =3D new_pte; + spin_unlock(&iommu->lock); + + intel_pasid_flush_present(iommu, dev, pasid, old_did, pte); + intel_iommu_drain_pasid_prq(dev, pasid); + + return 0; +} + /* * Set up dirty tracking on a second only or nested translation type. */ @@ -568,6 +663,38 @@ int intel_pasid_setup_pass_through(struct intel_iommu = *iommu, return 0; } =20 +int intel_pasid_replace_pass_through(struct intel_iommu *iommu, + struct device *dev, u16 old_did, + u32 pasid) +{ + struct pasid_entry *pte, new_pte; + u16 did =3D FLPT_DEFAULT_DID; + + pasid_pte_config_pass_through(iommu, &new_pte, did); + + spin_lock(&iommu->lock); + pte =3D intel_pasid_get_entry(dev, pasid); + if (!pte) { + spin_unlock(&iommu->lock); + return -ENODEV; + } + + if (!pasid_pte_is_present(pte)) { + spin_unlock(&iommu->lock); + return -EINVAL; + } + + WARN_ON(old_did !=3D pasid_get_domain_id(pte)); + + *pte =3D new_pte; + spin_unlock(&iommu->lock); + + intel_pasid_flush_present(iommu, dev, pasid, old_did, pte); + intel_iommu_drain_pasid_prq(dev, pasid); + + return 0; +} + /* * Set the page snoop control for a pasid entry which has been set up. */ @@ -698,6 +825,69 @@ int intel_pasid_setup_nested(struct intel_iommu *iommu= , struct device *dev, return 0; } =20 +int intel_pasid_replace_nested(struct intel_iommu *iommu, + struct device *dev, u32 pasid, + u16 old_did, struct dmar_domain *domain) +{ + struct iommu_hwpt_vtd_s1 *s1_cfg =3D &domain->s1_cfg; + struct dmar_domain *s2_domain =3D domain->s2_domain; + u16 did =3D domain_id_iommu(domain, iommu); + struct pasid_entry *pte, new_pte; + + /* Address width should match the address width supported by hardware */ + switch (s1_cfg->addr_width) { + case ADDR_WIDTH_4LEVEL: + break; + case ADDR_WIDTH_5LEVEL: + if (!cap_fl5lp_support(iommu->cap)) { + dev_err_ratelimited(dev, + "5-level paging not supported\n"); + return -EINVAL; + } + break; + default: + dev_err_ratelimited(dev, "Invalid stage-1 address width %d\n", + s1_cfg->addr_width); + return -EINVAL; + } + + if ((s1_cfg->flags & IOMMU_VTD_S1_SRE) && !ecap_srs(iommu->ecap)) { + pr_err_ratelimited("No supervisor request support on %s\n", + iommu->name); + return -EINVAL; + } + + if ((s1_cfg->flags & IOMMU_VTD_S1_EAFE) && !ecap_eafs(iommu->ecap)) { + pr_err_ratelimited("No extended access flag support on %s\n", + iommu->name); + return -EINVAL; + } + + pasid_pte_config_nestd(iommu, &new_pte, s1_cfg, s2_domain, did); + + spin_lock(&iommu->lock); + pte =3D intel_pasid_get_entry(dev, pasid); + if (!pte) { + spin_unlock(&iommu->lock); + return -ENODEV; + } + + if (!pasid_pte_is_present(pte)) { + spin_unlock(&iommu->lock); + return -EINVAL; + } + + WARN_ON(old_did !=3D pasid_get_domain_id(pte)); + + *pte =3D new_pte; + spin_unlock(&iommu->lock); + + intel_pasid_flush_present(iommu, dev, pasid, old_did, pte); + intel_iommu_drain_pasid_prq(dev, pasid); + + return 0; +} + /* * Interfaces to setup or teardown a pasid table to the scalable-mode * context table entry: diff --git a/drivers/iommu/intel/pasid.h b/drivers/iommu/intel/pasid.h index dde6d3ba5ae0..06d1f7006d01 100644 --- a/drivers/iommu/intel/pasid.h +++ b/drivers/iommu/intel/pasid.h @@ -303,6 +303,21 @@ int intel_pasid_setup_pass_through(struct intel_iommu = *iommu, struct device *dev, u32 pasid); int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev, u32 pasid, struct dmar_domain *domain); +int intel_pasid_replace_first_level(struct intel_iommu *iommu, + struct device *dev, pgd_t *pgd, + u32 pasid, u16 did, u16 old_did, + int flags); +int intel_pasid_replace_second_level(struct intel_iommu *iommu, + struct dmar_domain *domain, + struct device *dev, u16 old_did, + u32 pasid); +int intel_pasid_replace_pass_through(struct intel_iommu *iommu, + struct device *dev, u16 old_did, + u32 pasid); +int intel_pasid_replace_nested(struct intel_iommu *iommu, + struct device *dev, u32 pasid, + u16 old_did, struct dmar_domain *domain); + void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev, u32 pasid, bool fault_ignore); --=20 2.43.0 From nobody Sun Nov 24 01:19:00 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7AA5A142E7C for ; Fri, 8 Nov 2024 02:15:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032111; cv=none; b=Nvu48iRQiS+Yr7smwiOwGEmOqgz+Zoq0JtC9AEi/lQHeXS6dttiSYyQioaAlY9YvFYHo6pVb/ep/ouuaRSFHw+lMPjpqTza8lX38xXnA1YcRIR6GRdUeChbXUrQrDrk2P06nAW8p1+ehH+sAA7LvPNihcb5Z8/CuCGhPlJnrwNE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032111; c=relaxed/simple; bh=Ad/Uyrw6QTC4tKg0asmcSQrfUTh9tXUBj5y09i8V0hQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AinPz69s2RVwPEZMWCmCaT0hkIFAdkZBKWWpR5jt+0EiMQMS0W6TspAdQ3aoBVdhvEzrc9U6bn629NcCs8rzG+F+T2JU+7LFdbStwNAIMonwpORhZ0xuvvt6t6DdYkBTC+fYzIue+z2Pki8kR8cjYKGpuzHVU9jEk5aS+baJLMg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Z/9c1cYi; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Z/9c1cYi" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731032110; x=1762568110; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ad/Uyrw6QTC4tKg0asmcSQrfUTh9tXUBj5y09i8V0hQ=; b=Z/9c1cYiMJSKTOwAw7Yn9K4l97o7kzrKI010Ib8Iy+RDZgm8OjC4yOZp tgESdgP5+H8v/hs6TH9HtSwcGF9dymW451SdTgxJPNbC+E7eRWfrZ3SlF D6ZJ2Fcxhr12cRjvOTR1FKiCbdTueY6IpHE/jfPaG1WfEHnWi4GR8m8Ac 124usNEncOHxQCrgGON2nH4EZrLZudi9n0j837tXotVmgGAxysnMgp0LR 3HsUUmzHAxGa5hVRxyAzwDJRzemru7TvBCCYv4i1v5BNv1O32UNOeQwGw J57+8t8jb0UCguC56nLwgX17thFqBsdC7eCEf2n9/eBUi/J4id6vpZJ6Y A==; X-CSE-ConnectionGUID: GkmMW6GVSUmmMfdjLFKJXQ== X-CSE-MsgGUID: NKqeaHmNSrW5Bc6bk0Sxpw== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="31007780" X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="31007780" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 18:15:09 -0800 X-CSE-ConnectionGUID: +wdbPTMmTn+nw2Xs5hdhAQ== X-CSE-MsgGUID: Ag5cTilBRBWcIitVZHzGBQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="116213791" Received: from allen-sbox.sh.intel.com ([10.239.159.30]) by fmviesa001.fm.intel.com with ESMTP; 07 Nov 2024 18:15:06 -0800 From: Lu Baolu To: Joerg Roedel Cc: Jason Gunthorpe , Kevin Tian , Yi Liu , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 05/13] iommu/vt-d: Consolidate the struct dev_pasid_info add/remove Date: Fri, 8 Nov 2024 10:13:56 +0800 Message-ID: <20241108021406.173972-6-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241108021406.173972-1-baolu.lu@linux.intel.com> References: <20241108021406.173972-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Yi Liu The domain_add_dev_pasid() and domain_remove_dev_pasid() are added to consolidate the adding/removing of the struct dev_pasid_info. Besides, it includes the cache tag assign/unassign as well. This also prepares for adding domain replacement for pasid. The set_dev_pasid callbacks need to deal with the dev_pasid_info for both old and new domain. These two helpers make the life easier. intel_iommu_set_dev_pasid() and intel_svm_set_dev_pasid() are updated to use the helpers. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Yi Liu Link: https://lore.kernel.org/r/20241107122234.7424-6-yi.l.liu@intel.com Signed-off-by: Lu Baolu --- drivers/iommu/intel/iommu.c | 101 +++++++++++++++++++++++------------- drivers/iommu/intel/iommu.h | 6 +++ drivers/iommu/intel/svm.c | 28 +++------- 3 files changed, 79 insertions(+), 56 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 2d1d208d5125..103b109e23a9 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -4036,8 +4036,8 @@ static int intel_iommu_iotlb_sync_map(struct iommu_do= main *domain, return 0; } =20 -static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasi= d, - struct iommu_domain *domain) +void domain_remove_dev_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid) { struct device_domain_info *info =3D dev_iommu_priv_get(dev); struct dev_pasid_info *curr, *dev_pasid =3D NULL; @@ -4045,10 +4045,12 @@ static void intel_iommu_remove_dev_pasid(struct dev= ice *dev, ioasid_t pasid, struct dmar_domain *dmar_domain; unsigned long flags; =20 - if (domain->type =3D=3D IOMMU_DOMAIN_IDENTITY) { - intel_pasid_tear_down_entry(iommu, dev, pasid, false); + if (!domain) + return; + + /* Identity domain has no meta data for pasid. */ + if (domain->type =3D=3D IOMMU_DOMAIN_IDENTITY) return; - } =20 dmar_domain =3D to_dmar_domain(domain); spin_lock_irqsave(&dmar_domain->lock, flags); @@ -4066,12 +4068,20 @@ static void intel_iommu_remove_dev_pasid(struct dev= ice *dev, ioasid_t pasid, domain_detach_iommu(dmar_domain, iommu); intel_iommu_debugfs_remove_dev_pasid(dev_pasid); kfree(dev_pasid); - intel_pasid_tear_down_entry(iommu, dev, pasid, false); } =20 -static int intel_iommu_set_dev_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t pasid, - struct iommu_domain *old) +static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasi= d, + struct iommu_domain *domain) +{ + struct device_domain_info *info =3D dev_iommu_priv_get(dev); + + intel_pasid_tear_down_entry(info->iommu, dev, pasid, false); + domain_remove_dev_pasid(domain, dev, pasid); +} + +struct dev_pasid_info * +domain_add_dev_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid) { struct device_domain_info *info =3D dev_iommu_priv_get(dev); struct dmar_domain *dmar_domain =3D to_dmar_domain(domain); @@ -4080,22 +4090,9 @@ static int intel_iommu_set_dev_pasid(struct iommu_do= main *domain, unsigned long flags; int ret; =20 - if (!pasid_supported(iommu) || dev_is_real_dma_subdevice(dev)) - return -EOPNOTSUPP; - - if (domain->dirty_ops) - return -EINVAL; - - if (context_copied(iommu, info->bus, info->devfn)) - return -EBUSY; - - ret =3D paging_domain_compatible(domain, dev); - if (ret) - return ret; - dev_pasid =3D kzalloc(sizeof(*dev_pasid), GFP_KERNEL); if (!dev_pasid) - return -ENOMEM; + return ERR_PTR(-ENOMEM); =20 ret =3D domain_attach_iommu(dmar_domain, iommu); if (ret) @@ -4105,6 +4102,47 @@ static int intel_iommu_set_dev_pasid(struct iommu_do= main *domain, if (ret) goto out_detach_iommu; =20 + dev_pasid->dev =3D dev; + dev_pasid->pasid =3D pasid; + spin_lock_irqsave(&dmar_domain->lock, flags); + list_add(&dev_pasid->link_domain, &dmar_domain->dev_pasids); + spin_unlock_irqrestore(&dmar_domain->lock, flags); + + return dev_pasid; +out_detach_iommu: + domain_detach_iommu(dmar_domain, iommu); +out_free: + kfree(dev_pasid); + return ERR_PTR(ret); +} + +static int intel_iommu_set_dev_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid, + struct iommu_domain *old) +{ + struct device_domain_info *info =3D dev_iommu_priv_get(dev); + struct dmar_domain *dmar_domain =3D to_dmar_domain(domain); + struct intel_iommu *iommu =3D info->iommu; + struct dev_pasid_info *dev_pasid; + int ret; + + if (!pasid_supported(iommu) || dev_is_real_dma_subdevice(dev)) + return -EOPNOTSUPP; + + if (domain->dirty_ops) + return -EINVAL; + + if (context_copied(iommu, info->bus, info->devfn)) + return -EBUSY; + + ret =3D paging_domain_compatible(domain, dev); + if (ret) + return ret; + + dev_pasid =3D domain_add_dev_pasid(domain, dev, pasid); + if (IS_ERR(dev_pasid)) + return PTR_ERR(dev_pasid); + if (dmar_domain->use_first_level) ret =3D domain_setup_first_level(iommu, dmar_domain, dev, pasid); @@ -4112,24 +4150,17 @@ static int intel_iommu_set_dev_pasid(struct iommu_d= omain *domain, ret =3D intel_pasid_setup_second_level(iommu, dmar_domain, dev, pasid); if (ret) - goto out_unassign_tag; + goto out_remove_dev_pasid; =20 - dev_pasid->dev =3D dev; - dev_pasid->pasid =3D pasid; - spin_lock_irqsave(&dmar_domain->lock, flags); - list_add(&dev_pasid->link_domain, &dmar_domain->dev_pasids); - spin_unlock_irqrestore(&dmar_domain->lock, flags); + domain_remove_dev_pasid(old, dev, pasid); =20 if (domain->type & __IOMMU_DOMAIN_PAGING) intel_iommu_debugfs_create_dev_pasid(dev_pasid); =20 return 0; -out_unassign_tag: - cache_tag_unassign_domain(dmar_domain, dev, pasid); -out_detach_iommu: - domain_detach_iommu(dmar_domain, iommu); -out_free: - kfree(dev_pasid); + +out_remove_dev_pasid: + domain_remove_dev_pasid(domain, dev, pasid); return ret; } =20 diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index b3912633ce25..df0261e03fad 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -1228,6 +1228,12 @@ void domain_detach_iommu(struct dmar_domain *domain,= struct intel_iommu *iommu); void device_block_translation(struct device *dev); int paging_domain_compatible(struct iommu_domain *domain, struct device *d= ev); =20 +struct dev_pasid_info * +domain_add_dev_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid); +void domain_remove_dev_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid); + int dmar_ir_support(void); =20 void iommu_flush_write_buffer(struct intel_iommu *iommu); diff --git a/drivers/iommu/intel/svm.c b/drivers/iommu/intel/svm.c index 4a2bd65614ad..6c0685ea8466 100644 --- a/drivers/iommu/intel/svm.c +++ b/drivers/iommu/intel/svm.c @@ -115,43 +115,29 @@ static int intel_svm_set_dev_pasid(struct iommu_domai= n *domain, struct iommu_domain *old) { struct device_domain_info *info =3D dev_iommu_priv_get(dev); - struct dmar_domain *dmar_domain =3D to_dmar_domain(domain); struct intel_iommu *iommu =3D info->iommu; struct mm_struct *mm =3D domain->mm; struct dev_pasid_info *dev_pasid; unsigned long sflags; - unsigned long flags; int ret =3D 0; =20 - dev_pasid =3D kzalloc(sizeof(*dev_pasid), GFP_KERNEL); - if (!dev_pasid) - return -ENOMEM; - - dev_pasid->dev =3D dev; - dev_pasid->pasid =3D pasid; - - ret =3D cache_tag_assign_domain(to_dmar_domain(domain), dev, pasid); - if (ret) - goto free_dev_pasid; + dev_pasid =3D domain_add_dev_pasid(domain, dev, pasid); + if (IS_ERR(dev_pasid)) + return PTR_ERR(dev_pasid); =20 /* Setup the pasid table: */ sflags =3D cpu_feature_enabled(X86_FEATURE_LA57) ? PASID_FLAG_FL5LP : 0; ret =3D intel_pasid_setup_first_level(iommu, dev, mm->pgd, pasid, FLPT_DEFAULT_DID, sflags); if (ret) - goto unassign_tag; + goto out_remove_dev_pasid; =20 - spin_lock_irqsave(&dmar_domain->lock, flags); - list_add(&dev_pasid->link_domain, &dmar_domain->dev_pasids); - spin_unlock_irqrestore(&dmar_domain->lock, flags); + domain_remove_dev_pasid(old, dev, pasid); =20 return 0; =20 -unassign_tag: - cache_tag_unassign_domain(to_dmar_domain(domain), dev, pasid); -free_dev_pasid: - kfree(dev_pasid); - +out_remove_dev_pasid: + domain_remove_dev_pasid(domain, dev, pasid); return ret; } =20 --=20 2.43.0 From nobody Sun Nov 24 01:19:00 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46BEE3FB31 for ; Fri, 8 Nov 2024 02:15:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032113; cv=none; b=VbvG257I0WD6POpAFwfWfk2KeukGwyeqlvnLqH+qVe3vse72kwcq1YjohiXlPBaB+sKkiXs+ZmXD2d2JkYK/6T9v8+9PNtxbIGfSS979zP5MwIsPRcn/+ngKy/6AgInvThPpMU+E9vkc2yqRmENig81fXUuJewVkcdzpBdCNYk4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032113; c=relaxed/simple; bh=LQpx7CSmSpTBh60RHww69MTJ763Qtw2J6tMyE6wshwU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=esN6ZFowHin30nh5u05QOSj2mbBA+vPxV4XBH4gWPBk0xunPKr1QmPubNRGtVulO8uWAb2cGLafeRrb5LHiveC11x+hCBTkU6mWZBvlJtzDXRh4IbGduhoxIQxntG36dOFLGFvXEnu+KgE28qp9tcPuoNyRA47iVIibK6b/lX48= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=SIRt4RXG; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="SIRt4RXG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731032111; x=1762568111; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LQpx7CSmSpTBh60RHww69MTJ763Qtw2J6tMyE6wshwU=; b=SIRt4RXGiNrqcBHBIgSlmSQdf80r89VPqdb3HFH4ILMBcVoyElJs+Or2 S+q2vLzUXLrp0Pp826eb31HJAWCQlX4o4vxJxzRbb/HPu3Y7F/cGTy9rW wCUDOR66pFi2PXo1t+x5+E5Ur3LlcI7IBghJDQEPKpe04c985FQkO4ZH2 ivQXjXvNheWIiVxSnqHouwc6yiV1j3kg0MmaoBx09OQVTLwa18JnH1gJ/ UIqsds5ZZq1j1eiXZSRVg8Cy28jiCTqK85derS8B257jnvDxhV5YhhCAt 1njXibK5XinDYqu+j3q/aL1FgBjfKB2yBP6P3BBWUd0GSu3iiqNNWdKzC w==; X-CSE-ConnectionGUID: SiVwyE61R2e5ISC+Vhlx3w== X-CSE-MsgGUID: 76RNQTIeRpiB8JkmVuic2w== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="31007794" X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="31007794" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 18:15:10 -0800 X-CSE-ConnectionGUID: IF9fWNraSY+lul1foHWNcQ== X-CSE-MsgGUID: H1Go0SzvR8qaStFPqcuCaQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="116213803" Received: from allen-sbox.sh.intel.com ([10.239.159.30]) by fmviesa001.fm.intel.com with ESMTP; 07 Nov 2024 18:15:08 -0800 From: Lu Baolu To: Joerg Roedel Cc: Jason Gunthorpe , Kevin Tian , Yi Liu , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 06/13] iommu/vt-d: Add iommu_domain_did() to get did Date: Fri, 8 Nov 2024 10:13:57 +0800 Message-ID: <20241108021406.173972-7-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241108021406.173972-1-baolu.lu@linux.intel.com> References: <20241108021406.173972-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Yi Liu domain_id_iommu() does not support SVA type and identity type domains. Add iommu_domain_did() to support all domain types. Signed-off-by: Yi Liu Link: https://lore.kernel.org/r/20241107122234.7424-7-yi.l.liu@intel.com Signed-off-by: Lu Baolu --- drivers/iommu/intel/iommu.h | 16 ++++++++++++++++ drivers/iommu/intel/pasid.h | 7 ------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index df0261e03fad..cdca7d5061a7 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -806,6 +806,13 @@ static inline struct dmar_domain *to_dmar_domain(struc= t iommu_domain *dom) return container_of(dom, struct dmar_domain, domain); } =20 +/* + * Domain ID reserved for pasid entries programmed for first-level + * only and pass-through transfer modes. + */ +#define FLPT_DEFAULT_DID 1 +#define NUM_RESERVED_DID 2 + /* Retrieve the domain ID which has allocated to the domain */ static inline u16 domain_id_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) @@ -816,6 +823,15 @@ domain_id_iommu(struct dmar_domain *domain, struct int= el_iommu *iommu) return info->did; } =20 +static inline u16 +iommu_domain_did(struct iommu_domain *domain, struct intel_iommu *iommu) +{ + if (domain->type =3D=3D IOMMU_DOMAIN_SVA || + domain->type =3D=3D IOMMU_DOMAIN_IDENTITY) + return FLPT_DEFAULT_DID; + return domain_id_iommu(to_dmar_domain(domain), iommu); +} + /* * 0: readable * 1: writable diff --git a/drivers/iommu/intel/pasid.h b/drivers/iommu/intel/pasid.h index 06d1f7006d01..082f4fe20216 100644 --- a/drivers/iommu/intel/pasid.h +++ b/drivers/iommu/intel/pasid.h @@ -22,13 +22,6 @@ #define is_pasid_enabled(entry) (((entry)->lo >> 3) & 0x1) #define get_pasid_dir_size(entry) (1 << ((((entry)->lo >> 9) & 0x7) + 7)) =20 -/* - * Domain ID reserved for pasid entries programmed for first-level - * only and pass-through transfer modes. - */ -#define FLPT_DEFAULT_DID 1 -#define NUM_RESERVED_DID 2 - #define PASID_FLAG_NESTED BIT(1) #define PASID_FLAG_PAGE_SNOOP BIT(2) =20 --=20 2.43.0 From nobody Sun Nov 24 01:19:00 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BDCD21865EB for ; Fri, 8 Nov 2024 02:15:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032114; cv=none; b=P9/NvuR5oHSGkGD3niIWugYisOX6EmC7m+I4HoEhk3N5cAtwN5aQvSPsrqaw6R6NGjc88dw2gWJQ/8pb6m2H26nulGAc+6pIzcIaKcaQ3hPZNy3CNFQFEMwYxrWR8WPmAaiGxUW4puSCSkNB1Fwd0BVXu17EOBffP1iCtW94bM0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032114; c=relaxed/simple; bh=G+Y8J+0WNqmr3G0Kd7RRpjHuNjXAsp1Hy5kAf8nBlMs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QhOpN/U2zhdNMaEIILgRBvGtEYzauk9BlWAN4iBr50KB+tB786s8I9A4XrMXPJh/68ctEjfjh+FSAqVMliWq55g4A0c6E1CT3eIg9OBW7xMdEe3ycefx+qYPXDg+9DECONGM/7Cpqug0/kABRJrpPmn8Dx0IXIPgNMDOMGEpY7A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Yw3Xejfy; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Yw3Xejfy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731032113; x=1762568113; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=G+Y8J+0WNqmr3G0Kd7RRpjHuNjXAsp1Hy5kAf8nBlMs=; b=Yw3XejfyUSs0SpgGzUkF5o3VS+SHbB4gi5A6g74Jgf50ozCclhlyfE9X Fktx6EUIl05VO7I46z5L8m/5i3b44TAxe4NiAl1d6dKhwS0f40s+1y9d/ q2egrCJtM9pQPtTzfX5oBOSD1ciHNZqDhWojTVviGJE1evFr+3qyOXMIz FN8zBLQhvX/jYFr7NnBAMjV3oyfoocMsXqH0/F5TmFVYi6RLThhB7wugO yf3sZQDEEFUBkyVpeeSw3hXXjRLjsXzEhwif8Nm1tMYU1qDFBLtPz6/8Z qA2mP7ALXduZg/jXjnsZqdfBPTmvfwtAgLH61ZVFDG/Iqcpm64IIwT3qN w==; X-CSE-ConnectionGUID: bIhlvpS1Qq2v5kpcxbksDw== X-CSE-MsgGUID: ygqAhFaUTSufGE6OROlgqw== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="31007806" X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="31007806" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 18:15:12 -0800 X-CSE-ConnectionGUID: z2cZ34OcQWe+XflbuiLLvQ== X-CSE-MsgGUID: iX6aAnVMTeGLi7lSmAl89A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="116213812" Received: from allen-sbox.sh.intel.com ([10.239.159.30]) by fmviesa001.fm.intel.com with ESMTP; 07 Nov 2024 18:15:10 -0800 From: Lu Baolu To: Joerg Roedel Cc: Jason Gunthorpe , Kevin Tian , Yi Liu , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 07/13] iommu/vt-d: Make intel_iommu_set_dev_pasid() to handle domain replacement Date: Fri, 8 Nov 2024 10:13:58 +0800 Message-ID: <20241108021406.173972-8-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241108021406.173972-1-baolu.lu@linux.intel.com> References: <20241108021406.173972-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Yi Liu Let intel_iommu_set_dev_pasid() call the pasid replace helpers hence be able to do domain replacement. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Yi Liu Link: https://lore.kernel.org/r/20241107122234.7424-8-yi.l.liu@intel.com Signed-off-by: Lu Baolu --- drivers/iommu/intel/iommu.c | 46 +++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 103b109e23a9..d19bd0ff5220 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -1752,10 +1752,36 @@ static void domain_context_clear_one(struct device_= domain_info *info, u8 bus, u8 intel_context_flush_present(info, context, did, true); } =20 +static int __domain_setup_first_level(struct intel_iommu *iommu, + struct device *dev, ioasid_t pasid, + u16 did, pgd_t *pgd, int flags, + struct iommu_domain *old) +{ + if (!old) + return intel_pasid_setup_first_level(iommu, dev, pgd, + pasid, did, flags); + return intel_pasid_replace_first_level(iommu, dev, pgd, pasid, did, + iommu_domain_did(old, iommu), + flags); +} + +static int domain_setup_second_level(struct intel_iommu *iommu, + struct dmar_domain *domain, + struct device *dev, ioasid_t pasid, + struct iommu_domain *old) +{ + if (!old) + return intel_pasid_setup_second_level(iommu, domain, + dev, pasid); + return intel_pasid_replace_second_level(iommu, domain, dev, + iommu_domain_did(old, iommu), + pasid); +} + static int domain_setup_first_level(struct intel_iommu *iommu, struct dmar_domain *domain, struct device *dev, - u32 pasid) + u32 pasid, struct iommu_domain *old) { struct dma_pte *pgd =3D domain->pgd; int level, flags =3D 0; @@ -1770,9 +1796,9 @@ static int domain_setup_first_level(struct intel_iomm= u *iommu, if (domain->force_snooping) flags |=3D PASID_FLAG_PAGE_SNOOP; =20 - return intel_pasid_setup_first_level(iommu, dev, (pgd_t *)pgd, pasid, - domain_id_iommu(domain, iommu), - flags); + return __domain_setup_first_level(iommu, dev, pasid, + domain_id_iommu(domain, iommu), + (pgd_t *)pgd, flags, old); } =20 static bool dev_is_real_dma_subdevice(struct device *dev) @@ -1804,9 +1830,11 @@ static int dmar_domain_attach_device(struct dmar_dom= ain *domain, if (!sm_supported(iommu)) ret =3D domain_context_mapping(domain, dev); else if (domain->use_first_level) - ret =3D domain_setup_first_level(iommu, domain, dev, IOMMU_NO_PASID); + ret =3D domain_setup_first_level(iommu, domain, dev, + IOMMU_NO_PASID, NULL); else - ret =3D intel_pasid_setup_second_level(iommu, domain, dev, IOMMU_NO_PASI= D); + ret =3D domain_setup_second_level(iommu, domain, dev, + IOMMU_NO_PASID, NULL); =20 if (ret) goto out_block_translation; @@ -4145,10 +4173,10 @@ static int intel_iommu_set_dev_pasid(struct iommu_d= omain *domain, =20 if (dmar_domain->use_first_level) ret =3D domain_setup_first_level(iommu, dmar_domain, - dev, pasid); + dev, pasid, old); else - ret =3D intel_pasid_setup_second_level(iommu, dmar_domain, - dev, pasid); + ret =3D domain_setup_second_level(iommu, dmar_domain, + dev, pasid, old); if (ret) goto out_remove_dev_pasid; =20 --=20 2.43.0 From nobody Sun Nov 24 01:19:00 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D0A1208DA for ; Fri, 8 Nov 2024 02:15:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032127; cv=none; b=Yav4Th4XuZqE05VpbLBy7L/SL8dCAybRaW1eefgHqY/vADiaO1qmLGoWbbX2GFA9/uVQjx4P+x68OkKcLwUJZLyR6LeVbupMSkbpHprTV/LOsnxoiIgmgObrWJ0rVvDByL3A4u+pBnyBPpN9JMxxC4da/CwuurBB+VSQPK4SFLw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032127; c=relaxed/simple; bh=hqsP3BN5O7/9a9dPUsicIXA08bRbbQ6xZ0CYe/p9c5U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eBBNEqPxGmk6IecEkpaM6C3YbNkR2o4yn9nzuLITwGhpZS6PZMorT40mGQul+bdEa4YE1IqNRVZC5CKcguqyKvx0v24tVUKVHFnP5tEYgqiXzODV+thD48Dt0yDEGMm1las2QjLpqvhTFmSlCRGREYBsNQaFHTcYzLx0cZyvGLg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=InQCSps0; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="InQCSps0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731032127; x=1762568127; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hqsP3BN5O7/9a9dPUsicIXA08bRbbQ6xZ0CYe/p9c5U=; b=InQCSps0suIZVbJ8gGOTGYkpQWFHNtS87i5BgbUkPUOq4Zrk/M0a+Olz kAxgSsh5XJsNSAn/0MIxaBalj2jaUx0jpwnW4sSbdvc7CEpeYHWTcOXLm rhCBmLB6iupC3PmhjllncZDHujRrLp1qodCMHBj3Chstuc8/iFgK0ns1B m0rMupxxqLXXbhyjOgipgcsy1hmyJ2RqS0U3fuX+XCvj1zJNGkNZNuGbg RV9psA5esEkB9NdsViqlHonwHLFiSEzgXaPL9fVRjsg/+4jB1MZnEwjjh YfuJF+3gogSP6pU4cFe+IkSR5/XoDiyLFcUhwFT0VP3MbST6IMcqldHsa Q==; X-CSE-ConnectionGUID: VtuEyb0mRJmIL7dOzp8k+w== X-CSE-MsgGUID: liSVVHZkS9mFW0Euo3cHSA== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="31007844" X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="31007844" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 18:15:20 -0800 X-CSE-ConnectionGUID: fHDIbN78S0GJR9NDru8BkQ== X-CSE-MsgGUID: rfq6MEEhQGSXEpsZ0zQTNA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="116213818" Received: from allen-sbox.sh.intel.com ([10.239.159.30]) by fmviesa001.fm.intel.com with ESMTP; 07 Nov 2024 18:15:12 -0800 From: Lu Baolu To: Joerg Roedel Cc: Jason Gunthorpe , Kevin Tian , Yi Liu , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 08/13] iommu/vt-d: Limit intel_iommu_set_dev_pasid() for paging domain Date: Fri, 8 Nov 2024 10:13:59 +0800 Message-ID: <20241108021406.173972-9-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241108021406.173972-1-baolu.lu@linux.intel.com> References: <20241108021406.173972-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Yi Liu intel_iommu_set_dev_pasid() is only supposed to be used by paging domain, so limit it. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Yi Liu Link: https://lore.kernel.org/r/20241107122234.7424-9-yi.l.liu@intel.com Signed-off-by: Lu Baolu --- drivers/iommu/intel/iommu.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index d19bd0ff5220..f3ccbe516dcb 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -4154,6 +4154,9 @@ static int intel_iommu_set_dev_pasid(struct iommu_dom= ain *domain, struct dev_pasid_info *dev_pasid; int ret; =20 + if (WARN_ON_ONCE(!(domain->type & __IOMMU_DOMAIN_PAGING))) + return -EINVAL; + if (!pasid_supported(iommu) || dev_is_real_dma_subdevice(dev)) return -EOPNOTSUPP; =20 @@ -4182,8 +4185,7 @@ static int intel_iommu_set_dev_pasid(struct iommu_dom= ain *domain, =20 domain_remove_dev_pasid(old, dev, pasid); =20 - if (domain->type & __IOMMU_DOMAIN_PAGING) - intel_iommu_debugfs_create_dev_pasid(dev_pasid); + intel_iommu_debugfs_create_dev_pasid(dev_pasid); =20 return 0; =20 --=20 2.43.0 From nobody Sun Nov 24 01:19:00 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 166A916415 for ; Fri, 8 Nov 2024 02:15:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032126; cv=none; b=iz269w33aBVcITd8DK8NQvLAs8tew2dNrtLS1r0XDdoTkT0AwQEzT5SavwqR3hmaWsErZ2rAaLWdco1ZqYKkfNyxZRC93iNo6vzwx4RuPsKvH7DJxF4G0IyKk5nLXihKUUoIPi0i8Jlw30gnXSGjAporMY5FR2HW7H8FvYLnKRE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032126; c=relaxed/simple; bh=pqHDOW5sBvGO4ig/n9Yg0pA/3ZOlK4rABgySVhuXXYc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YHaQhY43feT6XQXB2KWfnMUAv2QFRnhH5WBAgytRM/+dmKdY91KZdDJK048xAYc2LPNnP5cRqifbmvv7yqBYT7RVGbHBP8mMJDV2jRmTmcLfN1Ovwv7+krwIkDzabNR39AtRVtjLjvfPMICMPDeAr7O3sjZRPA6VdTxUVSIQzYE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=NcRl68Bs; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="NcRl68Bs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731032125; x=1762568125; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pqHDOW5sBvGO4ig/n9Yg0pA/3ZOlK4rABgySVhuXXYc=; b=NcRl68BsE/0DKcrL0zsOo6svsWVNOTERzse2BaLIe2KEzn5pCEH7wxVM rOgswW4dU7FdTk1PmkFf+bNy0WsEUVmw3TmGWQervfTOcqy3DA4HsyJOt fjiwJQjuOxS1DU3LfaoJZUy1UWITE0hAC8BtH46IAAf4VInCO5Ihz6c6W O3wkjggbRzo3BYeb28bQXvlsGDxaM2V74i0aXdAzL1psz7Nm8hW2lo4nY AUfzhHTsACJpscz2dNwpykfkIQzpgGQpKbTtUPkbF5Tq9OH0FEDz46YWr JBBRlpBj8rwMd0Mf1gVFiBEn0LvXmVt7krrFBgya/AXnSzrFy8sd2+ICJ Q==; X-CSE-ConnectionGUID: 5miR3wr1S++fyy0Rj7OZcw== X-CSE-MsgGUID: THwUbrtzSAOwv8TuRIMoXw== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="31007840" X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="31007840" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 18:15:20 -0800 X-CSE-ConnectionGUID: munm1iUKQb+yfylhPl+4Cw== X-CSE-MsgGUID: T2T+qyOwRmeTIHSyHLNfNg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="116213831" Received: from allen-sbox.sh.intel.com ([10.239.159.30]) by fmviesa001.fm.intel.com with ESMTP; 07 Nov 2024 18:15:15 -0800 From: Lu Baolu To: Joerg Roedel Cc: Jason Gunthorpe , Kevin Tian , Yi Liu , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 09/13] iommu/vt-d: Make intel_svm_set_dev_pasid() support domain replacement Date: Fri, 8 Nov 2024 10:14:00 +0800 Message-ID: <20241108021406.173972-10-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241108021406.173972-1-baolu.lu@linux.intel.com> References: <20241108021406.173972-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Yi Liu Make intel_svm_set_dev_pasid() support replacement. Signed-off-by: Yi Liu Link: https://lore.kernel.org/r/20241107122234.7424-10-yi.l.liu@intel.com Signed-off-by: Lu Baolu --- drivers/iommu/intel/iommu.c | 8 ++++---- drivers/iommu/intel/iommu.h | 5 +++++ drivers/iommu/intel/svm.c | 5 +++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index f3ccbe516dcb..251cfebe6226 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -1752,10 +1752,10 @@ static void domain_context_clear_one(struct device_= domain_info *info, u8 bus, u8 intel_context_flush_present(info, context, did, true); } =20 -static int __domain_setup_first_level(struct intel_iommu *iommu, - struct device *dev, ioasid_t pasid, - u16 did, pgd_t *pgd, int flags, - struct iommu_domain *old) +int __domain_setup_first_level(struct intel_iommu *iommu, + struct device *dev, ioasid_t pasid, + u16 did, pgd_t *pgd, int flags, + struct iommu_domain *old) { if (!old) return intel_pasid_setup_first_level(iommu, dev, pgd, diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index cdca7d5061a7..d23977cc7d90 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -1250,6 +1250,11 @@ domain_add_dev_pasid(struct iommu_domain *domain, void domain_remove_dev_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t pasid); =20 +int __domain_setup_first_level(struct intel_iommu *iommu, + struct device *dev, ioasid_t pasid, + u16 did, pgd_t *pgd, int flags, + struct iommu_domain *old); + int dmar_ir_support(void); =20 void iommu_flush_write_buffer(struct intel_iommu *iommu); diff --git a/drivers/iommu/intel/svm.c b/drivers/iommu/intel/svm.c index 6c0685ea8466..f5569347591f 100644 --- a/drivers/iommu/intel/svm.c +++ b/drivers/iommu/intel/svm.c @@ -127,8 +127,9 @@ static int intel_svm_set_dev_pasid(struct iommu_domain = *domain, =20 /* Setup the pasid table: */ sflags =3D cpu_feature_enabled(X86_FEATURE_LA57) ? PASID_FLAG_FL5LP : 0; - ret =3D intel_pasid_setup_first_level(iommu, dev, mm->pgd, pasid, - FLPT_DEFAULT_DID, sflags); + ret =3D __domain_setup_first_level(iommu, dev, pasid, + FLPT_DEFAULT_DID, mm->pgd, + sflags, old); if (ret) goto out_remove_dev_pasid; =20 --=20 2.43.0 From nobody Sun Nov 24 01:19:00 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7CC1780C0C for ; Fri, 8 Nov 2024 02:15:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032129; cv=none; b=klgi7QDUDscy1efdWaUJdJMeUf7HSgSbZytSzMyZ6oFe2a2/KG3QHofHSMnM5D/1MeHo+76zr0uQZyLuV4g8WXbat9+bqGLL90/gC4T+YRUvE198rk5EWbC8/hUe61LelQrpQq/h8LfFdLfQfFQkzwn2ESu/K6GOsvAIPapYGn4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032129; c=relaxed/simple; bh=ovL5X6BKU6X15z7dlOJy3dPt43y7u+VRg9y7JYC2oEE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FIqJkKfNmp4FApXUU0MeklKvWMOiLMyiwJj1CbbvZlcLyx+tXknBOXaFYFdbJUFkxXB2MCGi7AyVr6iamvBzxf9zxxCYvGCjkV8NtSzwhnq1UhnAnjvOwgBb6b6cUe4iSdOXOk7dVvIdqHx9R4dYSm8nPueyJnYfDw8GNb4Mqhk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=CzcNQs9A; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="CzcNQs9A" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731032128; x=1762568128; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ovL5X6BKU6X15z7dlOJy3dPt43y7u+VRg9y7JYC2oEE=; b=CzcNQs9A5nxaD7rXYm/d53bfRzersQn97BSxoWxPnZIKmZm21lzbm6v/ SQm6nnxy4EKIcWejVlMwPMUcCFtv+YtH3Ibla3k636gN3FIGRH4+FUFiO GPYTV2f7IjK8aur0P8+lrLWXZGioBlu5J88rSCCJen/TNP8I5mvZ9ys65 NveSOPv6X0VZNfI0jHqEHM/A37vYQnXhPDuDIgc/YYHk1uPZJrMZGkzuj tufFOy+txgeOBp7hX7/HbO4hu8YsUYfkOgS+bGTYN9Hywvh2B0dp3Cs02 gMfg1jEHMyhSdzIYYgeWLRkaVjeF+BXS83KDcaRu8+ff4h/0IvUT2HqqX Q==; X-CSE-ConnectionGUID: 38BR9SGGT7uUv4Cr9mNGzQ== X-CSE-MsgGUID: mHs+p3FWTfOSpVtr6TYIQQ== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="31007851" X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="31007851" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 18:15:20 -0800 X-CSE-ConnectionGUID: DqhSkt81Rv6v9zW/t9tfdw== X-CSE-MsgGUID: fLFIUv85RlK49Dbpjj3pjw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="116213835" Received: from allen-sbox.sh.intel.com ([10.239.159.30]) by fmviesa001.fm.intel.com with ESMTP; 07 Nov 2024 18:15:16 -0800 From: Lu Baolu To: Joerg Roedel Cc: Jason Gunthorpe , Kevin Tian , Yi Liu , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 10/13] iommu/vt-d: Make identity_domain_set_dev_pasid() to handle domain replacement Date: Fri, 8 Nov 2024 10:14:01 +0800 Message-ID: <20241108021406.173972-11-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241108021406.173972-1-baolu.lu@linux.intel.com> References: <20241108021406.173972-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Yi Liu Let identity_domain_set_dev_pasid() call the pasid replace helpers hence be able to do domain replacement. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Yi Liu Link: https://lore.kernel.org/r/20241107122234.7424-11-yi.l.liu@intel.com Signed-off-by: Lu Baolu --- drivers/iommu/intel/iommu.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 251cfebe6226..ba984cb4aa44 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -1778,6 +1778,17 @@ static int domain_setup_second_level(struct intel_io= mmu *iommu, pasid); } =20 +static int domain_setup_passthrough(struct intel_iommu *iommu, + struct device *dev, ioasid_t pasid, + struct iommu_domain *old) +{ + if (!old) + return intel_pasid_setup_pass_through(iommu, dev, pasid); + return intel_pasid_replace_pass_through(iommu, dev, + iommu_domain_did(old, iommu), + pasid); +} + static int domain_setup_first_level(struct intel_iommu *iommu, struct dmar_domain *domain, struct device *dev, @@ -4423,11 +4434,17 @@ static int identity_domain_set_dev_pasid(struct iom= mu_domain *domain, { struct device_domain_info *info =3D dev_iommu_priv_get(dev); struct intel_iommu *iommu =3D info->iommu; + int ret; =20 if (!pasid_supported(iommu) || dev_is_real_dma_subdevice(dev)) return -EOPNOTSUPP; =20 - return intel_pasid_setup_pass_through(iommu, dev, pasid); + ret =3D domain_setup_passthrough(iommu, dev, pasid, old); + if (ret) + return ret; + + domain_remove_dev_pasid(old, dev, pasid); + return 0; } =20 static struct iommu_domain identity_domain =3D { --=20 2.43.0 From nobody Sun Nov 24 01:19:00 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 006C3192D98 for ; Fri, 8 Nov 2024 02:15:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032129; cv=none; b=k+sUlG3q4uBJbH/wHwrHVz9vWFT+RrTg1DGagzNghtPe8nKRpo2RD+T6G/isdFqnMRW1QIRSUtZl1YqUBA/jRwP0va1xKQ1Ci9OaoALNOpbT4kiy18b5elHoq9Ra1VWZUOlYUd8Gg1LyCvZp8+FOFreux3EUjAHg7QUw2JLc/k8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032129; c=relaxed/simple; bh=bzOyekv0clUGynmdW7dlvJLFDi2gKYykZx0AhaRfTPk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rPb6IhC+dpWWl4Ptt46lxPEM4mqmbUf5LdYOW1ggI6nmXuEd+FKEGRT6ShmszHJr85pjhzd3WQ/MkDGMTheV/zsm5IQnH/IC8mEjhsvt5FMseTH9YmkzNi8wOUJmsqYJI/cgLACWTAb5iUpIiyt0AwaHd9bleNImHtY2yXoWp0E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=maqGcqzA; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="maqGcqzA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731032128; x=1762568128; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bzOyekv0clUGynmdW7dlvJLFDi2gKYykZx0AhaRfTPk=; b=maqGcqzAm/6uVCCE/+NP4x/RFHEcve1+2ANV0FZViJfVZAGJ0kVrgUq1 IBfQENwKykTC3tdf25Rs8pyXzcEjsBRPPiaOWtGJXMTfrQWuwJc+M6T6c hBjaKAs4c5oG5ST0arQZx/wHUxzoeXMO0ldK9Zg1HAHf9x4USQ28Ylw9F fMlh41Dad4DzCEunV3+x90tnBTGeMJaWmeVsaQ/WwVreU2Vt8bkwgD58z lQbGIFPEJhWAe6dQPNnUJ42jDm9F01pFUsviyWdQ0WGzNYH8whW6Y3Bbk CEBQrhlIozNgeVG0ATFhaartJehnYwVVLoQp3HE/YfOmpF8huAemgOioi w==; X-CSE-ConnectionGUID: 91JO/iSrTuGKIz5z9aCWHw== X-CSE-MsgGUID: tAz2bxavR+Wtng70MOrG8A== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="31007855" X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="31007855" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 18:15:20 -0800 X-CSE-ConnectionGUID: 9cKA4ULqSLy7b0vvOVwdTg== X-CSE-MsgGUID: crUOT37tSMOSb/dVl35N0g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="116213840" Received: from allen-sbox.sh.intel.com ([10.239.159.30]) by fmviesa001.fm.intel.com with ESMTP; 07 Nov 2024 18:15:18 -0800 From: Lu Baolu To: Joerg Roedel Cc: Jason Gunthorpe , Kevin Tian , Yi Liu , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 11/13] iommu/vt-d: Add set_dev_pasid callback for nested domain Date: Fri, 8 Nov 2024 10:14:02 +0800 Message-ID: <20241108021406.173972-12-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241108021406.173972-1-baolu.lu@linux.intel.com> References: <20241108021406.173972-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Yi Liu Add intel_nested_set_dev_pasid() to set a nested type domain to a PASID of a device. Co-developed-by: Lu Baolu Signed-off-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Yi Liu Link: https://lore.kernel.org/r/20241107122234.7424-12-yi.l.liu@intel.com --- drivers/iommu/intel/iommu.c | 6 ----- drivers/iommu/intel/iommu.h | 7 +++++ drivers/iommu/intel/nested.c | 50 ++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index ba984cb4aa44..b380b38315b2 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -1812,12 +1812,6 @@ static int domain_setup_first_level(struct intel_iom= mu *iommu, (pgd_t *)pgd, flags, old); } =20 -static bool dev_is_real_dma_subdevice(struct device *dev) -{ - return dev && dev_is_pci(dev) && - pci_real_dma_dev(to_pci_dev(dev)) !=3D to_pci_dev(dev); -} - static int dmar_domain_attach_device(struct dmar_domain *domain, struct device *dev) { diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index d23977cc7d90..2cca094c259d 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -22,6 +22,7 @@ #include #include #include +#include =20 #include #include @@ -832,6 +833,12 @@ iommu_domain_did(struct iommu_domain *domain, struct i= ntel_iommu *iommu) return domain_id_iommu(to_dmar_domain(domain), iommu); } =20 +static inline bool dev_is_real_dma_subdevice(struct device *dev) +{ + return dev && dev_is_pci(dev) && + pci_real_dma_dev(to_pci_dev(dev)) !=3D to_pci_dev(dev); +} + /* * 0: readable * 1: writable diff --git a/drivers/iommu/intel/nested.c b/drivers/iommu/intel/nested.c index 989ca5cc04eb..42c4533a6ea2 100644 --- a/drivers/iommu/intel/nested.c +++ b/drivers/iommu/intel/nested.c @@ -130,8 +130,58 @@ static int intel_nested_cache_invalidate_user(struct i= ommu_domain *domain, return ret; } =20 +static int domain_setup_nested(struct intel_iommu *iommu, + struct dmar_domain *domain, + struct device *dev, ioasid_t pasid, + struct iommu_domain *old) +{ + if (!old) + return intel_pasid_setup_nested(iommu, dev, pasid, domain); + return intel_pasid_replace_nested(iommu, dev, pasid, + iommu_domain_did(old, iommu), + domain); +} + +static int intel_nested_set_dev_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid, + struct iommu_domain *old) +{ + struct device_domain_info *info =3D dev_iommu_priv_get(dev); + struct dmar_domain *dmar_domain =3D to_dmar_domain(domain); + struct intel_iommu *iommu =3D info->iommu; + struct dev_pasid_info *dev_pasid; + int ret; + + if (!pasid_supported(iommu) || dev_is_real_dma_subdevice(dev)) + return -EOPNOTSUPP; + + if (context_copied(iommu, info->bus, info->devfn)) + return -EBUSY; + + ret =3D paging_domain_compatible(&dmar_domain->s2_domain->domain, dev); + if (ret) + return ret; + + dev_pasid =3D domain_add_dev_pasid(domain, dev, pasid); + if (IS_ERR(dev_pasid)) + return PTR_ERR(dev_pasid); + + ret =3D domain_setup_nested(iommu, dmar_domain, dev, pasid, old); + if (ret) + goto out_remove_dev_pasid; + + domain_remove_dev_pasid(old, dev, pasid); + + return 0; + +out_remove_dev_pasid: + domain_remove_dev_pasid(domain, dev, pasid); + return ret; +} + static const struct iommu_domain_ops intel_nested_domain_ops =3D { .attach_dev =3D intel_nested_attach_dev, + .set_dev_pasid =3D intel_nested_set_dev_pasid, .free =3D intel_nested_domain_free, .cache_invalidate_user =3D intel_nested_cache_invalidate_user, }; --=20 2.43.0 From nobody Sun Nov 24 01:19:00 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B31283CA0 for ; Fri, 8 Nov 2024 02:15:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032134; cv=none; b=KWZYDhp65HE9zdrNs9DM0QmLYwtbpiXVgG1Qo5QikcC22ZZ8d5leLVcYLyFEvZXNMGN6M11WHgh26xpehUySRyuiTwLdslcE13kQrAK7KN3zarijBErb+KUGNV9m9DVYVW1RCm5mgffzl+r5WHEwLRp6Q4b5uPvioNUWrOnNLXE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032134; c=relaxed/simple; bh=sRZPTZ00M5Ft/vHNMDKUfam33wTHNeoKxKOUOiXBYn4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R0NF/VaRPUwyaeRlJsBrnTT/L9oEuyXKXIwNi8zM44BO1WcIbX4PvcotcdjoM/lg+mbiK4muS3tX6KSyRcgZLFiYVBLwnfgPd1ouZ2P7NqzrLD1Mpi6dCeTkExFLyQLQMk6cCCl0TvN5V7XgIMGrzHKTwkky/4JKUh17TuMVz5I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=j/BsEF33; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="j/BsEF33" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731032133; x=1762568133; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sRZPTZ00M5Ft/vHNMDKUfam33wTHNeoKxKOUOiXBYn4=; b=j/BsEF33HnK+uFZcZuV3NTqBEmnVCM7dtJg5V2eTYIh9q+qpzZaok4jK lTmIfcn6TR3eO/wwWWBVZH85GuFXp+rU3x2G/42biG/CBPEx8XdjMGFxU xk3R52pLG2BTiMmAe75kBjntkuv1Asj0b1wbx3PVZCOKvvpTobL3qtDgM HPgsQUTXImdRs9se+PStGM87RGNb6HQ/wiBbeOR2rM8Rpfm9XWGCEXOyH SbdxHhu6TqR2zOzX02IUGZ+B2BhijcasFhO2qzHLKusslYyvqEmzfDJE2 EylSDGz53iDwjIBLsn1mpEmwvkacGreK/vkemn0GRtLVn0jtsNuE4GJ+/ g==; X-CSE-ConnectionGUID: 26HvXZzMSnKUb8nh1qe2BA== X-CSE-MsgGUID: OD6OrnYyTseIFO1OTcEiAg== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="31007869" X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="31007869" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 18:15:23 -0800 X-CSE-ConnectionGUID: mERSf7AASyOkQz+8x+xTcA== X-CSE-MsgGUID: mMrk+jmCT66zRj7IUZ/w6g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="116213847" Received: from allen-sbox.sh.intel.com ([10.239.159.30]) by fmviesa001.fm.intel.com with ESMTP; 07 Nov 2024 18:15:20 -0800 From: Lu Baolu To: Joerg Roedel Cc: Jason Gunthorpe , Kevin Tian , Yi Liu , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 12/13] iommu/arm-smmu-v3: Make set_dev_pasid() op support replace Date: Fri, 8 Nov 2024 10:14:03 +0800 Message-ID: <20241108021406.173972-13-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241108021406.173972-1-baolu.lu@linux.intel.com> References: <20241108021406.173972-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jason Gunthorpe set_dev_pasid() op is going to be enhanced to support domain replacement of a pasid. This prepares for this op definition. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Nicolin Chen Signed-off-by: Yi Liu Link: https://lore.kernel.org/r/20241107122234.7424-13-yi.l.liu@intel.com Signed-off-by: Lu Baolu --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 2 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +++------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iomm= u/arm/arm-smmu-v3/arm-smmu-v3-sva.c index 645da7b69bed..1d3e71569775 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -349,7 +349,7 @@ static int arm_smmu_sva_set_dev_pasid(struct iommu_doma= in *domain, * get reassigned */ arm_smmu_make_sva_cd(&target, master, domain->mm, smmu_domain->cd.asid); - ret =3D arm_smmu_set_pasid(master, smmu_domain, id, &target); + ret =3D arm_smmu_set_pasid(master, smmu_domain, id, &target, old); =20 mmput(domain->mm); return ret; diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/ar= m/arm-smmu-v3/arm-smmu-v3.c index 1517fe2c356e..f70165f544df 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2883,7 +2883,7 @@ static int arm_smmu_s1_set_dev_pasid(struct iommu_dom= ain *domain, */ arm_smmu_make_s1_cd(&target_cd, master, smmu_domain); return arm_smmu_set_pasid(master, to_smmu_domain(domain), id, - &target_cd); + &target_cd, old); } =20 static void arm_smmu_update_ste(struct arm_smmu_master *master, @@ -2913,16 +2913,13 @@ static void arm_smmu_update_ste(struct arm_smmu_mas= ter *master, =20 int arm_smmu_set_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, ioasid_t pasid, - struct arm_smmu_cd *cd) + struct arm_smmu_cd *cd, struct iommu_domain *old) { struct iommu_domain *sid_domain =3D iommu_get_domain_for_dev(master->dev); struct arm_smmu_attach_state state =3D { .master =3D master, - /* - * For now the core code prevents calling this when a domain is - * already attached, no need to set old_domain. - */ .ssid =3D pasid, + .old_domain =3D old, }; struct arm_smmu_cd *cdptr; int ret; diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/ar= m/arm-smmu-v3/arm-smmu-v3.h index 1e9952ca989f..52eaa0bedee1 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -875,7 +875,7 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *ma= ster, int ssid, =20 int arm_smmu_set_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, ioasid_t pasid, - struct arm_smmu_cd *cd); + struct arm_smmu_cd *cd, struct iommu_domain *old); =20 void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid); void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid, --=20 2.43.0 From nobody Sun Nov 24 01:19:00 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E5DF29CEF for ; Fri, 8 Nov 2024 02:15:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032135; cv=none; b=h1d4DLJDPlat3S0cLAFD+owil0lafYkLDMnmTWwfOjI2dCqbj4CQUeKdQzFreQnPDn+QKo/89S1QyYEOWNWSQ4010hDWy3MdA5H1VK7TmC7Z55Qqf6lW/PL7ARUHds68Y78CGdF8PiQnwnjuMzPvnzNN9uH1TDdLZLkVGr/HinQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731032135; c=relaxed/simple; bh=vLIIkzIeeRGQ2f+7KVZkFTMmDLAJzpo0wxcN3DAX7KU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=czob4XM5JGhk4XOuV7Miyy8jbawqjyWoSNdJbS+JLrQ1fK/s+dRYjG/6s04CjdDKs/iJFGKspCuxfn9iU1p7TI3AHfSfnig3tlgb9asY/UQuA8jlIQdt+yFZKDfN1Lv59QdRr5mesDvX37PlErA25nE0AdEJIZ5/kHHiWxdAk6I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Zc3/zcTu; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Zc3/zcTu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731032135; x=1762568135; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vLIIkzIeeRGQ2f+7KVZkFTMmDLAJzpo0wxcN3DAX7KU=; b=Zc3/zcTu4TdnwZb3NtTdaz5sUUG6N8WsR3NXcy7PqiMNVcpiAuv/rRRo OOzUopG81lAXRClkQ9vG4aisTtcwiibeCrHLUA1If4ri4+oh70rPpZqY0 QzjUKB9Civ/LPiSBecWt0jcly/BLPOHMyNbhL6qvRZs0PdtTPvil+O8dF Z3wf0szUyd+3L/32xjokhmo3bmpWuIysIIf0YbBVSsYbhTALKo7b+kd5x zcu9GdbJli+yQqNkpX1v8HynIGwLvtEzngqQegpFiv1spBW3Vvoy3Qx/V /xHVTKjKkVnjWP538ev7spDjxK1kkjHrnr01mAlz6HmTgW2EakE9MHulA g==; X-CSE-ConnectionGUID: TwnYSU74R2K+ZTCtCea2eg== X-CSE-MsgGUID: Qie902diThSXSgq6SBoKJw== X-IronPort-AV: E=McAfee;i="6700,10204,11249"; a="31007874" X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="31007874" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2024 18:15:24 -0800 X-CSE-ConnectionGUID: v7/dZZN8S12eEBKcucLQLw== X-CSE-MsgGUID: fYToPKFuRmW65tzcONP0tA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,136,1728975600"; d="scan'208";a="116213852" Received: from allen-sbox.sh.intel.com ([10.239.159.30]) by fmviesa001.fm.intel.com with ESMTP; 07 Nov 2024 18:15:22 -0800 From: Lu Baolu To: Joerg Roedel Cc: Jason Gunthorpe , Kevin Tian , Yi Liu , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 13/13] iommu: Make set_dev_pasid op support domain replacement Date: Fri, 8 Nov 2024 10:14:04 +0800 Message-ID: <20241108021406.173972-14-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241108021406.173972-1-baolu.lu@linux.intel.com> References: <20241108021406.173972-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Yi Liu The iommu core is going to support domain replacement for pasid, it needs to make the set_dev_pasid op support replacing domain and keep the old domain config in the failure case. AMD iommu driver does not support domain replacement for pasid yet, so it would fail the set_dev_pasid op to keep the old config if the input @old is non-NULL. Till now, all the set_dev_pasid callbacks can handle the old parameter and can keep the old config when failed, so update the kdoc of set_dev_pasid op. Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Vasant Hegde Signed-off-by: Yi Liu Link: https://lore.kernel.org/r/20241107122234.7424-14-yi.l.liu@intel.com Signed-off-by: Lu Baolu --- drivers/iommu/amd/pasid.c | 3 +++ include/linux/iommu.h | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/amd/pasid.c b/drivers/iommu/amd/pasid.c index d1dfc745f55e..8c73a30c2800 100644 --- a/drivers/iommu/amd/pasid.c +++ b/drivers/iommu/amd/pasid.c @@ -109,6 +109,9 @@ int iommu_sva_set_dev_pasid(struct iommu_domain *domain, unsigned long flags; int ret =3D -EINVAL; =20 + if (old) + return -EOPNOTSUPP; + /* PASID zero is used for requests from the I/O device without PASID */ if (!is_pasid_valid(dev_data, pasid)) return ret; diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 32dce80aa7fd..27f923450a7c 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -616,7 +616,8 @@ struct iommu_ops { * * EBUSY - device is attached to a domain and cannot be changed * * ENODEV - device specific errors, not able to be attached * * - treated as ENODEV by the caller. Use is discouraged - * @set_dev_pasid: set an iommu domain to a pasid of device + * @set_dev_pasid: set or replace an iommu domain to a pasid of device. Th= e pasid of + * the device should be left in the old config in error ca= se. * @map_pages: map a physically contiguous set of pages of the same size to * an iommu domain. * @unmap_pages: unmap a number of pages of the same size from an iommu do= main --=20 2.43.0