xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision 98c716025d86d6220b2762171d9383f72e72cfe7)
109c6f1ddSLingrui98/***************************************************************************************
209c6f1ddSLingrui98* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
309c6f1ddSLingrui98* Copyright (c) 2020-2021 Peng Cheng Laboratory
409c6f1ddSLingrui98*
509c6f1ddSLingrui98* XiangShan is licensed under Mulan PSL v2.
609c6f1ddSLingrui98* You can use this software according to the terms and conditions of the Mulan PSL v2.
709c6f1ddSLingrui98* You may obtain a copy of Mulan PSL v2 at:
809c6f1ddSLingrui98*          http://license.coscl.org.cn/MulanPSL2
909c6f1ddSLingrui98*
1009c6f1ddSLingrui98* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
1109c6f1ddSLingrui98* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
1209c6f1ddSLingrui98* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
1309c6f1ddSLingrui98*
1409c6f1ddSLingrui98* See the Mulan PSL v2 for more details.
1509c6f1ddSLingrui98***************************************************************************************/
1609c6f1ddSLingrui98
1709c6f1ddSLingrui98package xiangshan.frontend
1809c6f1ddSLingrui98import utils._
1909c6f1ddSLingrui98import chisel3._
2009c6f1ddSLingrui98import chisel3.util._
2109c6f1ddSLingrui98import chipsalliance.rocketchip.config.Parameters
2209c6f1ddSLingrui98import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
2309c6f1ddSLingrui98import xiangshan._
2409c6f1ddSLingrui98import xiangshan.cache._
251d8f4dcbSJayimport xiangshan.frontend.icache._
2673be64b3SJiawei Linimport xiangshan.cache.mmu.{TlbRequestIO, TlbPtwIO,TLB}
27cd365d4cSrvcoresjwimport xiangshan.backend.fu.{HasExceptionNO, PMP, PMPChecker, PFEvent}
2809c6f1ddSLingrui98
2909c6f1ddSLingrui98
3009c6f1ddSLingrui98class Frontend()(implicit p: Parameters) extends LazyModule with HasXSParameter{
3109c6f1ddSLingrui98
3209c6f1ddSLingrui98  val instrUncache  = LazyModule(new InstrUncache())
3309c6f1ddSLingrui98  val icache        = LazyModule(new ICache())
3409c6f1ddSLingrui98
3509c6f1ddSLingrui98  lazy val module = new FrontendImp(this)
3609c6f1ddSLingrui98}
3709c6f1ddSLingrui98
3809c6f1ddSLingrui98
3909c6f1ddSLingrui98class FrontendImp (outer: Frontend) extends LazyModuleImp(outer)
4009c6f1ddSLingrui98  with HasXSParameter
4109c6f1ddSLingrui98  with HasExceptionNO
4209c6f1ddSLingrui98{
4309c6f1ddSLingrui98  val io = IO(new Bundle() {
4409c6f1ddSLingrui98    val fencei = Input(Bool())
4509c6f1ddSLingrui98    val ptw = new TlbPtwIO(2)
4609c6f1ddSLingrui98    val backend = new FrontendToCtrlIO
4709c6f1ddSLingrui98    val sfence = Input(new SfenceBundle)
4809c6f1ddSLingrui98    val tlbCsr = Input(new TlbCsrBundle)
4909c6f1ddSLingrui98    val csrCtrl = Input(new CustomCSRCtrlIO)
50e19f7967SWilliam Wang    val csrUpdate = new DistributedCSRUpdateReq
5109c6f1ddSLingrui98    val error  = new L1CacheErrorInfo
5209c6f1ddSLingrui98    val frontendInfo = new Bundle {
5309c6f1ddSLingrui98      val ibufFull  = Output(Bool())
5409c6f1ddSLingrui98      val bpuInfo = new Bundle {
5509c6f1ddSLingrui98        val bpRight = Output(UInt(XLEN.W))
5609c6f1ddSLingrui98        val bpWrong = Output(UInt(XLEN.W))
5709c6f1ddSLingrui98      }
5809c6f1ddSLingrui98    }
5909c6f1ddSLingrui98  })
6009c6f1ddSLingrui98
6109c6f1ddSLingrui98  //decouped-frontend modules
621d8f4dcbSJay  val instrUncache = outer.instrUncache.module
631d8f4dcbSJay  val icache       = outer.icache.module
6409c6f1ddSLingrui98  val bpu     = Module(new Predictor)
6509c6f1ddSLingrui98  val ifu     = Module(new NewIFU)
6609c6f1ddSLingrui98  val ibuffer =  Module(new Ibuffer)
6709c6f1ddSLingrui98  val ftq = Module(new Ftq)
6809c6f1ddSLingrui98
69cd365d4cSrvcoresjw  //PFEvent
70cd365d4cSrvcoresjw  val pfevent = Module(new PFEvent)
7145f497a4Shappy-lx  val tlbCsr = RegNext(io.tlbCsr)
72cd365d4cSrvcoresjw  pfevent.io.distribute_csr := io.csrCtrl.distribute_csr
7372951335SLi Qianruo
7472951335SLi Qianruo  // trigger
7572951335SLi Qianruo  ifu.io.frontendTrigger := io.csrCtrl.frontend_trigger
7672951335SLi Qianruo  val triggerEn = io.csrCtrl.trigger_enable
7772951335SLi Qianruo  ifu.io.csrTriggerEnable := VecInit(triggerEn(0), triggerEn(1), triggerEn(6), triggerEn(8))
7872951335SLi Qianruo
79b6982e83SLemover  // pmp
80b6982e83SLemover  val pmp = Module(new PMP())
81b6982e83SLemover  val pmp_check = VecInit(Seq.fill(2)(Module(new PMPChecker(3, sameCycle = true)).io))
82b6982e83SLemover  pmp.io.distribute_csr := io.csrCtrl.distribute_csr
83b6982e83SLemover  for (i <- pmp_check.indices) {
84*98c71602SJiawei Lin    pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, icache.io.pmp(i).req)
851d8f4dcbSJay    icache.io.pmp(i).resp <> pmp_check(i).resp
86b6982e83SLemover  }
87b6982e83SLemover
8809c6f1ddSLingrui98  io.ptw <> TLB(
891d8f4dcbSJay    in = Seq(icache.io.itlb(0), icache.io.itlb(1)),
9009c6f1ddSLingrui98    sfence = io.sfence,
9145f497a4Shappy-lx    csr = tlbCsr,
9209c6f1ddSLingrui98    width = 2,
93a0301c0dSLemover    shouldBlock = true,
94a0301c0dSLemover    itlbParams
9509c6f1ddSLingrui98  )
9609c6f1ddSLingrui98
97efcb3cd3SJinYue  icache.io.fencei := RegNext(io.fencei)
98efcb3cd3SJinYue
9909c6f1ddSLingrui98  val needFlush = io.backend.toFtq.stage3Redirect.valid
10009c6f1ddSLingrui98
10109c6f1ddSLingrui98  //IFU-Ftq
10209c6f1ddSLingrui98  ifu.io.ftqInter.fromFtq <> ftq.io.toIfu
10309c6f1ddSLingrui98  ftq.io.fromIfu          <> ifu.io.ftqInter.toFtq
10409c6f1ddSLingrui98  bpu.io.ftq_to_bpu       <> ftq.io.toBpu
10509c6f1ddSLingrui98  ftq.io.fromBpu          <> bpu.io.bpu_to_ftq
10609c6f1ddSLingrui98  //IFU-ICache
10709c6f1ddSLingrui98  for(i <- 0 until 2){
1081d8f4dcbSJay    ifu.io.icacheInter(i).req       <>      icache.io.fetch(i).req
1091d8f4dcbSJay    icache.io.fetch(i).req <> ifu.io.icacheInter(i).req
1101d8f4dcbSJay    ifu.io.icacheInter(i).resp <> icache.io.fetch(i).resp
11109c6f1ddSLingrui98  }
1121d8f4dcbSJay  icache.io.stop := ifu.io.icacheStop
11309c6f1ddSLingrui98
1141d8f4dcbSJay  ifu.io.icachePerfInfo := icache.io.perfInfo
1151d8f4dcbSJay
1161d8f4dcbSJay  //icache.io.missQueue.flush := ifu.io.ftqInter.fromFtq.redirect.valid || (ifu.io.ftqInter.toFtq.pdWb.valid && ifu.io.ftqInter.toFtq.pdWb.bits.misOffset.valid)
11709c6f1ddSLingrui98
118e19f7967SWilliam Wang  icache.io.csr.distribute_csr <> io.csrCtrl.distribute_csr
119e19f7967SWilliam Wang  icache.io.csr.update <> io.csrUpdate
120e19f7967SWilliam Wang
12109c6f1ddSLingrui98  //IFU-Ibuffer
12209c6f1ddSLingrui98  ifu.io.toIbuffer    <> ibuffer.io.in
12309c6f1ddSLingrui98
12409c6f1ddSLingrui98  ftq.io.fromBackend <> io.backend.toFtq
12509c6f1ddSLingrui98  io.backend.fromFtq <> ftq.io.toBackend
12609c6f1ddSLingrui98  io.frontendInfo.bpuInfo <> ftq.io.bpuInfo
12709c6f1ddSLingrui98
128a37fbf10SJay  ifu.io.rob_commits <> io.backend.toFtq.rob_commits
129a37fbf10SJay
13009c6f1ddSLingrui98  ibuffer.io.flush := needFlush
13109c6f1ddSLingrui98  io.backend.cfVec <> ibuffer.io.out
13209c6f1ddSLingrui98
1330be662e4SJay  instrUncache.io.req   <> ifu.io.uncacheInter.toUncache
1340be662e4SJay  ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp
1351d8f4dcbSJay  instrUncache.io.flush := false.B//icache.io.missQueue.flush
13609c6f1ddSLingrui98  io.error <> DontCare
13709c6f1ddSLingrui98
13809c6f1ddSLingrui98  val frontendBubble = PopCount((0 until DecodeWidth).map(i => io.backend.cfVec(i).ready && !ibuffer.io.out(i).valid))
13909c6f1ddSLingrui98  XSPerfAccumulate("FrontendBubble", frontendBubble)
14009c6f1ddSLingrui98  io.frontendInfo.ibufFull := RegNext(ibuffer.io.full)
141cd365d4cSrvcoresjw
142cd365d4cSrvcoresjw  if(print_perfcounter){
143cd365d4cSrvcoresjw    val ifu_perf     = ifu.perfEvents.map(_._1).zip(ifu.perfinfo.perfEvents.perf_events)
144cd365d4cSrvcoresjw    val ibuffer_perf = ibuffer.perfEvents.map(_._1).zip(ibuffer.perfinfo.perfEvents.perf_events)
145cd365d4cSrvcoresjw    val icache_perf  = icache.perfEvents.map(_._1).zip(icache.perfinfo.perfEvents.perf_events)
146cd365d4cSrvcoresjw    val ftq_perf     = ftq.perfEvents.map(_._1).zip(ftq.perfinfo.perfEvents.perf_events)
147cd365d4cSrvcoresjw    val bpu_perf     = bpu.perfEvents.map(_._1).zip(bpu.perfinfo.perfEvents.perf_events)
148cd365d4cSrvcoresjw    val perfEvents = ifu_perf ++ ibuffer_perf ++ icache_perf ++ ftq_perf ++ bpu_perf
149cd365d4cSrvcoresjw
150cd365d4cSrvcoresjw    for (((perf_name,perf),i) <- perfEvents.zipWithIndex) {
151cd365d4cSrvcoresjw      println(s"frontend perf $i: $perf_name")
152cd365d4cSrvcoresjw    }
153cd365d4cSrvcoresjw  }
154cd365d4cSrvcoresjw
155cd365d4cSrvcoresjw  val hpmEvents = ifu.perfinfo.perfEvents.perf_events ++ ibuffer.perfinfo.perfEvents.perf_events ++
156cd365d4cSrvcoresjw                  icache.perfinfo.perfEvents.perf_events ++ ftq.perfinfo.perfEvents.perf_events ++
157cd365d4cSrvcoresjw                  bpu.perfinfo.perfEvents.perf_events
158cd365d4cSrvcoresjw  val perf_length = hpmEvents.length
159cd365d4cSrvcoresjw  val csrevents = pfevent.io.hpmevent.slice(0,8)
160cd365d4cSrvcoresjw  val perfinfo = IO(new Bundle(){
161cd365d4cSrvcoresjw    val perfEvents        = Output(new PerfEventsBundle(csrevents.length))
162cd365d4cSrvcoresjw  })
163cd365d4cSrvcoresjw  val hpm_frontend = Module(new HPerfmonitor(perf_length,csrevents.length))
164cd365d4cSrvcoresjw  hpm_frontend.io.hpm_event := csrevents
165cd365d4cSrvcoresjw  hpm_frontend.io.events_sets.perf_events := hpmEvents
166cd365d4cSrvcoresjw  perfinfo.perfEvents := RegNext(hpm_frontend.io.events_selected)
16709c6f1ddSLingrui98}
168