Merge branch 'upstream/wm8974' into for-2.6.33
[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 static DEFINE_PER_CPU(spinlock_t, l1sram_lock) ____cacheline_aligned_in_smp;
46 static DEFINE_PER_CPU(spinlock_t, l1_data_sram_lock) ____cacheline_aligned_in_smp;
47 static DEFINE_PER_CPU(spinlock_t, l1_inst_sram_lock) ____cacheline_aligned_in_smp;
48 static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp;
49
50 /* the data structure for L1 scratchpad and DATA SRAM */
51 struct sram_piece {
52         void *paddr;
53         int size;
54         pid_t pid;
55         struct sram_piece *next;
56 };
57
58 static DEFINE_PER_CPU(struct sram_piece, free_l1_ssram_head);
59 static DEFINE_PER_CPU(struct sram_piece, used_l1_ssram_head);
60
61 #if L1_DATA_A_LENGTH != 0
62 static DEFINE_PER_CPU(struct sram_piece, free_l1_data_A_sram_head);
63 static DEFINE_PER_CPU(struct sram_piece, used_l1_data_A_sram_head);
64 #endif
65
66 #if L1_DATA_B_LENGTH != 0
67 static DEFINE_PER_CPU(struct sram_piece, free_l1_data_B_sram_head);
68 static DEFINE_PER_CPU(struct sram_piece, used_l1_data_B_sram_head);
69 #endif
70
71 #if L1_CODE_LENGTH != 0
72 static DEFINE_PER_CPU(struct sram_piece, free_l1_inst_sram_head);
73 static DEFINE_PER_CPU(struct sram_piece, used_l1_inst_sram_head);
74 #endif
75
76 #if L2_LENGTH != 0
77 static struct sram_piece free_l2_sram_head, used_l2_sram_head;
78 #endif
79
80 static struct kmem_cache *sram_piece_cache;
81
82 /* L1 Scratchpad SRAM initialization function */
83 static void __init l1sram_init(void)
84 {
85         unsigned int cpu;
86         unsigned long reserve;
87
88 #ifdef CONFIG_SMP
89         reserve = 0;
90 #else
91         reserve = sizeof(struct l1_scratch_task_info);
92 #endif
93
94         for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
95                 per_cpu(free_l1_ssram_head, cpu).next =
96                         kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
97                 if (!per_cpu(free_l1_ssram_head, cpu).next) {
98                         printk(KERN_INFO "Fail to initialize Scratchpad data SRAM.\n");
99                         return;
100                 }
101
102                 per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu) + reserve;
103                 per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH - reserve;
104                 per_cpu(free_l1_ssram_head, cpu).next->pid = 0;
105                 per_cpu(free_l1_ssram_head, cpu).next->next = NULL;
106
107                 per_cpu(used_l1_ssram_head, cpu).next = NULL;
108
109                 /* mutex initialize */
110                 spin_lock_init(&per_cpu(l1sram_lock, cpu));
111                 printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
112                         L1_SCRATCH_LENGTH >> 10);
113         }
114 }
115
116 static void __init l1_data_sram_init(void)
117 {
118 #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
119         unsigned int cpu;
120 #endif
121 #if L1_DATA_A_LENGTH != 0
122         for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
123                 per_cpu(free_l1_data_A_sram_head, cpu).next =
124                         kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
125                 if (!per_cpu(free_l1_data_A_sram_head, cpu).next) {
126                         printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n");
127                         return;
128                 }
129
130                 per_cpu(free_l1_data_A_sram_head, cpu).next->paddr =
131                         (void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1);
132                 per_cpu(free_l1_data_A_sram_head, cpu).next->size =
133                         L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
134                 per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0;
135                 per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL;
136
137                 per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;
138
139                 printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n",
140                         L1_DATA_A_LENGTH >> 10,
141                         per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10);
142         }
143 #endif
144 #if L1_DATA_B_LENGTH != 0
145         for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
146                 per_cpu(free_l1_data_B_sram_head, cpu).next =
147                         kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
148                 if (!per_cpu(free_l1_data_B_sram_head, cpu).next) {
149                         printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n");
150                         return;
151                 }
152
153                 per_cpu(free_l1_data_B_sram_head, cpu).next->paddr =
154                         (void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1);
155                 per_cpu(free_l1_data_B_sram_head, cpu).next->size =
156                         L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
157                 per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0;
158                 per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL;
159
160                 per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;
161
162                 printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n",
163                         L1_DATA_B_LENGTH >> 10,
164                         per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10);
165                 /* mutex initialize */
166         }
167 #endif
168
169 #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
170         for (cpu = 0; cpu < num_possible_cpus(); ++cpu)
171                 spin_lock_init(&per_cpu(l1_data_sram_lock, cpu));
172 #endif
173 }
174
175 static void __init l1_inst_sram_init(void)
176 {
177 #if L1_CODE_LENGTH != 0
178         unsigned int cpu;
179         for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
180                 per_cpu(free_l1_inst_sram_head, cpu).next =
181                         kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
182                 if (!per_cpu(free_l1_inst_sram_head, cpu).next) {
183                         printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n");
184                         return;
185                 }
186
187                 per_cpu(free_l1_inst_sram_head, cpu).next->paddr =
188                         (void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1);
189                 per_cpu(free_l1_inst_sram_head, cpu).next->size =
190                         L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
191                 per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0;
192                 per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL;
193
194                 per_cpu(used_l1_inst_sram_head, cpu).next = NULL;
195
196                 printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n",
197                         L1_CODE_LENGTH >> 10,
198                         per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10);
199
200                 /* mutex initialize */
201                 spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
202         }
203 #endif
204 }
205
206 static void __init l2_sram_init(void)
207 {
208 #if L2_LENGTH != 0
209         free_l2_sram_head.next =
210                 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
211         if (!free_l2_sram_head.next) {
212                 printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
213                 return;
214         }
215
216         free_l2_sram_head.next->paddr =
217                 (void *)L2_START + (_ebss_l2 - _stext_l2);
218         free_l2_sram_head.next->size =
219                 L2_LENGTH - (_ebss_l2 - _stext_l2);
220         free_l2_sram_head.next->pid = 0;
221         free_l2_sram_head.next->next = NULL;
222
223         used_l2_sram_head.next = NULL;
224
225         printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n",
226                 L2_LENGTH >> 10,
227                 free_l2_sram_head.next->size >> 10);
228 #endif
229
230         /* mutex initialize */
231         spin_lock_init(&l2_sram_lock);
232 }
233
234 static int __init bfin_sram_init(void)
235 {
236         sram_piece_cache = kmem_cache_create("sram_piece_cache",
237                                 sizeof(struct sram_piece),
238                                 0, SLAB_PANIC, NULL);
239
240         l1sram_init();
241         l1_data_sram_init();
242         l1_inst_sram_init();
243         l2_sram_init();
244
245         return 0;
246 }
247 pure_initcall(bfin_sram_init);
248
249 /* SRAM allocate function */
250 static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
251                 struct sram_piece *pused_head)
252 {
253         struct sram_piece *pslot, *plast, *pavail;
254
255         if (size <= 0 || !pfree_head || !pused_head)
256                 return NULL;
257
258         /* Align the size */
259         size = (size + 3) & ~3;
260
261         pslot = pfree_head->next;
262         plast = pfree_head;
263
264         /* search an available piece slot */
265         while (pslot != NULL && size > pslot->size) {
266                 plast = pslot;
267                 pslot = pslot->next;
268         }
269
270         if (!pslot)
271                 return NULL;
272
273         if (pslot->size == size) {
274                 plast->next = pslot->next;
275                 pavail = pslot;
276         } else {
277                 pavail = kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
278
279                 if (!pavail)
280                         return NULL;
281
282                 pavail->paddr = pslot->paddr;
283                 pavail->size = size;
284                 pslot->paddr += size;
285                 pslot->size -= size;
286         }
287
288         pavail->pid = current->pid;
289
290         pslot = pused_head->next;
291         plast = pused_head;
292
293         /* insert new piece into used piece list !!! */
294         while (pslot != NULL && pavail->paddr < pslot->paddr) {
295                 plast = pslot;
296                 pslot = pslot->next;
297         }
298
299         pavail->next = pslot;
300         plast->next = pavail;
301
302         return pavail->paddr;
303 }
304
305 /* Allocate the largest available block.  */
306 static void *_sram_alloc_max(struct sram_piece *pfree_head,
307                                 struct sram_piece *pused_head,
308                                 unsigned long *psize)
309 {
310         struct sram_piece *pslot, *pmax;
311
312         if (!pfree_head || !pused_head)
313                 return NULL;
314
315         pmax = pslot = pfree_head->next;
316
317         /* search an available piece slot */
318         while (pslot != NULL) {
319                 if (pslot->size > pmax->size)
320                         pmax = pslot;
321                 pslot = pslot->next;
322         }
323
324         if (!pmax)
325                 return NULL;
326
327         *psize = pmax->size;
328
329         return _sram_alloc(*psize, pfree_head, pused_head);
330 }
331
332 /* SRAM free function */
333 static int _sram_free(const void *addr,
334                         struct sram_piece *pfree_head,
335                         struct sram_piece *pused_head)
336 {
337         struct sram_piece *pslot, *plast, *pavail;
338
339         if (!pfree_head || !pused_head)
340                 return -1;
341
342         /* search the relevant memory slot */
343         pslot = pused_head->next;
344         plast = pused_head;
345
346         /* search an available piece slot */
347         while (pslot != NULL && pslot->paddr != addr) {
348                 plast = pslot;
349                 pslot = pslot->next;
350         }
351
352         if (!pslot)
353                 return -1;
354
355         plast->next = pslot->next;
356         pavail = pslot;
357         pavail->pid = 0;
358
359         /* insert free pieces back to the free list */
360         pslot = pfree_head->next;
361         plast = pfree_head;
362
363         while (pslot != NULL && addr > pslot->paddr) {
364                 plast = pslot;
365                 pslot = pslot->next;
366         }
367
368         if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
369                 plast->size += pavail->size;
370                 kmem_cache_free(sram_piece_cache, pavail);
371         } else {
372                 pavail->next = plast->next;
373                 plast->next = pavail;
374                 plast = pavail;
375         }
376
377         if (pslot && plast->paddr + plast->size == pslot->paddr) {
378                 plast->size += pslot->size;
379                 plast->next = pslot->next;
380                 kmem_cache_free(sram_piece_cache, pslot);
381         }
382
383         return 0;
384 }
385
386 int sram_free(const void *addr)
387 {
388
389 #if L1_CODE_LENGTH != 0
390         if (addr >= (void *)get_l1_code_start()
391                  && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
392                 return l1_inst_sram_free(addr);
393         else
394 #endif
395 #if L1_DATA_A_LENGTH != 0
396         if (addr >= (void *)get_l1_data_a_start()
397                  && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH))
398                 return l1_data_A_sram_free(addr);
399         else
400 #endif
401 #if L1_DATA_B_LENGTH != 0
402         if (addr >= (void *)get_l1_data_b_start()
403                  && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH))
404                 return l1_data_B_sram_free(addr);
405         else
406 #endif
407 #if L2_LENGTH != 0
408         if (addr >= (void *)L2_START
409                  && addr < (void *)(L2_START + L2_LENGTH))
410                 return l2_sram_free(addr);
411         else
412 #endif
413                 return -1;
414 }
415 EXPORT_SYMBOL(sram_free);
416
417 void *l1_data_A_sram_alloc(size_t size)
418 {
419         unsigned long flags;
420         void *addr = NULL;
421         unsigned int cpu;
422
423         cpu = get_cpu();
424         /* add mutex operation */
425         spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
426
427 #if L1_DATA_A_LENGTH != 0
428         addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
429                         &per_cpu(used_l1_data_A_sram_head, cpu));
430 #endif
431
432         /* add mutex operation */
433         spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
434         put_cpu();
435
436         pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
437                  (long unsigned int)addr, size);
438
439         return addr;
440 }
441 EXPORT_SYMBOL(l1_data_A_sram_alloc);
442
443 int l1_data_A_sram_free(const void *addr)
444 {
445         unsigned long flags;
446         int ret;
447         unsigned int cpu;
448
449         cpu = get_cpu();
450         /* add mutex operation */
451         spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
452
453 #if L1_DATA_A_LENGTH != 0
454         ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu),
455                         &per_cpu(used_l1_data_A_sram_head, cpu));
456 #else
457         ret = -1;
458 #endif
459
460         /* add mutex operation */
461         spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
462         put_cpu();
463
464         return ret;
465 }
466 EXPORT_SYMBOL(l1_data_A_sram_free);
467
468 void *l1_data_B_sram_alloc(size_t size)
469 {
470 #if L1_DATA_B_LENGTH != 0
471         unsigned long flags;
472         void *addr;
473         unsigned int cpu;
474
475         cpu = get_cpu();
476         /* add mutex operation */
477         spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
478
479         addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
480                         &per_cpu(used_l1_data_B_sram_head, cpu));
481
482         /* add mutex operation */
483         spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
484         put_cpu();
485
486         pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
487                  (long unsigned int)addr, size);
488
489         return addr;
490 #else
491         return NULL;
492 #endif
493 }
494 EXPORT_SYMBOL(l1_data_B_sram_alloc);
495
496 int l1_data_B_sram_free(const void *addr)
497 {
498 #if L1_DATA_B_LENGTH != 0
499         unsigned long flags;
500         int ret;
501         unsigned int cpu;
502
503         cpu = get_cpu();
504         /* add mutex operation */
505         spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
506
507         ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu),
508                         &per_cpu(used_l1_data_B_sram_head, cpu));
509
510         /* add mutex operation */
511         spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
512         put_cpu();
513
514         return ret;
515 #else
516         return -1;
517 #endif
518 }
519 EXPORT_SYMBOL(l1_data_B_sram_free);
520
521 void *l1_data_sram_alloc(size_t size)
522 {
523         void *addr = l1_data_A_sram_alloc(size);
524
525         if (!addr)
526                 addr = l1_data_B_sram_alloc(size);
527
528         return addr;
529 }
530 EXPORT_SYMBOL(l1_data_sram_alloc);
531
532 void *l1_data_sram_zalloc(size_t size)
533 {
534         void *addr = l1_data_sram_alloc(size);
535
536         if (addr)
537                 memset(addr, 0x00, size);
538
539         return addr;
540 }
541 EXPORT_SYMBOL(l1_data_sram_zalloc);
542
543 int l1_data_sram_free(const void *addr)
544 {
545         int ret;
546         ret = l1_data_A_sram_free(addr);
547         if (ret == -1)
548                 ret = l1_data_B_sram_free(addr);
549         return ret;
550 }
551 EXPORT_SYMBOL(l1_data_sram_free);
552
553 void *l1_inst_sram_alloc(size_t size)
554 {
555 #if L1_CODE_LENGTH != 0
556         unsigned long flags;
557         void *addr;
558         unsigned int cpu;
559
560         cpu = get_cpu();
561         /* add mutex operation */
562         spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
563
564         addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
565                         &per_cpu(used_l1_inst_sram_head, cpu));
566
567         /* add mutex operation */
568         spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
569         put_cpu();
570
571         pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
572                  (long unsigned int)addr, size);
573
574         return addr;
575 #else
576         return NULL;
577 #endif
578 }
579 EXPORT_SYMBOL(l1_inst_sram_alloc);
580
581 int l1_inst_sram_free(const void *addr)
582 {
583 #if L1_CODE_LENGTH != 0
584         unsigned long flags;
585         int ret;
586         unsigned int cpu;
587
588         cpu = get_cpu();
589         /* add mutex operation */
590         spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
591
592         ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
593                         &per_cpu(used_l1_inst_sram_head, cpu));
594
595         /* add mutex operation */
596         spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
597         put_cpu();
598
599         return ret;
600 #else
601         return -1;
602 #endif
603 }
604 EXPORT_SYMBOL(l1_inst_sram_free);
605
606 /* L1 Scratchpad memory allocate function */
607 void *l1sram_alloc(size_t size)
608 {
609         unsigned long flags;
610         void *addr;
611         unsigned int cpu;
612
613         cpu = get_cpu();
614         /* add mutex operation */
615         spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
616
617         addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu),
618                         &per_cpu(used_l1_ssram_head, cpu));
619
620         /* add mutex operation */
621         spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
622         put_cpu();
623
624         return addr;
625 }
626
627 /* L1 Scratchpad memory allocate function */
628 void *l1sram_alloc_max(size_t *psize)
629 {
630         unsigned long flags;
631         void *addr;
632         unsigned int cpu;
633
634         cpu = get_cpu();
635         /* add mutex operation */
636         spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
637
638         addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
639                         &per_cpu(used_l1_ssram_head, cpu), psize);
640
641         /* add mutex operation */
642         spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
643         put_cpu();
644
645         return addr;
646 }
647
648 /* L1 Scratchpad memory free function */
649 int l1sram_free(const void *addr)
650 {
651         unsigned long flags;
652         int ret;
653         unsigned int cpu;
654
655         cpu = get_cpu();
656         /* add mutex operation */
657         spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
658
659         ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
660                         &per_cpu(used_l1_ssram_head, cpu));
661
662         /* add mutex operation */
663         spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
664         put_cpu();
665
666         return ret;
667 }
668
669 void *l2_sram_alloc(size_t size)
670 {
671 #if L2_LENGTH != 0
672         unsigned long flags;
673         void *addr;
674
675         /* add mutex operation */
676         spin_lock_irqsave(&l2_sram_lock, flags);
677
678         addr = _sram_alloc(size, &free_l2_sram_head,
679                         &used_l2_sram_head);
680
681         /* add mutex operation */
682         spin_unlock_irqrestore(&l2_sram_lock, flags);
683
684         pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
685                  (long unsigned int)addr, size);
686
687         return addr;
688 #else
689         return NULL;
690 #endif
691 }
692 EXPORT_SYMBOL(l2_sram_alloc);
693
694 void *l2_sram_zalloc(size_t size)
695 {
696         void *addr = l2_sram_alloc(size);
697
698         if (addr)
699                 memset(addr, 0x00, size);
700
701         return addr;
702 }
703 EXPORT_SYMBOL(l2_sram_zalloc);
704
705 int l2_sram_free(const void *addr)
706 {
707 #if L2_LENGTH != 0
708         unsigned long flags;
709         int ret;
710
711         /* add mutex operation */
712         spin_lock_irqsave(&l2_sram_lock, flags);
713
714         ret = _sram_free(addr, &free_l2_sram_head,
715                         &used_l2_sram_head);
716
717         /* add mutex operation */
718         spin_unlock_irqrestore(&l2_sram_lock, flags);
719
720         return ret;
721 #else
722         return -1;
723 #endif
724 }
725 EXPORT_SYMBOL(l2_sram_free);
726
727 int sram_free_with_lsl(const void *addr)
728 {
729         struct sram_list_struct *lsl, **tmp;
730         struct mm_struct *mm = current->mm;
731
732         for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
733                 if ((*tmp)->addr == addr)
734                         goto found;
735         return -1;
736 found:
737         lsl = *tmp;
738         sram_free(addr);
739         *tmp = lsl->next;
740         kfree(lsl);
741
742         return 0;
743 }
744 EXPORT_SYMBOL(sram_free_with_lsl);
745
746 /* Allocate memory and keep in L1 SRAM List (lsl) so that the resources are
747  * tracked.  These are designed for userspace so that when a process exits,
748  * we can safely reap their resources.
749  */
750 void *sram_alloc_with_lsl(size_t size, unsigned long flags)
751 {
752         void *addr = NULL;
753         struct sram_list_struct *lsl = NULL;
754         struct mm_struct *mm = current->mm;
755
756         lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
757         if (!lsl)
758                 return NULL;
759
760         if (flags & L1_INST_SRAM)
761                 addr = l1_inst_sram_alloc(size);
762
763         if (addr == NULL && (flags & L1_DATA_A_SRAM))
764                 addr = l1_data_A_sram_alloc(size);
765
766         if (addr == NULL && (flags & L1_DATA_B_SRAM))
767                 addr = l1_data_B_sram_alloc(size);
768
769         if (addr == NULL && (flags & L2_SRAM))
770                 addr = l2_sram_alloc(size);
771
772         if (addr == NULL) {
773                 kfree(lsl);
774                 return NULL;
775         }
776         lsl->addr = addr;
777         lsl->length = size;
778         lsl->next = mm->context.sram_list;
779         mm->context.sram_list = lsl;
780         return addr;
781 }
782 EXPORT_SYMBOL(sram_alloc_with_lsl);
783
784 #ifdef CONFIG_PROC_FS
785 /* Once we get a real allocator, we'll throw all of this away.
786  * Until then, we need some sort of visibility into the L1 alloc.
787  */
788 /* Need to keep line of output the same.  Currently, that is 44 bytes
789  * (including newline).
790  */
791 static int _sram_proc_read(char *buf, int *len, int count, const char *desc,
792                 struct sram_piece *pfree_head,
793                 struct sram_piece *pused_head)
794 {
795         struct sram_piece *pslot;
796
797         if (!pfree_head || !pused_head)
798                 return -1;
799
800         *len += sprintf(&buf[*len], "--- SRAM %-14s Size   PID State     \n", desc);
801
802         /* search the relevant memory slot */
803         pslot = pused_head->next;
804
805         while (pslot != NULL) {
806                 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
807                         pslot->paddr, pslot->paddr + pslot->size,
808                         pslot->size, pslot->pid, "ALLOCATED");
809
810                 pslot = pslot->next;
811         }
812
813         pslot = pfree_head->next;
814
815         while (pslot != NULL) {
816                 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
817                         pslot->paddr, pslot->paddr + pslot->size,
818                         pslot->size, pslot->pid, "FREE");
819
820                 pslot = pslot->next;
821         }
822
823         return 0;
824 }
825 static int sram_proc_read(char *buf, char **start, off_t offset, int count,
826                 int *eof, void *data)
827 {
828         int len = 0;
829         unsigned int cpu;
830
831         for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
832                 if (_sram_proc_read(buf, &len, count, "Scratchpad",
833                         &per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu)))
834                         goto not_done;
835 #if L1_DATA_A_LENGTH != 0
836                 if (_sram_proc_read(buf, &len, count, "L1 Data A",
837                         &per_cpu(free_l1_data_A_sram_head, cpu),
838                         &per_cpu(used_l1_data_A_sram_head, cpu)))
839                         goto not_done;
840 #endif
841 #if L1_DATA_B_LENGTH != 0
842                 if (_sram_proc_read(buf, &len, count, "L1 Data B",
843                         &per_cpu(free_l1_data_B_sram_head, cpu),
844                         &per_cpu(used_l1_data_B_sram_head, cpu)))
845                         goto not_done;
846 #endif
847 #if L1_CODE_LENGTH != 0
848                 if (_sram_proc_read(buf, &len, count, "L1 Instruction",
849                         &per_cpu(free_l1_inst_sram_head, cpu),
850                         &per_cpu(used_l1_inst_sram_head, cpu)))
851                         goto not_done;
852 #endif
853         }
854 #if L2_LENGTH != 0
855         if (_sram_proc_read(buf, &len, count, "L2", &free_l2_sram_head,
856                 &used_l2_sram_head))
857                 goto not_done;
858 #endif
859         *eof = 1;
860  not_done:
861         return len;
862 }
863
864 static int __init sram_proc_init(void)
865 {
866         struct proc_dir_entry *ptr;
867         ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
868         if (!ptr) {
869                 printk(KERN_WARNING "unable to create /proc/sram\n");
870                 return -1;
871         }
872         ptr->read_proc = sram_proc_read;
873         return 0;
874 }
875 late_initcall(sram_proc_init);
876 #endif