blob: dbb37b8854f5e5cccf5308dc73ac764a956f28d2 [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
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:
Mike Frysinger321f6e02007-06-11 15:31:30 +080010 * Copyright 2004-2007 Analog Devices Inc.
Bryan Wu1394f032007-05-06 14:50:22 -070011 *
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
45spinlock_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 */
62struct l1_sram_piece {
63 void *paddr;
64 int size;
65 int flag;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +080066 pid_t pid;
Bryan Wu1394f032007-05-06 14:50:22 -070067};
68
69static struct l1_sram_piece l1_ssram[CONFIG_L1_MAX_PIECE];
70
71#if L1_DATA_A_LENGTH != 0
72static struct l1_sram_piece l1_data_A_sram[CONFIG_L1_MAX_PIECE];
73#endif
74
75#if L1_DATA_B_LENGTH != 0
76static struct l1_sram_piece l1_data_B_sram[CONFIG_L1_MAX_PIECE];
77#endif
78
79#if L1_CODE_LENGTH != 0
80static struct l1_sram_piece l1_inst_sram[CONFIG_L1_MAX_PIECE];
81#endif
82
83/* L1 Scratchpad SRAM initialization function */
Mike Frysinger321f6e02007-06-11 15:31:30 +080084void __init l1sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -070085{
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
Mike Frysinger321f6e02007-06-11 15:31:30 +080098void __init l1_data_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -070099{
100#if L1_DATA_A_LENGTH != 0
Bryan Wu1394f032007-05-06 14:50:22 -0700101 memset(&l1_data_A_sram, 0x00, sizeof(l1_data_A_sram));
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800102 l1_data_A_sram[0].paddr = (void *)L1_DATA_A_START +
103 (_ebss_l1 - _sdata_l1);
Bryan Wu1394f032007-05-06 14:50:22 -0700104 l1_data_A_sram[0].size = L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
105 l1_data_A_sram[0].flag = SRAM_SLT_FREE;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800106
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);
Bryan Wu1394f032007-05-06 14:50:22 -0700109#endif
110#if L1_DATA_B_LENGTH != 0
Bryan Wu1394f032007-05-06 14:50:22 -0700111 memset(&l1_data_B_sram, 0x00, sizeof(l1_data_B_sram));
112 l1_data_B_sram[0].paddr = (void*)L1_DATA_B_START;
113 l1_data_B_sram[0].size = L1_DATA_B_LENGTH;
114 l1_data_B_sram[0].flag = SRAM_SLT_FREE;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800115
116 printk(KERN_INFO "Blackfin Data B SRAM: %d KB (%d KB free)\n",
117 L1_DATA_B_LENGTH >> 10, l1_data_B_sram[0].size >> 10);
Bryan Wu1394f032007-05-06 14:50:22 -0700118#endif
119
120 /* mutex initialize */
121 spin_lock_init(&l1_data_sram_lock);
122}
123
Mike Frysinger321f6e02007-06-11 15:31:30 +0800124void __init l1_inst_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700125{
126#if L1_CODE_LENGTH != 0
Bryan Wu1394f032007-05-06 14:50:22 -0700127 memset(&l1_inst_sram, 0x00, sizeof(l1_inst_sram));
128 l1_inst_sram[0].paddr = (void*)L1_CODE_START + (_etext_l1 - _stext_l1);
129 l1_inst_sram[0].size = L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
130 l1_inst_sram[0].flag = SRAM_SLT_FREE;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800131
132 printk(KERN_INFO "Blackfin Instruction SRAM: %d KB (%d KB free)\n",
133 L1_CODE_LENGTH >> 10, l1_inst_sram[0].size >> 10);
Bryan Wu1394f032007-05-06 14:50:22 -0700134#endif
135
136 /* mutex initialize */
137 spin_lock_init(&l1_inst_sram_lock);
138}
139
140/* L1 memory allocate function */
141static void *_l1_sram_alloc(size_t size, struct l1_sram_piece *pfree, int count)
142{
143 int i, index = 0;
144 void *addr = NULL;
145
146 if (size <= 0)
147 return NULL;
148
149 /* Align the size */
150 size = (size + 3) & ~3;
151
152 /* not use the good method to match the best slot !!! */
Simon Arlottd2d50aa2007-06-11 15:31:30 +0800153 /* search an available memory slot */
Bryan Wu1394f032007-05-06 14:50:22 -0700154 for (i = 0; i < count; i++) {
155 if ((pfree[i].flag == SRAM_SLT_FREE)
156 && (pfree[i].size >= size)) {
157 addr = pfree[i].paddr;
158 pfree[i].flag = SRAM_SLT_ALLOCATED;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800159 pfree[i].pid = current->pid;
Bryan Wu1394f032007-05-06 14:50:22 -0700160 index = i;
161 break;
162 }
163 }
164 if (i >= count)
165 return NULL;
166
Simon Arlottd2d50aa2007-06-11 15:31:30 +0800167 /* updated the NULL memory slot !!! */
Bryan Wu1394f032007-05-06 14:50:22 -0700168 if (pfree[i].size > size) {
169 for (i = 0; i < count; i++) {
170 if (pfree[i].flag == SRAM_SLT_NULL) {
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800171 pfree[i].pid = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700172 pfree[i].flag = SRAM_SLT_FREE;
173 pfree[i].paddr = addr + size;
174 pfree[i].size = pfree[index].size - size;
175 pfree[index].size = size;
176 break;
177 }
178 }
179 }
180
181 return addr;
182}
183
184/* Allocate the largest available block. */
185static void *_l1_sram_alloc_max(struct l1_sram_piece *pfree, int count,
186 unsigned long *psize)
187{
188 unsigned long best = 0;
189 int i, index = -1;
190 void *addr = NULL;
191
Simon Arlottd2d50aa2007-06-11 15:31:30 +0800192 /* search an available memory slot */
Bryan Wu1394f032007-05-06 14:50:22 -0700193 for (i = 0; i < count; i++) {
194 if (pfree[i].flag == SRAM_SLT_FREE && pfree[i].size > best) {
195 addr = pfree[i].paddr;
196 index = i;
197 best = pfree[i].size;
198 }
199 }
200 if (index < 0)
201 return NULL;
202 *psize = best;
203
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800204 pfree[index].pid = current->pid;
Bryan Wu1394f032007-05-06 14:50:22 -0700205 pfree[index].flag = SRAM_SLT_ALLOCATED;
206 return addr;
207}
208
209/* L1 memory free function */
210static int _l1_sram_free(const void *addr,
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800211 struct l1_sram_piece *pfree,
212 int count)
Bryan Wu1394f032007-05-06 14:50:22 -0700213{
214 int i, index = 0;
215
216 /* search the relevant memory slot */
217 for (i = 0; i < count; i++) {
218 if (pfree[i].paddr == addr) {
219 if (pfree[i].flag != SRAM_SLT_ALLOCATED) {
220 /* error log */
221 return -1;
222 }
223 index = i;
224 break;
225 }
226 }
227 if (i >= count)
228 return -1;
229
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800230 pfree[index].pid = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700231 pfree[index].flag = SRAM_SLT_FREE;
232
233 /* link the next address slot */
234 for (i = 0; i < count; i++) {
235 if (((pfree[index].paddr + pfree[index].size) == pfree[i].paddr)
236 && (pfree[i].flag == SRAM_SLT_FREE)) {
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800237 pfree[i].pid = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700238 pfree[i].flag = SRAM_SLT_NULL;
239 pfree[index].size += pfree[i].size;
240 pfree[index].flag = SRAM_SLT_FREE;
241 break;
242 }
243 }
244
245 /* link the last address slot */
246 for (i = 0; i < count; i++) {
247 if (((pfree[i].paddr + pfree[i].size) == pfree[index].paddr) &&
248 (pfree[i].flag == SRAM_SLT_FREE)) {
249 pfree[index].flag = SRAM_SLT_NULL;
250 pfree[i].size += pfree[index].size;
251 break;
252 }
253 }
254
255 return 0;
256}
257
258int sram_free(const void *addr)
259{
260 if (0) {}
261#if L1_CODE_LENGTH != 0
262 else if (addr >= (void *)L1_CODE_START
263 && addr < (void *)(L1_CODE_START + L1_CODE_LENGTH))
264 return l1_inst_sram_free(addr);
265#endif
266#if L1_DATA_A_LENGTH != 0
267 else if (addr >= (void *)L1_DATA_A_START
268 && addr < (void *)(L1_DATA_A_START + L1_DATA_A_LENGTH))
269 return l1_data_A_sram_free(addr);
270#endif
271#if L1_DATA_B_LENGTH != 0
272 else if (addr >= (void *)L1_DATA_B_START
273 && addr < (void *)(L1_DATA_B_START + L1_DATA_B_LENGTH))
274 return l1_data_B_sram_free(addr);
275#endif
276 else
277 return -1;
278}
279EXPORT_SYMBOL(sram_free);
280
281void *l1_data_A_sram_alloc(size_t size)
282{
283 unsigned flags;
284 void *addr = NULL;
285
286 /* add mutex operation */
287 spin_lock_irqsave(&l1_data_sram_lock, flags);
288
289#if L1_DATA_A_LENGTH != 0
290 addr = _l1_sram_alloc(size, l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
291#endif
292
293 /* add mutex operation */
294 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
295
296 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
297 (long unsigned int)addr, size);
298
299 return addr;
300}
301EXPORT_SYMBOL(l1_data_A_sram_alloc);
302
303int l1_data_A_sram_free(const void *addr)
304{
305 unsigned flags;
306 int ret;
307
308 /* add mutex operation */
309 spin_lock_irqsave(&l1_data_sram_lock, flags);
310
311#if L1_DATA_A_LENGTH != 0
312 ret = _l1_sram_free(addr,
313 l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
314#else
315 ret = -1;
316#endif
317
318 /* add mutex operation */
319 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
320
321 return ret;
322}
323EXPORT_SYMBOL(l1_data_A_sram_free);
324
325void *l1_data_B_sram_alloc(size_t size)
326{
327#if L1_DATA_B_LENGTH != 0
328 unsigned flags;
329 void *addr;
330
331 /* add mutex operation */
332 spin_lock_irqsave(&l1_data_sram_lock, flags);
333
334 addr = _l1_sram_alloc(size, l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
335
336 /* add mutex operation */
337 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
338
339 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
340 (long unsigned int)addr, size);
341
342 return addr;
343#else
344 return NULL;
345#endif
346}
347EXPORT_SYMBOL(l1_data_B_sram_alloc);
348
349int l1_data_B_sram_free(const void *addr)
350{
351#if L1_DATA_B_LENGTH != 0
352 unsigned flags;
353 int ret;
354
355 /* add mutex operation */
356 spin_lock_irqsave(&l1_data_sram_lock, flags);
357
358 ret = _l1_sram_free(addr, l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
359
360 /* add mutex operation */
361 spin_unlock_irqrestore(&l1_data_sram_lock, flags);
362
363 return ret;
364#else
365 return -1;
366#endif
367}
368EXPORT_SYMBOL(l1_data_B_sram_free);
369
370void *l1_data_sram_alloc(size_t size)
371{
372 void *addr = l1_data_A_sram_alloc(size);
373
374 if (!addr)
375 addr = l1_data_B_sram_alloc(size);
376
377 return addr;
378}
379EXPORT_SYMBOL(l1_data_sram_alloc);
380
381void *l1_data_sram_zalloc(size_t size)
382{
383 void *addr = l1_data_sram_alloc(size);
384
385 if (addr)
386 memset(addr, 0x00, size);
387
388 return addr;
389}
390EXPORT_SYMBOL(l1_data_sram_zalloc);
391
392int l1_data_sram_free(const void *addr)
393{
394 int ret;
395 ret = l1_data_A_sram_free(addr);
396 if (ret == -1)
397 ret = l1_data_B_sram_free(addr);
398 return ret;
399}
400EXPORT_SYMBOL(l1_data_sram_free);
401
402void *l1_inst_sram_alloc(size_t size)
403{
404#if L1_DATA_A_LENGTH != 0
405 unsigned flags;
406 void *addr;
407
408 /* add mutex operation */
409 spin_lock_irqsave(&l1_inst_sram_lock, flags);
410
411 addr = _l1_sram_alloc(size, l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
412
413 /* add mutex operation */
414 spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
415
416 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
417 (long unsigned int)addr, size);
418
419 return addr;
420#else
421 return NULL;
422#endif
423}
424EXPORT_SYMBOL(l1_inst_sram_alloc);
425
426int l1_inst_sram_free(const void *addr)
427{
428#if L1_CODE_LENGTH != 0
429 unsigned flags;
430 int ret;
431
432 /* add mutex operation */
433 spin_lock_irqsave(&l1_inst_sram_lock, flags);
434
435 ret = _l1_sram_free(addr, l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
436
437 /* add mutex operation */
438 spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
439
440 return ret;
441#else
442 return -1;
443#endif
444}
445EXPORT_SYMBOL(l1_inst_sram_free);
446
447/* L1 Scratchpad memory allocate function */
448void *l1sram_alloc(size_t size)
449{
450 unsigned flags;
451 void *addr;
452
453 /* add mutex operation */
454 spin_lock_irqsave(&l1sram_lock, flags);
455
456 addr = _l1_sram_alloc(size, l1_ssram, ARRAY_SIZE(l1_ssram));
457
458 /* add mutex operation */
459 spin_unlock_irqrestore(&l1sram_lock, flags);
460
461 return addr;
462}
463
464/* L1 Scratchpad memory allocate function */
465void *l1sram_alloc_max(size_t *psize)
466{
467 unsigned flags;
468 void *addr;
469
470 /* add mutex operation */
471 spin_lock_irqsave(&l1sram_lock, flags);
472
473 addr = _l1_sram_alloc_max(l1_ssram, ARRAY_SIZE(l1_ssram), psize);
474
475 /* add mutex operation */
476 spin_unlock_irqrestore(&l1sram_lock, flags);
477
478 return addr;
479}
480
481/* L1 Scratchpad memory free function */
482int l1sram_free(const void *addr)
483{
484 unsigned flags;
485 int ret;
486
487 /* add mutex operation */
488 spin_lock_irqsave(&l1sram_lock, flags);
489
490 ret = _l1_sram_free(addr, l1_ssram, ARRAY_SIZE(l1_ssram));
491
492 /* add mutex operation */
493 spin_unlock_irqrestore(&l1sram_lock, flags);
494
495 return ret;
496}
497
498int sram_free_with_lsl(const void *addr)
499{
500 struct sram_list_struct *lsl, **tmp;
501 struct mm_struct *mm = current->mm;
502
503 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
504 if ((*tmp)->addr == addr)
505 goto found;
506 return -1;
507found:
508 lsl = *tmp;
509 sram_free(addr);
510 *tmp = lsl->next;
511 kfree(lsl);
512
513 return 0;
514}
515EXPORT_SYMBOL(sram_free_with_lsl);
516
517void *sram_alloc_with_lsl(size_t size, unsigned long flags)
518{
519 void *addr = NULL;
520 struct sram_list_struct *lsl = NULL;
521 struct mm_struct *mm = current->mm;
522
523 lsl = kmalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
524 if (!lsl)
525 return NULL;
526 memset(lsl, 0, sizeof(*lsl));
527
528 if (flags & L1_INST_SRAM)
529 addr = l1_inst_sram_alloc(size);
530
531 if (addr == NULL && (flags & L1_DATA_A_SRAM))
532 addr = l1_data_A_sram_alloc(size);
533
534 if (addr == NULL && (flags & L1_DATA_B_SRAM))
535 addr = l1_data_B_sram_alloc(size);
536
537 if (addr == NULL) {
538 kfree(lsl);
539 return NULL;
540 }
541 lsl->addr = addr;
542 lsl->length = size;
543 lsl->next = mm->context.sram_list;
544 mm->context.sram_list = lsl;
545 return addr;
546}
547EXPORT_SYMBOL(sram_alloc_with_lsl);
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800548
549#ifdef CONFIG_PROC_FS
550/* Once we get a real allocator, we'll throw all of this away.
551 * Until then, we need some sort of visibility into the L1 alloc.
552 */
553static void _l1sram_proc_read(char *buf, int *len, const char *desc,
554 struct l1_sram_piece *pfree, const int array_size)
555{
556 int i;
557
558 *len += sprintf(&buf[*len], "--- L1 %-14s Size PID State\n", desc);
559 for (i = 0; i < array_size; ++i) {
560 const char *alloc_type;
561 switch (pfree[i].flag) {
562 case SRAM_SLT_NULL: alloc_type = "NULL"; break;
563 case SRAM_SLT_FREE: alloc_type = "FREE"; break;
564 case SRAM_SLT_ALLOCATED: alloc_type = "ALLOCATED"; break;
565 default: alloc_type = "????"; break;
566 }
567 *len += sprintf(&buf[*len], "%p-%p %8i %4i %s\n",
568 pfree[i].paddr, pfree[i].paddr + pfree[i].size,
569 pfree[i].size, pfree[i].pid, alloc_type);
570 }
571}
572static int l1sram_proc_read(char *buf, char **start, off_t offset, int count,
573 int *eof, void *data)
574{
575 int len = 0;
576
577 _l1sram_proc_read(buf, &len, "Scratchpad",
578 l1_ssram, ARRAY_SIZE(l1_ssram));
579#if L1_DATA_A_LENGTH != 0
580 _l1sram_proc_read(buf, &len, "Data A",
581 l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
582#endif
583#if L1_DATA_B_LENGTH != 0
584 _l1sram_proc_read(buf, &len, "Data B",
585 l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
586#endif
587#if L1_CODE_LENGTH != 0
588 _l1sram_proc_read(buf, &len, "Instruction",
589 l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
590#endif
591
592 return len;
593}
594
595static int __init l1sram_proc_init(void)
596{
597 struct proc_dir_entry *ptr;
598 ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
599 if (!ptr) {
600 printk(KERN_WARNING "unable to create /proc/sram\n");
601 return -1;
602 }
603 ptr->owner = THIS_MODULE;
604 ptr->read_proc = l1sram_proc_read;
605 return 0;
606}
607late_initcall(l1sram_proc_init);
608#endif