sram-alloc.c 20 KB

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