144643d
From patchwork Wed Mar 25 20:16:03 2020
144643d
Content-Type: text/plain; charset="utf-8"
144643d
MIME-Version: 1.0
144643d
Content-Transfer-Encoding: 7bit
144643d
X-Patchwork-Submitter: Thierry Reding <thierry.reding@gmail.com>
144643d
X-Patchwork-Id: 1261638
144643d
Return-Path: <linux-tegra-owner@vger.kernel.org>
144643d
X-Original-To: incoming@patchwork.ozlabs.org
144643d
Delivered-To: patchwork-incoming@bilbo.ozlabs.org
144643d
Authentication-Results: ozlabs.org; spf=none (no SPF record)
144643d
 smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67;
144643d
 helo=vger.kernel.org;
144643d
 envelope-from=linux-tegra-owner@vger.kernel.org;
144643d
 receiver=<UNKNOWN>)
144643d
Authentication-Results: ozlabs.org;
144643d
 dmarc=pass (p=none dis=none) header.from=gmail.com
144643d
Authentication-Results: ozlabs.org; dkim=pass (2048-bit key;
144643d
 unprotected) header.d=gmail.com header.i=@gmail.com
144643d
 header.a=rsa-sha256 header.s=20161025 header.b=sj7XVrax; 
144643d
 dkim-atps=neutral
144643d
Received: from vger.kernel.org (vger.kernel.org [209.132.180.67])
144643d
 by ozlabs.org (Postfix) with ESMTP id 48nfWs1X7mz9sRf
144643d
 for <incoming@patchwork.ozlabs.org>;
144643d
 Thu, 26 Mar 2020 07:16:09 +1100 (AEDT)
144643d
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
144643d
 id S1727374AbgCYUQI (ORCPT <rfc822;incoming@patchwork.ozlabs.org>);
144643d
 Wed, 25 Mar 2020 16:16:08 -0400
