From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.6 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE17BC38BE8 for ; Mon, 24 Feb 2020 18:25:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8859820838 for ; Mon, 24 Feb 2020 18:25:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="zuB/c0OA" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8859820838 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id CAFB86B009B; Mon, 24 Feb 2020 13:24:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C5B8E6B009C; Mon, 24 Feb 2020 13:24:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B24BB6B009D; Mon, 24 Feb 2020 13:24:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0157.hostedemail.com [216.40.44.157]) by kanga.kvack.org (Postfix) with ESMTP id 96DBA6B009B for ; Mon, 24 Feb 2020 13:24:53 -0500 (EST) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 3EEC18245578 for ; Mon, 24 Feb 2020 18:24:53 +0000 (UTC) X-FDA: 76525846866.27.money59_64c834f8d715f X-HE-Tag: money59_64c834f8d715f X-Filterd-Recvd-Size: 12068 Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by imf16.hostedemail.com (Postfix) with ESMTP for ; Mon, 24 Feb 2020 18:24:52 +0000 (UTC) Received: by mail-wm1-f65.google.com with SMTP id a9so345615wmj.3 for ; Mon, 24 Feb 2020 10:24:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5LozG5AByqC+Y2QsZ96qniz5z6ofm6RdMRwjrLXS+sI=; b=zuB/c0OAZTGH2gXaCP3au67WP342eVaV0fpGipW5OGGN73wuEBs/1ROZf/qKpO3VQt woFLTxZNGIJ4967+Me5izVNTthCeacqxnSzqP00CSdVZMYLxj/tNulidjUqG/0VzmRjZ QBZKhGAJqmIBtlFc+1BYL0isbmHkquFthlxS1h9jLjYAR2EePZIEbd27msJirvOsnTsx RtziIkQVAZ7U2qRYVUnPq6uOoH+JWffn9cp0foAx+2pOdmEGO8+xrnjYEsG5ZmahMt01 evtZlwEGkxH6SJ4mAKSe6obj4rsKWhmZnxT9Y8StwOwgIqQtl1cZzvX0yP51JfpyAWBn X2ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5LozG5AByqC+Y2QsZ96qniz5z6ofm6RdMRwjrLXS+sI=; b=ZEacwt/dgCxibT0TR+iUttozmcpAVeUg3OybRJGrh11knsTWrOc8pbYpcAaDnSeSLL C5GZz2a3fFAZi3rz6alLj7EPEkSaFbqjcu5RMycBrFQLpDt2YNdxy3dx0lr1+lGghV6S x/M/TgwrOXgWq+GRr+cmCGPs6JQfq7zM4j0iBvuRbko9zVx2PFjwIrhsm2hHAjkV0h9y +PYU467GRD4LqIrK8nAwMPSrfNh6LfKZd3uR2SG4abMg0SVmzpFXYyE1/t2HpXTrxLrG W9Khmptpj9Hq73edx/HJm/0V0bxhQXlIW21+6zFUzoQ3OFkS99t9Vq0V+JOLjvQPjaZi pIiQ== X-Gm-Message-State: APjAAAUIBHWHDSNXL7UnPfcaCTPdPlxy2o4FkVlrm8fviZ4kvIbXI6sL DpF2CeVY6YKzN/gAWmRzJ5jDfbDyu1I= X-Google-Smtp-Source: APXvYqzYYT4fN7IA/sD4kQOlIG2hDqJ3nfsH3tflLkE3KADa5Uj7qCLqWe6yrMSFQh0UM4q/4UEzKA== X-Received: by 2002:a1c:f001:: with SMTP id a1mr323565wmb.76.1582568691412; Mon, 24 Feb 2020 10:24:51 -0800 (PST) Received: from localhost.localdomain ([2001:171b:c9a8:fbc0:116c:c27a:3e7f:5eaf]) by smtp.gmail.com with ESMTPSA id n3sm304255wmc.27.2020.02.24.10.24.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Feb 2020 10:24:51 -0800 (PST) From: Jean-Philippe Brucker To: iommu@lists.linux-foundation.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-pci@vger.kernel.org, linux-mm@kvack.org Cc: joro@8bytes.org, robh+dt@kernel.org, mark.rutland@arm.com, catalin.marinas@arm.com, will@kernel.org, robin.murphy@arm.com, kevin.tian@intel.com, baolu.lu@linux.intel.com, Jonathan.Cameron@huawei.com, jacob.jun.pan@linux.intel.com, christian.koenig@amd.com, yi.l.liu@intel.com, zhangfei.gao@linaro.org, Jean-Philippe Brucker Subject: [PATCH v4 20/26] iommu/arm-smmu-v3: Maintain a SID->device structure Date: Mon, 24 Feb 2020 19:23:55 +0100 Message-Id: <20200224182401.353359-21-jean-philippe@linaro.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200224182401.353359-1-jean-philippe@linaro.org> References: <20200224182401.353359-1-jean-philippe@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: From: Jean-Philippe Brucker 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 | 177 +++++++++++++++++++++++++++++------- 1 file changed, 145 insertions(+), 32 deletions(-) diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c index 015e8e59e0ef..28f8583cd47b 100644 --- a/drivers/iommu/arm-smmu-v3.c +++ b/drivers/iommu/arm-smmu-v3.c @@ -684,6 +684,15 @@ struct arm_smmu_device { =20 /* IOMMU core code handle */ struct iommu_device iommu; + + struct rb_root streams; + struct mutex streams_mutex; +}; + +struct arm_smmu_stream { + u32 id; + struct arm_smmu_master *master; + struct rb_node node; }; =20 /* SMMU private data for each master */ @@ -692,8 +701,8 @@ struct arm_smmu_master { struct device *dev; struct arm_smmu_domain *domain; struct list_head domain_head; - u32 *sids; - unsigned int num_sids; + struct arm_smmu_stream *streams; + unsigned int num_streams; bool ats_enabled; unsigned int ssid_bits; }; @@ -1573,8 +1582,8 @@ static void arm_smmu_sync_cd(struct arm_smmu_domain= *smmu_domain, =20 spin_lock_irqsave(&smmu_domain->devices_lock, flags); list_for_each_entry(master, &smmu_domain->devices, domain_head) { - for (i =3D 0; i < master->num_sids; i++) { - cmd.cfgi.sid =3D master->sids[i]; + for (i =3D 0; i < master->num_streams; i++) { + cmd.cfgi.sid =3D master->streams[i].id; arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd); } } @@ -2201,6 +2210,32 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu= _device *smmu, u32 sid) return 0; } =20 +__maybe_unused +static struct arm_smmu_master * +arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid) +{ + struct rb_node *node; + struct arm_smmu_stream *stream; + struct arm_smmu_master *master =3D NULL; + + mutex_lock(&smmu->streams_mutex); + node =3D smmu->streams.rb_node; + while (node) { + stream =3D rb_entry(node, struct arm_smmu_stream, node); + if (stream->id < sid) { + node =3D node->rb_right; + } else if (stream->id > sid) { + node =3D node->rb_left; + } else { + master =3D 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) { @@ -2434,8 +2469,8 @@ static int arm_smmu_atc_inv_master(struct arm_smmu_= master *master, int ssid) =20 arm_smmu_atc_inv_to_cmd(ssid, 0, 0, &cmd); =20 - for (i =3D 0; i < master->num_sids; i++) { - cmd.atc.sid =3D master->sids[i]; + for (i =3D 0; i < master->num_streams; i++) { + cmd.atc.sid =3D master->streams[i].id; arm_smmu_cmdq_issue_cmd(master->smmu, &cmd); } =20 @@ -2478,8 +2513,8 @@ static int arm_smmu_atc_inv_domain(struct arm_smmu_= domain *smmu_domain, if (!master->ats_enabled) continue; =20 - for (i =3D 0; i < master->num_sids; i++) { - cmd.atc.sid =3D master->sids[i]; + for (i =3D 0; i < master->num_streams; i++) { + cmd.atc.sid =3D master->streams[i].id; arm_smmu_cmdq_batch_add(smmu_domain->smmu, &cmds, &cmd); } } @@ -2846,13 +2881,13 @@ static void arm_smmu_install_ste_for_dev(struct a= rm_smmu_master *master) int i, j; struct arm_smmu_device *smmu =3D master->smmu; =20 - for (i =3D 0; i < master->num_sids; ++i) { - u32 sid =3D master->sids[i]; + for (i =3D 0; i < master->num_streams; ++i) { + u32 sid =3D master->streams[i].id; __le64 *step =3D arm_smmu_get_step_for_sid(smmu, sid); =20 /* Bridged PCI devices may end up with duplicated IDs */ for (j =3D 0; j < i; j++) - if (master->sids[j] =3D=3D sid) + if (master->streams[j].id =3D=3D sid) break; if (j < i) continue; @@ -3105,8 +3140,8 @@ static void arm_smmu_mm_invalidate(struct device *d= ev, int pasid, void *entry, =20 arm_smmu_atc_inv_to_cmd(pasid, iova, size, &cmd); =20 - for (i =3D 0; i < master->num_sids; i++) { - cmd.atc.sid =3D master->sids[i]; + for (i =3D 0; i < master->num_streams; i++) { + cmd.atc.sid =3D master->streams[i].id; arm_smmu_cmdq_batch_add(master->smmu, &cmds, &cmd); } =20 @@ -3206,11 +3241,99 @@ static bool arm_smmu_sid_in_range(struct arm_smmu= _device *smmu, u32 sid) return sid < limit; } =20 +static int arm_smmu_insert_master(struct arm_smmu_device *smmu, + struct arm_smmu_master *master) +{ + int i; + int ret =3D 0; + struct arm_smmu_stream *new_stream, *cur_stream; + struct rb_node **new_node, *parent_node =3D NULL; + struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(master->dev); + + master->streams =3D kcalloc(fwspec->num_ids, + sizeof(struct arm_smmu_stream), GFP_KERNEL); + if (!master->streams) + return -ENOMEM; + master->num_streams =3D fwspec->num_ids; + + mutex_lock(&smmu->streams_mutex); + for (i =3D 0; i < fwspec->num_ids && !ret; i++) { + u32 sid =3D fwspec->ids[i]; + + new_stream =3D &master->streams[i]; + new_stream->id =3D sid; + new_stream->master =3D master; + + /* Check the SIDs are in range of the SMMU and our stream table */ + if (!arm_smmu_sid_in_range(smmu, sid)) { + ret =3D -ERANGE; + break; + } + + /* Ensure l2 strtab is initialised */ + if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) { + ret =3D arm_smmu_init_l2_strtab(smmu, sid); + if (ret) + break; + } + + /* Insert into SID tree */ + new_node =3D &(smmu->streams.rb_node); + while (*new_node) { + cur_stream =3D rb_entry(*new_node, struct arm_smmu_stream, + node); + parent_node =3D *new_node; + if (cur_stream->id > new_stream->id) { + new_node =3D &((*new_node)->rb_left); + } else if (cur_stream->id < new_stream->id) { + new_node =3D &((*new_node)->rb_right); + } else { + dev_warn(master->dev, + "stream %u already in tree\n", + cur_stream->id); + ret =3D -EINVAL; + break; + } + } + + if (!ret) { + rb_link_node(&new_stream->node, parent_node, new_node); + rb_insert_color(&new_stream->node, &smmu->streams); + } + } + + if (ret) { + for (; i > 0; i--) + rb_erase(&master->streams[i].node, &smmu->streams); + kfree(master->streams); + } + mutex_unlock(&smmu->streams_mutex); + + return ret; +} + +static void arm_smmu_remove_master(struct arm_smmu_device *smmu, + struct arm_smmu_master *master) +{ + int i; + struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(master->dev); + + if (!master->streams) + return; + + mutex_lock(&smmu->streams_mutex); + for (i =3D 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; =20 static int arm_smmu_add_device(struct device *dev) { - int i, ret; + int ret; struct arm_smmu_device *smmu; struct arm_smmu_master *master; struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); @@ -3232,26 +3355,11 @@ static int arm_smmu_add_device(struct device *dev= ) =20 master->dev =3D dev; master->smmu =3D smmu; - master->sids =3D fwspec->ids; - master->num_sids =3D fwspec->num_ids; fwspec->iommu_priv =3D master; =20 - /* Check the SIDs are in range of the SMMU and our stream table */ - for (i =3D 0; i < master->num_sids; i++) { - u32 sid =3D master->sids[i]; - - if (!arm_smmu_sid_in_range(smmu, sid)) { - ret =3D -ERANGE; - goto err_free_master; - } - - /* Ensure l2 strtab is initialised */ - if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) { - ret =3D arm_smmu_init_l2_strtab(smmu, sid); - if (ret) - goto err_free_master; - } - } + ret =3D arm_smmu_insert_master(smmu, master); + if (ret) + goto err_free_master; =20 master->ssid_bits =3D min(smmu->ssid_bits, fwspec->num_pasid_bits); =20 @@ -3286,6 +3394,7 @@ static int arm_smmu_add_device(struct device *dev) iommu_device_unlink(&smmu->iommu, dev); err_disable_pasid: arm_smmu_disable_pasid(master); + arm_smmu_remove_master(smmu, master); err_free_master: kfree(master); fwspec->iommu_priv =3D NULL; @@ -3308,6 +3417,7 @@ static void arm_smmu_remove_device(struct device *d= ev) iommu_group_remove_device(dev); iommu_device_unlink(&smmu->iommu, dev); arm_smmu_disable_pasid(master); + arm_smmu_remove_master(smmu, master); kfree(master); iommu_fwspec_free(dev); } @@ -3751,6 +3861,9 @@ static int arm_smmu_init_structures(struct arm_smmu= _device *smmu) { int ret; =20 + mutex_init(&smmu->streams_mutex); + smmu->streams =3D RB_ROOT; + ret =3D arm_smmu_init_queues(smmu); if (ret) return ret; --=20 2.25.0