coproc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * arm co-processors
  3. * CP15 (system control) is the one that gets used the most in practice.
  4. */
  5. #include "u.h"
  6. #include "../port/lib.h"
  7. #include "mem.h"
  8. #include "dat.h"
  9. #include "fns.h"
  10. #include "io.h"
  11. #include "arm.h"
  12. #define MAP2PCSPACE(va, pc) ((uintptr)(va) & ~KSEGM | (pc) & KSEGM)
  13. enum {
  14. /* alternates: 0xe12fff1e BX (R14); last e is R14 */
  15. /* 0xe28ef000 B 0(R14); second e is R14 (ken) */
  16. Retinst = 0xe1a0f00e, /* MOVW R14, R15 */
  17. Fpproc = 10, /* for vfp 3+; also 11 for doubles */
  18. };
  19. void
  20. cpwr(int cp, int op1, int crn, int crm, int op2, ulong val)
  21. {
  22. int s;
  23. volatile ulong instr[2];
  24. void *pcaddr;
  25. void (*fp)(ulong);
  26. s = splhi();
  27. op1 &= 7;
  28. op2 &= 7;
  29. crn &= 017;
  30. crm &= 017;
  31. cp &= 017;
  32. /* MCR. Rt will be R0. */
  33. instr[0] = 0xee000010 |
  34. op1 << 21 | crn << 16 | cp << 8 | op2 << 5 | crm;
  35. instr[1] = Retinst;
  36. coherence();
  37. pcaddr = (void *)MAP2PCSPACE(instr, getcallerpc(&cp));
  38. cachedwbse(pcaddr, sizeof instr);
  39. cacheiinv();
  40. fp = (void (*)(ulong))pcaddr;
  41. (*fp)(val);
  42. coherence();
  43. splx(s);
  44. }
  45. void
  46. cpwrsc(int op1, int crn, int crm, int op2, ulong val)
  47. {
  48. cpwr(CpSC, op1, crn, crm, op2, val);
  49. }
  50. ulong
  51. cprd(int cp, int op1, int crn, int crm, int op2)
  52. {
  53. int s;
  54. ulong res;
  55. volatile ulong instr[2];
  56. void *pcaddr;
  57. ulong (*fp)(void);
  58. s = splhi();
  59. op1 &= 7;
  60. op2 &= 7;
  61. crn &= 017;
  62. crm &= 017;
  63. /*
  64. * MRC. return value will be in R0, which is convenient.
  65. * Rt will be R0.
  66. */
  67. instr[0] = 0xee100010 |
  68. op1 << 21 | crn << 16 | cp << 8 | op2 << 5 | crm;
  69. instr[1] = Retinst;
  70. coherence();
  71. pcaddr = (void *)MAP2PCSPACE(instr, getcallerpc(&cp));
  72. cachedwbse(pcaddr, sizeof instr);
  73. cacheiinv();
  74. fp = (ulong (*)(void))pcaddr;
  75. res = (*fp)();
  76. splx(s);
  77. return res;
  78. }
  79. ulong
  80. cprdsc(int op1, int crn, int crm, int op2)
  81. {
  82. return cprd(CpSC, op1, crn, crm, op2);
  83. }
  84. /* floating point */
  85. ulong
  86. fprd(int fpreg)
  87. {
  88. int s;
  89. ulong res;
  90. volatile ulong instr[2];
  91. void *pcaddr;
  92. ulong (*fp)(void);
  93. s = splhi();
  94. fpreg &= 017;
  95. /*
  96. * VMRS. return value will be in R0, which is convenient.
  97. * Rt will be R0.
  98. */
  99. instr[0] = 0xeef00010 | fpreg << 16 | 0 << 12 | Fpproc << 8;
  100. instr[1] = Retinst;
  101. coherence();
  102. pcaddr = (void *)MAP2PCSPACE(instr, getcallerpc(&fpreg));
  103. cachedwbse(pcaddr, sizeof instr);
  104. cacheiinv();
  105. fp = (ulong (*)(void))pcaddr;
  106. res = (*fp)();
  107. splx(s);
  108. return res;
  109. }
  110. void
  111. fpwr(int fpreg, ulong val)
  112. {
  113. int s;
  114. volatile ulong instr[2];
  115. void *pcaddr;
  116. void (*fp)(ulong);
  117. s = splhi();
  118. fpreg &= 017;
  119. /* VMSR. Rt will be R0. */
  120. instr[0] = 0xeee00010 | fpreg << 16 | 0 << 12 | Fpproc << 8;
  121. instr[1] = Retinst;
  122. coherence();
  123. pcaddr = (void *)MAP2PCSPACE(instr, getcallerpc(&fpreg));
  124. cachedwbse(pcaddr, sizeof instr);
  125. cacheiinv();
  126. fp = (void (*)(ulong))pcaddr;
  127. (*fp)(val);
  128. coherence();
  129. splx(s);
  130. }