blob: 99e4dbb1dfd12502becc3d2506f3d51a1076433d [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
Tejun Heob9bf3122009-06-24 15:13:47 +090045static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1sram_lock);
46static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_data_sram_lock);
47static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_inst_sram_lock);
Graf Yang8f658732008-11-18 17:48:22 +080048static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp;
Bryan Wu1394f032007-05-06 14:50:22 -070049
50/* the data structure for L1 scratchpad and DATA SRAM */
Sonic Zhang5d481f42008-07-19 14:51:31 +080051struct sram_piece {
Bryan Wu1394f032007-05-06 14:50:22 -070052 void *paddr;
53 int size;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +080054 pid_t pid;
Sonic Zhang5d481f42008-07-19 14:51:31 +080055 struct sram_piece *next;
Bryan Wu1394f032007-05-06 14:50:22 -070056};
57
Graf Yang8f658732008-11-18 17:48:22 +080058static DEFINE_PER_CPU(struct sram_piece, free_l1_ssram_head);
59static DEFINE_PER_CPU(struct sram_piece, used_l1_ssram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070060
61#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +080062static DEFINE_PER_CPU(struct sram_piece, free_l1_data_A_sram_head);
63static DEFINE_PER_CPU(struct sram_piece, used_l1_data_A_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070064#endif
65
66#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +080067static DEFINE_PER_CPU(struct sram_piece, free_l1_data_B_sram_head);
68static DEFINE_PER_CPU(struct sram_piece, used_l1_data_B_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070069#endif
70
71#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +080072static DEFINE_PER_CPU(struct sram_piece, free_l1_inst_sram_head);
73static DEFINE_PER_CPU(struct sram_piece, used_l1_inst_sram_head);
Bryan Wu1394f032007-05-06 14:50:22 -070074#endif
75
Mike Frysinger07aa7be2008-08-13 16:16:11 +080076#if L2_LENGTH != 0
Sonic Zhang262c3822008-07-19 15:42:41 +080077static struct sram_piece free_l2_sram_head, used_l2_sram_head;
78#endif
79
Sonic Zhang5d481f42008-07-19 14:51:31 +080080static struct kmem_cache *sram_piece_cache;
Bryan Wu1394f032007-05-06 14:50:22 -070081
Sonic Zhang5d481f42008-07-19 14:51:31 +080082/* L1 Scratchpad SRAM initialization function */
83static void __init l1sram_init(void)
84{
Graf Yang8f658732008-11-18 17:48:22 +080085 unsigned int cpu;
Graf Yang89ecd502009-05-25 06:40:42 +000086 unsigned long reserve;
87
88#ifdef CONFIG_SMP
89 reserve = 0;
90#else
91 reserve = sizeof(struct l1_scratch_task_info);
92#endif
93
Graf Yang8f658732008-11-18 17:48:22 +080094 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
95 per_cpu(free_l1_ssram_head, cpu).next =
96 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
97 if (!per_cpu(free_l1_ssram_head, cpu).next) {
98 printk(KERN_INFO "Fail to initialize Scratchpad data SRAM.\n");
99 return;
100 }
101
Graf Yang89ecd502009-05-25 06:40:42 +0000102 per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu) + reserve;
103 per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH - reserve;
Graf Yang8f658732008-11-18 17:48:22 +0800104 per_cpu(free_l1_ssram_head, cpu).next->pid = 0;
105 per_cpu(free_l1_ssram_head, cpu).next->next = NULL;
106
107 per_cpu(used_l1_ssram_head, cpu).next = NULL;
108
109 /* mutex initialize */
110 spin_lock_init(&per_cpu(l1sram_lock, cpu));
111 printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
112 L1_SCRATCH_LENGTH >> 10);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800113 }
Bryan Wu1394f032007-05-06 14:50:22 -0700114}
115
Sonic Zhang5d481f42008-07-19 14:51:31 +0800116static void __init l1_data_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700117{
Mike Frysinger0b82e272008-11-18 17:48:22 +0800118#if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800119 unsigned int cpu;
Mike Frysinger0b82e272008-11-18 17:48:22 +0800120#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700121#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800122 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
123 per_cpu(free_l1_data_A_sram_head, cpu).next =
124 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
125 if (!per_cpu(free_l1_data_A_sram_head, cpu).next) {
126 printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n");
127 return;
128 }
129
130 per_cpu(free_l1_data_A_sram_head, cpu).next->paddr =
131 (void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1);
132 per_cpu(free_l1_data_A_sram_head, cpu).next->size =
133 L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
134 per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0;
135 per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL;
136
137 per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;
138
139 printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n",
140 L1_DATA_A_LENGTH >> 10,
141 per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800142 }
Bryan Wu1394f032007-05-06 14:50:22 -0700143#endif
144#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800145 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
146 per_cpu(free_l1_data_B_sram_head, cpu).next =
147 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
148 if (!per_cpu(free_l1_data_B_sram_head, cpu).next) {
149 printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n");
150 return;
151 }
152
153 per_cpu(free_l1_data_B_sram_head, cpu).next->paddr =
154 (void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1);
155 per_cpu(free_l1_data_B_sram_head, cpu).next->size =
156 L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
157 per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0;
158 per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL;
159
160 per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;
161
162 printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n",
163 L1_DATA_B_LENGTH >> 10,
164 per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10);
165 /* mutex initialize */
Sonic Zhang5d481f42008-07-19 14:51:31 +0800166 }
Bryan Wu1394f032007-05-06 14:50:22 -0700167#endif
168
Graf Yang8f658732008-11-18 17:48:22 +0800169#if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
170 for (cpu = 0; cpu < num_possible_cpus(); ++cpu)
171 spin_lock_init(&per_cpu(l1_data_sram_lock, cpu));
172#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700173}
174
Sonic Zhang5d481f42008-07-19 14:51:31 +0800175static void __init l1_inst_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700176{
177#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800178 unsigned int cpu;
179 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
180 per_cpu(free_l1_inst_sram_head, cpu).next =
181 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
182 if (!per_cpu(free_l1_inst_sram_head, cpu).next) {
183 printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n");
184 return;
185 }
186
187 per_cpu(free_l1_inst_sram_head, cpu).next->paddr =
188 (void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1);
189 per_cpu(free_l1_inst_sram_head, cpu).next->size =
190 L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
191 per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0;
192 per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL;
193
194 per_cpu(used_l1_inst_sram_head, cpu).next = NULL;
195
196 printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n",
197 L1_CODE_LENGTH >> 10,
198 per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10);
199
200 /* mutex initialize */
201 spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
Sonic Zhang5d481f42008-07-19 14:51:31 +0800202 }
Bryan Wu1394f032007-05-06 14:50:22 -0700203#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700204}
205
Sonic Zhang262c3822008-07-19 15:42:41 +0800206static void __init l2_sram_init(void)
207{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800208#if L2_LENGTH != 0
Sonic Zhang262c3822008-07-19 15:42:41 +0800209 free_l2_sram_head.next =
210 kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
211 if (!free_l2_sram_head.next) {
Graf Yang8f658732008-11-18 17:48:22 +0800212 printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
Sonic Zhang262c3822008-07-19 15:42:41 +0800213 return;
214 }
215
Jie Zhangb2c2f302008-10-28 15:57:49 +0800216 free_l2_sram_head.next->paddr =
217 (void *)L2_START + (_ebss_l2 - _stext_l2);
218 free_l2_sram_head.next->size =
219 L2_LENGTH - (_ebss_l2 - _stext_l2);
Sonic Zhang262c3822008-07-19 15:42:41 +0800220 free_l2_sram_head.next->pid = 0;
221 free_l2_sram_head.next->next = NULL;
222
223 used_l2_sram_head.next = NULL;
224
225 printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n",
226 L2_LENGTH >> 10,
227 free_l2_sram_head.next->size >> 10);
228#endif
229
230 /* mutex initialize */
231 spin_lock_init(&l2_sram_lock);
232}
Graf Yang8f658732008-11-18 17:48:22 +0800233
Graf Yangc72aa072009-05-25 04:44:00 +0000234static int __init bfin_sram_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700235{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800236 sram_piece_cache = kmem_cache_create("sram_piece_cache",
237 sizeof(struct sram_piece),
238 0, SLAB_PANIC, NULL);
Bryan Wu1394f032007-05-06 14:50:22 -0700239
Sonic Zhang5d481f42008-07-19 14:51:31 +0800240 l1sram_init();
241 l1_data_sram_init();
242 l1_inst_sram_init();
Sonic Zhang262c3822008-07-19 15:42:41 +0800243 l2_sram_init();
Graf Yangc72aa072009-05-25 04:44:00 +0000244
245 return 0;
Sonic Zhang5d481f42008-07-19 14:51:31 +0800246}
Graf Yangc72aa072009-05-25 04:44:00 +0000247pure_initcall(bfin_sram_init);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800248
Sonic Zhang262c3822008-07-19 15:42:41 +0800249/* SRAM allocate function */
250static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800251 struct sram_piece *pused_head)
252{
253 struct sram_piece *pslot, *plast, *pavail;
254
255 if (size <= 0 || !pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700256 return NULL;
257
258 /* Align the size */
259 size = (size + 3) & ~3;
260
Sonic Zhang5d481f42008-07-19 14:51:31 +0800261 pslot = pfree_head->next;
262 plast = pfree_head;
263
264 /* search an available piece slot */
265 while (pslot != NULL && size > pslot->size) {
266 plast = pslot;
267 pslot = pslot->next;
Bryan Wu1394f032007-05-06 14:50:22 -0700268 }
Sonic Zhang5d481f42008-07-19 14:51:31 +0800269
270 if (!pslot)
Bryan Wu1394f032007-05-06 14:50:22 -0700271 return NULL;
272
Sonic Zhang5d481f42008-07-19 14:51:31 +0800273 if (pslot->size == size) {
274 plast->next = pslot->next;
275 pavail = pslot;
276 } else {
277 pavail = kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
278
279 if (!pavail)
280 return NULL;
281
282 pavail->paddr = pslot->paddr;
283 pavail->size = size;
284 pslot->paddr += size;
285 pslot->size -= size;
Bryan Wu1394f032007-05-06 14:50:22 -0700286 }
287
Sonic Zhang5d481f42008-07-19 14:51:31 +0800288 pavail->pid = current->pid;
289
290 pslot = pused_head->next;
291 plast = pused_head;
292
293 /* insert new piece into used piece list !!! */
294 while (pslot != NULL && pavail->paddr < pslot->paddr) {
295 plast = pslot;
296 pslot = pslot->next;
297 }
298
299 pavail->next = pslot;
300 plast->next = pavail;
301
302 return pavail->paddr;
Bryan Wu1394f032007-05-06 14:50:22 -0700303}
304
305/* Allocate the largest available block. */
Sonic Zhang262c3822008-07-19 15:42:41 +0800306static void *_sram_alloc_max(struct sram_piece *pfree_head,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800307 struct sram_piece *pused_head,
Bryan Wu1394f032007-05-06 14:50:22 -0700308 unsigned long *psize)
309{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800310 struct sram_piece *pslot, *pmax;
Bryan Wu1394f032007-05-06 14:50:22 -0700311
Sonic Zhang5d481f42008-07-19 14:51:31 +0800312 if (!pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700313 return NULL;
Bryan Wu1394f032007-05-06 14:50:22 -0700314
Sonic Zhang5d481f42008-07-19 14:51:31 +0800315 pmax = pslot = pfree_head->next;
316
317 /* search an available piece slot */
318 while (pslot != NULL) {
319 if (pslot->size > pmax->size)
320 pmax = pslot;
321 pslot = pslot->next;
322 }
323
324 if (!pmax)
325 return NULL;
326
327 *psize = pmax->size;
328
Sonic Zhang262c3822008-07-19 15:42:41 +0800329 return _sram_alloc(*psize, pfree_head, pused_head);
Bryan Wu1394f032007-05-06 14:50:22 -0700330}
331
Sonic Zhang262c3822008-07-19 15:42:41 +0800332/* SRAM free function */
333static int _sram_free(const void *addr,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800334 struct sram_piece *pfree_head,
335 struct sram_piece *pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700336{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800337 struct sram_piece *pslot, *plast, *pavail;
Bryan Wu1394f032007-05-06 14:50:22 -0700338
Sonic Zhang5d481f42008-07-19 14:51:31 +0800339 if (!pfree_head || !pused_head)
Bryan Wu1394f032007-05-06 14:50:22 -0700340 return -1;
341
Sonic Zhang5d481f42008-07-19 14:51:31 +0800342 /* search the relevant memory slot */
343 pslot = pused_head->next;
344 plast = pused_head;
Bryan Wu1394f032007-05-06 14:50:22 -0700345
Sonic Zhang5d481f42008-07-19 14:51:31 +0800346 /* search an available piece slot */
347 while (pslot != NULL && pslot->paddr != addr) {
348 plast = pslot;
349 pslot = pslot->next;
Bryan Wu1394f032007-05-06 14:50:22 -0700350 }
351
Sonic Zhang5d481f42008-07-19 14:51:31 +0800352 if (!pslot)
353 return -1;
354
355 plast->next = pslot->next;
356 pavail = pslot;
357 pavail->pid = 0;
358
359 /* insert free pieces back to the free list */
360 pslot = pfree_head->next;
361 plast = pfree_head;
362
363 while (pslot != NULL && addr > pslot->paddr) {
364 plast = pslot;
365 pslot = pslot->next;
366 }
367
368 if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
369 plast->size += pavail->size;
370 kmem_cache_free(sram_piece_cache, pavail);
371 } else {
Sonic Zhang225f7e12008-08-25 18:00:45 +0800372 pavail->next = plast->next;
Sonic Zhang5d481f42008-07-19 14:51:31 +0800373 plast->next = pavail;
374 plast = pavail;
375 }
376
377 if (pslot && plast->paddr + plast->size == pslot->paddr) {
378 plast->size += pslot->size;
379 plast->next = pslot->next;
380 kmem_cache_free(sram_piece_cache, pslot);
Bryan Wu1394f032007-05-06 14:50:22 -0700381 }
382
383 return 0;
384}
385
386int sram_free(const void *addr)
387{
Robin Getz5e953202008-10-08 17:22:49 +0800388
Bryan Wu1394f032007-05-06 14:50:22 -0700389#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800390 if (addr >= (void *)get_l1_code_start()
391 && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700392 return l1_inst_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800393 else
Bryan Wu1394f032007-05-06 14:50:22 -0700394#endif
395#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800396 if (addr >= (void *)get_l1_data_a_start()
397 && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700398 return l1_data_A_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800399 else
Bryan Wu1394f032007-05-06 14:50:22 -0700400#endif
401#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800402 if (addr >= (void *)get_l1_data_b_start()
403 && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH))
Bryan Wu1394f032007-05-06 14:50:22 -0700404 return l1_data_B_sram_free(addr);
Robin Getz5e953202008-10-08 17:22:49 +0800405 else
Bryan Wu1394f032007-05-06 14:50:22 -0700406#endif
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800407#if L2_LENGTH != 0
Robin Getz5e953202008-10-08 17:22:49 +0800408 if (addr >= (void *)L2_START
Sonic Zhang262c3822008-07-19 15:42:41 +0800409 && addr < (void *)(L2_START + L2_LENGTH))
410 return l2_sram_free(addr);
Bryan Wu1394f032007-05-06 14:50:22 -0700411 else
Robin Getz5e953202008-10-08 17:22:49 +0800412#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700413 return -1;
414}
415EXPORT_SYMBOL(sram_free);
416
417void *l1_data_A_sram_alloc(size_t size)
418{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800419 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700420 void *addr = NULL;
Graf Yang8f658732008-11-18 17:48:22 +0800421 unsigned int cpu;
Bryan Wu1394f032007-05-06 14:50:22 -0700422
Graf Yang8f658732008-11-18 17:48:22 +0800423 cpu = get_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700424 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800425 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700426
427#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800428 addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
429 &per_cpu(used_l1_data_A_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700430#endif
431
432 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800433 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
434 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700435
436 pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
437 (long unsigned int)addr, size);
438
439 return addr;
440}
441EXPORT_SYMBOL(l1_data_A_sram_alloc);
442
443int l1_data_A_sram_free(const void *addr)
444{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800445 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700446 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800447 unsigned int cpu;
Bryan Wu1394f032007-05-06 14:50:22 -0700448
Graf Yang8f658732008-11-18 17:48:22 +0800449 cpu = get_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700450 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800451 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700452
453#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800454 ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu),
455 &per_cpu(used_l1_data_A_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700456#else
457 ret = -1;
458#endif
459
460 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800461 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
462 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700463
464 return ret;
465}
466EXPORT_SYMBOL(l1_data_A_sram_free);
467
468void *l1_data_B_sram_alloc(size_t size)
469{
470#if L1_DATA_B_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800471 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700472 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800473 unsigned int cpu;
474
475 cpu = get_cpu();
476 /* add mutex operation */
477 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
478
479 addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
480 &per_cpu(used_l1_data_B_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700481
482 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800483 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
484 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700485
486 pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
487 (long unsigned int)addr, size);
488
489 return addr;
490#else
491 return NULL;
492#endif
493}
494EXPORT_SYMBOL(l1_data_B_sram_alloc);
495
496int l1_data_B_sram_free(const void *addr)
497{
498#if L1_DATA_B_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800499 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700500 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800501 unsigned int cpu;
502
503 cpu = get_cpu();
504 /* add mutex operation */
505 spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
506
507 ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu),
508 &per_cpu(used_l1_data_B_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700509
510 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800511 spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
512 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700513
514 return ret;
515#else
516 return -1;
517#endif
518}
519EXPORT_SYMBOL(l1_data_B_sram_free);
520
521void *l1_data_sram_alloc(size_t size)
522{
523 void *addr = l1_data_A_sram_alloc(size);
524
525 if (!addr)
526 addr = l1_data_B_sram_alloc(size);
527
528 return addr;
529}
530EXPORT_SYMBOL(l1_data_sram_alloc);
531
532void *l1_data_sram_zalloc(size_t size)
533{
534 void *addr = l1_data_sram_alloc(size);
535
536 if (addr)
537 memset(addr, 0x00, size);
538
539 return addr;
540}
541EXPORT_SYMBOL(l1_data_sram_zalloc);
542
543int l1_data_sram_free(const void *addr)
544{
545 int ret;
546 ret = l1_data_A_sram_free(addr);
547 if (ret == -1)
548 ret = l1_data_B_sram_free(addr);
549 return ret;
550}
551EXPORT_SYMBOL(l1_data_sram_free);
552
553void *l1_inst_sram_alloc(size_t size)
554{
Meihui Fanc5b50df2008-04-23 08:55:26 +0800555#if L1_CODE_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800556 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700557 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800558 unsigned int cpu;
559
560 cpu = get_cpu();
561 /* add mutex operation */
562 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
563
564 addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
565 &per_cpu(used_l1_inst_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700566
567 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800568 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
569 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700570
571 pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
572 (long unsigned int)addr, size);
573
574 return addr;
575#else
576 return NULL;
577#endif
578}
579EXPORT_SYMBOL(l1_inst_sram_alloc);
580
581int l1_inst_sram_free(const void *addr)
582{
583#if L1_CODE_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800584 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700585 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800586 unsigned int cpu;
587
588 cpu = get_cpu();
589 /* add mutex operation */
590 spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
591
592 ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
593 &per_cpu(used_l1_inst_sram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700594
595 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800596 spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
597 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700598
599 return ret;
600#else
601 return -1;
602#endif
603}
604EXPORT_SYMBOL(l1_inst_sram_free);
605
606/* L1 Scratchpad memory allocate function */
607void *l1sram_alloc(size_t size)
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
613 cpu = get_cpu();
614 /* add mutex operation */
615 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
616
617 addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu),
618 &per_cpu(used_l1_ssram_head, cpu));
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);
622 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700623
624 return addr;
625}
626
627/* L1 Scratchpad memory allocate function */
628void *l1sram_alloc_max(size_t *psize)
629{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800630 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700631 void *addr;
Graf Yang8f658732008-11-18 17:48:22 +0800632 unsigned int cpu;
633
634 cpu = get_cpu();
635 /* add mutex operation */
636 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
637
638 addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
639 &per_cpu(used_l1_ssram_head, cpu), psize);
Bryan Wu1394f032007-05-06 14:50:22 -0700640
641 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800642 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
643 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700644
645 return addr;
646}
647
648/* L1 Scratchpad memory free function */
649int l1sram_free(const void *addr)
650{
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800651 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700652 int ret;
Graf Yang8f658732008-11-18 17:48:22 +0800653 unsigned int cpu;
654
655 cpu = get_cpu();
656 /* add mutex operation */
657 spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
658
659 ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
660 &per_cpu(used_l1_ssram_head, cpu));
Bryan Wu1394f032007-05-06 14:50:22 -0700661
662 /* add mutex operation */
Graf Yang8f658732008-11-18 17:48:22 +0800663 spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
664 put_cpu();
Bryan Wu1394f032007-05-06 14:50:22 -0700665
666 return ret;
667}
668
Sonic Zhang262c3822008-07-19 15:42:41 +0800669void *l2_sram_alloc(size_t size)
670{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800671#if L2_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800672 unsigned long flags;
Sonic Zhang262c3822008-07-19 15:42:41 +0800673 void *addr;
674
675 /* add mutex operation */
676 spin_lock_irqsave(&l2_sram_lock, flags);
677
678 addr = _sram_alloc(size, &free_l2_sram_head,
679 &used_l2_sram_head);
680
681 /* add mutex operation */
682 spin_unlock_irqrestore(&l2_sram_lock, flags);
683
684 pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
685 (long unsigned int)addr, size);
686
687 return addr;
688#else
689 return NULL;
690#endif
691}
692EXPORT_SYMBOL(l2_sram_alloc);
693
694void *l2_sram_zalloc(size_t size)
695{
696 void *addr = l2_sram_alloc(size);
697
698 if (addr)
699 memset(addr, 0x00, size);
700
701 return addr;
702}
703EXPORT_SYMBOL(l2_sram_zalloc);
704
705int l2_sram_free(const void *addr)
706{
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800707#if L2_LENGTH != 0
Vegard Nossum226a6ec2008-08-28 17:28:46 +0800708 unsigned long flags;
Sonic Zhang262c3822008-07-19 15:42:41 +0800709 int ret;
710
711 /* add mutex operation */
712 spin_lock_irqsave(&l2_sram_lock, flags);
713
714 ret = _sram_free(addr, &free_l2_sram_head,
715 &used_l2_sram_head);
716
717 /* add mutex operation */
718 spin_unlock_irqrestore(&l2_sram_lock, flags);
719
720 return ret;
721#else
722 return -1;
723#endif
724}
725EXPORT_SYMBOL(l2_sram_free);
726
Bryan Wu1394f032007-05-06 14:50:22 -0700727int sram_free_with_lsl(const void *addr)
728{
729 struct sram_list_struct *lsl, **tmp;
730 struct mm_struct *mm = current->mm;
731
732 for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
733 if ((*tmp)->addr == addr)
734 goto found;
735 return -1;
736found:
737 lsl = *tmp;
738 sram_free(addr);
739 *tmp = lsl->next;
740 kfree(lsl);
741
742 return 0;
743}
744EXPORT_SYMBOL(sram_free_with_lsl);
745
Mike Frysingerf1db88d2009-06-02 08:46:35 +0000746/* Allocate memory and keep in L1 SRAM List (lsl) so that the resources are
747 * tracked. These are designed for userspace so that when a process exits,
748 * we can safely reap their resources.
749 */
Bryan Wu1394f032007-05-06 14:50:22 -0700750void *sram_alloc_with_lsl(size_t size, unsigned long flags)
751{
752 void *addr = NULL;
753 struct sram_list_struct *lsl = NULL;
754 struct mm_struct *mm = current->mm;
755
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700756 lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
Bryan Wu1394f032007-05-06 14:50:22 -0700757 if (!lsl)
758 return NULL;
Bryan Wu1394f032007-05-06 14:50:22 -0700759
760 if (flags & L1_INST_SRAM)
761 addr = l1_inst_sram_alloc(size);
762
763 if (addr == NULL && (flags & L1_DATA_A_SRAM))
764 addr = l1_data_A_sram_alloc(size);
765
766 if (addr == NULL && (flags & L1_DATA_B_SRAM))
767 addr = l1_data_B_sram_alloc(size);
768
Sonic Zhang262c3822008-07-19 15:42:41 +0800769 if (addr == NULL && (flags & L2_SRAM))
770 addr = l2_sram_alloc(size);
771
Bryan Wu1394f032007-05-06 14:50:22 -0700772 if (addr == NULL) {
773 kfree(lsl);
774 return NULL;
775 }
776 lsl->addr = addr;
777 lsl->length = size;
778 lsl->next = mm->context.sram_list;
779 mm->context.sram_list = lsl;
780 return addr;
781}
782EXPORT_SYMBOL(sram_alloc_with_lsl);
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800783
784#ifdef CONFIG_PROC_FS
785/* Once we get a real allocator, we'll throw all of this away.
786 * Until then, we need some sort of visibility into the L1 alloc.
787 */
Mike Frysinger260d5d32008-07-14 16:34:05 +0800788/* Need to keep line of output the same. Currently, that is 44 bytes
789 * (including newline).
790 */
Sonic Zhang262c3822008-07-19 15:42:41 +0800791static int _sram_proc_read(char *buf, int *len, int count, const char *desc,
Sonic Zhang5d481f42008-07-19 14:51:31 +0800792 struct sram_piece *pfree_head,
793 struct sram_piece *pused_head)
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800794{
Sonic Zhang5d481f42008-07-19 14:51:31 +0800795 struct sram_piece *pslot;
796
797 if (!pfree_head || !pused_head)
798 return -1;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800799
Sonic Zhang262c3822008-07-19 15:42:41 +0800800 *len += sprintf(&buf[*len], "--- SRAM %-14s Size PID State \n", desc);
Sonic Zhang5d481f42008-07-19 14:51:31 +0800801
802 /* search the relevant memory slot */
803 pslot = pused_head->next;
804
805 while (pslot != NULL) {
Sonic Zhang262c3822008-07-19 15:42:41 +0800806 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
Sonic Zhang5d481f42008-07-19 14:51:31 +0800807 pslot->paddr, pslot->paddr + pslot->size,
808 pslot->size, pslot->pid, "ALLOCATED");
809
810 pslot = pslot->next;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800811 }
Sonic Zhang5d481f42008-07-19 14:51:31 +0800812
813 pslot = pfree_head->next;
814
815 while (pslot != NULL) {
Sonic Zhang262c3822008-07-19 15:42:41 +0800816 *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
Sonic Zhang5d481f42008-07-19 14:51:31 +0800817 pslot->paddr, pslot->paddr + pslot->size,
818 pslot->size, pslot->pid, "FREE");
819
820 pslot = pslot->next;
821 }
822
823 return 0;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800824}
Sonic Zhang262c3822008-07-19 15:42:41 +0800825static int sram_proc_read(char *buf, char **start, off_t offset, int count,
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800826 int *eof, void *data)
827{
828 int len = 0;
Graf Yang8f658732008-11-18 17:48:22 +0800829 unsigned int cpu;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800830
Graf Yang8f658732008-11-18 17:48:22 +0800831 for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
832 if (_sram_proc_read(buf, &len, count, "Scratchpad",
833 &per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu)))
834 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800835#if L1_DATA_A_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800836 if (_sram_proc_read(buf, &len, count, "L1 Data A",
837 &per_cpu(free_l1_data_A_sram_head, cpu),
838 &per_cpu(used_l1_data_A_sram_head, cpu)))
839 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800840#endif
841#if L1_DATA_B_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800842 if (_sram_proc_read(buf, &len, count, "L1 Data B",
843 &per_cpu(free_l1_data_B_sram_head, cpu),
844 &per_cpu(used_l1_data_B_sram_head, cpu)))
845 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800846#endif
847#if L1_CODE_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800848 if (_sram_proc_read(buf, &len, count, "L1 Instruction",
849 &per_cpu(free_l1_inst_sram_head, cpu),
850 &per_cpu(used_l1_inst_sram_head, cpu)))
851 goto not_done;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800852#endif
Graf Yang8f658732008-11-18 17:48:22 +0800853 }
Mike Frysinger07aa7be2008-08-13 16:16:11 +0800854#if L2_LENGTH != 0
Graf Yang8f658732008-11-18 17:48:22 +0800855 if (_sram_proc_read(buf, &len, count, "L2", &free_l2_sram_head,
856 &used_l2_sram_head))
Sonic Zhang262c3822008-07-19 15:42:41 +0800857 goto not_done;
858#endif
Mike Frysinger260d5d32008-07-14 16:34:05 +0800859 *eof = 1;
860 not_done:
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800861 return len;
862}
863
Sonic Zhang262c3822008-07-19 15:42:41 +0800864static int __init sram_proc_init(void)
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800865{
866 struct proc_dir_entry *ptr;
867 ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
868 if (!ptr) {
869 printk(KERN_WARNING "unable to create /proc/sram\n");
870 return -1;
871 }
Sonic Zhang262c3822008-07-19 15:42:41 +0800872 ptr->read_proc = sram_proc_read;
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800873 return 0;
874}
Sonic Zhang262c3822008-07-19 15:42:41 +0800875late_initcall(sram_proc_init);
Mike Frysingerbc61b4e2007-06-14 13:21:08 +0800876#endif