1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * POWER Data Stream Control Register (DSCR)
4  *
5  * This header file contains helper functions and macros
6  * required for all the DSCR related test cases.
7  *
8  * Copyright 2012, Anton Blanchard, IBM Corporation.
9  * Copyright 2015, Anshuman Khandual, IBM Corporation.
10  */
11 #ifndef _SELFTESTS_POWERPC_DSCR_DSCR_H
12 #define _SELFTESTS_POWERPC_DSCR_DSCR_H
13 
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <fcntl.h>
19 #include <dirent.h>
20 #include <pthread.h>
21 #include <sched.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/wait.h>
25 
26 #include "reg.h"
27 #include "utils.h"
28 
29 #define THREADS		100	/* Max threads */
30 #define COUNT		100	/* Max iterations */
31 #define DSCR_MAX	16	/* Max DSCR value */
32 #define LEN_MAX		100	/* Max name length */
33 
34 #define DSCR_DEFAULT	"/sys/devices/system/cpu/dscr_default"
35 #define CPU_PATH	"/sys/devices/system/cpu/"
36 
37 #define rmb()  asm volatile("lwsync":::"memory")
38 #define wmb()  asm volatile("lwsync":::"memory")
39 
40 #define READ_ONCE(x) (*(volatile typeof(x) *)&(x))
41 
42 /* Prilvilege state DSCR access */
43 inline unsigned long get_dscr(void)
44 {
45 	return mfspr(SPRN_DSCR_PRIV);
46 }
47 
48 inline void set_dscr(unsigned long val)
49 {
50 	mtspr(SPRN_DSCR_PRIV, val);
51 }
52 
53 /* Problem state DSCR access */
54 inline unsigned long get_dscr_usr(void)
55 {
56 	return mfspr(SPRN_DSCR);
57 }
58 
59 inline void set_dscr_usr(unsigned long val)
60 {
61 	mtspr(SPRN_DSCR, val);
62 }
63 
64 /* Default DSCR access */
65 unsigned long get_default_dscr(void)
66 {
67 	int err;
68 	unsigned long val;
69 
70 	err = read_ulong(DSCR_DEFAULT, &val, 16);
71 	if (err) {
72 		perror("read() failed");
73 		exit(1);
74 	}
75 	return val;
76 }
77 
78 void set_default_dscr(unsigned long val)
79 {
80 	int err;
81 
82 	err = write_ulong(DSCR_DEFAULT, val, 16);
83 	if (err) {
84 		perror("write() failed");
85 		exit(1);
86 	}
87 }
88 
89 double uniform_deviate(int seed)
90 {
91 	return seed * (1.0 / (RAND_MAX + 1.0));
92 }
93 #endif	/* _SELFTESTS_POWERPC_DSCR_DSCR_H */
94