File: /usr/src/linux/arch/arm/mm/consistent.c
1 /*
2 * linux/arch/arm/mm/consistent.c
3 *
4 * Copyright (C) 2000 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Dynamic DMA mapping support.
11 */
12 #include <linux/config.h>
13 #include <linux/types.h>
14 #include <linux/mm.h>
15 #include <linux/string.h>
16 #include <linux/vmalloc.h>
17 #include <linux/interrupt.h>
18 #include <linux/errno.h>
19 #include <linux/pci.h>
20 #include <linux/init.h>
21
22 #include <asm/io.h>
23 #include <asm/pgtable.h>
24 #include <asm/pgalloc.h>
25
26 /*
27 * This allocates one page of cache-coherent memory space and returns
28 * both the virtual and a "dma" address to that space. It is not clear
29 * whether this could be called from an interrupt context or not. For
30 * now, we expressly forbid it, especially as some of the stuff we do
31 * here is not interrupt context safe.
32 *
33 * Note that this does *not* zero the allocated area!
34 */
35 void *consistent_alloc(int gfp, size_t size, dma_addr_t *dma_handle)
36 {
37 struct page *page, *end, *free;
38 unsigned long order;
39 void *ret, *virt;
40
41 if (in_interrupt())
42 BUG();
43
44 size = PAGE_ALIGN(size);
45 order = get_order(size);
46
47 page = alloc_pages(gfp, order);
48 if (!page)
49 goto no_page;
50
51 /*
52 * We could do with a page_to_phys and page_to_bus here.
53 */
54 virt = page_address(page);
55 *dma_handle = virt_to_bus(virt);
56 ret = __ioremap(virt_to_phys(virt), size, 0);
57 if (!ret)
58 goto no_remap;
59
60 #if 0 /* ioremap_does_flush_cache_all */
61 /*
62 * we need to ensure that there are no cachelines in use, or
63 * worse dirty in this area. Really, we don't need to do
64 * this since __ioremap does a flush_cache_all() anyway. --rmk
65 */
66 invalidate_dcache_range(virt, virt + size);
67 #endif
68
69 /*
70 * free wasted pages. We skip the first page since we know
71 * that it will have count = 1 and won't require freeing.
72 * We also mark the pages in use as reserved so that
73 * remap_page_range works.
74 */
75 page = virt_to_page(virt);
76 free = page + (size >> PAGE_SHIFT);
77 end = page + (1 << order);
78
79 for (; page < end; page++) {
80 set_page_count(page, 1);
81 if (page >= free)
82 __free_page(page);
83 else
84 SetPageReserved(page);
85 }
86 return ret;
87
88 no_remap:
89 __free_pages(page, order);
90 no_page:
91 return NULL;
92 }
93
94 void *pci_alloc_consistent(struct pci_dev *hwdev, size_t size, dma_addr_t *handle)
95 {
96 void *__ret;
97 int __gfp = GFP_KERNEL;
98
99 #ifdef CONFIG_PCI
100 if ((hwdev) == NULL ||
101 (hwdev)->dma_mask != 0xffffffff)
102 #endif
103 __gfp |= GFP_DMA;
104
105 __ret = consistent_alloc(__gfp, (size),
106 (handle));
107 return __ret;
108 }
109
110 /*
111 * free a page as defined by the above mapping. We expressly forbid
112 * calling this from interrupt context.
113 */
114 void consistent_free(void *vaddr, size_t size, dma_addr_t handle)
115 {
116 struct page *page, *end;
117 void *virt;
118
119 if (in_interrupt())
120 BUG();
121
122 virt = bus_to_virt(handle);
123
124 /*
125 * More messing around with the MM internals. This is
126 * sick, but then so is remap_page_range().
127 */
128 size = PAGE_ALIGN(size);
129 page = virt_to_page(virt);
130 end = page + (size >> PAGE_SHIFT);
131
132 for (; page < end; page++)
133 ClearPageReserved(page);
134
135 __iounmap(vaddr);
136 }
137
138 /*
139 * make an area consistent.
140 */
141 void consistent_sync(void *vaddr, size_t size, int direction)
142 {
143 unsigned long start = (unsigned long)vaddr;
144 unsigned long end = start + size;
145
146 switch (direction) {
147 case PCI_DMA_NONE:
148 BUG();
149 case PCI_DMA_FROMDEVICE: /* invalidate only */
150 invalidate_dcache_range(start, end);
151 break;
152 case PCI_DMA_TODEVICE: /* writeback only */
153 clean_dcache_range(start, end);
154 break;
155 case PCI_DMA_BIDIRECTIONAL: /* writeback and invalidate */
156 flush_dcache_range(start, end);
157 break;
158 }
159 }
160