Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * POWER Data Stream Control Register (DSCR)
0004  *
0005  * This header file contains helper functions and macros
0006  * required for all the DSCR related test cases.
0007  *
0008  * Copyright 2012, Anton Blanchard, IBM Corporation.
0009  * Copyright 2015, Anshuman Khandual, IBM Corporation.
0010  */
0011 #ifndef _SELFTESTS_POWERPC_DSCR_DSCR_H
0012 #define _SELFTESTS_POWERPC_DSCR_DSCR_H
0013 
0014 #include <unistd.h>
0015 #include <stdio.h>
0016 #include <stdlib.h>
0017 #include <string.h>
0018 #include <fcntl.h>
0019 #include <dirent.h>
0020 #include <pthread.h>
0021 #include <sched.h>
0022 #include <sys/types.h>
0023 #include <sys/stat.h>
0024 #include <sys/wait.h>
0025 
0026 #include "utils.h"
0027 
0028 #define THREADS     100 /* Max threads */
0029 #define COUNT       100 /* Max iterations */
0030 #define DSCR_MAX    16  /* Max DSCR value */
0031 #define LEN_MAX     100 /* Max name length */
0032 
0033 #define DSCR_DEFAULT    "/sys/devices/system/cpu/dscr_default"
0034 #define CPU_PATH    "/sys/devices/system/cpu/"
0035 
0036 #define rmb()  asm volatile("lwsync":::"memory")
0037 #define wmb()  asm volatile("lwsync":::"memory")
0038 
0039 #define READ_ONCE(x) (*(volatile typeof(x) *)&(x))
0040 
0041 /* Prilvilege state DSCR access */
0042 inline unsigned long get_dscr(void)
0043 {
0044     unsigned long ret;
0045 
0046     asm volatile("mfspr %0,%1" : "=r" (ret) : "i" (SPRN_DSCR_PRIV));
0047 
0048     return ret;
0049 }
0050 
0051 inline void set_dscr(unsigned long val)
0052 {
0053     asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR_PRIV));
0054 }
0055 
0056 /* Problem state DSCR access */
0057 inline unsigned long get_dscr_usr(void)
0058 {
0059     unsigned long ret;
0060 
0061     asm volatile("mfspr %0,%1" : "=r" (ret) : "i" (SPRN_DSCR));
0062 
0063     return ret;
0064 }
0065 
0066 inline void set_dscr_usr(unsigned long val)
0067 {
0068     asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR));
0069 }
0070 
0071 /* Default DSCR access */
0072 unsigned long get_default_dscr(void)
0073 {
0074     int fd = -1, ret;
0075     char buf[16];
0076     unsigned long val;
0077 
0078     if (fd == -1) {
0079         fd = open(DSCR_DEFAULT, O_RDONLY);
0080         if (fd == -1) {
0081             perror("open() failed");
0082             exit(1);
0083         }
0084     }
0085     memset(buf, 0, sizeof(buf));
0086     lseek(fd, 0, SEEK_SET);
0087     ret = read(fd, buf, sizeof(buf));
0088     if (ret == -1) {
0089         perror("read() failed");
0090         exit(1);
0091     }
0092     sscanf(buf, "%lx", &val);
0093     close(fd);
0094     return val;
0095 }
0096 
0097 void set_default_dscr(unsigned long val)
0098 {
0099     int fd = -1, ret;
0100     char buf[16];
0101 
0102     if (fd == -1) {
0103         fd = open(DSCR_DEFAULT, O_RDWR);
0104         if (fd == -1) {
0105             perror("open() failed");
0106             exit(1);
0107         }
0108     }
0109     sprintf(buf, "%lx\n", val);
0110     ret = write(fd, buf, strlen(buf));
0111     if (ret == -1) {
0112         perror("write() failed");
0113         exit(1);
0114     }
0115     close(fd);
0116 }
0117 
0118 double uniform_deviate(int seed)
0119 {
0120     return seed * (1.0 / (RAND_MAX + 1.0));
0121 }
0122 #endif  /* _SELFTESTS_POWERPC_DSCR_DSCR_H */