dscr.h 2.6 KB

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