sram-alloc.c 18 KB

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