在Linux中分配物理内存缓冲区 [英] Allocating a physical memory buffer in linux

查看:326
本文介绍了在Linux中分配物理内存缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个同时具有DSP和ARM内核的SoC,我想创建一个共享内存部分,我的用户空间软件和DSP软件都可以访问.在Linux中分配这样的缓冲区的最佳方法是什么?这是一个小背景,现在我所拥有的是一个内核模块,其中使用kmalloc()获取内核缓冲区,然后使用asm/page.h中的__pa()宏获取我的内核缓冲区的物理地址.我将此地址另存为sysfs条目,以便我的用户空间代码可以获取此缓冲区的物理地址.然后,我可以将该地址写入DSP,以便知道共享内存的位置,也可以mmap /dev/mem或我自己的内核模块,以便可以从用户空间访问此缓冲区(我也可以使用read /write fileops).

I have an SoC which has both DSP and ARM cores on it and I would like to create a section of shared memory that both my userspace software, and DSP software are able to access. What would be the best way to allocate a buffer like this in Linux? Here is a little background, right now what I have is a kernel module in which I use kmalloc() to get a kernel buffer, I then use the __pa() macro from asm/page.h to get the physical address of my kernel buffer. I save this address as a sysfs entry so that my userspace code can get the physical address of this buffer. I can then write this address to the DSP so it knows where the shared memory location is, and I can also mmap /dev/mem or my own kernel module so that I can access this buffer from userspace (I could also use the read/write fileops).

出于某种原因,我觉得这太过分了,但是我找不到最佳方法来做自己想做的事情.

For some reason I feel like this is overboard but I cannot find the best way to do what I am trying to do.

是否可以仅mmap \dev\mem一段内存并只对该部分进行读写操作?我的感觉是,这不会从内核中锁定"该部分内存,因此内核仍然可以在我不知情的情况下对该内存进行读/写操作.是这种情况.阅读了LDD3的内存管理一章后,我看到mmap创建了映射的新VMA.这样会锁定此内存区域,以便其他进程不会被分配此内存区域吗?

Would it be possible to just mmap \dev\mem a section of memory and just read and write to this section? My feeling is that this would not 'lock' this section of memory from the kernel, thus the kernel could still read/write to this memory without me knowing. Is this the case. After reading the memory management chapter of LDD3 I see that mmap creates a new VMA of the mapping. Would this lock this area of memory so that other processes would not get allocated this section of memory?

任何人都将受到感激

推荐答案

根据所使用的DMA的类型,您需要使用dma_alloc_coherent()分配缓冲区,或者使用标准分配和dma_map_*函数. (您不得使用__pa();物理地址不必与DMA总线地址相同.)

Depending on the kind of DMA you're using, you need to allocate the buffer with dma_alloc_coherent(), or use standard allocations and the dma_map_* functions. (You must not use __pa(); physical addresses are not necessarily the same as DMA bus addresses.)

要将缓冲区映射到用户空间,请对连贯缓冲区使用dma_mmap_coherent(),或者对于流缓冲区手动使用内存页面进行映射.

To map the buffers to user space, use dma_mmap_coherent() for coherent buffers, or map the memory pages manually for streaming buffers.

这篇关于在Linux中分配物理内存缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