atomic_32.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #include <linux/cache.h>
  15. #include <linux/delay.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/module.h>
  18. #include <linux/mm.h>
  19. #include <linux/atomic.h>
  20. #include <asm/futex.h>
  21. #include <arch/chip.h>
  22. /* See <asm/atomic_32.h> */
  23. #if ATOMIC_LOCKS_FOUND_VIA_TABLE()
  24. /*
  25. * A block of memory containing locks for atomic ops. Each instance of this
  26. * struct will be homed on a different CPU.
  27. */
  28. struct atomic_locks_on_cpu {
  29. int lock[ATOMIC_HASH_L2_SIZE];
  30. } __attribute__((aligned(ATOMIC_HASH_L2_SIZE * 4)));
  31. static DEFINE_PER_CPU(struct atomic_locks_on_cpu, atomic_lock_pool);
  32. /* The locks we'll use until __init_atomic_per_cpu is called. */
  33. static struct atomic_locks_on_cpu __initdata initial_atomic_locks;
  34. /* Hash into this vector to get a pointer to lock for the given atomic. */
  35. struct atomic_locks_on_cpu *atomic_lock_ptr[ATOMIC_HASH_L1_SIZE]
  36. __write_once = {
  37. [0 ... ATOMIC_HASH_L1_SIZE-1] (&initial_atomic_locks)
  38. };
  39. #else /* ATOMIC_LOCKS_FOUND_VIA_TABLE() */
  40. /* This page is remapped on startup to be hash-for-home. */
  41. int atomic_locks[PAGE_SIZE / sizeof(int)] __page_aligned_bss;
  42. #endif /* ATOMIC_LOCKS_FOUND_VIA_TABLE() */
  43. static inline int *__atomic_hashed_lock(volatile void *v)
  44. {
  45. /* NOTE: this code must match "sys_cmpxchg" in kernel/intvec_32.S */
  46. #if ATOMIC_LOCKS_FOUND_VIA_TABLE()
  47. unsigned long i =
  48. (unsigned long) v & ((PAGE_SIZE-1) & -sizeof(long long));
  49. unsigned long n = __insn_crc32_32(0, i);
  50. /* Grab high bits for L1 index. */
  51. unsigned long l1_index = n >> ((sizeof(n) * 8) - ATOMIC_HASH_L1_SHIFT);
  52. /* Grab low bits for L2 index. */
  53. unsigned long l2_index = n & (ATOMIC_HASH_L2_SIZE - 1);
  54. return &atomic_lock_ptr[l1_index]->lock[l2_index];
  55. #else
  56. /*
  57. * Use bits [3, 3 + ATOMIC_HASH_SHIFT) as the lock index.
  58. * Using mm works here because atomic_locks is page aligned.
  59. */
  60. unsigned long ptr = __insn_mm((unsigned long)v >> 1,
  61. (unsigned long)atomic_locks,
  62. 2, (ATOMIC_HASH_SHIFT + 2) - 1);
  63. return (int *)ptr;
  64. #endif
  65. }
  66. #ifdef CONFIG_SMP
  67. /* Return whether the passed pointer is a valid atomic lock pointer. */
  68. static int is_atomic_lock(int *p)
  69. {
  70. #if ATOMIC_LOCKS_FOUND_VIA_TABLE()
  71. int i;
  72. for (i = 0; i < ATOMIC_HASH_L1_SIZE; ++i) {
  73. if (p >= &atomic_lock_ptr[i]->lock[0] &&
  74. p < &atomic_lock_ptr[i]->lock[ATOMIC_HASH_L2_SIZE]) {
  75. return 1;
  76. }
  77. }
  78. return 0;
  79. #else
  80. return p >= &atomic_locks[0] && p < &atomic_locks[ATOMIC_HASH_SIZE];
  81. #endif
  82. }
  83. void __atomic_fault_unlock(int *irqlock_word)
  84. {
  85. BUG_ON(!is_atomic_lock(irqlock_word));
  86. BUG_ON(*irqlock_word != 1);
  87. *irqlock_word = 0;
  88. }
  89. #endif /* CONFIG_SMP */
  90. static inline int *__atomic_setup(volatile void *v)
  91. {
  92. /* Issue a load to the target to bring it into cache. */
  93. *(volatile int *)v;
  94. return __atomic_hashed_lock(v);
  95. }
  96. int _atomic_xchg(atomic_t *v, int n)
  97. {
  98. return __atomic_xchg(&v->counter, __atomic_setup(v), n).val;
  99. }
  100. EXPORT_SYMBOL(_atomic_xchg);
  101. int _atomic_xchg_add(atomic_t *v, int i)
  102. {
  103. return __atomic_xchg_add(&v->counter, __atomic_setup(v), i).val;
  104. }
  105. EXPORT_SYMBOL(_atomic_xchg_add);
  106. int _atomic_xchg_add_unless(atomic_t *v, int a, int u)
  107. {
  108. /*
  109. * Note: argument order is switched here since it is easier
  110. * to use the first argument consistently as the "old value"
  111. * in the assembly, as is done for _atomic_cmpxchg().
  112. */
  113. return __atomic_xchg_add_unless(&v->counter, __atomic_setup(v), u, a)
  114. .val;
  115. }
  116. EXPORT_SYMBOL(_atomic_xchg_add_unless);
  117. int _atomic_cmpxchg(atomic_t *v, int o, int n)
  118. {
  119. return __atomic_cmpxchg(&v->counter, __atomic_setup(v), o, n).val;
  120. }
  121. EXPORT_SYMBOL(_atomic_cmpxchg);
  122. unsigned long _atomic_or(volatile unsigned long *p, unsigned long mask)
  123. {
  124. return __atomic_or((int *)p, __atomic_setup(p), mask).val;
  125. }
  126. EXPORT_SYMBOL(_atomic_or);
  127. unsigned long _atomic_andn(volatile unsigned long *p, unsigned long mask)
  128. {
  129. return __atomic_andn((int *)p, __atomic_setup(p), mask).val;
  130. }
  131. EXPORT_SYMBOL(_atomic_andn);
  132. unsigned long _atomic_xor(volatile unsigned long *p, unsigned long mask)
  133. {
  134. return __atomic_xor((int *)p, __atomic_setup(p), mask).val;
  135. }
  136. EXPORT_SYMBOL(_atomic_xor);
  137. u64 _atomic64_xchg(atomic64_t *v, u64 n)
  138. {
  139. return __atomic64_xchg(&v->counter, __atomic_setup(v), n);
  140. }
  141. EXPORT_SYMBOL(_atomic64_xchg);
  142. u64 _atomic64_xchg_add(atomic64_t *v, u64 i)
  143. {
  144. return __atomic64_xchg_add(&v->counter, __atomic_setup(v), i);
  145. }
  146. EXPORT_SYMBOL(_atomic64_xchg_add);
  147. u64 _atomic64_xchg_add_unless(atomic64_t *v, u64 a, u64 u)
  148. {
  149. /*
  150. * Note: argument order is switched here since it is easier
  151. * to use the first argument consistently as the "old value"
  152. * in the assembly, as is done for _atomic_cmpxchg().
  153. */
  154. return __atomic64_xchg_add_unless(&v->counter, __atomic_setup(v),
  155. u, a);
  156. }
  157. EXPORT_SYMBOL(_atomic64_xchg_add_unless);
  158. u64 _atomic64_cmpxchg(atomic64_t *v, u64 o, u64 n)
  159. {
  160. return __atomic64_cmpxchg(&v->counter, __atomic_setup(v), o, n);
  161. }
  162. EXPORT_SYMBOL(_atomic64_cmpxchg);
  163. static inline int *__futex_setup(int __user *v)
  164. {
  165. /*
  166. * Issue a prefetch to the counter to bring it into cache.
  167. * As for __atomic_setup, but we can't do a read into the L1
  168. * since it might fault; instead we do a prefetch into the L2.
  169. */
  170. __insn_prefetch(v);
  171. return __atomic_hashed_lock((int __force *)v);
  172. }
  173. struct __get_user futex_set(u32 __user *v, int i)
  174. {
  175. return __atomic_xchg((int __force *)v, __futex_setup(v), i);
  176. }
  177. struct __get_user futex_add(u32 __user *v, int n)
  178. {
  179. return __atomic_xchg_add((int __force *)v, __futex_setup(v), n);
  180. }
  181. struct __get_user futex_or(u32 __user *v, int n)
  182. {
  183. return __atomic_or((int __force *)v, __futex_setup(v), n);
  184. }
  185. struct __get_user futex_andn(u32 __user *v, int n)
  186. {
  187. return __atomic_andn((int __force *)v, __futex_setup(v), n);
  188. }
  189. struct __get_user futex_xor(u32 __user *v, int n)
  190. {
  191. return __atomic_xor((int __force *)v, __futex_setup(v), n);
  192. }
  193. struct __get_user futex_cmpxchg(u32 __user *v, int o, int n)
  194. {
  195. return __atomic_cmpxchg((int __force *)v, __futex_setup(v), o, n);
  196. }
  197. /*
  198. * If any of the atomic or futex routines hit a bad address (not in
  199. * the page tables at kernel PL) this routine is called. The futex
  200. * routines are never used on kernel space, and the normal atomics and
  201. * bitops are never used on user space. So a fault on kernel space
  202. * must be fatal, but a fault on userspace is a futex fault and we
  203. * need to return -EFAULT. Note that the context this routine is
  204. * invoked in is the context of the "_atomic_xxx()" routines called
  205. * by the functions in this file.
  206. */
  207. struct __get_user __atomic_bad_address(int __user *addr)
  208. {
  209. if (unlikely(!access_ok(VERIFY_WRITE, addr, sizeof(int))))
  210. panic("Bad address used for kernel atomic op: %p\n", addr);
  211. return (struct __get_user) { .err = -EFAULT };
  212. }
  213. #if CHIP_HAS_CBOX_HOME_MAP()
  214. static int __init noatomichash(char *str)
  215. {
  216. pr_warning("noatomichash is deprecated.\n");
  217. return 1;
  218. }
  219. __setup("noatomichash", noatomichash);
  220. #endif
  221. void __init __init_atomic_per_cpu(void)
  222. {
  223. #if ATOMIC_LOCKS_FOUND_VIA_TABLE()
  224. unsigned int i;
  225. int actual_cpu;
  226. /*
  227. * Before this is called from setup, we just have one lock for
  228. * all atomic objects/operations. Here we replace the
  229. * elements of atomic_lock_ptr so that they point at per_cpu
  230. * integers. This seemingly over-complex approach stems from
  231. * the fact that DEFINE_PER_CPU defines an entry for each cpu
  232. * in the grid, not each cpu from 0..ATOMIC_HASH_SIZE-1. But
  233. * for efficient hashing of atomics to their locks we want a
  234. * compile time constant power of 2 for the size of this
  235. * table, so we use ATOMIC_HASH_SIZE.
  236. *
  237. * Here we populate atomic_lock_ptr from the per cpu
  238. * atomic_lock_pool, interspersing by actual cpu so that
  239. * subsequent elements are homed on consecutive cpus.
  240. */
  241. actual_cpu = cpumask_first(cpu_possible_mask);
  242. for (i = 0; i < ATOMIC_HASH_L1_SIZE; ++i) {
  243. /*
  244. * Preincrement to slightly bias against using cpu 0,
  245. * which has plenty of stuff homed on it already.
  246. */
  247. actual_cpu = cpumask_next(actual_cpu, cpu_possible_mask);
  248. if (actual_cpu >= nr_cpu_ids)
  249. actual_cpu = cpumask_first(cpu_possible_mask);
  250. atomic_lock_ptr[i] = &per_cpu(atomic_lock_pool, actual_cpu);
  251. }
  252. #else /* ATOMIC_LOCKS_FOUND_VIA_TABLE() */
  253. /* Validate power-of-two and "bigger than cpus" assumption */
  254. BUILD_BUG_ON(ATOMIC_HASH_SIZE & (ATOMIC_HASH_SIZE-1));
  255. BUG_ON(ATOMIC_HASH_SIZE < nr_cpu_ids);
  256. /*
  257. * On TILEPro we prefer to use a single hash-for-home
  258. * page, since this means atomic operations are less
  259. * likely to encounter a TLB fault and thus should
  260. * in general perform faster. You may wish to disable
  261. * this in situations where few hash-for-home tiles
  262. * are configured.
  263. */
  264. BUG_ON((unsigned long)atomic_locks % PAGE_SIZE != 0);
  265. /* The locks must all fit on one page. */
  266. BUILD_BUG_ON(ATOMIC_HASH_SIZE * sizeof(int) > PAGE_SIZE);
  267. /*
  268. * We use the page offset of the atomic value's address as
  269. * an index into atomic_locks, excluding the low 3 bits.
  270. * That should not produce more indices than ATOMIC_HASH_SIZE.
  271. */
  272. BUILD_BUG_ON((PAGE_SIZE >> 3) > ATOMIC_HASH_SIZE);
  273. #endif /* ATOMIC_LOCKS_FOUND_VIA_TABLE() */
  274. /* The futex code makes this assumption, so we validate it here. */
  275. BUILD_BUG_ON(sizeof(atomic_t) != sizeof(int));
  276. }