先看一下什么是IONION是google在Android4.0 ICS为了解决内存碎片管理而引入的通用内存管理器,它会更加融合kernel。目前QCOM MSM, NVDIA Tegra, TI OMAP, MRVL PXA都用ION替换PMEM。 ION与PMEM类似,管理一或多个内存池,其中有一些会在boot time的时候预先分配,以备给特殊的硬件使用(GPU,显示控制器等)。 它通过ION heaps来管理这些pool。它可以被userspace的process之间或者内核中的模块之间进行内存共享 ION 框架[1]ION 定义了四种不同的heap,实现不同的内存分配策略。
ION APIs用户空间 API定义了6种 ioctl 接口,可以与用户应用程序交互。
ION_IOC_SHARE 及ION_IOC_IMPORT是基于DMABUF实现的,所以当共享进程获取文件描述符后,可以直接调用mmap来操作共享内存。mmap实现由DMABUF子系统调用ION子系统中mmap回调函数完成。 内核空间 API内核驱动也可以注册为一个ION的客户端(client),可以选择使用哪种类型的heap来申请内存。
ion handle: 这里每个ion handle映射到一个buffer中,每个buffer关联一个heap。也就是说一个客户端可以操作多块buffer。 Buffer 申请及释放函数:
ION 通过handle来管理buffer,驱动需要可以访问到buffer的地址。ION通过下面的函数来达到这个目的
ION是通过handle而非buffer地址来实现驱动间共享内存,用户空间共享内存也是利用同样原理。
Heap APIHeap 接口定义 [drivers/gpu/ion/ion_priv.h] 这些接口不是暴露给驱动或者用户应用程序的。 /** * struct ion_heap_ops - ops to operate on a given heap * @allocate: allocate memory * @free: free memory * @phys get physical address of a buffer (only define on physically contiguous heaps) * @map_dma map the memory for dma to a scatterlist * @unmap_dma unmap the memory for dma * @map_kernel map memory to the kernel * @unmap_kernel unmap memory to the kernel * @map_user map memory to userspace */ struct ion_heap_ops { int (*allocate) (struct ion_heap *heap, struct ion_buffer *buffer, unsigned long len,unsigned long align, unsigned long flags); void (*free) (struct ion_buffer *buffer); int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer, ion_phys_addr_t *addr, size_t *len); struct scatterlist *(*map_dma) (struct ion_heap *heap, struct ion_buffer *buffer); void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer); void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer); void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer); int (*map_user) (struct ion_heap *mapper, struct ion_buffer *buffer, struct vm_area_struct *vma); }; ION debugION 在/sys/kernel/debug/ion/ 提供一个debugfs 接口。 每个heap都有自己的debugfs目录,client内存使用状况显示在/sys/kernel/debug/ion/<<heap name>> $cat /sys/kernel/debug/ion/ion-heap-1 client pid size test_ion 2890 16384 每个由pid标识的client也有一个debugfs目录/sys/kernel/debug/ion/<<pid>> $cat /sys/kernel/debug/ion/2890 heap_name: size_in_bytes ion-heap-1: 40960 11 Kernel Driver间如何共享memory kernel中使用ion_client_create来获得一个ion
client的句柄, userspace side ion通过/dev/ion设备来与用户侧的程序交互。userspace通过调用ioctl来进行内存的操作。 分配内存的ioctl 目前总有六个ioctl cmd提供给userspace
从中可以看出,DMABUF的适用面更广可以支持更多的平台,而ION目前只支持android。ION可以支持userspace端更方便的使用(这是android driver的通性),而DMABUF只有kernel端的API,通常只对driver/kernel开发者开放接口。 目前Linaro正在努力整合CMA和ION,以期能够更好的利用ION的用户接口来利用DMABUF的DMA Mapping功能。 |
|