1 /*
2     Copyright (c) 2013, The Regents of the University of California (Regents).
3     All Rights Reserved.
4 
5     Redistribution and use in source and binary forms, with or without
6     modification, are permitted provided that the following conditions are met:
7     1. Redistributions of source code must retain the above copyright
8        notice, this list of conditions and the following disclaimer.
9     2. Redistributions in binary form must reproduce the above copyright
10        notice, this list of conditions and the following disclaimer in the
11        documentation and/or other materials provided with the distribution.
12     3. Neither the name of the Regents nor the
13        names of its contributors may be used to endorse or promote products
14        derived from this software without specific prior written permission.
15 
16     IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
17     SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
18     OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
19     BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20 
21     REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22     THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23     PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
24     HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
25     MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26 */
27 
28 /***********************************************************************************
29  * Record of Microchip changes
30  */
31 
32 #ifndef RISCV_ATOMIC_H
33 #define RISCV_ATOMIC_H
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #define mb() __asm__ volatile ("fence" ::: "memory")
40 #define atomic_set(ptr, val) (*(volatile typeof(*(ptr)) *)(ptr) = val)
41 #define atomic_read(ptr) (*(volatile typeof(*(ptr)) *)(ptr))
42 
43 #ifdef __riscv_atomic
44 # define atomic_swap(ptr, swp) __sync_lock_test_and_set(ptr, swp)
45 # define atomic_or(ptr, inc) __sync_fetch_and_or(ptr, inc)
46 #else
47 #define atomic_binop(ptr, inc, op) ({ \
48   long flags = disable_irqsave(); \
49   typeof(*(ptr)) res = atomic_read(ptr); \
50   atomic_set(ptr, op); \
51   enable_irqrestore(flags); \
52   res; })
53 #define atomic_or(ptr, inc) atomic_binop(ptr, inc, res | (inc))
54 #define atomic_swap(ptr, swp) atomic_binop(ptr, swp, (swp))
55 #endif
56 
57 #ifdef __cplusplus
58 }
59 #endif
60 
61 #endif  //RISCV_ATOMIC_H
62 
63