mmap problem in device driver and application program.

8 messages, 5 authors, 2008-05-26 · open the first message on its own page

mmap problem in device driver and application program.

From: MingLiu <hidden>
Date: 2008-05-08 15:33:08

Dear all,
I had some problem when I use mmap() file operation in the device driver file operation. My situation can be described as:
 
1. I reserve 2MB DDR memory as a look-up-table (LUT) for my customized device. The physical start address in the memory is 0x03000000 and the size is 2MB. 
 
2. In the device driver, I use mmap file operation to make this memory area directly accessible by application programs. In the mmap file operation, it looks like:
 
  if(remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, vma->vm_end - vma->vm_start, vma->vm_page_prot)){     return -EAGAIN;   }
 
3. In the application program, I read from a file and initialize the LUT area. I use the following sentence to mmap the physical address into a virtual one which could be accessed by the application:
 
  // the physical address is from 0x03000000 with a size of LUT_SIZE_IN_BYTE equal to 2MB
  lut_mem_base = (unsigned int *) mmap(0, LUT_SIZE_IN_BYTE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0x03000000);
 
Then, I initialize the LUT area with the virtual address specified by lut_mem_base. 
 
  for(i=0;;i++){
    fread(&buf, sizeof(unsigned int), 1, fp);
    *(lut_mem_base + i) = buf;
    if(feop(fp)){
       break;
    }
  }
 
4. After the initialization of the LUT memory area, in the device driver, DMA transfer will be enabled using the physical address of 0x03000000 as the source address. The destination is a register in my customized device. The data is fed from the memory to my device register. 
 
HOWEVER, I found that the data fed to the device register is not the same as the one used to initialize the LUT memory. Maybe the DMA fetches wrong data from different addresses and feeds to my device. Since this is my first time to use mmap, I am not self-confident to my program, both in the device driver and in the application. Is there anything wrong with my mmap part? I will appreciate so much if some experts could give me some hints. Thanks in advance.
 
BR
Ming
 
 
 
 
 
 
_________________________________________________________________
用手机MSN聊天写邮件看空间,无限沟通,分享精彩!
http://mobile.msn.com.cn/

Re: mmap problem in device driver and application program.

From: Dave Cogley <hidden>
Date: 2008-05-08 17:40:53

The address you are passing to mmap is where the pointer will be mapped
in virtual address space. You need to determine the DMA memory address
page number down in the actual mmap call.

int foo_mmap(struct file* filep, struct vm_area_struct* vma)
{
	unsigned long dma_addr = 0x03000000;
	unsigned long dma_size = 0x00200000;
	unsigned long pfn;

	// convert the DMA address to page number
	pfn = virt_to_phys(dma_addr) >> PAGE_SHIFT;

	// remap our page frame to the vma offset
	remap_pfn_range(vma, vma->vm_start, pfn, 
		dma_size, vma->vm_page_prot);
}

Change the address parameter from 0x03000000 to 0:

lut_mem_base = (unsigned int *) mmap(0, LUT_SIZE_IN_BYTE, 
	PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);


Dave

On Thu, 2008-05-08 at 15:33 +0000, MingLiu wrote:
Dear all,
I had some problem when I use mmap() file operation in the device
driver file operation. My situation can be described as:
 
1. I reserve 2MB DDR memory as a look-up-table (LUT) for my customized
device. The physical start address in the memory is 0x03000000 and the
size is 2MB. 
 
2. In the device driver, I use mmap file operation to make this memory
area directly accessible by application programs. In the mmap file
operation, it looks like:
 
  if(remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, vma->vm_end -
vma->vm_start, vma->vm_page_prot)){
     return -EAGAIN;
   }
 
3. In the application program, I read from a file and initialize the
LUT area. I use the following sentence to mmap the physical address
into a virtual one which could be accessed by the application:
 
  // the physical address is from 0x03000000 with a size of
LUT_SIZE_IN_BYTE equal to 2MB
  lut_mem_base = (unsigned int *) mmap(0, LUT_SIZE_IN_BYTE, PROT_READ
| PROT_WRITE, MAP_SHARED, fd, 0x03000000);
 
Then, I initialize the LUT area with the virtual address specified by
lut_mem_base. 
 
  for(i=0;;i++){
    fread(&buf, sizeof(unsigned int), 1, fp);
    *(lut_mem_base + i) = buf;
    if(feop(fp)){
       break;
    }
  }
 
4. After the initialization of the LUT memory area, in the device
driver, DMA transfer will be enabled using the physical address of
0x03000000 as the source address. The destination is a register in my

28 more quoted lines

