From patchwork Fri May 11 19:06:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 10395033 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 35D0060348 for ; Fri, 11 May 2018 19:10:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2533528F8E for ; Fri, 11 May 2018 19:10:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1924628FA9; Fri, 11 May 2018 19:10:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 656C228F8E for ; Fri, 11 May 2018 19:10:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A3E1E6B06BE; Fri, 11 May 2018 15:10:30 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 9C8516B06C0; Fri, 11 May 2018 15:10:30 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 88C536B06C1; Fri, 11 May 2018 15:10:30 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ot0-f197.google.com (mail-ot0-f197.google.com [74.125.82.197]) by kanga.kvack.org (Postfix) with ESMTP id 618BA6B06BE for ; Fri, 11 May 2018 15:10:30 -0400 (EDT) Received: by mail-ot0-f197.google.com with SMTP id y49-v6so4300003oti.11 for ; Fri, 11 May 2018 12:10:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=A41zte70bkurMaF01VRsS4HWK7dV2ts3hxZSwyJ7kR0=; b=L12CW22E1oSHGJCfkXBzRR65+T/NSL5bGRumReAz+7qL4NAXqWOwQ4mXRloCSXEcPG 3jyaKMeaPFwpNHmGZ/k0GEE3pqbMsimg/+G2WVkCBZk+i6d/ERXmuTkw9Y9ukdgLr2/Z FcExb5Gqvh7PjNYcNjLNVWV0rNY+ePicmxsxx1ZEOUlgfiT2jjoNFG3ouUlPiKXcztBS UX2NTnfXPg/AEKi2p9yx5aK5Tg2vMpqDpR56n1ybORL79iJ8W7U2S6/nvfJ/nspO2Ipk AZblKZ33XKyghVkC6veZD9uUW6/cT5J0MSbTaRHYKBq7pmbIbIrJPcYskt2n7pS/yVs5 Zl8w== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of jean-philippe.brucker@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=jean-philippe.brucker@arm.com X-Gm-Message-State: ALKqPwe747u0veKsxT6+56Bduhqudvlqc2zOq12Np/RjF0/MB0AIC2YQ 0h3+8FbdrQjMAZ7prETtSMKa2DIswSF4Of8zrnpriqrl7i4TZ6cCG2yVpRyA6T3DBTZ3qJxa5YX QrJ3vcXolhPqnqpubs5I8fH95sQK+ac4NQQMGaDj52FK5T/eFAEQqLutdf2zkL1yoJQ== X-Received: by 2002:a9d:2083:: with SMTP id x3-v6mr4950753ota.338.1526065830197; Fri, 11 May 2018 12:10:30 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqTBtizSqzbeL3xfF14R9LZRhxaOOWgjGeZGt2ZvICa1d5nmFayN0toz2QERFCRQW3njJ/i X-Received: by 2002:a9d:2083:: with SMTP id x3-v6mr4950705ota.338.1526065829479; Fri, 11 May 2018 12:10:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526065829; cv=none; d=google.com; s=arc-20160816; b=rIQYhmtmfSw+Nv24DoIsdvlExok9YKQg0dn7mLiZpwt8J3Vq7r8b5vam5g7U1VBa7n 2sjvfJbGxp9M8098gYQ3kajB9iRQ99JpPIWuefQZuexHJFFhbzskWTsDNybqhXGGkEZX LlpyhhiuFH5KQshjLyW7EO5b9cb/3GXEZiLHA6o7kYAyxKlXVBnnEDaEVz9+dyWxq30N wsTdOtqIE+nJx9ySr4aj446dzfRRhM3B6xKEPIStqH+tFa877TTjEEJ1nWWOFaBu3DK0 iq1TYFXqcXLkQjeJNeh0e/CSoCEaa9SUqRoDOKPisYW2zLSwipWJOg6ZUoWx6peYQvxu oL8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=A41zte70bkurMaF01VRsS4HWK7dV2ts3hxZSwyJ7kR0=; b=Gt5JVCZ254zRTRLzSeB16QUZM7hfztYOgSZ3OEoQ9WEIsnGY2kgjrhtYZKpdNCkxcI iU5JUEjKgbl+N/QFyiwl4vZDgdk35Vl4y0BcmeWHgCpb0ty6Ta9I5ROuCBYU2IXeIHzI 9w5Jj2+7UlTE1to9Z7Zkj06EBJ/B4sbwpGB/NCnWnCe2dfhuQlPfICMC8RwdpCXU4YoZ n6GJgm82idtbk3KTA1R9SR/s5QdwZPCuLo54Z5NtXD/B2YhwMRbrfhPRB5yCAgqvmE+s zBSUSo8PuxlVTUlnGPAalVFH81Gi12AQdXhZ7JugRbCILylibaae728/+bhPnbLVZ287 DPTA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of jean-philippe.brucker@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=jean-philippe.brucker@arm.com Received: from foss.arm.com (foss.arm.com. [217.140.101.70]) by mx.google.com with ESMTP id x35-v6si1403539oti.95.2018.05.11.12.10.29 for ; Fri, 11 May 2018 12:10:29 -0700 (PDT) Received-SPF: pass (google.com: domain of jean-philippe.brucker@arm.com designates 217.140.101.70 as permitted sender) client-ip=217.140.101.70; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jean-philippe.brucker@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=jean-philippe.brucker@arm.com 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 1AAA91713; Fri, 11 May 2018 12:10:29 -0700 (PDT) Received: from ostrya.cambridge.arm.com (ostrya.cambridge.arm.com [10.1.210.33]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id D19943F23C; Fri, 11 May 2018 12:10:23 -0700 (PDT) From: Jean-Philippe Brucker To: linux-arm-kernel@lists.infradead.org, linux-pci@vger.kernel.org, linux-acpi@vger.kernel.org, devicetree@vger.kernel.org, iommu@lists.linux-foundation.org, kvm@vger.kernel.org, linux-mm@kvack.org Cc: joro@8bytes.org, will.deacon@arm.com, robin.murphy@arm.com, alex.williamson@redhat.com, tn@semihalf.com, liubo95@huawei.com, thunder.leizhen@huawei.com, xieyisheng1@huawei.com, xuzaibo@huawei.com, ilias.apalodimas@linaro.org, jonathan.cameron@huawei.com, liudongdong3@huawei.com, shunyong.yang@hxt-semitech.com, nwatters@codeaurora.org, okaya@codeaurora.org, jcrouse@codeaurora.org, rfranz@cavium.com, dwmw2@infradead.org, jacob.jun.pan@linux.intel.com, yi.l.liu@intel.com, ashok.raj@intel.com, kevin.tian@intel.com, baolu.lu@linux.intel.com, robdclark@gmail.com, christian.koenig@amd.com, bharatku@xilinx.com, rgummal@xilinx.com Subject: [PATCH v2 32/40] iommu/arm-smmu-v3: Maintain a SID->device structure Date: Fri, 11 May 2018 20:06:33 +0100 Message-Id: <20180511190641.23008-33-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180511190641.23008-1-jean-philippe.brucker@arm.com> References: <20180511190641.23008-1-jean-philippe.brucker@arm.com> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP When handling faults from the event or PRI queue, we need to find the struct device associated to a SID. Add a rb_tree to keep track of SIDs. Signed-off-by: Jean-Philippe Brucker --- drivers/iommu/arm-smmu-v3.c | 114 +++++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c index d5f3875abfb9..0f2d8aa0deee 100644 --- a/drivers/iommu/arm-smmu-v3.c +++ b/drivers/iommu/arm-smmu-v3.c @@ -570,9 +570,18 @@ struct arm_smmu_device { /* IOMMU core code handle */ struct iommu_device iommu; + struct rb_root streams; + struct mutex streams_mutex; + struct iopf_queue *iopf_queue; }; +struct arm_smmu_stream { + u32 id; + struct arm_smmu_master_data *master; + struct rb_node node; +}; + /* SMMU private data for each master */ struct arm_smmu_master_data { struct arm_smmu_device *smmu; @@ -580,6 +589,7 @@ struct arm_smmu_master_data { struct arm_smmu_domain *domain; struct list_head list; /* domain->devices */ + struct arm_smmu_stream *streams; struct device *dev; size_t ssid_bits; @@ -1186,6 +1196,32 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid) return 0; } +__maybe_unused +static struct arm_smmu_master_data * +arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid) +{ + struct rb_node *node; + struct arm_smmu_stream *stream; + struct arm_smmu_master_data *master = NULL; + + mutex_lock(&smmu->streams_mutex); + node = smmu->streams.rb_node; + while (node) { + stream = rb_entry(node, struct arm_smmu_stream, node); + if (stream->id < sid) { + node = node->rb_right; + } else if (stream->id > sid) { + node = node->rb_left; + } else { + master = stream->master; + break; + } + } + mutex_unlock(&smmu->streams_mutex); + + return master; +} + /* IRQ and event handlers */ static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev) { @@ -2086,6 +2122,71 @@ static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid) return sid < limit; } +static int arm_smmu_insert_master(struct arm_smmu_device *smmu, + struct arm_smmu_master_data *master) +{ + int i; + int ret = 0; + struct arm_smmu_stream *new_stream, *cur_stream; + struct rb_node **new_node, *parent_node = NULL; + struct iommu_fwspec *fwspec = master->dev->iommu_fwspec; + + master->streams = kcalloc(fwspec->num_ids, + sizeof(struct arm_smmu_stream), GFP_KERNEL); + if (!master->streams) + return -ENOMEM; + + mutex_lock(&smmu->streams_mutex); + for (i = 0; i < fwspec->num_ids && !ret; i++) { + new_stream = &master->streams[i]; + new_stream->id = fwspec->ids[i]; + new_stream->master = master; + + new_node = &(smmu->streams.rb_node); + while (*new_node) { + cur_stream = rb_entry(*new_node, struct arm_smmu_stream, + node); + parent_node = *new_node; + if (cur_stream->id > new_stream->id) { + new_node = &((*new_node)->rb_left); + } else if (cur_stream->id < new_stream->id) { + new_node = &((*new_node)->rb_right); + } else { + dev_warn(master->dev, + "stream %u already in tree\n", + cur_stream->id); + ret = -EINVAL; + break; + } + } + + if (!ret) { + rb_link_node(&new_stream->node, parent_node, new_node); + rb_insert_color(&new_stream->node, &smmu->streams); + } + } + mutex_unlock(&smmu->streams_mutex); + + return ret; +} + +static void arm_smmu_remove_master(struct arm_smmu_device *smmu, + struct arm_smmu_master_data *master) +{ + int i; + struct iommu_fwspec *fwspec = master->dev->iommu_fwspec; + + if (!master->streams) + return; + + mutex_lock(&smmu->streams_mutex); + for (i = 0; i < fwspec->num_ids; i++) + rb_erase(&master->streams[i].node, &smmu->streams); + mutex_unlock(&smmu->streams_mutex); + + kfree(master->streams); +} + static struct iommu_ops arm_smmu_ops; static int arm_smmu_add_device(struct device *dev) @@ -2142,16 +2243,23 @@ static int arm_smmu_add_device(struct device *dev) if (ret) goto err_free_master; + ret = arm_smmu_insert_master(smmu, master); + if (ret) + goto err_unlink; + group = iommu_group_get_for_dev(dev); if (IS_ERR(group)) { ret = PTR_ERR(group); - goto err_unlink; + goto err_remove_master; } iommu_group_put(group); return 0; +err_remove_master: + arm_smmu_remove_master(smmu, master); + err_unlink: iommu_device_unlink(&smmu->iommu, dev); @@ -2180,6 +2288,7 @@ static void arm_smmu_remove_device(struct device *dev) if (master->ste.assigned) arm_smmu_detach_dev(dev); iommu_group_remove_device(dev); + arm_smmu_remove_master(smmu, master); iommu_device_unlink(&smmu->iommu, dev); kfree(master); iommu_fwspec_free(dev); @@ -2483,6 +2592,9 @@ static int arm_smmu_init_structures(struct arm_smmu_device *smmu) int ret; atomic_set(&smmu->sync_nr, 0); + mutex_init(&smmu->streams_mutex); + smmu->streams = RB_ROOT; + ret = arm_smmu_init_queues(smmu); if (ret) return ret;