1#include <linux/mm.h>
2#include <linux/init.h>
3#include <linux/dma-mapping.h>
4#include <linux/scatterlist.h>
5#include <linux/swiotlb.h>
6#include <linux/bootmem.h>
7
8#include <asm/bootinfo.h>
9#include <boot_param.h>
10#include <dma-coherence.h>
11
12static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
13		dma_addr_t *dma_handle, gfp_t gfp, struct dma_attrs *attrs)
14{
15	void *ret;
16
17	if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
18		return ret;
19
20	/* ignore region specifiers */
21	gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
22
23#ifdef CONFIG_ISA
24	if (dev == NULL)
25		gfp |= __GFP_DMA;
26	else
27#endif
28#ifdef CONFIG_ZONE_DMA
29	if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
30		gfp |= __GFP_DMA;
31	else
32#endif
33#ifdef CONFIG_ZONE_DMA32
34	if (dev->coherent_dma_mask < DMA_BIT_MASK(40))
35		gfp |= __GFP_DMA32;
36	else
37#endif
38	;
39	gfp |= __GFP_NORETRY;
40
41	ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
42	mb();
43	return ret;
44}
45
46static void loongson_dma_free_coherent(struct device *dev, size_t size,
47		void *vaddr, dma_addr_t dma_handle, struct dma_attrs *attrs)
48{
49	int order = get_order(size);
50
51	if (dma_release_from_coherent(dev, order, vaddr))
52		return;
53
54	swiotlb_free_coherent(dev, size, vaddr, dma_handle);
55}
56
57static dma_addr_t loongson_dma_map_page(struct device *dev, struct page *page,
58				unsigned long offset, size_t size,
59				enum dma_data_direction dir,
60				struct dma_attrs *attrs)
61{
62	dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
63					dir, attrs);
64	mb();
65	return daddr;
66}
67
68static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg,
69				int nents, enum dma_data_direction dir,
70				struct dma_attrs *attrs)
71{
72	int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, NULL);
73	mb();
74
75	return r;
76}
77
78static void loongson_dma_sync_single_for_device(struct device *dev,
79				dma_addr_t dma_handle, size_t size,
80				enum dma_data_direction dir)
81{
82	swiotlb_sync_single_for_device(dev, dma_handle, size, dir);
83	mb();
84}
85
86static void loongson_dma_sync_sg_for_device(struct device *dev,
87				struct scatterlist *sg, int nents,
88				enum dma_data_direction dir)
89{
90	swiotlb_sync_sg_for_device(dev, sg, nents, dir);
91	mb();
92}
93
94static int loongson_dma_set_mask(struct device *dev, u64 mask)
95{
96	if (mask > DMA_BIT_MASK(loongson_sysconf.dma_mask_bits)) {
97		*dev->dma_mask = DMA_BIT_MASK(loongson_sysconf.dma_mask_bits);
98		return -EIO;
99	}
100
101	*dev->dma_mask = mask;
102
103	return 0;
104}
105
106dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
107{
108	long nid;
109#ifdef CONFIG_PHYS48_TO_HT40
110	/* We extract 2bit node id (bit 44~47, only bit 44~45 used now) from
111	 * Loongson-3's 48bit address space and embed it into 40bit */
112	nid = (paddr >> 44) & 0x3;
113	paddr = ((nid << 44) ^ paddr) | (nid << 37);
114#endif
115	return paddr;
116}
117
118phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
119{
120	long nid;
121#ifdef CONFIG_PHYS48_TO_HT40
122	/* We extract 2bit node id (bit 44~47, only bit 44~45 used now) from
123	 * Loongson-3's 48bit address space and embed it into 40bit */
124	nid = (daddr >> 37) & 0x3;
125	daddr = ((nid << 37) ^ daddr) | (nid << 44);
126#endif
127	return daddr;
128}
129
130static struct dma_map_ops loongson_dma_map_ops = {
131	.alloc = loongson_dma_alloc_coherent,
132	.free = loongson_dma_free_coherent,
133	.map_page = loongson_dma_map_page,
134	.unmap_page = swiotlb_unmap_page,
135	.map_sg = loongson_dma_map_sg,
136	.unmap_sg = swiotlb_unmap_sg_attrs,
137	.sync_single_for_cpu = swiotlb_sync_single_for_cpu,
138	.sync_single_for_device = loongson_dma_sync_single_for_device,
139	.sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
140	.sync_sg_for_device = loongson_dma_sync_sg_for_device,
141	.mapping_error = swiotlb_dma_mapping_error,
142	.dma_supported = swiotlb_dma_supported,
143	.set_dma_mask = loongson_dma_set_mask
144};
145
146void __init plat_swiotlb_setup(void)
147{
148	swiotlb_init(1);
149	mips_dma_map_ops = &loongson_dma_map_ops;
150}
151