sram-alloc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*
  2. * SRAM allocator for Blackfin on-chip memory
  3. *
  4. * Copyright 2004-2009 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/miscdevice.h>
  12. #include <linux/ioport.h>
  13. #include <linux/fcntl.h>
  14. #include <linux/init.h>
  15. #include <linux/poll.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/rtc.h>
  20. #include <linux/slab.h>
  21. #include <asm/blackfin.h>
  22. #include <asm/mem_map.h>
  23. #include "blackfin_sram.h"
  24. /* the data structure for L1 scratchpad and DATA SRAM */
  25. struct sram_piece {
  26. void *paddr;
  27. int size;
  28. pid_t pid;
  29. struct sram_piece *next;
  30. };
  31. static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1sram_lock);
  32. static DEFINE_PER_CPU(struct sram_piece, free_l1_ssram_head);
  33. static DEFINE_PER_CPU(struct sram_piece, used_l1_ssram_head);
  34. #if L1_DATA_A_LENGTH != 0
  35. static DEFINE_PER_CPU(struct sram_piece, free_l1_data_A_sram_head);
  36. static DEFINE_PER_CPU(struct sram_piece, used_l1_data_A_sram_head);
  37. #endif
  38. #if L1_DATA_B_LENGTH != 0
  39. static DEFINE_PER_CPU(struct sram_piece, free_l1_data_B_sram_head);
  40. static DEFINE_PER_CPU(struct sram_piece, used_l1_data_B_sram_head);
  41. #endif
  42. #if L1_DATA_A_LENGTH || L1_DATA_B_LENGTH
  43. static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_data_sram_lock);
  44. #endif
  45. #if L1_CODE_LENGTH != 0
  46. static DEFINE_PER_CPU_SHARED_ALIGNED(spinlock_t, l1_inst_sram_lock);
  47. static DEFINE_PER_CPU(struct sram_piece, free_l1_inst_sram_head);
  48. static DEFINE_PER_CPU(struct sram_piece, used_l1_inst_sram_head);
  49. #endif
  50. #if L2_LENGTH != 0
  51. static spinlock_t l2_sram_lock ____cacheline_aligned_in_smp;
  52. static struct sram_piece free_l2_sram_head, used_l2_sram_head;
  53. #endif
  54. static struct kmem_cache *sram_piece_cache;
  55. /* L1 Scratchpad SRAM initialization function */
  56. static void __init l1sram_init(void)
  57. {
  58. unsigned int cpu;
  59. unsigned long reserve;
  60. #ifdef CONFIG_SMP
  61. reserve = 0;
  62. #else
  63. reserve = sizeof(struct l1_scratch_task_info);
  64. #endif
  65. for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
  66. per_cpu(free_l1_ssram_head, cpu).next =
  67. kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
  68. if (!per_cpu(free_l1_ssram_head, cpu).next) {
  69. printk(KERN_INFO "Fail to initialize Scratchpad data SRAM.\n");
  70. return;
  71. }
  72. per_cpu(free_l1_ssram_head, cpu).next->paddr = (void *)get_l1_scratch_start_cpu(cpu) + reserve;
  73. per_cpu(free_l1_ssram_head, cpu).next->size = L1_SCRATCH_LENGTH - reserve;
  74. per_cpu(free_l1_ssram_head, cpu).next->pid = 0;
  75. per_cpu(free_l1_ssram_head, cpu).next->next = NULL;
  76. per_cpu(used_l1_ssram_head, cpu).next = NULL;
  77. /* mutex initialize */
  78. spin_lock_init(&per_cpu(l1sram_lock, cpu));
  79. printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
  80. L1_SCRATCH_LENGTH >> 10);
  81. }
  82. }
  83. static void __init l1_data_sram_init(void)
  84. {
  85. #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
  86. unsigned int cpu;
  87. #endif
  88. #if L1_DATA_A_LENGTH != 0
  89. for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
  90. per_cpu(free_l1_data_A_sram_head, cpu).next =
  91. kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
  92. if (!per_cpu(free_l1_data_A_sram_head, cpu).next) {
  93. printk(KERN_INFO "Fail to initialize L1 Data A SRAM.\n");
  94. return;
  95. }
  96. per_cpu(free_l1_data_A_sram_head, cpu).next->paddr =
  97. (void *)get_l1_data_a_start_cpu(cpu) + (_ebss_l1 - _sdata_l1);
  98. per_cpu(free_l1_data_A_sram_head, cpu).next->size =
  99. L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
  100. per_cpu(free_l1_data_A_sram_head, cpu).next->pid = 0;
  101. per_cpu(free_l1_data_A_sram_head, cpu).next->next = NULL;
  102. per_cpu(used_l1_data_A_sram_head, cpu).next = NULL;
  103. printk(KERN_INFO "Blackfin L1 Data A SRAM: %d KB (%d KB free)\n",
  104. L1_DATA_A_LENGTH >> 10,
  105. per_cpu(free_l1_data_A_sram_head, cpu).next->size >> 10);
  106. }
  107. #endif
  108. #if L1_DATA_B_LENGTH != 0
  109. for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
  110. per_cpu(free_l1_data_B_sram_head, cpu).next =
  111. kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
  112. if (!per_cpu(free_l1_data_B_sram_head, cpu).next) {
  113. printk(KERN_INFO "Fail to initialize L1 Data B SRAM.\n");
  114. return;
  115. }
  116. per_cpu(free_l1_data_B_sram_head, cpu).next->paddr =
  117. (void *)get_l1_data_b_start_cpu(cpu) + (_ebss_b_l1 - _sdata_b_l1);
  118. per_cpu(free_l1_data_B_sram_head, cpu).next->size =
  119. L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
  120. per_cpu(free_l1_data_B_sram_head, cpu).next->pid = 0;
  121. per_cpu(free_l1_data_B_sram_head, cpu).next->next = NULL;
  122. per_cpu(used_l1_data_B_sram_head, cpu).next = NULL;
  123. printk(KERN_INFO "Blackfin L1 Data B SRAM: %d KB (%d KB free)\n",
  124. L1_DATA_B_LENGTH >> 10,
  125. per_cpu(free_l1_data_B_sram_head, cpu).next->size >> 10);
  126. /* mutex initialize */
  127. }
  128. #endif
  129. #if L1_DATA_A_LENGTH != 0 || L1_DATA_B_LENGTH != 0
  130. for (cpu = 0; cpu < num_possible_cpus(); ++cpu)
  131. spin_lock_init(&per_cpu(l1_data_sram_lock, cpu));
  132. #endif
  133. }
  134. static void __init l1_inst_sram_init(void)
  135. {
  136. #if L1_CODE_LENGTH != 0
  137. unsigned int cpu;
  138. for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
  139. per_cpu(free_l1_inst_sram_head, cpu).next =
  140. kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
  141. if (!per_cpu(free_l1_inst_sram_head, cpu).next) {
  142. printk(KERN_INFO "Failed to initialize L1 Instruction SRAM\n");
  143. return;
  144. }
  145. per_cpu(free_l1_inst_sram_head, cpu).next->paddr =
  146. (void *)get_l1_code_start_cpu(cpu) + (_etext_l1 - _stext_l1);
  147. per_cpu(free_l1_inst_sram_head, cpu).next->size =
  148. L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
  149. per_cpu(free_l1_inst_sram_head, cpu).next->pid = 0;
  150. per_cpu(free_l1_inst_sram_head, cpu).next->next = NULL;
  151. per_cpu(used_l1_inst_sram_head, cpu).next = NULL;
  152. printk(KERN_INFO "Blackfin L1 Instruction SRAM: %d KB (%d KB free)\n",
  153. L1_CODE_LENGTH >> 10,
  154. per_cpu(free_l1_inst_sram_head, cpu).next->size >> 10);
  155. /* mutex initialize */
  156. spin_lock_init(&per_cpu(l1_inst_sram_lock, cpu));
  157. }
  158. #endif
  159. }
  160. #ifdef __ADSPBF60x__
  161. static irqreturn_t l2_ecc_err(int irq, void *dev_id)
  162. {
  163. int status;
  164. printk(KERN_ERR "L2 ecc error happened\n");
  165. status = bfin_read32(L2CTL0_STAT);
  166. if (status & 0x1)
  167. printk(KERN_ERR "Core channel error type:0x%x, addr:0x%x\n",
  168. bfin_read32(L2CTL0_ET0), bfin_read32(L2CTL0_EADDR0));
  169. if (status & 0x2)
  170. printk(KERN_ERR "System channel error type:0x%x, addr:0x%x\n",
  171. bfin_read32(L2CTL0_ET1), bfin_read32(L2CTL0_EADDR1));
  172. status = status >> 8;
  173. if (status)
  174. printk(KERN_ERR "L2 Bank%d error, addr:0x%x\n",
  175. status, bfin_read32(L2CTL0_ERRADDR0 + status));
  176. panic("L2 Ecc error");
  177. return IRQ_HANDLED;
  178. }
  179. #endif
  180. static void __init l2_sram_init(void)
  181. {
  182. #if L2_LENGTH != 0
  183. #ifdef __ADSPBF60x__
  184. int ret;
  185. ret = request_irq(IRQ_L2CTL0_ECC_ERR, l2_ecc_err, 0, "l2-ecc-err",
  186. NULL);
  187. if (unlikely(ret < 0)) {
  188. printk(KERN_INFO "Fail to request l2 ecc error interrupt");
  189. return;
  190. }
  191. #endif
  192. free_l2_sram_head.next =
  193. kmem_cache_alloc(sram_piece_cache, GFP_KERNEL);
  194. if (!free_l2_sram_head.next) {
  195. printk(KERN_INFO "Fail to initialize L2 SRAM.\n");
  196. return;
  197. }
  198. free_l2_sram_head.next->paddr =
  199. (void *)L2_START + (_ebss_l2 - _stext_l2);
  200. free_l2_sram_head.next->size =
  201. L2_LENGTH - (_ebss_l2 - _stext_l2);
  202. free_l2_sram_head.next->pid = 0;
  203. free_l2_sram_head.next->next = NULL;
  204. used_l2_sram_head.next = NULL;
  205. printk(KERN_INFO "Blackfin L2 SRAM: %d KB (%d KB free)\n",
  206. L2_LENGTH >> 10,
  207. free_l2_sram_head.next->size >> 10);
  208. /* mutex initialize */
  209. spin_lock_init(&l2_sram_lock);
  210. #endif
  211. }
  212. static int __init bfin_sram_init(void)
  213. {
  214. sram_piece_cache = kmem_cache_create("sram_piece_cache",
  215. sizeof(struct sram_piece),
  216. 0, SLAB_PANIC, NULL);
  217. l1sram_init();
  218. l1_data_sram_init();
  219. l1_inst_sram_init();
  220. l2_sram_init();
  221. return 0;
  222. }
  223. pure_initcall(bfin_sram_init);
  224. /* SRAM allocate function */
  225. static void *_sram_alloc(size_t size, struct sram_piece *pfree_head,
  226. struct sram_piece *pused_head)
  227. {
  228. struct sram_piece *pslot, *plast, *pavail;
  229. if (size <= 0 || !pfree_head || !pused_head)
  230. return NULL;
  231. /* Align the size */
  232. size = (size + 3) & ~3;
  233. pslot = pfree_head->next;
  234. plast = pfree_head;
  235. /* search an available piece slot */
  236. while (pslot != NULL && size > pslot->size) {
  237. plast = pslot;
  238. pslot = pslot->next;
  239. }
  240. if (!pslot)
  241. return NULL;
  242. if (pslot->size == size) {
  243. plast->next = pslot->next;
  244. pavail = pslot;
  245. } else {
  246. /* use atomic so our L1 allocator can be used atomically */
  247. pavail = kmem_cache_alloc(sram_piece_cache, GFP_ATOMIC);
  248. if (!pavail)
  249. return NULL;
  250. pavail->paddr = pslot->paddr;
  251. pavail->size = size;
  252. pslot->paddr += size;
  253. pslot->size -= size;
  254. }
  255. pavail->pid = current->pid;
  256. pslot = pused_head->next;
  257. plast = pused_head;
  258. /* insert new piece into used piece list !!! */
  259. while (pslot != NULL && pavail->paddr < pslot->paddr) {
  260. plast = pslot;
  261. pslot = pslot->next;
  262. }
  263. pavail->next = pslot;
  264. plast->next = pavail;
  265. return pavail->paddr;
  266. }
  267. /* Allocate the largest available block. */
  268. static void *_sram_alloc_max(struct sram_piece *pfree_head,
  269. struct sram_piece *pused_head,
  270. unsigned long *psize)
  271. {
  272. struct sram_piece *pslot, *pmax;
  273. if (!pfree_head || !pused_head)
  274. return NULL;
  275. pmax = pslot = pfree_head->next;
  276. /* search an available piece slot */
  277. while (pslot != NULL) {
  278. if (pslot->size > pmax->size)
  279. pmax = pslot;
  280. pslot = pslot->next;
  281. }
  282. if (!pmax)
  283. return NULL;
  284. *psize = pmax->size;
  285. return _sram_alloc(*psize, pfree_head, pused_head);
  286. }
  287. /* SRAM free function */
  288. static int _sram_free(const void *addr,
  289. struct sram_piece *pfree_head,
  290. struct sram_piece *pused_head)
  291. {
  292. struct sram_piece *pslot, *plast, *pavail;
  293. if (!pfree_head || !pused_head)
  294. return -1;
  295. /* search the relevant memory slot */
  296. pslot = pused_head->next;
  297. plast = pused_head;
  298. /* search an available piece slot */
  299. while (pslot != NULL && pslot->paddr != addr) {
  300. plast = pslot;
  301. pslot = pslot->next;
  302. }
  303. if (!pslot)
  304. return -1;
  305. plast->next = pslot->next;
  306. pavail = pslot;
  307. pavail->pid = 0;
  308. /* insert free pieces back to the free list */
  309. pslot = pfree_head->next;
  310. plast = pfree_head;
  311. while (pslot != NULL && addr > pslot->paddr) {
  312. plast = pslot;
  313. pslot = pslot->next;
  314. }
  315. if (plast != pfree_head && plast->paddr + plast->size == pavail->paddr) {
  316. plast->size += pavail->size;
  317. kmem_cache_free(sram_piece_cache, pavail);
  318. } else {
  319. pavail->next = plast->next;
  320. plast->next = pavail;
  321. plast = pavail;
  322. }
  323. if (pslot && plast->paddr + plast->size == pslot->paddr) {
  324. plast->size += pslot->size;
  325. plast->next = pslot->next;
  326. kmem_cache_free(sram_piece_cache, pslot);
  327. }
  328. return 0;
  329. }
  330. int sram_free(const void *addr)
  331. {
  332. #if L1_CODE_LENGTH != 0
  333. if (addr >= (void *)get_l1_code_start()
  334. && addr < (void *)(get_l1_code_start() + L1_CODE_LENGTH))
  335. return l1_inst_sram_free(addr);
  336. else
  337. #endif
  338. #if L1_DATA_A_LENGTH != 0
  339. if (addr >= (void *)get_l1_data_a_start()
  340. && addr < (void *)(get_l1_data_a_start() + L1_DATA_A_LENGTH))
  341. return l1_data_A_sram_free(addr);
  342. else
  343. #endif
  344. #if L1_DATA_B_LENGTH != 0
  345. if (addr >= (void *)get_l1_data_b_start()
  346. && addr < (void *)(get_l1_data_b_start() + L1_DATA_B_LENGTH))
  347. return l1_data_B_sram_free(addr);
  348. else
  349. #endif
  350. #if L2_LENGTH != 0
  351. if (addr >= (void *)L2_START
  352. && addr < (void *)(L2_START + L2_LENGTH))
  353. return l2_sram_free(addr);
  354. else
  355. #endif
  356. return -1;
  357. }
  358. EXPORT_SYMBOL(sram_free);
  359. void *l1_data_A_sram_alloc(size_t size)
  360. {
  361. #if L1_DATA_A_LENGTH != 0
  362. unsigned long flags;
  363. void *addr;
  364. unsigned int cpu;
  365. cpu = smp_processor_id();
  366. /* add mutex operation */
  367. spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
  368. addr = _sram_alloc(size, &per_cpu(free_l1_data_A_sram_head, cpu),
  369. &per_cpu(used_l1_data_A_sram_head, cpu));
  370. /* add mutex operation */
  371. spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
  372. pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
  373. (long unsigned int)addr, size);
  374. return addr;
  375. #else
  376. return NULL;
  377. #endif
  378. }
  379. EXPORT_SYMBOL(l1_data_A_sram_alloc);
  380. int l1_data_A_sram_free(const void *addr)
  381. {
  382. #if L1_DATA_A_LENGTH != 0
  383. unsigned long flags;
  384. int ret;
  385. unsigned int cpu;
  386. cpu = smp_processor_id();
  387. /* add mutex operation */
  388. spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
  389. ret = _sram_free(addr, &per_cpu(free_l1_data_A_sram_head, cpu),
  390. &per_cpu(used_l1_data_A_sram_head, cpu));
  391. /* add mutex operation */
  392. spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
  393. return ret;
  394. #else
  395. return -1;
  396. #endif
  397. }
  398. EXPORT_SYMBOL(l1_data_A_sram_free);
  399. void *l1_data_B_sram_alloc(size_t size)
  400. {
  401. #if L1_DATA_B_LENGTH != 0
  402. unsigned long flags;
  403. void *addr;
  404. unsigned int cpu;
  405. cpu = smp_processor_id();
  406. /* add mutex operation */
  407. spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
  408. addr = _sram_alloc(size, &per_cpu(free_l1_data_B_sram_head, cpu),
  409. &per_cpu(used_l1_data_B_sram_head, cpu));
  410. /* add mutex operation */
  411. spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
  412. pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
  413. (long unsigned int)addr, size);
  414. return addr;
  415. #else
  416. return NULL;
  417. #endif
  418. }
  419. EXPORT_SYMBOL(l1_data_B_sram_alloc);
  420. int l1_data_B_sram_free(const void *addr)
  421. {
  422. #if L1_DATA_B_LENGTH != 0
  423. unsigned long flags;
  424. int ret;
  425. unsigned int cpu;
  426. cpu = smp_processor_id();
  427. /* add mutex operation */
  428. spin_lock_irqsave(&per_cpu(l1_data_sram_lock, cpu), flags);
  429. ret = _sram_free(addr, &per_cpu(free_l1_data_B_sram_head, cpu),
  430. &per_cpu(used_l1_data_B_sram_head, cpu));
  431. /* add mutex operation */
  432. spin_unlock_irqrestore(&per_cpu(l1_data_sram_lock, cpu), flags);
  433. return ret;
  434. #else
  435. return -1;
  436. #endif
  437. }
  438. EXPORT_SYMBOL(l1_data_B_sram_free);
  439. void *l1_data_sram_alloc(size_t size)
  440. {
  441. void *addr = l1_data_A_sram_alloc(size);
  442. if (!addr)
  443. addr = l1_data_B_sram_alloc(size);
  444. return addr;
  445. }
  446. EXPORT_SYMBOL(l1_data_sram_alloc);
  447. void *l1_data_sram_zalloc(size_t size)
  448. {
  449. void *addr = l1_data_sram_alloc(size);
  450. if (addr)
  451. memset(addr, 0x00, size);
  452. return addr;
  453. }
  454. EXPORT_SYMBOL(l1_data_sram_zalloc);
  455. int l1_data_sram_free(const void *addr)
  456. {
  457. int ret;
  458. ret = l1_data_A_sram_free(addr);
  459. if (ret == -1)
  460. ret = l1_data_B_sram_free(addr);
  461. return ret;
  462. }
  463. EXPORT_SYMBOL(l1_data_sram_free);
  464. void *l1_inst_sram_alloc(size_t size)
  465. {
  466. #if L1_CODE_LENGTH != 0
  467. unsigned long flags;
  468. void *addr;
  469. unsigned int cpu;
  470. cpu = smp_processor_id();
  471. /* add mutex operation */
  472. spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
  473. addr = _sram_alloc(size, &per_cpu(free_l1_inst_sram_head, cpu),
  474. &per_cpu(used_l1_inst_sram_head, cpu));
  475. /* add mutex operation */
  476. spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
  477. pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
  478. (long unsigned int)addr, size);
  479. return addr;
  480. #else
  481. return NULL;
  482. #endif
  483. }
  484. EXPORT_SYMBOL(l1_inst_sram_alloc);
  485. int l1_inst_sram_free(const void *addr)
  486. {
  487. #if L1_CODE_LENGTH != 0
  488. unsigned long flags;
  489. int ret;
  490. unsigned int cpu;
  491. cpu = smp_processor_id();
  492. /* add mutex operation */
  493. spin_lock_irqsave(&per_cpu(l1_inst_sram_lock, cpu), flags);
  494. ret = _sram_free(addr, &per_cpu(free_l1_inst_sram_head, cpu),
  495. &per_cpu(used_l1_inst_sram_head, cpu));
  496. /* add mutex operation */
  497. spin_unlock_irqrestore(&per_cpu(l1_inst_sram_lock, cpu), flags);
  498. return ret;
  499. #else
  500. return -1;
  501. #endif
  502. }
  503. EXPORT_SYMBOL(l1_inst_sram_free);
  504. /* L1 Scratchpad memory allocate function */
  505. void *l1sram_alloc(size_t size)
  506. {
  507. unsigned long flags;
  508. void *addr;
  509. unsigned int cpu;
  510. cpu = smp_processor_id();
  511. /* add mutex operation */
  512. spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
  513. addr = _sram_alloc(size, &per_cpu(free_l1_ssram_head, cpu),
  514. &per_cpu(used_l1_ssram_head, cpu));
  515. /* add mutex operation */
  516. spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
  517. return addr;
  518. }
  519. /* L1 Scratchpad memory allocate function */
  520. void *l1sram_alloc_max(size_t *psize)
  521. {
  522. unsigned long flags;
  523. void *addr;
  524. unsigned int cpu;
  525. cpu = smp_processor_id();
  526. /* add mutex operation */
  527. spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
  528. addr = _sram_alloc_max(&per_cpu(free_l1_ssram_head, cpu),
  529. &per_cpu(used_l1_ssram_head, cpu), psize);
  530. /* add mutex operation */
  531. spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
  532. return addr;
  533. }
  534. /* L1 Scratchpad memory free function */
  535. int l1sram_free(const void *addr)
  536. {
  537. unsigned long flags;
  538. int ret;
  539. unsigned int cpu;
  540. cpu = smp_processor_id();
  541. /* add mutex operation */
  542. spin_lock_irqsave(&per_cpu(l1sram_lock, cpu), flags);
  543. ret = _sram_free(addr, &per_cpu(free_l1_ssram_head, cpu),
  544. &per_cpu(used_l1_ssram_head, cpu));
  545. /* add mutex operation */
  546. spin_unlock_irqrestore(&per_cpu(l1sram_lock, cpu), flags);
  547. return ret;
  548. }
  549. void *l2_sram_alloc(size_t size)
  550. {
  551. #if L2_LENGTH != 0
  552. unsigned long flags;
  553. void *addr;
  554. /* add mutex operation */
  555. spin_lock_irqsave(&l2_sram_lock, flags);
  556. addr = _sram_alloc(size, &free_l2_sram_head,
  557. &used_l2_sram_head);
  558. /* add mutex operation */
  559. spin_unlock_irqrestore(&l2_sram_lock, flags);
  560. pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
  561. (long unsigned int)addr, size);
  562. return addr;
  563. #else
  564. return NULL;
  565. #endif
  566. }
  567. EXPORT_SYMBOL(l2_sram_alloc);
  568. void *l2_sram_zalloc(size_t size)
  569. {
  570. void *addr = l2_sram_alloc(size);
  571. if (addr)
  572. memset(addr, 0x00, size);
  573. return addr;
  574. }
  575. EXPORT_SYMBOL(l2_sram_zalloc);
  576. int l2_sram_free(const void *addr)
  577. {
  578. #if L2_LENGTH != 0
  579. unsigned long flags;
  580. int ret;
  581. /* add mutex operation */
  582. spin_lock_irqsave(&l2_sram_lock, flags);
  583. ret = _sram_free(addr, &free_l2_sram_head,
  584. &used_l2_sram_head);
  585. /* add mutex operation */
  586. spin_unlock_irqrestore(&l2_sram_lock, flags);
  587. return ret;
  588. #else
  589. return -1;
  590. #endif
  591. }
  592. EXPORT_SYMBOL(l2_sram_free);
  593. int sram_free_with_lsl(const void *addr)
  594. {
  595. struct sram_list_struct *lsl, **tmp;
  596. struct mm_struct *mm = current->mm;
  597. int ret = -1;
  598. for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
  599. if ((*tmp)->addr == addr) {
  600. lsl = *tmp;
  601. ret = sram_free(addr);
  602. *tmp = lsl->next;
  603. kfree(lsl);
  604. break;
  605. }
  606. return ret;
  607. }
  608. EXPORT_SYMBOL(sram_free_with_lsl);
  609. /* Allocate memory and keep in L1 SRAM List (lsl) so that the resources are
  610. * tracked. These are designed for userspace so that when a process exits,
  611. * we can safely reap their resources.
  612. */
  613. void *sram_alloc_with_lsl(size_t size, unsigned long flags)
  614. {
  615. void *addr = NULL;
  616. struct sram_list_struct *lsl = NULL;
  617. struct mm_struct *mm = current->mm;
  618. lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
  619. if (!lsl)
  620. return NULL;
  621. if (flags & L1_INST_SRAM)
  622. addr = l1_inst_sram_alloc(size);
  623. if (addr == NULL && (flags & L1_DATA_A_SRAM))
  624. addr = l1_data_A_sram_alloc(size);
  625. if (addr == NULL && (flags & L1_DATA_B_SRAM))
  626. addr = l1_data_B_sram_alloc(size);
  627. if (addr == NULL && (flags & L2_SRAM))
  628. addr = l2_sram_alloc(size);
  629. if (addr == NULL) {
  630. kfree(lsl);
  631. return NULL;
  632. }
  633. lsl->addr = addr;
  634. lsl->length = size;
  635. lsl->next = mm->context.sram_list;
  636. mm->context.sram_list = lsl;
  637. return addr;
  638. }
  639. EXPORT_SYMBOL(sram_alloc_with_lsl);
  640. #ifdef CONFIG_PROC_FS
  641. /* Once we get a real allocator, we'll throw all of this away.
  642. * Until then, we need some sort of visibility into the L1 alloc.
  643. */
  644. /* Need to keep line of output the same. Currently, that is 44 bytes
  645. * (including newline).
  646. */
  647. static int _sram_proc_show(struct seq_file *m, const char *desc,
  648. struct sram_piece *pfree_head,
  649. struct sram_piece *pused_head)
  650. {
  651. struct sram_piece *pslot;
  652. if (!pfree_head || !pused_head)
  653. return -1;
  654. seq_printf(m, "--- SRAM %-14s Size PID State \n", desc);
  655. /* search the relevant memory slot */
  656. pslot = pused_head->next;
  657. while (pslot != NULL) {
  658. seq_printf(m, "%p-%p %10i %5i %-10s\n",
  659. pslot->paddr, pslot->paddr + pslot->size,
  660. pslot->size, pslot->pid, "ALLOCATED");
  661. pslot = pslot->next;
  662. }
  663. pslot = pfree_head->next;
  664. while (pslot != NULL) {
  665. seq_printf(m, "%p-%p %10i %5i %-10s\n",
  666. pslot->paddr, pslot->paddr + pslot->size,
  667. pslot->size, pslot->pid, "FREE");
  668. pslot = pslot->next;
  669. }
  670. return 0;
  671. }
  672. static int sram_proc_show(struct seq_file *m, void *v)
  673. {
  674. unsigned int cpu;
  675. for (cpu = 0; cpu < num_possible_cpus(); ++cpu) {
  676. if (_sram_proc_show(m, "Scratchpad",
  677. &per_cpu(free_l1_ssram_head, cpu), &per_cpu(used_l1_ssram_head, cpu)))
  678. goto not_done;
  679. #if L1_DATA_A_LENGTH != 0
  680. if (_sram_proc_show(m, "L1 Data A",
  681. &per_cpu(free_l1_data_A_sram_head, cpu),
  682. &per_cpu(used_l1_data_A_sram_head, cpu)))
  683. goto not_done;
  684. #endif
  685. #if L1_DATA_B_LENGTH != 0
  686. if (_sram_proc_show(m, "L1 Data B",
  687. &per_cpu(free_l1_data_B_sram_head, cpu),
  688. &per_cpu(used_l1_data_B_sram_head, cpu)))
  689. goto not_done;
  690. #endif
  691. #if L1_CODE_LENGTH != 0
  692. if (_sram_proc_show(m, "L1 Instruction",
  693. &per_cpu(free_l1_inst_sram_head, cpu),
  694. &per_cpu(used_l1_inst_sram_head, cpu)))
  695. goto not_done;
  696. #endif
  697. }
  698. #if L2_LENGTH != 0
  699. if (_sram_proc_show(m, "L2", &free_l2_sram_head, &used_l2_sram_head))
  700. goto not_done;
  701. #endif
  702. not_done:
  703. return 0;
  704. }
  705. static int sram_proc_open(struct inode *inode, struct file *file)
  706. {
  707. return single_open(file, sram_proc_show, NULL);
  708. }
  709. static const struct file_operations sram_proc_ops = {
  710. .open = sram_proc_open,
  711. .read = seq_read,
  712. .llseek = seq_lseek,
  713. .release = single_release,
  714. };
  715. static int __init sram_proc_init(void)
  716. {
  717. struct proc_dir_entry *ptr;
  718. ptr = proc_create("sram", S_IRUGO, NULL, &sram_proc_ops);
  719. if (!ptr) {
  720. printk(KERN_WARNING "unable to create /proc/sram\n");
  721. return -1;
  722. }
  723. return 0;
  724. }
  725. late_initcall(sram_proc_init);
  726. #endif