sram-alloc.c 20.4 KB
Newer Older
Bryan Wu's avatar
Bryan Wu committed
1
/*
2
 * File:         arch/blackfin/mm/sram-alloc.c
Bryan Wu's avatar
Bryan Wu committed
3 4 5 6
 * Based on:
 * Author:
 *
 * Created:
7
 * Description:  SRAM allocator for Blackfin L1 and L2 memory
Bryan Wu's avatar
Bryan Wu committed
8 9
 *
 * Modified:
10
 *               Copyright 2004-2008 Analog Devices Inc.
Bryan Wu's avatar
Bryan Wu committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *
 * Bugs:         Enter bugs at http://blackfin.uclinux.org/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see the file COPYING, or write
 * to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/miscdevice.h>
#include <linux/ioport.h>
#include <linux/fcntl.h>
#include <linux/init.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/spinlock.h>
#include <linux/rtc.h>
#include <asm/blackfin.h>
42
#include <asm/mem_map.h>
Bryan Wu's avatar
Bryan Wu committed
43 44
#include "blackfin_sram.h"

45 46 47 48
static DEFINE_PER_CPU(spinlock_t, l1sram_lock) ____cacheline_aligned_in_smp;
static DEFINE_PER_CPU(spinlock_t, l1_data_sram_lock) ____cacheline_aligned_in_smp;
static DEFINE_PER_CPU(spinlock_t, l1_inst_sram_lock) ____cacheline_aligned_in_smp;
static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp;
Bryan Wu's avatar
Bryan Wu committed
49 50

/* the data structure for L1 scratchpad and DATA SRAM */
51
struct sram_piece {
Bryan Wu's avatar
Bryan Wu committed
52 53
	void *paddr;
	int size;
54
	pid_t pid;
55
	struct sram_piece *next;
Bryan Wu's avatar
Bryan Wu committed
56 57
};

58 59
static DEFINE_PER_CPU(struct sram_piece, free_l1_ssram_head);
static DEFINE_PER_CPU(struct sram_piece, used_l1_ssram_head);
Bryan Wu's avatar
Bryan Wu committed
60 61

#if L1_DATA_A_LENGTH != 0
62 63
static DEFINE_PER_CPU(struct sram_piece, free_l1_data_A_sram_head);
static DEFINE_PER_CPU(struct sram_piece, used_l1_data_A_sram_head);
Bryan Wu's avatar
Bryan Wu committed
64 65 66
#endif

#if L1_DATA_B_LENGTH != 0
67 68
static DEFINE_PER_CPU(struct sram_piece, free_l1_data_B_sram_head);
static DEFINE_PER_CPU(struct sram_piece, used_l1_data_B_sram_head);
Bryan Wu's avatar
Bryan Wu committed
69 70 71
#endif

#if L1_CODE_LENGTH != 0
72 73
static DEFINE_PER_CPU(struct sram_piece, free_l1_inst_sram_head);
static DEFINE_PER_CPU(struct sram_piece, used_l1_inst_sram_head);
Bryan Wu's avatar
Bryan Wu committed
74 75
#endif

76
#if L2_LENGTH != 0
77 78 79
static struct sram_piece free_l2_sram_head, used_l2_sram_head;
#endif

80 81
static struct kmem_cache *sram_piece_cache;

Bryan Wu's avatar
Bryan Wu committed
82
/* L1 Scratchpad SRAM initialization function */
83
static void __init l1sram_init(void)
Bryan Wu's avatar
Bryan Wu committed
84
{
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	unsigned int cpu;
	for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
		per_cpu(free_l1_ssram_head, cpu).next =
			kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
		if (!per_cpu(free_l1_ssram_head, cpu).next) {
			printk(KERN_INFO "Fail to initialize Scratchpad data SRAM.\n");
			return;
		}

		per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu);
		per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH;
		per_cpu(free_l1_ssram_head, cpu).next->pid = 0;
		per_cpu(free_l1_ssram_head, cpu).next->next = NULL;

		per_cpu(used_l1_ssram_head, cpu).next = NULL;

		/* mutex initialize */
		spin_lock_init(&per_cpu(l1sram_lock, cpu));
		printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
			L1_SCRATCH_LENGTH >> 10);
