iommu-sysfs.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. iommu->dev = kzalloc(sizeof(*iommu->dev), GFP_KERNEL);
  57. if (!iommu->dev)
  58. return -ENOMEM;
  59. device_initialize(iommu->dev);
  60. iommu->dev->class = &iommu_class;
  61. iommu->dev->parent = parent;
  62. iommu->dev->groups = groups;
  63. va_start(vargs, fmt);
  64. ret = kobject_set_name_vargs(&iommu->dev->kobj, fmt, vargs);
  65. va_end(vargs);
  66. if (ret)
  67. goto error;
  68. ret = device_add(iommu->dev);
  69. if (ret)
  70. goto error;
  71. dev_set_drvdata(iommu->dev, iommu);
  72. return 0;
  73. error:
  74. put_device(iommu->dev);
  75. return ret;
  76. }
  77. void iommu_device_sysfs_remove(struct iommu_device *iommu)
  78. {
  79. dev_set_drvdata(iommu->dev, NULL);
  80. device_unregister(iommu->dev);
  81. iommu->dev = NULL;
  82. }
  83. /*
  84. * IOMMU drivers can indicate a device is managed by a given IOMMU using
  85. * this interface. A link to the device will be created in the "devices"
  86. * directory of the IOMMU device in sysfs and an "iommu" link will be
  87. * created under the linked device, pointing back at the IOMMU device.
  88. */
  89. int iommu_device_link(struct iommu_device *iommu, struct device *link)
  90. {
  91. int ret;
  92. if (!iommu || IS_ERR(iommu))
  93. return -ENODEV;
  94. ret = sysfs_add_link_to_group(&iommu->dev->kobj, "devices",
  95. &link->kobj, dev_name(link));
  96. if (ret)
  97. return ret;
  98. ret = sysfs_create_link_nowarn(&link->kobj, &iommu->dev->kobj, "iommu");
  99. if (ret)
  100. sysfs_remove_link_from_group(&iommu->dev->kobj, "devices",
  101. dev_name(link));
  102. return ret;
  103. }
  104. void iommu_device_unlink(struct iommu_device *iommu, struct device *link)
  105. {
  106. if (!iommu || IS_ERR(iommu))
  107. return;
  108. sysfs_remove_link(&link->kobj, "iommu");
  109. sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", dev_name(link));
  110. }