1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/*
* Copyright (C) 2010-2011, 2013-2014 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
*
* A copy of the licence is included with the program, and can also be obtained from Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* needed to detect kernel version specific code */
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
#include <linux/semaphore.h>
#else /* pre 2.6.26 the file was in the arch specific location */
#include <asm/semaphore.h>
#endif
#include <linux/mm.h>
#include <linux/slab.h>
#include <asm/atomic.h>
#include <linux/vmalloc.h>
#include "ump_kernel_common.h"
#include "ump_kernel_memory_backend.h"
#define UMP_BLOCK_SIZE (256UL * 1024UL) /* 256kB, remember to keep the ()s */
typedef struct block_info {
struct block_info *next;
} block_info;
typedef struct block_allocator {
struct semaphore mutex;
block_info *all_blocks;
block_info *first_free;
u32 base;
u32 num_blocks;
u32 num_free;
} block_allocator;
static void block_allocator_shutdown(ump_memory_backend *backend);
static int block_allocator_allocate(void *ctx, ump_dd_mem *mem);
static void block_allocator_release(void *ctx, ump_dd_mem *handle);
static inline u32 get_phys(block_allocator *allocator, block_info *block);
static u32 block_allocator_stat(struct ump_memory_backend *backend);
/*
* Create dedicated memory backend
*/
ump_memory_backend *ump_block_allocator_create(u32 base_address, u32 size)
{
ump_memory_backend *backend;
block_allocator *allocator;
u32 usable_size;
u32 num_blocks;
usable_size = (size + UMP_BLOCK_SIZE - 1) & ~(UMP_BLOCK_SIZE - 1);
num_blocks = usable_size / UMP_BLOCK_SIZE;
if (0 == usable_size) {
DBG_MSG(1, ("Memory block of size %u is unusable\n", size));
return NULL;
}
DBG_MSG(5, ("Creating dedicated UMP memory backend. Base address: 0x%08x, size: 0x%08x\n", base_address, size));
DBG_MSG(6, ("%u usable bytes which becomes %u blocks\n", usable_size, num_blocks));
backend = kzalloc(sizeof(ump_memory_backend), GFP_KERNEL);
if (NULL != backend) {
allocator = kmalloc(sizeof(block_allocator), GFP_KERNEL);
if (NULL != allocator) {
allocator->all_blocks = kmalloc(sizeof(block_info) * num_blocks, GFP_KERNEL);
if (NULL != allocator->all_blocks) {
int i;
allocator->first_free = NULL;
allocator->num_blocks = num_blocks;
allocator->num_free = num_blocks;
allocator->base = base_address;
sema_init(&allocator->mutex, 1);
for (i = 0; i < num_blocks; i++) {
allocator->all_blocks[i].next = allocator->first_free;
allocator->first_free = &allocator->all_blocks[i];
}
backend->ctx = allocator;
backend->allocate = block_allocator_allocate;
backend->release = block_allocator_release;
backend->shutdown = block_allocator_shutdown;
backend->stat = block_allocator_stat;
backend->pre_allocate_physical_check = NULL;
backend->adjust_to_mali_phys = NULL;
return backend;
}
kfree(allocator);
}
kfree(backend);
}
return NULL;
}
/*
* Destroy specified dedicated memory backend
*/
static void block_allocator_shutdown(ump_memory_backend *backend)
{
block_allocator *allocator;
BUG_ON(!backend);
BUG_ON(!backend->ctx);
allocator = (block_allocator *)backend->ctx;
DBG_MSG_IF(1, allocator->num_free != allocator->num_blocks, ("%u blocks still in use during shutdown\n", allocator->num_blocks - allocator->num_free));
kfree(allocator->all_blocks);
kfree(allocator);
kfree(backend);
}
static int block_allocator_allocate(void *ctx, ump_dd_mem *mem)
{
block_allocator *allocator;
u32 left;
block_info *last_allocated = NULL;
int i = 0;
BUG_ON(!ctx);
BUG_ON(!mem);
allocator = (block_allocator *)ctx;
left = mem->size_bytes;
BUG_ON(!left);
BUG_ON(!&allocator->mutex);
mem->nr_blocks = ((left + UMP_BLOCK_SIZE - 1) & ~(UMP_BLOCK_SIZE - 1)) / UMP_BLOCK_SIZE;
mem->block_array = (ump_dd_physical_block *)vmalloc(sizeof(ump_dd_physical_block) * mem->nr_blocks);
if (NULL == mem->block_array) {
MSG_ERR(("Failed to allocate block array\n"));
return 0;
}
if (down_interruptible(&allocator->mutex)) {
MSG_ERR(("Could not get mutex to do block_allocate\n"));
return 0;
}
mem->size_bytes = 0;
while ((left > 0) && (allocator->first_free)) {
block_info *block;
block = allocator->first_free;
allocator->first_free = allocator->first_free->next;
block->next = last_allocated;
last_allocated = block;
allocator->num_free--;
mem->block_array[i].addr = get_phys(allocator, block);
mem->block_array[i].size = UMP_BLOCK_SIZE;
mem->size_bytes += UMP_BLOCK_SIZE;
i++;
if (left < UMP_BLOCK_SIZE) left = 0;
else left -= UMP_BLOCK_SIZE;
}
if (left) {
block_info *block;
/* release all memory back to the pool */
while (last_allocated) {
block = last_allocated->next;
last_allocated->next = allocator->first_free;
allocator->first_free = last_allocated;
last_allocated = block;
allocator->num_free++;
}
vfree(mem->block_array);
mem->backend_info = NULL;
mem->block_array = NULL;
DBG_MSG(4, ("Could not find a mem-block for the allocation.\n"));
up(&allocator->mutex);
return 0;
}
mem->backend_info = last_allocated;
up(&allocator->mutex);
mem->is_cached = 0;
return 1;
}
static void block_allocator_release(void *ctx, ump_dd_mem *handle)
{
block_allocator *allocator;
block_info *block, * next;
BUG_ON(!ctx);
BUG_ON(!handle);
allocator = (block_allocator *)ctx;
block = (block_info *)handle->backend_info;
BUG_ON(!block);
if (down_interruptible(&allocator->mutex)) {
MSG_ERR(("Allocator release: Failed to get mutex - memory leak\n"));
return;
}
while (block) {
next = block->next;
BUG_ON((block < allocator->all_blocks) || (block > (allocator->all_blocks + allocator->num_blocks)));
block->next = allocator->first_free;
allocator->first_free = block;
allocator->num_free++;
block = next;
}
DBG_MSG(3, ("%d blocks free after release call\n", allocator->num_free));
up(&allocator->mutex);
vfree(handle->block_array);
handle->block_array = NULL;
}
/*
* Helper function for calculating the physical base adderss of a memory block
*/
static inline u32 get_phys(block_allocator *allocator, block_info *block)
{
return allocator->base + ((block - allocator->all_blocks) * UMP_BLOCK_SIZE);
}
static u32 block_allocator_stat(struct ump_memory_backend *backend)
{
block_allocator *allocator;
BUG_ON(!backend);
allocator = (block_allocator *)backend->ctx;
BUG_ON(!allocator);
return (allocator->num_blocks - allocator->num_free) * UMP_BLOCK_SIZE;
}
|