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