109c6f1ddSLingrui98/*************************************************************************************** 209c6f1ddSLingrui98* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 309c6f1ddSLingrui98* Copyright (c) 2020-2021 Peng Cheng Laboratory 409c6f1ddSLingrui98* 509c6f1ddSLingrui98* XiangShan is licensed under Mulan PSL v2. 609c6f1ddSLingrui98* You can use this software according to the terms and conditions of the Mulan PSL v2. 709c6f1ddSLingrui98* You may obtain a copy of Mulan PSL v2 at: 809c6f1ddSLingrui98* http://license.coscl.org.cn/MulanPSL2 909c6f1ddSLingrui98* 1009c6f1ddSLingrui98* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 1109c6f1ddSLingrui98* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 1209c6f1ddSLingrui98* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 1309c6f1ddSLingrui98* 1409c6f1ddSLingrui98* See the Mulan PSL v2 for more details. 1509c6f1ddSLingrui98***************************************************************************************/ 1609c6f1ddSLingrui98 1709c6f1ddSLingrui98package xiangshan.frontend 1809c6f1ddSLingrui98 198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters 2009c6f1ddSLingrui98import chisel3._ 2109c6f1ddSLingrui98import chisel3.util._ 221ca0e4f3SYinan Xuimport utils._ 233c02ee8fSwakafaimport utility._ 2409c6f1ddSLingrui98import xiangshan._ 25e30430c2SJayimport xiangshan.frontend.icache._ 261ca0e4f3SYinan Xuimport xiangshan.backend.CtrlToFtqIO 272e1be6e1SSteve Gouimport xiangshan.backend.decode.ImmUnion 283c02ee8fSwakafaimport utility.ChiselDB 2951532d8bSGuokai Chen 3051532d8bSGuokai Chenclass FtqDebugBundle extends Bundle { 3151532d8bSGuokai Chen val pc = UInt(39.W) 3251532d8bSGuokai Chen val target = UInt(39.W) 3351532d8bSGuokai Chen val isBr = Bool() 3451532d8bSGuokai Chen val isJmp = Bool() 3551532d8bSGuokai Chen val isCall = Bool() 3651532d8bSGuokai Chen val isRet = Bool() 3751532d8bSGuokai Chen val misPred = Bool() 3851532d8bSGuokai Chen val isTaken = Bool() 3951532d8bSGuokai Chen val predStage = UInt(2.W) 4051532d8bSGuokai Chen} 4109c6f1ddSLingrui98 423b739f49SXuan Huclass FtqPtr(entries: Int) extends CircularQueuePtr[FtqPtr]( 433b739f49SXuan Hu entries 4409c6f1ddSLingrui98){ 453b739f49SXuan Hu def this()(implicit p: Parameters) = this(p(XSCoreParamsKey).FtqSize) 4609c6f1ddSLingrui98} 4709c6f1ddSLingrui98 4809c6f1ddSLingrui98object FtqPtr { 4909c6f1ddSLingrui98 def apply(f: Bool, v: UInt)(implicit p: Parameters): FtqPtr = { 5009c6f1ddSLingrui98 val ptr = Wire(new FtqPtr) 5109c6f1ddSLingrui98 ptr.flag := f 5209c6f1ddSLingrui98 ptr.value := v 5309c6f1ddSLingrui98 ptr 5409c6f1ddSLingrui98 } 5509c6f1ddSLingrui98 def inverse(ptr: FtqPtr)(implicit p: Parameters): FtqPtr = { 5609c6f1ddSLingrui98 apply(!ptr.flag, ptr.value) 5709c6f1ddSLingrui98 } 5809c6f1ddSLingrui98} 5909c6f1ddSLingrui98 6009c6f1ddSLingrui98class FtqNRSRAM[T <: Data](gen: T, numRead: Int)(implicit p: Parameters) extends XSModule { 6109c6f1ddSLingrui98 6209c6f1ddSLingrui98 val io = IO(new Bundle() { 6309c6f1ddSLingrui98 val raddr = Input(Vec(numRead, UInt(log2Up(FtqSize).W))) 6409c6f1ddSLingrui98 val ren = Input(Vec(numRead, Bool())) 6509c6f1ddSLingrui98 val rdata = Output(Vec(numRead, gen)) 6609c6f1ddSLingrui98 val waddr = Input(UInt(log2Up(FtqSize).W)) 6709c6f1ddSLingrui98 val wen = Input(Bool()) 6809c6f1ddSLingrui98 val wdata = Input(gen) 6909c6f1ddSLingrui98 }) 7009c6f1ddSLingrui98 7109c6f1ddSLingrui98 for(i <- 0 until numRead){ 7209c6f1ddSLingrui98 val sram = Module(new SRAMTemplate(gen, FtqSize)) 7309c6f1ddSLingrui98 sram.io.r.req.valid := io.ren(i) 7409c6f1ddSLingrui98 sram.io.r.req.bits.setIdx := io.raddr(i) 7509c6f1ddSLingrui98 io.rdata(i) := sram.io.r.resp.data(0) 7609c6f1ddSLingrui98 sram.io.w.req.valid := io.wen 7709c6f1ddSLingrui98 sram.io.w.req.bits.setIdx := io.waddr 7809c6f1ddSLingrui98 sram.io.w.req.bits.data := VecInit(io.wdata) 7909c6f1ddSLingrui98 } 8009c6f1ddSLingrui98 8109c6f1ddSLingrui98} 8209c6f1ddSLingrui98 8309c6f1ddSLingrui98class Ftq_RF_Components(implicit p: Parameters) extends XSBundle with BPUUtils { 8409c6f1ddSLingrui98 val startAddr = UInt(VAddrBits.W) 85b37e4b45SLingrui98 val nextLineAddr = UInt(VAddrBits.W) 8609c6f1ddSLingrui98 val isNextMask = Vec(PredictWidth, Bool()) 87b37e4b45SLingrui98 val fallThruError = Bool() 88b37e4b45SLingrui98 // val carry = Bool() 8909c6f1ddSLingrui98 def getPc(offset: UInt) = { 9085215037SLingrui98 def getHigher(pc: UInt) = pc(VAddrBits-1, log2Ceil(PredictWidth)+instOffsetBits+1) 9185215037SLingrui98 def getOffset(pc: UInt) = pc(log2Ceil(PredictWidth)+instOffsetBits, instOffsetBits) 92b37e4b45SLingrui98 Cat(getHigher(Mux(isNextMask(offset) && startAddr(log2Ceil(PredictWidth)+instOffsetBits), nextLineAddr, startAddr)), 9309c6f1ddSLingrui98 getOffset(startAddr)+offset, 0.U(instOffsetBits.W)) 9409c6f1ddSLingrui98 } 9509c6f1ddSLingrui98 def fromBranchPrediction(resp: BranchPredictionBundle) = { 96a229ab6cSLingrui98 def carryPos(addr: UInt) = addr(instOffsetBits+log2Ceil(PredictWidth)+1) 97adc0b8dfSGuokai Chen this.startAddr := resp.pc(3) 98adc0b8dfSGuokai Chen this.nextLineAddr := resp.pc(3) + (FetchWidth * 4 * 2).U // may be broken on other configs 9909c6f1ddSLingrui98 this.isNextMask := VecInit((0 until PredictWidth).map(i => 100935edac4STang Haojin (resp.pc(3)(log2Ceil(PredictWidth), 1) +& i.U)(log2Ceil(PredictWidth)).asBool 10109c6f1ddSLingrui98 )) 102adc0b8dfSGuokai Chen this.fallThruError := resp.fallThruError(3) 10309c6f1ddSLingrui98 this 10409c6f1ddSLingrui98 } 10509c6f1ddSLingrui98 override def toPrintable: Printable = { 106b37e4b45SLingrui98 p"startAddr:${Hexadecimal(startAddr)}" 10709c6f1ddSLingrui98 } 10809c6f1ddSLingrui98} 10909c6f1ddSLingrui98 11009c6f1ddSLingrui98class Ftq_pd_Entry(implicit p: Parameters) extends XSBundle { 11109c6f1ddSLingrui98 val brMask = Vec(PredictWidth, Bool()) 11209c6f1ddSLingrui98 val jmpInfo = ValidUndirectioned(Vec(3, Bool())) 11309c6f1ddSLingrui98 val jmpOffset = UInt(log2Ceil(PredictWidth).W) 11409c6f1ddSLingrui98 val jalTarget = UInt(VAddrBits.W) 11509c6f1ddSLingrui98 val rvcMask = Vec(PredictWidth, Bool()) 11609c6f1ddSLingrui98 def hasJal = jmpInfo.valid && !jmpInfo.bits(0) 11709c6f1ddSLingrui98 def hasJalr = jmpInfo.valid && jmpInfo.bits(0) 11809c6f1ddSLingrui98 def hasCall = jmpInfo.valid && jmpInfo.bits(1) 11909c6f1ddSLingrui98 def hasRet = jmpInfo.valid && jmpInfo.bits(2) 12009c6f1ddSLingrui98 12109c6f1ddSLingrui98 def fromPdWb(pdWb: PredecodeWritebackBundle) = { 12209c6f1ddSLingrui98 val pds = pdWb.pd 12309c6f1ddSLingrui98 this.brMask := VecInit(pds.map(pd => pd.isBr && pd.valid)) 12409c6f1ddSLingrui98 this.jmpInfo.valid := VecInit(pds.map(pd => (pd.isJal || pd.isJalr) && pd.valid)).asUInt.orR 12509c6f1ddSLingrui98 this.jmpInfo.bits := ParallelPriorityMux(pds.map(pd => (pd.isJal || pd.isJalr) && pd.valid), 12609c6f1ddSLingrui98 pds.map(pd => VecInit(pd.isJalr, pd.isCall, pd.isRet))) 12709c6f1ddSLingrui98 this.jmpOffset := ParallelPriorityEncoder(pds.map(pd => (pd.isJal || pd.isJalr) && pd.valid)) 12809c6f1ddSLingrui98 this.rvcMask := VecInit(pds.map(pd => pd.isRVC)) 12909c6f1ddSLingrui98 this.jalTarget := pdWb.jalTarget 13009c6f1ddSLingrui98 } 13109c6f1ddSLingrui98 13209c6f1ddSLingrui98 def toPd(offset: UInt) = { 13309c6f1ddSLingrui98 require(offset.getWidth == log2Ceil(PredictWidth)) 13409c6f1ddSLingrui98 val pd = Wire(new PreDecodeInfo) 13509c6f1ddSLingrui98 pd.valid := true.B 13609c6f1ddSLingrui98 pd.isRVC := rvcMask(offset) 13709c6f1ddSLingrui98 val isBr = brMask(offset) 13809c6f1ddSLingrui98 val isJalr = offset === jmpOffset && jmpInfo.valid && jmpInfo.bits(0) 13909c6f1ddSLingrui98 pd.brType := Cat(offset === jmpOffset && jmpInfo.valid, isJalr || isBr) 14009c6f1ddSLingrui98 pd.isCall := offset === jmpOffset && jmpInfo.valid && jmpInfo.bits(1) 14109c6f1ddSLingrui98 pd.isRet := offset === jmpOffset && jmpInfo.valid && jmpInfo.bits(2) 14209c6f1ddSLingrui98 pd 14309c6f1ddSLingrui98 } 14409c6f1ddSLingrui98} 14509c6f1ddSLingrui98 146f9c51548Sssszwicclass PrefetchPtrDB(implicit p: Parameters) extends Bundle { 147f9c51548Sssszwic val fromFtqPtr = UInt(log2Up(p(XSCoreParamsKey).FtqSize).W) 148f9c51548Sssszwic val fromIfuPtr = UInt(log2Up(p(XSCoreParamsKey).FtqSize).W) 149f9c51548Sssszwic} 15009c6f1ddSLingrui98 1513711cf36S小造xu_zhclass Ftq_Redirect_SRAMEntry(implicit p: Parameters) extends SpeculativeInfo { 152abdc3a32Sxu_zh val sc_disagree = if (!env.FPGAPlatform) Some(Vec(numBr, Bool())) else None 1533711cf36S小造xu_zh} 15409c6f1ddSLingrui98 15509c6f1ddSLingrui98class Ftq_1R_SRAMEntry(implicit p: Parameters) extends XSBundle with HasBPUConst { 15609c6f1ddSLingrui98 val meta = UInt(MaxMetaLength.W) 157deb3a97eSGao-Zeyu val ftb_entry = new FTBEntry 15809c6f1ddSLingrui98} 15909c6f1ddSLingrui98 16009c6f1ddSLingrui98class Ftq_Pred_Info(implicit p: Parameters) extends XSBundle { 16109c6f1ddSLingrui98 val target = UInt(VAddrBits.W) 16209c6f1ddSLingrui98 val cfiIndex = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 16309c6f1ddSLingrui98} 16409c6f1ddSLingrui98 16509c6f1ddSLingrui98 16609c6f1ddSLingrui98class FtqRead[T <: Data](private val gen: T)(implicit p: Parameters) extends XSBundle { 1679477429fSsinceforYy val vld = Output(Bool()) 16809c6f1ddSLingrui98 val ptr = Output(new FtqPtr) 16909c6f1ddSLingrui98 val offset = Output(UInt(log2Ceil(PredictWidth).W)) 17009c6f1ddSLingrui98 val data = Input(gen) 1719477429fSsinceforYy def apply(vld: Bool, ptr: FtqPtr, offset: UInt) = { 1729477429fSsinceforYy this.vld := vld 17309c6f1ddSLingrui98 this.ptr := ptr 17409c6f1ddSLingrui98 this.offset := offset 17509c6f1ddSLingrui98 this.data 17609c6f1ddSLingrui98 } 17709c6f1ddSLingrui98} 17809c6f1ddSLingrui98 17909c6f1ddSLingrui98 18009c6f1ddSLingrui98class FtqToBpuIO(implicit p: Parameters) extends XSBundle { 18109c6f1ddSLingrui98 val redirect = Valid(new BranchPredictionRedirect) 18209c6f1ddSLingrui98 val update = Valid(new BranchPredictionUpdate) 18309c6f1ddSLingrui98 val enq_ptr = Output(new FtqPtr) 184*fd3aa057SYuandongliang val redirctFromIFU = Output(Bool()) 18509c6f1ddSLingrui98} 18609c6f1ddSLingrui98 18709c6f1ddSLingrui98class FtqToIfuIO(implicit p: Parameters) extends XSBundle with HasCircularQueuePtrHelper { 18809c6f1ddSLingrui98 val req = Decoupled(new FetchRequestBundle) 189d2b20d1aSTang Haojin val redirect = Valid(new BranchPredictionRedirect) 190d2b20d1aSTang Haojin val topdown_redirect = Valid(new BranchPredictionRedirect) 19109c6f1ddSLingrui98 val flushFromBpu = new Bundle { 19209c6f1ddSLingrui98 // when ifu pipeline is not stalled, 19309c6f1ddSLingrui98 // a packet from bpu s3 can reach f1 at most 19409c6f1ddSLingrui98 val s2 = Valid(new FtqPtr) 195cb4f77ceSLingrui98 val s3 = Valid(new FtqPtr) 19609c6f1ddSLingrui98 def shouldFlushBy(src: Valid[FtqPtr], idx_to_flush: FtqPtr) = { 19709c6f1ddSLingrui98 src.valid && !isAfter(src.bits, idx_to_flush) 19809c6f1ddSLingrui98 } 19909c6f1ddSLingrui98 def shouldFlushByStage2(idx: FtqPtr) = shouldFlushBy(s2, idx) 200cb4f77ceSLingrui98 def shouldFlushByStage3(idx: FtqPtr) = shouldFlushBy(s3, idx) 20109c6f1ddSLingrui98 } 20209c6f1ddSLingrui98} 20309c6f1ddSLingrui98 204c5c5edaeSJeniusclass FtqToICacheIO(implicit p: Parameters) extends XSBundle with HasCircularQueuePtrHelper { 205c5c5edaeSJenius //NOTE: req.bits must be prepare in T cycle 206c5c5edaeSJenius // while req.valid is set true in T + 1 cycle 207c5c5edaeSJenius val req = Decoupled(new FtqToICacheRequestBundle) 208c5c5edaeSJenius} 209c5c5edaeSJenius 21009c6f1ddSLingrui98trait HasBackendRedirectInfo extends HasXSParameter { 21109c6f1ddSLingrui98 def isLoadReplay(r: Valid[Redirect]) = r.bits.flushItself() 21209c6f1ddSLingrui98} 21309c6f1ddSLingrui98 21409c6f1ddSLingrui98class FtqToCtrlIO(implicit p: Parameters) extends XSBundle with HasBackendRedirectInfo { 215b56f947eSYinan Xu // write to backend pc mem 216b56f947eSYinan Xu val pc_mem_wen = Output(Bool()) 217b56f947eSYinan Xu val pc_mem_waddr = Output(UInt(log2Ceil(FtqSize).W)) 218b56f947eSYinan Xu val pc_mem_wdata = Output(new Ftq_RF_Components) 219873dc383SLingrui98 // newest target 2206022c595SsinceforYy val newest_entry_en = Output(Bool()) 221873dc383SLingrui98 val newest_entry_target = Output(UInt(VAddrBits.W)) 222873dc383SLingrui98 val newest_entry_ptr = Output(new FtqPtr) 22309c6f1ddSLingrui98} 22409c6f1ddSLingrui98 22509c6f1ddSLingrui98 22609c6f1ddSLingrui98class FTBEntryGen(implicit p: Parameters) extends XSModule with HasBackendRedirectInfo with HasBPUParameter { 22709c6f1ddSLingrui98 val io = IO(new Bundle { 22809c6f1ddSLingrui98 val start_addr = Input(UInt(VAddrBits.W)) 22909c6f1ddSLingrui98 val old_entry = Input(new FTBEntry) 23009c6f1ddSLingrui98 val pd = Input(new Ftq_pd_Entry) 23109c6f1ddSLingrui98 val cfiIndex = Flipped(Valid(UInt(log2Ceil(PredictWidth).W))) 23209c6f1ddSLingrui98 val target = Input(UInt(VAddrBits.W)) 23309c6f1ddSLingrui98 val hit = Input(Bool()) 23409c6f1ddSLingrui98 val mispredict_vec = Input(Vec(PredictWidth, Bool())) 23509c6f1ddSLingrui98 23609c6f1ddSLingrui98 val new_entry = Output(new FTBEntry) 23709c6f1ddSLingrui98 val new_br_insert_pos = Output(Vec(numBr, Bool())) 23809c6f1ddSLingrui98 val taken_mask = Output(Vec(numBr, Bool())) 239803124a6SLingrui98 val jmp_taken = Output(Bool()) 24009c6f1ddSLingrui98 val mispred_mask = Output(Vec(numBr+1, Bool())) 24109c6f1ddSLingrui98 24209c6f1ddSLingrui98 // for perf counters 24309c6f1ddSLingrui98 val is_init_entry = Output(Bool()) 24409c6f1ddSLingrui98 val is_old_entry = Output(Bool()) 24509c6f1ddSLingrui98 val is_new_br = Output(Bool()) 24609c6f1ddSLingrui98 val is_jalr_target_modified = Output(Bool()) 24709c6f1ddSLingrui98 val is_always_taken_modified = Output(Bool()) 24809c6f1ddSLingrui98 val is_br_full = Output(Bool()) 24909c6f1ddSLingrui98 }) 25009c6f1ddSLingrui98 25109c6f1ddSLingrui98 // no mispredictions detected at predecode 25209c6f1ddSLingrui98 val hit = io.hit 25309c6f1ddSLingrui98 val pd = io.pd 25409c6f1ddSLingrui98 25509c6f1ddSLingrui98 val init_entry = WireInit(0.U.asTypeOf(new FTBEntry)) 25609c6f1ddSLingrui98 25709c6f1ddSLingrui98 25809c6f1ddSLingrui98 val cfi_is_br = pd.brMask(io.cfiIndex.bits) && io.cfiIndex.valid 25909c6f1ddSLingrui98 val entry_has_jmp = pd.jmpInfo.valid 26009c6f1ddSLingrui98 val new_jmp_is_jal = entry_has_jmp && !pd.jmpInfo.bits(0) && io.cfiIndex.valid 26109c6f1ddSLingrui98 val new_jmp_is_jalr = entry_has_jmp && pd.jmpInfo.bits(0) && io.cfiIndex.valid 26209c6f1ddSLingrui98 val new_jmp_is_call = entry_has_jmp && pd.jmpInfo.bits(1) && io.cfiIndex.valid 26309c6f1ddSLingrui98 val new_jmp_is_ret = entry_has_jmp && pd.jmpInfo.bits(2) && io.cfiIndex.valid 26409c6f1ddSLingrui98 val last_jmp_rvi = entry_has_jmp && pd.jmpOffset === (PredictWidth-1).U && !pd.rvcMask.last 265a60a2901SLingrui98 // val last_br_rvi = cfi_is_br && io.cfiIndex.bits === (PredictWidth-1).U && !pd.rvcMask.last 26609c6f1ddSLingrui98 26709c6f1ddSLingrui98 val cfi_is_jal = io.cfiIndex.bits === pd.jmpOffset && new_jmp_is_jal 26809c6f1ddSLingrui98 val cfi_is_jalr = io.cfiIndex.bits === pd.jmpOffset && new_jmp_is_jalr 26909c6f1ddSLingrui98 270a60a2901SLingrui98 def carryPos = log2Ceil(PredictWidth)+instOffsetBits 27109c6f1ddSLingrui98 def getLower(pc: UInt) = pc(carryPos-1, instOffsetBits) 27209c6f1ddSLingrui98 // if not hit, establish a new entry 27309c6f1ddSLingrui98 init_entry.valid := true.B 27409c6f1ddSLingrui98 // tag is left for ftb to assign 275eeb5ff92SLingrui98 276eeb5ff92SLingrui98 // case br 277eeb5ff92SLingrui98 val init_br_slot = init_entry.getSlotForBr(0) 278eeb5ff92SLingrui98 when (cfi_is_br) { 279eeb5ff92SLingrui98 init_br_slot.valid := true.B 280eeb5ff92SLingrui98 init_br_slot.offset := io.cfiIndex.bits 281b37e4b45SLingrui98 init_br_slot.setLowerStatByTarget(io.start_addr, io.target, numBr == 1) 282eeb5ff92SLingrui98 init_entry.always_taken(0) := true.B // set to always taken on init 283eeb5ff92SLingrui98 } 284eeb5ff92SLingrui98 285eeb5ff92SLingrui98 // case jmp 286eeb5ff92SLingrui98 when (entry_has_jmp) { 287eeb5ff92SLingrui98 init_entry.tailSlot.offset := pd.jmpOffset 288eeb5ff92SLingrui98 init_entry.tailSlot.valid := new_jmp_is_jal || new_jmp_is_jalr 289eeb5ff92SLingrui98 init_entry.tailSlot.setLowerStatByTarget(io.start_addr, Mux(cfi_is_jalr, io.target, pd.jalTarget), isShare=false) 290eeb5ff92SLingrui98 } 291eeb5ff92SLingrui98 29209c6f1ddSLingrui98 val jmpPft = getLower(io.start_addr) +& pd.jmpOffset +& Mux(pd.rvcMask(pd.jmpOffset), 1.U, 2.U) 293a60a2901SLingrui98 init_entry.pftAddr := Mux(entry_has_jmp && !last_jmp_rvi, jmpPft, getLower(io.start_addr)) 294a60a2901SLingrui98 init_entry.carry := Mux(entry_has_jmp && !last_jmp_rvi, jmpPft(carryPos-instOffsetBits), true.B) 29509c6f1ddSLingrui98 init_entry.isJalr := new_jmp_is_jalr 29609c6f1ddSLingrui98 init_entry.isCall := new_jmp_is_call 29709c6f1ddSLingrui98 init_entry.isRet := new_jmp_is_ret 298f4ebc4b2SLingrui98 // that means fall thru points to the middle of an inst 299ae409b75SSteve Gou init_entry.last_may_be_rvi_call := pd.jmpOffset === (PredictWidth-1).U && !pd.rvcMask(pd.jmpOffset) 30009c6f1ddSLingrui98 30109c6f1ddSLingrui98 // if hit, check whether a new cfi(only br is possible) is detected 30209c6f1ddSLingrui98 val oe = io.old_entry 303eeb5ff92SLingrui98 val br_recorded_vec = oe.getBrRecordedVec(io.cfiIndex.bits) 30409c6f1ddSLingrui98 val br_recorded = br_recorded_vec.asUInt.orR 30509c6f1ddSLingrui98 val is_new_br = cfi_is_br && !br_recorded 30609c6f1ddSLingrui98 val new_br_offset = io.cfiIndex.bits 30709c6f1ddSLingrui98 // vec(i) means new br will be inserted BEFORE old br(i) 308eeb5ff92SLingrui98 val allBrSlotsVec = oe.allSlotsForBr 30909c6f1ddSLingrui98 val new_br_insert_onehot = VecInit((0 until numBr).map{ 31009c6f1ddSLingrui98 i => i match { 311eeb5ff92SLingrui98 case 0 => 312eeb5ff92SLingrui98 !allBrSlotsVec(0).valid || new_br_offset < allBrSlotsVec(0).offset 313eeb5ff92SLingrui98 case idx => 314eeb5ff92SLingrui98 allBrSlotsVec(idx-1).valid && new_br_offset > allBrSlotsVec(idx-1).offset && 315eeb5ff92SLingrui98 (!allBrSlotsVec(idx).valid || new_br_offset < allBrSlotsVec(idx).offset) 31609c6f1ddSLingrui98 } 31709c6f1ddSLingrui98 }) 31809c6f1ddSLingrui98 31909c6f1ddSLingrui98 val old_entry_modified = WireInit(io.old_entry) 32009c6f1ddSLingrui98 for (i <- 0 until numBr) { 321eeb5ff92SLingrui98 val slot = old_entry_modified.allSlotsForBr(i) 322eeb5ff92SLingrui98 when (new_br_insert_onehot(i)) { 323eeb5ff92SLingrui98 slot.valid := true.B 324eeb5ff92SLingrui98 slot.offset := new_br_offset 325b37e4b45SLingrui98 slot.setLowerStatByTarget(io.start_addr, io.target, i == numBr-1) 326eeb5ff92SLingrui98 old_entry_modified.always_taken(i) := true.B 327eeb5ff92SLingrui98 }.elsewhen (new_br_offset > oe.allSlotsForBr(i).offset) { 328eeb5ff92SLingrui98 old_entry_modified.always_taken(i) := false.B 329eeb5ff92SLingrui98 // all other fields remain unchanged 330eeb5ff92SLingrui98 }.otherwise { 331eeb5ff92SLingrui98 // case i == 0, remain unchanged 332eeb5ff92SLingrui98 if (i != 0) { 333b37e4b45SLingrui98 val noNeedToMoveFromFormerSlot = (i == numBr-1).B && !oe.brSlots.last.valid 334eeb5ff92SLingrui98 when (!noNeedToMoveFromFormerSlot) { 335eeb5ff92SLingrui98 slot.fromAnotherSlot(oe.allSlotsForBr(i-1)) 336eeb5ff92SLingrui98 old_entry_modified.always_taken(i) := oe.always_taken(i) 33709c6f1ddSLingrui98 } 338eeb5ff92SLingrui98 } 339eeb5ff92SLingrui98 } 340eeb5ff92SLingrui98 } 34109c6f1ddSLingrui98 342eeb5ff92SLingrui98 // two circumstances: 343eeb5ff92SLingrui98 // 1. oe: | br | j |, new br should be in front of j, thus addr of j should be new pft 344eeb5ff92SLingrui98 // 2. oe: | br | br |, new br could be anywhere between, thus new pft is the addr of either 345eeb5ff92SLingrui98 // the previous last br or the new br 346eeb5ff92SLingrui98 val may_have_to_replace = oe.noEmptySlotForNewBr 347eeb5ff92SLingrui98 val pft_need_to_change = is_new_br && may_have_to_replace 34809c6f1ddSLingrui98 // it should either be the given last br or the new br 34909c6f1ddSLingrui98 when (pft_need_to_change) { 350eeb5ff92SLingrui98 val new_pft_offset = 351710a8720SLingrui98 Mux(!new_br_insert_onehot.asUInt.orR, 352710a8720SLingrui98 new_br_offset, oe.allSlotsForBr.last.offset) 353eeb5ff92SLingrui98 354710a8720SLingrui98 // set jmp to invalid 35509c6f1ddSLingrui98 old_entry_modified.pftAddr := getLower(io.start_addr) + new_pft_offset 35609c6f1ddSLingrui98 old_entry_modified.carry := (getLower(io.start_addr) +& new_pft_offset).head(1).asBool 357f4ebc4b2SLingrui98 old_entry_modified.last_may_be_rvi_call := false.B 35809c6f1ddSLingrui98 old_entry_modified.isCall := false.B 35909c6f1ddSLingrui98 old_entry_modified.isRet := false.B 360eeb5ff92SLingrui98 old_entry_modified.isJalr := false.B 36109c6f1ddSLingrui98 } 36209c6f1ddSLingrui98 36309c6f1ddSLingrui98 val old_entry_jmp_target_modified = WireInit(oe) 364710a8720SLingrui98 val old_target = oe.tailSlot.getTarget(io.start_addr) // may be wrong because we store only 20 lowest bits 365b37e4b45SLingrui98 val old_tail_is_jmp = !oe.tailSlot.sharing 366eeb5ff92SLingrui98 val jalr_target_modified = cfi_is_jalr && (old_target =/= io.target) && old_tail_is_jmp // TODO: pass full jalr target 3673bcae573SLingrui98 when (jalr_target_modified) { 36809c6f1ddSLingrui98 old_entry_jmp_target_modified.setByJmpTarget(io.start_addr, io.target) 36909c6f1ddSLingrui98 old_entry_jmp_target_modified.always_taken := 0.U.asTypeOf(Vec(numBr, Bool())) 37009c6f1ddSLingrui98 } 37109c6f1ddSLingrui98 37209c6f1ddSLingrui98 val old_entry_always_taken = WireInit(oe) 37309c6f1ddSLingrui98 val always_taken_modified_vec = Wire(Vec(numBr, Bool())) // whether modified or not 37409c6f1ddSLingrui98 for (i <- 0 until numBr) { 37509c6f1ddSLingrui98 old_entry_always_taken.always_taken(i) := 37609c6f1ddSLingrui98 oe.always_taken(i) && io.cfiIndex.valid && oe.brValids(i) && io.cfiIndex.bits === oe.brOffset(i) 377710a8720SLingrui98 always_taken_modified_vec(i) := oe.always_taken(i) && !old_entry_always_taken.always_taken(i) 37809c6f1ddSLingrui98 } 37909c6f1ddSLingrui98 val always_taken_modified = always_taken_modified_vec.reduce(_||_) 38009c6f1ddSLingrui98 38109c6f1ddSLingrui98 38209c6f1ddSLingrui98 38309c6f1ddSLingrui98 val derived_from_old_entry = 38409c6f1ddSLingrui98 Mux(is_new_br, old_entry_modified, 3853bcae573SLingrui98 Mux(jalr_target_modified, old_entry_jmp_target_modified, old_entry_always_taken)) 38609c6f1ddSLingrui98 38709c6f1ddSLingrui98 38809c6f1ddSLingrui98 io.new_entry := Mux(!hit, init_entry, derived_from_old_entry) 38909c6f1ddSLingrui98 39009c6f1ddSLingrui98 io.new_br_insert_pos := new_br_insert_onehot 39109c6f1ddSLingrui98 io.taken_mask := VecInit((io.new_entry.brOffset zip io.new_entry.brValids).map{ 39209c6f1ddSLingrui98 case (off, v) => io.cfiIndex.bits === off && io.cfiIndex.valid && v 39309c6f1ddSLingrui98 }) 394803124a6SLingrui98 io.jmp_taken := io.new_entry.jmpValid && io.new_entry.tailSlot.offset === io.cfiIndex.bits 39509c6f1ddSLingrui98 for (i <- 0 until numBr) { 39609c6f1ddSLingrui98 io.mispred_mask(i) := io.new_entry.brValids(i) && io.mispredict_vec(io.new_entry.brOffset(i)) 39709c6f1ddSLingrui98 } 39809c6f1ddSLingrui98 io.mispred_mask.last := io.new_entry.jmpValid && io.mispredict_vec(pd.jmpOffset) 39909c6f1ddSLingrui98 40009c6f1ddSLingrui98 // for perf counters 40109c6f1ddSLingrui98 io.is_init_entry := !hit 4023bcae573SLingrui98 io.is_old_entry := hit && !is_new_br && !jalr_target_modified && !always_taken_modified 40309c6f1ddSLingrui98 io.is_new_br := hit && is_new_br 4043bcae573SLingrui98 io.is_jalr_target_modified := hit && jalr_target_modified 40509c6f1ddSLingrui98 io.is_always_taken_modified := hit && always_taken_modified 406eeb5ff92SLingrui98 io.is_br_full := hit && is_new_br && may_have_to_replace 40709c6f1ddSLingrui98} 40809c6f1ddSLingrui98 409c5c5edaeSJeniusclass FtqPcMemWrapper(numOtherReads: Int)(implicit p: Parameters) extends XSModule with HasBackendRedirectInfo { 410c5c5edaeSJenius val io = IO(new Bundle { 411c5c5edaeSJenius val ifuPtr_w = Input(new FtqPtr) 412c5c5edaeSJenius val ifuPtrPlus1_w = Input(new FtqPtr) 4136bf9b30dSLingrui98 val ifuPtrPlus2_w = Input(new FtqPtr) 414c5c5edaeSJenius val commPtr_w = Input(new FtqPtr) 4156bf9b30dSLingrui98 val commPtrPlus1_w = Input(new FtqPtr) 416c5c5edaeSJenius val ifuPtr_rdata = Output(new Ftq_RF_Components) 417c5c5edaeSJenius val ifuPtrPlus1_rdata = Output(new Ftq_RF_Components) 4186bf9b30dSLingrui98 val ifuPtrPlus2_rdata = Output(new Ftq_RF_Components) 419c5c5edaeSJenius val commPtr_rdata = Output(new Ftq_RF_Components) 4206bf9b30dSLingrui98 val commPtrPlus1_rdata = Output(new Ftq_RF_Components) 421c5c5edaeSJenius 422c5c5edaeSJenius val other_raddrs = Input(Vec(numOtherReads, UInt(log2Ceil(FtqSize).W))) 423c5c5edaeSJenius val other_rdatas = Output(Vec(numOtherReads, new Ftq_RF_Components)) 424c5c5edaeSJenius 425c5c5edaeSJenius val wen = Input(Bool()) 426c5c5edaeSJenius val waddr = Input(UInt(log2Ceil(FtqSize).W)) 427c5c5edaeSJenius val wdata = Input(new Ftq_RF_Components) 428c5c5edaeSJenius }) 429c5c5edaeSJenius 4306bf9b30dSLingrui98 val num_pc_read = numOtherReads + 5 431c5c5edaeSJenius val mem = Module(new SyncDataModuleTemplate(new Ftq_RF_Components, FtqSize, 43228f2cf58SLingrui98 num_pc_read, 1, "FtqPC")) 433c5c5edaeSJenius mem.io.wen(0) := io.wen 434c5c5edaeSJenius mem.io.waddr(0) := io.waddr 435c5c5edaeSJenius mem.io.wdata(0) := io.wdata 436c5c5edaeSJenius 4376bf9b30dSLingrui98 // read one cycle ahead for ftq local reads 438c5c5edaeSJenius val raddr_vec = VecInit(io.other_raddrs ++ 43988bc4f90SLingrui98 Seq(io.ifuPtr_w.value, io.ifuPtrPlus1_w.value, io.ifuPtrPlus2_w.value, io.commPtrPlus1_w.value, io.commPtr_w.value)) 440c5c5edaeSJenius 441c5c5edaeSJenius mem.io.raddr := raddr_vec 442c5c5edaeSJenius 4436bf9b30dSLingrui98 io.other_rdatas := mem.io.rdata.dropRight(5) 4446bf9b30dSLingrui98 io.ifuPtr_rdata := mem.io.rdata.dropRight(4).last 4456bf9b30dSLingrui98 io.ifuPtrPlus1_rdata := mem.io.rdata.dropRight(3).last 4466bf9b30dSLingrui98 io.ifuPtrPlus2_rdata := mem.io.rdata.dropRight(2).last 4476bf9b30dSLingrui98 io.commPtrPlus1_rdata := mem.io.rdata.dropRight(1).last 448c5c5edaeSJenius io.commPtr_rdata := mem.io.rdata.last 449c5c5edaeSJenius} 450c5c5edaeSJenius 45109c6f1ddSLingrui98class Ftq(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper 452e30430c2SJay with HasBackendRedirectInfo with BPUUtils with HasBPUConst with HasPerfEvents 453e30430c2SJay with HasICacheParameters{ 45409c6f1ddSLingrui98 val io = IO(new Bundle { 45509c6f1ddSLingrui98 val fromBpu = Flipped(new BpuToFtqIO) 45609c6f1ddSLingrui98 val fromIfu = Flipped(new IfuToFtqIO) 45709c6f1ddSLingrui98 val fromBackend = Flipped(new CtrlToFtqIO) 45809c6f1ddSLingrui98 45909c6f1ddSLingrui98 val toBpu = new FtqToBpuIO 46009c6f1ddSLingrui98 val toIfu = new FtqToIfuIO 461c5c5edaeSJenius val toICache = new FtqToICacheIO 46209c6f1ddSLingrui98 val toBackend = new FtqToCtrlIO 46309c6f1ddSLingrui98 4647052722fSJay val toPrefetch = new FtqPrefechBundle 4657052722fSJay 46609c6f1ddSLingrui98 val bpuInfo = new Bundle { 46709c6f1ddSLingrui98 val bpRight = Output(UInt(XLEN.W)) 46809c6f1ddSLingrui98 val bpWrong = Output(UInt(XLEN.W)) 46909c6f1ddSLingrui98 } 4701d1e6d4dSJenius 4711d1e6d4dSJenius val mmioCommitRead = Flipped(new mmioCommitRead) 472d2b20d1aSTang Haojin 473d2b20d1aSTang Haojin // for perf 474d2b20d1aSTang Haojin val ControlBTBMissBubble = Output(Bool()) 475d2b20d1aSTang Haojin val TAGEMissBubble = Output(Bool()) 476d2b20d1aSTang Haojin val SCMissBubble = Output(Bool()) 477d2b20d1aSTang Haojin val ITTAGEMissBubble = Output(Bool()) 478d2b20d1aSTang Haojin val RASMissBubble = Output(Bool()) 47909c6f1ddSLingrui98 }) 48009c6f1ddSLingrui98 io.bpuInfo := DontCare 48109c6f1ddSLingrui98 482d2b20d1aSTang Haojin val topdown_stage = RegInit(0.U.asTypeOf(new FrontendTopDownBundle)) 483d2b20d1aSTang Haojin // only driven by clock, not valid-ready 484d2b20d1aSTang Haojin topdown_stage := io.fromBpu.resp.bits.topdown_info 485d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info := topdown_stage 486d2b20d1aSTang Haojin 487d2b20d1aSTang Haojin val ifuRedirected = RegInit(VecInit(Seq.fill(FtqSize)(false.B))) 488d2b20d1aSTang Haojin 489bace178aSGao-Zeyu 49042dddaceSXuan Hu // io.fromBackend.ftqIdxAhead: bju(BjuCnt) + ldReplay + exception 49142dddaceSXuan Hu val ftqIdxAhead = VecInit(Seq.tabulate(FtqRedirectAheadNum)(i => io.fromBackend.ftqIdxAhead(i))) // only bju 49242dddaceSXuan Hu val ftqIdxSelOH = io.fromBackend.ftqIdxSelOH.bits(FtqRedirectAheadNum - 1, 0) 493bace178aSGao-Zeyu 494bace178aSGao-Zeyu val aheadValid = ftqIdxAhead.map(_.valid).reduce(_|_) && !io.fromBackend.redirect.valid 495bace178aSGao-Zeyu val realAhdValid = io.fromBackend.redirect.valid && (ftqIdxSelOH > 0.U) && RegNext(aheadValid) 496d2b20d1aSTang Haojin val backendRedirect = Wire(Valid(new BranchPredictionRedirect)) 4971c6fc24aSEaston Man val backendRedirectReg = Wire(Valid(new BranchPredictionRedirect)) 4981c6fc24aSEaston Man backendRedirectReg.valid := RegNext(Mux(realAhdValid, false.B, backendRedirect.valid)) 4991c6fc24aSEaston Man backendRedirectReg.bits := RegEnable(backendRedirect.bits, backendRedirect.valid) 500bace178aSGao-Zeyu val fromBackendRedirect = Wire(Valid(new BranchPredictionRedirect)) 501bace178aSGao-Zeyu fromBackendRedirect := Mux(realAhdValid, backendRedirect, backendRedirectReg) 50209c6f1ddSLingrui98 503df5b4b8eSYinan Xu val stage2Flush = backendRedirect.valid 50409c6f1ddSLingrui98 val backendFlush = stage2Flush || RegNext(stage2Flush) 50509c6f1ddSLingrui98 val ifuFlush = Wire(Bool()) 50609c6f1ddSLingrui98 50709c6f1ddSLingrui98 val flush = stage2Flush || RegNext(stage2Flush) 50809c6f1ddSLingrui98 50909c6f1ddSLingrui98 val allowBpuIn, allowToIfu = WireInit(false.B) 51009c6f1ddSLingrui98 val flushToIfu = !allowToIfu 511bace178aSGao-Zeyu allowBpuIn := !ifuFlush && !backendRedirect.valid && !backendRedirectReg.valid 512bace178aSGao-Zeyu allowToIfu := !ifuFlush && !backendRedirect.valid && !backendRedirectReg.valid 51309c6f1ddSLingrui98 514f56177cbSJenius def copyNum = 5 51589cc69c1STang Haojin val bpuPtr, ifuPtr, ifuWbPtr, commPtr, robCommPtr = RegInit(FtqPtr(false.B, 0.U)) 516c9bc5480SLingrui98 val ifuPtrPlus1 = RegInit(FtqPtr(false.B, 1.U)) 5176bf9b30dSLingrui98 val ifuPtrPlus2 = RegInit(FtqPtr(false.B, 2.U)) 5186bf9b30dSLingrui98 val commPtrPlus1 = RegInit(FtqPtr(false.B, 1.U)) 519f56177cbSJenius val copied_ifu_ptr = Seq.fill(copyNum)(RegInit(FtqPtr(false.B, 0.U))) 520dc270d3bSJenius val copied_bpu_ptr = Seq.fill(copyNum)(RegInit(FtqPtr(false.B, 0.U))) 5216bf9b30dSLingrui98 require(FtqSize >= 4) 522c5c5edaeSJenius val ifuPtr_write = WireInit(ifuPtr) 523c5c5edaeSJenius val ifuPtrPlus1_write = WireInit(ifuPtrPlus1) 5246bf9b30dSLingrui98 val ifuPtrPlus2_write = WireInit(ifuPtrPlus2) 525c5c5edaeSJenius val ifuWbPtr_write = WireInit(ifuWbPtr) 526c5c5edaeSJenius val commPtr_write = WireInit(commPtr) 5276bf9b30dSLingrui98 val commPtrPlus1_write = WireInit(commPtrPlus1) 52889cc69c1STang Haojin val robCommPtr_write = WireInit(robCommPtr) 529c5c5edaeSJenius ifuPtr := ifuPtr_write 530c5c5edaeSJenius ifuPtrPlus1 := ifuPtrPlus1_write 5316bf9b30dSLingrui98 ifuPtrPlus2 := ifuPtrPlus2_write 532c5c5edaeSJenius ifuWbPtr := ifuWbPtr_write 533c5c5edaeSJenius commPtr := commPtr_write 534f83ef67eSLingrui98 commPtrPlus1 := commPtrPlus1_write 535f56177cbSJenius copied_ifu_ptr.map{ptr => 536f56177cbSJenius ptr := ifuPtr_write 537f56177cbSJenius dontTouch(ptr) 538f56177cbSJenius } 53989cc69c1STang Haojin robCommPtr := robCommPtr_write 54009c6f1ddSLingrui98 val validEntries = distanceBetween(bpuPtr, commPtr) 54143aca6c2SGuokai Chen val canCommit = Wire(Bool()) 54209c6f1ddSLingrui98 54309c6f1ddSLingrui98 // ********************************************************************** 54409c6f1ddSLingrui98 // **************************** enq from bpu **************************** 54509c6f1ddSLingrui98 // ********************************************************************** 54643aca6c2SGuokai Chen val new_entry_ready = validEntries < FtqSize.U || canCommit 54709c6f1ddSLingrui98 io.fromBpu.resp.ready := new_entry_ready 54809c6f1ddSLingrui98 54909c6f1ddSLingrui98 val bpu_s2_resp = io.fromBpu.resp.bits.s2 550cb4f77ceSLingrui98 val bpu_s3_resp = io.fromBpu.resp.bits.s3 551adc0b8dfSGuokai Chen val bpu_s2_redirect = bpu_s2_resp.valid(3) && bpu_s2_resp.hasRedirect(3) 552adc0b8dfSGuokai Chen val bpu_s3_redirect = bpu_s3_resp.valid(3) && bpu_s3_resp.hasRedirect(3) 55309c6f1ddSLingrui98 55409c6f1ddSLingrui98 io.toBpu.enq_ptr := bpuPtr 555935edac4STang Haojin val enq_fire = io.fromBpu.resp.fire && allowBpuIn // from bpu s1 556935edac4STang Haojin val bpu_in_fire = (io.fromBpu.resp.fire || bpu_s2_redirect || bpu_s3_redirect) && allowBpuIn 55709c6f1ddSLingrui98 558b37e4b45SLingrui98 val bpu_in_resp = io.fromBpu.resp.bits.selectedResp 559adc0b8dfSGuokai Chen val bpu_in_stage = io.fromBpu.resp.bits.selectedRespIdxForFtq 56009c6f1ddSLingrui98 val bpu_in_resp_ptr = Mux(bpu_in_stage === BP_S1, bpuPtr, bpu_in_resp.ftq_idx) 56109c6f1ddSLingrui98 val bpu_in_resp_idx = bpu_in_resp_ptr.value 56209c6f1ddSLingrui98 563378f00d9SJenius // read ports: prefetchReq ++ ifuReq1 + ifuReq2 + ifuReq3 + commitUpdate2 + commitUpdate 564378f00d9SJenius val ftq_pc_mem = Module(new FtqPcMemWrapper(1)) 5656bf9b30dSLingrui98 // resp from uBTB 566c5c5edaeSJenius ftq_pc_mem.io.wen := bpu_in_fire 567c5c5edaeSJenius ftq_pc_mem.io.waddr := bpu_in_resp_idx 568c5c5edaeSJenius ftq_pc_mem.io.wdata.fromBranchPrediction(bpu_in_resp) 56909c6f1ddSLingrui98 57009c6f1ddSLingrui98 // ifuRedirect + backendRedirect + commit 57116a171eeSEaston Man val ftq_redirect_mem = Module(new SyncDataModuleTemplate(new Ftq_Redirect_SRAMEntry, 57216a171eeSEaston Man FtqSize, 1+FtqRedirectAheadNum+1, 1, hasRen = true)) 57309c6f1ddSLingrui98 // these info is intended to enq at the last stage of bpu 574deb3a97eSGao-Zeyu ftq_redirect_mem.io.wen(0) := io.fromBpu.resp.bits.lastStage.valid(3) 575deb3a97eSGao-Zeyu ftq_redirect_mem.io.waddr(0) := io.fromBpu.resp.bits.lastStage.ftq_idx.value 576deb3a97eSGao-Zeyu ftq_redirect_mem.io.wdata(0) := io.fromBpu.resp.bits.last_stage_spec_info 577deb3a97eSGao-Zeyu println(f"ftq redirect MEM: entry ${ftq_redirect_mem.io.wdata(0).getWidth} * ${FtqSize} * 3") 57809c6f1ddSLingrui98 57909c6f1ddSLingrui98 val ftq_meta_1r_sram = Module(new FtqNRSRAM(new Ftq_1R_SRAMEntry, 1)) 58009c6f1ddSLingrui98 // these info is intended to enq at the last stage of bpu 581adc0b8dfSGuokai Chen ftq_meta_1r_sram.io.wen := io.fromBpu.resp.bits.lastStage.valid(3) 58209c6f1ddSLingrui98 ftq_meta_1r_sram.io.waddr := io.fromBpu.resp.bits.lastStage.ftq_idx.value 583c2d1ec7dSLingrui98 ftq_meta_1r_sram.io.wdata.meta := io.fromBpu.resp.bits.last_stage_meta 584deb3a97eSGao-Zeyu ftq_meta_1r_sram.io.wdata.ftb_entry := io.fromBpu.resp.bits.last_stage_ftb_entry 58509c6f1ddSLingrui98 // ifuRedirect + backendRedirect + commit 586241781f0SEaston Man val ftb_entry_mem = Module(new SyncDataModuleTemplate(new FTBEntry_FtqMem, 587241781f0SEaston Man FtqSize, 1+FtqRedirectAheadNum, 1, hasRen = true)) 588adc0b8dfSGuokai Chen ftb_entry_mem.io.wen(0) := io.fromBpu.resp.bits.lastStage.valid(3) 58909c6f1ddSLingrui98 ftb_entry_mem.io.waddr(0) := io.fromBpu.resp.bits.lastStage.ftq_idx.value 590c2d1ec7dSLingrui98 ftb_entry_mem.io.wdata(0) := io.fromBpu.resp.bits.last_stage_ftb_entry 59109c6f1ddSLingrui98 59209c6f1ddSLingrui98 59309c6f1ddSLingrui98 // multi-write 594b0ed7239SLingrui98 val update_target = Reg(Vec(FtqSize, UInt(VAddrBits.W))) // could be taken target or fallThrough //TODO: remove this 5956bf9b30dSLingrui98 val newest_entry_target = Reg(UInt(VAddrBits.W)) 5961c6fc24aSEaston Man val newest_entry_target_modified = RegInit(false.B) 5976bf9b30dSLingrui98 val newest_entry_ptr = Reg(new FtqPtr) 5981c6fc24aSEaston Man val newest_entry_ptr_modified = RegInit(false.B) 59909c6f1ddSLingrui98 val cfiIndex_vec = Reg(Vec(FtqSize, ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)))) 60009c6f1ddSLingrui98 val mispredict_vec = Reg(Vec(FtqSize, Vec(PredictWidth, Bool()))) 60109c6f1ddSLingrui98 val pred_stage = Reg(Vec(FtqSize, UInt(2.W))) 602209a4cafSSteve Gou val pred_s1_cycle = if (!env.FPGAPlatform) Some(Reg(Vec(FtqSize, UInt(64.W)))) else None 60309c6f1ddSLingrui98 60409c6f1ddSLingrui98 val c_invalid :: c_valid :: c_commited :: Nil = Enum(3) 6051c6fc24aSEaston Man val commitStateQueueReg = RegInit(VecInit(Seq.fill(FtqSize) { 60609c6f1ddSLingrui98 VecInit(Seq.fill(PredictWidth)(c_invalid)) 60709c6f1ddSLingrui98 })) 6081c6fc24aSEaston Man val commitStateQueueEnable = WireInit(VecInit(Seq.fill(FtqSize)(false.B))) 6091c6fc24aSEaston Man val commitStateQueueNext = WireInit(commitStateQueueReg) 6101c6fc24aSEaston Man 6111c6fc24aSEaston Man for (f <- 0 until FtqSize) { 6121c6fc24aSEaston Man when(commitStateQueueEnable(f)) { 6131c6fc24aSEaston Man commitStateQueueReg(f) := commitStateQueueNext(f) 6141c6fc24aSEaston Man } 6151c6fc24aSEaston Man } 61609c6f1ddSLingrui98 61709c6f1ddSLingrui98 val f_to_send :: f_sent :: Nil = Enum(2) 61809c6f1ddSLingrui98 val entry_fetch_status = RegInit(VecInit(Seq.fill(FtqSize)(f_sent))) 61909c6f1ddSLingrui98 62009c6f1ddSLingrui98 val h_not_hit :: h_false_hit :: h_hit :: Nil = Enum(3) 62109c6f1ddSLingrui98 val entry_hit_status = RegInit(VecInit(Seq.fill(FtqSize)(h_not_hit))) 62209c6f1ddSLingrui98 623f63797a4SLingrui98 // modify registers one cycle later to cut critical path 624f63797a4SLingrui98 val last_cycle_bpu_in = RegNext(bpu_in_fire) 6251c6fc24aSEaston Man val last_cycle_bpu_in_ptr = RegEnable(bpu_in_resp_ptr, bpu_in_fire) 6266bf9b30dSLingrui98 val last_cycle_bpu_in_idx = last_cycle_bpu_in_ptr.value 6271c6fc24aSEaston Man val last_cycle_bpu_target = RegEnable(bpu_in_resp.getTarget(3), bpu_in_fire) 6281c6fc24aSEaston Man val last_cycle_cfiIndex = RegEnable(bpu_in_resp.cfiIndex(3), bpu_in_fire) 6291c6fc24aSEaston Man val last_cycle_bpu_in_stage = RegEnable(bpu_in_stage, bpu_in_fire) 630f56177cbSJenius 6317be982afSLingrui98 def extra_copyNum_for_commitStateQueue = 2 6321c6fc24aSEaston Man val copied_last_cycle_bpu_in = 6331c6fc24aSEaston Man VecInit(Seq.fill(copyNum + extra_copyNum_for_commitStateQueue)(RegNext(bpu_in_fire))) 6341c6fc24aSEaston Man val copied_last_cycle_bpu_in_ptr_for_ftq = 6351c6fc24aSEaston Man VecInit(Seq.fill(extra_copyNum_for_commitStateQueue)(RegEnable(bpu_in_resp_ptr, bpu_in_fire))) 636f56177cbSJenius 6371c6fc24aSEaston Man newest_entry_target_modified := false.B 6381c6fc24aSEaston Man newest_entry_ptr_modified := false.B 639f63797a4SLingrui98 when (last_cycle_bpu_in) { 640f63797a4SLingrui98 entry_fetch_status(last_cycle_bpu_in_idx) := f_to_send 641f63797a4SLingrui98 cfiIndex_vec(last_cycle_bpu_in_idx) := last_cycle_cfiIndex 642f63797a4SLingrui98 pred_stage(last_cycle_bpu_in_idx) := last_cycle_bpu_in_stage 6436bf9b30dSLingrui98 644b0ed7239SLingrui98 update_target(last_cycle_bpu_in_idx) := last_cycle_bpu_target // TODO: remove this 6451c6fc24aSEaston Man newest_entry_target_modified := true.B 6466bf9b30dSLingrui98 newest_entry_target := last_cycle_bpu_target 6471c6fc24aSEaston Man newest_entry_ptr_modified := true.B 6486bf9b30dSLingrui98 newest_entry_ptr := last_cycle_bpu_in_ptr 64909c6f1ddSLingrui98 } 65009c6f1ddSLingrui98 6517be982afSLingrui98 // reduce fanout by delay write for a cycle 6527be982afSLingrui98 when (RegNext(last_cycle_bpu_in)) { 6531c6fc24aSEaston Man mispredict_vec(RegEnable(last_cycle_bpu_in_idx, last_cycle_bpu_in)) := 6541c6fc24aSEaston Man WireInit(VecInit(Seq.fill(PredictWidth)(false.B))) 6557be982afSLingrui98 } 6567be982afSLingrui98 657209a4cafSSteve Gou // record s1 pred cycles 658209a4cafSSteve Gou pred_s1_cycle.map(vec => { 659209a4cafSSteve Gou when (bpu_in_fire && (bpu_in_stage === BP_S1)) { 660209a4cafSSteve Gou vec(bpu_in_resp_ptr.value) := bpu_in_resp.full_pred(0).predCycle.getOrElse(0.U) 661209a4cafSSteve Gou } 662209a4cafSSteve Gou }) 663209a4cafSSteve Gou 6647be982afSLingrui98 // reduce fanout using copied last_cycle_bpu_in and copied last_cycle_bpu_in_ptr 6657be982afSLingrui98 val copied_last_cycle_bpu_in_for_ftq = copied_last_cycle_bpu_in.takeRight(extra_copyNum_for_commitStateQueue) 6667be982afSLingrui98 copied_last_cycle_bpu_in_for_ftq.zip(copied_last_cycle_bpu_in_ptr_for_ftq).zipWithIndex.map { 6677be982afSLingrui98 case ((in, ptr), i) => 6687be982afSLingrui98 when (in) { 6697be982afSLingrui98 val perSetEntries = FtqSize / extra_copyNum_for_commitStateQueue // 32 6707be982afSLingrui98 require(FtqSize % extra_copyNum_for_commitStateQueue == 0) 6717be982afSLingrui98 for (j <- 0 until perSetEntries) { 6729361b0c5SLingrui98 when (ptr.value === (i * perSetEntries + j).U) { 6731c6fc24aSEaston Man commitStateQueueNext(i * perSetEntries + j) := VecInit(Seq.fill(PredictWidth)(c_invalid)) 6741c6fc24aSEaston Man // Clock gating optimization, use 1 gate cell to control a row 6751c6fc24aSEaston Man commitStateQueueEnable(i * perSetEntries + j) := true.B 6767be982afSLingrui98 } 6777be982afSLingrui98 } 6787be982afSLingrui98 } 6799361b0c5SLingrui98 } 6807be982afSLingrui98 68109c6f1ddSLingrui98 bpuPtr := bpuPtr + enq_fire 682dc270d3bSJenius copied_bpu_ptr.map(_ := bpuPtr + enq_fire) 683c9bc5480SLingrui98 when (io.toIfu.req.fire && allowToIfu) { 684c5c5edaeSJenius ifuPtr_write := ifuPtrPlus1 6856bf9b30dSLingrui98 ifuPtrPlus1_write := ifuPtrPlus2 6866bf9b30dSLingrui98 ifuPtrPlus2_write := ifuPtrPlus2 + 1.U 687c9bc5480SLingrui98 } 68809c6f1ddSLingrui98 68909c6f1ddSLingrui98 // only use ftb result to assign hit status 690adc0b8dfSGuokai Chen when (bpu_s2_resp.valid(3)) { 691adc0b8dfSGuokai Chen entry_hit_status(bpu_s2_resp.ftq_idx.value) := Mux(bpu_s2_resp.full_pred(3).hit, h_hit, h_not_hit) 69209c6f1ddSLingrui98 } 69309c6f1ddSLingrui98 69409c6f1ddSLingrui98 6952f4a3aa4SLingrui98 io.toIfu.flushFromBpu.s2.valid := bpu_s2_redirect 69609c6f1ddSLingrui98 io.toIfu.flushFromBpu.s2.bits := bpu_s2_resp.ftq_idx 697adc0b8dfSGuokai Chen when (bpu_s2_redirect) { 69809c6f1ddSLingrui98 bpuPtr := bpu_s2_resp.ftq_idx + 1.U 699dc270d3bSJenius copied_bpu_ptr.map(_ := bpu_s2_resp.ftq_idx + 1.U) 70009c6f1ddSLingrui98 // only when ifuPtr runs ahead of bpu s2 resp should we recover it 70109c6f1ddSLingrui98 when (!isBefore(ifuPtr, bpu_s2_resp.ftq_idx)) { 702c5c5edaeSJenius ifuPtr_write := bpu_s2_resp.ftq_idx 703c5c5edaeSJenius ifuPtrPlus1_write := bpu_s2_resp.ftq_idx + 1.U 7046bf9b30dSLingrui98 ifuPtrPlus2_write := bpu_s2_resp.ftq_idx + 2.U 70509c6f1ddSLingrui98 } 70609c6f1ddSLingrui98 } 70709c6f1ddSLingrui98 708cb4f77ceSLingrui98 io.toIfu.flushFromBpu.s3.valid := bpu_s3_redirect 709cb4f77ceSLingrui98 io.toIfu.flushFromBpu.s3.bits := bpu_s3_resp.ftq_idx 710adc0b8dfSGuokai Chen when (bpu_s3_redirect) { 711cb4f77ceSLingrui98 bpuPtr := bpu_s3_resp.ftq_idx + 1.U 712dc270d3bSJenius copied_bpu_ptr.map(_ := bpu_s3_resp.ftq_idx + 1.U) 713cb4f77ceSLingrui98 // only when ifuPtr runs ahead of bpu s2 resp should we recover it 714cb4f77ceSLingrui98 when (!isBefore(ifuPtr, bpu_s3_resp.ftq_idx)) { 715c5c5edaeSJenius ifuPtr_write := bpu_s3_resp.ftq_idx 716c5c5edaeSJenius ifuPtrPlus1_write := bpu_s3_resp.ftq_idx + 1.U 7176bf9b30dSLingrui98 ifuPtrPlus2_write := bpu_s3_resp.ftq_idx + 2.U 718cb4f77ceSLingrui98 } 719cb4f77ceSLingrui98 } 720cb4f77ceSLingrui98 72109c6f1ddSLingrui98 XSError(isBefore(bpuPtr, ifuPtr) && !isFull(bpuPtr, ifuPtr), "\nifuPtr is before bpuPtr!\n") 7222448f137SGuokai Chen XSError(isBefore(ifuWbPtr, commPtr) && !isFull(ifuWbPtr, commPtr), "\ncommPtr is before ifuWbPtr!\n") 72309c6f1ddSLingrui98 724dc270d3bSJenius (0 until copyNum).map{i => 725dc270d3bSJenius XSError(copied_bpu_ptr(i) =/= bpuPtr, "\ncopiedBpuPtr is different from bpuPtr!\n") 726dc270d3bSJenius } 727dc270d3bSJenius 72809c6f1ddSLingrui98 // **************************************************************** 72909c6f1ddSLingrui98 // **************************** to ifu **************************** 73009c6f1ddSLingrui98 // **************************************************************** 731f22cf846SJenius // 0 for ifu, and 1-4 for ICache 732935edac4STang Haojin val bpu_in_bypass_buf = RegEnable(ftq_pc_mem.io.wdata, bpu_in_fire) 733935edac4STang Haojin val copied_bpu_in_bypass_buf = VecInit(Seq.fill(copyNum)(RegEnable(ftq_pc_mem.io.wdata, bpu_in_fire))) 734f56177cbSJenius val bpu_in_bypass_buf_for_ifu = bpu_in_bypass_buf 7351c6fc24aSEaston Man val bpu_in_bypass_ptr = RegEnable(bpu_in_resp_ptr, bpu_in_fire) 73609c6f1ddSLingrui98 val last_cycle_to_ifu_fire = RegNext(io.toIfu.req.fire) 73709c6f1ddSLingrui98 7381c6fc24aSEaston Man val copied_bpu_in_bypass_ptr = VecInit(Seq.fill(copyNum)(RegEnable(bpu_in_resp_ptr, bpu_in_fire))) 739f56177cbSJenius val copied_last_cycle_to_ifu_fire = VecInit(Seq.fill(copyNum)(RegNext(io.toIfu.req.fire))) 74088bc4f90SLingrui98 74109c6f1ddSLingrui98 // read pc and target 7426bf9b30dSLingrui98 ftq_pc_mem.io.ifuPtr_w := ifuPtr_write 7436bf9b30dSLingrui98 ftq_pc_mem.io.ifuPtrPlus1_w := ifuPtrPlus1_write 7446bf9b30dSLingrui98 ftq_pc_mem.io.ifuPtrPlus2_w := ifuPtrPlus2_write 7456bf9b30dSLingrui98 ftq_pc_mem.io.commPtr_w := commPtr_write 7466bf9b30dSLingrui98 ftq_pc_mem.io.commPtrPlus1_w := commPtrPlus1_write 747c5c5edaeSJenius 74809c6f1ddSLingrui98 7495ff19bd8SLingrui98 io.toIfu.req.bits.ftqIdx := ifuPtr 750f63797a4SLingrui98 751f56177cbSJenius val toICachePcBundle = Wire(Vec(copyNum,new Ftq_RF_Components)) 752dc270d3bSJenius val toICacheEntryToSend = Wire(Vec(copyNum,Bool())) 753b37e4b45SLingrui98 val toIfuPcBundle = Wire(new Ftq_RF_Components) 754f63797a4SLingrui98 val entry_is_to_send = WireInit(entry_fetch_status(ifuPtr.value) === f_to_send) 755f63797a4SLingrui98 val entry_ftq_offset = WireInit(cfiIndex_vec(ifuPtr.value)) 7566bf9b30dSLingrui98 val entry_next_addr = Wire(UInt(VAddrBits.W)) 757b004fa13SJenius 758f56177cbSJenius val pc_mem_ifu_ptr_rdata = VecInit(Seq.fill(copyNum)(RegNext(ftq_pc_mem.io.ifuPtr_rdata))) 759f56177cbSJenius val pc_mem_ifu_plus1_rdata = VecInit(Seq.fill(copyNum)(RegNext(ftq_pc_mem.io.ifuPtrPlus1_rdata))) 760b0ed7239SLingrui98 val diff_entry_next_addr = WireInit(update_target(ifuPtr.value)) //TODO: remove this 761f63797a4SLingrui98 762dc270d3bSJenius val copied_ifu_plus1_to_send = VecInit(Seq.fill(copyNum)(RegNext(entry_fetch_status(ifuPtrPlus1.value) === f_to_send) || RegNext(last_cycle_bpu_in && bpu_in_bypass_ptr === (ifuPtrPlus1)))) 763dc270d3bSJenius val copied_ifu_ptr_to_send = VecInit(Seq.fill(copyNum)(RegNext(entry_fetch_status(ifuPtr.value) === f_to_send) || RegNext(last_cycle_bpu_in && bpu_in_bypass_ptr === ifuPtr))) 764dc270d3bSJenius 765f56177cbSJenius for(i <- 0 until copyNum){ 766f56177cbSJenius when(copied_last_cycle_bpu_in(i) && copied_bpu_in_bypass_ptr(i) === copied_ifu_ptr(i)){ 767f56177cbSJenius toICachePcBundle(i) := copied_bpu_in_bypass_buf(i) 768dc270d3bSJenius toICacheEntryToSend(i) := true.B 769f56177cbSJenius }.elsewhen(copied_last_cycle_to_ifu_fire(i)){ 770f56177cbSJenius toICachePcBundle(i) := pc_mem_ifu_plus1_rdata(i) 771dc270d3bSJenius toICacheEntryToSend(i) := copied_ifu_plus1_to_send(i) 772f56177cbSJenius }.otherwise{ 773f56177cbSJenius toICachePcBundle(i) := pc_mem_ifu_ptr_rdata(i) 774dc270d3bSJenius toICacheEntryToSend(i) := copied_ifu_ptr_to_send(i) 775f56177cbSJenius } 776f56177cbSJenius } 777f56177cbSJenius 778873dc383SLingrui98 // TODO: reconsider target address bypass logic 77909c6f1ddSLingrui98 when (last_cycle_bpu_in && bpu_in_bypass_ptr === ifuPtr) { 78088bc4f90SLingrui98 toIfuPcBundle := bpu_in_bypass_buf_for_ifu 781f678dd91SSteve Gou entry_is_to_send := true.B 7826bf9b30dSLingrui98 entry_next_addr := last_cycle_bpu_target 783f63797a4SLingrui98 entry_ftq_offset := last_cycle_cfiIndex 784b0ed7239SLingrui98 diff_entry_next_addr := last_cycle_bpu_target // TODO: remove this 78509c6f1ddSLingrui98 }.elsewhen (last_cycle_to_ifu_fire) { 786c5c5edaeSJenius toIfuPcBundle := RegNext(ftq_pc_mem.io.ifuPtrPlus1_rdata) 787c5c5edaeSJenius entry_is_to_send := RegNext(entry_fetch_status(ifuPtrPlus1.value) === f_to_send) || 788c5c5edaeSJenius RegNext(last_cycle_bpu_in && bpu_in_bypass_ptr === (ifuPtrPlus1)) // reduce potential bubbles 789ed434d67SLingrui98 entry_next_addr := Mux(last_cycle_bpu_in && bpu_in_bypass_ptr === (ifuPtrPlus1), 79088bc4f90SLingrui98 bpu_in_bypass_buf_for_ifu.startAddr, 791fef810c0SLingrui98 Mux(ifuPtr === newest_entry_ptr, 7926bf9b30dSLingrui98 newest_entry_target, 793f83ef67eSLingrui98 RegNext(ftq_pc_mem.io.ifuPtrPlus2_rdata.startAddr))) // ifuPtr+2 794c5c5edaeSJenius }.otherwise { 795c5c5edaeSJenius toIfuPcBundle := RegNext(ftq_pc_mem.io.ifuPtr_rdata) 79628f2cf58SLingrui98 entry_is_to_send := RegNext(entry_fetch_status(ifuPtr.value) === f_to_send) || 79728f2cf58SLingrui98 RegNext(last_cycle_bpu_in && bpu_in_bypass_ptr === ifuPtr) // reduce potential bubbles 7986bf9b30dSLingrui98 entry_next_addr := Mux(last_cycle_bpu_in && bpu_in_bypass_ptr === (ifuPtrPlus1), 79988bc4f90SLingrui98 bpu_in_bypass_buf_for_ifu.startAddr, 800fef810c0SLingrui98 Mux(ifuPtr === newest_entry_ptr, 8016bf9b30dSLingrui98 newest_entry_target, 802f83ef67eSLingrui98 RegNext(ftq_pc_mem.io.ifuPtrPlus1_rdata.startAddr))) // ifuPtr+1 80309c6f1ddSLingrui98 } 80409c6f1ddSLingrui98 805f678dd91SSteve Gou io.toIfu.req.valid := entry_is_to_send && ifuPtr =/= bpuPtr 806f63797a4SLingrui98 io.toIfu.req.bits.nextStartAddr := entry_next_addr 807f63797a4SLingrui98 io.toIfu.req.bits.ftqOffset := entry_ftq_offset 808b37e4b45SLingrui98 io.toIfu.req.bits.fromFtqPcBundle(toIfuPcBundle) 809c5c5edaeSJenius 810c5c5edaeSJenius io.toICache.req.valid := entry_is_to_send && ifuPtr =/= bpuPtr 811dc270d3bSJenius io.toICache.req.bits.readValid.zipWithIndex.map{case(copy, i) => copy := toICacheEntryToSend(i) && copied_ifu_ptr(i) =/= copied_bpu_ptr(i)} 812b004fa13SJenius io.toICache.req.bits.pcMemRead.zipWithIndex.map{case(copy,i) => copy.fromFtqPcBundle(toICachePcBundle(i))} 813b004fa13SJenius // io.toICache.req.bits.bypassSelect := last_cycle_bpu_in && bpu_in_bypass_ptr === ifuPtr 814b004fa13SJenius // io.toICache.req.bits.bpuBypassWrite.zipWithIndex.map{case(bypassWrtie, i) => 815b004fa13SJenius // bypassWrtie.startAddr := bpu_in_bypass_buf.tail(i).startAddr 816b004fa13SJenius // bypassWrtie.nextlineStart := bpu_in_bypass_buf.tail(i).nextLineAddr 817b004fa13SJenius // } 818f22cf846SJenius 819b0ed7239SLingrui98 // TODO: remove this 820b0ed7239SLingrui98 XSError(io.toIfu.req.valid && diff_entry_next_addr =/= entry_next_addr, 8215a674179SLingrui98 p"\nifu_req_target wrong! ifuPtr: ${ifuPtr}, entry_next_addr: ${Hexadecimal(entry_next_addr)} diff_entry_next_addr: ${Hexadecimal(diff_entry_next_addr)}\n") 822b0ed7239SLingrui98 82309c6f1ddSLingrui98 // when fall through is smaller in value than start address, there must be a false hit 824b37e4b45SLingrui98 when (toIfuPcBundle.fallThruError && entry_hit_status(ifuPtr.value) === h_hit) { 82509c6f1ddSLingrui98 when (io.toIfu.req.fire && 826cb4f77ceSLingrui98 !(bpu_s2_redirect && bpu_s2_resp.ftq_idx === ifuPtr) && 827cb4f77ceSLingrui98 !(bpu_s3_redirect && bpu_s3_resp.ftq_idx === ifuPtr) 82809c6f1ddSLingrui98 ) { 82909c6f1ddSLingrui98 entry_hit_status(ifuPtr.value) := h_false_hit 830352db50aSLingrui98 // XSError(true.B, "FTB false hit by fallThroughError, startAddr: %x, fallTHru: %x\n", io.toIfu.req.bits.startAddr, io.toIfu.req.bits.nextStartAddr) 83109c6f1ddSLingrui98 } 832b37e4b45SLingrui98 XSDebug(true.B, "fallThruError! start:%x, fallThru:%x\n", io.toIfu.req.bits.startAddr, io.toIfu.req.bits.nextStartAddr) 83309c6f1ddSLingrui98 } 83409c6f1ddSLingrui98 835a60a2901SLingrui98 XSPerfAccumulate(f"fall_through_error_to_ifu", toIfuPcBundle.fallThruError && entry_hit_status(ifuPtr.value) === h_hit && 836a60a2901SLingrui98 io.toIfu.req.fire && !(bpu_s2_redirect && bpu_s2_resp.ftq_idx === ifuPtr) && !(bpu_s3_redirect && bpu_s3_resp.ftq_idx === ifuPtr)) 837a60a2901SLingrui98 83809c6f1ddSLingrui98 val ifu_req_should_be_flushed = 839cb4f77ceSLingrui98 io.toIfu.flushFromBpu.shouldFlushByStage2(io.toIfu.req.bits.ftqIdx) || 840cb4f77ceSLingrui98 io.toIfu.flushFromBpu.shouldFlushByStage3(io.toIfu.req.bits.ftqIdx) 84109c6f1ddSLingrui98 84209c6f1ddSLingrui98 when (io.toIfu.req.fire && !ifu_req_should_be_flushed) { 84309c6f1ddSLingrui98 entry_fetch_status(ifuPtr.value) := f_sent 84409c6f1ddSLingrui98 } 84509c6f1ddSLingrui98 84609c6f1ddSLingrui98 // ********************************************************************* 84709c6f1ddSLingrui98 // **************************** wb from ifu **************************** 84809c6f1ddSLingrui98 // ********************************************************************* 84909c6f1ddSLingrui98 val pdWb = io.fromIfu.pdWb 85009c6f1ddSLingrui98 val pds = pdWb.bits.pd 85109c6f1ddSLingrui98 val ifu_wb_valid = pdWb.valid 85209c6f1ddSLingrui98 val ifu_wb_idx = pdWb.bits.ftqIdx.value 85309c6f1ddSLingrui98 // read ports: commit update 8541c6fc24aSEaston Man val ftq_pd_mem = Module(new SyncDataModuleTemplate(new Ftq_pd_Entry, FtqSize, 1, 1, hasRen = true)) 85509c6f1ddSLingrui98 ftq_pd_mem.io.wen(0) := ifu_wb_valid 85609c6f1ddSLingrui98 ftq_pd_mem.io.waddr(0) := pdWb.bits.ftqIdx.value 85709c6f1ddSLingrui98 ftq_pd_mem.io.wdata(0).fromPdWb(pdWb.bits) 85809c6f1ddSLingrui98 85909c6f1ddSLingrui98 val hit_pd_valid = entry_hit_status(ifu_wb_idx) === h_hit && ifu_wb_valid 86009c6f1ddSLingrui98 val hit_pd_mispred = hit_pd_valid && pdWb.bits.misOffset.valid 86109c6f1ddSLingrui98 val hit_pd_mispred_reg = RegNext(hit_pd_mispred, init=false.B) 862005e809bSJiuyang Liu val pd_reg = RegEnable(pds, pdWb.valid) 863005e809bSJiuyang Liu val start_pc_reg = RegEnable(pdWb.bits.pc(0), pdWb.valid) 864005e809bSJiuyang Liu val wb_idx_reg = RegEnable(ifu_wb_idx, pdWb.valid) 86509c6f1ddSLingrui98 86609c6f1ddSLingrui98 when (ifu_wb_valid) { 86709c6f1ddSLingrui98 val comm_stq_wen = VecInit(pds.map(_.valid).zip(pdWb.bits.instrRange).map{ 86809c6f1ddSLingrui98 case (v, inRange) => v && inRange 86909c6f1ddSLingrui98 }) 8701c6fc24aSEaston Man commitStateQueueEnable(ifu_wb_idx) := true.B 8711c6fc24aSEaston Man (commitStateQueueNext(ifu_wb_idx) zip comm_stq_wen).map { 8721c6fc24aSEaston Man case (qe, v) => when(v) { 8731c6fc24aSEaston Man qe := c_valid 8741c6fc24aSEaston Man } 87509c6f1ddSLingrui98 } 87609c6f1ddSLingrui98 } 87709c6f1ddSLingrui98 878c5c5edaeSJenius when (ifu_wb_valid) { 879c5c5edaeSJenius ifuWbPtr_write := ifuWbPtr + 1.U 880c5c5edaeSJenius } 88109c6f1ddSLingrui98 882f21bbcb2SGuokai Chen XSError(ifu_wb_valid && isAfter(pdWb.bits.ftqIdx, ifuPtr), "IFU returned a predecode before its req, check IFU") 883f21bbcb2SGuokai Chen 8841c6fc24aSEaston Man ftb_entry_mem.io.ren.get.head := ifu_wb_valid 88509c6f1ddSLingrui98 ftb_entry_mem.io.raddr.head := ifu_wb_idx 88609c6f1ddSLingrui98 val has_false_hit = WireInit(false.B) 88709c6f1ddSLingrui98 when (RegNext(hit_pd_valid)) { 88809c6f1ddSLingrui98 // check for false hit 88909c6f1ddSLingrui98 val pred_ftb_entry = ftb_entry_mem.io.rdata.head 890eeb5ff92SLingrui98 val brSlots = pred_ftb_entry.brSlots 891eeb5ff92SLingrui98 val tailSlot = pred_ftb_entry.tailSlot 89209c6f1ddSLingrui98 // we check cfis that bpu predicted 89309c6f1ddSLingrui98 894eeb5ff92SLingrui98 // bpu predicted branches but denied by predecode 895eeb5ff92SLingrui98 val br_false_hit = 896eeb5ff92SLingrui98 brSlots.map{ 897eeb5ff92SLingrui98 s => s.valid && !(pd_reg(s.offset).valid && pd_reg(s.offset).isBr) 898eeb5ff92SLingrui98 }.reduce(_||_) || 899b37e4b45SLingrui98 (tailSlot.valid && pred_ftb_entry.tailSlot.sharing && 900eeb5ff92SLingrui98 !(pd_reg(tailSlot.offset).valid && pd_reg(tailSlot.offset).isBr)) 901eeb5ff92SLingrui98 902eeb5ff92SLingrui98 val jmpOffset = tailSlot.offset 90309c6f1ddSLingrui98 val jmp_pd = pd_reg(jmpOffset) 90409c6f1ddSLingrui98 val jal_false_hit = pred_ftb_entry.jmpValid && 90509c6f1ddSLingrui98 ((pred_ftb_entry.isJal && !(jmp_pd.valid && jmp_pd.isJal)) || 90609c6f1ddSLingrui98 (pred_ftb_entry.isJalr && !(jmp_pd.valid && jmp_pd.isJalr)) || 90709c6f1ddSLingrui98 (pred_ftb_entry.isCall && !(jmp_pd.valid && jmp_pd.isCall)) || 90809c6f1ddSLingrui98 (pred_ftb_entry.isRet && !(jmp_pd.valid && jmp_pd.isRet)) 90909c6f1ddSLingrui98 ) 91009c6f1ddSLingrui98 91109c6f1ddSLingrui98 has_false_hit := br_false_hit || jal_false_hit || hit_pd_mispred_reg 91265fddcf0Szoujr XSDebug(has_false_hit, "FTB false hit by br or jal or hit_pd, startAddr: %x\n", pdWb.bits.pc(0)) 91365fddcf0Szoujr 914352db50aSLingrui98 // assert(!has_false_hit) 91509c6f1ddSLingrui98 } 91609c6f1ddSLingrui98 91709c6f1ddSLingrui98 when (has_false_hit) { 91809c6f1ddSLingrui98 entry_hit_status(wb_idx_reg) := h_false_hit 91909c6f1ddSLingrui98 } 92009c6f1ddSLingrui98 92109c6f1ddSLingrui98 // ******************************************************************************* 92209c6f1ddSLingrui98 // **************************** redirect from backend **************************** 92309c6f1ddSLingrui98 // ******************************************************************************* 92409c6f1ddSLingrui98 92509c6f1ddSLingrui98 // redirect read cfiInfo, couples to redirectGen s2 926bace178aSGao-Zeyu val redirectReadStart = 1 // 0 for ifuRedirect 927bace178aSGao-Zeyu val ftq_redirect_rdata = Wire(Vec(FtqRedirectAheadNum, new Ftq_Redirect_SRAMEntry)) 928deb3a97eSGao-Zeyu val ftb_redirect_rdata = Wire(Vec(FtqRedirectAheadNum, new FTBEntry_FtqMem)) 929bace178aSGao-Zeyu for (i <- redirectReadStart until FtqRedirectAheadNum) { 93016a171eeSEaston Man ftq_redirect_mem.io.ren.get(i + redirectReadStart) := ftqIdxAhead(i).valid 931deb3a97eSGao-Zeyu ftq_redirect_mem.io.raddr(i + redirectReadStart) := ftqIdxAhead(i).bits.value 93216a171eeSEaston Man ftb_entry_mem.io.ren.get(i + redirectReadStart) := ftqIdxAhead(i).valid 933bace178aSGao-Zeyu ftb_entry_mem.io.raddr(i + redirectReadStart) := ftqIdxAhead(i).bits.value 9349342624fSGao-Zeyu } 93516a171eeSEaston Man ftq_redirect_mem.io.ren.get(redirectReadStart) := Mux(aheadValid, ftqIdxAhead(0).valid, backendRedirect.valid) 936deb3a97eSGao-Zeyu ftq_redirect_mem.io.raddr(redirectReadStart) := Mux(aheadValid, ftqIdxAhead(0).bits.value, backendRedirect.bits.ftqIdx.value) 93716a171eeSEaston Man ftb_entry_mem.io.ren.get(redirectReadStart) := Mux(aheadValid, ftqIdxAhead(0).valid, backendRedirect.valid) 938deb3a97eSGao-Zeyu ftb_entry_mem.io.raddr(redirectReadStart) := Mux(aheadValid, ftqIdxAhead(0).bits.value, backendRedirect.bits.ftqIdx.value) 939bace178aSGao-Zeyu 940bace178aSGao-Zeyu for (i <- 0 until FtqRedirectAheadNum) { 941deb3a97eSGao-Zeyu ftq_redirect_rdata(i) := ftq_redirect_mem.io.rdata(i + redirectReadStart) 942bace178aSGao-Zeyu ftb_redirect_rdata(i) := ftb_entry_mem.io.rdata(i + redirectReadStart) 943bace178aSGao-Zeyu } 944deb3a97eSGao-Zeyu val stage3CfiInfo = Mux(realAhdValid, Mux1H(ftqIdxSelOH, ftq_redirect_rdata), ftq_redirect_mem.io.rdata(redirectReadStart)) 94509c6f1ddSLingrui98 val backendRedirectCfi = fromBackendRedirect.bits.cfiUpdate 94609c6f1ddSLingrui98 backendRedirectCfi.fromFtqRedirectSram(stage3CfiInfo) 94709c6f1ddSLingrui98 948d2b20d1aSTang Haojin 949bace178aSGao-Zeyu val r_ftb_entry = Mux(realAhdValid, Mux1H(ftqIdxSelOH, ftb_redirect_rdata), ftb_entry_mem.io.rdata(redirectReadStart)) 95009c6f1ddSLingrui98 val r_ftqOffset = fromBackendRedirect.bits.ftqOffset 95109c6f1ddSLingrui98 952d2b20d1aSTang Haojin backendRedirectCfi.br_hit := r_ftb_entry.brIsSaved(r_ftqOffset) 953d2b20d1aSTang Haojin backendRedirectCfi.jr_hit := r_ftb_entry.isJalr && r_ftb_entry.tailSlot.offset === r_ftqOffset 9543711cf36S小造xu_zh // FIXME: not portable 955abdc3a32Sxu_zh val sc_disagree = stage3CfiInfo.sc_disagree.getOrElse(VecInit(Seq.fill(numBr)(false.B))) 956d2b20d1aSTang Haojin backendRedirectCfi.sc_hit := backendRedirectCfi.br_hit && Mux(r_ftb_entry.brSlots(0).offset === r_ftqOffset, 957abdc3a32Sxu_zh sc_disagree(0), sc_disagree(1)) 958d2b20d1aSTang Haojin 95909c6f1ddSLingrui98 when (entry_hit_status(fromBackendRedirect.bits.ftqIdx.value) === h_hit) { 96009c6f1ddSLingrui98 backendRedirectCfi.shift := PopCount(r_ftb_entry.getBrMaskByOffset(r_ftqOffset)) +& 96109c6f1ddSLingrui98 (backendRedirectCfi.pd.isBr && !r_ftb_entry.brIsSaved(r_ftqOffset) && 962eeb5ff92SLingrui98 !r_ftb_entry.newBrCanNotInsert(r_ftqOffset)) 96309c6f1ddSLingrui98 96409c6f1ddSLingrui98 backendRedirectCfi.addIntoHist := backendRedirectCfi.pd.isBr && (r_ftb_entry.brIsSaved(r_ftqOffset) || 965eeb5ff92SLingrui98 !r_ftb_entry.newBrCanNotInsert(r_ftqOffset)) 96609c6f1ddSLingrui98 }.otherwise { 96709c6f1ddSLingrui98 backendRedirectCfi.shift := (backendRedirectCfi.pd.isBr && backendRedirectCfi.taken).asUInt 96809c6f1ddSLingrui98 backendRedirectCfi.addIntoHist := backendRedirectCfi.pd.isBr.asUInt 96909c6f1ddSLingrui98 } 97009c6f1ddSLingrui98 97109c6f1ddSLingrui98 97209c6f1ddSLingrui98 // *************************************************************************** 97309c6f1ddSLingrui98 // **************************** redirect from ifu **************************** 97409c6f1ddSLingrui98 // *************************************************************************** 975d2b20d1aSTang Haojin val fromIfuRedirect = WireInit(0.U.asTypeOf(Valid(new BranchPredictionRedirect))) 97609c6f1ddSLingrui98 fromIfuRedirect.valid := pdWb.valid && pdWb.bits.misOffset.valid && !backendFlush 97709c6f1ddSLingrui98 fromIfuRedirect.bits.ftqIdx := pdWb.bits.ftqIdx 97809c6f1ddSLingrui98 fromIfuRedirect.bits.ftqOffset := pdWb.bits.misOffset.bits 97909c6f1ddSLingrui98 fromIfuRedirect.bits.level := RedirectLevel.flushAfter 980d2b20d1aSTang Haojin fromIfuRedirect.bits.BTBMissBubble := true.B 981d2b20d1aSTang Haojin fromIfuRedirect.bits.debugIsMemVio := false.B 982d2b20d1aSTang Haojin fromIfuRedirect.bits.debugIsCtrl := false.B 98309c6f1ddSLingrui98 98409c6f1ddSLingrui98 val ifuRedirectCfiUpdate = fromIfuRedirect.bits.cfiUpdate 98509c6f1ddSLingrui98 ifuRedirectCfiUpdate.pc := pdWb.bits.pc(pdWb.bits.misOffset.bits) 98609c6f1ddSLingrui98 ifuRedirectCfiUpdate.pd := pdWb.bits.pd(pdWb.bits.misOffset.bits) 98709c6f1ddSLingrui98 ifuRedirectCfiUpdate.predTaken := cfiIndex_vec(pdWb.bits.ftqIdx.value).valid 98809c6f1ddSLingrui98 ifuRedirectCfiUpdate.target := pdWb.bits.target 98909c6f1ddSLingrui98 ifuRedirectCfiUpdate.taken := pdWb.bits.cfiOffset.valid 99009c6f1ddSLingrui98 ifuRedirectCfiUpdate.isMisPred := pdWb.bits.misOffset.valid 99109c6f1ddSLingrui98 9921c6fc24aSEaston Man val ifuRedirectReg = RegNextWithEnable(fromIfuRedirect, hasInit = true) 99309c6f1ddSLingrui98 val ifuRedirectToBpu = WireInit(ifuRedirectReg) 99409c6f1ddSLingrui98 ifuFlush := fromIfuRedirect.valid || ifuRedirectToBpu.valid 99509c6f1ddSLingrui98 99616a171eeSEaston Man ftq_redirect_mem.io.ren.get.head := fromIfuRedirect.valid 997deb3a97eSGao-Zeyu ftq_redirect_mem.io.raddr.head := fromIfuRedirect.bits.ftqIdx.value 99809c6f1ddSLingrui98 99909c6f1ddSLingrui98 val toBpuCfi = ifuRedirectToBpu.bits.cfiUpdate 1000deb3a97eSGao-Zeyu toBpuCfi.fromFtqRedirectSram(ftq_redirect_mem.io.rdata.head) 1001f1267a13SEaston Man when (ifuRedirectReg.bits.cfiUpdate.pd.isRet && ifuRedirectReg.bits.cfiUpdate.pd.valid) { 1002c89b4642SGuokai Chen toBpuCfi.target := toBpuCfi.topAddr 100309c6f1ddSLingrui98 } 100409c6f1ddSLingrui98 1005d2b20d1aSTang Haojin when (ifuRedirectReg.valid) { 1006d2b20d1aSTang Haojin ifuRedirected(ifuRedirectReg.bits.ftqIdx.value) := true.B 1007d2b20d1aSTang Haojin } .elsewhen(RegNext(pdWb.valid)) { 1008d2b20d1aSTang Haojin // if pdWb and no redirect, set to false 1009d2b20d1aSTang Haojin ifuRedirected(last_cycle_bpu_in_ptr.value) := false.B 1010d2b20d1aSTang Haojin } 1011d2b20d1aSTang Haojin 10126022c595SsinceforYy // ********************************************************************** 10136022c595SsinceforYy // ***************************** to backend ***************************** 10146022c595SsinceforYy // ********************************************************************** 10156022c595SsinceforYy // to backend pc mem / target 10166022c595SsinceforYy io.toBackend.pc_mem_wen := RegNext(last_cycle_bpu_in) 10176022c595SsinceforYy io.toBackend.pc_mem_waddr := RegEnable(last_cycle_bpu_in_idx, last_cycle_bpu_in) 10186022c595SsinceforYy io.toBackend.pc_mem_wdata := RegEnable(bpu_in_bypass_buf_for_ifu, last_cycle_bpu_in) 10196022c595SsinceforYy 10206022c595SsinceforYy // num cycle is fixed 10216022c595SsinceforYy val newest_entry_en: Bool = RegNext(last_cycle_bpu_in || backendRedirect.valid || ifuRedirectToBpu.valid) 10226022c595SsinceforYy io.toBackend.newest_entry_en := RegNext(newest_entry_en) 10236022c595SsinceforYy io.toBackend.newest_entry_ptr := RegEnable(newest_entry_ptr, newest_entry_en) 10246022c595SsinceforYy io.toBackend.newest_entry_target := RegEnable(newest_entry_target, newest_entry_en) 10256022c595SsinceforYy 102609c6f1ddSLingrui98 // ********************************************************************* 102709c6f1ddSLingrui98 // **************************** wb from exu **************************** 102809c6f1ddSLingrui98 // ********************************************************************* 102909c6f1ddSLingrui98 1030d2b20d1aSTang Haojin backendRedirect.valid := io.fromBackend.redirect.valid 1031d2b20d1aSTang Haojin backendRedirect.bits.connectRedirect(io.fromBackend.redirect.bits) 1032d2b20d1aSTang Haojin backendRedirect.bits.BTBMissBubble := false.B 1033d2b20d1aSTang Haojin 10342e1be6e1SSteve Gou 103509c6f1ddSLingrui98 def extractRedirectInfo(wb: Valid[Redirect]) = { 10366bf9b30dSLingrui98 val ftqPtr = wb.bits.ftqIdx 103709c6f1ddSLingrui98 val ftqOffset = wb.bits.ftqOffset 103809c6f1ddSLingrui98 val taken = wb.bits.cfiUpdate.taken 103909c6f1ddSLingrui98 val mispred = wb.bits.cfiUpdate.isMisPred 10406bf9b30dSLingrui98 (wb.valid, ftqPtr, ftqOffset, taken, mispred) 104109c6f1ddSLingrui98 } 104209c6f1ddSLingrui98 104309c6f1ddSLingrui98 // fix mispredict entry 104409c6f1ddSLingrui98 val lastIsMispredict = RegNext( 1045df5b4b8eSYinan Xu backendRedirect.valid && backendRedirect.bits.level === RedirectLevel.flushAfter, init = false.B 104609c6f1ddSLingrui98 ) 104709c6f1ddSLingrui98 104809c6f1ddSLingrui98 def updateCfiInfo(redirect: Valid[Redirect], isBackend: Boolean = true) = { 10496bf9b30dSLingrui98 val (r_valid, r_ptr, r_offset, r_taken, r_mispred) = extractRedirectInfo(redirect) 10506bf9b30dSLingrui98 val r_idx = r_ptr.value 105109c6f1ddSLingrui98 val cfiIndex_bits_wen = r_valid && r_taken && r_offset < cfiIndex_vec(r_idx).bits 105209c6f1ddSLingrui98 val cfiIndex_valid_wen = r_valid && r_offset === cfiIndex_vec(r_idx).bits 105309c6f1ddSLingrui98 when (cfiIndex_bits_wen || cfiIndex_valid_wen) { 105409c6f1ddSLingrui98 cfiIndex_vec(r_idx).valid := cfiIndex_bits_wen || cfiIndex_valid_wen && r_taken 10553f88c020SGuokai Chen } .elsewhen (r_valid && !r_taken && r_offset =/= cfiIndex_vec(r_idx).bits) { 10563f88c020SGuokai Chen cfiIndex_vec(r_idx).valid :=false.B 105709c6f1ddSLingrui98 } 105809c6f1ddSLingrui98 when (cfiIndex_bits_wen) { 105909c6f1ddSLingrui98 cfiIndex_vec(r_idx).bits := r_offset 106009c6f1ddSLingrui98 } 10611c6fc24aSEaston Man newest_entry_target_modified := true.B 10626bf9b30dSLingrui98 newest_entry_target := redirect.bits.cfiUpdate.target 10631c6fc24aSEaston Man newest_entry_ptr_modified := true.B 1064873dc383SLingrui98 newest_entry_ptr := r_ptr 10651c6fc24aSEaston Man 1066b0ed7239SLingrui98 update_target(r_idx) := redirect.bits.cfiUpdate.target // TODO: remove this 106709c6f1ddSLingrui98 if (isBackend) { 106809c6f1ddSLingrui98 mispredict_vec(r_idx)(r_offset) := r_mispred 106909c6f1ddSLingrui98 } 107009c6f1ddSLingrui98 } 107109c6f1ddSLingrui98 1072bace178aSGao-Zeyu when(fromBackendRedirect.valid) { 1073bace178aSGao-Zeyu updateCfiInfo(fromBackendRedirect) 107409c6f1ddSLingrui98 }.elsewhen (ifuRedirectToBpu.valid) { 107509c6f1ddSLingrui98 updateCfiInfo(ifuRedirectToBpu, isBackend=false) 107609c6f1ddSLingrui98 } 107709c6f1ddSLingrui98 1078bace178aSGao-Zeyu when (fromBackendRedirect.valid) { 1079bace178aSGao-Zeyu when (fromBackendRedirect.bits.ControlRedirectBubble) { 1080d2b20d1aSTang Haojin when (fromBackendRedirect.bits.ControlBTBMissBubble) { 1081d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.BTBMissBubble.id) := true.B 1082d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.BTBMissBubble.id) := true.B 1083d2b20d1aSTang Haojin } .elsewhen (fromBackendRedirect.bits.TAGEMissBubble) { 1084d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.TAGEMissBubble.id) := true.B 1085d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.TAGEMissBubble.id) := true.B 1086d2b20d1aSTang Haojin } .elsewhen (fromBackendRedirect.bits.SCMissBubble) { 1087d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.SCMissBubble.id) := true.B 1088d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.SCMissBubble.id) := true.B 1089d2b20d1aSTang Haojin } .elsewhen (fromBackendRedirect.bits.ITTAGEMissBubble) { 1090d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 1091d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 1092d2b20d1aSTang Haojin } .elsewhen (fromBackendRedirect.bits.RASMissBubble) { 1093d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.RASMissBubble.id) := true.B 1094d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.RASMissBubble.id) := true.B 1095d2b20d1aSTang Haojin } 1096d2b20d1aSTang Haojin 1097d2b20d1aSTang Haojin 10989342624fSGao-Zeyu } .elsewhen (backendRedirect.bits.MemVioRedirectBubble) { 1099d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 1100d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 1101d2b20d1aSTang Haojin } .otherwise { 1102d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 1103d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 1104d2b20d1aSTang Haojin } 1105d2b20d1aSTang Haojin } .elsewhen (ifuRedirectReg.valid) { 1106d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.BTBMissBubble.id) := true.B 1107d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.BTBMissBubble.id) := true.B 1108d2b20d1aSTang Haojin } 1109d2b20d1aSTang Haojin 1110d2b20d1aSTang Haojin io.ControlBTBMissBubble := fromBackendRedirect.bits.ControlBTBMissBubble 1111d2b20d1aSTang Haojin io.TAGEMissBubble := fromBackendRedirect.bits.TAGEMissBubble 1112d2b20d1aSTang Haojin io.SCMissBubble := fromBackendRedirect.bits.SCMissBubble 1113d2b20d1aSTang Haojin io.ITTAGEMissBubble := fromBackendRedirect.bits.ITTAGEMissBubble 1114d2b20d1aSTang Haojin io.RASMissBubble := fromBackendRedirect.bits.RASMissBubble 1115d2b20d1aSTang Haojin 111609c6f1ddSLingrui98 // *********************************************************************************** 111709c6f1ddSLingrui98 // **************************** flush ptr and state queue **************************** 111809c6f1ddSLingrui98 // *********************************************************************************** 111909c6f1ddSLingrui98 1120df5b4b8eSYinan Xu val redirectVec = VecInit(backendRedirect, fromIfuRedirect) 112109c6f1ddSLingrui98 112209c6f1ddSLingrui98 // when redirect, we should reset ptrs and status queues 112309c6f1ddSLingrui98 when(redirectVec.map(r => r.valid).reduce(_||_)){ 11242f4a3aa4SLingrui98 val r = PriorityMux(redirectVec.map(r => (r.valid -> r.bits))) 112509c6f1ddSLingrui98 val notIfu = redirectVec.dropRight(1).map(r => r.valid).reduce(_||_) 11262f4a3aa4SLingrui98 val (idx, offset, flushItSelf) = (r.ftqIdx, r.ftqOffset, RedirectLevel.flushItself(r.level)) 112709c6f1ddSLingrui98 val next = idx + 1.U 112809c6f1ddSLingrui98 bpuPtr := next 1129dc270d3bSJenius copied_bpu_ptr.map(_ := next) 1130c5c5edaeSJenius ifuPtr_write := next 1131c5c5edaeSJenius ifuWbPtr_write := next 1132c5c5edaeSJenius ifuPtrPlus1_write := idx + 2.U 11336bf9b30dSLingrui98 ifuPtrPlus2_write := idx + 3.U 11343f88c020SGuokai Chen 11353f88c020SGuokai Chen } 11363f88c020SGuokai Chen when(RegNext(redirectVec.map(r => r.valid).reduce(_||_))){ 11373f88c020SGuokai Chen val r = PriorityMux(redirectVec.map(r => (r.valid -> r.bits))) 11383f88c020SGuokai Chen val notIfu = redirectVec.dropRight(1).map(r => r.valid).reduce(_||_) 11393f88c020SGuokai Chen val (idx, offset, flushItSelf) = (r.ftqIdx, r.ftqOffset, RedirectLevel.flushItself(r.level)) 11403f88c020SGuokai Chen when (RegNext(notIfu)) { 11411c6fc24aSEaston Man commitStateQueueEnable(RegNext(idx.value)) := true.B 11421c6fc24aSEaston Man commitStateQueueNext(RegNext(idx.value)).zipWithIndex.foreach({ case (s, i) => 11433f88c020SGuokai Chen when(i.U > RegNext(offset) || i.U === RegNext(offset) && RegNext(flushItSelf)) { 114409c6f1ddSLingrui98 s := c_invalid 114509c6f1ddSLingrui98 } 114609c6f1ddSLingrui98 }) 114709c6f1ddSLingrui98 } 114809c6f1ddSLingrui98 } 114909c6f1ddSLingrui98 11503f88c020SGuokai Chen 115109c6f1ddSLingrui98 // only the valid bit is actually needed 1152df5b4b8eSYinan Xu io.toIfu.redirect.bits := backendRedirect.bits 115309c6f1ddSLingrui98 io.toIfu.redirect.valid := stage2Flush 1154d2b20d1aSTang Haojin io.toIfu.topdown_redirect := fromBackendRedirect 115509c6f1ddSLingrui98 115609c6f1ddSLingrui98 // commit 11579aca92b9SYinan Xu for (c <- io.fromBackend.rob_commits) { 115809c6f1ddSLingrui98 when(c.valid) { 11591c6fc24aSEaston Man commitStateQueueEnable(c.bits.ftqIdx.value) := true.B 11601c6fc24aSEaston Man commitStateQueueNext(c.bits.ftqIdx.value)(c.bits.ftqOffset) := c_commited 116188825c5cSYinan Xu // TODO: remove this 116288825c5cSYinan Xu // For instruction fusions, we also update the next instruction 1163c3abb8b6SYinan Xu when (c.bits.commitType === 4.U) { 11641c6fc24aSEaston Man commitStateQueueNext(c.bits.ftqIdx.value)(c.bits.ftqOffset + 1.U) := c_commited 1165c3abb8b6SYinan Xu }.elsewhen(c.bits.commitType === 5.U) { 11661c6fc24aSEaston Man commitStateQueueNext(c.bits.ftqIdx.value)(c.bits.ftqOffset + 2.U) := c_commited 1167c3abb8b6SYinan Xu }.elsewhen(c.bits.commitType === 6.U) { 116888825c5cSYinan Xu val index = (c.bits.ftqIdx + 1.U).value 11691c6fc24aSEaston Man commitStateQueueEnable(index) := true.B 11701c6fc24aSEaston Man commitStateQueueNext(index)(0) := c_commited 1171c3abb8b6SYinan Xu }.elsewhen(c.bits.commitType === 7.U) { 117288825c5cSYinan Xu val index = (c.bits.ftqIdx + 1.U).value 11731c6fc24aSEaston Man commitStateQueueEnable(index) := true.B 11741c6fc24aSEaston Man commitStateQueueNext(index)(1) := c_commited 117588825c5cSYinan Xu } 117609c6f1ddSLingrui98 } 117709c6f1ddSLingrui98 } 117809c6f1ddSLingrui98 117989cc69c1STang Haojin robCommPtr_write := Mux(io.fromBackend.rob_commits.map(_.valid).reduce(_ | _), ParallelPriorityMux(io.fromBackend.rob_commits.map(_.valid).reverse, io.fromBackend.rob_commits.map(_.bits.ftqIdx).reverse), robCommPtr) 118089cc69c1STang Haojin 118109c6f1ddSLingrui98 // **************************************************************** 118209c6f1ddSLingrui98 // **************************** to bpu **************************** 118309c6f1ddSLingrui98 // **************************************************************** 118409c6f1ddSLingrui98 1185*fd3aa057SYuandongliang io.toBpu.redirctFromIFU := ifuRedirectToBpu.valid 118651981c77SbugGenerator io.toBpu.redirect := Mux(fromBackendRedirect.valid, fromBackendRedirect, ifuRedirectToBpu) 1187209a4cafSSteve Gou val dummy_s1_pred_cycle_vec = VecInit(List.tabulate(FtqSize)(_=>0.U(64.W))) 1188209a4cafSSteve Gou val redirect_latency = GTimer() - pred_s1_cycle.getOrElse(dummy_s1_pred_cycle_vec)(io.toBpu.redirect.bits.ftqIdx.value) + 1.U 1189209a4cafSSteve Gou XSPerfHistogram("backend_redirect_latency", redirect_latency, fromBackendRedirect.valid, 0, 60, 1) 1190209a4cafSSteve Gou XSPerfHistogram("ifu_redirect_latency", redirect_latency, !fromBackendRedirect.valid && ifuRedirectToBpu.valid, 0, 60, 1) 119109c6f1ddSLingrui98 1192f21bbcb2SGuokai Chen XSError(io.toBpu.redirect.valid && isBefore(io.toBpu.redirect.bits.ftqIdx, commPtr), "Ftq received a redirect after its commit, check backend or replay") 119309c6f1ddSLingrui98 119402f21c16SLingrui98 val may_have_stall_from_bpu = Wire(Bool()) 119502f21c16SLingrui98 val bpu_ftb_update_stall = RegInit(0.U(2.W)) // 2-cycle stall, so we need 3 states 119602f21c16SLingrui98 may_have_stall_from_bpu := bpu_ftb_update_stall =/= 0.U 11971c6fc24aSEaston Man val notInvalidSeq = commitStateQueueReg(commPtr.value).map(s => s =/= c_invalid).reverse 11984b0d80d8SXuan Hu // Todo: @huxuan check it 11994b0d80d8SXuan Hu // canCommit := commPtr =/= ifuWbPtr && !may_have_stall_from_bpu && 12004b0d80d8SXuan Hu // Cat(commitStateQueue(commPtr.value).map(s => { 12014b0d80d8SXuan Hu // s === c_invalid || s === c_commited 12024b0d80d8SXuan Hu // })).andR 120343aca6c2SGuokai Chen canCommit := commPtr =/= ifuWbPtr && !may_have_stall_from_bpu && 12041c6fc24aSEaston Man (isAfter(robCommPtr, commPtr) || 12051c6fc24aSEaston Man PriorityMuxDefault(notInvalidSeq.zip(commitStateQueueReg(commPtr.value).reverse), c_invalid) === c_commited) 120609c6f1ddSLingrui98 12071d1e6d4dSJenius val mmioReadPtr = io.mmioCommitRead.mmioFtqPtr 12081d1e6d4dSJenius val mmioLastCommit = isBefore(commPtr, mmioReadPtr) && (isAfter(ifuPtr,mmioReadPtr) || mmioReadPtr === ifuPtr) && 12091c6fc24aSEaston Man Cat(commitStateQueueReg(mmioReadPtr.value).map(s => { s === c_invalid || s === c_commited})).andR 12101d1e6d4dSJenius io.mmioCommitRead.mmioLastCommit := RegNext(mmioLastCommit) 12111d1e6d4dSJenius 121209c6f1ddSLingrui98 // commit reads 1213c5c5edaeSJenius val commit_pc_bundle = RegNext(ftq_pc_mem.io.commPtr_rdata) 121481101dc4SLingrui98 val commit_target = 121534cf890eSLingrui98 Mux(RegNext(commPtr === newest_entry_ptr), 12161c6fc24aSEaston Man RegEnable(newest_entry_target, newest_entry_target_modified), 121781101dc4SLingrui98 RegNext(ftq_pc_mem.io.commPtrPlus1_rdata.startAddr)) 12181c6fc24aSEaston Man ftq_pd_mem.io.ren.get.last := canCommit 121909c6f1ddSLingrui98 ftq_pd_mem.io.raddr.last := commPtr.value 122009c6f1ddSLingrui98 val commit_pd = ftq_pd_mem.io.rdata.last 122116a171eeSEaston Man ftq_redirect_mem.io.ren.get.last := canCommit 1222deb3a97eSGao-Zeyu ftq_redirect_mem.io.raddr.last := commPtr.value 1223deb3a97eSGao-Zeyu val commit_spec_meta = ftq_redirect_mem.io.rdata.last 122409c6f1ddSLingrui98 ftq_meta_1r_sram.io.ren(0) := canCommit 122509c6f1ddSLingrui98 ftq_meta_1r_sram.io.raddr(0) := commPtr.value 1226deb3a97eSGao-Zeyu val commit_meta = ftq_meta_1r_sram.io.rdata(0).meta 1227deb3a97eSGao-Zeyu val commit_ftb_entry = ftq_meta_1r_sram.io.rdata(0).ftb_entry 122809c6f1ddSLingrui98 122909c6f1ddSLingrui98 // need one cycle to read mem and srams 12301c6fc24aSEaston Man val do_commit_ptr = RegEnable(commPtr, canCommit) 12315371700eSzoujr val do_commit = RegNext(canCommit, init=false.B) 12326bf9b30dSLingrui98 when (canCommit) { 12336bf9b30dSLingrui98 commPtr_write := commPtrPlus1 12346bf9b30dSLingrui98 commPtrPlus1_write := commPtrPlus1 + 1.U 12356bf9b30dSLingrui98 } 12361c6fc24aSEaston Man val commit_state = RegEnable(commitStateQueueReg(commPtr.value), canCommit) 12375371700eSzoujr val can_commit_cfi = WireInit(cfiIndex_vec(commPtr.value)) 1238d4fcfc3eSGuokai Chen val do_commit_cfi = WireInit(cfiIndex_vec(do_commit_ptr.value)) 12393f88c020SGuokai Chen // 12403f88c020SGuokai Chen //when (commitStateQueue(commPtr.value)(can_commit_cfi.bits) =/= c_commited) { 12413f88c020SGuokai Chen // can_commit_cfi.valid := false.B 12423f88c020SGuokai Chen //} 12431c6fc24aSEaston Man val commit_cfi = RegEnable(can_commit_cfi, canCommit) 12441c6fc24aSEaston Man val debug_cfi = commitStateQueueReg(do_commit_ptr.value)(do_commit_cfi.bits) =/= c_commited && do_commit_cfi.valid 124509c6f1ddSLingrui98 12461c6fc24aSEaston Man val commit_mispredict : Vec[Bool] = VecInit((RegEnable(mispredict_vec(commPtr.value), canCommit) zip commit_state).map { 124709c6f1ddSLingrui98 case (mis, state) => mis && state === c_commited 124809c6f1ddSLingrui98 }) 1249cc2d1573SEaston Man val commit_instCommited: Vec[Bool] = VecInit(commit_state.map(_ === c_commited)) // [PredictWidth] 12505371700eSzoujr val can_commit_hit = entry_hit_status(commPtr.value) 12511c6fc24aSEaston Man val commit_hit = RegEnable(can_commit_hit, canCommit) 12521c6fc24aSEaston Man val diff_commit_target = RegEnable(update_target(commPtr.value), canCommit) // TODO: remove this 12531c6fc24aSEaston Man val commit_stage = RegEnable(pred_stage(commPtr.value), canCommit) 125409c6f1ddSLingrui98 val commit_valid = commit_hit === h_hit || commit_cfi.valid // hit or taken 125509c6f1ddSLingrui98 12565371700eSzoujr val to_bpu_hit = can_commit_hit === h_hit || can_commit_hit === h_false_hit 125702f21c16SLingrui98 switch (bpu_ftb_update_stall) { 125802f21c16SLingrui98 is (0.U) { 125902f21c16SLingrui98 when (can_commit_cfi.valid && !to_bpu_hit && canCommit) { 126002f21c16SLingrui98 bpu_ftb_update_stall := 2.U // 2-cycle stall 126102f21c16SLingrui98 } 126202f21c16SLingrui98 } 126302f21c16SLingrui98 is (2.U) { 126402f21c16SLingrui98 bpu_ftb_update_stall := 1.U 126502f21c16SLingrui98 } 126602f21c16SLingrui98 is (1.U) { 126702f21c16SLingrui98 bpu_ftb_update_stall := 0.U 126802f21c16SLingrui98 } 126902f21c16SLingrui98 is (3.U) { 127002f21c16SLingrui98 XSError(true.B, "bpu_ftb_update_stall should be 0, 1 or 2") 127102f21c16SLingrui98 } 127202f21c16SLingrui98 } 127309c6f1ddSLingrui98 1274b0ed7239SLingrui98 // TODO: remove this 1275b0ed7239SLingrui98 XSError(do_commit && diff_commit_target =/= commit_target, "\ncommit target should be the same as update target\n") 1276b0ed7239SLingrui98 1277b2f6ed0aSSteve Gou // update latency stats 1278b2f6ed0aSSteve Gou val update_latency = GTimer() - pred_s1_cycle.getOrElse(dummy_s1_pred_cycle_vec)(do_commit_ptr.value) + 1.U 1279b2f6ed0aSSteve Gou XSPerfHistogram("bpu_update_latency", update_latency, io.toBpu.update.valid, 0, 64, 2) 1280b2f6ed0aSSteve Gou 128109c6f1ddSLingrui98 io.toBpu.update := DontCare 128209c6f1ddSLingrui98 io.toBpu.update.valid := commit_valid && do_commit 128309c6f1ddSLingrui98 val update = io.toBpu.update.bits 128409c6f1ddSLingrui98 update.false_hit := commit_hit === h_false_hit 128509c6f1ddSLingrui98 update.pc := commit_pc_bundle.startAddr 1286deb3a97eSGao-Zeyu update.meta := commit_meta 1287803124a6SLingrui98 update.cfi_idx := commit_cfi 12888ffcd86aSLingrui98 update.full_target := commit_target 1289edc18578SLingrui98 update.from_stage := commit_stage 1290c2d1ec7dSLingrui98 update.spec_info := commit_spec_meta 12913f88c020SGuokai Chen XSError(commit_valid && do_commit && debug_cfi, "\ncommit cfi can be non c_commited\n") 129209c6f1ddSLingrui98 129309c6f1ddSLingrui98 val commit_real_hit = commit_hit === h_hit 129409c6f1ddSLingrui98 val update_ftb_entry = update.ftb_entry 129509c6f1ddSLingrui98 129609c6f1ddSLingrui98 val ftbEntryGen = Module(new FTBEntryGen).io 129709c6f1ddSLingrui98 ftbEntryGen.start_addr := commit_pc_bundle.startAddr 129809c6f1ddSLingrui98 ftbEntryGen.old_entry := commit_ftb_entry 129909c6f1ddSLingrui98 ftbEntryGen.pd := commit_pd 130009c6f1ddSLingrui98 ftbEntryGen.cfiIndex := commit_cfi 130109c6f1ddSLingrui98 ftbEntryGen.target := commit_target 130209c6f1ddSLingrui98 ftbEntryGen.hit := commit_real_hit 130309c6f1ddSLingrui98 ftbEntryGen.mispredict_vec := commit_mispredict 130409c6f1ddSLingrui98 130509c6f1ddSLingrui98 update_ftb_entry := ftbEntryGen.new_entry 130609c6f1ddSLingrui98 update.new_br_insert_pos := ftbEntryGen.new_br_insert_pos 130709c6f1ddSLingrui98 update.mispred_mask := ftbEntryGen.mispred_mask 130809c6f1ddSLingrui98 update.old_entry := ftbEntryGen.is_old_entry 1309edc18578SLingrui98 update.pred_hit := commit_hit === h_hit || commit_hit === h_false_hit 1310803124a6SLingrui98 update.br_taken_mask := ftbEntryGen.taken_mask 1311cc2d1573SEaston Man update.br_committed := (ftbEntryGen.new_entry.brValids zip ftbEntryGen.new_entry.brOffset) map { 1312cc2d1573SEaston Man case (valid, offset) => valid && commit_instCommited(offset) 1313cc2d1573SEaston Man } 1314803124a6SLingrui98 update.jmp_taken := ftbEntryGen.jmp_taken 1315b37e4b45SLingrui98 1316803124a6SLingrui98 // update.full_pred.fromFtbEntry(ftbEntryGen.new_entry, update.pc) 1317803124a6SLingrui98 // update.full_pred.jalr_target := commit_target 1318803124a6SLingrui98 // update.full_pred.hit := true.B 1319803124a6SLingrui98 // when (update.full_pred.is_jalr) { 1320803124a6SLingrui98 // update.full_pred.targets.last := commit_target 1321803124a6SLingrui98 // } 132209c6f1ddSLingrui98 1323e30430c2SJay // **************************************************************** 1324e30430c2SJay // *********************** to prefetch **************************** 1325e30430c2SJay // **************************************************************** 1326f9c51548Sssszwic /** 1327f9c51548Sssszwic ****************************************************************************** 1328f9c51548Sssszwic * prefetchPtr control 1329f9c51548Sssszwic * - 1. prefetchPtr plus 1 when toPrefetch fire and keep distance from bpuPtr more than 2 1330f9c51548Sssszwic * - 2. limit range of prefetchPtr is in [ifuPtr + minRange, ifuPtr + maxRange] 1331f9c51548Sssszwic * - 3. flush prefetchPtr when receive redirect from ifu or backend 1332f9c51548Sssszwic ****************************************************************************** 1333f9c51548Sssszwic */ 1334e30430c2SJay val prefetchPtr = RegInit(FtqPtr(false.B, 0.U)) 1335f9c51548Sssszwic val nextPrefetchPtr = WireInit(prefetchPtr) 1336e30430c2SJay 1337f9c51548Sssszwic prefetchPtr := nextPrefetchPtr 1338f9c51548Sssszwic 1339f9c51548Sssszwic // TODO: consider req which cross cacheline 1340f9c51548Sssszwic when(io.toPrefetch.req.fire) { 1341f9c51548Sssszwic when(prefetchPtr < bpuPtr - 2.U) { 1342f9c51548Sssszwic nextPrefetchPtr := prefetchPtr + 1.U 1343a677d2cbSguohongyu } 1344a677d2cbSguohongyu } 1345a677d2cbSguohongyu 1346f9c51548Sssszwic when(prefetchPtr < ifuPtr + minRangeFromIFUptr.U) { 1347f9c51548Sssszwic nextPrefetchPtr := ifuPtr + minRangeFromIFUptr.U 1348f9c51548Sssszwic }.elsewhen(prefetchPtr > ifuPtr + maxRangeFromIFUptr.U) { 1349f9c51548Sssszwic nextPrefetchPtr := ifuPtr + maxRangeFromIFUptr.U 1350e30430c2SJay } 1351e30430c2SJay 1352de7689fcSJay when(redirectVec.map(r => r.valid).reduce(_||_)){ 1353de7689fcSJay val r = PriorityMux(redirectVec.map(r => (r.valid -> r.bits))) 1354f9c51548Sssszwic val next = r.ftqIdx + minRangeFromIFUptr.U 1355f9c51548Sssszwic nextPrefetchPtr := next 1356de7689fcSJay } 1357de7689fcSJay 1358f9c51548Sssszwic // data from ftq_pc_mem has 1 cycle delay 1359f9c51548Sssszwic io.toPrefetch.req.valid := RegNext(entry_fetch_status(nextPrefetchPtr.value) === f_to_send) 1360f9c51548Sssszwic ftq_pc_mem.io.other_raddrs(0) := nextPrefetchPtr.value 1361f9c51548Sssszwic io.toPrefetch.req.bits.target := RegNext(ftq_pc_mem.io.other_rdatas(0).startAddr) 1362378f00d9SJenius 1363f9c51548Sssszwic // record position relationship between ifuPtr, pfPtr and bpuPtr 1364c686adcdSYinan Xu val hartId = p(XSCoreParamsKey).HartId 1365c686adcdSYinan Xu val isWritePrefetchPtrTable = Constantin.createRecord(s"isWritePrefetchPtrTable$hartId") 1366c686adcdSYinan Xu val prefetchPtrTable = ChiselDB.createTable(s"PrefetchPtrTable$hartId", new PrefetchPtrDB) 1367f9c51548Sssszwic val prefetchPtrDumpData = Wire(new PrefetchPtrDB) 1368f9c51548Sssszwic prefetchPtrDumpData.fromFtqPtr := distanceBetween(bpuPtr, prefetchPtr) 1369f9c51548Sssszwic prefetchPtrDumpData.fromIfuPtr := distanceBetween(prefetchPtr, ifuPtr) 1370378f00d9SJenius 1371f9c51548Sssszwic prefetchPtrTable.log( 1372f9c51548Sssszwic data = prefetchPtrDumpData, 1373f9c51548Sssszwic en = isWritePrefetchPtrTable.orR && io.toPrefetch.req.fire, 1374f9c51548Sssszwic site = "FTQ" + p(XSCoreParamsKey).HartId.toString, 1375f9c51548Sssszwic clock = clock, 1376f9c51548Sssszwic reset = reset 1377f9c51548Sssszwic ) 1378f9c51548Sssszwic 1379de7689fcSJay 138009c6f1ddSLingrui98 // ****************************************************************************** 138109c6f1ddSLingrui98 // **************************** commit perf counters **************************** 138209c6f1ddSLingrui98 // ****************************************************************************** 138309c6f1ddSLingrui98 138409c6f1ddSLingrui98 val commit_inst_mask = VecInit(commit_state.map(c => c === c_commited && do_commit)).asUInt 138509c6f1ddSLingrui98 val commit_mispred_mask = commit_mispredict.asUInt 138609c6f1ddSLingrui98 val commit_not_mispred_mask = ~commit_mispred_mask 138709c6f1ddSLingrui98 138809c6f1ddSLingrui98 val commit_br_mask = commit_pd.brMask.asUInt 138909c6f1ddSLingrui98 val commit_jmp_mask = UIntToOH(commit_pd.jmpOffset) & Fill(PredictWidth, commit_pd.jmpInfo.valid.asTypeOf(UInt(1.W))) 139009c6f1ddSLingrui98 val commit_cfi_mask = (commit_br_mask | commit_jmp_mask) 139109c6f1ddSLingrui98 139209c6f1ddSLingrui98 val mbpInstrs = commit_inst_mask & commit_cfi_mask 139309c6f1ddSLingrui98 139409c6f1ddSLingrui98 val mbpRights = mbpInstrs & commit_not_mispred_mask 139509c6f1ddSLingrui98 val mbpWrongs = mbpInstrs & commit_mispred_mask 139609c6f1ddSLingrui98 139709c6f1ddSLingrui98 io.bpuInfo.bpRight := PopCount(mbpRights) 139809c6f1ddSLingrui98 io.bpuInfo.bpWrong := PopCount(mbpWrongs) 139909c6f1ddSLingrui98 1400c686adcdSYinan Xu val isWriteFTQTable = Constantin.createRecord(s"isWriteFTQTable$hartId") 1401c686adcdSYinan Xu val ftqBranchTraceDB = ChiselDB.createTable(s"FTQTable$hartId", new FtqDebugBundle) 140209c6f1ddSLingrui98 // Cfi Info 140309c6f1ddSLingrui98 for (i <- 0 until PredictWidth) { 140409c6f1ddSLingrui98 val pc = commit_pc_bundle.startAddr + (i * instBytes).U 140509c6f1ddSLingrui98 val v = commit_state(i) === c_commited 140609c6f1ddSLingrui98 val isBr = commit_pd.brMask(i) 140709c6f1ddSLingrui98 val isJmp = commit_pd.jmpInfo.valid && commit_pd.jmpOffset === i.U 140809c6f1ddSLingrui98 val isCfi = isBr || isJmp 140909c6f1ddSLingrui98 val isTaken = commit_cfi.valid && commit_cfi.bits === i.U 141009c6f1ddSLingrui98 val misPred = commit_mispredict(i) 1411c2ad24ebSLingrui98 // val ghist = commit_spec_meta.ghist.predHist 1412c2ad24ebSLingrui98 val histPtr = commit_spec_meta.histPtr 1413deb3a97eSGao-Zeyu val predCycle = commit_meta(63, 0) 141409c6f1ddSLingrui98 val target = commit_target 141509c6f1ddSLingrui98 141609c6f1ddSLingrui98 val brIdx = OHToUInt(Reverse(Cat(update_ftb_entry.brValids.zip(update_ftb_entry.brOffset).map{case(v, offset) => v && offset === i.U}))) 141709c6f1ddSLingrui98 val inFtbEntry = update_ftb_entry.brValids.zip(update_ftb_entry.brOffset).map{case(v, offset) => v && offset === i.U}.reduce(_||_) 141809c6f1ddSLingrui98 val addIntoHist = ((commit_hit === h_hit) && inFtbEntry) || ((!(commit_hit === h_hit) && i.U === commit_cfi.bits && isBr && commit_cfi.valid)) 141909c6f1ddSLingrui98 XSDebug(v && do_commit && isCfi, p"cfi_update: isBr(${isBr}) pc(${Hexadecimal(pc)}) " + 1420c2ad24ebSLingrui98 p"taken(${isTaken}) mispred(${misPred}) cycle($predCycle) hist(${histPtr.value}) " + 142109c6f1ddSLingrui98 p"startAddr(${Hexadecimal(commit_pc_bundle.startAddr)}) AddIntoHist(${addIntoHist}) " + 142209c6f1ddSLingrui98 p"brInEntry(${inFtbEntry}) brIdx(${brIdx}) target(${Hexadecimal(target)})\n") 142351532d8bSGuokai Chen 142451532d8bSGuokai Chen val logbundle = Wire(new FtqDebugBundle) 142551532d8bSGuokai Chen logbundle.pc := pc 142651532d8bSGuokai Chen logbundle.target := target 142751532d8bSGuokai Chen logbundle.isBr := isBr 142851532d8bSGuokai Chen logbundle.isJmp := isJmp 142951532d8bSGuokai Chen logbundle.isCall := isJmp && commit_pd.hasCall 143051532d8bSGuokai Chen logbundle.isRet := isJmp && commit_pd.hasRet 143151532d8bSGuokai Chen logbundle.misPred := misPred 143251532d8bSGuokai Chen logbundle.isTaken := isTaken 143351532d8bSGuokai Chen logbundle.predStage := commit_stage 143451532d8bSGuokai Chen 143551532d8bSGuokai Chen ftqBranchTraceDB.log( 143651532d8bSGuokai Chen data = logbundle /* hardware of type T */, 1437da3bf434SMaxpicca-Li en = isWriteFTQTable.orR && v && do_commit && isCfi, 143851532d8bSGuokai Chen site = "FTQ" + p(XSCoreParamsKey).HartId.toString, 143951532d8bSGuokai Chen clock = clock, 144051532d8bSGuokai Chen reset = reset 144151532d8bSGuokai Chen ) 144209c6f1ddSLingrui98 } 144309c6f1ddSLingrui98 144409c6f1ddSLingrui98 val enq = io.fromBpu.resp 14452e1be6e1SSteve Gou val perf_redirect = backendRedirect 144609c6f1ddSLingrui98 144709c6f1ddSLingrui98 XSPerfAccumulate("entry", validEntries) 144809c6f1ddSLingrui98 XSPerfAccumulate("bpu_to_ftq_stall", enq.valid && !enq.ready) 144909c6f1ddSLingrui98 XSPerfAccumulate("mispredictRedirect", perf_redirect.valid && RedirectLevel.flushAfter === perf_redirect.bits.level) 145009c6f1ddSLingrui98 XSPerfAccumulate("replayRedirect", perf_redirect.valid && RedirectLevel.flushItself(perf_redirect.bits.level)) 145109c6f1ddSLingrui98 XSPerfAccumulate("predecodeRedirect", fromIfuRedirect.valid) 145209c6f1ddSLingrui98 145309c6f1ddSLingrui98 XSPerfAccumulate("to_ifu_bubble", io.toIfu.req.ready && !io.toIfu.req.valid) 145409c6f1ddSLingrui98 145509c6f1ddSLingrui98 XSPerfAccumulate("to_ifu_stall", io.toIfu.req.valid && !io.toIfu.req.ready) 145609c6f1ddSLingrui98 XSPerfAccumulate("from_bpu_real_bubble", !enq.valid && enq.ready && allowBpuIn) 145712cedb6fSLingrui98 XSPerfAccumulate("bpu_to_ifu_bubble", bpuPtr === ifuPtr) 1458b2f6ed0aSSteve Gou XSPerfAccumulate("bpu_to_ifu_bubble_when_ftq_full", (bpuPtr === ifuPtr) && isFull(bpuPtr, commPtr) && io.toIfu.req.ready) 145909c6f1ddSLingrui98 1460bace178aSGao-Zeyu XSPerfAccumulate("redirectAhead_ValidNum", ftqIdxAhead.map(_.valid).reduce(_|_)) 14619342624fSGao-Zeyu XSPerfAccumulate("fromBackendRedirect_ValidNum", io.fromBackend.redirect.valid) 14629342624fSGao-Zeyu XSPerfAccumulate("toBpuRedirect_ValidNum", io.toBpu.redirect.valid) 14639342624fSGao-Zeyu 146409c6f1ddSLingrui98 val from_bpu = io.fromBpu.resp.bits 146509c6f1ddSLingrui98 val to_ifu = io.toIfu.req.bits 146609c6f1ddSLingrui98 146709c6f1ddSLingrui98 1468209a4cafSSteve Gou XSPerfHistogram("commit_num_inst", PopCount(commit_inst_mask), do_commit, 0, PredictWidth+1, 1) 146909c6f1ddSLingrui98 147009c6f1ddSLingrui98 147109c6f1ddSLingrui98 147209c6f1ddSLingrui98 147309c6f1ddSLingrui98 val commit_jal_mask = UIntToOH(commit_pd.jmpOffset) & Fill(PredictWidth, commit_pd.hasJal.asTypeOf(UInt(1.W))) 147409c6f1ddSLingrui98 val commit_jalr_mask = UIntToOH(commit_pd.jmpOffset) & Fill(PredictWidth, commit_pd.hasJalr.asTypeOf(UInt(1.W))) 147509c6f1ddSLingrui98 val commit_call_mask = UIntToOH(commit_pd.jmpOffset) & Fill(PredictWidth, commit_pd.hasCall.asTypeOf(UInt(1.W))) 147609c6f1ddSLingrui98 val commit_ret_mask = UIntToOH(commit_pd.jmpOffset) & Fill(PredictWidth, commit_pd.hasRet.asTypeOf(UInt(1.W))) 147709c6f1ddSLingrui98 147809c6f1ddSLingrui98 147909c6f1ddSLingrui98 val mbpBRights = mbpRights & commit_br_mask 148009c6f1ddSLingrui98 val mbpJRights = mbpRights & commit_jal_mask 148109c6f1ddSLingrui98 val mbpIRights = mbpRights & commit_jalr_mask 148209c6f1ddSLingrui98 val mbpCRights = mbpRights & commit_call_mask 148309c6f1ddSLingrui98 val mbpRRights = mbpRights & commit_ret_mask 148409c6f1ddSLingrui98 148509c6f1ddSLingrui98 val mbpBWrongs = mbpWrongs & commit_br_mask 148609c6f1ddSLingrui98 val mbpJWrongs = mbpWrongs & commit_jal_mask 148709c6f1ddSLingrui98 val mbpIWrongs = mbpWrongs & commit_jalr_mask 148809c6f1ddSLingrui98 val mbpCWrongs = mbpWrongs & commit_call_mask 148909c6f1ddSLingrui98 val mbpRWrongs = mbpWrongs & commit_ret_mask 149009c6f1ddSLingrui98 14911d7e5011SLingrui98 val commit_pred_stage = RegNext(pred_stage(commPtr.value)) 14921d7e5011SLingrui98 14931d7e5011SLingrui98 def pred_stage_map(src: UInt, name: String) = { 14941d7e5011SLingrui98 (0 until numBpStages).map(i => 14951d7e5011SLingrui98 f"${name}_stage_${i+1}" -> PopCount(src.asBools.map(_ && commit_pred_stage === BP_STAGES(i))) 14961d7e5011SLingrui98 ).foldLeft(Map[String, UInt]())(_+_) 14971d7e5011SLingrui98 } 14981d7e5011SLingrui98 14991d7e5011SLingrui98 val mispred_stage_map = pred_stage_map(mbpWrongs, "mispredict") 15001d7e5011SLingrui98 val br_mispred_stage_map = pred_stage_map(mbpBWrongs, "br_mispredict") 15011d7e5011SLingrui98 val jalr_mispred_stage_map = pred_stage_map(mbpIWrongs, "jalr_mispredict") 15021d7e5011SLingrui98 val correct_stage_map = pred_stage_map(mbpRights, "correct") 15031d7e5011SLingrui98 val br_correct_stage_map = pred_stage_map(mbpBRights, "br_correct") 15041d7e5011SLingrui98 val jalr_correct_stage_map = pred_stage_map(mbpIRights, "jalr_correct") 15051d7e5011SLingrui98 150609c6f1ddSLingrui98 val update_valid = io.toBpu.update.valid 150709c6f1ddSLingrui98 def u(cond: Bool) = update_valid && cond 150809c6f1ddSLingrui98 val ftb_false_hit = u(update.false_hit) 150965fddcf0Szoujr // assert(!ftb_false_hit) 151009c6f1ddSLingrui98 val ftb_hit = u(commit_hit === h_hit) 151109c6f1ddSLingrui98 151209c6f1ddSLingrui98 val ftb_new_entry = u(ftbEntryGen.is_init_entry) 1513b37e4b45SLingrui98 val ftb_new_entry_only_br = ftb_new_entry && !update_ftb_entry.jmpValid 1514b37e4b45SLingrui98 val ftb_new_entry_only_jmp = ftb_new_entry && !update_ftb_entry.brValids(0) 1515b37e4b45SLingrui98 val ftb_new_entry_has_br_and_jmp = ftb_new_entry && update_ftb_entry.brValids(0) && update_ftb_entry.jmpValid 151609c6f1ddSLingrui98 151709c6f1ddSLingrui98 val ftb_old_entry = u(ftbEntryGen.is_old_entry) 151809c6f1ddSLingrui98 151909c6f1ddSLingrui98 val ftb_modified_entry = u(ftbEntryGen.is_new_br || ftbEntryGen.is_jalr_target_modified || ftbEntryGen.is_always_taken_modified) 152009c6f1ddSLingrui98 val ftb_modified_entry_new_br = u(ftbEntryGen.is_new_br) 1521d2b20d1aSTang Haojin val ftb_modified_entry_ifu_redirected = u(ifuRedirected(do_commit_ptr.value)) 152209c6f1ddSLingrui98 val ftb_modified_entry_jalr_target_modified = u(ftbEntryGen.is_jalr_target_modified) 152309c6f1ddSLingrui98 val ftb_modified_entry_br_full = ftb_modified_entry && ftbEntryGen.is_br_full 152409c6f1ddSLingrui98 val ftb_modified_entry_always_taken = ftb_modified_entry && ftbEntryGen.is_always_taken_modified 152509c6f1ddSLingrui98 1526209a4cafSSteve Gou def getFtbEntryLen(pc: UInt, entry: FTBEntry) = (entry.getFallThrough(pc) - pc) >> instOffsetBits 1527209a4cafSSteve Gou val gen_ftb_entry_len = getFtbEntryLen(update.pc, ftbEntryGen.new_entry) 1528209a4cafSSteve Gou XSPerfHistogram("ftb_init_entry_len", gen_ftb_entry_len, ftb_new_entry, 0, PredictWidth+1, 1) 1529209a4cafSSteve Gou XSPerfHistogram("ftb_modified_entry_len", gen_ftb_entry_len, ftb_modified_entry, 0, PredictWidth+1, 1) 1530209a4cafSSteve Gou val s3_ftb_entry_len = getFtbEntryLen(from_bpu.s3.pc(0), from_bpu.last_stage_ftb_entry) 1531209a4cafSSteve Gou XSPerfHistogram("s3_ftb_entry_len", s3_ftb_entry_len, from_bpu.s3.valid(0), 0, PredictWidth+1, 1) 153209c6f1ddSLingrui98 1533209a4cafSSteve Gou XSPerfHistogram("ftq_has_entry", validEntries, true.B, 0, FtqSize+1, 1) 153409c6f1ddSLingrui98 153509c6f1ddSLingrui98 val perfCountsMap = Map( 153609c6f1ddSLingrui98 "BpInstr" -> PopCount(mbpInstrs), 153709c6f1ddSLingrui98 "BpBInstr" -> PopCount(mbpBRights | mbpBWrongs), 153809c6f1ddSLingrui98 "BpRight" -> PopCount(mbpRights), 153909c6f1ddSLingrui98 "BpWrong" -> PopCount(mbpWrongs), 154009c6f1ddSLingrui98 "BpBRight" -> PopCount(mbpBRights), 154109c6f1ddSLingrui98 "BpBWrong" -> PopCount(mbpBWrongs), 154209c6f1ddSLingrui98 "BpJRight" -> PopCount(mbpJRights), 154309c6f1ddSLingrui98 "BpJWrong" -> PopCount(mbpJWrongs), 154409c6f1ddSLingrui98 "BpIRight" -> PopCount(mbpIRights), 154509c6f1ddSLingrui98 "BpIWrong" -> PopCount(mbpIWrongs), 154609c6f1ddSLingrui98 "BpCRight" -> PopCount(mbpCRights), 154709c6f1ddSLingrui98 "BpCWrong" -> PopCount(mbpCWrongs), 154809c6f1ddSLingrui98 "BpRRight" -> PopCount(mbpRRights), 154909c6f1ddSLingrui98 "BpRWrong" -> PopCount(mbpRWrongs), 155009c6f1ddSLingrui98 155109c6f1ddSLingrui98 "ftb_false_hit" -> PopCount(ftb_false_hit), 155209c6f1ddSLingrui98 "ftb_hit" -> PopCount(ftb_hit), 155309c6f1ddSLingrui98 "ftb_new_entry" -> PopCount(ftb_new_entry), 155409c6f1ddSLingrui98 "ftb_new_entry_only_br" -> PopCount(ftb_new_entry_only_br), 155509c6f1ddSLingrui98 "ftb_new_entry_only_jmp" -> PopCount(ftb_new_entry_only_jmp), 155609c6f1ddSLingrui98 "ftb_new_entry_has_br_and_jmp" -> PopCount(ftb_new_entry_has_br_and_jmp), 155709c6f1ddSLingrui98 "ftb_old_entry" -> PopCount(ftb_old_entry), 155809c6f1ddSLingrui98 "ftb_modified_entry" -> PopCount(ftb_modified_entry), 155909c6f1ddSLingrui98 "ftb_modified_entry_new_br" -> PopCount(ftb_modified_entry_new_br), 156009c6f1ddSLingrui98 "ftb_jalr_target_modified" -> PopCount(ftb_modified_entry_jalr_target_modified), 156109c6f1ddSLingrui98 "ftb_modified_entry_br_full" -> PopCount(ftb_modified_entry_br_full), 156209c6f1ddSLingrui98 "ftb_modified_entry_always_taken" -> PopCount(ftb_modified_entry_always_taken) 1563209a4cafSSteve Gou ) ++ mispred_stage_map ++ br_mispred_stage_map ++ jalr_mispred_stage_map ++ 15641d7e5011SLingrui98 correct_stage_map ++ br_correct_stage_map ++ jalr_correct_stage_map 156509c6f1ddSLingrui98 156609c6f1ddSLingrui98 for((key, value) <- perfCountsMap) { 156709c6f1ddSLingrui98 XSPerfAccumulate(key, value) 156809c6f1ddSLingrui98 } 156909c6f1ddSLingrui98 157009c6f1ddSLingrui98 // --------------------------- Debug -------------------------------- 157109c6f1ddSLingrui98 // XSDebug(enq_fire, p"enq! " + io.fromBpu.resp.bits.toPrintable) 157209c6f1ddSLingrui98 XSDebug(io.toIfu.req.fire, p"fire to ifu " + io.toIfu.req.bits.toPrintable) 157309c6f1ddSLingrui98 XSDebug(do_commit, p"deq! [ptr] $do_commit_ptr\n") 157409c6f1ddSLingrui98 XSDebug(true.B, p"[bpuPtr] $bpuPtr, [ifuPtr] $ifuPtr, [ifuWbPtr] $ifuWbPtr [commPtr] $commPtr\n") 157509c6f1ddSLingrui98 XSDebug(true.B, p"[in] v:${io.fromBpu.resp.valid} r:${io.fromBpu.resp.ready} " + 157609c6f1ddSLingrui98 p"[out] v:${io.toIfu.req.valid} r:${io.toIfu.req.ready}\n") 157709c6f1ddSLingrui98 XSDebug(do_commit, p"[deq info] cfiIndex: $commit_cfi, $commit_pc_bundle, target: ${Hexadecimal(commit_target)}\n") 157809c6f1ddSLingrui98 157909c6f1ddSLingrui98 // def ubtbCheck(commit: FtqEntry, predAns: Seq[PredictorAnswer], isWrong: Bool) = { 158009c6f1ddSLingrui98 // commit.valids.zip(commit.pd).zip(predAns).zip(commit.takens).map { 158109c6f1ddSLingrui98 // case (((valid, pd), ans), taken) => 158209c6f1ddSLingrui98 // Mux(valid && pd.isBr, 158309c6f1ddSLingrui98 // isWrong ^ Mux(ans.hit.asBool, 158409c6f1ddSLingrui98 // Mux(ans.taken.asBool, taken && ans.target === commitEntry.target, 158509c6f1ddSLingrui98 // !taken), 158609c6f1ddSLingrui98 // !taken), 158709c6f1ddSLingrui98 // false.B) 158809c6f1ddSLingrui98 // } 158909c6f1ddSLingrui98 // } 159009c6f1ddSLingrui98 159109c6f1ddSLingrui98 // def btbCheck(commit: FtqEntry, predAns: Seq[PredictorAnswer], isWrong: Bool) = { 159209c6f1ddSLingrui98 // commit.valids.zip(commit.pd).zip(predAns).zip(commit.takens).map { 159309c6f1ddSLingrui98 // case (((valid, pd), ans), taken) => 159409c6f1ddSLingrui98 // Mux(valid && pd.isBr, 159509c6f1ddSLingrui98 // isWrong ^ Mux(ans.hit.asBool, 159609c6f1ddSLingrui98 // Mux(ans.taken.asBool, taken && ans.target === commitEntry.target, 159709c6f1ddSLingrui98 // !taken), 159809c6f1ddSLingrui98 // !taken), 159909c6f1ddSLingrui98 // false.B) 160009c6f1ddSLingrui98 // } 160109c6f1ddSLingrui98 // } 160209c6f1ddSLingrui98 160309c6f1ddSLingrui98 // def tageCheck(commit: FtqEntry, predAns: Seq[PredictorAnswer], isWrong: Bool) = { 160409c6f1ddSLingrui98 // commit.valids.zip(commit.pd).zip(predAns).zip(commit.takens).map { 160509c6f1ddSLingrui98 // case (((valid, pd), ans), taken) => 160609c6f1ddSLingrui98 // Mux(valid && pd.isBr, 160709c6f1ddSLingrui98 // isWrong ^ (ans.taken.asBool === taken), 160809c6f1ddSLingrui98 // false.B) 160909c6f1ddSLingrui98 // } 161009c6f1ddSLingrui98 // } 161109c6f1ddSLingrui98 161209c6f1ddSLingrui98 // def loopCheck(commit: FtqEntry, predAns: Seq[PredictorAnswer], isWrong: Bool) = { 161309c6f1ddSLingrui98 // commit.valids.zip(commit.pd).zip(predAns).zip(commit.takens).map { 161409c6f1ddSLingrui98 // case (((valid, pd), ans), taken) => 161509c6f1ddSLingrui98 // Mux(valid && (pd.isBr) && ans.hit.asBool, 161609c6f1ddSLingrui98 // isWrong ^ (!taken), 161709c6f1ddSLingrui98 // false.B) 161809c6f1ddSLingrui98 // } 161909c6f1ddSLingrui98 // } 162009c6f1ddSLingrui98 162109c6f1ddSLingrui98 // def rasCheck(commit: FtqEntry, predAns: Seq[PredictorAnswer], isWrong: Bool) = { 162209c6f1ddSLingrui98 // commit.valids.zip(commit.pd).zip(predAns).zip(commit.takens).map { 162309c6f1ddSLingrui98 // case (((valid, pd), ans), taken) => 162409c6f1ddSLingrui98 // Mux(valid && pd.isRet.asBool /*&& taken*/ && ans.hit.asBool, 162509c6f1ddSLingrui98 // isWrong ^ (ans.target === commitEntry.target), 162609c6f1ddSLingrui98 // false.B) 162709c6f1ddSLingrui98 // } 162809c6f1ddSLingrui98 // } 162909c6f1ddSLingrui98 163009c6f1ddSLingrui98 // val ubtbRights = ubtbCheck(commitEntry, commitEntry.metas.map(_.ubtbAns), false.B) 163109c6f1ddSLingrui98 // val ubtbWrongs = ubtbCheck(commitEntry, commitEntry.metas.map(_.ubtbAns), true.B) 163209c6f1ddSLingrui98 // // btb and ubtb pred jal and jalr as well 163309c6f1ddSLingrui98 // val btbRights = btbCheck(commitEntry, commitEntry.metas.map(_.btbAns), false.B) 163409c6f1ddSLingrui98 // val btbWrongs = btbCheck(commitEntry, commitEntry.metas.map(_.btbAns), true.B) 163509c6f1ddSLingrui98 // val tageRights = tageCheck(commitEntry, commitEntry.metas.map(_.tageAns), false.B) 163609c6f1ddSLingrui98 // val tageWrongs = tageCheck(commitEntry, commitEntry.metas.map(_.tageAns), true.B) 163709c6f1ddSLingrui98 163809c6f1ddSLingrui98 // val loopRights = loopCheck(commitEntry, commitEntry.metas.map(_.loopAns), false.B) 163909c6f1ddSLingrui98 // val loopWrongs = loopCheck(commitEntry, commitEntry.metas.map(_.loopAns), true.B) 164009c6f1ddSLingrui98 164109c6f1ddSLingrui98 // val rasRights = rasCheck(commitEntry, commitEntry.metas.map(_.rasAns), false.B) 164209c6f1ddSLingrui98 // val rasWrongs = rasCheck(commitEntry, commitEntry.metas.map(_.rasAns), true.B) 16431ca0e4f3SYinan Xu 1644cd365d4cSrvcoresjw val perfEvents = Seq( 1645cd365d4cSrvcoresjw ("bpu_s2_redirect ", bpu_s2_redirect ), 1646cb4f77ceSLingrui98 ("bpu_s3_redirect ", bpu_s3_redirect ), 1647cd365d4cSrvcoresjw ("bpu_to_ftq_stall ", enq.valid && ~enq.ready ), 1648cd365d4cSrvcoresjw ("mispredictRedirect ", perf_redirect.valid && RedirectLevel.flushAfter === perf_redirect.bits.level), 1649cd365d4cSrvcoresjw ("replayRedirect ", perf_redirect.valid && RedirectLevel.flushItself(perf_redirect.bits.level) ), 1650cd365d4cSrvcoresjw ("predecodeRedirect ", fromIfuRedirect.valid ), 1651cd365d4cSrvcoresjw ("to_ifu_bubble ", io.toIfu.req.ready && !io.toIfu.req.valid ), 1652cd365d4cSrvcoresjw ("from_bpu_real_bubble ", !enq.valid && enq.ready && allowBpuIn ), 1653cd365d4cSrvcoresjw ("BpInstr ", PopCount(mbpInstrs) ), 1654cd365d4cSrvcoresjw ("BpBInstr ", PopCount(mbpBRights | mbpBWrongs) ), 1655cd365d4cSrvcoresjw ("BpRight ", PopCount(mbpRights) ), 1656cd365d4cSrvcoresjw ("BpWrong ", PopCount(mbpWrongs) ), 1657cd365d4cSrvcoresjw ("BpBRight ", PopCount(mbpBRights) ), 1658cd365d4cSrvcoresjw ("BpBWrong ", PopCount(mbpBWrongs) ), 1659cd365d4cSrvcoresjw ("BpJRight ", PopCount(mbpJRights) ), 1660cd365d4cSrvcoresjw ("BpJWrong ", PopCount(mbpJWrongs) ), 1661cd365d4cSrvcoresjw ("BpIRight ", PopCount(mbpIRights) ), 1662cd365d4cSrvcoresjw ("BpIWrong ", PopCount(mbpIWrongs) ), 1663cd365d4cSrvcoresjw ("BpCRight ", PopCount(mbpCRights) ), 1664cd365d4cSrvcoresjw ("BpCWrong ", PopCount(mbpCWrongs) ), 1665cd365d4cSrvcoresjw ("BpRRight ", PopCount(mbpRRights) ), 1666cd365d4cSrvcoresjw ("BpRWrong ", PopCount(mbpRWrongs) ), 1667cd365d4cSrvcoresjw ("ftb_false_hit ", PopCount(ftb_false_hit) ), 1668cd365d4cSrvcoresjw ("ftb_hit ", PopCount(ftb_hit) ), 1669cd365d4cSrvcoresjw ) 16701ca0e4f3SYinan Xu generatePerfEvent() 167109c6f1ddSLingrui98} 1672