1package xiangshan.frontend 2import utils._ 3import chisel3._ 4import chisel3.util._ 5import chipsalliance.rocketchip.config.Parameters 6import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 7import xiangshan._ 8import xiangshan.cache._ 9import xiangshan.cache.prefetch.L1plusPrefetcher 10import xiangshan.backend.fu.HasExceptionNO 11import system.L1CacheErrorInfo 12 13 14class Frontend()(implicit p: Parameters) extends LazyModule with HasXSParameter{ 15 16 val instrUncache = LazyModule(new InstrUncache()) 17 18 lazy val module = new FrontendImp(this) 19} 20 21 22class FrontendImp (outer: Frontend) extends LazyModuleImp(outer) 23 with HasL1plusCacheParameters 24 with HasXSParameter 25 with HasExceptionNO 26 with HasXSLog 27{ 28 val io = IO(new Bundle() { 29 val icacheMemAcq = DecoupledIO(new L1plusCacheReq) 30 val icacheMemGrant = Flipped(DecoupledIO(new L1plusCacheResp)) 31 val l1plusFlush = Output(Bool()) 32 val fencei = Input(Bool()) 33 val ptw = new TlbPtwIO 34 val backend = new FrontendToBackendIO 35 val sfence = Input(new SfenceBundle) 36 val tlbCsr = Input(new TlbCsrBundle) 37 val csrCtrl = Input(new CustomCSRCtrlIO) 38 val error = new L1CacheErrorInfo 39 }) 40 41 val ifu = Module(new IFU) 42 val ibuffer = Module(new Ibuffer) 43 val l1plusPrefetcher = Module(new L1plusPrefetcher) 44 val instrUncache = outer.instrUncache.module 45 46 val needFlush = io.backend.redirect_cfiUpdate.valid 47 48 // from backend 49 ifu.io.redirect <> io.backend.redirect_cfiUpdate 50 ifu.io.bp_ctrl <> io.csrCtrl.bp_ctrl 51 ifu.io.commitUpdate <> io.backend.commit_cfiUpdate 52 ifu.io.ftqEnqPtr <> io.backend.ftqEnqPtr 53 ifu.io.ftqLeftOne <> io.backend.ftqLeftOne 54 // to icache 55 val grantClientId = clientId(io.icacheMemGrant.bits.id) 56 val grantEntryId = entryId(io.icacheMemGrant.bits.id) 57 ifu.io.icacheMemGrant.valid := io.icacheMemGrant.valid && grantClientId === icacheMissQueueId.U 58 ifu.io.icacheMemGrant.bits := io.icacheMemGrant.bits 59 ifu.io.icacheMemGrant.bits.id := Cat(0.U(clientIdWidth.W), grantEntryId) 60 l1plusPrefetcher.io.mem_grant.valid := io.icacheMemGrant.valid && grantClientId === l1plusPrefetcherId.U 61 l1plusPrefetcher.io.mem_grant.bits := io.icacheMemGrant.bits 62 l1plusPrefetcher.io.mem_grant.bits.id := Cat(0.U(clientIdWidth.W), grantEntryId) 63 io.icacheMemGrant.ready := Mux(grantClientId === icacheMissQueueId.U, 64 ifu.io.icacheMemGrant.ready, 65 l1plusPrefetcher.io.mem_grant.ready) 66 ifu.io.fencei := io.fencei 67 68 69 instrUncache.io.req <> ifu.io.mmio_acquire 70 instrUncache.io.resp <> ifu.io.mmio_grant 71 instrUncache.io.flush <> ifu.io.mmio_flush 72 // to tlb 73 ifu.io.sfence := io.sfence 74 ifu.io.tlbCsr := io.tlbCsr 75 // from icache and l1plus prefetcher 76 io.l1plusFlush := ifu.io.l1plusFlush 77 l1plusPrefetcher.io.in.valid := ifu.io.prefetchTrainReq.valid 78 l1plusPrefetcher.io.in.bits := ifu.io.prefetchTrainReq.bits 79 l1plusPrefetcher.io.enable := RegNext(io.csrCtrl.l1plus_pf_enable) 80 val memAcquireArb = Module(new Arbiter(new L1plusCacheReq, nClients)) 81 memAcquireArb.io.in(icacheMissQueueId) <> ifu.io.icacheMemAcq 82 memAcquireArb.io.in(icacheMissQueueId).bits.id := Cat(icacheMissQueueId.U(clientIdWidth.W), 83 entryId(ifu.io.icacheMemAcq.bits.id)) 84 memAcquireArb.io.in(l1plusPrefetcherId) <> l1plusPrefetcher.io.mem_acquire 85 memAcquireArb.io.in(l1plusPrefetcherId).bits.id := Cat(l1plusPrefetcherId.U(clientIdWidth.W), 86 entryId(l1plusPrefetcher.io.mem_acquire.bits.id)) 87 io.icacheMemAcq <> memAcquireArb.io.out 88 // itlb to ptw 89 io.ptw <> ifu.io.ptw 90 // ifu to ibuffer 91 ibuffer.io.in <> ifu.io.fetchPacket 92 // backend to ibuffer 93 ibuffer.io.flush := needFlush 94 // ibuffer to backend 95 io.backend.cfVec <> ibuffer.io.out 96 // ifu to backend 97 io.backend.fetchInfo <> ifu.io.toFtq 98 99 io.error <> RegNext(ifu.io.error) 100 101 // for(out <- ibuffer.io.out){ 102 // XSInfo(out.fire(), 103 // p"inst:${Hexadecimal(out.bits.instr)} pc:${Hexadecimal(out.bits.pc)}\n" 104 // ) 105 // } 106 107 val frontendBubble = PopCount((0 until DecodeWidth).map(i => io.backend.cfVec(i).ready && !ibuffer.io.out(i).valid)) 108 XSPerfAccumulate("FrontendBubble", frontendBubble) 109} 110