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#include <linux/dma-noncoherent.h>
37
38#include <asm/pgalloc.h>
39#include <linux/io.h>
40#include <linux/hardirq.h>
41#include <linux/mmu_context.h>
42#include <asm/mmu.h>
43#include <linux/uaccess.h>
44#include <asm/pgtable.h>
45#include <asm/cpuinfo.h>
46#include <asm/tlbflush.h>
47
48#ifndef CONFIG_MMU
49
50# define UNCACHED_SHADOW_MASK (cpuinfo.dcache_high - cpuinfo.dcache_base + 1)
51#endif
52
53
54
55
56
57
58
59
60
61
62
63void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
64 gfp_t gfp, unsigned long attrs)
65{
66 unsigned long order, vaddr;
67 void *ret;
68 unsigned int i, err = 0;
69 struct page *page, *end;
70
71#ifdef CONFIG_MMU
72 phys_addr_t pa;
73 struct vm_struct *area;
74 unsigned long va;
75#endif
76
77 if (in_interrupt())
78 BUG();
79
80
81 size = PAGE_ALIGN(size);
82 order = get_order(size);
83
84 vaddr = __get_free_pages(gfp, order);
85 if (!vaddr)
86 return NULL;
87
88
89
90
91
92 flush_dcache_range(virt_to_phys((void *)vaddr),
93 virt_to_phys((void *)vaddr) + size);
94
95#ifndef CONFIG_MMU
96 ret = (void *)vaddr;
97
98
99
100
101
102# ifdef CONFIG_XILINX_UNCACHED_SHADOW
103 ret = (void *)((unsigned) ret | UNCACHED_SHADOW_MASK);
104# endif
105 if ((unsigned int)ret > cpuinfo.dcache_base &&
106 (unsigned int)ret < cpuinfo.dcache_high)
107 pr_warn("ERROR: Your cache coherent area is CACHED!!!\n");
108
109
110 *dma_handle = (dma_addr_t)ret;
111#else
112
113 area = get_vm_area(size, VM_ALLOC);
114 if (!area) {
115 free_pages(vaddr, order);
116 return NULL;
117 }
118 va = (unsigned long) area->addr;
119 ret = (void *)va;
120
121
122 *dma_handle = pa = __virt_to_phys(vaddr);
123#endif
124
125
126
127
128
129
130
131 page = virt_to_page(vaddr);
132 end = page + (1 << order);
133
134 split_page(page, order);
135
136 for (i = 0; i < size && err == 0; i += PAGE_SIZE) {
137#ifdef CONFIG_MMU
138
139 err = map_page(va + i, pa + i, _PAGE_KERNEL | _PAGE_NO_CACHE);
140#endif
141
142 SetPageReserved(page);
143 page++;
144 }
145
146
147 while (page < end) {
148 __free_page(page);
149 page++;
150 }
151
152 if (err) {
153 free_pages(vaddr, order);
154 return NULL;
155 }
156
157 return ret;
158}
159
160#ifdef CONFIG_MMU
161static pte_t *consistent_virt_to_pte(void *vaddr)
162{
163 unsigned long addr = (unsigned long)vaddr;
164
165 return pte_offset_kernel(pmd_offset(pgd_offset_k(addr), addr), addr);
166}
167
168long arch_dma_coherent_to_pfn(struct device *dev, void *vaddr,
169 dma_addr_t dma_addr)
170{
171 pte_t *ptep = consistent_virt_to_pte(vaddr);
172
173 if (pte_none(*ptep) || !pte_present(*ptep))
174 return 0;
175
176 return pte_pfn(*ptep);
177}
178#endif
179
180
181
182
183void arch_dma_free(struct device *dev, size_t size, void *vaddr,
184 dma_addr_t dma_addr, unsigned long attrs)
185{
186 struct page *page;
187
188 if (in_interrupt())
189 BUG();
190
191 size = PAGE_ALIGN(size);
192
193#ifndef CONFIG_MMU
194
195# ifdef CONFIG_XILINX_UNCACHED_SHADOW
196 vaddr = (void *)((unsigned)vaddr & ~UNCACHED_SHADOW_MASK);
197# endif
198 page = virt_to_page(vaddr);
199
200 do {
201 __free_reserved_page(page);
202 page++;
203 } while (size -= PAGE_SIZE);
204#else
205 do {
206 pte_t *ptep = consistent_virt_to_pte(vaddr);
207 unsigned long pfn;
208
209 if (!pte_none(*ptep) && pte_present(*ptep)) {
210 pfn = pte_pfn(*ptep);
211 pte_clear(&init_mm, (unsigned int)vaddr, ptep);
212 if (pfn_valid(pfn)) {
213 page = pfn_to_page(pfn);
214 __free_reserved_page(page);
215 }
216 }
217 vaddr += PAGE_SIZE;
218 } while (size -= PAGE_SIZE);
219
220
221 flush_tlb_all();
222#endif
223}
224