xref: /openbmc/linux/arch/microblaze/kernel/dma.c (revision a8da474e)
1 /*
2  * Copyright (C) 2009-2010 PetaLogix
3  * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
4  *
5  * Provide default implementations of the DMA mapping callbacks for
6  * directly mapped busses.
7  */
8 
9 #include <linux/device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/gfp.h>
12 #include <linux/dma-debug.h>
13 #include <linux/export.h>
14 #include <linux/bug.h>
15 
16 #define NOT_COHERENT_CACHE
17 
18 static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
19 				       dma_addr_t *dma_handle, gfp_t flag,
20 				       struct dma_attrs *attrs)
21 {
22 #ifdef NOT_COHERENT_CACHE
23 	return consistent_alloc(flag, size, dma_handle);
24 #else
25 	void *ret;
26 	struct page *page;
27 	int node = dev_to_node(dev);
28 
29 	/* ignore region specifiers */
30 	flag  &= ~(__GFP_HIGHMEM);
31 
32 	page = alloc_pages_node(node, flag, get_order(size));
33 	if (page == NULL)
34 		return NULL;
35 	ret = page_address(page);
36 	memset(ret, 0, size);
37 	*dma_handle = virt_to_phys(ret);
38 
39 	return ret;
40 #endif
41 }
42 
43 static void dma_direct_free_coherent(struct device *dev, size_t size,
44 				     void *vaddr, dma_addr_t dma_handle,
45 				     struct dma_attrs *attrs)
46 {
47 #ifdef NOT_COHERENT_CACHE
48 	consistent_free(size, vaddr);
49 #else
50 	free_pages((unsigned long)vaddr, get_order(size));
51 #endif
52 }
53 
54 static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
55 			     int nents, enum dma_data_direction direction,
56 			     struct dma_attrs *attrs)
57 {
58 	struct scatterlist *sg;
59 	int i;
60 
61 	/* FIXME this part of code is untested */
62 	for_each_sg(sgl, sg, nents, i) {
63 		sg->dma_address = sg_phys(sg);
64 		__dma_sync(sg_phys(sg), sg->length, direction);
65 	}
66 
67 	return nents;
68 }
69 
70 static int dma_direct_dma_supported(struct device *dev, u64 mask)
71 {
72 	return 1;
73 }
74 
75 static inline dma_addr_t dma_direct_map_page(struct device *dev,
76 					     struct page *page,
77 					     unsigned long offset,
78 					     size_t size,
79 					     enum dma_data_direction direction,
80 					     struct dma_attrs *attrs)
81 {
82 	__dma_sync(page_to_phys(page) + offset, size, direction);
83 	return page_to_phys(page) + offset;
84 }
85 
86 static inline void dma_direct_unmap_page(struct device *dev,
87 					 dma_addr_t dma_address,
88 					 size_t size,
89 					 enum dma_data_direction direction,
90 					 struct dma_attrs *attrs)
91 {
92 /* There is not necessary to do cache cleanup
93  *
94  * phys_to_virt is here because in __dma_sync_page is __virt_to_phys and
95  * dma_address is physical address
96  */
97 	__dma_sync(dma_address, size, direction);
98 }
99 
100 static inline void
101 dma_direct_sync_single_for_cpu(struct device *dev,
102 			       dma_addr_t dma_handle, size_t size,
103 			       enum dma_data_direction direction)
104 {
105 	/*
106 	 * It's pointless to flush the cache as the memory segment
107 	 * is given to the CPU
108 	 */
109 
110 	if (direction == DMA_FROM_DEVICE)
111 		__dma_sync(dma_handle, size, direction);
112 }
113 
114 static inline void
115 dma_direct_sync_single_for_device(struct device *dev,
116 				  dma_addr_t dma_handle, size_t size,
117 				  enum dma_data_direction direction)
118 {
119 	/*
120 	 * It's pointless to invalidate the cache if the device isn't
121 	 * supposed to write to the relevant region
122 	 */
123 
124 	if (direction == DMA_TO_DEVICE)
125 		__dma_sync(dma_handle, size, direction);
126 }
127 
128 static inline void
129 dma_direct_sync_sg_for_cpu(struct device *dev,
130 			   struct scatterlist *sgl, int nents,
131 			   enum dma_data_direction direction)
132 {
133 	struct scatterlist *sg;
134 	int i;
135 
136 	/* FIXME this part of code is untested */
137 	if (direction == DMA_FROM_DEVICE)
138 		for_each_sg(sgl, sg, nents, i)
139 			__dma_sync(sg->dma_address, sg->length, direction);
140 }
141 
142 static inline void
143 dma_direct_sync_sg_for_device(struct device *dev,
144 			      struct scatterlist *sgl, int nents,
145 			      enum dma_data_direction direction)
146 {
147 	struct scatterlist *sg;
148 	int i;
149 
150 	/* FIXME this part of code is untested */
151 	if (direction == DMA_TO_DEVICE)
152 		for_each_sg(sgl, sg, nents, i)
153 			__dma_sync(sg->dma_address, sg->length, direction);
154 }
155 
156 static
157 int dma_direct_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
158 			     void *cpu_addr, dma_addr_t handle, size_t size,
159 			     struct dma_attrs *attrs)
160 {
161 #ifdef CONFIG_MMU
162 	unsigned long user_count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
163 	unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
164 	unsigned long off = vma->vm_pgoff;
165 	unsigned long pfn;
166 
167 	if (off >= count || user_count > (count - off))
168 		return -ENXIO;
169 
170 #ifdef NOT_COHERENT_CACHE
171 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
172 	pfn = consistent_virt_to_pfn(cpu_addr);
173 #else
174 	pfn = virt_to_pfn(cpu_addr);
175 #endif
176 	return remap_pfn_range(vma, vma->vm_start, pfn + off,
177 			       vma->vm_end - vma->vm_start, vma->vm_page_prot);
178 #else
179 	return -ENXIO;
180 #endif
181 }
182 
183 struct dma_map_ops dma_direct_ops = {
184 	.alloc		= dma_direct_alloc_coherent,
185 	.free		= dma_direct_free_coherent,
186 	.mmap		= dma_direct_mmap_coherent,
187 	.map_sg		= dma_direct_map_sg,
188 	.dma_supported	= dma_direct_dma_supported,
189 	.map_page	= dma_direct_map_page,
190 	.unmap_page	= dma_direct_unmap_page,
191 	.sync_single_for_cpu		= dma_direct_sync_single_for_cpu,
192 	.sync_single_for_device		= dma_direct_sync_single_for_device,
193 	.sync_sg_for_cpu		= dma_direct_sync_sg_for_cpu,
194 	.sync_sg_for_device		= dma_direct_sync_sg_for_device,
195 };
196 EXPORT_SYMBOL(dma_direct_ops);
197 
198 /* Number of entries preallocated for DMA-API debugging */
199 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
200 
201 static int __init dma_init(void)
202 {
203 	dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
204 
205 	return 0;
206 }
207 fs_initcall(dma_init);
208