2 * linux/arch/m68k/mm/sun3dvma.c
4 * Copyright (C) 2000 Sam Creasey
6 * Contains common routines for sun3/sun3x DVMA management.
9 #include <linux/config.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 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_del(&(prev
->list
));
123 list_add(&(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_del(&(hole
->list
));
186 list_add(&(hole
->list
), &hole_cache
);
187 dvma_entry_use(hole
->start
) = newlen
;
190 dvma_alloc_bytes
+= newlen
;
197 printk("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
);
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 memset(iommu_use
, 0, sizeof(iommu_use
));
271 dvma_unmap_iommu(DVMA_START
, DVMA_SIZE
);
279 inline unsigned long dvma_map_align(unsigned long kaddr
, int len
, int align
)
289 // printk("error: kaddr %lx len %x\n", kaddr, len);
295 printk("dvma_map request %08lx bytes from %08lx\n",
298 off
= kaddr
& ~DVMA_PAGE_MASK
;
301 len
= ((len
+ (DVMA_PAGE_SIZE
-1)) & DVMA_PAGE_MASK
);
304 align
= DVMA_PAGE_SIZE
;
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
);
319 void dvma_unmap(void *baddr
)
323 addr
= (unsigned long)baddr
;
324 /* check if this is a vme mapping */
325 if(!(addr
& 0x00f00000))
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
;
374 void dvma_free(void *vaddr
)