105
	}
Bryan Wu's avatar
Bryan Wu committed
106 107
}

108
static void __init l1_data_sram_init(void)
Bryan Wu's avatar
Bryan Wu committed
109
{
110
#if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
111
	unsigned int cpu;
112
#endif
Bryan Wu's avatar
Bryan Wu committed
113
#if L1_DATA_A_LENGTH != 0
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
		per_cpu(free_l1_data_A_sram_head, cpu).next =
			kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
		if (!per_cpu(free_l1_data_A_sram_head, cpu).next) {
			printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n");
			return;
		}

		per_cpu(free_l1_data_A_sram_head, cpu).next->paddr =
			(void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1);
		per_cpu(free_l1_data_A_sram_head, cpu).next->size =
			L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
		per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0;
		per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL;

		per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;

		printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n",
			L1_DATA_A_LENGTH >> 10,
			per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10);
134
	}
Bryan Wu's avatar
Bryan Wu committed
135 136
#endif
#if L1_DATA_B_LENGTH != 0
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
		per_cpu(free_l1_data_B_sram_head, cpu).next =
			kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
		if (!per_cpu(free_l1_data_B_sram_head, cpu).next) {
			printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n");
			return;
		}

		per_cpu(free_l1_data_B_sram_head, cpu).next->paddr =
			(void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1);
		per_cpu(free_l1_data_B_sram_head, cpu).next->size =
			L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
		per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0;
		per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL;

		per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;

		printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n",
			L1_DATA_B_LENGTH >> 10,
			per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10);
		/* mutex initialize */
158
	}
Bryan Wu's avatar
Bryan Wu committed
159 160
#endif

161 162 163 164
#if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
	for (cpu = 0; cpu < num_possible_cpus(); ++cpu)
		spin_lock_init(&per_cpu(l1_data_sram_lock, cpu));
#endif
Bryan Wu's avatar
Bryan Wu committed
165 166
}

167
static void __init l1_inst_sram_init(void)
Bryan Wu's avatar
Bryan Wu committed
168 169
{
#if L1_CODE_LENGTH != 0
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	unsigned int cpu;
	for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
		per_cpu(free_l1_inst_sram_head, cpu).next =
			kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
		if (!per_cpu(free_l1_inst_sram_head, cpu).next) {
			printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n");
			return;
		}

		per_cpu(free_l1_inst_sram_head, cpu).next->paddr =
			(void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1);
		per_cpu(free_l1_inst_sram_head, cpu).next->size =
			L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
		per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0;
		per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL;

		per_cpu(used_l1_inst_sram_head, cpu).next = NULL;

		printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n",
			L1_CODE_LENGTH >> 10,
			per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10);

		/* mutex initialize */
		spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
194
	}
Bryan Wu's avatar
Bryan Wu committed
195 196 197
#endif
}

198 199
static void __init l2_sram_init(void)
{
200
#if L2_LENGTH != 0
201 202 203
	free_l2_sram_head.next =
		kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
	if (!free_l2_sram_head.next) {
204
		printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
205 206 207
		return;
	}

208 209 210 211
	free_l2_sram_head.next->paddr =
		(void *)L2_START + (_ebss_l2 - _stext_l2);
	free_l2_sram_head.next->size =
		L2_LENGTH - (_ebss_l2 - _stext_l2);
212 213 214 215 216 217 218 219 220 221 222 223 224
	free_l2_sram_head.next->pid = 0;
	free_l2_sram_head.next->next = NULL;

	used_l2_sram_head.next = NULL;

	printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n",
		L2_LENGTH >> 10,
		free_l2_sram_head.next->size >> 10);
#endif

	/* mutex initialize */
	spin_lock_init(&l2_sram_lock);
}
225

