xref: /XiangShan/src/main/scala/xiangshan/frontend/icache/IPrefetch.scala (revision 2c9f4a9f8b3a5df2c18c0b59e73304da0eee610c)
17052722fSJay/***************************************************************************************
27052722fSJay  * Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
37052722fSJay  * Copyright (c) 2020-2021 Peng Cheng Laboratory
47052722fSJay  *
57052722fSJay  * XiangShan is licensed under Mulan PSL v2.
67052722fSJay  * You can use this software according to the terms and conditions of the Mulan PSL v2.
77052722fSJay  * You may obtain a copy of Mulan PSL v2 at:
87052722fSJay  *          http://license.coscl.org.cn/MulanPSL2
97052722fSJay  *
107052722fSJay  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
117052722fSJay  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
127052722fSJay  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
137052722fSJay  *
147052722fSJay  * See the Mulan PSL v2 for more details.
157052722fSJay  ***************************************************************************************/
167052722fSJay
177052722fSJaypackage xiangshan.frontend.icache
187052722fSJay
198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters
207052722fSJayimport chisel3._
217052722fSJayimport chisel3.util._
227d45a146SYinan Xuimport difftest._
237052722fSJayimport freechips.rocketchip.tilelink._
247052722fSJayimport utils._
257052722fSJayimport xiangshan.cache.mmu._
267052722fSJayimport xiangshan.frontend._
27d2b20d1aSTang Haojinimport xiangshan.backend.fu.{PMPReqBundle, PMPRespBundle}
28d2b20d1aSTang Haojinimport huancun.PreferCacheKey
29b92c5693STang Haojinimport xiangshan.XSCoreParamsKey
30*2c9f4a9fSxu_zhimport xiangshan.SoftIfetchPrefetchBundle
31b1ded4e8Sguohongyuimport utility._
327052722fSJay
337052722fSJayabstract class IPrefetchBundle(implicit p: Parameters) extends ICacheBundle
347052722fSJayabstract class IPrefetchModule(implicit p: Parameters) extends ICacheModule
357052722fSJay
36*2c9f4a9fSxu_zhclass IPrefetchReq(implicit p: Parameters) extends IPrefetchBundle {
37*2c9f4a9fSxu_zh  val startAddr     : UInt   = UInt(VAddrBits.W)
38*2c9f4a9fSxu_zh  val nextlineStart : UInt   = UInt(VAddrBits.W)
39*2c9f4a9fSxu_zh  val ftqIdx        : FtqPtr = new FtqPtr
40*2c9f4a9fSxu_zh  val isSoftPrefetch: Bool   = Bool()
41*2c9f4a9fSxu_zh  def crossCacheline: Bool   = startAddr(blockOffBits - 1) === 1.U
42*2c9f4a9fSxu_zh
43*2c9f4a9fSxu_zh  def fromFtqICacheInfo(info: FtqICacheInfo): IPrefetchReq = {
44*2c9f4a9fSxu_zh    this.startAddr := info.startAddr
45*2c9f4a9fSxu_zh    this.nextlineStart := info.nextlineStart
46*2c9f4a9fSxu_zh    this.ftqIdx := info.ftqIdx
47*2c9f4a9fSxu_zh    this.isSoftPrefetch := false.B
48*2c9f4a9fSxu_zh    this
49*2c9f4a9fSxu_zh  }
50*2c9f4a9fSxu_zh
51*2c9f4a9fSxu_zh  def fromSoftPrefetch(req: SoftIfetchPrefetchBundle): IPrefetchReq = {
52*2c9f4a9fSxu_zh    this.startAddr := req.vaddr
53*2c9f4a9fSxu_zh    this.nextlineStart := req.vaddr + (1 << blockOffBits).U
54*2c9f4a9fSxu_zh    this.ftqIdx := DontCare
55*2c9f4a9fSxu_zh    this.isSoftPrefetch := true.B
56*2c9f4a9fSxu_zh    this
57*2c9f4a9fSxu_zh  }
58*2c9f4a9fSxu_zh}
59*2c9f4a9fSxu_zh
6088895b11Sxu_zhclass IPrefetchIO(implicit p: Parameters) extends IPrefetchBundle {
61b92f8445Sssszwic  // control
62b92f8445Sssszwic  val csr_pf_enable     = Input(Bool())
63f80535c3Sxu_zh  val csr_parity_enable = Input(Bool())
64b92f8445Sssszwic  val flush             = Input(Bool())
6558c354d0Sssszwic
66*2c9f4a9fSxu_zh  val req               = Flipped(Decoupled(new IPrefetchReq))
67*2c9f4a9fSxu_zh  val flushFromBpu      = Flipped(new BpuFlushInfo)
68b92f8445Sssszwic  val itlb              = Vec(PortNumber, new TlbRequestIO)
69b92f8445Sssszwic  val pmp               = Vec(PortNumber, new ICachePMPBundle)
70b92f8445Sssszwic  val metaRead          = new ICacheMetaReqBundle
71b92f8445Sssszwic  val MSHRReq           = DecoupledIO(new ICacheMissReq)
72b92f8445Sssszwic  val MSHRResp          = Flipped(ValidIO(new ICacheMissResp))
73b92f8445Sssszwic  val wayLookupWrite    = DecoupledIO(new WayLookupInfo)
747052722fSJay}
757052722fSJay
767052722fSJayclass IPrefetchPipe(implicit p: Parameters) extends  IPrefetchModule
777052722fSJay{
7888895b11Sxu_zh  val io: IPrefetchIO = IO(new IPrefetchIO)
797052722fSJay
80b92f8445Sssszwic  val (toITLB,  fromITLB) = (io.itlb.map(_.req), io.itlb.map(_.resp))
81b92f8445Sssszwic  val (toPMP,  fromPMP)   = (io.pmp.map(_.req), io.pmp.map(_.resp))
82b92f8445Sssszwic  val (toMeta,  fromMeta) = (io.metaRead.toIMeta,  io.metaRead.fromIMeta)
83b92f8445Sssszwic  val (toMSHR, fromMSHR)  = (io.MSHRReq, io.MSHRResp)
84b92f8445Sssszwic  val toWayLookup = io.wayLookupWrite
857052722fSJay
86b92f8445Sssszwic  val s0_fire, s1_fire, s2_fire             = WireInit(false.B)
87b92f8445Sssszwic  val s0_discard, s2_discard                = WireInit(false.B)
88b92f8445Sssszwic  val s0_ready, s1_ready, s2_ready          = WireInit(false.B)
89b92f8445Sssszwic  val s0_flush, s1_flush, s2_flush          = WireInit(false.B)
90b92f8445Sssszwic  val from_bpu_s0_flush, from_bpu_s1_flush  = WireInit(false.B)
917052722fSJay
92cb6e5d3cSssszwic  /**
93cb6e5d3cSssszwic    ******************************************************************************
94cb6e5d3cSssszwic    * IPrefetch Stage 0
95b92f8445Sssszwic    * - 1. receive ftq req
96b92f8445Sssszwic    * - 2. send req to ITLB
97b92f8445Sssszwic    * - 3. send req to Meta SRAM
98cb6e5d3cSssszwic    ******************************************************************************
99cb6e5d3cSssszwic    */
100*2c9f4a9fSxu_zh  val s0_valid  = io.req.valid
101cb6e5d3cSssszwic
102b92f8445Sssszwic  /**
103b92f8445Sssszwic    ******************************************************************************
104b92f8445Sssszwic    * receive ftq req
105b92f8445Sssszwic    ******************************************************************************
106b92f8445Sssszwic    */
107*2c9f4a9fSxu_zh  val s0_req_vaddr    = VecInit(Seq(io.req.bits.startAddr, io.req.bits.nextlineStart))
108*2c9f4a9fSxu_zh  val s0_req_ftqIdx   = io.req.bits.ftqIdx
109*2c9f4a9fSxu_zh  val s0_isSoftPrefetch = io.req.bits.isSoftPrefetch
110*2c9f4a9fSxu_zh  val s0_doubleline   = io.req.bits.crossCacheline
11188895b11Sxu_zh  val s0_req_vSetIdx  = s0_req_vaddr.map(get_idx)
1127052722fSJay
113*2c9f4a9fSxu_zh  from_bpu_s0_flush := !s0_isSoftPrefetch && (io.flushFromBpu.shouldFlushByStage2(s0_req_ftqIdx) ||
114*2c9f4a9fSxu_zh                                              io.flushFromBpu.shouldFlushByStage3(s0_req_ftqIdx))
115b92f8445Sssszwic  s0_flush := io.flush || from_bpu_s0_flush || s1_flush
1167052722fSJay
117b92f8445Sssszwic  val s0_can_go = s1_ready && toITLB(0).ready && toITLB(1).ready && toMeta.ready
118*2c9f4a9fSxu_zh  io.req.ready := s0_can_go
1197052722fSJay
120b92f8445Sssszwic  s0_fire := s0_valid && s0_can_go && !s0_flush
121cb6e5d3cSssszwic
122cb6e5d3cSssszwic  /**
123cb6e5d3cSssszwic    ******************************************************************************
124cb6e5d3cSssszwic    * IPrefetch Stage 1
125b92f8445Sssszwic    * - 1. Receive resp from ITLB
126b92f8445Sssszwic    * - 2. Receive resp from IMeta and check
127b92f8445Sssszwic    * - 3. Monitor the requests from missUnit to write to SRAM.
128b92f8445Sssszwic    * - 4. Wirte wayLookup
129cb6e5d3cSssszwic    ******************************************************************************
130cb6e5d3cSssszwic    */
131b92f8445Sssszwic  val s1_valid = generatePipeControl(lastFire = s0_fire, thisFire = s1_fire, thisFlush = s1_flush, lastFlush = false.B)
132cb6e5d3cSssszwic
133b92f8445Sssszwic  val s1_req_vaddr    = RegEnable(s0_req_vaddr, 0.U.asTypeOf(s0_req_vaddr), s0_fire)
134*2c9f4a9fSxu_zh  val s1_isSoftPrefetch = RegEnable(s0_isSoftPrefetch, 0.U.asTypeOf(s0_isSoftPrefetch), s0_fire)
135b92f8445Sssszwic  val s1_doubleline   = RegEnable(s0_doubleline, 0.U.asTypeOf(s0_doubleline), s0_fire)
136b92f8445Sssszwic  val s1_req_ftqIdx   = RegEnable(s0_req_ftqIdx, 0.U.asTypeOf(s0_req_ftqIdx), s0_fire)
13788895b11Sxu_zh  val s1_req_vSetIdx  = VecInit(s1_req_vaddr.map(get_idx))
1387052722fSJay
139b92f8445Sssszwic  val m_idle :: m_itlbResend :: m_metaResend :: m_enqWay :: m_enterS2 :: Nil = Enum(5)
140b92f8445Sssszwic  val state = RegInit(m_idle)
141b92f8445Sssszwic  val next_state = WireDefault(state)
142b92f8445Sssszwic  val s0_fire_r = RegNext(s0_fire)
143b92f8445Sssszwic  dontTouch(state)
144b92f8445Sssszwic  dontTouch(next_state)
145b92f8445Sssszwic  state := next_state
1467052722fSJay
147b92f8445Sssszwic  /**
148b92f8445Sssszwic    ******************************************************************************
149b92f8445Sssszwic    * resend itlb req if miss
150b92f8445Sssszwic    ******************************************************************************
151b92f8445Sssszwic    */
152b92f8445Sssszwic  val s1_wait_itlb  = RegInit(VecInit(Seq.fill(PortNumber)(false.B)))
153b92f8445Sssszwic  (0 until PortNumber).foreach { i =>
154b92f8445Sssszwic    when(s1_flush) {
155b92f8445Sssszwic      s1_wait_itlb(i) := false.B
156b92f8445Sssszwic    }.elsewhen(RegNext(s0_fire) && fromITLB(i).bits.miss) {
157b92f8445Sssszwic      s1_wait_itlb(i) := true.B
158b92f8445Sssszwic    }.elsewhen(s1_wait_itlb(i) && !fromITLB(i).bits.miss) {
159b92f8445Sssszwic      s1_wait_itlb(i) := false.B
160b92f8445Sssszwic    }
161b92f8445Sssszwic  }
162b92f8445Sssszwic  val s1_need_itlb    = VecInit(Seq((RegNext(s0_fire) || s1_wait_itlb(0)) && fromITLB(0).bits.miss,
163b92f8445Sssszwic                                    (RegNext(s0_fire) || s1_wait_itlb(1)) && fromITLB(1).bits.miss && s1_doubleline))
164b92f8445Sssszwic  val tlb_valid_pulse = VecInit(Seq((RegNext(s0_fire) || s1_wait_itlb(0)) && !fromITLB(0).bits.miss,
165b92f8445Sssszwic                                    (RegNext(s0_fire) || s1_wait_itlb(1)) && !fromITLB(1).bits.miss && s1_doubleline))
166b92f8445Sssszwic  val tlb_valid_latch = VecInit((0 until PortNumber).map(i => ValidHoldBypass(tlb_valid_pulse(i), s1_fire, flush=s1_flush)))
167b92f8445Sssszwic  val itlb_finish     = tlb_valid_latch(0) && (!s1_doubleline || tlb_valid_latch(1))
1687052722fSJay
169b92f8445Sssszwic  for (i <- 0 until PortNumber) {
170b92f8445Sssszwic    toITLB(i).valid             := s1_need_itlb(i) || (s0_valid && (if(i == 0) true.B else s0_doubleline))
171b92f8445Sssszwic    toITLB(i).bits              := DontCare
172b92f8445Sssszwic    toITLB(i).bits.size         := 3.U
173b92f8445Sssszwic    toITLB(i).bits.vaddr        := Mux(s1_need_itlb(i), s1_req_vaddr(i), s0_req_vaddr(i))
174b92f8445Sssszwic    toITLB(i).bits.debug.pc     := Mux(s1_need_itlb(i), s1_req_vaddr(i), s0_req_vaddr(i))
175b92f8445Sssszwic    toITLB(i).bits.cmd          := TlbCmd.exec
176b92f8445Sssszwic    toITLB(i).bits.no_translate := false.B
177b92f8445Sssszwic  }
178b92f8445Sssszwic  fromITLB.foreach(_.ready := true.B)
179b92f8445Sssszwic  io.itlb.foreach(_.req_kill := false.B)
1807052722fSJay
181b92f8445Sssszwic  /**
182b92f8445Sssszwic    ******************************************************************************
183b92f8445Sssszwic    * Receive resp from ITLB
184b92f8445Sssszwic    ******************************************************************************
185b92f8445Sssszwic    */
186b92f8445Sssszwic  val s1_req_paddr_wire     = VecInit(fromITLB.map(_.bits.paddr(0)))
187b92f8445Sssszwic  val s1_req_paddr_reg      = VecInit((0 until PortNumber).map( i =>
18888895b11Sxu_zh    RegEnable(s1_req_paddr_wire(i), 0.U(PAddrBits.W), tlb_valid_pulse(i))
18988895b11Sxu_zh  ))
190b92f8445Sssszwic  val s1_req_paddr          = VecInit((0 until PortNumber).map( i =>
19188895b11Sxu_zh    Mux(tlb_valid_pulse(i), s1_req_paddr_wire(i), s1_req_paddr_reg(i))
19288895b11Sxu_zh  ))
19391946104Sxu_zh  val s1_req_gpaddr_tmp     = VecInit((0 until PortNumber).map( i =>
19488895b11Sxu_zh    ResultHoldBypass(valid = tlb_valid_pulse(i), init = 0.U.asTypeOf(fromITLB(i).bits.gpaddr(0)), data = fromITLB(i).bits.gpaddr(0))
19588895b11Sxu_zh  ))
19688895b11Sxu_zh  val s1_itlb_exception     = VecInit((0 until PortNumber).map( i =>
19788895b11Sxu_zh    ResultHoldBypass(valid = tlb_valid_pulse(i), init = 0.U(ExceptionType.width.W), data = ExceptionType.fromTlbResp(fromITLB(i).bits))
19888895b11Sxu_zh  ))
19988895b11Sxu_zh  val s1_itlb_exception_gpf = VecInit(s1_itlb_exception.map(_ === ExceptionType.gpf))
200b92f8445Sssszwic
20191946104Sxu_zh  /* Select gpaddr with the first gpf
20291946104Sxu_zh   * Note: the backend wants the base guest physical address of a fetch block
20391946104Sxu_zh   *       for port(i), its base gpaddr is actually (gpaddr - i * blocksize)
20491946104Sxu_zh   *       see GPAMem: https://github.com/OpenXiangShan/XiangShan/blob/344cf5d55568dd40cd658a9ee66047a505eeb504/src/main/scala/xiangshan/backend/GPAMem.scala#L33-L34
20591946104Sxu_zh   *       see also: https://github.com/OpenXiangShan/XiangShan/blob/344cf5d55568dd40cd658a9ee66047a505eeb504/src/main/scala/xiangshan/frontend/IFU.scala#L374-L375
20691946104Sxu_zh   */
20791946104Sxu_zh  val s1_req_gpaddr = PriorityMuxDefault(
20888895b11Sxu_zh    s1_itlb_exception_gpf zip (0 until PortNumber).map(i => s1_req_gpaddr_tmp(i) - (i << blockOffBits).U),
20991946104Sxu_zh    0.U.asTypeOf(s1_req_gpaddr_tmp(0))
21091946104Sxu_zh  )
21191946104Sxu_zh
212b92f8445Sssszwic  /**
213b92f8445Sssszwic    ******************************************************************************
214b92f8445Sssszwic    * resend metaArray read req when itlb miss finish
215b92f8445Sssszwic    ******************************************************************************
216b92f8445Sssszwic    */
217b92f8445Sssszwic  val s1_need_meta = ((state === m_itlbResend) && itlb_finish) || (state === m_metaResend)
218b92f8445Sssszwic  toMeta.valid              := s1_need_meta || s0_valid
219b92f8445Sssszwic  toMeta.bits               := DontCare
220b92f8445Sssszwic  toMeta.bits.isDoubleLine  := Mux(s1_need_meta, s1_doubleline, s0_doubleline)
221b92f8445Sssszwic
222b92f8445Sssszwic  for (i <- 0 until PortNumber) {
223b92f8445Sssszwic    toMeta.bits.vSetIdx(i)  := Mux(s1_need_meta, s1_req_vSetIdx(i), s0_req_vSetIdx(i))
224cb6e5d3cSssszwic  }
225cb6e5d3cSssszwic
226cb6e5d3cSssszwic  /**
227cb6e5d3cSssszwic    ******************************************************************************
228b92f8445Sssszwic    * Receive resp from IMeta and check
229cb6e5d3cSssszwic    ******************************************************************************
230cb6e5d3cSssszwic    */
23188895b11Sxu_zh  val s1_req_ptags    = VecInit(s1_req_paddr.map(get_phy_tag))
232cb6e5d3cSssszwic
233b92f8445Sssszwic  val s1_meta_ptags   = fromMeta.tags
234b92f8445Sssszwic  val s1_meta_valids  = fromMeta.entryValid
235b92f8445Sssszwic  // If error is found in either way, the tag_eq_vec is unreliable, so we do not use waymask, but directly .orR
23688895b11Sxu_zh  val s1_meta_corrupt = VecInit(fromMeta.errors.map(_.asUInt.orR))
2379bba777eSssszwic
238b92f8445Sssszwic  def get_waymask(paddrs: Vec[UInt]): Vec[UInt] = {
23988895b11Sxu_zh    val ptags         = paddrs.map(get_phy_tag)
240b92f8445Sssszwic    val tag_eq_vec    = VecInit((0 until PortNumber).map( p => VecInit((0 until nWays).map( w => s1_meta_ptags(p)(w) === ptags(p)))))
241b92f8445Sssszwic    val tag_match_vec = VecInit((0 until PortNumber).map( k => VecInit(tag_eq_vec(k).zipWithIndex.map{ case(way_tag_eq, w) => way_tag_eq && s1_meta_valids(k)(w)})))
242b92f8445Sssszwic    val waymasks      = VecInit(tag_match_vec.map(_.asUInt))
243b92f8445Sssszwic    waymasks
244cb6e5d3cSssszwic  }
2459bba777eSssszwic
246b92f8445Sssszwic  val s1_SRAM_waymasks = VecInit((0 until PortNumber).map(i =>
247b92f8445Sssszwic                            Mux(tlb_valid_pulse(i), get_waymask(s1_req_paddr_wire)(i), get_waymask(s1_req_paddr_reg)(i))))
248b92f8445Sssszwic
249b92f8445Sssszwic  /**
250b92f8445Sssszwic    ******************************************************************************
251b4f1e5b2Sxu_zh    * update waymask according to MSHR update data
252b92f8445Sssszwic    ******************************************************************************
253b92f8445Sssszwic    */
254b92f8445Sssszwic  def update_waymask(mask: UInt, vSetIdx: UInt, ptag: UInt): UInt = {
255b92f8445Sssszwic    require(mask.getWidth == nWays)
256b92f8445Sssszwic    val new_mask  = WireInit(mask)
257b4f1e5b2Sxu_zh    val valid = fromMSHR.valid && !fromMSHR.bits.corrupt
258b4f1e5b2Sxu_zh    val vset_same = fromMSHR.bits.vSetIdx === vSetIdx
259b92f8445Sssszwic    val ptag_same = getPhyTagFromBlk(fromMSHR.bits.blkPaddr) === ptag
260b92f8445Sssszwic    val way_same  = fromMSHR.bits.waymask === mask
261b4f1e5b2Sxu_zh    when(valid && vset_same) {
262b92f8445Sssszwic      when(ptag_same) {
263b92f8445Sssszwic        new_mask := fromMSHR.bits.waymask
264b92f8445Sssszwic      }.elsewhen(way_same) {
265b92f8445Sssszwic        new_mask := 0.U
266cb6e5d3cSssszwic      }
267b92f8445Sssszwic    }
268b92f8445Sssszwic    new_mask
269b92f8445Sssszwic  }
270b92f8445Sssszwic
271b92f8445Sssszwic  val s1_SRAM_valid = s0_fire_r || RegNext(s1_need_meta && toMeta.ready)
272b4f1e5b2Sxu_zh  val s1_MSHR_valid = fromMSHR.valid && !fromMSHR.bits.corrupt
273b4f1e5b2Sxu_zh  val s1_waymasks   = WireInit(VecInit(Seq.fill(PortNumber)(0.U(nWays.W))))
274b4f1e5b2Sxu_zh  val s1_waymasks_r = RegEnable(s1_waymasks, 0.U.asTypeOf(s1_waymasks), s1_SRAM_valid || s1_MSHR_valid)
275b92f8445Sssszwic  (0 until PortNumber).foreach{i =>
276b4f1e5b2Sxu_zh    val old_waymask = Mux(s1_SRAM_valid, s1_SRAM_waymasks(i), s1_waymasks_r(i))
277b4f1e5b2Sxu_zh    s1_waymasks(i) := update_waymask(old_waymask, s1_req_vSetIdx(i), s1_req_ptags(i))
278b92f8445Sssszwic  }
279b92f8445Sssszwic
280b92f8445Sssszwic  /**
281b92f8445Sssszwic    ******************************************************************************
282b92f8445Sssszwic    * send enqueu req to WayLookup
283b92f8445Sssszwic    ******** **********************************************************************
284b92f8445Sssszwic    */
285b92f8445Sssszwic  // Disallow enqueuing wayLookup when SRAM write occurs.
286*2c9f4a9fSxu_zh  toWayLookup.valid             := ((state === m_enqWay) || ((state === m_idle) && itlb_finish)) &&
287*2c9f4a9fSxu_zh    !s1_flush && !fromMSHR.valid && !s1_isSoftPrefetch  // do not enqueue soft prefetch
288b92f8445Sssszwic  toWayLookup.bits.vSetIdx      := s1_req_vSetIdx
289b92f8445Sssszwic  toWayLookup.bits.waymask      := s1_waymasks
290b92f8445Sssszwic  toWayLookup.bits.ptag         := s1_req_ptags
291b92f8445Sssszwic  toWayLookup.bits.gpaddr       := s1_req_gpaddr
2921a5af821Sxu_zh  (0 until PortNumber).foreach { i =>
2931a5af821Sxu_zh    val excpValid = (if (i == 0) true.B else s1_doubleline)  // exception in first line is always valid, in second line is valid iff is doubleline request
29488895b11Sxu_zh    // Send s1_itlb_exception to WayLookup (instead of s1_exception_out) for better timing. Will check pmp again in mainPipe
29588895b11Sxu_zh    toWayLookup.bits.itlb_exception(i) := Mux(excpValid, s1_itlb_exception(i), ExceptionType.none)
29688895b11Sxu_zh    toWayLookup.bits.meta_corrupt(i)   := excpValid && s1_meta_corrupt(i)
2971a5af821Sxu_zh  }
298b92f8445Sssszwic
299b92f8445Sssszwic  val s1_waymasks_vec = s1_waymasks.map(_.asTypeOf(Vec(nWays, Bool())))
300b92f8445Sssszwic  when(toWayLookup.fire) {
301b92f8445Sssszwic    assert(PopCount(s1_waymasks_vec(0)) <= 1.U && (PopCount(s1_waymasks_vec(1)) <= 1.U || !s1_doubleline),
302b92f8445Sssszwic      "Multiple hit in main pipe, port0:is=%d,ptag=0x%x,vidx=0x%x,vaddr=0x%x port1:is=%d,ptag=0x%x,vidx=0x%x,vaddr=0x%x ",
303b92f8445Sssszwic      PopCount(s1_waymasks_vec(0)) > 1.U, s1_req_ptags(0), get_idx(s1_req_vaddr(0)), s1_req_vaddr(0),
304b92f8445Sssszwic      PopCount(s1_waymasks_vec(1)) > 1.U && s1_doubleline, s1_req_ptags(1), get_idx(s1_req_vaddr(1)), s1_req_vaddr(1))
305b92f8445Sssszwic  }
306b92f8445Sssszwic
307b92f8445Sssszwic  /**
308b92f8445Sssszwic    ******************************************************************************
309b92f8445Sssszwic    * PMP check
310b92f8445Sssszwic    ******************************************************************************
311b92f8445Sssszwic    */
31288895b11Sxu_zh  toPMP.zipWithIndex.foreach { case (p, i) =>
31388895b11Sxu_zh    // if itlb has exception, paddr can be invalid, therefore pmp check can be skipped
31488895b11Sxu_zh    p.valid     := s1_valid // && s1_itlb_exception === ExceptionType.none
315b92f8445Sssszwic    p.bits.addr := s1_req_paddr(i)
316b92f8445Sssszwic    p.bits.size := 3.U // TODO
317b92f8445Sssszwic    p.bits.cmd  := TlbCmd.exec
318b92f8445Sssszwic  }
31988895b11Sxu_zh  val s1_pmp_exception = VecInit(fromPMP.map(ExceptionType.fromPMPResp))
32088895b11Sxu_zh  val s1_mmio          = VecInit(fromPMP.map(_.mmio))
32188895b11Sxu_zh
322f80535c3Sxu_zh  // also raise af when meta array corrupt is detected, to cancel prefetch
323f80535c3Sxu_zh  val s1_meta_exception = VecInit(s1_meta_corrupt.map(ExceptionType.fromECC(io.csr_parity_enable, _)))
324f80535c3Sxu_zh
325f80535c3Sxu_zh  // merge s1 itlb/pmp/meta exceptions, itlb has the highest priority, pmp next, meta lowest
326f80535c3Sxu_zh  val s1_exception_out = ExceptionType.merge(
327f80535c3Sxu_zh    s1_itlb_exception,
328f80535c3Sxu_zh    s1_pmp_exception,
329f80535c3Sxu_zh    s1_meta_exception
330f80535c3Sxu_zh  )
331b92f8445Sssszwic
332b92f8445Sssszwic  /**
333b92f8445Sssszwic    ******************************************************************************
334b92f8445Sssszwic    * state machine
335b92f8445Sssszwic    ******** **********************************************************************
336b92f8445Sssszwic    */
337b92f8445Sssszwic
338b92f8445Sssszwic  switch(state) {
339b92f8445Sssszwic    is(m_idle) {
340*2c9f4a9fSxu_zh      when(s1_valid) {
341*2c9f4a9fSxu_zh        when(!itlb_finish) {
342b92f8445Sssszwic          next_state := m_itlbResend
343*2c9f4a9fSxu_zh        }.elsewhen(!toWayLookup.fire && !s1_isSoftPrefetch) {  // itlb_finish
344b92f8445Sssszwic          next_state := m_enqWay
345*2c9f4a9fSxu_zh        }.elsewhen(!s2_ready) { // itlb_finish && (toWayLookup.fire || s1_isSoftPrefetch)
346b92f8445Sssszwic          next_state := m_enterS2
347*2c9f4a9fSxu_zh        } // .otherwise { next_state := m_idle }
348*2c9f4a9fSxu_zh      } // .otherwise { next_state := m_idle }  // !s1_valid
349b92f8445Sssszwic    }
350b92f8445Sssszwic    is(m_itlbResend) {
351*2c9f4a9fSxu_zh      when(itlb_finish) {
352*2c9f4a9fSxu_zh        when(!toMeta.ready) {
353b92f8445Sssszwic          next_state := m_metaResend
354*2c9f4a9fSxu_zh        }.elsewhen(!s1_isSoftPrefetch) { // toMeta.ready
355b92f8445Sssszwic          next_state := m_enqWay
356*2c9f4a9fSxu_zh        }.elsewhen(!s2_ready) { // toMeta.ready && s1_isSoftPrefetch
357*2c9f4a9fSxu_zh          next_state := m_enterS2
358*2c9f4a9fSxu_zh        }.otherwise { // toMeta.ready && s1_isSoftPrefetch && s2_ready
359*2c9f4a9fSxu_zh          next_state := m_idle
360b92f8445Sssszwic        }
361*2c9f4a9fSxu_zh      } // .otherwise { next_state := m_itlbResend }  // !itlb_finish
362b92f8445Sssszwic    }
363b92f8445Sssszwic    is(m_metaResend) {
364b92f8445Sssszwic      when(toMeta.ready) {
365*2c9f4a9fSxu_zh        when (!s1_isSoftPrefetch) {
366b92f8445Sssszwic          next_state := m_enqWay
367*2c9f4a9fSxu_zh        }.elsewhen(!s2_ready) { // s1_isSoftPrefetch
368*2c9f4a9fSxu_zh          next_state := m_enterS2
369*2c9f4a9fSxu_zh        }.otherwise { // s1_isSoftPrefetch && s2_ready
370*2c9f4a9fSxu_zh          next_state := m_idle
371b92f8445Sssszwic        }
372*2c9f4a9fSxu_zh      } // .otherwise { next_state := m_metaResend }  // !toMeta.ready
373b92f8445Sssszwic    }
374b92f8445Sssszwic    is(m_enqWay) {
375*2c9f4a9fSxu_zh      // sanity check
376*2c9f4a9fSxu_zh      assert(!s1_isSoftPrefetch, "Soft prefetch enters m_enqWay")
377b92f8445Sssszwic      when(toWayLookup.fire && !s2_ready) {
378b92f8445Sssszwic        next_state := m_enterS2
379b92f8445Sssszwic      }.elsewhen(toWayLookup.fire && s2_ready) {
380b92f8445Sssszwic        next_state := m_idle
381b92f8445Sssszwic      }
382b92f8445Sssszwic    }
383b92f8445Sssszwic    is(m_enterS2) {
384b92f8445Sssszwic      when(s2_ready) {
385b92f8445Sssszwic        next_state := m_idle
386b92f8445Sssszwic      }
387b92f8445Sssszwic    }
388b92f8445Sssszwic  }
389b92f8445Sssszwic
390b92f8445Sssszwic  when(s1_flush) {
391b92f8445Sssszwic    next_state := m_idle
392b92f8445Sssszwic  }
393b92f8445Sssszwic
394b92f8445Sssszwic  /** Stage 1 control */
395*2c9f4a9fSxu_zh  from_bpu_s1_flush := s1_valid && !s1_isSoftPrefetch && io.flushFromBpu.shouldFlushByStage3(s1_req_ftqIdx)
396b92f8445Sssszwic  s1_flush := io.flush || from_bpu_s1_flush
397b92f8445Sssszwic
398b92f8445Sssszwic  s1_ready      := next_state === m_idle
399400391a3Sxu_zh  s1_fire       := (next_state === m_idle) && s1_valid && !s1_flush  // used to clear s1_valid & itlb_valid_latch
400400391a3Sxu_zh  val s1_real_fire = s1_fire && io.csr_pf_enable                     // real "s1 fire" that s1 enters s2
401b92f8445Sssszwic
402b92f8445Sssszwic  /**
403b92f8445Sssszwic    ******************************************************************************
404b92f8445Sssszwic    * IPrefetch Stage 2
405b92f8445Sssszwic    * - 1. Monitor the requests from missUnit to write to SRAM.
406b92f8445Sssszwic    * - 2. send req to missUnit
407b92f8445Sssszwic    ******************************************************************************
408b92f8445Sssszwic    */
409400391a3Sxu_zh  val s2_valid  = generatePipeControl(lastFire = s1_real_fire, thisFire = s2_fire, thisFlush = s2_flush, lastFlush = false.B)
410b92f8445Sssszwic
411400391a3Sxu_zh  val s2_req_vaddr    = RegEnable(s1_req_vaddr,     0.U.asTypeOf(s1_req_vaddr),     s1_real_fire)
412*2c9f4a9fSxu_zh  val s2_isSoftPrefetch = RegEnable(s1_isSoftPrefetch, 0.U.asTypeOf(s1_isSoftPrefetch), s1_real_fire)
413400391a3Sxu_zh  val s2_doubleline   = RegEnable(s1_doubleline,    0.U.asTypeOf(s1_doubleline),    s1_real_fire)
414400391a3Sxu_zh  val s2_req_paddr    = RegEnable(s1_req_paddr,     0.U.asTypeOf(s1_req_paddr),     s1_real_fire)
415400391a3Sxu_zh  val s2_exception    = RegEnable(s1_exception_out, 0.U.asTypeOf(s1_exception_out), s1_real_fire)  // includes itlb/pmp/meta exception
416400391a3Sxu_zh  val s2_mmio         = RegEnable(s1_mmio,          0.U.asTypeOf(s1_mmio),          s1_real_fire)
417400391a3Sxu_zh  val s2_waymasks     = RegEnable(s1_waymasks,      0.U.asTypeOf(s1_waymasks),      s1_real_fire)
418b92f8445Sssszwic
41988895b11Sxu_zh  val s2_req_vSetIdx  = s2_req_vaddr.map(get_idx)
42088895b11Sxu_zh  val s2_req_ptags    = s2_req_paddr.map(get_phy_tag)
421b92f8445Sssszwic
422b92f8445Sssszwic  /**
423b92f8445Sssszwic    ******************************************************************************
424b92f8445Sssszwic    * Monitor the requests from missUnit to write to SRAM
425b92f8445Sssszwic    ******************************************************************************
426b92f8445Sssszwic    */
427b808ac73Sxu_zh
428b808ac73Sxu_zh  /* NOTE: If fromMSHR.bits.corrupt, we should set s2_MSHR_hits to false.B, and send prefetch requests again.
429b808ac73Sxu_zh   * This is the opposite of how mainPipe handles fromMSHR.bits.corrupt,
430b808ac73Sxu_zh   *   in which we should set s2_MSHR_hits to true.B, and send error to ifu.
431b808ac73Sxu_zh   */
432b808ac73Sxu_zh  val s2_MSHR_match = VecInit((0 until PortNumber).map(i =>
433b808ac73Sxu_zh    (s2_req_vSetIdx(i) === fromMSHR.bits.vSetIdx) &&
434b92f8445Sssszwic    (s2_req_ptags(i) === getPhyTagFromBlk(fromMSHR.bits.blkPaddr)) &&
435b808ac73Sxu_zh    s2_valid && fromMSHR.valid && !fromMSHR.bits.corrupt
436b808ac73Sxu_zh  ))
437b92f8445Sssszwic  val s2_MSHR_hits = (0 until PortNumber).map(i => ValidHoldBypass(s2_MSHR_match(i), s2_fire || s2_flush))
438b92f8445Sssszwic
439b808ac73Sxu_zh  val s2_SRAM_hits = s2_waymasks.map(_.orR)
440b808ac73Sxu_zh  val s2_hits = VecInit((0 until PortNumber).map(i => s2_MSHR_hits(i) || s2_SRAM_hits(i)))
441b808ac73Sxu_zh
442f80535c3Sxu_zh  /* s2_exception includes itlb pf/gpf/af, pmp af and meta corruption (af), neither of which should be prefetched
44388895b11Sxu_zh   * mmio should not be prefetched
444f80535c3Sxu_zh   * also, if previous has exception, latter port should also not be prefetched
44588895b11Sxu_zh   */
446b808ac73Sxu_zh  val s2_miss = VecInit((0 until PortNumber).map { i =>
447b808ac73Sxu_zh    !s2_hits(i) && (if (i==0) true.B else s2_doubleline) &&
44888895b11Sxu_zh      s2_exception.take(i+1).map(_ === ExceptionType.none).reduce(_&&_) &&
44988895b11Sxu_zh      s2_mmio.take(i+1).map(!_).reduce(_&&_)
450b808ac73Sxu_zh  })
451b92f8445Sssszwic
452b92f8445Sssszwic  /**
453b92f8445Sssszwic    ******************************************************************************
454b92f8445Sssszwic    * send req to missUnit
455b92f8445Sssszwic    ******************************************************************************
456b92f8445Sssszwic    */
457b92f8445Sssszwic  val toMSHRArbiter = Module(new Arbiter(new ICacheMissReq, PortNumber))
458b92f8445Sssszwic
459b92f8445Sssszwic  // To avoid sending duplicate requests.
460b808ac73Sxu_zh  val has_send = RegInit(VecInit(Seq.fill(PortNumber)(false.B)))
461b92f8445Sssszwic  (0 until PortNumber).foreach{ i =>
462400391a3Sxu_zh    when(s1_real_fire) {
463b92f8445Sssszwic      has_send(i) := false.B
464b92f8445Sssszwic    }.elsewhen(toMSHRArbiter.io.in(i).fire) {
465b92f8445Sssszwic      has_send(i) := true.B
466b92f8445Sssszwic    }
467b92f8445Sssszwic  }
468b92f8445Sssszwic
469b92f8445Sssszwic  (0 until PortNumber).map{ i =>
470b92f8445Sssszwic    toMSHRArbiter.io.in(i).valid          := s2_valid && s2_miss(i) && !has_send(i)
471b92f8445Sssszwic    toMSHRArbiter.io.in(i).bits.blkPaddr  := getBlkAddr(s2_req_paddr(i))
472b92f8445Sssszwic    toMSHRArbiter.io.in(i).bits.vSetIdx   := s2_req_vSetIdx(i)
473b92f8445Sssszwic  }
474b92f8445Sssszwic
475b92f8445Sssszwic  toMSHR <> toMSHRArbiter.io.out
476b92f8445Sssszwic
477b92f8445Sssszwic  s2_flush := io.flush
478b92f8445Sssszwic
479b92f8445Sssszwic  val s2_finish  = (0 until PortNumber).map(i => has_send(i) || !s2_miss(i) || toMSHRArbiter.io.in(i).fire).reduce(_&&_)
480b92f8445Sssszwic  s2_ready      := s2_finish || !s2_valid
481b92f8445Sssszwic  s2_fire       := s2_valid && s2_finish && !s2_flush
4829bba777eSssszwic
483cb6e5d3cSssszwic  /** PerfAccumulate */
484*2c9f4a9fSxu_zh  // the number of bpu flush
485*2c9f4a9fSxu_zh  XSPerfAccumulate("bpu_s0_flush", from_bpu_s0_flush)
486*2c9f4a9fSxu_zh  XSPerfAccumulate("bpu_s1_flush", from_bpu_s1_flush)
487*2c9f4a9fSxu_zh  // the number of prefetch request received from ftq or backend (software prefetch)
488*2c9f4a9fSxu_zh//  XSPerfAccumulate("prefetch_req_receive", io.req.fire)
489*2c9f4a9fSxu_zh  XSPerfAccumulate("prefetch_req_receive_hw", io.req.fire && !io.req.bits.isSoftPrefetch)
490*2c9f4a9fSxu_zh  XSPerfAccumulate("prefetch_req_receive_sw", io.req.fire && io.req.bits.isSoftPrefetch)
491b92f8445Sssszwic  // the number of prefetch request sent to missUnit
492*2c9f4a9fSxu_zh//  XSPerfAccumulate("prefetch_req_send", toMSHR.fire)
493*2c9f4a9fSxu_zh  XSPerfAccumulate("prefetch_req_send_hw", toMSHR.fire && !s2_isSoftPrefetch)
494*2c9f4a9fSxu_zh  XSPerfAccumulate("prefetch_req_send_sw", toMSHR.fire && s2_isSoftPrefetch)
495b92f8445Sssszwic  XSPerfAccumulate("to_missUnit_stall", toMSHR.valid && !toMSHR.ready)
496cb6e5d3cSssszwic  /**
497cb6e5d3cSssszwic    * Count the number of requests that are filtered for various reasons.
498cb6e5d3cSssszwic    * The number of prefetch discard in Performance Accumulator may be
499cb6e5d3cSssszwic    * a littel larger the number of really discarded. Because there can
500cb6e5d3cSssszwic    * be multiple reasons for a canceled request at the same time.
501cb6e5d3cSssszwic    */
502b92f8445Sssszwic  // discard prefetch request by flush
503b92f8445Sssszwic  // XSPerfAccumulate("fdip_prefetch_discard_by_tlb_except",  p1_discard && p1_tlb_except)
504b92f8445Sssszwic  // // discard prefetch request by hit icache SRAM
505b92f8445Sssszwic  // XSPerfAccumulate("fdip_prefetch_discard_by_hit_cache",   p2_discard && p1_meta_hit)
506b92f8445Sssszwic  // // discard prefetch request by hit wirte SRAM
507b92f8445Sssszwic  // XSPerfAccumulate("fdip_prefetch_discard_by_p1_monoitor", p1_discard && p1_monitor_hit)
508b92f8445Sssszwic  // // discard prefetch request by pmp except or mmio
509b92f8445Sssszwic  // XSPerfAccumulate("fdip_prefetch_discard_by_pmp",         p2_discard && p2_pmp_except)
510b92f8445Sssszwic  // // discard prefetch request by hit mainPipe info
511b92f8445Sssszwic  // // XSPerfAccumulate("fdip_prefetch_discard_by_mainPipe",    p2_discard && p2_mainPipe_hit)
5127052722fSJay}