sram-alloc.c 21 KB

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