xref: /XiangShan/src/main/scala/xiangshan/mem/pipeline/StoreUnit.scala (revision 4ab7f02c251981009225c54bc740213e3937eeab)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16
17package xiangshan.mem
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import utils._
23import utility._
24import xiangshan.ExceptionNO._
25import xiangshan._
26import xiangshan.backend.Bundles.{MemExuInput, MemExuOutput}
27import xiangshan.backend.fu.PMPRespBundle
28import xiangshan.backend.fu.FuConfig._
29import xiangshan.backend.fu.FuType._
30import xiangshan.backend.ctrlblock.DebugLsInfoBundle
31import xiangshan.cache.mmu.{TlbCmd, TlbReq, TlbRequestIO, TlbResp}
32import xiangshan.cache.{DcacheStoreRequestIO, DCacheStoreIO, MemoryOpConstants, HasDCacheParameters, StorePrefetchReq}
33
34class StoreUnit(implicit p: Parameters) extends XSModule
35  with HasDCacheParameters
36  with HasVLSUParameters
37  {
38  val io = IO(new Bundle() {
39    val redirect        = Flipped(ValidIO(new Redirect))
40    val stin            = Flipped(Decoupled(new MemExuInput))
41    val issue           = Valid(new MemExuInput)
42    val tlb             = new TlbRequestIO()
43    val dcache          = new DCacheStoreIO
44    val pmp             = Flipped(new PMPRespBundle())
45    val lsq             = ValidIO(new LsPipelineBundle)
46    val lsq_replenish   = Output(new LsPipelineBundle())
47    val feedback_slow   = ValidIO(new RSFeedback)
48    val prefetch_req    = Flipped(DecoupledIO(new StorePrefetchReq))
49    // provide prefetch info to sms
50    val prefetch_train  = ValidIO(new StPrefetchTrainBundle())
51    // speculative for gated control
52    val s1_prefetch_spec = Output(Bool())
53    val s2_prefetch_spec = Output(Bool())
54    val stld_nuke_query = Valid(new StoreNukeQueryIO)
55    val stout           = DecoupledIO(new MemExuOutput) // writeback store
56    val vecstout        = DecoupledIO(new VecPipelineFeedbackIO(isVStore = true))
57    // store mask, send to sq in store_s0
58    val st_mask_out     = Valid(new StoreMaskBundle)
59    val debug_ls        = Output(new DebugLsInfoBundle)
60    // vector
61    val vecstin           = Flipped(Decoupled(new VecPipeBundle(isVStore = true)))
62    val vec_isFirstIssue  = Input(Bool())
63  })
64
65  val s1_ready, s2_ready, s3_ready = WireInit(false.B)
66
67  // Pipeline
68  // --------------------------------------------------------------------------------
69  // stage 0
70  // --------------------------------------------------------------------------------
71  // generate addr, use addr to query DCache and DTLB
72  val s0_iss_valid    = io.stin.valid
73  val s0_prf_valid    = io.prefetch_req.valid && io.dcache.req.ready
74  val s0_vec_valid    = io.vecstin.valid
75  val s0_valid        = s0_iss_valid || s0_prf_valid || s0_vec_valid
76  val s0_use_flow_vec = s0_vec_valid
77  val s0_use_flow_rs  = s0_iss_valid && !s0_vec_valid
78  val s0_use_flow_prf = !s0_iss_valid && !s0_vec_valid && s0_prf_valid
79  val s0_stin         = Mux(s0_use_flow_rs, io.stin.bits, 0.U.asTypeOf(io.stin.bits))
80  val s0_vecstin      = Mux(s0_use_flow_vec, io.vecstin.bits, 0.U.asTypeOf(io.vecstin.bits))
81  val s0_uop          = Mux(s0_use_flow_rs, s0_stin.uop, s0_vecstin.uop)
82  val s0_isFirstIssue = s0_use_flow_rs && io.stin.bits.isFirstIssue || s0_use_flow_vec && io.vec_isFirstIssue
83  val s0_iqIdx        = Mux(s0_use_flow_rs, io.stin.bits.iqIdx, 0.U)
84  val s0_size         = Mux(s0_use_flow_rs || s0_use_flow_vec, s0_uop.fuOpType(2,0), 0.U)// may broken if use it in feature
85  val s0_mem_idx      = Mux(s0_use_flow_rs || s0_use_flow_vec, s0_uop.sqIdx.value, 0.U)
86  val s0_rob_idx      = Mux(s0_use_flow_rs || s0_use_flow_vec, s0_uop.robIdx, 0.U.asTypeOf(s0_uop.robIdx))
87  val s0_pc           = Mux(s0_use_flow_rs || s0_use_flow_vec, s0_uop.pc, 0.U)
88  val s0_instr_type   = Mux(s0_use_flow_rs || s0_use_flow_vec, STORE_SOURCE.U, DCACHE_PREFETCH_SOURCE.U)
89  val s0_wlineflag    = Mux(s0_use_flow_rs, s0_uop.fuOpType === LSUOpType.cbo_zero, false.B)
90  val s0_out          = Wire(new LsPipelineBundle)
91  val s0_kill         = s0_uop.robIdx.needFlush(io.redirect)
92  val s0_can_go       = s1_ready
93  val s0_fire         = s0_valid && !s0_kill && s0_can_go
94  val s0_is128bit     = is128Bit(s0_vecstin.alignedType)
95  // vector
96  val s0_vecActive    = !s0_use_flow_vec || s0_vecstin.vecActive
97  // val s0_flowPtr      = s0_vecstin.flowPtr
98  // val s0_isLastElem   = s0_vecstin.isLastElem
99  val s0_secondInv    = s0_vecstin.usSecondInv
100  val s0_elemIdx      = s0_vecstin.elemIdx
101  val s0_alignedType  = s0_vecstin.alignedType
102  val s0_mBIndex      = s0_vecstin.mBIndex
103
104  // generate addr
105  // val saddr = s0_in.bits.src(0) + SignExt(s0_in.bits.uop.imm(11,0), VAddrBits)
106  val imm12 = WireInit(s0_uop.imm(11,0))
107  val saddr_lo = s0_stin.src(0)(11,0) + Cat(0.U(1.W), imm12)
108  val saddr_hi = Mux(saddr_lo(12),
109    Mux(imm12(11), s0_stin.src(0)(VAddrBits-1, 12), s0_stin.src(0)(VAddrBits-1, 12)+1.U),
110    Mux(imm12(11), s0_stin.src(0)(VAddrBits-1, 12)+SignExt(1.U, VAddrBits-12), s0_stin.src(0)(VAddrBits-1, 12)),
111  )
112  val s0_saddr = Cat(saddr_hi, saddr_lo(11,0))
113  val s0_vaddr = Mux(
114    s0_use_flow_rs,
115    s0_saddr,
116    Mux(
117      s0_use_flow_vec,
118      s0_vecstin.vaddr,
119      io.prefetch_req.bits.vaddr
120    )
121  )
122  val s0_mask  = Mux(
123    s0_use_flow_rs,
124    genVWmask128(s0_saddr, s0_uop.fuOpType(2,0)),
125    Mux(
126      s0_use_flow_vec,
127      s0_vecstin.mask,
128      // -1.asSInt.asUInt
129      Fill(VLEN/8, 1.U(1.W))
130    )
131  )
132
133  io.tlb.req.valid                   := s0_valid
134  io.tlb.req.bits.vaddr              := s0_vaddr
135  io.tlb.req.bits.cmd                := TlbCmd.write
136  io.tlb.req.bits.size               := s0_size
137  io.tlb.req.bits.kill               := false.B
138  io.tlb.req.bits.memidx.is_ld       := false.B
139  io.tlb.req.bits.memidx.is_st       := true.B
140  io.tlb.req.bits.memidx.idx         := s0_mem_idx
141  io.tlb.req.bits.debug.robIdx       := s0_rob_idx
142  io.tlb.req.bits.no_translate       := false.B
143  io.tlb.req.bits.debug.pc           := s0_pc
144  io.tlb.req.bits.debug.isFirstIssue := s0_isFirstIssue
145  io.tlb.req_kill                    := false.B
146  io.tlb.req.bits.hyperinst          := LSUOpType.isHsv(s0_uop.fuOpType)
147  io.tlb.req.bits.hlvx               := false.B
148
149  // Dcache access here: not **real** dcache write
150  // just read meta and tag in dcache, to find out the store will hit or miss
151
152  // NOTE: The store request does not wait for the dcache to be ready.
153  //       If the dcache is not ready at this time, the dcache is not queried.
154  //       But, store prefetch request will always wait for dcache to be ready to make progress.
155  io.dcache.req.valid              := s0_fire
156  io.dcache.req.bits.cmd           := MemoryOpConstants.M_PFW
157  io.dcache.req.bits.vaddr         := s0_vaddr
158  io.dcache.req.bits.instrtype     := s0_instr_type
159
160  s0_out              := DontCare
161  s0_out.vaddr        := s0_vaddr
162  // Now data use its own io
163  // s1_out.data := genWdata(s1_in.src(1), s1_in.uop.fuOpType(1,0))
164  s0_out.data         := s0_stin.src(1)
165  s0_out.uop          := s0_uop
166  s0_out.miss         := false.B
167  s0_out.mask         := s0_mask
168  s0_out.isFirstIssue := s0_isFirstIssue
169  s0_out.isHWPrefetch := s0_use_flow_prf
170  s0_out.wlineflag    := s0_wlineflag
171  s0_out.isvec        := s0_use_flow_vec
172  s0_out.is128bit     := s0_is128bit
173  s0_out.vecActive    := s0_vecActive
174  s0_out.usSecondInv  := s0_secondInv
175  s0_out.elemIdx      := s0_elemIdx
176  s0_out.alignedType  := s0_alignedType
177  s0_out.mbIndex      := s0_mBIndex
178  when(s0_valid && s0_isFirstIssue) {
179    s0_out.uop.debugInfo.tlbFirstReqTime := GTimer()
180  }
181
182  // exception check
183  val s0_addr_aligned = LookupTree(Mux(s0_use_flow_vec, s0_vecstin.alignedType(1,0), s0_uop.fuOpType(1, 0)), List(
184    "b00".U   -> true.B,              //b
185    "b01".U   -> (s0_out.vaddr(0) === 0.U),   //h
186    "b10".U   -> (s0_out.vaddr(1,0) === 0.U), //w
187    "b11".U   -> (s0_out.vaddr(2,0) === 0.U)  //d
188  ))
189  // if vector store sends 128-bit requests, its address must be 128-aligned
190  XSError(s0_use_flow_vec && s0_out.vaddr(3, 0) =/= 0.U && s0_vecstin.alignedType(2), "unit stride 128 bit element is not aligned!")
191  s0_out.uop.exceptionVec(storeAddrMisaligned) := Mux(s0_use_flow_rs || s0_use_flow_vec, !s0_addr_aligned, false.B)
192
193  io.st_mask_out.valid       := s0_use_flow_rs || s0_use_flow_vec
194  io.st_mask_out.bits.mask   := s0_out.mask
195  io.st_mask_out.bits.sqIdx  := s0_out.uop.sqIdx
196
197  io.stin.ready := s1_ready && s0_use_flow_rs
198  io.vecstin.ready := s1_ready && s0_use_flow_vec
199  io.prefetch_req.ready := s1_ready && io.dcache.req.ready && !s0_iss_valid && !s0_vec_valid
200
201  // Pipeline
202  // --------------------------------------------------------------------------------
203  // stage 1
204  // --------------------------------------------------------------------------------
205  // TLB resp (send paddr to dcache)
206  val s1_valid  = RegInit(false.B)
207  val s1_in     = RegEnable(s0_out, s0_fire)
208  val s1_out    = Wire(new LsPipelineBundle)
209  val s1_kill   = Wire(Bool())
210  val s1_can_go = s2_ready
211  val s1_fire   = s1_valid && !s1_kill && s1_can_go
212  val s1_vecActive    = RegEnable(s0_out.vecActive, true.B, s0_fire)
213
214  // mmio cbo decoder
215  val s1_mmio_cbo  = s1_in.uop.fuOpType === LSUOpType.cbo_clean ||
216                     s1_in.uop.fuOpType === LSUOpType.cbo_flush ||
217                     s1_in.uop.fuOpType === LSUOpType.cbo_inval
218  val s1_paddr     = io.tlb.resp.bits.paddr(0)
219  val s1_gpaddr    = io.tlb.resp.bits.gpaddr(0)
220  val s1_tlb_miss  = io.tlb.resp.bits.miss
221  val s1_mmio      = s1_mmio_cbo
222  val s1_exception = ExceptionNO.selectByFu(s1_out.uop.exceptionVec, StaCfg).asUInt.orR
223  val s1_isvec     = RegEnable(s0_out.isvec, false.B, s0_fire)
224  // val s1_isLastElem = RegEnable(s0_isLastElem, false.B, s0_fire)
225  s1_kill := s1_in.uop.robIdx.needFlush(io.redirect) || (s1_tlb_miss && !s1_isvec)
226
227  s1_ready := !s1_valid || s1_kill || s2_ready
228  io.tlb.resp.ready := true.B // TODO: why dtlbResp needs a ready?
229  when (s0_fire) { s1_valid := true.B }
230  .elsewhen (s1_fire) { s1_valid := false.B }
231  .elsewhen (s1_kill) { s1_valid := false.B }
232
233  // st-ld violation dectect request.
234  io.stld_nuke_query.valid       := s1_valid && !s1_tlb_miss && !s1_in.isHWPrefetch
235  io.stld_nuke_query.bits.robIdx := s1_in.uop.robIdx
236  io.stld_nuke_query.bits.paddr  := s1_paddr
237  io.stld_nuke_query.bits.mask   := s1_in.mask
238  io.stld_nuke_query.bits.matchLine := s1_in.isvec && s1_in.is128bit
239
240  // issue
241  io.issue.valid := s1_valid && !s1_tlb_miss && !s1_in.isHWPrefetch && !s1_isvec
242  io.issue.bits  := RegEnable(s0_stin, s0_valid)
243
244
245  // Send TLB feedback to store issue queue
246  // Store feedback is generated in store_s1, sent to RS in store_s2
247  val s1_feedback = Wire(Valid(new RSFeedback))
248  s1_feedback.valid                 := s1_valid & !s1_in.isHWPrefetch
249  s1_feedback.bits.hit              := !s1_tlb_miss
250  s1_feedback.bits.flushState       := io.tlb.resp.bits.ptwBack
251  s1_feedback.bits.robIdx           := s1_out.uop.robIdx
252  s1_feedback.bits.sourceType       := RSFeedbackType.tlbMiss
253  s1_feedback.bits.dataInvalidSqIdx := DontCare
254  s1_feedback.bits.sqIdx            := s1_out.uop.sqIdx
255
256  XSDebug(s1_feedback.valid,
257    "S1 Store: tlbHit: %d robIdx: %d\n",
258    s1_feedback.bits.hit,
259    s1_feedback.bits.robIdx.value
260  )
261
262  // io.feedback_slow := s1_feedback
263
264  // get paddr from dtlb, check if rollback is needed
265  // writeback store inst to lsq
266  s1_out         := s1_in
267  s1_out.paddr   := s1_paddr
268  s1_out.gpaddr  := s1_gpaddr
269  s1_out.miss    := false.B
270  s1_out.mmio    := s1_mmio
271  s1_out.tlbMiss := s1_tlb_miss
272  s1_out.atomic  := s1_mmio
273  s1_out.uop.exceptionVec(storePageFault)      := io.tlb.resp.bits.excp(0).pf.st && s1_vecActive
274  s1_out.uop.exceptionVec(storeAccessFault)    := io.tlb.resp.bits.excp(0).af.st && s1_vecActive
275  s1_out.uop.exceptionVec(storeGuestPageFault) := io.tlb.resp.bits.excp(0).gpf.st && s1_vecActive
276
277  // scalar store and scalar load nuke check, and also other purposes
278  io.lsq.valid     := s1_valid && !s1_in.isHWPrefetch
279  io.lsq.bits      := s1_out
280  io.lsq.bits.miss := s1_tlb_miss
281
282  // kill dcache write intent request when tlb miss or exception
283  io.dcache.s1_kill  := (s1_tlb_miss || s1_exception || s1_mmio || s1_in.uop.robIdx.needFlush(io.redirect))
284  io.dcache.s1_paddr := s1_paddr
285
286  // write below io.out.bits assign sentence to prevent overwriting values
287  val s1_tlb_memidx = io.tlb.resp.bits.memidx
288  when(s1_tlb_memidx.is_st && io.tlb.resp.valid && !s1_tlb_miss && s1_tlb_memidx.idx === s1_out.uop.sqIdx.value) {
289    // printf("Store idx = %d\n", s1_tlb_memidx.idx)
290    s1_out.uop.debugInfo.tlbRespTime := GTimer()
291  }
292
293  // Pipeline
294  // --------------------------------------------------------------------------------
295  // stage 2
296  // --------------------------------------------------------------------------------
297  // mmio check
298  val s2_valid  = RegInit(false.B)
299  val s2_in     = RegEnable(s1_out, s1_fire)
300  val s2_out    = Wire(new LsPipelineBundle)
301  val s2_kill   = Wire(Bool())
302  val s2_can_go = s3_ready
303  val s2_fire   = s2_valid && !s2_kill && s2_can_go
304  val s2_vecActive    = RegEnable(s1_out.vecActive, true.B, s1_fire)
305
306  s2_ready := !s2_valid || s2_kill || s3_ready
307  when (s1_fire) { s2_valid := true.B }
308  .elsewhen (s2_fire) { s2_valid := false.B }
309  .elsewhen (s2_kill) { s2_valid := false.B }
310
311  val s2_pmp = WireInit(io.pmp)
312
313  val s2_exception = (ExceptionNO.selectByFu(s2_out.uop.exceptionVec, StaCfg).asUInt.orR) && RegNext(s1_feedback.bits.hit)
314  val s2_mmio = (s2_in.mmio || s2_pmp.mmio) && RegNext(s1_feedback.bits.hit)
315  s2_kill := ((s2_mmio && !s2_exception) && !s2_in.isvec) || s2_in.uop.robIdx.needFlush(io.redirect)
316
317  s2_out        := s2_in
318  s2_out.af     := s2_pmp.st && !s2_in.isvec
319  s2_out.mmio   := s2_mmio && !s2_exception
320  s2_out.atomic := s2_in.atomic || s2_pmp.atomic
321  s2_out.uop.exceptionVec(storeAccessFault) := (s2_in.uop.exceptionVec(storeAccessFault) ||
322                                                s2_pmp.st ||
323                                                (s2_in.isvec && s2_pmp.mmio && RegNext(s1_feedback.bits.hit))
324                                                ) && s2_vecActive
325
326  // kill dcache write intent request when mmio or exception
327  io.dcache.s2_kill := (s2_mmio || s2_exception || s2_in.uop.robIdx.needFlush(io.redirect))
328  io.dcache.s2_pc   := s2_out.uop.pc
329  // TODO: dcache resp
330  io.dcache.resp.ready := true.B
331
332  // feedback tlb miss to RS in store_s2
333  val feedback_slow_valid = WireInit(false.B)
334  feedback_slow_valid := s1_feedback.valid && !s1_out.uop.robIdx.needFlush(io.redirect) && !s1_out.isvec
335  io.feedback_slow.valid := GatedValidRegNext(feedback_slow_valid)
336  io.feedback_slow.bits  := RegEnable(s1_feedback.bits, feedback_slow_valid)
337
338  val s2_vecFeedback = RegNext(!s1_out.uop.robIdx.needFlush(io.redirect) && s1_feedback.bits.hit) && s2_in.isvec
339
340  // mmio and exception
341  io.lsq_replenish := s2_out
342  io.lsq_replenish.af := s2_out.af && !s2_kill
343
344  // prefetch related
345  io.lsq_replenish.miss := io.dcache.resp.fire && io.dcache.resp.bits.miss // miss info
346
347  // RegNext prefetch train for better timing
348  // ** Now, prefetch train is valid at store s3 **
349  val s2_prefetch_train_valid = WireInit(false.B)
350  s2_prefetch_train_valid := s2_valid && io.dcache.resp.fire && !s2_out.mmio && !s2_in.tlbMiss && !s2_in.isHWPrefetch
351  if(EnableStorePrefetchSMS) {
352    io.s1_prefetch_spec := s1_fire
353    io.s2_prefetch_spec := s2_prefetch_train_valid
354    io.prefetch_train.valid := RegNext(s2_prefetch_train_valid)
355    io.prefetch_train.bits.fromLsPipelineBundle(s2_in, latch = true, enable = s2_prefetch_train_valid)
356  }else {
357    io.s1_prefetch_spec := false.B
358    io.s2_prefetch_spec := false.B
359    io.prefetch_train.valid := false.B
360    io.prefetch_train.bits.fromLsPipelineBundle(s2_in, latch = true, enable = false.B)
361  }
362  // override miss bit
363  io.prefetch_train.bits.miss := RegEnable(io.dcache.resp.bits.miss, s2_prefetch_train_valid)
364  // TODO: add prefetch and access bit
365  io.prefetch_train.bits.meta_prefetch := false.B
366  io.prefetch_train.bits.meta_access := false.B
367
368  // Pipeline
369  // --------------------------------------------------------------------------------
370  // stage 3
371  // --------------------------------------------------------------------------------
372  // store write back
373  val s3_valid  = RegInit(false.B)
374  val s3_in     = RegEnable(s2_out, s2_fire)
375  val s3_out    = Wire(new MemExuOutput(isVector = true))
376  val s3_kill   = s3_in.uop.robIdx.needFlush(io.redirect)
377  val s3_can_go = s3_ready
378  val s3_fire   = s3_valid && !s3_kill && s3_can_go
379  val s3_vecFeedback = RegEnable(s2_vecFeedback, s2_fire)
380
381  when (s2_fire) { s3_valid := (!s2_mmio || s2_exception) && !s2_out.isHWPrefetch  }
382  .elsewhen (s3_fire) { s3_valid := false.B }
383  .elsewhen (s3_kill) { s3_valid := false.B }
384
385  // wb: writeback
386  val SelectGroupSize   = RollbackGroupSize
387  val lgSelectGroupSize = log2Ceil(SelectGroupSize)
388  val TotalSelectCycles = scala.math.ceil(log2Ceil(LoadQueueRAWSize).toFloat / lgSelectGroupSize).toInt + 1
389
390  s3_out                 := DontCare
391  s3_out.uop             := s3_in.uop
392  s3_out.data            := DontCare
393  s3_out.debug.isMMIO    := s3_in.mmio
394  s3_out.debug.paddr     := s3_in.paddr
395  s3_out.debug.vaddr     := s3_in.vaddr
396  s3_out.debug.isPerfCnt := false.B
397
398  // Pipeline
399  // --------------------------------------------------------------------------------
400  // stage x
401  // --------------------------------------------------------------------------------
402  // delay TotalSelectCycles - 2 cycle(s)
403  val TotalDelayCycles = TotalSelectCycles - 2
404  val sx_valid = Wire(Vec(TotalDelayCycles + 1, Bool()))
405  val sx_ready = Wire(Vec(TotalDelayCycles + 1, Bool()))
406  val sx_in    = Wire(Vec(TotalDelayCycles + 1, new VecMemExuOutput(isVector = true)))
407
408  // backward ready signal
409  s3_ready := sx_ready.head
410  for (i <- 0 until TotalDelayCycles + 1) {
411    if (i == 0) {
412      sx_valid(i)          := s3_valid
413      sx_in(i).output      := s3_out
414      sx_in(i).vecFeedback := s3_vecFeedback
415      sx_in(i).mmio        := s3_in.mmio
416      sx_in(i).usSecondInv := s3_in.usSecondInv
417      sx_in(i).elemIdx     := s3_in.elemIdx
418      sx_in(i).alignedType := s3_in.alignedType
419      sx_in(i).mbIndex     := s3_in.mbIndex
420      sx_in(i).mask        := s3_in.mask
421      sx_in(i).vaddr       := s3_in.vaddr
422      sx_ready(i) := !s3_valid(i) || sx_in(i).output.uop.robIdx.needFlush(io.redirect) || (if (TotalDelayCycles == 0) io.stout.ready else sx_ready(i+1))
423    } else {
424      val cur_kill   = sx_in(i).output.uop.robIdx.needFlush(io.redirect)
425      val cur_can_go = (if (i == TotalDelayCycles) io.stout.ready else sx_ready(i+1))
426      val cur_fire   = sx_valid(i) && !cur_kill && cur_can_go
427      val prev_fire  = sx_valid(i-1) && !sx_in(i-1).output.uop.robIdx.needFlush(io.redirect) && sx_ready(i)
428
429      sx_ready(i) := !sx_valid(i) || cur_kill || (if (i == TotalDelayCycles) io.stout.ready else sx_ready(i+1))
430      val sx_valid_can_go = prev_fire || cur_fire || cur_kill
431      sx_valid(i) := RegEnable(Mux(prev_fire, true.B, false.B), false.B, sx_valid_can_go)
432      sx_in(i) := RegEnable(sx_in(i-1), prev_fire)
433    }
434  }
435  val sx_last_valid = sx_valid.takeRight(1).head
436  val sx_last_ready = sx_ready.takeRight(1).head
437  val sx_last_in    = sx_in.takeRight(1).head
438  sx_last_ready := !sx_last_valid || sx_last_in.output.uop.robIdx.needFlush(io.redirect) || io.stout.ready
439
440  io.stout.valid := sx_last_valid && !sx_last_in.output.uop.robIdx.needFlush(io.redirect) && isStore(sx_last_in.output.uop.fuType)
441  io.stout.bits := sx_last_in.output
442
443  io.vecstout.valid := sx_last_valid && !sx_last_in.output.uop.robIdx.needFlush(io.redirect) && isVStore(sx_last_in.output.uop.fuType)
444  // TODO: implement it!
445  io.vecstout.bits.mBIndex := sx_last_in.mbIndex
446  io.vecstout.bits.hit := sx_last_in.vecFeedback
447  io.vecstout.bits.isvec := true.B
448  io.vecstout.bits.sourceType := RSFeedbackType.tlbMiss
449  io.vecstout.bits.flushState := DontCare
450  io.vecstout.bits.mmio := sx_last_in.mmio
451  io.vecstout.bits.exceptionVec := sx_last_in.output.uop.exceptionVec
452  io.vecstout.bits.usSecondInv := sx_last_in.usSecondInv
453  io.vecstout.bits.vecFeedback := sx_last_in.vecFeedback
454  io.vecstout.bits.elemIdx     := sx_last_in.elemIdx
455  io.vecstout.bits.alignedType := sx_last_in.alignedType
456  io.vecstout.bits.mask        := sx_last_in.mask
457  io.vecstout.bits.vaddr       := sx_last_in.vaddr
458  // io.vecstout.bits.reg_offset.map(_ := DontCare)
459  // io.vecstout.bits.elemIdx.map(_ := sx_last_in.elemIdx)
460  // io.vecstout.bits.elemIdxInsideVd.map(_ := DontCare)
461  // io.vecstout.bits.vecdata.map(_ := DontCare)
462  // io.vecstout.bits.mask.map(_ := DontCare)
463  // io.vecstout.bits.alignedType.map(_ := sx_last_in.alignedType)
464
465  io.debug_ls := DontCare
466  io.debug_ls.s1_robIdx := s1_in.uop.robIdx.value
467  io.debug_ls.s1_isTlbFirstMiss := io.tlb.resp.valid && io.tlb.resp.bits.miss && io.tlb.resp.bits.debug.isFirstIssue && !s1_in.isHWPrefetch
468
469  private def printPipeLine(pipeline: LsPipelineBundle, cond: Bool, name: String): Unit = {
470    XSDebug(cond,
471      p"$name" + p" pc ${Hexadecimal(pipeline.uop.pc)} " +
472        p"addr ${Hexadecimal(pipeline.vaddr)} -> ${Hexadecimal(pipeline.paddr)} " +
473        p"op ${Binary(pipeline.uop.fuOpType)} " +
474        p"data ${Hexadecimal(pipeline.data)} " +
475        p"mask ${Hexadecimal(pipeline.mask)}\n"
476    )
477  }
478
479  printPipeLine(s0_out, s0_valid, "S0")
480  printPipeLine(s1_out, s1_valid, "S1")
481
482  // perf cnt
483  XSPerfAccumulate("s0_in_valid",                s0_valid)
484  XSPerfAccumulate("s0_in_fire",                 s0_fire)
485  XSPerfAccumulate("s0_vecin_fire",              s0_fire && s0_use_flow_vec)
486  XSPerfAccumulate("s0_in_fire_first_issue",     s0_fire && s0_isFirstIssue)
487  XSPerfAccumulate("s0_addr_spec_success",       s0_fire && !s0_use_flow_vec && s0_saddr(VAddrBits-1, 12) === s0_stin.src(0)(VAddrBits-1, 12))
488  XSPerfAccumulate("s0_addr_spec_failed",        s0_fire && !s0_use_flow_vec && s0_saddr(VAddrBits-1, 12) =/= s0_stin.src(0)(VAddrBits-1, 12))
489  XSPerfAccumulate("s0_addr_spec_success_once",  s0_fire && !s0_use_flow_vec && s0_saddr(VAddrBits-1, 12) === s0_stin.src(0)(VAddrBits-1, 12) && s0_isFirstIssue)
490  XSPerfAccumulate("s0_addr_spec_failed_once",   s0_fire && !s0_use_flow_vec && s0_saddr(VAddrBits-1, 12) =/= s0_stin.src(0)(VAddrBits-1, 12) && s0_isFirstIssue)
491
492  XSPerfAccumulate("s1_in_valid",                s1_valid)
493  XSPerfAccumulate("s1_in_fire",                 s1_fire)
494  XSPerfAccumulate("s1_in_fire_first_issue",     s1_fire && s1_in.isFirstIssue)
495  XSPerfAccumulate("s1_tlb_miss",                s1_fire && s1_tlb_miss)
496  XSPerfAccumulate("s1_tlb_miss_first_issue",    s1_fire && s1_tlb_miss && s1_in.isFirstIssue)
497  // end
498}