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.{ClockGate, 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 31import xiangshan.backend.fu.vector.Bundles.{VType, Vxrm} 32import xiangshan.backend.fu.fpu.Bundles.Frm 33 34class ExeUnitIO(params: ExeUnitParams)(implicit p: Parameters) extends XSBundle { 35 val flush = Flipped(ValidIO(new Redirect())) 36 val in = Flipped(DecoupledIO(new ExuInput(params))) 37 val out = DecoupledIO(new ExuOutput(params)) 38 val csrio = OptionWrapper(params.hasCSR, new CSRFileIO) 39 val fenceio = OptionWrapper(params.hasFence, new FenceIO) 40 val frm = OptionWrapper(params.needSrcFrm, Input(Frm())) 41 val vxrm = OptionWrapper(params.needSrcVxrm, Input(Vxrm())) 42 val vtype = OptionWrapper(params.writeVConfig, new VType) 43 val vlIsZero = OptionWrapper(params.writeVConfig, Output(Bool())) 44 val vlIsVlmax = OptionWrapper(params.writeVConfig, Output(Bool())) 45} 46 47class ExeUnit(val exuParams: ExeUnitParams)(implicit p: Parameters) extends LazyModule { 48 override def shouldBeInlined: Boolean = false 49 50 lazy val module = new ExeUnitImp(this)(p, exuParams) 51} 52 53class ExeUnitImp( 54 override val wrapper: ExeUnit 55)(implicit 56 p: Parameters, exuParams: ExeUnitParams 57) extends LazyModuleImp(wrapper) with HasXSParameter{ 58 private val fuCfgs = exuParams.fuConfigs 59 60 val io = IO(new ExeUnitIO(exuParams)) 61 62 val funcUnits = fuCfgs.map(cfg => { 63 assert(cfg.fuGen != null, cfg.name + "Cfg'fuGen is null !!!") 64 val module = cfg.fuGen(p, cfg) 65 module 66 }) 67 68 if (EnableClockGate) { 69 fuCfgs.zip(funcUnits).foreach { case (cfg, fu) => 70 val clk_en = WireInit(false.B) 71 val fuVld_en = WireInit(false.B) 72 val fuVld_en_reg = RegInit(false.B) 73 val uncer_en_reg = RegInit(false.B) 74 75 def latReal: Int = cfg.latency.latencyVal.getOrElse(0) 76 def extralat: Int = cfg.latency.extraLatencyVal.getOrElse(0) 77 78 val uncerLat = cfg.latency.uncertainLatencyVal.nonEmpty 79 val lat0 = (latReal == 0 && !uncerLat).asBool 80 val latN = (latReal > 0&& !uncerLat).asBool 81 82 83 84 val fuVldVec = (io.in.valid && latN) +: Seq.fill(latReal)(RegInit(false.B)) 85 val fuRdyVec = Seq.fill(latReal)(Wire(Bool())) :+ io.out.ready 86 87 for (i <- 0 until latReal) { 88 fuRdyVec(i) := !fuVldVec(i + 1) || fuRdyVec(i + 1) 89 } 90 91 for (i <- 1 to latReal) { 92 when(fuRdyVec(i - 1) && fuVldVec(i - 1)) { 93 fuVldVec(i) := fuVldVec(i - 1) 94 }.elsewhen(fuRdyVec(i)) { 95 fuVldVec(i) := false.B 96 } 97 } 98 fuVld_en := fuVldVec.map(v => v).reduce(_ || _) 99 fuVld_en_reg := fuVld_en 100 101 when(uncerLat.asBool && io.in.fire) { 102 uncer_en_reg := true.B 103 }.elsewhen(uncerLat.asBool && io.out.fire) { 104 uncer_en_reg := false.B 105 } 106 107 when(lat0 && io.in.fire) { 108 clk_en := true.B 109 }.elsewhen(latN && fuVld_en || fuVld_en_reg) { 110 clk_en := true.B 111 }.elsewhen(uncerLat.asBool && io.in.fire || uncer_en_reg) { 112 clk_en := true.B 113 } 114 115 if (cfg.ckAlwaysEn) { 116 clk_en := true.B 117 } 118 119 fu.clock := ClockGate(false.B, clk_en, clock) 120 XSPerfAccumulate(s"clock_gate_en_${fu.cfg.name}", clk_en) 121 } 122 } 123 124 val busy = RegInit(false.B) 125 if (exuParams.latencyCertain){ 126 busy := false.B 127 } 128 else { 129 val robIdx = RegEnable(io.in.bits.robIdx, io.in.fire) 130 when(io.in.fire && io.in.bits.robIdx.needFlush(io.flush)) { 131 busy := false.B 132 }.elsewhen(busy && robIdx.needFlush(io.flush)) { 133 busy := false.B 134 }.elsewhen(io.out.fire) { 135 busy := false.B 136 }.elsewhen(io.in.fire) { 137 busy := true.B 138 } 139 } 140 141 exuParams.wbPortConfigs.map{ 142 x => x match { 143 case IntWB(port, priority) => assert(priority >= 0 && priority <= 2, 144 s"${exuParams.name}: WbPort must priority=0 or priority=1") 145 case FpWB(port, priority) => assert(priority >= 0 && priority <= 2, 146 s"${exuParams.name}: WbPort must priority=0 or priority=1") 147 case VfWB (port, priority) => assert(priority >= 0 && priority <= 2, 148 s"${exuParams.name}: WbPort must priority=0 or priority=1") 149 case _ => 150 } 151 } 152 val intWbPort = exuParams.getIntWBPort 153 if (intWbPort.isDefined){ 154 val sameIntPortExuParam = backendParams.allExuParams.filter(_.getIntWBPort.isDefined) 155 .filter(_.getIntWBPort.get.port == intWbPort.get.port) 156 val samePortOneCertainOneUncertain = sameIntPortExuParam.map(_.latencyCertain).contains(true) && sameIntPortExuParam.map(_.latencyCertain).contains(false) 157 if (samePortOneCertainOneUncertain) sameIntPortExuParam.map(samePort => 158 samePort.wbPortConfigs.map( 159 x => x match { 160 case IntWB(port, priority) => { 161 if (!samePort.latencyCertain) assert(priority == sameIntPortExuParam.size - 1, 162 s"${samePort.name}: IntWbPort $port must latencyCertain priority=0 or latencyUnCertain priority=max(${sameIntPortExuParam.size - 1})") 163 // Certain latency can be handled by WbBusyTable, so there is no need to limit the exu's WB priority 164 } 165 case _ => 166 } 167 ) 168 ) 169 } 170 val fpWbPort = exuParams.getFpWBPort 171 if (fpWbPort.isDefined) { 172 val sameFpPortExuParam = backendParams.allExuParams.filter(_.getFpWBPort.isDefined) 173 .filter(_.getFpWBPort.get.port == fpWbPort.get.port) 174 val samePortOneCertainOneUncertain = sameFpPortExuParam.map(_.latencyCertain).contains(true) && sameFpPortExuParam.map(_.latencyCertain).contains(false) 175 if (samePortOneCertainOneUncertain) sameFpPortExuParam.map(samePort => 176 samePort.wbPortConfigs.map( 177 x => x match { 178 case FpWB(port, priority) => { 179 if (!samePort.latencyCertain) assert(priority == sameFpPortExuParam.size - 1, 180 s"${samePort.name}: FpWbPort $port must latencyCertain priority=0 or latencyUnCertain priority=max(${sameFpPortExuParam.size - 1})") 181 // Certain latency can be handled by WbBusyTable, so there is no need to limit the exu's WB priority 182 } 183 case _ => 184 } 185 ) 186 ) 187 } 188 val vfWbPort = exuParams.getVfWBPort 189 if (vfWbPort.isDefined) { 190 val sameVfPortExuParam = backendParams.allExuParams.filter(_.getVfWBPort.isDefined) 191 .filter(_.getVfWBPort.get.port == vfWbPort.get.port) 192 val samePortOneCertainOneUncertain = sameVfPortExuParam.map(_.latencyCertain).contains(true) && sameVfPortExuParam.map(_.latencyCertain).contains(false) 193 if (samePortOneCertainOneUncertain) sameVfPortExuParam.map(samePort => 194 samePort.wbPortConfigs.map( 195 x => x match { 196 case VfWB(port, priority) => { 197 if (!samePort.latencyCertain) assert(priority == sameVfPortExuParam.size - 1, 198 s"${samePort.name}: VfWbPort $port must latencyCertain priority=0 or latencyUnCertain priority=max(${sameVfPortExuParam.size - 1})") 199 // Certain latency can be handled by WbBusyTable, so there is no need to limit the exu's WB priority 200 } 201 case _ => 202 } 203 ) 204 ) 205 } 206 if(backendParams.debugEn) { 207 dontTouch(io.out.ready) 208 } 209 // rob flush --> funcUnits 210 funcUnits.zipWithIndex.foreach { case (fu, i) => 211 fu.io.flush <> io.flush 212 } 213 214 def acceptCond(input: ExuInput): Seq[Bool] = { 215 input.params.fuConfigs.map(_.fuSel(input)) 216 } 217 218 val in1ToN = Module(new Dispatcher(new ExuInput(exuParams), funcUnits.length, acceptCond)) 219 220 // ExeUnit.in <---> Dispatcher.in 221 in1ToN.io.in.valid := io.in.valid && !busy 222 in1ToN.io.in.bits := io.in.bits 223 io.in.ready := !busy && in1ToN.io.in.ready 224 225 // Dispatcher.out <---> FunctionUnits 226 in1ToN.io.out.zip(funcUnits.map(_.io.in)).foreach { 227 case (source: DecoupledIO[ExuInput], sink: DecoupledIO[FuncUnitInput]) => 228 sink.valid := source.valid 229 source.ready := sink.ready 230 231 sink.bits.data.src.zip(source.bits.src).foreach { case(fuSrc, exuSrc) => fuSrc := exuSrc } 232 sink.bits.data.pc .foreach(x => x := source.bits.pc.get) 233 sink.bits.data.imm := source.bits.imm 234 sink.bits.ctrl.fuOpType := source.bits.fuOpType 235 sink.bits.ctrl.robIdx := source.bits.robIdx 236 sink.bits.ctrl.pdest := source.bits.pdest 237 sink.bits.ctrl.rfWen .foreach(x => x := source.bits.rfWen.get) 238 sink.bits.ctrl.fpWen .foreach(x => x := source.bits.fpWen.get) 239 sink.bits.ctrl.vecWen .foreach(x => x := source.bits.vecWen.get) 240 sink.bits.ctrl.flushPipe .foreach(x => x := source.bits.flushPipe.get) 241 sink.bits.ctrl.preDecode .foreach(x => x := source.bits.preDecode.get) 242 sink.bits.ctrl.ftqIdx .foreach(x => x := source.bits.ftqIdx.get) 243 sink.bits.ctrl.ftqOffset .foreach(x => x := source.bits.ftqOffset.get) 244 sink.bits.ctrl.predictInfo .foreach(x => x := source.bits.predictInfo.get) 245 sink.bits.ctrl.fpu .foreach(x => x := source.bits.fpu.get) 246 sink.bits.ctrl.vpu .foreach(x => x := source.bits.vpu.get) 247 sink.bits.perfDebugInfo := source.bits.perfDebugInfo 248 } 249 250 private val OutresVecs = funcUnits.map { fu => 251 def latDiff :Int = fu.cfg.latency.extraLatencyVal.getOrElse(0) 252 val OutresVec = fu.io.out.bits.res +: Seq.fill(latDiff)(Reg(chiselTypeOf(fu.io.out.bits.res))) 253 for (i <- 1 to latDiff) { 254 OutresVec(i) := OutresVec(i - 1) 255 } 256 OutresVec 257 } 258 OutresVecs.foreach(vec => vec.foreach(res =>dontTouch(res))) 259 260 private val fuOutValidOH = funcUnits.map(_.io.out.valid) 261 XSError(PopCount(fuOutValidOH) > 1.U, p"fuOutValidOH ${Binary(VecInit(fuOutValidOH).asUInt)} should be one-hot)\n") 262 private val fuOutBitsVec = funcUnits.map(_.io.out.bits) 263 private val fuOutresVec = OutresVecs.map(_.last) 264 private val fuRedirectVec: Seq[Option[ValidIO[Redirect]]] = fuOutresVec.map(_.redirect) 265 266 // Assume that one fu can only write int or fp or vec, 267 // otherwise, wenVec should be assigned to wen in fu. 268 private val fuIntWenVec = funcUnits.map(x => x.cfg.needIntWen.B && x.io.out.bits.ctrl.rfWen.getOrElse(false.B)) 269 private val fuFpWenVec = funcUnits.map(x => x.cfg.needFpWen.B && x.io.out.bits.ctrl.fpWen.getOrElse(false.B)) 270 private val fuVecWenVec = funcUnits.map(x => x.cfg.needVecWen.B && x.io.out.bits.ctrl.vecWen.getOrElse(false.B)) 271 // FunctionUnits <---> ExeUnit.out 272 io.out.valid := Cat(fuOutValidOH).orR 273 funcUnits.foreach(fu => fu.io.out.ready := io.out.ready) 274 275 // select one fu's result 276 io.out.bits.data := Mux1H(fuOutValidOH, fuOutresVec.map(_.data)) 277 io.out.bits.robIdx := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.ctrl.robIdx)) 278 io.out.bits.pdest := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.ctrl.pdest)) 279 io.out.bits.intWen.foreach(x => x := Mux1H(fuOutValidOH, fuIntWenVec)) 280 io.out.bits.fpWen.foreach(x => x := Mux1H(fuOutValidOH, fuFpWenVec)) 281 io.out.bits.vecWen.foreach(x => x := Mux1H(fuOutValidOH, fuVecWenVec)) 282 io.out.bits.redirect.foreach(x => x := Mux1H((fuOutValidOH zip fuRedirectVec).filter(_._2.isDefined).map(x => (x._1, x._2.get)))) 283 io.out.bits.fflags.foreach(x => x := Mux1H(fuOutValidOH, fuOutresVec.map(_.fflags.getOrElse(0.U.asTypeOf(io.out.bits.fflags.get))))) 284 io.out.bits.wflags.foreach(x => x := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.ctrl.fpu.getOrElse(0.U.asTypeOf(new FPUCtrlSignals)).wflags))) 285 io.out.bits.vxsat.foreach(x => x := Mux1H(fuOutValidOH, fuOutresVec.map(_.vxsat.getOrElse(0.U.asTypeOf(io.out.bits.vxsat.get))))) 286 io.out.bits.exceptionVec.foreach(x => x := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.ctrl.exceptionVec.getOrElse(0.U.asTypeOf(io.out.bits.exceptionVec.get))))) 287 io.out.bits.flushPipe.foreach(x => x := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.ctrl.flushPipe.getOrElse(0.U.asTypeOf(io.out.bits.flushPipe.get))))) 288 io.out.bits.replay.foreach(x => x := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.ctrl.replay.getOrElse(0.U.asTypeOf(io.out.bits.replay.get))))) 289 io.out.bits.predecodeInfo.foreach(x => x := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.ctrl.preDecode.getOrElse(0.U.asTypeOf(io.out.bits.predecodeInfo.get))))) 290 291 io.csrio.foreach(exuio => funcUnits.foreach(fu => fu.io.csrio.foreach{ 292 fuio => 293 exuio <> fuio 294 fuio.exception := DelayN(exuio.exception, 2) 295 })) 296 297 io.vtype.foreach(exuio => funcUnits.foreach(fu => fu.io.vtype.foreach(fuio => exuio := fuio))) 298 io.fenceio.foreach(exuio => funcUnits.foreach(fu => fu.io.fenceio.foreach(fuio => fuio <> exuio))) 299 io.frm.foreach(exuio => funcUnits.foreach(fu => fu.io.frm.foreach(fuio => fuio <> exuio))) 300 io.vxrm.foreach(exuio => funcUnits.foreach(fu => fu.io.vxrm.foreach(fuio => fuio <> exuio))) 301 io.vlIsZero.foreach(exuio => funcUnits.foreach(fu => fu.io.vlIsZero.foreach(fuio => exuio := fuio))) 302 io.vlIsVlmax.foreach(exuio => funcUnits.foreach(fu => fu.io.vlIsVlmax.foreach(fuio => exuio := fuio))) 303 304 // debug info 305 io.out.bits.debug := 0.U.asTypeOf(io.out.bits.debug) 306 io.out.bits.debug.isPerfCnt := funcUnits.map(_.io.csrio.map(_.isPerfCnt)).map(_.getOrElse(false.B)).reduce(_ || _) 307 io.out.bits.debugInfo := Mux1H(fuOutValidOH, fuOutBitsVec.map(_.perfDebugInfo)) 308} 309 310class DispatcherIO[T <: Data](private val gen: T, n: Int) extends Bundle { 311 val in = Flipped(DecoupledIO(gen)) 312 313 val out = Vec(n, DecoupledIO(gen)) 314} 315 316class Dispatcher[T <: Data](private val gen: T, n: Int, acceptCond: T => Seq[Bool]) 317 (implicit p: Parameters) 318 extends Module { 319 320 val io = IO(new DispatcherIO(gen, n)) 321 322 private val acceptVec: Vec[Bool] = VecInit(acceptCond(io.in.bits)) 323 324 XSError(io.in.valid && PopCount(acceptVec) > 1.U, p"[ExeUnit] accept vec should no more than 1, ${Binary(acceptVec.asUInt)} ") 325 XSError(io.in.valid && PopCount(acceptVec) === 0.U, "[ExeUnit] there is a inst not dispatched to any fu") 326 327 io.out.zipWithIndex.foreach { case (out, i) => 328 out.valid := acceptVec(i) && io.in.valid 329 out.bits := io.in.bits 330 } 331 332 io.in.ready := Mux1H(acceptVec,io.out.map(_.ready)) 333} 334 335class MemExeUnitIO (implicit p: Parameters) extends XSBundle { 336 val flush = Flipped(ValidIO(new Redirect())) 337 val in = Flipped(DecoupledIO(new MemExuInput())) 338 val out = DecoupledIO(new MemExuOutput()) 339} 340 341class MemExeUnit(exuParams: ExeUnitParams)(implicit p: Parameters) extends XSModule { 342 val io = IO(new MemExeUnitIO) 343 val fu = exuParams.fuConfigs.head.fuGen(p, exuParams.fuConfigs.head) 344 fu.io.flush := io.flush 345 fu.io.in.valid := io.in.valid 346 io.in.ready := fu.io.in.ready 347 348 fu.io.in.bits.ctrl.robIdx := io.in.bits.uop.robIdx 349 fu.io.in.bits.ctrl.pdest := io.in.bits.uop.pdest 350 fu.io.in.bits.ctrl.fuOpType := io.in.bits.uop.fuOpType 351 fu.io.in.bits.data.imm := io.in.bits.uop.imm 352 fu.io.in.bits.data.src.zip(io.in.bits.src).foreach(x => x._1 := x._2) 353 fu.io.in.bits.perfDebugInfo := io.in.bits.uop.debugInfo 354 355 io.out.valid := fu.io.out.valid 356 fu.io.out.ready := io.out.ready 357 358 io.out.bits := 0.U.asTypeOf(io.out.bits) // dontCare other fields 359 io.out.bits.data := fu.io.out.bits.res.data 360 io.out.bits.uop.robIdx := fu.io.out.bits.ctrl.robIdx 361 io.out.bits.uop.pdest := fu.io.out.bits.ctrl.pdest 362 io.out.bits.uop.fuType := io.in.bits.uop.fuType 363 io.out.bits.uop.fuOpType:= io.in.bits.uop.fuOpType 364 io.out.bits.uop.sqIdx := io.in.bits.uop.sqIdx 365 io.out.bits.uop.debugInfo := fu.io.out.bits.perfDebugInfo 366 367 io.out.bits.debug := 0.U.asTypeOf(io.out.bits.debug) 368}