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/bootmem.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 void omap_vram_free_region(struct vram_region
*vr
)
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
);
124 list_for_each_entry(va
, &vr
->alloc_list
, list
) {
125 if (va
->paddr
> new->paddr
)
129 list_add_tail(&new->list
, &va
->list
);
134 static void omap_vram_free_allocation(struct vram_alloc
*va
)
140 int omap_vram_add_region(unsigned long paddr
, size_t size
)
142 struct vram_region
*rm
;
145 if (vram_initialized
) {
146 DBG("adding region paddr %08lx size %d\n",
150 pages
= size
>> PAGE_SHIFT
;
152 rm
= omap_vram_create_region(paddr
, pages
);
156 list_add(&rm
->list
, ®ion_list
);
158 if (postponed_cnt
== MAX_POSTPONED_REGIONS
)
161 postponed_regions
[postponed_cnt
].paddr
= paddr
;
162 postponed_regions
[postponed_cnt
].size
= size
;
169 int omap_vram_free(unsigned long paddr
, size_t size
)
171 struct vram_region
*rm
;
172 struct vram_alloc
*alloc
;
175 DBG("free mem paddr %08lx size %d\n", paddr
, size
);
177 size
= PAGE_ALIGN(size
);
179 mutex_lock(®ion_mutex
);
181 list_for_each_entry(rm
, ®ion_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
)
191 mutex_unlock(®ion_mutex
);
195 omap_vram_free_allocation(alloc
);
197 mutex_unlock(®ion_mutex
);
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
;
208 size
= pages
<< PAGE_SHIFT
;
210 list_for_each_entry(rm
, ®ion_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
))
219 end
= start
+ (rm
->pages
<< PAGE_SHIFT
) - 1;
220 if (start
> paddr
|| end
< paddr
+ size
- 1)
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)
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))
239 DBG("found area start %lx, end %lx\n", start
, end
);
241 if (omap_vram_create_allocation(rm
, paddr
, pages
) == NULL
)
250 int omap_vram_reserve(unsigned long paddr
, size_t size
)
255 DBG("reserve mem paddr %08lx size %d\n", paddr
, size
);
257 size
= PAGE_ALIGN(size
);
258 pages
= size
>> PAGE_SHIFT
;
260 mutex_lock(®ion_mutex
);
262 r
= _omap_vram_reserve(paddr
, pages
);
264 mutex_unlock(®ion_mutex
);
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
;
276 static int _omap_vram_clear(u32 paddr
, unsigned pages
)
278 struct completion
compl;
280 unsigned frame_count
;
284 init_completion(&compl);
286 r
= omap_request_dma(OMAP_DMA_NO_DEVICE
, "VRAM DMA",
290 pr_err("VRAM: request_dma failed for memory clear\n");
294 elem_count
= pages
* PAGE_SIZE
/ 4;
297 omap_set_dma_transfer_params(lch
, OMAP_DMA_DATA_TYPE_S32
,
298 elem_count
, frame_count
,
299 OMAP_DMA_SYNC_ELEMENT
,
302 omap_set_dma_dest_params(lch
, 0, OMAP_DMA_AMODE_POST_INC
,
305 omap_set_dma_color_mode(lch
, OMAP_DMA_CONSTANT_FILL
, 0x000000);
309 if (wait_for_completion_timeout(&compl, msecs_to_jiffies(1000)) == 0) {
311 pr_err("VRAM: dma timeout while clearing memory\n");
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
, ®ion_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
)
338 list_for_each_entry(alloc
, &rm
->alloc_list
, list
) {
341 if (end
- start
>= pages
<< PAGE_SHIFT
)
344 start
= alloc
->paddr
+ (alloc
->pages
<< PAGE_SHIFT
);
347 end
= rm
->paddr
+ (rm
->pages
<< PAGE_SHIFT
);
349 if (end
- start
< pages
<< PAGE_SHIFT
)
352 DBG("found %lx, end %lx\n", start
, end
);
354 alloc
= omap_vram_create_allocation(rm
, start
, pages
);
360 _omap_vram_clear(start
, pages
);
368 int omap_vram_alloc(int mtype
, size_t size
, unsigned long *paddr
)
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(®ion_mutex
);
382 r
= _omap_vram_alloc(mtype
, pages
, paddr
);
384 mutex_unlock(®ion_mutex
);
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
;
399 *largest_free_block
= 0;
401 mutex_lock(®ion_mutex
);
403 list_for_each_entry(vr
, ®ion_list
, list
) {
408 *vram
+= vr
->pages
<< PAGE_SHIFT
;
410 list_for_each_entry(va
, &vr
->alloc_list
, list
) {
411 free
= va
->paddr
- pa
;
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
;
420 if (free
> *largest_free_block
)
421 *largest_free_block
= free
;
424 mutex_unlock(®ion_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
;
435 mutex_lock(®ion_mutex
);
437 list_for_each_entry(vr
, ®ion_list
, list
) {
438 size
= vr
->pages
<< PAGE_SHIFT
;
439 seq_printf(s
, "%08lx-%08lx (%d bytes)\n",
440 vr
->paddr
, vr
->paddr
+ size
- 1,
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,
451 mutex_unlock(®ion_mutex
);
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
,
465 .release
= single_release
,
468 static int __init
omap_vram_create_debugfs(void)
472 d
= debugfs_create_file("vram", S_IRUGO
, NULL
,
473 NULL
, &vram_debug_fops
);
481 static __init
int omap_vram_init(void)
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");
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
);
519 omap_vram_def_sdram_start
= simple_strtoul(p
+ 1, &p
, 16);
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(void)
530 struct bootmem_data
*bdata
;
531 unsigned long sdram_start
, sdram_size
;
535 /* cmdline arg overrides the board file definition */
536 if (omap_vram_def_sdram_size
) {
537 size
= omap_vram_def_sdram_size
;
538 paddr
= omap_vram_def_sdram_start
;
542 size
= omap_vram_sdram_size
;
543 paddr
= omap_vram_sdram_start
;
546 #ifdef CONFIG_OMAP2_VRAM_SIZE
548 size
= CONFIG_OMAP2_VRAM_SIZE
* 1024 * 1024;
556 size
= PAGE_ALIGN(size
);
558 bdata
= NODE_DATA(0)->bdata
;
559 sdram_start
= bdata
->node_min_pfn
<< PAGE_SHIFT
;
560 sdram_size
= (bdata
->node_low_pfn
<< PAGE_SHIFT
) - sdram_start
;
563 if ((paddr
& ~PAGE_MASK
) || paddr
< sdram_start
||
564 paddr
+ size
> sdram_start
+ sdram_size
) {
565 pr_err("Illegal SDRAM region for VRAM\n");
569 if (reserve_bootmem(paddr
, size
, BOOTMEM_EXCLUSIVE
) < 0) {
570 pr_err("FB: failed to reserve VRAM\n");
574 if (size
> sdram_size
) {
575 pr_err("Illegal SDRAM size for VRAM\n");
579 paddr
= virt_to_phys(alloc_bootmem_pages(size
));
580 BUG_ON(paddr
& ~PAGE_MASK
);
583 omap_vram_add_region(paddr
, size
);
585 pr_info("Reserving %u bytes SDRAM for VRAM\n", size
);
589 * Called at sram init time, before anything is pushed to the SRAM stack.
590 * Because of the stack scheme, we will allocate everything from the
591 * start of the lowest address region to the end of SRAM. This will also
592 * include padding for page alignment and possible holes between regions.
594 * As opposed to the SDRAM case, we'll also do any dynamic allocations at
595 * this point, since the driver built as a module would have problem with
596 * freeing / reallocating the regions.
598 unsigned long __init
omap_vram_reserve_sram(unsigned long sram_pstart
,
599 unsigned long sram_vstart
,
600 unsigned long sram_size
,
601 unsigned long pstart_avail
,
602 unsigned long size_avail
)
604 unsigned long pend_avail
;
605 unsigned long reserved
;
609 paddr
= omap_vram_sram_start
;
610 size
= omap_vram_sram_size
;
616 pend_avail
= pstart_avail
+ size_avail
;
619 /* Dynamic allocation */
620 if ((size_avail
& PAGE_MASK
) < size
) {
621 pr_err("Not enough SRAM for VRAM\n");
624 size_avail
= (size_avail
- size
) & PAGE_MASK
;
625 paddr
= pstart_avail
+ size_avail
;
628 if (paddr
< sram_pstart
||
629 paddr
+ size
> sram_pstart
+ sram_size
) {
630 pr_err("Illegal SRAM region for VRAM\n");
634 /* Reserve everything above the start of the region. */
635 if (pend_avail
- paddr
> reserved
)
636 reserved
= pend_avail
- paddr
;
637 size_avail
= pend_avail
- reserved
- pstart_avail
;
639 omap_vram_add_region(paddr
, size
);
642 pr_info("Reserving %lu bytes SRAM for VRAM\n", reserved
);
647 void __init
omap_vram_set_sdram_vram(u32 size
, u32 start
)
649 omap_vram_sdram_start
= start
;
650 omap_vram_sdram_size
= size
;
653 void __init
omap_vram_set_sram_vram(u32 size
, u32 start
)
655 omap_vram_sram_start
= start
;
656 omap_vram_sram_size
= size
;