check for IOMMU support on linux [closed]

谁都会走 提交于 2019-12-05 05:51:30

Since 2014 enabled iommu are registered in /sys (sysfs) special file system as class iommu (documented at ABI/testing/sysfs-class-iommu): https://patchwork.kernel.org/patch/4345491/ "[2/3] iommu/intel: Make use of IOMMU sysfs support" - June 12, 2014

Register our DRHD IOMMUs, cross link devices, and provide a base set of attributes for the IOMMU. ... On a typical desktop system, this provides the following (pruned):

$ find /sys | grep dmar
/sys/devices/virtual/iommu/dmar0
...
/sys/class/iommu/dmar0
/sys/class/iommu/dmar1

The code is iommu_device_create (http://elixir.free-electrons.com/linux/v4.5/ident/iommu_device_create, around 4.5) or iommu_device_sysfs_add (http://elixir.free-electrons.com/linux/v4.11/ident/iommu_device_sysfs_add) in more recent kernels.

/*
 * Create an IOMMU device and return a pointer to it.  IOMMU specific
 * attributes can be provided as an attribute group, allowing a unique
 * namespace per IOMMU type.
 */
struct device *iommu_device_create(struct device *parent, void *drvdata,
                   const struct attribute_group **groups,
                   const char *fmt, ...)

Registration is done only for enabled IOMMU. DMAR:

if (intel_iommu_enabled) {
    iommu->iommu_dev = iommu_device_create(NULL, iommu,
                           intel_iommu_groups,
                           "%s", iommu->name);

AMD IOMMU:

static int iommu_init_pci(struct amd_iommu *iommu)
{ ...
    if (!iommu->dev)
        return -ENODEV;
...
    iommu->iommu_dev = iommu_device_create(&iommu->dev->dev, iommu,
                           amd_iommu_groups, "ivhd%d",
                           iommu->index);

Intel:

int __init intel_iommu_init(void)
{ ...
    pr_info("Intel(R) Virtualization Technology for Directed I/O\n");
...
    for_each_active_iommu(iommu, drhd)
        iommu->iommu_dev = iommu_device_create(NULL, iommu,
                               intel_iommu_groups,
                               "%s", iommu->name);

With 4.11 linux kernel version iommu_device_sysfs_add is referenced in many IOMMU drivers, so checking /sys/class/iommu is better (more universal) way to programmatically detect enabled IOMMU than parsing dmesg output or searching in /var/log/kern.log or /var/log/messages for driver-specific enable messages:

Referenced in 10 files:

  • drivers/iommu/amd_iommu_init.c, line 1640
  • drivers/iommu/arm-smmu-v3.c, line 2709
  • drivers/iommu/arm-smmu.c, line 2163
  • drivers/iommu/dmar.c, line 1083
  • drivers/iommu/exynos-iommu.c, line 623
  • drivers/iommu/intel-iommu.c, line 4878
  • drivers/iommu/iommu-sysfs.c, line 57
  • drivers/iommu/msm_iommu.c, line 797
  • drivers/iommu/mtk_iommu.c, line 581
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!