If I have only the physical address of device buffer (PCIe), how can I map this buffer to user-space?

前端 未结 2 1333
[愿得一人]
[愿得一人] 2021-01-27 08:10

If I have only the physical address of the memory buffer to which is mapped the device buffer via the PCI-Express BAR (Base Address Register), how can I map thi

2条回答
  •  时光说笑
    2021-01-27 08:54

    The Linux kernel, at least, versions 2.6.x use the ioremap() function.

    void *vaddr = ioremap (phys_addr, size_addr);
    if (vaddr) {
      /* do stuff with the memory using vaddr pointer */
      iounmap (vaddr);
    }
    

    You should make a previous call to request_mem_region() to check if that memory space is already reclaimed by another driver, and politely request that memory to be owned by your code (driver). The complete example should look like this:

    void *vaddr;
    if (request_mem_region (phys_addr, size_addr, "my_driver")) {
      vaddr = ioremap (phys_addr, size_addr);
      if (vaddr) {
        /* do stuff with the memory */
        iounmap (vaddr);
      }
      release_mem_region (phys_addr, size_addr);
    }
    

    You can check your ownership by checking /proc/iomem, which will reflect the address range and the owner of every piece of memory in your system.

    UPDATE: I don't really know if this works for 64-bit kernels. It does for 32-bit. If 64-bit kernel don't have these kernel functions, they will have similar ones, I guess.

提交回复
热议问题