base.c 2.5 KB

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