1570af302Sopenharmony_ci#if __mips_isa_rev < 6
2570af302Sopenharmony_ci#define LLSC_M "m"
3570af302Sopenharmony_ci#else
4570af302Sopenharmony_ci#define LLSC_M "ZC"
5570af302Sopenharmony_ci#endif
6570af302Sopenharmony_ci
7570af302Sopenharmony_ci#define a_ll a_ll
8570af302Sopenharmony_cistatic inline int a_ll(volatile int *p)
9570af302Sopenharmony_ci{
10570af302Sopenharmony_ci	int v;
11570af302Sopenharmony_ci#if __mips < 2
12570af302Sopenharmony_ci	__asm__ __volatile__ (
13570af302Sopenharmony_ci		".set push ; .set mips2\n\t"
14570af302Sopenharmony_ci		"ll %0, %1"
15570af302Sopenharmony_ci		"\n\t.set pop"
16570af302Sopenharmony_ci		: "=r"(v) : "m"(*p));
17570af302Sopenharmony_ci#else
18570af302Sopenharmony_ci	__asm__ __volatile__ (
19570af302Sopenharmony_ci		"ll %0, %1"
20570af302Sopenharmony_ci		: "=r"(v) : LLSC_M(*p));
21570af302Sopenharmony_ci#endif
22570af302Sopenharmony_ci	return v;
23570af302Sopenharmony_ci}
24570af302Sopenharmony_ci
25570af302Sopenharmony_ci#define a_sc a_sc
26570af302Sopenharmony_cistatic inline int a_sc(volatile int *p, int v)
27570af302Sopenharmony_ci{
28570af302Sopenharmony_ci	int r;
29570af302Sopenharmony_ci#if __mips < 2
30570af302Sopenharmony_ci	__asm__ __volatile__ (
31570af302Sopenharmony_ci		".set push ; .set mips2\n\t"
32570af302Sopenharmony_ci		"sc %0, %1"
33570af302Sopenharmony_ci		"\n\t.set pop"
34570af302Sopenharmony_ci		: "=r"(r), "=m"(*p) : "0"(v) : "memory");
35570af302Sopenharmony_ci#else
36570af302Sopenharmony_ci	__asm__ __volatile__ (
37570af302Sopenharmony_ci		"sc %0, %1"
38570af302Sopenharmony_ci		: "=r"(r), "="LLSC_M(*p) : "0"(v) : "memory");
39570af302Sopenharmony_ci#endif
40570af302Sopenharmony_ci	return r;
41570af302Sopenharmony_ci}
42570af302Sopenharmony_ci
43570af302Sopenharmony_ci#define a_barrier a_barrier
44570af302Sopenharmony_cistatic inline void a_barrier()
45570af302Sopenharmony_ci{
46570af302Sopenharmony_ci#if __mips < 2
47570af302Sopenharmony_ci	/* mips2 sync, but using too many directives causes
48570af302Sopenharmony_ci	 * gcc not to inline it, so encode with .long instead. */
49570af302Sopenharmony_ci	__asm__ __volatile__ (".long 0xf" : : : "memory");
50570af302Sopenharmony_ci#else
51570af302Sopenharmony_ci	__asm__ __volatile__ ("sync" : : : "memory");
52570af302Sopenharmony_ci#endif
53570af302Sopenharmony_ci}
54570af302Sopenharmony_ci
55570af302Sopenharmony_ci#define a_pre_llsc a_barrier
56570af302Sopenharmony_ci#define a_post_llsc a_barrier
57570af302Sopenharmony_ci
58570af302Sopenharmony_ci#undef LLSC_M
59