1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/export.h>
17#include <linux/signal.h>
18#include <linux/sched.h>
19#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/string.h>
22#include <linux/types.h>
23#include <linux/ptrace.h>
24#include <linux/mman.h>
25#include <linux/mm.h>
26#include <linux/swap.h>
27#include <linux/stddef.h>
28#include <linux/vmalloc.h>
29#include <linux/init.h>
30#include <linux/delay.h>
31#include <linux/memblock.h>
32#include <linux/highmem.h>
33#include <linux/pci.h>
34#include <linux/interrupt.h>
35#include <linux/gfp.h>
36
37#include <asm/pgalloc.h>
38#include <linux/io.h>
39#include <linux/hardirq.h>
40#include <linux/mmu_context.h>
41#include <asm/mmu.h>
42#include <linux/uaccess.h>
43#include <asm/pgtable.h>
44#include <asm/cpuinfo.h>
45#include <asm/tlbflush.h>
46
47#ifndef CONFIG_MMU
48
49# define UNCACHED_SHADOW_MASK (cpuinfo.dcache_high - cpuinfo.dcache_base + 1)
50#endif
51
52
53
54
55
56
57
58
59
60
61
62void *consistent_alloc(gfp_t gfp, size_t size, dma_addr_t *dma_handle)
63{
64 unsigned long order, vaddr;
65 void *ret;
66 unsigned int i, err = 0;
67 struct page *page, *end;
68
69#ifdef CONFIG_MMU
70 phys_addr_t pa;
71 struct vm_struct *area;
72 unsigned long va;
73#endif
74
75 if (in_interrupt())
76 BUG();
77
78
79 size = PAGE_ALIGN(size);
80 order = get_order(size);
81
82 vaddr = __get_free_pages(gfp, order);
83 if (!vaddr)
84 return NULL;
85
86
87
88
89
90 flush_dcache_range(virt_to_phys((void *)vaddr),
91 virt_to_phys((void *)vaddr) + size);
92
93#ifndef CONFIG_MMU
94 ret = (void *)vaddr;
95
96
97
98
99
100# ifdef CONFIG_XILINX_UNCACHED_SHADOW
101 ret = (void *)((unsigned) ret | UNCACHED_SHADOW_MASK);
102# endif
103 if ((unsigned int)ret > cpuinfo.dcache_base &&
104 (unsigned int)ret < cpuinfo.dcache_high)
105 pr_warn("ERROR: Your cache coherent area is CACHED!!!\n");
106
107
108 *dma_handle = (dma_addr_t)ret;
109#else
110
111 area = get_vm_area(size, VM_ALLOC);
112 if (!area) {
113 free_pages(vaddr, order);
114 return NULL;
115 }
116 va = (unsigned long) area->addr;
117 ret = (void *)va;
118
119
120 *dma_handle = pa = __virt_to_phys(vaddr);
121#endif
122
123
124
125
126
127
128
129 page = virt_to_page(vaddr);
130 end = page + (1 << order);
131
132 split_page(page, order);
133
134 for (i = 0; i < size && err == 0; i += PAGE_SIZE) {
135#ifdef CONFIG_MMU
136
137 err = map_page(va + i, pa + i, _PAGE_KERNEL | _PAGE_NO_CACHE);
138#endif
139
140 SetPageReserved(page);
141 page++;
142 }
143
144
145 while (page < end) {
146 __free_page(page);
147 page++;
148 }
149
150 if (err) {
151 free_pages(vaddr, order);
152 return NULL;
153 }
154
155 return ret;
156}
157EXPORT_SYMBOL(consistent_alloc);
158
159#ifdef CONFIG_MMU
160static pte_t *consistent_virt_to_pte(void *vaddr)
161{
162 unsigned long addr = (unsigned long)vaddr;
163
164 return pte_offset_kernel(pmd_offset(pgd_offset_k(addr), addr), addr);
165}
166
167unsigned long consistent_virt_to_pfn(void *vaddr)
168{
169 pte_t *ptep = consistent_virt_to_pte(vaddr);
170
171 if (pte_none(*ptep) || !pte_present(*ptep))
172 return 0;
173
174 return pte_pfn(*ptep);
175}
176#endif
177
178
179
180
181void consistent_free(size_t size, void *vaddr)
182{
183 struct page *page;
184
185 if (in_interrupt())
186 BUG();
187
188 size = PAGE_ALIGN(size);
189
190#ifndef CONFIG_MMU
191
192# ifdef CONFIG_XILINX_UNCACHED_SHADOW
193 vaddr = (void *)((unsigned)vaddr & ~UNCACHED_SHADOW_MASK);
194# endif
195 page = virt_to_page(vaddr);
196
197 do {
198 __free_reserved_page(page);
199 page++;
200 } while (size -= PAGE_SIZE);
201#else
202 do {
203 pte_t *ptep = consistent_virt_to_pte(vaddr);
204 unsigned long pfn;
205
206 if (!pte_none(*ptep) && pte_present(*ptep)) {
207 pfn = pte_pfn(*ptep);
208 pte_clear(&init_mm, (unsigned int)vaddr, ptep);
209 if (pfn_valid(pfn)) {
210 page = pfn_to_page(pfn);
211 __free_reserved_page(page);
212 }
213 }
214 vaddr += PAGE_SIZE;
215 } while (size -= PAGE_SIZE);
216
217
218 flush_tlb_all();
219#endif
220}
221EXPORT_SYMBOL(consistent_free);
222
223
224
225
226void consistent_sync(void *vaddr, size_t size, int direction)
227{
228 unsigned long start;
229 unsigned long end;
230
231 start = (unsigned long)vaddr;
232
233
234#ifdef CONFIG_XILINX_UNCACHED_SHADOW
235 start &= ~UNCACHED_SHADOW_MASK;
236#endif
237 end = start + size;
238
239 switch (direction) {
240 case PCI_DMA_NONE:
241 BUG();
242 case PCI_DMA_FROMDEVICE:
243 invalidate_dcache_range(start, end);
244 break;
245 case PCI_DMA_TODEVICE:
246 flush_dcache_range(start, end);
247 break;
248 case PCI_DMA_BIDIRECTIONAL:
249 flush_dcache_range(start, end);
250 break;
251 }
252}
253EXPORT_SYMBOL(consistent_sync);
254
255
256
257
258
259
260void consistent_sync_page(struct page *page, unsigned long offset,
261 size_t size, int direction)
262{
263 unsigned long start = (unsigned long)page_address(page) + offset;
264 consistent_sync((void *)start, size, direction);
265}
266EXPORT_SYMBOL(consistent_sync_page);
267