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) \ |
| 28 | __percpu_mov_op %__percpu_seg:per_cpu__this_cpu_off, reg; \ |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 29 | lea per_cpu__##var(reg), reg |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 30 | #define PER_CPU_VAR(var) %__percpu_seg:per_cpu__##var |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 31 | #else /* ! SMP */ |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 32 | #define PER_CPU(var, reg) \ |
| 33 | __percpu_mov_op $per_cpu__##var, reg |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 34 | #define PER_CPU_VAR(var) per_cpu__##var |
| 35 | #endif /* SMP */ |
| 36 | |
Brian Gerst | 2add8e2 | 2009-02-08 09:58:39 -0500 | [diff] [blame] | 37 | #ifdef CONFIG_X86_64_SMP |
| 38 | #define INIT_PER_CPU_VAR(var) init_per_cpu__##var |
| 39 | #else |
| 40 | #define INIT_PER_CPU_VAR(var) per_cpu__##var |
| 41 | #endif |
| 42 | |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 43 | #else /* ...!ASSEMBLY */ |
| 44 | |
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) |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 50 | #else |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 51 | #define __percpu_arg(x) "%" #x |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 52 | #endif |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 53 | |
Brian Gerst | 2add8e2 | 2009-02-08 09:58:39 -0500 | [diff] [blame] | 54 | /* |
| 55 | * Initialized pointers to per-cpu variables needed for the boot |
| 56 | * processor need to use these macros to get the proper address |
| 57 | * offset from __per_cpu_load on SMP. |
| 58 | * |
| 59 | * There also must be an entry in vmlinux_64.lds.S |
| 60 | */ |
| 61 | #define DECLARE_INIT_PER_CPU(var) \ |
| 62 | extern typeof(per_cpu_var(var)) init_per_cpu_var(var) |
| 63 | |
| 64 | #ifdef CONFIG_X86_64_SMP |
| 65 | #define init_per_cpu_var(var) init_per_cpu__##var |
| 66 | #else |
| 67 | #define init_per_cpu_var(var) per_cpu_var(var) |
| 68 | #endif |
| 69 | |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 70 | /* For arch-specific code, we can use direct single-insn ops (they |
| 71 | * don't give an lvalue though). */ |
| 72 | extern void __bad_percpu_size(void); |
| 73 | |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 74 | #define percpu_to_op(op, var, val) \ |
| 75 | do { \ |
| 76 | typedef typeof(var) T__; \ |
| 77 | if (0) { \ |
| 78 | T__ tmp__; \ |
| 79 | tmp__ = (val); \ |
| 80 | } \ |
| 81 | switch (sizeof(var)) { \ |
| 82 | case 1: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 83 | asm(op "b %1,"__percpu_arg(0) \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 84 | : "+m" (var) \ |
| 85 | : "ri" ((T__)val)); \ |
| 86 | break; \ |
| 87 | case 2: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 88 | asm(op "w %1,"__percpu_arg(0) \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 89 | : "+m" (var) \ |
| 90 | : "ri" ((T__)val)); \ |
| 91 | break; \ |
| 92 | case 4: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 93 | asm(op "l %1,"__percpu_arg(0) \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 94 | : "+m" (var) \ |
| 95 | : "ri" ((T__)val)); \ |
| 96 | break; \ |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 97 | case 8: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 98 | asm(op "q %1,"__percpu_arg(0) \ |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 99 | : "+m" (var) \ |
Brian Gerst | 299e269 | 2009-01-21 17:26:05 +0900 | [diff] [blame] | 100 | : "re" ((T__)val)); \ |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 101 | break; \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 102 | default: __bad_percpu_size(); \ |
| 103 | } \ |
| 104 | } while (0) |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 105 | |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 106 | #define percpu_from_op(op, var) \ |
| 107 | ({ \ |
| 108 | typeof(var) ret__; \ |
| 109 | switch (sizeof(var)) { \ |
| 110 | case 1: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 111 | asm(op "b "__percpu_arg(1)",%0" \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 112 | : "=r" (ret__) \ |
| 113 | : "m" (var)); \ |
| 114 | break; \ |
| 115 | case 2: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 116 | asm(op "w "__percpu_arg(1)",%0" \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 117 | : "=r" (ret__) \ |
| 118 | : "m" (var)); \ |
| 119 | break; \ |
| 120 | case 4: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 121 | asm(op "l "__percpu_arg(1)",%0" \ |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 122 | : "=r" (ret__) \ |
| 123 | : "m" (var)); \ |
| 124 | break; \ |
| 125 | case 8: \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 126 | asm(op "q "__percpu_arg(1)",%0" \ |
Joe Perches | bc9e3be | 2008-03-23 01:03:06 -0700 | [diff] [blame] | 127 | : "=r" (ret__) \ |
| 128 | : "m" (var)); \ |
| 129 | break; \ |
| 130 | default: __bad_percpu_size(); \ |
| 131 | } \ |
| 132 | ret__; \ |
| 133 | }) |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 134 | |
Ingo Molnar | 6dbde35 | 2009-01-15 22:15:53 +0900 | [diff] [blame] | 135 | #define percpu_read(var) percpu_from_op("mov", per_cpu__##var) |
| 136 | #define percpu_write(var, val) percpu_to_op("mov", per_cpu__##var, val) |
| 137 | #define percpu_add(var, val) percpu_to_op("add", per_cpu__##var, val) |
| 138 | #define percpu_sub(var, val) percpu_to_op("sub", per_cpu__##var, val) |
| 139 | #define percpu_and(var, val) percpu_to_op("and", per_cpu__##var, val) |
| 140 | #define percpu_or(var, val) percpu_to_op("or", per_cpu__##var, val) |
| 141 | #define percpu_xor(var, val) percpu_to_op("xor", per_cpu__##var, val) |
Tejun Heo | 9939dda | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 142 | |
Tejun Heo | 49357d1 | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 143 | /* This is not atomic against other CPUs -- CPU preemption needs to be off */ |
| 144 | #define x86_test_and_clear_bit_percpu(bit, var) \ |
| 145 | ({ \ |
| 146 | int old__; \ |
Brian Gerst | 87b2640 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 147 | asm volatile("btr %2,"__percpu_arg(1)"\n\tsbbl %0,%0" \ |
| 148 | : "=r" (old__), "+m" (per_cpu__##var) \ |
| 149 | : "dIr" (bit)); \ |
Tejun Heo | 49357d1 | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 150 | old__; \ |
| 151 | }) |
| 152 | |
Ingo Molnar | 6dbde35 | 2009-01-15 22:15:53 +0900 | [diff] [blame] | 153 | #include <asm-generic/percpu.h> |
| 154 | |
| 155 | /* We can use this directly for local CPU (faster). */ |
| 156 | DECLARE_PER_CPU(unsigned long, this_cpu_off); |
| 157 | |
travis@sgi.com | 3334052 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 158 | #endif /* !__ASSEMBLY__ */ |
Mike Travis | 23ca4bb | 2008-05-12 21:21:12 +0200 | [diff] [blame] | 159 | |
| 160 | #ifdef CONFIG_SMP |
| 161 | |
| 162 | /* |
| 163 | * Define the "EARLY_PER_CPU" macros. These are used for some per_cpu |
| 164 | * variables that are initialized and accessed before there are per_cpu |
| 165 | * areas allocated. |
| 166 | */ |
| 167 | |
| 168 | #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \ |
| 169 | DEFINE_PER_CPU(_type, _name) = _initvalue; \ |
| 170 | __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \ |
| 171 | { [0 ... NR_CPUS-1] = _initvalue }; \ |
Marcin Slusarz | c6a92a2 | 2008-08-17 17:50:50 +0200 | [diff] [blame] | 172 | __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map |
Mike Travis | 23ca4bb | 2008-05-12 21:21:12 +0200 | [diff] [blame] | 173 | |
| 174 | #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \ |
| 175 | EXPORT_PER_CPU_SYMBOL(_name) |
| 176 | |
| 177 | #define DECLARE_EARLY_PER_CPU(_type, _name) \ |
| 178 | DECLARE_PER_CPU(_type, _name); \ |
| 179 | extern __typeof__(_type) *_name##_early_ptr; \ |
| 180 | extern __typeof__(_type) _name##_early_map[] |
| 181 | |
| 182 | #define early_per_cpu_ptr(_name) (_name##_early_ptr) |
| 183 | #define early_per_cpu_map(_name, _idx) (_name##_early_map[_idx]) |
| 184 | #define early_per_cpu(_name, _cpu) \ |
Tejun Heo | f10fcd4 | 2009-01-13 20:41:34 +0900 | [diff] [blame] | 185 | *(early_per_cpu_ptr(_name) ? \ |
| 186 | &early_per_cpu_ptr(_name)[_cpu] : \ |
| 187 | &per_cpu(_name, _cpu)) |
Mike Travis | 23ca4bb | 2008-05-12 21:21:12 +0200 | [diff] [blame] | 188 | |
| 189 | #else /* !CONFIG_SMP */ |
| 190 | #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \ |
| 191 | DEFINE_PER_CPU(_type, _name) = _initvalue |
| 192 | |
| 193 | #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \ |
| 194 | EXPORT_PER_CPU_SYMBOL(_name) |
| 195 | |
| 196 | #define DECLARE_EARLY_PER_CPU(_type, _name) \ |
| 197 | DECLARE_PER_CPU(_type, _name) |
| 198 | |
| 199 | #define early_per_cpu(_name, _cpu) per_cpu(_name, _cpu) |
| 200 | #define early_per_cpu_ptr(_name) NULL |
| 201 | /* no early_per_cpu_map() */ |
| 202 | |
| 203 | #endif /* !CONFIG_SMP */ |
| 204 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 205 | #endif /* _ASM_X86_PERCPU_H */ |