spu_save.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * spu_save.c
  3. *
  4. * (C) Copyright IBM Corp. 2005
  5. *
  6. * SPU-side context save sequence outlined in
  7. * Synergistic Processor Element Book IV
  8. *
  9. * Author: Mark Nutter <mnutter@us.ibm.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. */
  26. #ifndef LS_SIZE
  27. #define LS_SIZE 0x40000 /* 256K (in bytes) */
  28. #endif
  29. typedef unsigned int u32;
  30. typedef unsigned long long u64;
  31. #include <spu_intrinsics.h>
  32. #include <asm/spu_csa.h>
  33. #include "spu_utils.h"
  34. static inline void save_event_mask(void)
  35. {
  36. unsigned int offset;
  37. /* Save, Step 2:
  38. * Read the SPU_RdEventMsk channel and save to the LSCSA.
  39. */
  40. offset = LSCSA_QW_OFFSET(event_mask);
  41. regs_spill[offset].slot[0] = spu_readch(SPU_RdEventMask);
  42. }
  43. static inline void save_tag_mask(void)
  44. {
  45. unsigned int offset;
  46. /* Save, Step 3:
  47. * Read the SPU_RdTagMsk channel and save to the LSCSA.
  48. */
  49. offset = LSCSA_QW_OFFSET(tag_mask);
  50. regs_spill[offset].slot[0] = spu_readch(MFC_RdTagMask);
  51. }
  52. static inline void save_upper_240kb(addr64 lscsa_ea)
  53. {
  54. unsigned int ls = 16384;
  55. unsigned int list = (unsigned int)&dma_list[0];
  56. unsigned int size = sizeof(dma_list);
  57. unsigned int tag_id = 0;
  58. unsigned int cmd = 0x24; /* PUTL */
  59. /* Save, Step 7:
  60. * Enqueue the PUTL command (tag 0) to the MFC SPU command
  61. * queue to transfer the remaining 240 kb of LS to CSA.
  62. */
  63. spu_writech(MFC_LSA, ls);
  64. spu_writech(MFC_EAH, lscsa_ea.ui[0]);
  65. spu_writech(MFC_EAL, list);
  66. spu_writech(MFC_Size, size);
  67. spu_writech(MFC_TagID, tag_id);
  68. spu_writech(MFC_Cmd, cmd);
  69. }
  70. static inline void save_fpcr(void)
  71. {
  72. // vector unsigned int fpcr;
  73. unsigned int offset;
  74. /* Save, Step 9:
  75. * Issue the floating-point status and control register
  76. * read instruction, and save to the LSCSA.
  77. */
  78. offset = LSCSA_QW_OFFSET(fpcr);
  79. regs_spill[offset].v = spu_mffpscr();
  80. }
  81. static inline void save_decr(void)
  82. {
  83. unsigned int offset;
  84. /* Save, Step 10:
  85. * Read and save the SPU_RdDec channel data to
  86. * the LSCSA.
  87. */
  88. offset = LSCSA_QW_OFFSET(decr);
  89. regs_spill[offset].slot[0] = spu_readch(SPU_RdDec);
  90. }
  91. static inline void save_srr0(void)
  92. {
  93. unsigned int offset;
  94. /* Save, Step 11:
  95. * Read and save the SPU_WSRR0 channel data to
  96. * the LSCSA.
  97. */
  98. offset = LSCSA_QW_OFFSET(srr0);
  99. regs_spill[offset].slot[0] = spu_readch(SPU_RdSRR0);
  100. }
  101. static inline void spill_regs_to_mem(addr64 lscsa_ea)
  102. {
  103. unsigned int ls = (unsigned int)&regs_spill[0];
  104. unsigned int size = sizeof(regs_spill);
  105. unsigned int tag_id = 0;
  106. unsigned int cmd = 0x20; /* PUT */
  107. /* Save, Step 13:
  108. * Enqueue a PUT command (tag 0) to send the LSCSA
  109. * to the CSA.
  110. */
  111. spu_writech(MFC_LSA, ls);
  112. spu_writech(MFC_EAH, lscsa_ea.ui[0]);
  113. spu_writech(MFC_EAL, lscsa_ea.ui[1]);
  114. spu_writech(MFC_Size, size);
  115. spu_writech(MFC_TagID, tag_id);
  116. spu_writech(MFC_Cmd, cmd);
  117. }
  118. static inline void enqueue_sync(addr64 lscsa_ea)
  119. {
  120. unsigned int tag_id = 0;
  121. unsigned int cmd = 0xCC;
  122. /* Save, Step 14:
  123. * Enqueue an MFC_SYNC command (tag 0).
  124. */
  125. spu_writech(MFC_TagID, tag_id);
  126. spu_writech(MFC_Cmd, cmd);
  127. }
  128. static inline void save_complete(void)
  129. {
  130. /* Save, Step 18:
  131. * Issue a stop-and-signal instruction indicating
  132. * "save complete". Note: This function will not
  133. * return!!
  134. */
  135. spu_stop(SPU_SAVE_COMPLETE);
  136. }
  137. /**
  138. * main - entry point for SPU-side context save.
  139. *
  140. * This code deviates from the documented sequence as follows:
  141. *
  142. * 1. The EA for LSCSA is passed from PPE in the
  143. * signal notification channels.
  144. * 2. All 128 registers are saved by crt0.o.
  145. */
  146. int main()
  147. {
  148. addr64 lscsa_ea;
  149. lscsa_ea.ui[0] = spu_readch(SPU_RdSigNotify1);
  150. lscsa_ea.ui[1] = spu_readch(SPU_RdSigNotify2);
  151. /* Step 1: done by exit(). */
  152. save_event_mask(); /* Step 2. */
  153. save_tag_mask(); /* Step 3. */
  154. set_event_mask(); /* Step 4. */
  155. set_tag_mask(); /* Step 5. */
  156. build_dma_list(lscsa_ea); /* Step 6. */
  157. save_upper_240kb(lscsa_ea); /* Step 7. */
  158. /* Step 8: done by exit(). */
  159. save_fpcr(); /* Step 9. */
  160. save_decr(); /* Step 10. */
  161. save_srr0(); /* Step 11. */
  162. enqueue_putllc(lscsa_ea); /* Step 12. */
  163. spill_regs_to_mem(lscsa_ea); /* Step 13. */
  164. enqueue_sync(lscsa_ea); /* Step 14. */
  165. set_tag_update(); /* Step 15. */
  166. read_tag_status(); /* Step 16. */
  167. read_llar_status(); /* Step 17. */
  168. save_complete(); /* Step 18. */
  169. return 0;
  170. }