xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision 5c5bd416ce761d956348a8e2fbbf268922371d8b)
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{
27  val io = IO(new Bundle() {
28    val icacheMemAcq = DecoupledIO(new L1plusCacheReq)
29    val icacheMemGrant = Flipped(DecoupledIO(new L1plusCacheResp))
30    val l1plusFlush = Output(Bool())
31    val fencei = Input(Bool())
32    val ptw = new TlbPtwIO
33    val backend = new FrontendToBackendIO
34    val sfence = Input(new SfenceBundle)
35    val tlbCsr = Input(new TlbCsrBundle)
36    val csrCtrl = Input(new CustomCSRCtrlIO)
37    val error  = new L1CacheErrorInfo
38  })
39
40  val ifu = Module(new IFU)
41  val ibuffer =  Module(new Ibuffer)
42  val l1plusPrefetcher = Module(new L1plusPrefetcher)
43  val instrUncache = outer.instrUncache.module
44
45  val needFlush = io.backend.redirect_cfiUpdate.valid
46
47  // from backend
48  ifu.io.redirect <> io.backend.redirect_cfiUpdate
49  ifu.io.bp_ctrl <> io.csrCtrl.bp_ctrl
50  ifu.io.commitUpdate <> io.backend.commit_cfiUpdate
51  ifu.io.ftqEnqPtr <> io.backend.ftqEnqPtr
52  ifu.io.ftqLeftOne <> io.backend.ftqLeftOne
53  // to icache
54  val grantClientId = clientId(io.icacheMemGrant.bits.id)
55  val grantEntryId = entryId(io.icacheMemGrant.bits.id)
56  ifu.io.icacheMemGrant.valid := io.icacheMemGrant.valid && grantClientId === icacheMissQueueId.U
57  ifu.io.icacheMemGrant.bits := io.icacheMemGrant.bits
58  ifu.io.icacheMemGrant.bits.id := Cat(0.U(clientIdWidth.W), grantEntryId)
59  l1plusPrefetcher.io.mem_grant.valid := io.icacheMemGrant.valid && grantClientId === l1plusPrefetcherId.U
60  l1plusPrefetcher.io.mem_grant.bits := io.icacheMemGrant.bits
61  l1plusPrefetcher.io.mem_grant.bits.id := Cat(0.U(clientIdWidth.W), grantEntryId)
62  assert(RegNext(!l1plusPrefetcher.io.mem_grant.valid || (l1plusPrefetcher.io.mem_grant.ready && grantClientId === l1plusPrefetcherId.U)))
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