From fca31f86328e687474cd18b7570678cfd19c63d7 Mon Sep 17 00:00:00 2001
From: Jason Mei <jianjunm@nvidia.com>
Date: Tue, 19 Jan 2021 22:03:42 +0800
Subject: [PATCH] gathered all dma performance test patches

---
 .../tegra194-soc/tegra194-soc-pcie.dtsi       |  2 +-
 .../arch/arm64/configs/tegra_defconfig        |  1 +
 .../nvidia/drivers/misc/tegra-pcie-ep-mem.c   | 37 +++++------
 kernel/nvidia/drivers/pci/dwc/pcie-tegra.c    |  6 +-
 .../pci/endpoint/functions/pci-epf-nv-test.c  | 61 ++++---------------
 5 files changed, 39 insertions(+), 68 deletions(-)

diff --git a/hardware/nvidia/soc/t19x/kernel-dts/tegra194-soc/tegra194-soc-pcie.dtsi b/hardware/nvidia/soc/t19x/kernel-dts/tegra194-soc/tegra194-soc-pcie.dtsi
index fddb0fbca..745b94708 100644
--- a/hardware/nvidia/soc/t19x/kernel-dts/tegra194-soc/tegra194-soc-pcie.dtsi
+++ b/hardware/nvidia/soc/t19x/kernel-dts/tegra194-soc/tegra194-soc-pcie.dtsi
@@ -882,7 +882,7 @@
 							204000000 204000000 408000000  666000000
 							204000000 408000000 666000000  1066000000
 							408000000 666000000 1066000000 2133000000 >;
-
+		nvidia,dma-poll;
 		nvidia,max-speed = <4>;
 		nvidia,disable-aspm-states = <0xf>;
 		nvidia,controller-id = <&bpmp 0x5>;
diff --git a/kernel/kernel-4.9/arch/arm64/configs/tegra_defconfig b/kernel/kernel-4.9/arch/arm64/configs/tegra_defconfig
index 52c08e6ff..1db996d4e 100644
--- a/kernel/kernel-4.9/arch/arm64/configs/tegra_defconfig
+++ b/kernel/kernel-4.9/arch/arm64/configs/tegra_defconfig
@@ -51,6 +51,7 @@ CONFIG_PCIEASPM_POWERSAVE=y
 CONFIG_PCI_STUB=m
 CONFIG_PCI_IOV=y
 CONFIG_PCIE_TEGRA=y
+CONFIG_PCIE_TEGRA_DW_DMA_TEST=y
 CONFIG_PCIE_TEGRA_EP=y
 CONFIG_PCI_TEGRA=y
 CONFIG_PCI_ENDPOINT=y
diff --git a/kernel/nvidia/drivers/misc/tegra-pcie-ep-mem.c b/kernel/nvidia/drivers/misc/tegra-pcie-ep-mem.c
index 1f04f9f70..622b724d5 100644
--- a/kernel/nvidia/drivers/misc/tegra-pcie-ep-mem.c
+++ b/kernel/nvidia/drivers/misc/tegra-pcie-ep-mem.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2018, NVIDIA CORPORATION.  All rights reserved.
+ * Copyright (c) 2017-2020, NVIDIA CORPORATION.  All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms and conditions of the GNU General Public License,
@@ -90,7 +90,7 @@
 #define DMA_LLP_LOW_OFF_RDCH		(0x1C + 0x100)
 #define DMA_LLP_HIGH_OFF_RDCH		(0x20 + 0x100)
 
-static unsigned long alloc_size = 0xA00000;
+static unsigned long alloc_size = 0x40000000;
 
 module_param(alloc_size, ulong, 0660);
 MODULE_PARM_DESC(alloc_size, "Allocation Size");
@@ -457,10 +457,12 @@ static int write(struct seq_file *s, void *data)
 	}
 
 	/* compare copied data */
-	if (!memcmp(__io_virt(bar_mem), phys_to_virt(ep->dst), ep->size))
+#if 0	
+	if (!memcmp(__io_virt(bar_mem), p_cpu_addr, ep->size))
 		dev_info(&ep->pdev->dev, "DMA-Write test PASSED\n");
 	else
 		dev_info(&ep->pdev->dev, "DMA-Write test FAILED\n");
+#endif
 err_out:
 	iounmap(bar_mem);
 err_remap:
@@ -484,7 +486,7 @@ static int write_ll(struct seq_file *s, void *data)
 	}
 
 	/* create linked list to be sent to ep's local memory */
