blob: 08366b1fd273d77a26acb3adcf7afa72e7f9222d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Mathieu Desnoyers72323112007-05-08 00:34:47 -07002#ifndef _ARCH_MIPS_LOCAL_H
3#define _ARCH_MIPS_LOCAL_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/percpu.h>
Mathieu Desnoyers72323112007-05-08 00:34:47 -07006#include <linux/bitops.h>
Arun Sharma600634972011-07-26 16:09:06 -07007#include <linux/atomic.h>
Huang Pei277c8cb2021-12-15 16:44:57 +08008#include <asm/asm.h>
Ralf Baechlefef74702007-10-01 04:15:00 +01009#include <asm/cmpxchg.h>
Markos Chandras82e7ce82015-01-15 10:31:36 +000010#include <asm/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Mathieu Desnoyers72323112007-05-08 00:34:47 -070012typedef struct
13{
14 atomic_long_t a;
15} local_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Mathieu Desnoyers72323112007-05-08 00:34:47 -070017#define LOCAL_INIT(i) { ATOMIC_LONG_INIT(i) }
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Mathieu Desnoyers72323112007-05-08 00:34:47 -070019#define local_read(l) atomic_long_read(&(l)->a)
Ralf Baechle70342282013-01-22 12:59:30 +010020#define local_set(l, i) atomic_long_set(&(l)->a, (i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Ralf Baechle70342282013-01-22 12:59:30 +010022#define local_add(i, l) atomic_long_add((i), (&(l)->a))
23#define local_sub(i, l) atomic_long_sub((i), (&(l)->a))
Mathieu Desnoyers72323112007-05-08 00:34:47 -070024#define local_inc(l) atomic_long_inc(&(l)->a)
25#define local_dec(l) atomic_long_dec(&(l)->a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/*
Mathieu Desnoyers72323112007-05-08 00:34:47 -070028 * Same as above, but return the result value
29 */
30static __inline__ long local_add_return(long i, local_t * l)
31{
32 unsigned long result;
33
Huang Pei10657662021-12-15 16:44:59 +080034 if (kernel_uses_llsc) {
Mathieu Desnoyers72323112007-05-08 00:34:47 -070035 unsigned long temp;
36
37 __asm__ __volatile__(
Paul Burton378ed6f2018-11-08 20:14:38 +000038 " .set push \n"
Markos Chandras82e7ce82015-01-15 10:31:36 +000039 " .set "MIPS_ISA_ARCH_LEVEL" \n"
Huang Pei10657662021-12-15 16:44:59 +080040 __SYNC(full, loongson3_war) " \n"
41 "1:" __stringify(LONG_LL) " %1, %2 \n"
Huang Pei277c8cb2021-12-15 16:44:57 +080042 __stringify(LONG_ADDU) " %0, %1, %3 \n"
Huang Pei10657662021-12-15 16:44:59 +080043 __stringify(LONG_SC) " %0, %2 \n"
44 __stringify(SC_BEQZ) " %0, 1b \n"
45 __stringify(LONG_ADDU) " %0, %1, %3 \n"
Paul Burton378ed6f2018-11-08 20:14:38 +000046 " .set pop \n"
Mathieu Desnoyers72323112007-05-08 00:34:47 -070047 : "=&r" (result), "=&r" (temp), "=m" (l->a.counter)
48 : "Ir" (i), "m" (l->a.counter)
49 : "memory");
50 } else {
51 unsigned long flags;
52
53 local_irq_save(flags);
54 result = l->a.counter;
55 result += i;
56 l->a.counter = result;
57 local_irq_restore(flags);
58 }
59
60 return result;
61}
62
63static __inline__ long local_sub_return(long i, local_t * l)
64{
65 unsigned long result;
66
Huang Pei10657662021-12-15 16:44:59 +080067 if (kernel_uses_llsc) {
Mathieu Desnoyers72323112007-05-08 00:34:47 -070068 unsigned long temp;
69
70 __asm__ __volatile__(
Paul Burton378ed6f2018-11-08 20:14:38 +000071 " .set push \n"
Markos Chandras82e7ce82015-01-15 10:31:36 +000072 " .set "MIPS_ISA_ARCH_LEVEL" \n"
Huang Pei10657662021-12-15 16:44:59 +080073 __SYNC(full, loongson3_war) " \n"
74 "1:" __stringify(LONG_LL) " %1, %2 \n"
Huang Pei277c8cb2021-12-15 16:44:57 +080075 __stringify(LONG_SUBU) " %0, %1, %3 \n"
Huang Pei10657662021-12-15 16:44:59 +080076 __stringify(LONG_SUBU) " %0, %1, %3 \n"
77 __stringify(LONG_SC) " %0, %2 \n"
78 __stringify(SC_BEQZ) " %0, 1b \n"
79 __stringify(LONG_SUBU) " %0, %1, %3 \n"
Paul Burton378ed6f2018-11-08 20:14:38 +000080 " .set pop \n"
Mathieu Desnoyers72323112007-05-08 00:34:47 -070081 : "=&r" (result), "=&r" (temp), "=m" (l->a.counter)
82 : "Ir" (i), "m" (l->a.counter)
83 : "memory");
84 } else {
85 unsigned long flags;
86
87 local_irq_save(flags);
88 result = l->a.counter;
89 result -= i;
90 l->a.counter = result;
91 local_irq_restore(flags);
92 }
93
94 return result;
95}
96
Mathieu Desnoyers72323112007-05-08 00:34:47 -070097#define local_cmpxchg(l, o, n) \
98 ((long)cmpxchg_local(&((l)->a.counter), (o), (n)))
Deng-Cheng Zhu6dbd9722010-10-12 19:37:20 +080099#define local_xchg(l, n) (atomic_long_xchg((&(l)->a), (n)))
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700100
101/**
102 * local_add_unless - add unless the number is a given value
103 * @l: pointer of type local_t
104 * @a: the amount to add to l...
105 * @u: ...unless l is equal to u.
106 *
107 * Atomically adds @a to @l, so long as it was not @u.
108 * Returns non-zero if @l was not @u, and zero otherwise.
109 */
110#define local_add_unless(l, a, u) \
111({ \
112 long c, old; \
113 c = local_read(l); \
114 while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \
115 c = old; \
116 c != (u); \
117})
118#define local_inc_not_zero(l) local_add_unless((l), 1, 0)
119
Ralf Baechle21a151d2007-10-11 23:46:15 +0100120#define local_dec_return(l) local_sub_return(1, (l))
121#define local_inc_return(l) local_add_return(1, (l))
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700122
123/*
124 * local_sub_and_test - subtract value from variable and test result
125 * @i: integer value to subtract
126 * @l: pointer of type local_t
127 *
128 * Atomically subtracts @i from @l and returns
129 * true if the result is zero, or false for all
130 * other cases.
131 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100132#define local_sub_and_test(i, l) (local_sub_return((i), (l)) == 0)
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700133
134/*
135 * local_inc_and_test - increment and test
136 * @l: pointer of type local_t
137 *
138 * Atomically increments @l by 1
139 * and returns true if the result is zero, or false for all
140 * other cases.
141 */
142#define local_inc_and_test(l) (local_inc_return(l) == 0)
143
144/*
145 * local_dec_and_test - decrement by 1 and test
146 * @l: pointer of type local_t
147 *
148 * Atomically decrements @l by 1 and
149 * returns true if the result is 0, or false for all other
150 * cases.
151 */
152#define local_dec_and_test(l) (local_sub_return(1, (l)) == 0)
153
154/*
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700155 * local_add_negative - add and test if negative
156 * @l: pointer of type local_t
157 * @i: integer value to add
158 *
159 * Atomically adds @i to @l and returns true
160 * if the result is negative, or false when
161 * result is greater than or equal to zero.
162 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100163#define local_add_negative(i, l) (local_add_return(i, (l)) < 0)
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700164
165/* Use these for per-cpu local_t variables: on some archs they are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * much more efficient than these naive implementations. Note they take
167 * a variable, not an address.
168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700170#define __local_inc(l) ((l)->a.counter++)
171#define __local_dec(l) ((l)->a.counter++)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100172#define __local_add(i, l) ((l)->a.counter+=(i))
173#define __local_sub(i, l) ((l)->a.counter-=(i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Mathieu Desnoyers72323112007-05-08 00:34:47 -0700175#endif /* _ARCH_MIPS_LOCAL_H */