1/*************************************************************************************** 2* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC) 3* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences 4* Copyright (c) 2020-2021 Peng Cheng Laboratory 5* 6* XiangShan is licensed under Mulan PSL v2. 7* You can use this software according to the terms and conditions of the Mulan PSL v2. 8* You may obtain a copy of Mulan PSL v2 at: 9* http://license.coscl.org.cn/MulanPSL2 10* 11* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 12* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 13* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 14* 15* See the Mulan PSL v2 for more details. 16***************************************************************************************/ 17 18package xiangshan.frontend.icache 19 20import chisel3._ 21import chisel3.util._ 22import org.chipsalliance.cde.config.Parameters 23import utility._ 24import xiangshan.SoftIfetchPrefetchBundle 25import xiangshan.cache.mmu._ 26import xiangshan.frontend._ 27 28abstract class IPrefetchBundle(implicit p: Parameters) extends ICacheBundle 29abstract class IPrefetchModule(implicit p: Parameters) extends ICacheModule 30 31class IPrefetchReq(implicit p: Parameters) extends IPrefetchBundle { 32 val startAddr: UInt = UInt(VAddrBits.W) 33 val nextlineStart: UInt = UInt(VAddrBits.W) 34 val ftqIdx: FtqPtr = new FtqPtr 35 val isSoftPrefetch: Bool = Bool() 36 val backendException: UInt = UInt(ExceptionType.width.W) 37 def crossCacheline: Bool = startAddr(blockOffBits - 1) === 1.U 38 39 def fromFtqICacheInfo(info: FtqICacheInfo): IPrefetchReq = { 40 this.startAddr := info.startAddr 41 this.nextlineStart := info.nextlineStart 42 this.ftqIdx := info.ftqIdx 43 this.isSoftPrefetch := false.B 44 this 45 } 46 47 def fromSoftPrefetch(req: SoftIfetchPrefetchBundle): IPrefetchReq = { 48 this.startAddr := req.vaddr 49 this.nextlineStart := req.vaddr + (1 << blockOffBits).U 50 this.ftqIdx := DontCare 51 this.isSoftPrefetch := true.B 52 this 53 } 54} 55 56class IPrefetchIO(implicit p: Parameters) extends IPrefetchBundle { 57 // control 58 val csr_pf_enable: Bool = Input(Bool()) 59 val ecc_enable: Bool = Input(Bool()) 60 val flush: Bool = Input(Bool()) 61 62 val req: DecoupledIO[IPrefetchReq] = Flipped(Decoupled(new IPrefetchReq)) 63 val flushFromBpu: BpuFlushInfo = Flipped(new BpuFlushInfo) 64 val itlb: Vec[TlbRequestIO] = Vec(PortNumber, new TlbRequestIO) 65 val itlbFlushPipe: Bool = Bool() 66 val pmp: Vec[ICachePMPBundle] = Vec(PortNumber, new ICachePMPBundle) 67 val metaRead: ICacheMetaReqBundle = new ICacheMetaReqBundle 68 val MSHRReq: DecoupledIO[ICacheMissReq] = DecoupledIO(new ICacheMissReq) 69 val MSHRResp: Valid[ICacheMissResp] = Flipped(ValidIO(new ICacheMissResp)) 70 val wayLookupWrite: DecoupledIO[WayLookupInfo] = DecoupledIO(new WayLookupInfo) 71} 72 73class IPrefetchPipe(implicit p: Parameters) extends IPrefetchModule with HasICacheECCHelper { 74 val io: IPrefetchIO = IO(new IPrefetchIO) 75 76 private val (toITLB, fromITLB) = (io.itlb.map(_.req), io.itlb.map(_.resp)) 77 private val (toPMP, fromPMP) = (io.pmp.map(_.req), io.pmp.map(_.resp)) 78 private val (toMeta, fromMeta) = (io.metaRead.toIMeta, io.metaRead.fromIMeta) 79 private val (toMSHR, fromMSHR) = (io.MSHRReq, io.MSHRResp) 80 private val toWayLookup = io.wayLookupWrite 81 82 private val s0_fire, s1_fire, s2_fire = WireInit(false.B) 83 private val s1_ready, s2_ready = WireInit(false.B) 84 private val s0_flush, s1_flush, s2_flush = WireInit(false.B) 85 private val from_bpu_s0_flush, from_bpu_s1_flush = WireInit(false.B) 86 87 /** 88 ****************************************************************************** 89 * IPrefetch Stage 0 90 * - 1. receive ftq req 91 * - 2. send req to ITLB 92 * - 3. send req to Meta SRAM 93 ****************************************************************************** 94 */ 95 private val s0_valid = io.req.valid 96 97 /** 98 ****************************************************************************** 99 * receive ftq req 100 ****************************************************************************** 101 */ 102 private val s0_req_vaddr = VecInit(Seq(io.req.bits.startAddr, io.req.bits.nextlineStart)) 103 private val s0_req_ftqIdx = io.req.bits.ftqIdx 104 private val s0_isSoftPrefetch = io.req.bits.isSoftPrefetch 105 private val s0_doubleline = io.req.bits.crossCacheline 106 private val s0_req_vSetIdx = s0_req_vaddr.map(get_idx) 107 private val s0_backendException = VecInit(Seq.fill(PortNumber)(io.req.bits.backendException)) 108 109 from_bpu_s0_flush := !s0_isSoftPrefetch && (io.flushFromBpu.shouldFlushByStage2(s0_req_ftqIdx) || 110 io.flushFromBpu.shouldFlushByStage3(s0_req_ftqIdx)) 111 s0_flush := io.flush || from_bpu_s0_flush || s1_flush 112 113 private val s0_can_go = s1_ready && toITLB(0).ready && toITLB(1).ready && toMeta.ready 114 io.req.ready := s0_can_go 115 116 s0_fire := s0_valid && s0_can_go && !s0_flush 117 118 /** 119 ****************************************************************************** 120 * IPrefetch Stage 1 121 * - 1. Receive resp from ITLB 122 * - 2. Receive resp from IMeta and check 123 * - 3. Monitor the requests from missUnit to write to SRAM. 124 * - 4. Write wayLookup 125 ****************************************************************************** 126 */ 127 private val s1_valid = 128 generatePipeControl(lastFire = s0_fire, thisFire = s1_fire, thisFlush = s1_flush, lastFlush = false.B) 129 130 private val s1_req_vaddr = RegEnable(s0_req_vaddr, 0.U.asTypeOf(s0_req_vaddr), s0_fire) 131 private val s1_isSoftPrefetch = RegEnable(s0_isSoftPrefetch, 0.U.asTypeOf(s0_isSoftPrefetch), s0_fire) 132 private val s1_doubleline = RegEnable(s0_doubleline, 0.U.asTypeOf(s0_doubleline), s0_fire) 133 private val s1_req_ftqIdx = RegEnable(s0_req_ftqIdx, 0.U.asTypeOf(s0_req_ftqIdx), s0_fire) 134 private val s1_req_vSetIdx = VecInit(s1_req_vaddr.map(get_idx)) 135 private val s1_backendException = RegEnable(s0_backendException, 0.U.asTypeOf(s0_backendException), s0_fire) 136 137 private val m_idle :: m_itlbResend :: m_metaResend :: m_enqWay :: m_enterS2 :: Nil = Enum(5) 138 139 private val state = RegInit(m_idle) 140 private val next_state = WireDefault(state) 141 private val s0_fire_r = RegNext(s0_fire) 142 dontTouch(state) 143 dontTouch(next_state) 144 state := next_state 145 146 /** 147 ****************************************************************************** 148 * resend itlb req if miss 149 ****************************************************************************** 150 */ 151 private val s1_wait_itlb = RegInit(VecInit(Seq.fill(PortNumber)(false.B))) 152 (0 until PortNumber).foreach { i => 153 when(s1_flush) { 154 s1_wait_itlb(i) := false.B 155 }.elsewhen(RegNext(s0_fire) && fromITLB(i).bits.miss) { 156 s1_wait_itlb(i) := true.B 157 }.elsewhen(s1_wait_itlb(i) && !fromITLB(i).bits.miss) { 158 s1_wait_itlb(i) := false.B 159 } 160 } 161 private val s1_need_itlb = VecInit(Seq( 162 (RegNext(s0_fire) || s1_wait_itlb(0)) && fromITLB(0).bits.miss, 163 (RegNext(s0_fire) || s1_wait_itlb(1)) && fromITLB(1).bits.miss && s1_doubleline 164 )) 165 private val tlb_valid_pulse = VecInit(Seq( 166 (RegNext(s0_fire) || s1_wait_itlb(0)) && !fromITLB(0).bits.miss, 167 (RegNext(s0_fire) || s1_wait_itlb(1)) && !fromITLB(1).bits.miss && s1_doubleline 168 )) 169 private val tlb_valid_latch = 170 VecInit((0 until PortNumber).map(i => ValidHoldBypass(tlb_valid_pulse(i), s1_fire, flush = s1_flush))) 171 private val itlb_finish = tlb_valid_latch(0) && (!s1_doubleline || tlb_valid_latch(1)) 172 173 (0 until PortNumber).foreach { i => 174 toITLB(i).valid := s1_need_itlb(i) || (s0_valid && (if (i == 0) true.B else s0_doubleline)) 175 toITLB(i).bits := DontCare 176 toITLB(i).bits.size := 3.U 177 toITLB(i).bits.vaddr := Mux(s1_need_itlb(i), s1_req_vaddr(i), s0_req_vaddr(i)) 178 toITLB(i).bits.debug.pc := Mux(s1_need_itlb(i), s1_req_vaddr(i), s0_req_vaddr(i)) 179 toITLB(i).bits.cmd := TlbCmd.exec 180 toITLB(i).bits.no_translate := false.B 181 } 182 fromITLB.foreach(_.ready := true.B) 183 io.itlb.foreach(_.req_kill := false.B) 184 185 /** 186 ****************************************************************************** 187 * Receive resp from ITLB 188 ****************************************************************************** 189 */ 190 private val s1_req_paddr_wire = VecInit(fromITLB.map(_.bits.paddr(0))) 191 private val s1_req_paddr_reg = VecInit((0 until PortNumber).map { i => 192 RegEnable(s1_req_paddr_wire(i), 0.U(PAddrBits.W), tlb_valid_pulse(i)) 193 }) 194 private val s1_req_paddr = VecInit((0 until PortNumber).map { i => 195 Mux(tlb_valid_pulse(i), s1_req_paddr_wire(i), s1_req_paddr_reg(i)) 196 }) 197 private val s1_req_gpaddr_tmp = VecInit((0 until PortNumber).map { i => 198 ResultHoldBypass( 199 valid = tlb_valid_pulse(i), 200 // NOTE: we dont use GPAddrBits or XLEN here, refer to ICacheMainPipe.scala L43-48 and PR#3795 201 init = 0.U(PAddrBitsMax.W), 202 data = fromITLB(i).bits.gpaddr(0) 203 ) 204 }) 205 private val s1_req_isForVSnonLeafPTE_tmp = VecInit((0 until PortNumber).map { i => 206 ResultHoldBypass( 207 valid = tlb_valid_pulse(i), 208 init = 0.U.asTypeOf(fromITLB(i).bits.isForVSnonLeafPTE), 209 data = fromITLB(i).bits.isForVSnonLeafPTE 210 ) 211 }) 212 private val s1_itlb_exception = VecInit((0 until PortNumber).map { i => 213 ResultHoldBypass( 214 valid = tlb_valid_pulse(i), 215 init = 0.U(ExceptionType.width.W), 216 data = ExceptionType.fromTlbResp(fromITLB(i).bits) 217 ) 218 }) 219 private val s1_itlb_pbmt = VecInit((0 until PortNumber).map { i => 220 ResultHoldBypass( 221 valid = tlb_valid_pulse(i), 222 init = 0.U.asTypeOf(fromITLB(i).bits.pbmt(0)), 223 data = fromITLB(i).bits.pbmt(0) 224 ) 225 }) 226 private val s1_itlb_exception_gpf = VecInit(s1_itlb_exception.map(_ === ExceptionType.gpf)) 227 228 /* Select gpaddr with the first gpf 229 * Note: the backend wants the base guest physical address of a fetch block 230 * for port(i), its base gpaddr is actually (gpaddr - i * blocksize) 231 * see GPAMem: https://github.com/OpenXiangShan/XiangShan/blob/344cf5d55568dd40cd658a9ee66047a505eeb504/src/main/scala/xiangshan/backend/GPAMem.scala#L33-L34 232 * see also: https://github.com/OpenXiangShan/XiangShan/blob/344cf5d55568dd40cd658a9ee66047a505eeb504/src/main/scala/xiangshan/frontend/IFU.scala#L374-L375 233 */ 234 private val s1_req_gpaddr = PriorityMuxDefault( 235 s1_itlb_exception_gpf zip (0 until PortNumber).map(i => s1_req_gpaddr_tmp(i) - (i << blockOffBits).U), 236 0.U.asTypeOf(s1_req_gpaddr_tmp(0)) 237 ) 238 239 private val s1_req_isForVSnonLeafPTE = PriorityMuxDefault( 240 s1_itlb_exception_gpf zip s1_req_isForVSnonLeafPTE_tmp, 241 0.U.asTypeOf(s1_req_isForVSnonLeafPTE_tmp(0)) 242 ) 243 244 /** 245 ****************************************************************************** 246 * resend metaArray read req when itlb miss finish 247 ****************************************************************************** 248 */ 249 private val s1_need_meta = ((state === m_itlbResend) && itlb_finish) || (state === m_metaResend) 250 toMeta.valid := s1_need_meta || s0_valid 251 toMeta.bits := DontCare 252 toMeta.bits.isDoubleLine := Mux(s1_need_meta, s1_doubleline, s0_doubleline) 253 254 (0 until PortNumber).foreach { i => 255 toMeta.bits.vSetIdx(i) := Mux(s1_need_meta, s1_req_vSetIdx(i), s0_req_vSetIdx(i)) 256 } 257 258 /** 259 ****************************************************************************** 260 * Receive resp from IMeta and check 261 ****************************************************************************** 262 */ 263 private val s1_req_ptags = VecInit(s1_req_paddr.map(get_phy_tag)) 264 265 private val s1_meta_ptags = fromMeta.tags 266 private val s1_meta_valids = fromMeta.entryValid 267 268 private def getWaymask(paddrs: Vec[UInt]): Vec[UInt] = { 269 val ptags = paddrs.map(get_phy_tag) 270 val tag_eq_vec = 271 VecInit((0 until PortNumber).map(p => VecInit((0 until nWays).map(w => s1_meta_ptags(p)(w) === ptags(p))))) 272 val tag_match_vec = VecInit((0 until PortNumber).map { k => 273 VecInit(tag_eq_vec(k).zipWithIndex.map { case (way_tag_eq, w) => way_tag_eq && s1_meta_valids(k)(w) }) 274 }) 275 val waymasks = VecInit(tag_match_vec.map(_.asUInt)) 276 waymasks 277 } 278 279 private val s1_SRAM_waymasks = VecInit((0 until PortNumber).map { port => 280 Mux(tlb_valid_pulse(port), getWaymask(s1_req_paddr_wire)(port), getWaymask(s1_req_paddr_reg)(port)) 281 }) 282 283 // select ecc code 284 /* NOTE: 285 * When ECC check fails, s1_waymasks may be corrupted, so this selected meta_codes may be wrong. 286 * However, we can guarantee that the request sent to the l2 cache and the response to the IFU are both correct, 287 * considering the probability of bit flipping abnormally is very small, consider there's up to 1 bit being wrong: 288 * 1. miss -> fake hit: The wrong bit in s1_waymasks was set to true.B, thus selects the wrong meta_codes, 289 * but we can detect this by checking whether `encodeMetaECC(req_ptags) === meta_codes`. 290 * 2. hit -> fake multi-hit: In normal situation, multi-hit never happens, so multi-hit indicates ECC failure, 291 * we can detect this by checking whether `PopCount(waymasks) <= 1.U`, 292 * and meta_codes is not important in this situation. 293 * 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. 294 * 4. hit -> hit / miss -> miss: ECC failure happens in an irrelevant way, so we don't care about it this time. 295 */ 296 private val s1_SRAM_meta_codes = VecInit((0 until PortNumber).map { port => 297 Mux1H(s1_SRAM_waymasks(port), fromMeta.codes(port)) 298 }) 299 300 /** 301 ****************************************************************************** 302 * update waymasks and meta_codes according to MSHR update data 303 ****************************************************************************** 304 */ 305 private def updateMetaInfo(mask: UInt, vSetIdx: UInt, ptag: UInt, code: UInt): (UInt, UInt) = { 306 require(mask.getWidth == nWays) 307 val new_mask = WireInit(mask) 308 val new_code = WireInit(code) 309 val valid = fromMSHR.valid && !fromMSHR.bits.corrupt 310 val vset_same = fromMSHR.bits.vSetIdx === vSetIdx 311 val ptag_same = getPhyTagFromBlk(fromMSHR.bits.blkPaddr) === ptag 312 val way_same = fromMSHR.bits.waymask === mask 313 when(valid && vset_same) { 314 when(ptag_same) { 315 new_mask := fromMSHR.bits.waymask 316 // also update meta_codes 317 // we have getPhyTagFromBlk(fromMSHR.bits.blkPaddr) === ptag, so we can use ptag directly for better timing 318 new_code := encodeMetaECC(ptag) 319 }.elsewhen(way_same) { 320 new_mask := 0.U 321 // we don't care about new_code, since it's not used for a missed request 322 } 323 } 324 (new_mask, new_code) 325 } 326 327 private val s1_SRAM_valid = s0_fire_r || RegNext(s1_need_meta && toMeta.ready) 328 private val s1_MSHR_valid = fromMSHR.valid && !fromMSHR.bits.corrupt 329 private val s1_waymasks = WireInit(VecInit(Seq.fill(PortNumber)(0.U(nWays.W)))) 330 private val s1_waymasks_r = RegEnable(s1_waymasks, 0.U.asTypeOf(s1_waymasks), s1_SRAM_valid || s1_MSHR_valid) 331 private val s1_meta_codes = WireInit(VecInit(Seq.fill(PortNumber)(0.U(ICacheMetaCodeBits.W)))) 332 private val s1_meta_codes_r = RegEnable(s1_meta_codes, 0.U.asTypeOf(s1_meta_codes), s1_SRAM_valid || s1_MSHR_valid) 333 334 // update waymasks and meta_codes 335 (0 until PortNumber).foreach { i => 336 val old_waymask = Mux(s1_SRAM_valid, s1_SRAM_waymasks(i), s1_waymasks_r(i)) 337 val old_meta_codes = Mux(s1_SRAM_valid, s1_SRAM_meta_codes(i), s1_meta_codes_r(i)) 338 val new_info = updateMetaInfo(old_waymask, s1_req_vSetIdx(i), s1_req_ptags(i), old_meta_codes) 339 s1_waymasks(i) := new_info._1 340 s1_meta_codes(i) := new_info._2 341 } 342 343 /** 344 ****************************************************************************** 345 * send enqueue req to WayLookup 346 ******** ********************************************************************** 347 */ 348 // Disallow enqueuing wayLookup when SRAM write occurs. 349 toWayLookup.valid := ((state === m_enqWay) || ((state === m_idle) && itlb_finish)) && 350 !s1_flush && !fromMSHR.valid && !s1_isSoftPrefetch // do not enqueue soft prefetch 351 toWayLookup.bits.vSetIdx := s1_req_vSetIdx 352 toWayLookup.bits.waymask := s1_waymasks 353 toWayLookup.bits.ptag := s1_req_ptags 354 toWayLookup.bits.gpaddr := s1_req_gpaddr 355 toWayLookup.bits.isForVSnonLeafPTE := s1_req_isForVSnonLeafPTE 356 toWayLookup.bits.meta_codes := s1_meta_codes 357 (0 until PortNumber).foreach { i => 358 // exception in first line is always valid, in second line is valid iff is doubleline request 359 val excpValid = if (i == 0) true.B else s1_doubleline 360 // Send s1_itlb_exception to WayLookup (instead of s1_exception_out) for better timing. 361 // Will check pmp again in mainPipe 362 toWayLookup.bits.itlb_exception(i) := Mux(excpValid, s1_itlb_exception(i), ExceptionType.none) 363 toWayLookup.bits.itlb_pbmt(i) := Mux(excpValid, s1_itlb_pbmt(i), Pbmt.pma) 364 } 365 366 private val s1_waymasks_vec = s1_waymasks.map(_.asTypeOf(Vec(nWays, Bool()))) 367 when(toWayLookup.fire) { 368 assert( 369 PopCount(s1_waymasks_vec(0)) <= 1.U && (PopCount(s1_waymasks_vec(1)) <= 1.U || !s1_doubleline), 370 "Multi-hit:\nport0: count=%d ptag=0x%x vSet=0x%x vaddr=0x%x\nport1: count=%d ptag=0x%x vSet=0x%x vaddr=0x%x", 371 PopCount(s1_waymasks_vec(0)) > 1.U, 372 s1_req_ptags(0), 373 get_idx(s1_req_vaddr(0)), 374 s1_req_vaddr(0), 375 PopCount(s1_waymasks_vec(1)) > 1.U && s1_doubleline, 376 s1_req_ptags(1), 377 get_idx(s1_req_vaddr(1)), 378 s1_req_vaddr(1) 379 ) 380 } 381 382 /** 383 ****************************************************************************** 384 * PMP check 385 ****************************************************************************** 386 */ 387 toPMP.zipWithIndex.foreach { case (p, i) => 388 // if itlb has exception, paddr can be invalid, therefore pmp check can be skipped 389 p.valid := s1_valid // !ExceptionType.hasException(s1_itlb_exception(i)) 390 p.bits.addr := s1_req_paddr(i) 391 p.bits.size := 3.U 392 p.bits.cmd := TlbCmd.exec 393 } 394 private val s1_pmp_exception = VecInit(fromPMP.map(ExceptionType.fromPMPResp)) 395 private val s1_pmp_mmio = VecInit(fromPMP.map(_.mmio)) 396 397 // merge s1 itlb/pmp exceptions, itlb has the highest priority, pmp next 398 // for timing consideration, meta_corrupt is not merged, and it will NOT cancel prefetch 399 private val s1_exception_out = ExceptionType.merge( 400 s1_backendException, 401 s1_itlb_exception, 402 s1_pmp_exception 403 ) 404 405 // merge pmp mmio and itlb pbmt 406 private val s1_mmio = VecInit((s1_pmp_mmio zip s1_itlb_pbmt).map { case (mmio, pbmt) => 407 mmio || Pbmt.isUncache(pbmt) 408 }) 409 410 /** 411 ****************************************************************************** 412 * state machine 413 ******** ********************************************************************** 414 */ 415 416 switch(state) { 417 is(m_idle) { 418 when(s1_valid) { 419 when(!itlb_finish) { 420 next_state := m_itlbResend 421 }.elsewhen(!toWayLookup.fire) { // itlb_finish 422 next_state := m_enqWay 423 }.elsewhen(!s2_ready) { // itlb_finish && toWayLookup.fire 424 next_state := m_enterS2 425 } // .otherwise { next_state := m_idle } 426 } // .otherwise { next_state := m_idle } // !s1_valid 427 } 428 is(m_itlbResend) { 429 when(itlb_finish) { 430 when(!toMeta.ready) { 431 next_state := m_metaResend 432 }.otherwise { // toMeta.ready 433 next_state := m_enqWay 434 } 435 } // .otherwise { next_state := m_itlbResend } // !itlb_finish 436 } 437 is(m_metaResend) { 438 when(toMeta.ready) { 439 next_state := m_enqWay 440 } // .otherwise { next_state := m_metaResend } // !toMeta.ready 441 } 442 is(m_enqWay) { 443 when(toWayLookup.fire || s1_isSoftPrefetch) { 444 when(!s2_ready) { 445 next_state := m_enterS2 446 }.otherwise { // s2_ready 447 next_state := m_idle 448 } 449 } // .otherwise { next_state := m_enqWay } 450 } 451 is(m_enterS2) { 452 when(s2_ready) { 453 next_state := m_idle 454 } 455 } 456 } 457 458 when(s1_flush) { 459 next_state := m_idle 460 } 461 462 /** Stage 1 control */ 463 from_bpu_s1_flush := s1_valid && !s1_isSoftPrefetch && io.flushFromBpu.shouldFlushByStage3(s1_req_ftqIdx) 464 s1_flush := io.flush || from_bpu_s1_flush 465 // when s1 is flushed, itlb pipeline should also be flushed 466 io.itlbFlushPipe := s1_flush 467 468 s1_ready := next_state === m_idle 469 s1_fire := (next_state === m_idle) && s1_valid && !s1_flush // used to clear s1_valid & itlb_valid_latch 470 private val s1_real_fire = s1_fire && io.csr_pf_enable // real "s1 fire" that s1 enters s2 471 472 /** 473 ****************************************************************************** 474 * IPrefetch Stage 2 475 * - 1. Monitor the requests from missUnit to write to SRAM. 476 * - 2. send req to missUnit 477 ****************************************************************************** 478 */ 479 private val s2_valid = 480 generatePipeControl(lastFire = s1_real_fire, thisFire = s2_fire, thisFlush = s2_flush, lastFlush = false.B) 481 482 private val s2_req_vaddr = RegEnable(s1_req_vaddr, 0.U.asTypeOf(s1_req_vaddr), s1_real_fire) 483 private val s2_isSoftPrefetch = RegEnable(s1_isSoftPrefetch, 0.U.asTypeOf(s1_isSoftPrefetch), s1_real_fire) 484 private val s2_doubleline = RegEnable(s1_doubleline, 0.U.asTypeOf(s1_doubleline), s1_real_fire) 485 private val s2_req_paddr = RegEnable(s1_req_paddr, 0.U.asTypeOf(s1_req_paddr), s1_real_fire) 486 private val s2_exception = 487 RegEnable(s1_exception_out, 0.U.asTypeOf(s1_exception_out), s1_real_fire) // includes itlb/pmp exception 488 // disabled for timing consideration 489// private val s2_exception_in = 490// RegEnable(s1_exception_out, 0.U.asTypeOf(s1_exception_out), s1_real_fire) 491 private val s2_mmio = RegEnable(s1_mmio, 0.U.asTypeOf(s1_mmio), s1_real_fire) 492 private val s2_waymasks = RegEnable(s1_waymasks, 0.U.asTypeOf(s1_waymasks), s1_real_fire) 493 // disabled for timing consideration 494// private val s2_meta_codes = RegEnable(s1_meta_codes, 0.U.asTypeOf(s1_meta_codes), s1_real_fire) 495 496 private val s2_req_vSetIdx = s2_req_vaddr.map(get_idx) 497 private val s2_req_ptags = s2_req_paddr.map(get_phy_tag) 498 499 // disabled for timing consideration 500// // do metaArray ECC check 501// val s2_meta_corrupt = VecInit((s2_req_ptags zip s2_meta_codes zip s2_waymasks).map{ case ((meta, code), waymask) => 502// val hit_num = PopCount(waymask) 503// // NOTE: if not hit, encodeMetaECC(meta) =/= code can also be true, but we don't care about it 504// (encodeMetaECC(meta) =/= code && hit_num === 1.U) || // hit one way, but parity code does not match, ECC failure 505// hit_num > 1.U // hit multi-way, must be an ECC failure 506// }) 507// 508// // generate exception 509// val s2_meta_exception = VecInit(s2_meta_corrupt.map(ExceptionType.fromECC(io.ecc_enable, _))) 510// 511// // merge meta exception and itlb/pmp exception 512// val s2_exception = ExceptionType.merge(s2_exception_in, s2_meta_exception) 513 514 /** 515 ****************************************************************************** 516 * Monitor the requests from missUnit to write to SRAM 517 ****************************************************************************** 518 */ 519 520 /* NOTE: If fromMSHR.bits.corrupt, we should set s2_MSHR_hits to false.B, and send prefetch requests again. 521 * This is the opposite of how mainPipe handles fromMSHR.bits.corrupt, 522 * in which we should set s2_MSHR_hits to true.B, and send error to ifu. 523 */ 524 private val s2_MSHR_match = VecInit((0 until PortNumber).map { i => 525 (s2_req_vSetIdx(i) === fromMSHR.bits.vSetIdx) && 526 (s2_req_ptags(i) === getPhyTagFromBlk(fromMSHR.bits.blkPaddr)) && 527 s2_valid && fromMSHR.valid && !fromMSHR.bits.corrupt 528 }) 529 private val s2_MSHR_hits = (0 until PortNumber).map(i => ValidHoldBypass(s2_MSHR_match(i), s2_fire || s2_flush)) 530 531 private val s2_SRAM_hits = s2_waymasks.map(_.orR) 532 private val s2_hits = VecInit((0 until PortNumber).map(i => s2_MSHR_hits(i) || s2_SRAM_hits(i))) 533 534 /* s2_exception includes itlb pf/gpf/af, pmp af and meta corruption (af), neither of which should be prefetched 535 * mmio should not be prefetched 536 * also, if previous has exception, latter port should also not be prefetched 537 */ 538 private val s2_miss = VecInit((0 until PortNumber).map { i => 539 !s2_hits(i) && (if (i == 0) true.B else s2_doubleline) && 540 !ExceptionType.hasException(s2_exception.take(i + 1)) && 541 s2_mmio.take(i + 1).map(!_).reduce(_ && _) 542 }) 543 544 /** 545 ****************************************************************************** 546 * send req to missUnit 547 ****************************************************************************** 548 */ 549 private val toMSHRArbiter = Module(new Arbiter(new ICacheMissReq, PortNumber)) 550 551 // To avoid sending duplicate requests. 552 private val has_send = RegInit(VecInit(Seq.fill(PortNumber)(false.B))) 553 (0 until PortNumber).foreach { i => 554 when(s1_real_fire) { 555 has_send(i) := false.B 556 }.elsewhen(toMSHRArbiter.io.in(i).fire) { 557 has_send(i) := true.B 558 } 559 } 560 561 (0 until PortNumber).foreach { i => 562 toMSHRArbiter.io.in(i).valid := s2_valid && s2_miss(i) && !has_send(i) 563 toMSHRArbiter.io.in(i).bits.blkPaddr := getBlkAddr(s2_req_paddr(i)) 564 toMSHRArbiter.io.in(i).bits.vSetIdx := s2_req_vSetIdx(i) 565 } 566 567 toMSHR <> toMSHRArbiter.io.out 568 569 s2_flush := io.flush 570 571 // toMSHRArbiter.io.in(i).fire is not used here for timing consideration 572// private val s2_finish = 573// (0 until PortNumber).map(i => has_send(i) || !s2_miss(i) || toMSHRArbiter.io.in(i).fire).reduce(_ && _) 574 private val s2_finish = (0 until PortNumber).map(i => has_send(i) || !s2_miss(i)).reduce(_ && _) 575 s2_ready := s2_finish || !s2_valid 576 s2_fire := s2_valid && s2_finish && !s2_flush 577 578 /** PerfAccumulate */ 579 // the number of bpu flush 580 XSPerfAccumulate("bpu_s0_flush", from_bpu_s0_flush) 581 XSPerfAccumulate("bpu_s1_flush", from_bpu_s1_flush) 582 // the number of prefetch request received from ftq or backend (software prefetch) 583// XSPerfAccumulate("prefetch_req_receive", io.req.fire) 584 XSPerfAccumulate("prefetch_req_receive_hw", io.req.fire && !io.req.bits.isSoftPrefetch) 585 XSPerfAccumulate("prefetch_req_receive_sw", io.req.fire && io.req.bits.isSoftPrefetch) 586 // the number of prefetch request sent to missUnit 587// XSPerfAccumulate("prefetch_req_send", toMSHR.fire) 588 XSPerfAccumulate("prefetch_req_send_hw", toMSHR.fire && !s2_isSoftPrefetch) 589 XSPerfAccumulate("prefetch_req_send_sw", toMSHR.fire && s2_isSoftPrefetch) 590 XSPerfAccumulate("to_missUnit_stall", toMSHR.valid && !toMSHR.ready) 591 592 /** 593 * Count the number of requests that are filtered for various reasons. 594 * The number of prefetch discard in Performance Accumulator may be 595 * a little larger the number of really discarded. Because there can 596 * be multiple reasons for a canceled request at the same time. 597 */ 598 // discard prefetch request by flush 599 // XSPerfAccumulate("fdip_prefetch_discard_by_tlb_except", p1_discard && p1_tlb_except) 600 // // discard prefetch request by hit icache SRAM 601 // XSPerfAccumulate("fdip_prefetch_discard_by_hit_cache", p2_discard && p1_meta_hit) 602 // // discard prefetch request by hit write SRAM 603 // XSPerfAccumulate("fdip_prefetch_discard_by_p1_monitor", p1_discard && p1_monitor_hit) 604 // // discard prefetch request by pmp except or mmio 605 // XSPerfAccumulate("fdip_prefetch_discard_by_pmp", p2_discard && p2_pmp_except) 606 // // discard prefetch request by hit mainPipe info 607 // // XSPerfAccumulate("fdip_prefetch_discard_by_mainPipe", p2_discard && p2_mainPipe_hit) 608} 609