sram-alloc.c 21 KB

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