vram.c 14 KB

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