dscr.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include <unistd.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <fcntl.h>
  18. #include <dirent.h>
  19. #include <pthread.h>
  20. #include <sched.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <sys/wait.h>
  24. #include "utils.h"
  25. #define THREADS 100 /* Max threads */
  26. #define COUNT 100 /* Max iterations */
  27. #define DSCR_MAX 16 /* Max DSCR value */
  28. #define LEN_MAX 100 /* Max name length */
  29. #define DSCR_DEFAULT "/sys/devices/system/cpu/dscr_default"
  30. #define CPU_PATH "/sys/devices/system/cpu/"
  31. #define rmb() asm volatile("lwsync":::"memory")
  32. #define wmb() asm volatile("lwsync":::"memory")
  33. #define READ_ONCE(x) (*(volatile typeof(x) *)&(x))
  34. /* Prilvilege state DSCR access */
  35. inline unsigned long get_dscr(void)
  36. {
  37. unsigned long ret;
  38. asm volatile("mfspr %0,%1" : "=r" (ret) : "i" (SPRN_DSCR_PRIV));
  39. return ret;
  40. }
  41. inline void set_dscr(unsigned long val)
  42. {
  43. asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR_PRIV));
  44. }
  45. /* Problem state DSCR access */
  46. inline unsigned long get_dscr_usr(void)
  47. {
  48. unsigned long ret;
  49. asm volatile("mfspr %0,%1" : "=r" (ret) : "i" (SPRN_DSCR));
  50. return ret;
  51. }
  52. inline void set_dscr_usr(unsigned long val)
  53. {
  54. asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR));
  55. }
  56. /* Default DSCR access */
  57. unsigned long get_default_dscr(void)
  58. {
  59. int fd = -1, ret;
  60. char buf[16];
  61. unsigned long val;
  62. if (fd == -1) {
  63. fd = open(DSCR_DEFAULT, O_RDONLY);
  64. if (fd == -1) {
  65. perror("open() failed");
  66. exit(1);
  67. }
  68. }
  69. memset(buf, 0, sizeof(buf));
  70. lseek(fd, 0, SEEK_SET);
  71. ret = read(fd, buf, sizeof(buf));
  72. if (ret == -1) {
  73. perror("read() failed");
  74. exit(1);
  75. }
  76. sscanf(buf, "%lx", &val);
  77. close(fd);
  78. return val;
  79. }
  80. void set_default_dscr(unsigned long val)
  81. {
  82. int fd = -1, ret;
  83. char buf[16];
  84. if (fd == -1) {
  85. fd = open(DSCR_DEFAULT, O_RDWR);
  86. if (fd == -1) {
  87. perror("open() failed");
  88. exit(1);
  89. }
  90. }
  91. sprintf(buf, "%lx\n", val);
  92. ret = write(fd, buf, strlen(buf));
  93. if (ret == -1) {
  94. perror("write() failed");
  95. exit(1);
  96. }
  97. close(fd);
  98. }
  99. double uniform_deviate(int seed)
  100. {
  101. return seed * (1.0 / (RAND_MAX + 1.0));
  102. }
  103. #endif /* _SELFTESTS_POWERPC_DSCR_DSCR_H */