xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision a0301c0d86a76a8bbed79fab2db5e6571a62b88a)
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._
2509c6f1ddSLingrui98import xiangshan.cache.prefetch.L1plusPrefetcher
2609c6f1ddSLingrui98import xiangshan.cache.mmu.{TlbRequestIO, TlbPtwIO,TLB}
2709c6f1ddSLingrui98import xiangshan.backend.fu.HasExceptionNO
2809c6f1ddSLingrui98import system.L1CacheErrorInfo
2909c6f1ddSLingrui98
3009c6f1ddSLingrui98
3109c6f1ddSLingrui98class Frontend()(implicit p: Parameters) extends LazyModule with HasXSParameter{
3209c6f1ddSLingrui98
3309c6f1ddSLingrui98  val instrUncache  = LazyModule(new InstrUncache())
3409c6f1ddSLingrui98  val icache        = LazyModule(new ICache())
3509c6f1ddSLingrui98
3609c6f1ddSLingrui98  lazy val module = new FrontendImp(this)
3709c6f1ddSLingrui98}
3809c6f1ddSLingrui98
3909c6f1ddSLingrui98
4009c6f1ddSLingrui98class FrontendImp (outer: Frontend) extends LazyModuleImp(outer)
4109c6f1ddSLingrui98  with HasL1plusCacheParameters
4209c6f1ddSLingrui98  with HasXSParameter
4309c6f1ddSLingrui98  with HasExceptionNO
4409c6f1ddSLingrui98{
4509c6f1ddSLingrui98  val io = IO(new Bundle() {
4609c6f1ddSLingrui98    val fencei = Input(Bool())
4709c6f1ddSLingrui98    val ptw = new TlbPtwIO(2)
4809c6f1ddSLingrui98    val backend = new FrontendToCtrlIO
4909c6f1ddSLingrui98    val sfence = Input(new SfenceBundle)
5009c6f1ddSLingrui98    val tlbCsr = Input(new TlbCsrBundle)
5109c6f1ddSLingrui98    val csrCtrl = Input(new CustomCSRCtrlIO)
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
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  //icache
6809c6f1ddSLingrui98
6909c6f1ddSLingrui98  io.ptw <> TLB(
7009c6f1ddSLingrui98    in = Seq(ifu.io.iTLBInter(0), ifu.io.iTLBInter(1)),
7109c6f1ddSLingrui98    sfence = io.sfence,
7209c6f1ddSLingrui98    csr = io.tlbCsr,
7309c6f1ddSLingrui98    width = 2,
74*a0301c0dSLemover    shouldBlock = true,
75*a0301c0dSLemover    itlbParams
7609c6f1ddSLingrui98  )
7709c6f1ddSLingrui98  //TODO: modules need to be removed
7809c6f1ddSLingrui98  val instrUncache = outer.instrUncache.module
7909c6f1ddSLingrui98  val icache       = outer.icache.module
8009c6f1ddSLingrui98
8109c6f1ddSLingrui98  val needFlush = io.backend.toFtq.stage3Redirect.valid
8209c6f1ddSLingrui98
8309c6f1ddSLingrui98  //IFU-Ftq
8409c6f1ddSLingrui98  ifu.io.ftqInter.fromFtq <> ftq.io.toIfu
8509c6f1ddSLingrui98  ftq.io.fromIfu          <> ifu.io.ftqInter.toFtq
8609c6f1ddSLingrui98  bpu.io.ftq_to_bpu       <> ftq.io.toBpu
8709c6f1ddSLingrui98  ftq.io.fromBpu          <> bpu.io.bpu_to_ftq
8809c6f1ddSLingrui98  //IFU-ICache
8909c6f1ddSLingrui98  ifu.io.icacheInter.toIMeta    <>      icache.io.metaRead.req
9009c6f1ddSLingrui98  ifu.io.icacheInter.fromIMeta  <>      icache.io.metaRead.resp
9109c6f1ddSLingrui98  ifu.io.icacheInter.toIData    <>      icache.io.dataRead.req
9209c6f1ddSLingrui98  ifu.io.icacheInter.fromIData  <>      icache.io.dataRead.resp
9309c6f1ddSLingrui98
9409c6f1ddSLingrui98  for(i <- 0 until 2){
9509c6f1ddSLingrui98    ifu.io.icacheInter.toMissQueue(i)         <> icache.io.missQueue.req(i)
9609c6f1ddSLingrui98    ifu.io.icacheInter.fromMissQueue(i)       <> icache.io.missQueue.resp(i)
9709c6f1ddSLingrui98  }
9809c6f1ddSLingrui98
9909c6f1ddSLingrui98  icache.io.missQueue.flush := ifu.io.ftqInter.fromFtq.redirect.valid || (ifu.io.ftqInter.toFtq.pdWb.valid && ifu.io.ftqInter.toFtq.pdWb.bits.misOffset.valid)
10009c6f1ddSLingrui98
10109c6f1ddSLingrui98  //IFU-Ibuffer
10209c6f1ddSLingrui98  ifu.io.toIbuffer    <> ibuffer.io.in
10309c6f1ddSLingrui98
10409c6f1ddSLingrui98  ftq.io.fromBackend <> io.backend.toFtq
10509c6f1ddSLingrui98  io.backend.fromFtq <> ftq.io.toBackend
10609c6f1ddSLingrui98  io.frontendInfo.bpuInfo <> ftq.io.bpuInfo
10709c6f1ddSLingrui98
10809c6f1ddSLingrui98  ibuffer.io.flush := needFlush
10909c6f1ddSLingrui98  io.backend.cfVec <> ibuffer.io.out
11009c6f1ddSLingrui98
11109c6f1ddSLingrui98  instrUncache.io.req   <> DontCare
11209c6f1ddSLingrui98  instrUncache.io.resp  <> DontCare
11309c6f1ddSLingrui98  instrUncache.io.flush <> DontCare
11409c6f1ddSLingrui98  io.error <> DontCare
11509c6f1ddSLingrui98
11609c6f1ddSLingrui98  val frontendBubble = PopCount((0 until DecodeWidth).map(i => io.backend.cfVec(i).ready && !ibuffer.io.out(i).valid))
11709c6f1ddSLingrui98  XSPerfAccumulate("FrontendBubble", frontendBubble)
11809c6f1ddSLingrui98
11909c6f1ddSLingrui98  io.frontendInfo.ibufFull := RegNext(ibuffer.io.full)
12009c6f1ddSLingrui98}
121