Received: by 10.223.148.5 with SMTP id 5csp7397165wrq; Thu, 18 Jan 2018 05:04:29 -0800 (PST) X-Google-Smtp-Source: ACJfBosMOfsjg4xGD4NdjcnOlL5uy9aopb8R0+AewPZe/ou08cfKZ3vUR8wVX9RWtE7k52Ezpnlw X-Received: by 10.99.171.78 with SMTP id k14mr2179881pgp.287.1516280669196; Thu, 18 Jan 2018 05:04:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516280669; cv=none; d=google.com; s=arc-20160816; b=QTDnBo+q9+NNpYTdNeosXenhKHRKZ0Ne0UMicHuIAETmIrpV/dpkA5SNpcDLzD/Q5E 3FPpZo3jP9srd17sc5U7oPipwYncHm+rSrD/xd2EIVh4Wcj+4dcxpTzWjH8p7Veubb93 Higw7111FelU5I5ekwMW7p+0jPtod9U6DZTQxffXSwI49jcChRc24CfdoMYvqOGcdUJp vr+a/beDw1VpSRAbjKssqGXhhtQhiXp5aUPzLZ6Vm9S0LIICTEdBTiELULSAwBhNDOXT KiVZesTqDttyzgGCjabe8boueXYCLxz9WtVrOJG4tVhZ3AKqD/ZSWkioEyAbX/3K2oeE 04ZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:arc-authentication-results; bh=56DfWQ7loYMm30qJJ6OL4k7G05EfR/7GRh2hF1M7djs=; b=E/Lc7xpMixszMUT6/l8DQqyuKVVeUwj5CgB1ziOPCjuF5nfpwl4xIZZQYDhKDUBnc9 zTSmY9uBsf2ZHJodY0ORdzSXrRGmBbM4WzFeGjvj5J/z+s/99y5reaP42ibqu+IxYR12 QjjjtsNxEZpWlbz3yRTtkJCKER4UXj37osbgqgjSgRH6Bk2zXABF9Z3kgb9qRo7Z3g6V YMqJ82cbo+3DqlJOFL45HbrkQ1s6FHNAEZcjy8sfZ8wlSGQ0VMQTSByi9lkm3ZeT3BGD br4i2O/sDFVdeuPu/tV6NMsEyL3kLM+KVmHAiifgEBsVa+mZE1w6lluNwydjPRdTKIv2 F2YQ== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b13si6729511pfd.85.2018.01.18.05.04.15; Thu, 18 Jan 2018 05:04:29 -0800 (PST) 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755625AbeARMqe (ORCPT + 99 others); Thu, 18 Jan 2018 07:46:34 -0500 Received: from foss.arm.com ([217.140.101.70]:54726 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753975AbeARMqd (ORCPT ); Thu, 18 Jan 2018 07:46:33 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 2AE241435; Thu, 18 Jan 2018 04:46:33 -0800 (PST) Received: from [10.1.210.88] (e110467-lin.cambridge.arm.com [10.1.210.88]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 797403F557; Thu, 18 Jan 2018 04:46:31 -0800 (PST) Subject: Re: [PATCH v4 10/13] iommu/rockchip: Use OF_IOMMU to attach devices automatically To: Jeffy Chen , linux-kernel@vger.kernel.org Cc: jcliang@chromium.org, xxm@rock-chips.com, tfiga@chromium.org, Heiko Stuebner , linux-rockchip@lists.infradead.org, iommu@lists.linux-foundation.org, Joerg Roedel , linux-arm-kernel@lists.infradead.org References: <20180118115251.5542-1-jeffy.chen@rock-chips.com> <20180118115251.5542-11-jeffy.chen@rock-chips.com> From: Robin Murphy Message-ID: <45265d6e-6b1a-57b8-11c4-6f1973702715@arm.com> Date: Thu, 18 Jan 2018 12:46:30 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: <20180118115251.5542-11-jeffy.chen@rock-chips.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 18/01/18 11:52, Jeffy Chen wrote: > Converts the rockchip-iommu driver to use the OF_IOMMU infrastructure, > which allows attaching master devices to their IOMMUs automatically > according to DT properties. > > Signed-off-by: Jeffy Chen > --- > > Changes in v4: None > Changes in v3: > Add struct rk_iommudata. > Squash iommu/rockchip: Use iommu_group_get_for_dev() for add_device > > Changes in v2: None > > drivers/iommu/rockchip-iommu.c | 139 +++++++++++++---------------------------- > 1 file changed, 44 insertions(+), 95 deletions(-) > > diff --git a/drivers/iommu/rockchip-iommu.c b/drivers/iommu/rockchip-iommu.c > index bdb7c5de6fc2..b1f177ae03c7 100644 > --- a/drivers/iommu/rockchip-iommu.c > +++ b/drivers/iommu/rockchip-iommu.c > @@ -19,6 +19,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -97,6 +98,10 @@ struct rk_iommu { > struct iommu_domain *domain; /* domain to which iommu is attached */ > }; > > +struct rk_iommudata { > + struct rk_iommu *iommu; > +}; > + > static struct device *dma_dev; > > static inline void rk_table_flush(struct rk_iommu_domain *dom, dma_addr_t dma, > @@ -872,18 +877,9 @@ static size_t rk_iommu_unmap(struct iommu_domain *domain, unsigned long _iova, > > static struct rk_iommu *rk_iommu_from_dev(struct device *dev) > { > - struct iommu_group *group; > - struct device *iommu_dev; > - struct rk_iommu *rk_iommu; > + struct rk_iommudata *data = dev->archdata.iommu; > > - group = iommu_group_get(dev); > - if (!group) > - return NULL; > - iommu_dev = iommu_group_get_iommudata(group); > - rk_iommu = dev_get_drvdata(iommu_dev); > - iommu_group_put(group); > - > - return rk_iommu; > + return data ? data->iommu : NULL; > } > > static int rk_iommu_attach_device(struct iommu_domain *domain, > @@ -1060,110 +1056,57 @@ static void rk_iommu_domain_free(struct iommu_domain *domain) > iommu_put_dma_cookie(&rk_domain->domain); > } > > -static bool rk_iommu_is_dev_iommu_master(struct device *dev) > -{ > - struct device_node *np = dev->of_node; > - int ret; > - > - /* > - * An iommu master has an iommus property containing a list of phandles > - * to iommu nodes, each with an #iommu-cells property with value 0. > - */ > - ret = of_count_phandle_with_args(np, "iommus", "#iommu-cells"); > - return (ret > 0); > -} > - > -static int rk_iommu_group_set_iommudata(struct iommu_group *group, > - struct device *dev) > +static int rk_iommu_add_device(struct device *dev) > { > - struct device_node *np = dev->of_node; > - struct platform_device *pd; > - int ret; > - struct of_phandle_args args; > + struct iommu_group *group; > + struct rk_iommu *iommu; > > - /* > - * An iommu master has an iommus property containing a list of phandles > - * to iommu nodes, each with an #iommu-cells property with value 0. > - */ > - ret = of_parse_phandle_with_args(np, "iommus", "#iommu-cells", 0, > - &args); > - if (ret) { > - dev_err(dev, "of_parse_phandle_with_args(%pOF) => %d\n", > - np, ret); > - return ret; > - } > - if (args.args_count != 0) { > - dev_err(dev, "incorrect number of iommu params found for %pOF (found %d, expected 0)\n", > - args.np, args.args_count); > - return -EINVAL; > - } > + iommu = rk_iommu_from_dev(dev); > + if (!iommu) > + return -ENODEV; > > - pd = of_find_device_by_node(args.np); > - of_node_put(args.np); > - if (!pd) { > - dev_err(dev, "iommu %pOF not found\n", args.np); > - return -EPROBE_DEFER; > - } > + group = iommu_group_get_for_dev(dev); > + if (IS_ERR(group)) > + return PTR_ERR(group); > + iommu_group_put(group); > > - /* TODO(djkurtz): handle multiple slave iommus for a single master */ > - iommu_group_set_iommudata(group, &pd->dev, NULL); > + iommu_device_link(&iommu->iommu, dev); > > return 0; > } > > -static int rk_iommu_add_device(struct device *dev) > +static void rk_iommu_remove_device(struct device *dev) > { > - struct iommu_group *group; > struct rk_iommu *iommu; > - int ret; > - > - if (!rk_iommu_is_dev_iommu_master(dev)) > - return -ENODEV; > - > - group = iommu_group_get(dev); > - if (!group) { > - group = iommu_group_alloc(); > - if (IS_ERR(group)) { > - dev_err(dev, "Failed to allocate IOMMU group\n"); > - return PTR_ERR(group); > - } > - } > - > - ret = iommu_group_add_device(group, dev); > - if (ret) > - goto err_put_group; > - > - ret = rk_iommu_group_set_iommudata(group, dev); > - if (ret) > - goto err_remove_device; > > iommu = rk_iommu_from_dev(dev); > - if (iommu) > - iommu_device_link(&iommu->iommu, dev); > - > - iommu_group_put(group); > - > - return 0; > > -err_remove_device: > + iommu_device_unlink(&iommu->iommu, dev); > iommu_group_remove_device(dev); > -err_put_group: > - iommu_group_put(group); > - return ret; > } > > -static void rk_iommu_remove_device(struct device *dev) > +static int rk_iommu_of_xlate(struct device *dev, > + struct of_phandle_args *args) > { > - struct rk_iommu *iommu; > + struct platform_device *iommu_dev; > + struct rk_iommudata *data; > > - if (!rk_iommu_is_dev_iommu_master(dev)) > - return; > + data = devm_kzalloc(dma_dev, sizeof(*data), GFP_KERNEL); > + if (!data) > + return -ENOMEM; > > - iommu = rk_iommu_from_dev(dev); > - if (iommu) > - iommu_device_unlink(&iommu->iommu, dev); > + iommu_dev = of_find_device_by_node(args->np); > + if (!iommu_dev) { > + dev_err(dev, "iommu %pOF not found\n", args->np); > + return -ENODEV; Nit: I think this could only happen if the IOMMU device managed to probe successfully enough to call iommu_device_register(), but then somehow disappeared from the platform bus - given patch #1, that now seems impossible enough that it's probably not worth checking for. Either way, this looks pretty tidy now; Reviewed-by: Robin Murphy > + } > > - iommu_group_remove_device(dev); > + data->iommu = platform_get_drvdata(iommu_dev); > + dev->archdata.iommu = data; > + > + of_dev_put(iommu_dev); > + > + return 0; > } > > static const struct iommu_ops rk_iommu_ops = { > @@ -1177,7 +1120,9 @@ static const struct iommu_ops rk_iommu_ops = { > .add_device = rk_iommu_add_device, > .remove_device = rk_iommu_remove_device, > .iova_to_phys = rk_iommu_iova_to_phys, > + .device_group = generic_device_group, > .pgsize_bitmap = RK_IOMMU_PGSIZE_BITMAP, > + .of_xlate = rk_iommu_of_xlate, > }; > > static int rk_iommu_probe(struct platform_device *pdev) > @@ -1236,6 +1181,8 @@ static int rk_iommu_probe(struct platform_device *pdev) > goto err_put_clocks; > > iommu_device_set_ops(&iommu->iommu, &rk_iommu_ops); > + iommu_device_set_fwnode(&iommu->iommu, &dev->of_node->fwnode); > + > err = iommu_device_register(&iommu->iommu); > if (err) > goto err_remove_sysfs; > @@ -1290,6 +1237,8 @@ static int __init rk_iommu_init(void) > } > subsys_initcall(rk_iommu_init); > > +IOMMU_OF_DECLARE(rk_iommu_of, "rockchip,iommu", NULL); > + > MODULE_DESCRIPTION("IOMMU API for Rockchip"); > MODULE_AUTHOR("Simon Xue and Daniel Kurtz "); > MODULE_ALIAS("platform:rockchip-iommu"); >