Blame view

include/asm-generic/cmpxchg-local.h 1.43 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  /* SPDX-License-Identifier: GPL-2.0 */
068fbad28   Mathieu Desnoyers   Add cmpxchg_local...
2
3
4
5
  #ifndef __ASM_GENERIC_CMPXCHG_LOCAL_H
  #define __ASM_GENERIC_CMPXCHG_LOCAL_H
  
  #include <linux/types.h>
df9ee2927   David Howells   Fix IRQ flag hand...
6
  #include <linux/irqflags.h>
068fbad28   Mathieu Desnoyers   Add cmpxchg_local...
7

aa93685af   Behan Webster   LLVMLinux: Remove...
8
9
  extern unsigned long wrong_size_cmpxchg(volatile void *ptr)
  	__noreturn;
068fbad28   Mathieu Desnoyers   Add cmpxchg_local...
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  
  /*
   * Generic version of __cmpxchg_local (disables interrupts). Takes an unsigned
   * long parameter, supporting various types of architectures.
   */
  static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
  		unsigned long old, unsigned long new, int size)
  {
  	unsigned long flags, prev;
  
  	/*
  	 * Sanity checking, compile-time.
  	 */
  	if (size == 8 && sizeof(unsigned long) != 8)
  		wrong_size_cmpxchg(ptr);
5042afe7f   Ingo Molnar   generic: Use raw ...
25
  	raw_local_irq_save(flags);
068fbad28   Mathieu Desnoyers   Add cmpxchg_local...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  	switch (size) {
  	case 1: prev = *(u8 *)ptr;
  		if (prev == old)
  			*(u8 *)ptr = (u8)new;
  		break;
  	case 2: prev = *(u16 *)ptr;
  		if (prev == old)
  			*(u16 *)ptr = (u16)new;
  		break;
  	case 4: prev = *(u32 *)ptr;
  		if (prev == old)
  			*(u32 *)ptr = (u32)new;
  		break;
  	case 8: prev = *(u64 *)ptr;
  		if (prev == old)
  			*(u64 *)ptr = (u64)new;
  		break;
  	default:
  		wrong_size_cmpxchg(ptr);
  	}
5042afe7f   Ingo Molnar   generic: Use raw ...
46
  	raw_local_irq_restore(flags);
068fbad28   Mathieu Desnoyers   Add cmpxchg_local...
47
48
49
50
51
52
53
54
55
56
57
  	return prev;
  }
  
  /*
   * Generic version of __cmpxchg64_local. Takes an u64 parameter.
   */
  static inline u64 __cmpxchg64_local_generic(volatile void *ptr,
  		u64 old, u64 new)
  {
  	u64 prev;
  	unsigned long flags;
5042afe7f   Ingo Molnar   generic: Use raw ...
58
  	raw_local_irq_save(flags);
068fbad28   Mathieu Desnoyers   Add cmpxchg_local...
59
60
61
  	prev = *(u64 *)ptr;
  	if (prev == old)
  		*(u64 *)ptr = new;
5042afe7f   Ingo Molnar   generic: Use raw ...
62
  	raw_local_irq_restore(flags);
068fbad28   Mathieu Desnoyers   Add cmpxchg_local...
63
64
65
66
  	return prev;
  }
  
  #endif