1 | /* |
---|
2 | * hal_atomic.c - implementation of Generic Atomic Operations API for TSAR-MIPS32 |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH.. |
---|
9 | * |
---|
10 | * ALMOS-MKH. is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH. is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH.; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include <hal_types.h> |
---|
25 | |
---|
26 | //////////////////////////////////// |
---|
27 | void hal_atomic_and( uint32_t * ptr, |
---|
28 | int32_t val ) |
---|
29 | { |
---|
30 | asm volatile ( |
---|
31 | ".set noreorder \n" |
---|
32 | "1: \n" |
---|
33 | "ll $3, (%0) \n" |
---|
34 | "and $3, $3, %1 \n" |
---|
35 | "sc $3, (%0) \n" |
---|
36 | "beq $3, $0, 1b \n" |
---|
37 | "nop \n" |
---|
38 | "sync \n" |
---|
39 | ".set reorder \n" |
---|
40 | : : "r" (ptr), "r" (val) : "$3" , "memory" ); |
---|
41 | } |
---|
42 | |
---|
43 | /////////////////////////////////// |
---|
44 | void hal_atomic_or( uint32_t * ptr, |
---|
45 | int32_t val ) |
---|
46 | { |
---|
47 | asm volatile ( |
---|
48 | ".set noreorder \n" |
---|
49 | "1: \n" |
---|
50 | "ll $3, (%0) \n" |
---|
51 | "or $3, $3, %1 \n" |
---|
52 | "sc $3, (%0) \n" |
---|
53 | "beq $3, $0, 1b \n" |
---|
54 | "nop \n" |
---|
55 | "sync \n" |
---|
56 | ".set reorder \n" |
---|
57 | : : "r" (ptr), "r" (val) : "$3" , "memory" ); |
---|
58 | } |
---|
59 | |
---|
60 | /////////////////////////////////////// |
---|
61 | uint32_t hal_atomic_add( void * ptr, |
---|
62 | int32_t val ) |
---|
63 | { |
---|
64 | int32_t current; |
---|
65 | |
---|
66 | asm volatile ( |
---|
67 | ".set noreorder \n" |
---|
68 | "1: \n" |
---|
69 | "ll %0, (%1) \n" |
---|
70 | "addu $3, %0, %2 \n" |
---|
71 | "sc $3, (%1) \n" |
---|
72 | "beq $3, $0, 1b \n" |
---|
73 | "nop \n" |
---|
74 | "sync \n" |
---|
75 | ".set reorder \n" |
---|
76 | :"=&r"(current) : "r" (ptr), "r" (val) : "$3" , "memory" ); |
---|
77 | |
---|
78 | return current; |
---|
79 | } |
---|
80 | |
---|
81 | /////////////////////////////////////// |
---|
82 | int32_t hal_atomic_inc( uint32_t * ptr) |
---|
83 | { |
---|
84 | return hal_atomic_add( ptr , 1 ); |
---|
85 | } |
---|
86 | |
---|
87 | /////////////////////////////////////// |
---|
88 | int32_t hal_atomic_dec( uint32_t * ptr) |
---|
89 | { |
---|
90 | return hal_atomic_add( ptr , -1 ); |
---|
91 | } |
---|
92 | |
---|
93 | ////////////////////////////////////// |
---|
94 | bool_t hal_atomic_cas( uint32_t * ptr, |
---|
95 | uint32_t old, |
---|
96 | uint32_t new ) |
---|
97 | { |
---|
98 | bool_t isAtomic; |
---|
99 | |
---|
100 | asm volatile ( |
---|
101 | ".set noreorder \n" |
---|
102 | "sync \n" |
---|
103 | "or $8, $0, %3 \n" |
---|
104 | "ll $3, (%1) \n" |
---|
105 | "bne $3, %2, 1f \n" |
---|
106 | "li $7, 0 \n" |
---|
107 | "sc $8, (%1) \n" |
---|
108 | "or $7, $8, $0 \n" |
---|
109 | "sync \n" |
---|
110 | ".set reorder \n" |
---|
111 | "1: \n" |
---|
112 | "or %0, $7, $0 \n" |
---|
113 | : "=&r" (isAtomic): "r" (ptr), "r" (old) , "r" (new) : "$3", "$7", "$8"); |
---|
114 | |
---|
115 | return isAtomic; |
---|
116 | } |
---|
117 | |
---|
118 | /////////////////////////////////////////// |
---|
119 | bool_t hal_atomic_test_set( uint32_t * ptr, |
---|
120 | int32_t val ) |
---|
121 | { |
---|
122 | return hal_atomic_cas( ptr , 0 , val ); |
---|
123 | } |
---|
124 | |
---|