]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/include/atomic.h
Import DTS files from Linux 5.4
[FreeBSD/FreeBSD.git] / sys / mips / include / atomic.h
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *      from: src/sys/alpha/include/atomic.h,v 1.21.2.3 2005/10/06 18:12:05 jhb
29  * $FreeBSD$
30  */
31
32 #ifndef _MACHINE_ATOMIC_H_
33 #define _MACHINE_ATOMIC_H_
34
35 #ifndef _SYS_CDEFS_H_
36 #error this file needs sys/cdefs.h as a prerequisite
37 #endif
38
39 #include <sys/atomic_common.h>
40
41 /*
42  * Note: All the 64-bit atomic operations are only atomic when running
43  * in 64-bit mode.  It is assumed that code compiled for n32 and n64
44  * fits into this definition and no further safeties are needed.
45  *
46  * It is also assumed that the add, subtract and other arithmetic is
47  * done on numbers not pointers.  The special rules for n32 pointers
48  * do not have atomic operations defined for them, but generally shouldn't
49  * need atomic operations.
50  */
51 #ifndef __MIPS_PLATFORM_SYNC_NOPS
52 #define __MIPS_PLATFORM_SYNC_NOPS ""
53 #endif
54
55 static __inline  void
56 mips_sync(void)
57 {
58         __asm __volatile (".set noreorder\n"
59                         "\tsync\n"
60                         __MIPS_PLATFORM_SYNC_NOPS
61                         ".set reorder\n"
62                         : : : "memory");
63 }
64
65 #define mb()    mips_sync()
66 #define wmb()   mips_sync()
67 #define rmb()   mips_sync()
68
69 /*
70  * Various simple arithmetic on memory which is atomic in the presence
71  * of interrupts and SMP safe.
72  */
73
74 void atomic_set_8(__volatile uint8_t *, uint8_t);
75 void atomic_clear_8(__volatile uint8_t *, uint8_t);
76 void atomic_add_8(__volatile uint8_t *, uint8_t);
77 void atomic_subtract_8(__volatile uint8_t *, uint8_t);
78
79 void atomic_set_16(__volatile uint16_t *, uint16_t);
80 void atomic_clear_16(__volatile uint16_t *, uint16_t);
81 void atomic_add_16(__volatile uint16_t *, uint16_t);
82 void atomic_subtract_16(__volatile uint16_t *, uint16_t);
83
84 static __inline int atomic_cmpset_8(__volatile uint8_t *, uint8_t, uint8_t);
85 static __inline int atomic_fcmpset_8(__volatile uint8_t *, uint8_t *, uint8_t);
86 static __inline int atomic_cmpset_16(__volatile uint16_t *, uint16_t, uint16_t);
87 static __inline int atomic_fcmpset_16(__volatile uint16_t *, uint16_t *, uint16_t);
88
89 static __inline void
90 atomic_set_32(__volatile uint32_t *p, uint32_t v)
91 {
92         uint32_t temp;
93
94         __asm __volatile (
95                 "1:\tll %0, %3\n\t"             /* load old value */
96                 "or     %0, %2, %0\n\t"         /* calculate new value */
97                 "sc     %0, %1\n\t"             /* attempt to store */
98                 "beqz   %0, 1b\n\t"             /* spin if failed */
99                 : "=&r" (temp), "=m" (*p)
100                 : "r" (v), "m" (*p)
101                 : "memory");
102
103 }
104
105 static __inline void
106 atomic_clear_32(__volatile uint32_t *p, uint32_t v)
107 {
108         uint32_t temp;
109         v = ~v;
110
111         __asm __volatile (
112                 "1:\tll %0, %3\n\t"             /* load old value */
113                 "and    %0, %2, %0\n\t"         /* calculate new value */
114                 "sc     %0, %1\n\t"             /* attempt to store */
115                 "beqz   %0, 1b\n\t"             /* spin if failed */
116                 : "=&r" (temp), "=m" (*p)
117                 : "r" (v), "m" (*p)
118                 : "memory");
119 }
120
121 static __inline void
122 atomic_add_32(__volatile uint32_t *p, uint32_t v)
123 {
124         uint32_t temp;
125
126         __asm __volatile (
127                 "1:\tll %0, %3\n\t"             /* load old value */
128                 "addu   %0, %2, %0\n\t"         /* calculate new value */
129                 "sc     %0, %1\n\t"             /* attempt to store */
130                 "beqz   %0, 1b\n\t"             /* spin if failed */
131                 : "=&r" (temp), "=m" (*p)
132                 : "r" (v), "m" (*p)
133                 : "memory");
134 }
135
136 static __inline void
137 atomic_subtract_32(__volatile uint32_t *p, uint32_t v)
138 {
139         uint32_t temp;
140
141         __asm __volatile (
142                 "1:\tll %0, %3\n\t"             /* load old value */
143                 "subu   %0, %2\n\t"             /* calculate new value */
144                 "sc     %0, %1\n\t"             /* attempt to store */
145                 "beqz   %0, 1b\n\t"             /* spin if failed */
146                 : "=&r" (temp), "=m" (*p)
147                 : "r" (v), "m" (*p)
148                 : "memory");
149 }
150
151 static __inline uint32_t
152 atomic_readandclear_32(__volatile uint32_t *addr)
153 {
154         uint32_t result,temp;
155
156         __asm __volatile (
157                 "1:\tll  %0,%3\n\t"     /* load current value, asserting lock */
158                 "li      %1,0\n\t"              /* value to store */
159                 "sc      %1,%2\n\t"     /* attempt to store */
160                 "beqz    %1, 1b\n\t"            /* if the store failed, spin */
161                 : "=&r"(result), "=&r"(temp), "=m" (*addr)
162                 : "m" (*addr)
163                 : "memory");
164
165         return result;
166 }
167
168 static __inline uint32_t
169 atomic_readandset_32(__volatile uint32_t *addr, uint32_t value)
170 {
171         uint32_t result,temp;
172
173         __asm __volatile (
174                 "1:\tll  %0,%3\n\t"     /* load current value, asserting lock */
175                 "or      %1,$0,%4\n\t"
176                 "sc      %1,%2\n\t"     /* attempt to store */
177                 "beqz    %1, 1b\n\t"            /* if the store failed, spin */
178                 : "=&r"(result), "=&r"(temp), "=m" (*addr)
179                 : "m" (*addr), "r" (value)
180                 : "memory");
181
182         return result;
183 }
184
185 #if defined(__mips_n64) || defined(__mips_n32)
186 static __inline void
187 atomic_set_64(__volatile uint64_t *p, uint64_t v)
188 {
189         uint64_t temp;
190
191         __asm __volatile (
192                 "1:\n\t"
193                 "lld    %0, %3\n\t"             /* load old value */
194                 "or     %0, %2, %0\n\t"         /* calculate new value */
195                 "scd    %0, %1\n\t"             /* attempt to store */
196                 "beqz   %0, 1b\n\t"             /* spin if failed */
197                 : "=&r" (temp), "=m" (*p)
198                 : "r" (v), "m" (*p)
199                 : "memory");
200
201 }
202
203 static __inline void
204 atomic_clear_64(__volatile uint64_t *p, uint64_t v)
205 {
206         uint64_t temp;
207         v = ~v;
208
209         __asm __volatile (
210                 "1:\n\t"
211                 "lld    %0, %3\n\t"             /* load old value */
212                 "and    %0, %2, %0\n\t"         /* calculate new value */
213                 "scd    %0, %1\n\t"             /* attempt to store */
214                 "beqz   %0, 1b\n\t"             /* spin if failed */
215                 : "=&r" (temp), "=m" (*p)
216                 : "r" (v), "m" (*p)
217                 : "memory");
218 }
219
220 static __inline void
221 atomic_add_64(__volatile uint64_t *p, uint64_t v)
222 {
223         uint64_t temp;
224
225         __asm __volatile (
226                 "1:\n\t"
227                 "lld    %0, %3\n\t"             /* load old value */
228                 "daddu  %0, %2, %0\n\t"         /* calculate new value */
229                 "scd    %0, %1\n\t"             /* attempt to store */
230                 "beqz   %0, 1b\n\t"             /* spin if failed */
231                 : "=&r" (temp), "=m" (*p)
232                 : "r" (v), "m" (*p)
233                 : "memory");
234 }
235
236 static __inline void
237 atomic_subtract_64(__volatile uint64_t *p, uint64_t v)
238 {
239         uint64_t temp;
240
241         __asm __volatile (
242                 "1:\n\t"
243                 "lld    %0, %3\n\t"             /* load old value */
244                 "dsubu  %0, %2\n\t"             /* calculate new value */
245                 "scd    %0, %1\n\t"             /* attempt to store */
246                 "beqz   %0, 1b\n\t"             /* spin if failed */
247                 : "=&r" (temp), "=m" (*p)
248                 : "r" (v), "m" (*p)
249                 : "memory");
250 }
251
252 static __inline uint64_t
253 atomic_readandclear_64(__volatile uint64_t *addr)
254 {
255         uint64_t result,temp;
256
257         __asm __volatile (
258                 "1:\n\t"
259                 "lld     %0, %3\n\t"            /* load old value */
260                 "li      %1, 0\n\t"             /* value to store */
261                 "scd     %1, %2\n\t"            /* attempt to store */
262                 "beqz    %1, 1b\n\t"            /* if the store failed, spin */
263                 : "=&r"(result), "=&r"(temp), "=m" (*addr)
264                 : "m" (*addr)
265                 : "memory");
266
267         return result;
268 }
269
270 static __inline uint64_t
271 atomic_readandset_64(__volatile uint64_t *addr, uint64_t value)
272 {
273         uint64_t result,temp;
274
275         __asm __volatile (
276                 "1:\n\t"
277                 "lld     %0,%3\n\t"             /* Load old value*/
278                 "or      %1,$0,%4\n\t"
279                 "scd     %1,%2\n\t"             /* attempt to store */
280                 "beqz    %1, 1b\n\t"            /* if the store failed, spin */
281                 : "=&r"(result), "=&r"(temp), "=m" (*addr)
282                 : "m" (*addr), "r" (value)
283                 : "memory");
284
285         return result;
286 }
287 #endif
288
289 #define ATOMIC_ACQ_REL(NAME, WIDTH)                                     \
290 static __inline  void                                                   \
291 atomic_##NAME##_acq_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
292 {                                                                       \
293         atomic_##NAME##_##WIDTH(p, v);                                  \
294         mips_sync();                                                    \
295 }                                                                       \
296                                                                         \
297 static __inline  void                                                   \
298 atomic_##NAME##_rel_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
299 {                                                                       \
300         mips_sync();                                                    \
301         atomic_##NAME##_##WIDTH(p, v);                                  \
302 }
303
304 /* Variants of simple arithmetic with memory barriers. */
305 ATOMIC_ACQ_REL(set, 8)
306 ATOMIC_ACQ_REL(clear, 8)
307 ATOMIC_ACQ_REL(add, 8)
308 ATOMIC_ACQ_REL(subtract, 8)
309 ATOMIC_ACQ_REL(set, 16)
310 ATOMIC_ACQ_REL(clear, 16)
311 ATOMIC_ACQ_REL(add, 16)
312 ATOMIC_ACQ_REL(subtract, 16)
313 ATOMIC_ACQ_REL(set, 32)
314 ATOMIC_ACQ_REL(clear, 32)
315 ATOMIC_ACQ_REL(add, 32)
316 ATOMIC_ACQ_REL(subtract, 32)
317 #if defined(__mips_n64) || defined(__mips_n32)
318 ATOMIC_ACQ_REL(set, 64)
319 ATOMIC_ACQ_REL(clear, 64)
320 ATOMIC_ACQ_REL(add, 64)
321 ATOMIC_ACQ_REL(subtract, 64)
322 #endif
323
324 #undef ATOMIC_ACQ_REL
325
326 /*
327  * We assume that a = b will do atomic loads and stores.
328  */
329 #define ATOMIC_STORE_LOAD(WIDTH)                        \
330 static __inline  uint##WIDTH##_t                        \
331 atomic_load_acq_##WIDTH(__volatile uint##WIDTH##_t *p)  \
332 {                                                       \
333         uint##WIDTH##_t v;                              \
334                                                         \
335         v = *p;                                         \
336         mips_sync();                                    \
337         return (v);                                     \
338 }                                                       \
339                                                         \
340 static __inline  void                                   \
341 atomic_store_rel_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
342 {                                                       \
343         mips_sync();                                    \
344         *p = v;                                         \
345 }
346
347 ATOMIC_STORE_LOAD(32)
348 ATOMIC_STORE_LOAD(64)
349 #undef ATOMIC_STORE_LOAD
350
351 #ifdef __mips_n32
352 #define atomic_load_64  atomic_load_acq_64
353 #endif
354
355 /*
356  * Atomically compare the value stored at *p with cmpval and if the
357  * two values are equal, update the value of *p with newval. Returns
358  * zero if the compare failed, nonzero otherwise.
359  */
360 static __inline int
361 atomic_cmpset_32(__volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
362 {
363         int ret;
364
365         __asm __volatile (
366                 "1:\tll %0, %4\n\t"             /* load old value */
367                 "bne %0, %2, 2f\n\t"            /* compare */
368                 "move %0, %3\n\t"               /* value to store */
369                 "sc %0, %1\n\t"                 /* attempt to store */
370                 "beqz %0, 1b\n\t"               /* if it failed, spin */
371                 "j 3f\n\t"
372                 "2:\n\t"
373                 "li     %0, 0\n\t"
374                 "3:\n"
375                 : "=&r" (ret), "=m" (*p)
376                 : "r" (cmpval), "r" (newval), "m" (*p)
377                 : "memory");
378
379         return ret;
380 }
381
382 /*
383  * Atomically compare the value stored at *p with cmpval and if the
384  * two values are equal, update the value of *p with newval. Returns
385  * zero if the compare failed, nonzero otherwise.
386  */
387 static __inline int
388 atomic_fcmpset_32(__volatile uint32_t *p, uint32_t *cmpval, uint32_t newval)
389 {
390         int ret;
391
392         /*
393          * The following sequence (similar to that in atomic_fcmpset_64) will
394          * attempt to update the value of *p with newval if the comparison
395          * succeeds.  Note that they'll exit regardless of whether the store
396          * actually succeeded, leaving *cmpval untouched.  This is in line with
397          * the documentation of atomic_fcmpset_<type>() in atomic(9) for ll/sc
398          * architectures.
399          */
400         __asm __volatile (
401                 "ll     %0, %1\n\t"             /* load old value */
402                 "bne    %0, %4, 1f\n\t"         /* compare */
403                 "move   %0, %3\n\t"             /* value to store */
404                 "sc     %0, %1\n\t"             /* attempt to store */
405                 "j      2f\n\t"                 /* exit regardless of success */
406                 "nop\n\t"                       /* avoid delay slot accident */
407                 "1:\n\t"
408                 "sw     %0, %2\n\t"             /* save old value */
409                 "li     %0, 0\n\t"
410                 "2:\n"
411                 : "=&r" (ret), "+m" (*p), "=m" (*cmpval)
412                 : "r" (newval), "r" (*cmpval)
413                 : "memory");
414         return ret;
415 }
416
417 #define ATOMIC_CMPSET_ACQ_REL(WIDTH)                                    \
418 static __inline  int                                                    \
419 atomic_cmpset_acq_##WIDTH(__volatile uint##WIDTH##_t *p,                \
420     uint##WIDTH##_t cmpval, uint##WIDTH##_t newval)                     \
421 {                                                                       \
422         int retval;                                                     \
423                                                                         \
424         retval = atomic_cmpset_##WIDTH(p, cmpval, newval);              \
425         mips_sync();                                                    \
426         return (retval);                                                \
427 }                                                                       \
428                                                                         \
429 static __inline  int                                                    \
430 atomic_cmpset_rel_##WIDTH(__volatile uint##WIDTH##_t *p,                \
431     uint##WIDTH##_t cmpval, uint##WIDTH##_t newval)                     \
432 {                                                                       \
433         mips_sync();                                                    \
434         return (atomic_cmpset_##WIDTH(p, cmpval, newval));              \
435 }
436
437 #define ATOMIC_FCMPSET_ACQ_REL(WIDTH)                                   \
438 static __inline  int                                                    \
439 atomic_fcmpset_acq_##WIDTH(__volatile uint##WIDTH##_t *p,               \
440     uint##WIDTH##_t *cmpval, uint##WIDTH##_t newval)                    \
441 {                                                                       \
442         int retval;                                                     \
443                                                                         \
444         retval = atomic_fcmpset_##WIDTH(p, cmpval, newval);             \
445         mips_sync();                                                    \
446         return (retval);                                                \
447 }                                                                       \
448                                                                         \
449 static __inline  int                                                    \
450 atomic_fcmpset_rel_##WIDTH(__volatile uint##WIDTH##_t *p,               \
451     uint##WIDTH##_t *cmpval, uint##WIDTH##_t newval)                    \
452 {                                                                       \
453         mips_sync();                                                    \
454         return (atomic_fcmpset_##WIDTH(p, cmpval, newval));             \
455 }
456
457 /*
458  * Atomically compare the value stored at *p with cmpval and if the
459  * two values are equal, update the value of *p with newval. Returns
460  * zero if the compare failed, nonzero otherwise.
461  */
462 ATOMIC_CMPSET_ACQ_REL(8);
463 ATOMIC_CMPSET_ACQ_REL(16);
464 ATOMIC_CMPSET_ACQ_REL(32);
465 ATOMIC_FCMPSET_ACQ_REL(8);
466 ATOMIC_FCMPSET_ACQ_REL(16);
467 ATOMIC_FCMPSET_ACQ_REL(32);
468
469 /*
470  * Atomically add the value of v to the integer pointed to by p and return
471  * the previous value of *p.
472  */
473 static __inline uint32_t
474 atomic_fetchadd_32(__volatile uint32_t *p, uint32_t v)
475 {
476         uint32_t value, temp;
477
478         __asm __volatile (
479                 "1:\tll %0, %1\n\t"             /* load old value */
480                 "addu %2, %3, %0\n\t"           /* calculate new value */
481                 "sc %2, %1\n\t"                 /* attempt to store */
482                 "beqz %2, 1b\n\t"               /* spin if failed */
483                 : "=&r" (value), "=m" (*p), "=&r" (temp)
484                 : "r" (v), "m" (*p));
485         return (value);
486 }
487
488 #if defined(__mips_n64) || defined(__mips_n32)
489 /*
490  * Atomically compare the value stored at *p with cmpval and if the
491  * two values are equal, update the value of *p with newval. Returns
492  * zero if the compare failed, nonzero otherwise.
493  */
494 static __inline int
495 atomic_cmpset_64(__volatile uint64_t *p, uint64_t cmpval, uint64_t newval)
496 {
497         int ret;
498
499         __asm __volatile (
500                 "1:\n\t"
501                 "lld    %0, %4\n\t"             /* load old value */
502                 "bne    %0, %2, 2f\n\t"         /* compare */
503                 "move   %0, %3\n\t"             /* value to store */
504                 "scd    %0, %1\n\t"             /* attempt to store */
505                 "beqz   %0, 1b\n\t"             /* if it failed, spin */
506                 "j      3f\n\t"
507                 "2:\n\t"
508                 "li     %0, 0\n\t"
509                 "3:\n"
510                 : "=&r" (ret), "=m" (*p)
511                 : "r" (cmpval), "r" (newval), "m" (*p)
512                 : "memory");
513
514         return ret;
515 }
516
517 static __inline int
518 atomic_fcmpset_64(__volatile uint64_t *p, uint64_t *cmpval, uint64_t newval)
519 {
520         int ret;
521
522         __asm __volatile (
523                 "lld    %0, %1\n\t"             /* load old value */
524                 "bne    %0, %4, 1f\n\t"         /* compare */
525                 "move   %0, %3\n\t"             /* value to store */
526                 "scd    %0, %1\n\t"             /* attempt to store */
527                 "j      2f\n\t"                 /* exit regardless of success */
528                 "nop\n\t"                       /* avoid delay slot accident */
529                 "1:\n\t"
530                 "sd     %0, %2\n\t"             /* save old value */
531                 "li     %0, 0\n\t"
532                 "2:\n"
533                 : "=&r" (ret), "+m" (*p), "=m" (*cmpval)
534                 : "r" (newval), "r" (*cmpval)
535                 : "memory");
536
537         return ret;
538 }
539
540 /*
541  * Atomically compare the value stored at *p with cmpval and if the
542  * two values are equal, update the value of *p with newval. Returns
543  * zero if the compare failed, nonzero otherwise.
544  */
545 ATOMIC_CMPSET_ACQ_REL(64);
546 ATOMIC_FCMPSET_ACQ_REL(64);
547
548 /*
549  * Atomically add the value of v to the integer pointed to by p and return
550  * the previous value of *p.
551  */
552 static __inline uint64_t
553 atomic_fetchadd_64(__volatile uint64_t *p, uint64_t v)
554 {
555         uint64_t value, temp;
556
557         __asm __volatile (
558                 "1:\n\t"
559                 "lld    %0, %1\n\t"             /* load old value */
560                 "daddu  %2, %3, %0\n\t"         /* calculate new value */
561                 "scd    %2, %1\n\t"             /* attempt to store */
562                 "beqz   %2, 1b\n\t"             /* spin if failed */
563                 : "=&r" (value), "=m" (*p), "=&r" (temp)
564                 : "r" (v), "m" (*p));
565         return (value);
566 }
567 #endif
568
569 static __inline void
570 atomic_thread_fence_acq(void)
571 {
572
573         mips_sync();
574 }
575
576 static __inline void
577 atomic_thread_fence_rel(void)
578 {
579
580         mips_sync();
581 }
582
583 static __inline void
584 atomic_thread_fence_acq_rel(void)
585 {
586
587         mips_sync();
588 }
589
590 static __inline void
591 atomic_thread_fence_seq_cst(void)
592 {
593
594         mips_sync();
595 }
596
597 /* Operations on chars. */
598 #define atomic_set_char         atomic_set_8
599 #define atomic_set_acq_char     atomic_set_acq_8
600 #define atomic_set_rel_char     atomic_set_rel_8
601 #define atomic_clear_char       atomic_clear_8
602 #define atomic_clear_acq_char   atomic_clear_acq_8
603 #define atomic_clear_rel_char   atomic_clear_rel_8
604 #define atomic_add_char         atomic_add_8
605 #define atomic_add_acq_char     atomic_add_acq_8
606 #define atomic_add_rel_char     atomic_add_rel_8
607 #define atomic_subtract_char    atomic_subtract_8
608 #define atomic_subtract_acq_char        atomic_subtract_acq_8
609 #define atomic_subtract_rel_char        atomic_subtract_rel_8
610 #define atomic_cmpset_char      atomic_cmpset_8
611 #define atomic_cmpset_acq_char  atomic_cmpset_acq_8
612 #define atomic_cmpset_rel_char  atomic_cmpset_rel_8
613 #define atomic_fcmpset_char     atomic_fcmpset_8
614 #define atomic_fcmpset_acq_char atomic_fcmpset_acq_8
615 #define atomic_fcmpset_rel_char atomic_fcmpset_rel_8
616
617 /* Operations on shorts. */
618 #define atomic_set_short        atomic_set_16
619 #define atomic_set_acq_short    atomic_set_acq_16
620 #define atomic_set_rel_short    atomic_set_rel_16
621 #define atomic_clear_short      atomic_clear_16
622 #define atomic_clear_acq_short  atomic_clear_acq_16
623 #define atomic_clear_rel_short  atomic_clear_rel_16
624 #define atomic_add_short        atomic_add_16
625 #define atomic_add_acq_short    atomic_add_acq_16
626 #define atomic_add_rel_short    atomic_add_rel_16
627 #define atomic_subtract_short   atomic_subtract_16
628 #define atomic_subtract_acq_short       atomic_subtract_acq_16
629 #define atomic_subtract_rel_short       atomic_subtract_rel_16
630 #define atomic_cmpset_short     atomic_cmpset_16
631 #define atomic_cmpset_acq_short atomic_cmpset_acq_16
632 #define atomic_cmpset_rel_short atomic_cmpset_rel_16
633 #define atomic_fcmpset_short    atomic_fcmpset_16
634 #define atomic_fcmpset_acq_short        atomic_fcmpset_acq_16
635 #define atomic_fcmpset_rel_short        atomic_fcmpset_rel_16
636
637 /* Operations on ints. */
638 #define atomic_set_int          atomic_set_32
639 #define atomic_set_acq_int      atomic_set_acq_32
640 #define atomic_set_rel_int      atomic_set_rel_32
641 #define atomic_clear_int        atomic_clear_32
642 #define atomic_clear_acq_int    atomic_clear_acq_32
643 #define atomic_clear_rel_int    atomic_clear_rel_32
644 #define atomic_add_int          atomic_add_32
645 #define atomic_add_acq_int      atomic_add_acq_32
646 #define atomic_add_rel_int      atomic_add_rel_32
647 #define atomic_subtract_int     atomic_subtract_32
648 #define atomic_subtract_acq_int atomic_subtract_acq_32
649 #define atomic_subtract_rel_int atomic_subtract_rel_32
650 #define atomic_cmpset_int       atomic_cmpset_32
651 #define atomic_cmpset_acq_int   atomic_cmpset_acq_32
652 #define atomic_cmpset_rel_int   atomic_cmpset_rel_32
653 #define atomic_fcmpset_int      atomic_fcmpset_32
654 #define atomic_fcmpset_acq_int  atomic_fcmpset_acq_32
655 #define atomic_fcmpset_rel_int  atomic_fcmpset_rel_32
656 #define atomic_load_acq_int     atomic_load_acq_32
657 #define atomic_store_rel_int    atomic_store_rel_32
658 #define atomic_readandclear_int atomic_readandclear_32
659 #define atomic_readandset_int   atomic_readandset_32
660 #define atomic_fetchadd_int     atomic_fetchadd_32
661
662 /*
663  * I think the following is right, even for n32.  For n32 the pointers
664  * are still 32-bits, so we need to operate on them as 32-bit quantities,
665  * even though they are sign extended in operation.  For longs, there's
666  * no question because they are always 32-bits.
667  */
668 #ifdef __mips_n64
669 /* Operations on longs. */
670 #define atomic_set_long         atomic_set_64
671 #define atomic_set_acq_long     atomic_set_acq_64
672 #define atomic_set_rel_long     atomic_set_rel_64
673 #define atomic_clear_long       atomic_clear_64
674 #define atomic_clear_acq_long   atomic_clear_acq_64
675 #define atomic_clear_rel_long   atomic_clear_rel_64
676 #define atomic_add_long         atomic_add_64
677 #define atomic_add_acq_long     atomic_add_acq_64
678 #define atomic_add_rel_long     atomic_add_rel_64
679 #define atomic_subtract_long    atomic_subtract_64
680 #define atomic_subtract_acq_long        atomic_subtract_acq_64
681 #define atomic_subtract_rel_long        atomic_subtract_rel_64
682 #define atomic_cmpset_long      atomic_cmpset_64
683 #define atomic_cmpset_acq_long  atomic_cmpset_acq_64
684 #define atomic_cmpset_rel_long  atomic_cmpset_rel_64
685 #define atomic_fcmpset_long     atomic_fcmpset_64
686 #define atomic_fcmpset_acq_long atomic_fcmpset_acq_64
687 #define atomic_fcmpset_rel_long atomic_fcmpset_rel_64
688 #define atomic_load_acq_long    atomic_load_acq_64
689 #define atomic_store_rel_long   atomic_store_rel_64
690 #define atomic_fetchadd_long    atomic_fetchadd_64
691 #define atomic_readandclear_long        atomic_readandclear_64
692
693 #else /* !__mips_n64 */
694
695 /* Operations on longs. */
696 #define atomic_set_long(p, v)                                           \
697         atomic_set_32((volatile u_int *)(p), (u_int)(v))
698 #define atomic_set_acq_long(p, v)                                       \
699         atomic_set_acq_32((volatile u_int *)(p), (u_int)(v))
700 #define atomic_set_rel_long(p, v)                                       \
701         atomic_set_rel_32((volatile u_int *)(p), (u_int)(v))
702 #define atomic_clear_long(p, v)                                         \
703         atomic_clear_32((volatile u_int *)(p), (u_int)(v))
704 #define atomic_clear_acq_long(p, v)                                     \
705         atomic_clear_acq_32((volatile u_int *)(p), (u_int)(v))
706 #define atomic_clear_rel_long(p, v)                                     \
707         atomic_clear_rel_32((volatile u_int *)(p), (u_int)(v))
708 #define atomic_add_long(p, v)                                           \
709         atomic_add_32((volatile u_int *)(p), (u_int)(v))
710 #define atomic_add_acq_long(p, v)                                       \
711         atomic_add_32((volatile u_int *)(p), (u_int)(v))
712 #define atomic_add_rel_long(p, v)                                       \
713         atomic_add_32((volatile u_int *)(p), (u_int)(v))
714 #define atomic_subtract_long(p, v)                                      \
715         atomic_subtract_32((volatile u_int *)(p), (u_int)(v))
716 #define atomic_subtract_acq_long(p, v)                                  \
717         atomic_subtract_acq_32((volatile u_int *)(p), (u_int)(v))
718 #define atomic_subtract_rel_long(p, v)                                  \
719         atomic_subtract_rel_32((volatile u_int *)(p), (u_int)(v))
720 #define atomic_cmpset_long(p, cmpval, newval)                           \
721         atomic_cmpset_32((volatile u_int *)(p), (u_int)(cmpval),        \
722             (u_int)(newval))
723 #define atomic_cmpset_acq_long(p, cmpval, newval)                       \
724         atomic_cmpset_acq_32((volatile u_int *)(p), (u_int)(cmpval),    \
725             (u_int)(newval))
726 #define atomic_cmpset_rel_long(p, cmpval, newval)                       \
727         atomic_cmpset_rel_32((volatile u_int *)(p), (u_int)(cmpval),    \
728             (u_int)(newval))
729 #define atomic_fcmpset_long(p, cmpval, newval)                          \
730         atomic_fcmpset_32((volatile u_int *)(p), (u_int *)(cmpval),     \
731             (u_int)(newval))
732 #define atomic_fcmpset_acq_long(p, cmpval, newval)                      \
733         atomic_fcmpset_acq_32((volatile u_int *)(p), (u_int *)(cmpval), \
734             (u_int)(newval))
735 #define atomic_fcmpset_rel_long(p, cmpval, newval)                      \
736         atomic_fcmpset_rel_32((volatile u_int *)(p), (u_int *)(cmpval), \
737             (u_int)(newval))
738 #define atomic_load_acq_long(p)                                         \
739         (u_long)atomic_load_acq_32((volatile u_int *)(p))
740 #define atomic_store_rel_long(p, v)                                     \
741         atomic_store_rel_32((volatile u_int *)(p), (u_int)(v))
742 #define atomic_fetchadd_long(p, v)                                      \
743         atomic_fetchadd_32((volatile u_int *)(p), (u_int)(v))
744 #define atomic_readandclear_long(p)                                     \
745         atomic_readandclear_32((volatile u_int *)(p))
746
747 #endif /* __mips_n64 */
748
749 /* Operations on pointers. */
750 #define atomic_set_ptr          atomic_set_long
751 #define atomic_set_acq_ptr      atomic_set_acq_long
752 #define atomic_set_rel_ptr      atomic_set_rel_long
753 #define atomic_clear_ptr        atomic_clear_long
754 #define atomic_clear_acq_ptr    atomic_clear_acq_long
755 #define atomic_clear_rel_ptr    atomic_clear_rel_long
756 #define atomic_add_ptr          atomic_add_long
757 #define atomic_add_acq_ptr      atomic_add_acq_long
758 #define atomic_add_rel_ptr      atomic_add_rel_long
759 #define atomic_subtract_ptr     atomic_subtract_long
760 #define atomic_subtract_acq_ptr atomic_subtract_acq_long
761 #define atomic_subtract_rel_ptr atomic_subtract_rel_long
762 #define atomic_cmpset_ptr       atomic_cmpset_long
763 #define atomic_cmpset_acq_ptr   atomic_cmpset_acq_long
764 #define atomic_cmpset_rel_ptr   atomic_cmpset_rel_long
765 #define atomic_fcmpset_ptr      atomic_fcmpset_long
766 #define atomic_fcmpset_acq_ptr  atomic_fcmpset_acq_long
767 #define atomic_fcmpset_rel_ptr  atomic_fcmpset_rel_long
768 #define atomic_load_acq_ptr     atomic_load_acq_long
769 #define atomic_store_rel_ptr    atomic_store_rel_long
770 #define atomic_readandclear_ptr atomic_readandclear_long
771
772 static __inline unsigned int
773 atomic_swap_int(volatile unsigned int *ptr, const unsigned int value)
774 {
775         unsigned int retval;
776
777         retval = *ptr;
778
779         while (!atomic_fcmpset_int(ptr, &retval, value))
780                 ;
781         return (retval);
782 }
783
784 static __inline uint32_t
785 atomic_swap_32(volatile uint32_t *ptr, const uint32_t value)
786 {
787         uint32_t retval;
788
789         retval = *ptr;
790
791         while (!atomic_fcmpset_32(ptr, &retval, value))
792                 ;
793         return (retval);
794 }
795
796 #if defined(__mips_n64) || defined(__mips_n32)
797 static __inline uint64_t
798 atomic_swap_64(volatile uint64_t *ptr, const uint64_t value)
799 {
800         uint64_t retval;
801
802         retval = *ptr;
803
804         while (!atomic_fcmpset_64(ptr, &retval, value))
805                 ;
806         return (retval);
807 }
808 #endif
809
810 #ifdef __mips_n64
811 static __inline unsigned long
812 atomic_swap_long(volatile unsigned long *ptr, const unsigned long value)
813 {
814         unsigned long retval;
815
816         retval = *ptr;
817
818         while (!atomic_fcmpset_64((volatile uint64_t *)ptr,
819             (uint64_t *)&retval, value))
820                 ;
821         return (retval);
822 }
823 #else
824 static __inline unsigned long
825 atomic_swap_long(volatile unsigned long *ptr, const unsigned long value)
826 {
827         unsigned long retval;
828
829         retval = *ptr;
830
831         while (!atomic_fcmpset_32((volatile uint32_t *)ptr,
832             (uint32_t *)&retval, value))
833                 ;
834         return (retval);
835 }
836 #endif
837 #define atomic_swap_ptr(ptr, value) atomic_swap_long((unsigned long *)(ptr), value)
838
839 #include <sys/_atomic_subword.h>
840
841 #endif /* ! _MACHINE_ATOMIC_H_ */