AFS: Don't pass error value to page_cache_release() in error handling
[linux-2.6/kvm.git] / arch / blackfin / mm / sram-alloc.c
blob49b2ff2c8b74bee191df47682bcbae227b5beac6
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/spinlock.h>
19 #include <linux/rtc.h>
20 #include <linux/slab.h>
21 #include <asm/blackfin.h>
22 #include <asm/mem_map.h>
23 #include "blackfin_sram.h"
25 /* the data structure for L1 scratchpad and DATA SRAM */
26 struct sram_piece {
27 void *paddr;
28 int size;
29 pid_t pid;
30 struct sram_piece *next;
33 static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1sram_lock);
34 static DEFINE_PER_CPU(struct sram_piece, free_l1_ssram_head);
35 static DEFINE_PER_CPU(struct sram_piece, used_l1_ssram_head);
37 #if L1_DATA_A_LENGTH != 0
38 static DEFINE_PER_CPU(struct sram_piece, free_l1_data_A_sram_head);
39 static DEFINE_PER_CPU(struct sram_piece, used_l1_data_A_sram_head);
40 #endif
42 #if L1_DATA_B_LENGTH != 0
43 static DEFINE_PER_CPU(struct sram_piece, free_l1_data_B_sram_head);
44 static DEFINE_PER_CPU(struct sram_piece, used_l1_data_B_sram_head);
45 #endif
47 #if L1_DATA_A_LENGTH || L1_DATA_B_LENGTH
48 static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_data_sram_lock);
49 #endif
51 #if L1_CODE_LENGTH != 0
52 static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_inst_sram_lock);
53 static DEFINE_PER_CPU(struct sram_piece, free_l1_inst_sram_head);
54 static DEFINE_PER_CPU(struct sram_piece, used_l1_inst_sram_head);
55 #endif
57 #if L2_LENGTH != 0
58 static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp;
59 static struct sram_piece free_l2_sram_head, used_l2_sram_head;
60 #endif
62 static struct kmem_cache *sram_piece_cache;
64 /* L1 Scratchpad SRAM initialization function */
65 static void __init l1sram_init(void)
67 unsigned int cpu;
68 unsigned long reserve;
70 #ifdef CONFIG_SMP
71 reserve = 0;
72 #else
73 reserve = sizeof(struct l1_scratch_task_info);
74 #endif
76 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
77 per_cpu(free_l1_ssram_head, cpu).next =
78 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
79 if (!per_cpu(free_l1_ssram_head, cpu).next) {
80 printk(KERN_INFO "Fail to initialize Scratchpad data SRAM.\n");
81 return;
84 per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu) + reserve;
85 per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH - reserve;
86 per_cpu(free_l1_ssram_head, cpu).next->pid = 0;
87 per_cpu(free_l1_ssram_head, cpu).next->next = NULL;
89 per_cpu(used_l1_ssram_head, cpu).next = NULL;
91 /* mutex initialize */
92 spin_lock_init(&per_cpu(l1sram_lock, cpu));
93 printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
94 L1_SCRATCH_LENGTH >> 10);
98 static void __init l1_data_sram_init(void)
100 #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
101 unsigned int cpu;
102 #endif
103 #if L1_DATA_A_LENGTH != 0
104 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
105 per_cpu(free_l1_data_A_sram_head, cpu).next =
106 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
107 if (!per_cpu(free_l1_data_A_sram_head, cpu).next) {
108 printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n");
109 return;
112 per_cpu(free_l1_data_A_sram_head, cpu).next->paddr =
113 (void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1);
114 per_cpu(free_l1_data_A_sram_head, cpu).next->size =
115 L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
116 per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0;
117 per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL;
119 per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;
121 printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n",
122 L1_DATA_A_LENGTH >> 10,
123 per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10);
125 #endif
126 #if L1_DATA_B_LENGTH != 0
127 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
128 per_cpu(free_l1_data_B_sram_head, cpu).next =
129 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
130 if (!per_cpu(free_l1_data_B_sram_head, cpu).next) {
131 printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n");
132 return;
135 per_cpu(free_l1_data_B_sram_head, cpu).next->paddr =
136 (void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1);
137 per_cpu(free_l1_data_B_sram_head, cpu).next->size =
138 L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
139 per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0;
140 per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL;
142 per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;
144 printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n",
145 L1_DATA_B_LENGTH >> 10,
146 per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10);
147 /* mutex initialize */
149 #endif
151 #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
152 for (cpu = 0; cpu < num_possible_cpus(); ++cpu)
153 spin_lock_init(&per_cpu(l1_data_sram_lock, cpu));
154 #endif
157 static void __init l1_inst_sram_init(void)
159 #if L1_CODE_LENGTH != 0
160 unsigned int cpu;
161 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
162 per_cpu(free_l1_inst_sram_head, cpu).next =
163 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
164 if (!per_cpu(free_l1_inst_sram_head, cpu).next) {
165 printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n");
166 return;
169 per_cpu(free_l1_inst_sram_head, cpu).next->paddr =
170 (void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1);
171 per_cpu(free_l1_inst_sram_head, cpu).next->size =
172 L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
173 per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0;
174 per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL;
176 per_cpu(used_l1_inst_sram_head, cpu).next = NULL;
178 printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n",
179 L1_CODE_LENGTH >> 10,
180 per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10);
182 /* mutex initialize */
183 spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
185 #endif
188 static void __init l2_sram_init(void)
190 #if L2_LENGTH != 0
191 free_l2_sram_head.next =
192 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
193 if (!free_l2_sram_head.next) {
194 printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
195 return;
198 free_l2_sram_head.next->paddr =
199 (void *)L2_START + (_ebss_l2 - _stext_l2);
200 free_l2_sram_head.next->size =
201 L2_LENGTH - (_ebss_l2 - _stext_l2);
202 free_l2_sram_head.next->pid = 0;
203 free_l2_sram_head.next->next = NULL;
205 used_l2_sram_head.next = NULL;
207 printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n",
208 L2_LENGTH >> 10,
209 free_l2_sram_head.next->size >> 10);
211 /* mutex initialize */
212 spin_lock_init(&l2_sram_lock);
213 #endif
216 static int __init bfin_sram_init(void)
218 sram_piece_cache = kmem_cache_create("sram_piece_cache",
219 sizeof(struct sram_piece),
220 0, SLAB_PANIC, NULL);
222 l1sram_init();
223 l1_data_sram_init();
224 l1_inst_sram_init();
225 l2_sram_init();
227 return 0;
229 pure_initcall(bfin_sram_init);
231 /* SRAM allocate function */
232 static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
233 struct sram_piece *pused_head)
235 struct sram_piece *pslot, *plast, *pavail;
237 if (size <= 0 || !pfree_head || !pused_head)
238 return NULL;
240 /* Align the size */
241 size = (size + 3) & ~3;
243 pslot = pfree_head->next;
244 plast = pfree_head;
246 /* search an available piece slot */
247 while (pslot != NULL && size > pslot->size) {
248 plast = pslot;
249 pslot = pslot->next;
252 if (!pslot)
253 return NULL;
255 if (pslot->size == size) {
256 plast->next = pslot->next;
257 pavail = pslot;
258 } else {
259 pavail = kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
261 if (!pavail)
262 return NULL;
264 pavail->paddr = pslot->paddr;
265 pavail->size = size;
266 pslot->paddr += size;
267 pslot->size -= size;
270 pavail->pid = current->pid;
272 pslot = pused_head->next;
273 plast = pused_head;
275 /* insert new piece into used piece list !!! */
276 while (pslot != NULL && pavail->paddr < pslot->paddr) {
277 plast = pslot;
278 pslot = pslot->next;
281 pavail->next = pslot;
282 plast->next = pavail;
284 return pavail->paddr;
287 /* Allocate the largest available block. */
288 static void *_sram_alloc_max(struct sram_piece *pfree_head,
289 struct sram_piece *pused_head,
290 unsigned long *psize)
292 struct sram_piece *pslot, *pmax;
294 if (!pfree_head || !pused_head)
295 return NULL;
297 pmax = pslot = pfree_head->next;
299 /* search an available piece slot */
300 while (pslot != NULL) {
301 if (pslot->size > pmax->size)
302 pmax = pslot;
303 pslot = pslot->next;
306 if (!pmax)
307 return NULL;
309 *psize = pmax->size;
311 return _sram_alloc(*psize, pfree_head, pused_head);
314 /* SRAM free function */
315 static int _sram_free(const void *addr,
316 struct sram_piece *pfree_head,
317 struct sram_piece *pused_head)
319 struct sram_piece *pslot, *plast, *pavail;
321 if (!pfree_head || !pused_head)
322 return -1;
324 /* search the relevant memory slot */
325 pslot = pused_head->next;
326 plast = pused_head;
328 /* search an available piece slot */
329 while (pslot != NULL && pslot->paddr != addr) {
330 plast = pslot;
331 pslot = pslot->next;
334 if (!pslot)
335 return -1;
337 plast->next = pslot->next;
338 pavail = pslot;
339 pavail->pid = 0;
341 /* insert free pieces back to the free list */
342 pslot = pfree_head->next;
343 plast = pfree_head;
345 while (pslot != NULL && addr > pslot->paddr) {
346 plast = pslot;
347 pslot = pslot->next;
350 if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
351 plast->size += pavail->size;
352 kmem_cache_free(sram_piece_cache, pavail);
353 } else {
354 pavail->next = plast->next;
355 plast->next = pavail;
356 plast = pavail;
359 if (pslot && plast->paddr + plast->size == pslot->paddr) {
360 plast->size += pslot->size;
361 plast->next = pslot->next;
362 kmem_cache_free(sram_piece_cache, pslot);
365 return 0;
368 int sram_free(const void *addr)
371 #if L1_CODE_LENGTH != 0
372 if (addr >= (void *)get_l1_code_start()
373 && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
374 return l1_inst_sram_free(addr);
375 else
376 #endif
377 #if L1_DATA_A_LENGTH != 0
378 if (addr >= (void *)get_l1_data_a_start()
379 && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH))
380 return l1_data_A_sram_free(addr);
381 else
382 #endif
383 #if L1_DATA_B_LENGTH != 0
384 if (addr >= (void *)get_l1_data_b_start()
385 && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH))
386 return l1_data_B_sram_free(addr);
387 else
388 #endif
389 #if L2_LENGTH != 0
390 if (addr >= (void *)L2_START
391 && addr < (void *)(L2_START + L2_LENGTH))
392 return l2_sram_free(addr);
393 else
394 #endif
395 return -1;
397 EXPORT_SYMBOL(sram_free);
399 void *l1_data_A_sram_alloc(size_t size)
401 #if L1_DATA_A_LENGTH != 0
402 unsigned long flags;
403 void *addr;
404 unsigned int cpu;
406 cpu = smp_processor_id();
407 /* add mutex operation */
408 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
410 addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
411 &per_cpu(used_l1_data_A_sram_head, cpu));
413 /* add mutex operation */
414 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
416 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
417 (long unsigned int)addr, size);
419 return addr;
420 #else
421 return NULL;
422 #endif
424 EXPORT_SYMBOL(l1_data_A_sram_alloc);
426 int l1_data_A_sram_free(const void *addr)
428 #if L1_DATA_A_LENGTH != 0
429 unsigned long flags;
430 int ret;
431 unsigned int cpu;
433 cpu = smp_processor_id();
434 /* add mutex operation */
435 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
437 ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu),
438 &per_cpu(used_l1_data_A_sram_head, cpu));
440 /* add mutex operation */
441 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
443 return ret;
444 #else
445 return -1;
446 #endif
448 EXPORT_SYMBOL(l1_data_A_sram_free);
450 void *l1_data_B_sram_alloc(size_t size)
452 #if L1_DATA_B_LENGTH != 0
453 unsigned long flags;
454 void *addr;
455 unsigned int cpu;
457 cpu = smp_processor_id();
458 /* add mutex operation */
459 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
461 addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
462 &per_cpu(used_l1_data_B_sram_head, cpu));
464 /* add mutex operation */
465 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
467 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
468 (long unsigned int)addr, size);
470 return addr;
471 #else
472 return NULL;
473 #endif
475 EXPORT_SYMBOL(l1_data_B_sram_alloc);
477 int l1_data_B_sram_free(const void *addr)
479 #if L1_DATA_B_LENGTH != 0
480 unsigned long flags;
481 int ret;
482 unsigned int cpu;
484 cpu = smp_processor_id();
485 /* add mutex operation */
486 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
488 ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu),
489 &per_cpu(used_l1_data_B_sram_head, cpu));
491 /* add mutex operation */
492 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
494 return ret;
495 #else
496 return -1;
497 #endif
499 EXPORT_SYMBOL(l1_data_B_sram_free);
501 void *l1_data_sram_alloc(size_t size)
503 void *addr = l1_data_A_sram_alloc(size);
505 if (!addr)
506 addr = l1_data_B_sram_alloc(size);
508 return addr;
510 EXPORT_SYMBOL(l1_data_sram_alloc);
512 void *l1_data_sram_zalloc(size_t size)
514 void *addr = l1_data_sram_alloc(size);
516 if (addr)
517 memset(addr, 0x00, size);
519 return addr;
521 EXPORT_SYMBOL(l1_data_sram_zalloc);
523 int l1_data_sram_free(const void *addr)
525 int ret;
526 ret = l1_data_A_sram_free(addr);
527 if (ret == -1)
528 ret = l1_data_B_sram_free(addr);
529 return ret;
531 EXPORT_SYMBOL(l1_data_sram_free);
533 void *l1_inst_sram_alloc(size_t size)
535 #if L1_CODE_LENGTH != 0
536 unsigned long flags;
537 void *addr;
538 unsigned int cpu;
540 cpu = smp_processor_id();
541 /* add mutex operation */
542 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
544 addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
545 &per_cpu(used_l1_inst_sram_head, cpu));
547 /* add mutex operation */
548 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
550 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
551 (long unsigned int)addr, size);
553 return addr;
554 #else
555 return NULL;
556 #endif
558 EXPORT_SYMBOL(l1_inst_sram_alloc);
560 int l1_inst_sram_free(const void *addr)
562 #if L1_CODE_LENGTH != 0
563 unsigned long flags;
564 int ret;
565 unsigned int cpu;
567 cpu = smp_processor_id();
568 /* add mutex operation */
569 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
571 ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
572 &per_cpu(used_l1_inst_sram_head, cpu));
574 /* add mutex operation */
575 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
577 return ret;
578 #else
579 return -1;
580 #endif
582 EXPORT_SYMBOL(l1_inst_sram_free);
584 /* L1 Scratchpad memory allocate function */
585 void *l1sram_alloc(size_t size)
587 unsigned long flags;
588 void *addr;
589 unsigned int cpu;
591 cpu = smp_processor_id();
592 /* add mutex operation */
593 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
595 addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu),
596 &per_cpu(used_l1_ssram_head, cpu));
598 /* add mutex operation */
599 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
601 return addr;
604 /* L1 Scratchpad memory allocate function */
605 void *l1sram_alloc_max(size_t *psize)
607 unsigned long flags;
608 void *addr;
609 unsigned int cpu;
611 cpu = smp_processor_id();
612 /* add mutex operation */
613 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
615 addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
616 &per_cpu(used_l1_ssram_head, cpu), psize);
618 /* add mutex operation */
619 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
621 return addr;
624 /* L1 Scratchpad memory free function */
625 int l1sram_free(const void *addr)
627 unsigned long flags;
628 int ret;
629 unsigned int cpu;
631 cpu = smp_processor_id();
632 /* add mutex operation */
633 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
635 ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
636 &per_cpu(used_l1_ssram_head, cpu));
638 /* add mutex operation */
639 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
641 return ret;
644 void *l2_sram_alloc(size_t size)
646 #if L2_LENGTH != 0
647 unsigned long flags;
648 void *addr;
650 /* add mutex operation */
651 spin_lock_irqsave(&l2_sram_lock, flags);
653 addr = _sram_alloc(size, &free_l2_sram_head,
654 &used_l2_sram_head);
656 /* add mutex operation */
657 spin_unlock_irqrestore(&l2_sram_lock, flags);
659 pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
660 (long unsigned int)addr, size);
662 return addr;
663 #else
664 return NULL;
665 #endif
667 EXPORT_SYMBOL(l2_sram_alloc);
669 void *l2_sram_zalloc(size_t size)
671 void *addr = l2_sram_alloc(size);
673 if (addr)
674 memset(addr, 0x00, size);
676 return addr;
678 EXPORT_SYMBOL(l2_sram_zalloc);
680 int l2_sram_free(const void *addr)
682 #if L2_LENGTH != 0
683 unsigned long flags;
684 int ret;
686 /* add mutex operation */
687 spin_lock_irqsave(&l2_sram_lock, flags);
689 ret = _sram_free(addr, &free_l2_sram_head,
690 &used_l2_sram_head);
692 /* add mutex operation */
693 spin_unlock_irqrestore(&l2_sram_lock, flags);
695 return ret;
696 #else
697 return -1;
698 #endif
700 EXPORT_SYMBOL(l2_sram_free);
702 int sram_free_with_lsl(const void *addr)
704 struct sram_list_struct *lsl, **tmp;
705 struct mm_struct *mm = current->mm;
707 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
708 if ((*tmp)->addr == addr)
709 goto found;
710 return -1;
711 found:
712 lsl = *tmp;
713 sram_free(addr);
714 *tmp = lsl->next;
715 kfree(lsl);
717 return 0;
719 EXPORT_SYMBOL(sram_free_with_lsl);
721 /* Allocate memory and keep in L1 SRAM List (lsl) so that the resources are
722 * tracked. These are designed for userspace so that when a process exits,
723 * we can safely reap their resources.
725 void *sram_alloc_with_lsl(size_t size, unsigned long flags)
727 void *addr = NULL;
728 struct sram_list_struct *lsl = NULL;
729 struct mm_struct *mm = current->mm;
731 lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
732 if (!lsl)
733 return NULL;
735 if (flags & L1_INST_SRAM)
736 addr = l1_inst_sram_alloc(size);
738 if (addr == NULL && (flags & L1_DATA_A_SRAM))
739 addr = l1_data_A_sram_alloc(size);
741 if (addr == NULL && (flags & L1_DATA_B_SRAM))
742 addr = l1_data_B_sram_alloc(size);
744 if (addr == NULL && (flags & L2_SRAM))
745 addr = l2_sram_alloc(size);
747 if (addr == NULL) {
748 kfree(lsl);
749 return NULL;
751 lsl->addr = addr;
752 lsl->length = size;
753 lsl->next = mm->context.sram_list;
754 mm->context.sram_list = lsl;
755 return addr;
757 EXPORT_SYMBOL(sram_alloc_with_lsl);
759 #ifdef CONFIG_PROC_FS
760 /* Once we get a real allocator, we'll throw all of this away.
761 * Until then, we need some sort of visibility into the L1 alloc.
763 /* Need to keep line of output the same. Currently, that is 44 bytes
764 * (including newline).
766 static int _sram_proc_read(char *buf, int *len, int count, const char *desc,
767 struct sram_piece *pfree_head,
768 struct sram_piece *pused_head)
770 struct sram_piece *pslot;
772 if (!pfree_head || !pused_head)
773 return -1;
775 *len += sprintf(&buf[*len], "--- SRAM %-14s Size PID State \n", desc);
777 /* search the relevant memory slot */
778 pslot = pused_head->next;
780 while (pslot != NULL) {
781 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
782 pslot->paddr, pslot->paddr + pslot->size,
783 pslot->size, pslot->pid, "ALLOCATED");
785 pslot = pslot->next;
788 pslot = pfree_head->next;
790 while (pslot != NULL) {
791 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
792 pslot->paddr, pslot->paddr + pslot->size,
793 pslot->size, pslot->pid, "FREE");
795 pslot = pslot->next;
798 return 0;
800 static int sram_proc_read(char *buf, char **start, off_t offset, int count,
801 int *eof, void *data)
803 int len = 0;
804 unsigned int cpu;
806 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
807 if (_sram_proc_read(buf, &len, count, "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_read(buf, &len, count, "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_read(buf, &len, count, "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_read(buf, &len, count, "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_read(buf, &len, count, "L2", &free_l2_sram_head,
831 &used_l2_sram_head))
832 goto not_done;
833 #endif
834 *eof = 1;
835 not_done:
836 return len;
839 static int __init sram_proc_init(void)
841 struct proc_dir_entry *ptr;
842 ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
843 if (!ptr) {
844 printk(KERN_WARNING "unable to create /proc/sram\n");
845 return -1;
847 ptr->read_proc = sram_proc_read;
848 return 0;
850 late_initcall(sram_proc_init);
851 #endif