1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/init.h>
4 #include <linux/dma-mapping.h>
5 #include <linux/scatterlist.h>
6 #include <linux/swiotlb.h>
7 #include <linux/bootmem.h>
9 #include <asm/bootinfo.h>
10 #include <boot_param.h>
11 #include <dma-coherence.h>
13 static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
14 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
18 /* ignore region specifiers */
19 gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
21 if ((IS_ENABLED(CONFIG_ISA) && dev == NULL) ||
22 (IS_ENABLED(CONFIG_ZONE_DMA) &&
23 dev->coherent_dma_mask < DMA_BIT_MASK(32)))
25 else if (IS_ENABLED(CONFIG_ZONE_DMA32) &&
26 dev->coherent_dma_mask < DMA_BIT_MASK(40))
31 ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
36 static void loongson_dma_free_coherent(struct device *dev, size_t size,
37 void *vaddr, dma_addr_t dma_handle, unsigned long attrs)
39 swiotlb_free_coherent(dev, size, vaddr, dma_handle);
42 static dma_addr_t loongson_dma_map_page(struct device *dev, struct page *page,
43 unsigned long offset, size_t size,
44 enum dma_data_direction dir,
47 dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
53 static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
54 int nents, enum dma_data_direction dir,
57 int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, attrs);
63 static void loongson_dma_sync_single_for_device(struct device *dev,
64 dma_addr_t dma_handle, size_t size,
65 enum dma_data_direction dir)
67 swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
71 static void loongson_dma_sync_sg_for_device(struct device *dev,
72 struct scatterlist *sg, int nents,
73 enum dma_data_direction dir)
75 swiotlb_sync_sg_for_device(dev, sg, nents, dir);
79 static int loongson_dma_supported(struct device *dev, u64 mask)
81 if (mask > DMA_BIT_MASK(loongson_sysconf.dma_mask_bits))
83 return swiotlb_dma_supported(dev, mask);
86 dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
89 #ifdef CONFIG_PHYS48_TO_HT40
90 /* We extract 2bit node id (bit 44~47, only bit 44~45 used now) from
91 * Loongson-3's 48bit address space and embed it into 40bit */
92 nid = (paddr >> 44) & 0x3;
93 paddr = ((nid << 44) ^ paddr) | (nid << 37);
98 phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
101 #ifdef CONFIG_PHYS48_TO_HT40
102 /* We extract 2bit node id (bit 44~47, only bit 44~45 used now) from
103 * Loongson-3's 48bit address space and embed it into 40bit */
104 nid = (daddr >> 37) & 0x3;
105 daddr = ((nid << 37) ^ daddr) | (nid << 44);
110 static const struct dma_map_ops loongson_dma_map_ops = {
111 .alloc = loongson_dma_alloc_coherent,
112 .free = loongson_dma_free_coherent,
113 .map_page = loongson_dma_map_page,
114 .unmap_page = swiotlb_unmap_page,
115 .map_sg = loongson_dma_map_sg,
116 .unmap_sg = swiotlb_unmap_sg_attrs,
117 .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
118 .sync_single_for_device = loongson_dma_sync_single_for_device,
119 .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
120 .sync_sg_for_device = loongson_dma_sync_sg_for_device,
121 .mapping_error = swiotlb_dma_mapping_error,
122 .dma_supported = loongson_dma_supported,
125 void __init plat_swiotlb_setup(void)
128 mips_dma_map_ops = &loongson_dma_map_ops;