xref: /XiangShan/src/main/scala/xiangshan/frontend/IFU.scala (revision a273862e37f1d43bee748f2a6353320a2f52f6f4)
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 xiangshan.cache._
24import xiangshan.cache.mmu._
25import chisel3.experimental.verification
26import utils._
27import xiangshan.backend.fu.{PMPReqBundle, PMPRespBundle}
28
29trait HasInstrMMIOConst extends HasXSParameter with HasIFUConst{
30  def mmioBusWidth = 64
31  def mmioBusBytes = mmioBusWidth /8
32  def maxInstrLen = 32
33}
34
35trait HasIFUConst extends HasXSParameter {
36  def align(pc: UInt, bytes: Int): UInt = Cat(pc(VAddrBits-1, log2Ceil(bytes)), 0.U(log2Ceil(bytes).W))
37  // def groupAligned(pc: UInt)  = align(pc, groupBytes)
38  // def packetAligned(pc: UInt) = align(pc, packetBytes)
39}
40
41class IfuToFtqIO(implicit p:Parameters) extends XSBundle {
42  val pdWb = Valid(new PredecodeWritebackBundle)
43}
44
45class FtqInterface(implicit p: Parameters) extends XSBundle {
46  val fromFtq = Flipped(new FtqToIfuIO)
47  val toFtq   = new IfuToFtqIO
48}
49
50class UncacheInterface(implicit p: Parameters) extends XSBundle {
51  val fromUncache = Flipped(DecoupledIO(new InsUncacheResp))
52  val toUncache   = DecoupledIO( new InsUncacheReq )
53}
54
55class ICacheInterface(implicit p: Parameters) extends XSBundle {
56  val toIMeta       = Decoupled(new ICacheReadBundle)
57  val toIData       = Decoupled(new ICacheReadBundle)
58  val toMissQueue   = Vec(2,Decoupled(new ICacheMissReq))
59  val fromIMeta     = Input(new ICacheMetaRespBundle)
60  val fromIData     = Input(new ICacheDataRespBundle)
61  val fromMissQueue = Vec(2,Flipped(Decoupled(new ICacheMissResp)))
62}
63
64class NewIFUIO(implicit p: Parameters) extends XSBundle {
65  val ftqInter        = new FtqInterface
66  val icacheInter     = new ICacheInterface
67  val toIbuffer       = Decoupled(new FetchToIBuffer)
68  val iTLBInter       = Vec(2, new BlockTlbRequestIO)
69  val uncacheInter   =  new UncacheInterface
70  val pmp             = Vec(2, new Bundle {
71    val req = Valid(new PMPReqBundle())
72    val resp = Flipped(new PMPRespBundle())
73  })
74}
75
76// record the situation in which fallThruAddr falls into
77// the middle of an RVI inst
78class LastHalfInfo(implicit p: Parameters) extends XSBundle {
79  val valid = Bool()
80  val middlePC = UInt(VAddrBits.W)
81  def matchThisBlock(startAddr: UInt) = valid && middlePC === startAddr
82}
83
84class IfuToPreDecode(implicit p: Parameters) extends XSBundle {
85  val data          = if(HasCExtension) Vec(PredictWidth + 1, UInt(16.W)) else Vec(PredictWidth, UInt(32.W))
86  val startAddr     = UInt(VAddrBits.W)
87  val fallThruAddr  = UInt(VAddrBits.W)
88  val fallThruError = Bool()
89  val isDoubleLine  = Bool()
90  val ftqOffset     = Valid(UInt(log2Ceil(PredictWidth).W))
91  val target        = UInt(VAddrBits.W)
92  val pageFault     = Vec(2, Bool())
93  val accessFault   = Vec(2, Bool())
94  val instValid     = Bool()
95  val lastHalfMatch = Bool()
96  val oversize      = Bool()
97  val mmio = Bool()
98}
99
100class NewIFU(implicit p: Parameters) extends XSModule with HasICacheParameters
101{
102  println(s"icache ways: ${nWays} sets:${nSets}")
103  val io = IO(new NewIFUIO)
104  val (toFtq, fromFtq)    = (io.ftqInter.toFtq, io.ftqInter.fromFtq)
105  val (toMeta, toData, meta_resp, data_resp) =  (io.icacheInter.toIMeta, io.icacheInter.toIData, io.icacheInter.fromIMeta, io.icacheInter.fromIData)
106  val (toMissQueue, fromMissQueue) = (io.icacheInter.toMissQueue, io.icacheInter.fromMissQueue)
107  val (toUncache, fromUncache) = (io.uncacheInter.toUncache , io.uncacheInter.fromUncache)
108  val (toITLB, fromITLB) = (VecInit(io.iTLBInter.map(_.req)), VecInit(io.iTLBInter.map(_.resp)))
109  val fromPMP = io.pmp.map(_.resp)
110
111  def isCrossLineReq(start: UInt, end: UInt): Bool = start(blockOffBits) ^ end(blockOffBits)
112
113  def isLastInCacheline(fallThruAddr: UInt): Bool = fallThruAddr(blockOffBits - 1, 1) === 0.U
114
115
116  //---------------------------------------------
117  //  Fetch Stage 1 :
118  //  * Send req to ICache Meta/Data
119  //  * Check whether need 2 line fetch
120  //---------------------------------------------
121
122  val f0_valid                             = fromFtq.req.valid
123  val f0_ftq_req                           = fromFtq.req.bits
124  val f0_situation                         = VecInit(Seq(isCrossLineReq(f0_ftq_req.startAddr, f0_ftq_req.fallThruAddr), isLastInCacheline(f0_ftq_req.fallThruAddr)))
125  val f0_doubleLine                        = f0_situation(0) || f0_situation(1)
126  val f0_vSetIdx                           = VecInit(get_idx((f0_ftq_req.startAddr)), get_idx(f0_ftq_req.fallThruAddr))
127  val f0_fire                              = fromFtq.req.fire()
128
129  val f0_flush, f1_flush, f2_flush, f3_flush = WireInit(false.B)
130  val from_bpu_f0_flush, from_bpu_f1_flush, from_bpu_f2_flush, from_bpu_f3_flush = WireInit(false.B)
131
132  from_bpu_f0_flush := fromFtq.flushFromBpu.shouldFlushByStage2(f0_ftq_req.ftqIdx) ||
133                       fromFtq.flushFromBpu.shouldFlushByStage3(f0_ftq_req.ftqIdx)
134
135  val f3_redirect = WireInit(false.B)
136  f3_flush := fromFtq.redirect.valid
137  f2_flush := f3_flush || f3_redirect
138  f1_flush := f2_flush || from_bpu_f1_flush
139  f0_flush := f1_flush || from_bpu_f0_flush
140
141  val f1_ready, f2_ready, f3_ready         = WireInit(false.B)
142
143  //fetch: send addr to Meta/TLB and Data simultaneously
144  val fetch_req = List(toMeta, toData)
145  for(i <- 0 until 2) {
146    fetch_req(i).valid := f0_fire
147    fetch_req(i).bits.isDoubleLine := f0_doubleLine
148    fetch_req(i).bits.vSetIdx := f0_vSetIdx
149  }
150
151  fromFtq.req.ready := fetch_req(0).ready && fetch_req(1).ready && f1_ready && GTimer() > 500.U
152
153  XSPerfAccumulate("ifu_bubble_ftq_not_valid",   !f0_valid )
154  XSPerfAccumulate("ifu_bubble_pipe_stall",    f0_valid && fetch_req(0).ready && fetch_req(1).ready && !f1_ready )
155  XSPerfAccumulate("ifu_bubble_sram_0_busy",   f0_valid && !fetch_req(0).ready  )
156  XSPerfAccumulate("ifu_bubble_sram_1_busy",   f0_valid && !fetch_req(1).ready  )
157
158  //---------------------------------------------
159  //  Fetch Stage 2 :
160  //  * Send req to ITLB and TLB Response (Get Paddr)
161  //  * ICache Response (Get Meta and Data)
162  //  * Hit Check (Generate hit signal and hit vector)
163  //  * Get victim way
164  //---------------------------------------------
165
166  //TODO: handle fetch exceptions
167
168  val tlbRespAllValid = WireInit(false.B)
169
170  val f1_valid      = RegInit(false.B)
171  val f1_ftq_req    = RegEnable(next = f0_ftq_req,    enable=f0_fire)
172  val f1_situation  = RegEnable(next = f0_situation,  enable=f0_fire)
173  val f1_doubleLine = RegEnable(next = f0_doubleLine, enable=f0_fire)
174  val f1_vSetIdx    = RegEnable(next = f0_vSetIdx,    enable=f0_fire)
175  val f1_fire       = f1_valid && tlbRespAllValid && f2_ready
176
177  f1_ready := f2_ready && tlbRespAllValid || !f1_valid
178
179  from_bpu_f1_flush := fromFtq.flushFromBpu.shouldFlushByStage3(f1_ftq_req.ftqIdx)
180
181  val preDecoder      = Module(new PreDecode)
182  val (preDecoderIn, preDecoderOut)   = (preDecoder.io.in, preDecoder.io.out)
183
184  //flush generate and to Ftq
185  val predecodeOutValid = WireInit(false.B)
186
187  when(f1_flush)                  {f1_valid  := false.B}
188  .elsewhen(f0_fire && !f0_flush) {f1_valid  := true.B}
189  .elsewhen(f1_fire)              {f1_valid  := false.B}
190
191  toITLB(0).valid         := f1_valid
192  toITLB(0).bits.size     := 3.U // TODO: fix the size
193  toITLB(0).bits.vaddr    := align(f1_ftq_req.startAddr, blockBytes)
194  toITLB(0).bits.debug.pc := align(f1_ftq_req.startAddr, blockBytes)
195
196  toITLB(1).valid         := f1_valid && f1_doubleLine
197  toITLB(1).bits.size     := 3.U // TODO: fix the size
198  toITLB(1).bits.vaddr    := align(f1_ftq_req.fallThruAddr, blockBytes)
199  toITLB(1).bits.debug.pc := align(f1_ftq_req.fallThruAddr, blockBytes)
200
201  toITLB.map{port =>
202    port.bits.cmd                 := TlbCmd.exec
203    port.bits.robIdx              := DontCare
204    port.bits.debug.isFirstIssue  := DontCare
205  }
206
207  fromITLB.map(_.ready := true.B)
208
209  val (tlbRespValid, tlbRespPAddr) = (fromITLB.map(_.valid), VecInit(fromITLB.map(_.bits.paddr)))
210  val (tlbRespMiss) = (fromITLB.map(port => port.bits.miss && port.valid))
211  val (tlbExcpPF,    tlbExcpAF)    = (fromITLB.map(port => port.bits.excp.pf.instr && port.valid),
212    fromITLB.map(port => (port.bits.excp.af.instr) && port.valid)) //TODO: Temp treat mmio req as access fault
213
214  tlbRespAllValid := tlbRespValid(0)  && (tlbRespValid(1) || !f1_doubleLine)
215
216  val f1_pAddrs             = tlbRespPAddr   //TODO: Temporary assignment
217  val f1_pTags              = VecInit(f1_pAddrs.map(get_phy_tag(_)))
218  val (f1_tags, f1_cacheline_valid, f1_datas)   = (meta_resp.tags, meta_resp.valid, data_resp.datas)
219  val bank0_hit_vec         = VecInit(f1_tags(0).zipWithIndex.map{ case(way_tag,i) => f1_cacheline_valid(0)(i) && way_tag ===  f1_pTags(0) })
220  val bank1_hit_vec         = VecInit(f1_tags(1).zipWithIndex.map{ case(way_tag,i) => f1_cacheline_valid(1)(i) && way_tag ===  f1_pTags(1) })
221  val (bank0_hit,bank1_hit) = (ParallelOR(bank0_hit_vec) && !tlbExcpPF(0) && !tlbExcpAF(0), ParallelOR(bank1_hit_vec) && !tlbExcpPF(1) && !tlbExcpAF(1))
222  val f1_hit                = (bank0_hit && bank1_hit && f1_valid && f1_doubleLine) || (f1_valid && !f1_doubleLine && bank0_hit)
223  val f1_bank_hit_vec       = VecInit(Seq(bank0_hit_vec, bank1_hit_vec))
224  val f1_bank_hit           = VecInit(Seq(bank0_hit, bank1_hit))
225
226  //MMIO
227  //MMIO only need 1 instruction
228  // val f1_mmio = tlbRespMMIO(0) && f1_valid
229
230
231  val replacers       = Seq.fill(2)(ReplacementPolicy.fromString(Some("random"),nWays,nSets/2))
232  val f1_victim_masks = VecInit(replacers.zipWithIndex.map{case (replacer, i) => UIntToOH(replacer.way(f1_vSetIdx(i)))})
233
234  val touch_sets = Seq.fill(2)(Wire(Vec(2, UInt(log2Ceil(nSets/2).W))))
235  val touch_ways = Seq.fill(2)(Wire(Vec(2, Valid(UInt(log2Ceil(nWays).W)))) )
236
237  ((replacers zip touch_sets) zip touch_ways).map{case ((r, s),w) => r.access(s,w)}
238
239  val f1_hit_data      =  VecInit(f1_datas.zipWithIndex.map { case(bank, i) =>
240    val bank_hit_data = Mux1H(f1_bank_hit_vec(i).asUInt, bank)
241    bank_hit_data
242  })
243
244  (0 until nWays).map{ w =>
245    XSPerfAccumulate("line_0_hit_way_" + Integer.toString(w, 10),  f1_fire && f1_bank_hit(0) && OHToUInt(f1_bank_hit_vec(0))  === w.U)
246  }
247
248  (0 until nWays).map{ w =>
249    XSPerfAccumulate("line_0_victim_way_" + Integer.toString(w, 10),  f1_fire && !f1_bank_hit(0) && OHToUInt(f1_victim_masks(0))  === w.U)
250  }
251
252  (0 until nWays).map{ w =>
253    XSPerfAccumulate("line_1_hit_way_" + Integer.toString(w, 10),  f1_fire && f1_doubleLine && f1_bank_hit(1) && OHToUInt(f1_bank_hit_vec(1))  === w.U)
254  }
255
256  (0 until nWays).map{ w =>
257    XSPerfAccumulate("line_1_victim_way_" + Integer.toString(w, 10),  f1_fire && f1_doubleLine && !f1_bank_hit(1) && OHToUInt(f1_victim_masks(1))  === w.U)
258  }
259
260  XSPerfAccumulate("ifu_bubble_f1_tlb_miss",    f1_valid && !tlbRespAllValid )
261
262  //---------------------------------------------
263  //  Fetch Stage 3 :
264  //  * get data from last stage (hit from f1_hit_data/miss from missQueue response)
265  //  * if at least one needed cacheline miss, wait for miss queue response (a wait_state machine) THIS IS TOO UGLY!!!
266  //  * cut cacheline(s) and send to PreDecode
267  //  * check if prediction is right (branch target and type, jump direction and type , jal target )
268  //---------------------------------------------
269  val f2_fetchFinish = Wire(Bool())
270
271  val f2_valid        = RegInit(false.B)
272  val f2_ftq_req      = RegEnable(next = f1_ftq_req,    enable = f1_fire)
273  val f2_situation    = RegEnable(next = f1_situation,  enable=f1_fire)
274  val f2_doubleLine   = RegEnable(next = f1_doubleLine, enable=f1_fire)
275  val f2_fire         = f2_valid && f2_fetchFinish && f3_ready
276
277  f2_ready := (f3_ready && f2_fetchFinish) || !f2_valid
278
279  when(f2_flush)                  {f2_valid := false.B}
280  .elsewhen(f1_fire && !f1_flush) {f2_valid := true.B }
281  .elsewhen(f2_fire)              {f2_valid := false.B}
282
283  val pmpExcpAF = fromPMP.map(port => port.instr)
284  val mmio = fromPMP.map(port => port.mmio) // TODO: handle it
285
286
287  val f2_pAddrs   = RegEnable(next = f1_pAddrs, enable = f1_fire)
288  val f2_hit      = RegEnable(next = f1_hit   , enable = f1_fire)
289  val f2_bank_hit = RegEnable(next = f1_bank_hit, enable = f1_fire)
290  val f2_miss     = f2_valid && !f2_hit
291  val (f2_vSetIdx, f2_pTags) = (RegEnable(next = f1_vSetIdx, enable = f1_fire), RegEnable(next = f1_pTags, enable = f1_fire))
292  val f2_waymask  = RegEnable(next = f1_victim_masks, enable = f1_fire)
293  //exception information
294  val f2_except_pf = RegEnable(next = VecInit(tlbExcpPF), enable = f1_fire)
295  val f2_except_af = VecInit(RegEnable(next = VecInit(tlbExcpAF), enable = f1_fire).zip(pmpExcpAF).map(a => a._1 || DataHoldBypass(a._2, RegNext(f1_fire)).asBool))
296  val f2_except    = VecInit((0 until 2).map{i => f2_except_pf(i) || f2_except_af(i)})
297  val f2_has_except = f2_valid && (f2_except_af.reduce(_||_) || f2_except_pf.reduce(_||_))
298  //MMIO
299  val f2_mmio      = DataHoldBypass(io.pmp(0).resp.mmio && !f2_except_af(0) && !f2_except_pf(0), RegNext(f1_fire)).asBool()
300
301  io.pmp.zipWithIndex.map { case (p, i) =>
302    p.req.valid := f2_fire
303    p.req.bits.addr := f2_pAddrs(i)
304    p.req.bits.size := 3.U // TODO
305    p.req.bits.cmd := TlbCmd.exec
306  }
307
308  //instruction
309  val wait_idle :: wait_queue_ready :: wait_send_req  :: wait_two_resp :: wait_0_resp :: wait_1_resp :: wait_one_resp ::wait_finish :: wait_send_mmio :: wait_mmio_resp ::Nil = Enum(10)
310  val wait_state = RegInit(wait_idle)
311
312  fromMissQueue.map{port => port.ready := true.B}
313
314  val (miss0_resp, miss1_resp) = (fromMissQueue(0).fire(), fromMissQueue(1).fire())
315  val (bank0_fix, bank1_fix)   = (miss0_resp  && !f2_bank_hit(0), miss1_resp && f2_doubleLine && !f2_bank_hit(1))
316
317  val  only_0_miss = f2_valid && !f2_hit && !f2_doubleLine && !f2_has_except && !f2_mmio
318  val  only_0_hit  = f2_valid && f2_hit && !f2_doubleLine  && !f2_mmio
319  val  hit_0_hit_1  = f2_valid && f2_hit && f2_doubleLine  && !f2_mmio
320  val (hit_0_miss_1 ,  miss_0_hit_1,  miss_0_miss_1) = (  (f2_valid && !f2_bank_hit(1) && f2_bank_hit(0) && f2_doubleLine  && !f2_has_except  && !f2_mmio),
321                                                          (f2_valid && !f2_bank_hit(0) && f2_bank_hit(1) && f2_doubleLine  && !f2_has_except  && !f2_mmio),
322                                                          (f2_valid && !f2_bank_hit(0) && !f2_bank_hit(1) && f2_doubleLine && !f2_has_except  && !f2_mmio),
323                                                       )
324
325  val  hit_0_except_1  = f2_valid && f2_doubleLine &&  !f2_except(0) && f2_except(1)  &&  f2_bank_hit(0)
326  val  miss_0_except_1 = f2_valid && f2_doubleLine &&  !f2_except(0) && f2_except(1)  && !f2_bank_hit(0)
327  //val  fetch0_except_1 = hit_0_except_1 || miss_0_except_1
328  val  except_0        = f2_valid && f2_except(0)
329
330  val f2_mq_datas     = Reg(Vec(2, UInt(blockBits.W)))
331  val f2_mmio_data    = Reg(UInt(maxInstrLen.W))
332
333  when(fromMissQueue(0).fire) {f2_mq_datas(0) :=  fromMissQueue(0).bits.data}
334  when(fromMissQueue(1).fire) {f2_mq_datas(1) :=  fromMissQueue(1).bits.data}
335  when(fromUncache.fire())    {f2_mmio_data   :=  fromUncache.bits.data}
336
337  switch(wait_state){
338    is(wait_idle){
339      when(f2_mmio && f2_valid && !f2_except_af(0) && !f2_except_pf(0)){
340        wait_state :=  wait_send_mmio
341      }.elsewhen(miss_0_except_1){
342        wait_state :=  Mux(toMissQueue(0).ready, wait_queue_ready ,wait_idle )
343      }.elsewhen( only_0_miss  || miss_0_hit_1){
344        wait_state :=  Mux(toMissQueue(0).ready, wait_queue_ready ,wait_idle )
345      }.elsewhen(hit_0_miss_1){
346        wait_state :=  Mux(toMissQueue(1).ready, wait_queue_ready ,wait_idle )
347      }.elsewhen( miss_0_miss_1 ){
348        wait_state := Mux(toMissQueue(0).ready && toMissQueue(1).ready, wait_queue_ready ,wait_idle)
349      }
350    }
351
352    is(wait_send_mmio){
353      wait_state := Mux(toUncache.fire(), wait_mmio_resp,wait_send_mmio )
354    }
355
356    is(wait_mmio_resp){
357      wait_state :=  Mux(fromUncache.fire(), wait_finish, wait_mmio_resp)
358    }
359
360    //TODO: naive logic for wait icache response
361    is(wait_queue_ready){
362      wait_state := wait_send_req
363    }
364
365    is(wait_send_req) {
366      when(miss_0_except_1 || only_0_miss || hit_0_miss_1 || miss_0_hit_1){
367        wait_state :=  wait_one_resp
368      }.elsewhen( miss_0_miss_1 ){
369        wait_state := wait_two_resp
370      }
371    }
372
373    is(wait_one_resp) {
374      when( (miss_0_except_1 ||only_0_miss || miss_0_hit_1) && fromMissQueue(0).fire()){
375        wait_state := wait_finish
376      }.elsewhen( hit_0_miss_1 && fromMissQueue(1).fire()){
377        wait_state := wait_finish
378      }
379    }
380
381    is(wait_two_resp) {
382      when(fromMissQueue(0).fire() && fromMissQueue(1).fire()){
383        wait_state := wait_finish
384      }.elsewhen( !fromMissQueue(0).fire() && fromMissQueue(1).fire() ){
385        wait_state := wait_0_resp
386      }.elsewhen(fromMissQueue(0).fire() && !fromMissQueue(1).fire()){
387        wait_state := wait_1_resp
388      }
389    }
390
391    is(wait_0_resp) {
392      when(fromMissQueue(0).fire()){
393        wait_state := wait_finish
394      }
395    }
396
397    is(wait_1_resp) {
398      when(fromMissQueue(1).fire()){
399        wait_state := wait_finish
400      }
401    }
402
403    is(wait_finish) {
404      when(f2_fire) {wait_state := wait_idle }
405    }
406  }
407
408  when(f2_flush) { wait_state := wait_idle }
409
410  (0 until 2).map { i =>
411    if(i == 1) toMissQueue(i).valid := (hit_0_miss_1 || miss_0_miss_1) && wait_state === wait_queue_ready
412      else     toMissQueue(i).valid := (only_0_miss || miss_0_hit_1 || miss_0_miss_1 || miss_0_except_1) && wait_state === wait_queue_ready
413    toMissQueue(i).bits.addr    := f2_pAddrs(i)
414    toMissQueue(i).bits.vSetIdx := f2_vSetIdx(i)
415    toMissQueue(i).bits.waymask := f2_waymask(i)
416    toMissQueue(i).bits.clientID :=0.U
417  }
418
419  toUncache.valid :=  (wait_state === wait_send_mmio) && !f2_except_af(0)
420  //assert( (GTimer() < 5000.U && toUncache.fire()) || !toUncache.fire() )
421  toUncache.bits.addr := f2_ftq_req.startAddr
422
423  fromUncache.ready := true.B
424
425  val miss_all_fix       = (wait_state === wait_finish)
426
427  f2_fetchFinish         := ((f2_valid && f2_hit) || miss_all_fix || hit_0_except_1 || except_0)
428
429  XSPerfAccumulate("ifu_bubble_f2_miss",    f2_valid && !f2_fetchFinish )
430
431  (touch_ways zip touch_sets).zipWithIndex.map{ case((t_w,t_s), i) =>
432    t_s(0)         := f1_vSetIdx(i)
433    t_w(0).valid   := f1_bank_hit(i)
434    t_w(0).bits    := OHToUInt(f1_bank_hit_vec(i))
435
436    t_s(1)         := f2_vSetIdx(i)
437    t_w(1).valid   := f2_valid && !f2_bank_hit(i)
438    t_w(1).bits    := OHToUInt(f2_waymask(i))
439  }
440
441  val sec_miss_reg   = RegInit(0.U.asTypeOf(Vec(4, Bool())))
442  val reservedRefillData = Reg(Vec(2, UInt(blockBits.W)))
443  val f2_hit_datas    = RegEnable(next = f1_hit_data, enable = f1_fire)
444  val f2_datas        = Wire(Vec(2, UInt(blockBits.W)))
445
446  f2_datas.zipWithIndex.map{case(bank,i) =>
447    if(i == 0) bank := Mux(f2_bank_hit(i), f2_hit_datas(i),Mux(sec_miss_reg(2),reservedRefillData(1),Mux(sec_miss_reg(0),reservedRefillData(0), f2_mq_datas(i))))
448    else bank := Mux(f2_bank_hit(i), f2_hit_datas(i),Mux(sec_miss_reg(3),reservedRefillData(1),Mux(sec_miss_reg(1),reservedRefillData(0), f2_mq_datas(i))))
449  }
450
451  val f2_jump_valids          = Fill(PredictWidth, !preDecoderOut.cfiOffset.valid)   | Fill(PredictWidth, 1.U(1.W)) >> (~preDecoderOut.cfiOffset.bits)
452  val f2_predecode_valids     = VecInit(preDecoderOut.pd.map(instr => instr.valid)).asUInt & f2_jump_valids
453
454  def cut(cacheline: UInt, start: UInt) : Vec[UInt] ={
455    if(HasCExtension){
456      val result   = Wire(Vec(PredictWidth + 1, UInt(16.W)))
457      val dataVec  = cacheline.asTypeOf(Vec(blockBytes * 2/ 2, UInt(16.W)))
458      val startPtr = Cat(0.U(1.W), start(blockOffBits-1, 1))
459      (0 until PredictWidth + 1).foreach( i =>
460        result(i) := dataVec(startPtr + i.U)
461      )
462      result
463    } else {
464      val result   = Wire(Vec(PredictWidth, UInt(32.W)) )
465      val dataVec  = cacheline.asTypeOf(Vec(blockBytes * 2/ 4, UInt(32.W)))
466      val startPtr = Cat(0.U(1.W), start(blockOffBits-1, 2))
467      (0 until PredictWidth).foreach( i =>
468        result(i) := dataVec(startPtr + i.U)
469      )
470      result
471    }
472  }
473
474  val f2_cut_data = cut( Cat(f2_datas.map(cacheline => cacheline.asUInt ).reverse).asUInt, f2_ftq_req.startAddr )
475  when(f2_mmio){
476    f2_cut_data(0) := f2_mmio_data(15, 0)
477    f2_cut_data(1) := f2_mmio_data(31, 16)
478  }
479
480  // deal with secondary miss in f1
481  val f2_0_f1_0 =   ((f2_valid && !f2_bank_hit(0)) && f1_valid && (get_block_addr(f2_ftq_req.startAddr) === get_block_addr(f1_ftq_req.startAddr)))
482  val f2_0_f1_1 =   ((f2_valid && !f2_bank_hit(0)) && f1_valid && f1_doubleLine && (get_block_addr(f2_ftq_req.startAddr) === get_block_addr(f1_ftq_req.startAddr + blockBytes.U)))
483  val f2_1_f1_0 =   ((f2_valid && !f2_bank_hit(1) && f2_doubleLine) && f1_valid && (get_block_addr(f2_ftq_req.startAddr+ blockBytes.U) === get_block_addr(f1_ftq_req.startAddr) ))
484  val f2_1_f1_1 =   ((f2_valid && !f2_bank_hit(1) && f2_doubleLine) && f1_valid && f1_doubleLine && (get_block_addr(f2_ftq_req.startAddr+ blockBytes.U) === get_block_addr(f1_ftq_req.startAddr + blockBytes.U) ))
485
486  val isSameLine = f2_0_f1_0 || f2_0_f1_1 || f2_1_f1_0 || f2_1_f1_1
487  val sec_miss_sit   = VecInit(Seq(f2_0_f1_0, f2_0_f1_1, f2_1_f1_0, f2_1_f1_1))
488  val hasSecMiss     = RegInit(false.B)
489
490  when(f2_flush){
491    sec_miss_reg.map(sig => sig := false.B)
492    hasSecMiss := false.B
493  }.elsewhen(isSameLine && !f1_flush && f2_fire){
494    sec_miss_reg.zipWithIndex.map{case(sig, i) => sig := sec_miss_sit(i)}
495    hasSecMiss := true.B
496  }.elsewhen((!isSameLine || f1_flush) && hasSecMiss && f2_fire){
497    sec_miss_reg.map(sig => sig := false.B)
498    hasSecMiss := false.B
499  }
500
501  when((f2_0_f1_0 || f2_0_f1_1) && f2_fire){
502    reservedRefillData(0) := f2_mq_datas(0)
503  }
504
505  when((f2_1_f1_0 || f2_1_f1_1) && f2_fire){
506    reservedRefillData(1) := f2_mq_datas(1)
507  }
508
509
510  //---------------------------------------------
511  //  Fetch Stage 4 :
512  //  * get data from last stage (hit from f1_hit_data/miss from missQueue response)
513  //  * if at least one needed cacheline miss, wait for miss queue response (a wait_state machine) THIS IS TOO UGLY!!!
514  //  * cut cacheline(s) and send to PreDecode
515  //  * check if prediction is right (branch target and type, jump direction and type , jal target )
516  //---------------------------------------------
517  val f3_valid          = RegInit(false.B)
518  val f3_ftq_req        = RegEnable(next = f2_ftq_req,    enable=f2_fire)
519  val f3_situation      = RegEnable(next = f2_situation,  enable=f2_fire)
520  val f3_doubleLine     = RegEnable(next = f2_doubleLine, enable=f2_fire)
521  val f3_fire           = io.toIbuffer.fire()
522
523  when(f3_flush)                  {f3_valid := false.B}
524  .elsewhen(f2_fire && !f2_flush) {f3_valid := true.B }
525  .elsewhen(io.toIbuffer.fire())  {f3_valid := false.B}
526
527  f3_ready := io.toIbuffer.ready || !f2_valid
528
529  val f3_cut_data       = RegEnable(next = f2_cut_data, enable=f2_fire)
530  val f3_except_pf      = RegEnable(next = f2_except_pf, enable = f2_fire)
531  val f3_except_af      = RegEnable(next = f2_except_af, enable = f2_fire)
532  val f3_hit            = RegEnable(next = f2_hit   , enable = f2_fire)
533  val f3_mmio           = RegEnable(next = f2_mmio   , enable = f2_fire)
534
535  val f3_lastHalf       = RegInit(0.U.asTypeOf(new LastHalfInfo))
536  val f3_lastHalfMatch  = f3_lastHalf.matchThisBlock(f3_ftq_req.startAddr)
537  val f3_except         = VecInit((0 until 2).map{i => f3_except_pf(i) || f3_except_af(i)})
538  val f3_has_except     = f3_valid && (f3_except_af.reduce(_||_) || f3_except_pf.reduce(_||_))
539
540  //performance counter
541  val f3_only_0_hit     = RegEnable(next = only_0_hit, enable = f2_fire)
542  val f3_only_0_miss    = RegEnable(next = only_0_miss, enable = f2_fire)
543  val f3_hit_0_hit_1    = RegEnable(next = hit_0_hit_1, enable = f2_fire)
544  val f3_hit_0_miss_1   = RegEnable(next = hit_0_miss_1, enable = f2_fire)
545  val f3_miss_0_hit_1   = RegEnable(next = miss_0_hit_1, enable = f2_fire)
546  val f3_miss_0_miss_1  = RegEnable(next = miss_0_miss_1, enable = f2_fire)
547
548  val f3_bank_hit = RegEnable(next = f2_bank_hit, enable = f2_fire)
549  val f3_req_0 = io.toIbuffer.fire()
550  val f3_req_1 = io.toIbuffer.fire() && f3_doubleLine
551  val f3_hit_0 = io.toIbuffer.fire() & f3_bank_hit(0)
552  val f3_hit_1 = io.toIbuffer.fire() && f3_doubleLine & f3_bank_hit(1)
553
554  preDecoderIn.instValid     :=  f3_valid && !f3_has_except
555  preDecoderIn.data          :=  f3_cut_data
556  preDecoderIn.startAddr     :=  f3_ftq_req.startAddr
557  preDecoderIn.fallThruAddr  :=  f3_ftq_req.fallThruAddr
558  preDecoderIn.fallThruError :=  f3_ftq_req.fallThruError
559  preDecoderIn.isDoubleLine  :=  f3_doubleLine
560  preDecoderIn.ftqOffset     :=  f3_ftq_req.ftqOffset
561  preDecoderIn.target        :=  f3_ftq_req.target
562  preDecoderIn.oversize      :=  f3_ftq_req.oversize
563  preDecoderIn.lastHalfMatch :=  f3_lastHalfMatch
564  preDecoderIn.pageFault     :=  f3_except_pf
565  preDecoderIn.accessFault   :=  f3_except_af
566  preDecoderIn.mmio          :=  f3_mmio
567
568
569  // TODO: What if next packet does not match?
570  when (f3_flush) {
571    f3_lastHalf.valid := false.B
572  }.elsewhen (io.toIbuffer.fire()) {
573    f3_lastHalf.valid := preDecoderOut.hasLastHalf
574    f3_lastHalf.middlePC := preDecoderOut.realEndPC
575  }
576
577  val f3_predecode_range = VecInit(preDecoderOut.pd.map(inst => inst.valid)).asUInt
578  val f3_mmio_range      = VecInit((0 until PredictWidth).map(i => if(i ==0) true.B else false.B))
579
580  io.toIbuffer.valid          := f3_valid
581  io.toIbuffer.bits.instrs    := preDecoderOut.instrs
582  io.toIbuffer.bits.valid     := Mux(f3_mmio, f3_mmio_range.asUInt, f3_predecode_range & preDecoderOut.instrRange.asUInt)
583  io.toIbuffer.bits.pd        := preDecoderOut.pd
584  io.toIbuffer.bits.ftqPtr    := f3_ftq_req.ftqIdx
585  io.toIbuffer.bits.pc        := preDecoderOut.pc
586  io.toIbuffer.bits.ftqOffset.zipWithIndex.map{case(a, i) => a.bits := i.U; a.valid := preDecoderOut.takens(i) && !f3_mmio}
587  io.toIbuffer.bits.foldpc    := preDecoderOut.pc.map(i => XORFold(i(VAddrBits-1,1), MemPredPCWidth))
588  io.toIbuffer.bits.ipf       := preDecoderOut.pageFault
589  io.toIbuffer.bits.acf       := preDecoderOut.accessFault
590  io.toIbuffer.bits.crossPageIPFFix := preDecoderOut.crossPageIPF
591
592  //Write back to Ftq
593  val finishFetchMaskReg = RegNext(f3_valid && !(f2_fire && !f2_flush))
594
595  val f3_mmio_missOffset = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)))
596  f3_mmio_missOffset.valid := f3_mmio
597  f3_mmio_missOffset.bits  := 0.U
598
599  toFtq.pdWb.valid           := !finishFetchMaskReg && f3_valid
600  toFtq.pdWb.bits.pc         := preDecoderOut.pc
601  toFtq.pdWb.bits.pd         := preDecoderOut.pd
602  toFtq.pdWb.bits.pd.zipWithIndex.map{case(instr,i) => instr.valid :=  Mux(f3_mmio, f3_mmio_range(i), f3_predecode_range(i))}
603  toFtq.pdWb.bits.ftqIdx     := f3_ftq_req.ftqIdx
604  toFtq.pdWb.bits.ftqOffset  := f3_ftq_req.ftqOffset.bits
605  toFtq.pdWb.bits.misOffset  := Mux(f3_mmio, f3_mmio_missOffset, preDecoderOut.misOffset)
606  toFtq.pdWb.bits.cfiOffset  := preDecoderOut.cfiOffset
607  toFtq.pdWb.bits.target     :=  Mux(f3_mmio,Mux(toFtq.pdWb.bits.pd(0).isRVC, toFtq.pdWb.bits.pc(0) + 2.U , toFtq.pdWb.bits.pc(0)+4.U) ,preDecoderOut.target)
608  toFtq.pdWb.bits.jalTarget  := preDecoderOut.jalTarget
609  toFtq.pdWb.bits.instrRange := Mux(f3_mmio, f3_mmio_range, preDecoderOut.instrRange)
610
611  val predecodeFlush     = ((preDecoderOut.misOffset.valid || f3_mmio) && f3_valid)
612  val predecodeFlushReg  = RegNext(predecodeFlush && !(f2_fire && !f2_flush))
613
614  val perfinfo = IO(new Bundle(){
615    val perfEvents = Output(new PerfEventsBundle(15))
616  })
617
618  val perfEvents = Seq(
619    ("frontendFlush                ", f3_redirect                                ),
620    ("ifu_req                      ", io.toIbuffer.fire()                        ),
621    ("ifu_miss                     ", io.toIbuffer.fire() && !f3_hit             ),
622    ("ifu_req_cacheline_0          ", f3_req_0                                   ),
623    ("ifu_req_cacheline_1          ", f3_req_1                                   ),
624    ("ifu_req_cacheline_0_hit      ", f3_hit_1                                   ),
625    ("ifu_req_cacheline_1_hit      ", f3_hit_1                                   ),
626    ("only_0_hit                   ", f3_only_0_hit       && io.toIbuffer.fire() ),
627    ("only_0_miss                  ", f3_only_0_miss      && io.toIbuffer.fire() ),
628    ("hit_0_hit_1                  ", f3_hit_0_hit_1      && io.toIbuffer.fire() ),
629    ("hit_0_miss_1                 ", f3_hit_0_miss_1     && io.toIbuffer.fire() ),
630    ("miss_0_hit_1                 ", f3_miss_0_hit_1     && io.toIbuffer.fire() ),
631    ("miss_0_miss_1                ", f3_miss_0_miss_1    && io.toIbuffer.fire() ),
632    ("cross_line_block             ", io.toIbuffer.fire() && f3_situation(0)     ),
633    ("fall_through_is_cacheline_end", io.toIbuffer.fire() && f3_situation(1)     ),
634  )
635
636  for (((perf_out,(perf_name,perf)),i) <- perfinfo.perfEvents.perf_events.zip(perfEvents).zipWithIndex) {
637    perf_out.incr_step := RegNext(perf)
638  }
639
640  f3_redirect := !predecodeFlushReg && predecodeFlush
641
642  XSPerfAccumulate("ifu_req",   io.toIbuffer.fire() )
643  XSPerfAccumulate("ifu_miss",  io.toIbuffer.fire() && !f3_hit )
644  XSPerfAccumulate("ifu_req_cacheline_0", f3_req_0  )
645  XSPerfAccumulate("ifu_req_cacheline_1", f3_req_1  )
646  XSPerfAccumulate("ifu_req_cacheline_0_hit",   f3_hit_0 )
647  XSPerfAccumulate("ifu_req_cacheline_1_hit",   f3_hit_1 )
648  XSPerfAccumulate("frontendFlush",  f3_redirect )
649  XSPerfAccumulate("only_0_hit",      f3_only_0_hit   && io.toIbuffer.fire()  )
650  XSPerfAccumulate("only_0_miss",     f3_only_0_miss  && io.toIbuffer.fire()  )
651  XSPerfAccumulate("hit_0_hit_1",     f3_hit_0_hit_1  && io.toIbuffer.fire()  )
652  XSPerfAccumulate("hit_0_miss_1",    f3_hit_0_miss_1 && io.toIbuffer.fire()  )
653  XSPerfAccumulate("miss_0_hit_1",    f3_miss_0_hit_1  && io.toIbuffer.fire() )
654  XSPerfAccumulate("miss_0_miss_1",   f3_miss_0_miss_1 && io.toIbuffer.fire() )
655  XSPerfAccumulate("cross_line_block", io.toIbuffer.fire() && f3_situation(0) )
656  XSPerfAccumulate("fall_through_is_cacheline_end", io.toIbuffer.fire() && f3_situation(1) )
657}
658