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 4209c6f1ddSLingrui98class FtqPtr(implicit p: Parameters) extends CircularQueuePtr[FtqPtr]( 4309c6f1ddSLingrui98 p => p(XSCoreParamsKey).FtqSize 4409c6f1ddSLingrui98){ 4509c6f1ddSLingrui98} 4609c6f1ddSLingrui98 4709c6f1ddSLingrui98object FtqPtr { 4809c6f1ddSLingrui98 def apply(f: Bool, v: UInt)(implicit p: Parameters): FtqPtr = { 4909c6f1ddSLingrui98 val ptr = Wire(new FtqPtr) 5009c6f1ddSLingrui98 ptr.flag := f 5109c6f1ddSLingrui98 ptr.value := v 5209c6f1ddSLingrui98 ptr 5309c6f1ddSLingrui98 } 5409c6f1ddSLingrui98 def inverse(ptr: FtqPtr)(implicit p: Parameters): FtqPtr = { 5509c6f1ddSLingrui98 apply(!ptr.flag, ptr.value) 5609c6f1ddSLingrui98 } 5709c6f1ddSLingrui98} 5809c6f1ddSLingrui98 5909c6f1ddSLingrui98class FtqNRSRAM[T <: Data](gen: T, numRead: Int)(implicit p: Parameters) extends XSModule { 6009c6f1ddSLingrui98 6109c6f1ddSLingrui98 val io = IO(new Bundle() { 6209c6f1ddSLingrui98 val raddr = Input(Vec(numRead, UInt(log2Up(FtqSize).W))) 6309c6f1ddSLingrui98 val ren = Input(Vec(numRead, Bool())) 6409c6f1ddSLingrui98 val rdata = Output(Vec(numRead, gen)) 6509c6f1ddSLingrui98 val waddr = Input(UInt(log2Up(FtqSize).W)) 6609c6f1ddSLingrui98 val wen = Input(Bool()) 6709c6f1ddSLingrui98 val wdata = Input(gen) 6809c6f1ddSLingrui98 }) 6909c6f1ddSLingrui98 7009c6f1ddSLingrui98 for(i <- 0 until numRead){ 7109c6f1ddSLingrui98 val sram = Module(new SRAMTemplate(gen, FtqSize)) 7209c6f1ddSLingrui98 sram.io.r.req.valid := io.ren(i) 7309c6f1ddSLingrui98 sram.io.r.req.bits.setIdx := io.raddr(i) 7409c6f1ddSLingrui98 io.rdata(i) := sram.io.r.resp.data(0) 7509c6f1ddSLingrui98 sram.io.w.req.valid := io.wen 7609c6f1ddSLingrui98 sram.io.w.req.bits.setIdx := io.waddr 7709c6f1ddSLingrui98 sram.io.w.req.bits.data := VecInit(io.wdata) 7809c6f1ddSLingrui98 } 7909c6f1ddSLingrui98 8009c6f1ddSLingrui98} 8109c6f1ddSLingrui98 8209c6f1ddSLingrui98class Ftq_RF_Components(implicit p: Parameters) extends XSBundle with BPUUtils { 8309c6f1ddSLingrui98 val startAddr = UInt(VAddrBits.W) 84b37e4b45SLingrui98 val nextLineAddr = UInt(VAddrBits.W) 8509c6f1ddSLingrui98 val isNextMask = Vec(PredictWidth, Bool()) 86b37e4b45SLingrui98 val fallThruError = Bool() 87b37e4b45SLingrui98 // val carry = Bool() 8809c6f1ddSLingrui98 def getPc(offset: UInt) = { 8985215037SLingrui98 def getHigher(pc: UInt) = pc(VAddrBits-1, log2Ceil(PredictWidth)+instOffsetBits+1) 9085215037SLingrui98 def getOffset(pc: UInt) = pc(log2Ceil(PredictWidth)+instOffsetBits, instOffsetBits) 91b37e4b45SLingrui98 Cat(getHigher(Mux(isNextMask(offset) && startAddr(log2Ceil(PredictWidth)+instOffsetBits), nextLineAddr, startAddr)), 9209c6f1ddSLingrui98 getOffset(startAddr)+offset, 0.U(instOffsetBits.W)) 9309c6f1ddSLingrui98 } 9409c6f1ddSLingrui98 def fromBranchPrediction(resp: BranchPredictionBundle) = { 95a229ab6cSLingrui98 def carryPos(addr: UInt) = addr(instOffsetBits+log2Ceil(PredictWidth)+1) 96adc0b8dfSGuokai Chen this.startAddr := resp.pc(3) 97adc0b8dfSGuokai Chen this.nextLineAddr := resp.pc(3) + (FetchWidth * 4 * 2).U // may be broken on other configs 9809c6f1ddSLingrui98 this.isNextMask := VecInit((0 until PredictWidth).map(i => 99935edac4STang Haojin (resp.pc(3)(log2Ceil(PredictWidth), 1) +& i.U)(log2Ceil(PredictWidth)).asBool 10009c6f1ddSLingrui98 )) 101adc0b8dfSGuokai Chen this.fallThruError := resp.fallThruError(3) 10209c6f1ddSLingrui98 this 10309c6f1ddSLingrui98 } 10409c6f1ddSLingrui98 override def toPrintable: Printable = { 105b37e4b45SLingrui98 p"startAddr:${Hexadecimal(startAddr)}" 10609c6f1ddSLingrui98 } 10709c6f1ddSLingrui98} 10809c6f1ddSLingrui98 10909c6f1ddSLingrui98class Ftq_pd_Entry(implicit p: Parameters) extends XSBundle { 11009c6f1ddSLingrui98 val brMask = Vec(PredictWidth, Bool()) 11109c6f1ddSLingrui98 val jmpInfo = ValidUndirectioned(Vec(3, Bool())) 11209c6f1ddSLingrui98 val jmpOffset = UInt(log2Ceil(PredictWidth).W) 11309c6f1ddSLingrui98 val jalTarget = UInt(VAddrBits.W) 11409c6f1ddSLingrui98 val rvcMask = Vec(PredictWidth, Bool()) 11509c6f1ddSLingrui98 def hasJal = jmpInfo.valid && !jmpInfo.bits(0) 11609c6f1ddSLingrui98 def hasJalr = jmpInfo.valid && jmpInfo.bits(0) 11709c6f1ddSLingrui98 def hasCall = jmpInfo.valid && jmpInfo.bits(1) 11809c6f1ddSLingrui98 def hasRet = jmpInfo.valid && jmpInfo.bits(2) 11909c6f1ddSLingrui98 12009c6f1ddSLingrui98 def fromPdWb(pdWb: PredecodeWritebackBundle) = { 12109c6f1ddSLingrui98 val pds = pdWb.pd 12209c6f1ddSLingrui98 this.brMask := VecInit(pds.map(pd => pd.isBr && pd.valid)) 12309c6f1ddSLingrui98 this.jmpInfo.valid := VecInit(pds.map(pd => (pd.isJal || pd.isJalr) && pd.valid)).asUInt.orR 12409c6f1ddSLingrui98 this.jmpInfo.bits := ParallelPriorityMux(pds.map(pd => (pd.isJal || pd.isJalr) && pd.valid), 12509c6f1ddSLingrui98 pds.map(pd => VecInit(pd.isJalr, pd.isCall, pd.isRet))) 12609c6f1ddSLingrui98 this.jmpOffset := ParallelPriorityEncoder(pds.map(pd => (pd.isJal || pd.isJalr) && pd.valid)) 12709c6f1ddSLingrui98 this.rvcMask := VecInit(pds.map(pd => pd.isRVC)) 12809c6f1ddSLingrui98 this.jalTarget := pdWb.jalTarget 12909c6f1ddSLingrui98 } 13009c6f1ddSLingrui98 13109c6f1ddSLingrui98 def toPd(offset: UInt) = { 13209c6f1ddSLingrui98 require(offset.getWidth == log2Ceil(PredictWidth)) 13309c6f1ddSLingrui98 val pd = Wire(new PreDecodeInfo) 13409c6f1ddSLingrui98 pd.valid := true.B 13509c6f1ddSLingrui98 pd.isRVC := rvcMask(offset) 13609c6f1ddSLingrui98 val isBr = brMask(offset) 13709c6f1ddSLingrui98 val isJalr = offset === jmpOffset && jmpInfo.valid && jmpInfo.bits(0) 13809c6f1ddSLingrui98 pd.brType := Cat(offset === jmpOffset && jmpInfo.valid, isJalr || isBr) 13909c6f1ddSLingrui98 pd.isCall := offset === jmpOffset && jmpInfo.valid && jmpInfo.bits(1) 14009c6f1ddSLingrui98 pd.isRet := offset === jmpOffset && jmpInfo.valid && jmpInfo.bits(2) 14109c6f1ddSLingrui98 pd 14209c6f1ddSLingrui98 } 14309c6f1ddSLingrui98} 14409c6f1ddSLingrui98 14509c6f1ddSLingrui98 14609c6f1ddSLingrui98 147*3711cf36S小造xu_zhclass Ftq_Redirect_SRAMEntry(implicit p: Parameters) extends SpeculativeInfo { 148*3711cf36S小造xu_zh val sc_disagree = Vec(numBr, Bool()) 149*3711cf36S小造xu_zh} 15009c6f1ddSLingrui98 15109c6f1ddSLingrui98class Ftq_1R_SRAMEntry(implicit p: Parameters) extends XSBundle with HasBPUConst { 15209c6f1ddSLingrui98 val meta = UInt(MaxMetaLength.W) 15309c6f1ddSLingrui98} 15409c6f1ddSLingrui98 15509c6f1ddSLingrui98class Ftq_Pred_Info(implicit p: Parameters) extends XSBundle { 15609c6f1ddSLingrui98 val target = UInt(VAddrBits.W) 15709c6f1ddSLingrui98 val cfiIndex = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 15809c6f1ddSLingrui98} 15909c6f1ddSLingrui98 16009c6f1ddSLingrui98 16109c6f1ddSLingrui98class FtqRead[T <: Data](private val gen: T)(implicit p: Parameters) extends XSBundle { 16209c6f1ddSLingrui98 val ptr = Output(new FtqPtr) 16309c6f1ddSLingrui98 val offset = Output(UInt(log2Ceil(PredictWidth).W)) 16409c6f1ddSLingrui98 val data = Input(gen) 16509c6f1ddSLingrui98 def apply(ptr: FtqPtr, offset: UInt) = { 16609c6f1ddSLingrui98 this.ptr := ptr 16709c6f1ddSLingrui98 this.offset := offset 16809c6f1ddSLingrui98 this.data 16909c6f1ddSLingrui98 } 17009c6f1ddSLingrui98} 17109c6f1ddSLingrui98 17209c6f1ddSLingrui98 17309c6f1ddSLingrui98class FtqToBpuIO(implicit p: Parameters) extends XSBundle { 17409c6f1ddSLingrui98 val redirect = Valid(new BranchPredictionRedirect) 17509c6f1ddSLingrui98 val update = Valid(new BranchPredictionUpdate) 17609c6f1ddSLingrui98 val enq_ptr = Output(new FtqPtr) 17709c6f1ddSLingrui98} 17809c6f1ddSLingrui98 17909c6f1ddSLingrui98class FtqToIfuIO(implicit p: Parameters) extends XSBundle with HasCircularQueuePtrHelper { 18009c6f1ddSLingrui98 val req = Decoupled(new FetchRequestBundle) 181d2b20d1aSTang Haojin val redirect = Valid(new BranchPredictionRedirect) 182d2b20d1aSTang Haojin val topdown_redirect = Valid(new BranchPredictionRedirect) 18309c6f1ddSLingrui98 val flushFromBpu = new Bundle { 18409c6f1ddSLingrui98 // when ifu pipeline is not stalled, 18509c6f1ddSLingrui98 // a packet from bpu s3 can reach f1 at most 18609c6f1ddSLingrui98 val s2 = Valid(new FtqPtr) 187cb4f77ceSLingrui98 val s3 = Valid(new FtqPtr) 18809c6f1ddSLingrui98 def shouldFlushBy(src: Valid[FtqPtr], idx_to_flush: FtqPtr) = { 18909c6f1ddSLingrui98 src.valid && !isAfter(src.bits, idx_to_flush) 19009c6f1ddSLingrui98 } 19109c6f1ddSLingrui98 def shouldFlushByStage2(idx: FtqPtr) = shouldFlushBy(s2, idx) 192cb4f77ceSLingrui98 def shouldFlushByStage3(idx: FtqPtr) = shouldFlushBy(s3, idx) 19309c6f1ddSLingrui98 } 19409c6f1ddSLingrui98} 19509c6f1ddSLingrui98 196c5c5edaeSJeniusclass FtqToICacheIO(implicit p: Parameters) extends XSBundle with HasCircularQueuePtrHelper { 197c5c5edaeSJenius //NOTE: req.bits must be prepare in T cycle 198c5c5edaeSJenius // while req.valid is set true in T + 1 cycle 199c5c5edaeSJenius val req = Decoupled(new FtqToICacheRequestBundle) 200c5c5edaeSJenius} 201c5c5edaeSJenius 20209c6f1ddSLingrui98trait HasBackendRedirectInfo extends HasXSParameter { 2032e1be6e1SSteve Gou def numRedirectPcRead = exuParameters.JmpCnt + exuParameters.AluCnt + 1 20409c6f1ddSLingrui98 def isLoadReplay(r: Valid[Redirect]) = r.bits.flushItself() 20509c6f1ddSLingrui98} 20609c6f1ddSLingrui98 20709c6f1ddSLingrui98class FtqToCtrlIO(implicit p: Parameters) extends XSBundle with HasBackendRedirectInfo { 208b56f947eSYinan Xu // write to backend pc mem 209b56f947eSYinan Xu val pc_mem_wen = Output(Bool()) 210b56f947eSYinan Xu val pc_mem_waddr = Output(UInt(log2Ceil(FtqSize).W)) 211b56f947eSYinan Xu val pc_mem_wdata = Output(new Ftq_RF_Components) 212873dc383SLingrui98 // newest target 213873dc383SLingrui98 val newest_entry_target = Output(UInt(VAddrBits.W)) 214873dc383SLingrui98 val newest_entry_ptr = Output(new FtqPtr) 21509c6f1ddSLingrui98} 21609c6f1ddSLingrui98 21709c6f1ddSLingrui98 21809c6f1ddSLingrui98class FTBEntryGen(implicit p: Parameters) extends XSModule with HasBackendRedirectInfo with HasBPUParameter { 21909c6f1ddSLingrui98 val io = IO(new Bundle { 22009c6f1ddSLingrui98 val start_addr = Input(UInt(VAddrBits.W)) 22109c6f1ddSLingrui98 val old_entry = Input(new FTBEntry) 22209c6f1ddSLingrui98 val pd = Input(new Ftq_pd_Entry) 22309c6f1ddSLingrui98 val cfiIndex = Flipped(Valid(UInt(log2Ceil(PredictWidth).W))) 22409c6f1ddSLingrui98 val target = Input(UInt(VAddrBits.W)) 22509c6f1ddSLingrui98 val hit = Input(Bool()) 22609c6f1ddSLingrui98 val mispredict_vec = Input(Vec(PredictWidth, Bool())) 22709c6f1ddSLingrui98 22809c6f1ddSLingrui98 val new_entry = Output(new FTBEntry) 22909c6f1ddSLingrui98 val new_br_insert_pos = Output(Vec(numBr, Bool())) 23009c6f1ddSLingrui98 val taken_mask = Output(Vec(numBr, Bool())) 231803124a6SLingrui98 val jmp_taken = Output(Bool()) 23209c6f1ddSLingrui98 val mispred_mask = Output(Vec(numBr+1, Bool())) 23309c6f1ddSLingrui98 23409c6f1ddSLingrui98 // for perf counters 23509c6f1ddSLingrui98 val is_init_entry = Output(Bool()) 23609c6f1ddSLingrui98 val is_old_entry = Output(Bool()) 23709c6f1ddSLingrui98 val is_new_br = Output(Bool()) 23809c6f1ddSLingrui98 val is_jalr_target_modified = Output(Bool()) 23909c6f1ddSLingrui98 val is_always_taken_modified = Output(Bool()) 24009c6f1ddSLingrui98 val is_br_full = Output(Bool()) 24109c6f1ddSLingrui98 }) 24209c6f1ddSLingrui98 24309c6f1ddSLingrui98 // no mispredictions detected at predecode 24409c6f1ddSLingrui98 val hit = io.hit 24509c6f1ddSLingrui98 val pd = io.pd 24609c6f1ddSLingrui98 24709c6f1ddSLingrui98 val init_entry = WireInit(0.U.asTypeOf(new FTBEntry)) 24809c6f1ddSLingrui98 24909c6f1ddSLingrui98 25009c6f1ddSLingrui98 val cfi_is_br = pd.brMask(io.cfiIndex.bits) && io.cfiIndex.valid 25109c6f1ddSLingrui98 val entry_has_jmp = pd.jmpInfo.valid 25209c6f1ddSLingrui98 val new_jmp_is_jal = entry_has_jmp && !pd.jmpInfo.bits(0) && io.cfiIndex.valid 25309c6f1ddSLingrui98 val new_jmp_is_jalr = entry_has_jmp && pd.jmpInfo.bits(0) && io.cfiIndex.valid 25409c6f1ddSLingrui98 val new_jmp_is_call = entry_has_jmp && pd.jmpInfo.bits(1) && io.cfiIndex.valid 25509c6f1ddSLingrui98 val new_jmp_is_ret = entry_has_jmp && pd.jmpInfo.bits(2) && io.cfiIndex.valid 25609c6f1ddSLingrui98 val last_jmp_rvi = entry_has_jmp && pd.jmpOffset === (PredictWidth-1).U && !pd.rvcMask.last 257a60a2901SLingrui98 // val last_br_rvi = cfi_is_br && io.cfiIndex.bits === (PredictWidth-1).U && !pd.rvcMask.last 25809c6f1ddSLingrui98 25909c6f1ddSLingrui98 val cfi_is_jal = io.cfiIndex.bits === pd.jmpOffset && new_jmp_is_jal 26009c6f1ddSLingrui98 val cfi_is_jalr = io.cfiIndex.bits === pd.jmpOffset && new_jmp_is_jalr 26109c6f1ddSLingrui98 262a60a2901SLingrui98 def carryPos = log2Ceil(PredictWidth)+instOffsetBits 26309c6f1ddSLingrui98 def getLower(pc: UInt) = pc(carryPos-1, instOffsetBits) 26409c6f1ddSLingrui98 // if not hit, establish a new entry 26509c6f1ddSLingrui98 init_entry.valid := true.B 26609c6f1ddSLingrui98 // tag is left for ftb to assign 267eeb5ff92SLingrui98 268eeb5ff92SLingrui98 // case br 269eeb5ff92SLingrui98 val init_br_slot = init_entry.getSlotForBr(0) 270eeb5ff92SLingrui98 when (cfi_is_br) { 271eeb5ff92SLingrui98 init_br_slot.valid := true.B 272eeb5ff92SLingrui98 init_br_slot.offset := io.cfiIndex.bits 273b37e4b45SLingrui98 init_br_slot.setLowerStatByTarget(io.start_addr, io.target, numBr == 1) 274eeb5ff92SLingrui98 init_entry.always_taken(0) := true.B // set to always taken on init 275eeb5ff92SLingrui98 } 276eeb5ff92SLingrui98 277eeb5ff92SLingrui98 // case jmp 278eeb5ff92SLingrui98 when (entry_has_jmp) { 279eeb5ff92SLingrui98 init_entry.tailSlot.offset := pd.jmpOffset 280eeb5ff92SLingrui98 init_entry.tailSlot.valid := new_jmp_is_jal || new_jmp_is_jalr 281eeb5ff92SLingrui98 init_entry.tailSlot.setLowerStatByTarget(io.start_addr, Mux(cfi_is_jalr, io.target, pd.jalTarget), isShare=false) 282eeb5ff92SLingrui98 } 283eeb5ff92SLingrui98 28409c6f1ddSLingrui98 val jmpPft = getLower(io.start_addr) +& pd.jmpOffset +& Mux(pd.rvcMask(pd.jmpOffset), 1.U, 2.U) 285a60a2901SLingrui98 init_entry.pftAddr := Mux(entry_has_jmp && !last_jmp_rvi, jmpPft, getLower(io.start_addr)) 286a60a2901SLingrui98 init_entry.carry := Mux(entry_has_jmp && !last_jmp_rvi, jmpPft(carryPos-instOffsetBits), true.B) 28709c6f1ddSLingrui98 init_entry.isJalr := new_jmp_is_jalr 28809c6f1ddSLingrui98 init_entry.isCall := new_jmp_is_call 28909c6f1ddSLingrui98 init_entry.isRet := new_jmp_is_ret 290f4ebc4b2SLingrui98 // that means fall thru points to the middle of an inst 291ae409b75SSteve Gou init_entry.last_may_be_rvi_call := pd.jmpOffset === (PredictWidth-1).U && !pd.rvcMask(pd.jmpOffset) 29209c6f1ddSLingrui98 29309c6f1ddSLingrui98 // if hit, check whether a new cfi(only br is possible) is detected 29409c6f1ddSLingrui98 val oe = io.old_entry 295eeb5ff92SLingrui98 val br_recorded_vec = oe.getBrRecordedVec(io.cfiIndex.bits) 29609c6f1ddSLingrui98 val br_recorded = br_recorded_vec.asUInt.orR 29709c6f1ddSLingrui98 val is_new_br = cfi_is_br && !br_recorded 29809c6f1ddSLingrui98 val new_br_offset = io.cfiIndex.bits 29909c6f1ddSLingrui98 // vec(i) means new br will be inserted BEFORE old br(i) 300eeb5ff92SLingrui98 val allBrSlotsVec = oe.allSlotsForBr 30109c6f1ddSLingrui98 val new_br_insert_onehot = VecInit((0 until numBr).map{ 30209c6f1ddSLingrui98 i => i match { 303eeb5ff92SLingrui98 case 0 => 304eeb5ff92SLingrui98 !allBrSlotsVec(0).valid || new_br_offset < allBrSlotsVec(0).offset 305eeb5ff92SLingrui98 case idx => 306eeb5ff92SLingrui98 allBrSlotsVec(idx-1).valid && new_br_offset > allBrSlotsVec(idx-1).offset && 307eeb5ff92SLingrui98 (!allBrSlotsVec(idx).valid || new_br_offset < allBrSlotsVec(idx).offset) 30809c6f1ddSLingrui98 } 30909c6f1ddSLingrui98 }) 31009c6f1ddSLingrui98 31109c6f1ddSLingrui98 val old_entry_modified = WireInit(io.old_entry) 31209c6f1ddSLingrui98 for (i <- 0 until numBr) { 313eeb5ff92SLingrui98 val slot = old_entry_modified.allSlotsForBr(i) 314eeb5ff92SLingrui98 when (new_br_insert_onehot(i)) { 315eeb5ff92SLingrui98 slot.valid := true.B 316eeb5ff92SLingrui98 slot.offset := new_br_offset 317b37e4b45SLingrui98 slot.setLowerStatByTarget(io.start_addr, io.target, i == numBr-1) 318eeb5ff92SLingrui98 old_entry_modified.always_taken(i) := true.B 319eeb5ff92SLingrui98 }.elsewhen (new_br_offset > oe.allSlotsForBr(i).offset) { 320eeb5ff92SLingrui98 old_entry_modified.always_taken(i) := false.B 321eeb5ff92SLingrui98 // all other fields remain unchanged 322eeb5ff92SLingrui98 }.otherwise { 323eeb5ff92SLingrui98 // case i == 0, remain unchanged 324eeb5ff92SLingrui98 if (i != 0) { 325b37e4b45SLingrui98 val noNeedToMoveFromFormerSlot = (i == numBr-1).B && !oe.brSlots.last.valid 326eeb5ff92SLingrui98 when (!noNeedToMoveFromFormerSlot) { 327eeb5ff92SLingrui98 slot.fromAnotherSlot(oe.allSlotsForBr(i-1)) 328eeb5ff92SLingrui98 old_entry_modified.always_taken(i) := oe.always_taken(i) 32909c6f1ddSLingrui98 } 330eeb5ff92SLingrui98 } 331eeb5ff92SLingrui98 } 332eeb5ff92SLingrui98 } 33309c6f1ddSLingrui98 334eeb5ff92SLingrui98 // two circumstances: 335eeb5ff92SLingrui98 // 1. oe: | br | j |, new br should be in front of j, thus addr of j should be new pft 336eeb5ff92SLingrui98 // 2. oe: | br | br |, new br could be anywhere between, thus new pft is the addr of either 337eeb5ff92SLingrui98 // the previous last br or the new br 338eeb5ff92SLingrui98 val may_have_to_replace = oe.noEmptySlotForNewBr 339eeb5ff92SLingrui98 val pft_need_to_change = is_new_br && may_have_to_replace 34009c6f1ddSLingrui98 // it should either be the given last br or the new br 34109c6f1ddSLingrui98 when (pft_need_to_change) { 342eeb5ff92SLingrui98 val new_pft_offset = 343710a8720SLingrui98 Mux(!new_br_insert_onehot.asUInt.orR, 344710a8720SLingrui98 new_br_offset, oe.allSlotsForBr.last.offset) 345eeb5ff92SLingrui98 346710a8720SLingrui98 // set jmp to invalid 34709c6f1ddSLingrui98 old_entry_modified.pftAddr := getLower(io.start_addr) + new_pft_offset 34809c6f1ddSLingrui98 old_entry_modified.carry := (getLower(io.start_addr) +& new_pft_offset).head(1).asBool 349f4ebc4b2SLingrui98 old_entry_modified.last_may_be_rvi_call := false.B 35009c6f1ddSLingrui98 old_entry_modified.isCall := false.B 35109c6f1ddSLingrui98 old_entry_modified.isRet := false.B 352eeb5ff92SLingrui98 old_entry_modified.isJalr := false.B 35309c6f1ddSLingrui98 } 35409c6f1ddSLingrui98 35509c6f1ddSLingrui98 val old_entry_jmp_target_modified = WireInit(oe) 356710a8720SLingrui98 val old_target = oe.tailSlot.getTarget(io.start_addr) // may be wrong because we store only 20 lowest bits 357b37e4b45SLingrui98 val old_tail_is_jmp = !oe.tailSlot.sharing 358eeb5ff92SLingrui98 val jalr_target_modified = cfi_is_jalr && (old_target =/= io.target) && old_tail_is_jmp // TODO: pass full jalr target 3593bcae573SLingrui98 when (jalr_target_modified) { 36009c6f1ddSLingrui98 old_entry_jmp_target_modified.setByJmpTarget(io.start_addr, io.target) 36109c6f1ddSLingrui98 old_entry_jmp_target_modified.always_taken := 0.U.asTypeOf(Vec(numBr, Bool())) 36209c6f1ddSLingrui98 } 36309c6f1ddSLingrui98 36409c6f1ddSLingrui98 val old_entry_always_taken = WireInit(oe) 36509c6f1ddSLingrui98 val always_taken_modified_vec = Wire(Vec(numBr, Bool())) // whether modified or not 36609c6f1ddSLingrui98 for (i <- 0 until numBr) { 36709c6f1ddSLingrui98 old_entry_always_taken.always_taken(i) := 36809c6f1ddSLingrui98 oe.always_taken(i) && io.cfiIndex.valid && oe.brValids(i) && io.cfiIndex.bits === oe.brOffset(i) 369710a8720SLingrui98 always_taken_modified_vec(i) := oe.always_taken(i) && !old_entry_always_taken.always_taken(i) 37009c6f1ddSLingrui98 } 37109c6f1ddSLingrui98 val always_taken_modified = always_taken_modified_vec.reduce(_||_) 37209c6f1ddSLingrui98 37309c6f1ddSLingrui98 37409c6f1ddSLingrui98 37509c6f1ddSLingrui98 val derived_from_old_entry = 37609c6f1ddSLingrui98 Mux(is_new_br, old_entry_modified, 3773bcae573SLingrui98 Mux(jalr_target_modified, old_entry_jmp_target_modified, old_entry_always_taken)) 37809c6f1ddSLingrui98 37909c6f1ddSLingrui98 38009c6f1ddSLingrui98 io.new_entry := Mux(!hit, init_entry, derived_from_old_entry) 38109c6f1ddSLingrui98 38209c6f1ddSLingrui98 io.new_br_insert_pos := new_br_insert_onehot 38309c6f1ddSLingrui98 io.taken_mask := VecInit((io.new_entry.brOffset zip io.new_entry.brValids).map{ 38409c6f1ddSLingrui98 case (off, v) => io.cfiIndex.bits === off && io.cfiIndex.valid && v 38509c6f1ddSLingrui98 }) 386803124a6SLingrui98 io.jmp_taken := io.new_entry.jmpValid && io.new_entry.tailSlot.offset === io.cfiIndex.bits 38709c6f1ddSLingrui98 for (i <- 0 until numBr) { 38809c6f1ddSLingrui98 io.mispred_mask(i) := io.new_entry.brValids(i) && io.mispredict_vec(io.new_entry.brOffset(i)) 38909c6f1ddSLingrui98 } 39009c6f1ddSLingrui98 io.mispred_mask.last := io.new_entry.jmpValid && io.mispredict_vec(pd.jmpOffset) 39109c6f1ddSLingrui98 39209c6f1ddSLingrui98 // for perf counters 39309c6f1ddSLingrui98 io.is_init_entry := !hit 3943bcae573SLingrui98 io.is_old_entry := hit && !is_new_br && !jalr_target_modified && !always_taken_modified 39509c6f1ddSLingrui98 io.is_new_br := hit && is_new_br 3963bcae573SLingrui98 io.is_jalr_target_modified := hit && jalr_target_modified 39709c6f1ddSLingrui98 io.is_always_taken_modified := hit && always_taken_modified 398eeb5ff92SLingrui98 io.is_br_full := hit && is_new_br && may_have_to_replace 39909c6f1ddSLingrui98} 40009c6f1ddSLingrui98 401c5c5edaeSJeniusclass FtqPcMemWrapper(numOtherReads: Int)(implicit p: Parameters) extends XSModule with HasBackendRedirectInfo { 402c5c5edaeSJenius val io = IO(new Bundle { 403c5c5edaeSJenius val ifuPtr_w = Input(new FtqPtr) 404c5c5edaeSJenius val ifuPtrPlus1_w = Input(new FtqPtr) 4056bf9b30dSLingrui98 val ifuPtrPlus2_w = Input(new FtqPtr) 406c5c5edaeSJenius val commPtr_w = Input(new FtqPtr) 4076bf9b30dSLingrui98 val commPtrPlus1_w = Input(new FtqPtr) 408c5c5edaeSJenius val ifuPtr_rdata = Output(new Ftq_RF_Components) 409c5c5edaeSJenius val ifuPtrPlus1_rdata = Output(new Ftq_RF_Components) 4106bf9b30dSLingrui98 val ifuPtrPlus2_rdata = Output(new Ftq_RF_Components) 411c5c5edaeSJenius val commPtr_rdata = Output(new Ftq_RF_Components) 4126bf9b30dSLingrui98 val commPtrPlus1_rdata = Output(new Ftq_RF_Components) 413c5c5edaeSJenius 414c5c5edaeSJenius val other_raddrs = Input(Vec(numOtherReads, UInt(log2Ceil(FtqSize).W))) 415c5c5edaeSJenius val other_rdatas = Output(Vec(numOtherReads, new Ftq_RF_Components)) 416c5c5edaeSJenius 417c5c5edaeSJenius val wen = Input(Bool()) 418c5c5edaeSJenius val waddr = Input(UInt(log2Ceil(FtqSize).W)) 419c5c5edaeSJenius val wdata = Input(new Ftq_RF_Components) 420c5c5edaeSJenius }) 421c5c5edaeSJenius 4226bf9b30dSLingrui98 val num_pc_read = numOtherReads + 5 423c5c5edaeSJenius val mem = Module(new SyncDataModuleTemplate(new Ftq_RF_Components, FtqSize, 42428f2cf58SLingrui98 num_pc_read, 1, "FtqPC")) 425c5c5edaeSJenius mem.io.wen(0) := io.wen 426c5c5edaeSJenius mem.io.waddr(0) := io.waddr 427c5c5edaeSJenius mem.io.wdata(0) := io.wdata 428c5c5edaeSJenius 4296bf9b30dSLingrui98 // read one cycle ahead for ftq local reads 430c5c5edaeSJenius val raddr_vec = VecInit(io.other_raddrs ++ 43188bc4f90SLingrui98 Seq(io.ifuPtr_w.value, io.ifuPtrPlus1_w.value, io.ifuPtrPlus2_w.value, io.commPtrPlus1_w.value, io.commPtr_w.value)) 432c5c5edaeSJenius 433c5c5edaeSJenius mem.io.raddr := raddr_vec 434c5c5edaeSJenius 4356bf9b30dSLingrui98 io.other_rdatas := mem.io.rdata.dropRight(5) 4366bf9b30dSLingrui98 io.ifuPtr_rdata := mem.io.rdata.dropRight(4).last 4376bf9b30dSLingrui98 io.ifuPtrPlus1_rdata := mem.io.rdata.dropRight(3).last 4386bf9b30dSLingrui98 io.ifuPtrPlus2_rdata := mem.io.rdata.dropRight(2).last 4396bf9b30dSLingrui98 io.commPtrPlus1_rdata := mem.io.rdata.dropRight(1).last 440c5c5edaeSJenius io.commPtr_rdata := mem.io.rdata.last 441c5c5edaeSJenius} 442c5c5edaeSJenius 44309c6f1ddSLingrui98class Ftq(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper 444e30430c2SJay with HasBackendRedirectInfo with BPUUtils with HasBPUConst with HasPerfEvents 445e30430c2SJay with HasICacheParameters{ 44609c6f1ddSLingrui98 val io = IO(new Bundle { 44709c6f1ddSLingrui98 val fromBpu = Flipped(new BpuToFtqIO) 44809c6f1ddSLingrui98 val fromIfu = Flipped(new IfuToFtqIO) 44909c6f1ddSLingrui98 val fromBackend = Flipped(new CtrlToFtqIO) 45009c6f1ddSLingrui98 45109c6f1ddSLingrui98 val toBpu = new FtqToBpuIO 45209c6f1ddSLingrui98 val toIfu = new FtqToIfuIO 453c5c5edaeSJenius val toICache = new FtqToICacheIO 45409c6f1ddSLingrui98 val toBackend = new FtqToCtrlIO 45509c6f1ddSLingrui98 4567052722fSJay val toPrefetch = new FtqPrefechBundle 4577052722fSJay 45809c6f1ddSLingrui98 val bpuInfo = new Bundle { 45909c6f1ddSLingrui98 val bpRight = Output(UInt(XLEN.W)) 46009c6f1ddSLingrui98 val bpWrong = Output(UInt(XLEN.W)) 46109c6f1ddSLingrui98 } 4621d1e6d4dSJenius 4631d1e6d4dSJenius val mmioCommitRead = Flipped(new mmioCommitRead) 464d2b20d1aSTang Haojin 465d2b20d1aSTang Haojin // for perf 466d2b20d1aSTang Haojin val ControlBTBMissBubble = Output(Bool()) 467d2b20d1aSTang Haojin val TAGEMissBubble = Output(Bool()) 468d2b20d1aSTang Haojin val SCMissBubble = Output(Bool()) 469d2b20d1aSTang Haojin val ITTAGEMissBubble = Output(Bool()) 470d2b20d1aSTang Haojin val RASMissBubble = Output(Bool()) 47109c6f1ddSLingrui98 }) 47209c6f1ddSLingrui98 io.bpuInfo := DontCare 47309c6f1ddSLingrui98 474d2b20d1aSTang Haojin val topdown_stage = RegInit(0.U.asTypeOf(new FrontendTopDownBundle)) 475d2b20d1aSTang Haojin dontTouch(topdown_stage) 476d2b20d1aSTang Haojin // only driven by clock, not valid-ready 477d2b20d1aSTang Haojin topdown_stage := io.fromBpu.resp.bits.topdown_info 478d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info := topdown_stage 479d2b20d1aSTang Haojin 480d2b20d1aSTang Haojin val ifuRedirected = RegInit(VecInit(Seq.fill(FtqSize)(false.B))) 481d2b20d1aSTang Haojin 482d2b20d1aSTang Haojin val backendRedirect = Wire(Valid(new BranchPredictionRedirect)) 4839342624fSGao-Zeyu when(io.fromBackend.redirect.valid) { 4849342624fSGao-Zeyu assert(RegNext(io.fromBackend.ftqIdxAhead.map(_.valid).reduce(_|_))) 4859342624fSGao-Zeyu assert(io.fromBackend.ftqIdxSelOH.valid) 4869342624fSGao-Zeyu assert(PopCount(io.fromBackend.ftqIdxSelOH.bits) === 1.U) 4879342624fSGao-Zeyu } 48809c6f1ddSLingrui98 489df5b4b8eSYinan Xu val stage2Flush = backendRedirect.valid 49009c6f1ddSLingrui98 val backendFlush = stage2Flush || RegNext(stage2Flush) 49109c6f1ddSLingrui98 val ifuFlush = Wire(Bool()) 49209c6f1ddSLingrui98 49309c6f1ddSLingrui98 val flush = stage2Flush || RegNext(stage2Flush) 49409c6f1ddSLingrui98 49509c6f1ddSLingrui98 val allowBpuIn, allowToIfu = WireInit(false.B) 49609c6f1ddSLingrui98 val flushToIfu = !allowToIfu 4979342624fSGao-Zeyu allowBpuIn := !ifuFlush && !backendRedirect.valid 4989342624fSGao-Zeyu allowToIfu := !ifuFlush && !backendRedirect.valid 49909c6f1ddSLingrui98 500f56177cbSJenius def copyNum = 5 501e30430c2SJay val bpuPtr, ifuPtr, ifuWbPtr, commPtr = RegInit(FtqPtr(false.B, 0.U)) 502c9bc5480SLingrui98 val ifuPtrPlus1 = RegInit(FtqPtr(false.B, 1.U)) 5036bf9b30dSLingrui98 val ifuPtrPlus2 = RegInit(FtqPtr(false.B, 2.U)) 5046bf9b30dSLingrui98 val commPtrPlus1 = RegInit(FtqPtr(false.B, 1.U)) 505f56177cbSJenius val copied_ifu_ptr = Seq.fill(copyNum)(RegInit(FtqPtr(false.B, 0.U))) 506dc270d3bSJenius val copied_bpu_ptr = Seq.fill(copyNum)(RegInit(FtqPtr(false.B, 0.U))) 5076bf9b30dSLingrui98 require(FtqSize >= 4) 508c5c5edaeSJenius val ifuPtr_write = WireInit(ifuPtr) 509c5c5edaeSJenius val ifuPtrPlus1_write = WireInit(ifuPtrPlus1) 5106bf9b30dSLingrui98 val ifuPtrPlus2_write = WireInit(ifuPtrPlus2) 511c5c5edaeSJenius val ifuWbPtr_write = WireInit(ifuWbPtr) 512c5c5edaeSJenius val commPtr_write = WireInit(commPtr) 5136bf9b30dSLingrui98 val commPtrPlus1_write = WireInit(commPtrPlus1) 514c5c5edaeSJenius ifuPtr := ifuPtr_write 515c5c5edaeSJenius ifuPtrPlus1 := ifuPtrPlus1_write 5166bf9b30dSLingrui98 ifuPtrPlus2 := ifuPtrPlus2_write 517c5c5edaeSJenius ifuWbPtr := ifuWbPtr_write 518c5c5edaeSJenius commPtr := commPtr_write 519f83ef67eSLingrui98 commPtrPlus1 := commPtrPlus1_write 520f56177cbSJenius copied_ifu_ptr.map{ptr => 521f56177cbSJenius ptr := ifuPtr_write 522f56177cbSJenius dontTouch(ptr) 523f56177cbSJenius } 52409c6f1ddSLingrui98 val validEntries = distanceBetween(bpuPtr, commPtr) 52543aca6c2SGuokai Chen val canCommit = Wire(Bool()) 52609c6f1ddSLingrui98 52709c6f1ddSLingrui98 // ********************************************************************** 52809c6f1ddSLingrui98 // **************************** enq from bpu **************************** 52909c6f1ddSLingrui98 // ********************************************************************** 53043aca6c2SGuokai Chen val new_entry_ready = validEntries < FtqSize.U || canCommit 53109c6f1ddSLingrui98 io.fromBpu.resp.ready := new_entry_ready 53209c6f1ddSLingrui98 53309c6f1ddSLingrui98 val bpu_s2_resp = io.fromBpu.resp.bits.s2 534cb4f77ceSLingrui98 val bpu_s3_resp = io.fromBpu.resp.bits.s3 535adc0b8dfSGuokai Chen val bpu_s2_redirect = bpu_s2_resp.valid(3) && bpu_s2_resp.hasRedirect(3) 536adc0b8dfSGuokai Chen val bpu_s3_redirect = bpu_s3_resp.valid(3) && bpu_s3_resp.hasRedirect(3) 53709c6f1ddSLingrui98 53809c6f1ddSLingrui98 io.toBpu.enq_ptr := bpuPtr 539935edac4STang Haojin val enq_fire = io.fromBpu.resp.fire && allowBpuIn // from bpu s1 540935edac4STang Haojin val bpu_in_fire = (io.fromBpu.resp.fire || bpu_s2_redirect || bpu_s3_redirect) && allowBpuIn 54109c6f1ddSLingrui98 542b37e4b45SLingrui98 val bpu_in_resp = io.fromBpu.resp.bits.selectedResp 543adc0b8dfSGuokai Chen val bpu_in_stage = io.fromBpu.resp.bits.selectedRespIdxForFtq 54409c6f1ddSLingrui98 val bpu_in_resp_ptr = Mux(bpu_in_stage === BP_S1, bpuPtr, bpu_in_resp.ftq_idx) 54509c6f1ddSLingrui98 val bpu_in_resp_idx = bpu_in_resp_ptr.value 54609c6f1ddSLingrui98 547378f00d9SJenius // read ports: prefetchReq ++ ifuReq1 + ifuReq2 + ifuReq3 + commitUpdate2 + commitUpdate 548378f00d9SJenius val ftq_pc_mem = Module(new FtqPcMemWrapper(1)) 5496bf9b30dSLingrui98 // resp from uBTB 550c5c5edaeSJenius ftq_pc_mem.io.wen := bpu_in_fire 551c5c5edaeSJenius ftq_pc_mem.io.waddr := bpu_in_resp_idx 552c5c5edaeSJenius ftq_pc_mem.io.wdata.fromBranchPrediction(bpu_in_resp) 55309c6f1ddSLingrui98 55409c6f1ddSLingrui98 // ifuRedirect + backendRedirect + commit 5559342624fSGao-Zeyu val ftq_redirect_sram = Module(new FtqNRSRAM(new Ftq_Redirect_SRAMEntry, 1+BackendRedirectNum+1)) 55609c6f1ddSLingrui98 // these info is intended to enq at the last stage of bpu 557adc0b8dfSGuokai Chen ftq_redirect_sram.io.wen := io.fromBpu.resp.bits.lastStage.valid(3) 55809c6f1ddSLingrui98 ftq_redirect_sram.io.waddr := io.fromBpu.resp.bits.lastStage.ftq_idx.value 559c2d1ec7dSLingrui98 ftq_redirect_sram.io.wdata := io.fromBpu.resp.bits.last_stage_spec_info 56049cbc998SLingrui98 println(f"ftq redirect SRAM: entry ${ftq_redirect_sram.io.wdata.getWidth} * ${FtqSize} * 3") 56149cbc998SLingrui98 println(f"ftq redirect SRAM: ahead fh ${ftq_redirect_sram.io.wdata.afhob.getWidth} * ${FtqSize} * 3") 56209c6f1ddSLingrui98 56309c6f1ddSLingrui98 val ftq_meta_1r_sram = Module(new FtqNRSRAM(new Ftq_1R_SRAMEntry, 1)) 56409c6f1ddSLingrui98 // these info is intended to enq at the last stage of bpu 565adc0b8dfSGuokai Chen ftq_meta_1r_sram.io.wen := io.fromBpu.resp.bits.lastStage.valid(3) 56609c6f1ddSLingrui98 ftq_meta_1r_sram.io.waddr := io.fromBpu.resp.bits.lastStage.ftq_idx.value 567c2d1ec7dSLingrui98 ftq_meta_1r_sram.io.wdata.meta := io.fromBpu.resp.bits.last_stage_meta 56809c6f1ddSLingrui98 // ifuRedirect + backendRedirect + commit 5699342624fSGao-Zeyu val ftb_entry_mem = Module(new SyncDataModuleTemplate(new FTBEntry, FtqSize, 1+BackendRedirectNum+1, 1)) 570adc0b8dfSGuokai Chen ftb_entry_mem.io.wen(0) := io.fromBpu.resp.bits.lastStage.valid(3) 57109c6f1ddSLingrui98 ftb_entry_mem.io.waddr(0) := io.fromBpu.resp.bits.lastStage.ftq_idx.value 572c2d1ec7dSLingrui98 ftb_entry_mem.io.wdata(0) := io.fromBpu.resp.bits.last_stage_ftb_entry 57309c6f1ddSLingrui98 57409c6f1ddSLingrui98 57509c6f1ddSLingrui98 // multi-write 576b0ed7239SLingrui98 val update_target = Reg(Vec(FtqSize, UInt(VAddrBits.W))) // could be taken target or fallThrough //TODO: remove this 5776bf9b30dSLingrui98 val newest_entry_target = Reg(UInt(VAddrBits.W)) 5786bf9b30dSLingrui98 val newest_entry_ptr = Reg(new FtqPtr) 57909c6f1ddSLingrui98 val cfiIndex_vec = Reg(Vec(FtqSize, ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)))) 58009c6f1ddSLingrui98 val mispredict_vec = Reg(Vec(FtqSize, Vec(PredictWidth, Bool()))) 58109c6f1ddSLingrui98 val pred_stage = Reg(Vec(FtqSize, UInt(2.W))) 582209a4cafSSteve Gou val pred_s1_cycle = if (!env.FPGAPlatform) Some(Reg(Vec(FtqSize, UInt(64.W)))) else None 58309c6f1ddSLingrui98 584b5808fc2Ssfencevma val c_invalid :: c_valid :: c_commited :: Nil = Enum(3) 58509c6f1ddSLingrui98 val commitStateQueue = RegInit(VecInit(Seq.fill(FtqSize) { 58609c6f1ddSLingrui98 VecInit(Seq.fill(PredictWidth)(c_invalid)) 58709c6f1ddSLingrui98 })) 58809c6f1ddSLingrui98 58909c6f1ddSLingrui98 val f_to_send :: f_sent :: Nil = Enum(2) 59009c6f1ddSLingrui98 val entry_fetch_status = RegInit(VecInit(Seq.fill(FtqSize)(f_sent))) 59109c6f1ddSLingrui98 59209c6f1ddSLingrui98 val h_not_hit :: h_false_hit :: h_hit :: Nil = Enum(3) 59309c6f1ddSLingrui98 val entry_hit_status = RegInit(VecInit(Seq.fill(FtqSize)(h_not_hit))) 59409c6f1ddSLingrui98 595f63797a4SLingrui98 // modify registers one cycle later to cut critical path 596f63797a4SLingrui98 val last_cycle_bpu_in = RegNext(bpu_in_fire) 5976bf9b30dSLingrui98 val last_cycle_bpu_in_ptr = RegNext(bpu_in_resp_ptr) 5986bf9b30dSLingrui98 val last_cycle_bpu_in_idx = last_cycle_bpu_in_ptr.value 599adc0b8dfSGuokai Chen val last_cycle_bpu_target = RegNext(bpu_in_resp.getTarget(3)) 600adc0b8dfSGuokai Chen val last_cycle_cfiIndex = RegNext(bpu_in_resp.cfiIndex(3)) 601f63797a4SLingrui98 val last_cycle_bpu_in_stage = RegNext(bpu_in_stage) 602f56177cbSJenius 6037be982afSLingrui98 def extra_copyNum_for_commitStateQueue = 2 6047be982afSLingrui98 val copied_last_cycle_bpu_in = VecInit(Seq.fill(copyNum+extra_copyNum_for_commitStateQueue)(RegNext(bpu_in_fire))) 6057be982afSLingrui98 val copied_last_cycle_bpu_in_ptr_for_ftq = VecInit(Seq.fill(extra_copyNum_for_commitStateQueue)(RegNext(bpu_in_resp_ptr))) 606f56177cbSJenius 607f63797a4SLingrui98 when (last_cycle_bpu_in) { 608f63797a4SLingrui98 entry_fetch_status(last_cycle_bpu_in_idx) := f_to_send 609f63797a4SLingrui98 cfiIndex_vec(last_cycle_bpu_in_idx) := last_cycle_cfiIndex 610f63797a4SLingrui98 pred_stage(last_cycle_bpu_in_idx) := last_cycle_bpu_in_stage 6116bf9b30dSLingrui98 612b0ed7239SLingrui98 update_target(last_cycle_bpu_in_idx) := last_cycle_bpu_target // TODO: remove this 6136bf9b30dSLingrui98 newest_entry_target := last_cycle_bpu_target 6146bf9b30dSLingrui98 newest_entry_ptr := last_cycle_bpu_in_ptr 61509c6f1ddSLingrui98 } 61609c6f1ddSLingrui98 6177be982afSLingrui98 // reduce fanout by delay write for a cycle 6187be982afSLingrui98 when (RegNext(last_cycle_bpu_in)) { 6197be982afSLingrui98 mispredict_vec(RegNext(last_cycle_bpu_in_idx)) := WireInit(VecInit(Seq.fill(PredictWidth)(false.B))) 6207be982afSLingrui98 } 6217be982afSLingrui98 622209a4cafSSteve Gou // record s1 pred cycles 623209a4cafSSteve Gou pred_s1_cycle.map(vec => { 624209a4cafSSteve Gou when (bpu_in_fire && (bpu_in_stage === BP_S1)) { 625209a4cafSSteve Gou vec(bpu_in_resp_ptr.value) := bpu_in_resp.full_pred(0).predCycle.getOrElse(0.U) 626209a4cafSSteve Gou } 627209a4cafSSteve Gou }) 628209a4cafSSteve Gou 6297be982afSLingrui98 // reduce fanout using copied last_cycle_bpu_in and copied last_cycle_bpu_in_ptr 6307be982afSLingrui98 val copied_last_cycle_bpu_in_for_ftq = copied_last_cycle_bpu_in.takeRight(extra_copyNum_for_commitStateQueue) 6317be982afSLingrui98 copied_last_cycle_bpu_in_for_ftq.zip(copied_last_cycle_bpu_in_ptr_for_ftq).zipWithIndex.map { 6327be982afSLingrui98 case ((in, ptr), i) => 6337be982afSLingrui98 when (in) { 6347be982afSLingrui98 val perSetEntries = FtqSize / extra_copyNum_for_commitStateQueue // 32 6357be982afSLingrui98 require(FtqSize % extra_copyNum_for_commitStateQueue == 0) 6367be982afSLingrui98 for (j <- 0 until perSetEntries) { 6379361b0c5SLingrui98 when (ptr.value === (i*perSetEntries+j).U) { 6387be982afSLingrui98 commitStateQueue(i*perSetEntries+j) := VecInit(Seq.fill(PredictWidth)(c_invalid)) 6397be982afSLingrui98 } 6407be982afSLingrui98 } 6417be982afSLingrui98 } 6429361b0c5SLingrui98 } 6437be982afSLingrui98 644873dc383SLingrui98 // num cycle is fixed 645873dc383SLingrui98 io.toBackend.newest_entry_ptr := RegNext(newest_entry_ptr) 646873dc383SLingrui98 io.toBackend.newest_entry_target := RegNext(newest_entry_target) 647873dc383SLingrui98 648f63797a4SLingrui98 64909c6f1ddSLingrui98 bpuPtr := bpuPtr + enq_fire 650dc270d3bSJenius copied_bpu_ptr.map(_ := bpuPtr + enq_fire) 651c9bc5480SLingrui98 when (io.toIfu.req.fire && allowToIfu) { 652c5c5edaeSJenius ifuPtr_write := ifuPtrPlus1 6536bf9b30dSLingrui98 ifuPtrPlus1_write := ifuPtrPlus2 6546bf9b30dSLingrui98 ifuPtrPlus2_write := ifuPtrPlus2 + 1.U 655c9bc5480SLingrui98 } 65609c6f1ddSLingrui98 65709c6f1ddSLingrui98 // only use ftb result to assign hit status 658adc0b8dfSGuokai Chen when (bpu_s2_resp.valid(3)) { 659adc0b8dfSGuokai Chen entry_hit_status(bpu_s2_resp.ftq_idx.value) := Mux(bpu_s2_resp.full_pred(3).hit, h_hit, h_not_hit) 66009c6f1ddSLingrui98 } 66109c6f1ddSLingrui98 66209c6f1ddSLingrui98 6632f4a3aa4SLingrui98 io.toIfu.flushFromBpu.s2.valid := bpu_s2_redirect 66409c6f1ddSLingrui98 io.toIfu.flushFromBpu.s2.bits := bpu_s2_resp.ftq_idx 665adc0b8dfSGuokai Chen when (bpu_s2_redirect) { 66609c6f1ddSLingrui98 bpuPtr := bpu_s2_resp.ftq_idx + 1.U 667dc270d3bSJenius copied_bpu_ptr.map(_ := bpu_s2_resp.ftq_idx + 1.U) 66809c6f1ddSLingrui98 // only when ifuPtr runs ahead of bpu s2 resp should we recover it 66909c6f1ddSLingrui98 when (!isBefore(ifuPtr, bpu_s2_resp.ftq_idx)) { 670c5c5edaeSJenius ifuPtr_write := bpu_s2_resp.ftq_idx 671c5c5edaeSJenius ifuPtrPlus1_write := bpu_s2_resp.ftq_idx + 1.U 6726bf9b30dSLingrui98 ifuPtrPlus2_write := bpu_s2_resp.ftq_idx + 2.U 67309c6f1ddSLingrui98 } 67409c6f1ddSLingrui98 } 67509c6f1ddSLingrui98 676cb4f77ceSLingrui98 io.toIfu.flushFromBpu.s3.valid := bpu_s3_redirect 677cb4f77ceSLingrui98 io.toIfu.flushFromBpu.s3.bits := bpu_s3_resp.ftq_idx 678adc0b8dfSGuokai Chen when (bpu_s3_redirect) { 679cb4f77ceSLingrui98 bpuPtr := bpu_s3_resp.ftq_idx + 1.U 680dc270d3bSJenius copied_bpu_ptr.map(_ := bpu_s3_resp.ftq_idx + 1.U) 681cb4f77ceSLingrui98 // only when ifuPtr runs ahead of bpu s2 resp should we recover it 682cb4f77ceSLingrui98 when (!isBefore(ifuPtr, bpu_s3_resp.ftq_idx)) { 683c5c5edaeSJenius ifuPtr_write := bpu_s3_resp.ftq_idx 684c5c5edaeSJenius ifuPtrPlus1_write := bpu_s3_resp.ftq_idx + 1.U 6856bf9b30dSLingrui98 ifuPtrPlus2_write := bpu_s3_resp.ftq_idx + 2.U 686cb4f77ceSLingrui98 } 687cb4f77ceSLingrui98 } 688cb4f77ceSLingrui98 68909c6f1ddSLingrui98 XSError(isBefore(bpuPtr, ifuPtr) && !isFull(bpuPtr, ifuPtr), "\nifuPtr is before bpuPtr!\n") 6902448f137SGuokai Chen XSError(isBefore(ifuWbPtr, commPtr) && !isFull(ifuWbPtr, commPtr), "\ncommPtr is before ifuWbPtr!\n") 69109c6f1ddSLingrui98 692dc270d3bSJenius (0 until copyNum).map{i => 693dc270d3bSJenius XSError(copied_bpu_ptr(i) =/= bpuPtr, "\ncopiedBpuPtr is different from bpuPtr!\n") 694dc270d3bSJenius } 695dc270d3bSJenius 69609c6f1ddSLingrui98 // **************************************************************** 69709c6f1ddSLingrui98 // **************************** to ifu **************************** 69809c6f1ddSLingrui98 // **************************************************************** 699f22cf846SJenius // 0 for ifu, and 1-4 for ICache 700935edac4STang Haojin val bpu_in_bypass_buf = RegEnable(ftq_pc_mem.io.wdata, bpu_in_fire) 701935edac4STang Haojin val copied_bpu_in_bypass_buf = VecInit(Seq.fill(copyNum)(RegEnable(ftq_pc_mem.io.wdata, bpu_in_fire))) 702f56177cbSJenius val bpu_in_bypass_buf_for_ifu = bpu_in_bypass_buf 70309c6f1ddSLingrui98 val bpu_in_bypass_ptr = RegNext(bpu_in_resp_ptr) 70409c6f1ddSLingrui98 val last_cycle_to_ifu_fire = RegNext(io.toIfu.req.fire) 70509c6f1ddSLingrui98 706f56177cbSJenius val copied_bpu_in_bypass_ptr = VecInit(Seq.fill(copyNum)(RegNext(bpu_in_resp_ptr))) 707f56177cbSJenius val copied_last_cycle_to_ifu_fire = VecInit(Seq.fill(copyNum)(RegNext(io.toIfu.req.fire))) 70888bc4f90SLingrui98 70909c6f1ddSLingrui98 // read pc and target 7106bf9b30dSLingrui98 ftq_pc_mem.io.ifuPtr_w := ifuPtr_write 7116bf9b30dSLingrui98 ftq_pc_mem.io.ifuPtrPlus1_w := ifuPtrPlus1_write 7126bf9b30dSLingrui98 ftq_pc_mem.io.ifuPtrPlus2_w := ifuPtrPlus2_write 7136bf9b30dSLingrui98 ftq_pc_mem.io.commPtr_w := commPtr_write 7146bf9b30dSLingrui98 ftq_pc_mem.io.commPtrPlus1_w := commPtrPlus1_write 715c5c5edaeSJenius 71609c6f1ddSLingrui98 7175ff19bd8SLingrui98 io.toIfu.req.bits.ftqIdx := ifuPtr 718f63797a4SLingrui98 719f56177cbSJenius val toICachePcBundle = Wire(Vec(copyNum,new Ftq_RF_Components)) 720dc270d3bSJenius val toICacheEntryToSend = Wire(Vec(copyNum,Bool())) 721b37e4b45SLingrui98 val toIfuPcBundle = Wire(new Ftq_RF_Components) 722f63797a4SLingrui98 val entry_is_to_send = WireInit(entry_fetch_status(ifuPtr.value) === f_to_send) 723f63797a4SLingrui98 val entry_ftq_offset = WireInit(cfiIndex_vec(ifuPtr.value)) 7246bf9b30dSLingrui98 val entry_next_addr = Wire(UInt(VAddrBits.W)) 725b004fa13SJenius 726f56177cbSJenius val pc_mem_ifu_ptr_rdata = VecInit(Seq.fill(copyNum)(RegNext(ftq_pc_mem.io.ifuPtr_rdata))) 727f56177cbSJenius val pc_mem_ifu_plus1_rdata = VecInit(Seq.fill(copyNum)(RegNext(ftq_pc_mem.io.ifuPtrPlus1_rdata))) 728b0ed7239SLingrui98 val diff_entry_next_addr = WireInit(update_target(ifuPtr.value)) //TODO: remove this 729f63797a4SLingrui98 730dc270d3bSJenius 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)))) 731dc270d3bSJenius 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))) 732dc270d3bSJenius 733f56177cbSJenius for(i <- 0 until copyNum){ 734f56177cbSJenius when(copied_last_cycle_bpu_in(i) && copied_bpu_in_bypass_ptr(i) === copied_ifu_ptr(i)){ 735f56177cbSJenius toICachePcBundle(i) := copied_bpu_in_bypass_buf(i) 736dc270d3bSJenius toICacheEntryToSend(i) := true.B 737f56177cbSJenius }.elsewhen(copied_last_cycle_to_ifu_fire(i)){ 738f56177cbSJenius toICachePcBundle(i) := pc_mem_ifu_plus1_rdata(i) 739dc270d3bSJenius toICacheEntryToSend(i) := copied_ifu_plus1_to_send(i) 740f56177cbSJenius }.otherwise{ 741f56177cbSJenius toICachePcBundle(i) := pc_mem_ifu_ptr_rdata(i) 742dc270d3bSJenius toICacheEntryToSend(i) := copied_ifu_ptr_to_send(i) 743f56177cbSJenius } 744f56177cbSJenius } 745f56177cbSJenius 746873dc383SLingrui98 // TODO: reconsider target address bypass logic 74709c6f1ddSLingrui98 when (last_cycle_bpu_in && bpu_in_bypass_ptr === ifuPtr) { 74888bc4f90SLingrui98 toIfuPcBundle := bpu_in_bypass_buf_for_ifu 749f678dd91SSteve Gou entry_is_to_send := true.B 7506bf9b30dSLingrui98 entry_next_addr := last_cycle_bpu_target 751f63797a4SLingrui98 entry_ftq_offset := last_cycle_cfiIndex 752b0ed7239SLingrui98 diff_entry_next_addr := last_cycle_bpu_target // TODO: remove this 75309c6f1ddSLingrui98 }.elsewhen (last_cycle_to_ifu_fire) { 754c5c5edaeSJenius toIfuPcBundle := RegNext(ftq_pc_mem.io.ifuPtrPlus1_rdata) 755c5c5edaeSJenius entry_is_to_send := RegNext(entry_fetch_status(ifuPtrPlus1.value) === f_to_send) || 756c5c5edaeSJenius RegNext(last_cycle_bpu_in && bpu_in_bypass_ptr === (ifuPtrPlus1)) // reduce potential bubbles 757ed434d67SLingrui98 entry_next_addr := Mux(last_cycle_bpu_in && bpu_in_bypass_ptr === (ifuPtrPlus1), 75888bc4f90SLingrui98 bpu_in_bypass_buf_for_ifu.startAddr, 759fef810c0SLingrui98 Mux(ifuPtr === newest_entry_ptr, 7606bf9b30dSLingrui98 newest_entry_target, 761f83ef67eSLingrui98 RegNext(ftq_pc_mem.io.ifuPtrPlus2_rdata.startAddr))) // ifuPtr+2 762c5c5edaeSJenius }.otherwise { 763c5c5edaeSJenius toIfuPcBundle := RegNext(ftq_pc_mem.io.ifuPtr_rdata) 76428f2cf58SLingrui98 entry_is_to_send := RegNext(entry_fetch_status(ifuPtr.value) === f_to_send) || 76528f2cf58SLingrui98 RegNext(last_cycle_bpu_in && bpu_in_bypass_ptr === ifuPtr) // reduce potential bubbles 7666bf9b30dSLingrui98 entry_next_addr := Mux(last_cycle_bpu_in && bpu_in_bypass_ptr === (ifuPtrPlus1), 76788bc4f90SLingrui98 bpu_in_bypass_buf_for_ifu.startAddr, 768fef810c0SLingrui98 Mux(ifuPtr === newest_entry_ptr, 7696bf9b30dSLingrui98 newest_entry_target, 770f83ef67eSLingrui98 RegNext(ftq_pc_mem.io.ifuPtrPlus1_rdata.startAddr))) // ifuPtr+1 77109c6f1ddSLingrui98 } 77209c6f1ddSLingrui98 773f678dd91SSteve Gou io.toIfu.req.valid := entry_is_to_send && ifuPtr =/= bpuPtr 774f63797a4SLingrui98 io.toIfu.req.bits.nextStartAddr := entry_next_addr 775f63797a4SLingrui98 io.toIfu.req.bits.ftqOffset := entry_ftq_offset 776b37e4b45SLingrui98 io.toIfu.req.bits.fromFtqPcBundle(toIfuPcBundle) 777c5c5edaeSJenius 778c5c5edaeSJenius io.toICache.req.valid := entry_is_to_send && ifuPtr =/= bpuPtr 779dc270d3bSJenius io.toICache.req.bits.readValid.zipWithIndex.map{case(copy, i) => copy := toICacheEntryToSend(i) && copied_ifu_ptr(i) =/= copied_bpu_ptr(i)} 780b004fa13SJenius io.toICache.req.bits.pcMemRead.zipWithIndex.map{case(copy,i) => copy.fromFtqPcBundle(toICachePcBundle(i))} 781b004fa13SJenius // io.toICache.req.bits.bypassSelect := last_cycle_bpu_in && bpu_in_bypass_ptr === ifuPtr 782b004fa13SJenius // io.toICache.req.bits.bpuBypassWrite.zipWithIndex.map{case(bypassWrtie, i) => 783b004fa13SJenius // bypassWrtie.startAddr := bpu_in_bypass_buf.tail(i).startAddr 784b004fa13SJenius // bypassWrtie.nextlineStart := bpu_in_bypass_buf.tail(i).nextLineAddr 785b004fa13SJenius // } 786f22cf846SJenius 787b0ed7239SLingrui98 // TODO: remove this 788b0ed7239SLingrui98 XSError(io.toIfu.req.valid && diff_entry_next_addr =/= entry_next_addr, 7895a674179SLingrui98 p"\nifu_req_target wrong! ifuPtr: ${ifuPtr}, entry_next_addr: ${Hexadecimal(entry_next_addr)} diff_entry_next_addr: ${Hexadecimal(diff_entry_next_addr)}\n") 790b0ed7239SLingrui98 79109c6f1ddSLingrui98 // when fall through is smaller in value than start address, there must be a false hit 792b37e4b45SLingrui98 when (toIfuPcBundle.fallThruError && entry_hit_status(ifuPtr.value) === h_hit) { 79309c6f1ddSLingrui98 when (io.toIfu.req.fire && 794cb4f77ceSLingrui98 !(bpu_s2_redirect && bpu_s2_resp.ftq_idx === ifuPtr) && 795cb4f77ceSLingrui98 !(bpu_s3_redirect && bpu_s3_resp.ftq_idx === ifuPtr) 79609c6f1ddSLingrui98 ) { 79709c6f1ddSLingrui98 entry_hit_status(ifuPtr.value) := h_false_hit 798352db50aSLingrui98 // XSError(true.B, "FTB false hit by fallThroughError, startAddr: %x, fallTHru: %x\n", io.toIfu.req.bits.startAddr, io.toIfu.req.bits.nextStartAddr) 79909c6f1ddSLingrui98 } 800b37e4b45SLingrui98 XSDebug(true.B, "fallThruError! start:%x, fallThru:%x\n", io.toIfu.req.bits.startAddr, io.toIfu.req.bits.nextStartAddr) 80109c6f1ddSLingrui98 } 80209c6f1ddSLingrui98 803a60a2901SLingrui98 XSPerfAccumulate(f"fall_through_error_to_ifu", toIfuPcBundle.fallThruError && entry_hit_status(ifuPtr.value) === h_hit && 804a60a2901SLingrui98 io.toIfu.req.fire && !(bpu_s2_redirect && bpu_s2_resp.ftq_idx === ifuPtr) && !(bpu_s3_redirect && bpu_s3_resp.ftq_idx === ifuPtr)) 805a60a2901SLingrui98 80609c6f1ddSLingrui98 val ifu_req_should_be_flushed = 807cb4f77ceSLingrui98 io.toIfu.flushFromBpu.shouldFlushByStage2(io.toIfu.req.bits.ftqIdx) || 808cb4f77ceSLingrui98 io.toIfu.flushFromBpu.shouldFlushByStage3(io.toIfu.req.bits.ftqIdx) 80909c6f1ddSLingrui98 81009c6f1ddSLingrui98 when (io.toIfu.req.fire && !ifu_req_should_be_flushed) { 81109c6f1ddSLingrui98 entry_fetch_status(ifuPtr.value) := f_sent 81209c6f1ddSLingrui98 } 81309c6f1ddSLingrui98 81409c6f1ddSLingrui98 // ********************************************************************* 81509c6f1ddSLingrui98 // **************************** wb from ifu **************************** 81609c6f1ddSLingrui98 // ********************************************************************* 81709c6f1ddSLingrui98 val pdWb = io.fromIfu.pdWb 81809c6f1ddSLingrui98 val pds = pdWb.bits.pd 81909c6f1ddSLingrui98 val ifu_wb_valid = pdWb.valid 82009c6f1ddSLingrui98 val ifu_wb_idx = pdWb.bits.ftqIdx.value 82109c6f1ddSLingrui98 // read ports: commit update 82209c6f1ddSLingrui98 val ftq_pd_mem = Module(new SyncDataModuleTemplate(new Ftq_pd_Entry, FtqSize, 1, 1)) 82309c6f1ddSLingrui98 ftq_pd_mem.io.wen(0) := ifu_wb_valid 82409c6f1ddSLingrui98 ftq_pd_mem.io.waddr(0) := pdWb.bits.ftqIdx.value 82509c6f1ddSLingrui98 ftq_pd_mem.io.wdata(0).fromPdWb(pdWb.bits) 82609c6f1ddSLingrui98 82709c6f1ddSLingrui98 val hit_pd_valid = entry_hit_status(ifu_wb_idx) === h_hit && ifu_wb_valid 82809c6f1ddSLingrui98 val hit_pd_mispred = hit_pd_valid && pdWb.bits.misOffset.valid 82909c6f1ddSLingrui98 val hit_pd_mispred_reg = RegNext(hit_pd_mispred, init=false.B) 830005e809bSJiuyang Liu val pd_reg = RegEnable(pds, pdWb.valid) 831005e809bSJiuyang Liu val start_pc_reg = RegEnable(pdWb.bits.pc(0), pdWb.valid) 832005e809bSJiuyang Liu val wb_idx_reg = RegEnable(ifu_wb_idx, pdWb.valid) 83309c6f1ddSLingrui98 83409c6f1ddSLingrui98 when (ifu_wb_valid) { 83509c6f1ddSLingrui98 val comm_stq_wen = VecInit(pds.map(_.valid).zip(pdWb.bits.instrRange).map{ 83609c6f1ddSLingrui98 case (v, inRange) => v && inRange 83709c6f1ddSLingrui98 }) 83809c6f1ddSLingrui98 (commitStateQueue(ifu_wb_idx) zip comm_stq_wen).map{ 83909c6f1ddSLingrui98 case (qe, v) => when (v) { qe := c_valid } 84009c6f1ddSLingrui98 } 84109c6f1ddSLingrui98 } 84209c6f1ddSLingrui98 843c5c5edaeSJenius when (ifu_wb_valid) { 844c5c5edaeSJenius ifuWbPtr_write := ifuWbPtr + 1.U 845c5c5edaeSJenius } 84609c6f1ddSLingrui98 847f21bbcb2SGuokai Chen XSError(ifu_wb_valid && isAfter(pdWb.bits.ftqIdx, ifuPtr), "IFU returned a predecode before its req, check IFU") 848f21bbcb2SGuokai Chen 84909c6f1ddSLingrui98 ftb_entry_mem.io.raddr.head := ifu_wb_idx 85009c6f1ddSLingrui98 val has_false_hit = WireInit(false.B) 85109c6f1ddSLingrui98 when (RegNext(hit_pd_valid)) { 85209c6f1ddSLingrui98 // check for false hit 85309c6f1ddSLingrui98 val pred_ftb_entry = ftb_entry_mem.io.rdata.head 854eeb5ff92SLingrui98 val brSlots = pred_ftb_entry.brSlots 855eeb5ff92SLingrui98 val tailSlot = pred_ftb_entry.tailSlot 85609c6f1ddSLingrui98 // we check cfis that bpu predicted 85709c6f1ddSLingrui98 858eeb5ff92SLingrui98 // bpu predicted branches but denied by predecode 859eeb5ff92SLingrui98 val br_false_hit = 860eeb5ff92SLingrui98 brSlots.map{ 861eeb5ff92SLingrui98 s => s.valid && !(pd_reg(s.offset).valid && pd_reg(s.offset).isBr) 862eeb5ff92SLingrui98 }.reduce(_||_) || 863b37e4b45SLingrui98 (tailSlot.valid && pred_ftb_entry.tailSlot.sharing && 864eeb5ff92SLingrui98 !(pd_reg(tailSlot.offset).valid && pd_reg(tailSlot.offset).isBr)) 865eeb5ff92SLingrui98 866eeb5ff92SLingrui98 val jmpOffset = tailSlot.offset 86709c6f1ddSLingrui98 val jmp_pd = pd_reg(jmpOffset) 86809c6f1ddSLingrui98 val jal_false_hit = pred_ftb_entry.jmpValid && 86909c6f1ddSLingrui98 ((pred_ftb_entry.isJal && !(jmp_pd.valid && jmp_pd.isJal)) || 87009c6f1ddSLingrui98 (pred_ftb_entry.isJalr && !(jmp_pd.valid && jmp_pd.isJalr)) || 87109c6f1ddSLingrui98 (pred_ftb_entry.isCall && !(jmp_pd.valid && jmp_pd.isCall)) || 87209c6f1ddSLingrui98 (pred_ftb_entry.isRet && !(jmp_pd.valid && jmp_pd.isRet)) 87309c6f1ddSLingrui98 ) 87409c6f1ddSLingrui98 87509c6f1ddSLingrui98 has_false_hit := br_false_hit || jal_false_hit || hit_pd_mispred_reg 87665fddcf0Szoujr XSDebug(has_false_hit, "FTB false hit by br or jal or hit_pd, startAddr: %x\n", pdWb.bits.pc(0)) 87765fddcf0Szoujr 878352db50aSLingrui98 // assert(!has_false_hit) 87909c6f1ddSLingrui98 } 88009c6f1ddSLingrui98 88109c6f1ddSLingrui98 when (has_false_hit) { 88209c6f1ddSLingrui98 entry_hit_status(wb_idx_reg) := h_false_hit 88309c6f1ddSLingrui98 } 88409c6f1ddSLingrui98 88509c6f1ddSLingrui98 88609c6f1ddSLingrui98 // ********************************************************************** 887b56f947eSYinan Xu // ***************************** to backend ***************************** 88809c6f1ddSLingrui98 // ********************************************************************** 889b56f947eSYinan Xu // to backend pc mem / target 890b56f947eSYinan Xu io.toBackend.pc_mem_wen := RegNext(last_cycle_bpu_in) 891b56f947eSYinan Xu io.toBackend.pc_mem_waddr := RegNext(last_cycle_bpu_in_idx) 89288bc4f90SLingrui98 io.toBackend.pc_mem_wdata := RegNext(bpu_in_bypass_buf_for_ifu) 89309c6f1ddSLingrui98 89409c6f1ddSLingrui98 // ******************************************************************************* 89509c6f1ddSLingrui98 // **************************** redirect from backend **************************** 89609c6f1ddSLingrui98 // ******************************************************************************* 89709c6f1ddSLingrui98 89809c6f1ddSLingrui98 // redirect read cfiInfo, couples to redirectGen s2 8999342624fSGao-Zeyu val ftq_redirect_rdata = Wire(Vec(BackendRedirectNum, new Ftq_Redirect_SRAMEntry)) 9009342624fSGao-Zeyu val ftb_redirect_rdata = Wire(Vec(BackendRedirectNum, new FTBEntry)) 9019342624fSGao-Zeyu for (i <- 0 until BackendRedirectNum) { 9029342624fSGao-Zeyu ftq_redirect_sram.io.ren(i + 1) := io.fromBackend.ftqIdxAhead(i).valid 9039342624fSGao-Zeyu ftq_redirect_sram.io.raddr(i + 1) := io.fromBackend.ftqIdxAhead(i).bits.value 9049342624fSGao-Zeyu ftb_entry_mem.io.raddr(i + 1) := io.fromBackend.ftqIdxAhead(i).bits.value 90509c6f1ddSLingrui98 9069342624fSGao-Zeyu ftq_redirect_rdata(i) := ftq_redirect_sram.io.rdata(i + 1) 9079342624fSGao-Zeyu ftb_redirect_rdata(i) := ftb_entry_mem.io.rdata(i + 1) 9089342624fSGao-Zeyu } 9099342624fSGao-Zeyu val stage3CfiInfo = Mux1H(io.fromBackend.ftqIdxSelOH.bits, ftq_redirect_rdata) 9109342624fSGao-Zeyu val fromBackendRedirect = WireInit(backendRedirect) 91109c6f1ddSLingrui98 val backendRedirectCfi = fromBackendRedirect.bits.cfiUpdate 91209c6f1ddSLingrui98 backendRedirectCfi.fromFtqRedirectSram(stage3CfiInfo) 91309c6f1ddSLingrui98 914d2b20d1aSTang Haojin 9159342624fSGao-Zeyu val r_ftb_entry = Mux1H(io.fromBackend.ftqIdxSelOH.bits, ftb_redirect_rdata) 91609c6f1ddSLingrui98 val r_ftqOffset = fromBackendRedirect.bits.ftqOffset 91709c6f1ddSLingrui98 918d2b20d1aSTang Haojin backendRedirectCfi.br_hit := r_ftb_entry.brIsSaved(r_ftqOffset) 919d2b20d1aSTang Haojin backendRedirectCfi.jr_hit := r_ftb_entry.isJalr && r_ftb_entry.tailSlot.offset === r_ftqOffset 920*3711cf36S小造xu_zh // FIXME: not portable 921d2b20d1aSTang Haojin backendRedirectCfi.sc_hit := backendRedirectCfi.br_hit && Mux(r_ftb_entry.brSlots(0).offset === r_ftqOffset, 922*3711cf36S小造xu_zh stage3CfiInfo.sc_disagree(0), stage3CfiInfo.sc_disagree(1)) 923d2b20d1aSTang Haojin 92409c6f1ddSLingrui98 when (entry_hit_status(fromBackendRedirect.bits.ftqIdx.value) === h_hit) { 92509c6f1ddSLingrui98 backendRedirectCfi.shift := PopCount(r_ftb_entry.getBrMaskByOffset(r_ftqOffset)) +& 92609c6f1ddSLingrui98 (backendRedirectCfi.pd.isBr && !r_ftb_entry.brIsSaved(r_ftqOffset) && 927eeb5ff92SLingrui98 !r_ftb_entry.newBrCanNotInsert(r_ftqOffset)) 92809c6f1ddSLingrui98 92909c6f1ddSLingrui98 backendRedirectCfi.addIntoHist := backendRedirectCfi.pd.isBr && (r_ftb_entry.brIsSaved(r_ftqOffset) || 930eeb5ff92SLingrui98 !r_ftb_entry.newBrCanNotInsert(r_ftqOffset)) 93109c6f1ddSLingrui98 }.otherwise { 93209c6f1ddSLingrui98 backendRedirectCfi.shift := (backendRedirectCfi.pd.isBr && backendRedirectCfi.taken).asUInt 93309c6f1ddSLingrui98 backendRedirectCfi.addIntoHist := backendRedirectCfi.pd.isBr.asUInt 93409c6f1ddSLingrui98 } 93509c6f1ddSLingrui98 93609c6f1ddSLingrui98 93709c6f1ddSLingrui98 // *************************************************************************** 93809c6f1ddSLingrui98 // **************************** redirect from ifu **************************** 93909c6f1ddSLingrui98 // *************************************************************************** 940d2b20d1aSTang Haojin val fromIfuRedirect = WireInit(0.U.asTypeOf(Valid(new BranchPredictionRedirect))) 94109c6f1ddSLingrui98 fromIfuRedirect.valid := pdWb.valid && pdWb.bits.misOffset.valid && !backendFlush 94209c6f1ddSLingrui98 fromIfuRedirect.bits.ftqIdx := pdWb.bits.ftqIdx 94309c6f1ddSLingrui98 fromIfuRedirect.bits.ftqOffset := pdWb.bits.misOffset.bits 94409c6f1ddSLingrui98 fromIfuRedirect.bits.level := RedirectLevel.flushAfter 945d2b20d1aSTang Haojin fromIfuRedirect.bits.BTBMissBubble := true.B 946d2b20d1aSTang Haojin fromIfuRedirect.bits.debugIsMemVio := false.B 947d2b20d1aSTang Haojin fromIfuRedirect.bits.debugIsCtrl := false.B 94809c6f1ddSLingrui98 94909c6f1ddSLingrui98 val ifuRedirectCfiUpdate = fromIfuRedirect.bits.cfiUpdate 95009c6f1ddSLingrui98 ifuRedirectCfiUpdate.pc := pdWb.bits.pc(pdWb.bits.misOffset.bits) 95109c6f1ddSLingrui98 ifuRedirectCfiUpdate.pd := pdWb.bits.pd(pdWb.bits.misOffset.bits) 95209c6f1ddSLingrui98 ifuRedirectCfiUpdate.predTaken := cfiIndex_vec(pdWb.bits.ftqIdx.value).valid 95309c6f1ddSLingrui98 ifuRedirectCfiUpdate.target := pdWb.bits.target 95409c6f1ddSLingrui98 ifuRedirectCfiUpdate.taken := pdWb.bits.cfiOffset.valid 95509c6f1ddSLingrui98 ifuRedirectCfiUpdate.isMisPred := pdWb.bits.misOffset.valid 95609c6f1ddSLingrui98 957d2b20d1aSTang Haojin val ifuRedirectReg = RegNext(fromIfuRedirect, init=0.U.asTypeOf(Valid(new BranchPredictionRedirect))) 95809c6f1ddSLingrui98 val ifuRedirectToBpu = WireInit(ifuRedirectReg) 95909c6f1ddSLingrui98 ifuFlush := fromIfuRedirect.valid || ifuRedirectToBpu.valid 96009c6f1ddSLingrui98 96109c6f1ddSLingrui98 ftq_redirect_sram.io.ren.head := fromIfuRedirect.valid 96209c6f1ddSLingrui98 ftq_redirect_sram.io.raddr.head := fromIfuRedirect.bits.ftqIdx.value 96309c6f1ddSLingrui98 96409c6f1ddSLingrui98 ftb_entry_mem.io.raddr.head := fromIfuRedirect.bits.ftqIdx.value 96509c6f1ddSLingrui98 96609c6f1ddSLingrui98 val toBpuCfi = ifuRedirectToBpu.bits.cfiUpdate 96709c6f1ddSLingrui98 toBpuCfi.fromFtqRedirectSram(ftq_redirect_sram.io.rdata.head) 968f1267a13SEaston Man when (ifuRedirectReg.bits.cfiUpdate.pd.isRet && ifuRedirectReg.bits.cfiUpdate.pd.valid) { 969c89b4642SGuokai Chen toBpuCfi.target := toBpuCfi.topAddr 97009c6f1ddSLingrui98 } 97109c6f1ddSLingrui98 972d2b20d1aSTang Haojin when (ifuRedirectReg.valid) { 973d2b20d1aSTang Haojin ifuRedirected(ifuRedirectReg.bits.ftqIdx.value) := true.B 974d2b20d1aSTang Haojin } .elsewhen(RegNext(pdWb.valid)) { 975d2b20d1aSTang Haojin // if pdWb and no redirect, set to false 976d2b20d1aSTang Haojin ifuRedirected(last_cycle_bpu_in_ptr.value) := false.B 977d2b20d1aSTang Haojin } 978d2b20d1aSTang Haojin 97909c6f1ddSLingrui98 // ********************************************************************* 98009c6f1ddSLingrui98 // **************************** wb from exu **************************** 98109c6f1ddSLingrui98 // ********************************************************************* 98209c6f1ddSLingrui98 983d2b20d1aSTang Haojin backendRedirect.valid := io.fromBackend.redirect.valid 984d2b20d1aSTang Haojin backendRedirect.bits.connectRedirect(io.fromBackend.redirect.bits) 985d2b20d1aSTang Haojin backendRedirect.bits.BTBMissBubble := false.B 986d2b20d1aSTang Haojin 9872e1be6e1SSteve Gou 98809c6f1ddSLingrui98 def extractRedirectInfo(wb: Valid[Redirect]) = { 9896bf9b30dSLingrui98 val ftqPtr = wb.bits.ftqIdx 99009c6f1ddSLingrui98 val ftqOffset = wb.bits.ftqOffset 99109c6f1ddSLingrui98 val taken = wb.bits.cfiUpdate.taken 99209c6f1ddSLingrui98 val mispred = wb.bits.cfiUpdate.isMisPred 9936bf9b30dSLingrui98 (wb.valid, ftqPtr, ftqOffset, taken, mispred) 99409c6f1ddSLingrui98 } 99509c6f1ddSLingrui98 99609c6f1ddSLingrui98 // fix mispredict entry 99709c6f1ddSLingrui98 val lastIsMispredict = RegNext( 998df5b4b8eSYinan Xu backendRedirect.valid && backendRedirect.bits.level === RedirectLevel.flushAfter, init = false.B 99909c6f1ddSLingrui98 ) 100009c6f1ddSLingrui98 100109c6f1ddSLingrui98 def updateCfiInfo(redirect: Valid[Redirect], isBackend: Boolean = true) = { 10026bf9b30dSLingrui98 val (r_valid, r_ptr, r_offset, r_taken, r_mispred) = extractRedirectInfo(redirect) 10036bf9b30dSLingrui98 val r_idx = r_ptr.value 100409c6f1ddSLingrui98 val cfiIndex_bits_wen = r_valid && r_taken && r_offset < cfiIndex_vec(r_idx).bits 100509c6f1ddSLingrui98 val cfiIndex_valid_wen = r_valid && r_offset === cfiIndex_vec(r_idx).bits 100609c6f1ddSLingrui98 when (cfiIndex_bits_wen || cfiIndex_valid_wen) { 100709c6f1ddSLingrui98 cfiIndex_vec(r_idx).valid := cfiIndex_bits_wen || cfiIndex_valid_wen && r_taken 10083f88c020SGuokai Chen } .elsewhen (r_valid && !r_taken && r_offset =/= cfiIndex_vec(r_idx).bits) { 10093f88c020SGuokai Chen cfiIndex_vec(r_idx).valid :=false.B 101009c6f1ddSLingrui98 } 101109c6f1ddSLingrui98 when (cfiIndex_bits_wen) { 101209c6f1ddSLingrui98 cfiIndex_vec(r_idx).bits := r_offset 101309c6f1ddSLingrui98 } 10146bf9b30dSLingrui98 newest_entry_target := redirect.bits.cfiUpdate.target 1015873dc383SLingrui98 newest_entry_ptr := r_ptr 1016b0ed7239SLingrui98 update_target(r_idx) := redirect.bits.cfiUpdate.target // TODO: remove this 101709c6f1ddSLingrui98 if (isBackend) { 101809c6f1ddSLingrui98 mispredict_vec(r_idx)(r_offset) := r_mispred 101909c6f1ddSLingrui98 } 102009c6f1ddSLingrui98 } 102109c6f1ddSLingrui98 10229342624fSGao-Zeyu when(backendRedirect.valid) { 10239342624fSGao-Zeyu updateCfiInfo(backendRedirect) 102409c6f1ddSLingrui98 }.elsewhen (ifuRedirectToBpu.valid) { 102509c6f1ddSLingrui98 updateCfiInfo(ifuRedirectToBpu, isBackend=false) 102609c6f1ddSLingrui98 } 102709c6f1ddSLingrui98 10289342624fSGao-Zeyu when (backendRedirect.valid) { 10299342624fSGao-Zeyu when (backendRedirect.bits.ControlRedirectBubble) { 1030d2b20d1aSTang Haojin when (fromBackendRedirect.bits.ControlBTBMissBubble) { 1031d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.BTBMissBubble.id) := true.B 1032d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.BTBMissBubble.id) := true.B 1033d2b20d1aSTang Haojin } .elsewhen (fromBackendRedirect.bits.TAGEMissBubble) { 1034d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.TAGEMissBubble.id) := true.B 1035d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.TAGEMissBubble.id) := true.B 1036d2b20d1aSTang Haojin } .elsewhen (fromBackendRedirect.bits.SCMissBubble) { 1037d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.SCMissBubble.id) := true.B 1038d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.SCMissBubble.id) := true.B 1039d2b20d1aSTang Haojin } .elsewhen (fromBackendRedirect.bits.ITTAGEMissBubble) { 1040d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 1041d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 1042d2b20d1aSTang Haojin } .elsewhen (fromBackendRedirect.bits.RASMissBubble) { 1043d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.RASMissBubble.id) := true.B 1044d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.RASMissBubble.id) := true.B 1045d2b20d1aSTang Haojin } 1046d2b20d1aSTang Haojin 1047d2b20d1aSTang Haojin 10489342624fSGao-Zeyu } .elsewhen (backendRedirect.bits.MemVioRedirectBubble) { 1049d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 1050d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 1051d2b20d1aSTang Haojin } .otherwise { 1052d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 1053d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 1054d2b20d1aSTang Haojin } 1055d2b20d1aSTang Haojin } .elsewhen (ifuRedirectReg.valid) { 1056d2b20d1aSTang Haojin topdown_stage.reasons(TopDownCounters.BTBMissBubble.id) := true.B 1057d2b20d1aSTang Haojin io.toIfu.req.bits.topdown_info.reasons(TopDownCounters.BTBMissBubble.id) := true.B 1058d2b20d1aSTang Haojin } 1059d2b20d1aSTang Haojin 1060d2b20d1aSTang Haojin io.ControlBTBMissBubble := fromBackendRedirect.bits.ControlBTBMissBubble 1061d2b20d1aSTang Haojin io.TAGEMissBubble := fromBackendRedirect.bits.TAGEMissBubble 1062d2b20d1aSTang Haojin io.SCMissBubble := fromBackendRedirect.bits.SCMissBubble 1063d2b20d1aSTang Haojin io.ITTAGEMissBubble := fromBackendRedirect.bits.ITTAGEMissBubble 1064d2b20d1aSTang Haojin io.RASMissBubble := fromBackendRedirect.bits.RASMissBubble 1065d2b20d1aSTang Haojin 106609c6f1ddSLingrui98 // *********************************************************************************** 106709c6f1ddSLingrui98 // **************************** flush ptr and state queue **************************** 106809c6f1ddSLingrui98 // *********************************************************************************** 106909c6f1ddSLingrui98 1070df5b4b8eSYinan Xu val redirectVec = VecInit(backendRedirect, fromIfuRedirect) 107109c6f1ddSLingrui98 107209c6f1ddSLingrui98 // when redirect, we should reset ptrs and status queues 107309c6f1ddSLingrui98 when(redirectVec.map(r => r.valid).reduce(_||_)){ 10742f4a3aa4SLingrui98 val r = PriorityMux(redirectVec.map(r => (r.valid -> r.bits))) 107509c6f1ddSLingrui98 val notIfu = redirectVec.dropRight(1).map(r => r.valid).reduce(_||_) 10762f4a3aa4SLingrui98 val (idx, offset, flushItSelf) = (r.ftqIdx, r.ftqOffset, RedirectLevel.flushItself(r.level)) 107709c6f1ddSLingrui98 val next = idx + 1.U 107809c6f1ddSLingrui98 bpuPtr := next 1079dc270d3bSJenius copied_bpu_ptr.map(_ := next) 1080c5c5edaeSJenius ifuPtr_write := next 1081c5c5edaeSJenius ifuWbPtr_write := next 1082c5c5edaeSJenius ifuPtrPlus1_write := idx + 2.U 10836bf9b30dSLingrui98 ifuPtrPlus2_write := idx + 3.U 10843f88c020SGuokai Chen 10853f88c020SGuokai Chen } 10863f88c020SGuokai Chen when(RegNext(redirectVec.map(r => r.valid).reduce(_||_))){ 10873f88c020SGuokai Chen val r = PriorityMux(redirectVec.map(r => (r.valid -> r.bits))) 10883f88c020SGuokai Chen val notIfu = redirectVec.dropRight(1).map(r => r.valid).reduce(_||_) 10893f88c020SGuokai Chen val (idx, offset, flushItSelf) = (r.ftqIdx, r.ftqOffset, RedirectLevel.flushItself(r.level)) 10903f88c020SGuokai Chen when (RegNext(notIfu)) { 10913f88c020SGuokai Chen commitStateQueue(RegNext(idx.value)).zipWithIndex.foreach({ case (s, i) => 10923f88c020SGuokai Chen when(i.U > RegNext(offset) || i.U === RegNext(offset) && RegNext(flushItSelf)){ 1093b5808fc2Ssfencevma s := c_invalid 109409c6f1ddSLingrui98 } 109509c6f1ddSLingrui98 }) 109609c6f1ddSLingrui98 } 109709c6f1ddSLingrui98 } 109809c6f1ddSLingrui98 10993f88c020SGuokai Chen 110009c6f1ddSLingrui98 // only the valid bit is actually needed 1101df5b4b8eSYinan Xu io.toIfu.redirect.bits := backendRedirect.bits 110209c6f1ddSLingrui98 io.toIfu.redirect.valid := stage2Flush 1103d2b20d1aSTang Haojin io.toIfu.topdown_redirect := fromBackendRedirect 110409c6f1ddSLingrui98 110509c6f1ddSLingrui98 // commit 11069aca92b9SYinan Xu for (c <- io.fromBackend.rob_commits) { 110709c6f1ddSLingrui98 when(c.valid) { 110809c6f1ddSLingrui98 commitStateQueue(c.bits.ftqIdx.value)(c.bits.ftqOffset) := c_commited 110988825c5cSYinan Xu // TODO: remove this 111088825c5cSYinan Xu // For instruction fusions, we also update the next instruction 1111c3abb8b6SYinan Xu when (c.bits.commitType === 4.U) { 111288825c5cSYinan Xu commitStateQueue(c.bits.ftqIdx.value)(c.bits.ftqOffset + 1.U) := c_commited 1113c3abb8b6SYinan Xu }.elsewhen(c.bits.commitType === 5.U) { 111488825c5cSYinan Xu commitStateQueue(c.bits.ftqIdx.value)(c.bits.ftqOffset + 2.U) := c_commited 1115c3abb8b6SYinan Xu }.elsewhen(c.bits.commitType === 6.U) { 111688825c5cSYinan Xu val index = (c.bits.ftqIdx + 1.U).value 111788825c5cSYinan Xu commitStateQueue(index)(0) := c_commited 1118c3abb8b6SYinan Xu }.elsewhen(c.bits.commitType === 7.U) { 111988825c5cSYinan Xu val index = (c.bits.ftqIdx + 1.U).value 112088825c5cSYinan Xu commitStateQueue(index)(1) := c_commited 112188825c5cSYinan Xu } 112209c6f1ddSLingrui98 } 112309c6f1ddSLingrui98 } 112409c6f1ddSLingrui98 112509c6f1ddSLingrui98 // **************************************************************** 112609c6f1ddSLingrui98 // **************************** to bpu **************************** 112709c6f1ddSLingrui98 // **************************************************************** 112809c6f1ddSLingrui98 112951981c77SbugGenerator io.toBpu.redirect := Mux(fromBackendRedirect.valid, fromBackendRedirect, ifuRedirectToBpu) 1130209a4cafSSteve Gou val dummy_s1_pred_cycle_vec = VecInit(List.tabulate(FtqSize)(_=>0.U(64.W))) 1131209a4cafSSteve Gou val redirect_latency = GTimer() - pred_s1_cycle.getOrElse(dummy_s1_pred_cycle_vec)(io.toBpu.redirect.bits.ftqIdx.value) + 1.U 1132209a4cafSSteve Gou XSPerfHistogram("backend_redirect_latency", redirect_latency, fromBackendRedirect.valid, 0, 60, 1) 1133209a4cafSSteve Gou XSPerfHistogram("ifu_redirect_latency", redirect_latency, !fromBackendRedirect.valid && ifuRedirectToBpu.valid, 0, 60, 1) 113409c6f1ddSLingrui98 1135f21bbcb2SGuokai Chen XSError(io.toBpu.redirect.valid && isBefore(io.toBpu.redirect.bits.ftqIdx, commPtr), "Ftq received a redirect after its commit, check backend or replay") 1136f21bbcb2SGuokai Chen 113702f21c16SLingrui98 val may_have_stall_from_bpu = Wire(Bool()) 113802f21c16SLingrui98 val bpu_ftb_update_stall = RegInit(0.U(2.W)) // 2-cycle stall, so we need 3 states 113902f21c16SLingrui98 may_have_stall_from_bpu := bpu_ftb_update_stall =/= 0.U 114043aca6c2SGuokai Chen canCommit := commPtr =/= ifuWbPtr && !may_have_stall_from_bpu && 114109c6f1ddSLingrui98 Cat(commitStateQueue(commPtr.value).map(s => { 1142b5808fc2Ssfencevma s === c_invalid || s === c_commited 1143935edac4STang Haojin })).andR 114409c6f1ddSLingrui98 11451d1e6d4dSJenius val mmioReadPtr = io.mmioCommitRead.mmioFtqPtr 11461d1e6d4dSJenius val mmioLastCommit = isBefore(commPtr, mmioReadPtr) && (isAfter(ifuPtr,mmioReadPtr) || mmioReadPtr === ifuPtr) && 1147935edac4STang Haojin Cat(commitStateQueue(mmioReadPtr.value).map(s => { s === c_invalid || s === c_commited})).andR 11481d1e6d4dSJenius io.mmioCommitRead.mmioLastCommit := RegNext(mmioLastCommit) 11491d1e6d4dSJenius 115009c6f1ddSLingrui98 // commit reads 1151c5c5edaeSJenius val commit_pc_bundle = RegNext(ftq_pc_mem.io.commPtr_rdata) 115281101dc4SLingrui98 val commit_target = 115334cf890eSLingrui98 Mux(RegNext(commPtr === newest_entry_ptr), 115434cf890eSLingrui98 RegNext(newest_entry_target), 115581101dc4SLingrui98 RegNext(ftq_pc_mem.io.commPtrPlus1_rdata.startAddr)) 115609c6f1ddSLingrui98 ftq_pd_mem.io.raddr.last := commPtr.value 115709c6f1ddSLingrui98 val commit_pd = ftq_pd_mem.io.rdata.last 115809c6f1ddSLingrui98 ftq_redirect_sram.io.ren.last := canCommit 115909c6f1ddSLingrui98 ftq_redirect_sram.io.raddr.last := commPtr.value 116009c6f1ddSLingrui98 val commit_spec_meta = ftq_redirect_sram.io.rdata.last 116109c6f1ddSLingrui98 ftq_meta_1r_sram.io.ren(0) := canCommit 116209c6f1ddSLingrui98 ftq_meta_1r_sram.io.raddr(0) := commPtr.value 116309c6f1ddSLingrui98 val commit_meta = ftq_meta_1r_sram.io.rdata(0) 116409c6f1ddSLingrui98 ftb_entry_mem.io.raddr.last := commPtr.value 116509c6f1ddSLingrui98 val commit_ftb_entry = ftb_entry_mem.io.rdata.last 116609c6f1ddSLingrui98 116709c6f1ddSLingrui98 // need one cycle to read mem and srams 116809c6f1ddSLingrui98 val do_commit_ptr = RegNext(commPtr) 11695371700eSzoujr val do_commit = RegNext(canCommit, init=false.B) 11706bf9b30dSLingrui98 when (canCommit) { 11716bf9b30dSLingrui98 commPtr_write := commPtrPlus1 11726bf9b30dSLingrui98 commPtrPlus1_write := commPtrPlus1 + 1.U 11736bf9b30dSLingrui98 } 117409c6f1ddSLingrui98 val commit_state = RegNext(commitStateQueue(commPtr.value)) 11755371700eSzoujr val can_commit_cfi = WireInit(cfiIndex_vec(commPtr.value)) 1176d4fcfc3eSGuokai Chen val do_commit_cfi = WireInit(cfiIndex_vec(do_commit_ptr.value)) 11773f88c020SGuokai Chen // 11783f88c020SGuokai Chen //when (commitStateQueue(commPtr.value)(can_commit_cfi.bits) =/= c_commited) { 11793f88c020SGuokai Chen // can_commit_cfi.valid := false.B 11803f88c020SGuokai Chen //} 11815371700eSzoujr val commit_cfi = RegNext(can_commit_cfi) 1182d4fcfc3eSGuokai Chen val debug_cfi = commitStateQueue(do_commit_ptr.value)(do_commit_cfi.bits) =/= c_commited && do_commit_cfi.valid 118309c6f1ddSLingrui98 1184cc2d1573SEaston Man val commit_mispredict : Vec[Bool] = VecInit((RegNext(mispredict_vec(commPtr.value)) zip commit_state).map { 118509c6f1ddSLingrui98 case (mis, state) => mis && state === c_commited 118609c6f1ddSLingrui98 }) 1187cc2d1573SEaston Man val commit_instCommited: Vec[Bool] = VecInit(commit_state.map(_ === c_commited)) // [PredictWidth] 11885371700eSzoujr val can_commit_hit = entry_hit_status(commPtr.value) 11895371700eSzoujr val commit_hit = RegNext(can_commit_hit) 11905fa3df0dSLingrui98 val diff_commit_target = RegNext(update_target(commPtr.value)) // TODO: remove this 1191edc18578SLingrui98 val commit_stage = RegNext(pred_stage(commPtr.value)) 119209c6f1ddSLingrui98 val commit_valid = commit_hit === h_hit || commit_cfi.valid // hit or taken 119309c6f1ddSLingrui98 11945371700eSzoujr val to_bpu_hit = can_commit_hit === h_hit || can_commit_hit === h_false_hit 119502f21c16SLingrui98 switch (bpu_ftb_update_stall) { 119602f21c16SLingrui98 is (0.U) { 119702f21c16SLingrui98 when (can_commit_cfi.valid && !to_bpu_hit && canCommit) { 119802f21c16SLingrui98 bpu_ftb_update_stall := 2.U // 2-cycle stall 119902f21c16SLingrui98 } 120002f21c16SLingrui98 } 120102f21c16SLingrui98 is (2.U) { 120202f21c16SLingrui98 bpu_ftb_update_stall := 1.U 120302f21c16SLingrui98 } 120402f21c16SLingrui98 is (1.U) { 120502f21c16SLingrui98 bpu_ftb_update_stall := 0.U 120602f21c16SLingrui98 } 120702f21c16SLingrui98 is (3.U) { 120802f21c16SLingrui98 XSError(true.B, "bpu_ftb_update_stall should be 0, 1 or 2") 120902f21c16SLingrui98 } 121002f21c16SLingrui98 } 121109c6f1ddSLingrui98 1212b0ed7239SLingrui98 // TODO: remove this 1213b0ed7239SLingrui98 XSError(do_commit && diff_commit_target =/= commit_target, "\ncommit target should be the same as update target\n") 1214b0ed7239SLingrui98 1215b2f6ed0aSSteve Gou // update latency stats 1216b2f6ed0aSSteve Gou val update_latency = GTimer() - pred_s1_cycle.getOrElse(dummy_s1_pred_cycle_vec)(do_commit_ptr.value) + 1.U 1217b2f6ed0aSSteve Gou XSPerfHistogram("bpu_update_latency", update_latency, io.toBpu.update.valid, 0, 64, 2) 1218b2f6ed0aSSteve Gou 121909c6f1ddSLingrui98 io.toBpu.update := DontCare 122009c6f1ddSLingrui98 io.toBpu.update.valid := commit_valid && do_commit 122109c6f1ddSLingrui98 val update = io.toBpu.update.bits 122209c6f1ddSLingrui98 update.false_hit := commit_hit === h_false_hit 122309c6f1ddSLingrui98 update.pc := commit_pc_bundle.startAddr 122409c6f1ddSLingrui98 update.meta := commit_meta.meta 1225803124a6SLingrui98 update.cfi_idx := commit_cfi 12268ffcd86aSLingrui98 update.full_target := commit_target 1227edc18578SLingrui98 update.from_stage := commit_stage 1228c2d1ec7dSLingrui98 update.spec_info := commit_spec_meta 12293f88c020SGuokai Chen XSError(commit_valid && do_commit && debug_cfi, "\ncommit cfi can be non c_commited\n") 123009c6f1ddSLingrui98 123109c6f1ddSLingrui98 val commit_real_hit = commit_hit === h_hit 123209c6f1ddSLingrui98 val update_ftb_entry = update.ftb_entry 123309c6f1ddSLingrui98 123409c6f1ddSLingrui98 val ftbEntryGen = Module(new FTBEntryGen).io 123509c6f1ddSLingrui98 ftbEntryGen.start_addr := commit_pc_bundle.startAddr 123609c6f1ddSLingrui98 ftbEntryGen.old_entry := commit_ftb_entry 123709c6f1ddSLingrui98 ftbEntryGen.pd := commit_pd 123809c6f1ddSLingrui98 ftbEntryGen.cfiIndex := commit_cfi 123909c6f1ddSLingrui98 ftbEntryGen.target := commit_target 124009c6f1ddSLingrui98 ftbEntryGen.hit := commit_real_hit 124109c6f1ddSLingrui98 ftbEntryGen.mispredict_vec := commit_mispredict 124209c6f1ddSLingrui98 124309c6f1ddSLingrui98 update_ftb_entry := ftbEntryGen.new_entry 124409c6f1ddSLingrui98 update.new_br_insert_pos := ftbEntryGen.new_br_insert_pos 124509c6f1ddSLingrui98 update.mispred_mask := ftbEntryGen.mispred_mask 124609c6f1ddSLingrui98 update.old_entry := ftbEntryGen.is_old_entry 1247edc18578SLingrui98 update.pred_hit := commit_hit === h_hit || commit_hit === h_false_hit 1248803124a6SLingrui98 update.br_taken_mask := ftbEntryGen.taken_mask 1249cc2d1573SEaston Man update.br_committed := (ftbEntryGen.new_entry.brValids zip ftbEntryGen.new_entry.brOffset) map { 1250cc2d1573SEaston Man case (valid, offset) => valid && commit_instCommited(offset) 1251cc2d1573SEaston Man } 1252803124a6SLingrui98 update.jmp_taken := ftbEntryGen.jmp_taken 1253b37e4b45SLingrui98 1254803124a6SLingrui98 // update.full_pred.fromFtbEntry(ftbEntryGen.new_entry, update.pc) 1255803124a6SLingrui98 // update.full_pred.jalr_target := commit_target 1256803124a6SLingrui98 // update.full_pred.hit := true.B 1257803124a6SLingrui98 // when (update.full_pred.is_jalr) { 1258803124a6SLingrui98 // update.full_pred.targets.last := commit_target 1259803124a6SLingrui98 // } 126009c6f1ddSLingrui98 1261e30430c2SJay // **************************************************************** 1262e30430c2SJay // *********************** to prefetch **************************** 1263e30430c2SJay // **************************************************************** 1264e30430c2SJay 12659c8f16f2SJenius ftq_pc_mem.io.other_raddrs(0) := DontCare 126658c354d0Sssszwic if(cacheParams.enableICachePrefetch){ 1267e30430c2SJay val prefetchPtr = RegInit(FtqPtr(false.B, 0.U)) 1268378f00d9SJenius val diff_prefetch_addr = WireInit(update_target(prefetchPtr.value)) //TODO: remove this 126934f9624dSguohongyu // TODO : MUST WIDER 1270935edac4STang Haojin prefetchPtr := prefetchPtr + io.toPrefetch.req.fire 1271e30430c2SJay 1272a677d2cbSguohongyu val prefetch_too_late = (isBefore(prefetchPtr, ifuPtr) && !isFull(ifuPtr, prefetchPtr)) || (prefetchPtr === ifuPtr) 1273a677d2cbSguohongyu when(prefetch_too_late){ 1274a677d2cbSguohongyu when(prefetchPtr =/= bpuPtr){ 127534f9624dSguohongyu prefetchPtr := bpuPtr - 1.U 1276a677d2cbSguohongyu }.otherwise{ 1277a677d2cbSguohongyu prefetchPtr := ifuPtr 1278a677d2cbSguohongyu } 1279a677d2cbSguohongyu } 1280a677d2cbSguohongyu 1281378f00d9SJenius ftq_pc_mem.io.other_raddrs(0) := prefetchPtr.value 1282378f00d9SJenius 1283adc0b8dfSGuokai Chen when (bpu_s2_redirect && !isBefore(prefetchPtr, bpu_s2_resp.ftq_idx)) { 1284e30430c2SJay prefetchPtr := bpu_s2_resp.ftq_idx 1285e30430c2SJay } 1286e30430c2SJay 1287adc0b8dfSGuokai Chen when (bpu_s3_redirect && !isBefore(prefetchPtr, bpu_s3_resp.ftq_idx)) { 1288cb4f77ceSLingrui98 prefetchPtr := bpu_s3_resp.ftq_idx 1289a3c55791SJinYue // XSError(true.B, "\ns3_redirect mechanism not implemented!\n") 1290cb4f77ceSLingrui98 } 1291de7689fcSJay 1292f63797a4SLingrui98 1293f63797a4SLingrui98 val prefetch_is_to_send = WireInit(entry_fetch_status(prefetchPtr.value) === f_to_send) 1294f56177cbSJenius val prefetch_addr = Wire(UInt(VAddrBits.W)) 1295f63797a4SLingrui98 1296f63797a4SLingrui98 when (last_cycle_bpu_in && bpu_in_bypass_ptr === prefetchPtr) { 1297f63797a4SLingrui98 prefetch_is_to_send := true.B 12986bf9b30dSLingrui98 prefetch_addr := last_cycle_bpu_target 1299378f00d9SJenius diff_prefetch_addr := last_cycle_bpu_target // TODO: remove this 1300f56177cbSJenius }.otherwise{ 1301f56177cbSJenius prefetch_addr := RegNext( ftq_pc_mem.io.other_rdatas(0).startAddr) 1302f63797a4SLingrui98 } 1303f63797a4SLingrui98 io.toPrefetch.req.valid := prefetchPtr =/= bpuPtr && prefetch_is_to_send 1304f63797a4SLingrui98 io.toPrefetch.req.bits.target := prefetch_addr 1305de7689fcSJay 1306de7689fcSJay when(redirectVec.map(r => r.valid).reduce(_||_)){ 1307de7689fcSJay val r = PriorityMux(redirectVec.map(r => (r.valid -> r.bits))) 1308de7689fcSJay val next = r.ftqIdx + 1.U 1309de7689fcSJay prefetchPtr := next 1310de7689fcSJay } 1311de7689fcSJay 1312378f00d9SJenius // TODO: remove this 131310f8eea3SLingrui98 // XSError(io.toPrefetch.req.valid && diff_prefetch_addr =/= prefetch_addr, 131410f8eea3SLingrui98 // f"\nprefetch_req_target wrong! prefetchPtr: ${prefetchPtr}, prefetch_addr: ${Hexadecimal(prefetch_addr)} diff_prefetch_addr: ${Hexadecimal(diff_prefetch_addr)}\n") 1315378f00d9SJenius 1316378f00d9SJenius 1317a677d2cbSguohongyu XSError(isBefore(bpuPtr, prefetchPtr) && !isFull(bpuPtr, prefetchPtr), "\nprefetchPtr is before bpuPtr!\n") 131826a0efd4Sguohongyu// XSError(isBefore(prefetchPtr, ifuPtr) && !isFull(ifuPtr, prefetchPtr), "\nifuPtr is before prefetchPtr!\n") 1319de7689fcSJay } 1320de7689fcSJay else { 1321de7689fcSJay io.toPrefetch.req <> DontCare 1322de7689fcSJay } 1323de7689fcSJay 132409c6f1ddSLingrui98 // ****************************************************************************** 132509c6f1ddSLingrui98 // **************************** commit perf counters **************************** 132609c6f1ddSLingrui98 // ****************************************************************************** 132709c6f1ddSLingrui98 132809c6f1ddSLingrui98 val commit_inst_mask = VecInit(commit_state.map(c => c === c_commited && do_commit)).asUInt 132909c6f1ddSLingrui98 val commit_mispred_mask = commit_mispredict.asUInt 133009c6f1ddSLingrui98 val commit_not_mispred_mask = ~commit_mispred_mask 133109c6f1ddSLingrui98 133209c6f1ddSLingrui98 val commit_br_mask = commit_pd.brMask.asUInt 133309c6f1ddSLingrui98 val commit_jmp_mask = UIntToOH(commit_pd.jmpOffset) & Fill(PredictWidth, commit_pd.jmpInfo.valid.asTypeOf(UInt(1.W))) 133409c6f1ddSLingrui98 val commit_cfi_mask = (commit_br_mask | commit_jmp_mask) 133509c6f1ddSLingrui98 133609c6f1ddSLingrui98 val mbpInstrs = commit_inst_mask & commit_cfi_mask 133709c6f1ddSLingrui98 133809c6f1ddSLingrui98 val mbpRights = mbpInstrs & commit_not_mispred_mask 133909c6f1ddSLingrui98 val mbpWrongs = mbpInstrs & commit_mispred_mask 134009c6f1ddSLingrui98 134109c6f1ddSLingrui98 io.bpuInfo.bpRight := PopCount(mbpRights) 134209c6f1ddSLingrui98 io.bpuInfo.bpWrong := PopCount(mbpWrongs) 134309c6f1ddSLingrui98 1344da3bf434SMaxpicca-Li val isWriteFTQTable = WireInit(Constantin.createRecord("isWriteFTQTable" + p(XSCoreParamsKey).HartId.toString)) 134551532d8bSGuokai Chen val ftqBranchTraceDB = ChiselDB.createTable("FTQTable" + p(XSCoreParamsKey).HartId.toString, new FtqDebugBundle) 134609c6f1ddSLingrui98 // Cfi Info 134709c6f1ddSLingrui98 for (i <- 0 until PredictWidth) { 134809c6f1ddSLingrui98 val pc = commit_pc_bundle.startAddr + (i * instBytes).U 134909c6f1ddSLingrui98 val v = commit_state(i) === c_commited 135009c6f1ddSLingrui98 val isBr = commit_pd.brMask(i) 135109c6f1ddSLingrui98 val isJmp = commit_pd.jmpInfo.valid && commit_pd.jmpOffset === i.U 135209c6f1ddSLingrui98 val isCfi = isBr || isJmp 135309c6f1ddSLingrui98 val isTaken = commit_cfi.valid && commit_cfi.bits === i.U 135409c6f1ddSLingrui98 val misPred = commit_mispredict(i) 1355c2ad24ebSLingrui98 // val ghist = commit_spec_meta.ghist.predHist 1356c2ad24ebSLingrui98 val histPtr = commit_spec_meta.histPtr 135709c6f1ddSLingrui98 val predCycle = commit_meta.meta(63, 0) 135809c6f1ddSLingrui98 val target = commit_target 135909c6f1ddSLingrui98 136009c6f1ddSLingrui98 val brIdx = OHToUInt(Reverse(Cat(update_ftb_entry.brValids.zip(update_ftb_entry.brOffset).map{case(v, offset) => v && offset === i.U}))) 136109c6f1ddSLingrui98 val inFtbEntry = update_ftb_entry.brValids.zip(update_ftb_entry.brOffset).map{case(v, offset) => v && offset === i.U}.reduce(_||_) 136209c6f1ddSLingrui98 val addIntoHist = ((commit_hit === h_hit) && inFtbEntry) || ((!(commit_hit === h_hit) && i.U === commit_cfi.bits && isBr && commit_cfi.valid)) 136309c6f1ddSLingrui98 XSDebug(v && do_commit && isCfi, p"cfi_update: isBr(${isBr}) pc(${Hexadecimal(pc)}) " + 1364c2ad24ebSLingrui98 p"taken(${isTaken}) mispred(${misPred}) cycle($predCycle) hist(${histPtr.value}) " + 136509c6f1ddSLingrui98 p"startAddr(${Hexadecimal(commit_pc_bundle.startAddr)}) AddIntoHist(${addIntoHist}) " + 136609c6f1ddSLingrui98 p"brInEntry(${inFtbEntry}) brIdx(${brIdx}) target(${Hexadecimal(target)})\n") 136751532d8bSGuokai Chen 136851532d8bSGuokai Chen val logbundle = Wire(new FtqDebugBundle) 136951532d8bSGuokai Chen logbundle.pc := pc 137051532d8bSGuokai Chen logbundle.target := target 137151532d8bSGuokai Chen logbundle.isBr := isBr 137251532d8bSGuokai Chen logbundle.isJmp := isJmp 137351532d8bSGuokai Chen logbundle.isCall := isJmp && commit_pd.hasCall 137451532d8bSGuokai Chen logbundle.isRet := isJmp && commit_pd.hasRet 137551532d8bSGuokai Chen logbundle.misPred := misPred 137651532d8bSGuokai Chen logbundle.isTaken := isTaken 137751532d8bSGuokai Chen logbundle.predStage := commit_stage 137851532d8bSGuokai Chen 137951532d8bSGuokai Chen ftqBranchTraceDB.log( 138051532d8bSGuokai Chen data = logbundle /* hardware of type T */, 1381da3bf434SMaxpicca-Li en = isWriteFTQTable.orR && v && do_commit && isCfi, 138251532d8bSGuokai Chen site = "FTQ" + p(XSCoreParamsKey).HartId.toString, 138351532d8bSGuokai Chen clock = clock, 138451532d8bSGuokai Chen reset = reset 138551532d8bSGuokai Chen ) 138609c6f1ddSLingrui98 } 138709c6f1ddSLingrui98 138809c6f1ddSLingrui98 val enq = io.fromBpu.resp 13892e1be6e1SSteve Gou val perf_redirect = backendRedirect 139009c6f1ddSLingrui98 139109c6f1ddSLingrui98 XSPerfAccumulate("entry", validEntries) 139209c6f1ddSLingrui98 XSPerfAccumulate("bpu_to_ftq_stall", enq.valid && !enq.ready) 139309c6f1ddSLingrui98 XSPerfAccumulate("mispredictRedirect", perf_redirect.valid && RedirectLevel.flushAfter === perf_redirect.bits.level) 139409c6f1ddSLingrui98 XSPerfAccumulate("replayRedirect", perf_redirect.valid && RedirectLevel.flushItself(perf_redirect.bits.level)) 139509c6f1ddSLingrui98 XSPerfAccumulate("predecodeRedirect", fromIfuRedirect.valid) 139609c6f1ddSLingrui98 139709c6f1ddSLingrui98 XSPerfAccumulate("to_ifu_bubble", io.toIfu.req.ready && !io.toIfu.req.valid) 139809c6f1ddSLingrui98 139909c6f1ddSLingrui98 XSPerfAccumulate("to_ifu_stall", io.toIfu.req.valid && !io.toIfu.req.ready) 140009c6f1ddSLingrui98 XSPerfAccumulate("from_bpu_real_bubble", !enq.valid && enq.ready && allowBpuIn) 140112cedb6fSLingrui98 XSPerfAccumulate("bpu_to_ifu_bubble", bpuPtr === ifuPtr) 1402b2f6ed0aSSteve Gou XSPerfAccumulate("bpu_to_ifu_bubble_when_ftq_full", (bpuPtr === ifuPtr) && isFull(bpuPtr, commPtr) && io.toIfu.req.ready) 140309c6f1ddSLingrui98 14049342624fSGao-Zeyu XSPerfAccumulate("redirectAhead_ValidNum", io.fromBackend.ftqIdxAhead.map(_.valid).reduce(_|_)) 14059342624fSGao-Zeyu XSPerfAccumulate("fromBackendRedirect_ValidNum", io.fromBackend.redirect.valid) 14069342624fSGao-Zeyu XSPerfAccumulate("toBpuRedirect_ValidNum", io.toBpu.redirect.valid) 14079342624fSGao-Zeyu 140809c6f1ddSLingrui98 val from_bpu = io.fromBpu.resp.bits 140909c6f1ddSLingrui98 val to_ifu = io.toIfu.req.bits 141009c6f1ddSLingrui98 141109c6f1ddSLingrui98 1412209a4cafSSteve Gou XSPerfHistogram("commit_num_inst", PopCount(commit_inst_mask), do_commit, 0, PredictWidth+1, 1) 141309c6f1ddSLingrui98 141409c6f1ddSLingrui98 141509c6f1ddSLingrui98 141609c6f1ddSLingrui98 141709c6f1ddSLingrui98 val commit_jal_mask = UIntToOH(commit_pd.jmpOffset) & Fill(PredictWidth, commit_pd.hasJal.asTypeOf(UInt(1.W))) 141809c6f1ddSLingrui98 val commit_jalr_mask = UIntToOH(commit_pd.jmpOffset) & Fill(PredictWidth, commit_pd.hasJalr.asTypeOf(UInt(1.W))) 141909c6f1ddSLingrui98 val commit_call_mask = UIntToOH(commit_pd.jmpOffset) & Fill(PredictWidth, commit_pd.hasCall.asTypeOf(UInt(1.W))) 142009c6f1ddSLingrui98 val commit_ret_mask = UIntToOH(commit_pd.jmpOffset) & Fill(PredictWidth, commit_pd.hasRet.asTypeOf(UInt(1.W))) 142109c6f1ddSLingrui98 142209c6f1ddSLingrui98 142309c6f1ddSLingrui98 val mbpBRights = mbpRights & commit_br_mask 142409c6f1ddSLingrui98 val mbpJRights = mbpRights & commit_jal_mask 142509c6f1ddSLingrui98 val mbpIRights = mbpRights & commit_jalr_mask 142609c6f1ddSLingrui98 val mbpCRights = mbpRights & commit_call_mask 142709c6f1ddSLingrui98 val mbpRRights = mbpRights & commit_ret_mask 142809c6f1ddSLingrui98 142909c6f1ddSLingrui98 val mbpBWrongs = mbpWrongs & commit_br_mask 143009c6f1ddSLingrui98 val mbpJWrongs = mbpWrongs & commit_jal_mask 143109c6f1ddSLingrui98 val mbpIWrongs = mbpWrongs & commit_jalr_mask 143209c6f1ddSLingrui98 val mbpCWrongs = mbpWrongs & commit_call_mask 143309c6f1ddSLingrui98 val mbpRWrongs = mbpWrongs & commit_ret_mask 143409c6f1ddSLingrui98 14351d7e5011SLingrui98 val commit_pred_stage = RegNext(pred_stage(commPtr.value)) 14361d7e5011SLingrui98 14371d7e5011SLingrui98 def pred_stage_map(src: UInt, name: String) = { 14381d7e5011SLingrui98 (0 until numBpStages).map(i => 14391d7e5011SLingrui98 f"${name}_stage_${i+1}" -> PopCount(src.asBools.map(_ && commit_pred_stage === BP_STAGES(i))) 14401d7e5011SLingrui98 ).foldLeft(Map[String, UInt]())(_+_) 14411d7e5011SLingrui98 } 14421d7e5011SLingrui98 14431d7e5011SLingrui98 val mispred_stage_map = pred_stage_map(mbpWrongs, "mispredict") 14441d7e5011SLingrui98 val br_mispred_stage_map = pred_stage_map(mbpBWrongs, "br_mispredict") 14451d7e5011SLingrui98 val jalr_mispred_stage_map = pred_stage_map(mbpIWrongs, "jalr_mispredict") 14461d7e5011SLingrui98 val correct_stage_map = pred_stage_map(mbpRights, "correct") 14471d7e5011SLingrui98 val br_correct_stage_map = pred_stage_map(mbpBRights, "br_correct") 14481d7e5011SLingrui98 val jalr_correct_stage_map = pred_stage_map(mbpIRights, "jalr_correct") 14491d7e5011SLingrui98 145009c6f1ddSLingrui98 val update_valid = io.toBpu.update.valid 145109c6f1ddSLingrui98 def u(cond: Bool) = update_valid && cond 145209c6f1ddSLingrui98 val ftb_false_hit = u(update.false_hit) 145365fddcf0Szoujr // assert(!ftb_false_hit) 145409c6f1ddSLingrui98 val ftb_hit = u(commit_hit === h_hit) 145509c6f1ddSLingrui98 145609c6f1ddSLingrui98 val ftb_new_entry = u(ftbEntryGen.is_init_entry) 1457b37e4b45SLingrui98 val ftb_new_entry_only_br = ftb_new_entry && !update_ftb_entry.jmpValid 1458b37e4b45SLingrui98 val ftb_new_entry_only_jmp = ftb_new_entry && !update_ftb_entry.brValids(0) 1459b37e4b45SLingrui98 val ftb_new_entry_has_br_and_jmp = ftb_new_entry && update_ftb_entry.brValids(0) && update_ftb_entry.jmpValid 146009c6f1ddSLingrui98 146109c6f1ddSLingrui98 val ftb_old_entry = u(ftbEntryGen.is_old_entry) 146209c6f1ddSLingrui98 146309c6f1ddSLingrui98 val ftb_modified_entry = u(ftbEntryGen.is_new_br || ftbEntryGen.is_jalr_target_modified || ftbEntryGen.is_always_taken_modified) 146409c6f1ddSLingrui98 val ftb_modified_entry_new_br = u(ftbEntryGen.is_new_br) 1465d2b20d1aSTang Haojin val ftb_modified_entry_ifu_redirected = u(ifuRedirected(do_commit_ptr.value)) 146609c6f1ddSLingrui98 val ftb_modified_entry_jalr_target_modified = u(ftbEntryGen.is_jalr_target_modified) 146709c6f1ddSLingrui98 val ftb_modified_entry_br_full = ftb_modified_entry && ftbEntryGen.is_br_full 146809c6f1ddSLingrui98 val ftb_modified_entry_always_taken = ftb_modified_entry && ftbEntryGen.is_always_taken_modified 146909c6f1ddSLingrui98 1470209a4cafSSteve Gou def getFtbEntryLen(pc: UInt, entry: FTBEntry) = (entry.getFallThrough(pc) - pc) >> instOffsetBits 1471209a4cafSSteve Gou val gen_ftb_entry_len = getFtbEntryLen(update.pc, ftbEntryGen.new_entry) 1472209a4cafSSteve Gou XSPerfHistogram("ftb_init_entry_len", gen_ftb_entry_len, ftb_new_entry, 0, PredictWidth+1, 1) 1473209a4cafSSteve Gou XSPerfHistogram("ftb_modified_entry_len", gen_ftb_entry_len, ftb_modified_entry, 0, PredictWidth+1, 1) 1474209a4cafSSteve Gou val s3_ftb_entry_len = getFtbEntryLen(from_bpu.s3.pc(0), from_bpu.last_stage_ftb_entry) 1475209a4cafSSteve Gou XSPerfHistogram("s3_ftb_entry_len", s3_ftb_entry_len, from_bpu.s3.valid(0), 0, PredictWidth+1, 1) 147609c6f1ddSLingrui98 1477209a4cafSSteve Gou XSPerfHistogram("ftq_has_entry", validEntries, true.B, 0, FtqSize+1, 1) 147809c6f1ddSLingrui98 147909c6f1ddSLingrui98 val perfCountsMap = Map( 148009c6f1ddSLingrui98 "BpInstr" -> PopCount(mbpInstrs), 148109c6f1ddSLingrui98 "BpBInstr" -> PopCount(mbpBRights | mbpBWrongs), 148209c6f1ddSLingrui98 "BpRight" -> PopCount(mbpRights), 148309c6f1ddSLingrui98 "BpWrong" -> PopCount(mbpWrongs), 148409c6f1ddSLingrui98 "BpBRight" -> PopCount(mbpBRights), 148509c6f1ddSLingrui98 "BpBWrong" -> PopCount(mbpBWrongs), 148609c6f1ddSLingrui98 "BpJRight" -> PopCount(mbpJRights), 148709c6f1ddSLingrui98 "BpJWrong" -> PopCount(mbpJWrongs), 148809c6f1ddSLingrui98 "BpIRight" -> PopCount(mbpIRights), 148909c6f1ddSLingrui98 "BpIWrong" -> PopCount(mbpIWrongs), 149009c6f1ddSLingrui98 "BpCRight" -> PopCount(mbpCRights), 149109c6f1ddSLingrui98 "BpCWrong" -> PopCount(mbpCWrongs), 149209c6f1ddSLingrui98 "BpRRight" -> PopCount(mbpRRights), 149309c6f1ddSLingrui98 "BpRWrong" -> PopCount(mbpRWrongs), 149409c6f1ddSLingrui98 149509c6f1ddSLingrui98 "ftb_false_hit" -> PopCount(ftb_false_hit), 149609c6f1ddSLingrui98 "ftb_hit" -> PopCount(ftb_hit), 149709c6f1ddSLingrui98 "ftb_new_entry" -> PopCount(ftb_new_entry), 149809c6f1ddSLingrui98 "ftb_new_entry_only_br" -> PopCount(ftb_new_entry_only_br), 149909c6f1ddSLingrui98 "ftb_new_entry_only_jmp" -> PopCount(ftb_new_entry_only_jmp), 150009c6f1ddSLingrui98 "ftb_new_entry_has_br_and_jmp" -> PopCount(ftb_new_entry_has_br_and_jmp), 150109c6f1ddSLingrui98 "ftb_old_entry" -> PopCount(ftb_old_entry), 150209c6f1ddSLingrui98 "ftb_modified_entry" -> PopCount(ftb_modified_entry), 150309c6f1ddSLingrui98 "ftb_modified_entry_new_br" -> PopCount(ftb_modified_entry_new_br), 150409c6f1ddSLingrui98 "ftb_jalr_target_modified" -> PopCount(ftb_modified_entry_jalr_target_modified), 150509c6f1ddSLingrui98 "ftb_modified_entry_br_full" -> PopCount(ftb_modified_entry_br_full), 150609c6f1ddSLingrui98 "ftb_modified_entry_always_taken" -> PopCount(ftb_modified_entry_always_taken) 1507209a4cafSSteve Gou ) ++ mispred_stage_map ++ br_mispred_stage_map ++ jalr_mispred_stage_map ++ 15081d7e5011SLingrui98 correct_stage_map ++ br_correct_stage_map ++ jalr_correct_stage_map 150909c6f1ddSLingrui98 151009c6f1ddSLingrui98 for((key, value) <- perfCountsMap) { 151109c6f1ddSLingrui98 XSPerfAccumulate(key, value) 151209c6f1ddSLingrui98 } 151309c6f1ddSLingrui98 151409c6f1ddSLingrui98 // --------------------------- Debug -------------------------------- 151509c6f1ddSLingrui98 // XSDebug(enq_fire, p"enq! " + io.fromBpu.resp.bits.toPrintable) 151609c6f1ddSLingrui98 XSDebug(io.toIfu.req.fire, p"fire to ifu " + io.toIfu.req.bits.toPrintable) 151709c6f1ddSLingrui98 XSDebug(do_commit, p"deq! [ptr] $do_commit_ptr\n") 151809c6f1ddSLingrui98 XSDebug(true.B, p"[bpuPtr] $bpuPtr, [ifuPtr] $ifuPtr, [ifuWbPtr] $ifuWbPtr [commPtr] $commPtr\n") 151909c6f1ddSLingrui98 XSDebug(true.B, p"[in] v:${io.fromBpu.resp.valid} r:${io.fromBpu.resp.ready} " + 152009c6f1ddSLingrui98 p"[out] v:${io.toIfu.req.valid} r:${io.toIfu.req.ready}\n") 152109c6f1ddSLingrui98 XSDebug(do_commit, p"[deq info] cfiIndex: $commit_cfi, $commit_pc_bundle, target: ${Hexadecimal(commit_target)}\n") 152209c6f1ddSLingrui98 152309c6f1ddSLingrui98 // def ubtbCheck(commit: FtqEntry, predAns: Seq[PredictorAnswer], isWrong: Bool) = { 152409c6f1ddSLingrui98 // commit.valids.zip(commit.pd).zip(predAns).zip(commit.takens).map { 152509c6f1ddSLingrui98 // case (((valid, pd), ans), taken) => 152609c6f1ddSLingrui98 // Mux(valid && pd.isBr, 152709c6f1ddSLingrui98 // isWrong ^ Mux(ans.hit.asBool, 152809c6f1ddSLingrui98 // Mux(ans.taken.asBool, taken && ans.target === commitEntry.target, 152909c6f1ddSLingrui98 // !taken), 153009c6f1ddSLingrui98 // !taken), 153109c6f1ddSLingrui98 // false.B) 153209c6f1ddSLingrui98 // } 153309c6f1ddSLingrui98 // } 153409c6f1ddSLingrui98 153509c6f1ddSLingrui98 // def btbCheck(commit: FtqEntry, predAns: Seq[PredictorAnswer], isWrong: Bool) = { 153609c6f1ddSLingrui98 // commit.valids.zip(commit.pd).zip(predAns).zip(commit.takens).map { 153709c6f1ddSLingrui98 // case (((valid, pd), ans), taken) => 153809c6f1ddSLingrui98 // Mux(valid && pd.isBr, 153909c6f1ddSLingrui98 // isWrong ^ Mux(ans.hit.asBool, 154009c6f1ddSLingrui98 // Mux(ans.taken.asBool, taken && ans.target === commitEntry.target, 154109c6f1ddSLingrui98 // !taken), 154209c6f1ddSLingrui98 // !taken), 154309c6f1ddSLingrui98 // false.B) 154409c6f1ddSLingrui98 // } 154509c6f1ddSLingrui98 // } 154609c6f1ddSLingrui98 154709c6f1ddSLingrui98 // def tageCheck(commit: FtqEntry, predAns: Seq[PredictorAnswer], isWrong: Bool) = { 154809c6f1ddSLingrui98 // commit.valids.zip(commit.pd).zip(predAns).zip(commit.takens).map { 154909c6f1ddSLingrui98 // case (((valid, pd), ans), taken) => 155009c6f1ddSLingrui98 // Mux(valid && pd.isBr, 155109c6f1ddSLingrui98 // isWrong ^ (ans.taken.asBool === taken), 155209c6f1ddSLingrui98 // false.B) 155309c6f1ddSLingrui98 // } 155409c6f1ddSLingrui98 // } 155509c6f1ddSLingrui98 155609c6f1ddSLingrui98 // def loopCheck(commit: FtqEntry, predAns: Seq[PredictorAnswer], isWrong: Bool) = { 155709c6f1ddSLingrui98 // commit.valids.zip(commit.pd).zip(predAns).zip(commit.takens).map { 155809c6f1ddSLingrui98 // case (((valid, pd), ans), taken) => 155909c6f1ddSLingrui98 // Mux(valid && (pd.isBr) && ans.hit.asBool, 156009c6f1ddSLingrui98 // isWrong ^ (!taken), 156109c6f1ddSLingrui98 // false.B) 156209c6f1ddSLingrui98 // } 156309c6f1ddSLingrui98 // } 156409c6f1ddSLingrui98 156509c6f1ddSLingrui98 // def rasCheck(commit: FtqEntry, predAns: Seq[PredictorAnswer], isWrong: Bool) = { 156609c6f1ddSLingrui98 // commit.valids.zip(commit.pd).zip(predAns).zip(commit.takens).map { 156709c6f1ddSLingrui98 // case (((valid, pd), ans), taken) => 156809c6f1ddSLingrui98 // Mux(valid && pd.isRet.asBool /*&& taken*/ && ans.hit.asBool, 156909c6f1ddSLingrui98 // isWrong ^ (ans.target === commitEntry.target), 157009c6f1ddSLingrui98 // false.B) 157109c6f1ddSLingrui98 // } 157209c6f1ddSLingrui98 // } 157309c6f1ddSLingrui98 157409c6f1ddSLingrui98 // val ubtbRights = ubtbCheck(commitEntry, commitEntry.metas.map(_.ubtbAns), false.B) 157509c6f1ddSLingrui98 // val ubtbWrongs = ubtbCheck(commitEntry, commitEntry.metas.map(_.ubtbAns), true.B) 157609c6f1ddSLingrui98 // // btb and ubtb pred jal and jalr as well 157709c6f1ddSLingrui98 // val btbRights = btbCheck(commitEntry, commitEntry.metas.map(_.btbAns), false.B) 157809c6f1ddSLingrui98 // val btbWrongs = btbCheck(commitEntry, commitEntry.metas.map(_.btbAns), true.B) 157909c6f1ddSLingrui98 // val tageRights = tageCheck(commitEntry, commitEntry.metas.map(_.tageAns), false.B) 158009c6f1ddSLingrui98 // val tageWrongs = tageCheck(commitEntry, commitEntry.metas.map(_.tageAns), true.B) 158109c6f1ddSLingrui98 158209c6f1ddSLingrui98 // val loopRights = loopCheck(commitEntry, commitEntry.metas.map(_.loopAns), false.B) 158309c6f1ddSLingrui98 // val loopWrongs = loopCheck(commitEntry, commitEntry.metas.map(_.loopAns), true.B) 158409c6f1ddSLingrui98 158509c6f1ddSLingrui98 // val rasRights = rasCheck(commitEntry, commitEntry.metas.map(_.rasAns), false.B) 158609c6f1ddSLingrui98 // val rasWrongs = rasCheck(commitEntry, commitEntry.metas.map(_.rasAns), true.B) 15871ca0e4f3SYinan Xu 1588cd365d4cSrvcoresjw val perfEvents = Seq( 1589cd365d4cSrvcoresjw ("bpu_s2_redirect ", bpu_s2_redirect ), 1590cb4f77ceSLingrui98 ("bpu_s3_redirect ", bpu_s3_redirect ), 1591cd365d4cSrvcoresjw ("bpu_to_ftq_stall ", enq.valid && ~enq.ready ), 1592cd365d4cSrvcoresjw ("mispredictRedirect ", perf_redirect.valid && RedirectLevel.flushAfter === perf_redirect.bits.level), 1593cd365d4cSrvcoresjw ("replayRedirect ", perf_redirect.valid && RedirectLevel.flushItself(perf_redirect.bits.level) ), 1594cd365d4cSrvcoresjw ("predecodeRedirect ", fromIfuRedirect.valid ), 1595cd365d4cSrvcoresjw ("to_ifu_bubble ", io.toIfu.req.ready && !io.toIfu.req.valid ), 1596cd365d4cSrvcoresjw ("from_bpu_real_bubble ", !enq.valid && enq.ready && allowBpuIn ), 1597cd365d4cSrvcoresjw ("BpInstr ", PopCount(mbpInstrs) ), 1598cd365d4cSrvcoresjw ("BpBInstr ", PopCount(mbpBRights | mbpBWrongs) ), 1599cd365d4cSrvcoresjw ("BpRight ", PopCount(mbpRights) ), 1600cd365d4cSrvcoresjw ("BpWrong ", PopCount(mbpWrongs) ), 1601cd365d4cSrvcoresjw ("BpBRight ", PopCount(mbpBRights) ), 1602cd365d4cSrvcoresjw ("BpBWrong ", PopCount(mbpBWrongs) ), 1603cd365d4cSrvcoresjw ("BpJRight ", PopCount(mbpJRights) ), 1604cd365d4cSrvcoresjw ("BpJWrong ", PopCount(mbpJWrongs) ), 1605cd365d4cSrvcoresjw ("BpIRight ", PopCount(mbpIRights) ), 1606cd365d4cSrvcoresjw ("BpIWrong ", PopCount(mbpIWrongs) ), 1607cd365d4cSrvcoresjw ("BpCRight ", PopCount(mbpCRights) ), 1608cd365d4cSrvcoresjw ("BpCWrong ", PopCount(mbpCWrongs) ), 1609cd365d4cSrvcoresjw ("BpRRight ", PopCount(mbpRRights) ), 1610cd365d4cSrvcoresjw ("BpRWrong ", PopCount(mbpRWrongs) ), 1611cd365d4cSrvcoresjw ("ftb_false_hit ", PopCount(ftb_false_hit) ), 1612cd365d4cSrvcoresjw ("ftb_hit ", PopCount(ftb_hit) ), 1613cd365d4cSrvcoresjw ) 16141ca0e4f3SYinan Xu generatePerfEvent() 161509c6f1ddSLingrui98}