Tom, you used the wrong GPL license header. Please fix that to be v2 not v3.
The
+# SPDX-License-Identifier: GPL-2.0
is correct but the adi-test.c one is not.
On 04/20/2018 10:34 AM, Tom Hromatka wrote:
Add a selftest for the sparc64 privileged ADI driver. These
tests verify the read(), pread(), write(), pwrite(), and seek()
functionality of the driver. The tests also report simple
performance statistics:
ÂÂÂÂÂÂÂÂ Syscall CallÂÂÂ AvgTime AvgSize
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ CountÂÂ (ticks) (bytes)
ÂÂÂÂÂÂÂÂ -------------------------------
ÂÂÂÂÂÂÂÂ readÂÂÂÂÂÂÂÂÂ 3Â 119638ÂÂÂ 8133
ÂÂÂÂÂÂÂÂ preadÂÂÂÂÂÂÂÂ 4Â 118164ÂÂÂ 6741
ÂÂÂÂÂÂÂÂ writeÂÂÂÂÂÂÂÂ 3Â 339442ÂÂÂ 8133
ÂÂÂÂÂÂÂÂ pwriteÂÂÂÂÂÂÂ 4Â 280134ÂÂÂ 6741
ÂÂÂÂÂÂÂÂ seekÂÂÂÂÂÂÂÂ 10ÂÂÂ 2919ÂÂÂÂÂÂ 0
ÂÂÂÂÂÂÂÂ Pass 8 Fail 0 Xfail 0 Xpass 0 Skip 0 Error 0
Signed-off-by: Tom Hromatka <tom.hromatka@xxxxxxxxxx>
Reviewed-by: Allen Pais <allen.pais@xxxxxxxxxx>
---
 tools/testing/selftests/Makefile | 1 +
 tools/testing/selftests/sparc64/Makefile | 46 ++
 tools/testing/selftests/sparc64/drivers/.gitignore | 1 +
 tools/testing/selftests/sparc64/drivers/Makefile | 15 +
 tools/testing/selftests/sparc64/drivers/adi-test.c | 732 +++++++++++++++++++++
 .../selftests/sparc64/drivers/drivers_test.sh | 30 +
 tools/testing/selftests/sparc64/run.sh | 3 +
 7 files changed, 828 insertions(+)
 create mode 100644 tools/testing/selftests/sparc64/Makefile
 create mode 100644 tools/testing/selftests/sparc64/drivers/.gitignore
 create mode 100644 tools/testing/selftests/sparc64/drivers/Makefile
 create mode 100644 tools/testing/selftests/sparc64/drivers/adi-test.c
 create mode 100755 tools/testing/selftests/sparc64/drivers/drivers_test.sh
 create mode 100755 tools/testing/selftests/sparc64/run.sh
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 7442dfb73b7f..94da632579e7 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -28,6 +28,7 @@ TARGETS += ptrace
 TARGETS += seccomp
 TARGETS += sigaltstack
 TARGETS += size
+TARGETS += sparc64
 TARGETS += splice
 TARGETS += static_keys
 TARGETS += sync
