1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <linux/dma-mapping.h>
35#include <linux/scatterlist.h>
36#include <linux/bootmem.h>
37#include <linux/export.h>
38#include <linux/swiotlb.h>
39#include <linux/types.h>
40#include <linux/init.h>
41#include <linux/mm.h>
42
43#include <asm/bootinfo.h>
44
45static char *nlm_swiotlb;
46
47static void *nlm_dma_alloc_coherent(struct device *dev, size_t size,
48 dma_addr_t *dma_handle, gfp_t gfp, struct dma_attrs *attrs)
49{
50 void *ret;
51
52 if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
53 return ret;
54
55
56 gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
57
58#ifdef CONFIG_ZONE_DMA32
59 if (dev->coherent_dma_mask <= DMA_BIT_MASK(32))
60 gfp |= __GFP_DMA32;
61#endif
62
63
64 gfp |= __GFP_NORETRY;
65
66 return swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
67}
68
69static void nlm_dma_free_coherent(struct device *dev, size_t size,
70 void *vaddr, dma_addr_t dma_handle, struct dma_attrs *attrs)
71{
72 int order = get_order(size);
73
74 if (dma_release_from_coherent(dev, order, vaddr))
75 return;
76
77 swiotlb_free_coherent(dev, size, vaddr, dma_handle);
78}
79
80struct dma_map_ops nlm_swiotlb_dma_ops = {
81 .alloc = nlm_dma_alloc_coherent,
82 .free = nlm_dma_free_coherent,
83 .map_page = swiotlb_map_page,
84 .unmap_page = swiotlb_unmap_page,
85 .map_sg = swiotlb_map_sg_attrs,
86 .unmap_sg = swiotlb_unmap_sg_attrs,
87 .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
88 .sync_single_for_device = swiotlb_sync_single_for_device,
89 .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
90 .sync_sg_for_device = swiotlb_sync_sg_for_device,
91 .mapping_error = swiotlb_dma_mapping_error,
92 .dma_supported = swiotlb_dma_supported
93};
94
95void __init plat_swiotlb_setup(void)
96{
97 size_t swiotlbsize;
98 unsigned long swiotlb_nslabs;
99
100 swiotlbsize = 1 << 20;
101 swiotlb_nslabs = swiotlbsize >> IO_TLB_SHIFT;
102 swiotlb_nslabs = ALIGN(swiotlb_nslabs, IO_TLB_SEGSIZE);
103 swiotlbsize = swiotlb_nslabs << IO_TLB_SHIFT;
104
105 nlm_swiotlb = alloc_bootmem_low_pages(swiotlbsize);
106 swiotlb_init_with_tbl(nlm_swiotlb, swiotlb_nslabs, 1);
107}
108