vram.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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/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. /* postponed regions are used to temporarily store region information at boot
  40. * time when we cannot yet allocate the region list */
  41. #define MAX_POSTPONED_REGIONS 10
  42. static bool vram_initialized;
  43. static int postponed_cnt;
  44. static struct {
  45. unsigned long paddr;
  46. size_t size;
  47. } postponed_regions[MAX_POSTPONED_REGIONS];
  48. struct vram_alloc {
  49. struct list_head list;
  50. unsigned long paddr;
  51. unsigned pages;
  52. };
  53. struct vram_region {
  54. struct list_head list;
  55. struct list_head alloc_list;
  56. unsigned long paddr;
  57. unsigned pages;
  58. };
  59. static DEFINE_MUTEX(region_mutex);
  60. static LIST_HEAD(region_list);
  61. static struct vram_region *omap_vram_create_region(unsigned long paddr,
  62. unsigned pages)
  63. {
  64. struct vram_region *rm;
  65. rm = kzalloc(sizeof(*rm), GFP_KERNEL);
  66. if (rm) {
  67. INIT_LIST_HEAD(&rm->alloc_list);
  68. rm->paddr = paddr;
  69. rm->pages = pages;
  70. }
  71. return rm;
  72. }
  73. #if 0
  74. static void omap_vram_free_region(struct vram_region *vr)
  75. {
  76. list_del(&vr->list);
  77. kfree(vr);
  78. }
  79. #endif
  80. static struct vram_alloc *omap_vram_create_allocation(struct vram_region *vr,
  81. unsigned long paddr, unsigned pages)
  82. {
  83. struct vram_alloc *va;
  84. struct vram_alloc *new;
  85. new = kzalloc(sizeof(*va), GFP_KERNEL);
  86. if (!new)
  87. return NULL;
  88. new->paddr = paddr;
  89. new->pages = pages;
  90. list_for_each_entry(va, &vr->alloc_list, list) {
  91. if (va->paddr > new->paddr)
  92. break;
  93. }
  94. list_add_tail(&new->list, &va->list);
  95. return new;
  96. }
  97. static void omap_vram_free_allocation(struct vram_alloc *va)
  98. {
  99. list_del(&va->list);
  100. kfree(va);
  101. }
  102. int omap_vram_add_region(unsigned long paddr, size_t size)
  103. {
  104. struct vram_region *rm;
  105. unsigned pages;
  106. if (vram_initialized) {
  107. DBG("adding region paddr %08lx size %d\n",
  108. paddr, size);
  109. size &= PAGE_MASK;
  110. pages = size >> PAGE_SHIFT;
  111. rm = omap_vram_create_region(paddr, pages);
  112. if (rm == NULL)
  113. return -ENOMEM;
  114. list_add(&rm->list, &region_list);
  115. } else {
  116. if (postponed_cnt == MAX_POSTPONED_REGIONS)
  117. return -ENOMEM;
  118. postponed_regions[postponed_cnt].paddr = paddr;
  119. postponed_regions[postponed_cnt].size = size;
  120. ++postponed_cnt;
  121. }
  122. return 0;
  123. }
  124. int omap_vram_free(unsigned long paddr, size_t size)
  125. {
  126. struct vram_region *rm;
  127. struct vram_alloc *alloc;
  128. unsigned start, end;
  129. DBG("free mem paddr %08lx size %d\n", paddr, size);
  130. size = PAGE_ALIGN(size);
  131. mutex_lock(&region_mutex);
  132. list_for_each_entry(rm, &region_list, list) {
  133. list_for_each_entry(alloc, &rm->alloc_list, list) {
  134. start = alloc->paddr;
  135. end = alloc->paddr + (alloc->pages >> PAGE_SHIFT);
  136. if (start >= paddr && end < paddr + size)
  137. goto found;
  138. }
  139. }
  140. mutex_unlock(&region_mutex);
  141. return -EINVAL;
  142. found:
  143. omap_vram_free_allocation(alloc);
  144. mutex_unlock(&region_mutex);
  145. return 0;
  146. }
  147. EXPORT_SYMBOL(omap_vram_free);
  148. static int _omap_vram_reserve(unsigned long paddr, unsigned pages)
  149. {
  150. struct vram_region *rm;
  151. struct vram_alloc *alloc;
  152. size_t size;
  153. size = pages << PAGE_SHIFT;
  154. list_for_each_entry(rm, &region_list, list) {
  155. unsigned long start, end;
  156. DBG("checking region %lx %d\n", rm->paddr, rm->pages);
  157. start = rm->paddr;
  158. end = start + (rm->pages << PAGE_SHIFT) - 1;
  159. if (start > paddr || end < paddr + size - 1)
  160. continue;
  161. DBG("block ok, checking allocs\n");
  162. list_for_each_entry(alloc, &rm->alloc_list, list) {
  163. end = alloc->paddr - 1;
  164. if (start <= paddr && end >= paddr + size - 1)
  165. goto found;
  166. start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
  167. }
  168. end = rm->paddr + (rm->pages << PAGE_SHIFT) - 1;
  169. if (!(start <= paddr && end >= paddr + size - 1))
  170. continue;
  171. found:
  172. DBG("found area start %lx, end %lx\n", start, end);
  173. if (omap_vram_create_allocation(rm, paddr, pages) == NULL)
  174. return -ENOMEM;
  175. return 0;
  176. }
  177. return -ENOMEM;
  178. }
  179. int omap_vram_reserve(unsigned long paddr, size_t size)
  180. {
  181. unsigned pages;
  182. int r;
  183. DBG("reserve mem paddr %08lx size %d\n", paddr, size);
  184. size = PAGE_ALIGN(size);
  185. pages = size >> PAGE_SHIFT;
  186. mutex_lock(&region_mutex);
  187. r = _omap_vram_reserve(paddr, pages);
  188. mutex_unlock(&region_mutex);
  189. return r;
  190. }
  191. EXPORT_SYMBOL(omap_vram_reserve);
  192. static void _omap_vram_dma_cb(int lch, u16 ch_status, void *data)
  193. {
  194. struct completion *compl = data;
  195. complete(compl);
  196. }
  197. static int _omap_vram_clear(u32 paddr, unsigned pages)
  198. {
  199. struct completion compl;
  200. unsigned elem_count;
  201. unsigned frame_count;
  202. int r;
  203. int lch;
  204. init_completion(&compl);
  205. r = omap_request_dma(OMAP_DMA_NO_DEVICE, "VRAM DMA",
  206. _omap_vram_dma_cb,
  207. &compl, &lch);
  208. if (r) {
  209. pr_err("VRAM: request_dma failed for memory clear\n");
  210. return -EBUSY;
  211. }
  212. elem_count = pages * PAGE_SIZE / 4;
  213. frame_count = 1;
  214. omap_set_dma_transfer_params(lch, OMAP_DMA_DATA_TYPE_S32,
  215. elem_count, frame_count,
  216. OMAP_DMA_SYNC_ELEMENT,
  217. 0, 0);
  218. omap_set_dma_dest_params(lch, 0, OMAP_DMA_AMODE_POST_INC,
  219. paddr, 0, 0);
  220. omap_set_dma_color_mode(lch, OMAP_DMA_CONSTANT_FILL, 0x000000);
  221. omap_start_dma(lch);
  222. if (wait_for_completion_timeout(&compl, msecs_to_jiffies(1000)) == 0) {
  223. omap_stop_dma(lch);
  224. pr_err("VRAM: dma timeout while clearing memory\n");
  225. r = -EIO;
  226. goto err;
  227. }
  228. r = 0;
  229. err:
  230. omap_free_dma(lch);
  231. return r;
  232. }
  233. static int _omap_vram_alloc(unsigned pages, unsigned long *paddr)
  234. {
  235. struct vram_region *rm;
  236. struct vram_alloc *alloc;
  237. list_for_each_entry(rm, &region_list, list) {
  238. unsigned long start, end;
  239. DBG("checking region %lx %d\n", rm->paddr, rm->pages);
  240. start = rm->paddr;
  241. list_for_each_entry(alloc, &rm->alloc_list, list) {
  242. end = alloc->paddr;
  243. if (end - start >= pages << PAGE_SHIFT)
  244. goto found;
  245. start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
  246. }
  247. end = rm->paddr + (rm->pages << PAGE_SHIFT);
  248. found:
  249. if (end - start < pages << PAGE_SHIFT)
  250. continue;
  251. DBG("found %lx, end %lx\n", start, end);
  252. alloc = omap_vram_create_allocation(rm, start, pages);
  253. if (alloc == NULL)
  254. return -ENOMEM;
  255. *paddr = start;
  256. _omap_vram_clear(start, pages);
  257. return 0;
  258. }
  259. return -ENOMEM;
  260. }
  261. int omap_vram_alloc(size_t size, unsigned long *paddr)
  262. {
  263. unsigned pages;
  264. int r;
  265. BUG_ON(!size);
  266. DBG("alloc mem size %d\n", size);
  267. size = PAGE_ALIGN(size);
  268. pages = size >> PAGE_SHIFT;
  269. mutex_lock(&region_mutex);
  270. r = _omap_vram_alloc(pages, paddr);
  271. mutex_unlock(&region_mutex);
  272. return r;
  273. }
  274. EXPORT_SYMBOL(omap_vram_alloc);
  275. void omap_vram_get_info(unsigned long *vram,
  276. unsigned long *free_vram,
  277. unsigned long *largest_free_block)
  278. {
  279. struct vram_region *vr;
  280. struct vram_alloc *va;
  281. *vram = 0;
  282. *free_vram = 0;
  283. *largest_free_block = 0;
  284. mutex_lock(&region_mutex);
  285. list_for_each_entry(vr, &region_list, list) {
  286. unsigned free;
  287. unsigned long pa;
  288. pa = vr->paddr;
  289. *vram += vr->pages << PAGE_SHIFT;
  290. list_for_each_entry(va, &vr->alloc_list, list) {
  291. free = va->paddr - pa;
  292. *free_vram += free;
  293. if (free > *largest_free_block)
  294. *largest_free_block = free;
  295. pa = va->paddr + (va->pages << PAGE_SHIFT);
  296. }
  297. free = vr->paddr + (vr->pages << PAGE_SHIFT) - pa;
  298. *free_vram += free;
  299. if (free > *largest_free_block)
  300. *largest_free_block = free;
  301. }
  302. mutex_unlock(&region_mutex);
  303. }
  304. EXPORT_SYMBOL(omap_vram_get_info);
  305. #if defined(CONFIG_DEBUG_FS)
  306. static int vram_debug_show(struct seq_file *s, void *unused)
  307. {
  308. struct vram_region *vr;
  309. struct vram_alloc *va;
  310. unsigned size;
  311. mutex_lock(&region_mutex);
  312. list_for_each_entry(vr, &region_list, list) {
  313. size = vr->pages << PAGE_SHIFT;
  314. seq_printf(s, "%08lx-%08lx (%d bytes)\n",
  315. vr->paddr, vr->paddr + size - 1,
  316. size);
  317. list_for_each_entry(va, &vr->alloc_list, list) {
  318. size = va->pages << PAGE_SHIFT;
  319. seq_printf(s, " %08lx-%08lx (%d bytes)\n",
  320. va->paddr, va->paddr + size - 1,
  321. size);
  322. }
  323. }
  324. mutex_unlock(&region_mutex);
  325. return 0;
  326. }
  327. static int vram_debug_open(struct inode *inode, struct file *file)
  328. {
  329. return single_open(file, vram_debug_show, inode->i_private);
  330. }
  331. static const struct file_operations vram_debug_fops = {
  332. .open = vram_debug_open,
  333. .read = seq_read,
  334. .llseek = seq_lseek,
  335. .release = single_release,
  336. };
  337. static int __init omap_vram_create_debugfs(void)
  338. {
  339. struct dentry *d;
  340. d = debugfs_create_file("vram", S_IRUGO, NULL,
  341. NULL, &vram_debug_fops);
  342. if (IS_ERR(d))
  343. return PTR_ERR(d);
  344. return 0;
  345. }
  346. #endif
  347. static __init int omap_vram_init(void)
  348. {
  349. int i;
  350. vram_initialized = 1;
  351. for (i = 0; i < postponed_cnt; i++)
  352. omap_vram_add_region(postponed_regions[i].paddr,
  353. postponed_regions[i].size);
  354. #ifdef CONFIG_DEBUG_FS
  355. if (omap_vram_create_debugfs())
  356. pr_err("VRAM: Failed to create debugfs file\n");
  357. #endif
  358. return 0;
  359. }
  360. arch_initcall(omap_vram_init);
  361. /* boottime vram alloc stuff */
  362. /* set from board file */
  363. static u32 omap_vram_sdram_start __initdata;
  364. static u32 omap_vram_sdram_size __initdata;
  365. /* set from kernel cmdline */
  366. static u32 omap_vram_def_sdram_size __initdata;
  367. static u32 omap_vram_def_sdram_start __initdata;
  368. static int __init omap_vram_early_vram(char *p)
  369. {
  370. omap_vram_def_sdram_size = memparse(p, &p);
  371. if (*p == ',')
  372. omap_vram_def_sdram_start = simple_strtoul(p + 1, &p, 16);
  373. return 0;
  374. }
  375. early_param("vram", omap_vram_early_vram);
  376. /*
  377. * Called from map_io. We need to call to this early enough so that we
  378. * can reserve the fixed SDRAM regions before VM could get hold of them.
  379. */
  380. void __init omap_vram_reserve_sdram_memblock(void)
  381. {
  382. u32 paddr;
  383. u32 size = 0;
  384. /* cmdline arg overrides the board file definition */
  385. if (omap_vram_def_sdram_size) {
  386. size = omap_vram_def_sdram_size;
  387. paddr = omap_vram_def_sdram_start;
  388. }
  389. if (!size) {
  390. size = omap_vram_sdram_size;
  391. paddr = omap_vram_sdram_start;
  392. }
  393. #ifdef CONFIG_OMAP2_VRAM_SIZE
  394. if (!size) {
  395. size = CONFIG_OMAP2_VRAM_SIZE * 1024 * 1024;
  396. paddr = 0;
  397. }
  398. #endif
  399. if (!size)
  400. return;
  401. size = ALIGN(size, SZ_2M);
  402. if (paddr) {
  403. if (paddr & ~PAGE_MASK) {
  404. pr_err("VRAM start address 0x%08x not page aligned\n",
  405. paddr);
  406. return;
  407. }
  408. if (!memblock_is_region_memory(paddr, size)) {
  409. pr_err("Illegal SDRAM region 0x%08x..0x%08x for VRAM\n",
  410. paddr, paddr + size - 1);
  411. return;
  412. }
  413. if (memblock_is_region_reserved(paddr, size)) {
  414. pr_err("FB: failed to reserve VRAM - busy\n");
  415. return;
  416. }
  417. if (memblock_reserve(paddr, size) < 0) {
  418. pr_err("FB: failed to reserve VRAM - no memory\n");
  419. return;
  420. }
  421. } else {
  422. paddr = memblock_alloc(size, SZ_2M);
  423. }
  424. memblock_free(paddr, size);
  425. memblock_remove(paddr, size);
  426. omap_vram_add_region(paddr, size);
  427. pr_info("Reserving %u bytes SDRAM for VRAM\n", size);
  428. }
  429. void __init omap_vram_set_sdram_vram(u32 size, u32 start)
  430. {
  431. omap_vram_sdram_start = start;
  432. omap_vram_sdram_size = size;
  433. }