目录
Background knowledge
Introduction ia pa va and memory addressing
Memory Addressing
Page Frame Management
Memory area management
DMA
IOVA and IOMMU
Introduce DMABUF
What is DMABUF
DMABUF 关键概念
DMABUF APIS –The Exporter
DMABUF APIS –The Importer
DMABUF APIS
DMABUF usage flow
代码和调试
Introduce ION
What is ION
ION Overview
ION关键概念
ION 配置
ION APIS
ION changes
ION与DMABUF的关联
代码和调试
Introduce Gralloc
What is gralloc
Gralloc 和 ION的关联
代码和调试
Gralloc ion DMABUF in Camera & display
相机架构简述
相机smmu
相机DMABUF调用栈分析
相机驱动内存管理实例分析
Display DMABUF调用栈分析
总结1
总结2
Background knowledge
Introduction ia pa va and memory addressing
ia(Intermediate Address):CPU指令构造的地址空间,指令的演进具有滞后性,分段需求诞生。中间地址,也称为物理中间地址(Physical Intermediate Address),是指 CPU 访问内存时使用的地址。IA 是 CPU 通过地址转换机制将 VA 转换为 PA 的中间步骤。
pa(Physical Address):物理地址总线发出的地址构成的地址空间。物理地址,是指 CPU 访问内存时实际使用的地址。PA 是 CPU 通过地址转换机制将 VA 转换为 PA 的最终结果。
va(Virtual Address):pa的空间过大后,引入页表,经过分页处理ia和pa不再一一对应,ia由此之后可称为va。虚拟地址,是指程序在运行时使用的地址。VA 是程序中使用的地址,与物理内存地址没有直接关系。
Memory Addressing
Page Frame Management
Memory area management
DMA
- DMA(Direct Memory Access,直接内存存取) 它允许不同速度的硬件装置来沟通,而不需要依赖于 CPU 的大量中断负载 。
- DMA 传输将数据从一个地址空间复制到另外一个地址空间。
- Reference : http://www.wowotech.net/memory_management/DMA-Mapping-api.html
IOVA and IOMMU
- DMA 最初只接受物理地址,设备地址长度比CPU总线长度短,分配低地址给设备使用
- 设备增强后可以自带页表将DMA地址转换为同等长度的物理地址
- 设备自带页表转换可以认为在IO上做了mmu转换,硬件上有了IOMMU的概念,arm上称为SMMU
- IOMMU 作用是连接DMA-capable I/O总线(Direct Memory Access-capable I/O Bus)和主存(main memory)
- DMA地址可以由此可称为IOVA
Introduce DMABUF
What is DMABUF
- A generic kernel level framework to share buffers.
- Defines a new buffer object, which provides mechanism for exporting and using shared buffers.
- Provides uniform APIs that allow various operations related to buffer sharing.
DMABUF 关键概念
- The exporter
- implements and manages operations in struct dma_buf_ops for the buffer,
- allows other users to share the buffer by using dma_buf sharing APIs,
- manages the details of buffer allocation, wrapped int a struct dma_buf,
- decides about the actual backing storage where this allocation happens,
- and takes care of any migration of scatterlist - for all (shared) users of this buffer.
- The buffer-user
- is one of (many) sharing users of the buffer.
- doesn’t need to worry about how the buffer is allocated, or where.
- and needs a mechanism to get access to the scatterlist that makes up this buffer in memory, mapped into its own address space, so it can access the same area of memory. This interface is provided by struct dma_buf_attachment.
DMABUF APIS –The Exporter
1、dma_buf_export() Used to export a buffer,Connects the exporter's private metadata for the buffer, an implementation of buffer operations for this buffer, and flags for the associated file. Returns a handle to the dma_buf object with all the above associated information.
2、dma_buf_fd() Returns a FD associated with the dma_buf object. Userspace then passes the FD to other devices & sub-systems participating in sharing this dma_buf object.
3、dma_buf_get() Used by importing device to get the dma_buf object associated with the FD
DMABUF APIS –The Importer
4、dma_buf_attach(): The importing device can attach itself with the dma_buf object. Called once at beginning of dma_buf object sharing.
5、dma_buf_map_attachment() Used by importing device to request access to the buffer so it can do DMA. Returns an sg_table, containing the scatterlist mapped in the importing device's address space.
6、dma_buf_unmap_attachment() Once the DMA access is done, the device tells the exporter that the currently requested access is completed by calling this API.
DMABUF APIS
7、dma_buf_detach() At the end of need to access this dma_buf object, the importer device tells the exporter of its intent to 'detach' from the current sharing.
8、dma_buf_put() After dma_buf_detach() is called, the reference count of this buffer is decremented by calling dma_buf_put().
DMABUF usage flow
代码和调试
- 代码路径:kernel/msm-4.14/drivers/dma-buf
- Debug:/sys/kernel/debug/dma_buf
- Reference: https://www.kernel.org/doc/html/latest/driver-api/dma-buf.html
Introduce ION
What is ION
- ION is a generalized memory manager that Google introduced in the Android 4.0 ICS (Ice Cream Sandwich) release to address the issue of fragmented memory management interfaces across different Android devices.
- 内存池管理器
ION Overview
ION关键概念
- ION device : metadata of the ion device node
- ION heap ops : ops to operate on a given heap
- ION heap : represents a heap in the system
- ION page pool: ION memory pool in heap
- ION buffer : metadata for a particular buffer
ION 配置
path: msm-4.14/arch/arm/boot/dts/qcom/sm8150-ion.dtsi
msm_ion_probe
Ion_device_create
Ion_heap_create
Ion_device_add_heap
ION APIS
- Ion_alloc_fd()
- ion_ioctl()
- Ion_map()
- Ion_free()
ION changes
ION与DMABUF的关联
代码和调试
Code :
- system/core/libion/
- kernel/msm-4.14/drivers/staging/android/ion/
Debug:
- /sys/kernel/debug/dma_buf
Introduce Gralloc
What is gralloc
- Gralloc is a vendor-supplied library, at run-time in /system/lib/hw/gralloc.[product name].so
- allocates graphic buffers
- gralloc allocates all graphic buffers using a kernel memory manager, typically ION
- Selects appropriate ION heap based on the buffer usage flags
Gralloc 和 ION的关联
代码和调试
code
gralloc0 gralloc1
hardware/libhardware/include/hardware/gralloc.h libhardware/modules/gralloc/
gralloc2
hardware/interfaces/graphics/allocator/ hardware/qcom/display/gralloc/