blob: f068c11ea98f5ca6c53568b4e7cf2dfc3a363a1f [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>
20#include <asm/blackfin.h>
Graf Yangdbc895f2009-01-07 23:14:39 +080021#include <asm/mem_map.h>
Bryan Wu1394f032007-05-06 14:50:22 -070022#include "blackfin_sram.h"
23
Bryan Wu1394f032007-05-06 14:50:22 -070024/* the data structure for L1 scratchpad and DATA SRAM */
Sonic Zhang5d481f42008-07-19 14:51:31 +080025struct sram_piece {
Bryan Wu1394f032007-05-06 14:50:22 -070026 void *paddr;
27 int size;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +080028 pid_t pid;
Sonic Zhang5d481f42008-07-19 14:51:31 +080029 struct sram_piece *next;
Bryan Wu1394f032007-05-06 14:50:22 -070030};
31
Mike Frysinger81c969a2009-07-01 15:42:13 +000032static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1sram_lock);
Graf Yang8f658732008-11-18 17:48:22 +080033static DEFINE_PER_CPU(struct sram_piece, free_l1_ssram_head);
34static DEFINE_PER_CPU(struct sram_piece, used_l1_ssram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070035
36#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +080037static DEFINE_PER_CPU(struct sram_piece, free_l1_data_A_sram_head);
38static DEFINE_PER_CPU(struct sram_piece, used_l1_data_A_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070039#endif
40
41#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +080042static DEFINE_PER_CPU(struct sram_piece, free_l1_data_B_sram_head);
43static DEFINE_PER_CPU(struct sram_piece, used_l1_data_B_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070044#endif
45
Mike Frysinger81c969a2009-07-01 15:42:13 +000046#if L1_DATA_A_LENGTH || L1_DATA_B_LENGTH
47static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_data_sram_lock);
48#endif
49
Bryan Wu1394f032007-05-06 14:50:22 -070050#if L1_CODE_LENGTH != 0
Mike Frysinger81c969a2009-07-01 15:42:13 +000051static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_inst_sram_lock);
Graf Yang8f658732008-11-18 17:48:22 +080052static DEFINE_PER_CPU(struct sram_piece, free_l1_inst_sram_head);
53static DEFINE_PER_CPU(struct sram_piece, used_l1_inst_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070054#endif
55
Mike Frysinger07aa7be2008-08-13 16:16:11 +080056#if L2_LENGTH != 0
Mike Frysinger81c969a2009-07-01 15:42:13 +000057static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp;
Sonic Zhang262c3822008-07-19 15:42:41 +080058static struct sram_piece free_l2_sram_head, used_l2_sram_head;
59#endif
60
Sonic Zhang5d481f42008-07-19 14:51:31 +080061static struct kmem_cache *sram_piece_cache;
Bryan Wu1394f032007-05-06 14:50:22 -070062
Sonic Zhang5d481f42008-07-19 14:51:31 +080063/* L1 Scratchpad SRAM initialization function */
64static void __init l1sram_init(void)
65{
Graf Yang8f658732008-11-18 17:48:22 +080066 unsigned int cpu;
Graf Yang89ecd502009-05-25 06:40:42 +000067 unsigned long reserve;
68
69#ifdef CONFIG_SMP
70 reserve = 0;
71#else
72 reserve = sizeof(struct l1_scratch_task_info);
73#endif
74
Graf Yang8f658732008-11-18 17:48:22 +080075 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
76 per_cpu(free_l1_ssram_head, cpu).next =
77 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
78 if (!per_cpu(free_l1_ssram_head, cpu).next) {
79 printk(KERN_INFO "Fail to initialize Scratchpad data SRAM.\n");
80 return;
81 }
82
Graf Yang89ecd502009-05-25 06:40:42 +000083 per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu) + reserve;
84 per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH - reserve;
Graf Yang8f658732008-11-18 17:48:22 +080085 per_cpu(free_l1_ssram_head, cpu).next->pid = 0;
86 per_cpu(free_l1_ssram_head, cpu).next->next = NULL;
87
88 per_cpu(used_l1_ssram_head, cpu).next = NULL;
89
90 /* mutex initialize */
91 spin_lock_init(&per_cpu(l1sram_lock, cpu));
92 printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
93 L1_SCRATCH_LENGTH >> 10);
Sonic Zhang5d481f42008-07-19 14:51:31 +080094 }
Bryan Wu1394f032007-05-06 14:50:22 -070095}
96
Sonic Zhang5d481f42008-07-19 14:51:31 +080097static void __init l1_data_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -070098{
Mike Frysinger0b82e272008-11-18 17:48:22 +080099#if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800100 unsigned int cpu;
Mike Frysinger0b82e272008-11-18 17:48:22 +0800101#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700102#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800103 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
104 per_cpu(free_l1_data_A_sram_head, cpu).next =
105 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
106 if (!per_cpu(free_l1_data_A_sram_head, cpu).next) {
107 printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n");
108 return;
109 }
110
111 per_cpu(free_l1_data_A_sram_head, cpu).next->paddr =
112 (void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1);
113 per_cpu(free_l1_data_A_sram_head, cpu).next->size =
114 L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
115 per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0;
116 per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL;
117
118 per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;
119
120 printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n",
121 L1_DATA_A_LENGTH >> 10,
122 per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800123 }
Bryan Wu1394f032007-05-06 14:50:22 -0700124#endif
125#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800126 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
127 per_cpu(free_l1_data_B_sram_head, cpu).next =
128 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
129 if (!per_cpu(free_l1_data_B_sram_head, cpu).next) {
130 printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n");
131 return;
132 }
133
134 per_cpu(free_l1_data_B_sram_head, cpu).next->paddr =
135 (void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1);
136 per_cpu(free_l1_data_B_sram_head, cpu).next->size =
137 L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
138 per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0;
139 per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL;
140
141 per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;
142
143 printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n",
144 L1_DATA_B_LENGTH >> 10,
145 per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10);
146 /* mutex initialize */
Sonic Zhang5d481f42008-07-19 14:51:31 +0800147 }
Bryan Wu1394f032007-05-06 14:50:22 -0700148#endif
149
Graf Yang8f658732008-11-18 17:48:22 +0800150#if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
151 for (cpu = 0; cpu < num_possible_cpus(); ++cpu)
152 spin_lock_init(&per_cpu(l1_data_sram_lock, cpu));
153#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700154}
155
Sonic Zhang5d481f42008-07-19 14:51:31 +0800156static void __init l1_inst_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700157{
158#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800159 unsigned int cpu;
160 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
161 per_cpu(free_l1_inst_sram_head, cpu).next =
162 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
163 if (!per_cpu(free_l1_inst_sram_head, cpu).next) {
164 printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n");
165 return;
166 }
167
168 per_cpu(free_l1_inst_sram_head, cpu).next->paddr =
169 (void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1);
170 per_cpu(free_l1_inst_sram_head, cpu).next->size =
171 L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
172 per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0;
173 per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL;
174
175 per_cpu(used_l1_inst_sram_head, cpu).next = NULL;
176
177 printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n",
178 L1_CODE_LENGTH >> 10,
179 per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10);
180
181 /* mutex initialize */
182 spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
Sonic Zhang5d481f42008-07-19 14:51:31 +0800183 }
Bryan Wu1394f032007-05-06 14:50:22 -0700184#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700185}
186
Sonic Zhang262c3822008-07-19 15:42:41 +0800187static void __init l2_sram_init(void)
188{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800189#if L2_LENGTH != 0
Sonic Zhang262c3822008-07-19 15:42:41 +0800190 free_l2_sram_head.next =
191 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
192 if (!free_l2_sram_head.next) {
Graf Yang8f658732008-11-18 17:48:22 +0800193 printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
Sonic Zhang262c3822008-07-19 15:42:41 +0800194 return;
195 }
196
Jie Zhangb2c2f302008-10-28 15:57:49 +0800197 free_l2_sram_head.next->paddr =
198 (void *)L2_START + (_ebss_l2 - _stext_l2);
199 free_l2_sram_head.next->size =
200 L2_LENGTH - (_ebss_l2 - _stext_l2);
Sonic Zhang262c3822008-07-19 15:42:41 +0800201 free_l2_sram_head.next->pid = 0;
202 free_l2_sram_head.next->next = NULL;
203
204 used_l2_sram_head.next = NULL;
205
206 printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n",
207 L2_LENGTH >> 10,
208 free_l2_sram_head.next->size >> 10);
Sonic Zhang262c3822008-07-19 15:42:41 +0800209
210 /* mutex initialize */
211 spin_lock_init(&l2_sram_lock);
Mike Frysinger81c969a2009-07-01 15:42:13 +0000212#endif
Sonic Zhang262c3822008-07-19 15:42:41 +0800213}
Graf Yang8f658732008-11-18 17:48:22 +0800214
Graf Yangc72aa072009-05-25 04:44:00 +0000215static int __init bfin_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700216{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800217 sram_piece_cache = kmem_cache_create("sram_piece_cache",
218 sizeof(struct sram_piece),
219 0, SLAB_PANIC, NULL);
Bryan Wu1394f032007-05-06 14:50:22 -0700220
Sonic Zhang5d481f42008-07-19 14:51:31 +0800221 l1sram_init();
222 l1_data_sram_init();
223 l1_inst_sram_init();
Sonic Zhang262c3822008-07-19 15:42:41 +0800224 l2_sram_init();
Graf Yangc72aa072009-05-25 04:44:00 +0000225
226 return 0;
Sonic Zhang5d481f42008-07-19 14:51:31 +0800227}
Graf Yangc72aa072009-05-25 04:44:00 +0000228pure_initcall(bfin_sram_init);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800229
Sonic Zhang262c3822008-07-19 15:42:41 +0800230/* SRAM allocate function */
231static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800232 struct sram_piece *pused_head)
233{
234 struct sram_piece *pslot, *plast, *pavail;
235
236 if (size <= 0 || !pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700237 return NULL;
238
239 /* Align the size */
240 size = (size + 3) & ~3;
241
Sonic Zhang5d481f42008-07-19 14:51:31 +0800242 pslot = pfree_head->next;
243 plast = pfree_head;
244
245 /* search an available piece slot */
246 while (pslot != NULL && size > pslot->size) {
247 plast = pslot;
248 pslot = pslot->next;
Bryan Wu1394f032007-05-06 14:50:22 -0700249 }
Sonic Zhang5d481f42008-07-19 14:51:31 +0800250
251 if (!pslot)
Bryan Wu1394f032007-05-06 14:50:22 -0700252 return NULL;
253
Sonic Zhang5d481f42008-07-19 14:51:31 +0800254 if (pslot->size == size) {
255 plast->next = pslot->next;
256 pavail = pslot;
257 } else {
258 pavail = kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
259
260 if (!pavail)
261 return NULL;
262
263 pavail->paddr = pslot->paddr;
264 pavail->size = size;
265 pslot->paddr += size;
266 pslot->size -= size;
Bryan Wu1394f032007-05-06 14:50:22 -0700267 }
268
Sonic Zhang5d481f42008-07-19 14:51:31 +0800269 pavail->pid = current->pid;
270
271 pslot = pused_head->next;
272 plast = pused_head;
273
274 /* insert new piece into used piece list !!! */
275 while (pslot != NULL && pavail->paddr < pslot->paddr) {
276 plast = pslot;
277 pslot = pslot->next;
278 }
279
280 pavail->next = pslot;
281 plast->next = pavail;
282
283 return pavail->paddr;
Bryan Wu1394f032007-05-06 14:50:22 -0700284}
285
286/* Allocate the largest available block. */
Sonic Zhang262c3822008-07-19 15:42:41 +0800287static void *_sram_alloc_max(struct sram_piece *pfree_head,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800288 struct sram_piece *pused_head,
Bryan Wu1394f032007-05-06 14:50:22 -0700289 unsigned long *psize)
290{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800291 struct sram_piece *pslot, *pmax;
Bryan Wu1394f032007-05-06 14:50:22 -0700292
Sonic Zhang5d481f42008-07-19 14:51:31 +0800293 if (!pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700294 return NULL;
Bryan Wu1394f032007-05-06 14:50:22 -0700295
Sonic Zhang5d481f42008-07-19 14:51:31 +0800296 pmax = pslot = pfree_head->next;
297
298 /* search an available piece slot */
299 while (pslot != NULL) {
300 if (pslot->size > pmax->size)
301 pmax = pslot;
302 pslot = pslot->next;
303 }
304
305 if (!pmax)
306 return NULL;
307
308 *psize = pmax->size;
309
Sonic Zhang262c3822008-07-19 15:42:41 +0800310 return _sram_alloc(*psize, pfree_head, pused_head);
Bryan Wu1394f032007-05-06 14:50:22 -0700311}
312
Sonic Zhang262c3822008-07-19 15:42:41 +0800313/* SRAM free function */
314static int _sram_free(const void *addr,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800315 struct sram_piece *pfree_head,
316 struct sram_piece *pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700317{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800318 struct sram_piece *pslot, *plast, *pavail;
Bryan Wu1394f032007-05-06 14:50:22 -0700319
Sonic Zhang5d481f42008-07-19 14:51:31 +0800320 if (!pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700321 return -1;
322
Sonic Zhang5d481f42008-07-19 14:51:31 +0800323 /* search the relevant memory slot */
324 pslot = pused_head->next;
325 plast = pused_head;
Bryan Wu1394f032007-05-06 14:50:22 -0700326
Sonic Zhang5d481f42008-07-19 14:51:31 +0800327 /* search an available piece slot */
328 while (pslot != NULL && pslot->paddr != addr) {
329 plast = pslot;
330 pslot = pslot->next;
Bryan Wu1394f032007-05-06 14:50:22 -0700331 }
332
Sonic Zhang5d481f42008-07-19 14:51:31 +0800333 if (!pslot)
334 return -1;
335
336 plast->next = pslot->next;
337 pavail = pslot;
338 pavail->pid = 0;
339
340 /* insert free pieces back to the free list */
341 pslot = pfree_head->next;
342 plast = pfree_head;
343
344 while (pslot != NULL && addr > pslot->paddr) {
345 plast = pslot;
346 pslot = pslot->next;
347 }
348
349 if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
350 plast->size += pavail->size;
351 kmem_cache_free(sram_piece_cache, pavail);
352 } else {
Sonic Zhang225f7e12008-08-25 18:00:45 +0800353 pavail->next = plast->next;
Sonic Zhang5d481f42008-07-19 14:51:31 +0800354 plast->next = pavail;
355 plast = pavail;
356 }
357
358 if (pslot && plast->paddr + plast->size == pslot->paddr) {
359 plast->size += pslot->size;
360 plast->next = pslot->next;
361 kmem_cache_free(sram_piece_cache, pslot);
Bryan Wu1394f032007-05-06 14:50:22 -0700362 }
363
364 return 0;
365}
366
367int sram_free(const void *addr)
368{
Robin Getz5e953202008-10-08 17:22:49 +0800369
Bryan Wu1394f032007-05-06 14:50:22 -0700370#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800371 if (addr >= (void *)get_l1_code_start()
372 && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700373 return l1_inst_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800374 else
Bryan Wu1394f032007-05-06 14:50:22 -0700375#endif
376#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800377 if (addr >= (void *)get_l1_data_a_start()
378 && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700379 return l1_data_A_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800380 else
Bryan Wu1394f032007-05-06 14:50:22 -0700381#endif
382#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800383 if (addr >= (void *)get_l1_data_b_start()
384 && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700385 return l1_data_B_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800386 else
Bryan Wu1394f032007-05-06 14:50:22 -0700387#endif
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800388#if L2_LENGTH != 0
Robin Getz5e953202008-10-08 17:22:49 +0800389 if (addr >= (void *)L2_START
Sonic Zhang262c3822008-07-19 15:42:41 +0800390 && addr < (void *)(L2_START + L2_LENGTH))
391 return l2_sram_free(addr);
Bryan Wu1394f032007-05-06 14:50:22 -0700392 else
Robin Getz5e953202008-10-08 17:22:49 +0800393#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700394 return -1;
395}
396EXPORT_SYMBOL(sram_free);
397
398void *l1_data_A_sram_alloc(size_t size)
399{
Mike Frysinger81c969a2009-07-01 15:42:13 +0000400#if L1_DATA_A_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800401 unsigned long flags;
Mike Frysinger81c969a2009-07-01 15:42:13 +0000402 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800403 unsigned int cpu;
Bryan Wu1394f032007-05-06 14:50:22 -0700404
Graf Yang8f658732008-11-18 17:48:22 +0800405 cpu = get_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700406 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800407 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700408
Graf Yang8f658732008-11-18 17:48:22 +0800409 addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
410 &per_cpu(used_l1_data_A_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700411
412 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800413 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
414 put_cpu();
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
Graf Yang8f658732008-11-18 17:48:22 +0800433 cpu = get_cpu();
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);
442 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700443
444 return ret;
Mike Frysinger81c969a2009-07-01 15:42:13 +0000445#else
446 return -1;
447#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700448}
449EXPORT_SYMBOL(l1_data_A_sram_free);
450
451void *l1_data_B_sram_alloc(size_t size)
452{
453#if L1_DATA_B_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800454 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700455 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800456 unsigned int cpu;
457
458 cpu = get_cpu();
459 /* add mutex operation */
460 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
461
462 addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
463 &per_cpu(used_l1_data_B_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700464
465 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800466 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
467 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700468
469 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
470 (long unsigned int)addr, size);
471
472 return addr;
473#else
474 return NULL;
475#endif
476}
477EXPORT_SYMBOL(l1_data_B_sram_alloc);
478
479int l1_data_B_sram_free(const void *addr)
480{
481#if L1_DATA_B_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800482 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700483 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800484 unsigned int cpu;
485
486 cpu = get_cpu();
487 /* add mutex operation */
488 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
489
490 ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu),
491 &per_cpu(used_l1_data_B_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700492
493 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800494 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
495 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700496
497 return ret;
498#else
499 return -1;
500#endif
501}
502EXPORT_SYMBOL(l1_data_B_sram_free);
503
504void *l1_data_sram_alloc(size_t size)
505{
506 void *addr = l1_data_A_sram_alloc(size);
507
508 if (!addr)
509 addr = l1_data_B_sram_alloc(size);
510
511 return addr;
512}
513EXPORT_SYMBOL(l1_data_sram_alloc);
514
515void *l1_data_sram_zalloc(size_t size)
516{
517 void *addr = l1_data_sram_alloc(size);
518
519 if (addr)
520 memset(addr, 0x00, size);
521
522 return addr;
523}
524EXPORT_SYMBOL(l1_data_sram_zalloc);
525
526int l1_data_sram_free(const void *addr)
527{
528 int ret;
529 ret = l1_data_A_sram_free(addr);
530 if (ret == -1)
531 ret = l1_data_B_sram_free(addr);
532 return ret;
533}
534EXPORT_SYMBOL(l1_data_sram_free);
535
536void *l1_inst_sram_alloc(size_t size)
537{
Meihui Fanc5b50df2008-04-23 08:55:26 +0800538#if L1_CODE_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800539 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700540 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800541 unsigned int cpu;
542
543 cpu = get_cpu();
544 /* add mutex operation */
545 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
546
547 addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
548 &per_cpu(used_l1_inst_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700549
550 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800551 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
552 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700553
554 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
555 (long unsigned int)addr, size);
556
557 return addr;
558#else
559 return NULL;
560#endif
561}
562EXPORT_SYMBOL(l1_inst_sram_alloc);
563
564int l1_inst_sram_free(const void *addr)
565{
566#if L1_CODE_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800567 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700568 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800569 unsigned int cpu;
570
571 cpu = get_cpu();
572 /* add mutex operation */
573 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
574
575 ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
576 &per_cpu(used_l1_inst_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700577
578 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800579 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
580 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700581
582 return ret;
583#else
584 return -1;
585#endif
586}
587EXPORT_SYMBOL(l1_inst_sram_free);
588
589/* L1 Scratchpad memory allocate function */
590void *l1sram_alloc(size_t size)
591{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800592 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700593 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800594 unsigned int cpu;
595
596 cpu = get_cpu();
597 /* add mutex operation */
598 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
599
600 addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu),
601 &per_cpu(used_l1_ssram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700602
603 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800604 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
605 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700606
607 return addr;
608}
609
610/* L1 Scratchpad memory allocate function */
611void *l1sram_alloc_max(size_t *psize)
612{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800613 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700614 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800615 unsigned int cpu;
616
617 cpu = get_cpu();
618 /* add mutex operation */
619 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
620
621 addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
622 &per_cpu(used_l1_ssram_head, cpu), psize);
Bryan Wu1394f032007-05-06 14:50:22 -0700623
624 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800625 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
626 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700627
628 return addr;
629}
630
631/* L1 Scratchpad memory free function */
632int l1sram_free(const void *addr)
633{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800634 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700635 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800636 unsigned int cpu;
637
638 cpu = get_cpu();
639 /* add mutex operation */
640 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
641
642 ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
643 &per_cpu(used_l1_ssram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700644
645 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800646 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
647 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700648
649 return ret;
650}
651
Sonic Zhang262c3822008-07-19 15:42:41 +0800652void *l2_sram_alloc(size_t size)
653{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800654#if L2_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800655 unsigned long flags;
Sonic Zhang262c3822008-07-19 15:42:41 +0800656 void *addr;
657
658 /* add mutex operation */
659 spin_lock_irqsave(&l2_sram_lock, flags);
660
661 addr = _sram_alloc(size, &free_l2_sram_head,
662 &used_l2_sram_head);
663
664 /* add mutex operation */
665 spin_unlock_irqrestore(&l2_sram_lock, flags);
666
667 pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
668 (long unsigned int)addr, size);
669
670 return addr;
671#else
672 return NULL;
673#endif
674}
675EXPORT_SYMBOL(l2_sram_alloc);
676
677void *l2_sram_zalloc(size_t size)
678{
679 void *addr = l2_sram_alloc(size);
680
681 if (addr)
682 memset(addr, 0x00, size);
683
684 return addr;
685}
686EXPORT_SYMBOL(l2_sram_zalloc);
687
688int l2_sram_free(const void *addr)
689{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800690#if L2_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800691 unsigned long flags;
Sonic Zhang262c3822008-07-19 15:42:41 +0800692 int ret;
693
694 /* add mutex operation */
695 spin_lock_irqsave(&l2_sram_lock, flags);
696
697 ret = _sram_free(addr, &free_l2_sram_head,
698 &used_l2_sram_head);
699
700 /* add mutex operation */
701 spin_unlock_irqrestore(&l2_sram_lock, flags);
702
703 return ret;
704#else
705 return -1;
706#endif
707}
708EXPORT_SYMBOL(l2_sram_free);
709
Bryan Wu1394f032007-05-06 14:50:22 -0700710int sram_free_with_lsl(const void *addr)
711{
712 struct sram_list_struct *lsl, **tmp;
713 struct mm_struct *mm = current->mm;
714
715 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
716 if ((*tmp)->addr == addr)
717 goto found;
718 return -1;
719found:
720 lsl = *tmp;
721 sram_free(addr);
722 *tmp = lsl->next;
723 kfree(lsl);
724
725 return 0;
726}
727EXPORT_SYMBOL(sram_free_with_lsl);
728
Mike Frysingerf1db88d2009-06-02 08:46:35 +0000729/* Allocate memory and keep in L1 SRAM List (lsl) so that the resources are
730 * tracked. These are designed for userspace so that when a process exits,
731 * we can safely reap their resources.
732 */
Bryan Wu1394f032007-05-06 14:50:22 -0700733void *sram_alloc_with_lsl(size_t size, unsigned long flags)
734{
735 void *addr = NULL;
736 struct sram_list_struct *lsl = NULL;
737 struct mm_struct *mm = current->mm;
738
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700739 lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
Bryan Wu1394f032007-05-06 14:50:22 -0700740 if (!lsl)
741 return NULL;
Bryan Wu1394f032007-05-06 14:50:22 -0700742
743 if (flags & L1_INST_SRAM)
744 addr = l1_inst_sram_alloc(size);
745
746 if (addr == NULL && (flags & L1_DATA_A_SRAM))
747 addr = l1_data_A_sram_alloc(size);
748
749 if (addr == NULL && (flags & L1_DATA_B_SRAM))
750 addr = l1_data_B_sram_alloc(size);
751
Sonic Zhang262c3822008-07-19 15:42:41 +0800752 if (addr == NULL && (flags & L2_SRAM))
753 addr = l2_sram_alloc(size);
754
Bryan Wu1394f032007-05-06 14:50:22 -0700755 if (addr == NULL) {
756 kfree(lsl);
757 return NULL;
758 }
759 lsl->addr = addr;
760 lsl->length = size;
761 lsl->next = mm->context.sram_list;
762 mm->context.sram_list = lsl;
763 return addr;
764}
765EXPORT_SYMBOL(sram_alloc_with_lsl);
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800766
767#ifdef CONFIG_PROC_FS
768/* Once we get a real allocator, we'll throw all of this away.
769 * Until then, we need some sort of visibility into the L1 alloc.
770 */
Mike Frysinger260d5d32008-07-14 16:34:05 +0800771/* Need to keep line of output the same. Currently, that is 44 bytes
772 * (including newline).
773 */
Sonic Zhang262c3822008-07-19 15:42:41 +0800774static int _sram_proc_read(char *buf, int *len, int count, const char *desc,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800775 struct sram_piece *pfree_head,
776 struct sram_piece *pused_head)
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800777{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800778 struct sram_piece *pslot;
779
780 if (!pfree_head || !pused_head)
781 return -1;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800782
Sonic Zhang262c3822008-07-19 15:42:41 +0800783 *len += sprintf(&buf[*len], "--- SRAM %-14s Size PID State \n", desc);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800784
785 /* search the relevant memory slot */
786 pslot = pused_head->next;
787
788 while (pslot != NULL) {
Sonic Zhang262c3822008-07-19 15:42:41 +0800789 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
Sonic Zhang5d481f42008-07-19 14:51:31 +0800790 pslot->paddr, pslot->paddr + pslot->size,
791 pslot->size, pslot->pid, "ALLOCATED");
792
793 pslot = pslot->next;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800794 }
Sonic Zhang5d481f42008-07-19 14:51:31 +0800795
796 pslot = pfree_head->next;
797
798 while (pslot != NULL) {
Sonic Zhang262c3822008-07-19 15:42:41 +0800799 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
Sonic Zhang5d481f42008-07-19 14:51:31 +0800800 pslot->paddr, pslot->paddr + pslot->size,
801 pslot->size, pslot->pid, "FREE");
802
803 pslot = pslot->next;
804 }
805
806 return 0;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800807}
Sonic Zhang262c3822008-07-19 15:42:41 +0800808static int sram_proc_read(char *buf, char **start, off_t offset, int count,
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800809 int *eof, void *data)
810{
811 int len = 0;
Graf Yang8f658732008-11-18 17:48:22 +0800812 unsigned int cpu;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800813
Graf Yang8f658732008-11-18 17:48:22 +0800814 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
815 if (_sram_proc_read(buf, &len, count, "Scratchpad",
816 &per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu)))
817 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800818#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800819 if (_sram_proc_read(buf, &len, count, "L1 Data A",
820 &per_cpu(free_l1_data_A_sram_head, cpu),
821 &per_cpu(used_l1_data_A_sram_head, cpu)))
822 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800823#endif
824#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800825 if (_sram_proc_read(buf, &len, count, "L1 Data B",
826 &per_cpu(free_l1_data_B_sram_head, cpu),
827 &per_cpu(used_l1_data_B_sram_head, cpu)))
828 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800829#endif
830#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800831 if (_sram_proc_read(buf, &len, count, "L1 Instruction",
832 &per_cpu(free_l1_inst_sram_head, cpu),
833 &per_cpu(used_l1_inst_sram_head, cpu)))
834 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800835#endif
Graf Yang8f658732008-11-18 17:48:22 +0800836 }
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800837#if L2_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800838 if (_sram_proc_read(buf, &len, count, "L2", &free_l2_sram_head,
839 &used_l2_sram_head))
Sonic Zhang262c3822008-07-19 15:42:41 +0800840 goto not_done;
841#endif
Mike Frysinger260d5d32008-07-14 16:34:05 +0800842 *eof = 1;
843 not_done:
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800844 return len;
845}
846
Sonic Zhang262c3822008-07-19 15:42:41 +0800847static int __init sram_proc_init(void)
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800848{
849 struct proc_dir_entry *ptr;
850 ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
851 if (!ptr) {
852 printk(KERN_WARNING "unable to create /proc/sram\n");
853 return -1;
854 }
Sonic Zhang262c3822008-07-19 15:42:41 +0800855 ptr->read_proc = sram_proc_read;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800856 return 0;
857}
Sonic Zhang262c3822008-07-19 15:42:41 +0800858late_initcall(sram_proc_init);
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800859#endif