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_tmp = 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 227 // merge backend exception and itlb exception 228 // for area concern, we don't have 64 bits vaddr in frontend, but spec asks page fault when high bits are not all 0/1 229 // this check is finished in backend, and passed to frontend with redirect, we see it as a part of itlb exception 230 private val s1_itlb_exception = ExceptionType.merge( 231 s1_backendException, 232 s1_itlb_exception_tmp 233 ) 234 // debug 235 dontTouch(s1_itlb_exception_tmp) 236 dontTouch(s1_itlb_exception) 237 238 private val s1_itlb_exception_gpf = VecInit(s1_itlb_exception.map(_ === ExceptionType.gpf)) 239 240 /* Select gpaddr with the first gpf 241 * Note: the backend wants the base guest physical address of a fetch block 242 * for port(i), its base gpaddr is actually (gpaddr - i * blocksize) 243 * see GPAMem: https://github.com/OpenXiangShan/XiangShan/blob/344cf5d55568dd40cd658a9ee66047a505eeb504/src/main/scala/xiangshan/backend/GPAMem.scala#L33-L34 244 * see also: https://github.com/OpenXiangShan/XiangShan/blob/344cf5d55568dd40cd658a9ee66047a505eeb504/src/main/scala/xiangshan/frontend/IFU.scala#L374-L375 245 */ 246 private val s1_req_gpaddr = PriorityMuxDefault( 247 s1_itlb_exception_gpf zip (0 until PortNumber).map(i => s1_req_gpaddr_tmp(i) - (i << blockOffBits).U), 248 0.U.asTypeOf(s1_req_gpaddr_tmp(0)) 249 ) 250 251 private val s1_req_isForVSnonLeafPTE = PriorityMuxDefault( 252 s1_itlb_exception_gpf zip s1_req_isForVSnonLeafPTE_tmp, 253 0.U.asTypeOf(s1_req_isForVSnonLeafPTE_tmp(0)) 254 ) 255 256 /** 257 ****************************************************************************** 258 * resend metaArray read req when itlb miss finish 259 ****************************************************************************** 260 */ 261 private val s1_need_meta = ((state === m_itlbResend) && itlb_finish) || (state === m_metaResend) 262 toMeta.valid := s1_need_meta || s0_valid 263 toMeta.bits := DontCare 264 toMeta.bits.isDoubleLine := Mux(s1_need_meta, s1_doubleline, s0_doubleline) 265 266 (0 until PortNumber).foreach { i => 267 toMeta.bits.vSetIdx(i) := Mux(s1_need_meta, s1_req_vSetIdx(i), s0_req_vSetIdx(i)) 268 } 269 270 /** 271 ****************************************************************************** 272 * Receive resp from IMeta and check 273 ****************************************************************************** 274 */ 275 private val s1_req_ptags = VecInit(s1_req_paddr.map(get_phy_tag)) 276 277 private val s1_meta_ptags = fromMeta.tags 278 private val s1_meta_valids = fromMeta.entryValid 279 280 private def getWaymask(paddrs: Vec[UInt]): Vec[UInt] = { 281 val ptags = paddrs.map(get_phy_tag) 282 val tag_eq_vec = 283 VecInit((0 until PortNumber).map(p => VecInit((0 until nWays).map(w => s1_meta_ptags(p)(w) === ptags(p))))) 284 val tag_match_vec = VecInit((0 until PortNumber).map { k => 285 VecInit(tag_eq_vec(k).zipWithIndex.map { case (way_tag_eq, w) => way_tag_eq && s1_meta_valids(k)(w) }) 286 }) 287 val waymasks = VecInit(tag_match_vec.map(_.asUInt)) 288 waymasks 289 } 290 291 private val s1_SRAM_waymasks = VecInit((0 until PortNumber).map { port => 292 Mux(tlb_valid_pulse(port), getWaymask(s1_req_paddr_wire)(port), getWaymask(s1_req_paddr_reg)(port)) 293 }) 294 295 // select ecc code 296 /* NOTE: 297 * When ECC check fails, s1_waymasks may be corrupted, so this selected meta_codes may be wrong. 298 * However, we can guarantee that the request sent to the l2 cache and the response to the IFU are both correct, 299 * considering the probability of bit flipping abnormally is very small, consider there's up to 1 bit being wrong: 300 * 1. miss -> fake hit: The wrong bit in s1_waymasks was set to true.B, thus selects the wrong meta_codes, 301 * but we can detect this by checking whether `encodeMetaECC(req_ptags) === meta_codes`. 302 * 2. hit -> fake multi-hit: In normal situation, multi-hit never happens, so multi-hit indicates ECC failure, 303 * we can detect this by checking whether `PopCount(waymasks) <= 1.U`, 304 * and meta_codes is not important in this situation. 305 * 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. 306 * 4. hit -> hit / miss -> miss: ECC failure happens in an irrelevant way, so we don't care about it this time. 307 */ 308 private val s1_SRAM_meta_codes = VecInit((0 until PortNumber).map { port => 309 Mux1H(s1_SRAM_waymasks(port), fromMeta.codes(port)) 310 }) 311 312 /** 313 ****************************************************************************** 314 * update waymasks and meta_codes according to MSHR update data 315 ****************************************************************************** 316 */ 317 private def updateMetaInfo(mask: UInt, vSetIdx: UInt, ptag: UInt, code: UInt): (UInt, UInt) = { 318 require(mask.getWidth == nWays) 319 val new_mask = WireInit(mask) 320 val new_code = WireInit(code) 321 val valid = fromMSHR.valid && !fromMSHR.bits.corrupt 322 val vset_same = fromMSHR.bits.vSetIdx === vSetIdx 323 val ptag_same = getPhyTagFromBlk(fromMSHR.bits.blkPaddr) === ptag 324 val way_same = fromMSHR.bits.waymask === mask 325 when(valid && vset_same) { 326 when(ptag_same) { 327 new_mask := fromMSHR.bits.waymask 328 // also update meta_codes 329 // we have getPhyTagFromBlk(fromMSHR.bits.blkPaddr) === ptag, so we can use ptag directly for better timing 330 new_code := encodeMetaECC(ptag) 331 }.elsewhen(way_same) { 332 new_mask := 0.U 333 // we don't care about new_code, since it's not used for a missed request 334 } 335 } 336 (new_mask, new_code) 337 } 338 339 private val s1_SRAM_valid = s0_fire_r || RegNext(s1_need_meta && toMeta.ready) 340 private val s1_MSHR_valid = fromMSHR.valid && !fromMSHR.bits.corrupt 341 private val s1_waymasks = WireInit(VecInit(Seq.fill(PortNumber)(0.U(nWays.W)))) 342 private val s1_waymasks_r = RegEnable(s1_waymasks, 0.U.asTypeOf(s1_waymasks), s1_SRAM_valid || s1_MSHR_valid) 343 private val s1_meta_codes = WireInit(VecInit(Seq.fill(PortNumber)(0.U(ICacheMetaCodeBits.W)))) 344 private val s1_meta_codes_r = RegEnable(s1_meta_codes, 0.U.asTypeOf(s1_meta_codes), s1_SRAM_valid || s1_MSHR_valid) 345 346 // update waymasks and meta_codes 347 (0 until PortNumber).foreach { i => 348 val old_waymask = Mux(s1_SRAM_valid, s1_SRAM_waymasks(i), s1_waymasks_r(i)) 349 val old_meta_codes = Mux(s1_SRAM_valid, s1_SRAM_meta_codes(i), s1_meta_codes_r(i)) 350 val new_info = updateMetaInfo(old_waymask, s1_req_vSetIdx(i), s1_req_ptags(i), old_meta_codes) 351 s1_waymasks(i) := new_info._1 352 s1_meta_codes(i) := new_info._2 353 } 354 355 /** 356 ****************************************************************************** 357 * send enqueue req to WayLookup 358 ******** ********************************************************************** 359 */ 360 // Disallow enqueuing wayLookup when SRAM write occurs. 361 toWayLookup.valid := ((state === m_enqWay) || ((state === m_idle) && itlb_finish)) && 362 !s1_flush && !fromMSHR.valid && !s1_isSoftPrefetch // do not enqueue soft prefetch 363 toWayLookup.bits.vSetIdx := s1_req_vSetIdx 364 toWayLookup.bits.waymask := s1_waymasks 365 toWayLookup.bits.ptag := s1_req_ptags 366 toWayLookup.bits.gpaddr := s1_req_gpaddr 367 toWayLookup.bits.isForVSnonLeafPTE := s1_req_isForVSnonLeafPTE 368 toWayLookup.bits.meta_codes := s1_meta_codes 369 (0 until PortNumber).foreach { i => 370 // exception in first line is always valid, in second line is valid iff is doubleline request 371 val excpValid = if (i == 0) true.B else s1_doubleline 372 // Send s1_itlb_exception to WayLookup (instead of s1_exception_out) for better timing. 373 // Will check pmp again in mainPipe 374 toWayLookup.bits.itlb_exception(i) := Mux( 375 excpValid, 376 s1_itlb_exception(i), // includes backend exception 377 ExceptionType.none 378 ) 379 toWayLookup.bits.itlb_pbmt(i) := Mux(excpValid, s1_itlb_pbmt(i), Pbmt.pma) 380 } 381 382 private val s1_waymasks_vec = s1_waymasks.map(_.asTypeOf(Vec(nWays, Bool()))) 383 when(toWayLookup.fire) { 384 assert( 385 PopCount(s1_waymasks_vec(0)) <= 1.U && (PopCount(s1_waymasks_vec(1)) <= 1.U || !s1_doubleline), 386 "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", 387 PopCount(s1_waymasks_vec(0)) > 1.U, 388 s1_req_ptags(0), 389 get_idx(s1_req_vaddr(0)), 390 s1_req_vaddr(0), 391 PopCount(s1_waymasks_vec(1)) > 1.U && s1_doubleline, 392 s1_req_ptags(1), 393 get_idx(s1_req_vaddr(1)), 394 s1_req_vaddr(1) 395 ) 396 } 397 398 /** 399 ****************************************************************************** 400 * PMP check 401 ****************************************************************************** 402 */ 403 toPMP.zipWithIndex.foreach { case (p, i) => 404 // if itlb has exception, paddr can be invalid, therefore pmp check can be skipped 405 p.valid := s1_valid // !ExceptionType.hasException(s1_itlb_exception(i)) 406 p.bits.addr := s1_req_paddr(i) 407 p.bits.size := 3.U 408 p.bits.cmd := TlbCmd.exec 409 } 410 private val s1_pmp_exception = VecInit(fromPMP.map(ExceptionType.fromPMPResp)) 411 private val s1_pmp_mmio = VecInit(fromPMP.map(_.mmio)) 412 413 // merge s1 itlb/pmp exceptions, itlb has the highest priority, pmp next 414 // for timing consideration, meta_corrupt is not merged, and it will NOT cancel prefetch 415 private val s1_exception_out = ExceptionType.merge( 416 s1_itlb_exception, // includes backend exception 417 s1_pmp_exception 418 ) 419 420 // merge pmp mmio and itlb pbmt 421 private val s1_mmio = VecInit((s1_pmp_mmio zip s1_itlb_pbmt).map { case (mmio, pbmt) => 422 mmio || Pbmt.isUncache(pbmt) 423 }) 424 425 /** 426 ****************************************************************************** 427 * state machine 428 ******** ********************************************************************** 429 */ 430 431 switch(state) { 432 is(m_idle) { 433 when(s1_valid) { 434 when(!itlb_finish) { 435 next_state := m_itlbResend 436 }.elsewhen(!toWayLookup.fire) { // itlb_finish 437 next_state := m_enqWay 438 }.elsewhen(!s2_ready) { // itlb_finish && toWayLookup.fire 439 next_state := m_enterS2 440 } // .otherwise { next_state := m_idle } 441 } // .otherwise { next_state := m_idle } // !s1_valid 442 } 443 is(m_itlbResend) { 444 when(itlb_finish) { 445 when(!toMeta.ready) { 446 next_state := m_metaResend 447 }.otherwise { // toMeta.ready 448 next_state := m_enqWay 449 } 450 } // .otherwise { next_state := m_itlbResend } // !itlb_finish 451 } 452 is(m_metaResend) { 453 when(toMeta.ready) { 454 next_state := m_enqWay 455 } // .otherwise { next_state := m_metaResend } // !toMeta.ready 456 } 457 is(m_enqWay) { 458 when(toWayLookup.fire || s1_isSoftPrefetch) { 459 when(!s2_ready) { 460 next_state := m_enterS2 461 }.otherwise { // s2_ready 462 next_state := m_idle 463 } 464 } // .otherwise { next_state := m_enqWay } 465 } 466 is(m_enterS2) { 467 when(s2_ready) { 468 next_state := m_idle 469 } 470 } 471 } 472 473 when(s1_flush) { 474 next_state := m_idle 475 } 476 477 /** Stage 1 control */ 478 from_bpu_s1_flush := s1_valid && !s1_isSoftPrefetch && io.flushFromBpu.shouldFlushByStage3(s1_req_ftqIdx) 479 s1_flush := io.flush || from_bpu_s1_flush 480 // when s1 is flushed, itlb pipeline should also be flushed 481 io.itlbFlushPipe := s1_flush 482 483 s1_ready := next_state === m_idle 484 s1_fire := (next_state === m_idle) && s1_valid && !s1_flush // used to clear s1_valid & itlb_valid_latch 485 private val s1_real_fire = s1_fire && io.csr_pf_enable // real "s1 fire" that s1 enters s2 486 487 /** 488 ****************************************************************************** 489 * IPrefetch Stage 2 490 * - 1. Monitor the requests from missUnit to write to SRAM. 491 * - 2. send req to missUnit 492 ****************************************************************************** 493 */ 494 private val s2_valid = 495 generatePipeControl(lastFire = s1_real_fire, thisFire = s2_fire, thisFlush = s2_flush, lastFlush = false.B) 496 497 private val s2_req_vaddr = RegEnable(s1_req_vaddr, 0.U.asTypeOf(s1_req_vaddr), s1_real_fire) 498 private val s2_isSoftPrefetch = RegEnable(s1_isSoftPrefetch, 0.U.asTypeOf(s1_isSoftPrefetch), s1_real_fire) 499 private val s2_doubleline = RegEnable(s1_doubleline, 0.U.asTypeOf(s1_doubleline), s1_real_fire) 500 private val s2_req_paddr = RegEnable(s1_req_paddr, 0.U.asTypeOf(s1_req_paddr), s1_real_fire) 501 private val s2_exception = 502 RegEnable(s1_exception_out, 0.U.asTypeOf(s1_exception_out), s1_real_fire) // includes itlb/pmp exception 503 // disabled for timing consideration 504// private val s2_exception_in = 505// RegEnable(s1_exception_out, 0.U.asTypeOf(s1_exception_out), s1_real_fire) 506 private val s2_mmio = RegEnable(s1_mmio, 0.U.asTypeOf(s1_mmio), s1_real_fire) 507 private val s2_waymasks = RegEnable(s1_waymasks, 0.U.asTypeOf(s1_waymasks), s1_real_fire) 508 // disabled for timing consideration 509// private val s2_meta_codes = RegEnable(s1_meta_codes, 0.U.asTypeOf(s1_meta_codes), s1_real_fire) 510 511 private val s2_req_vSetIdx = s2_req_vaddr.map(get_idx) 512 private val s2_req_ptags = s2_req_paddr.map(get_phy_tag) 513 514 // disabled for timing consideration 515// // do metaArray ECC check 516// val s2_meta_corrupt = VecInit((s2_req_ptags zip s2_meta_codes zip s2_waymasks).map{ case ((meta, code), waymask) => 517// val hit_num = PopCount(waymask) 518// // NOTE: if not hit, encodeMetaECC(meta) =/= code can also be true, but we don't care about it 519// (encodeMetaECC(meta) =/= code && hit_num === 1.U) || // hit one way, but parity code does not match, ECC failure 520// hit_num > 1.U // hit multi-way, must be an ECC failure 521// }) 522// 523// // generate exception 524// val s2_meta_exception = VecInit(s2_meta_corrupt.map(ExceptionType.fromECC(io.ecc_enable, _))) 525// 526// // merge meta exception and itlb/pmp exception 527// val s2_exception = ExceptionType.merge(s2_exception_in, s2_meta_exception) 528 529 /** 530 ****************************************************************************** 531 * Monitor the requests from missUnit to write to SRAM 532 ****************************************************************************** 533 */ 534 535 /* NOTE: If fromMSHR.bits.corrupt, we should set s2_MSHR_hits to false.B, and send prefetch requests again. 536 * This is the opposite of how mainPipe handles fromMSHR.bits.corrupt, 537 * in which we should set s2_MSHR_hits to true.B, and send error to ifu. 538 */ 539 private val s2_MSHR_match = VecInit((0 until PortNumber).map { i => 540 (s2_req_vSetIdx(i) === fromMSHR.bits.vSetIdx) && 541 (s2_req_ptags(i) === getPhyTagFromBlk(fromMSHR.bits.blkPaddr)) && 542 s2_valid && fromMSHR.valid && !fromMSHR.bits.corrupt 543 }) 544 private val s2_MSHR_hits = (0 until PortNumber).map(i => ValidHoldBypass(s2_MSHR_match(i), s2_fire || s2_flush)) 545 546 private val s2_SRAM_hits = s2_waymasks.map(_.orR) 547 private val s2_hits = VecInit((0 until PortNumber).map(i => s2_MSHR_hits(i) || s2_SRAM_hits(i))) 548 549 /* s2_exception includes itlb pf/gpf/af, pmp af and meta corruption (af), neither of which should be prefetched 550 * mmio should not be prefetched 551 * also, if previous has exception, latter port should also not be prefetched 552 */ 553 private val s2_miss = VecInit((0 until PortNumber).map { i => 554 !s2_hits(i) && (if (i == 0) true.B else s2_doubleline) && 555 !ExceptionType.hasException(s2_exception.take(i + 1)) && 556 s2_mmio.take(i + 1).map(!_).reduce(_ && _) 557 }) 558 559 /** 560 ****************************************************************************** 561 * send req to missUnit 562 ****************************************************************************** 563 */ 564 private val toMSHRArbiter = Module(new Arbiter(new ICacheMissReq, PortNumber)) 565 566 // To avoid sending duplicate requests. 567 private val has_send = RegInit(VecInit(Seq.fill(PortNumber)(false.B))) 568 (0 until PortNumber).foreach { i => 569 when(s1_real_fire) { 570 has_send(i) := false.B 571 }.elsewhen(toMSHRArbiter.io.in(i).fire) { 572 has_send(i) := true.B 573 } 574 } 575 576 (0 until PortNumber).foreach { i => 577 toMSHRArbiter.io.in(i).valid := s2_valid && s2_miss(i) && !has_send(i) 578 toMSHRArbiter.io.in(i).bits.blkPaddr := getBlkAddr(s2_req_paddr(i)) 579 toMSHRArbiter.io.in(i).bits.vSetIdx := s2_req_vSetIdx(i) 580 } 581 582 toMSHR <> toMSHRArbiter.io.out 583 584 s2_flush := io.flush 585 586 // toMSHRArbiter.io.in(i).fire is not used here for timing consideration 587// private val s2_finish = 588// (0 until PortNumber).map(i => has_send(i) || !s2_miss(i) || toMSHRArbiter.io.in(i).fire).reduce(_ && _) 589 private val s2_finish = (0 until PortNumber).map(i => has_send(i) || !s2_miss(i)).reduce(_ && _) 590 s2_ready := s2_finish || !s2_valid 591 s2_fire := s2_valid && s2_finish && !s2_flush 592 593 /** PerfAccumulate */ 594 // the number of bpu flush 595 XSPerfAccumulate("bpu_s0_flush", from_bpu_s0_flush) 596 XSPerfAccumulate("bpu_s1_flush", from_bpu_s1_flush) 597 // the number of prefetch request received from ftq or backend (software prefetch) 598// XSPerfAccumulate("prefetch_req_receive", io.req.fire) 599 XSPerfAccumulate("prefetch_req_receive_hw", io.req.fire && !io.req.bits.isSoftPrefetch) 600 XSPerfAccumulate("prefetch_req_receive_sw", io.req.fire && io.req.bits.isSoftPrefetch) 601 // the number of prefetch request sent to missUnit 602// XSPerfAccumulate("prefetch_req_send", toMSHR.fire) 603 XSPerfAccumulate("prefetch_req_send_hw", toMSHR.fire && !s2_isSoftPrefetch) 604 XSPerfAccumulate("prefetch_req_send_sw", toMSHR.fire && s2_isSoftPrefetch) 605 XSPerfAccumulate("to_missUnit_stall", toMSHR.valid && !toMSHR.ready) 606 607 /** 608 * Count the number of requests that are filtered for various reasons. 609 * The number of prefetch discard in Performance Accumulator may be 610 * a little larger the number of really discarded. Because there can 611 * be multiple reasons for a canceled request at the same time. 612 */ 613 // discard prefetch request by flush 614 // XSPerfAccumulate("fdip_prefetch_discard_by_tlb_except", p1_discard && p1_tlb_except) 615 // // discard prefetch request by hit icache SRAM 616 // XSPerfAccumulate("fdip_prefetch_discard_by_hit_cache", p2_discard && p1_meta_hit) 617 // // discard prefetch request by hit write SRAM 618 // XSPerfAccumulate("fdip_prefetch_discard_by_p1_monitor", p1_discard && p1_monitor_hit) 619 // // discard prefetch request by pmp except or mmio 620 // XSPerfAccumulate("fdip_prefetch_discard_by_pmp", p2_discard && p2_pmp_except) 621 // // discard prefetch request by hit mainPipe info 622 // // XSPerfAccumulate("fdip_prefetch_discard_by_mainPipe", p2_discard && p2_mainPipe_hit) 623} 624