Blackfin: push SRAM locks down into related ifdefs
[linux-2.6/cjktty.git] / arch / blackfin / mm / sram-alloc.c
blobeb63ab353e5a1efdd5a124eade0fb1b355de4490
1 /*
2 * File: arch/blackfin/mm/sram-alloc.c
3 * Based on:
4 * Author:
6 * Created:
7 * Description: SRAM allocator for Blackfin L1 and L2 memory
9 * Modified:
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 <asm/mem_map.h>
43 #include "blackfin_sram.h"
45 /* the data structure for L1 scratchpad and DATA SRAM */
46 struct sram_piece {
47 void *paddr;
48 int size;
49 pid_t pid;
50 struct sram_piece *next;
53 static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1sram_lock);
54 static DEFINE_PER_CPU(struct sram_piece, free_l1_ssram_head);
55 static DEFINE_PER_CPU(struct sram_piece, used_l1_ssram_head);
57 #if L1_DATA_A_LENGTH != 0
58 static DEFINE_PER_CPU(struct sram_piece, free_l1_data_A_sram_head);
59 static DEFINE_PER_CPU(struct sram_piece, used_l1_data_A_sram_head);
60 #endif
62 #if L1_DATA_B_LENGTH != 0
63 static DEFINE_PER_CPU(struct sram_piece, free_l1_data_B_sram_head);
64 static DEFINE_PER_CPU(struct sram_piece, used_l1_data_B_sram_head);
65 #endif
67 #if L1_DATA_A_LENGTH || L1_DATA_B_LENGTH
68 static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_data_sram_lock);
69 #endif
71 #if L1_CODE_LENGTH != 0
72 static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_inst_sram_lock);
73 static DEFINE_PER_CPU(struct sram_piece, free_l1_inst_sram_head);
74 static DEFINE_PER_CPU(struct sram_piece, used_l1_inst_sram_head);
75 #endif
77 #if L2_LENGTH != 0
78 static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp;
79 static struct sram_piece free_l2_sram_head, used_l2_sram_head;
80 #endif
82 static struct kmem_cache *sram_piece_cache;
84 /* L1 Scratchpad SRAM initialization function */
85 static void __init l1sram_init(void)
87 unsigned int cpu;
88 unsigned long reserve;
90 #ifdef CONFIG_SMP
91 reserve = 0;
92 #else
93 reserve = sizeof(struct l1_scratch_task_info);
94 #endif
96 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
97 per_cpu(free_l1_ssram_head, cpu).next =
98 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
99 if (!per_cpu(free_l1_ssram_head, cpu).next) {
100 printk(KERN_INFO "Fail to initialize Scratchpad data SRAM.\n");
101 return;
104 per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu) + reserve;
105 per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH - reserve;
106 per_cpu(free_l1_ssram_head, cpu).next->pid = 0;
107 per_cpu(free_l1_ssram_head, cpu).next->next = NULL;
109 per_cpu(used_l1_ssram_head, cpu).next = NULL;
111 /* mutex initialize */
112 spin_lock_init(&per_cpu(l1sram_lock, cpu));
113 printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
114 L1_SCRATCH_LENGTH >> 10);
118 static void __init l1_data_sram_init(void)
120 #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
121 unsigned int cpu;
122 #endif
123 #if L1_DATA_A_LENGTH != 0
124 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
125 per_cpu(free_l1_data_A_sram_head, cpu).next =
126 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
127 if (!per_cpu(free_l1_data_A_sram_head, cpu).next) {
128 printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n");
129 return;
132 per_cpu(free_l1_data_A_sram_head, cpu).next->paddr =
133 (void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1);
134 per_cpu(free_l1_data_A_sram_head, cpu).next->size =
135 L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
136 per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0;
137 per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL;
139 per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;
141 printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n",
142 L1_DATA_A_LENGTH >> 10,
143 per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10);
145 #endif
146 #if L1_DATA_B_LENGTH != 0
147 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
148 per_cpu(free_l1_data_B_sram_head, cpu).next =
149 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
150 if (!per_cpu(free_l1_data_B_sram_head, cpu).next) {
151 printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n");
152 return;
155 per_cpu(free_l1_data_B_sram_head, cpu).next->paddr =
156 (void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1);
157 per_cpu(free_l1_data_B_sram_head, cpu).next->size =
158 L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
159 per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0;
160 per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL;
162 per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;
164 printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n",
165 L1_DATA_B_LENGTH >> 10,
166 per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10);
167 /* mutex initialize */
169 #endif
171 #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
172 for (cpu = 0; cpu < num_possible_cpus(); ++cpu)
173 spin_lock_init(&per_cpu(l1_data_sram_lock, cpu));
174 #endif
177 static void __init l1_inst_sram_init(void)
179 #if L1_CODE_LENGTH != 0
180 unsigned int cpu;
181 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
182 per_cpu(free_l1_inst_sram_head, cpu).next =
183 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
184 if (!per_cpu(free_l1_inst_sram_head, cpu).next) {
185 printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n");
186 return;
189 per_cpu(free_l1_inst_sram_head, cpu).next->paddr =
190 (void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1);
191 per_cpu(free_l1_inst_sram_head, cpu).next->size =
192 L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
193 per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0;
194 per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL;
196 per_cpu(used_l1_inst_sram_head, cpu).next = NULL;
198 printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n",
199 L1_CODE_LENGTH >> 10,
200 per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10);
202 /* mutex initialize */
203 spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
205 #endif
208 static void __init l2_sram_init(void)
210 #if L2_LENGTH != 0
211 free_l2_sram_head.next =
212 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
213 if (!free_l2_sram_head.next) {
214 printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
215 return;
218 free_l2_sram_head.next->paddr =
219 (void *)L2_START + (_ebss_l2 - _stext_l2);
220 free_l2_sram_head.next->size =
221 L2_LENGTH - (_ebss_l2 - _stext_l2);
222 free_l2_sram_head.next->pid = 0;
223 free_l2_sram_head.next->next = NULL;
225 used_l2_sram_head.next = NULL;
227 printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n",
228 L2_LENGTH >> 10,
229 free_l2_sram_head.next->size >> 10);
231 /* mutex initialize */
232 spin_lock_init(&l2_sram_lock);
233 #endif
236 static int __init bfin_sram_init(void)
238 sram_piece_cache = kmem_cache_create("sram_piece_cache",
239 sizeof(struct sram_piece),
240 0, SLAB_PANIC, NULL);
242 l1sram_init();
243 l1_data_sram_init();
244 l1_inst_sram_init();
245 l2_sram_init();
247 return 0;
249 pure_initcall(bfin_sram_init);
251 /* SRAM allocate function */
252 static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
253 struct sram_piece *pused_head)
255 struct sram_piece *pslot, *plast, *pavail;
257 if (size <= 0 || !pfree_head || !pused_head)
258 return NULL;
260 /* Align the size */
261 size = (size + 3) & ~3;
263 pslot = pfree_head->next;
264 plast = pfree_head;
266 /* search an available piece slot */
267 while (pslot != NULL && size > pslot->size) {
268 plast = pslot;
269 pslot = pslot->next;
272 if (!pslot)
273 return NULL;
275 if (pslot->size == size) {
276 plast->next = pslot->next;
277 pavail = pslot;
278 } else {
279 pavail = kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
281 if (!pavail)
282 return NULL;
284 pavail->paddr = pslot->paddr;
285 pavail->size = size;
286 pslot->paddr += size;
287 pslot->size -= size;
290 pavail->pid = current->pid;
292 pslot = pused_head->next;
293 plast = pused_head;
295 /* insert new piece into used piece list !!! */
296 while (pslot != NULL && pavail->paddr < pslot->paddr) {
297 plast = pslot;
298 pslot = pslot->next;
301 pavail->next = pslot;
302 plast->next = pavail;
304 return pavail->paddr;
307 /* Allocate the largest available block. */
308 static void *_sram_alloc_max(struct sram_piece *pfree_head,
309 struct sram_piece *pused_head,
310 unsigned long *psize)
312 struct sram_piece *pslot, *pmax;
314 if (!pfree_head || !pused_head)
315 return NULL;
317 pmax = pslot = pfree_head->next;
319 /* search an available piece slot */
320 while (pslot != NULL) {
321 if (pslot->size > pmax->size)
322 pmax = pslot;
323 pslot = pslot->next;
326 if (!pmax)
327 return NULL;
329 *psize = pmax->size;
331 return _sram_alloc(*psize, pfree_head, pused_head);
334 /* SRAM free function */
335 static int _sram_free(const void *addr,
336 struct sram_piece *pfree_head,
337 struct sram_piece *pused_head)
339 struct sram_piece *pslot, *plast, *pavail;
341 if (!pfree_head || !pused_head)
342 return -1;
344 /* search the relevant memory slot */
345 pslot = pused_head->next;
346 plast = pused_head;
348 /* search an available piece slot */
349 while (pslot != NULL && pslot->paddr != addr) {
350 plast = pslot;
351 pslot = pslot->next;
354 if (!pslot)
355 return -1;
357 plast->next = pslot->next;
358 pavail = pslot;
359 pavail->pid = 0;
361 /* insert free pieces back to the free list */
362 pslot = pfree_head->next;
363 plast = pfree_head;
365 while (pslot != NULL && addr > pslot->paddr) {
366 plast = pslot;
367 pslot = pslot->next;
370 if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
371 plast->size += pavail->size;
372 kmem_cache_free(sram_piece_cache, pavail);
373 } else {
374 pavail->next = plast->next;
375 plast->next = pavail;
376 plast = pavail;
379 if (pslot && plast->paddr + plast->size == pslot->paddr) {
380 plast->size += pslot->size;
381 plast->next = pslot->next;
382 kmem_cache_free(sram_piece_cache, pslot);
385 return 0;
388 int sram_free(const void *addr)
391 #if L1_CODE_LENGTH != 0
392 if (addr >= (void *)get_l1_code_start()
393 && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
394 return l1_inst_sram_free(addr);
395 else
396 #endif
397 #if L1_DATA_A_LENGTH != 0
398 if (addr >= (void *)get_l1_data_a_start()
399 && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH))
400 return l1_data_A_sram_free(addr);
401 else
402 #endif
403 #if L1_DATA_B_LENGTH != 0
404 if (addr >= (void *)get_l1_data_b_start()
405 && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH))
406 return l1_data_B_sram_free(addr);
407 else
408 #endif
409 #if L2_LENGTH != 0
410 if (addr >= (void *)L2_START
411 && addr < (void *)(L2_START + L2_LENGTH))
412 return l2_sram_free(addr);
413 else
414 #endif
415 return -1;
417 EXPORT_SYMBOL(sram_free);
419 void *l1_data_A_sram_alloc(size_t size)
421 #if L1_DATA_A_LENGTH != 0
422 unsigned long flags;
423 void *addr;
424 unsigned int cpu;
426 cpu = get_cpu();
427 /* add mutex operation */
428 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
430 addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
431 &per_cpu(used_l1_data_A_sram_head, cpu));
433 /* add mutex operation */
434 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
435 put_cpu();
437 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
438 (long unsigned int)addr, size);
440 return addr;
441 #else
442 return NULL;
443 #endif
445 EXPORT_SYMBOL(l1_data_A_sram_alloc);
447 int l1_data_A_sram_free(const void *addr)
449 #if L1_DATA_A_LENGTH != 0
450 unsigned long flags;
451 int ret;
452 unsigned int cpu;
454 cpu = get_cpu();
455 /* add mutex operation */
456 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
458 ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu),
459 &per_cpu(used_l1_data_A_sram_head, cpu));
461 /* add mutex operation */
462 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
463 put_cpu();
465 return ret;
466 #else
467 return -1;
468 #endif
470 EXPORT_SYMBOL(l1_data_A_sram_free);
472 void *l1_data_B_sram_alloc(size_t size)
474 #if L1_DATA_B_LENGTH != 0
475 unsigned long flags;
476 void *addr;
477 unsigned int cpu;
479 cpu = get_cpu();
480 /* add mutex operation */
481 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
483 addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
484 &per_cpu(used_l1_data_B_sram_head, cpu));
486 /* add mutex operation */
487 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
488 put_cpu();
490 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
491 (long unsigned int)addr, size);
493 return addr;
494 #else
495 return NULL;
496 #endif
498 EXPORT_SYMBOL(l1_data_B_sram_alloc);
500 int l1_data_B_sram_free(const void *addr)
502 #if L1_DATA_B_LENGTH != 0
503 unsigned long flags;
504 int ret;
505 unsigned int cpu;
507 cpu = get_cpu();
508 /* add mutex operation */
509 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
511 ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu),
512 &per_cpu(used_l1_data_B_sram_head, cpu));
514 /* add mutex operation */
515 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
516 put_cpu();
518 return ret;
519 #else
520 return -1;
521 #endif
523 EXPORT_SYMBOL(l1_data_B_sram_free);
525 void *l1_data_sram_alloc(size_t size)
527 void *addr = l1_data_A_sram_alloc(size);
529 if (!addr)
530 addr = l1_data_B_sram_alloc(size);
532 return addr;
534 EXPORT_SYMBOL(l1_data_sram_alloc);
536 void *l1_data_sram_zalloc(size_t size)
538 void *addr = l1_data_sram_alloc(size);
540 if (addr)
541 memset(addr, 0x00, size);
543 return addr;
545 EXPORT_SYMBOL(l1_data_sram_zalloc);
547 int l1_data_sram_free(const void *addr)
549 int ret;
550 ret = l1_data_A_sram_free(addr);
551 if (ret == -1)
552 ret = l1_data_B_sram_free(addr);
553 return ret;
555 EXPORT_SYMBOL(l1_data_sram_free);
557 void *l1_inst_sram_alloc(size_t size)
559 #if L1_CODE_LENGTH != 0
560 unsigned long flags;
561 void *addr;
562 unsigned int cpu;
564 cpu = get_cpu();
565 /* add mutex operation */
566 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
568 addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
569 &per_cpu(used_l1_inst_sram_head, cpu));
571 /* add mutex operation */
572 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
573 put_cpu();
575 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
576 (long unsigned int)addr, size);
578 return addr;
579 #else
580 return NULL;
581 #endif
583 EXPORT_SYMBOL(l1_inst_sram_alloc);
585 int l1_inst_sram_free(const void *addr)
587 #if L1_CODE_LENGTH != 0
588 unsigned long flags;
589 int ret;
590 unsigned int cpu;
592 cpu = get_cpu();
593 /* add mutex operation */
594 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
596 ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
597 &per_cpu(used_l1_inst_sram_head, cpu));
599 /* add mutex operation */
600 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
601 put_cpu();
603 return ret;
604 #else
605 return -1;
606 #endif
608 EXPORT_SYMBOL(l1_inst_sram_free);
610 /* L1 Scratchpad memory allocate function */
611 void *l1sram_alloc(size_t size)
613 unsigned long flags;
614 void *addr;
615 unsigned int cpu;
617 cpu = get_cpu();
618 /* add mutex operation */
619 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
621 addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu),
622 &per_cpu(used_l1_ssram_head, cpu));
624 /* add mutex operation */
625 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
626 put_cpu();
628 return addr;
631 /* L1 Scratchpad memory allocate function */
632 void *l1sram_alloc_max(size_t *psize)
634 unsigned long flags;
635 void *addr;
636 unsigned int cpu;
638 cpu = get_cpu();
639 /* add mutex operation */
640 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
642 addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
643 &per_cpu(used_l1_ssram_head, cpu), psize);
645 /* add mutex operation */
646 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
647 put_cpu();
649 return addr;
652 /* L1 Scratchpad memory free function */
653 int l1sram_free(const void *addr)
655 unsigned long flags;
656 int ret;
657 unsigned int cpu;
659 cpu = get_cpu();
660 /* add mutex operation */
661 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
663 ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
664 &per_cpu(used_l1_ssram_head, cpu));
666 /* add mutex operation */
667 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
668 put_cpu();
670 return ret;
673 void *l2_sram_alloc(size_t size)
675 #if L2_LENGTH != 0
676 unsigned long flags;
677 void *addr;
679 /* add mutex operation */
680 spin_lock_irqsave(&l2_sram_lock, flags);
682 addr = _sram_alloc(size, &free_l2_sram_head,
683 &used_l2_sram_head);
685 /* add mutex operation */
686 spin_unlock_irqrestore(&l2_sram_lock, flags);
688 pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
689 (long unsigned int)addr, size);
691 return addr;
692 #else
693 return NULL;
694 #endif
696 EXPORT_SYMBOL(l2_sram_alloc);
698 void *l2_sram_zalloc(size_t size)
700 void *addr = l2_sram_alloc(size);
702 if (addr)
703 memset(addr, 0x00, size);
705 return addr;
707 EXPORT_SYMBOL(l2_sram_zalloc);
709 int l2_sram_free(const void *addr)
711 #if L2_LENGTH != 0
712 unsigned long flags;
713 int ret;
715 /* add mutex operation */
716 spin_lock_irqsave(&l2_sram_lock, flags);
718 ret = _sram_free(addr, &free_l2_sram_head,
719 &used_l2_sram_head);
721 /* add mutex operation */
722 spin_unlock_irqrestore(&l2_sram_lock, flags);
724 return ret;
725 #else
726 return -1;
727 #endif
729 EXPORT_SYMBOL(l2_sram_free);
731 int sram_free_with_lsl(const void *addr)
733 struct sram_list_struct *lsl, **tmp;
734 struct mm_struct *mm = current->mm;
736 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
737 if ((*tmp)->addr == addr)
738 goto found;
739 return -1;
740 found:
741 lsl = *tmp;
742 sram_free(addr);
743 *tmp = lsl->next;
744 kfree(lsl);
746 return 0;
748 EXPORT_SYMBOL(sram_free_with_lsl);
750 /* Allocate memory and keep in L1 SRAM List (lsl) so that the resources are
751 * tracked. These are designed for userspace so that when a process exits,
752 * we can safely reap their resources.
754 void *sram_alloc_with_lsl(size_t size, unsigned long flags)
756 void *addr = NULL;
757 struct sram_list_struct *lsl = NULL;
758 struct mm_struct *mm = current->mm;
760 lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
761 if (!lsl)
762 return NULL;
764 if (flags & L1_INST_SRAM)
765 addr = l1_inst_sram_alloc(size);
767 if (addr == NULL && (flags & L1_DATA_A_SRAM))
768 addr = l1_data_A_sram_alloc(size);
770 if (addr == NULL && (flags & L1_DATA_B_SRAM))
771 addr = l1_data_B_sram_alloc(size);
773 if (addr == NULL && (flags & L2_SRAM))
774 addr = l2_sram_alloc(size);
776 if (addr == NULL) {
777 kfree(lsl);
778 return NULL;
780 lsl->addr = addr;
781 lsl->length = size;
782 lsl->next = mm->context.sram_list;
783 mm->context.sram_list = lsl;
784 return addr;
786 EXPORT_SYMBOL(sram_alloc_with_lsl);
788 #ifdef CONFIG_PROC_FS
789 /* Once we get a real allocator, we'll throw all of this away.
790 * Until then, we need some sort of visibility into the L1 alloc.
792 /* Need to keep line of output the same. Currently, that is 44 bytes
793 * (including newline).
795 static int _sram_proc_read(char *buf, int *len, int count, const char *desc,
796 struct sram_piece *pfree_head,
797 struct sram_piece *pused_head)
799 struct sram_piece *pslot;
801 if (!pfree_head || !pused_head)
802 return -1;
804 *len += sprintf(&buf[*len], "--- SRAM %-14s Size PID State \n", desc);
806 /* search the relevant memory slot */
807 pslot = pused_head->next;
809 while (pslot != NULL) {
810 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
811 pslot->paddr, pslot->paddr + pslot->size,
812 pslot->size, pslot->pid, "ALLOCATED");
814 pslot = pslot->next;
817 pslot = pfree_head->next;
819 while (pslot != NULL) {
820 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
821 pslot->paddr, pslot->paddr + pslot->size,
822 pslot->size, pslot->pid, "FREE");
824 pslot = pslot->next;
827 return 0;
829 static int sram_proc_read(char *buf, char **start, off_t offset, int count,
830 int *eof, void *data)
832 int len = 0;
833 unsigned int cpu;
835 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
836 if (_sram_proc_read(buf, &len, count, "Scratchpad",
837 &per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu)))
838 goto not_done;
839 #if L1_DATA_A_LENGTH != 0
840 if (_sram_proc_read(buf, &len, count, "L1 Data A",
841 &per_cpu(free_l1_data_A_sram_head, cpu),
842 &per_cpu(used_l1_data_A_sram_head, cpu)))
843 goto not_done;
844 #endif
845 #if L1_DATA_B_LENGTH != 0
846 if (_sram_proc_read(buf, &len, count, "L1 Data B",
847 &per_cpu(free_l1_data_B_sram_head, cpu),
848 &per_cpu(used_l1_data_B_sram_head, cpu)))
849 goto not_done;
850 #endif
851 #if L1_CODE_LENGTH != 0
852 if (_sram_proc_read(buf, &len, count, "L1 Instruction",
853 &per_cpu(free_l1_inst_sram_head, cpu),
854 &per_cpu(used_l1_inst_sram_head, cpu)))
855 goto not_done;
856 #endif
858 #if L2_LENGTH != 0
859 if (_sram_proc_read(buf, &len, count, "L2", &free_l2_sram_head,
860 &used_l2_sram_head))
861 goto not_done;
862 #endif
863 *eof = 1;
864 not_done:
865 return len;
868 static int __init sram_proc_init(void)
870 struct proc_dir_entry *ptr;
871 ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
872 if (!ptr) {
873 printk(KERN_WARNING "unable to create /proc/sram\n");
874 return -1;
876 ptr->read_proc = sram_proc_read;
877 return 0;
879 late_initcall(sram_proc_init);
880 #endif