2 * File: arch/blackfin/mm/sram-alloc.c
7 * Description: SRAM allocator for Blackfin L1 and L2 memory
10 * Copyright 2004-2008 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 DEFINE_PER_CPU(spinlock_t, l1sram_lock) ____cacheline_aligned_in_smp;
45 static DEFINE_PER_CPU(spinlock_t, l1_data_sram_lock) ____cacheline_aligned_in_smp;
46 static DEFINE_PER_CPU(spinlock_t, l1_inst_sram_lock) ____cacheline_aligned_in_smp;
47 static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp;
49 /* the data structure for L1 scratchpad and DATA SRAM */
54 struct sram_piece *next;
57 static DEFINE_PER_CPU(struct sram_piece, free_l1_ssram_head);
58 static DEFINE_PER_CPU(struct sram_piece, used_l1_ssram_head);
60 #if L1_DATA_A_LENGTH != 0
61 static DEFINE_PER_CPU(struct sram_piece, free_l1_data_A_sram_head);
62 static DEFINE_PER_CPU(struct sram_piece, used_l1_data_A_sram_head);
65 #if L1_DATA_B_LENGTH != 0
66 static DEFINE_PER_CPU(struct sram_piece, free_l1_data_B_sram_head);
67 static DEFINE_PER_CPU(struct sram_piece, used_l1_data_B_sram_head);
70 #if L1_CODE_LENGTH != 0
71 static DEFINE_PER_CPU(struct sram_piece, free_l1_inst_sram_head);
72 static DEFINE_PER_CPU(struct sram_piece, used_l1_inst_sram_head);
76 static struct sram_piece free_l2_sram_head, used_l2_sram_head;
79 static struct kmem_cache *sram_piece_cache;
81 /* L1 Scratchpad SRAM initialization function */
82 static void __init l1sram_init(void)
85 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
86 per_cpu(free_l1_ssram_head, cpu).next =
87 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
88 if (!per_cpu(free_l1_ssram_head, cpu).next) {
89 printk(KERN_INFO "Fail to initialize Scratchpad data SRAM.\n");
93 per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu);
94 per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH;
95 per_cpu(free_l1_ssram_head, cpu).next->pid = 0;
96 per_cpu(free_l1_ssram_head, cpu).next->next = NULL;
98 per_cpu(used_l1_ssram_head, cpu).next = NULL;
100 /* mutex initialize */
101 spin_lock_init(&per_cpu(l1sram_lock, cpu));
102 printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
103 L1_SCRATCH_LENGTH >> 10);
107 static void __init l1_data_sram_init(void)
109 #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
112 #if L1_DATA_A_LENGTH != 0
113 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
114 per_cpu(free_l1_data_A_sram_head, cpu).next =
115 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
116 if (!per_cpu(free_l1_data_A_sram_head, cpu).next) {
117 printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n");
121 per_cpu(free_l1_data_A_sram_head, cpu).next->paddr =
122 (void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1);
123 per_cpu(free_l1_data_A_sram_head, cpu).next->size =
124 L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
125 per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0;
126 per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL;
128 per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;
130 printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n",
131 L1_DATA_A_LENGTH >> 10,
132 per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10);
135 #if L1_DATA_B_LENGTH != 0
136 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
137 per_cpu(free_l1_data_B_sram_head, cpu).next =
138 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
139 if (!per_cpu(free_l1_data_B_sram_head, cpu).next) {
140 printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n");
144 per_cpu(free_l1_data_B_sram_head, cpu).next->paddr =
145 (void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1);
146 per_cpu(free_l1_data_B_sram_head, cpu).next->size =
147 L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
148 per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0;
149 per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL;
151 per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;
153 printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n",
154 L1_DATA_B_LENGTH >> 10,
155 per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10);
156 /* mutex initialize */
160 #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
161 for (cpu = 0; cpu < num_possible_cpus(); ++cpu)
162 spin_lock_init(&per_cpu(l1_data_sram_lock, cpu));
166 static void __init l1_inst_sram_init(void)
168 #if L1_CODE_LENGTH != 0
170 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
171 per_cpu(free_l1_inst_sram_head, cpu).next =
172 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
173 if (!per_cpu(free_l1_inst_sram_head, cpu).next) {
174 printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n");
178 per_cpu(free_l1_inst_sram_head, cpu).next->paddr =
179 (void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1);
180 per_cpu(free_l1_inst_sram_head, cpu).next->size =
181 L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
182 per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0;
183 per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL;
185 per_cpu(used_l1_inst_sram_head, cpu).next = NULL;
187 printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n",
188 L1_CODE_LENGTH >> 10,
189 per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10);
191 /* mutex initialize */
192 spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
197 static void __init l2_sram_init(void)
200 free_l2_sram_head.next =
201 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
202 if (!free_l2_sram_head.next) {
203 printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
207 free_l2_sram_head.next->paddr =
208 (void *)L2_START + (_ebss_l2 - _stext_l2);
209 free_l2_sram_head.next->size =
210 L2_LENGTH - (_ebss_l2 - _stext_l2);
211 free_l2_sram_head.next->pid = 0;
212 free_l2_sram_head.next->next = NULL;
214 used_l2_sram_head.next = NULL;
216 printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n",
218 free_l2_sram_head.next->size >> 10);
221 /* mutex initialize */
222 spin_lock_init(&l2_sram_lock);
225 void __init bfin_sram_init(void)
227 sram_piece_cache = kmem_cache_create("sram_piece_cache",
228 sizeof(struct sram_piece),
229 0, SLAB_PANIC, NULL);
237 /* SRAM allocate function */
238 static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
239 struct sram_piece *pused_head)
241 struct sram_piece *pslot, *plast, *pavail;
243 if (size <= 0 || !pfree_head || !pused_head)
247 size = (size + 3) & ~3;
249 pslot = pfree_head->next;
252 /* search an available piece slot */
253 while (pslot != NULL && size > pslot->size) {
261 if (pslot->size == size) {
262 plast->next = pslot->next;
265 pavail = kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
270 pavail->paddr = pslot->paddr;
272 pslot->paddr += size;
276 pavail->pid = current->pid;
278 pslot = pused_head->next;
281 /* insert new piece into used piece list !!! */
282 while (pslot != NULL && pavail->paddr < pslot->paddr) {
287 pavail->next = pslot;
288 plast->next = pavail;
290 return pavail->paddr;
293 /* Allocate the largest available block. */
294 static void *_sram_alloc_max(struct sram_piece *pfree_head,
295 struct sram_piece *pused_head,
296 unsigned long *psize)
298 struct sram_piece *pslot, *pmax;
300 if (!pfree_head || !pused_head)
303 pmax = pslot = pfree_head->next;
305 /* search an available piece slot */
306 while (pslot != NULL) {
307 if (pslot->size > pmax->size)
317 return _sram_alloc(*psize, pfree_head, pused_head);
320 /* SRAM free function */
321 static int _sram_free(const void *addr,
322 struct sram_piece *pfree_head,
323 struct sram_piece *pused_head)
325 struct sram_piece *pslot, *plast, *pavail;
327 if (!pfree_head || !pused_head)
330 /* search the relevant memory slot */
331 pslot = pused_head->next;
334 /* search an available piece slot */
335 while (pslot != NULL && pslot->paddr != addr) {
343 plast->next = pslot->next;
347 /* insert free pieces back to the free list */
348 pslot = pfree_head->next;
351 while (pslot != NULL && addr > pslot->paddr) {
356 if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
357 plast->size += pavail->size;
358 kmem_cache_free(sram_piece_cache, pavail);
360 pavail->next = plast->next;
361 plast->next = pavail;
365 if (pslot && plast->paddr + plast->size == pslot->paddr) {
366 plast->size += pslot->size;
367 plast->next = pslot->next;
368 kmem_cache_free(sram_piece_cache, pslot);
374 int sram_free(const void *addr)
377 #if L1_CODE_LENGTH != 0
378 if (addr >= (void *)get_l1_code_start()
379 && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
380 return l1_inst_sram_free(addr);
383 #if L1_DATA_A_LENGTH != 0
384 if (addr >= (void *)get_l1_data_a_start()
385 && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH))
386 return l1_data_A_sram_free(addr);
389 #if L1_DATA_B_LENGTH != 0
390 if (addr >= (void *)get_l1_data_b_start()
391 && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH))
392 return l1_data_B_sram_free(addr);
396 if (addr >= (void *)L2_START
397 && addr < (void *)(L2_START + L2_LENGTH))
398 return l2_sram_free(addr);
403 EXPORT_SYMBOL(sram_free);
405 void *l1_data_A_sram_alloc(size_t size)
412 /* add mutex operation */
413 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
415 #if L1_DATA_A_LENGTH != 0
416 addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
417 &per_cpu(used_l1_data_A_sram_head, cpu));
420 /* add mutex operation */
421 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
424 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
425 (long unsigned int)addr, size);
429 EXPORT_SYMBOL(l1_data_A_sram_alloc);
431 int l1_data_A_sram_free(const void *addr)
438 /* add mutex operation */
439 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
441 #if L1_DATA_A_LENGTH != 0
442 ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu),
443 &per_cpu(used_l1_data_A_sram_head, cpu));
448 /* add mutex operation */
449 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
454 EXPORT_SYMBOL(l1_data_A_sram_free);
456 void *l1_data_B_sram_alloc(size_t size)
458 #if L1_DATA_B_LENGTH != 0
464 /* add mutex operation */
465 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
467 addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
468 &per_cpu(used_l1_data_B_sram_head, cpu));
470 /* add mutex operation */
471 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
474 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
475 (long unsigned int)addr, size);
482 EXPORT_SYMBOL(l1_data_B_sram_alloc);
484 int l1_data_B_sram_free(const void *addr)
486 #if L1_DATA_B_LENGTH != 0
492 /* add mutex operation */
493 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
495 ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu),
496 &per_cpu(used_l1_data_B_sram_head, cpu));
498 /* add mutex operation */
499 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
507 EXPORT_SYMBOL(l1_data_B_sram_free);
509 void *l1_data_sram_alloc(size_t size)
511 void *addr = l1_data_A_sram_alloc(size);
514 addr = l1_data_B_sram_alloc(size);
518 EXPORT_SYMBOL(l1_data_sram_alloc);
520 void *l1_data_sram_zalloc(size_t size)
522 void *addr = l1_data_sram_alloc(size);
525 memset(addr, 0x00, size);
529 EXPORT_SYMBOL(l1_data_sram_zalloc);
531 int l1_data_sram_free(const void *addr)
534 ret = l1_data_A_sram_free(addr);
536 ret = l1_data_B_sram_free(addr);
539 EXPORT_SYMBOL(l1_data_sram_free);
541 void *l1_inst_sram_alloc(size_t size)
543 #if L1_CODE_LENGTH != 0
549 /* add mutex operation */
550 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
552 addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
553 &per_cpu(used_l1_inst_sram_head, cpu));
555 /* add mutex operation */
556 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
559 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
560 (long unsigned int)addr, size);
567 EXPORT_SYMBOL(l1_inst_sram_alloc);
569 int l1_inst_sram_free(const void *addr)
571 #if L1_CODE_LENGTH != 0
577 /* add mutex operation */
578 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
580 ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
581 &per_cpu(used_l1_inst_sram_head, cpu));
583 /* add mutex operation */
584 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
592 EXPORT_SYMBOL(l1_inst_sram_free);
594 /* L1 Scratchpad memory allocate function */
595 void *l1sram_alloc(size_t size)
602 /* add mutex operation */
603 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
605 addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu),
606 &per_cpu(used_l1_ssram_head, cpu));
608 /* add mutex operation */
609 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
615 /* L1 Scratchpad memory allocate function */
616 void *l1sram_alloc_max(size_t *psize)
623 /* add mutex operation */
624 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
626 addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
627 &per_cpu(used_l1_ssram_head, cpu), psize);
629 /* add mutex operation */
630 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
636 /* L1 Scratchpad memory free function */
637 int l1sram_free(const void *addr)
644 /* add mutex operation */
645 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
647 ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
648 &per_cpu(used_l1_ssram_head, cpu));
650 /* add mutex operation */
651 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
657 void *l2_sram_alloc(size_t size)
663 /* add mutex operation */
664 spin_lock_irqsave(&l2_sram_lock, flags);
666 addr = _sram_alloc(size, &free_l2_sram_head,
669 /* add mutex operation */
670 spin_unlock_irqrestore(&l2_sram_lock, flags);
672 pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
673 (long unsigned int)addr, size);
680 EXPORT_SYMBOL(l2_sram_alloc);
682 void *l2_sram_zalloc(size_t size)
684 void *addr = l2_sram_alloc(size);
687 memset(addr, 0x00, size);
691 EXPORT_SYMBOL(l2_sram_zalloc);
693 int l2_sram_free(const void *addr)
699 /* add mutex operation */
700 spin_lock_irqsave(&l2_sram_lock, flags);
702 ret = _sram_free(addr, &free_l2_sram_head,
705 /* add mutex operation */
706 spin_unlock_irqrestore(&l2_sram_lock, flags);
713 EXPORT_SYMBOL(l2_sram_free);
715 int sram_free_with_lsl(const void *addr)
717 struct sram_list_struct *lsl, **tmp;
718 struct mm_struct *mm = current->mm;
720 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
721 if ((*tmp)->addr == addr)
732 EXPORT_SYMBOL(sram_free_with_lsl);
734 void *sram_alloc_with_lsl(size_t size, unsigned long flags)
737 struct sram_list_struct *lsl = NULL;
738 struct mm_struct *mm = current->mm;
740 lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
744 if (flags & L1_INST_SRAM)
745 addr = l1_inst_sram_alloc(size);
747 if (addr == NULL && (flags & L1_DATA_A_SRAM))
748 addr = l1_data_A_sram_alloc(size);
750 if (addr == NULL && (flags & L1_DATA_B_SRAM))
751 addr = l1_data_B_sram_alloc(size);
753 if (addr == NULL && (flags & L2_SRAM))
754 addr = l2_sram_alloc(size);
762 lsl->next = mm->context.sram_list;
763 mm->context.sram_list = lsl;
766 EXPORT_SYMBOL(sram_alloc_with_lsl);
768 #ifdef CONFIG_PROC_FS
769 /* Once we get a real allocator, we'll throw all of this away.
770 * Until then, we need some sort of visibility into the L1 alloc.
772 /* Need to keep line of output the same. Currently, that is 44 bytes
773 * (including newline).
775 static int _sram_proc_read(char *buf, int *len, int count, const char *desc,
776 struct sram_piece *pfree_head,
777 struct sram_piece *pused_head)
779 struct sram_piece *pslot;
781 if (!pfree_head || !pused_head)
784 *len += sprintf(&buf[*len], "--- SRAM %-14s Size PID State \n", desc);
786 /* search the relevant memory slot */
787 pslot = pused_head->next;
789 while (pslot != NULL) {
790 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
791 pslot->paddr, pslot->paddr + pslot->size,
792 pslot->size, pslot->pid, "ALLOCATED");
797 pslot = pfree_head->next;
799 while (pslot != NULL) {
800 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
801 pslot->paddr, pslot->paddr + pslot->size,
802 pslot->size, pslot->pid, "FREE");
809 static int sram_proc_read(char *buf, char **start, off_t offset, int count,
810 int *eof, void *data)
815 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
816 if (_sram_proc_read(buf, &len, count, "Scratchpad",
817 &per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu)))
819 #if L1_DATA_A_LENGTH != 0
820 if (_sram_proc_read(buf, &len, count, "L1 Data A",
821 &per_cpu(free_l1_data_A_sram_head, cpu),
822 &per_cpu(used_l1_data_A_sram_head, cpu)))
825 #if L1_DATA_B_LENGTH != 0
826 if (_sram_proc_read(buf, &len, count, "L1 Data B",
827 &per_cpu(free_l1_data_B_sram_head, cpu),
828 &per_cpu(used_l1_data_B_sram_head, cpu)))
831 #if L1_CODE_LENGTH != 0
832 if (_sram_proc_read(buf, &len, count, "L1 Instruction",
833 &per_cpu(free_l1_inst_sram_head, cpu),
834 &per_cpu(used_l1_inst_sram_head, cpu)))
839 if (_sram_proc_read(buf, &len, count, "L2", &free_l2_sram_head,
848 static int __init sram_proc_init(void)
850 struct proc_dir_entry *ptr;
851 ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
853 printk(KERN_WARNING "unable to create /proc/sram\n");
856 ptr->owner = THIS_MODULE;
857 ptr->read_proc = sram_proc_read;
860 late_initcall(sram_proc_init);