All new accounts created on Gitlab now require administrator approval. If you invite any collaborators, please let Flux staff know so they can approve the accounts.

sram-alloc.c 20.9 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
 */

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

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

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

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

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

48 49 50 51
#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
52
#if L1_CODE_LENGTH != 0
53
static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_inst_sram_lock);
54 55
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
56 57
#endif

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

63 64
static struct kmem_cache *sram_piece_cache;

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

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

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

85 86
		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;
87 88 89 90 91 92 93 94 95
		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);
96
	}
Bryan Wu's avatar
Bryan Wu committed
97 98
}

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

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

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

189 190 191 192 193
#ifdef __ADSPBF60x__
static irqreturn_t l2_ecc_err(int irq, void *dev_id)
{
	int status;

194
	printk(KERN_ERR "L2 ecc error happened\n");
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	status = bfin_read32(L2CTL0_STAT);
	if (status & 0x1)
		printk(KERN_ERR "Core channel error type:0x%x, addr:0x%x\n",
			bfin_read32(L2CTL0_ET0), bfin_read32(L2CTL0_EADDR0));
	if (status & 0x2)
		printk(KERN_ERR "System channel error type:0x%x, addr:0x%x\n",
			bfin_read32(L2CTL0_ET1), bfin_read32(L2CTL0_EADDR1));

	status = status >> 8;
	if (status)
		printk(KERN_ERR "L2 Bank%d error, addr:0x%x\n",
			status, bfin_read32(L2CTL0_ERRADDR0 + status));

	panic("L2 Ecc error");
	return IRQ_HANDLED;
}
#endif

213 214
static void __init l2_sram_init(void)
{
215
#if L2_LENGTH != 0
216 217 218 219 220 221 222 223 224 225 226 227

#ifdef __ADSPBF60x__
	int ret;

	ret = request_irq(IRQ_L2CTL0_ECC_ERR, l2_ecc_err, 0, "l2-ecc-err",
			NULL);
	if (unlikely(ret < 0)) {
		printk(KERN_INFO "Fail to request l2 ecc error interrupt");
		return;
	}
#endif

228 229 230
	free_l2_sram_head.next =
		kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
	if (!free_l2_sram_head.next) {
231
		printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
232 233 234
		return;
	}

235 236 237 238
	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);
239 240 241 242 243 244 245 246 247 248 249
	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);
250
#endif
251
}
252

253
static int __init bfin_sram_init(void)
254 255 256 257 258 259 260 261
{
	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();
262
	l2_sram_init();
263 264

	return 0;
265
}
266
pure_initcall(bfin_sram_init);
267

268 269
/* SRAM allocate function */
static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
270
		struct sram_piece *pused_head)
Bryan Wu's avatar
Bryan Wu committed
271
{
272
	struct sram_piece *pslot, *plast, *pavail;
Bryan Wu's avatar
Bryan Wu committed
273

274
	if (size <= 0 || !pfree_head || !pused_head)
Bryan Wu's avatar
Bryan Wu committed
275 276 277 278 279
		return NULL;

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

280 281 282 283 284 285 286
	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
287
	}
288 289

	if (!pslot)
Bryan Wu's avatar
Bryan Wu committed
290 291
		return NULL;

292 293 294 295
	if (pslot->size == size) {
		plast->next = pslot->next;
		pavail = pslot;
	} else {
296 297
		/* use atomic so our L1 allocator can be used atomically */
		pavail = kmem_cache_alloc(sram_piece_cache, GFP_ATOMIC);
298 299 300 301 302 303 304 305

		if (!pavail)
			return NULL;

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

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	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
323 324 325
}

/* Allocate the largest available block.  */
326
static void *_sram_alloc_max(struct sram_piece *pfree_head,
327
				struct sram_piece *pused_head,
Bryan Wu's avatar
Bryan Wu committed
328 329
				unsigned long *psize)
{
330 331 332 333 334 335
	struct sram_piece *pslot, *pmax;

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

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

337 338 339 340 341
	/* 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
342
	}
343 344

	if (!pmax)
Bryan Wu's avatar
Bryan Wu committed
345 346
		return NULL;

347 348
	*psize = pmax->size;

349
	return _sram_alloc(*psize, pfree_head, pused_head);
Bryan Wu's avatar
Bryan Wu committed
350 351
}

352 353
/* SRAM free function */
static int _sram_free(const void *addr,
354 355
			struct sram_piece *pfree_head,
			struct sram_piece *pused_head)
Bryan Wu's avatar
Bryan Wu committed
356
{
357 358 359 360
	struct sram_piece *pslot, *plast, *pavail;

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

	/* search the relevant memory slot */
363 364 365 366 367 368 369
	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
370
	}
371 372

	if (!pslot)
Bryan Wu's avatar
Bryan Wu committed
373 374
		return -1;

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
	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 {
392
		pavail->next = plast->next;
393 394
		plast->next = pavail;
		plast = pavail;
Bryan Wu's avatar
Bryan Wu committed
395 396
	}

397 398 399 400
	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
401 402 403 404 405 406 407
	}

	return 0;
}

