Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp586198ybz; Wed, 15 Apr 2020 14:35:20 -0700 (PDT) X-Google-Smtp-Source: APiQypLhYo9enYbc1ph0MoVs4TGJrzq2l08W4isuSzlT5fiS4KJiW9FqD90kLIeCZmatydZUpHSe X-Received: by 2002:a50:a68a:: with SMTP id e10mr26857053edc.113.1586986520398; Wed, 15 Apr 2020 14:35:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586986520; cv=none; d=google.com; s=arc-20160816; b=h4urODqlPCmpms4JtNCEuLJvJQRrTiGXJYYBqbUhM4ZRkH5rmCxRKUiuHCeP2GtUFm DY3ivFL7Pe/6yIO2AfTq2bhaqJcuu22C/UOyJBl4x71QaFvQh/aS9WnqLMRt+npzIY3V b8SF23VixyGPtE2H+PCjg2Dljmir29gshzxdMv5zWGB/9hPAYjQxCloONVB3Pnupx4bP B3324Z7Y0G70Is5EJz5U9r6fbeXxbTns0a7YMEa2bKiJ2/rXOKE8esSoxTat9laSqTKl s3bI9KON6gA55lbhR5tpauGQXsGatGSFP2a6ct8/U+Q0b78AAMa3D6mpTKowT/KRzy5/ mxQQ== 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=aHjCLIiLIb1XI5CBYkQFJQjv78ooV6W3PztTzIwDCU8=; b=ghF1WgXAIQ12lU/sVqlzkeL3+WzBeWeHdLgi3Ujn6vGBYpW82UNHZ5s9IHbTULV3iy qQwGeyL2dod0ZnWJQYFlvY0vvl8bHw6Kr/sQXSBv/kGN/JlyALNOetIvrI2rVFNpVjrP geu52KyH3Sqtn52bCNx1hbc+zPTZxzZcKimvVbdeTrLnIYrA3iGWqI1TZwiEP7xGH+gb xEE1Dgp91OVVkKI5xth8CRdJ0/91HBg0AFWkwySvG45Gyy18zKzwDLrSE8l4TKPDNZQZ Oo/OxNCceudiN1uQQ5F62OoSZ83V4a7wselcnUczs30q4EgFtpwEp8xahtl/mgGUfG+Z XNlw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i13si13093719edt.174.2020.04.15.14.34.56; Wed, 15 Apr 2020 14:35:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387551AbgDNNbj (ORCPT + 99 others); Tue, 14 Apr 2020 09:31:39 -0400 Received: from 8bytes.org ([81.169.241.247]:34664 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730955AbgDNNP5 (ORCPT ); Tue, 14 Apr 2020 09:15:57 -0400 Received: by theia.8bytes.org (Postfix, from userid 1000) id 71CA32A5; Tue, 14 Apr 2020 15:15:51 +0200 (CEST) From: Joerg Roedel To: Joerg Roedel , Will Deacon , Robin Murphy , Marek Szyprowski , Kukjin Kim , Krzysztof Kozlowski , David Woodhouse , Lu Baolu , Andy Gross , Bjorn Andersson , Matthias Brugger , Rob Clark , Heiko Stuebner , Gerald Schaefer , Thierry Reding , Jonathan Hunter , Jean-Philippe Brucker Cc: iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, linux-samsung-soc@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mediatek@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-s390@vger.kernel.org, linux-tegra@vger.kernel.org, virtualization@lists.linux-foundation.org, Joerg Roedel Subject: [PATCH v2 01/33] iommu: Move default domain allocation to separate function Date: Tue, 14 Apr 2020 15:15:10 +0200 Message-Id: <20200414131542.25608-2-joro@8bytes.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200414131542.25608-1-joro@8bytes.org> References: <20200414131542.25608-1-joro@8bytes.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel Move the code out of iommu_group_get_for_dev() into a separate function. Signed-off-by: Joerg Roedel --- drivers/iommu/iommu.c | 74 ++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 29 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 2b471419e26c..bfe011760ed1 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1361,6 +1361,41 @@ struct iommu_group *fsl_mc_device_group(struct device *dev) } EXPORT_SYMBOL_GPL(fsl_mc_device_group); +static int iommu_alloc_default_domain(struct device *dev, + struct iommu_group *group) +{ + struct iommu_domain *dom; + + if (group->default_domain) + return 0; + + dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type); + if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) { + dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA); + if (dom) { + dev_warn(dev, + "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA", + iommu_def_domain_type); + } + } + + if (!dom) + return -ENOMEM; + + group->default_domain = dom; + if (!group->domain) + group->domain = dom; + + if (!iommu_dma_strict) { + int attr = 1; + iommu_domain_set_attr(dom, + DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE, + &attr); + } + + return 0; +} + /** * iommu_group_get_for_dev - Find or create the IOMMU group for a device * @dev: target device @@ -1393,40 +1428,21 @@ struct iommu_group *iommu_group_get_for_dev(struct device *dev) /* * Try to allocate a default domain - needs support from the - * IOMMU driver. + * IOMMU driver. There are still some drivers which don't support + * default domains, so the return value is not yet checked. */ - if (!group->default_domain) { - struct iommu_domain *dom; - - dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type); - if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) { - dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA); - if (dom) { - dev_warn(dev, - "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA", - iommu_def_domain_type); - } - } - - group->default_domain = dom; - if (!group->domain) - group->domain = dom; - - if (dom && !iommu_dma_strict) { - int attr = 1; - iommu_domain_set_attr(dom, - DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE, - &attr); - } - } + iommu_alloc_default_domain(dev, group); ret = iommu_group_add_device(group, dev); - if (ret) { - iommu_group_put(group); - return ERR_PTR(ret); - } + if (ret) + goto out_put_group; return group; + +out_put_group: + iommu_group_put(group); + + return ERR_PTR(ret); } EXPORT_SYMBOL(iommu_group_get_for_dev); -- 2.17.1