2 * Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
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.
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.
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/>.
21 #define DEBUG_PRINTF(...) \
24 printf(__VA_ARGS__); \
29 #define NBYTES (1 << 8)
30 #define NHALFS (NBYTES / sizeof(short))
31 #define NWORDS (NBYTES / sizeof(int))
32 #define NDOBLS (NBYTES / sizeof(long long))
34 long long dbuf
[NDOBLS
] __attribute__((aligned(1 << 12))) = {0};
35 int wbuf
[NWORDS
] __attribute__((aligned(1 << 12))) = {0};
36 short hbuf
[NHALFS
] __attribute__((aligned(1 << 12))) = {0};
37 unsigned char bbuf
[NBYTES
] __attribute__((aligned(1 << 12))) = {0};
40 * We use the C preporcessor to deal with the combinations of types
43 #define INIT(BUF, N) \
44 void init_##BUF(void) \
47 for (i = 0; i < N; i++) { \
58 * Macros for performing circular load
61 * START start address of buffer
62 * LEN length of buffer (in bytes)
63 * INC address increment (in bytes for IMM, elements for REG)
65 #define CIRC_LOAD_IMM(SIZE, RES, ADDR, START, LEN, INC) \
70 "%0 = mem" #SIZE "(%1++#" #INC ":circ(M0))\n\t" \
71 : "=r"(RES), "+r"(ADDR) \
72 : "r"(START), "r"(LEN) \
74 #define CIRC_LOAD_IMM_b(RES, ADDR, START, LEN, INC) \
75 CIRC_LOAD_IMM(b, RES, ADDR, START, LEN, INC)
76 #define CIRC_LOAD_IMM_ub(RES, ADDR, START, LEN, INC) \
77 CIRC_LOAD_IMM(ub, RES, ADDR, START, LEN, INC)
78 #define CIRC_LOAD_IMM_h(RES, ADDR, START, LEN, INC) \
79 CIRC_LOAD_IMM(h, RES, ADDR, START, LEN, INC)
80 #define CIRC_LOAD_IMM_uh(RES, ADDR, START, LEN, INC) \
81 CIRC_LOAD_IMM(uh, RES, ADDR, START, LEN, INC)
82 #define CIRC_LOAD_IMM_w(RES, ADDR, START, LEN, INC) \
83 CIRC_LOAD_IMM(w, RES, ADDR, START, LEN, INC)
84 #define CIRC_LOAD_IMM_d(RES, ADDR, START, LEN, INC) \
85 CIRC_LOAD_IMM(d, RES, ADDR, START, LEN, INC)
88 * The mreg has the following pieces
89 * mreg[31:28] increment[10:7]
90 * mreg[27:24] K value (used Hexagon v3 and earlier)
91 * mreg[23:17] increment[6:0]
92 * mreg[16:0] circular buffer length
94 static int build_mreg(int inc
, int K
, int len
)
96 return ((inc
& 0x780) << 21) |
98 ((inc
& 0x7f) << 17) |
102 #define CIRC_LOAD_REG(SIZE, RES, ADDR, START, LEN, INC) \
107 "%0 = mem" #SIZE "(%1++I:circ(M1))\n\t" \
108 : "=r"(RES), "+r"(ADDR) \
109 : "r"(build_mreg((INC), 0, (LEN))), \
112 #define CIRC_LOAD_REG_b(RES, ADDR, START, LEN, INC) \
113 CIRC_LOAD_REG(b, RES, ADDR, START, LEN, INC)
114 #define CIRC_LOAD_REG_ub(RES, ADDR, START, LEN, INC) \
115 CIRC_LOAD_REG(ub, RES, ADDR, START, LEN, INC)
116 #define CIRC_LOAD_REG_h(RES, ADDR, START, LEN, INC) \
117 CIRC_LOAD_REG(h, RES, ADDR, START, LEN, INC)
118 #define CIRC_LOAD_REG_uh(RES, ADDR, START, LEN, INC) \
119 CIRC_LOAD_REG(uh, RES, ADDR, START, LEN, INC)
120 #define CIRC_LOAD_REG_w(RES, ADDR, START, LEN, INC) \
121 CIRC_LOAD_REG(w, RES, ADDR, START, LEN, INC)
122 #define CIRC_LOAD_REG_d(RES, ADDR, START, LEN, INC) \
123 CIRC_LOAD_REG(d, RES, ADDR, START, LEN, INC)
126 * Macros for performing circular store
129 * START start address of buffer
130 * LEN length of buffer (in bytes)
131 * INC address increment (in bytes for IMM, elements for REG)
133 #define CIRC_STORE_IMM(SIZE, PART, VAL, ADDR, START, LEN, INC) \
138 "mem" #SIZE "(%0++#" #INC ":circ(M0)) = %2" PART "\n\t" \
140 : "r"(START), "r"(VAL), "r"(LEN) \
141 : "r4", "m0", "cs0", "memory")
142 #define CIRC_STORE_IMM_b(VAL, ADDR, START, LEN, INC) \
143 CIRC_STORE_IMM(b, "", VAL, ADDR, START, LEN, INC)
144 #define CIRC_STORE_IMM_h(VAL, ADDR, START, LEN, INC) \
145 CIRC_STORE_IMM(h, "", VAL, ADDR, START, LEN, INC)
146 #define CIRC_STORE_IMM_f(VAL, ADDR, START, LEN, INC) \
147 CIRC_STORE_IMM(h, ".H", VAL, ADDR, START, LEN, INC)
148 #define CIRC_STORE_IMM_w(VAL, ADDR, START, LEN, INC) \
149 CIRC_STORE_IMM(w, "", VAL, ADDR, START, LEN, INC)
150 #define CIRC_STORE_IMM_d(VAL, ADDR, START, LEN, INC) \
151 CIRC_STORE_IMM(d, "", VAL, ADDR, START, LEN, INC)
153 #define CIRC_STORE_NEW_IMM(SIZE, VAL, ADDR, START, LEN, INC) \
160 " mem" #SIZE "(%0++#" #INC ":circ(M0)) = r5.new\n\t" \
163 : "r"(START), "r"(VAL), "r"(LEN) \
164 : "r4", "r5", "m0", "cs0", "memory")
165 #define CIRC_STORE_IMM_bnew(VAL, ADDR, START, LEN, INC) \
166 CIRC_STORE_NEW_IMM(b, VAL, ADDR, START, LEN, INC)
167 #define CIRC_STORE_IMM_hnew(VAL, ADDR, START, LEN, INC) \
168 CIRC_STORE_NEW_IMM(h, VAL, ADDR, START, LEN, INC)
169 #define CIRC_STORE_IMM_wnew(VAL, ADDR, START, LEN, INC) \
170 CIRC_STORE_NEW_IMM(w, VAL, ADDR, START, LEN, INC)
172 #define CIRC_STORE_REG(SIZE, PART, VAL, ADDR, START, LEN, INC) \
177 "mem" #SIZE "(%0++I:circ(M1)) = %3" PART "\n\t" \
179 : "r"(build_mreg((INC), 0, (LEN))), \
182 : "r4", "m1", "cs1", "memory")
183 #define CIRC_STORE_REG_b(VAL, ADDR, START, LEN, INC) \
184 CIRC_STORE_REG(b, "", VAL, ADDR, START, LEN, INC)
185 #define CIRC_STORE_REG_h(VAL, ADDR, START, LEN, INC) \
186 CIRC_STORE_REG(h, "", VAL, ADDR, START, LEN, INC)
187 #define CIRC_STORE_REG_f(VAL, ADDR, START, LEN, INC) \
188 CIRC_STORE_REG(h, ".H", VAL, ADDR, START, LEN, INC)
189 #define CIRC_STORE_REG_w(VAL, ADDR, START, LEN, INC) \
190 CIRC_STORE_REG(w, "", VAL, ADDR, START, LEN, INC)
191 #define CIRC_STORE_REG_d(VAL, ADDR, START, LEN, INC) \
192 CIRC_STORE_REG(d, "", VAL, ADDR, START, LEN, INC)
194 #define CIRC_STORE_NEW_REG(SIZE, VAL, ADDR, START, LEN, INC) \
201 " mem" #SIZE "(%0++I:circ(M1)) = r5.new\n\t" \
204 : "r"(build_mreg((INC), 0, (LEN))), \
207 : "r4", "r5", "m1", "cs1", "memory")
208 #define CIRC_STORE_REG_bnew(VAL, ADDR, START, LEN, INC) \
209 CIRC_STORE_NEW_REG(b, VAL, ADDR, START, LEN, INC)
210 #define CIRC_STORE_REG_hnew(VAL, ADDR, START, LEN, INC) \
211 CIRC_STORE_NEW_REG(h, VAL, ADDR, START, LEN, INC)
212 #define CIRC_STORE_REG_wnew(VAL, ADDR, START, LEN, INC) \
213 CIRC_STORE_NEW_REG(w, VAL, ADDR, START, LEN, INC)
218 /* We'll test increments +1 and -1 */
219 void check_load(int i
, long long result
, int inc
, int size
)
221 int expect
= (i
* inc
);
222 while (expect
>= size
) {
228 if (result
!= expect
) {
229 printf("ERROR(%d): %lld != %d\n", i
, result
, expect
);
234 #define TEST_LOAD_IMM(SZ, TYPE, BUF, BUFSIZE, INC, FMT) \
235 void circ_test_load_imm_##SZ(void) \
237 TYPE *p = (TYPE *)BUF; \
240 for (i = 0; i < BUFSIZE; i++) { \
242 CIRC_LOAD_IMM_##SZ(element, p, BUF, size * sizeof(TYPE), (INC)); \
243 DEBUG_PRINTF("i = %2d, p = 0x%p, element = %2" #FMT "\n", \
245 check_load(i, element, ((INC) / (int)sizeof(TYPE)), size); \
248 for (i = 0; i < BUFSIZE; i++) { \
250 CIRC_LOAD_IMM_##SZ(element, p, BUF, size * sizeof(TYPE), -(INC)); \
251 DEBUG_PRINTF("i = %2d, p = 0x%p, element = %2" #FMT "\n", \
253 check_load(i, element, (-(INC) / (int)sizeof(TYPE)), size); \
257 TEST_LOAD_IMM(b
, char, bbuf
, NBYTES
, 1, d
)
258 TEST_LOAD_IMM(ub
, unsigned char, bbuf
, NBYTES
, 1, d
)
259 TEST_LOAD_IMM(h
, short, hbuf
, NHALFS
, 2, d
)
260 TEST_LOAD_IMM(uh
, unsigned short, hbuf
, NHALFS
, 2, d
)
261 TEST_LOAD_IMM(w
, int, wbuf
, NWORDS
, 4, d
)
262 TEST_LOAD_IMM(d
, long long, dbuf
, NDOBLS
, 8, lld
)
264 #define TEST_LOAD_REG(SZ, TYPE, BUF, BUFSIZE, FMT) \
265 void circ_test_load_reg_##SZ(void) \
267 TYPE *p = (TYPE *)BUF; \
270 for (i = 0; i < BUFSIZE; i++) { \
272 CIRC_LOAD_REG_##SZ(element, p, BUF, size * sizeof(TYPE), 1); \
273 DEBUG_PRINTF("i = %2d, p = 0x%p, element = %2" #FMT "\n", \
275 check_load(i, element, 1, size); \
278 for (i = 0; i < BUFSIZE; i++) { \
280 CIRC_LOAD_REG_##SZ(element, p, BUF, size * sizeof(TYPE), -1); \
281 DEBUG_PRINTF("i = %2d, p = 0x%p, element = %2" #FMT "\n", \
283 check_load(i, element, -1, size); \
287 TEST_LOAD_REG(b
, char, bbuf
, NBYTES
, d
)
288 TEST_LOAD_REG(ub
, unsigned char, bbuf
, NBYTES
, d
)
289 TEST_LOAD_REG(h
, short, hbuf
, NHALFS
, d
)
290 TEST_LOAD_REG(uh
, unsigned short, hbuf
, NHALFS
, d
)
291 TEST_LOAD_REG(w
, int, wbuf
, NWORDS
, d
)
292 TEST_LOAD_REG(d
, long long, dbuf
, NDOBLS
, lld
)
294 /* The circular stores will wrap around somewhere inside the buffer */
295 #define CIRC_VAL(SZ, TYPE, BUFSIZE) \
296 TYPE circ_val_##SZ(int i, int inc, int size) \
298 int mod = BUFSIZE % size; \
299 int elem = i * inc; \
301 if (-elem <= size - mod) { \
302 return (elem + BUFSIZE - mod); \
304 return (elem + BUFSIZE + size - mod); \
306 } else if (elem < mod) {\
307 return (elem + BUFSIZE - mod); \
309 return (elem + BUFSIZE - size - mod); \
313 CIRC_VAL(b
, unsigned char, NBYTES
)
314 CIRC_VAL(h
, short, NHALFS
)
315 CIRC_VAL(w
, int, NWORDS
)
316 CIRC_VAL(d
, long long, NDOBLS
)
319 * Circular stores should only write to the first "size" elements of the buffer
320 * the remainder of the elements should have BUF[i] == i
322 #define CHECK_STORE(SZ, BUF, BUFSIZE, FMT) \
323 void check_store_##SZ(int inc, int size) \
326 for (i = 0; i < size; i++) { \
327 DEBUG_PRINTF(#BUF "[%3d] = 0x%02" #FMT ", guess = 0x%02" #FMT "\n", \
328 i, BUF[i], circ_val_##SZ(i, inc, size)); \
329 if (BUF[i] != circ_val_##SZ(i, inc, size)) { \
330 printf("ERROR(%3d): 0x%02" #FMT " != 0x%02" #FMT "\n", \
331 i, BUF[i], circ_val_##SZ(i, inc, size)); \
335 for (i = size; i < BUFSIZE; i++) { \
337 printf("ERROR(%3d): 0x%02" #FMT " != 0x%02x\n", i, BUF[i], i); \
343 CHECK_STORE(b
, bbuf
, NBYTES
, x
)
344 CHECK_STORE(h
, hbuf
, NHALFS
, x
)
345 CHECK_STORE(w
, wbuf
, NWORDS
, x
)
346 CHECK_STORE(d
, dbuf
, NDOBLS
, llx
)
348 #define CIRC_TEST_STORE_IMM(SZ, CHK, TYPE, BUF, BUFSIZE, SHIFT, INC) \
349 void circ_test_store_imm_##SZ(void) \
351 unsigned int size = 27; \
356 for (i = 0; i < BUFSIZE; i++) { \
357 CIRC_STORE_IMM_##SZ(val << SHIFT, p, BUF, size * sizeof(TYPE), INC); \
360 check_store_##CHK(((INC) / (int)sizeof(TYPE)), size); \
364 for (i = 0; i < BUFSIZE; i++) { \
365 CIRC_STORE_IMM_##SZ(val << SHIFT, p, BUF, size * sizeof(TYPE), \
369 check_store_##CHK((-(INC) / (int)sizeof(TYPE)), size); \
372 CIRC_TEST_STORE_IMM(b
, b
, unsigned char, bbuf
, NBYTES
, 0, 1)
373 CIRC_TEST_STORE_IMM(h
, h
, short, hbuf
, NHALFS
, 0, 2)
374 CIRC_TEST_STORE_IMM(f
, h
, short, hbuf
, NHALFS
, 16, 2)
375 CIRC_TEST_STORE_IMM(w
, w
, int, wbuf
, NWORDS
, 0, 4)
376 CIRC_TEST_STORE_IMM(d
, d
, long long, dbuf
, NDOBLS
, 0, 8)
377 CIRC_TEST_STORE_IMM(bnew
, b
, unsigned char, bbuf
, NBYTES
, 0, 1)
378 CIRC_TEST_STORE_IMM(hnew
, h
, short, hbuf
, NHALFS
, 0, 2)
379 CIRC_TEST_STORE_IMM(wnew
, w
, int, wbuf
, NWORDS
, 0, 4)
381 #define CIRC_TEST_STORE_REG(SZ, CHK, TYPE, BUF, BUFSIZE, SHIFT) \
382 void circ_test_store_reg_##SZ(void) \
385 unsigned int size = 19; \
389 for (i = 0; i < BUFSIZE; i++) { \
390 CIRC_STORE_REG_##SZ(val << SHIFT, p, BUF, size * sizeof(TYPE), 1); \
393 check_store_##CHK(1, size); \
397 for (i = 0; i < BUFSIZE; i++) { \
398 CIRC_STORE_REG_##SZ(val << SHIFT, p, BUF, size * sizeof(TYPE), -1); \
401 check_store_##CHK(-1, size); \
404 CIRC_TEST_STORE_REG(b
, b
, unsigned char, bbuf
, NBYTES
, 0)
405 CIRC_TEST_STORE_REG(h
, h
, short, hbuf
, NHALFS
, 0)
406 CIRC_TEST_STORE_REG(f
, h
, short, hbuf
, NHALFS
, 16)
407 CIRC_TEST_STORE_REG(w
, w
, int, wbuf
, NWORDS
, 0)
408 CIRC_TEST_STORE_REG(d
, d
, long long, dbuf
, NDOBLS
, 0)
409 CIRC_TEST_STORE_REG(bnew
, b
, unsigned char, bbuf
, NBYTES
, 0)
410 CIRC_TEST_STORE_REG(hnew
, h
, short, hbuf
, NHALFS
, 0)
411 CIRC_TEST_STORE_REG(wnew
, w
, int, wbuf
, NWORDS
, 0)
413 /* Test the old scheme used in Hexagon V3 */
414 static void circ_test_v3(void)
418 int K
= 4; /* 64 bytes */
424 for (i
= 0; i
< NWORDS
; i
++) {
428 "%0 = memw(%1++I:circ(M1))\n\t"
429 : "=r"(element
), "+r"(p
)
430 : "r"(build_mreg(1, K
, size
* sizeof(int)))
432 DEBUG_PRINTF("i = %2d, p = 0x%p, element = %2d\n", i
, p
, element
);
433 check_load(i
, element
, 1, size
);
444 DEBUG_PRINTF("NBYTES = %d\n", NBYTES
);
445 DEBUG_PRINTF("Address of dbuf = 0x%p\n", dbuf
);
446 DEBUG_PRINTF("Address of wbuf = 0x%p\n", wbuf
);
447 DEBUG_PRINTF("Address of hbuf = 0x%p\n", hbuf
);
448 DEBUG_PRINTF("Address of bbuf = 0x%p\n", bbuf
);
450 circ_test_load_imm_b();
451 circ_test_load_imm_ub();
452 circ_test_load_imm_h();
453 circ_test_load_imm_uh();
454 circ_test_load_imm_w();
455 circ_test_load_imm_d();
457 circ_test_load_reg_b();
458 circ_test_load_reg_ub();
459 circ_test_load_reg_h();
460 circ_test_load_reg_uh();
461 circ_test_load_reg_w();
462 circ_test_load_reg_d();
464 circ_test_store_imm_b();
465 circ_test_store_imm_h();
466 circ_test_store_imm_f();
467 circ_test_store_imm_w();
468 circ_test_store_imm_d();
469 circ_test_store_imm_bnew();
470 circ_test_store_imm_hnew();
471 circ_test_store_imm_wnew();
473 circ_test_store_reg_b();
474 circ_test_store_reg_h();
475 circ_test_store_reg_f();
476 circ_test_store_reg_w();
477 circ_test_store_reg_d();
478 circ_test_store_reg_bnew();
479 circ_test_store_reg_hnew();
480 circ_test_store_reg_wnew();
484 puts(err
? "FAIL" : "PASS");