Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp126078lqt; Wed, 5 Jun 2024 20:42:50 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCULdp9tIymP0wYnRW5EJA4dqxAstm/9Txb07xCZjLxadEOpK2TCUi+WSJgV3pbYcC8rZQaVSHVhj55OUIH78y0xuLPLsKqiZJtscCcSpw== X-Google-Smtp-Source: AGHT+IEeXGHp7625Sep5n6dI7AKWyTEdvTS6fPJjE4VAc2wMrsYkGVPmcT43IV5M+SdQesOq5adb X-Received: by 2002:a17:902:f544:b0:1f6:80cd:ec92 with SMTP id d9443c01a7336-1f6a5a7a268mr57977125ad.63.1717645369977; Wed, 05 Jun 2024 20:42:49 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717645369; cv=pass; d=google.com; s=arc-20160816; b=t7ai03Rvgh5pQhtRs/wAUgcROBFIdSUPr142a86x1DOd3FcNmOhcJQpkEzpjr8G8vO FO6Nj4sNGaCQp2WKDHVsR3R6+Z/prvdegD1uA7+31HNxGp8VuTvZzFSTJqFCoTX5jIrd jYvzHJekkLuWvi4GrOzZS8ItumJKm2AqjbTkpZJsNjQC9UqwOIB7foxOeWaDCXGb2JCL 9kzXbEXpbFl0vAQ0rDOJ0h9AH91FykXJSkZCaCvFON11k2zjFng2zIux4UsyAw+yDG2p X3bugRn1rV+T/gKIiRlvsTPPECbYFX9vEzYh4CFFoqsLXHUFLTGtf9PTtLlUOfUpw3oN Ucig== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=2szbUKV04ergLWpcuAIRlh5sl6nN2mnZ2bHSX7PE704=; fh=5K4v/0ku97kpa0vTvhWLyUEWwXZiIFVMmzwLLirrjJ4=; b=QqzaHW2Gvg587+kBOY2rt5b4Ji3T3OPsr2z4BHoqT4/QO8UT71xotb4vf9rLfU0wxN nrQfgaLadbGD0rMAdtohgj1ckyzUDFIX/Jon4NzyMsOYRWfTKmC7Rb7gzLRqbHpU3m6C IqH3BNpCFaGdjJhVB26RZEaZVr0vC+SOFHBP3p5vKwEJtWy9ibuvyxry6GKtzlsSLU9g +bvOWzp/8aH6ktc5BkB7OjtRLMCNHV9YRmDGblMr3D6kIPOJQeqUK+B2PtS8Umk4pDTE 779sDZATroYuj0KLBSk4mrn+yveOkP2FLXcM3OTZR+qQ3FLZ2iDyhbOXiTZ2cQK2Eiu0 LSYg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZKZbW8ya; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-203594-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-203594-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d9443c01a7336-1f6bd7d4c4csi882095ad.310.2024.06.05.20.42.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 20:42:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-203594-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZKZbW8ya; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-203594-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-203594-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 8571F28758F for ; Thu, 6 Jun 2024 03:42:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5C4321EB48; Thu, 6 Jun 2024 03:42:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ZKZbW8ya" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) (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 1A82F19D89B for ; Thu, 6 Jun 2024 03:42:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.10 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717645363; cv=none; b=NFt1bVJHtG464acdEv5HwGUsVXIgQoSKJ7uIVq2z/PNjdxjbvWq16jOsXWTK48qDPL9JEf/iN3Mszd7IRarU8PVncM345Cmp79pS1tgYxou+N3CxvJYO48ZtR5vTuez/JXoW9X4yWvrKPINSRsPItE8+Ee3VnVPs9jl9TeUeX7s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717645363; c=relaxed/simple; bh=+/KH7Dt/gunYlOnwVVcGKIkZIvdq00WPW2Ix2BGRdk0=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=LpZXLxzAqNxFaOR3VNZx/DE8Cs50Gg9lKsiVJe+PsLA1cjDP8MDuJi8pbpQg4pWARlo0GB1+k1V6Lebkw1sVjCC+X0SFROOkKcqGip/Ws6f3HhbVquog8zFFNBrxQVXkXTyw/ybo9doKamOz2wRtfgC/mIAzB8G6mnI90gSPbKc= 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=ZKZbW8ya; arc=none smtp.client-ip=198.175.65.10 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1717645362; x=1749181362; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=+/KH7Dt/gunYlOnwVVcGKIkZIvdq00WPW2Ix2BGRdk0=; b=ZKZbW8yaJZ2OIqPZKIi2PgOB+cn8web0FIHvfLAT6ai72uKkxhelsKi6 qsc0bBQ+CiqUV6HZ6V2/QJd/UMaZoOzPEfdlG7RtuynJzCkgj9zTSpFIY JxEWdGhY8nfn0Io4sbyBsHQofznSwAj59OMCt6kpqI5WqUvayUgIMWVQJ 0nJEFBtR0MLXw0cnLu5bU1EM20qni0e9n+H6wXKHnf/cbuC/aDh0QiCA3 yMBdVsadgTr7JxNBo0N62pVNgLfF6AQN+IF/c0B+XS35oxRPhGypVavM3 aYhLM2Pc9W/445ihWSdb+8wdaRMyEc5J+7PwfwAmnZ90a1ZbHBBxi2VvZ A==; X-CSE-ConnectionGUID: wr/AJ4SyS1yPE66LW2u+eQ== X-CSE-MsgGUID: jvMygjmwQRaGzh7ms8MtnQ== X-IronPort-AV: E=McAfee;i="6600,9927,11094"; a="31790051" X-IronPort-AV: E=Sophos;i="6.08,218,1712646000"; d="scan'208";a="31790051" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Jun 2024 20:42:41 -0700 X-CSE-ConnectionGUID: eEeBY0UNT3u3DlMV5iHCXw== X-CSE-MsgGUID: wAibKvBGQvC4FgeNYjCrig== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,218,1712646000"; d="scan'208";a="75296815" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by orviesa001.jf.intel.com with ESMTP; 05 Jun 2024 20:42:39 -0700 From: Lu Baolu To: Joerg Roedel , Will Deacon , Robin Murphy , Jason Gunthorpe , Kevin Tian Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH] iommu/vt-d: Refactor PCI PRI enabling/disabling callbacks Date: Thu, 6 Jun 2024 11:40:19 +0800 Message-Id: <20240606034019.42795-1-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Commit 0095bf83554f8 ("iommu: Improve iopf_queue_remove_device()") specified the flow for disabling the PRI on a device. Refactor the PRI callbacks in the intel iommu driver to better manage PRI enabling and disabling and align it with the device queue interfaces in the iommu core. Signed-off-by: Lu Baolu --- drivers/iommu/intel/iommu.h | 9 +++++ drivers/iommu/intel/iommu.c | 77 ++++++++++++++++++++++++++++++++++--- drivers/iommu/intel/pasid.c | 2 - 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index eaf015b4353b..3d5d8f786906 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -1047,6 +1047,15 @@ static inline void context_set_sm_pre(struct context_entry *context) context->lo |= BIT_ULL(4); } +/* + * Clear the PRE(Page Request Enable) field of a scalable mode context + * entry. + */ +static inline void context_clear_sm_pre(struct context_entry *context) +{ + context->lo &= ~BIT_ULL(4); +} + /* Returns a number of VTD pages, but aligned to MM page size */ static inline unsigned long aligned_nrpages(unsigned long host_addr, size_t size) { diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 2e9811bf2a4e..2d4b122bcc1c 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -4213,6 +4213,57 @@ static int intel_iommu_enable_sva(struct device *dev) return 0; } +/* + * Invalidate the caches for a present-to-present change in a context + * table entry according to the Spec 6.5.3.3 (Guidance to Software for + * Invalidations). + * + * Since context entry is not encoded by domain-id when operating in + * scalable-mode (refer Section 6.2.1), this performs coarser + * invalidation than the domain-selective granularity requested. + */ +static void invalidate_present_context_change(struct device_domain_info *info) +{ + struct intel_iommu *iommu = info->iommu; + + iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL); + if (sm_supported(iommu)) + qi_flush_pasid_cache(iommu, 0, QI_PC_GLOBAL, 0); + iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); + __iommu_flush_dev_iotlb(info, 0, MAX_AGAW_PFN_WIDTH); +} + +static int context_flip_pri(struct device_domain_info *info, bool enable) +{ + struct intel_iommu *iommu = info->iommu; + u8 bus = info->bus, devfn = info->devfn; + struct context_entry *context; + + spin_lock(&iommu->lock); + if (context_copied(iommu, bus, devfn)) { + spin_unlock(&iommu->lock); + return -EINVAL; + } + + context = iommu_context_addr(iommu, bus, devfn, false); + if (!context || !context_present(context)) { + spin_unlock(&iommu->lock); + return -ENODEV; + } + + if (enable) + context_set_sm_pre(context); + else + context_clear_sm_pre(context); + + if (!ecap_coherent(iommu->ecap)) + clflush_cache_range(context, sizeof(*context)); + invalidate_present_context_change(info); + spin_unlock(&iommu->lock); + + return 0; +} + static int intel_iommu_enable_iopf(struct device *dev) { struct pci_dev *pdev = dev_is_pci(dev) ? to_pci_dev(dev) : NULL; @@ -4242,15 +4293,23 @@ static int intel_iommu_enable_iopf(struct device *dev) if (ret) return ret; + ret = context_flip_pri(info, true); + if (ret) + goto err_remove_device; + ret = pci_enable_pri(pdev, PRQ_DEPTH); - if (ret) { - iopf_queue_remove_device(iommu->iopf_queue, dev); - return ret; - } + if (ret) + goto err_clear_pri; info->pri_enabled = 1; return 0; +err_clear_pri: + context_flip_pri(info, false); +err_remove_device: + iopf_queue_remove_device(iommu->iopf_queue, dev); + + return ret; } static int intel_iommu_disable_iopf(struct device *dev) @@ -4261,6 +4320,15 @@ static int intel_iommu_disable_iopf(struct device *dev) if (!info->pri_enabled) return -EINVAL; + /* Disable new PRI reception: */ + context_flip_pri(info, false); + + /* + * Remove device from fault queue and acknowledge all outstanding + * PRQs to the device: + */ + iopf_queue_remove_device(iommu->iopf_queue, dev); + /* * PCIe spec states that by clearing PRI enable bit, the Page * Request Interface will not issue new page requests, but has @@ -4271,7 +4339,6 @@ static int intel_iommu_disable_iopf(struct device *dev) */ pci_disable_pri(to_pci_dev(dev)); info->pri_enabled = 0; - iopf_queue_remove_device(iommu->iopf_queue, dev); return 0; } diff --git a/drivers/iommu/intel/pasid.c b/drivers/iommu/intel/pasid.c index abce19e2ad6f..286a8a7d66f5 100644 --- a/drivers/iommu/intel/pasid.c +++ b/drivers/iommu/intel/pasid.c @@ -768,8 +768,6 @@ static int context_entry_set_pasid_table(struct context_entry *context, if (info->ats_supported) context_set_sm_dte(context); - if (info->pri_supported) - context_set_sm_pre(context); if (info->pasid_supported) context_set_pasid(context); -- 2.34.1