sram-alloc.c 20 KB
Newer Older
Bryan Wu's avatar
Bryan Wu committed
1
/*
2
 * SRAM allocator for Blackfin on-chip memory
Bryan Wu's avatar
Bryan Wu committed
3
 *
4
 * Copyright 2004-2009 Analog Devices Inc.
Bryan Wu's avatar
Bryan Wu committed
5
 *
6
 * Licensed under the GPL-2 or later.
Bryan Wu's avatar
Bryan Wu committed
7 8 9 10 11 12 13 14 15 16 17 18 19
 */

#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>
20
#include <linux/slab.h>
Bryan Wu's avatar
Bryan Wu committed
21
#include <asm/blackfin.h>
22
#include <asm/mem_map.h>
Bryan Wu's avatar
Bryan Wu committed
23 24 25
#include "blackfin_sram.h"

/* the data structure for L1 scratchpad and DATA SRAM */
26
struct sram_piece {
Bryan Wu's avatar
Bryan Wu committed
27 28
	void *paddr;
	int size;
29
	pid_t pid;
30
	struct sram_piece *next;
Bryan Wu's avatar
Bryan Wu committed
31 32
};

33
static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1sram_lock);
34 35
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
36 37

#if L1_DATA_A_LENGTH != 0
38 39
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
40 41 42
#endif

#if L1_DATA_B_LENGTH != 0
43 44
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
45 46
#endif

47 48 49 50
#if L1_DATA_A_LENGTH || L1_DATA_B_LENGTH
static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_data_sram_lock);
#endif

Bryan Wu's avatar
Bryan Wu committed
51
#if L1_CODE_LENGTH != 0
52
static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_inst_sram_lock);
53 54
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
55 56
#endif

57
#if L2_LENGTH != 0
58
static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp;
59 60 61
static struct sram_piece free_l2_sram_head, used_l2_sram_head;
#endif

62 63
static struct kmem_cache *sram_piece_cache;

Bryan Wu's avatar
Bryan Wu committed
64
/* L1 Scratchpad SRAM initialization function */
65
static void __init l1sram_init(void)
Bryan Wu's avatar
Bryan Wu committed
66
{
67
	unsigned int cpu;
68 69 70 71 72 73 74 75
	unsigned long reserve;

#ifdef CONFIG_SMP
	reserve = 0;
#else
	reserve = sizeof(struct l1_scratch_task_info);
#endif

76 77 78 79 80 81 82 83
	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;
		}

84 85
		per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu) + reserve;
		per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH - reserve;
86 87 88 89 90 91 92 93 94
		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);
95
	}
Bryan Wu's avatar
Bryan Wu committed
96 97
}

98
static void __init l1_data_sram_init(void)
Bryan Wu's avatar
Bryan Wu committed
99
{
100
#if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
101
	unsigned int cpu;
102
#endif
Bryan Wu's avatar
Bryan Wu committed
103
#if L1_DATA_A_LENGTH != 0
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	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);
124
	}
Bryan Wu's avatar
Bryan Wu committed
125 126
#endif
#if L1_DATA_B_LENGTH != 0
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	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 */
148
	}
Bryan Wu's avatar
Bryan Wu committed
149 150
#endif

151 152 153 154
#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
155 156
}

157
static void __init l1_inst_sram_init(void)
Bryan Wu's avatar
Bryan Wu committed
158 159
{
#if L1_CODE_LENGTH != 0
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	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));
184
	}
Bryan Wu's avatar
Bryan Wu committed
185 186 187
#endif
}

188 189
static void __init l2_sram_init(void)
{
190
#if L2_LENGTH != 0
191 192 193
	free_l2_sram_head.next =
		kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
	if (!free_l2_sram_head.next) {
194
		printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
195 196 197
		return;
	}

198 199 200 201
	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);
202 203 204 205 206 207 208 209 210 211 212
	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);

	/* mutex initialize */
	spin_lock_init(&l2_sram_lock);
213
#endif
214
}
215

216
static int __init bfin_sram_init(void)
217 218 219 220 221 222 223 224
{
	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();
225
	l2_sram_init();
226 227

	return 0;
228
}
229
pure_initcall(bfin_sram_init);
230

231 232
/* SRAM allocate function */
static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
233
		struct sram_piece *pused_head)
Bryan Wu's avatar
Bryan Wu committed
234
{
235
	struct sram_piece *pslot, *plast, *pavail;
Bryan Wu's avatar
Bryan Wu committed
236

237
	if (size <= 0 || !pfree_head || !pused_head)
Bryan Wu's avatar
Bryan Wu committed
238 239 240 241 242
		return NULL;

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

243 244 245 246 247 248 249
	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
250
	}
251 252

	if (!pslot)
Bryan Wu's avatar
Bryan Wu committed
253 254
		return NULL;

255 256 257 258
	if (pslot->size == size) {
		plast->next = pslot->next;
		pavail = pslot;
	} else {
259 260
		/* use atomic so our L1 allocator can be used atomically */
		pavail = kmem_cache_alloc(sram_piece_cache, GFP_ATOMIC);
261 262 263 264 265 266 267 268

		if (!pavail)
			return NULL;

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

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	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
286 287 288
}

