xref: /XiangShan/src/main/scala/xiangshan/mem/lsqueue/StoreQueue.scala (revision 17985fbbb1c69e82059f583150a6eff05516032d)
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 chisel3._
20import chisel3.util._
21import difftest._
22import difftest.common.DifftestMem
23import org.chipsalliance.cde.config.Parameters
24import utility._
25import utils._
26import xiangshan._
27import xiangshan.cache._
28import xiangshan.cache.{DCacheLineIO, DCacheWordIO, MemoryOpConstants}
29import xiangshan.backend._
30import xiangshan.backend.rob.{RobLsqIO, RobPtr}
31import xiangshan.backend.Bundles.{DynInst, MemExuOutput}
32import xiangshan.backend.decode.isa.bitfield.{Riscv32BitInst, XSInstBitFields}
33
34class SqPtr(implicit p: Parameters) extends CircularQueuePtr[SqPtr](
35  p => p(XSCoreParamsKey).StoreQueueSize
36){
37}
38
39object SqPtr {
40  def apply(f: Bool, v: UInt)(implicit p: Parameters): SqPtr = {
41    val ptr = Wire(new SqPtr)
42    ptr.flag := f
43    ptr.value := v
44    ptr
45  }
46}
47
48class SqEnqIO(implicit p: Parameters) extends MemBlockBundle {
49  val canAccept = Output(Bool())
50  val lqCanAccept = Input(Bool())
51  val needAlloc = Vec(LSQEnqWidth, Input(Bool()))
52  val req = Vec(LSQEnqWidth, Flipped(ValidIO(new DynInst)))
53  val resp = Vec(LSQEnqWidth, Output(new SqPtr))
54}
55
56class DataBufferEntry (implicit p: Parameters)  extends DCacheBundle {
57  val addr   = UInt(PAddrBits.W)
58  val vaddr  = UInt(VAddrBits.W)
59  val data   = UInt(VLEN.W)
60  val mask   = UInt((VLEN/8).W)
61  val wline = Bool()
62  val sqPtr  = new SqPtr
63  val prefetch = Bool()
64}
65
66// Store Queue
67class StoreQueue(implicit p: Parameters) extends XSModule
68  with HasDCacheParameters with HasCircularQueuePtrHelper with HasPerfEvents {
69  val io = IO(new Bundle() {
70    val hartId = Input(UInt(8.W))
71    val enq = new SqEnqIO
72    val brqRedirect = Flipped(ValidIO(new Redirect))
73    val storeAddrIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) // store addr, data is not included
74    val vecStoreAddrIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) // vec store addr, data is not include , from stu
75    val vecStoreAddrInactivate = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) // vec store addr, data is not include , from vsFlowQueue
76    val storeAddrInRe = Vec(StorePipelineWidth, Input(new LsPipelineBundle())) // store more mmio and exception
77    val storeDataIn = Vec(StorePipelineWidth, Flipped(Valid(new MemExuOutput))) // store data, send to sq from rs
78    val storeMaskIn = Vec(StorePipelineWidth, Flipped(Valid(new StoreMaskBundle))) // store mask, send to sq from rs
79    val sbuffer = Vec(EnsbufferWidth, Decoupled(new DCacheWordReqWithVaddrAndPfFlag)) // write committed store to sbuffer
80    val uncacheOutstanding = Input(Bool())
81    val mmioStout = DecoupledIO(new MemExuOutput) // writeback uncached store
82    val forward = Vec(LoadPipelineWidth, Flipped(new PipeLoadForwardQueryIO))
83    val rob = Flipped(new RobLsqIO)
84    val uncache = new UncacheWordIO
85    // val refill = Flipped(Valid(new DCacheLineReq ))
86    val exceptionAddr = new ExceptionAddrIO
87    val sqEmpty = Output(Bool())
88    val stAddrReadySqPtr = Output(new SqPtr)
89    val stAddrReadyVec = Output(Vec(StoreQueueSize, Bool()))
90    val stDataReadySqPtr = Output(new SqPtr)
91    val stDataReadyVec = Output(Vec(StoreQueueSize, Bool()))
92    val stIssuePtr = Output(new SqPtr)
93    val sqDeqPtr = Output(new SqPtr)
94    val sqFull = Output(Bool())
95    val sqCancelCnt = Output(UInt(log2Up(StoreQueueSize + 1).W))
96    val sqDeq = Output(UInt(log2Ceil(EnsbufferWidth + 1).W))
97    val force_write = Output(Bool())
98    val vecStoreRetire = Flipped(ValidIO(new SqPtr))
99  })
100
101  println("StoreQueue: size:" + StoreQueueSize)
102
103  // data modules
104  val uop = Reg(Vec(StoreQueueSize, new DynInst))
105  // val data = Reg(Vec(StoreQueueSize, new LsqEntry))
106  val dataModule = Module(new SQDataModule(
107    numEntries = StoreQueueSize,
108    numRead = EnsbufferWidth,
109    numWrite = StorePipelineWidth,
110    numForward = LoadPipelineWidth
111  ))
112  dataModule.io := DontCare
113  val paddrModule = Module(new SQAddrModule(
114    dataWidth = PAddrBits,
115    numEntries = StoreQueueSize,
116    numRead = EnsbufferWidth,
117    numWrite = StorePipelineWidth,
118    numForward = LoadPipelineWidth
119  ))
120  paddrModule.io := DontCare
121  val vaddrModule = Module(new SQAddrModule(
122    dataWidth = VAddrBits,
123    numEntries = StoreQueueSize,
124    numRead = EnsbufferWidth + 1, // sbuffer + badvaddr 1 (TODO)
125    numWrite = StorePipelineWidth,
126    numForward = LoadPipelineWidth
127  ))
128  vaddrModule.io := DontCare
129  val dataBuffer = Module(new DatamoduleResultBuffer(new DataBufferEntry))
130  val debug_paddr = Reg(Vec(StoreQueueSize, UInt((PAddrBits).W)))
131  val debug_vaddr = Reg(Vec(StoreQueueSize, UInt((VAddrBits).W)))
132  val debug_data = Reg(Vec(StoreQueueSize, UInt((XLEN).W)))
133
134  // state & misc
135  val allocated = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // sq entry has been allocated
136  val addrvalid = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // non-mmio addr is valid
137  val datavalid = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // non-mmio data is valid
138  val allvalid  = VecInit((0 until StoreQueueSize).map(i => addrvalid(i) && datavalid(i))) // non-mmio data & addr is valid
139  val committed = Reg(Vec(StoreQueueSize, Bool())) // inst has been committed by rob
140  val pending = Reg(Vec(StoreQueueSize, Bool())) // mmio pending: inst is an mmio inst, it will not be executed until it reachs the end of rob
141  val mmio = Reg(Vec(StoreQueueSize, Bool())) // mmio: inst is an mmio inst
142  val atomic = Reg(Vec(StoreQueueSize, Bool()))
143  val prefetch = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // need prefetch when committing this store to sbuffer?
144  val vec = Reg(Vec(StoreQueueSize, Bool()))
145  val vecAddrvalid = Reg(Vec(StoreQueueSize, Bool())) // TODO
146
147  // ptr
148  val enqPtrExt = RegInit(VecInit((0 until io.enq.req.length).map(_.U.asTypeOf(new SqPtr))))
149  val rdataPtrExt = RegInit(VecInit((0 until EnsbufferWidth).map(_.U.asTypeOf(new SqPtr))))
150  val deqPtrExt = RegInit(VecInit((0 until EnsbufferWidth).map(_.U.asTypeOf(new SqPtr))))
151  val cmtPtrExt = RegInit(VecInit((0 until CommitWidth).map(_.U.asTypeOf(new SqPtr))))
152  val addrReadyPtrExt = RegInit(0.U.asTypeOf(new SqPtr))
153  val dataReadyPtrExt = RegInit(0.U.asTypeOf(new SqPtr))
154  val validCounter = RegInit(0.U(log2Ceil(VirtualLoadQueueSize + 1).W))
155
156  val enqPtr = enqPtrExt(0).value
157  val deqPtr = deqPtrExt(0).value
158  val cmtPtr = cmtPtrExt(0).value
159
160  val validCount = distanceBetween(enqPtrExt(0), deqPtrExt(0))
161  val allowEnqueue = validCount <= (StoreQueueSize - LSQStEnqWidth).U
162
163  val deqMask = UIntToMask(deqPtr, StoreQueueSize)
164  val enqMask = UIntToMask(enqPtr, StoreQueueSize)
165
166  val commitCount = RegNext(io.rob.scommit)
167
168  // store can be committed by ROB
169  io.rob.mmio := DontCare
170  io.rob.uop := DontCare
171
172  // Read dataModule
173  assert(EnsbufferWidth <= 2)
174  // rdataPtrExtNext and rdataPtrExtNext+1 entry will be read from dataModule
175  val rdataPtrExtNext = WireInit(Mux(dataBuffer.io.enq(1).fire,
176    VecInit(rdataPtrExt.map(_ + 2.U)),
177    Mux(dataBuffer.io.enq(0).fire || io.mmioStout.fire || io.vecStoreRetire.valid,
178      VecInit(rdataPtrExt.map(_ + 1.U)),
179      rdataPtrExt
180    )
181  ))
182
183  // deqPtrExtNext traces which inst is about to leave store queue
184  //
185  // io.sbuffer(i).fire is RegNexted, as sbuffer data write takes 2 cycles.
186  // Before data write finish, sbuffer is unable to provide store to load
187  // forward data. As an workaround, deqPtrExt and allocated flag update
188  // is delayed so that load can get the right data from store queue.
189  //
190  // Modify deqPtrExtNext and io.sqDeq with care!
191  val deqPtrExtNext = Mux(RegNext(io.sbuffer(1).fire),
192    VecInit(deqPtrExt.map(_ + 2.U)),
193    Mux(RegNext(io.sbuffer(0).fire) || io.mmioStout.fire || io.vecStoreRetire.valid,
194      VecInit(deqPtrExt.map(_ + 1.U)),
195      deqPtrExt
196    )
197  )
198  io.sqDeq := RegNext(Mux(RegNext(io.sbuffer(1).fire), 2.U,
199    Mux(RegNext(io.sbuffer(0).fire) || io.mmioStout.fire || io.vecStoreRetire.valid, 1.U, 0.U)
200  ))
201  assert(!RegNext(RegNext(io.sbuffer(0).fire) && io.mmioStout.fire))
202
203  for (i <- 0 until EnsbufferWidth) {
204    dataModule.io.raddr(i) := rdataPtrExtNext(i).value
205    paddrModule.io.raddr(i) := rdataPtrExtNext(i).value
206    vaddrModule.io.raddr(i) := rdataPtrExtNext(i).value
207  }
208
209  // no inst will be committed 1 cycle before tval update
210  vaddrModule.io.raddr(EnsbufferWidth) := (cmtPtrExt(0) + commitCount).value
211
212  /**
213    * Enqueue at dispatch
214    *
215    * Currently, StoreQueue only allows enqueue when #emptyEntries > EnqWidth
216    */
217  io.enq.canAccept := allowEnqueue
218  val canEnqueue = io.enq.req.map(_.valid)
219  val enqCancel = io.enq.req.map(_.bits.robIdx.needFlush(io.brqRedirect))
220  for (i <- 0 until io.enq.req.length) {
221    val offset = if (i == 0) 0.U else PopCount(io.enq.needAlloc.take(i))
222    val sqIdx = enqPtrExt(offset)
223    val index = io.enq.req(i).bits.sqIdx.value
224    val enqInstr = io.enq.req(i).bits.instr.asTypeOf(new XSInstBitFields)
225    when (canEnqueue(i) && !enqCancel(i)) {
226      uop(index) := io.enq.req(i).bits
227      // NOTE: the index will be used when replay
228      uop(index).sqIdx := sqIdx
229      allocated(index) := true.B
230      datavalid(index) := false.B
231      addrvalid(index) := false.B
232      committed(index) := false.B
233      pending(index) := false.B
234      prefetch(index) := false.B
235      mmio(index) := false.B
236      vec(index) := enqInstr.isVecStore // check vector store by the encoding of inst
237      vecAddrvalid(index) := false.B//TODO
238
239      XSError(!io.enq.canAccept || !io.enq.lqCanAccept, s"must accept $i\n")
240      XSError(index =/= sqIdx.value, s"must be the same entry $i\n")
241    }
242    io.enq.resp(i) := sqIdx
243  }
244  XSDebug(p"(ready, valid): ${io.enq.canAccept}, ${Binary(Cat(io.enq.req.map(_.valid)))}\n")
245
246  /**
247    * Update addr/dataReadyPtr when issue from rs
248    */
249  // update issuePtr
250  val IssuePtrMoveStride = 4
251  require(IssuePtrMoveStride >= 2)
252
253  val addrReadyLookupVec = (0 until IssuePtrMoveStride).map(addrReadyPtrExt + _.U)
254  val addrReadyLookup = addrReadyLookupVec.map(ptr => allocated(ptr.value) && (mmio(ptr.value) || addrvalid(ptr.value) || (vec(ptr.value) && vecAddrvalid(ptr.value))) && ptr =/= enqPtrExt(0))
255  val nextAddrReadyPtr = addrReadyPtrExt + PriorityEncoder(VecInit(addrReadyLookup.map(!_) :+ true.B))
256  addrReadyPtrExt := nextAddrReadyPtr
257
258  (0 until StoreQueueSize).map(i => {
259    io.stAddrReadyVec(i) := RegNext(allocated(i) && (mmio(i) || addrvalid(i)))
260  })
261
262  when (io.brqRedirect.valid) {
263    addrReadyPtrExt := Mux(
264      isAfter(cmtPtrExt(0), deqPtrExt(0)),
265      cmtPtrExt(0),
266      deqPtrExtNext(0) // for mmio insts, deqPtr may be ahead of cmtPtr
267    )
268  }
269
270  io.stAddrReadySqPtr := addrReadyPtrExt
271
272  // update
273  val dataReadyLookupVec = (0 until IssuePtrMoveStride).map(dataReadyPtrExt + _.U)
274  val dataReadyLookup = dataReadyLookupVec.map(ptr => allocated(ptr.value) && (mmio(ptr.value) || datavalid(ptr.value) || vec(ptr.value)) && ptr =/= enqPtrExt(0)) // TODO : flag of vector store data valid not add yet
275  val nextDataReadyPtr = dataReadyPtrExt + PriorityEncoder(VecInit(dataReadyLookup.map(!_) :+ true.B))
276  dataReadyPtrExt := nextDataReadyPtr
277
278  (0 until StoreQueueSize).map(i => {
279    io.stDataReadyVec(i) := RegNext(allocated(i) && (mmio(i) || datavalid(i)))
280  })
281
282  when (io.brqRedirect.valid) {
283    dataReadyPtrExt := Mux(
284      isAfter(cmtPtrExt(0), deqPtrExt(0)),
285      cmtPtrExt(0),
286      deqPtrExtNext(0) // for mmio insts, deqPtr may be ahead of cmtPtr
287    )
288  }
289
290  io.stDataReadySqPtr := dataReadyPtrExt
291  io.stIssuePtr := enqPtrExt(0)
292  io.sqDeqPtr := deqPtrExt(0)
293
294  /**
295    * Writeback store from store units
296    *
297    * Most store instructions writeback to regfile in the previous cycle.
298    * However,
299    *   (1) For an mmio instruction with exceptions, we need to mark it as addrvalid
300    * (in this way it will trigger an exception when it reaches ROB's head)
301    * instead of pending to avoid sending them to lower level.
302    *   (2) For an mmio instruction without exceptions, we mark it as pending.
303    * When the instruction reaches ROB's head, StoreQueue sends it to uncache channel.
304    * Upon receiving the response, StoreQueue writes back the instruction
305    * through arbiter with store units. It will later commit as normal.
306    */
307
308  // Write addr to sq
309  for (i <- 0 until StorePipelineWidth) {
310    paddrModule.io.wen(i) := false.B
311    vaddrModule.io.wen(i) := false.B
312    dataModule.io.mask.wen(i) := false.B
313    val stWbIndex = io.storeAddrIn(i).bits.uop.sqIdx.value
314    when (io.storeAddrIn(i).fire) {
315      val addr_valid = !io.storeAddrIn(i).bits.miss
316      addrvalid(stWbIndex) := addr_valid //!io.storeAddrIn(i).bits.mmio
317      // pending(stWbIndex) := io.storeAddrIn(i).bits.mmio
318
319      paddrModule.io.waddr(i) := stWbIndex
320      paddrModule.io.wdata(i) := io.storeAddrIn(i).bits.paddr
321      paddrModule.io.wmask(i) := io.storeAddrIn(i).bits.mask
322      paddrModule.io.wlineflag(i) := io.storeAddrIn(i).bits.wlineflag
323      paddrModule.io.wen(i) := true.B
324
325      vaddrModule.io.waddr(i) := stWbIndex
326      vaddrModule.io.wdata(i) := io.storeAddrIn(i).bits.vaddr
327      vaddrModule.io.wmask(i) := io.storeAddrIn(i).bits.mask
328      vaddrModule.io.wlineflag(i) := io.storeAddrIn(i).bits.wlineflag
329      vaddrModule.io.wen(i) := true.B
330
331      debug_paddr(paddrModule.io.waddr(i)) := paddrModule.io.wdata(i)
332
333      // mmio(stWbIndex) := io.storeAddrIn(i).bits.mmio
334
335      uop(stWbIndex) := io.storeAddrIn(i).bits.uop
336      uop(stWbIndex).debugInfo := io.storeAddrIn(i).bits.uop.debugInfo
337      XSInfo("store addr write to sq idx %d pc 0x%x miss:%d vaddr %x paddr %x mmio %x\n",
338        io.storeAddrIn(i).bits.uop.sqIdx.value,
339        io.storeAddrIn(i).bits.uop.pc,
340        io.storeAddrIn(i).bits.miss,
341        io.storeAddrIn(i).bits.vaddr,
342        io.storeAddrIn(i).bits.paddr,
343        io.storeAddrIn(i).bits.mmio
344      )
345    }
346
347    // re-replinish mmio, for pma/pmp will get mmio one cycle later
348    val storeAddrInFireReg = RegNext(io.storeAddrIn(i).fire && !io.storeAddrIn(i).bits.miss)
349    val stWbIndexReg = RegNext(stWbIndex)
350    when (storeAddrInFireReg) {
351      pending(stWbIndexReg) := io.storeAddrInRe(i).mmio
352      mmio(stWbIndexReg) := io.storeAddrInRe(i).mmio
353      atomic(stWbIndexReg) := io.storeAddrInRe(i).atomic
354    }
355    // dcache miss info (one cycle later than storeIn)
356    // if dcache report a miss in sta pipeline, this store will trigger a prefetch when committing to sbuffer (if EnableAtCommitMissTrigger)
357    when (storeAddrInFireReg) {
358      prefetch(stWbIndexReg) := io.storeAddrInRe(i).miss
359    }
360
361    when(vaddrModule.io.wen(i)){
362      debug_vaddr(vaddrModule.io.waddr(i)) := vaddrModule.io.wdata(i)
363    }
364    // TODO :  When lastElem issue to stu or inactivative issue in vsFlowQueue, set vector store addr ready
365    val vecStWbIndex = io.vecStoreAddrIn(i).bits.uop.sqIdx.value
366    val vecFlowStWbIndex = io.vecStoreAddrInactivate(i).bits.uop.sqIdx.value
367    when(io.vecStoreAddrIn(i).fire && io.vecStoreAddrIn(i).bits.isLastElem){
368      vecAddrvalid(vecStWbIndex) := !io.vecStoreAddrIn(i).bits.miss
369    }
370    when(io.vecStoreAddrInactivate(i).fire){
371      vecAddrvalid(vecFlowStWbIndex) := true.B
372    }
373  }
374
375  // Write data to sq
376  // Now store data pipeline is actually 2 stages
377  for (i <- 0 until StorePipelineWidth) {
378    dataModule.io.data.wen(i) := false.B
379    val stWbIndex = io.storeDataIn(i).bits.uop.sqIdx.value
380    // sq data write takes 2 cycles:
381    // sq data write s0
382    when (io.storeDataIn(i).fire) {
383      // send data write req to data module
384      dataModule.io.data.waddr(i) := stWbIndex
385      dataModule.io.data.wdata(i) := Mux(io.storeDataIn(i).bits.uop.fuOpType === LSUOpType.cbo_zero,
386        0.U,
387        genWdata(io.storeDataIn(i).bits.data, io.storeDataIn(i).bits.uop.fuOpType(1,0))
388      )
389      dataModule.io.data.wen(i) := true.B
390
391      debug_data(dataModule.io.data.waddr(i)) := dataModule.io.data.wdata(i)
392
393      XSInfo("store data write to sq idx %d pc 0x%x data %x -> %x\n",
394        io.storeDataIn(i).bits.uop.sqIdx.value,
395        io.storeDataIn(i).bits.uop.pc,
396        io.storeDataIn(i).bits.data,
397        dataModule.io.data.wdata(i)
398      )
399    }
400    // sq data write s1
401    when (
402      RegNext(io.storeDataIn(i).fire)
403      // && !RegNext(io.storeDataIn(i).bits.uop).robIdx.needFlush(io.brqRedirect)
404    ) {
405      datavalid(RegNext(stWbIndex)) := true.B
406    }
407  }
408
409  // Write mask to sq
410  for (i <- 0 until StorePipelineWidth) {
411    // sq mask write s0
412    when (io.storeMaskIn(i).fire) {
413      // send data write req to data module
414      dataModule.io.mask.waddr(i) := io.storeMaskIn(i).bits.sqIdx.value
415      dataModule.io.mask.wdata(i) := io.storeMaskIn(i).bits.mask
416      dataModule.io.mask.wen(i) := true.B
417    }
418  }
419
420  /**
421    * load forward query
422    *
423    * Check store queue for instructions that is older than the load.
424    * The response will be valid at the next cycle after req.
425    */
426  // check over all lq entries and forward data from the first matched store
427  for (i <- 0 until LoadPipelineWidth) {
428    // Compare deqPtr (deqPtr) and forward.sqIdx, we have two cases:
429    // (1) if they have the same flag, we need to check range(tail, sqIdx)
430    // (2) if they have different flags, we need to check range(tail, VirtualLoadQueueSize) and range(0, sqIdx)
431    // Forward1: Mux(same_flag, range(tail, sqIdx), range(tail, VirtualLoadQueueSize))
432    // Forward2: Mux(same_flag, 0.U,                   range(0, sqIdx)    )
433    // i.e. forward1 is the target entries with the same flag bits and forward2 otherwise
434    val differentFlag = deqPtrExt(0).flag =/= io.forward(i).sqIdx.flag
435    val forwardMask = io.forward(i).sqIdxMask
436    // all addrvalid terms need to be checked
437    val addrValidVec = WireInit(VecInit((0 until StoreQueueSize).map(j => addrvalid(j) && allocated(j))))
438    val dataValidVec = WireInit(VecInit((0 until StoreQueueSize).map(j => datavalid(j))))
439    val allValidVec  = WireInit(VecInit((0 until StoreQueueSize).map(j => addrvalid(j) && datavalid(j) && allocated(j))))
440
441    val lfstEnable = Constantin.createRecord("LFSTEnable", LFSTEnable.B).orR
442    val storeSetHitVec = Mux(lfstEnable,
443      WireInit(VecInit((0 until StoreQueueSize).map(j => io.forward(i).uop.loadWaitBit && uop(j).robIdx === io.forward(i).uop.waitForRobIdx))),
444      WireInit(VecInit((0 until StoreQueueSize).map(j => uop(j).storeSetHit && uop(j).ssid === io.forward(i).uop.ssid)))
445    )
446
447    val forwardMask1 = Mux(differentFlag, ~deqMask, deqMask ^ forwardMask)
448    val forwardMask2 = Mux(differentFlag, forwardMask, 0.U(StoreQueueSize.W))
449    val canForward1 = forwardMask1 & allValidVec.asUInt
450    val canForward2 = forwardMask2 & allValidVec.asUInt
451    val needForward = Mux(differentFlag, ~deqMask | forwardMask, deqMask ^ forwardMask)
452
453    XSDebug(p"$i f1 ${Binary(canForward1)} f2 ${Binary(canForward2)} " +
454      p"sqIdx ${io.forward(i).sqIdx} pa ${Hexadecimal(io.forward(i).paddr)}\n"
455    )
456
457    // do real fwd query (cam lookup in load_s1)
458    dataModule.io.needForward(i)(0) := canForward1 & vaddrModule.io.forwardMmask(i).asUInt
459    dataModule.io.needForward(i)(1) := canForward2 & vaddrModule.io.forwardMmask(i).asUInt
460
461    vaddrModule.io.forwardMdata(i) := io.forward(i).vaddr
462    vaddrModule.io.forwardDataMask(i) := io.forward(i).mask
463    paddrModule.io.forwardMdata(i) := io.forward(i).paddr
464    paddrModule.io.forwardDataMask(i) := io.forward(i).mask
465
466
467    // vaddr cam result does not equal to paddr cam result
468    // replay needed
469    // val vpmaskNotEqual = ((paddrModule.io.forwardMmask(i).asUInt ^ vaddrModule.io.forwardMmask(i).asUInt) & needForward) =/= 0.U
470    // val vaddrMatchFailed = vpmaskNotEqual && io.forward(i).valid
471    val vpmaskNotEqual = (
472      (RegNext(paddrModule.io.forwardMmask(i).asUInt) ^ RegNext(vaddrModule.io.forwardMmask(i).asUInt)) &
473      RegNext(needForward) &
474      RegNext(addrValidVec.asUInt)
475    ) =/= 0.U
476    val vaddrMatchFailed = vpmaskNotEqual && RegNext(io.forward(i).valid)
477    when (vaddrMatchFailed) {
478      XSInfo("vaddrMatchFailed: pc %x pmask %x vmask %x\n",
479        RegNext(io.forward(i).uop.pc),
480        RegNext(needForward & paddrModule.io.forwardMmask(i).asUInt),
481        RegNext(needForward & vaddrModule.io.forwardMmask(i).asUInt)
482      );
483    }
484    XSPerfAccumulate("vaddr_match_failed", vpmaskNotEqual)
485    XSPerfAccumulate("vaddr_match_really_failed", vaddrMatchFailed)
486
487    // Fast forward mask will be generated immediately (load_s1)
488    io.forward(i).forwardMaskFast := dataModule.io.forwardMaskFast(i)
489
490    // Forward result will be generated 1 cycle later (load_s2)
491    io.forward(i).forwardMask := dataModule.io.forwardMask(i)
492    io.forward(i).forwardData := dataModule.io.forwardData(i)
493    // If addr match, data not ready, mark it as dataInvalid
494    // load_s1: generate dataInvalid in load_s1 to set fastUop
495    val dataInvalidMask1 = (addrValidVec.asUInt & ~dataValidVec.asUInt & vaddrModule.io.forwardMmask(i).asUInt & forwardMask1.asUInt)
496    val dataInvalidMask2 = (addrValidVec.asUInt & ~dataValidVec.asUInt & vaddrModule.io.forwardMmask(i).asUInt & forwardMask2.asUInt)
497    val dataInvalidMask = dataInvalidMask1 | dataInvalidMask2
498    io.forward(i).dataInvalidFast := dataInvalidMask.orR
499
500    // make chisel happy
501    val dataInvalidMask1Reg = Wire(UInt(StoreQueueSize.W))
502    dataInvalidMask1Reg := RegNext(dataInvalidMask1)
503    // make chisel happy
504    val dataInvalidMask2Reg = Wire(UInt(StoreQueueSize.W))
505    dataInvalidMask2Reg := RegNext(dataInvalidMask2)
506    val dataInvalidMaskReg = dataInvalidMask1Reg | dataInvalidMask2Reg
507
508    // If SSID match, address not ready, mark it as addrInvalid
509    // load_s2: generate addrInvalid
510    val addrInvalidMask1 = (~addrValidVec.asUInt & storeSetHitVec.asUInt & forwardMask1.asUInt)
511    val addrInvalidMask2 = (~addrValidVec.asUInt & storeSetHitVec.asUInt & forwardMask2.asUInt)
512    // make chisel happy
513    val addrInvalidMask1Reg = Wire(UInt(StoreQueueSize.W))
514    addrInvalidMask1Reg := RegNext(addrInvalidMask1)
515    // make chisel happy
516    val addrInvalidMask2Reg = Wire(UInt(StoreQueueSize.W))
517    addrInvalidMask2Reg := RegNext(addrInvalidMask2)
518    val addrInvalidMaskReg = addrInvalidMask1Reg | addrInvalidMask2Reg
519
520    // load_s2
521    io.forward(i).dataInvalid := RegNext(io.forward(i).dataInvalidFast)
522    // check if vaddr forward mismatched
523    io.forward(i).matchInvalid := vaddrMatchFailed
524
525    // data invalid sq index
526    // check whether false fail
527    // check flag
528    val s2_differentFlag = RegNext(differentFlag)
529    val s2_enqPtrExt = RegNext(enqPtrExt(0))
530    val s2_deqPtrExt = RegNext(deqPtrExt(0))
531
532    // addr invalid sq index
533    // make chisel happy
534    val addrInvalidMaskRegWire = Wire(UInt(StoreQueueSize.W))
535    addrInvalidMaskRegWire := addrInvalidMaskReg
536    val addrInvalidFlag = addrInvalidMaskRegWire.orR
537    val hasInvalidAddr = (~addrValidVec.asUInt & needForward).orR
538
539    val addrInvalidSqIdx1 = OHToUInt(Reverse(PriorityEncoderOH(Reverse(addrInvalidMask1Reg))))
540    val addrInvalidSqIdx2 = OHToUInt(Reverse(PriorityEncoderOH(Reverse(addrInvalidMask2Reg))))
541    val addrInvalidSqIdx = Mux(addrInvalidMask2Reg.orR, addrInvalidSqIdx2, addrInvalidSqIdx1)
542
543    // store-set content management
544    //                +-----------------------+
545    //                | Search a SSID for the |
546    //                |    load operation     |
547    //                +-----------------------+
548    //                           |
549    //                           V
550    //                 +-------------------+
551    //                 | load wait strict? |
552    //                 +-------------------+
553    //                           |
554    //                           V
555    //               +----------------------+
556    //            Set|                      |Clean
557    //               V                      V
558    //  +------------------------+   +------------------------------+
559    //  | Waiting for all older  |   | Wait until the corresponding |
560    //  |   stores operations    |   | older store operations       |
561    //  +------------------------+   +------------------------------+
562
563
564
565    when (RegNext(io.forward(i).uop.loadWaitStrict)) {
566      io.forward(i).addrInvalidSqIdx := RegNext(io.forward(i).uop.sqIdx - 1.U)
567    } .elsewhen (addrInvalidFlag) {
568      io.forward(i).addrInvalidSqIdx.flag := Mux(!s2_differentFlag || addrInvalidSqIdx >= s2_deqPtrExt.value, s2_deqPtrExt.flag, s2_enqPtrExt.flag)
569      io.forward(i).addrInvalidSqIdx.value := addrInvalidSqIdx
570    } .otherwise {
571      // may be store inst has been written to sbuffer already.
572      io.forward(i).addrInvalidSqIdx := RegNext(io.forward(i).uop.sqIdx)
573    }
574    io.forward(i).addrInvalid := Mux(RegNext(io.forward(i).uop.loadWaitStrict), RegNext(hasInvalidAddr), addrInvalidFlag)
575
576    // data invalid sq index
577    // make chisel happy
578    val dataInvalidMaskRegWire = Wire(UInt(StoreQueueSize.W))
579    dataInvalidMaskRegWire := dataInvalidMaskReg
580    val dataInvalidFlag = dataInvalidMaskRegWire.orR
581
582    val dataInvalidSqIdx1 = OHToUInt(Reverse(PriorityEncoderOH(Reverse(dataInvalidMask1Reg))))
583    val dataInvalidSqIdx2 = OHToUInt(Reverse(PriorityEncoderOH(Reverse(dataInvalidMask2Reg))))
584    val dataInvalidSqIdx = Mux(dataInvalidMask2Reg.orR, dataInvalidSqIdx2, dataInvalidSqIdx1)
585
586    when (dataInvalidFlag) {
587      io.forward(i).dataInvalidSqIdx.flag := Mux(!s2_differentFlag || dataInvalidSqIdx >= s2_deqPtrExt.value, s2_deqPtrExt.flag, s2_enqPtrExt.flag)
588      io.forward(i).dataInvalidSqIdx.value := dataInvalidSqIdx
589    } .otherwise {
590      // may be store inst has been written to sbuffer already.
591      io.forward(i).dataInvalidSqIdx := RegNext(io.forward(i).uop.sqIdx)
592    }
593  }
594
595  /**
596    * Memory mapped IO / other uncached operations
597    *
598    * States:
599    * (1) writeback from store units: mark as pending
600    * (2) when they reach ROB's head, they can be sent to uncache channel
601    * (3) response from uncache channel: mark as datavalidmask.wen
602    * (4) writeback to ROB (and other units): mark as writebacked
603    * (5) ROB commits the instruction: same as normal instructions
604    */
605  //(2) when they reach ROB's head, they can be sent to uncache channel
606  val s_idle :: s_req :: s_resp :: s_wb :: s_wait :: Nil = Enum(5)
607  val uncacheState = RegInit(s_idle)
608  switch(uncacheState) {
609    is(s_idle) {
610      when(RegNext(io.rob.pendingst && pending(deqPtr) && allocated(deqPtr) && datavalid(deqPtr) && addrvalid(deqPtr))) {
611        uncacheState := s_req
612      }
613    }
614    is(s_req) {
615      when (io.uncache.req.fire) {
616        when (io.uncacheOutstanding) {
617          uncacheState := s_wb
618        } .otherwise {
619          uncacheState := s_resp
620        }
621      }
622    }
623    is(s_resp) {
624      when(io.uncache.resp.fire) {
625        uncacheState := s_wb
626      }
627    }
628    is(s_wb) {
629      when (io.mmioStout.fire) {
630        uncacheState := s_wait
631      }
632    }
633    is(s_wait) {
634      when(commitCount > 0.U) {
635        uncacheState := s_idle // ready for next mmio
636      }
637    }
638  }
639  io.uncache.req.valid := uncacheState === s_req
640
641  io.uncache.req.bits := DontCare
642  io.uncache.req.bits.cmd  := MemoryOpConstants.M_XWR
643  io.uncache.req.bits.addr := paddrModule.io.rdata(0) // data(deqPtr) -> rdata(0)
644  io.uncache.req.bits.data := shiftDataToLow(paddrModule.io.rdata(0), dataModule.io.rdata(0).data)
645  io.uncache.req.bits.mask := shiftMaskToLow(paddrModule.io.rdata(0), dataModule.io.rdata(0).mask)
646
647  // CBO op type check can be delayed for 1 cycle,
648  // as uncache op will not start in s_idle
649  val cbo_mmio_addr = paddrModule.io.rdata(0) >> 2 << 2 // clear lowest 2 bits for op
650  val cbo_mmio_op = 0.U //TODO
651  val cbo_mmio_data = cbo_mmio_addr | cbo_mmio_op
652  when(RegNext(LSUOpType.isCbo(uop(deqPtr).fuOpType))){
653    io.uncache.req.bits.addr := DontCare // TODO
654    io.uncache.req.bits.data := paddrModule.io.rdata(0)
655    io.uncache.req.bits.mask := DontCare // TODO
656  }
657
658  io.uncache.req.bits.atomic := atomic(RegNext(rdataPtrExtNext(0)).value)
659
660  when(io.uncache.req.fire){
661    // mmio store should not be committed until uncache req is sent
662    pending(deqPtr) := false.B
663
664    XSDebug(
665      p"uncache req: pc ${Hexadecimal(uop(deqPtr).pc)} " +
666      p"addr ${Hexadecimal(io.uncache.req.bits.addr)} " +
667      p"data ${Hexadecimal(io.uncache.req.bits.data)} " +
668      p"op ${Hexadecimal(io.uncache.req.bits.cmd)} " +
669      p"mask ${Hexadecimal(io.uncache.req.bits.mask)}\n"
670    )
671  }
672
673  // (3) response from uncache channel: mark as datavalid
674  io.uncache.resp.ready := true.B
675
676  // (4) writeback to ROB (and other units): mark as writebacked
677  io.mmioStout.valid := uncacheState === s_wb
678  io.mmioStout.bits.uop := uop(deqPtr)
679  io.mmioStout.bits.uop.sqIdx := deqPtrExt(0)
680  io.mmioStout.bits.data := shiftDataToLow(paddrModule.io.rdata(0), dataModule.io.rdata(0).data) // dataModule.io.rdata.read(deqPtr)
681  io.mmioStout.bits.debug.isMMIO := true.B
682  io.mmioStout.bits.debug.paddr := DontCare
683  io.mmioStout.bits.debug.isPerfCnt := false.B
684  io.mmioStout.bits.debug.vaddr := DontCare
685  // Remove MMIO inst from store queue after MMIO request is being sent
686  // That inst will be traced by uncache state machine
687  when (io.mmioStout.fire) {
688    allocated(deqPtr) := false.B
689  }
690
691  /**
692    * ROB commits store instructions (mark them as committed)
693    *
694    * (1) When store commits, mark it as committed.
695    * (2) They will not be cancelled and can be sent to lower level.
696    */
697  XSError(uncacheState =/= s_idle && uncacheState =/= s_wait && commitCount > 0.U,
698   "should not commit instruction when MMIO has not been finished\n")
699  for (i <- 0 until CommitWidth) {
700    when (commitCount > i.U) { // MMIO inst is not in progress
701      if(i == 0){
702        // MMIO inst should not update committed flag
703        // Note that commit count has been delayed for 1 cycle
704        when(uncacheState === s_idle){
705          committed(cmtPtrExt(0).value) := true.B
706        }
707      } else {
708        committed(cmtPtrExt(i).value) := true.B
709      }
710    }
711  }
712  cmtPtrExt := cmtPtrExt.map(_ + commitCount)
713
714  // committed stores will not be cancelled and can be sent to lower level.
715  // remove retired insts from sq, add retired store to sbuffer
716
717  // Read data from data module
718  // As store queue grows larger and larger, time needed to read data from data
719  // module keeps growing higher. Now we give data read a whole cycle.
720
721  // Vector stores are written to sbuffer by vector store flow queue rather than sq
722  XSError(io.vecStoreRetire.valid && !vec(rdataPtrExt(0).value), "Vector store flow queue is trying to retire a scalar store")
723  XSError(io.vecStoreRetire.valid && !allocated(rdataPtrExt(0).value), "Vector store flow queue is trying to retire an invalid entry")
724  XSError(io.vecStoreRetire.valid && vec(rdataPtrExt(0).value) && !vecAddrvalid(rdataPtrExt(0).value), "Vector store is trying to retire without write last element!")
725  when (io.vecStoreRetire.valid) {
726    assert(io.vecStoreRetire.bits === rdataPtrExt(0))
727    vec(rdataPtrExt(0).value) := false.B
728    vecAddrvalid(rdataPtrExt(0).value) := false.B
729    allocated(rdataPtrExt(0).value) := false.B
730  }
731
732  val mmioStall = mmio(rdataPtrExt(0).value)
733  val vecStall = vec(rdataPtrExt(0).value)
734  for (i <- 0 until EnsbufferWidth) {
735    val ptr = rdataPtrExt(i).value
736    dataBuffer.io.enq(i).valid := allocated(ptr) && committed(ptr) && !mmioStall && !vecStall
737    // Note that store data/addr should both be valid after store's commit
738    assert(!dataBuffer.io.enq(i).valid || allvalid(ptr))
739    dataBuffer.io.enq(i).bits.addr     := paddrModule.io.rdata(i)
740    dataBuffer.io.enq(i).bits.vaddr    := vaddrModule.io.rdata(i)
741    dataBuffer.io.enq(i).bits.data     := dataModule.io.rdata(i).data
742    dataBuffer.io.enq(i).bits.mask     := dataModule.io.rdata(i).mask
743    dataBuffer.io.enq(i).bits.wline    := paddrModule.io.rlineflag(i)
744    dataBuffer.io.enq(i).bits.sqPtr    := rdataPtrExt(i)
745    dataBuffer.io.enq(i).bits.prefetch := prefetch(ptr)
746  }
747
748  // Send data stored in sbufferReqBitsReg to sbuffer
749  for (i <- 0 until EnsbufferWidth) {
750    io.sbuffer(i).valid := dataBuffer.io.deq(i).valid
751    dataBuffer.io.deq(i).ready := io.sbuffer(i).ready
752    // Write line request should have all 1 mask
753    assert(!(io.sbuffer(i).valid && io.sbuffer(i).bits.wline && !io.sbuffer(i).bits.mask.andR))
754    io.sbuffer(i).bits := DontCare
755    io.sbuffer(i).bits.cmd   := MemoryOpConstants.M_XWR
756    io.sbuffer(i).bits.addr  := dataBuffer.io.deq(i).bits.addr
757    io.sbuffer(i).bits.vaddr := dataBuffer.io.deq(i).bits.vaddr
758    io.sbuffer(i).bits.data  := dataBuffer.io.deq(i).bits.data
759    io.sbuffer(i).bits.mask  := dataBuffer.io.deq(i).bits.mask
760    io.sbuffer(i).bits.wline := dataBuffer.io.deq(i).bits.wline
761    io.sbuffer(i).bits.prefetch := dataBuffer.io.deq(i).bits.prefetch
762
763    // io.sbuffer(i).fire is RegNexted, as sbuffer data write takes 2 cycles.
764    // Before data write finish, sbuffer is unable to provide store to load
765    // forward data. As an workaround, deqPtrExt and allocated flag update
766    // is delayed so that load can get the right data from store queue.
767    val ptr = dataBuffer.io.deq(i).bits.sqPtr.value
768    when (RegNext(io.sbuffer(i).fire)) {
769      allocated(RegEnable(ptr, io.sbuffer(i).fire)) := false.B
770      XSDebug("sbuffer "+i+" fire: ptr %d\n", ptr)
771    }
772  }
773  (1 until EnsbufferWidth).foreach(i => when(io.sbuffer(i).fire) { assert(io.sbuffer(i - 1).fire) })
774  if (coreParams.dcacheParametersOpt.isEmpty) {
775    for (i <- 0 until EnsbufferWidth) {
776      val ptr = deqPtrExt(i).value
777      val ram = DifftestMem(64L * 1024 * 1024 * 1024, 8)
778      val wen = allocated(ptr) && committed(ptr) && !mmio(ptr)
779      val waddr = ((paddrModule.io.rdata(i) - "h80000000".U) >> 3).asUInt
780      val wdata = Mux(paddrModule.io.rdata(i)(3), dataModule.io.rdata(i).data(127, 64), dataModule.io.rdata(i).data(63, 0))
781      val wmask = Mux(paddrModule.io.rdata(i)(3), dataModule.io.rdata(i).mask(15, 8), dataModule.io.rdata(i).mask(7, 0))
782      when (wen) {
783        ram.write(waddr, wdata.asTypeOf(Vec(8, UInt(8.W))), wmask.asBools)
784      }
785    }
786  }
787
788  // Read vaddr for mem exception
789  io.exceptionAddr.vaddr := vaddrModule.io.rdata(EnsbufferWidth)
790
791  // misprediction recovery / exception redirect
792  // invalidate sq term using robIdx
793  val needCancel = Wire(Vec(StoreQueueSize, Bool()))
794  for (i <- 0 until StoreQueueSize) {
795    needCancel(i) := uop(i).robIdx.needFlush(io.brqRedirect) && allocated(i) && !committed(i)
796    when (needCancel(i)) {
797      allocated(i) := false.B
798    }
799  }
800
801 /**
802* update pointers
803**/
804  val lastEnqCancel = PopCount(RegNext(VecInit(canEnqueue.zip(enqCancel).map(x => x._1 && x._2)))) // 1 cycle after redirect
805  val lastCycleCancelCount = PopCount(RegNext(needCancel)) // 1 cycle after redirect
806  val lastCycleRedirect = RegNext(io.brqRedirect.valid) // 1 cycle after redirect
807  val enqNumber = Mux(!lastCycleRedirect&&io.enq.canAccept && io.enq.lqCanAccept, PopCount(io.enq.req.map(_.valid)), 0.U) // 1 cycle after redirect
808
809  val lastlastCycleRedirect=RegNext(lastCycleRedirect)// 2 cycle after redirect
810  val redirectCancelCount = RegEnable(lastCycleCancelCount + lastEnqCancel, lastCycleRedirect) // 2 cycle after redirect
811
812  when (lastlastCycleRedirect) {
813    // we recover the pointers in 2 cycle after redirect for better timing
814    enqPtrExt := VecInit(enqPtrExt.map(_ - redirectCancelCount))
815  }.otherwise {
816    // lastCycleRedirect.valid or nornal case
817    // when lastCycleRedirect.valid, enqNumber === 0.U, enqPtrExt will not change
818    enqPtrExt := VecInit(enqPtrExt.map(_ + enqNumber))
819  }
820  assert(!(lastCycleRedirect && enqNumber =/= 0.U))
821
822  deqPtrExt := deqPtrExtNext
823  rdataPtrExt := rdataPtrExtNext
824
825  // val dequeueCount = Mux(io.sbuffer(1).fire, 2.U, Mux(io.sbuffer(0).fire || io.mmioStout.fire, 1.U, 0.U))
826
827  // If redirect at T0, sqCancelCnt is at T2
828  io.sqCancelCnt := redirectCancelCount
829  val ForceWriteUpper = Wire(UInt(log2Up(StoreQueueSize + 1).W))
830  ForceWriteUpper := Constantin.createRecord("ForceWriteUpper_"+p(XSCoreParamsKey).HartId.toString(), initValue = 60.U)
831  val ForceWriteLower = Wire(UInt(log2Up(StoreQueueSize + 1).W))
832  ForceWriteLower := Constantin.createRecord("ForceWriteLower_"+p(XSCoreParamsKey).HartId.toString(), initValue = 55.U)
833
834  val valid_cnt = PopCount(allocated)
835  io.force_write := RegNext(Mux(valid_cnt >= ForceWriteUpper, true.B, valid_cnt >= ForceWriteLower && io.force_write), init = false.B)
836
837  // io.sqempty will be used by sbuffer
838  // We delay it for 1 cycle for better timing
839  // When sbuffer need to check if it is empty, the pipeline is blocked, which means delay io.sqempty
840  // for 1 cycle will also promise that sq is empty in that cycle
841  io.sqEmpty := RegNext(
842    enqPtrExt(0).value === deqPtrExt(0).value &&
843    enqPtrExt(0).flag === deqPtrExt(0).flag
844  )
845  // perf counter
846  QueuePerf(StoreQueueSize, validCount, !allowEnqueue)
847  io.sqFull := !allowEnqueue
848  XSPerfAccumulate("mmioCycle", uncacheState =/= s_idle) // lq is busy dealing with uncache req
849  XSPerfAccumulate("mmioCnt", io.uncache.req.fire)
850  XSPerfAccumulate("mmio_wb_success", io.mmioStout.fire)
851  XSPerfAccumulate("mmio_wb_blocked", io.mmioStout.valid && !io.mmioStout.ready)
852  XSPerfAccumulate("validEntryCnt", distanceBetween(enqPtrExt(0), deqPtrExt(0)))
853  XSPerfAccumulate("cmtEntryCnt", distanceBetween(cmtPtrExt(0), deqPtrExt(0)))
854  XSPerfAccumulate("nCmtEntryCnt", distanceBetween(enqPtrExt(0), cmtPtrExt(0)))
855
856  val perfValidCount = distanceBetween(enqPtrExt(0), deqPtrExt(0))
857  val perfEvents = Seq(
858    ("mmioCycle      ", uncacheState =/= s_idle),
859    ("mmioCnt        ", io.uncache.req.fire),
860    ("mmio_wb_success", io.mmioStout.fire),
861    ("mmio_wb_blocked", io.mmioStout.valid && !io.mmioStout.ready),
862    ("stq_1_4_valid  ", (perfValidCount < (StoreQueueSize.U/4.U))),
863    ("stq_2_4_valid  ", (perfValidCount > (StoreQueueSize.U/4.U)) & (perfValidCount <= (StoreQueueSize.U/2.U))),
864    ("stq_3_4_valid  ", (perfValidCount > (StoreQueueSize.U/2.U)) & (perfValidCount <= (StoreQueueSize.U*3.U/4.U))),
865    ("stq_4_4_valid  ", (perfValidCount > (StoreQueueSize.U*3.U/4.U))),
866  )
867  generatePerfEvent()
868
869  // debug info
870  XSDebug("enqPtrExt %d:%d deqPtrExt %d:%d\n", enqPtrExt(0).flag, enqPtr, deqPtrExt(0).flag, deqPtr)
871
872  def PrintFlag(flag: Bool, name: String): Unit = {
873    when(flag) {
874      XSDebug(false, true.B, name)
875    }.otherwise {
876      XSDebug(false, true.B, " ")
877    }
878  }
879
880  for (i <- 0 until StoreQueueSize) {
881    XSDebug(i + ": pc %x va %x pa %x data %x ",
882      uop(i).pc,
883      debug_vaddr(i),
884      debug_paddr(i),
885      debug_data(i)
886    )
887    PrintFlag(allocated(i), "a")
888    PrintFlag(allocated(i) && addrvalid(i), "a")
889    PrintFlag(allocated(i) && datavalid(i), "d")
890    PrintFlag(allocated(i) && committed(i), "c")
891    PrintFlag(allocated(i) && pending(i), "p")
892    PrintFlag(allocated(i) && mmio(i), "m")
893    XSDebug(false, true.B, "\n")
894  }
895
896}
897