dscr.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * POWER Data Stream Control Register (DSCR)
  3. *
  4. * This header file contains helper functions and macros
  5. * required for all the DSCR related test cases.
  6. *
  7. * Copyright 2012, Anton Blanchard, IBM Corporation.
  8. * Copyright 2015, Anshuman Khandual, IBM Corporation.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. */
  14. #ifndef _SELFTESTS_POWERPC_DSCR_DSCR_H
  15. #define _SELFTESTS_POWERPC_DSCR_DSCR_H
  16. #include <unistd.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <fcntl.h>
  21. #include <dirent.h>
  22. #include <pthread.h>
  23. #include <sched.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/wait.h>
  27. #include "utils.h"
  28. #define SPRN_DSCR 0x11 /* Privilege state SPR */
  29. #define SPRN_DSCR_USR 0x03 /* Problem state SPR */
  30. #define THREADS 100 /* Max threads */
  31. #define COUNT 100 /* Max iterations */
  32. #define DSCR_MAX 16 /* Max DSCR value */
  33. #define LEN_MAX 100 /* Max name length */
  34. #define DSCR_DEFAULT "/sys/devices/system/cpu/dscr_default"
  35. #define CPU_PATH "/sys/devices/system/cpu/"
  36. #define rmb() asm volatile("lwsync":::"memory")
  37. #define wmb() asm volatile("lwsync":::"memory")
  38. #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
  39. /* Prilvilege state DSCR access */
  40. inline unsigned long get_dscr(void)
  41. {
  42. unsigned long ret;
  43. asm volatile("mfspr %0,%1" : "=r" (ret): "i" (SPRN_DSCR));
  44. return ret;
  45. }
  46. inline void set_dscr(unsigned long val)
  47. {
  48. asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR));
  49. }
  50. /* Problem state DSCR access */
  51. inline unsigned long get_dscr_usr(void)
  52. {
  53. unsigned long ret;
  54. asm volatile("mfspr %0,%1" : "=r" (ret): "i" (SPRN_DSCR_USR));
  55. return ret;
  56. }
  57. inline void set_dscr_usr(unsigned long val)
  58. {
  59. asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR_USR));
  60. }
  61. /* Default DSCR access */
  62. unsigned long get_default_dscr(void)
  63. {
  64. int fd = -1, ret;
  65. char buf[16];
  66. unsigned long val;
  67. if (fd == -1) {
  68. fd = open(DSCR_DEFAULT, O_RDONLY);
  69. if (fd == -1) {
  70. perror("open() failed");
  71. exit(1);
  72. }
  73. }
  74. memset(buf, 0, sizeof(buf));
  75. lseek(fd, 0, SEEK_SET);
  76. ret = read(fd, buf, sizeof(buf));
  77. if (ret == -1) {
  78. perror("read() failed");
  79. exit(1);
  80. }
  81. sscanf(buf, "%lx", &val);
  82. close(fd);
  83. return val;
  84. }
  85. void set_default_dscr(unsigned long val)
  86. {
  87. int fd = -1, ret;
  88. char buf[16];
  89. if (fd == -1) {
  90. fd = open(DSCR_DEFAULT, O_RDWR);
  91. if (fd == -1) {
  92. perror("open() failed");
  93. exit(1);
  94. }
  95. }
  96. sprintf(buf, "%lx\n", val);
  97. ret = write(fd, buf, strlen(buf));
  98. if (ret == -1) {
  99. perror("write() failed");
  100. exit(1);
  101. }
  102. close(fd);
  103. }
  104. double uniform_deviate(int seed)
  105. {
  106. return seed * (1.0 / (RAND_MAX + 1.0));
  107. }
  108. #endif /* _SELFTESTS_POWERPC_DSCR_DSCR_H */