H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_PERCPU_H |
| 2 | #define _ASM_X86_PERCPU_H |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 3 | |
Tejun Heo | 1a51e3a | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 4 | #ifdef CONFIG_X86_64 |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 5 | #define __percpu_seg gs |
| 6 | #define __percpu_mov_op movq |
Tejun Heo | 1a51e3a | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 7 | #else |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 8 | #define __percpu_seg fs |
| 9 | #define __percpu_mov_op movl |
Tejun Heo | 1a51e3a | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 10 | #endif |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 11 | |
| 12 | #ifdef __ASSEMBLY__ |
| 13 | |
| 14 | /* |
| 15 | * PER_CPU finds an address of a per-cpu variable. |
| 16 | * |
| 17 | * Args: |
| 18 | * var - variable name |
| 19 | * reg - 32bit register |
| 20 | * |
| 21 | * The resulting address is stored in the "reg" argument. |
| 22 | * |
| 23 | * Example: |
| 24 | * PER_CPU(cpu_gdt_descr, %ebx) |
| 25 | */ |
| 26 | #ifdef CONFIG_SMP |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 27 | #define PER_CPU(var, reg) \ |
Rusty Russell | dd17c8f | 2009-10-29 22:34:15 +0900 | [diff] [blame] | 28 | __percpu_mov_op %__percpu_seg:this_cpu_off, reg; \ |
| 29 | lea var(reg), reg |
| 30 | #define PER_CPU_VAR(var) %__percpu_seg:var |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 31 | #else /* ! SMP */ |
Rusty Russell | dd17c8f | 2009-10-29 22:34:15 +0900 | [diff] [blame] | 32 | #define PER_CPU(var, reg) __percpu_mov_op $var, reg |
| 33 | #define PER_CPU_VAR(var) var |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 34 | #endif /* SMP */ |
| 35 | |
Brian Gerst | 2add8e2 | 2009-02-08 09:58:39 -0500 | [diff] [blame] | 36 | #ifdef CONFIG_X86_64_SMP |
| 37 | #define INIT_PER_CPU_VAR(var) init_per_cpu__##var |
| 38 | #else |
Rusty Russell | dd17c8f | 2009-10-29 22:34:15 +0900 | [diff] [blame] | 39 | #define INIT_PER_CPU_VAR(var) var |
Brian Gerst | 2add8e2 | 2009-02-08 09:58:39 -0500 | [diff] [blame] | 40 | #endif |
| 41 | |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 42 | #else /* ...!ASSEMBLY */ |
| 43 | |
Tejun Heo | e59a1bb | 2009-06-22 11:56:24 +0900 | [diff] [blame] | 44 | #include <linux/kernel.h> |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 45 | #include <linux/stringify.h> |
| 46 | |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 47 | #ifdef CONFIG_SMP |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 48 | #define __percpu_arg(x) "%%"__stringify(__percpu_seg)":%P" #x |
Ingo Molnar | 6dbde35 | 2009-01-15 22:15:53 +0900 | [diff] [blame] | 49 | #define __my_cpu_offset percpu_read(this_cpu_off) |
Brian Gerst | db7829c | 2010-09-09 18:17:26 +0200 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * Compared to the generic __my_cpu_offset version, the following |
| 53 | * saves one instruction and avoids clobbering a temp register. |
| 54 | */ |
| 55 | #define __this_cpu_ptr(ptr) \ |
| 56 | ({ \ |
| 57 | unsigned long tcp_ptr__; \ |
| 58 | __verify_pcpu_ptr(ptr); \ |
| 59 | asm volatile("add " __percpu_arg(1) ", %0" \ |
| 60 | : "=r" (tcp_ptr__) \ |
| 61 | : "m" (this_cpu_off), "0" (ptr)); \ |
| 62 | (typeof(*(ptr)) __kernel __force *)tcp_ptr__; \ |
| 63 | }) |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 64 | #else |
Linus Torvalds | ed8d9ad | 2009-08-03 14:08:48 +0900 | [diff] [blame] | 65 | #define __percpu_arg(x) "%P" #x |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 66 | #endif |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 67 | |
Brian Gerst | 2add8e2 | 2009-02-08 09:58:39 -0500 | [diff] [blame] | 68 | /* |
| 69 | * Initialized pointers to per-cpu variables needed for the boot |
| 70 | * processor need to use these macros to get the proper address |
| 71 | * offset from __per_cpu_load on SMP. |
| 72 | * |
| 73 | * There also must be an entry in vmlinux_64.lds.S |
| 74 | */ |
| 75 | #define DECLARE_INIT_PER_CPU(var) \ |
Rusty Russell | dd17c8f | 2009-10-29 22:34:15 +0900 | [diff] [blame] | 76 | extern typeof(var) init_per_cpu_var(var) |
Brian Gerst | 2add8e2 | 2009-02-08 09:58:39 -0500 | [diff] [blame] | 77 | |
| 78 | #ifdef CONFIG_X86_64_SMP |
| 79 | #define init_per_cpu_var(var) init_per_cpu__##var |
| 80 | #else |
Rusty Russell | dd17c8f | 2009-10-29 22:34:15 +0900 | [diff] [blame] | 81 | #define init_per_cpu_var(var) var |
Brian Gerst | 2add8e2 | 2009-02-08 09:58:39 -0500 | [diff] [blame] | 82 | #endif |
| 83 | |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 84 | /* For arch-specific code, we can use direct single-insn ops (they |
| 85 | * don't give an lvalue though). */ |
| 86 | extern void __bad_percpu_size(void); |
| 87 | |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 88 | #define percpu_to_op(op, var, val) \ |
| 89 | do { \ |
Tejun Heo | 0f5e481 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 90 | typedef typeof(var) pto_T__; \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 91 | if (0) { \ |
Tejun Heo | 0f5e481 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 92 | pto_T__ pto_tmp__; \ |
| 93 | pto_tmp__ = (val); \ |
Andi Kleen | 23b764d | 2010-06-10 13:10:36 +0200 | [diff] [blame] | 94 | (void)pto_tmp__; \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 95 | } \ |
| 96 | switch (sizeof(var)) { \ |
| 97 | case 1: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 98 | asm(op "b %1,"__percpu_arg(0) \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 99 | : "+m" (var) \ |
Tejun Heo | 0f5e481 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 100 | : "qi" ((pto_T__)(val))); \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 101 | break; \ |
| 102 | case 2: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 103 | asm(op "w %1,"__percpu_arg(0) \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 104 | : "+m" (var) \ |
Tejun Heo | 0f5e481 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 105 | : "ri" ((pto_T__)(val))); \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 106 | break; \ |
| 107 | case 4: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 108 | asm(op "l %1,"__percpu_arg(0) \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 109 | : "+m" (var) \ |
Tejun Heo | 0f5e481 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 110 | : "ri" ((pto_T__)(val))); \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 111 | break; \ |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 112 | case 8: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 113 | asm(op "q %1,"__percpu_arg(0) \ |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 114 | : "+m" (var) \ |
Tejun Heo | 0f5e481 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 115 | : "re" ((pto_T__)(val))); \ |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 116 | break; \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 117 | default: __bad_percpu_size(); \ |
| 118 | } \ |
| 119 | } while (0) |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 120 | |
Christoph Lameter | 5917dae | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 121 | /* |
| 122 | * Generate a percpu add to memory instruction and optimize code |
Justin P. Mattock | 40f0a5d | 2010-04-19 11:51:16 -0700 | [diff] [blame] | 123 | * if one is added or subtracted. |
Christoph Lameter | 5917dae | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 124 | */ |
| 125 | #define percpu_add_op(var, val) \ |
| 126 | do { \ |
| 127 | typedef typeof(var) pao_T__; \ |
| 128 | const int pao_ID__ = (__builtin_constant_p(val) && \ |
| 129 | ((val) == 1 || (val) == -1)) ? (val) : 0; \ |
| 130 | if (0) { \ |
| 131 | pao_T__ pao_tmp__; \ |
| 132 | pao_tmp__ = (val); \ |
Andi Kleen | 23b764d | 2010-06-10 13:10:36 +0200 | [diff] [blame] | 133 | (void)pao_tmp__; \ |
Christoph Lameter | 5917dae | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 134 | } \ |
| 135 | switch (sizeof(var)) { \ |
| 136 | case 1: \ |
| 137 | if (pao_ID__ == 1) \ |
| 138 | asm("incb "__percpu_arg(0) : "+m" (var)); \ |
| 139 | else if (pao_ID__ == -1) \ |
| 140 | asm("decb "__percpu_arg(0) : "+m" (var)); \ |
| 141 | else \ |
| 142 | asm("addb %1, "__percpu_arg(0) \ |
| 143 | : "+m" (var) \ |
| 144 | : "qi" ((pao_T__)(val))); \ |
| 145 | break; \ |
| 146 | case 2: \ |
| 147 | if (pao_ID__ == 1) \ |
| 148 | asm("incw "__percpu_arg(0) : "+m" (var)); \ |
| 149 | else if (pao_ID__ == -1) \ |
| 150 | asm("decw "__percpu_arg(0) : "+m" (var)); \ |
| 151 | else \ |
| 152 | asm("addw %1, "__percpu_arg(0) \ |
| 153 | : "+m" (var) \ |
| 154 | : "ri" ((pao_T__)(val))); \ |
| 155 | break; \ |
| 156 | case 4: \ |
| 157 | if (pao_ID__ == 1) \ |
| 158 | asm("incl "__percpu_arg(0) : "+m" (var)); \ |
| 159 | else if (pao_ID__ == -1) \ |
| 160 | asm("decl "__percpu_arg(0) : "+m" (var)); \ |
| 161 | else \ |
| 162 | asm("addl %1, "__percpu_arg(0) \ |
| 163 | : "+m" (var) \ |
| 164 | : "ri" ((pao_T__)(val))); \ |
| 165 | break; \ |
| 166 | case 8: \ |
| 167 | if (pao_ID__ == 1) \ |
| 168 | asm("incq "__percpu_arg(0) : "+m" (var)); \ |
| 169 | else if (pao_ID__ == -1) \ |
| 170 | asm("decq "__percpu_arg(0) : "+m" (var)); \ |
| 171 | else \ |
| 172 | asm("addq %1, "__percpu_arg(0) \ |
| 173 | : "+m" (var) \ |
| 174 | : "re" ((pao_T__)(val))); \ |
| 175 | break; \ |
| 176 | default: __bad_percpu_size(); \ |
| 177 | } \ |
| 178 | } while (0) |
| 179 | |
Linus Torvalds | ed8d9ad | 2009-08-03 14:08:48 +0900 | [diff] [blame] | 180 | #define percpu_from_op(op, var, constraint) \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 181 | ({ \ |
Tejun Heo | 0f5e481 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 182 | typeof(var) pfo_ret__; \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 183 | switch (sizeof(var)) { \ |
| 184 | case 1: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 185 | asm(op "b "__percpu_arg(1)",%0" \ |
Tejun Heo | 0f5e481 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 186 | : "=q" (pfo_ret__) \ |
Linus Torvalds | ed8d9ad | 2009-08-03 14:08:48 +0900 | [diff] [blame] | 187 | : constraint); \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 188 | break; \ |
| 189 | case 2: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 190 | asm(op "w "__percpu_arg(1)",%0" \ |
Tejun Heo | 0f5e481 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 191 | : "=r" (pfo_ret__) \ |
Linus Torvalds | ed8d9ad | 2009-08-03 14:08:48 +0900 | [diff] [blame] | 192 | : constraint); \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 193 | break; \ |
| 194 | case 4: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 195 | asm(op "l "__percpu_arg(1)",%0" \ |
Tejun Heo | 0f5e481 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 196 | : "=r" (pfo_ret__) \ |
Linus Torvalds | ed8d9ad | 2009-08-03 14:08:48 +0900 | [diff] [blame] | 197 | : constraint); \ |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 198 | break; \ |
| 199 | case 8: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 200 | asm(op "q "__percpu_arg(1)",%0" \ |
Tejun Heo | 0f5e481 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 201 | : "=r" (pfo_ret__) \ |
Linus Torvalds | ed8d9ad | 2009-08-03 14:08:48 +0900 | [diff] [blame] | 202 | : constraint); \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 203 | break; \ |
| 204 | default: __bad_percpu_size(); \ |
| 205 | } \ |
Tejun Heo | 0f5e481 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 206 | pfo_ret__; \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 207 | }) |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 208 | |
Jan Beulich | 402af0d | 2010-04-21 15:21:51 +0100 | [diff] [blame] | 209 | #define percpu_unary_op(op, var) \ |
| 210 | ({ \ |
| 211 | switch (sizeof(var)) { \ |
| 212 | case 1: \ |
| 213 | asm(op "b "__percpu_arg(0) \ |
| 214 | : "+m" (var)); \ |
| 215 | break; \ |
| 216 | case 2: \ |
| 217 | asm(op "w "__percpu_arg(0) \ |
| 218 | : "+m" (var)); \ |
| 219 | break; \ |
| 220 | case 4: \ |
| 221 | asm(op "l "__percpu_arg(0) \ |
| 222 | : "+m" (var)); \ |
| 223 | break; \ |
| 224 | case 8: \ |
| 225 | asm(op "q "__percpu_arg(0) \ |
| 226 | : "+m" (var)); \ |
| 227 | break; \ |
| 228 | default: __bad_percpu_size(); \ |
| 229 | } \ |
| 230 | }) |
| 231 | |
Linus Torvalds | ed8d9ad | 2009-08-03 14:08:48 +0900 | [diff] [blame] | 232 | /* |
Tejun Heo | 4030477 | 2010-12-17 15:47:04 +0100 | [diff] [blame] | 233 | * Add return operation |
| 234 | */ |
| 235 | #define percpu_add_return_op(var, val) \ |
| 236 | ({ \ |
| 237 | typeof(var) paro_ret__ = val; \ |
| 238 | switch (sizeof(var)) { \ |
| 239 | case 1: \ |
| 240 | asm("xaddb %0, "__percpu_arg(1) \ |
| 241 | : "+q" (paro_ret__), "+m" (var) \ |
| 242 | : : "memory"); \ |
| 243 | break; \ |
| 244 | case 2: \ |
| 245 | asm("xaddw %0, "__percpu_arg(1) \ |
| 246 | : "+r" (paro_ret__), "+m" (var) \ |
| 247 | : : "memory"); \ |
| 248 | break; \ |
| 249 | case 4: \ |
| 250 | asm("xaddl %0, "__percpu_arg(1) \ |
| 251 | : "+r" (paro_ret__), "+m" (var) \ |
| 252 | : : "memory"); \ |
| 253 | break; \ |
| 254 | case 8: \ |
| 255 | asm("xaddq %0, "__percpu_arg(1) \ |
| 256 | : "+re" (paro_ret__), "+m" (var) \ |
| 257 | : : "memory"); \ |
| 258 | break; \ |
| 259 | default: __bad_percpu_size(); \ |
| 260 | } \ |
| 261 | paro_ret__ += val; \ |
| 262 | paro_ret__; \ |
| 263 | }) |
| 264 | |
| 265 | /* |
Christoph Lameter | 8270137 | 2010-12-14 10:28:47 -0600 | [diff] [blame] | 266 | * xchg is implemented using cmpxchg without a lock prefix. xchg is |
| 267 | * expensive due to the implied lock prefix. The processor cannot prefetch |
| 268 | * cachelines if xchg is used. |
Christoph Lameter | 7296e08 | 2010-12-14 10:28:44 -0600 | [diff] [blame] | 269 | */ |
| 270 | #define percpu_xchg_op(var, nval) \ |
| 271 | ({ \ |
| 272 | typeof(var) pxo_ret__; \ |
| 273 | typeof(var) pxo_new__ = (nval); \ |
| 274 | switch (sizeof(var)) { \ |
| 275 | case 1: \ |
Christoph Lameter | 8270137 | 2010-12-14 10:28:47 -0600 | [diff] [blame] | 276 | asm("\n1:mov "__percpu_arg(1)",%%al" \ |
| 277 | "\n\tcmpxchgb %2, "__percpu_arg(1) \ |
| 278 | "\n\tjnz 1b" \ |
Christoph Lameter | 7296e08 | 2010-12-14 10:28:44 -0600 | [diff] [blame] | 279 | : "=a" (pxo_ret__), "+m" (var) \ |
| 280 | : "q" (pxo_new__) \ |
| 281 | : "memory"); \ |
| 282 | break; \ |
| 283 | case 2: \ |
Christoph Lameter | 8270137 | 2010-12-14 10:28:47 -0600 | [diff] [blame] | 284 | asm("\n1:mov "__percpu_arg(1)",%%ax" \ |
| 285 | "\n\tcmpxchgw %2, "__percpu_arg(1) \ |
| 286 | "\n\tjnz 1b" \ |
Christoph Lameter | 7296e08 | 2010-12-14 10:28:44 -0600 | [diff] [blame] | 287 | : "=a" (pxo_ret__), "+m" (var) \ |
| 288 | : "r" (pxo_new__) \ |
| 289 | : "memory"); \ |
| 290 | break; \ |
| 291 | case 4: \ |
Christoph Lameter | 8270137 | 2010-12-14 10:28:47 -0600 | [diff] [blame] | 292 | asm("\n1:mov "__percpu_arg(1)",%%eax" \ |
| 293 | "\n\tcmpxchgl %2, "__percpu_arg(1) \ |
| 294 | "\n\tjnz 1b" \ |
Christoph Lameter | 7296e08 | 2010-12-14 10:28:44 -0600 | [diff] [blame] | 295 | : "=a" (pxo_ret__), "+m" (var) \ |
| 296 | : "r" (pxo_new__) \ |
| 297 | : "memory"); \ |
| 298 | break; \ |
| 299 | case 8: \ |
Christoph Lameter | 8270137 | 2010-12-14 10:28:47 -0600 | [diff] [blame] | 300 | asm("\n1:mov "__percpu_arg(1)",%%rax" \ |
| 301 | "\n\tcmpxchgq %2, "__percpu_arg(1) \ |
| 302 | "\n\tjnz 1b" \ |
Christoph Lameter | 7296e08 | 2010-12-14 10:28:44 -0600 | [diff] [blame] | 303 | : "=a" (pxo_ret__), "+m" (var) \ |
| 304 | : "r" (pxo_new__) \ |
| 305 | : "memory"); \ |
| 306 | break; \ |
| 307 | default: __bad_percpu_size(); \ |
| 308 | } \ |
| 309 | pxo_ret__; \ |
| 310 | }) |
| 311 | |
| 312 | /* |
| 313 | * cmpxchg has no such implied lock semantics as a result it is much |
| 314 | * more efficient for cpu local operations. |
| 315 | */ |
| 316 | #define percpu_cmpxchg_op(var, oval, nval) \ |
| 317 | ({ \ |
| 318 | typeof(var) pco_ret__; \ |
| 319 | typeof(var) pco_old__ = (oval); \ |
| 320 | typeof(var) pco_new__ = (nval); \ |
| 321 | switch (sizeof(var)) { \ |
| 322 | case 1: \ |
| 323 | asm("cmpxchgb %2, "__percpu_arg(1) \ |
| 324 | : "=a" (pco_ret__), "+m" (var) \ |
| 325 | : "q" (pco_new__), "0" (pco_old__) \ |
| 326 | : "memory"); \ |
| 327 | break; \ |
| 328 | case 2: \ |
| 329 | asm("cmpxchgw %2, "__percpu_arg(1) \ |
| 330 | : "=a" (pco_ret__), "+m" (var) \ |
| 331 | : "r" (pco_new__), "0" (pco_old__) \ |
| 332 | : "memory"); \ |
| 333 | break; \ |
| 334 | case 4: \ |
| 335 | asm("cmpxchgl %2, "__percpu_arg(1) \ |
| 336 | : "=a" (pco_ret__), "+m" (var) \ |
| 337 | : "r" (pco_new__), "0" (pco_old__) \ |
| 338 | : "memory"); \ |
| 339 | break; \ |
| 340 | case 8: \ |
| 341 | asm("cmpxchgq %2, "__percpu_arg(1) \ |
| 342 | : "=a" (pco_ret__), "+m" (var) \ |
| 343 | : "r" (pco_new__), "0" (pco_old__) \ |
| 344 | : "memory"); \ |
| 345 | break; \ |
| 346 | default: __bad_percpu_size(); \ |
| 347 | } \ |
| 348 | pco_ret__; \ |
| 349 | }) |
| 350 | |
| 351 | /* |
Linus Torvalds | ed8d9ad | 2009-08-03 14:08:48 +0900 | [diff] [blame] | 352 | * percpu_read() makes gcc load the percpu variable every time it is |
| 353 | * accessed while percpu_read_stable() allows the value to be cached. |
| 354 | * percpu_read_stable() is more efficient and can be used if its value |
| 355 | * is guaranteed to be valid across cpus. The current users include |
| 356 | * get_current() and get_thread_info() both of which are actually |
| 357 | * per-thread variables implemented as per-cpu variables and thus |
| 358 | * stable for the duration of the respective task. |
| 359 | */ |
Rusty Russell | dd17c8f | 2009-10-29 22:34:15 +0900 | [diff] [blame] | 360 | #define percpu_read(var) percpu_from_op("mov", var, "m" (var)) |
| 361 | #define percpu_read_stable(var) percpu_from_op("mov", var, "p" (&(var))) |
| 362 | #define percpu_write(var, val) percpu_to_op("mov", var, val) |
Christoph Lameter | 5917dae | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 363 | #define percpu_add(var, val) percpu_add_op(var, val) |
| 364 | #define percpu_sub(var, val) percpu_add_op(var, -(val)) |
Rusty Russell | dd17c8f | 2009-10-29 22:34:15 +0900 | [diff] [blame] | 365 | #define percpu_and(var, val) percpu_to_op("and", var, val) |
| 366 | #define percpu_or(var, val) percpu_to_op("or", var, val) |
| 367 | #define percpu_xor(var, val) percpu_to_op("xor", var, val) |
Jan Beulich | 402af0d | 2010-04-21 15:21:51 +0100 | [diff] [blame] | 368 | #define percpu_inc(var) percpu_unary_op("inc", var) |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 369 | |
Christoph Lameter | 30ed1a7 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 370 | #define __this_cpu_read_1(pcp) percpu_from_op("mov", (pcp), "m"(pcp)) |
| 371 | #define __this_cpu_read_2(pcp) percpu_from_op("mov", (pcp), "m"(pcp)) |
| 372 | #define __this_cpu_read_4(pcp) percpu_from_op("mov", (pcp), "m"(pcp)) |
| 373 | |
| 374 | #define __this_cpu_write_1(pcp, val) percpu_to_op("mov", (pcp), val) |
| 375 | #define __this_cpu_write_2(pcp, val) percpu_to_op("mov", (pcp), val) |
| 376 | #define __this_cpu_write_4(pcp, val) percpu_to_op("mov", (pcp), val) |
Christoph Lameter | 5917dae | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 377 | #define __this_cpu_add_1(pcp, val) percpu_add_op((pcp), val) |
| 378 | #define __this_cpu_add_2(pcp, val) percpu_add_op((pcp), val) |
| 379 | #define __this_cpu_add_4(pcp, val) percpu_add_op((pcp), val) |
Christoph Lameter | 30ed1a7 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 380 | #define __this_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val) |
| 381 | #define __this_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val) |
| 382 | #define __this_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val) |
| 383 | #define __this_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val) |
| 384 | #define __this_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val) |
| 385 | #define __this_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val) |
| 386 | #define __this_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val) |
| 387 | #define __this_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val) |
| 388 | #define __this_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val) |
Christoph Lameter | 7296e08 | 2010-12-14 10:28:44 -0600 | [diff] [blame] | 389 | /* |
| 390 | * Generic fallback operations for __this_cpu_xchg_[1-4] are okay and much |
| 391 | * faster than an xchg with forced lock semantics. |
| 392 | */ |
| 393 | #define __this_cpu_xchg_8(pcp, nval) percpu_xchg_op(pcp, nval) |
| 394 | #define __this_cpu_cmpxchg_8(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval) |
Christoph Lameter | 30ed1a7 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 395 | |
| 396 | #define this_cpu_read_1(pcp) percpu_from_op("mov", (pcp), "m"(pcp)) |
| 397 | #define this_cpu_read_2(pcp) percpu_from_op("mov", (pcp), "m"(pcp)) |
| 398 | #define this_cpu_read_4(pcp) percpu_from_op("mov", (pcp), "m"(pcp)) |
| 399 | #define this_cpu_write_1(pcp, val) percpu_to_op("mov", (pcp), val) |
| 400 | #define this_cpu_write_2(pcp, val) percpu_to_op("mov", (pcp), val) |
| 401 | #define this_cpu_write_4(pcp, val) percpu_to_op("mov", (pcp), val) |
Christoph Lameter | 5917dae | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 402 | #define this_cpu_add_1(pcp, val) percpu_add_op((pcp), val) |
| 403 | #define this_cpu_add_2(pcp, val) percpu_add_op((pcp), val) |
| 404 | #define this_cpu_add_4(pcp, val) percpu_add_op((pcp), val) |
Christoph Lameter | 30ed1a7 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 405 | #define this_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val) |
| 406 | #define this_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val) |
| 407 | #define this_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val) |
| 408 | #define this_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val) |
| 409 | #define this_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val) |
| 410 | #define this_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val) |
| 411 | #define this_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val) |
| 412 | #define this_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val) |
| 413 | #define this_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val) |
Christoph Lameter | 7296e08 | 2010-12-14 10:28:44 -0600 | [diff] [blame] | 414 | #define this_cpu_xchg_1(pcp, nval) percpu_xchg_op(pcp, nval) |
| 415 | #define this_cpu_xchg_2(pcp, nval) percpu_xchg_op(pcp, nval) |
| 416 | #define this_cpu_xchg_4(pcp, nval) percpu_xchg_op(pcp, nval) |
Christoph Lameter | 30ed1a7 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 417 | |
Christoph Lameter | 5917dae | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 418 | #define irqsafe_cpu_add_1(pcp, val) percpu_add_op((pcp), val) |
| 419 | #define irqsafe_cpu_add_2(pcp, val) percpu_add_op((pcp), val) |
| 420 | #define irqsafe_cpu_add_4(pcp, val) percpu_add_op((pcp), val) |
Christoph Lameter | 30ed1a7 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 421 | #define irqsafe_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val) |
| 422 | #define irqsafe_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val) |
| 423 | #define irqsafe_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val) |
| 424 | #define irqsafe_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val) |
| 425 | #define irqsafe_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val) |
| 426 | #define irqsafe_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val) |
| 427 | #define irqsafe_cpu_xor_1(pcp, val) percpu_to_op("xor", (pcp), val) |
| 428 | #define irqsafe_cpu_xor_2(pcp, val) percpu_to_op("xor", (pcp), val) |
| 429 | #define irqsafe_cpu_xor_4(pcp, val) percpu_to_op("xor", (pcp), val) |
Christoph Lameter | 7296e08 | 2010-12-14 10:28:44 -0600 | [diff] [blame] | 430 | #define irqsafe_cpu_xchg_1(pcp, nval) percpu_xchg_op(pcp, nval) |
| 431 | #define irqsafe_cpu_xchg_2(pcp, nval) percpu_xchg_op(pcp, nval) |
| 432 | #define irqsafe_cpu_xchg_4(pcp, nval) percpu_xchg_op(pcp, nval) |
Christoph Lameter | 30ed1a7 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 433 | |
Christoph Lameter | 8f1d97c | 2010-12-06 11:40:00 -0600 | [diff] [blame] | 434 | #ifndef CONFIG_M386 |
| 435 | #define __this_cpu_add_return_1(pcp, val) percpu_add_return_op(pcp, val) |
| 436 | #define __this_cpu_add_return_2(pcp, val) percpu_add_return_op(pcp, val) |
| 437 | #define __this_cpu_add_return_4(pcp, val) percpu_add_return_op(pcp, val) |
Christoph Lameter | 7296e08 | 2010-12-14 10:28:44 -0600 | [diff] [blame] | 438 | #define __this_cpu_cmpxchg_1(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval) |
| 439 | #define __this_cpu_cmpxchg_2(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval) |
| 440 | #define __this_cpu_cmpxchg_4(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval) |
| 441 | |
Christoph Lameter | 8f1d97c | 2010-12-06 11:40:00 -0600 | [diff] [blame] | 442 | #define this_cpu_add_return_1(pcp, val) percpu_add_return_op(pcp, val) |
| 443 | #define this_cpu_add_return_2(pcp, val) percpu_add_return_op(pcp, val) |
| 444 | #define this_cpu_add_return_4(pcp, val) percpu_add_return_op(pcp, val) |
Christoph Lameter | 7296e08 | 2010-12-14 10:28:44 -0600 | [diff] [blame] | 445 | #define this_cpu_cmpxchg_1(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval) |
| 446 | #define this_cpu_cmpxchg_2(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval) |
| 447 | #define this_cpu_cmpxchg_4(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval) |
| 448 | |
| 449 | #define irqsafe_cpu_cmpxchg_1(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval) |
| 450 | #define irqsafe_cpu_cmpxchg_2(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval) |
| 451 | #define irqsafe_cpu_cmpxchg_4(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval) |
| 452 | #endif /* !CONFIG_M386 */ |
| 453 | |
Christoph Lameter | 30ed1a7 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 454 | /* |
| 455 | * Per cpu atomic 64 bit operations are only available under 64 bit. |
| 456 | * 32 bit must fall back to generic operations. |
| 457 | */ |
| 458 | #ifdef CONFIG_X86_64 |
| 459 | #define __this_cpu_read_8(pcp) percpu_from_op("mov", (pcp), "m"(pcp)) |
| 460 | #define __this_cpu_write_8(pcp, val) percpu_to_op("mov", (pcp), val) |
Christoph Lameter | 5917dae | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 461 | #define __this_cpu_add_8(pcp, val) percpu_add_op((pcp), val) |
Christoph Lameter | 30ed1a7 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 462 | #define __this_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val) |
| 463 | #define __this_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val) |
| 464 | #define __this_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val) |
Tejun Heo | 4030477 | 2010-12-17 15:47:04 +0100 | [diff] [blame] | 465 | #define __this_cpu_add_return_8(pcp, val) percpu_add_return_op(pcp, val) |
Christoph Lameter | 30ed1a7 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 466 | |
| 467 | #define this_cpu_read_8(pcp) percpu_from_op("mov", (pcp), "m"(pcp)) |
| 468 | #define this_cpu_write_8(pcp, val) percpu_to_op("mov", (pcp), val) |
Christoph Lameter | 5917dae | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 469 | #define this_cpu_add_8(pcp, val) percpu_add_op((pcp), val) |
Christoph Lameter | 30ed1a7 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 470 | #define this_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val) |
| 471 | #define this_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val) |
| 472 | #define this_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val) |
Tejun Heo | 4030477 | 2010-12-17 15:47:04 +0100 | [diff] [blame] | 473 | #define this_cpu_add_return_8(pcp, val) percpu_add_return_op(pcp, val) |
Christoph Lameter | 2485b64 | 2011-01-11 18:54:53 +0100 | [diff] [blame] | 474 | #define this_cpu_xchg_8(pcp, nval) percpu_xchg_op(pcp, nval) |
| 475 | #define this_cpu_cmpxchg_8(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval) |
Christoph Lameter | 30ed1a7 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 476 | |
Christoph Lameter | 5917dae | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 477 | #define irqsafe_cpu_add_8(pcp, val) percpu_add_op((pcp), val) |
Christoph Lameter | 30ed1a7 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 478 | #define irqsafe_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val) |
| 479 | #define irqsafe_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val) |
| 480 | #define irqsafe_cpu_xor_8(pcp, val) percpu_to_op("xor", (pcp), val) |
Christoph Lameter | 2485b64 | 2011-01-11 18:54:53 +0100 | [diff] [blame] | 481 | #define irqsafe_cpu_xchg_8(pcp, nval) percpu_xchg_op(pcp, nval) |
| 482 | #define irqsafe_cpu_cmpxchg_8(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval) |
Christoph Lameter | 30ed1a7 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 483 | #endif |
| 484 | |
Tejun Heo | 49357d1 | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 485 | /* This is not atomic against other CPUs -- CPU preemption needs to be off */ |
| 486 | #define x86_test_and_clear_bit_percpu(bit, var) \ |
| 487 | ({ \ |
| 488 | int old__; \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 489 | asm volatile("btr %2,"__percpu_arg(1)"\n\tsbbl %0,%0" \ |
Rusty Russell | dd17c8f | 2009-10-29 22:34:15 +0900 | [diff] [blame] | 490 | : "=r" (old__), "+m" (var) \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 491 | : "dIr" (bit)); \ |
Tejun Heo | 49357d1 | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 492 | old__; \ |
| 493 | }) |
| 494 | |
Ingo Molnar | 6dbde35 | 2009-01-15 22:15:53 +0900 | [diff] [blame] | 495 | #include <asm-generic/percpu.h> |
| 496 | |
| 497 | /* We can use this directly for local CPU (faster). */ |
| 498 | DECLARE_PER_CPU(unsigned long, this_cpu_off); |
| 499 | |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 500 | #endif /* !__ASSEMBLY__ */ |
Mike Travis | 23ca4bb | 2008-05-12 21:21:12 +0200 | [diff] [blame] | 501 | |
| 502 | #ifdef CONFIG_SMP |
| 503 | |
| 504 | /* |
| 505 | * Define the "EARLY_PER_CPU" macros. These are used for some per_cpu |
| 506 | * variables that are initialized and accessed before there are per_cpu |
| 507 | * areas allocated. |
| 508 | */ |
| 509 | |
| 510 | #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \ |
| 511 | DEFINE_PER_CPU(_type, _name) = _initvalue; \ |
| 512 | __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \ |
| 513 | { [0 ... NR_CPUS-1] = _initvalue }; \ |
Marcin Slusarz | c6a92a2 | 2008-08-17 17:50:50 +0200 | [diff] [blame] | 514 | __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map |
Mike Travis | 23ca4bb | 2008-05-12 21:21:12 +0200 | [diff] [blame] | 515 | |
| 516 | #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \ |
| 517 | EXPORT_PER_CPU_SYMBOL(_name) |
| 518 | |
| 519 | #define DECLARE_EARLY_PER_CPU(_type, _name) \ |
| 520 | DECLARE_PER_CPU(_type, _name); \ |
| 521 | extern __typeof__(_type) *_name##_early_ptr; \ |
| 522 | extern __typeof__(_type) _name##_early_map[] |
| 523 | |
| 524 | #define early_per_cpu_ptr(_name) (_name##_early_ptr) |
| 525 | #define early_per_cpu_map(_name, _idx) (_name##_early_map[_idx]) |
| 526 | #define early_per_cpu(_name, _cpu) \ |
Tejun Heo | f10fcd47 | 2009-01-13 20:41:34 +0900 | [diff] [blame] | 527 | *(early_per_cpu_ptr(_name) ? \ |
| 528 | &early_per_cpu_ptr(_name)[_cpu] : \ |
| 529 | &per_cpu(_name, _cpu)) |
Mike Travis | 23ca4bb | 2008-05-12 21:21:12 +0200 | [diff] [blame] | 530 | |
| 531 | #else /* !CONFIG_SMP */ |
| 532 | #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \ |
| 533 | DEFINE_PER_CPU(_type, _name) = _initvalue |
| 534 | |
| 535 | #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \ |
| 536 | EXPORT_PER_CPU_SYMBOL(_name) |
| 537 | |
| 538 | #define DECLARE_EARLY_PER_CPU(_type, _name) \ |
| 539 | DECLARE_PER_CPU(_type, _name) |
| 540 | |
| 541 | #define early_per_cpu(_name, _cpu) per_cpu(_name, _cpu) |
| 542 | #define early_per_cpu_ptr(_name) NULL |
| 543 | /* no early_per_cpu_map() */ |
| 544 | |
| 545 | #endif /* !CONFIG_SMP */ |
| 546 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 547 | #endif /* _ASM_X86_PERCPU_H */ |