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/vram.h>
39 #define DBG(format, ...) pr_debug("VRAM: " format, ## __VA_ARGS__)
41 #define DBG(format, ...)
44 /* postponed regions are used to temporarily store region information at boot
45 * time when we cannot yet allocate the region list */
46 #define MAX_POSTPONED_REGIONS 10
48 static bool vram_initialized;
49 static int postponed_cnt;
53 } postponed_regions[MAX_POSTPONED_REGIONS];
56 struct list_head list;
62 struct list_head list;
63 struct list_head alloc_list;
68 static DEFINE_MUTEX(region_mutex);
69 static LIST_HEAD(region_list);
71 static struct vram_region *omap_vram_create_region(unsigned long paddr,
74 struct vram_region *rm;
76 rm = kzalloc(sizeof(*rm), GFP_KERNEL);
79 INIT_LIST_HEAD(&rm->alloc_list);
88 static void omap_vram_free_region(struct vram_region *vr)
95 static struct vram_alloc *omap_vram_create_allocation(struct vram_region *vr,
96 unsigned long paddr, unsigned pages)
98 struct vram_alloc *va;
99 struct vram_alloc *new;
101 new = kzalloc(sizeof(*va), GFP_KERNEL);
109 list_for_each_entry(va, &vr->alloc_list, list) {
110 if (va->paddr > new->paddr)
114 list_add_tail(&new->list, &va->list);
119 static void omap_vram_free_allocation(struct vram_alloc *va)
125 int omap_vram_add_region(unsigned long paddr, size_t size)
127 struct vram_region *rm;
130 if (vram_initialized) {
131 DBG("adding region paddr %08lx size %d\n",
135 pages = size >> PAGE_SHIFT;
137 rm = omap_vram_create_region(paddr, pages);
141 list_add(&rm->list, ®ion_list);
143 if (postponed_cnt == MAX_POSTPONED_REGIONS)
146 postponed_regions[postponed_cnt].paddr = paddr;
147 postponed_regions[postponed_cnt].size = size;
154 int omap_vram_free(unsigned long paddr, size_t size)
156 struct vram_region *rm;
157 struct vram_alloc *alloc;
160 DBG("free mem paddr %08lx size %d\n", paddr, size);
162 size = PAGE_ALIGN(size);
164 mutex_lock(®ion_mutex);
166 list_for_each_entry(rm, ®ion_list, list) {
167 list_for_each_entry(alloc, &rm->alloc_list, list) {
168 start = alloc->paddr;
169 end = alloc->paddr + (alloc->pages >> PAGE_SHIFT);
171 if (start >= paddr && end < paddr + size)
176 mutex_unlock(®ion_mutex);
180 omap_vram_free_allocation(alloc);
182 mutex_unlock(®ion_mutex);
185 EXPORT_SYMBOL(omap_vram_free);
187 static int _omap_vram_reserve(unsigned long paddr, unsigned pages)
189 struct vram_region *rm;
190 struct vram_alloc *alloc;
193 size = pages << PAGE_SHIFT;
195 list_for_each_entry(rm, ®ion_list, list) {
196 unsigned long start, end;
198 DBG("checking region %lx %d\n", rm->paddr, rm->pages);
201 end = start + (rm->pages << PAGE_SHIFT) - 1;
202 if (start > paddr || end < paddr + size - 1)
205 DBG("block ok, checking allocs\n");
207 list_for_each_entry(alloc, &rm->alloc_list, list) {
208 end = alloc->paddr - 1;
210 if (start <= paddr && end >= paddr + size - 1)
213 start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
216 end = rm->paddr + (rm->pages << PAGE_SHIFT) - 1;
218 if (!(start <= paddr && end >= paddr + size - 1))
221 DBG("found area start %lx, end %lx\n", start, end);
223 if (omap_vram_create_allocation(rm, paddr, pages) == NULL)
232 int omap_vram_reserve(unsigned long paddr, size_t size)
237 DBG("reserve mem paddr %08lx size %d\n", paddr, size);
239 size = PAGE_ALIGN(size);
240 pages = size >> PAGE_SHIFT;
242 mutex_lock(®ion_mutex);
244 r = _omap_vram_reserve(paddr, pages);
246 mutex_unlock(®ion_mutex);
250 EXPORT_SYMBOL(omap_vram_reserve);
252 static int _omap_vram_alloc(unsigned pages, unsigned long *paddr)
254 struct vram_region *rm;
255 struct vram_alloc *alloc;
257 list_for_each_entry(rm, ®ion_list, list) {
258 unsigned long start, end;
260 DBG("checking region %lx %d\n", rm->paddr, rm->pages);
264 list_for_each_entry(alloc, &rm->alloc_list, list) {
267 if (end - start >= pages << PAGE_SHIFT)
270 start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
273 end = rm->paddr + (rm->pages << PAGE_SHIFT);
275 if (end - start < pages << PAGE_SHIFT)
278 DBG("found %lx, end %lx\n", start, end);
280 alloc = omap_vram_create_allocation(rm, start, pages);
292 int omap_vram_alloc(size_t size, unsigned long *paddr)
299 DBG("alloc mem size %d\n", size);
301 size = PAGE_ALIGN(size);
302 pages = size >> PAGE_SHIFT;
304 mutex_lock(®ion_mutex);
306 r = _omap_vram_alloc(pages, paddr);
308 mutex_unlock(®ion_mutex);
312 EXPORT_SYMBOL(omap_vram_alloc);
314 void omap_vram_get_info(unsigned long *vram,
315 unsigned long *free_vram,
316 unsigned long *largest_free_block)
318 struct vram_region *vr;
319 struct vram_alloc *va;
323 *largest_free_block = 0;
325 mutex_lock(®ion_mutex);
327 list_for_each_entry(vr, ®ion_list, list) {
332 *vram += vr->pages << PAGE_SHIFT;
334 list_for_each_entry(va, &vr->alloc_list, list) {
335 free = va->paddr - pa;
337 if (free > *largest_free_block)
338 *largest_free_block = free;
339 pa = va->paddr + (va->pages << PAGE_SHIFT);
342 free = vr->paddr + (vr->pages << PAGE_SHIFT) - pa;
344 if (free > *largest_free_block)
345 *largest_free_block = free;
348 mutex_unlock(®ion_mutex);
350 EXPORT_SYMBOL(omap_vram_get_info);
352 #if defined(CONFIG_DEBUG_FS)
353 static int vram_debug_show(struct seq_file *s, void *unused)
355 struct vram_region *vr;
356 struct vram_alloc *va;
359 mutex_lock(®ion_mutex);
361 list_for_each_entry(vr, ®ion_list, list) {
362 size = vr->pages << PAGE_SHIFT;
363 seq_printf(s, "%08lx-%08lx (%d bytes)\n",
364 vr->paddr, vr->paddr + size - 1,
367 list_for_each_entry(va, &vr->alloc_list, list) {
368 size = va->pages << PAGE_SHIFT;
369 seq_printf(s, " %08lx-%08lx (%d bytes)\n",
370 va->paddr, va->paddr + size - 1,
375 mutex_unlock(®ion_mutex);
380 static int vram_debug_open(struct inode *inode, struct file *file)
382 return single_open(file, vram_debug_show, inode->i_private);
385 static const struct file_operations vram_debug_fops = {
386 .open = vram_debug_open,
389 .release = single_release,
392 static int __init omap_vram_create_debugfs(void)
396 d = debugfs_create_file("vram", S_IRUGO, NULL,
397 NULL, &vram_debug_fops);
405 static __init int omap_vram_init(void)
409 vram_initialized = 1;
411 for (i = 0; i < postponed_cnt; i++)
412 omap_vram_add_region(postponed_regions[i].paddr,
413 postponed_regions[i].size);
415 #ifdef CONFIG_DEBUG_FS
416 if (omap_vram_create_debugfs())
417 pr_err("VRAM: Failed to create debugfs file\n");
423 arch_initcall(omap_vram_init);
425 /* boottime vram alloc stuff */
427 /* set from board file */
428 static u32 omap_vram_sdram_start __initdata;
429 static u32 omap_vram_sdram_size __initdata;
431 /* set from kernel cmdline */
432 static u32 omap_vram_def_sdram_size __initdata;
433 static u32 omap_vram_def_sdram_start __initdata;
435 static int __init omap_vram_early_vram(char *p)
437 omap_vram_def_sdram_size = memparse(p, &p);
439 omap_vram_def_sdram_start = simple_strtoul(p + 1, &p, 16);
442 early_param("vram", omap_vram_early_vram);
445 * Called from map_io. We need to call to this early enough so that we
446 * can reserve the fixed SDRAM regions before VM could get hold of them.
448 void __init omap_vram_reserve_sdram_memblock(void)
453 /* cmdline arg overrides the board file definition */
454 if (omap_vram_def_sdram_size) {
455 size = omap_vram_def_sdram_size;
456 paddr = omap_vram_def_sdram_start;
460 size = omap_vram_sdram_size;
461 paddr = omap_vram_sdram_start;
464 #ifdef CONFIG_OMAP2_VRAM_SIZE
466 size = CONFIG_OMAP2_VRAM_SIZE * 1024 * 1024;
474 size = ALIGN(size, SZ_2M);
477 if (paddr & ~PAGE_MASK) {
478 pr_err("VRAM start address 0x%08x not page aligned\n",
483 if (!memblock_is_region_memory(paddr, size)) {
484 pr_err("Illegal SDRAM region 0x%08x..0x%08x for VRAM\n",
485 paddr, paddr + size - 1);
489 if (memblock_is_region_reserved(paddr, size)) {
490 pr_err("FB: failed to reserve VRAM - busy\n");
494 if (memblock_reserve(paddr, size) < 0) {
495 pr_err("FB: failed to reserve VRAM - no memory\n");
499 paddr = memblock_alloc(size, SZ_2M);
502 memblock_free(paddr, size);
503 memblock_remove(paddr, size);
505 omap_vram_add_region(paddr, size);
507 pr_info("Reserving %u bytes SDRAM for VRAM\n", size);
510 void __init omap_vram_set_sdram_vram(u32 size, u32 start)
512 omap_vram_sdram_start = start;
513 omap_vram_sdram_size = size;