1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * POWER Data Stream Control Register (DSCR) sysfs interface test
4  *
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.
8  *
9  * Copyright 2015, Anshuman Khandual, IBM Corporation.
10  */
11 #include "dscr.h"
12 
13 static int check_cpu_dscr_default(char *file, unsigned long val)
14 {
15 	char buf[10];
16 	int fd, rc;
17 
18 	fd = open(file, O_RDWR);
19 	if (fd == -1) {
20 		perror("open() failed");
21 		return 1;
22 	}
23 
24 	rc = read(fd, buf, sizeof(buf));
25 	if (rc == -1) {
26 		perror("read() failed");
27 		return 1;
28 	}
29 	close(fd);
30 
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;
36 	}
37 	return 0;
38 }
39 
40 static int check_all_cpu_dscr_defaults(unsigned long val)
41 {
42 	DIR *sysfs;
43 	struct dirent *dp;
44 	char file[LEN_MAX];
45 
46 	sysfs = opendir(CPU_PATH);
47 	if (!sysfs) {
48 		perror("opendir() failed");
49 		return 1;
50 	}
51 
52 	while ((dp = readdir(sysfs))) {
53 		int len;
54 
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;
61 
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;
67 
68 		if (check_cpu_dscr_default(file, val))
69 			return 1;
70 	}
71 	closedir(sysfs);
72 	return 0;
73 }
74 
75 int dscr_sysfs(void)
76 {
77 	unsigned long orig_dscr_default;
78 	int i, j;
79 
80 	orig_dscr_default = get_default_dscr();
81 	for (i = 0; i < COUNT; i++) {
82 		for (j = 0; j < DSCR_MAX; j++) {
83 			set_default_dscr(j);
84 			if (check_all_cpu_dscr_defaults(j))
85 				goto fail;
86 		}
87 	}
88 	set_default_dscr(orig_dscr_default);
89 	return 0;
90 fail:
91 	set_default_dscr(orig_dscr_default);
92 	return 1;
93 }
94 
95 int main(int argc, char *argv[])
96 {
97 	return test_harness(dscr_sysfs, "dscr_sysfs_test");
98 }
99