xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision 34f9624d7cc45b990e48286ffb813fdad2ae1172)
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
32  val instrUncache  = LazyModule(new InstrUncache())
33  val icache        = LazyModule(new ICache())
34
35  lazy val module = new FrontendImp(this)
36}
37
38
39class FrontendImp (outer: Frontend) extends LazyModuleImp(outer)
40  with HasXSParameter
41  with HasPerfEvents
42{
43  val io = IO(new Bundle() {
44    val hartId = Input(UInt(8.W))
45    val reset_vector = Input(UInt(PAddrBits.W))
46    val fencei = Input(Bool())
47    val ptw = new VectorTlbPtwIO(coreParams.itlbPortNum)
48    val backend = new FrontendToCtrlIO
49    val sfence = Input(new SfenceBundle)
50    val tlbCsr = Input(new TlbCsrBundle)
51    val csrCtrl = Input(new CustomCSRCtrlIO)
52    val csrUpdate = new DistributedCSRUpdateReq
53    val error  = new L1CacheErrorInfo
54    val frontendInfo = new Bundle {
55      val ibufFull  = Output(Bool())
56      val bpuInfo = new Bundle {
57        val bpRight = Output(UInt(XLEN.W))
58        val bpWrong = Output(UInt(XLEN.W))
59      }
60    }
61  })
62
63  //decouped-frontend modules
64  val instrUncache = outer.instrUncache.module
65  val icache       = outer.icache.module
66  val bpu     = Module(new Predictor)
67  val ifu     = Module(new NewIFU)
68  val ibuffer =  Module(new Ibuffer)
69  val ftq = Module(new Ftq)
70
71  val needFlush = RegNext(io.backend.toFtq.redirect.valid)
72
73  val tlbCsr = DelayN(io.tlbCsr, 2)
74  val csrCtrl = DelayN(io.csrCtrl, 2)
75  val sfence = RegNext(RegNext(io.sfence))
76
77  // trigger
78  ifu.io.frontendTrigger := csrCtrl.frontend_trigger
79  val triggerEn = csrCtrl.trigger_enable
80  ifu.io.csrTriggerEnable := VecInit(triggerEn(0), triggerEn(1), triggerEn(6), triggerEn(8))
81
82  // bpu ctrl
83  bpu.io.ctrl := csrCtrl.bp_ctrl
84  bpu.io.reset_vector := io.reset_vector
85
86// pmp
87  val prefetchPipeNum = ICacheParameters().prefetchPipeNum
88  val pmp = Module(new PMP())
89  val pmp_check = VecInit(Seq.fill(coreParams.ipmpPortNum)(Module(new PMPChecker(3, sameCycle = true)).io))
90  pmp.io.distribute_csr := csrCtrl.distribute_csr
91  val pmp_req_vec     = Wire(Vec(coreParams.ipmpPortNum, Valid(new PMPReqBundle())))
92  (0 until 2 + prefetchPipeNum).foreach(i => pmp_req_vec(i) <> icache.io.pmp(i).req)
93  pmp_req_vec.last <> ifu.io.pmp.req
94
95  for (i <- pmp_check.indices) {
96    pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, pmp_req_vec(i))
97  }
98  (0 until 2 + prefetchPipeNum).foreach(i => icache.io.pmp(i).resp <> pmp_check(i).resp)
99  ifu.io.pmp.resp <> pmp_check.last.resp
100
101  val itlb = Module(new TLB(coreParams.itlbPortNum, nRespDups = 1,
102    Seq(true, true) ++ Seq.fill(prefetchPipeNum)(false) ++ Seq(true), itlbParams))
103  itlb.io.requestor.take(2 + prefetchPipeNum) zip icache.io.itlb foreach {case (a,b) => a <> b}
104  itlb.io.requestor.last <> ifu.io.iTLBInter // mmio may need re-tlb, blocked
105  itlb.io.base_connect(io.sfence, tlbCsr)
106  io.ptw.connect(itlb.io.ptw)
107  itlb.io.ptw_replenish <> DontCare
108  itlb.io.flushPipe.map(_ := needFlush)
109
110  icache.io.prefetch <> ftq.io.toPrefetch
111
112
113  //IFU-Ftq
114  ifu.io.ftqInter.fromFtq <> ftq.io.toIfu
115  ftq.io.toIfu.req.ready :=  ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
116
117  ftq.io.fromIfu          <> ifu.io.ftqInter.toFtq
118  bpu.io.ftq_to_bpu       <> ftq.io.toBpu
119  ftq.io.fromBpu          <> bpu.io.bpu_to_ftq
120
121  ftq.io.mmioCommitRead   <> ifu.io.mmioCommitRead
122  //IFU-ICache
123
124  icache.io.fetch.req <> ftq.io.toICache.req
125  ftq.io.toICache.req.ready :=  ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
126
127  ifu.io.icacheInter.resp <>    icache.io.fetch.resp
128  ifu.io.icacheInter.icacheReady :=  icache.io.toIFU
129  icache.io.stop := ifu.io.icacheStop
130
131  ifu.io.icachePerfInfo := icache.io.perfInfo
132
133  icache.io.csr.distribute_csr <> csrCtrl.distribute_csr
134  io.csrUpdate := RegNext(icache.io.csr.update)
135
136  icache.io.csr_pf_enable     := RegNext(csrCtrl.l1I_pf_enable)
137  icache.io.csr_parity_enable := RegNext(csrCtrl.icache_parity_enable)
138
139  //IFU-Ibuffer
140  ifu.io.toIbuffer    <> ibuffer.io.in
141
142  ftq.io.fromBackend <> io.backend.toFtq
143  io.backend.fromFtq <> ftq.io.toBackend
144  io.frontendInfo.bpuInfo <> ftq.io.bpuInfo
145
146  ifu.io.rob_commits <> io.backend.toFtq.rob_commits
147
148  ibuffer.io.flush := needFlush
149  io.backend.cfVec <> ibuffer.io.out
150
151  instrUncache.io.req   <> ifu.io.uncacheInter.toUncache
152  ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp
153  instrUncache.io.flush := false.B
154  io.error <> RegNext(RegNext(icache.io.error))
155
156  icache.io.hartId := io.hartId
157
158  val frontendBubble = PopCount((0 until DecodeWidth).map(i => io.backend.cfVec(i).ready && !ibuffer.io.out(i).valid))
159  XSPerfAccumulate("FrontendBubble", frontendBubble)
160  io.frontendInfo.ibufFull := RegNext(ibuffer.io.full)
161
162  // PFEvent
163  val pfevent = Module(new PFEvent)
164  pfevent.io.distribute_csr := io.csrCtrl.distribute_csr
165  val csrevents = pfevent.io.hpmevent.take(8)
166
167  val allPerfEvents = Seq(ifu, ibuffer, icache, ftq, bpu).flatMap(_.getPerf)
168  override val perfEvents = HPerfMonitor(csrevents, allPerfEvents).getPerfEvents
169  generatePerfEvent()
170}
171