Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / powerpc / dscr / dscr_sysfs_test.c
blob1899bd85121f0ba46cec282f8dcfadc4cc34fa2c
1 /*
2 * POWER Data Stream Control Register (DSCR) sysfs interface test
4 * This test updates to system wide DSCR default through the sysfs interface
5 * and then verifies that all the CPU specific DSCR defaults are updated as
6 * well verified from their sysfs interfaces.
8 * Copyright 2015, Anshuman Khandual, IBM Corporation.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
14 #include "dscr.h"
16 static int check_cpu_dscr_default(char *file, unsigned long val)
18 char buf[10];
19 int fd, rc;
21 fd = open(file, O_RDWR);
22 if (fd == -1) {
23 perror("open() failed");
24 return 1;
27 rc = read(fd, buf, sizeof(buf));
28 if (rc == -1) {
29 perror("read() failed");
30 return 1;
32 close(fd);
34 buf[rc] = '\0';
35 if (strtol(buf, NULL, 16) != val) {
36 printf("DSCR match failed: %ld (system) %ld (cpu)\n",
37 val, strtol(buf, NULL, 16));
38 return 1;
40 return 0;
43 static int check_all_cpu_dscr_defaults(unsigned long val)
45 DIR *sysfs;
46 struct dirent *dp;
47 char file[LEN_MAX];
49 sysfs = opendir(CPU_PATH);
50 if (!sysfs) {
51 perror("opendir() failed");
52 return 1;
55 while ((dp = readdir(sysfs))) {
56 int len;
58 if (!(dp->d_type & DT_DIR))
59 continue;
60 if (!strcmp(dp->d_name, "cpuidle"))
61 continue;
62 if (!strstr(dp->d_name, "cpu"))
63 continue;
65 len = snprintf(file, LEN_MAX, "%s%s/dscr", CPU_PATH, dp->d_name);
66 if (len >= LEN_MAX)
67 continue;
68 if (access(file, F_OK))
69 continue;
71 if (check_cpu_dscr_default(file, val))
72 return 1;
74 closedir(sysfs);
75 return 0;
78 int dscr_sysfs(void)
80 unsigned long orig_dscr_default;
81 int i, j;
83 orig_dscr_default = get_default_dscr();
84 for (i = 0; i < COUNT; i++) {
85 for (j = 0; j < DSCR_MAX; j++) {
86 set_default_dscr(j);
87 if (check_all_cpu_dscr_defaults(j))
88 goto fail;
91 set_default_dscr(orig_dscr_default);
92 return 0;
93 fail:
94 set_default_dscr(orig_dscr_default);
95 return 1;
98 int main(int argc, char *argv[])
100 return test_harness(dscr_sysfs, "dscr_sysfs_test");