base.c 2.7 KB

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