xref: /openbmc/qemu/tests/tcg/hexagon/atomics.c (revision 6016b7b4)
1 /*
2  *  Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <inttypes.h>
23 #include <pthread.h>
24 
25 /* Using volatile because we are testing atomics */
26 static inline int atomic_inc32(volatile int *x)
27 {
28     int old, dummy;
29     __asm__ __volatile__(
30         "1: %0 = memw_locked(%2)\n\t"
31         "   %1 = add(%0, #1)\n\t"
32         "   memw_locked(%2, p0) = %1\n\t"
33         "   if (!p0) jump 1b\n\t"
34         : "=&r"(old), "=&r"(dummy)
35         : "r"(x)
36         : "p0", "memory");
37     return old;
38 }
39 
40 /* Using volatile because we are testing atomics */
41 static inline long long atomic_inc64(volatile long long *x)
42 {
43     long long old, dummy;
44     __asm__ __volatile__(
45         "1: %0 = memd_locked(%2)\n\t"
46         "   %1 = #1\n\t"
47         "   %1 = add(%0, %1)\n\t"
48         "   memd_locked(%2, p0) = %1\n\t"
49         "   if (!p0) jump 1b\n\t"
50         : "=&r"(old), "=&r"(dummy)
51         : "r"(x)
52         : "p0", "memory");
53     return old;
54 }
55 
56 /* Using volatile because we are testing atomics */
57 static inline int atomic_dec32(volatile int *x)
58 {
59     int old, dummy;
60     __asm__ __volatile__(
61         "1: %0 = memw_locked(%2)\n\t"
62         "   %1 = add(%0, #-1)\n\t"
63         "   memw_locked(%2, p0) = %1\n\t"
64         "   if (!p0) jump 1b\n\t"
65         : "=&r"(old), "=&r"(dummy)
66         : "r"(x)
67         : "p0", "memory");
68     return old;
69 }
70 
71 /* Using volatile because we are testing atomics */
72 static inline long long atomic_dec64(volatile long long *x)
73 {
74     long long old, dummy;
75     __asm__ __volatile__(
76         "1: %0 = memd_locked(%2)\n\t"
77         "   %1 = #-1\n\t"
78         "   %1 = add(%0, %1)\n\t"
79         "   memd_locked(%2, p0) = %1\n\t"
80         "   if (!p0) jump 1b\n\t"
81         : "=&r"(old), "=&r"(dummy)
82         : "r"(x)
83         : "p0", "memory");
84     return old;
85 }
86 
87 #define LOOP_CNT 1000
88 /* Using volatile because we are testing atomics */
89 volatile int tick32 = 1;
90 /* Using volatile because we are testing atomics */
91 volatile long long tick64 = 1;
92 int err;
93 
94 void *thread1_func(void *arg)
95 {
96     int i;
97 
98     for (i = 0; i < LOOP_CNT; i++) {
99         atomic_inc32(&tick32);
100         atomic_dec64(&tick64);
101     }
102     return NULL;
103 }
104 
105 void *thread2_func(void *arg)
106 {
107     int i;
108     for (i = 0; i < LOOP_CNT; i++) {
109         atomic_dec32(&tick32);
110         atomic_inc64(&tick64);
111     }
112     return NULL;
113 }
114 
115 void test_pthread(void)
116 {
117     pthread_t tid1, tid2;
118 
119     pthread_create(&tid1, NULL, thread1_func, "hello1");
120     pthread_create(&tid2, NULL, thread2_func, "hello2");
121     pthread_join(tid1, NULL);
122     pthread_join(tid2, NULL);
123 
124     if (tick32 != 1) {
125         printf("ERROR: tick32 %d != 1\n", tick32);
126         err++;
127     }
128     if (tick64 != 1) {
129         printf("ERROR: tick64 %lld != 1\n", tick64);
130         err++;
131     }
132 }
133 
134 int main(int argc, char **argv)
135 {
136     test_pthread();
137     puts(err ? "FAIL" : "PASS");
138     return err;
139 }
140