[PATCH V2 2/2] LoongArch: Remove ARCH_HAS_PHYS_TO_DMA

Jianmin Lv posted 2 patches 3 years, 4 months ago
There is a newer version of this series
[PATCH V2 2/2] LoongArch: Remove ARCH_HAS_PHYS_TO_DMA
Posted by Jianmin Lv 3 years, 4 months ago
Use _DMA defined in ACPI spec for translation betweeen
DMA address and CPU address, and implement acpi_arch_dma_setup
for initializing dev->dma_range_map, where acpi_dma_get_range
is called for parsing _DMA.

e.g.
If we have two dma ranges:
cpu address      dma address    size         offset
0x200080000000   0x2080000000   0x400000000  0x1fe000000000
0x400080000000   0x4080000000   0x400000000  0x3fc000000000

_DMA for pci devices should be declared in host bridge as
flowing:

Name (_DMA, ResourceTemplate() {
        QWordMemory (ResourceProducer,
            PosDecode,
            MinFixed,
            MaxFixed,
            NonCacheable,
            ReadWrite,
            0x0,
            0x4080000000,
            0x447fffffff,
            0x3fc000000000,
            0x400000000,
            ,
            ,
            )

        QWordMemory (ResourceProducer,
            PosDecode,
            MinFixed,
            MaxFixed,
            NonCacheable,
            ReadWrite,
            0x0,
            0x2080000000,
            0x247fffffff,
            0x1fe000000000,
            0x400000000,
            ,
            ,
            )
    })

Signed-off-by: Jianmin Lv <lvjianmin@loongson.cn>
---
 arch/loongarch/Kconfig        |  1 -
 arch/loongarch/kernel/dma.c   | 62 +++++++++++++++++++++----------------------
 arch/loongarch/kernel/setup.c |  2 +-
 include/linux/acpi.h          |  9 ++++---
 4 files changed, 38 insertions(+), 36 deletions(-)

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index b57daee..9dedcf9 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -7,7 +7,6 @@ config LOONGARCH
 	select ARCH_ENABLE_MEMORY_HOTPLUG
 	select ARCH_ENABLE_MEMORY_HOTREMOVE
 	select ARCH_HAS_ACPI_TABLE_UPGRADE	if ACPI
-	select ARCH_HAS_PHYS_TO_DMA
 	select ARCH_HAS_PTE_SPECIAL
 	select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
 	select ARCH_INLINE_READ_LOCK if !PREEMPTION
diff --git a/arch/loongarch/kernel/dma.c b/arch/loongarch/kernel/dma.c
index 8c9b531..7850e6b 100644
--- a/arch/loongarch/kernel/dma.c
+++ b/arch/loongarch/kernel/dma.c
@@ -2,39 +2,39 @@
 /*
  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  */
-#include <linux/init.h>
+#include <linux/acpi.h>
 #include <linux/dma-direct.h>
-#include <linux/dma-mapping.h>
-#include <linux/dma-map-ops.h>
-#include <linux/swiotlb.h>
 
-#include <asm/bootinfo.h>
-#include <asm/dma.h>
-#include <asm/loongson.h>
-
-/*
- * We extract 4bit node id (bit 44~47) from Loongson-3's
- * 48bit physical address space and embed it into 40bit.
- */
-
-static int node_id_offset;
-
-dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
-{
-	long nid = (paddr >> 44) & 0xf;
-
-	return ((nid << 44) ^ paddr) | (nid << node_id_offset);
-}
-
-phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
+void acpi_arch_dma_setup(struct device *dev)
 {
-	long nid = (daddr >> node_id_offset) & 0xf;
+	int ret;
+	const struct bus_dma_region *map = NULL;
+
+	ret = acpi_dma_get_range(dev, &map);
+	if (!ret) {
+		const struct bus_dma_region *r = map;
+		u64 mask, dma_start, dma_end = 0;
+
+		/* determine the overall bounds of all dma regions */
+		for (dma_start = U64_MAX; r->size; r++) {
+
+			/* Take lower and upper limits */
+			if (r->dma_start < dma_start)
+				dma_start = r->dma_start;
+			if (r->dma_start + r->size - 1 > dma_end)
+				dma_end = r->dma_start + r->size - 1;
+		}
+
+		if (dma_start >= dma_end) {
+			dev_dbg(dev, "Invalid DMA regions configuration\n");
+			return;
+		}
+
+		mask = DMA_BIT_MASK(ilog2(dma_end) + 1);
+		dev->bus_dma_limit = dma_end;
+		dev->dma_range_map = map;
+		dev->coherent_dma_mask = min(dev->coherent_dma_mask, mask);
+		*dev->dma_mask = min(*dev->dma_mask, mask);
+	}
 
-	return ((nid << node_id_offset) ^ daddr) | (nid << 44);
-}
-
-void __init plat_swiotlb_setup(void)
-{
-	swiotlb_init(true, SWIOTLB_VERBOSE);
-	node_id_offset = ((readl(LS7A_DMA_CFG) & LS7A_DMA_NODE_MASK) >> LS7A_DMA_NODE_SHF) + 36;
 }
diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c
index c74860b..974f085 100644
--- a/arch/loongarch/kernel/setup.c
+++ b/arch/loongarch/kernel/setup.c
@@ -247,7 +247,7 @@ static void __init arch_mem_init(char **cmdline_p)
 	sparse_init();
 	memblock_set_bottom_up(true);
 
-	plat_swiotlb_setup();
+	swiotlb_init(true, SWIOTLB_VERBOSE);
 
 	dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
 
diff --git a/include/linux/acpi.h b/include/linux/acpi.h
index 34e0545..33977b87 100644
--- a/include/linux/acpi.h
+++ b/include/linux/acpi.h
@@ -278,14 +278,17 @@ int acpi_table_parse_madt(enum acpi_madt_type id,
 
 void acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa);
 
+#if defined(CONFIG_ARM64) || defined(CONFIG_LOONGARCH)
+void acpi_arch_dma_setup(struct device *dev);
+#else
+static inline void acpi_arch_dma_setup(struct device *dev) { }
+#endif
+
 #ifdef CONFIG_ARM64
 void acpi_numa_gicc_affinity_init(struct acpi_srat_gicc_affinity *pa);
-void acpi_arch_dma_setup(struct device *dev);
 #else
 static inline void
 acpi_numa_gicc_affinity_init(struct acpi_srat_gicc_affinity *pa) { }
-static inline void
-acpi_arch_dma_setup(struct device *dev) { }
 #endif
 
 int acpi_numa_memory_affinity_init (struct acpi_srat_mem_affinity *ma);
-- 
1.8.3.1
Re: [PATCH V2 2/2] LoongArch: Remove ARCH_HAS_PHYS_TO_DMA
Posted by kernel test robot 3 years, 4 months ago
Hi Jianmin,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on rafael-pm/linux-next]
[also build test WARNING on linus/master v5.19 next-20220804]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Jianmin-Lv/DMA-update-acpi_dma_get_range-to-return-dma-map-regions/20220805-153307
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git linux-next
config: loongarch-randconfig-r011-20220805 (https://download.01.org/0day-ci/archive/20220806/202208060021.6kjospL6-lkp@intel.com/config)
compiler: loongarch64-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/30f8fbc6b485f30e8533a7848a4ad9a2e1351be8
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Jianmin-Lv/DMA-update-acpi_dma_get_range-to-return-dma-map-regions/20220805-153307
        git checkout 30f8fbc6b485f30e8533a7848a4ad9a2e1351be8
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=loongarch SHELL=/bin/bash arch/loongarch/kernel/

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> arch/loongarch/kernel/dma.c:8:6: warning: no previous prototype for 'acpi_arch_dma_setup' [-Wmissing-prototypes]
       8 | void acpi_arch_dma_setup(struct device *dev)
         |      ^~~~~~~~~~~~~~~~~~~


vim +/acpi_arch_dma_setup +8 arch/loongarch/kernel/dma.c

     7	
   > 8	void acpi_arch_dma_setup(struct device *dev)
     9	{
    10		int ret;
    11		const struct bus_dma_region *map = NULL;
    12	
    13		ret = acpi_dma_get_range(dev, &map);
    14		if (!ret) {
    15			const struct bus_dma_region *r = map;
    16			u64 mask, dma_start, dma_end = 0;
    17	
    18			/* determine the overall bounds of all dma regions */
    19			for (dma_start = U64_MAX; r->size; r++) {
    20	
    21				/* Take lower and upper limits */
    22				if (r->dma_start < dma_start)
    23					dma_start = r->dma_start;
    24				if (r->dma_start + r->size - 1 > dma_end)
    25					dma_end = r->dma_start + r->size - 1;
    26			}
    27	
    28			if (dma_start >= dma_end) {
    29				dev_dbg(dev, "Invalid DMA regions configuration\n");
    30				return;
    31			}
    32	
    33			mask = DMA_BIT_MASK(ilog2(dma_end) + 1);
    34			dev->bus_dma_limit = dma_end;
    35			dev->dma_range_map = map;
    36			dev->coherent_dma_mask = min(dev->coherent_dma_mask, mask);
    37			*dev->dma_mask = min(*dev->dma_mask, mask);
    38		}
    39	

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp