sram-alloc.c 20 KB

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