iommu-sysfs.c 3.0 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. * Create an IOMMU device and return a pointer to it. IOMMU specific
  46. * attributes can be provided as an attribute group, allowing a unique
  47. * namespace per IOMMU type.
  48. */
  49. struct device *iommu_device_create(struct device *parent, void *drvdata,
  50. const struct attribute_group **groups,
  51. const char *fmt, ...)
  52. {
  53. struct device *dev;
  54. va_list vargs;
  55. int ret;
  56. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  57. if (!dev)
  58. return ERR_PTR(-ENOMEM);
  59. device_initialize(dev);
  60. dev->class = &iommu_class;
  61. dev->parent = parent;
  62. dev->groups = groups;
  63. dev_set_drvdata(dev, drvdata);
  64. va_start(vargs, fmt);
  65. ret = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
  66. va_end(vargs);
  67. if (ret)
  68. goto error;
  69. ret = device_add(dev);
  70. if (ret)
  71. goto error;
  72. return dev;
  73. error:
  74. put_device(dev);
  75. return ERR_PTR(ret);
  76. }
  77. void iommu_device_destroy(struct device *dev)
  78. {
  79. if (!dev || IS_ERR(dev))
  80. return;
  81. device_unregister(dev);
  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 device *dev, struct device *link)
  90. {
  91. int ret;
  92. if (!dev || IS_ERR(dev))
  93. return -ENODEV;
  94. ret = sysfs_add_link_to_group(&dev->kobj, "devices",
  95. &link->kobj, dev_name(link));
  96. if (ret)
  97. return ret;
  98. ret = sysfs_create_link_nowarn(&link->kobj, &dev->kobj, "iommu");
  99. if (ret)
  100. sysfs_remove_link_from_group(&dev->kobj, "devices",
  101. dev_name(link));
  102. return ret;
  103. }
  104. void iommu_device_unlink(struct device *dev, struct device *link)
  105. {
  106. if (!dev || IS_ERR(dev))
  107. return;
  108. sysfs_remove_link(&link->kobj, "iommu");
  109. sysfs_remove_link_from_group(&dev->kobj, "devices", dev_name(link));
  110. }