xref: /XiangShan/src/main/scala/xiangshan/backend/issue/Scheduler.scala (revision e88008978ecb7073b9e8126282e0a0b913d9e6f7)
1package xiangshan.backend.issue
2
3import org.chipsalliance.cde.config.Parameters
4import chisel3._
5import chisel3.util._
6import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
7import xiangshan._
8import xiangshan.backend.Bundles._
9import xiangshan.backend.datapath.DataConfig.{IntData, VAddrData, VecData}
10import xiangshan.backend.datapath.WbConfig.{IntWB, VfWB}
11import xiangshan.backend.regfile.RfWritePortWithConfig
12import xiangshan.backend.rename.BusyTable
13import xiangshan.mem.{LsqEnqCtrl, LsqEnqIO, MemWaitUpdateReq, SqPtr}
14
15sealed trait SchedulerType
16
17case class IntScheduler() extends SchedulerType
18case class MemScheduler() extends SchedulerType
19case class VfScheduler() extends SchedulerType
20case class NoScheduler() extends SchedulerType
21
22class Scheduler(val params: SchdBlockParams)(implicit p: Parameters) extends LazyModule with HasXSParameter {
23  override def shouldBeInlined: Boolean = false
24
25  val numIntStateWrite = backendParams.numPregWb(IntData())
26  val numVfStateWrite = backendParams.numPregWb(VecData())
27
28  val dispatch2Iq = LazyModule(new Dispatch2Iq(params))
29  val issueQueue = params.issueBlockParams.map(x => LazyModule(new IssueQueue(x).suggestName(x.getIQName)))
30
31  lazy val module: SchedulerImpBase = params.schdType match {
32    case IntScheduler() => new SchedulerArithImp(this)(params, p)
33    case MemScheduler() => new SchedulerMemImp(this)(params, p)
34    case VfScheduler() => new SchedulerArithImp(this)(params, p)
35    case _ => null
36  }
37}
38
39class SchedulerIO()(implicit params: SchdBlockParams, p: Parameters) extends XSBundle {
40  // params alias
41  private val LoadQueueSize = VirtualLoadQueueSize
42
43  val fromTop = new Bundle {
44    val hartId = Input(UInt(8.W))
45  }
46  val fromWbFuBusyTable = new Bundle{
47    val fuBusyTableRead = MixedVec(params.issueBlockParams.map(x => Input(x.genWbFuBusyTableReadBundle)))
48  }
49  val wbFuBusyTable = MixedVec(params.issueBlockParams.map(x => Output(x.genWbFuBusyTableWriteBundle)))
50
51  val fromCtrlBlock = new Bundle {
52    val pcVec = Input(Vec(params.numPcReadPort, UInt(VAddrData().dataWidth.W)))
53    val flush = Flipped(ValidIO(new Redirect))
54  }
55  val fromDispatch = new Bundle {
56    val allocPregs = Vec(RenameWidth, Input(new ResetPregStateReq))
57    val uops =  Vec(params.numUopIn, Flipped(DecoupledIO(new DynInst)))
58  }
59  val intWriteBack = MixedVec(Vec(backendParams.numPregWb(IntData()),
60    new RfWritePortWithConfig(backendParams.intPregParams.dataCfg, backendParams.intPregParams.addrWidth)))
61  val vfWriteBack = MixedVec(Vec(backendParams.numPregWb(VecData()),
62    new RfWritePortWithConfig(backendParams.vfPregParams.dataCfg, backendParams.vfPregParams.addrWidth)))
63  val toDataPath: MixedVec[MixedVec[DecoupledIO[IssueQueueIssueBundle]]] = MixedVec(params.issueBlockParams.map(_.genIssueDecoupledBundle))
64  val toDataPathAfterDelay: MixedVec[MixedVec[DecoupledIO[IssueQueueIssueBundle]]] = MixedVec(params.issueBlockParams.map(_.genIssueDecoupledBundle))
65  val fromCancelNetwork = Flipped(MixedVec(params.issueBlockParams.map(_.genIssueDecoupledBundle)))
66
67  val fromSchedulers = new Bundle {
68    val wakeupVec: MixedVec[ValidIO[IssueQueueIQWakeUpBundle]] = Flipped(params.genIQWakeUpInValidBundle)
69  }
70
71  val toSchedulers = new Bundle {
72    val wakeupVec: MixedVec[ValidIO[IssueQueueIQWakeUpBundle]] = params.genIQWakeUpOutValidBundle
73  }
74
75  val fromDataPath = new Bundle {
76    val resp: MixedVec[MixedVec[OGRespBundle]] = MixedVec(params.issueBlockParams.map(x => Flipped(x.genOGRespBundle)))
77    val og0Cancel = Input(ExuOH(backendParams.numExu))
78    // Todo: remove this after no cancel signal from og1
79    val og1Cancel = Input(ExuOH(backendParams.numExu))
80    val cancelToBusyTable = Vec(backendParams.numExu, Flipped(ValidIO(new CancelSignal)))
81    // just be compatible to old code
82    def apply(i: Int)(j: Int) = resp(i)(j)
83  }
84
85  val loadFinalIssueResp = MixedVec(params.issueBlockParams.map(x => MixedVec(Vec(x.LduCnt, Flipped(ValidIO(new IssueQueueDeqRespBundle()(p, x)))))))
86  val memAddrIssueResp = MixedVec(params.issueBlockParams.map(x => MixedVec(Vec(x.LduCnt, Flipped(ValidIO(new IssueQueueDeqRespBundle()(p, x)))))))
87
88  val ldCancel = Vec(backendParams.LduCnt, Flipped(new LoadCancelIO))
89
90  val memIO = if (params.isMemSchd) Some(new Bundle {
91    val lsqEnqIO = Flipped(new LsqEnqIO)
92  }) else None
93  val fromMem = if (params.isMemSchd) Some(new Bundle {
94    val ldaFeedback = Flipped(Vec(params.LduCnt, new MemRSFeedbackIO))
95    val staFeedback = Flipped(Vec(params.StaCnt, new MemRSFeedbackIO))
96    val stIssuePtr = Input(new SqPtr())
97    val lcommit = Input(UInt(log2Up(CommitWidth + 1).W))
98    val scommit = Input(UInt(log2Ceil(EnsbufferWidth + 1).W)) // connected to `memBlock.io.sqDeq` instead of ROB
99    // from lsq
100    val lqCancelCnt = Input(UInt(log2Up(LoadQueueSize + 1).W))
101    val sqCancelCnt = Input(UInt(log2Up(StoreQueueSize + 1).W))
102    val memWaitUpdateReq = Flipped(new MemWaitUpdateReq)
103  }) else None
104  val toMem = if (params.isMemSchd) Some(new Bundle {
105    val loadFastMatch = Output(Vec(params.LduCnt, new IssueQueueLoadBundle))
106  }) else None
107}
108
109abstract class SchedulerImpBase(wrapper: Scheduler)(implicit params: SchdBlockParams, p: Parameters)
110  extends LazyModuleImp(wrapper)
111    with HasXSParameter
112{
113  val io = IO(new SchedulerIO())
114
115  // alias
116  private val iqWakeUpInMap: Map[Int, ValidIO[IssueQueueIQWakeUpBundle]] =
117    io.fromSchedulers.wakeupVec.map(x => (x.bits.exuIdx, x)).toMap
118  private val schdType = params.schdType
119
120  // Modules
121  val dispatch2Iq: Dispatch2IqImp = wrapper.dispatch2Iq.module
122  val issueQueues: Seq[IssueQueueImp] = wrapper.issueQueue.map(_.module)
123
124  // BusyTable Modules
125  val intBusyTable = schdType match {
126    case IntScheduler() | MemScheduler() => Some(Module(new BusyTable(dispatch2Iq.numIntStateRead, wrapper.numIntStateWrite, IntPhyRegs, IntWB())))
127    case _ => None
128  }
129
130  val vfBusyTable = schdType match {
131    case VfScheduler() | MemScheduler() => Some(Module(new BusyTable(dispatch2Iq.numVfStateRead, wrapper.numVfStateWrite, VfPhyRegs, VfWB())))
132    case _ => None
133  }
134
135  dispatch2Iq.io match { case dp2iq =>
136    dp2iq.redirect <> io.fromCtrlBlock.flush
137    dp2iq.in <> io.fromDispatch.uops
138    dp2iq.readIntState.foreach(_ <> intBusyTable.get.io.read)
139    dp2iq.readVfState.foreach(_ <> vfBusyTable.get.io.read)
140  }
141
142  intBusyTable match {
143    case Some(bt) =>
144      bt.io.allocPregs.zip(io.fromDispatch.allocPregs).foreach { case (btAllocPregs, dpAllocPregs) =>
145        btAllocPregs.valid := dpAllocPregs.isInt
146        btAllocPregs.bits := dpAllocPregs.preg
147      }
148      bt.io.wbPregs.zipWithIndex.foreach { case (wb, i) =>
149        wb.valid := io.intWriteBack(i).wen && io.intWriteBack(i).intWen
150        wb.bits := io.intWriteBack(i).addr
151      }
152      bt.io.wakeUp := io.fromSchedulers.wakeupVec
153      bt.io.cancel := io.fromDataPath.cancelToBusyTable
154    case None =>
155  }
156
157  vfBusyTable match {
158    case Some(bt) =>
159      bt.io.allocPregs.zip(io.fromDispatch.allocPregs).foreach { case (btAllocPregs, dpAllocPregs) =>
160        btAllocPregs.valid := dpAllocPregs.isFp
161        btAllocPregs.bits := dpAllocPregs.preg
162      }
163      bt.io.wbPregs.zipWithIndex.foreach { case (wb, i) =>
164        wb.valid := io.vfWriteBack(i).wen && (io.vfWriteBack(i).fpWen || io.vfWriteBack(i).vecWen)
165        wb.bits := io.vfWriteBack(i).addr
166      }
167      bt.io.wakeUp := io.fromSchedulers.wakeupVec
168      bt.io.cancel := io.fromDataPath.cancelToBusyTable
169    case None =>
170  }
171
172  val wakeupFromWBVec = Wire(params.genWBWakeUpSinkValidBundle)
173  val writeback = params.schdType match {
174    case IntScheduler() => io.intWriteBack
175    case MemScheduler() => io.intWriteBack ++ io.vfWriteBack
176    case VfScheduler() => io.vfWriteBack
177    case _ => Seq()
178  }
179  wakeupFromWBVec.zip(writeback).foreach { case (sink, source) =>
180    sink.valid := source.wen
181    sink.bits.rfWen := source.intWen
182    sink.bits.fpWen := source.fpWen
183    sink.bits.vecWen := source.vecWen
184    sink.bits.pdest := source.addr
185  }
186
187  // Connect bundles having the same wakeup source
188  issueQueues.zipWithIndex.foreach { case(iq, i) =>
189    iq.io.wakeupFromIQ.foreach { wakeUp =>
190      wakeUp := iqWakeUpInMap(wakeUp.bits.exuIdx)
191    }
192    iq.io.og0Cancel := io.fromDataPath.og0Cancel
193    iq.io.og1Cancel := io.fromDataPath.og1Cancel
194    iq.io.ldCancel := io.ldCancel
195    iq.io.fromCancelNetwork <> io.fromCancelNetwork(i)
196  }
197
198  private val iqWakeUpOutMap: Map[Int, ValidIO[IssueQueueIQWakeUpBundle]] =
199    issueQueues.flatMap(_.io.wakeupToIQ)
200      .map(x => (x.bits.exuIdx, x))
201      .toMap
202
203  // Connect bundles having the same wakeup source
204  io.toSchedulers.wakeupVec.foreach { wakeUp =>
205    wakeUp := iqWakeUpOutMap(wakeUp.bits.exuIdx)
206  }
207
208  io.toDataPath.zipWithIndex.foreach { case (toDp, i) =>
209    toDp <> issueQueues(i).io.deq
210  }
211  io.toDataPathAfterDelay.zipWithIndex.foreach { case (toDpDy, i) =>
212    toDpDy <> issueQueues(i).io.deqDelay
213  }
214
215  // Response
216  issueQueues.zipWithIndex.foreach { case (iq, i) =>
217    iq.io.deqResp.zipWithIndex.foreach { case (deqResp, j) =>
218      deqResp.valid := iq.io.deq(j).valid && io.toDataPath(i)(j).ready
219      deqResp.bits.respType := RSFeedbackType.issueSuccess
220      deqResp.bits.robIdx := iq.io.deq(j).bits.common.robIdx
221      deqResp.bits.dataInvalidSqIdx := DontCare
222      deqResp.bits.rfWen := iq.io.deq(j).bits.common.rfWen.getOrElse(false.B)
223      deqResp.bits.fuType := iq.io.deq(j).bits.common.fuType
224    }
225    iq.io.og0Resp.zipWithIndex.foreach { case (og0Resp, j) =>
226      og0Resp := io.fromDataPath(i)(j).og0resp
227    }
228    iq.io.og1Resp.zipWithIndex.foreach { case (og1Resp, j) =>
229      og1Resp := io.fromDataPath(i)(j).og1resp
230    }
231    iq.io.finalIssueResp.foreach(_.zipWithIndex.foreach { case (finalIssueResp, j) =>
232      finalIssueResp := io.loadFinalIssueResp(i)(j)
233    })
234    iq.io.memAddrIssueResp.foreach(_.zipWithIndex.foreach { case (memAddrIssueResp, j) =>
235      memAddrIssueResp := io.memAddrIssueResp(i)(j)
236    })
237    iq.io.wbBusyTableRead := io.fromWbFuBusyTable.fuBusyTableRead(i)
238    io.wbFuBusyTable(i) := iq.io.wbBusyTableWrite
239  }
240
241  println(s"[Scheduler] io.fromSchedulers.wakeupVec: ${io.fromSchedulers.wakeupVec.map(x => backendParams.getExuName(x.bits.exuIdx))}")
242  println(s"[Scheduler] iqWakeUpInKeys: ${iqWakeUpInMap.keys}")
243
244  println(s"[Scheduler] iqWakeUpOutKeys: ${iqWakeUpOutMap.keys}")
245  println(s"[Scheduler] io.toSchedulers.wakeupVec: ${io.toSchedulers.wakeupVec.map(x => backendParams.getExuName(x.bits.exuIdx))}")
246}
247
248class SchedulerArithImp(override val wrapper: Scheduler)(implicit params: SchdBlockParams, p: Parameters)
249  extends SchedulerImpBase(wrapper)
250    with HasXSParameter
251{
252//  dontTouch(io.vfWbFuBusyTable)
253  println(s"[SchedulerArithImp] " +
254    s"has intBusyTable: ${intBusyTable.nonEmpty}, " +
255    s"has vfBusyTable: ${vfBusyTable.nonEmpty}")
256
257  issueQueues.zipWithIndex.foreach { case (iq, i) =>
258    iq.io.flush <> io.fromCtrlBlock.flush
259    iq.io.enq <> dispatch2Iq.io.out(i)
260    iq.io.wakeupFromWB := wakeupFromWBVec
261  }
262}
263
264// FIXME: Vector mem instructions may not be handled properly!
265class SchedulerMemImp(override val wrapper: Scheduler)(implicit params: SchdBlockParams, p: Parameters)
266  extends SchedulerImpBase(wrapper)
267    with HasXSParameter
268{
269  println(s"[SchedulerMemImp] " +
270    s"has intBusyTable: ${intBusyTable.nonEmpty}, " +
271    s"has vfBusyTable: ${vfBusyTable.nonEmpty}")
272
273  val memAddrIQs = issueQueues.filter(iq => iq.params.StdCnt == 0)
274  val stAddrIQs = issueQueues.filter(iq => iq.params.StaCnt > 0) // included in memAddrIQs
275  val ldAddrIQs = issueQueues.filter(iq => iq.params.LduCnt > 0)
276  val stDataIQs = issueQueues.filter(iq => iq.params.StdCnt > 0)
277  require(memAddrIQs.nonEmpty && stDataIQs.nonEmpty)
278
279  io.toMem.get.loadFastMatch := 0.U.asTypeOf(io.toMem.get.loadFastMatch) // TODO: is still needed?
280
281  memAddrIQs.zipWithIndex.foreach { case (iq, i) =>
282    iq.io.flush <> io.fromCtrlBlock.flush
283    iq.io.enq <> dispatch2Iq.io.out(i)
284    iq.io.wakeupFromWB := wakeupFromWBVec
285  }
286
287  ldAddrIQs.foreach {
288    case imp: IssueQueueMemAddrImp =>
289      imp.io.memIO.get.feedbackIO <> io.fromMem.get.ldaFeedback
290      imp.io.memIO.get.checkWait.stIssuePtr := io.fromMem.get.stIssuePtr
291      imp.io.memIO.get.checkWait.memWaitUpdateReq := io.fromMem.get.memWaitUpdateReq
292    case _ =>
293  }
294
295  stAddrIQs.foreach {
296    case imp: IssueQueueMemAddrImp =>
297      imp.io.memIO.get.feedbackIO <> io.fromMem.get.staFeedback
298      imp.io.memIO.get.checkWait.stIssuePtr := io.fromMem.get.stIssuePtr
299      imp.io.memIO.get.checkWait.memWaitUpdateReq := io.fromMem.get.memWaitUpdateReq
300    case _ =>
301  }
302
303  // TODO: Implement vstu
304  issueQueues.filter(iq => iq.params.VstuCnt > 0).foreach {
305    case imp: IssueQueueMemAddrImp =>
306      imp.io.memIO.get.feedbackIO <> DontCare
307      imp.io.memIO.get.checkWait.stIssuePtr := DontCare
308      imp.io.memIO.get.checkWait.memWaitUpdateReq := DontCare
309    case _ =>
310  }
311
312  // TODO: Implement vldu
313  issueQueues.filter(iq => iq.params.VlduCnt > 0).foreach {
314    case imp: IssueQueueMemAddrImp =>
315      imp.io.memIO.get.feedbackIO <> DontCare
316      imp.io.memIO.get.checkWait.stIssuePtr := DontCare
317      imp.io.memIO.get.checkWait.memWaitUpdateReq := DontCare
318    case _ =>
319  }
320
321  private val staIdxSeq = issueQueues.filter(iq => iq.params.StaCnt > 0).map(iq => iq.params.idxInSchBlk)
322
323  for ((idxInSchBlk, i) <- staIdxSeq.zipWithIndex) {
324    dispatch2Iq.io.out(idxInSchBlk).zip(stAddrIQs(i).io.enq).zip(stDataIQs(i).io.enq).foreach{ case((di, staIQ), stdIQ) =>
325      val isAllReady = staIQ.ready && stdIQ.ready
326      di.ready := isAllReady
327      staIQ.valid := di.valid && isAllReady
328      stdIQ.valid := di.valid && isAllReady
329    }
330  }
331
332  require(stAddrIQs.size == stDataIQs.size, s"number of store address IQs(${stAddrIQs.size}) " +
333    s"should be equal to number of data IQs(${stDataIQs})")
334  stDataIQs.zip(stAddrIQs).zipWithIndex.foreach { case ((stdIQ, staIQ), i) =>
335    stdIQ.io.flush <> io.fromCtrlBlock.flush
336
337    stdIQ.io.enq.zip(staIQ.io.enq).foreach { case (stdIQEnq, staIQEnq) =>
338      stdIQEnq.bits  := staIQEnq.bits
339      // Store data reuses store addr src(1) in dispatch2iq
340      // [dispatch2iq] --src*------src*(0)--> [staIQ]
341      //                       \
342      //                        ---src*(1)--> [stdIQ]
343      // Since the src(1) of sta is easier to get, stdIQEnq.bits.src*(0) is assigned to staIQEnq.bits.src*(1)
344      // instead of dispatch2Iq.io.out(x).bits.src*(1)
345      stdIQEnq.bits.srcState(0) := staIQEnq.bits.srcState(1)
346      stdIQEnq.bits.srcType(0) := staIQEnq.bits.srcType(1)
347      stdIQEnq.bits.dataSource(0) := staIQEnq.bits.dataSource(1)
348      stdIQEnq.bits.l1ExuOH(0) := staIQEnq.bits.l1ExuOH(1)
349      stdIQEnq.bits.psrc(0) := staIQEnq.bits.psrc(1)
350      stdIQEnq.bits.sqIdx := staIQEnq.bits.sqIdx
351    }
352    stdIQ.io.wakeupFromWB := wakeupFromWBVec
353  }
354
355  val lsqEnqCtrl = Module(new LsqEnqCtrl)
356
357  lsqEnqCtrl.io.redirect <> io.fromCtrlBlock.flush
358  lsqEnqCtrl.io.enq <> dispatch2Iq.io.enqLsqIO.get
359  lsqEnqCtrl.io.lcommit := io.fromMem.get.lcommit
360  lsqEnqCtrl.io.scommit := io.fromMem.get.scommit
361  lsqEnqCtrl.io.lqCancelCnt := io.fromMem.get.lqCancelCnt
362  lsqEnqCtrl.io.sqCancelCnt := io.fromMem.get.sqCancelCnt
363  io.memIO.get.lsqEnqIO <> lsqEnqCtrl.io.enqLsq
364}
365