atomic_32.c 9.6 KB

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