226
static int __init bfin_sram_init(void)
227 228 229 230 231 232 233 234
{
	sram_piece_cache = kmem_cache_create("sram_piece_cache",
				sizeof(struct sram_piece),
				0, SLAB_PANIC, NULL);

	l1sram_init();
	l1_data_sram_init();
	l1_inst_sram_init();
235
	l2_sram_init();
236 237

	return 0;
238
}
239
pure_initcall(bfin_sram_init);
240

241 242
/* SRAM allocate function */
static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
243
		struct sram_piece *pused_head)
Bryan Wu's avatar
Bryan Wu committed
244
{
245
	struct sram_piece *pslot, *plast, *pavail;
Bryan Wu's avatar
Bryan Wu committed
246

247
	if (size <= 0 || !pfree_head || !pused_head)
Bryan Wu's avatar
Bryan Wu committed
248 249 250 251 252
		return NULL;

	/* Align the size */
	size = (size + 3) & ~3;

253 254 255 256 257 258 259
	pslot = pfree_head->next;
	plast = pfree_head;

	/* search an available piece slot */
	while (pslot != NULL && size > pslot->size) {
		plast = pslot;
		pslot = pslot->next;
Bryan Wu's avatar
Bryan Wu committed
260
	}
261 262

	if (!pslot)
Bryan Wu's avatar
Bryan Wu committed
263 264
		return NULL;

265 266 267 268 269 270 271 272 273 274 275 276 277
	if (pslot->size == size) {
		plast->next = pslot->next;
		pavail = pslot;
	} else {
		pavail = kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);

		if (!pavail)
			return NULL;

		pavail->paddr = pslot->paddr;
		pavail->size = size;
		pslot->paddr += size;
		pslot->size -= size;
Bryan Wu's avatar
Bryan Wu committed
278 279
	}

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
	pavail->pid = current->pid;

	pslot = pused_head->next;
	plast = pused_head;

	/* insert new piece into used piece list !!! */
	while (pslot != NULL && pavail->paddr < pslot->paddr) {
		plast = pslot;
		pslot = pslot->next;
	}

	pavail->next = pslot;
	plast->next = pavail;

	return pavail->paddr;
Bryan Wu's avatar
Bryan Wu committed
295 296 297
}

/* Allocate the largest available block.  */
298
static void *_sram_alloc_max(struct sram_piece *pfree_head,
299
				struct sram_piece *pused_head,
Bryan Wu's avatar
Bryan Wu committed
300 301
				unsigned long *psize)
{
302 303 304 305 306 307
	struct sram_piece *pslot, *pmax;

	if (!pfree_head || !pused_head)
		return NULL;

	pmax = pslot = pfree_head->next;
Bryan Wu's avatar
Bryan Wu committed
308

309 310 311 312 313
	/* search an available piece slot */
	while (pslot != NULL) {
		if (pslot->size > pmax->size)
			pmax = pslot;
		pslot = pslot->next;
Bryan Wu's avatar
Bryan Wu committed
314
	}
315 316

	if (!pmax)
Bryan Wu's avatar
Bryan Wu committed
317 318
		return NULL;

319 320
	*psize = pmax->size;

321
	return _sram_alloc(*psize, pfree_head, pused_head);
Bryan Wu's avatar
Bryan Wu committed
322 323
}

324 325
/* SRAM free function */
static int _sram_free(const void *addr,
326 327
			struct sram_piece *pfree_head,
			struct sram_piece *pused_head)
Bryan Wu's avatar
Bryan Wu committed
328
{
329 330 331 332
	struct sram_piece *pslot, *plast, *pavail;

	if (!pfree_head || !pused_head)
		return -1;
Bryan Wu's avatar
Bryan Wu committed
333 334

	/* search the relevant memory slot */
335 336 337 338 339 340 341
	pslot = pused_head->next;
	plast = pused_head;

	/* search an available piece slot */
	while (pslot != NULL && pslot->paddr != addr) {
		plast = pslot;
		pslot = pslot->next;
Bryan Wu's avatar
Bryan Wu committed
342
	}
343 344

	if (!pslot)
Bryan Wu's avatar
Bryan Wu committed
345 346
		return -1;

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	plast->next = pslot->next;
	pavail = pslot;
	pavail->pid = 0;

	/* insert free pieces back to the free list */
	pslot = pfree_head->next;
	plast = pfree_head;

	while (pslot != NULL && addr > pslot->paddr) {
		plast = pslot;
		pslot = pslot->next;
	}

	if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
		plast->size += pavail->size;
		kmem_cache_free(sram_piece_cache, pavail);
	} else {
364
		pavail->next = plast->next;
365 366
		plast->next = pavail;
		plast = pavail;
Bryan Wu's avatar
Bryan Wu committed
367 368
	}

