LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 07-01-2011, 07:07 PM   #1
liny1984
LQ Newbie
 
Registered: Jun 2006
Distribution: openSUSE, Ubuntu
Posts: 23

Rep: Reputation: 0
Question Physical address being returned as 0 in mmap


Hi,

I have written a PCI driver for my device. The device is an FPGA which is configured with a design that allows it to have direct access to memory of a host computer to which the fpga board is connected. My device driver is responsible for translating virtual addresses to physical addresses and sending these to the FPGA so the DMA unit in the FPGA can directly access pages in memory using the physical address.

I want to send about 1 MB of data to the FPGA. I currently 1st mmap the required data file in my c program as follows :

//Open the bitstream file
int fd = open(filename, O_RDWR);
if (fd < 0){
perror("molen_set : Filename Open");
return;
}

//Get bitstream file size
fstat(fd, &buf);
length = buf.st_size;
if(length == 0){
fprintf(stderr, "\nmolen_set : Empty input file\n");
return;
}

//Map bitstream file to memory to read it in via page faults(faster than explicit file I/O)
bs_map = mmap(NULL, length, PROT_WRITE | PROT_READ, MAP_PRIVATE, fd, 0);

This allows the file to be read in via page faults.
Then I get a suitable area from the kernel using another mmap which is called on the PCI device :

//Get user space address of empty kernel memory of length bytes
arg = mmap(NULL, length, PROT_WRITE | PROT_READ, MAP_PRIVATE, htex_handle, 0);

Then I copy the data using memcpy and signal the PCI device to start reading the data using a IOCTL

The problem I have is in the mmap implementation in the PCI driver :

static int htex_mmap(struct file * file, struct vm_area_struct * vma)
{
vma->vm_flags |= VM_RESERVED;

if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
vma->vm_end - vma->vm_start,
vma->vm_page_prot))
return -EAGAIN;

vma->vm_ops = &htex_vm_ops;
htex_vma_open(vma);

return 0;
}

When the driver gets the above call then I assume the kernel has already allocated the required space and its details are in the vma parameter. When I try to view the physical address of the virtual address in the vma i use :

virt_to_phys(vma->vm_start)

But this gives me back the original address. A call like :
DEBUG_MSG("htex_mmap called start:%lx, end:%lx, size:%lx, pfn:%lx, pa:%lx\n",
vma->vm_start, vma->vm_end, vma->vm_end - vma->vm_start,
vma->vm_pgoff, virt_to_phys(vma->vm_start));

gives me

start:7f14b5afc000, end:7f14b5c0d000, size:111000, pfn:0, pa:f714b5afc000

I am able to get the page associated with vm_pgoff using :

struct page *pg = pfn_to_page(vma->vm_pgoff);
DEBUG_MSG("htex_mmap page: page address : %lx\n", page_address(pg));

which gives me something like :
page address :: ffff880000000000

But this does not appear to be the correct physical address.

So how can I get the physical address of the space that the kernel just allocated in response to the user's mmap() call on the device ?

I need the correct physical address later to send to the FPGA as well.
 
Old 07-01-2011, 07:18 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Hmm.... Kind of sounds like a pointer issue. Have you double checked your code for inconsistent pointers for specific addresses? I say it like that because if the physical address is not really the address you are hoping for, it may be looking else where instead of the location that you need, hence maybe you missed a pointer? I am just giving my two cents on your issue, that is all.

Cheers,

Josh
 
Old 07-01-2011, 08:21 PM   #3
liny1984
LQ Newbie
 
Registered: Jun 2006
Distribution: openSUSE, Ubuntu
Posts: 23

Original Poster
Rep: Reputation: 0
Well yes, the pointers i have used in my code are correct. It seems that perhaps I am not looking up the physical address in the right way.

virt_to_phys(vma->vm_start) seem to only work for kernel logical addresses and not virtual addresses.

struct page *pg = pfn_to_page(vma->vm_pgoff); may be returniing 0 because vma->vm_pgoff represents the offset of the area in the file, in pages. When a file or device is mapped, this is apparently the file position of the first page mapped in this area. So it should be 0

Which leaves the question, how would I translate a virtual address to physical address inside the driver's mmap() implementation ?

-----------


Well it seems that I need to use the DMA API as virt_to_phys() does not work on all platforms.

http://lxr.linux.no/#linux/Documenta...-API-HOWTO.txt

One thing I am not sure of though is , when my driver gets the mmap() call then has the
kernel already allocated memory and put its details in the vma
parameter or do I need to allocate memory myself (using dma API or whatever else)


I am already using the driver read and write functions to read/write
some other parameters to the FPGA, so I will setup the DMA in mmap()

Last edited by liny1984; 07-02-2011 at 10:31 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to translate virtual address to physical address in linux application saurin Programming 1 11-18-2009 09:05 PM
Question on using mmap to access physical memory Gnu2Linux2 Linux - Embedded & Single-board computer 1 10-05-2009 03:54 AM
Help needed: mmap and physical memory access manaav Linux - Newbie 3 03-25-2009 03:38 AM
mmap a physical address fardad Programming 3 10-30-2008 11:30 AM
Mmap and munmap physical address to user space karttik Linux - Software 0 10-28-2007 02:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 04:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration