blob: ca0966cac72adfe68cf6eef8b680c395ee7eea5d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/arch/m68k/sun3/sun3dvma.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2000 Sam Creasey
5 *
6 * Contains common routines for sun3/sun3x DVMA management.
7 */
8
Al Viro2e811482006-10-11 17:28:27 +01009#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mm.h>
13#include <linux/list.h>
14
15#include <asm/page.h>
16#include <asm/pgtable.h>
17#include <asm/dvma.h>
18
19#undef DVMA_DEBUG
20
21#ifdef CONFIG_SUN3X
22extern void dvma_unmap_iommu(unsigned long baddr, int len);
23#else
24static inline void dvma_unmap_iommu(unsigned long a, int b)
25{
26}
27#endif
28
29#ifdef CONFIG_SUN3
30extern void sun3_dvma_init(void);
31#endif
32
Adrian Bunk07b81252008-07-17 21:16:27 +020033static unsigned long iommu_use[IOMMU_TOTAL_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#define dvma_index(baddr) ((baddr - DVMA_START) >> DVMA_PAGE_SHIFT)
36
37#define dvma_entry_use(baddr) (iommu_use[dvma_index(baddr)])
38
39struct hole {
40 unsigned long start;
41 unsigned long end;
42 unsigned long size;
43 struct list_head list;
44};
45
46static struct list_head hole_list;
47static struct list_head hole_cache;
48static struct hole initholes[64];
49
50#ifdef DVMA_DEBUG
51
52static unsigned long dvma_allocs;
53static unsigned long dvma_frees;
54static unsigned long long dvma_alloc_bytes;
55static unsigned long long dvma_free_bytes;
56
57static void print_use(void)
58{
59
60 int i;
61 int j = 0;
62
63 printk("dvma entry usage:\n");
64
65 for(i = 0; i < IOMMU_TOTAL_ENTRIES; i++) {
66 if(!iommu_use[i])
67 continue;
68
69 j++;
70
71 printk("dvma entry: %08lx len %08lx\n",
72 ( i << DVMA_PAGE_SHIFT) + DVMA_START,
73 iommu_use[i]);
74 }
75
76 printk("%d entries in use total\n", j);
77
78 printk("allocation/free calls: %lu/%lu\n", dvma_allocs, dvma_frees);
79 printk("allocation/free bytes: %Lx/%Lx\n", dvma_alloc_bytes,
80 dvma_free_bytes);
81}
82
83static void print_holes(struct list_head *holes)
84{
85
86 struct list_head *cur;
87 struct hole *hole;
88
89 printk("listing dvma holes\n");
90 list_for_each(cur, holes) {
91 hole = list_entry(cur, struct hole, list);
92
93 if((hole->start == 0) && (hole->end == 0) && (hole->size == 0))
94 continue;
95
96 printk("hole: start %08lx end %08lx size %08lx\n", hole->start, hole->end, hole->size);
97 }
98
99 printk("end of hole listing...\n");
100
101}
102#endif /* DVMA_DEBUG */
103
104static inline int refill(void)
105{
106
107 struct hole *hole;
108 struct hole *prev = NULL;
109 struct list_head *cur;
110 int ret = 0;
111
112 list_for_each(cur, &hole_list) {
113 hole = list_entry(cur, struct hole, list);
114
115 if(!prev) {
116 prev = hole;
117 continue;
118 }
119
120 if(hole->end == prev->start) {
121 hole->size += prev->size;
122 hole->end = prev->end;
Akinobu Mitaa7addce2006-06-26 00:24:39 -0700123 list_move(&(prev->list), &hole_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 ret++;
125 }
126
127 }
128
129 return ret;
130}
131
132static inline struct hole *rmcache(void)
133{
134 struct hole *ret;
135
136 if(list_empty(&hole_cache)) {
137 if(!refill()) {
138 printk("out of dvma hole cache!\n");
139 BUG();
140 }
141 }
142
143 ret = list_entry(hole_cache.next, struct hole, list);
144 list_del(&(ret->list));
145
146 return ret;
147
148}
149
150static inline unsigned long get_baddr(int len, unsigned long align)
151{
152
153 struct list_head *cur;
154 struct hole *hole;
155
156 if(list_empty(&hole_list)) {
157#ifdef DVMA_DEBUG
158 printk("out of dvma holes! (printing hole cache)\n");
159 print_holes(&hole_cache);
160 print_use();
161#endif
162 BUG();
163 }
164
165 list_for_each(cur, &hole_list) {
166 unsigned long newlen;
167
168 hole = list_entry(cur, struct hole, list);
169
170 if(align > DVMA_PAGE_SIZE)
171 newlen = len + ((hole->end - len) & (align-1));
172 else
173 newlen = len;
174
175 if(hole->size > newlen) {
176 hole->end -= newlen;
177 hole->size -= newlen;
178 dvma_entry_use(hole->end) = newlen;
179#ifdef DVMA_DEBUG
180 dvma_allocs++;
181 dvma_alloc_bytes += newlen;
182#endif
183 return hole->end;
184 } else if(hole->size == newlen) {
Akinobu Mitaa7addce2006-06-26 00:24:39 -0700185 list_move(&(hole->list), &hole_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 dvma_entry_use(hole->start) = newlen;
187#ifdef DVMA_DEBUG
188 dvma_allocs++;
189 dvma_alloc_bytes += newlen;
190#endif
191 return hole->start;
192 }
193
194 }
195
196 printk("unable to find dvma hole!\n");
197 BUG();
198 return 0;
199}
200
201static inline int free_baddr(unsigned long baddr)
202{
203
204 unsigned long len;
205 struct hole *hole;
206 struct list_head *cur;
207 unsigned long orig_baddr;
208
209 orig_baddr = baddr;
210 len = dvma_entry_use(baddr);
211 dvma_entry_use(baddr) = 0;
212 baddr &= DVMA_PAGE_MASK;
213 dvma_unmap_iommu(baddr, len);
214
215#ifdef DVMA_DEBUG
216 dvma_frees++;
217 dvma_free_bytes += len;
218#endif
219
220 list_for_each(cur, &hole_list) {
221 hole = list_entry(cur, struct hole, list);
222
223 if(hole->end == baddr) {
224 hole->end += len;
225 hole->size += len;
226 return 0;
227 } else if(hole->start == (baddr + len)) {
228 hole->start = baddr;
229 hole->size += len;
230 return 0;
231 }
232
233 }
234
235 hole = rmcache();
236
237 hole->start = baddr;
238 hole->end = baddr + len;
239 hole->size = len;
240
241// list_add_tail(&(hole->list), cur);
242 list_add(&(hole->list), cur);
243
244 return 0;
245
246}
247
248void dvma_init(void)
249{
250
251 struct hole *hole;
252 int i;
253
254 INIT_LIST_HEAD(&hole_list);
255 INIT_LIST_HEAD(&hole_cache);
256
257 /* prepare the hole cache */
258 for(i = 0; i < 64; i++)
259 list_add(&(initholes[i].list), &hole_cache);
260
261 hole = rmcache();
262 hole->start = DVMA_START;
263 hole->end = DVMA_END;
264 hole->size = DVMA_SIZE;
265
266 list_add(&(hole->list), &hole_list);
267
268 memset(iommu_use, 0, sizeof(iommu_use));
269
270 dvma_unmap_iommu(DVMA_START, DVMA_SIZE);
271
272#ifdef CONFIG_SUN3
273 sun3_dvma_init();
274#endif
275
276}
277
278inline unsigned long dvma_map_align(unsigned long kaddr, int len, int align)
279{
280
281 unsigned long baddr;
282 unsigned long off;
283
284 if(!len)
285 len = 0x800;
286
287 if(!kaddr || !len) {
288// printk("error: kaddr %lx len %x\n", kaddr, len);
289// *(int *)4 = 0;
290 return 0;
291 }
292
293#ifdef DEBUG
294 printk("dvma_map request %08lx bytes from %08lx\n",
295 len, kaddr);
296#endif
297 off = kaddr & ~DVMA_PAGE_MASK;
298 kaddr &= PAGE_MASK;
299 len += off;
300 len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
301
302 if(align == 0)
303 align = DVMA_PAGE_SIZE;
304 else
305 align = ((align + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
306
307 baddr = get_baddr(len, align);
308// printk("using baddr %lx\n", baddr);
309
310 if(!dvma_map_iommu(kaddr, baddr, len))
311 return (baddr + off);
312
313 printk("dvma_map failed kaddr %lx baddr %lx len %x\n", kaddr, baddr, len);
314 BUG();
315 return 0;
316}
Al Viro2e811482006-10-11 17:28:27 +0100317EXPORT_SYMBOL(dvma_map_align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319void dvma_unmap(void *baddr)
320{
321 unsigned long addr;
322
323 addr = (unsigned long)baddr;
324 /* check if this is a vme mapping */
325 if(!(addr & 0x00f00000))
326 addr |= 0xf00000;
327
328 free_baddr(addr);
329
330 return;
331
332}
Al Viro2e811482006-10-11 17:28:27 +0100333EXPORT_SYMBOL(dvma_unmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335void *dvma_malloc_align(unsigned long len, unsigned long align)
336{
337 unsigned long kaddr;
338 unsigned long baddr;
339 unsigned long vaddr;
340
341 if(!len)
342 return NULL;
343
344#ifdef DEBUG
345 printk("dvma_malloc request %lx bytes\n", len);
346#endif
347 len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
348
349 if((kaddr = __get_free_pages(GFP_ATOMIC, get_order(len))) == 0)
350 return NULL;
351
352 if((baddr = (unsigned long)dvma_map_align(kaddr, len, align)) == 0) {
353 free_pages(kaddr, get_order(len));
354 return NULL;
355 }
356
357 vaddr = dvma_btov(baddr);
358
359 if(dvma_map_cpu(kaddr, vaddr, len) < 0) {
360 dvma_unmap((void *)baddr);
361 free_pages(kaddr, get_order(len));
362 return NULL;
363 }
364
365#ifdef DEBUG
366 printk("mapped %08lx bytes %08lx kern -> %08lx bus\n",
367 len, kaddr, baddr);
368#endif
369
370 return (void *)vaddr;
371
372}
Al Viro2e811482006-10-11 17:28:27 +0100373EXPORT_SYMBOL(dvma_malloc_align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375void dvma_free(void *vaddr)
376{
377
378 return;
379
380}
Al Viro2e811482006-10-11 17:28:27 +0100381EXPORT_SYMBOL(dvma_free);