1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2af0be08eSMichael Ellerman /*
3af0be08eSMichael Ellerman  * POWER Data Stream Control Register (DSCR) sysfs thread test
4af0be08eSMichael Ellerman  *
5af0be08eSMichael Ellerman  * This test updates the system wide DSCR default value through
6af0be08eSMichael Ellerman  * sysfs interface which should then update all the CPU specific
7af0be08eSMichael Ellerman  * DSCR default values which must also be then visible to threads
8af0be08eSMichael Ellerman  * executing on individual CPUs on the system.
9af0be08eSMichael Ellerman  *
10af0be08eSMichael Ellerman  * Copyright 2015, Anshuman Khandual, IBM Corporation.
11af0be08eSMichael Ellerman  */
12af0be08eSMichael Ellerman #define _GNU_SOURCE
13af0be08eSMichael Ellerman #include "dscr.h"
14af0be08eSMichael Ellerman 
test_thread_dscr(unsigned long val)15af0be08eSMichael Ellerman static int test_thread_dscr(unsigned long val)
16af0be08eSMichael Ellerman {
17af0be08eSMichael Ellerman 	unsigned long cur_dscr, cur_dscr_usr;
18af0be08eSMichael Ellerman 
19af0be08eSMichael Ellerman 	cur_dscr = get_dscr();
20af0be08eSMichael Ellerman 	cur_dscr_usr = get_dscr_usr();
21af0be08eSMichael Ellerman 
22af0be08eSMichael Ellerman 	if (val != cur_dscr) {
23af0be08eSMichael Ellerman 		printf("[cpu %d] Kernel DSCR should be %ld but is %ld\n",
24af0be08eSMichael Ellerman 					sched_getcpu(), val, cur_dscr);
25af0be08eSMichael Ellerman 		return 1;
26af0be08eSMichael Ellerman 	}
27af0be08eSMichael Ellerman 
28af0be08eSMichael Ellerman 	if (val != cur_dscr_usr) {
29af0be08eSMichael Ellerman 		printf("[cpu %d] User DSCR should be %ld but is %ld\n",
30af0be08eSMichael Ellerman 					sched_getcpu(), val, cur_dscr_usr);
31af0be08eSMichael Ellerman 		return 1;
32af0be08eSMichael Ellerman 	}
33af0be08eSMichael Ellerman 	return 0;
34af0be08eSMichael Ellerman }
35af0be08eSMichael Ellerman 
check_cpu_dscr_thread(unsigned long val)36af0be08eSMichael Ellerman static int check_cpu_dscr_thread(unsigned long val)
37af0be08eSMichael Ellerman {
38af0be08eSMichael Ellerman 	cpu_set_t mask;
39af0be08eSMichael Ellerman 	int cpu;
40af0be08eSMichael Ellerman 
41af0be08eSMichael Ellerman 	for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
42af0be08eSMichael Ellerman 		CPU_ZERO(&mask);
43af0be08eSMichael Ellerman 		CPU_SET(cpu, &mask);
44af0be08eSMichael Ellerman 		if (sched_setaffinity(0, sizeof(mask), &mask))
45af0be08eSMichael Ellerman 			continue;
46af0be08eSMichael Ellerman 
47af0be08eSMichael Ellerman 		if (test_thread_dscr(val))
48af0be08eSMichael Ellerman 			return 1;
49af0be08eSMichael Ellerman 	}
50af0be08eSMichael Ellerman 	return 0;
51af0be08eSMichael Ellerman 
52af0be08eSMichael Ellerman }
53af0be08eSMichael Ellerman 
dscr_sysfs_thread(void)54af0be08eSMichael Ellerman int dscr_sysfs_thread(void)
55af0be08eSMichael Ellerman {
56af0be08eSMichael Ellerman 	unsigned long orig_dscr_default;
57af0be08eSMichael Ellerman 	int i, j;
58af0be08eSMichael Ellerman 
594c3c3c50SMichael Ellerman 	SKIP_IF(!have_hwcap2(PPC_FEATURE2_DSCR));
604c3c3c50SMichael Ellerman 
61af0be08eSMichael Ellerman 	orig_dscr_default = get_default_dscr();
62af0be08eSMichael Ellerman 	for (i = 0; i < COUNT; i++) {
63af0be08eSMichael Ellerman 		for (j = 0; j < DSCR_MAX; j++) {
64af0be08eSMichael Ellerman 			set_default_dscr(j);
65af0be08eSMichael Ellerman 			if (check_cpu_dscr_thread(j))
66af0be08eSMichael Ellerman 				goto fail;
67af0be08eSMichael Ellerman 		}
68af0be08eSMichael Ellerman 	}
69af0be08eSMichael Ellerman 	set_default_dscr(orig_dscr_default);
70af0be08eSMichael Ellerman 	return 0;
71af0be08eSMichael Ellerman fail:
72af0be08eSMichael Ellerman 	set_default_dscr(orig_dscr_default);
73af0be08eSMichael Ellerman 	return 1;
74af0be08eSMichael Ellerman }
75af0be08eSMichael Ellerman 
main(int argc,char * argv[])76af0be08eSMichael Ellerman int main(int argc, char *argv[])
77af0be08eSMichael Ellerman {
78af0be08eSMichael Ellerman 	return test_harness(dscr_sysfs_thread, "dscr_sysfs_thread_test");
79af0be08eSMichael Ellerman }
80