109c6f1ddSLingrui98/*************************************************************************************** 209c6f1ddSLingrui98* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 309c6f1ddSLingrui98* Copyright (c) 2020-2021 Peng Cheng Laboratory 409c6f1ddSLingrui98* 509c6f1ddSLingrui98* XiangShan is licensed under Mulan PSL v2. 609c6f1ddSLingrui98* You can use this software according to the terms and conditions of the Mulan PSL v2. 709c6f1ddSLingrui98* You may obtain a copy of Mulan PSL v2 at: 809c6f1ddSLingrui98* http://license.coscl.org.cn/MulanPSL2 909c6f1ddSLingrui98* 1009c6f1ddSLingrui98* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 1109c6f1ddSLingrui98* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 1209c6f1ddSLingrui98* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 1309c6f1ddSLingrui98* 1409c6f1ddSLingrui98* See the Mulan PSL v2 for more details. 15c49ebec8SHaoyuan Feng* 16c49ebec8SHaoyuan Feng* 17c49ebec8SHaoyuan Feng* Acknowledgement 18c49ebec8SHaoyuan Feng* 19c49ebec8SHaoyuan Feng* This implementation is inspired by several key papers: 20c49ebec8SHaoyuan Feng* [1] Alex Ramirez, Oliverio J. Santana, Josep L. Larriba-Pey, and Mateo Valero. "[Fetching instruction streams.] 21c49ebec8SHaoyuan Feng* (https://doi.org/10.1109/MICRO.2002.1176264)" 35th Annual IEEE/ACM International Symposium on Microarchitecture 22c49ebec8SHaoyuan Feng* (MICRO). 2002. 23c49ebec8SHaoyuan Feng* [2] Yasuo Ishii, Jaekyu Lee, Krishnendra Nathella, and Dam Sunwoo. "[Rebasing instruction prefetching: An industry 24c49ebec8SHaoyuan Feng* perspective.](https://doi.org/10.1109/LCA.2020.3035068)" IEEE Computer Architecture Letters 19.2: 147-150. 2020. 25c49ebec8SHaoyuan Feng* [3] Yasuo Ishii, Jaekyu Lee, Krishnendra Nathella, and Dam Sunwoo. "[Re-establishing fetch-directed instruction 26c49ebec8SHaoyuan Feng* prefetching: An industry perspective.](https://doi.org/10.1109/ISPASS51385.2021.00034)" 2021 IEEE International 27c49ebec8SHaoyuan Feng* Symposium on Performance Analysis of Systems and Software (ISPASS). 2021. 2809c6f1ddSLingrui98***************************************************************************************/ 2909c6f1ddSLingrui98 3009c6f1ddSLingrui98package xiangshan.frontend 3109c6f1ddSLingrui98import chisel3._ 3209c6f1ddSLingrui98import chisel3.util._ 33cf7d6b7aSMuziimport freechips.rocketchip.diplomacy.LazyModule 34cf7d6b7aSMuziimport freechips.rocketchip.diplomacy.LazyModuleImp 35cf7d6b7aSMuziimport org.chipsalliance.cde.config.Parameters 363c02ee8fSwakafaimport utility._ 3709c6f1ddSLingrui98import xiangshan._ 38cf7d6b7aSMuziimport xiangshan.backend.fu.PFEvent 39cf7d6b7aSMuziimport xiangshan.backend.fu.PMP 40cf7d6b7aSMuziimport xiangshan.backend.fu.PMPChecker 41cf7d6b7aSMuziimport xiangshan.backend.fu.PMPReqBundle 42ee175d78SJayimport xiangshan.cache.mmu._ 431d8f4dcbSJayimport xiangshan.frontend.icache._ 4409c6f1ddSLingrui98 4509c6f1ddSLingrui98class Frontend()(implicit p: Parameters) extends LazyModule with HasXSParameter { 4695e60e55STang Haojin override def shouldBeInlined: Boolean = false 47233f2ad0Szhanglinjuan val inner = LazyModule(new FrontendInlined) 48233f2ad0Szhanglinjuan lazy val module = new FrontendImp(this) 49233f2ad0Szhanglinjuan} 50233f2ad0Szhanglinjuan 51233f2ad0Szhanglinjuanclass FrontendImp(wrapper: Frontend)(implicit p: Parameters) extends LazyModuleImp(wrapper) { 52233f2ad0Szhanglinjuan val io = IO(wrapper.inner.module.io.cloneType) 53233f2ad0Szhanglinjuan val io_perf = IO(wrapper.inner.module.io_perf.cloneType) 54233f2ad0Szhanglinjuan io <> wrapper.inner.module.io 55233f2ad0Szhanglinjuan io_perf <> wrapper.inner.module.io_perf 56233f2ad0Szhanglinjuan if (p(DebugOptionsKey).ResetGen) { 57233f2ad0Szhanglinjuan ResetGen(ResetGenNode(Seq(ModuleNode(wrapper.inner.module))), reset, sim = false) 58233f2ad0Szhanglinjuan } 59233f2ad0Szhanglinjuan} 60233f2ad0Szhanglinjuan 61233f2ad0Szhanglinjuanclass FrontendInlined()(implicit p: Parameters) extends LazyModule with HasXSParameter { 62233f2ad0Szhanglinjuan override def shouldBeInlined: Boolean = true 6309c6f1ddSLingrui98 6409c6f1ddSLingrui98 val instrUncache = LazyModule(new InstrUncache()) 6509c6f1ddSLingrui98 val icache = LazyModule(new ICache()) 6609c6f1ddSLingrui98 67233f2ad0Szhanglinjuan lazy val module = new FrontendInlinedImp(this) 6809c6f1ddSLingrui98} 6909c6f1ddSLingrui98 70233f2ad0Szhanglinjuanclass FrontendInlinedImp(outer: FrontendInlined) extends LazyModuleImp(outer) 7109c6f1ddSLingrui98 with HasXSParameter 72cf7d6b7aSMuzi with HasPerfEvents { 7309c6f1ddSLingrui98 val io = IO(new Bundle() { 74f57f7f2aSYangyu Chen val hartId = Input(UInt(hartIdLen.W)) 75c4b44470SGuokai Chen val reset_vector = Input(UInt(PAddrBits.W)) 7609c6f1ddSLingrui98 val fencei = Input(Bool()) 771a718038SHaoyuan Feng val ptw = new TlbPtwIO() 7809c6f1ddSLingrui98 val backend = new FrontendToCtrlIO 792c9f4a9fSxu_zh val softPrefetch = Vec(backendParams.LduCnt, Flipped(Valid(new SoftIfetchPrefetchBundle))) 8009c6f1ddSLingrui98 val sfence = Input(new SfenceBundle) 8109c6f1ddSLingrui98 val tlbCsr = Input(new TlbCsrBundle) 8209c6f1ddSLingrui98 val csrCtrl = Input(new CustomCSRCtrlIO) 830184a80eSYanqin Li val error = ValidIO(new L1CacheErrorInfo) 8409c6f1ddSLingrui98 val frontendInfo = new Bundle { 8509c6f1ddSLingrui98 val ibufFull = Output(Bool()) 8609c6f1ddSLingrui98 val bpuInfo = new Bundle { 8709c6f1ddSLingrui98 val bpRight = Output(UInt(XLEN.W)) 8809c6f1ddSLingrui98 val bpWrong = Output(UInt(XLEN.W)) 8909c6f1ddSLingrui98 } 9009c6f1ddSLingrui98 } 91233f2ad0Szhanglinjuan val resetInFrontend = Output(Bool()) 9260ebee38STang Haojin val debugTopDown = new Bundle { 9360ebee38STang Haojin val robHeadVaddr = Flipped(Valid(UInt(VAddrBits.W))) 9460ebee38STang Haojin } 9509c6f1ddSLingrui98 }) 9609c6f1ddSLingrui98 9709c6f1ddSLingrui98 // decouped-frontend modules 981d8f4dcbSJay val instrUncache = outer.instrUncache.module 991d8f4dcbSJay val icache = outer.icache.module 10009c6f1ddSLingrui98 val bpu = Module(new Predictor) 10109c6f1ddSLingrui98 val ifu = Module(new NewIFU) 10244c9c1deSEaston Man val ibuffer = Module(new IBuffer) 10309c6f1ddSLingrui98 val ftq = Module(new Ftq) 10409c6f1ddSLingrui98 105f1fe8698SLemover val needFlush = RegNext(io.backend.toFtq.redirect.valid) 106d2b20d1aSTang Haojin val FlushControlRedirect = RegNext(io.backend.toFtq.redirect.bits.debugIsCtrl) 107d2b20d1aSTang Haojin val FlushMemVioRedirect = RegNext(io.backend.toFtq.redirect.bits.debugIsMemVio) 108d2b20d1aSTang Haojin val FlushControlBTBMiss = Wire(Bool()) 109d2b20d1aSTang Haojin val FlushTAGEMiss = Wire(Bool()) 110d2b20d1aSTang Haojin val FlushSCMiss = Wire(Bool()) 111d2b20d1aSTang Haojin val FlushITTAGEMiss = Wire(Bool()) 112d2b20d1aSTang Haojin val FlushRASMiss = Wire(Bool()) 113f1fe8698SLemover 1146f688dacSYinan Xu val tlbCsr = DelayN(io.tlbCsr, 2) 1156f688dacSYinan Xu val csrCtrl = DelayN(io.csrCtrl, 2) 116fa9f9690SLemover val sfence = RegNext(RegNext(io.sfence)) 11772951335SLi Qianruo 11872951335SLi Qianruo // trigger 1196f688dacSYinan Xu ifu.io.frontendTrigger := csrCtrl.frontend_trigger 12072951335SLi Qianruo 12171b6c42eSxu_zh // RVCDecoder fsIsOff 12271b6c42eSxu_zh ifu.io.csr_fsIsOff := csrCtrl.fsIsOff 12371b6c42eSxu_zh 1246ee06c7aSSteve Gou // bpu ctrl 1256ee06c7aSSteve Gou bpu.io.ctrl := csrCtrl.bp_ctrl 1265f119905STang Haojin bpu.io.reset_vector := io.reset_vector 1276ee06c7aSSteve Gou 128b6982e83SLemover // pmp 129b92f8445Sssszwic val PortNumber = ICacheParameters().PortNumber 130b6982e83SLemover val pmp = Module(new PMP()) 13134f9624dSguohongyu val pmp_check = VecInit(Seq.fill(coreParams.ipmpPortNum)(Module(new PMPChecker(3, sameCycle = true)).io)) 1326f688dacSYinan Xu pmp.io.distribute_csr := csrCtrl.distribute_csr 13334f9624dSguohongyu val pmp_req_vec = Wire(Vec(coreParams.ipmpPortNum, Valid(new PMPReqBundle()))) 134b92f8445Sssszwic (0 until 2 * PortNumber).foreach(i => pmp_req_vec(i) <> icache.io.pmp(i).req) 1350c26d810Sguohongyu pmp_req_vec.last <> ifu.io.pmp.req 136ee175d78SJay 137b6982e83SLemover for (i <- pmp_check.indices) { 138ee175d78SJay pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, pmp_req_vec(i)) 139b6982e83SLemover } 140b92f8445Sssszwic (0 until 2 * PortNumber).foreach(i => icache.io.pmp(i).resp <> pmp_check(i).resp) 1410c26d810Sguohongyu ifu.io.pmp.resp <> pmp_check.last.resp 142ee175d78SJay 143cf7d6b7aSMuzi val itlb = 144cf7d6b7aSMuzi Module(new TLB(coreParams.itlbPortNum, nRespDups = 1, Seq.fill(PortNumber)(false) ++ Seq(true), itlbParams)) 145b92f8445Sssszwic itlb.io.requestor.take(PortNumber) zip icache.io.itlb foreach { case (a, b) => a <> b } 1460c26d810Sguohongyu itlb.io.requestor.last <> ifu.io.iTLBInter // mmio may need re-tlb, blocked 147254e4960SHaoyuan Feng itlb.io.hartId := io.hartId 1481a718038SHaoyuan Feng itlb.io.base_connect(sfence, tlbCsr) 149*fad7803dSxu_zh itlb.io.flushPipe.foreach(_ := icache.io.itlbFlushPipe) 150a4f9c77fSpeixiaokun itlb.io.redirect := DontCare // itlb has flushpipe, don't need redirect signal 15109c6f1ddSLingrui98 1521a718038SHaoyuan Feng val itlb_ptw = Wire(new VectorTlbPtwIO(coreParams.itlbPortNum)) 1531a718038SHaoyuan Feng itlb_ptw.connect(itlb.io.ptw) 1541a718038SHaoyuan Feng val itlbRepeater1 = PTWFilter(itlbParams.fenceDelay, itlb_ptw, sfence, tlbCsr, l2tlbParams.ifilterSize) 155cf7d6b7aSMuzi val itlbRepeater2 = 156cf7d6b7aSMuzi PTWRepeaterNB(passReady = false, itlbParams.fenceDelay, itlbRepeater1.io.ptw, io.ptw, sfence, tlbCsr) 1571a718038SHaoyuan Feng 1582c9f4a9fSxu_zh icache.io.ftqPrefetch <> ftq.io.toPrefetch 1592c9f4a9fSxu_zh icache.io.softPrefetch <> io.softPrefetch 16009c6f1ddSLingrui98 16109c6f1ddSLingrui98 // IFU-Ftq 16209c6f1ddSLingrui98 ifu.io.ftqInter.fromFtq <> ftq.io.toIfu 163c5c5edaeSJenius ftq.io.toIfu.req.ready := ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready 164c5c5edaeSJenius 16509c6f1ddSLingrui98 ftq.io.fromIfu <> ifu.io.ftqInter.toFtq 16609c6f1ddSLingrui98 bpu.io.ftq_to_bpu <> ftq.io.toBpu 16709c6f1ddSLingrui98 ftq.io.fromBpu <> bpu.io.bpu_to_ftq 1681d1e6d4dSJenius 1691d1e6d4dSJenius ftq.io.mmioCommitRead <> ifu.io.mmioCommitRead 170c5c5edaeSJenius 17171b6c42eSxu_zh // IFU-ICache 172c5c5edaeSJenius icache.io.fetch.req <> ftq.io.toICache.req 173c5c5edaeSJenius ftq.io.toICache.req.ready := ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready 174c5c5edaeSJenius 175c5c5edaeSJenius ifu.io.icacheInter.resp <> icache.io.fetch.resp 17650780602SJenius ifu.io.icacheInter.icacheReady := icache.io.toIFU 177d2b20d1aSTang Haojin ifu.io.icacheInter.topdownIcacheMiss := icache.io.fetch.topdownIcacheMiss 178d2b20d1aSTang Haojin ifu.io.icacheInter.topdownItlbMiss := icache.io.fetch.topdownItlbMiss 1791d8f4dcbSJay icache.io.stop := ifu.io.icacheStop 180b92f8445Sssszwic icache.io.flush := ftq.io.icacheFlush 18109c6f1ddSLingrui98 1821d8f4dcbSJay ifu.io.icachePerfInfo := icache.io.perfInfo 1831d8f4dcbSJay 184ecccf78fSJay icache.io.csr_pf_enable := RegNext(csrCtrl.l1I_pf_enable) 185ecccf78fSJay icache.io.csr_parity_enable := RegNext(csrCtrl.icache_parity_enable) 186ecccf78fSJay 1873dbaa960SEaston Man icache.io.fencei := RegNext(io.fencei) 1882a6078bfSguohongyu 18909c6f1ddSLingrui98 // IFU-Ibuffer 19009c6f1ddSLingrui98 ifu.io.toIbuffer <> ibuffer.io.in 19109c6f1ddSLingrui98 19209c6f1ddSLingrui98 ftq.io.fromBackend <> io.backend.toFtq 19392c61038SXuan Hu io.backend.fromFtq := ftq.io.toBackend 19492c61038SXuan Hu io.backend.fromIfu := ifu.io.toBackend 19509c6f1ddSLingrui98 io.frontendInfo.bpuInfo <> ftq.io.bpuInfo 19609c6f1ddSLingrui98 1975359309bSGuokai Chen val checkPcMem = Reg(Vec(FtqSize, new Ftq_RF_Components)) 1985359309bSGuokai Chen when(ftq.io.toBackend.pc_mem_wen) { 199f533cba7SHuSipeng checkPcMem(ftq.io.toBackend.pc_mem_waddr) := ftq.io.toBackend.pc_mem_wdata 2005359309bSGuokai Chen } 2015359309bSGuokai Chen 2025359309bSGuokai Chen val checkTargetIdx = Wire(Vec(DecodeWidth, UInt(log2Up(FtqSize).W))) 2035359309bSGuokai Chen val checkTarget = Wire(Vec(DecodeWidth, UInt(VAddrBits.W))) 2045359309bSGuokai Chen 2055359309bSGuokai Chen for (i <- 0 until DecodeWidth) { 2065359309bSGuokai Chen checkTargetIdx(i) := ibuffer.io.out(i).bits.ftqPtr.value 207cf7d6b7aSMuzi checkTarget(i) := Mux( 208cf7d6b7aSMuzi ftq.io.toBackend.newest_entry_ptr.value === checkTargetIdx(i), 2095359309bSGuokai Chen ftq.io.toBackend.newest_entry_target, 210cf7d6b7aSMuzi checkPcMem(checkTargetIdx(i) + 1.U).startAddr 211cf7d6b7aSMuzi ) 2125359309bSGuokai Chen } 2135359309bSGuokai Chen 2145359309bSGuokai Chen // commented out for this br could be the last instruction in the fetch block 2155359309bSGuokai Chen def checkNotTakenConsecutive = { 2165359309bSGuokai Chen val prevNotTakenValid = RegInit(0.B) 2175359309bSGuokai Chen val prevNotTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W)) 2185359309bSGuokai Chen for (i <- 0 until DecodeWidth - 1) { 2195359309bSGuokai Chen // for instrs that is not the last, if a not-taken br, the next instr should have the same ftqPtr 2205359309bSGuokai Chen // for instrs that is the last, record and check next request 2215359309bSGuokai Chen when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr) { 2225359309bSGuokai Chen when(ibuffer.io.out(i + 1).fire) { 2235359309bSGuokai Chen // not last br, check now 2245359309bSGuokai Chen XSError(checkTargetIdx(i) =/= checkTargetIdx(i + 1), "not-taken br should have same ftqPtr\n") 2255359309bSGuokai Chen }.otherwise { 2265359309bSGuokai Chen // last br, record its info 2275359309bSGuokai Chen prevNotTakenValid := true.B 2285359309bSGuokai Chen prevNotTakenFtqIdx := checkTargetIdx(i) 2295359309bSGuokai Chen } 2305359309bSGuokai Chen } 2315359309bSGuokai Chen } 2325359309bSGuokai Chen when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr) { 2335359309bSGuokai Chen // last instr is a br, record its info 2345359309bSGuokai Chen prevNotTakenValid := true.B 2355359309bSGuokai Chen prevNotTakenFtqIdx := checkTargetIdx(DecodeWidth - 1) 2365359309bSGuokai Chen } 2375359309bSGuokai Chen when(prevNotTakenValid && ibuffer.io.out(0).fire) { 2385359309bSGuokai Chen XSError(prevNotTakenFtqIdx =/= checkTargetIdx(0), "not-taken br should have same ftqPtr\n") 2395359309bSGuokai Chen prevNotTakenValid := false.B 2405359309bSGuokai Chen } 2415359309bSGuokai Chen when(needFlush) { 2425359309bSGuokai Chen prevNotTakenValid := false.B 2435359309bSGuokai Chen } 2445359309bSGuokai Chen } 2455359309bSGuokai Chen 2465359309bSGuokai Chen def checkTakenNotConsecutive = { 2475359309bSGuokai Chen val prevTakenValid = RegInit(0.B) 2485359309bSGuokai Chen val prevTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W)) 2495359309bSGuokai Chen for (i <- 0 until DecodeWidth - 1) { 2505359309bSGuokai Chen // for instrs that is not the last, if a taken br, the next instr should not have the same ftqPtr 2515359309bSGuokai Chen // for instrs that is the last, record and check next request 2525359309bSGuokai Chen when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && ibuffer.io.out(i).bits.pred_taken) { 2535359309bSGuokai Chen when(ibuffer.io.out(i + 1).fire) { 2545359309bSGuokai Chen // not last br, check now 2555359309bSGuokai Chen XSError(checkTargetIdx(i) + 1.U =/= checkTargetIdx(i + 1), "taken br should have consecutive ftqPtr\n") 2565359309bSGuokai Chen }.otherwise { 2575359309bSGuokai Chen // last br, record its info 2585359309bSGuokai Chen prevTakenValid := true.B 2595359309bSGuokai Chen prevTakenFtqIdx := checkTargetIdx(i) 2605359309bSGuokai Chen } 2615359309bSGuokai Chen } 2625359309bSGuokai Chen } 263cf7d6b7aSMuzi when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && ibuffer.io.out( 264cf7d6b7aSMuzi DecodeWidth - 1 265cf7d6b7aSMuzi ).bits.pred_taken) { 2665359309bSGuokai Chen // last instr is a br, record its info 2675359309bSGuokai Chen prevTakenValid := true.B 2685359309bSGuokai Chen prevTakenFtqIdx := checkTargetIdx(DecodeWidth - 1) 2695359309bSGuokai Chen } 2705359309bSGuokai Chen when(prevTakenValid && ibuffer.io.out(0).fire) { 2715359309bSGuokai Chen XSError(prevTakenFtqIdx + 1.U =/= checkTargetIdx(0), "taken br should have consecutive ftqPtr\n") 2725359309bSGuokai Chen prevTakenValid := false.B 2735359309bSGuokai Chen } 2745359309bSGuokai Chen when(needFlush) { 2755359309bSGuokai Chen prevTakenValid := false.B 2765359309bSGuokai Chen } 2775359309bSGuokai Chen } 2785359309bSGuokai Chen 2795359309bSGuokai Chen def checkNotTakenPC = { 2805359309bSGuokai Chen val prevNotTakenPC = Reg(UInt(VAddrBits.W)) 2815359309bSGuokai Chen val prevIsRVC = Reg(Bool()) 2825359309bSGuokai Chen val prevNotTakenValid = RegInit(0.B) 2835359309bSGuokai Chen 2845359309bSGuokai Chen for (i <- 0 until DecodeWidth - 1) { 2855359309bSGuokai Chen when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && !ibuffer.io.out(i).bits.pred_taken) { 2865359309bSGuokai Chen when(ibuffer.io.out(i + 1).fire) { 287cf7d6b7aSMuzi XSError( 288cf7d6b7aSMuzi ibuffer.io.out(i).bits.pc + Mux(ibuffer.io.out(i).bits.pd.isRVC, 2.U, 4.U) =/= ibuffer.io.out( 289cf7d6b7aSMuzi i + 1 290cf7d6b7aSMuzi ).bits.pc, 291cf7d6b7aSMuzi "not-taken br should have consecutive pc\n" 292cf7d6b7aSMuzi ) 2935359309bSGuokai Chen }.otherwise { 2945359309bSGuokai Chen prevNotTakenValid := true.B 2955359309bSGuokai Chen prevIsRVC := ibuffer.io.out(i).bits.pd.isRVC 2965359309bSGuokai Chen prevNotTakenPC := ibuffer.io.out(i).bits.pc 2975359309bSGuokai Chen } 2985359309bSGuokai Chen } 2995359309bSGuokai Chen } 300cf7d6b7aSMuzi when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && !ibuffer.io.out( 301cf7d6b7aSMuzi DecodeWidth - 1 302cf7d6b7aSMuzi ).bits.pred_taken) { 3035359309bSGuokai Chen prevNotTakenValid := true.B 3045359309bSGuokai Chen prevIsRVC := ibuffer.io.out(DecodeWidth - 1).bits.pd.isRVC 3055359309bSGuokai Chen prevNotTakenPC := ibuffer.io.out(DecodeWidth - 1).bits.pc 3065359309bSGuokai Chen } 3075359309bSGuokai Chen when(prevNotTakenValid && ibuffer.io.out(0).fire) { 308cf7d6b7aSMuzi XSError( 309cf7d6b7aSMuzi prevNotTakenPC + Mux(prevIsRVC, 2.U, 4.U) =/= ibuffer.io.out(0).bits.pc, 310cf7d6b7aSMuzi "not-taken br should have same pc\n" 311cf7d6b7aSMuzi ) 3125359309bSGuokai Chen prevNotTakenValid := false.B 3135359309bSGuokai Chen } 3145359309bSGuokai Chen when(needFlush) { 3155359309bSGuokai Chen prevNotTakenValid := false.B 3165359309bSGuokai Chen } 3175359309bSGuokai Chen } 3185359309bSGuokai Chen 3195359309bSGuokai Chen def checkTakenPC = { 3205359309bSGuokai Chen val prevTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W)) 3215359309bSGuokai Chen val prevTakenValid = RegInit(0.B) 3225359309bSGuokai Chen val prevTakenTarget = Wire(UInt(VAddrBits.W)) 3235359309bSGuokai Chen prevTakenTarget := checkPcMem(prevTakenFtqIdx + 1.U).startAddr 3245359309bSGuokai Chen 3255359309bSGuokai Chen for (i <- 0 until DecodeWidth - 1) { 3265359309bSGuokai Chen when(ibuffer.io.out(i).fire && !ibuffer.io.out(i).bits.pd.notCFI && ibuffer.io.out(i).bits.pred_taken) { 3275359309bSGuokai Chen when(ibuffer.io.out(i + 1).fire) { 3285359309bSGuokai Chen XSError(checkTarget(i) =/= ibuffer.io.out(i + 1).bits.pc, "taken instr should follow target pc\n") 3295359309bSGuokai Chen }.otherwise { 3305359309bSGuokai Chen prevTakenValid := true.B 3315359309bSGuokai Chen prevTakenFtqIdx := checkTargetIdx(i) 3325359309bSGuokai Chen } 3335359309bSGuokai Chen } 3345359309bSGuokai Chen } 335cf7d6b7aSMuzi when(ibuffer.io.out(DecodeWidth - 1).fire && !ibuffer.io.out(DecodeWidth - 1).bits.pd.notCFI && ibuffer.io.out( 336cf7d6b7aSMuzi DecodeWidth - 1 337cf7d6b7aSMuzi ).bits.pred_taken) { 3385359309bSGuokai Chen prevTakenValid := true.B 3395359309bSGuokai Chen prevTakenFtqIdx := checkTargetIdx(DecodeWidth - 1) 3405359309bSGuokai Chen } 3415359309bSGuokai Chen when(prevTakenValid && ibuffer.io.out(0).fire) { 3425359309bSGuokai Chen XSError(prevTakenTarget =/= ibuffer.io.out(0).bits.pc, "taken instr should follow target pc\n") 3435359309bSGuokai Chen prevTakenValid := false.B 3445359309bSGuokai Chen } 3455359309bSGuokai Chen when(needFlush) { 3465359309bSGuokai Chen prevTakenValid := false.B 3475359309bSGuokai Chen } 3485359309bSGuokai Chen } 3495359309bSGuokai Chen 3505359309bSGuokai Chen // checkNotTakenConsecutive 3515359309bSGuokai Chen checkTakenNotConsecutive 3525359309bSGuokai Chen checkTakenPC 3535359309bSGuokai Chen checkNotTakenPC 3545359309bSGuokai Chen 355a37fbf10SJay ifu.io.rob_commits <> io.backend.toFtq.rob_commits 356a37fbf10SJay 35709c6f1ddSLingrui98 ibuffer.io.flush := needFlush 358d2b20d1aSTang Haojin ibuffer.io.ControlRedirect := FlushControlRedirect 359d2b20d1aSTang Haojin ibuffer.io.MemVioRedirect := FlushMemVioRedirect 360d2b20d1aSTang Haojin ibuffer.io.ControlBTBMissBubble := FlushControlBTBMiss 361d2b20d1aSTang Haojin ibuffer.io.TAGEMissBubble := FlushTAGEMiss 362d2b20d1aSTang Haojin ibuffer.io.SCMissBubble := FlushSCMiss 363d2b20d1aSTang Haojin ibuffer.io.ITTAGEMissBubble := FlushITTAGEMiss 364d2b20d1aSTang Haojin ibuffer.io.RASMissBubble := FlushRASMiss 36505cc2a4eSXuan Hu ibuffer.io.decodeCanAccept := io.backend.canAccept 366d2b20d1aSTang Haojin 367d2b20d1aSTang Haojin FlushControlBTBMiss := ftq.io.ControlBTBMissBubble 368d2b20d1aSTang Haojin FlushTAGEMiss := ftq.io.TAGEMissBubble 369d2b20d1aSTang Haojin FlushSCMiss := ftq.io.SCMissBubble 370d2b20d1aSTang Haojin FlushITTAGEMiss := ftq.io.ITTAGEMissBubble 371d2b20d1aSTang Haojin FlushRASMiss := ftq.io.RASMissBubble 372d2b20d1aSTang Haojin 37309c6f1ddSLingrui98 io.backend.cfVec <> ibuffer.io.out 374d2b20d1aSTang Haojin io.backend.stallReason <> ibuffer.io.stallReason 37509c6f1ddSLingrui98 3760be662e4SJay instrUncache.io.req <> ifu.io.uncacheInter.toUncache 3770be662e4SJay ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp 37858dbdfc2SJay instrUncache.io.flush := false.B 37958dbdfc2SJay io.error <> RegNext(RegNext(icache.io.error)) 38009c6f1ddSLingrui98 38141cb8b61SJenius icache.io.hartId := io.hartId 38241cb8b61SJenius 38360ebee38STang Haojin itlbRepeater1.io.debugTopDown.robHeadVaddr := io.debugTopDown.robHeadVaddr 38460ebee38STang Haojin 3859c55e669SEaston Man val frontendBubble = Mux(io.backend.canAccept, DecodeWidth.U - PopCount(ibuffer.io.out.map(_.valid)), 0.U) 38609c6f1ddSLingrui98 XSPerfAccumulate("FrontendBubble", frontendBubble) 38709c6f1ddSLingrui98 io.frontendInfo.ibufFull := RegNext(ibuffer.io.full) 388233f2ad0Szhanglinjuan io.resetInFrontend := reset.asBool 389cd365d4cSrvcoresjw 3901ca0e4f3SYinan Xu // PFEvent 3911ca0e4f3SYinan Xu val pfevent = Module(new PFEvent) 3921ca0e4f3SYinan Xu pfevent.io.distribute_csr := io.csrCtrl.distribute_csr 3931ca0e4f3SYinan Xu val csrevents = pfevent.io.hpmevent.take(8) 394cd365d4cSrvcoresjw 3959a128342SHaoyuan Feng val perfFromUnits = Seq(ifu, ibuffer, icache, ftq, bpu).flatMap(_.getPerfEvents) 3969a128342SHaoyuan Feng val perfFromIO = Seq() 3979a128342SHaoyuan Feng val perfBlock = Seq() 3989a128342SHaoyuan Feng // let index = 0 be no event 3999a128342SHaoyuan Feng val allPerfEvents = Seq(("noEvent", 0.U)) ++ perfFromUnits ++ perfFromIO ++ perfBlock 4009a128342SHaoyuan Feng 4019a128342SHaoyuan Feng if (printEventCoding) { 4029a128342SHaoyuan Feng for (((name, inc), i) <- allPerfEvents.zipWithIndex) { 4039a128342SHaoyuan Feng println("Frontend perfEvents Set", name, inc, i) 4049a128342SHaoyuan Feng } 4059a128342SHaoyuan Feng } 4069a128342SHaoyuan Feng 4079a128342SHaoyuan Feng val allPerfInc = allPerfEvents.map(_._2.asTypeOf(new PerfEvent)) 4089a128342SHaoyuan Feng override val perfEvents = HPerfMonitor(csrevents, allPerfInc).getPerfEvents 4091ca0e4f3SYinan Xu generatePerfEvent() 41009c6f1ddSLingrui98} 411