Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp926455pxj; Wed, 16 Jun 2021 17:26:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzhWpCMnYpC7cjSlFy5PhGO3xVKqzfyzmhLYb7ZyL6YFMS9zX5UsJX5uz0c/Ao+Aroafv6K X-Received: by 2002:a17:906:35cd:: with SMTP id p13mr2196949ejb.24.1623889618633; Wed, 16 Jun 2021 17:26:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623889618; cv=none; d=google.com; s=arc-20160816; b=r94BYWoIN0IuMij4axG+Mh+W+A2MLUC3mer9QGPqeqBNJxFx34yJ+g3irK3tgXL9B1 HZS9qE53/enR8mNttzSVAv4UhPSKqkedkwDCHQmVVrvI7t3fFfCF2HGVU2HgNyrsNPOX DFQnk0Z2787PGP6QUu9kKexuee2WNVp/abK1yrf31cy36Hke2JDFNMmYcH93UYjCHT8I 6bYbajzt02BCgX8x+I0AHlkmvHeLj2jor/273SfFuVgrlrnIbUu6Pz+irltcekL/PGPY LUnOsM8BUsYg6PJFZdsDnBwXlKM6RdJ92FZZtvYyVhaFafyA/PBo3jK4Lbe1jAemsCyl WVvg== 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=dRmBSLhAuXxBVTuhCH5oFREPk0hy4CPzH2ZUQRDqv1I=; b=EgCmmq5/VujBr/Kp6aov/vtJfO3zSrFxceAyuFHKUlRTRk4rk8rmwcnWLz9U54tvbH uJoKMjsMIYI9x8fRy40IBLe506KYg+IlgHmmnFkH6fbd3JC2xQ8rdoUCdsoQ955Smyit YACvwr9T0baJ3DKbjG2gX4ERhBtHzkyhavxLHmftvoucbhoN7QpMXNiq55rU2D89lQpW /Hz9JONlhH3WnGZodVJWXBNPjhE+tm3G1Tkjc+mBYMATjDM8vwFCkxovYgivt6o18Tgq O7QhYzO8O/Z1oAWLUMvbApQaVUf6amwcRSqsIEkptb0MfA6aVWA+77sIs7v12VF10w/M cThA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="BQujgSI/"; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h8si3771825edj.149.2021.06.16.17.26.36; Wed, 16 Jun 2021 17:26:58 -0700 (PDT) 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=@gmail.com header.s=20161025 header.b="BQujgSI/"; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231416AbhFPRgk (ORCPT + 99 others); Wed, 16 Jun 2021 13:36:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231372AbhFPRge (ORCPT ); Wed, 16 Jun 2021 13:36:34 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F44BC061574 for ; Wed, 16 Jun 2021 10:34:28 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id t9so2577182pgn.4 for ; Wed, 16 Jun 2021 10:34:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dRmBSLhAuXxBVTuhCH5oFREPk0hy4CPzH2ZUQRDqv1I=; b=BQujgSI/SQuHMrtiNYyK9oa6Xkfiw5O6r5xaRKxR6ggIFV/lLfFN2U4CWDUguYX9UY 9jpQBY6lck0rgEk9mvySpQp9laDFb9yNlYdGvZOlr3F4VijrCK63JKcT+e63tY4lPKc4 urCyYX9XgencbyvDKb5mWWeUJpbjqbPrtdAMUkXSUL7YfIQyK2n68UakUcbe1Xnl+FX5 eCuhPaLG2UK8XLqeUEs9M1G4jHPgnO/zdSFIi5NuznYdnN5a60txyM4Xixl4Qm409Uqf u/OPa6yktVFgohfdFrZ9OEVNWZuURcq3GgHDqrzDDozq7G9PSbAjaQ09Al6o5eoYKQ8h LaYw== 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=dRmBSLhAuXxBVTuhCH5oFREPk0hy4CPzH2ZUQRDqv1I=; b=eoYpdrsnFqSvl6dpAOawBcFQSFmtorCSqlAaA/iqbKc5Y7p8LMUGLrnnAp63WsHPgv e3UAjCP6JP8gm3M9rGa/+MBuQIpxXyVDau1+Y/+4rbOhdviYxQVZYG3AWzJN29ZAOuMG rk1tEnhb81d7QO9SNOVD60jP4L16Hci8Aidsu5OUvTa/DT+cdA3WXVMqsz8Q+NFZKiVs QKCKjTLqN118yjR11Dj1yeJKEFgKUasEIFuCX/7qkmWZ8W1OH9F7Us+irkxF8rpjFI4E ze5fC6x0sAFylNjfGxVfjyRJHCL//+f3oG01X2PVyR08ZCyOIE1bCiu+yEP2rx05WzXI JufA== X-Gm-Message-State: AOAM532GdeLNwQV/cATXLbSCJXcfH4Dz8X3PEOCGB1juVnIw5+zOd1Za hZmP6F4ExgE6z/fkH7oxN64= X-Received: by 2002:a05:6a00:c2:b029:2ee:9cfc:af85 with SMTP id e2-20020a056a0000c2b02902ee9cfcaf85mr983338pfj.78.1623864867588; Wed, 16 Jun 2021 10:34:27 -0700 (PDT) Received: from sc2-haas01-esx0118.eng.vmware.com ([66.170.99.1]) by smtp.gmail.com with ESMTPSA id m3sm2933901pfh.174.2021.06.16.10.34.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Jun 2021 10:34:27 -0700 (PDT) From: Nadav Amit X-Google-Original-From: Nadav Amit To: Joerg Roedel Cc: Robin Murphy , Will Deacon , Jiajun Cao , Lu Baolu , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Nadav Amit Subject: [PATCH v4 3/7] iommu: Improve iommu_iotlb_gather helpers Date: Wed, 16 Jun 2021 03:04:56 -0700 Message-Id: <20210616100500.174507-4-namit@vmware.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210616100500.174507-1-namit@vmware.com> References: <20210616100500.174507-1-namit@vmware.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Robin Murphy The Mediatek driver is not the only one which might want a basic address-based gathering behaviour, so although it's arguably simple enough to open-code, let's factor it out for the sake of cleanliness. Let's also take this opportunity to document the intent of these helpers for clarity. Cc: Joerg Roedel Cc: Will Deacon Cc: Jiajun Cao Cc: Robin Murphy Cc: Lu Baolu Cc: iommu@lists.linux-foundation.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Robin Murphy Signed-off-by: Nadav Amit --- Changes from Robin's version: * Added iommu_iotlb_gather_add_range() stub !CONFIG_IOMMU_API * Use iommu_iotlb_gather_add_range() in iommu_iotlb_gather_add_page() --- drivers/iommu/mtk_iommu.c | 6 +----- include/linux/iommu.h | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c index e06b8a0e2b56..cd457487ce81 100644 --- a/drivers/iommu/mtk_iommu.c +++ b/drivers/iommu/mtk_iommu.c @@ -521,12 +521,8 @@ static size_t mtk_iommu_unmap(struct iommu_domain *domain, struct iommu_iotlb_gather *gather) { struct mtk_iommu_domain *dom = to_mtk_domain(domain); - unsigned long end = iova + size - 1; - if (gather->start > iova) - gather->start = iova; - if (gather->end < end) - gather->end = end; + iommu_iotlb_gather_add_range(gather, iova, size); return dom->iop->unmap(dom->iop, iova, size, gather); } diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 32d448050bf7..e554871db46f 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -497,6 +497,38 @@ static inline void iommu_iotlb_sync(struct iommu_domain *domain, iommu_iotlb_gather_init(iotlb_gather); } +/** + * iommu_iotlb_gather_add_range - Gather for address-based TLB invalidation + * @gather: TLB gather data + * @iova: start of page to invalidate + * @size: size of page to invalidate + * + * Helper for IOMMU drivers to build arbitrarily-sized invalidation commands + * where only the address range matters, and simply minimising intermediate + * syncs is preferred. + */ +static inline void iommu_iotlb_gather_add_range(struct iommu_iotlb_gather *gather, + unsigned long iova, size_t size) +{ + unsigned long end = iova + size - 1; + + if (gather->start > iova) + gather->start = iova; + if (gather->end < end) + gather->end = end; +} + +/** + * iommu_iotlb_gather_add_page - Gather for page-based TLB invalidation + * @domain: IOMMU domain to be invalidated + * @gather: TLB gather data + * @iova: start of page to invalidate + * @size: size of page to invalidate + * + * Helper for IOMMU drivers to build invalidation commands based on individual + * pages, or with page size/table level hints which cannot be gathered if they + * differ. + */ static inline void iommu_iotlb_gather_add_page(struct iommu_domain *domain, struct iommu_iotlb_gather *gather, unsigned long iova, size_t size) @@ -515,11 +547,7 @@ static inline void iommu_iotlb_gather_add_page(struct iommu_domain *domain, gather->pgsize = size; } - if (gather->end < end) - gather->end = end; - - if (gather->start > start) - gather->start = start; + iommu_iotlb_gather_add_range(gather, iova, size); } /* PCI device grouping function */ -- 2.25.1