cwsr_trap_handler_gfx8.asm 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /*
  2. * Copyright 2015-2017 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. /* To compile this assembly code:
  23. * PROJECT=vi ./sp3 cwsr_trap_handler_gfx8.asm -hex tmp.hex
  24. */
  25. /* HW (VI) source code for CWSR trap handler */
  26. /* Version 18 + multiple trap handler */
  27. // this performance-optimal version was originally from Seven Xu at SRDC
  28. // Revison #18 --...
  29. /* Rev History
  30. ** #1. Branch from gc dv. //gfxip/gfx8/main/src/test/suites/block/cs/sr/cs_trap_handler.sp3#1,#50, #51, #52-53(Skip, Already Fixed by PV), #54-56(merged),#57-58(mergerd, skiped-already fixed by PV)
  31. ** #4. SR Memory Layout:
  32. ** 1. VGPR-SGPR-HWREG-{LDS}
  33. ** 2. tba_hi.bits.26 - reconfigured as the first wave in tg bits, for defer Save LDS for a threadgroup.. performance concern..
  34. ** #5. Update: 1. Accurate g8sr_ts_save_d timestamp
  35. ** #6. Update: 1. Fix s_barrier usage; 2. VGPR s/r using swizzle buffer?(NoNeed, already matched the swizzle pattern, more investigation)
  36. ** #7. Update: 1. don't barrier if noLDS
  37. ** #8. Branch: 1. Branch to ver#0, which is very similar to gc dv version
  38. ** 2. Fix SQ issue by s_sleep 2
  39. ** #9. Update: 1. Fix scc restore failed issue, restore wave_status at last
  40. ** 2. optimize s_buffer save by burst 16sgprs...
  41. ** #10. Update 1. Optimize restore sgpr by busrt 16 sgprs.
  42. ** #11. Update 1. Add 2 more timestamp for debug version
  43. ** #12. Update 1. Add VGPR SR using DWx4, some case improve and some case drop performance
  44. ** #13. Integ 1. Always use MUBUF for PV trap shader...
  45. ** #14. Update 1. s_buffer_store soft clause...
  46. ** #15. Update 1. PERF - sclar write with glc:0/mtype0 to allow L2 combine. perf improvement a lot.
  47. ** #16. Update 1. PRRF - UNROLL LDS_DMA got 2500cycle save in IP tree
  48. ** #17. Update 1. FUNC - LDS_DMA has issues while ATC, replace with ds_read/buffer_store for save part[TODO restore part]
  49. ** 2. PERF - Save LDS before save VGPR to cover LDS save long latency...
  50. ** #18. Update 1. FUNC - Implicitly estore STATUS.VCCZ, which is not writable by s_setreg_b32
  51. ** 2. FUNC - Handle non-CWSR traps
  52. */
  53. var G8SR_WDMEM_HWREG_OFFSET = 0
  54. var G8SR_WDMEM_SGPR_OFFSET = 128 // in bytes
  55. // Keep definition same as the app shader, These 2 time stamps are part of the app shader... Should before any Save and after restore.
  56. var G8SR_DEBUG_TIMESTAMP = 0
  57. var G8SR_DEBUG_TS_SAVE_D_OFFSET = 40*4 // ts_save_d timestamp offset relative to SGPR_SR_memory_offset
  58. var s_g8sr_ts_save_s = s[34:35] // save start
  59. var s_g8sr_ts_sq_save_msg = s[36:37] // The save shader send SAVEWAVE msg to spi
  60. var s_g8sr_ts_spi_wrexec = s[38:39] // the SPI write the sr address to SQ
  61. var s_g8sr_ts_save_d = s[40:41] // save end
  62. var s_g8sr_ts_restore_s = s[42:43] // restore start
  63. var s_g8sr_ts_restore_d = s[44:45] // restore end
  64. var G8SR_VGPR_SR_IN_DWX4 = 0
  65. var G8SR_SAVE_BUF_RSRC_WORD1_STRIDE_DWx4 = 0x00100000 // DWx4 stride is 4*4Bytes
  66. var G8SR_RESTORE_BUF_RSRC_WORD1_STRIDE_DWx4 = G8SR_SAVE_BUF_RSRC_WORD1_STRIDE_DWx4
  67. /*************************************************************************/
  68. /* control on how to run the shader */
  69. /*************************************************************************/
  70. //any hack that needs to be made to run this code in EMU (either because various EMU code are not ready or no compute save & restore in EMU run)
  71. var EMU_RUN_HACK = 0
  72. var EMU_RUN_HACK_RESTORE_NORMAL = 0
  73. var EMU_RUN_HACK_SAVE_NORMAL_EXIT = 0
  74. var EMU_RUN_HACK_SAVE_SINGLE_WAVE = 0
  75. var EMU_RUN_HACK_SAVE_FIRST_TIME = 0 //for interrupted restore in which the first save is through EMU_RUN_HACK
  76. var EMU_RUN_HACK_SAVE_FIRST_TIME_TBA_LO = 0 //for interrupted restore in which the first save is through EMU_RUN_HACK
  77. var EMU_RUN_HACK_SAVE_FIRST_TIME_TBA_HI = 0 //for interrupted restore in which the first save is through EMU_RUN_HACK
  78. var SAVE_LDS = 1
  79. var WG_BASE_ADDR_LO = 0x9000a000
  80. var WG_BASE_ADDR_HI = 0x0
  81. var WAVE_SPACE = 0x5000 //memory size that each wave occupies in workgroup state mem
  82. var CTX_SAVE_CONTROL = 0x0
  83. var CTX_RESTORE_CONTROL = CTX_SAVE_CONTROL
  84. var SIM_RUN_HACK = 0 //any hack that needs to be made to run this code in SIM (either because various RTL code are not ready or no compute save & restore in RTL run)
  85. var SGPR_SAVE_USE_SQC = 1 //use SQC D$ to do the write
  86. var USE_MTBUF_INSTEAD_OF_MUBUF = 0 //because TC EMU currently asserts on 0 of // overload DFMT field to carry 4 more bits of stride for MUBUF opcodes
  87. var SWIZZLE_EN = 0 //whether we use swizzled buffer addressing
  88. /**************************************************************************/
  89. /* variables */
  90. /**************************************************************************/
  91. var SQ_WAVE_STATUS_INST_ATC_SHIFT = 23
  92. var SQ_WAVE_STATUS_INST_ATC_MASK = 0x00800000
  93. var SQ_WAVE_STATUS_SPI_PRIO_SHIFT = 1
  94. var SQ_WAVE_STATUS_SPI_PRIO_MASK = 0x00000006
  95. var SQ_WAVE_STATUS_PRE_SPI_PRIO_SHIFT = 0
  96. var SQ_WAVE_STATUS_PRE_SPI_PRIO_SIZE = 1
  97. var SQ_WAVE_STATUS_POST_SPI_PRIO_SHIFT = 3
  98. var SQ_WAVE_STATUS_POST_SPI_PRIO_SIZE = 29
  99. var SQ_WAVE_LDS_ALLOC_LDS_SIZE_SHIFT = 12
  100. var SQ_WAVE_LDS_ALLOC_LDS_SIZE_SIZE = 9
  101. var SQ_WAVE_GPR_ALLOC_VGPR_SIZE_SHIFT = 8
  102. var SQ_WAVE_GPR_ALLOC_VGPR_SIZE_SIZE = 6
  103. var SQ_WAVE_GPR_ALLOC_SGPR_SIZE_SHIFT = 24
  104. var SQ_WAVE_GPR_ALLOC_SGPR_SIZE_SIZE = 3 //FIXME sq.blk still has 4 bits at this time while SQ programming guide has 3 bits
  105. var SQ_WAVE_TRAPSTS_SAVECTX_MASK = 0x400
  106. var SQ_WAVE_TRAPSTS_EXCE_MASK = 0x1FF // Exception mask
  107. var SQ_WAVE_TRAPSTS_SAVECTX_SHIFT = 10
  108. var SQ_WAVE_TRAPSTS_MEM_VIOL_MASK = 0x100
  109. var SQ_WAVE_TRAPSTS_MEM_VIOL_SHIFT = 8
  110. var SQ_WAVE_TRAPSTS_PRE_SAVECTX_MASK = 0x3FF
  111. var SQ_WAVE_TRAPSTS_PRE_SAVECTX_SHIFT = 0x0
  112. var SQ_WAVE_TRAPSTS_PRE_SAVECTX_SIZE = 10
  113. var SQ_WAVE_TRAPSTS_POST_SAVECTX_MASK = 0xFFFFF800
  114. var SQ_WAVE_TRAPSTS_POST_SAVECTX_SHIFT = 11
  115. var SQ_WAVE_TRAPSTS_POST_SAVECTX_SIZE = 21
  116. var SQ_WAVE_IB_STS_RCNT_SHIFT = 16 //FIXME
  117. var SQ_WAVE_IB_STS_RCNT_SIZE = 4 //FIXME
  118. var SQ_WAVE_IB_STS_FIRST_REPLAY_SHIFT = 15 //FIXME
  119. var SQ_WAVE_IB_STS_FIRST_REPLAY_SIZE = 1 //FIXME
  120. var SQ_WAVE_IB_STS_RCNT_FIRST_REPLAY_MASK_NEG = 0x00007FFF //FIXME
  121. var SQ_BUF_RSRC_WORD1_ATC_SHIFT = 24
  122. var SQ_BUF_RSRC_WORD3_MTYPE_SHIFT = 27
  123. /* Save */
  124. var S_SAVE_BUF_RSRC_WORD1_STRIDE = 0x00040000 //stride is 4 bytes
  125. var S_SAVE_BUF_RSRC_WORD3_MISC = 0x00807FAC //SQ_SEL_X/Y/Z/W, BUF_NUM_FORMAT_FLOAT, (0 for MUBUF stride[17:14] when ADD_TID_ENABLE and BUF_DATA_FORMAT_32 for MTBUF), ADD_TID_ENABLE
  126. var S_SAVE_SPI_INIT_ATC_MASK = 0x08000000 //bit[27]: ATC bit
  127. var S_SAVE_SPI_INIT_ATC_SHIFT = 27
  128. var S_SAVE_SPI_INIT_MTYPE_MASK = 0x70000000 //bit[30:28]: Mtype
  129. var S_SAVE_SPI_INIT_MTYPE_SHIFT = 28
  130. var S_SAVE_SPI_INIT_FIRST_WAVE_MASK = 0x04000000 //bit[26]: FirstWaveInTG
  131. var S_SAVE_SPI_INIT_FIRST_WAVE_SHIFT = 26
  132. var S_SAVE_PC_HI_RCNT_SHIFT = 28 //FIXME check with Brian to ensure all fields other than PC[47:0] can be used
  133. var S_SAVE_PC_HI_RCNT_MASK = 0xF0000000 //FIXME
  134. var S_SAVE_PC_HI_FIRST_REPLAY_SHIFT = 27 //FIXME
  135. var S_SAVE_PC_HI_FIRST_REPLAY_MASK = 0x08000000 //FIXME
  136. var s_save_spi_init_lo = exec_lo
  137. var s_save_spi_init_hi = exec_hi
  138. //tba_lo and tba_hi need to be saved/restored
  139. var s_save_pc_lo = ttmp0 //{TTMP1, TTMP0} = {3'h0,pc_rewind[3:0], HT[0],trapID[7:0], PC[47:0]}
  140. var s_save_pc_hi = ttmp1
  141. var s_save_exec_lo = ttmp2
  142. var s_save_exec_hi = ttmp3
  143. var s_save_status = ttmp4
  144. var s_save_trapsts = ttmp5 //not really used until the end of the SAVE routine
  145. var s_save_xnack_mask_lo = ttmp6
  146. var s_save_xnack_mask_hi = ttmp7
  147. var s_save_buf_rsrc0 = ttmp8
  148. var s_save_buf_rsrc1 = ttmp9
  149. var s_save_buf_rsrc2 = ttmp10
  150. var s_save_buf_rsrc3 = ttmp11
  151. var s_save_mem_offset = tma_lo
  152. var s_save_alloc_size = s_save_trapsts //conflict
  153. var s_save_tmp = s_save_buf_rsrc2 //shared with s_save_buf_rsrc2 (conflict: should not use mem access with s_save_tmp at the same time)
  154. var s_save_m0 = tma_hi
  155. /* Restore */
  156. var S_RESTORE_BUF_RSRC_WORD1_STRIDE = S_SAVE_BUF_RSRC_WORD1_STRIDE
  157. var S_RESTORE_BUF_RSRC_WORD3_MISC = S_SAVE_BUF_RSRC_WORD3_MISC
  158. var S_RESTORE_SPI_INIT_ATC_MASK = 0x08000000 //bit[27]: ATC bit
  159. var S_RESTORE_SPI_INIT_ATC_SHIFT = 27
  160. var S_RESTORE_SPI_INIT_MTYPE_MASK = 0x70000000 //bit[30:28]: Mtype
  161. var S_RESTORE_SPI_INIT_MTYPE_SHIFT = 28
  162. var S_RESTORE_SPI_INIT_FIRST_WAVE_MASK = 0x04000000 //bit[26]: FirstWaveInTG
  163. var S_RESTORE_SPI_INIT_FIRST_WAVE_SHIFT = 26
  164. var S_RESTORE_PC_HI_RCNT_SHIFT = S_SAVE_PC_HI_RCNT_SHIFT
  165. var S_RESTORE_PC_HI_RCNT_MASK = S_SAVE_PC_HI_RCNT_MASK
  166. var S_RESTORE_PC_HI_FIRST_REPLAY_SHIFT = S_SAVE_PC_HI_FIRST_REPLAY_SHIFT
  167. var S_RESTORE_PC_HI_FIRST_REPLAY_MASK = S_SAVE_PC_HI_FIRST_REPLAY_MASK
  168. var s_restore_spi_init_lo = exec_lo
  169. var s_restore_spi_init_hi = exec_hi
  170. var s_restore_mem_offset = ttmp2
  171. var s_restore_alloc_size = ttmp3
  172. var s_restore_tmp = ttmp6 //tba_lo/hi need to be restored
  173. var s_restore_mem_offset_save = s_restore_tmp //no conflict
  174. var s_restore_m0 = s_restore_alloc_size //no conflict
  175. var s_restore_mode = ttmp7
  176. var s_restore_pc_lo = ttmp0
  177. var s_restore_pc_hi = ttmp1
  178. var s_restore_exec_lo = tma_lo //no conflict
  179. var s_restore_exec_hi = tma_hi //no conflict
  180. var s_restore_status = ttmp4
  181. var s_restore_trapsts = ttmp5
  182. var s_restore_xnack_mask_lo = xnack_mask_lo
  183. var s_restore_xnack_mask_hi = xnack_mask_hi
  184. var s_restore_buf_rsrc0 = ttmp8
  185. var s_restore_buf_rsrc1 = ttmp9
  186. var s_restore_buf_rsrc2 = ttmp10
  187. var s_restore_buf_rsrc3 = ttmp11
  188. /**************************************************************************/
  189. /* trap handler entry points */
  190. /**************************************************************************/
  191. /* Shader Main*/
  192. shader main
  193. asic(VI)
  194. type(CS)
  195. if ((EMU_RUN_HACK) && (!EMU_RUN_HACK_RESTORE_NORMAL)) //hack to use trap_id for determining save/restore
  196. //FIXME VCCZ un-init assertion s_getreg_b32 s_save_status, hwreg(HW_REG_STATUS) //save STATUS since we will change SCC
  197. s_and_b32 s_save_tmp, s_save_pc_hi, 0xffff0000 //change SCC
  198. s_cmp_eq_u32 s_save_tmp, 0x007e0000 //Save: trap_id = 0x7e. Restore: trap_id = 0x7f.
  199. s_cbranch_scc0 L_JUMP_TO_RESTORE //do not need to recover STATUS here since we are going to RESTORE
  200. //FIXME s_setreg_b32 hwreg(HW_REG_STATUS), s_save_status //need to recover STATUS since we are going to SAVE
  201. s_branch L_SKIP_RESTORE //NOT restore, SAVE actually
  202. else
  203. s_branch L_SKIP_RESTORE //NOT restore. might be a regular trap or save
  204. end
  205. L_JUMP_TO_RESTORE:
  206. s_branch L_RESTORE //restore
  207. L_SKIP_RESTORE:
  208. s_getreg_b32 s_save_status, hwreg(HW_REG_STATUS) //save STATUS since we will change SCC
  209. s_andn2_b32 s_save_status, s_save_status, SQ_WAVE_STATUS_SPI_PRIO_MASK //check whether this is for save
  210. s_getreg_b32 s_save_trapsts, hwreg(HW_REG_TRAPSTS)
  211. s_and_b32 s_save_trapsts, s_save_trapsts, SQ_WAVE_TRAPSTS_SAVECTX_MASK //check whether this is for save
  212. s_cbranch_scc1 L_SAVE //this is the operation for save
  213. // ********* Handle non-CWSR traps *******************
  214. if (!EMU_RUN_HACK)
  215. /* read tba and tma for next level trap handler, ttmp4 is used as s_save_status */
  216. s_load_dwordx4 [ttmp8,ttmp9,ttmp10, ttmp11], [tma_lo,tma_hi], 0
  217. s_waitcnt lgkmcnt(0)
  218. s_or_b32 ttmp7, ttmp8, ttmp9
  219. s_cbranch_scc0 L_NO_NEXT_TRAP //next level trap handler not been set
  220. set_status_without_spi_prio(s_save_status, ttmp2) //restore HW status(SCC)
  221. s_setpc_b64 [ttmp8,ttmp9] //jump to next level trap handler
  222. L_NO_NEXT_TRAP:
  223. s_getreg_b32 s_save_trapsts, hwreg(HW_REG_TRAPSTS)
  224. s_and_b32 s_save_trapsts, s_save_trapsts, SQ_WAVE_TRAPSTS_EXCE_MASK // Check whether it is an exception
  225. s_cbranch_scc1 L_EXCP_CASE // Exception, jump back to the shader program directly.
  226. s_add_u32 ttmp0, ttmp0, 4 // S_TRAP case, add 4 to ttmp0
  227. s_addc_u32 ttmp1, ttmp1, 0
  228. L_EXCP_CASE:
  229. s_and_b32 ttmp1, ttmp1, 0xFFFF
  230. set_status_without_spi_prio(s_save_status, ttmp2) //restore HW status(SCC)
  231. s_rfe_b64 [ttmp0, ttmp1]
  232. end
  233. // ********* End handling of non-CWSR traps *******************
  234. /**************************************************************************/
  235. /* save routine */
  236. /**************************************************************************/
  237. L_SAVE:
  238. if G8SR_DEBUG_TIMESTAMP
  239. s_memrealtime s_g8sr_ts_save_s
  240. s_waitcnt lgkmcnt(0) //FIXME, will cause xnack??
  241. end
  242. //check whether there is mem_viol
  243. s_getreg_b32 s_save_trapsts, hwreg(HW_REG_TRAPSTS)
  244. s_and_b32 s_save_trapsts, s_save_trapsts, SQ_WAVE_TRAPSTS_MEM_VIOL_MASK
  245. s_cbranch_scc0 L_NO_PC_REWIND
  246. //if so, need rewind PC assuming GDS operation gets NACKed
  247. s_mov_b32 s_save_tmp, 0 //clear mem_viol bit
  248. s_setreg_b32 hwreg(HW_REG_TRAPSTS, SQ_WAVE_TRAPSTS_MEM_VIOL_SHIFT, 1), s_save_tmp //clear mem_viol bit
  249. s_and_b32 s_save_pc_hi, s_save_pc_hi, 0x0000ffff //pc[47:32]
  250. s_sub_u32 s_save_pc_lo, s_save_pc_lo, 8 //pc[31:0]-8
  251. s_subb_u32 s_save_pc_hi, s_save_pc_hi, 0x0 // -scc
  252. L_NO_PC_REWIND:
  253. s_mov_b32 s_save_tmp, 0 //clear saveCtx bit
  254. s_setreg_b32 hwreg(HW_REG_TRAPSTS, SQ_WAVE_TRAPSTS_SAVECTX_SHIFT, 1), s_save_tmp //clear saveCtx bit
  255. s_mov_b32 s_save_xnack_mask_lo, xnack_mask_lo //save XNACK_MASK
  256. s_mov_b32 s_save_xnack_mask_hi, xnack_mask_hi //save XNACK must before any memory operation
  257. s_getreg_b32 s_save_tmp, hwreg(HW_REG_IB_STS, SQ_WAVE_IB_STS_RCNT_SHIFT, SQ_WAVE_IB_STS_RCNT_SIZE) //save RCNT
  258. s_lshl_b32 s_save_tmp, s_save_tmp, S_SAVE_PC_HI_RCNT_SHIFT
  259. s_or_b32 s_save_pc_hi, s_save_pc_hi, s_save_tmp
  260. s_getreg_b32 s_save_tmp, hwreg(HW_REG_IB_STS, SQ_WAVE_IB_STS_FIRST_REPLAY_SHIFT, SQ_WAVE_IB_STS_FIRST_REPLAY_SIZE) //save FIRST_REPLAY
  261. s_lshl_b32 s_save_tmp, s_save_tmp, S_SAVE_PC_HI_FIRST_REPLAY_SHIFT
  262. s_or_b32 s_save_pc_hi, s_save_pc_hi, s_save_tmp
  263. s_getreg_b32 s_save_tmp, hwreg(HW_REG_IB_STS) //clear RCNT and FIRST_REPLAY in IB_STS
  264. s_and_b32 s_save_tmp, s_save_tmp, SQ_WAVE_IB_STS_RCNT_FIRST_REPLAY_MASK_NEG
  265. s_setreg_b32 hwreg(HW_REG_IB_STS), s_save_tmp
  266. /* inform SPI the readiness and wait for SPI's go signal */
  267. s_mov_b32 s_save_exec_lo, exec_lo //save EXEC and use EXEC for the go signal from SPI
  268. s_mov_b32 s_save_exec_hi, exec_hi
  269. s_mov_b64 exec, 0x0 //clear EXEC to get ready to receive
  270. if G8SR_DEBUG_TIMESTAMP
  271. s_memrealtime s_g8sr_ts_sq_save_msg
  272. s_waitcnt lgkmcnt(0)
  273. end
  274. if (EMU_RUN_HACK)
  275. else
  276. s_sendmsg sendmsg(MSG_SAVEWAVE) //send SPI a message and wait for SPI's write to EXEC
  277. end
  278. // Set SPI_PRIO=2 to avoid starving instruction fetch in the waves we're waiting for.
  279. s_or_b32 s_save_tmp, s_save_status, (2 << SQ_WAVE_STATUS_SPI_PRIO_SHIFT)
  280. s_setreg_b32 hwreg(HW_REG_STATUS), s_save_tmp
  281. L_SLEEP:
  282. s_sleep 0x2 // sleep 1 (64clk) is not enough for 8 waves per SIMD, which will cause SQ hang, since the 7,8th wave could not get arbit to exec inst, while other waves are stuck into the sleep-loop and waiting for wrexec!=0
  283. if (EMU_RUN_HACK)
  284. else
  285. s_cbranch_execz L_SLEEP
  286. end
  287. if G8SR_DEBUG_TIMESTAMP
  288. s_memrealtime s_g8sr_ts_spi_wrexec
  289. s_waitcnt lgkmcnt(0)
  290. end
  291. /* setup Resource Contants */
  292. if ((EMU_RUN_HACK) && (!EMU_RUN_HACK_SAVE_SINGLE_WAVE))
  293. //calculate wd_addr using absolute thread id
  294. v_readlane_b32 s_save_tmp, v9, 0
  295. s_lshr_b32 s_save_tmp, s_save_tmp, 6
  296. s_mul_i32 s_save_tmp, s_save_tmp, WAVE_SPACE
  297. s_add_i32 s_save_spi_init_lo, s_save_tmp, WG_BASE_ADDR_LO
  298. s_mov_b32 s_save_spi_init_hi, WG_BASE_ADDR_HI
  299. s_and_b32 s_save_spi_init_hi, s_save_spi_init_hi, CTX_SAVE_CONTROL
  300. else
  301. end
  302. if ((EMU_RUN_HACK) && (EMU_RUN_HACK_SAVE_SINGLE_WAVE))
  303. s_add_i32 s_save_spi_init_lo, s_save_tmp, WG_BASE_ADDR_LO
  304. s_mov_b32 s_save_spi_init_hi, WG_BASE_ADDR_HI
  305. s_and_b32 s_save_spi_init_hi, s_save_spi_init_hi, CTX_SAVE_CONTROL
  306. else
  307. end
  308. s_mov_b32 s_save_buf_rsrc0, s_save_spi_init_lo //base_addr_lo
  309. s_and_b32 s_save_buf_rsrc1, s_save_spi_init_hi, 0x0000FFFF //base_addr_hi
  310. s_or_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, S_SAVE_BUF_RSRC_WORD1_STRIDE
  311. s_mov_b32 s_save_buf_rsrc2, 0 //NUM_RECORDS initial value = 0 (in bytes) although not neccessarily inited
  312. s_mov_b32 s_save_buf_rsrc3, S_SAVE_BUF_RSRC_WORD3_MISC
  313. s_and_b32 s_save_tmp, s_save_spi_init_hi, S_SAVE_SPI_INIT_ATC_MASK
  314. s_lshr_b32 s_save_tmp, s_save_tmp, (S_SAVE_SPI_INIT_ATC_SHIFT-SQ_BUF_RSRC_WORD1_ATC_SHIFT) //get ATC bit into position
  315. s_or_b32 s_save_buf_rsrc3, s_save_buf_rsrc3, s_save_tmp //or ATC
  316. s_and_b32 s_save_tmp, s_save_spi_init_hi, S_SAVE_SPI_INIT_MTYPE_MASK
  317. s_lshr_b32 s_save_tmp, s_save_tmp, (S_SAVE_SPI_INIT_MTYPE_SHIFT-SQ_BUF_RSRC_WORD3_MTYPE_SHIFT) //get MTYPE bits into position
  318. s_or_b32 s_save_buf_rsrc3, s_save_buf_rsrc3, s_save_tmp //or MTYPE
  319. //FIXME right now s_save_m0/s_save_mem_offset use tma_lo/tma_hi (might need to save them before using them?)
  320. s_mov_b32 s_save_m0, m0 //save M0
  321. /* global mem offset */
  322. s_mov_b32 s_save_mem_offset, 0x0 //mem offset initial value = 0
  323. /* save HW registers */
  324. //////////////////////////////
  325. L_SAVE_HWREG:
  326. // HWREG SR memory offset : size(VGPR)+size(SGPR)
  327. get_vgpr_size_bytes(s_save_mem_offset)
  328. get_sgpr_size_bytes(s_save_tmp)
  329. s_add_u32 s_save_mem_offset, s_save_mem_offset, s_save_tmp
  330. s_mov_b32 s_save_buf_rsrc2, 0x4 //NUM_RECORDS in bytes
  331. if (SWIZZLE_EN)
  332. s_add_u32 s_save_buf_rsrc2, s_save_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  333. else
  334. s_mov_b32 s_save_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  335. end
  336. write_hwreg_to_mem(s_save_m0, s_save_buf_rsrc0, s_save_mem_offset) //M0
  337. if ((EMU_RUN_HACK) && (EMU_RUN_HACK_SAVE_FIRST_TIME))
  338. s_add_u32 s_save_pc_lo, s_save_pc_lo, 4 //pc[31:0]+4
  339. s_addc_u32 s_save_pc_hi, s_save_pc_hi, 0x0 //carry bit over
  340. s_mov_b32 tba_lo, EMU_RUN_HACK_SAVE_FIRST_TIME_TBA_LO
  341. s_mov_b32 tba_hi, EMU_RUN_HACK_SAVE_FIRST_TIME_TBA_HI
  342. end
  343. write_hwreg_to_mem(s_save_pc_lo, s_save_buf_rsrc0, s_save_mem_offset) //PC
  344. write_hwreg_to_mem(s_save_pc_hi, s_save_buf_rsrc0, s_save_mem_offset)
  345. write_hwreg_to_mem(s_save_exec_lo, s_save_buf_rsrc0, s_save_mem_offset) //EXEC
  346. write_hwreg_to_mem(s_save_exec_hi, s_save_buf_rsrc0, s_save_mem_offset)
  347. write_hwreg_to_mem(s_save_status, s_save_buf_rsrc0, s_save_mem_offset) //STATUS
  348. //s_save_trapsts conflicts with s_save_alloc_size
  349. s_getreg_b32 s_save_trapsts, hwreg(HW_REG_TRAPSTS)
  350. write_hwreg_to_mem(s_save_trapsts, s_save_buf_rsrc0, s_save_mem_offset) //TRAPSTS
  351. write_hwreg_to_mem(s_save_xnack_mask_lo, s_save_buf_rsrc0, s_save_mem_offset) //XNACK_MASK_LO
  352. write_hwreg_to_mem(s_save_xnack_mask_hi, s_save_buf_rsrc0, s_save_mem_offset) //XNACK_MASK_HI
  353. //use s_save_tmp would introduce conflict here between s_save_tmp and s_save_buf_rsrc2
  354. s_getreg_b32 s_save_m0, hwreg(HW_REG_MODE) //MODE
  355. write_hwreg_to_mem(s_save_m0, s_save_buf_rsrc0, s_save_mem_offset)
  356. write_hwreg_to_mem(tba_lo, s_save_buf_rsrc0, s_save_mem_offset) //TBA_LO
  357. write_hwreg_to_mem(tba_hi, s_save_buf_rsrc0, s_save_mem_offset) //TBA_HI
  358. /* the first wave in the threadgroup */
  359. // save fist_wave bits in tba_hi unused bit.26
  360. s_and_b32 s_save_tmp, s_save_spi_init_hi, S_SAVE_SPI_INIT_FIRST_WAVE_MASK // extract fisrt wave bit
  361. //s_or_b32 tba_hi, s_save_tmp, tba_hi // save first wave bit to tba_hi.bits[26]
  362. s_mov_b32 s_save_exec_hi, 0x0
  363. s_or_b32 s_save_exec_hi, s_save_tmp, s_save_exec_hi // save first wave bit to s_save_exec_hi.bits[26]
  364. /* save SGPRs */
  365. // Save SGPR before LDS save, then the s0 to s4 can be used during LDS save...
  366. //////////////////////////////
  367. // SGPR SR memory offset : size(VGPR)
  368. get_vgpr_size_bytes(s_save_mem_offset)
  369. // TODO, change RSRC word to rearrange memory layout for SGPRS
  370. s_getreg_b32 s_save_alloc_size, hwreg(HW_REG_GPR_ALLOC,SQ_WAVE_GPR_ALLOC_SGPR_SIZE_SHIFT,SQ_WAVE_GPR_ALLOC_SGPR_SIZE_SIZE) //spgr_size
  371. s_add_u32 s_save_alloc_size, s_save_alloc_size, 1
  372. s_lshl_b32 s_save_alloc_size, s_save_alloc_size, 4 //Number of SGPRs = (sgpr_size + 1) * 16 (non-zero value)
  373. if (SGPR_SAVE_USE_SQC)
  374. s_lshl_b32 s_save_buf_rsrc2, s_save_alloc_size, 2 //NUM_RECORDS in bytes
  375. else
  376. s_lshl_b32 s_save_buf_rsrc2, s_save_alloc_size, 8 //NUM_RECORDS in bytes (64 threads)
  377. end
  378. if (SWIZZLE_EN)
  379. s_add_u32 s_save_buf_rsrc2, s_save_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  380. else
  381. s_mov_b32 s_save_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  382. end
  383. // backup s_save_buf_rsrc0,1 to s_save_pc_lo/hi, since write_16sgpr_to_mem function will change the rsrc0
  384. //s_mov_b64 s_save_pc_lo, s_save_buf_rsrc0
  385. s_mov_b64 s_save_xnack_mask_lo, s_save_buf_rsrc0
  386. s_add_u32 s_save_buf_rsrc0, s_save_buf_rsrc0, s_save_mem_offset
  387. s_addc_u32 s_save_buf_rsrc1, s_save_buf_rsrc1, 0
  388. s_mov_b32 m0, 0x0 //SGPR initial index value =0
  389. L_SAVE_SGPR_LOOP:
  390. // SGPR is allocated in 16 SGPR granularity
  391. s_movrels_b64 s0, s0 //s0 = s[0+m0], s1 = s[1+m0]
  392. s_movrels_b64 s2, s2 //s2 = s[2+m0], s3 = s[3+m0]
  393. s_movrels_b64 s4, s4 //s4 = s[4+m0], s5 = s[5+m0]
  394. s_movrels_b64 s6, s6 //s6 = s[6+m0], s7 = s[7+m0]
  395. s_movrels_b64 s8, s8 //s8 = s[8+m0], s9 = s[9+m0]
  396. s_movrels_b64 s10, s10 //s10 = s[10+m0], s11 = s[11+m0]
  397. s_movrels_b64 s12, s12 //s12 = s[12+m0], s13 = s[13+m0]
  398. s_movrels_b64 s14, s14 //s14 = s[14+m0], s15 = s[15+m0]
  399. write_16sgpr_to_mem(s0, s_save_buf_rsrc0, s_save_mem_offset) //PV: the best performance should be using s_buffer_store_dwordx4
  400. s_add_u32 m0, m0, 16 //next sgpr index
  401. s_cmp_lt_u32 m0, s_save_alloc_size //scc = (m0 < s_save_alloc_size) ? 1 : 0
  402. s_cbranch_scc1 L_SAVE_SGPR_LOOP //SGPR save is complete?
  403. // restore s_save_buf_rsrc0,1
  404. //s_mov_b64 s_save_buf_rsrc0, s_save_pc_lo
  405. s_mov_b64 s_save_buf_rsrc0, s_save_xnack_mask_lo
  406. /* save first 4 VGPR, then LDS save could use */
  407. // each wave will alloc 4 vgprs at least...
  408. /////////////////////////////////////////////////////////////////////////////////////
  409. s_mov_b32 s_save_mem_offset, 0
  410. s_mov_b32 exec_lo, 0xFFFFFFFF //need every thread from now on
  411. s_mov_b32 exec_hi, 0xFFFFFFFF
  412. if (SWIZZLE_EN)
  413. s_add_u32 s_save_buf_rsrc2, s_save_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  414. else
  415. s_mov_b32 s_save_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  416. end
  417. // VGPR Allocated in 4-GPR granularity
  418. if G8SR_VGPR_SR_IN_DWX4
  419. // the const stride for DWx4 is 4*4 bytes
  420. s_and_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, 0x0000FFFF // reset const stride to 0
  421. s_or_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, G8SR_SAVE_BUF_RSRC_WORD1_STRIDE_DWx4 // const stride to 4*4 bytes
  422. buffer_store_dwordx4 v0, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1
  423. s_and_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, 0x0000FFFF // reset const stride to 0
  424. s_or_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, S_SAVE_BUF_RSRC_WORD1_STRIDE // reset const stride to 4 bytes
  425. else
  426. buffer_store_dword v0, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1
  427. buffer_store_dword v1, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1 offset:256
  428. buffer_store_dword v2, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1 offset:256*2
  429. buffer_store_dword v3, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1 offset:256*3
  430. end
  431. /* save LDS */
  432. //////////////////////////////
  433. L_SAVE_LDS:
  434. // Change EXEC to all threads...
  435. s_mov_b32 exec_lo, 0xFFFFFFFF //need every thread from now on
  436. s_mov_b32 exec_hi, 0xFFFFFFFF
  437. s_getreg_b32 s_save_alloc_size, hwreg(HW_REG_LDS_ALLOC,SQ_WAVE_LDS_ALLOC_LDS_SIZE_SHIFT,SQ_WAVE_LDS_ALLOC_LDS_SIZE_SIZE) //lds_size
  438. s_and_b32 s_save_alloc_size, s_save_alloc_size, 0xFFFFFFFF //lds_size is zero?
  439. s_cbranch_scc0 L_SAVE_LDS_DONE //no lds used? jump to L_SAVE_DONE
  440. s_barrier //LDS is used? wait for other waves in the same TG
  441. //s_and_b32 s_save_tmp, tba_hi, S_SAVE_SPI_INIT_FIRST_WAVE_MASK //exec is still used here
  442. s_and_b32 s_save_tmp, s_save_exec_hi, S_SAVE_SPI_INIT_FIRST_WAVE_MASK //exec is still used here
  443. s_cbranch_scc0 L_SAVE_LDS_DONE
  444. // first wave do LDS save;
  445. s_lshl_b32 s_save_alloc_size, s_save_alloc_size, 6 //LDS size in dwords = lds_size * 64dw
  446. s_lshl_b32 s_save_alloc_size, s_save_alloc_size, 2 //LDS size in bytes
  447. s_mov_b32 s_save_buf_rsrc2, s_save_alloc_size //NUM_RECORDS in bytes
  448. // LDS at offset: size(VGPR)+SIZE(SGPR)+SIZE(HWREG)
  449. //
  450. get_vgpr_size_bytes(s_save_mem_offset)
  451. get_sgpr_size_bytes(s_save_tmp)
  452. s_add_u32 s_save_mem_offset, s_save_mem_offset, s_save_tmp
  453. s_add_u32 s_save_mem_offset, s_save_mem_offset, get_hwreg_size_bytes()
  454. if (SWIZZLE_EN)
  455. s_add_u32 s_save_buf_rsrc2, s_save_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  456. else
  457. s_mov_b32 s_save_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  458. end
  459. s_mov_b32 m0, 0x0 //lds_offset initial value = 0
  460. var LDS_DMA_ENABLE = 0
  461. var UNROLL = 0
  462. if UNROLL==0 && LDS_DMA_ENABLE==1
  463. s_mov_b32 s3, 256*2
  464. s_nop 0
  465. s_nop 0
  466. s_nop 0
  467. L_SAVE_LDS_LOOP:
  468. //TODO: looks the 2 buffer_store/load clause for s/r will hurt performance.???
  469. if (SAVE_LDS) //SPI always alloc LDS space in 128DW granularity
  470. buffer_store_lds_dword s_save_buf_rsrc0, s_save_mem_offset lds:1 // first 64DW
  471. buffer_store_lds_dword s_save_buf_rsrc0, s_save_mem_offset lds:1 offset:256 // second 64DW
  472. end
  473. s_add_u32 m0, m0, s3 //every buffer_store_lds does 256 bytes
  474. s_add_u32 s_save_mem_offset, s_save_mem_offset, s3 //mem offset increased by 256 bytes
  475. s_cmp_lt_u32 m0, s_save_alloc_size //scc=(m0 < s_save_alloc_size) ? 1 : 0
  476. s_cbranch_scc1 L_SAVE_LDS_LOOP //LDS save is complete?
  477. elsif LDS_DMA_ENABLE==1 && UNROLL==1 // UNROOL , has ichace miss
  478. // store from higest LDS address to lowest
  479. s_mov_b32 s3, 256*2
  480. s_sub_u32 m0, s_save_alloc_size, s3
  481. s_add_u32 s_save_mem_offset, s_save_mem_offset, m0
  482. s_lshr_b32 s_save_alloc_size, s_save_alloc_size, 9 // how many 128 trunks...
  483. s_sub_u32 s_save_alloc_size, 128, s_save_alloc_size // store from higheset addr to lowest
  484. s_mul_i32 s_save_alloc_size, s_save_alloc_size, 6*4 // PC offset increment, each LDS save block cost 6*4 Bytes instruction
  485. s_add_u32 s_save_alloc_size, s_save_alloc_size, 3*4 //2is the below 2 inst...//s_addc and s_setpc
  486. s_nop 0
  487. s_nop 0
  488. s_nop 0 //pad 3 dw to let LDS_DMA align with 64Bytes
  489. s_getpc_b64 s[0:1] // reuse s[0:1], since s[0:1] already saved
  490. s_add_u32 s0, s0,s_save_alloc_size
  491. s_addc_u32 s1, s1, 0
  492. s_setpc_b64 s[0:1]
  493. for var i =0; i< 128; i++
  494. // be careful to make here a 64Byte aligned address, which could improve performance...
  495. buffer_store_lds_dword s_save_buf_rsrc0, s_save_mem_offset lds:1 offset:0 // first 64DW
  496. buffer_store_lds_dword s_save_buf_rsrc0, s_save_mem_offset lds:1 offset:256 // second 64DW
  497. if i!=127
  498. s_sub_u32 m0, m0, s3 // use a sgpr to shrink 2DW-inst to 1DW inst to improve performance , i.e. pack more LDS_DMA inst to one Cacheline
  499. s_sub_u32 s_save_mem_offset, s_save_mem_offset, s3
  500. end
  501. end
  502. else // BUFFER_STORE
  503. v_mbcnt_lo_u32_b32 v2, 0xffffffff, 0x0
  504. v_mbcnt_hi_u32_b32 v3, 0xffffffff, v2 // tid
  505. v_mul_i32_i24 v2, v3, 8 // tid*8
  506. v_mov_b32 v3, 256*2
  507. s_mov_b32 m0, 0x10000
  508. s_mov_b32 s0, s_save_buf_rsrc3
  509. s_and_b32 s_save_buf_rsrc3, s_save_buf_rsrc3, 0xFF7FFFFF // disable add_tid
  510. s_or_b32 s_save_buf_rsrc3, s_save_buf_rsrc3, 0x58000 //DFMT
  511. L_SAVE_LDS_LOOP_VECTOR:
  512. ds_read_b64 v[0:1], v2 //x =LDS[a], byte address
  513. s_waitcnt lgkmcnt(0)
  514. buffer_store_dwordx2 v[0:1], v2, s_save_buf_rsrc0, s_save_mem_offset offen:1 glc:1 slc:1
  515. // s_waitcnt vmcnt(0)
  516. v_add_u32 v2, vcc[0:1], v2, v3
  517. v_cmp_lt_u32 vcc[0:1], v2, s_save_alloc_size
  518. s_cbranch_vccnz L_SAVE_LDS_LOOP_VECTOR
  519. // restore rsrc3
  520. s_mov_b32 s_save_buf_rsrc3, s0
  521. end
  522. L_SAVE_LDS_DONE:
  523. /* save VGPRs - set the Rest VGPRs */
  524. //////////////////////////////////////////////////////////////////////////////////////
  525. L_SAVE_VGPR:
  526. // VGPR SR memory offset: 0
  527. // TODO rearrange the RSRC words to use swizzle for VGPR save...
  528. s_mov_b32 s_save_mem_offset, (0+256*4) // for the rest VGPRs
  529. s_mov_b32 exec_lo, 0xFFFFFFFF //need every thread from now on
  530. s_mov_b32 exec_hi, 0xFFFFFFFF
  531. s_getreg_b32 s_save_alloc_size, hwreg(HW_REG_GPR_ALLOC,SQ_WAVE_GPR_ALLOC_VGPR_SIZE_SHIFT,SQ_WAVE_GPR_ALLOC_VGPR_SIZE_SIZE) //vpgr_size
  532. s_add_u32 s_save_alloc_size, s_save_alloc_size, 1
  533. s_lshl_b32 s_save_alloc_size, s_save_alloc_size, 2 //Number of VGPRs = (vgpr_size + 1) * 4 (non-zero value) //FIXME for GFX, zero is possible
  534. s_lshl_b32 s_save_buf_rsrc2, s_save_alloc_size, 8 //NUM_RECORDS in bytes (64 threads*4)
  535. if (SWIZZLE_EN)
  536. s_add_u32 s_save_buf_rsrc2, s_save_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  537. else
  538. s_mov_b32 s_save_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  539. end
  540. // VGPR Allocated in 4-GPR granularity
  541. if G8SR_VGPR_SR_IN_DWX4
  542. // the const stride for DWx4 is 4*4 bytes
  543. s_and_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, 0x0000FFFF // reset const stride to 0
  544. s_or_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, G8SR_SAVE_BUF_RSRC_WORD1_STRIDE_DWx4 // const stride to 4*4 bytes
  545. s_mov_b32 m0, 4 // skip first 4 VGPRs
  546. s_cmp_lt_u32 m0, s_save_alloc_size
  547. s_cbranch_scc0 L_SAVE_VGPR_LOOP_END // no more vgprs
  548. s_set_gpr_idx_on m0, 0x1 // This will change M0
  549. s_add_u32 s_save_alloc_size, s_save_alloc_size, 0x1000 // because above inst change m0
  550. L_SAVE_VGPR_LOOP:
  551. v_mov_b32 v0, v0 // v0 = v[0+m0]
  552. v_mov_b32 v1, v1
  553. v_mov_b32 v2, v2
  554. v_mov_b32 v3, v3
  555. buffer_store_dwordx4 v0, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1
  556. s_add_u32 m0, m0, 4
  557. s_add_u32 s_save_mem_offset, s_save_mem_offset, 256*4
  558. s_cmp_lt_u32 m0, s_save_alloc_size
  559. s_cbranch_scc1 L_SAVE_VGPR_LOOP //VGPR save is complete?
  560. s_set_gpr_idx_off
  561. L_SAVE_VGPR_LOOP_END:
  562. s_and_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, 0x0000FFFF // reset const stride to 0
  563. s_or_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, S_SAVE_BUF_RSRC_WORD1_STRIDE // reset const stride to 4 bytes
  564. else
  565. // VGPR store using dw burst
  566. s_mov_b32 m0, 0x4 //VGPR initial index value =0
  567. s_cmp_lt_u32 m0, s_save_alloc_size
  568. s_cbranch_scc0 L_SAVE_VGPR_END
  569. s_set_gpr_idx_on m0, 0x1 //M0[7:0] = M0[7:0] and M0[15:12] = 0x1
  570. s_add_u32 s_save_alloc_size, s_save_alloc_size, 0x1000 //add 0x1000 since we compare m0 against it later
  571. L_SAVE_VGPR_LOOP:
  572. v_mov_b32 v0, v0 //v0 = v[0+m0]
  573. v_mov_b32 v1, v1 //v0 = v[0+m0]
  574. v_mov_b32 v2, v2 //v0 = v[0+m0]
  575. v_mov_b32 v3, v3 //v0 = v[0+m0]
  576. if(USE_MTBUF_INSTEAD_OF_MUBUF)
  577. tbuffer_store_format_x v0, v0, s_save_buf_rsrc0, s_save_mem_offset format:BUF_NUM_FORMAT_FLOAT format: BUF_DATA_FORMAT_32 slc:1 glc:1
  578. else
  579. buffer_store_dword v0, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1
  580. buffer_store_dword v1, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1 offset:256
  581. buffer_store_dword v2, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1 offset:256*2
  582. buffer_store_dword v3, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1 offset:256*3
  583. end
  584. s_add_u32 m0, m0, 4 //next vgpr index
  585. s_add_u32 s_save_mem_offset, s_save_mem_offset, 256*4 //every buffer_store_dword does 256 bytes
  586. s_cmp_lt_u32 m0, s_save_alloc_size //scc = (m0 < s_save_alloc_size) ? 1 : 0
  587. s_cbranch_scc1 L_SAVE_VGPR_LOOP //VGPR save is complete?
  588. s_set_gpr_idx_off
  589. end
  590. L_SAVE_VGPR_END:
  591. /* S_PGM_END_SAVED */ //FIXME graphics ONLY
  592. if ((EMU_RUN_HACK) && (!EMU_RUN_HACK_SAVE_NORMAL_EXIT))
  593. s_and_b32 s_save_pc_hi, s_save_pc_hi, 0x0000ffff //pc[47:32]
  594. s_add_u32 s_save_pc_lo, s_save_pc_lo, 4 //pc[31:0]+4
  595. s_addc_u32 s_save_pc_hi, s_save_pc_hi, 0x0 //carry bit over
  596. s_rfe_b64 s_save_pc_lo //Return to the main shader program
  597. else
  598. end
  599. // Save Done timestamp
  600. if G8SR_DEBUG_TIMESTAMP
  601. s_memrealtime s_g8sr_ts_save_d
  602. // SGPR SR memory offset : size(VGPR)
  603. get_vgpr_size_bytes(s_save_mem_offset)
  604. s_add_u32 s_save_mem_offset, s_save_mem_offset, G8SR_DEBUG_TS_SAVE_D_OFFSET
  605. s_waitcnt lgkmcnt(0) //FIXME, will cause xnack??
  606. // Need reset rsrc2??
  607. s_mov_b32 m0, s_save_mem_offset
  608. s_mov_b32 s_save_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  609. s_buffer_store_dwordx2 s_g8sr_ts_save_d, s_save_buf_rsrc0, m0 glc:1
  610. end
  611. s_branch L_END_PGM
  612. /**************************************************************************/
  613. /* restore routine */
  614. /**************************************************************************/
  615. L_RESTORE:
  616. /* Setup Resource Contants */
  617. if ((EMU_RUN_HACK) && (!EMU_RUN_HACK_RESTORE_NORMAL))
  618. //calculate wd_addr using absolute thread id
  619. v_readlane_b32 s_restore_tmp, v9, 0
  620. s_lshr_b32 s_restore_tmp, s_restore_tmp, 6
  621. s_mul_i32 s_restore_tmp, s_restore_tmp, WAVE_SPACE
  622. s_add_i32 s_restore_spi_init_lo, s_restore_tmp, WG_BASE_ADDR_LO
  623. s_mov_b32 s_restore_spi_init_hi, WG_BASE_ADDR_HI
  624. s_and_b32 s_restore_spi_init_hi, s_restore_spi_init_hi, CTX_RESTORE_CONTROL
  625. else
  626. end
  627. if G8SR_DEBUG_TIMESTAMP
  628. s_memrealtime s_g8sr_ts_restore_s
  629. s_waitcnt lgkmcnt(0) //FIXME, will cause xnack??
  630. // tma_lo/hi are sgpr 110, 111, which will not used for 112 SGPR allocated case...
  631. s_mov_b32 s_restore_pc_lo, s_g8sr_ts_restore_s[0]
  632. s_mov_b32 s_restore_pc_hi, s_g8sr_ts_restore_s[1] //backup ts to ttmp0/1, sicne exec will be finally restored..
  633. end
  634. s_mov_b32 s_restore_buf_rsrc0, s_restore_spi_init_lo //base_addr_lo
  635. s_and_b32 s_restore_buf_rsrc1, s_restore_spi_init_hi, 0x0000FFFF //base_addr_hi
  636. s_or_b32 s_restore_buf_rsrc1, s_restore_buf_rsrc1, S_RESTORE_BUF_RSRC_WORD1_STRIDE
  637. s_mov_b32 s_restore_buf_rsrc2, 0 //NUM_RECORDS initial value = 0 (in bytes)
  638. s_mov_b32 s_restore_buf_rsrc3, S_RESTORE_BUF_RSRC_WORD3_MISC
  639. s_and_b32 s_restore_tmp, s_restore_spi_init_hi, S_RESTORE_SPI_INIT_ATC_MASK
  640. s_lshr_b32 s_restore_tmp, s_restore_tmp, (S_RESTORE_SPI_INIT_ATC_SHIFT-SQ_BUF_RSRC_WORD1_ATC_SHIFT) //get ATC bit into position
  641. s_or_b32 s_restore_buf_rsrc3, s_restore_buf_rsrc3, s_restore_tmp //or ATC
  642. s_and_b32 s_restore_tmp, s_restore_spi_init_hi, S_RESTORE_SPI_INIT_MTYPE_MASK
  643. s_lshr_b32 s_restore_tmp, s_restore_tmp, (S_RESTORE_SPI_INIT_MTYPE_SHIFT-SQ_BUF_RSRC_WORD3_MTYPE_SHIFT) //get MTYPE bits into position
  644. s_or_b32 s_restore_buf_rsrc3, s_restore_buf_rsrc3, s_restore_tmp //or MTYPE
  645. /* global mem offset */
  646. // s_mov_b32 s_restore_mem_offset, 0x0 //mem offset initial value = 0
  647. /* the first wave in the threadgroup */
  648. s_and_b32 s_restore_tmp, s_restore_spi_init_hi, S_RESTORE_SPI_INIT_FIRST_WAVE_MASK
  649. s_cbranch_scc0 L_RESTORE_VGPR
  650. /* restore LDS */
  651. //////////////////////////////
  652. L_RESTORE_LDS:
  653. s_mov_b32 exec_lo, 0xFFFFFFFF //need every thread from now on //be consistent with SAVE although can be moved ahead
  654. s_mov_b32 exec_hi, 0xFFFFFFFF
  655. s_getreg_b32 s_restore_alloc_size, hwreg(HW_REG_LDS_ALLOC,SQ_WAVE_LDS_ALLOC_LDS_SIZE_SHIFT,SQ_WAVE_LDS_ALLOC_LDS_SIZE_SIZE) //lds_size
  656. s_and_b32 s_restore_alloc_size, s_restore_alloc_size, 0xFFFFFFFF //lds_size is zero?
  657. s_cbranch_scc0 L_RESTORE_VGPR //no lds used? jump to L_RESTORE_VGPR
  658. s_lshl_b32 s_restore_alloc_size, s_restore_alloc_size, 6 //LDS size in dwords = lds_size * 64dw
  659. s_lshl_b32 s_restore_alloc_size, s_restore_alloc_size, 2 //LDS size in bytes
  660. s_mov_b32 s_restore_buf_rsrc2, s_restore_alloc_size //NUM_RECORDS in bytes
  661. // LDS at offset: size(VGPR)+SIZE(SGPR)+SIZE(HWREG)
  662. //
  663. get_vgpr_size_bytes(s_restore_mem_offset)
  664. get_sgpr_size_bytes(s_restore_tmp)
  665. s_add_u32 s_restore_mem_offset, s_restore_mem_offset, s_restore_tmp
  666. s_add_u32 s_restore_mem_offset, s_restore_mem_offset, get_hwreg_size_bytes() //FIXME, Check if offset overflow???
  667. if (SWIZZLE_EN)
  668. s_add_u32 s_restore_buf_rsrc2, s_restore_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  669. else
  670. s_mov_b32 s_restore_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  671. end
  672. s_mov_b32 m0, 0x0 //lds_offset initial value = 0
  673. L_RESTORE_LDS_LOOP:
  674. if (SAVE_LDS)
  675. buffer_load_dword v0, v0, s_restore_buf_rsrc0, s_restore_mem_offset lds:1 // first 64DW
  676. buffer_load_dword v0, v0, s_restore_buf_rsrc0, s_restore_mem_offset lds:1 offset:256 // second 64DW
  677. end
  678. s_add_u32 m0, m0, 256*2 // 128 DW
  679. s_add_u32 s_restore_mem_offset, s_restore_mem_offset, 256*2 //mem offset increased by 128DW
  680. s_cmp_lt_u32 m0, s_restore_alloc_size //scc=(m0 < s_restore_alloc_size) ? 1 : 0
  681. s_cbranch_scc1 L_RESTORE_LDS_LOOP //LDS restore is complete?
  682. /* restore VGPRs */
  683. //////////////////////////////
  684. L_RESTORE_VGPR:
  685. // VGPR SR memory offset : 0
  686. s_mov_b32 s_restore_mem_offset, 0x0
  687. s_mov_b32 exec_lo, 0xFFFFFFFF //need every thread from now on //be consistent with SAVE although can be moved ahead
  688. s_mov_b32 exec_hi, 0xFFFFFFFF
  689. s_getreg_b32 s_restore_alloc_size, hwreg(HW_REG_GPR_ALLOC,SQ_WAVE_GPR_ALLOC_VGPR_SIZE_SHIFT,SQ_WAVE_GPR_ALLOC_VGPR_SIZE_SIZE) //vpgr_size
  690. s_add_u32 s_restore_alloc_size, s_restore_alloc_size, 1
  691. s_lshl_b32 s_restore_alloc_size, s_restore_alloc_size, 2 //Number of VGPRs = (vgpr_size + 1) * 4 (non-zero value)
  692. s_lshl_b32 s_restore_buf_rsrc2, s_restore_alloc_size, 8 //NUM_RECORDS in bytes (64 threads*4)
  693. if (SWIZZLE_EN)
  694. s_add_u32 s_restore_buf_rsrc2, s_restore_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  695. else
  696. s_mov_b32 s_restore_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  697. end
  698. if G8SR_VGPR_SR_IN_DWX4
  699. get_vgpr_size_bytes(s_restore_mem_offset)
  700. s_sub_u32 s_restore_mem_offset, s_restore_mem_offset, 256*4
  701. // the const stride for DWx4 is 4*4 bytes
  702. s_and_b32 s_restore_buf_rsrc1, s_restore_buf_rsrc1, 0x0000FFFF // reset const stride to 0
  703. s_or_b32 s_restore_buf_rsrc1, s_restore_buf_rsrc1, G8SR_RESTORE_BUF_RSRC_WORD1_STRIDE_DWx4 // const stride to 4*4 bytes
  704. s_mov_b32 m0, s_restore_alloc_size
  705. s_set_gpr_idx_on m0, 0x8 // Note.. This will change m0
  706. L_RESTORE_VGPR_LOOP:
  707. buffer_load_dwordx4 v0, v0, s_restore_buf_rsrc0, s_restore_mem_offset slc:1 glc:1
  708. s_waitcnt vmcnt(0)
  709. s_sub_u32 m0, m0, 4
  710. v_mov_b32 v0, v0 // v[0+m0] = v0
  711. v_mov_b32 v1, v1
  712. v_mov_b32 v2, v2
  713. v_mov_b32 v3, v3
  714. s_sub_u32 s_restore_mem_offset, s_restore_mem_offset, 256*4
  715. s_cmp_eq_u32 m0, 0x8000
  716. s_cbranch_scc0 L_RESTORE_VGPR_LOOP
  717. s_set_gpr_idx_off
  718. s_and_b32 s_restore_buf_rsrc1, s_restore_buf_rsrc1, 0x0000FFFF // reset const stride to 0
  719. s_or_b32 s_restore_buf_rsrc1, s_restore_buf_rsrc1, S_RESTORE_BUF_RSRC_WORD1_STRIDE // const stride to 4*4 bytes
  720. else
  721. // VGPR load using dw burst
  722. s_mov_b32 s_restore_mem_offset_save, s_restore_mem_offset // restore start with v1, v0 will be the last
  723. s_add_u32 s_restore_mem_offset, s_restore_mem_offset, 256*4
  724. s_mov_b32 m0, 4 //VGPR initial index value = 1
  725. s_set_gpr_idx_on m0, 0x8 //M0[7:0] = M0[7:0] and M0[15:12] = 0x8
  726. s_add_u32 s_restore_alloc_size, s_restore_alloc_size, 0x8000 //add 0x8000 since we compare m0 against it later
  727. L_RESTORE_VGPR_LOOP:
  728. if(USE_MTBUF_INSTEAD_OF_MUBUF)
  729. tbuffer_load_format_x v0, v0, s_restore_buf_rsrc0, s_restore_mem_offset format:BUF_NUM_FORMAT_FLOAT format: BUF_DATA_FORMAT_32 slc:1 glc:1
  730. else
  731. buffer_load_dword v0, v0, s_restore_buf_rsrc0, s_restore_mem_offset slc:1 glc:1
  732. buffer_load_dword v1, v0, s_restore_buf_rsrc0, s_restore_mem_offset slc:1 glc:1 offset:256
  733. buffer_load_dword v2, v0, s_restore_buf_rsrc0, s_restore_mem_offset slc:1 glc:1 offset:256*2
  734. buffer_load_dword v3, v0, s_restore_buf_rsrc0, s_restore_mem_offset slc:1 glc:1 offset:256*3
  735. end
  736. s_waitcnt vmcnt(0) //ensure data ready
  737. v_mov_b32 v0, v0 //v[0+m0] = v0
  738. v_mov_b32 v1, v1
  739. v_mov_b32 v2, v2
  740. v_mov_b32 v3, v3
  741. s_add_u32 m0, m0, 4 //next vgpr index
  742. s_add_u32 s_restore_mem_offset, s_restore_mem_offset, 256*4 //every buffer_load_dword does 256 bytes
  743. s_cmp_lt_u32 m0, s_restore_alloc_size //scc = (m0 < s_restore_alloc_size) ? 1 : 0
  744. s_cbranch_scc1 L_RESTORE_VGPR_LOOP //VGPR restore (except v0) is complete?
  745. s_set_gpr_idx_off
  746. /* VGPR restore on v0 */
  747. if(USE_MTBUF_INSTEAD_OF_MUBUF)
  748. tbuffer_load_format_x v0, v0, s_restore_buf_rsrc0, s_restore_mem_offset_save format:BUF_NUM_FORMAT_FLOAT format: BUF_DATA_FORMAT_32 slc:1 glc:1
  749. else
  750. buffer_load_dword v0, v0, s_restore_buf_rsrc0, s_restore_mem_offset_save slc:1 glc:1
  751. buffer_load_dword v1, v0, s_restore_buf_rsrc0, s_restore_mem_offset_save slc:1 glc:1 offset:256
  752. buffer_load_dword v2, v0, s_restore_buf_rsrc0, s_restore_mem_offset_save slc:1 glc:1 offset:256*2
  753. buffer_load_dword v3, v0, s_restore_buf_rsrc0, s_restore_mem_offset_save slc:1 glc:1 offset:256*3
  754. end
  755. end
  756. /* restore SGPRs */
  757. //////////////////////////////
  758. // SGPR SR memory offset : size(VGPR)
  759. get_vgpr_size_bytes(s_restore_mem_offset)
  760. get_sgpr_size_bytes(s_restore_tmp)
  761. s_add_u32 s_restore_mem_offset, s_restore_mem_offset, s_restore_tmp
  762. s_sub_u32 s_restore_mem_offset, s_restore_mem_offset, 16*4 // restore SGPR from S[n] to S[0], by 16 sgprs group
  763. // TODO, change RSRC word to rearrange memory layout for SGPRS
  764. s_getreg_b32 s_restore_alloc_size, hwreg(HW_REG_GPR_ALLOC,SQ_WAVE_GPR_ALLOC_SGPR_SIZE_SHIFT,SQ_WAVE_GPR_ALLOC_SGPR_SIZE_SIZE) //spgr_size
  765. s_add_u32 s_restore_alloc_size, s_restore_alloc_size, 1
  766. s_lshl_b32 s_restore_alloc_size, s_restore_alloc_size, 4 //Number of SGPRs = (sgpr_size + 1) * 16 (non-zero value)
  767. if (SGPR_SAVE_USE_SQC)
  768. s_lshl_b32 s_restore_buf_rsrc2, s_restore_alloc_size, 2 //NUM_RECORDS in bytes
  769. else
  770. s_lshl_b32 s_restore_buf_rsrc2, s_restore_alloc_size, 8 //NUM_RECORDS in bytes (64 threads)
  771. end
  772. if (SWIZZLE_EN)
  773. s_add_u32 s_restore_buf_rsrc2, s_restore_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  774. else
  775. s_mov_b32 s_restore_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  776. end
  777. /* If 112 SGPRs ar allocated, 4 sgprs are not used TBA(108,109),TMA(110,111),
  778. However, we are safe to restore these 4 SGPRs anyway, since TBA,TMA will later be restored by HWREG
  779. */
  780. s_mov_b32 m0, s_restore_alloc_size
  781. L_RESTORE_SGPR_LOOP:
  782. read_16sgpr_from_mem(s0, s_restore_buf_rsrc0, s_restore_mem_offset) //PV: further performance improvement can be made
  783. s_waitcnt lgkmcnt(0) //ensure data ready
  784. s_sub_u32 m0, m0, 16 // Restore from S[n] to S[0]
  785. s_movreld_b64 s0, s0 //s[0+m0] = s0
  786. s_movreld_b64 s2, s2
  787. s_movreld_b64 s4, s4
  788. s_movreld_b64 s6, s6
  789. s_movreld_b64 s8, s8
  790. s_movreld_b64 s10, s10
  791. s_movreld_b64 s12, s12
  792. s_movreld_b64 s14, s14
  793. s_cmp_eq_u32 m0, 0 //scc = (m0 < s_restore_alloc_size) ? 1 : 0
  794. s_cbranch_scc0 L_RESTORE_SGPR_LOOP //SGPR restore (except s0) is complete?
  795. /* restore HW registers */
  796. //////////////////////////////
  797. L_RESTORE_HWREG:
  798. if G8SR_DEBUG_TIMESTAMP
  799. s_mov_b32 s_g8sr_ts_restore_s[0], s_restore_pc_lo
  800. s_mov_b32 s_g8sr_ts_restore_s[1], s_restore_pc_hi
  801. end
  802. // HWREG SR memory offset : size(VGPR)+size(SGPR)
  803. get_vgpr_size_bytes(s_restore_mem_offset)
  804. get_sgpr_size_bytes(s_restore_tmp)
  805. s_add_u32 s_restore_mem_offset, s_restore_mem_offset, s_restore_tmp
  806. s_mov_b32 s_restore_buf_rsrc2, 0x4 //NUM_RECORDS in bytes
  807. if (SWIZZLE_EN)
  808. s_add_u32 s_restore_buf_rsrc2, s_restore_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  809. else
  810. s_mov_b32 s_restore_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  811. end
  812. read_hwreg_from_mem(s_restore_m0, s_restore_buf_rsrc0, s_restore_mem_offset) //M0
  813. read_hwreg_from_mem(s_restore_pc_lo, s_restore_buf_rsrc0, s_restore_mem_offset) //PC
  814. read_hwreg_from_mem(s_restore_pc_hi, s_restore_buf_rsrc0, s_restore_mem_offset)
  815. read_hwreg_from_mem(s_restore_exec_lo, s_restore_buf_rsrc0, s_restore_mem_offset) //EXEC
  816. read_hwreg_from_mem(s_restore_exec_hi, s_restore_buf_rsrc0, s_restore_mem_offset)
  817. read_hwreg_from_mem(s_restore_status, s_restore_buf_rsrc0, s_restore_mem_offset) //STATUS
  818. read_hwreg_from_mem(s_restore_trapsts, s_restore_buf_rsrc0, s_restore_mem_offset) //TRAPSTS
  819. read_hwreg_from_mem(xnack_mask_lo, s_restore_buf_rsrc0, s_restore_mem_offset) //XNACK_MASK_LO
  820. read_hwreg_from_mem(xnack_mask_hi, s_restore_buf_rsrc0, s_restore_mem_offset) //XNACK_MASK_HI
  821. read_hwreg_from_mem(s_restore_mode, s_restore_buf_rsrc0, s_restore_mem_offset) //MODE
  822. read_hwreg_from_mem(tba_lo, s_restore_buf_rsrc0, s_restore_mem_offset) //TBA_LO
  823. read_hwreg_from_mem(tba_hi, s_restore_buf_rsrc0, s_restore_mem_offset) //TBA_HI
  824. s_waitcnt lgkmcnt(0) //from now on, it is safe to restore STATUS and IB_STS
  825. //for normal save & restore, the saved PC points to the next inst to execute, no adjustment needs to be made, otherwise:
  826. if ((EMU_RUN_HACK) && (!EMU_RUN_HACK_RESTORE_NORMAL))
  827. s_add_u32 s_restore_pc_lo, s_restore_pc_lo, 8 //pc[31:0]+8 //two back-to-back s_trap are used (first for save and second for restore)
  828. s_addc_u32 s_restore_pc_hi, s_restore_pc_hi, 0x0 //carry bit over
  829. end
  830. if ((EMU_RUN_HACK) && (EMU_RUN_HACK_RESTORE_NORMAL))
  831. s_add_u32 s_restore_pc_lo, s_restore_pc_lo, 4 //pc[31:0]+4 // save is hack through s_trap but restore is normal
  832. s_addc_u32 s_restore_pc_hi, s_restore_pc_hi, 0x0 //carry bit over
  833. end
  834. s_mov_b32 m0, s_restore_m0
  835. s_mov_b32 exec_lo, s_restore_exec_lo
  836. s_mov_b32 exec_hi, s_restore_exec_hi
  837. s_and_b32 s_restore_m0, SQ_WAVE_TRAPSTS_PRE_SAVECTX_MASK, s_restore_trapsts
  838. s_setreg_b32 hwreg(HW_REG_TRAPSTS, SQ_WAVE_TRAPSTS_PRE_SAVECTX_SHIFT, SQ_WAVE_TRAPSTS_PRE_SAVECTX_SIZE), s_restore_m0
  839. s_and_b32 s_restore_m0, SQ_WAVE_TRAPSTS_POST_SAVECTX_MASK, s_restore_trapsts
  840. s_lshr_b32 s_restore_m0, s_restore_m0, SQ_WAVE_TRAPSTS_POST_SAVECTX_SHIFT
  841. s_setreg_b32 hwreg(HW_REG_TRAPSTS, SQ_WAVE_TRAPSTS_POST_SAVECTX_SHIFT, SQ_WAVE_TRAPSTS_POST_SAVECTX_SIZE), s_restore_m0
  842. //s_setreg_b32 hwreg(HW_REG_TRAPSTS), s_restore_trapsts //don't overwrite SAVECTX bit as it may be set through external SAVECTX during restore
  843. s_setreg_b32 hwreg(HW_REG_MODE), s_restore_mode
  844. //reuse s_restore_m0 as a temp register
  845. s_and_b32 s_restore_m0, s_restore_pc_hi, S_SAVE_PC_HI_RCNT_MASK
  846. s_lshr_b32 s_restore_m0, s_restore_m0, S_SAVE_PC_HI_RCNT_SHIFT
  847. s_lshl_b32 s_restore_m0, s_restore_m0, SQ_WAVE_IB_STS_RCNT_SHIFT
  848. s_mov_b32 s_restore_tmp, 0x0 //IB_STS is zero
  849. s_or_b32 s_restore_tmp, s_restore_tmp, s_restore_m0
  850. s_and_b32 s_restore_m0, s_restore_pc_hi, S_SAVE_PC_HI_FIRST_REPLAY_MASK
  851. s_lshr_b32 s_restore_m0, s_restore_m0, S_SAVE_PC_HI_FIRST_REPLAY_SHIFT
  852. s_lshl_b32 s_restore_m0, s_restore_m0, SQ_WAVE_IB_STS_FIRST_REPLAY_SHIFT
  853. s_or_b32 s_restore_tmp, s_restore_tmp, s_restore_m0
  854. s_and_b32 s_restore_m0, s_restore_status, SQ_WAVE_STATUS_INST_ATC_MASK
  855. s_lshr_b32 s_restore_m0, s_restore_m0, SQ_WAVE_STATUS_INST_ATC_SHIFT
  856. s_setreg_b32 hwreg(HW_REG_IB_STS), s_restore_tmp
  857. s_and_b32 s_restore_pc_hi, s_restore_pc_hi, 0x0000ffff //pc[47:32] //Do it here in order not to affect STATUS
  858. s_and_b64 exec, exec, exec // Restore STATUS.EXECZ, not writable by s_setreg_b32
  859. s_and_b64 vcc, vcc, vcc // Restore STATUS.VCCZ, not writable by s_setreg_b32
  860. set_status_without_spi_prio(s_restore_status, s_restore_tmp) // SCC is included, which is changed by previous salu
  861. s_barrier //barrier to ensure the readiness of LDS before access attempts from any other wave in the same TG //FIXME not performance-optimal at this time
  862. if G8SR_DEBUG_TIMESTAMP
  863. s_memrealtime s_g8sr_ts_restore_d
  864. s_waitcnt lgkmcnt(0)
  865. end
  866. // s_rfe_b64 s_restore_pc_lo //Return to the main shader program and resume execution
  867. s_rfe_restore_b64 s_restore_pc_lo, s_restore_m0 // s_restore_m0[0] is used to set STATUS.inst_atc
  868. /**************************************************************************/
  869. /* the END */
  870. /**************************************************************************/
  871. L_END_PGM:
  872. s_endpgm
  873. end
  874. /**************************************************************************/
  875. /* the helper functions */
  876. /**************************************************************************/
  877. //Only for save hwreg to mem
  878. function write_hwreg_to_mem(s, s_rsrc, s_mem_offset)
  879. s_mov_b32 exec_lo, m0 //assuming exec_lo is not needed anymore from this point on
  880. s_mov_b32 m0, s_mem_offset
  881. s_buffer_store_dword s, s_rsrc, m0 glc:1
  882. s_add_u32 s_mem_offset, s_mem_offset, 4
  883. s_mov_b32 m0, exec_lo
  884. end
  885. // HWREG are saved before SGPRs, so all HWREG could be use.
  886. function write_16sgpr_to_mem(s, s_rsrc, s_mem_offset)
  887. s_buffer_store_dwordx4 s[0], s_rsrc, 0 glc:1
  888. s_buffer_store_dwordx4 s[4], s_rsrc, 16 glc:1
  889. s_buffer_store_dwordx4 s[8], s_rsrc, 32 glc:1
  890. s_buffer_store_dwordx4 s[12], s_rsrc, 48 glc:1
  891. s_add_u32 s_rsrc[0], s_rsrc[0], 4*16
  892. s_addc_u32 s_rsrc[1], s_rsrc[1], 0x0 // +scc
  893. end
  894. function read_hwreg_from_mem(s, s_rsrc, s_mem_offset)
  895. s_buffer_load_dword s, s_rsrc, s_mem_offset glc:1
  896. s_add_u32 s_mem_offset, s_mem_offset, 4
  897. end
  898. function read_16sgpr_from_mem(s, s_rsrc, s_mem_offset)
  899. s_buffer_load_dwordx16 s, s_rsrc, s_mem_offset glc:1
  900. s_sub_u32 s_mem_offset, s_mem_offset, 4*16
  901. end
  902. function get_lds_size_bytes(s_lds_size_byte)
  903. // SQ LDS granularity is 64DW, while PGM_RSRC2.lds_size is in granularity 128DW
  904. s_getreg_b32 s_lds_size_byte, hwreg(HW_REG_LDS_ALLOC, SQ_WAVE_LDS_ALLOC_LDS_SIZE_SHIFT, SQ_WAVE_LDS_ALLOC_LDS_SIZE_SIZE) // lds_size
  905. s_lshl_b32 s_lds_size_byte, s_lds_size_byte, 8 //LDS size in dwords = lds_size * 64 *4Bytes // granularity 64DW
  906. end
  907. function get_vgpr_size_bytes(s_vgpr_size_byte)
  908. s_getreg_b32 s_vgpr_size_byte, hwreg(HW_REG_GPR_ALLOC,SQ_WAVE_GPR_ALLOC_VGPR_SIZE_SHIFT,SQ_WAVE_GPR_ALLOC_VGPR_SIZE_SIZE) //vpgr_size
  909. s_add_u32 s_vgpr_size_byte, s_vgpr_size_byte, 1
  910. s_lshl_b32 s_vgpr_size_byte, s_vgpr_size_byte, (2+8) //Number of VGPRs = (vgpr_size + 1) * 4 * 64 * 4 (non-zero value) //FIXME for GFX, zero is possible
  911. end
  912. function get_sgpr_size_bytes(s_sgpr_size_byte)
  913. s_getreg_b32 s_sgpr_size_byte, hwreg(HW_REG_GPR_ALLOC,SQ_WAVE_GPR_ALLOC_SGPR_SIZE_SHIFT,SQ_WAVE_GPR_ALLOC_SGPR_SIZE_SIZE) //spgr_size
  914. s_add_u32 s_sgpr_size_byte, s_sgpr_size_byte, 1
  915. s_lshl_b32 s_sgpr_size_byte, s_sgpr_size_byte, 6 //Number of SGPRs = (sgpr_size + 1) * 16 *4 (non-zero value)
  916. end
  917. function get_hwreg_size_bytes
  918. return 128 //HWREG size 128 bytes
  919. end
  920. function set_status_without_spi_prio(status, tmp)
  921. // Do not restore STATUS.SPI_PRIO since scheduler may have raised it.
  922. s_lshr_b32 tmp, status, SQ_WAVE_STATUS_POST_SPI_PRIO_SHIFT
  923. s_setreg_b32 hwreg(HW_REG_STATUS, SQ_WAVE_STATUS_POST_SPI_PRIO_SHIFT, SQ_WAVE_STATUS_POST_SPI_PRIO_SIZE), tmp
  924. s_nop 0x2 // avoid S_SETREG => S_SETREG hazard
  925. s_setreg_b32 hwreg(HW_REG_STATUS, SQ_WAVE_STATUS_PRE_SPI_PRIO_SHIFT, SQ_WAVE_STATUS_PRE_SPI_PRIO_SIZE), status
  926. end