blob: b99ea883cd22bdafa38377d1e8d4d66c9a55da93 [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));
Mike Frysinger1f83b8f2007-07-12 22:58:21 +080090 l1_ssram[0].paddr = (void *)L1_SCRATCH_START;
Bryan Wu1394f032007-05-06 14:50:22 -070091 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));
Mike Frysinger43a31882007-06-14 13:33:37 +0800112 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);
Bryan Wu1394f032007-05-06 14:50:22 -0700115 l1_data_B_sram[0].flag = SRAM_SLT_FREE;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800116
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);
Bryan Wu1394f032007-05-06 14:50:22 -0700119#endif
120
121 /* mutex initialize */
122 spin_lock_init(&l1_data_sram_lock);
123}
124
Mike Frysinger321f6e02007-06-11 15:31:30 +0800125void __init l1_inst_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700126{
127#if L1_CODE_LENGTH != 0
Bryan Wu1394f032007-05-06 14:50:22 -0700128 memset(&l1_inst_sram, 0x00, sizeof(l1_inst_sram));
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800129 l1_inst_sram[0].paddr = (void *)L1_CODE_START + (_etext_l1 - _stext_l1);
Bryan Wu1394f032007-05-06 14:50:22 -0700130 l1_inst_sram[0].size = L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
131 l1_inst_sram[0].flag = SRAM_SLT_FREE;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800132
133 printk(KERN_INFO "Blackfin Instruction SRAM: %d KB (%d KB free)\n",
134 L1_CODE_LENGTH >> 10, l1_inst_sram[0].size >> 10);
Bryan Wu1394f032007-05-06 14:50:22 -0700135#endif
136
137 /* mutex initialize */
138 spin_lock_init(&l1_inst_sram_lock);
139}
140
141/* L1 memory allocate function */
142static 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 !!! */
Simon Arlottd2d50aa2007-06-11 15:31:30 +0800154 /* search an available memory slot */
Bryan Wu1394f032007-05-06 14:50:22 -0700155 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;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800160 pfree[i].pid = current->pid;
Bryan Wu1394f032007-05-06 14:50:22 -0700161 index = i;
162 break;
163 }
164 }
165 if (i >= count)
166 return NULL;
167
Simon Arlottd2d50aa2007-06-11 15:31:30 +0800168 /* updated the NULL memory slot !!! */
Bryan Wu1394f032007-05-06 14:50:22 -0700169 if (pfree[i].size > size) {
170 for (i = 0; i < count; i++) {
171 if (pfree[i].flag == SRAM_SLT_NULL) {
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800172 pfree[i].pid = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700173 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. */
186static 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
Simon Arlottd2d50aa2007-06-11 15:31:30 +0800193 /* search an available memory slot */
Bryan Wu1394f032007-05-06 14:50:22 -0700194 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
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800205 pfree[index].pid = current->pid;
Bryan Wu1394f032007-05-06 14:50:22 -0700206 pfree[index].flag = SRAM_SLT_ALLOCATED;
207 return addr;
208}
209
210/* L1 memory free function */
211static int _l1_sram_free(const void *addr,
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800212 struct l1_sram_piece *pfree,
213 int count)
Bryan Wu1394f032007-05-06 14:50:22 -0700214{
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
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800231 pfree[index].pid = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700232 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)) {
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800238 pfree[i].pid = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700239 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
259int 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}
280EXPORT_SYMBOL(sram_free);
281
282void *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}
302EXPORT_SYMBOL(l1_data_A_sram_alloc);
303
304int 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}
324EXPORT_SYMBOL(l1_data_A_sram_free);
325
326void *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}
348EXPORT_SYMBOL(l1_data_B_sram_alloc);
349
350int 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}
369EXPORT_SYMBOL(l1_data_B_sram_free);
370
371void *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}
380EXPORT_SYMBOL(l1_data_sram_alloc);
381
382void *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}
391EXPORT_SYMBOL(l1_data_sram_zalloc);
392
393int 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}
401EXPORT_SYMBOL(l1_data_sram_free);
402
403void *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}
425EXPORT_SYMBOL(l1_inst_sram_alloc);
426
427int 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}
446EXPORT_SYMBOL(l1_inst_sram_free);
447
448/* L1 Scratchpad memory allocate function */
449void *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 */
466void *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 */
483int 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
499int 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;
508found:
509 lsl = *tmp;
510 sram_free(addr);
511 *tmp = lsl->next;
512 kfree(lsl);
513
514 return 0;
515}
516EXPORT_SYMBOL(sram_free_with_lsl);
517
518void *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
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700524 lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
Bryan Wu1394f032007-05-06 14:50:22 -0700525 if (!lsl)
526 return NULL;
Bryan Wu1394f032007-05-06 14:50:22 -0700527
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