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