xref: /XiangShan/src/main/scala/xiangshan/mem/pipeline/StoreUnit.scala (revision 94aa21c6009c2f39c5c5dae9c87260c78887efcc)
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.backend.fu.NewCSR._
32import xiangshan.cache.mmu.{TlbCmd, TlbReq, TlbRequestIO, TlbResp, Pbmt}
33import xiangshan.cache.{DcacheStoreRequestIO, DCacheStoreIO, MemoryOpConstants, HasDCacheParameters, StorePrefetchReq}
34
35class StoreUnit(implicit p: Parameters) extends XSModule
36  with HasDCacheParameters
37  with HasVLSUParameters
38  {
39  val io = IO(new Bundle() {
40    val redirect        = Flipped(ValidIO(new Redirect))
41    val csrCtrl         = Flipped(new CustomCSRCtrlIO)
42    val stin            = Flipped(Decoupled(new MemExuInput))
43    val issue           = Valid(new MemExuInput)
44    // misalignBuffer issue path
45    val misalign_stin   = Flipped(Decoupled(new LsPipelineBundle))
46    val misalign_stout  = Valid(new SqWriteBundle)
47    val tlb             = new TlbRequestIO()
48    val dcache          = new DCacheStoreIO
49    val pmp             = Flipped(new PMPRespBundle())
50    val lsq             = ValidIO(new LsPipelineBundle)
51    val lsq_replenish   = Output(new LsPipelineBundle())
52    val feedback_slow   = ValidIO(new RSFeedback)
53    val prefetch_req    = Flipped(DecoupledIO(new StorePrefetchReq))
54    // provide prefetch info to sms
55    val prefetch_train  = ValidIO(new StPrefetchTrainBundle())
56    // speculative for gated control
57    val s1_prefetch_spec = Output(Bool())
58    val s2_prefetch_spec = Output(Bool())
59    val stld_nuke_query = Valid(new StoreNukeQueryIO)
60    val stout           = DecoupledIO(new MemExuOutput) // writeback store
61    val vecstout        = DecoupledIO(new VecPipelineFeedbackIO(isVStore = true))
62    // store mask, send to sq in store_s0
63    val st_mask_out     = Valid(new StoreMaskBundle)
64    val debug_ls        = Output(new DebugLsInfoBundle)
65    // vector
66    val vecstin           = Flipped(Decoupled(new VecPipeBundle(isVStore = true)))
67    val vec_isFirstIssue  = Input(Bool())
68    // writeback to misalign buffer
69    val misalign_buf = Decoupled(new LsPipelineBundle)
70    // trigger
71    val fromCsrTrigger = Input(new CsrTriggerBundle)
72
73    val s0_s1_valid = Output(Bool())
74  })
75
76  val s1_ready, s2_ready, s3_ready = WireInit(false.B)
77
78  // Pipeline
79  // --------------------------------------------------------------------------------
80  // stage 0
81  // --------------------------------------------------------------------------------
82  // generate addr, use addr to query DCache and DTLB
83  val s0_iss_valid        = io.stin.valid
84  val s0_prf_valid        = io.prefetch_req.valid && io.dcache.req.ready
85  val s0_vec_valid        = io.vecstin.valid
86  val s0_ma_st_valid      = io.misalign_stin.valid
87  val s0_valid            = s0_iss_valid || s0_prf_valid || s0_vec_valid || s0_ma_st_valid
88  val s0_use_flow_ma      = s0_ma_st_valid
89  val s0_use_flow_vec     = s0_vec_valid && !s0_ma_st_valid
90  val s0_use_flow_rs      = s0_iss_valid && !s0_vec_valid && !s0_ma_st_valid
91  val s0_use_flow_prf     = s0_prf_valid && !s0_iss_valid && !s0_vec_valid && !s0_ma_st_valid
92  val s0_use_non_prf_flow = s0_use_flow_rs || s0_use_flow_vec || s0_use_flow_ma
93  val s0_stin             = Mux(s0_use_flow_rs, io.stin.bits, 0.U.asTypeOf(io.stin.bits))
94  val s0_vecstin          = Mux(s0_use_flow_vec, io.vecstin.bits, 0.U.asTypeOf(io.vecstin.bits))
95  val s0_uop              = Mux(
96    s0_use_flow_ma,
97    io.misalign_stin.bits.uop,
98    Mux(
99      s0_use_flow_rs,
100      s0_stin.uop,
101      s0_vecstin.uop
102    )
103  )
104  val s0_isFirstIssue = Mux(
105    s0_use_flow_ma,
106    false.B,
107    s0_use_flow_rs && io.stin.bits.isFirstIssue || s0_use_flow_vec && io.vec_isFirstIssue
108  )
109  val s0_size         = Mux(s0_use_non_prf_flow, s0_uop.fuOpType(2,0), 0.U)// may broken if use it in feature
110  val s0_mem_idx      = Mux(s0_use_non_prf_flow, s0_uop.sqIdx.value, 0.U)
111  val s0_rob_idx      = Mux(s0_use_non_prf_flow, s0_uop.robIdx, 0.U.asTypeOf(s0_uop.robIdx))
112  val s0_pc           = Mux(s0_use_non_prf_flow, s0_uop.pc, 0.U)
113  val s0_instr_type   = Mux(s0_use_non_prf_flow, STORE_SOURCE.U, DCACHE_PREFETCH_SOURCE.U)
114  val s0_wlineflag    = Mux(s0_use_flow_rs, s0_uop.fuOpType === LSUOpType.cbo_zero, false.B)
115  val s0_out          = Wire(new LsPipelineBundle)
116  val s0_kill         = s0_uop.robIdx.needFlush(io.redirect)
117  val s0_can_go       = s1_ready
118  val s0_fire         = s0_valid && !s0_kill && s0_can_go
119  val s0_is128bit     = Wire(Bool())
120  // vector
121  val s0_vecActive    = !s0_use_flow_vec || s0_vecstin.vecActive
122  // val s0_flowPtr      = s0_vecstin.flowPtr
123  // val s0_isLastElem   = s0_vecstin.isLastElem
124  val s0_secondInv    = s0_vecstin.usSecondInv
125  val s0_elemIdx      = s0_vecstin.elemIdx
126  val s0_alignedType  = s0_vecstin.alignedType
127  val s0_mBIndex      = s0_vecstin.mBIndex
128  val s0_vecBaseVaddr = s0_vecstin.basevaddr
129  val s0_isFinalSplit = io.misalign_stin.valid && io.misalign_stin.bits.isFinalSplit
130
131  // generate addr
132  val s0_saddr = s0_stin.src(0) + SignExt(s0_stin.uop.imm(11,0), VAddrBits)
133  val s0_fullva = Wire(UInt(XLEN.W))
134
135  val s0_vaddr = Mux(
136    s0_use_flow_ma,
137    io.misalign_stin.bits.vaddr,
138    Mux(
139      s0_use_flow_rs,
140      s0_saddr,
141      Mux(
142        s0_use_flow_vec,
143        s0_vecstin.vaddr(VAddrBits - 1, 0),
144        io.prefetch_req.bits.vaddr
145      )
146    )
147  )
148
149    val s0_alignTpye = Mux(s0_use_flow_vec, s0_vecstin.alignedType(1,0), s0_uop.fuOpType(1, 0))
150  // exception check
151  val s0_addr_aligned = LookupTree(s0_alignTpye, List(
152    "b00".U   -> true.B,              //b
153    "b01".U   -> (s0_vaddr(0) === 0.U),   //h
154    "b10".U   -> (s0_vaddr(1,0) === 0.U), //w
155    "b11".U   -> (s0_vaddr(2,0) === 0.U)  //d
156  ))
157  // if vector store sends 128-bit requests, its address must be 128-aligned
158  XSError(s0_use_flow_vec && s0_vaddr(3, 0) =/= 0.U && s0_vecstin.alignedType(2), "unit stride 128 bit element is not aligned!")
159
160  val s0_isMisalign = Mux(s0_use_non_prf_flow, (!s0_addr_aligned || s0_vecstin.uop.exceptionVec(storeAddrMisaligned) && s0_vecActive), false.B)
161  val s0_addr_low = s0_vaddr(4, 0)
162  val s0_addr_Up_low = LookupTree(s0_alignTpye, List(
163    "b00".U -> 0.U,
164    "b01".U -> 1.U,
165    "b10".U -> 3.U,
166    "b11".U -> 7.U
167  )) + s0_addr_low
168  val s0_rs_corss16Bytes = s0_addr_Up_low(4) =/= s0_addr_low(4)
169  val s0_misalignWith16Byte = !s0_rs_corss16Bytes && !s0_addr_aligned && !s0_use_flow_prf
170  s0_is128bit := Mux(s0_use_flow_ma, io.misalign_stin.bits.is128bit, is128Bit(s0_vecstin.alignedType) || s0_misalignWith16Byte)
171
172  s0_fullva := Mux(
173    s0_use_flow_rs,
174    s0_stin.src(0) + SignExt(s0_stin.uop.imm(11,0), XLEN),
175    Mux(
176      s0_use_flow_vec,
177      s0_vecstin.vaddr,
178      s0_vaddr
179    )
180  )
181
182  val s0_mask = Mux(
183    s0_use_flow_ma,
184    io.misalign_stin.bits.mask,
185    Mux(
186      s0_use_flow_rs,
187      genVWmask128(s0_saddr, s0_uop.fuOpType(2,0)),
188      Mux(
189        s0_use_flow_vec,
190        s0_vecstin.mask,
191        // -1.asSInt.asUInt
192        Fill(VLEN/8, 1.U(1.W))
193      )
194    )
195  )
196
197  io.tlb.req.valid                   := s0_valid
198  io.tlb.req.bits.vaddr              := s0_vaddr
199  io.tlb.req.bits.fullva             := s0_fullva
200  io.tlb.req.bits.checkfullva        := s0_use_flow_rs || s0_use_flow_vec
201  io.tlb.req.bits.cmd                := TlbCmd.write
202  io.tlb.req.bits.isPrefetch         := s0_use_flow_prf
203  io.tlb.req.bits.size               := s0_size
204  io.tlb.req.bits.kill               := false.B
205  io.tlb.req.bits.memidx.is_ld       := false.B
206  io.tlb.req.bits.memidx.is_st       := true.B
207  io.tlb.req.bits.memidx.idx         := s0_mem_idx
208  io.tlb.req.bits.debug.robIdx       := s0_rob_idx
209  io.tlb.req.bits.no_translate       := false.B
210  io.tlb.req.bits.debug.pc           := s0_pc
211  io.tlb.req.bits.debug.isFirstIssue := s0_isFirstIssue
212  io.tlb.req_kill                    := false.B
213  io.tlb.req.bits.hyperinst          := LSUOpType.isHsv(s0_uop.fuOpType)
214  io.tlb.req.bits.hlvx               := false.B
215  io.tlb.req.bits.pmp_addr           := DontCare
216
217  // Dcache access here: not **real** dcache write
218  // just read meta and tag in dcache, to find out the store will hit or miss
219
220  // NOTE: The store request does not wait for the dcache to be ready.
221  //       If the dcache is not ready at this time, the dcache is not queried.
222  //       But, store prefetch request will always wait for dcache to be ready to make progress.
223  io.dcache.req.valid              := s0_fire
224  io.dcache.req.bits.cmd           := MemoryOpConstants.M_PFW
225  io.dcache.req.bits.vaddr         := s0_vaddr
226  io.dcache.req.bits.instrtype     := s0_instr_type
227
228  s0_out              := DontCare
229  s0_out.vaddr        := s0_vaddr
230  s0_out.fullva       := s0_fullva
231  // Now data use its own io
232  s0_out.data         := s0_stin.src(1)
233  s0_out.uop          := s0_uop
234  s0_out.miss         := false.B
235  // For unaligned, we need to generate a base-aligned mask in storeunit and then do a shift split in StoreQueue.
236  s0_out.mask         := Mux(s0_rs_corss16Bytes && !s0_addr_aligned, genBasemask(s0_saddr,s0_alignTpye(1,0)), s0_mask)
237  s0_out.isFirstIssue := s0_isFirstIssue
238  s0_out.isHWPrefetch := s0_use_flow_prf
239  s0_out.wlineflag    := s0_wlineflag
240  s0_out.isvec        := s0_use_flow_vec
241  s0_out.is128bit     := s0_is128bit
242  s0_out.vecActive    := s0_vecActive
243  s0_out.usSecondInv  := s0_secondInv
244  s0_out.elemIdx      := s0_elemIdx
245  s0_out.alignedType  := s0_alignedType
246  s0_out.mbIndex      := s0_mBIndex
247  s0_out.misalignWith16Byte      := s0_misalignWith16Byte
248  s0_out.isMisalign      := s0_isMisalign
249  s0_out.vecBaseVaddr := s0_vecBaseVaddr
250  when(s0_valid && s0_isFirstIssue) {
251    s0_out.uop.debugInfo.tlbFirstReqTime := GTimer()
252  }
253  s0_out.isFrmMisAlignBuf := s0_use_flow_ma
254  s0_out.isFinalSplit := s0_isFinalSplit
255//  s0_out.uop.exceptionVec(storeAddrMisaligned) := Mux(s0_use_non_prf_flow, (!s0_addr_aligned || s0_vecstin.uop.exceptionVec(storeAddrMisaligned) && s0_vecActive), false.B) && !s0_misalignWith16Byte
256
257  io.st_mask_out.valid       := s0_use_flow_rs || s0_use_flow_vec
258  io.st_mask_out.bits.mask   := s0_out.mask
259  io.st_mask_out.bits.sqIdx  := s0_out.uop.sqIdx
260
261  io.stin.ready := s1_ready && s0_use_flow_rs
262  io.vecstin.ready := s1_ready && s0_use_flow_vec
263  io.prefetch_req.ready := s1_ready && io.dcache.req.ready && !s0_iss_valid && !s0_vec_valid && !s0_ma_st_valid
264  io.misalign_stin.ready := s1_ready && s0_use_flow_ma
265
266  // Pipeline
267  // --------------------------------------------------------------------------------
268  // stage 1
269  // --------------------------------------------------------------------------------
270  // TLB resp (send paddr to dcache)
271  val s1_valid  = RegInit(false.B)
272  val s1_in     = RegEnable(s0_out, s0_fire)
273  val s1_out    = Wire(new LsPipelineBundle)
274  val s1_kill   = Wire(Bool())
275  val s1_can_go = s2_ready
276  val s1_fire   = s1_valid && !s1_kill && s1_can_go
277  val s1_vecActive    = RegEnable(s0_out.vecActive, true.B, s0_fire)
278  val s1_frm_mabuf    = s1_in.isFrmMisAlignBuf
279  val s1_is128bit     = s1_in.is128bit
280
281  // mmio cbo decoder
282  val s1_mmio_cbo  = s1_in.uop.fuOpType === LSUOpType.cbo_clean ||
283                     s1_in.uop.fuOpType === LSUOpType.cbo_flush ||
284                     s1_in.uop.fuOpType === LSUOpType.cbo_inval
285  val s1_vaNeedExt = io.tlb.resp.bits.excp(0).vaNeedExt
286  val s1_isHyper   = io.tlb.resp.bits.excp(0).isHyper
287  val s1_paddr     = io.tlb.resp.bits.paddr(0)
288  val s1_gpaddr    = io.tlb.resp.bits.gpaddr(0)
289  val s1_fullva    = io.tlb.resp.bits.fullva
290  val s1_isForVSnonLeafPTE   = io.tlb.resp.bits.isForVSnonLeafPTE
291  val s1_tlb_miss  = io.tlb.resp.bits.miss && io.tlb.resp.valid && s1_valid
292  val s1_mmio      = s1_mmio_cbo
293  val s1_pbmt      = Mux(!s1_tlb_miss, io.tlb.resp.bits.pbmt.head, 0.U(Pbmt.width.W))
294  val s1_exception = ExceptionNO.selectByFu(s1_out.uop.exceptionVec, StaCfg).asUInt.orR
295  val s1_isvec     = RegEnable(s0_out.isvec, false.B, s0_fire)
296  //We don't want `StoreUnit` to have an additional effect on the Store of vector from a `misalignBuffer,`
297  //But there are places where a marker bit is needed to enable additional processing of vector instructions.
298  //For example: `StoreQueue` is exceptionBuffer
299  val s1_frm_mab_vec = RegEnable(s0_use_flow_ma && io.misalign_stin.bits.isvec, false.B, s0_fire)
300  // val s1_isLastElem = RegEnable(s0_isLastElem, false.B, s0_fire)
301  s1_kill := s1_in.uop.robIdx.needFlush(io.redirect) || (s1_tlb_miss && !s1_isvec && !s1_frm_mabuf)
302
303  s1_ready := !s1_valid || s1_kill || s2_ready
304  io.tlb.resp.ready := true.B // TODO: why dtlbResp needs a ready?
305  when (s0_fire) { s1_valid := true.B }
306  .elsewhen (s1_fire) { s1_valid := false.B }
307  .elsewhen (s1_kill) { s1_valid := false.B }
308
309  // st-ld violation dectect request.
310  io.stld_nuke_query.valid       := s1_valid && !s1_tlb_miss && !s1_in.isHWPrefetch && !s1_frm_mabuf
311  io.stld_nuke_query.bits.robIdx := s1_in.uop.robIdx
312  io.stld_nuke_query.bits.paddr  := s1_paddr
313  io.stld_nuke_query.bits.mask   := s1_in.mask
314  io.stld_nuke_query.bits.matchLine := (s1_in.isvec || s1_in.misalignWith16Byte) && s1_in.is128bit
315
316  // issue
317  io.issue.valid := s1_valid && !s1_tlb_miss && !s1_in.isHWPrefetch && !s1_isvec && !s1_frm_mabuf
318  io.issue.bits  := RegEnable(s0_stin, s0_valid)
319
320  // trigger
321  val storeTrigger = Module(new MemTrigger(MemType.STORE))
322  storeTrigger.io.fromCsrTrigger.tdataVec             := io.fromCsrTrigger.tdataVec
323  storeTrigger.io.fromCsrTrigger.tEnableVec           := io.fromCsrTrigger.tEnableVec
324  storeTrigger.io.fromCsrTrigger.triggerCanRaiseBpExp := io.fromCsrTrigger.triggerCanRaiseBpExp
325  storeTrigger.io.fromCsrTrigger.debugMode            := io.fromCsrTrigger.debugMode
326  storeTrigger.io.fromLoadStore.vaddr                 := s1_in.vaddr
327  storeTrigger.io.fromLoadStore.isVectorUnitStride    := s1_in.isvec && s1_in.is128bit
328  storeTrigger.io.fromLoadStore.mask                  := s1_in.mask
329
330  val s1_trigger_action = storeTrigger.io.toLoadStore.triggerAction
331  val s1_trigger_debug_mode = TriggerAction.isDmode(s1_trigger_action)
332  val s1_trigger_breakpoint = TriggerAction.isExp(s1_trigger_action)
333
334  // for misalign in vsMergeBuffer
335  io.s0_s1_valid := s0_valid || s1_valid
336
337  // Send TLB feedback to store issue queue
338  // Store feedback is generated in store_s1, sent to RS in store_s2
339  val s1_feedback = Wire(Valid(new RSFeedback))
340  s1_feedback.valid                 := s1_valid & !s1_in.isHWPrefetch
341  s1_feedback.bits.hit              := !s1_tlb_miss
342  s1_feedback.bits.flushState       := io.tlb.resp.bits.ptwBack
343  s1_feedback.bits.robIdx           := s1_out.uop.robIdx
344  s1_feedback.bits.sourceType       := RSFeedbackType.tlbMiss
345  s1_feedback.bits.dataInvalidSqIdx := DontCare
346  s1_feedback.bits.sqIdx            := s1_out.uop.sqIdx
347  s1_feedback.bits.lqIdx            := s1_out.uop.lqIdx
348
349  XSDebug(s1_feedback.valid,
350    "S1 Store: tlbHit: %d robIdx: %d\n",
351    s1_feedback.bits.hit,
352    s1_feedback.bits.robIdx.value
353  )
354
355  // io.feedback_slow := s1_feedback
356
357  // get paddr from dtlb, check if rollback is needed
358  // writeback store inst to lsq
359  s1_out           := s1_in
360  s1_out.paddr     := s1_paddr
361  s1_out.gpaddr    := s1_gpaddr
362  s1_out.fullva    := s1_fullva
363  s1_out.vaNeedExt := s1_vaNeedExt
364  s1_out.isHyper   := s1_isHyper
365  s1_out.miss      := false.B
366  s1_out.nc        := Pbmt.isNC(s1_pbmt)
367  s1_out.mmio      := s1_mmio || Pbmt.isIO(s1_pbmt)
368  s1_out.tlbMiss   := s1_tlb_miss
369  s1_out.atomic    := s1_mmio || Pbmt.isIO(s1_pbmt)
370  s1_out.isForVSnonLeafPTE := s1_isForVSnonLeafPTE
371  when (RegNext(io.tlb.req.bits.checkfullva) &&
372    (s1_out.uop.exceptionVec(storePageFault) ||
373      s1_out.uop.exceptionVec(storeAccessFault) ||
374      s1_out.uop.exceptionVec(storeGuestPageFault))) {
375    s1_out.uop.exceptionVec(storeAddrMisaligned) := false.B
376  }
377  s1_out.uop.exceptionVec(storePageFault)      := io.tlb.resp.bits.excp(0).pf.st && s1_vecActive
378  s1_out.uop.exceptionVec(storeAccessFault)    := io.tlb.resp.bits.excp(0).af.st && s1_vecActive
379  s1_out.uop.exceptionVec(storeGuestPageFault) := io.tlb.resp.bits.excp(0).gpf.st && s1_vecActive
380
381  s1_out.uop.flushPipe                := false.B
382  s1_out.uop.trigger                  := s1_trigger_action
383  s1_out.uop.exceptionVec(breakPoint) := s1_trigger_breakpoint
384  s1_out.uop.exceptionVec(storeAddrMisaligned) := s1_mmio && s1_in.isMisalign
385  s1_out.vecVaddrOffset := Mux(
386    s1_trigger_debug_mode || s1_trigger_breakpoint,
387    storeTrigger.io.toLoadStore.triggerVaddr - s1_in.vecBaseVaddr,
388    s1_in.vaddr + genVFirstUnmask(s1_in.mask).asUInt - s1_in.vecBaseVaddr ,
389  )
390  s1_out.vecTriggerMask := Mux(s1_trigger_debug_mode || s1_trigger_breakpoint, storeTrigger.io.toLoadStore.triggerMask, 0.U)
391
392  // scalar store and scalar load nuke check, and also other purposes
393  //A 128-bit aligned unaligned memory access requires changing the unaligned flag bit in sq
394  io.lsq.valid     := s1_valid && !s1_in.isHWPrefetch
395  io.lsq.bits      := s1_out
396  io.lsq.bits.miss := s1_tlb_miss
397  io.lsq.bits.isvec := s1_out.isvec || s1_frm_mab_vec
398  io.lsq.bits.updateAddrValid := (!s1_in.isMisalign || s1_in.misalignWith16Byte) && (!s1_frm_mabuf || s1_in.isFinalSplit) || s1_exception
399  // kill dcache write intent request when tlb miss or exception
400  io.dcache.s1_kill  := (s1_tlb_miss || s1_exception || s1_mmio || s1_in.uop.robIdx.needFlush(io.redirect))
401  io.dcache.s1_paddr := s1_paddr
402
403  // write below io.out.bits assign sentence to prevent overwriting values
404  val s1_tlb_memidx = io.tlb.resp.bits.memidx
405  when(s1_tlb_memidx.is_st && io.tlb.resp.valid && !s1_tlb_miss && s1_tlb_memidx.idx === s1_out.uop.sqIdx.value) {
406    // printf("Store idx = %d\n", s1_tlb_memidx.idx)
407    s1_out.uop.debugInfo.tlbRespTime := GTimer()
408  }
409  val s1_mis_align = s1_valid && !s1_tlb_miss && !s1_in.isHWPrefetch &&
410                      GatedValidRegNext(io.csrCtrl.hd_misalign_st_enable) && s1_in.isMisalign && !s1_in.misalignWith16Byte &&
411                      !s1_trigger_breakpoint && !s1_trigger_debug_mode
412
413  // Pipeline
414  // --------------------------------------------------------------------------------
415  // stage 2
416  // --------------------------------------------------------------------------------
417  // mmio check
418  val s2_valid  = RegInit(false.B)
419  val s2_in     = RegEnable(s1_out, s1_fire)
420  val s2_out    = Wire(new LsPipelineBundle)
421  val s2_kill   = Wire(Bool())
422  val s2_can_go = s3_ready
423  val s2_fire   = s2_valid && !s2_kill && s2_can_go
424  val s2_vecActive    = RegEnable(s1_out.vecActive, true.B, s1_fire)
425  val s2_frm_mabuf    = s2_in.isFrmMisAlignBuf
426  val s2_frm_mab_vec  = RegEnable(s1_frm_mab_vec, true.B, s1_fire)
427  val s2_pbmt   = RegEnable(s1_pbmt, s1_fire)
428  val s2_trigger_debug_mode = RegEnable(s1_trigger_debug_mode, false.B, s1_fire)
429
430  s2_ready := !s2_valid || s2_kill || s3_ready
431  when (s1_fire) { s2_valid := true.B }
432  .elsewhen (s2_fire) { s2_valid := false.B }
433  .elsewhen (s2_kill) { s2_valid := false.B }
434
435  val s2_pmp = WireInit(io.pmp)
436
437  val s2_exception = RegNext(s1_feedback.bits.hit) &&
438                    (s2_trigger_debug_mode || ExceptionNO.selectByFu(s2_out.uop.exceptionVec, StaCfg).asUInt.orR)
439  val s2_mmio = (s2_in.mmio || (Pbmt.isPMA(s2_pbmt) && s2_pmp.mmio)) && RegNext(s1_feedback.bits.hit)
440  val s2_actually_uncache = (Pbmt.isPMA(s2_pbmt) && s2_pmp.mmio || s2_in.nc || s2_in.mmio) && RegNext(s1_feedback.bits.hit)
441  val s2_uncache = !s2_exception && !s2_in.tlbMiss && s2_actually_uncache
442  s2_kill := ((s2_mmio && !s2_exception) && !s2_in.isvec && !s2_frm_mabuf) || s2_in.uop.robIdx.needFlush(io.redirect)
443
444  s2_out        := s2_in
445  s2_out.af     := s2_out.uop.exceptionVec(storeAccessFault)
446  s2_out.mmio   := s2_mmio && !s2_exception
447  s2_out.atomic := s2_in.atomic || Pbmt.isPMA(s2_pbmt) && s2_pmp.atomic
448  s2_out.uop.exceptionVec(storeAccessFault) := (s2_in.uop.exceptionVec(storeAccessFault) ||
449                                                s2_pmp.st ||
450                                                ((s2_in.isvec || s2_frm_mabuf) && s2_actually_uncache && RegNext(s1_feedback.bits.hit))
451                                                ) && s2_vecActive
452  s2_out.uop.exceptionVec(storeAddrMisaligned) := s2_mmio && s2_in.isMisalign
453  s2_out.uop.vpu.vstart     := s2_in.vecVaddrOffset >> s2_in.uop.vpu.veew
454
455  // kill dcache write intent request when mmio or exception
456  io.dcache.s2_kill := (s2_uncache || s2_exception || s2_in.uop.robIdx.needFlush(io.redirect))
457  io.dcache.s2_pc   := s2_out.uop.pc
458  // TODO: dcache resp
459  io.dcache.resp.ready := true.B
460
461  val s2_mis_align = s2_valid && RegEnable(s1_mis_align, s1_fire) && !s2_mmio
462  // goto misalignBuffer
463  val toMisalignBufferValid =  s2_mis_align && !s2_frm_mabuf
464  io.misalign_buf.valid := toMisalignBufferValid
465  io.misalign_buf.bits  := s2_in
466  val misalignBufferNack = toMisalignBufferValid && !io.misalign_buf.ready
467
468  // feedback tlb miss to RS in store_s2
469  val feedback_slow_valid = WireInit(false.B)
470
471  feedback_slow_valid := s1_feedback.valid && !s1_out.uop.robIdx.needFlush(io.redirect) && !s1_out.isvec && !s1_frm_mabuf
472  io.feedback_slow.valid := GatedValidRegNext(feedback_slow_valid)
473  io.feedback_slow.bits  := RegEnable(s1_feedback.bits, feedback_slow_valid)
474  io.feedback_slow.bits.hit  := RegEnable(s1_feedback.bits.hit, feedback_slow_valid) && !misalignBufferNack
475
476  val s2_vecFeedback = RegNext(!s1_out.uop.robIdx.needFlush(io.redirect) && s1_feedback.bits.hit && s1_feedback.valid) &&
477                       !misalignBufferNack && s2_in.isvec && !s2_frm_mabuf
478
479  val s2_misalign_stout = WireInit(0.U.asTypeOf(io.misalign_stout))
480  s2_misalign_stout.valid := s2_valid && s2_can_go && s2_frm_mabuf
481  s2_misalign_stout.bits.mmio := s2_out.mmio
482  s2_misalign_stout.bits.vaddr := s2_out.vaddr
483  s2_misalign_stout.bits.isHyper := s2_out.isHyper
484  s2_misalign_stout.bits.paddr := s2_out.paddr
485  s2_misalign_stout.bits.gpaddr := s2_out.gpaddr
486  s2_misalign_stout.bits.isForVSnonLeafPTE := s2_out.isForVSnonLeafPTE
487  s2_misalign_stout.bits.need_rep := RegEnable(s1_tlb_miss, s1_fire)
488  s2_misalign_stout.bits.uop.exceptionVec := s2_out.uop.exceptionVec
489  io.misalign_stout := s2_misalign_stout
490
491  // mmio and exception
492  io.lsq_replenish := s2_out
493  io.lsq_replenish.af := s2_out.af && s2_valid && !s2_kill
494
495  // prefetch related
496  io.lsq_replenish.miss := io.dcache.resp.fire && io.dcache.resp.bits.miss // miss info
497  io.lsq_replenish.updateAddrValid := !s2_mis_align && (!s2_frm_mabuf || s2_out.isFinalSplit) || s2_exception
498  io.lsq_replenish.isvec := s2_out.isvec || s2_frm_mab_vec
499
500  io.lsq_replenish.hasException := (ExceptionNO.selectByFu(s2_out.uop.exceptionVec, StaCfg).asUInt.orR ||
501    TriggerAction.isDmode(s2_out.uop.trigger) || s2_out.af) && s2_valid && !s2_kill
502
503
504  // RegNext prefetch train for better timing
505  // ** Now, prefetch train is valid at store s3 **
506  val s2_prefetch_train_valid = WireInit(false.B)
507  s2_prefetch_train_valid := s2_valid && io.dcache.resp.fire && !s2_out.mmio && !s2_out.nc && !s2_in.tlbMiss && !s2_in.isHWPrefetch
508  if(EnableStorePrefetchSMS) {
509    io.s1_prefetch_spec := s1_fire
510    io.s2_prefetch_spec := s2_prefetch_train_valid
511    io.prefetch_train.valid := RegNext(s2_prefetch_train_valid)
512    io.prefetch_train.bits.fromLsPipelineBundle(s2_in, latch = true, enable = s2_prefetch_train_valid)
513  }else {
514    io.s1_prefetch_spec := false.B
515    io.s2_prefetch_spec := false.B
516    io.prefetch_train.valid := false.B
517    io.prefetch_train.bits.fromLsPipelineBundle(s2_in, latch = true, enable = false.B)
518  }
519  // override miss bit
520  io.prefetch_train.bits.miss := RegEnable(io.dcache.resp.bits.miss, s2_prefetch_train_valid)
521  // TODO: add prefetch and access bit
522  io.prefetch_train.bits.meta_prefetch := false.B
523  io.prefetch_train.bits.meta_access := false.B
524  io.prefetch_train.bits.isFinalSplit := false.B
525  io.prefetch_train.bits.misalignWith16Byte := false.B
526  io.prefetch_train.bits.isMisalign := false.B
527  io.prefetch_train.bits.misalignNeedWakeUp := false.B
528  io.prefetch_train.bits.updateAddrValid := false.B
529  io.prefetch_train.bits.hasException := false.B
530
531  // Pipeline
532  // --------------------------------------------------------------------------------
533  // stage 3
534  // --------------------------------------------------------------------------------
535  // store write back
536  val s3_valid  = RegInit(false.B)
537  val s3_in     = RegEnable(s2_out, s2_fire)
538  val s3_out    = Wire(new MemExuOutput(isVector = true))
539  val s3_kill   = s3_in.uop.robIdx.needFlush(io.redirect)
540  val s3_can_go = s3_ready
541  val s3_fire   = s3_valid && !s3_kill && s3_can_go
542  val s3_vecFeedback = RegEnable(s2_vecFeedback, s2_fire)
543
544  // store misalign will not writeback to rob now
545  when (s2_fire) { s3_valid := (!s2_mmio || s2_exception) && !s2_out.isHWPrefetch && !s2_mis_align && !s2_frm_mabuf }
546  .elsewhen (s3_fire) { s3_valid := false.B }
547  .elsewhen (s3_kill) { s3_valid := false.B }
548
549  // wb: writeback
550  val SelectGroupSize   = RollbackGroupSize
551  val lgSelectGroupSize = log2Ceil(SelectGroupSize)
552  val TotalSelectCycles = scala.math.ceil(log2Ceil(LoadQueueRAWSize).toFloat / lgSelectGroupSize).toInt + 1
553
554  s3_out                 := DontCare
555  s3_out.uop             := s3_in.uop
556  s3_out.data            := DontCare
557  s3_out.debug.isMMIO    := s3_in.mmio
558  s3_out.debug.isNC      := s3_in.nc
559  s3_out.debug.paddr     := s3_in.paddr
560  s3_out.debug.vaddr     := s3_in.vaddr
561  s3_out.debug.isPerfCnt := false.B
562
563  // Pipeline
564  // --------------------------------------------------------------------------------
565  // stage x
566  // --------------------------------------------------------------------------------
567  // delay TotalSelectCycles - 2 cycle(s)
568  val TotalDelayCycles = TotalSelectCycles - 2
569  val sx_valid = Wire(Vec(TotalDelayCycles + 1, Bool()))
570  val sx_ready = Wire(Vec(TotalDelayCycles + 1, Bool()))
571  val sx_in    = Wire(Vec(TotalDelayCycles + 1, new VecMemExuOutput(isVector = true)))
572  val sx_in_vec = Wire(Vec(TotalDelayCycles +1, Bool()))
573
574  // backward ready signal
575  s3_ready := sx_ready.head
576  for (i <- 0 until TotalDelayCycles + 1) {
577    if (i == 0) {
578      sx_valid(i)          := s3_valid
579      sx_in(i).output      := s3_out
580      sx_in(i).vecFeedback := s3_vecFeedback
581      sx_in(i).nc          := s3_in.nc
582      sx_in(i).mmio        := s3_in.mmio
583      sx_in(i).usSecondInv := s3_in.usSecondInv
584      sx_in(i).elemIdx     := s3_in.elemIdx
585      sx_in(i).alignedType := s3_in.alignedType
586      sx_in(i).mbIndex     := s3_in.mbIndex
587      sx_in(i).mask        := s3_in.mask
588      sx_in(i).vaddr       := s3_in.fullva
589      sx_in(i).vaNeedExt   := s3_in.vaNeedExt
590      sx_in(i).gpaddr      := s3_in.gpaddr
591      sx_in(i).isForVSnonLeafPTE     := s3_in.isForVSnonLeafPTE
592      sx_in(i).vecTriggerMask := s3_in.vecTriggerMask
593      sx_in_vec(i)         := s3_in.isvec
594      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))
595    } else {
596      val cur_kill   = sx_in(i).output.uop.robIdx.needFlush(io.redirect)
597      val cur_can_go = (if (i == TotalDelayCycles) io.stout.ready else sx_ready(i+1))
598      val cur_fire   = sx_valid(i) && !cur_kill && cur_can_go
599      val prev_fire  = sx_valid(i-1) && !sx_in(i-1).output.uop.robIdx.needFlush(io.redirect) && sx_ready(i)
600
601      sx_ready(i) := !sx_valid(i) || cur_kill || (if (i == TotalDelayCycles) io.stout.ready else sx_ready(i+1))
602      val sx_valid_can_go = prev_fire || cur_fire || cur_kill
603      sx_valid(i) := RegEnable(Mux(prev_fire, true.B, false.B), false.B, sx_valid_can_go)
604      sx_in(i) := RegEnable(sx_in(i-1), prev_fire)
605      sx_in_vec(i) := RegEnable(sx_in_vec(i-1), prev_fire)
606    }
607  }
608  val sx_last_valid = sx_valid.takeRight(1).head
609  val sx_last_ready = sx_ready.takeRight(1).head
610  val sx_last_in    = sx_in.takeRight(1).head
611  val sx_last_in_vec = sx_in_vec.takeRight(1).head
612  sx_last_ready := !sx_last_valid || sx_last_in.output.uop.robIdx.needFlush(io.redirect) || io.stout.ready
613
614  // write back: normal store, nc store
615  io.stout.valid := sx_last_valid && !sx_last_in.output.uop.robIdx.needFlush(io.redirect) && !sx_last_in_vec //isStore(sx_last_in.output.uop.fuType)
616  io.stout.bits := sx_last_in.output
617  io.stout.bits.uop.exceptionVec := ExceptionNO.selectByFu(sx_last_in.output.uop.exceptionVec, StaCfg)
618
619  io.vecstout.valid := sx_last_valid && !sx_last_in.output.uop.robIdx.needFlush(io.redirect) && sx_last_in_vec //isVStore(sx_last_in.output.uop.fuType)
620  // TODO: implement it!
621  io.vecstout.bits.mBIndex := sx_last_in.mbIndex
622  io.vecstout.bits.hit := sx_last_in.vecFeedback
623  io.vecstout.bits.isvec := true.B
624  io.vecstout.bits.sourceType := RSFeedbackType.tlbMiss
625  io.vecstout.bits.flushState := DontCare
626  io.vecstout.bits.trigger    := sx_last_in.output.uop.trigger
627  io.vecstout.bits.nc := sx_last_in.nc
628  io.vecstout.bits.mmio := sx_last_in.mmio
629  io.vecstout.bits.exceptionVec := ExceptionNO.selectByFu(sx_last_in.output.uop.exceptionVec, VstuCfg)
630  io.vecstout.bits.usSecondInv := sx_last_in.usSecondInv
631  io.vecstout.bits.vecFeedback := sx_last_in.vecFeedback
632  io.vecstout.bits.elemIdx     := sx_last_in.elemIdx
633  io.vecstout.bits.alignedType := sx_last_in.alignedType
634  io.vecstout.bits.mask        := sx_last_in.mask
635  io.vecstout.bits.vaddr       := sx_last_in.vaddr
636  io.vecstout.bits.vaNeedExt   := sx_last_in.vaNeedExt
637  io.vecstout.bits.gpaddr      := sx_last_in.gpaddr
638  io.vecstout.bits.isForVSnonLeafPTE     := sx_last_in.isForVSnonLeafPTE
639  io.vecstout.bits.vstart      := sx_last_in.output.uop.vpu.vstart
640  io.vecstout.bits.vecTriggerMask := sx_last_in.vecTriggerMask
641  // io.vecstout.bits.reg_offset.map(_ := DontCare)
642  // io.vecstout.bits.elemIdx.map(_ := sx_last_in.elemIdx)
643  // io.vecstout.bits.elemIdxInsideVd.map(_ := DontCare)
644  // io.vecstout.bits.vecdata.map(_ := DontCare)
645  // io.vecstout.bits.mask.map(_ := DontCare)
646  // io.vecstout.bits.alignedType.map(_ := sx_last_in.alignedType)
647
648  io.debug_ls := DontCare
649  io.debug_ls.s1_robIdx := s1_in.uop.robIdx.value
650  io.debug_ls.s1_isTlbFirstMiss := io.tlb.resp.valid && io.tlb.resp.bits.miss && io.tlb.resp.bits.debug.isFirstIssue && !s1_in.isHWPrefetch
651
652  private def printPipeLine(pipeline: LsPipelineBundle, cond: Bool, name: String): Unit = {
653    XSDebug(cond,
654      p"$name" + p" pc ${Hexadecimal(pipeline.uop.pc)} " +
655        p"addr ${Hexadecimal(pipeline.vaddr)} -> ${Hexadecimal(pipeline.paddr)} " +
656        p"op ${Binary(pipeline.uop.fuOpType)} " +
657        p"data ${Hexadecimal(pipeline.data)} " +
658        p"mask ${Hexadecimal(pipeline.mask)}\n"
659    )
660  }
661
662  printPipeLine(s0_out, s0_valid, "S0")
663  printPipeLine(s1_out, s1_valid, "S1")
664
665  // perf cnt
666  XSPerfAccumulate("s0_in_valid",                s0_valid)
667  XSPerfAccumulate("s0_in_fire",                 s0_fire)
668  XSPerfAccumulate("s0_vecin_fire",              s0_fire && s0_use_flow_vec)
669  XSPerfAccumulate("s0_in_fire_first_issue",     s0_fire && s0_isFirstIssue)
670  XSPerfAccumulate("s0_addr_spec_success",       s0_fire && !s0_use_flow_vec && s0_saddr(VAddrBits-1, 12) === s0_stin.src(0)(VAddrBits-1, 12))
671  XSPerfAccumulate("s0_addr_spec_failed",        s0_fire && !s0_use_flow_vec && s0_saddr(VAddrBits-1, 12) =/= s0_stin.src(0)(VAddrBits-1, 12))
672  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)
673  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)
674
675  XSPerfAccumulate("s1_in_valid",                s1_valid)
676  XSPerfAccumulate("s1_in_fire",                 s1_fire)
677  XSPerfAccumulate("s1_in_fire_first_issue",     s1_fire && s1_in.isFirstIssue)
678  XSPerfAccumulate("s1_tlb_miss",                s1_fire && s1_tlb_miss)
679  XSPerfAccumulate("s1_tlb_miss_first_issue",    s1_fire && s1_tlb_miss && s1_in.isFirstIssue)
680  // end
681}
682