From patchwork Mon Feb 29 13:46:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 8454591 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 1277BC0553 for ; Mon, 29 Feb 2016 13:51:48 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id EBA1B20160 for ; Mon, 29 Feb 2016 13:51:46 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B30962014A for ; Mon, 29 Feb 2016 13:51:45 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1aaOCx-0002cI-Iw; Mon, 29 Feb 2016 13:49:55 +0000 Received: from foss.arm.com ([217.140.101.70]) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1aaOAd-0000kE-Qw for linux-arm-kernel@lists.infradead.org; Mon, 29 Feb 2016 13:47:33 +0000 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 811593A1; Mon, 29 Feb 2016 05:46:15 -0800 (PST) Received: from e104324-lin.cambridge.arm.com (e104324-lin.cambridge.arm.com [10.1.205.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id AABA23F213; Mon, 29 Feb 2016 05:47:08 -0800 (PST) From: Robin Murphy To: iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org Subject: [PATCH 09/12] iommu/arm-smmu: Simplify mmu-masters handling Date: Mon, 29 Feb 2016 13:46:18 +0000 Message-Id: <535593468d8489fa217513da4fa4ebcc6c8c57c6.1456514380.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.7.2.333.g70bd996.dirty In-Reply-To: References: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160229_054732_039119_1C66F6E0 X-CRM114-Status: GOOD ( 15.25 ) X-Spam-Score: -6.9 (------) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Thomas.Lendacky@amd.com, anup.patel@broadcom.com, thunder.leizhen@huawei.com, will.deacon@arm.com, stuart.yoder@nxp.com, Suravee.Suthikulpanit@amd.com, tchalamarla@caviumnetworks.com MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP With the preceding data reshuffles allowing SMMU instances to be looked up directly wherever needed, we can now wipe out the last vestiges of the global SMMU list. With mmu-masters lookup now a one-time thing outside of any real hotpath, condense our stash of those out of the SMMU instance data down to a single shared list, where they may await their eventual deprecation. This also seems like a good opportunity to move the parts only relevant to the probe/remove routines down to live in that area of the code. Signed-off-by: Robin Murphy --- drivers/iommu/arm-smmu.c | 231 +++++++++++++++++++---------------------------- 1 file changed, 91 insertions(+), 140 deletions(-) diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c index 5eaa8cf..91b0a1b 100644 --- a/drivers/iommu/arm-smmu.c +++ b/drivers/iommu/arm-smmu.c @@ -301,7 +301,7 @@ struct arm_smmu_group_cfg { struct arm_smmu_master { struct device_node *of_node; - struct rb_node node; + struct list_head list; struct arm_smmu_master_cfg cfg; }; @@ -372,8 +372,8 @@ struct arm_smmu_domain { static struct iommu_ops arm_smmu_ops; -static DEFINE_SPINLOCK(arm_smmu_devices_lock); -static LIST_HEAD(arm_smmu_devices); +static DEFINE_RWLOCK(arm_smmu_masters_lock); +static LIST_HEAD(arm_smmu_masters); struct arm_smmu_option_prop { u32 opt; @@ -417,111 +417,20 @@ static struct device_node *dev_get_dev_node(struct device *dev) return dev->of_node; } -static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu, - struct device_node *dev_node) +static struct arm_smmu_master *find_smmu_master(struct device_node *dev_node) { - struct rb_node *node = smmu->masters.rb_node; - - while (node) { - struct arm_smmu_master *master; - - master = container_of(node, struct arm_smmu_master, node); - - if (dev_node < master->of_node) - node = node->rb_left; - else if (dev_node > master->of_node) - node = node->rb_right; - else - return master; - } - - return NULL; -} - -static int insert_smmu_master(struct arm_smmu_device *smmu, - struct arm_smmu_master *master) -{ - struct rb_node **new, *parent; - - new = &smmu->masters.rb_node; - parent = NULL; - while (*new) { - struct arm_smmu_master *this - = container_of(*new, struct arm_smmu_master, node); - - parent = *new; - if (master->of_node < this->of_node) - new = &((*new)->rb_left); - else if (master->of_node > this->of_node) - new = &((*new)->rb_right); - else - return -EEXIST; - } - - rb_link_node(&master->node, parent, new); - rb_insert_color(&master->node, &smmu->masters); - return 0; -} - -static int register_smmu_master(struct arm_smmu_device *smmu, - struct of_phandle_args *masterspec) -{ - int i; struct arm_smmu_master *master; - master = find_smmu_master(smmu, masterspec->np); - if (master) { - dev_err(smmu->dev, - "rejecting multiple registrations for master device %s\n", - masterspec->np->name); - return -EBUSY; - } + read_lock(&arm_smmu_masters_lock); + list_for_each_entry(master, &arm_smmu_masters, list) + if (master->of_node == dev_node) + goto out_unlock; - if (masterspec->args_count > MAX_MASTER_STREAMIDS) { - dev_err(smmu->dev, - "reached maximum number (%d) of stream IDs for master device %s\n", - MAX_MASTER_STREAMIDS, masterspec->np->name); - return -ENOSPC; - } + master = NULL; - master = devm_kzalloc(smmu->dev, sizeof(*master), GFP_KERNEL); - if (!master) - return -ENOMEM; - - master->of_node = masterspec->np; - master->cfg.num_streamids = masterspec->args_count; - - for (i = 0; i < master->cfg.num_streamids; ++i) { - u16 streamid = masterspec->args[i]; - - if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && - (streamid >= smmu->num_mapping_groups)) { - dev_err(smmu->dev, - "stream ID for master device %s greater than maximum allowed (%d)\n", - masterspec->np->name, smmu->num_mapping_groups); - return -ERANGE; - } - master->cfg.streamids[i].id = streamid; - /* leave .mask 0; we don't currently share SMRs */ - } - return insert_smmu_master(smmu, master); -} - -static struct arm_smmu_device *find_smmu_for_device(struct device *dev) -{ - struct arm_smmu_device *smmu; - struct arm_smmu_master *master = NULL; - struct device_node *dev_node = dev_get_dev_node(dev); - - spin_lock(&arm_smmu_devices_lock); - list_for_each_entry(smmu, &arm_smmu_devices, list) { - master = find_smmu_master(smmu, dev_node); - if (master) - break; - } - spin_unlock(&arm_smmu_devices_lock); - - return master ? smmu : NULL; +out_unlock: + read_unlock(&arm_smmu_masters_lock); + return master; } static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end) @@ -1327,36 +1236,26 @@ static int arm_smmu_init_pci_device(struct arm_smmu_device *smmu, return 0; } -static int arm_smmu_init_platform_device(struct arm_smmu_device *smmu, - struct device *dev) -{ - struct arm_smmu_master *master; - - master = find_smmu_master(smmu, dev_get_dev_node(dev)); - dev->archdata.iommu = &master->cfg; - - return 0; -} - static int arm_smmu_add_device(struct device *dev) { struct iommu_group *group; - struct arm_smmu_device *smmu; - int ret; + struct arm_smmu_master *master; if (dev->archdata.iommu) return -EEXIST; - smmu = find_smmu_for_device(dev); - if (!smmu) + master = find_smmu_master(dev_get_dev_node(dev)); + if (!master) return -ENODEV; - if (dev_is_pci(dev)) - ret = arm_smmu_init_pci_device(smmu, to_pci_dev(dev)); - else - ret = arm_smmu_init_platform_device(smmu, dev); - if (ret) - return ret; + if (dev_is_pci(dev)) { + int ret = arm_smmu_init_pci_device(master->cfg.smmu, + to_pci_dev(dev)); + if (ret) + return ret; + } else { + dev->archdata.iommu = &master->cfg; + } group = iommu_group_get_for_dev(dev); if (IS_ERR(group)) @@ -1752,19 +1651,79 @@ MODULE_DEVICE_TABLE(of, arm_smmu_of_match); static void arm_smmu_remove_mmu_masters(struct arm_smmu_device *smmu) { - struct arm_smmu_master *master; - struct rb_node *node, *tmp; + struct arm_smmu_master *master, *tmp; - node = rb_first(&smmu->masters); - while (node) { - tmp = node; - node = rb_next(node); - master = container_of(tmp, struct arm_smmu_master, node); + write_lock(&arm_smmu_masters_lock); + list_for_each_entry_safe(master, tmp, &arm_smmu_masters, list) { + if (master->cfg.smmu != smmu) + continue; - rb_erase(tmp, &smmu->masters); + list_del(&master->list); of_node_put(master->of_node); devm_kfree(smmu->dev, master); } + write_unlock(&arm_smmu_masters_lock); +} + +static int insert_smmu_master(struct arm_smmu_master *master) +{ + struct arm_smmu_master *tmp; + int ret = -EEXIST; + + write_lock(&arm_smmu_masters_lock); + list_for_each_entry(tmp, &arm_smmu_masters, list) + if (tmp->of_node == master->of_node) + goto out_unlock; + + ret = 0; + list_add(&master->list, &arm_smmu_masters); +out_unlock: + write_unlock(&arm_smmu_masters_lock); + return ret; +} + +static int register_smmu_master(struct arm_smmu_device *smmu, + struct of_phandle_args *masterspec) +{ + int i; + struct arm_smmu_master *master; + + master = find_smmu_master(masterspec->np); + if (master) { + dev_err(smmu->dev, + "rejecting multiple registrations for master device %s\n", + masterspec->np->name); + return -EBUSY; + } + + if (masterspec->args_count > MAX_MASTER_STREAMIDS) { + dev_err(smmu->dev, + "reached maximum number (%d) of stream IDs for master device %s\n", + MAX_MASTER_STREAMIDS, masterspec->np->name); + return -ENOSPC; + } + + master = devm_kzalloc(smmu->dev, sizeof(*master), GFP_KERNEL); + if (!master) + return -ENOMEM; + + master->of_node = masterspec->np; + master->cfg.num_streamids = masterspec->args_count; + + for (i = 0; i < master->cfg.num_streamids; ++i) { + u16 streamid = masterspec->args[i]; + + if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && + (streamid >= smmu->num_mapping_groups)) { + dev_err(smmu->dev, + "stream ID for master device %s greater than maximum allowed (%d)\n", + masterspec->np->name, smmu->num_mapping_groups); + return -ERANGE; + } + master->cfg.streamids[i].id = streamid; + /* leave .mask 0; we don't currently share SMRs */ + } + return insert_smmu_master(master); } static int arm_smmu_probe_mmu_masters(struct arm_smmu_device *smmu) @@ -1880,11 +1839,6 @@ static int arm_smmu_device_dt_probe(struct platform_device *pdev) platform_set_drvdata(pdev, smmu); arm_smmu_probe_mmu_masters(smmu); - INIT_LIST_HEAD(&smmu->list); - spin_lock(&arm_smmu_devices_lock); - list_add(&smmu->list, &arm_smmu_devices); - spin_unlock(&arm_smmu_devices_lock); - arm_smmu_device_reset(smmu); return 0; @@ -1903,9 +1857,6 @@ static int arm_smmu_device_remove(struct platform_device *pdev) return -ENODEV; arm_smmu_remove_mmu_masters(smmu); - spin_lock(&arm_smmu_devices_lock); - list_del(&smmu->list); - spin_unlock(&arm_smmu_devices_lock); if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS)) dev_err(&pdev->dev, "removing device with active domains!\n");