GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / video / omap2 / vram.c
bloba7aa80e2c748809b236c940bea0bc8084b7486a1
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/sram.h>
37 #include <plat/vram.h>
38 #include <plat/dma.h>
40 #ifdef DEBUG
41 #define DBG(format, ...) pr_debug("VRAM: " format, ## __VA_ARGS__)
42 #else
43 #define DBG(format, ...)
44 #endif
46 #define OMAP2_SRAM_START 0x40200000
47 /* Maximum size, in reality this is smaller if SRAM is partially locked. */
48 #define OMAP2_SRAM_SIZE 0xa0000 /* 640k */
50 /* postponed regions are used to temporarily store region information at boot
51 * time when we cannot yet allocate the region list */
52 #define MAX_POSTPONED_REGIONS 10
54 static bool vram_initialized;
55 static int postponed_cnt;
56 static struct {
57 unsigned long paddr;
58 size_t size;
59 } postponed_regions[MAX_POSTPONED_REGIONS];
61 struct vram_alloc {
62 struct list_head list;
63 unsigned long paddr;
64 unsigned pages;
67 struct vram_region {
68 struct list_head list;
69 struct list_head alloc_list;
70 unsigned long paddr;
71 unsigned pages;
74 static DEFINE_MUTEX(region_mutex);
75 static LIST_HEAD(region_list);
77 static inline int region_mem_type(unsigned long paddr)
79 if (paddr >= OMAP2_SRAM_START &&
80 paddr < OMAP2_SRAM_START + OMAP2_SRAM_SIZE)
81 return OMAP_VRAM_MEMTYPE_SRAM;
82 else
83 return OMAP_VRAM_MEMTYPE_SDRAM;
86 static struct vram_region *omap_vram_create_region(unsigned long paddr,
87 unsigned pages)
89 struct vram_region *rm;
91 rm = kzalloc(sizeof(*rm), GFP_KERNEL);
93 if (rm) {
94 INIT_LIST_HEAD(&rm->alloc_list);
95 rm->paddr = paddr;
96 rm->pages = pages;
99 return rm;
103 static struct vram_alloc *omap_vram_create_allocation(struct vram_region *vr,
104 unsigned long paddr, unsigned pages)
106 struct vram_alloc *va;
107 struct vram_alloc *new;
109 new = kzalloc(sizeof(*va), GFP_KERNEL);
111 if (!new)
112 return NULL;
114 new->paddr = paddr;
115 new->pages = pages;
117 list_for_each_entry(va, &vr->alloc_list, list) {
118 if (va->paddr > new->paddr)
119 break;
122 list_add_tail(&new->list, &va->list);
124 return new;
127 static void omap_vram_free_allocation(struct vram_alloc *va)
129 list_del(&va->list);
130 kfree(va);
133 int omap_vram_add_region(unsigned long paddr, size_t size)
135 struct vram_region *rm;
136 unsigned pages;
138 if (vram_initialized) {
139 DBG("adding region paddr %08lx size %d\n",
140 paddr, size);
142 size &= PAGE_MASK;
143 pages = size >> PAGE_SHIFT;
145 rm = omap_vram_create_region(paddr, pages);
146 if (rm == NULL)
147 return -ENOMEM;
149 list_add(&rm->list, &region_list);
150 } else {
151 if (postponed_cnt == MAX_POSTPONED_REGIONS)
152 return -ENOMEM;
154 postponed_regions[postponed_cnt].paddr = paddr;
155 postponed_regions[postponed_cnt].size = size;
157 ++postponed_cnt;
159 return 0;
162 int omap_vram_free(unsigned long paddr, size_t size)
164 struct vram_region *rm;
165 struct vram_alloc *alloc;
166 unsigned start, end;
168 DBG("free mem paddr %08lx size %d\n", paddr, size);
170 size = PAGE_ALIGN(size);
172 mutex_lock(&region_mutex);
174 list_for_each_entry(rm, &region_list, list) {
175 list_for_each_entry(alloc, &rm->alloc_list, list) {
176 start = alloc->paddr;
177 end = alloc->paddr + (alloc->pages >> PAGE_SHIFT);
179 if (start >= paddr && end < paddr + size)
180 goto found;
184 mutex_unlock(&region_mutex);
185 return -EINVAL;
187 found:
188 omap_vram_free_allocation(alloc);
190 mutex_unlock(&region_mutex);
191 return 0;
193 EXPORT_SYMBOL(omap_vram_free);
195 static int _omap_vram_reserve(unsigned long paddr, unsigned pages)
197 struct vram_region *rm;
198 struct vram_alloc *alloc;
199 size_t size;
201 size = pages << PAGE_SHIFT;
203 list_for_each_entry(rm, &region_list, list) {
204 unsigned long start, end;
206 DBG("checking region %lx %d\n", rm->paddr, rm->pages);
208 if (region_mem_type(rm->paddr) != region_mem_type(paddr))
209 continue;
211 start = rm->paddr;
212 end = start + (rm->pages << PAGE_SHIFT) - 1;
213 if (start > paddr || end < paddr + size - 1)
214 continue;
216 DBG("block ok, checking allocs\n");
218 list_for_each_entry(alloc, &rm->alloc_list, list) {
219 end = alloc->paddr - 1;
221 if (start <= paddr && end >= paddr + size - 1)
222 goto found;
224 start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
227 end = rm->paddr + (rm->pages << PAGE_SHIFT) - 1;
229 if (!(start <= paddr && end >= paddr + size - 1))
230 continue;
231 found:
232 DBG("found area start %lx, end %lx\n", start, end);
234 if (omap_vram_create_allocation(rm, paddr, pages) == NULL)
235 return -ENOMEM;
237 return 0;
240 return -ENOMEM;
243 int omap_vram_reserve(unsigned long paddr, size_t size)
245 unsigned pages;
246 int r;
248 DBG("reserve mem paddr %08lx size %d\n", paddr, size);
250 size = PAGE_ALIGN(size);
251 pages = size >> PAGE_SHIFT;
253 mutex_lock(&region_mutex);
255 r = _omap_vram_reserve(paddr, pages);
257 mutex_unlock(&region_mutex);
259 return r;
261 EXPORT_SYMBOL(omap_vram_reserve);
263 static void _omap_vram_dma_cb(int lch, u16 ch_status, void *data)
265 struct completion *compl = data;
266 complete(compl);
269 static int _omap_vram_clear(u32 paddr, unsigned pages)
271 struct completion compl;
272 unsigned elem_count;
273 unsigned frame_count;
274 int r;
275 int lch;
277 init_completion(&compl);
279 r = omap_request_dma(OMAP_DMA_NO_DEVICE, "VRAM DMA",
280 _omap_vram_dma_cb,
281 &compl, &lch);
282 if (r) {
283 pr_err("VRAM: request_dma failed for memory clear\n");
284 return -EBUSY;
287 elem_count = pages * PAGE_SIZE / 4;
288 frame_count = 1;
290 omap_set_dma_transfer_params(lch, OMAP_DMA_DATA_TYPE_S32,
291 elem_count, frame_count,
292 OMAP_DMA_SYNC_ELEMENT,
293 0, 0);
295 omap_set_dma_dest_params(lch, 0, OMAP_DMA_AMODE_POST_INC,
296 paddr, 0, 0);
298 omap_set_dma_color_mode(lch, OMAP_DMA_CONSTANT_FILL, 0x000000);
300 omap_start_dma(lch);
302 if (wait_for_completion_timeout(&compl, msecs_to_jiffies(1000)) == 0) {
303 omap_stop_dma(lch);
304 pr_err("VRAM: dma timeout while clearing memory\n");
305 r = -EIO;
306 goto err;
309 r = 0;
310 err:
311 omap_free_dma(lch);
313 return r;
316 static int _omap_vram_alloc(int mtype, unsigned pages, unsigned long *paddr)
318 struct vram_region *rm;
319 struct vram_alloc *alloc;
321 list_for_each_entry(rm, &region_list, list) {
322 unsigned long start, end;
324 DBG("checking region %lx %d\n", rm->paddr, rm->pages);
326 if (region_mem_type(rm->paddr) != mtype)
327 continue;
329 start = rm->paddr;
331 list_for_each_entry(alloc, &rm->alloc_list, list) {
332 end = alloc->paddr;
334 if (end - start >= pages << PAGE_SHIFT)
335 goto found;
337 start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
340 end = rm->paddr + (rm->pages << PAGE_SHIFT);
341 found:
342 if (end - start < pages << PAGE_SHIFT)
343 continue;
345 DBG("found %lx, end %lx\n", start, end);
347 alloc = omap_vram_create_allocation(rm, start, pages);
348 if (alloc == NULL)
349 return -ENOMEM;
351 *paddr = start;
353 _omap_vram_clear(start, pages);
355 return 0;
358 return -ENOMEM;
361 int omap_vram_alloc(int mtype, size_t size, unsigned long *paddr)
363 unsigned pages;
364 int r;
366 BUG_ON(mtype > OMAP_VRAM_MEMTYPE_MAX || !size);
368 DBG("alloc mem type %d size %d\n", mtype, size);
370 size = PAGE_ALIGN(size);
371 pages = size >> PAGE_SHIFT;
373 mutex_lock(&region_mutex);
375 r = _omap_vram_alloc(mtype, pages, paddr);
377 mutex_unlock(&region_mutex);
379 return r;
381 EXPORT_SYMBOL(omap_vram_alloc);
383 void omap_vram_get_info(unsigned long *vram,
384 unsigned long *free_vram,
385 unsigned long *largest_free_block)
387 struct vram_region *vr;
388 struct vram_alloc *va;
390 *vram = 0;
391 *free_vram = 0;
392 *largest_free_block = 0;
394 mutex_lock(&region_mutex);
396 list_for_each_entry(vr, &region_list, list) {
397 unsigned free;
398 unsigned long pa;
400 pa = vr->paddr;
401 *vram += vr->pages << PAGE_SHIFT;
403 list_for_each_entry(va, &vr->alloc_list, list) {
404 free = va->paddr - pa;
405 *free_vram += free;
406 if (free > *largest_free_block)
407 *largest_free_block = free;
408 pa = va->paddr + (va->pages << PAGE_SHIFT);
411 free = vr->paddr + (vr->pages << PAGE_SHIFT) - pa;
412 *free_vram += free;
413 if (free > *largest_free_block)
414 *largest_free_block = free;
417 mutex_unlock(&region_mutex);
419 EXPORT_SYMBOL(omap_vram_get_info);
421 #if defined(CONFIG_DEBUG_FS)
422 static int vram_debug_show(struct seq_file *s, void *unused)
424 struct vram_region *vr;
425 struct vram_alloc *va;
426 unsigned size;
428 mutex_lock(&region_mutex);
430 list_for_each_entry(vr, &region_list, list) {
431 size = vr->pages << PAGE_SHIFT;
432 seq_printf(s, "%08lx-%08lx (%d bytes)\n",
433 vr->paddr, vr->paddr + size - 1,
434 size);
436 list_for_each_entry(va, &vr->alloc_list, list) {
437 size = va->pages << PAGE_SHIFT;
438 seq_printf(s, " %08lx-%08lx (%d bytes)\n",
439 va->paddr, va->paddr + size - 1,
440 size);
444 mutex_unlock(&region_mutex);
446 return 0;
449 static int vram_debug_open(struct inode *inode, struct file *file)
451 return single_open(file, vram_debug_show, inode->i_private);
454 static const struct file_operations vram_debug_fops = {
455 .open = vram_debug_open,
456 .read = seq_read,
457 .llseek = seq_lseek,
458 .release = single_release,
461 static int __init omap_vram_create_debugfs(void)
463 struct dentry *d;
465 d = debugfs_create_file("vram", S_IRUGO, NULL,
466 NULL, &vram_debug_fops);
467 if (IS_ERR(d))
468 return PTR_ERR(d);
470 return 0;
472 #endif
474 static __init int omap_vram_init(void)
476 int i;
478 vram_initialized = 1;
480 for (i = 0; i < postponed_cnt; i++)
481 omap_vram_add_region(postponed_regions[i].paddr,
482 postponed_regions[i].size);
484 #ifdef CONFIG_DEBUG_FS
485 if (omap_vram_create_debugfs())
486 pr_err("VRAM: Failed to create debugfs file\n");
487 #endif
489 return 0;
492 arch_initcall(omap_vram_init);
494 /* boottime vram alloc stuff */
496 /* set from board file */
497 static u32 omap_vram_sram_start __initdata;
498 static u32 omap_vram_sram_size __initdata;
500 /* set from board file */
501 static u32 omap_vram_sdram_start __initdata;
502 static u32 omap_vram_sdram_size __initdata;
504 /* set from kernel cmdline */
505 static u32 omap_vram_def_sdram_size __initdata;
506 static u32 omap_vram_def_sdram_start __initdata;
508 static int __init omap_vram_early_vram(char *p)
510 omap_vram_def_sdram_size = memparse(p, &p);
511 if (*p == ',')
512 omap_vram_def_sdram_start = simple_strtoul(p + 1, &p, 16);
513 return 0;
515 early_param("vram", omap_vram_early_vram);
518 * Called from map_io. We need to call to this early enough so that we
519 * can reserve the fixed SDRAM regions before VM could get hold of them.
521 void __init omap_vram_reserve_sdram_memblock(void)
523 u32 paddr;
524 u32 size = 0;
526 /* cmdline arg overrides the board file definition */
527 if (omap_vram_def_sdram_size) {
528 size = omap_vram_def_sdram_size;
529 paddr = omap_vram_def_sdram_start;
532 if (!size) {
533 size = omap_vram_sdram_size;
534 paddr = omap_vram_sdram_start;
537 #ifdef CONFIG_OMAP2_VRAM_SIZE
538 if (!size) {
539 size = CONFIG_OMAP2_VRAM_SIZE * 1024 * 1024;
540 paddr = 0;
542 #endif
544 if (!size)
545 return;
547 size = PAGE_ALIGN(size);
549 if (paddr) {
550 struct memblock_property res;
552 res.base = paddr;
553 res.size = size;
554 if ((paddr & ~PAGE_MASK) || memblock_find(&res) ||
555 res.base != paddr || res.size != size) {
556 pr_err("Illegal SDRAM region for VRAM\n");
557 return;
560 if (memblock_is_region_reserved(paddr, size)) {
561 pr_err("FB: failed to reserve VRAM - busy\n");
562 return;
565 if (memblock_reserve(paddr, size) < 0) {
566 pr_err("FB: failed to reserve VRAM - no memory\n");
567 return;
569 } else {
570 paddr = memblock_alloc_base(size, PAGE_SIZE, MEMBLOCK_REAL_LIMIT);
573 omap_vram_add_region(paddr, size);
575 pr_info("Reserving %u bytes SDRAM for VRAM\n", size);
579 * Called at sram init time, before anything is pushed to the SRAM stack.
580 * Because of the stack scheme, we will allocate everything from the
581 * start of the lowest address region to the end of SRAM. This will also
582 * include padding for page alignment and possible holes between regions.
584 * As opposed to the SDRAM case, we'll also do any dynamic allocations at
585 * this point, since the driver built as a module would have problem with
586 * freeing / reallocating the regions.
588 unsigned long __init omap_vram_reserve_sram(unsigned long sram_pstart,
589 unsigned long sram_vstart,
590 unsigned long sram_size,
591 unsigned long pstart_avail,
592 unsigned long size_avail)
594 unsigned long pend_avail;
595 unsigned long reserved;
596 u32 paddr;
597 u32 size;
599 paddr = omap_vram_sram_start;
600 size = omap_vram_sram_size;
602 if (!size)
603 return 0;
605 reserved = 0;
606 pend_avail = pstart_avail + size_avail;
608 if (!paddr) {
609 /* Dynamic allocation */
610 if ((size_avail & PAGE_MASK) < size) {
611 pr_err("Not enough SRAM for VRAM\n");
612 return 0;
614 size_avail = (size_avail - size) & PAGE_MASK;
615 paddr = pstart_avail + size_avail;
618 if (paddr < sram_pstart ||
619 paddr + size > sram_pstart + sram_size) {
620 pr_err("Illegal SRAM region for VRAM\n");
621 return 0;
624 /* Reserve everything above the start of the region. */
625 if (pend_avail - paddr > reserved)
626 reserved = pend_avail - paddr;
627 size_avail = pend_avail - reserved - pstart_avail;
629 omap_vram_add_region(paddr, size);
631 if (reserved)
632 pr_info("Reserving %lu bytes SRAM for VRAM\n", reserved);
634 return reserved;
637 void __init omap_vram_set_sdram_vram(u32 size, u32 start)
639 omap_vram_sdram_start = start;
640 omap_vram_sdram_size = size;
643 void __init omap_vram_set_sram_vram(u32 size, u32 start)
645 omap_vram_sram_start = start;
646 omap_vram_sram_size = size;