Received: by 10.213.65.68 with SMTP id h4csp1079576imn; Sun, 25 Mar 2018 23:32:36 -0700 (PDT) X-Google-Smtp-Source: AG47ELsO53rCRzYI4rjMfoYXEhRPyEVA3N/IcOCNuGmBLRUKp9QSrdOsCPZikPKB1UIuStokKnkC X-Received: by 2002:a17:902:5910:: with SMTP id o16-v6mr22903938pli.258.1522045955978; Sun, 25 Mar 2018 23:32:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522045955; cv=none; d=google.com; s=arc-20160816; b=X52O4vZERxVQY3p7WuOUxC/h9nWJ0Up4kPGEL1xnGagrEUYPRE7DX3s1kmCSiC9D6E DUKPgZEueufavkhJhRJN1GG4qvNkcdIyNXTY6RFP0IPAGksd3JDOWg0EvC8OOKyw78PU mVvWGqv66u+5IENLCNDZiIZDz5UTOCzKBEmkwyQ7BHe7N6+kDfzX5h7TOczGOorJM9E/ cqMtP0CZyfwLvMoCFDJlR2fftRLrbpkHHEp7aIW/qZqc8zI8BLowYVvUkImezrdr4wJM EvHSkfxravFkkDC3fwE0gMI+pI4KcQKkDComti6x+Id7YcVeMjSHs13Prp8JFbG54Irv PDjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature :arc-authentication-results; bh=KYfny7yAj1dKvl2sbxnmBiaGV+nkt0Ms5cZgUDYBEbc=; b=do6De5JPLK60bpMPHYFycw///m9fyPz/n5JrpVugLqg1No097y3suKePeQOpbO4SSQ Po8RA6ZOwQHLVc2FBI1kzE2RpbF1rmEWcf8mxbhOeDEPVL4YrZfbZ7ULV90lYHYhnGoP wTi/voSV//vA68foRlZgIL8r+LLC5sV5KHGSSe52XIGNmIsYOLY4oW+KNpOd5YKut+sk cxG0cLhWQGeQ+UPr2cp+S4i9M+Kagf9vq1ZVMs63dfZ4F24hG/PxWIvkgn4yjoSU/qLu v/ZK4goGuAIaqmVHm59aT+VLsVdymMKgw/RL6v2hQXReaJOaSiC6M8BOXrgdgQkX+9P/ 1WhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=KDqi2fMI; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j62si9645348pge.747.2018.03.25.23.32.19; Sun, 25 Mar 2018 23:32:35 -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; dkim=pass header.i=@chromium.org header.s=google header.b=KDqi2fMI; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751449AbeCZGb1 (ORCPT + 99 others); Mon, 26 Mar 2018 02:31:27 -0400 Received: from mail-ua0-f194.google.com ([209.85.217.194]:37598 "EHLO mail-ua0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751116AbeCZGbY (ORCPT ); Mon, 26 Mar 2018 02:31:24 -0400 Received: by mail-ua0-f194.google.com with SMTP id q12so11388809uae.4 for ; Sun, 25 Mar 2018 23:31:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=KYfny7yAj1dKvl2sbxnmBiaGV+nkt0Ms5cZgUDYBEbc=; b=KDqi2fMIXfJ7qc+dJyiphOG1aqiUvdBUrR1f3N+4TfoaQUg9ILwirlcB/3bpbEQby0 9RR8RtXtBwnADAxV7gdhGlzuFRuaXgP71/8Hr/O3QxaIiGImrbeenLWhJgJabB+g1ODO 2hVrqjKDMo+QIdlNElgTR1JTLTA0TK04jWLSU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=KYfny7yAj1dKvl2sbxnmBiaGV+nkt0Ms5cZgUDYBEbc=; b=I7qyi7+HSsRoHKPN1glrK0EHnYTKtdyFL1Z4+aRhwlLLkXNN4EFjpIhJYH3qcbd6JC h1VbNVL9J2Qvm+T0zRN7qQQCki8lPRz64+Ec30x4Y35X2ikxikJn2Tf0FdwFA4lN6WdP biSlsDy9GJpZ2XeAm3uS6ODSxLpI9GOJ9khfDeLMO4iutzY9D5MHRjxeS+nA/iAXh6Kf R+d06aNm6PvM/O5186vysjwCXgJPfQYyInVnUu/kb9mUBQl7RczmBsvpjqNAZ98ANyhG I1ql2sw/izk+Ei/q20FczwULwE/TIlSa6pcMW5eoRY8fF7SHiMmzoWy5QkacStADu/zV Mp3Q== X-Gm-Message-State: AElRT7HYmmvDXKAGiJJLG6qzPaWod3W7QPL7G+IFgpF6a307a/xsoghx tmtEx8bDTp0tIk8z0OFbjtaB+AL47Z0L6uld4Y6cNQ== X-Received: by 10.176.19.115 with SMTP id h48mr15560840uae.135.1522045883189; Sun, 25 Mar 2018 23:31:23 -0700 (PDT) MIME-Version: 1.0 References: <20180323073814.5802-1-jeffy.chen@rock-chips.com> <20180323073814.5802-12-jeffy.chen@rock-chips.com> In-Reply-To: <20180323073814.5802-12-jeffy.chen@rock-chips.com> From: Daniel Kurtz Date: Mon, 26 Mar 2018 06:31:12 +0000 Message-ID: Subject: Re: [PATCH v8 11/14] iommu/rockchip: Use OF_IOMMU to attach devices automatically To: Jeffy Cc: linux-kernel@vger.kernel.org, Ricky Liang , Robin Murphy , xxm , Tomasz Figa , Heiko Stuebner , "open list:ARM/Rockchip SoC..." , "open list:IOMMU DRIVERS" , Joerg Roedel , linux-arm-kernel@lists.infradead.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 23, 2018 at 1:40 AM 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 > Reviewed-by: Robin Murphy > --- > Changes in v8: None > Changes in v7: None > Changes in v6: None > Changes in v5: None > 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 | 135 ++++++++++++----------------------------- > 1 file changed, 40 insertions(+), 95 deletions(-) > diff --git a/drivers/iommu/rockchip-iommu.c b/drivers/iommu/rockchip-iommu.c > index 7970d21b9858..bd8580b897e9 100644 > --- a/drivers/iommu/rockchip-iommu.c > +++ b/drivers/iommu/rockchip-iommu.c > @@ -19,6 +19,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -104,6 +105,10 @@ struct rk_iommu { > struct iommu_domain *domain; /* domain to which iommu is attached */ > }; > +struct rk_iommudata { > + struct rk_iommu *iommu; > +}; Why do we need this struct? Can't we just assign a pointer to struct rk_iommu directly to dev->archdata.iommu? > + > static struct device *dma_dev; > static inline void rk_table_flush(struct rk_iommu_domain *dom, dma_addr_t dma, > @@ -807,18 +812,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, > @@ -989,110 +985,53 @@ 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); > - 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 = { > @@ -1106,7 +1045,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) > @@ -1178,6 +1119,8 @@ static int rk_iommu_probe(struct platform_device *pdev) > goto err_unprepare_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; > @@ -1250,6 +1193,8 @@ static int __init rk_iommu_init(void) > } > subsys_initcall(rk_iommu_init); > +IOMMU_OF_DECLARE(rk_iommu_of, "rockchip,iommu"); > + > MODULE_DESCRIPTION("IOMMU API for Rockchip"); > MODULE_AUTHOR("Simon Xue and Daniel Kurtz < djkurtz@chromium.org>"); > MODULE_ALIAS("platform:rockchip-iommu"); > -- > 2.11.0