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