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 132aeedc8eeSGuokai Chen with HasXSParameter 1332a3050c2SJay with HasIFUConst 1342a3050c2SJay with HasPdConst 135167bcd01SJay with HasCircularQueuePtrHelper 1362a3050c2SJay with HasPerfEvents 13721ae6bc4Speixiaokun with HasTlbConst 13809c6f1ddSLingrui98{ 13909c6f1ddSLingrui98 val io = IO(new NewIFUIO) 14009c6f1ddSLingrui98 val (toFtq, fromFtq) = (io.ftqInter.toFtq, io.ftqInter.fromFtq) 141c5c5edaeSJenius val fromICache = io.icacheInter.resp 1420be662e4SJay val (toUncache, fromUncache) = (io.uncacheInter.toUncache , io.uncacheInter.fromUncache) 14309c6f1ddSLingrui98 14409c6f1ddSLingrui98 def isCrossLineReq(start: UInt, end: UInt): Bool = start(blockOffBits) ^ end(blockOffBits) 14509c6f1ddSLingrui98 146d2b20d1aSTang Haojin def numOfStage = 3 147e4d2f6a9Smy-mayfly // equal lower_result overflow bit 148e4d2f6a9Smy-mayfly def PcCutPoint = (VAddrBits/4) - 1 149e4d2f6a9Smy-mayfly def CatPC(low: UInt, high: UInt, high1: UInt): UInt = { 150e4d2f6a9Smy-mayfly Mux( 151e4d2f6a9Smy-mayfly low(PcCutPoint), 152e4d2f6a9Smy-mayfly Cat(high1, low(PcCutPoint-1, 0)), 153e4d2f6a9Smy-mayfly Cat(high, low(PcCutPoint-1, 0)) 154e4d2f6a9Smy-mayfly ) 155e4d2f6a9Smy-mayfly } 156e4d2f6a9Smy-mayfly def CatPC(lowVec: Vec[UInt], high: UInt, high1: UInt): Vec[UInt] = VecInit(lowVec.map(CatPC(_, high, high1))) 157d2b20d1aSTang Haojin require(numOfStage > 1, "BPU numOfStage must be greater than 1") 158d2b20d1aSTang Haojin val topdown_stages = RegInit(VecInit(Seq.fill(numOfStage)(0.U.asTypeOf(new FrontendTopDownBundle)))) 159d2b20d1aSTang Haojin // bubble events in IFU, only happen in stage 1 160d2b20d1aSTang Haojin val icacheMissBubble = Wire(Bool()) 161d2b20d1aSTang Haojin val itlbMissBubble =Wire(Bool()) 162d2b20d1aSTang Haojin 163d2b20d1aSTang Haojin // only driven by clock, not valid-ready 164d2b20d1aSTang Haojin topdown_stages(0) := fromFtq.req.bits.topdown_info 165d2b20d1aSTang Haojin for (i <- 1 until numOfStage) { 166d2b20d1aSTang Haojin topdown_stages(i) := topdown_stages(i - 1) 167d2b20d1aSTang Haojin } 168d2b20d1aSTang Haojin when (icacheMissBubble) { 169d2b20d1aSTang Haojin topdown_stages(1).reasons(TopDownCounters.ICacheMissBubble.id) := true.B 170d2b20d1aSTang Haojin } 171d2b20d1aSTang Haojin when (itlbMissBubble) { 172d2b20d1aSTang Haojin topdown_stages(1).reasons(TopDownCounters.ITLBMissBubble.id) := true.B 173d2b20d1aSTang Haojin } 174d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info := topdown_stages(numOfStage - 1) 175d2b20d1aSTang Haojin when (fromFtq.topdown_redirect.valid) { 176d2b20d1aSTang Haojin // only redirect from backend, IFU redirect itself is handled elsewhere 177d2b20d1aSTang Haojin when (fromFtq.topdown_redirect.bits.debugIsCtrl) { 178d2b20d1aSTang Haojin /* 179d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 180d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.ControlRedirectBubble.id) := true.B 181d2b20d1aSTang Haojin } 182d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.ControlRedirectBubble.id) := true.B 183d2b20d1aSTang Haojin */ 184d2b20d1aSTang Haojin when (fromFtq.topdown_redirect.bits.ControlBTBMissBubble) { 185d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 186d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.BTBMissBubble.id) := true.B 187d2b20d1aSTang Haojin } 188d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.BTBMissBubble.id) := true.B 189d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.TAGEMissBubble) { 190d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 191d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.TAGEMissBubble.id) := true.B 192d2b20d1aSTang Haojin } 193d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.TAGEMissBubble.id) := true.B 194d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.SCMissBubble) { 195d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 196d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.SCMissBubble.id) := true.B 197d2b20d1aSTang Haojin } 198d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.SCMissBubble.id) := true.B 199d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.ITTAGEMissBubble) { 200d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 201d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 202d2b20d1aSTang Haojin } 203d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 204d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.RASMissBubble) { 205d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 206d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.RASMissBubble.id) := true.B 207d2b20d1aSTang Haojin } 208d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.RASMissBubble.id) := true.B 209d2b20d1aSTang Haojin } 210d2b20d1aSTang Haojin } .elsewhen (fromFtq.topdown_redirect.bits.debugIsMemVio) { 211d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 212d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 213d2b20d1aSTang Haojin } 214d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 215d2b20d1aSTang Haojin } .otherwise { 216d2b20d1aSTang Haojin for (i <- 0 until numOfStage) { 217d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 218d2b20d1aSTang Haojin } 219d2b20d1aSTang Haojin io.toIbuffer.bits.topdown_info.reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 220d2b20d1aSTang Haojin } 221d2b20d1aSTang Haojin } 222d2b20d1aSTang Haojin 2231d8f4dcbSJay class TlbExept(implicit p: Parameters) extends XSBundle{ 2241d8f4dcbSJay val pageFault = Bool() 2251d8f4dcbSJay val accessFault = Bool() 2261d8f4dcbSJay val mmio = Bool() 227b005f7c6SJay } 22809c6f1ddSLingrui98 229a61a35e0Sssszwic val preDecoder = Module(new PreDecode) 230dc270d3bSJenius 2312a3050c2SJay val predChecker = Module(new PredChecker) 2322a3050c2SJay val frontendTrigger = Module(new FrontendTrigger) 2335995c9e7SJenius val (checkerIn, checkerOutStage1, checkerOutStage2) = (predChecker.io.in, predChecker.io.out.stage1Out,predChecker.io.out.stage2Out) 2341d8f4dcbSJay 23558dbdfc2SJay /** 23658dbdfc2SJay ****************************************************************************** 23758dbdfc2SJay * IFU Stage 0 23858dbdfc2SJay * - send cacheline fetch request to ICacheMainPipe 23958dbdfc2SJay ****************************************************************************** 24058dbdfc2SJay */ 24109c6f1ddSLingrui98 24209c6f1ddSLingrui98 val f0_valid = fromFtq.req.valid 24309c6f1ddSLingrui98 val f0_ftq_req = fromFtq.req.bits 2446ce52296SJinYue val f0_doubleLine = fromFtq.req.bits.crossCacheline 24534a88126SJinYue val f0_vSetIdx = VecInit(get_idx((f0_ftq_req.startAddr)), get_idx(f0_ftq_req.nextlineStart)) 246935edac4STang Haojin val f0_fire = fromFtq.req.fire 24709c6f1ddSLingrui98 24809c6f1ddSLingrui98 val f0_flush, f1_flush, f2_flush, f3_flush = WireInit(false.B) 24909c6f1ddSLingrui98 val from_bpu_f0_flush, from_bpu_f1_flush, from_bpu_f2_flush, from_bpu_f3_flush = WireInit(false.B) 25009c6f1ddSLingrui98 251cb4f77ceSLingrui98 from_bpu_f0_flush := fromFtq.flushFromBpu.shouldFlushByStage2(f0_ftq_req.ftqIdx) || 252cb4f77ceSLingrui98 fromFtq.flushFromBpu.shouldFlushByStage3(f0_ftq_req.ftqIdx) 25309c6f1ddSLingrui98 2542a3050c2SJay val wb_redirect , mmio_redirect, backend_redirect= WireInit(false.B) 2552a3050c2SJay val f3_wb_not_flush = WireInit(false.B) 2562a3050c2SJay 2572a3050c2SJay backend_redirect := fromFtq.redirect.valid 2582a3050c2SJay f3_flush := backend_redirect || (wb_redirect && !f3_wb_not_flush) 2592a3050c2SJay f2_flush := backend_redirect || mmio_redirect || wb_redirect 26009c6f1ddSLingrui98 f1_flush := f2_flush || from_bpu_f1_flush 26109c6f1ddSLingrui98 f0_flush := f1_flush || from_bpu_f0_flush 26209c6f1ddSLingrui98 26309c6f1ddSLingrui98 val f1_ready, f2_ready, f3_ready = WireInit(false.B) 26409c6f1ddSLingrui98 26550780602SJenius fromFtq.req.ready := f1_ready && io.icacheInter.icacheReady 26609c6f1ddSLingrui98 267d2b20d1aSTang Haojin 268d2b20d1aSTang Haojin when (wb_redirect) { 269d2b20d1aSTang Haojin when (f3_wb_not_flush) { 270d2b20d1aSTang Haojin topdown_stages(2).reasons(TopDownCounters.BTBMissBubble.id) := true.B 271d2b20d1aSTang Haojin } 272d2b20d1aSTang Haojin for (i <- 0 until numOfStage - 1) { 273d2b20d1aSTang Haojin topdown_stages(i).reasons(TopDownCounters.BTBMissBubble.id) := true.B 274d2b20d1aSTang Haojin } 275d2b20d1aSTang Haojin } 276d2b20d1aSTang Haojin 27758dbdfc2SJay /** <PERF> f0 fetch bubble */ 278f7c29b0aSJinYue 27900240ba6SJay XSPerfAccumulate("fetch_bubble_ftq_not_valid", !fromFtq.req.valid && fromFtq.req.ready ) 280c5c5edaeSJenius // XSPerfAccumulate("fetch_bubble_pipe_stall", f0_valid && toICache(0).ready && toICache(1).ready && !f1_ready ) 281c5c5edaeSJenius // XSPerfAccumulate("fetch_bubble_icache_0_busy", f0_valid && !toICache(0).ready ) 282c5c5edaeSJenius // XSPerfAccumulate("fetch_bubble_icache_1_busy", f0_valid && !toICache(1).ready ) 28300240ba6SJay XSPerfAccumulate("fetch_flush_backend_redirect", backend_redirect ) 28400240ba6SJay XSPerfAccumulate("fetch_flush_wb_redirect", wb_redirect ) 28500240ba6SJay XSPerfAccumulate("fetch_flush_bpu_f1_flush", from_bpu_f1_flush ) 28600240ba6SJay XSPerfAccumulate("fetch_flush_bpu_f0_flush", from_bpu_f0_flush ) 28758dbdfc2SJay 28858dbdfc2SJay 28958dbdfc2SJay /** 29058dbdfc2SJay ****************************************************************************** 29158dbdfc2SJay * IFU Stage 1 29258dbdfc2SJay * - calculate pc/half_pc/cut_ptr for every instruction 29358dbdfc2SJay ****************************************************************************** 29458dbdfc2SJay */ 29509c6f1ddSLingrui98 29609c6f1ddSLingrui98 val f1_valid = RegInit(false.B) 297005e809bSJiuyang Liu val f1_ftq_req = RegEnable(f0_ftq_req, f0_fire) 298005e809bSJiuyang Liu // val f1_situation = RegEnable(f0_situation, f0_fire) 299005e809bSJiuyang Liu val f1_doubleLine = RegEnable(f0_doubleLine, f0_fire) 300005e809bSJiuyang Liu val f1_vSetIdx = RegEnable(f0_vSetIdx, f0_fire) 301625ecd17SJenius val f1_fire = f1_valid && f2_ready 30209c6f1ddSLingrui98 303625ecd17SJenius f1_ready := f1_fire || !f1_valid 30409c6f1ddSLingrui98 3050d756c48SJinYue from_bpu_f1_flush := fromFtq.flushFromBpu.shouldFlushByStage3(f1_ftq_req.ftqIdx) && f1_valid 306cb4f77ceSLingrui98 // from_bpu_f1_flush := false.B 30709c6f1ddSLingrui98 30809c6f1ddSLingrui98 when(f1_flush) {f1_valid := false.B} 30909c6f1ddSLingrui98 .elsewhen(f0_fire && !f0_flush) {f1_valid := true.B} 31009c6f1ddSLingrui98 .elsewhen(f1_fire) {f1_valid := false.B} 31109c6f1ddSLingrui98 312e4d2f6a9Smy-mayfly val f1_pc_high = f1_ftq_req.startAddr(VAddrBits-1, PcCutPoint) 313f2f493deSstride val f1_pc_high_plus1 = f1_pc_high + 1.U 314f2f493deSstride 315e4d2f6a9Smy-mayfly /** 316e4d2f6a9Smy-mayfly * In order to reduce power consumption, avoid calculating the full PC value in the first level. 317e4d2f6a9Smy-mayfly * code of original logic, this code has been deprecated 318e4d2f6a9Smy-mayfly * val f1_pc = VecInit(f1_pc_lower_result.map{ i => 319e4d2f6a9Smy-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)))}) 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 37988895b11Sxu_zh val f2_exception = VecInit((0 until PortNumber).map(i => fromICache(i).bits.exception)) 380c1b28b66STang Haojin val f2_except_fromBackend = fromICache(0).bits.exceptionFromBackend 381d7ac23a3SEaston Man // paddr and gpaddr of [startAddr, nextLineAddr] 382d7ac23a3SEaston Man val f2_paddrs = VecInit((0 until PortNumber).map(i => fromICache(i).bits.paddr)) 38391946104Sxu_zh val f2_gpaddr = fromICache(0).bits.gpaddr 384002c10a4SYanqin Li 385002c10a4SYanqin Li // FIXME: what if port 0 is not mmio, but port 1 is? 38688895b11Sxu_zh // cancel mmio fetch if exception occurs 387002c10a4SYanqin Li val f2_mmio = f2_exception(0) === ExceptionType.none && ( 388002c10a4SYanqin Li fromICache(0).bits.pmp_mmio || 389002c10a4SYanqin Li // currently, we do not distinguish between Pbmt.nc and Pbmt.io 390002c10a4SYanqin Li // anyway, they are both non-cacheable, and should be handled with mmio fsm and sent to Uncache module 391002c10a4SYanqin Li Pbmt.isUncache(fromICache(0).bits.itlb_pbmt) 392002c10a4SYanqin Li ) 393002c10a4SYanqin Li 3940be662e4SJay 395e4d2f6a9Smy-mayfly /** 396e4d2f6a9Smy-mayfly * reduce the number of registers, origin code 397e4d2f6a9Smy-mayfly * f2_pc = RegEnable(f1_pc, f1_fire) 398e4d2f6a9Smy-mayfly */ 399e4d2f6a9Smy-mayfly val f2_pc_lower_result = RegEnable(f1_pc_lower_result, f1_fire) 400e4d2f6a9Smy-mayfly val f2_pc_high = RegEnable(f1_pc_high, f1_fire) 401e4d2f6a9Smy-mayfly val f2_pc_high_plus1 = RegEnable(f1_pc_high_plus1, f1_fire) 402e4d2f6a9Smy-mayfly val f2_pc = CatPC(f2_pc_lower_result, f2_pc_high, f2_pc_high_plus1) 403a37fbf10SJay 404e4d2f6a9Smy-mayfly val f2_cut_ptr = RegEnable(f1_cut_ptr, f1_fire) 405005e809bSJiuyang Liu val f2_resend_vaddr = RegEnable(f1_ftq_req.startAddr + 2.U, f1_fire) 4062a3050c2SJay 4072a3050c2SJay def isNextLine(pc: UInt, startAddr: UInt) = { 4082a3050c2SJay startAddr(blockOffBits) ^ pc(blockOffBits) 409b6982e83SLemover } 41009c6f1ddSLingrui98 4112a3050c2SJay def isLastInLine(pc: UInt) = { 4122a3050c2SJay pc(blockOffBits - 1, 0) === "b111110".U 41309c6f1ddSLingrui98 } 41409c6f1ddSLingrui98 4152a3050c2SJay val f2_foldpc = VecInit(f2_pc.map(i => XORFold(i(VAddrBits-1,1), MemPredPCWidth))) 4162a3050c2SJay val f2_jump_range = Fill(PredictWidth, !f2_ftq_req.ftqOffset.valid) | Fill(PredictWidth, 1.U(1.W)) >> ~f2_ftq_req.ftqOffset.bits 4171d011975SJinYue 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) 4182a3050c2SJay val f2_instr_range = f2_jump_range & f2_ftr_range 41988895b11Sxu_zh val f2_exception_vec = VecInit((0 until PredictWidth).map( i => MuxCase(ExceptionType.none, Seq( 42088895b11Sxu_zh !isNextLine(f2_pc(i), f2_ftq_req.startAddr) -> f2_exception(0), 42188895b11Sxu_zh (isNextLine(f2_pc(i), f2_ftq_req.startAddr) && f2_doubleLine) -> f2_exception(1) 42288895b11Sxu_zh )))) 4231d8f4dcbSJay val f2_perf_info = io.icachePerfInfo 42409c6f1ddSLingrui98 4252a3050c2SJay def cut(cacheline: UInt, cutPtr: Vec[UInt]) : Vec[UInt] ={ 426d558bd61SJenius require(HasCExtension) 427d558bd61SJenius // if(HasCExtension){ 42809c6f1ddSLingrui98 val result = Wire(Vec(PredictWidth + 1, UInt(16.W))) 429b92f8445Sssszwic val dataVec = cacheline.asTypeOf(Vec(blockBytes, UInt(16.W))) //32 16-bit data vector 43009c6f1ddSLingrui98 (0 until PredictWidth + 1).foreach( i => 431d558bd61SJenius result(i) := dataVec(cutPtr(i)) //the max ptr is 3*blockBytes/4-1 43209c6f1ddSLingrui98 ) 43309c6f1ddSLingrui98 result 434d558bd61SJenius // } else { 435d558bd61SJenius // val result = Wire(Vec(PredictWidth, UInt(32.W)) ) 436d558bd61SJenius // val dataVec = cacheline.asTypeOf(Vec(blockBytes * 2/ 4, UInt(32.W))) 437d558bd61SJenius // (0 until PredictWidth).foreach( i => 438d558bd61SJenius // result(i) := dataVec(cutPtr(i)) 439d558bd61SJenius // ) 440d558bd61SJenius // result 441d558bd61SJenius // } 44209c6f1ddSLingrui98 } 44309c6f1ddSLingrui98 444a61a35e0Sssszwic val f2_cache_response_data = fromICache.map(_.bits.data) 445b92f8445Sssszwic val f2_data_2_cacheline = Cat(f2_cache_response_data(0), f2_cache_response_data(0)) 446dc270d3bSJenius 447a61a35e0Sssszwic val f2_cut_data = cut(f2_data_2_cacheline, f2_cut_ptr) 44809c6f1ddSLingrui98 44958dbdfc2SJay /** predecode (include RVC expander) */ 450dc270d3bSJenius // preDecoderRegIn.data := f2_reg_cut_data 451dc270d3bSJenius // preDecoderRegInIn.frontendTrigger := io.frontendTrigger 452dc270d3bSJenius // preDecoderRegInIn.csrTriggerEnable := io.csrTriggerEnable 453dc270d3bSJenius // preDecoderRegIn.pc := f2_pc 454dc270d3bSJenius 455a61a35e0Sssszwic val preDecoderIn = preDecoder.io.in 4569afa8a47STang Haojin preDecoderIn.valid := f2_valid 4579afa8a47STang Haojin preDecoderIn.bits.data := f2_cut_data 4589afa8a47STang Haojin preDecoderIn.bits.frontendTrigger := io.frontendTrigger 4599afa8a47STang Haojin preDecoderIn.bits.pc := f2_pc 460a61a35e0Sssszwic val preDecoderOut = preDecoder.io.out 46109c6f1ddSLingrui98 46248a62719SJenius //val f2_expd_instr = preDecoderOut.expInstr 46348a62719SJenius val f2_instr = preDecoderOut.instr 4642a3050c2SJay val f2_pd = preDecoderOut.pd 4652a3050c2SJay val f2_jump_offset = preDecoderOut.jumpOffset 4662a3050c2SJay val f2_hasHalfValid = preDecoderOut.hasHalfValid 467a2568a60Sxu_zh /* if there is a cross-page RVI instruction, and the former page has no exception, 468a2568a60Sxu_zh * whether it has exception is actually depends on the latter page 469a2568a60Sxu_zh */ 470a2568a60Sxu_zh val f2_crossPage_exception_vec = VecInit((0 until PredictWidth).map { i => Mux( 471a2568a60Sxu_zh isLastInLine(f2_pc(i)) && !f2_pd(i).isRVC && f2_doubleLine && f2_exception(0) === ExceptionType.none, 472a2568a60Sxu_zh f2_exception(1), 473a2568a60Sxu_zh ExceptionType.none 474a2568a60Sxu_zh )}) 47500240ba6SJay XSPerfAccumulate("fetch_bubble_icache_not_resp", f2_valid && !icacheRespAllValid ) 47600240ba6SJay 47709c6f1ddSLingrui98 47858dbdfc2SJay /** 47958dbdfc2SJay ****************************************************************************** 48058dbdfc2SJay * IFU Stage 3 48158dbdfc2SJay * - handle MMIO instruciton 48258dbdfc2SJay * -send request to Uncache fetch Unit 48358dbdfc2SJay * -every packet include 1 MMIO instruction 48458dbdfc2SJay * -MMIO instructions will stop fetch pipeline until commiting from RoB 48558dbdfc2SJay * -flush to snpc (send ifu_redirect to Ftq) 48658dbdfc2SJay * - Ibuffer enqueue 48758dbdfc2SJay * - check predict result in Frontend (jalFault/retFault/notCFIFault/invalidTakenFault/targetFault) 48858dbdfc2SJay * - handle last half RVI instruction 48958dbdfc2SJay ****************************************************************************** 49058dbdfc2SJay */ 49158dbdfc2SJay 49292c61038SXuan Hu val expanders = Seq.fill(PredictWidth)(Module(new RVCExpander)) 49392c61038SXuan Hu 49409c6f1ddSLingrui98 val f3_valid = RegInit(false.B) 495005e809bSJiuyang Liu val f3_ftq_req = RegEnable(f2_ftq_req, f2_fire) 496005e809bSJiuyang Liu // val f3_situation = RegEnable(f2_situation, f2_fire) 497005e809bSJiuyang Liu val f3_doubleLine = RegEnable(f2_doubleLine, f2_fire) 498935edac4STang Haojin val f3_fire = io.toIbuffer.fire 4991d8f4dcbSJay 500a61a35e0Sssszwic val f3_cut_data = RegEnable(f2_cut_data, f2_fire) 5011d8f4dcbSJay 50288895b11Sxu_zh val f3_exception = RegEnable(f2_exception, f2_fire) 503005e809bSJiuyang Liu val f3_mmio = RegEnable(f2_mmio, f2_fire) 504c1b28b66STang Haojin val f3_except_fromBackend = RegEnable(f2_except_fromBackend, f2_fire) 50509c6f1ddSLingrui98 506935edac4STang Haojin val f3_instr = RegEnable(f2_instr, f2_fire) 507aeedc8eeSGuokai Chen 50892c61038SXuan Hu expanders.zipWithIndex.foreach { case (expander, i) => 50992c61038SXuan Hu expander.io.in := f3_instr(i) 51092c61038SXuan Hu } 51192c61038SXuan Hu // Use expanded instruction only when input is legal. 51292c61038SXuan Hu // Otherwise use origin illegal RVC instruction. 51392c61038SXuan Hu val f3_expd_instr = VecInit(expanders.map { expander: RVCExpander => 51492c61038SXuan Hu Mux(expander.io.ill, expander.io.in, expander.io.out.bits) 51592c61038SXuan Hu }) 51692c61038SXuan Hu val f3_ill = VecInit(expanders.map(_.io.ill)) 51748a62719SJenius 518935edac4STang Haojin val f3_pd_wire = RegEnable(f2_pd, f2_fire) 519330aad7fSGuokai Chen val f3_pd = WireInit(f3_pd_wire) 520935edac4STang Haojin val f3_jump_offset = RegEnable(f2_jump_offset, f2_fire) 52188895b11Sxu_zh val f3_exception_vec = RegEnable(f2_exception_vec, f2_fire) 522a2568a60Sxu_zh val f3_crossPage_exception_vec = RegEnable(f2_crossPage_exception_vec, f2_fire) 523e4d2f6a9Smy-mayfly 524e4d2f6a9Smy-mayfly val f3_pc_lower_result = RegEnable(f2_pc_lower_result, f2_fire) 525e4d2f6a9Smy-mayfly val f3_pc_high = RegEnable(f2_pc_high, f2_fire) 526e4d2f6a9Smy-mayfly val f3_pc_high_plus1 = RegEnable(f2_pc_high_plus1, f2_fire) 527e4d2f6a9Smy-mayfly val f3_pc = CatPC(f3_pc_lower_result, f3_pc_high, f3_pc_high_plus1) 528e4d2f6a9Smy-mayfly 529e4d2f6a9Smy-mayfly val f3_pc_last_lower_result_plus2 = RegEnable(f2_pc_lower_result(PredictWidth - 1) + 2.U, f2_fire) 530e4d2f6a9Smy-mayfly val f3_pc_last_lower_result_plus4 = RegEnable(f2_pc_lower_result(PredictWidth - 1) + 4.U, f2_fire) 531e4d2f6a9Smy-mayfly //val f3_half_snpc = RegEnable(f2_half_snpc, f2_fire) 532e4d2f6a9Smy-mayfly 533e4d2f6a9Smy-mayfly /** 534e4d2f6a9Smy-mayfly *********************************************************************** 535e4d2f6a9Smy-mayfly * Half snpc(i) is larger than pc(i) by 4. Using pc to calculate half snpc may be a good choice. 536e4d2f6a9Smy-mayfly *********************************************************************** 537e4d2f6a9Smy-mayfly */ 538e4d2f6a9Smy-mayfly val f3_half_snpc = Wire(Vec(PredictWidth,UInt(VAddrBits.W))) 539e4d2f6a9Smy-mayfly for(i <- 0 until PredictWidth){ 540e4d2f6a9Smy-mayfly if(i == (PredictWidth - 2)){ 541e4d2f6a9Smy-mayfly f3_half_snpc(i) := CatPC(f3_pc_last_lower_result_plus2, f3_pc_high, f3_pc_high_plus1) 542e4d2f6a9Smy-mayfly } else if (i == (PredictWidth - 1)){ 543e4d2f6a9Smy-mayfly f3_half_snpc(i) := CatPC(f3_pc_last_lower_result_plus4, f3_pc_high, f3_pc_high_plus1) 544e4d2f6a9Smy-mayfly } else { 545e4d2f6a9Smy-mayfly f3_half_snpc(i) := f3_pc(i+2) 546e4d2f6a9Smy-mayfly } 547e4d2f6a9Smy-mayfly } 548e4d2f6a9Smy-mayfly 549935edac4STang Haojin val f3_instr_range = RegEnable(f2_instr_range, f2_fire) 550935edac4STang Haojin val f3_foldpc = RegEnable(f2_foldpc, f2_fire) 551935edac4STang Haojin val f3_hasHalfValid = RegEnable(f2_hasHalfValid, f2_fire) 552d7ac23a3SEaston Man val f3_paddrs = RegEnable(f2_paddrs, f2_fire) 55391946104Sxu_zh val f3_gpaddr = RegEnable(f2_gpaddr, f2_fire) 554005e809bSJiuyang Liu val f3_resend_vaddr = RegEnable(f2_resend_vaddr, f2_fire) 555ee175d78SJay 556cb6e5d3cSssszwic // Expand 1 bit to prevent overflow when assert 557cb6e5d3cSssszwic val f3_ftq_req_startAddr = Cat(0.U(1.W), f3_ftq_req.startAddr) 558cb6e5d3cSssszwic val f3_ftq_req_nextStartAddr = Cat(0.U(1.W), f3_ftq_req.nextStartAddr) 559330aad7fSGuokai Chen // brType, isCall and isRet generation is delayed to f3 stage 560330aad7fSGuokai Chen val f3Predecoder = Module(new F3Predecoder) 561330aad7fSGuokai Chen 562330aad7fSGuokai Chen f3Predecoder.io.in.instr := f3_instr 563330aad7fSGuokai Chen 564330aad7fSGuokai Chen f3_pd.zipWithIndex.map{ case (pd,i) => 565330aad7fSGuokai Chen pd.brType := f3Predecoder.io.out.pd(i).brType 566330aad7fSGuokai Chen pd.isCall := f3Predecoder.io.out.pd(i).isCall 567330aad7fSGuokai Chen pd.isRet := f3Predecoder.io.out.pd(i).isRet 568330aad7fSGuokai Chen } 569330aad7fSGuokai Chen 570330aad7fSGuokai Chen val f3PdDiff = f3_pd_wire.zip(f3_pd).map{ case (a,b) => a.asUInt =/= b.asUInt }.reduce(_||_) 571330aad7fSGuokai Chen XSError(f3_valid && f3PdDiff, "f3 pd diff") 572330aad7fSGuokai Chen 5731d011975SJinYue when(f3_valid && !f3_ftq_req.ftqOffset.valid){ 574cb6e5d3cSssszwic assert(f3_ftq_req_startAddr + (2*PredictWidth).U >= f3_ftq_req_nextStartAddr, s"More tha ${2*PredictWidth} Bytes fetch is not allowed!") 5751d011975SJinYue } 576a1351e5dSJay 5772a3050c2SJay /*** MMIO State Machine***/ 578ee175d78SJay val f3_mmio_data = Reg(Vec(2, UInt(16.W))) 579ee175d78SJay val mmio_is_RVC = RegInit(false.B) 580ee175d78SJay val mmio_resend_addr = RegInit(0.U(PAddrBits.W)) 58188895b11Sxu_zh val mmio_resend_exception = RegInit(0.U(ExceptionType.width.W)) 582b5a614b9Sxu_zh val mmio_resend_gpaddr = RegInit(0.U(GPAddrBits.W)) 583c3b2d83aSJay 5841d1e6d4dSJenius //last instuction finish 5851d1e6d4dSJenius val is_first_instr = RegInit(true.B) 586ba5ba1dcSmy-mayfly /*** Determine whether the MMIO instruction is executable based on the previous prediction block ***/ 587ba5ba1dcSmy-mayfly io.mmioCommitRead.mmioFtqPtr := RegNext(f3_ftq_req.ftqIdx - 1.U) 588a37fbf10SJay 5891d1e6d4dSJenius 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) 590ee175d78SJay val mmio_state = RegInit(m_idle) 591a37fbf10SJay 5929bae7d6eSJay val f3_req_is_mmio = f3_mmio && f3_valid 5932a3050c2SJay 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 594ee175d78SJay val f3_mmio_req_commit = f3_req_is_mmio && mmio_state === m_commited 595a37fbf10SJay 596ee175d78SJay val f3_mmio_to_commit = f3_req_is_mmio && mmio_state === m_waitCommit 597a37fbf10SJay val f3_mmio_to_commit_next = RegNext(f3_mmio_to_commit) 598a37fbf10SJay val f3_mmio_can_go = f3_mmio_to_commit && !f3_mmio_to_commit_next 599a37fbf10SJay 6000c70648eSEaston Man val fromFtqRedirectReg = Wire(fromFtq.redirect.cloneType) 6010c70648eSEaston Man fromFtqRedirectReg.bits := RegEnable(fromFtq.redirect.bits, 0.U.asTypeOf(fromFtq.redirect.bits), fromFtq.redirect.valid) 6020c70648eSEaston Man fromFtqRedirectReg.valid := RegNext(fromFtq.redirect.valid, init = false.B) 6034a74a727SJenius val mmioF3Flush = RegNext(f3_flush,init = false.B) 60456788a33SJinYue val f3_ftq_flush_self = fromFtqRedirectReg.valid && RedirectLevel.flushItself(fromFtqRedirectReg.bits.level) 60556788a33SJinYue val f3_ftq_flush_by_older = fromFtqRedirectReg.valid && isBefore(fromFtqRedirectReg.bits.ftqIdx, f3_ftq_req.ftqIdx) 6069bae7d6eSJay 60756788a33SJinYue val f3_need_not_flush = f3_req_is_mmio && fromFtqRedirectReg.valid && !f3_ftq_flush_self && !f3_ftq_flush_by_older 6089bae7d6eSJay 609ba5ba1dcSmy-mayfly /** 610ba5ba1dcSmy-mayfly ********************************************************************************** 611ba5ba1dcSmy-mayfly * We want to defer instruction fetching when encountering MMIO instructions to ensure that the MMIO region is not negatively impacted. 612ba5ba1dcSmy-mayfly * This is the exception when the first instruction is an MMIO instruction. 613ba5ba1dcSmy-mayfly ********************************************************************************** 614ba5ba1dcSmy-mayfly */ 615ba5ba1dcSmy-mayfly when(is_first_instr && f3_fire){ 6161d1e6d4dSJenius is_first_instr := false.B 6171d1e6d4dSJenius } 6181d1e6d4dSJenius 6194a74a727SJenius when(f3_flush && !f3_req_is_mmio) {f3_valid := false.B} 6204a74a727SJenius .elsewhen(mmioF3Flush && f3_req_is_mmio && !f3_need_not_flush) {f3_valid := false.B} 621a37fbf10SJay .elsewhen(f2_fire && !f2_flush ) {f3_valid := true.B } 622935edac4STang Haojin .elsewhen(io.toIbuffer.fire && !f3_req_is_mmio) {f3_valid := false.B} 623a37fbf10SJay .elsewhen{f3_req_is_mmio && f3_mmio_req_commit} {f3_valid := false.B} 624a37fbf10SJay 625a37fbf10SJay val f3_mmio_use_seq_pc = RegInit(false.B) 626a37fbf10SJay 62756788a33SJinYue val (redirect_ftqIdx, redirect_ftqOffset) = (fromFtqRedirectReg.bits.ftqIdx,fromFtqRedirectReg.bits.ftqOffset) 62856788a33SJinYue val redirect_mmio_req = fromFtqRedirectReg.valid && redirect_ftqIdx === f3_ftq_req.ftqIdx && redirect_ftqOffset === 0.U 629a37fbf10SJay 630a37fbf10SJay when(RegNext(f2_fire && !f2_flush) && f3_req_is_mmio) { f3_mmio_use_seq_pc := true.B } 631a37fbf10SJay .elsewhen(redirect_mmio_req) { f3_mmio_use_seq_pc := false.B } 632a37fbf10SJay 6338c192ff7Sxu_zh f3_ready := (io.toIbuffer.ready && (f3_mmio_req_commit || !f3_req_is_mmio)) || !f3_valid 634a37fbf10SJay 6351d1e6d4dSJenius // mmio state machine 636a37fbf10SJay switch(mmio_state){ 637ee175d78SJay is(m_idle){ 6389bae7d6eSJay when(f3_req_is_mmio){ 6391d1e6d4dSJenius mmio_state := m_waitLastCmt 6401d1e6d4dSJenius } 6411d1e6d4dSJenius } 6421d1e6d4dSJenius 6431d1e6d4dSJenius is(m_waitLastCmt){ 6441d1e6d4dSJenius when(is_first_instr){ 645ee175d78SJay mmio_state := m_sendReq 6461d1e6d4dSJenius }.otherwise{ 6471d1e6d4dSJenius mmio_state := Mux(io.mmioCommitRead.mmioLastCommit, m_sendReq, m_waitLastCmt) 648a37fbf10SJay } 649a37fbf10SJay } 650a37fbf10SJay 651ee175d78SJay is(m_sendReq){ 652935edac4STang Haojin mmio_state := Mux(toUncache.fire, m_waitResp, m_sendReq) 653a37fbf10SJay } 654a37fbf10SJay 655ee175d78SJay is(m_waitResp){ 656935edac4STang Haojin when(fromUncache.fire){ 657a37fbf10SJay val isRVC = fromUncache.bits.data(1,0) =/= 3.U 658d7ac23a3SEaston Man val needResend = !isRVC && f3_paddrs(0)(2,1) === 3.U 659ee175d78SJay mmio_state := Mux(needResend, m_sendTLB, m_waitCommit) 660ee175d78SJay mmio_is_RVC := isRVC 661ee175d78SJay f3_mmio_data(0) := fromUncache.bits.data(15,0) 662ee175d78SJay f3_mmio_data(1) := fromUncache.bits.data(31,16) 663a37fbf10SJay } 664a37fbf10SJay } 665a37fbf10SJay 666ee175d78SJay is(m_sendTLB){ 6677b7232f9Sxu_zh mmio_state := Mux(io.iTLBInter.req.fire, m_tlbResp, m_sendTLB) 668c3b2d83aSJay } 669a37fbf10SJay 670ee175d78SJay is(m_tlbResp){ 6717b7232f9Sxu_zh when(io.iTLBInter.resp.fire) { 6727b7232f9Sxu_zh // we are using a blocked tlb, so resp.fire must have !resp.bits.miss 6737b7232f9Sxu_zh assert(!io.iTLBInter.resp.bits.miss, "blocked mode iTLB miss when resp.fire") 67488895b11Sxu_zh val tlb_exception = ExceptionType.fromTlbResp(io.iTLBInter.resp.bits) 6757b7232f9Sxu_zh // if tlb has exception, abort checking pmp, just send instr & exception to ibuffer and wait for commit 67688895b11Sxu_zh mmio_state := Mux(tlb_exception === ExceptionType.none, m_sendPMP, m_waitCommit) 6777b7232f9Sxu_zh // also save itlb response 67803efd994Shappy-lx mmio_resend_addr := io.iTLBInter.resp.bits.paddr(0) 67988895b11Sxu_zh mmio_resend_exception := tlb_exception 680b5a614b9Sxu_zh mmio_resend_gpaddr := io.iTLBInter.resp.bits.gpaddr(0) 681ee175d78SJay } 6827b7232f9Sxu_zh } 683ee175d78SJay 684ee175d78SJay is(m_sendPMP){ 68588895b11Sxu_zh // if pmp re-check does not respond mmio, must be access fault 68688895b11Sxu_zh val pmp_exception = Mux(io.pmp.resp.mmio, ExceptionType.fromPMPResp(io.pmp.resp), ExceptionType.af) 68788895b11Sxu_zh // if pmp has exception, abort sending request, just send instr & exception to ibuffer and wait for commit 68888895b11Sxu_zh mmio_state := Mux(pmp_exception === ExceptionType.none, m_resendReq, m_waitCommit) 68988895b11Sxu_zh // also save pmp response 69088895b11Sxu_zh mmio_resend_exception := pmp_exception 691ee175d78SJay } 692ee175d78SJay 693ee175d78SJay is(m_resendReq){ 694935edac4STang Haojin mmio_state := Mux(toUncache.fire, m_waitResendResp, m_resendReq) 695ee175d78SJay } 696ee175d78SJay 697ee175d78SJay is(m_waitResendResp) { 698935edac4STang Haojin when(fromUncache.fire) { 699ee175d78SJay mmio_state := m_waitCommit 700ee175d78SJay f3_mmio_data(1) := fromUncache.bits.data(15,0) 701a37fbf10SJay } 702a37fbf10SJay } 703a37fbf10SJay 704ee175d78SJay is(m_waitCommit) { 7057b7232f9Sxu_zh mmio_state := Mux(mmio_commit, m_commited, m_waitCommit) 706a37fbf10SJay } 7072a3050c2SJay 708ee175d78SJay //normal mmio instruction 709ee175d78SJay is(m_commited) { 710ee175d78SJay mmio_state := m_idle 711ee175d78SJay mmio_is_RVC := false.B 712ee175d78SJay mmio_resend_addr := 0.U 71388895b11Sxu_zh mmio_resend_exception := ExceptionType.none 714b5a614b9Sxu_zh mmio_resend_gpaddr := 0.U 7152a3050c2SJay } 716a37fbf10SJay } 717a37fbf10SJay 7188abe1810SEaston Man // Exception or flush by older branch prediction 7198abe1810SEaston Man // Condition is from RegNext(fromFtq.redirect), 1 cycle after backend rediect 720167bcd01SJay when(f3_ftq_flush_self || f3_ftq_flush_by_older) { 721ee175d78SJay mmio_state := m_idle 722ee175d78SJay mmio_is_RVC := false.B 723ee175d78SJay mmio_resend_addr := 0.U 72488895b11Sxu_zh mmio_resend_exception := ExceptionType.none 725b5a614b9Sxu_zh mmio_resend_gpaddr := 0.U 726ee175d78SJay f3_mmio_data.map(_ := 0.U) 7279bae7d6eSJay } 7289bae7d6eSJay 729ee175d78SJay toUncache.valid := ((mmio_state === m_sendReq) || (mmio_state === m_resendReq)) && f3_req_is_mmio 730d7ac23a3SEaston Man toUncache.bits.addr := Mux((mmio_state === m_resendReq), mmio_resend_addr, f3_paddrs(0)) 731a37fbf10SJay fromUncache.ready := true.B 732a37fbf10SJay 7337b7232f9Sxu_zh // send itlb request in m_sendTLB state 734ee175d78SJay io.iTLBInter.req.valid := (mmio_state === m_sendTLB) && f3_req_is_mmio 735ee175d78SJay io.iTLBInter.req.bits.size := 3.U 736ee175d78SJay io.iTLBInter.req.bits.vaddr := f3_resend_vaddr 737ee175d78SJay io.iTLBInter.req.bits.debug.pc := f3_resend_vaddr 7387b7232f9Sxu_zh io.iTLBInter.req.bits.cmd := TlbCmd.exec 7397b7232f9Sxu_zh io.iTLBInter.req.bits.kill := false.B // IFU use itlb for mmio, doesn't need sync, set it to false 7407b7232f9Sxu_zh io.iTLBInter.req.bits.no_translate := false.B 741*db6cfb5aSHaoyuan Feng io.iTLBInter.req.bits.fullva := 0.U 742*db6cfb5aSHaoyuan Feng io.iTLBInter.req.bits.checkfullva := false.B 743d0de7e4aSpeixiaokun io.iTLBInter.req.bits.hyperinst := DontCare 744d0de7e4aSpeixiaokun io.iTLBInter.req.bits.hlvx := DontCare 7458744445eSMaxpicca-Li io.iTLBInter.req.bits.memidx := DontCare 746f1fe8698SLemover io.iTLBInter.req.bits.debug.robIdx := DontCare 747ee175d78SJay io.iTLBInter.req.bits.debug.isFirstIssue := DontCare 748149a2326Sweiding liu io.iTLBInter.req.bits.pmp_addr := DontCare 7497b7232f9Sxu_zh // whats the difference between req_kill and req.bits.kill? 7507b7232f9Sxu_zh io.iTLBInter.req_kill := false.B 7517b7232f9Sxu_zh // wait for itlb response in m_tlbResp state 7527b7232f9Sxu_zh io.iTLBInter.resp.ready := (mmio_state === m_tlbResp) && f3_req_is_mmio 753ee175d78SJay 754ee175d78SJay io.pmp.req.valid := (mmio_state === m_sendPMP) && f3_req_is_mmio 755ee175d78SJay io.pmp.req.bits.addr := mmio_resend_addr 756ee175d78SJay io.pmp.req.bits.size := 3.U 757ee175d78SJay io.pmp.req.bits.cmd := TlbCmd.exec 758f7c29b0aSJinYue 7592a3050c2SJay val f3_lastHalf = RegInit(0.U.asTypeOf(new LastHalfInfo)) 76009c6f1ddSLingrui98 76109c6f1ddSLingrui98 val f3_predecode_range = VecInit(preDecoderOut.pd.map(inst => inst.valid)).asUInt 7620be662e4SJay val f3_mmio_range = VecInit((0 until PredictWidth).map(i => if(i ==0) true.B else false.B)) 7632a3050c2SJay val f3_instr_valid = Wire(Vec(PredictWidth, Bool())) 76409c6f1ddSLingrui98 7652a3050c2SJay /*** prediction result check ***/ 7662a3050c2SJay checkerIn.ftqOffset := f3_ftq_req.ftqOffset 7672a3050c2SJay checkerIn.jumpOffset := f3_jump_offset 7686ce52296SJinYue checkerIn.target := f3_ftq_req.nextStartAddr 7692a3050c2SJay checkerIn.instrRange := f3_instr_range.asTypeOf(Vec(PredictWidth, Bool())) 7702a3050c2SJay checkerIn.instrValid := f3_instr_valid.asTypeOf(Vec(PredictWidth, Bool())) 7712a3050c2SJay checkerIn.pds := f3_pd 7722a3050c2SJay checkerIn.pc := f3_pc 7730c70648eSEaston Man checkerIn.fire_in := RegNext(f2_fire, init = false.B) 7742a3050c2SJay 77558dbdfc2SJay /*** handle half RVI in the last 2 Bytes ***/ 7762a3050c2SJay 7772a3050c2SJay def hasLastHalf(idx: UInt) = { 7785995c9e7SJenius //!f3_pd(idx).isRVC && checkerOutStage1.fixedRange(idx) && f3_instr_valid(idx) && !checkerOutStage1.fixedTaken(idx) && !checkerOutStage2.fixedMissPred(idx) && ! f3_req_is_mmio 7795995c9e7SJenius !f3_pd(idx).isRVC && checkerOutStage1.fixedRange(idx) && f3_instr_valid(idx) && !checkerOutStage1.fixedTaken(idx) && ! f3_req_is_mmio 7802a3050c2SJay } 7812a3050c2SJay 782b665b650STang Haojin val f3_last_validIdx = ParallelPosteriorityEncoder(checkerOutStage1.fixedRange) 7832a3050c2SJay 7842a3050c2SJay val f3_hasLastHalf = hasLastHalf((PredictWidth - 1).U) 7852a3050c2SJay val f3_false_lastHalf = hasLastHalf(f3_last_validIdx) 7862a3050c2SJay val f3_false_snpc = f3_half_snpc(f3_last_validIdx) 7872a3050c2SJay 788935edac4STang Haojin val f3_lastHalf_mask = VecInit((0 until PredictWidth).map( i => if(i ==0) false.B else true.B )).asUInt 7893f785aa3SJenius val f3_lastHalf_disable = RegInit(false.B) 7902a3050c2SJay 791804985a5SJenius when(f3_flush || (f3_fire && f3_lastHalf_disable)){ 792804985a5SJenius f3_lastHalf_disable := false.B 793804985a5SJenius } 794804985a5SJenius 7952a3050c2SJay when (f3_flush) { 7962a3050c2SJay f3_lastHalf.valid := false.B 7972a3050c2SJay }.elsewhen (f3_fire) { 7983f785aa3SJenius f3_lastHalf.valid := f3_hasLastHalf && !f3_lastHalf_disable 7996ce52296SJinYue f3_lastHalf.middlePC := f3_ftq_req.nextStartAddr 8002a3050c2SJay } 8012a3050c2SJay 8022a3050c2SJay f3_instr_valid := Mux(f3_lastHalf.valid,f3_hasHalfValid ,VecInit(f3_pd.map(inst => inst.valid))) 8032a3050c2SJay 8042a3050c2SJay /*** frontend Trigger ***/ 8052a3050c2SJay frontendTrigger.io.pds := f3_pd 8062a3050c2SJay frontendTrigger.io.pc := f3_pc 8072a3050c2SJay frontendTrigger.io.data := f3_cut_data 8082a3050c2SJay 8092a3050c2SJay frontendTrigger.io.frontendTrigger := io.frontendTrigger 8102a3050c2SJay 8112a3050c2SJay val f3_triggered = frontendTrigger.io.triggered 81291946104Sxu_zh val f3_toIbuffer_valid = f3_valid && (!f3_req_is_mmio || f3_mmio_can_go) && !f3_flush 8132a3050c2SJay 8142a3050c2SJay /*** send to Ibuffer ***/ 81591946104Sxu_zh io.toIbuffer.valid := f3_toIbuffer_valid 8162a3050c2SJay io.toIbuffer.bits.instrs := f3_expd_instr 8172a3050c2SJay io.toIbuffer.bits.valid := f3_instr_valid.asUInt 8185995c9e7SJenius io.toIbuffer.bits.enqEnable := checkerOutStage1.fixedRange.asUInt & f3_instr_valid.asUInt 8192a3050c2SJay io.toIbuffer.bits.pd := f3_pd 82009c6f1ddSLingrui98 io.toIbuffer.bits.ftqPtr := f3_ftq_req.ftqIdx 8212a3050c2SJay io.toIbuffer.bits.pc := f3_pc 8225995c9e7SJenius io.toIbuffer.bits.ftqOffset.zipWithIndex.map{case(a, i) => a.bits := i.U; a.valid := checkerOutStage1.fixedTaken(i) && !f3_req_is_mmio} 8232a3050c2SJay io.toIbuffer.bits.foldpc := f3_foldpc 824a2568a60Sxu_zh io.toIbuffer.bits.exceptionType := ExceptionType.merge(f3_exception_vec, f3_crossPage_exception_vec) 825c1b28b66STang Haojin // exceptionFromBackend only needs to be set for the first instruction. 826c1b28b66STang Haojin // Other instructions in the same block may have pf or af set, 827c1b28b66STang Haojin // which is a side effect of the first instruction and actually not necessary. 828c1b28b66STang Haojin io.toIbuffer.bits.exceptionFromBackend := (0 until PredictWidth).map { 829c1b28b66STang Haojin case 0 => f3_except_fromBackend 830c1b28b66STang Haojin case _ => false.B 831c1b28b66STang Haojin } 832a2568a60Sxu_zh io.toIbuffer.bits.crossPageIPFFix := f3_crossPage_exception_vec.map(_ =/= ExceptionType.none) 83392c61038SXuan Hu io.toIbuffer.bits.illegalInstr:= f3_ill 8342a3050c2SJay io.toIbuffer.bits.triggered := f3_triggered 8352a3050c2SJay 8362a3050c2SJay when(f3_lastHalf.valid){ 8375995c9e7SJenius io.toIbuffer.bits.enqEnable := checkerOutStage1.fixedRange.asUInt & f3_instr_valid.asUInt & f3_lastHalf_mask 8382a3050c2SJay io.toIbuffer.bits.valid := f3_lastHalf_mask & f3_instr_valid.asUInt 8392a3050c2SJay } 8402a3050c2SJay 841d7ac23a3SEaston Man /** to backend */ 84291946104Sxu_zh // f3_gpaddr is valid iff gpf is detected 843b5a614b9Sxu_zh io.toBackend.gpaddrMem_wen := f3_toIbuffer_valid && Mux( 844b5a614b9Sxu_zh f3_req_is_mmio, 84588895b11Sxu_zh mmio_resend_exception === ExceptionType.gpf, 84688895b11Sxu_zh f3_exception.map(_ === ExceptionType.gpf).reduce(_||_) 847b5a614b9Sxu_zh ) 848d7ac23a3SEaston Man io.toBackend.gpaddrMem_waddr := f3_ftq_req.ftqIdx.value 849b5a614b9Sxu_zh io.toBackend.gpaddrMem_wdata := Mux(f3_req_is_mmio, mmio_resend_gpaddr, f3_gpaddr) 85009c6f1ddSLingrui98 85109c6f1ddSLingrui98 //Write back to Ftq 852a37fbf10SJay val f3_cache_fetch = f3_valid && !(f2_fire && !f2_flush) 853a37fbf10SJay val finishFetchMaskReg = RegNext(f3_cache_fetch) 854a37fbf10SJay 8552a3050c2SJay val mmioFlushWb = Wire(Valid(new PredecodeWritebackBundle)) 8560be662e4SJay val f3_mmio_missOffset = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 857a37fbf10SJay f3_mmio_missOffset.valid := f3_req_is_mmio 8580be662e4SJay f3_mmio_missOffset.bits := 0.U 8590be662e4SJay 8608abe1810SEaston Man // Send mmioFlushWb back to FTQ 1 cycle after uncache fetch return 8618abe1810SEaston Man // When backend redirect, mmio_state reset after 1 cycle. 8628abe1810SEaston Man // In this case, mask .valid to avoid overriding backend redirect 8638abe1810SEaston Man mmioFlushWb.valid := (f3_req_is_mmio && mmio_state === m_waitCommit && RegNext(fromUncache.fire) && 8648abe1810SEaston Man f3_mmio_use_seq_pc && !f3_ftq_flush_self && !f3_ftq_flush_by_older) 8652a3050c2SJay mmioFlushWb.bits.pc := f3_pc 8662a3050c2SJay mmioFlushWb.bits.pd := f3_pd 8672a3050c2SJay mmioFlushWb.bits.pd.zipWithIndex.map{case(instr,i) => instr.valid := f3_mmio_range(i)} 8682a3050c2SJay mmioFlushWb.bits.ftqIdx := f3_ftq_req.ftqIdx 8692a3050c2SJay mmioFlushWb.bits.ftqOffset := f3_ftq_req.ftqOffset.bits 8702a3050c2SJay mmioFlushWb.bits.misOffset := f3_mmio_missOffset 8712a3050c2SJay mmioFlushWb.bits.cfiOffset := DontCare 872ee175d78SJay mmioFlushWb.bits.target := Mux(mmio_is_RVC, f3_ftq_req.startAddr + 2.U , f3_ftq_req.startAddr + 4.U) 8732a3050c2SJay mmioFlushWb.bits.jalTarget := DontCare 8742a3050c2SJay mmioFlushWb.bits.instrRange := f3_mmio_range 87509c6f1ddSLingrui98 87673e96011SXuan Hu val mmioRVCExpander = Module(new RVCExpander) 87773e96011SXuan Hu mmioRVCExpander.io.in := Mux(f3_req_is_mmio, Cat(f3_mmio_data(1), f3_mmio_data(0)), 0.U) 87873e96011SXuan Hu 8792dfa9e76SJenius /** external predecode for MMIO instruction */ 8802dfa9e76SJenius when(f3_req_is_mmio){ 8812dfa9e76SJenius val inst = Cat(f3_mmio_data(1), f3_mmio_data(0)) 8822dfa9e76SJenius val currentIsRVC = isRVC(inst) 8832dfa9e76SJenius 8842dfa9e76SJenius val brType::isCall::isRet::Nil = brInfo(inst) 8852dfa9e76SJenius val jalOffset = jal_offset(inst, currentIsRVC) 8862dfa9e76SJenius val brOffset = br_offset(inst, currentIsRVC) 8872dfa9e76SJenius 88873e96011SXuan Hu io.toIbuffer.bits.instrs(0) := Mux(mmioRVCExpander.io.ill, mmioRVCExpander.io.in, mmioRVCExpander.io.out.bits) 8892dfa9e76SJenius 8902dfa9e76SJenius io.toIbuffer.bits.pd(0).valid := true.B 8912dfa9e76SJenius io.toIbuffer.bits.pd(0).isRVC := currentIsRVC 8922dfa9e76SJenius io.toIbuffer.bits.pd(0).brType := brType 8932dfa9e76SJenius io.toIbuffer.bits.pd(0).isCall := isCall 8942dfa9e76SJenius io.toIbuffer.bits.pd(0).isRet := isRet 8952dfa9e76SJenius 89688895b11Sxu_zh io.toIbuffer.bits.exceptionType(0) := mmio_resend_exception 897a2568a60Sxu_zh io.toIbuffer.bits.crossPageIPFFix(0) := mmio_resend_exception =/= ExceptionType.none 89873e96011SXuan Hu io.toIbuffer.bits.illegalInstr(0) := mmioRVCExpander.io.ill 8992dfa9e76SJenius 9002dfa9e76SJenius io.toIbuffer.bits.enqEnable := f3_mmio_range.asUInt 9012dfa9e76SJenius 9022dfa9e76SJenius mmioFlushWb.bits.pd(0).valid := true.B 9032dfa9e76SJenius mmioFlushWb.bits.pd(0).isRVC := currentIsRVC 9042dfa9e76SJenius mmioFlushWb.bits.pd(0).brType := brType 9052dfa9e76SJenius mmioFlushWb.bits.pd(0).isCall := isCall 9062dfa9e76SJenius mmioFlushWb.bits.pd(0).isRet := isRet 9072dfa9e76SJenius } 9082dfa9e76SJenius 909935edac4STang Haojin mmio_redirect := (f3_req_is_mmio && mmio_state === m_waitCommit && RegNext(fromUncache.fire) && f3_mmio_use_seq_pc) 91009c6f1ddSLingrui98 91100240ba6SJay XSPerfAccumulate("fetch_bubble_ibuffer_not_ready", io.toIbuffer.valid && !io.toIbuffer.ready ) 91200240ba6SJay 91300240ba6SJay 91458dbdfc2SJay /** 91558dbdfc2SJay ****************************************************************************** 91658dbdfc2SJay * IFU Write Back Stage 91758dbdfc2SJay * - write back predecode information to Ftq to update 91858dbdfc2SJay * - redirect if found fault prediction 91958dbdfc2SJay * - redirect if has false hit last half (last PC is not start + 32 Bytes, but in the midle of an notCFI RVI instruction) 92058dbdfc2SJay ****************************************************************************** 9212a3050c2SJay */ 9220c70648eSEaston Man val wb_enable = RegNext(f2_fire && !f2_flush) && !f3_req_is_mmio && !f3_flush 9230c70648eSEaston Man val wb_valid = RegNext(wb_enable, init = false.B) 9240c70648eSEaston Man val wb_ftq_req = RegEnable(f3_ftq_req, wb_enable) 92558dbdfc2SJay 9260c70648eSEaston Man val wb_check_result_stage1 = RegEnable(checkerOutStage1, wb_enable) 9275995c9e7SJenius val wb_check_result_stage2 = checkerOutStage2 9280c70648eSEaston Man val wb_instr_range = RegEnable(io.toIbuffer.bits.enqEnable, wb_enable) 929e4d2f6a9Smy-mayfly 930e4d2f6a9Smy-mayfly val wb_pc_lower_result = RegEnable(f3_pc_lower_result, wb_enable) 931e4d2f6a9Smy-mayfly val wb_pc_high = RegEnable(f3_pc_high, wb_enable) 932e4d2f6a9Smy-mayfly val wb_pc_high_plus1 = RegEnable(f3_pc_high_plus1, wb_enable) 933e4d2f6a9Smy-mayfly val wb_pc = CatPC(wb_pc_lower_result, wb_pc_high, wb_pc_high_plus1) 934e4d2f6a9Smy-mayfly 935e4d2f6a9Smy-mayfly //val wb_pc = RegEnable(f3_pc, wb_enable) 9360c70648eSEaston Man val wb_pd = RegEnable(f3_pd, wb_enable) 9370c70648eSEaston Man val wb_instr_valid = RegEnable(f3_instr_valid, wb_enable) 9382a3050c2SJay 9392a3050c2SJay /* false hit lastHalf */ 9400c70648eSEaston Man val wb_lastIdx = RegEnable(f3_last_validIdx, wb_enable) 9410c70648eSEaston Man val wb_false_lastHalf = RegEnable(f3_false_lastHalf, wb_enable) && wb_lastIdx =/= (PredictWidth - 1).U 9420c70648eSEaston Man val wb_false_target = RegEnable(f3_false_snpc, wb_enable) 9432a3050c2SJay 9442a3050c2SJay val wb_half_flush = wb_false_lastHalf 9452a3050c2SJay val wb_half_target = wb_false_target 9462a3050c2SJay 947a1351e5dSJay /* false oversize */ 948a1351e5dSJay val lastIsRVC = wb_instr_range.asTypeOf(Vec(PredictWidth,Bool())).last && wb_pd.last.isRVC 949a1351e5dSJay val lastIsRVI = wb_instr_range.asTypeOf(Vec(PredictWidth,Bool()))(PredictWidth - 2) && !wb_pd(PredictWidth - 2).isRVC 9505995c9e7SJenius val lastTaken = wb_check_result_stage1.fixedTaken.last 951a1351e5dSJay 9522a3050c2SJay f3_wb_not_flush := wb_ftq_req.ftqIdx === f3_ftq_req.ftqIdx && f3_valid && wb_valid 9532a3050c2SJay 9543f785aa3SJenius /** if a req with a last half but miss predicted enters in wb stage, and this cycle f3 stalls, 9553f785aa3SJenius * we set a flag to notify f3 that the last half flag need not to be set. 9563f785aa3SJenius */ 957804985a5SJenius //f3_fire is after wb_valid 958076dea5fSJenius when(wb_valid && RegNext(f3_hasLastHalf,init = false.B) 959251a37e4SJenius && wb_check_result_stage2.fixedMissPred(PredictWidth - 1) && !f3_fire && !RegNext(f3_fire,init = false.B) && !f3_flush 9603f785aa3SJenius ){ 9613f785aa3SJenius f3_lastHalf_disable := true.B 962ab6202e2SJenius } 963ab6202e2SJenius 964804985a5SJenius //wb_valid and f3_fire are in same cycle 965076dea5fSJenius when(wb_valid && RegNext(f3_hasLastHalf,init = false.B) 966076dea5fSJenius && wb_check_result_stage2.fixedMissPred(PredictWidth - 1) && f3_fire 967804985a5SJenius ){ 968804985a5SJenius f3_lastHalf.valid := false.B 969804985a5SJenius } 970804985a5SJenius 9712a3050c2SJay val checkFlushWb = Wire(Valid(new PredecodeWritebackBundle)) 972b665b650STang Haojin val checkFlushWbjalTargetIdx = ParallelPriorityEncoder(VecInit(wb_pd.zip(wb_instr_valid).map{case (pd, v) => v && pd.isJal })) 973b665b650STang Haojin val checkFlushWbTargetIdx = ParallelPriorityEncoder(wb_check_result_stage2.fixedMissPred) 9742a3050c2SJay checkFlushWb.valid := wb_valid 9752a3050c2SJay checkFlushWb.bits.pc := wb_pc 9762a3050c2SJay checkFlushWb.bits.pd := wb_pd 9772a3050c2SJay checkFlushWb.bits.pd.zipWithIndex.map{case(instr,i) => instr.valid := wb_instr_valid(i)} 9782a3050c2SJay checkFlushWb.bits.ftqIdx := wb_ftq_req.ftqIdx 9792a3050c2SJay checkFlushWb.bits.ftqOffset := wb_ftq_req.ftqOffset.bits 9805995c9e7SJenius checkFlushWb.bits.misOffset.valid := ParallelOR(wb_check_result_stage2.fixedMissPred) || wb_half_flush 9815995c9e7SJenius checkFlushWb.bits.misOffset.bits := Mux(wb_half_flush, wb_lastIdx, ParallelPriorityEncoder(wb_check_result_stage2.fixedMissPred)) 9825995c9e7SJenius checkFlushWb.bits.cfiOffset.valid := ParallelOR(wb_check_result_stage1.fixedTaken) 9835995c9e7SJenius checkFlushWb.bits.cfiOffset.bits := ParallelPriorityEncoder(wb_check_result_stage1.fixedTaken) 984b665b650STang Haojin checkFlushWb.bits.target := Mux(wb_half_flush, wb_half_target, wb_check_result_stage2.fixedTarget(checkFlushWbTargetIdx)) 985d10ddd67SGuokai Chen checkFlushWb.bits.jalTarget := wb_check_result_stage2.jalTarget(checkFlushWbjalTargetIdx) 9862a3050c2SJay checkFlushWb.bits.instrRange := wb_instr_range.asTypeOf(Vec(PredictWidth, Bool())) 9872a3050c2SJay 988bccc5520SJenius toFtq.pdWb := Mux(wb_valid, checkFlushWb, mmioFlushWb) 9892a3050c2SJay 9902a3050c2SJay wb_redirect := checkFlushWb.bits.misOffset.valid && wb_valid 99109c6f1ddSLingrui98 9925b3c20f7SJinYue /*write back flush type*/ 9935995c9e7SJenius val checkFaultType = wb_check_result_stage2.faultType 9945b3c20f7SJinYue val checkJalFault = wb_valid && checkFaultType.map(_.isjalFault).reduce(_||_) 9955b3c20f7SJinYue val checkRetFault = wb_valid && checkFaultType.map(_.isRetFault).reduce(_||_) 9965b3c20f7SJinYue val checkTargetFault = wb_valid && checkFaultType.map(_.istargetFault).reduce(_||_) 9975b3c20f7SJinYue val checkNotCFIFault = wb_valid && checkFaultType.map(_.notCFIFault).reduce(_||_) 9985b3c20f7SJinYue val checkInvalidTaken = wb_valid && checkFaultType.map(_.invalidTakenFault).reduce(_||_) 9995b3c20f7SJinYue 10005b3c20f7SJinYue 10015b3c20f7SJinYue XSPerfAccumulate("predecode_flush_jalFault", checkJalFault ) 10025b3c20f7SJinYue XSPerfAccumulate("predecode_flush_retFault", checkRetFault ) 10035b3c20f7SJinYue XSPerfAccumulate("predecode_flush_targetFault", checkTargetFault ) 10045b3c20f7SJinYue XSPerfAccumulate("predecode_flush_notCFIFault", checkNotCFIFault ) 10055b3c20f7SJinYue XSPerfAccumulate("predecode_flush_incalidTakenFault", checkInvalidTaken ) 10065b3c20f7SJinYue 10075b3c20f7SJinYue when(checkRetFault){ 10085b3c20f7SJinYue XSDebug("startAddr:%x nextstartAddr:%x taken:%d takenIdx:%d\n", 10095b3c20f7SJinYue wb_ftq_req.startAddr, wb_ftq_req.nextStartAddr, wb_ftq_req.ftqOffset.valid, wb_ftq_req.ftqOffset.bits) 10105b3c20f7SJinYue } 10115b3c20f7SJinYue 101251532d8bSGuokai Chen 10131d8f4dcbSJay /** performance counter */ 1014005e809bSJiuyang Liu val f3_perf_info = RegEnable(f2_perf_info, f2_fire) 1015935edac4STang Haojin val f3_req_0 = io.toIbuffer.fire 1016935edac4STang Haojin val f3_req_1 = io.toIbuffer.fire && f3_doubleLine 1017935edac4STang Haojin val f3_hit_0 = io.toIbuffer.fire && f3_perf_info.bank_hit(0) 1018935edac4STang Haojin val f3_hit_1 = io.toIbuffer.fire && f3_doubleLine & f3_perf_info.bank_hit(1) 10191d8f4dcbSJay val f3_hit = f3_perf_info.hit 1020cd365d4cSrvcoresjw val perfEvents = Seq( 10212a3050c2SJay ("frontendFlush ", wb_redirect ), 1022935edac4STang Haojin ("ifu_req ", io.toIbuffer.fire ), 1023935edac4STang Haojin ("ifu_miss ", io.toIbuffer.fire && !f3_perf_info.hit ), 1024cd365d4cSrvcoresjw ("ifu_req_cacheline_0 ", f3_req_0 ), 1025cd365d4cSrvcoresjw ("ifu_req_cacheline_1 ", f3_req_1 ), 1026cd365d4cSrvcoresjw ("ifu_req_cacheline_0_hit ", f3_hit_1 ), 1027cd365d4cSrvcoresjw ("ifu_req_cacheline_1_hit ", f3_hit_1 ), 1028935edac4STang Haojin ("only_0_hit ", f3_perf_info.only_0_hit && io.toIbuffer.fire ), 1029935edac4STang Haojin ("only_0_miss ", f3_perf_info.only_0_miss && io.toIbuffer.fire ), 1030935edac4STang Haojin ("hit_0_hit_1 ", f3_perf_info.hit_0_hit_1 && io.toIbuffer.fire ), 1031935edac4STang Haojin ("hit_0_miss_1 ", f3_perf_info.hit_0_miss_1 && io.toIbuffer.fire ), 1032935edac4STang Haojin ("miss_0_hit_1 ", f3_perf_info.miss_0_hit_1 && io.toIbuffer.fire ), 1033935edac4STang Haojin ("miss_0_miss_1 ", f3_perf_info.miss_0_miss_1 && io.toIbuffer.fire ), 1034cd365d4cSrvcoresjw ) 10351ca0e4f3SYinan Xu generatePerfEvent() 103609c6f1ddSLingrui98 1037935edac4STang Haojin XSPerfAccumulate("ifu_req", io.toIbuffer.fire ) 1038935edac4STang Haojin XSPerfAccumulate("ifu_miss", io.toIbuffer.fire && !f3_hit ) 1039f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_0", f3_req_0 ) 1040f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_1", f3_req_1 ) 1041f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_0_hit", f3_hit_0 ) 1042f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_1_hit", f3_hit_1 ) 10432a3050c2SJay XSPerfAccumulate("frontendFlush", wb_redirect ) 1044935edac4STang Haojin XSPerfAccumulate("only_0_hit", f3_perf_info.only_0_hit && io.toIbuffer.fire ) 1045935edac4STang Haojin XSPerfAccumulate("only_0_miss", f3_perf_info.only_0_miss && io.toIbuffer.fire ) 1046935edac4STang Haojin XSPerfAccumulate("hit_0_hit_1", f3_perf_info.hit_0_hit_1 && io.toIbuffer.fire ) 1047935edac4STang Haojin XSPerfAccumulate("hit_0_miss_1", f3_perf_info.hit_0_miss_1 && io.toIbuffer.fire ) 1048935edac4STang Haojin XSPerfAccumulate("miss_0_hit_1", f3_perf_info.miss_0_hit_1 && io.toIbuffer.fire ) 1049935edac4STang Haojin XSPerfAccumulate("miss_0_miss_1", f3_perf_info.miss_0_miss_1 && io.toIbuffer.fire ) 1050935edac4STang Haojin XSPerfAccumulate("hit_0_except_1", f3_perf_info.hit_0_except_1 && io.toIbuffer.fire ) 1051935edac4STang Haojin XSPerfAccumulate("miss_0_except_1", f3_perf_info.miss_0_except_1 && io.toIbuffer.fire ) 1052935edac4STang Haojin XSPerfAccumulate("except_0", f3_perf_info.except_0 && io.toIbuffer.fire ) 1053eb163ef0SHaojin Tang XSPerfHistogram("ifu2ibuffer_validCnt", PopCount(io.toIbuffer.bits.valid & io.toIbuffer.bits.enqEnable), io.toIbuffer.fire, 0, PredictWidth + 1, 1) 105451532d8bSGuokai Chen 1055c686adcdSYinan Xu val hartId = p(XSCoreParamsKey).HartId 1056c686adcdSYinan Xu val isWriteFetchToIBufferTable = Constantin.createRecord(s"isWriteFetchToIBufferTable$hartId") 1057c686adcdSYinan Xu val isWriteIfuWbToFtqTable = Constantin.createRecord(s"isWriteIfuWbToFtqTable$hartId") 1058c686adcdSYinan Xu val fetchToIBufferTable = ChiselDB.createTable(s"FetchToIBuffer$hartId", new FetchToIBufferDB) 1059c686adcdSYinan Xu val ifuWbToFtqTable = ChiselDB.createTable(s"IfuWbToFtq$hartId", new IfuWbToFtqDB) 106051532d8bSGuokai Chen 106151532d8bSGuokai Chen val fetchIBufferDumpData = Wire(new FetchToIBufferDB) 106251532d8bSGuokai Chen fetchIBufferDumpData.start_addr := f3_ftq_req.startAddr 106351532d8bSGuokai Chen fetchIBufferDumpData.instr_count := PopCount(io.toIbuffer.bits.enqEnable) 1064935edac4STang 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) 106551532d8bSGuokai Chen fetchIBufferDumpData.is_cache_hit := f3_hit 106651532d8bSGuokai Chen 106751532d8bSGuokai Chen val ifuWbToFtqDumpData = Wire(new IfuWbToFtqDB) 106851532d8bSGuokai Chen ifuWbToFtqDumpData.start_addr := wb_ftq_req.startAddr 106951532d8bSGuokai Chen ifuWbToFtqDumpData.is_miss_pred := checkFlushWb.bits.misOffset.valid 107051532d8bSGuokai Chen ifuWbToFtqDumpData.miss_pred_offset := checkFlushWb.bits.misOffset.bits 107151532d8bSGuokai Chen ifuWbToFtqDumpData.checkJalFault := checkJalFault 107251532d8bSGuokai Chen ifuWbToFtqDumpData.checkRetFault := checkRetFault 107351532d8bSGuokai Chen ifuWbToFtqDumpData.checkTargetFault := checkTargetFault 107451532d8bSGuokai Chen ifuWbToFtqDumpData.checkNotCFIFault := checkNotCFIFault 107551532d8bSGuokai Chen ifuWbToFtqDumpData.checkInvalidTaken := checkInvalidTaken 107651532d8bSGuokai Chen 107751532d8bSGuokai Chen fetchToIBufferTable.log( 107851532d8bSGuokai Chen data = fetchIBufferDumpData, 1079da3bf434SMaxpicca-Li en = isWriteFetchToIBufferTable.orR && io.toIbuffer.fire, 108051532d8bSGuokai Chen site = "IFU" + p(XSCoreParamsKey).HartId.toString, 108151532d8bSGuokai Chen clock = clock, 108251532d8bSGuokai Chen reset = reset 108351532d8bSGuokai Chen ) 108451532d8bSGuokai Chen ifuWbToFtqTable.log( 108551532d8bSGuokai Chen data = ifuWbToFtqDumpData, 1086da3bf434SMaxpicca-Li en = isWriteIfuWbToFtqTable.orR && checkFlushWb.valid, 108751532d8bSGuokai Chen site = "IFU" + p(XSCoreParamsKey).HartId.toString, 108851532d8bSGuokai Chen clock = clock, 108951532d8bSGuokai Chen reset = reset 109051532d8bSGuokai Chen ) 109151532d8bSGuokai Chen 109209c6f1ddSLingrui98} 1093