License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / arch / mips / loongson64 / common / dma-swiotlb.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/mm.h>
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>
8
9 #include <asm/bootinfo.h>
10 #include <boot_param.h>
11 #include <dma-coherence.h>
12
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)
15 {
16         void *ret;
17
18         /* ignore region specifiers */
19         gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
20
21         if ((IS_ENABLED(CONFIG_ISA) && dev == NULL) ||
22             (IS_ENABLED(CONFIG_ZONE_DMA) &&
23              dev->coherent_dma_mask < DMA_BIT_MASK(32)))
24                 gfp |= __GFP_DMA;
25         else if (IS_ENABLED(CONFIG_ZONE_DMA32) &&
26                  dev->coherent_dma_mask < DMA_BIT_MASK(40))
27                 gfp |= __GFP_DMA32;
28
29         gfp |= __GFP_NORETRY;
30
31         ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
32         mb();
33         return ret;
34 }
35
36 static void loongson_dma_free_coherent(struct device *dev, size_t size,
37                 void *vaddr, dma_addr_t dma_handle, unsigned long attrs)
38 {
39         swiotlb_free_coherent(dev, size, vaddr, dma_handle);
40 }
41
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,
45                                 unsigned long attrs)
46 {
47         dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
48                                         dir, attrs);
49         mb();
50         return daddr;
51 }
52
53 static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
54                                 int nents, enum dma_data_direction dir,
55                                 unsigned long attrs)
56 {
57         int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, attrs);
58         mb();
59
60         return r;
61 }
62
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)
66 {
67         swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
68         mb();
69 }
70
71 static void loongson_dma_sync_sg_for_device(struct device *dev,
72                                 struct scatterlist *sg, int nents,
73                                 enum dma_data_direction dir)
74 {
75         swiotlb_sync_sg_for_device(dev, sg, nents, dir);
76         mb();
77 }
78
79 static int loongson_dma_supported(struct device *dev, u64 mask)
80 {
81         if (mask > DMA_BIT_MASK(loongson_sysconf.dma_mask_bits))
82                 return 0;
83         return swiotlb_dma_supported(dev, mask);
84 }
85
86 dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
87 {
88         long nid;
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);
94 #endif
95         return paddr;
96 }
97
98 phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
99 {
100         long nid;
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);
106 #endif
107         return daddr;
108 }
109
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,
123 };
124
125 void __init plat_swiotlb_setup(void)
126 {
127         swiotlb_init(1);
128         mips_dma_map_ops = &loongson_dma_map_ops;
129 }