2 * c 2001 PPC 64 Team, IBM Corp
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #include <linux/slab.h>
11 #include <linux/vmalloc.h>
13 #include <asm/uaccess.h>
14 #include <asm/pgalloc.h>
15 #include <asm/pgtable.h>
16 #include <asm/semaphore.h>
18 static DECLARE_MUTEX(imlist_sem);
19 struct vm_struct * imlist = NULL;
21 static int get_free_im_addr(unsigned long size, unsigned long *im_addr)
24 struct vm_struct **p, *tmp;
27 for (p = &imlist; (tmp = *p) ; p = &tmp->next) {
28 if (size + addr < (unsigned long) tmp->addr)
30 if ((unsigned long)tmp->addr >= IMALLOC_START)
31 addr = tmp->size + (unsigned long) tmp->addr;
32 if (addr > IMALLOC_END-size)
40 /* Return whether the region described by v_addr and size overlaps
41 * the region described by vm. Overlapping regions meet the
42 * following conditions:
43 * 1) The regions share some part of the address space
44 * 2) The regions aren't identical
45 * 3) The first region is not a subset of the second
47 static inline int im_region_overlaps(unsigned long v_addr, unsigned long size,
50 return (v_addr + size > (unsigned long) vm->addr + vm->size &&
51 v_addr < (unsigned long) vm->addr + vm->size) ||
52 (v_addr < (unsigned long) vm->addr &&
53 v_addr + size > (unsigned long) vm->addr);
56 /* Return whether the region described by v_addr and size is a subset
57 * of the region described by vm
59 static inline int im_region_is_subset(unsigned long v_addr, unsigned long size,
62 return (int) (v_addr >= (unsigned long) vm->addr &&
63 v_addr < (unsigned long) vm->addr + vm->size &&
67 /* Determine imalloc status of region described by v_addr and size.
68 * Can return one of the following:
69 * IM_REGION_UNUSED - Entire region is unallocated in imalloc space.
70 * IM_REGION_SUBSET - Region is a subset of a region that is already
71 * allocated in imalloc space.
72 * vm will be assigned to a ptr to the parent region.
73 * IM_REGION_EXISTS - Exact region already allocated in imalloc space.
74 * vm will be assigned to a ptr to the existing imlist
76 * IM_REGION_OVERLAPS - A portion of the region is already allocated in
79 static int im_region_status(unsigned long v_addr, unsigned long size,
80 struct vm_struct **vm)
82 struct vm_struct *tmp;
84 for (tmp = imlist; tmp; tmp = tmp->next)
85 if (v_addr < (unsigned long) tmp->addr + tmp->size)
89 if (im_region_overlaps(v_addr, size, tmp))
90 return IM_REGION_OVERLAP;
93 if (im_region_is_subset(v_addr, size, tmp))
94 return IM_REGION_SUBSET;
95 else if (v_addr == (unsigned long) tmp->addr &&
97 return IM_REGION_EXISTS;
101 return IM_REGION_UNUSED;
104 static struct vm_struct * split_im_region(unsigned long v_addr,
105 unsigned long size, struct vm_struct *parent)
107 struct vm_struct *vm1 = NULL;
108 struct vm_struct *vm2 = NULL;
109 struct vm_struct *new_vm = NULL;
111 vm1 = (struct vm_struct *) kmalloc(sizeof(*vm1), GFP_KERNEL);
113 printk(KERN_ERR "%s() out of memory\n", __FUNCTION__);
117 if (v_addr == (unsigned long) parent->addr) {
118 /* Use existing parent vm_struct to represent child, allocate
119 * new one for the remainder of parent range
121 vm1->size = parent->size - size;
122 vm1->addr = (void *) (v_addr + size);
123 vm1->next = parent->next;
128 } else if (v_addr + size == (unsigned long) parent->addr +
130 /* Allocate new vm_struct to represent child, use existing
131 * parent one for remainder of parent range
134 vm1->addr = (void *) v_addr;
135 vm1->next = parent->next;
138 parent->size -= size;
141 /* Allocate two new vm_structs for the new child and
142 * uppermost remainder, and use existing parent one for the
143 * lower remainder of parent range
145 vm2 = (struct vm_struct *) kmalloc(sizeof(*vm2), GFP_KERNEL);
147 printk(KERN_ERR "%s() out of memory\n", __FUNCTION__);
153 vm1->addr = (void *) v_addr;
157 vm2->size = ((unsigned long) parent->addr + parent->size) -
159 vm2->addr = (void *) v_addr + size;
160 vm2->next = parent->next;
162 parent->size = v_addr - (unsigned long) parent->addr;
169 static struct vm_struct * __add_new_im_area(unsigned long req_addr,
172 struct vm_struct **p, *tmp, *area;
174 for (p = &imlist; (tmp = *p) ; p = &tmp->next) {
175 if (req_addr + size <= (unsigned long)tmp->addr)
179 area = (struct vm_struct *) kmalloc(sizeof(*area), GFP_KERNEL);
183 area->addr = (void *)req_addr;
191 static struct vm_struct * __im_get_area(unsigned long req_addr,
195 struct vm_struct *tmp;
198 status = im_region_status(req_addr, size, &tmp);
199 if ((criteria & status) == 0) {
204 case IM_REGION_UNUSED:
205 tmp = __add_new_im_area(req_addr, size);
207 case IM_REGION_SUBSET:
208 tmp = split_im_region(req_addr, size, tmp);
210 case IM_REGION_EXISTS:
213 printk(KERN_ERR "%s() unexpected imalloc region status\n",
221 struct vm_struct * im_get_free_area(unsigned long size)
223 struct vm_struct *area;
227 if (get_free_im_addr(size, &addr)) {
228 printk(KERN_ERR "%s() cannot obtain addr for size 0x%lx\n",
234 area = __im_get_area(addr, size, IM_REGION_UNUSED);
237 "%s() cannot obtain area for addr 0x%lx size 0x%lx\n",
238 __FUNCTION__, addr, size);
245 struct vm_struct * im_get_area(unsigned long v_addr, unsigned long size,
248 struct vm_struct *area;
251 area = __im_get_area(v_addr, size, criteria);
256 unsigned long im_free(void * addr)
258 struct vm_struct **p, *tmp;
259 unsigned long ret_size = 0;
263 if ((PAGE_SIZE-1) & (unsigned long) addr) {
264 printk(KERN_ERR "Trying to %s bad address (%p)\n", __FUNCTION__, addr);
268 for (p = &imlist ; (tmp = *p) ; p = &tmp->next) {
269 if (tmp->addr == addr) {
270 ret_size = tmp->size;
278 printk(KERN_ERR "Trying to %s nonexistent area (%p)\n", __FUNCTION__,