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} 30*ad415ae0SXiaokun-Peiimport xiangshan.backend.GPAMemEntry 313c02ee8fSwakafaimport utility.ChiselDB 3209c6f1ddSLingrui98 3309c6f1ddSLingrui98trait HasInstrMMIOConst extends HasXSParameter with HasIFUConst{ 3409c6f1ddSLingrui98 def mmioBusWidth = 64 3509c6f1ddSLingrui98 def mmioBusBytes = mmioBusWidth / 8 360be662e4SJay def maxInstrLen = 32 3709c6f1ddSLingrui98} 3809c6f1ddSLingrui98 3909c6f1ddSLingrui98trait HasIFUConst extends HasXSParameter{ 401d8f4dcbSJay def addrAlign(addr: UInt, bytes: Int, highest: Int): UInt = Cat(addr(highest-1, log2Ceil(bytes)), 0.U(log2Ceil(bytes).W)) 411d8f4dcbSJay def fetchQueueSize = 2 421d8f4dcbSJay 432a3050c2SJay def getBasicBlockIdx( pc: UInt, start: UInt ): UInt = { 442a3050c2SJay val byteOffset = pc - start 452a3050c2SJay (byteOffset - instBytes.U)(log2Ceil(PredictWidth),instOffsetBits) 461d8f4dcbSJay } 4709c6f1ddSLingrui98} 4809c6f1ddSLingrui98 4909c6f1ddSLingrui98class IfuToFtqIO(implicit p:Parameters) extends XSBundle { 5009c6f1ddSLingrui98 val pdWb = Valid(new PredecodeWritebackBundle) 5109c6f1ddSLingrui98} 5209c6f1ddSLingrui98 53d7ac23a3SEaston Manclass IfuToBackendIO(implicit p:Parameters) extends XSBundle { 54d7ac23a3SEaston Man // write to backend gpaddr mem 55d7ac23a3SEaston Man val gpaddrMem_wen = Output(Bool()) 56d7ac23a3SEaston Man val gpaddrMem_waddr = Output(UInt(log2Ceil(FtqSize).W)) // Ftq Ptr 57d7ac23a3SEaston Man // 2 gpaddrs, correspond to startAddr & nextLineAddr in bundle FtqICacheInfo 58d7ac23a3SEaston Man // TODO: avoid cross page entry in Ftq 59*ad415ae0SXiaokun-Pei val gpaddrMem_wdata = Output(new GPAMemEntry) 60d7ac23a3SEaston Man} 61d7ac23a3SEaston Man 6209c6f1ddSLingrui98class FtqInterface(implicit p: Parameters) extends XSBundle { 6309c6f1ddSLingrui98 val fromFtq = Flipped(new FtqToIfuIO) 6409c6f1ddSLingrui98 val toFtq = new IfuToFtqIO 6509c6f1ddSLingrui98} 6609c6f1ddSLingrui98 670be662e4SJayclass UncacheInterface(implicit p: Parameters) extends XSBundle { 680be662e4SJay val fromUncache = Flipped(DecoupledIO(new InsUncacheResp)) 690be662e4SJay val toUncache = DecoupledIO( new InsUncacheReq ) 700be662e4SJay} 711d1e6d4dSJenius 7209c6f1ddSLingrui98class NewIFUIO(implicit p: Parameters) extends XSBundle { 7309c6f1ddSLingrui98 val ftqInter = new FtqInterface 7450780602SJenius val icacheInter = Flipped(new IFUICacheIO) 751d8f4dcbSJay val icacheStop = Output(Bool()) 761d8f4dcbSJay val icachePerfInfo = Input(new ICachePerfInfo) 7709c6f1ddSLingrui98 val toIbuffer = Decoupled(new FetchToIBuffer) 78d7ac23a3SEaston Man val toBackend = new IfuToBackendIO 790be662e4SJay val uncacheInter = new UncacheInterface 8072951335SLi Qianruo val frontendTrigger = Flipped(new FrontendTdataDistributeIO) 81a37fbf10SJay val rob_commits = Flipped(Vec(CommitWidth, Valid(new RobCommitInfo))) 82f1fe8698SLemover val iTLBInter = new TlbRequestIO 8356788a33SJinYue val pmp = new ICachePMPBundle 841d1e6d4dSJenius val mmioCommitRead = new mmioCommitRead 8509c6f1ddSLingrui98} 8609c6f1ddSLingrui98 8709c6f1ddSLingrui98// record the situation in which fallThruAddr falls into 8809c6f1ddSLingrui98// the middle of an RVI inst 8909c6f1ddSLingrui98class LastHalfInfo(implicit p: Parameters) extends XSBundle { 9009c6f1ddSLingrui98 val valid = Bool() 9109c6f1ddSLingrui98 val middlePC = UInt(VAddrBits.W) 9209c6f1ddSLingrui98 def matchThisBlock(startAddr: UInt) = valid && middlePC === startAddr 9309c6f1ddSLingrui98} 9409c6f1ddSLingrui98 9509c6f1ddSLingrui98class IfuToPreDecode(implicit p: Parameters) extends XSBundle { 9609c6f1ddSLingrui98 val data = if(HasCExtension) Vec(PredictWidth + 1, UInt(16.W)) else Vec(PredictWidth, UInt(32.W)) 9772951335SLi Qianruo val frontendTrigger = new FrontendTdataDistributeIO 982a3050c2SJay val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 9909c6f1ddSLingrui98} 10009c6f1ddSLingrui98 1012a3050c2SJay 1022a3050c2SJayclass IfuToPredChecker(implicit p: Parameters) extends XSBundle { 1032a3050c2SJay val ftqOffset = Valid(UInt(log2Ceil(PredictWidth).W)) 1042a3050c2SJay val jumpOffset = Vec(PredictWidth, UInt(XLEN.W)) 1052a3050c2SJay val target = UInt(VAddrBits.W) 1062a3050c2SJay val instrRange = Vec(PredictWidth, Bool()) 1072a3050c2SJay val instrValid = Vec(PredictWidth, Bool()) 1082a3050c2SJay val pds = Vec(PredictWidth, new PreDecodeInfo) 1092a3050c2SJay val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 1100c70648eSEaston Man val fire_in = Bool() 1112a3050c2SJay} 1122a3050c2SJay 11351532d8bSGuokai Chenclass FetchToIBufferDB extends Bundle { 11451532d8bSGuokai Chen val start_addr = UInt(39.W) 11551532d8bSGuokai Chen val instr_count = UInt(32.W) 11651532d8bSGuokai Chen val exception = Bool() 11751532d8bSGuokai Chen val is_cache_hit = Bool() 11851532d8bSGuokai Chen} 11951532d8bSGuokai Chen 12051532d8bSGuokai Chenclass IfuWbToFtqDB extends Bundle { 12151532d8bSGuokai Chen val start_addr = UInt(39.W) 12251532d8bSGuokai Chen val is_miss_pred = Bool() 12351532d8bSGuokai Chen val miss_pred_offset = UInt(32.W) 12451532d8bSGuokai Chen val checkJalFault = Bool() 12551532d8bSGuokai Chen val checkRetFault = Bool() 12651532d8bSGuokai Chen val checkTargetFault = Bool() 12751532d8bSGuokai Chen val checkNotCFIFault = Bool() 12851532d8bSGuokai Chen val checkInvalidTaken = Bool() 12951532d8bSGuokai Chen} 13051532d8bSGuokai Chen 1312a3050c2SJayclass NewIFU(implicit p: Parameters) extends XSModule 1322a3050c2SJay with HasICacheParameters 133aeedc8eeSGuokai Chen with HasXSParameter 1342a3050c2SJay with HasIFUConst 1352a3050c2SJay with HasPdConst 136167bcd01SJay with HasCircularQueuePtrHelper 1372a3050c2SJay with HasPerfEvents 13821ae6bc4Speixiaokun with HasTlbConst 13909c6f1ddSLingrui98{ 14009c6f1ddSLingrui98 val io = IO(new NewIFUIO) 14109c6f1ddSLingrui98 val (toFtq, fromFtq) = (io.ftqInter.toFtq, io.ftqInter.fromFtq) 142c5c5edaeSJenius val fromICache = io.icacheInter.resp 1430be662e4SJay val (toUncache, fromUncache) = (io.uncacheInter.toUncache , io.uncacheInter.fromUncache) 14409c6f1ddSLingrui98 14509c6f1ddSLingrui98 def isCrossLineReq(start: UInt, end: UInt): Bool = start(blockOffBits) ^ end(blockOffBits) 14609c6f1ddSLingrui98 147d2b20d1aSTang Haojin def numOfStage = 3 148e4d2f6a9Smy-mayfly // equal lower_result overflow bit 149e4d2f6a9Smy-mayfly def PcCutPoint = (VAddrBits/4) - 1 150e4d2f6a9Smy-mayfly def CatPC(low: UInt, high: UInt, high1: UInt): UInt = { 151e4d2f6a9Smy-mayfly Mux( 152e4d2f6a9Smy-mayfly low(PcCutPoint), 153e4d2f6a9Smy-mayfly Cat(high1, low(PcCutPoint-1, 0)), 154e4d2f6a9Smy-mayfly Cat(high, low(PcCutPoint-1, 0)) 155e4d2f6a9Smy-mayfly ) 156e4d2f6a9Smy-mayfly } 157e4d2f6a9Smy-mayfly def CatPC(lowVec: Vec[UInt], high: UInt, high1: UInt): Vec[UInt] = VecInit(lowVec.map(CatPC(_, high, high1))) 158d2b20d1aSTang Haojin require(numOfStage > 1, "BPU numOfStage must be greater than 1") 159d2b20d1aSTang Haojin val topdown_stages = RegInit(VecInit(Seq.fill(numOfStage)(0.U.asTypeOf(new FrontendTopDownBundle)))) 160d2b20d1aSTang Haojin // bubble events in IFU, only happen in stage 1 161d2b20d1aSTang Haojin val icacheMissBubble = Wire(Bool()) 162d2b20d1aSTang Haojin val itlbMissBubble =Wire(Bool()) 163d2b20d1aSTang Haojin 164d2b20d1aSTang Haojin // only driven by clock, not valid-ready 165d2b20d1aSTang Haojin topdown_stages(0) := fromFtq.req.bits.topdown_info 166d2b20d1aSTang Haojin for (i <- 1 until numOfStage) { 167d2b20d1aSTang Haojin topdown_stages(i) := topdown_stages(i - 1) 168d2b20d1aSTang Haojin } 169d2b20d1aSTang Haojin when (icacheMissBubble) { 170d2b20d1aSTang Haojin topdown_stages(1).reasons(TopDownCounters.ICacheMissBubble.id) := true.B 171d2b20d1aSTang Haojin } 172d2b20d1aSTang Haojin when (itlbMissBubble) { 173d2b20d1aSTang Haojin topdown_stages(1).reasons(TopDownCounters.ITLBMissBubble.id) := true.B 174d2b20d1aSTang Haojin } 175d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info := topdown_stages(numOfStage - 1) 176d2b20d1aSTang Haojin when (fromFtq.topdown_redirect.valid) { 177d2b20d1aSTang Haojin // only redirect from backend, IFU redirect itself is handled elsewhere 178d2b20d1aSTang Haojin when (fromFtq.topdown_redirect.bits.debugIsCtrl) { 179d2b20d1aSTang Haojin /* 180d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 181d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.ControlRedirectBubble.id) := true.B 182d2b20d1aSTang Haojin } 183d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.ControlRedirectBubble.id) := true.B 184d2b20d1aSTang Haojin */ 185d2b20d1aSTang Haojin when (fromFtq.topdown_redirect.bits.ControlBTBMissBubble) { 186d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 187d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.BTBMissBubble.id) := true.B 188d2b20d1aSTang Haojin } 189d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.BTBMissBubble.id) := true.B 190d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.TAGEMissBubble) { 191d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 192d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.TAGEMissBubble.id) := true.B 193d2b20d1aSTang Haojin } 194d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.TAGEMissBubble.id) := true.B 195d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.SCMissBubble) { 196d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 197d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.SCMissBubble.id) := true.B 198d2b20d1aSTang Haojin } 199d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.SCMissBubble.id) := true.B 200d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.ITTAGEMissBubble) { 201d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 202d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 203d2b20d1aSTang Haojin } 204d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 205d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.RASMissBubble) { 206d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 207d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.RASMissBubble.id) := true.B 208d2b20d1aSTang Haojin } 209d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.RASMissBubble.id) := true.B 210d2b20d1aSTang Haojin } 211d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.debugIsMemVio) { 212d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 213d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 214d2b20d1aSTang Haojin } 215d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 216d2b20d1aSTang Haojin } .otherwise { 217d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 218d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 219d2b20d1aSTang Haojin } 220d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 221d2b20d1aSTang Haojin } 222d2b20d1aSTang Haojin } 223d2b20d1aSTang Haojin 2241d8f4dcbSJay class TlbExept(implicit p: Parameters) extends XSBundle{ 2251d8f4dcbSJay val pageFault = Bool() 2261d8f4dcbSJay val accessFault = Bool() 2271d8f4dcbSJay val mmio = Bool() 228b005f7c6SJay } 22909c6f1ddSLingrui98 230a61a35e0Sssszwic val preDecoder = Module(new PreDecode) 231dc270d3bSJenius 2322a3050c2SJay val predChecker = Module(new PredChecker) 2332a3050c2SJay val frontendTrigger = Module(new FrontendTrigger) 2345995c9e7SJenius val (checkerIn, checkerOutStage1, checkerOutStage2) = (predChecker.io.in, predChecker.io.out.stage1Out,predChecker.io.out.stage2Out) 2351d8f4dcbSJay 23658dbdfc2SJay /** 23758dbdfc2SJay ****************************************************************************** 23858dbdfc2SJay * IFU Stage 0 23958dbdfc2SJay * - send cacheline fetch request to ICacheMainPipe 24058dbdfc2SJay ****************************************************************************** 24158dbdfc2SJay */ 24209c6f1ddSLingrui98 24309c6f1ddSLingrui98 val f0_valid = fromFtq.req.valid 24409c6f1ddSLingrui98 val f0_ftq_req = fromFtq.req.bits 2456ce52296SJinYue val f0_doubleLine = fromFtq.req.bits.crossCacheline 24634a88126SJinYue val f0_vSetIdx = VecInit(get_idx((f0_ftq_req.startAddr)), get_idx(f0_ftq_req.nextlineStart)) 247935edac4STang Haojin val f0_fire = fromFtq.req.fire 24809c6f1ddSLingrui98 24909c6f1ddSLingrui98 val f0_flush, f1_flush, f2_flush, f3_flush = WireInit(false.B) 25009c6f1ddSLingrui98 val from_bpu_f0_flush, from_bpu_f1_flush, from_bpu_f2_flush, from_bpu_f3_flush = WireInit(false.B) 25109c6f1ddSLingrui98 252cb4f77ceSLingrui98 from_bpu_f0_flush := fromFtq.flushFromBpu.shouldFlushByStage2(f0_ftq_req.ftqIdx) || 253cb4f77ceSLingrui98 fromFtq.flushFromBpu.shouldFlushByStage3(f0_ftq_req.ftqIdx) 25409c6f1ddSLingrui98 2552a3050c2SJay val wb_redirect , mmio_redirect, backend_redirect= WireInit(false.B) 2562a3050c2SJay val f3_wb_not_flush = WireInit(false.B) 2572a3050c2SJay 2582a3050c2SJay backend_redirect := fromFtq.redirect.valid 2592a3050c2SJay f3_flush := backend_redirect || (wb_redirect && !f3_wb_not_flush) 2602a3050c2SJay f2_flush := backend_redirect || mmio_redirect || wb_redirect 26109c6f1ddSLingrui98 f1_flush := f2_flush || from_bpu_f1_flush 26209c6f1ddSLingrui98 f0_flush := f1_flush || from_bpu_f0_flush 26309c6f1ddSLingrui98 26409c6f1ddSLingrui98 val f1_ready, f2_ready, f3_ready = WireInit(false.B) 26509c6f1ddSLingrui98 26650780602SJenius fromFtq.req.ready := f1_ready && io.icacheInter.icacheReady 26709c6f1ddSLingrui98 268d2b20d1aSTang Haojin 269d2b20d1aSTang Haojin when (wb_redirect) { 270d2b20d1aSTang Haojin when (f3_wb_not_flush) { 271d2b20d1aSTang Haojin topdown_stages(2).reasons(TopDownCounters.BTBMissBubble.id) := true.B 272d2b20d1aSTang Haojin } 273d2b20d1aSTang Haojin for (i <- 0 until numOfStage - 1) { 274d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.BTBMissBubble.id) := true.B 275d2b20d1aSTang Haojin } 276d2b20d1aSTang Haojin } 277d2b20d1aSTang Haojin 27858dbdfc2SJay /** <PERF> f0 fetch bubble */ 279f7c29b0aSJinYue 28000240ba6SJay XSPerfAccumulate("fetch_bubble_ftq_not_valid", !fromFtq.req.valid && fromFtq.req.ready ) 281c5c5edaeSJenius // XSPerfAccumulate("fetch_bubble_pipe_stall", f0_valid && toICache(0).ready && toICache(1).ready && !f1_ready ) 282c5c5edaeSJenius // XSPerfAccumulate("fetch_bubble_icache_0_busy", f0_valid && !toICache(0).ready ) 283c5c5edaeSJenius // XSPerfAccumulate("fetch_bubble_icache_1_busy", f0_valid && !toICache(1).ready ) 28400240ba6SJay XSPerfAccumulate("fetch_flush_backend_redirect", backend_redirect ) 28500240ba6SJay XSPerfAccumulate("fetch_flush_wb_redirect", wb_redirect ) 28600240ba6SJay XSPerfAccumulate("fetch_flush_bpu_f1_flush", from_bpu_f1_flush ) 28700240ba6SJay XSPerfAccumulate("fetch_flush_bpu_f0_flush", from_bpu_f0_flush ) 28858dbdfc2SJay 28958dbdfc2SJay 29058dbdfc2SJay /** 29158dbdfc2SJay ****************************************************************************** 29258dbdfc2SJay * IFU Stage 1 29358dbdfc2SJay * - calculate pc/half_pc/cut_ptr for every instruction 29458dbdfc2SJay ****************************************************************************** 29558dbdfc2SJay */ 29609c6f1ddSLingrui98 29709c6f1ddSLingrui98 val f1_valid = RegInit(false.B) 298005e809bSJiuyang Liu val f1_ftq_req = RegEnable(f0_ftq_req, f0_fire) 299005e809bSJiuyang Liu // val f1_situation = RegEnable(f0_situation, f0_fire) 300005e809bSJiuyang Liu val f1_doubleLine = RegEnable(f0_doubleLine, f0_fire) 301005e809bSJiuyang Liu val f1_vSetIdx = RegEnable(f0_vSetIdx, f0_fire) 302625ecd17SJenius val f1_fire = f1_valid && f2_ready 30309c6f1ddSLingrui98 304625ecd17SJenius f1_ready := f1_fire || !f1_valid 30509c6f1ddSLingrui98 3060d756c48SJinYue from_bpu_f1_flush := fromFtq.flushFromBpu.shouldFlushByStage3(f1_ftq_req.ftqIdx) && f1_valid 307cb4f77ceSLingrui98 // from_bpu_f1_flush := false.B 30809c6f1ddSLingrui98 30909c6f1ddSLingrui98 when(f1_flush) {f1_valid := false.B} 31009c6f1ddSLingrui98 .elsewhen(f0_fire && !f0_flush) {f1_valid := true.B} 31109c6f1ddSLingrui98 .elsewhen(f1_fire) {f1_valid := false.B} 31209c6f1ddSLingrui98 313e4d2f6a9Smy-mayfly val f1_pc_high = f1_ftq_req.startAddr(VAddrBits-1, PcCutPoint) 314f2f493deSstride val f1_pc_high_plus1 = f1_pc_high + 1.U 315f2f493deSstride 316e4d2f6a9Smy-mayfly /** 317e4d2f6a9Smy-mayfly * In order to reduce power consumption, avoid calculating the full PC value in the first level. 318e4d2f6a9Smy-mayfly * code of original logic, this code has been deprecated 319e4d2f6a9Smy-mayfly * val f1_pc = VecInit(f1_pc_lower_result.map{ i => 320e4d2f6a9Smy-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)))}) 321e4d2f6a9Smy-mayfly */ 322e4d2f6a9Smy-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 323f2f493deSstride 324e4d2f6a9Smy-mayfly val f1_pc = CatPC(f1_pc_lower_result, f1_pc_high, f1_pc_high_plus1) 325e4d2f6a9Smy-mayfly 326e4d2f6a9Smy-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 327e4d2f6a9Smy-mayfly val f1_half_snpc = CatPC(f1_half_snpc_lower_result, f1_pc_high, f1_pc_high_plus1) 328f2f493deSstride 329f2f493deSstride if (env.FPGAPlatform){ 330f2f493deSstride val f1_pc_diff = VecInit((0 until PredictWidth).map(i => f1_ftq_req.startAddr + (i * 2).U)) 331f2f493deSstride val f1_half_snpc_diff = VecInit((0 until PredictWidth).map(i => f1_ftq_req.startAddr + ((i+2) * 2).U)) 332f2f493deSstride 333f2f493deSstride XSError(f1_pc.zip(f1_pc_diff).map{ case (a,b) => a.asUInt =/= b.asUInt }.reduce(_||_), "f1_half_snpc adder cut fail") 334f2f493deSstride XSError(f1_half_snpc.zip(f1_half_snpc_diff).map{ case (a,b) => a.asUInt =/= b.asUInt }.reduce(_||_), "f1_half_snpc adder cut fail") 335f2f493deSstride } 336f2f493deSstride 337b92f8445Sssszwic 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 )) 338b92f8445Sssszwic else VecInit((0 until PredictWidth).map(i => Cat(0.U(2.W), f1_ftq_req.startAddr(blockOffBits-1, 2)) + i.U )) 33909c6f1ddSLingrui98 34058dbdfc2SJay /** 34158dbdfc2SJay ****************************************************************************** 34258dbdfc2SJay * IFU Stage 2 34358dbdfc2SJay * - icache response data (latched for pipeline stop) 34458dbdfc2SJay * - generate exceprion bits for every instruciton (page fault/access fault/mmio) 34558dbdfc2SJay * - generate predicted instruction range (1 means this instruciton is in this fetch packet) 34658dbdfc2SJay * - cut data from cachlines to packet instruction code 34758dbdfc2SJay * - instruction predecode and RVC expand 34858dbdfc2SJay ****************************************************************************** 34958dbdfc2SJay */ 35058dbdfc2SJay 3511d8f4dcbSJay val icacheRespAllValid = WireInit(false.B) 35209c6f1ddSLingrui98 35309c6f1ddSLingrui98 val f2_valid = RegInit(false.B) 354005e809bSJiuyang Liu val f2_ftq_req = RegEnable(f1_ftq_req, f1_fire) 355005e809bSJiuyang Liu // val f2_situation = RegEnable(f1_situation, f1_fire) 356005e809bSJiuyang Liu val f2_doubleLine = RegEnable(f1_doubleLine, f1_fire) 357005e809bSJiuyang Liu val f2_vSetIdx = RegEnable(f1_vSetIdx, f1_fire) 358625ecd17SJenius val f2_fire = f2_valid && f3_ready && icacheRespAllValid 3591d8f4dcbSJay 360625ecd17SJenius f2_ready := f2_fire || !f2_valid 3611d8f4dcbSJay //TODO: addr compare may be timing critical 36234a88126SJinYue 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) 3631d8f4dcbSJay val f2_icache_all_resp_reg = RegInit(false.B) 3641d8f4dcbSJay 3651d8f4dcbSJay icacheRespAllValid := f2_icache_all_resp_reg || f2_icache_all_resp_wire 3661d8f4dcbSJay 367d2b20d1aSTang Haojin icacheMissBubble := io.icacheInter.topdownIcacheMiss 368d2b20d1aSTang Haojin itlbMissBubble := io.icacheInter.topdownItlbMiss 369d2b20d1aSTang Haojin 3701d8f4dcbSJay io.icacheStop := !f3_ready 3711d8f4dcbSJay 3721d8f4dcbSJay when(f2_flush) {f2_icache_all_resp_reg := false.B} 3731d8f4dcbSJay .elsewhen(f2_valid && f2_icache_all_resp_wire && !f3_ready) {f2_icache_all_resp_reg := true.B} 3741d8f4dcbSJay .elsewhen(f2_fire && f2_icache_all_resp_reg) {f2_icache_all_resp_reg := false.B} 37509c6f1ddSLingrui98 37609c6f1ddSLingrui98 when(f2_flush) {f2_valid := false.B} 37709c6f1ddSLingrui98 .elsewhen(f1_fire && !f1_flush) {f2_valid := true.B } 37809c6f1ddSLingrui98 .elsewhen(f2_fire) {f2_valid := false.B} 37909c6f1ddSLingrui98 38088895b11Sxu_zh val f2_exception = VecInit((0 until PortNumber).map(i => fromICache(i).bits.exception)) 381c1b28b66STang Haojin val f2_except_fromBackend = fromICache(0).bits.exceptionFromBackend 382d7ac23a3SEaston Man // paddr and gpaddr of [startAddr, nextLineAddr] 383d7ac23a3SEaston Man val f2_paddrs = VecInit((0 until PortNumber).map(i => fromICache(i).bits.paddr)) 38491946104Sxu_zh val f2_gpaddr = fromICache(0).bits.gpaddr 385*ad415ae0SXiaokun-Pei val f2_isForVSnonLeafPTE = fromICache(0).bits.isForVSnonLeafPTE 386002c10a4SYanqin Li 387002c10a4SYanqin Li // FIXME: what if port 0 is not mmio, but port 1 is? 38888895b11Sxu_zh // cancel mmio fetch if exception occurs 389002c10a4SYanqin Li val f2_mmio = f2_exception(0) === ExceptionType.none && ( 390002c10a4SYanqin Li fromICache(0).bits.pmp_mmio || 391002c10a4SYanqin Li // currently, we do not distinguish between Pbmt.nc and Pbmt.io 392002c10a4SYanqin Li // anyway, they are both non-cacheable, and should be handled with mmio fsm and sent to Uncache module 393002c10a4SYanqin Li Pbmt.isUncache(fromICache(0).bits.itlb_pbmt) 394002c10a4SYanqin Li ) 395002c10a4SYanqin Li 3960be662e4SJay 397e4d2f6a9Smy-mayfly /** 398e4d2f6a9Smy-mayfly * reduce the number of registers, origin code 399e4d2f6a9Smy-mayfly * f2_pc = RegEnable(f1_pc, f1_fire) 400e4d2f6a9Smy-mayfly */ 401e4d2f6a9Smy-mayfly val f2_pc_lower_result = RegEnable(f1_pc_lower_result, f1_fire) 402e4d2f6a9Smy-mayfly val f2_pc_high = RegEnable(f1_pc_high, f1_fire) 403e4d2f6a9Smy-mayfly val f2_pc_high_plus1 = RegEnable(f1_pc_high_plus1, f1_fire) 404e4d2f6a9Smy-mayfly val f2_pc = CatPC(f2_pc_lower_result, f2_pc_high, f2_pc_high_plus1) 405a37fbf10SJay 406e4d2f6a9Smy-mayfly val f2_cut_ptr = RegEnable(f1_cut_ptr, f1_fire) 407005e809bSJiuyang Liu val f2_resend_vaddr = RegEnable(f1_ftq_req.startAddr + 2.U, f1_fire) 4082a3050c2SJay 4092a3050c2SJay def isNextLine(pc: UInt, startAddr: UInt) = { 4102a3050c2SJay startAddr(blockOffBits) ^ pc(blockOffBits) 411b6982e83SLemover } 41209c6f1ddSLingrui98 4132a3050c2SJay def isLastInLine(pc: UInt) = { 4142a3050c2SJay pc(blockOffBits - 1, 0) === "b111110".U 41509c6f1ddSLingrui98 } 41609c6f1ddSLingrui98 4172a3050c2SJay val f2_foldpc = VecInit(f2_pc.map(i => XORFold(i(VAddrBits-1,1), MemPredPCWidth))) 4182a3050c2SJay val f2_jump_range = Fill(PredictWidth, !f2_ftq_req.ftqOffset.valid) | Fill(PredictWidth, 1.U(1.W)) >> ~f2_ftq_req.ftqOffset.bits 4191d011975SJinYue 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) 4202a3050c2SJay val f2_instr_range = f2_jump_range & f2_ftr_range 42188895b11Sxu_zh val f2_exception_vec = VecInit((0 until PredictWidth).map( i => MuxCase(ExceptionType.none, Seq( 42288895b11Sxu_zh !isNextLine(f2_pc(i), f2_ftq_req.startAddr) -> f2_exception(0), 42388895b11Sxu_zh (isNextLine(f2_pc(i), f2_ftq_req.startAddr) && f2_doubleLine) -> f2_exception(1) 42488895b11Sxu_zh )))) 4251d8f4dcbSJay val f2_perf_info = io.icachePerfInfo 42609c6f1ddSLingrui98 4272a3050c2SJay def cut(cacheline: UInt, cutPtr: Vec[UInt]) : Vec[UInt] ={ 428d558bd61SJenius require(HasCExtension) 429d558bd61SJenius // if(HasCExtension){ 43009c6f1ddSLingrui98 val result = Wire(Vec(PredictWidth + 1, UInt(16.W))) 431b92f8445Sssszwic val dataVec = cacheline.asTypeOf(Vec(blockBytes, UInt(16.W))) //32 16-bit data vector 43209c6f1ddSLingrui98 (0 until PredictWidth + 1).foreach( i => 433d558bd61SJenius result(i) := dataVec(cutPtr(i)) //the max ptr is 3*blockBytes/4-1 43409c6f1ddSLingrui98 ) 43509c6f1ddSLingrui98 result 436d558bd61SJenius // } else { 437d558bd61SJenius // val result = Wire(Vec(PredictWidth, UInt(32.W)) ) 438d558bd61SJenius // val dataVec = cacheline.asTypeOf(Vec(blockBytes * 2/ 4, UInt(32.W))) 439d558bd61SJenius // (0 until PredictWidth).foreach( i => 440d558bd61SJenius // result(i) := dataVec(cutPtr(i)) 441d558bd61SJenius // ) 442d558bd61SJenius // result 443d558bd61SJenius // } 44409c6f1ddSLingrui98 } 44509c6f1ddSLingrui98 446a61a35e0Sssszwic val f2_cache_response_data = fromICache.map(_.bits.data) 447b92f8445Sssszwic val f2_data_2_cacheline = Cat(f2_cache_response_data(0), f2_cache_response_data(0)) 448dc270d3bSJenius 449a61a35e0Sssszwic val f2_cut_data = cut(f2_data_2_cacheline, f2_cut_ptr) 45009c6f1ddSLingrui98 45158dbdfc2SJay /** predecode (include RVC expander) */ 452dc270d3bSJenius // preDecoderRegIn.data := f2_reg_cut_data 453dc270d3bSJenius // preDecoderRegInIn.frontendTrigger := io.frontendTrigger 454dc270d3bSJenius // preDecoderRegInIn.csrTriggerEnable := io.csrTriggerEnable 455dc270d3bSJenius // preDecoderRegIn.pc := f2_pc 456dc270d3bSJenius 457a61a35e0Sssszwic val preDecoderIn = preDecoder.io.in 4589afa8a47STang Haojin preDecoderIn.valid := f2_valid 4599afa8a47STang Haojin preDecoderIn.bits.data := f2_cut_data 4609afa8a47STang Haojin preDecoderIn.bits.frontendTrigger := io.frontendTrigger 4619afa8a47STang Haojin preDecoderIn.bits.pc := f2_pc 462a61a35e0Sssszwic val preDecoderOut = preDecoder.io.out 46309c6f1ddSLingrui98 46448a62719SJenius //val f2_expd_instr = preDecoderOut.expInstr 46548a62719SJenius val f2_instr = preDecoderOut.instr 4662a3050c2SJay val f2_pd = preDecoderOut.pd 4672a3050c2SJay val f2_jump_offset = preDecoderOut.jumpOffset 4682a3050c2SJay val f2_hasHalfValid = preDecoderOut.hasHalfValid 469a2568a60Sxu_zh /* if there is a cross-page RVI instruction, and the former page has no exception, 470a2568a60Sxu_zh * whether it has exception is actually depends on the latter page 471a2568a60Sxu_zh */ 472a2568a60Sxu_zh val f2_crossPage_exception_vec = VecInit((0 until PredictWidth).map { i => Mux( 473a2568a60Sxu_zh isLastInLine(f2_pc(i)) && !f2_pd(i).isRVC && f2_doubleLine && f2_exception(0) === ExceptionType.none, 474a2568a60Sxu_zh f2_exception(1), 475a2568a60Sxu_zh ExceptionType.none 476a2568a60Sxu_zh )}) 47700240ba6SJay XSPerfAccumulate("fetch_bubble_icache_not_resp", f2_valid && !icacheRespAllValid ) 47800240ba6SJay 47909c6f1ddSLingrui98 48058dbdfc2SJay /** 48158dbdfc2SJay ****************************************************************************** 48258dbdfc2SJay * IFU Stage 3 48358dbdfc2SJay * - handle MMIO instruciton 48458dbdfc2SJay * -send request to Uncache fetch Unit 48558dbdfc2SJay * -every packet include 1 MMIO instruction 48658dbdfc2SJay * -MMIO instructions will stop fetch pipeline until commiting from RoB 48758dbdfc2SJay * -flush to snpc (send ifu_redirect to Ftq) 48858dbdfc2SJay * - Ibuffer enqueue 48958dbdfc2SJay * - check predict result in Frontend (jalFault/retFault/notCFIFault/invalidTakenFault/targetFault) 49058dbdfc2SJay * - handle last half RVI instruction 49158dbdfc2SJay ****************************************************************************** 49258dbdfc2SJay */ 49358dbdfc2SJay 49492c61038SXuan Hu val expanders = Seq.fill(PredictWidth)(Module(new RVCExpander)) 49592c61038SXuan Hu 49609c6f1ddSLingrui98 val f3_valid = RegInit(false.B) 497005e809bSJiuyang Liu val f3_ftq_req = RegEnable(f2_ftq_req, f2_fire) 498005e809bSJiuyang Liu // val f3_situation = RegEnable(f2_situation, f2_fire) 499005e809bSJiuyang Liu val f3_doubleLine = RegEnable(f2_doubleLine, f2_fire) 500935edac4STang Haojin val f3_fire = io.toIbuffer.fire 5011d8f4dcbSJay 502a61a35e0Sssszwic val f3_cut_data = RegEnable(f2_cut_data, f2_fire) 5031d8f4dcbSJay 50488895b11Sxu_zh val f3_exception = RegEnable(f2_exception, f2_fire) 505005e809bSJiuyang Liu val f3_mmio = RegEnable(f2_mmio, f2_fire) 506c1b28b66STang Haojin val f3_except_fromBackend = RegEnable(f2_except_fromBackend, f2_fire) 50709c6f1ddSLingrui98 508935edac4STang Haojin val f3_instr = RegEnable(f2_instr, f2_fire) 509aeedc8eeSGuokai Chen 51092c61038SXuan Hu expanders.zipWithIndex.foreach { case (expander, i) => 51192c61038SXuan Hu expander.io.in := f3_instr(i) 51292c61038SXuan Hu } 51392c61038SXuan Hu // Use expanded instruction only when input is legal. 51492c61038SXuan Hu // Otherwise use origin illegal RVC instruction. 51592c61038SXuan Hu val f3_expd_instr = VecInit(expanders.map { expander: RVCExpander => 51692c61038SXuan Hu Mux(expander.io.ill, expander.io.in, expander.io.out.bits) 51792c61038SXuan Hu }) 51892c61038SXuan Hu val f3_ill = VecInit(expanders.map(_.io.ill)) 51948a62719SJenius 520935edac4STang Haojin val f3_pd_wire = RegEnable(f2_pd, f2_fire) 521330aad7fSGuokai Chen val f3_pd = WireInit(f3_pd_wire) 522935edac4STang Haojin val f3_jump_offset = RegEnable(f2_jump_offset, f2_fire) 52388895b11Sxu_zh val f3_exception_vec = RegEnable(f2_exception_vec, f2_fire) 524a2568a60Sxu_zh val f3_crossPage_exception_vec = RegEnable(f2_crossPage_exception_vec, f2_fire) 525e4d2f6a9Smy-mayfly 526e4d2f6a9Smy-mayfly val f3_pc_lower_result = RegEnable(f2_pc_lower_result, f2_fire) 527e4d2f6a9Smy-mayfly val f3_pc_high = RegEnable(f2_pc_high, f2_fire) 528e4d2f6a9Smy-mayfly val f3_pc_high_plus1 = RegEnable(f2_pc_high_plus1, f2_fire) 529e4d2f6a9Smy-mayfly val f3_pc = CatPC(f3_pc_lower_result, f3_pc_high, f3_pc_high_plus1) 530e4d2f6a9Smy-mayfly 531e4d2f6a9Smy-mayfly val f3_pc_last_lower_result_plus2 = RegEnable(f2_pc_lower_result(PredictWidth - 1) + 2.U, f2_fire) 532e4d2f6a9Smy-mayfly val f3_pc_last_lower_result_plus4 = RegEnable(f2_pc_lower_result(PredictWidth - 1) + 4.U, f2_fire) 533e4d2f6a9Smy-mayfly //val f3_half_snpc = RegEnable(f2_half_snpc, f2_fire) 534e4d2f6a9Smy-mayfly 535e4d2f6a9Smy-mayfly /** 536e4d2f6a9Smy-mayfly *********************************************************************** 537e4d2f6a9Smy-mayfly * Half snpc(i) is larger than pc(i) by 4. Using pc to calculate half snpc may be a good choice. 538e4d2f6a9Smy-mayfly *********************************************************************** 539e4d2f6a9Smy-mayfly */ 540e4d2f6a9Smy-mayfly val f3_half_snpc = Wire(Vec(PredictWidth,UInt(VAddrBits.W))) 541e4d2f6a9Smy-mayfly for(i <- 0 until PredictWidth){ 542e4d2f6a9Smy-mayfly if(i == (PredictWidth - 2)){ 543e4d2f6a9Smy-mayfly f3_half_snpc(i) := CatPC(f3_pc_last_lower_result_plus2, f3_pc_high, f3_pc_high_plus1) 544e4d2f6a9Smy-mayfly } else if (i == (PredictWidth - 1)){ 545e4d2f6a9Smy-mayfly f3_half_snpc(i) := CatPC(f3_pc_last_lower_result_plus4, f3_pc_high, f3_pc_high_plus1) 546e4d2f6a9Smy-mayfly } else { 547e4d2f6a9Smy-mayfly f3_half_snpc(i) := f3_pc(i+2) 548e4d2f6a9Smy-mayfly } 549e4d2f6a9Smy-mayfly } 550e4d2f6a9Smy-mayfly 551935edac4STang Haojin val f3_instr_range = RegEnable(f2_instr_range, f2_fire) 552935edac4STang Haojin val f3_foldpc = RegEnable(f2_foldpc, f2_fire) 553935edac4STang Haojin val f3_hasHalfValid = RegEnable(f2_hasHalfValid, f2_fire) 554d7ac23a3SEaston Man val f3_paddrs = RegEnable(f2_paddrs, f2_fire) 55591946104Sxu_zh val f3_gpaddr = RegEnable(f2_gpaddr, f2_fire) 556*ad415ae0SXiaokun-Pei val f3_isForVSnonLeafPTE = RegEnable(f2_isForVSnonLeafPTE, f2_fire) 557005e809bSJiuyang Liu val f3_resend_vaddr = RegEnable(f2_resend_vaddr, f2_fire) 558ee175d78SJay 559cb6e5d3cSssszwic // Expand 1 bit to prevent overflow when assert 560cb6e5d3cSssszwic val f3_ftq_req_startAddr = Cat(0.U(1.W), f3_ftq_req.startAddr) 561cb6e5d3cSssszwic val f3_ftq_req_nextStartAddr = Cat(0.U(1.W), f3_ftq_req.nextStartAddr) 562330aad7fSGuokai Chen // brType, isCall and isRet generation is delayed to f3 stage 563330aad7fSGuokai Chen val f3Predecoder = Module(new F3Predecoder) 564330aad7fSGuokai Chen 565330aad7fSGuokai Chen f3Predecoder.io.in.instr := f3_instr 566330aad7fSGuokai Chen 567330aad7fSGuokai Chen f3_pd.zipWithIndex.map{ case (pd,i) => 568330aad7fSGuokai Chen pd.brType := f3Predecoder.io.out.pd(i).brType 569330aad7fSGuokai Chen pd.isCall := f3Predecoder.io.out.pd(i).isCall 570330aad7fSGuokai Chen pd.isRet := f3Predecoder.io.out.pd(i).isRet 571330aad7fSGuokai Chen } 572330aad7fSGuokai Chen 573330aad7fSGuokai Chen val f3PdDiff = f3_pd_wire.zip(f3_pd).map{ case (a,b) => a.asUInt =/= b.asUInt }.reduce(_||_) 574330aad7fSGuokai Chen XSError(f3_valid && f3PdDiff, "f3 pd diff") 575330aad7fSGuokai Chen 5761d011975SJinYue when(f3_valid && !f3_ftq_req.ftqOffset.valid){ 577cb6e5d3cSssszwic assert(f3_ftq_req_startAddr + (2*PredictWidth).U >= f3_ftq_req_nextStartAddr, s"More tha ${2*PredictWidth} Bytes fetch is not allowed!") 5781d011975SJinYue } 579a1351e5dSJay 5802a3050c2SJay /*** MMIO State Machine***/ 581ee175d78SJay val f3_mmio_data = Reg(Vec(2, UInt(16.W))) 582ee175d78SJay val mmio_is_RVC = RegInit(false.B) 583ee175d78SJay val mmio_resend_addr = RegInit(0.U(PAddrBits.W)) 58488895b11Sxu_zh val mmio_resend_exception = RegInit(0.U(ExceptionType.width.W)) 585b5a614b9Sxu_zh val mmio_resend_gpaddr = RegInit(0.U(GPAddrBits.W)) 586*ad415ae0SXiaokun-Pei val mmio_resend_isForVSnonLeafPTE = RegInit(false.B) 587c3b2d83aSJay 5881d1e6d4dSJenius //last instuction finish 5891d1e6d4dSJenius val is_first_instr = RegInit(true.B) 590ba5ba1dcSmy-mayfly /*** Determine whether the MMIO instruction is executable based on the previous prediction block ***/ 591ba5ba1dcSmy-mayfly io.mmioCommitRead.mmioFtqPtr := RegNext(f3_ftq_req.ftqIdx - 1.U) 592a37fbf10SJay 5931d1e6d4dSJenius 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) 594ee175d78SJay val mmio_state = RegInit(m_idle) 595a37fbf10SJay 5969bae7d6eSJay val f3_req_is_mmio = f3_mmio && f3_valid 5972a3050c2SJay 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 598ee175d78SJay val f3_mmio_req_commit = f3_req_is_mmio && mmio_state === m_commited 599a37fbf10SJay 600ee175d78SJay val f3_mmio_to_commit = f3_req_is_mmio && mmio_state === m_waitCommit 601a37fbf10SJay val f3_mmio_to_commit_next = RegNext(f3_mmio_to_commit) 602a37fbf10SJay val f3_mmio_can_go = f3_mmio_to_commit && !f3_mmio_to_commit_next 603a37fbf10SJay 6040c70648eSEaston Man val fromFtqRedirectReg = Wire(fromFtq.redirect.cloneType) 6050c70648eSEaston Man fromFtqRedirectReg.bits := RegEnable(fromFtq.redirect.bits, 0.U.asTypeOf(fromFtq.redirect.bits), fromFtq.redirect.valid) 6060c70648eSEaston Man fromFtqRedirectReg.valid := RegNext(fromFtq.redirect.valid, init = false.B) 6074a74a727SJenius val mmioF3Flush = RegNext(f3_flush,init = false.B) 60856788a33SJinYue val f3_ftq_flush_self = fromFtqRedirectReg.valid && RedirectLevel.flushItself(fromFtqRedirectReg.bits.level) 60956788a33SJinYue val f3_ftq_flush_by_older = fromFtqRedirectReg.valid && isBefore(fromFtqRedirectReg.bits.ftqIdx, f3_ftq_req.ftqIdx) 6109bae7d6eSJay 61156788a33SJinYue val f3_need_not_flush = f3_req_is_mmio && fromFtqRedirectReg.valid && !f3_ftq_flush_self && !f3_ftq_flush_by_older 6129bae7d6eSJay 613ba5ba1dcSmy-mayfly /** 614ba5ba1dcSmy-mayfly ********************************************************************************** 615ba5ba1dcSmy-mayfly * We want to defer instruction fetching when encountering MMIO instructions to ensure that the MMIO region is not negatively impacted. 616ba5ba1dcSmy-mayfly * This is the exception when the first instruction is an MMIO instruction. 617ba5ba1dcSmy-mayfly ********************************************************************************** 618ba5ba1dcSmy-mayfly */ 619ba5ba1dcSmy-mayfly when(is_first_instr && f3_fire){ 6201d1e6d4dSJenius is_first_instr := false.B 6211d1e6d4dSJenius } 6221d1e6d4dSJenius 6234a74a727SJenius when(f3_flush && !f3_req_is_mmio) {f3_valid := false.B} 6244a74a727SJenius .elsewhen(mmioF3Flush && f3_req_is_mmio && !f3_need_not_flush) {f3_valid := false.B} 625a37fbf10SJay .elsewhen(f2_fire && !f2_flush ) {f3_valid := true.B } 626935edac4STang Haojin .elsewhen(io.toIbuffer.fire && !f3_req_is_mmio) {f3_valid := false.B} 627a37fbf10SJay .elsewhen{f3_req_is_mmio && f3_mmio_req_commit} {f3_valid := false.B} 628a37fbf10SJay 629a37fbf10SJay val f3_mmio_use_seq_pc = RegInit(false.B) 630a37fbf10SJay 63156788a33SJinYue val (redirect_ftqIdx, redirect_ftqOffset) = (fromFtqRedirectReg.bits.ftqIdx,fromFtqRedirectReg.bits.ftqOffset) 63256788a33SJinYue val redirect_mmio_req = fromFtqRedirectReg.valid && redirect_ftqIdx === f3_ftq_req.ftqIdx && redirect_ftqOffset === 0.U 633a37fbf10SJay 634a37fbf10SJay when(RegNext(f2_fire && !f2_flush) && f3_req_is_mmio) { f3_mmio_use_seq_pc := true.B } 635a37fbf10SJay .elsewhen(redirect_mmio_req) { f3_mmio_use_seq_pc := false.B } 636a37fbf10SJay 6378c192ff7Sxu_zh f3_ready := (io.toIbuffer.ready && (f3_mmio_req_commit || !f3_req_is_mmio)) || !f3_valid 638a37fbf10SJay 6391d1e6d4dSJenius // mmio state machine 640a37fbf10SJay switch(mmio_state){ 641ee175d78SJay is(m_idle){ 6429bae7d6eSJay when(f3_req_is_mmio){ 6431d1e6d4dSJenius mmio_state := m_waitLastCmt 6441d1e6d4dSJenius } 6451d1e6d4dSJenius } 6461d1e6d4dSJenius 6471d1e6d4dSJenius is(m_waitLastCmt){ 6481d1e6d4dSJenius when(is_first_instr){ 649ee175d78SJay mmio_state := m_sendReq 6501d1e6d4dSJenius }.otherwise{ 6511d1e6d4dSJenius mmio_state := Mux(io.mmioCommitRead.mmioLastCommit, m_sendReq, m_waitLastCmt) 652a37fbf10SJay } 653a37fbf10SJay } 654a37fbf10SJay 655ee175d78SJay is(m_sendReq){ 656935edac4STang Haojin mmio_state := Mux(toUncache.fire, m_waitResp, m_sendReq) 657a37fbf10SJay } 658a37fbf10SJay 659ee175d78SJay is(m_waitResp){ 660935edac4STang Haojin when(fromUncache.fire){ 661a37fbf10SJay val isRVC = fromUncache.bits.data(1,0) =/= 3.U 662d7ac23a3SEaston Man val needResend = !isRVC && f3_paddrs(0)(2,1) === 3.U 663ee175d78SJay mmio_state := Mux(needResend, m_sendTLB, m_waitCommit) 664ee175d78SJay mmio_is_RVC := isRVC 665ee175d78SJay f3_mmio_data(0) := fromUncache.bits.data(15,0) 666ee175d78SJay f3_mmio_data(1) := fromUncache.bits.data(31,16) 667a37fbf10SJay } 668a37fbf10SJay } 669a37fbf10SJay 670ee175d78SJay is(m_sendTLB){ 6717b7232f9Sxu_zh mmio_state := Mux(io.iTLBInter.req.fire, m_tlbResp, m_sendTLB) 672c3b2d83aSJay } 673a37fbf10SJay 674ee175d78SJay is(m_tlbResp){ 6757b7232f9Sxu_zh when(io.iTLBInter.resp.fire) { 6767b7232f9Sxu_zh // we are using a blocked tlb, so resp.fire must have !resp.bits.miss 6777b7232f9Sxu_zh assert(!io.iTLBInter.resp.bits.miss, "blocked mode iTLB miss when resp.fire") 67888895b11Sxu_zh val tlb_exception = ExceptionType.fromTlbResp(io.iTLBInter.resp.bits) 6797b7232f9Sxu_zh // if tlb has exception, abort checking pmp, just send instr & exception to ibuffer and wait for commit 68088895b11Sxu_zh mmio_state := Mux(tlb_exception === ExceptionType.none, m_sendPMP, m_waitCommit) 6817b7232f9Sxu_zh // also save itlb response 68203efd994Shappy-lx mmio_resend_addr := io.iTLBInter.resp.bits.paddr(0) 68388895b11Sxu_zh mmio_resend_exception := tlb_exception 684b5a614b9Sxu_zh mmio_resend_gpaddr := io.iTLBInter.resp.bits.gpaddr(0) 685*ad415ae0SXiaokun-Pei mmio_resend_isForVSnonLeafPTE := io.iTLBInter.resp.bits.isForVSnonLeafPTE(0) 686ee175d78SJay } 6877b7232f9Sxu_zh } 688ee175d78SJay 689ee175d78SJay is(m_sendPMP){ 69088895b11Sxu_zh // if pmp re-check does not respond mmio, must be access fault 69188895b11Sxu_zh val pmp_exception = Mux(io.pmp.resp.mmio, ExceptionType.fromPMPResp(io.pmp.resp), ExceptionType.af) 69288895b11Sxu_zh // if pmp has exception, abort sending request, just send instr & exception to ibuffer and wait for commit 69388895b11Sxu_zh mmio_state := Mux(pmp_exception === ExceptionType.none, m_resendReq, m_waitCommit) 69488895b11Sxu_zh // also save pmp response 69588895b11Sxu_zh mmio_resend_exception := pmp_exception 696ee175d78SJay } 697ee175d78SJay 698ee175d78SJay is(m_resendReq){ 699935edac4STang Haojin mmio_state := Mux(toUncache.fire, m_waitResendResp, m_resendReq) 700ee175d78SJay } 701ee175d78SJay 702ee175d78SJay is(m_waitResendResp) { 703935edac4STang Haojin when(fromUncache.fire) { 704ee175d78SJay mmio_state := m_waitCommit 705ee175d78SJay f3_mmio_data(1) := fromUncache.bits.data(15,0) 706a37fbf10SJay } 707a37fbf10SJay } 708a37fbf10SJay 709ee175d78SJay is(m_waitCommit) { 7107b7232f9Sxu_zh mmio_state := Mux(mmio_commit, m_commited, m_waitCommit) 711a37fbf10SJay } 7122a3050c2SJay 713ee175d78SJay //normal mmio instruction 714ee175d78SJay is(m_commited) { 715ee175d78SJay mmio_state := m_idle 716ee175d78SJay mmio_is_RVC := false.B 717ee175d78SJay mmio_resend_addr := 0.U 71888895b11Sxu_zh mmio_resend_exception := ExceptionType.none 719b5a614b9Sxu_zh mmio_resend_gpaddr := 0.U 720*ad415ae0SXiaokun-Pei mmio_resend_isForVSnonLeafPTE := false.B 7212a3050c2SJay } 722a37fbf10SJay } 723a37fbf10SJay 7248abe1810SEaston Man // Exception or flush by older branch prediction 7258abe1810SEaston Man // Condition is from RegNext(fromFtq.redirect), 1 cycle after backend rediect 726167bcd01SJay when(f3_ftq_flush_self || f3_ftq_flush_by_older) { 727ee175d78SJay mmio_state := m_idle 728ee175d78SJay mmio_is_RVC := false.B 729ee175d78SJay mmio_resend_addr := 0.U 73088895b11Sxu_zh mmio_resend_exception := ExceptionType.none 731b5a614b9Sxu_zh mmio_resend_gpaddr := 0.U 732*ad415ae0SXiaokun-Pei mmio_resend_isForVSnonLeafPTE := false.B 733ee175d78SJay f3_mmio_data.map(_ := 0.U) 7349bae7d6eSJay } 7359bae7d6eSJay 736ee175d78SJay toUncache.valid := ((mmio_state === m_sendReq) || (mmio_state === m_resendReq)) && f3_req_is_mmio 737d7ac23a3SEaston Man toUncache.bits.addr := Mux((mmio_state === m_resendReq), mmio_resend_addr, f3_paddrs(0)) 738a37fbf10SJay fromUncache.ready := true.B 739a37fbf10SJay 7407b7232f9Sxu_zh // send itlb request in m_sendTLB state 741ee175d78SJay io.iTLBInter.req.valid := (mmio_state === m_sendTLB) && f3_req_is_mmio 742ee175d78SJay io.iTLBInter.req.bits.size := 3.U 743ee175d78SJay io.iTLBInter.req.bits.vaddr := f3_resend_vaddr 744ee175d78SJay io.iTLBInter.req.bits.debug.pc := f3_resend_vaddr 7457b7232f9Sxu_zh io.iTLBInter.req.bits.cmd := TlbCmd.exec 7467b7232f9Sxu_zh io.iTLBInter.req.bits.kill := false.B // IFU use itlb for mmio, doesn't need sync, set it to false 7477b7232f9Sxu_zh io.iTLBInter.req.bits.no_translate := false.B 748db6cfb5aSHaoyuan Feng io.iTLBInter.req.bits.fullva := 0.U 749db6cfb5aSHaoyuan Feng io.iTLBInter.req.bits.checkfullva := false.B 750d0de7e4aSpeixiaokun io.iTLBInter.req.bits.hyperinst := DontCare 751d0de7e4aSpeixiaokun io.iTLBInter.req.bits.hlvx := DontCare 7528744445eSMaxpicca-Li io.iTLBInter.req.bits.memidx := DontCare 753f1fe8698SLemover io.iTLBInter.req.bits.debug.robIdx := DontCare 754ee175d78SJay io.iTLBInter.req.bits.debug.isFirstIssue := DontCare 755149a2326Sweiding liu io.iTLBInter.req.bits.pmp_addr := DontCare 7567b7232f9Sxu_zh // whats the difference between req_kill and req.bits.kill? 7577b7232f9Sxu_zh io.iTLBInter.req_kill := false.B 7587b7232f9Sxu_zh // wait for itlb response in m_tlbResp state 7597b7232f9Sxu_zh io.iTLBInter.resp.ready := (mmio_state === m_tlbResp) && f3_req_is_mmio 760ee175d78SJay 761ee175d78SJay io.pmp.req.valid := (mmio_state === m_sendPMP) && f3_req_is_mmio 762ee175d78SJay io.pmp.req.bits.addr := mmio_resend_addr 763ee175d78SJay io.pmp.req.bits.size := 3.U 764ee175d78SJay io.pmp.req.bits.cmd := TlbCmd.exec 765f7c29b0aSJinYue 7662a3050c2SJay val f3_lastHalf = RegInit(0.U.asTypeOf(new LastHalfInfo)) 76709c6f1ddSLingrui98 76809c6f1ddSLingrui98 val f3_predecode_range = VecInit(preDecoderOut.pd.map(inst => inst.valid)).asUInt 7690be662e4SJay val f3_mmio_range = VecInit((0 until PredictWidth).map(i => if(i ==0) true.B else false.B)) 7702a3050c2SJay val f3_instr_valid = Wire(Vec(PredictWidth, Bool())) 77109c6f1ddSLingrui98 7722a3050c2SJay /*** prediction result check ***/ 7732a3050c2SJay checkerIn.ftqOffset := f3_ftq_req.ftqOffset 7742a3050c2SJay checkerIn.jumpOffset := f3_jump_offset 7756ce52296SJinYue checkerIn.target := f3_ftq_req.nextStartAddr 7762a3050c2SJay checkerIn.instrRange := f3_instr_range.asTypeOf(Vec(PredictWidth, Bool())) 7772a3050c2SJay checkerIn.instrValid := f3_instr_valid.asTypeOf(Vec(PredictWidth, Bool())) 7782a3050c2SJay checkerIn.pds := f3_pd 7792a3050c2SJay checkerIn.pc := f3_pc 7800c70648eSEaston Man checkerIn.fire_in := RegNext(f2_fire, init = false.B) 7812a3050c2SJay 78258dbdfc2SJay /*** handle half RVI in the last 2 Bytes ***/ 7832a3050c2SJay 7842a3050c2SJay def hasLastHalf(idx: UInt) = { 7855995c9e7SJenius //!f3_pd(idx).isRVC && checkerOutStage1.fixedRange(idx) && f3_instr_valid(idx) && !checkerOutStage1.fixedTaken(idx) && !checkerOutStage2.fixedMissPred(idx) && ! f3_req_is_mmio 7865995c9e7SJenius !f3_pd(idx).isRVC && checkerOutStage1.fixedRange(idx) && f3_instr_valid(idx) && !checkerOutStage1.fixedTaken(idx) && ! f3_req_is_mmio 7872a3050c2SJay } 7882a3050c2SJay 789b665b650STang Haojin val f3_last_validIdx = ParallelPosteriorityEncoder(checkerOutStage1.fixedRange) 7902a3050c2SJay 7912a3050c2SJay val f3_hasLastHalf = hasLastHalf((PredictWidth - 1).U) 7922a3050c2SJay val f3_false_lastHalf = hasLastHalf(f3_last_validIdx) 7932a3050c2SJay val f3_false_snpc = f3_half_snpc(f3_last_validIdx) 7942a3050c2SJay 795935edac4STang Haojin val f3_lastHalf_mask = VecInit((0 until PredictWidth).map( i => if(i ==0) false.B else true.B )).asUInt 7963f785aa3SJenius val f3_lastHalf_disable = RegInit(false.B) 7972a3050c2SJay 798804985a5SJenius when(f3_flush || (f3_fire && f3_lastHalf_disable)){ 799804985a5SJenius f3_lastHalf_disable := false.B 800804985a5SJenius } 801804985a5SJenius 8022a3050c2SJay when (f3_flush) { 8032a3050c2SJay f3_lastHalf.valid := false.B 8042a3050c2SJay }.elsewhen (f3_fire) { 8053f785aa3SJenius f3_lastHalf.valid := f3_hasLastHalf && !f3_lastHalf_disable 8066ce52296SJinYue f3_lastHalf.middlePC := f3_ftq_req.nextStartAddr 8072a3050c2SJay } 8082a3050c2SJay 8092a3050c2SJay f3_instr_valid := Mux(f3_lastHalf.valid,f3_hasHalfValid ,VecInit(f3_pd.map(inst => inst.valid))) 8102a3050c2SJay 8112a3050c2SJay /*** frontend Trigger ***/ 8122a3050c2SJay frontendTrigger.io.pds := f3_pd 8132a3050c2SJay frontendTrigger.io.pc := f3_pc 8142a3050c2SJay frontendTrigger.io.data := f3_cut_data 8152a3050c2SJay 8162a3050c2SJay frontendTrigger.io.frontendTrigger := io.frontendTrigger 8172a3050c2SJay 8182a3050c2SJay val f3_triggered = frontendTrigger.io.triggered 81991946104Sxu_zh val f3_toIbuffer_valid = f3_valid && (!f3_req_is_mmio || f3_mmio_can_go) && !f3_flush 8202a3050c2SJay 8212a3050c2SJay /*** send to Ibuffer ***/ 82291946104Sxu_zh io.toIbuffer.valid := f3_toIbuffer_valid 8232a3050c2SJay io.toIbuffer.bits.instrs := f3_expd_instr 8242a3050c2SJay io.toIbuffer.bits.valid := f3_instr_valid.asUInt 8255995c9e7SJenius io.toIbuffer.bits.enqEnable := checkerOutStage1.fixedRange.asUInt & f3_instr_valid.asUInt 8262a3050c2SJay io.toIbuffer.bits.pd := f3_pd 82709c6f1ddSLingrui98 io.toIbuffer.bits.ftqPtr := f3_ftq_req.ftqIdx 8282a3050c2SJay io.toIbuffer.bits.pc := f3_pc 8295995c9e7SJenius io.toIbuffer.bits.ftqOffset.zipWithIndex.map{case(a, i) => a.bits := i.U; a.valid := checkerOutStage1.fixedTaken(i) && !f3_req_is_mmio} 8302a3050c2SJay io.toIbuffer.bits.foldpc := f3_foldpc 831a2568a60Sxu_zh io.toIbuffer.bits.exceptionType := ExceptionType.merge(f3_exception_vec, f3_crossPage_exception_vec) 832c1b28b66STang Haojin // exceptionFromBackend only needs to be set for the first instruction. 833c1b28b66STang Haojin // Other instructions in the same block may have pf or af set, 834c1b28b66STang Haojin // which is a side effect of the first instruction and actually not necessary. 835c1b28b66STang Haojin io.toIbuffer.bits.exceptionFromBackend := (0 until PredictWidth).map { 836c1b28b66STang Haojin case 0 => f3_except_fromBackend 837c1b28b66STang Haojin case _ => false.B 838c1b28b66STang Haojin } 839a2568a60Sxu_zh io.toIbuffer.bits.crossPageIPFFix := f3_crossPage_exception_vec.map(_ =/= ExceptionType.none) 84092c61038SXuan Hu io.toIbuffer.bits.illegalInstr:= f3_ill 8412a3050c2SJay io.toIbuffer.bits.triggered := f3_triggered 8422a3050c2SJay 8432a3050c2SJay when(f3_lastHalf.valid){ 8445995c9e7SJenius io.toIbuffer.bits.enqEnable := checkerOutStage1.fixedRange.asUInt & f3_instr_valid.asUInt & f3_lastHalf_mask 8452a3050c2SJay io.toIbuffer.bits.valid := f3_lastHalf_mask & f3_instr_valid.asUInt 8462a3050c2SJay } 8472a3050c2SJay 848d7ac23a3SEaston Man /** to backend */ 84991946104Sxu_zh // f3_gpaddr is valid iff gpf is detected 850b5a614b9Sxu_zh io.toBackend.gpaddrMem_wen := f3_toIbuffer_valid && Mux( 851b5a614b9Sxu_zh f3_req_is_mmio, 85288895b11Sxu_zh mmio_resend_exception === ExceptionType.gpf, 85388895b11Sxu_zh f3_exception.map(_ === ExceptionType.gpf).reduce(_||_) 854b5a614b9Sxu_zh ) 855d7ac23a3SEaston Man io.toBackend.gpaddrMem_waddr := f3_ftq_req.ftqIdx.value 856*ad415ae0SXiaokun-Pei io.toBackend.gpaddrMem_wdata.gpaddr := Mux(f3_req_is_mmio, mmio_resend_gpaddr, f3_gpaddr) 857*ad415ae0SXiaokun-Pei io.toBackend.gpaddrMem_wdata.isForVSnonLeafPTE := Mux(f3_req_is_mmio, mmio_resend_isForVSnonLeafPTE, f3_isForVSnonLeafPTE) 85809c6f1ddSLingrui98 85909c6f1ddSLingrui98 //Write back to Ftq 860a37fbf10SJay val f3_cache_fetch = f3_valid && !(f2_fire && !f2_flush) 861a37fbf10SJay val finishFetchMaskReg = RegNext(f3_cache_fetch) 862a37fbf10SJay 8632a3050c2SJay val mmioFlushWb = Wire(Valid(new PredecodeWritebackBundle)) 8640be662e4SJay val f3_mmio_missOffset = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 865a37fbf10SJay f3_mmio_missOffset.valid := f3_req_is_mmio 8660be662e4SJay f3_mmio_missOffset.bits := 0.U 8670be662e4SJay 8688abe1810SEaston Man // Send mmioFlushWb back to FTQ 1 cycle after uncache fetch return 8698abe1810SEaston Man // When backend redirect, mmio_state reset after 1 cycle. 8708abe1810SEaston Man // In this case, mask .valid to avoid overriding backend redirect 8718abe1810SEaston Man mmioFlushWb.valid := (f3_req_is_mmio && mmio_state === m_waitCommit && RegNext(fromUncache.fire) && 8728abe1810SEaston Man f3_mmio_use_seq_pc && !f3_ftq_flush_self && !f3_ftq_flush_by_older) 8732a3050c2SJay mmioFlushWb.bits.pc := f3_pc 8742a3050c2SJay mmioFlushWb.bits.pd := f3_pd 8752a3050c2SJay mmioFlushWb.bits.pd.zipWithIndex.map{case(instr,i) => instr.valid := f3_mmio_range(i)} 8762a3050c2SJay mmioFlushWb.bits.ftqIdx := f3_ftq_req.ftqIdx 8772a3050c2SJay mmioFlushWb.bits.ftqOffset := f3_ftq_req.ftqOffset.bits 8782a3050c2SJay mmioFlushWb.bits.misOffset := f3_mmio_missOffset 8792a3050c2SJay mmioFlushWb.bits.cfiOffset := DontCare 880ee175d78SJay mmioFlushWb.bits.target := Mux(mmio_is_RVC, f3_ftq_req.startAddr + 2.U , f3_ftq_req.startAddr + 4.U) 8812a3050c2SJay mmioFlushWb.bits.jalTarget := DontCare 8822a3050c2SJay mmioFlushWb.bits.instrRange := f3_mmio_range 88309c6f1ddSLingrui98 88473e96011SXuan Hu val mmioRVCExpander = Module(new RVCExpander) 88573e96011SXuan Hu mmioRVCExpander.io.in := Mux(f3_req_is_mmio, Cat(f3_mmio_data(1), f3_mmio_data(0)), 0.U) 88673e96011SXuan Hu 8872dfa9e76SJenius /** external predecode for MMIO instruction */ 8882dfa9e76SJenius when(f3_req_is_mmio){ 8892dfa9e76SJenius val inst = Cat(f3_mmio_data(1), f3_mmio_data(0)) 8902dfa9e76SJenius val currentIsRVC = isRVC(inst) 8912dfa9e76SJenius 8922dfa9e76SJenius val brType::isCall::isRet::Nil = brInfo(inst) 8932dfa9e76SJenius val jalOffset = jal_offset(inst, currentIsRVC) 8942dfa9e76SJenius val brOffset = br_offset(inst, currentIsRVC) 8952dfa9e76SJenius 89673e96011SXuan Hu io.toIbuffer.bits.instrs(0) := Mux(mmioRVCExpander.io.ill, mmioRVCExpander.io.in, mmioRVCExpander.io.out.bits) 8972dfa9e76SJenius 8982dfa9e76SJenius io.toIbuffer.bits.pd(0).valid := true.B 8992dfa9e76SJenius io.toIbuffer.bits.pd(0).isRVC := currentIsRVC 9002dfa9e76SJenius io.toIbuffer.bits.pd(0).brType := brType 9012dfa9e76SJenius io.toIbuffer.bits.pd(0).isCall := isCall 9022dfa9e76SJenius io.toIbuffer.bits.pd(0).isRet := isRet 9032dfa9e76SJenius 90488895b11Sxu_zh io.toIbuffer.bits.exceptionType(0) := mmio_resend_exception 905a2568a60Sxu_zh io.toIbuffer.bits.crossPageIPFFix(0) := mmio_resend_exception =/= ExceptionType.none 90673e96011SXuan Hu io.toIbuffer.bits.illegalInstr(0) := mmioRVCExpander.io.ill 9072dfa9e76SJenius 9082dfa9e76SJenius io.toIbuffer.bits.enqEnable := f3_mmio_range.asUInt 9092dfa9e76SJenius 9102dfa9e76SJenius mmioFlushWb.bits.pd(0).valid := true.B 9112dfa9e76SJenius mmioFlushWb.bits.pd(0).isRVC := currentIsRVC 9122dfa9e76SJenius mmioFlushWb.bits.pd(0).brType := brType 9132dfa9e76SJenius mmioFlushWb.bits.pd(0).isCall := isCall 9142dfa9e76SJenius mmioFlushWb.bits.pd(0).isRet := isRet 9152dfa9e76SJenius } 9162dfa9e76SJenius 917935edac4STang Haojin mmio_redirect := (f3_req_is_mmio && mmio_state === m_waitCommit && RegNext(fromUncache.fire) && f3_mmio_use_seq_pc) 91809c6f1ddSLingrui98 91900240ba6SJay XSPerfAccumulate("fetch_bubble_ibuffer_not_ready", io.toIbuffer.valid && !io.toIbuffer.ready ) 92000240ba6SJay 92100240ba6SJay 92258dbdfc2SJay /** 92358dbdfc2SJay ****************************************************************************** 92458dbdfc2SJay * IFU Write Back Stage 92558dbdfc2SJay * - write back predecode information to Ftq to update 92658dbdfc2SJay * - redirect if found fault prediction 92758dbdfc2SJay * - redirect if has false hit last half (last PC is not start + 32 Bytes, but in the midle of an notCFI RVI instruction) 92858dbdfc2SJay ****************************************************************************** 9292a3050c2SJay */ 9300c70648eSEaston Man val wb_enable = RegNext(f2_fire && !f2_flush) && !f3_req_is_mmio && !f3_flush 9310c70648eSEaston Man val wb_valid = RegNext(wb_enable, init = false.B) 9320c70648eSEaston Man val wb_ftq_req = RegEnable(f3_ftq_req, wb_enable) 93358dbdfc2SJay 9340c70648eSEaston Man val wb_check_result_stage1 = RegEnable(checkerOutStage1, wb_enable) 9355995c9e7SJenius val wb_check_result_stage2 = checkerOutStage2 9360c70648eSEaston Man val wb_instr_range = RegEnable(io.toIbuffer.bits.enqEnable, wb_enable) 937e4d2f6a9Smy-mayfly 938e4d2f6a9Smy-mayfly val wb_pc_lower_result = RegEnable(f3_pc_lower_result, wb_enable) 939e4d2f6a9Smy-mayfly val wb_pc_high = RegEnable(f3_pc_high, wb_enable) 940e4d2f6a9Smy-mayfly val wb_pc_high_plus1 = RegEnable(f3_pc_high_plus1, wb_enable) 941e4d2f6a9Smy-mayfly val wb_pc = CatPC(wb_pc_lower_result, wb_pc_high, wb_pc_high_plus1) 942e4d2f6a9Smy-mayfly 943e4d2f6a9Smy-mayfly //val wb_pc = RegEnable(f3_pc, wb_enable) 9440c70648eSEaston Man val wb_pd = RegEnable(f3_pd, wb_enable) 9450c70648eSEaston Man val wb_instr_valid = RegEnable(f3_instr_valid, wb_enable) 9462a3050c2SJay 9472a3050c2SJay /* false hit lastHalf */ 9480c70648eSEaston Man val wb_lastIdx = RegEnable(f3_last_validIdx, wb_enable) 9490c70648eSEaston Man val wb_false_lastHalf = RegEnable(f3_false_lastHalf, wb_enable) && wb_lastIdx =/= (PredictWidth - 1).U 9500c70648eSEaston Man val wb_false_target = RegEnable(f3_false_snpc, wb_enable) 9512a3050c2SJay 9522a3050c2SJay val wb_half_flush = wb_false_lastHalf 9532a3050c2SJay val wb_half_target = wb_false_target 9542a3050c2SJay 955a1351e5dSJay /* false oversize */ 956a1351e5dSJay val lastIsRVC = wb_instr_range.asTypeOf(Vec(PredictWidth,Bool())).last && wb_pd.last.isRVC 957a1351e5dSJay val lastIsRVI = wb_instr_range.asTypeOf(Vec(PredictWidth,Bool()))(PredictWidth - 2) && !wb_pd(PredictWidth - 2).isRVC 9585995c9e7SJenius val lastTaken = wb_check_result_stage1.fixedTaken.last 959a1351e5dSJay 9602a3050c2SJay f3_wb_not_flush := wb_ftq_req.ftqIdx === f3_ftq_req.ftqIdx && f3_valid && wb_valid 9612a3050c2SJay 9623f785aa3SJenius /** if a req with a last half but miss predicted enters in wb stage, and this cycle f3 stalls, 9633f785aa3SJenius * we set a flag to notify f3 that the last half flag need not to be set. 9643f785aa3SJenius */ 965804985a5SJenius //f3_fire is after wb_valid 966076dea5fSJenius when(wb_valid && RegNext(f3_hasLastHalf,init = false.B) 967251a37e4SJenius && wb_check_result_stage2.fixedMissPred(PredictWidth - 1) && !f3_fire && !RegNext(f3_fire,init = false.B) && !f3_flush 9683f785aa3SJenius ){ 9693f785aa3SJenius f3_lastHalf_disable := true.B 970ab6202e2SJenius } 971ab6202e2SJenius 972804985a5SJenius //wb_valid and f3_fire are in same cycle 973076dea5fSJenius when(wb_valid && RegNext(f3_hasLastHalf,init = false.B) 974076dea5fSJenius && wb_check_result_stage2.fixedMissPred(PredictWidth - 1) && f3_fire 975804985a5SJenius ){ 976804985a5SJenius f3_lastHalf.valid := false.B 977804985a5SJenius } 978804985a5SJenius 9792a3050c2SJay val checkFlushWb = Wire(Valid(new PredecodeWritebackBundle)) 980b665b650STang Haojin val checkFlushWbjalTargetIdx = ParallelPriorityEncoder(VecInit(wb_pd.zip(wb_instr_valid).map{case (pd, v) => v && pd.isJal })) 981b665b650STang Haojin val checkFlushWbTargetIdx = ParallelPriorityEncoder(wb_check_result_stage2.fixedMissPred) 9822a3050c2SJay checkFlushWb.valid := wb_valid 9832a3050c2SJay checkFlushWb.bits.pc := wb_pc 9842a3050c2SJay checkFlushWb.bits.pd := wb_pd 9852a3050c2SJay checkFlushWb.bits.pd.zipWithIndex.map{case(instr,i) => instr.valid := wb_instr_valid(i)} 9862a3050c2SJay checkFlushWb.bits.ftqIdx := wb_ftq_req.ftqIdx 9872a3050c2SJay checkFlushWb.bits.ftqOffset := wb_ftq_req.ftqOffset.bits 9885995c9e7SJenius checkFlushWb.bits.misOffset.valid := ParallelOR(wb_check_result_stage2.fixedMissPred) || wb_half_flush 9895995c9e7SJenius checkFlushWb.bits.misOffset.bits := Mux(wb_half_flush, wb_lastIdx, ParallelPriorityEncoder(wb_check_result_stage2.fixedMissPred)) 9905995c9e7SJenius checkFlushWb.bits.cfiOffset.valid := ParallelOR(wb_check_result_stage1.fixedTaken) 9915995c9e7SJenius checkFlushWb.bits.cfiOffset.bits := ParallelPriorityEncoder(wb_check_result_stage1.fixedTaken) 992b665b650STang Haojin checkFlushWb.bits.target := Mux(wb_half_flush, wb_half_target, wb_check_result_stage2.fixedTarget(checkFlushWbTargetIdx)) 993d10ddd67SGuokai Chen checkFlushWb.bits.jalTarget := wb_check_result_stage2.jalTarget(checkFlushWbjalTargetIdx) 9942a3050c2SJay checkFlushWb.bits.instrRange := wb_instr_range.asTypeOf(Vec(PredictWidth, Bool())) 9952a3050c2SJay 996bccc5520SJenius toFtq.pdWb := Mux(wb_valid, checkFlushWb, mmioFlushWb) 9972a3050c2SJay 9982a3050c2SJay wb_redirect := checkFlushWb.bits.misOffset.valid && wb_valid 99909c6f1ddSLingrui98 10005b3c20f7SJinYue /*write back flush type*/ 10015995c9e7SJenius val checkFaultType = wb_check_result_stage2.faultType 10025b3c20f7SJinYue val checkJalFault = wb_valid && checkFaultType.map(_.isjalFault).reduce(_||_) 10035b3c20f7SJinYue val checkRetFault = wb_valid && checkFaultType.map(_.isRetFault).reduce(_||_) 10045b3c20f7SJinYue val checkTargetFault = wb_valid && checkFaultType.map(_.istargetFault).reduce(_||_) 10055b3c20f7SJinYue val checkNotCFIFault = wb_valid && checkFaultType.map(_.notCFIFault).reduce(_||_) 10065b3c20f7SJinYue val checkInvalidTaken = wb_valid && checkFaultType.map(_.invalidTakenFault).reduce(_||_) 10075b3c20f7SJinYue 10085b3c20f7SJinYue 10095b3c20f7SJinYue XSPerfAccumulate("predecode_flush_jalFault", checkJalFault ) 10105b3c20f7SJinYue XSPerfAccumulate("predecode_flush_retFault", checkRetFault ) 10115b3c20f7SJinYue XSPerfAccumulate("predecode_flush_targetFault", checkTargetFault ) 10125b3c20f7SJinYue XSPerfAccumulate("predecode_flush_notCFIFault", checkNotCFIFault ) 10135b3c20f7SJinYue XSPerfAccumulate("predecode_flush_incalidTakenFault", checkInvalidTaken ) 10145b3c20f7SJinYue 10155b3c20f7SJinYue when(checkRetFault){ 10165b3c20f7SJinYue XSDebug("startAddr:%x nextstartAddr:%x taken:%d takenIdx:%d\n", 10175b3c20f7SJinYue wb_ftq_req.startAddr, wb_ftq_req.nextStartAddr, wb_ftq_req.ftqOffset.valid, wb_ftq_req.ftqOffset.bits) 10185b3c20f7SJinYue } 10195b3c20f7SJinYue 102051532d8bSGuokai Chen 10211d8f4dcbSJay /** performance counter */ 1022005e809bSJiuyang Liu val f3_perf_info = RegEnable(f2_perf_info, f2_fire) 1023935edac4STang Haojin val f3_req_0 = io.toIbuffer.fire 1024935edac4STang Haojin val f3_req_1 = io.toIbuffer.fire && f3_doubleLine 1025935edac4STang Haojin val f3_hit_0 = io.toIbuffer.fire && f3_perf_info.bank_hit(0) 1026935edac4STang Haojin val f3_hit_1 = io.toIbuffer.fire && f3_doubleLine & f3_perf_info.bank_hit(1) 10271d8f4dcbSJay val f3_hit = f3_perf_info.hit 1028cd365d4cSrvcoresjw val perfEvents = Seq( 10292a3050c2SJay ("frontendFlush ", wb_redirect ), 1030935edac4STang Haojin ("ifu_req ", io.toIbuffer.fire ), 1031935edac4STang Haojin ("ifu_miss ", io.toIbuffer.fire && !f3_perf_info.hit ), 1032cd365d4cSrvcoresjw ("ifu_req_cacheline_0 ", f3_req_0 ), 1033cd365d4cSrvcoresjw ("ifu_req_cacheline_1 ", f3_req_1 ), 1034cd365d4cSrvcoresjw ("ifu_req_cacheline_0_hit ", f3_hit_1 ), 1035cd365d4cSrvcoresjw ("ifu_req_cacheline_1_hit ", f3_hit_1 ), 1036935edac4STang Haojin ("only_0_hit ", f3_perf_info.only_0_hit && io.toIbuffer.fire ), 1037935edac4STang Haojin ("only_0_miss ", f3_perf_info.only_0_miss && io.toIbuffer.fire ), 1038935edac4STang Haojin ("hit_0_hit_1 ", f3_perf_info.hit_0_hit_1 && io.toIbuffer.fire ), 1039935edac4STang Haojin ("hit_0_miss_1 ", f3_perf_info.hit_0_miss_1 && io.toIbuffer.fire ), 1040935edac4STang Haojin ("miss_0_hit_1 ", f3_perf_info.miss_0_hit_1 && io.toIbuffer.fire ), 1041935edac4STang Haojin ("miss_0_miss_1 ", f3_perf_info.miss_0_miss_1 && io.toIbuffer.fire ), 1042cd365d4cSrvcoresjw ) 10431ca0e4f3SYinan Xu generatePerfEvent() 104409c6f1ddSLingrui98 1045935edac4STang Haojin XSPerfAccumulate("ifu_req", io.toIbuffer.fire ) 1046935edac4STang Haojin XSPerfAccumulate("ifu_miss", io.toIbuffer.fire && !f3_hit ) 1047f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_0", f3_req_0 ) 1048f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_1", f3_req_1 ) 1049f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_0_hit", f3_hit_0 ) 1050f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_1_hit", f3_hit_1 ) 10512a3050c2SJay XSPerfAccumulate("frontendFlush", wb_redirect ) 1052935edac4STang Haojin XSPerfAccumulate("only_0_hit", f3_perf_info.only_0_hit && io.toIbuffer.fire ) 1053935edac4STang Haojin XSPerfAccumulate("only_0_miss", f3_perf_info.only_0_miss && io.toIbuffer.fire ) 1054935edac4STang Haojin XSPerfAccumulate("hit_0_hit_1", f3_perf_info.hit_0_hit_1 && io.toIbuffer.fire ) 1055935edac4STang Haojin XSPerfAccumulate("hit_0_miss_1", f3_perf_info.hit_0_miss_1 && io.toIbuffer.fire ) 1056935edac4STang Haojin XSPerfAccumulate("miss_0_hit_1", f3_perf_info.miss_0_hit_1 && io.toIbuffer.fire ) 1057935edac4STang Haojin XSPerfAccumulate("miss_0_miss_1", f3_perf_info.miss_0_miss_1 && io.toIbuffer.fire ) 1058935edac4STang Haojin XSPerfAccumulate("hit_0_except_1", f3_perf_info.hit_0_except_1 && io.toIbuffer.fire ) 1059935edac4STang Haojin XSPerfAccumulate("miss_0_except_1", f3_perf_info.miss_0_except_1 && io.toIbuffer.fire ) 1060935edac4STang Haojin XSPerfAccumulate("except_0", f3_perf_info.except_0 && io.toIbuffer.fire ) 1061eb163ef0SHaojin Tang XSPerfHistogram("ifu2ibuffer_validCnt", PopCount(io.toIbuffer.bits.valid & io.toIbuffer.bits.enqEnable), io.toIbuffer.fire, 0, PredictWidth + 1, 1) 106251532d8bSGuokai Chen 1063c686adcdSYinan Xu val hartId = p(XSCoreParamsKey).HartId 1064c686adcdSYinan Xu val isWriteFetchToIBufferTable = Constantin.createRecord(s"isWriteFetchToIBufferTable$hartId") 1065c686adcdSYinan Xu val isWriteIfuWbToFtqTable = Constantin.createRecord(s"isWriteIfuWbToFtqTable$hartId") 1066c686adcdSYinan Xu val fetchToIBufferTable = ChiselDB.createTable(s"FetchToIBuffer$hartId", new FetchToIBufferDB) 1067c686adcdSYinan Xu val ifuWbToFtqTable = ChiselDB.createTable(s"IfuWbToFtq$hartId", new IfuWbToFtqDB) 106851532d8bSGuokai Chen 106951532d8bSGuokai Chen val fetchIBufferDumpData = Wire(new FetchToIBufferDB) 107051532d8bSGuokai Chen fetchIBufferDumpData.start_addr := f3_ftq_req.startAddr 107151532d8bSGuokai Chen fetchIBufferDumpData.instr_count := PopCount(io.toIbuffer.bits.enqEnable) 1072935edac4STang 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) 107351532d8bSGuokai Chen fetchIBufferDumpData.is_cache_hit := f3_hit 107451532d8bSGuokai Chen 107551532d8bSGuokai Chen val ifuWbToFtqDumpData = Wire(new IfuWbToFtqDB) 107651532d8bSGuokai Chen ifuWbToFtqDumpData.start_addr := wb_ftq_req.startAddr 107751532d8bSGuokai Chen ifuWbToFtqDumpData.is_miss_pred := checkFlushWb.bits.misOffset.valid 107851532d8bSGuokai Chen ifuWbToFtqDumpData.miss_pred_offset := checkFlushWb.bits.misOffset.bits 107951532d8bSGuokai Chen ifuWbToFtqDumpData.checkJalFault := checkJalFault 108051532d8bSGuokai Chen ifuWbToFtqDumpData.checkRetFault := checkRetFault 108151532d8bSGuokai Chen ifuWbToFtqDumpData.checkTargetFault := checkTargetFault 108251532d8bSGuokai Chen ifuWbToFtqDumpData.checkNotCFIFault := checkNotCFIFault 108351532d8bSGuokai Chen ifuWbToFtqDumpData.checkInvalidTaken := checkInvalidTaken 108451532d8bSGuokai Chen 108551532d8bSGuokai Chen fetchToIBufferTable.log( 108651532d8bSGuokai Chen data = fetchIBufferDumpData, 1087da3bf434SMaxpicca-Li en = isWriteFetchToIBufferTable.orR && io.toIbuffer.fire, 108851532d8bSGuokai Chen site = "IFU" + p(XSCoreParamsKey).HartId.toString, 108951532d8bSGuokai Chen clock = clock, 109051532d8bSGuokai Chen reset = reset 109151532d8bSGuokai Chen ) 109251532d8bSGuokai Chen ifuWbToFtqTable.log( 109351532d8bSGuokai Chen data = ifuWbToFtqDumpData, 1094da3bf434SMaxpicca-Li en = isWriteIfuWbToFtqTable.orR && checkFlushWb.valid, 109551532d8bSGuokai Chen site = "IFU" + p(XSCoreParamsKey).HartId.toString, 109651532d8bSGuokai Chen clock = clock, 109751532d8bSGuokai Chen reset = reset 109851532d8bSGuokai Chen ) 109951532d8bSGuokai Chen 110009c6f1ddSLingrui98} 1101