[IA64] remove time interpolator
[pandora-kernel.git] / arch / blackfin / mm / blackfin_sram.c
1 /*
2  * File:         arch/blackfin/mm/blackfin_sram.c
3  * Based on:
4  * Author:
5  *
6  * Created:
7  * Description:  SRAM driver for Blackfin ADSP-BF5xx
8  *
9  * Modified:
10  *               Copyright 2004-2007 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/autoconf.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/miscdevice.h>
35 #include <linux/ioport.h>
36 #include <linux/fcntl.h>
37 #include <linux/init.h>
38 #include <linux/poll.h>
39 #include <linux/proc_fs.h>
40 #include <linux/spinlock.h>
41 #include <linux/rtc.h>
42 #include <asm/blackfin.h>
43 #include "blackfin_sram.h"
44
45 spinlock_t l1sram_lock, l1_data_sram_lock, l1_inst_sram_lock;
46
47 #if CONFIG_L1_MAX_PIECE < 16
48 #undef CONFIG_L1_MAX_PIECE
49 #define CONFIG_L1_MAX_PIECE        16
50 #endif
51
52 #if CONFIG_L1_MAX_PIECE > 1024
53 #undef CONFIG_L1_MAX_PIECE
54 #define CONFIG_L1_MAX_PIECE        1024
55 #endif
56
57 #define SRAM_SLT_NULL      0
58 #define SRAM_SLT_FREE      1
59 #define SRAM_SLT_ALLOCATED 2
60
61 /* the data structure for L1 scratchpad and DATA SRAM */
62 struct l1_sram_piece {
63         void *paddr;
64         int size;
65         int flag;
66         pid_t pid;
67 };
68
69 static struct l1_sram_piece l1_ssram[CONFIG_L1_MAX_PIECE];
70
71 #if L1_DATA_A_LENGTH != 0
72 static struct l1_sram_piece l1_data_A_sram[CONFIG_L1_MAX_PIECE];
73 #endif
74
75 #if L1_DATA_B_LENGTH != 0
76 static struct l1_sram_piece l1_data_B_sram[CONFIG_L1_MAX_PIECE];
77 #endif
78
79 #if L1_CODE_LENGTH != 0
80 static struct l1_sram_piece l1_inst_sram[CONFIG_L1_MAX_PIECE];
81 #endif
82
83 /* L1 Scratchpad SRAM initialization function */
84 void __init l1sram_init(void)
85 {
86         printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
87                L1_SCRATCH_LENGTH >> 10);
88
89         memset(&l1_ssram, 0x00, sizeof(l1_ssram));
90         l1_ssram[0].paddr = (void *)L1_SCRATCH_START;
91         l1_ssram[0].size = L1_SCRATCH_LENGTH;
92         l1_ssram[0].flag = SRAM_SLT_FREE;
93
94         /* mutex initialize */
95         spin_lock_init(&l1sram_lock);
96 }
97
98 void __init l1_data_sram_init(void)
99 {
100 #if L1_DATA_A_LENGTH != 0
101         memset(&l1_data_A_sram, 0x00, sizeof(l1_data_A_sram));
102         l1_data_A_sram[0].paddr = (void *)L1_DATA_A_START +
103                                         (_ebss_l1 - _sdata_l1);
104         l1_data_A_sram[0].size = L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
105         l1_data_A_sram[0].flag = SRAM_SLT_FREE;
106
107         printk(KERN_INFO "Blackfin Data A SRAM: %d KB (%d KB free)\n",
108                L1_DATA_A_LENGTH >> 10, l1_data_A_sram[0].size >> 10);
109 #endif
110 #if L1_DATA_B_LENGTH != 0
111         memset(&l1_data_B_sram, 0x00, sizeof(l1_data_B_sram));
112         l1_data_B_sram[0].paddr = (void *)L1_DATA_B_START +
113                                 (_ebss_b_l1 - _sdata_b_l1);
114         l1_data_B_sram[0].size = L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
115         l1_data_B_sram[0].flag = SRAM_SLT_FREE;
116
117         printk(KERN_INFO "Blackfin Data B SRAM: %d KB (%d KB free)\n",
118                L1_DATA_B_LENGTH >> 10, l1_data_B_sram[0].size >> 10);
119 #endif
120
121         /* mutex initialize */
122         spin_lock_init(&l1_data_sram_lock);
123 }
124
125 void __init l1_inst_sram_init(void)
126 {
127 #if L1_CODE_LENGTH != 0
128         memset(&l1_inst_sram, 0x00, sizeof(l1_inst_sram));
129         l1_inst_sram[0].paddr = (void *)L1_CODE_START + (_etext_l1 - _stext_l1);
130         l1_inst_sram[0].size = L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
131         l1_inst_sram[0].flag = SRAM_SLT_FREE;
132
133         printk(KERN_INFO "Blackfin Instruction SRAM: %d KB (%d KB free)\n",
134                L1_CODE_LENGTH >> 10, l1_inst_sram[0].size >> 10);
135 #endif
136
137         /* mutex initialize */
138         spin_lock_init(&l1_inst_sram_lock);
139 }
140
141 /* L1 memory allocate function */
142 static void *_l1_sram_alloc(size_t size, struct l1_sram_piece *pfree, int count)
143 {
144         int i, index = 0;
145         void *addr = NULL;
146
147         if (size <= 0)
148                 return NULL;
149
150         /* Align the size */
151         size = (size + 3) & ~3;
152
153         /* not use the good method to match the best slot !!! */
154         /* search an available memory slot */
155         for (i = 0; i < count; i++) {
156                 if ((pfree[i].flag == SRAM_SLT_FREE)
157                     && (pfree[i].size >= size)) {
158                         addr = pfree[i].paddr;
159                         pfree[i].flag = SRAM_SLT_ALLOCATED;
160                         pfree[i].pid = current->pid;
161                         index = i;
162                         break;
163                 }
164         }
165         if (i >= count)
166                 return NULL;
167
168         /* updated the NULL memory slot !!! */
169         if (pfree[i].size > size) {
170                 for (i = 0; i < count; i++) {
171                         if (pfree[i].flag == SRAM_SLT_NULL) {
172                                 pfree[i].pid = 0;
173                                 pfree[i].flag = SRAM_SLT_FREE;
174                                 pfree[i].paddr = addr + size;
175                                 pfree[i].size = pfree[index].size - size;
176                                 pfree[index].size = size;
177                                 break;
178                         }
179                 }
180         }
181
182         return addr;
183 }
184
185 /* Allocate the largest available block.  */
186 static void *_l1_sram_alloc_max(struct l1_sram_piece *pfree, int count,
187                                 unsigned long *psize)
188 {
189         unsigned long best = 0;
190         int i, index = -1;
191         void *addr = NULL;
192
193         /* search an available memory slot */
194         for (i = 0; i < count; i++) {
195                 if (pfree[i].flag == SRAM_SLT_FREE && pfree[i].size > best) {
196                         addr = pfree[i].paddr;
197                         index = i;
198                         best = pfree[i].size;
199                 }
200         }
201         if (index < 0)
202                 return NULL;
203         *psize = best;
204
205         pfree[index].pid = current->pid;
206         pfree[index].flag = SRAM_SLT_ALLOCATED;
207         return addr;
208 }
209
210 /* L1 memory free function */
211 static int _l1_sram_free(const void *addr,
212                         struct l1_sram_piece *pfree,
213                         int count)
214 {
215         int i, index = 0;
216
217         /* search the relevant memory slot */
218         for (i = 0; i < count; i++) {
219                 if (pfree[i].paddr == addr) {
220                         if (pfree[i].flag != SRAM_SLT_ALLOCATED) {
221                                 /* error log */
222                                 return -1;
223                         }
224                         index = i;
225                         break;
226                 }
227         }
228         if (i >= count)
229                 return -1;
230
231         pfree[index].pid = 0;
232         pfree[index].flag = SRAM_SLT_FREE;
233
234         /* link the next address slot */
235         for (i = 0; i < count; i++) {
236                 if (((pfree[index].paddr + pfree[index].size) == pfree[i].paddr)
237                     && (pfree[i].flag == SRAM_SLT_FREE)) {
238                         pfree[i].pid = 0;
239                         pfree[i].flag = SRAM_SLT_NULL;
240                         pfree[index].size += pfree[i].size;
241                         pfree[index].flag = SRAM_SLT_FREE;
242                         break;
243                 }
244         }
245
246         /* link the last address slot */
247         for (i = 0; i < count; i++) {
248                 if (((pfree[i].paddr + pfree[i].size) == pfree[index].paddr) &&
249                     (pfree[i].flag == SRAM_SLT_FREE)) {
250                         pfree[index].flag = SRAM_SLT_NULL;
251                         pfree[i].size += pfree[index].size;
252                         break;
253                 }
254         }
255
256         return 0;
257 }
258
259 int sram_free(const void *addr)
260 {
261         if (0) {}
262 #if L1_CODE_LENGTH != 0
263         else if (addr >= (void *)L1_CODE_START
264                  && addr < (void *)(L1_CODE_START + L1_CODE_LENGTH))
265                 return l1_inst_sram_free(addr);
266 #endif
267 #if L1_DATA_A_LENGTH != 0
268         else if (addr >= (void *)L1_DATA_A_START
269                  && addr < (void *)(L1_DATA_A_START + L1_DATA_A_LENGTH))
270                 return l1_data_A_sram_free(addr);
271 #endif
272 #if L1_DATA_B_LENGTH != 0
273         else if (addr >= (void *)L1_DATA_B_START
274                  && addr < (void *)(L1_DATA_B_START + L1_DATA_B_LENGTH))
275                 return l1_data_B_sram_free(addr);
276 #endif
277         else
278                 return -1;
279 }
280 EXPORT_SYMBOL(sram_free);
281
282 void *l1_data_A_sram_alloc(size_t size)
283 {
284         unsigned flags;
285         void *addr = NULL;
286
287         /* add mutex operation */
288         spin_lock_irqsave(&l1_data_sram_lock, flags);
289
290 #if L1_DATA_A_LENGTH != 0
291         addr = _l1_sram_alloc(size, l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
292 #endif
293
294         /* add mutex operation */
295         spin_unlock_irqrestore(&l1_data_sram_lock, flags);
296
297         pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
298                  (long unsigned int)addr, size);
299
300         return addr;
301 }
302 EXPORT_SYMBOL(l1_data_A_sram_alloc);
303
304 int l1_data_A_sram_free(const void *addr)
305 {
306         unsigned flags;
307         int ret;
308
309         /* add mutex operation */
310         spin_lock_irqsave(&l1_data_sram_lock, flags);
311
312 #if L1_DATA_A_LENGTH != 0
313         ret = _l1_sram_free(addr,
314                            l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
315 #else
316         ret = -1;
317 #endif
318
319         /* add mutex operation */
320         spin_unlock_irqrestore(&l1_data_sram_lock, flags);
321
322         return ret;
323 }
324 EXPORT_SYMBOL(l1_data_A_sram_free);
325
326 void *l1_data_B_sram_alloc(size_t size)
327 {
328 #if L1_DATA_B_LENGTH != 0
329         unsigned flags;
330         void *addr;
331
332         /* add mutex operation */
333         spin_lock_irqsave(&l1_data_sram_lock, flags);
334
335         addr = _l1_sram_alloc(size, l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
336
337         /* add mutex operation */
338         spin_unlock_irqrestore(&l1_data_sram_lock, flags);
339
340         pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
341                  (long unsigned int)addr, size);
342
343         return addr;
344 #else
345         return NULL;
346 #endif
347 }
348 EXPORT_SYMBOL(l1_data_B_sram_alloc);
349
350 int l1_data_B_sram_free(const void *addr)
351 {
352 #if L1_DATA_B_LENGTH != 0
353         unsigned flags;
354         int ret;
355
356         /* add mutex operation */
357         spin_lock_irqsave(&l1_data_sram_lock, flags);
358
359         ret = _l1_sram_free(addr, l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
360
361         /* add mutex operation */
362         spin_unlock_irqrestore(&l1_data_sram_lock, flags);
363
364         return ret;
365 #else
366         return -1;
367 #endif
368 }
369 EXPORT_SYMBOL(l1_data_B_sram_free);
370
371 void *l1_data_sram_alloc(size_t size)
372 {
373         void *addr = l1_data_A_sram_alloc(size);
374
375         if (!addr)
376                 addr = l1_data_B_sram_alloc(size);
377
378         return addr;
379 }
380 EXPORT_SYMBOL(l1_data_sram_alloc);
381
382 void *l1_data_sram_zalloc(size_t size)
383 {
384         void *addr = l1_data_sram_alloc(size);
385
386         if (addr)
387                 memset(addr, 0x00, size);
388
389         return addr;
390 }
391 EXPORT_SYMBOL(l1_data_sram_zalloc);
392
393 int l1_data_sram_free(const void *addr)
394 {
395         int ret;
396         ret = l1_data_A_sram_free(addr);
397         if (ret == -1)
398                 ret = l1_data_B_sram_free(addr);
399         return ret;
400 }
401 EXPORT_SYMBOL(l1_data_sram_free);
402
403 void *l1_inst_sram_alloc(size_t size)
404 {
405 #if L1_DATA_A_LENGTH != 0
406         unsigned flags;
407         void *addr;
408
409         /* add mutex operation */
410         spin_lock_irqsave(&l1_inst_sram_lock, flags);
411
412         addr = _l1_sram_alloc(size, l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
413
414         /* add mutex operation */
415         spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
416
417         pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
418                  (long unsigned int)addr, size);
419
420         return addr;
421 #else
422         return NULL;
423 #endif
424 }
425 EXPORT_SYMBOL(l1_inst_sram_alloc);
426
427 int l1_inst_sram_free(const void *addr)
428 {
429 #if L1_CODE_LENGTH != 0
430         unsigned flags;
431         int ret;
432
433         /* add mutex operation */
434         spin_lock_irqsave(&l1_inst_sram_lock, flags);
435
436         ret = _l1_sram_free(addr, l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
437
438         /* add mutex operation */
439         spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
440
441         return ret;
442 #else
443         return -1;
444 #endif
445 }
446 EXPORT_SYMBOL(l1_inst_sram_free);
447
448 /* L1 Scratchpad memory allocate function */
449 void *l1sram_alloc(size_t size)
450 {
451         unsigned flags;
452         void *addr;
453
454         /* add mutex operation */
455         spin_lock_irqsave(&l1sram_lock, flags);
456
457         addr = _l1_sram_alloc(size, l1_ssram, ARRAY_SIZE(l1_ssram));
458
459         /* add mutex operation */
460         spin_unlock_irqrestore(&l1sram_lock, flags);
461
462         return addr;
463 }
464
465 /* L1 Scratchpad memory allocate function */
466 void *l1sram_alloc_max(size_t *psize)
467 {
468         unsigned flags;
469         void *addr;
470
471         /* add mutex operation */
472         spin_lock_irqsave(&l1sram_lock, flags);
473
474         addr = _l1_sram_alloc_max(l1_ssram, ARRAY_SIZE(l1_ssram), psize);
475
476         /* add mutex operation */
477         spin_unlock_irqrestore(&l1sram_lock, flags);
478
479         return addr;
480 }
481
482 /* L1 Scratchpad memory free function */
483 int l1sram_free(const void *addr)
484 {
485         unsigned flags;
486         int ret;
487
488         /* add mutex operation */
489         spin_lock_irqsave(&l1sram_lock, flags);
490
491         ret = _l1_sram_free(addr, l1_ssram, ARRAY_SIZE(l1_ssram));
492
493         /* add mutex operation */
494         spin_unlock_irqrestore(&l1sram_lock, flags);
495
496         return ret;
497 }
498
499 int sram_free_with_lsl(const void *addr)
500 {
501         struct sram_list_struct *lsl, **tmp;
502         struct mm_struct *mm = current->mm;
503
504         for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
505                 if ((*tmp)->addr == addr)
506                         goto found;
507         return -1;
508 found:
509         lsl = *tmp;
510         sram_free(addr);
511         *tmp = lsl->next;
512         kfree(lsl);
513
514         return 0;
515 }
516 EXPORT_SYMBOL(sram_free_with_lsl);
517
518 void *sram_alloc_with_lsl(size_t size, unsigned long flags)
519 {
520         void *addr = NULL;
521         struct sram_list_struct *lsl = NULL;
522         struct mm_struct *mm = current->mm;
523
524         lsl = kmalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
525         if (!lsl)
526                 return NULL;
527         memset(lsl, 0, sizeof(*lsl));
528
529         if (flags & L1_INST_SRAM)
530                 addr = l1_inst_sram_alloc(size);
531
532         if (addr == NULL && (flags & L1_DATA_A_SRAM))
533                 addr = l1_data_A_sram_alloc(size);
534
535         if (addr == NULL && (flags & L1_DATA_B_SRAM))
536                 addr = l1_data_B_sram_alloc(size);
537
538         if (addr == NULL) {
539                 kfree(lsl);
540                 return NULL;
541         }
542         lsl->addr = addr;
543         lsl->length = size;
544         lsl->next = mm->context.sram_list;
545         mm->context.sram_list = lsl;
546         return addr;
547 }
548 EXPORT_SYMBOL(sram_alloc_with_lsl);
549
550 #ifdef CONFIG_PROC_FS
551 /* Once we get a real allocator, we'll throw all of this away.
552  * Until then, we need some sort of visibility into the L1 alloc.
553  */
554 static void _l1sram_proc_read(char *buf, int *len, const char *desc,
555                 struct l1_sram_piece *pfree, const int array_size)
556 {
557         int i;
558
559         *len += sprintf(&buf[*len], "--- L1 %-14s Size  PID State\n", desc);
560         for (i = 0; i < array_size; ++i) {
561                 const char *alloc_type;
562                 switch (pfree[i].flag) {
563                 case SRAM_SLT_NULL:      alloc_type = "NULL"; break;
564                 case SRAM_SLT_FREE:      alloc_type = "FREE"; break;
565                 case SRAM_SLT_ALLOCATED: alloc_type = "ALLOCATED"; break;
566                 default:                 alloc_type = "????"; break;
567                 }
568                 *len += sprintf(&buf[*len], "%p-%p %8i %4i %s\n",
569                         pfree[i].paddr, pfree[i].paddr + pfree[i].size,
570                         pfree[i].size, pfree[i].pid, alloc_type);
571         }
572 }
573 static int l1sram_proc_read(char *buf, char **start, off_t offset, int count,
574                 int *eof, void *data)
575 {
576         int len = 0;
577
578         _l1sram_proc_read(buf, &len, "Scratchpad",
579                         l1_ssram, ARRAY_SIZE(l1_ssram));
580 #if L1_DATA_A_LENGTH != 0
581         _l1sram_proc_read(buf, &len, "Data A",
582                         l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
583 #endif
584 #if L1_DATA_B_LENGTH != 0
585         _l1sram_proc_read(buf, &len, "Data B",
586                         l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
587 #endif
588 #if L1_CODE_LENGTH != 0
589         _l1sram_proc_read(buf, &len, "Instruction",
590                         l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
591 #endif
592
593         return len;
594 }
595
596 static int __init l1sram_proc_init(void)
597 {
598         struct proc_dir_entry *ptr;
599         ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
600         if (!ptr) {
601                 printk(KERN_WARNING "unable to create /proc/sram\n");
602                 return -1;
603         }
604         ptr->owner = THIS_MODULE;
605         ptr->read_proc = l1sram_proc_read;
606         return 0;
607 }
608 late_initcall(l1sram_proc_init);
609 #endif