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.rob 18 19import chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import difftest._ 23import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 24import utils._ 25import utility._ 26import xiangshan._ 27import xiangshan.backend.exu.ExuConfig 28import xiangshan.frontend.FtqPtr 29 30class DebugMdpInfo(implicit p: Parameters) extends XSBundle{ 31 val ssid = UInt(SSIDWidth.W) 32 val waitAllStore = Bool() 33} 34 35class DebugLsInfo(implicit p: Parameters) extends XSBundle{ 36 val s1 = new Bundle{ 37 val isTlbFirstMiss = Bool() // in s1 38 val isBankConflict = Bool() // in s1 39 val isLoadToLoadForward = Bool() 40 val isReplayFast = Bool() 41 } 42 val s2 = new Bundle{ 43 val isDcacheFirstMiss = Bool() // in s2 (predicted result is in s1 when using WPU, real result is in s2) 44 val isForwardFail = Bool() // in s2 45 val isReplaySlow = Bool() 46 val isLoadReplayTLBMiss = Bool() 47 val isLoadReplayCacheMiss = Bool() 48 } 49 val replayCnt = UInt(XLEN.W) 50 51 def s1SignalEnable(ena: DebugLsInfo) = { 52 when(ena.s1.isTlbFirstMiss) { s1.isTlbFirstMiss := true.B } 53 when(ena.s1.isBankConflict) { s1.isBankConflict := true.B } 54 when(ena.s1.isLoadToLoadForward) { s1.isLoadToLoadForward := true.B } 55 when(ena.s1.isReplayFast) { 56 s1.isReplayFast := true.B 57 replayCnt := replayCnt + 1.U 58 } 59 } 60 61 def s2SignalEnable(ena: DebugLsInfo) = { 62 when(ena.s2.isDcacheFirstMiss) { s2.isDcacheFirstMiss := true.B } 63 when(ena.s2.isForwardFail) { s2.isForwardFail := true.B } 64 when(ena.s2.isLoadReplayTLBMiss) { s2.isLoadReplayTLBMiss := true.B } 65 when(ena.s2.isLoadReplayCacheMiss) { s2.isLoadReplayCacheMiss := true.B } 66 when(ena.s2.isReplaySlow) { 67 s2.isReplaySlow := true.B 68 replayCnt := replayCnt + 1.U 69 } 70 } 71 72} 73object DebugLsInfo{ 74 def init(implicit p: Parameters): DebugLsInfo = { 75 val lsInfo = Wire(new DebugLsInfo) 76 lsInfo.s1.isTlbFirstMiss := false.B 77 lsInfo.s1.isBankConflict := false.B 78 lsInfo.s1.isLoadToLoadForward := false.B 79 lsInfo.s1.isReplayFast := false.B 80 lsInfo.s2.isDcacheFirstMiss := false.B 81 lsInfo.s2.isForwardFail := false.B 82 lsInfo.s2.isReplaySlow := false.B 83 lsInfo.s2.isLoadReplayTLBMiss := false.B 84 lsInfo.s2.isLoadReplayCacheMiss := false.B 85 lsInfo.replayCnt := 0.U 86 lsInfo 87 } 88 89} 90class DebugLsInfoBundle(implicit p: Parameters) extends DebugLsInfo { 91 // unified processing at the end stage of load/store ==> s2 ==> bug that will write error robIdx data 92 val s1_robIdx = UInt(log2Ceil(RobSize).W) 93 val s2_robIdx = UInt(log2Ceil(RobSize).W) 94} 95class DebugLSIO(implicit p: Parameters) extends XSBundle { 96 val debugLsInfo = Vec(exuParameters.LduCnt + exuParameters.StuCnt, Output(new DebugLsInfoBundle)) 97} 98 99class DebugInstDB(implicit p: Parameters) extends XSBundle{ 100 val globalID = UInt(XLEN.W) 101 val robIdx = UInt(log2Ceil(RobSize).W) 102 val instType = FuType() 103 val exceptType = ExceptionVec() 104 val ivaddr = UInt(VAddrBits.W) 105 val dvaddr = UInt(VAddrBits.W) // the l/s access address 106 val dpaddr = UInt(VAddrBits.W) // need the physical address when the TLB is valid 107 val tlbLatency = UInt(XLEN.W) // original requirements is L1toL2TlbLatency 108 // val levelTlbHit = UInt(2.W) // 01, 10, 11(memory) 109 // val otherPerfNoteThing // FIXME: how much? 110 val accessLatency = UInt(XLEN.W) // RS out time --> write back time 111 val executeLatency = UInt(XLEN.W) 112 val issueLatency = UInt(XLEN.W) 113 val lsInfo = new DebugLsInfo 114 val mdpInfo = new DebugMdpInfo 115} 116 117class RobPtr(implicit p: Parameters) extends CircularQueuePtr[RobPtr]( 118 p => p(XSCoreParamsKey).RobSize 119) with HasCircularQueuePtrHelper { 120 121 def needFlush(redirect: Valid[Redirect]): Bool = { 122 val flushItself = redirect.bits.flushItself() && this === redirect.bits.robIdx 123 redirect.valid && (flushItself || isAfter(this, redirect.bits.robIdx)) 124 } 125 126 def needFlush(redirect: Seq[Valid[Redirect]]): Bool = VecInit(redirect.map(needFlush)).asUInt.orR 127} 128 129object RobPtr { 130 def apply(f: Bool, v: UInt)(implicit p: Parameters): RobPtr = { 131 val ptr = Wire(new RobPtr) 132 ptr.flag := f 133 ptr.value := v 134 ptr 135 } 136} 137 138class RobCSRIO(implicit p: Parameters) extends XSBundle { 139 val intrBitSet = Input(Bool()) 140 val trapTarget = Input(UInt(VAddrBits.W)) 141 val isXRet = Input(Bool()) 142 val wfiEvent = Input(Bool()) 143 144 val fflags = Output(Valid(UInt(5.W))) 145 val vxsat = Output(Valid(UInt(1.W))) 146 val dirty_fs = Output(Bool()) 147 val perfinfo = new Bundle { 148 val retiredInstr = Output(UInt(3.W)) 149 } 150 151 val vcsrFlag = Output(Bool()) 152} 153 154class RobLsqIO(implicit p: Parameters) extends XSBundle { 155 val lcommit = Output(UInt(log2Up(CommitWidth + 1).W)) 156 val scommit = Output(UInt(log2Up(CommitWidth + 1).W)) 157 val pendingld = Output(Bool()) 158 val pendingst = Output(Bool()) 159 val commit = Output(Bool()) 160} 161 162class RobEnqIO(implicit p: Parameters) extends XSBundle { 163 val canAccept = Output(Bool()) 164 val isEmpty = Output(Bool()) 165 // valid vector, for robIdx gen and walk 166 val needAlloc = Vec(RenameWidth, Input(Bool())) 167 val req = Vec(RenameWidth, Flipped(ValidIO(new MicroOp))) 168 val resp = Vec(RenameWidth, Output(new RobPtr)) 169} 170 171class RobDispatchData(implicit p: Parameters) extends RobCommitInfo 172 173class RobDeqPtrWrapper(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper { 174 val io = IO(new Bundle { 175 // for commits/flush 176 val state = Input(UInt(2.W)) 177 val deq_v = Vec(CommitWidth, Input(Bool())) 178 val deq_w = Vec(CommitWidth, Input(Bool())) 179 val exception_state = Flipped(ValidIO(new RobExceptionInfo)) 180 // for flush: when exception occurs, reset deqPtrs to range(0, CommitWidth) 181 val intrBitSetReg = Input(Bool()) 182 val hasNoSpecExec = Input(Bool()) 183 val interrupt_safe = Input(Bool()) 184 val blockCommit = Input(Bool()) 185 // output: the CommitWidth deqPtr 186 val out = Vec(CommitWidth, Output(new RobPtr)) 187 val next_out = Vec(CommitWidth, Output(new RobPtr)) 188 }) 189 190 val deqPtrVec = RegInit(VecInit((0 until CommitWidth).map(_.U.asTypeOf(new RobPtr)))) 191 192 // for exceptions (flushPipe included) and interrupts: 193 // only consider the first instruction 194 val intrEnable = io.intrBitSetReg && !io.hasNoSpecExec && io.interrupt_safe 195 val exceptionEnable = io.deq_w(0) && io.exception_state.valid && io.exception_state.bits.not_commit && io.exception_state.bits.robIdx === deqPtrVec(0) 196 val redirectOutValid = io.state === 0.U && io.deq_v(0) && (intrEnable || exceptionEnable) 197 198 // for normal commits: only to consider when there're no exceptions 199 // we don't need to consider whether the first instruction has exceptions since it wil trigger exceptions. 200 val commit_exception = io.exception_state.valid && !isAfter(io.exception_state.bits.robIdx, deqPtrVec.last) 201 val canCommit = VecInit((0 until CommitWidth).map(i => io.deq_v(i) && io.deq_w(i))) 202 val normalCommitCnt = PriorityEncoder(canCommit.map(c => !c) :+ true.B) 203 // when io.intrBitSetReg or there're possible exceptions in these instructions, 204 // only one instruction is allowed to commit 205 val allowOnlyOne = commit_exception || io.intrBitSetReg 206 val commitCnt = Mux(allowOnlyOne, canCommit(0), normalCommitCnt) 207 208 val commitDeqPtrVec = VecInit(deqPtrVec.map(_ + commitCnt)) 209 val deqPtrVec_next = Mux(io.state === 0.U && !redirectOutValid && !io.blockCommit, commitDeqPtrVec, deqPtrVec) 210 211 deqPtrVec := deqPtrVec_next 212 213 io.next_out := deqPtrVec_next 214 io.out := deqPtrVec 215 216 when (io.state === 0.U) { 217 XSInfo(io.state === 0.U && commitCnt > 0.U, "retired %d insts\n", commitCnt) 218 } 219 220} 221 222class RobEnqPtrWrapper(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper { 223 val io = IO(new Bundle { 224 // for input redirect 225 val redirect = Input(Valid(new Redirect)) 226 // for enqueue 227 val allowEnqueue = Input(Bool()) 228 val hasBlockBackward = Input(Bool()) 229 val enq = Vec(RenameWidth, Input(Bool())) 230 val out = Output(Vec(RenameWidth, new RobPtr)) 231 }) 232 233 val enqPtrVec = RegInit(VecInit.tabulate(RenameWidth)(_.U.asTypeOf(new RobPtr))) 234 235 // enqueue 236 val canAccept = io.allowEnqueue && !io.hasBlockBackward 237 val dispatchNum = Mux(canAccept, PopCount(io.enq), 0.U) 238 239 for ((ptr, i) <- enqPtrVec.zipWithIndex) { 240 when(io.redirect.valid) { 241 ptr := Mux(io.redirect.bits.flushItself(), io.redirect.bits.robIdx + i.U, io.redirect.bits.robIdx + (i + 1).U) 242 }.otherwise { 243 ptr := ptr + dispatchNum 244 } 245 } 246 247 io.out := enqPtrVec 248 249} 250 251class RobExceptionInfo(implicit p: Parameters) extends XSBundle { 252 // val valid = Bool() 253 val robIdx = new RobPtr 254 val exceptionVec = ExceptionVec() 255 val flushPipe = Bool() 256 val isVset = Bool() 257 val replayInst = Bool() // redirect to that inst itself 258 val singleStep = Bool() // TODO add frontend hit beneath 259 val crossPageIPFFix = Bool() 260 val trigger = new TriggerCf 261 262// def trigger_before = !trigger.getTimingBackend && trigger.getHitBackend 263// def trigger_after = trigger.getTimingBackend && trigger.getHitBackend 264 def has_exception = exceptionVec.asUInt.orR || flushPipe || singleStep || replayInst || trigger.hit 265 def not_commit = exceptionVec.asUInt.orR || singleStep || replayInst || trigger.hit 266 // only exceptions are allowed to writeback when enqueue 267 def can_writeback = exceptionVec.asUInt.orR || singleStep || trigger.hit 268} 269 270class ExceptionGen(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper { 271 val io = IO(new Bundle { 272 val redirect = Input(Valid(new Redirect)) 273 val flush = Input(Bool()) 274 val enq = Vec(RenameWidth, Flipped(ValidIO(new RobExceptionInfo))) 275 val wb = Vec(1 + LoadPipelineWidth + StorePipelineWidth, Flipped(ValidIO(new RobExceptionInfo))) 276 val out = ValidIO(new RobExceptionInfo) 277 val state = ValidIO(new RobExceptionInfo) 278 }) 279 280 def getOldest(valid: Seq[Bool], bits: Seq[RobExceptionInfo]): (Seq[Bool], Seq[RobExceptionInfo]) = { 281 assert(valid.length == bits.length) 282 assert(isPow2(valid.length)) 283 if (valid.length == 1) { 284 (valid, bits) 285 } else if (valid.length == 2) { 286 val res = Seq.fill(2)(Wire(ValidIO(chiselTypeOf(bits(0))))) 287 for (i <- res.indices) { 288 res(i).valid := valid(i) 289 res(i).bits := bits(i) 290 } 291 val oldest = Mux(!valid(1) || valid(0) && isAfter(bits(1).robIdx, bits(0).robIdx), res(0), res(1)) 292 (Seq(oldest.valid), Seq(oldest.bits)) 293 } else { 294 val left = getOldest(valid.take(valid.length / 2), bits.take(valid.length / 2)) 295 val right = getOldest(valid.takeRight(valid.length / 2), bits.takeRight(valid.length / 2)) 296 getOldest(left._1 ++ right._1, left._2 ++ right._2) 297 } 298 } 299 300 val currentValid = RegInit(false.B) 301 val current = Reg(new RobExceptionInfo) 302 303 // orR the exceptionVec 304 val lastCycleFlush = RegNext(io.flush) 305 val in_enq_valid = VecInit(io.enq.map(e => e.valid && e.bits.has_exception && !lastCycleFlush)) 306 val in_wb_valid = io.wb.map(w => w.valid && w.bits.has_exception && !lastCycleFlush) 307 308 // s0: compare wb(1)~wb(LoadPipelineWidth) and wb(1 + LoadPipelineWidth)~wb(LoadPipelineWidth + StorePipelineWidth) 309 val wb_valid = in_wb_valid.zip(io.wb.map(_.bits)).map{ case (v, bits) => v && !(bits.robIdx.needFlush(io.redirect) || io.flush) } 310 val csr_wb_bits = io.wb(0).bits 311 val load_wb_bits = getOldest(in_wb_valid.slice(1, 1 + LoadPipelineWidth), io.wb.map(_.bits).slice(1, 1 + LoadPipelineWidth))._2(0) 312 val store_wb_bits = getOldest(in_wb_valid.slice(1 + LoadPipelineWidth, 1 + LoadPipelineWidth + StorePipelineWidth), io.wb.map(_.bits).slice(1 + LoadPipelineWidth, 1 + LoadPipelineWidth + StorePipelineWidth))._2(0) 313 val s0_out_valid = RegNext(VecInit(Seq(wb_valid(0), wb_valid.slice(1, 1 + LoadPipelineWidth).reduce(_ || _), wb_valid.slice(1 + LoadPipelineWidth, 1 + LoadPipelineWidth + StorePipelineWidth).reduce(_ || _)))) 314 val s0_out_bits = RegNext(VecInit(Seq(csr_wb_bits, load_wb_bits, store_wb_bits))) 315 316 // s1: compare last four and current flush 317 val s1_valid = VecInit(s0_out_valid.zip(s0_out_bits).map{ case (v, b) => v && !(b.robIdx.needFlush(io.redirect) || io.flush) }) 318 val compare_01_valid = s0_out_valid(0) || s0_out_valid(1) 319 val compare_01_bits = Mux(!s0_out_valid(0) || s0_out_valid(1) && isAfter(s0_out_bits(0).robIdx, s0_out_bits(1).robIdx), s0_out_bits(1), s0_out_bits(0)) 320 val compare_bits = Mux(!s0_out_valid(2) || compare_01_valid && isAfter(s0_out_bits(2).robIdx, compare_01_bits.robIdx), compare_01_bits, s0_out_bits(2)) 321 val s1_out_bits = RegNext(compare_bits) 322 val s1_out_valid = RegNext(s1_valid.asUInt.orR) 323 324 val enq_valid = RegNext(in_enq_valid.asUInt.orR && !io.redirect.valid && !io.flush) 325 val enq_bits = RegNext(ParallelPriorityMux(in_enq_valid, io.enq.map(_.bits))) 326 327 // s2: compare the input exception with the current one 328 // priorities: 329 // (1) system reset 330 // (2) current is valid: flush, remain, merge, update 331 // (3) current is not valid: s1 or enq 332 val current_flush = current.robIdx.needFlush(io.redirect) || io.flush 333 val s1_flush = s1_out_bits.robIdx.needFlush(io.redirect) || io.flush 334 when (currentValid) { 335 when (current_flush) { 336 currentValid := Mux(s1_flush, false.B, s1_out_valid) 337 } 338 when (s1_out_valid && !s1_flush) { 339 when (isAfter(current.robIdx, s1_out_bits.robIdx)) { 340 current := s1_out_bits 341 }.elsewhen (current.robIdx === s1_out_bits.robIdx) { 342 current.exceptionVec := (s1_out_bits.exceptionVec.asUInt | current.exceptionVec.asUInt).asTypeOf(ExceptionVec()) 343 current.flushPipe := s1_out_bits.flushPipe || current.flushPipe 344 current.replayInst := s1_out_bits.replayInst || current.replayInst 345 current.singleStep := s1_out_bits.singleStep || current.singleStep 346 current.trigger := (s1_out_bits.trigger.asUInt | current.trigger.asUInt).asTypeOf(new TriggerCf) 347 } 348 } 349 }.elsewhen (s1_out_valid && !s1_flush) { 350 currentValid := true.B 351 current := s1_out_bits 352 }.elsewhen (enq_valid && !(io.redirect.valid || io.flush)) { 353 currentValid := true.B 354 current := enq_bits 355 } 356 357 io.out.valid := s1_out_valid || enq_valid && enq_bits.can_writeback 358 io.out.bits := Mux(s1_out_valid, s1_out_bits, enq_bits) 359 io.state.valid := currentValid 360 io.state.bits := current 361 362} 363 364class RobFlushInfo(implicit p: Parameters) extends XSBundle { 365 val ftqIdx = new FtqPtr 366 val robIdx = new RobPtr 367 val ftqOffset = UInt(log2Up(PredictWidth).W) 368 val replayInst = Bool() 369} 370 371class Rob(implicit p: Parameters) extends LazyModule with HasWritebackSink with HasXSParameter { 372 373 lazy val module = new RobImp(this) 374 375 override def generateWritebackIO( 376 thisMod: Option[HasWritebackSource] = None, 377 thisModImp: Option[HasWritebackSourceImp] = None 378 ): Unit = { 379 val sources = writebackSinksImp(thisMod, thisModImp) 380 module.io.writeback.zip(sources).foreach(x => x._1 := x._2) 381 } 382} 383 384class RobImp(outer: Rob)(implicit p: Parameters) extends LazyModuleImp(outer) 385 with HasXSParameter with HasCircularQueuePtrHelper with HasPerfEvents { 386 val wbExuConfigs = outer.writebackSinksParams.map(_.exuConfigs) 387 val numWbPorts = wbExuConfigs.map(_.length) 388 389 val io = IO(new Bundle() { 390 val hartId = Input(UInt(8.W)) 391 val redirect = Input(Valid(new Redirect)) 392 val enq = new RobEnqIO 393 val flushOut = ValidIO(new Redirect) 394 val isVsetFlushPipe = Output(Bool()) 395 val exception = ValidIO(new ExceptionInfo) 396 // exu + brq 397 val writeback = MixedVec(numWbPorts.map(num => Vec(num, Flipped(ValidIO(new ExuOutput))))) 398 val commits = Output(new RobCommitIO) 399 val rabCommits = Output(new RobCommitIO) 400 val diffCommits = Output(new DiffCommitIO) 401 val lsq = new RobLsqIO 402 val robDeqPtr = Output(new RobPtr) 403 val csr = new RobCSRIO 404 val robFull = Output(Bool()) 405 val cpu_halt = Output(Bool()) 406 val wfi_enable = Input(Bool()) 407 val debug_ls = Flipped(new DebugLSIO) 408 }) 409 410 def selectWb(index: Int, func: Seq[ExuConfig] => Boolean): Seq[(Seq[ExuConfig], ValidIO[ExuOutput])] = { 411 wbExuConfigs(index).zip(io.writeback(index)).filter(x => func(x._1)) 412 } 413 val exeWbSel = outer.selWritebackSinks(_.exuConfigs.length) 414 val fflagsWbSel = outer.selWritebackSinks(_.exuConfigs.count(_.exists(_.writeFflags))) 415 val fflagsPorts = selectWb(fflagsWbSel, _.exists(_.writeFflags)) 416 val vxsatWbSel = outer.selWritebackSinks(_.exuConfigs.count(_.exists(_.writeVxsat))) 417 val vxsatPorts = selectWb(vxsatWbSel, _.exists(_.writeVxsat)) 418 val exceptionWbSel = outer.selWritebackSinks(_.exuConfigs.count(_.exists(_.needExceptionGen))) 419 val exceptionPorts = selectWb(exceptionWbSel, _.exists(_.needExceptionGen)) 420 val exuWbPorts = selectWb(exeWbSel, _.forall(_ != StdExeUnitCfg)) 421 val stdWbPorts = selectWb(exeWbSel, _.contains(StdExeUnitCfg)) 422 println(s"Rob: size $RobSize, numWbPorts: $numWbPorts, commitwidth: $CommitWidth") 423 println(s"exuPorts: ${exuWbPorts.map(_._1.map(_.name))}") 424 println(s"stdPorts: ${stdWbPorts.map(_._1.map(_.name))}") 425 println(s"fflags: ${fflagsPorts.map(_._1.map(_.name))}") 426 println(s"vxsat: ${vxsatPorts.map(_._1.map(_.name))}") 427 428 429 val exuWriteback = exuWbPorts.map(_._2) 430 val stdWriteback = stdWbPorts.map(_._2) 431 432 // instvalid field 433 val valid = RegInit(VecInit(Seq.fill(RobSize)(false.B))) 434 // writeback status 435 val writebackedCounter = Mem(RobSize, UInt(log2Up(MaxUopSize * 2).W)) 436 val realDestSize = Mem(RobSize, UInt(log2Up(MaxUopSize).W)) 437// val writebackedCounter = RegInit(VecInit(Seq.fill(RobSize)(0.U(4.W)))) 438// val realDestSize = RegInit(VecInit(Seq.fill(RobSize)(0.U(4.W)))) 439 440 def isWritebacked(ptr: UInt): Bool = { 441 !writebackedCounter(ptr).orR 442 } 443 444 // data for redirect, exception, etc. 445 val flagBkup = Mem(RobSize, Bool()) 446 // some instructions are not allowed to trigger interrupts 447 // They have side effects on the states of the processor before they write back 448 val interrupt_safe = Mem(RobSize, Bool()) 449 450 // data for debug 451 // Warn: debug_* prefix should not exist in generated verilog. 452 val debug_microOp = Mem(RobSize, new MicroOp) 453 val debug_exuData = Reg(Vec(RobSize, UInt(XLEN.W)))//for debug 454 val debug_exuDebug = Reg(Vec(RobSize, new DebugBundle))//for debug 455 val debug_lsInfo = RegInit(VecInit(Seq.fill(RobSize)(DebugLsInfo.init))) 456 457 // pointers 458 // For enqueue ptr, we don't duplicate it since only enqueue needs it. 459 val enqPtrVec = Wire(Vec(RenameWidth, new RobPtr)) 460 val deqPtrVec = Wire(Vec(CommitWidth, new RobPtr)) 461 462 val walkPtrVec = Reg(Vec(CommitWidth, new RobPtr)) 463 val allowEnqueue = RegInit(true.B) 464 465 val enqPtr = enqPtrVec.head 466 val deqPtr = deqPtrVec(0) 467 val walkPtr = walkPtrVec(0) 468 469 val isEmpty = enqPtr === deqPtr 470 val isReplaying = io.redirect.valid && RedirectLevel.flushItself(io.redirect.bits.level) 471 472 /** 473 * states of Rob 474 */ 475 val s_idle :: s_walk :: Nil = Enum(2) 476 val state = RegInit(s_idle) 477 478 /** 479 * Data Modules 480 * 481 * CommitDataModule: data from dispatch 482 * (1) read: commits/walk/exception 483 * (2) write: enqueue 484 * 485 * WritebackData: data from writeback 486 * (1) read: commits/walk/exception 487 * (2) write: write back from exe units 488 */ 489 val dispatchData = Module(new SyncDataModuleTemplate(new RobDispatchData, RobSize, CommitWidth, RenameWidth)) 490 val dispatchDataRead = dispatchData.io.rdata 491 492 val exceptionGen = Module(new ExceptionGen) 493 val exceptionDataRead = exceptionGen.io.state 494 val fflagsDataRead = Wire(Vec(CommitWidth, UInt(5.W))) 495 val vxsatDataRead = Wire(Vec(CommitWidth, UInt(1.W))) 496 497 io.robDeqPtr := deqPtr 498 499 val rab = Module(new RenameBuffer(RabSize)) 500 rab.io.redirectValid := io.redirect.valid 501 rab.io.req.zip(io.enq.req).map{ case(dest, src) => 502 dest.bits := src.bits 503 dest.valid := src.valid && io.enq.canAccept 504 } 505 506 val realDestSizeCandidates = (0 until CommitWidth).map(i => realDestSize(Mux(state === s_idle, deqPtrVec(i).value, walkPtrVec(i).value))) 507 val wbSizeSeq = io.commits.commitValid.zip(io.commits.walkValid).zip(realDestSizeCandidates).map{ case((commitValid, walkValid), realDestSize) => 508 Mux(io.commits.isCommit, Mux(commitValid, realDestSize, 0.U), Mux(walkValid, realDestSize, 0.U)) 509 } 510 val wbSizeSum = wbSizeSeq.reduce(_ + _) 511 rab.io.commitSize := wbSizeSum 512 rab.io.walkSize := wbSizeSum 513 514 io.rabCommits := rab.io.commits 515 io.diffCommits := rab.io.diffCommits 516 /** 517 * Enqueue (from dispatch) 518 */ 519 // special cases 520 val hasBlockBackward = RegInit(false.B) 521 val hasNoSpecExec = RegInit(false.B) 522 val doingSvinval = RegInit(false.B) 523 // When blockBackward instruction leaves Rob (commit or walk), hasBlockBackward should be set to false.B 524 // To reduce registers usage, for hasBlockBackward cases, we allow enqueue after ROB is empty. 525 when (isEmpty) { hasBlockBackward:= false.B } 526 // When any instruction commits, hasNoSpecExec should be set to false.B 527 when (io.commits.hasWalkInstr || io.commits.hasCommitInstr) { hasNoSpecExec:= false.B } 528 529 // The wait-for-interrupt (WFI) instruction waits in the ROB until an interrupt might need servicing. 530 // io.csr.wfiEvent will be asserted if the WFI can resume execution, and we change the state to s_wfi_idle. 531 // It does not affect how interrupts are serviced. Note that WFI is noSpecExec and it does not trigger interrupts. 532 val hasWFI = RegInit(false.B) 533 io.cpu_halt := hasWFI 534 // WFI Timeout: 2^20 = 1M cycles 535 val wfi_cycles = RegInit(0.U(20.W)) 536 when (hasWFI) { 537 wfi_cycles := wfi_cycles + 1.U 538 }.elsewhen (!hasWFI && RegNext(hasWFI)) { 539 wfi_cycles := 0.U 540 } 541 val wfi_timeout = wfi_cycles.andR 542 when (RegNext(RegNext(io.csr.wfiEvent)) || io.flushOut.valid || wfi_timeout) { 543 hasWFI := false.B 544 } 545 546 // inst allocate 547 val allocatePtrVec = VecInit((0 until RenameWidth).map(i => enqPtrVec(PopCount(io.enq.req.take(i).map(req => req.valid && req.bits.ctrl.firstUop))))) 548 io.enq.canAccept := allowEnqueue && !hasBlockBackward && rab.io.canEnq 549 io.enq.resp := allocatePtrVec 550 val canEnqueue = VecInit(io.enq.req.map(req => req.valid && req.bits.ctrl.firstUop && io.enq.canAccept)) 551 val timer = GTimer() 552 for (i <- 0 until RenameWidth) { 553 // we don't check whether io.redirect is valid here since redirect has higher priority 554 when (canEnqueue(i)) { 555 val enqUop = io.enq.req(i).bits 556 val enqIndex = allocatePtrVec(i).value 557 // store uop in data module and debug_microOp Vec 558 debug_microOp(enqIndex) := enqUop 559 debug_microOp(enqIndex).debugInfo.dispatchTime := timer 560 debug_microOp(enqIndex).debugInfo.enqRsTime := timer 561 debug_microOp(enqIndex).debugInfo.selectTime := timer 562 debug_microOp(enqIndex).debugInfo.issueTime := timer 563 debug_microOp(enqIndex).debugInfo.writebackTime := timer 564 debug_microOp(enqIndex).debugInfo.tlbFirstReqTime := timer 565 debug_microOp(enqIndex).debugInfo.tlbRespTime := timer 566 debug_lsInfo(enqIndex) := DebugLsInfo.init 567 when (enqUop.ctrl.blockBackward) { 568 hasBlockBackward := true.B 569 } 570 when (enqUop.ctrl.noSpecExec) { 571 hasNoSpecExec := true.B 572 } 573 val enqHasTriggerHit = io.enq.req(i).bits.cf.trigger.getHitFrontend 574 val enqHasException = ExceptionNO.selectFrontend(enqUop.cf.exceptionVec).asUInt.orR 575 // the begin instruction of Svinval enqs so mark doingSvinval as true to indicate this process 576 when(!enqHasTriggerHit && !enqHasException && FuType.isSvinvalBegin(enqUop.ctrl.fuType, enqUop.ctrl.fuOpType, enqUop.ctrl.flushPipe)) 577 { 578 doingSvinval := true.B 579 } 580 // the end instruction of Svinval enqs so clear doingSvinval 581 when(!enqHasTriggerHit && !enqHasException && FuType.isSvinvalEnd(enqUop.ctrl.fuType, enqUop.ctrl.fuOpType, enqUop.ctrl.flushPipe)) 582 { 583 doingSvinval := false.B 584 } 585 // when we are in the process of Svinval software code area , only Svinval.vma and end instruction of Svinval can appear 586 assert(!doingSvinval || (FuType.isSvinval(enqUop.ctrl.fuType, enqUop.ctrl.fuOpType, enqUop.ctrl.flushPipe) || 587 FuType.isSvinvalEnd(enqUop.ctrl.fuType, enqUop.ctrl.fuOpType, enqUop.ctrl.flushPipe))) 588 when (enqUop.ctrl.isWFI && !enqHasException && !enqHasTriggerHit) { 589 hasWFI := true.B 590 } 591 } 592 } 593 val dispatchNum = Mux(io.enq.canAccept, PopCount(io.enq.req.map(req => req.valid && req.bits.ctrl.firstUop)), 0.U) 594 io.enq.isEmpty := RegNext(isEmpty && !VecInit(io.enq.req.map(_.valid)).asUInt.orR) 595 596 when (!io.wfi_enable) { 597 hasWFI := false.B 598 } 599 // sel vsetvl's flush position 600 val vs_idle :: vs_waitVinstr :: vs_waitFlush :: Nil = Enum(3) 601 val vsetvlState = RegInit(vs_idle) 602 603 val firstVInstrFtqPtr = RegInit(0.U.asTypeOf(new FtqPtr)) 604 val firstVInstrFtqOffset = RegInit(0.U.asTypeOf(UInt(log2Up(PredictWidth).W))) 605 val firstVInstrRobIdx = RegInit(0.U.asTypeOf(new RobPtr)) 606 607 val enq0 = io.enq.req(0) 608 val enq0IsVset = FuType.isIntExu(enq0.bits.ctrl.fuType) && ALUOpType.isVset(enq0.bits.ctrl.fuOpType) && enq0.bits.ctrl.lastUop && canEnqueue(0) 609 val enq0IsVsetFlush = enq0IsVset && enq0.bits.ctrl.flushPipe 610 val enqIsVInstrVec = io.enq.req.zip(canEnqueue).map{case (req, fire) => FuType.isVecExu(req.bits.ctrl.fuType) && fire} 611 // for vs_idle 612 val firstVInstrIdle = PriorityMux(enqIsVInstrVec.zip(io.enq.req).drop(1) :+ (true.B, 0.U.asTypeOf(io.enq.req(0).cloneType))) 613 // for vs_waitVinstr 614 val enqIsVInstrOrVset = (enqIsVInstrVec(0) || enq0IsVset) +: enqIsVInstrVec.drop(1) 615 val firstVInstrWait = PriorityMux(enqIsVInstrOrVset, io.enq.req) 616 when(vsetvlState === vs_idle){ 617 firstVInstrFtqPtr := firstVInstrIdle.bits.cf.ftqPtr 618 firstVInstrFtqOffset := firstVInstrIdle.bits.cf.ftqOffset 619 firstVInstrRobIdx := firstVInstrIdle.bits.robIdx 620 }.elsewhen(vsetvlState === vs_waitVinstr){ 621 firstVInstrFtqPtr := firstVInstrWait.bits.cf.ftqPtr 622 firstVInstrFtqOffset := firstVInstrWait.bits.cf.ftqOffset 623 firstVInstrRobIdx := firstVInstrWait.bits.robIdx 624 } 625 626 val hasVInstrAfterI = Cat(enqIsVInstrVec.drop(1)).orR 627 when(vsetvlState === vs_idle){ 628 when(enq0IsVsetFlush){ 629 vsetvlState := Mux(hasVInstrAfterI, vs_waitFlush, vs_waitVinstr) 630 } 631 }.elsewhen(vsetvlState === vs_waitVinstr){ 632 when(io.redirect.valid){ 633 vsetvlState := vs_idle 634 }.elsewhen(Cat(enqIsVInstrOrVset).orR){ 635 vsetvlState := vs_waitFlush 636 } 637 }.elsewhen(vsetvlState === vs_waitFlush){ 638 when(io.redirect.valid){ 639 vsetvlState := vs_idle 640 } 641 } 642 643 /** 644 * Writeback (from execution units) 645 */ 646 for (wb <- exuWriteback) { 647 when (wb.valid) { 648 val wbIdx = wb.bits.uop.robIdx.value 649 debug_exuData(wbIdx) := wb.bits.data 650 debug_exuDebug(wbIdx) := wb.bits.debug 651 debug_microOp(wbIdx).debugInfo.enqRsTime := wb.bits.uop.debugInfo.enqRsTime 652 debug_microOp(wbIdx).debugInfo.selectTime := wb.bits.uop.debugInfo.selectTime 653 debug_microOp(wbIdx).debugInfo.issueTime := wb.bits.uop.debugInfo.issueTime 654 debug_microOp(wbIdx).debugInfo.writebackTime := wb.bits.uop.debugInfo.writebackTime 655 debug_microOp(wbIdx).debugInfo.tlbFirstReqTime := wb.bits.uop.debugInfo.tlbFirstReqTime 656 debug_microOp(wbIdx).debugInfo.tlbRespTime := wb.bits.uop.debugInfo.tlbRespTime 657 658 // debug for lqidx and sqidx 659 debug_microOp(wbIdx).lqIdx := wb.bits.uop.lqIdx 660 debug_microOp(wbIdx).sqIdx := wb.bits.uop.sqIdx 661 662 val debug_Uop = debug_microOp(wbIdx) 663 XSInfo(true.B, 664 p"writebacked pc 0x${Hexadecimal(debug_Uop.cf.pc)} wen ${debug_Uop.ctrl.rfWen} " + 665 p"data 0x${Hexadecimal(wb.bits.data)} ldst ${debug_Uop.ctrl.ldest} pdst ${debug_Uop.pdest} " + 666 p"skip ${wb.bits.debug.isMMIO} robIdx: ${wb.bits.uop.robIdx}\n" 667 ) 668 } 669 } 670 val writebackNum = PopCount(exuWriteback.map(_.valid)) 671 XSInfo(writebackNum =/= 0.U, "writebacked %d insts\n", writebackNum) 672 673 674 /** 675 * RedirectOut: Interrupt and Exceptions 676 */ 677 val deqDispatchData = dispatchDataRead(0) 678 val debug_deqUop = debug_microOp(deqPtr.value) 679 680 val intrBitSetReg = RegNext(io.csr.intrBitSet) 681 val intrEnable = intrBitSetReg && !hasNoSpecExec && interrupt_safe(deqPtr.value) 682 val deqHasExceptionOrFlush = exceptionDataRead.valid && exceptionDataRead.bits.robIdx === deqPtr 683 val deqHasException = deqHasExceptionOrFlush && (exceptionDataRead.bits.exceptionVec.asUInt.orR || 684 exceptionDataRead.bits.singleStep || exceptionDataRead.bits.trigger.hit) 685 val deqHasFlushPipe = deqHasExceptionOrFlush && exceptionDataRead.bits.flushPipe 686 val deqHasReplayInst = deqHasExceptionOrFlush && exceptionDataRead.bits.replayInst 687 val exceptionEnable = isWritebacked(deqPtr.value) && deqHasException 688 689 XSDebug(deqHasException && exceptionDataRead.bits.singleStep, "Debug Mode: Deq has singlestep exception\n") 690 XSDebug(deqHasException && exceptionDataRead.bits.trigger.getHitFrontend, "Debug Mode: Deq has frontend trigger exception\n") 691 XSDebug(deqHasException && exceptionDataRead.bits.trigger.getHitBackend, "Debug Mode: Deq has backend trigger exception\n") 692 693 val isFlushPipe = isWritebacked(deqPtr.value) && (deqHasFlushPipe || deqHasReplayInst) 694 695 val isVsetFlushPipe = isWritebacked(deqPtr.value) && deqHasFlushPipe && exceptionDataRead.bits.isVset 696 val needModifyFtqIdxOffset = isVsetFlushPipe && (vsetvlState === vs_waitFlush) 697 io.isVsetFlushPipe := RegNext(isVsetFlushPipe) 698 // io.flushOut will trigger redirect at the next cycle. 699 // Block any redirect or commit at the next cycle. 700 val lastCycleFlush = RegNext(io.flushOut.valid) 701 702 io.flushOut.valid := (state === s_idle) && valid(deqPtr.value) && (intrEnable || exceptionEnable || isFlushPipe) && !lastCycleFlush 703 io.flushOut.bits := DontCare 704 io.flushOut.bits.robIdx := Mux(needModifyFtqIdxOffset, firstVInstrRobIdx, deqPtr) 705 io.flushOut.bits.ftqIdx := Mux(needModifyFtqIdxOffset, firstVInstrFtqPtr, deqDispatchData.ftqIdx) 706 io.flushOut.bits.ftqOffset := Mux(needModifyFtqIdxOffset, firstVInstrFtqOffset, deqDispatchData.ftqOffset) 707 io.flushOut.bits.level := Mux(deqHasReplayInst || intrEnable || exceptionEnable || needModifyFtqIdxOffset, RedirectLevel.flush, RedirectLevel.flushAfter) // TODO use this to implement "exception next" 708 io.flushOut.bits.interrupt := true.B 709 XSPerfAccumulate("interrupt_num", io.flushOut.valid && intrEnable) 710 XSPerfAccumulate("exception_num", io.flushOut.valid && exceptionEnable) 711 XSPerfAccumulate("flush_pipe_num", io.flushOut.valid && isFlushPipe) 712 XSPerfAccumulate("replay_inst_num", io.flushOut.valid && isFlushPipe && deqHasReplayInst) 713 714 val exceptionHappen = (state === s_idle) && valid(deqPtr.value) && (intrEnable || exceptionEnable) && !lastCycleFlush 715 io.exception.valid := RegNext(exceptionHappen) 716 io.exception.bits.uop := RegEnable(debug_deqUop, exceptionHappen) 717 io.exception.bits.uop.ctrl.commitType := RegEnable(deqDispatchData.commitType, exceptionHappen) 718 io.exception.bits.uop.cf.exceptionVec := RegEnable(exceptionDataRead.bits.exceptionVec, exceptionHappen) 719 io.exception.bits.uop.ctrl.singleStep := RegEnable(exceptionDataRead.bits.singleStep, exceptionHappen) 720 io.exception.bits.uop.cf.crossPageIPFFix := RegEnable(exceptionDataRead.bits.crossPageIPFFix, exceptionHappen) 721 io.exception.bits.isInterrupt := RegEnable(intrEnable, exceptionHappen) 722 io.exception.bits.uop.cf.trigger := RegEnable(exceptionDataRead.bits.trigger, exceptionHappen) 723 724 XSDebug(io.flushOut.valid, 725 p"generate redirect: pc 0x${Hexadecimal(io.exception.bits.uop.cf.pc)} intr $intrEnable " + 726 p"excp $exceptionEnable flushPipe $isFlushPipe " + 727 p"Trap_target 0x${Hexadecimal(io.csr.trapTarget)} exceptionVec ${Binary(exceptionDataRead.bits.exceptionVec.asUInt)}\n") 728 729 730 /** 731 * Commits (and walk) 732 * They share the same width. 733 */ 734 val walkCounter = Reg(UInt(log2Up(RobSize + 1).W)) 735 val shouldWalkVec = VecInit((0 until CommitWidth).map(_.U < walkCounter)) 736 val walkFinished = walkCounter <= CommitWidth.U 737 rab.io.robWalkEnd := state === s_walk && walkFinished 738 require(RenameWidth <= CommitWidth) 739 740 // wiring to csr 741 val (wflags, fpWen) = (0 until CommitWidth).map(i => { 742 val v = io.commits.commitValid(i) 743 val info = io.commits.info(i) 744 (v & info.wflags, v & info.fpWen) 745 }).unzip 746 val fflags = Wire(Valid(UInt(5.W))) 747 fflags.valid := io.commits.isCommit && VecInit(wflags).asUInt.orR 748 fflags.bits := wflags.zip(fflagsDataRead).map({ 749 case (w, f) => Mux(w, f, 0.U) 750 }).reduce(_|_) 751 val vxsat = Wire(Valid(UInt(1.W))) 752 vxsat.valid := io.commits.isCommit && VecInit(wflags).asUInt.orR 753 vxsat.bits := wflags.zip(vxsatDataRead).map({ 754 case (w, f) => Mux(w, f, 0.U) 755 }).reduce(_|_) 756 val dirty_fs = io.commits.isCommit && VecInit(fpWen).asUInt.orR 757 758 // when mispredict branches writeback, stop commit in the next 2 cycles 759 // TODO: don't check all exu write back 760 val misPredWb = Cat(VecInit(exuWriteback.map(wb => 761 wb.bits.redirect.cfiUpdate.isMisPred && wb.bits.redirectValid 762 ))).orR 763 val misPredBlockCounter = Reg(UInt(3.W)) 764 misPredBlockCounter := Mux(misPredWb, 765 "b111".U, 766 misPredBlockCounter >> 1.U 767 ) 768 val misPredBlock = misPredBlockCounter(0) 769 val blockCommit = misPredBlock || isReplaying || lastCycleFlush || hasWFI 770 771 io.commits.isWalk := state === s_walk 772 io.commits.isCommit := state === s_idle && !blockCommit 773 val walk_v = VecInit(walkPtrVec.map(ptr => valid(ptr.value))) 774 val commit_v = VecInit(deqPtrVec.map(ptr => valid(ptr.value))) 775 // store will be commited iff both sta & std have been writebacked 776 val commit_w = VecInit(deqPtrVec.map(ptr => isWritebacked(ptr.value))) 777 val commit_exception = exceptionDataRead.valid && !isAfter(exceptionDataRead.bits.robIdx, deqPtrVec.last) 778 val commit_block = VecInit((0 until CommitWidth).map(i => !commit_w(i))) 779 val allowOnlyOneCommit = commit_exception || intrBitSetReg 780 // for instructions that may block others, we don't allow them to commit 781 for (i <- 0 until CommitWidth) { 782 // defaults: state === s_idle and instructions commit 783 // when intrBitSetReg, allow only one instruction to commit at each clock cycle 784 val isBlocked = if (i != 0) Cat(commit_block.take(i)).orR || allowOnlyOneCommit else intrEnable || deqHasException || deqHasReplayInst 785 io.commits.commitValid(i) := commit_v(i) && commit_w(i) && !isBlocked 786 io.commits.info(i) := dispatchDataRead(i) 787 788 when (state === s_walk) { 789 io.commits.walkValid(i) := shouldWalkVec(i) 790 when (io.commits.isWalk && state === s_walk && shouldWalkVec(i)) { 791 XSError(!walk_v(i), s"why not $i???\n") 792 } 793 } 794 795 XSInfo(io.commits.isCommit && io.commits.commitValid(i), 796 "retired pc %x wen %d ldest %d pdest %x old_pdest %x data %x fflags: %b vxsat: %b\n", 797 debug_microOp(deqPtrVec(i).value).cf.pc, 798 io.commits.info(i).rfWen, 799 io.commits.info(i).ldest, 800 io.commits.info(i).pdest, 801 io.commits.info(i).old_pdest, 802 debug_exuData(deqPtrVec(i).value), 803 fflagsDataRead(i), 804 vxsatDataRead(i) 805 ) 806 XSInfo(state === s_walk && io.commits.walkValid(i), "walked pc %x wen %d ldst %d data %x\n", 807 debug_microOp(walkPtrVec(i).value).cf.pc, 808 io.commits.info(i).rfWen, 809 io.commits.info(i).ldest, 810 debug_exuData(walkPtrVec(i).value) 811 ) 812 } 813 if (env.EnableDifftest) { 814 io.commits.info.map(info => dontTouch(info.pc)) 815 } 816 817 // sync fflags/dirty_fs/vxsat to csr 818 io.csr.fflags := RegNext(fflags) 819 io.csr.dirty_fs := RegNext(dirty_fs) 820 io.csr.vxsat := RegNext(vxsat) 821 822 // sync v csr to csr 823// io.csr.vcsrFlag := RegNext(isVsetFlushPipe) 824 825 // commit load/store to lsq 826 val ldCommitVec = VecInit((0 until CommitWidth).map(i => io.commits.commitValid(i) && io.commits.info(i).commitType === CommitType.LOAD)) 827 val stCommitVec = VecInit((0 until CommitWidth).map(i => io.commits.commitValid(i) && io.commits.info(i).commitType === CommitType.STORE)) 828 io.lsq.lcommit := RegNext(Mux(io.commits.isCommit, PopCount(ldCommitVec), 0.U)) 829 io.lsq.scommit := RegNext(Mux(io.commits.isCommit, PopCount(stCommitVec), 0.U)) 830 // indicate a pending load or store 831 io.lsq.pendingld := RegNext(io.commits.isCommit && io.commits.info(0).commitType === CommitType.LOAD && valid(deqPtr.value)) 832 io.lsq.pendingst := RegNext(io.commits.isCommit && io.commits.info(0).commitType === CommitType.STORE && valid(deqPtr.value)) 833 io.lsq.commit := RegNext(io.commits.isCommit && io.commits.commitValid(0)) 834 835 /** 836 * state changes 837 * (1) redirect: switch to s_walk 838 * (2) walk: when walking comes to the end, switch to s_idle 839 */ 840 val state_next = Mux(io.redirect.valid, s_walk, Mux(state === s_walk && walkFinished && rab.io.rabWalkEnd, s_idle, state)) 841 XSPerfAccumulate("s_idle_to_idle", state === s_idle && state_next === s_idle) 842 XSPerfAccumulate("s_idle_to_walk", state === s_idle && state_next === s_walk) 843 XSPerfAccumulate("s_walk_to_idle", state === s_walk && state_next === s_idle) 844 XSPerfAccumulate("s_walk_to_walk", state === s_walk && state_next === s_walk) 845 state := state_next 846 847 /** 848 * pointers and counters 849 */ 850 val deqPtrGenModule = Module(new RobDeqPtrWrapper) 851 deqPtrGenModule.io.state := state 852 deqPtrGenModule.io.deq_v := commit_v 853 deqPtrGenModule.io.deq_w := commit_w 854 deqPtrGenModule.io.exception_state := exceptionDataRead 855 deqPtrGenModule.io.intrBitSetReg := intrBitSetReg 856 deqPtrGenModule.io.hasNoSpecExec := hasNoSpecExec 857 deqPtrGenModule.io.interrupt_safe := interrupt_safe(deqPtr.value) 858 deqPtrGenModule.io.blockCommit := blockCommit 859 deqPtrVec := deqPtrGenModule.io.out 860 val deqPtrVec_next = deqPtrGenModule.io.next_out 861 862 val enqPtrGenModule = Module(new RobEnqPtrWrapper) 863 enqPtrGenModule.io.redirect := io.redirect 864 enqPtrGenModule.io.allowEnqueue := allowEnqueue 865 enqPtrGenModule.io.hasBlockBackward := hasBlockBackward 866 enqPtrGenModule.io.enq := VecInit(io.enq.req.map(req => req.valid && req.bits.ctrl.firstUop)) 867 enqPtrVec := enqPtrGenModule.io.out 868 869 val thisCycleWalkCount = Mux(walkFinished, walkCounter, CommitWidth.U) 870 // next walkPtrVec: 871 // (1) redirect occurs: update according to state 872 // (2) walk: move forwards 873 val walkPtrVec_next = Mux(io.redirect.valid, 874 deqPtrVec_next, 875 Mux(state === s_walk, VecInit(walkPtrVec.map(_ + CommitWidth.U)), walkPtrVec) 876 ) 877 walkPtrVec := walkPtrVec_next 878 879 val numValidEntries = distanceBetween(enqPtr, deqPtr) 880 val commitCnt = PopCount(io.commits.commitValid) 881 882 allowEnqueue := numValidEntries + dispatchNum <= (RobSize - RenameWidth).U 883 884 val currentWalkPtr = Mux(state === s_walk, walkPtr, deqPtrVec_next(0)) 885 val redirectWalkDistance = distanceBetween(io.redirect.bits.robIdx, deqPtrVec_next(0)) 886 when (io.redirect.valid) { 887 // full condition: 888 // +& is used here because: 889 // When rob is full and the tail instruction causes a misprediction, 890 // the redirect robIdx is the deqPtr - 1. In this case, redirectWalkDistance 891 // is RobSize - 1. 892 // Since misprediction does not flush the instruction itself, flushItSelf is false.B. 893 // Previously we use `+` to count the walk distance and it causes overflows 894 // when RobSize is power of 2. We change it to `+&` to allow walkCounter to be RobSize. 895 // The width of walkCounter also needs to be changed. 896 // empty condition: 897 // When the last instruction in ROB commits and causes a flush, a redirect 898 // will be raised later. In such circumstances, the redirect robIdx is before 899 // the deqPtrVec_next(0) and will cause underflow. 900 walkCounter := Mux(isBefore(io.redirect.bits.robIdx, deqPtrVec_next(0)), 0.U, 901 redirectWalkDistance +& !io.redirect.bits.flushItself()) 902 }.elsewhen (state === s_walk) { 903 walkCounter := walkCounter - thisCycleWalkCount 904 XSInfo(p"rolling back: $enqPtr $deqPtr walk $walkPtr walkcnt $walkCounter\n") 905 } 906 907 908 /** 909 * States 910 * We put all the stage bits changes here. 911 912 * All events: (1) enqueue (dispatch); (2) writeback; (3) cancel; (4) dequeue (commit); 913 * All states: (1) valid; (2) writebacked; (3) flagBkup 914 */ 915 val commitReadAddr = Mux(state === s_idle, VecInit(deqPtrVec.map(_.value)), VecInit(walkPtrVec.map(_.value))) 916 917 // redirect logic writes 6 valid 918 val redirectHeadVec = Reg(Vec(RenameWidth, new RobPtr)) 919 val redirectTail = Reg(new RobPtr) 920 val redirectIdle :: redirectBusy :: Nil = Enum(2) 921 val redirectState = RegInit(redirectIdle) 922 val invMask = redirectHeadVec.map(redirectHead => isBefore(redirectHead, redirectTail)) 923 when(redirectState === redirectBusy) { 924 redirectHeadVec.foreach(redirectHead => redirectHead := redirectHead + RenameWidth.U) 925 redirectHeadVec zip invMask foreach { 926 case (redirectHead, inv) => when(inv) { 927 valid(redirectHead.value) := false.B 928 } 929 } 930 when(!invMask.last) { 931 redirectState := redirectIdle 932 } 933 } 934 when(io.redirect.valid) { 935 redirectState := redirectBusy 936 when(redirectState === redirectIdle) { 937 redirectTail := enqPtr 938 } 939 redirectHeadVec.zipWithIndex.foreach { case (redirectHead, i) => 940 redirectHead := Mux(io.redirect.bits.flushItself(), io.redirect.bits.robIdx + i.U, io.redirect.bits.robIdx + (i + 1).U) 941 } 942 } 943 // enqueue logic writes 6 valid 944 for (i <- 0 until RenameWidth) { 945 when (canEnqueue(i) && !io.redirect.valid) { 946 valid(allocatePtrVec(i).value) := true.B 947 } 948 } 949 // dequeue logic writes 6 valid 950 for (i <- 0 until CommitWidth) { 951 val commitValid = io.commits.isCommit && io.commits.commitValid(i) 952 when (commitValid) { 953 valid(commitReadAddr(i)) := false.B 954 } 955 } 956 957 // debug_inst update 958 for(i <- 0 until (exuParameters.LduCnt + exuParameters.StuCnt)) { 959 debug_lsInfo(io.debug_ls.debugLsInfo(i).s1_robIdx).s1SignalEnable(io.debug_ls.debugLsInfo(i)) 960 debug_lsInfo(io.debug_ls.debugLsInfo(i).s2_robIdx).s2SignalEnable(io.debug_ls.debugLsInfo(i)) 961 } 962 963 // writeback logic set numWbPorts writebacked to true 964 val blockWbSeq = Wire(Vec(exuWriteback.length, Bool())) 965 blockWbSeq.map(_ := false.B) 966 for (((wb, cfgs), blockWb) <- exuWriteback.zip(wbExuConfigs(exeWbSel)).zip(blockWbSeq)) { 967 when(wb.valid) { 968 val wbHasException = ExceptionNO.selectByExu(wb.bits.uop.cf.exceptionVec, cfgs).asUInt.orR 969 val wbHasTriggerHit = wb.bits.uop.cf.trigger.getHitBackend 970 val wbHasFlushPipe = cfgs.exists(_.flushPipe).B && wb.bits.uop.ctrl.flushPipe 971 val wbHasReplayInst = cfgs.exists(_.replayInst).B && wb.bits.uop.ctrl.replayInst 972 blockWb := wbHasException || wbHasFlushPipe || wbHasReplayInst || wbHasTriggerHit 973 } 974 } 975 976 // if the first uop of an instruction is valid , write writebackedCounter 977 val uopEnqValidSeq = io.enq.req.map(req => io.enq.canAccept && req.valid) 978 val instEnqValidSeq = io.enq.req.map (req => io.enq.canAccept && req.valid && req.bits.ctrl.firstUop) 979 val enqNeedWriteRFSeq = io.enq.req.map(_.bits.ctrl.needWriteRf) 980 val enqRobIdxSeq = io.enq.req.map(req => req.bits.robIdx.value) 981 982 val enqWbSizeSeq = io.enq.req.map { req => 983 val enqHasException = ExceptionNO.selectFrontend(req.bits.cf.exceptionVec).asUInt.orR 984 val enqHasTriggerHit = req.bits.cf.trigger.getHitFrontend 985 Mux(req.bits.eliminatedMove, Mux(enqHasException || enqHasTriggerHit, 1.U, 0.U), 986 Mux(FuType.isMemExu(req.bits.ctrl.fuType) && FuType.isAMO(req.bits.ctrl.fuType), 3.U, 987 Mux(FuType.isStoreExu(req.bits.ctrl.fuType), 2.U, 1.U))) 988 } 989 val enqWbSizeSumSeq = enqRobIdxSeq.zipWithIndex.map { case (robIdx, idx) => 990 val addend = enqRobIdxSeq.zip(enqWbSizeSeq).take(idx + 1).map { case (uopRobIdx, uopWbSize) => Mux(robIdx === uopRobIdx, uopWbSize, 0.U) } 991 addend.reduce(_ +& _) 992 } 993 for(i <- 0 until RobSize){ 994 995 val robIdxMatchSeq = io.enq.req.map(_.bits.robIdx.value === i.U) 996 val uopCanEnqSeq = uopEnqValidSeq.zip(robIdxMatchSeq).map{ case(valid, isMatch) => valid && isMatch } 997 val instCanEnqSeq = instEnqValidSeq.zip(robIdxMatchSeq).map{ case(valid, isMatch) => valid && isMatch } 998 val instCanEnqFlag = Cat(instCanEnqSeq).orR 999 1000 realDestSize(i) := Mux(!valid(i) && instCanEnqFlag || valid(i), realDestSize(i) + PopCount(enqNeedWriteRFSeq.zip(uopCanEnqSeq).map{ case(writeFlag, valid) => writeFlag && valid }), 0.U) 1001 1002 1003 val enqCnt = ParallelPriorityMux(uopCanEnqSeq.reverse :+ true.B, enqWbSizeSumSeq.reverse :+ 0.U) 1004 1005 val canWbSeq = exuWriteback.map(writeback => writeback.valid && writeback.bits.uop.robIdx.value === i.U) 1006 val canWbNoBlockSeq = canWbSeq.zip(blockWbSeq).map{ case(canWb, blockWb) => canWb && !blockWb } 1007 val canStuWbSeq = stdWriteback.map(writeback => writeback.valid && writeback.bits.uop.robIdx.value === i.U) 1008 val wbCnt = PopCount(canWbNoBlockSeq ++ canStuWbSeq) 1009 1010 writebackedCounter(i) := Mux(!valid(i) && instCanEnqFlag || valid(i), Mux(exceptionGen.io.out.valid && exceptionGen.io.out.bits.robIdx.value === i.U, 0.U, writebackedCounter(i) + enqCnt - wbCnt), 0.U) 1011 } 1012 1013 // flagBkup 1014 // enqueue logic set 6 flagBkup at most 1015 for (i <- 0 until RenameWidth) { 1016 when (canEnqueue(i)) { 1017 flagBkup(allocatePtrVec(i).value) := allocatePtrVec(i).flag 1018 } 1019 } 1020 1021 // interrupt_safe 1022 for (i <- 0 until RenameWidth) { 1023 // We RegNext the updates for better timing. 1024 // Note that instructions won't change the system's states in this cycle. 1025 when (RegNext(canEnqueue(i))) { 1026 // For now, we allow non-load-store instructions to trigger interrupts 1027 // For MMIO instructions, they should not trigger interrupts since they may 1028 // be sent to lower level before it writes back. 1029 // However, we cannot determine whether a load/store instruction is MMIO. 1030 // Thus, we don't allow load/store instructions to trigger an interrupt. 1031 // TODO: support non-MMIO load-store instructions to trigger interrupts 1032 val allow_interrupts = !CommitType.isLoadStore(io.enq.req(i).bits.ctrl.commitType) 1033 interrupt_safe(RegNext(allocatePtrVec(i).value)) := RegNext(allow_interrupts) 1034 } 1035 } 1036 1037 /** 1038 * read and write of data modules 1039 */ 1040 val commitReadAddr_next = Mux(state_next === s_idle, 1041 VecInit(deqPtrVec_next.map(_.value)), 1042 VecInit(walkPtrVec_next.map(_.value)) 1043 ) 1044 // NOTE: dispatch info will record the uop of inst 1045 dispatchData.io.wen := canEnqueue 1046 dispatchData.io.waddr := allocatePtrVec.map(_.value) 1047 dispatchData.io.wdata.zip(io.enq.req.map(_.bits)).foreach{ case (wdata, req) => 1048 wdata.ldest := req.ctrl.ldest 1049 wdata.rfWen := req.ctrl.rfWen 1050 wdata.fpWen := req.ctrl.fpWen 1051 wdata.vecWen := req.ctrl.vecWen 1052 wdata.wflags := req.ctrl.fpu.wflags 1053 wdata.commitType := req.ctrl.commitType 1054 wdata.pdest := req.pdest 1055 wdata.old_pdest := req.old_pdest 1056 wdata.ftqIdx := req.cf.ftqPtr 1057 wdata.ftqOffset := req.cf.ftqOffset 1058 wdata.isMove := req.eliminatedMove 1059 wdata.pc := req.cf.pc 1060 wdata.uopIdx := req.ctrl.uopIdx 1061 wdata.vconfig := req.ctrl.vconfig 1062 } 1063 dispatchData.io.raddr := commitReadAddr_next 1064 1065 exceptionGen.io.redirect <> io.redirect 1066 exceptionGen.io.flush := io.flushOut.valid 1067 for (i <- 0 until RenameWidth) { 1068 exceptionGen.io.enq(i).valid := canEnqueue(i) 1069 exceptionGen.io.enq(i).bits.robIdx := io.enq.req(i).bits.robIdx 1070 exceptionGen.io.enq(i).bits.exceptionVec := ExceptionNO.selectFrontend(io.enq.req(i).bits.cf.exceptionVec) 1071 exceptionGen.io.enq(i).bits.flushPipe := io.enq.req(i).bits.ctrl.flushPipe 1072 exceptionGen.io.enq(i).bits.isVset := FuType.isIntExu(io.enq.req(i).bits.ctrl.fuType) && ALUOpType.isVset(io.enq.req(i).bits.ctrl.fuOpType) 1073 exceptionGen.io.enq(i).bits.replayInst := false.B 1074 XSError(canEnqueue(i) && io.enq.req(i).bits.ctrl.replayInst, "enq should not set replayInst") 1075 exceptionGen.io.enq(i).bits.singleStep := io.enq.req(i).bits.ctrl.singleStep 1076 exceptionGen.io.enq(i).bits.crossPageIPFFix := io.enq.req(i).bits.cf.crossPageIPFFix 1077 exceptionGen.io.enq(i).bits.trigger.clear() 1078 exceptionGen.io.enq(i).bits.trigger.frontendHit := io.enq.req(i).bits.cf.trigger.frontendHit 1079 } 1080 1081 println(s"ExceptionGen:") 1082 val exceptionCases = exceptionPorts.map(_._1.flatMap(_.exceptionOut).distinct.sorted) 1083 require(exceptionCases.length == exceptionGen.io.wb.length) 1084 for ((((configs, wb), exc_wb), i) <- exceptionPorts.zip(exceptionGen.io.wb).zipWithIndex) { 1085 exc_wb.valid := wb.valid 1086 exc_wb.bits.robIdx := wb.bits.uop.robIdx 1087 exc_wb.bits.exceptionVec := ExceptionNO.selectByExu(wb.bits.uop.cf.exceptionVec, configs) 1088 exc_wb.bits.flushPipe := configs.exists(_.flushPipe).B && wb.bits.uop.ctrl.flushPipe 1089 exc_wb.bits.isVset := false.B 1090 exc_wb.bits.replayInst := configs.exists(_.replayInst).B && wb.bits.uop.ctrl.replayInst 1091 exc_wb.bits.singleStep := false.B 1092 exc_wb.bits.crossPageIPFFix := false.B 1093 // TODO: make trigger configurable 1094 exc_wb.bits.trigger.clear() 1095 exc_wb.bits.trigger.backendHit := wb.bits.uop.cf.trigger.backendHit 1096 println(s" [$i] ${configs.map(_.name)}: exception ${exceptionCases(i)}, " + 1097 s"flushPipe ${configs.exists(_.flushPipe)}, " + 1098 s"replayInst ${configs.exists(_.replayInst)}") 1099 } 1100 1101 val fflags_wb = fflagsPorts.map(_._2) 1102 val fflagsDataModule = Module(new SyncDataModuleTemplate( 1103 UInt(5.W), RobSize, CommitWidth, fflags_wb.size) 1104 ) 1105 for(i <- fflags_wb.indices){ 1106 fflagsDataModule.io.wen (i) := fflags_wb(i).valid 1107 fflagsDataModule.io.waddr(i) := fflags_wb(i).bits.uop.robIdx.value 1108 fflagsDataModule.io.wdata(i) := fflags_wb(i).bits.fflags 1109 } 1110 fflagsDataModule.io.raddr := VecInit(deqPtrVec_next.map(_.value)) 1111 fflagsDataRead := fflagsDataModule.io.rdata 1112 1113 val vxsat_wb = vxsatPorts.map(_._2) 1114 val vxsatDataModule = Module(new SyncDataModuleTemplate( 1115 UInt(1.W), RobSize, CommitWidth, vxsat_wb.size) 1116 ) 1117 for(i <- vxsat_wb.indices){ 1118 vxsatDataModule.io.wen (i) := vxsat_wb(i).valid 1119 vxsatDataModule.io.waddr(i) := vxsat_wb(i).bits.uop.robIdx.value 1120 vxsatDataModule.io.wdata(i) := vxsat_wb(i).bits.vxsat 1121 } 1122 vxsatDataModule.io.raddr := VecInit(deqPtrVec_next.map(_.value)) 1123 vxsatDataRead := vxsatDataModule.io.rdata 1124 1125 val instrCntReg = RegInit(0.U(64.W)) 1126 val fuseCommitCnt = PopCount(io.commits.commitValid.zip(io.commits.info).map{ case (v, i) => RegNext(v && CommitType.isFused(i.commitType)) }) 1127 val trueCommitCnt = RegNext(commitCnt) +& fuseCommitCnt 1128 val retireCounter = Mux(RegNext(io.commits.isCommit), trueCommitCnt, 0.U) 1129 val instrCnt = instrCntReg + retireCounter 1130 instrCntReg := instrCnt 1131 io.csr.perfinfo.retiredInstr := retireCounter 1132 io.robFull := !allowEnqueue 1133 1134 /** 1135 * debug info 1136 */ 1137 XSDebug(p"enqPtr ${enqPtr} deqPtr ${deqPtr}\n") 1138 XSDebug("") 1139 for(i <- 0 until RobSize){ 1140 XSDebug(false, !valid(i), "-") 1141// XSDebug(false, valid(i) && writebacked(i), "w") 1142// XSDebug(false, valid(i) && !writebacked(i), "v") 1143 } 1144 XSDebug(false, true.B, "\n") 1145 1146 for(i <- 0 until RobSize) { 1147 if(i % 4 == 0) XSDebug("") 1148 XSDebug(false, true.B, "%x ", debug_microOp(i).cf.pc) 1149 XSDebug(false, !valid(i), "- ") 1150// XSDebug(false, valid(i) && writebacked(i), "w ") 1151// XSDebug(false, valid(i) && !writebacked(i), "v ") 1152 if(i % 4 == 3) XSDebug(false, true.B, "\n") 1153 } 1154 1155 def ifCommit(counter: UInt): UInt = Mux(io.commits.isCommit, counter, 0.U) 1156 def ifCommitReg(counter: UInt): UInt = Mux(RegNext(io.commits.isCommit), counter, 0.U) 1157 1158 val commitDebugExu = deqPtrVec.map(_.value).map(debug_exuDebug(_)) 1159 val commitDebugUop = deqPtrVec.map(_.value).map(debug_microOp(_)) 1160 val commitDebugLsInfo = deqPtrVec.map(_.value).map(debug_lsInfo(_)) 1161 XSPerfAccumulate("clock_cycle", 1.U) 1162 QueuePerf(RobSize, PopCount((0 until RobSize).map(valid(_))), !allowEnqueue) 1163 XSPerfAccumulate("commitUop", ifCommit(commitCnt)) 1164 XSPerfAccumulate("commitInstr", ifCommitReg(trueCommitCnt)) 1165 val commitIsMove = commitDebugUop.map(_.ctrl.isMove) 1166 XSPerfAccumulate("commitInstrMove", ifCommit(PopCount(io.commits.commitValid.zip(commitIsMove).map{ case (v, m) => v && m }))) 1167 val commitMoveElim = commitDebugUop.map(_.debugInfo.eliminatedMove) 1168 XSPerfAccumulate("commitInstrMoveElim", ifCommit(PopCount(io.commits.commitValid zip commitMoveElim map { case (v, e) => v && e }))) 1169 XSPerfAccumulate("commitInstrFused", ifCommitReg(fuseCommitCnt)) 1170 val commitIsLoad = io.commits.info.map(_.commitType).map(_ === CommitType.LOAD) 1171 val commitLoadValid = io.commits.commitValid.zip(commitIsLoad).map{ case (v, t) => v && t } 1172 XSPerfAccumulate("commitInstrLoad", ifCommit(PopCount(commitLoadValid))) 1173 val commitIsBranch = io.commits.info.map(_.commitType).map(_ === CommitType.BRANCH) 1174 val commitBranchValid = io.commits.commitValid.zip(commitIsBranch).map{ case (v, t) => v && t } 1175 XSPerfAccumulate("commitInstrBranch", ifCommit(PopCount(commitBranchValid))) 1176 val commitLoadWaitBit = commitDebugUop.map(_.cf.loadWaitBit) 1177 XSPerfAccumulate("commitInstrLoadWait", ifCommit(PopCount(commitLoadValid.zip(commitLoadWaitBit).map{ case (v, w) => v && w }))) 1178 val commitIsStore = io.commits.info.map(_.commitType).map(_ === CommitType.STORE) 1179 XSPerfAccumulate("commitInstrStore", ifCommit(PopCount(io.commits.commitValid.zip(commitIsStore).map{ case (v, t) => v && t }))) 1180// XSPerfAccumulate("writeback", PopCount((0 until RobSize).map(i => valid(i) && writebacked(i)))) 1181 // XSPerfAccumulate("enqInstr", PopCount(io.dp1Req.map(_.fire))) 1182 // XSPerfAccumulate("d2rVnR", PopCount(io.dp1Req.map(p => p.valid && !p.ready))) 1183 XSPerfAccumulate("walkInstr", Mux(io.commits.isWalk, PopCount(io.commits.walkValid), 0.U)) 1184 XSPerfAccumulate("walkCycle", state === s_walk) 1185// val deqNotWritebacked = valid(deqPtr.value) && !writebacked(deqPtr.value) 1186 val deqUopCommitType = io.commits.info(0).commitType 1187// XSPerfAccumulate("waitNormalCycle", deqNotWritebacked && deqUopCommitType === CommitType.NORMAL) 1188// XSPerfAccumulate("waitBranchCycle", deqNotWritebacked && deqUopCommitType === CommitType.BRANCH) 1189// XSPerfAccumulate("waitLoadCycle", deqNotWritebacked && deqUopCommitType === CommitType.LOAD) 1190// XSPerfAccumulate("waitStoreCycle", deqNotWritebacked && deqUopCommitType === CommitType.STORE) 1191 XSPerfAccumulate("robHeadPC", io.commits.info(0).pc) 1192 val dispatchLatency = commitDebugUop.map(uop => uop.debugInfo.dispatchTime - uop.debugInfo.renameTime) 1193 val enqRsLatency = commitDebugUop.map(uop => uop.debugInfo.enqRsTime - uop.debugInfo.dispatchTime) 1194 val selectLatency = commitDebugUop.map(uop => uop.debugInfo.selectTime - uop.debugInfo.enqRsTime) 1195 val issueLatency = commitDebugUop.map(uop => uop.debugInfo.issueTime - uop.debugInfo.selectTime) 1196 val executeLatency = commitDebugUop.map(uop => uop.debugInfo.writebackTime - uop.debugInfo.issueTime) 1197 val rsFuLatency = commitDebugUop.map(uop => uop.debugInfo.writebackTime - uop.debugInfo.enqRsTime) 1198 val commitLatency = commitDebugUop.map(uop => timer - uop.debugInfo.writebackTime) 1199 val accessLatency = commitDebugUop.map(uop => uop.debugInfo.writebackTime - uop.debugInfo.issueTime) 1200 val tlbLatency = commitDebugUop.map(uop => uop.debugInfo.tlbRespTime - uop.debugInfo.tlbFirstReqTime) 1201 def latencySum(cond: Seq[Bool], latency: Seq[UInt]): UInt = { 1202 cond.zip(latency).map(x => Mux(x._1, x._2, 0.U)).reduce(_ +& _) 1203 } 1204 for (fuType <- FuType.functionNameMap.keys) { 1205 val fuName = FuType.functionNameMap(fuType) 1206 val commitIsFuType = io.commits.commitValid.zip(commitDebugUop).map(x => x._1 && x._2.ctrl.fuType === fuType.U ) 1207 XSPerfAccumulate(s"${fuName}_instr_cnt", ifCommit(PopCount(commitIsFuType))) 1208 XSPerfAccumulate(s"${fuName}_latency_dispatch", ifCommit(latencySum(commitIsFuType, dispatchLatency))) 1209 XSPerfAccumulate(s"${fuName}_latency_enq_rs", ifCommit(latencySum(commitIsFuType, enqRsLatency))) 1210 XSPerfAccumulate(s"${fuName}_latency_select", ifCommit(latencySum(commitIsFuType, selectLatency))) 1211 XSPerfAccumulate(s"${fuName}_latency_issue", ifCommit(latencySum(commitIsFuType, issueLatency))) 1212 XSPerfAccumulate(s"${fuName}_latency_execute", ifCommit(latencySum(commitIsFuType, executeLatency))) 1213 XSPerfAccumulate(s"${fuName}_latency_enq_rs_execute", ifCommit(latencySum(commitIsFuType, rsFuLatency))) 1214 XSPerfAccumulate(s"${fuName}_latency_commit", ifCommit(latencySum(commitIsFuType, commitLatency))) 1215 if (fuType == FuType.fmac.litValue) { 1216 val commitIsFma = commitIsFuType.zip(commitDebugUop).map(x => x._1 && x._2.ctrl.fpu.ren3 ) 1217 XSPerfAccumulate(s"${fuName}_instr_cnt_fma", ifCommit(PopCount(commitIsFma))) 1218 XSPerfAccumulate(s"${fuName}_latency_enq_rs_execute_fma", ifCommit(latencySum(commitIsFma, rsFuLatency))) 1219 XSPerfAccumulate(s"${fuName}_latency_execute_fma", ifCommit(latencySum(commitIsFma, executeLatency))) 1220 } 1221 } 1222 1223 /** 1224 * DataBase info: 1225 * log trigger is at writeback valid 1226 * */ 1227 if(!env.FPGAPlatform){ 1228 val instTableName = "InstDB" + p(XSCoreParamsKey).HartId.toString 1229 val instSiteName = "Rob" + p(XSCoreParamsKey).HartId.toString 1230 val debug_instTable = ChiselDB.createTable(instTableName, new DebugInstDB) 1231 // FIXME lyq: only get inst (alu, bj, ls) in exuWriteback 1232 for (wb <- exuWriteback) { 1233 when(wb.valid) { 1234 val debug_instData = Wire(new DebugInstDB) 1235 val idx = wb.bits.uop.robIdx.value 1236 debug_instData.globalID := wb.bits.uop.ctrl.debug_globalID 1237 debug_instData.robIdx := idx 1238 debug_instData.instType := wb.bits.uop.ctrl.fuType 1239 debug_instData.ivaddr := wb.bits.uop.cf.pc 1240 debug_instData.dvaddr := wb.bits.debug.vaddr 1241 debug_instData.dpaddr := wb.bits.debug.paddr 1242 debug_instData.tlbLatency := wb.bits.uop.debugInfo.tlbRespTime - wb.bits.uop.debugInfo.tlbFirstReqTime 1243 debug_instData.accessLatency := wb.bits.uop.debugInfo.writebackTime - wb.bits.uop.debugInfo.issueTime 1244 debug_instData.executeLatency := wb.bits.uop.debugInfo.writebackTime - wb.bits.uop.debugInfo.issueTime 1245 debug_instData.issueLatency := wb.bits.uop.debugInfo.issueTime - wb.bits.uop.debugInfo.selectTime 1246 debug_instData.exceptType := wb.bits.uop.cf.exceptionVec 1247 debug_instData.lsInfo := debug_lsInfo(idx) 1248 debug_instData.mdpInfo.ssid := wb.bits.uop.cf.ssid 1249 debug_instData.mdpInfo.waitAllStore := wb.bits.uop.cf.loadWaitStrict && wb.bits.uop.cf.loadWaitBit 1250 debug_instTable.log( 1251 data = debug_instData, 1252 en = wb.valid, 1253 site = instSiteName, 1254 clock = clock, 1255 reset = reset 1256 ) 1257 } 1258 } 1259 } 1260 1261 1262 //difftest signals 1263 val firstValidCommit = (deqPtr + PriorityMux(io.commits.commitValid, VecInit(List.tabulate(CommitWidth)(_.U(log2Up(CommitWidth).W))))).value 1264 1265 val wdata = Wire(Vec(CommitWidth, UInt(XLEN.W))) 1266 val wpc = Wire(Vec(CommitWidth, UInt(XLEN.W))) 1267 1268 for(i <- 0 until CommitWidth) { 1269 val idx = deqPtrVec(i).value 1270 wdata(i) := debug_exuData(idx) 1271 wpc(i) := SignExt(commitDebugUop(i).cf.pc, XLEN) 1272 } 1273 1274 if (env.EnableDifftest) { 1275 for (i <- 0 until CommitWidth) { 1276 val difftest = Module(new DifftestInstrCommit) 1277 // assgin default value 1278 difftest.io := DontCare 1279 1280 difftest.io.clock := clock 1281 difftest.io.coreid := io.hartId 1282 difftest.io.index := i.U 1283 1284 val ptr = deqPtrVec(i).value 1285 val uop = commitDebugUop(i) 1286 val exuOut = debug_exuDebug(ptr) 1287 val exuData = debug_exuData(ptr) 1288 difftest.io.valid := RegNext(RegNext(RegNext(io.commits.commitValid(i) && io.commits.isCommit))) 1289 difftest.io.pc := RegNext(RegNext(RegNext(SignExt(uop.cf.pc, XLEN)))) 1290 difftest.io.instr := RegNext(RegNext(RegNext(uop.cf.instr))) 1291 difftest.io.robIdx := RegNext(RegNext(RegNext(ZeroExt(ptr, 10)))) 1292 difftest.io.lqIdx := RegNext(RegNext(RegNext(ZeroExt(uop.lqIdx.value, 7)))) 1293 difftest.io.sqIdx := RegNext(RegNext(RegNext(ZeroExt(uop.sqIdx.value, 7)))) 1294 difftest.io.isLoad := RegNext(RegNext(RegNext(io.commits.info(i).commitType === CommitType.LOAD))) 1295 difftest.io.isStore := RegNext(RegNext(RegNext(io.commits.info(i).commitType === CommitType.STORE))) 1296 difftest.io.special := RegNext(RegNext(RegNext(CommitType.isFused(io.commits.info(i).commitType)))) 1297 // when committing an eliminated move instruction, 1298 // we must make sure that skip is properly set to false (output from EXU is random value) 1299 difftest.io.skip := RegNext(RegNext(RegNext(Mux(uop.eliminatedMove, false.B, exuOut.isMMIO || exuOut.isPerfCnt)))) 1300 difftest.io.isRVC := RegNext(RegNext(RegNext(uop.cf.pd.isRVC))) 1301 difftest.io.rfwen := RegNext(RegNext(RegNext(io.commits.commitValid(i) && io.commits.info(i).rfWen && io.commits.info(i).ldest =/= 0.U))) 1302 difftest.io.fpwen := RegNext(RegNext(RegNext(io.commits.commitValid(i) && io.commits.info(i).fpWen))) 1303 difftest.io.vecwen := RegNext(RegNext(RegNext(io.commits.commitValid(i) && io.commits.info(i).vecWen))) 1304 difftest.io.wpdest := RegNext(RegNext(RegNext(io.commits.info(i).pdest))) 1305 difftest.io.wdest := RegNext(RegNext(RegNext(io.commits.info(i).ldest))) 1306 // // runahead commit hint 1307 // val runahead_commit = Module(new DifftestRunaheadCommitEvent) 1308 // runahead_commit.io.clock := clock 1309 // runahead_commit.io.coreid := io.hartId 1310 // runahead_commit.io.index := i.U 1311 // runahead_commit.io.valid := difftest.io.valid && 1312 // (commitBranchValid(i) || commitIsStore(i)) 1313 // // TODO: is branch or store 1314 // runahead_commit.io.pc := difftest.io.pc 1315 } 1316 } 1317 else if (env.AlwaysBasicDiff) { 1318 // These are the structures used by difftest only and should be optimized after synthesis. 1319 val dt_eliminatedMove = Mem(RobSize, Bool()) 1320 val dt_isRVC = Mem(RobSize, Bool()) 1321 val dt_exuDebug = Reg(Vec(RobSize, new DebugBundle)) 1322 for (i <- 0 until RenameWidth) { 1323 when (canEnqueue(i)) { 1324 dt_eliminatedMove(allocatePtrVec(i).value) := io.enq.req(i).bits.eliminatedMove 1325 dt_isRVC(allocatePtrVec(i).value) := io.enq.req(i).bits.cf.pd.isRVC 1326 } 1327 } 1328 for (wb <- exuWriteback) { 1329 when (wb.valid) { 1330 val wbIdx = wb.bits.uop.robIdx.value 1331 dt_exuDebug(wbIdx) := wb.bits.debug 1332 } 1333 } 1334 // Always instantiate basic difftest modules. 1335 for (i <- 0 until CommitWidth) { 1336 val commitInfo = io.commits.info(i) 1337 val ptr = deqPtrVec(i).value 1338 val exuOut = dt_exuDebug(ptr) 1339 val eliminatedMove = dt_eliminatedMove(ptr) 1340 val isRVC = dt_isRVC(ptr) 1341 1342 val difftest = Module(new DifftestBasicInstrCommit) 1343 difftest.io.clock := clock 1344 difftest.io.coreid := io.hartId 1345 difftest.io.index := i.U 1346 difftest.io.valid := RegNext(RegNext(RegNext(io.commits.commitValid(i) && io.commits.isCommit))) 1347 difftest.io.special := RegNext(RegNext(RegNext(CommitType.isFused(commitInfo.commitType)))) 1348 difftest.io.skip := RegNext(RegNext(RegNext(Mux(eliminatedMove, false.B, exuOut.isMMIO || exuOut.isPerfCnt)))) 1349 difftest.io.isRVC := RegNext(RegNext(RegNext(isRVC))) 1350 difftest.io.rfwen := RegNext(RegNext(RegNext(io.commits.commitValid(i) && commitInfo.rfWen && commitInfo.ldest =/= 0.U))) 1351 difftest.io.fpwen := RegNext(RegNext(RegNext(io.commits.commitValid(i) && commitInfo.fpWen))) 1352 difftest.io.vecwen := RegNext(RegNext(RegNext(io.commits.commitValid(i) && io.commits.info(i).vecWen))) 1353 difftest.io.wpdest := RegNext(RegNext(RegNext(commitInfo.pdest))) 1354 difftest.io.wdest := RegNext(RegNext(RegNext(commitInfo.ldest))) 1355 } 1356 } 1357 1358 if (env.EnableDifftest) { 1359 for (i <- 0 until CommitWidth) { 1360 val difftest = Module(new DifftestLoadEvent) 1361 difftest.io.clock := clock 1362 difftest.io.coreid := io.hartId 1363 difftest.io.index := i.U 1364 1365 val ptr = deqPtrVec(i).value 1366 val uop = commitDebugUop(i) 1367 val exuOut = debug_exuDebug(ptr) 1368 difftest.io.valid := RegNext(RegNext(RegNext(io.commits.commitValid(i) && io.commits.isCommit))) 1369 difftest.io.paddr := RegNext(RegNext(RegNext(exuOut.paddr))) 1370 difftest.io.opType := RegNext(RegNext(RegNext(uop.ctrl.fuOpType))) 1371 difftest.io.fuType := RegNext(RegNext(RegNext(uop.ctrl.fuType))) 1372 } 1373 } 1374 1375 // Always instantiate basic difftest modules. 1376 if (env.EnableDifftest) { 1377 val dt_isXSTrap = Mem(RobSize, Bool()) 1378 for (i <- 0 until RenameWidth) { 1379 when (canEnqueue(i)) { 1380 dt_isXSTrap(allocatePtrVec(i).value) := io.enq.req(i).bits.ctrl.isXSTrap 1381 } 1382 } 1383 val trapVec = io.commits.commitValid.zip(deqPtrVec).map{ case (v, d) => io.commits.isCommit && v && dt_isXSTrap(d.value) } 1384 val hitTrap = trapVec.reduce(_||_) 1385 val trapCode = PriorityMux(wdata.zip(trapVec).map(x => x._2 -> x._1)) 1386 val trapPC = SignExt(PriorityMux(wpc.zip(trapVec).map(x => x._2 ->x._1)), XLEN) 1387 val difftest = Module(new DifftestTrapEvent) 1388 difftest.io.clock := clock 1389 difftest.io.coreid := io.hartId 1390 difftest.io.valid := hitTrap 1391 difftest.io.code := trapCode 1392 difftest.io.pc := trapPC 1393 difftest.io.cycleCnt := timer 1394 difftest.io.instrCnt := instrCnt 1395 difftest.io.hasWFI := hasWFI 1396 } 1397 else if (env.AlwaysBasicDiff) { 1398 val dt_isXSTrap = Mem(RobSize, Bool()) 1399 for (i <- 0 until RenameWidth) { 1400 when (canEnqueue(i)) { 1401 dt_isXSTrap(allocatePtrVec(i).value) := io.enq.req(i).bits.ctrl.isXSTrap 1402 } 1403 } 1404 val trapVec = io.commits.commitValid.zip(deqPtrVec).map{ case (v, d) => io.commits.isCommit && v && dt_isXSTrap(d.value) } 1405 val hitTrap = trapVec.reduce(_||_) 1406 val difftest = Module(new DifftestBasicTrapEvent) 1407 difftest.io.clock := clock 1408 difftest.io.coreid := io.hartId 1409 difftest.io.valid := hitTrap 1410 difftest.io.cycleCnt := timer 1411 difftest.io.instrCnt := instrCnt 1412 } 1413 1414 val validEntriesBanks = (0 until (RobSize + 63) / 64).map(i => RegNext(PopCount(valid.drop(i * 64).take(64)))) 1415 val validEntries = RegNext(ParallelOperation(validEntriesBanks, (a: UInt, b: UInt) => a +& b)) 1416 val commitMoveVec = VecInit(io.commits.commitValid.zip(commitIsMove).map{ case (v, m) => v && m }) 1417 val commitLoadVec = VecInit(commitLoadValid) 1418 val commitBranchVec = VecInit(commitBranchValid) 1419 val commitLoadWaitVec = VecInit(commitLoadValid.zip(commitLoadWaitBit).map{ case (v, w) => v && w }) 1420 val commitStoreVec = VecInit(io.commits.commitValid.zip(commitIsStore).map{ case (v, t) => v && t }) 1421 val perfEvents = Seq( 1422 ("rob_interrupt_num ", io.flushOut.valid && intrEnable ), 1423 ("rob_exception_num ", io.flushOut.valid && exceptionEnable ), 1424 ("rob_flush_pipe_num ", io.flushOut.valid && isFlushPipe ), 1425 ("rob_replay_inst_num ", io.flushOut.valid && isFlushPipe && deqHasReplayInst ), 1426 ("rob_commitUop ", ifCommit(commitCnt) ), 1427 ("rob_commitInstr ", ifCommitReg(trueCommitCnt) ), 1428 ("rob_commitInstrMove ", ifCommitReg(PopCount(RegNext(commitMoveVec))) ), 1429 ("rob_commitInstrFused ", ifCommitReg(fuseCommitCnt) ), 1430 ("rob_commitInstrLoad ", ifCommitReg(PopCount(RegNext(commitLoadVec))) ), 1431 ("rob_commitInstrBranch ", ifCommitReg(PopCount(RegNext(commitBranchVec))) ), 1432 ("rob_commitInstrLoadWait", ifCommitReg(PopCount(RegNext(commitLoadWaitVec))) ), 1433 ("rob_commitInstrStore ", ifCommitReg(PopCount(RegNext(commitStoreVec))) ), 1434 ("rob_walkInstr ", Mux(io.commits.isWalk, PopCount(io.commits.walkValid), 0.U) ), 1435 ("rob_walkCycle ", (state === s_walk) ), 1436 ("rob_1_4_valid ", validEntries <= (RobSize / 4).U ), 1437 ("rob_2_4_valid ", validEntries > (RobSize / 4).U && validEntries <= (RobSize / 2).U ), 1438 ("rob_3_4_valid ", validEntries > (RobSize / 2).U && validEntries <= (RobSize * 3 / 4).U), 1439 ("rob_4_4_valid ", validEntries > (RobSize * 3 / 4).U ), 1440 ) 1441 generatePerfEvent() 1442} 1443