-	ll = (struct dma_ll *)(phys_to_virt(ep->dst));
+	ll = (struct dma_ll *)(p_cpu_addr);
 
 	/* leave first 64K for LL element preparation */
 	memset((ll + 0), 0x0, sizeof(struct dma_ll));
@@ -541,19 +543,19 @@ static int write_ll(struct seq_file *s, void *data)
 
 	/* compare copied data */
 	if (memcmp((void *)(__io_virt(bar_mem) + (64 * 1024 * 1)),
-		   phys_to_virt(ep->dst + (64 * 1024 * 1)),
+		   (p_cpu_addr + (64 * 1024 * 1)),
 		   64 * 1024)) {
 		dev_err(&ep->pdev->dev, "DMA-Write-LL Chunk-1 FAILED\n");
 		goto err_out;
 	}
 	if (memcmp((void *)(__io_virt(bar_mem) + (64 * 1024 * 2)),
-		   phys_to_virt(ep->dst + (64 * 1024 * 2)),
+		   (p_cpu_addr + (64 * 1024 * 2)),
 		   64 * 1024)) {
 		dev_err(&ep->pdev->dev, "DMA-Write-LL Chunk-2 FAILED\n");
 		goto err_out;
 	}
 	if (memcmp((void *)(__io_virt(bar_mem) + (64 * 1024 * 4)),
-		   phys_to_virt(ep->dst + (64 * 1024 * 4)),
+		   (p_cpu_addr + (64 * 1024 * 4)),
 		   64 * 1024)) {
 		dev_err(&ep->pdev->dev, "DMA-Write-LL Chunk-3 FAILED\n");
 		goto err_out;
@@ -586,7 +588,7 @@ static int read(struct seq_file *s, void *data)
 		ret = -ENOMEM;
 		goto err_remap;
 	}
-	get_random_bytes(phys_to_virt(ep->src), ep->size);
+	get_random_bytes(p_cpu_addr, ep->size);
 
 	ret = dma_read(ep, &tx);
 	if (ret < 0) {
@@ -596,11 +598,12 @@ static int read(struct seq_file *s, void *data)
 	}
 
 	/* compare copied data */
-	if (!memcmp(__io_virt(bar_mem), phys_to_virt(ep->src), ep->size))
+#if 0	
+	if (!memcmp(__io_virt(bar_mem), p_cpu_addr, ep->size))
 		dev_info(&ep->pdev->dev, "DMA-Read test PASSED\n");
 	else
 		dev_info(&ep->pdev->dev, "DMA-Read test FAILED\n");
-
+#endif
 err_out:
 	iounmap(bar_mem);
 err_remap:
@@ -624,20 +627,20 @@ static int read_ll(struct seq_file *s, void *data)
 	}
 
 	/* create linked list to be sent to ep's local memory */
-	ll = (struct dma_ll *)(phys_to_virt(ep->src));
+	ll = (struct dma_ll *)(p_cpu_addr);
 
 	/* leave first 64K for LL element preparation */
 	memset((ll + 0), 0x0, sizeof(struct dma_ll));
 	(ll + 0)->size = (64 * 1024);
 	(ll + 0)->sar_low = ep->src + (64 * 1024 * 1);
 	(ll + 0)->dar_low = ep->dst + (64 * 1024 * 1);
-	get_random_bytes(phys_to_virt(ep->src + (64 * 1024 * 1)), 64 * 1024);
+	get_random_bytes((p_cpu_addr + (64 * 1024 * 1)), 64 * 1024);
 
 	memset((ll + 1), 0x0, sizeof(struct dma_ll));
 	(ll + 1)->size = (64 * 1024);
 	(ll + 1)->sar_low = ep->src + (64 * 1024 * 2);
 	(ll + 1)->dar_low = ep->dst + (64 * 1024 * 2);
-	get_random_bytes(phys_to_virt(ep->src + (64 * 1024 * 2)), 64 * 1024);
+	get_random_bytes((p_cpu_addr + (64 * 1024 * 2)), 64 * 1024);
 
 	memset((ll + 2), 0x0, sizeof(struct dma_ll));
 	(ll + 2)->ele_1.llp = 1;
@@ -649,7 +652,7 @@ static int read_ll(struct seq_file *s, void *data)
 	(ll + 4)->size = (64 * 1024);
 	(ll + 4)->sar_low = ep->src + (64 * 1024 * 4);
 	(ll + 4)->dar_low = ep->dst + (64 * 1024 * 4);