369 370 371 372
	if (pslot && plast->paddr + plast->size == pslot->paddr) {
		plast->size += pslot->size;
		plast->next = pslot->next;
		kmem_cache_free(sram_piece_cache, pslot);
Bryan Wu's avatar
Bryan Wu committed
373 374 375 376 377 378 379
	}

	return 0;
}

int sram_free(const void *addr)
{
380

Bryan Wu's avatar
Bryan Wu committed
381
#if L1_CODE_LENGTH != 0
382 383
	if (addr >= (void *)get_l1_code_start()
		 && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
Bryan Wu's avatar
Bryan Wu committed
384
		return l1_inst_sram_free(addr);
385
	else
Bryan Wu's avatar
Bryan Wu committed
386 387
#endif
#if L1_DATA_A_LENGTH != 0
388 389
	if (addr >= (void *)get_l1_data_a_start()
		 && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH))
Bryan Wu's avatar
Bryan Wu committed
390
		return l1_data_A_sram_free(addr);
391
	else
Bryan Wu's avatar
Bryan Wu committed
392 393
#endif
#if L1_DATA_B_LENGTH != 0
394 395
	if (addr >= (void *)get_l1_data_b_start()
		 && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH))
Bryan Wu's avatar
Bryan Wu committed
396
		return l1_data_B_sram_free(addr);
397
	else
398
#endif
399
#if L2_LENGTH != 0
400
	if (addr >= (void *)L2_START
401 402
		 && addr < (void *)(L2_START + L2_LENGTH))
		return l2_sram_free(addr);
Bryan Wu's avatar
Bryan Wu committed
403
	else
404
#endif
Bryan Wu's avatar
Bryan Wu committed
405 406 407 408 409 410
		return -1;
}
EXPORT_SYMBOL(sram_free);

void *l1_data_A_sram_alloc(size_t size)
{
411
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
412
	void *addr = NULL;
413
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
414

415
	cpu = get_cpu();
Bryan Wu's avatar
Bryan Wu committed
416
	/* add mutex operation */
417
	spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
418 419

#if L1_DATA_A_LENGTH != 0
420 421
	addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
			&per_cpu(used_l1_data_A_sram_head, cpu));
Bryan Wu's avatar
Bryan Wu committed
422 423 424
#endif

	/* add mutex operation */
425 426
	spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
	put_cpu();
Bryan Wu's avatar
Bryan Wu committed
427 428 429 430 431 432 433 434 435 436

	pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
		 (long unsigned int)addr, size);

	return addr;
}
EXPORT_SYMBOL(l1_data_A_sram_alloc);

int l1_data_A_sram_free(const void *addr)
{
437
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
438
	int ret;
439
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
440

441
	cpu = get_cpu();
Bryan Wu's avatar
Bryan Wu committed
442
	/* add mutex operation */
443
	spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
444 445

#if L1_DATA_A_LENGTH != 0
446 447
	ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu),
			&per_cpu(used_l1_data_A_sram_head, cpu));
Bryan Wu's avatar
Bryan Wu committed
448 449 450 451 452
#else
	ret = -1;
#endif

	/* add mutex operation */
453 454
	spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
	put_cpu();
Bryan Wu's avatar
Bryan Wu committed
455 456 457 458 459 460 461 462

	return ret;
}
EXPORT_SYMBOL(l1_data_A_sram_free);

