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