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 fd = -1, ret; 68 char buf[16]; 69 unsigned long val; 70 71 if (fd == -1) { 72 fd = open(DSCR_DEFAULT, O_RDONLY); 73 if (fd == -1) { 74 perror("open() failed"); 75 exit(1); 76 } 77 } 78 memset(buf, 0, sizeof(buf)); 79 lseek(fd, 0, SEEK_SET); 80 ret = read(fd, buf, sizeof(buf)); 81 if (ret == -1) { 82 perror("read() failed"); 83 exit(1); 84 } 85 sscanf(buf, "%lx", &val); 86 close(fd); 87 return val; 88 } 89 90 void set_default_dscr(unsigned long val) 91 { 92 int fd = -1, ret; 93 char buf[16]; 94 95 if (fd == -1) { 96 fd = open(DSCR_DEFAULT, O_RDWR); 97 if (fd == -1) { 98 perror("open() failed"); 99 exit(1); 100 } 101 } 102 sprintf(buf, "%lx\n", val); 103 ret = write(fd, buf, strlen(buf)); 104 if (ret == -1) { 105 perror("write() failed"); 106 exit(1); 107 } 108 close(fd); 109 } 110 111 double uniform_deviate(int seed) 112 { 113 return seed * (1.0 / (RAND_MAX + 1.0)); 114 } 115 #endif /* _SELFTESTS_POWERPC_DSCR_DSCR_H */ 116