block: move down direct IO plugging
[linux/fpc-iii.git] / drivers / video / omap2 / vram.c
blob87e421e25afe0f5a1e61c4bd9affab339ffc7640
1 /*
2 * VRAM manager for OMAP
4 * Copyright (C) 2009 Nokia Corporation
5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
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.
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.
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.
21 /*#define DEBUG*/
23 #include <linux/kernel.h>
24 #include <linux/mm.h>
25 #include <linux/list.h>
26 #include <linux/slab.h>
27 #include <linux/seq_file.h>
28 #include <linux/memblock.h>
29 #include <linux/completion.h>
30 #include <linux/debugfs.h>
31 #include <linux/jiffies.h>
32 #include <linux/module.h>
34 #include <asm/setup.h>
36 #include <plat/vram.h>
37 #include <plat/dma.h>
39 #ifdef DEBUG
40 #define DBG(format, ...) pr_debug("VRAM: " format, ## __VA_ARGS__)
41 #else
42 #define DBG(format, ...)
43 #endif
45 /* postponed regions are used to temporarily store region information at boot
46 * time when we cannot yet allocate the region list */
47 #define MAX_POSTPONED_REGIONS 10
49 static bool vram_initialized;
50 static int postponed_cnt;
51 static struct {
52 unsigned long paddr;
53 size_t size;
54 } postponed_regions[MAX_POSTPONED_REGIONS];
56 struct vram_alloc {
57 struct list_head list;
58 unsigned long paddr;
59 unsigned pages;
62 struct vram_region {
63 struct list_head list;
64 struct list_head alloc_list;
65 unsigned long paddr;
66 unsigned pages;
69 static DEFINE_MUTEX(region_mutex);
70 static LIST_HEAD(region_list);
72 static struct vram_region *omap_vram_create_region(unsigned long paddr,
73 unsigned pages)
75 struct vram_region *rm;
77 rm = kzalloc(sizeof(*rm), GFP_KERNEL);
79 if (rm) {
80 INIT_LIST_HEAD(&rm->alloc_list);
81 rm->paddr = paddr;
82 rm->pages = pages;
85 return rm;
88 #if 0
89 static void omap_vram_free_region(struct vram_region *vr)
91 list_del(&vr->list);
92 kfree(vr);
94 #endif
96 static struct vram_alloc *omap_vram_create_allocation(struct vram_region *vr,
97 unsigned long paddr, unsigned pages)
99 struct vram_alloc *va;
100 struct vram_alloc *new;
102 new = kzalloc(sizeof(*va), GFP_KERNEL);
104 if (!new)
105 return NULL;
107 new->paddr = paddr;
108 new->pages = pages;
110 list_for_each_entry(va, &vr->alloc_list, list) {
111 if (va->paddr > new->paddr)
112 break;
115 list_add_tail(&new->list, &va->list);
117 return new;
120 static void omap_vram_free_allocation(struct vram_alloc *va)
122 list_del(&va->list);
123 kfree(va);
126 int omap_vram_add_region(unsigned long paddr, size_t size)
128 struct vram_region *rm;
129 unsigned pages;
131 if (vram_initialized) {
132 DBG("adding region paddr %08lx size %d\n",
133 paddr, size);
135 size &= PAGE_MASK;
136 pages = size >> PAGE_SHIFT;
138 rm = omap_vram_create_region(paddr, pages);
139 if (rm == NULL)
140 return -ENOMEM;
142 list_add(&rm->list, &region_list);
143 } else {
144 if (postponed_cnt == MAX_POSTPONED_REGIONS)
145 return -ENOMEM;
147 postponed_regions[postponed_cnt].paddr = paddr;
148 postponed_regions[postponed_cnt].size = size;
150 ++postponed_cnt;
152 return 0;
155 int omap_vram_free(unsigned long paddr, size_t size)
157 struct vram_region *rm;
158 struct vram_alloc *alloc;
159 unsigned start, end;
161 DBG("free mem paddr %08lx size %d\n", paddr, size);
163 size = PAGE_ALIGN(size);
165 mutex_lock(&region_mutex);
167 list_for_each_entry(rm, &region_list, list) {
168 list_for_each_entry(alloc, &rm->alloc_list, list) {
169 start = alloc->paddr;
170 end = alloc->paddr + (alloc->pages >> PAGE_SHIFT);
172 if (start >= paddr && end < paddr + size)
173 goto found;
177 mutex_unlock(&region_mutex);
178 return -EINVAL;
180 found:
181 omap_vram_free_allocation(alloc);
183 mutex_unlock(&region_mutex);
184 return 0;
186 EXPORT_SYMBOL(omap_vram_free);
188 static int _omap_vram_reserve(unsigned long paddr, unsigned pages)
190 struct vram_region *rm;
191 struct vram_alloc *alloc;
192 size_t size;
194 size = pages << PAGE_SHIFT;
196 list_for_each_entry(rm, &region_list, list) {
197 unsigned long start, end;
199 DBG("checking region %lx %d\n", rm->paddr, rm->pages);
201 start = rm->paddr;
202 end = start + (rm->pages << PAGE_SHIFT) - 1;
203 if (start > paddr || end < paddr + size - 1)
204 continue;
206 DBG("block ok, checking allocs\n");
208 list_for_each_entry(alloc, &rm->alloc_list, list) {
209 end = alloc->paddr - 1;
211 if (start <= paddr && end >= paddr + size - 1)
212 goto found;
214 start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
217 end = rm->paddr + (rm->pages << PAGE_SHIFT) - 1;
219 if (!(start <= paddr && end >= paddr + size - 1))
220 continue;
221 found:
222 DBG("found area start %lx, end %lx\n", start, end);
224 if (omap_vram_create_allocation(rm, paddr, pages) == NULL)
225 return -ENOMEM;
227 return 0;
230 return -ENOMEM;
233 int omap_vram_reserve(unsigned long paddr, size_t size)
235 unsigned pages;
236 int r;
238 DBG("reserve mem paddr %08lx size %d\n", paddr, size);
240 size = PAGE_ALIGN(size);
241 pages = size >> PAGE_SHIFT;
243 mutex_lock(&region_mutex);
245 r = _omap_vram_reserve(paddr, pages);
247 mutex_unlock(&region_mutex);
249 return r;
251 EXPORT_SYMBOL(omap_vram_reserve);
253 static void _omap_vram_dma_cb(int lch, u16 ch_status, void *data)
255 struct completion *compl = data;
256 complete(compl);
259 static int _omap_vram_clear(u32 paddr, unsigned pages)
261 struct completion compl;
262 unsigned elem_count;
263 unsigned frame_count;
264 int r;
265 int lch;
267 init_completion(&compl);
269 r = omap_request_dma(OMAP_DMA_NO_DEVICE, "VRAM DMA",
270 _omap_vram_dma_cb,
271 &compl, &lch);
272 if (r) {
273 pr_err("VRAM: request_dma failed for memory clear\n");
274 return -EBUSY;
277 elem_count = pages * PAGE_SIZE / 4;
278 frame_count = 1;
280 omap_set_dma_transfer_params(lch, OMAP_DMA_DATA_TYPE_S32,
281 elem_count, frame_count,
282 OMAP_DMA_SYNC_ELEMENT,
283 0, 0);
285 omap_set_dma_dest_params(lch, 0, OMAP_DMA_AMODE_POST_INC,
286 paddr, 0, 0);
288 omap_set_dma_color_mode(lch, OMAP_DMA_CONSTANT_FILL, 0x000000);
290 omap_start_dma(lch);
292 if (wait_for_completion_timeout(&compl, msecs_to_jiffies(1000)) == 0) {
293 omap_stop_dma(lch);
294 pr_err("VRAM: dma timeout while clearing memory\n");
295 r = -EIO;
296 goto err;
299 r = 0;
300 err:
301 omap_free_dma(lch);
303 return r;
306 static int _omap_vram_alloc(unsigned pages, unsigned long *paddr)
308 struct vram_region *rm;
309 struct vram_alloc *alloc;
311 list_for_each_entry(rm, &region_list, list) {
312 unsigned long start, end;
314 DBG("checking region %lx %d\n", rm->paddr, rm->pages);
316 start = rm->paddr;
318 list_for_each_entry(alloc, &rm->alloc_list, list) {
319 end = alloc->paddr;
321 if (end - start >= pages << PAGE_SHIFT)
322 goto found;
324 start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
327 end = rm->paddr + (rm->pages << PAGE_SHIFT);
328 found:
329 if (end - start < pages << PAGE_SHIFT)
330 continue;
332 DBG("found %lx, end %lx\n", start, end);
334 alloc = omap_vram_create_allocation(rm, start, pages);
335 if (alloc == NULL)
336 return -ENOMEM;
338 *paddr = start;
340 _omap_vram_clear(start, pages);
342 return 0;
345 return -ENOMEM;
348 int omap_vram_alloc(size_t size, unsigned long *paddr)
350 unsigned pages;
351 int r;
353 BUG_ON(!size);
355 DBG("alloc mem size %d\n", size);
357 size = PAGE_ALIGN(size);
358 pages = size >> PAGE_SHIFT;
360 mutex_lock(&region_mutex);
362 r = _omap_vram_alloc(pages, paddr);
364 mutex_unlock(&region_mutex);
366 return r;
368 EXPORT_SYMBOL(omap_vram_alloc);
370 void omap_vram_get_info(unsigned long *vram,
371 unsigned long *free_vram,
372 unsigned long *largest_free_block)
374 struct vram_region *vr;
375 struct vram_alloc *va;
377 *vram = 0;
378 *free_vram = 0;
379 *largest_free_block = 0;
381 mutex_lock(&region_mutex);
383 list_for_each_entry(vr, &region_list, list) {
384 unsigned free;
385 unsigned long pa;
387 pa = vr->paddr;
388 *vram += vr->pages << PAGE_SHIFT;
390 list_for_each_entry(va, &vr->alloc_list, list) {
391 free = va->paddr - pa;
392 *free_vram += free;
393 if (free > *largest_free_block)
394 *largest_free_block = free;
395 pa = va->paddr + (va->pages << PAGE_SHIFT);
398 free = vr->paddr + (vr->pages << PAGE_SHIFT) - pa;
399 *free_vram += free;
400 if (free > *largest_free_block)
401 *largest_free_block = free;
404 mutex_unlock(&region_mutex);
406 EXPORT_SYMBOL(omap_vram_get_info);
408 #if defined(CONFIG_DEBUG_FS)
409 static int vram_debug_show(struct seq_file *s, void *unused)
411 struct vram_region *vr;
412 struct vram_alloc *va;
413 unsigned size;
415 mutex_lock(&region_mutex);
417 list_for_each_entry(vr, &region_list, list) {
418 size = vr->pages << PAGE_SHIFT;
419 seq_printf(s, "%08lx-%08lx (%d bytes)\n",
420 vr->paddr, vr->paddr + size - 1,
421 size);
423 list_for_each_entry(va, &vr->alloc_list, list) {
424 size = va->pages << PAGE_SHIFT;
425 seq_printf(s, " %08lx-%08lx (%d bytes)\n",
426 va->paddr, va->paddr + size - 1,
427 size);
431 mutex_unlock(&region_mutex);
433 return 0;
436 static int vram_debug_open(struct inode *inode, struct file *file)
438 return single_open(file, vram_debug_show, inode->i_private);
441 static const struct file_operations vram_debug_fops = {
442 .open = vram_debug_open,
443 .read = seq_read,
444 .llseek = seq_lseek,
445 .release = single_release,
448 static int __init omap_vram_create_debugfs(void)
450 struct dentry *d;
452 d = debugfs_create_file("vram", S_IRUGO, NULL,
453 NULL, &vram_debug_fops);
454 if (IS_ERR(d))
455 return PTR_ERR(d);
457 return 0;
459 #endif
461 static __init int omap_vram_init(void)
463 int i;
465 vram_initialized = 1;
467 for (i = 0; i < postponed_cnt; i++)
468 omap_vram_add_region(postponed_regions[i].paddr,
469 postponed_regions[i].size);
471 #ifdef CONFIG_DEBUG_FS
472 if (omap_vram_create_debugfs())
473 pr_err("VRAM: Failed to create debugfs file\n");
474 #endif
476 return 0;
479 arch_initcall(omap_vram_init);
481 /* boottime vram alloc stuff */
483 /* set from board file */
484 static u32 omap_vram_sdram_start __initdata;
485 static u32 omap_vram_sdram_size __initdata;
487 /* set from kernel cmdline */
488 static u32 omap_vram_def_sdram_size __initdata;
489 static u32 omap_vram_def_sdram_start __initdata;
491 static int __init omap_vram_early_vram(char *p)
493 omap_vram_def_sdram_size = memparse(p, &p);
494 if (*p == ',')
495 omap_vram_def_sdram_start = simple_strtoul(p + 1, &p, 16);
496 return 0;
498 early_param("vram", omap_vram_early_vram);
501 * Called from map_io. We need to call to this early enough so that we
502 * can reserve the fixed SDRAM regions before VM could get hold of them.
504 void __init omap_vram_reserve_sdram_memblock(void)
506 u32 paddr;
507 u32 size = 0;
509 /* cmdline arg overrides the board file definition */
510 if (omap_vram_def_sdram_size) {
511 size = omap_vram_def_sdram_size;
512 paddr = omap_vram_def_sdram_start;
515 if (!size) {
516 size = omap_vram_sdram_size;
517 paddr = omap_vram_sdram_start;
520 #ifdef CONFIG_OMAP2_VRAM_SIZE
521 if (!size) {
522 size = CONFIG_OMAP2_VRAM_SIZE * 1024 * 1024;
523 paddr = 0;
525 #endif
527 if (!size)
528 return;
530 size = ALIGN(size, SZ_2M);
532 if (paddr) {
533 if (paddr & ~PAGE_MASK) {
534 pr_err("VRAM start address 0x%08x not page aligned\n",
535 paddr);
536 return;
539 if (!memblock_is_region_memory(paddr, size)) {
540 pr_err("Illegal SDRAM region 0x%08x..0x%08x for VRAM\n",
541 paddr, paddr + size - 1);
542 return;
545 if (memblock_is_region_reserved(paddr, size)) {
546 pr_err("FB: failed to reserve VRAM - busy\n");
547 return;
550 if (memblock_reserve(paddr, size) < 0) {
551 pr_err("FB: failed to reserve VRAM - no memory\n");
552 return;
554 } else {
555 paddr = memblock_alloc(size, SZ_2M);
558 memblock_free(paddr, size);
559 memblock_remove(paddr, size);
561 omap_vram_add_region(paddr, size);
563 pr_info("Reserving %u bytes SDRAM for VRAM\n", size);
566 void __init omap_vram_set_sdram_vram(u32 size, u32 start)
568 omap_vram_sdram_start = start;
569 omap_vram_sdram_size = size;