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 <linux/mutex.h>
17 #include <asm/cacheflush.h>
21 static DEFINE_MUTEX(imlist_mutex
);
22 struct vm_struct
* imlist
= NULL
;
24 static int get_free_im_addr(unsigned long size
, unsigned long *im_addr
)
27 struct vm_struct
**p
, *tmp
;
30 for (p
= &imlist
; (tmp
= *p
) ; p
= &tmp
->next
) {
31 if (size
+ addr
< (unsigned long) tmp
->addr
)
33 if ((unsigned long)tmp
->addr
>= ioremap_bot
)
34 addr
= tmp
->size
+ (unsigned long) tmp
->addr
;
35 if (addr
>= IMALLOC_END
-size
)
43 /* Return whether the region described by v_addr and size is a subset
44 * of the region described by parent
46 static inline int im_region_is_subset(unsigned long v_addr
, unsigned long size
,
47 struct vm_struct
*parent
)
49 return (int) (v_addr
>= (unsigned long) parent
->addr
&&
50 v_addr
< (unsigned long) parent
->addr
+ parent
->size
&&
54 /* Return whether the region described by v_addr and size is a superset
55 * of the region described by child
57 static int im_region_is_superset(unsigned long v_addr
, unsigned long size
,
58 struct vm_struct
*child
)
60 struct vm_struct parent
;
62 parent
.addr
= (void *) v_addr
;
65 return im_region_is_subset((unsigned long) child
->addr
, child
->size
,
69 /* Return whether the region described by v_addr and size overlaps
70 * the region described by vm. Overlapping regions meet the
71 * following conditions:
72 * 1) The regions share some part of the address space
73 * 2) The regions aren't identical
74 * 3) Neither region is a subset of the other
76 static int im_region_overlaps(unsigned long v_addr
, unsigned long size
,
79 if (im_region_is_superset(v_addr
, size
, vm
))
82 return (v_addr
+ size
> (unsigned long) vm
->addr
+ vm
->size
&&
83 v_addr
< (unsigned long) vm
->addr
+ vm
->size
) ||
84 (v_addr
< (unsigned long) vm
->addr
&&
85 v_addr
+ size
> (unsigned long) vm
->addr
);
88 /* Determine imalloc status of region described by v_addr and size.
89 * Can return one of the following:
90 * IM_REGION_UNUSED - Entire region is unallocated in imalloc space.
91 * IM_REGION_SUBSET - Region is a subset of a region that is already
92 * allocated in imalloc space.
93 * vm will be assigned to a ptr to the parent region.
94 * IM_REGION_EXISTS - Exact region already allocated in imalloc space.
95 * vm will be assigned to a ptr to the existing imlist
97 * IM_REGION_OVERLAPS - Region overlaps an allocated region in imalloc space.
98 * IM_REGION_SUPERSET - Region is a superset of a region that is already
99 * allocated in imalloc space.
101 static int im_region_status(unsigned long v_addr
, unsigned long size
,
102 struct vm_struct
**vm
)
104 struct vm_struct
*tmp
;
106 for (tmp
= imlist
; tmp
; tmp
= tmp
->next
)
107 if (v_addr
< (unsigned long) tmp
->addr
+ tmp
->size
)
112 if (im_region_overlaps(v_addr
, size
, tmp
))
113 return IM_REGION_OVERLAP
;
116 if (im_region_is_subset(v_addr
, size
, tmp
)) {
117 /* Return with tmp pointing to superset */
118 return IM_REGION_SUBSET
;
120 if (im_region_is_superset(v_addr
, size
, tmp
)) {
121 /* Return with tmp pointing to first subset */
122 return IM_REGION_SUPERSET
;
124 else if (v_addr
== (unsigned long) tmp
->addr
&&
126 /* Return with tmp pointing to exact region */
127 return IM_REGION_EXISTS
;
131 return IM_REGION_UNUSED
;
134 static struct vm_struct
* split_im_region(unsigned long v_addr
,
135 unsigned long size
, struct vm_struct
*parent
)
137 struct vm_struct
*vm1
= NULL
;
138 struct vm_struct
*vm2
= NULL
;
139 struct vm_struct
*new_vm
= NULL
;
141 vm1
= (struct vm_struct
*) kmalloc(sizeof(*vm1
), GFP_KERNEL
);
143 printk(KERN_ERR
"%s() out of memory\n", __FUNCTION__
);
147 if (v_addr
== (unsigned long) parent
->addr
) {
148 /* Use existing parent vm_struct to represent child, allocate
149 * new one for the remainder of parent range
151 vm1
->size
= parent
->size
- size
;
152 vm1
->addr
= (void *) (v_addr
+ size
);
153 vm1
->next
= parent
->next
;
158 } else if (v_addr
+ size
== (unsigned long) parent
->addr
+
160 /* Allocate new vm_struct to represent child, use existing
161 * parent one for remainder of parent range
164 vm1
->addr
= (void *) v_addr
;
165 vm1
->next
= parent
->next
;
168 parent
->size
-= size
;
171 /* Allocate two new vm_structs for the new child and
172 * uppermost remainder, and use existing parent one for the
173 * lower remainder of parent range
175 vm2
= (struct vm_struct
*) kmalloc(sizeof(*vm2
), GFP_KERNEL
);
177 printk(KERN_ERR
"%s() out of memory\n", __FUNCTION__
);
183 vm1
->addr
= (void *) v_addr
;
187 vm2
->size
= ((unsigned long) parent
->addr
+ parent
->size
) -
189 vm2
->addr
= (void *) v_addr
+ size
;
190 vm2
->next
= parent
->next
;
192 parent
->size
= v_addr
- (unsigned long) parent
->addr
;
199 static struct vm_struct
* __add_new_im_area(unsigned long req_addr
,
202 struct vm_struct
**p
, *tmp
, *area
;
204 for (p
= &imlist
; (tmp
= *p
) ; p
= &tmp
->next
) {
205 if (req_addr
+ size
<= (unsigned long)tmp
->addr
)
209 area
= (struct vm_struct
*) kmalloc(sizeof(*area
), GFP_KERNEL
);
213 area
->addr
= (void *)req_addr
;
221 static struct vm_struct
* __im_get_area(unsigned long req_addr
,
225 struct vm_struct
*tmp
;
228 status
= im_region_status(req_addr
, size
, &tmp
);
229 if ((criteria
& status
) == 0) {
234 case IM_REGION_UNUSED
:
235 tmp
= __add_new_im_area(req_addr
, size
);
237 case IM_REGION_SUBSET
:
238 tmp
= split_im_region(req_addr
, size
, tmp
);
240 case IM_REGION_EXISTS
:
241 /* Return requested region */
243 case IM_REGION_SUPERSET
:
244 /* Return first existing subset of requested region */
247 printk(KERN_ERR
"%s() unexpected imalloc region status\n",
255 struct vm_struct
* im_get_free_area(unsigned long size
)
257 struct vm_struct
*area
;
260 mutex_lock(&imlist_mutex
);
261 if (get_free_im_addr(size
, &addr
)) {
262 printk(KERN_ERR
"%s() cannot obtain addr for size 0x%lx\n",
268 area
= __im_get_area(addr
, size
, IM_REGION_UNUSED
);
271 "%s() cannot obtain area for addr 0x%lx size 0x%lx\n",
272 __FUNCTION__
, addr
, size
);
275 mutex_unlock(&imlist_mutex
);
279 struct vm_struct
* im_get_area(unsigned long v_addr
, unsigned long size
,
282 struct vm_struct
*area
;
284 mutex_lock(&imlist_mutex
);
285 area
= __im_get_area(v_addr
, size
, criteria
);
286 mutex_unlock(&imlist_mutex
);
290 void im_free(void * addr
)
292 struct vm_struct
**p
, *tmp
;
296 if ((unsigned long) addr
& ~PAGE_MASK
) {
297 printk(KERN_ERR
"Trying to %s bad address (%p)\n", __FUNCTION__
, addr
);
300 mutex_lock(&imlist_mutex
);
301 for (p
= &imlist
; (tmp
= *p
) ; p
= &tmp
->next
) {
302 if (tmp
->addr
== addr
) {
306 mutex_unlock(&imlist_mutex
);
310 mutex_unlock(&imlist_mutex
);
311 printk(KERN_ERR
"Trying to %s nonexistent area (%p)\n", __FUNCTION__
,