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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
/*
* File: arch/blackfin/mm/blackfin_sram.c
* Based on:
* Author:
*
* Created:
* Description: SRAM driver for Blackfin ADSP-BF5xx
*
* Modified:
* Copyright 2004-2007 Analog Devices Inc.
*
* Bugs: Enter bugs at http://blackfin.uclinux.org/
*
* 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, see the file COPYING, or write
* to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/miscdevice.h>
#include <linux/ioport.h>
#include <linux/fcntl.h>
#include <linux/init.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/spinlock.h>
#include <linux/rtc.h>
#include <asm/blackfin.h>
#include "blackfin_sram.h"
spinlock_t l1sram_lock, l1_data_sram_lock, l1_inst_sram_lock;
#if CONFIG_L1_MAX_PIECE < 16
#undef CONFIG_L1_MAX_PIECE
#define CONFIG_L1_MAX_PIECE 16
#endif
#if CONFIG_L1_MAX_PIECE > 1024
#undef CONFIG_L1_MAX_PIECE
#define CONFIG_L1_MAX_PIECE 1024
#endif
#define SRAM_SLT_NULL 0
#define SRAM_SLT_FREE 1
#define SRAM_SLT_ALLOCATED 2
/* the data structure for L1 scratchpad and DATA SRAM */
struct l1_sram_piece {
void *paddr;
int size;
int flag;
pid_t pid;
};
static struct l1_sram_piece l1_ssram[CONFIG_L1_MAX_PIECE];
#if L1_DATA_A_LENGTH != 0
static struct l1_sram_piece l1_data_A_sram[CONFIG_L1_MAX_PIECE];
#endif
#if L1_DATA_B_LENGTH != 0
static struct l1_sram_piece l1_data_B_sram[CONFIG_L1_MAX_PIECE];
#endif
#if L1_CODE_LENGTH != 0
static struct l1_sram_piece l1_inst_sram[CONFIG_L1_MAX_PIECE];
#endif
/* L1 Scratchpad SRAM initialization function */
void __init l1sram_init(void)
{
printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
L1_SCRATCH_LENGTH >> 10);
memset(&l1_ssram, 0x00, sizeof(l1_ssram));
l1_ssram[0].paddr = (void *)L1_SCRATCH_START;
l1_ssram[0].size = L1_SCRATCH_LENGTH;
l1_ssram[0].flag = SRAM_SLT_FREE;
/* mutex initialize */
spin_lock_init(&l1sram_lock);
}
void __init l1_data_sram_init(void)
{
#if L1_DATA_A_LENGTH != 0
memset(&l1_data_A_sram, 0x00, sizeof(l1_data_A_sram));
l1_data_A_sram[0].paddr = (void *)L1_DATA_A_START +
(_ebss_l1 - _sdata_l1);
l1_data_A_sram[0].size = L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
l1_data_A_sram[0].flag = SRAM_SLT_FREE;
printk(KERN_INFO "Blackfin Data A SRAM: %d KB (%d KB free)\n",
L1_DATA_A_LENGTH >> 10, l1_data_A_sram[0].size >> 10);
#endif
#if L1_DATA_B_LENGTH != 0
memset(&l1_data_B_sram, 0x00, sizeof(l1_data_B_sram));
l1_data_B_sram[0].paddr = (void *)L1_DATA_B_START +
(_ebss_b_l1 - _sdata_b_l1);
l1_data_B_sram[0].size = L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
l1_data_B_sram[0].flag = SRAM_SLT_FREE;
printk(KERN_INFO "Blackfin Data B SRAM: %d KB (%d KB free)\n",
L1_DATA_B_LENGTH >> 10, l1_data_B_sram[0].size >> 10);
#endif
/* mutex initialize */
spin_lock_init(&l1_data_sram_lock);
}
void __init l1_inst_sram_init(void)
{
#if L1_CODE_LENGTH != 0
memset(&l1_inst_sram, 0x00, sizeof(l1_inst_sram));
l1_inst_sram[0].paddr = (void *)L1_CODE_START + (_etext_l1 - _stext_l1);
l1_inst_sram[0].size = L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
l1_inst_sram[0].flag = SRAM_SLT_FREE;
printk(KERN_INFO "Blackfin Instruction SRAM: %d KB (%d KB free)\n",
L1_CODE_LENGTH >> 10, l1_inst_sram[0].size >> 10);
#endif
/* mutex initialize */
spin_lock_init(&l1_inst_sram_lock);
}
/* L1 memory allocate function */
static void *_l1_sram_alloc(size_t size, struct l1_sram_piece *pfree, int count)
{
int i, index = 0;
void *addr = NULL;
if (size <= 0)
return NULL;
/* Align the size */
size = (size + 3) & ~3;
/* not use the good method to match the best slot !!! */
/* search an available memory slot */
for (i = 0; i < count; i++) {
if ((pfree[i].flag == SRAM_SLT_FREE)
&& (pfree[i].size >= size)) {
addr = pfree[i].paddr;
pfree[i].flag = SRAM_SLT_ALLOCATED;
pfree[i].pid = current->pid;
index = i;
break;
}
}
if (i >= count)
return NULL;
/* updated the NULL memory slot !!! */
if (pfree[i].size > size) {
for (i = 0; i < count; i++) {
if (pfree[i].flag == SRAM_SLT_NULL) {
pfree[i].pid = 0;
pfree[i].flag = SRAM_SLT_FREE;
pfree[i].paddr = addr + size;
pfree[i].size = pfree[index].size - size;
pfree[index].size = size;
break;
}
}
}
return addr;
}
/* Allocate the largest available block. */
static void *_l1_sram_alloc_max(struct l1_sram_piece *pfree, int count,
unsigned long *psize)
{
unsigned long best = 0;
int i, index = -1;
void *addr = NULL;
/* search an available memory slot */
for (i = 0; i < count; i++) {
if (pfree[i].flag == SRAM_SLT_FREE && pfree[i].size > best) {
addr = pfree[i].paddr;
index = i;
best = pfree[i].size;
}
}
if (index < 0)
return NULL;
*psize = best;
pfree[index].pid = current->pid;
pfree[index].flag = SRAM_SLT_ALLOCATED;
return addr;
}
/* L1 memory free function */
static int _l1_sram_free(const void *addr,
struct l1_sram_piece *pfree,
int count)
{
int i, index = 0;
/* search the relevant memory slot */
for (i = 0; i < count; i++) {
if (pfree[i].paddr == addr) {
if (pfree[i].flag != SRAM_SLT_ALLOCATED) {
/* error log */
return -1;
}
index = i;
break;
}
}
if (i >= count)
return -1;
pfree[index].pid = 0;
pfree[index].flag = SRAM_SLT_FREE;
/* link the next address slot */
for (i = 0; i < count; i++) {
if (((pfree[index].paddr + pfree[index].size) == pfree[i].paddr)
&& (pfree[i].flag == SRAM_SLT_FREE)) {
pfree[i].pid = 0;
pfree[i].flag = SRAM_SLT_NULL;
pfree[index].size += pfree[i].size;
pfree[index].flag = SRAM_SLT_FREE;
break;
}
}
/* link the last address slot */
for (i = 0; i < count; i++) {
if (((pfree[i].paddr + pfree[i].size) == pfree[index].paddr) &&
(pfree[i].flag == SRAM_SLT_FREE)) {
pfree[index].flag = SRAM_SLT_NULL;
pfree[i].size += pfree[index].size;
break;
}
}
return 0;
}
int sram_free(const void *addr)
{
if (0) {}
#if L1_CODE_LENGTH != 0
else if (addr >= (void *)L1_CODE_START
&& addr < (void *)(L1_CODE_START + L1_CODE_LENGTH))
return l1_inst_sram_free(addr);
#endif
#if L1_DATA_A_LENGTH != 0
else if (addr >= (void *)L1_DATA_A_START
&& addr < (void *)(L1_DATA_A_START + L1_DATA_A_LENGTH))
return l1_data_A_sram_free(addr);
#endif
#if L1_DATA_B_LENGTH != 0
else if (addr >= (void *)L1_DATA_B_START
&& addr < (void *)(L1_DATA_B_START + L1_DATA_B_LENGTH))
return l1_data_B_sram_free(addr);
#endif
else
return -1;
}
EXPORT_SYMBOL(sram_free);
void *l1_data_A_sram_alloc(size_t size)
{
unsigned flags;
void *addr = NULL;
/* add mutex operation */
spin_lock_irqsave(&l1_data_sram_lock, flags);
#if L1_DATA_A_LENGTH != 0
addr = _l1_sram_alloc(size, l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
#endif
/* add mutex operation */
spin_unlock_irqrestore(&l1_data_sram_lock, flags);
pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
(long unsigned int)addr, size);
return addr;
}
EXPORT_SYMBOL(l1_data_A_sram_alloc);
int l1_data_A_sram_free(const void *addr)
{
unsigned flags;
int ret;
/* add mutex operation */
spin_lock_irqsave(&l1_data_sram_lock, flags);
#if L1_DATA_A_LENGTH != 0
ret = _l1_sram_free(addr,
l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
#else
ret = -1;
#endif
/* add mutex operation */
spin_unlock_irqrestore(&l1_data_sram_lock, flags);
return ret;
}
EXPORT_SYMBOL(l1_data_A_sram_free);
void *l1_data_B_sram_alloc(size_t size)
{
#if L1_DATA_B_LENGTH != 0
unsigned flags;
void *addr;
/* add mutex operation */
spin_lock_irqsave(&l1_data_sram_lock, flags);
addr = _l1_sram_alloc(size, l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
/* add mutex operation */
spin_unlock_irqrestore(&l1_data_sram_lock, flags);
pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
(long unsigned int)addr, size);
return addr;
#else
return NULL;
#endif
}
EXPORT_SYMBOL(l1_data_B_sram_alloc);
int l1_data_B_sram_free(const void *addr)
{
#if L1_DATA_B_LENGTH != 0
unsigned flags;
int ret;
/* add mutex operation */
spin_lock_irqsave(&l1_data_sram_lock, flags);
ret = _l1_sram_free(addr, l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
/* add mutex operation */
spin_unlock_irqrestore(&l1_data_sram_lock, flags);
return ret;
#else
return -1;
#endif
}
EXPORT_SYMBOL(l1_data_B_sram_free);
void *l1_data_sram_alloc(size_t size)
{
void *addr = l1_data_A_sram_alloc(size);
if (!addr)
addr = l1_data_B_sram_alloc(size);
return addr;
}
EXPORT_SYMBOL(l1_data_sram_alloc);
void *l1_data_sram_zalloc(size_t size)
{
void *addr = l1_data_sram_alloc(size);
if (addr)
memset(addr, 0x00, size);
return addr;
}
EXPORT_SYMBOL(l1_data_sram_zalloc);
int l1_data_sram_free(const void *addr)
{
int ret;
ret = l1_data_A_sram_free(addr);
if (ret == -1)
ret = l1_data_B_sram_free(addr);
return ret;
}
EXPORT_SYMBOL(l1_data_sram_free);
void *l1_inst_sram_alloc(size_t size)
{
#if L1_DATA_A_LENGTH != 0
unsigned flags;
void *addr;
/* add mutex operation */
spin_lock_irqsave(&l1_inst_sram_lock, flags);
addr = _l1_sram_alloc(size, l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
/* add mutex operation */
spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
(long unsigned int)addr, size);
return addr;
#else
return NULL;
#endif
}
EXPORT_SYMBOL(l1_inst_sram_alloc);
int l1_inst_sram_free(const void *addr)
{
#if L1_CODE_LENGTH != 0
unsigned flags;
int ret;
/* add mutex operation */
spin_lock_irqsave(&l1_inst_sram_lock, flags);
ret = _l1_sram_free(addr, l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
/* add mutex operation */
spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
return ret;
#else
return -1;
#endif
}
EXPORT_SYMBOL(l1_inst_sram_free);
/* L1 Scratchpad memory allocate function */
void *l1sram_alloc(size_t size)
{
unsigned flags;
void *addr;
/* add mutex operation */
spin_lock_irqsave(&l1sram_lock, flags);
addr = _l1_sram_alloc(size, l1_ssram, ARRAY_SIZE(l1_ssram));
/* add mutex operation */
spin_unlock_irqrestore(&l1sram_lock, flags);
return addr;
}
/* L1 Scratchpad memory allocate function */
void *l1sram_alloc_max(size_t *psize)
{
unsigned flags;
void *addr;
/* add mutex operation */
spin_lock_irqsave(&l1sram_lock, flags);
addr = _l1_sram_alloc_max(l1_ssram, ARRAY_SIZE(l1_ssram), psize);
/* add mutex operation */
spin_unlock_irqrestore(&l1sram_lock, flags);
return addr;
}
/* L1 Scratchpad memory free function */
int l1sram_free(const void *addr)
{
unsigned flags;
int ret;
/* add mutex operation */
spin_lock_irqsave(&l1sram_lock, flags);
ret = _l1_sram_free(addr, l1_ssram, ARRAY_SIZE(l1_ssram));
/* add mutex operation */
spin_unlock_irqrestore(&l1sram_lock, flags);
return ret;
}
int sram_free_with_lsl(const void *addr)
{
struct sram_list_struct *lsl, **tmp;
struct mm_struct *mm = current->mm;
for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
if ((*tmp)->addr == addr)
goto found;
return -1;
found:
lsl = *tmp;
sram_free(addr);
*tmp = lsl->next;
kfree(lsl);
return 0;
}
EXPORT_SYMBOL(sram_free_with_lsl);
void *sram_alloc_with_lsl(size_t size, unsigned long flags)
{
void *addr = NULL;
struct sram_list_struct *lsl = NULL;
struct mm_struct *mm = current->mm;
lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
if (!lsl)
return NULL;
if (flags & L1_INST_SRAM)
addr = l1_inst_sram_alloc(size);
if (addr == NULL && (flags & L1_DATA_A_SRAM))
addr = l1_data_A_sram_alloc(size);
if (addr == NULL && (flags & L1_DATA_B_SRAM))
addr = l1_data_B_sram_alloc(size);
if (addr == NULL) {
kfree(lsl);
return NULL;
}
lsl->addr = addr;
lsl->length = size;
lsl->next = mm->context.sram_list;
mm->context.sram_list = lsl;
return addr;
}
EXPORT_SYMBOL(sram_alloc_with_lsl);
#ifdef CONFIG_PROC_FS
/* Once we get a real allocator, we'll throw all of this away.
* Until then, we need some sort of visibility into the L1 alloc.
*/
static void _l1sram_proc_read(char *buf, int *len, const char *desc,
struct l1_sram_piece *pfree, const int array_size)
{
int i;
*len += sprintf(&buf[*len], "--- L1 %-14s Size PID State\n", desc);
for (i = 0; i < array_size; ++i) {
const char *alloc_type;
switch (pfree[i].flag) {
case SRAM_SLT_NULL: alloc_type = "NULL"; break;
case SRAM_SLT_FREE: alloc_type = "FREE"; break;
case SRAM_SLT_ALLOCATED: alloc_type = "ALLOCATED"; break;
default: alloc_type = "????"; break;
}
*len += sprintf(&buf[*len], "%p-%p %8i %4i %s\n",
pfree[i].paddr, pfree[i].paddr + pfree[i].size,
pfree[i].size, pfree[i].pid, alloc_type);
}
}
static int l1sram_proc_read(char *buf, char **start, off_t offset, int count,
int *eof, void *data)
{
int len = 0;
_l1sram_proc_read(buf, &len, "Scratchpad",
l1_ssram, ARRAY_SIZE(l1_ssram));
#if L1_DATA_A_LENGTH != 0
_l1sram_proc_read(buf, &len, "Data A",
l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
#endif
#if L1_DATA_B_LENGTH != 0
_l1sram_proc_read(buf, &len, "Data B",
l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
#endif
#if L1_CODE_LENGTH != 0
_l1sram_proc_read(buf, &len, "Instruction",
l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
#endif
return len;
}
static int __init l1sram_proc_init(void)
{
struct proc_dir_entry *ptr;
ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
if (!ptr) {
printk(KERN_WARNING "unable to create /proc/sram\n");
return -1;
}
ptr->owner = THIS_MODULE;
ptr->read_proc = l1sram_proc_read;
return 0;
}
late_initcall(l1sram_proc_init);
#endif
|