linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Phil Nitschke <Phil.Nitschke@avalon.com.au>
To: Kumar Gala <galak@kernel.crashing.org>
Cc: linuxppc-embedded@ozlabs.org
Subject: Re: Memory mapping PCI memory region to user space
Date: Wed, 29 Mar 2006 12:56:03 +1030	[thread overview]
Message-ID: <1143599163.24304.37.camel@lamorak.int.avalon.com.au> (raw)
In-Reply-To: <43A30583-5E99-46A8-B419-1AEAB8440840@kernel.crashing.org>

[-- Attachment #1: Type: text/plain, Size: 1960 bytes --]

On Mon, 2006-03-27 at 10:18 -0600, Kumar Gala wrote:
> On Mar 27, 2006, at 2:02 AM, Phil Nitschke wrote:
> 
> > On Thu, 2006-03-23 at 09:44 -0600, Kumar Gala wrote:
> >> On Mar 23, 2006, at 8:21 AM, Wyse, Chris wrote:
> >>
> >>> Hi,
> >>>
> >>> I'm trying to map a PCI memory region 1 into user space from my
> >>> driver (PPC440GX, Linux 2.6.10).  Here's the mmap routine of the
> >>> driver that I'm using:
> >>
> >> Why don't use the mmap file exposed by sysfs so you dont have to
> >> write your own code?
> >>
> >> See Documentation/filesystems/sysfs-pci.txt.  But effectively down
> >> under /sys/bus/pci/devices/[domain:bus:dev:func]/ you will get
> >> resource[0..N-1] that corresponds to each BAR on the device.  This is
> >> a mmap file to access that region.
> >
> > I have some custom hardware that appears on the PCI bus as follows:
> >
> > bash-3.00# lspci -vv
> > 00:01.0 Class 0680: 1172:0004 (rev 01)
> > 	Control: I/O+ Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr-
> > Stepping- SERR- FastB2B-
> > 	Status: Cap- 66Mhz+ UDF- FastB2B+ ParErr- DEVSEL=medium >TAbort+
> > <TAbort- <MAbort- >SERR- <PERR-
> > 	Latency: 128, Cache Line Size 08
> > 	Interrupt: pin A routed to IRQ 71
> > 	Region 0: Memory at 000000009ffff000 (32-bit, non-prefetchable)
> > [size=4K]
> > 	Region 1: Memory at 000000009fc00000 (32-bit, non-prefetchable)
> > [size=2M]
> >
> > But when I try to access resource0 or resource1, I get a read error.
> > What characteristic of the device or driver determines whether it will
> > allow mmap-ing?
> >
> > (I've written the driver for this device myself.)
> 
> Nothing special beyond normal unix perms on the resource[0..n] files  
> to my knowledge.  When you say you get a read error what exactly does  
> that mean?

It means I had a bug in my program which read (mmap()ed) the resouce :-(
It is fixed now (see below).  Thanks for the tip.

-- 
Phil Nitschke <Phil.Nitschke@avalon.com.au>
Avalon Systems Pty Ltd



[-- Attachment #2: mmap.c --]
[-- Type: text/x-csrc, Size: 1009 bytes --]

#include <assert.h>
#include <byteswap.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/mman.h>
#include <unistd.h>

int
main (int argc, char **argv)
{
  /* Define constants for specific to the Base Address Register(s)
   * (defining the PCI I/O Address Region) for our hardware. */
  const uint32_t BAR_LENGTH = 0x200000;
  const uint32_t CTRL_REGS  =  0x80000;
  const uint32_t CTRL_LEN   =     0x30;

  int i, fd;
  uint32_t *ptr;

  fd = open("/sys/bus/pci/devices/0000:00:01.0/resource1", O_RDWR);
  ptr = (uint32_t*) mmap(NULL, BAR_LENGTH, PROT_READ|PROT_WRITE,
			 MAP_SHARED, fd, 0);
  // If ptr == -1, then the user was probably not root
  assert(ptr != MAP_FAILED);

  printf ("Mapped %p bytes beginning at address %p\n", BAR_LENGTH, ptr);

  for (i = CTRL_REGS; i < (CTRL_REGS + CTRL_LEN); i += 4)
    printf ("mmap[0x%08x] (%p) = 0x%08x [0x%08x]\n",
	    i, ptr + i/4, *(ptr + i/4), bswap_32(*(ptr + i/4)));

  munmap(ptr, BAR_LENGTH);
  close(fd);
}

  reply	other threads:[~2006-03-29  2:26 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-23 14:21 Memory mapping PCI memory region to user space Wyse, Chris
2006-03-23 15:44 ` Kumar Gala
2006-03-23 17:12   ` David Hawkins
2006-03-23 17:19     ` Kumar Gala
2006-03-23 17:43       ` Mark Chambers
2006-03-23 17:54         ` David Hawkins
2006-03-23 19:55           ` Mark Chambers
2006-03-23 20:26             ` David Hawkins
2006-03-23 17:46       ` David Hawkins
2006-03-27  8:02   ` Phil Nitschke
2006-03-27 16:05     ` David Hawkins
2006-03-28  4:21       ` Phil Nitschke
2006-03-28  4:55         ` David Hawkins
2006-03-28  6:44           ` Phil Nitschke
2006-03-28 16:35             ` David Hawkins
2006-03-27 16:18     ` Kumar Gala
2006-03-29  2:26       ` Phil Nitschke [this message]
2006-03-23 17:04 ` David Hawkins
  -- strict thread matches above, loose matches on Subject: below --
2006-03-23 19:52 Wyse, Chris
2006-03-23 20:01 ` Kumar Gala

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1143599163.24304.37.camel@lamorak.int.avalon.com.au \
    --to=phil.nitschke@avalon.com.au \
    --cc=galak@kernel.crashing.org \
    --cc=linuxppc-embedded@ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).