UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Adventurer
Adventurer
11,134 Views
Registered: ‎10-21-2013

UIO mmap() fails

Jump to solution

I am learning user space driver technology. 

Ther are three GPIOs in the FPGA project. I can read and write the GPIOs by devmem in Linux:

 

> devmem 0x41200000 32
0x00000005

>

 

I see three uioX files in /dev and three uioX folders in /sys/class/uio.

 

Then  I try to use UIO-based access. When I do mmap()  the error "Invalid argument" (in C++ program) or "Permission denied" (in C program) appears.

 

I use Zynq ZC706 board, Linux 3.19.0-xilinx.

 

My device tree is:

dip0: gpio_dip_sw@41200000 { 
               compatible = "generic-uio","uio";
               reg = <0x41200000 0x10>;
               text_data = "GPIO_DIP_SWITCH"; //for test
               bin_data=<12>; //for test
};

key0: gpio_key@41210000 {
               compatible = "generic-uio","uio";
               reg = <0x41210000 0x10>;
};

led0: gpio_led@41220000 {
               compatible = "generic-uio","uio";
               reg = <0x41220000 0x10>;
}; 

My program code is:

C++

      fd=::open(filename,O_RDWR);
      if(fd<0)
        return 4;

       mapped_base_address = mmap(0/*NULL*/, memory_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,0/*base_address*/ );
       if(mapped_base_address==MAP_FAILED)
         {perror("mmap");
    	   return 5;
         }

C

fd_led=open(filename,O_RDONLY);
      if(fd_led<0)
        return 1;

	base_address = mmap(NULL, 0x10, PROT_READ | PROT_WRITE, MAP_SHARED, fd_led, 0);
    if(base_address==MAP_FAILED)
      {perror("mmap");
      }

 

Why???

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
Adventurer
Adventurer
20,408 Views
Registered: ‎10-21-2013

Re: UIO mmap() fails

Jump to solution

The trouble is in device's memory range. It must be at least 0x1000.

Now all works fine.

 

		key0: gpio_key@41210000 {
                        compatible = "generic-uio","uio";
                        reg = <0x41210000 0x1000>;
                };

0 Kudos
3 Replies
Scholar rfs613
Scholar
11,094 Views
Registered: ‎05-28-2013

Re: UIO mmap() fails

Jump to solution

So devmem tells you that the hardware is working okay. Also your device tree entries for UIO look reasonable, although the size field (0x10) will get rounded up to page size (4096).

 

You could check contents of /sys/class/uio/ui0/maps/map0/size. This should give you the number of bytes of the region, should match what you put in the device tree.

 

One difference between your C++ and C code is the permissions on the open() call. If you open the file read-only, you will get permission error when you ask for PROT_WRITE in the mmap() call.

 

Are you running the program as root user?

 

void *do_map(void)
{
        int size = 4096;
        int fd;
        void *mio;

        if ((fd = open("/dev/uio0", O_RDWR)) == -1)
                perror("open uio0\n");

        mio = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        if (mio == MAP_FAILED)
                perror("mmap failed\n");

        return mio;
}
0 Kudos
Adventurer
Adventurer
20,409 Views
Registered: ‎10-21-2013

Re: UIO mmap() fails

Jump to solution

The trouble is in device's memory range. It must be at least 0x1000.

Now all works fine.

 

		key0: gpio_key@41210000 {
                        compatible = "generic-uio","uio";
                        reg = <0x41210000 0x1000>;
                };

0 Kudos
Observer rijuxavier
Observer
7,958 Views
Registered: ‎06-07-2016

Re: UIO mmap() fails

Jump to solution

I am trying to achieve similar thing.  Did you use any generic design file for the PL?  

0 Kudos