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/module.h>
10 #include <linux/kernel.h>
11 #include <linux/gfp.h>
13 #include <linux/list.h>
16 #include <asm/pgtable.h>
22 extern void dvma_unmap_iommu(unsigned long baddr
, int len
);
24 static inline void dvma_unmap_iommu(unsigned long a
, int b
)
30 extern void sun3_dvma_init(void);
33 static unsigned long iommu_use
[IOMMU_TOTAL_ENTRIES
];
35 #define dvma_index(baddr) ((baddr - DVMA_START) >> DVMA_PAGE_SHIFT)
37 #define dvma_entry_use(baddr) (iommu_use[dvma_index(baddr)])
43 struct list_head list
;
46 static struct list_head hole_list
;
47 static struct list_head hole_cache
;
48 static struct hole initholes
[64];
52 static unsigned long dvma_allocs
;
53 static unsigned long dvma_frees
;
54 static unsigned long long dvma_alloc_bytes
;
55 static unsigned long long dvma_free_bytes
;
57 static void print_use(void)
63 printk("dvma entry usage:\n");
65 for(i
= 0; i
< IOMMU_TOTAL_ENTRIES
; i
++) {
71 printk("dvma entry: %08lx len %08lx\n",
72 ( i
<< DVMA_PAGE_SHIFT
) + DVMA_START
,
76 printk("%d entries in use total\n", j
);
78 printk("allocation/free calls: %lu/%lu\n", dvma_allocs
, dvma_frees
);
79 printk("allocation/free bytes: %Lx/%Lx\n", dvma_alloc_bytes
,
83 static void print_holes(struct list_head
*holes
)
86 struct list_head
*cur
;
89 printk("listing dvma holes\n");
90 list_for_each(cur
, holes
) {
91 hole
= list_entry(cur
, struct hole
, list
);
93 if((hole
->start
== 0) && (hole
->end
== 0) && (hole
->size
== 0))
96 printk("hole: start %08lx end %08lx size %08lx\n", hole
->start
, hole
->end
, hole
->size
);
99 printk("end of hole listing...\n");
102 #endif /* DVMA_DEBUG */
104 static inline int refill(void)
108 struct hole
*prev
= NULL
;
109 struct list_head
*cur
;
112 list_for_each(cur
, &hole_list
) {
113 hole
= list_entry(cur
, struct hole
, list
);
120 if(hole
->end
== prev
->start
) {
121 hole
->size
+= prev
->size
;
122 hole
->end
= prev
->end
;
123 list_move(&(prev
->list
), &hole_cache
);
132 static inline struct hole
*rmcache(void)
136 if(list_empty(&hole_cache
)) {
138 printk("out of dvma hole cache!\n");
143 ret
= list_entry(hole_cache
.next
, struct hole
, list
);
144 list_del(&(ret
->list
));
150 static inline unsigned long get_baddr(int len
, unsigned long align
)
153 struct list_head
*cur
;
156 if(list_empty(&hole_list
)) {
158 printk("out of dvma holes! (printing hole cache)\n");
159 print_holes(&hole_cache
);
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));
175 if(hole
->size
> newlen
) {
177 hole
->size
-= newlen
;
178 dvma_entry_use(hole
->end
) = newlen
;
181 dvma_alloc_bytes
+= newlen
;
184 } else if(hole
->size
== newlen
) {
185 list_move(&(hole
->list
), &hole_cache
);
186 dvma_entry_use(hole
->start
) = newlen
;
189 dvma_alloc_bytes
+= newlen
;
196 printk("unable to find dvma hole!\n");
201 static inline int free_baddr(unsigned long baddr
)
206 struct list_head
*cur
;
207 unsigned long orig_baddr
;
210 len
= dvma_entry_use(baddr
);
211 dvma_entry_use(baddr
) = 0;
212 baddr
&= DVMA_PAGE_MASK
;
213 dvma_unmap_iommu(baddr
, len
);
217 dvma_free_bytes
+= len
;
220 list_for_each(cur
, &hole_list
) {
221 hole
= list_entry(cur
, struct hole
, list
);
223 if(hole
->end
== baddr
) {
227 } else if(hole
->start
== (baddr
+ len
)) {
238 hole
->end
= baddr
+ len
;
241 // list_add_tail(&(hole->list), cur);
242 list_add(&(hole
->list
), cur
);
254 INIT_LIST_HEAD(&hole_list
);
255 INIT_LIST_HEAD(&hole_cache
);
257 /* prepare the hole cache */
258 for(i
= 0; i
< 64; i
++)
259 list_add(&(initholes
[i
].list
), &hole_cache
);
262 hole
->start
= DVMA_START
;
263 hole
->end
= DVMA_END
;
264 hole
->size
= DVMA_SIZE
;
266 list_add(&(hole
->list
), &hole_list
);
268 memset(iommu_use
, 0, sizeof(iommu_use
));
270 dvma_unmap_iommu(DVMA_START
, DVMA_SIZE
);
278 inline unsigned long dvma_map_align(unsigned long kaddr
, int len
, int align
)
288 // printk("error: kaddr %lx len %x\n", kaddr, len);
294 printk("dvma_map request %08lx bytes from %08lx\n",
297 off
= kaddr
& ~DVMA_PAGE_MASK
;
300 len
= ((len
+ (DVMA_PAGE_SIZE
-1)) & DVMA_PAGE_MASK
);
303 align
= DVMA_PAGE_SIZE
;
305 align
= ((align
+ (DVMA_PAGE_SIZE
-1)) & DVMA_PAGE_MASK
);
307 baddr
= get_baddr(len
, align
);
308 // printk("using baddr %lx\n", baddr);
310 if(!dvma_map_iommu(kaddr
, baddr
, len
))
311 return (baddr
+ off
);
313 printk("dvma_map failed kaddr %lx baddr %lx len %x\n", kaddr
, baddr
, len
);
317 EXPORT_SYMBOL(dvma_map_align
);
319 void dvma_unmap(void *baddr
)
323 addr
= (unsigned long)baddr
;
324 /* check if this is a vme mapping */
325 if(!(addr
& 0x00f00000))
333 EXPORT_SYMBOL(dvma_unmap
);
335 void *dvma_malloc_align(unsigned long len
, unsigned long align
)
345 printk("dvma_malloc request %lx bytes\n", len
);
347 len
= ((len
+ (DVMA_PAGE_SIZE
-1)) & DVMA_PAGE_MASK
);
349 if((kaddr
= __get_free_pages(GFP_ATOMIC
, get_order(len
))) == 0)
352 if((baddr
= (unsigned long)dvma_map_align(kaddr
, len
, align
)) == 0) {
353 free_pages(kaddr
, get_order(len
));
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
));
366 printk("mapped %08lx bytes %08lx kern -> %08lx bus\n",
370 return (void *)vaddr
;
373 EXPORT_SYMBOL(dvma_malloc_align
);
375 void dvma_free(void *vaddr
)
381 EXPORT_SYMBOL(dvma_free
);