sh: Fixup TEI IRQ requests in request_dma().
[linux-2.6/verdex.git] / arch / powerpc / lib / rheap.c
blob31e511856dc58bc2b819e80c32a35840db5bd9ca
1 /*
2 * A Remote Heap. Remote means that we don't touch the memory that the
3 * heap points to. Normal heap implementations use the memory they manage
4 * to place their list. We cannot do that because the memory we manage may
5 * have special properties, for example it is uncachable or of different
6 * endianess.
8 * Author: Pantelis Antoniou <panto@intracom.gr>
10 * 2004 (c) INTRACOM S.A. Greece. This file is licensed under
11 * the terms of the GNU General Public License version 2. This program
12 * is licensed "as is" without any warranty of any kind, whether express
13 * or implied.
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/mm.h>
18 #include <linux/slab.h>
20 #include <asm/rheap.h>
23 * Fixup a list_head, needed when copying lists. If the pointers fall
24 * between s and e, apply the delta. This assumes that
25 * sizeof(struct list_head *) == sizeof(unsigned long *).
27 static inline void fixup(unsigned long s, unsigned long e, int d,
28 struct list_head *l)
30 unsigned long *pp;
32 pp = (unsigned long *)&l->next;
33 if (*pp >= s && *pp < e)
34 *pp += d;
36 pp = (unsigned long *)&l->prev;
37 if (*pp >= s && *pp < e)
38 *pp += d;
41 /* Grow the allocated blocks */
42 static int grow(rh_info_t * info, int max_blocks)
44 rh_block_t *block, *blk;
45 int i, new_blocks;
46 int delta;
47 unsigned long blks, blke;
49 if (max_blocks <= info->max_blocks)
50 return -EINVAL;
52 new_blocks = max_blocks - info->max_blocks;
54 block = kmalloc(sizeof(rh_block_t) * max_blocks, GFP_KERNEL);
55 if (block == NULL)
56 return -ENOMEM;
58 if (info->max_blocks > 0) {
60 /* copy old block area */
61 memcpy(block, info->block,
62 sizeof(rh_block_t) * info->max_blocks);
64 delta = (char *)block - (char *)info->block;
66 /* and fixup list pointers */
67 blks = (unsigned long)info->block;
68 blke = (unsigned long)(info->block + info->max_blocks);
70 for (i = 0, blk = block; i < info->max_blocks; i++, blk++)
71 fixup(blks, blke, delta, &blk->list);
73 fixup(blks, blke, delta, &info->empty_list);
74 fixup(blks, blke, delta, &info->free_list);
75 fixup(blks, blke, delta, &info->taken_list);
77 /* free the old allocated memory */
78 if ((info->flags & RHIF_STATIC_BLOCK) == 0)
79 kfree(info->block);
82 info->block = block;
83 info->empty_slots += new_blocks;
84 info->max_blocks = max_blocks;
85 info->flags &= ~RHIF_STATIC_BLOCK;
87 /* add all new blocks to the free list */
88 for (i = 0, blk = block + info->max_blocks; i < new_blocks; i++, blk++)
89 list_add(&blk->list, &info->empty_list);
91 return 0;
95 * Assure at least the required amount of empty slots. If this function
96 * causes a grow in the block area then all pointers kept to the block
97 * area are invalid!
99 static int assure_empty(rh_info_t * info, int slots)
101 int max_blocks;
103 /* This function is not meant to be used to grow uncontrollably */
104 if (slots >= 4)
105 return -EINVAL;
107 /* Enough space */
108 if (info->empty_slots >= slots)
109 return 0;
111 /* Next 16 sized block */
112 max_blocks = ((info->max_blocks + slots) + 15) & ~15;
114 return grow(info, max_blocks);
117 static rh_block_t *get_slot(rh_info_t * info)
119 rh_block_t *blk;
121 /* If no more free slots, and failure to extend. */
122 /* XXX: You should have called assure_empty before */
123 if (info->empty_slots == 0) {
124 printk(KERN_ERR "rh: out of slots; crash is imminent.\n");
125 return NULL;
128 /* Get empty slot to use */
129 blk = list_entry(info->empty_list.next, rh_block_t, list);
130 list_del_init(&blk->list);
131 info->empty_slots--;
133 /* Initialize */
134 blk->start = NULL;
135 blk->size = 0;
136 blk->owner = NULL;
138 return blk;
141 static inline void release_slot(rh_info_t * info, rh_block_t * blk)
143 list_add(&blk->list, &info->empty_list);
144 info->empty_slots++;
147 static void attach_free_block(rh_info_t * info, rh_block_t * blkn)
149 rh_block_t *blk;
150 rh_block_t *before;
151 rh_block_t *after;
152 rh_block_t *next;
153 int size;
154 unsigned long s, e, bs, be;
155 struct list_head *l;
157 /* We assume that they are aligned properly */
158 size = blkn->size;
159 s = (unsigned long)blkn->start;
160 e = s + size;
162 /* Find the blocks immediately before and after the given one
163 * (if any) */
164 before = NULL;
165 after = NULL;
166 next = NULL;
168 list_for_each(l, &info->free_list) {
169 blk = list_entry(l, rh_block_t, list);
171 bs = (unsigned long)blk->start;
172 be = bs + blk->size;
174 if (next == NULL && s >= bs)
175 next = blk;
177 if (be == s)
178 before = blk;
180 if (e == bs)
181 after = blk;
183 /* If both are not null, break now */
184 if (before != NULL && after != NULL)
185 break;
188 /* Now check if they are really adjacent */
189 if (before != NULL && s != (unsigned long)before->start + before->size)
190 before = NULL;
192 if (after != NULL && e != (unsigned long)after->start)
193 after = NULL;
195 /* No coalescing; list insert and return */
196 if (before == NULL && after == NULL) {
198 if (next != NULL)
199 list_add(&blkn->list, &next->list);
200 else
201 list_add(&blkn->list, &info->free_list);
203 return;
206 /* We don't need it anymore */
207 release_slot(info, blkn);
209 /* Grow the before block */
210 if (before != NULL && after == NULL) {
211 before->size += size;
212 return;
215 /* Grow the after block backwards */
216 if (before == NULL && after != NULL) {
217 after->start = (int8_t *)after->start - size;
218 after->size += size;
219 return;
222 /* Grow the before block, and release the after block */
223 before->size += size + after->size;
224 list_del(&after->list);
225 release_slot(info, after);
228 static void attach_taken_block(rh_info_t * info, rh_block_t * blkn)
230 rh_block_t *blk;
231 struct list_head *l;
233 /* Find the block immediately before the given one (if any) */
234 list_for_each(l, &info->taken_list) {
235 blk = list_entry(l, rh_block_t, list);
236 if (blk->start > blkn->start) {
237 list_add_tail(&blkn->list, &blk->list);
238 return;
242 list_add_tail(&blkn->list, &info->taken_list);
246 * Create a remote heap dynamically. Note that no memory for the blocks
247 * are allocated. It will upon the first allocation
249 rh_info_t *rh_create(unsigned int alignment)
251 rh_info_t *info;
253 /* Alignment must be a power of two */
254 if ((alignment & (alignment - 1)) != 0)
255 return ERR_PTR(-EINVAL);
257 info = kmalloc(sizeof(*info), GFP_KERNEL);
258 if (info == NULL)
259 return ERR_PTR(-ENOMEM);
261 info->alignment = alignment;
263 /* Initially everything as empty */
264 info->block = NULL;
265 info->max_blocks = 0;
266 info->empty_slots = 0;
267 info->flags = 0;
269 INIT_LIST_HEAD(&info->empty_list);
270 INIT_LIST_HEAD(&info->free_list);
271 INIT_LIST_HEAD(&info->taken_list);
273 return info;
277 * Destroy a dynamically created remote heap. Deallocate only if the areas
278 * are not static
280 void rh_destroy(rh_info_t * info)
282 if ((info->flags & RHIF_STATIC_BLOCK) == 0 && info->block != NULL)
283 kfree(info->block);
285 if ((info->flags & RHIF_STATIC_INFO) == 0)
286 kfree(info);
290 * Initialize in place a remote heap info block. This is needed to support
291 * operation very early in the startup of the kernel, when it is not yet safe
292 * to call kmalloc.
294 void rh_init(rh_info_t * info, unsigned int alignment, int max_blocks,
295 rh_block_t * block)
297 int i;
298 rh_block_t *blk;
300 /* Alignment must be a power of two */
301 if ((alignment & (alignment - 1)) != 0)
302 return;
304 info->alignment = alignment;
306 /* Initially everything as empty */
307 info->block = block;
308 info->max_blocks = max_blocks;
309 info->empty_slots = max_blocks;
310 info->flags = RHIF_STATIC_INFO | RHIF_STATIC_BLOCK;
312 INIT_LIST_HEAD(&info->empty_list);
313 INIT_LIST_HEAD(&info->free_list);
314 INIT_LIST_HEAD(&info->taken_list);
316 /* Add all new blocks to the free list */
317 for (i = 0, blk = block; i < max_blocks; i++, blk++)
318 list_add(&blk->list, &info->empty_list);
321 /* Attach a free memory region, coalesces regions if adjuscent */
322 int rh_attach_region(rh_info_t * info, void *start, int size)
324 rh_block_t *blk;
325 unsigned long s, e, m;
326 int r;
328 /* The region must be aligned */
329 s = (unsigned long)start;
330 e = s + size;
331 m = info->alignment - 1;
333 /* Round start up */
334 s = (s + m) & ~m;
336 /* Round end down */
337 e = e & ~m;
339 /* Take final values */
340 start = (void *)s;
341 size = (int)(e - s);
343 /* Grow the blocks, if needed */
344 r = assure_empty(info, 1);
345 if (r < 0)
346 return r;
348 blk = get_slot(info);
349 blk->start = start;
350 blk->size = size;
351 blk->owner = NULL;
353 attach_free_block(info, blk);
355 return 0;
358 /* Detatch given address range, splits free block if needed. */
359 void *rh_detach_region(rh_info_t * info, void *start, int size)
361 struct list_head *l;
362 rh_block_t *blk, *newblk;
363 unsigned long s, e, m, bs, be;
365 /* Validate size */
366 if (size <= 0)
367 return ERR_PTR(-EINVAL);
369 /* The region must be aligned */
370 s = (unsigned long)start;
371 e = s + size;
372 m = info->alignment - 1;
374 /* Round start up */
375 s = (s + m) & ~m;
377 /* Round end down */
378 e = e & ~m;
380 if (assure_empty(info, 1) < 0)
381 return ERR_PTR(-ENOMEM);
383 blk = NULL;
384 list_for_each(l, &info->free_list) {
385 blk = list_entry(l, rh_block_t, list);
386 /* The range must lie entirely inside one free block */
387 bs = (unsigned long)blk->start;
388 be = (unsigned long)blk->start + blk->size;
389 if (s >= bs && e <= be)
390 break;
391 blk = NULL;
394 if (blk == NULL)
395 return ERR_PTR(-ENOMEM);
397 /* Perfect fit */
398 if (bs == s && be == e) {
399 /* Delete from free list, release slot */
400 list_del(&blk->list);
401 release_slot(info, blk);
402 return (void *)s;
405 /* blk still in free list, with updated start and/or size */
406 if (bs == s || be == e) {
407 if (bs == s)
408 blk->start = (int8_t *)blk->start + size;
409 blk->size -= size;
411 } else {
412 /* The front free fragment */
413 blk->size = s - bs;
415 /* the back free fragment */
416 newblk = get_slot(info);
417 newblk->start = (void *)e;
418 newblk->size = be - e;
420 list_add(&newblk->list, &blk->list);
423 return (void *)s;
426 void *rh_alloc(rh_info_t * info, int size, const char *owner)
428 struct list_head *l;
429 rh_block_t *blk;
430 rh_block_t *newblk;
431 void *start;
433 /* Validate size */
434 if (size <= 0)
435 return ERR_PTR(-EINVAL);
437 /* Align to configured alignment */
438 size = (size + (info->alignment - 1)) & ~(info->alignment - 1);
440 if (assure_empty(info, 1) < 0)
441 return ERR_PTR(-ENOMEM);
443 blk = NULL;
444 list_for_each(l, &info->free_list) {
445 blk = list_entry(l, rh_block_t, list);
446 if (size <= blk->size)
447 break;
448 blk = NULL;
451 if (blk == NULL)
452 return ERR_PTR(-ENOMEM);
454 /* Just fits */
455 if (blk->size == size) {
456 /* Move from free list to taken list */
457 list_del(&blk->list);
458 blk->owner = owner;
459 start = blk->start;
461 attach_taken_block(info, blk);
463 return start;
466 newblk = get_slot(info);
467 newblk->start = blk->start;
468 newblk->size = size;
469 newblk->owner = owner;
471 /* blk still in free list, with updated start, size */
472 blk->start = (int8_t *)blk->start + size;
473 blk->size -= size;
475 start = newblk->start;
477 attach_taken_block(info, newblk);
479 return start;
482 /* allocate at precisely the given address */
483 void *rh_alloc_fixed(rh_info_t * info, void *start, int size, const char *owner)
485 struct list_head *l;
486 rh_block_t *blk, *newblk1, *newblk2;
487 unsigned long s, e, m, bs, be;
489 /* Validate size */
490 if (size <= 0)
491 return ERR_PTR(-EINVAL);
493 /* The region must be aligned */
494 s = (unsigned long)start;
495 e = s + size;
496 m = info->alignment - 1;
498 /* Round start up */
499 s = (s + m) & ~m;
501 /* Round end down */
502 e = e & ~m;
504 if (assure_empty(info, 2) < 0)
505 return ERR_PTR(-ENOMEM);
507 blk = NULL;
508 list_for_each(l, &info->free_list) {
509 blk = list_entry(l, rh_block_t, list);
510 /* The range must lie entirely inside one free block */
511 bs = (unsigned long)blk->start;
512 be = (unsigned long)blk->start + blk->size;
513 if (s >= bs && e <= be)
514 break;
517 if (blk == NULL)
518 return ERR_PTR(-ENOMEM);
520 /* Perfect fit */
521 if (bs == s && be == e) {
522 /* Move from free list to taken list */
523 list_del(&blk->list);
524 blk->owner = owner;
526 start = blk->start;
527 attach_taken_block(info, blk);
529 return start;
533 /* blk still in free list, with updated start and/or size */
534 if (bs == s || be == e) {
535 if (bs == s)
536 blk->start = (int8_t *)blk->start + size;
537 blk->size -= size;
539 } else {
540 /* The front free fragment */
541 blk->size = s - bs;
543 /* The back free fragment */
544 newblk2 = get_slot(info);
545 newblk2->start = (void *)e;
546 newblk2->size = be - e;
548 list_add(&newblk2->list, &blk->list);
551 newblk1 = get_slot(info);
552 newblk1->start = (void *)s;
553 newblk1->size = e - s;
554 newblk1->owner = owner;
556 start = newblk1->start;
557 attach_taken_block(info, newblk1);
559 return start;
562 int rh_free(rh_info_t * info, void *start)
564 rh_block_t *blk, *blk2;
565 struct list_head *l;
566 int size;
568 /* Linear search for block */
569 blk = NULL;
570 list_for_each(l, &info->taken_list) {
571 blk2 = list_entry(l, rh_block_t, list);
572 if (start < blk2->start)
573 break;
574 blk = blk2;
577 if (blk == NULL || start > (blk->start + blk->size))
578 return -EINVAL;
580 /* Remove from taken list */
581 list_del(&blk->list);
583 /* Get size of freed block */
584 size = blk->size;
585 attach_free_block(info, blk);
587 return size;
590 int rh_get_stats(rh_info_t * info, int what, int max_stats, rh_stats_t * stats)
592 rh_block_t *blk;
593 struct list_head *l;
594 struct list_head *h;
595 int nr;
597 switch (what) {
599 case RHGS_FREE:
600 h = &info->free_list;
601 break;
603 case RHGS_TAKEN:
604 h = &info->taken_list;
605 break;
607 default:
608 return -EINVAL;
611 /* Linear search for block */
612 nr = 0;
613 list_for_each(l, h) {
614 blk = list_entry(l, rh_block_t, list);
615 if (stats != NULL && nr < max_stats) {
616 stats->start = blk->start;
617 stats->size = blk->size;
618 stats->owner = blk->owner;
619 stats++;
621 nr++;
624 return nr;
627 int rh_set_owner(rh_info_t * info, void *start, const char *owner)
629 rh_block_t *blk, *blk2;
630 struct list_head *l;
631 int size;
633 /* Linear search for block */
634 blk = NULL;
635 list_for_each(l, &info->taken_list) {
636 blk2 = list_entry(l, rh_block_t, list);
637 if (start < blk2->start)
638 break;
639 blk = blk2;
642 if (blk == NULL || start > (blk->start + blk->size))
643 return -EINVAL;
645 blk->owner = owner;
646 size = blk->size;
648 return size;
651 void rh_dump(rh_info_t * info)
653 static rh_stats_t st[32]; /* XXX maximum 32 blocks */
654 int maxnr;
655 int i, nr;
657 maxnr = sizeof(st) / sizeof(st[0]);
659 printk(KERN_INFO
660 "info @0x%p (%d slots empty / %d max)\n",
661 info, info->empty_slots, info->max_blocks);
663 printk(KERN_INFO " Free:\n");
664 nr = rh_get_stats(info, RHGS_FREE, maxnr, st);
665 if (nr > maxnr)
666 nr = maxnr;
667 for (i = 0; i < nr; i++)
668 printk(KERN_INFO
669 " 0x%p-0x%p (%u)\n",
670 st[i].start, (int8_t *) st[i].start + st[i].size,
671 st[i].size);
672 printk(KERN_INFO "\n");
674 printk(KERN_INFO " Taken:\n");
675 nr = rh_get_stats(info, RHGS_TAKEN, maxnr, st);
676 if (nr > maxnr)
677 nr = maxnr;
678 for (i = 0; i < nr; i++)
679 printk(KERN_INFO
680 " 0x%p-0x%p (%u) %s\n",
681 st[i].start, (int8_t *) st[i].start + st[i].size,
682 st[i].size, st[i].owner != NULL ? st[i].owner : "");
683 printk(KERN_INFO "\n");
686 void rh_dump_blk(rh_info_t * info, rh_block_t * blk)
688 printk(KERN_INFO
689 "blk @0x%p: 0x%p-0x%p (%u)\n",
690 blk, blk->start, (int8_t *) blk->start + blk->size, blk->size);