GNU Linux-libre 4.14.303-gnu1
[releases.git] / tools / testing / selftests / powerpc / dscr / dscr_sysfs_test.c
1 /*
2  * POWER Data Stream Control Register (DSCR) sysfs interface test
3  *
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.
7  *
8  * Copyright 2015, Anshuman Khandual, IBM Corporation.
9  *
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.
13  */
14 #include "dscr.h"
15
16 static int check_cpu_dscr_default(char *file, unsigned long val)
17 {
18         char buf[10];
19         int fd, rc;
20
21         fd = open(file, O_RDWR);
22         if (fd == -1) {
23                 perror("open() failed");
24                 return 1;
25         }
26
27         rc = read(fd, buf, sizeof(buf));
28         if (rc == -1) {
29                 perror("read() failed");
30                 close(fd);
31                 return 1;
32         }
33         close(fd);
34
35         buf[rc] = '\0';
36         if (strtol(buf, NULL, 16) != val) {
37                 printf("DSCR match failed: %ld (system) %ld (cpu)\n",
38                                         val, strtol(buf, NULL, 16));
39                 return 1;
40         }
41         return 0;
42 }
43
44 static int check_all_cpu_dscr_defaults(unsigned long val)
45 {
46         DIR *sysfs;
47         struct dirent *dp;
48         char file[LEN_MAX];
49
50         sysfs = opendir(CPU_PATH);
51         if (!sysfs) {
52                 perror("opendir() failed");
53                 return 1;
54         }
55
56         while ((dp = readdir(sysfs))) {
57                 if (!(dp->d_type & DT_DIR))
58                         continue;
59                 if (!strcmp(dp->d_name, "cpuidle"))
60                         continue;
61                 if (!strstr(dp->d_name, "cpu"))
62                         continue;
63
64                 sprintf(file, "%s%s/dscr", CPU_PATH, dp->d_name);
65                 if (access(file, F_OK))
66                         continue;
67
68                 if (check_cpu_dscr_default(file, val)) {
69                         closedir(sysfs);
70                         return 1;
71                 }
72         }
73         closedir(sysfs);
74         return 0;
75 }
76
77 int dscr_sysfs(void)
78 {
79         unsigned long orig_dscr_default;
80         int i, j;
81
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;
88                 }
89         }
90         set_default_dscr(orig_dscr_default);
91         return 0;
92 fail:
93         set_default_dscr(orig_dscr_default);
94         return 1;
95 }
96
97 int main(int argc, char *argv[])
98 {
99         return test_harness(dscr_sysfs, "dscr_sysfs_test");
100 }