WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / powerpc / dscr / dscr_sysfs_test.c
blobfbbdffdb2e5d2a9901e3a1bc241d501b17cac4aa
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * POWER Data Stream Control Register (DSCR) sysfs interface test
5 * This test updates to system wide DSCR default through the sysfs interface
6 * and then verifies that all the CPU specific DSCR defaults are updated as
7 * well verified from their sysfs interfaces.
9 * Copyright 2015, Anshuman Khandual, IBM Corporation.
11 #include "dscr.h"
13 static int check_cpu_dscr_default(char *file, unsigned long val)
15 char buf[10];
16 int fd, rc;
18 fd = open(file, O_RDWR);
19 if (fd == -1) {
20 perror("open() failed");
21 return 1;
24 rc = read(fd, buf, sizeof(buf));
25 if (rc == -1) {
26 perror("read() failed");
27 return 1;
29 close(fd);
31 buf[rc] = '\0';
32 if (strtol(buf, NULL, 16) != val) {
33 printf("DSCR match failed: %ld (system) %ld (cpu)\n",
34 val, strtol(buf, NULL, 16));
35 return 1;
37 return 0;
40 static int check_all_cpu_dscr_defaults(unsigned long val)
42 DIR *sysfs;
43 struct dirent *dp;
44 char file[LEN_MAX];
46 sysfs = opendir(CPU_PATH);
47 if (!sysfs) {
48 perror("opendir() failed");
49 return 1;
52 while ((dp = readdir(sysfs))) {
53 int len;
55 if (!(dp->d_type & DT_DIR))
56 continue;
57 if (!strcmp(dp->d_name, "cpuidle"))
58 continue;
59 if (!strstr(dp->d_name, "cpu"))
60 continue;
62 len = snprintf(file, LEN_MAX, "%s%s/dscr", CPU_PATH, dp->d_name);
63 if (len >= LEN_MAX)
64 continue;
65 if (access(file, F_OK))
66 continue;
68 if (check_cpu_dscr_default(file, val))
69 return 1;
71 closedir(sysfs);
72 return 0;
75 int dscr_sysfs(void)
77 unsigned long orig_dscr_default;
78 int i, j;
80 SKIP_IF(!have_hwcap2(PPC_FEATURE2_DSCR));
82 orig_dscr_default = get_default_dscr();
83 for (i = 0; i < COUNT; i++) {
84 for (j = 0; j < DSCR_MAX; j++) {
85 set_default_dscr(j);
86 if (check_all_cpu_dscr_defaults(j))
87 goto fail;
90 set_default_dscr(orig_dscr_default);
91 return 0;
92 fail:
93 set_default_dscr(orig_dscr_default);
94 return 1;
97 int main(int argc, char *argv[])
99 return test_harness(dscr_sysfs, "dscr_sysfs_test");