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/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>
40 #define DBG(format, ...) pr_debug("VRAM: " format, ## __VA_ARGS__)
42 #define DBG(format, ...)
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
;
58 } postponed_regions
[MAX_POSTPONED_REGIONS
];
61 struct list_head list
;
67 struct list_head list
;
68 struct list_head alloc_list
;
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
;
82 return OMAP_VRAM_MEMTYPE_SDRAM
;
85 static struct vram_region
*omap_vram_create_region(unsigned long paddr
,
88 struct vram_region
*rm
;
90 rm
= kzalloc(sizeof(*rm
), GFP_KERNEL
);
93 INIT_LIST_HEAD(&rm
->alloc_list
);
102 static void omap_vram_free_region(struct vram_region
*vr
)
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
);
123 list_for_each_entry(va
, &vr
->alloc_list
, list
) {
124 if (va
->paddr
> new->paddr
)
128 list_add_tail(&new->list
, &va
->list
);
133 static void omap_vram_free_allocation(struct vram_alloc
*va
)
139 int omap_vram_add_region(unsigned long paddr
, size_t size
)
141 struct vram_region
*rm
;
144 if (vram_initialized
) {
145 DBG("adding region paddr %08lx size %d\n",
149 pages
= size
>> PAGE_SHIFT
;
151 rm
= omap_vram_create_region(paddr
, pages
);
155 list_add(&rm
->list
, ®ion_list
);
157 if (postponed_cnt
== MAX_POSTPONED_REGIONS
)
160 postponed_regions
[postponed_cnt
].paddr
= paddr
;
161 postponed_regions
[postponed_cnt
].size
= size
;
168 int omap_vram_free(unsigned long paddr
, size_t size
)
170 struct vram_region
*rm
;
171 struct vram_alloc
*alloc
;
174 DBG("free mem paddr %08lx size %d\n", paddr
, size
);
176 size
= PAGE_ALIGN(size
);
178 mutex_lock(®ion_mutex
);
180 list_for_each_entry(rm
, ®ion_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
)
190 mutex_unlock(®ion_mutex
);
194 omap_vram_free_allocation(alloc
);
196 mutex_unlock(®ion_mutex
);
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
;
207 size
= pages
<< PAGE_SHIFT
;
209 list_for_each_entry(rm
, ®ion_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
))
218 end
= start
+ (rm
->pages
<< PAGE_SHIFT
) - 1;
219 if (start
> paddr
|| end
< paddr
+ size
- 1)
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)
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))
238 DBG("found area start %lx, end %lx\n", start
, end
);
240 if (omap_vram_create_allocation(rm
, paddr
, pages
) == NULL
)
249 int omap_vram_reserve(unsigned long paddr
, size_t size
)
254 DBG("reserve mem paddr %08lx size %d\n", paddr
, size
);
256 size
= PAGE_ALIGN(size
);
257 pages
= size
>> PAGE_SHIFT
;
259 mutex_lock(®ion_mutex
);
261 r
= _omap_vram_reserve(paddr
, pages
);
263 mutex_unlock(®ion_mutex
);
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
;
275 static int _omap_vram_clear(u32 paddr
, unsigned pages
)
277 struct completion
compl;
279 unsigned frame_count
;
283 init_completion(&compl);
285 r
= omap_request_dma(OMAP_DMA_NO_DEVICE
, "VRAM DMA",
289 pr_err("VRAM: request_dma failed for memory clear\n");
293 elem_count
= pages
* PAGE_SIZE
/ 4;
296 omap_set_dma_transfer_params(lch
, OMAP_DMA_DATA_TYPE_S32
,
297 elem_count
, frame_count
,
298 OMAP_DMA_SYNC_ELEMENT
,
301 omap_set_dma_dest_params(lch
, 0, OMAP_DMA_AMODE_POST_INC
,
304 omap_set_dma_color_mode(lch
, OMAP_DMA_CONSTANT_FILL
, 0x000000);
308 if (wait_for_completion_timeout(&compl, msecs_to_jiffies(1000)) == 0) {
310 pr_err("VRAM: dma timeout while clearing memory\n");
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
, ®ion_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
)
337 list_for_each_entry(alloc
, &rm
->alloc_list
, list
) {
340 if (end
- start
>= pages
<< PAGE_SHIFT
)
343 start
= alloc
->paddr
+ (alloc
->pages
<< PAGE_SHIFT
);
346 end
= rm
->paddr
+ (rm
->pages
<< PAGE_SHIFT
);
348 if (end
- start
< pages
<< PAGE_SHIFT
)
351 DBG("found %lx, end %lx\n", start
, end
);
353 alloc
= omap_vram_create_allocation(rm
, start
, pages
);
359 _omap_vram_clear(start
, pages
);
367 int omap_vram_alloc(int mtype
, size_t size
, unsigned long *paddr
)
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(®ion_mutex
);
381 r
= _omap_vram_alloc(mtype
, pages
, paddr
);
383 mutex_unlock(®ion_mutex
);
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
;
398 *largest_free_block
= 0;
400 mutex_lock(®ion_mutex
);
402 list_for_each_entry(vr
, ®ion_list
, list
) {
407 *vram
+= vr
->pages
<< PAGE_SHIFT
;
409 list_for_each_entry(va
, &vr
->alloc_list
, list
) {
410 free
= va
->paddr
- pa
;
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
;
419 if (free
> *largest_free_block
)
420 *largest_free_block
= free
;
423 mutex_unlock(®ion_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
;
434 mutex_lock(®ion_mutex
);
436 list_for_each_entry(vr
, ®ion_list
, list
) {
437 size
= vr
->pages
<< PAGE_SHIFT
;
438 seq_printf(s
, "%08lx-%08lx (%d bytes)\n",
439 vr
->paddr
, vr
->paddr
+ size
- 1,
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,
450 mutex_unlock(®ion_mutex
);
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
,
464 .release
= single_release
,
467 static int __init
omap_vram_create_debugfs(void)
471 d
= debugfs_create_file("vram", S_IRUGO
, NULL
,
472 NULL
, &vram_debug_fops
);
480 static __init
int omap_vram_init(void)
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");
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
);
518 omap_vram_def_sdram_start
= simple_strtoul(p
+ 1, &p
, 16);
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
;
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
;
541 size
= omap_vram_sdram_size
;
542 paddr
= omap_vram_sdram_start
;
545 #ifdef CONFIG_OMAP2_VRAM_SIZE
547 size
= CONFIG_OMAP2_VRAM_SIZE
* 1024 * 1024;
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
;
562 if ((paddr
& ~PAGE_MASK
) || paddr
< sdram_start
||
563 paddr
+ size
> sdram_start
+ sdram_size
) {
564 pr_err("Illegal SDRAM region for VRAM\n");
568 if (reserve_bootmem(paddr
, size
, BOOTMEM_EXCLUSIVE
) < 0) {
569 pr_err("FB: failed to reserve VRAM\n");
573 if (size
> sdram_size
) {
574 pr_err("Illegal SDRAM size for VRAM\n");
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
;
608 paddr
= omap_vram_sram_start
;
609 size
= omap_vram_sram_size
;
615 pend_avail
= pstart_avail
+ size_avail
;
618 /* Dynamic allocation */
619 if ((size_avail
& PAGE_MASK
) < size
) {
620 pr_err("Not enough SRAM for VRAM\n");
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");
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
);
641 pr_info("Reserving %lu bytes SRAM for VRAM\n", 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
;