Linux 2.6.13-rc4
[linux-2.6/next.git] / arch / m68k / sun3 / sun3dvma.c
blobf04a1d25f1a25fea5d824f343e661c0a8006e256
1 /*
2 * linux/arch/m68k/mm/sun3dvma.c
4 * Copyright (C) 2000 Sam Creasey
6 * Contains common routines for sun3/sun3x DVMA management.
7 */
9 #include <linux/config.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_del(&(prev->list));
123 list_add(&(prev->list), &hole_cache);
124 ret++;
129 return ret;
132 static inline struct hole *rmcache(void)
134 struct hole *ret;
136 if(list_empty(&hole_cache)) {
137 if(!refill()) {
138 printk("out of dvma hole cache!\n");
139 BUG();
143 ret = list_entry(hole_cache.next, struct hole, list);
144 list_del(&(ret->list));
146 return ret;
150 static inline unsigned long get_baddr(int len, unsigned long align)
153 struct list_head *cur;
154 struct hole *hole;
156 if(list_empty(&hole_list)) {
157 #ifdef DVMA_DEBUG
158 printk("out of dvma holes! (printing hole cache)\n");
159 print_holes(&hole_cache);
160 print_use();
161 #endif
162 BUG();
165 list_for_each(cur, &hole_list) {
166 unsigned long newlen;
168 hole = list_entry(cur, struct hole, list);
170 if(align > DVMA_PAGE_SIZE)
171 newlen = len + ((hole->end - len) & (align-1));
172 else
173 newlen = len;
175 if(hole->size > newlen) {
176 hole->end -= newlen;
177 hole->size -= newlen;
178 dvma_entry_use(hole->end) = newlen;
179 #ifdef DVMA_DEBUG
180 dvma_allocs++;
181 dvma_alloc_bytes += newlen;
182 #endif
183 return hole->end;
184 } else if(hole->size == newlen) {
185 list_del(&(hole->list));
186 list_add(&(hole->list), &hole_cache);
187 dvma_entry_use(hole->start) = newlen;
188 #ifdef DVMA_DEBUG
189 dvma_allocs++;
190 dvma_alloc_bytes += newlen;
191 #endif
192 return hole->start;
197 printk("unable to find dvma hole!\n");
198 BUG();
199 return 0;
202 static inline int free_baddr(unsigned long baddr)
205 unsigned long len;
206 struct hole *hole;
207 struct list_head *cur;
208 unsigned long orig_baddr;
210 orig_baddr = baddr;
211 len = dvma_entry_use(baddr);
212 dvma_entry_use(baddr) = 0;
213 baddr &= DVMA_PAGE_MASK;
214 dvma_unmap_iommu(baddr, len);
216 #ifdef DVMA_DEBUG
217 dvma_frees++;
218 dvma_free_bytes += len;
219 #endif
221 list_for_each(cur, &hole_list) {
222 hole = list_entry(cur, struct hole, list);
224 if(hole->end == baddr) {
225 hole->end += len;
226 hole->size += len;
227 return 0;
228 } else if(hole->start == (baddr + len)) {
229 hole->start = baddr;
230 hole->size += len;
231 return 0;
236 hole = rmcache();
238 hole->start = baddr;
239 hole->end = baddr + len;
240 hole->size = len;
242 // list_add_tail(&(hole->list), cur);
243 list_add(&(hole->list), cur);
245 return 0;
249 void dvma_init(void)
252 struct hole *hole;
253 int i;
255 INIT_LIST_HEAD(&hole_list);
256 INIT_LIST_HEAD(&hole_cache);
258 /* prepare the hole cache */
259 for(i = 0; i < 64; i++)
260 list_add(&(initholes[i].list), &hole_cache);
262 hole = rmcache();
263 hole->start = DVMA_START;
264 hole->end = DVMA_END;
265 hole->size = DVMA_SIZE;
267 list_add(&(hole->list), &hole_list);
269 memset(iommu_use, 0, sizeof(iommu_use));
271 dvma_unmap_iommu(DVMA_START, DVMA_SIZE);
273 #ifdef CONFIG_SUN3
274 sun3_dvma_init();
275 #endif
279 inline unsigned long dvma_map_align(unsigned long kaddr, int len, int align)
282 unsigned long baddr;
283 unsigned long off;
285 if(!len)
286 len = 0x800;
288 if(!kaddr || !len) {
289 // printk("error: kaddr %lx len %x\n", kaddr, len);
290 // *(int *)4 = 0;
291 return 0;
294 #ifdef DEBUG
295 printk("dvma_map request %08lx bytes from %08lx\n",
296 len, kaddr);
297 #endif
298 off = kaddr & ~DVMA_PAGE_MASK;
299 kaddr &= PAGE_MASK;
300 len += off;
301 len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
303 if(align == 0)
304 align = DVMA_PAGE_SIZE;
305 else
306 align = ((align + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
308 baddr = get_baddr(len, align);
309 // printk("using baddr %lx\n", baddr);
311 if(!dvma_map_iommu(kaddr, baddr, len))
312 return (baddr + off);
314 printk("dvma_map failed kaddr %lx baddr %lx len %x\n", kaddr, baddr, len);
315 BUG();
316 return 0;
319 void dvma_unmap(void *baddr)
321 unsigned long addr;
323 addr = (unsigned long)baddr;
324 /* check if this is a vme mapping */
325 if(!(addr & 0x00f00000))
326 addr |= 0xf00000;
328 free_baddr(addr);
330 return;
335 void *dvma_malloc_align(unsigned long len, unsigned long align)
337 unsigned long kaddr;
338 unsigned long baddr;
339 unsigned long vaddr;
341 if(!len)
342 return NULL;
344 #ifdef DEBUG
345 printk("dvma_malloc request %lx bytes\n", len);
346 #endif
347 len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
349 if((kaddr = __get_free_pages(GFP_ATOMIC, get_order(len))) == 0)
350 return NULL;
352 if((baddr = (unsigned long)dvma_map_align(kaddr, len, align)) == 0) {
353 free_pages(kaddr, get_order(len));
354 return NULL;
357 vaddr = dvma_btov(baddr);
359 if(dvma_map_cpu(kaddr, vaddr, len) < 0) {
360 dvma_unmap((void *)baddr);
361 free_pages(kaddr, get_order(len));
362 return NULL;
365 #ifdef DEBUG
366 printk("mapped %08lx bytes %08lx kern -> %08lx bus\n",
367 len, kaddr, baddr);
368 #endif
370 return (void *)vaddr;
374 void dvma_free(void *vaddr)
377 return;