xref: /XiangShan/src/main/scala/xiangshan/backend/exu/ExeUnit.scala (revision 9477429f7dc92dfd72de3908b8e953de2886a01d)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16
17package xiangshan.backend.exu
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.experimental.hierarchy.{Definition, instantiable}
22import chisel3.util._
23import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
24import utility.DelayN
25import utils._
26import xiangshan.backend.fu.{CSRFileIO, FenceIO, FuncUnitInput}
27import xiangshan.backend.Bundles.{ExuInput, ExuOutput, MemExuInput, MemExuOutput}
28import xiangshan.{FPUCtrlSignals, HasXSParameter, Redirect, XSBundle, XSModule}
29import xiangshan.backend.datapath.WbConfig.{PregWB, _}
30import xiangshan.backend.fu.FuType
31
32class ExeUnitIO(params: ExeUnitParams)(implicit p: Parameters) extends XSBundle {
33  val flush = Flipped(ValidIO(new Redirect()))
34  val in = Flipped(DecoupledIO(new ExuInput(params)))
35  val out = DecoupledIO(new ExuOutput(params))
36  val csrio = if (params.hasCSR) Some(new CSRFileIO) else None
37  val fenceio = if (params.hasFence) Some(new FenceIO) else None
38  val frm = if (params.needSrcFrm) Some(Input(UInt(3.W))) else None
39  val vxrm = if (params.needSrcVxrm) Some(Input(UInt(2.W))) else None
40}
41
42class ExeUnit(val exuParams: ExeUnitParams)(implicit p: Parameters) extends LazyModule {
43  override def shouldBeInlined: Boolean = false
44
45  lazy val module = new ExeUnitImp(this)(p, exuParams)
46}
47
48class ExeUnitImp(
49  override val wrapper: ExeUnit
50)(implicit
51  p: Parameters, exuParams: ExeUnitParams
52) extends LazyModuleImp(wrapper) with HasXSParameter{
53  private val fuCfgs = exuParams.fuConfigs
54
55  val io = IO(new ExeUnitIO(exuParams))
56
57  val funcUnit = fuCfgs.map(cfg => {
58    assert(cfg.fuGen != null, cfg.name + "Cfg'fuGen is null !!!")
59    val module = cfg.fuGen(p, cfg)
60    module
61  })
62
63  val funcUnits = fuCfgs.zip(funcUnit).map{case(cfg, fu) =>
64    val clk_en = WireInit(false.B)
65    val fuVld_en = WireInit(false.B)
66    val fuVld_en_reg = RegInit(false.B)
67    val uncer_en_reg = RegInit(false.B)
68
69    val lat0 = FuType.isLat0(io.in.bits.fuType)
70    val latN = FuType.isLatN(io.in.bits.fuType)
71    val uncerLat = FuType.isUncerLat(io.in.bits.fuType)
72
73    def lat: Int = cfg.latency.latencyVal.getOrElse(0)
74
75    val fuVldVec = (io.in.valid && latN) +: Seq.fill(lat)(RegInit(false.B))
76    val fuRdyVec = Seq.fill(lat)(Wire(Bool())) :+ io.out.ready
77
78    for (i <- 0 until lat) {
79      fuRdyVec(i) := !fuVldVec(i + 1) || fuRdyVec(i + 1)
80    }
81
82    for (i <- 1 to lat) {
83      when(fuRdyVec(i - 1) && fuVldVec(i - 1)) {
84        fuVldVec(i) := fuVldVec(i - 1)
85      }.elsewhen(fuRdyVec(i)) {
86        fuVldVec(i) := false.B
87      }
88    }
89    fuVld_en := fuVldVec.map(v => v).reduce(_ || _)
90    fuVld_en_reg := fuVld_en
91
92    when(uncerLat && io.in.fire) {
93      uncer_en_reg := true.B
94    }.elsewhen(uncerLat && io.out.fire) {
95      uncer_en_reg := false.B
96    }
97
98    when(lat0 && io.in.fire) {
99      clk_en := true.B
100    }.elsewhen(latN && fuVld_en || fuVld_en_reg) {
101      clk_en := true.B
102    }.elsewhen(uncerLat && io.in.fire || uncer_en_reg) {
103      clk_en := true.B
104    }
105
106    if (cfg.ckAlwaysEn) {
107      clk_en := true.B
108    }
109
110    val clk_gate = Module(new ClockGate)
111    clk_gate.io.TE := false.B
112    clk_gate.io.E := clk_en
113    clk_gate.io.CK := clock
114    fu.clock := clk_gate.io.Q
115    XSPerfAccumulate(s"clock_gate_en_${fu.cfg.name}", clk_en)
116    fu
117  }
118
119  val busy = RegInit(false.B)
120  if (exuParams.latencyCertain){
121    busy := false.B
122  }
123  else {
124    val robIdx = RegEnable(io.in.bits.robIdx, io.in.fire)
125    when(io.in.fire && io.in.bits.robIdx.needFlush(io.flush)) {
126      busy := false.B
127    }.elsewhen(busy && robIdx.needFlush(io.flush)) {
128      busy := false.B
129    }.elsewhen(io.out.fire) {
130      busy := false.B
131    }.elsewhen(io.in.fire) {
132      busy := true.B
133    }
134  }
135
136  exuParams.wbPortConfigs.map{
137    x => x match {
138      case IntWB(port, priority) => assert(priority >= 0 && priority <= 2,
139        s"${exuParams.name}: WbPort must priority=0 or priority=1")
140      case VfWB (port, priority) => assert(priority >= 0 && priority <= 2,
141        s"${exuParams.name}: WbPort must priority=0 or priority=1")
142      case _ =>
143    }
144  }
145  val intWbPort = exuParams.getIntWBPort
146  if (intWbPort.isDefined){
147    val sameIntPortExuParam = backendParams.allExuParams.filter(_.getIntWBPort.isDefined)
148      .filter(_.getIntWBPort.get.port == intWbPort.get.port)
149    val samePortOneCertainOneUncertain = sameIntPortExuParam.map(_.latencyCertain).contains(true) && sameIntPortExuParam.map(_.latencyCertain).contains(false)
150    if (samePortOneCertainOneUncertain) sameIntPortExuParam.map(samePort =>
151      samePort.wbPortConfigs.map(
152        x => x match {
153          case IntWB(port, priority) => {
154            if (!samePort.latencyCertain) assert(priority == sameIntPortExuParam.size - 1,
155              s"${samePort.name}: IntWbPort $port must latencyCertain priority=0 or latencyUnCertain priority=max(${sameIntPortExuParam.size - 1})")
156            // Certain latency can be handled by WbBusyTable, so there is no need to limit the exu's WB priority
157          }
158          case _ =>
159        }
160      )
161    )
162  }
163  val vfWbPort = exuParams.getVfWBPort
164  if (vfWbPort.isDefined) {
165    val sameVfPortExuParam = backendParams.allExuParams.filter(_.getVfWBPort.isDefined)
166      .filter(_.getVfWBPort.get.port == vfWbPort.get.port)
167    val samePortOneCertainOneUncertain = sameVfPortExuParam.map(_.latencyCertain).contains(true) && sameVfPortExuParam.map(_.latencyCertain).contains(false)
168    if (samePortOneCertainOneUncertain)  sameVfPortExuParam.map(samePort =>
169      samePort.wbPortConfigs.map(
170        x => x match {
171          case VfWB(port, priority) => {
172            if (!samePort.latencyCertain) assert(priority == sameVfPortExuParam.size - 1,
173              s"${samePort.name}: VfWbPort $port must latencyCertain priority=0 or latencyUnCertain priority=max(${sameVfPortExuParam.size - 1})")
174            // Certain latency can be handled by WbBusyTable, so there is no need to limit the exu's WB priority
175          }
176          case _ =>
177        }
178      )
179    )
180  }
181  if(backendParams.debugEn) {
182    dontTouch(io.out.ready)
183  }
184  // rob flush --> funcUnits
185  funcUnits.zipWithIndex.foreach { case (fu, i) =>
186    fu.io.flush <> io.flush
187  }
188
189  def acceptCond(input: ExuInput): Seq[Bool] = {
190    input.params.fuConfigs.map(_.fuSel(input))
191  }
192
193  val in1ToN = Module(new Dispatcher(new ExuInput(exuParams), funcUnits.length, acceptCond))
194
195  // ExeUnit.in <---> Dispatcher.in
196  in1ToN.io.in.valid := io.in.valid && !busy
197  in1ToN.io.in.bits := io.in.bits
198  io.in.ready := !busy && in1ToN.io.in.ready
199
200  // Dispatcher.out <---> FunctionUnits
201  in1ToN.io.out.zip(funcUnits.map(_.io.in)).foreach {
202    case (source: DecoupledIO[ExuInput], sink: DecoupledIO[FuncUnitInput]) =>
203      sink.valid := source.valid
204      source.ready := sink.ready
205
206      sink.bits.data.src.zip(source.bits.src).foreach { case(fuSrc, exuSrc) => fuSrc := exuSrc }
207      sink.bits.data.pc          .foreach(x => x := source.bits.pc.get)
208      sink.bits.data.imm         := source.bits.imm
209      sink.bits.ctrl.fuOpType    := source.bits.fuOpType
210      sink.bits.ctrl.robIdx      := source.bits.robIdx
211      sink.bits.ctrl.pdest       := source.bits.pdest
212      sink.bits.ctrl.rfWen       .foreach(x => x := source.bits.rfWen.get)
213      sink.bits.ctrl.fpWen       .foreach(x => x := source.bits.fpWen.get)
214      sink.bits.ctrl.vecWen      .foreach(x => x := source.bits.vecWen.get)
215      sink.bits.ctrl.flushPipe   .foreach(x => x := source.bits.flushPipe.get)
216      sink.bits.ctrl.preDecode   .foreach(x => x := source.bits.preDecode.get)
217      sink.bits.ctrl.ftqIdx      .foreach(x => x := source.bits.ftqIdx.get)
218      sink.bits.ctrl.ftqOffset   .foreach(x => x := source.bits.ftqOffset.get)
219      sink.bits.ctrl.predictInfo .foreach(x => x := source.bits.predictInfo.get)
220      sink.bits.ctrl.fpu         .foreach(x => x := source.bits.fpu.get)
221      sink.bits.ctrl.vpu         .foreach(x => x := source.bits.vpu.get)
222      sink.bits.perfDebugInfo    := source.bits.perfDebugInfo
223  }
224
225  private val fuOutValidOH = funcUnits.map(_.io.out.valid)
226  XSError(PopCount(fuOutValidOH) > 1.U, p"fuOutValidOH ${Binary(VecInit(fuOutValidOH).asUInt)} should be one-hot)\n")
227  private val fuOutBitsVec = funcUnits.map(_.io.out.bits)
228  private val fuRedirectVec: Seq[Option[ValidIO[Redirect]]] = funcUnits.map(_.io.out.bits.res.redirect)
229
230  // Assume that one fu can only write int or fp or vec,
231  // otherwise, wenVec should be assigned to wen in fu.
232  private val fuIntWenVec = funcUnits.map(x => x.cfg.needIntWen.B && x.io.out.bits.ctrl.rfWen.getOrElse(false.B))
233  private val fuFpWenVec  = funcUnits.map(x => x.cfg.needFpWen.B  && x.io.out.bits.ctrl.fpWen.getOrElse(false.B))
234  private val fuVecWenVec = funcUnits.map(x => x.cfg.needVecWen.B && x.io.out.bits.ctrl.vecWen.getOrElse(false.B))
235  // FunctionUnits <---> ExeUnit.out
236  io.out.valid := Cat(fuOutValidOH).orR
237  funcUnits.foreach(fu => fu.io.out.ready := io.out.ready)
238
239  // select one fu's result
240  io.out.bits.data := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.res.data))
241  io.out.bits.robIdx := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.ctrl.robIdx))
242  io.out.bits.pdest := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.ctrl.pdest))
243  io.out.bits.intWen.foreach(x => x := Mux1H(fuOutValidOH, fuIntWenVec))
244  io.out.bits.fpWen.foreach(x => x := Mux1H(fuOutValidOH, fuFpWenVec))
245  io.out.bits.vecWen.foreach(x => x := Mux1H(fuOutValidOH, fuVecWenVec))
246  io.out.bits.redirect.foreach(x => x := Mux1H((fuOutValidOH zip fuRedirectVec).filter(_._2.isDefined).map(x => (x._1, x._2.get))))
247  io.out.bits.fflags.foreach(x => x := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.res.fflags.getOrElse(0.U.asTypeOf(io.out.bits.fflags.get)))))
248  io.out.bits.wflags.foreach(x => x := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.ctrl.fpu.getOrElse(0.U.asTypeOf(new FPUCtrlSignals)).wflags)))
249  io.out.bits.vxsat.foreach(x => x := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.res.vxsat.getOrElse(0.U.asTypeOf(io.out.bits.vxsat.get)))))
250  io.out.bits.exceptionVec.foreach(x => x := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.ctrl.exceptionVec.getOrElse(0.U.asTypeOf(io.out.bits.exceptionVec.get)))))
251  io.out.bits.flushPipe.foreach(x => x := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.ctrl.flushPipe.getOrElse(0.U.asTypeOf(io.out.bits.flushPipe.get)))))
252  io.out.bits.replay.foreach(x => x := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.ctrl.replay.getOrElse(0.U.asTypeOf(io.out.bits.replay.get)))))
253  io.out.bits.predecodeInfo.foreach(x => x := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.ctrl.preDecode.getOrElse(0.U.asTypeOf(io.out.bits.predecodeInfo.get)))))
254
255  io.csrio.foreach(exuio => funcUnits.foreach(fu => fu.io.csrio.foreach{
256    fuio =>
257      exuio <> fuio
258      fuio.exception := DelayN(exuio.exception, 2)
259  }))
260  io.fenceio.foreach(exuio => funcUnits.foreach(fu => fu.io.fenceio.foreach(fuio => fuio <> exuio)))
261  io.frm.foreach(exuio => funcUnits.foreach(fu => fu.io.frm.foreach(fuio => fuio <> exuio)))
262  io.vxrm.foreach(exuio => funcUnits.foreach(fu => fu.io.vxrm.foreach(fuio => fuio <> exuio)))
263
264  // debug info
265  io.out.bits.debug     := 0.U.asTypeOf(io.out.bits.debug)
266  io.out.bits.debug.isPerfCnt := funcUnits.map(_.io.csrio.map(_.isPerfCnt)).map(_.getOrElse(false.B)).reduce(_ || _)
267  io.out.bits.debugInfo := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.perfDebugInfo))
268}
269
270class DispatcherIO[T <: Data](private val gen: T, n: Int) extends Bundle {
271  val in = Flipped(DecoupledIO(gen))
272
273  val out = Vec(n, DecoupledIO(gen))
274}
275
276class Dispatcher[T <: Data](private val gen: T, n: Int, acceptCond: T => Seq[Bool])
277  (implicit p: Parameters)
278  extends Module {
279
280  val io = IO(new DispatcherIO(gen, n))
281
282  private val acceptVec: Vec[Bool] = VecInit(acceptCond(io.in.bits))
283
284  XSError(io.in.valid && PopCount(acceptVec) > 1.U, s"s[ExeUnit] accept vec should no more than 1, ${Binary(acceptVec.asUInt)} ")
285  XSError(io.in.valid && PopCount(acceptVec) === 0.U, "[ExeUnit] there is a inst not dispatched to any fu")
286
287  io.out.zipWithIndex.foreach { case (out, i) =>
288    out.valid := acceptVec(i) && io.in.valid
289    out.bits := io.in.bits
290  }
291
292  io.in.ready := Mux1H(acceptVec,io.out.map(_.ready))
293}
294
295class MemExeUnitIO (implicit p: Parameters) extends XSBundle {
296  val flush = Flipped(ValidIO(new Redirect()))
297  val in = Flipped(DecoupledIO(new MemExuInput()))
298  val out = DecoupledIO(new MemExuOutput())
299}
300
301class MemExeUnit(exuParams: ExeUnitParams)(implicit p: Parameters) extends XSModule {
302  val io = IO(new MemExeUnitIO)
303  val fu = exuParams.fuConfigs.head.fuGen(p, exuParams.fuConfigs.head)
304  fu.io.flush             := io.flush
305  fu.io.in.valid          := io.in.valid
306  io.in.ready             := fu.io.in.ready
307
308  fu.io.in.bits.ctrl.robIdx    := io.in.bits.uop.robIdx
309  fu.io.in.bits.ctrl.pdest     := io.in.bits.uop.pdest
310  fu.io.in.bits.ctrl.fuOpType  := io.in.bits.uop.fuOpType
311  fu.io.in.bits.data.imm       := io.in.bits.uop.imm
312  fu.io.in.bits.data.src.zip(io.in.bits.src).foreach(x => x._1 := x._2)
313  fu.io.in.bits.perfDebugInfo := io.in.bits.uop.debugInfo
314
315  io.out.valid            := fu.io.out.valid
316  fu.io.out.ready         := io.out.ready
317
318  io.out.bits             := 0.U.asTypeOf(io.out.bits) // dontCare other fields
319  io.out.bits.data        := fu.io.out.bits.res.data
320  io.out.bits.uop.robIdx  := fu.io.out.bits.ctrl.robIdx
321  io.out.bits.uop.pdest   := fu.io.out.bits.ctrl.pdest
322  io.out.bits.uop.fuType  := io.in.bits.uop.fuType
323  io.out.bits.uop.fuOpType:= io.in.bits.uop.fuOpType
324  io.out.bits.uop.sqIdx   := io.in.bits.uop.sqIdx
325  io.out.bits.uop.debugInfo := fu.io.out.bits.perfDebugInfo
326
327  io.out.bits.debug       := 0.U.asTypeOf(io.out.bits.debug)
328}
329