xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision 3c02ee8f82edea481fa8336c7f54ffc17fafba91)
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._
23*3c02ee8fSwakafaimport utility._
2409c6f1ddSLingrui98import xiangshan._
25ee175d78SJayimport xiangshan.backend.fu.{PFEvent, PMP, PMPChecker,PMPReqBundle}
26ee175d78SJayimport xiangshan.cache.mmu._
271d8f4dcbSJayimport xiangshan.frontend.icache._
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
411ca0e4f3SYinan Xu  with HasPerfEvents
4209c6f1ddSLingrui98{
4309c6f1ddSLingrui98  val io = IO(new Bundle() {
4441cb8b61SJenius    val hartId = Input(UInt(8.W))
45c4b44470SGuokai Chen    val reset_vector = Input(UInt(PAddrBits.W))
4609c6f1ddSLingrui98    val fencei = Input(Bool())
47f1fe8698SLemover    val ptw = new VectorTlbPtwIO(4)
4809c6f1ddSLingrui98    val backend = new FrontendToCtrlIO
4909c6f1ddSLingrui98    val sfence = Input(new SfenceBundle)
5009c6f1ddSLingrui98    val tlbCsr = Input(new TlbCsrBundle)
5109c6f1ddSLingrui98    val csrCtrl = Input(new CustomCSRCtrlIO)
52e19f7967SWilliam Wang    val csrUpdate = new DistributedCSRUpdateReq
5309c6f1ddSLingrui98    val error  = new L1CacheErrorInfo
5409c6f1ddSLingrui98    val frontendInfo = new Bundle {
5509c6f1ddSLingrui98      val ibufFull  = Output(Bool())
5609c6f1ddSLingrui98      val bpuInfo = new Bundle {
5709c6f1ddSLingrui98        val bpRight = Output(UInt(XLEN.W))
5809c6f1ddSLingrui98        val bpWrong = Output(UInt(XLEN.W))
5909c6f1ddSLingrui98      }
6009c6f1ddSLingrui98    }
6109c6f1ddSLingrui98  })
6209c6f1ddSLingrui98
6309c6f1ddSLingrui98  //decouped-frontend modules
641d8f4dcbSJay  val instrUncache = outer.instrUncache.module
651d8f4dcbSJay  val icache       = outer.icache.module
6609c6f1ddSLingrui98  val bpu     = Module(new Predictor)
6709c6f1ddSLingrui98  val ifu     = Module(new NewIFU)
6809c6f1ddSLingrui98  val ibuffer =  Module(new Ibuffer)
6909c6f1ddSLingrui98  val ftq = Module(new Ftq)
7009c6f1ddSLingrui98
71f1fe8698SLemover  val needFlush = RegNext(io.backend.toFtq.redirect.valid)
72f1fe8698SLemover
736f688dacSYinan Xu  val tlbCsr = DelayN(io.tlbCsr, 2)
746f688dacSYinan Xu  val csrCtrl = DelayN(io.csrCtrl, 2)
75fa9f9690SLemover  val sfence = RegNext(RegNext(io.sfence))
7672951335SLi Qianruo
7772951335SLi Qianruo  // trigger
786f688dacSYinan Xu  ifu.io.frontendTrigger := csrCtrl.frontend_trigger
796f688dacSYinan Xu  val triggerEn = csrCtrl.trigger_enable
8072951335SLi Qianruo  ifu.io.csrTriggerEnable := VecInit(triggerEn(0), triggerEn(1), triggerEn(6), triggerEn(8))
8172951335SLi Qianruo
826ee06c7aSSteve Gou  // bpu ctrl
836ee06c7aSSteve Gou  bpu.io.ctrl := csrCtrl.bp_ctrl
84c4b44470SGuokai Chen  bpu.io.reset_vector := io.reset_vector
856ee06c7aSSteve Gou
86b6982e83SLemover// pmp
87b6982e83SLemover  val pmp = Module(new PMP())
8861e1db30SJay  val pmp_check = VecInit(Seq.fill(4)(Module(new PMPChecker(3, sameCycle = true)).io))
896f688dacSYinan Xu  pmp.io.distribute_csr := csrCtrl.distribute_csr
9061e1db30SJay  val pmp_req_vec     = Wire(Vec(4, Valid(new PMPReqBundle())))
91ee175d78SJay  pmp_req_vec(0) <> icache.io.pmp(0).req
9261e1db30SJay  pmp_req_vec(1) <> icache.io.pmp(1).req
9361e1db30SJay  pmp_req_vec(2) <> icache.io.pmp(2).req
9461e1db30SJay  pmp_req_vec(3) <> ifu.io.pmp.req
95ee175d78SJay
96b6982e83SLemover  for (i <- pmp_check.indices) {
97ee175d78SJay    pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, pmp_req_vec(i))
98b6982e83SLemover  }
9961e1db30SJay  icache.io.pmp(0).resp <> pmp_check(0).resp
10061e1db30SJay  icache.io.pmp(1).resp <> pmp_check(1).resp
10161e1db30SJay  icache.io.pmp(2).resp <> pmp_check(2).resp
10261e1db30SJay  ifu.io.pmp.resp <> pmp_check(3).resp
103ee175d78SJay
10403efd994Shappy-lx  val itlb = Module(new TLB(4, nRespDups = 1, Seq(true, true, false, true), itlbParams))
105f1fe8698SLemover  itlb.io.requestor.take(3) zip icache.io.itlb foreach {case (a,b) => a <> b}
106f1fe8698SLemover  itlb.io.requestor(3) <> ifu.io.iTLBInter // mmio may need re-tlb, blocked
107f1fe8698SLemover  itlb.io.base_connect(io.sfence, tlbCsr)
108f1fe8698SLemover  io.ptw.connect(itlb.io.ptw)
109f1fe8698SLemover  itlb.io.ptw_replenish <> DontCare
110f1fe8698SLemover  itlb.io.flushPipe.map(_ := needFlush)
11109c6f1ddSLingrui98
1127052722fSJay  icache.io.prefetch <> ftq.io.toPrefetch
113efcb3cd3SJinYue
11409c6f1ddSLingrui98
11509c6f1ddSLingrui98  //IFU-Ftq
11609c6f1ddSLingrui98  ifu.io.ftqInter.fromFtq <> ftq.io.toIfu
117c5c5edaeSJenius  ftq.io.toIfu.req.ready :=  ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
118c5c5edaeSJenius
11909c6f1ddSLingrui98  ftq.io.fromIfu          <> ifu.io.ftqInter.toFtq
12009c6f1ddSLingrui98  bpu.io.ftq_to_bpu       <> ftq.io.toBpu
12109c6f1ddSLingrui98  ftq.io.fromBpu          <> bpu.io.bpu_to_ftq
1221d1e6d4dSJenius
1231d1e6d4dSJenius  ftq.io.mmioCommitRead   <> ifu.io.mmioCommitRead
12409c6f1ddSLingrui98  //IFU-ICache
125c5c5edaeSJenius
126c5c5edaeSJenius  icache.io.fetch.req <> ftq.io.toICache.req
127c5c5edaeSJenius  ftq.io.toICache.req.ready :=  ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
128c5c5edaeSJenius
129c5c5edaeSJenius  ifu.io.icacheInter.resp <>    icache.io.fetch.resp
13050780602SJenius  ifu.io.icacheInter.icacheReady :=  icache.io.toIFU
1311d8f4dcbSJay  icache.io.stop := ifu.io.icacheStop
13209c6f1ddSLingrui98
1331d8f4dcbSJay  ifu.io.icachePerfInfo := icache.io.perfInfo
1341d8f4dcbSJay
1356f688dacSYinan Xu  icache.io.csr.distribute_csr <> csrCtrl.distribute_csr
13670899835SWilliam Wang  io.csrUpdate := RegNext(icache.io.csr.update)
137e19f7967SWilliam Wang
138ecccf78fSJay  icache.io.csr_pf_enable     := RegNext(csrCtrl.l1I_pf_enable)
139ecccf78fSJay  icache.io.csr_parity_enable := RegNext(csrCtrl.icache_parity_enable)
140ecccf78fSJay
14109c6f1ddSLingrui98  //IFU-Ibuffer
14209c6f1ddSLingrui98  ifu.io.toIbuffer    <> ibuffer.io.in
14309c6f1ddSLingrui98
14409c6f1ddSLingrui98  ftq.io.fromBackend <> io.backend.toFtq
14509c6f1ddSLingrui98  io.backend.fromFtq <> ftq.io.toBackend
14609c6f1ddSLingrui98  io.frontendInfo.bpuInfo <> ftq.io.bpuInfo
14709c6f1ddSLingrui98
148a37fbf10SJay  ifu.io.rob_commits <> io.backend.toFtq.rob_commits
149a37fbf10SJay
15009c6f1ddSLingrui98  ibuffer.io.flush := needFlush
15109c6f1ddSLingrui98  io.backend.cfVec <> ibuffer.io.out
15209c6f1ddSLingrui98
1530be662e4SJay  instrUncache.io.req   <> ifu.io.uncacheInter.toUncache
1540be662e4SJay  ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp
15558dbdfc2SJay  instrUncache.io.flush := false.B
15658dbdfc2SJay  io.error <> RegNext(RegNext(icache.io.error))
15709c6f1ddSLingrui98
15841cb8b61SJenius  icache.io.hartId := io.hartId
15941cb8b61SJenius
16009c6f1ddSLingrui98  val frontendBubble = PopCount((0 until DecodeWidth).map(i => io.backend.cfVec(i).ready && !ibuffer.io.out(i).valid))
16109c6f1ddSLingrui98  XSPerfAccumulate("FrontendBubble", frontendBubble)
16209c6f1ddSLingrui98  io.frontendInfo.ibufFull := RegNext(ibuffer.io.full)
163cd365d4cSrvcoresjw
1641ca0e4f3SYinan Xu  // PFEvent
1651ca0e4f3SYinan Xu  val pfevent = Module(new PFEvent)
1661ca0e4f3SYinan Xu  pfevent.io.distribute_csr := io.csrCtrl.distribute_csr
1671ca0e4f3SYinan Xu  val csrevents = pfevent.io.hpmevent.take(8)
168cd365d4cSrvcoresjw
1691ca0e4f3SYinan Xu  val allPerfEvents = Seq(ifu, ibuffer, icache, ftq, bpu).flatMap(_.getPerf)
1701ca0e4f3SYinan Xu  override val perfEvents = HPerfMonitor(csrevents, allPerfEvents).getPerfEvents
1711ca0e4f3SYinan Xu  generatePerfEvent()
17209c6f1ddSLingrui98}
173