RE: mmap problem in device driver and application program.

From: MingLiu <hidden>
Date: 2008-05-08 18:42:12

Dear Dave,
 
Thanks for your answer first. However I am very confused. It seems that I understand the memory-mapping-related functions completely wrong. Here comes more questions. > The address you are passing to mmap is where the pointer will be mapped> in virtual address space. You need to determine the DMA memory address> page number down in the actual mmap call.> > int foo_mmap(struct file* filep, struct vm_area_struct* vma)> {> unsigned long dma_addr = 0x03000000;> unsigned long dma_size = 0x00200000;> unsigned long pfn;> > // convert the DMA address to page number> pfn = virt_to_phys(dma_addr) >> PAGE_SHIFT;
Shall I use virt_to_phys? dma_addr is already exactly physical address which is used to initiate DMA transfers. 
 
// remap our page frame to the vma offset> remap_pfn_range(vma, vma->vm_start, pfn, > dma_size, vma->vm_page_prot);> }> > Change the address parameter from 0x03000000 to 0:> > lut_mem_base = (unsigned int *) mmap(0, LUT_SIZE_IN_BYTE, > PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Previously I used "remap_page_range(vma, vma->vm_start, physical_addr, vm_size, vma->vm_page_prot)" in the device driver, where physical_addr is 0x03000000. In the application program, I used "lut_mem_base = (unsigned int *) mmap(0, LUT_SIZE_IN_BYTE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);". I think it is correct but it doesn't work. I heard that in the new kernels, remap_page_range is not supported any more. However when I compile the driver, nothing was shown to complain that. I am using 2.6.10 kernel. Any hint for this?
 
Thank you for your help to make me understand. 
 
BR
Ming
_________________________________________________________________
Windows Live Photo gallery 数码相机的超级伴侣,轻松管理和编辑照片,还能制作全景美图!
http://get.live.cn/product/photo.html

RE: mmap problem in device driver and application program.

From: Dave Cogley <hidden>
Date: 2008-05-09 20:55:54

Shall I use virt_to_phys? dma_addr is already exactly physical address
which is used to initiate DMA transfers. 
Is the physical memory outside of the reach of the kernel? If it is
under control of the kernel memory management you will need to ioremap
it. This will return a kernel virtual address which will need to be
converted to a physical address and further convert to a page number.
remap_pfn_range only works on page numbers not physical addresses.

So I guess it would look something like this:

int foo_mmap(struct file* filep, struct vm_area_struct* vma)
{
	unsigned long dma_addr = ioremap(0x03000000);
	unsigned long dma_size = 0x00200000;
	unsigned long pfn;

	// convert the DMA address to page number
	pfn = virt_to_phys(dma_addr) >> PAGE_SHIFT;

	...
}

If you are using memory that is not under the kernel's memory management
you will need to use something other than remap_pfn_range.

Dave

On Thu, 2008-05-08 at 18:42 +0000, MingLiu wrote:
Dear Dave,
 
Thanks for your answer first. However I am very confused. It seems
that I understand the memory-mapping-related functions completely
wrong. Here comes more questions. 

quoted
The address you are passing to mmap is where the pointer will be
mapped
quoted
in virtual address space. You need to determine the DMA memory
address
quoted
page number down in the actual mmap call.

int foo_mmap(struct file* filep, struct vm_area_struct* vma)
{
unsigned long dma_addr = 0x03000000;
unsigned long dma_size = 0x00200000;
unsigned long pfn;

// convert the DMA address to page number
pfn = virt_to_phys(dma_addr) >> PAGE_SHIFT;
Shall I use virt_to_phys? dma_addr is already exactly physical address
which is used to initiate DMA transfers. 
 
quoted
// remap our page frame to the vma offset
remap_pfn_range(vma, vma->vm_start, pfn, 
dma_size, vma->vm_page_prot);
}

Change the address parameter from 0x03000000 to 0:

lut_mem_base = (unsigned int *) mmap(0, LUT_SIZE_IN_BYTE, 
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Previously I used "remap_page_range(vma, vma->vm_start, physical_addr,
vm_size, vma->vm_page_prot)" in the device driver, where physical_addr
is 0x03000000. In the application program, I used "lut_mem_base =
(unsigned int *) mmap(0, LUT_SIZE_IN_BYTE, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);". I think it is correct but it doesn't work. I

12 more quoted lines

RE: mmap problem in device driver and application program.

From: MingLiu <hidden>
Date: 2008-05-13 09:11:52

Dear Dave,
If you are using memory that is not under the kernel's memory management> you will need to use something other than remap_pfn_range.> 
Happenly and unfortunately we are using the memory space out of the control from the kernel. We have 2GB memory on the board and only 64(or 32) MB is reserved for the kernel. The rest large space is for the data buffering or LUT use. In my question, the LUT memory is located in the rest part rather than the kernel memory area. 
 
Any more suggestion on how to mmap this LUT area? Any idea will be valuable for a beginner like me. Thanks in advance. 
 
BR
Ming
_________________________________________________________________
新年换新颜,快来妆扮自己的MSN给心仪的TA一个惊喜!
http://im.live.cn/emoticons/?ID=18

RE: mmap problem in device driver and application program.

From: Howard, Marc <hidden>
Date: 2008-05-13 18:31:38

 

	Dear Dave,
	
	> If you are using memory that is not under the kernel's memory management
	> you will need to use something other than remap_pfn_range.
	> 
	
	Happenly and unfortunately we are using the memory space out of the control from the kernel. We have 2GB memory on the board and only 64(or 32) MB is reserved for the kernel. The rest large space is for the data buffering or LUT use. In my question, the LUT memory is located in the rest part rather than the kernel memory area. 
	 
	Any more suggestion on how to mmap this LUT area? Any idea will be valuable for a beginner like me. Thanks in advance. 
	 
	BR
	Ming
	
	
________________________________

	“七件武器,七种完美” 立刻体验! <http://get.live.cn>   

I have a similar setup where the upper 64MB are reserved for hardware.  The mmap implementation in my device driver works just fine using io_remap_page_range().
 
Marc Howard
 

Re: mmap problem in device driver and application program.

From: Johan Borkhuis <hidden>
Date: 2008-05-14 06:28:13

MingLiu wrote:
Dear Dave,
  
quoted
If you are using memory that is not under the kernel's memory management> you will need to use something other than remap_pfn_range.> 
    
Happenly and unfortunately we are using the memory space out of the control from the kernel. We have 2GB memory on the board and only 64(or 32) MB is reserved for the kernel. The rest large space is for the data buffering or LUT use. In my question, the LUT memory is located in the rest part rather than the kernel memory area. 
 
Any more suggestion on how to mmap this LUT area? Any idea will be valuable for a beginner like me. Thanks in advance. 
  
If this is physical memory that is not controlled by the kernel you
could also use the /dev/mem device. You could do something like:

memFd = open("/dev/mem", O_RDWR);
memBuf = mmap(0, <Memory size>, PROT_READ | PROT_WRITE, MAP_SHARED,
memFd, <Physical address of memory);

which maps the physical memory into user space, without the need for a
special device driver or mmap implementation.

Kind regards,
Johan Borkhuis

RE: mmap problem in device driver and application program.

From: Misbah khan <hidden>
Date: 2008-05-26 10:08:46

Hi ...

I guess you can remap the memory region outside the kernel control and then
convert it into page and use it into your mmap implementation in the driver
else it is easy to use the same in application by opening /dev/mmap file=20

regards=20
Misbah <><


mliu wrote:
=20
=20
Dear Dave,
quoted
If you are using memory that is not under the kernel's memory management=
quoted
you will need to use something other than remap_pfn_range.>=20
Happenly and unfortunately we are using the memory space out of the
control from the kernel. We have 2GB memory on the board and only 64(or
32) MB is reserved for the kernel. The rest large space is for the data
buffering or LUT use. In my question, the LUT memory is located in the
rest part rather than the kernel memory area.=20
=20
Any more suggestion on how to mmap this LUT area? Any idea will be
valuable for a beginner like me. Thanks in advance.=20
=20
BR
Ming
_________________________________________________________________
=E6=96=B0=E5=B9=B4=E6=8D=A2=E6=96=B0=E9=A2=9C=EF=BC=8C=E5=BF=AB=E6=9D=A5=
=E5=A6=86=E6=89=AE=E8=87=AA=E5=B7=B1=E7=9A=84MSN=E7=BB=99=E5=BF=83=E4=BB=AA=
=E7=9A=84TA=E4=B8=80=E4=B8=AA=E6=83=8A=E5=96=9C=EF=BC=81
http://im.live.cn/emoticons/?ID=3D18
_______________________________________________
Linuxppc-embedded mailing list
Linuxppc-embedded@ozlabs.org
https://ozlabs.org/mailman/listinfo/linuxppc-embedded
=20
--=20
View this message in context: http://www.nabble.com/mmap-problem-in-device-=
driver-and-application-program.-tp17130303p17469128.html
Sent from the linuxppc-embedded mailing list archive at Nabble.com.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help