remote_spinlock.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Copyright (c) 2008-2009, 2011, 2013-2014 The Linux Foundation.
  2. * All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #ifndef __LINUX_REMOTE_SPINLOCK_H
  15. #define __LINUX_REMOTE_SPINLOCK_H
  16. #include <linux/spinlock.h>
  17. #include <linux/mutex.h>
  18. #include <asm/remote_spinlock.h>
  19. /* Grabbing a local spin lock before going for a remote lock has several
  20. * advantages:
  21. * 1. Get calls to preempt enable/disable and IRQ save/restore for free.
  22. * 2. For UP kernel, there is no overhead.
  23. * 3. Reduces the possibility of executing the remote spin lock code. This is
  24. * especially useful when the remote CPUs' mutual exclusion instructions
  25. * don't work with the local CPUs' instructions. In such cases, one has to
  26. * use software based mutex algorithms (e.g. Lamport's bakery algorithm)
  27. * which could get expensive when the no. of contending CPUs is high.
  28. * 4. In the case of software based mutex algorithm the exection time will be
  29. * smaller since the no. of contending CPUs is reduced by having just one
  30. * contender for all the local CPUs.
  31. * 5. Get most of the spin lock debug features for free.
  32. * 6. The code will continue to work "gracefully" even when the remote spin
  33. * lock code is stubbed out for debug purposes or when there is no remote
  34. * CPU in some board/machine types.
  35. */
  36. typedef struct {
  37. spinlock_t local;
  38. _remote_spinlock_t remote;
  39. } remote_spinlock_t;
  40. #define remote_spin_lock_init(lock, id) \
  41. ({ \
  42. spin_lock_init(&((lock)->local)); \
  43. _remote_spin_lock_init(id, &((lock)->remote)); \
  44. })
  45. #define remote_spin_lock(lock) \
  46. do { \
  47. spin_lock(&((lock)->local)); \
  48. _remote_spin_lock(&((lock)->remote)); \
  49. } while (0)
  50. #define remote_spin_unlock(lock) \
  51. do { \
  52. _remote_spin_unlock(&((lock)->remote)); \
  53. spin_unlock(&((lock)->local)); \
  54. } while (0)
  55. #define remote_spin_lock_irqsave(lock, flags) \
  56. do { \
  57. spin_lock_irqsave(&((lock)->local), flags); \
  58. _remote_spin_lock(&((lock)->remote)); \
  59. } while (0)
  60. #define remote_spin_unlock_irqrestore(lock, flags) \
  61. do { \
  62. _remote_spin_unlock(&((lock)->remote)); \
  63. spin_unlock_irqrestore(&((lock)->local), flags); \
  64. } while (0)
  65. #define remote_spin_trylock(lock) \
  66. ({ \
  67. spin_trylock(&((lock)->local)) \
  68. ? _remote_spin_trylock(&((lock)->remote)) \
  69. ? 1 \
  70. : ({ spin_unlock(&((lock)->local)); 0; }) \
  71. : 0; \
  72. })
  73. #define remote_spin_trylock_irqsave(lock, flags) \
  74. ({ \
  75. spin_trylock_irqsave(&((lock)->local), flags) \
  76. ? _remote_spin_trylock(&((lock)->remote)) \
  77. ? 1 \
  78. : ({ spin_unlock_irqrestore(&((lock)->local), flags); \
  79. 0; }) \
  80. : 0; \
  81. })
  82. #define remote_spin_lock_rlock_id(lock, tid) \
  83. _remote_spin_lock_rlock_id(&((lock)->remote), tid)
  84. #define remote_spin_unlock_rlock(lock) \
  85. _remote_spin_unlock_rlock(&((lock)->remote))
  86. #define remote_spin_release(lock, pid) \
  87. _remote_spin_release(&((lock)->remote), pid)
  88. #define remote_spin_release_all(pid) \
  89. _remote_spin_release_all(pid)
  90. #define remote_spin_owner(lock) \
  91. _remote_spin_owner(&((lock)->remote))
  92. typedef struct {
  93. struct mutex local;
  94. _remote_mutex_t remote;
  95. } remote_mutex_t;
  96. #define remote_mutex_init(lock, id) \
  97. ({ \
  98. mutex_init(&((lock)->local)); \
  99. _remote_mutex_init(id, &((lock)->remote)); \
  100. })
  101. #define remote_mutex_lock(lock) \
  102. do { \
  103. mutex_lock(&((lock)->local)); \
  104. _remote_mutex_lock(&((lock)->remote)); \
  105. } while (0)
  106. #define remote_mutex_trylock(lock) \
  107. ({ \
  108. mutex_trylock(&((lock)->local)) \
  109. ? _remote_mutex_trylock(&((lock)->remote)) \
  110. ? 1 \
  111. : ({mutex_unlock(&((lock)->local)); 0; }) \
  112. : 0; \
  113. })
  114. #define remote_mutex_unlock(lock) \
  115. do { \
  116. _remote_mutex_unlock(&((lock)->remote)); \
  117. mutex_unlock(&((lock)->local)); \
  118. } while (0)
  119. #endif