AMD IOMMU与Linux (3) -- DMA

2023-05-16

Linux中DMA会使用硬件IOMMU如AMD IOMMU, INTEL VT-D, 也会使用软件的SWIOTLB

这篇梳理一下LINUX内核在有AMD IOMMU的情况下,是如何做DMA的,内容包括如下

1. struct iommu_ops amd_iommu_ops

2. struct dma_map_ops iommu_dma_ops

3. DMA struct dma_map_ops 与 struct iommu_ops的关系

    Consistent, Streaming

4. struct io_pgtable_ops, 及与struct iommu_ops amd_iommu_ops的关系

1. 两处会设置struct iommu_ops amd_iommu_ops;

const struct iommu_ops amd_iommu_ops = {
    .capable = amd_iommu_capable,
    .domain_alloc = amd_iommu_domain_alloc,  // 分配一个iommu_domain
    .domain_free  = amd_iommu_domain_free,
    .attach_dev = amd_iommu_attach_device, //针对独立设备(即所在Group里只有自己),将设备所在Group与domain进行绑定
    .detach_dev = amd_iommu_detach_device,
    .map = amd_iommu_map, //用于映射domain内的iova,将长度为sizeiova为起始地址的iova区域映射到以paddr为起始地址的物理地址。该函数只能用于UNMANAGED类型和DMA类型的domain
    .iotlb_sync_map    = amd_iommu_iotlb_sync_map,
    .unmap = amd_iommu_unmap,
    .iova_to_phys = amd_iommu_iova_to_phys, // 将iova转换成物理地址
    .probe_device = amd_iommu_probe_device,
    .release_device = amd_iommu_release_device,
    .probe_finalize = amd_iommu_probe_finalize,
    .device_group = amd_iommu_device_group,
    .get_resv_regions = amd_iommu_get_resv_regions,
    .put_resv_regions = generic_iommu_put_resv_regions,
    .is_attach_deferred = amd_iommu_is_attach_deferred,
    .pgsize_bitmap    = AMD_IOMMU_PGSIZES,
    .flush_iotlb_all = amd_iommu_flush_iotlb_all,
    .iotlb_sync = amd_iommu_iotlb_sync,
    .def_domain_type = amd_iommu_def_domain_type,
};

一处在struct iommu_device的iommu ops;

另一处在struct bus_type的iommu ops;

amd_iommu_init ->

        iommu_go_to_state ->

                state_next ->

                        amd_iommu_init_pci ->

                                iommu_init_pci ->

                                        iommu_device_register

struct iommu_device {
    struct list_head list;
    const struct iommu_ops *ops;
    struct fwnode_handle *fwnode;
    struct device *dev;
};

/*
 * Structure where we save information about one hardware AMD IOMMU in the
 * system.
 */

struct amd_iommu {

        ...

        struct iommu_device iommu;/* Handle for IOMMU core code */

        ...

}

amd_iommu_init ->

        iommu_go_to_state ->

                state_next ->

                        amd_iommu_init_pci ->

                                amd_iommu_init_api ->

                                        bus_set_iommu ->  //将自身挂入到 对应总线中

struct bus_type {

        ...

        const struct iommu_ops *iommu_ops;

        ...
};

2. 设置struct dma_map_ops iommu_dma_ops

amd_iommu_ops.probe_finalize = amd_iommu_probe_finalize ->

        iommu_setup_dma_ops->

                struct device *dev->dma_ops = &iommu_dma_ops;

struct device {
        ...
    const struct dma_map_ops *dma_ops; // DMA mapping operations for this device
        ...
};

static const struct dma_map_ops iommu_dma_ops = {
    .alloc            = iommu_dma_alloc,
    .free            = iommu_dma_free,
    .alloc_pages        = dma_common_alloc_pages,
    .free_pages        = dma_common_free_pages,
#ifdef CONFIG_DMA_REMAP
    .alloc_noncontiguous    = iommu_dma_alloc_noncontiguous,
    .free_noncontiguous    = iommu_dma_free_noncontiguous,
#endif
    .mmap            = iommu_dma_mmap,
    .get_sgtable        = iommu_dma_get_sgtable,
    .map_page        = iommu_dma_map_page,
    .unmap_page        = iommu_dma_unmap_page,
    .map_sg            = iommu_dma_map_sg,
    .unmap_sg        = iommu_dma_unmap_sg,
    .sync_single_for_cpu    = iommu_dma_sync_single_for_cpu,
    .sync_single_for_device    = iommu_dma_sync_single_for_device,
    .sync_sg_for_cpu    = iommu_dma_sync_sg_for_cpu,
    .sync_sg_for_device    = iommu_dma_sync_sg_for_device,
    .map_resource        = iommu_dma_map_resource,
    .unmap_resource        = iommu_dma_unmap_resource,
    .get_merge_boundary    = iommu_dma_get_merge_boundary,
};

