tsm-core.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright(c) 2024-2025 Intel Corporation. All rights reserved. */
  3. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  4. #include <linux/tsm.h>
  5. #include <linux/device.h>
  6. #include <linux/module.h>
  7. #include <linux/cleanup.h>
  8. #include <linux/pci-tsm.h>
  9. static struct class *tsm_class;
  10. static DEFINE_IDA(tsm_ida);
  11. static int match_id(struct device *dev, const void *data)
  12. {
  13. struct tsm_dev *tsm_dev = container_of(dev, struct tsm_dev, dev);
  14. int id = *(const int *)data;
  15. return tsm_dev->id == id;
  16. }
  17. struct tsm_dev *find_tsm_dev(int id)
  18. {
  19. struct device *dev = class_find_device(tsm_class, NULL, &id, match_id);
  20. if (!dev)
  21. return NULL;
  22. return container_of(dev, struct tsm_dev, dev);
  23. }
  24. static struct tsm_dev *alloc_tsm_dev(struct device *parent)
  25. {
  26. struct device *dev;
  27. int id;
  28. struct tsm_dev *tsm_dev __free(kfree) =
  29. kzalloc_obj(*tsm_dev);
  30. if (!tsm_dev)
  31. return ERR_PTR(-ENOMEM);
  32. id = ida_alloc(&tsm_ida, GFP_KERNEL);
  33. if (id < 0)
  34. return ERR_PTR(id);
  35. tsm_dev->id = id;
  36. dev = &tsm_dev->dev;
  37. dev->parent = parent;
  38. dev->class = tsm_class;
  39. device_initialize(dev);
  40. return no_free_ptr(tsm_dev);
  41. }
  42. static struct tsm_dev *tsm_register_pci_or_reset(struct tsm_dev *tsm_dev,
  43. struct pci_tsm_ops *pci_ops)
  44. {
  45. int rc;
  46. if (!pci_ops)
  47. return tsm_dev;
  48. tsm_dev->pci_ops = pci_ops;
  49. rc = pci_tsm_register(tsm_dev);
  50. if (rc) {
  51. dev_err(tsm_dev->dev.parent,
  52. "PCI/TSM registration failure: %d\n", rc);
  53. device_unregister(&tsm_dev->dev);
  54. return ERR_PTR(rc);
  55. }
  56. /* Notify TSM userspace that PCI/TSM operations are now possible */
  57. kobject_uevent(&tsm_dev->dev.kobj, KOBJ_CHANGE);
  58. return tsm_dev;
  59. }
  60. struct tsm_dev *tsm_register(struct device *parent, struct pci_tsm_ops *pci_ops)
  61. {
  62. struct tsm_dev *tsm_dev __free(put_tsm_dev) = alloc_tsm_dev(parent);
  63. struct device *dev;
  64. int rc;
  65. if (IS_ERR(tsm_dev))
  66. return tsm_dev;
  67. dev = &tsm_dev->dev;
  68. rc = dev_set_name(dev, "tsm%d", tsm_dev->id);
  69. if (rc)
  70. return ERR_PTR(rc);
  71. rc = device_add(dev);
  72. if (rc)
  73. return ERR_PTR(rc);
  74. return tsm_register_pci_or_reset(no_free_ptr(tsm_dev), pci_ops);
  75. }
  76. EXPORT_SYMBOL_GPL(tsm_register);
  77. void tsm_unregister(struct tsm_dev *tsm_dev)
  78. {
  79. if (tsm_dev->pci_ops)
  80. pci_tsm_unregister(tsm_dev);
  81. device_unregister(&tsm_dev->dev);
  82. }
  83. EXPORT_SYMBOL_GPL(tsm_unregister);
  84. static void tsm_release(struct device *dev)
  85. {
  86. struct tsm_dev *tsm_dev = container_of(dev, typeof(*tsm_dev), dev);
  87. ida_free(&tsm_ida, tsm_dev->id);
  88. kfree(tsm_dev);
  89. }
  90. static int __init tsm_init(void)
  91. {
  92. tsm_class = class_create("tsm");
  93. if (IS_ERR(tsm_class))
  94. return PTR_ERR(tsm_class);
  95. tsm_class->dev_release = tsm_release;
  96. return 0;
  97. }
  98. module_init(tsm_init)
  99. static void __exit tsm_exit(void)
  100. {
  101. class_destroy(tsm_class);
  102. }
  103. module_exit(tsm_exit)
  104. MODULE_LICENSE("GPL");
  105. MODULE_DESCRIPTION("TEE Security Manager Class Device");