本文将总结下Linux swiotlb技术。

1. 概述

2. bounce buffer


If the requested DMA operation is a DMA read (the device reads from memory DMA_TO_DEVICE), the data is copied from the original buffer to the bounce buffer, and the adapter reads it from the bounce buffer’s memory location. If the requested DMA operation is a write, the data is written(the device writes to memory DMA_FROM_DEVICE) by the adapter to the bounce buffer, and then copied to the original buffer.


3. 总结

理解mmap时、同步时、unmap时的内存copy即可。
为了简单起见,只讨论DMA_FROM_DEVICE与DMA_TO_DEVICE这两个case。

  1. mmap时,建立原始物理地址与swiotlb buffer的映射,对于DMA_TO_DEVICE,需要将数据从原始物理地址处拷贝到swiotlb buffer
  2. 当设备往swiotlb buffer写入后,driver CPU需要读DMA内存时,需要sync下,将数据从swiotlb buffer拷贝到原始物理地址处
  3. driver CPU更改原始物理地址内存,当需要DMA_TO_DEVICE时,需要sync下,将数据从原始物理地址处拷贝到swiotlb buffer
  4. unmap时,解除原始物理地址与swiotlb buffer的映射,对于DMA_FROM_DEVICE,需要将数据从swiotlb buffer拷贝到原始物理地址处

参考资料:

  1. Utilizing IOMMUs for Virtualization in Linux and Xen
  2. Linux swiotlb技术解析
  3. Linux x86-64 IOMMU详解(二)——SWIOTLB(软件IOMMU)