tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

How to map PCI memory to user space ?



 Hi

I am trying to map PCI device memory  using mmap()  to user space for
powerpc based platform. I came across previous email thread where it
is mentioned that it is possible to mmap PCI memory using pcimmap()
implemented in dev/pci/pci_usrreq.c.

I wrote a small application to map the the PCI memory but mmap()
always return "invalid argument". The example is given below.  Does
mmap() using pcimmap() works ? Is there a sample code that illustrates
how to map PCI memory using mmap(). ?

static int memfd = -1;

int     main(int, char *[]);

static void *

mapmem(int offset, int len)

{

        void *base;



        if (memfd == -1)

                memfd = open("/dev/pci8", O_RDWR, 0);

        if (memfd < 0) {

                 warn("open /dev/pci8");

                 return (0);



        }

        base = mmap(0, len, PROT_READ , MAP_SHARED,

                            memfd, offset);

        if (base == (void *)-1) {

                warn("mmap %x-%x", offset, offset + len - 1);

                return (0);

        }

        return (base);

}


 int

main(int argc, char *argv[])

{

    uint32_t *bar0_virt_addr = 0;


   bar_virt_addr = (uint32_t *)mapmem(0xa0000000,4096);

   printf("bar0_virt_addr %p \n",bar0_virt_addr);

     if(bar0_virt_addr)

    {

         printf ("reg@offset = %lu \n", 0x0,(unsigned long
int)*(bar0_virt_addr + 0x0));
    }



    return 0;

}


Thanks

Rohan


Home | Main Index | Thread Index | Old Index