percpu.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. #ifndef _ASM_X86_PERCPU_H
  2. #define _ASM_X86_PERCPU_H
  3. #ifdef CONFIG_X86_64
  4. #define __percpu_seg gs
  5. #define __percpu_mov_op movq
  6. #else
  7. #define __percpu_seg fs
  8. #define __percpu_mov_op movl
  9. #endif
  10. #ifdef __ASSEMBLY__
  11. /*
  12. * PER_CPU finds an address of a per-cpu variable.
  13. *
  14. * Args:
  15. * var - variable name
  16. * reg - 32bit register
  17. *
  18. * The resulting address is stored in the "reg" argument.
  19. *
  20. * Example:
  21. * PER_CPU(cpu_gdt_descr, %ebx)
  22. */
  23. #ifdef CONFIG_SMP
  24. #define PER_CPU(var, reg) \
  25. __percpu_mov_op %__percpu_seg:this_cpu_off, reg; \
  26. lea var(reg), reg
  27. #define PER_CPU_VAR(var) %__percpu_seg:var
  28. #else /* ! SMP */
  29. #define PER_CPU(var, reg) __percpu_mov_op $var, reg
  30. #define PER_CPU_VAR(var) var
  31. #endif /* SMP */
  32. #ifdef CONFIG_X86_64_SMP
  33. #define INIT_PER_CPU_VAR(var) init_per_cpu__##var
  34. #else
  35. #define INIT_PER_CPU_VAR(var) var
  36. #endif
  37. #else /* ...!ASSEMBLY */
  38. #include <linux/kernel.h>
  39. #include <linux/stringify.h>
  40. #ifdef CONFIG_SMP
  41. #define __percpu_prefix "%%"__stringify(__percpu_seg)":"
  42. #define __my_cpu_offset this_cpu_read(this_cpu_off)
  43. /*
  44. * Compared to the generic __my_cpu_offset version, the following
  45. * saves one instruction and avoids clobbering a temp register.
  46. */
  47. #define arch_raw_cpu_ptr(ptr) \
  48. ({ \
  49. unsigned long tcp_ptr__; \
  50. asm volatile("add " __percpu_arg(1) ", %0" \
  51. : "=r" (tcp_ptr__) \
  52. : "m" (this_cpu_off), "0" (ptr)); \
  53. (typeof(*(ptr)) __kernel __force *)tcp_ptr__; \
  54. })
  55. #else
  56. #define __percpu_prefix ""
  57. #endif
  58. #define __percpu_arg(x) __percpu_prefix "%" #x
  59. /*
  60. * Initialized pointers to per-cpu variables needed for the boot
  61. * processor need to use these macros to get the proper address
  62. * offset from __per_cpu_load on SMP.
  63. *
  64. * There also must be an entry in vmlinux_64.lds.S
  65. */
  66. #define DECLARE_INIT_PER_CPU(var) \
  67. extern typeof(var) init_per_cpu_var(var)
  68. #ifdef CONFIG_X86_64_SMP
  69. #define init_per_cpu_var(var) init_per_cpu__##var
  70. #else
  71. #define init_per_cpu_var(var) var
  72. #endif
  73. /* For arch-specific code, we can use direct single-insn ops (they
  74. * don't give an lvalue though). */
  75. extern void __bad_percpu_size(void);
  76. #define percpu_to_op(op, var, val) \
  77. do { \
  78. typedef typeof(var) pto_T__; \
  79. if (0) { \
  80. pto_T__ pto_tmp__; \
  81. pto_tmp__ = (val); \
  82. (void)pto_tmp__; \
  83. } \
  84. switch (sizeof(var)) { \
  85. case 1: \
  86. asm(op "b %1,"__percpu_arg(0) \
  87. : "+m" (var) \
  88. : "qi" ((pto_T__)(val))); \
  89. break; \
  90. case 2: \
  91. asm(op "w %1,"__percpu_arg(0) \
  92. : "+m" (var) \
  93. : "ri" ((pto_T__)(val))); \
  94. break; \
  95. case 4: \
  96. asm(op "l %1,"__percpu_arg(0) \
  97. : "+m" (var) \
  98. : "ri" ((pto_T__)(val))); \
  99. break; \
  100. case 8: \
  101. asm(op "q %1,"__percpu_arg(0) \
  102. : "+m" (var) \
  103. : "re" ((pto_T__)(val))); \
  104. break; \
  105. default: __bad_percpu_size(); \
  106. } \
  107. } while (0)
  108. /*
  109. * Generate a percpu add to memory instruction and optimize code
  110. * if one is added or subtracted.
  111. */
  112. #define percpu_add_op(var, val) \
  113. do { \
  114. typedef typeof(var) pao_T__; \
  115. const int pao_ID__ = (__builtin_constant_p(val) && \
  116. ((val) == 1 || (val) == -1)) ? \
  117. (int)(val) : 0; \
  118. if (0) { \
  119. pao_T__ pao_tmp__; \
  120. pao_tmp__ = (val); \
  121. (void)pao_tmp__; \
  122. } \
  123. switch (sizeof(var)) { \
  124. case 1: \
  125. if (pao_ID__ == 1) \
  126. asm("incb "__percpu_arg(0) : "+m" (var)); \
  127. else if (pao_ID__ == -1) \
  128. asm("decb "__percpu_arg(0) : "+m" (var)); \
  129. else \
  130. asm("addb %1, "__percpu_arg(0) \
  131. : "+m" (var) \
  132. : "qi" ((pao_T__)(val))); \
  133. break; \
  134. case 2: \
  135. if (pao_ID__ == 1) \
  136. asm("incw "__percpu_arg(0) : "+m" (var)); \
  137. else if (pao_ID__ == -1) \
  138. asm("decw "__percpu_arg(0) : "+m" (var)); \
  139. else \
  140. asm("addw %1, "__percpu_arg(0) \
  141. : "+m" (var) \
  142. : "ri" ((pao_T__)(val))); \
  143. break; \
  144. case 4: \
  145. if (pao_ID__ == 1) \
  146. asm("incl "__percpu_arg(0) : "+m" (var)); \
  147. else if (pao_ID__ == -1) \
  148. asm("decl "__percpu_arg(0) : "+m" (var)); \
  149. else \
  150. asm("addl %1, "__percpu_arg(0) \
  151. : "+m" (var) \
  152. : "ri" ((pao_T__)(val))); \
  153. break; \
  154. case 8: \
  155. if (pao_ID__ == 1) \
  156. asm("incq "__percpu_arg(0) : "+m" (var)); \
  157. else if (pao_ID__ == -1) \
  158. asm("decq "__percpu_arg(0) : "+m" (var)); \
  159. else \
  160. asm("addq %1, "__percpu_arg(0) \
  161. : "+m" (var) \
  162. : "re" ((pao_T__)(val))); \
  163. break; \
  164. default: __bad_percpu_size(); \
  165. } \
  166. } while (0)
  167. #define percpu_from_op(op, var) \
  168. ({ \
  169. typeof(var) pfo_ret__; \
  170. switch (sizeof(var)) { \
  171. case 1: \
  172. asm(op "b "__percpu_arg(1)",%0" \
  173. : "=q" (pfo_ret__) \
  174. : "m" (var)); \
  175. break; \
  176. case 2: \
  177. asm(op "w "__percpu_arg(1)",%0" \
  178. : "=r" (pfo_ret__) \
  179. : "m" (var)); \
  180. break; \
  181. case 4: \
  182. asm(op "l "__percpu_arg(1)",%0" \
  183. : "=r" (pfo_ret__) \
  184. : "m" (var)); \
  185. break; \
  186. case 8: \
  187. asm(op "q "__percpu_arg(1)",%0" \
  188. : "=r" (pfo_ret__) \
  189. : "m" (var)); \
  190. break; \
  191. default: __bad_percpu_size(); \
  192. } \
  193. pfo_ret__; \
  194. })
  195. #define percpu_stable_op(op, var) \
  196. ({ \
  197. typeof(var) pfo_ret__; \
  198. switch (sizeof(var)) { \
  199. case 1: \
  200. asm(op "b "__percpu_arg(P1)",%0" \
  201. : "=q" (pfo_ret__) \
  202. : "p" (&(var))); \
  203. break; \
  204. case 2: \
  205. asm(op "w "__percpu_arg(P1)",%0" \
  206. : "=r" (pfo_ret__) \
  207. : "p" (&(var))); \
  208. break; \
  209. case 4: \
  210. asm(op "l "__percpu_arg(P1)",%0" \
  211. : "=r" (pfo_ret__) \
  212. : "p" (&(var))); \
  213. break; \
  214. case 8: \
  215. asm(op "q "__percpu_arg(P1)",%0" \
  216. : "=r" (pfo_ret__) \
  217. : "p" (&(var))); \
  218. break; \
  219. default: __bad_percpu_size(); \
  220. } \
  221. pfo_ret__; \
  222. })
  223. #define percpu_unary_op(op, var) \
  224. ({ \
  225. switch (sizeof(var)) { \
  226. case 1: \
  227. asm(op "b "__percpu_arg(0) \
  228. : "+m" (var)); \
  229. break; \
  230. case 2: \
  231. asm(op "w "__percpu_arg(0) \
  232. : "+m" (var)); \
  233. break; \
  234. case 4: \
  235. asm(op "l "__percpu_arg(0) \
  236. : "+m" (var)); \
  237. break; \
  238. case 8: \
  239. asm(op "q "__percpu_arg(0) \
  240. : "+m" (var)); \
  241. break; \
  242. default: __bad_percpu_size(); \
  243. } \
  244. })
  245. /*
  246. * Add return operation
  247. */
  248. #define percpu_add_return_op(var, val) \
  249. ({ \
  250. typeof(var) paro_ret__ = val; \
  251. switch (sizeof(var)) { \
  252. case 1: \
  253. asm("xaddb %0, "__percpu_arg(1) \
  254. : "+q" (paro_ret__), "+m" (var) \
  255. : : "memory"); \
  256. break; \
  257. case 2: \
  258. asm("xaddw %0, "__percpu_arg(1) \
  259. : "+r" (paro_ret__), "+m" (var) \
  260. : : "memory"); \
  261. break; \
  262. case 4: \
  263. asm("xaddl %0, "__percpu_arg(1) \
  264. : "+r" (paro_ret__), "+m" (var) \
  265. : : "memory"); \
  266. break; \
  267. case 8: \
  268. asm("xaddq %0, "__percpu_arg(1) \
  269. : "+re" (paro_ret__), "+m" (var) \
  270. : : "memory"); \
  271. break; \
  272. default: __bad_percpu_size(); \
  273. } \
  274. paro_ret__ += val; \
  275. paro_ret__; \
  276. })
  277. /*
  278. * xchg is implemented using cmpxchg without a lock prefix. xchg is
  279. * expensive due to the implied lock prefix. The processor cannot prefetch
  280. * cachelines if xchg is used.
  281. */
  282. #define percpu_xchg_op(var, nval) \
  283. ({ \
  284. typeof(var) pxo_ret__; \
  285. typeof(var) pxo_new__ = (nval); \
  286. switch (sizeof(var)) { \
  287. case 1: \
  288. asm("\n\tmov "__percpu_arg(1)",%%al" \
  289. "\n1:\tcmpxchgb %2, "__percpu_arg(1) \
  290. "\n\tjnz 1b" \
  291. : "=&a" (pxo_ret__), "+m" (var) \
  292. : "q" (pxo_new__) \
  293. : "memory"); \
  294. break; \
  295. case 2: \
  296. asm("\n\tmov "__percpu_arg(1)",%%ax" \
  297. "\n1:\tcmpxchgw %2, "__percpu_arg(1) \
  298. "\n\tjnz 1b" \
  299. : "=&a" (pxo_ret__), "+m" (var) \
  300. : "r" (pxo_new__) \
  301. : "memory"); \
  302. break; \
  303. case 4: \
  304. asm("\n\tmov "__percpu_arg(1)",%%eax" \
  305. "\n1:\tcmpxchgl %2, "__percpu_arg(1) \
  306. "\n\tjnz 1b" \
  307. : "=&a" (pxo_ret__), "+m" (var) \
  308. : "r" (pxo_new__) \
  309. : "memory"); \
  310. break; \
  311. case 8: \
  312. asm("\n\tmov "__percpu_arg(1)",%%rax" \
  313. "\n1:\tcmpxchgq %2, "__percpu_arg(1) \
  314. "\n\tjnz 1b" \
  315. : "=&a" (pxo_ret__), "+m" (var) \
  316. : "r" (pxo_new__) \
  317. : "memory"); \
  318. break; \
  319. default: __bad_percpu_size(); \
  320. } \
  321. pxo_ret__; \
  322. })
  323. /*
  324. * cmpxchg has no such implied lock semantics as a result it is much
  325. * more efficient for cpu local operations.
  326. */
  327. #define percpu_cmpxchg_op(var, oval, nval) \
  328. ({ \
  329. typeof(var) pco_ret__; \
  330. typeof(var) pco_old__ = (oval); \
  331. typeof(var) pco_new__ = (nval); \
  332. switch (sizeof(var)) { \
  333. case 1: \
  334. asm("cmpxchgb %2, "__percpu_arg(1) \
  335. : "=a" (pco_ret__), "+m" (var) \
  336. : "q" (pco_new__), "0" (pco_old__) \
  337. : "memory"); \
  338. break; \
  339. case 2: \
  340. asm("cmpxchgw %2, "__percpu_arg(1) \
  341. : "=a" (pco_ret__), "+m" (var) \
  342. : "r" (pco_new__), "0" (pco_old__) \
  343. : "memory"); \
  344. break; \
  345. case 4: \
  346. asm("cmpxchgl %2, "__percpu_arg(1) \
  347. : "=a" (pco_ret__), "+m" (var) \
  348. : "r" (pco_new__), "0" (pco_old__) \
  349. : "memory"); \
  350. break; \
  351. case 8: \
  352. asm("cmpxchgq %2, "__percpu_arg(1) \
  353. : "=a" (pco_ret__), "+m" (var) \
  354. : "r" (pco_new__), "0" (pco_old__) \
  355. : "memory"); \
  356. break; \
  357. default: __bad_percpu_size(); \
  358. } \
  359. pco_ret__; \
  360. })
  361. /*
  362. * this_cpu_read() makes gcc load the percpu variable every time it is
  363. * accessed while this_cpu_read_stable() allows the value to be cached.
  364. * this_cpu_read_stable() is more efficient and can be used if its value
  365. * is guaranteed to be valid across cpus. The current users include
  366. * get_current() and get_thread_info() both of which are actually
  367. * per-thread variables implemented as per-cpu variables and thus
  368. * stable for the duration of the respective task.
  369. */
  370. #define this_cpu_read_stable(var) percpu_stable_op("mov", var)
  371. #define raw_cpu_read_1(pcp) percpu_from_op("mov", pcp)
  372. #define raw_cpu_read_2(pcp) percpu_from_op("mov", pcp)
  373. #define raw_cpu_read_4(pcp) percpu_from_op("mov", pcp)
  374. #define raw_cpu_write_1(pcp, val) percpu_to_op("mov", (pcp), val)
  375. #define raw_cpu_write_2(pcp, val) percpu_to_op("mov", (pcp), val)
  376. #define raw_cpu_write_4(pcp, val) percpu_to_op("mov", (pcp), val)
  377. #define raw_cpu_add_1(pcp, val) percpu_add_op((pcp), val)
  378. #define raw_cpu_add_2(pcp, val) percpu_add_op((pcp), val)
  379. #define raw_cpu_add_4(pcp, val) percpu_add_op((pcp), val)
  380. #define raw_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
  381. #define raw_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
  382. #define raw_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
  383. #define raw_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
  384. #define raw_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
  385. #define raw_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
  386. #define raw_cpu_xchg_1(pcp, val) percpu_xchg_op(pcp, val)
  387. #define raw_cpu_xchg_2(pcp, val) percpu_xchg_op(pcp, val)
  388. #define raw_cpu_xchg_4(pcp, val) percpu_xchg_op(pcp, val)
  389. #define this_cpu_read_1(pcp) percpu_from_op("mov", pcp)
  390. #define this_cpu_read_2(pcp) percpu_from_op("mov", pcp)
  391. #define this_cpu_read_4(pcp) percpu_from_op("mov", pcp)
  392. #define this_cpu_write_1(pcp, val) percpu_to_op("mov", (pcp), val)
  393. #define this_cpu_write_2(pcp, val) percpu_to_op("mov", (pcp), val)
  394. #define this_cpu_write_4(pcp, val) percpu_to_op("mov", (pcp), val)
  395. #define this_cpu_add_1(pcp, val) percpu_add_op((pcp), val)
  396. #define this_cpu_add_2(pcp, val) percpu_add_op((pcp), val)
  397. #define this_cpu_add_4(pcp, val) percpu_add_op((pcp), val)
  398. #define this_cpu_and_1(pcp, val) percpu_to_op("and", (pcp), val)
  399. #define this_cpu_and_2(pcp, val) percpu_to_op("and", (pcp), val)
  400. #define this_cpu_and_4(pcp, val) percpu_to_op("and", (pcp), val)
  401. #define this_cpu_or_1(pcp, val) percpu_to_op("or", (pcp), val)
  402. #define this_cpu_or_2(pcp, val) percpu_to_op("or", (pcp), val)
  403. #define this_cpu_or_4(pcp, val) percpu_to_op("or", (pcp), val)
  404. #define this_cpu_xchg_1(pcp, nval) percpu_xchg_op(pcp, nval)
  405. #define this_cpu_xchg_2(pcp, nval) percpu_xchg_op(pcp, nval)
  406. #define this_cpu_xchg_4(pcp, nval) percpu_xchg_op(pcp, nval)
  407. #define raw_cpu_add_return_1(pcp, val) percpu_add_return_op(pcp, val)
  408. #define raw_cpu_add_return_2(pcp, val) percpu_add_return_op(pcp, val)
  409. #define raw_cpu_add_return_4(pcp, val) percpu_add_return_op(pcp, val)
  410. #define raw_cpu_cmpxchg_1(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
  411. #define raw_cpu_cmpxchg_2(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
  412. #define raw_cpu_cmpxchg_4(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
  413. #define this_cpu_add_return_1(pcp, val) percpu_add_return_op(pcp, val)
  414. #define this_cpu_add_return_2(pcp, val) percpu_add_return_op(pcp, val)
  415. #define this_cpu_add_return_4(pcp, val) percpu_add_return_op(pcp, val)
  416. #define this_cpu_cmpxchg_1(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
  417. #define this_cpu_cmpxchg_2(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
  418. #define this_cpu_cmpxchg_4(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
  419. #ifdef CONFIG_X86_CMPXCHG64
  420. #define percpu_cmpxchg8b_double(pcp1, pcp2, o1, o2, n1, n2) \
  421. ({ \
  422. bool __ret; \
  423. typeof(pcp1) __o1 = (o1), __n1 = (n1); \
  424. typeof(pcp2) __o2 = (o2), __n2 = (n2); \
  425. asm volatile("cmpxchg8b "__percpu_arg(1)"\n\tsetz %0\n\t" \
  426. : "=a" (__ret), "+m" (pcp1), "+m" (pcp2), "+d" (__o2) \
  427. : "b" (__n1), "c" (__n2), "a" (__o1)); \
  428. __ret; \
  429. })
  430. #define raw_cpu_cmpxchg_double_4 percpu_cmpxchg8b_double
  431. #define this_cpu_cmpxchg_double_4 percpu_cmpxchg8b_double
  432. #endif /* CONFIG_X86_CMPXCHG64 */
  433. /*
  434. * Per cpu atomic 64 bit operations are only available under 64 bit.
  435. * 32 bit must fall back to generic operations.
  436. */
  437. #ifdef CONFIG_X86_64
  438. #define raw_cpu_read_8(pcp) percpu_from_op("mov", pcp)
  439. #define raw_cpu_write_8(pcp, val) percpu_to_op("mov", (pcp), val)
  440. #define raw_cpu_add_8(pcp, val) percpu_add_op((pcp), val)
  441. #define raw_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
  442. #define raw_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
  443. #define raw_cpu_add_return_8(pcp, val) percpu_add_return_op(pcp, val)
  444. #define raw_cpu_xchg_8(pcp, nval) percpu_xchg_op(pcp, nval)
  445. #define raw_cpu_cmpxchg_8(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
  446. #define this_cpu_read_8(pcp) percpu_from_op("mov", pcp)
  447. #define this_cpu_write_8(pcp, val) percpu_to_op("mov", (pcp), val)
  448. #define this_cpu_add_8(pcp, val) percpu_add_op((pcp), val)
  449. #define this_cpu_and_8(pcp, val) percpu_to_op("and", (pcp), val)
  450. #define this_cpu_or_8(pcp, val) percpu_to_op("or", (pcp), val)
  451. #define this_cpu_add_return_8(pcp, val) percpu_add_return_op(pcp, val)
  452. #define this_cpu_xchg_8(pcp, nval) percpu_xchg_op(pcp, nval)
  453. #define this_cpu_cmpxchg_8(pcp, oval, nval) percpu_cmpxchg_op(pcp, oval, nval)
  454. /*
  455. * Pretty complex macro to generate cmpxchg16 instruction. The instruction
  456. * is not supported on early AMD64 processors so we must be able to emulate
  457. * it in software. The address used in the cmpxchg16 instruction must be
  458. * aligned to a 16 byte boundary.
  459. */
  460. #define percpu_cmpxchg16b_double(pcp1, pcp2, o1, o2, n1, n2) \
  461. ({ \
  462. bool __ret; \
  463. typeof(pcp1) __o1 = (o1), __n1 = (n1); \
  464. typeof(pcp2) __o2 = (o2), __n2 = (n2); \
  465. alternative_io("leaq %P1,%%rsi\n\tcall this_cpu_cmpxchg16b_emu\n\t", \
  466. "cmpxchg16b " __percpu_arg(1) "\n\tsetz %0\n\t", \
  467. X86_FEATURE_CX16, \
  468. ASM_OUTPUT2("=a" (__ret), "+m" (pcp1), \
  469. "+m" (pcp2), "+d" (__o2)), \
  470. "b" (__n1), "c" (__n2), "a" (__o1) : "rsi"); \
  471. __ret; \
  472. })
  473. #define raw_cpu_cmpxchg_double_8 percpu_cmpxchg16b_double
  474. #define this_cpu_cmpxchg_double_8 percpu_cmpxchg16b_double
  475. #endif
  476. /* This is not atomic against other CPUs -- CPU preemption needs to be off */
  477. #define x86_test_and_clear_bit_percpu(bit, var) \
  478. ({ \
  479. bool old__; \
  480. asm volatile("btr %2,"__percpu_arg(1)"\n\t" \
  481. CC_SET(c) \
  482. : CC_OUT(c) (old__), "+m" (var) \
  483. : "dIr" (bit)); \
  484. old__; \
  485. })
  486. static __always_inline bool x86_this_cpu_constant_test_bit(unsigned int nr,
  487. const unsigned long __percpu *addr)
  488. {
  489. unsigned long __percpu *a =
  490. (unsigned long __percpu *)addr + nr / BITS_PER_LONG;
  491. #ifdef CONFIG_X86_64
  492. return ((1UL << (nr % BITS_PER_LONG)) & raw_cpu_read_8(*a)) != 0;
  493. #else
  494. return ((1UL << (nr % BITS_PER_LONG)) & raw_cpu_read_4(*a)) != 0;
  495. #endif
  496. }
  497. static inline bool x86_this_cpu_variable_test_bit(int nr,
  498. const unsigned long __percpu *addr)
  499. {
  500. bool oldbit;
  501. asm volatile("bt "__percpu_arg(2)",%1\n\t"
  502. CC_SET(c)
  503. : CC_OUT(c) (oldbit)
  504. : "m" (*(unsigned long __percpu *)addr), "Ir" (nr));
  505. return oldbit;
  506. }
  507. #define x86_this_cpu_test_bit(nr, addr) \
  508. (__builtin_constant_p((nr)) \
  509. ? x86_this_cpu_constant_test_bit((nr), (addr)) \
  510. : x86_this_cpu_variable_test_bit((nr), (addr)))
  511. #include <asm-generic/percpu.h>
  512. /* We can use this directly for local CPU (faster). */
  513. DECLARE_PER_CPU_READ_MOSTLY(unsigned long, this_cpu_off);
  514. #endif /* !__ASSEMBLY__ */
  515. #ifdef CONFIG_SMP
  516. /*
  517. * Define the "EARLY_PER_CPU" macros. These are used for some per_cpu
  518. * variables that are initialized and accessed before there are per_cpu
  519. * areas allocated.
  520. */
  521. #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
  522. DEFINE_PER_CPU(_type, _name) = _initvalue; \
  523. __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
  524. { [0 ... NR_CPUS-1] = _initvalue }; \
  525. __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
  526. #define DEFINE_EARLY_PER_CPU_READ_MOSTLY(_type, _name, _initvalue) \
  527. DEFINE_PER_CPU_READ_MOSTLY(_type, _name) = _initvalue; \
  528. __typeof__(_type) _name##_early_map[NR_CPUS] __initdata = \
  529. { [0 ... NR_CPUS-1] = _initvalue }; \
  530. __typeof__(_type) *_name##_early_ptr __refdata = _name##_early_map
  531. #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
  532. EXPORT_PER_CPU_SYMBOL(_name)
  533. #define DECLARE_EARLY_PER_CPU(_type, _name) \
  534. DECLARE_PER_CPU(_type, _name); \
  535. extern __typeof__(_type) *_name##_early_ptr; \
  536. extern __typeof__(_type) _name##_early_map[]
  537. #define DECLARE_EARLY_PER_CPU_READ_MOSTLY(_type, _name) \
  538. DECLARE_PER_CPU_READ_MOSTLY(_type, _name); \
  539. extern __typeof__(_type) *_name##_early_ptr; \
  540. extern __typeof__(_type) _name##_early_map[]
  541. #define early_per_cpu_ptr(_name) (_name##_early_ptr)
  542. #define early_per_cpu_map(_name, _idx) (_name##_early_map[_idx])
  543. #define early_per_cpu(_name, _cpu) \
  544. *(early_per_cpu_ptr(_name) ? \
  545. &early_per_cpu_ptr(_name)[_cpu] : \
  546. &per_cpu(_name, _cpu))
  547. #else /* !CONFIG_SMP */
  548. #define DEFINE_EARLY_PER_CPU(_type, _name, _initvalue) \
  549. DEFINE_PER_CPU(_type, _name) = _initvalue
  550. #define DEFINE_EARLY_PER_CPU_READ_MOSTLY(_type, _name, _initvalue) \
  551. DEFINE_PER_CPU_READ_MOSTLY(_type, _name) = _initvalue
  552. #define EXPORT_EARLY_PER_CPU_SYMBOL(_name) \
  553. EXPORT_PER_CPU_SYMBOL(_name)
  554. #define DECLARE_EARLY_PER_CPU(_type, _name) \
  555. DECLARE_PER_CPU(_type, _name)
  556. #define DECLARE_EARLY_PER_CPU_READ_MOSTLY(_type, _name) \
  557. DECLARE_PER_CPU_READ_MOSTLY(_type, _name)
  558. #define early_per_cpu(_name, _cpu) per_cpu(_name, _cpu)
  559. #define early_per_cpu_ptr(_name) NULL
  560. /* no early_per_cpu_map() */
  561. #endif /* !CONFIG_SMP */
  562. #endif /* _ASM_X86_PERCPU_H */