Skip to contentSkip to conversation

Virtual framebuffer driver - mmap problem

DISCUSSION Shivappa Kushtagi 3 participants 4 messages Sep 8, 2004 – Sep 9, 2004 Framebuffer layer

lore
Share & tools
lore
#0 lore raw
Quoted 3 lines

I would like to use virtual framebuffer on a linux system which is installed with R.H.9 to simulate my target display ( an embedded device with 320*480 resolution).

== The virtual framebuffer is intended for testing _the kernel_ only.

Quoted 7 lines

I have written a application which will mmap /dev/fb0 and fill it with some RGB values. I run framebuffer based VNC server to see whatever I have written. I always get a white image. Looks like mmap is mapped to some other address and fbvnc server is reading from somewhere else.

Does the existing drivers/video/vfb.c code works ? or Do I need to tweak it ?

== You cannot mmap() the video memory of vfb.

== Why don"t you use vesafb?

As I had mentioned in my earlier post, I wanted to use virtual framebuffer to simulate the display of my target device (say some PDA with size 320*480 ). With vesafb, I will not be able to change the resolution ( fbset fails telling, invalid argument )

Is there any other framebuffer based methods to simulate a display ?

Regards
Shivappa

-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click

#1 lore raw
Quoted 4 lines · Shivappa Kushtagi

== You cannot mmap() the video memory of vfb.

== Why don"t you use vesafb?

Quoted 8 lines

As I had mentioned in my earlier post, I wanted to use virtual framebuffer to simulate the display of my target device (say some PDA with size 320*480 ). With vesafb, I will not be able to change the resolution ( fbset fails telling, invalid argument )

Is there any other framebuffer based methods to simulate a display ?

There are 2 problems with vfb.c that makes mmap fail:

1. uses vmalloc to allocate framebuffer memory
2. has its own fb_mmap hook which always return -EINVAL

If you want to really use vfb.c, you can try to modify vfb.c so it behaves like a normal, mmappable framebuffer.

A. First you need to change the framebuffer allocation method: Do not use vmalloc, since it doesn't give you linear memory. Instead use __get_free_pages.

videomemorysize = PAGE_SIZE << order;

So if PAGE_SIZE is 4096, to allocate 8192 bytes, use order = 1. Note that there is an upper limit to order, so keep videomemorysize small.

videomemory = __get_free_pages(GFP_KERNEL, order)

B. Advertise the framebuffer to userspace:

In 2.6:
info->fix.smem_len = videomemorysize; info->fix.smem_start = virt_to_phys(videomemory) & PAGE_MASK;

In 2.4, you can do the same, but do it in vfb_encode_fix().

C. Finally, create your own vfb_mmap() function. The important thing with this version of mmap is the use of the VM_RESERVED flag when dealing with system RAM. This flag is not necessary for actual framebuffers. You can copy the fb_mmap function in fbmem.c and modify it for vfb. For 2.6, you can use the example below.

Tony

static int vfb_mmap(struct fb_info *info, struct file *file,
struct vm_area_struct *vma)

{
	unsigned long off;
#if !defined(__sparc__) || defined(__sparc_v9__)
	unsigned long start;
	u32 len;

#endif

	if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
		return -EINVAL;
	off = vma->vm_pgoff << PAGE_SHIFT;
#if defined(__sparc__) && !defined(__sparc_v9__)
	/* Should never get here, all fb drivers should have their own
	   mmap routines */
	return -EINVAL;
#else
	/* !sparc32... */
	lock_kernel();

	/* frame buffer memory */
	start = info->fix.smem_start;
	len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.smem_len);
	if (off >= len) {
		/* memory mapped io */
		off -= len;
		if (info->var.accel_flags) {
			unlock_kernel();
			return -EINVAL;
		}
		start = info->fix.mmio_start;
		len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.mmio_len);
	}
	unlock_kernel();
	start &= PAGE_MASK;
	if ((vma->vm_end - vma->vm_start + off) > len)
		return -EINVAL;
	off += start;
	vma->vm_pgoff = off >> PAGE_SHIFT;
	/* This is an IO map - tell maydump to skip this VMA */
	vma->vm_flags |= VM_IO | VM_RESERVED;
