Remove fs.h from mm.h
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / powerpc / lib / rheap.c
blobada5b42dd231635205bccdbc3cedd6c72f80f0cd
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/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
22 #include <asm/rheap.h>
25 * Fixup a list_head, needed when copying lists. If the pointers fall
26 * between s and e, apply the delta. This assumes that
27 * sizeof(struct list_head *) == sizeof(unsigned long *).
29 static inline void fixup(unsigned long s, unsigned long e, int d,
30 struct list_head *l)
32 unsigned long *pp;
34 pp = (unsigned long *)&l->next;
35 if (*pp >= s && *pp < e)
36 *pp += d;
38 pp = (unsigned long *)&l->prev;
39 if (*pp >= s && *pp < e)
40 *pp += d;
43 /* Grow the allocated blocks */
44 static int grow(rh_info_t * info, int max_blocks)
46 rh_block_t *block, *blk;
47 int i, new_blocks;
48 int delta;
49 unsigned long blks, blke;
51 if (max_blocks <= info->max_blocks)
52 return -EINVAL;
54 new_blocks = max_blocks - info->max_blocks;
56 block = kmalloc(sizeof(rh_block_t) * max_blocks, GFP_KERNEL);
57 if (block == NULL)
58 return -ENOMEM;
60 if (info->max_blocks > 0) {
62 /* copy old block area */
63 memcpy(block, info->block,
64 sizeof(rh_block_t) * info->max_blocks);
66 delta = (char *)block - (char *)info->block;
68 /* and fixup list pointers */
69 blks = (unsigned long)info->block;
70 blke = (unsigned long)(info->block + info->max_blocks);
72 for (i = 0, blk = block; i < info->max_blocks; i++, blk++)
73 fixup(blks, blke, delta, &blk->list);
75 fixup(blks, blke, delta, &info->empty_list);
76 fixup(blks, blke, delta, &info->free_list);
77 fixup(blks, blke, delta, &info->taken_list);
79 /* free the old allocated memory */
80 if ((info->flags & RHIF_STATIC_BLOCK) == 0)
81 kfree(info->block);
84 info->block = block;
85 info->empty_slots += new_blocks;
86 info->max_blocks = max_blocks;
87 info->flags &= ~RHIF_STATIC_BLOCK;
89 /* add all new blocks to the free list */
90 blk = block + info->max_blocks - new_blocks;
91 for (i = 0; i < new_blocks; i++, blk++)
92 list_add(&blk->list, &info->empty_list);
94 return 0;
98 * Assure at least the required amount of empty slots. If this function
99 * causes a grow in the block area then all pointers kept to the block
100 * area are invalid!
102 static int assure_empty(rh_info_t * info, int slots)
104 int max_blocks;
106 /* This function is not meant to be used to grow uncontrollably */
107 if (slots >= 4)
108 return -EINVAL;
110 /* Enough space */
111 if (info->empty_slots >= slots)
112 return 0;
114 /* Next 16 sized block */
115 max_blocks = ((info->max_blocks + slots) + 15) & ~15;
117 return grow(info, max_blocks);
120 static rh_block_t *get_slot(rh_info_t * info)
122 rh_block_t *blk;
124 /* If no more free slots, and failure to extend. */
125 /* XXX: You should have called assure_empty before */
126 if (info->empty_slots == 0) {
127 printk(KERN_ERR "rh: out of slots; crash is imminent.\n");
128 return NULL;
131 /* Get empty slot to use */
132 blk = list_entry(info->empty_list.next, rh_block_t, list);
133 list_del_init(&blk->list);
134 info->empty_slots--;
136 /* Initialize */
137 blk->start = 0;
138 blk->size = 0;
139 blk->owner = NULL;
141 return blk;
144 static inline void release_slot(rh_info_t * info, rh_block_t * blk)
146 list_add(&blk->list, &info->empty_list);
147 info->empty_slots++;
150 static void attach_free_block(rh_info_t * info, rh_block_t * blkn)
152 rh_block_t *blk;
153 rh_block_t *before;
154 rh_block_t *after;
155 rh_block_t *next;
156 int size;
157 unsigned long s, e, bs, be;
158 struct list_head *l;
160 /* We assume that they are aligned properly */
161 size = blkn->size;
162 s = blkn->start;
163 e = s + size;
165 /* Find the blocks immediately before and after the given one
166 * (if any) */
167 before = NULL;
168 after = NULL;
169 next = NULL;
171 list_for_each(l, &info->free_list) {
172 blk = list_entry(l, rh_block_t, list);
174 bs = blk->start;
175 be = bs + blk->size;
177 if (next == NULL && s >= bs)
178 next = blk;
180 if (be == s)
181 before = blk;
183 if (e == bs)
184 after = blk;
186 /* If both are not null, break now */
187 if (before != NULL && after != NULL)
188 break;
191 /* Now check if they are really adjacent */
192 if (before && s != (before->start + before->size))
193 before = NULL;
195 if (after && e != after->start)
196 after = NULL;
198 /* No coalescing; list insert and return */
199 if (before == NULL && after == NULL) {
201 if (next != NULL)
202 list_add(&blkn->list, &next->list);
203 else
204 list_add(&blkn->list, &info->free_list);
206 return;
209 /* We don't need it anymore */
210 release_slot(info, blkn);
212 /* Grow the before block */
213 if (before != NULL && after == NULL) {
214 before->size += size;
215 return;
218 /* Grow the after block backwards */
219 if (before == NULL && after != NULL) {
220 after->start -= size;
221 after->size += size;
222 return;
225 /* Grow the before block, and release the after block */
226 before->size += size + after->size;
227 list_del(&after->list);
228 release_slot(info, after);
231 static void attach_taken_block(rh_info_t * info, rh_block_t * blkn)
233 rh_block_t *blk;
234 struct list_head *l;
236 /* Find the block immediately before the given one (if any) */
237 list_for_each(l, &info->taken_list) {
238 blk = list_entry(l, rh_block_t, list);
239 if (blk->start > blkn->start) {
240 list_add_tail(&blkn->list, &blk->list);
241 return;
245 list_add_tail(&blkn->list, &info->taken_list);
249 * Create a remote heap dynamically. Note that no memory for the blocks
250 * are allocated. It will upon the first allocation
252 rh_info_t *rh_create(unsigned int alignment)
254 rh_info_t *info;
256 /* Alignment must be a power of two */
257 if ((alignment & (alignment - 1)) != 0)
258 return ERR_PTR(-EINVAL);
260 info = kmalloc(sizeof(*info), GFP_KERNEL);
261 if (info == NULL)
262 return ERR_PTR(-ENOMEM);
264 info->alignment = alignment;
266 /* Initially everything as empty */
267 info->block = NULL;
268 info->max_blocks = 0;
269 info->empty_slots = 0;
270 info->flags = 0;
272 INIT_LIST_HEAD(&info->empty_list);
273 INIT_LIST_HEAD(&info->free_list);
274 INIT_LIST_HEAD(&info->taken_list);
276 return info;
280 * Destroy a dynamically created remote heap. Deallocate only if the areas
281 * are not static
283 void rh_destroy(rh_info_t * info)
285 if ((info->flags & RHIF_STATIC_BLOCK) == 0 && info->block != NULL)
286 kfree(info->block);
288 if ((info->flags & RHIF_STATIC_INFO) == 0)
289 kfree(info);
293 * Initialize in place a remote heap info block. This is needed to support
294 * operation very early in the startup of the kernel, when it is not yet safe
295 * to call kmalloc.
297 void rh_init(rh_info_t * info, unsigned int alignment, int max_blocks,
298 rh_block_t * block)
300 int i;
301 rh_block_t *blk;
303 /* Alignment must be a power of two */
304 if ((alignment & (alignment - 1)) != 0)
305 return;
307 info->alignment = alignment;
309 /* Initially everything as empty */
310 info->block = block;
311 info->max_blocks = max_blocks;
312 info->empty_slots = max_blocks;
313 info->flags = RHIF_STATIC_INFO | RHIF_STATIC_BLOCK;
315 INIT_LIST_HEAD(&info->empty_list);
316 INIT_LIST_HEAD(&info->free_list);
317 INIT_LIST_HEAD(&info->taken_list);
319 /* Add all new blocks to the free list */
320 for (i = 0, blk = block; i < max_blocks; i++, blk++)
321 list_add(&blk->list, &info->empty_list);
324 /* Attach a free memory region, coalesces regions if adjuscent */
325 int rh_attach_region(rh_info_t * info, unsigned long start, int size)
327 rh_block_t *blk;
328 unsigned long s, e, m;
329 int r;
331 /* The region must be aligned */
332 s = start;
333 e = s + size;
334 m = info->alignment - 1;
336 /* Round start up */
337 s = (s + m) & ~m;
339 /* Round end down */
340 e = e & ~m;
342 if (IS_ERR_VALUE(e) || (e < s))
343 return -ERANGE;
345 /* Take final values */
346 start = s;
347 size = e - s;
349 /* Grow the blocks, if needed */
350 r = assure_empty(info, 1);
351 if (r < 0)
352 return r;
354 blk = get_slot(info);
355 blk->start = start;
356 blk->size = size;
357 blk->owner = NULL;
359 attach_free_block(info, blk);
361 return 0;
364 /* Detatch given address range, splits free block if needed. */
365 unsigned long rh_detach_region(rh_info_t * info, unsigned long start, int size)
367 struct list_head *l;
368 rh_block_t *blk, *newblk;
369 unsigned long s, e, m, bs, be;
371 /* Validate size */
372 if (size <= 0)
373 return (unsigned long) -EINVAL;
375 /* The region must be aligned */
376 s = start;
377 e = s + size;
378 m = info->alignment - 1;
380 /* Round start up */
381 s = (s + m) & ~m;
383 /* Round end down */
384 e = e & ~m;
386 if (assure_empty(info, 1) < 0)
387 return (unsigned long) -ENOMEM;
389 blk = NULL;
390 list_for_each(l, &info->free_list) {
391 blk = list_entry(l, rh_block_t, list);
392 /* The range must lie entirely inside one free block */
393 bs = blk->start;
394 be = blk->start + blk->size;
395 if (s >= bs && e <= be)
396 break;
397 blk = NULL;
400 if (blk == NULL)
401 return (unsigned long) -ENOMEM;
403 /* Perfect fit */
404 if (bs == s && be == e) {
405 /* Delete from free list, release slot */
406 list_del(&blk->list);
407 release_slot(info, blk);
408 return s;
411 /* blk still in free list, with updated start and/or size */
412 if (bs == s || be == e) {
413 if (bs == s)
414 blk->start += size;
415 blk->size -= size;
417 } else {
418 /* The front free fragment */
419 blk->size = s - bs;
421 /* the back free fragment */
422 newblk = get_slot(info);
423 newblk->start = e;
424 newblk->size = be - e;
426 list_add(&newblk->list, &blk->list);
429 return s;
432 /* Allocate a block of memory at the specified alignment. The value returned
433 * is an offset into the buffer initialized by rh_init(), or a negative number
434 * if there is an error.
436 unsigned long rh_alloc_align(rh_info_t * info, int size, int alignment, const char *owner)
438 struct list_head *l;
439 rh_block_t *blk;
440 rh_block_t *newblk;
441 unsigned long start, sp_size;
443 /* Validate size, and alignment must be power of two */
444 if (size <= 0 || (alignment & (alignment - 1)) != 0)
445 return (unsigned long) -EINVAL;
447 /* Align to configured alignment */
448 size = (size + (info->alignment - 1)) & ~(info->alignment - 1);
450 if (assure_empty(info, 2) < 0)
451 return (unsigned long) -ENOMEM;
453 blk = NULL;
454 list_for_each(l, &info->free_list) {
455 blk = list_entry(l, rh_block_t, list);
456 if (size <= blk->size) {
457 start = (blk->start + alignment - 1) & ~(alignment - 1);
458 if (start + size <= blk->start + blk->size)
459 break;
461 blk = NULL;
464 if (blk == NULL)
465 return (unsigned long) -ENOMEM;
467 /* Just fits */
468 if (blk->size == size) {
469 /* Move from free list to taken list */
470 list_del(&blk->list);
471 newblk = blk;
472 } else {
473 /* Fragment caused, split if needed */
474 /* Create block for fragment in the beginning */
475 sp_size = start - blk->start;
476 if (sp_size) {
477 rh_block_t *spblk;
479 spblk = get_slot(info);
480 spblk->start = blk->start;
481 spblk->size = sp_size;
482 /* add before the blk */
483 list_add(&spblk->list, blk->list.prev);
485 newblk = get_slot(info);
486 newblk->start = start;
487 newblk->size = size;
489 /* blk still in free list, with updated start and size
490 * for fragment in the end */
491 blk->start = start + size;
492 blk->size -= sp_size + size;
493 /* No fragment in the end, remove blk */
494 if (blk->size == 0) {
495 list_del(&blk->list);
496 release_slot(info, blk);
500 newblk->owner = owner;
501 attach_taken_block(info, newblk);
503 return start;
506 /* Allocate a block of memory at the default alignment. The value returned is
507 * an offset into the buffer initialized by rh_init(), or a negative number if
508 * there is an error.
510 unsigned long rh_alloc(rh_info_t * info, int size, const char *owner)
512 return rh_alloc_align(info, size, info->alignment, owner);
515 /* Allocate a block of memory at the given offset, rounded up to the default
516 * alignment. The value returned is an offset into the buffer initialized by
517 * rh_init(), or a negative number if there is an error.
519 unsigned long rh_alloc_fixed(rh_info_t * info, unsigned long start, int size, const char *owner)
521 struct list_head *l;
522 rh_block_t *blk, *newblk1, *newblk2;
523 unsigned long s, e, m, bs = 0, be = 0;
525 /* Validate size */
526 if (size <= 0)
527 return (unsigned long) -EINVAL;
529 /* The region must be aligned */
530 s = start;
531 e = s + size;
532 m = info->alignment - 1;
534 /* Round start up */
535 s = (s + m) & ~m;
537 /* Round end down */
538 e = e & ~m;
540 if (assure_empty(info, 2) < 0)
541 return (unsigned long) -ENOMEM;
543 blk = NULL;
544 list_for_each(l, &info->free_list) {
545 blk = list_entry(l, rh_block_t, list);
546 /* The range must lie entirely inside one free block */
547 bs = blk->start;
548 be = blk->start + blk->size;
549 if (s >= bs && e <= be)
550 break;
553 if (blk == NULL)
554 return (unsigned long) -ENOMEM;
556 /* Perfect fit */
557 if (bs == s && be == e) {
558 /* Move from free list to taken list */
559 list_del(&blk->list);
560 blk->owner = owner;
562 start = blk->start;
563 attach_taken_block(info, blk);
565 return start;
569 /* blk still in free list, with updated start and/or size */
570 if (bs == s || be == e) {
571 if (bs == s)
572 blk->start += size;
573 blk->size -= size;
575 } else {
576 /* The front free fragment */
577 blk->size = s - bs;
579 /* The back free fragment */
580 newblk2 = get_slot(info);
581 newblk2->start = e;
582 newblk2->size = be - e;
584 list_add(&newblk2->list, &blk->list);
587 newblk1 = get_slot(info);
588 newblk1->start = s;
589 newblk1->size = e - s;
590 newblk1->owner = owner;
592 start = newblk1->start;
593 attach_taken_block(info, newblk1);
595 return start;
598 /* Deallocate the memory previously allocated by one of the rh_alloc functions.
599 * The return value is the size of the deallocated block, or a negative number
600 * if there is an error.
602 int rh_free(rh_info_t * info, unsigned long start)
604 rh_block_t *blk, *blk2;
605 struct list_head *l;
606 int size;
608 /* Linear search for block */
609 blk = NULL;
610 list_for_each(l, &info->taken_list) {
611 blk2 = list_entry(l, rh_block_t, list);
612 if (start < blk2->start)
613 break;
614 blk = blk2;
617 if (blk == NULL || start > (blk->start + blk->size))
618 return -EINVAL;
620 /* Remove from taken list */
621 list_del(&blk->list);
623 /* Get size of freed block */
624 size = blk->size;
625 attach_free_block(info, blk);
627 return size;
630 int rh_get_stats(rh_info_t * info, int what, int max_stats, rh_stats_t * stats)
632 rh_block_t *blk;
633 struct list_head *l;
634 struct list_head *h;
635 int nr;
637 switch (what) {
639 case RHGS_FREE:
640 h = &info->free_list;
641 break;
643 case RHGS_TAKEN:
644 h = &info->taken_list;
645 break;
647 default:
648 return -EINVAL;
651 /* Linear search for block */
652 nr = 0;
653 list_for_each(l, h) {
654 blk = list_entry(l, rh_block_t, list);
655 if (stats != NULL && nr < max_stats) {
656 stats->start = blk->start;
657 stats->size = blk->size;
658 stats->owner = blk->owner;
659 stats++;
661 nr++;
664 return nr;
667 int rh_set_owner(rh_info_t * info, unsigned long start, const char *owner)
669 rh_block_t *blk, *blk2;
670 struct list_head *l;
671 int size;
673 /* Linear search for block */
674 blk = NULL;
675 list_for_each(l, &info->taken_list) {
676 blk2 = list_entry(l, rh_block_t, list);
677 if (start < blk2->start)
678 break;
679 blk = blk2;
682 if (blk == NULL || start > (blk->start + blk->size))
683 return -EINVAL;
685 blk->owner = owner;
686 size = blk->size;
688 return size;
691 void rh_dump(rh_info_t * info)
693 static rh_stats_t st[32]; /* XXX maximum 32 blocks */
694 int maxnr;
695 int i, nr;
697 maxnr = ARRAY_SIZE(st);
699 printk(KERN_INFO
700 "info @0x%p (%d slots empty / %d max)\n",
701 info, info->empty_slots, info->max_blocks);
703 printk(KERN_INFO " Free:\n");
704 nr = rh_get_stats(info, RHGS_FREE, maxnr, st);
705 if (nr > maxnr)
706 nr = maxnr;
707 for (i = 0; i < nr; i++)
708 printk(KERN_INFO
709 " 0x%lx-0x%lx (%u)\n",
710 st[i].start, st[i].start + st[i].size,
711 st[i].size);
712 printk(KERN_INFO "\n");
714 printk(KERN_INFO " Taken:\n");
715 nr = rh_get_stats(info, RHGS_TAKEN, maxnr, st);
716 if (nr > maxnr)
717 nr = maxnr;
718 for (i = 0; i < nr; i++)
719 printk(KERN_INFO
720 " 0x%lx-0x%lx (%u) %s\n",
721 st[i].start, st[i].start + st[i].size,
722 st[i].size, st[i].owner != NULL ? st[i].owner : "");
723 printk(KERN_INFO "\n");
726 void rh_dump_blk(rh_info_t * info, rh_block_t * blk)
728 printk(KERN_INFO
729 "blk @0x%p: 0x%lx-0x%lx (%u)\n",
730 blk, blk->start, blk->start + blk->size, blk->size);