-	get_random_bytes(phys_to_virt(ep->src + (64 * 1024 * 4)), 64 * 1024);
+	get_random_bytes((p_cpu_addr + (64 * 1024 * 4)), 64 * 1024);
 
 	memset((ll + 5), 0x0, sizeof(struct dma_ll));
 	(ll + 5)->ele_1.llp = 1;
@@ -681,19 +684,19 @@ static int read_ll(struct seq_file *s, void *data)
 
 	/* compare copied data */
 	if (memcmp((void *)(__io_virt(bar_mem) + (64 * 1024 * 1)),
-		   phys_to_virt(ep->src + (64 * 1024 * 1)),
+		   (p_cpu_addr + (64 * 1024 * 1)),
 		   64 * 1024)) {
 		dev_err(&ep->pdev->dev, "DMA-Read-LL Chunk-1 FAILED\n");
 		goto err_out;
 	}
 	if (memcmp((void *)(__io_virt(bar_mem) + (64 * 1024 * 2)),
-		   phys_to_virt(ep->src + (64 * 1024 * 2)),
+		   (p_cpu_addr + (64 * 1024 * 2)),
 		   64 * 1024)) {
 		dev_err(&ep->pdev->dev, "DMA-Read-LL Chunk-2 FAILED\n");
 		goto err_out;
 	}
 	if (memcmp((void *)(__io_virt(bar_mem) + (64 * 1024 * 4)),
-		   phys_to_virt(ep->src + (64 * 1024 * 4)),
+		   (p_cpu_addr + (64 * 1024 * 4)),
 		   64 * 1024)) {
 		dev_err(&ep->pdev->dev, "DMA-Read-LL Chunk-3 FAILED\n");
 		goto err_out;
diff --git a/kernel/nvidia/drivers/pci/dwc/pcie-tegra.c b/kernel/nvidia/drivers/pci/dwc/pcie-tegra.c
index 6f3919f85..3d8c68496 100644
--- a/kernel/nvidia/drivers/pci/dwc/pcie-tegra.c
+++ b/kernel/nvidia/drivers/pci/dwc/pcie-tegra.c
@@ -1315,11 +1315,12 @@ static int write(struct seq_file *s, void *data)
 	}
 
 	/* compare copied data */
+#if 0
 	if (!memcmp(pcie->cpu_virt_addr, dst_cpu_virt, pcie->size))
 		dev_info(pcie->dev, "DMA-Write test PASSED\n");
 	else
 		dev_info(pcie->dev, "DMA-Write test FAILED\n");
-
+#endif
 err_out:
 	iounmap(dst_cpu_virt);
 	return ret;
@@ -1430,11 +1431,12 @@ static int read(struct seq_file *s, void *data)
 	}
 
 	/* compare copied data */
+#if 0
 	if (!memcmp(dst_cpu_virt, pcie->cpu_virt_addr, pcie->size))
 		dev_info(pcie->dev, "DMA-Read test PASSED\n");
 	else
 		dev_info(pcie->dev, "DMA-Read test FAILED\n");
-
+#endif
 err_out:
 	iounmap(dst_cpu_virt);
 	return ret;
diff --git a/kernel/nvidia/drivers/pci/endpoint/functions/pci-epf-nv-test.c b/kernel/nvidia/drivers/pci/endpoint/functions/pci-epf-nv-test.c
index 8b2a1dcec..48bbf03f4 100644
--- a/kernel/nvidia/drivers/pci/endpoint/functions/pci-epf-nv-test.c
+++ b/kernel/nvidia/drivers/pci/endpoint/functions/pci-epf-nv-test.c
@@ -16,7 +16,7 @@
 #include <linux/pci-epc.h>
 #include <linux/pci-epf.h>
 
