spi-topcliff-pch: Fix issue for transmitting over 4KByte
[zen-stable.git] / arch / blackfin / mm / sram-alloc.c
blob29d98faa1efdacd4a9cf93723fb268ab91dcbc85
1 /*
2 * SRAM allocator for Blackfin on-chip memory
4 * Copyright 2004-2009 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <linux/miscdevice.h>
13 #include <linux/ioport.h>
14 #include <linux/fcntl.h>
15 #include <linux/init.h>
16 #include <linux/poll.h>
17 #include <linux/proc_fs.h>
18 #include <linux/seq_file.h>
19 #include <linux/spinlock.h>
20 #include <linux/rtc.h>
21 #include <linux/slab.h>
22 #include <asm/blackfin.h>
23 #include <asm/mem_map.h>
24 #include "blackfin_sram.h"
26 /* the data structure for L1 scratchpad and DATA SRAM */
27 struct sram_piece {
28 void *paddr;
29 int size;
30 pid_t pid;
31 struct sram_piece *next;
34 static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1sram_lock);
35 static DEFINE_PER_CPU(struct sram_piece, free_l1_ssram_head);
36 static DEFINE_PER_CPU(struct sram_piece, used_l1_ssram_head);
38 #if L1_DATA_A_LENGTH != 0
39 static DEFINE_PER_CPU(struct sram_piece, free_l1_data_A_sram_head);
40 static DEFINE_PER_CPU(struct sram_piece, used_l1_data_A_sram_head);
41 #endif
43 #if L1_DATA_B_LENGTH != 0
44 static DEFINE_PER_CPU(struct sram_piece, free_l1_data_B_sram_head);
45 static DEFINE_PER_CPU(struct sram_piece, used_l1_data_B_sram_head);
46 #endif
48 #if L1_DATA_A_LENGTH || L1_DATA_B_LENGTH
49 static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_data_sram_lock);
50 #endif
52 #if L1_CODE_LENGTH != 0
53 static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_inst_sram_lock);
54 static DEFINE_PER_CPU(struct sram_piece, free_l1_inst_sram_head);
55 static DEFINE_PER_CPU(struct sram_piece, used_l1_inst_sram_head);
56 #endif
58 #if L2_LENGTH != 0
59 static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp;
60 static struct sram_piece free_l2_sram_head, used_l2_sram_head;
61 #endif
63 static struct kmem_cache *sram_piece_cache;
65 /* L1 Scratchpad SRAM initialization function */
66 static void __init l1sram_init(void)
68 unsigned int cpu;
69 unsigned long reserve;
71 #ifdef CONFIG_SMP
72 reserve = 0;
73 #else
74 reserve = sizeof(struct l1_scratch_task_info);
75 #endif
77 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
78 per_cpu(free_l1_ssram_head, cpu).next =
79 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
80 if (!per_cpu(free_l1_ssram_head, cpu).next) {
81 printk(KERN_INFO "Fail to initialize Scratchpad data SRAM.\n");
82 return;
85 per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu) + reserve;
86 per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH - reserve;
87 per_cpu(free_l1_ssram_head, cpu).next->pid = 0;
88 per_cpu(free_l1_ssram_head, cpu).next->next = NULL;
90 per_cpu(used_l1_ssram_head, cpu).next = NULL;
92 /* mutex initialize */
93 spin_lock_init(&per_cpu(l1sram_lock, cpu));
94 printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
95 L1_SCRATCH_LENGTH >> 10);
99 static void __init l1_data_sram_init(void)
101 #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
102 unsigned int cpu;
103 #endif
104 #if L1_DATA_A_LENGTH != 0
105 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
106 per_cpu(free_l1_data_A_sram_head, cpu).next =
107 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
108 if (!per_cpu(free_l1_data_A_sram_head, cpu).next) {
109 printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n");
110 return;
113 per_cpu(free_l1_data_A_sram_head, cpu).next->paddr =
114 (void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1);
115 per_cpu(free_l1_data_A_sram_head, cpu).next->size =
116 L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
117 per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0;
118 per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL;
120 per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;
122 printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n",
123 L1_DATA_A_LENGTH >> 10,
124 per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10);
126 #endif
127 #if L1_DATA_B_LENGTH != 0
128 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
129 per_cpu(free_l1_data_B_sram_head, cpu).next =
130 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
131 if (!per_cpu(free_l1_data_B_sram_head, cpu).next) {
132 printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n");
133 return;
136 per_cpu(free_l1_data_B_sram_head, cpu).next->paddr =
137 (void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1);
138 per_cpu(free_l1_data_B_sram_head, cpu).next->size =
139 L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
140 per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0;
141 per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL;
143 per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;
145 printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n",
146 L1_DATA_B_LENGTH >> 10,
147 per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10);
148 /* mutex initialize */
150 #endif
152 #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
153 for (cpu = 0; cpu < num_possible_cpus(); ++cpu)
154 spin_lock_init(&per_cpu(l1_data_sram_lock, cpu));
155 #endif
158 static void __init l1_inst_sram_init(void)
160 #if L1_CODE_LENGTH != 0
161 unsigned int cpu;
162 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
163 per_cpu(free_l1_inst_sram_head, cpu).next =
164 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
165 if (!per_cpu(free_l1_inst_sram_head, cpu).next) {
166 printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n");
167 return;
170 per_cpu(free_l1_inst_sram_head, cpu).next->paddr =
171 (void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1);
172 per_cpu(free_l1_inst_sram_head, cpu).next->size =
173 L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
174 per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0;
175 per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL;
177 per_cpu(used_l1_inst_sram_head, cpu).next = NULL;
179 printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n",
180 L1_CODE_LENGTH >> 10,
181 per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10);
183 /* mutex initialize */
184 spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
186 #endif
189 static void __init l2_sram_init(void)
191 #if L2_LENGTH != 0
192 free_l2_sram_head.next =
193 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
194 if (!free_l2_sram_head.next) {
195 printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
196 return;
199 free_l2_sram_head.next->paddr =
200 (void *)L2_START + (_ebss_l2 - _stext_l2);
201 free_l2_sram_head.next->size =
202 L2_LENGTH - (_ebss_l2 - _stext_l2);
203 free_l2_sram_head.next->pid = 0;
204 free_l2_sram_head.next->next = NULL;
206 used_l2_sram_head.next = NULL;
208 printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n",
209 L2_LENGTH >> 10,
210 free_l2_sram_head.next->size >> 10);
212 /* mutex initialize */
213 spin_lock_init(&l2_sram_lock);
214 #endif
217 static int __init bfin_sram_init(void)
219 sram_piece_cache = kmem_cache_create("sram_piece_cache",
220 sizeof(struct sram_piece),
221 0, SLAB_PANIC, NULL);
223 l1sram_init();
224 l1_data_sram_init();
225 l1_inst_sram_init();
226 l2_sram_init();
228 return 0;
230 pure_initcall(bfin_sram_init);
232 /* SRAM allocate function */
233 static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
234 struct sram_piece *pused_head)
236 struct sram_piece *pslot, *plast, *pavail;
238 if (size <= 0 || !pfree_head || !pused_head)
239 return NULL;
241 /* Align the size */
242 size = (size + 3) & ~3;
244 pslot = pfree_head->next;
245 plast = pfree_head;
247 /* search an available piece slot */
248 while (pslot != NULL && size > pslot->size) {
249 plast = pslot;
250 pslot = pslot->next;
253 if (!pslot)
254 return NULL;
256 if (pslot->size == size) {
257 plast->next = pslot->next;
258 pavail = pslot;
259 } else {
260 /* use atomic so our L1 allocator can be used atomically */
261 pavail = kmem_cache_alloc(sram_piece_cache, GFP_ATOMIC);
263 if (!pavail)
264 return NULL;
266 pavail->paddr = pslot->paddr;
267 pavail->size = size;
268 pslot->paddr += size;
269 pslot->size -= size;
272 pavail->pid = current->pid;
274 pslot = pused_head->next;
275 plast = pused_head;
277 /* insert new piece into used piece list !!! */
278 while (pslot != NULL && pavail->paddr < pslot->paddr) {
279 plast = pslot;
280 pslot = pslot->next;
283 pavail->next = pslot;
284 plast->next = pavail;
286 return pavail->paddr;
289 /* Allocate the largest available block. */
290 static void *_sram_alloc_max(struct sram_piece *pfree_head,
291 struct sram_piece *pused_head,
292 unsigned long *psize)
294 struct sram_piece *pslot, *pmax;
296 if (!pfree_head || !pused_head)
297 return NULL;
299 pmax = pslot = pfree_head->next;
301 /* search an available piece slot */
302 while (pslot != NULL) {
303 if (pslot->size > pmax->size)
304 pmax = pslot;
305 pslot = pslot->next;
308 if (!pmax)
309 return NULL;
311 *psize = pmax->size;
313 return _sram_alloc(*psize, pfree_head, pused_head);
316 /* SRAM free function */
317 static int _sram_free(const void *addr,
318 struct sram_piece *pfree_head,
319 struct sram_piece *pused_head)
321 struct sram_piece *pslot, *plast, *pavail;
323 if (!pfree_head || !pused_head)
324 return -1;
326 /* search the relevant memory slot */
327 pslot = pused_head->next;
328 plast = pused_head;
330 /* search an available piece slot */
331 while (pslot != NULL && pslot->paddr != addr) {
332 plast = pslot;
333 pslot = pslot->next;
336 if (!pslot)
337 return -1;
339 plast->next = pslot->next;
340 pavail = pslot;
341 pavail->pid = 0;
343 /* insert free pieces back to the free list */
344 pslot = pfree_head->next;
345 plast = pfree_head;
347 while (pslot != NULL && addr > pslot->paddr) {
348 plast = pslot;
349 pslot = pslot->next;
352 if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
353 plast->size += pavail->size;
354 kmem_cache_free(sram_piece_cache, pavail);
355 } else {
356 pavail->next = plast->next;
357 plast->next = pavail;
358 plast = pavail;
361 if (pslot && plast->paddr + plast->size == pslot->paddr) {
362 plast->size += pslot->size;
363 plast->next = pslot->next;
364 kmem_cache_free(sram_piece_cache, pslot);
367 return 0;
370 int sram_free(const void *addr)
373 #if L1_CODE_LENGTH != 0
374 if (addr >= (void *)get_l1_code_start()
375 && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
376 return l1_inst_sram_free(addr);
377 else
378 #endif
379 #if L1_DATA_A_LENGTH != 0
380 if (addr >= (void *)get_l1_data_a_start()
381 && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH))
382 return l1_data_A_sram_free(addr);
383 else
384 #endif
385 #if L1_DATA_B_LENGTH != 0
386 if (addr >= (void *)get_l1_data_b_start()
387 && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH))
388 return l1_data_B_sram_free(addr);
389 else
390 #endif
391 #if L2_LENGTH != 0
392 if (addr >= (void *)L2_START
393 && addr < (void *)(L2_START + L2_LENGTH))
394 return l2_sram_free(addr);
395 else
396 #endif
397 return -1;
399 EXPORT_SYMBOL(sram_free);
401 void *l1_data_A_sram_alloc(size_t size)
403 #if L1_DATA_A_LENGTH != 0
404 unsigned long flags;
405 void *addr;
406 unsigned int cpu;
408 cpu = smp_processor_id();
409 /* add mutex operation */
410 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
412 addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
413 &per_cpu(used_l1_data_A_sram_head, cpu));
415 /* add mutex operation */
416 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
418 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
419 (long unsigned int)addr, size);
421 return addr;
422 #else
423 return NULL;
424 #endif
426 EXPORT_SYMBOL(l1_data_A_sram_alloc);
428 int l1_data_A_sram_free(const void *addr)
430 #if L1_DATA_A_LENGTH != 0
431 unsigned long flags;
432 int ret;
433 unsigned int cpu;
435 cpu = smp_processor_id();
436 /* add mutex operation */
437 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
439 ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu),
440 &per_cpu(used_l1_data_A_sram_head, cpu));
442 /* add mutex operation */
443 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
445 return ret;
446 #else
447 return -1;
448 #endif
450 EXPORT_SYMBOL(l1_data_A_sram_free);
452 void *l1_data_B_sram_alloc(size_t size)
454 #if L1_DATA_B_LENGTH != 0
455 unsigned long flags;
456 void *addr;
457 unsigned int cpu;
459 cpu = smp_processor_id();
460 /* add mutex operation */
461 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
463 addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
464 &per_cpu(used_l1_data_B_sram_head, cpu));
466 /* add mutex operation */
467 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
469 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
470 (long unsigned int)addr, size);
472 return addr;
473 #else
474 return NULL;
475 #endif
477 EXPORT_SYMBOL(l1_data_B_sram_alloc);
479 int l1_data_B_sram_free(const void *addr)
481 #if L1_DATA_B_LENGTH != 0
482 unsigned long flags;
483 int ret;
484 unsigned int cpu;
486 cpu = smp_processor_id();
487 /* add mutex operation */
488 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
490 ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu),
491 &per_cpu(used_l1_data_B_sram_head, cpu));
493 /* add mutex operation */
494 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
496 return ret;
497 #else
498 return -1;
499 #endif
501 EXPORT_SYMBOL(l1_data_B_sram_free);
503 void *l1_data_sram_alloc(size_t size)
505 void *addr = l1_data_A_sram_alloc(size);
507 if (!addr)
508 addr = l1_data_B_sram_alloc(size);
510 return addr;
512 EXPORT_SYMBOL(l1_data_sram_alloc);
514 void *l1_data_sram_zalloc(size_t size)
516 void *addr = l1_data_sram_alloc(size);
518 if (addr)
519 memset(addr, 0x00, size);
521 return addr;
523 EXPORT_SYMBOL(l1_data_sram_zalloc);
525 int l1_data_sram_free(const void *addr)
527 int ret;
528 ret = l1_data_A_sram_free(addr);
529 if (ret == -1)
530 ret = l1_data_B_sram_free(addr);
531 return ret;
533 EXPORT_SYMBOL(l1_data_sram_free);
535 void *l1_inst_sram_alloc(size_t size)
537 #if L1_CODE_LENGTH != 0
538 unsigned long flags;
539 void *addr;
540 unsigned int cpu;
542 cpu = smp_processor_id();
543 /* add mutex operation */
544 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
546 addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
547 &per_cpu(used_l1_inst_sram_head, cpu));
549 /* add mutex operation */
550 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
552 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
553 (long unsigned int)addr, size);
555 return addr;
556 #else
557 return NULL;
558 #endif
560 EXPORT_SYMBOL(l1_inst_sram_alloc);
562 int l1_inst_sram_free(const void *addr)
564 #if L1_CODE_LENGTH != 0
565 unsigned long flags;
566 int ret;
567 unsigned int cpu;
569 cpu = smp_processor_id();
570 /* add mutex operation */
571 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
573 ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
574 &per_cpu(used_l1_inst_sram_head, cpu));
576 /* add mutex operation */
577 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
579 return ret;
580 #else
581 return -1;
582 #endif
584 EXPORT_SYMBOL(l1_inst_sram_free);
586 /* L1 Scratchpad memory allocate function */
587 void *l1sram_alloc(size_t size)
589 unsigned long flags;
590 void *addr;
591 unsigned int cpu;
593 cpu = smp_processor_id();
594 /* add mutex operation */
595 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
597 addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu),
598 &per_cpu(used_l1_ssram_head, cpu));
600 /* add mutex operation */
601 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
603 return addr;
606 /* L1 Scratchpad memory allocate function */
607 void *l1sram_alloc_max(size_t *psize)
609 unsigned long flags;
610 void *addr;
611 unsigned int cpu;
613 cpu = smp_processor_id();
614 /* add mutex operation */
615 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
617 addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
618 &per_cpu(used_l1_ssram_head, cpu), psize);
620 /* add mutex operation */
621 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
623 return addr;
626 /* L1 Scratchpad memory free function */
627 int l1sram_free(const void *addr)
629 unsigned long flags;
630 int ret;
631 unsigned int cpu;
633 cpu = smp_processor_id();
634 /* add mutex operation */
635 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
637 ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
638 &per_cpu(used_l1_ssram_head, cpu));
640 /* add mutex operation */
641 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
643 return ret;
646 void *l2_sram_alloc(size_t size)
648 #if L2_LENGTH != 0
649 unsigned long flags;
650 void *addr;
652 /* add mutex operation */
653 spin_lock_irqsave(&l2_sram_lock, flags);
655 addr = _sram_alloc(size, &free_l2_sram_head,
656 &used_l2_sram_head);
658 /* add mutex operation */
659 spin_unlock_irqrestore(&l2_sram_lock, flags);
661 pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
662 (long unsigned int)addr, size);
664 return addr;
665 #else
666 return NULL;
667 #endif
669 EXPORT_SYMBOL(l2_sram_alloc);
671 void *l2_sram_zalloc(size_t size)
673 void *addr = l2_sram_alloc(size);
675 if (addr)
676 memset(addr, 0x00, size);
678 return addr;
680 EXPORT_SYMBOL(l2_sram_zalloc);
682 int l2_sram_free(const void *addr)
684 #if L2_LENGTH != 0
685 unsigned long flags;
686 int ret;
688 /* add mutex operation */
689 spin_lock_irqsave(&l2_sram_lock, flags);
691 ret = _sram_free(addr, &free_l2_sram_head,
692 &used_l2_sram_head);
694 /* add mutex operation */
695 spin_unlock_irqrestore(&l2_sram_lock, flags);
697 return ret;
698 #else
699 return -1;
700 #endif
702 EXPORT_SYMBOL(l2_sram_free);
704 int sram_free_with_lsl(const void *addr)
706 struct sram_list_struct *lsl, **tmp;
707 struct mm_struct *mm = current->mm;
708 int ret = -1;
710 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
711 if ((*tmp)->addr == addr) {
712 lsl = *tmp;
713 ret = sram_free(addr);
714 *tmp = lsl->next;
715 kfree(lsl);
716 break;
719 return ret;
721 EXPORT_SYMBOL(sram_free_with_lsl);
723 /* Allocate memory and keep in L1 SRAM List (lsl) so that the resources are
724 * tracked. These are designed for userspace so that when a process exits,
725 * we can safely reap their resources.
727 void *sram_alloc_with_lsl(size_t size, unsigned long flags)
729 void *addr = NULL;
730 struct sram_list_struct *lsl = NULL;
731 struct mm_struct *mm = current->mm;
733 lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
734 if (!lsl)
735 return NULL;
737 if (flags & L1_INST_SRAM)
738 addr = l1_inst_sram_alloc(size);
740 if (addr == NULL && (flags & L1_DATA_A_SRAM))
741 addr = l1_data_A_sram_alloc(size);
743 if (addr == NULL && (flags & L1_DATA_B_SRAM))
744 addr = l1_data_B_sram_alloc(size);
746 if (addr == NULL && (flags & L2_SRAM))
747 addr = l2_sram_alloc(size);
749 if (addr == NULL) {
750 kfree(lsl);
751 return NULL;
753 lsl->addr = addr;
754 lsl->length = size;
755 lsl->next = mm->context.sram_list;
756 mm->context.sram_list = lsl;
757 return addr;
759 EXPORT_SYMBOL(sram_alloc_with_lsl);
761 #ifdef CONFIG_PROC_FS
762 /* Once we get a real allocator, we'll throw all of this away.
763 * Until then, we need some sort of visibility into the L1 alloc.
765 /* Need to keep line of output the same. Currently, that is 44 bytes
766 * (including newline).
768 static int _sram_proc_show(struct seq_file *m, const char *desc,
769 struct sram_piece *pfree_head,
770 struct sram_piece *pused_head)
772 struct sram_piece *pslot;
774 if (!pfree_head || !pused_head)
775 return -1;
777 seq_printf(m, "--- SRAM %-14s Size PID State \n", desc);
779 /* search the relevant memory slot */
780 pslot = pused_head->next;
782 while (pslot != NULL) {
783 seq_printf(m, "%p-%p %10i %5i %-10s\n",
784 pslot->paddr, pslot->paddr + pslot->size,
785 pslot->size, pslot->pid, "ALLOCATED");
787 pslot = pslot->next;
790 pslot = pfree_head->next;
792 while (pslot != NULL) {
793 seq_printf(m, "%p-%p %10i %5i %-10s\n",
794 pslot->paddr, pslot->paddr + pslot->size,
795 pslot->size, pslot->pid, "FREE");
797 pslot = pslot->next;
800 return 0;
802 static int sram_proc_show(struct seq_file *m, void *v)
804 unsigned int cpu;
806 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
807 if (_sram_proc_show(m, "Scratchpad",
808 &per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu)))
809 goto not_done;
810 #if L1_DATA_A_LENGTH != 0
811 if (_sram_proc_show(m, "L1 Data A",
812 &per_cpu(free_l1_data_A_sram_head, cpu),
813 &per_cpu(used_l1_data_A_sram_head, cpu)))
814 goto not_done;
815 #endif
816 #if L1_DATA_B_LENGTH != 0
817 if (_sram_proc_show(m, "L1 Data B",
818 &per_cpu(free_l1_data_B_sram_head, cpu),
819 &per_cpu(used_l1_data_B_sram_head, cpu)))
820 goto not_done;
821 #endif
822 #if L1_CODE_LENGTH != 0
823 if (_sram_proc_show(m, "L1 Instruction",
824 &per_cpu(free_l1_inst_sram_head, cpu),
825 &per_cpu(used_l1_inst_sram_head, cpu)))
826 goto not_done;
827 #endif
829 #if L2_LENGTH != 0
830 if (_sram_proc_show(m, "L2", &free_l2_sram_head, &used_l2_sram_head))
831 goto not_done;
832 #endif
833 not_done:
834 return 0;
837 static int sram_proc_open(struct inode *inode, struct file *file)
839 return single_open(file, sram_proc_show, NULL);
842 static const struct file_operations sram_proc_ops = {
843 .open = sram_proc_open,
844 .read = seq_read,
845 .llseek = seq_lseek,
846 .release = single_release,
849 static int __init sram_proc_init(void)
851 struct proc_dir_entry *ptr;
853 ptr = proc_create("sram", S_IRUGO, NULL, &sram_proc_ops);
854 if (!ptr) {
855 printk(KERN_WARNING "unable to create /proc/sram\n");
856 return -1;
858 return 0;
860 late_initcall(sram_proc_init);
861 #endif