1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.frontend 18import chipsalliance.rocketchip.config.Parameters 19import chisel3._ 20import chisel3.util._ 21import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 22import utils._ 23import utility._ 24import xiangshan._ 25import xiangshan.backend.fu.{PFEvent, PMP, PMPChecker,PMPReqBundle} 26import xiangshan.cache.mmu._ 27import xiangshan.frontend.icache._ 28 29 30class Frontend()(implicit p: Parameters) extends LazyModule with HasXSParameter { 31 override def shouldBeInlined: Boolean = false 32 33 val instrUncache = LazyModule(new InstrUncache()) 34 val icache = LazyModule(new ICache()) 35 36 lazy val module = new FrontendImp(this) 37} 38 39 40class FrontendImp (outer: Frontend) extends LazyModuleImp(outer) 41 with HasXSParameter 42 with HasPerfEvents 43{ 44 val io = IO(new Bundle() { 45 val hartId = Input(UInt(8.W)) 46 val reset_vector = Input(UInt(PAddrBits.W)) 47 val fencei = Input(Bool()) 48 val ptw = new TlbPtwIO() 49 val backend = new FrontendToCtrlIO 50 val sfence = Input(new SfenceBundle) 51 val tlbCsr = Input(new TlbCsrBundle) 52 val csrCtrl = Input(new CustomCSRCtrlIO) 53 val csrUpdate = new DistributedCSRUpdateReq 54 val error = new L1CacheErrorInfo 55 val frontendInfo = new Bundle { 56 val ibufFull = Output(Bool()) 57 val bpuInfo = new Bundle { 58 val bpRight = Output(UInt(XLEN.W)) 59 val bpWrong = Output(UInt(XLEN.W)) 60 } 61 } 62 val debugTopDown = new Bundle { 63 val robHeadVaddr = Flipped(Valid(UInt(VAddrBits.W))) 64 } 65 }) 66 67 //decouped-frontend modules 68 val instrUncache = outer.instrUncache.module 69 val icache = outer.icache.module 70 val bpu = Module(new Predictor) 71 val ifu = Module(new NewIFU) 72 val ibuffer = Module(new Ibuffer) 73 val ftq = Module(new Ftq) 74 75 val needFlush = RegNext(io.backend.toFtq.redirect.valid) 76 val FlushControlRedirect = RegNext(io.backend.toFtq.redirect.bits.debugIsCtrl) 77 val FlushMemVioRedirect = RegNext(io.backend.toFtq.redirect.bits.debugIsMemVio) 78 val FlushControlBTBMiss = Wire(Bool()) 79 val FlushTAGEMiss = Wire(Bool()) 80 val FlushSCMiss = Wire(Bool()) 81 val FlushITTAGEMiss = Wire(Bool()) 82 val FlushRASMiss = Wire(Bool()) 83 84 val tlbCsr = DelayN(io.tlbCsr, 2) 85 val csrCtrl = DelayN(io.csrCtrl, 2) 86 val sfence = RegNext(RegNext(io.sfence)) 87 88 // trigger 89 ifu.io.frontendTrigger := csrCtrl.frontend_trigger 90 val triggerEn = csrCtrl.trigger_enable 91 ifu.io.csrTriggerEnable := VecInit(triggerEn(0), triggerEn(1), triggerEn(6), triggerEn(8)) 92 93 // bpu ctrl 94 bpu.io.ctrl := csrCtrl.bp_ctrl 95 bpu.io.reset_vector := io.reset_vector 96 97// pmp 98 val prefetchPipeNum = ICacheParameters().prefetchPipeNum 99 val pmp = Module(new PMP()) 100 val pmp_check = VecInit(Seq.fill(coreParams.ipmpPortNum)(Module(new PMPChecker(3, sameCycle = true)).io)) 101 pmp.io.distribute_csr := csrCtrl.distribute_csr 102 val pmp_req_vec = Wire(Vec(coreParams.ipmpPortNum, Valid(new PMPReqBundle()))) 103 (0 until 2 + prefetchPipeNum).foreach(i => pmp_req_vec(i) <> icache.io.pmp(i).req) 104 pmp_req_vec.last <> ifu.io.pmp.req 105 106 for (i <- pmp_check.indices) { 107 pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, pmp_req_vec(i)) 108 } 109 (0 until 2 + prefetchPipeNum).foreach(i => icache.io.pmp(i).resp <> pmp_check(i).resp) 110 ifu.io.pmp.resp <> pmp_check.last.resp 111 112 val itlb = Module(new TLB(coreParams.itlbPortNum, nRespDups = 1, 113 Seq(false, false) ++ Seq.fill(prefetchPipeNum)(false) ++ Seq(true), itlbParams)) 114 itlb.io.requestor.take(2 + prefetchPipeNum) zip icache.io.itlb foreach {case (a,b) => a <> b} 115 itlb.io.requestor.last <> ifu.io.iTLBInter // mmio may need re-tlb, blocked 116 itlb.io.base_connect(sfence, tlbCsr) 117 itlb.io.flushPipe.map(_ := needFlush) 118 119 val itlb_ptw = Wire(new VectorTlbPtwIO(coreParams.itlbPortNum)) 120 itlb_ptw.connect(itlb.io.ptw) 121 val itlbRepeater1 = PTWFilter(itlbParams.fenceDelay, itlb_ptw, sfence, tlbCsr, l2tlbParams.ifilterSize) 122 io.ptw <> itlbRepeater1.io.ptw 123 124 icache.io.prefetch <> ftq.io.toPrefetch 125 126 127 //IFU-Ftq 128 ifu.io.ftqInter.fromFtq <> ftq.io.toIfu 129 ftq.io.toIfu.req.ready := ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready 130 131 ftq.io.fromIfu <> ifu.io.ftqInter.toFtq 132 bpu.io.ftq_to_bpu <> ftq.io.toBpu 133 ftq.io.fromBpu <> bpu.io.bpu_to_ftq 134 135 ftq.io.mmioCommitRead <> ifu.io.mmioCommitRead 136 //IFU-ICache 137 138 icache.io.fetch.req <> ftq.io.toICache.req 139 ftq.io.toICache.req.ready := ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready 140 141 ifu.io.icacheInter.resp <> icache.io.fetch.resp 142 ifu.io.icacheInter.icacheReady := icache.io.toIFU 143 ifu.io.icacheInter.topdownIcacheMiss := icache.io.fetch.topdownIcacheMiss 144 ifu.io.icacheInter.topdownItlbMiss := icache.io.fetch.topdownItlbMiss 145 icache.io.stop := ifu.io.icacheStop 146 147 ifu.io.icachePerfInfo := icache.io.perfInfo 148 149 icache.io.csr.distribute_csr <> DontCare 150 io.csrUpdate := DontCare 151 152 icache.io.csr_pf_enable := RegNext(csrCtrl.l1I_pf_enable) 153 icache.io.csr_parity_enable := RegNext(csrCtrl.icache_parity_enable) 154 155 icache.io.fencei := io.fencei 156 157 //IFU-Ibuffer 158 ifu.io.toIbuffer <> ibuffer.io.in 159 160 ftq.io.fromBackend <> io.backend.toFtq 161 io.backend.fromFtq <> ftq.io.toBackend 162 io.frontendInfo.bpuInfo <> ftq.io.bpuInfo 163 164 val checkPcMem = Reg(Vec(FtqSize, new Ftq_RF_Components)) 165 when (ftq.io.toBackend.pc_mem_wen) { 166 checkPcMem(ftq.io.toBackend.pc_mem_waddr) := ftq.io.toBackend.pc_mem_wdata 167 } 168 169 val checkTargetIdx = Wire(Vec(DecodeWidth, UInt(log2Up(FtqSize).W))) 170 val checkTarget = Wire(Vec(DecodeWidth, UInt(VAddrBits.W))) 171 172 for (i <- 0 until DecodeWidth) { 173 checkTargetIdx(i) := ibuffer.io.out(i).bits.ftqPtr.value 174 checkTarget(i) := Mux(ftq.io.toBackend.newest_entry_ptr.value === checkTargetIdx(i), 175 ftq.io.toBackend.newest_entry_target, 176 checkPcMem(checkTargetIdx(i) + 1.U).startAddr) 177 } 178 179 // commented out for this br could be the last instruction in the fetch block 180 def checkNotTakenConsecutive = { 181 val prevNotTakenValid = RegInit(0.B) 182 val prevNotTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W)) 183 for (i <- 0 until DecodeWidth - 1) { 184 // for instrs that is not the last, if a not-taken br, the next instr should have the same ftqPtr 185 // for instrs that is the last, record and check next request 186 when (ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr) { 187 when (ibuffer.io.out(i+1).fire) { 188 // not last br, check now 189 XSError(checkTargetIdx(i) =/= checkTargetIdx(i+1), "not-taken br should have same ftqPtr\n") 190 } .otherwise { 191 // last br, record its info 192 prevNotTakenValid := true.B 193 prevNotTakenFtqIdx := checkTargetIdx(i) 194 } 195 } 196 } 197 when (ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr) { 198 // last instr is a br, record its info 199 prevNotTakenValid := true.B 200 prevNotTakenFtqIdx := checkTargetIdx(DecodeWidth - 1) 201 } 202 when (prevNotTakenValid && ibuffer.io.out(0).fire) { 203 XSError(prevNotTakenFtqIdx =/= checkTargetIdx(0), "not-taken br should have same ftqPtr\n") 204 prevNotTakenValid := false.B 205 } 206 when (needFlush) { 207 prevNotTakenValid := false.B 208 } 209 } 210 211 def checkTakenNotConsecutive = { 212 val prevTakenValid = RegInit(0.B) 213 val prevTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W)) 214 for (i <- 0 until DecodeWidth - 1) { 215 // for instrs that is not the last, if a taken br, the next instr should not have the same ftqPtr 216 // for instrs that is the last, record and check next request 217 when (ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && ibuffer.io.out(i).bits.pred_taken) { 218 when (ibuffer.io.out(i+1).fire) { 219 // not last br, check now 220 XSError(checkTargetIdx(i) + 1.U =/= checkTargetIdx(i+1), "taken br should have consecutive ftqPtr\n") 221 } .otherwise { 222 // last br, record its info 223 prevTakenValid := true.B 224 prevTakenFtqIdx := checkTargetIdx(i) 225 } 226 } 227 } 228 when (ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && ibuffer.io.out(DecodeWidth - 1).bits.pred_taken) { 229 // last instr is a br, record its info 230 prevTakenValid := true.B 231 prevTakenFtqIdx := checkTargetIdx(DecodeWidth - 1) 232 } 233 when (prevTakenValid && ibuffer.io.out(0).fire) { 234 XSError(prevTakenFtqIdx + 1.U =/= checkTargetIdx(0), "taken br should have consecutive ftqPtr\n") 235 prevTakenValid := false.B 236 } 237 when (needFlush) { 238 prevTakenValid := false.B 239 } 240 } 241 242 def checkNotTakenPC = { 243 val prevNotTakenPC = Reg(UInt(VAddrBits.W)) 244 val prevIsRVC = Reg(Bool()) 245 val prevNotTakenValid = RegInit(0.B) 246 247 for (i <- 0 until DecodeWidth - 1) { 248 when (ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && !ibuffer.io.out(i).bits.pred_taken) { 249 when (ibuffer.io.out(i+1).fire) { 250 XSError(ibuffer.io.out(i).bits.pc + Mux(ibuffer.io.out(i).bits.pd.isRVC, 2.U, 4.U) =/= ibuffer.io.out(i+1).bits.pc, "not-taken br should have consecutive pc\n") 251 } .otherwise { 252 prevNotTakenValid := true.B 253 prevIsRVC := ibuffer.io.out(i).bits.pd.isRVC 254 prevNotTakenPC := ibuffer.io.out(i).bits.pc 255 } 256 } 257 } 258 when (ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && !ibuffer.io.out(DecodeWidth - 1).bits.pred_taken) { 259 prevNotTakenValid := true.B 260 prevIsRVC := ibuffer.io.out(DecodeWidth - 1).bits.pd.isRVC 261 prevNotTakenPC := ibuffer.io.out(DecodeWidth - 1).bits.pc 262 } 263 when (prevNotTakenValid && ibuffer.io.out(0).fire) { 264 XSError(prevNotTakenPC + Mux(prevIsRVC, 2.U, 4.U) =/= ibuffer.io.out(0).bits.pc, "not-taken br should have same pc\n") 265 prevNotTakenValid := false.B 266 } 267 when (needFlush) { 268 prevNotTakenValid := false.B 269 } 270 } 271 272 def checkTakenPC = { 273 val prevTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W)) 274 val prevTakenValid = RegInit(0.B) 275 val prevTakenTarget = Wire(UInt(VAddrBits.W)) 276 prevTakenTarget := checkPcMem(prevTakenFtqIdx + 1.U).startAddr 277 278 for (i <- 0 until DecodeWidth - 1) { 279 when (ibuffer.io.out(i).fire && !ibuffer.io.out(i).bits.pd.notCFI && ibuffer.io.out(i).bits.pred_taken) { 280 when (ibuffer.io.out(i+1).fire) { 281 XSError(checkTarget(i) =/= ibuffer.io.out(i+1).bits.pc, "taken instr should follow target pc\n") 282 } .otherwise { 283 prevTakenValid := true.B 284 prevTakenFtqIdx := checkTargetIdx(i) 285 } 286 } 287 } 288 when (ibuffer.io.out(DecodeWidth - 1).fire && !ibuffer.io.out(DecodeWidth - 1).bits.pd.notCFI && ibuffer.io.out(DecodeWidth - 1).bits.pred_taken) { 289 prevTakenValid := true.B 290 prevTakenFtqIdx := checkTargetIdx(DecodeWidth - 1) 291 } 292 when (prevTakenValid && ibuffer.io.out(0).fire) { 293 XSError(prevTakenTarget =/= ibuffer.io.out(0).bits.pc, "taken instr should follow target pc\n") 294 prevTakenValid := false.B 295 } 296 when (needFlush) { 297 prevTakenValid := false.B 298 } 299 } 300 301 //checkNotTakenConsecutive 302 checkTakenNotConsecutive 303 checkTakenPC 304 checkNotTakenPC 305 306 ifu.io.rob_commits <> io.backend.toFtq.rob_commits 307 308 ibuffer.io.flush := needFlush 309 ibuffer.io.ControlRedirect := FlushControlRedirect 310 ibuffer.io.MemVioRedirect := FlushMemVioRedirect 311 ibuffer.io.ControlBTBMissBubble := FlushControlBTBMiss 312 ibuffer.io.TAGEMissBubble := FlushTAGEMiss 313 ibuffer.io.SCMissBubble := FlushSCMiss 314 ibuffer.io.ITTAGEMissBubble := FlushITTAGEMiss 315 ibuffer.io.RASMissBubble := FlushRASMiss 316 317 FlushControlBTBMiss := ftq.io.ControlBTBMissBubble 318 FlushTAGEMiss := ftq.io.TAGEMissBubble 319 FlushSCMiss := ftq.io.SCMissBubble 320 FlushITTAGEMiss := ftq.io.ITTAGEMissBubble 321 FlushRASMiss := ftq.io.RASMissBubble 322 323 io.backend.cfVec <> ibuffer.io.out 324 io.backend.stallReason <> ibuffer.io.stallReason 325 dontTouch(io.backend.stallReason) 326 327 instrUncache.io.req <> ifu.io.uncacheInter.toUncache 328 ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp 329 instrUncache.io.flush := false.B 330 io.error <> RegNext(RegNext(icache.io.error)) 331 332 icache.io.hartId := io.hartId 333 334 itlbRepeater1.io.debugTopDown.robHeadVaddr := io.debugTopDown.robHeadVaddr 335 336 val frontendBubble = PopCount((0 until DecodeWidth).map(i => io.backend.cfVec(i).ready && !ibuffer.io.out(i).valid)) 337 XSPerfAccumulate("FrontendBubble", frontendBubble) 338 io.frontendInfo.ibufFull := RegNext(ibuffer.io.full) 339 340 // PFEvent 341 val pfevent = Module(new PFEvent) 342 pfevent.io.distribute_csr := io.csrCtrl.distribute_csr 343 val csrevents = pfevent.io.hpmevent.take(8) 344 345 val allPerfEvents = Seq(ifu, ibuffer, icache, ftq, bpu).flatMap(_.getPerf) 346 override val perfEvents = HPerfMonitor(csrevents, allPerfEvents).getPerfEvents 347 generatePerfEvent() 348} 349