144643d
Received: from mail-wm1-f68.google.com ([209.85.128.68]:50585 "EHLO
144643d
 mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
144643d
 with ESMTP id S1727328AbgCYUQI (ORCPT
144643d
 <rfc822;linux-tegra@vger.kernel.org>);
144643d
 Wed, 25 Mar 2020 16:16:08 -0400
144643d
Received: by mail-wm1-f68.google.com with SMTP id d198so4073496wmd.0
144643d
 for <linux-tegra@vger.kernel.org>;
144643d
 Wed, 25 Mar 2020 13:16:07 -0700 (PDT)
144643d
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
144643d
 h=from:to:cc:subject:date:message-id:mime-version
144643d
 :content-transfer-encoding;
144643d
 bh=yPLnagV0XBnTWT+nGjtRaD+LnSq2BqmeAJnp8U+CWPw=;
144643d
 b=sj7XVraxdwiyRAeepEQ0wy1nLUUH6vcloNotxoFwaAZmvU2GILePtp+OM8VZxzmSg1
144643d
 qVjos+BzgdtxI0QGYvlsRwZJmw1PdwfTDzM8kMKmP2AfXDgnFG7LZsGZnzTmdPqErqG6
144643d
 RfQwpZiPunHplEvI/epnPHACQlV9HoX+teAIWP9gyJkMYwBCVOirkfv4yGqGZWyEciZ2
144643d
 yM5mGeUZ/OprHtVVEEuF5yb50CJm8cBEHBMr2ooS+0jm+avVEG8DKe9QM2nWgJB7+TXH
144643d
 7+iryK1A4PDr9L6syw0p6sAbkFd2+P/p44d/rqsKPWTQG0lkd0cgRHx9fVPls/P4Snyr
144643d
 JwCA==
144643d
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
144643d
 d=1e100.net; s=20161025;
144643d
 h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version
144643d
 :content-transfer-encoding;
144643d
 bh=yPLnagV0XBnTWT+nGjtRaD+LnSq2BqmeAJnp8U+CWPw=;
144643d
 b=HWu2t1YnW/GoMLlkfp6ZQha8CvUnfMi/OK1zsN3hDtTtMLwVQL9YBFPvXYfAASIGzA
144643d
 qXmgdbIdQmwOXRxlDmgcXk8KcOJmvnJTSoE+GPeLrKGVq9h2c6XLINshs7RDWqY7//GM
144643d
 /NMVkESX/sVh5qVQYVzsQOBWAsLkwpVAmt3lJ81XrCGdA/L5aN2FWOftTWJWoStgtHuB
144643d
 9N27ffBkV8/72gDCcGxM/lJlfxMBcfPIEMDGWlErsl2U/EPtF+e5AH1kF9/a+lImxa1h
144643d
 vBlXvgfPKazfOLm1jA809U0QJrCy5bmTOJsaLqnkLPNJRyvlY6JZqk8a1Wc4u6l44uoI
144643d
 4l3g==
144643d
X-Gm-Message-State: ANhLgQ0GzmzHn/uC4G4GzXRW/D8i6fcQ7Y04Wxx+yBOvoeixp0lD9PYD
144643d
 9Q7E3Ezt7uCnfh5D41Ym8jY=
144643d
X-Google-Smtp-Source: ADFU+vvV+Qjqcd+wksczhsC9MSisSEM36LfhftNulFkmYxqwCfpDcq22YDEoWHYpgjaXwwZC4lgCyg==
144643d
X-Received: by 2002:a7b:c842:: with SMTP id c2mr5416219wml.154.1585167366416; 
144643d
 Wed, 25 Mar 2020 13:16:06 -0700 (PDT)
144643d
Received: from localhost
144643d
 (p200300E41F4A9B0076D02BFFFE273F51.dip0.t-ipconnect.de.
144643d
 [2003:e4:1f4a:9b00:76d0:2bff:fe27:3f51])
144643d
 by smtp.gmail.com with ESMTPSA id
144643d
 i4sm132568wrm.32.2020.03.25.13.16.05
144643d
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
144643d
 Wed, 25 Mar 2020 13:16:05 -0700 (PDT)
144643d
From: Thierry Reding <thierry.reding@gmail.com>
144643d
To: Thierry Reding <thierry.reding@gmail.com>
144643d
Cc: dri-devel@lists.freedesktop.org, linux-tegra@vger.kernel.org
144643d
Subject: [PATCH 1/2] drm/tegra: Fix SMMU support on Tegra124 and Tegra210
144643d
Date: Wed, 25 Mar 2020 21:16:03 +0100
144643d
Message-Id: <20200325201604.833898-1-thierry.reding@gmail.com>
144643d
X-Mailer: git-send-email 2.24.1
144643d
MIME-Version: 1.0
144643d
Sender: linux-tegra-owner@vger.kernel.org
144643d
Precedence: bulk
144643d
List-ID: <linux-tegra.vger.kernel.org>
144643d
X-Mailing-List: linux-tegra@vger.kernel.org
144643d
144643d
From: Thierry Reding <treding@nvidia.com>
144643d
144643d
When testing whether or not to enable the use of the SMMU, consult the
144643d
supported DMA mask rather than the actually configured DMA mask, since
144643d
the latter might already have been restricted.
144643d
144643d
Fixes: 2d9384ff9177 ("drm/tegra: Relax IOMMU usage criteria on old Tegra")
144643d
Signed-off-by: Thierry Reding <treding@nvidia.com>
144643d
Tested-by: Jon Hunter <jonathanh@nvidia.com>
144643d
---
144643d
 drivers/gpu/drm/tegra/drm.c |  3 ++-
144643d
 drivers/gpu/host1x/dev.c    | 13 +++++++++++++
144643d
 include/linux/host1x.h      |  3 +++
144643d
 3 files changed, 18 insertions(+), 1 deletion(-)
144643d
144643d
diff --git a/drivers/gpu/drm/tegra/drm.c b/drivers/gpu/drm/tegra/drm.c
144643d
index bd268028fb3d..583cd6e0ae27 100644
144643d
--- a/drivers/gpu/drm/tegra/drm.c
144643d
+++ b/drivers/gpu/drm/tegra/drm.c
144643d
@@ -1039,6 +1039,7 @@ void tegra_drm_free(struct tegra_drm *tegra, size_t size, void *virt,
144643d
 
144643d
 static bool host1x_drm_wants_iommu(struct host1x_device *dev)
144643d
 {
144643d
+	struct host1x *host1x = dev_get_drvdata(dev->dev.parent);
144643d
 	struct iommu_domain *domain;
144643d
 
144643d
 	/*
144643d
@@ -1076,7 +1077,7 @@ static bool host1x_drm_wants_iommu(struct host1x_device *dev)
144643d
 	 * sufficient and whether or not the host1x is attached to an IOMMU
144643d
 	 * doesn't matter.
144643d
 	 */
144643d
-	if (!domain && dma_get_mask(dev->dev.parent) <= DMA_BIT_MASK(32))
144643d
+	if (!domain && host1x_get_dma_mask(host1x) <= DMA_BIT_MASK(32))
144643d
 		return true;
144643d
 
144643d
 	return domain != NULL;
144643d
diff --git a/drivers/gpu/host1x/dev.c b/drivers/gpu/host1x/dev.c
144643d
index 388bcc2889aa..40a4b9f8b861 100644
144643d
--- a/drivers/gpu/host1x/dev.c
144643d
+++ b/drivers/gpu/host1x/dev.c
144643d
@@ -502,6 +502,19 @@ static void __exit tegra_host1x_exit(void)
144643d
 }
144643d
 module_exit(tegra_host1x_exit);
144643d
 
144643d
+/**
144643d
+ * host1x_get_dma_mask() - query the supported DMA mask for host1x
144643d
+ * @host1x: host1x instance
144643d
+ *
144643d
+ * Note that this returns the supported DMA mask for host1x, which can be
144643d
+ * different from the applicable DMA mask under certain circumstances.
144643d
+ */
144643d
+u64 host1x_get_dma_mask(struct host1x *host1x)
144643d
+{
144643d
+	return host1x->info->dma_mask;
144643d
+}
144643d
+EXPORT_SYMBOL(host1x_get_dma_mask);
144643d
+
144643d
 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
144643d
 MODULE_AUTHOR("Terje Bergstrom <tbergstrom@nvidia.com>");
144643d
 MODULE_DESCRIPTION("Host1x driver for Tegra products");
144643d
diff --git a/include/linux/host1x.h b/include/linux/host1x.h
144643d
index 62d216ff1097..c230b4e70d75 100644
144643d
--- a/include/linux/host1x.h
144643d
+++ b/include/linux/host1x.h
144643d
@@ -17,9 +17,12 @@ enum host1x_class {
144643d
 	HOST1X_CLASS_GR3D = 0x60,
144643d
 };
144643d
 
144643d
+struct host1x;
144643d
 struct host1x_client;
144643d
 struct iommu_group;
144643d
 
144643d
+u64 host1x_get_dma_mask(struct host1x *host1x);
144643d
+
144643d
 /**
144643d
  * struct host1x_client_ops - host1x client operations
144643d
  * @init: host1x client initialization code
144643d
144643d
From patchwork Wed Mar 25 20:16:04 2020
144643d
Content-Type: text/plain; charset="utf-8"
144643d
MIME-Version: 1.0
144643d
Content-Transfer-Encoding: 7bit
144643d
X-Patchwork-Submitter: Thierry Reding <thierry.reding@gmail.com>
144643d
X-Patchwork-Id: 1261639
144643d
Return-Path: <linux-tegra-owner@vger.kernel.org>
144643d
X-Original-To: incoming@patchwork.ozlabs.org
144643d
Delivered-To: patchwork-incoming@bilbo.ozlabs.org
144643d
Authentication-Results: ozlabs.org; spf=none (no SPF record)
144643d
 smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67;
144643d
 helo=vger.kernel.org;
144643d
 envelope-from=linux-tegra-owner@vger.kernel.org;
144643d
 receiver=<UNKNOWN>)
144643d
Authentication-Results: ozlabs.org;
144643d
 dmarc=pass (p=none dis=none) header.from=gmail.com
144643d
Authentication-Results: ozlabs.org; dkim=pass (2048-bit key;
144643d
 unprotected) header.d=gmail.com header.i=@gmail.com
144643d
 header.a=rsa-sha256 header.s=20161025 header.b=XXUz449u; 
144643d
 dkim-atps=neutral
144643d
Received: from vger.kernel.org (vger.kernel.org [209.132.180.67])
144643d
 by ozlabs.org (Postfix) with ESMTP id 48nfWw6NvSz9sPk
144643d
 for <incoming@patchwork.ozlabs.org>;
144643d
 Thu, 26 Mar 2020 07:16:12 +1100 (AEDT)
144643d
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
144643d
 id S1727316AbgCYUQM (ORCPT <rfc822;incoming@patchwork.ozlabs.org>);
144643d
 Wed, 25 Mar 2020 16:16:12 -0400
144643d
Received: from mail-wr1-f65.google.com ([209.85.221.65]:33914 "EHLO
144643d
 mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
144643d
 with ESMTP id S1727328AbgCYUQM (ORCPT
144643d
 <rfc822;linux-tegra@vger.kernel.org>);
144643d
 Wed, 25 Mar 2020 16:16:12 -0400
144643d
Received: by mail-wr1-f65.google.com with SMTP id 65so4990084wrl.1
144643d
 for <linux-tegra@vger.kernel.org>;
144643d
 Wed, 25 Mar 2020 13:16:09 -0700 (PDT)
144643d
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
144643d
 h=from:to:cc:subject:date:message-id:in-reply-to:references
144643d
 :mime-version:content-transfer-encoding;
144643d
 bh=aW1zxIHiei+l8kDSE2lVXf/aMBDE/GtIkGFrQXvKkrY=;
144643d
 b=XXUz449uJivXz+1lH6pKa9IvT3vUx61/skXaEyQxpkslFR268FwckKE0ryQDUx701N
144643d
 hFN9ocSGCuE6bKpdgya8YmthXDASOYWZzKV0R5jms1rqgazVMF6jARv+kE4Jaj9Ek4tl
144643d
 4eTpmnHinx0xIrgGWCQbfltjb+zAE5XOGX8UCX1526r3yQQpu+OQlKZ70Tvq3pdw0zfT
144643d
 URkTU8sfdTa9DCxUSsUukPcK9vKOk6XHkFleL6FisODDvXphdzzLa1TCv9UTGLrUsHSd
144643d
 XDrukLto5efrUE03q5jP6ZN4xbnLDbhY6IkB7PAW1qwSPG/Eg0p0ivpJ58+QwwmBH6zF
144643d
 ByDQ==
144643d
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
144643d
 d=1e100.net; s=20161025;
144643d
 h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
144643d
 :references:mime-version:content-transfer-encoding;
144643d
 bh=aW1zxIHiei+l8kDSE2lVXf/aMBDE/GtIkGFrQXvKkrY=;
144643d
 b=DIWKPWCoYx1rnX34DSkRPm2K6lR1SurVvq+IIY5Nrc9uq+E3pmXQcActG0DDAHHK8a
144643d
 SgnziEvuWTeROgrlwONYq+FUZRQ6s1TRR1+qDXqAlRtdebU/cEep+LRvdzJe/qJBpPqd
144643d
 SnSTR3Xntgo7EcyLRj9YqSodasylPt3OzrhuDudfTSQtKZghElLfyJV/tzgwG+OC3TD4
144643d
 RJAykZ0tgWHy7Bc1UB+z6LovuT/sgcPUSLfNqDehQWqwQeqHqXgFAomUN0CCEr2YdjkT
144643d
 sCpBZPqKtb22FdDWlDiNnEkEmMPA+K4MIWbZL9VuvArjFaaBn6fBxvnX4tAKEcOiKeUy
144643d
 EZXw==
144643d
X-Gm-Message-State: ANhLgQ1Vj1gSFYKgV/7jV1T3UIwTE5jasGmLOhuuGuWvjBs2xXUgieyz
144643d
 VhNVgYIYU/8R/0Vx9Hv44rw=
144643d
X-Google-Smtp-Source: ADFU+vtTfrVHW69I+ZhOz8qw8xUje/j42rKoNxAP2wTt+E5WQ5s6QhBcgeHzC4Bw5Q5NdWxjLUtZ/g==
144643d
X-Received: by 2002:adf:800e:: with SMTP id 14mr5104354wrk.369.1585167368929; 
144643d
 Wed, 25 Mar 2020 13:16:08 -0700 (PDT)
144643d
Received: from localhost
144643d
 (p200300E41F4A9B0076D02BFFFE273F51.dip0.t-ipconnect.de.
144643d
 [2003:e4:1f4a:9b00:76d0:2bff:fe27:3f51])
144643d
 by smtp.gmail.com with ESMTPSA id
144643d
 e9sm151985wrw.30.2020.03.25.13.16.07
144643d
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
144643d
 Wed, 25 Mar 2020 13:16:07 -0700 (PDT)
144643d
From: Thierry Reding <thierry.reding@gmail.com>
144643d
To: Thierry Reding <thierry.reding@gmail.com>
144643d
Cc: dri-devel@lists.freedesktop.org, linux-tegra@vger.kernel.org
144643d
Subject: [PATCH 2/2] gpu: host1x: Use SMMU on Tegra124 and Tegra210
144643d
Date: Wed, 25 Mar 2020 21:16:04 +0100
144643d
Message-Id: <20200325201604.833898-2-thierry.reding@gmail.com>
144643d
X-Mailer: git-send-email 2.24.1
144643d
In-Reply-To: <20200325201604.833898-1-thierry.reding@gmail.com>
144643d
References: <20200325201604.833898-1-thierry.reding@gmail.com>
144643d
MIME-Version: 1.0
144643d
Sender: linux-tegra-owner@vger.kernel.org
144643d
Precedence: bulk
144643d
List-ID: <linux-tegra.vger.kernel.org>
144643d
X-Mailing-List: linux-tegra@vger.kernel.org
144643d
144643d
From: Thierry Reding <treding@nvidia.com>
144643d
144643d
Tegra124 and Tegra210 support addressing more than 32 bits of physical
144643d
memory. However, since their host1x does not support the wide GATHER
144643d
opcode, they should use the SMMU if at all possible to ensure that all
144643d
the system memory can be used for command buffers, irrespective of
144643d
whether or not the host1x firewall is enabled.
144643d
144643d
Signed-off-by: Thierry Reding <treding@nvidia.com>
144643d
Tested-by: Jon Hunter <jonathanh@nvidia.com>
144643d
---
144643d
 drivers/gpu/host1x/dev.c | 46 ++++++++++++++++++++++++++++++++++++----
144643d
 1 file changed, 42 insertions(+), 4 deletions(-)
144643d
144643d
diff --git a/drivers/gpu/host1x/dev.c b/drivers/gpu/host1x/dev.c
144643d
index 40a4b9f8b861..d24344e91922 100644
144643d
--- a/drivers/gpu/host1x/dev.c
144643d
+++ b/drivers/gpu/host1x/dev.c
144643d
@@ -192,17 +192,55 @@ static void host1x_setup_sid_table(struct host1x *host)
144643d
 	}
144643d
 }
144643d
 
144643d
+static bool host1x_wants_iommu(struct host1x *host1x)
144643d
+{
144643d
+	/*
144643d
+	 * If we support addressing a maximum of 32 bits of physical memory
144643d
+	 * and if the host1x firewall is enabled, there's no need to enable
144643d
+	 * IOMMU support. This can happen for example on Tegra20, Tegra30
144643d
+	 * and Tegra114.
144643d
+	 *
144643d
+	 * Tegra124 and later can address up to 34 bits of physical memory and
144643d
+	 * many platforms come equipped with more than 2 GiB of system memory,
144643d
+	 * which requires crossing the 4 GiB boundary. But there's a catch: on
144643d
+	 * SoCs before Tegra186 (i.e. Tegra124 and Tegra210), the host1x can
144643d
+	 * only address up to 32 bits of memory in GATHER opcodes, which means
144643d
+	 * that command buffers need to either be in the first 2 GiB of system
144643d
+	 * memory (which could quickly lead to memory exhaustion), or command
144643d
+	 * buffers need to be treated differently from other buffers (which is
144643d
+	 * not possible with the current ABI).
144643d
+	 *
144643d
+	 * A third option is to use the IOMMU in these cases to make sure all
144643d
+	 * buffers will be mapped into a 32-bit IOVA space that host1x can
144643d
+	 * address. This allows all of the system memory to be used and works
144643d
+	 * within the limitations of the host1x on these SoCs.
144643d
+	 *
144643d
+	 * In summary, default to enable IOMMU on Tegra124 and later. For any
144643d
+	 * of the earlier SoCs, only use the IOMMU for additional safety when
144643d
+	 * the host1x firewall is disabled.
144643d
+	 */
144643d
+	if (host1x->info->dma_mask <= DMA_BIT_MASK(32)) {
144643d
+		if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
144643d
+			return false;
144643d
+	}
144643d
+
144643d
+	return true;
144643d
+}
144643d
+
144643d
 static struct iommu_domain *host1x_iommu_attach(struct host1x *host)
144643d
 {
144643d
 	struct iommu_domain *domain = iommu_get_domain_for_dev(host->dev);
144643d
 	int err;
144643d
 
144643d
 	/*
144643d
-	 * If the host1x firewall is enabled, there's no need to enable IOMMU
144643d
-	 * support. Similarly, if host1x is already attached to an IOMMU (via
144643d
-	 * the DMA API), don't try to attach again.
144643d
+	 * We may not always want to enable IOMMU support (for example if the
144643d
+	 * host1x firewall is already enabled and we don't support addressing
144643d
+	 * more than 32 bits of physical memory), so check for that first.
144643d
+	 *
144643d
+	 * Similarly, if host1x is already attached to an IOMMU (via the DMA
144643d
+	 * API), don't try to attach again.
144643d
 	 */
144643d
-	if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) || domain)
144643d
+	if (!host1x_wants_iommu(host) || domain)
144643d
 		return domain;
144643d
 
144643d
 	host->group = iommu_group_get(host->dev);