Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / m68k / sun3 / sun3dvma.c
blob8709677fa0255aa03dbe512e8e558c224f546f81
1 /*
2 * linux/arch/m68k/sun3/sun3dvma.c
4 * Copyright (C) 2000 Sam Creasey
6 * Contains common routines for sun3/sun3x DVMA management.
7 */
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/mm.h>
12 #include <linux/list.h>
14 #include <asm/page.h>
15 #include <asm/pgtable.h>
16 #include <asm/dvma.h>
18 #undef DVMA_DEBUG
20 #ifdef CONFIG_SUN3X
21 extern void dvma_unmap_iommu(unsigned long baddr, int len);
22 #else
23 static inline void dvma_unmap_iommu(unsigned long a, int b)
26 #endif
28 #ifdef CONFIG_SUN3
29 extern void sun3_dvma_init(void);
30 #endif
32 unsigned long iommu_use[IOMMU_TOTAL_ENTRIES];
34 #define dvma_index(baddr) ((baddr - DVMA_START) >> DVMA_PAGE_SHIFT)
36 #define dvma_entry_use(baddr) (iommu_use[dvma_index(baddr)])
38 struct hole {
39 unsigned long start;
40 unsigned long end;
41 unsigned long size;
42 struct list_head list;
45 static struct list_head hole_list;
46 static struct list_head hole_cache;
47 static struct hole initholes[64];
49 #ifdef DVMA_DEBUG
51 static unsigned long dvma_allocs;
52 static unsigned long dvma_frees;
53 static unsigned long long dvma_alloc_bytes;
54 static unsigned long long dvma_free_bytes;
56 static void print_use(void)
59 int i;
60 int j = 0;
62 printk("dvma entry usage:\n");
64 for(i = 0; i < IOMMU_TOTAL_ENTRIES; i++) {
65 if(!iommu_use[i])
66 continue;
68 j++;
70 printk("dvma entry: %08lx len %08lx\n",
71 ( i << DVMA_PAGE_SHIFT) + DVMA_START,
72 iommu_use[i]);
75 printk("%d entries in use total\n", j);
77 printk("allocation/free calls: %lu/%lu\n", dvma_allocs, dvma_frees);
78 printk("allocation/free bytes: %Lx/%Lx\n", dvma_alloc_bytes,
79 dvma_free_bytes);
82 static void print_holes(struct list_head *holes)
85 struct list_head *cur;
86 struct hole *hole;
88 printk("listing dvma holes\n");
89 list_for_each(cur, holes) {
90 hole = list_entry(cur, struct hole, list);
92 if((hole->start == 0) && (hole->end == 0) && (hole->size == 0))
93 continue;
95 printk("hole: start %08lx end %08lx size %08lx\n", hole->start, hole->end, hole->size);
98 printk("end of hole listing...\n");
101 #endif /* DVMA_DEBUG */
103 static inline int refill(void)
106 struct hole *hole;
107 struct hole *prev = NULL;
108 struct list_head *cur;
109 int ret = 0;
111 list_for_each(cur, &hole_list) {
112 hole = list_entry(cur, struct hole, list);
114 if(!prev) {
115 prev = hole;
116 continue;
119 if(hole->end == prev->start) {
120 hole->size += prev->size;
121 hole->end = prev->end;
122 list_move(&(prev->list), &hole_cache);
123 ret++;
128 return ret;
131 static inline struct hole *rmcache(void)
133 struct hole *ret;
135 if(list_empty(&hole_cache)) {
136 if(!refill()) {
137 printk("out of dvma hole cache!\n");
138 BUG();
142 ret = list_entry(hole_cache.next, struct hole, list);
143 list_del(&(ret->list));
145 return ret;
149 static inline unsigned long get_baddr(int len, unsigned long align)
152 struct list_head *cur;
153 struct hole *hole;
155 if(list_empty(&hole_list)) {
156 #ifdef DVMA_DEBUG
157 printk("out of dvma holes! (printing hole cache)\n");
158 print_holes(&hole_cache);
159 print_use();
160 #endif
161 BUG();
164 list_for_each(cur, &hole_list) {
165 unsigned long newlen;
167 hole = list_entry(cur, struct hole, list);
169 if(align > DVMA_PAGE_SIZE)
170 newlen = len + ((hole->end - len) & (align-1));
171 else
172 newlen = len;
174 if(hole->size > newlen) {
175 hole->end -= newlen;
176 hole->size -= newlen;
177 dvma_entry_use(hole->end) = newlen;
178 #ifdef DVMA_DEBUG
179 dvma_allocs++;
180 dvma_alloc_bytes += newlen;
181 #endif
182 return hole->end;
183 } else if(hole->size == newlen) {
184 list_move(&(hole->list), &hole_cache);
185 dvma_entry_use(hole->start) = newlen;
186 #ifdef DVMA_DEBUG
187 dvma_allocs++;
188 dvma_alloc_bytes += newlen;
189 #endif
190 return hole->start;
195 printk("unable to find dvma hole!\n");
196 BUG();
197 return 0;
200 static inline int free_baddr(unsigned long baddr)
203 unsigned long len;
204 struct hole *hole;
205 struct list_head *cur;
206 unsigned long orig_baddr;
208 orig_baddr = baddr;
209 len = dvma_entry_use(baddr);
210 dvma_entry_use(baddr) = 0;
211 baddr &= DVMA_PAGE_MASK;
212 dvma_unmap_iommu(baddr, len);
214 #ifdef DVMA_DEBUG
215 dvma_frees++;
216 dvma_free_bytes += len;
217 #endif
219 list_for_each(cur, &hole_list) {
220 hole = list_entry(cur, struct hole, list);
222 if(hole->end == baddr) {
223 hole->end += len;
224 hole->size += len;
225 return 0;
226 } else if(hole->start == (baddr + len)) {
227 hole->start = baddr;
228 hole->size += len;
229 return 0;
234 hole = rmcache();
236 hole->start = baddr;
237 hole->end = baddr + len;
238 hole->size = len;
240 // list_add_tail(&(hole->list), cur);
241 list_add(&(hole->list), cur);
243 return 0;
247 void dvma_init(void)
250 struct hole *hole;
251 int i;
253 INIT_LIST_HEAD(&hole_list);
254 INIT_LIST_HEAD(&hole_cache);
256 /* prepare the hole cache */
257 for(i = 0; i < 64; i++)
258 list_add(&(initholes[i].list), &hole_cache);
260 hole = rmcache();
261 hole->start = DVMA_START;
262 hole->end = DVMA_END;
263 hole->size = DVMA_SIZE;
265 list_add(&(hole->list), &hole_list);
267 memset(iommu_use, 0, sizeof(iommu_use));
269 dvma_unmap_iommu(DVMA_START, DVMA_SIZE);
271 #ifdef CONFIG_SUN3
272 sun3_dvma_init();
273 #endif
277 inline unsigned long dvma_map_align(unsigned long kaddr, int len, int align)
280 unsigned long baddr;
281 unsigned long off;
283 if(!len)
284 len = 0x800;
286 if(!kaddr || !len) {
287 // printk("error: kaddr %lx len %x\n", kaddr, len);
288 // *(int *)4 = 0;
289 return 0;
292 #ifdef DEBUG
293 printk("dvma_map request %08lx bytes from %08lx\n",
294 len, kaddr);
295 #endif
296 off = kaddr & ~DVMA_PAGE_MASK;
297 kaddr &= PAGE_MASK;
298 len += off;
299 len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
301 if(align == 0)
302 align = DVMA_PAGE_SIZE;
303 else
304 align = ((align + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
306 baddr = get_baddr(len, align);
307 // printk("using baddr %lx\n", baddr);
309 if(!dvma_map_iommu(kaddr, baddr, len))
310 return (baddr + off);
312 printk("dvma_map failed kaddr %lx baddr %lx len %x\n", kaddr, baddr, len);
313 BUG();
314 return 0;
316 EXPORT_SYMBOL(dvma_map_align);
318 void dvma_unmap(void *baddr)
320 unsigned long addr;
322 addr = (unsigned long)baddr;
323 /* check if this is a vme mapping */
324 if(!(addr & 0x00f00000))
325 addr |= 0xf00000;
327 free_baddr(addr);
329 return;
332 EXPORT_SYMBOL(dvma_unmap);
334 void *dvma_malloc_align(unsigned long len, unsigned long align)
336 unsigned long kaddr;
337 unsigned long baddr;
338 unsigned long vaddr;
340 if(!len)
341 return NULL;
343 #ifdef DEBUG
344 printk("dvma_malloc request %lx bytes\n", len);
345 #endif
346 len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
348 if((kaddr = __get_free_pages(GFP_ATOMIC, get_order(len))) == 0)
349 return NULL;
351 if((baddr = (unsigned long)dvma_map_align(kaddr, len, align)) == 0) {
352 free_pages(kaddr, get_order(len));
353 return NULL;
356 vaddr = dvma_btov(baddr);
358 if(dvma_map_cpu(kaddr, vaddr, len) < 0) {
359 dvma_unmap((void *)baddr);
360 free_pages(kaddr, get_order(len));
361 return NULL;
364 #ifdef DEBUG
365 printk("mapped %08lx bytes %08lx kern -> %08lx bus\n",
366 len, kaddr, baddr);
367 #endif
369 return (void *)vaddr;
372 EXPORT_SYMBOL(dvma_malloc_align);
374 void dvma_free(void *vaddr)
377 return;
380 EXPORT_SYMBOL(dvma_free);