int sram_free(const void *addr)
{
408

Bryan Wu's avatar
Bryan Wu committed
409
#if L1_CODE_LENGTH != 0
410 411
	if (addr >= (void *)get_l1_code_start()
		 && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
Bryan Wu's avatar
Bryan Wu committed
412
		return l1_inst_sram_free(addr);
413
	else
Bryan Wu's avatar
Bryan Wu committed
414 415
#endif
#if L1_DATA_A_LENGTH != 0
416 417
	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
418
		return l1_data_A_sram_free(addr);
419
	else
Bryan Wu's avatar
Bryan Wu committed
420 421
#endif
#if L1_DATA_B_LENGTH != 0
422 423
	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
424
		return l1_data_B_sram_free(addr);
425
	else
426
#endif
427
#if L2_LENGTH != 0
428
	if (addr >= (void *)L2_START
429 430
		 && addr < (void *)(L2_START + L2_LENGTH))
		return l2_sram_free(addr);
Bryan Wu's avatar
Bryan Wu committed
431
	else
432
#endif
Bryan Wu's avatar
Bryan Wu committed
433 434 435 436 437 438
		return -1;
}
EXPORT_SYMBOL(sram_free);

void *l1_data_A_sram_alloc(size_t size)
{
439
#if L1_DATA_A_LENGTH != 0
440
	unsigned long flags;
441
	void *addr;
442
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
443

444
	cpu = smp_processor_id();
Bryan Wu's avatar
Bryan Wu committed
445
	/* add mutex operation */
446
	spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
447

448 449
	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
450 451

	/* add mutex operation */
452
	spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
453 454 455 456 457

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

	return addr;
458 459 460
#else
	return NULL;
#endif
Bryan Wu's avatar
Bryan Wu committed
461 462 463 464 465
}
EXPORT_SYMBOL(l1_data_A_sram_alloc);

int l1_data_A_sram_free(const void *addr)
{
466
#if L1_DATA_A_LENGTH != 0
467
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
468
	int ret;
469
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
470

471
	cpu = smp_processor_id();
Bryan Wu's avatar
Bryan Wu committed
472
	/* add mutex operation */
473
	spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
474

475 476
	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
477 478

	/* add mutex operation */
479
	spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
480 481

	return ret;
482 483 484
#else
	return -1;
#endif
Bryan Wu's avatar
Bryan Wu committed
485 486 487 488 489 490
}
EXPORT_SYMBOL(l1_data_A_sram_free);

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

495
	cpu = smp_processor_id();
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
	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
501 502

	/* add mutex operation */
503
	spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
504 505 506 507 508 509 510 511 512 513 514 515 516 517

	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
518
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
519
	int ret;
520
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
521

522
	cpu = smp_processor_id();
Bryan Wu's avatar
Bryan Wu committed
523
	/* add mutex operation */
524
	spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
525

526 527
	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
528 529

	/* add mutex operation */
530
	spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

	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)
{
573
#if L1_CODE_LENGTH != 0
574
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
575
	void *addr;
576
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
577

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

582 583
	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
584 585

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

	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
601
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
602
	int ret;
603
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
604

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

609 610
	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
611 612

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

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

/* L1 Scratchpad memory allocate function */
void *l1sram_alloc(size_t size)
{
625
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
626
	void *addr;
627
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
628

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

633 634
	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
635 636

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

	return addr;
}

/* L1 Scratchpad memory allocate function */
void *l1sram_alloc_max(size_t *psize)
{
645
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
646
	void *addr;
647
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
648

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

653 654
	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
655 656

	/* add mutex operation */
657
	spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
658 659 660 661 662 663 664

	return addr;
}

/* L1 Scratchpad memory free function */
int l1sram_free(const void *addr)
{
665
	unsigned long flags;
Bryan Wu's avatar
Bryan Wu committed
666
	int ret;
667
	unsigned int cpu;
Bryan Wu's avatar
Bryan Wu committed
668

669
	cpu = smp_processor_id();
Bryan Wu's avatar
Bryan Wu committed
670
	/* add mutex operation */
671
	spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
672

673 674
	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
675 676

	/* add mutex operation */
677
	spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
Bryan Wu's avatar
Bryan Wu committed
678 679 680 681

	return ret;
}

682 683
void *l2_sram_alloc(size_t size)
{
684
#if L2_LENGTH != 0
685
	unsigned long flags;
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
	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)
{
720
#if L2_LENGTH != 0
721
	unsigned long flags;
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
	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
740 741 742 743
int sram_free_with_lsl(const void *addr)
{
	struct sram_list_struct *lsl, **tmp;
	struct mm_struct *mm = current->mm;
744
	int ret = -1;
Bryan Wu's avatar
Bryan Wu committed
745 746

	for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
747 748 749 750 751 752 753
		if ((*tmp)->addr == addr) {
			lsl = *tmp;
			ret = sram_free(addr);
			*tmp = lsl->next;
			kfree(lsl);
			break;
		}
Bryan Wu's avatar
Bryan Wu committed
754

755
	return ret;
Bryan Wu's avatar
Bryan Wu committed
756 757 758
}
EXPORT_SYMBOL(sram_free_with_lsl);

759 760 761 762
/* 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
763 764 765 766 767 768
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;

769
	lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
Bryan Wu's avatar
Bryan Wu committed
770 771 772 773 774 775 776 777 778 779 780 781
	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);

782 783 784
	if (addr == NULL && (flags & L2_SRAM))
		addr = l2_sram_alloc(size);

Bryan Wu's avatar
Bryan Wu committed
785 786 787 788 789 790 791 792 793 794 795
	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);
796 797 798 799 800

#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.
 */
801 802 803
/* Need to keep line of output the same.  Currently, that is 44 bytes
 * (including newline).
 */
804
static int _sram_proc_show(struct seq_file *m, const char *desc,
805 806
		struct sram_piece *pfree_head,
		struct sram_piece *pused_head)
807
{
808 809 810 811
	struct sram_piece *pslot;

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

813
	seq_printf(m, "--- SRAM %-14s Size   PID State     \n", desc);
814 815 816 817 818

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

	while (pslot != NULL) {
819
		seq_printf(m, "%p-%p %10i %5i %-10s\n",
820 821 822 823 824 825 826 827 828
			pslot->paddr, pslot->paddr + pslot->size,
			pslot->size, pslot->pid, "ALLOCATED");

		pslot = pslot->next;
	}

	pslot = pfree_head->next;

	while (pslot != NULL) {
829
		seq_printf(m, "%p-%p %10i %5i %-10s\n",
830 831 832 833
			pslot->paddr, pslot->paddr + pslot->size,
			pslot->size, pslot->pid, "FREE");

		pslot = pslot->next;
834
	}
835 836

	return 0;
837
}
838
static int sram_proc_show(struct seq_file *m, void *v)
839
{
840
	unsigned int cpu;
841

842
	for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
843
		if (_sram_proc_show(m, "Scratchpad",
844 845
			&per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu)))
			goto not_done;
846
#if L1_DATA_A_LENGTH != 0
847
		if (_sram_proc_show(m, "L1 Data A",
848 849 850
			&per_cpu(free_l1_data_A_sram_head, cpu),
			&per_cpu(used_l1_data_A_sram_head, cpu)))
			goto not_done;
851 852
#endif
#if L1_DATA_B_LENGTH != 0
853
		if (_sram_proc_show(m, "L1 Data B",
854 855 856
			&per_cpu(free_l1_data_B_sram_head, cpu),
			&per_cpu(used_l1_data_B_sram_head, cpu)))
			goto not_done;
857 858
#endif
#if L1_CODE_LENGTH != 0
859
		if (_sram_proc_show(m, "L1 Instruction",
860 861 862
			&per_cpu(free_l1_inst_sram_head, cpu),
			&per_cpu(used_l1_inst_sram_head, cpu)))
			goto not_done;
863
#endif
864
	}
865
#if L2_LENGTH != 0
866
	if (_sram_proc_show(m, "L2", &free_l2_sram_head, &used_l2_sram_head))
867 868
		goto not_done;
#endif
869
 not_done:
870 871 872 873 874 875
	return 0;
}

static int sram_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, sram_proc_show, NULL);
876 877
}

878 879 880 881 882 883 884
static const struct file_operations sram_proc_ops = {
	.open		= sram_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

885
static int __init sram_proc_init(void)
886 887
{
	struct proc_dir_entry *ptr;
888 889

	ptr = proc_create("sram", S_IRUGO, NULL, &sram_proc_ops);
890 891 892 893 894 895
	if (!ptr) {
		printk(KERN_WARNING "unable to create /proc/sram\n");
		return -1;
	}
	return 0;
}
896
late_initcall(sram_proc_init);
897
#endif