mfd: Fix mismatch in twl4030 mutex lock-unlock
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / video / omap2 / vram.c
blob9441e2eb3dee1a1d66259a406d0653da323570e6
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;
102 #if 0
103 static void omap_vram_free_region(struct vram_region *vr)
105 list_del(&vr->list);
106 kfree(vr);
108 #endif
110 static struct vram_alloc *omap_vram_create_allocation(struct vram_region *vr,
111 unsigned long paddr, unsigned pages)
113 struct vram_alloc *va;
114 struct vram_alloc *new;
116 new = kzalloc(sizeof(*va), GFP_KERNEL);
118 if (!new)
119 return NULL;
121 new->paddr = paddr;
122 new->pages = pages;
124 list_for_each_entry(va, &vr->alloc_list, list) {
125 if (va->paddr > new->paddr)
126 break;
129 list_add_tail(&new->list, &va->list);
131 return new;
134 static void omap_vram_free_allocation(struct vram_alloc *va)
136 list_del(&va->list);
137 kfree(va);
140 int omap_vram_add_region(unsigned long paddr, size_t size)
142 struct vram_region *rm;
143 unsigned pages;
145 if (vram_initialized) {
146 DBG("adding region paddr %08lx size %d\n",
147 paddr, size);
149 size &= PAGE_MASK;
150 pages = size >> PAGE_SHIFT;
152 rm = omap_vram_create_region(paddr, pages);
153 if (rm == NULL)
154 return -ENOMEM;
156 list_add(&rm->list, &region_list);
157 } else {
158 if (postponed_cnt == MAX_POSTPONED_REGIONS)
159 return -ENOMEM;
161 postponed_regions[postponed_cnt].paddr = paddr;
162 postponed_regions[postponed_cnt].size = size;
164 ++postponed_cnt;
166 return 0;
169 int omap_vram_free(unsigned long paddr, size_t size)
171 struct vram_region *rm;
172 struct vram_alloc *alloc;
173 unsigned start, end;
175 DBG("free mem paddr %08lx size %d\n", paddr, size);
177 size = PAGE_ALIGN(size);
179 mutex_lock(&region_mutex);
181 list_for_each_entry(rm, &region_list, list) {
182 list_for_each_entry(alloc, &rm->alloc_list, list) {
183 start = alloc->paddr;
184 end = alloc->paddr + (alloc->pages >> PAGE_SHIFT);
186 if (start >= paddr && end < paddr + size)
187 goto found;
191 mutex_unlock(&region_mutex);
192 return -EINVAL;
194 found:
195 omap_vram_free_allocation(alloc);
197 mutex_unlock(&region_mutex);
198 return 0;
200 EXPORT_SYMBOL(omap_vram_free);
202 static int _omap_vram_reserve(unsigned long paddr, unsigned pages)
204 struct vram_region *rm;
205 struct vram_alloc *alloc;
206 size_t size;
208 size = pages << PAGE_SHIFT;
210 list_for_each_entry(rm, &region_list, list) {
211 unsigned long start, end;
213 DBG("checking region %lx %d\n", rm->paddr, rm->pages);
215 if (region_mem_type(rm->paddr) != region_mem_type(paddr))
216 continue;
218 start = rm->paddr;
219 end = start + (rm->pages << PAGE_SHIFT) - 1;
220 if (start > paddr || end < paddr + size - 1)
221 continue;
223 DBG("block ok, checking allocs\n");
225 list_for_each_entry(alloc, &rm->alloc_list, list) {
226 end = alloc->paddr - 1;
228 if (start <= paddr && end >= paddr + size - 1)
229 goto found;
231 start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
234 end = rm->paddr + (rm->pages << PAGE_SHIFT) - 1;
236 if (!(start <= paddr && end >= paddr + size - 1))
237 continue;
238 found:
239 DBG("found area start %lx, end %lx\n", start, end);
241 if (omap_vram_create_allocation(rm, paddr, pages) == NULL)
242 return -ENOMEM;
244 return 0;
247 return -ENOMEM;
250 int omap_vram_reserve(unsigned long paddr, size_t size)
252 unsigned pages;
253 int r;
255 DBG("reserve mem paddr %08lx size %d\n", paddr, size);
257 size = PAGE_ALIGN(size);
258 pages = size >> PAGE_SHIFT;
260 mutex_lock(&region_mutex);
262 r = _omap_vram_reserve(paddr, pages);
264 mutex_unlock(&region_mutex);
266 return r;
268 EXPORT_SYMBOL(omap_vram_reserve);
270 static void _omap_vram_dma_cb(int lch, u16 ch_status, void *data)
272 struct completion *compl = data;
273 complete(compl);
276 static int _omap_vram_clear(u32 paddr, unsigned pages)
278 struct completion compl;
279 unsigned elem_count;
280 unsigned frame_count;
281 int r;
282 int lch;
284 init_completion(&compl);
286 r = omap_request_dma(OMAP_DMA_NO_DEVICE, "VRAM DMA",
287 _omap_vram_dma_cb,
288 &compl, &lch);
289 if (r) {
290 pr_err("VRAM: request_dma failed for memory clear\n");
291 return -EBUSY;
294 elem_count = pages * PAGE_SIZE / 4;
295 frame_count = 1;
297 omap_set_dma_transfer_params(lch, OMAP_DMA_DATA_TYPE_S32,
298 elem_count, frame_count,
299 OMAP_DMA_SYNC_ELEMENT,
300 0, 0);
302 omap_set_dma_dest_params(lch, 0, OMAP_DMA_AMODE_POST_INC,
303 paddr, 0, 0);
305 omap_set_dma_color_mode(lch, OMAP_DMA_CONSTANT_FILL, 0x000000);
307 omap_start_dma(lch);
309 if (wait_for_completion_timeout(&compl, msecs_to_jiffies(1000)) == 0) {
310 omap_stop_dma(lch);
311 pr_err("VRAM: dma timeout while clearing memory\n");
312 r = -EIO;
313 goto err;
316 r = 0;
317 err:
318 omap_free_dma(lch);
320 return r;
323 static int _omap_vram_alloc(int mtype, unsigned pages, unsigned long *paddr)
325 struct vram_region *rm;
326 struct vram_alloc *alloc;
328 list_for_each_entry(rm, &region_list, list) {
329 unsigned long start, end;
331 DBG("checking region %lx %d\n", rm->paddr, rm->pages);
333 if (region_mem_type(rm->paddr) != mtype)
334 continue;
336 start = rm->paddr;
338 list_for_each_entry(alloc, &rm->alloc_list, list) {
339 end = alloc->paddr;
341 if (end - start >= pages << PAGE_SHIFT)
342 goto found;
344 start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
347 end = rm->paddr + (rm->pages << PAGE_SHIFT);
348 found:
349 if (end - start < pages << PAGE_SHIFT)
350 continue;
352 DBG("found %lx, end %lx\n", start, end);
354 alloc = omap_vram_create_allocation(rm, start, pages);
355 if (alloc == NULL)
356 return -ENOMEM;
358 *paddr = start;
360 _omap_vram_clear(start, pages);
362 return 0;
365 return -ENOMEM;
368 int omap_vram_alloc(int mtype, size_t size, unsigned long *paddr)
370 unsigned pages;
371 int r;
373 BUG_ON(mtype > OMAP_VRAM_MEMTYPE_MAX || !size);
375 DBG("alloc mem type %d size %d\n", mtype, size);
377 size = PAGE_ALIGN(size);
378 pages = size >> PAGE_SHIFT;
380 mutex_lock(&region_mutex);
382 r = _omap_vram_alloc(mtype, pages, paddr);
384 mutex_unlock(&region_mutex);
386 return r;
388 EXPORT_SYMBOL(omap_vram_alloc);
390 void omap_vram_get_info(unsigned long *vram,
391 unsigned long *free_vram,
392 unsigned long *largest_free_block)
394 struct vram_region *vr;
395 struct vram_alloc *va;
397 *vram = 0;
398 *free_vram = 0;
399 *largest_free_block = 0;
401 mutex_lock(&region_mutex);
403 list_for_each_entry(vr, &region_list, list) {
404 unsigned free;
405 unsigned long pa;
407 pa = vr->paddr;
408 *vram += vr->pages << PAGE_SHIFT;
410 list_for_each_entry(va, &vr->alloc_list, list) {
411 free = va->paddr - pa;
412 *free_vram += free;
413 if (free > *largest_free_block)
414 *largest_free_block = free;
415 pa = va->paddr + (va->pages << PAGE_SHIFT);
418 free = vr->paddr + (vr->pages << PAGE_SHIFT) - pa;
419 *free_vram += free;
420 if (free > *largest_free_block)
421 *largest_free_block = free;
424 mutex_unlock(&region_mutex);
426 EXPORT_SYMBOL(omap_vram_get_info);
428 #if defined(CONFIG_DEBUG_FS)
429 static int vram_debug_show(struct seq_file *s, void *unused)
431 struct vram_region *vr;
432 struct vram_alloc *va;
433 unsigned size;
435 mutex_lock(&region_mutex);
437 list_for_each_entry(vr, &region_list, list) {
438 size = vr->pages << PAGE_SHIFT;
439 seq_printf(s, "%08lx-%08lx (%d bytes)\n",
440 vr->paddr, vr->paddr + size - 1,
441 size);
443 list_for_each_entry(va, &vr->alloc_list, list) {
444 size = va->pages << PAGE_SHIFT;
445 seq_printf(s, " %08lx-%08lx (%d bytes)\n",
446 va->paddr, va->paddr + size - 1,
447 size);
451 mutex_unlock(&region_mutex);
453 return 0;
456 static int vram_debug_open(struct inode *inode, struct file *file)
458 return single_open(file, vram_debug_show, inode->i_private);
461 static const struct file_operations vram_debug_fops = {
462 .open = vram_debug_open,
463 .read = seq_read,
464 .llseek = seq_lseek,
465 .release = single_release,
468 static int __init omap_vram_create_debugfs(void)
470 struct dentry *d;
472 d = debugfs_create_file("vram", S_IRUGO, NULL,
473 NULL, &vram_debug_fops);
474 if (IS_ERR(d))
475 return PTR_ERR(d);
477 return 0;
479 #endif
481 static __init int omap_vram_init(void)
483 int i;
485 vram_initialized = 1;
487 for (i = 0; i < postponed_cnt; i++)
488 omap_vram_add_region(postponed_regions[i].paddr,
489 postponed_regions[i].size);
491 #ifdef CONFIG_DEBUG_FS
492 if (omap_vram_create_debugfs())
493 pr_err("VRAM: Failed to create debugfs file\n");
494 #endif
496 return 0;
499 arch_initcall(omap_vram_init);
501 /* boottime vram alloc stuff */
503 /* set from board file */
504 static u32 omap_vram_sram_start __initdata;
505 static u32 omap_vram_sram_size __initdata;
507 /* set from board file */
508 static u32 omap_vram_sdram_start __initdata;
509 static u32 omap_vram_sdram_size __initdata;
511 /* set from kernel cmdline */
512 static u32 omap_vram_def_sdram_size __initdata;
513 static u32 omap_vram_def_sdram_start __initdata;
515 static int __init omap_vram_early_vram(char *p)
517 omap_vram_def_sdram_size = memparse(p, &p);
518 if (*p == ',')
519 omap_vram_def_sdram_start = simple_strtoul(p + 1, &p, 16);
520 return 0;
522 early_param("vram", omap_vram_early_vram);
525 * Called from map_io. We need to call to this early enough so that we
526 * can reserve the fixed SDRAM regions before VM could get hold of them.
528 void __init omap_vram_reserve_sdram_memblock(void)
530 u32 paddr;
531 u32 size = 0;
533 /* cmdline arg overrides the board file definition */
534 if (omap_vram_def_sdram_size) {
535 size = omap_vram_def_sdram_size;
536 paddr = omap_vram_def_sdram_start;
539 if (!size) {
540 size = omap_vram_sdram_size;
541 paddr = omap_vram_sdram_start;
544 #ifdef CONFIG_OMAP2_VRAM_SIZE
545 if (!size) {
546 size = CONFIG_OMAP2_VRAM_SIZE * 1024 * 1024;
547 paddr = 0;
549 #endif
551 if (!size)
552 return;
554 size = ALIGN(size, SZ_2M);
556 if (paddr) {
557 if (paddr & ~PAGE_MASK) {
558 pr_err("VRAM start address 0x%08x not page aligned\n",
559 paddr);
560 return;
563 if (!memblock_is_region_memory(paddr, size)) {
564 pr_err("Illegal SDRAM region 0x%08x..0x%08x for VRAM\n",
565 paddr, paddr + size - 1);
566 return;
569 if (memblock_is_region_reserved(paddr, size)) {
570 pr_err("FB: failed to reserve VRAM - busy\n");
571 return;
574 if (memblock_reserve(paddr, size) < 0) {
575 pr_err("FB: failed to reserve VRAM - no memory\n");
576 return;
578 } else {
579 paddr = memblock_alloc(size, SZ_2M);
582 memblock_free(paddr, size);
583 memblock_remove(paddr, size);
585 omap_vram_add_region(paddr, size);
587 pr_info("Reserving %u bytes SDRAM for VRAM\n", size);
591 * Called at sram init time, before anything is pushed to the SRAM stack.
592 * Because of the stack scheme, we will allocate everything from the
593 * start of the lowest address region to the end of SRAM. This will also
594 * include padding for page alignment and possible holes between regions.
596 * As opposed to the SDRAM case, we'll also do any dynamic allocations at
597 * this point, since the driver built as a module would have problem with
598 * freeing / reallocating the regions.
600 unsigned long __init omap_vram_reserve_sram(unsigned long sram_pstart,
601 unsigned long sram_vstart,
602 unsigned long sram_size,
603 unsigned long pstart_avail,
604 unsigned long size_avail)
606 unsigned long pend_avail;
607 unsigned long reserved;
608 u32 paddr;
609 u32 size;
611 paddr = omap_vram_sram_start;
612 size = omap_vram_sram_size;
614 if (!size)
615 return 0;
617 reserved = 0;
618 pend_avail = pstart_avail + size_avail;
620 if (!paddr) {
621 /* Dynamic allocation */
622 if ((size_avail & PAGE_MASK) < size) {
623 pr_err("Not enough SRAM for VRAM\n");
624 return 0;
626 size_avail = (size_avail - size) & PAGE_MASK;
627 paddr = pstart_avail + size_avail;
630 if (paddr < sram_pstart ||
631 paddr + size > sram_pstart + sram_size) {
632 pr_err("Illegal SRAM region for VRAM\n");
633 return 0;
636 /* Reserve everything above the start of the region. */
637 if (pend_avail - paddr > reserved)
638 reserved = pend_avail - paddr;
639 size_avail = pend_avail - reserved - pstart_avail;
641 omap_vram_add_region(paddr, size);
643 if (reserved)
644 pr_info("Reserving %lu bytes SRAM for VRAM\n", reserved);
646 return reserved;
649 void __init omap_vram_set_sdram_vram(u32 size, u32 start)
651 omap_vram_sdram_start = start;
652 omap_vram_sdram_size = size;
655 void __init omap_vram_set_sram_vram(u32 size, u32 start)
657 omap_vram_sram_start = start;
658 omap_vram_sram_size = size;