简体   繁体   English

如何将kmalloc()地址转换为物理地址

[英]How to convert kmalloc() address to physical address

I have a PCI device which exposes a BAR and few offsets in the bar for accessing the device. 我有一个PCI设备,该设备公开了BAR,并且在访问设备的栏中几乎没有偏移。 On one of the Bar offset, i need to program a 64KB allocated memory. 在小节偏移之一上,我需要对64KB分配的内存进行编程。 In my Linux driver, i allocate a 64KB of memory using kmalloc() which as i know returns virtual address. 在我的Linux驱动程序中,我使用kmalloc()分配了64KB的内存,据我所知返回了虚拟地址。 If this is programmed into the offset, HW won't be able to see the same. 如果将此设置为偏移量,则HW将无法看到相同的内容。 How do i convert this virtual address to physical ? 如何将该虚拟地址转换为物理地址? When i Google, i see few links pointing to virt_to_phys() but few responses says this doesn't work well with kmalloc(). 当我使用Google时,我看不到指向virt_to_phys()的链接,但很少有响应说这与kmalloc()不能很好地配合。 Any idea how to go about this? 任何想法如何去做?

You normally use pci_resource_start() / pci_resource_end() from within a kernel driver. 通常,您可以从内核驱动程序中使用pci_resource_start()/ pci_resource_end()。 I assume you are writing a device driver ? 我假设您正在编写设备驱动程序?

I won't map the memory yourself : That's where the kernel functions are for. 我不会自己映射内存:这就是内核功能所在的位置。 That way, you're sure it works on all platforms. 这样,您确定它可以在所有平台上运行。 That 64k block I assume is some memory map that is provided by the PCI device ? 我认为该64k块是PCI设备提供的一些内存映射? if yes, then the above is correct. 如果是,则以上是正确的。 If no, please give more information. 如果否,请提供更多信息。

Rather than using kmalloc() , use alloc_pages() function. 与其使用kmalloc(),不如使用alloc_pages()函数。

struct page* alloc_pages(gfp_t gfp_mask, 4 ); struct page * alloc_pages(gfp_t gfp_mask,4);

one page is of 4K, so it will allocate 2^4=16 pages which is equal to 16 * 4K = 64K memory and returns physical address. 一页是4K,因此它将分配2 ^ 4 = 16页,这等于16 * 4K = 64K内存,并返回物理地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM