Search code examples
linux-kernellinux-device-drivermemory-mappingpetalinux

mapping a memory region from kernel


I have a register which needs to be accessed from more then one driver. It is a global read-only register resides in FPGA space The register address is exported via device tree. The first call to "request_mem_region" is ok, but any consecutive call fails.

Is there a way to share a register between drivers ?

Linux Kernel release is 4.14 , using petalinux

Thanks, Ran


Solution

  • You need to remap the memory region with something like ioremap() after you have requested it.

    Then, as Tsyvarev and others mentioned, create and export a function in your "parent" driver that returns the mapped memory.

    Here is some rough code:

    void * mapped_mem;
    
    void * map_addr(unsigned int phy_addr, char * name) {
    
        struct resource * resource;
        void * mapped_mem;
    
        resource = request_mem_region(phy_addr, page_size * 4, name);
        // check for errors
    
        mapped_mem= ioremap_nocache(phy_addr, page_size * 4);
        // check for errors
        return mappedMem;
    
        //handle errors
    }
    
    
    void * get_mapped_addr(void) {
        return mapped_mem
    }
    
    EXPORT_SYMBOL( get_mapped_addr);
    

    Now, mapped_mem should actually be tracked as part of your devices private info, but I figure thats beyond the scope of the question. Also, make sure to check for all possible errors. Make sure that request_mem_region() returns something >0 and not Null.