xref: /XiangShan/src/main/scala/xiangshan/backend/datapath/RFWBConflictChecker.scala (revision 9b40a1819fb7704bee8a64803f8dc3462b0976b6)
1package xiangshan.backend.datapath
2
3import org.chipsalliance.cde.config.Parameters
4import chisel3._
5import chisel3.util._
6import utils.OptionWrapper
7import utils.SeqUtils.MixedVec2
8import xiangshan.backend.BackendParams
9import xiangshan.backend.datapath.DataConfig.{IntData, VecData}
10import xiangshan.backend.datapath.WbConfig.{NoWB, PregWB}
11import xiangshan.backend.regfile.PregParams
12
13case class RFWBCollideCheckerParams (
14  inWbCfgs: Seq[Seq[Set[PregWB]]],
15  pregParams: PregParams,
16) {
17  def genInputBundle: MixedVec2[DecoupledIO[RFWBCollideCheckerBundle]] = {
18    val pregWidth = pregParams.addrWidth
19    utils.SeqUtils.mapToMixedVec2(this.filteredCfgs, (wb: PregWB) => DecoupledIO(new RFWBCollideCheckerBundle(wb, pregWidth)))
20  }
21
22  def filteredCfgs: Seq[Seq[PregWB]] = inWbCfgs.map(_.map(x =>
23    if (x.map(_.dataCfg).contains(pregParams.dataCfg))
24      x.find(x => x.dataCfg == pregParams.dataCfg).get
25    else
26      NoWB()
27  ))
28
29  def portMax = filteredCfgs.flatten.map(_.port).max
30}
31
32class RFWBCollideCheckerBundle(var wbCfg: Option[PregWB], pregWidth: Int) extends Bundle {
33
34  def this(wbCfg_ : PregWB, pregWidth_ : Int) = this(Some(wbCfg_), pregWidth_)
35
36  def this(pregWidth_ : Int) = this(None, pregWidth_)
37}
38
39class RFWBCollideCheckerIO(val params: RFWBCollideCheckerParams)(implicit p: Parameters) extends Bundle {
40  private val pregWidth = params.pregParams.addrWidth
41  val in: MixedVec2[DecoupledIO[RFWBCollideCheckerBundle]] = Flipped(params.genInputBundle)
42  val out = Vec(params.portMax + 1, Valid(new RFWBCollideCheckerBundle(pregWidth)))
43}
44
45private object ArbiterCtrl {
46  def apply(request: Seq[Bool]): Seq[Bool] = request.length match {
47    case 0 => Seq()
48    case 1 => Seq(true.B)
49    case _ => request.head +: request.tail.init.scanLeft(request.head)(_ || _).map(!_)
50  }
51}
52
53// when io.in.valid is false.B, io.in.ready is true.B
54class WBArbiter[T <: Data](val gen: T, val n: Int) extends Module {
55  val io = IO(new ArbiterIO(gen, n))
56
57  // These parameters are not carefully set, and may be improved in the future
58  private val CounterWidth = 3
59  private val CounterThreshold = 7
60
61  /* To avoid some weird deadlock caused by delay of og0Cancel */
62  // Use a saturation counter to record the number of consecutive failed requests for each input port
63  // When a counter reaches the threshold, mark it as full
64  // Port marked as full will be prioritized the next time it sends a request
65
66  val cancelCounter      = RegInit(VecInit(Seq.fill(n)(0.U(CounterWidth.W))))
67  val isFull             = RegInit(VecInit(Seq.fill(n)(false.B)))
68  val cancelCounterNext  = Wire(Vec(n, UInt(CounterWidth.W)))
69  val isFullNext         = Wire(Vec(n, Bool()))
70  val hasFull            = RegInit(false.B)
71  val hasFullReq         = Wire(Bool())
72  val finalValid         = Wire(Vec(n, Bool()))
73
74  cancelCounter := cancelCounterNext
75  isFull        := isFullNext
76  hasFull       := isFullNext.asUInt.orR
77  hasFullReq    := io.in.zip(isFull).map{case (in, full) => in.valid && full}.reduce(_ || _)
78
79  cancelCounterNext.zip(isFullNext).zip(cancelCounter).zip(isFull).zipWithIndex.foreach{ case ((((cntNext, fullNext), cnt), full), i) =>
80    when (io.in(i).valid && !io.in(i).ready) {
81      cntNext   := Mux(cnt === CounterThreshold.U, CounterThreshold.U, cnt + 1.U)
82      fullNext  := cnt(CounterWidth - 1, 1).orR  // counterNext === CounterThreshold.U
83    }.elsewhen (io.in(i).valid && io.in(i).ready) {
84      cntNext   := 0.U
85      fullNext  := false.B
86    }.otherwise {
87      cntNext   := cnt
88      fullNext  := full
89    }
90  }
91
92  finalValid := io.in.zipWithIndex.map{ case (in, i) => in.valid && (!hasFull || !hasFullReq || isFull(i)) }
93
94  io.chosen := (n - 1).asUInt
95  io.out.bits := io.in(n - 1).bits
96  for (i <- n - 2 to 0 by -1) {
97    when(finalValid(i)) {
98      io.chosen := i.asUInt
99      io.out.bits := io.in(i).bits
100    }
101  }
102
103  // in_valid    grant      ready
104  // 0           *          1
105  // 1           0          0
106  // 1           1          1
107  val grant = ArbiterCtrl(finalValid)
108  for ((in, g) <- io.in.zip(grant))
109    in.ready := (g || !in.valid) && io.out.ready
110  io.out.valid := !grant.last || finalValid.last
111}
112
113abstract class RFWBCollideCheckerBase(params: RFWBCollideCheckerParams)(implicit p: Parameters) extends Module {
114  protected def portRange: Range
115
116  val io = IO(new RFWBCollideCheckerIO(params))
117  dontTouch(io)
118
119  protected val pregParams = params.pregParams
120  protected val pregWidth = pregParams.addrWidth
121
122  protected val inGroup = io.in
123    .flatten
124    .groupBy(_.bits.wbCfg.get.port)
125    .map(x => (x._1, x._2.sortBy(_.bits.wbCfg.get.priority)))
126
127  protected val arbiters: Seq[Option[WBArbiter[RFWBCollideCheckerBundle]]] = portRange.map { portIdx =>
128    OptionWrapper(
129      inGroup.isDefinedAt(portIdx),
130      Module(new WBArbiter(
131        new RFWBCollideCheckerBundle(pregWidth),
132        inGroup(portIdx).size
133      ))
134    )
135  }
136
137  // connection of IntWB or VfWB
138  arbiters.zipWithIndex.foreach { case (arb, portIdx) =>
139    if (arb.nonEmpty) {
140      arb.get.io.in.zip(inGroup(portIdx)).foreach { case (arbiterIn, ioIn) =>
141        arbiterIn <> ioIn
142      }
143    }
144  }
145
146  // connection of NoWB
147  io.in.map(_.map(x =>
148    if (x.bits.wbCfg.get.isInstanceOf[NoWB]) {
149      x.ready := true.B
150    }
151  ))
152
153  io.out.zip(arbiters).foreach { case (out, arb) =>
154    if (arb.nonEmpty) {
155      val arbOut = arb.get.io.out
156      arbOut.ready := true.B
157      out.valid := arbOut.valid
158      out.bits := arbOut.bits
159    } else {
160      out := 0.U.asTypeOf(out)
161    }
162  }
163}
164
165class IntRFWBCollideChecker(
166  backendParams: BackendParams
167)(implicit
168  p:Parameters
169) extends RFWBCollideCheckerBase(RFWBCollideCheckerParams(backendParams.getAllWbCfgs, backendParams.intPregParams)) {
170  override protected def portRange: Range = 0 to backendParams.getWbPortIndices(IntData()).max
171}
172
173class VfRFWBCollideChecker(
174  backendParams: BackendParams
175)(implicit
176  p:Parameters
177) extends RFWBCollideCheckerBase(RFWBCollideCheckerParams(backendParams.getAllWbCfgs, backendParams.vfPregParams)) {
178  override protected def portRange: Range = 0 to backendParams.getWbPortIndices(VecData()).max
179}
180