/* Allocate the largest available block.  */
289
static void *_sram_alloc_max(struct sram_piece *pfree_head,
290
				struct sram_piece *pused_head,
Bryan Wu's avatar
Bryan Wu committed
291 292
				unsigned long *psize)
{
293 294 295 296 297 298
	struct sram_piece *pslot, *pmax;

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

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

300 301 302 303 304
	/* 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
305
	}
306 307

	if (!pmax)
Bryan Wu's avatar
Bryan Wu committed
308 309
		return NULL;

310 311
	*psize = pmax->size;

312
	return _sram_alloc(*psize, pfree_head, pused_head);
Bryan Wu's avatar
Bryan Wu committed
313 314
}

315 316
/* SRAM free function */
static int _sram_free(const void *addr,
317 318
			struct sram_piece *pfree_head,
			struct sram_piece *pused_head)
Bryan Wu's avatar
Bryan Wu committed
319
{
320 321 322 323
	struct sram_piece *pslot, *plast, *pavail;

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

	/* search the relevant memory slot */
326 327 328 329 330 331 332
	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
333
	}
334 335

	if (!pslot)
Bryan Wu's avatar
Bryan Wu committed
336 337
		return -1;

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	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 {
355
		pavail->next = plast->next;
356 357
		plast->next = pavail;
		plast = pavail;
Bryan Wu's avatar
Bryan Wu committed
358 359
	}

360 361 362 363
	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
364 365 366 367 368 369 370
	}

	return 0;
}

