xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision 41cb8b61fb6d39fda091a592d80ae7e08dcced57)
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._
24ee175d78SJayimport xiangshan.backend.fu.{PFEvent, PMP, PMPChecker,PMPReqBundle}
25ee175d78SJayimport 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() {
43*41cb8b61SJenius    val hartId = Input(UInt(8.W))
44c4b44470SGuokai Chen    val reset_vector = Input(UInt(PAddrBits.W))
4509c6f1ddSLingrui98    val fencei = Input(Bool())
4691df15e5SJay    val ptw = new TlbPtwIO(6)
4709c6f1ddSLingrui98    val backend = new FrontendToCtrlIO
4809c6f1ddSLingrui98    val sfence = Input(new SfenceBundle)
4909c6f1ddSLingrui98    val tlbCsr = Input(new TlbCsrBundle)
5009c6f1ddSLingrui98    val csrCtrl = Input(new CustomCSRCtrlIO)
51e19f7967SWilliam Wang    val csrUpdate = new DistributedCSRUpdateReq
5209c6f1ddSLingrui98    val error  = new L1CacheErrorInfo
5309c6f1ddSLingrui98    val frontendInfo = new Bundle {
5409c6f1ddSLingrui98      val ibufFull  = Output(Bool())
5509c6f1ddSLingrui98      val bpuInfo = new Bundle {
5609c6f1ddSLingrui98        val bpRight = Output(UInt(XLEN.W))
5709c6f1ddSLingrui98        val bpWrong = Output(UInt(XLEN.W))
5809c6f1ddSLingrui98      }
5909c6f1ddSLingrui98    }
6009c6f1ddSLingrui98  })
6109c6f1ddSLingrui98
6209c6f1ddSLingrui98  //decouped-frontend modules
631d8f4dcbSJay  val instrUncache = outer.instrUncache.module
641d8f4dcbSJay  val icache       = outer.icache.module
6509c6f1ddSLingrui98  val bpu     = Module(new Predictor)
6609c6f1ddSLingrui98  val ifu     = Module(new NewIFU)
6709c6f1ddSLingrui98  val ibuffer =  Module(new Ibuffer)
6809c6f1ddSLingrui98  val ftq = Module(new Ftq)
6909c6f1ddSLingrui98
706f688dacSYinan Xu  val tlbCsr = DelayN(io.tlbCsr, 2)
716f688dacSYinan Xu  val csrCtrl = DelayN(io.csrCtrl, 2)
7272951335SLi Qianruo
7372951335SLi Qianruo  // trigger
746f688dacSYinan Xu  ifu.io.frontendTrigger := csrCtrl.frontend_trigger
756f688dacSYinan Xu  val triggerEn = csrCtrl.trigger_enable
7672951335SLi Qianruo  ifu.io.csrTriggerEnable := VecInit(triggerEn(0), triggerEn(1), triggerEn(6), triggerEn(8))
7772951335SLi Qianruo
786ee06c7aSSteve Gou  // bpu ctrl
796ee06c7aSSteve Gou  bpu.io.ctrl := csrCtrl.bp_ctrl
80c4b44470SGuokai Chen  bpu.io.reset_vector := io.reset_vector
816ee06c7aSSteve Gou
82b6982e83SLemover// pmp
83b6982e83SLemover  val pmp = Module(new PMP())
8461e1db30SJay  val pmp_check = VecInit(Seq.fill(4)(Module(new PMPChecker(3, sameCycle = true)).io))
856f688dacSYinan Xu  pmp.io.distribute_csr := csrCtrl.distribute_csr
8661e1db30SJay  val pmp_req_vec     = Wire(Vec(4, Valid(new PMPReqBundle())))
87ee175d78SJay  pmp_req_vec(0) <> icache.io.pmp(0).req
8861e1db30SJay  pmp_req_vec(1) <> icache.io.pmp(1).req
8961e1db30SJay  pmp_req_vec(2) <> icache.io.pmp(2).req
9061e1db30SJay  pmp_req_vec(3) <> ifu.io.pmp.req
91ee175d78SJay
92b6982e83SLemover  for (i <- pmp_check.indices) {
93ee175d78SJay    pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, pmp_req_vec(i))
94b6982e83SLemover  }
9561e1db30SJay  icache.io.pmp(0).resp <> pmp_check(0).resp
9661e1db30SJay  icache.io.pmp(1).resp <> pmp_check(1).resp
9761e1db30SJay  icache.io.pmp(2).resp <> pmp_check(2).resp
9861e1db30SJay  ifu.io.pmp.resp <> pmp_check(3).resp
99ee175d78SJay
10091df15e5SJay  // val tlb_req_arb     = Module(new Arbiter(new TlbReq, 2))
10191df15e5SJay  // tlb_req_arb.io.in(0) <> ifu.io.iTLBInter.req
10291df15e5SJay  // tlb_req_arb.io.in(1) <> icache.io.itlb(1).req
103ee175d78SJay
10491df15e5SJay  val itlb_requestors = Wire(Vec(6, new BlockTlbRequestIO))
105ee175d78SJay  itlb_requestors(0) <> icache.io.itlb(0)
10691df15e5SJay  itlb_requestors(1) <> icache.io.itlb(1)
10791df15e5SJay  itlb_requestors(2) <> icache.io.itlb(2)
10891df15e5SJay  itlb_requestors(3) <> icache.io.itlb(3)
10991df15e5SJay  itlb_requestors(4) <> icache.io.itlb(4)
11091df15e5SJay  itlb_requestors(5) <> ifu.io.iTLBInter
11191df15e5SJay
11291df15e5SJay  // itlb_requestors(1).req <>  tlb_req_arb.io.out
11391df15e5SJay
11491df15e5SJay  // ifu.io.iTLBInter.resp  <> itlb_requestors(1).resp
11591df15e5SJay  // icache.io.itlb(1).resp <> itlb_requestors(1).resp
116b6982e83SLemover
11709c6f1ddSLingrui98  io.ptw <> TLB(
118ee175d78SJay    //in = Seq(icache.io.itlb(0), icache.io.itlb(1)),
11991df15e5SJay    in = Seq(itlb_requestors(0),itlb_requestors(1),itlb_requestors(2),itlb_requestors(3),itlb_requestors(4),itlb_requestors(5)),
12009c6f1ddSLingrui98    sfence = io.sfence,
12145f497a4Shappy-lx    csr = tlbCsr,
12291df15e5SJay    width = 6,
123a0301c0dSLemover    shouldBlock = true,
124a0301c0dSLemover    itlbParams
12509c6f1ddSLingrui98  )
12609c6f1ddSLingrui98
1277052722fSJay  icache.io.prefetch <> ftq.io.toPrefetch
128efcb3cd3SJinYue
129df5b4b8eSYinan Xu  val needFlush = RegNext(io.backend.toFtq.redirect.valid)
13009c6f1ddSLingrui98
13109c6f1ddSLingrui98  //IFU-Ftq
13209c6f1ddSLingrui98  ifu.io.ftqInter.fromFtq <> ftq.io.toIfu
13309c6f1ddSLingrui98  ftq.io.fromIfu          <> ifu.io.ftqInter.toFtq
13409c6f1ddSLingrui98  bpu.io.ftq_to_bpu       <> ftq.io.toBpu
13509c6f1ddSLingrui98  ftq.io.fromBpu          <> bpu.io.bpu_to_ftq
13609c6f1ddSLingrui98  //IFU-ICache
13709c6f1ddSLingrui98  for(i <- 0 until 2){
1381d8f4dcbSJay    ifu.io.icacheInter(i).req       <>      icache.io.fetch(i).req
1391d8f4dcbSJay    icache.io.fetch(i).req <> ifu.io.icacheInter(i).req
1401d8f4dcbSJay    ifu.io.icacheInter(i).resp <> icache.io.fetch(i).resp
14109c6f1ddSLingrui98  }
1421d8f4dcbSJay  icache.io.stop := ifu.io.icacheStop
14309c6f1ddSLingrui98
1441d8f4dcbSJay  ifu.io.icachePerfInfo := icache.io.perfInfo
1451d8f4dcbSJay
1466f688dacSYinan Xu  icache.io.csr.distribute_csr <> csrCtrl.distribute_csr
14770899835SWilliam Wang  io.csrUpdate := RegNext(icache.io.csr.update)
148e19f7967SWilliam Wang
149ecccf78fSJay  icache.io.csr_pf_enable     := RegNext(csrCtrl.l1I_pf_enable)
150ecccf78fSJay  icache.io.csr_parity_enable := RegNext(csrCtrl.icache_parity_enable)
151ecccf78fSJay
15209c6f1ddSLingrui98  //IFU-Ibuffer
15309c6f1ddSLingrui98  ifu.io.toIbuffer    <> ibuffer.io.in
15409c6f1ddSLingrui98
15509c6f1ddSLingrui98  ftq.io.fromBackend <> io.backend.toFtq
15609c6f1ddSLingrui98  io.backend.fromFtq <> ftq.io.toBackend
15709c6f1ddSLingrui98  io.frontendInfo.bpuInfo <> ftq.io.bpuInfo
15809c6f1ddSLingrui98
159a37fbf10SJay  ifu.io.rob_commits <> io.backend.toFtq.rob_commits
160a37fbf10SJay
16109c6f1ddSLingrui98  ibuffer.io.flush := needFlush
16209c6f1ddSLingrui98  io.backend.cfVec <> ibuffer.io.out
16309c6f1ddSLingrui98
1640be662e4SJay  instrUncache.io.req   <> ifu.io.uncacheInter.toUncache
1650be662e4SJay  ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp
16658dbdfc2SJay  instrUncache.io.flush := false.B
16758dbdfc2SJay  io.error <> RegNext(RegNext(icache.io.error))
16809c6f1ddSLingrui98
169*41cb8b61SJenius  icache.io.hartId := io.hartId
170*41cb8b61SJenius
17109c6f1ddSLingrui98  val frontendBubble = PopCount((0 until DecodeWidth).map(i => io.backend.cfVec(i).ready && !ibuffer.io.out(i).valid))
17209c6f1ddSLingrui98  XSPerfAccumulate("FrontendBubble", frontendBubble)
17309c6f1ddSLingrui98  io.frontendInfo.ibufFull := RegNext(ibuffer.io.full)
174cd365d4cSrvcoresjw
1751ca0e4f3SYinan Xu  // PFEvent
1761ca0e4f3SYinan Xu  val pfevent = Module(new PFEvent)
1771ca0e4f3SYinan Xu  pfevent.io.distribute_csr := io.csrCtrl.distribute_csr
1781ca0e4f3SYinan Xu  val csrevents = pfevent.io.hpmevent.take(8)
179cd365d4cSrvcoresjw
1801ca0e4f3SYinan Xu  val allPerfEvents = Seq(ifu, ibuffer, icache, ftq, bpu).flatMap(_.getPerf)
1811ca0e4f3SYinan Xu  override val perfEvents = HPerfMonitor(csrevents, allPerfEvents).getPerfEvents
1821ca0e4f3SYinan Xu  generatePerfEvent()
18309c6f1ddSLingrui98}
184