coproc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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, /* MOV R14, R15 */
  17. };
  18. void
  19. cpwr(int cp, int op1, int crn, int crm, int op2, ulong val)
  20. {
  21. int s;
  22. volatile ulong instr[2];
  23. void *pcaddr;
  24. void (*fp)(ulong);
  25. s = splhi();
  26. op1 &= 7;
  27. op2 &= 7;
  28. crn &= 017;
  29. crm &= 017;
  30. cp &= 017;
  31. /* MCR. Rt will be R0. */
  32. instr[0] = 0xee000010 |
  33. op1 << 21 | crn << 16 | cp << 8 | op2 << 5 | crm;
  34. instr[1] = Retinst;
  35. coherence();
  36. pcaddr = (void *)MAP2PCSPACE(instr, getcallerpc(&cp));
  37. cachedwbse(pcaddr, sizeof instr);
  38. cacheiinv();
  39. fp = (void (*)(ulong))pcaddr;
  40. (*fp)(val);
  41. coherence();
  42. splx(s);
  43. }
  44. void
  45. cpwrsc(int op1, int crn, int crm, int op2, ulong val)
  46. {
  47. cpwr(CpSC, op1, crn, crm, op2, val);
  48. }
  49. ulong
  50. cprd(int cp, int op1, int crn, int crm, int op2)
  51. {
  52. int s;
  53. ulong res;
  54. volatile ulong instr[2];
  55. void *pcaddr;
  56. ulong (*fp)(void);
  57. s = splhi();
  58. op1 &= 7;
  59. op2 &= 7;
  60. crn &= 017;
  61. crm &= 017;
  62. /*
  63. * MRC. return value will be in R0, which is convenient.
  64. * Rt will be R0.
  65. */
  66. instr[0] = 0xee100010 |
  67. op1 << 21 | crn << 16 | cp << 8 | op2 << 5 | crm;
  68. instr[1] = Retinst;
  69. coherence();
  70. pcaddr = (void *)MAP2PCSPACE(instr, getcallerpc(&cp));
  71. cachedwbse(pcaddr, sizeof instr);
  72. cacheiinv();
  73. fp = (ulong (*)(void))pcaddr;
  74. res = (*fp)();
  75. splx(s);
  76. return res;
  77. }
  78. ulong
  79. cprdsc(int op1, int crn, int crm, int op2)
  80. {
  81. return cprd(CpSC, op1, crn, crm, op2);
  82. }
  83. /* floating point */
  84. ulong
  85. fprd(int fpreg)
  86. {
  87. int s;
  88. ulong res;
  89. volatile ulong instr[2];
  90. void *pcaddr;
  91. ulong (*fp)(void);
  92. s = splhi();
  93. fpreg &= 017;
  94. /*
  95. * VMRS. return value will be in R0, which is convenient.
  96. * Rt will be R0.
  97. */
  98. instr[0] = 0xeef00a10 | fpreg << 16 | 0 << 12;
  99. instr[1] = Retinst;
  100. coherence();
  101. pcaddr = (void *)MAP2PCSPACE(instr, getcallerpc(&fpreg));
  102. cachedwbse(pcaddr, sizeof instr);
  103. cacheiinv();
  104. fp = (ulong (*)(void))pcaddr;
  105. res = (*fp)();
  106. splx(s);
  107. return res;
  108. }
  109. void
  110. fpwr(int fpreg, ulong val)
  111. {
  112. int s;
  113. volatile ulong instr[2];
  114. void *pcaddr;
  115. void (*fp)(ulong);
  116. s = splhi();
  117. fpreg &= 017;
  118. /* VMSR. Rt will be R0. */
  119. instr[0] = 0xeee00a10 | fpreg << 16 | 0 << 12;
  120. instr[1] = Retinst;
  121. coherence();
  122. pcaddr = (void *)MAP2PCSPACE(instr, getcallerpc(&fpreg));
  123. cachedwbse(pcaddr, sizeof instr);
  124. cacheiinv();
  125. fp = (void (*)(ulong))pcaddr;
  126. (*fp)(val);
  127. coherence();
  128. splx(s);
  129. }