xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision 6ab6918f8d678bdfe6b2bd2a326acf56cb4325b6)
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 xiangshan._
24import xiangshan.backend.fu.{PFEvent, PMP, PMPChecker}
25import xiangshan.cache.mmu.{TLB, TlbPtwIO}
26import xiangshan.frontend.icache._
27
28
29class Frontend()(implicit p: Parameters) extends LazyModule with HasXSParameter{
30
31  val instrUncache  = LazyModule(new InstrUncache())
32  val icache        = LazyModule(new ICache())
33
34  lazy val module = new FrontendImp(this)
35}
36
37
38class FrontendImp (outer: Frontend) extends LazyModuleImp(outer)
39  with HasXSParameter
40{
41  val io = IO(new Bundle() {
42    val fencei = Input(Bool())
43    val ptw = new TlbPtwIO(2)
44    val backend = new FrontendToCtrlIO
45    val sfence = Input(new SfenceBundle)
46    val tlbCsr = Input(new TlbCsrBundle)
47    val csrCtrl = Input(new CustomCSRCtrlIO)
48    val csrUpdate = new DistributedCSRUpdateReq
49    val error  = new L1CacheErrorInfo
50    val frontendInfo = new Bundle {
51      val ibufFull  = Output(Bool())
52      val bpuInfo = new Bundle {
53        val bpRight = Output(UInt(XLEN.W))
54        val bpWrong = Output(UInt(XLEN.W))
55      }
56    }
57  })
58
59  //decouped-frontend modules
60  val instrUncache = outer.instrUncache.module
61  val icache       = outer.icache.module
62  val bpu     = Module(new Predictor)
63  val ifu     = Module(new NewIFU)
64  val ibuffer =  Module(new Ibuffer)
65  val ftq = Module(new Ftq)
66
67  //PFEvent
68  val pfevent = Module(new PFEvent)
69  val tlbCsr = RegNext(io.tlbCsr)
70  pfevent.io.distribute_csr := io.csrCtrl.distribute_csr
71
72  // trigger
73  ifu.io.frontendTrigger := io.csrCtrl.frontend_trigger
74  val triggerEn = io.csrCtrl.trigger_enable
75  ifu.io.csrTriggerEnable := VecInit(triggerEn(0), triggerEn(1), triggerEn(6), triggerEn(8))
76
77  // pmp
78  val pmp = Module(new PMP())
79  val pmp_check = VecInit(Seq.fill(2)(Module(new PMPChecker(3, sameCycle = true)).io))
80  pmp.io.distribute_csr := io.csrCtrl.distribute_csr
81  for (i <- pmp_check.indices) {
82    pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, icache.io.pmp(i).req)
83    icache.io.pmp(i).resp <> pmp_check(i).resp
84  }
85
86  io.ptw <> TLB(
87    in = Seq(icache.io.itlb(0), icache.io.itlb(1)),
88    sfence = io.sfence,
89    csr = tlbCsr,
90    width = 2,
91    shouldBlock = true,
92    itlbParams
93  )
94
95  icache.io.fencei := RegNext(io.fencei)
96
97  val needFlush = io.backend.toFtq.stage3Redirect.valid
98
99  //IFU-Ftq
100  ifu.io.ftqInter.fromFtq <> ftq.io.toIfu
101  ftq.io.fromIfu          <> ifu.io.ftqInter.toFtq
102  bpu.io.ftq_to_bpu       <> ftq.io.toBpu
103  ftq.io.fromBpu          <> bpu.io.bpu_to_ftq
104  //IFU-ICache
105  for(i <- 0 until 2){
106    ifu.io.icacheInter(i).req       <>      icache.io.fetch(i).req
107    icache.io.fetch(i).req <> ifu.io.icacheInter(i).req
108    ifu.io.icacheInter(i).resp <> icache.io.fetch(i).resp
109  }
110  icache.io.stop := ifu.io.icacheStop
111
112  ifu.io.icachePerfInfo := icache.io.perfInfo
113
114  //icache.io.missQueue.flush := ifu.io.ftqInter.fromFtq.redirect.valid || (ifu.io.ftqInter.toFtq.pdWb.valid && ifu.io.ftqInter.toFtq.pdWb.bits.misOffset.valid)
115
116  icache.io.csr.distribute_csr <> io.csrCtrl.distribute_csr
117  icache.io.csr.update <> io.csrUpdate
118
119  //IFU-Ibuffer
120  ifu.io.toIbuffer    <> ibuffer.io.in
121
122  ftq.io.fromBackend <> io.backend.toFtq
123  io.backend.fromFtq <> ftq.io.toBackend
124  io.frontendInfo.bpuInfo <> ftq.io.bpuInfo
125
126  ifu.io.rob_commits <> io.backend.toFtq.rob_commits
127
128  ibuffer.io.flush := needFlush
129  io.backend.cfVec <> ibuffer.io.out
130
131  instrUncache.io.req   <> ifu.io.uncacheInter.toUncache
132  ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp
133  instrUncache.io.flush := false.B//icache.io.missQueue.flush
134  io.error <> DontCare
135
136  val frontendBubble = PopCount((0 until DecodeWidth).map(i => io.backend.cfVec(i).ready && !ibuffer.io.out(i).valid))
137  XSPerfAccumulate("FrontendBubble", frontendBubble)
138  io.frontendInfo.ibufFull := RegNext(ibuffer.io.full)
139
140  if(print_perfcounter){
141    val ifu_perf     = ifu.perfEvents.map(_._1).zip(ifu.perfinfo.perfEvents.perf_events)
142    val ibuffer_perf = ibuffer.perfEvents.map(_._1).zip(ibuffer.perfinfo.perfEvents.perf_events)
143    val icache_perf  = icache.perfEvents.map(_._1).zip(icache.perfinfo.perfEvents.perf_events)
144    val ftq_perf     = ftq.perfEvents.map(_._1).zip(ftq.perfinfo.perfEvents.perf_events)
145    val bpu_perf     = bpu.perfEvents.map(_._1).zip(bpu.perfinfo.perfEvents.perf_events)
146    val perfEvents = ifu_perf ++ ibuffer_perf ++ icache_perf ++ ftq_perf ++ bpu_perf
147
148    for (((perf_name,perf),i) <- perfEvents.zipWithIndex) {
149      println(s"frontend perf $i: $perf_name")
150    }
151  }
152
153  val hpmEvents = ifu.perfinfo.perfEvents.perf_events ++ ibuffer.perfinfo.perfEvents.perf_events ++
154                  icache.perfinfo.perfEvents.perf_events ++ ftq.perfinfo.perfEvents.perf_events ++
155                  bpu.perfinfo.perfEvents.perf_events
156  val perf_length = hpmEvents.length
157  val csrevents = pfevent.io.hpmevent.slice(0,8)
158  val perfinfo = IO(new Bundle(){
159    val perfEvents        = Output(new PerfEventsBundle(csrevents.length))
160  })
161  val hpm_frontend = Module(new HPerfmonitor(perf_length,csrevents.length))
162  hpm_frontend.io.hpm_event := csrevents
163  hpm_frontend.io.events_sets.perf_events := hpmEvents
164  perfinfo.perfEvents := RegNext(hpm_frontend.io.events_selected)
165}
166