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