base.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. /*
  86. * API calls into the driver that may be called from the PHB code and must be
  87. * built in.
  88. */
  89. bool cxl_pci_associate_default_context(struct pci_dev *dev, struct cxl_afu *afu)
  90. {
  91. bool ret;
  92. struct cxl_calls *calls;
  93. calls = cxl_calls_get();
  94. if (!calls)
  95. return false;
  96. ret = calls->cxl_pci_associate_default_context(dev, afu);
  97. cxl_calls_put(calls);
  98. return ret;
  99. }
  100. EXPORT_SYMBOL_GPL(cxl_pci_associate_default_context);
  101. void cxl_pci_disable_device(struct pci_dev *dev)
  102. {
  103. struct cxl_calls *calls;
  104. calls = cxl_calls_get();
  105. if (!calls)
  106. return;
  107. calls->cxl_pci_disable_device(dev);
  108. cxl_calls_put(calls);
  109. }
  110. EXPORT_SYMBOL_GPL(cxl_pci_disable_device);
  111. int cxl_next_msi_hwirq(struct pci_dev *pdev, struct cxl_context **ctx, int *afu_irq)
  112. {
  113. int ret;
  114. struct cxl_calls *calls;
  115. calls = cxl_calls_get();
  116. if (!calls)
  117. return -EBUSY;
  118. ret = calls->cxl_next_msi_hwirq(pdev, ctx, afu_irq);
  119. cxl_calls_put(calls);
  120. return ret;
  121. }
  122. EXPORT_SYMBOL_GPL(cxl_next_msi_hwirq);
  123. int cxl_cx4_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  124. {
  125. int ret;
  126. struct cxl_calls *calls;
  127. calls = cxl_calls_get();
  128. if (!calls)
  129. return false;
  130. ret = calls->cxl_cx4_setup_msi_irqs(pdev, nvec, type);
  131. cxl_calls_put(calls);
  132. return ret;
  133. }
  134. EXPORT_SYMBOL_GPL(cxl_cx4_setup_msi_irqs);
  135. void cxl_cx4_teardown_msi_irqs(struct pci_dev *pdev)
  136. {
  137. struct cxl_calls *calls;
  138. calls = cxl_calls_get();
  139. if (!calls)
  140. return;
  141. calls->cxl_cx4_teardown_msi_irqs(pdev);
  142. cxl_calls_put(calls);
  143. }
  144. EXPORT_SYMBOL_GPL(cxl_cx4_teardown_msi_irqs);
  145. static int __init cxl_base_init(void)
  146. {
  147. struct device_node *np;
  148. struct platform_device *dev;
  149. int count = 0;
  150. /*
  151. * Scan for compatible devices in guest only
  152. */
  153. if (cpu_has_feature(CPU_FTR_HVMODE))
  154. return 0;
  155. for_each_compatible_node(np, NULL, "ibm,coherent-platform-facility") {
  156. dev = of_platform_device_create(np, NULL, NULL);
  157. if (dev)
  158. count++;
  159. }
  160. pr_devel("Found %d cxl device(s)\n", count);
  161. return 0;
  162. }
  163. device_initcall(cxl_base_init);