iommu-sysfs.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * IOMMU sysfs class support
  3. *
  4. * Copyright (C) 2014 Red Hat, Inc. All rights reserved.
  5. * Author: Alex Williamson <alex.williamson@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/device.h>
  12. #include <linux/iommu.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. /*
  16. * We provide a common class "devices" group which initially has no attributes.
  17. * As devices are added to the IOMMU, we'll add links to the group.
  18. */
  19. static struct attribute *devices_attr[] = {
  20. NULL,
  21. };
  22. static const struct attribute_group iommu_devices_attr_group = {
  23. .name = "devices",
  24. .attrs = devices_attr,
  25. };
  26. static const struct attribute_group *iommu_dev_groups[] = {
  27. &iommu_devices_attr_group,
  28. NULL,
  29. };
  30. static void iommu_release_device(struct device *dev)
  31. {
  32. kfree(dev);
  33. }
  34. static struct class iommu_class = {
  35. .name = "iommu",
  36. .dev_release = iommu_release_device,
  37. .dev_groups = iommu_dev_groups,
  38. };
  39. static int __init iommu_dev_init(void)
  40. {
  41. return class_register(&iommu_class);
  42. }
  43. postcore_initcall(iommu_dev_init);
  44. /*
  45. * Init the struct device for the IOMMU. IOMMU specific attributes can
  46. * be provided as an attribute group, allowing a unique namespace per
  47. * IOMMU type.
  48. */
  49. int iommu_device_sysfs_add(struct iommu_device *iommu,
  50. struct device *parent,
  51. const struct attribute_group **groups,
  52. const char *fmt, ...)
  53. {
  54. va_list vargs;
  55. int ret;
  56. device_initialize(&iommu->dev);
  57. iommu->dev.class = &iommu_class;
  58. iommu->dev.parent = parent;
  59. iommu->dev.groups = groups;
  60. va_start(vargs, fmt);
  61. ret = kobject_set_name_vargs(&iommu->dev.kobj, fmt, vargs);
  62. va_end(vargs);
  63. if (ret)
  64. goto error;
  65. ret = device_add(&iommu->dev);
  66. if (ret)
  67. goto error;
  68. return 0;
  69. error:
  70. put_device(&iommu->dev);
  71. return ret;
  72. }
  73. void iommu_device_sysfs_remove(struct iommu_device *iommu)
  74. {
  75. device_unregister(&iommu->dev);
  76. }
  77. /*
  78. * IOMMU drivers can indicate a device is managed by a given IOMMU using
  79. * this interface. A link to the device will be created in the "devices"
  80. * directory of the IOMMU device in sysfs and an "iommu" link will be
  81. * created under the linked device, pointing back at the IOMMU device.
  82. */
  83. int iommu_device_link(struct iommu_device *iommu, struct device *link)
  84. {
  85. int ret;
  86. if (!iommu || IS_ERR(iommu))
  87. return -ENODEV;
  88. ret = sysfs_add_link_to_group(&iommu->dev.kobj, "devices",
  89. &link->kobj, dev_name(link));
  90. if (ret)
  91. return ret;
  92. ret = sysfs_create_link_nowarn(&link->kobj, &iommu->dev.kobj, "iommu");
  93. if (ret)
  94. sysfs_remove_link_from_group(&iommu->dev.kobj, "devices",
  95. dev_name(link));
  96. return ret;
  97. }
  98. void iommu_device_unlink(struct iommu_device *iommu, struct device *link)
  99. {
  100. if (!iommu || IS_ERR(iommu))
  101. return;
  102. sysfs_remove_link(&link->kobj, "iommu");
  103. sysfs_remove_link_from_group(&iommu->dev.kobj, "devices", dev_name(link));
  104. }