-
Notifications
You must be signed in to change notification settings - Fork 1
/
memalloc.c
450 lines (371 loc) · 10.7 KB
/
memalloc.c
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* Memalloc, encoder memory allocation driver (kernel module)
*
* Copyright (C) 2009 Hantro Products Oy.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
--------------------------------------------------------------------------------
--
-- Abstract : Allocate memory blocks
--
--------------------------------------------------------------------------------
--
-- Version control information, please leave untouched.
--
-- $RCSfile: memalloc.c,v $
-- $Date: 2009/12/22 12:19:59 $
-- $Revision: 1.1 $
--
------------------------------------------------------------------------------*/
#include <linux/kernel.h>
#include <linux/module.h>
/* needed for __init,__exit directives */
#include <linux/init.h>
/* needed for remap_page_range */
#include <linux/mm.h>
/* obviously, for kmalloc */
#include <linux/slab.h>
/* for struct file_operations, register_chrdev() */
#include <linux/fs.h>
/* standard error codes */
#include <linux/errno.h>
/* this header files wraps some common module-space operations ...
here we use mem_map_reserve() macro */
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/ioport.h>
#include <linux/list.h>
/* for current pid */
#include <linux/sched.h>
/* Our header */
#include "memalloc.h"
/* module description */
MODULE_LICENSE("Proprietary");
MODULE_AUTHOR("Hantro Products Oy");
MODULE_DESCRIPTION("RAM allocation");
#define HLINA_START_ADDRESS 0x74000000 /* 64 MB for Linux, 64 MB for VDEC */
#ifndef HLINA_START_ADDRESS
#define HLINA_START_ADDRESS 0x02000000
#endif
#define MAX_OPEN 32
#define ID_UNUSED 0xFF
#define MEMALLOC_BASIC 0
#define MEMALLOC_MAX_OUTPUT 1
#define MEMALLOC_BASIC_X2 2
/* selects the memory allocation method, i.e. which allocation scheme table is used */
unsigned int alloc_method = MEMALLOC_BASIC;
static int memalloc_major = 0; /* dynamic */
int id[MAX_OPEN] = { ID_UNUSED };
/* module_param(name, type, perm) */
module_param(alloc_method, uint, 0);
/* here's all the must remember stuff */
struct allocation
{
struct list_head list;
void *buffer;
unsigned int order;
int fid;
};
struct list_head heap_list;
static DEFINE_SPINLOCK(mem_lock);
typedef struct hlinc
{
unsigned int bus_address;
unsigned int used;
unsigned int size;
int file_id;
} hlina_chunk;
static unsigned int *size_table = NULL;
static size_t chunks = 0;
unsigned int size_table_0[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1,
4, 4, 4, 4, 4, 4, 4, 4,
10, 10, 10, 10,
22, 22, 22, 22,
38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38,
50, 50, 50, 50, 50, 50, 50,
75, 75, 75, 75, 75,
86, 86, 86, 86, 86,
113, 113,
152, 152,
162, 162, 162,
270, 270, 270,
403, 403, 403, 403,
403, 403,
450, 450,
893, 893,
893, 893,
1999,
3997,
4096,
8192
};
unsigned int size_table_1[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0,
0, 0,
0, 0, 0,
0, 0, 0,
0, 0,
0, 64,
64, 128,
512,
3072,
8448
};
unsigned int size_table_2[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
10, 10, 10, 10, 10, 10, 10, 10,
22, 22, 22, 22, 22, 22, 22, 22,
38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38,
50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
75, 75, 75, 75, 75, 75, 75, 75, 75, 75,
86, 86, 86, 86, 86, 86, 86, 86, 86, 86,
113, 113, 113, 113,
152, 152, 152, 152,
162, 162, 162, 162, 162, 162,
270, 270, 270, 270, 270, 270,
403, 403, 403, 403, 403, 403, 403, 403,
403, 403, 403, 403,
450, 450, 450, 450,
893, 893, 893, 893,
893, 893, 893, 893,
1999, 1999,
3997, 3997,
4096, 4096,
8192, 8192
};
static hlina_chunk hlina_chunks[256];
static int AllocMemory(unsigned *busaddr, unsigned int size, struct file *filp);
static int FreeMemory(unsigned long busaddr);
static void ResetMems(void);
static long memalloc_ioctl(struct file *filp,
unsigned int cmd, unsigned long arg)
{
int err = 0;
int ret;
PDEBUG("ioctl cmd 0x%08x\n", cmd);
if(filp == NULL || arg == 0)
{
return -EFAULT;
}
/*
* extract the type and number bitfields, and don't decode
* wrong cmds: return ENOTTY (inappropriate ioctl) before access_ok()
*/
if(_IOC_TYPE(cmd) != MEMALLOC_IOC_MAGIC)
return -ENOTTY;
if(_IOC_NR(cmd) > MEMALLOC_IOC_MAXNR)
return -ENOTTY;
if(_IOC_DIR(cmd) & _IOC_READ)
err = !access_ok(VERIFY_WRITE, (void *) arg, _IOC_SIZE(cmd));
else if(_IOC_DIR(cmd) & _IOC_WRITE)
err = !access_ok(VERIFY_READ, (void *) arg, _IOC_SIZE(cmd));
if(err)
return -EFAULT;
switch (cmd)
{
case MEMALLOC_IOCHARDRESET:
PDEBUG("HARDRESET\n");
ResetMems();
break;
case MEMALLOC_IOCXGETBUFFER:
{
int result, ignored;
MemallocParams memparams;
PDEBUG("GETBUFFER\n");
spin_lock(&mem_lock);
ignored = copy_from_user(&memparams, (const void *) arg, sizeof(memparams));
result = AllocMemory(&memparams.busAddress, memparams.size, filp);
ignored = copy_to_user((void *) arg, &memparams, sizeof(memparams));
spin_unlock(&mem_lock);
return result;
}
case MEMALLOC_IOCSFREEBUFFER:
{
unsigned long busaddr;
PDEBUG("FREEBUFFER\n");
spin_lock(&mem_lock);
__get_user(busaddr, (unsigned long *) arg);
ret = FreeMemory(busaddr);
spin_unlock(&mem_lock);
return ret;
}
}
return 0;
}
static int memalloc_open(struct inode *inode, struct file *filp)
{
int i = 0;
for(i = 0; i < MAX_OPEN + 1; i++)
{
if(i == MAX_OPEN)
return -1;
if(id[i] == ID_UNUSED)
{
id[i] = i;
filp->private_data = id + i;
break;
}
}
PDEBUG("dev opened\n");
return 0;
}
static int memalloc_release(struct inode *inode, struct file *filp)
{
int i = 0;
for(i = 0; i < chunks; i++)
{
if(hlina_chunks[i].file_id == *((int *) (filp->private_data)))
{
hlina_chunks[i].used = 0;
hlina_chunks[i].file_id = ID_UNUSED;
}
}
*((int *) filp->private_data) = ID_UNUSED;
PDEBUG("dev closed\n");
return 0;
}
/* VFS methods */
static struct file_operations memalloc_fops = {
open:memalloc_open,
release:memalloc_release,
unlocked_ioctl:memalloc_ioctl,
};
int __init memalloc_init(void)
{
int result;
int i = 0;
PDEBUG("module init\n");
printk("memalloc: 8190 Linear Memory Allocator, %s \n", "$Revision: 1.1 $");
printk("memalloc: linear memory base = 0x%08x \n", HLINA_START_ADDRESS);
switch (alloc_method)
{
case MEMALLOC_MAX_OUTPUT:
size_table = size_table_1;
chunks = (sizeof(size_table_1) / sizeof(*size_table_1));
printk(KERN_INFO "memalloc: allocation method: MEMALLOC_MAX_OUTPUT\n");
break;
case MEMALLOC_BASIC_X2:
size_table = size_table_2;
chunks = (sizeof(size_table_2) / sizeof(*size_table_2));
printk(KERN_INFO "memalloc: allocation method: MEMALLOC_BASIC x 2\n");
break;
default:
size_table = size_table_0;
chunks = (sizeof(size_table_0) / sizeof(*size_table_0));
printk(KERN_INFO "memalloc: allocation method: MEMALLOC_BASIC\n");
break;
}
result = register_chrdev(memalloc_major, "memalloc", &memalloc_fops);
if(result < 0)
{
PDEBUG("memalloc: unable to get major %d\n", memalloc_major);
goto err;
}
else if(result != 0) /* this is for dynamic major */
{
memalloc_major = result;
}
ResetMems();
/* We keep a register of out customers, reset it */
for(i = 0; i < MAX_OPEN; i++)
{
id[i] = ID_UNUSED;
}
return 0;
err:
PDEBUG("memalloc: module not inserted\n");
unregister_chrdev(memalloc_major, "memalloc");
return result;
}
void __exit memalloc_cleanup(void)
{
PDEBUG("clenup called\n");
unregister_chrdev(memalloc_major, "memalloc");
PDEBUG("memalloc: module removed\n");
return;
}
module_init(memalloc_init);
module_exit(memalloc_cleanup);
/* Cycle through the buffers we have, give the first free one */
static int AllocMemory(unsigned *busaddr, unsigned int size, struct file *filp)
{
int i = 0;
*busaddr = 0;
for(i = 0; i < chunks; i++)
{
if(!hlina_chunks[i].used && (hlina_chunks[i].size >= size))
{
*busaddr = hlina_chunks[i].bus_address;
hlina_chunks[i].used = 1;
hlina_chunks[i].file_id = *((int *) (filp->private_data));
break;
}
}
if(*busaddr == 0)
{
printk("memalloc: Allocation FAILED: size = %d\n", size);
}
else
{
PDEBUG("MEMALLOC OK: size: %d, size reserved: %d\n", size,
hlina_chunks[i].size);
}
return 0;
}
/* Free a buffer based on bus address */
static int FreeMemory(unsigned long busaddr)
{
int i = 0;
for(i = 0; i < chunks; i++)
{
if(hlina_chunks[i].bus_address == busaddr)
{
hlina_chunks[i].used = 0;
hlina_chunks[i].file_id = ID_UNUSED;
}
}
return 0;
}
/* Reset "used" status */
void ResetMems(void)
{
int i = 0;
unsigned int ba = HLINA_START_ADDRESS;
for(i = 0; i < chunks; i++)
{
hlina_chunks[i].bus_address = ba;
hlina_chunks[i].used = 0;
hlina_chunks[i].file_id = ID_UNUSED;
hlina_chunks[i].size = 4096 * size_table[i];
ba += hlina_chunks[i].size;
}
printk("memalloc: %d bytes (%dMB) configured. Check RAM size!\n",
ba - (unsigned int)(HLINA_START_ADDRESS),
(ba - (unsigned int)(HLINA_START_ADDRESS)) / (1024 * 1024));
if(ba - (unsigned int)(HLINA_START_ADDRESS) > 96 * 1024 * 1024)
{
PDEBUG("MEMALLOC ERROR: MEMORY ALLOC BUG\n");
}
}