atomic_32.c 10 KB

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