xref: /XiangShan/src/main/scala/xiangshan/frontend/IFU.scala (revision f320e0f01bd645f0a3045a8a740e60dd770734a9)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16
17package xiangshan.frontend
18
19import chipsalliance.rocketchip.config.Parameters
20import chisel3._
21import chisel3.util._
22import xiangshan._
23import utils._
24import xiangshan.cache._
25import xiangshan.cache.mmu.{TLB, TlbPtwIO}
26import chisel3.experimental.chiselName
27import freechips.rocketchip.tile.HasLazyRoCC
28import xiangshan.backend.ftq.FtqPtr
29import system.L1CacheErrorInfo
30
31trait HasInstrMMIOConst extends HasXSParameter with HasIFUConst{
32  def mmioBusWidth = 64
33  def mmioBusBytes = mmioBusWidth /8
34  def mmioBeats = FetchWidth * 4 * 8 / mmioBusWidth
35  def mmioMask  = VecInit(List.fill(PredictWidth)(true.B)).asUInt
36  def mmioBusAligned(pc :UInt): UInt = align(pc, mmioBusBytes)
37}
38
39trait HasIFUConst extends HasXSParameter {
40  val resetVector = 0x10000000L//TODO: set reset vec
41  def align(pc: UInt, bytes: Int): UInt = Cat(pc(VAddrBits-1, log2Ceil(bytes)), 0.U(log2Ceil(bytes).W))
42  val groupBytes = 64 // correspond to cache line size
43  val groupOffsetBits = log2Ceil(groupBytes)
44  val groupWidth = groupBytes / instBytes
45  val packetBytes = PredictWidth * instBytes
46  val packetOffsetBits = log2Ceil(packetBytes)
47  def offsetInPacket(pc: UInt) = pc(packetOffsetBits-1, instOffsetBits)
48  def packetIdx(pc: UInt) = pc(VAddrBits-1, log2Ceil(packetBytes))
49  def groupAligned(pc: UInt)  = align(pc, groupBytes)
50  def packetAligned(pc: UInt) = align(pc, packetBytes)
51  def mask(pc: UInt): UInt = ((~(0.U(PredictWidth.W))) << offsetInPacket(pc))(PredictWidth-1,0)
52  def snpc(pc: UInt): UInt = packetAligned(pc) + packetBytes.U
53
54  val enableGhistRepair = true
55  val IFUDebug = true
56}
57
58class GlobalHistory(implicit p: Parameters) extends XSBundle {
59  val predHist = UInt(HistoryLength.W)
60  def update(sawNTBr: Bool, takenOnBr: Bool, hist: UInt = predHist): GlobalHistory = {
61    val g = Wire(new GlobalHistory)
62    val shifted = takenOnBr || sawNTBr
63    g.predHist := Mux(shifted, (hist << 1) | takenOnBr.asUInt, hist)
64    g
65  }
66
67  final def === (that: GlobalHistory): Bool = {
68    predHist === that.predHist
69  }
70
71  final def =/= (that: GlobalHistory): Bool = !(this === that)
72
73  implicit val name = "IFU"
74  def debug(where: String) = XSDebug(p"[${where}_GlobalHistory] hist=${Binary(predHist)}\n")
75  // override def toString(): String = "histPtr=%d, sawNTBr=%d, takenOnBr=%d, saveHalfRVI=%d".format(histPtr, sawNTBr, takenOnBr, saveHalfRVI)
76}
77
78
79class IFUIO(implicit p: Parameters) extends XSBundle
80{
81  // to ibuffer
82  val fetchPacket = DecoupledIO(new FetchPacket)
83  // from backend
84  val redirect = Flipped(ValidIO(new Redirect))
85  val bp_ctrl = Input(new BPUCtrl)
86  val commitUpdate = Flipped(ValidIO(new FtqEntry))
87  val ftqEnqPtr = Input(new FtqPtr)
88  val ftqLeftOne = Input(Bool())
89  // to backend
90  val toFtq = DecoupledIO(new FtqEntry)
91  // to icache
92  val icacheMemGrant = Flipped(DecoupledIO(new L1plusCacheResp))
93  val fencei = Input(Bool())
94  // from icache
95  val icacheMemAcq = DecoupledIO(new L1plusCacheReq)
96  val l1plusFlush = Output(Bool())
97  val prefetchTrainReq = ValidIO(new IcacheMissReq)
98  val error = new L1CacheErrorInfo
99  // to tlb
100  val sfence = Input(new SfenceBundle)
101  val tlbCsr = Input(new TlbCsrBundle)
102  // from tlb
103  val ptw = new TlbPtwIO
104  // icache uncache
105  val mmio_acquire = DecoupledIO(new InsUncacheReq)
106  val mmio_grant  = Flipped(DecoupledIO(new InsUncacheResp))
107  val mmio_flush = Output(Bool())
108}
109
110class PrevHalfInstr(implicit p: Parameters) extends XSBundle {
111  val pc = UInt(VAddrBits.W)
112  val npc = UInt(VAddrBits.W)
113  val instr = UInt(16.W)
114  val ipf = Bool()
115}
116
117@chiselName
118class IFU(implicit p: Parameters) extends XSModule with HasIFUConst with HasCircularQueuePtrHelper
119{
120  val io = IO(new IFUIO)
121  val bpu = BPU(EnableBPU)
122  val icache = Module(new ICache)
123
124  io.ptw <> TLB(
125    in = Seq(icache.io.tlb),
126    sfence = io.sfence,
127    csr = io.tlbCsr,
128    width = 1,
129    isDtlb = false,
130    shouldBlock = true
131  )
132
133  val if2_redirect, if3_redirect, if4_redirect = WireInit(false.B)
134  val if1_flush, if2_flush, if3_flush, if4_flush = WireInit(false.B)
135
136  val icacheResp = icache.io.resp.bits
137
138  if4_flush := io.redirect.valid
139  if3_flush := if4_flush || if4_redirect
140  if2_flush := if3_flush || if3_redirect
141  if1_flush := if2_flush || if2_redirect
142
143  //********************** IF1 ****************************//
144  val if1_valid = !reset.asBool && GTimer() > 500.U
145  val if1_npc = WireInit(0.U(VAddrBits.W))
146  val if2_ready = WireInit(false.B)
147  val if2_valid = RegInit(init = false.B)
148  val if2_allReady = WireInit(if2_ready && icache.io.req.ready && bpu.io.in_ready)
149  val if1_fire = if1_valid &&  if2_allReady
150
151  val if1_gh, if2_gh, if3_gh, if4_gh = Wire(new GlobalHistory)
152  val if2_predicted_gh, if3_predicted_gh, if4_predicted_gh = Wire(new GlobalHistory)
153  val final_gh = RegInit(0.U.asTypeOf(new GlobalHistory))
154
155  //********************** IF2 ****************************//
156  val if2_allValid = if2_valid && icache.io.tlb.resp.valid
157  val if3_ready = WireInit(false.B)
158  val if2_fire = if2_allValid && if3_ready
159  val if2_pc = RegEnable(next = if1_npc, init = resetVector.U, enable = if1_fire)
160  val if2_snpc = snpc(if2_pc)
161  val if2_predHist = RegEnable(if1_gh.predHist, enable=if1_fire)
162  if2_ready := if3_ready && icache.io.tlb.resp.valid || !if2_valid
163  when (if1_fire)       { if2_valid := true.B }
164  .elsewhen (if2_flush) { if2_valid := false.B }
165  .elsewhen (if2_fire)  { if2_valid := false.B }
166
167  val npcGen = new PriorityMuxGenerator[UInt]
168  npcGen.register(true.B, RegNext(if1_npc), Some("stallPC"))
169  val if2_bp = bpu.io.out(0)
170
171  // if taken, bp_redirect should be true
172  // when taken on half RVI, we suppress this redirect signal
173
174  npcGen.register(if2_valid, Mux(if2_bp.taken, if2_bp.target, if2_snpc), Some("if2_target"))
175
176  if2_predicted_gh := if2_gh.update(if2_bp.hasNotTakenBrs, if2_bp.takenOnBr)
177
178  //********************** IF3 ****************************//
179  // if3 should wait for instructions resp to arrive
180  val if3_valid = RegInit(init = false.B)
181  val if4_ready = WireInit(false.B)
182  val if3_allValid = if3_valid && icache.io.resp.valid
183  val if3_fire = if3_allValid && if4_ready
184  val if3_pc = RegEnable(if2_pc, if2_fire)
185  val if3_snpc = RegEnable(if2_snpc, if2_fire)
186  val if3_predHist = RegEnable(if2_predHist, enable=if2_fire)
187  if3_ready := if4_ready && icache.io.resp.valid || !if3_valid
188  when (if3_flush) {
189    if3_valid := false.B
190  }.elsewhen (if2_fire && !if2_flush) {
191    if3_valid := true.B
192  }.elsewhen (if3_fire) {
193    if3_valid := false.B
194  }
195
196  val if3_bp = bpu.io.out(1)
197  if3_predicted_gh := if3_gh.update(if3_bp.hasNotTakenBrs, if3_bp.takenOnBr)
198
199
200  val prevHalfInstrReq = WireInit(0.U.asTypeOf(ValidUndirectioned(new PrevHalfInstr)))
201  // only valid when if4_fire
202  val hasPrevHalfInstrReq = prevHalfInstrReq.valid && HasCExtension.B
203
204  val if3_prevHalfInstr = RegInit(0.U.asTypeOf(ValidUndirectioned(new PrevHalfInstr)))
205
206  // 32-bit instr crosses 2 pages, and the higher 16-bit triggers page fault
207  val crossPageIPF = WireInit(false.B)
208
209  val if3_pendingPrevHalfInstr = if3_prevHalfInstr.valid && HasCExtension.B
210
211  // the previous half of RVI instruction waits until it meets its last half
212  val if3_prevHalfInstrMet = if3_pendingPrevHalfInstr && if3_prevHalfInstr.bits.npc === if3_pc && if3_valid
213  // set to invalid once consumed or redirect from backend
214  val if3_prevHalfConsumed = if3_prevHalfInstrMet && if3_fire
215  val if3_prevHalfFlush = if4_flush
216  when (if3_prevHalfFlush) {
217    if3_prevHalfInstr.valid := false.B
218  }.elsewhen (hasPrevHalfInstrReq) {
219    if3_prevHalfInstr.valid := true.B
220  }.elsewhen (if3_prevHalfConsumed) {
221    if3_prevHalfInstr.valid := false.B
222  }
223  when (hasPrevHalfInstrReq) {
224    if3_prevHalfInstr.bits := prevHalfInstrReq.bits
225  }
226  // when bp signal a redirect, we distinguish between taken and not taken
227  // if taken and saveHalfRVI is true, we do not redirect to the target
228
229  class IF3_PC_COMP extends XSModule {
230    val io = IO(new Bundle {
231      val if2_pc = Input(UInt(VAddrBits.W))
232      val pc     = Input(UInt(VAddrBits.W))
233      val if2_valid = Input(Bool())
234      val res = Output(Bool())
235    })
236    io.res := !io.if2_valid || io.if2_valid && io.if2_pc =/= io.pc
237  }
238  def if3_nextValidPCNotEquals(pc: UInt) = {
239    val comp = Module(new IF3_PC_COMP)
240    comp.io.if2_pc := if2_pc
241    comp.io.pc     := pc
242    comp.io.if2_valid := if2_valid
243    comp.io.res
244  }
245
246  val if3_prevHalfNotMetRedirect = if3_pendingPrevHalfInstr && !if3_prevHalfInstrMet && if3_nextValidPCNotEquals(if3_prevHalfInstr.bits.npc)
247  val if3_predTakenRedirect    = !if3_pendingPrevHalfInstr && if3_bp.taken && if3_nextValidPCNotEquals(if3_bp.target)
248  val if3_predNotTakenRedirect = !if3_pendingPrevHalfInstr && !if3_bp.taken && if3_nextValidPCNotEquals(if3_snpc)
249  // when pendingPrevHalfInstr, if3_GHInfo is set to the info of last prev half instr
250  // val if3_ghInfoNotIdenticalRedirect = !if3_pendingPrevHalfInstr && if3_GHInfo =/= if3_lastGHInfo && enableGhistRepair.B
251
252  if3_redirect := if3_valid && (
253                    // prevHalf does not match if3_pc and the next fetch packet is not snpc
254                    if3_prevHalfNotMetRedirect && HasCExtension.B ||
255                    // pred taken and next fetch packet is not the predicted target
256                    if3_predTakenRedirect ||
257                    // pred not taken and next fetch packet is not snpc
258                    if3_predNotTakenRedirect
259                    // GHInfo from last pred does not corresponds with this packet
260                    // if3_ghInfoNotIdenticalRedirect
261                  )
262
263  val if3_target = WireInit(if3_snpc)
264
265  if3_target := Mux1H(Seq((if3_prevHalfNotMetRedirect -> if3_prevHalfInstr.bits.npc),
266                          (if3_predTakenRedirect      -> if3_bp.target),
267                          (if3_predNotTakenRedirect   -> if3_snpc)))
268
269  npcGen.register(if3_redirect, if3_target, Some("if3_target"))
270
271
272  //********************** IF4 ****************************//
273  val ftqEnqBuf_ready = Wire(Bool())
274  val if4_ftqEnqPtr = Wire(new FtqPtr)
275  val if4_pd = RegEnable(icache.io.pd_out, if3_fire)
276  val if4_ipf = RegEnable(icacheResp.ipf || if3_prevHalfInstrMet && if3_prevHalfInstr.bits.ipf, if3_fire)
277  val if4_acf = RegEnable(icacheResp.acf, if3_fire)
278  val if4_crossPageIPF = RegEnable(crossPageIPF, if3_fire)
279  val if4_valid = RegInit(false.B)
280  val if4_fire = if4_valid && io.fetchPacket.ready && ftqEnqBuf_ready
281  val if4_pc = RegEnable(if3_pc, if3_fire)
282  val if4_snpc = RegEnable(if3_snpc, if3_fire)
283  // This is the real mask given from icache
284  val if4_mask = RegEnable(icacheResp.mask, if3_fire)
285
286
287  val if4_predHist = RegEnable(if3_predHist, enable=if3_fire)
288  // wait until prevHalfInstr written into reg
289  if4_ready := (io.fetchPacket.ready && !hasPrevHalfInstrReq && ftqEnqBuf_ready || !if4_valid) && GTimer() > 500.U
290  when (if4_flush) {
291    if4_valid := false.B
292  }.elsewhen (if3_fire && !if3_flush) {
293    if4_valid := Mux(if3_pendingPrevHalfInstr, if3_prevHalfInstrMet, true.B)
294  }.elsewhen (if4_fire) {
295    if4_valid := false.B
296  }
297
298  val if4_bp = Wire(new BranchPrediction)
299  if4_bp := bpu.io.out(2)
300
301  if4_predicted_gh := if4_gh.update(if4_bp.hasNotTakenBrs, if4_bp.takenOnBr)
302
303  def jal_offset(inst: UInt, rvc: Bool): SInt = {
304    Mux(rvc,
305      Cat(inst(12), inst(8), inst(10, 9), inst(6), inst(7), inst(2), inst(11), inst(5, 3), 0.U(1.W)).asSInt(),
306      Cat(inst(31), inst(19, 12), inst(20), inst(30, 21), 0.U(1.W)).asSInt()
307    )
308  }
309  def br_offset(inst: UInt, rvc: Bool): SInt = {
310    Mux(rvc,
311      Cat(inst(12), inst(6, 5), inst(2), inst(11, 10), inst(4, 3), 0.U(1.W)).asSInt,
312      Cat(inst(31), inst(7), inst(30, 25), inst(11, 8), 0.U(1.W)).asSInt()
313    )
314  }
315  val if4_instrs = if4_pd.instrs
316  val if4_jals = if4_bp.jalMask
317  val if4_jal_tgts = VecInit((0 until PredictWidth).map(i => (if4_pd.pc(i).asSInt + jal_offset(if4_instrs(i), if4_pd.pd(i).isRVC)).asUInt))
318  val if4_brs = if4_bp.brMask
319  val if4_br_tgts = VecInit((0 until PredictWidth).map(i => (if4_pd.pc(i).asSInt + br_offset(if4_instrs(i), if4_pd.pd(i).isRVC)).asUInt))
320  (0 until PredictWidth).foreach {i =>
321    when (if4_jals(i)) {
322      if4_bp.targets(i) := if4_jal_tgts(i)
323    }.elsewhen (if4_brs(i)) {
324      if4_bp.targets(i) := if4_br_tgts(i)
325    }
326  }
327
328  // we need this to tell BPU the prediction of prev half
329  // because the prediction is with the start of each inst
330  val if4_prevHalfInstr = RegInit(0.U.asTypeOf(ValidUndirectioned(new PrevHalfInstr)))
331  val if4_pendingPrevHalfInstr = if4_prevHalfInstr.valid && HasCExtension.B
332  val if4_prevHalfInstrMet = if4_pendingPrevHalfInstr && if4_valid
333  val if4_prevHalfConsumed = if4_prevHalfInstrMet && if4_fire
334  val if4_prevHalfFlush = if4_flush
335
336  when (if4_prevHalfFlush) {
337    if4_prevHalfInstr.valid := false.B
338  }.elsewhen (if3_prevHalfConsumed) {
339    if4_prevHalfInstr.valid := if3_prevHalfInstr.valid
340  }.elsewhen (if4_prevHalfConsumed) {
341    if4_prevHalfInstr.valid := false.B
342  }
343
344  when (if3_prevHalfConsumed) {
345    if4_prevHalfInstr.bits := if3_prevHalfInstr.bits
346  }
347
348  prevHalfInstrReq.valid := if4_fire && if4_bp.saveHalfRVI && HasCExtension.B
349
350  // // this is result of the last half RVI
351  prevHalfInstrReq.bits.pc := if4_pd.pc(PredictWidth-1)
352  prevHalfInstrReq.bits.npc := snpc(if4_pc)
353  prevHalfInstrReq.bits.instr := if4_pd.instrs(PredictWidth-1)(15, 0)
354  prevHalfInstrReq.bits.ipf := if4_ipf
355
356  class IF4_PC_COMP extends XSModule {
357    val io = IO(new Bundle {
358      val if2_pc = Input(UInt(VAddrBits.W))
359      val if3_pc = Input(UInt(VAddrBits.W))
360      val pc     = Input(UInt(VAddrBits.W))
361      val if2_valid = Input(Bool())
362      val if3_valid = Input(Bool())
363      val res = Output(Bool())
364    })
365    io.res := io.if3_valid  && io.if3_pc =/= io.pc ||
366              !io.if3_valid && (io.if2_valid && io.if2_pc =/= io.pc) ||
367              !io.if3_valid && !io.if2_valid
368  }
369  def if4_nextValidPCNotEquals(pc: UInt) = {
370    val comp = Module(new IF4_PC_COMP)
371    comp.io.if2_pc := if2_pc
372    comp.io.if3_pc := if3_pc
373    comp.io.pc     := pc
374    comp.io.if2_valid := if2_valid
375    comp.io.if3_valid := if3_valid
376    comp.io.res
377  }
378
379  val if4_prevHalfNextNotMet = hasPrevHalfInstrReq && if4_nextValidPCNotEquals(prevHalfInstrReq.bits.pc+2.U)
380  val if4_predTakenRedirect = if4_bp.taken && if4_nextValidPCNotEquals(if4_bp.target)
381  val if4_predNotTakenRedirect = !if4_bp.taken && if4_nextValidPCNotEquals(if4_snpc)
382  // val if4_ghInfoNotIdenticalRedirect = if4_GHInfo =/= if4_lastGHInfo && enableGhistRepair.B
383
384  if4_redirect := if4_valid && (
385                    // when if4 has a lastHalfRVI, but the next fetch packet is not snpc
386                    // if4_prevHalfNextNotMet ||
387                    // when if4 preds taken, but the pc of next fetch packet is not the target
388                    if4_predTakenRedirect ||
389                    // when if4 preds not taken, but the pc of next fetch packet is not snpc
390                    if4_predNotTakenRedirect
391                    // GHInfo from last pred does not corresponds with this packet
392                    // if4_ghInfoNotIdenticalRedirect
393                  )
394
395  val if4_target = WireInit(if4_snpc)
396
397  if4_target := Mux(if4_bp.taken, if4_bp.target, if4_snpc)
398
399  npcGen.register(if4_redirect, if4_target, Some("if4_target"))
400
401  when (if4_fire) {
402    final_gh := if4_predicted_gh
403  }
404  if4_gh := final_gh
405  if3_gh := Mux(if4_valid, if4_predicted_gh, if4_gh)
406  if2_gh := Mux(if3_valid && !if3_flush, if3_predicted_gh, if3_gh)
407  if1_gh := Mux(if2_valid && !if2_flush, if2_predicted_gh, if2_gh)
408
409  // ***************** Ftq enq buffer ********************
410  val toFtqBuf = Wire(new FtqEntry)
411  val ftqEnqBuf = RegEnable(toFtqBuf, enable=if4_fire)
412  val ftqEnqBuf_valid = RegInit(false.B)
413  val ftqLeftOne = WireInit(false.B) // TODO: to be replaced
414  ftqEnqBuf_ready := io.toFtq.ready && !(io.ftqLeftOne && ftqEnqBuf_valid)
415  if4_ftqEnqPtr := Mux(ftqEnqBuf_valid, io.ftqEnqPtr+1.U, io.ftqEnqPtr)
416  when (io.redirect.valid)  { ftqEnqBuf_valid := false.B }
417  .elsewhen (if4_fire)      { ftqEnqBuf_valid := true.B }
418  .elsewhen (io.toFtq.fire) { ftqEnqBuf_valid := false.B }
419
420  io.toFtq.valid := ftqEnqBuf_valid
421  io.toFtq.bits  := ftqEnqBuf
422
423  toFtqBuf := DontCare
424  toFtqBuf.ftqPC    := if4_pc
425  toFtqBuf.lastPacketPC.valid := if4_pendingPrevHalfInstr
426  toFtqBuf.lastPacketPC.bits  := if4_prevHalfInstr.bits.pc
427
428  toFtqBuf.hist     := final_gh
429  toFtqBuf.predHist := if4_predHist.asTypeOf(new GlobalHistory)
430  toFtqBuf.rasSp    := bpu.io.brInfo.rasSp
431  toFtqBuf.rasTop   := bpu.io.brInfo.rasTop
432  toFtqBuf.specCnt  := bpu.io.brInfo.specCnt
433  toFtqBuf.metas    := bpu.io.brInfo.metas
434
435  // For perf counters
436  toFtqBuf.pd    := if4_pd.pd
437
438
439  val if4_jmpIdx = WireInit(if4_bp.jmpIdx)
440  val if4_taken = WireInit(if4_bp.taken)
441  val if4_real_valids = if4_pd.mask &
442    (Fill(PredictWidth, !if4_taken) |
443      (Fill(PredictWidth, 1.U(1.W)) >> (~if4_jmpIdx)))
444
445  val cfiIsCall = if4_pd.pd(if4_jmpIdx).isCall
446  val cfiIsRet  = if4_pd.pd(if4_jmpIdx).isRet
447  val cfiIsRVC  = if4_pd.pd(if4_jmpIdx).isRVC
448  val cfiIsJalr = if4_pd.pd(if4_jmpIdx).isJalr
449  toFtqBuf.cfiIsCall := cfiIsCall
450  toFtqBuf.cfiIsRet  := cfiIsRet
451  toFtqBuf.cfiIsJalr := cfiIsJalr
452  toFtqBuf.cfiIsRVC  := cfiIsRVC
453  toFtqBuf.cfiIndex.valid := if4_taken
454  toFtqBuf.cfiIndex.bits  := if4_jmpIdx
455
456  toFtqBuf.br_mask   := if4_bp.brMask.asTypeOf(Vec(PredictWidth, Bool()))
457  toFtqBuf.rvc_mask  := VecInit(if4_pd.pd.map(_.isRVC))
458  toFtqBuf.valids    := if4_real_valids.asTypeOf(Vec(PredictWidth, Bool()))
459  toFtqBuf.target := Mux(if4_taken, if4_target, if4_snpc)
460
461
462
463  val r = io.redirect
464  val cfiUpdate = io.redirect.bits.cfiUpdate
465  when (r.valid) {
466    val isMisPred = r.bits.level === 0.U
467    val b = cfiUpdate
468    val oldGh = b.hist
469    val sawNTBr = b.sawNotTakenBranch
470    val isBr = b.pd.isBr
471    val taken = Mux(isMisPred, b.taken, b.predTaken)
472    val updatedGh = oldGh.update(sawNTBr || isBr, isBr && taken)
473    final_gh := updatedGh
474    if1_gh := updatedGh
475  }
476
477  npcGen.register(io.redirect.valid, io.redirect.bits.cfiUpdate.target, Some("backend_redirect"))
478  npcGen.register(RegNext(reset.asBool) && !reset.asBool, resetVector.U(VAddrBits.W), Some("reset_vector"))
479
480  if1_npc := npcGen()
481
482
483  icache.io.req.valid := if1_fire
484  icache.io.resp.ready := if4_ready
485  icache.io.req.bits.addr := if1_npc
486  icache.io.req.bits.mask := mask(if1_npc)
487  icache.io.flush := Cat(if3_flush, if2_flush)
488  icache.io.mem_grant <> io.icacheMemGrant
489  icache.io.fencei := io.fencei
490  icache.io.prev.valid := if3_prevHalfInstrMet
491  icache.io.prev.bits := if3_prevHalfInstr.bits.instr
492  icache.io.prev_ipf := if3_prevHalfInstr.bits.ipf
493  icache.io.prev_pc := if3_prevHalfInstr.bits.pc
494  icache.io.mmio_acquire <> io.mmio_acquire
495  icache.io.mmio_grant <> io.mmio_grant
496  icache.io.mmio_flush <> io.mmio_flush
497  io.icacheMemAcq <> icache.io.mem_acquire
498  io.l1plusFlush := icache.io.l1plusflush
499  io.prefetchTrainReq := icache.io.prefetchTrainReq
500  io.error <> icache.io.error
501
502  bpu.io.ctrl := RegNext(io.bp_ctrl)
503  bpu.io.commit <> io.commitUpdate
504  bpu.io.redirect <> io.redirect
505
506  bpu.io.inFire(0) := if1_fire
507  bpu.io.inFire(1) := if2_fire
508  bpu.io.inFire(2) := if3_fire
509  bpu.io.inFire(3) := if4_fire
510  bpu.io.in.pc := if1_npc
511  bpu.io.in.hist := if1_gh.asUInt
512  bpu.io.in.inMask := mask(if1_npc)
513  bpu.io.predecode.mask := if4_pd.mask
514  bpu.io.predecode.lastHalf := if4_pd.lastHalf
515  bpu.io.predecode.pd := if4_pd.pd
516  bpu.io.predecode.hasLastHalfRVI := if4_prevHalfInstrMet
517
518
519  when (if3_prevHalfInstrMet && icacheResp.ipf && !if3_prevHalfInstr.bits.ipf) {
520    crossPageIPF := true.B // higher 16 bits page fault
521  }
522
523  val fetchPacketValid = if4_valid && !io.redirect.valid && ftqEnqBuf_ready
524  val fetchPacketWire = Wire(new FetchPacket)
525
526  fetchPacketWire.mask := if4_real_valids
527  //RVC expand
528  val expandedInstrs = Wire(Vec(PredictWidth, UInt(32.W)))
529  for(i <- 0 until PredictWidth){
530      val expander = Module(new RVCExpander)
531      expander.io.in := if4_pd.instrs(i)
532      expandedInstrs(i) := expander.io.out.bits
533  }
534  fetchPacketWire.instrs := expandedInstrs
535
536  fetchPacketWire.pc := if4_pd.pc
537  fetchPacketWire.foldpc := if4_pd.pc.map(i => XORFold(i(VAddrBits-1,1), MemPredPCWidth))
538
539  fetchPacketWire.pdmask := if4_pd.mask
540  fetchPacketWire.pd := if4_pd.pd
541  fetchPacketWire.ipf := if4_ipf
542  fetchPacketWire.acf := if4_acf
543  fetchPacketWire.crossPageIPFFix := if4_crossPageIPF
544  fetchPacketWire.ftqPtr := if4_ftqEnqPtr
545
546  // predTaken Vec
547  fetchPacketWire.pred_taken := if4_bp.takens
548
549  io.fetchPacket.bits := fetchPacketWire
550  io.fetchPacket.valid := fetchPacketValid
551
552  if (!env.FPGAPlatform && env.EnablePerfDebug) {
553    val predictor_s3 = RegEnable(Mux(if3_redirect, 1.U(log2Up(4).W), 0.U(log2Up(4).W)), if3_fire)
554    val predictor_s4 = Mux(if4_redirect, 2.U, predictor_s3)
555    val predictor = predictor_s4
556    toFtqBuf.metas.map(_.predictor := predictor)
557
558    toFtqBuf.metas.zipWithIndex.foreach{ case(x,i) =>
559      x.predictor := predictor
560
561      x.ubtbAns := bpu.io.brInfo.metas(i).ubtbAns
562      x.btbAns := bpu.io.brInfo.metas(i).btbAns
563      x.tageAns := bpu.io.brInfo.metas(i).tageAns
564      x.rasAns := bpu.io.brInfo.metas(i).rasAns // Is this right?
565      x.loopAns := bpu.io.brInfo.metas(i).loopAns
566    }
567  }
568
569  // TODO: perfs
570  // frontend redirect from each stage
571  XSPerfAccumulate("if2_redirect", if2_valid && if2_bp.taken && !if2_flush)
572  XSPerfAccumulate("if2_redirect_fired", if2_fire && if2_bp.taken && !if2_flush)
573  XSPerfAccumulate("if3_redirect", if3_valid && if3_redirect && !if3_flush)
574  XSPerfAccumulate("if3_redirect_fired", if3_fire && if3_redirect && !if3_flush)
575  XSPerfAccumulate("if4_redirect", if4_valid && if4_redirect && !if4_flush)
576  XSPerfAccumulate("if4_redirect_fired", if4_fire && if4_redirect && !if4_flush)
577
578  XSPerfAccumulate("if1_total_stall", !if2_allReady && if1_valid)
579  XSPerfAccumulate("if1_stall_from_icache_req", !icache.io.req.ready && if1_valid)
580  XSPerfAccumulate("if1_stall_from_if2", !if2_ready && if1_valid)
581  XSPerfAccumulate("if1_stall_from_bpu", !bpu.io.in_ready && if1_valid)
582  XSPerfAccumulate("itlb_stall", if2_valid && if3_ready && !icache.io.tlb.resp.valid)
583  XSPerfAccumulate("icache_resp_stall", if3_valid && if4_ready && !icache.io.resp.valid)
584  XSPerfAccumulate("if4_stall", if4_valid && !if4_fire)
585  XSPerfAccumulate("if4_stall_ibuffer", if4_valid && !io.fetchPacket.ready && ftqEnqBuf_ready)
586  XSPerfAccumulate("if4_stall_ftq", if4_valid && io.fetchPacket.ready && !ftqEnqBuf_ready)
587
588  XSPerfAccumulate("if3_prevHalfConsumed", if3_prevHalfConsumed)
589  XSPerfAccumulate("if4_prevHalfConsumed", if4_prevHalfConsumed)
590
591
592  // debug info
593  if (IFUDebug) {
594    XSDebug(RegNext(reset.asBool) && !reset.asBool, "Reseting...\n")
595    XSDebug(icache.io.flush(0).asBool, "Flush icache stage2...\n")
596    XSDebug(icache.io.flush(1).asBool, "Flush icache stage3...\n")
597    XSDebug(io.redirect.valid, p"Redirect from backend! target=${Hexadecimal(io.redirect.bits.cfiUpdate.target)}\n")
598
599    XSDebug("[IF1] v=%d      fire=%d             flush=%d pc=%x mask=%b\n", if1_valid, if1_fire, if1_flush, if1_npc, mask(if1_npc))
600    XSDebug("[IF2] v=%d r=%d fire=%d redirect=%d flush=%d pc=%x snpc=%x\n", if2_valid, if2_ready, if2_fire, if2_redirect, if2_flush, if2_pc, if2_snpc)
601    XSDebug("[IF3] v=%d r=%d fire=%d redirect=%d flush=%d pc=%x crossPageIPF=%d sawNTBrs=%d\n", if3_valid, if3_ready, if3_fire, if3_redirect, if3_flush, if3_pc, crossPageIPF, if3_bp.hasNotTakenBrs)
602    XSDebug("[IF4] v=%d r=%d fire=%d redirect=%d flush=%d pc=%x crossPageIPF=%d sawNTBrs=%d\n", if4_valid, if4_ready, if4_fire, if4_redirect, if4_flush, if4_pc, if4_crossPageIPF, if4_bp.hasNotTakenBrs)
603    XSDebug("[IF1][icacheReq] v=%d r=%d addr=%x\n", icache.io.req.valid, icache.io.req.ready, icache.io.req.bits.addr)
604    XSDebug("[IF1][ghr] hist=%b\n", if1_gh.asUInt)
605
606    XSDebug("[IF2][bp] taken=%d jmpIdx=%d hasNTBrs=%d target=%x saveHalfRVI=%d\n\n", if2_bp.taken, if2_bp.jmpIdx, if2_bp.hasNotTakenBrs, if2_bp.target, if2_bp.saveHalfRVI)
607    if2_gh.debug("if2")
608
609    XSDebug("[IF3][icacheResp] v=%d r=%d pc=%x mask=%b\n", icache.io.resp.valid, icache.io.resp.ready, icache.io.resp.bits.pc, icache.io.resp.bits.mask)
610    XSDebug("[IF3][bp] taken=%d jmpIdx=%d hasNTBrs=%d target=%x saveHalfRVI=%d\n", if3_bp.taken, if3_bp.jmpIdx, if3_bp.hasNotTakenBrs, if3_bp.target, if3_bp.saveHalfRVI)
611    XSDebug("[IF3][redirect]: v=%d, prevNMet=%d, predT=%d, predNT=%d\n", if3_redirect, if3_prevHalfNotMetRedirect, if3_predTakenRedirect, if3_predNotTakenRedirect)
612    // XSDebug("[IF3][prevHalfInstr] v=%d redirect=%d fetchpc=%x idx=%d tgt=%x taken=%d instr=%x\n\n",
613    //   prev_half_valid, prev_half_redirect, prev_half_fetchpc, prev_half_idx, prev_half_tgt, prev_half_taken, prev_half_instr)
614    XSDebug("[IF3][if3_prevHalfInstr] v=%d pc=%x npc=%x  instr=%x ipf=%d\n\n",
615    if3_prevHalfInstr.valid, if3_prevHalfInstr.bits.pc, if3_prevHalfInstr.bits.npc, if3_prevHalfInstr.bits.instr, if3_prevHalfInstr.bits.ipf)
616    if3_gh.debug("if3")
617
618    XSDebug("[IF4][predecode] mask=%b\n", if4_pd.mask)
619    XSDebug("[IF4][snpc]: %x, realMask=%b\n", if4_snpc, if4_mask)
620    XSDebug("[IF4][bp] taken=%d jmpIdx=%d hasNTBrs=%d target=%x saveHalfRVI=%d\n", if4_bp.taken, if4_bp.jmpIdx, if4_bp.hasNotTakenBrs, if4_bp.target, if4_bp.saveHalfRVI)
621    XSDebug("[IF4][redirect]: v=%d, prevNotMet=%d, predT=%d, predNT=%d\n", if4_redirect, if4_prevHalfNextNotMet, if4_predTakenRedirect, if4_predNotTakenRedirect)
622    XSDebug(if4_pd.pd(if4_bp.jmpIdx).isJal && if4_bp.taken, "[IF4] cfi is jal!  instr=%x target=%x\n", if4_instrs(if4_bp.jmpIdx), if4_jal_tgts(if4_bp.jmpIdx))
623    XSDebug("[IF4][ prevHalfInstrReq] v=%d pc=%x npc=%x instr=%x ipf=%d\n",
624      prevHalfInstrReq.valid, prevHalfInstrReq.bits.pc, prevHalfInstrReq.bits.npc, prevHalfInstrReq.bits.instr, prevHalfInstrReq.bits.ipf)
625    XSDebug("[IF4][if4_prevHalfInstr] v=%d pc=%x npc=%x instr=%x ipf=%d\n",
626      if4_prevHalfInstr.valid, if4_prevHalfInstr.bits.pc, if4_prevHalfInstr.bits.npc, if4_prevHalfInstr.bits.instr, if4_prevHalfInstr.bits.ipf)
627    if4_gh.debug("if4")
628    XSDebug(io.fetchPacket.fire(), "[IF4][fetchPacket] v=%d r=%d mask=%b ipf=%d acf=%d crossPageIPF=%d\n",
629      io.fetchPacket.valid, io.fetchPacket.ready, io.fetchPacket.bits.mask, io.fetchPacket.bits.ipf, io.fetchPacket.bits.acf, io.fetchPacket.bits.crossPageIPFFix)
630    for (i <- 0 until PredictWidth) {
631      XSDebug(io.fetchPacket.fire(), "[IF4][fetchPacket] %b %x pc=%x pd: rvc=%d brType=%b call=%d ret=%d\n",
632        io.fetchPacket.bits.mask(i),
633        io.fetchPacket.bits.instrs(i),
634        io.fetchPacket.bits.pc(i),
635        io.fetchPacket.bits.pd(i).isRVC,
636        io.fetchPacket.bits.pd(i).brType,
637        io.fetchPacket.bits.pd(i).isCall,
638        io.fetchPacket.bits.pd(i).isRet
639      )
640    }
641    val b = ftqEnqBuf
642    XSDebug("[FtqEnqBuf] v=%d r=%d pc=%x cfiIndex(%d)=%d cfiIsCall=%d cfiIsRet=%d cfiIsJalr=%d cfiIsRVC=%d\n",
643      ftqEnqBuf_valid, ftqEnqBuf_ready, b.ftqPC, b.cfiIndex.valid, b.cfiIndex.bits, b.cfiIsCall, b.cfiIsRet, b.cfiIsJalr, b.cfiIsRVC)
644    XSDebug("[FtqEnqBuf] valids=%b br_mask=%b rvc_mask=%b hist=%x predHist=%x rasSp=%d rasTopAddr=%x rasTopCtr=%d\n",
645      b.valids.asUInt, b.br_mask.asUInt, b.rvc_mask.asUInt, b.hist.asUInt, b.predHist.asUInt, b.rasSp, b.rasTop.retAddr, b.rasTop.ctr)
646    XSDebug("[ToFTQ] v=%d r=%d leftOne=%d ptr=%d\n", io.toFtq.valid, io.toFtq.ready, io.ftqLeftOne, io.ftqEnqPtr.value)
647  }
648
649}
650