|
5 | 5 | #ifndef __ASM_CMPXCHG_H
|
6 | 6 | #define __ASM_CMPXCHG_H
|
7 | 7 |
|
8 |
| -#include <asm/barrier.h> |
| 8 | +#include <linux/bits.h> |
9 | 9 | #include <linux/build_bug.h>
|
| 10 | +#include <asm/barrier.h> |
10 | 11 |
|
11 | 12 | #define __xchg_asm(amswap_db, m, val) \
|
12 | 13 | ({ \
|
|
21 | 22 | __ret; \
|
22 | 23 | })
|
23 | 24 |
|
| 25 | +static inline unsigned int __xchg_small(volatile void *ptr, unsigned int val, |
| 26 | + unsigned int size) |
| 27 | +{ |
| 28 | + unsigned int shift; |
| 29 | + u32 old32, mask, temp; |
| 30 | + volatile u32 *ptr32; |
| 31 | + |
| 32 | + /* Mask value to the correct size. */ |
| 33 | + mask = GENMASK((size * BITS_PER_BYTE) - 1, 0); |
| 34 | + val &= mask; |
| 35 | + |
| 36 | + /* |
| 37 | + * Calculate a shift & mask that correspond to the value we wish to |
| 38 | + * exchange within the naturally aligned 4 byte integerthat includes |
| 39 | + * it. |
| 40 | + */ |
| 41 | + shift = (unsigned long)ptr & 0x3; |
| 42 | + shift *= BITS_PER_BYTE; |
| 43 | + mask <<= shift; |
| 44 | + |
| 45 | + /* |
| 46 | + * Calculate a pointer to the naturally aligned 4 byte integer that |
| 47 | + * includes our byte of interest, and load its value. |
| 48 | + */ |
| 49 | + ptr32 = (volatile u32 *)((unsigned long)ptr & ~0x3); |
| 50 | + |
| 51 | + asm volatile ( |
| 52 | + "1: ll.w %0, %3 \n" |
| 53 | + " andn %1, %0, %z4 \n" |
| 54 | + " or %1, %1, %z5 \n" |
| 55 | + " sc.w %1, %2 \n" |
| 56 | + " beqz %1, 1b \n" |
| 57 | + : "=&r" (old32), "=&r" (temp), "=ZC" (*ptr32) |
| 58 | + : "ZC" (*ptr32), "Jr" (mask), "Jr" (val << shift) |
| 59 | + : "memory"); |
| 60 | + |
| 61 | + return (old32 & mask) >> shift; |
| 62 | +} |
| 63 | + |
24 | 64 | static inline unsigned long __xchg(volatile void *ptr, unsigned long x,
|
25 | 65 | int size)
|
26 | 66 | {
|
27 | 67 | switch (size) {
|
| 68 | + case 1: |
| 69 | + case 2: |
| 70 | + return __xchg_small(ptr, x, size); |
| 71 | + |
28 | 72 | case 4:
|
29 | 73 | return __xchg_asm("amswap_db.w", (volatile u32 *)ptr, (u32)x);
|
30 | 74 |
|
@@ -67,10 +111,62 @@ static inline unsigned long __xchg(volatile void *ptr, unsigned long x,
|
67 | 111 | __ret; \
|
68 | 112 | })
|
69 | 113 |
|
| 114 | +static inline unsigned int __cmpxchg_small(volatile void *ptr, unsigned int old, |
| 115 | + unsigned int new, unsigned int size) |
| 116 | +{ |
| 117 | + unsigned int shift; |
| 118 | + u32 old32, mask, temp; |
| 119 | + volatile u32 *ptr32; |
| 120 | + |
| 121 | + /* Mask inputs to the correct size. */ |
| 122 | + mask = GENMASK((size * BITS_PER_BYTE) - 1, 0); |
| 123 | + old &= mask; |
| 124 | + new &= mask; |
| 125 | + |
| 126 | + /* |
| 127 | + * Calculate a shift & mask that correspond to the value we wish to |
| 128 | + * compare & exchange within the naturally aligned 4 byte integer |
| 129 | + * that includes it. |
| 130 | + */ |
| 131 | + shift = (unsigned long)ptr & 0x3; |
| 132 | + shift *= BITS_PER_BYTE; |
| 133 | + old <<= shift; |
| 134 | + new <<= shift; |
| 135 | + mask <<= shift; |
| 136 | + |
| 137 | + /* |
| 138 | + * Calculate a pointer to the naturally aligned 4 byte integer that |
| 139 | + * includes our byte of interest, and load its value. |
| 140 | + */ |
| 141 | + ptr32 = (volatile u32 *)((unsigned long)ptr & ~0x3); |
| 142 | + |
| 143 | + asm volatile ( |
| 144 | + "1: ll.w %0, %3 \n" |
| 145 | + " and %1, %0, %z4 \n" |
| 146 | + " bne %1, %z5, 2f \n" |
| 147 | + " andn %1, %0, %z4 \n" |
| 148 | + " or %1, %1, %z6 \n" |
| 149 | + " sc.w %1, %2 \n" |
| 150 | + " beqz %1, 1b \n" |
| 151 | + " b 3f \n" |
| 152 | + "2: \n" |
| 153 | + __WEAK_LLSC_MB |
| 154 | + "3: \n" |
| 155 | + : "=&r" (old32), "=&r" (temp), "=ZC" (*ptr32) |
| 156 | + : "ZC" (*ptr32), "Jr" (mask), "Jr" (old), "Jr" (new) |
| 157 | + : "memory"); |
| 158 | + |
| 159 | + return (old32 & mask) >> shift; |
| 160 | +} |
| 161 | + |
70 | 162 | static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
|
71 | 163 | unsigned long new, unsigned int size)
|
72 | 164 | {
|
73 | 165 | switch (size) {
|
| 166 | + case 1: |
| 167 | + case 2: |
| 168 | + return __cmpxchg_small(ptr, old, new, size); |
| 169 | + |
74 | 170 | case 4:
|
75 | 171 | return __cmpxchg_asm("ll.w", "sc.w", (volatile u32 *)ptr,
|
76 | 172 | (u32)old, new);
|
|
0 commit comments