xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision 9473e04d5cab97eaf63add958b2392eec3d876a2)
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(4)
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 pmp = Module(new PMP())
88  val pmp_check = VecInit(Seq.fill(4)(Module(new PMPChecker(3, sameCycle = true)).io))
89  pmp.io.distribute_csr := csrCtrl.distribute_csr
90  val pmp_req_vec     = Wire(Vec(4, Valid(new PMPReqBundle())))
91  pmp_req_vec(0) <> icache.io.pmp(0).req
92  pmp_req_vec(1) <> icache.io.pmp(1).req
93  pmp_req_vec(2) <> icache.io.pmp(2).req
94  pmp_req_vec(3) <> ifu.io.pmp.req
95
96  for (i <- pmp_check.indices) {
97    pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, pmp_req_vec(i))
98  }
99  icache.io.pmp(0).resp <> pmp_check(0).resp
100  icache.io.pmp(1).resp <> pmp_check(1).resp
101  icache.io.pmp(2).resp <> pmp_check(2).resp
102  ifu.io.pmp.resp <> pmp_check(3).resp
103
104  val itlb = Module(new TLB(4, nRespDups = 1, Seq(true, true, false, true), itlbParams))
105  itlb.io.requestor.take(3) zip icache.io.itlb foreach {case (a,b) => a <> b}
106  itlb.io.requestor(3) <> ifu.io.iTLBInter // mmio may need re-tlb, blocked
107  itlb.io.base_connect(io.sfence, tlbCsr)
108  io.ptw.connect(itlb.io.ptw)
109  itlb.io.ptw_replenish <> DontCare
110  itlb.io.flushPipe.map(_ := needFlush)
111
112  icache.io.prefetch <> ftq.io.toPrefetch
113
114
115  //IFU-Ftq
116  ifu.io.ftqInter.fromFtq <> ftq.io.toIfu
117  ftq.io.toIfu.req.ready :=  ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
118
119  ftq.io.fromIfu          <> ifu.io.ftqInter.toFtq
120  bpu.io.ftq_to_bpu       <> ftq.io.toBpu
121  ftq.io.fromBpu          <> bpu.io.bpu_to_ftq
122
123  ftq.io.mmioCommitRead   <> ifu.io.mmioCommitRead
124  //IFU-ICache
125
126  icache.io.fetch.req <> ftq.io.toICache.req
127  ftq.io.toICache.req.ready :=  ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
128
129  ifu.io.icacheInter.resp <>    icache.io.fetch.resp
130  ifu.io.icacheInter.icacheReady :=  icache.io.toIFU
131  icache.io.stop := ifu.io.icacheStop
132
133  ifu.io.icachePerfInfo := icache.io.perfInfo
134
135  icache.io.csr.distribute_csr <> csrCtrl.distribute_csr
136  io.csrUpdate := RegNext(icache.io.csr.update)
137
138  icache.io.csr_pf_enable     := RegNext(csrCtrl.l1I_pf_enable)
139  icache.io.csr_parity_enable := RegNext(csrCtrl.icache_parity_enable)
140
141  //IFU-Ibuffer
142  ifu.io.toIbuffer    <> ibuffer.io.in
143
144  ftq.io.fromBackend <> io.backend.toFtq
145  io.backend.fromFtq <> ftq.io.toBackend
146  io.frontendInfo.bpuInfo <> ftq.io.bpuInfo
147
148  ifu.io.rob_commits <> io.backend.toFtq.rob_commits
149
150  ibuffer.io.flush := needFlush
151  io.backend.cfVec <> ibuffer.io.out
152
153  instrUncache.io.req   <> ifu.io.uncacheInter.toUncache
154  ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp
155  instrUncache.io.flush := false.B
156  io.error <> RegNext(RegNext(icache.io.error))
157
158  icache.io.hartId := io.hartId
159
160  val frontendBubble = PopCount((0 until DecodeWidth).map(i => io.backend.cfVec(i).ready && !ibuffer.io.out(i).valid))
161  XSPerfAccumulate("FrontendBubble", frontendBubble)
162  io.frontendInfo.ibufFull := RegNext(ibuffer.io.full)
163
164  // PFEvent
165  val pfevent = Module(new PFEvent)
166  pfevent.io.distribute_csr := io.csrCtrl.distribute_csr
167  val csrevents = pfevent.io.hpmevent.take(8)
168
169  val allPerfEvents = Seq(ifu, ibuffer, icache, ftq, bpu).flatMap(_.getPerf)
170  override val perfEvents = HPerfMonitor(csrevents, allPerfEvents).getPerfEvents
171  generatePerfEvent()
172}
173