From patchwork Sat Dec 2 09:25:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yan Zhao X-Patchwork-Id: 172778 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp1675634vqy; Sat, 2 Dec 2023 01:54:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IETEDqwGf26bNVqJcVOLX7LKDMYAV3kWv7OhSwYKuargZU8FTqBfdfxqtRjq8kLKR2AWn9N X-Received: by 2002:a05:6a20:12ce:b0:18f:97c:9776 with SMTP id v14-20020a056a2012ce00b0018f097c9776mr1453303pzg.94.1701510871556; Sat, 02 Dec 2023 01:54:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701510871; cv=none; d=google.com; s=arc-20160816; b=Y9uRsc1W5dTOzIGt3sepwy3ne3/+KhfZAg2Zt9Dddf/8AmKQURBkTn5HgzdEp6d817 NumslCf3GrquV227OLzyhAR7Qxqi0RuiP81rQA6FX4q1wP9hxijXOzKtPLDUbaW20o3K XIAW2qX1epUSt3cOX6oVtq4U3v0EMTAjR8tp+Ps/XWJgnq4FVq+ido5hsDJ1nGY9kn+j D8X7GH2wesxKfeSbCPM5ic48YieLljxtmHsei4Wd5jNc9hfOmnoV7GiSx/CONZhJMYw2 WxDKMD2C6H4EeKslKnuwvoJFqAys9acJmnNPFMKSUCQuSOUB0QiriGa3LhYn0pD6YXYZ quDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=Pn19RCNrPihsrg4QJFvANDIdAEV5QDf1OvhCwgVqSWE=; fh=+WI4m5k3dRLR+dR3neThuZkNBTzIm/a8HgtddERL9fA=; b=AIBnXXtFYbhfSEICNh+w7lN7JHuh+lSg82ViutytPZJh9gPCWyzVA0S1McHynWI/8y f0Iz5h2BK6hFdqPQqVFffxH7HmnBVECNviidj50nMB+Soa0ss2oZiaBvCgtdpKxP2C7Y 29fZc6BaPf2eUJQlN3JzQyPm9JZOkUQkZQgiTsmbwViLXiUPvT5smft5ozS2BRdkroKB 1Hu0l5BJIJRwQEoRTU3crsmGUImrivwP2fDU0wv0vJsEK5yH6WQ6to8PVTbfS1Lw1h7T oS0iSimSI1Eeu4wUikpqAFKNif8cnGnJzEA++X1x5bmsdLXn8l7OEyaGwqV6+cRS52oO ZIxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=icl1q9Od; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id bw25-20020a056a02049900b005c65ee483edsi847192pgb.655.2023.12.02.01.54.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 01:54:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=icl1q9Od; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (Postfix) with ESMTP id B6EEE80EA789; Sat, 2 Dec 2023 01:54:27 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232416AbjLBJyS (ORCPT + 99 others); Sat, 2 Dec 2023 04:54:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232359AbjLBJyQ (ORCPT ); Sat, 2 Dec 2023 04:54:16 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6ECBCC; Sat, 2 Dec 2023 01:54:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701510862; x=1733046862; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=CgOHdWHDwhSGt7y0GRo0NgHb+w74ehYb80zPfydmvj4=; b=icl1q9OdqATdQwtP/FyMtCYDZbDn0HnlhxcsatMDLyMDZXhAVHmTW/zf YhO0mEv29762zymrDL7ukM5HwZAvgCXHWLpJGRDtG+fQ9j3P1IodEzy5G 8cMVbIPzycmfrvbzszdJoK+Z9tmy+QnylTtbIcAE075LW6+G6mgd+nUMC Q6ZV1sC3Se+Ea8b5oVSkLPXmy+TmMaXeWdbSIpO9N1G0spcsTTNteLdEx aALNu7hQClE0VQ23u46xSK8BRok3ouVvG8aI8D0aK8aGVmYdFahKMoWOF whfuWZtwYgl2S9ZMIkZb7WLYSWNoLTRGX+F6/Sb6Eap0mwpYWzgqZXneR w==; X-IronPort-AV: E=McAfee;i="6600,9927,10911"; a="424756075" X-IronPort-AV: E=Sophos;i="6.04,245,1695711600"; d="scan'208";a="424756075" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2023 01:54:22 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10911"; a="836021580" X-IronPort-AV: E=Sophos;i="6.04,245,1695711600"; d="scan'208";a="836021580" Received: from yzhao56-desk.sh.intel.com ([10.239.159.62]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2023 01:54:19 -0800 From: Yan Zhao To: iommu@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: alex.williamson@redhat.com, jgg@nvidia.com, pbonzini@redhat.com, seanjc@google.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, kevin.tian@intel.com, baolu.lu@linux.intel.com, dwmw2@infradead.org, yi.l.liu@intel.com, Yan Zhao Subject: [RFC PATCH 20/42] iommu/vt-d: Support attach devices to IOMMU_DOMAIN_KVM domain Date: Sat, 2 Dec 2023 17:25:24 +0800 Message-Id: <20231202092524.14647-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20231202091211.13376-1-yan.y.zhao@intel.com> References: <20231202091211.13376-1-yan.y.zhao@intel.com> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email 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 (fry.vger.email [0.0.0.0]); Sat, 02 Dec 2023 01:54:28 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784163463403989974 X-GMAIL-MSGID: 1784163463403989974 IOMMU_DOMAIN_KVM domain reuses intel_iommu_attach_device() for device attachment. But unlike attaching to other dmar_domain, domain caps (e.g. iommu_superpage) are not updated after device attach. Instead, IOMMU caps are checked for compatibility before domain attachment. Signed-off-by: Yan Zhao --- drivers/iommu/intel/iommu.c | 11 +++++++++++ drivers/iommu/intel/iommu.h | 7 +++++++ drivers/iommu/intel/kvm.c | 9 +++++++++ 3 files changed, 27 insertions(+) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index fcdee40f30ed1..9cc42b3d24f65 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -552,6 +552,13 @@ static unsigned long domain_super_pgsize_bitmap(struct dmar_domain *domain) /* Some capabilities may be different across iommus */ void domain_update_iommu_cap(struct dmar_domain *domain) { + /* + * No need to adjust iommu cap of kvm domain. + * Instead, iommu will be checked in pre-attach phase. + */ + if (domain_type_is_kvm(domain)) + return; + domain_update_iommu_coherency(domain); domain->iommu_superpage = domain_update_iommu_superpage(domain, NULL); @@ -4104,6 +4111,9 @@ int prepare_domain_attach_device(struct iommu_domain *domain, if (!iommu) return -ENODEV; + if (domain_type_is_kvm(dmar_domain)) + return prepare_kvm_domain_attach(dmar_domain, iommu); + if (dmar_domain->force_snooping && !ecap_sc_support(iommu->ecap)) return -EINVAL; @@ -4117,6 +4127,7 @@ int prepare_domain_attach_device(struct iommu_domain *domain, if (dmar_domain->max_addr > (1LL << addr_width)) return -EINVAL; + dmar_domain->gaw = addr_width; /* diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 8826e9248f6ed..801700bc7d820 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -1059,6 +1059,13 @@ static inline int width_to_agaw(int width) #ifdef CONFIG_INTEL_IOMMU_KVM struct iommu_domain * intel_iommu_domain_alloc_kvm(struct device *dev, u32 flags, const void *data); +int prepare_kvm_domain_attach(struct dmar_domain *domain, struct intel_iommu *iommu); +#else +static inline int prepare_kvm_domain_attach(struct dmar_domain *domain, + struct intel_iommu *iommu) +{ + return 0; +} #endif #endif diff --git a/drivers/iommu/intel/kvm.c b/drivers/iommu/intel/kvm.c index 188ec90083051..1ce334785430b 100644 --- a/drivers/iommu/intel/kvm.c +++ b/drivers/iommu/intel/kvm.c @@ -32,6 +32,14 @@ static bool is_iommu_cap_compatible_to_kvm_domain(struct dmar_domain *domain, return true; } +int prepare_kvm_domain_attach(struct dmar_domain *domain, struct intel_iommu *iommu) +{ + if (is_iommu_cap_compatible_to_kvm_domain(domain, iommu)) + return 0; + + return -EINVAL; +} + /* * Cache coherency is always enforced in KVM domain. * IOMMU hardware caps will be checked to allow the cache coherency before @@ -43,6 +51,7 @@ static bool kvm_domain_enforce_cache_coherency(struct iommu_domain *domain) } static const struct iommu_domain_ops intel_kvm_domain_ops = { + .attach_dev = intel_iommu_attach_device, .free = intel_iommu_domain_free, .enforce_cache_coherency = kvm_domain_enforce_cache_coherency, };