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