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/module.h>
31 #include <linux/kernel.h>
32 #include <linux/types.h>
33 #include <linux/miscdevice.h>
34 #include <linux/ioport.h>
35 #include <linux/fcntl.h>
36 #include <linux/init.h>
37 #include <linux/poll.h>
38 #include <linux/proc_fs.h>
39 #include <linux/spinlock.h>
40 #include <linux/rtc.h>
41 #include <asm/blackfin.h>
42 #include "blackfin_sram.h"
44 static spinlock_t l1sram_lock, l1_data_sram_lock, l1_inst_sram_lock;
46 /* the data structure for L1 scratchpad and DATA SRAM */
51 struct sram_piece *next;
54 static struct sram_piece free_l1_ssram_head, used_l1_ssram_head;
56 #if L1_DATA_A_LENGTH != 0
57 static struct sram_piece free_l1_data_A_sram_head, used_l1_data_A_sram_head;
60 #if L1_DATA_B_LENGTH != 0
61 static struct sram_piece free_l1_data_B_sram_head, used_l1_data_B_sram_head;
64 #if L1_CODE_LENGTH != 0
65 static struct sram_piece free_l1_inst_sram_head, used_l1_inst_sram_head;
68 static struct kmem_cache *sram_piece_cache;
70 /* L1 Scratchpad SRAM initialization function */
71 static void __init l1sram_init(void)
73 free_l1_ssram_head.next =
74 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
75 if (!free_l1_ssram_head.next) {
76 printk(KERN_INFO"Fail to initialize Scratchpad data SRAM.\n");
80 free_l1_ssram_head.next->paddr = (void *)L1_SCRATCH_START;
81 free_l1_ssram_head.next->size = L1_SCRATCH_LENGTH;
82 free_l1_ssram_head.next->pid = 0;
83 free_l1_ssram_head.next->next = NULL;
85 used_l1_ssram_head.next = NULL;
87 /* mutex initialize */
88 spin_lock_init(&l1sram_lock);
90 printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
91 L1_SCRATCH_LENGTH >> 10);
94 static void __init l1_data_sram_init(void)
96 #if L1_DATA_A_LENGTH != 0
97 free_l1_data_A_sram_head.next =
98 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
99 if (!free_l1_data_A_sram_head.next) {
100 printk(KERN_INFO"Fail to initialize Data A SRAM.\n");
104 free_l1_data_A_sram_head.next->paddr =
105 (void *)L1_DATA_A_START + (_ebss_l1 - _sdata_l1);
106 free_l1_data_A_sram_head.next->size =
107 L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
108 free_l1_data_A_sram_head.next->pid = 0;
109 free_l1_data_A_sram_head.next->next = NULL;
111 used_l1_data_A_sram_head.next = NULL;
113 printk(KERN_INFO "Blackfin Data A SRAM: %d KB (%d KB free)\n",
114 L1_DATA_A_LENGTH >> 10,
115 free_l1_data_A_sram_head.next->size >> 10);
117 #if L1_DATA_B_LENGTH != 0
118 free_l1_data_B_sram_head.next =
119 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
120 if (!free_l1_data_B_sram_head.next) {
121 printk(KERN_INFO"Fail to initialize Data B SRAM.\n");
125 free_l1_data_B_sram_head.next->paddr =
126 (void *)L1_DATA_B_START + (_ebss_b_l1 - _sdata_b_l1);
127 free_l1_data_B_sram_head.next->size =
128 L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
129 free_l1_data_B_sram_head.next->pid = 0;
130 free_l1_data_B_sram_head.next->next = NULL;
132 used_l1_data_B_sram_head.next = NULL;
134 printk(KERN_INFO "Blackfin Data B SRAM: %d KB (%d KB free)\n",
135 L1_DATA_B_LENGTH >> 10,
136 free_l1_data_B_sram_head.next->size >> 10);
139 /* mutex initialize */
140 spin_lock_init(&l1_data_sram_lock);
143 static void __init l1_inst_sram_init(void)
145 #if L1_CODE_LENGTH != 0
146 free_l1_inst_sram_head.next =
147 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
148 if (!free_l1_inst_sram_head.next) {
149 printk(KERN_INFO"Fail to initialize Instruction SRAM.\n");
153 free_l1_inst_sram_head.next->paddr =
154 (void *)L1_CODE_START + (_etext_l1 - _stext_l1);
155 free_l1_inst_sram_head.next->size =
156 L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
157 free_l1_inst_sram_head.next->pid = 0;
158 free_l1_inst_sram_head.next->next = NULL;
160 used_l1_inst_sram_head.next = NULL;
162 printk(KERN_INFO "Blackfin Instruction SRAM: %d KB (%d KB free)\n",
163 L1_CODE_LENGTH >> 10,
164 free_l1_inst_sram_head.next->size >> 10);
167 /* mutex initialize */
168 spin_lock_init(&l1_inst_sram_lock);
171 void __init bfin_sram_init(void)
173 sram_piece_cache = kmem_cache_create("sram_piece_cache",
174 sizeof(struct sram_piece),
175 0, SLAB_PANIC, NULL);
182 /* L1 memory allocate function */
183 static void *_l1_sram_alloc(size_t size, struct sram_piece *pfree_head,
184 struct sram_piece *pused_head)
186 struct sram_piece *pslot, *plast, *pavail;
188 if (size <= 0 || !pfree_head || !pused_head)
192 size = (size + 3) & ~3;
194 pslot = pfree_head->next;
197 /* search an available piece slot */
198 while (pslot != NULL && size > pslot->size) {
206 if (pslot->size == size) {
207 plast->next = pslot->next;
210 pavail = kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
215 pavail->paddr = pslot->paddr;
217 pslot->paddr += size;
221 pavail->pid = current->pid;
223 pslot = pused_head->next;
226 /* insert new piece into used piece list !!! */
227 while (pslot != NULL && pavail->paddr < pslot->paddr) {
232 pavail->next = pslot;
233 plast->next = pavail;
235 return pavail->paddr;
238 /* Allocate the largest available block. */
239 static void *_l1_sram_alloc_max(struct sram_piece *pfree_head,
240 struct sram_piece *pused_head,
241 unsigned long *psize)
243 struct sram_piece *pslot, *pmax;
245 if (!pfree_head || !pused_head)
248 pmax = pslot = pfree_head->next;
250 /* search an available piece slot */
251 while (pslot != NULL) {
252 if (pslot->size > pmax->size)
262 return _l1_sram_alloc(*psize, pfree_head, pused_head);
265 /* L1 memory free function */
266 static int _l1_sram_free(const void *addr,
267 struct sram_piece *pfree_head,
268 struct sram_piece *pused_head)
270 struct sram_piece *pslot, *plast, *pavail;
272 if (!pfree_head || !pused_head)
275 /* search the relevant memory slot */
276 pslot = pused_head->next;
279 /* search an available piece slot */
280 while (pslot != NULL && pslot->paddr != addr) {
288 plast->next = pslot->next;
292 /* insert free pieces back to the free list */
293 pslot = pfree_head->next;
296 while (pslot != NULL && addr > pslot->paddr) {
301 if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
302 plast->size += pavail->size;
303 kmem_cache_free(sram_piece_cache, pavail);
305 pavail->next = plast;
306 plast->next = pavail;
310 if (pslot && plast->paddr + plast->size == pslot->paddr) {
311 plast->size += pslot->size;
312 plast->next = pslot->next;
313 kmem_cache_free(sram_piece_cache, pslot);
319 int sram_free(const void *addr)
322 #if L1_CODE_LENGTH != 0
323 else if (addr >= (void *)L1_CODE_START
324 && addr < (void *)(L1_CODE_START + L1_CODE_LENGTH))
325 return l1_inst_sram_free(addr);
327 #if L1_DATA_A_LENGTH != 0
328 else if (addr >= (void *)L1_DATA_A_START
329 && addr < (void *)(L1_DATA_A_START + L1_DATA_A_LENGTH))
330 return l1_data_A_sram_free(addr);
332 #if L1_DATA_B_LENGTH != 0
333 else if (addr >= (void *)L1_DATA_B_START
334 && addr < (void *)(L1_DATA_B_START + L1_DATA_B_LENGTH))
335 return l1_data_B_sram_free(addr);
340 EXPORT_SYMBOL(sram_free);
342 void *l1_data_A_sram_alloc(size_t size)
347 /* add mutex operation */
348 spin_lock_irqsave(&l1_data_sram_lock, flags);
350 #if L1_DATA_A_LENGTH != 0
351 addr = _l1_sram_alloc(size, &free_l1_data_A_sram_head,
352 &used_l1_data_A_sram_head);
355 /* add mutex operation */
356 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
358 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
359 (long unsigned int)addr, size);
363 EXPORT_SYMBOL(l1_data_A_sram_alloc);
365 int l1_data_A_sram_free(const void *addr)
370 /* add mutex operation */
371 spin_lock_irqsave(&l1_data_sram_lock, flags);
373 #if L1_DATA_A_LENGTH != 0
374 ret = _l1_sram_free(addr, &free_l1_data_A_sram_head,
375 &used_l1_data_A_sram_head);
380 /* add mutex operation */
381 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
385 EXPORT_SYMBOL(l1_data_A_sram_free);
387 void *l1_data_B_sram_alloc(size_t size)
389 #if L1_DATA_B_LENGTH != 0
393 /* add mutex operation */
394 spin_lock_irqsave(&l1_data_sram_lock, flags);
396 addr = _l1_sram_alloc(size, &free_l1_data_B_sram_head,
397 &used_l1_data_B_sram_head);
399 /* add mutex operation */
400 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
402 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
403 (long unsigned int)addr, size);
410 EXPORT_SYMBOL(l1_data_B_sram_alloc);
412 int l1_data_B_sram_free(const void *addr)
414 #if L1_DATA_B_LENGTH != 0
418 /* add mutex operation */
419 spin_lock_irqsave(&l1_data_sram_lock, flags);
421 ret = _l1_sram_free(addr, &free_l1_data_B_sram_head,
422 &used_l1_data_B_sram_head);
424 /* add mutex operation */
425 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
432 EXPORT_SYMBOL(l1_data_B_sram_free);
434 void *l1_data_sram_alloc(size_t size)
436 void *addr = l1_data_A_sram_alloc(size);
439 addr = l1_data_B_sram_alloc(size);
443 EXPORT_SYMBOL(l1_data_sram_alloc);
445 void *l1_data_sram_zalloc(size_t size)
447 void *addr = l1_data_sram_alloc(size);
450 memset(addr, 0x00, size);
454 EXPORT_SYMBOL(l1_data_sram_zalloc);
456 int l1_data_sram_free(const void *addr)
459 ret = l1_data_A_sram_free(addr);
461 ret = l1_data_B_sram_free(addr);
464 EXPORT_SYMBOL(l1_data_sram_free);
466 void *l1_inst_sram_alloc(size_t size)
468 #if L1_CODE_LENGTH != 0
472 /* add mutex operation */
473 spin_lock_irqsave(&l1_inst_sram_lock, flags);
475 addr = _l1_sram_alloc(size, &free_l1_inst_sram_head,
476 &used_l1_inst_sram_head);
478 /* add mutex operation */
479 spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
481 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
482 (long unsigned int)addr, size);
489 EXPORT_SYMBOL(l1_inst_sram_alloc);
491 int l1_inst_sram_free(const void *addr)
493 #if L1_CODE_LENGTH != 0
497 /* add mutex operation */
498 spin_lock_irqsave(&l1_inst_sram_lock, flags);
500 ret = _l1_sram_free(addr, &free_l1_inst_sram_head,
501 &used_l1_inst_sram_head);
503 /* add mutex operation */
504 spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
511 EXPORT_SYMBOL(l1_inst_sram_free);
513 /* L1 Scratchpad memory allocate function */
514 void *l1sram_alloc(size_t size)
519 /* add mutex operation */
520 spin_lock_irqsave(&l1sram_lock, flags);
522 addr = _l1_sram_alloc(size, &free_l1_ssram_head,
523 &used_l1_ssram_head);
525 /* add mutex operation */
526 spin_unlock_irqrestore(&l1sram_lock, flags);
531 /* L1 Scratchpad memory allocate function */
532 void *l1sram_alloc_max(size_t *psize)
537 /* add mutex operation */
538 spin_lock_irqsave(&l1sram_lock, flags);
540 addr = _l1_sram_alloc_max(&free_l1_ssram_head,
541 &used_l1_ssram_head, psize);
543 /* add mutex operation */
544 spin_unlock_irqrestore(&l1sram_lock, flags);
549 /* L1 Scratchpad memory free function */
550 int l1sram_free(const void *addr)
555 /* add mutex operation */
556 spin_lock_irqsave(&l1sram_lock, flags);
558 ret = _l1_sram_free(addr, &free_l1_ssram_head,
559 &used_l1_ssram_head);
561 /* add mutex operation */
562 spin_unlock_irqrestore(&l1sram_lock, flags);
567 int sram_free_with_lsl(const void *addr)
569 struct sram_list_struct *lsl, **tmp;
570 struct mm_struct *mm = current->mm;
572 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
573 if ((*tmp)->addr == addr)
584 EXPORT_SYMBOL(sram_free_with_lsl);
586 void *sram_alloc_with_lsl(size_t size, unsigned long flags)
589 struct sram_list_struct *lsl = NULL;
590 struct mm_struct *mm = current->mm;
592 lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
596 if (flags & L1_INST_SRAM)
597 addr = l1_inst_sram_alloc(size);
599 if (addr == NULL && (flags & L1_DATA_A_SRAM))
600 addr = l1_data_A_sram_alloc(size);
602 if (addr == NULL && (flags & L1_DATA_B_SRAM))
603 addr = l1_data_B_sram_alloc(size);
611 lsl->next = mm->context.sram_list;
612 mm->context.sram_list = lsl;
615 EXPORT_SYMBOL(sram_alloc_with_lsl);
617 #ifdef CONFIG_PROC_FS
618 /* Once we get a real allocator, we'll throw all of this away.
619 * Until then, we need some sort of visibility into the L1 alloc.
621 /* Need to keep line of output the same. Currently, that is 44 bytes
622 * (including newline).
624 static int _l1sram_proc_read(char *buf, int *len, int count, const char *desc,
625 struct sram_piece *pfree_head,
626 struct sram_piece *pused_head)
628 struct sram_piece *pslot;
630 if (!pfree_head || !pused_head)
633 *len += sprintf(&buf[*len], "--- L1 %-14s Size PID State \n", desc);
635 /* search the relevant memory slot */
636 pslot = pused_head->next;
638 while (pslot != NULL) {
639 *len += sprintf(&buf[*len], "%p-%p %8i %5i %-10s\n",
640 pslot->paddr, pslot->paddr + pslot->size,
641 pslot->size, pslot->pid, "ALLOCATED");
646 pslot = pfree_head->next;
648 while (pslot != NULL) {
649 *len += sprintf(&buf[*len], "%p-%p %8i %5i %-10s\n",
650 pslot->paddr, pslot->paddr + pslot->size,
651 pslot->size, pslot->pid, "FREE");
658 static int l1sram_proc_read(char *buf, char **start, off_t offset, int count,
659 int *eof, void *data)
663 if (_l1sram_proc_read(buf, &len, count, "Scratchpad",
664 &free_l1_ssram_head, &used_l1_ssram_head))
666 #if L1_DATA_A_LENGTH != 0
667 if (_l1sram_proc_read(buf, &len, count, "Data A",
668 &free_l1_data_A_sram_head,
669 &used_l1_data_A_sram_head))
672 #if L1_DATA_B_LENGTH != 0
673 if (_l1sram_proc_read(buf, &len, count, "Data B",
674 &free_l1_data_B_sram_head,
675 &used_l1_data_B_sram_head))
678 #if L1_CODE_LENGTH != 0
679 if (_l1sram_proc_read(buf, &len, count, "Instruction",
680 &free_l1_inst_sram_head, &used_l1_inst_sram_head))
689 static int __init l1sram_proc_init(void)
691 struct proc_dir_entry *ptr;
692 ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
694 printk(KERN_WARNING "unable to create /proc/sram\n");
697 ptr->owner = THIS_MODULE;
698 ptr->read_proc = l1sram_proc_read;
701 late_initcall(l1sram_proc_init);