xref: /XiangShan/src/main/scala/xiangshan/frontend/IFU.scala (revision 1d8f4dcb81f46f3830f09013a9836143401cc425)
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._
24*1d8f4dcbSJayimport 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 {
35*1d8f4dcbSJay  def addrAlign(addr: UInt, bytes: Int, highest: Int): UInt = Cat(addr(highest-1, log2Ceil(bytes)), 0.U(log2Ceil(bytes).W))
36*1d8f4dcbSJay  def fetchQueueSize = 2
37*1d8f4dcbSJay}
38*1d8f4dcbSJay
39*1d8f4dcbSJayclass IfuPtr(implicit p: Parameters) extends CircularQueuePtr[IfuPtr](entries = 2){
40*1d8f4dcbSJay  override def cloneType = (new IfuPtr).asInstanceOf[this.type]
41*1d8f4dcbSJay}
42*1d8f4dcbSJay
43*1d8f4dcbSJayobject IfuPtr {
44*1d8f4dcbSJay  def apply(f: Bool, v: UInt)(implicit p: Parameters): IfuPtr = {
45*1d8f4dcbSJay    val ptr = Wire(new IfuPtr)
46*1d8f4dcbSJay    ptr.flag := f
47*1d8f4dcbSJay    ptr.value := v
48*1d8f4dcbSJay    ptr
49*1d8f4dcbSJay  }
50*1d8f4dcbSJay  def inverse(ptr: IfuPtr)(implicit p: Parameters): IfuPtr = {
51*1d8f4dcbSJay    apply(!ptr.flag, ptr.value)
52*1d8f4dcbSJay  }
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
70*1d8f4dcbSJay  val icacheInter     = Vec(2, Flipped(new ICacheMainPipeBundle))
71*1d8f4dcbSJay  val icacheStop      = Output(Bool())
72*1d8f4dcbSJay  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
106*1d8f4dcbSJayclass NewIFU(implicit p: Parameters) extends XSModule with HasICacheParameters with HasIFUConst
107167bcd01SJaywith HasCircularQueuePtrHelper
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)
112*1d8f4dcbSJay  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
119*1d8f4dcbSJay  class TlbExept(implicit p: Parameters) extends XSBundle{
120*1d8f4dcbSJay    val pageFault = Bool()
121*1d8f4dcbSJay    val accessFault = Bool()
122*1d8f4dcbSJay    val mmio = Bool()
123b005f7c6SJay  }
12409c6f1ddSLingrui98
125*1d8f4dcbSJay
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
153*1d8f4dcbSJay  fromFtq.req.ready := toICache(0).ready && toICache(1).ready && f2_ready && GTimer() > 500.U
15409c6f1ddSLingrui98
155*1d8f4dcbSJay  toICache(0).valid       := fromFtq.req.fire() && !f0_flush
156*1d8f4dcbSJay  toICache(0).bits.vaddr  := fromFtq.req.bits.startAddr
157*1d8f4dcbSJay  toICache(1).valid       := fromFtq.req.fire() && f0_doubleLine && !f0_flush
158*1d8f4dcbSJay  toICache(1).bits.vaddr  := fromFtq.req.bits.fallThruAddr
15909c6f1ddSLingrui98
160f7c29b0aSJinYue
16109c6f1ddSLingrui98  //---------------------------------------------
162*1d8f4dcbSJay  //  Fetch Stage 1 :
163*1d8f4dcbSJay  //  * Send req to ITLB and TLB Response (Get Paddr)
164*1d8f4dcbSJay  //  * ICache Response (Get Meta and Data)
165*1d8f4dcbSJay  //  * Hit Check (Generate hit signal and hit vector)
166*1d8f4dcbSJay  //  * Get victim way
167*1d8f4dcbSJay  //---------------------------------------------
168*1d8f4dcbSJay
169*1d8f4dcbSJay  val f1_valid      = RegInit(false.B)
170*1d8f4dcbSJay  val f1_ftq_req    = RegEnable(next = f0_ftq_req,    enable=f0_fire)
171*1d8f4dcbSJay  val f1_situation  = RegEnable(next = f0_situation,  enable=f0_fire)
172*1d8f4dcbSJay  val f1_doubleLine = RegEnable(next = f0_doubleLine, enable=f0_fire)
173*1d8f4dcbSJay  val f1_vSetIdx    = RegEnable(next = f0_vSetIdx,    enable=f0_fire)
174*1d8f4dcbSJay  val f1_fire       = f1_valid && f1_ready
175*1d8f4dcbSJay
176*1d8f4dcbSJay  f1_ready := f2_ready || !f1_valid
177*1d8f4dcbSJay
178*1d8f4dcbSJay  from_bpu_f1_flush := fromFtq.flushFromBpu.shouldFlushByStage3(f1_ftq_req.ftqIdx)
179*1d8f4dcbSJay
180*1d8f4dcbSJay  when(f1_flush)                  {f1_valid  := false.B}
181*1d8f4dcbSJay  .elsewhen(f0_fire && !f0_flush) {f1_valid  := true.B}
182*1d8f4dcbSJay  .elsewhen(f1_fire)              {f1_valid  := false.B}
183*1d8f4dcbSJay  //---------------------------------------------
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  //---------------------------------------------
190*1d8f4dcbSJay  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)
196*1d8f4dcbSJay  val f2_vSetIdx    = RegEnable(next = f1_vSetIdx,    enable=f1_fire)
197*1d8f4dcbSJay  val f2_fire       = f2_valid && f2_ready
198*1d8f4dcbSJay
199*1d8f4dcbSJay  def ResultHoldBypass[T<:Data](data: T, valid: Bool): T = {
200*1d8f4dcbSJay    Mux(valid, data, RegEnable(data, valid))
201*1d8f4dcbSJay  }
202*1d8f4dcbSJay
203*1d8f4dcbSJay  f2_ready := f3_ready && icacheRespAllValid || !f2_valid
204*1d8f4dcbSJay  //TODO: addr compare may be timing critical
205*1d8f4dcbSJay  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)
206*1d8f4dcbSJay  val f2_icache_all_resp_reg        = RegInit(false.B)
207*1d8f4dcbSJay
208*1d8f4dcbSJay  icacheRespAllValid := f2_icache_all_resp_reg || f2_icache_all_resp_wire
209*1d8f4dcbSJay
210*1d8f4dcbSJay  io.icacheStop := !f3_ready
211*1d8f4dcbSJay
212*1d8f4dcbSJay  when(f2_flush)                                              {f2_icache_all_resp_reg := false.B}
213*1d8f4dcbSJay  .elsewhen(f2_valid && f2_icache_all_resp_wire && !f3_ready) {f2_icache_all_resp_reg := true.B}
214*1d8f4dcbSJay  .elsewhen(f2_fire && f2_icache_all_resp_reg)                {f2_icache_all_resp_reg := false.B}
21509c6f1ddSLingrui98
21609c6f1ddSLingrui98  when(f2_flush)                  {f2_valid := false.B}
21709c6f1ddSLingrui98  .elsewhen(f1_fire && !f1_flush) {f2_valid := true.B }
21809c6f1ddSLingrui98  .elsewhen(f2_fire)              {f2_valid := false.B}
21909c6f1ddSLingrui98
220*1d8f4dcbSJay  val f2_cache_response_data = ResultHoldBypass(valid = f2_icache_all_resp_wire, data = VecInit(fromICache.map(_.bits.readData)))
22109c6f1ddSLingrui98
222*1d8f4dcbSJay  val f2_datas        = VecInit((0 until PortNumber).map(i => f2_cache_response_data(i)))
223*1d8f4dcbSJay  val f2_except_pf    = VecInit((0 until PortNumber).map(i => fromICache(i).bits.tlbExcp.pageFault))
224*1d8f4dcbSJay  val f2_except_af    = VecInit((0 until PortNumber).map(i => fromICache(i).bits.tlbExcp.accessFault))
225*1d8f4dcbSJay  val f2_mmio         = fromICache(0).bits.tlbExcp.mmio && !fromICache(0).bits.tlbExcp.accessFault
2260be662e4SJay
227*1d8f4dcbSJay  val f2_paddrs       = VecInit((0 until PortNumber).map(i => fromICache(i).bits.paddr))
228*1d8f4dcbSJay  val f2_perf_info    = io.icachePerfInfo
22909c6f1ddSLingrui98
23009c6f1ddSLingrui98  def cut(cacheline: UInt, start: UInt) : Vec[UInt] ={
23109c6f1ddSLingrui98    if(HasCExtension){
23209c6f1ddSLingrui98      val result   = Wire(Vec(PredictWidth + 1, UInt(16.W)))
23309c6f1ddSLingrui98      val dataVec  = cacheline.asTypeOf(Vec(blockBytes * 2/ 2, UInt(16.W)))
23409c6f1ddSLingrui98      val startPtr = Cat(0.U(1.W), start(blockOffBits-1, 1))
23509c6f1ddSLingrui98      (0 until PredictWidth + 1).foreach( i =>
23609c6f1ddSLingrui98        result(i) := dataVec(startPtr + i.U)
23709c6f1ddSLingrui98      )
23809c6f1ddSLingrui98      result
23909c6f1ddSLingrui98    } else {
24009c6f1ddSLingrui98      val result   = Wire(Vec(PredictWidth, UInt(32.W)) )
24109c6f1ddSLingrui98      val dataVec  = cacheline.asTypeOf(Vec(blockBytes * 2/ 4, UInt(32.W)))
24209c6f1ddSLingrui98      val startPtr = Cat(0.U(1.W), start(blockOffBits-1, 2))
24309c6f1ddSLingrui98      (0 until PredictWidth).foreach( i =>
24409c6f1ddSLingrui98        result(i) := dataVec(startPtr + i.U)
24509c6f1ddSLingrui98      )
24609c6f1ddSLingrui98      result
24709c6f1ddSLingrui98    }
24809c6f1ddSLingrui98  }
24909c6f1ddSLingrui98
250*1d8f4dcbSJay  val preDecoder      = Module(new PreDecode)
251*1d8f4dcbSJay  val (preDecoderIn, preDecoderOut)   = (preDecoder.io.in, preDecoder.io.out)
252*1d8f4dcbSJay  val predecodeOutValid = WireInit(false.B)
253*1d8f4dcbSJay
25409c6f1ddSLingrui98  val f2_cut_data = cut( Cat(f2_datas.map(cacheline => cacheline.asUInt ).reverse).asUInt, f2_ftq_req.startAddr )
25509c6f1ddSLingrui98
25609c6f1ddSLingrui98  //---------------------------------------------
257*1d8f4dcbSJay  //  Fetch Stage 3 :
258*1d8f4dcbSJay  //  * get data from last stage (hit from f2_hit_data/miss from missQueue response)
25909c6f1ddSLingrui98  //  * if at least one needed cacheline miss, wait for miss queue response (a wait_state machine) THIS IS TOO UGLY!!!
26009c6f1ddSLingrui98  //  * cut cacheline(s) and send to PreDecode
26109c6f1ddSLingrui98  //  * check if prediction is right (branch target and type, jump direction and type , jal target )
26209c6f1ddSLingrui98  //---------------------------------------------
26309c6f1ddSLingrui98  val f3_valid          = RegInit(false.B)
26409c6f1ddSLingrui98  val f3_ftq_req        = RegEnable(next = f2_ftq_req,    enable=f2_fire)
26509c6f1ddSLingrui98  val f3_situation      = RegEnable(next = f2_situation,  enable=f2_fire)
26609c6f1ddSLingrui98  val f3_doubleLine     = RegEnable(next = f2_doubleLine, enable=f2_fire)
267*1d8f4dcbSJay  val f3_fire           = io.toIbuffer.fire()
268*1d8f4dcbSJay
269*1d8f4dcbSJay  f3_ready := io.toIbuffer.ready || !f3_valid
27009c6f1ddSLingrui98
27109c6f1ddSLingrui98  val f3_cut_data       = RegEnable(next = f2_cut_data, enable=f2_fire)
272*1d8f4dcbSJay
27309c6f1ddSLingrui98  val f3_except_pf      = RegEnable(next = f2_except_pf, enable = f2_fire)
27409c6f1ddSLingrui98  val f3_except_af      = RegEnable(next = f2_except_af, enable = f2_fire)
2750be662e4SJay  val f3_mmio           = RegEnable(next = f2_mmio   , enable = f2_fire)
27609c6f1ddSLingrui98
27709c6f1ddSLingrui98  val f3_lastHalf       = RegInit(0.U.asTypeOf(new LastHalfInfo))
27809c6f1ddSLingrui98  val f3_lastHalfMatch  = f3_lastHalf.matchThisBlock(f3_ftq_req.startAddr)
27909c6f1ddSLingrui98  val f3_except         = VecInit((0 until 2).map{i => f3_except_pf(i) || f3_except_af(i)})
28009c6f1ddSLingrui98  val f3_has_except     = f3_valid && (f3_except_af.reduce(_||_) || f3_except_pf.reduce(_||_))
281*1d8f4dcbSJay  val f3_pAddrs   = RegEnable(next = f2_paddrs, enable = f2_fire)
282a37fbf10SJay
283a37fbf10SJay  val f3_mmio_data    = Reg(UInt(maxInstrLen.W))
284a37fbf10SJay
285a37fbf10SJay  val f3_data = if(HasCExtension) Wire(Vec(PredictWidth + 1, UInt(16.W))) else Wire(Vec(PredictWidth, UInt(32.W)))
286a37fbf10SJay  f3_data       :=  f3_cut_data
28709c6f1ddSLingrui98
288a37fbf10SJay  val mmio_idle :: mmio_send_req :: mmio_w_resp :: mmio_resend :: mmio_resend_w_resp :: mmio_w_commit :: Nil = Enum(6)
289a37fbf10SJay  val mmio_state = RegInit(mmio_idle)
290a37fbf10SJay
2919bae7d6eSJay  val f3_req_is_mmio     = f3_mmio && f3_valid
292a37fbf10SJay  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
293a37fbf10SJay  val f3_mmio_req_commit = f3_req_is_mmio && mmio_state === mmio_w_commit && mmio_has_commited
294a37fbf10SJay
295a37fbf10SJay  val f3_mmio_to_commit =  f3_req_is_mmio && mmio_state === mmio_w_commit
296a37fbf10SJay  val f3_mmio_to_commit_next = RegNext(f3_mmio_to_commit)
297a37fbf10SJay  val f3_mmio_can_go      = f3_mmio_to_commit && !f3_mmio_to_commit_next
298a37fbf10SJay
2999bae7d6eSJay  val f3_ftq_flush_self     = fromFtq.redirect.valid && RedirectLevel.flushItself(fromFtq.redirect.bits.level)
300167bcd01SJay  val f3_ftq_flush_by_older = fromFtq.redirect.valid && isBefore(fromFtq.redirect.bits.ftqIdx, f3_ftq_req.ftqIdx)
3019bae7d6eSJay
302167bcd01SJay  val f3_need_not_flush = f3_req_is_mmio && fromFtq.redirect.valid && !f3_ftq_flush_self && !f3_ftq_flush_by_older
3039bae7d6eSJay
3049bae7d6eSJay  when(f3_flush && !f3_need_not_flush)               {f3_valid := false.B}
305a37fbf10SJay  .elsewhen(f2_fire && !f2_flush)                    {f3_valid := true.B }
306a37fbf10SJay  .elsewhen(io.toIbuffer.fire() && !f3_req_is_mmio)  {f3_valid := false.B}
307a37fbf10SJay  .elsewhen{f3_req_is_mmio && f3_mmio_req_commit}    {f3_valid := false.B}
308a37fbf10SJay
309a37fbf10SJay  val f3_mmio_use_seq_pc = RegInit(false.B)
310a37fbf10SJay
311a37fbf10SJay  val (redirect_ftqIdx, redirect_ftqOffset)  = (fromFtq.redirect.bits.ftqIdx,fromFtq.redirect.bits.ftqOffset)
312a37fbf10SJay  val redirect_mmio_req = fromFtq.redirect.valid && redirect_ftqIdx === f3_ftq_req.ftqIdx && redirect_ftqOffset === 0.U
313a37fbf10SJay
314a37fbf10SJay  when(RegNext(f2_fire && !f2_flush) && f3_req_is_mmio)        { f3_mmio_use_seq_pc := true.B  }
315a37fbf10SJay  .elsewhen(redirect_mmio_req)                                 { f3_mmio_use_seq_pc := false.B }
316a37fbf10SJay
317a37fbf10SJay  f3_ready := Mux(f3_req_is_mmio, io.toIbuffer.ready && f3_mmio_req_commit || !f3_valid , io.toIbuffer.ready || !f3_valid)
318a37fbf10SJay
3199bae7d6eSJay  when(f3_req_is_mmio){
3209bae7d6eSJay    f3_data(0) := f3_mmio_data(15, 0)
3219bae7d6eSJay    f3_data(1) := f3_mmio_data(31, 16)
3229bae7d6eSJay  }
3239bae7d6eSJay
324a37fbf10SJay  when(fromUncache.fire())    {f3_mmio_data   :=  fromUncache.bits.data}
325a37fbf10SJay
326a37fbf10SJay
327a37fbf10SJay  switch(mmio_state){
328a37fbf10SJay    is(mmio_idle){
3299bae7d6eSJay      when(f3_req_is_mmio){
330a37fbf10SJay        mmio_state :=  mmio_send_req
331a37fbf10SJay      }
332a37fbf10SJay    }
333a37fbf10SJay
334a37fbf10SJay    is(mmio_send_req){
335a37fbf10SJay      mmio_state :=  Mux(toUncache.fire(), mmio_w_resp, mmio_send_req )
336a37fbf10SJay    }
337a37fbf10SJay
338a37fbf10SJay    is(mmio_w_resp){
339a37fbf10SJay      when(fromUncache.fire()){
340a37fbf10SJay          val isRVC =  fromUncache.bits.data(1,0) =/= 3.U
341a37fbf10SJay          mmio_state :=  Mux(isRVC, mmio_resend , mmio_w_commit)
342a37fbf10SJay      }
343a37fbf10SJay    }
344a37fbf10SJay
345a37fbf10SJay    is(mmio_resend){
346a37fbf10SJay      mmio_state :=  Mux(toUncache.fire(), mmio_resend_w_resp, mmio_resend )
347a37fbf10SJay    }
348a37fbf10SJay
349a37fbf10SJay    is(mmio_resend_w_resp){
350a37fbf10SJay      when(fromUncache.fire()){
351a37fbf10SJay          mmio_state :=  mmio_w_commit
352a37fbf10SJay      }
353a37fbf10SJay    }
354a37fbf10SJay
355a37fbf10SJay    is(mmio_w_commit){
356a37fbf10SJay      when(mmio_has_commited){
357a37fbf10SJay          mmio_state  :=  mmio_idle
358a37fbf10SJay      }
359a37fbf10SJay    }
360a37fbf10SJay  }
361a37fbf10SJay
362167bcd01SJay  when(f3_ftq_flush_self || f3_ftq_flush_by_older)  {
3639bae7d6eSJay    mmio_state := mmio_idle
3649bae7d6eSJay    f3_mmio_data := 0.U
3659bae7d6eSJay  }
3669bae7d6eSJay
367a37fbf10SJay  toUncache.valid     :=  ((mmio_state === mmio_send_req) || (mmio_state === mmio_resend)) && f3_req_is_mmio
368a37fbf10SJay  toUncache.bits.addr := Mux((mmio_state === mmio_resend), f3_pAddrs(0) + 2.U, f3_pAddrs(0))
369a37fbf10SJay  fromUncache.ready   := true.B
370a37fbf10SJay
37109c6f1ddSLingrui98  preDecoderIn.instValid     :=  f3_valid && !f3_has_except
372a37fbf10SJay  preDecoderIn.data          :=  f3_data
37309c6f1ddSLingrui98  preDecoderIn.startAddr     :=  f3_ftq_req.startAddr
37409c6f1ddSLingrui98  preDecoderIn.fallThruAddr  :=  f3_ftq_req.fallThruAddr
37509c6f1ddSLingrui98  preDecoderIn.fallThruError :=  f3_ftq_req.fallThruError
37609c6f1ddSLingrui98  preDecoderIn.isDoubleLine  :=  f3_doubleLine
37709c6f1ddSLingrui98  preDecoderIn.ftqOffset     :=  f3_ftq_req.ftqOffset
37809c6f1ddSLingrui98  preDecoderIn.target        :=  f3_ftq_req.target
37909c6f1ddSLingrui98  preDecoderIn.oversize      :=  f3_ftq_req.oversize
38009c6f1ddSLingrui98  preDecoderIn.lastHalfMatch :=  f3_lastHalfMatch
38109c6f1ddSLingrui98  preDecoderIn.pageFault     :=  f3_except_pf
38209c6f1ddSLingrui98  preDecoderIn.accessFault   :=  f3_except_af
38372951335SLi Qianruo  preDecoderIn.mmio          :=  f3_mmio
38472951335SLi Qianruo  preDecoderIn.frontendTrigger := io.frontendTrigger
38572951335SLi Qianruo  preDecoderIn.csrTriggerEnable := io.csrTriggerEnable
38609c6f1ddSLingrui98
38709c6f1ddSLingrui98
38809c6f1ddSLingrui98  // TODO: What if next packet does not match?
38909c6f1ddSLingrui98  when (f3_flush) {
39009c6f1ddSLingrui98    f3_lastHalf.valid := false.B
39109c6f1ddSLingrui98  }.elsewhen (io.toIbuffer.fire()) {
39209c6f1ddSLingrui98    f3_lastHalf.valid := preDecoderOut.hasLastHalf
39309c6f1ddSLingrui98    f3_lastHalf.middlePC := preDecoderOut.realEndPC
39409c6f1ddSLingrui98  }
39509c6f1ddSLingrui98
39609c6f1ddSLingrui98  val f3_predecode_range = VecInit(preDecoderOut.pd.map(inst => inst.valid)).asUInt
3970be662e4SJay  val f3_mmio_range      = VecInit((0 until PredictWidth).map(i => if(i ==0) true.B else false.B))
39809c6f1ddSLingrui98
399a37fbf10SJay  io.toIbuffer.valid          := f3_valid && (!f3_req_is_mmio || f3_mmio_can_go)
40009c6f1ddSLingrui98  io.toIbuffer.bits.instrs    := preDecoderOut.instrs
401a37fbf10SJay  io.toIbuffer.bits.valid     := Mux(f3_req_is_mmio, f3_mmio_range.asUInt, f3_predecode_range & preDecoderOut.instrRange.asUInt)
40209c6f1ddSLingrui98  io.toIbuffer.bits.pd        := preDecoderOut.pd
40309c6f1ddSLingrui98  io.toIbuffer.bits.ftqPtr    := f3_ftq_req.ftqIdx
40409c6f1ddSLingrui98  io.toIbuffer.bits.pc        := preDecoderOut.pc
405a37fbf10SJay  io.toIbuffer.bits.ftqOffset.zipWithIndex.map{case(a, i) => a.bits := i.U; a.valid := preDecoderOut.takens(i) && !f3_req_is_mmio}
40609c6f1ddSLingrui98  io.toIbuffer.bits.foldpc    := preDecoderOut.pc.map(i => XORFold(i(VAddrBits-1,1), MemPredPCWidth))
40709c6f1ddSLingrui98  io.toIbuffer.bits.ipf       := preDecoderOut.pageFault
40809c6f1ddSLingrui98  io.toIbuffer.bits.acf       := preDecoderOut.accessFault
40909c6f1ddSLingrui98  io.toIbuffer.bits.crossPageIPFFix := preDecoderOut.crossPageIPF
41072951335SLi Qianruo  io.toIbuffer.bits.triggered := preDecoderOut.triggered
41109c6f1ddSLingrui98
41209c6f1ddSLingrui98  //Write back to Ftq
413a37fbf10SJay  val f3_cache_fetch = f3_valid && !(f2_fire && !f2_flush)
414a37fbf10SJay  val finishFetchMaskReg = RegNext(f3_cache_fetch)
415a37fbf10SJay
41609c6f1ddSLingrui98
4170be662e4SJay  val f3_mmio_missOffset = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)))
418a37fbf10SJay  f3_mmio_missOffset.valid := f3_req_is_mmio
4190be662e4SJay  f3_mmio_missOffset.bits  := 0.U
4200be662e4SJay
421a37fbf10SJay  toFtq.pdWb.valid           := (!finishFetchMaskReg && f3_valid && !f3_req_is_mmio) || (f3_mmio_req_commit && f3_mmio_use_seq_pc)
42209c6f1ddSLingrui98  toFtq.pdWb.bits.pc         := preDecoderOut.pc
42309c6f1ddSLingrui98  toFtq.pdWb.bits.pd         := preDecoderOut.pd
424a37fbf10SJay  toFtq.pdWb.bits.pd.zipWithIndex.map{case(instr,i) => instr.valid :=  Mux(f3_req_is_mmio, f3_mmio_range(i), f3_predecode_range(i))}
42509c6f1ddSLingrui98  toFtq.pdWb.bits.ftqIdx     := f3_ftq_req.ftqIdx
42609c6f1ddSLingrui98  toFtq.pdWb.bits.ftqOffset  := f3_ftq_req.ftqOffset.bits
427a37fbf10SJay  toFtq.pdWb.bits.misOffset  := Mux(f3_req_is_mmio, f3_mmio_missOffset, preDecoderOut.misOffset)
42809c6f1ddSLingrui98  toFtq.pdWb.bits.cfiOffset  := preDecoderOut.cfiOffset
429a37fbf10SJay  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)
43009c6f1ddSLingrui98  toFtq.pdWb.bits.jalTarget  := preDecoderOut.jalTarget
431a37fbf10SJay  toFtq.pdWb.bits.instrRange := Mux(f3_req_is_mmio, f3_mmio_range, preDecoderOut.instrRange)
43209c6f1ddSLingrui98
433a37fbf10SJay  val predecodeFlush     = preDecoderOut.misOffset.valid && f3_valid
43409c6f1ddSLingrui98  val predecodeFlushReg  = RegNext(predecodeFlush && !(f2_fire && !f2_flush))
43509c6f1ddSLingrui98
436*1d8f4dcbSJay
437*1d8f4dcbSJay  /** performance counter */
438*1d8f4dcbSJay  val f3_perf_info     = RegEnable(next = f2_perf_info, enable = f2_fire)
439*1d8f4dcbSJay  val f3_req_0    = io.toIbuffer.fire()
440*1d8f4dcbSJay  val f3_req_1    = io.toIbuffer.fire() && f3_doubleLine
441*1d8f4dcbSJay  val f3_hit_0    = io.toIbuffer.fire() && f3_perf_info.bank_hit(0)
442*1d8f4dcbSJay  val f3_hit_1    = io.toIbuffer.fire() && f3_doubleLine & f3_perf_info.bank_hit(1)
443*1d8f4dcbSJay  val f3_hit      = f3_perf_info.hit
444*1d8f4dcbSJay
445cd365d4cSrvcoresjw  val perfinfo = IO(new Bundle(){
446cd365d4cSrvcoresjw    val perfEvents = Output(new PerfEventsBundle(15))
447cd365d4cSrvcoresjw  })
448cd365d4cSrvcoresjw
449cd365d4cSrvcoresjw  val perfEvents = Seq(
450cd365d4cSrvcoresjw    ("frontendFlush                ", f3_redirect                                ),
451cd365d4cSrvcoresjw    ("ifu_req                      ", io.toIbuffer.fire()                        ),
452*1d8f4dcbSJay    ("ifu_miss                     ", io.toIbuffer.fire() && !f3_perf_info.hit   ),
453cd365d4cSrvcoresjw    ("ifu_req_cacheline_0          ", f3_req_0                                   ),
454cd365d4cSrvcoresjw    ("ifu_req_cacheline_1          ", f3_req_1                                   ),
455cd365d4cSrvcoresjw    ("ifu_req_cacheline_0_hit      ", f3_hit_1                                   ),
456cd365d4cSrvcoresjw    ("ifu_req_cacheline_1_hit      ", f3_hit_1                                   ),
457*1d8f4dcbSJay    ("only_0_hit                   ", f3_perf_info.only_0_hit       && io.toIbuffer.fire() ),
458*1d8f4dcbSJay    ("only_0_miss                  ", f3_perf_info.only_0_miss      && io.toIbuffer.fire() ),
459*1d8f4dcbSJay    ("hit_0_hit_1                  ", f3_perf_info.hit_0_hit_1      && io.toIbuffer.fire() ),
460*1d8f4dcbSJay    ("hit_0_miss_1                 ", f3_perf_info.hit_0_miss_1     && io.toIbuffer.fire() ),
461*1d8f4dcbSJay    ("miss_0_hit_1                 ", f3_perf_info.miss_0_hit_1     && io.toIbuffer.fire() ),
462*1d8f4dcbSJay    ("miss_0_miss_1                ", f3_perf_info.miss_0_miss_1    && io.toIbuffer.fire() ),
463cd365d4cSrvcoresjw    ("cross_line_block             ", io.toIbuffer.fire() && f3_situation(0)     ),
464cd365d4cSrvcoresjw    ("fall_through_is_cacheline_end", io.toIbuffer.fire() && f3_situation(1)     ),
465cd365d4cSrvcoresjw  )
466cd365d4cSrvcoresjw
467cd365d4cSrvcoresjw  for (((perf_out,(perf_name,perf)),i) <- perfinfo.perfEvents.perf_events.zip(perfEvents).zipWithIndex) {
468cd365d4cSrvcoresjw    perf_out.incr_step := RegNext(perf)
469cd365d4cSrvcoresjw  }
470f7c29b0aSJinYue
471a37fbf10SJay  f3_redirect := (!predecodeFlushReg && predecodeFlush && !f3_req_is_mmio) || (f3_mmio_req_commit && f3_mmio_use_seq_pc)
47209c6f1ddSLingrui98
473f7c29b0aSJinYue  XSPerfAccumulate("ifu_req",   io.toIbuffer.fire() )
474f7c29b0aSJinYue  XSPerfAccumulate("ifu_miss",  io.toIbuffer.fire() && !f3_hit )
475f7c29b0aSJinYue  XSPerfAccumulate("ifu_req_cacheline_0", f3_req_0  )
476f7c29b0aSJinYue  XSPerfAccumulate("ifu_req_cacheline_1", f3_req_1  )
477f7c29b0aSJinYue  XSPerfAccumulate("ifu_req_cacheline_0_hit",   f3_hit_0 )
478f7c29b0aSJinYue  XSPerfAccumulate("ifu_req_cacheline_1_hit",   f3_hit_1 )
47909c6f1ddSLingrui98  XSPerfAccumulate("frontendFlush",  f3_redirect )
480*1d8f4dcbSJay  XSPerfAccumulate("only_0_hit",      f3_perf_info.only_0_hit   && io.toIbuffer.fire()  )
481*1d8f4dcbSJay  XSPerfAccumulate("only_0_miss",     f3_perf_info.only_0_miss  && io.toIbuffer.fire()  )
482*1d8f4dcbSJay  XSPerfAccumulate("hit_0_hit_1",     f3_perf_info.hit_0_hit_1  && io.toIbuffer.fire()  )
483*1d8f4dcbSJay  XSPerfAccumulate("hit_0_miss_1",    f3_perf_info.hit_0_miss_1  && io.toIbuffer.fire()  )
484*1d8f4dcbSJay  XSPerfAccumulate("miss_0_hit_1",    f3_perf_info.miss_0_hit_1   && io.toIbuffer.fire() )
485*1d8f4dcbSJay  XSPerfAccumulate("miss_0_miss_1",   f3_perf_info.miss_0_miss_1 && io.toIbuffer.fire() )
486f7c29b0aSJinYue  XSPerfAccumulate("cross_line_block", io.toIbuffer.fire() && f3_situation(0) )
487f7c29b0aSJinYue  XSPerfAccumulate("fall_through_is_cacheline_end", io.toIbuffer.fire() && f3_situation(1) )
48809c6f1ddSLingrui98}
489