Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp2989074pxb; Mon, 16 Nov 2020 02:47:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJwBuBbKMCDuNxb2FTfTR1GMN3Kf4iBF5JVMujy2cFihagDsgGcV1K4eblmPvDilfh7O56UG X-Received: by 2002:a17:906:6d99:: with SMTP id h25mr14271905ejt.281.1605523672258; Mon, 16 Nov 2020 02:47:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605523672; cv=none; d=google.com; s=arc-20160816; b=JWwKOOSI6SJAzFXDmYZE7dTzReNbsugxy4RX/8NoqZME++/n8MtxzUpi8lOWH+7+UG h4sdt3KnsqY43rd1toRuaJ+pDREZH+nodCNpOjvctNHqe3JLoUAxiV0OR6Xy7MRb5jUv mNGWcahWHI02jT3dkzPPuOTKcTgNJtG3+CqPv0ynh+3PD8565wLgYHueIj2qClc58trS EHRgMhWLbJ9JdhvDLkN3V/BVOx8isg6TUUX91c0sT81kNBMtDEn1Kb7U0GtLTWS219XU KO2IuhrrfkR0W5BWDg7f3hT4Ui/yL81gHVtW2FxHuWKfs0uMsgDp6sgm/9LfdACIdCVq py4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8xskpuXRfsMuaVwlRSgKJl6bCTbY9rR/XvcIHxGGJ1U=; b=x/iYmO4ogr/hPXnYmnkUKahDh6yZJnnv/ckyBIUhnc/Z5yWPoPZFm0R/QGeu/XSF9s Q5jospJEe3EVtp4TwJmgK3Xn0LLFs8+Y6UTDLmKZoPKhVInsMeAMwLyybHUV4KvArK6w Wn3vDCA0/voA4l+y90X9fEVJpoacv0zeCdnlZ6bZLUhChdiw0H5+0N/a/DCSIdpzph6H Fdhy1BNM2IUNmQooJVGwhMGu23w+edM/Sc4sRj2KlEdgMCM78Ja78y7nHQ7fSamoV0+E FsuhR9MKldgZypUPnuc+Dtd/9NWC4oW02hDC2Bp+nlsI569Iir/8GqZlfg2J24pmXHdw iRfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ed0lNQXC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r21si10926510ejs.293.2020.11.16.02.47.29; Mon, 16 Nov 2020 02:47:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ed0lNQXC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729326AbgKPKn7 (ORCPT + 99 others); Mon, 16 Nov 2020 05:43:59 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:52707 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726281AbgKPKn6 (ORCPT ); Mon, 16 Nov 2020 05:43:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1605523436; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8xskpuXRfsMuaVwlRSgKJl6bCTbY9rR/XvcIHxGGJ1U=; b=Ed0lNQXCxfc9B5h8rRbHZWLbOdjK2EUpRimjYNL4cJ9yF6h6m/qSN+5Y6JzaeGRqQk+7J2 dak/SVaBYGVhk72w64o00RCFNLvUUTuLSLeYY8Uz1lOrWJv8ZGZuFTsBGaSHGB0205/Piq Wz5qRzVEUeO6f3Q5ECoIdJLemaT8BPg= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-295-ZmlLVbVMMGGlvksS42kUwQ-1; Mon, 16 Nov 2020 05:43:52 -0500 X-MC-Unique: ZmlLVbVMMGGlvksS42kUwQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3753310074CC; Mon, 16 Nov 2020 10:43:50 +0000 (UTC) Received: from laptop.redhat.com (ovpn-113-230.ams2.redhat.com [10.36.113.230]) by smtp.corp.redhat.com (Postfix) with ESMTP id 444995C5AF; Mon, 16 Nov 2020 10:43:40 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, will@kernel.org, joro@8bytes.org, maz@kernel.org, robin.murphy@arm.com Cc: jean-philippe@linaro.org, zhangfei.gao@linaro.org, zhangfei.gao@gmail.com, vivek.gautam@arm.com, shameerali.kolothum.thodi@huawei.com, alex.williamson@redhat.com, jacob.jun.pan@linux.intel.com, yi.l.liu@intel.com, tn@semihalf.com, nicoleotsuka@gmail.com Subject: [PATCH v12 03/15] iommu/arm-smmu-v3: Maintain a SID->device structure Date: Mon, 16 Nov 2020 11:43:04 +0100 Message-Id: <20201116104316.31816-4-eric.auger@redhat.com> In-Reply-To: <20201116104316.31816-1-eric.auger@redhat.com> References: <20201116104316.31816-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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: Eric Auger Signed-off-by: Jean-Philippe Brucker --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 99 +++++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 10 +++ 2 files changed, 109 insertions(+) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c index e634bbe60573..d828d6cbeb0e 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1350,6 +1350,32 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid) return 0; } +__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 = 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) { @@ -2306,6 +2332,69 @@ 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 *master) +{ + int i; + int ret = 0; + struct arm_smmu_stream *new_stream, *cur_stream; + struct rb_node **new_node, *parent_node = NULL; + + master->streams = kcalloc(master->num_sids, + sizeof(struct arm_smmu_stream), GFP_KERNEL); + if (!master->streams) + return -ENOMEM; + + mutex_lock(&smmu->streams_mutex); + for (i = 0; i < master->num_sids && !ret; i++) { + new_stream = &master->streams[i]; + new_stream->id = master->sids[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 *master) +{ + int i; + + if (!master->streams) + return; + + mutex_lock(&smmu->streams_mutex); + for (i = 0; i < master->num_sids; 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 struct iommu_device *arm_smmu_probe_device(struct device *dev) @@ -2369,6 +2458,10 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev) master->ssid_bits = min_t(u8, master->ssid_bits, CTXDESC_LINEAR_CDMAX); + ret = arm_smmu_insert_master(smmu, master); + if (ret) + goto err_free_master; + return &smmu->iommu; err_free_master: @@ -2381,14 +2474,17 @@ static void arm_smmu_release_device(struct device *dev) { struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); struct arm_smmu_master *master; + struct arm_smmu_device *smmu; if (!fwspec || fwspec->ops != &arm_smmu_ops) return; master = dev_iommu_priv_get(dev); + smmu = master->smmu; WARN_ON(arm_smmu_master_sva_enabled(master)); arm_smmu_detach_dev(master); arm_smmu_disable_pasid(master); + arm_smmu_remove_master(smmu, master); kfree(master); iommu_fwspec_free(dev); } @@ -2808,6 +2904,9 @@ static int arm_smmu_init_structures(struct arm_smmu_device *smmu) { int ret; + mutex_init(&smmu->streams_mutex); + smmu->streams = RB_ROOT; + ret = arm_smmu_init_queues(smmu); if (ret) return ret; diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h index d4b7f40ccb02..2944beb1571b 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -636,6 +636,15 @@ struct arm_smmu_device { /* 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; }; /* SMMU private data for each master */ @@ -646,6 +655,7 @@ struct arm_smmu_master { struct list_head domain_head; u32 *sids; unsigned int num_sids; + struct arm_smmu_stream *streams; bool ats_enabled; bool sva_enabled; struct list_head bonds; -- 2.21.3