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 		close(fd);
28 		return 1;
29 	}
30 	close(fd);
31 
32 	buf[rc] = '\0';
33 	if (strtol(buf, NULL, 16) != val) {
34 		printf("DSCR match failed: %ld (system) %ld (cpu)\n",
35 					val, strtol(buf, NULL, 16));
36 		return 1;
37 	}
38 	return 0;
39 }
40 
41 static int check_all_cpu_dscr_defaults(unsigned long val)
42 {
43 	DIR *sysfs;
44 	struct dirent *dp;
45 	char file[LEN_MAX];
46 
47 	sysfs = opendir(CPU_PATH);
48 	if (!sysfs) {
49 		perror("opendir() failed");
50 		return 1;
51 	}
52 
53 	while ((dp = readdir(sysfs))) {
54 		int len;
55 
56 		if (!(dp->d_type & DT_DIR))
57 			continue;
58 		if (!strcmp(dp->d_name, "cpuidle"))
59 			continue;
60 		if (!strstr(dp->d_name, "cpu"))
61 			continue;
62 
63 		len = snprintf(file, LEN_MAX, "%s%s/dscr", CPU_PATH, dp->d_name);
64 		if (len >= LEN_MAX)
65 			continue;
66 		if (access(file, F_OK))
67 			continue;
68 
69 		if (check_cpu_dscr_default(file, val)) {
70 			closedir(sysfs);
71 			return 1;
72 		}
73 	}
74 	closedir(sysfs);
75 	return 0;
76 }
77 
78 int dscr_sysfs(void)
79 {
80 	unsigned long orig_dscr_default;
81 	int i, j;
82 
83 	SKIP_IF(!have_hwcap2(PPC_FEATURE2_DSCR));
84 
85 	orig_dscr_default = get_default_dscr();
86 	for (i = 0; i < COUNT; i++) {
87 		for (j = 0; j < DSCR_MAX; j++) {
88 			set_default_dscr(j);
89 			if (check_all_cpu_dscr_defaults(j))
90 				goto fail;
91 		}
92 	}
93 	set_default_dscr(orig_dscr_default);
94 	return 0;
95 fail:
96 	set_default_dscr(orig_dscr_default);
97 	return 1;
98 }
99 
100 int main(int argc, char *argv[])
101 {
102 	return test_harness(dscr_sysfs, "dscr_sysfs_test");
103 }
104