Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / powerpc / dscr / dscr_default_test.c
blob9e1a37e93b63f7d4c8ece995fc2d2439f1d3cf40
1 /*
2 * POWER Data Stream Control Register (DSCR) default test
4 * This test modifies the system wide default DSCR through
5 * it's sysfs interface and then verifies that all threads
6 * see the correct changed DSCR value immediately.
8 * Copyright 2012, Anton Blanchard, IBM Corporation.
9 * Copyright 2015, Anshuman Khandual, IBM Corporation.
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.
15 #include "dscr.h"
17 static unsigned long dscr; /* System DSCR default */
18 static unsigned long sequence;
19 static unsigned long result[THREADS];
21 static void *do_test(void *in)
23 unsigned long thread = (unsigned long)in;
24 unsigned long i;
26 for (i = 0; i < COUNT; i++) {
27 unsigned long d, cur_dscr, cur_dscr_usr;
28 unsigned long s1, s2;
30 s1 = READ_ONCE(sequence);
31 if (s1 & 1)
32 continue;
33 rmb();
35 d = dscr;
36 cur_dscr = get_dscr();
37 cur_dscr_usr = get_dscr_usr();
39 rmb();
40 s2 = sequence;
42 if (s1 != s2)
43 continue;
45 if (cur_dscr != d) {
46 fprintf(stderr, "thread %ld kernel DSCR should be %ld "
47 "but is %ld\n", thread, d, cur_dscr);
48 result[thread] = 1;
49 pthread_exit(&result[thread]);
52 if (cur_dscr_usr != d) {
53 fprintf(stderr, "thread %ld user DSCR should be %ld "
54 "but is %ld\n", thread, d, cur_dscr_usr);
55 result[thread] = 1;
56 pthread_exit(&result[thread]);
59 result[thread] = 0;
60 pthread_exit(&result[thread]);
63 int dscr_default(void)
65 pthread_t threads[THREADS];
66 unsigned long i, *status[THREADS];
67 unsigned long orig_dscr_default;
69 orig_dscr_default = get_default_dscr();
71 /* Initial DSCR default */
72 dscr = 1;
73 set_default_dscr(dscr);
75 /* Spawn all testing threads */
76 for (i = 0; i < THREADS; i++) {
77 if (pthread_create(&threads[i], NULL, do_test, (void *)i)) {
78 perror("pthread_create() failed");
79 goto fail;
83 srand(getpid());
85 /* Keep changing the DSCR default */
86 for (i = 0; i < COUNT; i++) {
87 double ret = uniform_deviate(rand());
89 if (ret < 0.0001) {
90 sequence++;
91 wmb();
93 dscr++;
94 if (dscr > DSCR_MAX)
95 dscr = 0;
97 set_default_dscr(dscr);
99 wmb();
100 sequence++;
104 /* Individual testing thread exit status */
105 for (i = 0; i < THREADS; i++) {
106 if (pthread_join(threads[i], (void **)&(status[i]))) {
107 perror("pthread_join() failed");
108 goto fail;
111 if (*status[i]) {
112 printf("%ldth thread failed to join with %ld status\n",
113 i, *status[i]);
114 goto fail;
117 set_default_dscr(orig_dscr_default);
118 return 0;
119 fail:
120 set_default_dscr(orig_dscr_default);
121 return 1;
124 int main(int argc, char *argv[])
126 return test_harness(dscr_default, "dscr_default_test");