18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_X86_SYNC_BITOPS_H
38c2ecf20Sopenharmony_ci#define _ASM_X86_SYNC_BITOPS_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci/*
68c2ecf20Sopenharmony_ci * Copyright 1992, Linus Torvalds.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci/*
108c2ecf20Sopenharmony_ci * These have to be done with inline assembly: that way the bit-setting
118c2ecf20Sopenharmony_ci * is guaranteed to be atomic. All bit operations return 0 if the bit
128c2ecf20Sopenharmony_ci * was cleared before the operation and != 0 if it was not.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <asm/rmwcc.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define ADDR (*(volatile long *)addr)
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/**
228c2ecf20Sopenharmony_ci * sync_set_bit - Atomically set a bit in memory
238c2ecf20Sopenharmony_ci * @nr: the bit to set
248c2ecf20Sopenharmony_ci * @addr: the address to start counting from
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * This function is atomic and may not be reordered.  See __set_bit()
278c2ecf20Sopenharmony_ci * if you do not require the atomic guarantees.
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * Note that @nr may be almost arbitrarily large; this function is not
308c2ecf20Sopenharmony_ci * restricted to acting on a single-word quantity.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_cistatic inline void sync_set_bit(long nr, volatile unsigned long *addr)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	asm volatile("lock; " __ASM_SIZE(bts) " %1,%0"
358c2ecf20Sopenharmony_ci		     : "+m" (ADDR)
368c2ecf20Sopenharmony_ci		     : "Ir" (nr)
378c2ecf20Sopenharmony_ci		     : "memory");
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/**
418c2ecf20Sopenharmony_ci * sync_clear_bit - Clears a bit in memory
428c2ecf20Sopenharmony_ci * @nr: Bit to clear
438c2ecf20Sopenharmony_ci * @addr: Address to start counting from
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci * sync_clear_bit() is atomic and may not be reordered.  However, it does
468c2ecf20Sopenharmony_ci * not contain a memory barrier, so if it is used for locking purposes,
478c2ecf20Sopenharmony_ci * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
488c2ecf20Sopenharmony_ci * in order to ensure changes are visible on other processors.
498c2ecf20Sopenharmony_ci */
508c2ecf20Sopenharmony_cistatic inline void sync_clear_bit(long nr, volatile unsigned long *addr)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	asm volatile("lock; " __ASM_SIZE(btr) " %1,%0"
538c2ecf20Sopenharmony_ci		     : "+m" (ADDR)
548c2ecf20Sopenharmony_ci		     : "Ir" (nr)
558c2ecf20Sopenharmony_ci		     : "memory");
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/**
598c2ecf20Sopenharmony_ci * sync_change_bit - Toggle a bit in memory
608c2ecf20Sopenharmony_ci * @nr: Bit to change
618c2ecf20Sopenharmony_ci * @addr: Address to start counting from
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci * sync_change_bit() is atomic and may not be reordered.
648c2ecf20Sopenharmony_ci * Note that @nr may be almost arbitrarily large; this function is not
658c2ecf20Sopenharmony_ci * restricted to acting on a single-word quantity.
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_cistatic inline void sync_change_bit(long nr, volatile unsigned long *addr)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	asm volatile("lock; " __ASM_SIZE(btc) " %1,%0"
708c2ecf20Sopenharmony_ci		     : "+m" (ADDR)
718c2ecf20Sopenharmony_ci		     : "Ir" (nr)
728c2ecf20Sopenharmony_ci		     : "memory");
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/**
768c2ecf20Sopenharmony_ci * sync_test_and_set_bit - Set a bit and return its old value
778c2ecf20Sopenharmony_ci * @nr: Bit to set
788c2ecf20Sopenharmony_ci * @addr: Address to count from
798c2ecf20Sopenharmony_ci *
808c2ecf20Sopenharmony_ci * This operation is atomic and cannot be reordered.
818c2ecf20Sopenharmony_ci * It also implies a memory barrier.
828c2ecf20Sopenharmony_ci */
838c2ecf20Sopenharmony_cistatic inline bool sync_test_and_set_bit(long nr, volatile unsigned long *addr)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(bts), *addr, c, "Ir", nr);
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/**
898c2ecf20Sopenharmony_ci * sync_test_and_clear_bit - Clear a bit and return its old value
908c2ecf20Sopenharmony_ci * @nr: Bit to clear
918c2ecf20Sopenharmony_ci * @addr: Address to count from
928c2ecf20Sopenharmony_ci *
938c2ecf20Sopenharmony_ci * This operation is atomic and cannot be reordered.
948c2ecf20Sopenharmony_ci * It also implies a memory barrier.
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_cistatic inline int sync_test_and_clear_bit(long nr, volatile unsigned long *addr)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(btr), *addr, c, "Ir", nr);
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/**
1028c2ecf20Sopenharmony_ci * sync_test_and_change_bit - Change a bit and return its old value
1038c2ecf20Sopenharmony_ci * @nr: Bit to change
1048c2ecf20Sopenharmony_ci * @addr: Address to count from
1058c2ecf20Sopenharmony_ci *
1068c2ecf20Sopenharmony_ci * This operation is atomic and cannot be reordered.
1078c2ecf20Sopenharmony_ci * It also implies a memory barrier.
1088c2ecf20Sopenharmony_ci */
1098c2ecf20Sopenharmony_cistatic inline int sync_test_and_change_bit(long nr, volatile unsigned long *addr)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(btc), *addr, c, "Ir", nr);
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define sync_test_bit(nr, addr) test_bit(nr, addr)
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci#undef ADDR
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci#endif /* _ASM_X86_SYNC_BITOPS_H */
119