xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision d2b20d1a96e238e36a849bd253f65ec7b6a5db38)
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._
233c02ee8fSwakafaimport 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())
4734f9624dSguohongyu    val ptw = new VectorTlbPtwIO(coreParams.itlbPortNum)
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)
72*d2b20d1aSTang Haojin  val FlushControlRedirect = RegNext(io.backend.toFtq.redirect.bits.debugIsCtrl)
73*d2b20d1aSTang Haojin  val FlushMemVioRedirect = RegNext(io.backend.toFtq.redirect.bits.debugIsMemVio)
74*d2b20d1aSTang Haojin  val FlushControlBTBMiss = Wire(Bool())
75*d2b20d1aSTang Haojin  val FlushTAGEMiss = Wire(Bool())
76*d2b20d1aSTang Haojin  val FlushSCMiss = Wire(Bool())
77*d2b20d1aSTang Haojin  val FlushITTAGEMiss = Wire(Bool())
78*d2b20d1aSTang Haojin  val FlushRASMiss = Wire(Bool())
79f1fe8698SLemover
806f688dacSYinan Xu  val tlbCsr = DelayN(io.tlbCsr, 2)
816f688dacSYinan Xu  val csrCtrl = DelayN(io.csrCtrl, 2)
82fa9f9690SLemover  val sfence = RegNext(RegNext(io.sfence))
8372951335SLi Qianruo
8472951335SLi Qianruo  // trigger
856f688dacSYinan Xu  ifu.io.frontendTrigger := csrCtrl.frontend_trigger
866f688dacSYinan Xu  val triggerEn = csrCtrl.trigger_enable
8772951335SLi Qianruo  ifu.io.csrTriggerEnable := VecInit(triggerEn(0), triggerEn(1), triggerEn(6), triggerEn(8))
8872951335SLi Qianruo
896ee06c7aSSteve Gou  // bpu ctrl
906ee06c7aSSteve Gou  bpu.io.ctrl := csrCtrl.bp_ctrl
91c4b44470SGuokai Chen  bpu.io.reset_vector := io.reset_vector
926ee06c7aSSteve Gou
93b6982e83SLemover// pmp
940c26d810Sguohongyu  val prefetchPipeNum = ICacheParameters().prefetchPipeNum
95b6982e83SLemover  val pmp = Module(new PMP())
9634f9624dSguohongyu  val pmp_check = VecInit(Seq.fill(coreParams.ipmpPortNum)(Module(new PMPChecker(3, sameCycle = true)).io))
976f688dacSYinan Xu  pmp.io.distribute_csr := csrCtrl.distribute_csr
9834f9624dSguohongyu  val pmp_req_vec     = Wire(Vec(coreParams.ipmpPortNum, Valid(new PMPReqBundle())))
990c26d810Sguohongyu  (0 until 2 + prefetchPipeNum).foreach(i => pmp_req_vec(i) <> icache.io.pmp(i).req)
1000c26d810Sguohongyu  pmp_req_vec.last <> ifu.io.pmp.req
101ee175d78SJay
102b6982e83SLemover  for (i <- pmp_check.indices) {
103ee175d78SJay    pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, pmp_req_vec(i))
104b6982e83SLemover  }
1050c26d810Sguohongyu  (0 until 2 + prefetchPipeNum).foreach(i => icache.io.pmp(i).resp <> pmp_check(i).resp)
1060c26d810Sguohongyu  ifu.io.pmp.resp <> pmp_check.last.resp
107ee175d78SJay
10834f9624dSguohongyu  val itlb = Module(new TLB(coreParams.itlbPortNum, nRespDups = 1,
1090c26d810Sguohongyu    Seq(true, true) ++ Seq.fill(prefetchPipeNum)(false) ++ Seq(true), itlbParams))
1100c26d810Sguohongyu  itlb.io.requestor.take(2 + prefetchPipeNum) zip icache.io.itlb foreach {case (a,b) => a <> b}
1110c26d810Sguohongyu  itlb.io.requestor.last <> ifu.io.iTLBInter // mmio may need re-tlb, blocked
112f1fe8698SLemover  itlb.io.base_connect(io.sfence, tlbCsr)
113f1fe8698SLemover  io.ptw.connect(itlb.io.ptw)
114f1fe8698SLemover  itlb.io.ptw_replenish <> DontCare
115f1fe8698SLemover  itlb.io.flushPipe.map(_ := needFlush)
11609c6f1ddSLingrui98
1177052722fSJay  icache.io.prefetch <> ftq.io.toPrefetch
118efcb3cd3SJinYue
11909c6f1ddSLingrui98
12009c6f1ddSLingrui98  //IFU-Ftq
12109c6f1ddSLingrui98  ifu.io.ftqInter.fromFtq <> ftq.io.toIfu
122c5c5edaeSJenius  ftq.io.toIfu.req.ready :=  ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
123c5c5edaeSJenius
12409c6f1ddSLingrui98  ftq.io.fromIfu          <> ifu.io.ftqInter.toFtq
12509c6f1ddSLingrui98  bpu.io.ftq_to_bpu       <> ftq.io.toBpu
12609c6f1ddSLingrui98  ftq.io.fromBpu          <> bpu.io.bpu_to_ftq
1271d1e6d4dSJenius
1281d1e6d4dSJenius  ftq.io.mmioCommitRead   <> ifu.io.mmioCommitRead
12909c6f1ddSLingrui98  //IFU-ICache
130c5c5edaeSJenius
131c5c5edaeSJenius  icache.io.fetch.req <> ftq.io.toICache.req
132c5c5edaeSJenius  ftq.io.toICache.req.ready :=  ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
133c5c5edaeSJenius
134c5c5edaeSJenius  ifu.io.icacheInter.resp <>    icache.io.fetch.resp
13550780602SJenius  ifu.io.icacheInter.icacheReady :=  icache.io.toIFU
136*d2b20d1aSTang Haojin  ifu.io.icacheInter.topdownIcacheMiss := icache.io.fetch.topdownIcacheMiss
137*d2b20d1aSTang Haojin  ifu.io.icacheInter.topdownItlbMiss := icache.io.fetch.topdownItlbMiss
1381d8f4dcbSJay  icache.io.stop := ifu.io.icacheStop
13909c6f1ddSLingrui98
1401d8f4dcbSJay  ifu.io.icachePerfInfo := icache.io.perfInfo
1411d8f4dcbSJay
1426f688dacSYinan Xu  icache.io.csr.distribute_csr <> csrCtrl.distribute_csr
14370899835SWilliam Wang  io.csrUpdate := RegNext(icache.io.csr.update)
144e19f7967SWilliam Wang
145ecccf78fSJay  icache.io.csr_pf_enable     := RegNext(csrCtrl.l1I_pf_enable)
146ecccf78fSJay  icache.io.csr_parity_enable := RegNext(csrCtrl.icache_parity_enable)
147ecccf78fSJay
1482a6078bfSguohongyu  icache.io.fencei := io.fencei
1492a6078bfSguohongyu
15009c6f1ddSLingrui98  //IFU-Ibuffer
15109c6f1ddSLingrui98  ifu.io.toIbuffer    <> ibuffer.io.in
15209c6f1ddSLingrui98
15309c6f1ddSLingrui98  ftq.io.fromBackend <> io.backend.toFtq
15409c6f1ddSLingrui98  io.backend.fromFtq <> ftq.io.toBackend
15509c6f1ddSLingrui98  io.frontendInfo.bpuInfo <> ftq.io.bpuInfo
15609c6f1ddSLingrui98
157a37fbf10SJay  ifu.io.rob_commits <> io.backend.toFtq.rob_commits
158a37fbf10SJay
15909c6f1ddSLingrui98  ibuffer.io.flush := needFlush
160*d2b20d1aSTang Haojin  ibuffer.io.ControlRedirect := FlushControlRedirect
161*d2b20d1aSTang Haojin  ibuffer.io.MemVioRedirect := FlushMemVioRedirect
162*d2b20d1aSTang Haojin  ibuffer.io.ControlBTBMissBubble := FlushControlBTBMiss
163*d2b20d1aSTang Haojin  ibuffer.io.TAGEMissBubble := FlushTAGEMiss
164*d2b20d1aSTang Haojin  ibuffer.io.SCMissBubble := FlushSCMiss
165*d2b20d1aSTang Haojin  ibuffer.io.ITTAGEMissBubble := FlushITTAGEMiss
166*d2b20d1aSTang Haojin  ibuffer.io.RASMissBubble := FlushRASMiss
167*d2b20d1aSTang Haojin
168*d2b20d1aSTang Haojin  FlushControlBTBMiss := ftq.io.ControlBTBMissBubble
169*d2b20d1aSTang Haojin  FlushTAGEMiss := ftq.io.TAGEMissBubble
170*d2b20d1aSTang Haojin  FlushSCMiss := ftq.io.SCMissBubble
171*d2b20d1aSTang Haojin  FlushITTAGEMiss := ftq.io.ITTAGEMissBubble
172*d2b20d1aSTang Haojin  FlushRASMiss := ftq.io.RASMissBubble
173*d2b20d1aSTang Haojin
17409c6f1ddSLingrui98  io.backend.cfVec <> ibuffer.io.out
175*d2b20d1aSTang Haojin  io.backend.stallReason <> ibuffer.io.stallReason
176*d2b20d1aSTang Haojin  dontTouch(io.backend.stallReason)
17709c6f1ddSLingrui98
1780be662e4SJay  instrUncache.io.req   <> ifu.io.uncacheInter.toUncache
1790be662e4SJay  ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp
18058dbdfc2SJay  instrUncache.io.flush := false.B
18158dbdfc2SJay  io.error <> RegNext(RegNext(icache.io.error))
18209c6f1ddSLingrui98
18341cb8b61SJenius  icache.io.hartId := io.hartId
18441cb8b61SJenius
18509c6f1ddSLingrui98  val frontendBubble = PopCount((0 until DecodeWidth).map(i => io.backend.cfVec(i).ready && !ibuffer.io.out(i).valid))
18609c6f1ddSLingrui98  XSPerfAccumulate("FrontendBubble", frontendBubble)
18709c6f1ddSLingrui98  io.frontendInfo.ibufFull := RegNext(ibuffer.io.full)
188cd365d4cSrvcoresjw
1891ca0e4f3SYinan Xu  // PFEvent
1901ca0e4f3SYinan Xu  val pfevent = Module(new PFEvent)
1911ca0e4f3SYinan Xu  pfevent.io.distribute_csr := io.csrCtrl.distribute_csr
1921ca0e4f3SYinan Xu  val csrevents = pfevent.io.hpmevent.take(8)
193cd365d4cSrvcoresjw
1941ca0e4f3SYinan Xu  val allPerfEvents = Seq(ifu, ibuffer, icache, ftq, bpu).flatMap(_.getPerf)
1951ca0e4f3SYinan Xu  override val perfEvents = HPerfMonitor(csrevents, allPerfEvents).getPerfEvents
1961ca0e4f3SYinan Xu  generatePerfEvent()
19709c6f1ddSLingrui98}
198