#if defined(__sparc_v9__)
	vma->vm_flags |= (VM_SHM | VM_LOCKED);
	if (io_remap_page_range(vma, vma->vm_start, off,
				vma->vm_end - vma->vm_start, vma->vm_page_prot, 0))
		return -EAGAIN;
#else
#if defined(__mc68000__)
#if defined(CONFIG_SUN3)
	pgprot_val(vma->vm_page_prot) |= SUN3_PAGE_NOCACHE;
#elif defined(CONFIG_MMU)
	if (CPU_IS_020_OR_030)
		pgprot_val(vma->vm_page_prot) |= _PAGE_NOCACHE030;
	if (CPU_IS_040_OR_060) {
		pgprot_val(vma->vm_page_prot) &= _CACHEMASK040;
		/* Use no-cache mode, serialized */
		pgprot_val(vma->vm_page_prot) |= _PAGE_NOCACHE_S;
	}
#endif
#elif defined(__powerpc__)
	pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE|_PAGE_GUARDED;
#elif defined(__alpha__)
	/* Caching is off in the I/O space quadrant by design.  */
#elif defined(__i386__) || defined(__x86_64__)
	if (boot_cpu_data.x86 > 3)
		pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
#elif defined(__mips__)
	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
#elif defined(__hppa__)
	pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
#elif defined(__ia64__) || defined(__arm__) || defined(__sh__)
	vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
#else
#warning What do we have to do here??
#endif
	if (io_remap_page_range(vma, vma->vm_start, off,
			     vma->vm_end - vma->vm_start, vma->vm_page_prot))
		return -EAGAIN;
#endif /* !__sparc_v9__ */
	return 0;

#endif /* !sparc32 */
}

-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click

#2 lore raw
Quoted 22 lines · Antonino A. Daplas
Quoted 4 lines · Shivappa Kushtagi

== You cannot mmap() the video memory of vfb.

== Why don"t you use vesafb?

Quoted 8 lines

As I had mentioned in my earlier post, I wanted to use virtual framebuffer to simulate the display of my target device (say some PDA with size 320*480 ). With vesafb, I will not be able to change the resolution ( fbset fails telling, invalid argument )

Is there any other framebuffer based methods to simulate a display ?

There are 2 problems with vfb.c that makes mmap fail:

1. uses vmalloc to allocate framebuffer memory
2. has its own fb_mmap hook which always return -EINVAL

If you want to really use vfb.c, you can try to modify vfb.c so it behaves like a normal, mmappable framebuffer.

Quoted 8 lines

A. First you need to change the framebuffer allocation method: Do not use vmalloc, since it doesn't give you linear memory. Instead use __get_free_pages.

videomemorysize = PAGE_SIZE << order;

So if PAGE_SIZE is 4096, to allocate 8192 bytes, use order = 1. Note that there is an upper limit to order, so keep videomemorysize small.

And the upper limit is 128 kiB on most platforms. Hence even 640x480 in 8 bpp won't fit...

Probably you can make a fb_mmap() hook that's compatible with the vmalloc()'ed frame buffer and thus doesn't suffer from this limitation, but it will require more code and more knowledge of memory management internals.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click

#3 lore raw
Quoted 4 lines · Geert Uytterhoeven
Quoted 3 lines

So if PAGE_SIZE is 4096, to allocate 8192 bytes, use order = 1. Note that there is an upper limit to order, so keep videomemorysize small.

Quoted 8 lines

And the upper limit is 128 kiB on most platforms. Hence even 640x480 in 8 bpp won't fit...

Probably you can make a fb_mmap() hook that's compatible with the vmalloc()'ed frame buffer and thus doesn't suffer from this limitation, but it will require more code and more knowledge of memory management internals.

Actually, I discovered that MAX_ORDER in 2.4 is 10 (4MiB) and 11 in 2.6 (8MiB), so it's probably not as limiting as it seems. However, the probability of __get_free_pages() failing at such a high order is very high.

Tony

-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click