Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / arch / blackfin / mm / sram-alloc.c
blobd2a96c2c02a3e64475f3c0c3426e6af1a0c9af17
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 <linux/mm_types.h>
24 #include <asm/blackfin.h>
25 #include <asm/mem_map.h>
26 #include "blackfin_sram.h"
28 /* the data structure for L1 scratchpad and DATA SRAM */
29 struct sram_piece {
30 void *paddr;
31 int size;
32 pid_t pid;
33 struct sram_piece *next;
36 static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1sram_lock);
37 static DEFINE_PER_CPU(struct sram_piece, free_l1_ssram_head);
38 static DEFINE_PER_CPU(struct sram_piece, used_l1_ssram_head);
40 #if L1_DATA_A_LENGTH != 0
41 static DEFINE_PER_CPU(struct sram_piece, free_l1_data_A_sram_head);
42 static DEFINE_PER_CPU(struct sram_piece, used_l1_data_A_sram_head);
43 #endif
45 #if L1_DATA_B_LENGTH != 0
46 static DEFINE_PER_CPU(struct sram_piece, free_l1_data_B_sram_head);
47 static DEFINE_PER_CPU(struct sram_piece, used_l1_data_B_sram_head);
48 #endif
50 #if L1_DATA_A_LENGTH || L1_DATA_B_LENGTH
51 static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_data_sram_lock);
52 #endif
54 #if L1_CODE_LENGTH != 0
55 static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_inst_sram_lock);
56 static DEFINE_PER_CPU(struct sram_piece, free_l1_inst_sram_head);
57 static DEFINE_PER_CPU(struct sram_piece, used_l1_inst_sram_head);
58 #endif
60 #if L2_LENGTH != 0
61 static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp;
62 static struct sram_piece free_l2_sram_head, used_l2_sram_head;
63 #endif
65 static struct kmem_cache *sram_piece_cache;
67 /* L1 Scratchpad SRAM initialization function */
68 static void __init l1sram_init(void)
70 unsigned int cpu;
71 unsigned long reserve;
73 #ifdef CONFIG_SMP
74 reserve = 0;
75 #else
76 reserve = sizeof(struct l1_scratch_task_info);
77 #endif
79 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
80 per_cpu(free_l1_ssram_head, cpu).next =
81 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
82 if (!per_cpu(free_l1_ssram_head, cpu).next) {
83 printk(KERN_INFO "Fail to initialize Scratchpad data SRAM.\n");
84 return;
87 per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu) + reserve;
88 per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH - reserve;
89 per_cpu(free_l1_ssram_head, cpu).next->pid = 0;
90 per_cpu(free_l1_ssram_head, cpu).next->next = NULL;
92 per_cpu(used_l1_ssram_head, cpu).next = NULL;
94 /* mutex initialize */
95 spin_lock_init(&per_cpu(l1sram_lock, cpu));
96 printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
97 L1_SCRATCH_LENGTH >> 10);
101 static void __init l1_data_sram_init(void)
103 #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
104 unsigned int cpu;
105 #endif
106 #if L1_DATA_A_LENGTH != 0
107 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
108 per_cpu(free_l1_data_A_sram_head, cpu).next =
109 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
110 if (!per_cpu(free_l1_data_A_sram_head, cpu).next) {
111 printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n");
112 return;
115 per_cpu(free_l1_data_A_sram_head, cpu).next->paddr =
116 (void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1);
117 per_cpu(free_l1_data_A_sram_head, cpu).next->size =
118 L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
119 per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0;
120 per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL;
122 per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;
124 printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n",
125 L1_DATA_A_LENGTH >> 10,
126 per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10);
128 #endif
129 #if L1_DATA_B_LENGTH != 0
130 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
131 per_cpu(free_l1_data_B_sram_head, cpu).next =
132 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
133 if (!per_cpu(free_l1_data_B_sram_head, cpu).next) {
134 printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n");
135 return;
138 per_cpu(free_l1_data_B_sram_head, cpu).next->paddr =
139 (void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1);
140 per_cpu(free_l1_data_B_sram_head, cpu).next->size =
141 L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
142 per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0;
143 per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL;
145 per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;
147 printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n",
148 L1_DATA_B_LENGTH >> 10,
149 per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10);
150 /* mutex initialize */
152 #endif
154 #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
155 for (cpu = 0; cpu < num_possible_cpus(); ++cpu)
156 spin_lock_init(&per_cpu(l1_data_sram_lock, cpu));
157 #endif
160 static void __init l1_inst_sram_init(void)
162 #if L1_CODE_LENGTH != 0
163 unsigned int cpu;
164 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
165 per_cpu(free_l1_inst_sram_head, cpu).next =
166 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
167 if (!per_cpu(free_l1_inst_sram_head, cpu).next) {
168 printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n");
169 return;
172 per_cpu(free_l1_inst_sram_head, cpu).next->paddr =
173 (void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1);
174 per_cpu(free_l1_inst_sram_head, cpu).next->size =
175 L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
176 per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0;
177 per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL;
179 per_cpu(used_l1_inst_sram_head, cpu).next = NULL;
181 printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n",
182 L1_CODE_LENGTH >> 10,
183 per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10);
185 /* mutex initialize */
186 spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
188 #endif
191 #ifdef __ADSPBF60x__
192 static irqreturn_t l2_ecc_err(int irq, void *dev_id)
194 int status;
196 printk(KERN_ERR "L2 ecc error happened\n");
197 status = bfin_read32(L2CTL0_STAT);
198 if (status & 0x1)
199 printk(KERN_ERR "Core channel error type:0x%x, addr:0x%x\n",
200 bfin_read32(L2CTL0_ET0), bfin_read32(L2CTL0_EADDR0));
201 if (status & 0x2)
202 printk(KERN_ERR "System channel error type:0x%x, addr:0x%x\n",
203 bfin_read32(L2CTL0_ET1), bfin_read32(L2CTL0_EADDR1));
205 status = status >> 8;
206 if (status)
207 printk(KERN_ERR "L2 Bank%d error, addr:0x%x\n",
208 status, bfin_read32(L2CTL0_ERRADDR0 + status));
210 panic("L2 Ecc error");
211 return IRQ_HANDLED;
213 #endif
215 static void __init l2_sram_init(void)
217 #if L2_LENGTH != 0
219 #ifdef __ADSPBF60x__
220 int ret;
222 ret = request_irq(IRQ_L2CTL0_ECC_ERR, l2_ecc_err, 0, "l2-ecc-err",
223 NULL);
224 if (unlikely(ret < 0)) {
225 printk(KERN_INFO "Fail to request l2 ecc error interrupt");
226 return;
228 #endif
230 free_l2_sram_head.next =
231 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
232 if (!free_l2_sram_head.next) {
233 printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
234 return;
237 free_l2_sram_head.next->paddr =
238 (void *)L2_START + (_ebss_l2 - _stext_l2);
239 free_l2_sram_head.next->size =
240 L2_LENGTH - (_ebss_l2 - _stext_l2);
241 free_l2_sram_head.next->pid = 0;
242 free_l2_sram_head.next->next = NULL;
244 used_l2_sram_head.next = NULL;
246 printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n",
247 L2_LENGTH >> 10,
248 free_l2_sram_head.next->size >> 10);
250 /* mutex initialize */
251 spin_lock_init(&l2_sram_lock);
252 #endif
255 static int __init bfin_sram_init(void)
257 sram_piece_cache = kmem_cache_create("sram_piece_cache",
258 sizeof(struct sram_piece),
259 0, SLAB_PANIC, NULL);
261 l1sram_init();
262 l1_data_sram_init();
263 l1_inst_sram_init();
264 l2_sram_init();
266 return 0;
268 pure_initcall(bfin_sram_init);
270 /* SRAM allocate function */
271 static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
272 struct sram_piece *pused_head)
274 struct sram_piece *pslot, *plast, *pavail;
276 if (size <= 0 || !pfree_head || !pused_head)
277 return NULL;
279 /* Align the size */
280 size = (size + 3) & ~3;
282 pslot = pfree_head->next;
283 plast = pfree_head;
285 /* search an available piece slot */
286 while (pslot != NULL && size > pslot->size) {
287 plast = pslot;
288 pslot = pslot->next;
291 if (!pslot)
292 return NULL;
294 if (pslot->size == size) {
295 plast->next = pslot->next;
296 pavail = pslot;
297 } else {
298 /* use atomic so our L1 allocator can be used atomically */
299 pavail = kmem_cache_alloc(sram_piece_cache, GFP_ATOMIC);
301 if (!pavail)
302 return NULL;
304 pavail->paddr = pslot->paddr;
305 pavail->size = size;
306 pslot->paddr += size;
307 pslot->size -= size;
310 pavail->pid = current->pid;
312 pslot = pused_head->next;
313 plast = pused_head;
315 /* insert new piece into used piece list !!! */
316 while (pslot != NULL && pavail->paddr < pslot->paddr) {
317 plast = pslot;
318 pslot = pslot->next;
321 pavail->next = pslot;
322 plast->next = pavail;
324 return pavail->paddr;
327 /* Allocate the largest available block. */
328 static void *_sram_alloc_max(struct sram_piece *pfree_head,
329 struct sram_piece *pused_head,
330 unsigned long *psize)
332 struct sram_piece *pslot, *pmax;
334 if (!pfree_head || !pused_head)
335 return NULL;
337 pmax = pslot = pfree_head->next;
339 /* search an available piece slot */
340 while (pslot != NULL) {
341 if (pslot->size > pmax->size)
342 pmax = pslot;
343 pslot = pslot->next;
346 if (!pmax)
347 return NULL;
349 *psize = pmax->size;
351 return _sram_alloc(*psize, pfree_head, pused_head);
354 /* SRAM free function */
355 static int _sram_free(const void *addr,
356 struct sram_piece *pfree_head,
357 struct sram_piece *pused_head)
359 struct sram_piece *pslot, *plast, *pavail;
361 if (!pfree_head || !pused_head)
362 return -1;
364 /* search the relevant memory slot */
365 pslot = pused_head->next;
366 plast = pused_head;
368 /* search an available piece slot */
369 while (pslot != NULL && pslot->paddr != addr) {
370 plast = pslot;
371 pslot = pslot->next;
374 if (!pslot)
375 return -1;
377 plast->next = pslot->next;
378 pavail = pslot;
379 pavail->pid = 0;
381 /* insert free pieces back to the free list */
382 pslot = pfree_head->next;
383 plast = pfree_head;
385 while (pslot != NULL && addr > pslot->paddr) {
386 plast = pslot;
387 pslot = pslot->next;
390 if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
391 plast->size += pavail->size;
392 kmem_cache_free(sram_piece_cache, pavail);
393 } else {
394 pavail->next = plast->next;
395 plast->next = pavail;
396 plast = pavail;
399 if (pslot && plast->paddr + plast->size == pslot->paddr) {
400 plast->size += pslot->size;
401 plast->next = pslot->next;
402 kmem_cache_free(sram_piece_cache, pslot);
405 return 0;
408 int sram_free(const void *addr)
411 #if L1_CODE_LENGTH != 0
412 if (addr >= (void *)get_l1_code_start()
413 && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
414 return l1_inst_sram_free(addr);
415 else
416 #endif
417 #if L1_DATA_A_LENGTH != 0
418 if (addr >= (void *)get_l1_data_a_start()
419 && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH))
420 return l1_data_A_sram_free(addr);
421 else
422 #endif
423 #if L1_DATA_B_LENGTH != 0
424 if (addr >= (void *)get_l1_data_b_start()
425 && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH))
426 return l1_data_B_sram_free(addr);
427 else
428 #endif
429 #if L2_LENGTH != 0
430 if (addr >= (void *)L2_START
431 && addr < (void *)(L2_START + L2_LENGTH))
432 return l2_sram_free(addr);
433 else
434 #endif
435 return -1;
437 EXPORT_SYMBOL(sram_free);
439 void *l1_data_A_sram_alloc(size_t size)
441 #if L1_DATA_A_LENGTH != 0
442 unsigned long flags;
443 void *addr;
444 unsigned int cpu;
446 cpu = smp_processor_id();
447 /* add mutex operation */
448 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
450 addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
451 &per_cpu(used_l1_data_A_sram_head, cpu));
453 /* add mutex operation */
454 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
456 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
457 (long unsigned int)addr, size);
459 return addr;
460 #else
461 return NULL;
462 #endif
464 EXPORT_SYMBOL(l1_data_A_sram_alloc);
466 int l1_data_A_sram_free(const void *addr)
468 #if L1_DATA_A_LENGTH != 0
469 unsigned long flags;
470 int ret;
471 unsigned int cpu;
473 cpu = smp_processor_id();
474 /* add mutex operation */
475 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
477 ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu),
478 &per_cpu(used_l1_data_A_sram_head, cpu));
480 /* add mutex operation */
481 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
483 return ret;
484 #else
485 return -1;
486 #endif
488 EXPORT_SYMBOL(l1_data_A_sram_free);
490 void *l1_data_B_sram_alloc(size_t size)
492 #if L1_DATA_B_LENGTH != 0
493 unsigned long flags;
494 void *addr;
495 unsigned int cpu;
497 cpu = smp_processor_id();
498 /* add mutex operation */
499 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
501 addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
502 &per_cpu(used_l1_data_B_sram_head, cpu));
504 /* add mutex operation */
505 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
507 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
508 (long unsigned int)addr, size);
510 return addr;
511 #else
512 return NULL;
513 #endif
515 EXPORT_SYMBOL(l1_data_B_sram_alloc);
517 int l1_data_B_sram_free(const void *addr)
519 #if L1_DATA_B_LENGTH != 0
520 unsigned long flags;
521 int ret;
522 unsigned int cpu;
524 cpu = smp_processor_id();
525 /* add mutex operation */
526 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
528 ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu),
529 &per_cpu(used_l1_data_B_sram_head, cpu));
531 /* add mutex operation */
532 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
534 return ret;
535 #else
536 return -1;
537 #endif
539 EXPORT_SYMBOL(l1_data_B_sram_free);
541 void *l1_data_sram_alloc(size_t size)
543 void *addr = l1_data_A_sram_alloc(size);
545 if (!addr)
546 addr = l1_data_B_sram_alloc(size);
548 return addr;
550 EXPORT_SYMBOL(l1_data_sram_alloc);
552 void *l1_data_sram_zalloc(size_t size)
554 void *addr = l1_data_sram_alloc(size);
556 if (addr)
557 memset(addr, 0x00, size);
559 return addr;
561 EXPORT_SYMBOL(l1_data_sram_zalloc);
563 int l1_data_sram_free(const void *addr)
565 int ret;
566 ret = l1_data_A_sram_free(addr);
567 if (ret == -1)
568 ret = l1_data_B_sram_free(addr);
569 return ret;
571 EXPORT_SYMBOL(l1_data_sram_free);
573 void *l1_inst_sram_alloc(size_t size)
575 #if L1_CODE_LENGTH != 0
576 unsigned long flags;
577 void *addr;
578 unsigned int cpu;
580 cpu = smp_processor_id();
581 /* add mutex operation */
582 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
584 addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
585 &per_cpu(used_l1_inst_sram_head, cpu));
587 /* add mutex operation */
588 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
590 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
591 (long unsigned int)addr, size);
593 return addr;
594 #else
595 return NULL;
596 #endif
598 EXPORT_SYMBOL(l1_inst_sram_alloc);
600 int l1_inst_sram_free(const void *addr)
602 #if L1_CODE_LENGTH != 0
603 unsigned long flags;
604 int ret;
605 unsigned int cpu;
607 cpu = smp_processor_id();
608 /* add mutex operation */
609 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
611 ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
612 &per_cpu(used_l1_inst_sram_head, cpu));
614 /* add mutex operation */
615 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
617 return ret;
618 #else
619 return -1;
620 #endif
622 EXPORT_SYMBOL(l1_inst_sram_free);
624 /* L1 Scratchpad memory allocate function */
625 void *l1sram_alloc(size_t size)
627 unsigned long flags;
628 void *addr;
629 unsigned int cpu;
631 cpu = smp_processor_id();
632 /* add mutex operation */
633 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
635 addr = _sram_alloc(size, &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 addr;
644 /* L1 Scratchpad memory allocate function */
645 void *l1sram_alloc_max(size_t *psize)
647 unsigned long flags;
648 void *addr;
649 unsigned int cpu;
651 cpu = smp_processor_id();
652 /* add mutex operation */
653 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
655 addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
656 &per_cpu(used_l1_ssram_head, cpu), psize);
658 /* add mutex operation */
659 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
661 return addr;
664 /* L1 Scratchpad memory free function */
665 int l1sram_free(const void *addr)
667 unsigned long flags;
668 int ret;
669 unsigned int cpu;
671 cpu = smp_processor_id();
672 /* add mutex operation */
673 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
675 ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
676 &per_cpu(used_l1_ssram_head, cpu));
678 /* add mutex operation */
679 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
681 return ret;
684 void *l2_sram_alloc(size_t size)
686 #if L2_LENGTH != 0
687 unsigned long flags;
688 void *addr;
690 /* add mutex operation */
691 spin_lock_irqsave(&l2_sram_lock, flags);
693 addr = _sram_alloc(size, &free_l2_sram_head,
694 &used_l2_sram_head);
696 /* add mutex operation */
697 spin_unlock_irqrestore(&l2_sram_lock, flags);
699 pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
700 (long unsigned int)addr, size);
702 return addr;
703 #else
704 return NULL;
705 #endif
707 EXPORT_SYMBOL(l2_sram_alloc);
709 void *l2_sram_zalloc(size_t size)
711 void *addr = l2_sram_alloc(size);
713 if (addr)
714 memset(addr, 0x00, size);
716 return addr;
718 EXPORT_SYMBOL(l2_sram_zalloc);
720 int l2_sram_free(const void *addr)
722 #if L2_LENGTH != 0
723 unsigned long flags;
724 int ret;
726 /* add mutex operation */
727 spin_lock_irqsave(&l2_sram_lock, flags);
729 ret = _sram_free(addr, &free_l2_sram_head,
730 &used_l2_sram_head);
732 /* add mutex operation */
733 spin_unlock_irqrestore(&l2_sram_lock, flags);
735 return ret;
736 #else
737 return -1;
738 #endif
740 EXPORT_SYMBOL(l2_sram_free);
742 int sram_free_with_lsl(const void *addr)
744 struct sram_list_struct *lsl, **tmp;
745 struct mm_struct *mm = current->mm;
746 int ret = -1;
748 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
749 if ((*tmp)->addr == addr) {
750 lsl = *tmp;
751 ret = sram_free(addr);
752 *tmp = lsl->next;
753 kfree(lsl);
754 break;
757 return ret;
759 EXPORT_SYMBOL(sram_free_with_lsl);
761 /* Allocate memory and keep in L1 SRAM List (lsl) so that the resources are
762 * tracked. These are designed for userspace so that when a process exits,
763 * we can safely reap their resources.
765 void *sram_alloc_with_lsl(size_t size, unsigned long flags)
767 void *addr = NULL;
768 struct sram_list_struct *lsl = NULL;
769 struct mm_struct *mm = current->mm;
771 lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
772 if (!lsl)
773 return NULL;
775 if (flags & L1_INST_SRAM)
776 addr = l1_inst_sram_alloc(size);
778 if (addr == NULL && (flags & L1_DATA_A_SRAM))
779 addr = l1_data_A_sram_alloc(size);
781 if (addr == NULL && (flags & L1_DATA_B_SRAM))
782 addr = l1_data_B_sram_alloc(size);
784 if (addr == NULL && (flags & L2_SRAM))
785 addr = l2_sram_alloc(size);
787 if (addr == NULL) {
788 kfree(lsl);
789 return NULL;
791 lsl->addr = addr;
792 lsl->length = size;
793 lsl->next = mm->context.sram_list;
794 mm->context.sram_list = lsl;
795 return addr;
797 EXPORT_SYMBOL(sram_alloc_with_lsl);
799 #ifdef CONFIG_PROC_FS
800 /* Once we get a real allocator, we'll throw all of this away.
801 * Until then, we need some sort of visibility into the L1 alloc.
803 /* Need to keep line of output the same. Currently, that is 44 bytes
804 * (including newline).
806 static int _sram_proc_show(struct seq_file *m, const char *desc,
807 struct sram_piece *pfree_head,
808 struct sram_piece *pused_head)
810 struct sram_piece *pslot;
812 if (!pfree_head || !pused_head)
813 return -1;
815 seq_printf(m, "--- SRAM %-14s Size PID State \n", desc);
817 /* search the relevant memory slot */
818 pslot = pused_head->next;
820 while (pslot != NULL) {
821 seq_printf(m, "%p-%p %10i %5i %-10s\n",
822 pslot->paddr, pslot->paddr + pslot->size,
823 pslot->size, pslot->pid, "ALLOCATED");
825 pslot = pslot->next;
828 pslot = pfree_head->next;
830 while (pslot != NULL) {
831 seq_printf(m, "%p-%p %10i %5i %-10s\n",
832 pslot->paddr, pslot->paddr + pslot->size,
833 pslot->size, pslot->pid, "FREE");
835 pslot = pslot->next;
838 return 0;
840 static int sram_proc_show(struct seq_file *m, void *v)
842 unsigned int cpu;
844 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
845 if (_sram_proc_show(m, "Scratchpad",
846 &per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu)))
847 goto not_done;
848 #if L1_DATA_A_LENGTH != 0
849 if (_sram_proc_show(m, "L1 Data A",
850 &per_cpu(free_l1_data_A_sram_head, cpu),
851 &per_cpu(used_l1_data_A_sram_head, cpu)))
852 goto not_done;
853 #endif
854 #if L1_DATA_B_LENGTH != 0
855 if (_sram_proc_show(m, "L1 Data B",
856 &per_cpu(free_l1_data_B_sram_head, cpu),
857 &per_cpu(used_l1_data_B_sram_head, cpu)))
858 goto not_done;
859 #endif
860 #if L1_CODE_LENGTH != 0
861 if (_sram_proc_show(m, "L1 Instruction",
862 &per_cpu(free_l1_inst_sram_head, cpu),
863 &per_cpu(used_l1_inst_sram_head, cpu)))
864 goto not_done;
865 #endif
867 #if L2_LENGTH != 0
868 if (_sram_proc_show(m, "L2", &free_l2_sram_head, &used_l2_sram_head))
869 goto not_done;
870 #endif
871 not_done:
872 return 0;
875 static int sram_proc_open(struct inode *inode, struct file *file)
877 return single_open(file, sram_proc_show, NULL);
880 static const struct file_operations sram_proc_ops = {
881 .open = sram_proc_open,
882 .read = seq_read,
883 .llseek = seq_lseek,
884 .release = single_release,
887 static int __init sram_proc_init(void)
889 struct proc_dir_entry *ptr;
891 ptr = proc_create("sram", S_IRUGO, NULL, &sram_proc_ops);
892 if (!ptr) {
893 printk(KERN_WARNING "unable to create /proc/sram\n");
894 return -1;
896 return 0;
898 late_initcall(sram_proc_init);
899 #endif