blob: b266ffae0bdea5f23d4d8ca4fa007ed9166f605e [file] [log] [blame]
Tomi Valkeinenafedec12009-08-07 12:01:55 +03001/*
2 * VRAM manager for OMAP
3 *
4 * Copyright (C) 2009 Nokia Corporation
5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
6 *
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.
10 *
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.
15 *
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.
19 */
20
21/*#define DEBUG*/
22
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>
32
33#include <asm/setup.h>
34
35#include <plat/sram.h>
36#include <plat/vram.h>
37#include <plat/dma.h>
38
39#ifdef DEBUG
40#define DBG(format, ...) pr_debug("VRAM: " format, ## __VA_ARGS__)
41#else
42#define DBG(format, ...)
43#endif
44
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 */
48
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
52
53static bool vram_initialized;
54static int postponed_cnt;
55static struct {
56 unsigned long paddr;
57 size_t size;
58} postponed_regions[MAX_POSTPONED_REGIONS];
59
60struct vram_alloc {
61 struct list_head list;
62 unsigned long paddr;
63 unsigned pages;
64};
65
66struct vram_region {
67 struct list_head list;
68 struct list_head alloc_list;
69 unsigned long paddr;
70 unsigned pages;
71};
72
73static DEFINE_MUTEX(region_mutex);
74static LIST_HEAD(region_list);
75
76static inline int region_mem_type(unsigned long paddr)
77{
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;
83}
84
85static struct vram_region *omap_vram_create_region(unsigned long paddr,
86 unsigned pages)
87{
88 struct vram_region *rm;
89
90 rm = kzalloc(sizeof(*rm), GFP_KERNEL);
91
92 if (rm) {
93 INIT_LIST_HEAD(&rm->alloc_list);
94 rm->paddr = paddr;
95 rm->pages = pages;
96 }
97
98 return rm;
99}
100
101#if 0
102static void omap_vram_free_region(struct vram_region *vr)
103{
104 list_del(&vr->list);
105 kfree(vr);
106}
107#endif
108
109static struct vram_alloc *omap_vram_create_allocation(struct vram_region *vr,
110 unsigned long paddr, unsigned pages)
111{
112 struct vram_alloc *va;
113 struct vram_alloc *new;
114
115 new = kzalloc(sizeof(*va), GFP_KERNEL);
116
117 if (!new)
118 return NULL;
119
120 new->paddr = paddr;
121 new->pages = pages;
122
123 list_for_each_entry(va, &vr->alloc_list, list) {
124 if (va->paddr > new->paddr)
125 break;
126 }
127
128 list_add_tail(&new->list, &va->list);
129
130 return new;
131}
132
133static void omap_vram_free_allocation(struct vram_alloc *va)
134{
135 list_del(&va->list);
136 kfree(va);
137}
138
139int omap_vram_add_region(unsigned long paddr, size_t size)
140{
141 struct vram_region *rm;
142 unsigned pages;
143
144 if (vram_initialized) {
145 DBG("adding region paddr %08lx size %d\n",
146 paddr, size);
147
148 size &= PAGE_MASK;
149 pages = size >> PAGE_SHIFT;
150
151 rm = omap_vram_create_region(paddr, pages);
152 if (rm == NULL)
153 return -ENOMEM;
154
155 list_add(&rm->list, &region_list);
156 } else {
157 if (postponed_cnt == MAX_POSTPONED_REGIONS)
158 return -ENOMEM;
159
160 postponed_regions[postponed_cnt].paddr = paddr;
161 postponed_regions[postponed_cnt].size = size;
162
163 ++postponed_cnt;
164 }
165 return 0;
166}
167
168int omap_vram_free(unsigned long paddr, size_t size)
169{
170 struct vram_region *rm;
171 struct vram_alloc *alloc;
172 unsigned start, end;
173
174 DBG("free mem paddr %08lx size %d\n", paddr, size);
175
176 size = PAGE_ALIGN(size);
177
178 mutex_lock(&region_mutex);
179
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);
184
185 if (start >= paddr && end < paddr + size)
186 goto found;
187 }
188 }
189
190 mutex_unlock(&region_mutex);
191 return -EINVAL;
192
193found:
194 omap_vram_free_allocation(alloc);
195
196 mutex_unlock(&region_mutex);
197 return 0;
198}
199EXPORT_SYMBOL(omap_vram_free);
200
201static int _omap_vram_reserve(unsigned long paddr, unsigned pages)
202{
203 struct vram_region *rm;
204 struct vram_alloc *alloc;
205 size_t size;
206
207 size = pages << PAGE_SHIFT;
208
209 list_for_each_entry(rm, &region_list, list) {
210 unsigned long start, end;
211
212 DBG("checking region %lx %d\n", rm->paddr, rm->pages);
213
214 if (region_mem_type(rm->paddr) != region_mem_type(paddr))
215 continue;
216
217 start = rm->paddr;
218 end = start + (rm->pages << PAGE_SHIFT) - 1;
219 if (start > paddr || end < paddr + size - 1)
220 continue;
221
222 DBG("block ok, checking allocs\n");
223
224 list_for_each_entry(alloc, &rm->alloc_list, list) {
225 end = alloc->paddr - 1;
226
227 if (start <= paddr && end >= paddr + size - 1)
228 goto found;
229
230 start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
231 }
232
233 end = rm->paddr + (rm->pages << PAGE_SHIFT) - 1;
234
235 if (!(start <= paddr && end >= paddr + size - 1))
236 continue;
237found:
238 DBG("found area start %lx, end %lx\n", start, end);
239
240 if (omap_vram_create_allocation(rm, paddr, pages) == NULL)
241 return -ENOMEM;
242
243 return 0;
244 }
245
246 return -ENOMEM;
247}
248
249int omap_vram_reserve(unsigned long paddr, size_t size)
250{
251 unsigned pages;
252 int r;
253
254 DBG("reserve mem paddr %08lx size %d\n", paddr, size);
255
256 size = PAGE_ALIGN(size);
257 pages = size >> PAGE_SHIFT;
258
259 mutex_lock(&region_mutex);
260
261 r = _omap_vram_reserve(paddr, pages);
262
263 mutex_unlock(&region_mutex);
264
265 return r;
266}
267EXPORT_SYMBOL(omap_vram_reserve);
268
269static void _omap_vram_dma_cb(int lch, u16 ch_status, void *data)
270{
271 struct completion *compl = data;
272 complete(compl);
273}
274
275static int _omap_vram_clear(u32 paddr, unsigned pages)
276{
277 struct completion compl;
278 unsigned elem_count;
279 unsigned frame_count;
280 int r;
281 int lch;
282
283 init_completion(&compl);
284
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;
291 }
292
293 elem_count = pages * PAGE_SIZE / 4;
294 frame_count = 1;
295
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);
300
301 omap_set_dma_dest_params(lch, 0, OMAP_DMA_AMODE_POST_INC,
302 paddr, 0, 0);
303
304 omap_set_dma_color_mode(lch, OMAP_DMA_CONSTANT_FILL, 0x000000);
305
306 omap_start_dma(lch);
307
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;
313 }
314
315 r = 0;
316err:
317 omap_free_dma(lch);
318
319 return r;
320}
321
322static int _omap_vram_alloc(int mtype, unsigned pages, unsigned long *paddr)
323{
324 struct vram_region *rm;
325 struct vram_alloc *alloc;
326
327 list_for_each_entry(rm, &region_list, list) {
328 unsigned long start, end;
329
330 DBG("checking region %lx %d\n", rm->paddr, rm->pages);
331
332 if (region_mem_type(rm->paddr) != mtype)
333 continue;
334
335 start = rm->paddr;
336
337 list_for_each_entry(alloc, &rm->alloc_list, list) {
338 end = alloc->paddr;
339
340 if (end - start >= pages << PAGE_SHIFT)
341 goto found;
342
343 start = alloc->paddr + (alloc->pages << PAGE_SHIFT);
344 }
345
346 end = rm->paddr + (rm->pages << PAGE_SHIFT);
347found:
348 if (end - start < pages << PAGE_SHIFT)
349 continue;
350
351 DBG("found %lx, end %lx\n", start, end);
352
353 alloc = omap_vram_create_allocation(rm, start, pages);
354 if (alloc == NULL)
355 return -ENOMEM;
356
357 *paddr = start;
358
359 _omap_vram_clear(start, pages);
360
361 return 0;
362 }
363
364 return -ENOMEM;
365}
366
367int omap_vram_alloc(int mtype, size_t size, unsigned long *paddr)
368{
369 unsigned pages;
370 int r;
371
372 BUG_ON(mtype > OMAP_VRAM_MEMTYPE_MAX || !size);
373
374 DBG("alloc mem type %d size %d\n", mtype, size);
375
376 size = PAGE_ALIGN(size);
377 pages = size >> PAGE_SHIFT;
378
379 mutex_lock(&region_mutex);
380
381 r = _omap_vram_alloc(mtype, pages, paddr);
382
383 mutex_unlock(&region_mutex);
384
385 return r;
386}
387EXPORT_SYMBOL(omap_vram_alloc);
388
389void omap_vram_get_info(unsigned long *vram,
390 unsigned long *free_vram,
391 unsigned long *largest_free_block)
392{
393 struct vram_region *vr;
394 struct vram_alloc *va;
395
396 *vram = 0;
397 *free_vram = 0;
398 *largest_free_block = 0;
399
400 mutex_lock(&region_mutex);
401
402 list_for_each_entry(vr, &region_list, list) {
403 unsigned free;
404 unsigned long pa;
405
406 pa = vr->paddr;
407 *vram += vr->pages << PAGE_SHIFT;
408
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);
415 }
416
417 free = vr->paddr + (vr->pages << PAGE_SHIFT) - pa;
418 *free_vram += free;
419 if (free > *largest_free_block)
420 *largest_free_block = free;
421 }
422
423 mutex_unlock(&region_mutex);
424}
425EXPORT_SYMBOL(omap_vram_get_info);
426
427#if defined(CONFIG_DEBUG_FS)
428static int vram_debug_show(struct seq_file *s, void *unused)
429{
430 struct vram_region *vr;
431 struct vram_alloc *va;
432 unsigned size;
433
434 mutex_lock(&region_mutex);
435
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);
441
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);
447 }
448 }
449
450 mutex_unlock(&region_mutex);
451
452 return 0;
453}
454
455static int vram_debug_open(struct inode *inode, struct file *file)
456{
457 return single_open(file, vram_debug_show, inode->i_private);
458}
459
460static const struct file_operations vram_debug_fops = {
461 .open = vram_debug_open,
462 .read = seq_read,
463 .llseek = seq_lseek,
464 .release = single_release,
465};
466
467static int __init omap_vram_create_debugfs(void)
468{
469 struct dentry *d;
470
471 d = debugfs_create_file("vram", S_IRUGO, NULL,
472 NULL, &vram_debug_fops);
473 if (IS_ERR(d))
474 return PTR_ERR(d);
475
476 return 0;
477}
478#endif
479
480static __init int omap_vram_init(void)
481{
482 int i;
483
484 vram_initialized = 1;
485
486 for (i = 0; i < postponed_cnt; i++)
487 omap_vram_add_region(postponed_regions[i].paddr,
488 postponed_regions[i].size);
489
490#ifdef CONFIG_DEBUG_FS
491 if (omap_vram_create_debugfs())
492 pr_err("VRAM: Failed to create debugfs file\n");
493#endif
494
495 return 0;
496}
497
498arch_initcall(omap_vram_init);
499
500/* boottime vram alloc stuff */
501
502/* set from board file */
503static u32 omap_vram_sram_start __initdata;
504static u32 omap_vram_sram_size __initdata;
505
506/* set from board file */
507static u32 omap_vram_sdram_start __initdata;
508static u32 omap_vram_sdram_size __initdata;
509
510/* set from kernel cmdline */
511static u32 omap_vram_def_sdram_size __initdata;
512static u32 omap_vram_def_sdram_start __initdata;
513
Thomas Weber5c1f96f2010-03-03 09:16:54 +0100514static int __init omap_vram_early_vram(char *p)
Tomi Valkeinenafedec12009-08-07 12:01:55 +0300515{
Thomas Weber5c1f96f2010-03-03 09:16:54 +0100516 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;
Tomi Valkeinenafedec12009-08-07 12:01:55 +0300520}
Thomas Weber5c1f96f2010-03-03 09:16:54 +0100521early_param("vram", omap_vram_early_vram);
Tomi Valkeinenafedec12009-08-07 12:01:55 +0300522
523/*
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.
526 */
527void __init omap_vram_reserve_sdram(void)
528{
529 struct bootmem_data *bdata;
530 unsigned long sdram_start, sdram_size;
531 u32 paddr;
532 u32 size = 0;
533
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;
538 }
539
540 if (!size) {
541 size = omap_vram_sdram_size;
542 paddr = omap_vram_sdram_start;
543 }
544
545#ifdef CONFIG_OMAP2_VRAM_SIZE
546 if (!size) {
547 size = CONFIG_OMAP2_VRAM_SIZE * 1024 * 1024;
548 paddr = 0;
549 }
550#endif
551
552 if (!size)
553 return;
554
555 size = PAGE_ALIGN(size);
556
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;
560
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;
566 }
567
568 if (reserve_bootmem(paddr, size, BOOTMEM_EXCLUSIVE) < 0) {
569 pr_err("FB: failed to reserve VRAM\n");
570 return;
571 }
572 } else {
573 if (size > sdram_size) {
574 pr_err("Illegal SDRAM size for VRAM\n");
575 return;
576 }
577
578 paddr = virt_to_phys(alloc_bootmem_pages(size));
579 BUG_ON(paddr & ~PAGE_MASK);
580 }
581
582 omap_vram_add_region(paddr, size);
583
584 pr_info("Reserving %u bytes SDRAM for VRAM\n", size);
585}
586
587/*
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.
592 *
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.
596 */
597unsigned 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)
602{
603 unsigned long pend_avail;
604 unsigned long reserved;
605 u32 paddr;
606 u32 size;
607
608 paddr = omap_vram_sram_start;
609 size = omap_vram_sram_size;
610
611 if (!size)
612 return 0;
613
614 reserved = 0;
615 pend_avail = pstart_avail + size_avail;
616
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;
622 }
623 size_avail = (size_avail - size) & PAGE_MASK;
624 paddr = pstart_avail + size_avail;
625 }
626
627 if (paddr < sram_pstart ||
628 paddr + size > sram_pstart + sram_size) {
629 pr_err("Illegal SRAM region for VRAM\n");
630 return 0;
631 }
632
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;
637
638 omap_vram_add_region(paddr, size);
639
640 if (reserved)
641 pr_info("Reserving %lu bytes SRAM for VRAM\n", reserved);
642
643 return reserved;
644}
645
646void __init omap_vram_set_sdram_vram(u32 size, u32 start)
647{
648 omap_vram_sdram_start = start;
649 omap_vram_sdram_size = size;
650}
651
652void __init omap_vram_set_sram_vram(u32 size, u32 start)
653{
654 omap_vram_sram_start = start;
655 omap_vram_sram_size = size;
656}