109c6f1ddSLingrui98/*************************************************************************************** 2e3da8badSTang Haojin* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC) 3e3da8badSTang Haojin* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences 409c6f1ddSLingrui98* Copyright (c) 2020-2021 Peng Cheng Laboratory 509c6f1ddSLingrui98* 609c6f1ddSLingrui98* XiangShan is licensed under Mulan PSL v2. 709c6f1ddSLingrui98* You can use this software according to the terms and conditions of the Mulan PSL v2. 809c6f1ddSLingrui98* You may obtain a copy of Mulan PSL v2 at: 909c6f1ddSLingrui98* http://license.coscl.org.cn/MulanPSL2 1009c6f1ddSLingrui98* 1109c6f1ddSLingrui98* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 1209c6f1ddSLingrui98* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 1309c6f1ddSLingrui98* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 1409c6f1ddSLingrui98* 1509c6f1ddSLingrui98* See the Mulan PSL v2 for more details. 1609c6f1ddSLingrui98***************************************************************************************/ 1709c6f1ddSLingrui98 1809c6f1ddSLingrui98package xiangshan.frontend 1909c6f1ddSLingrui98 208891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters 2109c6f1ddSLingrui98import chisel3._ 2209c6f1ddSLingrui98import chisel3.util._ 232a3050c2SJayimport freechips.rocketchip.rocket.RVCDecoder 2409c6f1ddSLingrui98import xiangshan._ 2509c6f1ddSLingrui98import xiangshan.cache.mmu._ 261d8f4dcbSJayimport xiangshan.frontend.icache._ 2709c6f1ddSLingrui98import utils._ 283c02ee8fSwakafaimport utility._ 29b6982e83SLemoverimport xiangshan.backend.fu.{PMPReqBundle, PMPRespBundle} 303c02ee8fSwakafaimport utility.ChiselDB 3109c6f1ddSLingrui98 3209c6f1ddSLingrui98trait HasInstrMMIOConst extends HasXSParameter with HasIFUConst{ 3309c6f1ddSLingrui98 def mmioBusWidth = 64 3409c6f1ddSLingrui98 def mmioBusBytes = mmioBusWidth / 8 350be662e4SJay def maxInstrLen = 32 3609c6f1ddSLingrui98} 3709c6f1ddSLingrui98 3809c6f1ddSLingrui98trait HasIFUConst extends HasXSParameter{ 391d8f4dcbSJay def addrAlign(addr: UInt, bytes: Int, highest: Int): UInt = Cat(addr(highest-1, log2Ceil(bytes)), 0.U(log2Ceil(bytes).W)) 401d8f4dcbSJay def fetchQueueSize = 2 411d8f4dcbSJay 422a3050c2SJay def getBasicBlockIdx( pc: UInt, start: UInt ): UInt = { 432a3050c2SJay val byteOffset = pc - start 442a3050c2SJay (byteOffset - instBytes.U)(log2Ceil(PredictWidth),instOffsetBits) 451d8f4dcbSJay } 4609c6f1ddSLingrui98} 4709c6f1ddSLingrui98 4809c6f1ddSLingrui98class IfuToFtqIO(implicit p:Parameters) extends XSBundle { 4909c6f1ddSLingrui98 val pdWb = Valid(new PredecodeWritebackBundle) 5009c6f1ddSLingrui98} 5109c6f1ddSLingrui98 52d7ac23a3SEaston Manclass IfuToBackendIO(implicit p:Parameters) extends XSBundle { 53d7ac23a3SEaston Man // write to backend gpaddr mem 54d7ac23a3SEaston Man val gpaddrMem_wen = Output(Bool()) 55d7ac23a3SEaston Man val gpaddrMem_waddr = Output(UInt(log2Ceil(FtqSize).W)) // Ftq Ptr 56d7ac23a3SEaston Man // 2 gpaddrs, correspond to startAddr & nextLineAddr in bundle FtqICacheInfo 57d7ac23a3SEaston Man // TODO: avoid cross page entry in Ftq 58bad60841SXiaokun-Pei val gpaddrMem_wdata = Output(UInt(GPAddrBits.W)) 59d7ac23a3SEaston Man} 60d7ac23a3SEaston Man 6109c6f1ddSLingrui98class FtqInterface(implicit p: Parameters) extends XSBundle { 6209c6f1ddSLingrui98 val fromFtq = Flipped(new FtqToIfuIO) 6309c6f1ddSLingrui98 val toFtq = new IfuToFtqIO 6409c6f1ddSLingrui98} 6509c6f1ddSLingrui98 660be662e4SJayclass UncacheInterface(implicit p: Parameters) extends XSBundle { 670be662e4SJay val fromUncache = Flipped(DecoupledIO(new InsUncacheResp)) 680be662e4SJay val toUncache = DecoupledIO( new InsUncacheReq ) 690be662e4SJay} 701d1e6d4dSJenius 7109c6f1ddSLingrui98class NewIFUIO(implicit p: Parameters) extends XSBundle { 7209c6f1ddSLingrui98 val ftqInter = new FtqInterface 7350780602SJenius val icacheInter = Flipped(new IFUICacheIO) 741d8f4dcbSJay val icacheStop = Output(Bool()) 751d8f4dcbSJay val icachePerfInfo = Input(new ICachePerfInfo) 7609c6f1ddSLingrui98 val toIbuffer = Decoupled(new FetchToIBuffer) 77d7ac23a3SEaston Man val toBackend = new IfuToBackendIO 780be662e4SJay val uncacheInter = new UncacheInterface 7972951335SLi Qianruo val frontendTrigger = Flipped(new FrontendTdataDistributeIO) 80a37fbf10SJay val rob_commits = Flipped(Vec(CommitWidth, Valid(new RobCommitInfo))) 81f1fe8698SLemover val iTLBInter = new TlbRequestIO 8256788a33SJinYue val pmp = new ICachePMPBundle 831d1e6d4dSJenius val mmioCommitRead = new mmioCommitRead 8409c6f1ddSLingrui98} 8509c6f1ddSLingrui98 8609c6f1ddSLingrui98// record the situation in which fallThruAddr falls into 8709c6f1ddSLingrui98// the middle of an RVI inst 8809c6f1ddSLingrui98class LastHalfInfo(implicit p: Parameters) extends XSBundle { 8909c6f1ddSLingrui98 val valid = Bool() 9009c6f1ddSLingrui98 val middlePC = UInt(VAddrBits.W) 9109c6f1ddSLingrui98 def matchThisBlock(startAddr: UInt) = valid && middlePC === startAddr 9209c6f1ddSLingrui98} 9309c6f1ddSLingrui98 9409c6f1ddSLingrui98class IfuToPreDecode(implicit p: Parameters) extends XSBundle { 9509c6f1ddSLingrui98 val data = if(HasCExtension) Vec(PredictWidth + 1, UInt(16.W)) else Vec(PredictWidth, UInt(32.W)) 9672951335SLi Qianruo val frontendTrigger = new FrontendTdataDistributeIO 972a3050c2SJay val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 9809c6f1ddSLingrui98} 9909c6f1ddSLingrui98 1002a3050c2SJay 1012a3050c2SJayclass IfuToPredChecker(implicit p: Parameters) extends XSBundle { 1022a3050c2SJay val ftqOffset = Valid(UInt(log2Ceil(PredictWidth).W)) 1032a3050c2SJay val jumpOffset = Vec(PredictWidth, UInt(XLEN.W)) 1042a3050c2SJay val target = UInt(VAddrBits.W) 1052a3050c2SJay val instrRange = Vec(PredictWidth, Bool()) 1062a3050c2SJay val instrValid = Vec(PredictWidth, Bool()) 1072a3050c2SJay val pds = Vec(PredictWidth, new PreDecodeInfo) 1082a3050c2SJay val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 1090c70648eSEaston Man val fire_in = Bool() 1102a3050c2SJay} 1112a3050c2SJay 11251532d8bSGuokai Chenclass FetchToIBufferDB extends Bundle { 11351532d8bSGuokai Chen val start_addr = UInt(39.W) 11451532d8bSGuokai Chen val instr_count = UInt(32.W) 11551532d8bSGuokai Chen val exception = Bool() 11651532d8bSGuokai Chen val is_cache_hit = Bool() 11751532d8bSGuokai Chen} 11851532d8bSGuokai Chen 11951532d8bSGuokai Chenclass IfuWbToFtqDB extends Bundle { 12051532d8bSGuokai Chen val start_addr = UInt(39.W) 12151532d8bSGuokai Chen val is_miss_pred = Bool() 12251532d8bSGuokai Chen val miss_pred_offset = UInt(32.W) 12351532d8bSGuokai Chen val checkJalFault = Bool() 12451532d8bSGuokai Chen val checkRetFault = Bool() 12551532d8bSGuokai Chen val checkTargetFault = Bool() 12651532d8bSGuokai Chen val checkNotCFIFault = Bool() 12751532d8bSGuokai Chen val checkInvalidTaken = Bool() 12851532d8bSGuokai Chen} 12951532d8bSGuokai Chen 1302a3050c2SJayclass NewIFU(implicit p: Parameters) extends XSModule 1312a3050c2SJay with HasICacheParameters 1322a3050c2SJay with HasIFUConst 1332a3050c2SJay with HasPdConst 134167bcd01SJay with HasCircularQueuePtrHelper 1352a3050c2SJay with HasPerfEvents 13621ae6bc4Speixiaokun with HasTlbConst 13709c6f1ddSLingrui98{ 13809c6f1ddSLingrui98 val io = IO(new NewIFUIO) 13909c6f1ddSLingrui98 val (toFtq, fromFtq) = (io.ftqInter.toFtq, io.ftqInter.fromFtq) 140c5c5edaeSJenius val fromICache = io.icacheInter.resp 1410be662e4SJay val (toUncache, fromUncache) = (io.uncacheInter.toUncache , io.uncacheInter.fromUncache) 14209c6f1ddSLingrui98 14309c6f1ddSLingrui98 def isCrossLineReq(start: UInt, end: UInt): Bool = start(blockOffBits) ^ end(blockOffBits) 14409c6f1ddSLingrui98 145d2b20d1aSTang Haojin def numOfStage = 3 146e4d2f6a9Smy-mayfly // equal lower_result overflow bit 147e4d2f6a9Smy-mayfly def PcCutPoint = (VAddrBits/4) - 1 148e4d2f6a9Smy-mayfly def CatPC(low: UInt, high: UInt, high1: UInt): UInt = { 149e4d2f6a9Smy-mayfly Mux( 150e4d2f6a9Smy-mayfly low(PcCutPoint), 151e4d2f6a9Smy-mayfly Cat(high1, low(PcCutPoint-1, 0)), 152e4d2f6a9Smy-mayfly Cat(high, low(PcCutPoint-1, 0)) 153e4d2f6a9Smy-mayfly ) 154e4d2f6a9Smy-mayfly } 155e4d2f6a9Smy-mayfly def CatPC(lowVec: Vec[UInt], high: UInt, high1: UInt): Vec[UInt] = VecInit(lowVec.map(CatPC(_, high, high1))) 156d2b20d1aSTang Haojin require(numOfStage > 1, "BPU numOfStage must be greater than 1") 157d2b20d1aSTang Haojin val topdown_stages = RegInit(VecInit(Seq.fill(numOfStage)(0.U.asTypeOf(new FrontendTopDownBundle)))) 158d2b20d1aSTang Haojin // bubble events in IFU, only happen in stage 1 159d2b20d1aSTang Haojin val icacheMissBubble = Wire(Bool()) 160d2b20d1aSTang Haojin val itlbMissBubble =Wire(Bool()) 161d2b20d1aSTang Haojin 162d2b20d1aSTang Haojin // only driven by clock, not valid-ready 163d2b20d1aSTang Haojin topdown_stages(0) := fromFtq.req.bits.topdown_info 164d2b20d1aSTang Haojin for (i <- 1 until numOfStage) { 165d2b20d1aSTang Haojin topdown_stages(i) := topdown_stages(i - 1) 166d2b20d1aSTang Haojin } 167d2b20d1aSTang Haojin when (icacheMissBubble) { 168d2b20d1aSTang Haojin topdown_stages(1).reasons(TopDownCounters.ICacheMissBubble.id) := true.B 169d2b20d1aSTang Haojin } 170d2b20d1aSTang Haojin when (itlbMissBubble) { 171d2b20d1aSTang Haojin topdown_stages(1).reasons(TopDownCounters.ITLBMissBubble.id) := true.B 172d2b20d1aSTang Haojin } 173d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info := topdown_stages(numOfStage - 1) 174d2b20d1aSTang Haojin when (fromFtq.topdown_redirect.valid) { 175d2b20d1aSTang Haojin // only redirect from backend, IFU redirect itself is handled elsewhere 176d2b20d1aSTang Haojin when (fromFtq.topdown_redirect.bits.debugIsCtrl) { 177d2b20d1aSTang Haojin /* 178d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 179d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.ControlRedirectBubble.id) := true.B 180d2b20d1aSTang Haojin } 181d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.ControlRedirectBubble.id) := true.B 182d2b20d1aSTang Haojin */ 183d2b20d1aSTang Haojin when (fromFtq.topdown_redirect.bits.ControlBTBMissBubble) { 184d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 185d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.BTBMissBubble.id) := true.B 186d2b20d1aSTang Haojin } 187d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.BTBMissBubble.id) := true.B 188d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.TAGEMissBubble) { 189d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 190d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.TAGEMissBubble.id) := true.B 191d2b20d1aSTang Haojin } 192d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.TAGEMissBubble.id) := true.B 193d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.SCMissBubble) { 194d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 195d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.SCMissBubble.id) := true.B 196d2b20d1aSTang Haojin } 197d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.SCMissBubble.id) := true.B 198d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.ITTAGEMissBubble) { 199d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 200d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 201d2b20d1aSTang Haojin } 202d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 203d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.RASMissBubble) { 204d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 205d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.RASMissBubble.id) := true.B 206d2b20d1aSTang Haojin } 207d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.RASMissBubble.id) := true.B 208d2b20d1aSTang Haojin } 209d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.debugIsMemVio) { 210d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 211d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 212d2b20d1aSTang Haojin } 213d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 214d2b20d1aSTang Haojin } .otherwise { 215d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 216d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 217d2b20d1aSTang Haojin } 218d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 219d2b20d1aSTang Haojin } 220d2b20d1aSTang Haojin } 221d2b20d1aSTang Haojin 2221d8f4dcbSJay class TlbExept(implicit p: Parameters) extends XSBundle{ 2231d8f4dcbSJay val pageFault = Bool() 2241d8f4dcbSJay val accessFault = Bool() 2251d8f4dcbSJay val mmio = Bool() 226b005f7c6SJay } 22709c6f1ddSLingrui98 228a61a35e0Sssszwic val preDecoder = Module(new PreDecode) 229dc270d3bSJenius 2302a3050c2SJay val predChecker = Module(new PredChecker) 2312a3050c2SJay val frontendTrigger = Module(new FrontendTrigger) 2325995c9e7SJenius val (checkerIn, checkerOutStage1, checkerOutStage2) = (predChecker.io.in, predChecker.io.out.stage1Out,predChecker.io.out.stage2Out) 2331d8f4dcbSJay 23458dbdfc2SJay /** 23558dbdfc2SJay ****************************************************************************** 23658dbdfc2SJay * IFU Stage 0 23758dbdfc2SJay * - send cacheline fetch request to ICacheMainPipe 23858dbdfc2SJay ****************************************************************************** 23958dbdfc2SJay */ 24009c6f1ddSLingrui98 24109c6f1ddSLingrui98 val f0_valid = fromFtq.req.valid 24209c6f1ddSLingrui98 val f0_ftq_req = fromFtq.req.bits 2436ce52296SJinYue val f0_doubleLine = fromFtq.req.bits.crossCacheline 24434a88126SJinYue val f0_vSetIdx = VecInit(get_idx((f0_ftq_req.startAddr)), get_idx(f0_ftq_req.nextlineStart)) 245935edac4STang Haojin val f0_fire = fromFtq.req.fire 24609c6f1ddSLingrui98 24709c6f1ddSLingrui98 val f0_flush, f1_flush, f2_flush, f3_flush = WireInit(false.B) 24809c6f1ddSLingrui98 val from_bpu_f0_flush, from_bpu_f1_flush, from_bpu_f2_flush, from_bpu_f3_flush = WireInit(false.B) 24909c6f1ddSLingrui98 250cb4f77ceSLingrui98 from_bpu_f0_flush := fromFtq.flushFromBpu.shouldFlushByStage2(f0_ftq_req.ftqIdx) || 251cb4f77ceSLingrui98 fromFtq.flushFromBpu.shouldFlushByStage3(f0_ftq_req.ftqIdx) 25209c6f1ddSLingrui98 2532a3050c2SJay val wb_redirect , mmio_redirect, backend_redirect= WireInit(false.B) 2542a3050c2SJay val f3_wb_not_flush = WireInit(false.B) 2552a3050c2SJay 2562a3050c2SJay backend_redirect := fromFtq.redirect.valid 2572a3050c2SJay f3_flush := backend_redirect || (wb_redirect && !f3_wb_not_flush) 2582a3050c2SJay f2_flush := backend_redirect || mmio_redirect || wb_redirect 25909c6f1ddSLingrui98 f1_flush := f2_flush || from_bpu_f1_flush 26009c6f1ddSLingrui98 f0_flush := f1_flush || from_bpu_f0_flush 26109c6f1ddSLingrui98 26209c6f1ddSLingrui98 val f1_ready, f2_ready, f3_ready = WireInit(false.B) 26309c6f1ddSLingrui98 26450780602SJenius fromFtq.req.ready := f1_ready && io.icacheInter.icacheReady 26509c6f1ddSLingrui98 266d2b20d1aSTang Haojin 267d2b20d1aSTang Haojin when (wb_redirect) { 268d2b20d1aSTang Haojin when (f3_wb_not_flush) { 269d2b20d1aSTang Haojin topdown_stages(2).reasons(TopDownCounters.BTBMissBubble.id) := true.B 270d2b20d1aSTang Haojin } 271d2b20d1aSTang Haojin for (i <- 0 until numOfStage - 1) { 272d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.BTBMissBubble.id) := true.B 273d2b20d1aSTang Haojin } 274d2b20d1aSTang Haojin } 275d2b20d1aSTang Haojin 27658dbdfc2SJay /** <PERF> f0 fetch bubble */ 277f7c29b0aSJinYue 27800240ba6SJay XSPerfAccumulate("fetch_bubble_ftq_not_valid", !fromFtq.req.valid && fromFtq.req.ready ) 279c5c5edaeSJenius // XSPerfAccumulate("fetch_bubble_pipe_stall", f0_valid && toICache(0).ready && toICache(1).ready && !f1_ready ) 280c5c5edaeSJenius // XSPerfAccumulate("fetch_bubble_icache_0_busy", f0_valid && !toICache(0).ready ) 281c5c5edaeSJenius // XSPerfAccumulate("fetch_bubble_icache_1_busy", f0_valid && !toICache(1).ready ) 28200240ba6SJay XSPerfAccumulate("fetch_flush_backend_redirect", backend_redirect ) 28300240ba6SJay XSPerfAccumulate("fetch_flush_wb_redirect", wb_redirect ) 28400240ba6SJay XSPerfAccumulate("fetch_flush_bpu_f1_flush", from_bpu_f1_flush ) 28500240ba6SJay XSPerfAccumulate("fetch_flush_bpu_f0_flush", from_bpu_f0_flush ) 28658dbdfc2SJay 28758dbdfc2SJay 28858dbdfc2SJay /** 28958dbdfc2SJay ****************************************************************************** 29058dbdfc2SJay * IFU Stage 1 29158dbdfc2SJay * - calculate pc/half_pc/cut_ptr for every instruction 29258dbdfc2SJay ****************************************************************************** 29358dbdfc2SJay */ 29409c6f1ddSLingrui98 29509c6f1ddSLingrui98 val f1_valid = RegInit(false.B) 296005e809bSJiuyang Liu val f1_ftq_req = RegEnable(f0_ftq_req, f0_fire) 297005e809bSJiuyang Liu // val f1_situation = RegEnable(f0_situation, f0_fire) 298005e809bSJiuyang Liu val f1_doubleLine = RegEnable(f0_doubleLine, f0_fire) 299005e809bSJiuyang Liu val f1_vSetIdx = RegEnable(f0_vSetIdx, f0_fire) 300625ecd17SJenius val f1_fire = f1_valid && f2_ready 30109c6f1ddSLingrui98 302625ecd17SJenius f1_ready := f1_fire || !f1_valid 30309c6f1ddSLingrui98 3040d756c48SJinYue from_bpu_f1_flush := fromFtq.flushFromBpu.shouldFlushByStage3(f1_ftq_req.ftqIdx) && f1_valid 305cb4f77ceSLingrui98 // from_bpu_f1_flush := false.B 30609c6f1ddSLingrui98 30709c6f1ddSLingrui98 when(f1_flush) {f1_valid := false.B} 30809c6f1ddSLingrui98 .elsewhen(f0_fire && !f0_flush) {f1_valid := true.B} 30909c6f1ddSLingrui98 .elsewhen(f1_fire) {f1_valid := false.B} 31009c6f1ddSLingrui98 311e4d2f6a9Smy-mayfly val f1_pc_high = f1_ftq_req.startAddr(VAddrBits-1, PcCutPoint) 312f2f493deSstride val f1_pc_high_plus1 = f1_pc_high + 1.U 313f2f493deSstride 314e4d2f6a9Smy-mayfly /** 315e4d2f6a9Smy-mayfly * In order to reduce power consumption, avoid calculating the full PC value in the first level. 316e4d2f6a9Smy-mayfly * code of original logic, this code has been deprecated 317e4d2f6a9Smy-mayfly * val f1_pc = VecInit(f1_pc_lower_result.map{ i => 318e4d2f6a9Smy-mayfly * Mux(i(f1_pc_adder_cut_point), Cat(f1_pc_high_plus1,i(f1_pc_adder_cut_point-1,0)), Cat(f1_pc_high,i(f1_pc_adder_cut_point-1,0)))}) 319e4d2f6a9Smy-mayfly * 320e4d2f6a9Smy-mayfly */ 321e4d2f6a9Smy-mayfly val f1_pc_lower_result = VecInit((0 until PredictWidth).map(i => Cat(0.U(1.W), f1_ftq_req.startAddr(PcCutPoint-1, 0)) + (i * 2).U)) // cat with overflow bit 322f2f493deSstride 323e4d2f6a9Smy-mayfly val f1_pc = CatPC(f1_pc_lower_result, f1_pc_high, f1_pc_high_plus1) 324e4d2f6a9Smy-mayfly 325e4d2f6a9Smy-mayfly val f1_half_snpc_lower_result = VecInit((0 until PredictWidth).map(i => Cat(0.U(1.W), f1_ftq_req.startAddr(PcCutPoint-1, 0)) + ((i+2) * 2).U)) // cat with overflow bit 326e4d2f6a9Smy-mayfly val f1_half_snpc = CatPC(f1_half_snpc_lower_result, f1_pc_high, f1_pc_high_plus1) 327f2f493deSstride 328f2f493deSstride if (env.FPGAPlatform){ 329f2f493deSstride val f1_pc_diff = VecInit((0 until PredictWidth).map(i => f1_ftq_req.startAddr + (i * 2).U)) 330f2f493deSstride val f1_half_snpc_diff = VecInit((0 until PredictWidth).map(i => f1_ftq_req.startAddr + ((i+2) * 2).U)) 331f2f493deSstride 332f2f493deSstride XSError(f1_pc.zip(f1_pc_diff).map{ case (a,b) => a.asUInt =/= b.asUInt }.reduce(_||_), "f1_half_snpc adder cut fail") 333f2f493deSstride XSError(f1_half_snpc.zip(f1_half_snpc_diff).map{ case (a,b) => a.asUInt =/= b.asUInt }.reduce(_||_), "f1_half_snpc adder cut fail") 334f2f493deSstride } 335f2f493deSstride 336b92f8445Sssszwic val f1_cut_ptr = if(HasCExtension) VecInit((0 until PredictWidth + 1).map(i => Cat(0.U(2.W), f1_ftq_req.startAddr(blockOffBits-1, 1)) + i.U )) 337b92f8445Sssszwic else VecInit((0 until PredictWidth).map(i => Cat(0.U(2.W), f1_ftq_req.startAddr(blockOffBits-1, 2)) + i.U )) 33809c6f1ddSLingrui98 33958dbdfc2SJay /** 34058dbdfc2SJay ****************************************************************************** 34158dbdfc2SJay * IFU Stage 2 34258dbdfc2SJay * - icache response data (latched for pipeline stop) 34358dbdfc2SJay * - generate exceprion bits for every instruciton (page fault/access fault/mmio) 34458dbdfc2SJay * - generate predicted instruction range (1 means this instruciton is in this fetch packet) 34558dbdfc2SJay * - cut data from cachlines to packet instruction code 34658dbdfc2SJay * - instruction predecode and RVC expand 34758dbdfc2SJay ****************************************************************************** 34858dbdfc2SJay */ 34958dbdfc2SJay 3501d8f4dcbSJay val icacheRespAllValid = WireInit(false.B) 35109c6f1ddSLingrui98 35209c6f1ddSLingrui98 val f2_valid = RegInit(false.B) 353005e809bSJiuyang Liu val f2_ftq_req = RegEnable(f1_ftq_req, f1_fire) 354005e809bSJiuyang Liu // val f2_situation = RegEnable(f1_situation, f1_fire) 355005e809bSJiuyang Liu val f2_doubleLine = RegEnable(f1_doubleLine, f1_fire) 356005e809bSJiuyang Liu val f2_vSetIdx = RegEnable(f1_vSetIdx, f1_fire) 357625ecd17SJenius val f2_fire = f2_valid && f3_ready && icacheRespAllValid 3581d8f4dcbSJay 359625ecd17SJenius f2_ready := f2_fire || !f2_valid 3601d8f4dcbSJay //TODO: addr compare may be timing critical 36134a88126SJinYue val f2_icache_all_resp_wire = fromICache(0).valid && (fromICache(0).bits.vaddr === f2_ftq_req.startAddr) && ((fromICache(1).valid && (fromICache(1).bits.vaddr === f2_ftq_req.nextlineStart)) || !f2_doubleLine) 3621d8f4dcbSJay val f2_icache_all_resp_reg = RegInit(false.B) 3631d8f4dcbSJay 3641d8f4dcbSJay icacheRespAllValid := f2_icache_all_resp_reg || f2_icache_all_resp_wire 3651d8f4dcbSJay 366d2b20d1aSTang Haojin icacheMissBubble := io.icacheInter.topdownIcacheMiss 367d2b20d1aSTang Haojin itlbMissBubble := io.icacheInter.topdownItlbMiss 368d2b20d1aSTang Haojin 3691d8f4dcbSJay io.icacheStop := !f3_ready 3701d8f4dcbSJay 3711d8f4dcbSJay when(f2_flush) {f2_icache_all_resp_reg := false.B} 3721d8f4dcbSJay .elsewhen(f2_valid && f2_icache_all_resp_wire && !f3_ready) {f2_icache_all_resp_reg := true.B} 3731d8f4dcbSJay .elsewhen(f2_fire && f2_icache_all_resp_reg) {f2_icache_all_resp_reg := false.B} 37409c6f1ddSLingrui98 37509c6f1ddSLingrui98 when(f2_flush) {f2_valid := false.B} 37609c6f1ddSLingrui98 .elsewhen(f1_fire && !f1_flush) {f2_valid := true.B } 37709c6f1ddSLingrui98 .elsewhen(f2_fire) {f2_valid := false.B} 37809c6f1ddSLingrui98 379*88895b11Sxu_zh val f2_exception = VecInit((0 until PortNumber).map(i => fromICache(i).bits.exception)) 380d7ac23a3SEaston Man // paddr and gpaddr of [startAddr, nextLineAddr] 381d7ac23a3SEaston Man val f2_paddrs = VecInit((0 until PortNumber).map(i => fromICache(i).bits.paddr)) 38291946104Sxu_zh val f2_gpaddr = fromICache(0).bits.gpaddr 383*88895b11Sxu_zh // cancel mmio fetch if exception occurs 384*88895b11Sxu_zh val f2_mmio = fromICache(0).bits.mmio && f2_exception(0) === ExceptionType.none 3850be662e4SJay 386e4d2f6a9Smy-mayfly /** 387e4d2f6a9Smy-mayfly * reduce the number of registers, origin code 388e4d2f6a9Smy-mayfly * f2_pc = RegEnable(f1_pc, f1_fire) 389e4d2f6a9Smy-mayfly */ 390e4d2f6a9Smy-mayfly val f2_pc_lower_result = RegEnable(f1_pc_lower_result, f1_fire) 391e4d2f6a9Smy-mayfly val f2_pc_high = RegEnable(f1_pc_high, f1_fire) 392e4d2f6a9Smy-mayfly val f2_pc_high_plus1 = RegEnable(f1_pc_high_plus1, f1_fire) 393e4d2f6a9Smy-mayfly val f2_pc = CatPC(f2_pc_lower_result, f2_pc_high, f2_pc_high_plus1) 394a37fbf10SJay 395e4d2f6a9Smy-mayfly val f2_cut_ptr = RegEnable(f1_cut_ptr, f1_fire) 396005e809bSJiuyang Liu val f2_resend_vaddr = RegEnable(f1_ftq_req.startAddr + 2.U, f1_fire) 3972a3050c2SJay 3982a3050c2SJay def isNextLine(pc: UInt, startAddr: UInt) = { 3992a3050c2SJay startAddr(blockOffBits) ^ pc(blockOffBits) 400b6982e83SLemover } 40109c6f1ddSLingrui98 4022a3050c2SJay def isLastInLine(pc: UInt) = { 4032a3050c2SJay pc(blockOffBits - 1, 0) === "b111110".U 40409c6f1ddSLingrui98 } 40509c6f1ddSLingrui98 4062a3050c2SJay val f2_foldpc = VecInit(f2_pc.map(i => XORFold(i(VAddrBits-1,1), MemPredPCWidth))) 4072a3050c2SJay val f2_jump_range = Fill(PredictWidth, !f2_ftq_req.ftqOffset.valid) | Fill(PredictWidth, 1.U(1.W)) >> ~f2_ftq_req.ftqOffset.bits 4081d011975SJinYue val f2_ftr_range = Fill(PredictWidth, f2_ftq_req.ftqOffset.valid) | Fill(PredictWidth, 1.U(1.W)) >> ~getBasicBlockIdx(f2_ftq_req.nextStartAddr, f2_ftq_req.startAddr) 4092a3050c2SJay val f2_instr_range = f2_jump_range & f2_ftr_range 410*88895b11Sxu_zh val f2_exception_vec = VecInit((0 until PredictWidth).map( i => MuxCase(ExceptionType.none, Seq( 411*88895b11Sxu_zh !isNextLine(f2_pc(i), f2_ftq_req.startAddr) -> f2_exception(0), 412*88895b11Sxu_zh (isNextLine(f2_pc(i), f2_ftq_req.startAddr) && f2_doubleLine) -> f2_exception(1) 413*88895b11Sxu_zh )))) 4141d8f4dcbSJay val f2_perf_info = io.icachePerfInfo 41509c6f1ddSLingrui98 4162a3050c2SJay def cut(cacheline: UInt, cutPtr: Vec[UInt]) : Vec[UInt] ={ 417d558bd61SJenius require(HasCExtension) 418d558bd61SJenius // if(HasCExtension){ 41909c6f1ddSLingrui98 val result = Wire(Vec(PredictWidth + 1, UInt(16.W))) 420b92f8445Sssszwic val dataVec = cacheline.asTypeOf(Vec(blockBytes, UInt(16.W))) //32 16-bit data vector 42109c6f1ddSLingrui98 (0 until PredictWidth + 1).foreach( i => 422d558bd61SJenius result(i) := dataVec(cutPtr(i)) //the max ptr is 3*blockBytes/4-1 42309c6f1ddSLingrui98 ) 42409c6f1ddSLingrui98 result 425d558bd61SJenius // } else { 426d558bd61SJenius // val result = Wire(Vec(PredictWidth, UInt(32.W)) ) 427d558bd61SJenius // val dataVec = cacheline.asTypeOf(Vec(blockBytes * 2/ 4, UInt(32.W))) 428d558bd61SJenius // (0 until PredictWidth).foreach( i => 429d558bd61SJenius // result(i) := dataVec(cutPtr(i)) 430d558bd61SJenius // ) 431d558bd61SJenius // result 432d558bd61SJenius // } 43309c6f1ddSLingrui98 } 43409c6f1ddSLingrui98 435a61a35e0Sssszwic val f2_cache_response_data = fromICache.map(_.bits.data) 436b92f8445Sssszwic val f2_data_2_cacheline = Cat(f2_cache_response_data(0), f2_cache_response_data(0)) 437dc270d3bSJenius 438a61a35e0Sssszwic val f2_cut_data = cut(f2_data_2_cacheline, f2_cut_ptr) 43909c6f1ddSLingrui98 44058dbdfc2SJay /** predecode (include RVC expander) */ 441dc270d3bSJenius // preDecoderRegIn.data := f2_reg_cut_data 442dc270d3bSJenius // preDecoderRegInIn.frontendTrigger := io.frontendTrigger 443dc270d3bSJenius // preDecoderRegInIn.csrTriggerEnable := io.csrTriggerEnable 444dc270d3bSJenius // preDecoderRegIn.pc := f2_pc 445dc270d3bSJenius 446a61a35e0Sssszwic val preDecoderIn = preDecoder.io.in 4479afa8a47STang Haojin preDecoderIn.valid := f2_valid 4489afa8a47STang Haojin preDecoderIn.bits.data := f2_cut_data 4499afa8a47STang Haojin preDecoderIn.bits.frontendTrigger := io.frontendTrigger 4509afa8a47STang Haojin preDecoderIn.bits.pc := f2_pc 451a61a35e0Sssszwic val preDecoderOut = preDecoder.io.out 45209c6f1ddSLingrui98 45348a62719SJenius //val f2_expd_instr = preDecoderOut.expInstr 45448a62719SJenius val f2_instr = preDecoderOut.instr 4552a3050c2SJay val f2_pd = preDecoderOut.pd 4562a3050c2SJay val f2_jump_offset = preDecoderOut.jumpOffset 4572a3050c2SJay val f2_hasHalfValid = preDecoderOut.hasHalfValid 458*88895b11Sxu_zh val f2_crossPageFault = VecInit((0 until PredictWidth).map( i => 459*88895b11Sxu_zh isLastInLine(f2_pc(i)) && (f2_exception(0) =/= ExceptionType.pf) && f2_doubleLine && (f2_exception(1) === ExceptionType.pf) && !f2_pd(i).isRVC 460*88895b11Sxu_zh )) 461*88895b11Sxu_zh val f2_crossGuestPageFault = VecInit((0 until PredictWidth).map( i => 462*88895b11Sxu_zh isLastInLine(f2_pc(i)) && (f2_exception(0) =/= ExceptionType.gpf) && f2_doubleLine && (f2_exception(1) === ExceptionType.gpf) && !f2_pd(i).isRVC 463*88895b11Sxu_zh )) 46400240ba6SJay XSPerfAccumulate("fetch_bubble_icache_not_resp", f2_valid && !icacheRespAllValid ) 46500240ba6SJay 46609c6f1ddSLingrui98 46758dbdfc2SJay /** 46858dbdfc2SJay ****************************************************************************** 46958dbdfc2SJay * IFU Stage 3 47058dbdfc2SJay * - handle MMIO instruciton 47158dbdfc2SJay * -send request to Uncache fetch Unit 47258dbdfc2SJay * -every packet include 1 MMIO instruction 47358dbdfc2SJay * -MMIO instructions will stop fetch pipeline until commiting from RoB 47458dbdfc2SJay * -flush to snpc (send ifu_redirect to Ftq) 47558dbdfc2SJay * - Ibuffer enqueue 47658dbdfc2SJay * - check predict result in Frontend (jalFault/retFault/notCFIFault/invalidTakenFault/targetFault) 47758dbdfc2SJay * - handle last half RVI instruction 47858dbdfc2SJay ****************************************************************************** 47958dbdfc2SJay */ 48058dbdfc2SJay 48109c6f1ddSLingrui98 val f3_valid = RegInit(false.B) 482005e809bSJiuyang Liu val f3_ftq_req = RegEnable(f2_ftq_req, f2_fire) 483005e809bSJiuyang Liu // val f3_situation = RegEnable(f2_situation, f2_fire) 484005e809bSJiuyang Liu val f3_doubleLine = RegEnable(f2_doubleLine, f2_fire) 485935edac4STang Haojin val f3_fire = io.toIbuffer.fire 4861d8f4dcbSJay 487a61a35e0Sssszwic val f3_cut_data = RegEnable(f2_cut_data, f2_fire) 4881d8f4dcbSJay 489*88895b11Sxu_zh val f3_exception = RegEnable(f2_exception, f2_fire) 490005e809bSJiuyang Liu val f3_mmio = RegEnable(f2_mmio, f2_fire) 49109c6f1ddSLingrui98 492935edac4STang Haojin //val f3_expd_instr = RegEnable(f2_expd_instr, f2_fire) 493935edac4STang Haojin val f3_instr = RegEnable(f2_instr, f2_fire) 49448a62719SJenius val f3_expd_instr = VecInit((0 until PredictWidth).map{ i => 49548a62719SJenius val expander = Module(new RVCExpander) 49648a62719SJenius expander.io.in := f3_instr(i) 49748a62719SJenius expander.io.out.bits 49848a62719SJenius }) 49948a62719SJenius 500935edac4STang Haojin val f3_pd_wire = RegEnable(f2_pd, f2_fire) 501330aad7fSGuokai Chen val f3_pd = WireInit(f3_pd_wire) 502935edac4STang Haojin val f3_jump_offset = RegEnable(f2_jump_offset, f2_fire) 503*88895b11Sxu_zh val f3_exception_vec = RegEnable(f2_exception_vec, f2_fire) 504e4d2f6a9Smy-mayfly 505e4d2f6a9Smy-mayfly val f3_pc_lower_result = RegEnable(f2_pc_lower_result, f2_fire) 506e4d2f6a9Smy-mayfly val f3_pc_high = RegEnable(f2_pc_high, f2_fire) 507e4d2f6a9Smy-mayfly val f3_pc_high_plus1 = RegEnable(f2_pc_high_plus1, f2_fire) 508e4d2f6a9Smy-mayfly val f3_pc = CatPC(f3_pc_lower_result, f3_pc_high, f3_pc_high_plus1) 509e4d2f6a9Smy-mayfly 510e4d2f6a9Smy-mayfly val f3_pc_last_lower_result_plus2 = RegEnable(f2_pc_lower_result(PredictWidth - 1) + 2.U, f2_fire) 511e4d2f6a9Smy-mayfly val f3_pc_last_lower_result_plus4 = RegEnable(f2_pc_lower_result(PredictWidth - 1) + 4.U, f2_fire) 512e4d2f6a9Smy-mayfly //val f3_half_snpc = RegEnable(f2_half_snpc, f2_fire) 513e4d2f6a9Smy-mayfly 514e4d2f6a9Smy-mayfly /** 515e4d2f6a9Smy-mayfly *********************************************************************** 516e4d2f6a9Smy-mayfly * Half snpc(i) is larger than pc(i) by 4. Using pc to calculate half snpc may be a good choice. 517e4d2f6a9Smy-mayfly *********************************************************************** 518e4d2f6a9Smy-mayfly */ 519e4d2f6a9Smy-mayfly val f3_half_snpc = Wire(Vec(PredictWidth,UInt(VAddrBits.W))) 520e4d2f6a9Smy-mayfly for(i <- 0 until PredictWidth){ 521e4d2f6a9Smy-mayfly if(i == (PredictWidth - 2)){ 522e4d2f6a9Smy-mayfly f3_half_snpc(i) := CatPC(f3_pc_last_lower_result_plus2, f3_pc_high, f3_pc_high_plus1) 523e4d2f6a9Smy-mayfly } else if (i == (PredictWidth - 1)){ 524e4d2f6a9Smy-mayfly f3_half_snpc(i) := CatPC(f3_pc_last_lower_result_plus4, f3_pc_high, f3_pc_high_plus1) 525e4d2f6a9Smy-mayfly } else { 526e4d2f6a9Smy-mayfly f3_half_snpc(i) := f3_pc(i+2) 527e4d2f6a9Smy-mayfly } 528e4d2f6a9Smy-mayfly } 529e4d2f6a9Smy-mayfly 530935edac4STang Haojin val f3_instr_range = RegEnable(f2_instr_range, f2_fire) 531935edac4STang Haojin val f3_foldpc = RegEnable(f2_foldpc, f2_fire) 532935edac4STang Haojin val f3_crossPageFault = RegEnable(f2_crossPageFault, f2_fire) 5330214776eSpeixiaokun val f3_crossGuestPageFault = RegEnable(f2_crossGuestPageFault, f2_fire) 534935edac4STang Haojin val f3_hasHalfValid = RegEnable(f2_hasHalfValid, f2_fire) 535d7ac23a3SEaston Man val f3_paddrs = RegEnable(f2_paddrs, f2_fire) 53691946104Sxu_zh val f3_gpaddr = RegEnable(f2_gpaddr, f2_fire) 537005e809bSJiuyang Liu val f3_resend_vaddr = RegEnable(f2_resend_vaddr, f2_fire) 538ee175d78SJay 539cb6e5d3cSssszwic // Expand 1 bit to prevent overflow when assert 540cb6e5d3cSssszwic val f3_ftq_req_startAddr = Cat(0.U(1.W), f3_ftq_req.startAddr) 541cb6e5d3cSssszwic val f3_ftq_req_nextStartAddr = Cat(0.U(1.W), f3_ftq_req.nextStartAddr) 542330aad7fSGuokai Chen // brType, isCall and isRet generation is delayed to f3 stage 543330aad7fSGuokai Chen val f3Predecoder = Module(new F3Predecoder) 544330aad7fSGuokai Chen 545330aad7fSGuokai Chen f3Predecoder.io.in.instr := f3_instr 546330aad7fSGuokai Chen 547330aad7fSGuokai Chen f3_pd.zipWithIndex.map{ case (pd,i) => 548330aad7fSGuokai Chen pd.brType := f3Predecoder.io.out.pd(i).brType 549330aad7fSGuokai Chen pd.isCall := f3Predecoder.io.out.pd(i).isCall 550330aad7fSGuokai Chen pd.isRet := f3Predecoder.io.out.pd(i).isRet 551330aad7fSGuokai Chen } 552330aad7fSGuokai Chen 553330aad7fSGuokai Chen val f3PdDiff = f3_pd_wire.zip(f3_pd).map{ case (a,b) => a.asUInt =/= b.asUInt }.reduce(_||_) 554330aad7fSGuokai Chen XSError(f3_valid && f3PdDiff, "f3 pd diff") 555330aad7fSGuokai Chen 5561d011975SJinYue when(f3_valid && !f3_ftq_req.ftqOffset.valid){ 557cb6e5d3cSssszwic assert(f3_ftq_req_startAddr + (2*PredictWidth).U >= f3_ftq_req_nextStartAddr, s"More tha ${2*PredictWidth} Bytes fetch is not allowed!") 5581d011975SJinYue } 559a1351e5dSJay 5602a3050c2SJay /*** MMIO State Machine***/ 561ee175d78SJay val f3_mmio_data = Reg(Vec(2, UInt(16.W))) 562ee175d78SJay val mmio_is_RVC = RegInit(false.B) 563ee175d78SJay val mmio_resend_addr = RegInit(0.U(PAddrBits.W)) 564*88895b11Sxu_zh val mmio_resend_exception = RegInit(0.U(ExceptionType.width.W)) 565b5a614b9Sxu_zh val mmio_resend_gpaddr = RegInit(0.U(GPAddrBits.W)) 566c3b2d83aSJay 5671d1e6d4dSJenius //last instuction finish 5681d1e6d4dSJenius val is_first_instr = RegInit(true.B) 569ba5ba1dcSmy-mayfly /*** Determine whether the MMIO instruction is executable based on the previous prediction block ***/ 570ba5ba1dcSmy-mayfly io.mmioCommitRead.mmioFtqPtr := RegNext(f3_ftq_req.ftqIdx - 1.U) 571a37fbf10SJay 5721d1e6d4dSJenius val m_idle :: m_waitLastCmt:: m_sendReq :: m_waitResp :: m_sendTLB :: m_tlbResp :: m_sendPMP :: m_resendReq :: m_waitResendResp :: m_waitCommit :: m_commited :: Nil = Enum(11) 573ee175d78SJay val mmio_state = RegInit(m_idle) 574a37fbf10SJay 5759bae7d6eSJay val f3_req_is_mmio = f3_mmio && f3_valid 5762a3050c2SJay val mmio_commit = VecInit(io.rob_commits.map{commit => commit.valid && commit.bits.ftqIdx === f3_ftq_req.ftqIdx && commit.bits.ftqOffset === 0.U}).asUInt.orR 577ee175d78SJay val f3_mmio_req_commit = f3_req_is_mmio && mmio_state === m_commited 578a37fbf10SJay 579ee175d78SJay val f3_mmio_to_commit = f3_req_is_mmio && mmio_state === m_waitCommit 580a37fbf10SJay val f3_mmio_to_commit_next = RegNext(f3_mmio_to_commit) 581a37fbf10SJay val f3_mmio_can_go = f3_mmio_to_commit && !f3_mmio_to_commit_next 582a37fbf10SJay 5830c70648eSEaston Man val fromFtqRedirectReg = Wire(fromFtq.redirect.cloneType) 5840c70648eSEaston Man fromFtqRedirectReg.bits := RegEnable(fromFtq.redirect.bits, 0.U.asTypeOf(fromFtq.redirect.bits), fromFtq.redirect.valid) 5850c70648eSEaston Man fromFtqRedirectReg.valid := RegNext(fromFtq.redirect.valid, init = false.B) 5864a74a727SJenius val mmioF3Flush = RegNext(f3_flush,init = false.B) 58756788a33SJinYue val f3_ftq_flush_self = fromFtqRedirectReg.valid && RedirectLevel.flushItself(fromFtqRedirectReg.bits.level) 58856788a33SJinYue val f3_ftq_flush_by_older = fromFtqRedirectReg.valid && isBefore(fromFtqRedirectReg.bits.ftqIdx, f3_ftq_req.ftqIdx) 5899bae7d6eSJay 59056788a33SJinYue val f3_need_not_flush = f3_req_is_mmio && fromFtqRedirectReg.valid && !f3_ftq_flush_self && !f3_ftq_flush_by_older 5919bae7d6eSJay 592ba5ba1dcSmy-mayfly /** 593ba5ba1dcSmy-mayfly ********************************************************************************** 594ba5ba1dcSmy-mayfly * We want to defer instruction fetching when encountering MMIO instructions to ensure that the MMIO region is not negatively impacted. 595ba5ba1dcSmy-mayfly * This is the exception when the first instruction is an MMIO instruction. 596ba5ba1dcSmy-mayfly ********************************************************************************** 597ba5ba1dcSmy-mayfly */ 598ba5ba1dcSmy-mayfly when(is_first_instr && f3_fire){ 5991d1e6d4dSJenius is_first_instr := false.B 6001d1e6d4dSJenius } 6011d1e6d4dSJenius 6024a74a727SJenius when(f3_flush && !f3_req_is_mmio) {f3_valid := false.B} 6034a74a727SJenius .elsewhen(mmioF3Flush && f3_req_is_mmio && !f3_need_not_flush) {f3_valid := false.B} 604a37fbf10SJay .elsewhen(f2_fire && !f2_flush ) {f3_valid := true.B } 605935edac4STang Haojin .elsewhen(io.toIbuffer.fire && !f3_req_is_mmio) {f3_valid := false.B} 606a37fbf10SJay .elsewhen{f3_req_is_mmio && f3_mmio_req_commit} {f3_valid := false.B} 607a37fbf10SJay 608a37fbf10SJay val f3_mmio_use_seq_pc = RegInit(false.B) 609a37fbf10SJay 61056788a33SJinYue val (redirect_ftqIdx, redirect_ftqOffset) = (fromFtqRedirectReg.bits.ftqIdx,fromFtqRedirectReg.bits.ftqOffset) 61156788a33SJinYue val redirect_mmio_req = fromFtqRedirectReg.valid && redirect_ftqIdx === f3_ftq_req.ftqIdx && redirect_ftqOffset === 0.U 612a37fbf10SJay 613a37fbf10SJay when(RegNext(f2_fire && !f2_flush) && f3_req_is_mmio) { f3_mmio_use_seq_pc := true.B } 614a37fbf10SJay .elsewhen(redirect_mmio_req) { f3_mmio_use_seq_pc := false.B } 615a37fbf10SJay 6168c192ff7Sxu_zh f3_ready := (io.toIbuffer.ready && (f3_mmio_req_commit || !f3_req_is_mmio)) || !f3_valid 617a37fbf10SJay 6181d1e6d4dSJenius // mmio state machine 619a37fbf10SJay switch(mmio_state){ 620ee175d78SJay is(m_idle){ 6219bae7d6eSJay when(f3_req_is_mmio){ 6221d1e6d4dSJenius mmio_state := m_waitLastCmt 6231d1e6d4dSJenius } 6241d1e6d4dSJenius } 6251d1e6d4dSJenius 6261d1e6d4dSJenius is(m_waitLastCmt){ 6271d1e6d4dSJenius when(is_first_instr){ 628ee175d78SJay mmio_state := m_sendReq 6291d1e6d4dSJenius }.otherwise{ 6301d1e6d4dSJenius mmio_state := Mux(io.mmioCommitRead.mmioLastCommit, m_sendReq, m_waitLastCmt) 631a37fbf10SJay } 632a37fbf10SJay } 633a37fbf10SJay 634ee175d78SJay is(m_sendReq){ 635935edac4STang Haojin mmio_state := Mux(toUncache.fire, m_waitResp, m_sendReq) 636a37fbf10SJay } 637a37fbf10SJay 638ee175d78SJay is(m_waitResp){ 639935edac4STang Haojin when(fromUncache.fire){ 640a37fbf10SJay val isRVC = fromUncache.bits.data(1,0) =/= 3.U 641d7ac23a3SEaston Man val needResend = !isRVC && f3_paddrs(0)(2,1) === 3.U 642ee175d78SJay mmio_state := Mux(needResend, m_sendTLB, m_waitCommit) 643ee175d78SJay mmio_is_RVC := isRVC 644ee175d78SJay f3_mmio_data(0) := fromUncache.bits.data(15,0) 645ee175d78SJay f3_mmio_data(1) := fromUncache.bits.data(31,16) 646a37fbf10SJay } 647a37fbf10SJay } 648a37fbf10SJay 649ee175d78SJay is(m_sendTLB){ 6507b7232f9Sxu_zh mmio_state := Mux(io.iTLBInter.req.fire, m_tlbResp, m_sendTLB) 651c3b2d83aSJay } 652a37fbf10SJay 653ee175d78SJay is(m_tlbResp){ 6547b7232f9Sxu_zh when(io.iTLBInter.resp.fire) { 6557b7232f9Sxu_zh // we are using a blocked tlb, so resp.fire must have !resp.bits.miss 6567b7232f9Sxu_zh assert(!io.iTLBInter.resp.bits.miss, "blocked mode iTLB miss when resp.fire") 657*88895b11Sxu_zh val tlb_exception = ExceptionType.fromTlbResp(io.iTLBInter.resp.bits) 6587b7232f9Sxu_zh // if tlb has exception, abort checking pmp, just send instr & exception to ibuffer and wait for commit 659*88895b11Sxu_zh mmio_state := Mux(tlb_exception === ExceptionType.none, m_sendPMP, m_waitCommit) 6607b7232f9Sxu_zh // also save itlb response 66103efd994Shappy-lx mmio_resend_addr := io.iTLBInter.resp.bits.paddr(0) 662*88895b11Sxu_zh mmio_resend_exception := tlb_exception 663b5a614b9Sxu_zh mmio_resend_gpaddr := io.iTLBInter.resp.bits.gpaddr(0) 664ee175d78SJay } 6657b7232f9Sxu_zh } 666ee175d78SJay 667ee175d78SJay is(m_sendPMP){ 668*88895b11Sxu_zh // if pmp re-check does not respond mmio, must be access fault 669*88895b11Sxu_zh val pmp_exception = Mux(io.pmp.resp.mmio, ExceptionType.fromPMPResp(io.pmp.resp), ExceptionType.af) 670*88895b11Sxu_zh // if pmp has exception, abort sending request, just send instr & exception to ibuffer and wait for commit 671*88895b11Sxu_zh mmio_state := Mux(pmp_exception === ExceptionType.none, m_resendReq, m_waitCommit) 672*88895b11Sxu_zh // also save pmp response 673*88895b11Sxu_zh mmio_resend_exception := pmp_exception 674ee175d78SJay } 675ee175d78SJay 676ee175d78SJay is(m_resendReq){ 677935edac4STang Haojin mmio_state := Mux(toUncache.fire, m_waitResendResp, m_resendReq) 678ee175d78SJay } 679ee175d78SJay 680ee175d78SJay is(m_waitResendResp) { 681935edac4STang Haojin when(fromUncache.fire) { 682ee175d78SJay mmio_state := m_waitCommit 683ee175d78SJay f3_mmio_data(1) := fromUncache.bits.data(15,0) 684a37fbf10SJay } 685a37fbf10SJay } 686a37fbf10SJay 687ee175d78SJay is(m_waitCommit) { 6887b7232f9Sxu_zh mmio_state := Mux(mmio_commit, m_commited, m_waitCommit) 689a37fbf10SJay } 6902a3050c2SJay 691ee175d78SJay //normal mmio instruction 692ee175d78SJay is(m_commited) { 693ee175d78SJay mmio_state := m_idle 694ee175d78SJay mmio_is_RVC := false.B 695ee175d78SJay mmio_resend_addr := 0.U 696*88895b11Sxu_zh mmio_resend_exception := ExceptionType.none 697b5a614b9Sxu_zh mmio_resend_gpaddr := 0.U 6982a3050c2SJay } 699a37fbf10SJay } 700a37fbf10SJay 7018abe1810SEaston Man // Exception or flush by older branch prediction 7028abe1810SEaston Man // Condition is from RegNext(fromFtq.redirect), 1 cycle after backend rediect 703167bcd01SJay when(f3_ftq_flush_self || f3_ftq_flush_by_older) { 704ee175d78SJay mmio_state := m_idle 705ee175d78SJay mmio_is_RVC := false.B 706ee175d78SJay mmio_resend_addr := 0.U 707*88895b11Sxu_zh mmio_resend_exception := ExceptionType.none 708b5a614b9Sxu_zh mmio_resend_gpaddr := 0.U 709ee175d78SJay f3_mmio_data.map(_ := 0.U) 7109bae7d6eSJay } 7119bae7d6eSJay 712ee175d78SJay toUncache.valid := ((mmio_state === m_sendReq) || (mmio_state === m_resendReq)) && f3_req_is_mmio 713d7ac23a3SEaston Man toUncache.bits.addr := Mux((mmio_state === m_resendReq), mmio_resend_addr, f3_paddrs(0)) 714a37fbf10SJay fromUncache.ready := true.B 715a37fbf10SJay 7167b7232f9Sxu_zh // send itlb request in m_sendTLB state 717ee175d78SJay io.iTLBInter.req.valid := (mmio_state === m_sendTLB) && f3_req_is_mmio 718ee175d78SJay io.iTLBInter.req.bits.size := 3.U 719ee175d78SJay io.iTLBInter.req.bits.vaddr := f3_resend_vaddr 720ee175d78SJay io.iTLBInter.req.bits.debug.pc := f3_resend_vaddr 7217b7232f9Sxu_zh io.iTLBInter.req.bits.cmd := TlbCmd.exec 7227b7232f9Sxu_zh io.iTLBInter.req.bits.kill := false.B // IFU use itlb for mmio, doesn't need sync, set it to false 7237b7232f9Sxu_zh io.iTLBInter.req.bits.no_translate := false.B 724d0de7e4aSpeixiaokun io.iTLBInter.req.bits.hyperinst := DontCare 725d0de7e4aSpeixiaokun io.iTLBInter.req.bits.hlvx := DontCare 7268744445eSMaxpicca-Li io.iTLBInter.req.bits.memidx := DontCare 727f1fe8698SLemover io.iTLBInter.req.bits.debug.robIdx := DontCare 728ee175d78SJay io.iTLBInter.req.bits.debug.isFirstIssue := DontCare 729149a2326Sweiding liu io.iTLBInter.req.bits.pmp_addr := DontCare 7307b7232f9Sxu_zh // whats the difference between req_kill and req.bits.kill? 7317b7232f9Sxu_zh io.iTLBInter.req_kill := false.B 7327b7232f9Sxu_zh // wait for itlb response in m_tlbResp state 7337b7232f9Sxu_zh io.iTLBInter.resp.ready := (mmio_state === m_tlbResp) && f3_req_is_mmio 734ee175d78SJay 735ee175d78SJay io.pmp.req.valid := (mmio_state === m_sendPMP) && f3_req_is_mmio 736ee175d78SJay io.pmp.req.bits.addr := mmio_resend_addr 737ee175d78SJay io.pmp.req.bits.size := 3.U 738ee175d78SJay io.pmp.req.bits.cmd := TlbCmd.exec 739f7c29b0aSJinYue 7402a3050c2SJay val f3_lastHalf = RegInit(0.U.asTypeOf(new LastHalfInfo)) 74109c6f1ddSLingrui98 74209c6f1ddSLingrui98 val f3_predecode_range = VecInit(preDecoderOut.pd.map(inst => inst.valid)).asUInt 7430be662e4SJay val f3_mmio_range = VecInit((0 until PredictWidth).map(i => if(i ==0) true.B else false.B)) 7442a3050c2SJay val f3_instr_valid = Wire(Vec(PredictWidth, Bool())) 74509c6f1ddSLingrui98 7462a3050c2SJay /*** prediction result check ***/ 7472a3050c2SJay checkerIn.ftqOffset := f3_ftq_req.ftqOffset 7482a3050c2SJay checkerIn.jumpOffset := f3_jump_offset 7496ce52296SJinYue checkerIn.target := f3_ftq_req.nextStartAddr 7502a3050c2SJay checkerIn.instrRange := f3_instr_range.asTypeOf(Vec(PredictWidth, Bool())) 7512a3050c2SJay checkerIn.instrValid := f3_instr_valid.asTypeOf(Vec(PredictWidth, Bool())) 7522a3050c2SJay checkerIn.pds := f3_pd 7532a3050c2SJay checkerIn.pc := f3_pc 7540c70648eSEaston Man checkerIn.fire_in := RegNext(f2_fire, init = false.B) 7552a3050c2SJay 75658dbdfc2SJay /*** handle half RVI in the last 2 Bytes ***/ 7572a3050c2SJay 7582a3050c2SJay def hasLastHalf(idx: UInt) = { 7595995c9e7SJenius //!f3_pd(idx).isRVC && checkerOutStage1.fixedRange(idx) && f3_instr_valid(idx) && !checkerOutStage1.fixedTaken(idx) && !checkerOutStage2.fixedMissPred(idx) && ! f3_req_is_mmio 7605995c9e7SJenius !f3_pd(idx).isRVC && checkerOutStage1.fixedRange(idx) && f3_instr_valid(idx) && !checkerOutStage1.fixedTaken(idx) && ! f3_req_is_mmio 7612a3050c2SJay } 7622a3050c2SJay 763b665b650STang Haojin val f3_last_validIdx = ParallelPosteriorityEncoder(checkerOutStage1.fixedRange) 7642a3050c2SJay 7652a3050c2SJay val f3_hasLastHalf = hasLastHalf((PredictWidth - 1).U) 7662a3050c2SJay val f3_false_lastHalf = hasLastHalf(f3_last_validIdx) 7672a3050c2SJay val f3_false_snpc = f3_half_snpc(f3_last_validIdx) 7682a3050c2SJay 769935edac4STang Haojin val f3_lastHalf_mask = VecInit((0 until PredictWidth).map( i => if(i ==0) false.B else true.B )).asUInt 7703f785aa3SJenius val f3_lastHalf_disable = RegInit(false.B) 7712a3050c2SJay 772804985a5SJenius when(f3_flush || (f3_fire && f3_lastHalf_disable)){ 773804985a5SJenius f3_lastHalf_disable := false.B 774804985a5SJenius } 775804985a5SJenius 7762a3050c2SJay when (f3_flush) { 7772a3050c2SJay f3_lastHalf.valid := false.B 7782a3050c2SJay }.elsewhen (f3_fire) { 7793f785aa3SJenius f3_lastHalf.valid := f3_hasLastHalf && !f3_lastHalf_disable 7806ce52296SJinYue f3_lastHalf.middlePC := f3_ftq_req.nextStartAddr 7812a3050c2SJay } 7822a3050c2SJay 7832a3050c2SJay f3_instr_valid := Mux(f3_lastHalf.valid,f3_hasHalfValid ,VecInit(f3_pd.map(inst => inst.valid))) 7842a3050c2SJay 7852a3050c2SJay /*** frontend Trigger ***/ 7862a3050c2SJay frontendTrigger.io.pds := f3_pd 7872a3050c2SJay frontendTrigger.io.pc := f3_pc 7882a3050c2SJay frontendTrigger.io.data := f3_cut_data 7892a3050c2SJay 7902a3050c2SJay frontendTrigger.io.frontendTrigger := io.frontendTrigger 7912a3050c2SJay 7922a3050c2SJay val f3_triggered = frontendTrigger.io.triggered 79391946104Sxu_zh val f3_toIbuffer_valid = f3_valid && (!f3_req_is_mmio || f3_mmio_can_go) && !f3_flush 7942a3050c2SJay 7952a3050c2SJay /*** send to Ibuffer ***/ 79691946104Sxu_zh io.toIbuffer.valid := f3_toIbuffer_valid 7972a3050c2SJay io.toIbuffer.bits.instrs := f3_expd_instr 7982a3050c2SJay io.toIbuffer.bits.valid := f3_instr_valid.asUInt 7995995c9e7SJenius io.toIbuffer.bits.enqEnable := checkerOutStage1.fixedRange.asUInt & f3_instr_valid.asUInt 8002a3050c2SJay io.toIbuffer.bits.pd := f3_pd 80109c6f1ddSLingrui98 io.toIbuffer.bits.ftqPtr := f3_ftq_req.ftqIdx 8022a3050c2SJay io.toIbuffer.bits.pc := f3_pc 8035995c9e7SJenius io.toIbuffer.bits.ftqOffset.zipWithIndex.map{case(a, i) => a.bits := i.U; a.valid := checkerOutStage1.fixedTaken(i) && !f3_req_is_mmio} 8042a3050c2SJay io.toIbuffer.bits.foldpc := f3_foldpc 805*88895b11Sxu_zh io.toIbuffer.bits.exceptionType := f3_exception_vec 806d0de7e4aSpeixiaokun io.toIbuffer.bits.crossPageIPFFix := (0 until PredictWidth).map(i => f3_crossPageFault(i) || f3_crossGuestPageFault(i)) 8072a3050c2SJay io.toIbuffer.bits.triggered := f3_triggered 8082a3050c2SJay 8092a3050c2SJay when(f3_lastHalf.valid){ 8105995c9e7SJenius io.toIbuffer.bits.enqEnable := checkerOutStage1.fixedRange.asUInt & f3_instr_valid.asUInt & f3_lastHalf_mask 8112a3050c2SJay io.toIbuffer.bits.valid := f3_lastHalf_mask & f3_instr_valid.asUInt 8122a3050c2SJay } 8132a3050c2SJay 814d7ac23a3SEaston Man /** to backend */ 81591946104Sxu_zh // f3_gpaddr is valid iff gpf is detected 816b5a614b9Sxu_zh io.toBackend.gpaddrMem_wen := f3_toIbuffer_valid && Mux( 817b5a614b9Sxu_zh f3_req_is_mmio, 818*88895b11Sxu_zh mmio_resend_exception === ExceptionType.gpf, 819*88895b11Sxu_zh f3_exception.map(_ === ExceptionType.gpf).reduce(_||_) 820b5a614b9Sxu_zh ) 821d7ac23a3SEaston Man io.toBackend.gpaddrMem_waddr := f3_ftq_req.ftqIdx.value 822b5a614b9Sxu_zh io.toBackend.gpaddrMem_wdata := Mux(f3_req_is_mmio, mmio_resend_gpaddr, f3_gpaddr) 82309c6f1ddSLingrui98 82409c6f1ddSLingrui98 //Write back to Ftq 825a37fbf10SJay val f3_cache_fetch = f3_valid && !(f2_fire && !f2_flush) 826a37fbf10SJay val finishFetchMaskReg = RegNext(f3_cache_fetch) 827a37fbf10SJay 8282a3050c2SJay val mmioFlushWb = Wire(Valid(new PredecodeWritebackBundle)) 8290be662e4SJay val f3_mmio_missOffset = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 830a37fbf10SJay f3_mmio_missOffset.valid := f3_req_is_mmio 8310be662e4SJay f3_mmio_missOffset.bits := 0.U 8320be662e4SJay 8338abe1810SEaston Man // Send mmioFlushWb back to FTQ 1 cycle after uncache fetch return 8348abe1810SEaston Man // When backend redirect, mmio_state reset after 1 cycle. 8358abe1810SEaston Man // In this case, mask .valid to avoid overriding backend redirect 8368abe1810SEaston Man mmioFlushWb.valid := (f3_req_is_mmio && mmio_state === m_waitCommit && RegNext(fromUncache.fire) && 8378abe1810SEaston Man f3_mmio_use_seq_pc && !f3_ftq_flush_self && !f3_ftq_flush_by_older) 8382a3050c2SJay mmioFlushWb.bits.pc := f3_pc 8392a3050c2SJay mmioFlushWb.bits.pd := f3_pd 8402a3050c2SJay mmioFlushWb.bits.pd.zipWithIndex.map{case(instr,i) => instr.valid := f3_mmio_range(i)} 8412a3050c2SJay mmioFlushWb.bits.ftqIdx := f3_ftq_req.ftqIdx 8422a3050c2SJay mmioFlushWb.bits.ftqOffset := f3_ftq_req.ftqOffset.bits 8432a3050c2SJay mmioFlushWb.bits.misOffset := f3_mmio_missOffset 8442a3050c2SJay mmioFlushWb.bits.cfiOffset := DontCare 845ee175d78SJay mmioFlushWb.bits.target := Mux(mmio_is_RVC, f3_ftq_req.startAddr + 2.U , f3_ftq_req.startAddr + 4.U) 8462a3050c2SJay mmioFlushWb.bits.jalTarget := DontCare 8472a3050c2SJay mmioFlushWb.bits.instrRange := f3_mmio_range 84809c6f1ddSLingrui98 8492dfa9e76SJenius /** external predecode for MMIO instruction */ 8502dfa9e76SJenius when(f3_req_is_mmio){ 8512dfa9e76SJenius val inst = Cat(f3_mmio_data(1), f3_mmio_data(0)) 8522dfa9e76SJenius val currentIsRVC = isRVC(inst) 8532dfa9e76SJenius 8542dfa9e76SJenius val brType::isCall::isRet::Nil = brInfo(inst) 8552dfa9e76SJenius val jalOffset = jal_offset(inst, currentIsRVC) 8562dfa9e76SJenius val brOffset = br_offset(inst, currentIsRVC) 8572dfa9e76SJenius 858195ef4a5STang Haojin io.toIbuffer.bits.instrs(0) := new RVCDecoder(inst, XLEN, fLen, useAddiForMv = true).decode.bits 8592dfa9e76SJenius 8602dfa9e76SJenius 8612dfa9e76SJenius io.toIbuffer.bits.pd(0).valid := true.B 8622dfa9e76SJenius io.toIbuffer.bits.pd(0).isRVC := currentIsRVC 8632dfa9e76SJenius io.toIbuffer.bits.pd(0).brType := brType 8642dfa9e76SJenius io.toIbuffer.bits.pd(0).isCall := isCall 8652dfa9e76SJenius io.toIbuffer.bits.pd(0).isRet := isRet 8662dfa9e76SJenius 867*88895b11Sxu_zh io.toIbuffer.bits.exceptionType(0) := mmio_resend_exception 868*88895b11Sxu_zh // resend must be cross-page 869*88895b11Sxu_zh // FIXME: should gpf set crossPageIPFFix to true? See https://github.com/OpenXiangShan/XiangShan/blame/89c99ce9fd7fc54dd7e7521527e6099040868e4c/src/main/scala/xiangshan/frontend/IFU.scala#L822 870*88895b11Sxu_zh io.toIbuffer.bits.crossPageIPFFix(0) := mmio_resend_exception === ExceptionType.pf // || mmio_resend_exception === ExceptionType.gpf 8712dfa9e76SJenius 8722dfa9e76SJenius io.toIbuffer.bits.enqEnable := f3_mmio_range.asUInt 8732dfa9e76SJenius 8742dfa9e76SJenius mmioFlushWb.bits.pd(0).valid := true.B 8752dfa9e76SJenius mmioFlushWb.bits.pd(0).isRVC := currentIsRVC 8762dfa9e76SJenius mmioFlushWb.bits.pd(0).brType := brType 8772dfa9e76SJenius mmioFlushWb.bits.pd(0).isCall := isCall 8782dfa9e76SJenius mmioFlushWb.bits.pd(0).isRet := isRet 8792dfa9e76SJenius } 8802dfa9e76SJenius 881935edac4STang Haojin mmio_redirect := (f3_req_is_mmio && mmio_state === m_waitCommit && RegNext(fromUncache.fire) && f3_mmio_use_seq_pc) 88209c6f1ddSLingrui98 88300240ba6SJay XSPerfAccumulate("fetch_bubble_ibuffer_not_ready", io.toIbuffer.valid && !io.toIbuffer.ready ) 88400240ba6SJay 88500240ba6SJay 88658dbdfc2SJay /** 88758dbdfc2SJay ****************************************************************************** 88858dbdfc2SJay * IFU Write Back Stage 88958dbdfc2SJay * - write back predecode information to Ftq to update 89058dbdfc2SJay * - redirect if found fault prediction 89158dbdfc2SJay * - redirect if has false hit last half (last PC is not start + 32 Bytes, but in the midle of an notCFI RVI instruction) 89258dbdfc2SJay ****************************************************************************** 8932a3050c2SJay */ 8940c70648eSEaston Man val wb_enable = RegNext(f2_fire && !f2_flush) && !f3_req_is_mmio && !f3_flush 8950c70648eSEaston Man val wb_valid = RegNext(wb_enable, init = false.B) 8960c70648eSEaston Man val wb_ftq_req = RegEnable(f3_ftq_req, wb_enable) 89758dbdfc2SJay 8980c70648eSEaston Man val wb_check_result_stage1 = RegEnable(checkerOutStage1, wb_enable) 8995995c9e7SJenius val wb_check_result_stage2 = checkerOutStage2 9000c70648eSEaston Man val wb_instr_range = RegEnable(io.toIbuffer.bits.enqEnable, wb_enable) 901e4d2f6a9Smy-mayfly 902e4d2f6a9Smy-mayfly val wb_pc_lower_result = RegEnable(f3_pc_lower_result, wb_enable) 903e4d2f6a9Smy-mayfly val wb_pc_high = RegEnable(f3_pc_high, wb_enable) 904e4d2f6a9Smy-mayfly val wb_pc_high_plus1 = RegEnable(f3_pc_high_plus1, wb_enable) 905e4d2f6a9Smy-mayfly val wb_pc = CatPC(wb_pc_lower_result, wb_pc_high, wb_pc_high_plus1) 906e4d2f6a9Smy-mayfly 907e4d2f6a9Smy-mayfly //val wb_pc = RegEnable(f3_pc, wb_enable) 9080c70648eSEaston Man val wb_pd = RegEnable(f3_pd, wb_enable) 9090c70648eSEaston Man val wb_instr_valid = RegEnable(f3_instr_valid, wb_enable) 9102a3050c2SJay 9112a3050c2SJay /* false hit lastHalf */ 9120c70648eSEaston Man val wb_lastIdx = RegEnable(f3_last_validIdx, wb_enable) 9130c70648eSEaston Man val wb_false_lastHalf = RegEnable(f3_false_lastHalf, wb_enable) && wb_lastIdx =/= (PredictWidth - 1).U 9140c70648eSEaston Man val wb_false_target = RegEnable(f3_false_snpc, wb_enable) 9152a3050c2SJay 9162a3050c2SJay val wb_half_flush = wb_false_lastHalf 9172a3050c2SJay val wb_half_target = wb_false_target 9182a3050c2SJay 919a1351e5dSJay /* false oversize */ 920a1351e5dSJay val lastIsRVC = wb_instr_range.asTypeOf(Vec(PredictWidth,Bool())).last && wb_pd.last.isRVC 921a1351e5dSJay val lastIsRVI = wb_instr_range.asTypeOf(Vec(PredictWidth,Bool()))(PredictWidth - 2) && !wb_pd(PredictWidth - 2).isRVC 9225995c9e7SJenius val lastTaken = wb_check_result_stage1.fixedTaken.last 923a1351e5dSJay 9242a3050c2SJay f3_wb_not_flush := wb_ftq_req.ftqIdx === f3_ftq_req.ftqIdx && f3_valid && wb_valid 9252a3050c2SJay 9263f785aa3SJenius /** if a req with a last half but miss predicted enters in wb stage, and this cycle f3 stalls, 9273f785aa3SJenius * we set a flag to notify f3 that the last half flag need not to be set. 9283f785aa3SJenius */ 929804985a5SJenius //f3_fire is after wb_valid 930076dea5fSJenius when(wb_valid && RegNext(f3_hasLastHalf,init = false.B) 931251a37e4SJenius && wb_check_result_stage2.fixedMissPred(PredictWidth - 1) && !f3_fire && !RegNext(f3_fire,init = false.B) && !f3_flush 9323f785aa3SJenius ){ 9333f785aa3SJenius f3_lastHalf_disable := true.B 934ab6202e2SJenius } 935ab6202e2SJenius 936804985a5SJenius //wb_valid and f3_fire are in same cycle 937076dea5fSJenius when(wb_valid && RegNext(f3_hasLastHalf,init = false.B) 938076dea5fSJenius && wb_check_result_stage2.fixedMissPred(PredictWidth - 1) && f3_fire 939804985a5SJenius ){ 940804985a5SJenius f3_lastHalf.valid := false.B 941804985a5SJenius } 942804985a5SJenius 9432a3050c2SJay val checkFlushWb = Wire(Valid(new PredecodeWritebackBundle)) 944b665b650STang Haojin val checkFlushWbjalTargetIdx = ParallelPriorityEncoder(VecInit(wb_pd.zip(wb_instr_valid).map{case (pd, v) => v && pd.isJal })) 945b665b650STang Haojin val checkFlushWbTargetIdx = ParallelPriorityEncoder(wb_check_result_stage2.fixedMissPred) 9462a3050c2SJay checkFlushWb.valid := wb_valid 9472a3050c2SJay checkFlushWb.bits.pc := wb_pc 9482a3050c2SJay checkFlushWb.bits.pd := wb_pd 9492a3050c2SJay checkFlushWb.bits.pd.zipWithIndex.map{case(instr,i) => instr.valid := wb_instr_valid(i)} 9502a3050c2SJay checkFlushWb.bits.ftqIdx := wb_ftq_req.ftqIdx 9512a3050c2SJay checkFlushWb.bits.ftqOffset := wb_ftq_req.ftqOffset.bits 9525995c9e7SJenius checkFlushWb.bits.misOffset.valid := ParallelOR(wb_check_result_stage2.fixedMissPred) || wb_half_flush 9535995c9e7SJenius checkFlushWb.bits.misOffset.bits := Mux(wb_half_flush, wb_lastIdx, ParallelPriorityEncoder(wb_check_result_stage2.fixedMissPred)) 9545995c9e7SJenius checkFlushWb.bits.cfiOffset.valid := ParallelOR(wb_check_result_stage1.fixedTaken) 9555995c9e7SJenius checkFlushWb.bits.cfiOffset.bits := ParallelPriorityEncoder(wb_check_result_stage1.fixedTaken) 956b665b650STang Haojin checkFlushWb.bits.target := Mux(wb_half_flush, wb_half_target, wb_check_result_stage2.fixedTarget(checkFlushWbTargetIdx)) 957d10ddd67SGuokai Chen checkFlushWb.bits.jalTarget := wb_check_result_stage2.jalTarget(checkFlushWbjalTargetIdx) 9582a3050c2SJay checkFlushWb.bits.instrRange := wb_instr_range.asTypeOf(Vec(PredictWidth, Bool())) 9592a3050c2SJay 960bccc5520SJenius toFtq.pdWb := Mux(wb_valid, checkFlushWb, mmioFlushWb) 9612a3050c2SJay 9622a3050c2SJay wb_redirect := checkFlushWb.bits.misOffset.valid && wb_valid 96309c6f1ddSLingrui98 9645b3c20f7SJinYue /*write back flush type*/ 9655995c9e7SJenius val checkFaultType = wb_check_result_stage2.faultType 9665b3c20f7SJinYue val checkJalFault = wb_valid && checkFaultType.map(_.isjalFault).reduce(_||_) 9675b3c20f7SJinYue val checkRetFault = wb_valid && checkFaultType.map(_.isRetFault).reduce(_||_) 9685b3c20f7SJinYue val checkTargetFault = wb_valid && checkFaultType.map(_.istargetFault).reduce(_||_) 9695b3c20f7SJinYue val checkNotCFIFault = wb_valid && checkFaultType.map(_.notCFIFault).reduce(_||_) 9705b3c20f7SJinYue val checkInvalidTaken = wb_valid && checkFaultType.map(_.invalidTakenFault).reduce(_||_) 9715b3c20f7SJinYue 9725b3c20f7SJinYue 9735b3c20f7SJinYue XSPerfAccumulate("predecode_flush_jalFault", checkJalFault ) 9745b3c20f7SJinYue XSPerfAccumulate("predecode_flush_retFault", checkRetFault ) 9755b3c20f7SJinYue XSPerfAccumulate("predecode_flush_targetFault", checkTargetFault ) 9765b3c20f7SJinYue XSPerfAccumulate("predecode_flush_notCFIFault", checkNotCFIFault ) 9775b3c20f7SJinYue XSPerfAccumulate("predecode_flush_incalidTakenFault", checkInvalidTaken ) 9785b3c20f7SJinYue 9795b3c20f7SJinYue when(checkRetFault){ 9805b3c20f7SJinYue XSDebug("startAddr:%x nextstartAddr:%x taken:%d takenIdx:%d\n", 9815b3c20f7SJinYue wb_ftq_req.startAddr, wb_ftq_req.nextStartAddr, wb_ftq_req.ftqOffset.valid, wb_ftq_req.ftqOffset.bits) 9825b3c20f7SJinYue } 9835b3c20f7SJinYue 98451532d8bSGuokai Chen 9851d8f4dcbSJay /** performance counter */ 986005e809bSJiuyang Liu val f3_perf_info = RegEnable(f2_perf_info, f2_fire) 987935edac4STang Haojin val f3_req_0 = io.toIbuffer.fire 988935edac4STang Haojin val f3_req_1 = io.toIbuffer.fire && f3_doubleLine 989935edac4STang Haojin val f3_hit_0 = io.toIbuffer.fire && f3_perf_info.bank_hit(0) 990935edac4STang Haojin val f3_hit_1 = io.toIbuffer.fire && f3_doubleLine & f3_perf_info.bank_hit(1) 9911d8f4dcbSJay val f3_hit = f3_perf_info.hit 992cd365d4cSrvcoresjw val perfEvents = Seq( 9932a3050c2SJay ("frontendFlush ", wb_redirect ), 994935edac4STang Haojin ("ifu_req ", io.toIbuffer.fire ), 995935edac4STang Haojin ("ifu_miss ", io.toIbuffer.fire && !f3_perf_info.hit ), 996cd365d4cSrvcoresjw ("ifu_req_cacheline_0 ", f3_req_0 ), 997cd365d4cSrvcoresjw ("ifu_req_cacheline_1 ", f3_req_1 ), 998cd365d4cSrvcoresjw ("ifu_req_cacheline_0_hit ", f3_hit_1 ), 999cd365d4cSrvcoresjw ("ifu_req_cacheline_1_hit ", f3_hit_1 ), 1000935edac4STang Haojin ("only_0_hit ", f3_perf_info.only_0_hit && io.toIbuffer.fire ), 1001935edac4STang Haojin ("only_0_miss ", f3_perf_info.only_0_miss && io.toIbuffer.fire ), 1002935edac4STang Haojin ("hit_0_hit_1 ", f3_perf_info.hit_0_hit_1 && io.toIbuffer.fire ), 1003935edac4STang Haojin ("hit_0_miss_1 ", f3_perf_info.hit_0_miss_1 && io.toIbuffer.fire ), 1004935edac4STang Haojin ("miss_0_hit_1 ", f3_perf_info.miss_0_hit_1 && io.toIbuffer.fire ), 1005935edac4STang Haojin ("miss_0_miss_1 ", f3_perf_info.miss_0_miss_1 && io.toIbuffer.fire ), 1006cd365d4cSrvcoresjw ) 10071ca0e4f3SYinan Xu generatePerfEvent() 100809c6f1ddSLingrui98 1009935edac4STang Haojin XSPerfAccumulate("ifu_req", io.toIbuffer.fire ) 1010935edac4STang Haojin XSPerfAccumulate("ifu_miss", io.toIbuffer.fire && !f3_hit ) 1011f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_0", f3_req_0 ) 1012f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_1", f3_req_1 ) 1013f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_0_hit", f3_hit_0 ) 1014f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_1_hit", f3_hit_1 ) 10152a3050c2SJay XSPerfAccumulate("frontendFlush", wb_redirect ) 1016935edac4STang Haojin XSPerfAccumulate("only_0_hit", f3_perf_info.only_0_hit && io.toIbuffer.fire ) 1017935edac4STang Haojin XSPerfAccumulate("only_0_miss", f3_perf_info.only_0_miss && io.toIbuffer.fire ) 1018935edac4STang Haojin XSPerfAccumulate("hit_0_hit_1", f3_perf_info.hit_0_hit_1 && io.toIbuffer.fire ) 1019935edac4STang Haojin XSPerfAccumulate("hit_0_miss_1", f3_perf_info.hit_0_miss_1 && io.toIbuffer.fire ) 1020935edac4STang Haojin XSPerfAccumulate("miss_0_hit_1", f3_perf_info.miss_0_hit_1 && io.toIbuffer.fire ) 1021935edac4STang Haojin XSPerfAccumulate("miss_0_miss_1", f3_perf_info.miss_0_miss_1 && io.toIbuffer.fire ) 1022935edac4STang Haojin XSPerfAccumulate("hit_0_except_1", f3_perf_info.hit_0_except_1 && io.toIbuffer.fire ) 1023935edac4STang Haojin XSPerfAccumulate("miss_0_except_1", f3_perf_info.miss_0_except_1 && io.toIbuffer.fire ) 1024935edac4STang Haojin XSPerfAccumulate("except_0", f3_perf_info.except_0 && io.toIbuffer.fire ) 1025eb163ef0SHaojin Tang XSPerfHistogram("ifu2ibuffer_validCnt", PopCount(io.toIbuffer.bits.valid & io.toIbuffer.bits.enqEnable), io.toIbuffer.fire, 0, PredictWidth + 1, 1) 102651532d8bSGuokai Chen 1027c686adcdSYinan Xu val hartId = p(XSCoreParamsKey).HartId 1028c686adcdSYinan Xu val isWriteFetchToIBufferTable = Constantin.createRecord(s"isWriteFetchToIBufferTable$hartId") 1029c686adcdSYinan Xu val isWriteIfuWbToFtqTable = Constantin.createRecord(s"isWriteIfuWbToFtqTable$hartId") 1030c686adcdSYinan Xu val fetchToIBufferTable = ChiselDB.createTable(s"FetchToIBuffer$hartId", new FetchToIBufferDB) 1031c686adcdSYinan Xu val ifuWbToFtqTable = ChiselDB.createTable(s"IfuWbToFtq$hartId", new IfuWbToFtqDB) 103251532d8bSGuokai Chen 103351532d8bSGuokai Chen val fetchIBufferDumpData = Wire(new FetchToIBufferDB) 103451532d8bSGuokai Chen fetchIBufferDumpData.start_addr := f3_ftq_req.startAddr 103551532d8bSGuokai Chen fetchIBufferDumpData.instr_count := PopCount(io.toIbuffer.bits.enqEnable) 1036935edac4STang Haojin fetchIBufferDumpData.exception := (f3_perf_info.except_0 && io.toIbuffer.fire) || (f3_perf_info.hit_0_except_1 && io.toIbuffer.fire) || (f3_perf_info.miss_0_except_1 && io.toIbuffer.fire) 103751532d8bSGuokai Chen fetchIBufferDumpData.is_cache_hit := f3_hit 103851532d8bSGuokai Chen 103951532d8bSGuokai Chen val ifuWbToFtqDumpData = Wire(new IfuWbToFtqDB) 104051532d8bSGuokai Chen ifuWbToFtqDumpData.start_addr := wb_ftq_req.startAddr 104151532d8bSGuokai Chen ifuWbToFtqDumpData.is_miss_pred := checkFlushWb.bits.misOffset.valid 104251532d8bSGuokai Chen ifuWbToFtqDumpData.miss_pred_offset := checkFlushWb.bits.misOffset.bits 104351532d8bSGuokai Chen ifuWbToFtqDumpData.checkJalFault := checkJalFault 104451532d8bSGuokai Chen ifuWbToFtqDumpData.checkRetFault := checkRetFault 104551532d8bSGuokai Chen ifuWbToFtqDumpData.checkTargetFault := checkTargetFault 104651532d8bSGuokai Chen ifuWbToFtqDumpData.checkNotCFIFault := checkNotCFIFault 104751532d8bSGuokai Chen ifuWbToFtqDumpData.checkInvalidTaken := checkInvalidTaken 104851532d8bSGuokai Chen 104951532d8bSGuokai Chen fetchToIBufferTable.log( 105051532d8bSGuokai Chen data = fetchIBufferDumpData, 1051da3bf434SMaxpicca-Li en = isWriteFetchToIBufferTable.orR && io.toIbuffer.fire, 105251532d8bSGuokai Chen site = "IFU" + p(XSCoreParamsKey).HartId.toString, 105351532d8bSGuokai Chen clock = clock, 105451532d8bSGuokai Chen reset = reset 105551532d8bSGuokai Chen ) 105651532d8bSGuokai Chen ifuWbToFtqTable.log( 105751532d8bSGuokai Chen data = ifuWbToFtqDumpData, 1058da3bf434SMaxpicca-Li en = isWriteIfuWbToFtqTable.orR && checkFlushWb.valid, 105951532d8bSGuokai Chen site = "IFU" + p(XSCoreParamsKey).HartId.toString, 106051532d8bSGuokai Chen clock = clock, 106151532d8bSGuokai Chen reset = reset 106251532d8bSGuokai Chen ) 106351532d8bSGuokai Chen 106409c6f1ddSLingrui98} 1065