Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp3097439rdb; Mon, 4 Dec 2023 17:27:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IHC4n3NG2B8IjR+IqDRIrbl07szvq+X75MHz88V5I7x75gVL2kDvRXjYqtekPcKa0bgfxE+ X-Received: by 2002:a05:6358:24aa:b0:170:756:90fd with SMTP id m42-20020a05635824aa00b00170075690fdmr4749588rwc.19.1701739622399; Mon, 04 Dec 2023 17:27:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701739622; cv=none; d=google.com; s=arc-20160816; b=g9QJOdfnN92X8DwGjneMjKGz6HD+nDGpxEgi/abcOq0GZMUfmJqQcvArDavcPZkMFU q5ubGnSPHHgQ3kXuRAfjM4twrIHIk0R/NV4iCZElzaimjOCoZO+fyob1RgMVB3cg/Qc2 aCCLbfNL03Gg1+mMzKhKoYfJ0aHHksJoY3n6ocJzVMWIxhNaR4sR0xCJotkv1Evblt8N 1PXR7fi0qCnSUB4bvgYs9zRsUYm0VG0ta6SXJLzwom/6j4VqFzvJyY+pYxfrgrB/GOno E0fnCi0kp2Kg8tJcIlDLhmKqtVbGh/nT78VyOQFOm1Z4JIvZPp1TdM2wWfDhDkpXxGSG a3fQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tpDdb98tk8zw1cj5dzIynH6qv+WHcH2U079WXO5IXls=; fh=5K4v/0ku97kpa0vTvhWLyUEWwXZiIFVMmzwLLirrjJ4=; b=HDECbhqWYC2Nu5MwDArXi+Qx4V/fTvQVlcrRYqHJdmKROHNCYIknXvZqSpZjDuY1dK 2MSnuLSlDNXEd21b02ou7pnUt9LEhcqgrrlsnQVozBp0ZwgZHxgsmBLd4WPbk5gFWUCY dPoFimxwJnrzB0ciKa9SeeQTT+/Wc9F2zCMWNGAemfFYfBGsnIgmVPTzKVObt2L3KCZd vlJeW7A2+XjsrtEzj5gLzs4qg5WF5lfXg/RPeOnI87YFPugtthFLcPxZYe26rLK+kW3R d6l7KwjC4tvW8dMs6DLaunpye9hT5aiiDpC5+QwFgxWEreNKJeRFnO9vTKzYrBpVAp7G 7R4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kGbXTOfw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id q6-20020a656846000000b005bdd8dcfe21si2586927pgt.195.2023.12.04.17.27.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 17:27:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kGbXTOfw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 044138047460; Mon, 4 Dec 2023 17:27:00 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234803AbjLEB0r (ORCPT + 99 others); Mon, 4 Dec 2023 20:26:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343956AbjLEB0n (ORCPT ); Mon, 4 Dec 2023 20:26:43 -0500 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F339135 for ; Mon, 4 Dec 2023 17:26:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701739609; x=1733275609; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Hu+gkVWZE83d4KZHeWPMt+YqjiEu98XZ0PIi+Ei4+Wc=; b=kGbXTOfw7rmc2yHYFRp2bYzEnow3iLpmg9BXkjkBADc6nVk/PPgsdyaz zfHOaFcYUGu830o36kiLSUmDgcu1coITXIPmXjAMJsy3oTON3aLFtDA2N TnOLai1IXQNaxpkNSdUeRK/BccY4ircQ4zxiYE1mmhmMCBKYkIw+0jfju dfbskFqk2tqcL0wMJ9AvVFkeECBNW4t4LKRK9OoUAz6Sn6JHZvvSG9GjS /zhnWP5meGjgv9YOTCRS6EaZNaRYtXagNKt/KwcOk1e5sLGxBvkEanu1t 0ppzIiPhC0nsU+68avY0BloCf9qqvqXiDAC8NsL2lcM14uXV1iQCmtK7n Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10914"; a="460313349" X-IronPort-AV: E=Sophos;i="6.04,251,1695711600"; d="scan'208";a="460313349" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2023 17:26:48 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10914"; a="1102276239" X-IronPort-AV: E=Sophos;i="6.04,251,1695711600"; d="scan'208";a="1102276239" Received: from allen-box.sh.intel.com ([10.239.159.127]) by fmsmga005.fm.intel.com with ESMTP; 04 Dec 2023 17:26:46 -0800 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 v2 3/6] iommu/vt-d: Refactor domain_context_mapping_one() to be reusable Date: Tue, 5 Dec 2023 09:22:00 +0800 Message-Id: <20231205012203.244584-4-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231205012203.244584-1-baolu.lu@linux.intel.com> References: <20231205012203.244584-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 04 Dec 2023 17:27:01 -0800 (PST) Extract common code from domain_context_mapping_one() into new functions, making it reusable by other functions such as the upcoming identity domain implementation. No intentional functional changes. Signed-off-by: Lu Baolu --- drivers/iommu/intel/iommu.c | 99 ++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 41 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index a324b3a3a005..605cd1c52e95 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -1727,6 +1727,61 @@ static void domain_exit(struct dmar_domain *domain) kfree(domain); } +/* + * For kdump cases, old valid entries may be cached due to the + * in-flight DMA and copied pgtable, but there is no unmapping + * behaviour for them, thus we need an explicit cache flush for + * the newly-mapped device. For kdump, at this point, the device + * is supposed to finish reset at its driver probe stage, so no + * in-flight DMA will exist, and we don't need to worry anymore + * hereafter. + */ +static void copied_context_tear_down(struct intel_iommu *iommu, + struct context_entry *context, + u8 bus, u8 devfn) +{ + u16 did_old; + + if (!context_copied(iommu, bus, devfn)) + return; + + assert_spin_locked(&iommu->lock); + + did_old = context_domain_id(context); + context_clear_entry(context); + + if (did_old < cap_ndoms(iommu->cap)) { + iommu->flush.flush_context(iommu, did_old, + (((u16)bus) << 8) | devfn, + DMA_CCMD_MASK_NOBIT, + DMA_CCMD_DEVICE_INVL); + iommu->flush.flush_iotlb(iommu, did_old, 0, 0, + DMA_TLB_DSI_FLUSH); + } + + clear_context_copied(iommu, bus, devfn); +} + +/* + * It's a non-present to present mapping. If hardware doesn't cache + * non-present entry we only need to flush the write-buffer. If the + * _does_ cache non-present entries, then it does so in the special + * domain #0, which we have to flush: + */ +static void context_present_cache_flush(struct intel_iommu *iommu, u16 did, + u8 bus, u8 devfn) +{ + if (cap_caching_mode(iommu->cap)) { + iommu->flush.flush_context(iommu, 0, + (((u16)bus) << 8) | devfn, + DMA_CCMD_MASK_NOBIT, + DMA_CCMD_DEVICE_INVL); + iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH); + } else { + iommu_flush_write_buffer(iommu); + } +} + static int domain_context_mapping_one(struct dmar_domain *domain, struct intel_iommu *iommu, u8 bus, u8 devfn) @@ -1755,31 +1810,9 @@ static int domain_context_mapping_one(struct dmar_domain *domain, if (context_present(context) && !context_copied(iommu, bus, devfn)) goto out_unlock; - /* - * For kdump cases, old valid entries may be cached due to the - * in-flight DMA and copied pgtable, but there is no unmapping - * behaviour for them, thus we need an explicit cache flush for - * the newly-mapped device. For kdump, at this point, the device - * is supposed to finish reset at its driver probe stage, so no - * in-flight DMA will exist, and we don't need to worry anymore - * hereafter. - */ - if (context_copied(iommu, bus, devfn)) { - u16 did_old = context_domain_id(context); - - if (did_old < cap_ndoms(iommu->cap)) { - iommu->flush.flush_context(iommu, did_old, - (((u16)bus) << 8) | devfn, - DMA_CCMD_MASK_NOBIT, - DMA_CCMD_DEVICE_INVL); - iommu->flush.flush_iotlb(iommu, did_old, 0, 0, - DMA_TLB_DSI_FLUSH); - } - - clear_context_copied(iommu, bus, devfn); - } - + copied_context_tear_down(iommu, context, bus, devfn); context_clear_entry(context); + context_set_domain_id(context, did); if (translation != CONTEXT_TT_PASS_THROUGH) { @@ -1815,23 +1848,7 @@ static int domain_context_mapping_one(struct dmar_domain *domain, context_set_present(context); if (!ecap_coherent(iommu->ecap)) clflush_cache_range(context, sizeof(*context)); - - /* - * It's a non-present to present mapping. If hardware doesn't cache - * non-present entry we only need to flush the write-buffer. If the - * _does_ cache non-present entries, then it does so in the special - * domain #0, which we have to flush: - */ - if (cap_caching_mode(iommu->cap)) { - iommu->flush.flush_context(iommu, 0, - (((u16)bus) << 8) | devfn, - DMA_CCMD_MASK_NOBIT, - DMA_CCMD_DEVICE_INVL); - iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH); - } else { - iommu_flush_write_buffer(iommu); - } - + context_present_cache_flush(iommu, did, bus, devfn); ret = 0; out_unlock: -- 2.34.1