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)
110 #if L1_DATA_A_LENGTH != 0
111 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
112 per_cpu(free_l1_data_A_sram_head, cpu).next =
113 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
114 if (!per_cpu(free_l1_data_A_sram_head, cpu).next) {
115 printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n");
119 per_cpu(free_l1_data_A_sram_head, cpu).next->paddr =
120 (void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1);
121 per_cpu(free_l1_data_A_sram_head, cpu).next->size =
122 L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
123 per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0;
124 per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL;
126 per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;
128 printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n",
129 L1_DATA_A_LENGTH >> 10,
130 per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10);
133 #if L1_DATA_B_LENGTH != 0
134 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
135 per_cpu(free_l1_data_B_sram_head, cpu).next =
136 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
137 if (!per_cpu(free_l1_data_B_sram_head, cpu).next) {
138 printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n");
142 per_cpu(free_l1_data_B_sram_head, cpu).next->paddr =
143 (void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1);
144 per_cpu(free_l1_data_B_sram_head, cpu).next->size =
145 L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
146 per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0;
147 per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL;
149 per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;
151 printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n",
152 L1_DATA_B_LENGTH >> 10,
153 per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10);
154 /* mutex initialize */
158 #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
159 for (cpu = 0; cpu < num_possible_cpus(); ++cpu)
160 spin_lock_init(&per_cpu(l1_data_sram_lock, cpu));
164 static void __init l1_inst_sram_init(void)
166 #if L1_CODE_LENGTH != 0
168 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
169 per_cpu(free_l1_inst_sram_head, cpu).next =
170 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
171 if (!per_cpu(free_l1_inst_sram_head, cpu).next) {
172 printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n");
176 per_cpu(free_l1_inst_sram_head, cpu).next->paddr =
177 (void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1);
178 per_cpu(free_l1_inst_sram_head, cpu).next->size =
179 L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
180 per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0;
181 per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL;
183 per_cpu(used_l1_inst_sram_head, cpu).next = NULL;
185 printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n",
186 L1_CODE_LENGTH >> 10,
187 per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10);
189 /* mutex initialize */
190 spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
195 static void __init l2_sram_init(void)
198 free_l2_sram_head.next =
199 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
200 if (!free_l2_sram_head.next) {
201 printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
205 free_l2_sram_head.next->paddr =
206 (void *)L2_START + (_ebss_l2 - _stext_l2);
207 free_l2_sram_head.next->size =
208 L2_LENGTH - (_ebss_l2 - _stext_l2);
209 free_l2_sram_head.next->pid = 0;
210 free_l2_sram_head.next->next = NULL;
212 used_l2_sram_head.next = NULL;
214 printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n",
216 free_l2_sram_head.next->size >> 10);
219 /* mutex initialize */
220 spin_lock_init(&l2_sram_lock);
223 void __init bfin_sram_init(void)
225 sram_piece_cache = kmem_cache_create("sram_piece_cache",
226 sizeof(struct sram_piece),
227 0, SLAB_PANIC, NULL);
235 /* SRAM allocate function */
236 static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
237 struct sram_piece *pused_head)
239 struct sram_piece *pslot, *plast, *pavail;
241 if (size <= 0 || !pfree_head || !pused_head)
245 size = (size + 3) & ~3;
247 pslot = pfree_head->next;
250 /* search an available piece slot */
251 while (pslot != NULL && size > pslot->size) {
259 if (pslot->size == size) {
260 plast->next = pslot->next;
263 pavail = kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
268 pavail->paddr = pslot->paddr;
270 pslot->paddr += size;
274 pavail->pid = current->pid;
276 pslot = pused_head->next;
279 /* insert new piece into used piece list !!! */
280 while (pslot != NULL && pavail->paddr < pslot->paddr) {
285 pavail->next = pslot;
286 plast->next = pavail;
288 return pavail->paddr;
291 /* Allocate the largest available block. */
292 static void *_sram_alloc_max(struct sram_piece *pfree_head,
293 struct sram_piece *pused_head,
294 unsigned long *psize)
296 struct sram_piece *pslot, *pmax;
298 if (!pfree_head || !pused_head)
301 pmax = pslot = pfree_head->next;
303 /* search an available piece slot */
304 while (pslot != NULL) {
305 if (pslot->size > pmax->size)
315 return _sram_alloc(*psize, pfree_head, pused_head);
318 /* SRAM free function */
319 static int _sram_free(const void *addr,
320 struct sram_piece *pfree_head,
321 struct sram_piece *pused_head)
323 struct sram_piece *pslot, *plast, *pavail;
325 if (!pfree_head || !pused_head)
328 /* search the relevant memory slot */
329 pslot = pused_head->next;
332 /* search an available piece slot */
333 while (pslot != NULL && pslot->paddr != addr) {
341 plast->next = pslot->next;
345 /* insert free pieces back to the free list */
346 pslot = pfree_head->next;
349 while (pslot != NULL && addr > pslot->paddr) {
354 if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
355 plast->size += pavail->size;
356 kmem_cache_free(sram_piece_cache, pavail);
358 pavail->next = plast->next;
359 plast->next = pavail;
363 if (pslot && plast->paddr + plast->size == pslot->paddr) {
364 plast->size += pslot->size;
365 plast->next = pslot->next;
366 kmem_cache_free(sram_piece_cache, pslot);
372 int sram_free(const void *addr)
375 #if L1_CODE_LENGTH != 0
376 if (addr >= (void *)get_l1_code_start()
377 && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
378 return l1_inst_sram_free(addr);
381 #if L1_DATA_A_LENGTH != 0
382 if (addr >= (void *)get_l1_data_a_start()
383 && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH))
384 return l1_data_A_sram_free(addr);
387 #if L1_DATA_B_LENGTH != 0
388 if (addr >= (void *)get_l1_data_b_start()
389 && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH))
390 return l1_data_B_sram_free(addr);
394 if (addr >= (void *)L2_START
395 && addr < (void *)(L2_START + L2_LENGTH))
396 return l2_sram_free(addr);
401 EXPORT_SYMBOL(sram_free);
403 void *l1_data_A_sram_alloc(size_t size)
410 /* add mutex operation */
411 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
413 #if L1_DATA_A_LENGTH != 0
414 addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
415 &per_cpu(used_l1_data_A_sram_head, cpu));
418 /* add mutex operation */
419 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
422 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
423 (long unsigned int)addr, size);
427 EXPORT_SYMBOL(l1_data_A_sram_alloc);
429 int l1_data_A_sram_free(const void *addr)
436 /* add mutex operation */
437 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
439 #if L1_DATA_A_LENGTH != 0
440 ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu),
441 &per_cpu(used_l1_data_A_sram_head, cpu));
446 /* add mutex operation */
447 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
452 EXPORT_SYMBOL(l1_data_A_sram_free);
454 void *l1_data_B_sram_alloc(size_t size)
456 #if L1_DATA_B_LENGTH != 0
462 /* add mutex operation */
463 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
465 addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
466 &per_cpu(used_l1_data_B_sram_head, cpu));
468 /* add mutex operation */
469 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
472 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
473 (long unsigned int)addr, size);
480 EXPORT_SYMBOL(l1_data_B_sram_alloc);
482 int l1_data_B_sram_free(const void *addr)
484 #if L1_DATA_B_LENGTH != 0
490 /* add mutex operation */
491 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
493 ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu),
494 &per_cpu(used_l1_data_B_sram_head, cpu));
496 /* add mutex operation */
497 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
505 EXPORT_SYMBOL(l1_data_B_sram_free);
507 void *l1_data_sram_alloc(size_t size)
509 void *addr = l1_data_A_sram_alloc(size);
512 addr = l1_data_B_sram_alloc(size);
516 EXPORT_SYMBOL(l1_data_sram_alloc);
518 void *l1_data_sram_zalloc(size_t size)
520 void *addr = l1_data_sram_alloc(size);
523 memset(addr, 0x00, size);
527 EXPORT_SYMBOL(l1_data_sram_zalloc);
529 int l1_data_sram_free(const void *addr)
532 ret = l1_data_A_sram_free(addr);
534 ret = l1_data_B_sram_free(addr);
537 EXPORT_SYMBOL(l1_data_sram_free);
539 void *l1_inst_sram_alloc(size_t size)
541 #if L1_CODE_LENGTH != 0
547 /* add mutex operation */
548 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
550 addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
551 &per_cpu(used_l1_inst_sram_head, cpu));
553 /* add mutex operation */
554 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
557 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
558 (long unsigned int)addr, size);
565 EXPORT_SYMBOL(l1_inst_sram_alloc);
567 int l1_inst_sram_free(const void *addr)
569 #if L1_CODE_LENGTH != 0
575 /* add mutex operation */
576 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
578 ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
579 &per_cpu(used_l1_inst_sram_head, cpu));
581 /* add mutex operation */
582 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
590 EXPORT_SYMBOL(l1_inst_sram_free);
592 /* L1 Scratchpad memory allocate function */
593 void *l1sram_alloc(size_t size)
600 /* add mutex operation */
601 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
603 addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu),
604 &per_cpu(used_l1_ssram_head, cpu));
606 /* add mutex operation */
607 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
613 /* L1 Scratchpad memory allocate function */
614 void *l1sram_alloc_max(size_t *psize)
621 /* add mutex operation */
622 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
624 addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
625 &per_cpu(used_l1_ssram_head, cpu), psize);
627 /* add mutex operation */
628 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
634 /* L1 Scratchpad memory free function */
635 int l1sram_free(const void *addr)
642 /* add mutex operation */
643 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
645 ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
646 &per_cpu(used_l1_ssram_head, cpu));
648 /* add mutex operation */
649 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
655 void *l2_sram_alloc(size_t size)
661 /* add mutex operation */
662 spin_lock_irqsave(&l2_sram_lock, flags);
664 addr = _sram_alloc(size, &free_l2_sram_head,
667 /* add mutex operation */
668 spin_unlock_irqrestore(&l2_sram_lock, flags);
670 pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
671 (long unsigned int)addr, size);
678 EXPORT_SYMBOL(l2_sram_alloc);
680 void *l2_sram_zalloc(size_t size)
682 void *addr = l2_sram_alloc(size);
685 memset(addr, 0x00, size);
689 EXPORT_SYMBOL(l2_sram_zalloc);
691 int l2_sram_free(const void *addr)
697 /* add mutex operation */
698 spin_lock_irqsave(&l2_sram_lock, flags);
700 ret = _sram_free(addr, &free_l2_sram_head,
703 /* add mutex operation */
704 spin_unlock_irqrestore(&l2_sram_lock, flags);
711 EXPORT_SYMBOL(l2_sram_free);
713 int sram_free_with_lsl(const void *addr)
715 struct sram_list_struct *lsl, **tmp;
716 struct mm_struct *mm = current->mm;
718 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
719 if ((*tmp)->addr == addr)
730 EXPORT_SYMBOL(sram_free_with_lsl);
732 void *sram_alloc_with_lsl(size_t size, unsigned long flags)
735 struct sram_list_struct *lsl = NULL;
736 struct mm_struct *mm = current->mm;
738 lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
742 if (flags & L1_INST_SRAM)
743 addr = l1_inst_sram_alloc(size);
745 if (addr == NULL && (flags & L1_DATA_A_SRAM))
746 addr = l1_data_A_sram_alloc(size);
748 if (addr == NULL && (flags & L1_DATA_B_SRAM))
749 addr = l1_data_B_sram_alloc(size);
751 if (addr == NULL && (flags & L2_SRAM))
752 addr = l2_sram_alloc(size);
760 lsl->next = mm->context.sram_list;
761 mm->context.sram_list = lsl;
764 EXPORT_SYMBOL(sram_alloc_with_lsl);
766 #ifdef CONFIG_PROC_FS
767 /* Once we get a real allocator, we'll throw all of this away.
768 * Until then, we need some sort of visibility into the L1 alloc.
770 /* Need to keep line of output the same. Currently, that is 44 bytes
771 * (including newline).
773 static int _sram_proc_read(char *buf, int *len, int count, const char *desc,
774 struct sram_piece *pfree_head,
775 struct sram_piece *pused_head)
777 struct sram_piece *pslot;
779 if (!pfree_head || !pused_head)
782 *len += sprintf(&buf[*len], "--- SRAM %-14s Size PID State \n", desc);
784 /* search the relevant memory slot */
785 pslot = pused_head->next;
787 while (pslot != NULL) {
788 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
789 pslot->paddr, pslot->paddr + pslot->size,
790 pslot->size, pslot->pid, "ALLOCATED");
795 pslot = pfree_head->next;
797 while (pslot != NULL) {
798 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
799 pslot->paddr, pslot->paddr + pslot->size,
800 pslot->size, pslot->pid, "FREE");
807 static int sram_proc_read(char *buf, char **start, off_t offset, int count,
808 int *eof, void *data)
813 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
814 if (_sram_proc_read(buf, &len, count, "Scratchpad",
815 &per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu)))
817 #if L1_DATA_A_LENGTH != 0
818 if (_sram_proc_read(buf, &len, count, "L1 Data A",
819 &per_cpu(free_l1_data_A_sram_head, cpu),
820 &per_cpu(used_l1_data_A_sram_head, cpu)))
823 #if L1_DATA_B_LENGTH != 0
824 if (_sram_proc_read(buf, &len, count, "L1 Data B",
825 &per_cpu(free_l1_data_B_sram_head, cpu),
826 &per_cpu(used_l1_data_B_sram_head, cpu)))
829 #if L1_CODE_LENGTH != 0
830 if (_sram_proc_read(buf, &len, count, "L1 Instruction",
831 &per_cpu(free_l1_inst_sram_head, cpu),
832 &per_cpu(used_l1_inst_sram_head, cpu)))
837 if (_sram_proc_read(buf, &len, count, "L2", &free_l2_sram_head,
846 static int __init sram_proc_init(void)
848 struct proc_dir_entry *ptr;
849 ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
851 printk(KERN_WARNING "unable to create /proc/sram\n");
854 ptr->owner = THIS_MODULE;
855 ptr->read_proc = sram_proc_read;
858 late_initcall(sram_proc_init);