xref: /XiangShan/src/main/scala/xiangshan/mem/vector/VMergeBuffer.scala (revision 939a787932102e17cb14773366a1dc3579827eb3)
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._
25import xiangshan.backend.rob.RobPtr
26import xiangshan.backend.Bundles._
27import xiangshan.mem._
28import xiangshan.backend.fu.FuType
29import freechips.rocketchip.diplomacy.BufferParams
30
31class MBufferBundle(implicit p: Parameters) extends VLSUBundle{
32  val data             = UInt(VLEN.W)
33  val mask             = UInt(VLENB.W)
34  val flowNum          = UInt(flowIdxBits.W)
35  val exceptionVec     = ExceptionVec()
36  val uop              = new DynInst
37  // val vdOffset         = UInt(vOffsetBits.W)
38  val sourceType       = VSFQFeedbackType()
39  val flushState       = Bool()
40  val vdIdx            = UInt(3.W)
41  // for exception
42  val vstart           = UInt(elemIdxBits.W)
43  val vl               = UInt(elemIdxBits.W)
44  val vaddr            = UInt(VAddrBits.W)
45  val fof              = Bool()
46  val vlmax            = UInt(elemIdxBits.W)
47
48  def allReady(): Bool = (flowNum === 0.U)
49}
50
51abstract class BaseVMergeBuffer(isVStore: Boolean=false)(implicit p: Parameters) extends VLSUModule{
52  val io = IO(new VMergeBufferIO(isVStore))
53
54  def EnqConnect(source: MergeBufferReq, sink: MBufferBundle) = {
55    sink.data         := source.data
56    sink.mask         := source.mask
57    sink.flowNum      := source.flowNum
58    sink.exceptionVec := 0.U.asTypeOf(ExceptionVec())
59    sink.uop          := source.uop
60    sink.sourceType   := 0.U.asTypeOf(VSFQFeedbackType())
61    sink.flushState   := false.B
62    sink.vdIdx        := source.vdIdx
63    sink.fof          := source.fof
64    sink.vlmax        := source.vlmax
65    sink.vl           := source.uop.vpu.vl
66    sink.vstart       := 0.U
67  }
68  def DeqConnect(source: MBufferBundle): MemExuOutput = {
69    val sink               = WireInit(0.U.asTypeOf(new MemExuOutput(isVector = true)))
70    sink.data             := source.data
71    sink.mask.get         := source.mask
72    sink.uop              := source.uop
73    sink.uop.exceptionVec := source.exceptionVec
74    sink.uop.vpu.vmask    := source.mask
75    sink.debug            := 0.U.asTypeOf(new DebugBundle)
76    sink.vdIdxInField.get := source.vdIdx // Mgu needs to use this.
77    sink.vdIdx.get        := source.vdIdx
78    sink.uop.vpu.vstart   := source.vstart
79    sink.uop.vpu.vl       := source.vl
80    sink
81  }
82  def ToLsqConnect(source: MBufferBundle): FeedbackToLsqIO = {
83    val sink                                 = WireInit(0.U.asTypeOf(new FeedbackToLsqIO))
84    val hasExp                               = source.exceptionVec.asUInt.orR
85    sink.robidx                             := source.uop.robIdx
86    sink.uopidx                             := source.uop.uopIdx
87    sink.feedback(VecFeedbacks.COMMIT)      := !hasExp
88    sink.feedback(VecFeedbacks.FLUSH)       := hasExp
89    sink.feedback(VecFeedbacks.LAST)        := true.B
90    sink.vstart                             := source.vstart // TODO: if lsq need vl for fof?
91    sink.vaddr                              := source.vaddr
92    sink.vl                                 := source.vl
93    sink.exceptionVec                       := source.exceptionVec
94    sink
95  }
96  // freeliset: store valid entries index.
97  // +---+---+--------------+-----+-----+
98  // | 0 | 1 |      ......  | n-2 | n-1 |
99  // +---+---+--------------+-----+-----+
100  val freeList: FreeList
101  val uopSize: Int
102  val enqWidth = io.fromSplit.length
103  val deqWidth = io.uopWriteback.length
104  val pipeWidth = io.fromPipeline.length
105
106  val entries      = Reg(Vec(uopSize, new MBufferBundle))
107  val needCancel   = WireInit(VecInit(Seq.fill(uopSize)(false.B)))
108  val allocated    = RegInit(VecInit(Seq.fill(uopSize)(false.B)))
109  val freeMaskVec  = WireInit(VecInit(Seq.fill(uopSize)(false.B)))
110  val uopFinish    = RegInit(VecInit(Seq.fill(uopSize)(false.B)))
111  val needRSReplay = RegInit(VecInit(Seq.fill(uopSize)(false.B)))
112  // enq, from splitPipeline
113  // val allowEnqueue =
114  val cancelEnq    = io.fromSplit.map(_.req.bits.uop.robIdx.needFlush(io.redirect))
115  val canEnqueue   = io.fromSplit.map(_.req.valid)
116  val needEnqueue  = (0 until enqWidth).map{i =>
117    canEnqueue(i) && !cancelEnq(i)
118  }
119
120  val freeCount    = uopSize.U - freeList.io.validCount
121
122  for ((enq, i) <- io.fromSplit.zipWithIndex){
123    freeList.io.doAllocate(i) := false.B
124
125    freeList.io.allocateReq(i) := true.B
126
127    val offset    = PopCount(needEnqueue.take(i))
128    val canAccept = freeList.io.canAllocate(offset)
129    val enqIndex  = freeList.io.allocateSlot(offset)
130    enq.req.ready := freeCount >= (i + 1).U // for better timing
131
132    when(needEnqueue(i) && enq.req.ready){
133      freeList.io.doAllocate(i) := true.B
134      // enqueue
135      allocated(enqIndex)       := true.B
136      uopFinish(enqIndex)       := false.B
137      needRSReplay(enqIndex)    := false.B
138
139      EnqConnect(enq.req.bits, entries(enqIndex))// initial entry
140    }
141
142    enq.resp.bits.mBIndex := enqIndex
143    enq.resp.bits.fail    := false.B
144    enq.resp.valid        := freeCount >= (i + 1).U // for better timing
145  }
146
147  //redirect
148  for (i <- 0 until uopSize){
149    needCancel(i) := entries(i).uop.robIdx.needFlush(io.redirect) && allocated(i)
150    when (needCancel(i)) {
151      allocated(i)   := false.B
152      freeMaskVec(i) := true.B
153      uopFinish(i)   := false.B
154      needRSReplay(i):= false.B
155    }
156  }
157  freeList.io.free := freeMaskVec.asUInt
158  //pipelineWriteback
159  // handle the situation where multiple ports are going to write the same uop queue entry
160  val mergePortMatrix        = Wire(Vec(pipeWidth, Vec(pipeWidth, Bool())))
161  val mergedByPrevPortVec    = Wire(Vec(pipeWidth, Bool()))
162  (0 until pipeWidth).map{case i => (0 until pipeWidth).map{case j =>
163    mergePortMatrix(i)(j) := (j == i).B ||
164      (j > i).B &&
165      io.fromPipeline(j).bits.mBIndex === io.fromPipeline(i).bits.mBIndex &&
166      io.fromPipeline(j).valid
167  }}
168  (0 until pipeWidth).map{case i =>
169    mergedByPrevPortVec(i) := (i != 0).B && Cat((0 until i).map(j =>
170      io.fromPipeline(j).bits.mBIndex === io.fromPipeline(i).bits.mBIndex &&
171      io.fromPipeline(j).valid)).orR
172  }
173  dontTouch(mergePortMatrix)
174  dontTouch(mergedByPrevPortVec)
175
176  // for exception, select exception, when multi port writeback exception, we need select oldest one
177  def selectOldest[T <: VecPipelineFeedbackIO](valid: Seq[Bool], bits: Seq[T], sel: Seq[UInt]): (Seq[Bool], Seq[T], Seq[UInt]) = {
178    assert(valid.length == bits.length)
179    assert(valid.length == sel.length)
180    if (valid.length == 0 || valid.length == 1) {
181      (valid, bits, sel)
182    } else if (valid.length == 2) {
183      val res = Seq.fill(2)(Wire(ValidIO(chiselTypeOf(bits(0)))))
184      for (i <- res.indices) {
185        res(i).valid := valid(i)
186        res(i).bits := bits(i)
187      }
188      val oldest = Mux(valid(0) && valid(1),
189        Mux(sel(0) < sel(1),
190            res(0), res(1)),
191        Mux(valid(0) && !valid(1), res(0), res(1)))
192      (Seq(oldest.valid), Seq(oldest.bits), Seq(0.U))
193    } else {
194      val left  = selectOldest(valid.take(valid.length / 2), bits.take(bits.length / 2), sel.take(sel.length / 2))
195      val right = selectOldest(valid.takeRight(valid.length - (valid.length / 2)), bits.takeRight(bits.length - (bits.length / 2)), sel.takeRight(sel.length - (sel.length / 2)))
196      selectOldest(left._1 ++ right._1, left._2 ++ right._2, left._3 ++ right._3)
197    }
198  }
199
200  val pipeValid        = io.fromPipeline.map(_.valid)
201  val pipeBits         = io.fromPipeline.map(x => x.bits)
202  val wbElemIdx        = pipeBits.map(_.elemIdx)
203  val wbMbIndex        = pipeBits.map(_.mBIndex)
204  val wbElemIdxInField = wbElemIdx.zip(wbMbIndex).map(x => x._1 & (entries(x._2).vlmax - 1.U))
205
206  val portHasExcp       = pipeBits.zip(mergePortMatrix).map{case (port, v) =>
207    (0 until pipeWidth).map{case i =>
208      (v(i) && io.fromPipeline(i).bits.exceptionVec.asUInt.orR && io.fromPipeline(i).bits.mask.orR) // this port have exception or merged port have exception
209    }.reduce(_ || _)
210  }
211
212  for((pipewb, i) <- io.fromPipeline.zipWithIndex){
213    val entry               = entries(wbMbIndex(i))
214    val entryVeew           = entry.uop.vpu.veew
215    val entryIsUS           = LSUOpType.isUStride(entry.uop.fuOpType)
216    val entryExcp           = entry.exceptionVec.asUInt.orR && entry.mask.orR
217
218    val sel                    = selectOldest(mergePortMatrix(i), pipeBits, wbElemIdxInField)
219    val selPort                = sel._2
220    val selElemInfield         = selPort(0).elemIdx & (entries(wbMbIndex(i)).vlmax - 1.U)
221    val selExceptionVec        = selPort(0).exceptionVec
222
223    val USFirstUopOffset       = (searchVFirstUnMask(selPort(0).mask) << entryVeew).asUInt
224    val isUSFirstUop           = !selPort(0).elemIdx.orR
225    val vaddr                  = selPort(0).vaddr + Mux(entryIsUS && isUSFirstUop, USFirstUopOffset, 0.U)
226
227    // select oldest port to raise exception
228    when((((entries(wbMbIndex(i)).vstart >= selElemInfield) && entryExcp && portHasExcp(i)) || (!entryExcp && portHasExcp(i))) && pipewb.valid && !mergedByPrevPortVec(i)){
229      when(!entries(wbMbIndex(i)).fof || selElemInfield === 0.U){
230        // For fof loads, if element 0 raises an exception, vl is not modified, and the trap is taken.
231        entries(wbMbIndex(i)).vstart       := selElemInfield
232        entries(wbMbIndex(i)).exceptionVec := selExceptionVec
233        entries(wbMbIndex(i)).vaddr        := vaddr
234      }.otherwise{
235        entries(wbMbIndex(i)).vl           := selElemInfield
236      }
237    }
238  }
239
240  // for pipeline writeback
241  for((pipewb, i) <- io.fromPipeline.zipWithIndex){
242    val wbIndex          = pipewb.bits.mBIndex
243    val flowNumOffset    = Mux(pipewb.bits.usSecondInv,
244                               2.U,
245                               PopCount(mergePortMatrix(i)))
246    val sourceTypeNext   = entries(wbIndex).sourceType | pipewb.bits.sourceType
247    val hasExp           = pipewb.bits.exceptionVec.asUInt.orR
248
249    // if is VLoad, need latch 1 cycle to merge data. only flowNum and wbIndex need to latch
250    val latchWbValid     = if(isVStore) pipewb.valid else RegNext(pipewb.valid)
251    val latchWbIndex     = if(isVStore) wbIndex      else RegEnable(wbIndex, pipewb.valid)
252    val latchFlowNum     = if(isVStore) flowNumOffset else RegEnable(flowNumOffset, pipewb.valid)
253    val latchMergeByPre  = if(isVStore) mergedByPrevPortVec(i) else RegEnable(mergedByPrevPortVec(i), pipewb.valid)
254    when(latchWbValid && !latchMergeByPre){
255      entries(latchWbIndex).flowNum := entries(latchWbIndex).flowNum - latchFlowNum
256    }
257
258    when(pipewb.valid){
259      entries(wbIndex).sourceType   := sourceTypeNext
260      entries(wbIndex).flushState   := pipewb.bits.flushState
261    }
262    when(pipewb.valid && !pipewb.bits.hit){
263      needRSReplay(wbIndex) := true.B
264    }
265    pipewb.ready := true.B
266    XSError((entries(latchWbIndex).flowNum - latchFlowNum > entries(latchWbIndex).flowNum) && latchWbValid && !latchMergeByPre, "FlowWriteback overflow!!\n")
267    XSError(!allocated(latchWbIndex) && latchWbValid, "Writeback error flow!!\n")
268  }
269  // for inorder mem asscess
270  io.toSplit := DontCare
271
272  //uopwriteback(deq)
273  for (i <- 0 until uopSize){
274    when(allocated(i) && entries(i).allReady()){
275      uopFinish(i) := true.B
276    }
277  }
278   val selPolicy = SelectOne("circ", uopFinish, deqWidth) // select one entry to deq
279   for(((port, lsqport), i) <- (io.uopWriteback zip io.toLsq).zipWithIndex){
280    val canGo    = port.ready
281    val (selValid, selOHVec) = selPolicy.getNthOH(i + 1)
282    val entryIdx = OHToUInt(selOHVec)
283    val selEntry = entries(entryIdx)
284    val selAllocated = allocated(entryIdx)
285    val selFire  = selValid && canGo
286    when(selFire){
287      freeMaskVec(entryIdx) := selAllocated
288      allocated(entryIdx)   := false.B
289      uopFinish(entryIdx)   := false.B
290      needRSReplay(entryIdx):= false.B
291    }
292    //writeback connect
293    port.valid   := selFire && selAllocated && !needRSReplay(entryIdx) && !selEntry.uop.robIdx.needFlush(io.redirect)
294    port.bits    := DeqConnect(selEntry)
295    //to lsq
296    lsqport.bits := ToLsqConnect(selEntry) // when uopwriteback, free MBuffer entry, write to lsq
297    lsqport.valid:= selFire && selAllocated && !needRSReplay(entryIdx)
298    //to RS
299    io.feedback(i).valid                 := selFire && selAllocated
300    io.feedback(i).bits.hit              := !needRSReplay(entryIdx)
301    io.feedback(i).bits.robIdx           := selEntry.uop.robIdx
302    io.feedback(i).bits.sourceType       := selEntry.sourceType
303    io.feedback(i).bits.flushState       := selEntry.flushState
304    io.feedback(i).bits.dataInvalidSqIdx := DontCare
305    io.feedback(i).bits.uopIdx.get       := selEntry.uop.uopIdx
306   }
307
308  QueuePerf(uopSize, freeList.io.validCount, freeList.io.validCount === 0.U)
309}
310
311class VLMergeBufferImp(implicit p: Parameters) extends BaseVMergeBuffer(isVStore=false){
312  override lazy val uopSize = VlMergeBufferSize
313  println(s"VLMergeBuffer Size: ${VlMergeBufferSize}")
314  override lazy val freeList = Module(new FreeList(
315    size = uopSize,
316    allocWidth = VecLoadPipelineWidth,
317    freeWidth = deqWidth,
318    enablePreAlloc = false,
319    moduleName = "VLoad MergeBuffer freelist"
320  ))
321
322  //merge data
323  val flowWbElemIdx     = Wire(Vec(pipeWidth, UInt(elemIdxBits.W)))
324  val flowWbElemIdxInVd = Wire(Vec(pipeWidth, UInt(elemIdxBits.W)))
325  val pipewbValidReg    = Wire(Vec(pipeWidth, Bool()))
326  val wbIndexReg        = Wire(Vec(pipeWidth, UInt(vlmBindexBits.W)))
327  val mergeDataReg      = Wire(Vec(pipeWidth, UInt(VLEN.W)))
328
329  for((pipewb, i) <- io.fromPipeline.zipWithIndex){
330    /** step0 **/
331    val wbIndex = pipewb.bits.mBIndex
332    val alignedType = pipewb.bits.alignedType
333    val elemIdxInsideVd = pipewb.bits.elemIdxInsideVd
334    flowWbElemIdx(i) := pipewb.bits.elemIdx
335    flowWbElemIdxInVd(i) := elemIdxInsideVd.get
336
337    val oldData = PriorityMux(Seq(
338      (pipewbValidReg(0) && (wbIndexReg(0) === wbIndex)) -> mergeDataReg(0),
339      (pipewbValidReg(1) && (wbIndexReg(1) === wbIndex)) -> mergeDataReg(1),
340      (pipewbValidReg(2) && (wbIndexReg(2) === wbIndex)) -> mergeDataReg(2),
341      true.B                                             -> entries(wbIndex).data // default use entries_data
342    ))
343    val mergedData = mergeDataWithElemIdx(
344      oldData = oldData,
345      newData = io.fromPipeline.map(_.bits.vecdata.get),
346      alignedType = alignedType(1,0),
347      elemIdx = flowWbElemIdxInVd,
348      valids = mergePortMatrix(i)
349    )
350    /* this only for unit-stride load data merge
351     * cycle0: broden 128-bits to 256-bits (max 6 to 1)
352     * cycle1: select 128-bits data from 256-bits (16 to 1)
353     */
354    val (brodenMergeData, brodenMergeMask)     = mergeDataByIndex(
355      data    = io.fromPipeline.map(_.bits.vecdata.get).drop(i),
356      mask    = io.fromPipeline.map(_.bits.mask).drop(i),
357      index   = io.fromPipeline(i).bits.elemIdxInsideVd.get,
358      valids  = mergePortMatrix(i).drop(i)
359    )
360    /** step1 **/
361    pipewbValidReg(i)      := RegNext(pipewb.valid)
362    wbIndexReg(i)          := RegEnable(wbIndex, pipewb.valid)
363    mergeDataReg(i)        := RegEnable(mergedData, pipewb.valid) // for not Unit-stride
364    val brodenMergeDataReg  = RegEnable(brodenMergeData, pipewb.valid) // only for Unit-stride
365    val brodenMergeMaskReg  = RegEnable(brodenMergeMask, pipewb.valid)
366    val mergedByPrevPortReg = RegEnable(mergedByPrevPortVec(i), pipewb.valid)
367    val regOffsetReg        = RegEnable(pipewb.bits.reg_offset.get, pipewb.valid) // only for Unit-stride
368    val isusMerge           = RegEnable(alignedType(2), pipewb.valid)
369
370    val usSelData           = Mux1H(UIntToOH(regOffsetReg), (0 until VLENB).map{case i => getNoAlignedSlice(brodenMergeDataReg, i, 128)})
371    val usSelMask           = Mux1H(UIntToOH(regOffsetReg), (0 until VLENB).map{case i => brodenMergeMaskReg(16 + i - 1, i)})
372    val usMergeData         = mergeDataByByte(entries(wbIndexReg(i)).data, usSelData, usSelMask)
373    when(pipewbValidReg(i) && !mergedByPrevPortReg){
374      entries(wbIndexReg(i)).data := Mux(isusMerge, usMergeData, mergeDataReg(i)) // if aligned(2) == 1, is Unit-Stride inst
375    }
376  }
377}
378
379class VSMergeBufferImp(implicit p: Parameters) extends BaseVMergeBuffer(isVStore=true){
380  override lazy val uopSize = VsMergeBufferSize
381  println(s"VSMergeBuffer Size: ${VsMergeBufferSize}")
382  override lazy val freeList = Module(new FreeList(
383    size = uopSize,
384    allocWidth = VecStorePipelineWidth,
385    freeWidth = deqWidth,
386    enablePreAlloc = false,
387    moduleName = "VStore MergeBuffer freelist"
388  ))
389  override def DeqConnect(source: MBufferBundle): MemExuOutput = {
390    val sink               = Wire(new MemExuOutput(isVector = true))
391    sink.data             := DontCare
392    sink.mask.get         := DontCare
393    sink.uop              := source.uop
394    sink.uop.exceptionVec := source.exceptionVec
395    sink.debug            := 0.U.asTypeOf(new DebugBundle)
396    sink.vdIdxInField.get := DontCare
397    sink.vdIdx.get        := DontCare
398    sink.uop.vpu.vstart   := source.vstart
399    sink
400  }
401}
402