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 197052722fSJayimport chisel3._ 207052722fSJayimport chisel3.util._ 217d45a146SYinan Xuimport difftest._ 227052722fSJayimport freechips.rocketchip.tilelink._ 23cf7d6b7aSMuziimport huancun.PreferCacheKey 24cf7d6b7aSMuziimport org.chipsalliance.cde.config.Parameters 25cf7d6b7aSMuziimport utility._ 267052722fSJayimport utils._ 27cf7d6b7aSMuziimport xiangshan.SoftIfetchPrefetchBundle 28cf7d6b7aSMuziimport xiangshan.XSCoreParamsKey 29cf7d6b7aSMuziimport xiangshan.backend.fu.PMPReqBundle 30cf7d6b7aSMuziimport xiangshan.backend.fu.PMPRespBundle 317052722fSJayimport xiangshan.cache.mmu._ 327052722fSJayimport xiangshan.frontend._ 337052722fSJay 347052722fSJayabstract class IPrefetchBundle(implicit p: Parameters) extends ICacheBundle 357052722fSJayabstract class IPrefetchModule(implicit p: Parameters) extends ICacheModule 367052722fSJay 372c9f4a9fSxu_zhclass IPrefetchReq(implicit p: Parameters) extends IPrefetchBundle { 382c9f4a9fSxu_zh val startAddr: UInt = UInt(VAddrBits.W) 392c9f4a9fSxu_zh val nextlineStart: UInt = UInt(VAddrBits.W) 402c9f4a9fSxu_zh val ftqIdx: FtqPtr = new FtqPtr 412c9f4a9fSxu_zh val isSoftPrefetch: Bool = Bool() 42*fbdb359dSMuzi val backendException: UInt = UInt(ExceptionType.width.W) 432c9f4a9fSxu_zh def crossCacheline: Bool = startAddr(blockOffBits - 1) === 1.U 442c9f4a9fSxu_zh 452c9f4a9fSxu_zh def fromFtqICacheInfo(info: FtqICacheInfo): IPrefetchReq = { 462c9f4a9fSxu_zh this.startAddr := info.startAddr 472c9f4a9fSxu_zh this.nextlineStart := info.nextlineStart 482c9f4a9fSxu_zh this.ftqIdx := info.ftqIdx 492c9f4a9fSxu_zh this.isSoftPrefetch := false.B 502c9f4a9fSxu_zh this 512c9f4a9fSxu_zh } 522c9f4a9fSxu_zh 532c9f4a9fSxu_zh def fromSoftPrefetch(req: SoftIfetchPrefetchBundle): IPrefetchReq = { 542c9f4a9fSxu_zh this.startAddr := req.vaddr 552c9f4a9fSxu_zh this.nextlineStart := req.vaddr + (1 << blockOffBits).U 562c9f4a9fSxu_zh this.ftqIdx := DontCare 572c9f4a9fSxu_zh this.isSoftPrefetch := true.B 582c9f4a9fSxu_zh this 592c9f4a9fSxu_zh } 602c9f4a9fSxu_zh} 612c9f4a9fSxu_zh 6288895b11Sxu_zhclass IPrefetchIO(implicit p: Parameters) extends IPrefetchBundle { 63b92f8445Sssszwic // control 64b92f8445Sssszwic val csr_pf_enable = Input(Bool()) 65f80535c3Sxu_zh val csr_parity_enable = Input(Bool()) 66b92f8445Sssszwic val flush = Input(Bool()) 6758c354d0Sssszwic 682c9f4a9fSxu_zh val req = Flipped(Decoupled(new IPrefetchReq)) 692c9f4a9fSxu_zh val flushFromBpu = Flipped(new BpuFlushInfo) 70b92f8445Sssszwic val itlb = Vec(PortNumber, new TlbRequestIO) 71b92f8445Sssszwic val pmp = Vec(PortNumber, new ICachePMPBundle) 72b92f8445Sssszwic val metaRead = new ICacheMetaReqBundle 73b92f8445Sssszwic val MSHRReq = DecoupledIO(new ICacheMissReq) 74b92f8445Sssszwic val MSHRResp = Flipped(ValidIO(new ICacheMissResp)) 75b92f8445Sssszwic val wayLookupWrite = DecoupledIO(new WayLookupInfo) 767052722fSJay} 777052722fSJay 78cf7d6b7aSMuziclass IPrefetchPipe(implicit p: Parameters) extends IPrefetchModule { 7988895b11Sxu_zh val io: IPrefetchIO = IO(new IPrefetchIO) 807052722fSJay 81b92f8445Sssszwic val (toITLB, fromITLB) = (io.itlb.map(_.req), io.itlb.map(_.resp)) 82b92f8445Sssszwic val (toPMP, fromPMP) = (io.pmp.map(_.req), io.pmp.map(_.resp)) 83b92f8445Sssszwic val (toMeta, fromMeta) = (io.metaRead.toIMeta, io.metaRead.fromIMeta) 84b92f8445Sssszwic val (toMSHR, fromMSHR) = (io.MSHRReq, io.MSHRResp) 85b92f8445Sssszwic val toWayLookup = io.wayLookupWrite 867052722fSJay 87b92f8445Sssszwic val s0_fire, s1_fire, s2_fire = WireInit(false.B) 88b92f8445Sssszwic val s0_discard, s2_discard = WireInit(false.B) 89b92f8445Sssszwic val s0_ready, s1_ready, s2_ready = WireInit(false.B) 90b92f8445Sssszwic val s0_flush, s1_flush, s2_flush = WireInit(false.B) 91b92f8445Sssszwic val from_bpu_s0_flush, from_bpu_s1_flush = WireInit(false.B) 927052722fSJay 93cb6e5d3cSssszwic /** 94cb6e5d3cSssszwic ****************************************************************************** 95cb6e5d3cSssszwic * IPrefetch Stage 0 96b92f8445Sssszwic * - 1. receive ftq req 97b92f8445Sssszwic * - 2. send req to ITLB 98b92f8445Sssszwic * - 3. send req to Meta SRAM 99cb6e5d3cSssszwic ****************************************************************************** 100cb6e5d3cSssszwic */ 1012c9f4a9fSxu_zh val s0_valid = io.req.valid 102cb6e5d3cSssszwic 103b92f8445Sssszwic /** 104b92f8445Sssszwic ****************************************************************************** 105b92f8445Sssszwic * receive ftq req 106b92f8445Sssszwic ****************************************************************************** 107b92f8445Sssszwic */ 1082c9f4a9fSxu_zh val s0_req_vaddr = VecInit(Seq(io.req.bits.startAddr, io.req.bits.nextlineStart)) 1092c9f4a9fSxu_zh val s0_req_ftqIdx = io.req.bits.ftqIdx 1102c9f4a9fSxu_zh val s0_isSoftPrefetch = io.req.bits.isSoftPrefetch 1112c9f4a9fSxu_zh val s0_doubleline = io.req.bits.crossCacheline 11288895b11Sxu_zh val s0_req_vSetIdx = s0_req_vaddr.map(get_idx) 113*fbdb359dSMuzi val s0_backendException = VecInit(Seq.fill(PortNumber)(io.req.bits.backendException)) 1147052722fSJay 1152c9f4a9fSxu_zh from_bpu_s0_flush := !s0_isSoftPrefetch && (io.flushFromBpu.shouldFlushByStage2(s0_req_ftqIdx) || 1162c9f4a9fSxu_zh io.flushFromBpu.shouldFlushByStage3(s0_req_ftqIdx)) 117b92f8445Sssszwic s0_flush := io.flush || from_bpu_s0_flush || s1_flush 1187052722fSJay 119b92f8445Sssszwic val s0_can_go = s1_ready && toITLB(0).ready && toITLB(1).ready && toMeta.ready 1202c9f4a9fSxu_zh io.req.ready := s0_can_go 1217052722fSJay 122b92f8445Sssszwic s0_fire := s0_valid && s0_can_go && !s0_flush 123cb6e5d3cSssszwic 124cb6e5d3cSssszwic /** 125cb6e5d3cSssszwic ****************************************************************************** 126cb6e5d3cSssszwic * IPrefetch Stage 1 127b92f8445Sssszwic * - 1. Receive resp from ITLB 128b92f8445Sssszwic * - 2. Receive resp from IMeta and check 129b92f8445Sssszwic * - 3. Monitor the requests from missUnit to write to SRAM. 130b92f8445Sssszwic * - 4. Wirte wayLookup 131cb6e5d3cSssszwic ****************************************************************************** 132cb6e5d3cSssszwic */ 133b92f8445Sssszwic val s1_valid = generatePipeControl(lastFire = s0_fire, thisFire = s1_fire, thisFlush = s1_flush, lastFlush = false.B) 134cb6e5d3cSssszwic 135b92f8445Sssszwic val s1_req_vaddr = RegEnable(s0_req_vaddr, 0.U.asTypeOf(s0_req_vaddr), s0_fire) 1362c9f4a9fSxu_zh val s1_isSoftPrefetch = RegEnable(s0_isSoftPrefetch, 0.U.asTypeOf(s0_isSoftPrefetch), s0_fire) 137b92f8445Sssszwic val s1_doubleline = RegEnable(s0_doubleline, 0.U.asTypeOf(s0_doubleline), s0_fire) 138b92f8445Sssszwic val s1_req_ftqIdx = RegEnable(s0_req_ftqIdx, 0.U.asTypeOf(s0_req_ftqIdx), s0_fire) 13988895b11Sxu_zh val s1_req_vSetIdx = VecInit(s1_req_vaddr.map(get_idx)) 140*fbdb359dSMuzi val s1_backendException = RegEnable(s0_backendException, 0.U.asTypeOf(s0_backendException), s0_fire) 1417052722fSJay 142b92f8445Sssszwic val m_idle :: m_itlbResend :: m_metaResend :: m_enqWay :: m_enterS2 :: Nil = Enum(5) 143b92f8445Sssszwic val state = RegInit(m_idle) 144b92f8445Sssszwic val next_state = WireDefault(state) 145b92f8445Sssszwic val s0_fire_r = RegNext(s0_fire) 146b92f8445Sssszwic dontTouch(state) 147b92f8445Sssszwic dontTouch(next_state) 148b92f8445Sssszwic state := next_state 1497052722fSJay 150b92f8445Sssszwic /** 151b92f8445Sssszwic ****************************************************************************** 152b92f8445Sssszwic * resend itlb req if miss 153b92f8445Sssszwic ****************************************************************************** 154b92f8445Sssszwic */ 155b92f8445Sssszwic val s1_wait_itlb = RegInit(VecInit(Seq.fill(PortNumber)(false.B))) 156b92f8445Sssszwic (0 until PortNumber).foreach { i => 157b92f8445Sssszwic when(s1_flush) { 158b92f8445Sssszwic s1_wait_itlb(i) := false.B 159b92f8445Sssszwic }.elsewhen(RegNext(s0_fire) && fromITLB(i).bits.miss) { 160b92f8445Sssszwic s1_wait_itlb(i) := true.B 161b92f8445Sssszwic }.elsewhen(s1_wait_itlb(i) && !fromITLB(i).bits.miss) { 162b92f8445Sssszwic s1_wait_itlb(i) := false.B 163b92f8445Sssszwic } 164b92f8445Sssszwic } 165cf7d6b7aSMuzi val s1_need_itlb = VecInit(Seq( 166cf7d6b7aSMuzi (RegNext(s0_fire) || s1_wait_itlb(0)) && fromITLB(0).bits.miss, 167cf7d6b7aSMuzi (RegNext(s0_fire) || s1_wait_itlb(1)) && fromITLB(1).bits.miss && s1_doubleline 168cf7d6b7aSMuzi )) 169cf7d6b7aSMuzi val tlb_valid_pulse = VecInit(Seq( 170cf7d6b7aSMuzi (RegNext(s0_fire) || s1_wait_itlb(0)) && !fromITLB(0).bits.miss, 171cf7d6b7aSMuzi (RegNext(s0_fire) || s1_wait_itlb(1)) && !fromITLB(1).bits.miss && s1_doubleline 172cf7d6b7aSMuzi )) 173cf7d6b7aSMuzi val tlb_valid_latch = 174cf7d6b7aSMuzi VecInit((0 until PortNumber).map(i => ValidHoldBypass(tlb_valid_pulse(i), s1_fire, flush = s1_flush))) 175b92f8445Sssszwic val itlb_finish = tlb_valid_latch(0) && (!s1_doubleline || tlb_valid_latch(1)) 1767052722fSJay 177b92f8445Sssszwic for (i <- 0 until PortNumber) { 178b92f8445Sssszwic toITLB(i).valid := s1_need_itlb(i) || (s0_valid && (if (i == 0) true.B else s0_doubleline)) 179b92f8445Sssszwic toITLB(i).bits := DontCare 180b92f8445Sssszwic toITLB(i).bits.size := 3.U 181b92f8445Sssszwic toITLB(i).bits.vaddr := Mux(s1_need_itlb(i), s1_req_vaddr(i), s0_req_vaddr(i)) 182b92f8445Sssszwic toITLB(i).bits.debug.pc := Mux(s1_need_itlb(i), s1_req_vaddr(i), s0_req_vaddr(i)) 183b92f8445Sssszwic toITLB(i).bits.cmd := TlbCmd.exec 184b92f8445Sssszwic toITLB(i).bits.no_translate := false.B 185b92f8445Sssszwic } 186b92f8445Sssszwic fromITLB.foreach(_.ready := true.B) 187b92f8445Sssszwic io.itlb.foreach(_.req_kill := false.B) 1887052722fSJay 189b92f8445Sssszwic /** 190b92f8445Sssszwic ****************************************************************************** 191b92f8445Sssszwic * Receive resp from ITLB 192b92f8445Sssszwic ****************************************************************************** 193b92f8445Sssszwic */ 194b92f8445Sssszwic val s1_req_paddr_wire = VecInit(fromITLB.map(_.bits.paddr(0))) 195b92f8445Sssszwic val s1_req_paddr_reg = VecInit((0 until PortNumber).map(i => 19688895b11Sxu_zh RegEnable(s1_req_paddr_wire(i), 0.U(PAddrBits.W), tlb_valid_pulse(i)) 19788895b11Sxu_zh )) 198b92f8445Sssszwic val s1_req_paddr = VecInit((0 until PortNumber).map(i => 19988895b11Sxu_zh Mux(tlb_valid_pulse(i), s1_req_paddr_wire(i), s1_req_paddr_reg(i)) 20088895b11Sxu_zh )) 20191946104Sxu_zh val s1_req_gpaddr_tmp = VecInit((0 until PortNumber).map(i => 202cf7d6b7aSMuzi ResultHoldBypass( 203cf7d6b7aSMuzi valid = tlb_valid_pulse(i), 204cf7d6b7aSMuzi init = 0.U.asTypeOf(fromITLB(i).bits.gpaddr(0)), 205cf7d6b7aSMuzi data = fromITLB(i).bits.gpaddr(0) 206cf7d6b7aSMuzi ) 20788895b11Sxu_zh )) 208ad415ae0SXiaokun-Pei val s1_req_isForVSnonLeafPTE_tmp = VecInit((0 until PortNumber).map(i => 209cf7d6b7aSMuzi ResultHoldBypass( 210cf7d6b7aSMuzi valid = tlb_valid_pulse(i), 211cf7d6b7aSMuzi init = 0.U.asTypeOf(fromITLB(i).bits.isForVSnonLeafPTE), 212cf7d6b7aSMuzi data = fromITLB(i).bits.isForVSnonLeafPTE 213cf7d6b7aSMuzi ) 214ad415ae0SXiaokun-Pei )) 21588895b11Sxu_zh val s1_itlb_exception = VecInit((0 until PortNumber).map(i => 216cf7d6b7aSMuzi ResultHoldBypass( 217cf7d6b7aSMuzi valid = tlb_valid_pulse(i), 218cf7d6b7aSMuzi init = 0.U(ExceptionType.width.W), 219cf7d6b7aSMuzi data = ExceptionType.fromTlbResp(fromITLB(i).bits) 220cf7d6b7aSMuzi ) 22188895b11Sxu_zh )) 222002c10a4SYanqin Li val s1_itlb_pbmt = VecInit((0 until PortNumber).map(i => 223cf7d6b7aSMuzi ResultHoldBypass( 224cf7d6b7aSMuzi valid = tlb_valid_pulse(i), 225cf7d6b7aSMuzi init = 0.U.asTypeOf(fromITLB(i).bits.pbmt(0)), 226cf7d6b7aSMuzi data = fromITLB(i).bits.pbmt(0) 227cf7d6b7aSMuzi ) 228002c10a4SYanqin Li )) 22988895b11Sxu_zh val s1_itlb_exception_gpf = VecInit(s1_itlb_exception.map(_ === ExceptionType.gpf)) 230b92f8445Sssszwic 23191946104Sxu_zh /* Select gpaddr with the first gpf 23291946104Sxu_zh * Note: the backend wants the base guest physical address of a fetch block 23391946104Sxu_zh * for port(i), its base gpaddr is actually (gpaddr - i * blocksize) 23491946104Sxu_zh * see GPAMem: https://github.com/OpenXiangShan/XiangShan/blob/344cf5d55568dd40cd658a9ee66047a505eeb504/src/main/scala/xiangshan/backend/GPAMem.scala#L33-L34 23591946104Sxu_zh * see also: https://github.com/OpenXiangShan/XiangShan/blob/344cf5d55568dd40cd658a9ee66047a505eeb504/src/main/scala/xiangshan/frontend/IFU.scala#L374-L375 23691946104Sxu_zh */ 23791946104Sxu_zh val s1_req_gpaddr = PriorityMuxDefault( 23888895b11Sxu_zh s1_itlb_exception_gpf zip (0 until PortNumber).map(i => s1_req_gpaddr_tmp(i) - (i << blockOffBits).U), 23991946104Sxu_zh 0.U.asTypeOf(s1_req_gpaddr_tmp(0)) 24091946104Sxu_zh ) 24191946104Sxu_zh 242ad415ae0SXiaokun-Pei val s1_req_isForVSnonLeafPTE = PriorityMuxDefault( 243ad415ae0SXiaokun-Pei s1_itlb_exception_gpf zip s1_req_isForVSnonLeafPTE_tmp, 244ad415ae0SXiaokun-Pei 0.U.asTypeOf(s1_req_isForVSnonLeafPTE_tmp(0)) 245ad415ae0SXiaokun-Pei ) 246ad415ae0SXiaokun-Pei 247b92f8445Sssszwic /** 248b92f8445Sssszwic ****************************************************************************** 249b92f8445Sssszwic * resend metaArray read req when itlb miss finish 250b92f8445Sssszwic ****************************************************************************** 251b92f8445Sssszwic */ 252b92f8445Sssszwic val s1_need_meta = ((state === m_itlbResend) && itlb_finish) || (state === m_metaResend) 253b92f8445Sssszwic toMeta.valid := s1_need_meta || s0_valid 254b92f8445Sssszwic toMeta.bits := DontCare 255b92f8445Sssszwic toMeta.bits.isDoubleLine := Mux(s1_need_meta, s1_doubleline, s0_doubleline) 256b92f8445Sssszwic 257b92f8445Sssszwic for (i <- 0 until PortNumber) { 258b92f8445Sssszwic toMeta.bits.vSetIdx(i) := Mux(s1_need_meta, s1_req_vSetIdx(i), s0_req_vSetIdx(i)) 259cb6e5d3cSssszwic } 260cb6e5d3cSssszwic 261cb6e5d3cSssszwic /** 262cb6e5d3cSssszwic ****************************************************************************** 263b92f8445Sssszwic * Receive resp from IMeta and check 264cb6e5d3cSssszwic ****************************************************************************** 265cb6e5d3cSssszwic */ 26688895b11Sxu_zh val s1_req_ptags = VecInit(s1_req_paddr.map(get_phy_tag)) 267cb6e5d3cSssszwic 268b92f8445Sssszwic val s1_meta_ptags = fromMeta.tags 269b92f8445Sssszwic val s1_meta_valids = fromMeta.entryValid 2709bba777eSssszwic 271b92f8445Sssszwic def get_waymask(paddrs: Vec[UInt]): Vec[UInt] = { 27288895b11Sxu_zh val ptags = paddrs.map(get_phy_tag) 273cf7d6b7aSMuzi val tag_eq_vec = 274cf7d6b7aSMuzi VecInit((0 until PortNumber).map(p => VecInit((0 until nWays).map(w => s1_meta_ptags(p)(w) === ptags(p))))) 275cf7d6b7aSMuzi val tag_match_vec = VecInit((0 until PortNumber).map(k => 276cf7d6b7aSMuzi VecInit(tag_eq_vec(k).zipWithIndex.map { case (way_tag_eq, w) => way_tag_eq && s1_meta_valids(k)(w) }) 277cf7d6b7aSMuzi )) 278b92f8445Sssszwic val waymasks = VecInit(tag_match_vec.map(_.asUInt)) 279b92f8445Sssszwic waymasks 280cb6e5d3cSssszwic } 2819bba777eSssszwic 2825ce94708Sxu_zh val s1_SRAM_waymasks = VecInit((0 until PortNumber).map { port => 2835ce94708Sxu_zh Mux(tlb_valid_pulse(port), get_waymask(s1_req_paddr_wire)(port), get_waymask(s1_req_paddr_reg)(port)) 2845ce94708Sxu_zh }) 285b92f8445Sssszwic 2868966a895Sxu_zh // select ecc code 2878966a895Sxu_zh /* NOTE: 2888966a895Sxu_zh * When ECC check fails, s1_waymasks may be corrupted, so this selected meta_codes may be wrong. 2898966a895Sxu_zh * However, we can guarantee that the request sent to the l2 cache and the response to the IFU are both correct, 2908966a895Sxu_zh * considering the probability of bit flipping abnormally is very small, consider there's up to 1 bit being wrong: 2918966a895Sxu_zh * 1. miss -> fake hit: The wrong bit in s1_waymasks was set to true.B, thus selects the wrong meta_codes, 2928966a895Sxu_zh * but we can detect this by checking whether `encodeMetaECC(req_ptags) === meta_codes`. 2938966a895Sxu_zh * 2. hit -> fake multi-hit: In normal situation, multi-hit never happens, so multi-hit indicates ECC failure, 2948966a895Sxu_zh * we can detect this by checking whether `PopCount(waymasks) <= 1.U`, 2958966a895Sxu_zh * and meta_codes is not important in this situation. 2968966a895Sxu_zh * 3. hit -> fake miss: We can't detect this, but we can (pre)fetch the correct data from L2 cache, so it's not a problem. 2978966a895Sxu_zh * 4. hit -> hit / miss -> miss: ECC failure happens in a irrelevant way, so we don't care about it this time. 2988966a895Sxu_zh */ 2995ce94708Sxu_zh val s1_SRAM_meta_codes = VecInit((0 until PortNumber).map { port => 3005ce94708Sxu_zh Mux1H(s1_SRAM_waymasks(port), fromMeta.codes(port)) 3018966a895Sxu_zh }) 3028966a895Sxu_zh 303b92f8445Sssszwic /** 304b92f8445Sssszwic ****************************************************************************** 3055ce94708Sxu_zh * update waymasks and meta_codes according to MSHR update data 3065ce94708Sxu_zh ****************************************************************************** 3075ce94708Sxu_zh */ 3085ce94708Sxu_zh def update_meta_info(mask: UInt, vSetIdx: UInt, ptag: UInt, code: UInt): Tuple2[UInt, UInt] = { 3095ce94708Sxu_zh require(mask.getWidth == nWays) 3105ce94708Sxu_zh val new_mask = WireInit(mask) 3115ce94708Sxu_zh val new_code = WireInit(code) 3125ce94708Sxu_zh val valid = fromMSHR.valid && !fromMSHR.bits.corrupt 3135ce94708Sxu_zh val vset_same = fromMSHR.bits.vSetIdx === vSetIdx 3145ce94708Sxu_zh val ptag_same = getPhyTagFromBlk(fromMSHR.bits.blkPaddr) === ptag 3155ce94708Sxu_zh val way_same = fromMSHR.bits.waymask === mask 3165ce94708Sxu_zh when(valid && vset_same) { 3175ce94708Sxu_zh when(ptag_same) { 3185ce94708Sxu_zh new_mask := fromMSHR.bits.waymask 3195ce94708Sxu_zh // also update meta_codes 3205ce94708Sxu_zh // we have getPhyTagFromBlk(fromMSHR.bits.blkPaddr) === ptag, so we can use ptag directly for better timing 3215ce94708Sxu_zh new_code := encodeMetaECC(ptag) 3225ce94708Sxu_zh }.elsewhen(way_same) { 3235ce94708Sxu_zh new_mask := 0.U 3245ce94708Sxu_zh // we dont care about new_code, since it's not used for a missed request 3255ce94708Sxu_zh } 3265ce94708Sxu_zh } 3275ce94708Sxu_zh (new_mask, new_code) 3285ce94708Sxu_zh } 3295ce94708Sxu_zh 3305ce94708Sxu_zh val s1_SRAM_valid = s0_fire_r || RegNext(s1_need_meta && toMeta.ready) 3315ce94708Sxu_zh val s1_MSHR_valid = fromMSHR.valid && !fromMSHR.bits.corrupt 3325ce94708Sxu_zh val s1_waymasks = WireInit(VecInit(Seq.fill(PortNumber)(0.U(nWays.W)))) 3335ce94708Sxu_zh val s1_waymasks_r = RegEnable(s1_waymasks, 0.U.asTypeOf(s1_waymasks), s1_SRAM_valid || s1_MSHR_valid) 3345ce94708Sxu_zh val s1_meta_codes = WireInit(VecInit(Seq.fill(PortNumber)(0.U(ICacheMetaCodeBits.W)))) 3355ce94708Sxu_zh val s1_meta_codes_r = RegEnable(s1_meta_codes, 0.U.asTypeOf(s1_meta_codes), s1_SRAM_valid || s1_MSHR_valid) 3365ce94708Sxu_zh 3375ce94708Sxu_zh // update waymasks and meta_codes 3385ce94708Sxu_zh (0 until PortNumber).foreach { i => 3395ce94708Sxu_zh val old_waymask = Mux(s1_SRAM_valid, s1_SRAM_waymasks(i), s1_waymasks_r(i)) 3405ce94708Sxu_zh val old_meta_codes = Mux(s1_SRAM_valid, s1_SRAM_meta_codes(i), s1_meta_codes_r(i)) 3415ce94708Sxu_zh val new_info = update_meta_info(old_waymask, s1_req_vSetIdx(i), s1_req_ptags(i), old_meta_codes) 3425ce94708Sxu_zh s1_waymasks(i) := new_info._1 3435ce94708Sxu_zh s1_meta_codes(i) := new_info._2 3445ce94708Sxu_zh } 3455ce94708Sxu_zh 3465ce94708Sxu_zh /** 3475ce94708Sxu_zh ****************************************************************************** 348b92f8445Sssszwic * send enqueu req to WayLookup 349b92f8445Sssszwic ******** ********************************************************************** 350b92f8445Sssszwic */ 351b92f8445Sssszwic // Disallow enqueuing wayLookup when SRAM write occurs. 3522c9f4a9fSxu_zh toWayLookup.valid := ((state === m_enqWay) || ((state === m_idle) && itlb_finish)) && 3532c9f4a9fSxu_zh !s1_flush && !fromMSHR.valid && !s1_isSoftPrefetch // do not enqueue soft prefetch 354b92f8445Sssszwic toWayLookup.bits.vSetIdx := s1_req_vSetIdx 355b92f8445Sssszwic toWayLookup.bits.waymask := s1_waymasks 356b92f8445Sssszwic toWayLookup.bits.ptag := s1_req_ptags 357b92f8445Sssszwic toWayLookup.bits.gpaddr := s1_req_gpaddr 358ad415ae0SXiaokun-Pei toWayLookup.bits.isForVSnonLeafPTE := s1_req_isForVSnonLeafPTE 3598966a895Sxu_zh toWayLookup.bits.meta_codes := s1_meta_codes 3601a5af821Sxu_zh (0 until PortNumber).foreach { i => 361cf7d6b7aSMuzi val excpValid = if (i == 0) true.B 362cf7d6b7aSMuzi else s1_doubleline // exception in first line is always valid, in second line is valid iff is doubleline request 36388895b11Sxu_zh // Send s1_itlb_exception to WayLookup (instead of s1_exception_out) for better timing. Will check pmp again in mainPipe 36488895b11Sxu_zh toWayLookup.bits.itlb_exception(i) := Mux(excpValid, s1_itlb_exception(i), ExceptionType.none) 365002c10a4SYanqin Li toWayLookup.bits.itlb_pbmt(i) := Mux(excpValid, s1_itlb_pbmt(i), Pbmt.pma) 3661a5af821Sxu_zh } 367b92f8445Sssszwic 368b92f8445Sssszwic val s1_waymasks_vec = s1_waymasks.map(_.asTypeOf(Vec(nWays, Bool()))) 369b92f8445Sssszwic when(toWayLookup.fire) { 370cf7d6b7aSMuzi assert( 371cf7d6b7aSMuzi PopCount(s1_waymasks_vec(0)) <= 1.U && (PopCount(s1_waymasks_vec(1)) <= 1.U || !s1_doubleline), 372b92f8445Sssszwic "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 ", 373cf7d6b7aSMuzi PopCount(s1_waymasks_vec(0)) > 1.U, 374cf7d6b7aSMuzi s1_req_ptags(0), 375cf7d6b7aSMuzi get_idx(s1_req_vaddr(0)), 376cf7d6b7aSMuzi s1_req_vaddr(0), 377cf7d6b7aSMuzi PopCount(s1_waymasks_vec(1)) > 1.U && s1_doubleline, 378cf7d6b7aSMuzi s1_req_ptags(1), 379cf7d6b7aSMuzi get_idx(s1_req_vaddr(1)), 380cf7d6b7aSMuzi s1_req_vaddr(1) 381cf7d6b7aSMuzi ) 382b92f8445Sssszwic } 383b92f8445Sssszwic 384b92f8445Sssszwic /** 385b92f8445Sssszwic ****************************************************************************** 386b92f8445Sssszwic * PMP check 387b92f8445Sssszwic ****************************************************************************** 388b92f8445Sssszwic */ 38988895b11Sxu_zh toPMP.zipWithIndex.foreach { case (p, i) => 39088895b11Sxu_zh // if itlb has exception, paddr can be invalid, therefore pmp check can be skipped 39188895b11Sxu_zh p.valid := s1_valid // && s1_itlb_exception === ExceptionType.none 392b92f8445Sssszwic p.bits.addr := s1_req_paddr(i) 393b92f8445Sssszwic p.bits.size := 3.U // TODO 394b92f8445Sssszwic p.bits.cmd := TlbCmd.exec 395b92f8445Sssszwic } 39688895b11Sxu_zh val s1_pmp_exception = VecInit(fromPMP.map(ExceptionType.fromPMPResp)) 397002c10a4SYanqin Li val s1_pmp_mmio = VecInit(fromPMP.map(_.mmio)) 39888895b11Sxu_zh 3998966a895Sxu_zh // merge s1 itlb/pmp exceptions, itlb has the highest priority, pmp next 4008966a895Sxu_zh // for timing consideration, meta_corrupt is not merged, and it will NOT cancel prefetch 401f80535c3Sxu_zh val s1_exception_out = ExceptionType.merge( 402*fbdb359dSMuzi s1_backendException, 403f80535c3Sxu_zh s1_itlb_exception, 4048966a895Sxu_zh s1_pmp_exception 405f80535c3Sxu_zh ) 406b92f8445Sssszwic 407002c10a4SYanqin Li // merge pmp mmio and itlb pbmt 408002c10a4SYanqin Li val s1_mmio = VecInit((s1_pmp_mmio zip s1_itlb_pbmt).map { case (mmio, pbmt) => 409002c10a4SYanqin Li mmio || Pbmt.isUncache(pbmt) 410002c10a4SYanqin Li }) 411002c10a4SYanqin Li 412b92f8445Sssszwic /** 413b92f8445Sssszwic ****************************************************************************** 414b92f8445Sssszwic * state machine 415b92f8445Sssszwic ******** ********************************************************************** 416b92f8445Sssszwic */ 417b92f8445Sssszwic 418b92f8445Sssszwic switch(state) { 419b92f8445Sssszwic is(m_idle) { 4202c9f4a9fSxu_zh when(s1_valid) { 4212c9f4a9fSxu_zh when(!itlb_finish) { 422b92f8445Sssszwic next_state := m_itlbResend 4238c57174eSxu_zh }.elsewhen(!toWayLookup.fire) { // itlb_finish 424b92f8445Sssszwic next_state := m_enqWay 4258c57174eSxu_zh }.elsewhen(!s2_ready) { // itlb_finish && toWayLookup.fire 426b92f8445Sssszwic next_state := m_enterS2 4272c9f4a9fSxu_zh } // .otherwise { next_state := m_idle } 4282c9f4a9fSxu_zh } // .otherwise { next_state := m_idle } // !s1_valid 429b92f8445Sssszwic } 430b92f8445Sssszwic is(m_itlbResend) { 4312c9f4a9fSxu_zh when(itlb_finish) { 4322c9f4a9fSxu_zh when(!toMeta.ready) { 433b92f8445Sssszwic next_state := m_metaResend 4348c57174eSxu_zh }.otherwise { // toMeta.ready 435b92f8445Sssszwic next_state := m_enqWay 436b92f8445Sssszwic } 4372c9f4a9fSxu_zh } // .otherwise { next_state := m_itlbResend } // !itlb_finish 438b92f8445Sssszwic } 439b92f8445Sssszwic is(m_metaResend) { 440b92f8445Sssszwic when(toMeta.ready) { 441b92f8445Sssszwic next_state := m_enqWay 4422c9f4a9fSxu_zh } // .otherwise { next_state := m_metaResend } // !toMeta.ready 443b92f8445Sssszwic } 444b92f8445Sssszwic is(m_enqWay) { 4458c57174eSxu_zh when(toWayLookup.fire || s1_isSoftPrefetch) { 4468c57174eSxu_zh when(!s2_ready) { 447b92f8445Sssszwic next_state := m_enterS2 4488c57174eSxu_zh }.otherwise { // s2_ready 449b92f8445Sssszwic next_state := m_idle 450b92f8445Sssszwic } 4518c57174eSxu_zh } // .otherwise { next_state := m_enqWay } 452b92f8445Sssszwic } 453b92f8445Sssszwic is(m_enterS2) { 454b92f8445Sssszwic when(s2_ready) { 455b92f8445Sssszwic next_state := m_idle 456b92f8445Sssszwic } 457b92f8445Sssszwic } 458b92f8445Sssszwic } 459b92f8445Sssszwic 460b92f8445Sssszwic when(s1_flush) { 461b92f8445Sssszwic next_state := m_idle 462b92f8445Sssszwic } 463b92f8445Sssszwic 464b92f8445Sssszwic /** Stage 1 control */ 4652c9f4a9fSxu_zh from_bpu_s1_flush := s1_valid && !s1_isSoftPrefetch && io.flushFromBpu.shouldFlushByStage3(s1_req_ftqIdx) 466b92f8445Sssszwic s1_flush := io.flush || from_bpu_s1_flush 467b92f8445Sssszwic 468b92f8445Sssszwic s1_ready := next_state === m_idle 469400391a3Sxu_zh s1_fire := (next_state === m_idle) && s1_valid && !s1_flush // used to clear s1_valid & itlb_valid_latch 470400391a3Sxu_zh val s1_real_fire = s1_fire && io.csr_pf_enable // real "s1 fire" that s1 enters s2 471b92f8445Sssszwic 472b92f8445Sssszwic /** 473b92f8445Sssszwic ****************************************************************************** 474b92f8445Sssszwic * IPrefetch Stage 2 475b92f8445Sssszwic * - 1. Monitor the requests from missUnit to write to SRAM. 476b92f8445Sssszwic * - 2. send req to missUnit 477b92f8445Sssszwic ****************************************************************************** 478b92f8445Sssszwic */ 479cf7d6b7aSMuzi val s2_valid = 480cf7d6b7aSMuzi generatePipeControl(lastFire = s1_real_fire, thisFire = s2_fire, thisFlush = s2_flush, lastFlush = false.B) 481b92f8445Sssszwic 482400391a3Sxu_zh val s2_req_vaddr = RegEnable(s1_req_vaddr, 0.U.asTypeOf(s1_req_vaddr), s1_real_fire) 4832c9f4a9fSxu_zh val s2_isSoftPrefetch = RegEnable(s1_isSoftPrefetch, 0.U.asTypeOf(s1_isSoftPrefetch), s1_real_fire) 484400391a3Sxu_zh val s2_doubleline = RegEnable(s1_doubleline, 0.U.asTypeOf(s1_doubleline), s1_real_fire) 485400391a3Sxu_zh val s2_req_paddr = RegEnable(s1_req_paddr, 0.U.asTypeOf(s1_req_paddr), s1_real_fire) 486cf7d6b7aSMuzi val s2_exception = 487cf7d6b7aSMuzi RegEnable(s1_exception_out, 0.U.asTypeOf(s1_exception_out), s1_real_fire) // includes itlb/pmp exception 4888966a895Sxu_zh// val s2_exception_in = RegEnable(s1_exception_out, 0.U.asTypeOf(s1_exception_out), s1_real_fire) // disabled for timing consideration 489400391a3Sxu_zh val s2_mmio = RegEnable(s1_mmio, 0.U.asTypeOf(s1_mmio), s1_real_fire) 490400391a3Sxu_zh val s2_waymasks = RegEnable(s1_waymasks, 0.U.asTypeOf(s1_waymasks), s1_real_fire) 4918966a895Sxu_zh// val s2_meta_codes = RegEnable(s1_meta_codes, 0.U.asTypeOf(s1_meta_codes), s1_real_fire) // disabled for timing consideration 492b92f8445Sssszwic 49388895b11Sxu_zh val s2_req_vSetIdx = s2_req_vaddr.map(get_idx) 49488895b11Sxu_zh val s2_req_ptags = s2_req_paddr.map(get_phy_tag) 495b92f8445Sssszwic 4968966a895Sxu_zh // disabled for timing consideration 4978966a895Sxu_zh// // do metaArray ECC check 4988966a895Sxu_zh// val s2_meta_corrupt = VecInit((s2_req_ptags zip s2_meta_codes zip s2_waymasks).map{ case ((meta, code), waymask) => 4998966a895Sxu_zh// val hit_num = PopCount(waymask) 5008966a895Sxu_zh// // NOTE: if not hit, encodeMetaECC(meta) =/= code can also be true, but we don't care about it 5018966a895Sxu_zh// (encodeMetaECC(meta) =/= code && hit_num === 1.U) || // hit one way, but parity code does not match, ECC failure 5028966a895Sxu_zh// hit_num > 1.U // hit multi way, must be a ECC failure 5038966a895Sxu_zh// }) 5048966a895Sxu_zh// 5058966a895Sxu_zh// // generate exception 5068966a895Sxu_zh// val s2_meta_exception = VecInit(s2_meta_corrupt.map(ExceptionType.fromECC(io.csr_parity_enable, _))) 5078966a895Sxu_zh// 5088966a895Sxu_zh// // merge meta exception and itlb/pmp exception 5098966a895Sxu_zh// val s2_exception = ExceptionType.merge(s2_exception_in, s2_meta_exception) 5108966a895Sxu_zh 511b92f8445Sssszwic /** 512b92f8445Sssszwic ****************************************************************************** 513b92f8445Sssszwic * Monitor the requests from missUnit to write to SRAM 514b92f8445Sssszwic ****************************************************************************** 515b92f8445Sssszwic */ 516b808ac73Sxu_zh 517b808ac73Sxu_zh /* NOTE: If fromMSHR.bits.corrupt, we should set s2_MSHR_hits to false.B, and send prefetch requests again. 518b808ac73Sxu_zh * This is the opposite of how mainPipe handles fromMSHR.bits.corrupt, 519b808ac73Sxu_zh * in which we should set s2_MSHR_hits to true.B, and send error to ifu. 520b808ac73Sxu_zh */ 521b808ac73Sxu_zh val s2_MSHR_match = VecInit((0 until PortNumber).map(i => 522b808ac73Sxu_zh (s2_req_vSetIdx(i) === fromMSHR.bits.vSetIdx) && 523b92f8445Sssszwic (s2_req_ptags(i) === getPhyTagFromBlk(fromMSHR.bits.blkPaddr)) && 524b808ac73Sxu_zh s2_valid && fromMSHR.valid && !fromMSHR.bits.corrupt 525b808ac73Sxu_zh )) 526b92f8445Sssszwic val s2_MSHR_hits = (0 until PortNumber).map(i => ValidHoldBypass(s2_MSHR_match(i), s2_fire || s2_flush)) 527b92f8445Sssszwic 528b808ac73Sxu_zh val s2_SRAM_hits = s2_waymasks.map(_.orR) 529b808ac73Sxu_zh val s2_hits = VecInit((0 until PortNumber).map(i => s2_MSHR_hits(i) || s2_SRAM_hits(i))) 530b808ac73Sxu_zh 531f80535c3Sxu_zh /* s2_exception includes itlb pf/gpf/af, pmp af and meta corruption (af), neither of which should be prefetched 53288895b11Sxu_zh * mmio should not be prefetched 533f80535c3Sxu_zh * also, if previous has exception, latter port should also not be prefetched 53488895b11Sxu_zh */ 535b808ac73Sxu_zh val s2_miss = VecInit((0 until PortNumber).map { i => 536b808ac73Sxu_zh !s2_hits(i) && (if (i == 0) true.B else s2_doubleline) && 53788895b11Sxu_zh s2_exception.take(i + 1).map(_ === ExceptionType.none).reduce(_ && _) && 53888895b11Sxu_zh s2_mmio.take(i + 1).map(!_).reduce(_ && _) 539b808ac73Sxu_zh }) 540b92f8445Sssszwic 541b92f8445Sssszwic /** 542b92f8445Sssszwic ****************************************************************************** 543b92f8445Sssszwic * send req to missUnit 544b92f8445Sssszwic ****************************************************************************** 545b92f8445Sssszwic */ 546b92f8445Sssszwic val toMSHRArbiter = Module(new Arbiter(new ICacheMissReq, PortNumber)) 547b92f8445Sssszwic 548b92f8445Sssszwic // To avoid sending duplicate requests. 549b808ac73Sxu_zh val has_send = RegInit(VecInit(Seq.fill(PortNumber)(false.B))) 550b92f8445Sssszwic (0 until PortNumber).foreach { i => 551400391a3Sxu_zh when(s1_real_fire) { 552b92f8445Sssszwic has_send(i) := false.B 553b92f8445Sssszwic }.elsewhen(toMSHRArbiter.io.in(i).fire) { 554b92f8445Sssszwic has_send(i) := true.B 555b92f8445Sssszwic } 556b92f8445Sssszwic } 557b92f8445Sssszwic 558b92f8445Sssszwic (0 until PortNumber).map { i => 559b92f8445Sssszwic toMSHRArbiter.io.in(i).valid := s2_valid && s2_miss(i) && !has_send(i) 560b92f8445Sssszwic toMSHRArbiter.io.in(i).bits.blkPaddr := getBlkAddr(s2_req_paddr(i)) 561b92f8445Sssszwic toMSHRArbiter.io.in(i).bits.vSetIdx := s2_req_vSetIdx(i) 562b92f8445Sssszwic } 563b92f8445Sssszwic 564b92f8445Sssszwic toMSHR <> toMSHRArbiter.io.out 565b92f8445Sssszwic 566b92f8445Sssszwic s2_flush := io.flush 567b92f8445Sssszwic 5682196d1caSxu_zh // toMSHRArbiter.io.in(i).fire is not used here for timing consideration 5692196d1caSxu_zh // val s2_finish = (0 until PortNumber).map(i => has_send(i) || !s2_miss(i) || toMSHRArbiter.io.in(i).fire).reduce(_&&_) 5702196d1caSxu_zh val s2_finish = (0 until PortNumber).map(i => has_send(i) || !s2_miss(i)).reduce(_ && _) 571b92f8445Sssszwic s2_ready := s2_finish || !s2_valid 572b92f8445Sssszwic s2_fire := s2_valid && s2_finish && !s2_flush 5739bba777eSssszwic 574cb6e5d3cSssszwic /** PerfAccumulate */ 5752c9f4a9fSxu_zh // the number of bpu flush 5762c9f4a9fSxu_zh XSPerfAccumulate("bpu_s0_flush", from_bpu_s0_flush) 5772c9f4a9fSxu_zh XSPerfAccumulate("bpu_s1_flush", from_bpu_s1_flush) 5782c9f4a9fSxu_zh // the number of prefetch request received from ftq or backend (software prefetch) 5792c9f4a9fSxu_zh// XSPerfAccumulate("prefetch_req_receive", io.req.fire) 5802c9f4a9fSxu_zh XSPerfAccumulate("prefetch_req_receive_hw", io.req.fire && !io.req.bits.isSoftPrefetch) 5812c9f4a9fSxu_zh XSPerfAccumulate("prefetch_req_receive_sw", io.req.fire && io.req.bits.isSoftPrefetch) 582b92f8445Sssszwic // the number of prefetch request sent to missUnit 5832c9f4a9fSxu_zh// XSPerfAccumulate("prefetch_req_send", toMSHR.fire) 5842c9f4a9fSxu_zh XSPerfAccumulate("prefetch_req_send_hw", toMSHR.fire && !s2_isSoftPrefetch) 5852c9f4a9fSxu_zh XSPerfAccumulate("prefetch_req_send_sw", toMSHR.fire && s2_isSoftPrefetch) 586b92f8445Sssszwic XSPerfAccumulate("to_missUnit_stall", toMSHR.valid && !toMSHR.ready) 587cf7d6b7aSMuzi 588cb6e5d3cSssszwic /** 589cb6e5d3cSssszwic * Count the number of requests that are filtered for various reasons. 590cb6e5d3cSssszwic * The number of prefetch discard in Performance Accumulator may be 591cb6e5d3cSssszwic * a littel larger the number of really discarded. Because there can 592cb6e5d3cSssszwic * be multiple reasons for a canceled request at the same time. 593cb6e5d3cSssszwic */ 594b92f8445Sssszwic // discard prefetch request by flush 595b92f8445Sssszwic // XSPerfAccumulate("fdip_prefetch_discard_by_tlb_except", p1_discard && p1_tlb_except) 596b92f8445Sssszwic // // discard prefetch request by hit icache SRAM 597b92f8445Sssszwic // XSPerfAccumulate("fdip_prefetch_discard_by_hit_cache", p2_discard && p1_meta_hit) 598b92f8445Sssszwic // // discard prefetch request by hit wirte SRAM 599b92f8445Sssszwic // XSPerfAccumulate("fdip_prefetch_discard_by_p1_monoitor", p1_discard && p1_monitor_hit) 600b92f8445Sssszwic // // discard prefetch request by pmp except or mmio 601b92f8445Sssszwic // XSPerfAccumulate("fdip_prefetch_discard_by_pmp", p2_discard && p2_pmp_except) 602b92f8445Sssszwic // // discard prefetch request by hit mainPipe info 603b92f8445Sssszwic // // XSPerfAccumulate("fdip_prefetch_discard_by_mainPipe", p2_discard && p2_mainPipe_hit) 6047052722fSJay} 605