-#define BAR0_SIZE SZ_64K
+#define BAR0_SIZE SZ_1G
 
 struct pci_epf_nv_test {
 	struct pci_epf_header header;
@@ -30,14 +30,11 @@ static void pci_epf_nv_test_unbind(struct pci_epf *epf)
 	struct pci_epf_nv_test *epfnv = epf_get_drvdata(epf);
 	struct pci_epc *epc = epf->epc;
 	struct device *cdev = epc->dev.parent;
-	struct iommu_domain *domain = iommu_get_domain_for_dev(cdev);
 
 	pci_epc_stop(epc);
 	pci_epc_clear_bar(epc, BAR_0);
-	vunmap(epfnv->bar0_ram_map);
-	iommu_unmap(domain, epfnv->bar0_iova, PAGE_SIZE);
-	iommu_dma_free_iova(cdev, epfnv->bar0_iova, BAR0_SIZE);
-	__free_pages(epfnv->bar0_ram_page, 1);
+	dma_free_coherent(cdev, BAR0_SIZE, epfnv->bar0_ram_map,
+			  epfnv->bar0_iova);
 }
 
 static int pci_epf_nv_test_bind(struct pci_epf *epf)
@@ -47,7 +44,6 @@ static int pci_epf_nv_test_bind(struct pci_epf *epf)
 	struct pci_epf_header *header = epf->header;
 	struct device *fdev = &epf->dev;
 	struct device *cdev = epc->dev.parent;
-	struct iommu_domain *domain = iommu_get_domain_for_dev(cdev);
 	int ret;
 
 	ret = pci_epc_write_header(epc, header);
@@ -56,60 +52,29 @@ static int pci_epf_nv_test_bind(struct pci_epf *epf)
 		return ret;
 	}
 
-	epfnv->bar0_ram_page = alloc_pages(GFP_KERNEL, 1);
-	if (!epfnv->bar0_ram_page) {
-		dev_err(fdev, "alloc_pages() failed\n");
-		ret = -ENOMEM;
-		goto fail;
-	}
-	dev_info(fdev, "BAR0 RAM phys: 0x%llx\n",
-		 page_to_phys(epfnv->bar0_ram_page));
-
-	epfnv->bar0_iova = iommu_dma_alloc_iova(cdev, BAR0_SIZE,
-						cdev->coherent_dma_mask);
-	if (!epfnv->bar0_iova) {
-		dev_err(fdev, "iommu_dma_alloc_iova() failed\n");
-		ret = -ENOMEM;
-		goto fail_free_pages;
-	}
-
-	dev_info(fdev, "BAR0 RAM IOVA: 0x%08llx\n", epfnv->bar0_iova);
-
-	ret = iommu_map(domain, epfnv->bar0_iova,
-			page_to_phys(epfnv->bar0_ram_page),
-			PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
-	if (ret) {
-		dev_err(fdev, "iommu_map(RAM) failed: %d\n", ret);
-		goto fail_free_iova;
-	}
-	epfnv->bar0_ram_map = vmap(&epfnv->bar0_ram_page, 1, VM_MAP,
-				   PAGE_KERNEL);
+	epfnv->bar0_ram_map = dma_alloc_coherent(cdev, BAR0_SIZE,
+						 &epfnv->bar0_iova, GFP_KERNEL);
 	if (!epfnv->bar0_ram_map) {
-		dev_err(fdev, "vmap() failed\n");
+		dev_err(fdev, "dma_alloc_coherent() failed\n");
 		ret = -ENOMEM;
-		goto fail_unmap_ram_iova;
+		return ret;
 	}
-	dev_info(fdev, "BAR0 RAM virt: 0x%p\n", epfnv->bar0_ram_map);
+	dev_info(fdev, "BAR0 RAM IOVA: 0x%08llx\n", epfnv->bar0_iova);
 
 	ret = pci_epc_set_bar(epc, BAR_0, epfnv->bar0_iova, BAR0_SIZE,
 			      PCI_BASE_ADDRESS_SPACE_MEMORY |
 			      PCI_BASE_ADDRESS_MEM_TYPE_32);
 	if (ret) {
 		dev_err(fdev, "pci_epc_set_bar() failed: %d\n", ret);
-		goto fail_unmap_ram_virt;
+		goto fail_set_bar;
+		return ret;
 	}
 
 	return 0;
 
-fail_unmap_ram_virt:
-	vunmap(epfnv->bar0_ram_map);
-fail_unmap_ram_iova:
-	iommu_unmap(domain, epfnv->bar0_iova, PAGE_SIZE);
-fail_free_iova:
-	iommu_dma_free_iova(cdev, epfnv->bar0_iova, BAR0_SIZE);
-fail_free_pages:
-	__free_pages(epfnv->bar0_ram_page, 1);
-fail:
+fail_set_bar:
+	dma_free_coherent(cdev, BAR0_SIZE, epfnv->bar0_ram_map,
+			  epfnv->bar0_iova);
 	return ret;
 }
 
-- 
2.17.1

