blackfin_sram.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * File: arch/blackfin/mm/blackfin_sram.c
  3. * Based on:
  4. * Author:
  5. *
  6. * Created:
  7. * Description: SRAM driver for Blackfin ADSP-BF5xx
  8. *
  9. * Modified:
  10. * Copyright 2004-2007 Analog Devices Inc.
  11. *
  12. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see the file COPYING, or write
  26. * to the Free Software Foundation, Inc.,
  27. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/types.h>
  32. #include <linux/miscdevice.h>
  33. #include <linux/ioport.h>
  34. #include <linux/fcntl.h>
  35. #include <linux/init.h>
  36. #include <linux/poll.h>
  37. #include <linux/proc_fs.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/rtc.h>
  40. #include <asm/blackfin.h>
  41. #include "blackfin_sram.h"
  42. spinlock_t l1sram_lock, l1_data_sram_lock, l1_inst_sram_lock;
  43. #if CONFIG_L1_MAX_PIECE < 16
  44. #undef CONFIG_L1_MAX_PIECE
  45. #define CONFIG_L1_MAX_PIECE 16
  46. #endif
  47. #if CONFIG_L1_MAX_PIECE > 1024
  48. #undef CONFIG_L1_MAX_PIECE
  49. #define CONFIG_L1_MAX_PIECE 1024
  50. #endif
  51. #define SRAM_SLT_NULL 0
  52. #define SRAM_SLT_FREE 1
  53. #define SRAM_SLT_ALLOCATED 2
  54. /* the data structure for L1 scratchpad and DATA SRAM */
  55. struct l1_sram_piece {
  56. void *paddr;
  57. int size;
  58. int flag;
  59. pid_t pid;
  60. };
  61. static struct l1_sram_piece l1_ssram[CONFIG_L1_MAX_PIECE];
  62. #if L1_DATA_A_LENGTH != 0
  63. static struct l1_sram_piece l1_data_A_sram[CONFIG_L1_MAX_PIECE];
  64. #endif
  65. #if L1_DATA_B_LENGTH != 0
  66. static struct l1_sram_piece l1_data_B_sram[CONFIG_L1_MAX_PIECE];
  67. #endif
  68. #if L1_CODE_LENGTH != 0
  69. static struct l1_sram_piece l1_inst_sram[CONFIG_L1_MAX_PIECE];
  70. #endif
  71. /* L1 Scratchpad SRAM initialization function */
  72. void __init l1sram_init(void)
  73. {
  74. printk(KERN_INFO "Blackfin Scratchpad data SRAM: %d KB\n",
  75. L1_SCRATCH_LENGTH >> 10);
  76. memset(&l1_ssram, 0x00, sizeof(l1_ssram));
  77. l1_ssram[0].paddr = (void *)L1_SCRATCH_START;
  78. l1_ssram[0].size = L1_SCRATCH_LENGTH;
  79. l1_ssram[0].flag = SRAM_SLT_FREE;
  80. /* mutex initialize */
  81. spin_lock_init(&l1sram_lock);
  82. }
  83. void __init l1_data_sram_init(void)
  84. {
  85. #if L1_DATA_A_LENGTH != 0
  86. memset(&l1_data_A_sram, 0x00, sizeof(l1_data_A_sram));
  87. l1_data_A_sram[0].paddr = (void *)L1_DATA_A_START +
  88. (_ebss_l1 - _sdata_l1);
  89. l1_data_A_sram[0].size = L1_DATA_A_LENGTH - (_ebss_l1 - _sdata_l1);
  90. l1_data_A_sram[0].flag = SRAM_SLT_FREE;
  91. printk(KERN_INFO "Blackfin Data A SRAM: %d KB (%d KB free)\n",
  92. L1_DATA_A_LENGTH >> 10, l1_data_A_sram[0].size >> 10);
  93. #endif
  94. #if L1_DATA_B_LENGTH != 0
  95. memset(&l1_data_B_sram, 0x00, sizeof(l1_data_B_sram));
  96. l1_data_B_sram[0].paddr = (void *)L1_DATA_B_START +
  97. (_ebss_b_l1 - _sdata_b_l1);
  98. l1_data_B_sram[0].size = L1_DATA_B_LENGTH - (_ebss_b_l1 - _sdata_b_l1);
  99. l1_data_B_sram[0].flag = SRAM_SLT_FREE;
  100. printk(KERN_INFO "Blackfin Data B SRAM: %d KB (%d KB free)\n",
  101. L1_DATA_B_LENGTH >> 10, l1_data_B_sram[0].size >> 10);
  102. #endif
  103. /* mutex initialize */
  104. spin_lock_init(&l1_data_sram_lock);
  105. }
  106. void __init l1_inst_sram_init(void)
  107. {
  108. #if L1_CODE_LENGTH != 0
  109. memset(&l1_inst_sram, 0x00, sizeof(l1_inst_sram));
  110. l1_inst_sram[0].paddr = (void *)L1_CODE_START + (_etext_l1 - _stext_l1);
  111. l1_inst_sram[0].size = L1_CODE_LENGTH - (_etext_l1 - _stext_l1);
  112. l1_inst_sram[0].flag = SRAM_SLT_FREE;
  113. printk(KERN_INFO "Blackfin Instruction SRAM: %d KB (%d KB free)\n",
  114. L1_CODE_LENGTH >> 10, l1_inst_sram[0].size >> 10);
  115. #endif
  116. /* mutex initialize */
  117. spin_lock_init(&l1_inst_sram_lock);
  118. }
  119. /* L1 memory allocate function */
  120. static void *_l1_sram_alloc(size_t size, struct l1_sram_piece *pfree, int count)
  121. {
  122. int i, index = 0;
  123. void *addr = NULL;
  124. if (size <= 0)
  125. return NULL;
  126. /* Align the size */
  127. size = (size + 3) & ~3;
  128. /* not use the good method to match the best slot !!! */
  129. /* search an available memory slot */
  130. for (i = 0; i < count; i++) {
  131. if ((pfree[i].flag == SRAM_SLT_FREE)
  132. && (pfree[i].size >= size)) {
  133. addr = pfree[i].paddr;
  134. pfree[i].flag = SRAM_SLT_ALLOCATED;
  135. pfree[i].pid = current->pid;
  136. index = i;
  137. break;
  138. }
  139. }
  140. if (i >= count)
  141. return NULL;
  142. /* updated the NULL memory slot !!! */
  143. if (pfree[i].size > size) {
  144. for (i = 0; i < count; i++) {
  145. if (pfree[i].flag == SRAM_SLT_NULL) {
  146. pfree[i].pid = 0;
  147. pfree[i].flag = SRAM_SLT_FREE;
  148. pfree[i].paddr = addr + size;
  149. pfree[i].size = pfree[index].size - size;
  150. pfree[index].size = size;
  151. break;
  152. }
  153. }
  154. }
  155. return addr;
  156. }
  157. /* Allocate the largest available block. */
  158. static void *_l1_sram_alloc_max(struct l1_sram_piece *pfree, int count,
  159. unsigned long *psize)
  160. {
  161. unsigned long best = 0;
  162. int i, index = -1;
  163. void *addr = NULL;
  164. /* search an available memory slot */
  165. for (i = 0; i < count; i++) {
  166. if (pfree[i].flag == SRAM_SLT_FREE && pfree[i].size > best) {
  167. addr = pfree[i].paddr;
  168. index = i;
  169. best = pfree[i].size;
  170. }
  171. }
  172. if (index < 0)
  173. return NULL;
  174. *psize = best;
  175. pfree[index].pid = current->pid;
  176. pfree[index].flag = SRAM_SLT_ALLOCATED;
  177. return addr;
  178. }
  179. /* L1 memory free function */
  180. static int _l1_sram_free(const void *addr,
  181. struct l1_sram_piece *pfree,
  182. int count)
  183. {
  184. int i, index = 0;
  185. /* search the relevant memory slot */
  186. for (i = 0; i < count; i++) {
  187. if (pfree[i].paddr == addr) {
  188. if (pfree[i].flag != SRAM_SLT_ALLOCATED) {
  189. /* error log */
  190. return -1;
  191. }
  192. index = i;
  193. break;
  194. }
  195. }
  196. if (i >= count)
  197. return -1;
  198. pfree[index].pid = 0;
  199. pfree[index].flag = SRAM_SLT_FREE;
  200. /* link the next address slot */
  201. for (i = 0; i < count; i++) {
  202. if (((pfree[index].paddr + pfree[index].size) == pfree[i].paddr)
  203. && (pfree[i].flag == SRAM_SLT_FREE)) {
  204. pfree[i].pid = 0;
  205. pfree[i].flag = SRAM_SLT_NULL;
  206. pfree[index].size += pfree[i].size;
  207. pfree[index].flag = SRAM_SLT_FREE;
  208. break;
  209. }
  210. }
  211. /* link the last address slot */
  212. for (i = 0; i < count; i++) {
  213. if (((pfree[i].paddr + pfree[i].size) == pfree[index].paddr) &&
  214. (pfree[i].flag == SRAM_SLT_FREE)) {
  215. pfree[index].flag = SRAM_SLT_NULL;
  216. pfree[i].size += pfree[index].size;
  217. break;
  218. }
  219. }
  220. return 0;
  221. }
  222. int sram_free(const void *addr)
  223. {
  224. if (0) {}
  225. #if L1_CODE_LENGTH != 0
  226. else if (addr >= (void *)L1_CODE_START
  227. && addr < (void *)(L1_CODE_START + L1_CODE_LENGTH))
  228. return l1_inst_sram_free(addr);
  229. #endif
  230. #if L1_DATA_A_LENGTH != 0
  231. else if (addr >= (void *)L1_DATA_A_START
  232. && addr < (void *)(L1_DATA_A_START + L1_DATA_A_LENGTH))
  233. return l1_data_A_sram_free(addr);
  234. #endif
  235. #if L1_DATA_B_LENGTH != 0
  236. else if (addr >= (void *)L1_DATA_B_START
  237. && addr < (void *)(L1_DATA_B_START + L1_DATA_B_LENGTH))
  238. return l1_data_B_sram_free(addr);
  239. #endif
  240. else
  241. return -1;
  242. }
  243. EXPORT_SYMBOL(sram_free);
  244. void *l1_data_A_sram_alloc(size_t size)
  245. {
  246. unsigned flags;
  247. void *addr = NULL;
  248. /* add mutex operation */
  249. spin_lock_irqsave(&l1_data_sram_lock, flags);
  250. #if L1_DATA_A_LENGTH != 0
  251. addr = _l1_sram_alloc(size, l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
  252. #endif
  253. /* add mutex operation */
  254. spin_unlock_irqrestore(&l1_data_sram_lock, flags);
  255. pr_debug("Allocated address in l1_data_A_sram_alloc is 0x%lx+0x%lx\n",
  256. (long unsigned int)addr, size);
  257. return addr;
  258. }
  259. EXPORT_SYMBOL(l1_data_A_sram_alloc);
  260. int l1_data_A_sram_free(const void *addr)
  261. {
  262. unsigned flags;
  263. int ret;
  264. /* add mutex operation */
  265. spin_lock_irqsave(&l1_data_sram_lock, flags);
  266. #if L1_DATA_A_LENGTH != 0
  267. ret = _l1_sram_free(addr,
  268. l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
  269. #else
  270. ret = -1;
  271. #endif
  272. /* add mutex operation */
  273. spin_unlock_irqrestore(&l1_data_sram_lock, flags);
  274. return ret;
  275. }
  276. EXPORT_SYMBOL(l1_data_A_sram_free);
  277. void *l1_data_B_sram_alloc(size_t size)
  278. {
  279. #if L1_DATA_B_LENGTH != 0
  280. unsigned flags;
  281. void *addr;
  282. /* add mutex operation */
  283. spin_lock_irqsave(&l1_data_sram_lock, flags);
  284. addr = _l1_sram_alloc(size, l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
  285. /* add mutex operation */
  286. spin_unlock_irqrestore(&l1_data_sram_lock, flags);
  287. pr_debug("Allocated address in l1_data_B_sram_alloc is 0x%lx+0x%lx\n",
  288. (long unsigned int)addr, size);
  289. return addr;
  290. #else
  291. return NULL;
  292. #endif
  293. }
  294. EXPORT_SYMBOL(l1_data_B_sram_alloc);
  295. int l1_data_B_sram_free(const void *addr)
  296. {
  297. #if L1_DATA_B_LENGTH != 0
  298. unsigned flags;
  299. int ret;
  300. /* add mutex operation */
  301. spin_lock_irqsave(&l1_data_sram_lock, flags);
  302. ret = _l1_sram_free(addr, l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
  303. /* add mutex operation */
  304. spin_unlock_irqrestore(&l1_data_sram_lock, flags);
  305. return ret;
  306. #else
  307. return -1;
  308. #endif
  309. }
  310. EXPORT_SYMBOL(l1_data_B_sram_free);
  311. void *l1_data_sram_alloc(size_t size)
  312. {
  313. void *addr = l1_data_A_sram_alloc(size);
  314. if (!addr)
  315. addr = l1_data_B_sram_alloc(size);
  316. return addr;
  317. }
  318. EXPORT_SYMBOL(l1_data_sram_alloc);
  319. void *l1_data_sram_zalloc(size_t size)
  320. {
  321. void *addr = l1_data_sram_alloc(size);
  322. if (addr)
  323. memset(addr, 0x00, size);
  324. return addr;
  325. }
  326. EXPORT_SYMBOL(l1_data_sram_zalloc);
  327. int l1_data_sram_free(const void *addr)
  328. {
  329. int ret;
  330. ret = l1_data_A_sram_free(addr);
  331. if (ret == -1)
  332. ret = l1_data_B_sram_free(addr);
  333. return ret;
  334. }
  335. EXPORT_SYMBOL(l1_data_sram_free);
  336. void *l1_inst_sram_alloc(size_t size)
  337. {
  338. #if L1_DATA_A_LENGTH != 0
  339. unsigned flags;
  340. void *addr;
  341. /* add mutex operation */
  342. spin_lock_irqsave(&l1_inst_sram_lock, flags);
  343. addr = _l1_sram_alloc(size, l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
  344. /* add mutex operation */
  345. spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
  346. pr_debug("Allocated address in l1_inst_sram_alloc is 0x%lx+0x%lx\n",
  347. (long unsigned int)addr, size);
  348. return addr;
  349. #else
  350. return NULL;
  351. #endif
  352. }
  353. EXPORT_SYMBOL(l1_inst_sram_alloc);
  354. int l1_inst_sram_free(const void *addr)
  355. {
  356. #if L1_CODE_LENGTH != 0
  357. unsigned flags;
  358. int ret;
  359. /* add mutex operation */
  360. spin_lock_irqsave(&l1_inst_sram_lock, flags);
  361. ret = _l1_sram_free(addr, l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
  362. /* add mutex operation */
  363. spin_unlock_irqrestore(&l1_inst_sram_lock, flags);
  364. return ret;
  365. #else
  366. return -1;
  367. #endif
  368. }
  369. EXPORT_SYMBOL(l1_inst_sram_free);
  370. /* L1 Scratchpad memory allocate function */
  371. void *l1sram_alloc(size_t size)
  372. {
  373. unsigned flags;
  374. void *addr;
  375. /* add mutex operation */
  376. spin_lock_irqsave(&l1sram_lock, flags);
  377. addr = _l1_sram_alloc(size, l1_ssram, ARRAY_SIZE(l1_ssram));
  378. /* add mutex operation */
  379. spin_unlock_irqrestore(&l1sram_lock, flags);
  380. return addr;
  381. }
  382. /* L1 Scratchpad memory allocate function */
  383. void *l1sram_alloc_max(size_t *psize)
  384. {
  385. unsigned flags;
  386. void *addr;
  387. /* add mutex operation */
  388. spin_lock_irqsave(&l1sram_lock, flags);
  389. addr = _l1_sram_alloc_max(l1_ssram, ARRAY_SIZE(l1_ssram), psize);
  390. /* add mutex operation */
  391. spin_unlock_irqrestore(&l1sram_lock, flags);
  392. return addr;
  393. }
  394. /* L1 Scratchpad memory free function */
  395. int l1sram_free(const void *addr)
  396. {
  397. unsigned flags;
  398. int ret;
  399. /* add mutex operation */
  400. spin_lock_irqsave(&l1sram_lock, flags);
  401. ret = _l1_sram_free(addr, l1_ssram, ARRAY_SIZE(l1_ssram));
  402. /* add mutex operation */
  403. spin_unlock_irqrestore(&l1sram_lock, flags);
  404. return ret;
  405. }
  406. int sram_free_with_lsl(const void *addr)
  407. {
  408. struct sram_list_struct *lsl, **tmp;
  409. struct mm_struct *mm = current->mm;
  410. for (tmp = &mm->context.sram_list; *tmp; tmp = &(*tmp)->next)
  411. if ((*tmp)->addr == addr)
  412. goto found;
  413. return -1;
  414. found:
  415. lsl = *tmp;
  416. sram_free(addr);
  417. *tmp = lsl->next;
  418. kfree(lsl);
  419. return 0;
  420. }
  421. EXPORT_SYMBOL(sram_free_with_lsl);
  422. void *sram_alloc_with_lsl(size_t size, unsigned long flags)
  423. {
  424. void *addr = NULL;
  425. struct sram_list_struct *lsl = NULL;
  426. struct mm_struct *mm = current->mm;
  427. lsl = kzalloc(sizeof(struct sram_list_struct), GFP_KERNEL);
  428. if (!lsl)
  429. return NULL;
  430. if (flags & L1_INST_SRAM)
  431. addr = l1_inst_sram_alloc(size);
  432. if (addr == NULL && (flags & L1_DATA_A_SRAM))
  433. addr = l1_data_A_sram_alloc(size);
  434. if (addr == NULL && (flags & L1_DATA_B_SRAM))
  435. addr = l1_data_B_sram_alloc(size);
  436. if (addr == NULL) {
  437. kfree(lsl);
  438. return NULL;
  439. }
  440. lsl->addr = addr;
  441. lsl->length = size;
  442. lsl->next = mm->context.sram_list;
  443. mm->context.sram_list = lsl;
  444. return addr;
  445. }
  446. EXPORT_SYMBOL(sram_alloc_with_lsl);
  447. #ifdef CONFIG_PROC_FS
  448. /* Once we get a real allocator, we'll throw all of this away.
  449. * Until then, we need some sort of visibility into the L1 alloc.
  450. */
  451. static void _l1sram_proc_read(char *buf, int *len, const char *desc,
  452. struct l1_sram_piece *pfree, const int array_size)
  453. {
  454. int i;
  455. *len += sprintf(&buf[*len], "--- L1 %-14s Size PID State\n", desc);
  456. for (i = 0; i < array_size; ++i) {
  457. const char *alloc_type;
  458. switch (pfree[i].flag) {
  459. case SRAM_SLT_NULL: alloc_type = "NULL"; break;
  460. case SRAM_SLT_FREE: alloc_type = "FREE"; break;
  461. case SRAM_SLT_ALLOCATED: alloc_type = "ALLOCATED"; break;
  462. default: alloc_type = "????"; break;
  463. }
  464. *len += sprintf(&buf[*len], "%p-%p %8i %4i %s\n",
  465. pfree[i].paddr, pfree[i].paddr + pfree[i].size,
  466. pfree[i].size, pfree[i].pid, alloc_type);
  467. }
  468. }
  469. static int l1sram_proc_read(char *buf, char **start, off_t offset, int count,
  470. int *eof, void *data)
  471. {
  472. int len = 0;
  473. _l1sram_proc_read(buf, &len, "Scratchpad",
  474. l1_ssram, ARRAY_SIZE(l1_ssram));
  475. #if L1_DATA_A_LENGTH != 0
  476. _l1sram_proc_read(buf, &len, "Data A",
  477. l1_data_A_sram, ARRAY_SIZE(l1_data_A_sram));
  478. #endif
  479. #if L1_DATA_B_LENGTH != 0
  480. _l1sram_proc_read(buf, &len, "Data B",
  481. l1_data_B_sram, ARRAY_SIZE(l1_data_B_sram));
  482. #endif
  483. #if L1_CODE_LENGTH != 0
  484. _l1sram_proc_read(buf, &len, "Instruction",
  485. l1_inst_sram, ARRAY_SIZE(l1_inst_sram));
  486. #endif
  487. return len;
  488. }
  489. static int __init l1sram_proc_init(void)
  490. {
  491. struct proc_dir_entry *ptr;
  492. ptr = create_proc_entry("sram", S_IFREG | S_IRUGO, NULL);
  493. if (!ptr) {
  494. printk(KERN_WARNING "unable to create /proc/sram\n");
  495. return -1;
  496. }
  497. ptr->owner = THIS_MODULE;
  498. ptr->read_proc = l1sram_proc_read;
  499. return 0;
  500. }
  501. late_initcall(l1sram_proc_init);
  502. #endif