Skip to contentSkip to conversation

Host1x IOMMU support + VIC support

PATCH In review Mikko Perttunen 7 participants 25 messages Nov 10, 2016 – Dec 14, 2016 10 messages not in the archive

Subsystems (5)
lore
Share & tools
lore

Mikko Perttunen

author maintainer
#0 lore raw

cover Host1x IOMMU support + VIC support

This series adds IOMMU support to Host1x and TegraDRM and adds support for the VIC host1x client so that host1x can be tested on modern Tegra platforms. It depends on the previous fix series. The whole thing (modulo patch order) is available as a git repository at
git://github.com/cyndis/linux.git; branch vic-v1.

IO memory management is organized such that there are two domains: the host1x domain and the tegradrm domain. The host1x domain is used by the host1x engine and contains the host1x CDMA and pushbuffers for submitted jobs.

The tegradrm domain is shared by all host1x units and contains GEM objects and memory allocated by the separate tegra_drm_alloc function. This function is currently used to allocate space for firmware blobs in the tegradrm domain.

A userspace test case for VIC can be found at
https://github.com/cyndis/drm/tree/work/tegra.
The testcase is in tests/tegra and is called submit_vic. The in-kernel firewall is not implemented for VIC; therefore, IOMMU must be enabled for the test to pass.

Tested with Jetson TX1 (T210). Probably works also with Jetson TK1 (T124). Note that due to hardware changes the testcase also needs to be changed to run properly on T124.

Thanks,
Mikko.

Arto Merilainen (2):
drm/tegra: Add falcon helper library
drm/tegra: Add VIC support

Mikko Perttunen (6):
drm/tegra: Add Tegra DRM allocation API drm/tegra: Allocate BOs from lower 4G when without IOMMU
gpu: host1x: Add IOMMU support
dt-bindings: Add bindings for the Tegra VIC
arm64: tegra: Enable VIC on T210
arm64: tegra: Enable IOMMU for Host1x on Tegra210

 .../display/tegra/nvidia,tegra20-host1x.txt        |  13 +
 arch/arm64/boot/dts/nvidia/tegra210.dtsi           |  19 +-
 drivers/gpu/drm/tegra/Makefile                     |   4 +-
 drivers/gpu/drm/tegra/drm.c                        | 101 +++++-
 drivers/gpu/drm/tegra/drm.h                        |   8 +
 drivers/gpu/drm/tegra/falcon.c                     | 256 +++++++++++++
 drivers/gpu/drm/tegra/falcon.h                     | 130 +++++++
 drivers/gpu/drm/tegra/gem.c                        |   2 +-
 drivers/gpu/drm/tegra/vic.c                        | 400 +++++++++++++++++++++
 drivers/gpu/drm/tegra/vic.h                        |  31 ++
 drivers/gpu/host1x/cdma.c                          |  65 +++-
 drivers/gpu/host1x/cdma.h                          |   4 +-
 drivers/gpu/host1x/dev.c                           |  39 +-
 drivers/gpu/host1x/dev.h                           |   5 +
 drivers/gpu/host1x/hw/cdma_hw.c                    |  10 +-
 drivers/gpu/host1x/job.c                           |  76 +++-
 drivers/gpu/host1x/job.h                           |   1 +
 include/linux/host1x.h                             |   1 +
 18 files changed, 1128 insertions(+), 37 deletions(-)
 create mode 100644 drivers/gpu/drm/tegra/falcon.c
 create mode 100644 drivers/gpu/drm/tegra/falcon.h
 create mode 100644 drivers/gpu/drm/tegra/vic.c
 create mode 100644 drivers/gpu/drm/tegra/vic.h
Signature · 6 lines
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#1 lore raw

PATCH 1/8 drm/tegra: Add Tegra DRM allocation API

Add a new IO virtual memory allocation API to allow clients to allocate non-GEM memory in the Tegra DRM IOMMU domain. This is required e.g. for loading client firmware when clients are attached to the IOMMU domain.

The allocator allocates contiguous physical pages that are then mapped contiguously to the IOMMU domain using the iova_domain library provided by the kernel. Contiguous physical pages are used so that the same allocator works also when IOMMU support is disabled and therefore devices access physical memory directly.

---
 drivers/gpu/drm/tegra/drm.c | 98 ++++++++++++++++++++++++++++++++++++++++++---
 drivers/gpu/drm/tegra/drm.h |  7 ++++
 2 files changed, 100 insertions(+), 5 deletions(-)
drivers/gpu/drm/tegra/drm.c +93 -5
index b8be3ee..ecfe7ba 100644
@@ -1,12 +1,13 @@
 /*
  * Copyright (C) 2012 Avionic Design GmbH
- * Copyright (C) 2012-2013 NVIDIA CORPORATION.  All rights reserved.
+ * Copyright (C) 2012-2015 NVIDIA CORPORATION.  All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
  */
 
+#include <linux/bitops.h>
 #include <linux/host1x.h>
 #include <linux/iommu.h>
 
@@ -23,6 +24,8 @@
 #define DRIVER_MINOR 0
 #define DRIVER_PATCHLEVEL 0
 
+#define IOVA_AREA_SZ (1024 * 1024 * 64) /* 64 MiB */
+
 struct tegra_drm_file {
 	struct list_head contexts;
 };
