blob: 49b2ff2c8b74bee191df47682bcbae227b5beac6 [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
Robin Getz96f10502009-09-24 14:11:24 +00002 * SRAM allocator for Blackfin on-chip memory
Bryan Wu1394f032007-05-06 14:50:22 -07003 *
Robin Getz96f10502009-09-24 14:11:24 +00004 * Copyright 2004-2009 Analog Devices Inc.
Bryan Wu1394f032007-05-06 14:50:22 -07005 *
Robin Getz96f10502009-09-24 14:11:24 +00006 * Licensed under the GPL-2 or later.
Bryan Wu1394f032007-05-06 14:50:22 -07007 */
8
Bryan Wu1394f032007-05-06 14:50:22 -07009#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/types.h>
12#include <linux/miscdevice.h>
13#include <linux/ioport.h>
14#include <linux/fcntl.h>
15#include <linux/init.h>
16#include <linux/poll.h>
17#include <linux/proc_fs.h>
18#include <linux/spinlock.h>
19#include <linux/rtc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Bryan Wu1394f032007-05-06 14:50:22 -070021#include <asm/blackfin.h>
Graf Yangdbc895f2009-01-07 23:14:39 +080022#include <asm/mem_map.h>
Bryan Wu1394f032007-05-06 14:50:22 -070023#include "blackfin_sram.h"
24
Bryan Wu1394f032007-05-06 14:50:22 -070025/* the data structure for L1 scratchpad and DATA SRAM */
Sonic Zhang5d481f42008-07-19 14:51:31 +080026struct sram_piece {
Bryan Wu1394f032007-05-06 14:50:22 -070027 void *paddr;
28 int size;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +080029 pid_t pid;
Sonic Zhang5d481f42008-07-19 14:51:31 +080030 struct sram_piece *next;
Bryan Wu1394f032007-05-06 14:50:22 -070031};
32
Mike Frysinger81c969a2009-07-01 15:42:13 +000033static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1sram_lock);
Graf Yang8f658732008-11-18 17:48:22 +080034static DEFINE_PER_CPU(struct sram_piece, free_l1_ssram_head);
35static DEFINE_PER_CPU(struct sram_piece, used_l1_ssram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070036
37#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +080038static DEFINE_PER_CPU(struct sram_piece, free_l1_data_A_sram_head);
39static DEFINE_PER_CPU(struct sram_piece, used_l1_data_A_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070040#endif
41
42#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +080043static DEFINE_PER_CPU(struct sram_piece, free_l1_data_B_sram_head);
44static DEFINE_PER_CPU(struct sram_piece, used_l1_data_B_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070045#endif
46
Mike Frysinger81c969a2009-07-01 15:42:13 +000047#if L1_DATA_A_LENGTH || L1_DATA_B_LENGTH
48static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_data_sram_lock);
49#endif
50
Bryan Wu1394f032007-05-06 14:50:22 -070051#if L1_CODE_LENGTH != 0
Mike Frysinger81c969a2009-07-01 15:42:13 +000052static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_inst_sram_lock);
Graf Yang8f658732008-11-18 17:48:22 +080053static DEFINE_PER_CPU(struct sram_piece, free_l1_inst_sram_head);
54static DEFINE_PER_CPU(struct sram_piece, used_l1_inst_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070055#endif
56
Mike Frysinger07aa7be2008-08-13 16:16:11 +080057#if L2_LENGTH != 0
Mike Frysinger81c969a2009-07-01 15:42:13 +000058static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp;
Sonic Zhang262c3822008-07-19 15:42:41 +080059static struct sram_piece free_l2_sram_head, used_l2_sram_head;
60#endif
61
Sonic Zhang5d481f42008-07-19 14:51:31 +080062static struct kmem_cache *sram_piece_cache;
Bryan Wu1394f032007-05-06 14:50:22 -070063
Sonic Zhang5d481f42008-07-19 14:51:31 +080064/* L1 Scratchpad SRAM initialization function */
65static void __init l1sram_init(void)
66{
Graf Yang8f658732008-11-18 17:48:22 +080067 unsigned int cpu;
Graf Yang89ecd502009-05-25 06:40:42 +000068 unsigned long reserve;
69
70#ifdef CONFIG_SMP
71 reserve = 0;
72#else
73 reserve = sizeof(struct l1_scratch_task_info);
74#endif
75
Graf Yang8f658732008-11-18 17:48:22 +080076 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
77 per_cpu(free_l1_ssram_head, cpu).next =
78 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
79 if (!per_cpu(free_l1_ssram_head, cpu).next) {
80 printk(KERN_INFO "Fail to initialize Scratchpad data SRAM.\n");
81 return;
82 }
83
Graf Yang89ecd502009-05-25 06:40:42 +000084 per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu) + reserve;
85 per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH - reserve;
Graf Yang8f658732008-11-18 17:48:22 +080086 per_cpu(free_l1_ssram_head, cpu).next->pid = 0;
87 per_cpu(free_l1_ssram_head, cpu).next->next = NULL;
88
89 per_cpu(used_l1_ssram_head, cpu).next = NULL;
90
91 /* mutex initialize */
92 spin_lock_init(&per_cpu(l1sram_lock, cpu));
93 printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
94 L1_SCRATCH_LENGTH >> 10);
Sonic Zhang5d481f42008-07-19 14:51:31 +080095 }
Bryan Wu1394f032007-05-06 14:50:22 -070096}
97
Sonic Zhang5d481f42008-07-19 14:51:31 +080098static void __init l1_data_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -070099{
Mike Frysinger0b82e272008-11-18 17:48:22 +0800100#if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800101 unsigned int cpu;
Mike Frysinger0b82e272008-11-18 17:48:22 +0800102#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700103#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800104 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
105 per_cpu(free_l1_data_A_sram_head, cpu).next =
106 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
107 if (!per_cpu(free_l1_data_A_sram_head, cpu).next) {
108 printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n");
109 return;
110 }
111
112 per_cpu(free_l1_data_A_sram_head, cpu).next->paddr =
113 (void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1);
114 per_cpu(free_l1_data_A_sram_head, cpu).next->size =
115 L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
116 per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0;
117 per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL;
118
119 per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;
120
121 printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n",
122 L1_DATA_A_LENGTH >> 10,
123 per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800124 }
Bryan Wu1394f032007-05-06 14:50:22 -0700125#endif
126#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800127 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
128 per_cpu(free_l1_data_B_sram_head, cpu).next =
129 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
130 if (!per_cpu(free_l1_data_B_sram_head, cpu).next) {
131 printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n");
132 return;
133 }
134
135 per_cpu(free_l1_data_B_sram_head, cpu).next->paddr =
136 (void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1);
137 per_cpu(free_l1_data_B_sram_head, cpu).next->size =
138 L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
139 per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0;
140 per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL;
141
142 per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;
143
144 printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n",
145 L1_DATA_B_LENGTH >> 10,
146 per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10);
147 /* mutex initialize */
Sonic Zhang5d481f42008-07-19 14:51:31 +0800148 }
Bryan Wu1394f032007-05-06 14:50:22 -0700149#endif
150
Graf Yang8f658732008-11-18 17:48:22 +0800151#if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
152 for (cpu = 0; cpu < num_possible_cpus(); ++cpu)
153 spin_lock_init(&per_cpu(l1_data_sram_lock, cpu));
154#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700155}
156
Sonic Zhang5d481f42008-07-19 14:51:31 +0800157static void __init l1_inst_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700158{
159#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800160 unsigned int cpu;
161 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
162 per_cpu(free_l1_inst_sram_head, cpu).next =
163 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
164 if (!per_cpu(free_l1_inst_sram_head, cpu).next) {
165 printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n");
166 return;
167 }
168
169 per_cpu(free_l1_inst_sram_head, cpu).next->paddr =
170 (void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1);
171 per_cpu(free_l1_inst_sram_head, cpu).next->size =
172 L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
173 per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0;
174 per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL;
175
176 per_cpu(used_l1_inst_sram_head, cpu).next = NULL;
177
178 printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n",
179 L1_CODE_LENGTH >> 10,
180 per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10);
181
182 /* mutex initialize */
183 spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
Sonic Zhang5d481f42008-07-19 14:51:31 +0800184 }
Bryan Wu1394f032007-05-06 14:50:22 -0700185#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700186}
187
Sonic Zhang262c3822008-07-19 15:42:41 +0800188static void __init l2_sram_init(void)
189{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800190#if L2_LENGTH != 0
Sonic Zhang262c3822008-07-19 15:42:41 +0800191 free_l2_sram_head.next =
192 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
193 if (!free_l2_sram_head.next) {
Graf Yang8f658732008-11-18 17:48:22 +0800194 printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
Sonic Zhang262c3822008-07-19 15:42:41 +0800195 return;
196 }
197
Jie Zhangb2c2f302008-10-28 15:57:49 +0800198 free_l2_sram_head.next->paddr =
199 (void *)L2_START + (_ebss_l2 - _stext_l2);
200 free_l2_sram_head.next->size =
201 L2_LENGTH - (_ebss_l2 - _stext_l2);
Sonic Zhang262c3822008-07-19 15:42:41 +0800202 free_l2_sram_head.next->pid = 0;
203 free_l2_sram_head.next->next = NULL;
204
205 used_l2_sram_head.next = NULL;
206
207 printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n",
208 L2_LENGTH >> 10,
209 free_l2_sram_head.next->size >> 10);
Sonic Zhang262c3822008-07-19 15:42:41 +0800210
211 /* mutex initialize */
212 spin_lock_init(&l2_sram_lock);
Mike Frysinger81c969a2009-07-01 15:42:13 +0000213#endif
Sonic Zhang262c3822008-07-19 15:42:41 +0800214}
Graf Yang8f658732008-11-18 17:48:22 +0800215
Graf Yangc72aa072009-05-25 04:44:00 +0000216static int __init bfin_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700217{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800218 sram_piece_cache = kmem_cache_create("sram_piece_cache",
219 sizeof(struct sram_piece),
220 0, SLAB_PANIC, NULL);
Bryan Wu1394f032007-05-06 14:50:22 -0700221
Sonic Zhang5d481f42008-07-19 14:51:31 +0800222 l1sram_init();
223 l1_data_sram_init();
224 l1_inst_sram_init();
Sonic Zhang262c3822008-07-19 15:42:41 +0800225 l2_sram_init();
Graf Yangc72aa072009-05-25 04:44:00 +0000226
227 return 0;
Sonic Zhang5d481f42008-07-19 14:51:31 +0800228}
Graf Yangc72aa072009-05-25 04:44:00 +0000229pure_initcall(bfin_sram_init);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800230
Sonic Zhang262c3822008-07-19 15:42:41 +0800231/* SRAM allocate function */
232static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800233 struct sram_piece *pused_head)
234{
235 struct sram_piece *pslot, *plast, *pavail;
236
237 if (size <= 0 || !pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700238 return NULL;
239
240 /* Align the size */
241 size = (size + 3) & ~3;
242
Sonic Zhang5d481f42008-07-19 14:51:31 +0800243 pslot = pfree_head->next;
244 plast = pfree_head;
245
246 /* search an available piece slot */
247 while (pslot != NULL && size > pslot->size) {
248 plast = pslot;
249 pslot = pslot->next;
Bryan Wu1394f032007-05-06 14:50:22 -0700250 }
Sonic Zhang5d481f42008-07-19 14:51:31 +0800251
252 if (!pslot)
Bryan Wu1394f032007-05-06 14:50:22 -0700253 return NULL;
254
Sonic Zhang5d481f42008-07-19 14:51:31 +0800255 if (pslot->size == size) {
256 plast->next = pslot->next;
257 pavail = pslot;
258 } else {
259 pavail = kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
260
261 if (!pavail)
262 return NULL;
263
264 pavail->paddr = pslot->paddr;
265 pavail->size = size;
266 pslot->paddr += size;
267 pslot->size -= size;
Bryan Wu1394f032007-05-06 14:50:22 -0700268 }
269
Sonic Zhang5d481f42008-07-19 14:51:31 +0800270 pavail->pid = current->pid;
271
272 pslot = pused_head->next;
273 plast = pused_head;
274
275 /* insert new piece into used piece list !!! */
276 while (pslot != NULL && pavail->paddr < pslot->paddr) {
277 plast = pslot;
278 pslot = pslot->next;
279 }
280
281 pavail->next = pslot;
282 plast->next = pavail;
283
284 return pavail->paddr;
Bryan Wu1394f032007-05-06 14:50:22 -0700285}
286
287/* Allocate the largest available block. */
Sonic Zhang262c3822008-07-19 15:42:41 +0800288static void *_sram_alloc_max(struct sram_piece *pfree_head,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800289 struct sram_piece *pused_head,
Bryan Wu1394f032007-05-06 14:50:22 -0700290 unsigned long *psize)
291{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800292 struct sram_piece *pslot, *pmax;
Bryan Wu1394f032007-05-06 14:50:22 -0700293
Sonic Zhang5d481f42008-07-19 14:51:31 +0800294 if (!pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700295 return NULL;
Bryan Wu1394f032007-05-06 14:50:22 -0700296
Sonic Zhang5d481f42008-07-19 14:51:31 +0800297 pmax = pslot = pfree_head->next;
298
299 /* search an available piece slot */
300 while (pslot != NULL) {
301 if (pslot->size > pmax->size)
302 pmax = pslot;
303 pslot = pslot->next;
304 }
305
306 if (!pmax)
307 return NULL;
308
309 *psize = pmax->size;
310
Sonic Zhang262c3822008-07-19 15:42:41 +0800311 return _sram_alloc(*psize, pfree_head, pused_head);
Bryan Wu1394f032007-05-06 14:50:22 -0700312}
313
Sonic Zhang262c3822008-07-19 15:42:41 +0800314/* SRAM free function */
315static int _sram_free(const void *addr,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800316 struct sram_piece *pfree_head,
317 struct sram_piece *pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700318{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800319 struct sram_piece *pslot, *plast, *pavail;
Bryan Wu1394f032007-05-06 14:50:22 -0700320
Sonic Zhang5d481f42008-07-19 14:51:31 +0800321 if (!pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700322 return -1;
323
Sonic Zhang5d481f42008-07-19 14:51:31 +0800324 /* search the relevant memory slot */
325 pslot = pused_head->next;
326 plast = pused_head;
Bryan Wu1394f032007-05-06 14:50:22 -0700327
Sonic Zhang5d481f42008-07-19 14:51:31 +0800328 /* search an available piece slot */
329 while (pslot != NULL && pslot->paddr != addr) {
330 plast = pslot;
331 pslot = pslot->next;
Bryan Wu1394f032007-05-06 14:50:22 -0700332 }
333
Sonic Zhang5d481f42008-07-19 14:51:31 +0800334 if (!pslot)
335 return -1;
336
337 plast->next = pslot->next;
338 pavail = pslot;
339 pavail->pid = 0;
340
341 /* insert free pieces back to the free list */
342 pslot = pfree_head->next;
343 plast = pfree_head;
344
345 while (pslot != NULL && addr > pslot->paddr) {
346 plast = pslot;
347 pslot = pslot->next;
348 }
349
350 if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
351 plast->size += pavail->size;
352 kmem_cache_free(sram_piece_cache, pavail);
353 } else {
Sonic Zhang225f7e12008-08-25 18:00:45 +0800354 pavail->next = plast->next;
Sonic Zhang5d481f42008-07-19 14:51:31 +0800355 plast->next = pavail;
356 plast = pavail;
357 }
358
359 if (pslot && plast->paddr + plast->size == pslot->paddr) {
360 plast->size += pslot->size;
361 plast->next = pslot->next;
362 kmem_cache_free(sram_piece_cache, pslot);
Bryan Wu1394f032007-05-06 14:50:22 -0700363 }
364
365 return 0;
366}
367
368int sram_free(const void *addr)
369{
Robin Getz5e953202008-10-08 17:22:49 +0800370
Bryan Wu1394f032007-05-06 14:50:22 -0700371#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800372 if (addr >= (void *)get_l1_code_start()
373 && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700374 return l1_inst_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800375 else
Bryan Wu1394f032007-05-06 14:50:22 -0700376#endif
377#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800378 if (addr >= (void *)get_l1_data_a_start()
379 && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700380 return l1_data_A_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800381 else
Bryan Wu1394f032007-05-06 14:50:22 -0700382#endif
383#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800384 if (addr >= (void *)get_l1_data_b_start()
385 && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700386 return l1_data_B_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800387 else
Bryan Wu1394f032007-05-06 14:50:22 -0700388#endif
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800389#if L2_LENGTH != 0
Robin Getz5e953202008-10-08 17:22:49 +0800390 if (addr >= (void *)L2_START
Sonic Zhang262c3822008-07-19 15:42:41 +0800391 && addr < (void *)(L2_START + L2_LENGTH))
392 return l2_sram_free(addr);
Bryan Wu1394f032007-05-06 14:50:22 -0700393 else
Robin Getz5e953202008-10-08 17:22:49 +0800394#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700395 return -1;
396}
397EXPORT_SYMBOL(sram_free);
398
399void *l1_data_A_sram_alloc(size_t size)
400{
Mike Frysinger81c969a2009-07-01 15:42:13 +0000401#if L1_DATA_A_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800402 unsigned long flags;
Mike Frysinger81c969a2009-07-01 15:42:13 +0000403 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800404 unsigned int cpu;
Bryan Wu1394f032007-05-06 14:50:22 -0700405
Yi Li54536c52009-12-30 04:04:07 +0000406 cpu = smp_processor_id();
Bryan Wu1394f032007-05-06 14:50:22 -0700407 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800408 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700409
Graf Yang8f658732008-11-18 17:48:22 +0800410 addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
411 &per_cpu(used_l1_data_A_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700412
413 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800414 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700415
416 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
417 (long unsigned int)addr, size);
418
419 return addr;
Mike Frysinger81c969a2009-07-01 15:42:13 +0000420#else
421 return NULL;
422#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700423}
424EXPORT_SYMBOL(l1_data_A_sram_alloc);
425
426int l1_data_A_sram_free(const void *addr)
427{
Mike Frysinger81c969a2009-07-01 15:42:13 +0000428#if L1_DATA_A_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800429 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700430 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800431 unsigned int cpu;
Bryan Wu1394f032007-05-06 14:50:22 -0700432
Yi Li54536c52009-12-30 04:04:07 +0000433 cpu = smp_processor_id();
Bryan Wu1394f032007-05-06 14:50:22 -0700434 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800435 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700436
Graf Yang8f658732008-11-18 17:48:22 +0800437 ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu),
438 &per_cpu(used_l1_data_A_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700439
440 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800441 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700442
443 return ret;
Mike Frysinger81c969a2009-07-01 15:42:13 +0000444#else
445 return -1;
446#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700447}
448EXPORT_SYMBOL(l1_data_A_sram_free);
449
450void *l1_data_B_sram_alloc(size_t size)
451{
452#if L1_DATA_B_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800453 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700454 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800455 unsigned int cpu;
456
Yi Li54536c52009-12-30 04:04:07 +0000457 cpu = smp_processor_id();
Graf Yang8f658732008-11-18 17:48:22 +0800458 /* add mutex operation */
459 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
460
461 addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
462 &per_cpu(used_l1_data_B_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700463
464 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800465 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700466
467 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
468 (long unsigned int)addr, size);
469
470 return addr;
471#else
472 return NULL;
473#endif
474}
475EXPORT_SYMBOL(l1_data_B_sram_alloc);
476
477int l1_data_B_sram_free(const void *addr)
478{
479#if L1_DATA_B_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800480 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700481 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800482 unsigned int cpu;
483
Yi Li54536c52009-12-30 04:04:07 +0000484 cpu = smp_processor_id();
Graf Yang8f658732008-11-18 17:48:22 +0800485 /* add mutex operation */
486 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
487
488 ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu),
489 &per_cpu(used_l1_data_B_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700490
491 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800492 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700493
494 return ret;
495#else
496 return -1;
497#endif
498}
499EXPORT_SYMBOL(l1_data_B_sram_free);
500
501void *l1_data_sram_alloc(size_t size)
502{
503 void *addr = l1_data_A_sram_alloc(size);
504
505 if (!addr)
506 addr = l1_data_B_sram_alloc(size);
507
508 return addr;
509}
510EXPORT_SYMBOL(l1_data_sram_alloc);
511
512void *l1_data_sram_zalloc(size_t size)
513{
514 void *addr = l1_data_sram_alloc(size);
515
516 if (addr)
517 memset(addr, 0x00, size);
518
519 return addr;
520}
521EXPORT_SYMBOL(l1_data_sram_zalloc);
522
523int l1_data_sram_free(const void *addr)
524{
525 int ret;
526 ret = l1_data_A_sram_free(addr);
527 if (ret == -1)
528 ret = l1_data_B_sram_free(addr);
529 return ret;
530}
531EXPORT_SYMBOL(l1_data_sram_free);
532
533void *l1_inst_sram_alloc(size_t size)
534{
Meihui Fanc5b50df2008-04-23 08:55:26 +0800535#if L1_CODE_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800536 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700537 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800538 unsigned int cpu;
539
Yi Li54536c52009-12-30 04:04:07 +0000540 cpu = smp_processor_id();
Graf Yang8f658732008-11-18 17:48:22 +0800541 /* add mutex operation */
542 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
543
544 addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
545 &per_cpu(used_l1_inst_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700546
547 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800548 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700549
550 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
551 (long unsigned int)addr, size);
552
553 return addr;
554#else
555 return NULL;
556#endif
557}
558EXPORT_SYMBOL(l1_inst_sram_alloc);
559
560int l1_inst_sram_free(const void *addr)
561{
562#if L1_CODE_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800563 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700564 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800565 unsigned int cpu;
566
Yi Li54536c52009-12-30 04:04:07 +0000567 cpu = smp_processor_id();
Graf Yang8f658732008-11-18 17:48:22 +0800568 /* add mutex operation */
569 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
570
571 ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
572 &per_cpu(used_l1_inst_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700573
574 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800575 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700576
577 return ret;
578#else
579 return -1;
580#endif
581}
582EXPORT_SYMBOL(l1_inst_sram_free);
583
584/* L1 Scratchpad memory allocate function */
585void *l1sram_alloc(size_t size)
586{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800587 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700588 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800589 unsigned int cpu;
590
Yi Li54536c52009-12-30 04:04:07 +0000591 cpu = smp_processor_id();
Graf Yang8f658732008-11-18 17:48:22 +0800592 /* add mutex operation */
593 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
594
595 addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu),
596 &per_cpu(used_l1_ssram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700597
598 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800599 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700600
601 return addr;
602}
603
604/* L1 Scratchpad memory allocate function */
605void *l1sram_alloc_max(size_t *psize)
606{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800607 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700608 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800609 unsigned int cpu;
610
Yi Li54536c52009-12-30 04:04:07 +0000611 cpu = smp_processor_id();
Graf Yang8f658732008-11-18 17:48:22 +0800612 /* add mutex operation */
613 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
614
615 addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
616 &per_cpu(used_l1_ssram_head, cpu), psize);
Bryan Wu1394f032007-05-06 14:50:22 -0700617
618 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800619 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700620
621 return addr;
622}
623
624/* L1 Scratchpad memory free function */
625int l1sram_free(const void *addr)
626{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800627 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700628 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800629 unsigned int cpu;
630
Yi Li54536c52009-12-30 04:04:07 +0000631 cpu = smp_processor_id();
Graf Yang8f658732008-11-18 17:48:22 +0800632 /* add mutex operation */
633 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
634
635 ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
636 &per_cpu(used_l1_ssram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700637
638 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800639 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700640
641 return ret;
642}
643
Sonic Zhang262c3822008-07-19 15:42:41 +0800644void *l2_sram_alloc(size_t size)
645{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800646#if L2_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800647 unsigned long flags;
Sonic Zhang262c3822008-07-19 15:42:41 +0800648 void *addr;
649
650 /* add mutex operation */
651 spin_lock_irqsave(&l2_sram_lock, flags);
652
653 addr = _sram_alloc(size, &free_l2_sram_head,
654 &used_l2_sram_head);
655
656 /* add mutex operation */
657 spin_unlock_irqrestore(&l2_sram_lock, flags);
658
659 pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
660 (long unsigned int)addr, size);
661
662 return addr;
663#else
664 return NULL;
665#endif
666}
667EXPORT_SYMBOL(l2_sram_alloc);
668
669void *l2_sram_zalloc(size_t size)
670{
671 void *addr = l2_sram_alloc(size);
672
673 if (addr)
674 memset(addr, 0x00, size);
675
676 return addr;
677}
678EXPORT_SYMBOL(l2_sram_zalloc);
679
680int l2_sram_free(const void *addr)
681{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800682#if L2_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800683 unsigned long flags;
Sonic Zhang262c3822008-07-19 15:42:41 +0800684 int ret;
685
686 /* add mutex operation */
687 spin_lock_irqsave(&l2_sram_lock, flags);
688
689 ret = _sram_free(addr, &free_l2_sram_head,
690 &used_l2_sram_head);
691
692 /* add mutex operation */
693 spin_unlock_irqrestore(&l2_sram_lock, flags);
694
695 return ret;
696#else
697 return -1;
698#endif
699}
700EXPORT_SYMBOL(l2_sram_free);
701
Bryan Wu1394f032007-05-06 14:50:22 -0700702int sram_free_with_lsl(const void *addr)
703{
704 struct sram_list_struct *lsl, **tmp;
705 struct mm_struct *mm = current->mm;
706
707 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
708 if ((*tmp)->addr == addr)
709 goto found;
710 return -1;
711found:
712 lsl = *tmp;
713 sram_free(addr);
714 *tmp = lsl->next;
715 kfree(lsl);
716
717 return 0;
718}
719EXPORT_SYMBOL(sram_free_with_lsl);
720
Mike Frysingerf1db88d2009-06-02 08:46:35 +0000721/* Allocate memory and keep in L1 SRAM List (lsl) so that the resources are
722 * tracked. These are designed for userspace so that when a process exits,
723 * we can safely reap their resources.
724 */
Bryan Wu1394f032007-05-06 14:50:22 -0700725void *sram_alloc_with_lsl(size_t size, unsigned long flags)
726{
727 void *addr = NULL;
728 struct sram_list_struct *lsl = NULL;
729 struct mm_struct *mm = current->mm;
730
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700731 lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
Bryan Wu1394f032007-05-06 14:50:22 -0700732 if (!lsl)
733 return NULL;
Bryan Wu1394f032007-05-06 14:50:22 -0700734
735 if (flags & L1_INST_SRAM)
736 addr = l1_inst_sram_alloc(size);
737
738 if (addr == NULL && (flags & L1_DATA_A_SRAM))
739 addr = l1_data_A_sram_alloc(size);
740
741 if (addr == NULL && (flags & L1_DATA_B_SRAM))
742 addr = l1_data_B_sram_alloc(size);
743
Sonic Zhang262c3822008-07-19 15:42:41 +0800744 if (addr == NULL && (flags & L2_SRAM))
745 addr = l2_sram_alloc(size);
746
Bryan Wu1394f032007-05-06 14:50:22 -0700747 if (addr == NULL) {
748 kfree(lsl);
749 return NULL;
750 }
751 lsl->addr = addr;
752 lsl->length = size;
753 lsl->next = mm->context.sram_list;
754 mm->context.sram_list = lsl;
755 return addr;
756}
757EXPORT_SYMBOL(sram_alloc_with_lsl);
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800758
759#ifdef CONFIG_PROC_FS
760/* Once we get a real allocator, we'll throw all of this away.
761 * Until then, we need some sort of visibility into the L1 alloc.
762 */
Mike Frysinger260d5d32008-07-14 16:34:05 +0800763/* Need to keep line of output the same. Currently, that is 44 bytes
764 * (including newline).
765 */
Sonic Zhang262c3822008-07-19 15:42:41 +0800766static int _sram_proc_read(char *buf, int *len, int count, const char *desc,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800767 struct sram_piece *pfree_head,
768 struct sram_piece *pused_head)
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800769{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800770 struct sram_piece *pslot;
771
772 if (!pfree_head || !pused_head)
773 return -1;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800774
Sonic Zhang262c3822008-07-19 15:42:41 +0800775 *len += sprintf(&buf[*len], "--- SRAM %-14s Size PID State \n", desc);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800776
777 /* search the relevant memory slot */
778 pslot = pused_head->next;
779
780 while (pslot != NULL) {
Sonic Zhang262c3822008-07-19 15:42:41 +0800781 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
Sonic Zhang5d481f42008-07-19 14:51:31 +0800782 pslot->paddr, pslot->paddr + pslot->size,
783 pslot->size, pslot->pid, "ALLOCATED");
784
785 pslot = pslot->next;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800786 }
Sonic Zhang5d481f42008-07-19 14:51:31 +0800787
788 pslot = pfree_head->next;
789
790 while (pslot != NULL) {
Sonic Zhang262c3822008-07-19 15:42:41 +0800791 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
Sonic Zhang5d481f42008-07-19 14:51:31 +0800792 pslot->paddr, pslot->paddr + pslot->size,
793 pslot->size, pslot->pid, "FREE");
794
795 pslot = pslot->next;
796 }
797
798 return 0;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800799}
Sonic Zhang262c3822008-07-19 15:42:41 +0800800static int sram_proc_read(char *buf, char **start, off_t offset, int count,
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800801 int *eof, void *data)
802{
803 int len = 0;
Graf Yang8f658732008-11-18 17:48:22 +0800804 unsigned int cpu;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800805
Graf Yang8f658732008-11-18 17:48:22 +0800806 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
807 if (_sram_proc_read(buf, &len, count, "Scratchpad",
808 &per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu)))
809 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800810#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800811 if (_sram_proc_read(buf, &len, count, "L1 Data A",
812 &per_cpu(free_l1_data_A_sram_head, cpu),
813 &per_cpu(used_l1_data_A_sram_head, cpu)))
814 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800815#endif
816#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800817 if (_sram_proc_read(buf, &len, count, "L1 Data B",
818 &per_cpu(free_l1_data_B_sram_head, cpu),
819 &per_cpu(used_l1_data_B_sram_head, cpu)))
820 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800821#endif
822#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800823 if (_sram_proc_read(buf, &len, count, "L1 Instruction",
824 &per_cpu(free_l1_inst_sram_head, cpu),
825 &per_cpu(used_l1_inst_sram_head, cpu)))
826 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800827#endif
Graf Yang8f658732008-11-18 17:48:22 +0800828 }
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800829#if L2_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800830 if (_sram_proc_read(buf, &len, count, "L2", &free_l2_sram_head,
831 &used_l2_sram_head))
Sonic Zhang262c3822008-07-19 15:42:41 +0800832 goto not_done;
833#endif
Mike Frysinger260d5d32008-07-14 16:34:05 +0800834 *eof = 1;
835 not_done:
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800836 return len;
837}
838
Sonic Zhang262c3822008-07-19 15:42:41 +0800839static int __init sram_proc_init(void)
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800840{
841 struct proc_dir_entry *ptr;
842 ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
843 if (!ptr) {
844 printk(KERN_WARNING "unable to create /proc/sram\n");
845 return -1;
846 }
Sonic Zhang262c3822008-07-19 15:42:41 +0800847 ptr->read_proc = sram_proc_read;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800848 return 0;
849}
Sonic Zhang262c3822008-07-19 15:42:41 +0800850late_initcall(sram_proc_init);
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800851#endif