iommu_dma_alloc ->

        void *cpu_addr = iommu_dma_alloc_pages

        dma_addr_t iova = __iommu_dma_map (-> iommu_dma_alloc_iova)

3. DMA -- struct dma_map_ops 与 struct iommu_ops

如果写过LINUX DMA驱动,会接触过以下几个函数:

Consistent

void * dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flag)

Streaming

dma_addr_t dma_map_single(struct device *dev, void *cpu_addr, size_t size, enum dma_data_direction direction)

dma_map_page

dma_map_sg

其中dma_alloc_coherent,的调用栈为:

dma_alloc_coherent ->

        dma_alloc_attrs ->

                struct dma_map_ops *ops->alloc == iommu_dma_alloc ->

                        __iommu_dma_map ->

                                iommu_map_atomic ->

                                        _iommu_map->

                                                __iommu_map->

                                                        __iommu_map_pages->

                                                                struct iommu_ops *ops->map_pages/map

这里的iommu_ops.map 在使用AMD IOMMU的时候,就是.map = amd_iommu_map,

                        

#define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
#define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
#define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
#define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
#define dma_map_page(d, p, o, s, r) dma_map_page_attrs(d, p, o, s, r, 0)
#define dma_unmap_page(d, a, s, r) dma_unmap_page_attrs(d, a, s, r, 0)

对应到dma_map_single的调用栈为:

dma_map_single_attrs->

        dma_map_page_attrs->

                struct dma_map_ops *ops->map_page == iommu_dma_map_page ->

                        __iommu_dma_map_swiotlb ->

                                __iommu_dma_map -> 

                                        之后与dma_alloc_coherent相同

                        

        

4.  struct io_pgtable_ops

amd_iommu_map最终的实现在struct io_pgtable_ops

amd_iommu_map ->

        struct io_pgtable_ops *ops->map

        

struct io_pgtable_ops {
    int (*map)(struct io_pgtable_ops *ops, unsigned long iova,
           phys_addr_t paddr, size_t size, int prot, gfp_t gfp);
    int (*map_pages)(struct io_pgtable_ops *ops, unsigned long iova,
             phys_addr_t paddr, size_t pgsize, size_t pgcount,
             int prot, gfp_t gfp, size_t *mapped);
    size_t (*unmap)(struct io_pgtable_ops *ops, unsigned long iova,
            size_t size, struct iommu_iotlb_gather *gather);
    size_t (*unmap_pages)(struct io_pgtable_ops *ops, unsigned long iova,
                  size_t pgsize, size_t pgcount,
                  struct iommu_iotlb_gather *gather);
    phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops,
                    unsigned long iova);
};

struct io_pgtable {
    enum io_pgtable_fmt    fmt;
    void            *cookie;
    struct io_pgtable_cfg    cfg;
    struct io_pgtable_ops    ops;
};

struct amd_io_pgtable {
    struct io_pgtable_cfg    pgtbl_cfg;
    struct io_pgtable    iop;
        ...
};

struct protection_domain {
        ...
    struct iommu_domain domain; /* generic domain handle used by
                       iommu core code */
    struct amd_io_pgtable iop;
        ...
};

struct amd_io_pgtable *pgtable

    pgtable->iop.ops.map          = iommu_v1_map_page; //maps a physical address into a DMA
address space. It allocates the page table pages if necessary, 建立DMA addr与paddr的page table

    pgtable->iop.ops.unmap        = iommu_v1_unmap_page;
    pgtable->iop.ops.iova_to_phys = iommu_v1_iova_to_phys;

5. Summary

系统中的调用关系如下:

struct dma_map_ops iommu_dma_ops ->

        struct iommu_ops amd_iommu_op ->

                struct io_pgtable_ops

