treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / testing / selftests / sparc64 / drivers / adi-test.c
blob95d93c6a88a5be18437c4e7c0d21b4ac5cbd16d9
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * selftest for sparc64's privileged ADI driver
5 * Author: Tom Hromatka <tom.hromatka@oracle.com>
6 */
7 #include <linux/kernel.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/syscall.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
19 #include "../../kselftest.h"
21 #define DEBUG_LEVEL_1_BIT (0x0001)
22 #define DEBUG_LEVEL_2_BIT (0x0002)
23 #define DEBUG_LEVEL_3_BIT (0x0004)
24 #define DEBUG_LEVEL_4_BIT (0x0008)
25 #define DEBUG_TIMING_BIT (0x1000)
27 #ifndef ARRAY_SIZE
28 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
29 #endif
31 /* bit mask of enabled bits to print */
32 #define DEBUG 0x0001
34 #define DEBUG_PRINT_L1(...) debug_print(DEBUG_LEVEL_1_BIT, __VA_ARGS__)
35 #define DEBUG_PRINT_L2(...) debug_print(DEBUG_LEVEL_2_BIT, __VA_ARGS__)
36 #define DEBUG_PRINT_L3(...) debug_print(DEBUG_LEVEL_3_BIT, __VA_ARGS__)
37 #define DEBUG_PRINT_L4(...) debug_print(DEBUG_LEVEL_4_BIT, __VA_ARGS__)
38 #define DEBUG_PRINT_T(...) debug_print(DEBUG_TIMING_BIT, __VA_ARGS__)
40 static void debug_print(int level, const char *s, ...)
42 va_list args;
44 va_start(args, s);
46 if (DEBUG & level)
47 vfprintf(stdout, s, args);
48 va_end(args);
51 #ifndef min
52 #define min(x, y) ((x) < (y) ? x : y)
53 #endif
55 #define RETURN_FROM_TEST(_ret) \
56 do { \
57 DEBUG_PRINT_L1( \
58 "\tTest %s returned %d\n", __func__, _ret); \
59 return _ret; \
60 } while (0)
62 #define ADI_BLKSZ 64
63 #define ADI_MAX_VERSION 15
65 #define TEST_STEP_FAILURE(_ret) \
66 do { \
67 fprintf(stderr, "\tTest step failure: %d at %s:%d\n", \
68 _ret, __func__, __LINE__); \
69 goto out; \
70 } while (0)
72 #define RDTICK(_x) \
73 asm volatile(" rd %%tick, %0\n" : "=r" (_x))
75 static int random_version(void)
77 long tick;
79 RDTICK(tick);
81 return tick % (ADI_MAX_VERSION + 1);
84 #define MAX_RANGES_SUPPORTED 5
85 static const char system_ram_str[] = "System RAM\n";
86 static int range_count;
87 static unsigned long long int start_addr[MAX_RANGES_SUPPORTED];
88 static unsigned long long int end_addr[MAX_RANGES_SUPPORTED];
90 struct stats {
91 char name[16];
92 unsigned long total;
93 unsigned long count;
94 unsigned long bytes;
97 static struct stats read_stats = {
98 .name = "read", .total = 0, .count = 0, .bytes = 0};
99 static struct stats pread_stats = {
100 .name = "pread", .total = 0, .count = 0, .bytes = 0};
101 static struct stats write_stats = {
102 .name = "write", .total = 0, .count = 0, .bytes = 0};
103 static struct stats pwrite_stats = {
104 .name = "pwrite", .total = 0, .count = 0, .bytes = 0};
105 static struct stats seek_stats = {
106 .name = "seek", .total = 0, .count = 0, .bytes = 0};
108 static void update_stats(struct stats * const ustats,
109 unsigned long measurement, unsigned long bytes)
111 ustats->total += measurement;
112 ustats->bytes += bytes;
113 ustats->count++;
116 static void print_ustats(const struct stats * const ustats)
118 DEBUG_PRINT_L1("%s\t%7d\t%7.0f\t%7.0f\n",
119 ustats->name, ustats->count,
120 (float)ustats->total / (float)ustats->count,
121 (float)ustats->bytes / (float)ustats->count);
124 static void print_stats(void)
126 DEBUG_PRINT_L1("\nSyscall\tCall\tAvgTime\tAvgSize\n"
127 "\tCount\t(ticks)\t(bytes)\n"
128 "-------------------------------\n");
130 print_ustats(&read_stats);
131 print_ustats(&pread_stats);
132 print_ustats(&write_stats);
133 print_ustats(&pwrite_stats);
134 print_ustats(&seek_stats);
137 static int build_memory_map(void)
139 char line[256];
140 FILE *fp;
141 int i;
143 range_count = 0;
145 fp = fopen("/proc/iomem", "r");
146 if (!fp) {
147 fprintf(stderr, "/proc/iomem: error %d: %s\n",
148 errno, strerror(errno));
149 return -errno;
152 while (fgets(line, sizeof(line), fp) != 0) {
153 if (strstr(line, system_ram_str)) {
154 char *dash, *end_ptr;
156 /* Given a line like this:
157 * d0400000-10ffaffff : System RAM
158 * replace the "-" with a space
160 dash = strstr(line, "-");
161 dash[0] = 0x20;
163 start_addr[range_count] = strtoull(line, &end_ptr, 16);
164 end_addr[range_count] = strtoull(end_ptr, NULL, 16);
165 range_count++;
169 fclose(fp);
171 DEBUG_PRINT_L1("RAM Ranges\n");
172 for (i = 0; i < range_count; i++)
173 DEBUG_PRINT_L1("\trange %d: 0x%llx\t- 0x%llx\n",
174 i, start_addr[i], end_addr[i]);
176 if (range_count == 0) {
177 fprintf(stderr, "No valid address ranges found. Error.\n");
178 return -1;
181 return 0;
184 static int read_adi(int fd, unsigned char *buf, int buf_sz)
186 int ret, bytes_read = 0;
187 long start, end, elapsed_time = 0;
189 do {
190 RDTICK(start);
191 ret = read(fd, buf + bytes_read, buf_sz - bytes_read);
192 RDTICK(end);
193 if (ret < 0)
194 return -errno;
196 elapsed_time += end - start;
197 update_stats(&read_stats, elapsed_time, buf_sz);
198 bytes_read += ret;
200 } while (bytes_read < buf_sz);
202 DEBUG_PRINT_T("\tread elapsed timed = %ld\n", elapsed_time);
203 DEBUG_PRINT_L3("\tRead %d bytes\n", bytes_read);
205 return bytes_read;
208 static int pread_adi(int fd, unsigned char *buf,
209 int buf_sz, unsigned long offset)
211 int ret, i, bytes_read = 0;
212 unsigned long cur_offset;
213 long start, end, elapsed_time = 0;
215 cur_offset = offset;
216 do {
217 RDTICK(start);
218 ret = pread(fd, buf + bytes_read, buf_sz - bytes_read,
219 cur_offset);
220 RDTICK(end);
221 if (ret < 0)
222 return -errno;
224 elapsed_time += end - start;
225 update_stats(&pread_stats, elapsed_time, buf_sz);
226 bytes_read += ret;
227 cur_offset += ret;
229 } while (bytes_read < buf_sz);
231 DEBUG_PRINT_T("\tpread elapsed timed = %ld\n", elapsed_time);
232 DEBUG_PRINT_L3("\tRead %d bytes starting at offset 0x%lx\n",
233 bytes_read, offset);
234 for (i = 0; i < bytes_read; i++)
235 DEBUG_PRINT_L4("\t\t0x%lx\t%d\n", offset + i, buf[i]);
237 return bytes_read;
240 static int write_adi(int fd, const unsigned char * const buf, int buf_sz)
242 int ret, bytes_written = 0;
243 long start, end, elapsed_time = 0;
245 do {
246 RDTICK(start);
247 ret = write(fd, buf + bytes_written, buf_sz - bytes_written);
248 RDTICK(end);
249 if (ret < 0)
250 return -errno;
252 elapsed_time += (end - start);
253 update_stats(&write_stats, elapsed_time, buf_sz);
254 bytes_written += ret;
255 } while (bytes_written < buf_sz);
257 DEBUG_PRINT_T("\twrite elapsed timed = %ld\n", elapsed_time);
258 DEBUG_PRINT_L3("\tWrote %d of %d bytes\n", bytes_written, buf_sz);
260 return bytes_written;
263 static int pwrite_adi(int fd, const unsigned char * const buf,
264 int buf_sz, unsigned long offset)
266 int ret, bytes_written = 0;
267 unsigned long cur_offset;
268 long start, end, elapsed_time = 0;
270 cur_offset = offset;
272 do {
273 RDTICK(start);
274 ret = pwrite(fd, buf + bytes_written,
275 buf_sz - bytes_written, cur_offset);
276 RDTICK(end);
277 if (ret < 0) {
278 fprintf(stderr, "pwrite(): error %d: %s\n",
279 errno, strerror(errno));
280 return -errno;
283 elapsed_time += (end - start);
284 update_stats(&pwrite_stats, elapsed_time, buf_sz);
285 bytes_written += ret;
286 cur_offset += ret;
288 } while (bytes_written < buf_sz);
290 DEBUG_PRINT_T("\tpwrite elapsed timed = %ld\n", elapsed_time);
291 DEBUG_PRINT_L3("\tWrote %d of %d bytes starting at address 0x%lx\n",
292 bytes_written, buf_sz, offset);
294 return bytes_written;
297 static off_t seek_adi(int fd, off_t offset, int whence)
299 long start, end;
300 off_t ret;
302 RDTICK(start);
303 ret = lseek(fd, offset, whence);
304 RDTICK(end);
305 DEBUG_PRINT_L2("\tlseek ret = 0x%llx\n", ret);
306 if (ret < 0)
307 goto out;
309 DEBUG_PRINT_T("\tlseek elapsed timed = %ld\n", end - start);
310 update_stats(&seek_stats, end - start, 0);
312 out:
313 (void)lseek(fd, 0, SEEK_END);
314 return ret;
317 static int test0_prpw_aligned_1byte(int fd)
319 /* somewhat arbitrarily chosen address */
320 unsigned long paddr =
321 (end_addr[range_count - 1] - 0x1000) & ~(ADI_BLKSZ - 1);
322 unsigned char version[1], expected_version;
323 loff_t offset;
324 int ret;
326 version[0] = random_version();
327 expected_version = version[0];
329 offset = paddr / ADI_BLKSZ;
331 ret = pwrite_adi(fd, version, sizeof(version), offset);
332 if (ret != sizeof(version))
333 TEST_STEP_FAILURE(ret);
335 ret = pread_adi(fd, version, sizeof(version), offset);
336 if (ret != sizeof(version))
337 TEST_STEP_FAILURE(ret);
339 if (expected_version != version[0]) {
340 DEBUG_PRINT_L2("\tExpected version %d but read version %d\n",
341 expected_version, version[0]);
342 TEST_STEP_FAILURE(-expected_version);
345 ret = 0;
346 out:
347 RETURN_FROM_TEST(ret);
350 #define TEST1_VERSION_SZ 4096
351 static int test1_prpw_aligned_4096bytes(int fd)
353 /* somewhat arbitrarily chosen address */
354 unsigned long paddr =
355 (end_addr[range_count - 1] - 0x6000) & ~(ADI_BLKSZ - 1);
356 unsigned char version[TEST1_VERSION_SZ],
357 expected_version[TEST1_VERSION_SZ];
358 loff_t offset;
359 int ret, i;
361 for (i = 0; i < TEST1_VERSION_SZ; i++) {
362 version[i] = random_version();
363 expected_version[i] = version[i];
366 offset = paddr / ADI_BLKSZ;
368 ret = pwrite_adi(fd, version, sizeof(version), offset);
369 if (ret != sizeof(version))
370 TEST_STEP_FAILURE(ret);
372 ret = pread_adi(fd, version, sizeof(version), offset);
373 if (ret != sizeof(version))
374 TEST_STEP_FAILURE(ret);
376 for (i = 0; i < TEST1_VERSION_SZ; i++) {
377 if (expected_version[i] != version[i]) {
378 DEBUG_PRINT_L2(
379 "\tExpected version %d but read version %d\n",
380 expected_version, version[0]);
381 TEST_STEP_FAILURE(-expected_version[i]);
385 ret = 0;
386 out:
387 RETURN_FROM_TEST(ret);
390 #define TEST2_VERSION_SZ 10327
391 static int test2_prpw_aligned_10327bytes(int fd)
393 /* somewhat arbitrarily chosen address */
394 unsigned long paddr =
395 (start_addr[0] + 0x6000) & ~(ADI_BLKSZ - 1);
396 unsigned char version[TEST2_VERSION_SZ],
397 expected_version[TEST2_VERSION_SZ];
398 loff_t offset;
399 int ret, i;
401 for (i = 0; i < TEST2_VERSION_SZ; i++) {
402 version[i] = random_version();
403 expected_version[i] = version[i];
406 offset = paddr / ADI_BLKSZ;
408 ret = pwrite_adi(fd, version, sizeof(version), offset);
409 if (ret != sizeof(version))
410 TEST_STEP_FAILURE(ret);
412 ret = pread_adi(fd, version, sizeof(version), offset);
413 if (ret != sizeof(version))
414 TEST_STEP_FAILURE(ret);
416 for (i = 0; i < TEST2_VERSION_SZ; i++) {
417 if (expected_version[i] != version[i]) {
418 DEBUG_PRINT_L2(
419 "\tExpected version %d but read version %d\n",
420 expected_version, version[0]);
421 TEST_STEP_FAILURE(-expected_version[i]);
425 ret = 0;
426 out:
427 RETURN_FROM_TEST(ret);
430 #define TEST3_VERSION_SZ 12541
431 static int test3_prpw_unaligned_12541bytes(int fd)
433 /* somewhat arbitrarily chosen address */
434 unsigned long paddr =
435 ((start_addr[0] + 0xC000) & ~(ADI_BLKSZ - 1)) + 17;
436 unsigned char version[TEST3_VERSION_SZ],
437 expected_version[TEST3_VERSION_SZ];
438 loff_t offset;
439 int ret, i;
441 for (i = 0; i < TEST3_VERSION_SZ; i++) {
442 version[i] = random_version();
443 expected_version[i] = version[i];
446 offset = paddr / ADI_BLKSZ;
448 ret = pwrite_adi(fd, version, sizeof(version), offset);
449 if (ret != sizeof(version))
450 TEST_STEP_FAILURE(ret);
452 ret = pread_adi(fd, version, sizeof(version), offset);
453 if (ret != sizeof(version))
454 TEST_STEP_FAILURE(ret);
456 for (i = 0; i < TEST3_VERSION_SZ; i++) {
457 if (expected_version[i] != version[i]) {
458 DEBUG_PRINT_L2(
459 "\tExpected version %d but read version %d\n",
460 expected_version, version[0]);
461 TEST_STEP_FAILURE(-expected_version[i]);
465 ret = 0;
466 out:
467 RETURN_FROM_TEST(ret);
470 static int test4_lseek(int fd)
472 #define OFFSET_ADD (0x100)
473 #define OFFSET_SUBTRACT (0xFFFFFFF000000000)
475 off_t offset_out, offset_in;
476 int ret;
479 offset_in = 0x123456789abcdef0;
480 offset_out = seek_adi(fd, offset_in, SEEK_SET);
481 if (offset_out != offset_in) {
482 ret = -1;
483 TEST_STEP_FAILURE(ret);
486 /* seek to the current offset. this should return EINVAL */
487 offset_out = seek_adi(fd, offset_in, SEEK_SET);
488 if (offset_out < 0 && errno == EINVAL)
489 DEBUG_PRINT_L2(
490 "\tSEEK_SET failed as designed. Not an error\n");
491 else {
492 ret = -2;
493 TEST_STEP_FAILURE(ret);
496 offset_out = seek_adi(fd, 0, SEEK_CUR);
497 if (offset_out != offset_in) {
498 ret = -3;
499 TEST_STEP_FAILURE(ret);
502 offset_out = seek_adi(fd, OFFSET_ADD, SEEK_CUR);
503 if (offset_out != (offset_in + OFFSET_ADD)) {
504 ret = -4;
505 TEST_STEP_FAILURE(ret);
508 offset_out = seek_adi(fd, OFFSET_SUBTRACT, SEEK_CUR);
509 if (offset_out != (offset_in + OFFSET_ADD + OFFSET_SUBTRACT)) {
510 ret = -5;
511 TEST_STEP_FAILURE(ret);
514 ret = 0;
515 out:
516 RETURN_FROM_TEST(ret);
519 static int test5_rw_aligned_1byte(int fd)
521 /* somewhat arbitrarily chosen address */
522 unsigned long paddr =
523 (end_addr[range_count - 1] - 0xF000) & ~(ADI_BLKSZ - 1);
524 unsigned char version, expected_version;
525 loff_t offset;
526 off_t oret;
527 int ret;
529 offset = paddr / ADI_BLKSZ;
530 version = expected_version = random_version();
532 oret = seek_adi(fd, offset, SEEK_SET);
533 if (oret != offset) {
534 ret = -1;
535 TEST_STEP_FAILURE(ret);
538 ret = write_adi(fd, &version, sizeof(version));
539 if (ret != sizeof(version))
540 TEST_STEP_FAILURE(ret);
542 oret = seek_adi(fd, offset, SEEK_SET);
543 if (oret != offset) {
544 ret = -1;
545 TEST_STEP_FAILURE(ret);
548 ret = read_adi(fd, &version, sizeof(version));
549 if (ret != sizeof(version))
550 TEST_STEP_FAILURE(ret);
552 if (expected_version != version) {
553 DEBUG_PRINT_L2("\tExpected version %d but read version %d\n",
554 expected_version, version);
555 TEST_STEP_FAILURE(-expected_version);
558 ret = 0;
559 out:
560 RETURN_FROM_TEST(ret);
563 #define TEST6_VERSION_SZ 9434
564 static int test6_rw_aligned_9434bytes(int fd)
566 /* somewhat arbitrarily chosen address */
567 unsigned long paddr =
568 (end_addr[range_count - 1] - 0x5F000) & ~(ADI_BLKSZ - 1);
569 unsigned char version[TEST6_VERSION_SZ],
570 expected_version[TEST6_VERSION_SZ];
571 loff_t offset;
572 off_t oret;
573 int ret, i;
575 offset = paddr / ADI_BLKSZ;
576 for (i = 0; i < TEST6_VERSION_SZ; i++)
577 version[i] = expected_version[i] = random_version();
579 oret = seek_adi(fd, offset, SEEK_SET);
580 if (oret != offset) {
581 ret = -1;
582 TEST_STEP_FAILURE(ret);
585 ret = write_adi(fd, version, sizeof(version));
586 if (ret != sizeof(version))
587 TEST_STEP_FAILURE(ret);
589 memset(version, 0, TEST6_VERSION_SZ);
591 oret = seek_adi(fd, offset, SEEK_SET);
592 if (oret != offset) {
593 ret = -1;
594 TEST_STEP_FAILURE(ret);
597 ret = read_adi(fd, version, sizeof(version));
598 if (ret != sizeof(version))
599 TEST_STEP_FAILURE(ret);
601 for (i = 0; i < TEST6_VERSION_SZ; i++) {
602 if (expected_version[i] != version[i]) {
603 DEBUG_PRINT_L2(
604 "\tExpected version %d but read version %d\n",
605 expected_version[i], version[i]);
606 TEST_STEP_FAILURE(-expected_version[i]);
610 ret = 0;
611 out:
612 RETURN_FROM_TEST(ret);
615 #define TEST7_VERSION_SZ 14963
616 static int test7_rw_aligned_14963bytes(int fd)
618 /* somewhat arbitrarily chosen address */
619 unsigned long paddr =
620 ((start_addr[range_count - 1] + 0xF000) & ~(ADI_BLKSZ - 1)) + 39;
621 unsigned char version[TEST7_VERSION_SZ],
622 expected_version[TEST7_VERSION_SZ];
623 loff_t offset;
624 off_t oret;
625 int ret, i;
627 offset = paddr / ADI_BLKSZ;
628 for (i = 0; i < TEST7_VERSION_SZ; i++) {
629 version[i] = random_version();
630 expected_version[i] = version[i];
633 oret = seek_adi(fd, offset, SEEK_SET);
634 if (oret != offset) {
635 ret = -1;
636 TEST_STEP_FAILURE(ret);
639 ret = write_adi(fd, version, sizeof(version));
640 if (ret != sizeof(version))
641 TEST_STEP_FAILURE(ret);
643 memset(version, 0, TEST7_VERSION_SZ);
645 oret = seek_adi(fd, offset, SEEK_SET);
646 if (oret != offset) {
647 ret = -1;
648 TEST_STEP_FAILURE(ret);
651 ret = read_adi(fd, version, sizeof(version));
652 if (ret != sizeof(version))
653 TEST_STEP_FAILURE(ret);
655 for (i = 0; i < TEST7_VERSION_SZ; i++) {
656 if (expected_version[i] != version[i]) {
657 DEBUG_PRINT_L2(
658 "\tExpected version %d but read version %d\n",
659 expected_version[i], version[i]);
660 TEST_STEP_FAILURE(-expected_version[i]);
663 paddr += ADI_BLKSZ;
666 ret = 0;
667 out:
668 RETURN_FROM_TEST(ret);
671 static int (*tests[])(int fd) = {
672 test0_prpw_aligned_1byte,
673 test1_prpw_aligned_4096bytes,
674 test2_prpw_aligned_10327bytes,
675 test3_prpw_unaligned_12541bytes,
676 test4_lseek,
677 test5_rw_aligned_1byte,
678 test6_rw_aligned_9434bytes,
679 test7_rw_aligned_14963bytes,
681 #define TEST_COUNT ARRAY_SIZE(tests)
683 int main(int argc, char *argv[])
685 int fd, ret, test;
687 ret = build_memory_map();
688 if (ret < 0)
689 return ret;
691 fd = open("/dev/adi", O_RDWR);
692 if (fd < 0) {
693 fprintf(stderr, "open: error %d: %s\n",
694 errno, strerror(errno));
695 return -errno;
698 for (test = 0; test < TEST_COUNT; test++) {
699 DEBUG_PRINT_L1("Running test #%d\n", test);
701 ret = (*tests[test])(fd);
702 if (ret != 0)
703 ksft_test_result_fail("Test #%d failed: error %d\n",
704 test, ret);
705 else
706 ksft_test_result_pass("Test #%d passed\n", test);
709 print_stats();
710 close(fd);
712 if (ksft_get_fail_cnt() > 0)
713 ksft_exit_fail();
714 else
715 ksft_exit_pass();
717 /* it's impossible to get here, but the compiler throws a warning
718 * about control reaching the end of non-void function. bah.
720 return 0;