Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1591112rdb; Sat, 2 Dec 2023 01:54:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IGgqHAW0NXv2wcUlkUeFEqtNbsqN8zDKtJvRtQTvL/WSoxkRo/V6eEjPDjGsHDcrSRJKTGw X-Received: by 2002:a05:6a00:438d:b0:6ce:2732:590 with SMTP id bt13-20020a056a00438d00b006ce27320590mr1198834pfb.65.1701510875069; Sat, 02 Dec 2023 01:54:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701510875; cv=none; d=google.com; s=arc-20160816; b=bBFfRXji1H2CALJ4zSzSq0uTb3PyyQdz8Ct7tY+FYoGIJg8QI4ZOfMe8ga/qzCtvLh yEKg6NTWH8tKtOONq5WGBFFK/QOkM6zwsCXTTxVIUS/t/4UFFZWmDFNDdIvybnGAHXuC Gjg+EqlFVfvZKhvA9GIQOMHtmqJjZtPJvXK1bZDC9y39O25HrXqqWzztAkMDbko8ZWjs BxvxXwT1gLrLF/0/4cxiPjI85Iwa9i5iN6GoAOA6BLxP/MaGGRwB+DUDaIypV5VMxvmE Uwmc4wzzFE4cJFR1Tp3JeSwcwgvD6mQJmww6cQT8ZVj7dR/1f+Q+8BaJ+g3Tkg4Tf+Vs 9OeQ== 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 Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id e17-20020a62aa11000000b006cd82b3585esi4591864pff.287.2023.12.02.01.54.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 01:54:35 -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 6951480EA791; Sat, 2 Dec 2023 01:54:32 -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:32 -0800 (PST) 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, }; -- 2.17.1