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 
check_cpu_dscr_default(char * file,unsigned long val)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 
check_all_cpu_dscr_defaults(unsigned long val)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 
dscr_sysfs(void)67 int dscr_sysfs(void)
68 {
69 	unsigned long orig_dscr_default;
70 
71 	SKIP_IF(!have_hwcap2(PPC_FEATURE2_DSCR));
72 
73 	orig_dscr_default = get_default_dscr();
74 	for (int i = 0; i < DSCR_MAX; i++) {
75 		set_default_dscr(i);
76 		if (check_all_cpu_dscr_defaults(i))
77 			goto fail;
78 	}
79 	set_default_dscr(orig_dscr_default);
80 	return 0;
81 fail:
82 	set_default_dscr(orig_dscr_default);
83 	return 1;
84 }
85 
main(int argc,char * argv[])86 int main(int argc, char *argv[])
87 {
88 	return test_harness(dscr_sysfs, "dscr_sysfs_test");
89 }
90