Linux 2.6.34-rc3
[pohmelfs.git] / drivers / video / omap2 / vram.c
blobb266ffae0bdea5f23d4d8ca4fa007ed9166f605e
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/seq_file.h>
27 #include <linux/bootmem.h>
28 #include <linux/completion.h>
29 #include <linux/debugfs.h>
30 #include <linux/jiffies.h>
31 #include <linux/module.h>
33 #include <asm/setup.h>
35 #include <plat/sram.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 #define OMAP2_SRAM_START 0x40200000
46 /* Maximum size, in reality this is smaller if SRAM is partially locked. */
47 #define OMAP2_SRAM_SIZE 0xa0000 /* 640k */
49 /* postponed regions are used to temporarily store region information at boot
50 * time when we cannot yet allocate the region list */
51 #define MAX_POSTPONED_REGIONS 10
53 static bool vram_initialized;
54 static int postponed_cnt;
55 static struct {
56 unsigned long paddr;
57 size_t size;
58 } postponed_regions[MAX_POSTPONED_REGIONS];
60 struct vram_alloc {
61 struct list_head list;
62 unsigned long paddr;
63 unsigned pages;
66 struct vram_region {
67 struct list_head list;
68 struct list_head alloc_list;
69 unsigned long paddr;
70 unsigned pages;
73 static DEFINE_MUTEX(region_mutex);
74 static LIST_HEAD(region_list);
76 static inline int region_mem_type(unsigned long paddr)
78 if (paddr >= OMAP2_SRAM_START &&
79 paddr < OMAP2_SRAM_START + OMAP2_SRAM_SIZE)
80 return OMAP_VRAM_MEMTYPE_SRAM;
81 else
82 return OMAP_VRAM_MEMTYPE_SDRAM;
85 static struct vram_region *omap_vram_create_region(unsigned long paddr,
86 unsigned pages)
88 struct vram_region *rm;
90 rm = kzalloc(sizeof(*rm), GFP_KERNEL);
92 if (rm) {
93 INIT_LIST_HEAD(&rm->alloc_list);
94 rm->paddr = paddr;
95 rm->pages = pages;
98 return rm;
101 #if 0
102 static void omap_vram_free_region(struct vram_region *vr)
104 list_del(&vr->list);
105 kfree(vr);
107 #endif
109 static struct vram_alloc *omap_vram_create_allocation(struct vram_region *vr,
110 unsigned long paddr, unsigned pages)
112 struct vram_alloc *va;
113 struct vram_alloc *new;
115 new = kzalloc(sizeof(*va), GFP_KERNEL);
117 if (!new)
118 return NULL;
120 new->paddr = paddr;
121 new->pages = pages;
123 list_for_each_entry(va, &vr->alloc_list, list) {
124 if (va->paddr > new->paddr)
125 break;
128 list_add_tail(&new->list, &va->list);
130 return new;
133 static void omap_vram_free_allocation(struct vram_alloc *va)
135 list_del(&va->list);
136 kfree(va);
139 int omap_vram_add_region(unsigned long paddr, size_t size)
141 struct vram_region *rm;
142 unsigned pages;
144 if (vram_initialized) {
145 DBG("adding region paddr %08lx size %d\n",
146 paddr, size);
148 size &= PAGE_MASK;
149 pages = size >> PAGE_SHIFT;
151 rm = omap_vram_create_region(paddr, pages);
152 if (rm == NULL)
153 return -ENOMEM;
155 list_add(&rm->list, &region_list);
156 } else {
157 if (postponed_cnt == MAX_POSTPONED_REGIONS)
158 return -ENOMEM;
160 postponed_regions[postponed_cnt].paddr = paddr;
161 postponed_regions[postponed_cnt].size = size;
163 ++postponed_cnt;
165 return 0;
168 int omap_vram_free(unsigned long paddr, size_t size)
170 struct vram_region *rm;
171 struct vram_alloc *alloc;
172 unsigned start, end;
174 DBG("free mem paddr %08lx size %d\n", paddr, size);
176 size = PAGE_ALIGN(size);
178 mutex_lock(&region_mutex);
180 list_for_each_entry(rm, &region_list, list) {
181 list_for_each_entry(alloc, &rm->alloc_list, list) {
182 start = alloc->paddr;
183 end = alloc->paddr + (alloc->pages >> PAGE_SHIFT);
185 if (start >= paddr && end < paddr + size)
186 goto found;
190 mutex_unlock(&region_mutex);
191 return -EINVAL;
193 found:
194 omap_vram_free_allocation(alloc);
196 mutex_unlock(&region_mutex);
197 return 0;
199 EXPORT_SYMBOL(omap_vram_free);
201 static int _omap_vram_reserve(unsigned long paddr, unsigned pages)
203 struct vram_region *rm;
204 struct vram_alloc *alloc;
205 size_t size;
207 size = pages << PAGE_SHIFT;
209 list_for_each_entry(rm, &region_list, list) {
210 unsigned long start, end;
212 DBG("checking region %lx %d\n", rm->paddr, rm->pages);
214 if (region_mem_type(rm->paddr) != region_mem_type(paddr))
215 continue;
217 start = rm->paddr;
218 end = start + (rm->pages << PAGE_SHIFT) - 1;
219 if (start > paddr || end < paddr + size - 1)
220 continue;
222 DBG("block ok, checking allocs\n");
224 list_for_each_entry(alloc, &rm->alloc_list, list) {
225 end = alloc->paddr - 1;
227 if (start <= paddr && end >= paddr + size - 1)
228 goto found;
230 start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
233 end = rm->paddr + (rm->pages << PAGE_SHIFT) - 1;
235 if (!(start <= paddr && end >= paddr + size - 1))
236 continue;
237 found:
238 DBG("found area start %lx, end %lx\n", start, end);
240 if (omap_vram_create_allocation(rm, paddr, pages) == NULL)
241 return -ENOMEM;
243 return 0;
246 return -ENOMEM;
249 int omap_vram_reserve(unsigned long paddr, size_t size)
251 unsigned pages;
252 int r;
254 DBG("reserve mem paddr %08lx size %d\n", paddr, size);
256 size = PAGE_ALIGN(size);
257 pages = size >> PAGE_SHIFT;
259 mutex_lock(&region_mutex);
261 r = _omap_vram_reserve(paddr, pages);
263 mutex_unlock(&region_mutex);
265 return r;
267 EXPORT_SYMBOL(omap_vram_reserve);
269 static void _omap_vram_dma_cb(int lch, u16 ch_status, void *data)
271 struct completion *compl = data;
272 complete(compl);
275 static int _omap_vram_clear(u32 paddr, unsigned pages)
277 struct completion compl;
278 unsigned elem_count;
279 unsigned frame_count;
280 int r;
281 int lch;
283 init_completion(&compl);
285 r = omap_request_dma(OMAP_DMA_NO_DEVICE, "VRAM DMA",
286 _omap_vram_dma_cb,
287 &compl, &lch);
288 if (r) {
289 pr_err("VRAM: request_dma failed for memory clear\n");
290 return -EBUSY;
293 elem_count = pages * PAGE_SIZE / 4;
294 frame_count = 1;
296 omap_set_dma_transfer_params(lch, OMAP_DMA_DATA_TYPE_S32,
297 elem_count, frame_count,
298 OMAP_DMA_SYNC_ELEMENT,
299 0, 0);
301 omap_set_dma_dest_params(lch, 0, OMAP_DMA_AMODE_POST_INC,
302 paddr, 0, 0);
304 omap_set_dma_color_mode(lch, OMAP_DMA_CONSTANT_FILL, 0x000000);
306 omap_start_dma(lch);
308 if (wait_for_completion_timeout(&compl, msecs_to_jiffies(1000)) == 0) {
309 omap_stop_dma(lch);
310 pr_err("VRAM: dma timeout while clearing memory\n");
311 r = -EIO;
312 goto err;
315 r = 0;
316 err:
317 omap_free_dma(lch);
319 return r;
322 static int _omap_vram_alloc(int mtype, unsigned pages, unsigned long *paddr)
324 struct vram_region *rm;
325 struct vram_alloc *alloc;
327 list_for_each_entry(rm, &region_list, list) {
328 unsigned long start, end;
330 DBG("checking region %lx %d\n", rm->paddr, rm->pages);
332 if (region_mem_type(rm->paddr) != mtype)
333 continue;
335 start = rm->paddr;
337 list_for_each_entry(alloc, &rm->alloc_list, list) {
338 end = alloc->paddr;
340 if (end - start >= pages << PAGE_SHIFT)
341 goto found;
343 start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
346 end = rm->paddr + (rm->pages << PAGE_SHIFT);
347 found:
348 if (end - start < pages << PAGE_SHIFT)
349 continue;
351 DBG("found %lx, end %lx\n", start, end);
353 alloc = omap_vram_create_allocation(rm, start, pages);
354 if (alloc == NULL)
355 return -ENOMEM;
357 *paddr = start;
359 _omap_vram_clear(start, pages);
361 return 0;
364 return -ENOMEM;
367 int omap_vram_alloc(int mtype, size_t size, unsigned long *paddr)
369 unsigned pages;
370 int r;
372 BUG_ON(mtype > OMAP_VRAM_MEMTYPE_MAX || !size);
374 DBG("alloc mem type %d size %d\n", mtype, size);
376 size = PAGE_ALIGN(size);
377 pages = size >> PAGE_SHIFT;
379 mutex_lock(&region_mutex);
381 r = _omap_vram_alloc(mtype, pages, paddr);
383 mutex_unlock(&region_mutex);
385 return r;
387 EXPORT_SYMBOL(omap_vram_alloc);
389 void omap_vram_get_info(unsigned long *vram,
390 unsigned long *free_vram,
391 unsigned long *largest_free_block)
393 struct vram_region *vr;
394 struct vram_alloc *va;
396 *vram = 0;
397 *free_vram = 0;
398 *largest_free_block = 0;
400 mutex_lock(&region_mutex);
402 list_for_each_entry(vr, &region_list, list) {
403 unsigned free;
404 unsigned long pa;
406 pa = vr->paddr;
407 *vram += vr->pages << PAGE_SHIFT;
409 list_for_each_entry(va, &vr->alloc_list, list) {
410 free = va->paddr - pa;
411 *free_vram += free;
412 if (free > *largest_free_block)
413 *largest_free_block = free;
414 pa = va->paddr + (va->pages << PAGE_SHIFT);
417 free = vr->paddr + (vr->pages << PAGE_SHIFT) - pa;
418 *free_vram += free;
419 if (free > *largest_free_block)
420 *largest_free_block = free;
423 mutex_unlock(&region_mutex);
425 EXPORT_SYMBOL(omap_vram_get_info);
427 #if defined(CONFIG_DEBUG_FS)
428 static int vram_debug_show(struct seq_file *s, void *unused)
430 struct vram_region *vr;
431 struct vram_alloc *va;
432 unsigned size;
434 mutex_lock(&region_mutex);
436 list_for_each_entry(vr, &region_list, list) {
437 size = vr->pages << PAGE_SHIFT;
438 seq_printf(s, "%08lx-%08lx (%d bytes)\n",
439 vr->paddr, vr->paddr + size - 1,
440 size);
442 list_for_each_entry(va, &vr->alloc_list, list) {
443 size = va->pages << PAGE_SHIFT;
444 seq_printf(s, " %08lx-%08lx (%d bytes)\n",
445 va->paddr, va->paddr + size - 1,
446 size);
450 mutex_unlock(&region_mutex);
452 return 0;
455 static int vram_debug_open(struct inode *inode, struct file *file)
457 return single_open(file, vram_debug_show, inode->i_private);
460 static const struct file_operations vram_debug_fops = {
461 .open = vram_debug_open,
462 .read = seq_read,
463 .llseek = seq_lseek,
464 .release = single_release,
467 static int __init omap_vram_create_debugfs(void)
469 struct dentry *d;
471 d = debugfs_create_file("vram", S_IRUGO, NULL,
472 NULL, &vram_debug_fops);
473 if (IS_ERR(d))
474 return PTR_ERR(d);
476 return 0;
478 #endif
480 static __init int omap_vram_init(void)
482 int i;
484 vram_initialized = 1;
486 for (i = 0; i < postponed_cnt; i++)
487 omap_vram_add_region(postponed_regions[i].paddr,
488 postponed_regions[i].size);
490 #ifdef CONFIG_DEBUG_FS
491 if (omap_vram_create_debugfs())
492 pr_err("VRAM: Failed to create debugfs file\n");
493 #endif
495 return 0;
498 arch_initcall(omap_vram_init);
500 /* boottime vram alloc stuff */
502 /* set from board file */
503 static u32 omap_vram_sram_start __initdata;
504 static u32 omap_vram_sram_size __initdata;
506 /* set from board file */
507 static u32 omap_vram_sdram_start __initdata;
508 static u32 omap_vram_sdram_size __initdata;
510 /* set from kernel cmdline */
511 static u32 omap_vram_def_sdram_size __initdata;
512 static u32 omap_vram_def_sdram_start __initdata;
514 static int __init omap_vram_early_vram(char *p)
516 omap_vram_def_sdram_size = memparse(p, &p);
517 if (*p == ',')
518 omap_vram_def_sdram_start = simple_strtoul(p + 1, &p, 16);
519 return 0;
521 early_param("vram", omap_vram_early_vram);
524 * Called from map_io. We need to call to this early enough so that we
525 * can reserve the fixed SDRAM regions before VM could get hold of them.
527 void __init omap_vram_reserve_sdram(void)
529 struct bootmem_data *bdata;
530 unsigned long sdram_start, sdram_size;
531 u32 paddr;
532 u32 size = 0;
534 /* cmdline arg overrides the board file definition */
535 if (omap_vram_def_sdram_size) {
536 size = omap_vram_def_sdram_size;
537 paddr = omap_vram_def_sdram_start;
540 if (!size) {
541 size = omap_vram_sdram_size;
542 paddr = omap_vram_sdram_start;
545 #ifdef CONFIG_OMAP2_VRAM_SIZE
546 if (!size) {
547 size = CONFIG_OMAP2_VRAM_SIZE * 1024 * 1024;
548 paddr = 0;
550 #endif
552 if (!size)
553 return;
555 size = PAGE_ALIGN(size);
557 bdata = NODE_DATA(0)->bdata;
558 sdram_start = bdata->node_min_pfn << PAGE_SHIFT;
559 sdram_size = (bdata->node_low_pfn << PAGE_SHIFT) - sdram_start;
561 if (paddr) {
562 if ((paddr & ~PAGE_MASK) || paddr < sdram_start ||
563 paddr + size > sdram_start + sdram_size) {
564 pr_err("Illegal SDRAM region for VRAM\n");
565 return;
568 if (reserve_bootmem(paddr, size, BOOTMEM_EXCLUSIVE) < 0) {
569 pr_err("FB: failed to reserve VRAM\n");
570 return;
572 } else {
573 if (size > sdram_size) {
574 pr_err("Illegal SDRAM size for VRAM\n");
575 return;
578 paddr = virt_to_phys(alloc_bootmem_pages(size));
579 BUG_ON(paddr & ~PAGE_MASK);
582 omap_vram_add_region(paddr, size);
584 pr_info("Reserving %u bytes SDRAM for VRAM\n", size);
588 * Called at sram init time, before anything is pushed to the SRAM stack.
589 * Because of the stack scheme, we will allocate everything from the
590 * start of the lowest address region to the end of SRAM. This will also
591 * include padding for page alignment and possible holes between regions.
593 * As opposed to the SDRAM case, we'll also do any dynamic allocations at
594 * this point, since the driver built as a module would have problem with
595 * freeing / reallocating the regions.
597 unsigned long __init omap_vram_reserve_sram(unsigned long sram_pstart,
598 unsigned long sram_vstart,
599 unsigned long sram_size,
600 unsigned long pstart_avail,
601 unsigned long size_avail)
603 unsigned long pend_avail;
604 unsigned long reserved;
605 u32 paddr;
606 u32 size;
608 paddr = omap_vram_sram_start;
609 size = omap_vram_sram_size;
611 if (!size)
612 return 0;
614 reserved = 0;
615 pend_avail = pstart_avail + size_avail;
617 if (!paddr) {
618 /* Dynamic allocation */
619 if ((size_avail & PAGE_MASK) < size) {
620 pr_err("Not enough SRAM for VRAM\n");
621 return 0;
623 size_avail = (size_avail - size) & PAGE_MASK;
624 paddr = pstart_avail + size_avail;
627 if (paddr < sram_pstart ||
628 paddr + size > sram_pstart + sram_size) {
629 pr_err("Illegal SRAM region for VRAM\n");
630 return 0;
633 /* Reserve everything above the start of the region. */
634 if (pend_avail - paddr > reserved)
635 reserved = pend_avail - paddr;
636 size_avail = pend_avail - reserved - pstart_avail;
638 omap_vram_add_region(paddr, size);
640 if (reserved)
641 pr_info("Reserving %lu bytes SRAM for VRAM\n", reserved);
643 return reserved;
646 void __init omap_vram_set_sdram_vram(u32 size, u32 start)
648 omap_vram_sdram_start = start;
649 omap_vram_sdram_size = size;
652 void __init omap_vram_set_sram_vram(u32 size, u32 start)
654 omap_vram_sram_start = start;
655 omap_vram_sram_size = size;