Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp5921140iob; Tue, 10 May 2022 06:46:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwg5m2zLy2aw22+qCG/Y15ISAwsaZn3u9wSY5sKBrQ1xksY8mu8TZlp7GAysFoqh53RcflO X-Received: by 2002:a17:907:6e08:b0:6f4:a749:f483 with SMTP id sd8-20020a1709076e0800b006f4a749f483mr19542066ejc.225.1652190385069; Tue, 10 May 2022 06:46:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652190385; cv=none; d=google.com; s=arc-20160816; b=AM8/xTQxKlywNWpm3mgGjLvZW+/YegjpWcoL9zJV+tDQF9c0biWTtxlPc5CwO6QHr6 6Buojh4/HxARbhbbKyhiBX7r0b4FlUhNypS7Fo3+lhKhw12AtZiG+NYs6WGe6SNyzqvi RyO0ft4M25YQnlf77FYhF7EnpflkXQ34NcU/M3KcwBO3JUszjCyFD/X4Q7cwt56W1Bhp 4Dxr6Iiio+8qRo52QaYmZIuhowwHMZIFy8psImiRVin2d+xJ0SGTXMIEcAeTGJBwsar4 rlt+DvfggEFfNR3/dGX3M7ob2SLUvNDXB0v1wCL1UqpePrmrpx0NMDC7Ndpoa52BGu8u zMzw== 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=FcVcXRszvrklQMK/iNu+a10X8T350n/w2Yqde/U/l1c=; b=scprbKVm3BmVz52NxZP2dtyPwd8aUipIlHeHOHt1K1qO64TFu8rRH9ObGLcYvFSzBV mCY/o/8oaSEKXHw4giUr886931bUuN9dMvHZMuKAn/6qqr+wKZMQQk5DzuCSctbup7VD xoibBYUCFmUAGW6r7h3BxValqfpQVgHw2/skT86brzWhaksEmIBoPOMpzztUWTJcgHOK 7SfkgTG3ClDK/P48d3Wh07/ywaaQ0k5EPyyHj0SNGb9jmzYSdBiinvzt5TFFYGBNurC7 2y8gDImEXTtWaL3lORjLr9KVBAUSImsvHY696RaICviHlNIZpLpC8aPRam61568ZrHtn cZAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nc6xityH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i18-20020a508712000000b00425b6b65568si14778607edb.107.2022.05.10.06.46.00; Tue, 10 May 2022 06:46:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nc6xityH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237146AbiEJGZ4 (ORCPT + 99 others); Tue, 10 May 2022 02:25:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232406AbiEJGZX (ORCPT ); Tue, 10 May 2022 02:25:23 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E31846673 for ; Mon, 9 May 2022 23:21:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1652163679; x=1683699679; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=60KTAdzso4Vrtd8ZqxID77V2u8yEj+ii15BiJceQGSM=; b=nc6xityHnxFF+gg+KYhDH0VMMl3db4vo2rDWfwqkJ75BhXt3L+aV3Kzc TKtyz3l4NWpRXnzUBr0IxE0mtMys5L2V1U0uPxPUzi0OfHTr+JjmEknOr x/O1z/67B+e8kspKx2go4QO4w/s+hgfNRT31ytvzuofpxjBFEhdcyrnRM 4Wf0gpSXI94cEMm1RKuaNDcX3bgVcdUfxCoWwbxPNgmvgwJ7ikap0XMvD jKL1Sjxx7KqetHEgH3logRuo5eTPCLADGw1DZDuA00Q0eosdg/miVoXWH YkjaL7uU3ABKXhU8DVjvVVRtO8PAtDgAqcR0Dsja82u3+aJWRKBEELxc5 A==; X-IronPort-AV: E=McAfee;i="6400,9594,10342"; a="332312970" X-IronPort-AV: E=Sophos;i="5.91,213,1647327600"; d="scan'208";a="332312970" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 May 2022 23:21:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.91,213,1647327600"; d="scan'208";a="552636530" Received: from allen-box.sh.intel.com ([10.239.159.48]) by orsmga002.jf.intel.com with ESMTP; 09 May 2022 23:21:12 -0700 From: Lu Baolu To: Joerg Roedel , Jason Gunthorpe , Christoph Hellwig , Kevin Tian , Ashok Raj , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Dave Jiang , Vinod Koul Cc: Eric Auger , Liu Yi L , Jacob jun Pan , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v6 07/12] arm-smmu-v3/sva: Add SVA domain support Date: Tue, 10 May 2022 14:17:33 +0800 Message-Id: <20220510061738.2761430-8-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220510061738.2761430-1-baolu.lu@linux.intel.com> References: <20220510061738.2761430-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add support for SVA domain allocation and provide an SVA-specific iommu_domain_ops. Signed-off-by: Lu Baolu --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 6 ++ .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 68 +++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 3 + 3 files changed, 77 insertions(+) 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 cd48590ada30..e077f21e2528 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -759,6 +759,7 @@ struct iommu_sva *arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm, void arm_smmu_sva_unbind(struct iommu_sva *handle); u32 arm_smmu_sva_get_pasid(struct iommu_sva *handle); void arm_smmu_sva_notifier_synchronize(void); +struct iommu_domain *arm_smmu_sva_domain_alloc(void); #else /* CONFIG_ARM_SMMU_V3_SVA */ static inline bool arm_smmu_sva_supported(struct arm_smmu_device *smmu) { @@ -804,5 +805,10 @@ static inline u32 arm_smmu_sva_get_pasid(struct iommu_sva *handle) } static inline void arm_smmu_sva_notifier_synchronize(void) {} + +static inline struct iommu_domain *arm_smmu_sva_domain_alloc(void) +{ + return NULL; +} #endif /* CONFIG_ARM_SMMU_V3_SVA */ #endif /* _ARM_SMMU_V3_H */ diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c index c623dae1e115..9d176e836d6b 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -541,3 +541,71 @@ void arm_smmu_sva_notifier_synchronize(void) */ mmu_notifier_synchronize(); } + +static int arm_smmu_sva_attach_dev_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t id) +{ + int ret = 0; + struct mm_struct *mm; + struct iommu_sva *handle; + + if (domain->type != IOMMU_DOMAIN_SVA) + return -EINVAL; + + mm = domain->mm; + if (WARN_ON(!mm)) + return -ENODEV; + + mutex_lock(&sva_lock); + handle = __arm_smmu_sva_bind(dev, mm); + if (IS_ERR(handle)) + ret = PTR_ERR(handle); + mutex_unlock(&sva_lock); + + return ret; +} + +static void arm_smmu_sva_detach_dev_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t id) +{ + struct mm_struct *mm = domain->mm; + struct arm_smmu_bond *bond = NULL, *t; + struct arm_smmu_master *master = dev_iommu_priv_get(dev); + + mutex_lock(&sva_lock); + list_for_each_entry(t, &master->bonds, list) { + if (t->mm == mm) { + bond = t; + break; + } + } + + if (!WARN_ON(!bond) && refcount_dec_and_test(&bond->refs)) { + list_del(&bond->list); + arm_smmu_mmu_notifier_put(bond->smmu_mn); + kfree(bond); + } + mutex_unlock(&sva_lock); +} + +static void arm_smmu_sva_domain_free(struct iommu_domain *domain) +{ + kfree(domain); +} + +static const struct iommu_domain_ops arm_smmu_sva_domain_ops = { + .attach_dev_pasid = arm_smmu_sva_attach_dev_pasid, + .detach_dev_pasid = arm_smmu_sva_detach_dev_pasid, + .free = arm_smmu_sva_domain_free, +}; + +struct iommu_domain *arm_smmu_sva_domain_alloc(void) +{ + struct iommu_domain *domain; + + domain = kzalloc(sizeof(*domain), GFP_KERNEL); + if (domain) + domain->ops = &arm_smmu_sva_domain_ops; + + return domain; +} 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 afc63fce6107..9daf3de7e539 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1999,6 +1999,9 @@ static struct iommu_domain *arm_smmu_domain_alloc(unsigned type) { struct arm_smmu_domain *smmu_domain; + if (type == IOMMU_DOMAIN_SVA) + return arm_smmu_sva_domain_alloc(); + if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA && type != IOMMU_DOMAIN_DMA_FQ && -- 2.25.1