cwsr_trap_handler_gfx9.asm 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  1. /*
  2. * Copyright 2016 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=greenland ./sp3 cwsr_trap_handler_gfx9.asm -hex tmp.hex
  24. */
  25. /* HW (GFX9) 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/gfx9/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 SAVE_LDS = 1
  77. var WG_BASE_ADDR_LO = 0x9000a000
  78. var WG_BASE_ADDR_HI = 0x0
  79. var WAVE_SPACE = 0x5000 //memory size that each wave occupies in workgroup state mem
  80. var CTX_SAVE_CONTROL = 0x0
  81. var CTX_RESTORE_CONTROL = CTX_SAVE_CONTROL
  82. 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)
  83. var SGPR_SAVE_USE_SQC = 1 //use SQC D$ to do the write
  84. 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
  85. var SWIZZLE_EN = 0 //whether we use swizzled buffer addressing
  86. var ACK_SQC_STORE = 1 //workaround for suspected SQC store bug causing incorrect stores under concurrency
  87. /**************************************************************************/
  88. /* variables */
  89. /**************************************************************************/
  90. var SQ_WAVE_STATUS_INST_ATC_SHIFT = 23
  91. var SQ_WAVE_STATUS_INST_ATC_MASK = 0x00800000
  92. var SQ_WAVE_STATUS_SPI_PRIO_SHIFT = 1
  93. var SQ_WAVE_STATUS_SPI_PRIO_MASK = 0x00000006
  94. var SQ_WAVE_STATUS_HALT_MASK = 0x2000
  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_TRAPSTS_ILLEGAL_INST_MASK = 0x800
  117. var SQ_WAVE_IB_STS_RCNT_SHIFT = 16 //FIXME
  118. var SQ_WAVE_IB_STS_FIRST_REPLAY_SHIFT = 15 //FIXME
  119. var SQ_WAVE_IB_STS_RCNT_FIRST_REPLAY_MASK = 0x1F8000
  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. var TTMP11_SAVE_RCNT_FIRST_REPLAY_SHIFT = 26 // bits [31:26] unused by SPI debug data
  124. var TTMP11_SAVE_RCNT_FIRST_REPLAY_MASK = 0xFC000000
  125. /* Save */
  126. var S_SAVE_BUF_RSRC_WORD1_STRIDE = 0x00040000 //stride is 4 bytes
  127. 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
  128. var S_SAVE_SPI_INIT_ATC_MASK = 0x08000000 //bit[27]: ATC bit
  129. var S_SAVE_SPI_INIT_ATC_SHIFT = 27
  130. var S_SAVE_SPI_INIT_MTYPE_MASK = 0x70000000 //bit[30:28]: Mtype
  131. var S_SAVE_SPI_INIT_MTYPE_SHIFT = 28
  132. var S_SAVE_SPI_INIT_FIRST_WAVE_MASK = 0x04000000 //bit[26]: FirstWaveInTG
  133. var S_SAVE_SPI_INIT_FIRST_WAVE_SHIFT = 26
  134. var S_SAVE_PC_HI_RCNT_SHIFT = 28 //FIXME check with Brian to ensure all fields other than PC[47:0] can be used
  135. var S_SAVE_PC_HI_RCNT_MASK = 0xF0000000 //FIXME
  136. var S_SAVE_PC_HI_FIRST_REPLAY_SHIFT = 27 //FIXME
  137. var S_SAVE_PC_HI_FIRST_REPLAY_MASK = 0x08000000 //FIXME
  138. var s_save_spi_init_lo = exec_lo
  139. var s_save_spi_init_hi = exec_hi
  140. var s_save_pc_lo = ttmp0 //{TTMP1, TTMP0} = {3'h0,pc_rewind[3:0], HT[0],trapID[7:0], PC[47:0]}
  141. var s_save_pc_hi = ttmp1
  142. var s_save_exec_lo = ttmp2
  143. var s_save_exec_hi = ttmp3
  144. var s_save_tmp = ttmp4
  145. var s_save_trapsts = ttmp5 //not really used until the end of the SAVE routine
  146. var s_save_xnack_mask_lo = ttmp6
  147. var s_save_xnack_mask_hi = ttmp7
  148. var s_save_buf_rsrc0 = ttmp8
  149. var s_save_buf_rsrc1 = ttmp9
  150. var s_save_buf_rsrc2 = ttmp10
  151. var s_save_buf_rsrc3 = ttmp11
  152. var s_save_status = ttmp12
  153. var s_save_mem_offset = ttmp14
  154. var s_save_alloc_size = s_save_trapsts //conflict
  155. var s_save_m0 = ttmp15
  156. var s_save_ttmps_lo = s_save_tmp //no conflict
  157. var s_save_ttmps_hi = s_save_trapsts //no conflict
  158. /* Restore */
  159. var S_RESTORE_BUF_RSRC_WORD1_STRIDE = S_SAVE_BUF_RSRC_WORD1_STRIDE
  160. var S_RESTORE_BUF_RSRC_WORD3_MISC = S_SAVE_BUF_RSRC_WORD3_MISC
  161. var S_RESTORE_SPI_INIT_ATC_MASK = 0x08000000 //bit[27]: ATC bit
  162. var S_RESTORE_SPI_INIT_ATC_SHIFT = 27
  163. var S_RESTORE_SPI_INIT_MTYPE_MASK = 0x70000000 //bit[30:28]: Mtype
  164. var S_RESTORE_SPI_INIT_MTYPE_SHIFT = 28
  165. var S_RESTORE_SPI_INIT_FIRST_WAVE_MASK = 0x04000000 //bit[26]: FirstWaveInTG
  166. var S_RESTORE_SPI_INIT_FIRST_WAVE_SHIFT = 26
  167. var S_RESTORE_PC_HI_RCNT_SHIFT = S_SAVE_PC_HI_RCNT_SHIFT
  168. var S_RESTORE_PC_HI_RCNT_MASK = S_SAVE_PC_HI_RCNT_MASK
  169. var S_RESTORE_PC_HI_FIRST_REPLAY_SHIFT = S_SAVE_PC_HI_FIRST_REPLAY_SHIFT
  170. var S_RESTORE_PC_HI_FIRST_REPLAY_MASK = S_SAVE_PC_HI_FIRST_REPLAY_MASK
  171. var s_restore_spi_init_lo = exec_lo
  172. var s_restore_spi_init_hi = exec_hi
  173. var s_restore_mem_offset = ttmp12
  174. var s_restore_alloc_size = ttmp3
  175. var s_restore_tmp = ttmp2
  176. var s_restore_mem_offset_save = s_restore_tmp //no conflict
  177. var s_restore_m0 = s_restore_alloc_size //no conflict
  178. var s_restore_mode = ttmp7
  179. var s_restore_pc_lo = ttmp0
  180. var s_restore_pc_hi = ttmp1
  181. var s_restore_exec_lo = ttmp14
  182. var s_restore_exec_hi = ttmp15
  183. var s_restore_status = ttmp4
  184. var s_restore_trapsts = ttmp5
  185. var s_restore_xnack_mask_lo = xnack_mask_lo
  186. var s_restore_xnack_mask_hi = xnack_mask_hi
  187. var s_restore_buf_rsrc0 = ttmp8
  188. var s_restore_buf_rsrc1 = ttmp9
  189. var s_restore_buf_rsrc2 = ttmp10
  190. var s_restore_buf_rsrc3 = ttmp11
  191. var s_restore_ttmps_lo = s_restore_tmp //no conflict
  192. var s_restore_ttmps_hi = s_restore_alloc_size //no conflict
  193. /**************************************************************************/
  194. /* trap handler entry points */
  195. /**************************************************************************/
  196. /* Shader Main*/
  197. shader main
  198. asic(GFX9)
  199. type(CS)
  200. if ((EMU_RUN_HACK) && (!EMU_RUN_HACK_RESTORE_NORMAL)) //hack to use trap_id for determining save/restore
  201. //FIXME VCCZ un-init assertion s_getreg_b32 s_save_status, hwreg(HW_REG_STATUS) //save STATUS since we will change SCC
  202. s_and_b32 s_save_tmp, s_save_pc_hi, 0xffff0000 //change SCC
  203. s_cmp_eq_u32 s_save_tmp, 0x007e0000 //Save: trap_id = 0x7e. Restore: trap_id = 0x7f.
  204. s_cbranch_scc0 L_JUMP_TO_RESTORE //do not need to recover STATUS here since we are going to RESTORE
  205. //FIXME s_setreg_b32 hwreg(HW_REG_STATUS), s_save_status //need to recover STATUS since we are going to SAVE
  206. s_branch L_SKIP_RESTORE //NOT restore, SAVE actually
  207. else
  208. s_branch L_SKIP_RESTORE //NOT restore. might be a regular trap or save
  209. end
  210. L_JUMP_TO_RESTORE:
  211. s_branch L_RESTORE //restore
  212. L_SKIP_RESTORE:
  213. s_getreg_b32 s_save_status, hwreg(HW_REG_STATUS) //save STATUS since we will change SCC
  214. s_andn2_b32 s_save_status, s_save_status, SQ_WAVE_STATUS_SPI_PRIO_MASK //check whether this is for save
  215. s_getreg_b32 s_save_trapsts, hwreg(HW_REG_TRAPSTS)
  216. s_and_b32 ttmp2, s_save_trapsts, SQ_WAVE_TRAPSTS_SAVECTX_MASK //check whether this is for save
  217. s_cbranch_scc1 L_SAVE //this is the operation for save
  218. // ********* Handle non-CWSR traps *******************
  219. if (!EMU_RUN_HACK)
  220. // Illegal instruction is a non-maskable exception which blocks context save.
  221. // Halt the wavefront and return from the trap.
  222. s_and_b32 ttmp2, s_save_trapsts, SQ_WAVE_TRAPSTS_ILLEGAL_INST_MASK
  223. s_cbranch_scc1 L_HALT_WAVE
  224. // If STATUS.MEM_VIOL is asserted then we cannot fetch from the TMA.
  225. // Instead, halt the wavefront and return from the trap.
  226. s_and_b32 ttmp2, s_save_trapsts, SQ_WAVE_TRAPSTS_MEM_VIOL_MASK
  227. s_cbranch_scc0 L_FETCH_2ND_TRAP
  228. L_HALT_WAVE:
  229. // If STATUS.HALT is set then this fault must come from SQC instruction fetch.
  230. // We cannot prevent further faults so just terminate the wavefront.
  231. s_and_b32 ttmp2, s_save_status, SQ_WAVE_STATUS_HALT_MASK
  232. s_cbranch_scc0 L_NOT_ALREADY_HALTED
  233. s_endpgm
  234. L_NOT_ALREADY_HALTED:
  235. s_or_b32 s_save_status, s_save_status, SQ_WAVE_STATUS_HALT_MASK
  236. // If the PC points to S_ENDPGM then context save will fail if STATUS.HALT is set.
  237. // Rewind the PC to prevent this from occurring. The debugger compensates for this.
  238. s_sub_u32 ttmp0, ttmp0, 0x8
  239. s_subb_u32 ttmp1, ttmp1, 0x0
  240. L_FETCH_2ND_TRAP:
  241. // Preserve and clear scalar XNACK state before issuing scalar reads.
  242. // Save IB_STS.FIRST_REPLAY[15] and IB_STS.RCNT[20:16] into unused space ttmp11[31:26].
  243. s_getreg_b32 ttmp2, hwreg(HW_REG_IB_STS)
  244. s_and_b32 ttmp3, ttmp2, SQ_WAVE_IB_STS_RCNT_FIRST_REPLAY_MASK
  245. s_lshl_b32 ttmp3, ttmp3, (TTMP11_SAVE_RCNT_FIRST_REPLAY_SHIFT - SQ_WAVE_IB_STS_FIRST_REPLAY_SHIFT)
  246. s_andn2_b32 ttmp11, ttmp11, TTMP11_SAVE_RCNT_FIRST_REPLAY_MASK
  247. s_or_b32 ttmp11, ttmp11, ttmp3
  248. s_andn2_b32 ttmp2, ttmp2, SQ_WAVE_IB_STS_RCNT_FIRST_REPLAY_MASK
  249. s_setreg_b32 hwreg(HW_REG_IB_STS), ttmp2
  250. // Read second-level TBA/TMA from first-level TMA and jump if available.
  251. // ttmp[2:5] and ttmp12 can be used (others hold SPI-initialized debug data)
  252. // ttmp12 holds SQ_WAVE_STATUS
  253. s_getreg_b32 ttmp4, hwreg(HW_REG_SQ_SHADER_TMA_LO)
  254. s_getreg_b32 ttmp5, hwreg(HW_REG_SQ_SHADER_TMA_HI)
  255. s_lshl_b64 [ttmp4, ttmp5], [ttmp4, ttmp5], 0x8
  256. s_load_dwordx2 [ttmp2, ttmp3], [ttmp4, ttmp5], 0x0 glc:1 // second-level TBA
  257. s_waitcnt lgkmcnt(0)
  258. s_load_dwordx2 [ttmp4, ttmp5], [ttmp4, ttmp5], 0x8 glc:1 // second-level TMA
  259. s_waitcnt lgkmcnt(0)
  260. s_and_b64 [ttmp2, ttmp3], [ttmp2, ttmp3], [ttmp2, ttmp3]
  261. s_cbranch_scc0 L_NO_NEXT_TRAP // second-level trap handler not been set
  262. s_setpc_b64 [ttmp2, ttmp3] // jump to second-level trap handler
  263. L_NO_NEXT_TRAP:
  264. s_getreg_b32 s_save_trapsts, hwreg(HW_REG_TRAPSTS)
  265. s_and_b32 s_save_trapsts, s_save_trapsts, SQ_WAVE_TRAPSTS_EXCE_MASK // Check whether it is an exception
  266. s_cbranch_scc1 L_EXCP_CASE // Exception, jump back to the shader program directly.
  267. s_add_u32 ttmp0, ttmp0, 4 // S_TRAP case, add 4 to ttmp0
  268. s_addc_u32 ttmp1, ttmp1, 0
  269. L_EXCP_CASE:
  270. s_and_b32 ttmp1, ttmp1, 0xFFFF
  271. // Restore SQ_WAVE_IB_STS.
  272. s_lshr_b32 ttmp2, ttmp11, (TTMP11_SAVE_RCNT_FIRST_REPLAY_SHIFT - SQ_WAVE_IB_STS_FIRST_REPLAY_SHIFT)
  273. s_and_b32 ttmp2, ttmp2, SQ_WAVE_IB_STS_RCNT_FIRST_REPLAY_MASK
  274. s_setreg_b32 hwreg(HW_REG_IB_STS), ttmp2
  275. // Restore SQ_WAVE_STATUS.
  276. s_and_b64 exec, exec, exec // Restore STATUS.EXECZ, not writable by s_setreg_b32
  277. s_and_b64 vcc, vcc, vcc // Restore STATUS.VCCZ, not writable by s_setreg_b32
  278. set_status_without_spi_prio(s_save_status, ttmp2)
  279. s_rfe_b64 [ttmp0, ttmp1]
  280. end
  281. // ********* End handling of non-CWSR traps *******************
  282. /**************************************************************************/
  283. /* save routine */
  284. /**************************************************************************/
  285. L_SAVE:
  286. if G8SR_DEBUG_TIMESTAMP
  287. s_memrealtime s_g8sr_ts_save_s
  288. s_waitcnt lgkmcnt(0) //FIXME, will cause xnack??
  289. end
  290. s_and_b32 s_save_pc_hi, s_save_pc_hi, 0x0000ffff //pc[47:32]
  291. s_mov_b32 s_save_tmp, 0 //clear saveCtx bit
  292. s_setreg_b32 hwreg(HW_REG_TRAPSTS, SQ_WAVE_TRAPSTS_SAVECTX_SHIFT, 1), s_save_tmp //clear saveCtx bit
  293. 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
  294. s_lshl_b32 s_save_tmp, s_save_tmp, S_SAVE_PC_HI_RCNT_SHIFT
  295. s_or_b32 s_save_pc_hi, s_save_pc_hi, s_save_tmp
  296. 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
  297. s_lshl_b32 s_save_tmp, s_save_tmp, S_SAVE_PC_HI_FIRST_REPLAY_SHIFT
  298. s_or_b32 s_save_pc_hi, s_save_pc_hi, s_save_tmp
  299. s_getreg_b32 s_save_tmp, hwreg(HW_REG_IB_STS) //clear RCNT and FIRST_REPLAY in IB_STS
  300. s_and_b32 s_save_tmp, s_save_tmp, SQ_WAVE_IB_STS_RCNT_FIRST_REPLAY_MASK_NEG
  301. s_setreg_b32 hwreg(HW_REG_IB_STS), s_save_tmp
  302. /* inform SPI the readiness and wait for SPI's go signal */
  303. s_mov_b32 s_save_exec_lo, exec_lo //save EXEC and use EXEC for the go signal from SPI
  304. s_mov_b32 s_save_exec_hi, exec_hi
  305. s_mov_b64 exec, 0x0 //clear EXEC to get ready to receive
  306. if G8SR_DEBUG_TIMESTAMP
  307. s_memrealtime s_g8sr_ts_sq_save_msg
  308. s_waitcnt lgkmcnt(0)
  309. end
  310. if (EMU_RUN_HACK)
  311. else
  312. s_sendmsg sendmsg(MSG_SAVEWAVE) //send SPI a message and wait for SPI's write to EXEC
  313. end
  314. // Set SPI_PRIO=2 to avoid starving instruction fetch in the waves we're waiting for.
  315. s_or_b32 s_save_tmp, s_save_status, (2 << SQ_WAVE_STATUS_SPI_PRIO_SHIFT)
  316. s_setreg_b32 hwreg(HW_REG_STATUS), s_save_tmp
  317. L_SLEEP:
  318. 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
  319. if (EMU_RUN_HACK)
  320. else
  321. s_cbranch_execz L_SLEEP
  322. end
  323. if G8SR_DEBUG_TIMESTAMP
  324. s_memrealtime s_g8sr_ts_spi_wrexec
  325. s_waitcnt lgkmcnt(0)
  326. end
  327. if ((EMU_RUN_HACK) && (!EMU_RUN_HACK_SAVE_SINGLE_WAVE))
  328. //calculate wd_addr using absolute thread id
  329. v_readlane_b32 s_save_tmp, v9, 0
  330. s_lshr_b32 s_save_tmp, s_save_tmp, 6
  331. s_mul_i32 s_save_tmp, s_save_tmp, WAVE_SPACE
  332. s_add_i32 s_save_spi_init_lo, s_save_tmp, WG_BASE_ADDR_LO
  333. s_mov_b32 s_save_spi_init_hi, WG_BASE_ADDR_HI
  334. s_and_b32 s_save_spi_init_hi, s_save_spi_init_hi, CTX_SAVE_CONTROL
  335. else
  336. end
  337. if ((EMU_RUN_HACK) && (EMU_RUN_HACK_SAVE_SINGLE_WAVE))
  338. s_add_i32 s_save_spi_init_lo, s_save_tmp, WG_BASE_ADDR_LO
  339. s_mov_b32 s_save_spi_init_hi, WG_BASE_ADDR_HI
  340. s_and_b32 s_save_spi_init_hi, s_save_spi_init_hi, CTX_SAVE_CONTROL
  341. else
  342. end
  343. // Save trap temporaries 6-11, 13-15 initialized by SPI debug dispatch logic
  344. // ttmp SR memory offset : size(VGPR)+size(SGPR)+0x40
  345. get_vgpr_size_bytes(s_save_ttmps_lo)
  346. get_sgpr_size_bytes(s_save_ttmps_hi)
  347. s_add_u32 s_save_ttmps_lo, s_save_ttmps_lo, s_save_ttmps_hi
  348. s_add_u32 s_save_ttmps_lo, s_save_ttmps_lo, s_save_spi_init_lo
  349. s_addc_u32 s_save_ttmps_hi, s_save_spi_init_hi, 0x0
  350. s_and_b32 s_save_ttmps_hi, s_save_ttmps_hi, 0xFFFF
  351. s_store_dwordx2 [ttmp6, ttmp7], [s_save_ttmps_lo, s_save_ttmps_hi], 0x40 glc:1
  352. ack_sqc_store_workaround()
  353. s_store_dwordx4 [ttmp8, ttmp9, ttmp10, ttmp11], [s_save_ttmps_lo, s_save_ttmps_hi], 0x48 glc:1
  354. ack_sqc_store_workaround()
  355. s_store_dword ttmp13, [s_save_ttmps_lo, s_save_ttmps_hi], 0x58 glc:1
  356. ack_sqc_store_workaround()
  357. s_store_dwordx2 [ttmp14, ttmp15], [s_save_ttmps_lo, s_save_ttmps_hi], 0x5C glc:1
  358. ack_sqc_store_workaround()
  359. /* setup Resource Contants */
  360. s_mov_b32 s_save_buf_rsrc0, s_save_spi_init_lo //base_addr_lo
  361. s_and_b32 s_save_buf_rsrc1, s_save_spi_init_hi, 0x0000FFFF //base_addr_hi
  362. s_or_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, S_SAVE_BUF_RSRC_WORD1_STRIDE
  363. s_mov_b32 s_save_buf_rsrc2, 0 //NUM_RECORDS initial value = 0 (in bytes) although not neccessarily inited
  364. s_mov_b32 s_save_buf_rsrc3, S_SAVE_BUF_RSRC_WORD3_MISC
  365. s_and_b32 s_save_tmp, s_save_spi_init_hi, S_SAVE_SPI_INIT_ATC_MASK
  366. 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
  367. s_or_b32 s_save_buf_rsrc3, s_save_buf_rsrc3, s_save_tmp //or ATC
  368. s_and_b32 s_save_tmp, s_save_spi_init_hi, S_SAVE_SPI_INIT_MTYPE_MASK
  369. 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
  370. s_or_b32 s_save_buf_rsrc3, s_save_buf_rsrc3, s_save_tmp //or MTYPE
  371. //FIXME right now s_save_m0/s_save_mem_offset use tma_lo/tma_hi (might need to save them before using them?)
  372. s_mov_b32 s_save_m0, m0 //save M0
  373. /* global mem offset */
  374. s_mov_b32 s_save_mem_offset, 0x0 //mem offset initial value = 0
  375. /* save HW registers */
  376. //////////////////////////////
  377. L_SAVE_HWREG:
  378. // HWREG SR memory offset : size(VGPR)+size(SGPR)
  379. get_vgpr_size_bytes(s_save_mem_offset)
  380. get_sgpr_size_bytes(s_save_tmp)
  381. s_add_u32 s_save_mem_offset, s_save_mem_offset, s_save_tmp
  382. s_mov_b32 s_save_buf_rsrc2, 0x4 //NUM_RECORDS in bytes
  383. if (SWIZZLE_EN)
  384. s_add_u32 s_save_buf_rsrc2, s_save_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  385. else
  386. s_mov_b32 s_save_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  387. end
  388. write_hwreg_to_mem(s_save_m0, s_save_buf_rsrc0, s_save_mem_offset) //M0
  389. if ((EMU_RUN_HACK) && (EMU_RUN_HACK_SAVE_FIRST_TIME))
  390. s_add_u32 s_save_pc_lo, s_save_pc_lo, 4 //pc[31:0]+4
  391. s_addc_u32 s_save_pc_hi, s_save_pc_hi, 0x0 //carry bit over
  392. end
  393. write_hwreg_to_mem(s_save_pc_lo, s_save_buf_rsrc0, s_save_mem_offset) //PC
  394. write_hwreg_to_mem(s_save_pc_hi, s_save_buf_rsrc0, s_save_mem_offset)
  395. write_hwreg_to_mem(s_save_exec_lo, s_save_buf_rsrc0, s_save_mem_offset) //EXEC
  396. write_hwreg_to_mem(s_save_exec_hi, s_save_buf_rsrc0, s_save_mem_offset)
  397. write_hwreg_to_mem(s_save_status, s_save_buf_rsrc0, s_save_mem_offset) //STATUS
  398. //s_save_trapsts conflicts with s_save_alloc_size
  399. s_getreg_b32 s_save_trapsts, hwreg(HW_REG_TRAPSTS)
  400. write_hwreg_to_mem(s_save_trapsts, s_save_buf_rsrc0, s_save_mem_offset) //TRAPSTS
  401. write_hwreg_to_mem(xnack_mask_lo, s_save_buf_rsrc0, s_save_mem_offset) //XNACK_MASK_LO
  402. write_hwreg_to_mem(xnack_mask_hi, s_save_buf_rsrc0, s_save_mem_offset) //XNACK_MASK_HI
  403. //use s_save_tmp would introduce conflict here between s_save_tmp and s_save_buf_rsrc2
  404. s_getreg_b32 s_save_m0, hwreg(HW_REG_MODE) //MODE
  405. write_hwreg_to_mem(s_save_m0, s_save_buf_rsrc0, s_save_mem_offset)
  406. /* the first wave in the threadgroup */
  407. s_and_b32 s_save_tmp, s_save_spi_init_hi, S_SAVE_SPI_INIT_FIRST_WAVE_MASK // extract fisrt wave bit
  408. s_mov_b32 s_save_exec_hi, 0x0
  409. 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]
  410. /* save SGPRs */
  411. // Save SGPR before LDS save, then the s0 to s4 can be used during LDS save...
  412. //////////////////////////////
  413. // SGPR SR memory offset : size(VGPR)
  414. get_vgpr_size_bytes(s_save_mem_offset)
  415. // TODO, change RSRC word to rearrange memory layout for SGPRS
  416. 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
  417. s_add_u32 s_save_alloc_size, s_save_alloc_size, 1
  418. s_lshl_b32 s_save_alloc_size, s_save_alloc_size, 4 //Number of SGPRs = (sgpr_size + 1) * 16 (non-zero value)
  419. if (SGPR_SAVE_USE_SQC)
  420. s_lshl_b32 s_save_buf_rsrc2, s_save_alloc_size, 2 //NUM_RECORDS in bytes
  421. else
  422. s_lshl_b32 s_save_buf_rsrc2, s_save_alloc_size, 8 //NUM_RECORDS in bytes (64 threads)
  423. end
  424. if (SWIZZLE_EN)
  425. s_add_u32 s_save_buf_rsrc2, s_save_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  426. else
  427. s_mov_b32 s_save_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  428. end
  429. // backup s_save_buf_rsrc0,1 to s_save_pc_lo/hi, since write_16sgpr_to_mem function will change the rsrc0
  430. //s_mov_b64 s_save_pc_lo, s_save_buf_rsrc0
  431. s_mov_b64 s_save_xnack_mask_lo, s_save_buf_rsrc0
  432. s_add_u32 s_save_buf_rsrc0, s_save_buf_rsrc0, s_save_mem_offset
  433. s_addc_u32 s_save_buf_rsrc1, s_save_buf_rsrc1, 0
  434. s_mov_b32 m0, 0x0 //SGPR initial index value =0
  435. s_nop 0x0 //Manually inserted wait states
  436. L_SAVE_SGPR_LOOP:
  437. // SGPR is allocated in 16 SGPR granularity
  438. s_movrels_b64 s0, s0 //s0 = s[0+m0], s1 = s[1+m0]
  439. s_movrels_b64 s2, s2 //s2 = s[2+m0], s3 = s[3+m0]
  440. s_movrels_b64 s4, s4 //s4 = s[4+m0], s5 = s[5+m0]
  441. s_movrels_b64 s6, s6 //s6 = s[6+m0], s7 = s[7+m0]
  442. s_movrels_b64 s8, s8 //s8 = s[8+m0], s9 = s[9+m0]
  443. s_movrels_b64 s10, s10 //s10 = s[10+m0], s11 = s[11+m0]
  444. s_movrels_b64 s12, s12 //s12 = s[12+m0], s13 = s[13+m0]
  445. s_movrels_b64 s14, s14 //s14 = s[14+m0], s15 = s[15+m0]
  446. write_16sgpr_to_mem(s0, s_save_buf_rsrc0, s_save_mem_offset) //PV: the best performance should be using s_buffer_store_dwordx4
  447. s_add_u32 m0, m0, 16 //next sgpr index
  448. s_cmp_lt_u32 m0, s_save_alloc_size //scc = (m0 < s_save_alloc_size) ? 1 : 0
  449. s_cbranch_scc1 L_SAVE_SGPR_LOOP //SGPR save is complete?
  450. // restore s_save_buf_rsrc0,1
  451. //s_mov_b64 s_save_buf_rsrc0, s_save_pc_lo
  452. s_mov_b64 s_save_buf_rsrc0, s_save_xnack_mask_lo
  453. /* save first 4 VGPR, then LDS save could use */
  454. // each wave will alloc 4 vgprs at least...
  455. /////////////////////////////////////////////////////////////////////////////////////
  456. s_mov_b32 s_save_mem_offset, 0
  457. s_mov_b32 exec_lo, 0xFFFFFFFF //need every thread from now on
  458. s_mov_b32 exec_hi, 0xFFFFFFFF
  459. s_mov_b32 xnack_mask_lo, 0x0
  460. s_mov_b32 xnack_mask_hi, 0x0
  461. if (SWIZZLE_EN)
  462. s_add_u32 s_save_buf_rsrc2, s_save_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  463. else
  464. s_mov_b32 s_save_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  465. end
  466. // VGPR Allocated in 4-GPR granularity
  467. if G8SR_VGPR_SR_IN_DWX4
  468. // the const stride for DWx4 is 4*4 bytes
  469. s_and_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, 0x0000FFFF // reset const stride to 0
  470. s_or_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, G8SR_SAVE_BUF_RSRC_WORD1_STRIDE_DWx4 // const stride to 4*4 bytes
  471. buffer_store_dwordx4 v0, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1
  472. s_and_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, 0x0000FFFF // reset const stride to 0
  473. s_or_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, S_SAVE_BUF_RSRC_WORD1_STRIDE // reset const stride to 4 bytes
  474. else
  475. buffer_store_dword v0, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1
  476. buffer_store_dword v1, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1 offset:256
  477. buffer_store_dword v2, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1 offset:256*2
  478. buffer_store_dword v3, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1 offset:256*3
  479. end
  480. /* save LDS */
  481. //////////////////////////////
  482. L_SAVE_LDS:
  483. // Change EXEC to all threads...
  484. s_mov_b32 exec_lo, 0xFFFFFFFF //need every thread from now on
  485. s_mov_b32 exec_hi, 0xFFFFFFFF
  486. 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
  487. s_and_b32 s_save_alloc_size, s_save_alloc_size, 0xFFFFFFFF //lds_size is zero?
  488. s_cbranch_scc0 L_SAVE_LDS_DONE //no lds used? jump to L_SAVE_DONE
  489. s_barrier //LDS is used? wait for other waves in the same TG
  490. s_and_b32 s_save_tmp, s_save_exec_hi, S_SAVE_SPI_INIT_FIRST_WAVE_MASK //exec is still used here
  491. s_cbranch_scc0 L_SAVE_LDS_DONE
  492. // first wave do LDS save;
  493. s_lshl_b32 s_save_alloc_size, s_save_alloc_size, 6 //LDS size in dwords = lds_size * 64dw
  494. s_lshl_b32 s_save_alloc_size, s_save_alloc_size, 2 //LDS size in bytes
  495. s_mov_b32 s_save_buf_rsrc2, s_save_alloc_size //NUM_RECORDS in bytes
  496. // LDS at offset: size(VGPR)+SIZE(SGPR)+SIZE(HWREG)
  497. //
  498. get_vgpr_size_bytes(s_save_mem_offset)
  499. get_sgpr_size_bytes(s_save_tmp)
  500. s_add_u32 s_save_mem_offset, s_save_mem_offset, s_save_tmp
  501. s_add_u32 s_save_mem_offset, s_save_mem_offset, get_hwreg_size_bytes()
  502. if (SWIZZLE_EN)
  503. s_add_u32 s_save_buf_rsrc2, s_save_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  504. else
  505. s_mov_b32 s_save_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  506. end
  507. s_mov_b32 m0, 0x0 //lds_offset initial value = 0
  508. var LDS_DMA_ENABLE = 0
  509. var UNROLL = 0
  510. if UNROLL==0 && LDS_DMA_ENABLE==1
  511. s_mov_b32 s3, 256*2
  512. s_nop 0
  513. s_nop 0
  514. s_nop 0
  515. L_SAVE_LDS_LOOP:
  516. //TODO: looks the 2 buffer_store/load clause for s/r will hurt performance.???
  517. if (SAVE_LDS) //SPI always alloc LDS space in 128DW granularity
  518. buffer_store_lds_dword s_save_buf_rsrc0, s_save_mem_offset lds:1 // first 64DW
  519. buffer_store_lds_dword s_save_buf_rsrc0, s_save_mem_offset lds:1 offset:256 // second 64DW
  520. end
  521. s_add_u32 m0, m0, s3 //every buffer_store_lds does 256 bytes
  522. s_add_u32 s_save_mem_offset, s_save_mem_offset, s3 //mem offset increased by 256 bytes
  523. s_cmp_lt_u32 m0, s_save_alloc_size //scc=(m0 < s_save_alloc_size) ? 1 : 0
  524. s_cbranch_scc1 L_SAVE_LDS_LOOP //LDS save is complete?
  525. elsif LDS_DMA_ENABLE==1 && UNROLL==1 // UNROOL , has ichace miss
  526. // store from higest LDS address to lowest
  527. s_mov_b32 s3, 256*2
  528. s_sub_u32 m0, s_save_alloc_size, s3
  529. s_add_u32 s_save_mem_offset, s_save_mem_offset, m0
  530. s_lshr_b32 s_save_alloc_size, s_save_alloc_size, 9 // how many 128 trunks...
  531. s_sub_u32 s_save_alloc_size, 128, s_save_alloc_size // store from higheset addr to lowest
  532. 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
  533. s_add_u32 s_save_alloc_size, s_save_alloc_size, 3*4 //2is the below 2 inst...//s_addc and s_setpc
  534. s_nop 0
  535. s_nop 0
  536. s_nop 0 //pad 3 dw to let LDS_DMA align with 64Bytes
  537. s_getpc_b64 s[0:1] // reuse s[0:1], since s[0:1] already saved
  538. s_add_u32 s0, s0,s_save_alloc_size
  539. s_addc_u32 s1, s1, 0
  540. s_setpc_b64 s[0:1]
  541. for var i =0; i< 128; i++
  542. // be careful to make here a 64Byte aligned address, which could improve performance...
  543. buffer_store_lds_dword s_save_buf_rsrc0, s_save_mem_offset lds:1 offset:0 // first 64DW
  544. buffer_store_lds_dword s_save_buf_rsrc0, s_save_mem_offset lds:1 offset:256 // second 64DW
  545. if i!=127
  546. 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
  547. s_sub_u32 s_save_mem_offset, s_save_mem_offset, s3
  548. end
  549. end
  550. else // BUFFER_STORE
  551. v_mbcnt_lo_u32_b32 v2, 0xffffffff, 0x0
  552. v_mbcnt_hi_u32_b32 v3, 0xffffffff, v2 // tid
  553. v_mul_i32_i24 v2, v3, 8 // tid*8
  554. v_mov_b32 v3, 256*2
  555. s_mov_b32 m0, 0x10000
  556. s_mov_b32 s0, s_save_buf_rsrc3
  557. s_and_b32 s_save_buf_rsrc3, s_save_buf_rsrc3, 0xFF7FFFFF // disable add_tid
  558. s_or_b32 s_save_buf_rsrc3, s_save_buf_rsrc3, 0x58000 //DFMT
  559. L_SAVE_LDS_LOOP_VECTOR:
  560. ds_read_b64 v[0:1], v2 //x =LDS[a], byte address
  561. s_waitcnt lgkmcnt(0)
  562. buffer_store_dwordx2 v[0:1], v2, s_save_buf_rsrc0, s_save_mem_offset offen:1 glc:1 slc:1
  563. // s_waitcnt vmcnt(0)
  564. // v_add_u32 v2, vcc[0:1], v2, v3
  565. v_add_u32 v2, v2, v3
  566. v_cmp_lt_u32 vcc[0:1], v2, s_save_alloc_size
  567. s_cbranch_vccnz L_SAVE_LDS_LOOP_VECTOR
  568. // restore rsrc3
  569. s_mov_b32 s_save_buf_rsrc3, s0
  570. end
  571. L_SAVE_LDS_DONE:
  572. /* save VGPRs - set the Rest VGPRs */
  573. //////////////////////////////////////////////////////////////////////////////////////
  574. L_SAVE_VGPR:
  575. // VGPR SR memory offset: 0
  576. // TODO rearrange the RSRC words to use swizzle for VGPR save...
  577. s_mov_b32 s_save_mem_offset, (0+256*4) // for the rest VGPRs
  578. s_mov_b32 exec_lo, 0xFFFFFFFF //need every thread from now on
  579. s_mov_b32 exec_hi, 0xFFFFFFFF
  580. 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
  581. s_add_u32 s_save_alloc_size, s_save_alloc_size, 1
  582. 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
  583. s_lshl_b32 s_save_buf_rsrc2, s_save_alloc_size, 8 //NUM_RECORDS in bytes (64 threads*4)
  584. if (SWIZZLE_EN)
  585. s_add_u32 s_save_buf_rsrc2, s_save_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  586. else
  587. s_mov_b32 s_save_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  588. end
  589. // VGPR Allocated in 4-GPR granularity
  590. if G8SR_VGPR_SR_IN_DWX4
  591. // the const stride for DWx4 is 4*4 bytes
  592. s_and_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, 0x0000FFFF // reset const stride to 0
  593. s_or_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, G8SR_SAVE_BUF_RSRC_WORD1_STRIDE_DWx4 // const stride to 4*4 bytes
  594. s_mov_b32 m0, 4 // skip first 4 VGPRs
  595. s_cmp_lt_u32 m0, s_save_alloc_size
  596. s_cbranch_scc0 L_SAVE_VGPR_LOOP_END // no more vgprs
  597. s_set_gpr_idx_on m0, 0x1 // This will change M0
  598. s_add_u32 s_save_alloc_size, s_save_alloc_size, 0x1000 // because above inst change m0
  599. L_SAVE_VGPR_LOOP:
  600. v_mov_b32 v0, v0 // v0 = v[0+m0]
  601. v_mov_b32 v1, v1
  602. v_mov_b32 v2, v2
  603. v_mov_b32 v3, v3
  604. buffer_store_dwordx4 v0, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1
  605. s_add_u32 m0, m0, 4
  606. s_add_u32 s_save_mem_offset, s_save_mem_offset, 256*4
  607. s_cmp_lt_u32 m0, s_save_alloc_size
  608. s_cbranch_scc1 L_SAVE_VGPR_LOOP //VGPR save is complete?
  609. s_set_gpr_idx_off
  610. L_SAVE_VGPR_LOOP_END:
  611. s_and_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, 0x0000FFFF // reset const stride to 0
  612. s_or_b32 s_save_buf_rsrc1, s_save_buf_rsrc1, S_SAVE_BUF_RSRC_WORD1_STRIDE // reset const stride to 4 bytes
  613. else
  614. // VGPR store using dw burst
  615. s_mov_b32 m0, 0x4 //VGPR initial index value =0
  616. s_cmp_lt_u32 m0, s_save_alloc_size
  617. s_cbranch_scc0 L_SAVE_VGPR_END
  618. s_set_gpr_idx_on m0, 0x1 //M0[7:0] = M0[7:0] and M0[15:12] = 0x1
  619. s_add_u32 s_save_alloc_size, s_save_alloc_size, 0x1000 //add 0x1000 since we compare m0 against it later
  620. L_SAVE_VGPR_LOOP:
  621. v_mov_b32 v0, v0 //v0 = v[0+m0]
  622. v_mov_b32 v1, v1 //v0 = v[0+m0]
  623. v_mov_b32 v2, v2 //v0 = v[0+m0]
  624. v_mov_b32 v3, v3 //v0 = v[0+m0]
  625. if(USE_MTBUF_INSTEAD_OF_MUBUF)
  626. 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
  627. else
  628. buffer_store_dword v0, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1
  629. buffer_store_dword v1, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1 offset:256
  630. buffer_store_dword v2, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1 offset:256*2
  631. buffer_store_dword v3, v0, s_save_buf_rsrc0, s_save_mem_offset slc:1 glc:1 offset:256*3
  632. end
  633. s_add_u32 m0, m0, 4 //next vgpr index
  634. s_add_u32 s_save_mem_offset, s_save_mem_offset, 256*4 //every buffer_store_dword does 256 bytes
  635. s_cmp_lt_u32 m0, s_save_alloc_size //scc = (m0 < s_save_alloc_size) ? 1 : 0
  636. s_cbranch_scc1 L_SAVE_VGPR_LOOP //VGPR save is complete?
  637. s_set_gpr_idx_off
  638. end
  639. L_SAVE_VGPR_END:
  640. /* S_PGM_END_SAVED */ //FIXME graphics ONLY
  641. if ((EMU_RUN_HACK) && (!EMU_RUN_HACK_SAVE_NORMAL_EXIT))
  642. s_and_b32 s_save_pc_hi, s_save_pc_hi, 0x0000ffff //pc[47:32]
  643. s_add_u32 s_save_pc_lo, s_save_pc_lo, 4 //pc[31:0]+4
  644. s_addc_u32 s_save_pc_hi, s_save_pc_hi, 0x0 //carry bit over
  645. s_rfe_b64 s_save_pc_lo //Return to the main shader program
  646. else
  647. end
  648. // Save Done timestamp
  649. if G8SR_DEBUG_TIMESTAMP
  650. s_memrealtime s_g8sr_ts_save_d
  651. // SGPR SR memory offset : size(VGPR)
  652. get_vgpr_size_bytes(s_save_mem_offset)
  653. s_add_u32 s_save_mem_offset, s_save_mem_offset, G8SR_DEBUG_TS_SAVE_D_OFFSET
  654. s_waitcnt lgkmcnt(0) //FIXME, will cause xnack??
  655. // Need reset rsrc2??
  656. s_mov_b32 m0, s_save_mem_offset
  657. s_mov_b32 s_save_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  658. s_buffer_store_dwordx2 s_g8sr_ts_save_d, s_save_buf_rsrc0, m0 glc:1
  659. end
  660. s_branch L_END_PGM
  661. /**************************************************************************/
  662. /* restore routine */
  663. /**************************************************************************/
  664. L_RESTORE:
  665. /* Setup Resource Contants */
  666. if ((EMU_RUN_HACK) && (!EMU_RUN_HACK_RESTORE_NORMAL))
  667. //calculate wd_addr using absolute thread id
  668. v_readlane_b32 s_restore_tmp, v9, 0
  669. s_lshr_b32 s_restore_tmp, s_restore_tmp, 6
  670. s_mul_i32 s_restore_tmp, s_restore_tmp, WAVE_SPACE
  671. s_add_i32 s_restore_spi_init_lo, s_restore_tmp, WG_BASE_ADDR_LO
  672. s_mov_b32 s_restore_spi_init_hi, WG_BASE_ADDR_HI
  673. s_and_b32 s_restore_spi_init_hi, s_restore_spi_init_hi, CTX_RESTORE_CONTROL
  674. else
  675. end
  676. if G8SR_DEBUG_TIMESTAMP
  677. s_memrealtime s_g8sr_ts_restore_s
  678. s_waitcnt lgkmcnt(0) //FIXME, will cause xnack??
  679. // tma_lo/hi are sgpr 110, 111, which will not used for 112 SGPR allocated case...
  680. s_mov_b32 s_restore_pc_lo, s_g8sr_ts_restore_s[0]
  681. s_mov_b32 s_restore_pc_hi, s_g8sr_ts_restore_s[1] //backup ts to ttmp0/1, sicne exec will be finally restored..
  682. end
  683. s_mov_b32 s_restore_buf_rsrc0, s_restore_spi_init_lo //base_addr_lo
  684. s_and_b32 s_restore_buf_rsrc1, s_restore_spi_init_hi, 0x0000FFFF //base_addr_hi
  685. s_or_b32 s_restore_buf_rsrc1, s_restore_buf_rsrc1, S_RESTORE_BUF_RSRC_WORD1_STRIDE
  686. s_mov_b32 s_restore_buf_rsrc2, 0 //NUM_RECORDS initial value = 0 (in bytes)
  687. s_mov_b32 s_restore_buf_rsrc3, S_RESTORE_BUF_RSRC_WORD3_MISC
  688. s_and_b32 s_restore_tmp, s_restore_spi_init_hi, S_RESTORE_SPI_INIT_ATC_MASK
  689. 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
  690. s_or_b32 s_restore_buf_rsrc3, s_restore_buf_rsrc3, s_restore_tmp //or ATC
  691. s_and_b32 s_restore_tmp, s_restore_spi_init_hi, S_RESTORE_SPI_INIT_MTYPE_MASK
  692. 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
  693. s_or_b32 s_restore_buf_rsrc3, s_restore_buf_rsrc3, s_restore_tmp //or MTYPE
  694. /* global mem offset */
  695. // s_mov_b32 s_restore_mem_offset, 0x0 //mem offset initial value = 0
  696. /* the first wave in the threadgroup */
  697. s_and_b32 s_restore_tmp, s_restore_spi_init_hi, S_RESTORE_SPI_INIT_FIRST_WAVE_MASK
  698. s_cbranch_scc0 L_RESTORE_VGPR
  699. /* restore LDS */
  700. //////////////////////////////
  701. L_RESTORE_LDS:
  702. s_mov_b32 exec_lo, 0xFFFFFFFF //need every thread from now on //be consistent with SAVE although can be moved ahead
  703. s_mov_b32 exec_hi, 0xFFFFFFFF
  704. 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
  705. s_and_b32 s_restore_alloc_size, s_restore_alloc_size, 0xFFFFFFFF //lds_size is zero?
  706. s_cbranch_scc0 L_RESTORE_VGPR //no lds used? jump to L_RESTORE_VGPR
  707. s_lshl_b32 s_restore_alloc_size, s_restore_alloc_size, 6 //LDS size in dwords = lds_size * 64dw
  708. s_lshl_b32 s_restore_alloc_size, s_restore_alloc_size, 2 //LDS size in bytes
  709. s_mov_b32 s_restore_buf_rsrc2, s_restore_alloc_size //NUM_RECORDS in bytes
  710. // LDS at offset: size(VGPR)+SIZE(SGPR)+SIZE(HWREG)
  711. //
  712. get_vgpr_size_bytes(s_restore_mem_offset)
  713. get_sgpr_size_bytes(s_restore_tmp)
  714. s_add_u32 s_restore_mem_offset, s_restore_mem_offset, s_restore_tmp
  715. s_add_u32 s_restore_mem_offset, s_restore_mem_offset, get_hwreg_size_bytes() //FIXME, Check if offset overflow???
  716. if (SWIZZLE_EN)
  717. s_add_u32 s_restore_buf_rsrc2, s_restore_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  718. else
  719. s_mov_b32 s_restore_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  720. end
  721. s_mov_b32 m0, 0x0 //lds_offset initial value = 0
  722. L_RESTORE_LDS_LOOP:
  723. if (SAVE_LDS)
  724. buffer_load_dword v0, v0, s_restore_buf_rsrc0, s_restore_mem_offset lds:1 // first 64DW
  725. buffer_load_dword v0, v0, s_restore_buf_rsrc0, s_restore_mem_offset lds:1 offset:256 // second 64DW
  726. end
  727. s_add_u32 m0, m0, 256*2 // 128 DW
  728. s_add_u32 s_restore_mem_offset, s_restore_mem_offset, 256*2 //mem offset increased by 128DW
  729. s_cmp_lt_u32 m0, s_restore_alloc_size //scc=(m0 < s_restore_alloc_size) ? 1 : 0
  730. s_cbranch_scc1 L_RESTORE_LDS_LOOP //LDS restore is complete?
  731. /* restore VGPRs */
  732. //////////////////////////////
  733. L_RESTORE_VGPR:
  734. // VGPR SR memory offset : 0
  735. s_mov_b32 s_restore_mem_offset, 0x0
  736. s_mov_b32 exec_lo, 0xFFFFFFFF //need every thread from now on //be consistent with SAVE although can be moved ahead
  737. s_mov_b32 exec_hi, 0xFFFFFFFF
  738. 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
  739. s_add_u32 s_restore_alloc_size, s_restore_alloc_size, 1
  740. s_lshl_b32 s_restore_alloc_size, s_restore_alloc_size, 2 //Number of VGPRs = (vgpr_size + 1) * 4 (non-zero value)
  741. s_lshl_b32 s_restore_buf_rsrc2, s_restore_alloc_size, 8 //NUM_RECORDS in bytes (64 threads*4)
  742. if (SWIZZLE_EN)
  743. s_add_u32 s_restore_buf_rsrc2, s_restore_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  744. else
  745. s_mov_b32 s_restore_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  746. end
  747. if G8SR_VGPR_SR_IN_DWX4
  748. get_vgpr_size_bytes(s_restore_mem_offset)
  749. s_sub_u32 s_restore_mem_offset, s_restore_mem_offset, 256*4
  750. // the const stride for DWx4 is 4*4 bytes
  751. s_and_b32 s_restore_buf_rsrc1, s_restore_buf_rsrc1, 0x0000FFFF // reset const stride to 0
  752. s_or_b32 s_restore_buf_rsrc1, s_restore_buf_rsrc1, G8SR_RESTORE_BUF_RSRC_WORD1_STRIDE_DWx4 // const stride to 4*4 bytes
  753. s_mov_b32 m0, s_restore_alloc_size
  754. s_set_gpr_idx_on m0, 0x8 // Note.. This will change m0
  755. L_RESTORE_VGPR_LOOP:
  756. buffer_load_dwordx4 v0, v0, s_restore_buf_rsrc0, s_restore_mem_offset slc:1 glc:1
  757. s_waitcnt vmcnt(0)
  758. s_sub_u32 m0, m0, 4
  759. v_mov_b32 v0, v0 // v[0+m0] = v0
  760. v_mov_b32 v1, v1
  761. v_mov_b32 v2, v2
  762. v_mov_b32 v3, v3
  763. s_sub_u32 s_restore_mem_offset, s_restore_mem_offset, 256*4
  764. s_cmp_eq_u32 m0, 0x8000
  765. s_cbranch_scc0 L_RESTORE_VGPR_LOOP
  766. s_set_gpr_idx_off
  767. s_and_b32 s_restore_buf_rsrc1, s_restore_buf_rsrc1, 0x0000FFFF // reset const stride to 0
  768. s_or_b32 s_restore_buf_rsrc1, s_restore_buf_rsrc1, S_RESTORE_BUF_RSRC_WORD1_STRIDE // const stride to 4*4 bytes
  769. else
  770. // VGPR load using dw burst
  771. s_mov_b32 s_restore_mem_offset_save, s_restore_mem_offset // restore start with v1, v0 will be the last
  772. s_add_u32 s_restore_mem_offset, s_restore_mem_offset, 256*4
  773. s_mov_b32 m0, 4 //VGPR initial index value = 1
  774. s_set_gpr_idx_on m0, 0x8 //M0[7:0] = M0[7:0] and M0[15:12] = 0x8
  775. s_add_u32 s_restore_alloc_size, s_restore_alloc_size, 0x8000 //add 0x8000 since we compare m0 against it later
  776. L_RESTORE_VGPR_LOOP:
  777. if(USE_MTBUF_INSTEAD_OF_MUBUF)
  778. 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
  779. else
  780. buffer_load_dword v0, v0, s_restore_buf_rsrc0, s_restore_mem_offset slc:1 glc:1
  781. buffer_load_dword v1, v0, s_restore_buf_rsrc0, s_restore_mem_offset slc:1 glc:1 offset:256
  782. buffer_load_dword v2, v0, s_restore_buf_rsrc0, s_restore_mem_offset slc:1 glc:1 offset:256*2
  783. buffer_load_dword v3, v0, s_restore_buf_rsrc0, s_restore_mem_offset slc:1 glc:1 offset:256*3
  784. end
  785. s_waitcnt vmcnt(0) //ensure data ready
  786. v_mov_b32 v0, v0 //v[0+m0] = v0
  787. v_mov_b32 v1, v1
  788. v_mov_b32 v2, v2
  789. v_mov_b32 v3, v3
  790. s_add_u32 m0, m0, 4 //next vgpr index
  791. s_add_u32 s_restore_mem_offset, s_restore_mem_offset, 256*4 //every buffer_load_dword does 256 bytes
  792. s_cmp_lt_u32 m0, s_restore_alloc_size //scc = (m0 < s_restore_alloc_size) ? 1 : 0
  793. s_cbranch_scc1 L_RESTORE_VGPR_LOOP //VGPR restore (except v0) is complete?
  794. s_set_gpr_idx_off
  795. /* VGPR restore on v0 */
  796. if(USE_MTBUF_INSTEAD_OF_MUBUF)
  797. 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
  798. else
  799. buffer_load_dword v0, v0, s_restore_buf_rsrc0, s_restore_mem_offset_save slc:1 glc:1
  800. buffer_load_dword v1, v0, s_restore_buf_rsrc0, s_restore_mem_offset_save slc:1 glc:1 offset:256
  801. buffer_load_dword v2, v0, s_restore_buf_rsrc0, s_restore_mem_offset_save slc:1 glc:1 offset:256*2
  802. buffer_load_dword v3, v0, s_restore_buf_rsrc0, s_restore_mem_offset_save slc:1 glc:1 offset:256*3
  803. end
  804. end
  805. /* restore SGPRs */
  806. //////////////////////////////
  807. // SGPR SR memory offset : size(VGPR)
  808. get_vgpr_size_bytes(s_restore_mem_offset)
  809. get_sgpr_size_bytes(s_restore_tmp)
  810. s_add_u32 s_restore_mem_offset, s_restore_mem_offset, s_restore_tmp
  811. 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
  812. // TODO, change RSRC word to rearrange memory layout for SGPRS
  813. 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
  814. s_add_u32 s_restore_alloc_size, s_restore_alloc_size, 1
  815. s_lshl_b32 s_restore_alloc_size, s_restore_alloc_size, 4 //Number of SGPRs = (sgpr_size + 1) * 16 (non-zero value)
  816. if (SGPR_SAVE_USE_SQC)
  817. s_lshl_b32 s_restore_buf_rsrc2, s_restore_alloc_size, 2 //NUM_RECORDS in bytes
  818. else
  819. s_lshl_b32 s_restore_buf_rsrc2, s_restore_alloc_size, 8 //NUM_RECORDS in bytes (64 threads)
  820. end
  821. if (SWIZZLE_EN)
  822. s_add_u32 s_restore_buf_rsrc2, s_restore_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  823. else
  824. s_mov_b32 s_restore_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  825. end
  826. s_mov_b32 m0, s_restore_alloc_size
  827. L_RESTORE_SGPR_LOOP:
  828. read_16sgpr_from_mem(s0, s_restore_buf_rsrc0, s_restore_mem_offset) //PV: further performance improvement can be made
  829. s_waitcnt lgkmcnt(0) //ensure data ready
  830. s_sub_u32 m0, m0, 16 // Restore from S[n] to S[0]
  831. s_nop 0 // hazard SALU M0=> S_MOVREL
  832. s_movreld_b64 s0, s0 //s[0+m0] = s0
  833. s_movreld_b64 s2, s2
  834. s_movreld_b64 s4, s4
  835. s_movreld_b64 s6, s6
  836. s_movreld_b64 s8, s8
  837. s_movreld_b64 s10, s10
  838. s_movreld_b64 s12, s12
  839. s_movreld_b64 s14, s14
  840. s_cmp_eq_u32 m0, 0 //scc = (m0 < s_restore_alloc_size) ? 1 : 0
  841. s_cbranch_scc0 L_RESTORE_SGPR_LOOP //SGPR restore (except s0) is complete?
  842. /* restore HW registers */
  843. //////////////////////////////
  844. L_RESTORE_HWREG:
  845. if G8SR_DEBUG_TIMESTAMP
  846. s_mov_b32 s_g8sr_ts_restore_s[0], s_restore_pc_lo
  847. s_mov_b32 s_g8sr_ts_restore_s[1], s_restore_pc_hi
  848. end
  849. // HWREG SR memory offset : size(VGPR)+size(SGPR)
  850. get_vgpr_size_bytes(s_restore_mem_offset)
  851. get_sgpr_size_bytes(s_restore_tmp)
  852. s_add_u32 s_restore_mem_offset, s_restore_mem_offset, s_restore_tmp
  853. s_mov_b32 s_restore_buf_rsrc2, 0x4 //NUM_RECORDS in bytes
  854. if (SWIZZLE_EN)
  855. s_add_u32 s_restore_buf_rsrc2, s_restore_buf_rsrc2, 0x0 //FIXME need to use swizzle to enable bounds checking?
  856. else
  857. s_mov_b32 s_restore_buf_rsrc2, 0x1000000 //NUM_RECORDS in bytes
  858. end
  859. read_hwreg_from_mem(s_restore_m0, s_restore_buf_rsrc0, s_restore_mem_offset) //M0
  860. read_hwreg_from_mem(s_restore_pc_lo, s_restore_buf_rsrc0, s_restore_mem_offset) //PC
  861. read_hwreg_from_mem(s_restore_pc_hi, s_restore_buf_rsrc0, s_restore_mem_offset)
  862. read_hwreg_from_mem(s_restore_exec_lo, s_restore_buf_rsrc0, s_restore_mem_offset) //EXEC
  863. read_hwreg_from_mem(s_restore_exec_hi, s_restore_buf_rsrc0, s_restore_mem_offset)
  864. read_hwreg_from_mem(s_restore_status, s_restore_buf_rsrc0, s_restore_mem_offset) //STATUS
  865. read_hwreg_from_mem(s_restore_trapsts, s_restore_buf_rsrc0, s_restore_mem_offset) //TRAPSTS
  866. read_hwreg_from_mem(xnack_mask_lo, s_restore_buf_rsrc0, s_restore_mem_offset) //XNACK_MASK_LO
  867. read_hwreg_from_mem(xnack_mask_hi, s_restore_buf_rsrc0, s_restore_mem_offset) //XNACK_MASK_HI
  868. read_hwreg_from_mem(s_restore_mode, s_restore_buf_rsrc0, s_restore_mem_offset) //MODE
  869. s_waitcnt lgkmcnt(0) //from now on, it is safe to restore STATUS and IB_STS
  870. //for normal save & restore, the saved PC points to the next inst to execute, no adjustment needs to be made, otherwise:
  871. if ((EMU_RUN_HACK) && (!EMU_RUN_HACK_RESTORE_NORMAL))
  872. 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)
  873. s_addc_u32 s_restore_pc_hi, s_restore_pc_hi, 0x0 //carry bit over
  874. end
  875. if ((EMU_RUN_HACK) && (EMU_RUN_HACK_RESTORE_NORMAL))
  876. 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
  877. s_addc_u32 s_restore_pc_hi, s_restore_pc_hi, 0x0 //carry bit over
  878. end
  879. s_mov_b32 m0, s_restore_m0
  880. s_mov_b32 exec_lo, s_restore_exec_lo
  881. s_mov_b32 exec_hi, s_restore_exec_hi
  882. s_and_b32 s_restore_m0, SQ_WAVE_TRAPSTS_PRE_SAVECTX_MASK, s_restore_trapsts
  883. s_setreg_b32 hwreg(HW_REG_TRAPSTS, SQ_WAVE_TRAPSTS_PRE_SAVECTX_SHIFT, SQ_WAVE_TRAPSTS_PRE_SAVECTX_SIZE), s_restore_m0
  884. s_and_b32 s_restore_m0, SQ_WAVE_TRAPSTS_POST_SAVECTX_MASK, s_restore_trapsts
  885. s_lshr_b32 s_restore_m0, s_restore_m0, SQ_WAVE_TRAPSTS_POST_SAVECTX_SHIFT
  886. s_setreg_b32 hwreg(HW_REG_TRAPSTS, SQ_WAVE_TRAPSTS_POST_SAVECTX_SHIFT, SQ_WAVE_TRAPSTS_POST_SAVECTX_SIZE), s_restore_m0
  887. //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
  888. s_setreg_b32 hwreg(HW_REG_MODE), s_restore_mode
  889. // Restore trap temporaries 6-11, 13-15 initialized by SPI debug dispatch logic
  890. // ttmp SR memory offset : size(VGPR)+size(SGPR)+0x40
  891. get_vgpr_size_bytes(s_restore_ttmps_lo)
  892. get_sgpr_size_bytes(s_restore_ttmps_hi)
  893. s_add_u32 s_restore_ttmps_lo, s_restore_ttmps_lo, s_restore_ttmps_hi
  894. s_add_u32 s_restore_ttmps_lo, s_restore_ttmps_lo, s_restore_buf_rsrc0
  895. s_addc_u32 s_restore_ttmps_hi, s_restore_buf_rsrc1, 0x0
  896. s_and_b32 s_restore_ttmps_hi, s_restore_ttmps_hi, 0xFFFF
  897. s_load_dwordx2 [ttmp6, ttmp7], [s_restore_ttmps_lo, s_restore_ttmps_hi], 0x40 glc:1
  898. s_load_dwordx4 [ttmp8, ttmp9, ttmp10, ttmp11], [s_restore_ttmps_lo, s_restore_ttmps_hi], 0x48 glc:1
  899. s_load_dword ttmp13, [s_restore_ttmps_lo, s_restore_ttmps_hi], 0x58 glc:1
  900. s_load_dwordx2 [ttmp14, ttmp15], [s_restore_ttmps_lo, s_restore_ttmps_hi], 0x5C glc:1
  901. s_waitcnt lgkmcnt(0)
  902. //reuse s_restore_m0 as a temp register
  903. s_and_b32 s_restore_m0, s_restore_pc_hi, S_SAVE_PC_HI_RCNT_MASK
  904. s_lshr_b32 s_restore_m0, s_restore_m0, S_SAVE_PC_HI_RCNT_SHIFT
  905. s_lshl_b32 s_restore_m0, s_restore_m0, SQ_WAVE_IB_STS_RCNT_SHIFT
  906. s_mov_b32 s_restore_tmp, 0x0 //IB_STS is zero
  907. s_or_b32 s_restore_tmp, s_restore_tmp, s_restore_m0
  908. s_and_b32 s_restore_m0, s_restore_pc_hi, S_SAVE_PC_HI_FIRST_REPLAY_MASK
  909. s_lshr_b32 s_restore_m0, s_restore_m0, S_SAVE_PC_HI_FIRST_REPLAY_SHIFT
  910. s_lshl_b32 s_restore_m0, s_restore_m0, SQ_WAVE_IB_STS_FIRST_REPLAY_SHIFT
  911. s_or_b32 s_restore_tmp, s_restore_tmp, s_restore_m0
  912. s_and_b32 s_restore_m0, s_restore_status, SQ_WAVE_STATUS_INST_ATC_MASK
  913. s_lshr_b32 s_restore_m0, s_restore_m0, SQ_WAVE_STATUS_INST_ATC_SHIFT
  914. s_setreg_b32 hwreg(HW_REG_IB_STS), s_restore_tmp
  915. s_and_b32 s_restore_pc_hi, s_restore_pc_hi, 0x0000ffff //pc[47:32] //Do it here in order not to affect STATUS
  916. s_and_b64 exec, exec, exec // Restore STATUS.EXECZ, not writable by s_setreg_b32
  917. s_and_b64 vcc, vcc, vcc // Restore STATUS.VCCZ, not writable by s_setreg_b32
  918. set_status_without_spi_prio(s_restore_status, s_restore_tmp) // SCC is included, which is changed by previous salu
  919. 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
  920. if G8SR_DEBUG_TIMESTAMP
  921. s_memrealtime s_g8sr_ts_restore_d
  922. s_waitcnt lgkmcnt(0)
  923. end
  924. // s_rfe_b64 s_restore_pc_lo //Return to the main shader program and resume execution
  925. s_rfe_restore_b64 s_restore_pc_lo, s_restore_m0 // s_restore_m0[0] is used to set STATUS.inst_atc
  926. /**************************************************************************/
  927. /* the END */
  928. /**************************************************************************/
  929. L_END_PGM:
  930. s_endpgm
  931. end
  932. /**************************************************************************/
  933. /* the helper functions */
  934. /**************************************************************************/
  935. //Only for save hwreg to mem
  936. function write_hwreg_to_mem(s, s_rsrc, s_mem_offset)
  937. s_mov_b32 exec_lo, m0 //assuming exec_lo is not needed anymore from this point on
  938. s_mov_b32 m0, s_mem_offset
  939. s_buffer_store_dword s, s_rsrc, m0 glc:1
  940. ack_sqc_store_workaround()
  941. s_add_u32 s_mem_offset, s_mem_offset, 4
  942. s_mov_b32 m0, exec_lo
  943. end
  944. // HWREG are saved before SGPRs, so all HWREG could be use.
  945. function write_16sgpr_to_mem(s, s_rsrc, s_mem_offset)
  946. s_buffer_store_dwordx4 s[0], s_rsrc, 0 glc:1
  947. ack_sqc_store_workaround()
  948. s_buffer_store_dwordx4 s[4], s_rsrc, 16 glc:1
  949. ack_sqc_store_workaround()
  950. s_buffer_store_dwordx4 s[8], s_rsrc, 32 glc:1
  951. ack_sqc_store_workaround()
  952. s_buffer_store_dwordx4 s[12], s_rsrc, 48 glc:1
  953. ack_sqc_store_workaround()
  954. s_add_u32 s_rsrc[0], s_rsrc[0], 4*16
  955. s_addc_u32 s_rsrc[1], s_rsrc[1], 0x0 // +scc
  956. end
  957. function read_hwreg_from_mem(s, s_rsrc, s_mem_offset)
  958. s_buffer_load_dword s, s_rsrc, s_mem_offset glc:1
  959. s_add_u32 s_mem_offset, s_mem_offset, 4
  960. end
  961. function read_16sgpr_from_mem(s, s_rsrc, s_mem_offset)
  962. s_buffer_load_dwordx16 s, s_rsrc, s_mem_offset glc:1
  963. s_sub_u32 s_mem_offset, s_mem_offset, 4*16
  964. end
  965. function get_lds_size_bytes(s_lds_size_byte)
  966. // SQ LDS granularity is 64DW, while PGM_RSRC2.lds_size is in granularity 128DW
  967. 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
  968. s_lshl_b32 s_lds_size_byte, s_lds_size_byte, 8 //LDS size in dwords = lds_size * 64 *4Bytes // granularity 64DW
  969. end
  970. function get_vgpr_size_bytes(s_vgpr_size_byte)
  971. 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
  972. s_add_u32 s_vgpr_size_byte, s_vgpr_size_byte, 1
  973. 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
  974. end
  975. function get_sgpr_size_bytes(s_sgpr_size_byte)
  976. 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
  977. s_add_u32 s_sgpr_size_byte, s_sgpr_size_byte, 1
  978. s_lshl_b32 s_sgpr_size_byte, s_sgpr_size_byte, 6 //Number of SGPRs = (sgpr_size + 1) * 16 *4 (non-zero value)
  979. end
  980. function get_hwreg_size_bytes
  981. return 128 //HWREG size 128 bytes
  982. end
  983. function ack_sqc_store_workaround
  984. if ACK_SQC_STORE
  985. s_waitcnt lgkmcnt(0)
  986. end
  987. end
  988. function set_status_without_spi_prio(status, tmp)
  989. // Do not restore STATUS.SPI_PRIO since scheduler may have raised it.
  990. s_lshr_b32 tmp, status, SQ_WAVE_STATUS_POST_SPI_PRIO_SHIFT
  991. s_setreg_b32 hwreg(HW_REG_STATUS, SQ_WAVE_STATUS_POST_SPI_PRIO_SHIFT, SQ_WAVE_STATUS_POST_SPI_PRIO_SIZE), tmp
  992. s_nop 0x2 // avoid S_SETREG => S_SETREG hazard
  993. s_setreg_b32 hwreg(HW_REG_STATUS, SQ_WAVE_STATUS_PRE_SPI_PRIO_SHIFT, SQ_WAVE_STATUS_PRE_SPI_PRIO_SIZE), status
  994. end