1 // SPDX-License-Identifier: GPL-2.0
3 * linux/arch/m68k/sun3/sun3dvma.c
5 * Copyright (C) 2000 Sam Creasey
7 * Contains common routines for sun3/sun3x DVMA management.
10 #include <linux/memblock.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/gfp.h>
16 #include <linux/list.h>
23 static unsigned long *iommu_use;
25 #define dvma_index(baddr) ((baddr - DVMA_START) >> DVMA_PAGE_SHIFT)
27 #define dvma_entry_use(baddr) (iommu_use[dvma_index(baddr)])
33 struct list_head list;
36 static struct list_head hole_list;
37 static struct list_head hole_cache;
38 static struct hole initholes[64];
42 static unsigned long dvma_allocs;
43 static unsigned long dvma_frees;
44 static unsigned long long dvma_alloc_bytes;
45 static unsigned long long dvma_free_bytes;
47 static void print_use(void)
53 pr_info("dvma entry usage:\n");
55 for(i = 0; i < IOMMU_TOTAL_ENTRIES; i++) {
61 pr_info("dvma entry: %08x len %08lx\n",
62 (i << DVMA_PAGE_SHIFT) + DVMA_START, iommu_use[i]);
65 pr_info("%d entries in use total\n", j);
67 pr_info("allocation/free calls: %lu/%lu\n", dvma_allocs, dvma_frees);
68 pr_info("allocation/free bytes: %Lx/%Lx\n", dvma_alloc_bytes,
72 static void print_holes(struct list_head *holes)
75 struct list_head *cur;
78 pr_info("listing dvma holes\n");
79 list_for_each(cur, holes) {
80 hole = list_entry(cur, struct hole, list);
82 if((hole->start == 0) && (hole->end == 0) && (hole->size == 0))
85 pr_info("hole: start %08lx end %08lx size %08lx\n",
86 hole->start, hole->end, hole->size);
89 pr_info("end of hole listing...\n");
91 #endif /* DVMA_DEBUG */
93 static inline int refill(void)
97 struct hole *prev = NULL;
98 struct list_head *cur;
101 list_for_each(cur, &hole_list) {
102 hole = list_entry(cur, struct hole, list);
109 if(hole->end == prev->start) {
110 hole->size += prev->size;
111 hole->end = prev->end;
112 list_move(&(prev->list), &hole_cache);
121 static inline struct hole *rmcache(void)
125 if(list_empty(&hole_cache)) {
127 pr_crit("out of dvma hole cache!\n");
132 ret = list_entry(hole_cache.next, struct hole, list);
133 list_del(&(ret->list));
139 static inline unsigned long get_baddr(int len, unsigned long align)
142 struct list_head *cur;
145 if(list_empty(&hole_list)) {
147 pr_crit("out of dvma holes! (printing hole cache)\n");
148 print_holes(&hole_cache);
154 list_for_each(cur, &hole_list) {
155 unsigned long newlen;
157 hole = list_entry(cur, struct hole, list);
159 if(align > DVMA_PAGE_SIZE)
160 newlen = len + ((hole->end - len) & (align-1));
164 if(hole->size > newlen) {
166 hole->size -= newlen;
167 dvma_entry_use(hole->end) = newlen;
170 dvma_alloc_bytes += newlen;
173 } else if(hole->size == newlen) {
174 list_move(&(hole->list), &hole_cache);
175 dvma_entry_use(hole->start) = newlen;
178 dvma_alloc_bytes += newlen;
185 pr_crit("unable to find dvma hole!\n");
190 static inline int free_baddr(unsigned long baddr)
195 struct list_head *cur;
197 len = dvma_entry_use(baddr);
198 dvma_entry_use(baddr) = 0;
199 baddr &= DVMA_PAGE_MASK;
200 dvma_unmap_iommu(baddr, len);
204 dvma_free_bytes += len;
207 list_for_each(cur, &hole_list) {
208 hole = list_entry(cur, struct hole, list);
210 if(hole->end == baddr) {
214 } else if(hole->start == (baddr + len)) {
225 hole->end = baddr + len;
228 // list_add_tail(&(hole->list), cur);
229 list_add(&(hole->list), cur);
235 void __init dvma_init(void)
241 INIT_LIST_HEAD(&hole_list);
242 INIT_LIST_HEAD(&hole_cache);
244 /* prepare the hole cache */
245 for(i = 0; i < 64; i++)
246 list_add(&(initholes[i].list), &hole_cache);
249 hole->start = DVMA_START;
250 hole->end = DVMA_END;
251 hole->size = DVMA_SIZE;
253 list_add(&(hole->list), &hole_list);
255 iommu_use = memblock_alloc(IOMMU_TOTAL_ENTRIES * sizeof(unsigned long),
258 panic("%s: Failed to allocate %zu bytes\n", __func__,
259 IOMMU_TOTAL_ENTRIES * sizeof(unsigned long));
261 dvma_unmap_iommu(DVMA_START, DVMA_SIZE);
266 unsigned long dvma_map_align(unsigned long kaddr, int len, int align)
276 // pr_err("error: kaddr %lx len %x\n", kaddr, len);
281 pr_debug("dvma_map request %08x bytes from %08lx\n", len, kaddr);
282 off = kaddr & ~DVMA_PAGE_MASK;
285 len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
288 align = DVMA_PAGE_SIZE;
290 align = ((align + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
292 baddr = get_baddr(len, align);
293 // pr_info("using baddr %lx\n", baddr);
295 if(!dvma_map_iommu(kaddr, baddr, len))
296 return (baddr + off);
298 pr_crit("dvma_map failed kaddr %lx baddr %lx len %x\n", kaddr, baddr,
303 EXPORT_SYMBOL(dvma_map_align);
305 void dvma_unmap(void *baddr)
309 addr = (unsigned long)baddr;
310 /* check if this is a vme mapping */
311 if(!(addr & 0x00f00000))
319 EXPORT_SYMBOL(dvma_unmap);
321 void *dvma_malloc_align(unsigned long len, unsigned long align)
330 pr_debug("dvma_malloc request %lx bytes\n", len);
331 len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
333 if((kaddr = __get_free_pages(GFP_ATOMIC, get_order(len))) == 0)
336 if((baddr = (unsigned long)dvma_map_align(kaddr, len, align)) == 0) {
337 free_pages(kaddr, get_order(len));
341 vaddr = dvma_btov(baddr);
343 if(dvma_map_cpu(kaddr, vaddr, len) < 0) {
344 dvma_unmap((void *)baddr);
345 free_pages(kaddr, get_order(len));
349 pr_debug("mapped %08lx bytes %08lx kern -> %08lx bus\n", len, kaddr,
352 return (void *)vaddr;
355 EXPORT_SYMBOL(dvma_malloc_align);
357 void dvma_free(void *vaddr)
363 EXPORT_SYMBOL(dvma_free);