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 pr_info("dvma entry usage:\n");
67 for(i
= 0; i
< IOMMU_TOTAL_ENTRIES
; i
++) {
73 pr_info("dvma entry: %08x len %08lx\n",
74 (i
<< DVMA_PAGE_SHIFT
) + DVMA_START
, iommu_use
[i
]);
77 pr_info("%d entries in use total\n", j
);
79 pr_info("allocation/free calls: %lu/%lu\n", dvma_allocs
, dvma_frees
);
80 pr_info("allocation/free bytes: %Lx/%Lx\n", dvma_alloc_bytes
,
84 static void print_holes(struct list_head
*holes
)
87 struct list_head
*cur
;
90 pr_info("listing dvma holes\n");
91 list_for_each(cur
, holes
) {
92 hole
= list_entry(cur
, struct hole
, list
);
94 if((hole
->start
== 0) && (hole
->end
== 0) && (hole
->size
== 0))
97 pr_info("hole: start %08lx end %08lx size %08lx\n",
98 hole
->start
, hole
->end
, hole
->size
);
101 pr_info("end of hole listing...\n");
103 #endif /* DVMA_DEBUG */
105 static inline int refill(void)
109 struct hole
*prev
= NULL
;
110 struct list_head
*cur
;
113 list_for_each(cur
, &hole_list
) {
114 hole
= list_entry(cur
, struct hole
, list
);
121 if(hole
->end
== prev
->start
) {
122 hole
->size
+= prev
->size
;
123 hole
->end
= prev
->end
;
124 list_move(&(prev
->list
), &hole_cache
);
133 static inline struct hole
*rmcache(void)
137 if(list_empty(&hole_cache
)) {
139 pr_crit("out of dvma hole cache!\n");
144 ret
= list_entry(hole_cache
.next
, struct hole
, list
);
145 list_del(&(ret
->list
));
151 static inline unsigned long get_baddr(int len
, unsigned long align
)
154 struct list_head
*cur
;
157 if(list_empty(&hole_list
)) {
159 pr_crit("out of dvma holes! (printing hole cache)\n");
160 print_holes(&hole_cache
);
166 list_for_each(cur
, &hole_list
) {
167 unsigned long newlen
;
169 hole
= list_entry(cur
, struct hole
, list
);
171 if(align
> DVMA_PAGE_SIZE
)
172 newlen
= len
+ ((hole
->end
- len
) & (align
-1));
176 if(hole
->size
> newlen
) {
178 hole
->size
-= newlen
;
179 dvma_entry_use(hole
->end
) = newlen
;
182 dvma_alloc_bytes
+= newlen
;
185 } else if(hole
->size
== newlen
) {
186 list_move(&(hole
->list
), &hole_cache
);
187 dvma_entry_use(hole
->start
) = newlen
;
190 dvma_alloc_bytes
+= newlen
;
197 pr_crit("unable to find dvma hole!\n");
202 static inline int free_baddr(unsigned long baddr
)
207 struct list_head
*cur
;
208 unsigned long orig_baddr
;
211 len
= dvma_entry_use(baddr
);
212 dvma_entry_use(baddr
) = 0;
213 baddr
&= DVMA_PAGE_MASK
;
214 dvma_unmap_iommu(baddr
, len
);
218 dvma_free_bytes
+= len
;
221 list_for_each(cur
, &hole_list
) {
222 hole
= list_entry(cur
, struct hole
, list
);
224 if(hole
->end
== baddr
) {
228 } else if(hole
->start
== (baddr
+ len
)) {
239 hole
->end
= baddr
+ len
;
242 // list_add_tail(&(hole->list), cur);
243 list_add(&(hole
->list
), cur
);
249 void __init
dvma_init(void)
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
);
263 hole
->start
= DVMA_START
;
264 hole
->end
= DVMA_END
;
265 hole
->size
= DVMA_SIZE
;
267 list_add(&(hole
->list
), &hole_list
);
269 iommu_use
= alloc_bootmem(IOMMU_TOTAL_ENTRIES
* sizeof(unsigned long));
271 dvma_unmap_iommu(DVMA_START
, DVMA_SIZE
);
279 unsigned long dvma_map_align(unsigned long kaddr
, int len
, int align
)
289 // pr_err("error: kaddr %lx len %x\n", kaddr, len);
294 pr_debug("dvma_map request %08x bytes from %08lx\n", len
, kaddr
);
295 off
= kaddr
& ~DVMA_PAGE_MASK
;
298 len
= ((len
+ (DVMA_PAGE_SIZE
-1)) & DVMA_PAGE_MASK
);
301 align
= DVMA_PAGE_SIZE
;
303 align
= ((align
+ (DVMA_PAGE_SIZE
-1)) & DVMA_PAGE_MASK
);
305 baddr
= get_baddr(len
, align
);
306 // pr_info("using baddr %lx\n", baddr);
308 if(!dvma_map_iommu(kaddr
, baddr
, len
))
309 return (baddr
+ off
);
311 pr_crit("dvma_map failed kaddr %lx baddr %lx len %x\n", kaddr
, baddr
,
316 EXPORT_SYMBOL(dvma_map_align
);
318 void dvma_unmap(void *baddr
)
322 addr
= (unsigned long)baddr
;
323 /* check if this is a vme mapping */
324 if(!(addr
& 0x00f00000))
332 EXPORT_SYMBOL(dvma_unmap
);
334 void *dvma_malloc_align(unsigned long len
, unsigned long align
)
343 pr_debug("dvma_malloc request %lx bytes\n", len
);
344 len
= ((len
+ (DVMA_PAGE_SIZE
-1)) & DVMA_PAGE_MASK
);
346 if((kaddr
= __get_free_pages(GFP_ATOMIC
, get_order(len
))) == 0)
349 if((baddr
= (unsigned long)dvma_map_align(kaddr
, len
, align
)) == 0) {
350 free_pages(kaddr
, get_order(len
));
354 vaddr
= dvma_btov(baddr
);
356 if(dvma_map_cpu(kaddr
, vaddr
, len
) < 0) {
357 dvma_unmap((void *)baddr
);
358 free_pages(kaddr
, get_order(len
));
362 pr_debug("mapped %08lx bytes %08lx kern -> %08lx bus\n", len
, kaddr
,
365 return (void *)vaddr
;
368 EXPORT_SYMBOL(dvma_malloc_align
);
370 void dvma_free(void *vaddr
)
376 EXPORT_SYMBOL(dvma_free
);