base.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "cxl.h"
  14. /* protected by rcu */
  15. static struct cxl_calls *cxl_calls;
  16. atomic_t cxl_use_count = ATOMIC_INIT(0);
  17. EXPORT_SYMBOL(cxl_use_count);
  18. #ifdef CONFIG_CXL_MODULE
  19. static inline struct cxl_calls *cxl_calls_get(void)
  20. {
  21. struct cxl_calls *calls = NULL;
  22. rcu_read_lock();
  23. calls = rcu_dereference(cxl_calls);
  24. if (calls && !try_module_get(calls->owner))
  25. calls = NULL;
  26. rcu_read_unlock();
  27. return calls;
  28. }
  29. static inline void cxl_calls_put(struct cxl_calls *calls)
  30. {
  31. BUG_ON(calls != cxl_calls);
  32. /* we don't need to rcu this, as we hold a reference to the module */
  33. module_put(cxl_calls->owner);
  34. }
  35. #else /* !defined CONFIG_CXL_MODULE */
  36. static inline struct cxl_calls *cxl_calls_get(void)
  37. {
  38. return cxl_calls;
  39. }
  40. static inline void cxl_calls_put(struct cxl_calls *calls) { }
  41. #endif /* CONFIG_CXL_MODULE */
  42. void cxl_slbia(struct mm_struct *mm)
  43. {
  44. struct cxl_calls *calls;
  45. calls = cxl_calls_get();
  46. if (!calls)
  47. return;
  48. if (cxl_ctx_in_use())
  49. calls->cxl_slbia(mm);
  50. cxl_calls_put(calls);
  51. }
  52. int register_cxl_calls(struct cxl_calls *calls)
  53. {
  54. if (cxl_calls)
  55. return -EBUSY;
  56. rcu_assign_pointer(cxl_calls, calls);
  57. return 0;
  58. }
  59. EXPORT_SYMBOL_GPL(register_cxl_calls);
  60. void unregister_cxl_calls(struct cxl_calls *calls)
  61. {
  62. BUG_ON(cxl_calls->owner != calls->owner);
  63. RCU_INIT_POINTER(cxl_calls, NULL);
  64. synchronize_rcu();
  65. }
  66. EXPORT_SYMBOL_GPL(unregister_cxl_calls);