xref: /openbmc/qemu/tests/tcg/hexagon/v68_scalar.c (revision 449d6d9eb44772e69f11d002e3c1e2be8a91c350)
1*860132e2STaylor Simpson /*
2*860132e2STaylor Simpson  *  Copyright(c) 2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
3*860132e2STaylor Simpson  *
4*860132e2STaylor Simpson  *  This program is free software; you can redistribute it and/or modify
5*860132e2STaylor Simpson  *  it under the terms of the GNU General Public License as published by
6*860132e2STaylor Simpson  *  the Free Software Foundation; either version 2 of the License, or
7*860132e2STaylor Simpson  *  (at your option) any later version.
8*860132e2STaylor Simpson  *
9*860132e2STaylor Simpson  *  This program is distributed in the hope that it will be useful,
10*860132e2STaylor Simpson  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11*860132e2STaylor Simpson  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*860132e2STaylor Simpson  *  GNU General Public License for more details.
13*860132e2STaylor Simpson  *
14*860132e2STaylor Simpson  *  You should have received a copy of the GNU General Public License
15*860132e2STaylor Simpson  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
16*860132e2STaylor Simpson  */
17*860132e2STaylor Simpson 
18*860132e2STaylor Simpson #include <stdio.h>
19*860132e2STaylor Simpson #include <stdbool.h>
20*860132e2STaylor Simpson #include <stdint.h>
21*860132e2STaylor Simpson 
22*860132e2STaylor Simpson /*
23*860132e2STaylor Simpson  *  Test the scalar core instructions that are new in v68
24*860132e2STaylor Simpson  */
25*860132e2STaylor Simpson 
26*860132e2STaylor Simpson int err;
27*860132e2STaylor Simpson 
28*860132e2STaylor Simpson static int buffer32[] = { 1, 2, 3, 4 };
29*860132e2STaylor Simpson static long long buffer64[] = { 5, 6, 7, 8 };
30*860132e2STaylor Simpson 
__check32(int line,uint32_t result,uint32_t expect)31*860132e2STaylor Simpson static void __check32(int line, uint32_t result, uint32_t expect)
32*860132e2STaylor Simpson {
33*860132e2STaylor Simpson     if (result != expect) {
34*860132e2STaylor Simpson         printf("ERROR at line %d: 0x%08x != 0x%08x\n",
35*860132e2STaylor Simpson                line, result, expect);
36*860132e2STaylor Simpson         err++;
37*860132e2STaylor Simpson     }
38*860132e2STaylor Simpson }
39*860132e2STaylor Simpson 
40*860132e2STaylor Simpson #define check32(RES, EXP) __check32(__LINE__, RES, EXP)
41*860132e2STaylor Simpson 
__check64(int line,uint64_t result,uint64_t expect)42*860132e2STaylor Simpson static void __check64(int line, uint64_t result, uint64_t expect)
43*860132e2STaylor Simpson {
44*860132e2STaylor Simpson     if (result != expect) {
45*860132e2STaylor Simpson         printf("ERROR at line %d: 0x%016llx != 0x%016llx\n",
46*860132e2STaylor Simpson                line, result, expect);
47*860132e2STaylor Simpson         err++;
48*860132e2STaylor Simpson     }
49*860132e2STaylor Simpson }
50*860132e2STaylor Simpson 
51*860132e2STaylor Simpson #define check64(RES, EXP) __check64(__LINE__, RES, EXP)
52*860132e2STaylor Simpson 
loadw_aq(int * p)53*860132e2STaylor Simpson static inline int loadw_aq(int *p)
54*860132e2STaylor Simpson {
55*860132e2STaylor Simpson     int res;
56*860132e2STaylor Simpson     asm volatile("%0 = memw_aq(%1)\n\t"
57*860132e2STaylor Simpson                  : "=r"(res) : "r"(p));
58*860132e2STaylor Simpson     return res;
59*860132e2STaylor Simpson }
60*860132e2STaylor Simpson 
test_loadw_aq(void)61*860132e2STaylor Simpson static void test_loadw_aq(void)
62*860132e2STaylor Simpson {
63*860132e2STaylor Simpson     int res;
64*860132e2STaylor Simpson 
65*860132e2STaylor Simpson     res = loadw_aq(&buffer32[0]);
66*860132e2STaylor Simpson     check32(res, 1);
67*860132e2STaylor Simpson     res = loadw_aq(&buffer32[1]);
68*860132e2STaylor Simpson     check32(res, 2);
69*860132e2STaylor Simpson }
70*860132e2STaylor Simpson 
loadd_aq(long long * p)71*860132e2STaylor Simpson static inline long long loadd_aq(long long *p)
72*860132e2STaylor Simpson {
73*860132e2STaylor Simpson     long long res;
74*860132e2STaylor Simpson     asm volatile("%0 = memd_aq(%1)\n\t"
75*860132e2STaylor Simpson                  : "=r"(res) : "r"(p));
76*860132e2STaylor Simpson     return res;
77*860132e2STaylor Simpson }
78*860132e2STaylor Simpson 
test_loadd_aq(void)79*860132e2STaylor Simpson static void test_loadd_aq(void)
80*860132e2STaylor Simpson {
81*860132e2STaylor Simpson     long long res;
82*860132e2STaylor Simpson 
83*860132e2STaylor Simpson     res = loadd_aq(&buffer64[2]);
84*860132e2STaylor Simpson     check64(res, 7);
85*860132e2STaylor Simpson     res = loadd_aq(&buffer64[3]);
86*860132e2STaylor Simpson     check64(res, 8);
87*860132e2STaylor Simpson }
88*860132e2STaylor Simpson 
release_at(int * p)89*860132e2STaylor Simpson static inline void release_at(int *p)
90*860132e2STaylor Simpson {
91*860132e2STaylor Simpson     asm volatile("release(%0):at\n\t"
92*860132e2STaylor Simpson                  : : "r"(p));
93*860132e2STaylor Simpson }
94*860132e2STaylor Simpson 
test_release_at(void)95*860132e2STaylor Simpson static void test_release_at(void)
96*860132e2STaylor Simpson {
97*860132e2STaylor Simpson     release_at(&buffer32[2]);
98*860132e2STaylor Simpson     check64(buffer32[2], 3);
99*860132e2STaylor Simpson     release_at(&buffer32[3]);
100*860132e2STaylor Simpson     check64(buffer32[3], 4);
101*860132e2STaylor Simpson }
102*860132e2STaylor Simpson 
release_st(int * p)103*860132e2STaylor Simpson static inline void release_st(int *p)
104*860132e2STaylor Simpson {
105*860132e2STaylor Simpson     asm volatile("release(%0):st\n\t"
106*860132e2STaylor Simpson                  : : "r"(p));
107*860132e2STaylor Simpson }
108*860132e2STaylor Simpson 
test_release_st(void)109*860132e2STaylor Simpson static void test_release_st(void)
110*860132e2STaylor Simpson {
111*860132e2STaylor Simpson     release_st(&buffer32[2]);
112*860132e2STaylor Simpson     check64(buffer32[2], 3);
113*860132e2STaylor Simpson     release_st(&buffer32[3]);
114*860132e2STaylor Simpson     check64(buffer32[3], 4);
115*860132e2STaylor Simpson }
116*860132e2STaylor Simpson 
storew_rl_at(int * p,int val)117*860132e2STaylor Simpson static inline void storew_rl_at(int *p, int val)
118*860132e2STaylor Simpson {
119*860132e2STaylor Simpson     asm volatile("memw_rl(%0):at = %1\n\t"
120*860132e2STaylor Simpson                  : : "r"(p), "r"(val) : "memory");
121*860132e2STaylor Simpson }
122*860132e2STaylor Simpson 
test_storew_rl_at(void)123*860132e2STaylor Simpson static void test_storew_rl_at(void)
124*860132e2STaylor Simpson {
125*860132e2STaylor Simpson     storew_rl_at(&buffer32[2], 9);
126*860132e2STaylor Simpson     check64(buffer32[2], 9);
127*860132e2STaylor Simpson     storew_rl_at(&buffer32[3], 10);
128*860132e2STaylor Simpson     check64(buffer32[3], 10);
129*860132e2STaylor Simpson }
130*860132e2STaylor Simpson 
stored_rl_at(long long * p,long long val)131*860132e2STaylor Simpson static inline void stored_rl_at(long long *p, long long val)
132*860132e2STaylor Simpson {
133*860132e2STaylor Simpson     asm volatile("memd_rl(%0):at = %1\n\t"
134*860132e2STaylor Simpson                  : : "r"(p), "r"(val) : "memory");
135*860132e2STaylor Simpson }
136*860132e2STaylor Simpson 
test_stored_rl_at(void)137*860132e2STaylor Simpson static void test_stored_rl_at(void)
138*860132e2STaylor Simpson {
139*860132e2STaylor Simpson     stored_rl_at(&buffer64[2], 11);
140*860132e2STaylor Simpson     check64(buffer64[2], 11);
141*860132e2STaylor Simpson     stored_rl_at(&buffer64[3], 12);
142*860132e2STaylor Simpson     check64(buffer64[3], 12);
143*860132e2STaylor Simpson }
144*860132e2STaylor Simpson 
storew_rl_st(int * p,int val)145*860132e2STaylor Simpson static inline void storew_rl_st(int *p, int val)
146*860132e2STaylor Simpson {
147*860132e2STaylor Simpson     asm volatile("memw_rl(%0):st = %1\n\t"
148*860132e2STaylor Simpson                  : : "r"(p), "r"(val) : "memory");
149*860132e2STaylor Simpson }
150*860132e2STaylor Simpson 
test_storew_rl_st(void)151*860132e2STaylor Simpson static void test_storew_rl_st(void)
152*860132e2STaylor Simpson {
153*860132e2STaylor Simpson     storew_rl_st(&buffer32[0], 13);
154*860132e2STaylor Simpson     check64(buffer32[0], 13);
155*860132e2STaylor Simpson     storew_rl_st(&buffer32[1], 14);
156*860132e2STaylor Simpson     check64(buffer32[1], 14);
157*860132e2STaylor Simpson }
158*860132e2STaylor Simpson 
stored_rl_st(long long * p,long long val)159*860132e2STaylor Simpson static inline void stored_rl_st(long long *p, long long val)
160*860132e2STaylor Simpson {
161*860132e2STaylor Simpson     asm volatile("memd_rl(%0):st = %1\n\t"
162*860132e2STaylor Simpson                  : : "r"(p), "r"(val) : "memory");
163*860132e2STaylor Simpson }
164*860132e2STaylor Simpson 
test_stored_rl_st(void)165*860132e2STaylor Simpson static void test_stored_rl_st(void)
166*860132e2STaylor Simpson {
167*860132e2STaylor Simpson     stored_rl_st(&buffer64[0], 15);
168*860132e2STaylor Simpson     check64(buffer64[0], 15);
169*860132e2STaylor Simpson     stored_rl_st(&buffer64[1], 15);
170*860132e2STaylor Simpson     check64(buffer64[1], 15);
171*860132e2STaylor Simpson }
172*860132e2STaylor Simpson 
main()173*860132e2STaylor Simpson int main()
174*860132e2STaylor Simpson {
175*860132e2STaylor Simpson     test_loadw_aq();
176*860132e2STaylor Simpson     test_loadd_aq();
177*860132e2STaylor Simpson     test_release_at();
178*860132e2STaylor Simpson     test_release_st();
179*860132e2STaylor Simpson     test_storew_rl_at();
180*860132e2STaylor Simpson     test_stored_rl_at();
181*860132e2STaylor Simpson     test_storew_rl_st();
182*860132e2STaylor Simpson     test_stored_rl_st();
183*860132e2STaylor Simpson 
184*860132e2STaylor Simpson     puts(err ? "FAIL" : "PASS");
185*860132e2STaylor Simpson     return err ? 1 : 0;
186*860132e2STaylor Simpson }
187