blob: eb63ab353e5a1efdd5a124eade0fb1b355de4490 [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
Robin Getz5e953202008-10-08 17:22:49 +08002 * File: arch/blackfin/mm/sram-alloc.c
Bryan Wu1394f032007-05-06 14:50:22 -07003 * Based on:
4 * Author:
5 *
6 * Created:
Robin Getz5e953202008-10-08 17:22:49 +08007 * Description: SRAM allocator for Blackfin L1 and L2 memory
Bryan Wu1394f032007-05-06 14:50:22 -07008 *
9 * Modified:
Robin Getz5e953202008-10-08 17:22:49 +080010 * Copyright 2004-2008 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
Bryan Wu1394f032007-05-06 14:50:22 -070030#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/types.h>
33#include <linux/miscdevice.h>
34#include <linux/ioport.h>
35#include <linux/fcntl.h>
36#include <linux/init.h>
37#include <linux/poll.h>
38#include <linux/proc_fs.h>
39#include <linux/spinlock.h>
40#include <linux/rtc.h>
41#include <asm/blackfin.h>
Graf Yangdbc895f2009-01-07 23:14:39 +080042#include <asm/mem_map.h>
Bryan Wu1394f032007-05-06 14:50:22 -070043#include "blackfin_sram.h"
44
Bryan Wu1394f032007-05-06 14:50:22 -070045/* the data structure for L1 scratchpad and DATA SRAM */
Sonic Zhang5d481f42008-07-19 14:51:31 +080046struct sram_piece {
Bryan Wu1394f032007-05-06 14:50:22 -070047 void *paddr;
48 int size;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +080049 pid_t pid;
Sonic Zhang5d481f42008-07-19 14:51:31 +080050 struct sram_piece *next;
Bryan Wu1394f032007-05-06 14:50:22 -070051};
52
Mike Frysinger81c969a2009-07-01 15:42:13 +000053static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1sram_lock);
Graf Yang8f658732008-11-18 17:48:22 +080054static DEFINE_PER_CPU(struct sram_piece, free_l1_ssram_head);
55static DEFINE_PER_CPU(struct sram_piece, used_l1_ssram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070056
57#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +080058static DEFINE_PER_CPU(struct sram_piece, free_l1_data_A_sram_head);
59static DEFINE_PER_CPU(struct sram_piece, used_l1_data_A_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070060#endif
61
62#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +080063static DEFINE_PER_CPU(struct sram_piece, free_l1_data_B_sram_head);
64static DEFINE_PER_CPU(struct sram_piece, used_l1_data_B_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070065#endif
66
Mike Frysinger81c969a2009-07-01 15:42:13 +000067#if L1_DATA_A_LENGTH || L1_DATA_B_LENGTH
68static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_data_sram_lock);
69#endif
70
Bryan Wu1394f032007-05-06 14:50:22 -070071#if L1_CODE_LENGTH != 0
Mike Frysinger81c969a2009-07-01 15:42:13 +000072static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_inst_sram_lock);
Graf Yang8f658732008-11-18 17:48:22 +080073static DEFINE_PER_CPU(struct sram_piece, free_l1_inst_sram_head);
74static DEFINE_PER_CPU(struct sram_piece, used_l1_inst_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070075#endif
76
Mike Frysinger07aa7be2008-08-13 16:16:11 +080077#if L2_LENGTH != 0
Mike Frysinger81c969a2009-07-01 15:42:13 +000078static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp;
Sonic Zhang262c3822008-07-19 15:42:41 +080079static struct sram_piece free_l2_sram_head, used_l2_sram_head;
80#endif
81
Sonic Zhang5d481f42008-07-19 14:51:31 +080082static struct kmem_cache *sram_piece_cache;
Bryan Wu1394f032007-05-06 14:50:22 -070083
Sonic Zhang5d481f42008-07-19 14:51:31 +080084/* L1 Scratchpad SRAM initialization function */
85static void __init l1sram_init(void)
86{
Graf Yang8f658732008-11-18 17:48:22 +080087 unsigned int cpu;
Graf Yang89ecd502009-05-25 06:40:42 +000088 unsigned long reserve;
89
90#ifdef CONFIG_SMP
91 reserve = 0;
92#else
93 reserve = sizeof(struct l1_scratch_task_info);
94#endif
95
Graf Yang8f658732008-11-18 17:48:22 +080096 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
97 per_cpu(free_l1_ssram_head, cpu).next =
98 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
99 if (!per_cpu(free_l1_ssram_head, cpu).next) {
100 printk(KERN_INFO "Fail to initialize Scratchpad data SRAM.\n");
101 return;
102 }
103
Graf Yang89ecd502009-05-25 06:40:42 +0000104 per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu) + reserve;
105 per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH - reserve;
Graf Yang8f658732008-11-18 17:48:22 +0800106 per_cpu(free_l1_ssram_head, cpu).next->pid = 0;
107 per_cpu(free_l1_ssram_head, cpu).next->next = NULL;
108
109 per_cpu(used_l1_ssram_head, cpu).next = NULL;
110
111 /* mutex initialize */
112 spin_lock_init(&per_cpu(l1sram_lock, cpu));
113 printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
114 L1_SCRATCH_LENGTH >> 10);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800115 }
Bryan Wu1394f032007-05-06 14:50:22 -0700116}
117
Sonic Zhang5d481f42008-07-19 14:51:31 +0800118static void __init l1_data_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700119{
Mike Frysinger0b82e272008-11-18 17:48:22 +0800120#if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800121 unsigned int cpu;
Mike Frysinger0b82e272008-11-18 17:48:22 +0800122#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700123#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800124 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
125 per_cpu(free_l1_data_A_sram_head, cpu).next =
126 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
127 if (!per_cpu(free_l1_data_A_sram_head, cpu).next) {
128 printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n");
129 return;
130 }
131
132 per_cpu(free_l1_data_A_sram_head, cpu).next->paddr =
133 (void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1);
134 per_cpu(free_l1_data_A_sram_head, cpu).next->size =
135 L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
136 per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0;
137 per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL;
138
139 per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;
140
141 printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n",
142 L1_DATA_A_LENGTH >> 10,
143 per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800144 }
Bryan Wu1394f032007-05-06 14:50:22 -0700145#endif
146#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800147 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
148 per_cpu(free_l1_data_B_sram_head, cpu).next =
149 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
150 if (!per_cpu(free_l1_data_B_sram_head, cpu).next) {
151 printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n");
152 return;
153 }
154
155 per_cpu(free_l1_data_B_sram_head, cpu).next->paddr =
156 (void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1);
157 per_cpu(free_l1_data_B_sram_head, cpu).next->size =
158 L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
159 per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0;
160 per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL;
161
162 per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;
163
164 printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n",
165 L1_DATA_B_LENGTH >> 10,
166 per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10);
167 /* mutex initialize */
Sonic Zhang5d481f42008-07-19 14:51:31 +0800168 }
Bryan Wu1394f032007-05-06 14:50:22 -0700169#endif
170
Graf Yang8f658732008-11-18 17:48:22 +0800171#if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
172 for (cpu = 0; cpu < num_possible_cpus(); ++cpu)
173 spin_lock_init(&per_cpu(l1_data_sram_lock, cpu));
174#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700175}
176
Sonic Zhang5d481f42008-07-19 14:51:31 +0800177static void __init l1_inst_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700178{
179#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800180 unsigned int cpu;
181 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
182 per_cpu(free_l1_inst_sram_head, cpu).next =
183 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
184 if (!per_cpu(free_l1_inst_sram_head, cpu).next) {
185 printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n");
186 return;
187 }
188
189 per_cpu(free_l1_inst_sram_head, cpu).next->paddr =
190 (void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1);
191 per_cpu(free_l1_inst_sram_head, cpu).next->size =
192 L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
193 per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0;
194 per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL;
195
196 per_cpu(used_l1_inst_sram_head, cpu).next = NULL;
197
198 printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n",
199 L1_CODE_LENGTH >> 10,
200 per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10);
201
202 /* mutex initialize */
203 spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
Sonic Zhang5d481f42008-07-19 14:51:31 +0800204 }
Bryan Wu1394f032007-05-06 14:50:22 -0700205#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700206}
207
Sonic Zhang262c3822008-07-19 15:42:41 +0800208static void __init l2_sram_init(void)
209{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800210#if L2_LENGTH != 0
Sonic Zhang262c3822008-07-19 15:42:41 +0800211 free_l2_sram_head.next =
212 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
213 if (!free_l2_sram_head.next) {
Graf Yang8f658732008-11-18 17:48:22 +0800214 printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
Sonic Zhang262c3822008-07-19 15:42:41 +0800215 return;
216 }
217
Jie Zhangb2c2f302008-10-28 15:57:49 +0800218 free_l2_sram_head.next->paddr =
219 (void *)L2_START + (_ebss_l2 - _stext_l2);
220 free_l2_sram_head.next->size =
221 L2_LENGTH - (_ebss_l2 - _stext_l2);
Sonic Zhang262c3822008-07-19 15:42:41 +0800222 free_l2_sram_head.next->pid = 0;
223 free_l2_sram_head.next->next = NULL;
224
225 used_l2_sram_head.next = NULL;
226
227 printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n",
228 L2_LENGTH >> 10,
229 free_l2_sram_head.next->size >> 10);
Sonic Zhang262c3822008-07-19 15:42:41 +0800230
231 /* mutex initialize */
232 spin_lock_init(&l2_sram_lock);
Mike Frysinger81c969a2009-07-01 15:42:13 +0000233#endif
Sonic Zhang262c3822008-07-19 15:42:41 +0800234}
Graf Yang8f658732008-11-18 17:48:22 +0800235
Graf Yangc72aa072009-05-25 04:44:00 +0000236static int __init bfin_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700237{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800238 sram_piece_cache = kmem_cache_create("sram_piece_cache",
239 sizeof(struct sram_piece),
240 0, SLAB_PANIC, NULL);
Bryan Wu1394f032007-05-06 14:50:22 -0700241
Sonic Zhang5d481f42008-07-19 14:51:31 +0800242 l1sram_init();
243 l1_data_sram_init();
244 l1_inst_sram_init();
Sonic Zhang262c3822008-07-19 15:42:41 +0800245 l2_sram_init();
Graf Yangc72aa072009-05-25 04:44:00 +0000246
247 return 0;
Sonic Zhang5d481f42008-07-19 14:51:31 +0800248}
Graf Yangc72aa072009-05-25 04:44:00 +0000249pure_initcall(bfin_sram_init);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800250
Sonic Zhang262c3822008-07-19 15:42:41 +0800251/* SRAM allocate function */
252static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800253 struct sram_piece *pused_head)
254{
255 struct sram_piece *pslot, *plast, *pavail;
256
257 if (size <= 0 || !pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700258 return NULL;
259
260 /* Align the size */
261 size = (size + 3) & ~3;
262
Sonic Zhang5d481f42008-07-19 14:51:31 +0800263 pslot = pfree_head->next;
264 plast = pfree_head;
265
266 /* search an available piece slot */
267 while (pslot != NULL && size > pslot->size) {
268 plast = pslot;
269 pslot = pslot->next;
Bryan Wu1394f032007-05-06 14:50:22 -0700270 }
Sonic Zhang5d481f42008-07-19 14:51:31 +0800271
272 if (!pslot)
Bryan Wu1394f032007-05-06 14:50:22 -0700273 return NULL;
274
Sonic Zhang5d481f42008-07-19 14:51:31 +0800275 if (pslot->size == size) {
276 plast->next = pslot->next;
277 pavail = pslot;
278 } else {
279 pavail = kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
280
281 if (!pavail)
282 return NULL;
283
284 pavail->paddr = pslot->paddr;
285 pavail->size = size;
286 pslot->paddr += size;
287 pslot->size -= size;
Bryan Wu1394f032007-05-06 14:50:22 -0700288 }
289
Sonic Zhang5d481f42008-07-19 14:51:31 +0800290 pavail->pid = current->pid;
291
292 pslot = pused_head->next;
293 plast = pused_head;
294
295 /* insert new piece into used piece list !!! */
296 while (pslot != NULL && pavail->paddr < pslot->paddr) {
297 plast = pslot;
298 pslot = pslot->next;
299 }
300
301 pavail->next = pslot;
302 plast->next = pavail;
303
304 return pavail->paddr;
Bryan Wu1394f032007-05-06 14:50:22 -0700305}
306
307/* Allocate the largest available block. */
Sonic Zhang262c3822008-07-19 15:42:41 +0800308static void *_sram_alloc_max(struct sram_piece *pfree_head,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800309 struct sram_piece *pused_head,
Bryan Wu1394f032007-05-06 14:50:22 -0700310 unsigned long *psize)
311{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800312 struct sram_piece *pslot, *pmax;
Bryan Wu1394f032007-05-06 14:50:22 -0700313
Sonic Zhang5d481f42008-07-19 14:51:31 +0800314 if (!pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700315 return NULL;
Bryan Wu1394f032007-05-06 14:50:22 -0700316
Sonic Zhang5d481f42008-07-19 14:51:31 +0800317 pmax = pslot = pfree_head->next;
318
319 /* search an available piece slot */
320 while (pslot != NULL) {
321 if (pslot->size > pmax->size)
322 pmax = pslot;
323 pslot = pslot->next;
324 }
325
326 if (!pmax)
327 return NULL;
328
329 *psize = pmax->size;
330
Sonic Zhang262c3822008-07-19 15:42:41 +0800331 return _sram_alloc(*psize, pfree_head, pused_head);
Bryan Wu1394f032007-05-06 14:50:22 -0700332}
333
Sonic Zhang262c3822008-07-19 15:42:41 +0800334/* SRAM free function */
335static int _sram_free(const void *addr,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800336 struct sram_piece *pfree_head,
337 struct sram_piece *pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700338{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800339 struct sram_piece *pslot, *plast, *pavail;
Bryan Wu1394f032007-05-06 14:50:22 -0700340
Sonic Zhang5d481f42008-07-19 14:51:31 +0800341 if (!pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700342 return -1;
343
Sonic Zhang5d481f42008-07-19 14:51:31 +0800344 /* search the relevant memory slot */
345 pslot = pused_head->next;
346 plast = pused_head;
Bryan Wu1394f032007-05-06 14:50:22 -0700347
Sonic Zhang5d481f42008-07-19 14:51:31 +0800348 /* search an available piece slot */
349 while (pslot != NULL && pslot->paddr != addr) {
350 plast = pslot;
351 pslot = pslot->next;
Bryan Wu1394f032007-05-06 14:50:22 -0700352 }
353
Sonic Zhang5d481f42008-07-19 14:51:31 +0800354 if (!pslot)
355 return -1;
356
357 plast->next = pslot->next;
358 pavail = pslot;
359 pavail->pid = 0;
360
361 /* insert free pieces back to the free list */
362 pslot = pfree_head->next;
363 plast = pfree_head;
364
365 while (pslot != NULL && addr > pslot->paddr) {
366 plast = pslot;
367 pslot = pslot->next;
368 }
369
370 if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
371 plast->size += pavail->size;
372 kmem_cache_free(sram_piece_cache, pavail);
373 } else {
Sonic Zhang225f7e12008-08-25 18:00:45 +0800374 pavail->next = plast->next;
Sonic Zhang5d481f42008-07-19 14:51:31 +0800375 plast->next = pavail;
376 plast = pavail;
377 }
378
379 if (pslot && plast->paddr + plast->size == pslot->paddr) {
380 plast->size += pslot->size;
381 plast->next = pslot->next;
382 kmem_cache_free(sram_piece_cache, pslot);
Bryan Wu1394f032007-05-06 14:50:22 -0700383 }
384
385 return 0;
386}
387
388int sram_free(const void *addr)
389{
Robin Getz5e953202008-10-08 17:22:49 +0800390
Bryan Wu1394f032007-05-06 14:50:22 -0700391#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800392 if (addr >= (void *)get_l1_code_start()
393 && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700394 return l1_inst_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800395 else
Bryan Wu1394f032007-05-06 14:50:22 -0700396#endif
397#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800398 if (addr >= (void *)get_l1_data_a_start()
399 && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700400 return l1_data_A_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800401 else
Bryan Wu1394f032007-05-06 14:50:22 -0700402#endif
403#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800404 if (addr >= (void *)get_l1_data_b_start()
405 && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700406 return l1_data_B_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800407 else
Bryan Wu1394f032007-05-06 14:50:22 -0700408#endif
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800409#if L2_LENGTH != 0
Robin Getz5e953202008-10-08 17:22:49 +0800410 if (addr >= (void *)L2_START
Sonic Zhang262c3822008-07-19 15:42:41 +0800411 && addr < (void *)(L2_START + L2_LENGTH))
412 return l2_sram_free(addr);
Bryan Wu1394f032007-05-06 14:50:22 -0700413 else
Robin Getz5e953202008-10-08 17:22:49 +0800414#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700415 return -1;
416}
417EXPORT_SYMBOL(sram_free);
418
419void *l1_data_A_sram_alloc(size_t size)
420{
Mike Frysinger81c969a2009-07-01 15:42:13 +0000421#if L1_DATA_A_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800422 unsigned long flags;
Mike Frysinger81c969a2009-07-01 15:42:13 +0000423 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800424 unsigned int cpu;
Bryan Wu1394f032007-05-06 14:50:22 -0700425
Graf Yang8f658732008-11-18 17:48:22 +0800426 cpu = get_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700427 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800428 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700429
Graf Yang8f658732008-11-18 17:48:22 +0800430 addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
431 &per_cpu(used_l1_data_A_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700432
433 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800434 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
435 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700436
437 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
438 (long unsigned int)addr, size);
439
440 return addr;
Mike Frysinger81c969a2009-07-01 15:42:13 +0000441#else
442 return NULL;
443#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700444}
445EXPORT_SYMBOL(l1_data_A_sram_alloc);
446
447int l1_data_A_sram_free(const void *addr)
448{
Mike Frysinger81c969a2009-07-01 15:42:13 +0000449#if L1_DATA_A_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800450 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700451 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800452 unsigned int cpu;
Bryan Wu1394f032007-05-06 14:50:22 -0700453
Graf Yang8f658732008-11-18 17:48:22 +0800454 cpu = get_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700455 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800456 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700457
Graf Yang8f658732008-11-18 17:48:22 +0800458 ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu),
459 &per_cpu(used_l1_data_A_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700460
461 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800462 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
463 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700464
465 return ret;
Mike Frysinger81c969a2009-07-01 15:42:13 +0000466#else
467 return -1;
468#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700469}
470EXPORT_SYMBOL(l1_data_A_sram_free);
471
472void *l1_data_B_sram_alloc(size_t size)
473{
474#if L1_DATA_B_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800475 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700476 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800477 unsigned int cpu;
478
479 cpu = get_cpu();
480 /* add mutex operation */
481 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
482
483 addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
484 &per_cpu(used_l1_data_B_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700485
486 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800487 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
488 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700489
490 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
491 (long unsigned int)addr, size);
492
493 return addr;
494#else
495 return NULL;
496#endif
497}
498EXPORT_SYMBOL(l1_data_B_sram_alloc);
499
500int l1_data_B_sram_free(const void *addr)
501{
502#if L1_DATA_B_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800503 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700504 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800505 unsigned int cpu;
506
507 cpu = get_cpu();
508 /* add mutex operation */
509 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
510
511 ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu),
512 &per_cpu(used_l1_data_B_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700513
514 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800515 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
516 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700517
518 return ret;
519#else
520 return -1;
521#endif
522}
523EXPORT_SYMBOL(l1_data_B_sram_free);
524
525void *l1_data_sram_alloc(size_t size)
526{
527 void *addr = l1_data_A_sram_alloc(size);
528
529 if (!addr)
530 addr = l1_data_B_sram_alloc(size);
531
532 return addr;
533}
534EXPORT_SYMBOL(l1_data_sram_alloc);
535
536void *l1_data_sram_zalloc(size_t size)
537{
538 void *addr = l1_data_sram_alloc(size);
539
540 if (addr)
541 memset(addr, 0x00, size);
542
543 return addr;
544}
545EXPORT_SYMBOL(l1_data_sram_zalloc);
546
547int l1_data_sram_free(const void *addr)
548{
549 int ret;
550 ret = l1_data_A_sram_free(addr);
551 if (ret == -1)
552 ret = l1_data_B_sram_free(addr);
553 return ret;
554}
555EXPORT_SYMBOL(l1_data_sram_free);
556
557void *l1_inst_sram_alloc(size_t size)
558{
Meihui Fanc5b50df2008-04-23 08:55:26 +0800559#if L1_CODE_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800560 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700561 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800562 unsigned int cpu;
563
564 cpu = get_cpu();
565 /* add mutex operation */
566 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
567
568 addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
569 &per_cpu(used_l1_inst_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700570
571 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800572 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
573 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700574
575 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
576 (long unsigned int)addr, size);
577
578 return addr;
579#else
580 return NULL;
581#endif
582}
583EXPORT_SYMBOL(l1_inst_sram_alloc);
584
585int l1_inst_sram_free(const void *addr)
586{
587#if L1_CODE_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800588 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700589 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800590 unsigned int cpu;
591
592 cpu = get_cpu();
593 /* add mutex operation */
594 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
595
596 ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
597 &per_cpu(used_l1_inst_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700598
599 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800600 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
601 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700602
603 return ret;
604#else
605 return -1;
606#endif
607}
608EXPORT_SYMBOL(l1_inst_sram_free);
609
610/* L1 Scratchpad memory allocate function */
611void *l1sram_alloc(size_t size)
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(size, &per_cpu(free_l1_ssram_head, cpu),
622 &per_cpu(used_l1_ssram_head, cpu));
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 allocate function */
632void *l1sram_alloc_max(size_t *psize)
633{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800634 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700635 void *addr;
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 addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
643 &per_cpu(used_l1_ssram_head, cpu), psize);
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 addr;
650}
651
652/* L1 Scratchpad memory free function */
653int l1sram_free(const void *addr)
654{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800655 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700656 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800657 unsigned int cpu;
658
659 cpu = get_cpu();
660 /* add mutex operation */
661 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
662
663 ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
664 &per_cpu(used_l1_ssram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700665
666 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800667 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
668 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700669
670 return ret;
671}
672
Sonic Zhang262c3822008-07-19 15:42:41 +0800673void *l2_sram_alloc(size_t size)
674{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800675#if L2_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800676 unsigned long flags;
Sonic Zhang262c3822008-07-19 15:42:41 +0800677 void *addr;
678
679 /* add mutex operation */
680 spin_lock_irqsave(&l2_sram_lock, flags);
681
682 addr = _sram_alloc(size, &free_l2_sram_head,
683 &used_l2_sram_head);
684
685 /* add mutex operation */
686 spin_unlock_irqrestore(&l2_sram_lock, flags);
687
688 pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
689 (long unsigned int)addr, size);
690
691 return addr;
692#else
693 return NULL;
694#endif
695}
696EXPORT_SYMBOL(l2_sram_alloc);
697
698void *l2_sram_zalloc(size_t size)
699{
700 void *addr = l2_sram_alloc(size);
701
702 if (addr)
703 memset(addr, 0x00, size);
704
705 return addr;
706}
707EXPORT_SYMBOL(l2_sram_zalloc);
708
709int l2_sram_free(const void *addr)
710{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800711#if L2_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800712 unsigned long flags;
Sonic Zhang262c3822008-07-19 15:42:41 +0800713 int ret;
714
715 /* add mutex operation */
716 spin_lock_irqsave(&l2_sram_lock, flags);
717
718 ret = _sram_free(addr, &free_l2_sram_head,
719 &used_l2_sram_head);
720
721 /* add mutex operation */
722 spin_unlock_irqrestore(&l2_sram_lock, flags);
723
724 return ret;
725#else
726 return -1;
727#endif
728}
729EXPORT_SYMBOL(l2_sram_free);
730
Bryan Wu1394f032007-05-06 14:50:22 -0700731int sram_free_with_lsl(const void *addr)
732{
733 struct sram_list_struct *lsl, **tmp;
734 struct mm_struct *mm = current->mm;
735
736 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
737 if ((*tmp)->addr == addr)
738 goto found;
739 return -1;
740found:
741 lsl = *tmp;
742 sram_free(addr);
743 *tmp = lsl->next;
744 kfree(lsl);
745
746 return 0;
747}
748EXPORT_SYMBOL(sram_free_with_lsl);
749
Mike Frysingerf1db88d2009-06-02 08:46:35 +0000750/* Allocate memory and keep in L1 SRAM List (lsl) so that the resources are
751 * tracked. These are designed for userspace so that when a process exits,
752 * we can safely reap their resources.
753 */
Bryan Wu1394f032007-05-06 14:50:22 -0700754void *sram_alloc_with_lsl(size_t size, unsigned long flags)
755{
756 void *addr = NULL;
757 struct sram_list_struct *lsl = NULL;
758 struct mm_struct *mm = current->mm;
759
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700760 lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
Bryan Wu1394f032007-05-06 14:50:22 -0700761 if (!lsl)
762 return NULL;
Bryan Wu1394f032007-05-06 14:50:22 -0700763
764 if (flags & L1_INST_SRAM)
765 addr = l1_inst_sram_alloc(size);
766
767 if (addr == NULL && (flags & L1_DATA_A_SRAM))
768 addr = l1_data_A_sram_alloc(size);
769
770 if (addr == NULL && (flags & L1_DATA_B_SRAM))
771 addr = l1_data_B_sram_alloc(size);
772
Sonic Zhang262c3822008-07-19 15:42:41 +0800773 if (addr == NULL && (flags & L2_SRAM))
774 addr = l2_sram_alloc(size);
775
Bryan Wu1394f032007-05-06 14:50:22 -0700776 if (addr == NULL) {
777 kfree(lsl);
778 return NULL;
779 }
780 lsl->addr = addr;
781 lsl->length = size;
782 lsl->next = mm->context.sram_list;
783 mm->context.sram_list = lsl;
784 return addr;
785}
786EXPORT_SYMBOL(sram_alloc_with_lsl);
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800787
788#ifdef CONFIG_PROC_FS
789/* Once we get a real allocator, we'll throw all of this away.
790 * Until then, we need some sort of visibility into the L1 alloc.
791 */
Mike Frysinger260d5d32008-07-14 16:34:05 +0800792/* Need to keep line of output the same. Currently, that is 44 bytes
793 * (including newline).
794 */
Sonic Zhang262c3822008-07-19 15:42:41 +0800795static int _sram_proc_read(char *buf, int *len, int count, const char *desc,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800796 struct sram_piece *pfree_head,
797 struct sram_piece *pused_head)
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800798{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800799 struct sram_piece *pslot;
800
801 if (!pfree_head || !pused_head)
802 return -1;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800803
Sonic Zhang262c3822008-07-19 15:42:41 +0800804 *len += sprintf(&buf[*len], "--- SRAM %-14s Size PID State \n", desc);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800805
806 /* search the relevant memory slot */
807 pslot = pused_head->next;
808
809 while (pslot != NULL) {
Sonic Zhang262c3822008-07-19 15:42:41 +0800810 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
Sonic Zhang5d481f42008-07-19 14:51:31 +0800811 pslot->paddr, pslot->paddr + pslot->size,
812 pslot->size, pslot->pid, "ALLOCATED");
813
814 pslot = pslot->next;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800815 }
Sonic Zhang5d481f42008-07-19 14:51:31 +0800816
817 pslot = pfree_head->next;
818
819 while (pslot != NULL) {
Sonic Zhang262c3822008-07-19 15:42:41 +0800820 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
Sonic Zhang5d481f42008-07-19 14:51:31 +0800821 pslot->paddr, pslot->paddr + pslot->size,
822 pslot->size, pslot->pid, "FREE");
823
824 pslot = pslot->next;
825 }
826
827 return 0;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800828}
Sonic Zhang262c3822008-07-19 15:42:41 +0800829static int sram_proc_read(char *buf, char **start, off_t offset, int count,
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800830 int *eof, void *data)
831{
832 int len = 0;
Graf Yang8f658732008-11-18 17:48:22 +0800833 unsigned int cpu;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800834
Graf Yang8f658732008-11-18 17:48:22 +0800835 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
836 if (_sram_proc_read(buf, &len, count, "Scratchpad",
837 &per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu)))
838 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800839#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800840 if (_sram_proc_read(buf, &len, count, "L1 Data A",
841 &per_cpu(free_l1_data_A_sram_head, cpu),
842 &per_cpu(used_l1_data_A_sram_head, cpu)))
843 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800844#endif
845#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800846 if (_sram_proc_read(buf, &len, count, "L1 Data B",
847 &per_cpu(free_l1_data_B_sram_head, cpu),
848 &per_cpu(used_l1_data_B_sram_head, cpu)))
849 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800850#endif
851#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800852 if (_sram_proc_read(buf, &len, count, "L1 Instruction",
853 &per_cpu(free_l1_inst_sram_head, cpu),
854 &per_cpu(used_l1_inst_sram_head, cpu)))
855 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800856#endif
Graf Yang8f658732008-11-18 17:48:22 +0800857 }
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800858#if L2_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800859 if (_sram_proc_read(buf, &len, count, "L2", &free_l2_sram_head,
860 &used_l2_sram_head))
Sonic Zhang262c3822008-07-19 15:42:41 +0800861 goto not_done;
862#endif
Mike Frysinger260d5d32008-07-14 16:34:05 +0800863 *eof = 1;
864 not_done:
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800865 return len;
866}
867
Sonic Zhang262c3822008-07-19 15:42:41 +0800868static int __init sram_proc_init(void)
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800869{
870 struct proc_dir_entry *ptr;
871 ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
872 if (!ptr) {
873 printk(KERN_WARNING "unable to create /proc/sram\n");
874 return -1;
875 }
Sonic Zhang262c3822008-07-19 15:42:41 +0800876 ptr->read_proc = sram_proc_read;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800877 return 0;
878}
Sonic Zhang262c3822008-07-19 15:42:41 +0800879late_initcall(sram_proc_init);
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800880#endif