void *l1_data_B_sram_alloc(size_t size)
{
#if L1_DATA_B_LENGTH != 0
463
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
464
	void *addr;
465
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
466

467
	cpu = get_cpu();
Bryan Wu's avatar
Bryan Wu committed
468
	/* add mutex operation */
469
	spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
470

471 472
	addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
			&per_cpu(used_l1_data_B_sram_head, cpu));
Bryan Wu's avatar
Bryan Wu committed
473 474

	/* add mutex operation */
475 476
	spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
	put_cpu();
Bryan Wu's avatar
Bryan Wu committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490

	pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
		 (long unsigned int)addr, size);

	return addr;
#else
	return NULL;
#endif
}
EXPORT_SYMBOL(l1_data_B_sram_alloc);

int l1_data_B_sram_free(const void *addr)
{
#if L1_DATA_B_LENGTH != 0
491
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
492
	int ret;
493
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
494

495
	cpu = get_cpu();
Bryan Wu's avatar
Bryan Wu committed
496
	/* add mutex operation */
497
	spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
498

499 500
	ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu),
			&per_cpu(used_l1_data_B_sram_head, cpu));
Bryan Wu's avatar
Bryan Wu committed
501 502

	/* add mutex operation */
503 504
	spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
	put_cpu();
Bryan Wu's avatar
Bryan Wu committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546

	return ret;
#else
	return -1;
#endif
}
EXPORT_SYMBOL(l1_data_B_sram_free);

void *l1_data_sram_alloc(size_t size)
{
	void *addr = l1_data_A_sram_alloc(size);

	if (!addr)
		addr = l1_data_B_sram_alloc(size);

	return addr;
}
EXPORT_SYMBOL(l1_data_sram_alloc);

void *l1_data_sram_zalloc(size_t size)
{
	void *addr = l1_data_sram_alloc(size);

	if (addr)
		memset(addr, 0x00, size);

	return addr;
}
EXPORT_SYMBOL(l1_data_sram_zalloc);

int l1_data_sram_free(const void *addr)
{
	int ret;
	ret = l1_data_A_sram_free(addr);
	if (ret == -1)
		ret = l1_data_B_sram_free(addr);
	return ret;
}
EXPORT_SYMBOL(l1_data_sram_free);

void *l1_inst_sram_alloc(size_t size)
{
547
#if L1_CODE_LENGTH != 0
548
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
549
	void *addr;
550
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
551

552
	cpu = get_cpu();
Bryan Wu's avatar
Bryan Wu committed
553
	/* add mutex operation */
554
	spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
555

556 557
	addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
			&per_cpu(used_l1_inst_sram_head, cpu));
Bryan Wu's avatar
Bryan Wu committed
558 559

	/* add mutex operation */
560 561
	spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
	put_cpu();
Bryan Wu's avatar
Bryan Wu committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575

	pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
		 (long unsigned int)addr, size);

	return addr;
#else
	return NULL;
#endif
}
EXPORT_SYMBOL(l1_inst_sram_alloc);

int l1_inst_sram_free(const void *addr)
{
#if L1_CODE_LENGTH != 0
576
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
577
	int ret;
578
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
579

580
	cpu = get_cpu();
Bryan Wu's avatar
Bryan Wu committed
581
	/* add mutex operation */
582
	spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
583

584 585
	ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
			&per_cpu(used_l1_inst_sram_head, cpu));
Bryan Wu's avatar
Bryan Wu committed
586 587

	/* add mutex operation */
588 589
	spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
	put_cpu();
Bryan Wu's avatar
Bryan Wu committed
590 591 592 593 594 595 596 597 598 599 600

	return ret;
#else
	return -1;
#endif
}
EXPORT_SYMBOL(l1_inst_sram_free);

/* L1 Scratchpad memory allocate function */
void *l1sram_alloc(size_t size)
{
601
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
602
	void *addr;
603
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
604

605
	cpu = get_cpu();
Bryan Wu's avatar
Bryan Wu committed
606
	/* add mutex operation */
607
	spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
608

609 610
	addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu),
			&per_cpu(used_l1_ssram_head, cpu));
