xref: /XiangShan/src/main/scala/xiangshan/frontend/icache/ICacheMainPipe.scala (revision 44af22172d764d72860feb70b7d6aa9819594d79)
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.icache
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import difftest._
23import freechips.rocketchip.tilelink.ClientStates
24import xiangshan._
25import xiangshan.cache.mmu._
26import utils._
27import utility._
28import xiangshan.backend.fu.{PMPReqBundle, PMPRespBundle}
29import xiangshan.frontend.{FtqICacheInfo, FtqToICacheRequestBundle, ExceptionType}
30
31class ICacheMainPipeReq(implicit p: Parameters) extends ICacheBundle
32{
33  val vaddr  = UInt(VAddrBits.W)
34  def vSetIdx = get_idx(vaddr)
35}
36
37class ICacheMainPipeResp(implicit p: Parameters) extends ICacheBundle
38{
39  val vaddr    = UInt(VAddrBits.W)
40  val data     = UInt((blockBits).W)
41  val paddr    = UInt(PAddrBits.W)
42  val gpaddr    = UInt(GPAddrBits.W)
43  val exception = UInt(ExceptionType.width.W)
44  val mmio      = Bool()
45}
46
47class ICacheMainPipeBundle(implicit p: Parameters) extends ICacheBundle
48{
49  val req  = Flipped(Decoupled(new FtqToICacheRequestBundle))
50  val resp = Vec(PortNumber, ValidIO(new ICacheMainPipeResp))
51  val topdownIcacheMiss = Output(Bool())
52  val topdownItlbMiss = Output(Bool())
53}
54
55class ICacheMetaReqBundle(implicit p: Parameters) extends ICacheBundle{
56  val toIMeta       = DecoupledIO(new ICacheReadBundle)
57  val fromIMeta     = Input(new ICacheMetaRespBundle)
58}
59
60class ICacheDataReqBundle(implicit p: Parameters) extends ICacheBundle{
61  val toIData       = Vec(partWayNum, DecoupledIO(new ICacheReadBundle))
62  val fromIData     = Input(new ICacheDataRespBundle)
63}
64
65class ICacheMSHRBundle(implicit p: Parameters) extends ICacheBundle{
66  val req   = Decoupled(new ICacheMissReq)
67  val resp  = Flipped(ValidIO(new ICacheMissResp))
68}
69
70class ICachePMPBundle(implicit p: Parameters) extends ICacheBundle{
71  val req  = Valid(new PMPReqBundle())
72  val resp = Input(new PMPRespBundle())
73}
74
75class ICachePerfInfo(implicit p: Parameters) extends ICacheBundle{
76  val only_0_hit     = Bool()
77  val only_0_miss    = Bool()
78  val hit_0_hit_1    = Bool()
79  val hit_0_miss_1   = Bool()
80  val miss_0_hit_1   = Bool()
81  val miss_0_miss_1  = Bool()
82  val hit_0_except_1 = Bool()
83  val miss_0_except_1 = Bool()
84  val except_0       = Bool()
85  val bank_hit       = Vec(2,Bool())
86  val hit            = Bool()
87}
88
89class ICacheMainPipeInterface(implicit p: Parameters) extends ICacheBundle {
90  val hartId = Input(UInt(hartIdLen.W))
91  /*** internal interface ***/
92  val dataArray     = new ICacheDataReqBundle
93  /** prefetch io */
94  val touch = Vec(PortNumber,ValidIO(new ReplacerTouch))
95  val wayLookupRead = Flipped(DecoupledIO(new WayLookupInfo))
96
97  val mshr          = new ICacheMSHRBundle
98  val errors        = Output(Vec(PortNumber, ValidIO(new L1CacheErrorInfo)))
99  /*** outside interface ***/
100  //val fetch       = Vec(PortNumber, new ICacheMainPipeBundle)
101  /* when ftq.valid is high in T + 1 cycle
102   * the ftq component must be valid in T cycle
103   */
104  val fetch       = new ICacheMainPipeBundle
105  val pmp         = Vec(PortNumber, new ICachePMPBundle)
106  val respStall   = Input(Bool())
107
108  val csr_parity_enable = Input(Bool())
109  val flush = Input(Bool())
110
111  val perfInfo = Output(new ICachePerfInfo)
112}
113
114class ICacheDB(implicit p: Parameters) extends ICacheBundle {
115  val blk_vaddr   = UInt((VAddrBits - blockOffBits).W)
116  val blk_paddr   = UInt((PAddrBits - blockOffBits).W)
117  val hit         = Bool()
118}
119
120class ICacheMainPipe(implicit p: Parameters) extends ICacheModule
121{
122  val io = IO(new ICacheMainPipeInterface)
123
124  /** Input/Output port */
125  val (fromFtq, toIFU)    = (io.fetch.req,          io.fetch.resp)
126  val (toData,  fromData) = (io.dataArray.toIData,  io.dataArray.fromIData)
127  val (toMSHR,  fromMSHR) = (io.mshr.req,           io.mshr.resp)
128  val (toPMP,   fromPMP)  = (io.pmp.map(_.req),     io.pmp.map(_.resp))
129  val fromWayLookup = io.wayLookupRead
130
131  // Statistics on the frequency distribution of FTQ fire interval
132  val cntFtqFireInterval = RegInit(0.U(32.W))
133  cntFtqFireInterval := Mux(fromFtq.fire, 1.U, cntFtqFireInterval + 1.U)
134  XSPerfHistogram("ftq2icache_fire",
135                  cntFtqFireInterval, fromFtq.fire,
136                  1, 300, 1, right_strict = true)
137
138  /** pipeline control signal */
139  val s1_ready, s2_ready = Wire(Bool())
140  val s0_fire,  s1_fire , s2_fire  = Wire(Bool())
141  val s0_flush,  s1_flush , s2_flush  = Wire(Bool())
142
143  /**
144    ******************************************************************************
145    * ICache Stage 0
146    * - send req to data SRAM
147    * - get waymask and tlb info from wayLookup
148    ******************************************************************************
149    */
150
151  /** s0 control */
152  // 0,1,2,3 -> dataArray(data); 4 -> mainPipe
153  // Ftq RegNext Register
154  val fromFtqReq          = fromFtq.bits.pcMemRead
155  val s0_valid            = fromFtq.valid
156  val s0_req_valid_all    = (0 until partWayNum + 1).map(i => fromFtq.bits.readValid(i))
157  val s0_req_vaddr_all    = (0 until partWayNum + 1).map(i => VecInit(Seq(fromFtqReq(i).startAddr, fromFtqReq(i).nextlineStart)))
158  val s0_req_vSetIdx_all  = (0 until partWayNum + 1).map(i => VecInit(s0_req_vaddr_all(i).map(get_idx)))
159  val s0_req_offset_all   = (0 until partWayNum + 1).map(i => s0_req_vaddr_all(i)(0)(log2Ceil(blockBytes)-1, 0))
160  val s0_doubleline_all   = (0 until partWayNum + 1).map(i => fromFtq.bits.readValid(i) && fromFtqReq(i).crossCacheline)
161
162  val s0_req_vaddr        = s0_req_vaddr_all.last
163  val s0_req_vSetIdx      = s0_req_vSetIdx_all.last
164  val s0_doubleline       = s0_doubleline_all.last
165
166  /**
167    ******************************************************************************
168    * get waymask and tlb info from wayLookup
169    ******************************************************************************
170    */
171  fromWayLookup.ready := s0_fire
172  val s0_waymasks       = VecInit(fromWayLookup.bits.waymask.map(_.asTypeOf(Vec(nWays, Bool()))))
173  val s0_req_ptags      = fromWayLookup.bits.ptag
174  val s0_req_gpaddr     = fromWayLookup.bits.gpaddr
175  val s0_itlb_exception = fromWayLookup.bits.itlb_exception
176  val s0_meta_corrupt   = fromWayLookup.bits.meta_corrupt
177  val s0_hits           = VecInit(fromWayLookup.bits.waymask.map(_.orR))
178
179  when(s0_fire){
180    assert((0 until PortNumber).map(i => s0_req_vSetIdx(i) === fromWayLookup.bits.vSetIdx(i)).reduce(_&&_),
181           "vSetIdxs from ftq and wayLookup are different! vaddr0=0x%x ftq: vidx0=0x%x vidx1=0x%x wayLookup: vidx0=0x%x vidx1=0x%x",
182           s0_req_vaddr(0), s0_req_vSetIdx(0), s0_req_vSetIdx(1), fromWayLookup.bits.vSetIdx(0), fromWayLookup.bits.vSetIdx(1))
183  }
184
185  /**
186    ******************************************************************************
187    * data SRAM request
188    ******************************************************************************
189    */
190  for(i <- 0 until partWayNum) {
191    toData(i).valid             := s0_req_valid_all(i)
192    toData(i).bits.isDoubleLine := s0_doubleline_all(i)
193    toData(i).bits.vSetIdx      := s0_req_vSetIdx_all(i)
194    toData(i).bits.blkOffset    := s0_req_offset_all(i)
195    toData(i).bits.wayMask      := s0_waymasks
196  }
197
198  val s0_can_go = toData.last.ready && fromWayLookup.valid && s1_ready
199  s0_flush  := io.flush
200  s0_fire   := s0_valid && s0_can_go && !s0_flush
201
202  fromFtq.ready := s0_can_go
203
204  /**
205    ******************************************************************************
206    * ICache Stage 1
207    * - PMP check
208    * - get Data SRAM read responses (latched for pipeline stop)
209    * - monitor missUint response port
210    ******************************************************************************
211    */
212  val s1_valid = generatePipeControl(lastFire = s0_fire, thisFire = s1_fire, thisFlush = s1_flush, lastFlush = false.B)
213
214  val s1_req_vaddr      = RegEnable(s0_req_vaddr,      0.U.asTypeOf(s0_req_vaddr),      s0_fire)
215  val s1_req_ptags      = RegEnable(s0_req_ptags,      0.U.asTypeOf(s0_req_ptags),      s0_fire)
216  val s1_req_gpaddr     = RegEnable(s0_req_gpaddr,     0.U.asTypeOf(s0_req_gpaddr),     s0_fire)
217  val s1_doubleline     = RegEnable(s0_doubleline,     0.U.asTypeOf(s0_doubleline),     s0_fire)
218  val s1_SRAMhits       = RegEnable(s0_hits,           0.U.asTypeOf(s0_hits),           s0_fire)
219  val s1_itlb_exception = RegEnable(s0_itlb_exception, 0.U.asTypeOf(s0_itlb_exception), s0_fire)
220  val s1_waymasks       = RegEnable(s0_waymasks,       0.U.asTypeOf(s0_waymasks),       s0_fire)
221  val s1_meta_corrupt   = RegEnable(s0_meta_corrupt,   0.U.asTypeOf(s0_meta_corrupt),   s0_fire)
222
223  val s1_req_vSetIdx  = s1_req_vaddr.map(get_idx)
224  val s1_req_paddr    = s1_req_vaddr.zip(s1_req_ptags).map{case(vaddr, ptag) => get_paddr_from_ptag(vaddr, ptag)}
225  val s1_req_offset   = s1_req_vaddr(0)(log2Ceil(blockBytes)-1, 0)
226
227  /**
228    ******************************************************************************
229    * update replacement status register
230    ******************************************************************************
231    */
232  (0 until PortNumber).foreach{ i =>
233    io.touch(i).bits.vSetIdx  := s1_req_vSetIdx(i)
234    io.touch(i).bits.way      := OHToUInt(s1_waymasks(i))
235  }
236  io.touch(0).valid := RegNext(s0_fire) && s1_SRAMhits(0)
237  io.touch(1).valid := RegNext(s0_fire) && s1_SRAMhits(1) && s1_doubleline
238
239  /**
240    ******************************************************************************
241    * PMP check
242    ******************************************************************************
243    */
244  toPMP.zipWithIndex.foreach { case (p, i) =>
245    // if itlb has exception, paddr can be invalid, therefore pmp check can be skipped
246    p.valid     := s1_valid // && s1_itlb_exception === ExceptionType.none
247    p.bits.addr := s1_req_paddr(i)
248    p.bits.size := 3.U // TODO
249    p.bits.cmd  := TlbCmd.exec
250  }
251  val s1_pmp_exception = VecInit(fromPMP.map(ExceptionType.fromPMPResp))
252  val s1_mmio          = VecInit(fromPMP.map(_.mmio))
253
254  // also raise af when meta array corrupt is detected, to cancel fetch
255  val s1_meta_exception = VecInit(s1_meta_corrupt.map(ExceptionType.fromECC(io.csr_parity_enable, _)))
256
257  // merge s1 itlb/pmp/meta exceptions, itlb has the highest priority, pmp next, meta lowest
258  val s1_exception_out = ExceptionType.merge(
259    s1_itlb_exception,
260    s1_pmp_exception,
261    s1_meta_exception
262  )
263
264  /**
265    ******************************************************************************
266    * select data from MSHR, SRAM
267    ******************************************************************************
268    */
269  val s1_MSHR_match = VecInit((0 until PortNumber).map(i => (s1_req_vSetIdx(i) === fromMSHR.bits.vSetIdx) &&
270                                                            (s1_req_ptags(i) === getPhyTagFromBlk(fromMSHR.bits.blkPaddr)) &&
271                                                            fromMSHR.valid && !fromMSHR.bits.corrupt))
272  val s1_MSHR_hits  = Seq(s1_valid && s1_MSHR_match(0),
273                          s1_valid && (s1_MSHR_match(1) && s1_doubleline))
274  val s1_MSHR_datas = fromMSHR.bits.data.asTypeOf(Vec(ICacheDataBanks, UInt((blockBits/ICacheDataBanks).W)))
275
276  val s1_hits = (0 until PortNumber).map(i => ValidHoldBypass(s1_MSHR_hits(i) || (RegNext(s0_fire) && s1_SRAMhits(i)), s1_fire || s1_flush))
277
278  val s1_bankIdxLow  = s1_req_offset >> log2Ceil(blockBytes/ICacheDataBanks)
279  val s1_bankMSHRHit = VecInit((0 until ICacheDataBanks).map(i => (i.U >= s1_bankIdxLow) && s1_MSHR_hits(0) ||
280                                                      (i.U < s1_bankIdxLow) && s1_MSHR_hits(1)))
281  val s1_datas       = VecInit((0 until ICacheDataBanks).map(i => DataHoldBypass(Mux(s1_bankMSHRHit(i), s1_MSHR_datas(i), fromData.datas(i)),
282                                                          s1_bankMSHRHit(i) || RegNext(s0_fire))))
283  val s1_codes       = DataHoldBypass(fromData.codes, RegNext(s0_fire))
284
285  s1_flush := io.flush
286  s1_ready := s2_ready || !s1_valid
287  s1_fire  := s1_valid && s2_ready && !s1_flush
288
289  /**
290    ******************************************************************************
291    * ICache Stage 2
292    * - send request to MSHR if ICache miss
293    * - monitor missUint response port
294    * - response to IFU
295    ******************************************************************************
296    */
297
298  val s2_valid = generatePipeControl(lastFire = s1_fire, thisFire = s2_fire, thisFlush = s2_flush, lastFlush = false.B)
299
300  val s2_req_vaddr    = RegEnable(s1_req_vaddr,     0.U.asTypeOf(s1_req_vaddr),     s1_fire)
301  val s2_req_ptags    = RegEnable(s1_req_ptags,     0.U.asTypeOf(s1_req_ptags),     s1_fire)
302  val s2_req_gpaddr   = RegEnable(s1_req_gpaddr,    0.U.asTypeOf(s1_req_gpaddr),    s1_fire)
303  val s2_doubleline   = RegEnable(s1_doubleline,    0.U.asTypeOf(s1_doubleline),    s1_fire)
304  val s2_exception    = RegEnable(s1_exception_out, 0.U.asTypeOf(s1_exception_out), s1_fire)  // includes itlb/pmp/meta exception
305  val s2_mmio         = RegEnable(s1_mmio,          0.U.asTypeOf(s1_mmio),          s1_fire)
306
307  val s2_req_vSetIdx  = s2_req_vaddr.map(get_idx)
308  val s2_req_offset   = s2_req_vaddr(0)(log2Ceil(blockBytes)-1, 0)
309  val s2_req_paddr    = s2_req_vaddr.zip(s2_req_ptags).map{case(vaddr, ptag) => get_paddr_from_ptag(vaddr, ptag)}
310
311  val s2_SRAMhits     = RegEnable(s1_SRAMhits, 0.U.asTypeOf(s1_SRAMhits), s1_fire)
312  val s2_codes        = RegEnable(s1_codes, 0.U.asTypeOf(s1_codes), s1_fire)
313  val s2_hits         = RegInit(VecInit(Seq.fill(PortNumber)(false.B)))
314  val s2_datas        = RegInit(VecInit(Seq.fill(ICacheDataBanks)(0.U((blockBits/ICacheDataBanks).W))))
315
316  /**
317    ******************************************************************************
318    * report data parity error
319    ******************************************************************************
320    */
321  // check data error
322  val s2_bankSel     = getBankSel(s2_req_offset, s2_valid)
323  val s2_bank_corrupt = (0 until ICacheDataBanks).map(i => (encode(s2_datas(i)) =/= s2_codes(i)))
324  val s2_data_corrupt = (0 until PortNumber).map(port => (0 until ICacheDataBanks).map(bank =>
325                         s2_bank_corrupt(bank) && s2_bankSel(port)(bank).asBool).reduce(_||_) && s2_SRAMhits(port))
326  // meta error is checked in prefetch pipeline
327  val s2_meta_corrupt = RegEnable(s1_meta_corrupt, 0.U.asTypeOf(s1_meta_corrupt), s1_fire)
328  // send errors to top
329  (0 until PortNumber).map{ i =>
330    io.errors(i).valid              := io.csr_parity_enable && RegNext(s1_fire) && (s2_meta_corrupt(i) || s2_data_corrupt(i))
331    io.errors(i).bits.report_to_beu := io.csr_parity_enable && RegNext(s1_fire) && (s2_meta_corrupt(i) || s2_data_corrupt(i))
332    io.errors(i).bits.paddr         := s2_req_paddr(i)
333    io.errors(i).bits.source        := DontCare
334    io.errors(i).bits.source.tag    := s2_meta_corrupt(i)
335    io.errors(i).bits.source.data   := s2_data_corrupt(i)
336    io.errors(i).bits.source.l2     := false.B
337    io.errors(i).bits.opType        := DontCare
338    io.errors(i).bits.opType.fetch  := true.B
339  }
340
341  /**
342    ******************************************************************************
343    * monitor missUint response port
344    ******************************************************************************
345    */
346  val s2_MSHR_match = VecInit((0 until PortNumber).map( i =>
347    (s2_req_vSetIdx(i) === fromMSHR.bits.vSetIdx) &&
348    (s2_req_ptags(i) === getPhyTagFromBlk(fromMSHR.bits.blkPaddr)) &&
349    fromMSHR.valid  // we don't care about whether it's corrupt here
350  ))
351  val s2_MSHR_hits  = Seq(s2_valid && s2_MSHR_match(0),
352                          s2_valid && s2_MSHR_match(1) && s2_doubleline)
353  val s2_MSHR_datas = fromMSHR.bits.data.asTypeOf(Vec(ICacheDataBanks, UInt((blockBits/ICacheDataBanks).W)))
354
355  val s2_bankIdxLow  = s2_req_offset >> log2Ceil(blockBytes/ICacheDataBanks)
356  val s2_bankMSHRHit = VecInit((0 until ICacheDataBanks).map( i =>
357    ((i.U >= s2_bankIdxLow) && s2_MSHR_hits(0)) || ((i.U < s2_bankIdxLow) && s2_MSHR_hits(1))
358  ))
359
360  (0 until ICacheDataBanks).foreach{ i =>
361    when(s1_fire) {
362      s2_datas := s1_datas
363    }.elsewhen(s2_bankMSHRHit(i) && !fromMSHR.bits.corrupt) {
364      // if corrupt, no need to update s2_datas (it's wrong anyway), to save power
365      s2_datas(i) := s2_MSHR_datas(i)
366    }
367  }
368
369  (0 until PortNumber).foreach{ i =>
370    when(s1_fire) {
371      s2_hits := s1_hits
372    }.elsewhen(s2_MSHR_hits(i)) {
373      // update s2_hits even if it's corrupt, to let s2_fire
374      s2_hits(i) := true.B
375    }
376  }
377
378  val s2_l2_corrupt = RegInit(VecInit(Seq.fill(PortNumber)(false.B)))
379  (0 until PortNumber).foreach{ i =>
380    when(s1_fire) {
381      s2_l2_corrupt(i) := false.B
382    }.elsewhen(s2_MSHR_hits(i)) {
383      s2_l2_corrupt(i) := fromMSHR.bits.corrupt
384    }
385  }
386
387  /**
388    ******************************************************************************
389    * send request to MSHR if ICache miss
390    ******************************************************************************
391    */
392  /* s2_exception includes itlb pf/gpf/af, pmp af and meta corruption (af), neither of which should be fetched
393   * mmio should not be fetched, it will be fetched by IFU mmio fsm
394   * also, if previous has exception, latter port should also not be fetched
395   */
396  val s2_miss = VecInit((0 until PortNumber).map { i =>
397    !s2_hits(i) && (if (i==0) true.B else s2_doubleline) &&
398      s2_exception.take(i+1).map(_ === ExceptionType.none).reduce(_&&_) &&
399      s2_mmio.take(i+1).map(!_).reduce(_&&_)
400  })
401
402  val toMSHRArbiter = Module(new Arbiter(new ICacheMissReq, PortNumber))
403
404  // To avoid sending duplicate requests.
405  val has_send = RegInit(VecInit(Seq.fill(PortNumber)(false.B)))
406  (0 until PortNumber).foreach{ i =>
407    when(s1_fire) {
408      has_send(i) := false.B
409    }.elsewhen(toMSHRArbiter.io.in(i).fire) {
410      has_send(i) := true.B
411    }
412  }
413
414  (0 until PortNumber).map{ i =>
415    toMSHRArbiter.io.in(i).valid          := s2_valid && s2_miss(i) && !has_send(i) && !s2_flush
416    toMSHRArbiter.io.in(i).bits.blkPaddr  := getBlkAddr(s2_req_paddr(i))
417    toMSHRArbiter.io.in(i).bits.vSetIdx   := s2_req_vSetIdx(i)
418  }
419  toMSHR <> toMSHRArbiter.io.out
420
421  XSPerfAccumulate("to_missUnit_stall",  toMSHR.valid && !toMSHR.ready)
422
423  val s2_fetch_finish = !s2_miss.reduce(_||_)
424
425  // also raise af if data/l2 corrupt is detected
426  val s2_data_exception = VecInit(s2_data_corrupt.map(ExceptionType.fromECC(io.csr_parity_enable, _)))
427  val s2_l2_exception   = VecInit(s2_l2_corrupt.map(ExceptionType.fromECC(true.B, _)))
428
429  // merge s2 exceptions, itlb has the highest priority, meta next, meta/data/l2 lowest (and we dont care about prioritizing between this three)
430  val s2_exception_out = ExceptionType.merge(
431    s2_exception,  // includes itlb/pmp/meta exception
432    s2_data_exception,
433    s2_l2_exception
434  )
435
436  /**
437    ******************************************************************************
438    * response to IFU
439    ******************************************************************************
440    */
441  (0 until PortNumber).foreach{ i =>
442    if(i == 0) {
443      toIFU(i).valid          := s2_fire
444      toIFU(i).bits.exception := s2_exception_out(i)
445      toIFU(i).bits.mmio      := s2_mmio(i)
446      toIFU(i).bits.data      := s2_datas.asTypeOf(UInt(blockBits.W))
447    } else {
448      toIFU(i).valid          := s2_fire && s2_doubleline
449      toIFU(i).bits.exception := Mux(s2_doubleline, s2_exception_out(i), ExceptionType.none)
450      toIFU(i).bits.mmio      := s2_mmio(i) && s2_doubleline
451      toIFU(i).bits.data      := DontCare
452    }
453    toIFU(i).bits.vaddr       := s2_req_vaddr(i)
454    toIFU(i).bits.paddr       := s2_req_paddr(i)
455    toIFU(i).bits.gpaddr      := s2_req_gpaddr  // Note: toIFU(1).bits.gpaddr is actually DontCare in current design
456  }
457
458  s2_flush := io.flush
459  s2_ready := (s2_fetch_finish && !io.respStall) || !s2_valid
460  s2_fire  := s2_valid && s2_fetch_finish && !io.respStall && !s2_flush
461
462  /**
463    ******************************************************************************
464    * report Tilelink corrupt error
465    ******************************************************************************
466    */
467  (0 until PortNumber).map{ i =>
468    when(RegNext(s2_fire && s2_l2_corrupt(i))){
469      io.errors(i).valid                 := true.B
470      io.errors(i).bits.report_to_beu    := false.B // l2 should have report that to bus error unit, no need to do it again
471      io.errors(i).bits.paddr            := RegNext(s2_req_paddr(i))
472      io.errors(i).bits.source.tag       := false.B
473      io.errors(i).bits.source.data      := false.B
474      io.errors(i).bits.source.l2        := true.B
475    }
476  }
477
478  /**
479    ******************************************************************************
480    * performance info. TODO: need to simplify the logic
481    ***********************************************************s*******************
482    */
483  io.perfInfo.only_0_hit      :=  s2_hits(0) && !s2_doubleline
484  io.perfInfo.only_0_miss     := !s2_hits(0) && !s2_doubleline
485  io.perfInfo.hit_0_hit_1     :=  s2_hits(0) &&  s2_hits(1) && s2_doubleline
486  io.perfInfo.hit_0_miss_1    :=  s2_hits(0) && !s2_hits(1) && s2_doubleline
487  io.perfInfo.miss_0_hit_1    := !s2_hits(0) &&  s2_hits(1) && s2_doubleline
488  io.perfInfo.miss_0_miss_1   := !s2_hits(0) && !s2_hits(1) && s2_doubleline
489  io.perfInfo.hit_0_except_1  :=  s2_hits(0) && (s2_exception(1) =/= ExceptionType.none) && s2_doubleline
490  io.perfInfo.miss_0_except_1 := !s2_hits(0) && (s2_exception(1) =/= ExceptionType.none) && s2_doubleline
491  io.perfInfo.bank_hit(0)     :=  s2_hits(0)
492  io.perfInfo.bank_hit(1)     :=  s2_hits(1) && s2_doubleline
493  io.perfInfo.except_0        :=  s2_exception(0) =/= ExceptionType.none
494  io.perfInfo.hit             :=  s2_hits(0) && (!s2_doubleline || s2_hits(1))
495
496  /** <PERF> fetch bubble generated by icache miss */
497  XSPerfAccumulate("icache_bubble_s2_miss", s2_valid && !s2_fetch_finish )
498  XSPerfAccumulate("icache_bubble_s0_wayLookup", s0_valid && !fromWayLookup.ready)
499
500  io.fetch.topdownIcacheMiss := !s2_fetch_finish
501  io.fetch.topdownItlbMiss := s0_valid && !fromWayLookup.ready
502
503  // class ICacheTouchDB(implicit p: Parameters) extends ICacheBundle{
504  //   val blkPaddr  = UInt((PAddrBits - blockOffBits).W)
505  //   val vSetIdx   = UInt(idxBits.W)
506  //   val waymask   = UInt(log2Ceil(nWays).W)
507  // }
508
509  // val isWriteICacheTouchTable = WireInit(Constantin.createRecord("isWriteICacheTouchTable" + p(XSCoreParamsKey).HartId.toString))
510  // val ICacheTouchTable = ChiselDB.createTable("ICacheTouchTable" + p(XSCoreParamsKey).HartId.toString, new ICacheTouchDB)
511
512  // val ICacheTouchDumpData = Wire(Vec(PortNumber, new ICacheTouchDB))
513  // (0 until PortNumber).foreach{ i =>
514  //   ICacheTouchDumpData(i).blkPaddr  := getBlkAddr(s2_req_paddr(i))
515  //   ICacheTouchDumpData(i).vSetIdx   := s2_req_vSetIdx(i)
516  //   ICacheTouchDumpData(i).waymask   := OHToUInt(s2_tag_match_vec(i))
517  //   ICacheTouchTable.log(
518  //     data  = ICacheTouchDumpData(i),
519  //     en    = io.touch(i).valid,
520  //     site  = "req_" + i.toString,
521  //     clock = clock,
522  //     reset = reset
523  //   )
524  // }
525
526  /**
527    ******************************************************************************
528    * difftest refill check
529    ******************************************************************************
530    */
531  if (env.EnableDifftest) {
532    val discards = (0 until PortNumber).map { i =>
533      val discard = toIFU(i).bits.exception =/= ExceptionType.none || toIFU(i).bits.mmio
534      discard
535    }
536    val blkPaddrAll = s2_req_paddr.map(addr => addr(PAddrBits - 1, blockOffBits) << blockOffBits)
537    (0 until ICacheDataBanks).map { i =>
538      val diffMainPipeOut = DifftestModule(new DiffRefillEvent, dontCare = true)
539      diffMainPipeOut.coreid := io.hartId
540      diffMainPipeOut.index := (3 + i).U
541
542      val bankSel = getBankSel(s2_req_offset, s2_valid).reduce(_|_)
543      val lineSel = getLineSel(s2_req_offset)
544
545      diffMainPipeOut.valid := s2_fire && bankSel(i).asBool && Mux(lineSel(i), !discards(1), !discards(0))
546      diffMainPipeOut.addr  := Mux(lineSel(i), blkPaddrAll(1) + (i.U << (log2Ceil(blockBytes/ICacheDataBanks))),
547                                               blkPaddrAll(0) + (i.U << (log2Ceil(blockBytes/ICacheDataBanks))))
548
549      diffMainPipeOut.data :=  s2_datas(i).asTypeOf(diffMainPipeOut.data)
550      diffMainPipeOut.idtfr := DontCare
551    }
552  }
553}