2 * linux/arch/m68k/sun3/sun3dvma.c
4 * Copyright (C) 2000 Sam Creasey
6 * Contains common routines for sun3/sun3x DVMA management.
9 #include <linux/bootmem.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/gfp.h>
15 #include <linux/list.h>
18 #include <asm/pgtable.h>
24 extern void dvma_unmap_iommu(unsigned long baddr
, int len
);
26 static inline void dvma_unmap_iommu(unsigned long a
, int b
)
32 extern void sun3_dvma_init(void);
35 static unsigned long *iommu_use
;
37 #define dvma_index(baddr) ((baddr - DVMA_START) >> DVMA_PAGE_SHIFT)
39 #define dvma_entry_use(baddr) (iommu_use[dvma_index(baddr)])
45 struct list_head list
;
48 static struct list_head hole_list
;
49 static struct list_head hole_cache
;
50 static struct hole initholes
[64];
54 static unsigned long dvma_allocs
;
55 static unsigned long dvma_frees
;
56 static unsigned long long dvma_alloc_bytes
;
57 static unsigned long long dvma_free_bytes
;
59 static void print_use(void)
65 printk("dvma entry usage:\n");
67 for(i
= 0; i
< IOMMU_TOTAL_ENTRIES
; i
++) {
73 printk("dvma entry: %08lx len %08lx\n",
74 ( i
<< DVMA_PAGE_SHIFT
) + DVMA_START
,
78 printk("%d entries in use total\n", j
);
80 printk("allocation/free calls: %lu/%lu\n", dvma_allocs
, dvma_frees
);
81 printk("allocation/free bytes: %Lx/%Lx\n", dvma_alloc_bytes
,
85 static void print_holes(struct list_head
*holes
)
88 struct list_head
*cur
;
91 printk("listing dvma holes\n");
92 list_for_each(cur
, holes
) {
93 hole
= list_entry(cur
, struct hole
, list
);
95 if((hole
->start
== 0) && (hole
->end
== 0) && (hole
->size
== 0))
98 printk("hole: start %08lx end %08lx size %08lx\n", hole
->start
, hole
->end
, hole
->size
);
101 printk("end of hole listing...\n");
104 #endif /* DVMA_DEBUG */
106 static inline int refill(void)
110 struct hole
*prev
= NULL
;
111 struct list_head
*cur
;
114 list_for_each(cur
, &hole_list
) {
115 hole
= list_entry(cur
, struct hole
, list
);
122 if(hole
->end
== prev
->start
) {
123 hole
->size
+= prev
->size
;
124 hole
->end
= prev
->end
;
125 list_move(&(prev
->list
), &hole_cache
);
134 static inline struct hole
*rmcache(void)
138 if(list_empty(&hole_cache
)) {
140 printk("out of dvma hole cache!\n");
145 ret
= list_entry(hole_cache
.next
, struct hole
, list
);
146 list_del(&(ret
->list
));
152 static inline unsigned long get_baddr(int len
, unsigned long align
)
155 struct list_head
*cur
;
158 if(list_empty(&hole_list
)) {
160 printk("out of dvma holes! (printing hole cache)\n");
161 print_holes(&hole_cache
);
167 list_for_each(cur
, &hole_list
) {
168 unsigned long newlen
;
170 hole
= list_entry(cur
, struct hole
, list
);
172 if(align
> DVMA_PAGE_SIZE
)
173 newlen
= len
+ ((hole
->end
- len
) & (align
-1));
177 if(hole
->size
> newlen
) {
179 hole
->size
-= newlen
;
180 dvma_entry_use(hole
->end
) = newlen
;
183 dvma_alloc_bytes
+= newlen
;
186 } else if(hole
->size
== newlen
) {
187 list_move(&(hole
->list
), &hole_cache
);
188 dvma_entry_use(hole
->start
) = newlen
;
191 dvma_alloc_bytes
+= newlen
;
198 printk("unable to find dvma hole!\n");
203 static inline int free_baddr(unsigned long baddr
)
208 struct list_head
*cur
;
209 unsigned long orig_baddr
;
212 len
= dvma_entry_use(baddr
);
213 dvma_entry_use(baddr
) = 0;
214 baddr
&= DVMA_PAGE_MASK
;
215 dvma_unmap_iommu(baddr
, len
);
219 dvma_free_bytes
+= len
;
222 list_for_each(cur
, &hole_list
) {
223 hole
= list_entry(cur
, struct hole
, list
);
225 if(hole
->end
== baddr
) {
229 } else if(hole
->start
== (baddr
+ len
)) {
240 hole
->end
= baddr
+ len
;
243 // list_add_tail(&(hole->list), cur);
244 list_add(&(hole
->list
), cur
);
250 void __init
dvma_init(void)
256 INIT_LIST_HEAD(&hole_list
);
257 INIT_LIST_HEAD(&hole_cache
);
259 /* prepare the hole cache */
260 for(i
= 0; i
< 64; i
++)
261 list_add(&(initholes
[i
].list
), &hole_cache
);
264 hole
->start
= DVMA_START
;
265 hole
->end
= DVMA_END
;
266 hole
->size
= DVMA_SIZE
;
268 list_add(&(hole
->list
), &hole_list
);
270 iommu_use
= alloc_bootmem(IOMMU_TOTAL_ENTRIES
* sizeof(unsigned long));
272 dvma_unmap_iommu(DVMA_START
, DVMA_SIZE
);
280 unsigned long dvma_map_align(unsigned long kaddr
, int len
, int align
)
290 // printk("error: kaddr %lx len %x\n", kaddr, len);
296 printk("dvma_map request %08lx bytes from %08lx\n",
299 off
= kaddr
& ~DVMA_PAGE_MASK
;
302 len
= ((len
+ (DVMA_PAGE_SIZE
-1)) & DVMA_PAGE_MASK
);
305 align
= DVMA_PAGE_SIZE
;
307 align
= ((align
+ (DVMA_PAGE_SIZE
-1)) & DVMA_PAGE_MASK
);
309 baddr
= get_baddr(len
, align
);
310 // printk("using baddr %lx\n", baddr);
312 if(!dvma_map_iommu(kaddr
, baddr
, len
))
313 return (baddr
+ off
);
315 printk("dvma_map failed kaddr %lx baddr %lx len %x\n", kaddr
, baddr
, len
);
319 EXPORT_SYMBOL(dvma_map_align
);
321 void dvma_unmap(void *baddr
)
325 addr
= (unsigned long)baddr
;
326 /* check if this is a vme mapping */
327 if(!(addr
& 0x00f00000))
335 EXPORT_SYMBOL(dvma_unmap
);
337 void *dvma_malloc_align(unsigned long len
, unsigned long align
)
347 printk("dvma_malloc request %lx bytes\n", len
);
349 len
= ((len
+ (DVMA_PAGE_SIZE
-1)) & DVMA_PAGE_MASK
);
351 if((kaddr
= __get_free_pages(GFP_ATOMIC
, get_order(len
))) == 0)
354 if((baddr
= (unsigned long)dvma_map_align(kaddr
, len
, align
)) == 0) {
355 free_pages(kaddr
, get_order(len
));
359 vaddr
= dvma_btov(baddr
);
361 if(dvma_map_cpu(kaddr
, vaddr
, len
) < 0) {
362 dvma_unmap((void *)baddr
);
363 free_pages(kaddr
, get_order(len
));
368 printk("mapped %08lx bytes %08lx kern -> %08lx bus\n",
372 return (void *)vaddr
;
375 EXPORT_SYMBOL(dvma_malloc_align
);
377 void dvma_free(void *vaddr
)
383 EXPORT_SYMBOL(dvma_free
);