altivec.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*-
  2. * SPDX-License-Identifier: BSD-4-Clause
  3. *
  4. * Copyright (C) 1996 Wolfgang Solfrank.
  5. * Copyright (C) 1996 TooLs GmbH.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. * must display the following acknowledgement:
  18. * This product includes software developed by TooLs GmbH.
  19. * 4. The name of TooLs GmbH may not be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
  23. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  28. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. * $NetBSD: fpu.c,v 1.5 2001/07/22 11:29:46 wiz Exp $
  34. */
  35. #include <sys/cdefs.h>
  36. __FBSDID("$FreeBSD$");
  37. #include <sys/param.h>
  38. #include <sys/proc.h>
  39. #include <sys/systm.h>
  40. #include <sys/limits.h>
  41. #include <machine/altivec.h>
  42. #include <machine/pcb.h>
  43. #include <machine/psl.h>
  44. static void
  45. save_vec_int(struct thread *td)
  46. {
  47. int msr;
  48. struct pcb *pcb;
  49. pcb = td->td_pcb;
  50. /*
  51. * Temporarily re-enable the vector unit during the save
  52. */
  53. msr = mfmsr();
  54. mtmsr(msr | PSL_VEC);
  55. /*
  56. * Save the vector registers and VSCR to the PCB
  57. */
  58. #define STVX(n) __asm ("stvx %1,0,%0" \
  59. :: "b"(pcb->pcb_vec.vr[n]), "n"(n));
  60. STVX(0); STVX(1); STVX(2); STVX(3);
  61. STVX(4); STVX(5); STVX(6); STVX(7);
  62. STVX(8); STVX(9); STVX(10); STVX(11);
  63. STVX(12); STVX(13); STVX(14); STVX(15);
  64. STVX(16); STVX(17); STVX(18); STVX(19);
  65. STVX(20); STVX(21); STVX(22); STVX(23);
  66. STVX(24); STVX(25); STVX(26); STVX(27);
  67. STVX(28); STVX(29); STVX(30); STVX(31);
  68. #undef STVX
  69. __asm __volatile("mfvscr 0; stvewx 0,0,%0" :: "b"(&pcb->pcb_vec.vscr));
  70. /*
  71. * Disable vector unit again
  72. */
  73. isync();
  74. mtmsr(msr);
  75. }
  76. void
  77. enable_vec(struct thread *td)
  78. {
  79. int msr;
  80. struct pcb *pcb;
  81. struct trapframe *tf;
  82. pcb = td->td_pcb;
  83. tf = trapframe(td);
  84. /*
  85. * Save the thread's Altivec CPU number, and set the CPU's current
  86. * vector thread
  87. */
  88. td->td_pcb->pcb_veccpu = PCPU_GET(cpuid);
  89. PCPU_SET(vecthread, td);
  90. /*
  91. * Enable the vector unit for when the thread returns from the
  92. * exception. If this is the first time the unit has been used by
  93. * the thread, initialise the vector registers and VSCR to 0, and
  94. * set the flag to indicate that the vector unit is in use.
  95. */
  96. tf->srr1 |= PSL_VEC;
  97. if (!(pcb->pcb_flags & PCB_VEC)) {
  98. memset(&pcb->pcb_vec, 0, sizeof pcb->pcb_vec);
  99. pcb->pcb_flags |= PCB_VEC;
  100. }
  101. /*
  102. * Temporarily enable the vector unit so the registers
  103. * can be restored.
  104. */
  105. msr = mfmsr();
  106. mtmsr(msr | PSL_VEC);
  107. /*
  108. * Restore VSCR by first loading it into a vector and then into VSCR.
  109. * (this needs to done before loading the user's vector registers
  110. * since we need to use a scratch vector register)
  111. */
  112. __asm __volatile("vxor 0,0,0; lvewx 0,0,%0; mtvscr 0" \
  113. :: "b"(&pcb->pcb_vec.vscr));
  114. #define LVX(n) __asm ("lvx " #n ",0,%0" \
  115. :: "b"(&pcb->pcb_vec.vr[n]));
  116. LVX(0); LVX(1); LVX(2); LVX(3);
  117. LVX(4); LVX(5); LVX(6); LVX(7);
  118. LVX(8); LVX(9); LVX(10); LVX(11);
  119. LVX(12); LVX(13); LVX(14); LVX(15);
  120. LVX(16); LVX(17); LVX(18); LVX(19);
  121. LVX(20); LVX(21); LVX(22); LVX(23);
  122. LVX(24); LVX(25); LVX(26); LVX(27);
  123. LVX(28); LVX(29); LVX(30); LVX(31);
  124. #undef LVX
  125. isync();
  126. mtmsr(msr);
  127. }
  128. void
  129. save_vec(struct thread *td)
  130. {
  131. struct pcb *pcb;
  132. pcb = td->td_pcb;
  133. save_vec_int(td);
  134. /*
  135. * Clear the current vec thread and pcb's CPU id
  136. * XXX should this be left clear to allow lazy save/restore ?
  137. */
  138. pcb->pcb_veccpu = INT_MAX;
  139. PCPU_SET(vecthread, NULL);
  140. }
  141. /*
  142. * Save altivec state without dropping ownership. This will only save state if
  143. * the current vector-thread is `td'.
  144. */
  145. void
  146. save_vec_nodrop(struct thread *td)
  147. {
  148. if (td == PCPU_GET(vecthread))
  149. save_vec_int(td);
  150. }