base.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2014 IBM Corp.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/rcupdate.h>
  7. #include <asm/errno.h>
  8. #include <misc/cxl-base.h>
  9. #include <linux/of_platform.h>
  10. #include "cxl.h"
  11. /* protected by rcu */
  12. static struct cxl_calls *cxl_calls;
  13. atomic_t cxl_use_count = ATOMIC_INIT(0);
  14. EXPORT_SYMBOL(cxl_use_count);
  15. #ifdef CONFIG_CXL_MODULE
  16. static inline struct cxl_calls *cxl_calls_get(void)
  17. {
  18. struct cxl_calls *calls = NULL;
  19. rcu_read_lock();
  20. calls = rcu_dereference(cxl_calls);
  21. if (calls && !try_module_get(calls->owner))
  22. calls = NULL;
  23. rcu_read_unlock();
  24. return calls;
  25. }
  26. static inline void cxl_calls_put(struct cxl_calls *calls)
  27. {
  28. BUG_ON(calls != cxl_calls);
  29. /* we don't need to rcu this, as we hold a reference to the module */
  30. module_put(cxl_calls->owner);
  31. }
  32. #else /* !defined CONFIG_CXL_MODULE */
  33. static inline struct cxl_calls *cxl_calls_get(void)
  34. {
  35. return cxl_calls;
  36. }
  37. static inline void cxl_calls_put(struct cxl_calls *calls) { }
  38. #endif /* CONFIG_CXL_MODULE */
  39. /* AFU refcount management */
  40. struct cxl_afu *cxl_afu_get(struct cxl_afu *afu)
  41. {
  42. return (get_device(&afu->dev) == NULL) ? NULL : afu;
  43. }
  44. EXPORT_SYMBOL_GPL(cxl_afu_get);
  45. void cxl_afu_put(struct cxl_afu *afu)
  46. {
  47. put_device(&afu->dev);
  48. }
  49. EXPORT_SYMBOL_GPL(cxl_afu_put);
  50. void cxl_slbia(struct mm_struct *mm)
  51. {
  52. struct cxl_calls *calls;
  53. calls = cxl_calls_get();
  54. if (!calls)
  55. return;
  56. if (cxl_ctx_in_use())
  57. calls->cxl_slbia(mm);
  58. cxl_calls_put(calls);
  59. }
  60. int register_cxl_calls(struct cxl_calls *calls)
  61. {
  62. if (cxl_calls)
  63. return -EBUSY;
  64. rcu_assign_pointer(cxl_calls, calls);
  65. return 0;
  66. }
  67. EXPORT_SYMBOL_GPL(register_cxl_calls);
  68. void unregister_cxl_calls(struct cxl_calls *calls)
  69. {
  70. BUG_ON(cxl_calls->owner != calls->owner);
  71. RCU_INIT_POINTER(cxl_calls, NULL);
  72. synchronize_rcu();
  73. }
  74. EXPORT_SYMBOL_GPL(unregister_cxl_calls);
  75. int cxl_update_properties(struct device_node *dn,
  76. struct property *new_prop)
  77. {
  78. return of_update_property(dn, new_prop);
  79. }
  80. EXPORT_SYMBOL_GPL(cxl_update_properties);
  81. static int __init cxl_base_init(void)
  82. {
  83. struct device_node *np;
  84. struct platform_device *dev;
  85. int count = 0;
  86. /*
  87. * Scan for compatible devices in guest only
  88. */
  89. if (cpu_has_feature(CPU_FTR_HVMODE))
  90. return 0;
  91. for_each_compatible_node(np, NULL, "ibm,coherent-platform-facility") {
  92. dev = of_platform_device_create(np, NULL, NULL);
  93. if (dev)
  94. count++;
  95. }
  96. pr_devel("Found %d cxl device(s)\n", count);
  97. return 0;
  98. }
  99. device_initcall(cxl_base_init);