2 * File: arch/blackfin/mm/blackfin_sram.c
7 * Description: SRAM driver for Blackfin ADSP-BF5xx
10 * Copyright 2004-2007 Analog Devices Inc.
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <linux/autoconf.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/miscdevice.h>
35 #include <linux/ioport.h>
36 #include <linux/fcntl.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
39 #include <linux/proc_fs.h>
40 #include <linux/spinlock.h>
41 #include <linux/rtc.h>
42 #include <asm/blackfin.h>
43 #include "blackfin_sram.h"
45 spinlock_t l1sram_lock, l1_data_sram_lock, l1_inst_sram_lock;
47 #if CONFIG_L1_MAX_PIECE < 16
48 #undef CONFIG_L1_MAX_PIECE
49 #define CONFIG_L1_MAX_PIECE 16
52 #if CONFIG_L1_MAX_PIECE > 1024
53 #undef CONFIG_L1_MAX_PIECE
54 #define CONFIG_L1_MAX_PIECE 1024
57 #define SRAM_SLT_NULL 0
58 #define SRAM_SLT_FREE 1
59 #define SRAM_SLT_ALLOCATED 2
61 /* the data structure for L1 scratchpad and DATA SRAM */
62 struct l1_sram_piece {
69 static struct l1_sram_piece l1_ssram[CONFIG_L1_MAX_PIECE];
71 #if L1_DATA_A_LENGTH != 0
72 static struct l1_sram_piece l1_data_A_sram[CONFIG_L1_MAX_PIECE];
75 #if L1_DATA_B_LENGTH != 0
76 static struct l1_sram_piece l1_data_B_sram[CONFIG_L1_MAX_PIECE];
79 #if L1_CODE_LENGTH != 0
80 static struct l1_sram_piece l1_inst_sram[CONFIG_L1_MAX_PIECE];
83 /* L1 Scratchpad SRAM initialization function */
84 void __init l1sram_init(void)
86 printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
87 L1_SCRATCH_LENGTH >> 10);
89 memset(&l1_ssram, 0x00, sizeof(l1_ssram));
90 l1_ssram[0].paddr = (void *)L1_SCRATCH_START;
91 l1_ssram[0].size = L1_SCRATCH_LENGTH;
92 l1_ssram[0].flag = SRAM_SLT_FREE;
94 /* mutex initialize */
95 spin_lock_init(&l1sram_lock);
98 void __init l1_data_sram_init(void)
100 #if L1_DATA_A_LENGTH != 0
101 memset(&l1_data_A_sram, 0x00, sizeof(l1_data_A_sram));
102 l1_data_A_sram[0].paddr = (void *)L1_DATA_A_START +
103 (_ebss_l1 - _sdata_l1);
104 l1_data_A_sram[0].size = L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
105 l1_data_A_sram[0].flag = SRAM_SLT_FREE;
107 printk(KERN_INFO "Blackfin Data A SRAM: %d KB (%d KB free)\n",
108 L1_DATA_A_LENGTH >> 10, l1_data_A_sram[0].size >> 10);
110 #if L1_DATA_B_LENGTH != 0
111 memset(&l1_data_B_sram, 0x00, sizeof(l1_data_B_sram));
112 l1_data_B_sram[0].paddr = (void *)L1_DATA_B_START +
113 (_ebss_b_l1 - _sdata_b_l1);
114 l1_data_B_sram[0].size = L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
115 l1_data_B_sram[0].flag = SRAM_SLT_FREE;
117 printk(KERN_INFO "Blackfin Data B SRAM: %d KB (%d KB free)\n",
118 L1_DATA_B_LENGTH >> 10, l1_data_B_sram[0].size >> 10);
121 /* mutex initialize */
122 spin_lock_init(&l1_data_sram_lock);
125 void __init l1_inst_sram_init(void)
127 #if L1_CODE_LENGTH != 0
128 memset(&l1_inst_sram, 0x00, sizeof(l1_inst_sram));
129 l1_inst_sram[0].paddr = (void *)L1_CODE_START + (_etext_l1 - _stext_l1);
130 l1_inst_sram[0].size = L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
131 l1_inst_sram[0].flag = SRAM_SLT_FREE;
133 printk(KERN_INFO "Blackfin Instruction SRAM: %d KB (%d KB free)\n",
134 L1_CODE_LENGTH >> 10, l1_inst_sram[0].size >> 10);
137 /* mutex initialize */
138 spin_lock_init(&l1_inst_sram_lock);
141 /* L1 memory allocate function */
142 static void *_l1_sram_alloc(size_t size, struct l1_sram_piece *pfree, int count)
151 size = (size + 3) & ~3;
153 /* not use the good method to match the best slot !!! */
154 /* search an available memory slot */
155 for (i = 0; i < count; i++) {
156 if ((pfree[i].flag == SRAM_SLT_FREE)
157 && (pfree[i].size >= size)) {
158 addr = pfree[i].paddr;
159 pfree[i].flag = SRAM_SLT_ALLOCATED;
160 pfree[i].pid = current->pid;
168 /* updated the NULL memory slot !!! */
169 if (pfree[i].size > size) {
170 for (i = 0; i < count; i++) {
171 if (pfree[i].flag == SRAM_SLT_NULL) {
173 pfree[i].flag = SRAM_SLT_FREE;
174 pfree[i].paddr = addr + size;
175 pfree[i].size = pfree[index].size - size;
176 pfree[index].size = size;
185 /* Allocate the largest available block. */
186 static void *_l1_sram_alloc_max(struct l1_sram_piece *pfree, int count,
187 unsigned long *psize)
189 unsigned long best = 0;
193 /* search an available memory slot */
194 for (i = 0; i < count; i++) {
195 if (pfree[i].flag == SRAM_SLT_FREE && pfree[i].size > best) {
196 addr = pfree[i].paddr;
198 best = pfree[i].size;
205 pfree[index].pid = current->pid;
206 pfree[index].flag = SRAM_SLT_ALLOCATED;
210 /* L1 memory free function */
211 static int _l1_sram_free(const void *addr,
212 struct l1_sram_piece *pfree,
217 /* search the relevant memory slot */
218 for (i = 0; i < count; i++) {
219 if (pfree[i].paddr == addr) {
220 if (pfree[i].flag != SRAM_SLT_ALLOCATED) {
231 pfree[index].pid = 0;
232 pfree[index].flag = SRAM_SLT_FREE;
234 /* link the next address slot */
235 for (i = 0; i < count; i++) {
236 if (((pfree[index].paddr + pfree[index].size) == pfree[i].paddr)
237 && (pfree[i].flag == SRAM_SLT_FREE)) {
239 pfree[i].flag = SRAM_SLT_NULL;
240 pfree[index].size += pfree[i].size;
241 pfree[index].flag = SRAM_SLT_FREE;
246 /* link the last address slot */
247 for (i = 0; i < count; i++) {
248 if (((pfree[i].paddr + pfree[i].size) == pfree[index].paddr) &&
249 (pfree[i].flag == SRAM_SLT_FREE)) {
250 pfree[index].flag = SRAM_SLT_NULL;
251 pfree[i].size += pfree[index].size;
259 int sram_free(const void *addr)
262 #if L1_CODE_LENGTH != 0
263 else if (addr >= (void *)L1_CODE_START
264 && addr < (void *)(L1_CODE_START + L1_CODE_LENGTH))
265 return l1_inst_sram_free(addr);
267 #if L1_DATA_A_LENGTH != 0
268 else if (addr >= (void *)L1_DATA_A_START
269 && addr < (void *)(L1_DATA_A_START + L1_DATA_A_LENGTH))
270 return l1_data_A_sram_free(addr);
272 #if L1_DATA_B_LENGTH != 0
273 else if (addr >= (void *)L1_DATA_B_START
274 && addr < (void *)(L1_DATA_B_START + L1_DATA_B_LENGTH))
275 return l1_data_B_sram_free(addr);
280 EXPORT_SYMBOL(sram_free);
282 void *l1_data_A_sram_alloc(size_t size)
287 /* add mutex operation */
288 spin_lock_irqsave(&l1_data_sram_lock, flags);
290 #if L1_DATA_A_LENGTH != 0
291 addr = _l1_sram_alloc(size, l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
294 /* add mutex operation */
295 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
297 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
298 (long unsigned int)addr, size);
302 EXPORT_SYMBOL(l1_data_A_sram_alloc);
304 int l1_data_A_sram_free(const void *addr)
309 /* add mutex operation */
310 spin_lock_irqsave(&l1_data_sram_lock, flags);
312 #if L1_DATA_A_LENGTH != 0
313 ret = _l1_sram_free(addr,
314 l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
319 /* add mutex operation */
320 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
324 EXPORT_SYMBOL(l1_data_A_sram_free);
326 void *l1_data_B_sram_alloc(size_t size)
328 #if L1_DATA_B_LENGTH != 0
332 /* add mutex operation */
333 spin_lock_irqsave(&l1_data_sram_lock, flags);
335 addr = _l1_sram_alloc(size, l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
337 /* add mutex operation */
338 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
340 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
341 (long unsigned int)addr, size);
348 EXPORT_SYMBOL(l1_data_B_sram_alloc);
350 int l1_data_B_sram_free(const void *addr)
352 #if L1_DATA_B_LENGTH != 0
356 /* add mutex operation */
357 spin_lock_irqsave(&l1_data_sram_lock, flags);
359 ret = _l1_sram_free(addr, l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
361 /* add mutex operation */
362 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
369 EXPORT_SYMBOL(l1_data_B_sram_free);
371 void *l1_data_sram_alloc(size_t size)
373 void *addr = l1_data_A_sram_alloc(size);
376 addr = l1_data_B_sram_alloc(size);
380 EXPORT_SYMBOL(l1_data_sram_alloc);
382 void *l1_data_sram_zalloc(size_t size)
384 void *addr = l1_data_sram_alloc(size);
387 memset(addr, 0x00, size);
391 EXPORT_SYMBOL(l1_data_sram_zalloc);
393 int l1_data_sram_free(const void *addr)
396 ret = l1_data_A_sram_free(addr);
398 ret = l1_data_B_sram_free(addr);
401 EXPORT_SYMBOL(l1_data_sram_free);
403 void *l1_inst_sram_alloc(size_t size)
405 #if L1_DATA_A_LENGTH != 0
409 /* add mutex operation */
410 spin_lock_irqsave(&l1_inst_sram_lock, flags);
412 addr = _l1_sram_alloc(size, l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
414 /* add mutex operation */
415 spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
417 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
418 (long unsigned int)addr, size);
425 EXPORT_SYMBOL(l1_inst_sram_alloc);
427 int l1_inst_sram_free(const void *addr)
429 #if L1_CODE_LENGTH != 0
433 /* add mutex operation */
434 spin_lock_irqsave(&l1_inst_sram_lock, flags);
436 ret = _l1_sram_free(addr, l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
438 /* add mutex operation */
439 spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
446 EXPORT_SYMBOL(l1_inst_sram_free);
448 /* L1 Scratchpad memory allocate function */
449 void *l1sram_alloc(size_t size)
454 /* add mutex operation */
455 spin_lock_irqsave(&l1sram_lock, flags);
457 addr = _l1_sram_alloc(size, l1_ssram, ARRAY_SIZE(l1_ssram));
459 /* add mutex operation */
460 spin_unlock_irqrestore(&l1sram_lock, flags);
465 /* L1 Scratchpad memory allocate function */
466 void *l1sram_alloc_max(size_t *psize)
471 /* add mutex operation */
472 spin_lock_irqsave(&l1sram_lock, flags);
474 addr = _l1_sram_alloc_max(l1_ssram, ARRAY_SIZE(l1_ssram), psize);
476 /* add mutex operation */
477 spin_unlock_irqrestore(&l1sram_lock, flags);
482 /* L1 Scratchpad memory free function */
483 int l1sram_free(const void *addr)
488 /* add mutex operation */
489 spin_lock_irqsave(&l1sram_lock, flags);
491 ret = _l1_sram_free(addr, l1_ssram, ARRAY_SIZE(l1_ssram));
493 /* add mutex operation */
494 spin_unlock_irqrestore(&l1sram_lock, flags);
499 int sram_free_with_lsl(const void *addr)
501 struct sram_list_struct *lsl, **tmp;
502 struct mm_struct *mm = current->mm;
504 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
505 if ((*tmp)->addr == addr)
516 EXPORT_SYMBOL(sram_free_with_lsl);
518 void *sram_alloc_with_lsl(size_t size, unsigned long flags)
521 struct sram_list_struct *lsl = NULL;
522 struct mm_struct *mm = current->mm;
524 lsl = kmalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
527 memset(lsl, 0, sizeof(*lsl));
529 if (flags & L1_INST_SRAM)
530 addr = l1_inst_sram_alloc(size);
532 if (addr == NULL && (flags & L1_DATA_A_SRAM))
533 addr = l1_data_A_sram_alloc(size);
535 if (addr == NULL && (flags & L1_DATA_B_SRAM))
536 addr = l1_data_B_sram_alloc(size);
544 lsl->next = mm->context.sram_list;
545 mm->context.sram_list = lsl;
548 EXPORT_SYMBOL(sram_alloc_with_lsl);
550 #ifdef CONFIG_PROC_FS
551 /* Once we get a real allocator, we'll throw all of this away.
552 * Until then, we need some sort of visibility into the L1 alloc.
554 static void _l1sram_proc_read(char *buf, int *len, const char *desc,
555 struct l1_sram_piece *pfree, const int array_size)
559 *len += sprintf(&buf[*len], "--- L1 %-14s Size PID State\n", desc);
560 for (i = 0; i < array_size; ++i) {
561 const char *alloc_type;
562 switch (pfree[i].flag) {
563 case SRAM_SLT_NULL: alloc_type = "NULL"; break;
564 case SRAM_SLT_FREE: alloc_type = "FREE"; break;
565 case SRAM_SLT_ALLOCATED: alloc_type = "ALLOCATED"; break;
566 default: alloc_type = "????"; break;
568 *len += sprintf(&buf[*len], "%p-%p %8i %4i %s\n",
569 pfree[i].paddr, pfree[i].paddr + pfree[i].size,
570 pfree[i].size, pfree[i].pid, alloc_type);
573 static int l1sram_proc_read(char *buf, char **start, off_t offset, int count,
574 int *eof, void *data)
578 _l1sram_proc_read(buf, &len, "Scratchpad",
579 l1_ssram, ARRAY_SIZE(l1_ssram));
580 #if L1_DATA_A_LENGTH != 0
581 _l1sram_proc_read(buf, &len, "Data A",
582 l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
584 #if L1_DATA_B_LENGTH != 0
585 _l1sram_proc_read(buf, &len, "Data B",
586 l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
588 #if L1_CODE_LENGTH != 0
589 _l1sram_proc_read(buf, &len, "Instruction",
590 l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
596 static int __init l1sram_proc_init(void)
598 struct proc_dir_entry *ptr;
599 ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
601 printk(KERN_WARNING "unable to create /proc/sram\n");
604 ptr->owner = THIS_MODULE;
605 ptr->read_proc = l1sram_proc_read;
608 late_initcall(l1sram_proc_init);