Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1319793ybi; Fri, 24 May 2019 22:52:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqwX6GYNPpqXANcLJUCXPycvsP8tnoOXsce7ofQM4tfFqm9ujZvpGH4+KApFUahUc9TrdYtd X-Received: by 2002:a65:6145:: with SMTP id o5mr9022745pgv.262.1558763571546; Fri, 24 May 2019 22:52:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558763571; cv=none; d=google.com; s=arc-20160816; b=VdMBHYl49fG+MNU8Zfd2+XaCkWiIxc3e8MjBO5QjVl74HrCnrZzlNG2vT49Yek8e5l lYxsIN1SR3vABO2+oEjAiOnn2eArjFCvEFqMRs9P0sTPSz0npw3B0fh7zOgELVdU+63B s+lV/4IC6oHa2ZOZ832hfDFXwyKPd3f9SGc31PpDNOtBP2Ys7Zor5jOvOzn22eQRhLay o/1p7kq5Rx59WtneCHpy+MXxPTZBPoIqT5h6W3Q1kfAz2dGIZwCHgQcfvgGITQ05kSlI nj4eJ4Sd/Jx8KD3HrsYHxy2ZdHtmHLaqs471HQZq2m0W1ULGftaPBNXOyZ4nND6sjV3L wjEw== 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=XcCOjOI4YIer9gLFYMRFu5krxVPYYqsueBzmCnD3mdk=; b=08Y5dIYI5NGRLXM3xG6bzZLNVWr8Qck12Hco+Wm7SxU5gTsV0lXJ0DQ/cC6g6SF1RJ XCVJQ2iQdGph4e0nbZvY2N7katN0Pxmd9Rpj/F0LJJSa8J9iaYKYYEYOe/0YEz/gCrU1 vLhz836oZHIHNZjujwrs/K/S5VioVmLTHWQzcPxmbdoD9NGJr0UL9x9bMMPOTvnk9zVA 5g1fX4q6gCAkyJgjgZ1GCGzIJcP0g7+h2xulU+M8HgrGTRCAdRdIkE17YVCitifwj9kf LyLjiYEB6ztOLRQj8D5XYKDo2Q3kn+zASaQ5mwMa33tzgD+8kL94etXdMMHAQAmIfwIL FUOA== 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 w13si6579988pge.212.2019.05.24.22.52.36; Fri, 24 May 2019 22:52:51 -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 S1727069AbfEYFtl (ORCPT + 99 others); Sat, 25 May 2019 01:49:41 -0400 Received: from mga05.intel.com ([192.55.52.43]:46780 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726802AbfEYFs7 (ORCPT ); Sat, 25 May 2019 01:48:59 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 May 2019 22:48:59 -0700 X-ExtLoop1: 1 Received: from allen-box.sh.intel.com ([10.239.159.136]) by fmsmga006.fm.intel.com with ESMTP; 24 May 2019 22:48:56 -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, sai.praneeth.prakhya@intel.com, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v4 06/15] iommu/vt-d: Delegate the identity domain to upper layer Date: Sat, 25 May 2019 13:41:27 +0800 Message-Id: <20190525054136.27810-7-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190525054136.27810-1-baolu.lu@linux.intel.com> References: <20190525054136.27810-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 This allows the iommu generic layer to allocate an identity domain and attach it to a device. Hence, the identity domain is delegated to upper layer. As a side effect, iommu_identity_mapping can't be used to check the existence of identity domains any more. Signed-off-by: James Sewart Signed-off-by: Lu Baolu --- drivers/iommu/intel-iommu.c | 90 ++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 32 deletions(-) diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c index e1663af4e01b..907f9323921d 100644 --- a/drivers/iommu/intel-iommu.c +++ b/drivers/iommu/intel-iommu.c @@ -349,6 +349,7 @@ static void domain_context_clear(struct intel_iommu *iommu, struct device *dev); static int domain_detach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu); +static bool device_is_rmrr_locked(struct device *dev); #ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON int dmar_disabled = 0; @@ -2835,7 +2836,9 @@ static int md_domain_init(struct dmar_domain *domain, int guest_width); static int __init si_domain_init(int hw) { - int nid, ret; + struct dmar_rmrr_unit *rmrr; + struct device *dev; + int i, nid, ret; si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY); if (!si_domain) @@ -2846,8 +2849,6 @@ static int __init si_domain_init(int hw) return -EFAULT; } - pr_debug("Identity mapping domain allocated\n"); - if (hw) return 0; @@ -2863,6 +2864,31 @@ static int __init si_domain_init(int hw) } } + /* + * Normally we use DMA domains for devices which have RMRRs. But we + * loose this requirement for graphic and usb devices. Identity map + * the RMRRs for graphic and USB devices so that they could use the + * si_domain. + */ + for_each_rmrr_units(rmrr) { + for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt, + i, dev) { + unsigned long long start = rmrr->base_address; + unsigned long long end = rmrr->end_address; + + if (device_is_rmrr_locked(dev)) + continue; + + if (WARN_ON(end < start || + end >> agaw_to_width(si_domain->agaw))) + continue; + + ret = iommu_domain_identity_map(si_domain, start, end); + if (ret) + return ret; + } + } + return 0; } @@ -2870,9 +2896,6 @@ static int identity_mapping(struct device *dev) { struct device_domain_info *info; - if (likely(!iommu_identity_mapping)) - return 0; - info = dev->archdata.iommu; if (info && info != DUMMY_DEVICE_DOMAIN_INFO) return (info->domain == si_domain); @@ -3459,11 +3482,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; /* @@ -3656,9 +3677,6 @@ static bool iommu_need_mapping(struct device *dev) if (iommu_dummy(dev)) return false; - if (!iommu_identity_mapping) - return true; - found = identity_mapping(dev); if (found) { if (iommu_should_identity_map(dev, 0)) @@ -5071,32 +5089,40 @@ static struct iommu_domain *intel_iommu_domain_alloc(unsigned type) struct dmar_domain *dmar_domain; struct iommu_domain *domain; - if (type != IOMMU_DOMAIN_UNMANAGED) - return NULL; + switch (type) { + case IOMMU_DOMAIN_UNMANAGED: + 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); + return NULL; + } + domain_update_iommu_cap(dmar_domain); - 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); + 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; + 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 NULL; } static void intel_iommu_domain_free(struct iommu_domain *domain) { - domain_exit(to_dmar_domain(domain)); + if (domain != &si_domain->domain) + domain_exit(to_dmar_domain(domain)); } /* -- 2.17.1