blackfin_sram.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * File: arch/blackfin/mm/blackfin_sram.c
  3. * Based on:
  4. * Author:
  5. *
  6. * Created:
  7. * Description: SRAM driver for Blackfin ADSP-BF5xx
  8. *
  9. * Modified:
  10. * Copyright 2004-2007 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"Fail 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"Fail 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"Fail 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"Fail 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"Fail to initialize L2 SRAM.\n");
  155. return;
  156. }
  157. free_l2_sram_head.next->paddr = (void *)L2_START +
  158. (_etext_l2 - _stext_l2) + (_edata_l2 - _sdata_l2);
  159. free_l2_sram_head.next->size = L2_LENGTH -
  160. (_etext_l2 - _stext_l2) + (_edata_l2 - _sdata_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 (0) {}
  289. #if L1_CODE_LENGTH != 0
  290. else if (addr >= (void *)L1_CODE_START
  291. && addr < (void *)(L1_CODE_START + L1_CODE_LENGTH))
  292. return l1_inst_sram_free(addr);
  293. #endif
  294. #if L1_DATA_A_LENGTH != 0
  295. else 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. #endif
  299. #if L1_DATA_B_LENGTH != 0
  300. else if (addr >= (void *)L1_DATA_B_START
  301. && addr < (void *)(L1_DATA_B_START + L1_DATA_B_LENGTH))
  302. return l1_data_B_sram_free(addr);
  303. #endif
  304. #if L2_LENGTH != 0
  305. else if (addr >= (void *)L2_START
  306. && addr < (void *)(L2_START + L2_LENGTH))
  307. return l2_sram_free(addr);
  308. #endif
  309. else
  310. return -1;
  311. }
  312. EXPORT_SYMBOL(sram_free);
  313. void *l1_data_A_sram_alloc(size_t size)
  314. {
  315. unsigned long flags;
  316. void *addr = NULL;
  317. /* add mutex operation */
  318. spin_lock_irqsave(&l1_data_sram_lock, flags);
  319. #if L1_DATA_A_LENGTH != 0
  320. addr = _sram_alloc(size, &free_l1_data_A_sram_head,
  321. &used_l1_data_A_sram_head);
  322. #endif
  323. /* add mutex operation */
  324. spin_unlock_irqrestore(&l1_data_sram_lock, flags);
  325. pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
  326. (long unsigned int)addr, size);
  327. return addr;
  328. }
  329. EXPORT_SYMBOL(l1_data_A_sram_alloc);
  330. int l1_data_A_sram_free(const void *addr)
  331. {
  332. unsigned long flags;
  333. int ret;
  334. /* add mutex operation */
  335. spin_lock_irqsave(&l1_data_sram_lock, flags);
  336. #if L1_DATA_A_LENGTH != 0
  337. ret = _sram_free(addr, &free_l1_data_A_sram_head,
  338. &used_l1_data_A_sram_head);
  339. #else
  340. ret = -1;
  341. #endif
  342. /* add mutex operation */
  343. spin_unlock_irqrestore(&l1_data_sram_lock, flags);
  344. return ret;
  345. }
  346. EXPORT_SYMBOL(l1_data_A_sram_free);
  347. void *l1_data_B_sram_alloc(size_t size)
  348. {
  349. #if L1_DATA_B_LENGTH != 0
  350. unsigned long flags;
  351. void *addr;
  352. /* add mutex operation */
  353. spin_lock_irqsave(&l1_data_sram_lock, flags);
  354. addr = _sram_alloc(size, &free_l1_data_B_sram_head,
  355. &used_l1_data_B_sram_head);
  356. /* add mutex operation */
  357. spin_unlock_irqrestore(&l1_data_sram_lock, flags);
  358. pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
  359. (long unsigned int)addr, size);
  360. return addr;
  361. #else
  362. return NULL;
  363. #endif
  364. }
  365. EXPORT_SYMBOL(l1_data_B_sram_alloc);
  366. int l1_data_B_sram_free(const void *addr)
  367. {
  368. #if L1_DATA_B_LENGTH != 0
  369. unsigned long flags;
  370. int ret;
  371. /* add mutex operation */
  372. spin_lock_irqsave(&l1_data_sram_lock, flags);
  373. ret = _sram_free(addr, &free_l1_data_B_sram_head,
  374. &used_l1_data_B_sram_head);
  375. /* add mutex operation */
  376. spin_unlock_irqrestore(&l1_data_sram_lock, flags);
  377. return ret;
  378. #else
  379. return -1;
  380. #endif
  381. }
  382. EXPORT_SYMBOL(l1_data_B_sram_free);
  383. void *l1_data_sram_alloc(size_t size)
  384. {
  385. void *addr = l1_data_A_sram_alloc(size);
  386. if (!addr)
  387. addr = l1_data_B_sram_alloc(size);
  388. return addr;
  389. }
  390. EXPORT_SYMBOL(l1_data_sram_alloc);
  391. void *l1_data_sram_zalloc(size_t size)
  392. {
  393. void *addr = l1_data_sram_alloc(size);
  394. if (addr)
  395. memset(addr, 0x00, size);
  396. return addr;
  397. }
  398. EXPORT_SYMBOL(l1_data_sram_zalloc);
  399. int l1_data_sram_free(const void *addr)
  400. {
  401. int ret;
  402. ret = l1_data_A_sram_free(addr);
  403. if (ret == -1)
  404. ret = l1_data_B_sram_free(addr);
  405. return ret;
  406. }
  407. EXPORT_SYMBOL(l1_data_sram_free);
  408. void *l1_inst_sram_alloc(size_t size)
  409. {
  410. #if L1_CODE_LENGTH != 0
  411. unsigned long flags;
  412. void *addr;
  413. /* add mutex operation */
  414. spin_lock_irqsave(&l1_inst_sram_lock, flags);
  415. addr = _sram_alloc(size, &free_l1_inst_sram_head,
  416. &used_l1_inst_sram_head);
  417. /* add mutex operation */
  418. spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
  419. pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
  420. (long unsigned int)addr, size);
  421. return addr;
  422. #else
  423. return NULL;
  424. #endif
  425. }
  426. EXPORT_SYMBOL(l1_inst_sram_alloc);
  427. int l1_inst_sram_free(const void *addr)
  428. {
  429. #if L1_CODE_LENGTH != 0
  430. unsigned long flags;
  431. int ret;
  432. /* add mutex operation */
  433. spin_lock_irqsave(&l1_inst_sram_lock, flags);
  434. ret = _sram_free(addr, &free_l1_inst_sram_head,
  435. &used_l1_inst_sram_head);
  436. /* add mutex operation */
  437. spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
  438. return ret;
  439. #else
  440. return -1;
  441. #endif
  442. }
  443. EXPORT_SYMBOL(l1_inst_sram_free);
  444. /* L1 Scratchpad memory allocate function */
  445. void *l1sram_alloc(size_t size)
  446. {
  447. unsigned long flags;
  448. void *addr;
  449. /* add mutex operation */
  450. spin_lock_irqsave(&l1sram_lock, flags);
  451. addr = _sram_alloc(size, &free_l1_ssram_head,
  452. &used_l1_ssram_head);
  453. /* add mutex operation */
  454. spin_unlock_irqrestore(&l1sram_lock, flags);
  455. return addr;
  456. }
  457. /* L1 Scratchpad memory allocate function */
  458. void *l1sram_alloc_max(size_t *psize)
  459. {
  460. unsigned long flags;
  461. void *addr;
  462. /* add mutex operation */
  463. spin_lock_irqsave(&l1sram_lock, flags);
  464. addr = _sram_alloc_max(&free_l1_ssram_head,
  465. &used_l1_ssram_head, psize);
  466. /* add mutex operation */
  467. spin_unlock_irqrestore(&l1sram_lock, flags);
  468. return addr;
  469. }
  470. /* L1 Scratchpad memory free function */
  471. int l1sram_free(const void *addr)
  472. {
  473. unsigned long flags;
  474. int ret;
  475. /* add mutex operation */
  476. spin_lock_irqsave(&l1sram_lock, flags);
  477. ret = _sram_free(addr, &free_l1_ssram_head,
  478. &used_l1_ssram_head);
  479. /* add mutex operation */
  480. spin_unlock_irqrestore(&l1sram_lock, flags);
  481. return ret;
  482. }
  483. void *l2_sram_alloc(size_t size)
  484. {
  485. #if L2_LENGTH != 0
  486. unsigned long flags;
  487. void *addr;
  488. /* add mutex operation */
  489. spin_lock_irqsave(&l2_sram_lock, flags);
  490. addr = _sram_alloc(size, &free_l2_sram_head,
  491. &used_l2_sram_head);
  492. /* add mutex operation */
  493. spin_unlock_irqrestore(&l2_sram_lock, flags);
  494. pr_debug("Allocated address in l2_sram_alloc is 0x%lx+0x%lx\n",
  495. (long unsigned int)addr, size);
  496. return addr;
  497. #else
  498. return NULL;
  499. #endif
  500. }
  501. EXPORT_SYMBOL(l2_sram_alloc);
  502. void *l2_sram_zalloc(size_t size)
  503. {
  504. void *addr = l2_sram_alloc(size);
  505. if (addr)
  506. memset(addr, 0x00, size);
  507. return addr;
  508. }
  509. EXPORT_SYMBOL(l2_sram_zalloc);
  510. int l2_sram_free(const void *addr)
  511. {
  512. #if L2_LENGTH != 0
  513. unsigned long flags;
  514. int ret;
  515. /* add mutex operation */
  516. spin_lock_irqsave(&l2_sram_lock, flags);
  517. ret = _sram_free(addr, &free_l2_sram_head,
  518. &used_l2_sram_head);
  519. /* add mutex operation */
  520. spin_unlock_irqrestore(&l2_sram_lock, flags);
  521. return ret;
  522. #else
  523. return -1;
  524. #endif
  525. }
  526. EXPORT_SYMBOL(l2_sram_free);
  527. int sram_free_with_lsl(const void *addr)
  528. {
  529. struct sram_list_struct *lsl, **tmp;
  530. struct mm_struct *mm = current->mm;
  531. for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
  532. if ((*tmp)->addr == addr)
  533. goto found;
  534. return -1;
  535. found:
  536. lsl = *tmp;
  537. sram_free(addr);
  538. *tmp = lsl->next;
  539. kfree(lsl);
  540. return 0;
  541. }
  542. EXPORT_SYMBOL(sram_free_with_lsl);
  543. void *sram_alloc_with_lsl(size_t size, unsigned long flags)
  544. {
  545. void *addr = NULL;
  546. struct sram_list_struct *lsl = NULL;
  547. struct mm_struct *mm = current->mm;
  548. lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
  549. if (!lsl)
  550. return NULL;
  551. if (flags & L1_INST_SRAM)
  552. addr = l1_inst_sram_alloc(size);
  553. if (addr == NULL && (flags & L1_DATA_A_SRAM))
  554. addr = l1_data_A_sram_alloc(size);
  555. if (addr == NULL && (flags & L1_DATA_B_SRAM))
  556. addr = l1_data_B_sram_alloc(size);
  557. if (addr == NULL && (flags & L2_SRAM))
  558. addr = l2_sram_alloc(size);
  559. if (addr == NULL) {
  560. kfree(lsl);
  561. return NULL;
  562. }
  563. lsl->addr = addr;
  564. lsl->length = size;
  565. lsl->next = mm->context.sram_list;
  566. mm->context.sram_list = lsl;
  567. return addr;
  568. }
  569. EXPORT_SYMBOL(sram_alloc_with_lsl);
  570. #ifdef CONFIG_PROC_FS
  571. /* Once we get a real allocator, we'll throw all of this away.
  572. * Until then, we need some sort of visibility into the L1 alloc.
  573. */
  574. /* Need to keep line of output the same. Currently, that is 44 bytes
  575. * (including newline).
  576. */
  577. static int _sram_proc_read(char *buf, int *len, int count, const char *desc,
  578. struct sram_piece *pfree_head,
  579. struct sram_piece *pused_head)
  580. {
  581. struct sram_piece *pslot;
  582. if (!pfree_head || !pused_head)
  583. return -1;
  584. *len += sprintf(&buf[*len], "--- SRAM %-14s Size PID State \n", desc);
  585. /* search the relevant memory slot */
  586. pslot = pused_head->next;
  587. while (pslot != NULL) {
  588. *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
  589. pslot->paddr, pslot->paddr + pslot->size,
  590. pslot->size, pslot->pid, "ALLOCATED");
  591. pslot = pslot->next;
  592. }
  593. pslot = pfree_head->next;
  594. while (pslot != NULL) {
  595. *len += sprintf(&buf[*len], "%p-%p %10i %5i %-10s\n",
  596. pslot->paddr, pslot->paddr + pslot->size,
  597. pslot->size, pslot->pid, "FREE");
  598. pslot = pslot->next;
  599. }
  600. return 0;
  601. }
  602. static int sram_proc_read(char *buf, char **start, off_t offset, int count,
  603. int *eof, void *data)
  604. {
  605. int len = 0;
  606. if (_sram_proc_read(buf, &len, count, "Scratchpad",
  607. &free_l1_ssram_head, &used_l1_ssram_head))
  608. goto not_done;
  609. #if L1_DATA_A_LENGTH != 0
  610. if (_sram_proc_read(buf, &len, count, "L1 Data A",
  611. &free_l1_data_A_sram_head,
  612. &used_l1_data_A_sram_head))
  613. goto not_done;
  614. #endif
  615. #if L1_DATA_B_LENGTH != 0
  616. if (_sram_proc_read(buf, &len, count, "L1 Data B",
  617. &free_l1_data_B_sram_head,
  618. &used_l1_data_B_sram_head))
  619. goto not_done;
  620. #endif
  621. #if L1_CODE_LENGTH != 0
  622. if (_sram_proc_read(buf, &len, count, "L1 Instruction",
  623. &free_l1_inst_sram_head, &used_l1_inst_sram_head))
  624. goto not_done;
  625. #endif
  626. #if L2_LENGTH != 0
  627. if (_sram_proc_read(buf, &len, count, "L2",
  628. &free_l2_sram_head, &used_l2_sram_head))
  629. goto not_done;
  630. #endif
  631. *eof = 1;
  632. not_done:
  633. return len;
  634. }
  635. static int __init sram_proc_init(void)
  636. {
  637. struct proc_dir_entry *ptr;
  638. ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
  639. if (!ptr) {
  640. printk(KERN_WARNING "unable to create /proc/sram\n");
  641. return -1;
  642. }
  643. ptr->owner = THIS_MODULE;
  644. ptr->read_proc = sram_proc_read;
  645. return 0;
  646. }
  647. late_initcall(sram_proc_init);
  648. #endif