diff --git a/tools/testing/selftests/sparc64/Makefile b/tools/testing/selftests/sparc64/Makefile
new file mode 100644
index 000000000000..2082eeffd779
--- /dev/null
+++ b/tools/testing/selftests/sparc64/Makefile
@@ -0,0 +1,46 @@
+SUBDIRS := drivers
+
+TEST_PROGS := run.sh
+
+.PHONY: all clean
+
+include ../lib.mk
+
+all:
+ÂÂÂ @for DIR in $(SUBDIRS); doÂÂÂÂÂÂÂ \
+ÂÂÂÂÂÂÂ BUILD_TARGET=$(OUTPUT)/$$DIR;ÂÂÂ \
+ÂÂÂÂÂÂÂ mkdir $$BUILD_TARGETÂ -p;ÂÂÂ \
+ÂÂÂÂÂÂÂ make OUTPUT=$$BUILD_TARGET -C $$DIR $@;\
+ÂÂÂÂÂÂÂ #SUBDIR test prog name should be in the form: SUBDIR_test.sh \
+ÂÂÂÂÂÂÂ TEST=$$DIR"_test.sh"; \
+ÂÂÂÂÂÂÂ if [ -e $$DIR/$$TEST ]; then \
+ÂÂÂÂÂÂÂÂÂÂÂ rsync -a $$DIR/$$TEST $$BUILD_TARGET/; \
+ÂÂÂÂÂÂÂ fi \
+ÂÂÂ done
+
+override define RUN_TESTS
+ÂÂÂ @cd $(OUTPUT); ./run.sh
+endef
+
+override define INSTALL_RULE
+ÂÂÂ mkdir -p $(INSTALL_PATH)
+ÂÂÂ install -t $(INSTALL_PATH) $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES)
+
+ÂÂÂ @for SUBDIR in $(SUBDIRS); do \
+ÂÂÂÂÂÂÂ BUILD_TARGET=$(OUTPUT)/$$SUBDIR;ÂÂÂ \
+ÂÂÂÂÂÂÂ mkdir $$BUILD_TARGETÂ -p;ÂÂÂ \
+ÂÂÂÂÂÂÂ $(MAKE) OUTPUT=$$BUILD_TARGET -C $$SUBDIR INSTALL_PATH=$(INSTALL_PATH)/$$SUBDIR install; \
+ÂÂÂ done;
+endef
+
+override define EMIT_TESTS
+ÂÂÂ echo "./run.sh"
+endef
+
+override define CLEAN
+ÂÂÂ @for DIR in $(SUBDIRS); doÂÂÂÂÂÂÂ \
+ÂÂÂÂÂÂÂ BUILD_TARGET=$(OUTPUT)/$$DIR;ÂÂÂ \
+ÂÂÂÂÂÂÂ mkdir $$BUILD_TARGETÂ -p;ÂÂÂ \
+ÂÂÂÂÂÂÂ make OUTPUT=$$BUILD_TARGET -C $$DIR $@;\
+ÂÂÂ done
+endef
diff --git a/tools/testing/selftests/sparc64/drivers/.gitignore b/tools/testing/selftests/sparc64/drivers/.gitignore
new file mode 100644
index 000000000000..90e835ed74e6
--- /dev/null
+++ b/tools/testing/selftests/sparc64/drivers/.gitignore
@@ -0,0 +1 @@
+adi-test
diff --git a/tools/testing/selftests/sparc64/drivers/Makefile b/tools/testing/selftests/sparc64/drivers/Makefile
new file mode 100644
index 000000000000..6264f40bbdbc
--- /dev/null
+++ b/tools/testing/selftests/sparc64/drivers/Makefile
@@ -0,0 +1,15 @@
+
+INCLUDEDIR := -I.
+CFLAGS := $(CFLAGS) $(INCLUDEDIR) -Wall -O2 -g
+
+TEST_GEN_FILES := adi-test
+
+all: $(TEST_GEN_FILES)
+
+$(TEST_GEN_FILES): adi-test.c
+
+TEST_PROGS := drivers_test.sh
+
+include ../../lib.mk
+
+$(OUTPUT)/adi-test: adi-test.c
diff --git a/tools/testing/selftests/sparc64/drivers/adi-test.c b/tools/testing/selftests/sparc64/drivers/adi-test.c
new file mode 100644
index 000000000000..22ffd06fba24
--- /dev/null
+++ b/tools/testing/selftests/sparc64/drivers/adi-test.c
@@ -0,0 +1,732 @@
+/*
+ * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Author: Tom Hromatka <tom.hromatka@xxxxxxxxxx>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <linux/kernel.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "../../kselftest.h"
+
+#define DEBUG_LEVEL_1_BITÂÂÂ (0x0001)
+#define DEBUG_LEVEL_2_BITÂÂÂ (0x0002)
+#define DEBUG_LEVEL_3_BITÂÂÂ (0x0004)
+#define DEBUG_LEVEL_4_BITÂÂÂ (0x0008)
+#define DEBUG_TIMING_BITÂÂÂ (0x1000)
+
+#ifndef ARRAY_SIZE
+# define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+#endif
+
+/* bit mask of enabled bits to print */
+#define DEBUG 0x0001
+
+#define DEBUG_PRINT_L1(...)ÂÂÂ debug_print(DEBUG_LEVEL_1_BIT, __VA_ARGS__)
+#define DEBUG_PRINT_L2(...)ÂÂÂ debug_print(DEBUG_LEVEL_2_BIT, __VA_ARGS__)
+#define DEBUG_PRINT_L3(...)ÂÂÂ debug_print(DEBUG_LEVEL_3_BIT, __VA_ARGS__)
+#define DEBUG_PRINT_L4(...)ÂÂÂ debug_print(DEBUG_LEVEL_4_BIT, __VA_ARGS__)
+#define DEBUG_PRINT_T(...)ÂÂÂ debug_print(DEBUG_TIMING_BIT, __VA_ARGS__)
+
+static void debug_print(int level, const char *s, ...)
+{
+ÂÂÂ va_list args;
+
+ÂÂÂ va_start(args, s);
+
+ÂÂÂ if (DEBUG & level)
+ÂÂÂÂÂÂÂ vfprintf(stdout, s, args);
+ÂÂÂ va_end(args);
+}
+
+#ifndef min
+#define min(x, y) ((x) < (y) ? x : y)
+#endif
+
+#define RETURN_FROM_TEST(_ret) \
+ÂÂÂ do { \
+ÂÂÂÂÂÂÂ DEBUG_PRINT_L1( \
+ÂÂÂÂÂÂÂÂÂÂÂ "\tTest %s returned %d\n", __func__, _ret); \
+ÂÂÂÂÂÂÂ return _ret; \
+ÂÂÂ } while (0)
+
+#define ADI_BLKSZÂÂÂ 64
+#define ADI_MAX_VERSIONÂÂÂ 15
+
+#define TEST_STEP_FAILURE(_ret) \
+ÂÂÂ do { \
+ÂÂÂÂÂÂÂ fprintf(stderr, "\tTest step failure: %d at %s:%d\n", \
+ÂÂÂÂÂÂÂÂÂÂÂ _ret, __func__, __LINE__); \
+ÂÂÂÂÂÂÂ goto out; \
+ÂÂÂ } while (0)
+
+#define RDTICK(_x) \
+ÂÂÂ asm volatile(" rd %%tick, %0\n" : "=r" (_x))
+
+static int random_version(void)
+{
+ÂÂÂ long tick;
+
+ÂÂÂ RDTICK(tick);
+
+ÂÂÂ return tick % (ADI_MAX_VERSION + 1);
+}
+
+#define MAX_RANGES_SUPPORTEDÂÂÂ 5
+static const char system_ram_str[] = "System RAM\n";
+static int range_count;
+static unsigned long long int start_addr[MAX_RANGES_SUPPORTED];
+static unsigned long long intÂÂ end_addr[MAX_RANGES_SUPPORTED];
+
+struct stats {
+ÂÂÂ charÂÂÂÂÂÂÂ name[16];
+ÂÂÂ unsigned longÂÂÂ total;
+ÂÂÂ unsigned longÂÂÂ count;
+ÂÂÂ unsigned longÂÂÂ bytes;
+};
+
+static struct stats read_stats = {
+ÂÂÂ .name = "read", .total = 0, .count = 0, .bytes = 0};
+static struct stats pread_stats = {
+ÂÂÂ .name = "pread", .total = 0, .count = 0, .bytes = 0};
+static struct stats write_stats = {
+ÂÂÂ .name = "write", .total = 0, .count = 0, .bytes = 0};
+static struct stats pwrite_stats = {
+ÂÂÂ .name = "pwrite", .total = 0, .count = 0, .bytes = 0};
+static struct stats seek_stats = {
+ÂÂÂ .name = "seek", .total = 0, .count = 0, .bytes = 0};
+
+static void update_stats(struct stats * const ustats,
+ÂÂÂÂÂÂÂÂÂÂÂÂ unsigned long measurement, unsigned long bytes)
+{
+ÂÂÂ ustats->total += measurement;
+ÂÂÂ ustats->bytes += bytes;
+ÂÂÂ ustats->count++;
+}
+
+static void print_ustats(const struct stats * const ustats)
+{
+ÂÂÂ DEBUG_PRINT_L1("%s\t%7d\t%7.0f\t%7.0f\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ ustats->name, ustats->count,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ (float)ustats->total / (float)ustats->count,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ (float)ustats->bytes / (float)ustats->count);
+}
+
+static void print_stats(void)
+{
+ÂÂÂ DEBUG_PRINT_L1("\nSyscall\tCall\tAvgTime\tAvgSize\n"
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "\tCount\t(ticks)\t(bytes)\n"
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "-------------------------------\n");
+
+ÂÂÂ print_ustats(&read_stats);
+ÂÂÂ print_ustats(&pread_stats);
+ÂÂÂ print_ustats(&write_stats);
+ÂÂÂ print_ustats(&pwrite_stats);
+ÂÂÂ print_ustats(&seek_stats);
+}
+
+static int build_memory_map(void)
+{
+ÂÂÂ char line[256];
+ÂÂÂ FILE *fp;
+ÂÂÂ int i;
+
+ÂÂÂ range_count = 0;
+
+ÂÂÂ fp = fopen("/proc/iomem", "r");
+ÂÂÂ if (!fp) {
+ÂÂÂÂÂÂÂ fprintf(stderr, "/proc/iomem: error %d: %s\n",
+ÂÂÂÂÂÂÂÂÂÂÂ errno, strerror(errno));
+ÂÂÂÂÂÂÂ return -errno;
+ÂÂÂ }
+
+ÂÂÂ while (fgets(line, sizeof(line), fp) != 0) {
+ÂÂÂÂÂÂÂ if (strstr(line, system_ram_str)) {
+ÂÂÂÂÂÂÂÂÂÂÂ char *dash, *end_ptr;
+
+ÂÂÂÂÂÂÂÂÂÂÂ /* Given a line like this:
+ÂÂÂÂÂÂÂÂÂÂÂÂ * d0400000-10ffaffff : System RAM
+ÂÂÂÂÂÂÂÂÂÂÂÂ * replace the "-" with a space
+ÂÂÂÂÂÂÂÂÂÂÂÂ */
+ÂÂÂÂÂÂÂÂÂÂÂ dash = strstr(line, "-");
+ÂÂÂÂÂÂÂÂÂÂÂ dash[0] = 0x20;
+
+ÂÂÂÂÂÂÂÂÂÂÂ start_addr[range_count] = strtoull(line, &end_ptr, 16);
+ÂÂÂÂÂÂÂÂÂÂÂ end_addr[range_count] = strtoull(end_ptr, NULL, 16);
+ÂÂÂÂÂÂÂÂÂÂÂ range_count++;
+ÂÂÂÂÂÂÂ }
+ÂÂÂ }
+
+ÂÂÂ fclose(fp);
+
+ÂÂÂ DEBUG_PRINT_L1("RAM Ranges\n");
+ÂÂÂ for (i = 0; i < range_count; i++)
+ÂÂÂÂÂÂÂ DEBUG_PRINT_L1("\trange %d: 0x%llx\t- 0x%llx\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ i, start_addr[i], end_addr[i]);
+
+ÂÂÂ if (range_count == 0) {
+ÂÂÂÂÂÂÂ fprintf(stderr, "No valid address ranges found. Error.\n");
+ÂÂÂÂÂÂÂ return -1;
+ÂÂÂ }
+
+ÂÂÂ return 0;
+}
+
+static int read_adi(int fd, unsigned char *buf, int buf_sz)
+{
+ÂÂÂ int ret, bytes_read = 0;
+ÂÂÂ long start, end, elapsed_time = 0;
+
+ÂÂÂ do {
+ÂÂÂÂÂÂÂ RDTICK(start);
+ÂÂÂÂÂÂÂ ret = read(fd, buf + bytes_read, buf_sz - bytes_read);
+ÂÂÂÂÂÂÂ RDTICK(end);
+ÂÂÂÂÂÂÂ if (ret < 0)
+ÂÂÂÂÂÂÂÂÂÂÂ return -errno;
+
+ÂÂÂÂÂÂÂ elapsed_time += end - start;
+ÂÂÂÂÂÂÂ update_stats(&read_stats, elapsed_time, buf_sz);
+ÂÂÂÂÂÂÂ bytes_read += ret;
+
+ÂÂÂ } while (bytes_read < buf_sz);
+
+ÂÂÂ DEBUG_PRINT_T("\tread elapsed timed = %ld\n", elapsed_time);
+ DEBUG_PRINT_L3("\tRead %d bytes\n", bytes_read);
+
+ÂÂÂ return bytes_read;
+}
+
+static int pread_adi(int fd, unsigned char *buf,
+ÂÂÂÂÂÂÂÂÂÂÂÂ int buf_sz, unsigned long offset)
+{
+ÂÂÂ int ret, i, bytes_read = 0;
+ÂÂÂ unsigned long cur_offset;
+ÂÂÂ long start, end, elapsed_time = 0;
+
+ÂÂÂ cur_offset = offset;
+ÂÂÂ do {
+ÂÂÂÂÂÂÂ RDTICK(start);
+ÂÂÂÂÂÂÂ ret = pread(fd, buf + bytes_read, buf_sz - bytes_read,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ cur_offset);
+ÂÂÂÂÂÂÂ RDTICK(end);
+ÂÂÂÂÂÂÂ if (ret < 0)
+ÂÂÂÂÂÂÂÂÂÂÂ return -errno;
+
+ÂÂÂÂÂÂÂ elapsed_time += end - start;
+ÂÂÂÂÂÂÂ update_stats(&pread_stats, elapsed_time, buf_sz);
+ÂÂÂÂÂÂÂ bytes_read += ret;
+ÂÂÂÂÂÂÂ cur_offset += ret;
+
+ÂÂÂ } while (bytes_read < buf_sz);
+
+ÂÂÂ DEBUG_PRINT_T("\tpread elapsed timed = %ld\n", elapsed_time);
+ DEBUG_PRINT_L3("\tRead %d bytes starting at offset 0x%lx\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ bytes_read, offset);
+ÂÂÂ for (i = 0; i < bytes_read; i++)
+ÂÂÂÂÂÂÂ DEBUG_PRINT_L4("\t\t0x%lx\t%d\n", offset + i, buf[i]);
+
+ÂÂÂ return bytes_read;
+}
+
+static int write_adi(int fd, const unsigned char * const buf, int buf_sz)
+{
+ÂÂÂ int ret, bytes_written = 0;
+ÂÂÂ long start, end, elapsed_time = 0;
+
+ÂÂÂ do {
+ÂÂÂÂÂÂÂ RDTICK(start);
+ÂÂÂÂÂÂÂ ret = write(fd, buf + bytes_written, buf_sz - bytes_written);
+ÂÂÂÂÂÂÂ RDTICK(end);
+ÂÂÂÂÂÂÂ if (ret < 0)
+ÂÂÂÂÂÂÂÂÂÂÂ return -errno;
+
+ÂÂÂÂÂÂÂ elapsed_time += (end - start);
+ÂÂÂÂÂÂÂ update_stats(&write_stats, elapsed_time, buf_sz);
+ÂÂÂÂÂÂÂ bytes_written += ret;
+ÂÂÂ } while (bytes_written < buf_sz);
+
+ÂÂÂ DEBUG_PRINT_T("\twrite elapsed timed = %ld\n", elapsed_time);
+ÂÂÂ DEBUG_PRINT_L3("\tWrote %d of %d bytes\n", bytes_written, buf_sz);
+
+ÂÂÂ return bytes_written;
+}
+
+static int pwrite_adi(int fd, const unsigned char * const buf,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂ int buf_sz, unsigned long offset)
+{
+ÂÂÂ int ret, bytes_written = 0;
+ÂÂÂ unsigned long cur_offset;
+ÂÂÂ long start, end, elapsed_time = 0;
+
+ÂÂÂ cur_offset = offset;
+
+ÂÂÂ do {
+ÂÂÂÂÂÂÂ RDTICK(start);
+ÂÂÂÂÂÂÂ ret = pwrite(fd, buf + bytes_written,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ buf_sz - bytes_written, cur_offset);
+ÂÂÂÂÂÂÂ RDTICK(end);
+ÂÂÂÂÂÂÂ if (ret < 0) {
+ÂÂÂÂÂÂÂÂÂÂÂ fprintf(stderr, "pwrite(): error %d: %s\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ errno, strerror(errno));
+ÂÂÂÂÂÂÂÂÂÂÂ return -errno;
+ÂÂÂÂÂÂÂ }
+
+ÂÂÂÂÂÂÂ elapsed_time += (end - start);
+ÂÂÂÂÂÂÂ update_stats(&pwrite_stats, elapsed_time, buf_sz);
+ÂÂÂÂÂÂÂ bytes_written += ret;
+ÂÂÂÂÂÂÂ cur_offset += ret;
+
+ÂÂÂ } while (bytes_written < buf_sz);
+
+ÂÂÂ DEBUG_PRINT_T("\tpwrite elapsed timed = %ld\n", elapsed_time);
+ÂÂÂ DEBUG_PRINT_L3("\tWrote %d of %d bytes starting at address 0x%lx\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ bytes_written, buf_sz, offset);
+
+ÂÂÂ return bytes_written;
+}
+
+static off_t seek_adi(int fd, off_t offset, int whence)
+{
+ÂÂÂ long start, end;
+ÂÂÂ off_t ret;
+
+ÂÂÂ RDTICK(start);
+ÂÂÂ ret = lseek(fd, offset, whence);
+ÂÂÂ RDTICK(end);
+ÂÂÂ DEBUG_PRINT_L2("\tlseek ret = 0x%llx\n", ret);
+ÂÂÂ if (ret < 0)
+ÂÂÂÂÂÂÂ goto out;
+
+ÂÂÂ DEBUG_PRINT_T("\tlseek elapsed timed = %ld\n", end - start);
+ÂÂÂ update_stats(&seek_stats, end - start, 0);
+
+out:
+ÂÂÂ (void)lseek(fd, 0, SEEK_END);
+ÂÂÂ return ret;
+}
+
+static int test0_prpw_aligned_1byte(int fd)
+{
+ÂÂÂ /* somewhat arbitrarily chosen address */
+ÂÂÂ unsigned long paddr =
+ÂÂÂÂÂÂÂ (end_addr[range_count - 1] - 0x1000) & ~(ADI_BLKSZ - 1);
+ÂÂÂ unsigned char version[1], expected_version;
+ÂÂÂ loff_t offset;
+ÂÂÂ int ret;
+
+ÂÂÂ version[0] = random_version();
+ÂÂÂ expected_version = version[0];
+
+ÂÂÂ offset = paddr / ADI_BLKSZ;
+
+ÂÂÂ ret = pwrite_adi(fd, version, sizeof(version), offset);
+ÂÂÂ if (ret != sizeof(version))
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+
+ÂÂÂ ret = pread_adi(fd, version, sizeof(version), offset);
+ÂÂÂ if (ret != sizeof(version))
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+
+ÂÂÂ if (expected_version != version[0]) {
+ÂÂÂÂÂÂÂ DEBUG_PRINT_L2("\tExpected version %d but read version %d\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ expected_version, version[0]);
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(-expected_version);
+ÂÂÂ }
+
+ÂÂÂ ret = 0;
+out:
+ÂÂÂ RETURN_FROM_TEST(ret);
+}
+
+#define TEST1_VERSION_SZÂÂÂ 4096
+static int test1_prpw_aligned_4096bytes(int fd)
+{
+ÂÂÂ /* somewhat arbitrarily chosen address */
+ÂÂÂ unsigned long paddr =
+ÂÂÂÂÂÂÂ (end_addr[range_count - 1] - 0x6000) & ~(ADI_BLKSZ - 1);
+ÂÂÂ unsigned char version[TEST1_VERSION_SZ],
+ÂÂÂÂÂÂÂ expected_version[TEST1_VERSION_SZ];
+ÂÂÂ loff_t offset;
+ÂÂÂ int ret, i;
+
+ÂÂÂ for (i = 0; i < TEST1_VERSION_SZ; i++) {
+ÂÂÂÂÂÂÂ version[i] = random_version();
+ÂÂÂÂÂÂÂ expected_version[i] = version[i];
+ÂÂÂ }
+
+ÂÂÂ offset = paddr / ADI_BLKSZ;
+
+ÂÂÂ ret = pwrite_adi(fd, version, sizeof(version), offset);
+ÂÂÂ if (ret != sizeof(version))
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+
+ÂÂÂ ret = pread_adi(fd, version, sizeof(version), offset);
+ÂÂÂ if (ret != sizeof(version))
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+
+ÂÂÂ for (i = 0; i < TEST1_VERSION_SZ; i++) {
+ÂÂÂÂÂÂÂ if (expected_version[i] != version[i]) {
+ÂÂÂÂÂÂÂÂÂÂÂ DEBUG_PRINT_L2(
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "\tExpected version %d but read version %d\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ expected_version, version[0]);
+ÂÂÂÂÂÂÂÂÂÂÂ TEST_STEP_FAILURE(-expected_version[i]);
+ÂÂÂÂÂÂÂ }
+ÂÂÂ }
+
+ÂÂÂ ret = 0;
+out:
+ÂÂÂ RETURN_FROM_TEST(ret);
+}
+
+#define TEST2_VERSION_SZÂÂÂ 10327
+static int test2_prpw_aligned_10327bytes(int fd)
+{
+ÂÂÂ /* somewhat arbitrarily chosen address */
+ÂÂÂ unsigned long paddr =
+ÂÂÂÂÂÂÂ (start_addr[0] + 0x6000) & ~(ADI_BLKSZ - 1);
+ÂÂÂ unsigned char version[TEST2_VERSION_SZ],
+ÂÂÂÂÂÂÂ expected_version[TEST2_VERSION_SZ];
+ÂÂÂ loff_t offset;
+ÂÂÂ int ret, i;
+
+ÂÂÂ for (i = 0; i < TEST2_VERSION_SZ; i++) {
+ÂÂÂÂÂÂÂ version[i] = random_version();
+ÂÂÂÂÂÂÂ expected_version[i] = version[i];
+ÂÂÂ }
+
+ÂÂÂ offset = paddr / ADI_BLKSZ;
+
+ÂÂÂ ret = pwrite_adi(fd, version, sizeof(version), offset);
+ÂÂÂ if (ret != sizeof(version))
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+
+ÂÂÂ ret = pread_adi(fd, version, sizeof(version), offset);
+ÂÂÂ if (ret != sizeof(version))
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+
+ÂÂÂ for (i = 0; i < TEST2_VERSION_SZ; i++) {
+ÂÂÂÂÂÂÂ if (expected_version[i] != version[i]) {
+ÂÂÂÂÂÂÂÂÂÂÂ DEBUG_PRINT_L2(
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "\tExpected version %d but read version %d\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ expected_version, version[0]);
+ÂÂÂÂÂÂÂÂÂÂÂ TEST_STEP_FAILURE(-expected_version[i]);
+ÂÂÂÂÂÂÂ }
+ÂÂÂ }
+
+ÂÂÂ ret = 0;
+out:
+ÂÂÂ RETURN_FROM_TEST(ret);
+}
+
+#define TEST3_VERSION_SZÂÂÂ 12541
+static int test3_prpw_unaligned_12541bytes(int fd)
+{
+ÂÂÂ /* somewhat arbitrarily chosen address */
+ÂÂÂ unsigned long paddr =
+ÂÂÂÂÂÂÂ ((start_addr[0] + 0xC000) & ~(ADI_BLKSZ - 1)) + 17;
+ÂÂÂ unsigned char version[TEST3_VERSION_SZ],
+ÂÂÂÂÂÂÂ expected_version[TEST3_VERSION_SZ];
+ÂÂÂ loff_t offset;
+ÂÂÂ int ret, i;
+
+ÂÂÂ for (i = 0; i < TEST3_VERSION_SZ; i++) {
+ÂÂÂÂÂÂÂ version[i] = random_version();
+ÂÂÂÂÂÂÂ expected_version[i] = version[i];
+ÂÂÂ }
+
+ÂÂÂ offset = paddr / ADI_BLKSZ;
+
+ÂÂÂ ret = pwrite_adi(fd, version, sizeof(version), offset);
+ÂÂÂ if (ret != sizeof(version))
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+
+ÂÂÂ ret = pread_adi(fd, version, sizeof(version), offset);
+ÂÂÂ if (ret != sizeof(version))
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+
+ÂÂÂ for (i = 0; i < TEST3_VERSION_SZ; i++) {
+ÂÂÂÂÂÂÂ if (expected_version[i] != version[i]) {
+ÂÂÂÂÂÂÂÂÂÂÂ DEBUG_PRINT_L2(
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "\tExpected version %d but read version %d\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ expected_version, version[0]);
+ÂÂÂÂÂÂÂÂÂÂÂ TEST_STEP_FAILURE(-expected_version[i]);
+ÂÂÂÂÂÂÂ }
+ÂÂÂ }
+
+ÂÂÂ ret = 0;
+out:
+ÂÂÂ RETURN_FROM_TEST(ret);
+}
+
+static int test4_lseek(int fd)
+{
+#defineÂÂÂ OFFSET_ADDÂÂÂ (0x100)
+#define OFFSET_SUBTRACTÂÂÂ (0xFFFFFFF000000000)
+
+ÂÂÂ off_t offset_out, offset_in;
+ÂÂÂ int ret;
+
+
+ÂÂÂ offset_in = 0x123456789abcdef0;
+ÂÂÂ offset_out = seek_adi(fd, offset_in, SEEK_SET);
+ÂÂÂ if (offset_out != offset_in) {
+ÂÂÂÂÂÂÂ ret = -1;
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+ÂÂÂ }
+
+ /* seek to the current offset. this should return EINVAL */
+ÂÂÂ offset_out = seek_adi(fd, offset_in, SEEK_SET);
+ÂÂÂ if (offset_out < 0 && errno == EINVAL)
+ÂÂÂÂÂÂÂ DEBUG_PRINT_L2(
+ÂÂÂÂÂÂÂÂÂÂÂ "\tSEEK_SET failed as designed. Not an error\n");
+ÂÂÂ else {
+ÂÂÂÂÂÂÂ ret = -2;
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+ÂÂÂ }
+
+ÂÂÂ offset_out = seek_adi(fd, 0, SEEK_CUR);
+ÂÂÂ if (offset_out != offset_in) {
+ÂÂÂÂÂÂÂ ret = -3;
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+ÂÂÂ }
+
+ÂÂÂ offset_out = seek_adi(fd, OFFSET_ADD, SEEK_CUR);
+ÂÂÂ if (offset_out != (offset_in + OFFSET_ADD)) {
+ÂÂÂÂÂÂÂ ret = -4;
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+ÂÂÂ }
+
+ÂÂÂ offset_out = seek_adi(fd, OFFSET_SUBTRACT, SEEK_CUR);
+ÂÂÂ if (offset_out != (offset_in + OFFSET_ADD + OFFSET_SUBTRACT)) {
+ÂÂÂÂÂÂÂ ret = -5;
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+ÂÂÂ }
+
+ÂÂÂ ret = 0;
+out:
+ÂÂÂ RETURN_FROM_TEST(ret);
+}
+
+static int test5_rw_aligned_1byte(int fd)
+{
+ÂÂÂ /* somewhat arbitrarily chosen address */
+ÂÂÂ unsigned long paddr =
+ÂÂÂÂÂÂÂ (end_addr[range_count - 1] - 0xF000) & ~(ADI_BLKSZ - 1);
+ÂÂÂ unsigned char version, expected_version;
+ÂÂÂ loff_t offset;
+ÂÂÂ off_t oret;
+ÂÂÂ int ret;
+
+ÂÂÂ offset = paddr / ADI_BLKSZ;
+ÂÂÂ version = expected_version = random_version();
+
+ÂÂÂ oret = seek_adi(fd, offset, SEEK_SET);
+ÂÂÂ if (oret != offset) {
+ÂÂÂÂÂÂÂ ret = -1;
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+ÂÂÂ }
+
+ÂÂÂ ret = write_adi(fd, &version, sizeof(version));
+ÂÂÂ if (ret != sizeof(version))
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+
+ÂÂÂ oret = seek_adi(fd, offset, SEEK_SET);
+ÂÂÂ if (oret != offset) {
+ÂÂÂÂÂÂÂ ret = -1;
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+ÂÂÂ }
+
+ÂÂÂ ret = read_adi(fd, &version, sizeof(version));
+ÂÂÂ if (ret != sizeof(version))
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+
+ÂÂÂ if (expected_version != version) {
+ÂÂÂÂÂÂÂ DEBUG_PRINT_L2("\tExpected version %d but read version %d\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ expected_version, version);
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(-expected_version);
+ÂÂÂ }
+
+ÂÂÂ ret = 0;
+out:
+ÂÂÂ RETURN_FROM_TEST(ret);
+}
+
+#define TEST6_VERSION_SZÂÂÂÂÂÂÂ 9434
+static int test6_rw_aligned_9434bytes(int fd)
+{
+ÂÂÂ /* somewhat arbitrarily chosen address */
+ÂÂÂ unsigned long paddr =
+ÂÂÂÂÂÂÂ (end_addr[range_count - 1] - 0x5F000) & ~(ADI_BLKSZ - 1);
+ÂÂÂ unsigned char version[TEST6_VERSION_SZ],
+ÂÂÂÂÂÂÂÂÂÂÂÂÂ expected_version[TEST6_VERSION_SZ];
+ÂÂÂ loff_t offset;
+ÂÂÂ off_t oret;
+ÂÂÂ int ret, i;
+
+ÂÂÂ offset = paddr / ADI_BLKSZ;
+ÂÂÂ for (i = 0; i < TEST6_VERSION_SZ; i++)
+ÂÂÂÂÂÂÂ version[i] = expected_version[i] = random_version();
+
+ÂÂÂ oret = seek_adi(fd, offset, SEEK_SET);
+ÂÂÂ if (oret != offset) {
+ÂÂÂÂÂÂÂ ret = -1;
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+ÂÂÂ }
+
+ÂÂÂ ret = write_adi(fd, version, sizeof(version));
+ÂÂÂ if (ret != sizeof(version))
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+
+ÂÂÂ memset(version, 0, TEST6_VERSION_SZ);
+
+ÂÂÂ oret = seek_adi(fd, offset, SEEK_SET);
+ÂÂÂ if (oret != offset) {
+ÂÂÂÂÂÂÂ ret = -1;
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+ÂÂÂ }
+
+ÂÂÂ ret = read_adi(fd, version, sizeof(version));
+ÂÂÂ if (ret != sizeof(version))
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+
+ÂÂÂ for (i = 0; i < TEST6_VERSION_SZ; i++) {
+ÂÂÂÂÂÂÂ if (expected_version[i] != version[i]) {
+ÂÂÂÂÂÂÂÂÂÂÂ DEBUG_PRINT_L2(
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "\tExpected version %d but read version %d\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ expected_version[i], version[i]);
+ÂÂÂÂÂÂÂÂÂÂÂ TEST_STEP_FAILURE(-expected_version[i]);
+ÂÂÂÂÂÂÂ }
+ÂÂÂ }
+
+ÂÂÂ ret = 0;
+out:
+ÂÂÂ RETURN_FROM_TEST(ret);
+}
+
+#define TEST7_VERSION_SZÂÂÂÂÂÂÂ 14963
+static int test7_rw_aligned_14963bytes(int fd)
+{
+ÂÂÂ /* somewhat arbitrarily chosen address */
+ÂÂÂ unsigned long paddr =
+ÂÂÂÂÂ ((start_addr[range_count - 1] + 0xF000) & ~(ADI_BLKSZ - 1)) + 39;
+ÂÂÂ unsigned char version[TEST7_VERSION_SZ],
+ÂÂÂÂÂÂÂÂÂÂÂÂÂ expected_version[TEST7_VERSION_SZ];
+ÂÂÂ loff_t offset;
+ÂÂÂ off_t oret;
+ÂÂÂ int ret, i;
+
+ÂÂÂ offset = paddr / ADI_BLKSZ;
+ÂÂÂ for (i = 0; i < TEST7_VERSION_SZ; i++) {
+ÂÂÂÂÂÂÂ version[i] = random_version();
+ÂÂÂÂÂÂÂ expected_version[i] = version[i];
+ÂÂÂ }
+
+ÂÂÂ oret = seek_adi(fd, offset, SEEK_SET);
+ÂÂÂ if (oret != offset) {
+ÂÂÂÂÂÂÂ ret = -1;
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+ÂÂÂ }
+
+ÂÂÂ ret = write_adi(fd, version, sizeof(version));
+ÂÂÂ if (ret != sizeof(version))
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+
+ÂÂÂ memset(version, 0, TEST7_VERSION_SZ);
+
+ÂÂÂ oret = seek_adi(fd, offset, SEEK_SET);
+ÂÂÂ if (oret != offset) {
+ÂÂÂÂÂÂÂ ret = -1;
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+ÂÂÂ }
+
+ÂÂÂ ret = read_adi(fd, version, sizeof(version));
+ÂÂÂ if (ret != sizeof(version))
+ÂÂÂÂÂÂÂ TEST_STEP_FAILURE(ret);
+
+ÂÂÂ for (i = 0; i < TEST7_VERSION_SZ; i++) {
+ÂÂÂÂÂÂÂ if (expected_version[i] != version[i]) {
+ÂÂÂÂÂÂÂÂÂÂÂ DEBUG_PRINT_L2(
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "\tExpected version %d but read version %d\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ expected_version[i], version[i]);
+ÂÂÂÂÂÂÂÂÂÂÂ TEST_STEP_FAILURE(-expected_version[i]);
+ÂÂÂÂÂÂÂ }
+
+ÂÂÂÂÂÂÂ paddr += ADI_BLKSZ;
+ÂÂÂ }
+
+ÂÂÂ ret = 0;
+out:
+ÂÂÂ RETURN_FROM_TEST(ret);
+}
+
+static int (*tests[])(int fd) = {
+ÂÂÂ test0_prpw_aligned_1byte,
+ÂÂÂ test1_prpw_aligned_4096bytes,
+ÂÂÂ test2_prpw_aligned_10327bytes,
+ÂÂÂ test3_prpw_unaligned_12541bytes,
+ÂÂÂ test4_lseek,
+ÂÂÂ test5_rw_aligned_1byte,
+ÂÂÂ test6_rw_aligned_9434bytes,
+ÂÂÂ test7_rw_aligned_14963bytes,
+};
+#define TEST_COUNTÂÂÂ ARRAY_SIZE(tests)
+
+int main(int argc, char *argv[])
+{
+ÂÂÂ int fd, ret, test;
+
+ÂÂÂ ret = build_memory_map();
+ÂÂÂ if (ret < 0)
+ÂÂÂÂÂÂÂ return ret;
+
+ÂÂÂ fd = open("/dev/adi", O_RDWR);
+ÂÂÂ if (fd < 0) {
+ÂÂÂÂÂÂÂ fprintf(stderr, "open: error %d: %s\n",
+ÂÂÂÂÂÂÂÂÂÂÂ errno, strerror(errno));
+ÂÂÂÂÂÂÂ return -errno;
+ÂÂÂ }
+
+ÂÂÂ for (test = 0; test < TEST_COUNT; test++) {
+ÂÂÂÂÂÂÂ DEBUG_PRINT_L1("Running test #%d\n", test);
+
+ÂÂÂÂÂÂÂ ret = (*tests[test])(fd);
+ÂÂÂÂÂÂÂ if (ret != 0)
+ÂÂÂÂÂÂÂÂÂÂÂ ksft_test_result_fail("Test #%d failed: error %d\n",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ test, ret);
+ÂÂÂÂÂÂÂ else
+ÂÂÂÂÂÂÂÂÂÂÂ ksft_test_result_pass("Test #%d passed\n", test);
+ÂÂÂ }
+
+ÂÂÂ print_stats();
+ÂÂÂ close(fd);
+
+ÂÂÂ if (ksft_get_fail_cnt() > 0)
+ÂÂÂÂÂÂÂ ksft_exit_fail();
+ÂÂÂ else
+ÂÂÂÂÂÂÂ ksft_exit_pass();
+
+ÂÂÂ /* it's impossible to get here, but the compiler throws a warning
+ÂÂÂÂ * about control reaching the end of non-void function. bah.
+ÂÂÂÂ */
+ÂÂÂ return 0;
+}
diff --git a/tools/testing/selftests/sparc64/drivers/drivers_test.sh b/tools/testing/selftests/sparc64/drivers/drivers_test.sh
new file mode 100755
index 000000000000..6d08273b7532
--- /dev/null
+++ b/tools/testing/selftests/sparc64/drivers/drivers_test.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+SRC_TREE=../../../../
+
+test_run()
+{
+ÂÂÂ if [ -f ${SRC_TREE}/drivers/char/adi.ko ]; then
+ÂÂÂÂÂÂÂ insmod ${SRC_TREE}/drivers/char/adi.ko 2> /dev/null
+ÂÂÂÂÂÂÂ if [ $? -ne 0 ]; then
+ÂÂÂÂÂÂÂÂÂÂÂ rc=1
+ÂÂÂÂÂÂÂ fi
+ÂÂÂ else
+ÂÂÂÂÂÂÂ # Use modprobe dry run to check for missing adi module
+ÂÂÂÂÂÂÂ if ! /sbin/modprobe -q -n adi; then
+ÂÂÂÂÂÂÂÂÂÂÂ echo "adi: [SKIP]"
+ÂÂÂÂÂÂÂ elif /sbin/modprobe -q adi; then
+ÂÂÂÂÂÂÂÂÂÂÂ echo "adi: ok"
+ÂÂÂÂÂÂÂ else
+ÂÂÂÂÂÂÂÂÂÂÂ echo "adi: [FAIL]"
+ÂÂÂÂÂÂÂÂÂÂÂ rc=1
+ÂÂÂÂÂÂÂ fi
+ÂÂÂ fi
+ÂÂÂ ./adi-test
+ÂÂÂ rmmod adi 2> /dev/null
+}
+
+rc=0
+test_run
+exit $rc
diff --git a/tools/testing/selftests/sparc64/run.sh b/tools/testing/selftests/sparc64/run.sh
new file mode 100755
index 000000000000..38ad61f9328e
--- /dev/null
+++ b/tools/testing/selftests/sparc64/run.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+(cd drivers; ./drivers_test.sh)