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