Merge branch 'fix/hda' into for-linus
[pandora-kernel.git] / arch / blackfin / mm / sram-alloc.c
1 /*
2  * File:         arch/blackfin/mm/sram-alloc.c
3  * Based on:
4  * Author:
5  *
6  * Created:
7  * Description:  SRAM allocator for Blackfin L1 and L2 memory
8  *
9  * Modified:
10  *               Copyright 2004-2008 Analog Devices Inc.
11  *
12  * Bugs:         Enter bugs at http://blackfin.uclinux.org/
13  *
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.
18  *
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.
23  *
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
28  */
29
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"
44
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;
51 };
52
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);
56
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
61
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
66
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
70
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
76
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
81
82 static struct kmem_cache *sram_piece_cache;
83
84 /* L1 Scratchpad SRAM initialization function */
85 static void __init l1sram_init(void)
86 {
87         unsigned int cpu;
88         unsigned long reserve;
89
90 #ifdef CONFIG_SMP
91         reserve = 0;
92 #else
93         reserve = sizeof(struct l1_scratch_task_info);
94 #endif
95
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;
102                 }
103
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;
108
109                 per_cpu(used_l1_ssram_head, cpu).next = NULL;
110
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);
115         }
116 }
117
118 static void __init l1_data_sram_init(void)
119 {
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;
130                 }
131
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;
138
139                 per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;
140
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);
144         }
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;
153                 }
154
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;
161
162                 per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;
163
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 */
168         }
169 #endif
170
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
175 }
176
177 static void __init l1_inst_sram_init(void)
178 {
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;
187                 }
188
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;
195
196                 per_cpu(used_l1_inst_sram_head, cpu).next = NULL;
197
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);
201
202                 /* mutex initialize */
203                 spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
204         }
205 #endif
206 }
207
208 static void __init l2_sram_init(void)
209 {
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;
216         }
217
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;
224
225         used_l2_sram_head.next = NULL;
226
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);
230
231         /* mutex initialize */
232         spin_lock_init(&l2_sram_lock);
233 #endif
234 }
235
236 static int __init bfin_sram_init(void)
237 {
238         sram_piece_cache = kmem_cache_create("sram_piece_cache",
239                                 sizeof(struct sram_piece),
240                                 0, SLAB_PANIC, NULL);
241
242         l1sram_init();
243         l1_data_sram_init();
244         l1_inst_sram_init();
245         l2_sram_init();
246
247         return 0;
248 }
249 pure_initcall(bfin_sram_init);
250
251 /* SRAM allocate function */
252 static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
253                 struct sram_piece *pused_head)
254 {
255         struct sram_piece *pslot, *plast, *pavail;
256
257         if (size <= 0 || !pfree_head || !pused_head)
258                 return NULL;
259
260         /* Align the size */
261         size = (size + 3) & ~3;
262
263         pslot = pfree_head->next;
264         plast = pfree_head;
265
266         /* search an available piece slot */
267         while (pslot != NULL && size > pslot->size) {
268                 plast = pslot;
269                 pslot = pslot->next;
270         }
271
272         if (!pslot)
273                 return NULL;
274
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);
280
281                 if (!pavail)
282                         return NULL;
283
284                 pavail->paddr = pslot->paddr;
285                 pavail->size = size;
286                 pslot->paddr += size;
287                 pslot->size -= size;
288         }
289
290         pavail->pid = current->pid;
291
292         pslot = pused_head->next;
293         plast = pused_head;
294
295         /* insert new piece into used piece list !!! */
296         while (pslot != NULL && pavail->paddr < pslot->paddr) {
297                 plast = pslot;
298                 pslot = pslot->next;
299         }
300
301         pavail->next = pslot;
302         plast->next = pavail;
303
304         return pavail->paddr;
305 }
306
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)
311 {
312         struct sram_piece *pslot, *pmax;
313
314         if (!pfree_head || !pused_head)
315                 return NULL;
316
317         pmax = pslot = pfree_head->next;
318
319         /* search an available piece slot */
320         while (pslot != NULL) {
321                 if (pslot->size > pmax->size)
322                         pmax = pslot;
323                 pslot = pslot->next;
324         }
325
326         if (!pmax)
327                 return NULL;
328
329         *psize = pmax->size;
330
331         return _sram_alloc(*psize, pfree_head, pused_head);
332 }
333
334 /* SRAM free function */
335 static int _sram_free(const void *addr,
336                         struct sram_piece *pfree_head,
337                         struct sram_piece *pused_head)
338 {
339         struct sram_piece *pslot, *plast, *pavail;
340
341         if (!pfree_head || !pused_head)
342                 return -1;
343
344         /* search the relevant memory slot */
345         pslot = pused_head->next;
346         plast = pused_head;
347
348         /* search an available piece slot */
349         while (pslot != NULL && pslot->paddr != addr) {
350                 plast = pslot;
351                 pslot = pslot->next;
352         }
353
354         if (!pslot)
355                 return -1;
356
357         plast->next = pslot->next;
358         pavail = pslot;
359         pavail->pid = 0;
360
361         /* insert free pieces back to the free list */
362         pslot = pfree_head->next;
363         plast = pfree_head;
364
365         while (pslot != NULL && addr > pslot->paddr) {
366                 plast = pslot;
367                 pslot = pslot->next;
368         }
369
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;
377         }
378
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);
383         }
384
385         return 0;
386 }
387
388 int sram_free(const void *addr)
389 {
390
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;
416 }
417 EXPORT_SYMBOL(sram_free);
418
419 void *l1_data_A_sram_alloc(size_t size)
420 {
421 #if L1_DATA_A_LENGTH != 0
422         unsigned long flags;
423         void *addr;
424         unsigned int cpu;
425
426         cpu = get_cpu();
427         /* add mutex operation */
428         spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
429
430         addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
431                         &per_cpu(used_l1_data_A_sram_head, cpu));
432
433         /* add mutex operation */
434         spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
435         put_cpu();
436
437         pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
438                  (long unsigned int)addr, size);
439
440         return addr;
441 #else
442         return NULL;
443 #endif
444 }
445 EXPORT_SYMBOL(l1_data_A_sram_alloc);
446
447 int l1_data_A_sram_free(const void *addr)
448 {
449 #if L1_DATA_A_LENGTH != 0
450         unsigned long flags;
451         int ret;
452         unsigned int cpu;
453
454         cpu = get_cpu();
455         /* add mutex operation */
456         spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
457
458         ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu),
459                         &per_cpu(used_l1_data_A_sram_head, cpu));
460
461         /* add mutex operation */
462         spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
463         put_cpu();
464
465         return ret;
466 #else
467         return -1;
468 #endif
469 }
470 EXPORT_SYMBOL(l1_data_A_sram_free);
471
472 void *l1_data_B_sram_alloc(size_t size)
473 {
474 #if L1_DATA_B_LENGTH != 0
475         unsigned long flags;
476         void *addr;
477         unsigned int cpu;
478
479         cpu = get_cpu();
480         /* add mutex operation */
481         spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
482
483         addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
484                         &per_cpu(used_l1_data_B_sram_head, cpu));
485
486         /* add mutex operation */
487         spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
488         put_cpu();
489
490         pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
491                  (long unsigned int)addr, size);
492
493         return addr;
494 #else
495         return NULL;
496 #endif
497 }
498 EXPORT_SYMBOL(l1_data_B_sram_alloc);
499
500 int l1_data_B_sram_free(const void *addr)
501 {
502 #if L1_DATA_B_LENGTH != 0
503         unsigned long flags;
504         int ret;
505         unsigned int cpu;
506
507         cpu = get_cpu();
508         /* add mutex operation */
509         spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
510
511         ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu),
512                         &per_cpu(used_l1_data_B_sram_head, cpu));
513
514         /* add mutex operation */
515         spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
516         put_cpu();
517
518         return ret;
519 #else
520         return -1;
521 #endif
522 }
523 EXPORT_SYMBOL(l1_data_B_sram_free);
524
525 void *l1_data_sram_alloc(size_t size)
526 {
527         void *addr = l1_data_A_sram_alloc(size);
528
529         if (!addr)
530                 addr = l1_data_B_sram_alloc(size);
531
532         return addr;
533 }
534 EXPORT_SYMBOL(l1_data_sram_alloc);
535
536 void *l1_data_sram_zalloc(size_t size)
537 {
538         void *addr = l1_data_sram_alloc(size);
539
540         if (addr)
541                 memset(addr, 0x00, size);
542
543         return addr;
544 }
545 EXPORT_SYMBOL(l1_data_sram_zalloc);
546
547 int l1_data_sram_free(const void *addr)
548 {
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;
554 }
555 EXPORT_SYMBOL(l1_data_sram_free);
556
557 void *l1_inst_sram_alloc(size_t size)
558 {
559 #if L1_CODE_LENGTH != 0
560         unsigned long flags;
561         void *addr;
562         unsigned int cpu;
563
564         cpu = get_cpu();
565         /* add mutex operation */
566         spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
567
568         addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
569                         &per_cpu(used_l1_inst_sram_head, cpu));
570
571         /* add mutex operation */
572         spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
573         put_cpu();
574
575         pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
576                  (long unsigned int)addr, size);
577
578         return addr;
579 #else
580         return NULL;
581 #endif
582 }
583 EXPORT_SYMBOL(l1_inst_sram_alloc);
584
585 int l1_inst_sram_free(const void *addr)
586 {
587 #if L1_CODE_LENGTH != 0
588         unsigned long flags;
589         int ret;
590         unsigned int cpu;
591
592         cpu = get_cpu();
593         /* add mutex operation */
594         spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
595
596         ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
597                         &per_cpu(used_l1_inst_sram_head, cpu));
598
599         /* add mutex operation */
600         spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
601         put_cpu();
602
603         return ret;
604 #else
605         return -1;
606 #endif
607 }
608 EXPORT_SYMBOL(l1_inst_sram_free);
609
610 /* L1 Scratchpad memory allocate function */
611 void *l1sram_alloc(size_t size)
612 {
613         unsigned long flags;
614         void *addr;
615         unsigned int cpu;
616
617         cpu = get_cpu();
618         /* add mutex operation */
619         spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
620
621         addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu),
622                         &per_cpu(used_l1_ssram_head, cpu));
623
624         /* add mutex operation */
625         spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
626         put_cpu();
627
628         return addr;
629 }
630
631 /* L1 Scratchpad memory allocate function */
632 void *l1sram_alloc_max(size_t *psize)
633 {
634         unsigned long flags;
635         void *addr;
636         unsigned int cpu;
637
638         cpu = get_cpu();
639         /* add mutex operation */
640         spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
641
642         addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
643                         &per_cpu(used_l1_ssram_head, cpu), psize);
644
645         /* add mutex operation */
646         spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
647         put_cpu();
648
649         return addr;
650 }
651
652 /* L1 Scratchpad memory free function */
653 int l1sram_free(const void *addr)
654 {
655         unsigned long flags;
656         int ret;
657         unsigned int cpu;
658
659         cpu = get_cpu();
660         /* add mutex operation */
661         spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
662
663         ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
664                         &per_cpu(used_l1_ssram_head, cpu));
665
666         /* add mutex operation */
667         spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
668         put_cpu();
669
670         return ret;
671 }
672
673 void *l2_sram_alloc(size_t size)
674 {
675 #if L2_LENGTH != 0
676         unsigned long flags;
677         void *addr;
678
679         /* add mutex operation */
680         spin_lock_irqsave(&l2_sram_lock, flags);
681
682         addr = _sram_alloc(size, &free_l2_sram_head,
683                         &used_l2_sram_head);
684
685         /* add mutex operation */
686         spin_unlock_irqrestore(&l2_sram_lock, flags);
687
688         pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
689                  (long unsigned int)addr, size);
690
691         return addr;
692 #else
693         return NULL;
694 #endif
695 }
696 EXPORT_SYMBOL(l2_sram_alloc);
697
698 void *l2_sram_zalloc(size_t size)
699 {
700         void *addr = l2_sram_alloc(size);
701
702         if (addr)
703                 memset(addr, 0x00, size);
704
705         return addr;
706 }
707 EXPORT_SYMBOL(l2_sram_zalloc);
708
709 int l2_sram_free(const void *addr)
710 {
711 #if L2_LENGTH != 0
712         unsigned long flags;
713         int ret;
714
715         /* add mutex operation */
716         spin_lock_irqsave(&l2_sram_lock, flags);
717
718         ret = _sram_free(addr, &free_l2_sram_head,
719                         &used_l2_sram_head);
720
721         /* add mutex operation */
722         spin_unlock_irqrestore(&l2_sram_lock, flags);
723
724         return ret;
725 #else
726         return -1;
727 #endif
728 }
729 EXPORT_SYMBOL(l2_sram_free);
730
731 int sram_free_with_lsl(const void *addr)
732 {
733         struct sram_list_struct *lsl, **tmp;
734         struct mm_struct *mm = current->mm;
735
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);
745
746         return 0;
747 }
748 EXPORT_SYMBOL(sram_free_with_lsl);
749
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.
753  */
754 void *sram_alloc_with_lsl(size_t size, unsigned long flags)
755 {
756         void *addr = NULL;
757         struct sram_list_struct *lsl = NULL;
758         struct mm_struct *mm = current->mm;
759
760         lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
761         if (!lsl)
762                 return NULL;
763
764         if (flags & L1_INST_SRAM)
765                 addr = l1_inst_sram_alloc(size);
766
767         if (addr == NULL && (flags & L1_DATA_A_SRAM))
768                 addr = l1_data_A_sram_alloc(size);
769
770         if (addr == NULL && (flags & L1_DATA_B_SRAM))
771                 addr = l1_data_B_sram_alloc(size);
772
773         if (addr == NULL && (flags & L2_SRAM))
774                 addr = l2_sram_alloc(size);
775
776         if (addr == NULL) {
777                 kfree(lsl);
778                 return NULL;
779         }
780         lsl->addr = addr;
781         lsl->length = size;
782         lsl->next = mm->context.sram_list;
783         mm->context.sram_list = lsl;
784         return addr;
785 }
786 EXPORT_SYMBOL(sram_alloc_with_lsl);
787
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.
791  */
792 /* Need to keep line of output the same.  Currently, that is 44 bytes
793  * (including newline).
794  */
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)
798 {
799         struct sram_piece *pslot;
800
801         if (!pfree_head || !pused_head)
802                 return -1;
803
804         *len += sprintf(&buf[*len], "--- SRAM %-14s Size   PID State     \n", desc);
805
806         /* search the relevant memory slot */
807         pslot = pused_head->next;
808
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");
813
814                 pslot = pslot->next;
815         }
816
817         pslot = pfree_head->next;
818
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");
823
824                 pslot = pslot->next;
825         }
826
827         return 0;
828 }
829 static int sram_proc_read(char *buf, char **start, off_t offset, int count,
830                 int *eof, void *data)
831 {
832         int len = 0;
833         unsigned int cpu;
834
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
857         }
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;
866 }
867
868 static int __init sram_proc_init(void)
869 {
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;
875         }
876         ptr->read_proc = sram_proc_read;
877         return 0;
878 }
879 late_initcall(sram_proc_init);
880 #endif