Linux-2.6.12-rc2
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / frv / mm / dma-alloc.c
1 /* dma-alloc.c: consistent DMA memory allocation
2 *
3 * Derived from arch/ppc/mm/cachemap.c
4 *
5 * PowerPC version derived from arch/arm/mm/consistent.c
6 * Copyright (C) 2001 Dan Malek (dmalek@jlc.net)
7 *
8 * linux/arch/arm/mm/consistent.c
9 *
10 * Copyright (C) 2000 Russell King
11 *
12 * Consistent memory allocators. Used for DMA devices that want to
13 * share uncached memory with the processor core. The function return
14 * is the virtual address and 'dma_handle' is the physical address.
15 * Mostly stolen from the ARM port, with some changes for PowerPC.
16 * -- Dan
17 * Modified for 36-bit support. -Matt
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 2 as
21 * published by the Free Software Foundation.
22 */
23
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/signal.h>
27 #include <linux/sched.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 #include <linux/string.h>
31 #include <linux/types.h>
32 #include <linux/ptrace.h>
33 #include <linux/mman.h>
34 #include <linux/mm.h>
35 #include <linux/swap.h>
36 #include <linux/stddef.h>
37 #include <linux/vmalloc.h>
38 #include <linux/init.h>
39 #include <linux/pci.h>
40
41 #include <asm/pgalloc.h>
42 #include <asm/io.h>
43 #include <asm/hardirq.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
50 static 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 spin_lock(&init_mm.page_table_lock);
59
60 /* Use upper 10 bits of VA to index the first level map */
61 pge = pgd_offset_k(va);
62 pue = pud_offset(pge, va);
63 pme = pmd_offset(pue, va);
64
65 /* Use middle 10 bits of VA to index the second-level map */
66 pte = pte_alloc_kernel(&init_mm, pme, va);
67 if (pte != 0) {
68 err = 0;
69 set_pte(pte, mk_pte_phys(pa & PAGE_MASK, prot));
70 }
71
72 spin_unlock(&init_mm.page_table_lock);
73 return err;
74 }
75
76 /*
77 * This function will allocate the requested contiguous pages and
78 * map them into the kernel's vmalloc() space. This is done so we
79 * get unique mapping for these pages, outside of the kernel's 1:1
80 * virtual:physical mapping. This is necessary so we can cover large
81 * portions of the kernel with single large page TLB entries, and
82 * still get unique uncached pages for consistent DMA.
83 */
84 void *consistent_alloc(int gfp, size_t size, dma_addr_t *dma_handle)
85 {
86 struct vm_struct *area;
87 unsigned long page, va, pa;
88 void *ret;
89 int order, err, i;
90
91 if (in_interrupt())
92 BUG();
93
94 /* only allocate page size areas */
95 size = PAGE_ALIGN(size);
96 order = get_order(size);
97
98 page = __get_free_pages(gfp, order);
99 if (!page) {
100 BUG();
101 return NULL;
102 }
103
104 /* allocate some common virtual space to map the new pages */
105 area = get_vm_area(size, VM_ALLOC);
106 if (area == 0) {
107 free_pages(page, order);
108 return NULL;
109 }
110 va = VMALLOC_VMADDR(area->addr);
111 ret = (void *) va;
112
113 /* this gives us the real physical address of the first page */
114 *dma_handle = pa = virt_to_bus((void *) page);
115
116 /* set refcount=1 on all pages in an order>0 allocation so that vfree() will actually free
117 * all pages that were allocated.
118 */
119 if (order > 0) {
120 struct page *rpage = virt_to_page(page);
121
122 for (i = 1; i < (1 << order); i++)
123 set_page_count(rpage + i, 1);
124 }
125
126 err = 0;
127 for (i = 0; i < size && err == 0; i += PAGE_SIZE)
128 err = map_page(va + i, pa + i, PAGE_KERNEL_NOCACHE);
129
130 if (err) {
131 vfree((void *) va);
132 return NULL;
133 }
134
135 /* we need to ensure that there are no cachelines in use, or worse dirty in this area
136 * - can't do until after virtual address mappings are created
137 */
138 frv_cache_invalidate(va, va + size);
139
140 return ret;
141 }
142
143 /*
144 * free page(s) as defined by the above mapping.
145 */
146 void consistent_free(void *vaddr)
147 {
148 if (in_interrupt())
149 BUG();
150 vfree(vaddr);
151 }
152
153 /*
154 * make an area consistent.
155 */
156 void consistent_sync(void *vaddr, size_t size, int direction)
157 {
158 unsigned long start = (unsigned long) vaddr;
159 unsigned long end = start + size;
160
161 switch (direction) {
162 case PCI_DMA_NONE:
163 BUG();
164 case PCI_DMA_FROMDEVICE: /* invalidate only */
165 frv_cache_invalidate(start, end);
166 break;
167 case PCI_DMA_TODEVICE: /* writeback only */
168 frv_dcache_writeback(start, end);
169 break;
170 case PCI_DMA_BIDIRECTIONAL: /* writeback and invalidate */
171 frv_dcache_writeback(start, end);
172 break;
173 }
174 }
175
176 /*
177 * consistent_sync_page make a page are consistent. identical
178 * to consistent_sync, but takes a struct page instead of a virtual address
179 */
180
181 void consistent_sync_page(struct page *page, unsigned long offset,
182 size_t size, int direction)
183 {
184 void *start;
185
186 start = page_address(page) + offset;
187 consistent_sync(start, size, direction);
188 }