Bryan Wu's avatar
Bryan Wu committed
611 612

	/* add mutex operation */
613 614
	spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
	put_cpu();
Bryan Wu's avatar
Bryan Wu committed
615 616 617 618 619 620 621

	return addr;
}

/* L1 Scratchpad memory allocate function */
void *l1sram_alloc_max(size_t *psize)
{
622
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
623
	void *addr;
624
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
625

626
	cpu = get_cpu();
Bryan Wu's avatar
Bryan Wu committed
627
	/* add mutex operation */
628
	spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
629

630 631
	addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
			&per_cpu(used_l1_ssram_head, cpu), psize);
Bryan Wu's avatar
Bryan Wu committed
632 633

	/* add mutex operation */
634 635
	spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
	put_cpu();
Bryan Wu's avatar
Bryan Wu committed
636 637 638 639 640 641 642

	return addr;
}

/* L1 Scratchpad memory free function */
int l1sram_free(const void *addr)
{
643
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
644
	int ret;
645
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
646

647
	cpu = get_cpu();
Bryan Wu's avatar
Bryan Wu committed
648
	/* add mutex operation */
649
	spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
650

651 652
	ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
			&per_cpu(used_l1_ssram_head, cpu));
Bryan Wu's avatar
Bryan Wu committed
653 654

	/* add mutex operation */
655 656
	spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
	put_cpu();
Bryan Wu's avatar
Bryan Wu committed
657 658 659 660

	return ret;
}

661 662
void *l2_sram_alloc(size_t size)
{
663
#if L2_LENGTH != 0
664
	unsigned long flags;
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
	void *addr;

	/* add mutex operation */
	spin_lock_irqsave(&l2_sram_lock, flags);

	addr = _sram_alloc(size, &free_l2_sram_head,
			&used_l2_sram_head);

	/* add mutex operation */
	spin_unlock_irqrestore(&l2_sram_lock, flags);

	pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
		 (long unsigned int)addr, size);

	return addr;
#else
	return NULL;
#endif
}
EXPORT_SYMBOL(l2_sram_alloc);

void *l2_sram_zalloc(size_t size)
{
	void *addr = l2_sram_alloc(size);

	if (addr)
		memset(addr, 0x00, size);

	return addr;
}
EXPORT_SYMBOL(l2_sram_zalloc);

int l2_sram_free(const void *addr)
{
699
#if L2_LENGTH != 0
700
	unsigned long flags;
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
	int ret;

	/* add mutex operation */
	spin_lock_irqsave(&l2_sram_lock, flags);

	ret = _sram_free(addr, &free_l2_sram_head,
			&used_l2_sram_head);

	/* add mutex operation */
	spin_unlock_irqrestore(&l2_sram_lock, flags);

	return ret;
#else
	return -1;
#endif
}
EXPORT_SYMBOL(l2_sram_free);

Bryan Wu's avatar
Bryan Wu committed
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
int sram_free_with_lsl(const void *addr)
{
	struct sram_list_struct *lsl, **tmp;
	struct mm_struct *mm = current->mm;

	for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
		if ((*tmp)->addr == addr)
			goto found;
	return -1;
found:
	lsl = *tmp;
	sram_free(addr);
	*tmp = lsl->next;
	kfree(lsl);

	return 0;
}
EXPORT_SYMBOL(sram_free_with_lsl);

