Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 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 | |
| 15 | #include <linux/cache.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/uaccess.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/mm.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 20 | #include <linux/atomic.h> |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 21 | #include <arch/chip.h> |
| 22 | |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 23 | /* This page is remapped on startup to be hash-for-home. */ |
Chris Metcalf | 2cb8240 | 2011-02-27 18:52:24 -0500 | [diff] [blame] | 24 | int atomic_locks[PAGE_SIZE / sizeof(int)] __page_aligned_bss; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 25 | |
Chris Metcalf | 47d632f | 2012-03-29 13:39:51 -0400 | [diff] [blame] | 26 | int *__atomic_hashed_lock(volatile void *v) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 27 | { |
Chris Metcalf | 5fb682b | 2011-02-28 15:58:39 -0500 | [diff] [blame] | 28 | /* NOTE: this code must match "sys_cmpxchg" in kernel/intvec_32.S */ |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 29 | /* |
| 30 | * Use bits [3, 3 + ATOMIC_HASH_SHIFT) as the lock index. |
| 31 | * Using mm works here because atomic_locks is page aligned. |
| 32 | */ |
| 33 | unsigned long ptr = __insn_mm((unsigned long)v >> 1, |
| 34 | (unsigned long)atomic_locks, |
| 35 | 2, (ATOMIC_HASH_SHIFT + 2) - 1); |
| 36 | return (int *)ptr; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | #ifdef CONFIG_SMP |
| 40 | /* Return whether the passed pointer is a valid atomic lock pointer. */ |
| 41 | static int is_atomic_lock(int *p) |
| 42 | { |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 43 | return p >= &atomic_locks[0] && p < &atomic_locks[ATOMIC_HASH_SIZE]; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void __atomic_fault_unlock(int *irqlock_word) |
| 47 | { |
| 48 | BUG_ON(!is_atomic_lock(irqlock_word)); |
| 49 | BUG_ON(*irqlock_word != 1); |
| 50 | *irqlock_word = 0; |
| 51 | } |
| 52 | |
| 53 | #endif /* CONFIG_SMP */ |
| 54 | |
| 55 | static inline int *__atomic_setup(volatile void *v) |
| 56 | { |
| 57 | /* Issue a load to the target to bring it into cache. */ |
| 58 | *(volatile int *)v; |
| 59 | return __atomic_hashed_lock(v); |
| 60 | } |
| 61 | |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 62 | int _atomic_xchg(int *v, int n) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 63 | { |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 64 | return __atomic_xchg(v, __atomic_setup(v), n).val; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 65 | } |
| 66 | EXPORT_SYMBOL(_atomic_xchg); |
| 67 | |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 68 | int _atomic_xchg_add(int *v, int i) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 69 | { |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 70 | return __atomic_xchg_add(v, __atomic_setup(v), i).val; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 71 | } |
| 72 | EXPORT_SYMBOL(_atomic_xchg_add); |
| 73 | |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 74 | int _atomic_xchg_add_unless(int *v, int a, int u) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 75 | { |
| 76 | /* |
| 77 | * Note: argument order is switched here since it is easier |
| 78 | * to use the first argument consistently as the "old value" |
| 79 | * in the assembly, as is done for _atomic_cmpxchg(). |
| 80 | */ |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 81 | return __atomic_xchg_add_unless(v, __atomic_setup(v), u, a).val; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 82 | } |
| 83 | EXPORT_SYMBOL(_atomic_xchg_add_unless); |
| 84 | |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 85 | int _atomic_cmpxchg(int *v, int o, int n) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 86 | { |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 87 | return __atomic_cmpxchg(v, __atomic_setup(v), o, n).val; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 88 | } |
| 89 | EXPORT_SYMBOL(_atomic_cmpxchg); |
| 90 | |
| 91 | unsigned long _atomic_or(volatile unsigned long *p, unsigned long mask) |
| 92 | { |
| 93 | return __atomic_or((int *)p, __atomic_setup(p), mask).val; |
| 94 | } |
| 95 | EXPORT_SYMBOL(_atomic_or); |
| 96 | |
| 97 | unsigned long _atomic_andn(volatile unsigned long *p, unsigned long mask) |
| 98 | { |
| 99 | return __atomic_andn((int *)p, __atomic_setup(p), mask).val; |
| 100 | } |
| 101 | EXPORT_SYMBOL(_atomic_andn); |
| 102 | |
| 103 | unsigned long _atomic_xor(volatile unsigned long *p, unsigned long mask) |
| 104 | { |
| 105 | return __atomic_xor((int *)p, __atomic_setup(p), mask).val; |
| 106 | } |
| 107 | EXPORT_SYMBOL(_atomic_xor); |
| 108 | |
| 109 | |
Chen Gang | b924a69 | 2013-09-25 12:14:08 +0800 | [diff] [blame^] | 110 | long long _atomic64_xchg(long long *v, long long n) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 111 | { |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 112 | return __atomic64_xchg(v, __atomic_setup(v), n); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 113 | } |
| 114 | EXPORT_SYMBOL(_atomic64_xchg); |
| 115 | |
Chen Gang | b924a69 | 2013-09-25 12:14:08 +0800 | [diff] [blame^] | 116 | long long _atomic64_xchg_add(long long *v, long long i) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 117 | { |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 118 | return __atomic64_xchg_add(v, __atomic_setup(v), i); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 119 | } |
| 120 | EXPORT_SYMBOL(_atomic64_xchg_add); |
| 121 | |
Chen Gang | b924a69 | 2013-09-25 12:14:08 +0800 | [diff] [blame^] | 122 | long long _atomic64_xchg_add_unless(long long *v, long long a, long long u) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 123 | { |
| 124 | /* |
| 125 | * Note: argument order is switched here since it is easier |
| 126 | * to use the first argument consistently as the "old value" |
| 127 | * in the assembly, as is done for _atomic_cmpxchg(). |
| 128 | */ |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 129 | return __atomic64_xchg_add_unless(v, __atomic_setup(v), u, a); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 130 | } |
| 131 | EXPORT_SYMBOL(_atomic64_xchg_add_unless); |
| 132 | |
Chen Gang | b924a69 | 2013-09-25 12:14:08 +0800 | [diff] [blame^] | 133 | long long _atomic64_cmpxchg(long long *v, long long o, long long n) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 134 | { |
Chris Metcalf | 6dc9658 | 2013-09-06 08:56:45 -0400 | [diff] [blame] | 135 | return __atomic64_cmpxchg(v, __atomic_setup(v), o, n); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 136 | } |
| 137 | EXPORT_SYMBOL(_atomic64_cmpxchg); |
| 138 | |
| 139 | |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 140 | /* |
| 141 | * If any of the atomic or futex routines hit a bad address (not in |
| 142 | * the page tables at kernel PL) this routine is called. The futex |
| 143 | * routines are never used on kernel space, and the normal atomics and |
| 144 | * bitops are never used on user space. So a fault on kernel space |
| 145 | * must be fatal, but a fault on userspace is a futex fault and we |
| 146 | * need to return -EFAULT. Note that the context this routine is |
| 147 | * invoked in is the context of the "_atomic_xxx()" routines called |
| 148 | * by the functions in this file. |
| 149 | */ |
Chris Metcalf | 0707ad3 | 2010-06-25 17:04:17 -0400 | [diff] [blame] | 150 | struct __get_user __atomic_bad_address(int __user *addr) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 151 | { |
| 152 | if (unlikely(!access_ok(VERIFY_WRITE, addr, sizeof(int)))) |
| 153 | panic("Bad address used for kernel atomic op: %p\n", addr); |
| 154 | return (struct __get_user) { .err = -EFAULT }; |
| 155 | } |
| 156 | |
| 157 | |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 158 | void __init __init_atomic_per_cpu(void) |
| 159 | { |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 160 | /* Validate power-of-two and "bigger than cpus" assumption */ |
Akinobu Mita | de5bbad | 2010-10-06 00:55:29 +0900 | [diff] [blame] | 161 | BUILD_BUG_ON(ATOMIC_HASH_SIZE & (ATOMIC_HASH_SIZE-1)); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 162 | BUG_ON(ATOMIC_HASH_SIZE < nr_cpu_ids); |
| 163 | |
| 164 | /* |
| 165 | * On TILEPro we prefer to use a single hash-for-home |
| 166 | * page, since this means atomic operations are less |
| 167 | * likely to encounter a TLB fault and thus should |
| 168 | * in general perform faster. You may wish to disable |
| 169 | * this in situations where few hash-for-home tiles |
| 170 | * are configured. |
| 171 | */ |
| 172 | BUG_ON((unsigned long)atomic_locks % PAGE_SIZE != 0); |
| 173 | |
| 174 | /* The locks must all fit on one page. */ |
Akinobu Mita | de5bbad | 2010-10-06 00:55:29 +0900 | [diff] [blame] | 175 | BUILD_BUG_ON(ATOMIC_HASH_SIZE * sizeof(int) > PAGE_SIZE); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * We use the page offset of the atomic value's address as |
| 179 | * an index into atomic_locks, excluding the low 3 bits. |
| 180 | * That should not produce more indices than ATOMIC_HASH_SIZE. |
| 181 | */ |
Akinobu Mita | de5bbad | 2010-10-06 00:55:29 +0900 | [diff] [blame] | 182 | BUILD_BUG_ON((PAGE_SIZE >> 3) > ATOMIC_HASH_SIZE); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 183 | } |