spinlock_64.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2011 Tilera Corporation. All Rights Reserved.
  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, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/spinlock.h>
  15. #include <linux/module.h>
  16. #include <asm/processor.h>
  17. #include "spinlock_common.h"
  18. /*
  19. * Read the spinlock value without allocating in our cache and without
  20. * causing an invalidation to another cpu with a copy of the cacheline.
  21. * This is important when we are spinning waiting for the lock.
  22. */
  23. static inline u32 arch_spin_read_noalloc(void *lock)
  24. {
  25. return atomic_cmpxchg((atomic_t *)lock, -1, -1);
  26. }
  27. /*
  28. * Wait until the high bits (current) match my ticket.
  29. * If we notice the overflow bit set on entry, we clear it.
  30. */
  31. void arch_spin_lock_slow(arch_spinlock_t *lock, u32 my_ticket)
  32. {
  33. if (unlikely(my_ticket & __ARCH_SPIN_NEXT_OVERFLOW)) {
  34. __insn_fetchand4(&lock->lock, ~__ARCH_SPIN_NEXT_OVERFLOW);
  35. my_ticket &= ~__ARCH_SPIN_NEXT_OVERFLOW;
  36. }
  37. for (;;) {
  38. u32 val = arch_spin_read_noalloc(lock);
  39. u32 delta = my_ticket - arch_spin_current(val);
  40. if (delta == 0)
  41. return;
  42. relax((128 / CYCLES_PER_RELAX_LOOP) * delta);
  43. }
  44. }
  45. EXPORT_SYMBOL(arch_spin_lock_slow);
  46. /*
  47. * Check the lock to see if it is plausible, and try to get it with cmpxchg().
  48. */
  49. int arch_spin_trylock(arch_spinlock_t *lock)
  50. {
  51. u32 val = arch_spin_read_noalloc(lock);
  52. if (unlikely(arch_spin_current(val) != arch_spin_next(val)))
  53. return 0;
  54. return cmpxchg(&lock->lock, val, (val + 1) & ~__ARCH_SPIN_NEXT_OVERFLOW)
  55. == val;
  56. }
  57. EXPORT_SYMBOL(arch_spin_trylock);
  58. void arch_spin_unlock_wait(arch_spinlock_t *lock)
  59. {
  60. u32 iterations = 0;
  61. u32 val = READ_ONCE(lock->lock);
  62. u32 curr = arch_spin_current(val);
  63. /* Return immediately if unlocked. */
  64. if (arch_spin_next(val) == curr)
  65. return;
  66. /* Wait until the current locker has released the lock. */
  67. do {
  68. delay_backoff(iterations++);
  69. } while (arch_spin_current(READ_ONCE(lock->lock)) == curr);
  70. }
  71. EXPORT_SYMBOL(arch_spin_unlock_wait);
  72. /*
  73. * If the read lock fails due to a writer, we retry periodically
  74. * until the value is positive and we write our incremented reader count.
  75. */
  76. void __read_lock_failed(arch_rwlock_t *rw)
  77. {
  78. u32 val;
  79. int iterations = 0;
  80. do {
  81. delay_backoff(iterations++);
  82. val = __insn_fetchaddgez4(&rw->lock, 1);
  83. } while (unlikely(arch_write_val_locked(val)));
  84. }
  85. EXPORT_SYMBOL(__read_lock_failed);
  86. /*
  87. * If we failed because there were readers, clear the "writer" bit
  88. * so we don't block additional readers. Otherwise, there was another
  89. * writer anyway, so our "fetchor" made no difference. Then wait,
  90. * issuing periodic fetchor instructions, till we get the lock.
  91. */
  92. void __write_lock_failed(arch_rwlock_t *rw, u32 val)
  93. {
  94. int iterations = 0;
  95. do {
  96. if (!arch_write_val_locked(val))
  97. val = __insn_fetchand4(&rw->lock, ~__WRITE_LOCK_BIT);
  98. delay_backoff(iterations++);
  99. val = __insn_fetchor4(&rw->lock, __WRITE_LOCK_BIT);
  100. } while (val != 0);
  101. }
  102. EXPORT_SYMBOL(__write_lock_failed);