Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1318227ybi; Fri, 24 May 2019 22:50:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqwAtO1f5izA3WT6OL2Xb6XRfrNl6ycQpYrmoTmWhKU/NVWHkuVO9Kii2yhiS6BKZGLH18RB X-Received: by 2002:a17:902:778d:: with SMTP id o13mr51630239pll.275.1558763407957; Fri, 24 May 2019 22:50:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558763407; cv=none; d=google.com; s=arc-20160816; b=n/Jre7VMc79+RTVn9JA1Hz5AbqBKLoWBBxMf8e/kXyX8+tzQp1EDrkvxxnZcJUnLr5 o2ejouB3YKrOejvA0uhh6WPhLJeJWzUvgGbydZaKu95FXeDKhuE5rtozgWfL9kmCgBOr CrHzL3jjcck/d7qSTjHamwJnpoDdclfs2tZ2gczbgmrK8zk60ai4dOKU3nlSLIKcXyjL rwoCWuI8nMKmmVN47ZQ8WytsW+oP9fUUVbSPayhbmF005JdlrT7s/AKGdBbpacZpd87/ IFBeblkGHxmmZnAnXISayD9VYn/fIT2FqVQWy0/8BOSGSVXkqjBdG1vvOB665ygCMsdy blNQ== 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=9qVPtvYUGhXwGe5UjBD1dzrlMpwamlCFJ9VEucyl3Jg=; b=Lt4C5OckX7LR6tXe0pBv8uK+jfS+Ts7FgVSGgjsRqEatgnOj1E9t98oOhqC/FIgvI0 R5KFsYWHEGEnjLkeZ8R80374sE7YzzkChgIG9nHAwDw6Jh3rH4D2HKssrVXebnR2twIK k/9khg5+D9AzP7/QZBq+gIr0eEycMCkh2OLRj2PE0B8S8tMUxvkfOd6bWr5rQm6uLV6V Au5DEoViJDmDFkVPAbxZvba8zRyE2qUHWs2U/cy6ZltKvk/71Y46IbpqdddRtmyS239l omjpF1ATk8p8NnxFkVa45SVeO191/60COLDlQ+FSNZ4boJYYuzSWPi6Sx7L9Mvvgp9u/ dGlw== 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 r71si7589324pfa.183.2019.05.24.22.49.53; Fri, 24 May 2019 22:50:07 -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 S1726642AbfEYFsu (ORCPT + 99 others); Sat, 25 May 2019 01:48:50 -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 S1726160AbfEYFst (ORCPT ); Sat, 25 May 2019 01:48:49 -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:49 -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:47 -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 01/15] iommu: Add API to request DMA domain for device Date: Sat, 25 May 2019 13:41:22 +0800 Message-Id: <20190525054136.27810-2-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 Normally during iommu probing a device, a default doamin will be allocated and attached to the device. The domain type of the default domain is statically defined, which results in a situation where the allocated default domain isn't suitable for the device due to some limitations. We already have API iommu_request_dm_for_dev() to replace a DMA domain with an identity one. This adds iommu_request_dma_domain_for_dev() to request a dma domain if an allocated identity domain isn't suitable for the device in question. Signed-off-by: Lu Baolu --- drivers/iommu/iommu.c | 36 +++++++++++++++++++++++++----------- include/linux/iommu.h | 6 ++++++ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index c09d60401778..03ba8406d1e4 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1910,10 +1910,10 @@ struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start, return region; } -/* Request that a device is direct mapped by the IOMMU */ -int iommu_request_dm_for_dev(struct device *dev) +static int +request_default_domain_for_dev(struct device *dev, unsigned long type) { - struct iommu_domain *dm_domain; + struct iommu_domain *domain; struct iommu_group *group; int ret; @@ -1926,8 +1926,7 @@ int iommu_request_dm_for_dev(struct device *dev) /* Check if the default domain is already direct mapped */ ret = 0; - if (group->default_domain && - group->default_domain->type == IOMMU_DOMAIN_IDENTITY) + if (group->default_domain && group->default_domain->type == type) goto out; /* Don't change mappings of existing devices */ @@ -1937,23 +1936,26 @@ int iommu_request_dm_for_dev(struct device *dev) /* Allocate a direct mapped domain */ ret = -ENOMEM; - dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY); - if (!dm_domain) + domain = __iommu_domain_alloc(dev->bus, type); + if (!domain) goto out; /* Attach the device to the domain */ - ret = __iommu_attach_group(dm_domain, group); + ret = __iommu_attach_group(domain, group); if (ret) { - iommu_domain_free(dm_domain); + iommu_domain_free(domain); goto out; } + iommu_group_create_direct_mappings(group, dev); + /* Make the direct mapped domain the default for this group */ if (group->default_domain) iommu_domain_free(group->default_domain); - group->default_domain = dm_domain; + group->default_domain = domain; - dev_info(dev, "Using iommu direct mapping\n"); + dev_info(dev, "Using iommu %s mapping\n", + type == IOMMU_DOMAIN_DMA ? "dma" : "direct"); ret = 0; out: @@ -1963,6 +1965,18 @@ int iommu_request_dm_for_dev(struct device *dev) return ret; } +/* Request that a device is direct mapped by the IOMMU */ +int iommu_request_dm_for_dev(struct device *dev) +{ + return request_default_domain_for_dev(dev, IOMMU_DOMAIN_IDENTITY); +} + +/* Request that a device can't be direct mapped by the IOMMU */ +int iommu_request_dma_domain_for_dev(struct device *dev) +{ + return request_default_domain_for_dev(dev, IOMMU_DOMAIN_DMA); +} + const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode) { const struct iommu_ops *ops = NULL; diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 14a521f85f14..593a3411d860 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -368,6 +368,7 @@ extern void iommu_set_fault_handler(struct iommu_domain *domain, extern void iommu_get_resv_regions(struct device *dev, struct list_head *list); extern void iommu_put_resv_regions(struct device *dev, struct list_head *list); extern int iommu_request_dm_for_dev(struct device *dev); +extern int iommu_request_dma_domain_for_dev(struct device *dev); extern struct iommu_resv_region * iommu_alloc_resv_region(phys_addr_t start, size_t length, int prot, enum iommu_resv_type type, gfp_t flags); @@ -632,6 +633,11 @@ static inline int iommu_request_dm_for_dev(struct device *dev) return -ENODEV; } +static inline int iommu_request_dma_domain_for_dev(struct device *dev) +{ + return -ENODEV; +} + static inline int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) { -- 2.17.1