public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Mapping PCI memory to user-space
@ 2007-10-24  6:59 joncglenn
  2007-10-24  7:52 ` Jiri Slaby
  2007-10-28  3:06 ` Roland Dreier
  0 siblings, 2 replies; 3+ messages in thread
From: joncglenn @ 2007-10-24  6:59 UTC (permalink / raw)
  To: linux-kernel


I am writing a driver to map a PCI board memory space (pcibar2) into a
user-space vma via 'mmap'.  What is the relationship between the address
returned from ioremap and the type of address needed in the
'io_remap_page_range' or 'remap_pfn_range' functions?  How about the
following? (I am developing under RHEL4 and a 2.6.9 kernel)

In the 'init' part of the driver:

     dev.pcibar2 = ioremap_nocache(resource,size);
     dev.region_start = dev.pcibar2 + offset;     // RAM is at some offset
from base
     dev.region_size  = <some size>

In the mydriver_mmap function:

static ssize_t mydriver_mmap (struct file *filp,
                                                    struct vm_area_struct
*vma)
{
  // off             = convert vm_pgoff back to user-space mmap 'off' value
  // phyaddr   = physical address of PCI memory area
  // vsize         = total size of area user wants to map
  // psize         = total avail size in device
  struct mydriver_dev *dev = filp->private_data;
  unsigned long off              = vma->vm_pgoff << PAGE_SHIFT;
  unsigned long phy            = __pa(dev->region_start + off);
  unsigned long vsize          = vma->vm_end - vma->vm_start;
  unsigned long psize          = dev->region_size - off;

  if (vsize > psize)
     return -EINVAL; /* spans too high */
  if (io_remap_page_range(vma, phyaddr, vma->vm_start, vsize,
vma->vm_page_prot))
     return -EAGAIN;

  vma->vm_ops    = &mydriver_vm_ops;
  vma->vm_flags |= VM_IO | VM_RESERVED;
  mydriver_vma_open(vma);

  return 0;
}

-- 
View this message in context: http://www.nabble.com/Mapping-PCI-memory-to-user-space-tf4682416.html#a13380086
Sent from the linux-kernel mailing list archive at Nabble.com.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Mapping PCI memory to user-space
  2007-10-24  6:59 Mapping PCI memory to user-space joncglenn
@ 2007-10-24  7:52 ` Jiri Slaby
  2007-10-28  3:06 ` Roland Dreier
  1 sibling, 0 replies; 3+ messages in thread
From: Jiri Slaby @ 2007-10-24  7:52 UTC (permalink / raw)
  To: joncglenn; +Cc: linux-kernel

On 10/24/2007 08:59 AM, joncglenn wrote:
> I am writing a driver to map a PCI board memory space (pcibar2) into a
> user-space vma via 'mmap'.  What is the relationship between the address
> returned from ioremap and the type of address needed in the
> 'io_remap_page_range' or 'remap_pfn_range' functions?  How about the
> following? (I am developing under RHEL4 and a 2.6.9 kernel)

I think you can use the method used for exporting pci resources in /sys. See
pci_mmap_resource. Don't know if this was yet in 2.6.9...

regards,
-- 
Jiri Slaby (jirislaby@gmail.com)
Faculty of Informatics, Masaryk University

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Mapping PCI memory to user-space
  2007-10-24  6:59 Mapping PCI memory to user-space joncglenn
  2007-10-24  7:52 ` Jiri Slaby
@ 2007-10-28  3:06 ` Roland Dreier
  1 sibling, 0 replies; 3+ messages in thread
From: Roland Dreier @ 2007-10-28  3:06 UTC (permalink / raw)
  To: joncglenn; +Cc: linux-kernel

 > I am writing a driver to map a PCI board memory space (pcibar2) into a
 > user-space vma via 'mmap'.  What is the relationship between the address
 > returned from ioremap and the type of address needed in the
 > 'io_remap_page_range' or 'remap_pfn_range' functions?  How about the
 > following? (I am developing under RHEL4 and a 2.6.9 kernel)

There is no relationship between the address returned from ioremap and
what you pass into io_remap_page_range().  ioremap gives you a kernel
virtual address for the PCI address you remap.  io_remap_page_range()
creates a userspace mapping in the same way, and you should pass in
the PCI address exactly the same way you pass in the PCI address into
ioremap.  io_remap_pfn_range() takes a PFN ("page frame number"),
which is basically the PCI address you want to map divided by
PAGE_SIZE.  The main reason for using PFNs is that they allow you to
map addresses above 4G even if sizeof long is only 4.

In your code:

 >      dev.pcibar2 = ioremap_nocache(resource,size);
 >      dev.region_start = dev.pcibar2 + offset;     // RAM is at some offset from base

This gives you a kernel mapping that you can use with readl(),
writel() etc to access the PCI memory from the kernel.

To map to userspace, this:

 >   if (io_remap_page_range(vma, phyaddr, vma->vm_start, vsize, vma->vm_page_prot))

should use phyaddr as you have it here:

 >   // phyaddr   = physical address of PCI memory area

This is just wrong:

 >   unsigned long phy            = __pa(dev->region_start + off);

__pa() doesn't work on addresses returned from ioremap.  Just use the
same resource address you passed into ioremap.

 - R.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-10-28  3:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-24  6:59 Mapping PCI memory to user-space joncglenn
2007-10-24  7:52 ` Jiri Slaby
2007-10-28  3:06 ` Roland Dreier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox