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* 17* Acknowledgement 18* 19* This implementation is inspired by several key papers: 20* [1] Alex Ramirez, Oliverio J. Santana, Josep L. Larriba-Pey, and Mateo Valero. "[Fetching instruction streams.] 21* (https://doi.org/10.1109/MICRO.2002.1176264)" 35th Annual IEEE/ACM International Symposium on Microarchitecture 22* (MICRO). 2002. 23* [2] Yasuo Ishii, Jaekyu Lee, Krishnendra Nathella, and Dam Sunwoo. "[Rebasing instruction prefetching: An industry 24* perspective.](https://doi.org/10.1109/LCA.2020.3035068)" IEEE Computer Architecture Letters 19.2: 147-150. 2020. 25* [3] Yasuo Ishii, Jaekyu Lee, Krishnendra Nathella, and Dam Sunwoo. "[Re-establishing fetch-directed instruction 26* prefetching: An industry perspective.](https://doi.org/10.1109/ISPASS51385.2021.00034)" 2021 IEEE International 27* Symposium on Performance Analysis of Systems and Software (ISPASS). 2021. 28***************************************************************************************/ 29 30package xiangshan.frontend 31import chisel3._ 32import chisel3.util._ 33import freechips.rocketchip.diplomacy.LazyModule 34import freechips.rocketchip.diplomacy.LazyModuleImp 35import org.chipsalliance.cde.config.Parameters 36import utility._ 37import xiangshan._ 38import xiangshan.backend.fu.PFEvent 39import xiangshan.backend.fu.PMP 40import xiangshan.backend.fu.PMPChecker 41import xiangshan.backend.fu.PMPReqBundle 42import xiangshan.cache.mmu._ 43import xiangshan.frontend.icache._ 44 45class Frontend()(implicit p: Parameters) extends LazyModule with HasXSParameter { 46 override def shouldBeInlined: Boolean = false 47 val inner = LazyModule(new FrontendInlined) 48 lazy val module = new FrontendImp(this) 49} 50 51class FrontendImp(wrapper: Frontend)(implicit p: Parameters) extends LazyModuleImp(wrapper) { 52 val io = IO(wrapper.inner.module.io.cloneType) 53 val io_perf = IO(wrapper.inner.module.io_perf.cloneType) 54 io <> wrapper.inner.module.io 55 io_perf <> wrapper.inner.module.io_perf 56 if (p(DebugOptionsKey).ResetGen) { 57 ResetGen(ResetGenNode(Seq(ModuleNode(wrapper.inner.module))), reset, sim = false) 58 } 59} 60 61class FrontendInlined()(implicit p: Parameters) extends LazyModule with HasXSParameter { 62 override def shouldBeInlined: Boolean = true 63 64 val instrUncache = LazyModule(new InstrUncache()) 65 val icache = LazyModule(new ICache()) 66 67 lazy val module = new FrontendInlinedImp(this) 68} 69 70class FrontendInlinedImp(outer: FrontendInlined) extends LazyModuleImp(outer) 71 with HasXSParameter 72 with HasPerfEvents { 73 val io = IO(new Bundle() { 74 val hartId = Input(UInt(hartIdLen.W)) 75 val reset_vector = Input(UInt(PAddrBits.W)) 76 val fencei = Input(Bool()) 77 val ptw = new TlbPtwIO() 78 val backend = new FrontendToCtrlIO 79 val softPrefetch = Vec(backendParams.LduCnt, Flipped(Valid(new SoftIfetchPrefetchBundle))) 80 val sfence = Input(new SfenceBundle) 81 val tlbCsr = Input(new TlbCsrBundle) 82 val csrCtrl = Input(new CustomCSRCtrlIO) 83 val error = ValidIO(new L1CacheErrorInfo) 84 val frontendInfo = new Bundle { 85 val ibufFull = Output(Bool()) 86 val bpuInfo = new Bundle { 87 val bpRight = Output(UInt(XLEN.W)) 88 val bpWrong = Output(UInt(XLEN.W)) 89 } 90 } 91 val resetInFrontend = Output(Bool()) 92 val debugTopDown = new Bundle { 93 val robHeadVaddr = Flipped(Valid(UInt(VAddrBits.W))) 94 } 95 }) 96 97 // decouped-frontend modules 98 val instrUncache = outer.instrUncache.module 99 val icache = outer.icache.module 100 val bpu = Module(new Predictor) 101 val ifu = Module(new NewIFU) 102 val ibuffer = Module(new IBuffer) 103 val ftq = Module(new Ftq) 104 105 val needFlush = RegNext(io.backend.toFtq.redirect.valid) 106 val FlushControlRedirect = RegNext(io.backend.toFtq.redirect.bits.debugIsCtrl) 107 val FlushMemVioRedirect = RegNext(io.backend.toFtq.redirect.bits.debugIsMemVio) 108 val FlushControlBTBMiss = Wire(Bool()) 109 val FlushTAGEMiss = Wire(Bool()) 110 val FlushSCMiss = Wire(Bool()) 111 val FlushITTAGEMiss = Wire(Bool()) 112 val FlushRASMiss = Wire(Bool()) 113 114 val tlbCsr = DelayN(io.tlbCsr, 2) 115 val csrCtrl = DelayN(io.csrCtrl, 2) 116 val sfence = RegNext(RegNext(io.sfence)) 117 118 // trigger 119 ifu.io.frontendTrigger := csrCtrl.frontend_trigger 120 121 // RVCDecoder fsIsOff 122 ifu.io.csr_fsIsOff := csrCtrl.fsIsOff 123 124 // bpu ctrl 125 bpu.io.ctrl := csrCtrl.bp_ctrl 126 bpu.io.reset_vector := io.reset_vector 127 128 // pmp 129 val PortNumber = ICacheParameters().PortNumber 130 val pmp = Module(new PMP()) 131 val pmp_check = VecInit(Seq.fill(coreParams.ipmpPortNum)(Module(new PMPChecker(3, sameCycle = true)).io)) 132 pmp.io.distribute_csr := csrCtrl.distribute_csr 133 val pmp_req_vec = Wire(Vec(coreParams.ipmpPortNum, Valid(new PMPReqBundle()))) 134 (0 until 2 * PortNumber).foreach(i => pmp_req_vec(i) <> icache.io.pmp(i).req) 135 pmp_req_vec.last <> ifu.io.pmp.req 136 137 for (i <- pmp_check.indices) { 138 pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, pmp_req_vec(i)) 139 } 140 (0 until 2 * PortNumber).foreach(i => icache.io.pmp(i).resp <> pmp_check(i).resp) 141 ifu.io.pmp.resp <> pmp_check.last.resp 142 143 val itlb = 144 Module(new TLB(coreParams.itlbPortNum, nRespDups = 1, Seq.fill(PortNumber)(false) ++ Seq(true), itlbParams)) 145 itlb.io.requestor.take(PortNumber) zip icache.io.itlb foreach { case (a, b) => a <> b } 146 itlb.io.requestor.last <> ifu.io.iTLBInter // mmio may need re-tlb, blocked 147 itlb.io.hartId := io.hartId 148 itlb.io.base_connect(sfence, tlbCsr) 149 itlb.io.flushPipe.foreach(_ := icache.io.itlbFlushPipe) 150 itlb.io.redirect := DontCare // itlb has flushpipe, don't need redirect signal 151 152 val itlb_ptw = Wire(new VectorTlbPtwIO(coreParams.itlbPortNum)) 153 itlb_ptw.connect(itlb.io.ptw) 154 val itlbRepeater1 = PTWFilter(itlbParams.fenceDelay, itlb_ptw, sfence, tlbCsr, l2tlbParams.ifilterSize) 155 val itlbRepeater2 = 156 PTWRepeaterNB(passReady = false, itlbParams.fenceDelay, itlbRepeater1.io.ptw, io.ptw, sfence, tlbCsr) 157 158 icache.io.ftqPrefetch <> ftq.io.toPrefetch 159 icache.io.softPrefetch <> io.softPrefetch 160 161 // IFU-Ftq 162 ifu.io.ftqInter.fromFtq <> ftq.io.toIfu 163 ftq.io.toIfu.req.ready := ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready 164 165 ftq.io.fromIfu <> ifu.io.ftqInter.toFtq 166 bpu.io.ftq_to_bpu <> ftq.io.toBpu 167 ftq.io.fromBpu <> bpu.io.bpu_to_ftq 168 169 ftq.io.mmioCommitRead <> ifu.io.mmioCommitRead 170 171 // IFU-ICache 172 icache.io.fetch.req <> ftq.io.toICache.req 173 ftq.io.toICache.req.ready := ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready 174 175 ifu.io.icacheInter.resp <> icache.io.fetch.resp 176 ifu.io.icacheInter.icacheReady := icache.io.toIFU 177 ifu.io.icacheInter.topdownIcacheMiss := icache.io.fetch.topdownIcacheMiss 178 ifu.io.icacheInter.topdownItlbMiss := icache.io.fetch.topdownItlbMiss 179 icache.io.stop := ifu.io.icacheStop 180 icache.io.flush := ftq.io.icacheFlush 181 182 ifu.io.icachePerfInfo := icache.io.perfInfo 183 184 icache.io.csr_pf_enable := RegNext(csrCtrl.l1I_pf_enable) 185 icache.io.csr_parity_enable := RegNext(csrCtrl.icache_parity_enable) 186 187 icache.io.fencei := RegNext(io.fencei) 188 189 // IFU-Ibuffer 190 ifu.io.toIbuffer <> ibuffer.io.in 191 192 ftq.io.fromBackend <> io.backend.toFtq 193 io.backend.fromFtq := ftq.io.toBackend 194 io.backend.fromIfu := ifu.io.toBackend 195 io.frontendInfo.bpuInfo <> ftq.io.bpuInfo 196 197 val checkPcMem = Reg(Vec(FtqSize, new Ftq_RF_Components)) 198 when(ftq.io.toBackend.pc_mem_wen) { 199 checkPcMem(ftq.io.toBackend.pc_mem_waddr) := ftq.io.toBackend.pc_mem_wdata 200 } 201 202 val checkTargetPtr = Wire(Vec(DecodeWidth, new FtqPtr)) 203 val checkTarget = Wire(Vec(DecodeWidth, UInt(VAddrBits.W))) 204 205 for (i <- 0 until DecodeWidth) { 206 checkTargetPtr(i) := ibuffer.io.out(i).bits.ftqPtr 207 checkTarget(i) := Mux( 208 ftq.io.toBackend.newest_entry_ptr.value === checkTargetPtr(i).value, 209 ftq.io.toBackend.newest_entry_target, 210 checkPcMem((checkTargetPtr(i) + 1.U).value).startAddr 211 ) 212 } 213 214 // commented out for this br could be the last instruction in the fetch block 215 def checkNotTakenConsecutive = { 216 val prevNotTakenValid = RegInit(0.B) 217 val prevNotTakenFtqPtr = Reg(new FtqPtr) 218 for (i <- 0 until DecodeWidth - 1) { 219 // for instrs that is not the last, if a not-taken br, the next instr should have the same ftqPtr 220 // for instrs that is the last, record and check next request 221 when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr) { 222 when(ibuffer.io.out(i + 1).fire) { 223 // not last br, check now 224 }.otherwise { 225 // last br, record its info 226 prevNotTakenValid := true.B 227 prevNotTakenFtqPtr := checkTargetPtr(i) 228 } 229 } 230 XSError( 231 ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && 232 ibuffer.io.out(i + 1).fire && 233 checkTargetPtr(i).value =/= checkTargetPtr(i + 1).value, 234 "not-taken br should have same ftqPtr\n" 235 ) 236 } 237 when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr) { 238 // last instr is a br, record its info 239 prevNotTakenValid := true.B 240 prevNotTakenFtqPtr := checkTargetPtr(DecodeWidth - 1) 241 } 242 when(prevNotTakenValid && ibuffer.io.out(0).fire) { 243 prevNotTakenValid := false.B 244 } 245 XSError( 246 prevNotTakenValid && ibuffer.io.out(0).fire && 247 prevNotTakenFtqPtr.value =/= checkTargetPtr(0).value, 248 "not-taken br should have same ftqPtr\n" 249 ) 250 251 when(needFlush) { 252 prevNotTakenValid := false.B 253 } 254 } 255 256 def checkTakenNotConsecutive = { 257 val prevTakenValid = RegInit(0.B) 258 val prevTakenFtqPtr = Reg(new FtqPtr) 259 for (i <- 0 until DecodeWidth - 1) { 260 // for instrs that is not the last, if a taken br, the next instr should not have the same ftqPtr 261 // for instrs that is the last, record and check next request 262 when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && ibuffer.io.out(i).bits.pred_taken) { 263 when(ibuffer.io.out(i + 1).fire) { 264 // not last br, check now 265 }.otherwise { 266 // last br, record its info 267 prevTakenValid := true.B 268 prevTakenFtqPtr := checkTargetPtr(i) 269 } 270 } 271 XSError( 272 ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && ibuffer.io.out(i).bits.pred_taken && 273 ibuffer.io.out(i + 1).fire && 274 (checkTargetPtr(i) + 1.U).value =/= checkTargetPtr(i + 1).value, 275 "taken br should have consecutive ftqPtr\n" 276 ) 277 } 278 when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && ibuffer.io.out( 279 DecodeWidth - 1 280 ).bits.pred_taken) { 281 // last instr is a br, record its info 282 prevTakenValid := true.B 283 prevTakenFtqPtr := checkTargetPtr(DecodeWidth - 1) 284 } 285 when(prevTakenValid && ibuffer.io.out(0).fire) { 286 prevTakenValid := false.B 287 } 288 XSError( 289 prevTakenValid && ibuffer.io.out(0).fire && 290 (prevTakenFtqPtr + 1.U).value =/= checkTargetPtr(0).value, 291 "taken br should have consecutive ftqPtr\n" 292 ) 293 when(needFlush) { 294 prevTakenValid := false.B 295 } 296 } 297 298 def checkNotTakenPC = { 299 val prevNotTakenPC = Reg(UInt(VAddrBits.W)) 300 val prevIsRVC = Reg(Bool()) 301 val prevNotTakenValid = RegInit(0.B) 302 303 for (i <- 0 until DecodeWidth - 1) { 304 when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && !ibuffer.io.out(i).bits.pred_taken) { 305 when(ibuffer.io.out(i + 1).fire) {}.otherwise { 306 prevNotTakenValid := true.B 307 prevIsRVC := ibuffer.io.out(i).bits.pd.isRVC 308 prevNotTakenPC := ibuffer.io.out(i).bits.pc 309 } 310 } 311 XSError( 312 ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && !ibuffer.io.out(i).bits.pred_taken && 313 ibuffer.io.out(i + 1).fire && 314 ibuffer.io.out(i).bits.pc + Mux(ibuffer.io.out(i).bits.pd.isRVC, 2.U, 4.U) =/= ibuffer.io.out( 315 i + 1 316 ).bits.pc, 317 "not-taken br should have consecutive pc\n" 318 ) 319 } 320 when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && !ibuffer.io.out( 321 DecodeWidth - 1 322 ).bits.pred_taken) { 323 prevNotTakenValid := true.B 324 prevIsRVC := ibuffer.io.out(DecodeWidth - 1).bits.pd.isRVC 325 prevNotTakenPC := ibuffer.io.out(DecodeWidth - 1).bits.pc 326 } 327 when(prevNotTakenValid && ibuffer.io.out(0).fire) { 328 prevNotTakenValid := false.B 329 } 330 XSError( 331 prevNotTakenValid && ibuffer.io.out(0).fire && 332 prevNotTakenPC + Mux(prevIsRVC, 2.U, 4.U) =/= ibuffer.io.out(0).bits.pc, 333 "not-taken br should have same pc\n" 334 ) 335 when(needFlush) { 336 prevNotTakenValid := false.B 337 } 338 } 339 340 def checkTakenPC = { 341 val prevTakenFtqPtr = Reg(new FtqPtr) 342 val prevTakenValid = RegInit(0.B) 343 val prevTakenTarget = Wire(UInt(VAddrBits.W)) 344 prevTakenTarget := checkPcMem((prevTakenFtqPtr + 1.U).value).startAddr 345 346 for (i <- 0 until DecodeWidth - 1) { 347 when(ibuffer.io.out(i).fire && !ibuffer.io.out(i).bits.pd.notCFI && ibuffer.io.out(i).bits.pred_taken) { 348 when(ibuffer.io.out(i + 1).fire) {}.otherwise { 349 prevTakenValid := true.B 350 prevTakenFtqPtr := checkTargetPtr(i) 351 } 352 } 353 XSError( 354 ibuffer.io.out(i).fire && !ibuffer.io.out(i).bits.pd.notCFI && ibuffer.io.out(i).bits.pred_taken && 355 ibuffer.io.out(i + 1).fire && 356 checkTarget(i) =/= ibuffer.io.out(i + 1).bits.pc, 357 "taken instr should follow target pc\n" 358 ) 359 } 360 when(ibuffer.io.out(DecodeWidth - 1).fire && !ibuffer.io.out(DecodeWidth - 1).bits.pd.notCFI && ibuffer.io.out( 361 DecodeWidth - 1 362 ).bits.pred_taken) { 363 prevTakenValid := true.B 364 prevTakenFtqPtr := checkTargetPtr(DecodeWidth - 1) 365 } 366 when(prevTakenValid && ibuffer.io.out(0).fire) { 367 prevTakenValid := false.B 368 } 369 XSError( 370 prevTakenValid && ibuffer.io.out(0).fire && 371 prevTakenTarget =/= ibuffer.io.out(0).bits.pc, 372 "taken instr should follow target pc\n" 373 ) 374 when(needFlush) { 375 prevTakenValid := false.B 376 } 377 } 378 379 // checkNotTakenConsecutive 380 checkTakenNotConsecutive 381 checkTakenPC 382 checkNotTakenPC 383 384 ifu.io.rob_commits <> io.backend.toFtq.rob_commits 385 386 ibuffer.io.flush := needFlush 387 ibuffer.io.ControlRedirect := FlushControlRedirect 388 ibuffer.io.MemVioRedirect := FlushMemVioRedirect 389 ibuffer.io.ControlBTBMissBubble := FlushControlBTBMiss 390 ibuffer.io.TAGEMissBubble := FlushTAGEMiss 391 ibuffer.io.SCMissBubble := FlushSCMiss 392 ibuffer.io.ITTAGEMissBubble := FlushITTAGEMiss 393 ibuffer.io.RASMissBubble := FlushRASMiss 394 ibuffer.io.decodeCanAccept := io.backend.canAccept 395 396 FlushControlBTBMiss := ftq.io.ControlBTBMissBubble 397 FlushTAGEMiss := ftq.io.TAGEMissBubble 398 FlushSCMiss := ftq.io.SCMissBubble 399 FlushITTAGEMiss := ftq.io.ITTAGEMissBubble 400 FlushRASMiss := ftq.io.RASMissBubble 401 402 io.backend.cfVec <> ibuffer.io.out 403 io.backend.stallReason <> ibuffer.io.stallReason 404 405 instrUncache.io.req <> ifu.io.uncacheInter.toUncache 406 ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp 407 instrUncache.io.flush := false.B 408 io.error <> RegNext(RegNext(icache.io.error)) 409 410 icache.io.hartId := io.hartId 411 412 itlbRepeater1.io.debugTopDown.robHeadVaddr := io.debugTopDown.robHeadVaddr 413 414 val frontendBubble = Mux(io.backend.canAccept, DecodeWidth.U - PopCount(ibuffer.io.out.map(_.valid)), 0.U) 415 XSPerfAccumulate("FrontendBubble", frontendBubble) 416 io.frontendInfo.ibufFull := RegNext(ibuffer.io.full) 417 io.resetInFrontend := reset.asBool 418 419 // PFEvent 420 val pfevent = Module(new PFEvent) 421 pfevent.io.distribute_csr := io.csrCtrl.distribute_csr 422 val csrevents = pfevent.io.hpmevent.take(8) 423 424 val perfFromUnits = Seq(ifu, ibuffer, icache, ftq, bpu).flatMap(_.getPerfEvents) 425 val perfFromIO = Seq() 426 val perfBlock = Seq() 427 // let index = 0 be no event 428 val allPerfEvents = Seq(("noEvent", 0.U)) ++ perfFromUnits ++ perfFromIO ++ perfBlock 429 430 if (printEventCoding) { 431 for (((name, inc), i) <- allPerfEvents.zipWithIndex) { 432 println("Frontend perfEvents Set", name, inc, i) 433 } 434 } 435 436 val allPerfInc = allPerfEvents.map(_._2.asTypeOf(new PerfEvent)) 437 override val perfEvents = HPerfMonitor(csrevents, allPerfInc).getPerfEvents 438 generatePerfEvent() 439} 440