xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision ee175d780662d40afec7de01965b68feab42d0d8)
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
186ab6918fSYinan Xuimport chipsalliance.rocketchip.config.Parameters
1909c6f1ddSLingrui98import chisel3._
2009c6f1ddSLingrui98import chisel3.util._
2109c6f1ddSLingrui98import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
226ab6918fSYinan Xuimport utils._
2309c6f1ddSLingrui98import xiangshan._
24*ee175d78SJayimport xiangshan.backend.fu.{PFEvent, PMP, PMPChecker,PMPReqBundle}
25*ee175d78SJayimport xiangshan.cache.mmu._
261d8f4dcbSJayimport xiangshan.frontend.icache._
2709c6f1ddSLingrui98
2809c6f1ddSLingrui98
2909c6f1ddSLingrui98class Frontend()(implicit p: Parameters) extends LazyModule with HasXSParameter{
3009c6f1ddSLingrui98
3109c6f1ddSLingrui98  val instrUncache  = LazyModule(new InstrUncache())
3209c6f1ddSLingrui98  val icache        = LazyModule(new ICache())
3309c6f1ddSLingrui98
3409c6f1ddSLingrui98  lazy val module = new FrontendImp(this)
3509c6f1ddSLingrui98}
3609c6f1ddSLingrui98
3709c6f1ddSLingrui98
3809c6f1ddSLingrui98class FrontendImp (outer: Frontend) extends LazyModuleImp(outer)
3909c6f1ddSLingrui98  with HasXSParameter
401ca0e4f3SYinan Xu  with HasPerfEvents
4109c6f1ddSLingrui98{
4209c6f1ddSLingrui98  val io = IO(new Bundle() {
4309c6f1ddSLingrui98    val fencei = Input(Bool())
4409c6f1ddSLingrui98    val ptw = new TlbPtwIO(2)
4509c6f1ddSLingrui98    val backend = new FrontendToCtrlIO
4609c6f1ddSLingrui98    val sfence = Input(new SfenceBundle)
4709c6f1ddSLingrui98    val tlbCsr = Input(new TlbCsrBundle)
4809c6f1ddSLingrui98    val csrCtrl = Input(new CustomCSRCtrlIO)
49e19f7967SWilliam Wang    val csrUpdate = new DistributedCSRUpdateReq
5009c6f1ddSLingrui98    val error  = new L1CacheErrorInfo
5109c6f1ddSLingrui98    val frontendInfo = new Bundle {
5209c6f1ddSLingrui98      val ibufFull  = Output(Bool())
5309c6f1ddSLingrui98      val bpuInfo = new Bundle {
5409c6f1ddSLingrui98        val bpRight = Output(UInt(XLEN.W))
5509c6f1ddSLingrui98        val bpWrong = Output(UInt(XLEN.W))
5609c6f1ddSLingrui98      }
5709c6f1ddSLingrui98    }
5809c6f1ddSLingrui98  })
5909c6f1ddSLingrui98
6009c6f1ddSLingrui98  //decouped-frontend modules
611d8f4dcbSJay  val instrUncache = outer.instrUncache.module
621d8f4dcbSJay  val icache       = outer.icache.module
6309c6f1ddSLingrui98  val bpu     = Module(new Predictor)
6409c6f1ddSLingrui98  val ifu     = Module(new NewIFU)
6509c6f1ddSLingrui98  val ibuffer =  Module(new Ibuffer)
6609c6f1ddSLingrui98  val ftq = Module(new Ftq)
6709c6f1ddSLingrui98
686f688dacSYinan Xu  val tlbCsr = DelayN(io.tlbCsr, 2)
696f688dacSYinan Xu  val csrCtrl = DelayN(io.csrCtrl, 2)
7072951335SLi Qianruo
7172951335SLi Qianruo  // trigger
726f688dacSYinan Xu  ifu.io.frontendTrigger := csrCtrl.frontend_trigger
736f688dacSYinan Xu  val triggerEn = csrCtrl.trigger_enable
7472951335SLi Qianruo  ifu.io.csrTriggerEnable := VecInit(triggerEn(0), triggerEn(1), triggerEn(6), triggerEn(8))
7572951335SLi Qianruo
76b6982e83SLemover  // pmp
77b6982e83SLemover  val pmp = Module(new PMP())
78b6982e83SLemover  val pmp_check = VecInit(Seq.fill(2)(Module(new PMPChecker(3, sameCycle = true)).io))
796f688dacSYinan Xu  pmp.io.distribute_csr := csrCtrl.distribute_csr
80*ee175d78SJay  val pmp_req_vec     = Wire(Vec(2, Valid(new PMPReqBundle())))
81*ee175d78SJay  pmp_req_vec(0) <> icache.io.pmp(0).req
82*ee175d78SJay  pmp_req_vec(1).valid  :=  icache.io.pmp(1).req.valid || ifu.io.pmp.req.valid
83*ee175d78SJay  pmp_req_vec(1).bits   := Mux(ifu.io.pmp.req.valid, ifu.io.pmp.req.bits, icache.io.pmp(1).req.bits)
84*ee175d78SJay
85b6982e83SLemover  for (i <- pmp_check.indices) {
86*ee175d78SJay    pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, pmp_req_vec(i))
871d8f4dcbSJay    icache.io.pmp(i).resp <> pmp_check(i).resp
88b6982e83SLemover  }
89*ee175d78SJay  ifu.io.pmp.resp <> pmp_check(1).resp
90*ee175d78SJay  ifu.io.pmp.req.ready := false.B
91*ee175d78SJay
92*ee175d78SJay  val tlb_req_arb     = Module(new Arbiter(new TlbReq, 2))
93*ee175d78SJay  tlb_req_arb.io.in(0) <> ifu.io.iTLBInter.req
94*ee175d78SJay  tlb_req_arb.io.in(1) <> icache.io.itlb(1).req
95*ee175d78SJay
96*ee175d78SJay  val itlb_requestors = Wire(Vec(2, new BlockTlbRequestIO))
97*ee175d78SJay  itlb_requestors(0) <> icache.io.itlb(0)
98*ee175d78SJay  itlb_requestors(1).req <>  tlb_req_arb.io.out
99*ee175d78SJay  ifu.io.iTLBInter.resp  <> itlb_requestors(1).resp
100*ee175d78SJay  icache.io.itlb(1).resp <> itlb_requestors(1).resp
101b6982e83SLemover
10209c6f1ddSLingrui98  io.ptw <> TLB(
103*ee175d78SJay    //in = Seq(icache.io.itlb(0), icache.io.itlb(1)),
104*ee175d78SJay    in = Seq(itlb_requestors(0), itlb_requestors(1)),
10509c6f1ddSLingrui98    sfence = io.sfence,
10645f497a4Shappy-lx    csr = tlbCsr,
10709c6f1ddSLingrui98    width = 2,
108a0301c0dSLemover    shouldBlock = true,
109a0301c0dSLemover    itlbParams
11009c6f1ddSLingrui98  )
11109c6f1ddSLingrui98
1127052722fSJay  icache.io.prefetch <> ftq.io.toPrefetch
113efcb3cd3SJinYue
114df5b4b8eSYinan Xu  val needFlush = RegNext(io.backend.toFtq.redirect.valid)
11509c6f1ddSLingrui98
11609c6f1ddSLingrui98  //IFU-Ftq
11709c6f1ddSLingrui98  ifu.io.ftqInter.fromFtq <> ftq.io.toIfu
11809c6f1ddSLingrui98  ftq.io.fromIfu          <> ifu.io.ftqInter.toFtq
11909c6f1ddSLingrui98  bpu.io.ftq_to_bpu       <> ftq.io.toBpu
12009c6f1ddSLingrui98  ftq.io.fromBpu          <> bpu.io.bpu_to_ftq
12109c6f1ddSLingrui98  //IFU-ICache
12209c6f1ddSLingrui98  for(i <- 0 until 2){
1231d8f4dcbSJay    ifu.io.icacheInter(i).req       <>      icache.io.fetch(i).req
1241d8f4dcbSJay    icache.io.fetch(i).req <> ifu.io.icacheInter(i).req
1251d8f4dcbSJay    ifu.io.icacheInter(i).resp <> icache.io.fetch(i).resp
12609c6f1ddSLingrui98  }
1271d8f4dcbSJay  icache.io.stop := ifu.io.icacheStop
12809c6f1ddSLingrui98
1291d8f4dcbSJay  ifu.io.icachePerfInfo := icache.io.perfInfo
1301d8f4dcbSJay
1316f688dacSYinan Xu  icache.io.csr.distribute_csr <> csrCtrl.distribute_csr
13270899835SWilliam Wang  io.csrUpdate := RegNext(icache.io.csr.update)
133e19f7967SWilliam Wang
13409c6f1ddSLingrui98  //IFU-Ibuffer
13509c6f1ddSLingrui98  ifu.io.toIbuffer    <> ibuffer.io.in
13609c6f1ddSLingrui98
13709c6f1ddSLingrui98  ftq.io.fromBackend <> io.backend.toFtq
13809c6f1ddSLingrui98  io.backend.fromFtq <> ftq.io.toBackend
13909c6f1ddSLingrui98  io.frontendInfo.bpuInfo <> ftq.io.bpuInfo
14009c6f1ddSLingrui98
141a37fbf10SJay  ifu.io.rob_commits <> io.backend.toFtq.rob_commits
142a37fbf10SJay
14309c6f1ddSLingrui98  ibuffer.io.flush := needFlush
14409c6f1ddSLingrui98  io.backend.cfVec <> ibuffer.io.out
14509c6f1ddSLingrui98
1460be662e4SJay  instrUncache.io.req   <> ifu.io.uncacheInter.toUncache
1470be662e4SJay  ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp
14858dbdfc2SJay  instrUncache.io.flush := false.B
14958dbdfc2SJay  io.error <> RegNext(RegNext(icache.io.error))
15009c6f1ddSLingrui98
15109c6f1ddSLingrui98  val frontendBubble = PopCount((0 until DecodeWidth).map(i => io.backend.cfVec(i).ready && !ibuffer.io.out(i).valid))
15209c6f1ddSLingrui98  XSPerfAccumulate("FrontendBubble", frontendBubble)
15309c6f1ddSLingrui98  io.frontendInfo.ibufFull := RegNext(ibuffer.io.full)
154cd365d4cSrvcoresjw
1551ca0e4f3SYinan Xu  // PFEvent
1561ca0e4f3SYinan Xu  val pfevent = Module(new PFEvent)
1571ca0e4f3SYinan Xu  pfevent.io.distribute_csr := io.csrCtrl.distribute_csr
1581ca0e4f3SYinan Xu  val csrevents = pfevent.io.hpmevent.take(8)
159cd365d4cSrvcoresjw
1601ca0e4f3SYinan Xu  val allPerfEvents = Seq(ifu, ibuffer, icache, ftq, bpu).flatMap(_.getPerf)
1611ca0e4f3SYinan Xu  override val perfEvents = HPerfMonitor(csrevents, allPerfEvents).getPerfEvents
1621ca0e4f3SYinan Xu  generatePerfEvent()
16309c6f1ddSLingrui98}
164