int sram_free(const void *addr)
{
371

Bryan Wu's avatar
Bryan Wu committed
372
#if L1_CODE_LENGTH != 0
373 374
	if (addr >= (void *)get_l1_code_start()
		 && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
Bryan Wu's avatar
Bryan Wu committed
375
		return l1_inst_sram_free(addr);
376
	else
Bryan Wu's avatar
Bryan Wu committed
377 378
#endif
#if L1_DATA_A_LENGTH != 0
379 380
	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
381
		return l1_data_A_sram_free(addr);
382
	else
Bryan Wu's avatar
Bryan Wu committed
383 384
#endif
#if L1_DATA_B_LENGTH != 0
385 386
	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
387
		return l1_data_B_sram_free(addr);
388
	else
389
#endif
390
#if L2_LENGTH != 0
391
	if (addr >= (void *)L2_START
392 393
		 && addr < (void *)(L2_START + L2_LENGTH))
		return l2_sram_free(addr);
Bryan Wu's avatar
Bryan Wu committed
394
	else
395
#endif
Bryan Wu's avatar
Bryan Wu committed
396 397 398 399 400 401
		return -1;
}
EXPORT_SYMBOL(sram_free);

void *l1_data_A_sram_alloc(size_t size)
{
402
#if L1_DATA_A_LENGTH != 0
403
	unsigned long flags;
404
	void *addr;
405
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
406

407
	cpu = smp_processor_id();
Bryan Wu's avatar
Bryan Wu committed
408
	/* add mutex operation */
409
	spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
410

411 412
	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
413 414

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

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

	return addr;
421 422 423
#else
	return NULL;
#endif
Bryan Wu's avatar
Bryan Wu committed
424 425 426 427 428
}
EXPORT_SYMBOL(l1_data_A_sram_alloc);

int l1_data_A_sram_free(const void *addr)
{
429
#if L1_DATA_A_LENGTH != 0
430
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
431
	int ret;
432
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
433

434
	cpu = smp_processor_id();
Bryan Wu's avatar
Bryan Wu committed
435
	/* add mutex operation */
436
	spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
437

438 439
	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
440 441

	/* add mutex operation */
442
	spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
443 444

	return ret;
445 446 447
#else
	return -1;
#endif
Bryan Wu's avatar
Bryan Wu committed
448 449 450 451 452 453
}
EXPORT_SYMBOL(l1_data_A_sram_free);

void *l1_data_B_sram_alloc(size_t size)
{
#if L1_DATA_B_LENGTH != 0
454
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
455
	void *addr;
456
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
457

458
	cpu = smp_processor_id();
Bryan Wu's avatar
Bryan Wu committed
459
	/* add mutex operation */
460
	spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
461

462 463
	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
464 465

	/* add mutex operation */
466
	spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480

	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
481
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
482
	int ret;
483
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
484

485
	cpu = smp_processor_id();
Bryan Wu's avatar
Bryan Wu committed
486
	/* add mutex operation */
487
	spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
488

489 490
	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
491 492

	/* add mutex operation */
493
	spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
494 495 496 497 498 499 500 501 502 503 504 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

	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)
{
536
#if L1_CODE_LENGTH != 0
537
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
538
	void *addr;
539
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
540

541
	cpu = smp_processor_id();
Bryan Wu's avatar
Bryan Wu committed
542
	/* add mutex operation */
543
	spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
544

545 546
	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
547 548

	/* add mutex operation */
549
	spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563

	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
564
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
565
	int ret;
566
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
567

568
	cpu = smp_processor_id();
Bryan Wu's avatar
Bryan Wu committed
569
	/* add mutex operation */
570
	spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
571

572 573
	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
574 575

	/* add mutex operation */
576
	spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
577 578 579 580 581 582 583 584 585 586 587

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

/* L1 Scratchpad memory allocate function */
void *l1sram_alloc(size_t size)
{
588
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
589
	void *addr;
590
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
591

592
	cpu = smp_processor_id();
Bryan Wu's avatar
Bryan Wu committed
593
	/* add mutex operation */
594
	spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
595

596 597
	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
598 599

	/* add mutex operation */
600
	spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
601 602 603 604 605 606 607

	return addr;
}

/* L1 Scratchpad memory allocate function */
void *l1sram_alloc_max(size_t *psize)
{
608
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
609
	void *addr;
610
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
611

612
	cpu = smp_processor_id();
Bryan Wu's avatar
Bryan Wu committed
613
	/* add mutex operation */
614
	spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
615

616 617
	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
618 619

	/* add mutex operation */
620
	spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
621 622 623 624 625 626 627

	return addr;
}

/* L1 Scratchpad memory free function */
int l1sram_free(const void *addr)
{
628
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
629
	int ret;
630
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
631

632
	cpu = smp_processor_id();
Bryan Wu's avatar
Bryan Wu committed
633
	/* add mutex operation */
634
	spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
635

636 637
	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
638 639

	/* add mutex operation */
640
	spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
641 642 643 644

	return ret;
}

645 646
void *l2_sram_alloc(size_t size)
{
647
#if L2_LENGTH != 0
648
	unsigned long flags;
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
	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)
{
683
#if L2_LENGTH != 0
684
	unsigned long flags;
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
	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
703 704 705 706
int sram_free_with_lsl(const void *addr)
{
	struct sram_list_struct *lsl, **tmp;
	struct mm_struct *mm = current->mm;
707
	int ret = -1;
Bryan Wu's avatar
Bryan Wu committed
708 709

	for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
710 711 712 713 714 715 716
		if ((*tmp)->addr == addr) {
			lsl = *tmp;
			ret = sram_free(addr);
			*tmp = lsl->next;
			kfree(lsl);
			break;
		}
Bryan Wu's avatar
Bryan Wu committed
717

718
	return ret;
Bryan Wu's avatar
Bryan Wu committed
719 720 721
}
EXPORT_SYMBOL(sram_free_with_lsl);

722 723 724 725
/* Allocate memory and keep in L1 SRAM List (lsl) so that the resources are
 * tracked.  These are designed for userspace so that when a process exits,
 * we can safely reap their resources.
 */
Bryan Wu's avatar
Bryan Wu committed
726 727 728 729 730 731
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;

732
	lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
Bryan Wu's avatar
Bryan Wu committed
733 734 735 736 737 738 739 740 741 742 743 744
	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);

745 746 747
	if (addr == NULL && (flags & L2_SRAM))
		addr = l2_sram_alloc(size);

Bryan Wu's avatar
Bryan Wu committed
748 749 750 751 752 753 754 755 756 757 758
	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);
759 760 761 762 763

#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.
 */
764 765 766
/* Need to keep line of output the same.  Currently, that is 44 bytes
 * (including newline).
 */
767
static int _sram_proc_read(char *buf, int *len, int count, const char *desc,
768 769
		struct sram_piece *pfree_head,
		struct sram_piece *pused_head)
770
{
771 772 773 774
	struct sram_piece *pslot;

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

776
	*len += sprintf(&buf[*len], "--- SRAM %-14s Size   PID State     \n", desc);
777 778 779 780 781

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

	while (pslot != NULL) {
782
		*len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
783 784 785 786 787 788 789 790 791
			pslot->paddr, pslot->paddr + pslot->size,
			pslot->size, pslot->pid, "ALLOCATED");

		pslot = pslot->next;
	}

	pslot = pfree_head->next;

	while (pslot != NULL) {
792
		*len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
793 794 795 796
			pslot->paddr, pslot->paddr + pslot->size,
			pslot->size, pslot->pid, "FREE");

		pslot = pslot->next;
797
	}
798 799

	return 0;
800
}
801
static int sram_proc_read(char *buf, char **start, off_t offset, int count,
802 803 804
		int *eof, void *data)
{
	int len = 0;
805
	unsigned int cpu;
806

807 808 809 810
	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;
811
#if L1_DATA_A_LENGTH != 0
812 813 814 815
		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;
816 817
#endif
#if L1_DATA_B_LENGTH != 0
818 819 820 821
		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;
822 823
#endif
#if L1_CODE_LENGTH != 0
824 825 826 827
		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;
828
#endif
829
	}
830
#if L2_LENGTH != 0
831 832
	if (_sram_proc_read(buf, &len, count, "L2", &free_l2_sram_head,
		&used_l2_sram_head))
833 834
		goto not_done;
#endif
835 836
	*eof = 1;
 not_done:
837 838 839
	return len;
}

840
static int __init sram_proc_init(void)
841 842 843 844 845 846 847
{
	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;
	}
848
	ptr->read_proc = sram_proc_read;
849 850
	return 0;
}
851
late_initcall(sram_proc_init);
852
#endif