1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/module.h>
25#include <linux/signal.h>
26#include <linux/sched.h>
27#include <linux/kernel.h>
28#include <linux/errno.h>
29#include <linux/string.h>
30#include <linux/types.h>
31#include <linux/ptrace.h>
32#include <linux/mman.h>
33#include <linux/mm.h>
34#include <linux/swap.h>
35#include <linux/stddef.h>
36#include <linux/vmalloc.h>
37#include <linux/init.h>
38#include <linux/pci.h>
39#include <linux/hardirq.h>
40#include <linux/gfp.h>
41
42#include <asm/pgalloc.h>
43#include <asm/io.h>
44#include <asm/mmu_context.h>
45#include <asm/pgtable.h>
46#include <asm/mmu.h>
47#include <asm/uaccess.h>
48#include <asm/smp.h>
49
50static int map_page(unsigned long va, unsigned long pa, pgprot_t prot)
51{
52 pgd_t *pge;
53 pud_t *pue;
54 pmd_t *pme;
55 pte_t *pte;
56 int err = -ENOMEM;
57
58
59 pge = pgd_offset_k(va);
60 pue = pud_offset(pge, va);
61 pme = pmd_offset(pue, va);
62
63
64 pte = pte_alloc_kernel(pme, va);
65 if (pte != 0) {
66 err = 0;
67 set_pte(pte, mk_pte_phys(pa & PAGE_MASK, prot));
68 }
69
70 return err;
71}
72
73
74
75
76
77
78
79
80
81void *consistent_alloc(gfp_t gfp, size_t size, dma_addr_t *dma_handle)
82{
83 struct vm_struct *area;
84 unsigned long page, va, pa;
85 void *ret;
86 int order, err, i;
87
88 if (in_interrupt())
89 BUG();
90
91
92 size = PAGE_ALIGN(size);
93 order = get_order(size);
94
95 page = __get_free_pages(gfp, order);
96 if (!page) {
97 BUG();
98 return NULL;
99 }
100
101
102 area = get_vm_area(size, VM_ALLOC);
103 if (area == 0) {
104 free_pages(page, order);
105 return NULL;
106 }
107 va = VMALLOC_VMADDR(area->addr);
108 ret = (void *) va;
109
110
111 *dma_handle = pa = virt_to_bus((void *) page);
112
113
114
115
116 if (order > 0) {
117 struct page *rpage = virt_to_page(page);
118 split_page(rpage, order);
119 }
120
121 err = 0;
122 for (i = 0; i < size && err == 0; i += PAGE_SIZE)
123 err = map_page(va + i, pa + i, PAGE_KERNEL_NOCACHE);
124
125 if (err) {
126 vfree((void *) va);
127 return NULL;
128 }
129
130
131
132
133 frv_cache_invalidate(va, va + size);
134
135 return ret;
136}
137
138
139
140
141void consistent_free(void *vaddr)
142{
143 if (in_interrupt())
144 BUG();
145 vfree(vaddr);
146}
147
148
149
150
151void consistent_sync(void *vaddr, size_t size, int direction)
152{
153 unsigned long start = (unsigned long) vaddr;
154 unsigned long end = start + size;
155
156 switch (direction) {
157 case PCI_DMA_NONE:
158 BUG();
159 case PCI_DMA_FROMDEVICE:
160 frv_cache_invalidate(start, end);
161 break;
162 case PCI_DMA_TODEVICE:
163 frv_dcache_writeback(start, end);
164 break;
165 case PCI_DMA_BIDIRECTIONAL:
166 frv_dcache_writeback(start, end);
167 break;
168 }
169}
170
171
172
173
174
175
176void consistent_sync_page(struct page *page, unsigned long offset,
177 size_t size, int direction)
178{
179 void *start;
180
181 start = page_address(page) + offset;
182 consistent_sync(start, size, direction);
183}
184