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.
23 #include <linux/kernel.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>
41 #define DBG(format, ...) pr_debug("VRAM: " format, ## __VA_ARGS__)
43 #define DBG(format, ...)
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
;
59 } postponed_regions
[MAX_POSTPONED_REGIONS
];
62 struct list_head list
;
68 struct list_head list
;
69 struct list_head alloc_list
;
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
;
83 return OMAP_VRAM_MEMTYPE_SDRAM
;
86 static struct vram_region
*omap_vram_create_region(unsigned long paddr
,
89 struct vram_region
*rm
;
91 rm
= kzalloc(sizeof(*rm
), GFP_KERNEL
);
94 INIT_LIST_HEAD(&rm
->alloc_list
);
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
);
117 list_for_each_entry(va
, &vr
->alloc_list
, list
) {
118 if (va
->paddr
> new->paddr
)
122 list_add_tail(&new->list
, &va
->list
);
127 static void omap_vram_free_allocation(struct vram_alloc
*va
)
133 int omap_vram_add_region(unsigned long paddr
, size_t size
)
135 struct vram_region
*rm
;
138 if (vram_initialized
) {
139 DBG("adding region paddr %08lx size %d\n",
143 pages
= size
>> PAGE_SHIFT
;
145 rm
= omap_vram_create_region(paddr
, pages
);
149 list_add(&rm
->list
, ®ion_list
);
151 if (postponed_cnt
== MAX_POSTPONED_REGIONS
)
154 postponed_regions
[postponed_cnt
].paddr
= paddr
;
155 postponed_regions
[postponed_cnt
].size
= size
;
162 int omap_vram_free(unsigned long paddr
, size_t size
)
164 struct vram_region
*rm
;
165 struct vram_alloc
*alloc
;
168 DBG("free mem paddr %08lx size %d\n", paddr
, size
);
170 size
= PAGE_ALIGN(size
);
172 mutex_lock(®ion_mutex
);
174 list_for_each_entry(rm
, ®ion_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
)
184 mutex_unlock(®ion_mutex
);
188 omap_vram_free_allocation(alloc
);
190 mutex_unlock(®ion_mutex
);
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
;
201 size
= pages
<< PAGE_SHIFT
;
203 list_for_each_entry(rm
, ®ion_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
))
212 end
= start
+ (rm
->pages
<< PAGE_SHIFT
) - 1;
213 if (start
> paddr
|| end
< paddr
+ size
- 1)
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)
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))
232 DBG("found area start %lx, end %lx\n", start
, end
);
234 if (omap_vram_create_allocation(rm
, paddr
, pages
) == NULL
)
243 int omap_vram_reserve(unsigned long paddr
, size_t size
)
248 DBG("reserve mem paddr %08lx size %d\n", paddr
, size
);
250 size
= PAGE_ALIGN(size
);
251 pages
= size
>> PAGE_SHIFT
;
253 mutex_lock(®ion_mutex
);
255 r
= _omap_vram_reserve(paddr
, pages
);
257 mutex_unlock(®ion_mutex
);
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
;
269 static int _omap_vram_clear(u32 paddr
, unsigned pages
)
271 struct completion
compl;
273 unsigned frame_count
;
277 init_completion(&compl);
279 r
= omap_request_dma(OMAP_DMA_NO_DEVICE
, "VRAM DMA",
283 pr_err("VRAM: request_dma failed for memory clear\n");
287 elem_count
= pages
* PAGE_SIZE
/ 4;
290 omap_set_dma_transfer_params(lch
, OMAP_DMA_DATA_TYPE_S32
,
291 elem_count
, frame_count
,
292 OMAP_DMA_SYNC_ELEMENT
,
295 omap_set_dma_dest_params(lch
, 0, OMAP_DMA_AMODE_POST_INC
,
298 omap_set_dma_color_mode(lch
, OMAP_DMA_CONSTANT_FILL
, 0x000000);
302 if (wait_for_completion_timeout(&compl, msecs_to_jiffies(1000)) == 0) {
304 pr_err("VRAM: dma timeout while clearing memory\n");
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
, ®ion_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
)
331 list_for_each_entry(alloc
, &rm
->alloc_list
, list
) {
334 if (end
- start
>= pages
<< PAGE_SHIFT
)
337 start
= alloc
->paddr
+ (alloc
->pages
<< PAGE_SHIFT
);
340 end
= rm
->paddr
+ (rm
->pages
<< PAGE_SHIFT
);
342 if (end
- start
< pages
<< PAGE_SHIFT
)
345 DBG("found %lx, end %lx\n", start
, end
);
347 alloc
= omap_vram_create_allocation(rm
, start
, pages
);
353 _omap_vram_clear(start
, pages
);
361 int omap_vram_alloc(int mtype
, size_t size
, unsigned long *paddr
)
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(®ion_mutex
);
375 r
= _omap_vram_alloc(mtype
, pages
, paddr
);
377 mutex_unlock(®ion_mutex
);
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
;
392 *largest_free_block
= 0;
394 mutex_lock(®ion_mutex
);
396 list_for_each_entry(vr
, ®ion_list
, list
) {
401 *vram
+= vr
->pages
<< PAGE_SHIFT
;
403 list_for_each_entry(va
, &vr
->alloc_list
, list
) {
404 free
= va
->paddr
- pa
;
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
;
413 if (free
> *largest_free_block
)
414 *largest_free_block
= free
;
417 mutex_unlock(®ion_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
;
428 mutex_lock(®ion_mutex
);
430 list_for_each_entry(vr
, ®ion_list
, list
) {
431 size
= vr
->pages
<< PAGE_SHIFT
;
432 seq_printf(s
, "%08lx-%08lx (%d bytes)\n",
433 vr
->paddr
, vr
->paddr
+ size
- 1,
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,
444 mutex_unlock(®ion_mutex
);
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
,
458 .release
= single_release
,
461 static int __init
omap_vram_create_debugfs(void)
465 d
= debugfs_create_file("vram", S_IRUGO
, NULL
,
466 NULL
, &vram_debug_fops
);
474 static __init
int omap_vram_init(void)
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");
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
);
512 omap_vram_def_sdram_start
= simple_strtoul(p
+ 1, &p
, 16);
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)
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
;
533 size
= omap_vram_sdram_size
;
534 paddr
= omap_vram_sdram_start
;
537 #ifdef CONFIG_OMAP2_VRAM_SIZE
539 size
= CONFIG_OMAP2_VRAM_SIZE
* 1024 * 1024;
547 size
= PAGE_ALIGN(size
);
550 struct memblock_property res
;
554 if ((paddr
& ~PAGE_MASK
) || memblock_find(&res
) ||
555 res
.base
!= paddr
|| res
.size
!= size
) {
556 pr_err("Illegal SDRAM region for VRAM\n");
560 if (memblock_is_region_reserved(paddr
, size
)) {
561 pr_err("FB: failed to reserve VRAM - busy\n");
565 if (memblock_reserve(paddr
, size
) < 0) {
566 pr_err("FB: failed to reserve VRAM - no memory\n");
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
;
599 paddr
= omap_vram_sram_start
;
600 size
= omap_vram_sram_size
;
606 pend_avail
= pstart_avail
+ size_avail
;
609 /* Dynamic allocation */
610 if ((size_avail
& PAGE_MASK
) < size
) {
611 pr_err("Not enough SRAM for VRAM\n");
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");
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
);
632 pr_info("Reserving %lu bytes SRAM for VRAM\n", 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
;