base.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. void cxl_slbia(struct mm_struct *mm)
  44. {
  45. struct cxl_calls *calls;
  46. calls = cxl_calls_get();
  47. if (!calls)
  48. return;
  49. if (cxl_ctx_in_use())
  50. calls->cxl_slbia(mm);
  51. cxl_calls_put(calls);
  52. }
  53. int register_cxl_calls(struct cxl_calls *calls)
  54. {
  55. if (cxl_calls)
  56. return -EBUSY;
  57. rcu_assign_pointer(cxl_calls, calls);
  58. return 0;
  59. }
  60. EXPORT_SYMBOL_GPL(register_cxl_calls);
  61. void unregister_cxl_calls(struct cxl_calls *calls)
  62. {
  63. BUG_ON(cxl_calls->owner != calls->owner);
  64. RCU_INIT_POINTER(cxl_calls, NULL);
  65. synchronize_rcu();
  66. }
  67. EXPORT_SYMBOL_GPL(unregister_cxl_calls);
  68. int cxl_update_properties(struct device_node *dn,
  69. struct property *new_prop)
  70. {
  71. return of_update_property(dn, new_prop);
  72. }
  73. EXPORT_SYMBOL_GPL(cxl_update_properties);
  74. static int __init cxl_base_init(void)
  75. {
  76. struct device_node *np = NULL;
  77. struct platform_device *dev;
  78. int count = 0;
  79. /*
  80. * Scan for compatible devices in guest only
  81. */
  82. if (cpu_has_feature(CPU_FTR_HVMODE))
  83. return 0;
  84. while ((np = of_find_compatible_node(np, NULL,
  85. "ibm,coherent-platform-facility"))) {
  86. dev = of_platform_device_create(np, NULL, NULL);
  87. if (dev)
  88. count++;
  89. }
  90. pr_devel("Found %d cxl device(s)\n", count);
  91. return 0;
  92. }
  93. module_init(cxl_base_init);