blob: 29d98faa1efdacd4a9cf93723fb268ab91dcbc85 [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>
Alexey Dobriyan934fe05b2011-05-14 19:51:54 +030018#include <linux/seq_file.h>
Bryan Wu1394f032007-05-06 14:50:22 -070019#include <linux/spinlock.h>
20#include <linux/rtc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Bryan Wu1394f032007-05-06 14:50:22 -070022#include <asm/blackfin.h>
Graf Yangdbc895f2009-01-07 23:14:39 +080023#include <asm/mem_map.h>
Bryan Wu1394f032007-05-06 14:50:22 -070024#include "blackfin_sram.h"
25
Bryan Wu1394f032007-05-06 14:50:22 -070026/* the data structure for L1 scratchpad and DATA SRAM */
Sonic Zhang5d481f42008-07-19 14:51:31 +080027struct sram_piece {
Bryan Wu1394f032007-05-06 14:50:22 -070028 void *paddr;
29 int size;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +080030 pid_t pid;
Sonic Zhang5d481f42008-07-19 14:51:31 +080031 struct sram_piece *next;
Bryan Wu1394f032007-05-06 14:50:22 -070032};
33
Mike Frysinger81c969a2009-07-01 15:42:13 +000034static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1sram_lock);
Graf Yang8f658732008-11-18 17:48:22 +080035static DEFINE_PER_CPU(struct sram_piece, free_l1_ssram_head);
36static DEFINE_PER_CPU(struct sram_piece, used_l1_ssram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070037
38#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +080039static DEFINE_PER_CPU(struct sram_piece, free_l1_data_A_sram_head);
40static DEFINE_PER_CPU(struct sram_piece, used_l1_data_A_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070041#endif
42
43#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +080044static DEFINE_PER_CPU(struct sram_piece, free_l1_data_B_sram_head);
45static DEFINE_PER_CPU(struct sram_piece, used_l1_data_B_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070046#endif
47
Mike Frysinger81c969a2009-07-01 15:42:13 +000048#if L1_DATA_A_LENGTH || L1_DATA_B_LENGTH
49static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_data_sram_lock);
50#endif
51
Bryan Wu1394f032007-05-06 14:50:22 -070052#if L1_CODE_LENGTH != 0
Mike Frysinger81c969a2009-07-01 15:42:13 +000053static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_inst_sram_lock);
Graf Yang8f658732008-11-18 17:48:22 +080054static DEFINE_PER_CPU(struct sram_piece, free_l1_inst_sram_head);
55static DEFINE_PER_CPU(struct sram_piece, used_l1_inst_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070056#endif
57
Mike Frysinger07aa7be2008-08-13 16:16:11 +080058#if L2_LENGTH != 0
Mike Frysinger81c969a2009-07-01 15:42:13 +000059static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp;
Sonic Zhang262c3822008-07-19 15:42:41 +080060static struct sram_piece free_l2_sram_head, used_l2_sram_head;
61#endif
62
Sonic Zhang5d481f42008-07-19 14:51:31 +080063static struct kmem_cache *sram_piece_cache;
Bryan Wu1394f032007-05-06 14:50:22 -070064
Sonic Zhang5d481f42008-07-19 14:51:31 +080065/* L1 Scratchpad SRAM initialization function */
66static void __init l1sram_init(void)
67{
Graf Yang8f658732008-11-18 17:48:22 +080068 unsigned int cpu;
Graf Yang89ecd502009-05-25 06:40:42 +000069 unsigned long reserve;
70
71#ifdef CONFIG_SMP
72 reserve = 0;
73#else
74 reserve = sizeof(struct l1_scratch_task_info);
75#endif
76
Graf Yang8f658732008-11-18 17:48:22 +080077 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
78 per_cpu(free_l1_ssram_head, cpu).next =
79 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
80 if (!per_cpu(free_l1_ssram_head, cpu).next) {
81 printk(KERN_INFO "Fail to initialize Scratchpad data SRAM.\n");
82 return;
83 }
84
Graf Yang89ecd502009-05-25 06:40:42 +000085 per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu) + reserve;
86 per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH - reserve;
Graf Yang8f658732008-11-18 17:48:22 +080087 per_cpu(free_l1_ssram_head, cpu).next->pid = 0;
88 per_cpu(free_l1_ssram_head, cpu).next->next = NULL;
89
90 per_cpu(used_l1_ssram_head, cpu).next = NULL;
91
92 /* mutex initialize */
93 spin_lock_init(&per_cpu(l1sram_lock, cpu));
94 printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
95 L1_SCRATCH_LENGTH >> 10);
Sonic Zhang5d481f42008-07-19 14:51:31 +080096 }
Bryan Wu1394f032007-05-06 14:50:22 -070097}
98
Sonic Zhang5d481f42008-07-19 14:51:31 +080099static void __init l1_data_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700100{
Mike Frysinger0b82e272008-11-18 17:48:22 +0800101#if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800102 unsigned int cpu;
Mike Frysinger0b82e272008-11-18 17:48:22 +0800103#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700104#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800105 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
106 per_cpu(free_l1_data_A_sram_head, cpu).next =
107 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
108 if (!per_cpu(free_l1_data_A_sram_head, cpu).next) {
109 printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n");
110 return;
111 }
112
113 per_cpu(free_l1_data_A_sram_head, cpu).next->paddr =
114 (void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1);
115 per_cpu(free_l1_data_A_sram_head, cpu).next->size =
116 L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
117 per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0;
118 per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL;
119
120 per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;
121
122 printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n",
123 L1_DATA_A_LENGTH >> 10,
124 per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800125 }
Bryan Wu1394f032007-05-06 14:50:22 -0700126#endif
127#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800128 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
129 per_cpu(free_l1_data_B_sram_head, cpu).next =
130 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
131 if (!per_cpu(free_l1_data_B_sram_head, cpu).next) {
132 printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n");
133 return;
134 }
135
136 per_cpu(free_l1_data_B_sram_head, cpu).next->paddr =
137 (void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1);
138 per_cpu(free_l1_data_B_sram_head, cpu).next->size =
139 L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
140 per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0;
141 per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL;
142
143 per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;
144
145 printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n",
146 L1_DATA_B_LENGTH >> 10,
147 per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10);
148 /* mutex initialize */
Sonic Zhang5d481f42008-07-19 14:51:31 +0800149 }
Bryan Wu1394f032007-05-06 14:50:22 -0700150#endif
151
Graf Yang8f658732008-11-18 17:48:22 +0800152#if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
153 for (cpu = 0; cpu < num_possible_cpus(); ++cpu)
154 spin_lock_init(&per_cpu(l1_data_sram_lock, cpu));
155#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700156}
157
Sonic Zhang5d481f42008-07-19 14:51:31 +0800158static void __init l1_inst_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700159{
160#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800161 unsigned int cpu;
162 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
163 per_cpu(free_l1_inst_sram_head, cpu).next =
164 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
165 if (!per_cpu(free_l1_inst_sram_head, cpu).next) {
166 printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n");
167 return;
168 }
169
170 per_cpu(free_l1_inst_sram_head, cpu).next->paddr =
171 (void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1);
172 per_cpu(free_l1_inst_sram_head, cpu).next->size =
173 L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
174 per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0;
175 per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL;
176
177 per_cpu(used_l1_inst_sram_head, cpu).next = NULL;
178
179 printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n",
180 L1_CODE_LENGTH >> 10,
181 per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10);
182
183 /* mutex initialize */
184 spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
Sonic Zhang5d481f42008-07-19 14:51:31 +0800185 }
Bryan Wu1394f032007-05-06 14:50:22 -0700186#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700187}
188
Sonic Zhang262c3822008-07-19 15:42:41 +0800189static void __init l2_sram_init(void)
190{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800191#if L2_LENGTH != 0
Sonic Zhang262c3822008-07-19 15:42:41 +0800192 free_l2_sram_head.next =
193 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
194 if (!free_l2_sram_head.next) {
Graf Yang8f658732008-11-18 17:48:22 +0800195 printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
Sonic Zhang262c3822008-07-19 15:42:41 +0800196 return;
197 }
198
Jie Zhangb2c2f302008-10-28 15:57:49 +0800199 free_l2_sram_head.next->paddr =
200 (void *)L2_START + (_ebss_l2 - _stext_l2);
201 free_l2_sram_head.next->size =
202 L2_LENGTH - (_ebss_l2 - _stext_l2);
Sonic Zhang262c3822008-07-19 15:42:41 +0800203 free_l2_sram_head.next->pid = 0;
204 free_l2_sram_head.next->next = NULL;
205
206 used_l2_sram_head.next = NULL;
207
208 printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n",
209 L2_LENGTH >> 10,
210 free_l2_sram_head.next->size >> 10);
Sonic Zhang262c3822008-07-19 15:42:41 +0800211
212 /* mutex initialize */
213 spin_lock_init(&l2_sram_lock);
Mike Frysinger81c969a2009-07-01 15:42:13 +0000214#endif
Sonic Zhang262c3822008-07-19 15:42:41 +0800215}
Graf Yang8f658732008-11-18 17:48:22 +0800216
Graf Yangc72aa072009-05-25 04:44:00 +0000217static int __init bfin_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700218{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800219 sram_piece_cache = kmem_cache_create("sram_piece_cache",
220 sizeof(struct sram_piece),
221 0, SLAB_PANIC, NULL);
Bryan Wu1394f032007-05-06 14:50:22 -0700222
Sonic Zhang5d481f42008-07-19 14:51:31 +0800223 l1sram_init();
224 l1_data_sram_init();
225 l1_inst_sram_init();
Sonic Zhang262c3822008-07-19 15:42:41 +0800226 l2_sram_init();
Graf Yangc72aa072009-05-25 04:44:00 +0000227
228 return 0;
Sonic Zhang5d481f42008-07-19 14:51:31 +0800229}
Graf Yangc72aa072009-05-25 04:44:00 +0000230pure_initcall(bfin_sram_init);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800231
Sonic Zhang262c3822008-07-19 15:42:41 +0800232/* SRAM allocate function */
233static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800234 struct sram_piece *pused_head)
235{
236 struct sram_piece *pslot, *plast, *pavail;
237
238 if (size <= 0 || !pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700239 return NULL;
240
241 /* Align the size */
242 size = (size + 3) & ~3;
243
Sonic Zhang5d481f42008-07-19 14:51:31 +0800244 pslot = pfree_head->next;
245 plast = pfree_head;
246
247 /* search an available piece slot */
248 while (pslot != NULL && size > pslot->size) {
249 plast = pslot;
250 pslot = pslot->next;
Bryan Wu1394f032007-05-06 14:50:22 -0700251 }
Sonic Zhang5d481f42008-07-19 14:51:31 +0800252
253 if (!pslot)
Bryan Wu1394f032007-05-06 14:50:22 -0700254 return NULL;
255
Sonic Zhang5d481f42008-07-19 14:51:31 +0800256 if (pslot->size == size) {
257 plast->next = pslot->next;
258 pavail = pslot;
259 } else {
Mike Frysingereb5400b2010-05-11 04:43:19 +0000260 /* use atomic so our L1 allocator can be used atomically */
261 pavail = kmem_cache_alloc(sram_piece_cache, GFP_ATOMIC);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800262
263 if (!pavail)
264 return NULL;
265
266 pavail->paddr = pslot->paddr;
267 pavail->size = size;
268 pslot->paddr += size;
269 pslot->size -= size;
Bryan Wu1394f032007-05-06 14:50:22 -0700270 }
271
Sonic Zhang5d481f42008-07-19 14:51:31 +0800272 pavail->pid = current->pid;
273
274 pslot = pused_head->next;
275 plast = pused_head;
276
277 /* insert new piece into used piece list !!! */
278 while (pslot != NULL && pavail->paddr < pslot->paddr) {
279 plast = pslot;
280 pslot = pslot->next;
281 }
282
283 pavail->next = pslot;
284 plast->next = pavail;
285
286 return pavail->paddr;
Bryan Wu1394f032007-05-06 14:50:22 -0700287}
288
289/* Allocate the largest available block. */
Sonic Zhang262c3822008-07-19 15:42:41 +0800290static void *_sram_alloc_max(struct sram_piece *pfree_head,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800291 struct sram_piece *pused_head,
Bryan Wu1394f032007-05-06 14:50:22 -0700292 unsigned long *psize)
293{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800294 struct sram_piece *pslot, *pmax;
Bryan Wu1394f032007-05-06 14:50:22 -0700295
Sonic Zhang5d481f42008-07-19 14:51:31 +0800296 if (!pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700297 return NULL;
Bryan Wu1394f032007-05-06 14:50:22 -0700298
Sonic Zhang5d481f42008-07-19 14:51:31 +0800299 pmax = pslot = pfree_head->next;
300
301 /* search an available piece slot */
302 while (pslot != NULL) {
303 if (pslot->size > pmax->size)
304 pmax = pslot;
305 pslot = pslot->next;
306 }
307
308 if (!pmax)
309 return NULL;
310
311 *psize = pmax->size;
312
Sonic Zhang262c3822008-07-19 15:42:41 +0800313 return _sram_alloc(*psize, pfree_head, pused_head);
Bryan Wu1394f032007-05-06 14:50:22 -0700314}
315
Sonic Zhang262c3822008-07-19 15:42:41 +0800316/* SRAM free function */
317static int _sram_free(const void *addr,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800318 struct sram_piece *pfree_head,
319 struct sram_piece *pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700320{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800321 struct sram_piece *pslot, *plast, *pavail;
Bryan Wu1394f032007-05-06 14:50:22 -0700322
Sonic Zhang5d481f42008-07-19 14:51:31 +0800323 if (!pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700324 return -1;
325
Sonic Zhang5d481f42008-07-19 14:51:31 +0800326 /* search the relevant memory slot */
327 pslot = pused_head->next;
328 plast = pused_head;
Bryan Wu1394f032007-05-06 14:50:22 -0700329
Sonic Zhang5d481f42008-07-19 14:51:31 +0800330 /* search an available piece slot */
331 while (pslot != NULL && pslot->paddr != addr) {
332 plast = pslot;
333 pslot = pslot->next;
Bryan Wu1394f032007-05-06 14:50:22 -0700334 }
335
Sonic Zhang5d481f42008-07-19 14:51:31 +0800336 if (!pslot)
337 return -1;
338
339 plast->next = pslot->next;
340 pavail = pslot;
341 pavail->pid = 0;
342
343 /* insert free pieces back to the free list */
344 pslot = pfree_head->next;
345 plast = pfree_head;
346
347 while (pslot != NULL && addr > pslot->paddr) {
348 plast = pslot;
349 pslot = pslot->next;
350 }
351
352 if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
353 plast->size += pavail->size;
354 kmem_cache_free(sram_piece_cache, pavail);
355 } else {
Sonic Zhang225f7e12008-08-25 18:00:45 +0800356 pavail->next = plast->next;
Sonic Zhang5d481f42008-07-19 14:51:31 +0800357 plast->next = pavail;
358 plast = pavail;
359 }
360
361 if (pslot && plast->paddr + plast->size == pslot->paddr) {
362 plast->size += pslot->size;
363 plast->next = pslot->next;
364 kmem_cache_free(sram_piece_cache, pslot);
Bryan Wu1394f032007-05-06 14:50:22 -0700365 }
366
367 return 0;
368}
369
370int sram_free(const void *addr)
371{
Robin Getz5e953202008-10-08 17:22:49 +0800372
Bryan Wu1394f032007-05-06 14:50:22 -0700373#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800374 if (addr >= (void *)get_l1_code_start()
375 && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700376 return l1_inst_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800377 else
Bryan Wu1394f032007-05-06 14:50:22 -0700378#endif
379#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800380 if (addr >= (void *)get_l1_data_a_start()
381 && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700382 return l1_data_A_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800383 else
Bryan Wu1394f032007-05-06 14:50:22 -0700384#endif
385#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800386 if (addr >= (void *)get_l1_data_b_start()
387 && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700388 return l1_data_B_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800389 else
Bryan Wu1394f032007-05-06 14:50:22 -0700390#endif
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800391#if L2_LENGTH != 0
Robin Getz5e953202008-10-08 17:22:49 +0800392 if (addr >= (void *)L2_START
Sonic Zhang262c3822008-07-19 15:42:41 +0800393 && addr < (void *)(L2_START + L2_LENGTH))
394 return l2_sram_free(addr);
Bryan Wu1394f032007-05-06 14:50:22 -0700395 else
Robin Getz5e953202008-10-08 17:22:49 +0800396#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700397 return -1;
398}
399EXPORT_SYMBOL(sram_free);
400
401void *l1_data_A_sram_alloc(size_t size)
402{
Mike Frysinger81c969a2009-07-01 15:42:13 +0000403#if L1_DATA_A_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800404 unsigned long flags;
Mike Frysinger81c969a2009-07-01 15:42:13 +0000405 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800406 unsigned int cpu;
Bryan Wu1394f032007-05-06 14:50:22 -0700407
Yi Li54536c52009-12-30 04:04:07 +0000408 cpu = smp_processor_id();
Bryan Wu1394f032007-05-06 14:50:22 -0700409 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800410 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700411
Graf Yang8f658732008-11-18 17:48:22 +0800412 addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
413 &per_cpu(used_l1_data_A_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700414
415 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800416 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700417
418 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
419 (long unsigned int)addr, size);
420
421 return addr;
Mike Frysinger81c969a2009-07-01 15:42:13 +0000422#else
423 return NULL;
424#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700425}
426EXPORT_SYMBOL(l1_data_A_sram_alloc);
427
428int l1_data_A_sram_free(const void *addr)
429{
Mike Frysinger81c969a2009-07-01 15:42:13 +0000430#if L1_DATA_A_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800431 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700432 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800433 unsigned int cpu;
Bryan Wu1394f032007-05-06 14:50:22 -0700434
Yi Li54536c52009-12-30 04:04:07 +0000435 cpu = smp_processor_id();
Bryan Wu1394f032007-05-06 14:50:22 -0700436 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800437 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700438
Graf Yang8f658732008-11-18 17:48:22 +0800439 ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu),
440 &per_cpu(used_l1_data_A_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700441
442 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800443 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700444
445 return ret;
Mike Frysinger81c969a2009-07-01 15:42:13 +0000446#else
447 return -1;
448#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700449}
450EXPORT_SYMBOL(l1_data_A_sram_free);
451
452void *l1_data_B_sram_alloc(size_t size)
453{
454#if L1_DATA_B_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800455 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700456 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800457 unsigned int cpu;
458
Yi Li54536c52009-12-30 04:04:07 +0000459 cpu = smp_processor_id();
Graf Yang8f658732008-11-18 17:48:22 +0800460 /* add mutex operation */
461 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
462
463 addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
464 &per_cpu(used_l1_data_B_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700465
466 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800467 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
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
Yi Li54536c52009-12-30 04:04:07 +0000486 cpu = smp_processor_id();
Graf Yang8f658732008-11-18 17:48:22 +0800487 /* 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);
Bryan Wu1394f032007-05-06 14:50:22 -0700495
496 return ret;
497#else
498 return -1;
499#endif
500}
501EXPORT_SYMBOL(l1_data_B_sram_free);
502
503void *l1_data_sram_alloc(size_t size)
504{
505 void *addr = l1_data_A_sram_alloc(size);
506
507 if (!addr)
508 addr = l1_data_B_sram_alloc(size);
509
510 return addr;
511}
512EXPORT_SYMBOL(l1_data_sram_alloc);
513
514void *l1_data_sram_zalloc(size_t size)
515{
516 void *addr = l1_data_sram_alloc(size);
517
518 if (addr)
519 memset(addr, 0x00, size);
520
521 return addr;
522}
523EXPORT_SYMBOL(l1_data_sram_zalloc);
524
525int l1_data_sram_free(const void *addr)
526{
527 int ret;
528 ret = l1_data_A_sram_free(addr);
529 if (ret == -1)
530 ret = l1_data_B_sram_free(addr);
531 return ret;
532}
533EXPORT_SYMBOL(l1_data_sram_free);
534
535void *l1_inst_sram_alloc(size_t size)
536{
Meihui Fanc5b50df2008-04-23 08:55:26 +0800537#if L1_CODE_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800538 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700539 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800540 unsigned int cpu;
541
Yi Li54536c52009-12-30 04:04:07 +0000542 cpu = smp_processor_id();
Graf Yang8f658732008-11-18 17:48:22 +0800543 /* add mutex operation */
544 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
545
546 addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
547 &per_cpu(used_l1_inst_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700548
549 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800550 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700551
552 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
553 (long unsigned int)addr, size);
554
555 return addr;
556#else
557 return NULL;
558#endif
559}
560EXPORT_SYMBOL(l1_inst_sram_alloc);
561
562int l1_inst_sram_free(const void *addr)
563{
564#if L1_CODE_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800565 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700566 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800567 unsigned int cpu;
568
Yi Li54536c52009-12-30 04:04:07 +0000569 cpu = smp_processor_id();
Graf Yang8f658732008-11-18 17:48:22 +0800570 /* add mutex operation */
571 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
572
573 ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
574 &per_cpu(used_l1_inst_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700575
576 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800577 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700578
579 return ret;
580#else
581 return -1;
582#endif
583}
584EXPORT_SYMBOL(l1_inst_sram_free);
585
586/* L1 Scratchpad memory allocate function */
587void *l1sram_alloc(size_t size)
588{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800589 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700590 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800591 unsigned int cpu;
592
Yi Li54536c52009-12-30 04:04:07 +0000593 cpu = smp_processor_id();
Graf Yang8f658732008-11-18 17:48:22 +0800594 /* add mutex operation */
595 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
596
597 addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu),
598 &per_cpu(used_l1_ssram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700599
600 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800601 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700602
603 return addr;
604}
605
606/* L1 Scratchpad memory allocate function */
607void *l1sram_alloc_max(size_t *psize)
608{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800609 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700610 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800611 unsigned int cpu;
612
Yi Li54536c52009-12-30 04:04:07 +0000613 cpu = smp_processor_id();
Graf Yang8f658732008-11-18 17:48:22 +0800614 /* add mutex operation */
615 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
616
617 addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
618 &per_cpu(used_l1_ssram_head, cpu), psize);
Bryan Wu1394f032007-05-06 14:50:22 -0700619
620 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800621 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700622
623 return addr;
624}
625
626/* L1 Scratchpad memory free function */
627int l1sram_free(const void *addr)
628{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800629 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700630 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800631 unsigned int cpu;
632
Yi Li54536c52009-12-30 04:04:07 +0000633 cpu = smp_processor_id();
Graf Yang8f658732008-11-18 17:48:22 +0800634 /* add mutex operation */
635 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
636
637 ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
638 &per_cpu(used_l1_ssram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700639
640 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800641 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700642
643 return ret;
644}
645
Sonic Zhang262c3822008-07-19 15:42:41 +0800646void *l2_sram_alloc(size_t size)
647{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800648#if L2_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800649 unsigned long flags;
Sonic Zhang262c3822008-07-19 15:42:41 +0800650 void *addr;
651
652 /* add mutex operation */
653 spin_lock_irqsave(&l2_sram_lock, flags);
654
655 addr = _sram_alloc(size, &free_l2_sram_head,
656 &used_l2_sram_head);
657
658 /* add mutex operation */
659 spin_unlock_irqrestore(&l2_sram_lock, flags);
660
661 pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
662 (long unsigned int)addr, size);
663
664 return addr;
665#else
666 return NULL;
667#endif
668}
669EXPORT_SYMBOL(l2_sram_alloc);
670
671void *l2_sram_zalloc(size_t size)
672{
673 void *addr = l2_sram_alloc(size);
674
675 if (addr)
676 memset(addr, 0x00, size);
677
678 return addr;
679}
680EXPORT_SYMBOL(l2_sram_zalloc);
681
682int l2_sram_free(const void *addr)
683{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800684#if L2_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800685 unsigned long flags;
Sonic Zhang262c3822008-07-19 15:42:41 +0800686 int ret;
687
688 /* add mutex operation */
689 spin_lock_irqsave(&l2_sram_lock, flags);
690
691 ret = _sram_free(addr, &free_l2_sram_head,
692 &used_l2_sram_head);
693
694 /* add mutex operation */
695 spin_unlock_irqrestore(&l2_sram_lock, flags);
696
697 return ret;
698#else
699 return -1;
700#endif
701}
702EXPORT_SYMBOL(l2_sram_free);
703
Bryan Wu1394f032007-05-06 14:50:22 -0700704int sram_free_with_lsl(const void *addr)
705{
706 struct sram_list_struct *lsl, **tmp;
707 struct mm_struct *mm = current->mm;
Mike Frysinger25f3ff22010-12-06 21:12:23 +0000708 int ret = -1;
Bryan Wu1394f032007-05-06 14:50:22 -0700709
710 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
Mike Frysinger25f3ff22010-12-06 21:12:23 +0000711 if ((*tmp)->addr == addr) {
712 lsl = *tmp;
713 ret = sram_free(addr);
714 *tmp = lsl->next;
715 kfree(lsl);
716 break;
717 }
Bryan Wu1394f032007-05-06 14:50:22 -0700718
Mike Frysinger25f3ff22010-12-06 21:12:23 +0000719 return ret;
Bryan Wu1394f032007-05-06 14:50:22 -0700720}
721EXPORT_SYMBOL(sram_free_with_lsl);
722
Mike Frysingerf1db88d2009-06-02 08:46:35 +0000723/* Allocate memory and keep in L1 SRAM List (lsl) so that the resources are
724 * tracked. These are designed for userspace so that when a process exits,
725 * we can safely reap their resources.
726 */
Bryan Wu1394f032007-05-06 14:50:22 -0700727void *sram_alloc_with_lsl(size_t size, unsigned long flags)
728{
729 void *addr = NULL;
730 struct sram_list_struct *lsl = NULL;
731 struct mm_struct *mm = current->mm;
732
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700733 lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
Bryan Wu1394f032007-05-06 14:50:22 -0700734 if (!lsl)
735 return NULL;
Bryan Wu1394f032007-05-06 14:50:22 -0700736
737 if (flags & L1_INST_SRAM)
738 addr = l1_inst_sram_alloc(size);
739
740 if (addr == NULL && (flags & L1_DATA_A_SRAM))
741 addr = l1_data_A_sram_alloc(size);
742
743 if (addr == NULL && (flags & L1_DATA_B_SRAM))
744 addr = l1_data_B_sram_alloc(size);
745
Sonic Zhang262c3822008-07-19 15:42:41 +0800746 if (addr == NULL && (flags & L2_SRAM))
747 addr = l2_sram_alloc(size);
748
Bryan Wu1394f032007-05-06 14:50:22 -0700749 if (addr == NULL) {
750 kfree(lsl);
751 return NULL;
752 }
753 lsl->addr = addr;
754 lsl->length = size;
755 lsl->next = mm->context.sram_list;
756 mm->context.sram_list = lsl;
757 return addr;
758}
759EXPORT_SYMBOL(sram_alloc_with_lsl);
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800760
761#ifdef CONFIG_PROC_FS
762/* Once we get a real allocator, we'll throw all of this away.
763 * Until then, we need some sort of visibility into the L1 alloc.
764 */
Mike Frysinger260d5d32008-07-14 16:34:05 +0800765/* Need to keep line of output the same. Currently, that is 44 bytes
766 * (including newline).
767 */
Alexey Dobriyan934fe05b2011-05-14 19:51:54 +0300768static int _sram_proc_show(struct seq_file *m, const char *desc,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800769 struct sram_piece *pfree_head,
770 struct sram_piece *pused_head)
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800771{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800772 struct sram_piece *pslot;
773
774 if (!pfree_head || !pused_head)
775 return -1;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800776
Alexey Dobriyan934fe05b2011-05-14 19:51:54 +0300777 seq_printf(m, "--- SRAM %-14s Size PID State \n", desc);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800778
779 /* search the relevant memory slot */
780 pslot = pused_head->next;
781
782 while (pslot != NULL) {
Alexey Dobriyan934fe05b2011-05-14 19:51:54 +0300783 seq_printf(m, "%p-%p %10i %5i %-10s\n",
Sonic Zhang5d481f42008-07-19 14:51:31 +0800784 pslot->paddr, pslot->paddr + pslot->size,
785 pslot->size, pslot->pid, "ALLOCATED");
786
787 pslot = pslot->next;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800788 }
Sonic Zhang5d481f42008-07-19 14:51:31 +0800789
790 pslot = pfree_head->next;
791
792 while (pslot != NULL) {
Alexey Dobriyan934fe05b2011-05-14 19:51:54 +0300793 seq_printf(m, "%p-%p %10i %5i %-10s\n",
Sonic Zhang5d481f42008-07-19 14:51:31 +0800794 pslot->paddr, pslot->paddr + pslot->size,
795 pslot->size, pslot->pid, "FREE");
796
797 pslot = pslot->next;
798 }
799
800 return 0;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800801}
Alexey Dobriyan934fe05b2011-05-14 19:51:54 +0300802static int sram_proc_show(struct seq_file *m, void *v)
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800803{
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) {
Alexey Dobriyan934fe05b2011-05-14 19:51:54 +0300807 if (_sram_proc_show(m, "Scratchpad",
Graf Yang8f658732008-11-18 17:48:22 +0800808 &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
Alexey Dobriyan934fe05b2011-05-14 19:51:54 +0300811 if (_sram_proc_show(m, "L1 Data A",
Graf Yang8f658732008-11-18 17:48:22 +0800812 &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
Alexey Dobriyan934fe05b2011-05-14 19:51:54 +0300817 if (_sram_proc_show(m, "L1 Data B",
Graf Yang8f658732008-11-18 17:48:22 +0800818 &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
Alexey Dobriyan934fe05b2011-05-14 19:51:54 +0300823 if (_sram_proc_show(m, "L1 Instruction",
Graf Yang8f658732008-11-18 17:48:22 +0800824 &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
Alexey Dobriyan934fe05b2011-05-14 19:51:54 +0300830 if (_sram_proc_show(m, "L2", &free_l2_sram_head, &used_l2_sram_head))
Sonic Zhang262c3822008-07-19 15:42:41 +0800831 goto not_done;
832#endif
Mike Frysinger260d5d32008-07-14 16:34:05 +0800833 not_done:
Alexey Dobriyan934fe05b2011-05-14 19:51:54 +0300834 return 0;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800835}
836
Alexey Dobriyan934fe05b2011-05-14 19:51:54 +0300837static int sram_proc_open(struct inode *inode, struct file *file)
838{
839 return single_open(file, sram_proc_show, NULL);
840}
841
842static const struct file_operations sram_proc_ops = {
843 .open = sram_proc_open,
844 .read = seq_read,
845 .llseek = seq_lseek,
846 .release = single_release,
847};
848
Sonic Zhang262c3822008-07-19 15:42:41 +0800849static int __init sram_proc_init(void)
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800850{
851 struct proc_dir_entry *ptr;
Alexey Dobriyan934fe05b2011-05-14 19:51:54 +0300852
853 ptr = proc_create("sram", S_IRUGO, NULL, &sram_proc_ops);
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800854 if (!ptr) {
855 printk(KERN_WARNING "unable to create /proc/sram\n");
856 return -1;
857 }
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800858 return 0;
859}
Sonic Zhang262c3822008-07-19 15:42:41 +0800860late_initcall(sram_proc_init);
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800861#endif