void *sram_alloc_with_lsl(size_t size, unsigned long flags)
{
	void *addr = NULL;
	struct sram_list_struct *lsl = NULL;
	struct mm_struct *mm = current->mm;

744
	lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
Bryan Wu's avatar
Bryan Wu committed
745 746 747 748 749 750 751 752 753 754 755 756
	if (!lsl)
		return NULL;

	if (flags & L1_INST_SRAM)
		addr = l1_inst_sram_alloc(size);

	if (addr == NULL && (flags & L1_DATA_A_SRAM))
		addr = l1_data_A_sram_alloc(size);

	if (addr == NULL && (flags & L1_DATA_B_SRAM))
		addr = l1_data_B_sram_alloc(size);

757 758 759
	if (addr == NULL && (flags & L2_SRAM))
		addr = l2_sram_alloc(size);

Bryan Wu's avatar
Bryan Wu committed
760 761 762 763 764 765 766 767 768 769 770
	if (addr == NULL) {
		kfree(lsl);
		return NULL;
	}
	lsl->addr = addr;
	lsl->length = size;
	lsl->next = mm->context.sram_list;
	mm->context.sram_list = lsl;
	return addr;
}
EXPORT_SYMBOL(sram_alloc_with_lsl);
771 772 773 774 775

#ifdef CONFIG_PROC_FS
/* Once we get a real allocator, we'll throw all of this away.
 * Until then, we need some sort of visibility into the L1 alloc.
 */
776 777 778
/* Need to keep line of output the same.  Currently, that is 44 bytes
 * (including newline).
 */
779
static int _sram_proc_read(char *buf, int *len, int count, const char *desc,
780 781
		struct sram_piece *pfree_head,
		struct sram_piece *pused_head)
782
{
783 784 785 786
	struct sram_piece *pslot;

	if (!pfree_head || !pused_head)
		return -1;
787

788
	*len += sprintf(&buf[*len], "--- SRAM %-14s Size   PID State     \n", desc);
789 790 791 792 793

	/* search the relevant memory slot */
	pslot = pused_head->next;

	while (pslot != NULL) {
794
		*len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
795 796 797 798 799 800 801 802 803
			pslot->paddr, pslot->paddr + pslot->size,
			pslot->size, pslot->pid, "ALLOCATED");

		pslot = pslot->next;
	}

	pslot = pfree_head->next;

	while (pslot != NULL) {
804
		*len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
805 806 807 808
			pslot->paddr, pslot->paddr + pslot->size,
			pslot->size, pslot->pid, "FREE");

		pslot = pslot->next;
809
	}
810 811

	return 0;
812
}
813
static int sram_proc_read(char *buf, char **start, off_t offset, int count,
814 815 816
		int *eof, void *data)
{
	int len = 0;
817
	unsigned int cpu;
818

819 820 821 822
	for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
		if (_sram_proc_read(buf, &len, count, "Scratchpad",
			&per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu)))
			goto not_done;
823
#if L1_DATA_A_LENGTH != 0
824 825 826 827
		if (_sram_proc_read(buf, &len, count, "L1 Data A",
			&per_cpu(free_l1_data_A_sram_head, cpu),
			&per_cpu(used_l1_data_A_sram_head, cpu)))
			goto not_done;
828 829
#endif
#if L1_DATA_B_LENGTH != 0
830 831 832 833
		if (_sram_proc_read(buf, &len, count, "L1 Data B",
			&per_cpu(free_l1_data_B_sram_head, cpu),
			&per_cpu(used_l1_data_B_sram_head, cpu)))
			goto not_done;
834 835
#endif
#if L1_CODE_LENGTH != 0
836 837 838 839
		if (_sram_proc_read(buf, &len, count, "L1 Instruction",
			&per_cpu(free_l1_inst_sram_head, cpu),
			&per_cpu(used_l1_inst_sram_head, cpu)))
			goto not_done;
840
#endif
841
	}
842
#if L2_LENGTH != 0
843 844
	if (_sram_proc_read(buf, &len, count, "L2", &free_l2_sram_head,
		&used_l2_sram_head))
845 846
		goto not_done;
#endif
847 848
	*eof = 1;
 not_done:
849 850 851
	return len;
}

852
static int __init sram_proc_init(void)
853 854 855 856 857 858 859
{
	struct proc_dir_entry *ptr;
	ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
	if (!ptr) {
		printk(KERN_WARNING "unable to create /proc/sram\n");
		return -1;
	}
860
	ptr->read_proc = sram_proc_read;
861 862
	return 0;
}
863
late_initcall(sram_proc_init);
864
#endif