sram-alloc.c 20 KB

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