iommu是实现在dma mapping api下层的驱动,所以我们只需要使用dma mapping的相关api,不需要直接调用iommu接口

AMD IOMMU驱动实现了自己的struct io_pgtable_ops

类似在内核中的还有ARM SMMU与Apple DART等,详见include/linux/io-pgtable.h文件

Reference:

[1] 

​​​​​​dma_map_ops 实现的三种方式_jason的笔记-CSDN博客_dma map​​​​​​

[2]

​​​​​​kernel是如何选择iommu的呢?_jason的笔记-CSDN博客[

[3]

iommu_dma_mmap + mmap - tycoon3 - 博客园 (cnblogs.com)

【4】

Documentation/core-api/dma-api-howto.rst

Documentation/core-api/dma-api.rst

Notes:

1. 处于同一个domain中的设备使用同一套映射做地址转换, 就是独立的页表

2. Group中default_domain和domain的概念:domain指group当前所在的domain,而default_domain指Group默认应该在的domain

进行attach操作时,会检查default_domain是否与domain相同,以此判断该Group是否已经attach到别的domain上了

如果Group有自己的default_domain,那么该函数iommu_detach_device在detach完成之后会重新attach到default_domain上

3. PCIE是一个点对点的协议,如果一个多function设备挂到了一个不支持ACS的bridge下,那么这两个function可以通过该bridge进行通信。这样的通信直接由bridge进行转发而无需通过Root Complex,自然也就无需通过IOMMU。这种情况下,这两个function的IOVA无法完全通过IOMMU隔离开,所以他们需要分到同一个Group中。同一个Group的设备应该是共用一个domain的

4. 每一个domain即代表一个iommu映射地址空间,即一个page table。一个Group逻辑上是需要与domain进行绑定的,即一个Group中的所有设备都位于一个domain中

Questions:

The difference between IOMMU_DOMAIN_UNMANAGED  & IOMMU_DOMAIN_DMA?  

/*
 * This are the possible domain-types
 *
 *    IOMMU_DOMAIN_BLOCKED    - All DMA is blocked, can be used to isolate
 *                  devices
 *    IOMMU_DOMAIN_IDENTITY    - DMA addresses are system physical addresses
 *    IOMMU_DOMAIN_UNMANAGED    - DMA mappings managed by IOMMU-API user, used
 *                  for VMs
 *    IOMMU_DOMAIN_DMA    - Internally used for DMA-API implementations.
 *                  This flag allows IOMMU drivers to implement
 *                  certain optimizations for these domains
 *    IOMMU_DOMAIN_DMA_FQ    - As above, but definitely using batched TLB
 *                  invalidation.
 */

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AMD IOMMU与Linux (3) -- DMA 的相关文章

  • Tomcat Intellij Idea:远程部署

    RackSpace 云服务器 Ubuntu 12 04 Intellij Idea 11 1 2 Windows 8 Tomcat 7 0 26 JDK 6 在 Intellij Idea 上 当我尝试在远程 Tomcat 7 服务器上运行
  • 批量删除文件名中包含 BASH 中特殊字符的子字符串

    我的目录中有一个文件列表 opencv calib3d so2410 so opencv contrib so2410 so opencv core so2410 so opencv features2d so2410 so opencv
  • 仅使用containerd(不使用Docker)修剪容器镜像

    如果我刚刚containerd安装在 Linux 系统上 即 Docker 是not安装 如何删除未使用的容器映像以节省磁盘空间 Docker 就是这么方便docker system prune https docs docker com
  • 有没有一种快速方法可以从 Jar/war 中删除文件,而无需提取 jar 并重新创建它?

    所以我需要从 jar war 文件中删除一个文件 我希望有类似 jar d myjar jar file I donot need txt 的内容 但现在我能看到从 Linux 命令行执行此操作的唯一方法 不使用 WinRAR Winzip
  • 为什么 Linux 没有 DirectX API?

    在考虑现代显卡的 Windows 系统上 DirectX API 的驱动程序端实现时 我想知道为什么此实现在非 Windows 系统 尤其是 Linux 上不可用 由于明显缺乏此功能 我只能假设有一个我无视的充分理由 但在我的原始理解中 我
  • 在 C 中使用单个消息队列是否可以实现双向通信

    我希望服务器向客户端发送一些消息 并让客户端确认它 我被分配了这个任务 我可以在 C linux 中使用单个消息队列来完成它还是我需要创建两个 谢谢 是的 可以使用 sysV 消息队列来做到这一点 从您之前的问题来看 您正在使用该队列 您可
  • 在centos中安装sqlite3 dev和其他包

    我正在尝试使用 cpanel 在 centos 机器上安装 sqlite dev 和其他库 以便能够编译应用程序 我对 debian 比 centos 更熟悉 我知道我需要的库是 libsqlite3 dev libkrb5 dev lib
  • C 语言的符号表

    我目前正在开发一种执行模式匹配的静态分析工具 我在用Flex https github com westes flex生成词法分析器 我编写了代码来管理符号表 我不太有经验C 所以我决定将符号表实现为线性链表 include
  • 尽管 if 语句,Visual Studio 仍尝试包含 Linux 标头

    我正在尝试创建一个强大的头文件 无需更改即可在 Windows 和 Linux 上进行编译 为此 我的包含内容中有一个 if 语句 如下所示 if defined WINDOWS include
  • 并行运行 shell 脚本

    我有一个 shell 脚本 打乱大型文本文件 600 万行和 6 列 根据第一列对文件进行排序 输出 1000 个文件 所以伪代码看起来像这样 file1 sh bin bash for i in seq 1 1000 do Generat
  • 使用 shell 脚本将行附加到 /etc/hosts 文件

    我有一个新的 Ubuntu 12 04 VPS 我正在尝试编写一个安装脚本来完成整个 LAMP 安装 我遇到问题的地方是在 etc hosts文件 我当前的主机文件如下所示 127 0 0 1 localhost Venus The fol
  • 使用包管理器时如何管理 Perl 模块?

    A 最近的问题 https stackoverflow com questions 397817 unable to find perl modules in intrepid ibex ubuntu这让我开始思考 在我尝试过的大多数 Li
  • .net-core:ILDASM / ILASM 的等效项

    net core 是否有相当于 ILDASM ILASM 的功能 具体来说 我正在寻找在 Linux 上运行的东西 因此为什么是 net core ildasm 和 ilasm 工具都是使用此存储库中的 CoreCLR 构建的 https
  • 与 pthread 的进程间互斥

    我想使用一个互斥体 它将用于同步对两个不同进程共享的内存中驻留的某些变量的访问 我怎样才能做到这一点 执行该操作的代码示例将非常感激 以下示例演示了 Pthread 进程间互斥体的创建 使用和销毁 将示例推广到多个进程作为读者的练习 inc
  • 配置tomat的server.xml文件并自动生成mod_jk.conf

    我在用apache 2 2 15 and tomcat6 6 0 24 on CentOS 6 4并希望使用 tomcat 服务器的功能 通过添加以下内容自动生成 mod jk conf 文件
  • Linux 为一组进程保留一个处理器(动态)

    有没有办法将处理器排除在正常调度之外 也就是说 使用sched setaffinity我可以指示线程应该在哪个处理器上运行 但我正在寻找相反的情况 也就是说 我想从正常调度中排除给定的处理器 以便只有已明确调度的进程才能在那里运行 我还知道
  • 检查已安装的软件包,如果没有找到则安装

    我需要检查已安装的软件包 如果未安装则安装它们 RHEL CentOS Fedora 示例 rpm qa grep glibc static glibc static 2 12 1 80 el6 3 5 i686 如何在 BASH 中进行检
  • 从 Linux 内核模块中调用用户空间函数

    我正在编写一个简单的 Linux 字符设备驱动程序 以通过 I O 端口将数据输出到硬件 我有一个执行浮点运算的函数来计算硬件的正确输出 不幸的是 这意味着我需要将此函数保留在用户空间中 因为 Linux 内核不能很好地处理浮点运算 这是设
  • ansible unarchive 模块如何查找 tar 二进制文件?

    我正在尝试执行一个 ansible 剧本 该剧本的任务是利用unarchive模块 因为我是在 OSX 上执行此操作 所以我需要使用它gnu tar 而不是bsd tar通常与 OSX 一起提供 因为BSD tar 不受官方支持 https
  • 无法显示 Laravel 欢迎页面

    我的服务器位于 DigitalOcean 云上 我正在使用 Ubuntu 和 Apache Web 服务器 我的家用计算机运行的是 Windows 7 我使用 putty 作为终端 遵循所有指示https laracasts com ser

随机推荐