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>
12 #include <linux/list.h>
15 #include <asm/pgtable.h>
21 extern void dvma_unmap_iommu(unsigned long baddr
, int len
);
23 static inline void dvma_unmap_iommu(unsigned long a
, int b
)
29 extern void sun3_dvma_init(void);
32 static 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)])
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];
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)
62 printk("dvma entry usage:\n");
64 for(i
= 0; i
< IOMMU_TOTAL_ENTRIES
; i
++) {
70 printk("dvma entry: %08lx len %08lx\n",
71 ( i
<< DVMA_PAGE_SHIFT
) + DVMA_START
,
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
,
82 static void print_holes(struct list_head
*holes
)
85 struct list_head
*cur
;
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))
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)
107 struct hole
*prev
= NULL
;
108 struct list_head
*cur
;
111 list_for_each(cur
, &hole_list
) {
112 hole
= list_entry(cur
, struct hole
, list
);
119 if(hole
->end
== prev
->start
) {
120 hole
->size
+= prev
->size
;
121 hole
->end
= prev
->end
;
122 list_move(&(prev
->list
), &hole_cache
);
131 static inline struct hole
*rmcache(void)
135 if(list_empty(&hole_cache
)) {
137 printk("out of dvma hole cache!\n");
142 ret
= list_entry(hole_cache
.next
, struct hole
, list
);
143 list_del(&(ret
->list
));
149 static inline unsigned long get_baddr(int len
, unsigned long align
)
152 struct list_head
*cur
;
155 if(list_empty(&hole_list
)) {
157 printk("out of dvma holes! (printing hole cache)\n");
158 print_holes(&hole_cache
);
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));
174 if(hole
->size
> newlen
) {
176 hole
->size
-= newlen
;
177 dvma_entry_use(hole
->end
) = newlen
;
180 dvma_alloc_bytes
+= newlen
;
183 } else if(hole
->size
== newlen
) {
184 list_move(&(hole
->list
), &hole_cache
);
185 dvma_entry_use(hole
->start
) = newlen
;
188 dvma_alloc_bytes
+= newlen
;
195 printk("unable to find dvma hole!\n");
200 static inline int free_baddr(unsigned long baddr
)
205 struct list_head
*cur
;
206 unsigned long orig_baddr
;
209 len
= dvma_entry_use(baddr
);
210 dvma_entry_use(baddr
) = 0;
211 baddr
&= DVMA_PAGE_MASK
;
212 dvma_unmap_iommu(baddr
, len
);
216 dvma_free_bytes
+= len
;
219 list_for_each(cur
, &hole_list
) {
220 hole
= list_entry(cur
, struct hole
, list
);
222 if(hole
->end
== baddr
) {
226 } else if(hole
->start
== (baddr
+ len
)) {
237 hole
->end
= baddr
+ len
;
240 // list_add_tail(&(hole->list), cur);
241 list_add(&(hole
->list
), cur
);
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
);
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
);
277 inline unsigned long dvma_map_align(unsigned long kaddr
, int len
, int align
)
287 // printk("error: kaddr %lx len %x\n", kaddr, len);
293 printk("dvma_map request %08lx bytes from %08lx\n",
296 off
= kaddr
& ~DVMA_PAGE_MASK
;
299 len
= ((len
+ (DVMA_PAGE_SIZE
-1)) & DVMA_PAGE_MASK
);
302 align
= DVMA_PAGE_SIZE
;
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
);
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
)
344 printk("dvma_malloc request %lx bytes\n", len
);
346 len
= ((len
+ (DVMA_PAGE_SIZE
-1)) & DVMA_PAGE_MASK
);
348 if((kaddr
= __get_free_pages(GFP_ATOMIC
, get_order(len
))) == 0)
351 if((baddr
= (unsigned long)dvma_map_align(kaddr
, len
, align
)) == 0) {
352 free_pages(kaddr
, get_order(len
));
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
));
365 printk("mapped %08lx bytes %08lx kern -> %08lx bus\n",
369 return (void *)vaddr
;
372 EXPORT_SYMBOL(dvma_malloc_align
);
374 void dvma_free(void *vaddr
)
380 EXPORT_SYMBOL(dvma_free
);