spinlock_32.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 2010 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. * 32-bit SMP spinlocks.
  15. */
  16. #ifndef _ASM_TILE_SPINLOCK_32_H
  17. #define _ASM_TILE_SPINLOCK_32_H
  18. #include <linux/atomic.h>
  19. #include <asm/page.h>
  20. #include <linux/compiler.h>
  21. /*
  22. * We only use even ticket numbers so the '1' inserted by a tns is
  23. * an unambiguous "ticket is busy" flag.
  24. */
  25. #define TICKET_QUANTUM 2
  26. /*
  27. * SMP ticket spinlocks, allowing only a single CPU anywhere
  28. *
  29. * (the type definitions are in asm/spinlock_types.h)
  30. */
  31. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  32. {
  33. /*
  34. * Note that even if a new ticket is in the process of being
  35. * acquired, so lock->next_ticket is 1, it's still reasonable
  36. * to claim the lock is held, since it will be momentarily
  37. * if not already. There's no need to wait for a "valid"
  38. * lock->next_ticket to become available.
  39. * Use READ_ONCE() to ensure that calling this in a loop is OK.
  40. */
  41. int curr = READ_ONCE(lock->current_ticket);
  42. int next = READ_ONCE(lock->next_ticket);
  43. return next != curr;
  44. }
  45. void arch_spin_lock(arch_spinlock_t *lock);
  46. /* We cannot take an interrupt after getting a ticket, so don't enable them. */
  47. #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
  48. int arch_spin_trylock(arch_spinlock_t *lock);
  49. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  50. {
  51. /* For efficiency, overlap fetching the old ticket with the wmb(). */
  52. int old_ticket = lock->current_ticket;
  53. wmb(); /* guarantee anything modified under the lock is visible */
  54. lock->current_ticket = old_ticket + TICKET_QUANTUM;
  55. }
  56. void arch_spin_unlock_wait(arch_spinlock_t *lock);
  57. /*
  58. * Read-write spinlocks, allowing multiple readers
  59. * but only one writer.
  60. *
  61. * We use a "tns/store-back" technique on a single word to manage
  62. * the lock state, looping around to retry if the tns returns 1.
  63. */
  64. /* Internal layout of the word; do not use. */
  65. #define _WR_NEXT_SHIFT 8
  66. #define _WR_CURR_SHIFT 16
  67. #define _WR_WIDTH 8
  68. #define _RD_COUNT_SHIFT 24
  69. #define _RD_COUNT_WIDTH 8
  70. /**
  71. * arch_read_can_lock() - would read_trylock() succeed?
  72. */
  73. static inline int arch_read_can_lock(arch_rwlock_t *rwlock)
  74. {
  75. return (rwlock->lock << _RD_COUNT_WIDTH) == 0;
  76. }
  77. /**
  78. * arch_write_can_lock() - would write_trylock() succeed?
  79. */
  80. static inline int arch_write_can_lock(arch_rwlock_t *rwlock)
  81. {
  82. return rwlock->lock == 0;
  83. }
  84. /**
  85. * arch_read_lock() - acquire a read lock.
  86. */
  87. void arch_read_lock(arch_rwlock_t *rwlock);
  88. /**
  89. * arch_write_lock() - acquire a write lock.
  90. */
  91. void arch_write_lock(arch_rwlock_t *rwlock);
  92. /**
  93. * arch_read_trylock() - try to acquire a read lock.
  94. */
  95. int arch_read_trylock(arch_rwlock_t *rwlock);
  96. /**
  97. * arch_write_trylock() - try to acquire a write lock.
  98. */
  99. int arch_write_trylock(arch_rwlock_t *rwlock);
  100. /**
  101. * arch_read_unlock() - release a read lock.
  102. */
  103. void arch_read_unlock(arch_rwlock_t *rwlock);
  104. /**
  105. * arch_write_unlock() - release a write lock.
  106. */
  107. void arch_write_unlock(arch_rwlock_t *rwlock);
  108. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  109. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  110. #endif /* _ASM_TILE_SPINLOCK_32_H */