instructions.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _SELFTESTS_POWERPC_INSTRUCTIONS_H
  3. #define _SELFTESTS_POWERPC_INSTRUCTIONS_H
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. /* This defines the "copy" instruction from Power ISA 3.0 Book II, section 4.4. */
  7. #define __COPY(RA, RB, L) \
  8. (0x7c00060c | (RA) << (31-15) | (RB) << (31-20) | (L) << (31-10))
  9. #define COPY(RA, RB, L) \
  10. .long __COPY((RA), (RB), (L))
  11. static inline void copy(void *i)
  12. {
  13. asm volatile(str(COPY(0, %0, 0))";"
  14. :
  15. : "b" (i)
  16. : "memory"
  17. );
  18. }
  19. static inline void copy_first(void *i)
  20. {
  21. asm volatile(str(COPY(0, %0, 1))";"
  22. :
  23. : "b" (i)
  24. : "memory"
  25. );
  26. }
  27. /* This defines the "paste" instruction from Power ISA 3.0 Book II, section 4.4. */
  28. #define __PASTE(RA, RB, L, RC) \
  29. (0x7c00070c | (RA) << (31-15) | (RB) << (31-20) | (L) << (31-10) | (RC) << (31-31))
  30. #define PASTE(RA, RB, L, RC) \
  31. .long __PASTE((RA), (RB), (L), (RC))
  32. static inline int paste(void *i)
  33. {
  34. int cr;
  35. asm volatile(str(PASTE(0, %1, 0, 0))";"
  36. "mfcr %0;"
  37. : "=r" (cr)
  38. : "b" (i)
  39. : "memory"
  40. );
  41. return cr;
  42. }
  43. static inline int paste_last(void *i)
  44. {
  45. int cr;
  46. asm volatile(str(PASTE(0, %1, 1, 1))";"
  47. "mfcr %0;"
  48. : "=r" (cr)
  49. : "b" (i)
  50. : "memory"
  51. );
  52. return cr;
  53. }
  54. #define PPC_INST_COPY __COPY(0, 0, 0)
  55. #define PPC_INST_COPY_FIRST __COPY(0, 0, 1)
  56. #define PPC_INST_PASTE __PASTE(0, 0, 0, 0)
  57. #define PPC_INST_PASTE_LAST __PASTE(0, 0, 1, 1)
  58. /* This defines the prefixed load/store instructions */
  59. #ifdef __ASSEMBLY__
  60. # define stringify_in_c(...) __VA_ARGS__
  61. #else
  62. # define __stringify_in_c(...) #__VA_ARGS__
  63. # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
  64. #endif
  65. #define __PPC_RA(a) (((a) & 0x1f) << 16)
  66. #define __PPC_RS(s) (((s) & 0x1f) << 21)
  67. #define __PPC_RT(t) __PPC_RS(t)
  68. #define __PPC_PREFIX_R(r) (((r) & 0x1) << 20)
  69. #define PPC_PREFIX_MLS 0x06000000
  70. #define PPC_PREFIX_8LS 0x04000000
  71. #define PPC_INST_LBZ 0x88000000
  72. #define PPC_INST_LHZ 0xa0000000
  73. #define PPC_INST_LHA 0xa8000000
  74. #define PPC_INST_LWZ 0x80000000
  75. #define PPC_INST_STB 0x98000000
  76. #define PPC_INST_STH 0xb0000000
  77. #define PPC_INST_STW 0x90000000
  78. #define PPC_INST_STD 0xf8000000
  79. #define PPC_INST_LFS 0xc0000000
  80. #define PPC_INST_LFD 0xc8000000
  81. #define PPC_INST_STFS 0xd0000000
  82. #define PPC_INST_STFD 0xd8000000
  83. #define PREFIX_MLS(instr, t, a, r, d) stringify_in_c(.balign 64, , 4;) \
  84. stringify_in_c(.long PPC_PREFIX_MLS | \
  85. __PPC_PREFIX_R(r) | \
  86. (((d) >> 16) & 0x3ffff);) \
  87. stringify_in_c(.long (instr) | \
  88. __PPC_RT(t) | \
  89. __PPC_RA(a) | \
  90. ((d) & 0xffff);\n)
  91. #define PREFIX_8LS(instr, t, a, r, d) stringify_in_c(.balign 64, , 4;) \
  92. stringify_in_c(.long PPC_PREFIX_8LS | \
  93. __PPC_PREFIX_R(r) | \
  94. (((d) >> 16) & 0x3ffff);) \
  95. stringify_in_c(.long (instr) | \
  96. __PPC_RT(t) | \
  97. __PPC_RA(a) | \
  98. ((d) & 0xffff);\n)
  99. /* Prefixed Integer Load/Store instructions */
  100. #define PLBZ(t, a, r, d) PREFIX_MLS(PPC_INST_LBZ, t, a, r, d)
  101. #define PLHZ(t, a, r, d) PREFIX_MLS(PPC_INST_LHZ, t, a, r, d)
  102. #define PLHA(t, a, r, d) PREFIX_MLS(PPC_INST_LHA, t, a, r, d)
  103. #define PLWZ(t, a, r, d) PREFIX_MLS(PPC_INST_LWZ, t, a, r, d)
  104. #define PLWA(t, a, r, d) PREFIX_8LS(0xa4000000, t, a, r, d)
  105. #define PLD(t, a, r, d) PREFIX_8LS(0xe4000000, t, a, r, d)
  106. #define PLQ(t, a, r, d) PREFIX_8LS(0xe0000000, t, a, r, d)
  107. #define PSTB(s, a, r, d) PREFIX_MLS(PPC_INST_STB, s, a, r, d)
  108. #define PSTH(s, a, r, d) PREFIX_MLS(PPC_INST_STH, s, a, r, d)
  109. #define PSTW(s, a, r, d) PREFIX_MLS(PPC_INST_STW, s, a, r, d)
  110. #define PSTD(s, a, r, d) PREFIX_8LS(0xf4000000, s, a, r, d)
  111. #define PSTQ(s, a, r, d) PREFIX_8LS(0xf0000000, s, a, r, d)
  112. /* Prefixed Floating-Point Load/Store Instructions */
  113. #define PLFS(frt, a, r, d) PREFIX_MLS(PPC_INST_LFS, frt, a, r, d)
  114. #define PLFD(frt, a, r, d) PREFIX_MLS(PPC_INST_LFD, frt, a, r, d)
  115. #define PSTFS(frs, a, r, d) PREFIX_MLS(PPC_INST_STFS, frs, a, r, d)
  116. #define PSTFD(frs, a, r, d) PREFIX_MLS(PPC_INST_STFD, frs, a, r, d)
  117. /* Prefixed VSX Load/Store Instructions */
  118. #define PLXSD(vrt, a, r, d) PREFIX_8LS(0xa8000000, vrt, a, r, d)
  119. #define PLXSSP(vrt, a, r, d) PREFIX_8LS(0xac000000, vrt, a, r, d)
  120. #define PLXV0(s, a, r, d) PREFIX_8LS(0xc8000000, s, a, r, d)
  121. #define PLXV1(s, a, r, d) PREFIX_8LS(0xcc000000, s, a, r, d)
  122. #define PSTXSD(vrs, a, r, d) PREFIX_8LS(0xb8000000, vrs, a, r, d)
  123. #define PSTXSSP(vrs, a, r, d) PREFIX_8LS(0xbc000000, vrs, a, r, d)
  124. #define PSTXV0(s, a, r, d) PREFIX_8LS(0xd8000000, s, a, r, d)
  125. #define PSTXV1(s, a, r, d) PREFIX_8LS(0xdc000000, s, a, r, d)
  126. #endif /* _SELFTESTS_POWERPC_INSTRUCTIONS_H */