Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3172474yba; Sun, 28 Apr 2019 19:18:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqyP7mO4gOqToaJ0CtMKEpFaRQGUimi2ueLAcKTngncC+TZqIgjcm333In1EPQp+6zBgeSjo X-Received: by 2002:a17:902:bd4a:: with SMTP id b10mr1506819plx.75.1556504296015; Sun, 28 Apr 2019 19:18:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556504296; cv=none; d=google.com; s=arc-20160816; b=A3aUOeiHm0y21Ch+xK6ycTO3TNoYpVQimPdD5FuizR5LmDOVDmDWrU0ywAjieuLqhu pK1wwDRXV/mnVsQRplyDhKtHV/vnHBdDGdeLl39CaxkKKzL0j+SKirabZxMy9GxrzPX6 KdQCE95ZVGJTiiuh4des/FNqAhR4JKeb4H0h/f7ekaWT9EVXqj0ls/ypdS/HgbuotP7s YoG16h+it+/tzZAvxy9cNWed5wvdcXIBSIYCBHSQg8RlgJjEWvGvfdpbTCesz+C6jzbY A2GEF0KKKzbt1GqYe77E3+wHeJtUoJm4eSB6krMwRe20o+GIfxoiLAzIrPTkeKXy8eNV r2KA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=Pt+q/ddk2oWiAPj7JIYYUPLYghE2j/r9GAO2ibSpjd8=; b=uRzSJCRa+7H+UYJQahwbKsV21E2ZaGVwbr1Tu/Cd0fU1xTCedF6lRMQp6h43ILHL0o jXJBXIIuNhln4Y4NWBlwWSGXmqg6lk3R6PvmvBgyqeGLuDeepQswfaSC9rkPyJGmDUXT fACsG29FgH14OZ45rhj4CtBs8tjg5Wg3BUr/4y+jzPLxCDfHz6WABNGfR90tCJVgiTMq VQC+TZxvbSDd18Rk2sCtaypMV6E9vwyjeCmxEzVgWtIiRj2SWp+hY4H5by5WVwPLPCN2 +Kw5Irq6eYSl4FcIipU/lB2ur5lcgaxHKymulzbZekv1j1rVFIQL3snoBivSBLNOOMVT bSug== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j1si7135199pfi.13.2019.04.28.19.18.00; Sun, 28 Apr 2019 19:18:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727335AbfD2CQ1 (ORCPT + 99 others); Sun, 28 Apr 2019 22:16:27 -0400 Received: from mga03.intel.com ([134.134.136.65]:46364 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727246AbfD2CQM (ORCPT ); Sun, 28 Apr 2019 22:16:12 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Apr 2019 19:16:11 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,408,1549958400"; d="scan'208";a="146537869" Received: from allen-box.sh.intel.com ([10.239.159.136]) by orsmga003.jf.intel.com with ESMTP; 28 Apr 2019 19:16:09 -0700 From: Lu Baolu To: David Woodhouse , Joerg Roedel Cc: ashok.raj@intel.com, jacob.jun.pan@intel.com, kevin.tian@intel.com, jamessewart@arista.com, tmurphy@arista.com, dima@arista.com, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v3 6/8] iommu/vt-d: Allow DMA domains to be allocated by iommu ops Date: Mon, 29 Apr 2019 10:09:23 +0800 Message-Id: <20190429020925.18136-7-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190429020925.18136-1-baolu.lu@linux.intel.com> References: <20190429020925.18136-1-baolu.lu@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: James Sewart Allowing IOMMU_DOMAIN_DMA and IOMMU_DOMAIN_IDENTITY types of domains to be allocated through iommu ops allows the default domain of an iommu_group to be set. This delegates domains of Intel IOMMU driver to the generic IOMMU layer. Signed-off-by: James Sewart Signed-off-by: Lu Baolu --- drivers/iommu/intel-iommu.c | 124 ++++++++++++++++++++++++++---------- 1 file changed, 90 insertions(+), 34 deletions(-) diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c index d2b51e045603..ec6ac39827ab 100644 --- a/drivers/iommu/intel-iommu.c +++ b/drivers/iommu/intel-iommu.c @@ -311,6 +311,12 @@ static int hw_pass_through = 1; /* si_domain contains mulitple devices */ #define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1) +/* + * Domain managed externally, don't cleanup if it isn't attached + * to any devices. + */ +#define DOMAIN_FLAG_MANAGED_EXTERNALLY (1 << 2) + #define for_each_domain_iommu(idx, domain) \ for (idx = 0; idx < g_num_of_iommus; idx++) \ if (domain->iommu_refcnt[idx]) @@ -561,6 +567,11 @@ static inline int domain_type_is_vm_or_si(struct dmar_domain *domain) DOMAIN_FLAG_STATIC_IDENTITY); } +static inline int domain_managed_externally(struct dmar_domain *domain) +{ + return domain->flags & DOMAIN_FLAG_MANAGED_EXTERNALLY; +} + static inline int domain_pfn_supported(struct dmar_domain *domain, unsigned long pfn) { @@ -1671,7 +1682,7 @@ static void disable_dmar_iommu(struct intel_iommu *iommu) __dmar_remove_one_dev_info(info); - if (!domain_type_is_vm_or_si(domain)) { + if (!domain_managed_externally(domain)) { /* * The domain_exit() function can't be called under * device_domain_lock, as it takes this lock itself. @@ -2366,7 +2377,12 @@ static int domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, } else { /* General domains only have one IOMMU */ iommu = domain_get_iommu(domain); - __mapping_notify_one(iommu, domain, iov_pfn, nr_pages); + /* + * There's no associated iommu if domain hasn't been attached + * to any device yet. + */ + if (iommu) + __mapping_notify_one(iommu, domain, iov_pfn, nr_pages); } return 0; @@ -3406,12 +3422,9 @@ static int __init init_dmars(void) check_tylersburg_isoch(); - if (iommu_identity_mapping) { - ret = si_domain_init(hw_pass_through); - if (ret) - goto free_iommu; - } - + ret = si_domain_init(hw_pass_through); + if (ret) + goto free_iommu; /* * If we copied translations from a previous kernel in the kdump @@ -4572,7 +4585,7 @@ static int device_notifier(struct notifier_block *nb, return 0; dmar_remove_one_dev_info(dev); - if (!domain_type_is_vm_or_si(domain) && + if (!domain_managed_externally(domain) && list_empty(&domain->devices)) domain_exit(domain); } else if (action == BUS_NOTIFY_ADD_DEVICE) { @@ -5038,33 +5051,52 @@ static struct iommu_domain *intel_iommu_domain_alloc(unsigned type) { struct dmar_domain *dmar_domain; struct iommu_domain *domain; + int flags = DOMAIN_FLAG_MANAGED_EXTERNALLY; - if (type != IOMMU_DOMAIN_UNMANAGED) - return NULL; + switch (type) { + case IOMMU_DOMAIN_UNMANAGED: + flags |= DOMAIN_FLAG_VIRTUAL_MACHINE; + /* fall through */ + case IOMMU_DOMAIN_DMA: + dmar_domain = alloc_domain(flags); + if (!dmar_domain) + return NULL; - dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE); - if (!dmar_domain) { - pr_err("Can't allocate dmar_domain\n"); - return NULL; - } - if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) { - pr_err("Domain initialization failed\n"); - domain_exit(dmar_domain); + if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) { + pr_err("Domain initialization failed\n"); + domain_exit(dmar_domain); + return NULL; + } + + if (type == IOMMU_DOMAIN_DMA && + init_iova_flush_queue(&dmar_domain->iovad, + iommu_flush_iova, iova_entry_free)) { + pr_warn("iova flush queue initialization failed\n"); + intel_iommu_strict = 1; + } + + domain_update_iommu_cap(dmar_domain); + domain = &dmar_domain->domain; + domain->geometry.aperture_start = 0; + domain->geometry.aperture_end = + __DOMAIN_MAX_ADDR(dmar_domain->gaw); + domain->geometry.force_aperture = true; + break; + case IOMMU_DOMAIN_IDENTITY: + return &si_domain->domain; + default: return NULL; } - domain_update_iommu_cap(dmar_domain); - - domain = &dmar_domain->domain; - domain->geometry.aperture_start = 0; - domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw); - domain->geometry.force_aperture = true; - return domain; + return &dmar_domain->domain; } static void intel_iommu_domain_free(struct iommu_domain *domain) { - domain_exit(to_dmar_domain(domain)); + struct dmar_domain *dmar_domain = to_dmar_domain(domain); + + if (domain_managed_externally(dmar_domain)) + domain_exit(dmar_domain); } /* @@ -5240,13 +5272,33 @@ static int intel_iommu_attach_device(struct iommu_domain *domain, { int ret; - if (device_is_rmrr_locked(dev)) { - dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n"); - return -EPERM; - } + switch (domain->type) { + case IOMMU_DOMAIN_UNMANAGED: + if (device_is_rmrr_locked(dev)) { + dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n"); + return -EPERM; + } - if (is_aux_domain(dev, domain)) - return -EPERM; + if (is_aux_domain(dev, domain)) + return -EPERM; + + break; + case IOMMU_DOMAIN_DMA: + if (intel_iommu_def_domain_type(dev) == IOMMU_DOMAIN_IDENTITY) { + dev_warn(dev, "Device requires identity domain\n"); + return -EINVAL; + } + break; + case IOMMU_DOMAIN_IDENTITY: + if (intel_iommu_def_domain_type(dev) == IOMMU_DOMAIN_DMA) { + dev_warn(dev, "Device requires non-identity domain\n"); + return -EINVAL; + } + break; + default: + dev_warn(dev, "Invalid domain type %u\n", domain->type); + return -EINVAL; + } /* normally dev is not mapped */ if (unlikely(domain_context_mapped(dev))) { @@ -5258,7 +5310,7 @@ static int intel_iommu_attach_device(struct iommu_domain *domain, dmar_remove_one_dev_info(dev); rcu_read_unlock(); - if (!domain_type_is_vm_or_si(old_domain) && + if (!domain_managed_externally(old_domain) && list_empty(&old_domain->devices)) domain_exit(old_domain); } @@ -5307,6 +5359,10 @@ static int intel_iommu_map(struct iommu_domain *domain, int prot = 0; int ret; + /* Don't bother if hardware passthrough used. */ + if (dmar_domain == si_domain && hw_pass_through) + return 0; + if (iommu_prot & IOMMU_READ) prot |= DMA_PTE_READ; if (iommu_prot & IOMMU_WRITE) -- 2.17.1