Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp120391rdb; Thu, 8 Feb 2024 00:23:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IFaWmKT9EAR37001yaznvAr3ruB50UzY61j6vF2kYc7eihIrc3FN74MidKQZ9ekXOMM58+8 X-Received: by 2002:a05:620a:55a6:b0:785:a94f:6061 with SMTP id vr6-20020a05620a55a600b00785a94f6061mr492473qkn.5.1707380637176; Thu, 08 Feb 2024 00:23:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707380637; cv=pass; d=google.com; s=arc-20160816; b=iYRKootdMxNPrVAhiiLrUq1WQIuEmHGYxTBOITKrY4ReSTbtbDH/oLnftG9sCoCJpH DNlVo1zYeFK01f3fbamodvDz56KhypCtaPkGPrP4iLfknJ/QcuoJ2JP9xDKQEssDCOlZ 7Xq9DffIda5HEe/IRh1iviOdg9+3pCn3RZyx1A2xIvLdr+US/qpaSfVf/cblb2SKSPhe pmbvPhwk2a4ROyCfUFoLGVtujxicAUCeu+Ut1g6ZLqsPaxLAi4GTzlXbBIojcpV6jzHi o18ywL4nyteJVJmTRuEUBJU3uZix15RzmC1Rrpqao0N8MvjSLo6KixwBuw8Vn5JJ/3mr rXbw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=6/LgyhAnm/AlEvoUCvBe12XiB0hNFdsiQwexWD2KdEs=; fh=hnx2oX9gCvlBWVn9Chu2QyDvQGnrJh4tZPimxHbdNPQ=; b=NXiagELsm1FOnxgkfLBSAFUGq/4kUP4vutbcwMPNW4g2KUtmoxCZs+LuQ4HoLxrIFt n1ec+wOURoh+l47tUeTgeQTvAly7YcrZWy0SgBcNKOfbhQWczqIMw/4ycJqQ8kwgefRQ XH/suMNUHr1n9eqaanE61H9nwe1H/6yuy3JBQpVTuLX3mudawyZTuCG5tSOJbgr8FFvF 8eSE4O/XamRy3NZ0HSgfR9WdGu1uBQ53R1aUcVhOrKw0fW6WaAshIUL/wWttuJURle4e dvrr+QNIdrSOR317BeI0mYgwzrwdZaflicG2HbgeazdqAzYay2tvj/CDxnElegIldn7O mm3w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Q8tpHErF; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-57606-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-57606-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=2; AJvYcCWuBbcQADX29Ck8oRKJc0RCb6H0lkgrfMMXHP2dB7cv8dveIgyL8b+ShSrS0/0iKWKlPfGDVXmdjtSUMyj4ipvMVw1OUufjaQQnIkYhdQ== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id t6-20020a05620a004600b00783e40fde6dsi2741846qkt.404.2024.02.08.00.23.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 00:23:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-57606-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Q8tpHErF; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-57606-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-57606-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id DB62C1C25A84 for ; Thu, 8 Feb 2024 08:23:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 311E36A8DE; Thu, 8 Feb 2024 08:23:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Q8tpHErF" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.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 598956A331; Thu, 8 Feb 2024 08:23:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.15 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707380594; cv=none; b=BJKfRkuUMd0km4SbYYJpTJn909q35QzJ99OaMGgLDOIffnN8Goa5Pp8TgNWG7zbIlTOrYyHX65Km08z+AP5eOlEeOQT5e1oMEuaHTIwo7ynOTNwOjGsGTpwlm76hVXdPVYeKimnFPrdGPeZwPDT8pUvGJ8deIraNKK+HrqFWUVs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707380594; c=relaxed/simple; bh=6WL8z2aMLDo6O3Bz7gM5e9uPczPryXbWDTbbIrVy8k8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=L5VSc7Kd8p9EIrHJqkWRQrL90nXOoDu8eefuhIQLi/cjyc82obJsrO8mjCsnkVcwj1AzsMbvL+myXCfdDuPu6BBuLAHaqUjjCDoEdBQekapmjBlZ+9qyLczGNuRCvHeV6ndGOyLAURlwjJndKsiuRX3LMA92rSR1Lqkx9gas+2A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Q8tpHErF; arc=none smtp.client-ip=198.175.65.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1707380593; x=1738916593; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6WL8z2aMLDo6O3Bz7gM5e9uPczPryXbWDTbbIrVy8k8=; b=Q8tpHErFodD8JZMW6uM6wIveJD9cvhJNGXn/E1xOy/IAYd2Uwgu4WVRl DrFZDDHOb5TKjzcjNHRupQ8tS2Ka+butHNCEwfZu/Xk3FjI1R0iZ+mZ7h 4Ugzw3d95EophpDnXjLhu6edGgfLfhhRUHN//YO9Y1/W/cI/Pq72my6Qw /OghZ+/TJlJvPzwFQ93ShU9dLSropVv9xprixjeE/hdhsKJhH4TGDBC5z 2noIkNIHdvUciMkOqRojjeCP76D2Aa2jWmrcmFiK3o3InOSbdBtpvDbfh LhbJvgvtsZkquIQhH2bJTZH1M2+4ThhnOnWarokefyMXafs0keF+iO6VX w==; X-IronPort-AV: E=McAfee;i="6600,9927,10977"; a="5036309" X-IronPort-AV: E=Sophos;i="6.05,253,1701158400"; d="scan'208";a="5036309" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2024 00:23:12 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,253,1701158400"; d="scan'208";a="6252102" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by orviesa003.jf.intel.com with ESMTP; 08 Feb 2024 00:23:11 -0800 From: Yi Liu To: joro@8bytes.org, jgg@nvidia.com, kevin.tian@intel.com, baolu.lu@linux.intel.com Cc: alex.williamson@redhat.com, robin.murphy@arm.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com Subject: [PATCH rc 2/8] iommu/vt-d: Add __iommu_flush_iotlb_psi() Date: Thu, 8 Feb 2024 00:23:01 -0800 Message-Id: <20240208082307.15759-3-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240208082307.15759-1-yi.l.liu@intel.com> References: <20240208082307.15759-1-yi.l.liu@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: 8bit Add __iommu_flush_iotlb_psi() to do the psi iotlb flush with a DID input rather than calculating it within the helper. This is useful when flushing cache for parent domain which reuses DIDs of its nested domains. Signed-off-by: Yi Liu --- drivers/iommu/intel/iommu.c | 79 +++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 35 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index e393c62776f3..eef6a187b651 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -1368,6 +1368,47 @@ static void domain_flush_pasid_iotlb(struct intel_iommu *iommu, spin_unlock_irqrestore(&domain->lock, flags); } +static void __iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did, + unsigned long pfn, unsigned int pages, + int ih) +{ + unsigned int aligned_pages = __roundup_pow_of_two(pages); + unsigned int mask = ilog2(aligned_pages); + uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT; + unsigned long bitmask = aligned_pages - 1; + + /* + * PSI masks the low order bits of the base address. If the + * address isn't aligned to the mask, then compute a mask value + * needed to ensure the target range is flushed. + */ + if (unlikely(bitmask & pfn)) { + unsigned long end_pfn = pfn + pages - 1, shared_bits; + + /* + * Since end_pfn <= pfn + bitmask, the only way bits + * higher than bitmask can differ in pfn and end_pfn is + * by carrying. This means after masking out bitmask, + * high bits starting with the first set bit in + * shared_bits are all equal in both pfn and end_pfn. + */ + shared_bits = ~(pfn ^ end_pfn) & ~bitmask; + mask = shared_bits ? __ffs(shared_bits) : BITS_PER_LONG; + } + + /* + * Fallback to domain selective flush if no PSI support or + * the size is too big. + */ + if (!cap_pgsel_inv(iommu->cap) || + mask > cap_max_amask_val(iommu->cap)) + iommu->flush.flush_iotlb(iommu, did, 0, 0, + DMA_TLB_DSI_FLUSH); + else + iommu->flush.flush_iotlb(iommu, did, addr | ih, mask, + DMA_TLB_PSI_FLUSH); +} + static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, struct dmar_domain *domain, unsigned long pfn, unsigned int pages, @@ -1384,42 +1425,10 @@ static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, if (ih) ih = 1 << 6; - if (domain->use_first_level) { + if (domain->use_first_level) domain_flush_pasid_iotlb(iommu, domain, addr, pages, ih); - } else { - unsigned long bitmask = aligned_pages - 1; - - /* - * PSI masks the low order bits of the base address. If the - * address isn't aligned to the mask, then compute a mask value - * needed to ensure the target range is flushed. - */ - if (unlikely(bitmask & pfn)) { - unsigned long end_pfn = pfn + pages - 1, shared_bits; - - /* - * Since end_pfn <= pfn + bitmask, the only way bits - * higher than bitmask can differ in pfn and end_pfn is - * by carrying. This means after masking out bitmask, - * high bits starting with the first set bit in - * shared_bits are all equal in both pfn and end_pfn. - */ - shared_bits = ~(pfn ^ end_pfn) & ~bitmask; - mask = shared_bits ? __ffs(shared_bits) : BITS_PER_LONG; - } - - /* - * Fallback to domain selective flush if no PSI support or - * the size is too big. - */ - if (!cap_pgsel_inv(iommu->cap) || - mask > cap_max_amask_val(iommu->cap)) - iommu->flush.flush_iotlb(iommu, did, 0, 0, - DMA_TLB_DSI_FLUSH); - else - iommu->flush.flush_iotlb(iommu, did, addr | ih, mask, - DMA_TLB_PSI_FLUSH); - } + else + __iommu_flush_iotlb_psi(iommu, did, pfn, pages, ih); /* * In caching mode, changes of pages from non-present to present require -- 2.34.1