vram.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * VRAM manager for OMAP
  3. *
  4. * Copyright (C) 2009 Nokia Corporation
  5. * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*#define DEBUG*/
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/list.h>
  24. #include <linux/slab.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/memblock.h>
  27. #include <linux/completion.h>
  28. #include <linux/debugfs.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/module.h>
  31. #include <asm/setup.h>
  32. #include <plat/sram.h>
  33. #include <plat/vram.h>
  34. #include <plat/dma.h>
  35. #ifdef DEBUG
  36. #define DBG(format, ...) pr_debug("VRAM: " format, ## __VA_ARGS__)
  37. #else
  38. #define DBG(format, ...)
  39. #endif
  40. #define OMAP2_SRAM_START 0x40200000
  41. /* Maximum size, in reality this is smaller if SRAM is partially locked. */
  42. #define OMAP2_SRAM_SIZE 0xa0000 /* 640k */
  43. /* postponed regions are used to temporarily store region information at boot
  44. * time when we cannot yet allocate the region list */
  45. #define MAX_POSTPONED_REGIONS 10
  46. static bool vram_initialized;
  47. static int postponed_cnt;
  48. static struct {
  49. unsigned long paddr;
  50. size_t size;
  51. } postponed_regions[MAX_POSTPONED_REGIONS];
  52. struct vram_alloc {
  53. struct list_head list;
  54. unsigned long paddr;
  55. unsigned pages;
  56. };
  57. struct vram_region {
  58. struct list_head list;
  59. struct list_head alloc_list;
  60. unsigned long paddr;
  61. unsigned pages;
  62. };
  63. static DEFINE_MUTEX(region_mutex);
  64. static LIST_HEAD(region_list);
  65. static inline int region_mem_type(unsigned long paddr)
  66. {
  67. if (paddr >= OMAP2_SRAM_START &&
  68. paddr < OMAP2_SRAM_START + OMAP2_SRAM_SIZE)
  69. return OMAP_VRAM_MEMTYPE_SRAM;
  70. else
  71. return OMAP_VRAM_MEMTYPE_SDRAM;
  72. }
  73. static struct vram_region *omap_vram_create_region(unsigned long paddr,
  74. unsigned pages)
  75. {
  76. struct vram_region *rm;
  77. rm = kzalloc(sizeof(*rm), GFP_KERNEL);
  78. if (rm) {
  79. INIT_LIST_HEAD(&rm->alloc_list);
  80. rm->paddr = paddr;
  81. rm->pages = pages;
  82. }
  83. return rm;
  84. }
  85. #if 0
  86. static void omap_vram_free_region(struct vram_region *vr)
  87. {
  88. list_del(&vr->list);
  89. kfree(vr);
  90. }
  91. #endif
  92. static struct vram_alloc *omap_vram_create_allocation(struct vram_region *vr,
  93. unsigned long paddr, unsigned pages)
  94. {
  95. struct vram_alloc *va;
  96. struct vram_alloc *new;
  97. new = kzalloc(sizeof(*va), GFP_KERNEL);
  98. if (!new)
  99. return NULL;
  100. new->paddr = paddr;
  101. new->pages = pages;
  102. list_for_each_entry(va, &vr->alloc_list, list) {
  103. if (va->paddr > new->paddr)
  104. break;
  105. }
  106. list_add_tail(&new->list, &va->list);
  107. return new;
  108. }
  109. static void omap_vram_free_allocation(struct vram_alloc *va)
  110. {
  111. list_del(&va->list);
  112. kfree(va);
  113. }
  114. int omap_vram_add_region(unsigned long paddr, size_t size)
  115. {
  116. struct vram_region *rm;
  117. unsigned pages;
  118. if (vram_initialized) {
  119. DBG("adding region paddr %08lx size %d\n",
  120. paddr, size);
  121. size &= PAGE_MASK;
  122. pages = size >> PAGE_SHIFT;
  123. rm = omap_vram_create_region(paddr, pages);
  124. if (rm == NULL)
  125. return -ENOMEM;
  126. list_add(&rm->list, &region_list);
  127. } else {
  128. if (postponed_cnt == MAX_POSTPONED_REGIONS)
  129. return -ENOMEM;
  130. postponed_regions[postponed_cnt].paddr = paddr;
  131. postponed_regions[postponed_cnt].size = size;
  132. ++postponed_cnt;
  133. }
  134. return 0;
  135. }
  136. int omap_vram_free(unsigned long paddr, size_t size)
  137. {
  138. struct vram_region *rm;
  139. struct vram_alloc *alloc;
  140. unsigned start, end;
  141. DBG("free mem paddr %08lx size %d\n", paddr, size);
  142. size = PAGE_ALIGN(size);
  143. mutex_lock(&region_mutex);
  144. list_for_each_entry(rm, &region_list, list) {
  145. list_for_each_entry(alloc, &rm->alloc_list, list) {
  146. start = alloc->paddr;
  147. end = alloc->paddr + (alloc->pages >> PAGE_SHIFT);
  148. if (start >= paddr && end < paddr + size)
  149. goto found;
  150. }
  151. }
  152. mutex_unlock(&region_mutex);
  153. return -EINVAL;
  154. found:
  155. omap_vram_free_allocation(alloc);
  156. mutex_unlock(&region_mutex);
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(omap_vram_free);
  160. static int _omap_vram_reserve(unsigned long paddr, unsigned pages)
  161. {
  162. struct vram_region *rm;
  163. struct vram_alloc *alloc;
  164. size_t size;
  165. size = pages << PAGE_SHIFT;
  166. list_for_each_entry(rm, &region_list, list) {
  167. unsigned long start, end;
  168. DBG("checking region %lx %d\n", rm->paddr, rm->pages);
  169. if (region_mem_type(rm->paddr) != region_mem_type(paddr))
  170. continue;
  171. start = rm->paddr;
  172. end = start + (rm->pages << PAGE_SHIFT) - 1;
  173. if (start > paddr || end < paddr + size - 1)
  174. continue;
  175. DBG("block ok, checking allocs\n");
  176. list_for_each_entry(alloc, &rm->alloc_list, list) {
  177. end = alloc->paddr - 1;
  178. if (start <= paddr && end >= paddr + size - 1)
  179. goto found;
  180. start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
  181. }
  182. end = rm->paddr + (rm->pages << PAGE_SHIFT) - 1;
  183. if (!(start <= paddr && end >= paddr + size - 1))
  184. continue;
  185. found:
  186. DBG("found area start %lx, end %lx\n", start, end);
  187. if (omap_vram_create_allocation(rm, paddr, pages) == NULL)
  188. return -ENOMEM;
  189. return 0;
  190. }
  191. return -ENOMEM;
  192. }
  193. int omap_vram_reserve(unsigned long paddr, size_t size)
  194. {
  195. unsigned pages;
  196. int r;
  197. DBG("reserve mem paddr %08lx size %d\n", paddr, size);
  198. size = PAGE_ALIGN(size);
  199. pages = size >> PAGE_SHIFT;
  200. mutex_lock(&region_mutex);
  201. r = _omap_vram_reserve(paddr, pages);
  202. mutex_unlock(&region_mutex);
  203. return r;
  204. }
  205. EXPORT_SYMBOL(omap_vram_reserve);
  206. static void _omap_vram_dma_cb(int lch, u16 ch_status, void *data)
  207. {
  208. struct completion *compl = data;
  209. complete(compl);
  210. }
  211. static int _omap_vram_clear(u32 paddr, unsigned pages)
  212. {
  213. struct completion compl;
  214. unsigned elem_count;
  215. unsigned frame_count;
  216. int r;
  217. int lch;
  218. init_completion(&compl);
  219. r = omap_request_dma(OMAP_DMA_NO_DEVICE, "VRAM DMA",
  220. _omap_vram_dma_cb,
  221. &compl, &lch);
  222. if (r) {
  223. pr_err("VRAM: request_dma failed for memory clear\n");
  224. return -EBUSY;
  225. }
  226. elem_count = pages * PAGE_SIZE / 4;
  227. frame_count = 1;
  228. omap_set_dma_transfer_params(lch, OMAP_DMA_DATA_TYPE_S32,
  229. elem_count, frame_count,
  230. OMAP_DMA_SYNC_ELEMENT,
  231. 0, 0);
  232. omap_set_dma_dest_params(lch, 0, OMAP_DMA_AMODE_POST_INC,
  233. paddr, 0, 0);
  234. omap_set_dma_color_mode(lch, OMAP_DMA_CONSTANT_FILL, 0x000000);
  235. omap_start_dma(lch);
  236. if (wait_for_completion_timeout(&compl, msecs_to_jiffies(1000)) == 0) {
  237. omap_stop_dma(lch);
  238. pr_err("VRAM: dma timeout while clearing memory\n");
  239. r = -EIO;
  240. goto err;
  241. }
  242. r = 0;
  243. err:
  244. omap_free_dma(lch);
  245. return r;
  246. }
  247. static int _omap_vram_alloc(int mtype, unsigned pages, unsigned long *paddr)
  248. {
  249. struct vram_region *rm;
  250. struct vram_alloc *alloc;
  251. list_for_each_entry(rm, &region_list, list) {
  252. unsigned long start, end;
  253. DBG("checking region %lx %d\n", rm->paddr, rm->pages);
  254. if (region_mem_type(rm->paddr) != mtype)
  255. continue;
  256. start = rm->paddr;
  257. list_for_each_entry(alloc, &rm->alloc_list, list) {
  258. end = alloc->paddr;
  259. if (end - start >= pages << PAGE_SHIFT)
  260. goto found;
  261. start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
  262. }
  263. end = rm->paddr + (rm->pages << PAGE_SHIFT);
  264. found:
  265. if (end - start < pages << PAGE_SHIFT)
  266. continue;
  267. DBG("found %lx, end %lx\n", start, end);
  268. alloc = omap_vram_create_allocation(rm, start, pages);
  269. if (alloc == NULL)
  270. return -ENOMEM;
  271. *paddr = start;
  272. _omap_vram_clear(start, pages);
  273. return 0;
  274. }
  275. return -ENOMEM;
  276. }
  277. int omap_vram_alloc(int mtype, size_t size, unsigned long *paddr)
  278. {
  279. unsigned pages;
  280. int r;
  281. BUG_ON(mtype > OMAP_VRAM_MEMTYPE_MAX || !size);
  282. DBG("alloc mem type %d size %d\n", mtype, size);
  283. size = PAGE_ALIGN(size);
  284. pages = size >> PAGE_SHIFT;
  285. mutex_lock(&region_mutex);
  286. r = _omap_vram_alloc(mtype, pages, paddr);
  287. mutex_unlock(&region_mutex);
  288. return r;
  289. }
  290. EXPORT_SYMBOL(omap_vram_alloc);
  291. void omap_vram_get_info(unsigned long *vram,
  292. unsigned long *free_vram,
  293. unsigned long *largest_free_block)
  294. {
  295. struct vram_region *vr;
  296. struct vram_alloc *va;
  297. *vram = 0;
  298. *free_vram = 0;
  299. *largest_free_block = 0;
  300. mutex_lock(&region_mutex);
  301. list_for_each_entry(vr, &region_list, list) {
  302. unsigned free;
  303. unsigned long pa;
  304. pa = vr->paddr;
  305. *vram += vr->pages << PAGE_SHIFT;
  306. list_for_each_entry(va, &vr->alloc_list, list) {
  307. free = va->paddr - pa;
  308. *free_vram += free;
  309. if (free > *largest_free_block)
  310. *largest_free_block = free;
  311. pa = va->paddr + (va->pages << PAGE_SHIFT);
  312. }
  313. free = vr->paddr + (vr->pages << PAGE_SHIFT) - pa;
  314. *free_vram += free;
  315. if (free > *largest_free_block)
  316. *largest_free_block = free;
  317. }
  318. mutex_unlock(&region_mutex);
  319. }
  320. EXPORT_SYMBOL(omap_vram_get_info);
  321. #if defined(CONFIG_DEBUG_FS)
  322. static int vram_debug_show(struct seq_file *s, void *unused)
  323. {
  324. struct vram_region *vr;
  325. struct vram_alloc *va;
  326. unsigned size;
  327. mutex_lock(&region_mutex);
  328. list_for_each_entry(vr, &region_list, list) {
  329. size = vr->pages << PAGE_SHIFT;
  330. seq_printf(s, "%08lx-%08lx (%d bytes)\n",
  331. vr->paddr, vr->paddr + size - 1,
  332. size);
  333. list_for_each_entry(va, &vr->alloc_list, list) {
  334. size = va->pages << PAGE_SHIFT;
  335. seq_printf(s, " %08lx-%08lx (%d bytes)\n",
  336. va->paddr, va->paddr + size - 1,
  337. size);
  338. }
  339. }
  340. mutex_unlock(&region_mutex);
  341. return 0;
  342. }
  343. static int vram_debug_open(struct inode *inode, struct file *file)
  344. {
  345. return single_open(file, vram_debug_show, inode->i_private);
  346. }
  347. static const struct file_operations vram_debug_fops = {
  348. .open = vram_debug_open,
  349. .read = seq_read,
  350. .llseek = seq_lseek,
  351. .release = single_release,
  352. };
  353. static int __init omap_vram_create_debugfs(void)
  354. {
  355. struct dentry *d;
  356. d = debugfs_create_file("vram", S_IRUGO, NULL,
  357. NULL, &vram_debug_fops);
  358. if (IS_ERR(d))
  359. return PTR_ERR(d);
  360. return 0;
  361. }
  362. #endif
  363. static __init int omap_vram_init(void)
  364. {
  365. int i;
  366. vram_initialized = 1;
  367. for (i = 0; i < postponed_cnt; i++)
  368. omap_vram_add_region(postponed_regions[i].paddr,
  369. postponed_regions[i].size);
  370. #ifdef CONFIG_DEBUG_FS
  371. if (omap_vram_create_debugfs())
  372. pr_err("VRAM: Failed to create debugfs file\n");
  373. #endif
  374. return 0;
  375. }
  376. arch_initcall(omap_vram_init);
  377. /* boottime vram alloc stuff */
  378. /* set from board file */
  379. static u32 omap_vram_sram_start __initdata;
  380. static u32 omap_vram_sram_size __initdata;
  381. /* set from board file */
  382. static u32 omap_vram_sdram_start __initdata;
  383. static u32 omap_vram_sdram_size __initdata;
  384. /* set from kernel cmdline */
  385. static u32 omap_vram_def_sdram_size __initdata;
  386. static u32 omap_vram_def_sdram_start __initdata;
  387. static int __init omap_vram_early_vram(char *p)
  388. {
  389. omap_vram_def_sdram_size = memparse(p, &p);
  390. if (*p == ',')
  391. omap_vram_def_sdram_start = simple_strtoul(p + 1, &p, 16);
  392. return 0;
  393. }
  394. early_param("vram", omap_vram_early_vram);
  395. /*
  396. * Called from map_io. We need to call to this early enough so that we
  397. * can reserve the fixed SDRAM regions before VM could get hold of them.
  398. */
  399. void __init omap_vram_reserve_sdram_memblock(void)
  400. {
  401. u32 paddr;
  402. u32 size = 0;
  403. /* cmdline arg overrides the board file definition */
  404. if (omap_vram_def_sdram_size) {
  405. size = omap_vram_def_sdram_size;
  406. paddr = omap_vram_def_sdram_start;
  407. }
  408. if (!size) {
  409. size = omap_vram_sdram_size;
  410. paddr = omap_vram_sdram_start;
  411. }
  412. #ifdef CONFIG_OMAP2_VRAM_SIZE
  413. if (!size) {
  414. size = CONFIG_OMAP2_VRAM_SIZE * 1024 * 1024;
  415. paddr = 0;
  416. }
  417. #endif
  418. if (!size)
  419. return;
  420. size = ALIGN(size, SZ_2M);
  421. if (paddr) {
  422. if (paddr & ~PAGE_MASK) {
  423. pr_err("VRAM start address 0x%08x not page aligned\n",
  424. paddr);
  425. return;
  426. }
  427. if (!memblock_is_region_memory(paddr, size)) {
  428. pr_err("Illegal SDRAM region 0x%08x..0x%08x for VRAM\n",
  429. paddr, paddr + size - 1);
  430. return;
  431. }
  432. if (memblock_is_region_reserved(paddr, size)) {
  433. pr_err("FB: failed to reserve VRAM - busy\n");
  434. return;
  435. }
  436. if (memblock_reserve(paddr, size) < 0) {
  437. pr_err("FB: failed to reserve VRAM - no memory\n");
  438. return;
  439. }
  440. } else {
  441. paddr = memblock_alloc(size, SZ_2M);
  442. }
  443. memblock_free(paddr, size);
  444. memblock_remove(paddr, size);
  445. omap_vram_add_region(paddr, size);
  446. pr_info("Reserving %u bytes SDRAM for VRAM\n", size);
  447. }
  448. /*
  449. * Called at sram init time, before anything is pushed to the SRAM stack.
  450. * Because of the stack scheme, we will allocate everything from the
  451. * start of the lowest address region to the end of SRAM. This will also
  452. * include padding for page alignment and possible holes between regions.
  453. *
  454. * As opposed to the SDRAM case, we'll also do any dynamic allocations at
  455. * this point, since the driver built as a module would have problem with
  456. * freeing / reallocating the regions.
  457. */
  458. unsigned long __init omap_vram_reserve_sram(unsigned long sram_pstart,
  459. unsigned long sram_vstart,
  460. unsigned long sram_size,
  461. unsigned long pstart_avail,
  462. unsigned long size_avail)
  463. {
  464. unsigned long pend_avail;
  465. unsigned long reserved;
  466. u32 paddr;
  467. u32 size;
  468. paddr = omap_vram_sram_start;
  469. size = omap_vram_sram_size;
  470. if (!size)
  471. return 0;
  472. reserved = 0;
  473. pend_avail = pstart_avail + size_avail;
  474. if (!paddr) {
  475. /* Dynamic allocation */
  476. if ((size_avail & PAGE_MASK) < size) {
  477. pr_err("Not enough SRAM for VRAM\n");
  478. return 0;
  479. }
  480. size_avail = (size_avail - size) & PAGE_MASK;
  481. paddr = pstart_avail + size_avail;
  482. }
  483. if (paddr < sram_pstart ||
  484. paddr + size > sram_pstart + sram_size) {
  485. pr_err("Illegal SRAM region for VRAM\n");
  486. return 0;
  487. }
  488. /* Reserve everything above the start of the region. */
  489. if (pend_avail - paddr > reserved)
  490. reserved = pend_avail - paddr;
  491. size_avail = pend_avail - reserved - pstart_avail;
  492. omap_vram_add_region(paddr, size);
  493. if (reserved)
  494. pr_info("Reserving %lu bytes SRAM for VRAM\n", reserved);
  495. return reserved;
  496. }
  497. void __init omap_vram_set_sdram_vram(u32 size, u32 start)
  498. {
  499. omap_vram_sdram_start = start;
  500. omap_vram_sdram_size = size;
  501. }
  502. void __init omap_vram_set_sram_vram(u32 size, u32 start)
  503. {
  504. omap_vram_sram_start = start;
  505. omap_vram_sram_size = size;
  506. }