@@ -127,7 +130,8 @@ static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
 
 	if (iommu_present(&platform_bus_type)) {
 		struct iommu_domain_geometry *geometry;
-		u64 start, end;
+		unsigned long order;
+		u64 iova_start, start, end;
 
 		tegra->domain = iommu_domain_alloc(&platform_bus_type);
 		if (!tegra->domain) {
@@ -138,10 +142,17 @@ static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
 		geometry = &tegra->domain->geometry;
 		start = geometry->aperture_start;
 		end = geometry->aperture_end;
+		iova_start = end - IOVA_AREA_SZ + 1;
+
+		order = __ffs(tegra->domain->pgsize_bitmap);
+		init_iova_domain(&tegra->iova, 1UL << order,
+				 iova_start >> order,
+				 end >> order);
 
-		DRM_DEBUG_DRIVER("IOMMU aperture initialized (%#llx-%#llx)\n",
-				 start, end);
-		drm_mm_init(&tegra->mm, start, end - start + 1);
+		drm_mm_init(&tegra->mm, start, iova_start - start);
+
+		DRM_DEBUG("IOMMU apertures initialized (GEM: %#llx-%#llx, IOVA: %#llx-%#llx)\n",
+			  start, iova_start-1, iova_start, end);
 	}
 
 	mutex_init(&tegra->clients_lock);
@@ -208,6 +219,7 @@ static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
 	if (tegra->domain) {
 		iommu_domain_free(tegra->domain);
 		drm_mm_takedown(&tegra->mm);
+		put_iova_domain(&tegra->iova);
 	}
 free:
 	kfree(tegra);
@@ -232,6 +244,7 @@ static int tegra_drm_unload(struct drm_device *drm)
 	if (tegra->domain) {
 		iommu_domain_free(tegra->domain);
 		drm_mm_takedown(&tegra->mm);
+		put_iova_domain(&tegra->iova);
 	}
 
 	kfree(tegra);
@@ -975,6 +988,81 @@ int tegra_drm_unregister_client(struct tegra_drm *tegra,
 	return 0;
 }
 
+void *tegra_drm_alloc(struct tegra_drm *tegra, size_t size,
+			      dma_addr_t *iova)
+{
+	struct iova *alloc;
+	unsigned long shift;
+	void *virt;
+	gfp_t gfp;
+	int err;
+
+	if (tegra->domain)
+		size = iova_align(&tegra->iova, size);
+	else
+		size = PAGE_ALIGN(size);
+
+	gfp = GFP_KERNEL | __GFP_ZERO;
+	if (!tegra->domain) {
+		/*
+		 * Tegra210 has 64-bit physical addresses but units only support
+		 * 32-bit addresses, so if we don't have an IOMMU to do
+		 * translation, force allocations to be in the 32-bit range.
+		 */
+		gfp |= GFP_DMA;
+	}
+
+	virt = (void *)__get_free_pages(gfp, get_order(size));
+	if (!virt)
+		return NULL;
+
+	if (!tegra->domain) {
+		/*
+		 * If IOMMU is disabled, devices address physical memory
+		 * directly.
+		 */
+		*iova = virt_to_phys(virt);
+		return virt;
+	}
+
+	shift = iova_shift(&tegra->iova);
+	alloc = alloc_iova(&tegra->iova, size >> shift,
+			   tegra->domain->geometry.aperture_end >> shift, true);
+	if (!alloc)
+		goto free_pages;
+
+	*iova = iova_dma_addr(&tegra->iova, alloc);
+	err = iommu_map(tegra->domain, *iova, virt_to_phys(virt),
+			size, IOMMU_READ | IOMMU_WRITE);
+	if (err < 0)
+		goto free_iova;
+
+	return virt;
+
+free_iova:
+	__free_iova(&tegra->iova, alloc);
+free_pages:
+	free_pages((unsigned long)virt, get_order(size));
+
+	return NULL;
+}
+
+void tegra_drm_free(struct tegra_drm *tegra, size_t size, void *virt,
+		    dma_addr_t iova)
+{
+	if (tegra->domain)
+		size = iova_align(&tegra->iova, size);
+	else
+		size = PAGE_ALIGN(size);
+
+	if (tegra->domain) {
+		iommu_unmap(tegra->domain, iova, size);
+		free_iova(&tegra->iova, iova_pfn(&tegra->iova, iova));
+	}
+
+	free_pages((unsigned long)virt, get_order(size));
+}
+
 static int host1x_drm_probe(struct host1x_device *dev)
 {
 	struct drm_driver *driver = &tegra_drm_driver;
drivers/gpu/drm/tegra/drm.h +7 -0
index 0ddcce1..f75b505 100644
@@ -12,6 +12,7 @@
 
 #include <uapi/drm/tegra_drm.h>
 #include <linux/host1x.h>
+#include <linux/iova.h>
 #include <linux/of_gpio.h>
 
 #include <drm/drmP.h>
@@ -43,6 +44,8 @@ struct tegra_drm {
 	struct iommu_domain *domain;
 	struct drm_mm mm;
 
+	struct iova_domain iova;
+
 	struct mutex clients_lock;
 	struct list_head clients;
 
@@ -104,6 +107,10 @@ int tegra_drm_unregister_client(struct tegra_drm *tegra,
 int tegra_drm_init(struct tegra_drm *tegra, struct drm_device *drm);
 int tegra_drm_exit(struct tegra_drm *tegra);
 
+void *tegra_drm_alloc(struct tegra_drm *tegra, size_t size, dma_addr_t *iova);
+void tegra_drm_free(struct tegra_drm *tegra, size_t size, void *virt,
+		    dma_addr_t iova);
+
 struct tegra_dc_soc_info;
 struct tegra_output;
 
Signature · 6 lines
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#6 lore raw

PATCH 2/8 drm/tegra: Allocate BOs from lower 4G when without IOMMU

In reply to Mikko Perttunen

On 64-bit Tegras, buffer object memory allocation may return memory above 4G that units behind Host1x cannot access. Add the GFP_DMA flag to these allocation when IOMMU is not enabled to ensure units can always access BO memory.

---
 drivers/gpu/drm/tegra/gem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/tegra/gem.c +1 -1
index 19bf9cd..e36e6c5 100644
@@ -233,7 +233,7 @@ static int tegra_bo_alloc(struct drm_device *drm, struct tegra_bo *bo)
 		size_t size = bo->gem.size;
 
 		bo->vaddr = dma_alloc_wc(drm->dev, size, &bo->paddr,
-					 GFP_KERNEL | __GFP_NOWARN);
+					 GFP_KERNEL | GFP_DMA | __GFP_NOWARN);
 		if (!bo->vaddr) {
 			dev_err(drm->dev,
 				"failed to allocate buffer of size %zu\n",
Signature · 6 lines
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#12 lore raw

PATCH 3/8 drm/tegra: Add falcon helper library

In reply to a message not in the archive

From: Arto Merilainen <[email protected]>

Add a set of falcon helper routines for use by the tegradrm client drivers of the various falcon-based engines.

The falcon is a microcontroller that acts as a frontend for the rest of a particular Tegra engine. In order to properly utilize these engines, the frontend must be booted before pushing any commands.

Based on work by Andrew Chew <[email protected]>

---
 drivers/gpu/drm/tegra/Makefile |   3 +-
 drivers/gpu/drm/tegra/falcon.c | 256 +++++++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/tegra/falcon.h | 130 +++++++++++++++++++++
 3 files changed, 388 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/tegra/falcon.c
 create mode 100644 drivers/gpu/drm/tegra/falcon.h
drivers/gpu/drm/tegra/Makefile +2 -1
index 2c66a8d..93e9a4a 100644
@@ -13,6 +13,7 @@ tegra-drm-y := \
 	sor.o \
 	dpaux.o \
 	gr2d.o \
-	gr3d.o
+	gr3d.o \
+	falcon.o
 
 obj-$(CONFIG_DRM_TEGRA) += tegra-drm.o
drivers/gpu/drm/tegra/falcon.c new file +256 -0

256 lines not on this page · raw message

drivers/gpu/drm/tegra/falcon.h new file +130 -0

130 lines not on this page · raw message

Signature · 1 line
2.10.2
#16 lore raw

PATCH 4/8 drm/tegra: Add VIC support

In reply to a message not in the archive

From: Arto Merilainen <[email protected]>

This patch adds support for Video Image Compositor engine which can be used for 2d operations.

---
 drivers/gpu/drm/tegra/Makefile |   3 +-
 drivers/gpu/drm/tegra/drm.c    |   3 +
 drivers/gpu/drm/tegra/drm.h    |   1 +
 drivers/gpu/drm/tegra/vic.c    | 400 +++++++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/tegra/vic.h    |  31 ++++
 include/linux/host1x.h         |   1 +
 6 files changed, 438 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/tegra/vic.c
 create mode 100644 drivers/gpu/drm/tegra/vic.h
drivers/gpu/drm/tegra/Makefile +2 -1
index 93e9a4a..6af3a9a 100644
@@ -14,6 +14,7 @@ tegra-drm-y := \
 	dpaux.o \
 	gr2d.o \
 	gr3d.o \
-	falcon.o
+	falcon.o \
+	vic.o
 
 obj-$(CONFIG_DRM_TEGRA) += tegra-drm.o
drivers/gpu/drm/tegra/drm.c +3 -0
index ecfe7ba..707404d 100644
@@ -1151,11 +1151,13 @@ static const struct of_device_id host1x_drm_subdevs[] = {
 	{ .compatible = "nvidia,tegra124-sor", },
 	{ .compatible = "nvidia,tegra124-hdmi", },
 	{ .compatible = "nvidia,tegra124-dsi", },
+	{ .compatible = "nvidia,tegra124-vic", },
 	{ .compatible = "nvidia,tegra132-dsi", },
 	{ .compatible = "nvidia,tegra210-dc", },
 	{ .compatible = "nvidia,tegra210-dsi", },
 	{ .compatible = "nvidia,tegra210-sor", },
 	{ .compatible = "nvidia,tegra210-sor1", },
+	{ .compatible = "nvidia,tegra210-vic", },
 	{ /* sentinel */ }
 };
 
@@ -1177,6 +1179,7 @@ static struct platform_driver * const drivers[] = {
 	&tegra_sor_driver,
 	&tegra_gr2d_driver,
 	&tegra_gr3d_driver,
+	&tegra_vic_driver,
 };
 
 static int __init host1x_drm_init(void)
drivers/gpu/drm/tegra/drm.h +1 -0
index f75b505..8efaaa4 100644
@@ -292,5 +292,6 @@ extern struct platform_driver tegra_dpaux_driver;
 extern struct platform_driver tegra_sor_driver;
 extern struct platform_driver tegra_gr2d_driver;
 extern struct platform_driver tegra_gr3d_driver;
+extern struct platform_driver tegra_vic_driver;
 
 #endif /* HOST1X_DRM_H */
drivers/gpu/drm/tegra/vic.c new file +400 -0

400 lines not on this page · raw message

drivers/gpu/drm/tegra/vic.h new file +31 -0
new file mode 100644
index 0000000..2184481
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2015, NVIDIA Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef TEGRA_VIC_H
+#define TEGRA_VIC_H
+
+/* VIC methods */
+
+#define VIC_SET_APPLICATION_ID			0x00000200
+#define VIC_SET_FCE_UCODE_SIZE			0x0000071C
+#define VIC_SET_FCE_UCODE_OFFSET		0x0000072C
+
+/* VIC registers */
+
+#define NV_PVIC_MISC_PRI_VIC_CG			0x000016d0
+#define CG_IDLE_CG_DLY_CNT(val)			((val & 0x3f) << 0)
+#define CG_IDLE_CG_EN				(1 << 6)
+#define CG_WAKEUP_DLY_CNT(val)			((val & 0xf) << 16)
+
+/* Firmware offsets */
+
+#define VIC_UCODE_FCE_HEADER_OFFSET		(6*4)
+#define VIC_UCODE_FCE_DATA_OFFSET		(7*4)
+#define FCE_UCODE_SIZE_OFFSET			(2*4)
+
+#endif /* TEGRA_VIC_H */
include/linux/host1x.h +1 -0
index 1ffbf2a..3d04aa1 100644
@@ -26,6 +26,7 @@ enum host1x_class {
 	HOST1X_CLASS_HOST1X = 0x1,
 	HOST1X_CLASS_GR2D = 0x51,
 	HOST1X_CLASS_GR2D_SB = 0x52,
+	HOST1X_CLASS_VIC = 0x5D,
 	HOST1X_CLASS_GR3D = 0x60,
 };
 
Signature · 1 line
2.10.2
author maintainer
#23 lore raw

PATCH 5/8 gpu: host1x: Add IOMMU support

In reply to Mikko Perttunen

Add support for the Host1x unit to be located behind an IOMMU. This is required when gather buffers may be allocated non-contiguously in physical memory, as can be the case when TegraDRM is also using the IOMMU.

---
 drivers/gpu/host1x/cdma.c       | 65 +++++++++++++++++++++++++++++------
 drivers/gpu/host1x/cdma.h       |  4 ++-
 drivers/gpu/host1x/dev.c        | 39 +++++++++++++++++++--
 drivers/gpu/host1x/dev.h        |  5 +++
 drivers/gpu/host1x/hw/cdma_hw.c | 10 +++---
 drivers/gpu/host1x/job.c        | 76 +++++++++++++++++++++++++++++++++++------
 drivers/gpu/host1x/job.h        |  1 +
 7 files changed, 171 insertions(+), 29 deletions(-)
drivers/gpu/host1x/cdma.c +54 -11
index c5d82a8..14692446e 100644
@@ -51,9 +51,15 @@ static void host1x_pushbuffer_destroy(struct push_buffer *pb)
 	struct host1x_cdma *cdma = pb_to_cdma(pb);
 	struct host1x *host1x = cdma_to_host1x(cdma);
 
-	if (pb->phys != 0)
-		dma_free_wc(host1x->dev, pb->size_bytes + 4, pb->mapped,
-			    pb->phys);
+	if (!pb->phys)
+		return;
+
+	if (host1x->domain) {
+		iommu_unmap(host1x->domain, pb->dma, pb->alloc_size_bytes);
+		free_iova(&host1x->iova, iova_pfn(&host1x->iova, pb->dma));
+	}
+
+	dma_free_wc(host1x->dev, pb->alloc_size_bytes, pb->mapped, pb->phys);
 
 	pb->mapped = NULL;
 	pb->phys = 0;
@@ -66,28 +72,65 @@ static int host1x_pushbuffer_init(struct push_buffer *pb)
 {
 	struct host1x_cdma *cdma = pb_to_cdma(pb);
 	struct host1x *host1x = cdma_to_host1x(cdma);
+	struct iova *alloc;
+	int err;
 
 	pb->mapped = NULL;
 	pb->phys = 0;
 	pb->size_bytes = HOST1X_PUSHBUFFER_SLOTS * 8;
+	pb->alloc_size_bytes = pb->size_bytes + 4;
 
 	/* initialize buffer pointers */
 	pb->fence = pb->size_bytes - 8;
 	pb->pos = 0;
 
-	/* allocate and map pushbuffer memory */
-	pb->mapped = dma_alloc_wc(host1x->dev, pb->size_bytes + 4, &pb->phys,
-				  GFP_KERNEL);
-	if (!pb->mapped)
-		goto fail;
+	if (host1x->domain) {
+		unsigned long shift;
+
+		pb->alloc_size_bytes = iova_align(&host1x->iova,
+						  pb->alloc_size_bytes);
+
+		pb->mapped = dma_alloc_wc(host1x->dev, pb->alloc_size_bytes,
+					  &pb->phys, GFP_KERNEL);
+		if (!pb->mapped)
+			return -ENOMEM;
+
+		shift = iova_shift(&host1x->iova);
+		alloc = alloc_iova(
+			&host1x->iova, pb->alloc_size_bytes >> shift,
+			host1x->domain->geometry.aperture_end >> shift, true);
+		if (!alloc) {
+			err = -ENOMEM;
+			goto iommu_free_mem;
+		}
+
+		err = iommu_map(host1x->domain,
+				iova_dma_addr(&host1x->iova, alloc),
+				pb->phys, pb->alloc_size_bytes,
+				IOMMU_READ);
+		if (err)
+			goto iommu_free_iova;
+
+		pb->dma = iova_dma_addr(&host1x->iova, alloc);
+	} else {
+		pb->mapped = dma_alloc_wc(host1x->dev, pb->alloc_size_bytes,
+					  &pb->phys, GFP_KERNEL);
+		if (!pb->mapped)
+			return -ENOMEM;
+
+		pb->dma = pb->phys;
+	}
 
 	host1x_hw_pushbuffer_init(host1x, pb);
 
 	return 0;
 
-fail:
-	host1x_pushbuffer_destroy(pb);
-	return -ENOMEM;
+iommu_free_iova:
+	__free_iova(&host1x->iova, alloc);
+iommu_free_mem:
+	dma_free_wc(host1x->dev, pb->alloc_size_bytes, pb->mapped, pb->phys);
+
+	return err;
 }
 
 /*
drivers/gpu/host1x/cdma.h +3 -1
index 470087a..4b3645f 100644
@@ -43,10 +43,12 @@ struct host1x_job;
 
 struct push_buffer {
 	void *mapped;			/* mapped pushbuffer memory */
-	dma_addr_t phys;		/* physical address of pushbuffer */
+	dma_addr_t dma;			/* device address of pushbuffer */
+	phys_addr_t phys;		/* physical address of pushbuffer */
 	u32 fence;			/* index we've written */
 	u32 pos;			/* index to write to */
 	u32 size_bytes;
+	u32 alloc_size_bytes;
 };
 
 struct buffer_timeout {
drivers/gpu/host1x/dev.c +37 -2
index a62317a..ca4527e 100644
@@ -168,16 +168,36 @@ static int host1x_probe(struct platform_device *pdev)
 		return err;
 	}
 
+	if (iommu_present(&platform_bus_type)) {
+		struct iommu_domain_geometry *geometry;
+		unsigned long order;
+
+		host->domain = iommu_domain_alloc(&platform_bus_type);
+		if (!host->domain)
+			return -ENOMEM;
+
+		err = iommu_attach_device(host->domain, &pdev->dev);
+		if (err)
+			goto fail_free_domain;
+
+		geometry = &host->domain->geometry;
+
+		order = __ffs(host->domain->pgsize_bitmap);
+		init_iova_domain(&host->iova, 1UL << order,
+				 geometry->aperture_start >> order,
+				 geometry->aperture_end >> order);
+	}
+
 	err = host1x_channel_list_init(host);
 	if (err) {
 		dev_err(&pdev->dev, "failed to initialize channel list\n");
-		return err;
+		goto fail_detach_device;
 	}
 
 	err = clk_prepare_enable(host->clk);
 	if (err < 0) {
 		dev_err(&pdev->dev, "failed to enable clock\n");
-		return err;
+		goto fail_detach_device;
 	}
 
 	err = host1x_syncpt_init(host);
@@ -206,6 +226,15 @@ static int host1x_probe(struct platform_device *pdev)
 	host1x_syncpt_deinit(host);
 fail_unprepare_disable:
 	clk_disable_unprepare(host->clk);
+fail_detach_device:
+	if (host->domain) {
+		put_iova_domain(&host->iova);
+		iommu_detach_device(host->domain, &pdev->dev);
+	}
+fail_free_domain:
+	if (host->domain)
+		iommu_domain_free(host->domain);
+
 	return err;
 }
 
@@ -218,6 +247,12 @@ static int host1x_remove(struct platform_device *pdev)
 	host1x_syncpt_deinit(host);
 	clk_disable_unprepare(host->clk);
 
+	if (host->domain) {
+		put_iova_domain(&host->iova);
+		iommu_detach_device(host->domain, &pdev->dev);
+		iommu_domain_free(host->domain);
+	}
+
 	return 0;
 }
 
drivers/gpu/host1x/dev.h +5 -0
index 5220510..6189825 100644
@@ -19,6 +19,8 @@
 
 #include <linux/platform_device.h>
 #include <linux/device.h>
+#include <linux/iommu.h>
+#include <linux/iova.h>
 
 #include "channel.h"
 #include "syncpt.h"
@@ -108,6 +110,9 @@ struct host1x {
 	struct device *dev;
 	struct clk *clk;
 
+	struct iommu_domain *domain;
+	struct iova_domain iova;
+
 	struct mutex intr_mutex;
 	int intr_syncpt_irq;
 
drivers/gpu/host1x/hw/cdma_hw.c +5 -5
index 659c1bb..b8c0348 100644
@@ -55,7 +55,7 @@ static void cdma_timeout_cpu_incr(struct host1x_cdma *cdma, u32 getptr,
 		*(p++) = HOST1X_OPCODE_NOP;
 		*(p++) = HOST1X_OPCODE_NOP;
 		dev_dbg(host1x->dev, "%s: NOP at %pad+%#x\n", __func__,
-			&pb->phys, getptr);
+			&pb->dma, getptr);
 		getptr = (getptr + 8) & (pb->size_bytes - 1);
 	}
 
@@ -78,9 +78,9 @@ static void cdma_start(struct host1x_cdma *cdma)
 			 HOST1X_CHANNEL_DMACTRL);
 
 	/* set base, put and end pointer */
-	host1x_ch_writel(ch, cdma->push_buffer.phys, HOST1X_CHANNEL_DMASTART);
+	host1x_ch_writel(ch, cdma->push_buffer.dma, HOST1X_CHANNEL_DMASTART);
 	host1x_ch_writel(ch, cdma->push_buffer.pos, HOST1X_CHANNEL_DMAPUT);
-	host1x_ch_writel(ch, cdma->push_buffer.phys +
+	host1x_ch_writel(ch, cdma->push_buffer.dma +
 			 cdma->push_buffer.size_bytes + 4,
 			 HOST1X_CHANNEL_DMAEND);
 
@@ -115,8 +115,8 @@ static void cdma_timeout_restart(struct host1x_cdma *cdma, u32 getptr)
 			 HOST1X_CHANNEL_DMACTRL);
 
 	/* set base, end pointer (all of memory) */
-	host1x_ch_writel(ch, cdma->push_buffer.phys, HOST1X_CHANNEL_DMASTART);
-	host1x_ch_writel(ch, cdma->push_buffer.phys +
+	host1x_ch_writel(ch, cdma->push_buffer.dma, HOST1X_CHANNEL_DMASTART);
+	host1x_ch_writel(ch, cdma->push_buffer.dma +
 			 cdma->push_buffer.size_bytes,
 			 HOST1X_CHANNEL_DMAEND);
 
drivers/gpu/host1x/job.c +66 -10

130 lines not on this page · raw message

drivers/gpu/host1x/job.h +1 -0
index 8b3c15d..878239c4 100644
@@ -44,6 +44,7 @@ struct host1x_waitchk {
 struct host1x_job_unpin_data {
 	struct host1x_bo *bo;
 	struct sg_table *sgt;
+	size_t size;
 };
 
 /*
Signature · 6 lines
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#27 lore raw

PATCH 6/8 dt-bindings: Add bindings for the Tegra VIC

In reply to Mikko Perttunen

The VIC (Video Image Compositor) is a Host1x client unit that can do various 2D composition and transform operations.

---
 .../bindings/display/tegra/nvidia,tegra20-host1x.txt        | 13 +++++++++++++
 1 file changed, 13 insertions(+)
Documentation/devicetree/bindings/display/tegra/nvidia,tegra20-host1x.txt +13 -0
index 0fad7ed..74e1e8a 100644
@@ -249,6 +249,19 @@ of the following host1x client modules:
   See ../pinctrl/nvidia,tegra124-dpaux-padctl.txt for information
   regarding the DPAUX pad controller bindings.
 
+- vic: Video Image Compositor
+  - compatible : "nvidia,tegra<chip>-vic"
+  - reg: Physical base address and length of the controller's registers.
+  - interrupts: The interrupt outputs from the controller.
+  - clocks: Must contain an entry for each entry in clock-names.
+    See ../clocks/clock-bindings.txt for details.
+  - clock-names: Must include the following entries:
+    - vic: clock input for the VIC hardware
+  - resets: Must contain an entry for each entry in reset-names.
+    See ../reset/reset.txt for details.
+  - reset-names: Must include the following entries:
+    - vic
+
 Example:
 
 / {
Signature · 6 lines
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#20 lore raw

PATCH 7/8 arm64: tegra: Enable VIC on T210

In reply to a message not in the archive

Enable the VIC (Video Image Compositor) host1x
unit on Tegra210 systems.

---
 arch/arm64/boot/dts/nvidia/tegra210.dtsi | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
arch/arm64/boot/dts/nvidia/tegra210.dtsi +16 -1
index 46045fe..e12390f 100644
@@ -122,7 +122,14 @@
 		vic@54340000 {
 			compatible = "nvidia,tegra210-vic";
 			reg = <0x0 0x54340000 0x0 0x00040000>;
-			status = "disabled";
+			interrupts = <GIC_SPI 72 IRQ_TYPE_LEVEL_HIGH>;
+			clocks = <&tegra_car TEGRA210_CLK_VIC03>;
+			clock-names = "vic";
+			resets = <&tegra_car 178>;
+			reset-names = "vic";
+
+			iommus = <&mc TEGRA_SWGROUP_VIC>;
+			power-domains = <&pd_vic>;
 		};
 
 		nvjpg@54380000 {
@@ -692,6 +699,14 @@
 				resets = <&tegra_car TEGRA210_CLK_XUSB_HOST>;
 				#power-domain-cells = <0>;
 			};
+
+			pd_vic: vic {
+				clocks = <&tegra_car TEGRA210_CLK_VIC03>;
+				clock-names = "vic";
+				resets = <&tegra_car TEGRA210_CLK_VIC03>;
+				reset-names = "vic";
+				#power-domain-cells = <0>;
+			};
 		};
 	};
 
Signature · 1 line
2.10.2
#21 lore raw

PATCH 8/8 arm64: tegra: Enable IOMMU for Host1x on Tegra210

In reply to a message not in the archive

The Host1x driver now supports operation behind an IOMMU, so add its IOMMU domain to the device tree.

---
 arch/arm64/boot/dts/nvidia/tegra210.dtsi | 2 ++
 1 file changed, 2 insertions(+)
arch/arm64/boot/dts/nvidia/tegra210.dtsi +2 -0
index e12390f..8d229c7 100644
@@ -26,6 +26,8 @@
 
 		ranges = <0x0 0x54000000 0x0 0x54000000 0x0 0x01000000>;
 
+		iommus = <&mc TEGRA_SWGROUP_HC>;
+
 		dpaux1: dpaux@54040000 {
 			compatible = "nvidia,tegra210-dpaux";
 			reg = <0x0 0x54040000 0x0 0x00040000>;
Signature · 1 line
2.10.2
#22 lore raw
In reply to a message not in the archive

Bump

Quoted 69 lines · Mikko Perttunen

This series adds IOMMU support to Host1x and TegraDRM and adds support for the VIC host1x client so that host1x can be tested on modern Tegra platforms. It depends on the previous fix series. The whole thing (modulo patch order) is available as a git repository at
git://github.com/cyndis/linux.git; branch vic-v1.

IO memory management is organized such that there are two domains: the host1x domain and the tegradrm domain. The host1x domain is used by the host1x engine and contains the host1x CDMA and pushbuffers for submitted jobs.

The tegradrm domain is shared by all host1x units and contains GEM objects and memory allocated by the separate tegra_drm_alloc function. This function is currently used to allocate space for firmware blobs in the tegradrm domain.

A userspace test case for VIC can be found at
https://github.com/cyndis/drm/tree/work/tegra.
The testcase is in tests/tegra and is called submit_vic. The in-kernel firewall is not implemented for VIC; therefore, IOMMU must be enabled for the test to pass.

Tested with Jetson TX1 (T210). Probably works also with Jetson TK1 (T124). Note that due to hardware changes the testcase also needs to be changed to run properly on T124.

Thanks,
Mikko.

Arto Merilainen (2):
drm/tegra: Add falcon helper library
drm/tegra: Add VIC support

Mikko Perttunen (6):
drm/tegra: Add Tegra DRM allocation API drm/tegra: Allocate BOs from lower 4G when without IOMMU
gpu: host1x: Add IOMMU support
dt-bindings: Add bindings for the Tegra VIC
arm64: tegra: Enable VIC on T210
arm64: tegra: Enable IOMMU for Host1x on Tegra210

 .../display/tegra/nvidia,tegra20-host1x.txt        |  13 +
 arch/arm64/boot/dts/nvidia/tegra210.dtsi           |  19 +-
 drivers/gpu/drm/tegra/Makefile                     |   4 +-
 drivers/gpu/drm/tegra/drm.c                        | 101 +++++-
 drivers/gpu/drm/tegra/drm.h                        |   8 +
 drivers/gpu/drm/tegra/falcon.c                     | 256 +++++++++++++
 drivers/gpu/drm/tegra/falcon.h                     | 130 +++++++
 drivers/gpu/drm/tegra/gem.c                        |   2 +-
 drivers/gpu/drm/tegra/vic.c                        | 400 +++++++++++++++++++++
 drivers/gpu/drm/tegra/vic.h                        |  31 ++
 drivers/gpu/host1x/cdma.c                          |  65 +++-
 drivers/gpu/host1x/cdma.h                          |   4 +-
 drivers/gpu/host1x/dev.c                           |  39 +-
 drivers/gpu/host1x/dev.h                           |   5 +
 drivers/gpu/host1x/hw/cdma_hw.c                    |  10 +-
 drivers/gpu/host1x/job.c                           |  76 +++-
 drivers/gpu/host1x/job.h                           |   1 +
 include/linux/host1x.h                             |   1 +
 18 files changed, 1128 insertions(+), 37 deletions(-)
 create mode 100644 drivers/gpu/drm/tegra/falcon.c
 create mode 100644 drivers/gpu/drm/tegra/falcon.h
 create mode 100644 drivers/gpu/drm/tegra/vic.c
 create mode 100644 drivers/gpu/drm/tegra/vic.h
#3 lore raw

Re: [PATCH 1/8] drm/tegra: Add Tegra DRM allocation API

In reply to a message not in the archive
Quoted 11 lines · Mikko Perttunen

Add a new IO virtual memory allocation API to allow clients to allocate non-GEM memory in the Tegra DRM IOMMU domain. This is required e.g. for loading client firmware when clients are attached to the IOMMU domain.

The allocator allocates contiguous physical pages that are then mapped contiguously to the IOMMU domain using the iova_domain library provided by the kernel. Contiguous physical pages are used so that the same allocator works also when IOMMU support is disabled and therefore devices access physical memory directly.

Quoted 5 lines
---
 drivers/gpu/drm/tegra/drm.c | 98 ++++++++++++++++++++++++++++++++++++++++++---
 drivers/gpu/drm/tegra/drm.h |  7 ++++
 2 files changed, 100 insertions(+), 5 deletions(-)

This looks mostly good, just a few comments below...

drivers/gpu/drm/tegra/drm.c line 3

Quoted 9 lines
drivers/gpu/drm/tegra/drm.c +1 -1
index b8be3ee..ecfe7ba 100644
@@ -1,12 +1,13 @@
 /*
  * Copyright (C) 2012 Avionic Design GmbH
- * Copyright (C) 2012-2013 NVIDIA CORPORATION.  All rights reserved.
+ * Copyright (C) 2012-2015 NVIDIA CORPORATION.  All rights reserved.

It's almost 2017 now, I think this is out of date. =)

Quoted 11 lines
drivers/gpu/drm/tegra/drm.c +1 -0
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
  */
 
+#include <linux/bitops.h>
 #include <linux/host1x.h>
 #include <linux/iommu.h>
 

drivers/gpu/drm/tegra/drm.c line 27

Quoted 4 lines
drivers/gpu/drm/tegra/drm.c +1 -0
@@ -23,6 +24,8 @@
 #define DRIVER_MINOR 0
 #define DRIVER_PATCHLEVEL 0
 
+#define IOVA_AREA_SZ (1024 * 1024 * 64) /* 64 MiB */

SZ_64M?

drivers/gpu/drm/tegra/drm.c static int tegra_drm_load(struct drm_device *drm, unsigned long flags) · line 134

Quoted 11 lines
drivers/gpu/drm/tegra/drm.c +3 -1
+
 struct tegra_drm_file {
 	struct list_head contexts;
 };
@@ -127,7 +130,8 @@ static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
 
 	if (iommu_present(&platform_bus_type)) {
 		struct iommu_domain_geometry *geometry;
-		u64 start, end;
+		unsigned long order;
+		u64 iova_start, start, end;

Can we be a little more explicit and keep an additional iova_end to simplify the code a little?

drivers/gpu/drm/tegra/drm.c static int tegra_drm_load(struct drm_device *drm, unsigned long flags) · line 150

Quoted 12 lines
drivers/gpu/drm/tegra/drm.c +6 -0
 		tegra->domain = iommu_domain_alloc(&platform_bus_type);
 		if (!tegra->domain) {
@@ -138,10 +142,17 @@ static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
 		geometry = &tegra->domain->geometry;
 		start = geometry->aperture_start;
 		end = geometry->aperture_end;
+		iova_start = end - IOVA_AREA_SZ + 1;
+
+		order = __ffs(tegra->domain->pgsize_bitmap);
+		init_iova_domain(&tegra->iova, 1UL << order,
+				 iova_start >> order,
+				 end >> order);

I hadn't seen this IOVA domain thing and it looks rather handy.

drivers/gpu/drm/tegra/drm.c static int tegra_drm_load(struct drm_device *drm, unsigned long flags) · line 155

Quoted 8 lines
drivers/gpu/drm/tegra/drm.c +4 -3
-		DRM_DEBUG_DRIVER("IOMMU aperture initialized (%#llx-%#llx)\n",
-				 start, end);
-		drm_mm_init(&tegra->mm, start, end - start + 1);
+		drm_mm_init(&tegra->mm, start, iova_start - start);
+
+		DRM_DEBUG("IOMMU apertures initialized (GEM: %#llx-%#llx, IOVA: %#llx-%#llx)\n",
+			  start, iova_start-1, iova_start, end);

Might be worth splitting this into two, or perhaps even three lines:

	IOMMU apertures:
	  GEM: %#llx-%#llx
	  IOVA: %#llx-%#llx

Maybe also find a better name for IOVA, since GEM will be using I/O virtual addresses as well. Perhaps just name it "carveout" or something like that?

Quoted 20 lines
drivers/gpu/drm/tegra/drm.c +2 -0
 	}
 
 	mutex_init(&tegra->clients_lock);
@@ -208,6 +219,7 @@ static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
 	if (tegra->domain) {
 		iommu_domain_free(tegra->domain);
 		drm_mm_takedown(&tegra->mm);
+		put_iova_domain(&tegra->iova);
 	}
 free:
 	kfree(tegra);
@@ -232,6 +244,7 @@ static int tegra_drm_unload(struct drm_device *drm)
 	if (tegra->domain) {
 		iommu_domain_free(tegra->domain);
 		drm_mm_takedown(&tegra->mm);
+		put_iova_domain(&tegra->iova);
 	}
 
 	kfree(tegra);

drivers/gpu/drm/tegra/drm.c int tegra_drm_unregister_client(struct tegra_drm *tegra, · line 992

Quoted 5 lines
drivers/gpu/drm/tegra/drm.c +2 -0
@@ -975,6 +988,81 @@ int tegra_drm_unregister_client(struct tegra_drm *tegra,
 	return 0;
 }
 
+void *tegra_drm_alloc(struct tegra_drm *tegra, size_t size,
+			      dma_addr_t *iova)

Maybe name the iova parameter phys? iova implies that it's always translated, whereas according to the code it can be physical, too.

Quoted 10 lines
drivers/gpu/drm/tegra/drm.c +10 -0
+{
+	struct iova *alloc;
+	unsigned long shift;
+	void *virt;
+	gfp_t gfp;
+	int err;
+
+	if (tegra->domain)
+		size = iova_align(&tegra->iova, size);
+	else

drivers/gpu/drm/tegra/drm.c int tegra_drm_unregister_client(struct tegra_drm *tegra, · line 1012

Quoted 10 lines
drivers/gpu/drm/tegra/drm.c +10 -0
+		size = PAGE_ALIGN(size);
+
+	gfp = GFP_KERNEL | __GFP_ZERO;
+	if (!tegra->domain) {
+		/*
+		 * Tegra210 has 64-bit physical addresses but units only support
+		 * 32-bit addresses, so if we don't have an IOMMU to do
+		 * translation, force allocations to be in the 32-bit range.
+		 */
+		gfp |= GFP_DMA;

Technically we do support Tegra132 and that already has 64-bit physical addresses, so perhaps include that in the comment, or make it more generic:

/*
* Many units only support 32-bit addresses, even on 64-bit SoCs.
* If there is no IOMMU to translate into a 32-bit IO virtual address
* address space, force allocations to be in the lower 32-bit range. */

drivers/gpu/drm/tegra/drm.c int tegra_drm_unregister_client(struct tegra_drm *tegra, · line 1017

Quoted 5 lines
drivers/gpu/drm/tegra/drm.c +5 -0
+	}
+
+	virt = (void *)__get_free_pages(gfp, get_order(size));
+	if (!virt)
+		return NULL;

Perhaps we want to return an ERR_PTR()-encoded error code here so we can differentiate between out-of-memory and out-of-virtual-address-space conditions in the caller? Or perhaps other conditions as well.

Also, is __get_free_pages() the right API here? I thought that it always returned contiguous memory, which, in the presence of an IOMMU, will be completely unnecessary.

drivers/gpu/drm/tegra/drm.c int tegra_drm_unregister_client(struct tegra_drm *tegra, · line 1030

Quoted 13 lines
drivers/gpu/drm/tegra/drm.c +13 -0
+
+	if (!tegra->domain) {
+		/*
+		 * If IOMMU is disabled, devices address physical memory
+		 * directly.
+		 */
+		*iova = virt_to_phys(virt);
+		return virt;
+	}
+
+	shift = iova_shift(&tegra->iova);
+	alloc = alloc_iova(&tegra->iova, size >> shift,
+			   tegra->domain->geometry.aperture_end >> shift, true);

This is fairly cumbersome to use. Maybe a drm_mm would be easier in this case? If not, could we at least store the upper limit of the carveout so that we don't have to use the really long expression above?

Thierry

#8 lore raw

Re: [PATCH 2/8] drm/tegra: Allocate BOs from lower 4G when without IOMMU

In reply to a message not in the archive
Quoted 5 lines · Mikko Perttunen

On 64-bit Tegras, buffer object memory allocation may return memory above 4G that units behind Host1x cannot access. Add the GFP_DMA flag to these allocation when IOMMU is not enabled to ensure units can always access BO memory.

I don't think that's entirely true. The display controller can address more than 32 bits. It's perhaps slightly different from other units that are behind host1x, but maybe we need a special case here to allow frame- buffers to reside in higher memory?

Thierry

#13 lore raw

Re: [PATCH 3/8] drm/tegra: Add falcon helper library

In reply to Mikko Perttunen
Quoted 50 lines · Mikko Perttunen

From: Arto Merilainen <[email protected]>

Add a set of falcon helper routines for use by the tegradrm client drivers of the various falcon-based engines.

The falcon is a microcontroller that acts as a frontend for the rest of a particular Tegra engine. In order to properly utilize these engines, the frontend must be booted before pushing any commands.

Based on work by Andrew Chew <[email protected]>

---
 drivers/gpu/drm/tegra/Makefile |   3 +-
 drivers/gpu/drm/tegra/falcon.c | 256 +++++++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/tegra/falcon.h | 130 +++++++++++++++++++++
 3 files changed, 388 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/tegra/falcon.c
 create mode 100644 drivers/gpu/drm/tegra/falcon.h
drivers/gpu/drm/tegra/Makefile +2 -1
index 2c66a8d..93e9a4a 100644
@@ -13,6 +13,7 @@ tegra-drm-y := \
 	sor.o \
 	dpaux.o \
 	gr2d.o \
-	gr3d.o
+	gr3d.o \
+	falcon.o
 
 obj-$(CONFIG_DRM_TEGRA) += tegra-drm.o
drivers/gpu/drm/tegra/falcon.c new file +9 -0
new file mode 100644
index 0000000..180b2fd
@@ -0,0 +1,256 @@
+/*
+ * Copyright (c) 2015, NVIDIA Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/platform_device.h>

drivers/gpu/drm/tegra/falcon.c line 19

Quoted 10 lines
drivers/gpu/drm/tegra/falcon.c new file +10 -0
+#include <linux/dma-mapping.h>
+#include <linux/firmware.h>
+#include <linux/pci_ids.h>
+#include <linux/iopoll.h>
+
+#include "falcon.h"
+#include "drm.h"
+
+#define FALCON_IDLE_TIMEOUT_US		100000
+#define FALCON_IDLE_CHECK_PERIOD_US	10

Seems a little overkill to have these here, given that their only used twice. They're also used in two cases where we may end up using different periods and timeouts eventually, so I'd just stick them into falcon_wait_idle() and falcon_dma_wait_idle() directly and omit these definitions. That's kind of a nitpick, so feel free to keep it as-is.

Quoted 7 lines
drivers/gpu/drm/tegra/falcon.c new file +7 -0
+
+enum falcon_memory {
+	FALCON_MEMORY_IMEM,
+	FALCON_MEMORY_DATA,
+};
+
+static void falcon_writel(struct falcon *falcon, u32 value, u32 offset)

drivers/gpu/drm/tegra/falcon.c line 36

Quoted 10 lines
drivers/gpu/drm/tegra/falcon.c new file +10 -0
+{
+	writel(value, falcon->regs + offset);
+}
+
+int falcon_wait_idle(struct falcon *falcon)
+{
+	u32 idlestate;
+
+	return readl_poll_timeout(falcon->regs + FALCON_IDLESTATE, idlestate,
+				  (!idlestate),

That's an odd way to write this. Why not just "idlestate == 0"? That's much easier to read and more explicit.

drivers/gpu/drm/tegra/falcon.c line 43

Quoted 7 lines
drivers/gpu/drm/tegra/falcon.c new file +7 -0
+				  FALCON_IDLE_CHECK_PERIOD_US,
+				  FALCON_IDLE_TIMEOUT_US);
+}
+
+static int falcon_dma_wait_idle(struct falcon *falcon)
+{
+	u32 dmatrfcmd;

u32 value? Naming it after a register suggests that it may be an offset rather than the register value.

Quoted 27 lines
drivers/gpu/drm/tegra/falcon.c new file +27 -0
+
+	return readl_poll_timeout(falcon->regs + FALCON_DMATRFCMD, dmatrfcmd,
+				  (dmatrfcmd & FALCON_DMATRFCMD_IDLE),
+				  FALCON_IDLE_CHECK_PERIOD_US,
+				  FALCON_IDLE_TIMEOUT_US);
+}
+
+static int falcon_copy_chunk(struct falcon *falcon,
+			     phys_addr_t base,
+			     unsigned long offset,
+			     enum falcon_memory target)
+{
+	u32 cmd = FALCON_DMATRFCMD_SIZE_256B;
+
+	if (target == FALCON_MEMORY_IMEM)
+		cmd |= FALCON_DMATRFCMD_IMEM;
+
+	falcon_writel(falcon, offset, FALCON_DMATRFMOFFS);
+	falcon_writel(falcon, base, FALCON_DMATRFFBOFFS);
+	falcon_writel(falcon, cmd, FALCON_DMATRFCMD);
+
+	return falcon_dma_wait_idle(falcon);
+}
+
+static void falcon_copy_firmware_image(struct falcon *falcon,
+				       const struct firmware *firmware)
+{

drivers/gpu/drm/tegra/falcon.c line 80

Quoted 10 lines
drivers/gpu/drm/tegra/falcon.c new file +10 -0
+	u32 *firmware_vaddr = falcon->firmware.vaddr;
+	size_t i;
+
+	/* copy the whole thing taking into account endianness */
+	for (i = 0; i < firmware->size / sizeof(u32); i++)
+		firmware_vaddr[i] = le32_to_cpu(((u32 *)firmware->data)[i]);
+
+	/* ensure that caches are flushed and falcon can see the firmware */
+	dma_sync_single_for_device(falcon->dev, virt_to_phys(firmware_vaddr),
+				   falcon->firmware.size, DMA_TO_DEVICE);

My understanding is that this is an error and the DMA API will complain about it if debugging is enabled. I think you need to call one of the dma_map_*() functions on the memory before you can do a dma_sync_*().

drivers/gpu/drm/tegra/falcon.c line 87

Quoted 7 lines
drivers/gpu/drm/tegra/falcon.c new file +7 -0
+}
+
+static int falcon_parse_firmware_image(struct falcon *falcon)
+{
+	struct falcon_firmware_bin_header_v1 *bin_header =
+		(void *)falcon->firmware.vaddr;
+	struct falcon_firmware_os_header_v1 *os_header;

Can we make these shorter? Perhaps by leaving out the _header suffix? It'd be nice if we could avoid splitting these across multiple lines.

Quoted 8 lines
drivers/gpu/drm/tegra/falcon.c new file +8 -0
+
+	/* endian problems would show up right here */
+	if (bin_header->bin_magic != PCI_VENDOR_ID_NVIDIA) {
+		dev_err(falcon->dev, "failed to get firmware magic");
+		return -EINVAL;
+	}
+
+	/* currently only version 1 is supported */

drivers/gpu/drm/tegra/falcon.c line 105

Quoted 10 lines
drivers/gpu/drm/tegra/falcon.c new file +10 -0
+	if (bin_header->bin_ver != 1) {
+		dev_err(falcon->dev, "unsupported firmware version");
+		return -EINVAL;
+	}
+
+	/* check that the firmware size is consistent */
+	if (bin_header->bin_size > falcon->firmware.size) {
+		dev_err(falcon->dev, "firmware image size inconsistency");
+		return -EINVAL;
+	}

These messages are missing newlines at the end.

drivers/gpu/drm/tegra/falcon.c line 108

Quoted 3 lines
drivers/gpu/drm/tegra/falcon.c new file +3 -0
+
+	os_header = (falcon->firmware.vaddr +
+		 bin_header->os_bin_header_offset);

I think if we shorten the variable names a little this will also fit on a single line. There's also no need for the parentheses.

drivers/gpu/drm/tegra/falcon.c line 115

Quoted 7 lines
drivers/gpu/drm/tegra/falcon.c new file +7 -0
+
+	falcon->firmware.bin_data.size = bin_header->os_bin_size;
+	falcon->firmware.bin_data.offset = bin_header->os_bin_data_offset;
+	falcon->firmware.code.offset = os_header->os_code_offset;
+	falcon->firmware.code.size   = os_header->os_code_size;
+	falcon->firmware.data.offset = os_header->os_data_offset;
+	falcon->firmware.data.size   = os_header->os_data_size;

Can you remove the extra padding before =, please? It's inconsistent with the other assignments.

drivers/gpu/drm/tegra/falcon.c line 120

Quoted 5 lines
drivers/gpu/drm/tegra/falcon.c new file +5 -0
+
+	return 0;
+}
+
+int falcon_read_firmware(struct falcon *falcon, const char *firmware_name)

The firmware_ prefix in firmware_name is redundant and can be dropped, in my opinion.

drivers/gpu/drm/tegra/falcon.c line 125

Quoted 5 lines
drivers/gpu/drm/tegra/falcon.c new file +5 -0
+{
+	const struct firmware *firmware;
+	int err;
+
+	if (falcon->firmware.valid)

Do we really need the .valid field? It seems like we should be able to derive it from the value of one of the other fields.

drivers/gpu/drm/tegra/falcon.c line 131

Quoted 6 lines
drivers/gpu/drm/tegra/falcon.c new file +6 -0
+		return 0;
+
+	err = request_firmware(&firmware, firmware_name, falcon->dev);
+	if (err < 0) {
+		dev_err(falcon->dev, "failed to get firmware\n");
+		return err;

It'd be nice to show the user what error occurred. Maybe also show the name of the firmware that couldn't be loaded.

drivers/gpu/drm/tegra/falcon.c line 138

Quoted 7 lines
drivers/gpu/drm/tegra/falcon.c new file +7 -0
+	}
+
+	falcon->firmware.size = firmware->size;
+
+	/* allocate iova space for the firmware */
+	falcon->firmware.vaddr = falcon->ops->alloc(falcon, firmware->size,
+						 &falcon->firmware.paddr);

The arguments aren't properly aligned.

Quoted 47 lines
drivers/gpu/drm/tegra/falcon.c new file +47 -0
+	if (!falcon->firmware.vaddr) {
+		dev_err(falcon->dev, "dma memory mapping failed");
+		err = -ENOMEM;
+		goto err_alloc_firmware;
+	}
+
+	/* copy firmware image into local area. this also ensures endianness */
+	falcon_copy_firmware_image(falcon, firmware);
+
+	/* parse the image data */
+	err = falcon_parse_firmware_image(falcon);
+	if (err < 0) {
+		dev_err(falcon->dev, "failed to parse firmware image\n");
+		goto err_setup_firmware_image;
+	}
+
+	falcon->firmware.valid = true;
+
+	release_firmware(firmware);
+
+	return 0;
+
+err_setup_firmware_image:
+	falcon->ops->free(falcon, falcon->firmware.size,
+			  falcon->firmware.paddr, falcon->firmware.vaddr);
+err_alloc_firmware:
+	release_firmware(firmware);
+
+	return err;
+}
+
+int falcon_init(struct falcon *falcon)
+{
+	/* check mandatory ops */
+	if (!falcon->ops || !falcon->ops->alloc || !falcon->ops->free)
+		return -EINVAL;
+
+	falcon->firmware.valid = false;
+
+	return 0;
+}
+
+void falcon_exit(struct falcon *falcon)
+{
+	if (!falcon->firmware.valid)
+		return;
+

drivers/gpu/drm/tegra/falcon.c line 195

Quoted 10 lines
drivers/gpu/drm/tegra/falcon.c new file +10 -0
+	falcon->ops->free(falcon, falcon->firmware.size,
+			  falcon->firmware.paddr,
+			  falcon->firmware.vaddr);
+	falcon->firmware.valid = false;
+}
+
+int falcon_boot(struct falcon *falcon)
+{
+	unsigned long offset;
+	int err = 0;

I don'n think it's necessary to initialize err here.

drivers/gpu/drm/tegra/falcon.c line 202

Quoted 7 lines
drivers/gpu/drm/tegra/falcon.c new file +7 -0
+
+	if (!falcon->firmware.valid)
+		return -EINVAL;
+
+	falcon_writel(falcon, 0, FALCON_DMACTL);
+
+	/* setup the address of the binary data. Falcon can access it later */

If you write sentences with a full stop, please use a capital first letter. In this case I think you can just drop the full stop:

	/* setup the address of the binary data so Falcon can access it later */
Quoted 35 lines
drivers/gpu/drm/tegra/falcon.c new file +35 -0
+	falcon_writel(falcon, (falcon->firmware.paddr +
+			       falcon->firmware.bin_data.offset) >> 8,
+		      FALCON_DMATRFBASE);
+
+	/* copy the data segment into Falcon internal memory */
+	for (offset = 0; offset < falcon->firmware.data.size; offset += 256)
+		falcon_copy_chunk(falcon,
+				  falcon->firmware.data.offset + offset,
+				  offset, FALCON_MEMORY_DATA);
+
+	/* copy the first code segment into Falcon internal memory */
+	falcon_copy_chunk(falcon, falcon->firmware.code.offset,
+			  0, FALCON_MEMORY_IMEM);
+
+	/* setup falcon interrupts */
+	falcon_writel(falcon, FALCON_IRQMSET_EXT(0xff) |
+			      FALCON_IRQMSET_SWGEN1 |
+			      FALCON_IRQMSET_SWGEN0 |
+			      FALCON_IRQMSET_EXTERR |
+			      FALCON_IRQMSET_HALT |
+			      FALCON_IRQMSET_WDTMR,
+		      FALCON_IRQMSET);
+	falcon_writel(falcon, FALCON_IRQDEST_EXT(0xff) |
+			      FALCON_IRQDEST_SWGEN1 |
+			      FALCON_IRQDEST_SWGEN0 |
+			      FALCON_IRQDEST_EXTERR |
+			      FALCON_IRQDEST_HALT,
+		      FALCON_IRQDEST);
+
+	/* enable interface */
+	falcon_writel(falcon, FALCON_ITFEN_MTHDEN |
+			      FALCON_ITFEN_CTXEN,
+		      FALCON_ITFEN);
+
+	/* boot falcon */

drivers/gpu/drm/tegra/falcon.c line 247

Quoted 10 lines
drivers/gpu/drm/tegra/falcon.c new file +10 -0
+	falcon_writel(falcon, 0x00000000, FALCON_BOOTVEC);
+	falcon_writel(falcon, FALCON_CPUCTL_STARTCPU, FALCON_CPUCTL);
+
+	err = falcon_wait_idle(falcon);
+	if (err < 0) {
+		dev_err(falcon->dev, "falcon boot failed due to timeout");
+		return err;
+	}
+
+	dev_info(falcon->dev, "falcon booted");

No need to brag here, it's supposed to be successful. Let the user know if it isn't. If you really want this, make sure it's output at debug level, not info. Also I think we need to agree on whether these are named Falcon or falcon and then use it consistently. I'm leaning towards the former. Also, there are a few messages with missing newlines.

Quoted 66 lines
drivers/gpu/drm/tegra/falcon.c new file +9 -0
+
+	return 0;
+}
+
+void falcon_execute_method(struct falcon *falcon, u32 method, u32 data)
+{
+	falcon_writel(falcon, method >> 2, FALCON_UCLASS_METHOD_OFFSET);
+	falcon_writel(falcon, data, FALCON_UCLASS_METHOD_DATA);
+}
drivers/gpu/drm/tegra/falcon.h new file +51 -0
new file mode 100644
index 0000000..56284b9
@@ -0,0 +1,130 @@
+/*
+ * Copyright (c) 2015, NVIDIA Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef _FALCON_H_
+#define _FALCON_H_
+
+#include <linux/types.h>
+
+#define FALCON_UCLASS_METHOD_OFFSET		0x00000040
+
+#define FALCON_UCLASS_METHOD_DATA		0x00000044
+
+#define FALCON_IRQMSET				0x00001010
+#define FALCON_IRQMSET_WDTMR			(1 << 1)
+#define FALCON_IRQMSET_HALT			(1 << 4)
+#define FALCON_IRQMSET_EXTERR			(1 << 5)
+#define FALCON_IRQMSET_SWGEN0			(1 << 6)
+#define FALCON_IRQMSET_SWGEN1			(1 << 7)
+#define FALCON_IRQMSET_EXT(v)			(((v) & 0xff) << 8)
+
+#define FALCON_IRQDEST				0x0000101c
+#define FALCON_IRQDEST_HALT			(1 << 4)
+#define FALCON_IRQDEST_EXTERR			(1 << 5)
+#define FALCON_IRQDEST_SWGEN0			(1 << 6)
+#define FALCON_IRQDEST_SWGEN1			(1 << 7)
+#define FALCON_IRQDEST_EXT(v)			(((v) & 0xff) << 8)
+
+#define FALCON_ITFEN				0x00001048
+#define FALCON_ITFEN_CTXEN			(1 << 0)
+#define FALCON_ITFEN_MTHDEN			(1 << 1)
+
+#define FALCON_IDLESTATE			0x0000104c
+
+#define FALCON_CPUCTL				0x00001100
+#define FALCON_CPUCTL_STARTCPU			(1 << 1)
+
+#define FALCON_BOOTVEC				0x00001104
+
+#define FALCON_DMACTL				0x0000110c
+#define FALCON_DMACTL_DMEM_SCRUBBING		(1 << 1)
+#define FALCON_DMACTL_IMEM_SCRUBBING		(1 << 2)
+
+#define FALCON_DMATRFBASE			0x00001110
+
+#define FALCON_DMATRFMOFFS			0x00001114
+

drivers/gpu/drm/tegra/falcon.h line 61

Quoted 10 lines
drivers/gpu/drm/tegra/falcon.h new file +10 -0
+#define FALCON_DMATRFCMD			0x00001118
+#define FALCON_DMATRFCMD_IDLE			(1 << 1)
+#define FALCON_DMATRFCMD_IMEM			(1 << 4)
+#define FALCON_DMATRFCMD_SIZE_256B		(6 << 8)
+
+#define FALCON_DMATRFFBOFFS			0x0000111c
+
+struct falcon_firmware_bin_header_v1 {
+	u32 bin_magic;		/* 0x10de */
+	u32 bin_ver;		/* cya, versioning of bin format (1) */

Not sure everyone understands what cya means, here. Perhaps just drop it?

Quoted 9 lines
drivers/gpu/drm/tegra/falcon.h new file +9 -0
+	u32 bin_size;		/* entire image size including this header */
+	u32 os_bin_header_offset;
+	u32 os_bin_data_offset;
+	u32 os_bin_size;
+};
+
+struct falcon_firmware_os_app_v1 {
+	u32 offset;
+	u32 size;

drivers/gpu/drm/tegra/falcon.h line 80

Quoted 10 lines
drivers/gpu/drm/tegra/falcon.h new file +10 -0
+};
+
+struct falcon_firmware_os_header_v1 {
+	u32 os_code_offset;
+	u32 os_code_size;
+	u32 os_data_offset;
+	u32 os_data_size;
+	u32 num_apps;
+	struct falcon_firmware_os_app_v1 *app_code;
+	struct falcon_firmware_os_app_v1 *app_data;

The above two seem to be completel unused. Should we drop them?

drivers/gpu/drm/tegra/falcon.h line 83

Quoted 3 lines
drivers/gpu/drm/tegra/falcon.h new file +3 -0
+	u32 *os_ovl_offset;
+	u32 *os_ovl_size;
+};

Can we in general sanitize the names of these a little? It's redundent to add a os_ prefix in a structure that contains an _os_ infix.

Thierry

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#17 lore raw

Re: [PATCH 4/8] drm/tegra: Add VIC support

In reply to Mikko Perttunen
Quoted 93 lines · Mikko Perttunen

From: Arto Merilainen <[email protected]>

This patch adds support for Video Image Compositor engine which can be used for 2d operations.

---
 drivers/gpu/drm/tegra/Makefile |   3 +-
 drivers/gpu/drm/tegra/drm.c    |   3 +
 drivers/gpu/drm/tegra/drm.h    |   1 +
 drivers/gpu/drm/tegra/vic.c    | 400 +++++++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/tegra/vic.h    |  31 ++++
 include/linux/host1x.h         |   1 +
 6 files changed, 438 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/tegra/vic.c
 create mode 100644 drivers/gpu/drm/tegra/vic.h
drivers/gpu/drm/tegra/Makefile +2 -1
index 93e9a4a..6af3a9a 100644
@@ -14,6 +14,7 @@ tegra-drm-y := \
 	dpaux.o \
 	gr2d.o \
 	gr3d.o \
-	falcon.o
+	falcon.o \
+	vic.o
 
 obj-$(CONFIG_DRM_TEGRA) += tegra-drm.o
drivers/gpu/drm/tegra/drm.c +3 -0
index ecfe7ba..707404d 100644
@@ -1151,11 +1151,13 @@ static const struct of_device_id host1x_drm_subdevs[] = {
 	{ .compatible = "nvidia,tegra124-sor", },
 	{ .compatible = "nvidia,tegra124-hdmi", },
 	{ .compatible = "nvidia,tegra124-dsi", },
+	{ .compatible = "nvidia,tegra124-vic", },
 	{ .compatible = "nvidia,tegra132-dsi", },
 	{ .compatible = "nvidia,tegra210-dc", },
 	{ .compatible = "nvidia,tegra210-dsi", },
 	{ .compatible = "nvidia,tegra210-sor", },
 	{ .compatible = "nvidia,tegra210-sor1", },
+	{ .compatible = "nvidia,tegra210-vic", },
 	{ /* sentinel */ }
 };
 
@@ -1177,6 +1179,7 @@ static struct platform_driver * const drivers[] = {
 	&tegra_sor_driver,
 	&tegra_gr2d_driver,
 	&tegra_gr3d_driver,
+	&tegra_vic_driver,
 };
 
 static int __init host1x_drm_init(void)
drivers/gpu/drm/tegra/drm.h +1 -0
index f75b505..8efaaa4 100644
@@ -292,5 +292,6 @@ extern struct platform_driver tegra_dpaux_driver;
 extern struct platform_driver tegra_sor_driver;
 extern struct platform_driver tegra_gr2d_driver;
 extern struct platform_driver tegra_gr3d_driver;
+extern struct platform_driver tegra_vic_driver;
 
 #endif /* HOST1X_DRM_H */
drivers/gpu/drm/tegra/vic.c new file +18 -0
new file mode 100644
index 0000000..9eda5e5
@@ -0,0 +1,400 @@
+/*
+ * Copyright (c) 2015, NVIDIA Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/clk.h>
+#include <linux/host1x.h>
+#include <linux/iommu.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/reset.h>

drivers/gpu/drm/tegra/vic.c line 28

Quoted 10 lines
drivers/gpu/drm/tegra/vic.c new file +10 -0
+
+#include <soc/tegra/pmc.h>
+
+#include "drm.h"
+#include "falcon.h"
+#include "vic.h"
+
+struct vic_config {
+	/* Firmware name */
+	const char *ucode_name;

Maybe just "firmware"? That way you could remove the comment.

Quoted 129 lines
drivers/gpu/drm/tegra/vic.c new file +129 -0
+};
+
+struct vic {
+	struct falcon falcon;
+	bool booted;
+
+	void __iomem *regs;
+	struct tegra_drm_client client;
+	struct host1x_channel *channel;
+	struct iommu_domain *domain;
+	struct device *dev;
+	struct clk *clk;
+
+	/* Platform configuration */
+	const struct vic_config *config;
+};
+
+static inline struct vic *to_vic(struct tegra_drm_client *client)
+{
+	return container_of(client, struct vic, client);
+}
+
+static void vic_writel(struct vic *vic, u32 value, unsigned int offset)
+{
+	writel(value, vic->regs + offset);
+}
+
+static int vic_runtime_resume(struct device *dev)
+{
+	struct vic *vic = dev_get_drvdata(dev);
+
+	return clk_prepare_enable(vic->clk);
+}
+
+static int vic_runtime_suspend(struct device *dev)
+{
+	struct vic *vic = dev_get_drvdata(dev);
+
+	clk_disable_unprepare(vic->clk);
+
+	vic->booted = false;
+
+	return 0;
+}
+
+static int vic_boot(struct vic *vic)
+{
+	u32 fce_ucode_size, fce_bin_data_offset;
+	void *hdr;
+	int err = 0;
+
+	if (vic->booted)
+		return 0;
+
+	if (!vic->falcon.firmware.valid) {
+		err = falcon_read_firmware(&vic->falcon,
+					   vic->config->ucode_name);
+		if (err < 0)
+			return err;
+	}
+
+	/* setup clockgating registers */
+	vic_writel(vic, CG_IDLE_CG_DLY_CNT(4) |
+			CG_IDLE_CG_EN |
+			CG_WAKEUP_DLY_CNT(4),
+		   NV_PVIC_MISC_PRI_VIC_CG);
+
+	err = falcon_boot(&vic->falcon);
+	if (err < 0)
+		return err;
+
+	hdr = vic->falcon.firmware.vaddr;
+	fce_bin_data_offset = *(u32 *)(hdr + VIC_UCODE_FCE_DATA_OFFSET);
+	hdr = vic->falcon.firmware.vaddr +
+		*(u32 *)(hdr + VIC_UCODE_FCE_HEADER_OFFSET);
+	fce_ucode_size = *(u32 *)(hdr + FCE_UCODE_SIZE_OFFSET);
+
+	falcon_execute_method(&vic->falcon, VIC_SET_APPLICATION_ID, 1);
+	falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_SIZE,
+			      fce_ucode_size);
+	falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_OFFSET,
+			      (vic->falcon.firmware.paddr + fce_bin_data_offset)
+				>> 8);
+
+	err = falcon_wait_idle(&vic->falcon);
+	if (err < 0) {
+		dev_err(vic->dev,
+			"failed to set application ID and FCE base\n");
+		return err;
+	}
+
+	vic->booted = true;
+
+	return 0;
+}
+
+static void *vic_falcon_alloc(struct falcon *falcon, size_t size,
+			       dma_addr_t *iova)
+{
+	struct tegra_drm *tegra = falcon->data;
+
+	return tegra_drm_alloc(tegra, size, iova);
+}
+
+static void vic_falcon_free(struct falcon *falcon, size_t size,
+			    dma_addr_t iova, void *va)
+{
+	struct tegra_drm *tegra = falcon->data;
+
+	return tegra_drm_free(tegra, size, va, iova);
+}
+
+static const struct falcon_ops vic_falcon_ops = {
+	.alloc = vic_falcon_alloc,
+	.free = vic_falcon_free
+};
+
+static int vic_init(struct host1x_client *client)
+{
+	struct tegra_drm_client *drm = host1x_to_drm_client(client);
+	struct drm_device *dev = dev_get_drvdata(client->parent);
+	struct tegra_drm *tegra = dev->dev_private;
+	struct vic *vic = to_vic(drm);
+	int err;
+
+	if (tegra->domain) {
+		err = iommu_attach_device(tegra->domain, vic->dev);
+		if (err < 0) {
+			dev_err(vic->dev, "failed to attach to domain: %d\n",

drivers/gpu/drm/tegra/vic.c line 167

Quoted 10 lines
drivers/gpu/drm/tegra/vic.c new file +10 -0
+				err);
+			return err;
+		}
+
+		vic->domain = tegra->domain;
+	}
+
+	vic->falcon.dev = vic->dev;
+	vic->falcon.regs = vic->regs;
+	vic->falcon.data = tegra;

Why do we need this? We can get at struct vic * from struct falcon * via an upcast, and once we have struct vic, we have struct tegra_drm_client.

But now I realize that apparently there's no way to get from that to the struct drm_device. I guess we could go via vic->client.base.parent and a call to dev_get_drvdata(), but that's rather far. It's still a little less confusing than stashing the struct tegra_drm * in it, since it is not at all driver-specific data.

drivers/gpu/drm/tegra/vic.c line 169

Quoted 2 lines
drivers/gpu/drm/tegra/vic.c new file +2 -0
+	vic->falcon.ops = &vic_falcon_ops;
+	err = falcon_init(&vic->falcon);

Could use a blank line between the above.

Quoted 69 lines
drivers/gpu/drm/tegra/vic.c new file +69 -0
+	if (err < 0)
+		goto detach_device;
+
+	vic->channel = host1x_channel_request(client->dev);
+	if (!vic->channel) {
+		err = -ENOMEM;
+		goto exit_falcon;
+	}
+
+	client->syncpts[0] = host1x_syncpt_request(client->dev, 0);
+	if (!client->syncpts[0]) {
+		err = -ENOMEM;
+		goto free_channel;
+	}
+
+	err = tegra_drm_register_client(tegra, drm);
+	if (err < 0)
+		goto free_syncpt;
+
+	return 0;
+
+free_syncpt:
+	host1x_syncpt_free(client->syncpts[0]);
+free_channel:
+	host1x_channel_free(vic->channel);
+exit_falcon:
+	falcon_exit(&vic->falcon);
+detach_device:
+	if (tegra->domain)
+		iommu_detach_device(tegra->domain, vic->dev);
+
+	return err;
+}
+
+static int vic_exit(struct host1x_client *client)
+{
+	struct tegra_drm_client *drm = host1x_to_drm_client(client);
+	struct drm_device *dev = dev_get_drvdata(client->parent);
+	struct tegra_drm *tegra = dev->dev_private;
+	struct vic *vic = to_vic(drm);
+	int err;
+
+	err = tegra_drm_unregister_client(tegra, drm);
+	if (err < 0)
+		return err;
+
+	host1x_syncpt_free(client->syncpts[0]);
+	host1x_channel_free(vic->channel);
+
+	falcon_exit(&vic->falcon);
+
+	if (vic->domain) {
+		iommu_detach_device(vic->domain, vic->dev);
+		vic->domain = NULL;
+	}
+
+	return 0;
+}
+
+static const struct host1x_client_ops vic_client_ops = {
+	.init = vic_init,
+	.exit = vic_exit,
+};
+
+static int vic_open_channel(struct tegra_drm_client *client,
+			    struct tegra_drm_context *context)
+{
+	struct vic *vic = to_vic(client);
+	int err;

drivers/gpu/drm/tegra/vic.c line 248

Quoted 10 lines
drivers/gpu/drm/tegra/vic.c new file +10 -0
+
+	err = pm_runtime_get_sync(vic->dev);
+	if (err < 0)
+		return err;
+
+	/*
+	 * Try to boot the Falcon microcontroller. Booting is deferred until
+	 * here because the firmware might not yet be available during system
+	 * boot, for example if it's on remote storage.
+	 */

That sounds like a hack. Typically you're supposed to have the firmware on the same medium as the module that requests it. That is, if the driver is built-in, then you need to make the firmware available as built-in as well or stash it into an initrd. If the driver is built as a loadable module and is on the root filesystem, that's where the firmware should be as well. There's also the possibility to put the loadable module into an initrd, in which case that's where the firmware should go as well.

That said, I think it makes sense to defer the actual booting until this point. Loading the firmware seems unappropriate to me. It really should be done in ->probe().

Quoted 98 lines
drivers/gpu/drm/tegra/vic.c new file +98 -0
+	err = vic_boot(vic);
+	if (err < 0) {
+		pm_runtime_put(vic->dev);
+		return err;
+	}
+
+	context->channel = host1x_channel_get(vic->channel);
+	if (!context->channel) {
+		pm_runtime_put(vic->dev);
+		return -ENOMEM;
+	}
+
+	return 0;
+}
+
+static void vic_close_channel(struct tegra_drm_context *context)
+{
+	struct vic *vic = to_vic(context->client);
+
+	host1x_channel_put(context->channel);
+
+	pm_runtime_put(vic->dev);
+}
+
+static const struct tegra_drm_client_ops vic_ops = {
+	.open_channel = vic_open_channel,
+	.close_channel = vic_close_channel,
+	.submit = tegra_drm_submit,
+};
+
+static const struct vic_config vic_t124_config = {
+	.ucode_name = "nvidia/tegra124/vic03_ucode.bin",
+};
+
+static const struct vic_config vic_t210_config = {
+	.ucode_name = "nvidia/tegra210/vic04_ucode.bin",
+};
+
+static const struct of_device_id vic_match[] = {
+	{ .compatible = "nvidia,tegra124-vic", .data = &vic_t124_config },
+	{ .compatible = "nvidia,tegra210-vic", .data = &vic_t210_config },
+	{ },
+};
+
+static int vic_probe(struct platform_device *pdev)
+{
+	struct vic_config *vic_config = NULL;
+	struct device *dev = &pdev->dev;
+	struct host1x_syncpt **syncpts;
+	struct resource *regs;
+	const struct of_device_id *match;
+	struct vic *vic;
+	int err;
+
+	match = of_match_device(vic_match, dev);
+	vic_config = (struct vic_config *)match->data;
+
+	vic = devm_kzalloc(dev, sizeof(*vic), GFP_KERNEL);
+	if (!vic)
+		return -ENOMEM;
+
+	syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
+	if (!syncpts)
+		return -ENOMEM;
+
+	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!regs) {
+		dev_err(&pdev->dev, "failed to get registers\n");
+		return -ENXIO;
+	}
+
+	vic->regs = devm_ioremap_resource(dev, regs);
+	if (IS_ERR(vic->regs))
+		return PTR_ERR(vic->regs);
+
+	vic->clk = devm_clk_get(dev, NULL);
+	if (IS_ERR(vic->clk)) {
+		dev_err(&pdev->dev, "failed to get clock\n");
+		return PTR_ERR(vic->clk);
+	}
+
+	platform_set_drvdata(pdev, vic);
+
+	INIT_LIST_HEAD(&vic->client.base.list);
+	vic->client.base.ops = &vic_client_ops;
+	vic->client.base.dev = dev;
+	vic->client.base.class = HOST1X_CLASS_VIC;
+	vic->client.base.syncpts = syncpts;
+	vic->client.base.num_syncpts = 1;
+	vic->dev = dev;
+	vic->config = vic_config;
+
+	INIT_LIST_HEAD(&vic->client.list);
+	vic->client.ops = &vic_ops;
+
+	err = host1x_client_register(&vic->client.base);
+	if (err < 0) {
+		dev_err(dev, "failed to register host1x client: %d\n", err);

drivers/gpu/drm/tegra/vic.c line 356

Quoted 10 lines
drivers/gpu/drm/tegra/vic.c new file +10 -0
+		platform_set_drvdata(pdev, NULL);
+		return err;
+	}
+
+	pm_runtime_enable(&pdev->dev);
+	if (!pm_runtime_enabled(&pdev->dev)) {
+		err = vic_runtime_resume(&pdev->dev);
+		if (err < 0)
+			goto unregister_client;
+	}

That's a slightly ugly construct, but I can't think of an easy way to get rid of it (other than to depend on PM, which actually might be the right thing to do here, it's already selected by ARCH_TEGRA on 64-bit ARM). I can't think of a scenario where we'd want to run without runtime PM.

drivers/gpu/drm/tegra/vic.c line 358

Quoted 2 lines
drivers/gpu/drm/tegra/vic.c new file +2 -0
+
+	dev_info(&pdev->dev, "initialized");

Again, no need for this.

Thierry

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#24 lore raw

Re: [PATCH 5/8] gpu: host1x: Add IOMMU support

In reply to Mikko Perttunen
Quoted 65 lines · Mikko Perttunen

Add support for the Host1x unit to be located behind an IOMMU. This is required when gather buffers may be allocated non-contiguously in physical memory, as can be the case when TegraDRM is also using the IOMMU.

---
 drivers/gpu/host1x/cdma.c       | 65 +++++++++++++++++++++++++++++------
 drivers/gpu/host1x/cdma.h       |  4 ++-
 drivers/gpu/host1x/dev.c        | 39 +++++++++++++++++++--
 drivers/gpu/host1x/dev.h        |  5 +++
 drivers/gpu/host1x/hw/cdma_hw.c | 10 +++---
 drivers/gpu/host1x/job.c        | 76 +++++++++++++++++++++++++++++++++++------
 drivers/gpu/host1x/job.h        |  1 +
 7 files changed, 171 insertions(+), 29 deletions(-)
drivers/gpu/host1x/cdma.c +17 -8
index c5d82a8..14692446e 100644
@@ -51,9 +51,15 @@ static void host1x_pushbuffer_destroy(struct push_buffer *pb)
 	struct host1x_cdma *cdma = pb_to_cdma(pb);
 	struct host1x *host1x = cdma_to_host1x(cdma);
 
-	if (pb->phys != 0)
-		dma_free_wc(host1x->dev, pb->size_bytes + 4, pb->mapped,
-			    pb->phys);
+	if (!pb->phys)
+		return;
+
+	if (host1x->domain) {
+		iommu_unmap(host1x->domain, pb->dma, pb->alloc_size_bytes);
+		free_iova(&host1x->iova, iova_pfn(&host1x->iova, pb->dma));
+	}
+
+	dma_free_wc(host1x->dev, pb->alloc_size_bytes, pb->mapped, pb->phys);
 
 	pb->mapped = NULL;
 	pb->phys = 0;
@@ -66,28 +72,65 @@ static int host1x_pushbuffer_init(struct push_buffer *pb)
 {
 	struct host1x_cdma *cdma = pb_to_cdma(pb);
 	struct host1x *host1x = cdma_to_host1x(cdma);
+	struct iova *alloc;
+	int err;
 
 	pb->mapped = NULL;
 	pb->phys = 0;
 	pb->size_bytes = HOST1X_PUSHBUFFER_SLOTS * 8;
+	pb->alloc_size_bytes = pb->size_bytes + 4;
 
 	/* initialize buffer pointers */
 	pb->fence = pb->size_bytes - 8;
 	pb->pos = 0;
 
-	/* allocate and map pushbuffer memory */
-	pb->mapped = dma_alloc_wc(host1x->dev, pb->size_bytes + 4, &pb->phys,
-				  GFP_KERNEL);
-	if (!pb->mapped)
-		goto fail;
+	if (host1x->domain) {
+		unsigned long shift;
+
+		pb->alloc_size_bytes = iova_align(&host1x->iova,
+						  pb->alloc_size_bytes);

drivers/gpu/host1x/cdma.c static int host1x_pushbuffer_init(struct push_buffer *pb) · line 101

Quoted 10 lines
drivers/gpu/host1x/cdma.c +10 -0
+
+		pb->mapped = dma_alloc_wc(host1x->dev, pb->alloc_size_bytes,
+					  &pb->phys, GFP_KERNEL);
+		if (!pb->mapped)
+			return -ENOMEM;
+
+		shift = iova_shift(&host1x->iova);
+		alloc = alloc_iova(
+			&host1x->iova, pb->alloc_size_bytes >> shift,
+			host1x->domain->geometry.aperture_end >> shift, true);

Let's precompute some of these values to make this expression more compact. For example pb->alloc_size_bytes is used a couple of times, so it could be stored temporarily in a local variable with a nice and short name such as "size".

If you store the aperture end, or limit_pfn, as I commented in an earlier patch, then this also becomes much more compact.

drivers/gpu/host1x/cdma.c static int host1x_pushbuffer_init(struct push_buffer *pb) · line 110

Quoted 9 lines
drivers/gpu/host1x/cdma.c +9 -0
+		if (!alloc) {
+			err = -ENOMEM;
+			goto iommu_free_mem;
+		}
+
+		err = iommu_map(host1x->domain,
+				iova_dma_addr(&host1x->iova, alloc),
+				pb->phys, pb->alloc_size_bytes,
+				IOMMU_READ);

Same here.

Quoted 34 lines
drivers/gpu/host1x/cdma.c +18 -3
+		if (err)
+			goto iommu_free_iova;
+
+		pb->dma = iova_dma_addr(&host1x->iova, alloc);
+	} else {
+		pb->mapped = dma_alloc_wc(host1x->dev, pb->alloc_size_bytes,
+					  &pb->phys, GFP_KERNEL);
+		if (!pb->mapped)
+			return -ENOMEM;
+
+		pb->dma = pb->phys;
+	}
 
 	host1x_hw_pushbuffer_init(host1x, pb);
 
 	return 0;
 
-fail:
-	host1x_pushbuffer_destroy(pb);
-	return -ENOMEM;
+iommu_free_iova:
+	__free_iova(&host1x->iova, alloc);
+iommu_free_mem:
+	dma_free_wc(host1x->dev, pb->alloc_size_bytes, pb->mapped, pb->phys);
+
+	return err;
 }
 
 /*
drivers/gpu/host1x/cdma.h +0 -0
index 470087a..4b3645f 100644

drivers/gpu/host1x/cdma.h struct host1x_job; · line 51

Quoted 10 lines
drivers/gpu/host1x/cdma.h +3 -1
@@ -43,10 +43,12 @@ struct host1x_job;
 
 struct push_buffer {
 	void *mapped;			/* mapped pushbuffer memory */
-	dma_addr_t phys;		/* physical address of pushbuffer */
+	dma_addr_t dma;			/* device address of pushbuffer */
+	phys_addr_t phys;		/* physical address of pushbuffer */
 	u32 fence;			/* index we've written */
 	u32 pos;			/* index to write to */
 	u32 size_bytes;
+	u32 alloc_size_bytes;

Maybe just "alloc_size"?

Quoted 139 lines
drivers/gpu/host1x/cdma.h +0 -0
 };
 
 struct buffer_timeout {
drivers/gpu/host1x/dev.c +37 -2
index a62317a..ca4527e 100644
@@ -168,16 +168,36 @@ static int host1x_probe(struct platform_device *pdev)
 		return err;
 	}
 
+	if (iommu_present(&platform_bus_type)) {
+		struct iommu_domain_geometry *geometry;
+		unsigned long order;
+
+		host->domain = iommu_domain_alloc(&platform_bus_type);
+		if (!host->domain)
+			return -ENOMEM;
+
+		err = iommu_attach_device(host->domain, &pdev->dev);
+		if (err)
+			goto fail_free_domain;
+
+		geometry = &host->domain->geometry;
+
+		order = __ffs(host->domain->pgsize_bitmap);
+		init_iova_domain(&host->iova, 1UL << order,
+				 geometry->aperture_start >> order,
+				 geometry->aperture_end >> order);
+	}
+
 	err = host1x_channel_list_init(host);
 	if (err) {
 		dev_err(&pdev->dev, "failed to initialize channel list\n");
-		return err;
+		goto fail_detach_device;
 	}
 
 	err = clk_prepare_enable(host->clk);
 	if (err < 0) {
 		dev_err(&pdev->dev, "failed to enable clock\n");
-		return err;
+		goto fail_detach_device;
 	}
 
 	err = host1x_syncpt_init(host);
@@ -206,6 +226,15 @@ static int host1x_probe(struct platform_device *pdev)
 	host1x_syncpt_deinit(host);
 fail_unprepare_disable:
 	clk_disable_unprepare(host->clk);
+fail_detach_device:
+	if (host->domain) {
+		put_iova_domain(&host->iova);
+		iommu_detach_device(host->domain, &pdev->dev);
+	}
+fail_free_domain:
+	if (host->domain)
+		iommu_domain_free(host->domain);
+
 	return err;
 }
 
@@ -218,6 +247,12 @@ static int host1x_remove(struct platform_device *pdev)
 	host1x_syncpt_deinit(host);
 	clk_disable_unprepare(host->clk);
 
+	if (host->domain) {
+		put_iova_domain(&host->iova);
+		iommu_detach_device(host->domain, &pdev->dev);
+		iommu_domain_free(host->domain);
+	}
+
 	return 0;
 }
 
drivers/gpu/host1x/dev.h +5 -0
index 5220510..6189825 100644
@@ -19,6 +19,8 @@
 
 #include <linux/platform_device.h>
 #include <linux/device.h>
+#include <linux/iommu.h>
+#include <linux/iova.h>
 
 #include "channel.h"
 #include "syncpt.h"
@@ -108,6 +110,9 @@ struct host1x {
 	struct device *dev;
 	struct clk *clk;
 
+	struct iommu_domain *domain;
+	struct iova_domain iova;
+
 	struct mutex intr_mutex;
 	int intr_syncpt_irq;
 
drivers/gpu/host1x/hw/cdma_hw.c +5 -5
index 659c1bb..b8c0348 100644
@@ -55,7 +55,7 @@ static void cdma_timeout_cpu_incr(struct host1x_cdma *cdma, u32 getptr,
 		*(p++) = HOST1X_OPCODE_NOP;
 		*(p++) = HOST1X_OPCODE_NOP;
 		dev_dbg(host1x->dev, "%s: NOP at %pad+%#x\n", __func__,
-			&pb->phys, getptr);
+			&pb->dma, getptr);
 		getptr = (getptr + 8) & (pb->size_bytes - 1);
 	}
 
@@ -78,9 +78,9 @@ static void cdma_start(struct host1x_cdma *cdma)
 			 HOST1X_CHANNEL_DMACTRL);
 
 	/* set base, put and end pointer */
-	host1x_ch_writel(ch, cdma->push_buffer.phys, HOST1X_CHANNEL_DMASTART);
+	host1x_ch_writel(ch, cdma->push_buffer.dma, HOST1X_CHANNEL_DMASTART);
 	host1x_ch_writel(ch, cdma->push_buffer.pos, HOST1X_CHANNEL_DMAPUT);
-	host1x_ch_writel(ch, cdma->push_buffer.phys +
+	host1x_ch_writel(ch, cdma->push_buffer.dma +
 			 cdma->push_buffer.size_bytes + 4,
 			 HOST1X_CHANNEL_DMAEND);
 
@@ -115,8 +115,8 @@ static void cdma_timeout_restart(struct host1x_cdma *cdma, u32 getptr)
 			 HOST1X_CHANNEL_DMACTRL);
 
 	/* set base, end pointer (all of memory) */
-	host1x_ch_writel(ch, cdma->push_buffer.phys, HOST1X_CHANNEL_DMASTART);
-	host1x_ch_writel(ch, cdma->push_buffer.phys +
+	host1x_ch_writel(ch, cdma->push_buffer.dma, HOST1X_CHANNEL_DMASTART);
+	host1x_ch_writel(ch, cdma->push_buffer.dma +
 			 cdma->push_buffer.size_bytes,
 			 HOST1X_CHANNEL_DMAEND);
 
drivers/gpu/host1x/job.c +0 -0
index a91b7c4..222d930 100644

drivers/gpu/host1x/job.c static int do_waitchks(struct host1x_job *job, struct host1x *host, · line 180

Quoted 8 lines
drivers/gpu/host1x/job.c +2 -1
@@ -174,8 +174,9 @@ static int do_waitchks(struct host1x_job *job, struct host1x *host,
 	return 0;
 }
 
-static unsigned int pin_job(struct host1x_job *job)
+static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
 {
+	int err;
 	unsigned int i;

Maybe use an inverse christmas tree for variables as in other parts of this driver? That is, add the new int err belowe the existing unsined int i.

Quoted 22 lines
drivers/gpu/host1x/job.c +6 -2
 	job->num_unpins = 0;
@@ -186,12 +187,16 @@ static unsigned int pin_job(struct host1x_job *job)
 		dma_addr_t phys_addr;
 
 		reloc->target.bo = host1x_bo_get(reloc->target.bo);
-		if (!reloc->target.bo)
+		if (!reloc->target.bo) {
+			err = -EINVAL;
 			goto unpin;
+		}
 
 		phys_addr = host1x_bo_pin(reloc->target.bo, &sgt);
-		if (!phys_addr)
+		if (!phys_addr) {
+			err = -EINVAL;
 			goto unpin;
+		}
 
 		job->addr_phys[job->num_unpins] = phys_addr;
 		job->unpins[job->num_unpins].bo = reloc->target.bo;

drivers/gpu/host1x/job.c static unsigned int pin_job(struct host1x_job *job) · line 216

Quoted 8 lines
drivers/gpu/host1x/job.c +5 -0
@@ -204,25 +209,68 @@ static unsigned int pin_job(struct host1x_job *job)
 		struct sg_table *sgt;
 		dma_addr_t phys_addr;
 
+		size_t gather_size = 0;
+		struct scatterlist *sg;
+		struct iova *alloc;
+		unsigned long shift;
+		int j;

There should be no blank line between blocks of variable declarations. Also, make j an unsigned int.

Quoted 9 lines
drivers/gpu/host1x/job.c +4 -1
+
 		g->bo = host1x_bo_get(g->bo);
-		if (!g->bo)
+		if (!g->bo) {
+			err = -EINVAL;
 			goto unpin;
+		}
 
 		phys_addr = host1x_bo_pin(g->bo, &sgt);

drivers/gpu/host1x/job.c static unsigned int pin_job(struct host1x_job *job) · line 233

Quoted 10 lines
drivers/gpu/host1x/job.c +8 -1
-		if (!phys_addr)
+		if (!phys_addr) {
+			err = -EINVAL;
 			goto unpin;
+		}
+
+		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) {
+			for_each_sg(sgt->sgl, sg, sgt->nents, j) {
+				gather_size += sg->length;
+			}

No need for curly brackets here.

drivers/gpu/host1x/job.c static unsigned int pin_job(struct host1x_job *job) · line 240

Quoted 7 lines
drivers/gpu/host1x/job.c +7 -0
+			gather_size = iova_align(&host->iova, gather_size);
+
+			shift = iova_shift(&host->iova);
+			alloc = alloc_iova(
+				&host->iova, gather_size >> shift,
+				host->domain->geometry.aperture_end >> shift,
+				true);

This could be made a little more compact as well.

Thierry

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#28 lore raw
In reply to Mikko Perttunen
Quoted 25 lines · Mikko Perttunen

This series adds IOMMU support to Host1x and TegraDRM and adds support for the VIC host1x client so that host1x can be tested on modern Tegra platforms. It depends on the previous fix series. The whole thing (modulo patch order) is available as a git repository at
git://github.com/cyndis/linux.git; branch vic-v1.

IO memory management is organized such that there are two domains: the host1x domain and the tegradrm domain. The host1x domain is used by the host1x engine and contains the host1x CDMA and pushbuffers for submitted jobs.

The tegradrm domain is shared by all host1x units and contains GEM objects and memory allocated by the separate tegra_drm_alloc function. This function is currently used to allocate space for firmware blobs in the tegradrm domain.

A userspace test case for VIC can be found at
https://github.com/cyndis/drm/tree/work/tegra.
The testcase is in tests/tegra and is called submit_vic. The in-kernel firewall is not implemented for VIC; therefore, IOMMU must be enabled for the test to pass.

Quoted 4 lines

Tested with Jetson TX1 (T210). Probably works also with Jetson TK1 (T124). Note that due to hardware changes the testcase also needs to be changed to run properly on T124.

What's the scope of the changes required for Tegra124? If we add the kernel bits for Tegra124 we should also have a userspace test program to exercise it.

Thanks,
Thierry

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#30 lore raw
In reply to a message not in the archive
Quoted 26 lines · Thierry Reding
Quoted 25 lines · Mikko Perttunen

This series adds IOMMU support to Host1x and TegraDRM and adds support for the VIC host1x client so that host1x can be tested on modern Tegra platforms. It depends on the previous fix series. The whole thing (modulo patch order) is available as a git repository at
git://github.com/cyndis/linux.git; branch vic-v1.

IO memory management is organized such that there are two domains: the host1x domain and the tegradrm domain. The host1x domain is used by the host1x engine and contains the host1x CDMA and pushbuffers for submitted jobs.

The tegradrm domain is shared by all host1x units and contains GEM objects and memory allocated by the separate tegra_drm_alloc function. This function is currently used to allocate space for firmware blobs in the tegradrm domain.

A userspace test case for VIC can be found at
https://github.com/cyndis/drm/tree/work/tegra.
The testcase is in tests/tegra and is called submit_vic. The in-kernel firewall is not implemented for VIC; therefore, IOMMU must be enabled for the test to pass.

Quoted 8 lines
Quoted 4 lines

Tested with Jetson TX1 (T210). Probably works also with Jetson TK1 (T124). Note that due to hardware changes the testcase also needs to be changed to run properly on T124.

What's the scope of the changes required for Tegra124? If we add the kernel bits for Tegra124 we should also have a userspace test program to exercise it.

There should be no change needed to the kernel driver for T124. For the userspace test, removing the one or two topmost commits in my repo will put the testcase to a form working on T124 - I was lazy and just modified the original file. Should be reasonably easy to have the test case support both chips, though.

Quoted 4 lines

Thanks,
Thierry

Cheers,
Mikko.

#31 lore raw
In reply to a message not in the archive
Quoted 26 lines · Thierry Reding
Quoted 25 lines · Mikko Perttunen

This series adds IOMMU support to Host1x and TegraDRM and adds support for the VIC host1x client so that host1x can be tested on modern Tegra platforms. It depends on the previous fix series. The whole thing (modulo patch order) is available as a git repository at
git://github.com/cyndis/linux.git; branch vic-v1.

IO memory management is organized such that there are two domains: the host1x domain and the tegradrm domain. The host1x domain is used by the host1x engine and contains the host1x CDMA and pushbuffers for submitted jobs.

The tegradrm domain is shared by all host1x units and contains GEM objects and memory allocated by the separate tegra_drm_alloc function. This function is currently used to allocate space for firmware blobs in the tegradrm domain.

A userspace test case for VIC can be found at
https://github.com/cyndis/drm/tree/work/tegra.
The testcase is in tests/tegra and is called submit_vic. The in-kernel firewall is not implemented for VIC; therefore, IOMMU must be enabled for the test to pass.

Quoted 8 lines
Quoted 4 lines

Tested with Jetson TX1 (T210). Probably works also with Jetson TK1 (T124). Note that due to hardware changes the testcase also needs to be changed to run properly on T124.

What's the scope of the changes required for Tegra124? If we add the kernel bits for Tegra124 we should also have a userspace test program to exercise it.

Please note that just a test program is not really enough to add more uabi (and VIC support here maybe classifies at that, no idea how this all works). Aka does mesa run, and for the vic stuff, does some libva/libvdpau open source implementation run on this?
-Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#33 lore raw
In reply to a message not in the archive
Quoted 36 lines · Daniel Vetter
Quoted 26 lines · Thierry Reding
Quoted 25 lines · Mikko Perttunen

This series adds IOMMU support to Host1x and TegraDRM and adds support for the VIC host1x client so that host1x can be tested on modern Tegra platforms. It depends on the previous fix series. The whole thing (modulo patch order) is available as a git repository at
git://github.com/cyndis/linux.git; branch vic-v1.

IO memory management is organized such that there are two domains: the host1x domain and the tegradrm domain. The host1x domain is used by the host1x engine and contains the host1x CDMA and pushbuffers for submitted jobs.

The tegradrm domain is shared by all host1x units and contains GEM objects and memory allocated by the separate tegra_drm_alloc function. This function is currently used to allocate space for firmware blobs in the tegradrm domain.

A userspace test case for VIC can be found at
https://github.com/cyndis/drm/tree/work/tegra.
The testcase is in tests/tegra and is called submit_vic. The in-kernel firewall is not implemented for VIC; therefore, IOMMU must be enabled for the test to pass.

Quoted 8 lines
Quoted 4 lines

Tested with Jetson TX1 (T210). Probably works also with Jetson TK1 (T124). Note that due to hardware changes the testcase also needs to be changed to run properly on T124.

What's the scope of the changes required for Tegra124? If we add the kernel bits for Tegra124 we should also have a userspace test program to exercise it.

Quoted 6 lines

Please note that just a test program is not really enough to add more uabi (and VIC support here maybe classifies at that, no idea how this all works). Aka does mesa run, and for the vic stuff, does some libva/libvdpau open source implementation run on this?
-Daniel

VIC is just an image compositor so if we wanted to use it alone we would probably need to implement X11 2D acceleration (at least nothing else comes to mind), which isn't that useful nowadays, since nouveau works too. I'm internally working on NVDEC (the video decoder) at the moment - if/when I'm allowed to upstream that, I'll provide more extensive userspace for decoding video. This would most likely be a libva or libvdpau backend; I have already prototyped these. This will also most likely use VIC for image format conversions.

As for the "more UABI" question; the UABI used here already exists in the kernel and libdrm. The same UABI can be used for any Host1x unit without modifications; we have previously exercised it on older Tegra revisions with different units. The only thing that changes is a new enumeration value is added for the new unit.

Is that reasonable?

Thanks,
Mikko.

#5 lore raw

Re: [PATCH 1/8] drm/tegra: Add Tegra DRM allocation API

In reply to a message not in the archive
Quoted 42 lines · Thierry Reding
Quoted 11 lines · Mikko Perttunen

Add a new IO virtual memory allocation API to allow clients to allocate non-GEM memory in the Tegra DRM IOMMU domain. This is required e.g. for loading client firmware when clients are attached to the IOMMU domain.

The allocator allocates contiguous physical pages that are then mapped contiguously to the IOMMU domain using the iova_domain library provided by the kernel. Contiguous physical pages are used so that the same allocator works also when IOMMU support is disabled and therefore devices access physical memory directly.

Quoted 5 lines
---
 drivers/gpu/drm/tegra/drm.c | 98 ++++++++++++++++++++++++++++++++++++++++++---
 drivers/gpu/drm/tegra/drm.h |  7 ++++
 2 files changed, 100 insertions(+), 5 deletions(-)

This looks mostly good, just a few comments below...

drivers/gpu/drm/tegra/drm.c line 3

Quoted 9 lines
drivers/gpu/drm/tegra/drm.c +1 -1
index b8be3ee..ecfe7ba 100644
@@ -1,12 +1,13 @@
 /*
  * Copyright (C) 2012 Avionic Design GmbH
- * Copyright (C) 2012-2013 NVIDIA CORPORATION.  All rights reserved.
+ * Copyright (C) 2012-2015 NVIDIA CORPORATION.  All rights reserved.

It's almost 2017 now, I think this is out of date. =)

Quoted 10 lines
drivers/gpu/drm/tegra/drm.c +1 -0
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
  */
 
+#include <linux/bitops.h>
 #include <linux/host1x.h>
 #include <linux/iommu.h>
 
Quoted 7 lines

drivers/gpu/drm/tegra/drm.c line 27

Quoted 5 lines
drivers/gpu/drm/tegra/drm.c +1 -0
@@ -23,6 +24,8 @@
 #define DRIVER_MINOR 0
 #define DRIVER_PATCHLEVEL 0
 
+#define IOVA_AREA_SZ (1024 * 1024 * 64) /* 64 MiB */

SZ_64M?

Fixed.

Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/tegra/drm.c +1 -0
+
 struct tegra_drm_file {
 	struct list_head contexts;
 };
@@ -127,7 +130,8 @@ static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
 
Quoted 8 lines

drivers/gpu/drm/tegra/drm.c static int tegra_drm_load(struct drm_device *drm, unsigned long flags) · line 134

Quoted 5 lines
drivers/gpu/drm/tegra/drm.c +2 -1
 	if (iommu_present(&platform_bus_type)) {
 		struct iommu_domain_geometry *geometry;
-		u64 start, end;
+		unsigned long order;
+		u64 iova_start, start, end;

Can we be a little more explicit and keep an additional iova_end to simplify the code a little?

Good idea. I replaced this with gem_{start,end} and carveout_{start,end} and it looks nicer.

Quoted 15 lines

drivers/gpu/drm/tegra/drm.c static int tegra_drm_load(struct drm_device *drm, unsigned long flags) · line 150

Quoted 13 lines
drivers/gpu/drm/tegra/drm.c +6 -0
 		tegra->domain = iommu_domain_alloc(&platform_bus_type);
 		if (!tegra->domain) {
@@ -138,10 +142,17 @@ static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
 		geometry = &tegra->domain->geometry;
 		start = geometry->aperture_start;
 		end = geometry->aperture_end;
+		iova_start = end - IOVA_AREA_SZ + 1;
+
+		order = __ffs(tegra->domain->pgsize_bitmap);
+		init_iova_domain(&tegra->iova, 1UL << order,
+				 iova_start >> order,
+				 end >> order);

I hadn't seen this IOVA domain thing and it looks rather handy.

Yes, definitely nicer than rolling by hand. Could be a bit easier still though :) Too many "<< order" everywhere.

Quoted 12 lines

drivers/gpu/drm/tegra/drm.c static int tegra_drm_load(struct drm_device *drm, unsigned long flags) · line 155

Quoted 9 lines
drivers/gpu/drm/tegra/drm.c +4 -3
-		DRM_DEBUG_DRIVER("IOMMU aperture initialized (%#llx-%#llx)\n",
-				 start, end);
-		drm_mm_init(&tegra->mm, start, end - start + 1);
+		drm_mm_init(&tegra->mm, start, iova_start - start);
+
+		DRM_DEBUG("IOMMU apertures initialized (GEM: %#llx-%#llx, IOVA: %#llx-%#llx)\n",
+			  start, iova_start-1, iova_start, end);

Might be worth splitting this into two, or perhaps even three lines:

Quoted 7 lines
	IOMMU apertures:
	  GEM: %#llx-%#llx
	  IOVA: %#llx-%#llx

Maybe also find a better name for IOVA, since GEM will be using I/O virtual addresses as well. Perhaps just name it "carveout" or something like that?

Done and renamed to carveout.

Quoted 24 lines
Quoted 24 lines
drivers/gpu/drm/tegra/drm.c +2 -0
 	}
 
 	mutex_init(&tegra->clients_lock);
@@ -208,6 +219,7 @@ static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
 	if (tegra->domain) {
 		iommu_domain_free(tegra->domain);
 		drm_mm_takedown(&tegra->mm);
+		put_iova_domain(&tegra->iova);
 	}
 free:
 	kfree(tegra);
@@ -232,6 +244,7 @@ static int tegra_drm_unload(struct drm_device *drm)
 	if (tegra->domain) {
 		iommu_domain_free(tegra->domain);
 		drm_mm_takedown(&tegra->mm);
+		put_iova_domain(&tegra->iova);
 	}
 
 	kfree(tegra);
@@ -975,6 +988,81 @@ int tegra_drm_unregister_client(struct tegra_drm *tegra,
 	return 0;
 }
 
Quoted 5 lines

drivers/gpu/drm/tegra/drm.c int tegra_drm_unregister_client(struct tegra_drm *tegra, · line 992

Quoted 2 lines
drivers/gpu/drm/tegra/drm.c +2 -0
+void *tegra_drm_alloc(struct tegra_drm *tegra, size_t size,
+			      dma_addr_t *iova)

Maybe name the iova parameter phys? iova implies that it's always translated, whereas according to the code it can be physical, too.

How about something like "dma"? I find the use of "phys" for device addresses slightly confusing since sometimes they are and sometimes not and it's hard to say at a glance if the code in question actually assumes that the addresses are physical or not. "dma" should convey the meaning of "address used by device" a bit better.

Quoted 26 lines
Quoted 11 lines
drivers/gpu/drm/tegra/drm.c +10 -0
+{
+	struct iova *alloc;
+	unsigned long shift;
+	void *virt;
+	gfp_t gfp;
+	int err;
+
+	if (tegra->domain)
+		size = iova_align(&tegra->iova, size);
+	else

drivers/gpu/drm/tegra/drm.c int tegra_drm_unregister_client(struct tegra_drm *tegra, · line 1012

Quoted 10 lines
drivers/gpu/drm/tegra/drm.c +10 -0
+		size = PAGE_ALIGN(size);
+
+	gfp = GFP_KERNEL | __GFP_ZERO;
+	if (!tegra->domain) {
+		/*
+		 * Tegra210 has 64-bit physical addresses but units only support
+		 * 32-bit addresses, so if we don't have an IOMMU to do
+		 * translation, force allocations to be in the 32-bit range.
+		 */
+		gfp |= GFP_DMA;

Technically we do support Tegra132 and that already has 64-bit physical addresses, so perhaps include that in the comment, or make it more generic:

Quoted 6 lines

/*
* Many units only support 32-bit addresses, even on 64-bit SoCs.
* If there is no IOMMU to translate into a 32-bit IO virtual address
* address space, force allocations to be in the lower 32-bit range. */

Will fix.

Quoted 9 lines

drivers/gpu/drm/tegra/drm.c int tegra_drm_unregister_client(struct tegra_drm *tegra, · line 1017

Quoted 5 lines
drivers/gpu/drm/tegra/drm.c +5 -0
+	}
+
+	virt = (void *)__get_free_pages(gfp, get_order(size));
+	if (!virt)
+		return NULL;

Perhaps we want to return an ERR_PTR()-encoded error code here so we can differentiate between out-of-memory and out-of-virtual-address-space conditions in the caller? Or perhaps other conditions as well.

Sure.

Quoted 4 lines

Also, is __get_free_pages() the right API here? I thought that it always returned contiguous memory, which, in the presence of an IOMMU, will be completely unnecessary.

That is true. However, this API is intended only for a few small allocations is done by the kernel, so it shouldn't be that bad. And sources on the internet say that vmalloc should only be used when absolutely necessary :)

Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/tegra/drm.c +9 -0
+
+	if (!tegra->domain) {
+		/*
+		 * If IOMMU is disabled, devices address physical memory
+		 * directly.
+		 */
+		*iova = virt_to_phys(virt);
+		return virt;
+	}
Quoted 8 lines

drivers/gpu/drm/tegra/drm.c int tegra_drm_unregister_client(struct tegra_drm *tegra, · line 1030

Quoted 4 lines
drivers/gpu/drm/tegra/drm.c +4 -0
+
+	shift = iova_shift(&tegra->iova);
+	alloc = alloc_iova(&tegra->iova, size >> shift,
+			   tegra->domain->geometry.aperture_end >> shift, true);

This is fairly cumbersome to use. Maybe a drm_mm would be easier in this case? If not, could we at least store the upper limit of the carveout so that we don't have to use the really long expression above?

Stored shift and aperture_end >> shift.

Quoted 3 lines

Thierry

Thanks,
Mikko.

#10 lore raw

Re: [PATCH 2/8] drm/tegra: Allocate BOs from lower 4G when without IOMMU

In reply to a message not in the archive
Quoted 11 lines · Thierry Reding
Quoted 5 lines · Mikko Perttunen

On 64-bit Tegras, buffer object memory allocation may return memory above 4G that units behind Host1x cannot access. Add the GFP_DMA flag to these allocation when IOMMU is not enabled to ensure units can always access BO memory.

I don't think that's entirely true. The display controller can address more than 32 bits. It's perhaps slightly different from other units that are behind host1x, but maybe we need a special case here to allow frame- buffers to reside in higher memory?

I checked and looks like all (or at least most) units can actually address 34 bits /almost always/. The thing that tripped me up was that apparently the firmware for VIC still needs be in the lower 32 bits. So we should be able to drop this patch.

Quoted 3 lines

Thierry

Thanks,
Mikko.

#34 lore raw
In reply to Mikko Perttunen
Quoted 55 lines · Mikko Perttunen
Quoted 36 lines · Daniel Vetter
Quoted 26 lines · Thierry Reding
Quoted 25 lines · Mikko Perttunen

This series adds IOMMU support to Host1x and TegraDRM and adds support for the VIC host1x client so that host1x can be tested on modern Tegra platforms. It depends on the previous fix series. The whole thing (modulo patch order) is available as a git repository at
git://github.com/cyndis/linux.git; branch vic-v1.

IO memory management is organized such that there are two domains: the host1x domain and the tegradrm domain. The host1x domain is used by the host1x engine and contains the host1x CDMA and pushbuffers for submitted jobs.

The tegradrm domain is shared by all host1x units and contains GEM objects and memory allocated by the separate tegra_drm_alloc function. This function is currently used to allocate space for firmware blobs in the tegradrm domain.

A userspace test case for VIC can be found at
https://github.com/cyndis/drm/tree/work/tegra.
The testcase is in tests/tegra and is called submit_vic. The in-kernel firewall is not implemented for VIC; therefore, IOMMU must be enabled for the test to pass.

Quoted 8 lines
Quoted 4 lines

Tested with Jetson TX1 (T210). Probably works also with Jetson TK1 (T124). Note that due to hardware changes the testcase also needs to be changed to run properly on T124.

What's the scope of the changes required for Tegra124? If we add the kernel bits for Tegra124 we should also have a userspace test program to exercise it.

Quoted 6 lines

Please note that just a test program is not really enough to add more uabi (and VIC support here maybe classifies at that, no idea how this all works). Aka does mesa run, and for the vic stuff, does some libva/libvdpau open source implementation run on this?
-Daniel

VIC is just an image compositor so if we wanted to use it alone we would probably need to implement X11 2D acceleration (at least nothing else comes to mind), which isn't that useful nowadays, since nouveau works too. I'm internally working on NVDEC (the video decoder) at the moment - if/when I'm allowed to upstream that, I'll provide more extensive userspace for decoding video. This would most likely be a libva or libvdpau backend; I have already prototyped these. This will also most likely use VIC for image format conversions.

Quoted 5 lines

As for the "more UABI" question; the UABI used here already exists in the kernel and libdrm. The same UABI can be used for any Host1x unit without modifications; we have previously exercised it on older Tegra revisions with different units. The only thing that changes is a new enumeration value is added for the new unit.

More uabi also includes stuff like adding more support for newer engines, more platforms, newer chips and all that. Anything really that is a contract between the kernel and userspace about how the hw is set up (so e.g. fancy features that need a few bits to be set correctly, or the right microcode to be loaded or something like that are also included.

Otherwise it'd be trivial to game by doing a basic driver for the very first chip that no one cares about any more, open source it, and then do all future enabling with the blob drivers only in userspace.

This open source userspace requirement isn't just to validate the bare ioctl structs and stuff. We have it _exactly_ because gpu interfaces are ridiculously wide (for speed and flexibility reasons), and hence it does apply for every extension. Even if you don't change any of the uapi headers (since those describe just a miniscule part of the overall drm interface exported to userspace). Another example is adding more atomic properties, which also doesn't change anything with the ioctl or structs itself.

Quoted 1 line

Is that reasonable?

If you have the userspace (and it needs to be real, not just promised) then yes. Otherwise imo no. E.g. Rob Clark always makes sure on msm that his r/e'ed mesa branch _does_ work properly, before merging support for new hw or engines or anything like that. Intel/AMD do the same, and so should nvidia.
-Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#15 lore raw

Re: [PATCH 3/8] drm/tegra: Add falcon helper library

In reply to a message not in the archive
Quoted 59 lines · Thierry Reding
Quoted 58 lines · Mikko Perttunen

From: Arto Merilainen <[email protected]>

Add a set of falcon helper routines for use by the tegradrm client drivers of the various falcon-based engines.

The falcon is a microcontroller that acts as a frontend for the rest of a particular Tegra engine. In order to properly utilize these engines, the frontend must be booted before pushing any commands.

Based on work by Andrew Chew <[email protected]>

---
 drivers/gpu/drm/tegra/Makefile |   3 +-
 drivers/gpu/drm/tegra/falcon.c | 256 +++++++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/tegra/falcon.h | 130 +++++++++++++++++++++
 3 files changed, 388 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/tegra/falcon.c
 create mode 100644 drivers/gpu/drm/tegra/falcon.h
drivers/gpu/drm/tegra/Makefile +2 -1
index 2c66a8d..93e9a4a 100644
@@ -13,6 +13,7 @@ tegra-drm-y := \
 	sor.o \
 	dpaux.o \
 	gr2d.o \
-	gr3d.o
+	gr3d.o \
+	falcon.o
 
 obj-$(CONFIG_DRM_TEGRA) += tegra-drm.o
drivers/gpu/drm/tegra/falcon.c new file +17 -0
new file mode 100644
index 0000000..180b2fd
@@ -0,0 +1,256 @@
+/*
+ * Copyright (c) 2015, NVIDIA Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/platform_device.h>
+#include <linux/dma-mapping.h>
+#include <linux/firmware.h>
+#include <linux/pci_ids.h>
+#include <linux/iopoll.h>
+
+#include "falcon.h"
+#include "drm.h"
+
Quoted 8 lines

drivers/gpu/drm/tegra/falcon.c line 19

Quoted 2 lines
drivers/gpu/drm/tegra/falcon.c new file +2 -0
+#define FALCON_IDLE_TIMEOUT_US		100000
+#define FALCON_IDLE_CHECK_PERIOD_US	10

Seems a little overkill to have these here, given that their only used twice. They're also used in two cases where we may end up using different periods and timeouts eventually, so I'd just stick them into falcon_wait_idle() and falcon_dma_wait_idle() directly and omit these definitions. That's kind of a nitpick, so feel free to keep it as-is.

Fixed.

Quoted 13 lines
Quoted 13 lines
drivers/gpu/drm/tegra/falcon.c new file +12 -0
+
+enum falcon_memory {
+	FALCON_MEMORY_IMEM,
+	FALCON_MEMORY_DATA,
+};
+
+static void falcon_writel(struct falcon *falcon, u32 value, u32 offset)
+{
+	writel(value, falcon->regs + offset);
+}
+
+int falcon_wait_idle(struct falcon *falcon)
Quoted 8 lines

drivers/gpu/drm/tegra/falcon.c line 36

Quoted 5 lines
drivers/gpu/drm/tegra/falcon.c new file +5 -0
+{
+	u32 idlestate;
+
+	return readl_poll_timeout(falcon->regs + FALCON_IDLESTATE, idlestate,
+				  (!idlestate),

That's an odd way to write this. Why not just "idlestate == 0"? That's much easier to read and more explicit.

Fixed.

Quoted 11 lines

drivers/gpu/drm/tegra/falcon.c line 43

Quoted 8 lines
drivers/gpu/drm/tegra/falcon.c new file +7 -0
+				  FALCON_IDLE_CHECK_PERIOD_US,
+				  FALCON_IDLE_TIMEOUT_US);
+}
+
+static int falcon_dma_wait_idle(struct falcon *falcon)
+{
+	u32 dmatrfcmd;

u32 value? Naming it after a register suggests that it may be an offset rather than the register value.

Fixed.

Quoted 34 lines
Quoted 34 lines
drivers/gpu/drm/tegra/falcon.c new file +33 -0
+
+	return readl_poll_timeout(falcon->regs + FALCON_DMATRFCMD, dmatrfcmd,
+				  (dmatrfcmd & FALCON_DMATRFCMD_IDLE),
+				  FALCON_IDLE_CHECK_PERIOD_US,
+				  FALCON_IDLE_TIMEOUT_US);
+}
+
+static int falcon_copy_chunk(struct falcon *falcon,
+			     phys_addr_t base,
+			     unsigned long offset,
+			     enum falcon_memory target)
+{
+	u32 cmd = FALCON_DMATRFCMD_SIZE_256B;
+
+	if (target == FALCON_MEMORY_IMEM)
+		cmd |= FALCON_DMATRFCMD_IMEM;
+
+	falcon_writel(falcon, offset, FALCON_DMATRFMOFFS);
+	falcon_writel(falcon, base, FALCON_DMATRFFBOFFS);
+	falcon_writel(falcon, cmd, FALCON_DMATRFCMD);
+
+	return falcon_dma_wait_idle(falcon);
+}
+
+static void falcon_copy_firmware_image(struct falcon *falcon,
+				       const struct firmware *firmware)
+{
+	u32 *firmware_vaddr = falcon->firmware.vaddr;
+	size_t i;
+
+	/* copy the whole thing taking into account endianness */
+	for (i = 0; i < firmware->size / sizeof(u32); i++)
+		firmware_vaddr[i] = le32_to_cpu(((u32 *)firmware->data)[i]);
Quoted 8 lines

drivers/gpu/drm/tegra/falcon.c line 80

Quoted 4 lines
drivers/gpu/drm/tegra/falcon.c new file +4 -0
+
+	/* ensure that caches are flushed and falcon can see the firmware */
+	dma_sync_single_for_device(falcon->dev, virt_to_phys(firmware_vaddr),
+				   falcon->firmware.size, DMA_TO_DEVICE);

My understanding is that this is an error and the DMA API will complain about it if debugging is enabled. I think you need to call one of the dma_map_*() functions on the memory before you can do a dma_sync_*().

Yeah, seems so. I added dma_map_single and dma_unmap_single around this call.

Quoted 24 lines

drivers/gpu/drm/tegra/falcon.c line 87

Quoted 8 lines
drivers/gpu/drm/tegra/falcon.c new file +7 -0
+}
+
+static int falcon_parse_firmware_image(struct falcon *falcon)
+{
+	struct falcon_firmware_bin_header_v1 *bin_header =
+		(void *)falcon->firmware.vaddr;
+	struct falcon_firmware_os_header_v1 *os_header;

Can we make these shorter? Perhaps by leaving out the _header suffix? It'd be nice if we could avoid splitting these across multiple lines.

Quoted 12 lines
drivers/gpu/drm/tegra/falcon.c new file +12 -0
+
+	/* endian problems would show up right here */
+	if (bin_header->bin_magic != PCI_VENDOR_ID_NVIDIA) {
+		dev_err(falcon->dev, "failed to get firmware magic");
+		return -EINVAL;
+	}
+
+	/* currently only version 1 is supported */
+	if (bin_header->bin_ver != 1) {
+		dev_err(falcon->dev, "unsupported firmware version");
+		return -EINVAL;
+	}
Quoted 8 lines

drivers/gpu/drm/tegra/falcon.c line 105

Quoted 6 lines
drivers/gpu/drm/tegra/falcon.c new file +6 -0
+
+	/* check that the firmware size is consistent */
+	if (bin_header->bin_size > falcon->firmware.size) {
+		dev_err(falcon->dev, "firmware image size inconsistency");
+		return -EINVAL;
+	}

These messages are missing newlines at the end.

Fixed.

Quoted 7 lines

drivers/gpu/drm/tegra/falcon.c line 108

Quoted 4 lines
drivers/gpu/drm/tegra/falcon.c new file +3 -0
+
+	os_header = (falcon->firmware.vaddr +
+		 bin_header->os_bin_header_offset);

I think if we shorten the variable names a little this will also fit on a single line. There's also no need for the parentheses.

Dropped the _header from variable names, did s/firmware/fw/ in struct names and also trimmed the field names a bit.

Quoted 11 lines

drivers/gpu/drm/tegra/falcon.c line 115

Quoted 8 lines
drivers/gpu/drm/tegra/falcon.c new file +7 -0
+
+	falcon->firmware.bin_data.size = bin_header->os_bin_size;
+	falcon->firmware.bin_data.offset = bin_header->os_bin_data_offset;
+	falcon->firmware.code.offset = os_header->os_code_offset;
+	falcon->firmware.code.size   = os_header->os_code_size;
+	falcon->firmware.data.offset = os_header->os_data_offset;
+	falcon->firmware.data.size   = os_header->os_data_size;

Can you remove the extra padding before =, please? It's inconsistent with the other assignments.

Fixed.

Quoted 9 lines

drivers/gpu/drm/tegra/falcon.c line 120

Quoted 6 lines
drivers/gpu/drm/tegra/falcon.c new file +5 -0
+
+	return 0;
+}
+
+int falcon_read_firmware(struct falcon *falcon, const char *firmware_name)

The firmware_ prefix in firmware_name is redundant and can be dropped, in my opinion.

Fixed.

Quoted 9 lines

drivers/gpu/drm/tegra/falcon.c line 125

Quoted 6 lines
drivers/gpu/drm/tegra/falcon.c new file +5 -0
+{
+	const struct firmware *firmware;
+	int err;
+
+	if (falcon->firmware.valid)

Do we really need the .valid field? It seems like we should be able to derive it from the value of one of the other fields.

Changed to use .vaddr.

Quoted 10 lines

drivers/gpu/drm/tegra/falcon.c line 131

Quoted 7 lines
drivers/gpu/drm/tegra/falcon.c new file +6 -0
+		return 0;
+
+	err = request_firmware(&firmware, firmware_name, falcon->dev);
+	if (err < 0) {
+		dev_err(falcon->dev, "failed to get firmware\n");
+		return err;

It'd be nice to show the user what error occurred. Maybe also show the name of the firmware that couldn't be loaded.

Fixed.

Quoted 10 lines

drivers/gpu/drm/tegra/falcon.c line 138

Quoted 8 lines
drivers/gpu/drm/tegra/falcon.c new file +7 -0
+	}
+
+	falcon->firmware.size = firmware->size;
+
+	/* allocate iova space for the firmware */
+	falcon->firmware.vaddr = falcon->ops->alloc(falcon, firmware->size,
+						 &falcon->firmware.paddr);

The arguments aren't properly aligned.

Fixed.

Quoted 52 lines
Quoted 52 lines
drivers/gpu/drm/tegra/falcon.c new file +51 -0
+	if (!falcon->firmware.vaddr) {
+		dev_err(falcon->dev, "dma memory mapping failed");
+		err = -ENOMEM;
+		goto err_alloc_firmware;
+	}
+
+	/* copy firmware image into local area. this also ensures endianness */
+	falcon_copy_firmware_image(falcon, firmware);
+
+	/* parse the image data */
+	err = falcon_parse_firmware_image(falcon);
+	if (err < 0) {
+		dev_err(falcon->dev, "failed to parse firmware image\n");
+		goto err_setup_firmware_image;
+	}
+
+	falcon->firmware.valid = true;
+
+	release_firmware(firmware);
+
+	return 0;
+
+err_setup_firmware_image:
+	falcon->ops->free(falcon, falcon->firmware.size,
+			  falcon->firmware.paddr, falcon->firmware.vaddr);
+err_alloc_firmware:
+	release_firmware(firmware);
+
+	return err;
+}
+
+int falcon_init(struct falcon *falcon)
+{
+	/* check mandatory ops */
+	if (!falcon->ops || !falcon->ops->alloc || !falcon->ops->free)
+		return -EINVAL;
+
+	falcon->firmware.valid = false;
+
+	return 0;
+}
+
+void falcon_exit(struct falcon *falcon)
+{
+	if (!falcon->firmware.valid)
+		return;
+
+	falcon->ops->free(falcon, falcon->firmware.size,
+			  falcon->firmware.paddr,
+			  falcon->firmware.vaddr);
+	falcon->firmware.valid = false;
Quoted 8 lines

drivers/gpu/drm/tegra/falcon.c line 195

Quoted 6 lines
drivers/gpu/drm/tegra/falcon.c new file +6 -0
+}
+
+int falcon_boot(struct falcon *falcon)
+{
+	unsigned long offset;
+	int err = 0;

I don'n think it's necessary to initialize err here.

Fixed.

Quoted 12 lines

drivers/gpu/drm/tegra/falcon.c line 202

Quoted 8 lines
drivers/gpu/drm/tegra/falcon.c new file +7 -0
+
+	if (!falcon->firmware.valid)
+		return -EINVAL;
+
+	falcon_writel(falcon, 0, FALCON_DMACTL);
+
+	/* setup the address of the binary data. Falcon can access it later */

If you write sentences with a full stop, please use a capital first letter. In this case I think you can just drop the full stop:

Quoted 1 line
	/* setup the address of the binary data so Falcon can access it later */

Fixed.

Quoted 44 lines
Quoted 44 lines
drivers/gpu/drm/tegra/falcon.c new file +43 -0
+	falcon_writel(falcon, (falcon->firmware.paddr +
+			       falcon->firmware.bin_data.offset) >> 8,
+		      FALCON_DMATRFBASE);
+
+	/* copy the data segment into Falcon internal memory */
+	for (offset = 0; offset < falcon->firmware.data.size; offset += 256)
+		falcon_copy_chunk(falcon,
+				  falcon->firmware.data.offset + offset,
+				  offset, FALCON_MEMORY_DATA);
+
+	/* copy the first code segment into Falcon internal memory */
+	falcon_copy_chunk(falcon, falcon->firmware.code.offset,
+			  0, FALCON_MEMORY_IMEM);
+
+	/* setup falcon interrupts */
+	falcon_writel(falcon, FALCON_IRQMSET_EXT(0xff) |
+			      FALCON_IRQMSET_SWGEN1 |
+			      FALCON_IRQMSET_SWGEN0 |
+			      FALCON_IRQMSET_EXTERR |
+			      FALCON_IRQMSET_HALT |
+			      FALCON_IRQMSET_WDTMR,
+		      FALCON_IRQMSET);
+	falcon_writel(falcon, FALCON_IRQDEST_EXT(0xff) |
+			      FALCON_IRQDEST_SWGEN1 |
+			      FALCON_IRQDEST_SWGEN0 |
+			      FALCON_IRQDEST_EXTERR |
+			      FALCON_IRQDEST_HALT,
+		      FALCON_IRQDEST);
+
+	/* enable interface */
+	falcon_writel(falcon, FALCON_ITFEN_MTHDEN |
+			      FALCON_ITFEN_CTXEN,
+		      FALCON_ITFEN);
+
+	/* boot falcon */
+	falcon_writel(falcon, 0x00000000, FALCON_BOOTVEC);
+	falcon_writel(falcon, FALCON_CPUCTL_STARTCPU, FALCON_CPUCTL);
+
+	err = falcon_wait_idle(falcon);
+	if (err < 0) {
+		dev_err(falcon->dev, "falcon boot failed due to timeout");
+		return err;
+	}
Quoted 8 lines

drivers/gpu/drm/tegra/falcon.c line 247

Quoted 2 lines
drivers/gpu/drm/tegra/falcon.c new file +2 -0
+
+	dev_info(falcon->dev, "falcon booted");

No need to brag here, it's supposed to be successful. Let the user know if it isn't. If you really want this, make sure it's output at debug level, not info. Also I think we need to agree on whether these are named Falcon or falcon and then use it consistently. I'm leaning towards the former. Also, there are a few messages with missing newlines.

Removed and fixed.

Quoted 72 lines
Quoted 72 lines
drivers/gpu/drm/tegra/falcon.c new file +9 -0
+
+	return 0;
+}
+
+void falcon_execute_method(struct falcon *falcon, u32 method, u32 data)
+{
+	falcon_writel(falcon, method >> 2, FALCON_UCLASS_METHOD_OFFSET);
+	falcon_writel(falcon, data, FALCON_UCLASS_METHOD_DATA);
+}
drivers/gpu/drm/tegra/falcon.h new file +56 -0
new file mode 100644
index 0000000..56284b9
@@ -0,0 +1,130 @@
+/*
+ * Copyright (c) 2015, NVIDIA Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef _FALCON_H_
+#define _FALCON_H_
+
+#include <linux/types.h>
+
+#define FALCON_UCLASS_METHOD_OFFSET		0x00000040
+
+#define FALCON_UCLASS_METHOD_DATA		0x00000044
+
+#define FALCON_IRQMSET				0x00001010
+#define FALCON_IRQMSET_WDTMR			(1 << 1)
+#define FALCON_IRQMSET_HALT			(1 << 4)
+#define FALCON_IRQMSET_EXTERR			(1 << 5)
+#define FALCON_IRQMSET_SWGEN0			(1 << 6)
+#define FALCON_IRQMSET_SWGEN1			(1 << 7)
+#define FALCON_IRQMSET_EXT(v)			(((v) & 0xff) << 8)
+
+#define FALCON_IRQDEST				0x0000101c
+#define FALCON_IRQDEST_HALT			(1 << 4)
+#define FALCON_IRQDEST_EXTERR			(1 << 5)
+#define FALCON_IRQDEST_SWGEN0			(1 << 6)
+#define FALCON_IRQDEST_SWGEN1			(1 << 7)
+#define FALCON_IRQDEST_EXT(v)			(((v) & 0xff) << 8)
+
+#define FALCON_ITFEN				0x00001048
+#define FALCON_ITFEN_CTXEN			(1 << 0)
+#define FALCON_ITFEN_MTHDEN			(1 << 1)
+
+#define FALCON_IDLESTATE			0x0000104c
+
+#define FALCON_CPUCTL				0x00001100
+#define FALCON_CPUCTL_STARTCPU			(1 << 1)
+
+#define FALCON_BOOTVEC				0x00001104
+
+#define FALCON_DMACTL				0x0000110c
+#define FALCON_DMACTL_DMEM_SCRUBBING		(1 << 1)
+#define FALCON_DMACTL_IMEM_SCRUBBING		(1 << 2)
+
+#define FALCON_DMATRFBASE			0x00001110
+
+#define FALCON_DMATRFMOFFS			0x00001114
+
+#define FALCON_DMATRFCMD			0x00001118
+#define FALCON_DMATRFCMD_IDLE			(1 << 1)
+#define FALCON_DMATRFCMD_IMEM			(1 << 4)
+#define FALCON_DMATRFCMD_SIZE_256B		(6 << 8)
+
Quoted 8 lines

drivers/gpu/drm/tegra/falcon.h line 61

Quoted 5 lines
drivers/gpu/drm/tegra/falcon.h new file +5 -0
+#define FALCON_DMATRFFBOFFS			0x0000111c
+
+struct falcon_firmware_bin_header_v1 {
+	u32 bin_magic;		/* 0x10de */
+	u32 bin_ver;		/* cya, versioning of bin format (1) */

Not sure everyone understands what cya means, here. Perhaps just drop it?

Removed.

Quoted 14 lines
Quoted 14 lines
drivers/gpu/drm/tegra/falcon.h new file +13 -0
+	u32 bin_size;		/* entire image size including this header */
+	u32 os_bin_header_offset;
+	u32 os_bin_data_offset;
+	u32 os_bin_size;
+};
+
+struct falcon_firmware_os_app_v1 {
+	u32 offset;
+	u32 size;
+};
+
+struct falcon_firmware_os_header_v1 {
+	u32 os_code_offset;
Quoted 8 lines

drivers/gpu/drm/tegra/falcon.h line 80

Quoted 6 lines
drivers/gpu/drm/tegra/falcon.h new file +6 -0
+	u32 os_code_size;
+	u32 os_data_offset;
+	u32 os_data_size;
+	u32 num_apps;
+	struct falcon_firmware_os_app_v1 *app_code;
+	struct falcon_firmware_os_app_v1 *app_data;

The above two seem to be completel unused. Should we drop them?

Dropped the last four fields.

Quoted 7 lines

drivers/gpu/drm/tegra/falcon.h line 83

Quoted 4 lines
drivers/gpu/drm/tegra/falcon.h new file +3 -0
+	u32 *os_ovl_offset;
+	u32 *os_ovl_size;
+};

Can we in general sanitize the names of these a little? It's redundent to add a os_ prefix in a structure that contains an _os_ infix.

Yep, removed the os_ prefixes.

Quoted 3 lines

Thierry

Thanks,
Mikko.

#19 lore raw

Re: [PATCH 4/8] drm/tegra: Add VIC support

In reply to a message not in the archive
Quoted 98 lines · Thierry Reding
Quoted 97 lines · Mikko Perttunen

From: Arto Merilainen <[email protected]>

This patch adds support for Video Image Compositor engine which can be used for 2d operations.

---
 drivers/gpu/drm/tegra/Makefile |   3 +-
 drivers/gpu/drm/tegra/drm.c    |   3 +
 drivers/gpu/drm/tegra/drm.h    |   1 +
 drivers/gpu/drm/tegra/vic.c    | 400 +++++++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/tegra/vic.h    |  31 ++++
 include/linux/host1x.h         |   1 +
 6 files changed, 438 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/tegra/vic.c
 create mode 100644 drivers/gpu/drm/tegra/vic.h
drivers/gpu/drm/tegra/Makefile +2 -1
index 93e9a4a..6af3a9a 100644
@@ -14,6 +14,7 @@ tegra-drm-y := \
 	dpaux.o \
 	gr2d.o \
 	gr3d.o \
-	falcon.o
+	falcon.o \
+	vic.o
 
 obj-$(CONFIG_DRM_TEGRA) += tegra-drm.o
drivers/gpu/drm/tegra/drm.c +3 -0
index ecfe7ba..707404d 100644
@@ -1151,11 +1151,13 @@ static const struct of_device_id host1x_drm_subdevs[] = {
 	{ .compatible = "nvidia,tegra124-sor", },
 	{ .compatible = "nvidia,tegra124-hdmi", },
 	{ .compatible = "nvidia,tegra124-dsi", },
+	{ .compatible = "nvidia,tegra124-vic", },
 	{ .compatible = "nvidia,tegra132-dsi", },
 	{ .compatible = "nvidia,tegra210-dc", },
 	{ .compatible = "nvidia,tegra210-dsi", },
 	{ .compatible = "nvidia,tegra210-sor", },
 	{ .compatible = "nvidia,tegra210-sor1", },
+	{ .compatible = "nvidia,tegra210-vic", },
 	{ /* sentinel */ }
 };
 
@@ -1177,6 +1179,7 @@ static struct platform_driver * const drivers[] = {
 	&tegra_sor_driver,
 	&tegra_gr2d_driver,
 	&tegra_gr3d_driver,
+	&tegra_vic_driver,
 };
 
 static int __init host1x_drm_init(void)
drivers/gpu/drm/tegra/drm.h +1 -0
index f75b505..8efaaa4 100644
@@ -292,5 +292,6 @@ extern struct platform_driver tegra_dpaux_driver;
 extern struct platform_driver tegra_sor_driver;
 extern struct platform_driver tegra_gr2d_driver;
 extern struct platform_driver tegra_gr3d_driver;
+extern struct platform_driver tegra_vic_driver;
 
 #endif /* HOST1X_DRM_H */
drivers/gpu/drm/tegra/vic.c new file +22 -0
new file mode 100644
index 0000000..9eda5e5
@@ -0,0 +1,400 @@
+/*
+ * Copyright (c) 2015, NVIDIA Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/clk.h>
+#include <linux/host1x.h>
+#include <linux/iommu.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/reset.h>
+
+#include <soc/tegra/pmc.h>
+
+#include "drm.h"
Quoted 8 lines

drivers/gpu/drm/tegra/vic.c line 28

Quoted 6 lines
drivers/gpu/drm/tegra/vic.c new file +6 -0
+#include "falcon.h"
+#include "vic.h"
+
+struct vic_config {
+	/* Firmware name */
+	const char *ucode_name;

Maybe just "firmware"? That way you could remove the comment.

Fixed.

Quoted 144 lines
Quoted 130 lines
drivers/gpu/drm/tegra/vic.c new file +129 -0
+};
+
+struct vic {
+	struct falcon falcon;
+	bool booted;
+
+	void __iomem *regs;
+	struct tegra_drm_client client;
+	struct host1x_channel *channel;
+	struct iommu_domain *domain;
+	struct device *dev;
+	struct clk *clk;
+
+	/* Platform configuration */
+	const struct vic_config *config;
+};
+
+static inline struct vic *to_vic(struct tegra_drm_client *client)
+{
+	return container_of(client, struct vic, client);
+}
+
+static void vic_writel(struct vic *vic, u32 value, unsigned int offset)
+{
+	writel(value, vic->regs + offset);
+}
+
+static int vic_runtime_resume(struct device *dev)
+{
+	struct vic *vic = dev_get_drvdata(dev);
+
+	return clk_prepare_enable(vic->clk);
+}
+
+static int vic_runtime_suspend(struct device *dev)
+{
+	struct vic *vic = dev_get_drvdata(dev);
+
+	clk_disable_unprepare(vic->clk);
+
+	vic->booted = false;
+
+	return 0;
+}
+
+static int vic_boot(struct vic *vic)
+{
+	u32 fce_ucode_size, fce_bin_data_offset;
+	void *hdr;
+	int err = 0;
+
+	if (vic->booted)
+		return 0;
+
+	if (!vic->falcon.firmware.valid) {
+		err = falcon_read_firmware(&vic->falcon,
+					   vic->config->ucode_name);
+		if (err < 0)
+			return err;
+	}
+
+	/* setup clockgating registers */
+	vic_writel(vic, CG_IDLE_CG_DLY_CNT(4) |
+			CG_IDLE_CG_EN |
+			CG_WAKEUP_DLY_CNT(4),
+		   NV_PVIC_MISC_PRI_VIC_CG);
+
+	err = falcon_boot(&vic->falcon);
+	if (err < 0)
+		return err;
+
+	hdr = vic->falcon.firmware.vaddr;
+	fce_bin_data_offset = *(u32 *)(hdr + VIC_UCODE_FCE_DATA_OFFSET);
+	hdr = vic->falcon.firmware.vaddr +
+		*(u32 *)(hdr + VIC_UCODE_FCE_HEADER_OFFSET);
+	fce_ucode_size = *(u32 *)(hdr + FCE_UCODE_SIZE_OFFSET);
+
+	falcon_execute_method(&vic->falcon, VIC_SET_APPLICATION_ID, 1);
+	falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_SIZE,
+			      fce_ucode_size);
+	falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_OFFSET,
+			      (vic->falcon.firmware.paddr + fce_bin_data_offset)
+				>> 8);
+
+	err = falcon_wait_idle(&vic->falcon);
+	if (err < 0) {
+		dev_err(vic->dev,
+			"failed to set application ID and FCE base\n");
+		return err;
+	}
+
+	vic->booted = true;
+
+	return 0;
+}
+
+static void *vic_falcon_alloc(struct falcon *falcon, size_t size,
+			       dma_addr_t *iova)
+{
+	struct tegra_drm *tegra = falcon->data;
+
+	return tegra_drm_alloc(tegra, size, iova);
+}
+
+static void vic_falcon_free(struct falcon *falcon, size_t size,
+			    dma_addr_t iova, void *va)
+{
+	struct tegra_drm *tegra = falcon->data;
+
+	return tegra_drm_free(tegra, size, va, iova);
+}
+
+static const struct falcon_ops vic_falcon_ops = {
+	.alloc = vic_falcon_alloc,
+	.free = vic_falcon_free
+};
+
+static int vic_init(struct host1x_client *client)
+{
+	struct tegra_drm_client *drm = host1x_to_drm_client(client);
+	struct drm_device *dev = dev_get_drvdata(client->parent);
+	struct tegra_drm *tegra = dev->dev_private;
+	struct vic *vic = to_vic(drm);
+	int err;
+
+	if (tegra->domain) {
+		err = iommu_attach_device(tegra->domain, vic->dev);
+		if (err < 0) {
+			dev_err(vic->dev, "failed to attach to domain: %d\n",

drivers/gpu/drm/tegra/vic.c line 167

Quoted 10 lines
drivers/gpu/drm/tegra/vic.c new file +10 -0
+				err);
+			return err;
+		}
+
+		vic->domain = tegra->domain;
+	}
+
+	vic->falcon.dev = vic->dev;
+	vic->falcon.regs = vic->regs;
+	vic->falcon.data = tegra;

Why do we need this? We can get at struct vic * from struct falcon * via an upcast, and once we have struct vic, we have struct tegra_drm_client.

Quoted 5 lines

But now I realize that apparently there's no way to get from that to the struct drm_device. I guess we could go via vic->client.base.parent and a call to dev_get_drvdata(), but that's rather far. It's still a little less confusing than stashing the struct tegra_drm * in it, since it is not at all driver-specific data.

I don't know, falcon.data is just supposed to be "priv pointer" for falcon ops callbacks so it could be anything, in principle. I can change this but I somewhat prefer the current version.

Quoted 5 lines

drivers/gpu/drm/tegra/vic.c line 169

Quoted 3 lines
drivers/gpu/drm/tegra/vic.c new file +2 -0
+	vic->falcon.ops = &vic_falcon_ops;
+	err = falcon_init(&vic->falcon);

Could use a blank line between the above.

Fixed.

Quoted 90 lines
Quoted 70 lines
drivers/gpu/drm/tegra/vic.c new file +69 -0
+	if (err < 0)
+		goto detach_device;
+
+	vic->channel = host1x_channel_request(client->dev);
+	if (!vic->channel) {
+		err = -ENOMEM;
+		goto exit_falcon;
+	}
+
+	client->syncpts[0] = host1x_syncpt_request(client->dev, 0);
+	if (!client->syncpts[0]) {
+		err = -ENOMEM;
+		goto free_channel;
+	}
+
+	err = tegra_drm_register_client(tegra, drm);
+	if (err < 0)
+		goto free_syncpt;
+
+	return 0;
+
+free_syncpt:
+	host1x_syncpt_free(client->syncpts[0]);
+free_channel:
+	host1x_channel_free(vic->channel);
+exit_falcon:
+	falcon_exit(&vic->falcon);
+detach_device:
+	if (tegra->domain)
+		iommu_detach_device(tegra->domain, vic->dev);
+
+	return err;
+}
+
+static int vic_exit(struct host1x_client *client)
+{
+	struct tegra_drm_client *drm = host1x_to_drm_client(client);
+	struct drm_device *dev = dev_get_drvdata(client->parent);
+	struct tegra_drm *tegra = dev->dev_private;
+	struct vic *vic = to_vic(drm);
+	int err;
+
+	err = tegra_drm_unregister_client(tegra, drm);
+	if (err < 0)
+		return err;
+
+	host1x_syncpt_free(client->syncpts[0]);
+	host1x_channel_free(vic->channel);
+
+	falcon_exit(&vic->falcon);
+
+	if (vic->domain) {
+		iommu_detach_device(vic->domain, vic->dev);
+		vic->domain = NULL;
+	}
+
+	return 0;
+}
+
+static const struct host1x_client_ops vic_client_ops = {
+	.init = vic_init,
+	.exit = vic_exit,
+};
+
+static int vic_open_channel(struct tegra_drm_client *client,
+			    struct tegra_drm_context *context)
+{
+	struct vic *vic = to_vic(client);
+	int err;

drivers/gpu/drm/tegra/vic.c line 248

Quoted 10 lines
drivers/gpu/drm/tegra/vic.c new file +10 -0
+
+	err = pm_runtime_get_sync(vic->dev);
+	if (err < 0)
+		return err;
+
+	/*
+	 * Try to boot the Falcon microcontroller. Booting is deferred until
+	 * here because the firmware might not yet be available during system
+	 * boot, for example if it's on remote storage.
+	 */

That sounds like a hack. Typically you're supposed to have the firmware on the same medium as the module that requests it. That is, if the driver is built-in, then you need to make the firmware available as built-in as well or stash it into an initrd. If the driver is built as a loadable module and is on the root filesystem, that's where the firmware should be as well. There's also the possibility to put the loadable module into an initrd, in which case that's where the firmware should go as well.

Quoted 3 lines

That said, I think it makes sense to defer the actual booting until this point. Loading the firmware seems unappropriate to me. It really should be done in ->probe().

Fixed. The way it's now done is that the firmware is read in probe and loaded in vic_init, as we cannot call tegra_drm functions from probe.

Quoted 107 lines
Quoted 107 lines
drivers/gpu/drm/tegra/vic.c new file +106 -0
+	err = vic_boot(vic);
+	if (err < 0) {
+		pm_runtime_put(vic->dev);
+		return err;
+	}
+
+	context->channel = host1x_channel_get(vic->channel);
+	if (!context->channel) {
+		pm_runtime_put(vic->dev);
+		return -ENOMEM;
+	}
+
+	return 0;
+}
+
+static void vic_close_channel(struct tegra_drm_context *context)
+{
+	struct vic *vic = to_vic(context->client);
+
+	host1x_channel_put(context->channel);
+
+	pm_runtime_put(vic->dev);
+}
+
+static const struct tegra_drm_client_ops vic_ops = {
+	.open_channel = vic_open_channel,
+	.close_channel = vic_close_channel,
+	.submit = tegra_drm_submit,
+};
+
+static const struct vic_config vic_t124_config = {
+	.ucode_name = "nvidia/tegra124/vic03_ucode.bin",
+};
+
+static const struct vic_config vic_t210_config = {
+	.ucode_name = "nvidia/tegra210/vic04_ucode.bin",
+};
+
+static const struct of_device_id vic_match[] = {
+	{ .compatible = "nvidia,tegra124-vic", .data = &vic_t124_config },
+	{ .compatible = "nvidia,tegra210-vic", .data = &vic_t210_config },
+	{ },
+};
+
+static int vic_probe(struct platform_device *pdev)
+{
+	struct vic_config *vic_config = NULL;
+	struct device *dev = &pdev->dev;
+	struct host1x_syncpt **syncpts;
+	struct resource *regs;
+	const struct of_device_id *match;
+	struct vic *vic;
+	int err;
+
+	match = of_match_device(vic_match, dev);
+	vic_config = (struct vic_config *)match->data;
+
+	vic = devm_kzalloc(dev, sizeof(*vic), GFP_KERNEL);
+	if (!vic)
+		return -ENOMEM;
+
+	syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
+	if (!syncpts)
+		return -ENOMEM;
+
+	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!regs) {
+		dev_err(&pdev->dev, "failed to get registers\n");
+		return -ENXIO;
+	}
+
+	vic->regs = devm_ioremap_resource(dev, regs);
+	if (IS_ERR(vic->regs))
+		return PTR_ERR(vic->regs);
+
+	vic->clk = devm_clk_get(dev, NULL);
+	if (IS_ERR(vic->clk)) {
+		dev_err(&pdev->dev, "failed to get clock\n");
+		return PTR_ERR(vic->clk);
+	}
+
+	platform_set_drvdata(pdev, vic);
+
+	INIT_LIST_HEAD(&vic->client.base.list);
+	vic->client.base.ops = &vic_client_ops;
+	vic->client.base.dev = dev;
+	vic->client.base.class = HOST1X_CLASS_VIC;
+	vic->client.base.syncpts = syncpts;
+	vic->client.base.num_syncpts = 1;
+	vic->dev = dev;
+	vic->config = vic_config;
+
+	INIT_LIST_HEAD(&vic->client.list);
+	vic->client.ops = &vic_ops;
+
+	err = host1x_client_register(&vic->client.base);
+	if (err < 0) {
+		dev_err(dev, "failed to register host1x client: %d\n", err);
+		platform_set_drvdata(pdev, NULL);
+		return err;
+	}
+
+	pm_runtime_enable(&pdev->dev);
+	if (!pm_runtime_enabled(&pdev->dev)) {
+		err = vic_runtime_resume(&pdev->dev);
+		if (err < 0)
Quoted 8 lines

drivers/gpu/drm/tegra/vic.c line 356

Quoted 2 lines
drivers/gpu/drm/tegra/vic.c new file +2 -0
+			goto unregister_client;
+	}

That's a slightly ugly construct, but I can't think of an easy way to get rid of it (other than to depend on PM, which actually might be the right thing to do here, it's already selected by ARCH_TEGRA on 64-bit ARM). I can't think of a scenario where we'd want to run without runtime PM.

Kept this for now.

Quoted 5 lines

drivers/gpu/drm/tegra/vic.c line 358

Quoted 3 lines
drivers/gpu/drm/tegra/vic.c new file +2 -0
+
+	dev_info(&pdev->dev, "initialized");

Again, no need for this.

Removed.

Quoted 3 lines

Thierry

Thanks,
Mikko.

#26 lore raw

Re: [PATCH 5/8] gpu: host1x: Add IOMMU support

In reply to a message not in the archive
Quoted 82 lines · Thierry Reding
Quoted 65 lines · Mikko Perttunen

Add support for the Host1x unit to be located behind an IOMMU. This is required when gather buffers may be allocated non-contiguously in physical memory, as can be the case when TegraDRM is also using the IOMMU.

---
 drivers/gpu/host1x/cdma.c       | 65 +++++++++++++++++++++++++++++------
 drivers/gpu/host1x/cdma.h       |  4 ++-
 drivers/gpu/host1x/dev.c        | 39 +++++++++++++++++++--
 drivers/gpu/host1x/dev.h        |  5 +++
 drivers/gpu/host1x/hw/cdma_hw.c | 10 +++---
 drivers/gpu/host1x/job.c        | 76 +++++++++++++++++++++++++++++++++++------
 drivers/gpu/host1x/job.h        |  1 +
 7 files changed, 171 insertions(+), 29 deletions(-)
drivers/gpu/host1x/cdma.c +17 -8
index c5d82a8..14692446e 100644
@@ -51,9 +51,15 @@ static void host1x_pushbuffer_destroy(struct push_buffer *pb)
 	struct host1x_cdma *cdma = pb_to_cdma(pb);
 	struct host1x *host1x = cdma_to_host1x(cdma);
 
-	if (pb->phys != 0)
-		dma_free_wc(host1x->dev, pb->size_bytes + 4, pb->mapped,
-			    pb->phys);
+	if (!pb->phys)
+		return;
+
+	if (host1x->domain) {
+		iommu_unmap(host1x->domain, pb->dma, pb->alloc_size_bytes);
+		free_iova(&host1x->iova, iova_pfn(&host1x->iova, pb->dma));
+	}
+
+	dma_free_wc(host1x->dev, pb->alloc_size_bytes, pb->mapped, pb->phys);
 
 	pb->mapped = NULL;
 	pb->phys = 0;
@@ -66,28 +72,65 @@ static int host1x_pushbuffer_init(struct push_buffer *pb)
 {
 	struct host1x_cdma *cdma = pb_to_cdma(pb);
 	struct host1x *host1x = cdma_to_host1x(cdma);
+	struct iova *alloc;
+	int err;
 
 	pb->mapped = NULL;
 	pb->phys = 0;
 	pb->size_bytes = HOST1X_PUSHBUFFER_SLOTS * 8;
+	pb->alloc_size_bytes = pb->size_bytes + 4;
 
 	/* initialize buffer pointers */
 	pb->fence = pb->size_bytes - 8;
 	pb->pos = 0;
 
-	/* allocate and map pushbuffer memory */
-	pb->mapped = dma_alloc_wc(host1x->dev, pb->size_bytes + 4, &pb->phys,
-				  GFP_KERNEL);
-	if (!pb->mapped)
-		goto fail;
+	if (host1x->domain) {
+		unsigned long shift;
+
+		pb->alloc_size_bytes = iova_align(&host1x->iova,
+						  pb->alloc_size_bytes);

drivers/gpu/host1x/cdma.c static int host1x_pushbuffer_init(struct push_buffer *pb) · line 101

Quoted 10 lines
drivers/gpu/host1x/cdma.c +10 -0
+
+		pb->mapped = dma_alloc_wc(host1x->dev, pb->alloc_size_bytes,
+					  &pb->phys, GFP_KERNEL);
+		if (!pb->mapped)
+			return -ENOMEM;
+
+		shift = iova_shift(&host1x->iova);
+		alloc = alloc_iova(
+			&host1x->iova, pb->alloc_size_bytes >> shift,
+			host1x->domain->geometry.aperture_end >> shift, true);

Let's precompute some of these values to make this expression more compact. For example pb->alloc_size_bytes is used a couple of times, so it could be stored temporarily in a local variable with a nice and short name such as "size".

Quoted 2 lines

If you store the aperture end, or limit_pfn, as I commented in an earlier patch, then this also becomes much more compact.

Made a local "size" variable and stored the aperture end and did some other cleanup as well.

Quoted 12 lines

drivers/gpu/host1x/cdma.c static int host1x_pushbuffer_init(struct push_buffer *pb) · line 110

Quoted 10 lines
drivers/gpu/host1x/cdma.c +9 -0
+		if (!alloc) {
+			err = -ENOMEM;
+			goto iommu_free_mem;
+		}
+
+		err = iommu_map(host1x->domain,
+				iova_dma_addr(&host1x->iova, alloc),
+				pb->phys, pb->alloc_size_bytes,
+				IOMMU_READ);

Same here.

Cleaned up.

Quoted 36 lines
Quoted 36 lines
drivers/gpu/host1x/cdma.c +18 -3
+		if (err)
+			goto iommu_free_iova;
+
+		pb->dma = iova_dma_addr(&host1x->iova, alloc);
+	} else {
+		pb->mapped = dma_alloc_wc(host1x->dev, pb->alloc_size_bytes,
+					  &pb->phys, GFP_KERNEL);
+		if (!pb->mapped)
+			return -ENOMEM;
+
+		pb->dma = pb->phys;
+	}
 
 	host1x_hw_pushbuffer_init(host1x, pb);
 
 	return 0;
 
-fail:
-	host1x_pushbuffer_destroy(pb);
-	return -ENOMEM;
+iommu_free_iova:
+	__free_iova(&host1x->iova, alloc);
+iommu_free_mem:
+	dma_free_wc(host1x->dev, pb->alloc_size_bytes, pb->mapped, pb->phys);
+
+	return err;
 }
 
 /*
drivers/gpu/host1x/cdma.h +0 -0
index 470087a..4b3645f 100644
@@ -43,10 +43,12 @@ struct host1x_job;
 
Quoted 11 lines

drivers/gpu/host1x/cdma.h struct host1x_job; · line 51

Quoted 9 lines
drivers/gpu/host1x/cdma.h +3 -1
 struct push_buffer {
 	void *mapped;			/* mapped pushbuffer memory */
-	dma_addr_t phys;		/* physical address of pushbuffer */
+	dma_addr_t dma;			/* device address of pushbuffer */
+	phys_addr_t phys;		/* physical address of pushbuffer */
 	u32 fence;			/* index we've written */
 	u32 pos;			/* index to write to */
 	u32 size_bytes;
+	u32 alloc_size_bytes;

Maybe just "alloc_size"?

Renamed. Also renamed size_bytes to size for consistency.

Quoted 143 lines
Quoted 143 lines
drivers/gpu/host1x/cdma.h +0 -0
 };
 
 struct buffer_timeout {
drivers/gpu/host1x/dev.c +37 -2
index a62317a..ca4527e 100644
@@ -168,16 +168,36 @@ static int host1x_probe(struct platform_device *pdev)
 		return err;
 	}
 
+	if (iommu_present(&platform_bus_type)) {
+		struct iommu_domain_geometry *geometry;
+		unsigned long order;
+
+		host->domain = iommu_domain_alloc(&platform_bus_type);
+		if (!host->domain)
+			return -ENOMEM;
+
+		err = iommu_attach_device(host->domain, &pdev->dev);
+		if (err)
+			goto fail_free_domain;
+
+		geometry = &host->domain->geometry;
+
+		order = __ffs(host->domain->pgsize_bitmap);
+		init_iova_domain(&host->iova, 1UL << order,
+				 geometry->aperture_start >> order,
+				 geometry->aperture_end >> order);
+	}
+
 	err = host1x_channel_list_init(host);
 	if (err) {
 		dev_err(&pdev->dev, "failed to initialize channel list\n");
-		return err;
+		goto fail_detach_device;
 	}
 
 	err = clk_prepare_enable(host->clk);
 	if (err < 0) {
 		dev_err(&pdev->dev, "failed to enable clock\n");
-		return err;
+		goto fail_detach_device;
 	}
 
 	err = host1x_syncpt_init(host);
@@ -206,6 +226,15 @@ static int host1x_probe(struct platform_device *pdev)
 	host1x_syncpt_deinit(host);
 fail_unprepare_disable:
 	clk_disable_unprepare(host->clk);
+fail_detach_device:
+	if (host->domain) {
+		put_iova_domain(&host->iova);
+		iommu_detach_device(host->domain, &pdev->dev);
+	}
+fail_free_domain:
+	if (host->domain)
+		iommu_domain_free(host->domain);
+
 	return err;
 }
 
@@ -218,6 +247,12 @@ static int host1x_remove(struct platform_device *pdev)
 	host1x_syncpt_deinit(host);
 	clk_disable_unprepare(host->clk);
 
+	if (host->domain) {
+		put_iova_domain(&host->iova);
+		iommu_detach_device(host->domain, &pdev->dev);
+		iommu_domain_free(host->domain);
+	}
+
 	return 0;
 }
 
drivers/gpu/host1x/dev.h +5 -0
index 5220510..6189825 100644
@@ -19,6 +19,8 @@
 
 #include <linux/platform_device.h>
 #include <linux/device.h>
+#include <linux/iommu.h>
+#include <linux/iova.h>
 
 #include "channel.h"
 #include "syncpt.h"
@@ -108,6 +110,9 @@ struct host1x {
 	struct device *dev;
 	struct clk *clk;
 
+	struct iommu_domain *domain;
+	struct iova_domain iova;
+
 	struct mutex intr_mutex;
 	int intr_syncpt_irq;
 
drivers/gpu/host1x/hw/cdma_hw.c +5 -5
index 659c1bb..b8c0348 100644
@@ -55,7 +55,7 @@ static void cdma_timeout_cpu_incr(struct host1x_cdma *cdma, u32 getptr,
 		*(p++) = HOST1X_OPCODE_NOP;
 		*(p++) = HOST1X_OPCODE_NOP;
 		dev_dbg(host1x->dev, "%s: NOP at %pad+%#x\n", __func__,
-			&pb->phys, getptr);
+			&pb->dma, getptr);
 		getptr = (getptr + 8) & (pb->size_bytes - 1);
 	}
 
@@ -78,9 +78,9 @@ static void cdma_start(struct host1x_cdma *cdma)
 			 HOST1X_CHANNEL_DMACTRL);
 
 	/* set base, put and end pointer */
-	host1x_ch_writel(ch, cdma->push_buffer.phys, HOST1X_CHANNEL_DMASTART);
+	host1x_ch_writel(ch, cdma->push_buffer.dma, HOST1X_CHANNEL_DMASTART);
 	host1x_ch_writel(ch, cdma->push_buffer.pos, HOST1X_CHANNEL_DMAPUT);
-	host1x_ch_writel(ch, cdma->push_buffer.phys +
+	host1x_ch_writel(ch, cdma->push_buffer.dma +
 			 cdma->push_buffer.size_bytes + 4,
 			 HOST1X_CHANNEL_DMAEND);
 
@@ -115,8 +115,8 @@ static void cdma_timeout_restart(struct host1x_cdma *cdma, u32 getptr)
 			 HOST1X_CHANNEL_DMACTRL);
 
 	/* set base, end pointer (all of memory) */
-	host1x_ch_writel(ch, cdma->push_buffer.phys, HOST1X_CHANNEL_DMASTART);
-	host1x_ch_writel(ch, cdma->push_buffer.phys +
+	host1x_ch_writel(ch, cdma->push_buffer.dma, HOST1X_CHANNEL_DMASTART);
+	host1x_ch_writel(ch, cdma->push_buffer.dma +
 			 cdma->push_buffer.size_bytes,
 			 HOST1X_CHANNEL_DMAEND);
 
drivers/gpu/host1x/job.c +0 -0
index a91b7c4..222d930 100644
@@ -174,8 +174,9 @@ static int do_waitchks(struct host1x_job *job, struct host1x *host,
 	return 0;
 }
 
Quoted 9 lines

drivers/gpu/host1x/job.c static int do_waitchks(struct host1x_job *job, struct host1x *host, · line 180

Quoted 5 lines
drivers/gpu/host1x/job.c +2 -1
-static unsigned int pin_job(struct host1x_job *job)
+static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
 {
+	int err;
 	unsigned int i;

Maybe use an inverse christmas tree for variables as in other parts of this driver? That is, add the new int err belowe the existing unsined int i.

Fixed.

Quoted 26 lines
Quoted 26 lines
drivers/gpu/host1x/job.c +6 -2
 	job->num_unpins = 0;
@@ -186,12 +187,16 @@ static unsigned int pin_job(struct host1x_job *job)
 		dma_addr_t phys_addr;
 
 		reloc->target.bo = host1x_bo_get(reloc->target.bo);
-		if (!reloc->target.bo)
+		if (!reloc->target.bo) {
+			err = -EINVAL;
 			goto unpin;
+		}
 
 		phys_addr = host1x_bo_pin(reloc->target.bo, &sgt);
-		if (!phys_addr)
+		if (!phys_addr) {
+			err = -EINVAL;
 			goto unpin;
+		}
 
 		job->addr_phys[job->num_unpins] = phys_addr;
 		job->unpins[job->num_unpins].bo = reloc->target.bo;
@@ -204,25 +209,68 @@ static unsigned int pin_job(struct host1x_job *job)
 		struct sg_table *sgt;
 		dma_addr_t phys_addr;
 
Quoted 8 lines

drivers/gpu/host1x/job.c static unsigned int pin_job(struct host1x_job *job) · line 216

Quoted 5 lines
drivers/gpu/host1x/job.c +5 -0
+		size_t gather_size = 0;
+		struct scatterlist *sg;
+		struct iova *alloc;
+		unsigned long shift;
+		int j;

There should be no blank line between blocks of variable declarations. Also, make j an unsigned int.

Fixed.

Quoted 9 lines
Quoted 9 lines
drivers/gpu/host1x/job.c +4 -1
+
 		g->bo = host1x_bo_get(g->bo);
-		if (!g->bo)
+		if (!g->bo) {
+			err = -EINVAL;
 			goto unpin;
+		}
 
Quoted 13 lines

drivers/gpu/host1x/job.c static unsigned int pin_job(struct host1x_job *job) · line 233

Quoted 11 lines
drivers/gpu/host1x/job.c +8 -1
 		phys_addr = host1x_bo_pin(g->bo, &sgt);
-		if (!phys_addr)
+		if (!phys_addr) {
+			err = -EINVAL;
 			goto unpin;
+		}
+
+		if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) {
+			for_each_sg(sgt->sgl, sg, sgt->nents, j) {
+				gather_size += sg->length;
+			}

No need for curly brackets here.

Fixed.

Quoted 10 lines

drivers/gpu/host1x/job.c static unsigned int pin_job(struct host1x_job *job) · line 240

Quoted 8 lines
drivers/gpu/host1x/job.c +7 -0
+			gather_size = iova_align(&host->iova, gather_size);
+
+			shift = iova_shift(&host->iova);
+			alloc = alloc_iova(
+				&host->iova, gather_size >> shift,
+				host->domain->geometry.aperture_end >> shift,
+				true);

This could be made a little more compact as well.

Fixed.

Quoted 3 lines

Thierry

Thanks,
Mikko.