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_kernel_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 | bool_t hal_atomic_cas( uint32_t * ptr, |
---|
83 | uint32_t old, |
---|
84 | uint32_t new ) |
---|
85 | { |
---|
86 | bool_t isAtomic; |
---|
87 | |
---|
88 | asm volatile ( |
---|
89 | ".set noreorder \n" |
---|
90 | "sync \n" |
---|
91 | "or $8, $0, %3 \n" |
---|
92 | "ll $3, (%1) \n" |
---|
93 | "bne $3, %2, 1f \n" |
---|
94 | "li $7, 0 \n" |
---|
95 | "sc $8, (%1) \n" |
---|
96 | "or $7, $8, $0 \n" |
---|
97 | "sync \n" |
---|
98 | ".set reorder \n" |
---|
99 | "1: \n" |
---|
100 | "or %0, $7, $0 \n" |
---|
101 | : "=&r" (isAtomic): "r" (ptr), "r" (old) , "r" (new) : "$3", "$7", "$8"); |
---|
102 | |
---|
103 | return isAtomic; |
---|
104 | } |
---|
105 | |
---|
106 | /////////////////////////////////////////// |
---|
107 | bool_t hal_atomic_test_set( uint32_t * ptr, |
---|
108 | int32_t val ) |
---|
109 | { |
---|
110 | return hal_atomic_cas( ptr , 0 , val ); |
---|
111 | } |
---|
112 | |
---|