xref: /XiangShan/src/main/scala/xiangshan/frontend/IFU.scala (revision 1ca0e4f33f402f31daec0e57d270079d2db13562)
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
1809c6f1ddSLingrui98
1909c6f1ddSLingrui98import chipsalliance.rocketchip.config.Parameters
2009c6f1ddSLingrui98import chisel3._
2109c6f1ddSLingrui98import chisel3.util._
2209c6f1ddSLingrui98import xiangshan._
2309c6f1ddSLingrui98import xiangshan.cache.mmu._
241d8f4dcbSJayimport xiangshan.frontend.icache._
2509c6f1ddSLingrui98import utils._
26b6982e83SLemoverimport xiangshan.backend.fu.{PMPReqBundle, PMPRespBundle}
2709c6f1ddSLingrui98
2809c6f1ddSLingrui98trait HasInstrMMIOConst extends HasXSParameter with HasIFUConst{
2909c6f1ddSLingrui98  def mmioBusWidth = 64
3009c6f1ddSLingrui98  def mmioBusBytes = mmioBusWidth / 8
310be662e4SJay  def maxInstrLen = 32
3209c6f1ddSLingrui98}
3309c6f1ddSLingrui98
3409c6f1ddSLingrui98trait HasIFUConst extends HasXSParameter {
351d8f4dcbSJay  def addrAlign(addr: UInt, bytes: Int, highest: Int): UInt = Cat(addr(highest-1, log2Ceil(bytes)), 0.U(log2Ceil(bytes).W))
361d8f4dcbSJay  def fetchQueueSize = 2
371d8f4dcbSJay}
381d8f4dcbSJay
391d8f4dcbSJayclass IfuPtr(implicit p: Parameters) extends CircularQueuePtr[IfuPtr](entries = 2){
401d8f4dcbSJay  override def cloneType = (new IfuPtr).asInstanceOf[this.type]
411d8f4dcbSJay}
421d8f4dcbSJay
431d8f4dcbSJayobject IfuPtr {
441d8f4dcbSJay  def apply(f: Bool, v: UInt)(implicit p: Parameters): IfuPtr = {
451d8f4dcbSJay    val ptr = Wire(new IfuPtr)
461d8f4dcbSJay    ptr.flag := f
471d8f4dcbSJay    ptr.value := v
481d8f4dcbSJay    ptr
491d8f4dcbSJay  }
501d8f4dcbSJay  def inverse(ptr: IfuPtr)(implicit p: Parameters): IfuPtr = {
511d8f4dcbSJay    apply(!ptr.flag, ptr.value)
521d8f4dcbSJay  }
5309c6f1ddSLingrui98}
5409c6f1ddSLingrui98
5509c6f1ddSLingrui98class IfuToFtqIO(implicit p:Parameters) extends XSBundle {
5609c6f1ddSLingrui98  val pdWb = Valid(new PredecodeWritebackBundle)
5709c6f1ddSLingrui98}
5809c6f1ddSLingrui98
5909c6f1ddSLingrui98class FtqInterface(implicit p: Parameters) extends XSBundle {
6009c6f1ddSLingrui98  val fromFtq = Flipped(new FtqToIfuIO)
6109c6f1ddSLingrui98  val toFtq   = new IfuToFtqIO
6209c6f1ddSLingrui98}
6309c6f1ddSLingrui98
640be662e4SJayclass UncacheInterface(implicit p: Parameters) extends XSBundle {
650be662e4SJay  val fromUncache = Flipped(DecoupledIO(new InsUncacheResp))
660be662e4SJay  val toUncache   = DecoupledIO( new InsUncacheReq )
670be662e4SJay}
6809c6f1ddSLingrui98class NewIFUIO(implicit p: Parameters) extends XSBundle {
6909c6f1ddSLingrui98  val ftqInter        = new FtqInterface
701d8f4dcbSJay  val icacheInter     = Vec(2, Flipped(new ICacheMainPipeBundle))
711d8f4dcbSJay  val icacheStop      = Output(Bool())
721d8f4dcbSJay  val icachePerfInfo  = Input(new ICachePerfInfo)
7309c6f1ddSLingrui98  val toIbuffer       = Decoupled(new FetchToIBuffer)
740be662e4SJay  val uncacheInter   =  new UncacheInterface
7572951335SLi Qianruo  val frontendTrigger = Flipped(new FrontendTdataDistributeIO)
7672951335SLi Qianruo  val csrTriggerEnable = Input(Vec(4, Bool()))
77a37fbf10SJay  val rob_commits = Flipped(Vec(CommitWidth, Valid(new RobCommitInfo)))
7809c6f1ddSLingrui98}
7909c6f1ddSLingrui98
8009c6f1ddSLingrui98// record the situation in which fallThruAddr falls into
8109c6f1ddSLingrui98// the middle of an RVI inst
8209c6f1ddSLingrui98class LastHalfInfo(implicit p: Parameters) extends XSBundle {
8309c6f1ddSLingrui98  val valid = Bool()
8409c6f1ddSLingrui98  val middlePC = UInt(VAddrBits.W)
8509c6f1ddSLingrui98  def matchThisBlock(startAddr: UInt) = valid && middlePC === startAddr
8609c6f1ddSLingrui98}
8709c6f1ddSLingrui98
8809c6f1ddSLingrui98class IfuToPreDecode(implicit p: Parameters) extends XSBundle {
8909c6f1ddSLingrui98  val data          = if(HasCExtension) Vec(PredictWidth + 1, UInt(16.W)) else Vec(PredictWidth, UInt(32.W))
9009c6f1ddSLingrui98  val startAddr     = UInt(VAddrBits.W)
9109c6f1ddSLingrui98  val fallThruAddr  = UInt(VAddrBits.W)
9209c6f1ddSLingrui98  val fallThruError = Bool()
9309c6f1ddSLingrui98  val isDoubleLine  = Bool()
9409c6f1ddSLingrui98  val ftqOffset     = Valid(UInt(log2Ceil(PredictWidth).W))
9509c6f1ddSLingrui98  val target        = UInt(VAddrBits.W)
9609c6f1ddSLingrui98  val pageFault     = Vec(2, Bool())
9709c6f1ddSLingrui98  val accessFault   = Vec(2, Bool())
9809c6f1ddSLingrui98  val instValid     = Bool()
9909c6f1ddSLingrui98  val lastHalfMatch = Bool()
10009c6f1ddSLingrui98  val oversize      = Bool()
10172951335SLi Qianruo  val mmio = Bool()
10272951335SLi Qianruo  val frontendTrigger = new FrontendTdataDistributeIO
10372951335SLi Qianruo  val csrTriggerEnable = Vec(4, Bool())
10409c6f1ddSLingrui98}
10509c6f1ddSLingrui98
1061d8f4dcbSJayclass NewIFU(implicit p: Parameters) extends XSModule with HasICacheParameters with HasIFUConst
107*1ca0e4f3SYinan Xu  with HasCircularQueuePtrHelper with HasPerfEvents
10809c6f1ddSLingrui98{
10909c6f1ddSLingrui98  println(s"icache ways: ${nWays} sets:${nSets}")
11009c6f1ddSLingrui98  val io = IO(new NewIFUIO)
11109c6f1ddSLingrui98  val (toFtq, fromFtq)    = (io.ftqInter.toFtq, io.ftqInter.fromFtq)
1121d8f4dcbSJay  val (toICache, fromICache) = (VecInit(io.icacheInter.map(_.req)), VecInit(io.icacheInter.map(_.resp)))
1130be662e4SJay  val (toUncache, fromUncache) = (io.uncacheInter.toUncache , io.uncacheInter.fromUncache)
11409c6f1ddSLingrui98
11509c6f1ddSLingrui98  def isCrossLineReq(start: UInt, end: UInt): Bool = start(blockOffBits) ^ end(blockOffBits)
11609c6f1ddSLingrui98
11709c6f1ddSLingrui98  def isLastInCacheline(fallThruAddr: UInt): Bool = fallThruAddr(blockOffBits - 1, 1) === 0.U
11809c6f1ddSLingrui98
1191d8f4dcbSJay  class TlbExept(implicit p: Parameters) extends XSBundle{
1201d8f4dcbSJay    val pageFault = Bool()
1211d8f4dcbSJay    val accessFault = Bool()
1221d8f4dcbSJay    val mmio = Bool()
123b005f7c6SJay  }
12409c6f1ddSLingrui98
1251d8f4dcbSJay
12609c6f1ddSLingrui98  //---------------------------------------------
12709c6f1ddSLingrui98  //  Fetch Stage 1 :
12809c6f1ddSLingrui98  //  * Send req to ICache Meta/Data
12909c6f1ddSLingrui98  //  * Check whether need 2 line fetch
13009c6f1ddSLingrui98  //---------------------------------------------
13109c6f1ddSLingrui98
13209c6f1ddSLingrui98  val f0_valid                             = fromFtq.req.valid
13309c6f1ddSLingrui98  val f0_ftq_req                           = fromFtq.req.bits
13409c6f1ddSLingrui98  val f0_situation                         = VecInit(Seq(isCrossLineReq(f0_ftq_req.startAddr, f0_ftq_req.fallThruAddr), isLastInCacheline(f0_ftq_req.fallThruAddr)))
13509c6f1ddSLingrui98  val f0_doubleLine                        = f0_situation(0) || f0_situation(1)
13609c6f1ddSLingrui98  val f0_vSetIdx                           = VecInit(get_idx((f0_ftq_req.startAddr)), get_idx(f0_ftq_req.fallThruAddr))
13709c6f1ddSLingrui98  val f0_fire                              = fromFtq.req.fire()
13809c6f1ddSLingrui98
13909c6f1ddSLingrui98  val f0_flush, f1_flush, f2_flush, f3_flush = WireInit(false.B)
14009c6f1ddSLingrui98  val from_bpu_f0_flush, from_bpu_f1_flush, from_bpu_f2_flush, from_bpu_f3_flush = WireInit(false.B)
14109c6f1ddSLingrui98
14209c6f1ddSLingrui98  from_bpu_f0_flush := fromFtq.flushFromBpu.shouldFlushByStage2(f0_ftq_req.ftqIdx) ||
14309c6f1ddSLingrui98                       fromFtq.flushFromBpu.shouldFlushByStage3(f0_ftq_req.ftqIdx)
14409c6f1ddSLingrui98
14509c6f1ddSLingrui98  val f3_redirect = WireInit(false.B)
14609c6f1ddSLingrui98  f3_flush := fromFtq.redirect.valid
14709c6f1ddSLingrui98  f2_flush := f3_flush || f3_redirect
14809c6f1ddSLingrui98  f1_flush := f2_flush || from_bpu_f1_flush
14909c6f1ddSLingrui98  f0_flush := f1_flush || from_bpu_f0_flush
15009c6f1ddSLingrui98
15109c6f1ddSLingrui98  val f1_ready, f2_ready, f3_ready         = WireInit(false.B)
15209c6f1ddSLingrui98
1531d8f4dcbSJay  fromFtq.req.ready := toICache(0).ready && toICache(1).ready && f2_ready && GTimer() > 500.U
15409c6f1ddSLingrui98
1551d8f4dcbSJay  toICache(0).valid       := fromFtq.req.fire() && !f0_flush
1561d8f4dcbSJay  toICache(0).bits.vaddr  := fromFtq.req.bits.startAddr
1571d8f4dcbSJay  toICache(1).valid       := fromFtq.req.fire() && f0_doubleLine && !f0_flush
1581d8f4dcbSJay  toICache(1).bits.vaddr  := fromFtq.req.bits.fallThruAddr
15909c6f1ddSLingrui98
160f7c29b0aSJinYue
16109c6f1ddSLingrui98  //---------------------------------------------
1621d8f4dcbSJay  //  Fetch Stage 1 :
1631d8f4dcbSJay  //  * Send req to ITLB and TLB Response (Get Paddr)
1641d8f4dcbSJay  //  * ICache Response (Get Meta and Data)
1651d8f4dcbSJay  //  * Hit Check (Generate hit signal and hit vector)
1661d8f4dcbSJay  //  * Get victim way
1671d8f4dcbSJay  //---------------------------------------------
1681d8f4dcbSJay
1691d8f4dcbSJay  val f1_valid      = RegInit(false.B)
1701d8f4dcbSJay  val f1_ftq_req    = RegEnable(next = f0_ftq_req,    enable=f0_fire)
1711d8f4dcbSJay  val f1_situation  = RegEnable(next = f0_situation,  enable=f0_fire)
1721d8f4dcbSJay  val f1_doubleLine = RegEnable(next = f0_doubleLine, enable=f0_fire)
1731d8f4dcbSJay  val f1_vSetIdx    = RegEnable(next = f0_vSetIdx,    enable=f0_fire)
1741d8f4dcbSJay  val f1_fire       = f1_valid && f1_ready
1751d8f4dcbSJay
1761d8f4dcbSJay  f1_ready := f2_ready || !f1_valid
1771d8f4dcbSJay
1781d8f4dcbSJay  from_bpu_f1_flush := fromFtq.flushFromBpu.shouldFlushByStage3(f1_ftq_req.ftqIdx)
1791d8f4dcbSJay
1801d8f4dcbSJay  when(f1_flush)                  {f1_valid  := false.B}
1811d8f4dcbSJay  .elsewhen(f0_fire && !f0_flush) {f1_valid  := true.B}
1821d8f4dcbSJay  .elsewhen(f1_fire)              {f1_valid  := false.B}
1831d8f4dcbSJay  //---------------------------------------------
18409c6f1ddSLingrui98  //  Fetch Stage 2 :
18509c6f1ddSLingrui98  //  * Send req to ITLB and TLB Response (Get Paddr)
18609c6f1ddSLingrui98  //  * ICache Response (Get Meta and Data)
18709c6f1ddSLingrui98  //  * Hit Check (Generate hit signal and hit vector)
18809c6f1ddSLingrui98  //  * Get victim way
18909c6f1ddSLingrui98  //---------------------------------------------
1901d8f4dcbSJay  val icacheRespAllValid = WireInit(false.B)
19109c6f1ddSLingrui98
19209c6f1ddSLingrui98  val f2_valid      = RegInit(false.B)
19309c6f1ddSLingrui98  val f2_ftq_req    = RegEnable(next = f1_ftq_req,    enable=f1_fire)
19409c6f1ddSLingrui98  val f2_situation  = RegEnable(next = f1_situation,  enable=f1_fire)
19509c6f1ddSLingrui98  val f2_doubleLine = RegEnable(next = f1_doubleLine, enable=f1_fire)
1961d8f4dcbSJay  val f2_vSetIdx    = RegEnable(next = f1_vSetIdx,    enable=f1_fire)
1971d8f4dcbSJay  val f2_fire       = f2_valid && f2_ready
1981d8f4dcbSJay
1991d8f4dcbSJay  f2_ready := f3_ready && icacheRespAllValid || !f2_valid
2001d8f4dcbSJay  //TODO: addr compare may be timing critical
2011d8f4dcbSJay  val f2_icache_all_resp_wire       =  fromICache(0).valid && (fromICache(0).bits.vaddr ===  f2_ftq_req.startAddr) && ((fromICache(1).valid && (fromICache(1).bits.vaddr ===  f2_ftq_req.fallThruAddr)) || !f2_doubleLine)
2021d8f4dcbSJay  val f2_icache_all_resp_reg        = RegInit(false.B)
2031d8f4dcbSJay
2041d8f4dcbSJay  icacheRespAllValid := f2_icache_all_resp_reg || f2_icache_all_resp_wire
2051d8f4dcbSJay
2061d8f4dcbSJay  io.icacheStop := !f3_ready
2071d8f4dcbSJay
2081d8f4dcbSJay  when(f2_flush)                                              {f2_icache_all_resp_reg := false.B}
2091d8f4dcbSJay  .elsewhen(f2_valid && f2_icache_all_resp_wire && !f3_ready) {f2_icache_all_resp_reg := true.B}
2101d8f4dcbSJay  .elsewhen(f2_fire && f2_icache_all_resp_reg)                {f2_icache_all_resp_reg := false.B}
21109c6f1ddSLingrui98
21209c6f1ddSLingrui98  when(f2_flush)                  {f2_valid := false.B}
21309c6f1ddSLingrui98  .elsewhen(f1_fire && !f1_flush) {f2_valid := true.B }
21409c6f1ddSLingrui98  .elsewhen(f2_fire)              {f2_valid := false.B}
21509c6f1ddSLingrui98
2161d8f4dcbSJay  val f2_cache_response_data = ResultHoldBypass(valid = f2_icache_all_resp_wire, data = VecInit(fromICache.map(_.bits.readData)))
21709c6f1ddSLingrui98
2181d8f4dcbSJay  val f2_datas        = VecInit((0 until PortNumber).map(i => f2_cache_response_data(i)))
2191d8f4dcbSJay  val f2_except_pf    = VecInit((0 until PortNumber).map(i => fromICache(i).bits.tlbExcp.pageFault))
2201d8f4dcbSJay  val f2_except_af    = VecInit((0 until PortNumber).map(i => fromICache(i).bits.tlbExcp.accessFault))
221c0b2b8e9Srvcoresjw  val f2_mmio         = fromICache(0).bits.tlbExcp.mmio && !fromICache(0).bits.tlbExcp.accessFault &&
222c0b2b8e9Srvcoresjw                                                           !fromICache(0).bits.tlbExcp.pageFault
2230be662e4SJay
2241d8f4dcbSJay  val f2_paddrs       = VecInit((0 until PortNumber).map(i => fromICache(i).bits.paddr))
2251d8f4dcbSJay  val f2_perf_info    = io.icachePerfInfo
22609c6f1ddSLingrui98
22709c6f1ddSLingrui98  def cut(cacheline: UInt, start: UInt) : Vec[UInt] ={
22809c6f1ddSLingrui98    if(HasCExtension){
22909c6f1ddSLingrui98      val result   = Wire(Vec(PredictWidth + 1, UInt(16.W)))
23009c6f1ddSLingrui98      val dataVec  = cacheline.asTypeOf(Vec(blockBytes * 2/ 2, UInt(16.W)))
23109c6f1ddSLingrui98      val startPtr = Cat(0.U(1.W), start(blockOffBits-1, 1))
23209c6f1ddSLingrui98      (0 until PredictWidth + 1).foreach( i =>
23309c6f1ddSLingrui98        result(i) := dataVec(startPtr + i.U)
23409c6f1ddSLingrui98      )
23509c6f1ddSLingrui98      result
23609c6f1ddSLingrui98    } else {
23709c6f1ddSLingrui98      val result   = Wire(Vec(PredictWidth, UInt(32.W)) )
23809c6f1ddSLingrui98      val dataVec  = cacheline.asTypeOf(Vec(blockBytes * 2/ 4, UInt(32.W)))
23909c6f1ddSLingrui98      val startPtr = Cat(0.U(1.W), start(blockOffBits-1, 2))
24009c6f1ddSLingrui98      (0 until PredictWidth).foreach( i =>
24109c6f1ddSLingrui98        result(i) := dataVec(startPtr + i.U)
24209c6f1ddSLingrui98      )
24309c6f1ddSLingrui98      result
24409c6f1ddSLingrui98    }
24509c6f1ddSLingrui98  }
24609c6f1ddSLingrui98
2471d8f4dcbSJay  val preDecoder      = Module(new PreDecode)
2481d8f4dcbSJay  val (preDecoderIn, preDecoderOut)   = (preDecoder.io.in, preDecoder.io.out)
2491d8f4dcbSJay  val predecodeOutValid = WireInit(false.B)
2501d8f4dcbSJay
25109c6f1ddSLingrui98  val f2_cut_data = cut( Cat(f2_datas.map(cacheline => cacheline.asUInt ).reverse).asUInt, f2_ftq_req.startAddr )
25209c6f1ddSLingrui98
25309c6f1ddSLingrui98  //---------------------------------------------
2541d8f4dcbSJay  //  Fetch Stage 3 :
2551d8f4dcbSJay  //  * get data from last stage (hit from f2_hit_data/miss from missQueue response)
25609c6f1ddSLingrui98  //  * if at least one needed cacheline miss, wait for miss queue response (a wait_state machine) THIS IS TOO UGLY!!!
25709c6f1ddSLingrui98  //  * cut cacheline(s) and send to PreDecode
25809c6f1ddSLingrui98  //  * check if prediction is right (branch target and type, jump direction and type , jal target )
25909c6f1ddSLingrui98  //---------------------------------------------
26009c6f1ddSLingrui98  val f3_valid          = RegInit(false.B)
26109c6f1ddSLingrui98  val f3_ftq_req        = RegEnable(next = f2_ftq_req,    enable=f2_fire)
26209c6f1ddSLingrui98  val f3_situation      = RegEnable(next = f2_situation,  enable=f2_fire)
26309c6f1ddSLingrui98  val f3_doubleLine     = RegEnable(next = f2_doubleLine, enable=f2_fire)
2641d8f4dcbSJay  val f3_fire           = io.toIbuffer.fire()
2651d8f4dcbSJay
2661d8f4dcbSJay  f3_ready := io.toIbuffer.ready || !f3_valid
26709c6f1ddSLingrui98
26809c6f1ddSLingrui98  val f3_cut_data       = RegEnable(next = f2_cut_data, enable=f2_fire)
2691d8f4dcbSJay
27009c6f1ddSLingrui98  val f3_except_pf      = RegEnable(next = f2_except_pf, enable = f2_fire)
27109c6f1ddSLingrui98  val f3_except_af      = RegEnable(next = f2_except_af, enable = f2_fire)
2720be662e4SJay  val f3_mmio           = RegEnable(next = f2_mmio   , enable = f2_fire)
27309c6f1ddSLingrui98
27409c6f1ddSLingrui98  val f3_lastHalf       = RegInit(0.U.asTypeOf(new LastHalfInfo))
27509c6f1ddSLingrui98  val f3_lastHalfMatch  = f3_lastHalf.matchThisBlock(f3_ftq_req.startAddr)
27609c6f1ddSLingrui98  val f3_except         = VecInit((0 until 2).map{i => f3_except_pf(i) || f3_except_af(i)})
27709c6f1ddSLingrui98  val f3_has_except     = f3_valid && (f3_except_af.reduce(_||_) || f3_except_pf.reduce(_||_))
2781d8f4dcbSJay  val f3_pAddrs   = RegEnable(next = f2_paddrs, enable = f2_fire)
279a37fbf10SJay
280a37fbf10SJay  val f3_mmio_data    = Reg(UInt(maxInstrLen.W))
281a37fbf10SJay
282a37fbf10SJay  val f3_data = if(HasCExtension) Wire(Vec(PredictWidth + 1, UInt(16.W))) else Wire(Vec(PredictWidth, UInt(32.W)))
283a37fbf10SJay  f3_data       :=  f3_cut_data
28409c6f1ddSLingrui98
285a37fbf10SJay  val mmio_idle :: mmio_send_req :: mmio_w_resp :: mmio_resend :: mmio_resend_w_resp :: mmio_w_commit :: Nil = Enum(6)
286a37fbf10SJay  val mmio_state = RegInit(mmio_idle)
287a37fbf10SJay
2889bae7d6eSJay  val f3_req_is_mmio     = f3_mmio && f3_valid
289a37fbf10SJay  val mmio_has_commited = VecInit(io.rob_commits.map{commit => commit.valid && commit.bits.ftqIdx === f3_ftq_req.ftqIdx &&  commit.bits.ftqOffset === 0.U}).asUInt.orR
290a37fbf10SJay  val f3_mmio_req_commit = f3_req_is_mmio && mmio_state === mmio_w_commit && mmio_has_commited
291a37fbf10SJay
292a37fbf10SJay  val f3_mmio_to_commit =  f3_req_is_mmio && mmio_state === mmio_w_commit
293a37fbf10SJay  val f3_mmio_to_commit_next = RegNext(f3_mmio_to_commit)
294a37fbf10SJay  val f3_mmio_can_go      = f3_mmio_to_commit && !f3_mmio_to_commit_next
295a37fbf10SJay
2969bae7d6eSJay  val f3_ftq_flush_self     = fromFtq.redirect.valid && RedirectLevel.flushItself(fromFtq.redirect.bits.level)
297167bcd01SJay  val f3_ftq_flush_by_older = fromFtq.redirect.valid && isBefore(fromFtq.redirect.bits.ftqIdx, f3_ftq_req.ftqIdx)
2989bae7d6eSJay
299167bcd01SJay  val f3_need_not_flush = f3_req_is_mmio && fromFtq.redirect.valid && !f3_ftq_flush_self && !f3_ftq_flush_by_older
3009bae7d6eSJay
3019bae7d6eSJay  when(f3_flush && !f3_need_not_flush)               {f3_valid := false.B}
302a37fbf10SJay  .elsewhen(f2_fire && !f2_flush)                    {f3_valid := true.B }
303a37fbf10SJay  .elsewhen(io.toIbuffer.fire() && !f3_req_is_mmio)  {f3_valid := false.B}
304a37fbf10SJay  .elsewhen{f3_req_is_mmio && f3_mmio_req_commit}    {f3_valid := false.B}
305a37fbf10SJay
306a37fbf10SJay  val f3_mmio_use_seq_pc = RegInit(false.B)
307a37fbf10SJay
308a37fbf10SJay  val (redirect_ftqIdx, redirect_ftqOffset)  = (fromFtq.redirect.bits.ftqIdx,fromFtq.redirect.bits.ftqOffset)
309a37fbf10SJay  val redirect_mmio_req = fromFtq.redirect.valid && redirect_ftqIdx === f3_ftq_req.ftqIdx && redirect_ftqOffset === 0.U
310a37fbf10SJay
311a37fbf10SJay  when(RegNext(f2_fire && !f2_flush) && f3_req_is_mmio)        { f3_mmio_use_seq_pc := true.B  }
312a37fbf10SJay  .elsewhen(redirect_mmio_req)                                 { f3_mmio_use_seq_pc := false.B }
313a37fbf10SJay
314a37fbf10SJay  f3_ready := Mux(f3_req_is_mmio, io.toIbuffer.ready && f3_mmio_req_commit || !f3_valid , io.toIbuffer.ready || !f3_valid)
315a37fbf10SJay
3169bae7d6eSJay  when(f3_req_is_mmio){
3179bae7d6eSJay    f3_data(0) := f3_mmio_data(15, 0)
3189bae7d6eSJay    f3_data(1) := f3_mmio_data(31, 16)
3199bae7d6eSJay  }
3209bae7d6eSJay
321a37fbf10SJay  when(fromUncache.fire())    {f3_mmio_data   :=  fromUncache.bits.data}
322a37fbf10SJay
323a37fbf10SJay
324a37fbf10SJay  switch(mmio_state){
325a37fbf10SJay    is(mmio_idle){
3269bae7d6eSJay      when(f3_req_is_mmio){
327a37fbf10SJay        mmio_state :=  mmio_send_req
328a37fbf10SJay      }
329a37fbf10SJay    }
330a37fbf10SJay
331a37fbf10SJay    is(mmio_send_req){
332a37fbf10SJay      mmio_state :=  Mux(toUncache.fire(), mmio_w_resp, mmio_send_req )
333a37fbf10SJay    }
334a37fbf10SJay
335a37fbf10SJay    is(mmio_w_resp){
336a37fbf10SJay      when(fromUncache.fire()){
337a37fbf10SJay          val isRVC =  fromUncache.bits.data(1,0) =/= 3.U
338a37fbf10SJay          mmio_state :=  Mux(isRVC, mmio_resend , mmio_w_commit)
339a37fbf10SJay      }
340a37fbf10SJay    }
341a37fbf10SJay
342a37fbf10SJay    is(mmio_resend){
343a37fbf10SJay      mmio_state :=  Mux(toUncache.fire(), mmio_resend_w_resp, mmio_resend )
344a37fbf10SJay    }
345a37fbf10SJay
346a37fbf10SJay    is(mmio_resend_w_resp){
347a37fbf10SJay      when(fromUncache.fire()){
348a37fbf10SJay          mmio_state :=  mmio_w_commit
349a37fbf10SJay      }
350a37fbf10SJay    }
351a37fbf10SJay
352a37fbf10SJay    is(mmio_w_commit){
353a37fbf10SJay      when(mmio_has_commited){
354a37fbf10SJay          mmio_state  :=  mmio_idle
355a37fbf10SJay      }
356a37fbf10SJay    }
357a37fbf10SJay  }
358a37fbf10SJay
359167bcd01SJay  when(f3_ftq_flush_self || f3_ftq_flush_by_older)  {
3609bae7d6eSJay    mmio_state := mmio_idle
3619bae7d6eSJay    f3_mmio_data := 0.U
3629bae7d6eSJay  }
3639bae7d6eSJay
364a37fbf10SJay  toUncache.valid     :=  ((mmio_state === mmio_send_req) || (mmio_state === mmio_resend)) && f3_req_is_mmio
365a37fbf10SJay  toUncache.bits.addr := Mux((mmio_state === mmio_resend), f3_pAddrs(0) + 2.U, f3_pAddrs(0))
366a37fbf10SJay  fromUncache.ready   := true.B
367a37fbf10SJay
36809c6f1ddSLingrui98  preDecoderIn.instValid     :=  f3_valid && !f3_has_except
369a37fbf10SJay  preDecoderIn.data          :=  f3_data
37009c6f1ddSLingrui98  preDecoderIn.startAddr     :=  f3_ftq_req.startAddr
37109c6f1ddSLingrui98  preDecoderIn.fallThruAddr  :=  f3_ftq_req.fallThruAddr
37209c6f1ddSLingrui98  preDecoderIn.fallThruError :=  f3_ftq_req.fallThruError
37309c6f1ddSLingrui98  preDecoderIn.isDoubleLine  :=  f3_doubleLine
37409c6f1ddSLingrui98  preDecoderIn.ftqOffset     :=  f3_ftq_req.ftqOffset
37509c6f1ddSLingrui98  preDecoderIn.target        :=  f3_ftq_req.target
37609c6f1ddSLingrui98  preDecoderIn.oversize      :=  f3_ftq_req.oversize
37709c6f1ddSLingrui98  preDecoderIn.lastHalfMatch :=  f3_lastHalfMatch
37809c6f1ddSLingrui98  preDecoderIn.pageFault     :=  f3_except_pf
37909c6f1ddSLingrui98  preDecoderIn.accessFault   :=  f3_except_af
38072951335SLi Qianruo  preDecoderIn.mmio          :=  f3_mmio
38172951335SLi Qianruo  preDecoderIn.frontendTrigger := io.frontendTrigger
38272951335SLi Qianruo  preDecoderIn.csrTriggerEnable := io.csrTriggerEnable
38309c6f1ddSLingrui98
38409c6f1ddSLingrui98
38509c6f1ddSLingrui98  // TODO: What if next packet does not match?
38609c6f1ddSLingrui98  when (f3_flush) {
38709c6f1ddSLingrui98    f3_lastHalf.valid := false.B
38809c6f1ddSLingrui98  }.elsewhen (io.toIbuffer.fire()) {
38909c6f1ddSLingrui98    f3_lastHalf.valid := preDecoderOut.hasLastHalf
39009c6f1ddSLingrui98    f3_lastHalf.middlePC := preDecoderOut.realEndPC
39109c6f1ddSLingrui98  }
39209c6f1ddSLingrui98
39309c6f1ddSLingrui98  val f3_predecode_range = VecInit(preDecoderOut.pd.map(inst => inst.valid)).asUInt
3940be662e4SJay  val f3_mmio_range      = VecInit((0 until PredictWidth).map(i => if(i ==0) true.B else false.B))
39509c6f1ddSLingrui98
396a37fbf10SJay  io.toIbuffer.valid          := f3_valid && (!f3_req_is_mmio || f3_mmio_can_go)
39709c6f1ddSLingrui98  io.toIbuffer.bits.instrs    := preDecoderOut.instrs
398a37fbf10SJay  io.toIbuffer.bits.valid     := Mux(f3_req_is_mmio, f3_mmio_range.asUInt, f3_predecode_range & preDecoderOut.instrRange.asUInt)
39909c6f1ddSLingrui98  io.toIbuffer.bits.pd        := preDecoderOut.pd
40009c6f1ddSLingrui98  io.toIbuffer.bits.ftqPtr    := f3_ftq_req.ftqIdx
40109c6f1ddSLingrui98  io.toIbuffer.bits.pc        := preDecoderOut.pc
402a37fbf10SJay  io.toIbuffer.bits.ftqOffset.zipWithIndex.map{case(a, i) => a.bits := i.U; a.valid := preDecoderOut.takens(i) && !f3_req_is_mmio}
40309c6f1ddSLingrui98  io.toIbuffer.bits.foldpc    := preDecoderOut.pc.map(i => XORFold(i(VAddrBits-1,1), MemPredPCWidth))
40409c6f1ddSLingrui98  io.toIbuffer.bits.ipf       := preDecoderOut.pageFault
40509c6f1ddSLingrui98  io.toIbuffer.bits.acf       := preDecoderOut.accessFault
40609c6f1ddSLingrui98  io.toIbuffer.bits.crossPageIPFFix := preDecoderOut.crossPageIPF
40772951335SLi Qianruo  io.toIbuffer.bits.triggered := preDecoderOut.triggered
40809c6f1ddSLingrui98
40909c6f1ddSLingrui98  //Write back to Ftq
410a37fbf10SJay  val f3_cache_fetch = f3_valid && !(f2_fire && !f2_flush)
411a37fbf10SJay  val finishFetchMaskReg = RegNext(f3_cache_fetch)
412a37fbf10SJay
41309c6f1ddSLingrui98
4140be662e4SJay  val f3_mmio_missOffset = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)))
415a37fbf10SJay  f3_mmio_missOffset.valid := f3_req_is_mmio
4160be662e4SJay  f3_mmio_missOffset.bits  := 0.U
4170be662e4SJay
418a37fbf10SJay  toFtq.pdWb.valid           := (!finishFetchMaskReg && f3_valid && !f3_req_is_mmio) || (f3_mmio_req_commit && f3_mmio_use_seq_pc)
41909c6f1ddSLingrui98  toFtq.pdWb.bits.pc         := preDecoderOut.pc
42009c6f1ddSLingrui98  toFtq.pdWb.bits.pd         := preDecoderOut.pd
421a37fbf10SJay  toFtq.pdWb.bits.pd.zipWithIndex.map{case(instr,i) => instr.valid :=  Mux(f3_req_is_mmio, f3_mmio_range(i), f3_predecode_range(i))}
42209c6f1ddSLingrui98  toFtq.pdWb.bits.ftqIdx     := f3_ftq_req.ftqIdx
42309c6f1ddSLingrui98  toFtq.pdWb.bits.ftqOffset  := f3_ftq_req.ftqOffset.bits
424a37fbf10SJay  toFtq.pdWb.bits.misOffset  := Mux(f3_req_is_mmio, f3_mmio_missOffset, preDecoderOut.misOffset)
42509c6f1ddSLingrui98  toFtq.pdWb.bits.cfiOffset  := preDecoderOut.cfiOffset
426a37fbf10SJay  toFtq.pdWb.bits.target     := Mux(f3_req_is_mmio,Mux((f3_mmio_data(1,0) =/= 3.U), f3_ftq_req.startAddr + 2.U , f3_ftq_req.startAddr + 4.U) ,preDecoderOut.target)
42709c6f1ddSLingrui98  toFtq.pdWb.bits.jalTarget  := preDecoderOut.jalTarget
428a37fbf10SJay  toFtq.pdWb.bits.instrRange := Mux(f3_req_is_mmio, f3_mmio_range, preDecoderOut.instrRange)
42909c6f1ddSLingrui98
430a37fbf10SJay  val predecodeFlush     = preDecoderOut.misOffset.valid && f3_valid
43109c6f1ddSLingrui98  val predecodeFlushReg  = RegNext(predecodeFlush && !(f2_fire && !f2_flush))
432*1ca0e4f3SYinan Xu  f3_redirect := (!predecodeFlushReg && predecodeFlush && !f3_req_is_mmio) || (f3_mmio_req_commit && f3_mmio_use_seq_pc)
43309c6f1ddSLingrui98
4341d8f4dcbSJay
4351d8f4dcbSJay  /** performance counter */
4361d8f4dcbSJay  val f3_perf_info     = RegEnable(next = f2_perf_info, enable = f2_fire)
4371d8f4dcbSJay  val f3_req_0    = io.toIbuffer.fire()
4381d8f4dcbSJay  val f3_req_1    = io.toIbuffer.fire() && f3_doubleLine
4391d8f4dcbSJay  val f3_hit_0    = io.toIbuffer.fire() && f3_perf_info.bank_hit(0)
4401d8f4dcbSJay  val f3_hit_1    = io.toIbuffer.fire() && f3_doubleLine & f3_perf_info.bank_hit(1)
4411d8f4dcbSJay  val f3_hit      = f3_perf_info.hit
442cd365d4cSrvcoresjw  val perfEvents = Seq(
443cd365d4cSrvcoresjw    ("frontendFlush                ", f3_redirect                                ),
444cd365d4cSrvcoresjw    ("ifu_req                      ", io.toIbuffer.fire()                        ),
4451d8f4dcbSJay    ("ifu_miss                     ", io.toIbuffer.fire() && !f3_perf_info.hit   ),
446cd365d4cSrvcoresjw    ("ifu_req_cacheline_0          ", f3_req_0                                   ),
447cd365d4cSrvcoresjw    ("ifu_req_cacheline_1          ", f3_req_1                                   ),
448cd365d4cSrvcoresjw    ("ifu_req_cacheline_0_hit      ", f3_hit_1                                   ),
449cd365d4cSrvcoresjw    ("ifu_req_cacheline_1_hit      ", f3_hit_1                                   ),
4501d8f4dcbSJay    ("only_0_hit                   ", f3_perf_info.only_0_hit       && io.toIbuffer.fire() ),
4511d8f4dcbSJay    ("only_0_miss                  ", f3_perf_info.only_0_miss      && io.toIbuffer.fire() ),
4521d8f4dcbSJay    ("hit_0_hit_1                  ", f3_perf_info.hit_0_hit_1      && io.toIbuffer.fire() ),
4531d8f4dcbSJay    ("hit_0_miss_1                 ", f3_perf_info.hit_0_miss_1     && io.toIbuffer.fire() ),
4541d8f4dcbSJay    ("miss_0_hit_1                 ", f3_perf_info.miss_0_hit_1     && io.toIbuffer.fire() ),
4551d8f4dcbSJay    ("miss_0_miss_1                ", f3_perf_info.miss_0_miss_1    && io.toIbuffer.fire() ),
456cd365d4cSrvcoresjw    ("cross_line_block             ", io.toIbuffer.fire() && f3_situation(0)     ),
457cd365d4cSrvcoresjw    ("fall_through_is_cacheline_end", io.toIbuffer.fire() && f3_situation(1)     ),
458cd365d4cSrvcoresjw  )
459*1ca0e4f3SYinan Xu  generatePerfEvent()
46009c6f1ddSLingrui98
461f7c29b0aSJinYue  XSPerfAccumulate("ifu_req",   io.toIbuffer.fire() )
462f7c29b0aSJinYue  XSPerfAccumulate("ifu_miss",  io.toIbuffer.fire() && !f3_hit )
463f7c29b0aSJinYue  XSPerfAccumulate("ifu_req_cacheline_0", f3_req_0  )
464f7c29b0aSJinYue  XSPerfAccumulate("ifu_req_cacheline_1", f3_req_1  )
465f7c29b0aSJinYue  XSPerfAccumulate("ifu_req_cacheline_0_hit",   f3_hit_0 )
466f7c29b0aSJinYue  XSPerfAccumulate("ifu_req_cacheline_1_hit",   f3_hit_1 )
46709c6f1ddSLingrui98  XSPerfAccumulate("frontendFlush",   f3_redirect )
4681d8f4dcbSJay  XSPerfAccumulate("only_0_hit",      f3_perf_info.only_0_hit   && io.toIbuffer.fire()  )
4691d8f4dcbSJay  XSPerfAccumulate("only_0_miss",     f3_perf_info.only_0_miss  && io.toIbuffer.fire()  )
4701d8f4dcbSJay  XSPerfAccumulate("hit_0_hit_1",     f3_perf_info.hit_0_hit_1  && io.toIbuffer.fire()  )
4711d8f4dcbSJay  XSPerfAccumulate("hit_0_miss_1",    f3_perf_info.hit_0_miss_1  && io.toIbuffer.fire()  )
4721d8f4dcbSJay  XSPerfAccumulate("miss_0_hit_1",    f3_perf_info.miss_0_hit_1   && io.toIbuffer.fire() )
4731d8f4dcbSJay  XSPerfAccumulate("miss_0_miss_1",   f3_perf_info.miss_0_miss_1 && io.toIbuffer.fire() )
474f7c29b0aSJinYue  XSPerfAccumulate("cross_line_block", io.toIbuffer.fire() && f3_situation(0) )
475f7c29b0aSJinYue  XSPerfAccumulate("fall_through_is_cacheline_end", io.toIbuffer.fire() && f3_situation(1) )
47609c6f1ddSLingrui98}
477