xref: /XiangShan/src/main/scala/xiangshan/backend/issue/IssueQueue.scala (revision f7f73727b40cad0c1c92804630163e337aaaecfd)
1package xiangshan.backend.issue
2
3import org.chipsalliance.cde.config.Parameters
4import chisel3._
5import chisel3.util._
6import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
7import utility.{GTimer, HasCircularQueuePtrHelper, SelectOne}
8import utils._
9import xiangshan._
10import xiangshan.backend.Bundles._
11import xiangshan.backend.decode.{ImmUnion, Imm_LUI_LOAD}
12import xiangshan.backend.datapath.DataConfig._
13import xiangshan.backend.datapath.DataSource
14import xiangshan.backend.fu.{FuConfig, FuType}
15import xiangshan.mem.{MemWaitUpdateReq, SqPtr, LqPtr}
16import xiangshan.backend.rob.RobPtr
17import xiangshan.backend.datapath.NewPipelineConnect
18
19class IssueQueue(params: IssueBlockParams)(implicit p: Parameters) extends LazyModule with HasXSParameter {
20  override def shouldBeInlined: Boolean = false
21
22  implicit val iqParams = params
23  lazy val module: IssueQueueImp = iqParams.schdType match {
24    case IntScheduler() => new IssueQueueIntImp(this)
25    case VfScheduler() => new IssueQueueVfImp(this)
26    case MemScheduler() =>
27      if (iqParams.StdCnt == 0 && !iqParams.isVecMemIQ) new IssueQueueMemAddrImp(this)
28      else if (iqParams.isVecMemIQ) new IssueQueueVecMemImp(this)
29      else new IssueQueueIntImp(this)
30    case _ => null
31  }
32}
33
34class IssueQueueStatusBundle(numEnq: Int, numEntries: Int) extends Bundle {
35  val empty = Output(Bool())
36  val full = Output(Bool())
37  val validCnt = Output(UInt(log2Ceil(numEntries).W))
38  val leftVec = Output(Vec(numEnq + 1, Bool()))
39}
40
41class IssueQueueDeqRespBundle(implicit p:Parameters, params: IssueBlockParams) extends EntryDeqRespBundle
42
43class IssueQueueIO()(implicit p: Parameters, params: IssueBlockParams) extends XSBundle {
44  // Inputs
45  val flush = Flipped(ValidIO(new Redirect))
46  val enq = Vec(params.numEnq, Flipped(DecoupledIO(new DynInst)))
47
48  val deqResp = Vec(params.numDeq, Flipped(ValidIO(new IssueQueueDeqRespBundle)))
49  val og0Resp = Vec(params.numDeq, Flipped(ValidIO(new IssueQueueDeqRespBundle)))
50  val og1Resp = Vec(params.numDeq, Flipped(ValidIO(new IssueQueueDeqRespBundle)))
51  val finalIssueResp = OptionWrapper(params.LdExuCnt > 0, Vec(params.LdExuCnt, Flipped(ValidIO(new IssueQueueDeqRespBundle))))
52  val memAddrIssueResp = OptionWrapper(params.LdExuCnt > 0, Vec(params.LdExuCnt, Flipped(ValidIO(new IssueQueueDeqRespBundle))))
53  val wbBusyTableRead = Input(params.genWbFuBusyTableReadBundle())
54  val wbBusyTableWrite = Output(params.genWbFuBusyTableWriteBundle())
55  val wakeupFromWB: MixedVec[ValidIO[IssueQueueWBWakeUpBundle]] = Flipped(params.genWBWakeUpSinkValidBundle)
56  val wakeupFromIQ: MixedVec[ValidIO[IssueQueueIQWakeUpBundle]] = Flipped(params.genIQWakeUpSinkValidBundle)
57  val og0Cancel = Input(ExuOH(backendParams.numExu))
58  val og1Cancel = Input(ExuOH(backendParams.numExu))
59  val ldCancel = Vec(backendParams.LduCnt + backendParams.HyuCnt, Flipped(new LoadCancelIO))
60
61  // Outputs
62  val deq: MixedVec[DecoupledIO[IssueQueueIssueBundle]] = params.genIssueDecoupledBundle
63  val wakeupToIQ: MixedVec[ValidIO[IssueQueueIQWakeUpBundle]] = params.genIQWakeUpSourceValidBundle
64  val status = Output(new IssueQueueStatusBundle(params.numEnq, params.numEntries))
65  // val statusNext = Output(new IssueQueueStatusBundle(params.numEnq))
66
67  val fromCancelNetwork = Flipped(params.genIssueDecoupledBundle)
68  val deqDelay: MixedVec[DecoupledIO[IssueQueueIssueBundle]] = params.genIssueDecoupledBundle// = deq.cloneType
69  def allWakeUp = wakeupFromWB ++ wakeupFromIQ
70}
71
72class IssueQueueImp(override val wrapper: IssueQueue)(implicit p: Parameters, val params: IssueBlockParams)
73  extends LazyModuleImp(wrapper)
74  with HasXSParameter {
75
76  println(s"[IssueQueueImp] ${params.getIQName} wakeupFromWB(${io.wakeupFromWB.size}), " +
77    s"wakeup exu in(${params.wakeUpInExuSources.size}): ${params.wakeUpInExuSources.map(_.name).mkString("{",",","}")}, " +
78    s"wakeup exu out(${params.wakeUpOutExuSources.size}): ${params.wakeUpOutExuSources.map(_.name).mkString("{",",","}")}, " +
79    s"numEntries: ${params.numEntries}, numRegSrc: ${params.numRegSrc}")
80
81  require(params.numExu <= 2, "IssueQueue has not supported more than 2 deq ports")
82  val deqFuCfgs     : Seq[Seq[FuConfig]] = params.exuBlockParams.map(_.fuConfigs)
83  val allDeqFuCfgs  : Seq[FuConfig] = params.exuBlockParams.flatMap(_.fuConfigs)
84  val fuCfgsCnt     : Map[FuConfig, Int] = allDeqFuCfgs.groupBy(x => x).map { case (cfg, cfgSeq) => (cfg, cfgSeq.length) }
85  val commonFuCfgs  : Seq[FuConfig] = fuCfgsCnt.filter(_._2 > 1).keys.toSeq
86  val fuLatencyMaps : Seq[Map[FuType.OHType, Int]] = params.exuBlockParams.map(x => x.fuLatencyMap)
87
88  println(s"[IssueQueueImp] ${params.getIQName} fuLatencyMaps: ${fuLatencyMaps}")
89  println(s"[IssueQueueImp] ${params.getIQName} commonFuCfgs: ${commonFuCfgs.map(_.name)}")
90  lazy val io = IO(new IssueQueueIO())
91  if(backendParams.debugEn) {
92    dontTouch(io.deq)
93    dontTouch(io.deqResp)
94  }
95  // Modules
96
97  val entries = Module(new Entries)
98  val subDeqPolicies  = deqFuCfgs.map(x => if (x.nonEmpty) Some(Module(new DeqPolicy())) else None)
99  val fuBusyTableWrite = params.exuBlockParams.map { case x => OptionWrapper(x.latencyValMax > 0, Module(new FuBusyTableWrite(x.fuLatencyMap))) }
100  val fuBusyTableRead = params.exuBlockParams.map { case x => OptionWrapper(x.latencyValMax > 0, Module(new FuBusyTableRead(x.fuLatencyMap))) }
101  val intWbBusyTableWrite = params.exuBlockParams.map { case x => OptionWrapper(x.intLatencyCertain, Module(new FuBusyTableWrite(x.intFuLatencyMap))) }
102  val intWbBusyTableRead = params.exuBlockParams.map { case x => OptionWrapper(x.intLatencyCertain, Module(new FuBusyTableRead(x.intFuLatencyMap))) }
103  val vfWbBusyTableWrite = params.exuBlockParams.map { case x => OptionWrapper(x.vfLatencyCertain, Module(new FuBusyTableWrite(x.vfFuLatencyMap))) }
104  val vfWbBusyTableRead = params.exuBlockParams.map { case x => OptionWrapper(x.vfLatencyCertain, Module(new FuBusyTableRead(x.vfFuLatencyMap))) }
105
106  class WakeupQueueFlush extends Bundle {
107    val redirect = ValidIO(new Redirect)
108    val ldCancel = Vec(backendParams.LduCnt + backendParams.HyuCnt, new LoadCancelIO)
109    val og0Fail = Output(Bool())
110    val og1Fail = Output(Bool())
111  }
112
113  private def flushFunc(exuInput: ExuInput, flush: WakeupQueueFlush, stage: Int): Bool = {
114    val redirectFlush = exuInput.robIdx.needFlush(flush.redirect)
115    val loadDependencyFlush = LoadShouldCancel(exuInput.loadDependency, flush.ldCancel)
116    val ogFailFlush = stage match {
117      case 1 => flush.og0Fail
118      case 2 => flush.og1Fail
119      case _ => false.B
120    }
121    redirectFlush || loadDependencyFlush || ogFailFlush
122  }
123
124  private def modificationFunc(exuInput: ExuInput): ExuInput = {
125    val newExuInput = WireDefault(exuInput)
126    newExuInput.loadDependency match {
127      case Some(deps) => deps.zip(exuInput.loadDependency.get).foreach(x => x._1 := x._2 << 1)
128      case None =>
129    }
130    newExuInput
131  }
132
133  val wakeUpQueues: Seq[Option[MultiWakeupQueue[ExuInput, WakeupQueueFlush]]] = params.exuBlockParams.map { x => OptionWrapper(x.isIQWakeUpSource, Module(
134    new MultiWakeupQueue(new ExuInput(x), new WakeupQueueFlush, x.fuLatancySet, flushFunc, modificationFunc)
135  ))}
136
137  val intWbBusyTableIn = io.wbBusyTableRead.map(_.intWbBusyTable)
138  val vfWbBusyTableIn = io.wbBusyTableRead.map(_.vfWbBusyTable)
139  val intWbBusyTableOut = io.wbBusyTableWrite.map(_.intWbBusyTable)
140  val vfWbBusyTableOut = io.wbBusyTableWrite.map(_.vfWbBusyTable)
141  val intDeqRespSetOut = io.wbBusyTableWrite.map(_.intDeqRespSet)
142  val vfDeqRespSetOut = io.wbBusyTableWrite.map(_.vfDeqRespSet)
143  val fuBusyTableMask = Wire(Vec(params.numDeq, UInt(params.numEntries.W)))
144  val intWbBusyTableMask = Wire(Vec(params.numDeq, UInt(params.numEntries.W)))
145  val vfWbBusyTableMask = Wire(Vec(params.numDeq, UInt(params.numEntries.W)))
146  val s0_enqValidVec = io.enq.map(_.valid)
147  val s0_enqSelValidVec = Wire(Vec(params.numEnq, Bool()))
148  val s0_enqNotFlush = !io.flush.valid
149  val s0_enqBits = WireInit(VecInit(io.enq.map(_.bits)))
150  val s0_doEnqSelValidVec = s0_enqSelValidVec.map(_ && s0_enqNotFlush) //enqValid && notFlush && enqReady
151
152
153  // One deq port only need one special deq policy
154  val subDeqSelValidVec: Seq[Option[Vec[Bool]]] = subDeqPolicies.map(_.map(_ => Wire(Vec(params.numDeq, Bool()))))
155  val subDeqSelOHVec: Seq[Option[Vec[UInt]]] = subDeqPolicies.map(_.map(_ => Wire(Vec(params.numDeq, UInt(params.numEntries.W)))))
156
157  val finalDeqSelValidVec = Wire(Vec(params.numDeq, Bool()))
158  val finalDeqSelOHVec    = Wire(Vec(params.numDeq, UInt(params.numEntries.W)))
159  val finalDeqOH: IndexedSeq[UInt] = (finalDeqSelValidVec zip finalDeqSelOHVec).map { case (valid, oh) =>
160    Mux(valid, oh, 0.U)
161  }
162  val finalDeqMask: UInt = finalDeqOH.reduce(_ | _)
163
164  val deqRespVec = io.deqResp
165
166  val validVec = VecInit(entries.io.valid.asBools)
167  val canIssueVec = VecInit(entries.io.canIssue.asBools)
168  val clearVec = VecInit(entries.io.clear.asBools)
169  val deqFirstIssueVec = VecInit(entries.io.deq.map(_.isFirstIssue))
170
171  val dataSources: Vec[Vec[DataSource]] = entries.io.dataSources
172  val finalDataSources: Vec[Vec[DataSource]] = VecInit(finalDeqOH.map(oh => Mux1H(oh, dataSources)))
173  // (entryIdx)(srcIdx)(exuIdx)
174  val wakeUpL1ExuOH: Option[Vec[Vec[UInt]]] = entries.io.srcWakeUpL1ExuOH
175  val srcTimer: Option[Vec[Vec[UInt]]] = entries.io.srcTimer
176
177  // (deqIdx)(srcIdx)(exuIdx)
178  val finalWakeUpL1ExuOH: Option[Vec[Vec[UInt]]] = wakeUpL1ExuOH.map(x => VecInit(finalDeqOH.map(oh => Mux1H(oh, x))))
179  val finalSrcTimer = srcTimer.map(x => VecInit(finalDeqOH.map(oh => Mux1H(oh, x))))
180
181  val wakeupEnqSrcStateBypassFromWB: Vec[Vec[UInt]] = Wire(Vec(io.enq.size, Vec(io.enq.head.bits.srcType.size, SrcState())))
182  val wakeupEnqSrcStateBypassFromIQ: Vec[Vec[UInt]] = Wire(Vec(io.enq.size, Vec(io.enq.head.bits.srcType.size, SrcState())))
183  val srcWakeUpEnqByIQMatrix = Wire(Vec(params.numEnq, Vec(params.numRegSrc, Vec(params.numWakeupFromIQ, Bool()))))
184
185  val shiftedWakeupLoadDependencyByIQVec = Wire(Vec(params.numWakeupFromIQ, Vec(LoadPipelineWidth, UInt(3.W))))
186  shiftedWakeupLoadDependencyByIQVec
187    .zip(io.wakeupFromIQ.map(_.bits.loadDependency))
188    .zip(params.wakeUpInExuSources.map(_.name)).foreach {
189    case ((deps, originalDeps), name) => deps.zip(originalDeps).zipWithIndex.foreach {
190      case ((dep, originalDep), deqPortIdx) =>
191        if (params.backendParam.getLdExuIdx(params.backendParam.allExuParams.find(_.name == name).get) == deqPortIdx)
192          dep := (originalDep << 1).asUInt | 1.U
193        else
194          dep := originalDep << 1
195    }
196  }
197
198  for (i <- io.enq.indices) {
199    for (j <- s0_enqBits(i).srcType.indices) {
200      wakeupEnqSrcStateBypassFromWB(i)(j) := Cat(
201        io.wakeupFromWB.map(x => x.bits.wakeUp(Seq((s0_enqBits(i).psrc(j), s0_enqBits(i).srcType(j))), x.valid).head).toSeq
202      ).orR
203    }
204  }
205
206  for (i <- io.enq.indices) {
207    val numLsrc = s0_enqBits(i).srcType.size.min(entries.io.enq(i).bits.status.srcType.size)
208    for (j <- s0_enqBits(i).srcType.indices) {
209      val ldTransCancel = if (params.numWakeupFromIQ > 0 && j < numLsrc) Mux(
210        srcWakeUpEnqByIQMatrix(i)(j).asUInt.orR,
211        Mux1H(srcWakeUpEnqByIQMatrix(i)(j), io.wakeupFromIQ.map(_.bits.loadDependency).map(dep => LoadShouldCancel(Some(dep), io.ldCancel)).toSeq),
212        false.B
213      ) else false.B
214      wakeupEnqSrcStateBypassFromIQ(i)(j) := Cat(
215        io.wakeupFromIQ.map(x => x.bits.wakeUp(Seq((s0_enqBits(i).psrc(j), s0_enqBits(i).srcType(j))), x.valid).head).toSeq
216      ).orR && !ldTransCancel
217    }
218  }
219
220  srcWakeUpEnqByIQMatrix.zipWithIndex.foreach { case (wakeups: Vec[Vec[Bool]], i) =>
221    if (io.wakeupFromIQ.isEmpty) {
222      wakeups := 0.U.asTypeOf(wakeups)
223    } else {
224      val wakeupVec: IndexedSeq[IndexedSeq[Bool]] = io.wakeupFromIQ.map((bundle: ValidIO[IssueQueueIQWakeUpBundle]) =>
225        bundle.bits.wakeUp(s0_enqBits(i).psrc.take(params.numRegSrc) zip s0_enqBits(i).srcType.take(params.numRegSrc), bundle.valid)
226      ).toIndexedSeq.transpose
227      wakeups := wakeupVec.map(x => VecInit(x))
228    }
229  }
230
231  val fuTypeVec = Wire(Vec(params.numEntries, FuType()))
232  val transEntryDeqVec = Wire(Vec(params.numEnq, ValidIO(new EntryBundle)))
233  val deqEntryVec = Wire(Vec(params.numDeq, ValidIO(new EntryBundle)))
234  val transSelVec = Wire(Vec(params.numEnq, UInt((params.numEntries-params.numEnq).W)))
235
236  val enqEntryOldestSel = Wire(Vec(params.numDeq, ValidIO(UInt(params.numEnq.W))))
237  val othersEntryOldestSel = Wire(Vec(params.numDeq, ValidIO(UInt((params.numEntries - params.numEnq).W))))
238  val subDeqPolicyRequest = Wire(Vec(params.numDeq, UInt(params.numEntries.W)))
239  val canIssueMergeAllBusy = Wire(Vec(params.numDeq, UInt(params.numEntries.W)))
240
241  val deqSelValidVec = Wire(Vec(params.numDeq, Bool()))
242  val deqSelOHVec    = Wire(Vec(params.numDeq, UInt(params.numEntries.W)))
243
244  /**
245    * Connection of [[entries]]
246    */
247  entries.io match { case entriesIO: EntriesIO =>
248    entriesIO.flush <> io.flush
249    entriesIO.wakeUpFromWB := io.wakeupFromWB
250    entriesIO.wakeUpFromIQ := io.wakeupFromIQ
251    entriesIO.og0Cancel := io.og0Cancel
252    entriesIO.og1Cancel := io.og1Cancel
253    entriesIO.ldCancel := io.ldCancel
254    entriesIO.enq.zipWithIndex.foreach { case (enq: ValidIO[EntryBundle], i) =>
255      enq.valid := s0_doEnqSelValidVec(i)
256      val numLsrc = s0_enqBits(i).srcType.size.min(enq.bits.status.srcType.size)
257      for(j <- 0 until numLsrc) {
258        enq.bits.status.srcState(j) := s0_enqBits(i).srcState(j) |
259                                       wakeupEnqSrcStateBypassFromWB(i)(j) |
260                                       wakeupEnqSrcStateBypassFromIQ(i)(j)
261        enq.bits.status.psrc(j) := s0_enqBits(i).psrc(j)
262        enq.bits.status.srcType(j) := s0_enqBits(i).srcType(j)
263        enq.bits.status.dataSources(j).value := Mux(wakeupEnqSrcStateBypassFromIQ(i)(j).asBool, DataSource.forward, s0_enqBits(i).dataSource(j).value)
264        enq.bits.payload.debugInfo.enqRsTime := GTimer()
265      }
266      enq.bits.status.fuType := s0_enqBits(i).fuType
267      enq.bits.status.robIdx := s0_enqBits(i).robIdx
268      enq.bits.status.uopIdx.foreach(_ := s0_enqBits(i).uopIdx)
269      enq.bits.status.issueTimer := "b11".U
270      enq.bits.status.deqPortIdx := 0.U
271      enq.bits.status.issued := false.B
272      enq.bits.status.firstIssue := false.B
273      enq.bits.status.blocked := false.B
274      enq.bits.status.srcWakeUpL1ExuOH match {
275        case Some(value) => value.zip(srcWakeUpEnqByIQMatrix(i)).zipWithIndex.foreach {
276          case ((exuOH, wakeUpByIQOH), srcIdx) =>
277            when(wakeUpByIQOH.asUInt.orR) {
278              exuOH := Mux1H(wakeUpByIQOH, io.wakeupFromIQ.toSeq.map(x => MathUtils.IntToOH(x.bits.exuIdx).U(backendParams.numExu.W)))
279            }.otherwise {
280              exuOH := s0_enqBits(i).l1ExuOH(srcIdx)
281            }
282        }
283        case None =>
284      }
285      enq.bits.status.srcTimer match {
286        case Some(value) => value.zip(srcWakeUpEnqByIQMatrix(i)).zipWithIndex.foreach {
287          case ((timer, wakeUpByIQOH), srcIdx) =>
288            when(wakeUpByIQOH.asUInt.orR) {
289              timer := 1.U.asTypeOf(timer)
290            }.otherwise {
291              timer := Mux(s0_enqBits(i).dataSource(srcIdx).value === DataSource.bypass, 2.U.asTypeOf(timer), 0.U.asTypeOf(timer))
292            }
293        }
294        case None =>
295      }
296      enq.bits.status.srcLoadDependency.foreach(_.zip(srcWakeUpEnqByIQMatrix(i)).zipWithIndex.foreach {
297        case ((dep, wakeUpByIQOH), srcIdx) =>
298          dep := Mux(wakeUpByIQOH.asUInt.orR, Mux1H(wakeUpByIQOH, shiftedWakeupLoadDependencyByIQVec), 0.U.asTypeOf(dep))
299      })
300      enq.bits.imm := s0_enqBits(i).imm
301      enq.bits.payload := s0_enqBits(i)
302    }
303    entriesIO.deq.zipWithIndex.foreach { case (deq, i) =>
304      deq.enqEntryOldestSel := enqEntryOldestSel(i)
305      deq.othersEntryOldestSel := othersEntryOldestSel(i)
306      deq.subDeqPolicyRequest := { if (subDeqPolicies(i).nonEmpty) subDeqPolicyRequest(i) else 0.U }
307      deq.subDeqSelOH := subDeqSelOHVec(i).getOrElse(Seq(0.U, 0.U))
308      deq.deqSelOH.valid := deqSelValidVec(i)
309      deq.deqSelOH.bits := deqSelOHVec(i)
310      deq.finalDeqSelOH.valid := finalDeqSelValidVec(i)
311      deq.finalDeqSelOH.bits := finalDeqSelOHVec(i)
312    }
313    entriesIO.deqResp.zipWithIndex.foreach { case (deqResp, i) =>
314      deqResp.valid := io.deqResp(i).valid
315      deqResp.bits.robIdx := io.deqResp(i).bits.robIdx
316      deqResp.bits.uopIdx := io.deqResp(i).bits.uopIdx
317      deqResp.bits.dataInvalidSqIdx := io.deqResp(i).bits.dataInvalidSqIdx
318      deqResp.bits.respType := io.deqResp(i).bits.respType
319      deqResp.bits.rfWen := io.deqResp(i).bits.rfWen
320      deqResp.bits.fuType := io.deqResp(i).bits.fuType
321    }
322    entriesIO.og0Resp.zipWithIndex.foreach { case (og0Resp, i) =>
323      og0Resp.valid := io.og0Resp(i).valid
324      og0Resp.bits.robIdx := io.og0Resp(i).bits.robIdx
325      og0Resp.bits.uopIdx := io.og0Resp(i).bits.uopIdx
326      og0Resp.bits.dataInvalidSqIdx := io.og0Resp(i).bits.dataInvalidSqIdx
327      og0Resp.bits.respType := io.og0Resp(i).bits.respType
328      og0Resp.bits.rfWen := io.og0Resp(i).bits.rfWen
329      og0Resp.bits.fuType := io.og0Resp(i).bits.fuType
330    }
331    entriesIO.og1Resp.zipWithIndex.foreach { case (og1Resp, i) =>
332      og1Resp.valid := io.og1Resp(i).valid
333      og1Resp.bits.robIdx := io.og1Resp(i).bits.robIdx
334      og1Resp.bits.uopIdx := io.og1Resp(i).bits.uopIdx
335      og1Resp.bits.dataInvalidSqIdx := io.og1Resp(i).bits.dataInvalidSqIdx
336      og1Resp.bits.respType := io.og1Resp(i).bits.respType
337      og1Resp.bits.rfWen := io.og1Resp(i).bits.rfWen
338      og1Resp.bits.fuType := io.og1Resp(i).bits.fuType
339    }
340    entriesIO.finalIssueResp.foreach(_.zipWithIndex.foreach { case (finalIssueResp, i) =>
341      finalIssueResp := io.finalIssueResp.get(i)
342    })
343    entriesIO.memAddrIssueResp.foreach(_.zipWithIndex.foreach { case (memAddrIssueResp, i) =>
344      memAddrIssueResp := io.memAddrIssueResp.get(i)
345    })
346    transEntryDeqVec := entriesIO.transEntryDeqVec
347    deqEntryVec := entriesIO.deq.map(_.deqEntry)
348    fuTypeVec := entriesIO.fuType
349    transSelVec := entriesIO.transSelVec
350  }
351
352
353  s0_enqSelValidVec := s0_enqValidVec.zip(io.enq).map{ case (enqValid, enq) => enqValid && enq.ready}
354
355  protected val commonAccept: UInt = Cat(fuTypeVec.map(fuType =>
356    FuType.FuTypeOrR(fuType, commonFuCfgs.map(_.fuType))
357  ).reverse)
358
359  // if deq port can accept the uop
360  protected val canAcceptVec: Seq[UInt] = deqFuCfgs.map { fuCfgs: Seq[FuConfig] =>
361    Cat(fuTypeVec.map(fuType =>
362      FuType.FuTypeOrR(fuType, fuCfgs.map(_.fuType))
363    ).reverse)
364  }
365
366  protected val deqCanAcceptVec: Seq[IndexedSeq[Bool]] = deqFuCfgs.map { fuCfgs: Seq[FuConfig] =>
367    fuTypeVec.map(fuType =>
368      FuType.FuTypeOrR(fuType, fuCfgs.map(_.fuType))) // C+E0    C+E1
369  }
370
371  canIssueMergeAllBusy.zipWithIndex.foreach { case (merge, i) =>
372    val mergeFuBusy = {
373      if (fuBusyTableWrite(i).nonEmpty) canIssueVec.asUInt & (~fuBusyTableMask(i))
374      else canIssueVec.asUInt
375    }
376    val mergeIntWbBusy = {
377      if (intWbBusyTableRead(i).nonEmpty) mergeFuBusy & (~intWbBusyTableMask(i))
378      else mergeFuBusy
379    }
380    val mergeVfWbBusy = {
381      if (vfWbBusyTableRead(i).nonEmpty) mergeIntWbBusy & (~vfWbBusyTableMask(i))
382      else mergeIntWbBusy
383    }
384    merge := mergeVfWbBusy
385  }
386
387  subDeqPolicies.zipWithIndex.foreach { case (dpOption: Option[DeqPolicy], i) =>
388    if (dpOption.nonEmpty) {
389      val dp = dpOption.get
390      dp.io.request             := subDeqPolicyRequest(i)
391      subDeqSelValidVec(i).get  := dp.io.deqSelOHVec.map(oh => oh.valid)
392      subDeqSelOHVec(i).get     := dp.io.deqSelOHVec.map(oh => oh.bits)
393    }
394  }
395
396  if (params.numDeq == 2) {
397    require(params.deqFuSame || params.deqFuDiff, "The 2 deq ports need to be identical or completely different")
398  }
399
400  if (params.numDeq == 2 && params.deqFuSame) {
401    enqEntryOldestSel(0) := NewAgeDetector(numEntries = params.numEnq,
402      enq = VecInit(s0_doEnqSelValidVec),
403      clear = VecInit(clearVec.take(params.numEnq)),
404      canIssue = canIssueVec.asUInt(params.numEnq-1, 0)
405    )
406    enqEntryOldestSel(1) := DontCare
407
408    othersEntryOldestSel(0) := AgeDetector(numEntries = params.numEntries - params.numEnq,
409      enq = VecInit(transEntryDeqVec.zip(transSelVec).map{ case (transEntry, transSel) => Fill(params.numEntries-params.numEnq, transEntry.valid) & transSel }),
410      deq = VecInit(clearVec.drop(params.numEnq)).asUInt,
411      canIssue = canIssueVec.asUInt(params.numEntries-1, params.numEnq)
412    )
413    othersEntryOldestSel(1) := DontCare
414
415    subDeqPolicyRequest.zipWithIndex.foreach { case (req, i) =>
416      req := canIssueVec.asUInt & ~Cat(othersEntryOldestSel(0).bits, enqEntryOldestSel(0).bits)
417    }
418
419    val deqRequestVec = VecInit(othersEntryOldestSel(0).valid, subDeqSelValidVec(0).getOrElse(Seq(false.B)).head, subDeqSelValidVec(0).getOrElse(Seq(false.B)).last, enqEntryOldestSel(0).valid)
420    val deqOHVec      = VecInit(Cat(othersEntryOldestSel(0).bits, 0.U((params.numEnq).W)), subDeqSelOHVec(0).getOrElse(Seq(0.U)).head, subDeqSelOHVec(0).getOrElse(Seq(0.U)).last, Cat(0.U((params.numEntries-params.numEnq).W), enqEntryOldestSel(0).bits))
421
422    deqSelValidVec.zipWithIndex.foreach { case(deqValid, i) => deqValid := SelectOne("circ", deqRequestVec, params.numDeq).getNthOH(i + 1)._1 }
423    deqSelOHVec(0) := PriorityMux(deqRequestVec, deqOHVec.map(_ & canIssueMergeAllBusy(0)))
424    deqSelOHVec(1) := PriorityMux(deqRequestVec.reverse, deqOHVec.map(_ & canIssueMergeAllBusy(1)).reverse)
425
426    finalDeqSelValidVec.zip(finalDeqSelOHVec).zip(deqSelValidVec).zip(deqSelOHVec).zipWithIndex.foreach { case ((((selValid, selOH), deqValid), deqOH), i) =>
427      selValid := deqValid && deqOH.orR
428      selOH := deqOH
429    }
430  }
431  else {
432    subDeqPolicyRequest.zipWithIndex.foreach { case (req, i) =>
433      req := canIssueMergeAllBusy(i) & VecInit(deqCanAcceptVec(i)).asUInt
434    }
435
436    val enqCanAcceptVec: Seq[IndexedSeq[Bool]] = deqFuCfgs.map { fuCfgs: Seq[FuConfig] =>
437      io.enq.map(_.bits.fuType).map(fuType =>
438        FuType.FuTypeOrR(fuType, fuCfgs.map(_.fuType))) // C+E0    C+E1
439    }
440
441    val transCanAcceptVec: Seq[IndexedSeq[Bool]] = deqFuCfgs.map { fuCfgs: Seq[FuConfig] =>
442      transEntryDeqVec.map(_.bits.status.fuType).zip(transEntryDeqVec.map(_.valid)).map{ case (fuType, valid) =>
443        FuType.FuTypeOrR(fuType, fuCfgs.map(_.fuType)) && valid }
444    }
445
446    enqEntryOldestSel.zipWithIndex.foreach { case (sel, deqIdx) =>
447      sel := NewAgeDetector(numEntries = params.numEnq,
448          enq = VecInit(enqCanAcceptVec(deqIdx).zip(s0_doEnqSelValidVec).map{ case (doCanAccept, valid) => doCanAccept && valid }),
449          clear = VecInit(clearVec.take(params.numEnq)),
450          canIssue = canIssueMergeAllBusy(deqIdx)(params.numEnq-1, 0)
451        )
452    }
453
454    othersEntryOldestSel.zipWithIndex.foreach { case (sel, deqIdx) =>
455      sel := AgeDetector(numEntries = params.numEntries - params.numEnq,
456          enq = VecInit(transCanAcceptVec(deqIdx).zip(transSelVec).map{ case (doCanAccept, transSel) => Fill(params.numEntries-params.numEnq, doCanAccept) & transSel }),
457          deq = VecInit(clearVec.drop(params.numEnq)).asUInt,
458          canIssue = canIssueMergeAllBusy(deqIdx)(params.numEntries-1, params.numEnq)
459        )
460    }
461
462    deqSelValidVec := finalDeqSelValidVec
463    deqSelOHVec := finalDeqSelOHVec
464
465    finalDeqSelValidVec.zip(finalDeqSelOHVec).zipWithIndex.foreach { case ((selValid, selOH), i) =>
466      if (params.exuBlockParams(i).fuConfigs.contains(FuConfig.FakeHystaCfg)) {
467        selValid := false.B
468        selOH := 0.U.asTypeOf(selOH)
469      } else {
470        selValid := othersEntryOldestSel(i).valid || enqEntryOldestSel(i).valid || subDeqSelValidVec(i).getOrElse(Seq(false.B)).head
471        selOH := Mux(othersEntryOldestSel(i).valid, Cat(othersEntryOldestSel(i).bits, 0.U((params.numEnq).W)),
472               Mux(enqEntryOldestSel(i).valid, Cat(0.U((params.numEntries-params.numEnq).W), enqEntryOldestSel(i).bits),
473                   subDeqSelOHVec(i).getOrElse(Seq(0.U)).head))
474      }
475    }
476  }
477
478  //fuBusyTable
479  fuBusyTableWrite.zip(fuBusyTableRead).zipWithIndex.foreach { case ((busyTableWrite: Option[FuBusyTableWrite], busyTableRead: Option[FuBusyTableRead]), i) =>
480    if(busyTableWrite.nonEmpty) {
481      val btwr = busyTableWrite.get
482      val btrd = busyTableRead.get
483      btwr.io.in.deqResp := io.deqResp(i)
484      btwr.io.in.og0Resp := io.og0Resp(i)
485      btwr.io.in.og1Resp := io.og1Resp(i)
486      btrd.io.in.fuBusyTable := btwr.io.out.fuBusyTable
487      btrd.io.in.fuTypeRegVec := fuTypeVec
488      fuBusyTableMask(i) := btrd.io.out.fuBusyTableMask
489    }
490    else {
491      fuBusyTableMask(i) := 0.U(params.numEntries.W)
492    }
493  }
494
495  //wbfuBusyTable write
496  intWbBusyTableWrite.zip(intWbBusyTableOut).zip(intDeqRespSetOut).zipWithIndex.foreach { case (((busyTableWrite: Option[FuBusyTableWrite], busyTable: Option[UInt]), deqResp), i) =>
497    if(busyTableWrite.nonEmpty) {
498      val btwr = busyTableWrite.get
499      val bt = busyTable.get
500      val dq = deqResp.get
501      btwr.io.in.deqResp := io.deqResp(i)
502      btwr.io.in.og0Resp := io.og0Resp(i)
503      btwr.io.in.og1Resp := io.og1Resp(i)
504      bt := btwr.io.out.fuBusyTable
505      dq := btwr.io.out.deqRespSet
506    }
507  }
508
509  vfWbBusyTableWrite.zip(vfWbBusyTableOut).zip(vfDeqRespSetOut).zipWithIndex.foreach { case (((busyTableWrite: Option[FuBusyTableWrite], busyTable: Option[UInt]), deqResp), i) =>
510    if (busyTableWrite.nonEmpty) {
511      val btwr = busyTableWrite.get
512      val bt = busyTable.get
513      val dq = deqResp.get
514      btwr.io.in.deqResp := io.deqResp(i)
515      btwr.io.in.og0Resp := io.og0Resp(i)
516      btwr.io.in.og1Resp := io.og1Resp(i)
517      bt := btwr.io.out.fuBusyTable
518      dq := btwr.io.out.deqRespSet
519    }
520  }
521
522  //wbfuBusyTable read
523  intWbBusyTableRead.zip(intWbBusyTableIn).zipWithIndex.foreach { case ((busyTableRead: Option[FuBusyTableRead], busyTable: Option[UInt]), i) =>
524    if(busyTableRead.nonEmpty) {
525      val btrd = busyTableRead.get
526      val bt = busyTable.get
527      btrd.io.in.fuBusyTable := bt
528      btrd.io.in.fuTypeRegVec := fuTypeVec
529      intWbBusyTableMask(i) := btrd.io.out.fuBusyTableMask
530    }
531    else {
532      intWbBusyTableMask(i) := 0.U(params.numEntries.W)
533    }
534  }
535  vfWbBusyTableRead.zip(vfWbBusyTableIn).zipWithIndex.foreach { case ((busyTableRead: Option[FuBusyTableRead], busyTable: Option[UInt]), i) =>
536    if (busyTableRead.nonEmpty) {
537      val btrd = busyTableRead.get
538      val bt = busyTable.get
539      btrd.io.in.fuBusyTable := bt
540      btrd.io.in.fuTypeRegVec := fuTypeVec
541      vfWbBusyTableMask(i) := btrd.io.out.fuBusyTableMask
542    }
543    else {
544      vfWbBusyTableMask(i) := 0.U(params.numEntries.W)
545    }
546  }
547
548  wakeUpQueues.zipWithIndex.foreach { case (wakeUpQueueOption, i) =>
549    val og0RespEach = io.og0Resp(i)
550    val og1RespEach = io.og1Resp(i)
551    wakeUpQueueOption.foreach {
552      wakeUpQueue =>
553        val flush = Wire(new WakeupQueueFlush)
554        flush.redirect := io.flush
555        flush.ldCancel := io.ldCancel
556        flush.og0Fail := io.og0Resp(i).valid && RSFeedbackType.isBlocked(io.og0Resp(i).bits.respType)
557        flush.og1Fail := io.og1Resp(i).valid && RSFeedbackType.isBlocked(io.og1Resp(i).bits.respType)
558        wakeUpQueue.io.flush := flush
559        wakeUpQueue.io.enq.valid := io.deq(i).fire && !io.deq(i).bits.common.needCancel(io.og0Cancel, io.og1Cancel) && {
560          io.deq(i).bits.common.rfWen.getOrElse(false.B) && io.deq(i).bits.common.pdest =/= 0.U ||
561          io.deq(i).bits.common.fpWen.getOrElse(false.B) ||
562          io.deq(i).bits.common.vecWen.getOrElse(false.B)
563        }
564        wakeUpQueue.io.enq.bits.uop := io.deq(i).bits.common
565        wakeUpQueue.io.enq.bits.lat := getDeqLat(i, io.deq(i).bits.common.fuType)
566        wakeUpQueue.io.og0IssueFail := flush.og0Fail
567        wakeUpQueue.io.og1IssueFail := flush.og1Fail
568    }
569  }
570
571  io.deq.zipWithIndex.foreach { case (deq, i) =>
572    deq.valid                := finalDeqSelValidVec(i)
573    deq.bits.addrOH          := finalDeqSelOHVec(i)
574    deq.bits.common.isFirstIssue := deqFirstIssueVec(i)
575    deq.bits.common.iqIdx    := OHToUInt(finalDeqSelOHVec(i))
576    deq.bits.common.fuType   := deqEntryVec(i).bits.payload.fuType
577    deq.bits.common.fuOpType := deqEntryVec(i).bits.payload.fuOpType
578    deq.bits.common.rfWen.foreach(_ := deqEntryVec(i).bits.payload.rfWen)
579    deq.bits.common.fpWen.foreach(_ := deqEntryVec(i).bits.payload.fpWen)
580    deq.bits.common.vecWen.foreach(_ := deqEntryVec(i).bits.payload.vecWen)
581    deq.bits.common.flushPipe.foreach(_ := deqEntryVec(i).bits.payload.flushPipe)
582    deq.bits.common.pdest := deqEntryVec(i).bits.payload.pdest
583    deq.bits.common.robIdx := deqEntryVec(i).bits.payload.robIdx
584    deq.bits.common.dataSources.zip(finalDataSources(i)).zipWithIndex.foreach {
585      case ((sink, source), srcIdx) =>
586        sink.value := Mux(
587          SrcType.isXp(deqEntryVec(i).bits.payload.srcType(srcIdx)) && deqEntryVec(i).bits.payload.psrc(srcIdx) === 0.U,
588          DataSource.none,
589          source.value
590        )
591    }
592    if (deq.bits.common.l1ExuOH.size > 0) {
593      if (params.hasIQWakeUp) {
594        deq.bits.common.l1ExuOH := finalWakeUpL1ExuOH.get(i)
595      } else {
596        deq.bits.common.l1ExuOH := deqEntryVec(i).bits.payload.l1ExuOH.take(deq.bits.common.l1ExuOH.length)
597      }
598    }
599    deq.bits.common.srcTimer.foreach(_ := finalSrcTimer.get(i))
600    deq.bits.common.loadDependency.foreach(_ := deqEntryVec(i).bits.status.mergedLoadDependency.get)
601    deq.bits.common.deqLdExuIdx.foreach(_ := params.backendParam.getLdExuIdx(deq.bits.exuParams).U)
602    deq.bits.common.src := DontCare
603    deq.bits.common.preDecode.foreach(_ := deqEntryVec(i).bits.payload.preDecodeInfo)
604
605    deq.bits.rf.zip(deqEntryVec(i).bits.payload.psrc).foreach { case (rf, psrc) =>
606      rf.foreach(_.addr := psrc) // psrc in payload array can be pregIdx of IntRegFile or VfRegFile
607    }
608    deq.bits.rf.zip(deqEntryVec(i).bits.payload.srcType).foreach { case (rf, srcType) =>
609      rf.foreach(_.srcType := srcType) // psrc in payload array can be pregIdx of IntRegFile or VfRegFile
610    }
611    deq.bits.srcType.zip(deqEntryVec(i).bits.payload.srcType).foreach { case (sink, source) =>
612      sink := source
613    }
614    deq.bits.immType := deqEntryVec(i).bits.payload.selImm
615
616    if (params.inIntSchd && params.AluCnt > 0) {
617      // dirty code for lui+addi(w) fusion
618      val isLuiAddiFusion = deqEntryVec(i).bits.payload.isLUI32
619      val luiImm = Cat(deqEntryVec(i).bits.payload.lsrc(1), deqEntryVec(i).bits.payload.lsrc(0), deqEntryVec(i).bits.imm(ImmUnion.maxLen - 1, 0))
620      deq.bits.common.imm := Mux(isLuiAddiFusion, ImmUnion.LUI32.toImm32(luiImm), deqEntryVec(i).bits.imm)
621    }
622    else if (params.inMemSchd && params.LduCnt > 0) {
623      // dirty code for fused_lui_load
624      val isLuiLoadFusion = SrcType.isNotReg(deqEntryVec(i).bits.payload.srcType(0)) && FuType.isLoad(deqEntryVec(i).bits.payload.fuType)
625      deq.bits.common.imm := Mux(isLuiLoadFusion, Imm_LUI_LOAD().getLuiImm(deqEntryVec(i).bits.payload), deqEntryVec(i).bits.imm)
626    }
627    else {
628      deq.bits.common.imm := deqEntryVec(i).bits.imm
629    }
630
631    deq.bits.common.perfDebugInfo := deqEntryVec(i).bits.payload.debugInfo
632    deq.bits.common.perfDebugInfo.selectTime := GTimer()
633    deq.bits.common.perfDebugInfo.issueTime := GTimer() + 1.U
634  }
635
636  private val ldCancels = io.fromCancelNetwork.map(in =>
637    LoadShouldCancel(in.bits.common.loadDependency, io.ldCancel)
638  )
639  private val fromCancelNetworkShift = WireDefault(io.fromCancelNetwork)
640  fromCancelNetworkShift.zip(io.fromCancelNetwork).foreach {
641    case (shifted, original) =>
642      original.ready := shifted.ready // this will not cause combinational loop
643      shifted.bits.common.loadDependency.foreach(
644        _ := original.bits.common.loadDependency.get.map(_ << 1)
645      )
646  }
647  io.deqDelay.zip(fromCancelNetworkShift).zip(ldCancels).foreach { case ((deqDly, deq), ldCancel) =>
648    NewPipelineConnect(
649      deq, deqDly, deqDly.valid,
650      deq.bits.common.robIdx.needFlush(io.flush) || ldCancel,
651      Option("Scheduler2DataPathPipe")
652    )
653  }
654  if(backendParams.debugEn) {
655    dontTouch(io.deqDelay)
656  }
657  io.wakeupToIQ.zipWithIndex.foreach { case (wakeup, i) =>
658    if (wakeUpQueues(i).nonEmpty && finalWakeUpL1ExuOH.nonEmpty) {
659      wakeup.valid := wakeUpQueues(i).get.io.deq.valid
660      wakeup.bits.fromExuInput(wakeUpQueues(i).get.io.deq.bits, finalWakeUpL1ExuOH.get(i))
661      wakeup.bits.loadDependency := wakeUpQueues(i).get.io.deq.bits.loadDependency.getOrElse(0.U.asTypeOf(wakeup.bits.loadDependency))
662    } else if (wakeUpQueues(i).nonEmpty) {
663      wakeup.valid := wakeUpQueues(i).get.io.deq.valid
664      wakeup.bits.fromExuInput(wakeUpQueues(i).get.io.deq.bits)
665      wakeup.bits.loadDependency := wakeUpQueues(i).get.io.deq.bits.loadDependency.getOrElse(0.U.asTypeOf(wakeup.bits.loadDependency))
666    } else {
667      wakeup.valid := false.B
668      wakeup.bits := 0.U.asTypeOf(wakeup.bits)
669    }
670  }
671
672  // Todo: better counter implementation
673  private val enqHasValid = validVec.take(params.numEnq).reduce(_ | _)
674  private val enqEntryValidCnt = PopCount(validVec.take(params.numEnq))
675  private val othersValidCnt = PopCount(validVec.drop(params.numEnq))
676  io.status.leftVec(0) := validVec.drop(params.numEnq).reduce(_ & _)
677  for (i <- 0 until params.numEnq) {
678    io.status.leftVec(i + 1) := othersValidCnt === (params.numEntries - params.numEnq - (i + 1)).U
679  }
680  io.enq.foreach(_.ready := !Cat(io.status.leftVec).orR || !enqHasValid) // Todo: more efficient implementation
681  io.status.empty := !Cat(validVec).orR
682  io.status.full := Cat(io.status.leftVec).orR
683  io.status.validCnt := PopCount(validVec)
684
685  protected def getDeqLat(deqPortIdx: Int, fuType: UInt) : UInt = {
686    Mux1H(fuLatencyMaps(deqPortIdx) map { case (k, v) => (fuType(k.id), v.U) })
687  }
688
689  // issue perf counter
690  // enq count
691  XSPerfAccumulate("enq_valid_cnt", PopCount(io.enq.map(_.fire)))
692  XSPerfAccumulate("enq_fire_cnt", PopCount(io.enq.map(_.fire)))
693  // valid count
694  XSPerfHistogram("enq_entry_valid_cnt", enqEntryValidCnt, true.B, 0, params.numEnq + 1)
695  XSPerfHistogram("other_entry_valid_cnt", othersValidCnt, true.B, 0, params.numEntries - params.numEnq + 1)
696  XSPerfHistogram("valid_cnt", PopCount(validVec), true.B, 0, params.numEntries + 1)
697  // only split when more than 1 func type
698  if (params.getFuCfgs.size > 0) {
699    for (t <- FuType.functionNameMap.keys) {
700      val fuName = FuType.functionNameMap(t)
701      if (params.getFuCfgs.map(_.fuType == t).reduce(_ | _)) {
702        XSPerfHistogram(s"valid_cnt_hist_futype_${fuName}", PopCount(validVec.zip(fuTypeVec).map { case (v, fu) => v && fu === t.U }), true.B, 0, params.numEntries, 1)
703      }
704    }
705  }
706  // ready instr count
707  private val readyEntriesCnt = PopCount(validVec.zip(canIssueVec).map(x => x._1 && x._2))
708  XSPerfHistogram("ready_cnt", readyEntriesCnt, true.B, 0, params.numEntries + 1)
709  // only split when more than 1 func type
710  if (params.getFuCfgs.size > 0) {
711    for (t <- FuType.functionNameMap.keys) {
712      val fuName = FuType.functionNameMap(t)
713      if (params.getFuCfgs.map(_.fuType == t).reduce(_ | _)) {
714        XSPerfHistogram(s"ready_cnt_hist_futype_${fuName}", PopCount(validVec.zip(canIssueVec).zip(fuTypeVec).map { case ((v, c), fu) => v && c && fu === t.U }), true.B, 0, params.numEntries, 1)
715      }
716    }
717  }
718
719  // deq instr count
720  XSPerfAccumulate("issue_instr_pre_count", PopCount(io.deq.map(_.valid)))
721  XSPerfHistogram("issue_instr_pre_count_hist", PopCount(io.deq.map(_.valid)), true.B, 0, params.numDeq + 1, 1)
722  XSPerfAccumulate("issue_instr_count", PopCount(io.deqDelay.map(_.valid)))
723  XSPerfHistogram("issue_instr_count_hist", PopCount(io.deqDelay.map(_.valid)), true.B, 0, params.numDeq + 1, 1)
724
725  // deq instr data source count
726  XSPerfAccumulate("issue_datasource_reg", io.deq.map{ deq =>
727    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.reg && !SrcType.isNotReg(deq.bits.srcType(j)) })
728  }.reduce(_ +& _))
729  XSPerfAccumulate("issue_datasource_bypass", io.deq.map{ deq =>
730    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.bypass && !SrcType.isNotReg(deq.bits.srcType(j)) })
731  }.reduce(_ +& _))
732  XSPerfAccumulate("issue_datasource_forward", io.deq.map{ deq =>
733    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.forward && !SrcType.isNotReg(deq.bits.srcType(j)) })
734  }.reduce(_ +& _))
735  XSPerfAccumulate("issue_datasource_noreg", io.deq.map{ deq =>
736    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && SrcType.isNotReg(deq.bits.srcType(j)) })
737  }.reduce(_ +& _))
738
739  XSPerfHistogram("issue_datasource_reg_hist", io.deq.map{ deq =>
740    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.reg && !SrcType.isNotReg(deq.bits.srcType(j)) })
741  }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
742  XSPerfHistogram("issue_datasource_bypass_hist", io.deq.map{ deq =>
743    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.bypass && !SrcType.isNotReg(deq.bits.srcType(j)) })
744  }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
745  XSPerfHistogram("issue_datasource_forward_hist", io.deq.map{ deq =>
746    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.forward && !SrcType.isNotReg(deq.bits.srcType(j)) })
747  }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
748  XSPerfHistogram("issue_datasource_noreg_hist", io.deq.map{ deq =>
749    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && SrcType.isNotReg(deq.bits.srcType(j)) })
750  }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
751
752  // deq instr data source count for each futype
753  for (t <- FuType.functionNameMap.keys) {
754    val fuName = FuType.functionNameMap(t)
755    if (params.getFuCfgs.map(_.fuType == t).reduce(_ | _)) {
756      XSPerfAccumulate(s"issue_datasource_reg_futype_${fuName}", io.deq.map{ deq =>
757        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.reg && !SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
758      }.reduce(_ +& _))
759      XSPerfAccumulate(s"issue_datasource_bypass_futype_${fuName}", io.deq.map{ deq =>
760        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.bypass && !SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
761      }.reduce(_ +& _))
762      XSPerfAccumulate(s"issue_datasource_forward_futype_${fuName}", io.deq.map{ deq =>
763        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.forward && !SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
764      }.reduce(_ +& _))
765      XSPerfAccumulate(s"issue_datasource_noreg_futype_${fuName}", io.deq.map{ deq =>
766        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
767      }.reduce(_ +& _))
768
769      XSPerfHistogram(s"issue_datasource_reg_hist_futype_${fuName}", io.deq.map{ deq =>
770        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.reg && !SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
771      }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
772      XSPerfHistogram(s"issue_datasource_bypass_hist_futype_${fuName}", io.deq.map{ deq =>
773        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.bypass && !SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
774      }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
775      XSPerfHistogram(s"issue_datasource_forward_hist_futype_${fuName}", io.deq.map{ deq =>
776        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.forward && !SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
777      }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
778      XSPerfHistogram(s"issue_datasource_noreg_hist_futype_${fuName}", io.deq.map{ deq =>
779        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
780      }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
781    }
782  }
783
784  // cancel instr count
785  if (params.hasIQWakeUp) {
786    val cancelVec: Vec[Bool] = entries.io.cancel.get
787    XSPerfAccumulate("cancel_instr_count", PopCount(validVec.zip(cancelVec).map(x => x._1 & x._2)))
788    XSPerfHistogram("cancel_instr_hist", PopCount(validVec.zip(cancelVec).map(x => x._1 & x._2)), true.B, 0, params.numEntries, 1)
789    for (t <- FuType.functionNameMap.keys) {
790      val fuName = FuType.functionNameMap(t)
791      if (params.getFuCfgs.map(_.fuType == t).reduce(_ | _)) {
792        XSPerfAccumulate(s"cancel_instr_count_futype_${fuName}", PopCount(validVec.zip(cancelVec).zip(fuTypeVec).map{ case ((x, y), fu) => x & y & fu === t.U }))
793        XSPerfHistogram(s"cancel_instr_hist_futype_${fuName}", PopCount(validVec.zip(cancelVec).zip(fuTypeVec).map{ case ((x, y), fu) => x & y & fu === t.U }), true.B, 0, params.numEntries, 1)
794      }
795    }
796  }
797}
798
799class IssueQueueJumpBundle extends Bundle {
800  val pc = UInt(VAddrData().dataWidth.W)
801}
802
803class IssueQueueLoadBundle(implicit p: Parameters) extends XSBundle {
804  val fastMatch = UInt(backendParams.LduCnt.W)
805  val fastImm = UInt(12.W)
806}
807
808class IssueQueueIntIO()(implicit p: Parameters, params: IssueBlockParams) extends IssueQueueIO
809
810class IssueQueueIntImp(override val wrapper: IssueQueue)(implicit p: Parameters, iqParams: IssueBlockParams)
811  extends IssueQueueImp(wrapper)
812{
813  io.suggestName("none")
814  override lazy val io = IO(new IssueQueueIntIO).suggestName("io")
815
816  if(params.needPc) {
817    entries.io.enq.zipWithIndex.foreach { case (entriesEnq, i) =>
818      entriesEnq.bits.status.pc.foreach(_ := io.enq(i).bits.pc)
819    }
820  }
821
822  io.deq.zipWithIndex.foreach{ case (deq, i) => {
823    deq.bits.common.pc.foreach(_ := deqEntryVec(i).bits.status.pc.get)
824    deq.bits.common.preDecode.foreach(_ := deqEntryVec(i).bits.payload.preDecodeInfo)
825    deq.bits.common.ftqIdx.foreach(_ := deqEntryVec(i).bits.payload.ftqPtr)
826    deq.bits.common.ftqOffset.foreach(_ := deqEntryVec(i).bits.payload.ftqOffset)
827    deq.bits.common.predictInfo.foreach(x => {
828      x.target := DontCare
829      x.taken := deqEntryVec(i).bits.payload.pred_taken
830    })
831    // for std
832    deq.bits.common.sqIdx.foreach(_ := deqEntryVec(i).bits.payload.sqIdx)
833    // for i2f
834    deq.bits.common.fpu.foreach(_ := deqEntryVec(i).bits.payload.fpu)
835  }}
836}
837
838class IssueQueueVfImp(override val wrapper: IssueQueue)(implicit p: Parameters, iqParams: IssueBlockParams)
839  extends IssueQueueImp(wrapper)
840{
841  s0_enqBits.foreach{ x =>
842    x.srcType(3) := SrcType.vp // v0: mask src
843    x.srcType(4) := SrcType.vp // vl&vtype
844  }
845  io.deq.zipWithIndex.foreach{ case (deq, i) => {
846    deq.bits.common.fpu.foreach(_ := deqEntryVec(i).bits.payload.fpu)
847    deq.bits.common.vpu.foreach(_ := deqEntryVec(i).bits.payload.vpu)
848    deq.bits.common.vpu.foreach(_.vuopIdx := deqEntryVec(i).bits.payload.uopIdx)
849    deq.bits.common.vpu.foreach(_.lastUop := deqEntryVec(i).bits.payload.lastUop)
850  }}
851}
852
853class IssueQueueMemBundle(implicit p: Parameters, params: IssueBlockParams) extends Bundle {
854  val feedbackIO = Flipped(Vec(params.numDeq, new MemRSFeedbackIO))
855  val checkWait = new Bundle {
856    val stIssuePtr = Input(new SqPtr)
857    val memWaitUpdateReq = Flipped(new MemWaitUpdateReq)
858  }
859  val loadFastMatch = Output(Vec(params.LduCnt, new IssueQueueLoadBundle))
860
861  // vector
862  val sqDeqPtr = OptionWrapper(params.isVecMemIQ, Input(new SqPtr))
863  val lqDeqPtr = OptionWrapper(params.isVecMemIQ, Input(new LqPtr))
864}
865
866class IssueQueueMemIO(implicit p: Parameters, params: IssueBlockParams) extends IssueQueueIO {
867  val memIO = Some(new IssueQueueMemBundle)
868}
869
870class IssueQueueMemAddrImp(override val wrapper: IssueQueue)(implicit p: Parameters, params: IssueBlockParams)
871  extends IssueQueueImp(wrapper) with HasCircularQueuePtrHelper {
872
873  require(params.StdCnt == 0 && (params.LduCnt + params.StaCnt + params.HyuCnt + params.VlduCnt) > 0, "IssueQueueMemAddrImp can only be instance of MemAddr IQ, " +
874    s"StdCnt: ${params.StdCnt}, LduCnt: ${params.LduCnt}, StaCnt: ${params.StaCnt}, HyuCnt: ${params.HyuCnt}")
875  println(s"[IssueQueueMemAddrImp] StdCnt: ${params.StdCnt}, LduCnt: ${params.LduCnt}, StaCnt: ${params.StaCnt}, HyuCnt: ${params.HyuCnt}")
876
877  io.suggestName("none")
878  override lazy val io = IO(new IssueQueueMemIO).suggestName("io")
879  private val memIO = io.memIO.get
880
881  memIO.loadFastMatch := 0.U.asTypeOf(memIO.loadFastMatch) // TODO: is still needed?
882
883  for (i <- io.enq.indices) {
884    val blockNotReleased = isAfter(io.enq(i).bits.sqIdx, memIO.checkWait.stIssuePtr)
885    val storeAddrWaitForIsIssuing = VecInit((0 until StorePipelineWidth).map(i => {
886      memIO.checkWait.memWaitUpdateReq.robIdx(i).valid &&
887        memIO.checkWait.memWaitUpdateReq.robIdx(i).bits.value === io.enq(i).bits.waitForRobIdx.value
888    })).asUInt.orR && !io.enq(i).bits.loadWaitStrict // is waiting for store addr ready
889    s0_enqBits(i).loadWaitBit := io.enq(i).bits.loadWaitBit && !storeAddrWaitForIsIssuing && blockNotReleased
890    // when have vpu
891    if (params.VlduCnt > 0 || params.VstuCnt > 0) {
892      s0_enqBits(i).srcType(3) := SrcType.vp // v0: mask src
893      s0_enqBits(i).srcType(4) := SrcType.vp // vl&vtype
894    }
895  }
896
897  for (i <- entries.io.enq.indices) {
898    entries.io.enq(i).bits.status match { case enqData =>
899      enqData.blocked := false.B // s0_enqBits(i).loadWaitBit
900      enqData.mem.get.strictWait := s0_enqBits(i).loadWaitStrict
901      enqData.mem.get.waitForStd := false.B
902      enqData.mem.get.waitForRobIdx := s0_enqBits(i).waitForRobIdx
903      enqData.mem.get.waitForSqIdx := 0.U.asTypeOf(enqData.mem.get.waitForSqIdx) // generated by sq, will be updated later
904      enqData.mem.get.sqIdx := s0_enqBits(i).sqIdx
905    }
906
907    entries.io.fromMem.get.slowResp.zipWithIndex.foreach { case (slowResp, i) =>
908      slowResp.valid                 := memIO.feedbackIO(i).feedbackSlow.valid
909      slowResp.bits.robIdx           := memIO.feedbackIO(i).feedbackSlow.bits.robIdx
910      slowResp.bits.uopIdx           := DontCare
911      slowResp.bits.respType         := Mux(memIO.feedbackIO(i).feedbackSlow.bits.hit, RSFeedbackType.fuIdle, RSFeedbackType.feedbackInvalid)
912      slowResp.bits.dataInvalidSqIdx := memIO.feedbackIO(i).feedbackSlow.bits.dataInvalidSqIdx
913      slowResp.bits.rfWen := DontCare
914      slowResp.bits.fuType := DontCare
915    }
916
917    entries.io.fromMem.get.fastResp.zipWithIndex.foreach { case (fastResp, i) =>
918      fastResp.valid                 := memIO.feedbackIO(i).feedbackFast.valid
919      fastResp.bits.robIdx           := memIO.feedbackIO(i).feedbackFast.bits.robIdx
920      fastResp.bits.uopIdx           := DontCare
921      fastResp.bits.respType         := Mux(memIO.feedbackIO(i).feedbackFast.bits.hit, RSFeedbackType.fuIdle, memIO.feedbackIO(i).feedbackFast.bits.sourceType)
922      fastResp.bits.dataInvalidSqIdx := 0.U.asTypeOf(fastResp.bits.dataInvalidSqIdx)
923      fastResp.bits.rfWen := DontCare
924      fastResp.bits.fuType := DontCare
925    }
926
927    entries.io.fromMem.get.memWaitUpdateReq := memIO.checkWait.memWaitUpdateReq
928    entries.io.fromMem.get.stIssuePtr := memIO.checkWait.stIssuePtr
929  }
930
931  io.deq.zipWithIndex.foreach { case (deq, i) =>
932    deq.bits.common.loadWaitBit.foreach(_ := deqEntryVec(i).bits.payload.loadWaitBit)
933    deq.bits.common.waitForRobIdx.foreach(_ := deqEntryVec(i).bits.payload.waitForRobIdx)
934    deq.bits.common.storeSetHit.foreach(_ := deqEntryVec(i).bits.payload.storeSetHit)
935    deq.bits.common.loadWaitStrict.foreach(_ := deqEntryVec(i).bits.payload.loadWaitStrict)
936    deq.bits.common.ssid.foreach(_ := deqEntryVec(i).bits.payload.ssid)
937    deq.bits.common.sqIdx.get := deqEntryVec(i).bits.payload.sqIdx
938    deq.bits.common.lqIdx.get := deqEntryVec(i).bits.payload.lqIdx
939    deq.bits.common.ftqIdx.foreach(_ := deqEntryVec(i).bits.payload.ftqPtr)
940    deq.bits.common.ftqOffset.foreach(_ := deqEntryVec(i).bits.payload.ftqOffset)
941    // when have vpu
942    if (params.VlduCnt > 0 || params.VstuCnt > 0) {
943      deq.bits.common.vpu.foreach(_ := deqEntryVec(i).bits.payload.vpu)
944      deq.bits.common.vpu.foreach(_.vuopIdx := deqEntryVec(i).bits.payload.uopIdx)
945    }
946  }
947}
948
949class IssueQueueVecMemImp(override val wrapper: IssueQueue)(implicit p: Parameters, params: IssueBlockParams)
950  extends IssueQueueImp(wrapper) with HasCircularQueuePtrHelper {
951
952  require((params.VstdCnt + params.VlduCnt + params.VstaCnt) > 0, "IssueQueueVecMemImp can only be instance of VecMem IQ")
953
954  io.suggestName("none")
955  override lazy val io = IO(new IssueQueueMemIO).suggestName("io")
956  private val memIO = io.memIO.get
957
958  def selectOldUop(robIdx: Seq[RobPtr], uopIdx: Seq[UInt], valid: Seq[Bool]): Vec[Bool] = {
959    val compareVec = (0 until robIdx.length).map(i => (0 until i).map(j => isAfter(robIdx(j), robIdx(i)) || (robIdx(j).value === robIdx(i).value && uopIdx(i) < uopIdx(j))))
960    val resultOnehot = VecInit((0 until robIdx.length).map(i => Cat((0 until robIdx.length).map(j =>
961      (if (j < i) !valid(j) || compareVec(i)(j)
962      else if (j == i) valid(i)
963      else !valid(j) || !compareVec(j)(i))
964    )).andR))
965    resultOnehot
966  }
967
968  val robIdxVec = entries.io.robIdx.get
969  val uopIdxVec = entries.io.uopIdx.get
970  val allEntryOldestOH = selectOldUop(robIdxVec, uopIdxVec, validVec)
971
972  finalDeqSelValidVec.head := (allEntryOldestOH.asUInt & canIssueVec.asUInt).orR
973  finalDeqSelOHVec.head := allEntryOldestOH.asUInt & canIssueVec.asUInt
974
975  if (params.isVecMemAddrIQ) {
976    s0_enqBits.foreach{ x =>
977      x.srcType(3) := SrcType.vp // v0: mask src
978      x.srcType(4) := SrcType.vp // vl&vtype
979    }
980
981    for (i <- io.enq.indices) {
982      s0_enqBits(i).loadWaitBit := false.B
983    }
984
985    for (i <- entries.io.enq.indices) {
986      entries.io.enq(i).bits.status match { case enqData =>
987        enqData.blocked := false.B // s0_enqBits(i).loadWaitBit
988        enqData.mem.get.strictWait := s0_enqBits(i).loadWaitStrict
989        enqData.mem.get.waitForStd := false.B
990        enqData.mem.get.waitForRobIdx := s0_enqBits(i).waitForRobIdx
991        enqData.mem.get.waitForSqIdx := 0.U.asTypeOf(enqData.mem.get.waitForSqIdx) // generated by sq, will be updated later
992        enqData.mem.get.sqIdx := s0_enqBits(i).sqIdx
993      }
994
995      entries.io.fromMem.get.slowResp.zipWithIndex.foreach { case (slowResp, i) =>
996        slowResp.valid                 := memIO.feedbackIO(i).feedbackSlow.valid
997        slowResp.bits.robIdx           := memIO.feedbackIO(i).feedbackSlow.bits.robIdx
998        slowResp.bits.uopIdx           := DontCare
999        slowResp.bits.respType         := Mux(memIO.feedbackIO(i).feedbackSlow.bits.hit, RSFeedbackType.fuIdle, RSFeedbackType.feedbackInvalid)
1000        slowResp.bits.dataInvalidSqIdx := memIO.feedbackIO(i).feedbackSlow.bits.dataInvalidSqIdx
1001        slowResp.bits.rfWen := DontCare
1002        slowResp.bits.fuType := DontCare
1003      }
1004
1005      entries.io.fromMem.get.fastResp.zipWithIndex.foreach { case (fastResp, i) =>
1006        fastResp.valid                 := memIO.feedbackIO(i).feedbackFast.valid
1007        fastResp.bits.robIdx           := memIO.feedbackIO(i).feedbackFast.bits.robIdx
1008        fastResp.bits.uopIdx           := DontCare
1009        fastResp.bits.respType         := memIO.feedbackIO(i).feedbackFast.bits.sourceType
1010        fastResp.bits.dataInvalidSqIdx := 0.U.asTypeOf(fastResp.bits.dataInvalidSqIdx)
1011        fastResp.bits.rfWen := DontCare
1012        fastResp.bits.fuType := DontCare
1013      }
1014
1015      entries.io.fromMem.get.memWaitUpdateReq := memIO.checkWait.memWaitUpdateReq
1016      entries.io.fromMem.get.stIssuePtr := memIO.checkWait.stIssuePtr
1017    }
1018  }
1019
1020  for (i <- entries.io.enq.indices) {
1021    entries.io.enq(i).bits.status match { case enqData =>
1022      enqData.vecMem.get.sqIdx := s0_enqBits(i).sqIdx
1023      enqData.vecMem.get.lqIdx := s0_enqBits(i).lqIdx
1024    }
1025  }
1026
1027  entries.io.fromLsq.get.sqDeqPtr := memIO.sqDeqPtr.get
1028  entries.io.fromLsq.get.lqDeqPtr := memIO.lqDeqPtr.get
1029
1030  io.deq.zipWithIndex.foreach { case (deq, i) =>
1031    deq.bits.common.sqIdx.foreach(_ := deqEntryVec(i).bits.payload.sqIdx)
1032    deq.bits.common.lqIdx.foreach(_ := deqEntryVec(i).bits.payload.lqIdx)
1033    if (params.isVecLdAddrIQ) {
1034      deq.bits.common.ftqIdx.get := deqEntryVec(i).bits.payload.ftqPtr
1035      deq.bits.common.ftqOffset.get := deqEntryVec(i).bits.payload.ftqOffset
1036    }
1037    deq.bits.common.fpu.foreach(_ := deqEntryVec(i).bits.payload.fpu)
1038    deq.bits.common.vpu.foreach(_ := deqEntryVec(i).bits.payload.vpu)
1039    deq.bits.common.vpu.foreach(_.vuopIdx := deqEntryVec(i).bits.payload.uopIdx)
1040    deq.bits.common.vpu.foreach(_.lastUop := deqEntryVec(i).bits.payload.lastUop)
1041  }
1042}
1043