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 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util.BitPat.bitPatToUInt 22import chisel3.util._ 23import chisel3.experimental.BundleLiterals._ 24import utility._ 25import utils._ 26import xiangshan.backend.decode.{ImmUnion, XDecode} 27import xiangshan.backend.fu.FuType 28import xiangshan.backend.rob.RobPtr 29import xiangshan.frontend._ 30import xiangshan.mem.{LqPtr, SqPtr} 31import xiangshan.backend.Bundles.{DynInst, UopIdx} 32import xiangshan.backend.fu.vector.Bundles.VType 33import xiangshan.frontend.{AllAheadFoldedHistoryOldestBits, AllFoldedHistories, BPUCtrl, CGHPtr, FtqPtr, FtqToCtrlIO} 34import xiangshan.frontend.{Ftq_Redirect_SRAMEntry, HasBPUParameter, IfuToBackendIO, PreDecodeInfo, RASPtr} 35import xiangshan.cache.HasDCacheParameters 36import utility._ 37 38import org.chipsalliance.cde.config.Parameters 39import chisel3.util.BitPat.bitPatToUInt 40import chisel3.util.experimental.decode.EspressoMinimizer 41import xiangshan.backend.CtrlToFtqIO 42import xiangshan.backend.fu.NewCSR.{Mcontrol6, Tdata1Bundle, Tdata2Bundle} 43import xiangshan.backend.fu.PMPEntry 44import xiangshan.frontend.Ftq_Redirect_SRAMEntry 45import xiangshan.frontend.AllFoldedHistories 46import xiangshan.frontend.AllAheadFoldedHistoryOldestBits 47import xiangshan.frontend.RASPtr 48import xiangshan.backend.rob.RobBundles.RobCommitEntryBundle 49import xiangshan.backend.trace._ 50 51class ValidUndirectioned[T <: Data](gen: T) extends Bundle { 52 val valid = Bool() 53 val bits = gen.cloneType.asInstanceOf[T] 54 55} 56 57object ValidUndirectioned { 58 def apply[T <: Data](gen: T) = { 59 new ValidUndirectioned[T](gen) 60 } 61} 62 63object RSFeedbackType { 64 val lrqFull = 0.U(4.W) 65 val tlbMiss = 1.U(4.W) 66 val mshrFull = 2.U(4.W) 67 val dataInvalid = 3.U(4.W) 68 val bankConflict = 4.U(4.W) 69 val ldVioCheckRedo = 5.U(4.W) 70 val feedbackInvalid = 7.U(4.W) 71 val issueSuccess = 8.U(4.W) 72 val rfArbitFail = 9.U(4.W) 73 val fuIdle = 10.U(4.W) 74 val fuBusy = 11.U(4.W) 75 val fuUncertain = 12.U(4.W) 76 77 val allTypes = 16 78 def apply() = UInt(4.W) 79 80 def isStageSuccess(feedbackType: UInt) = { 81 feedbackType === issueSuccess 82 } 83 84 def isBlocked(feedbackType: UInt) = { 85 feedbackType === rfArbitFail || feedbackType === fuBusy || feedbackType >= lrqFull && feedbackType <= feedbackInvalid 86 } 87} 88 89class PredictorAnswer(implicit p: Parameters) extends XSBundle { 90 val hit = if (!env.FPGAPlatform) Bool() else UInt(0.W) 91 val taken = if (!env.FPGAPlatform) Bool() else UInt(0.W) 92 val target = if (!env.FPGAPlatform) UInt(VAddrBits.W) else UInt(0.W) 93} 94 95class CfiUpdateInfo(implicit p: Parameters) extends XSBundle with HasBPUParameter { 96 // from backend 97 val pc = UInt(VAddrBits.W) 98 // frontend -> backend -> frontend 99 val pd = new PreDecodeInfo 100 val ssp = UInt(log2Up(RasSize).W) 101 val sctr = UInt(RasCtrSize.W) 102 val TOSW = new RASPtr 103 val TOSR = new RASPtr 104 val NOS = new RASPtr 105 val topAddr = UInt(VAddrBits.W) 106 // val hist = new ShiftingGlobalHistory 107 val folded_hist = new AllFoldedHistories(foldedGHistInfos) 108 val afhob = new AllAheadFoldedHistoryOldestBits(foldedGHistInfos) 109 val lastBrNumOH = UInt((numBr+1).W) 110 val ghr = UInt(UbtbGHRLength.W) 111 val histPtr = new CGHPtr 112 val specCnt = Vec(numBr, UInt(10.W)) 113 // need pipeline update 114 val br_hit = Bool() // if in ftb entry 115 val jr_hit = Bool() // if in ftb entry 116 val sc_hit = Bool() // if used in ftb entry, invalid if !br_hit 117 val predTaken = Bool() 118 val target = UInt(VAddrBits.W) 119 val taken = Bool() 120 val isMisPred = Bool() 121 val shift = UInt((log2Ceil(numBr)+1).W) 122 val addIntoHist = Bool() 123 // raise exceptions from backend 124 val backendIGPF = Bool() // instruction guest page fault 125 val backendIPF = Bool() // instruction page fault 126 val backendIAF = Bool() // instruction access fault 127 128 def fromFtqRedirectSram(entry: Ftq_Redirect_SRAMEntry) = { 129 // this.hist := entry.ghist 130 this.histPtr := entry.histPtr 131 this.ssp := entry.ssp 132 this.sctr := entry.sctr 133 this.TOSW := entry.TOSW 134 this.TOSR := entry.TOSR 135 this.NOS := entry.NOS 136 this.topAddr := entry.topAddr 137 this 138 } 139 140 def hasBackendFault = backendIGPF || backendIPF || backendIAF 141} 142 143// Dequeue DecodeWidth insts from Ibuffer 144class CtrlFlow(implicit p: Parameters) extends XSBundle { 145 val instr = UInt(32.W) 146 val pc = UInt(VAddrBits.W) 147 val foldpc = UInt(MemPredPCWidth.W) 148 val exceptionVec = ExceptionVec() 149 val backendException = Bool() 150 val trigger = TriggerAction() 151 val pd = new PreDecodeInfo 152 val pred_taken = Bool() 153 val crossPageIPFFix = Bool() 154 val storeSetHit = Bool() // inst has been allocated an store set 155 val waitForRobIdx = new RobPtr // store set predicted previous store robIdx 156 // Load wait is needed 157 // load inst will not be executed until former store (predicted by mdp) addr calcuated 158 val loadWaitBit = Bool() 159 // If (loadWaitBit && loadWaitStrict), strict load wait is needed 160 // load inst will not be executed until ALL former store addr calcuated 161 val loadWaitStrict = Bool() 162 val ssid = UInt(SSIDWidth.W) 163 val ftqPtr = new FtqPtr 164 val ftqOffset = UInt(log2Up(PredictWidth).W) 165 val isLastInFtqEntry = Bool() 166} 167 168 169class FPUCtrlSignals(implicit p: Parameters) extends XSBundle { 170 val typeTagOut = UInt(2.W) // H S D 171 val wflags = Bool() 172 val typ = UInt(2.W) 173 val fmt = UInt(2.W) 174 val rm = UInt(3.W) 175} 176 177// Decode DecodeWidth insts at Decode Stage 178class CtrlSignals(implicit p: Parameters) extends XSBundle { 179 // val debug_globalID = UInt(XLEN.W) 180 val srcType = Vec(4, SrcType()) 181 val lsrc = Vec(4, UInt(LogicRegsWidth.W)) 182 val ldest = UInt(LogicRegsWidth.W) 183 val fuType = FuType() 184 val fuOpType = FuOpType() 185 val rfWen = Bool() 186 val fpWen = Bool() 187 val vecWen = Bool() 188 val isXSTrap = Bool() 189 val noSpecExec = Bool() // wait forward 190 val blockBackward = Bool() // block backward 191 val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit 192 val uopSplitType = UopSplitType() 193 val selImm = SelImm() 194 val imm = UInt(32.W) 195 val commitType = CommitType() 196 val fpu = new FPUCtrlSignals 197 val uopIdx = UopIdx() 198 val isMove = Bool() 199 val vm = Bool() 200 val singleStep = Bool() 201 // This inst will flush all the pipe when it is the oldest inst in ROB, 202 // then replay from this inst itself 203 val replayInst = Bool() 204 val canRobCompress = Bool() 205 206 private def allSignals = srcType.take(3) ++ Seq(fuType, fuOpType, rfWen, fpWen, vecWen, 207 isXSTrap, noSpecExec, blockBackward, flushPipe, canRobCompress, uopSplitType, selImm) 208 209 def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]): CtrlSignals = { 210 val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table, EspressoMinimizer) 211 allSignals zip decoder foreach { case (s, d) => s := d } 212 commitType := DontCare 213 this 214 } 215 216 def decode(bit: List[BitPat]): CtrlSignals = { 217 allSignals.zip(bit.map(bitPatToUInt(_))).foreach{ case (s, d) => s := d } 218 this 219 } 220 221 def isWFI: Bool = fuType === FuType.csr.U && fuOpType === CSROpType.wfi 222 def isSoftPrefetch: Bool = { 223 fuType === FuType.alu.U && fuOpType === ALUOpType.or && selImm === SelImm.IMM_I && ldest === 0.U 224 } 225 def needWriteRf: Bool = rfWen || fpWen || vecWen 226 def isHyperInst: Bool = { 227 fuType === FuType.ldu.U && LSUOpType.isHlv(fuOpType) || fuType === FuType.stu.U && LSUOpType.isHsv(fuOpType) 228 } 229} 230 231class CfCtrl(implicit p: Parameters) extends XSBundle { 232 val cf = new CtrlFlow 233 val ctrl = new CtrlSignals 234} 235 236class PerfDebugInfo(implicit p: Parameters) extends XSBundle { 237 val eliminatedMove = Bool() 238 // val fetchTime = UInt(XLEN.W) 239 val renameTime = UInt(XLEN.W) 240 val dispatchTime = UInt(XLEN.W) 241 val enqRsTime = UInt(XLEN.W) 242 val selectTime = UInt(XLEN.W) 243 val issueTime = UInt(XLEN.W) 244 val writebackTime = UInt(XLEN.W) 245 // val commitTime = UInt(XLEN.W) 246 val runahead_checkpoint_id = UInt(XLEN.W) 247 val tlbFirstReqTime = UInt(XLEN.W) 248 val tlbRespTime = UInt(XLEN.W) // when getting hit result (including delay in L2TLB hit) 249} 250 251// Separate LSQ 252class LSIdx(implicit p: Parameters) extends XSBundle { 253 val lqIdx = new LqPtr 254 val sqIdx = new SqPtr 255} 256 257// CfCtrl -> MicroOp at Rename Stage 258class MicroOp(implicit p: Parameters) extends CfCtrl { 259 val srcState = Vec(4, SrcState()) 260 val psrc = Vec(4, UInt(PhyRegIdxWidth.W)) 261 val pdest = UInt(PhyRegIdxWidth.W) 262 val robIdx = new RobPtr 263 val instrSize = UInt(log2Ceil(RenameWidth + 1).W) 264 val lqIdx = new LqPtr 265 val sqIdx = new SqPtr 266 val eliminatedMove = Bool() 267 val snapshot = Bool() 268 val debugInfo = new PerfDebugInfo 269 def needRfRPort(index: Int, isFp: Boolean, ignoreState: Boolean = true) : Bool = { 270 val stateReady = srcState(index) === SrcState.rdy || ignoreState.B 271 val readReg = if (isFp) { 272 ctrl.srcType(index) === SrcType.fp 273 } else { 274 ctrl.srcType(index) === SrcType.reg && ctrl.lsrc(index) =/= 0.U 275 } 276 readReg && stateReady 277 } 278 def srcIsReady: Vec[Bool] = { 279 VecInit(ctrl.srcType.zip(srcState).map{ case (t, s) => SrcType.isPcOrImm(t) || s === SrcState.rdy }) 280 } 281 def clearExceptions( 282 exceptionBits: Seq[Int] = Seq(), 283 flushPipe: Boolean = false, 284 replayInst: Boolean = false 285 ): MicroOp = { 286 cf.exceptionVec.zipWithIndex.filterNot(x => exceptionBits.contains(x._2)).foreach(_._1 := false.B) 287 if (!flushPipe) { ctrl.flushPipe := false.B } 288 if (!replayInst) { ctrl.replayInst := false.B } 289 this 290 } 291} 292 293class XSBundleWithMicroOp(implicit p: Parameters) extends XSBundle { 294 val uop = new DynInst 295} 296 297class MicroOpRbExt(implicit p: Parameters) extends XSBundleWithMicroOp { 298 val flag = UInt(1.W) 299} 300 301class Redirect(implicit p: Parameters) extends XSBundle { 302 val isRVC = Bool() 303 val robIdx = new RobPtr 304 val ftqIdx = new FtqPtr 305 val ftqOffset = UInt(log2Up(PredictWidth).W) 306 val level = RedirectLevel() 307 val interrupt = Bool() 308 val cfiUpdate = new CfiUpdateInfo 309 val fullTarget = UInt(XLEN.W) // only used for tval storage in backend 310 311 val stFtqIdx = new FtqPtr // for load violation predict 312 val stFtqOffset = UInt(log2Up(PredictWidth).W) 313 314 val debug_runahead_checkpoint_id = UInt(64.W) 315 val debugIsCtrl = Bool() 316 val debugIsMemVio = Bool() 317 318 def flushItself() = RedirectLevel.flushItself(level) 319} 320 321object Redirect extends HasCircularQueuePtrHelper { 322 323 def selectOldestRedirect(xs: Seq[Valid[Redirect]]): Vec[Bool] = { 324 val compareVec = (0 until xs.length).map(i => (0 until i).map(j => isAfter(xs(j).bits.robIdx, xs(i).bits.robIdx))) 325 val resultOnehot = VecInit((0 until xs.length).map(i => Cat((0 until xs.length).map(j => 326 (if (j < i) !xs(j).valid || compareVec(i)(j) 327 else if (j == i) xs(i).valid 328 else !xs(j).valid || !compareVec(j)(i)) 329 )).andR)) 330 resultOnehot 331 } 332} 333 334class ResetPregStateReq(implicit p: Parameters) extends XSBundle { 335 // NOTE: set isInt and isFp both to 'false' when invalid 336 val isInt = Bool() 337 val isFp = Bool() 338 val isVec = Bool() 339 val isV0 = Bool() 340 val isVl = Bool() 341 val preg = UInt(PhyRegIdxWidth.W) 342} 343 344class DebugBundle(implicit p: Parameters) extends XSBundle { 345 val isMMIO = Bool() 346 val isNC = Bool() 347 val isPerfCnt = Bool() 348 val paddr = UInt(PAddrBits.W) 349 val vaddr = UInt(VAddrBits.W) 350 351 def isSkipDiff: Bool = isMMIO || isNC || isPerfCnt 352 /* add L/S inst info in EXU */ 353 // val L1toL2TlbLatency = UInt(XLEN.W) 354 // val levelTlbHit = UInt(2.W) 355} 356 357class SoftIfetchPrefetchBundle(implicit p: Parameters) extends XSBundle { 358 val vaddr = UInt(VAddrBits.W) 359} 360 361class ExternalInterruptIO(implicit p: Parameters) extends XSBundle { 362 val mtip = Input(Bool()) 363 val msip = Input(Bool()) 364 val meip = Input(Bool()) 365 val seip = Input(Bool()) 366 val debug = Input(Bool()) 367 val nmi = new NonmaskableInterruptIO() 368} 369 370class NonmaskableInterruptIO() extends Bundle { 371 val nmi_31 = Input(Bool()) 372 val nmi_43 = Input(Bool()) 373 // reserve for other nmi type 374} 375 376class CSRSpecialIO(implicit p: Parameters) extends XSBundle { 377 val exception = Flipped(ValidIO(new DynInst)) 378 val isInterrupt = Input(Bool()) 379 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 380 val trapTarget = Output(UInt(VAddrBits.W)) 381 val externalInterrupt = new ExternalInterruptIO 382 val interrupt = Output(Bool()) 383} 384 385class DiffCommitIO(implicit p: Parameters) extends XSBundle { 386 val isCommit = Bool() 387 val commitValid = Vec(CommitWidth * MaxUopSize, Bool()) 388 389 val info = Vec(CommitWidth * MaxUopSize, new RabCommitInfo) 390} 391 392class RobCommitInfo(implicit p: Parameters) extends RobCommitEntryBundle 393 394class RobCommitIO(implicit p: Parameters) extends XSBundle { 395 val isCommit = Bool() 396 val commitValid = Vec(CommitWidth, Bool()) 397 398 val isWalk = Bool() 399 // valid bits optimized for walk 400 val walkValid = Vec(CommitWidth, Bool()) 401 402 val info = Vec(CommitWidth, new RobCommitInfo) 403 val robIdx = Vec(CommitWidth, new RobPtr) 404 405 def hasWalkInstr: Bool = isWalk && walkValid.asUInt.orR 406 def hasCommitInstr: Bool = isCommit && commitValid.asUInt.orR 407} 408 409class RabCommitInfo(implicit p: Parameters) extends XSBundle { 410 val ldest = UInt(LogicRegsWidth.W) 411 val pdest = UInt(PhyRegIdxWidth.W) 412 val rfWen = Bool() 413 val fpWen = Bool() 414 val vecWen = Bool() 415 val v0Wen = Bool() 416 val vlWen = Bool() 417 val isMove = Bool() 418} 419 420class RabCommitIO(implicit p: Parameters) extends XSBundle { 421 val isCommit = Bool() 422 val commitValid = Vec(RabCommitWidth, Bool()) 423 424 val isWalk = Bool() 425 // valid bits optimized for walk 426 val walkValid = Vec(RabCommitWidth, Bool()) 427 428 val info = Vec(RabCommitWidth, new RabCommitInfo) 429 val robIdx = OptionWrapper(!env.FPGAPlatform, Vec(RabCommitWidth, new RobPtr)) 430 431 def hasWalkInstr: Bool = isWalk && walkValid.asUInt.orR 432 def hasCommitInstr: Bool = isCommit && commitValid.asUInt.orR 433} 434 435class SnapshotPort(implicit p: Parameters) extends XSBundle { 436 val snptEnq = Bool() 437 val snptDeq = Bool() 438 val useSnpt = Bool() 439 val snptSelect = UInt(log2Ceil(RenameSnapshotNum).W) 440 val flushVec = Vec(RenameSnapshotNum, Bool()) 441} 442 443class RSFeedback(isVector: Boolean = false)(implicit p: Parameters) extends XSBundle { 444 val robIdx = new RobPtr 445 val hit = Bool() 446 val flushState = Bool() 447 val sourceType = RSFeedbackType() 448 val dataInvalidSqIdx = new SqPtr 449 val sqIdx = new SqPtr 450 val lqIdx = new LqPtr 451} 452 453class MemRSFeedbackIO(isVector: Boolean = false)(implicit p: Parameters) extends XSBundle { 454 // Note: you need to update in implicit Parameters p before imp MemRSFeedbackIO 455 // for instance: MemRSFeedbackIO()(updateP) 456 val feedbackSlow = ValidIO(new RSFeedback(isVector)) // dcache miss queue full, dtlb miss 457 val feedbackFast = ValidIO(new RSFeedback(isVector)) // bank conflict 458} 459 460class LoadCancelIO(implicit p: Parameters) extends XSBundle { 461 val ld1Cancel = Bool() 462 val ld2Cancel = Bool() 463} 464 465class FrontendToCtrlIO(implicit p: Parameters) extends XSBundle { 466 // to backend end 467 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 468 val stallReason = new StallReasonIO(DecodeWidth) 469 val fromFtq = new FtqToCtrlIO 470 val fromIfu = new IfuToBackendIO 471 // from backend 472 val toFtq = Flipped(new CtrlToFtqIO) 473 val canAccept = Input(Bool()) 474} 475 476class SatpStruct(implicit p: Parameters) extends XSBundle { 477 val mode = UInt(4.W) 478 val asid = UInt(16.W) 479 val ppn = UInt(44.W) 480} 481 482class TlbSatpBundle(implicit p: Parameters) extends SatpStruct { 483 val changed = Bool() 484 485 // Todo: remove it 486 def apply(satp_value: UInt): Unit = { 487 require(satp_value.getWidth == XLEN) 488 val sa = satp_value.asTypeOf(new SatpStruct) 489 mode := sa.mode 490 asid := sa.asid 491 ppn := sa.ppn 492 changed := DataChanged(sa.asid) // when ppn is changed, software need do the flush 493 } 494} 495 496class HgatpStruct(implicit p: Parameters) extends XSBundle { 497 val mode = UInt(4.W) 498 val vmid = UInt(16.W) 499 val ppn = UInt(44.W) 500} 501 502class TlbHgatpBundle(implicit p: Parameters) extends HgatpStruct { 503 val changed = Bool() 504 505 // Todo: remove it 506 def apply(hgatp_value: UInt): Unit = { 507 require(hgatp_value.getWidth == XLEN) 508 val sa = hgatp_value.asTypeOf(new HgatpStruct) 509 mode := sa.mode 510 vmid := sa.vmid 511 ppn := sa.ppn 512 changed := DataChanged(sa.vmid) // when ppn is changed, software need do the flush 513 } 514} 515 516class TlbCsrBundle(implicit p: Parameters) extends XSBundle { 517 val satp = new TlbSatpBundle() 518 val vsatp = new TlbSatpBundle() 519 val hgatp = new TlbHgatpBundle() 520 val priv = new Bundle { 521 val mxr = Bool() 522 val sum = Bool() 523 val vmxr = Bool() 524 val vsum = Bool() 525 val virt = Bool() 526 val spvp = UInt(1.W) 527 val imode = UInt(2.W) 528 val dmode = UInt(2.W) 529 } 530 val mPBMTE = Bool() 531 val hPBMTE = Bool() 532 val pmm = new Bundle { 533 val mseccfg = UInt(2.W) 534 val menvcfg = UInt(2.W) 535 val henvcfg = UInt(2.W) 536 val hstatus = UInt(2.W) 537 val senvcfg = UInt(2.W) 538 } 539 540 override def toPrintable: Printable = { 541 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 542 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 543 } 544} 545 546class SfenceBundle(implicit p: Parameters) extends XSBundle { 547 val valid = Bool() 548 val bits = new Bundle { 549 val rs1 = Bool() 550 val rs2 = Bool() 551 val addr = UInt(VAddrBits.W) 552 val id = UInt((AsidLength).W) // asid or vmid 553 val flushPipe = Bool() 554 val hv = Bool() 555 val hg = Bool() 556 } 557 558 override def toPrintable: Printable = { 559 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}, flushPipe:${bits.flushPipe}" 560 } 561} 562 563// Bundle for load violation predictor updating 564class MemPredUpdateReq(implicit p: Parameters) extends XSBundle { 565 val valid = Bool() 566 567 // wait table update 568 val waddr = UInt(MemPredPCWidth.W) 569 val wdata = Bool() // true.B by default 570 571 // store set update 572 // by default, ldpc/stpc should be xor folded 573 val ldpc = UInt(MemPredPCWidth.W) 574 val stpc = UInt(MemPredPCWidth.W) 575} 576 577class CustomCSRCtrlIO(implicit p: Parameters) extends XSBundle { 578 // Prefetcher 579 val l1I_pf_enable = Output(Bool()) 580 val l2_pf_enable = Output(Bool()) 581 val l1D_pf_enable = Output(Bool()) 582 val l1D_pf_train_on_hit = Output(Bool()) 583 val l1D_pf_enable_agt = Output(Bool()) 584 val l1D_pf_enable_pht = Output(Bool()) 585 val l1D_pf_active_threshold = Output(UInt(4.W)) 586 val l1D_pf_active_stride = Output(UInt(6.W)) 587 val l1D_pf_enable_stride = Output(Bool()) 588 val l2_pf_store_only = Output(Bool()) 589 // ICache 590 val icache_parity_enable = Output(Bool()) 591 // Load violation predictor 592 val lvpred_disable = Output(Bool()) 593 val no_spec_load = Output(Bool()) 594 val storeset_wait_store = Output(Bool()) 595 val storeset_no_fast_wakeup = Output(Bool()) 596 val lvpred_timeout = Output(UInt(5.W)) 597 // Branch predictor 598 val bp_ctrl = Output(new BPUCtrl) 599 // Memory Block 600 val sbuffer_threshold = Output(UInt(4.W)) 601 val ldld_vio_check_enable = Output(Bool()) 602 val soft_prefetch_enable = Output(Bool()) 603 val cache_error_enable = Output(Bool()) 604 val uncache_write_outstanding_enable = Output(Bool()) 605 val hd_misalign_st_enable = Output(Bool()) 606 val hd_misalign_ld_enable = Output(Bool()) 607 // Rename 608 val fusion_enable = Output(Bool()) 609 val wfi_enable = Output(Bool()) 610 611 // distribute csr write signal 612 val distribute_csr = new DistributedCSRIO() 613 // TODO: move it to a new bundle, since single step is not a custom control signal 614 val singlestep = Output(Bool()) 615 val frontend_trigger = new FrontendTdataDistributeIO() 616 val mem_trigger = new MemTdataDistributeIO() 617 // Virtualization Mode 618 val virtMode = Output(Bool()) 619 // xstatus.fs field is off 620 val fsIsOff = Output(Bool()) 621} 622 623class DistributedCSRIO(implicit p: Parameters) extends XSBundle { 624 // CSR has been written by csr inst, copies of csr should be updated 625 val w = ValidIO(new Bundle { 626 val addr = Output(UInt(12.W)) 627 val data = Output(UInt(XLEN.W)) 628 }) 629} 630 631class DistributedCSRUpdateReq(implicit p: Parameters) extends XSBundle { 632 // Request csr to be updated 633 // 634 // Note that this request will ONLY update CSR Module it self, 635 // copies of csr will NOT be updated, use it with care! 636 // 637 // For each cycle, no more than 1 DistributedCSRUpdateReq is valid 638 val w = ValidIO(new Bundle { 639 val addr = Output(UInt(12.W)) 640 val data = Output(UInt(XLEN.W)) 641 }) 642 def apply(valid: Bool, addr: UInt, data: UInt, src_description: String) = { 643 when(valid){ 644 w.bits.addr := addr 645 w.bits.data := data 646 } 647 println("Distributed CSR update req registered for " + src_description) 648 } 649} 650 651class AddrTransType(implicit p: Parameters) extends XSBundle { 652 val bare, sv39, sv39x4, sv48, sv48x4 = Bool() 653 654 def checkAccessFault(target: UInt): Bool = bare && target(XLEN - 1, PAddrBits).orR 655 def checkPageFault(target: UInt): Bool = 656 sv39 && target(XLEN - 1, 39) =/= VecInit.fill(XLEN - 39)(target(38)).asUInt || 657 sv48 && target(XLEN - 1, 48) =/= VecInit.fill(XLEN - 48)(target(47)).asUInt 658 def checkGuestPageFault(target: UInt): Bool = 659 sv39x4 && target(XLEN - 1, 41).orR || sv48x4 && target(XLEN - 1, 50).orR 660} 661 662object AddrTransType { 663 def apply(bare: Boolean = false, 664 sv39: Boolean = false, 665 sv39x4: Boolean = false, 666 sv48: Boolean = false, 667 sv48x4: Boolean = false)(implicit p: Parameters): AddrTransType = 668 (new AddrTransType).Lit(_.bare -> bare.B, 669 _.sv39 -> sv39.B, 670 _.sv39x4 -> sv39x4.B, 671 _.sv48 -> sv48.B, 672 _.sv48x4 -> sv48x4.B) 673 674 def apply(bare: Bool, sv39: Bool, sv39x4: Bool, sv48: Bool, sv48x4: Bool)(implicit p: Parameters): AddrTransType = { 675 val addrTransType = Wire(new AddrTransType) 676 addrTransType.bare := bare 677 addrTransType.sv39 := sv39 678 addrTransType.sv39x4 := sv39x4 679 addrTransType.sv48 := sv48 680 addrTransType.sv48x4 := sv48x4 681 addrTransType 682 } 683} 684 685class L1CacheErrorInfo(implicit p: Parameters) extends XSBundle { 686 // L1CacheErrorInfo is also used to encode customized CACHE_ERROR CSR 687 val source = Output(new Bundle() { 688 val tag = Bool() // l1 tag array 689 val data = Bool() // l1 data array 690 val l2 = Bool() 691 }) 692 val opType = Output(new Bundle() { 693 val fetch = Bool() 694 val load = Bool() 695 val store = Bool() 696 val probe = Bool() 697 val release = Bool() 698 val atom = Bool() 699 }) 700 val paddr = Output(UInt(PAddrBits.W)) 701 702 // report error and paddr to beu 703 // bus error unit will receive error info iff ecc_error.valid 704 val report_to_beu = Output(Bool()) 705 706 def toL1BusErrorUnitInfo(valid: Bool): L1BusErrorUnitInfo = { 707 val beu_info = Wire(new L1BusErrorUnitInfo) 708 beu_info.ecc_error.valid := valid && report_to_beu 709 beu_info.ecc_error.bits := paddr 710 beu_info 711 } 712} 713 714object TriggerAction extends NamedUInt(4) { 715 // Put breakpoint Exception gererated by trigger in ExceptionVec[3]. 716 def BreakpointExp = 0.U(width.W) // raise breakpoint exception 717 def DebugMode = 1.U(width.W) // enter debug mode 718 def TraceOn = 2.U(width.W) 719 def TraceOff = 3.U(width.W) 720 def TraceNotify = 4.U(width.W) 721 def None = 15.U(width.W) // use triggerAction = 15.U to express that action is None; 722 723 def isExp(action: UInt) = action === BreakpointExp 724 def isDmode(action: UInt) = action === DebugMode 725 def isNone(action: UInt) = action === None 726} 727 728// these 3 bundles help distribute trigger control signals from CSR 729// to Frontend, Load and Store. 730class FrontendTdataDistributeIO(implicit p: Parameters) extends XSBundle { 731 val tUpdate = ValidIO(new Bundle { 732 val addr = Output(UInt(log2Up(TriggerNum).W)) 733 val tdata = new MatchTriggerIO 734 }) 735 val tEnableVec: Vec[Bool] = Output(Vec(TriggerNum, Bool())) 736 val debugMode = Output(Bool()) 737 val triggerCanRaiseBpExp = Output(Bool()) 738} 739 740class MemTdataDistributeIO(implicit p: Parameters) extends XSBundle { 741 val tUpdate = ValidIO(new Bundle { 742 val addr = Output(UInt(log2Up(TriggerNum).W)) 743 val tdata = new MatchTriggerIO 744 }) 745 val tEnableVec: Vec[Bool] = Output(Vec(TriggerNum, Bool())) 746 val debugMode = Output(Bool()) 747 val triggerCanRaiseBpExp = Output(Bool()) 748} 749 750class MatchTriggerIO(implicit p: Parameters) extends XSBundle { 751 val matchType = Output(UInt(2.W)) 752 val select = Output(Bool()) 753 val timing = Output(Bool()) 754 val action = Output(TriggerAction()) 755 val chain = Output(Bool()) 756 val execute = Output(Bool()) 757 val store = Output(Bool()) 758 val load = Output(Bool()) 759 val tdata2 = Output(UInt(64.W)) 760 761 def GenTdataDistribute(tdata1: Tdata1Bundle, tdata2: Tdata2Bundle): MatchTriggerIO = { 762 val mcontrol6 = Wire(new Mcontrol6) 763 mcontrol6 := tdata1.DATA.asUInt 764 this.matchType := mcontrol6.MATCH.asUInt 765 this.select := mcontrol6.SELECT.asBool 766 this.timing := false.B 767 this.action := mcontrol6.ACTION.asUInt 768 this.chain := mcontrol6.CHAIN.asBool 769 this.execute := mcontrol6.EXECUTE.asBool 770 this.load := mcontrol6.LOAD.asBool 771 this.store := mcontrol6.STORE.asBool 772 this.tdata2 := tdata2.asUInt 773 this 774 } 775} 776 777class StallReasonIO(width: Int) extends Bundle { 778 val reason = Output(Vec(width, UInt(log2Ceil(TopDownCounters.NumStallReasons.id).W))) 779 val backReason = Flipped(Valid(UInt(log2Ceil(TopDownCounters.NumStallReasons.id).W))) 780} 781 782// custom l2 - l1 interface 783class L2ToL1Hint(implicit p: Parameters) extends XSBundle with HasDCacheParameters { 784 val sourceId = UInt(log2Up(cfg.nMissEntries).W) // tilelink sourceID -> mshr id 785 val isKeyword = Bool() // miss entry keyword -> L1 load queue replay 786} 787 788