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.mem 18 19import chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import utils._ 23import utility._ 24import xiangshan.ExceptionNO._ 25import xiangshan._ 26import xiangshan.backend.fu.PMPRespBundle 27import xiangshan.cache._ 28import xiangshan.cache.dcache.ReplayCarry 29import xiangshan.cache.mmu.{TlbCmd, TlbReq, TlbRequestIO, TlbResp} 30 31class LoadToLsqFastIO(implicit p: Parameters) extends XSBundle { 32 val valid = Output(Bool()) 33 val ld_ld_check_ok = Output(Bool()) 34 val st_ld_check_ok = Output(Bool()) 35 val cache_bank_no_conflict = Output(Bool()) 36 val ld_idx = Output(UInt(log2Ceil(LoadQueueSize).W)) 37} 38 39class LoadToLsqSlowIO(implicit p: Parameters) extends XSBundle with HasDCacheParameters { 40 val valid = Output(Bool()) 41 val tlb_hited = Output(Bool()) 42 val st_ld_check_ok = Output(Bool()) 43 val cache_no_replay = Output(Bool()) 44 val forward_data_valid = Output(Bool()) 45 val cache_hited = Output(Bool()) 46 val can_forward_full_data = Output(Bool()) 47 val ld_idx = Output(UInt(log2Ceil(LoadQueueSize).W)) 48 val data_invalid_sq_idx = Output(UInt(log2Ceil(StoreQueueSize).W)) 49 val replayCarry = Output(new ReplayCarry) 50 val miss_mshr_id = Output(UInt(log2Up(cfg.nMissEntries).W)) 51 val data_in_last_beat = Output(Bool()) 52} 53 54class LoadToLsqIO(implicit p: Parameters) extends XSBundle { 55 val loadIn = ValidIO(new LqWriteBundle) 56 val loadPaddrIn = ValidIO(new LqPaddrWriteBundle) 57 val loadVaddrIn = ValidIO(new LqVaddrWriteBundle) 58 val ldout = Flipped(DecoupledIO(new ExuOutput)) 59 val ldRawData = Input(new LoadDataFromLQBundle) 60 val s2_load_data_forwarded = Output(Bool()) 61 val s3_delayed_load_error = Output(Bool()) 62 val s2_dcache_require_replay = Output(Bool()) 63 val s3_replay_from_fetch = Output(Bool()) // update uop.ctrl.replayInst in load queue in s3 64 val forward = new PipeLoadForwardQueryIO 65 val loadViolationQuery = new LoadViolationQueryIO 66 val trigger = Flipped(new LqTriggerIO) 67 68 // for load replay 69 val replayFast = new LoadToLsqFastIO 70 val replaySlow = new LoadToLsqSlowIO 71} 72 73class LoadToLoadIO(implicit p: Parameters) extends XSBundle { 74 // load to load fast path is limited to ld (64 bit) used as vaddr src1 only 75 val data = UInt(XLEN.W) 76 val valid = Bool() 77} 78 79class LoadUnitTriggerIO(implicit p: Parameters) extends XSBundle { 80 val tdata2 = Input(UInt(64.W)) 81 val matchType = Input(UInt(2.W)) 82 val tEnable = Input(Bool()) // timing is calculated before this 83 val addrHit = Output(Bool()) 84 val lastDataHit = Output(Bool()) 85} 86 87// Load Pipeline Stage 0 88// Generate addr, use addr to query DCache and DTLB 89class LoadUnit_S0(implicit p: Parameters) extends XSModule with HasDCacheParameters{ 90 val io = IO(new Bundle() { 91 val in = Flipped(Decoupled(new ExuInput)) 92 val out = Decoupled(new LsPipelineBundle) 93 val prefetch_in = Flipped(ValidIO(new L1PrefetchReq)) 94 val dtlbReq = DecoupledIO(new TlbReq) 95 val dcacheReq = DecoupledIO(new DCacheWordReq) 96 val rsIdx = Input(UInt(log2Up(IssQueSize).W)) 97 val isFirstIssue = Input(Bool()) 98 val fastpath = Input(new LoadToLoadIO) 99 val s0_kill = Input(Bool()) 100 // wire from lq to load pipeline 101 val lsqOut = Flipped(Decoupled(new LsPipelineBundle)) 102 103 val s0_sqIdx = Output(new SqPtr) 104 }) 105 require(LoadPipelineWidth == exuParameters.LduCnt) 106 107 // there are three sources of load pipeline's input 108 // * 1. load issued by RS (io.in) 109 // * 2. load replayed by LSQ (io.lsqOut) 110 // * 3. load try pointchaising when no issued or replayed load (io.fastpath) 111 112 // the priority is 113 // 2 > 1 > 3 114 // now in S0, choise a load according to priority 115 // TODO: io.prefetch_in.valid (source 4) to be added here 116 117 val s0_vaddr = Wire(UInt(VAddrBits.W)) 118 val s0_mask = Wire(UInt(8.W)) 119 val s0_uop = Wire(new MicroOp) 120 val s0_isFirstIssue = Wire(Bool()) 121 val s0_rsIdx = Wire(UInt(log2Up(IssQueSize).W)) 122 val s0_sqIdx = Wire(new SqPtr) 123 val s0_replayCarry = Wire(new ReplayCarry) 124 // default value 125 s0_replayCarry.valid := false.B 126 s0_replayCarry.real_way_en := 0.U 127 128 io.s0_sqIdx := s0_sqIdx 129 130 val tryFastpath = WireInit(false.B) 131 132 val s0_valid = Wire(Bool()) 133 134 s0_valid := io.in.valid || io.lsqOut.valid || tryFastpath 135 136 // assign default value 137 s0_uop := DontCare 138 139 when(io.lsqOut.valid) { 140 s0_vaddr := io.lsqOut.bits.vaddr 141 s0_mask := io.lsqOut.bits.mask 142 s0_uop := io.lsqOut.bits.uop 143 s0_isFirstIssue := io.lsqOut.bits.isFirstIssue 144 s0_rsIdx := io.lsqOut.bits.rsIdx 145 s0_sqIdx := io.lsqOut.bits.uop.sqIdx 146 s0_replayCarry := io.lsqOut.bits.replayCarry 147 }.elsewhen(io.in.valid) { 148 val imm12 = io.in.bits.uop.ctrl.imm(11, 0) 149 s0_vaddr := io.in.bits.src(0) + SignExt(imm12, VAddrBits) 150 s0_mask := genWmask(s0_vaddr, io.in.bits.uop.ctrl.fuOpType(1,0)) 151 s0_uop := io.in.bits.uop 152 s0_isFirstIssue := io.isFirstIssue 153 s0_rsIdx := io.rsIdx 154 s0_sqIdx := io.in.bits.uop.sqIdx 155 }.otherwise { 156 if (EnableLoadToLoadForward) { 157 tryFastpath := io.fastpath.valid 158 // When there's no valid instruction from RS and LSQ, we try the load-to-load forwarding. 159 s0_vaddr := io.fastpath.data 160 // Assume the pointer chasing is always ld. 161 s0_uop.ctrl.fuOpType := LSUOpType.ld 162 s0_mask := genWmask(0.U, LSUOpType.ld) 163 // we dont care s0_isFirstIssue and s0_rsIdx and s0_sqIdx in S0 when trying pointchasing 164 // because these signals will be updated in S1 165 s0_isFirstIssue := DontCare 166 s0_rsIdx := DontCare 167 s0_sqIdx := DontCare 168 } 169 } 170 171 // io.lsqOut has highest priority 172 io.lsqOut.ready := (io.out.ready && io.dcacheReq.ready) 173 174 val isPrefetch = WireInit(LSUOpType.isPrefetch(s0_uop.ctrl.fuOpType)) 175 val isPrefetchRead = WireInit(s0_uop.ctrl.fuOpType === LSUOpType.prefetch_r) 176 val isPrefetchWrite = WireInit(s0_uop.ctrl.fuOpType === LSUOpType.prefetch_w) 177 val isHWPrefetch = WireInit(false.B) 178 179 // query DTLB 180 io.dtlbReq.valid := s0_valid || io.prefetch_in.valid 181 io.dtlbReq.bits.vaddr := s0_vaddr 182 io.dtlbReq.bits.cmd := TlbCmd.read 183 io.dtlbReq.bits.size := LSUOpType.size(s0_uop.ctrl.fuOpType) 184 io.dtlbReq.bits.kill := DontCare 185 io.dtlbReq.bits.debug.robIdx := s0_uop.robIdx 186 io.dtlbReq.bits.no_translate := false.B 187 io.dtlbReq.bits.debug.pc := s0_uop.cf.pc 188 io.dtlbReq.bits.debug.isFirstIssue := s0_isFirstIssue 189 190 // query DCache 191 io.dcacheReq.valid := s0_valid || io.prefetch_in.valid 192 when (isPrefetchRead) { 193 io.dcacheReq.bits.cmd := MemoryOpConstants.M_PFR 194 }.elsewhen (isPrefetchWrite) { 195 io.dcacheReq.bits.cmd := MemoryOpConstants.M_PFW 196 }.otherwise { 197 io.dcacheReq.bits.cmd := MemoryOpConstants.M_XRD 198 } 199 io.dcacheReq.bits.addr := s0_vaddr 200 io.dcacheReq.bits.mask := s0_mask 201 io.dcacheReq.bits.data := DontCare 202 when(isPrefetch) { 203 io.dcacheReq.bits.instrtype := DCACHE_PREFETCH_SOURCE.U 204 }.otherwise { 205 io.dcacheReq.bits.instrtype := LOAD_SOURCE.U 206 } 207 io.dcacheReq.bits.replayCarry := s0_replayCarry 208 209 // TODO: update cache meta 210 io.dcacheReq.bits.id := DontCare 211 212 // address align check 213 val addrAligned = LookupTree(s0_uop.ctrl.fuOpType(1, 0), List( 214 "b00".U -> true.B, //b 215 "b01".U -> (s0_vaddr(0) === 0.U), //h 216 "b10".U -> (s0_vaddr(1, 0) === 0.U), //w 217 "b11".U -> (s0_vaddr(2, 0) === 0.U) //d 218 )) 219 220 // prefetch ctrl signal gen 221 val have_confident_hw_prefetch = io.prefetch_in.valid && (io.prefetch_in.bits.confidence > 0.U) 222 val hw_prefetch_override = io.prefetch_in.valid && 223 ((io.prefetch_in.bits.confidence > 0.U) || !s0_valid) && 224 !io.lsqOut.valid 225 226 // load flow select/gen 227 // 228 // load req may come from: 229 // 1) normal read / software prefetch from RS (io.in.valid) 230 // 2) load to load fast path (tryFastpath) 231 // 3) hardware prefetch from prefetchor (hw_prefetch_override) 232 io.out.valid := (s0_valid || hw_prefetch_override) && io.dcacheReq.ready && !io.s0_kill 233 234 io.out.bits := DontCare 235 io.out.bits.vaddr := s0_vaddr 236 io.out.bits.mask := s0_mask 237 io.out.bits.uop := s0_uop 238 io.out.bits.uop.cf.exceptionVec(loadAddrMisaligned) := !addrAligned 239 io.out.bits.rsIdx := s0_rsIdx 240 io.out.bits.isFirstIssue := s0_isFirstIssue 241 io.out.bits.isPrefetch := isPrefetch 242 io.out.bits.isHWPrefetch := isHWPrefetch 243 io.out.bits.isLoadReplay := io.lsqOut.valid 244 io.out.bits.mshrid := io.lsqOut.bits.mshrid 245 io.out.bits.forward_tlDchannel := io.lsqOut.valid && io.lsqOut.bits.forward_tlDchannel 246 247 when (hw_prefetch_override) { 248 // vaddr based index for dcache 249 io.out.bits.vaddr := io.prefetch_in.bits.getVaddr() 250 io.dcacheReq.bits.addr := io.prefetch_in.bits.getVaddr() 251 // dtlb 252 // send paddr to dcache, send a no_translate signal 253 io.dtlbReq.bits.vaddr := io.prefetch_in.bits.paddr 254 io.dtlbReq.bits.cmd := Mux(io.prefetch_in.bits.is_store, TlbCmd.write, TlbCmd.read) 255 io.dtlbReq.bits.no_translate := true.B 256 // ctrl signal 257 isPrefetch := true.B 258 isHWPrefetch := true.B 259 isPrefetchRead := !io.prefetch_in.bits.is_store 260 isPrefetchWrite := io.prefetch_in.bits.is_store 261 } 262 263 // io.in can fire only when: 264 // 1) there is no lsq-replayed load 265 // 2) there is no high confidence prefetch request 266 io.in.ready := (io.out.ready && io.dcacheReq.ready && !io.lsqOut.valid && !have_confident_hw_prefetch) 267 268 XSDebug(io.dcacheReq.fire, 269 p"[DCACHE LOAD REQ] pc ${Hexadecimal(s0_uop.cf.pc)}, vaddr ${Hexadecimal(s0_vaddr)}\n" 270 ) 271 XSPerfAccumulate("in_valid", io.in.valid) 272 XSPerfAccumulate("in_fire", io.in.fire) 273 XSPerfAccumulate("in_fire_first_issue", io.in.valid && io.isFirstIssue) 274 XSPerfAccumulate("stall_out", io.out.valid && !io.out.ready && io.dcacheReq.ready) 275 XSPerfAccumulate("stall_dcache", io.out.valid && io.out.ready && !io.dcacheReq.ready) 276 XSPerfAccumulate("addr_spec_success", io.out.fire && s0_vaddr(VAddrBits-1, 12) === io.in.bits.src(0)(VAddrBits-1, 12)) 277 XSPerfAccumulate("addr_spec_failed", io.out.fire && s0_vaddr(VAddrBits-1, 12) =/= io.in.bits.src(0)(VAddrBits-1, 12)) 278 XSPerfAccumulate("addr_spec_success_once", io.out.fire && s0_vaddr(VAddrBits-1, 12) === io.in.bits.src(0)(VAddrBits-1, 12) && io.isFirstIssue) 279 XSPerfAccumulate("addr_spec_failed_once", io.out.fire && s0_vaddr(VAddrBits-1, 12) =/= io.in.bits.src(0)(VAddrBits-1, 12) && io.isFirstIssue) 280 XSPerfAccumulate("forward_tlDchannel", io.out.bits.forward_tlDchannel) 281 XSPerfAccumulate("hardware_prefetch_fire", io.out.fire && isPrefetch && hw_prefetch_override) 282 XSPerfAccumulate("software_prefetch_fire", io.out.fire && isPrefetch && !hw_prefetch_override) 283 XSPerfAccumulate("hardware_prefetch_blocked", io.prefetch_in.valid && !hw_prefetch_override) 284 XSPerfAccumulate("hardware_prefetch_total", io.prefetch_in.valid) 285} 286 287 288// Load Pipeline Stage 1 289// TLB resp (send paddr to dcache) 290class LoadUnit_S1(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper { 291 val io = IO(new Bundle() { 292 val in = Flipped(Decoupled(new LsPipelineBundle)) 293 val s1_kill = Input(Bool()) 294 val out = Decoupled(new LsPipelineBundle) 295 val dtlbResp = Flipped(DecoupledIO(new TlbResp(2))) 296 val lsuPAddr = Output(UInt(PAddrBits.W)) 297 val dcachePAddr = Output(UInt(PAddrBits.W)) 298 val dcacheKill = Output(Bool()) 299 val dcacheBankConflict = Input(Bool()) 300 val fullForwardFast = Output(Bool()) 301 val sbuffer = new LoadForwardQueryIO 302 val lsq = new PipeLoadForwardQueryIO 303 val loadViolationQueryReq = Decoupled(new LoadViolationQueryReq) 304 val reExecuteQuery = Flipped(Vec(StorePipelineWidth, Valid(new LoadReExecuteQueryIO))) 305 val rsFeedback = ValidIO(new RSFeedback) 306 val replayFast = new LoadToLsqFastIO 307 val csrCtrl = Flipped(new CustomCSRCtrlIO) 308 val needLdVioCheckRedo = Output(Bool()) 309 val needReExecute = Output(Bool()) 310 }) 311 312 val s1_uop = io.in.bits.uop 313 val s1_paddr_dup_lsu = io.dtlbResp.bits.paddr(0) 314 val s1_paddr_dup_dcache = io.dtlbResp.bits.paddr(1) 315 // af & pf exception were modified below. 316 val s1_exception = ExceptionNO.selectByFu(io.out.bits.uop.cf.exceptionVec, lduCfg).asUInt.orR 317 val s1_tlb_miss = io.dtlbResp.bits.miss 318 val s1_mask = io.in.bits.mask 319 val s1_is_prefetch = io.in.bits.isPrefetch 320 val s1_is_hw_prefetch = io.in.bits.isHWPrefetch 321 val s1_bank_conflict = io.dcacheBankConflict 322 323 io.out.bits := io.in.bits // forwardXX field will be updated in s1 324 325 io.dtlbResp.ready := true.B 326 327 io.lsuPAddr := s1_paddr_dup_lsu 328 io.dcachePAddr := s1_paddr_dup_dcache 329 //io.dcacheKill := s1_tlb_miss || s1_exception || s1_mmio 330 io.dcacheKill := s1_tlb_miss || s1_exception || io.s1_kill 331 // load forward query datapath 332 io.sbuffer.valid := io.in.valid && !(s1_exception || s1_tlb_miss || io.s1_kill || s1_is_hw_prefetch) 333 io.sbuffer.vaddr := io.in.bits.vaddr 334 io.sbuffer.paddr := s1_paddr_dup_lsu 335 io.sbuffer.uop := s1_uop 336 io.sbuffer.sqIdx := s1_uop.sqIdx 337 io.sbuffer.mask := s1_mask 338 io.sbuffer.pc := s1_uop.cf.pc // FIXME: remove it 339 340 io.lsq.valid := io.in.valid && !(s1_exception || s1_tlb_miss || io.s1_kill || s1_is_hw_prefetch) 341 io.lsq.vaddr := io.in.bits.vaddr 342 io.lsq.paddr := s1_paddr_dup_lsu 343 io.lsq.uop := s1_uop 344 io.lsq.sqIdx := s1_uop.sqIdx 345 io.lsq.sqIdxMask := DontCare // will be overwritten by sqIdxMask pre-generated in s0 346 io.lsq.mask := s1_mask 347 io.lsq.pc := s1_uop.cf.pc // FIXME: remove it 348 349 // ld-ld violation query 350 io.loadViolationQueryReq.valid := io.in.valid && !(s1_exception || s1_tlb_miss || io.s1_kill || s1_is_hw_prefetch) 351 io.loadViolationQueryReq.bits.paddr := s1_paddr_dup_lsu 352 io.loadViolationQueryReq.bits.uop := s1_uop 353 354 // st-ld violation query 355 val needReExecuteVec = Wire(Vec(StorePipelineWidth, Bool())) 356 val needReExecute = Wire(Bool()) 357 358 for (w <- 0 until StorePipelineWidth) { 359 // needReExecute valid when 360 // 1. ReExecute query request valid. 361 // 2. Load instruction is younger than requestors(store instructions). 362 // 3. Physical address match. 363 // 4. Data contains. 364 365 needReExecuteVec(w) := io.reExecuteQuery(w).valid && 366 isAfter(io.in.bits.uop.robIdx, io.reExecuteQuery(w).bits.robIdx) && 367 !s1_tlb_miss && 368 (s1_paddr_dup_lsu(PAddrBits-1, 3) === io.reExecuteQuery(w).bits.paddr(PAddrBits-1, 3)) && 369 (s1_mask & io.reExecuteQuery(w).bits.mask).orR 370 } 371 needReExecute := needReExecuteVec.asUInt.orR 372 io.needReExecute := needReExecute 373 374 // Generate forwardMaskFast to wake up insts earlier 375 val forwardMaskFast = io.lsq.forwardMaskFast.asUInt | io.sbuffer.forwardMaskFast.asUInt 376 io.fullForwardFast := ((~forwardMaskFast).asUInt & s1_mask) === 0.U 377 378 // Generate feedback signal caused by: 379 // * dcache bank conflict 380 // * need redo ld-ld violation check 381 val needLdVioCheckRedo = io.loadViolationQueryReq.valid && 382 !io.loadViolationQueryReq.ready && 383 RegNext(io.csrCtrl.ldld_vio_check_enable) 384 io.needLdVioCheckRedo := needLdVioCheckRedo 385 386 // make nanhu rs feedback port happy 387 // if a load flow comes from rs, always feedback hit (no need to replay from rs) 388 io.rsFeedback.valid := Mux(io.in.bits.isLoadReplay, false.B, io.in.valid && !io.s1_kill && !s1_is_prefetch) 389 io.rsFeedback.bits.hit := true.B // we have found s1_bank_conflict / re do ld-ld violation check 390 io.rsFeedback.bits.rsIdx := io.in.bits.rsIdx 391 io.rsFeedback.bits.flushState := io.in.bits.ptwBack 392 io.rsFeedback.bits.sourceType := Mux(s1_bank_conflict, RSFeedbackType.bankConflict, RSFeedbackType.ldVioCheckRedo) 393 io.rsFeedback.bits.dataInvalidSqIdx := DontCare 394 395 // request replay from load replay queue, fast port 396 io.replayFast.valid := io.in.valid && !io.s1_kill && !s1_is_hw_prefetch 397 io.replayFast.ld_ld_check_ok := !needLdVioCheckRedo || s1_is_prefetch 398 io.replayFast.st_ld_check_ok := !needReExecute || s1_is_prefetch 399 io.replayFast.cache_bank_no_conflict := !s1_bank_conflict || s1_is_prefetch 400 io.replayFast.ld_idx := io.in.bits.uop.lqIdx.value 401 402 // if replay is detected in load_s1, 403 // load inst will be canceled immediately 404 io.out.valid := io.in.valid && (!needLdVioCheckRedo && !s1_bank_conflict && !needReExecute) && !io.s1_kill 405 io.out.bits.paddr := s1_paddr_dup_lsu 406 io.out.bits.tlbMiss := s1_tlb_miss 407 408 // current ori test will cause the case of ldest == 0, below will be modifeid in the future. 409 // af & pf exception were modified 410 io.out.bits.uop.cf.exceptionVec(loadPageFault) := io.dtlbResp.bits.excp(0).pf.ld 411 io.out.bits.uop.cf.exceptionVec(loadAccessFault) := io.dtlbResp.bits.excp(0).af.ld 412 413 io.out.bits.ptwBack := io.dtlbResp.bits.ptwBack 414 io.out.bits.rsIdx := io.in.bits.rsIdx 415 416 io.in.ready := !io.in.valid || io.out.ready 417 418 XSPerfAccumulate("in_valid", io.in.valid) 419 XSPerfAccumulate("in_fire", io.in.fire) 420 XSPerfAccumulate("in_fire_first_issue", io.in.fire && io.in.bits.isFirstIssue) 421 XSPerfAccumulate("tlb_miss", io.in.fire && s1_tlb_miss) 422 XSPerfAccumulate("tlb_miss_first_issue", io.in.fire && s1_tlb_miss && io.in.bits.isFirstIssue) 423 XSPerfAccumulate("stall_out", io.out.valid && !io.out.ready) 424} 425 426// Load Pipeline Stage 2 427// DCache resp 428class LoadUnit_S2(implicit p: Parameters) extends XSModule with HasLoadHelper with HasCircularQueuePtrHelper with HasDCacheParameters { 429 val io = IO(new Bundle() { 430 val in = Flipped(Decoupled(new LsPipelineBundle)) 431 val out = Decoupled(new LsPipelineBundle) 432 val rsFeedback = ValidIO(new RSFeedback) 433 val replaySlow = new LoadToLsqSlowIO 434 val dcacheResp = Flipped(DecoupledIO(new DCacheWordResp)) 435 val pmpResp = Flipped(new PMPRespBundle()) 436 val lsq = new LoadForwardQueryIO 437 val dataInvalidSqIdx = Input(UInt()) 438 val sbuffer = new LoadForwardQueryIO 439 val dataForwarded = Output(Bool()) 440 val s2_dcache_require_replay = Output(Bool()) 441 val fullForward = Output(Bool()) 442 val dcache_kill = Output(Bool()) 443 val s3_delayed_load_error = Output(Bool()) 444 val loadViolationQueryResp = Flipped(Valid(new LoadViolationQueryResp)) 445 val csrCtrl = Flipped(new CustomCSRCtrlIO) 446 val sentFastUop = Input(Bool()) 447 val static_pm = Input(Valid(Bool())) // valid for static, bits for mmio 448 val s2_can_replay_from_fetch = Output(Bool()) // dirty code 449 val loadDataFromDcache = Output(new LoadDataFromDcacheBundle) 450 val reExecuteQuery = Flipped(Vec(StorePipelineWidth, Valid(new LoadReExecuteQueryIO))) 451 val needReExecute = Output(Bool()) 452 // forward tilelink D channel 453 val forward_D = Input(Bool()) 454 val forwardData_D = Input(Vec(8, UInt(8.W))) 455 456 // forward mshr data 457 val forward_mshr = Input(Bool()) 458 val forwardData_mshr = Input(Vec(8, UInt(8.W))) 459 460 // indicate whether forward tilelink D channel or mshr data is valid 461 val forward_result_valid = Input(Bool()) 462 }) 463 464 val pmp = WireInit(io.pmpResp) 465 when (io.static_pm.valid) { 466 pmp.ld := false.B 467 pmp.st := false.B 468 pmp.instr := false.B 469 pmp.mmio := io.static_pm.bits 470 } 471 472 val s2_is_prefetch = io.in.bits.isPrefetch 473 val s2_is_hw_prefetch = io.in.bits.isHWPrefetch 474 475 val forward_D_or_mshr_valid = io.forward_result_valid && (io.forward_D || io.forward_mshr) 476 477 // assert(!reset && io.forward_D && io.forward_mshr && io.in.valid && io.in.bits.forward_tlDchannel, "forward D and mshr at the same time") 478 479 // exception that may cause load addr to be invalid / illegal 480 // 481 // if such exception happen, that inst and its exception info 482 // will be force writebacked to rob 483 val s2_exception_vec = WireInit(io.in.bits.uop.cf.exceptionVec) 484 s2_exception_vec(loadAccessFault) := io.in.bits.uop.cf.exceptionVec(loadAccessFault) || pmp.ld 485 // soft prefetch will not trigger any exception (but ecc error interrupt may be triggered) 486 when (s2_is_prefetch) { 487 s2_exception_vec := 0.U.asTypeOf(s2_exception_vec.cloneType) 488 } 489 val s2_exception = ExceptionNO.selectByFu(s2_exception_vec, lduCfg).asUInt.orR && !io.in.bits.tlbMiss 490 491 // writeback access fault caused by ecc error / bus error 492 // 493 // * ecc data error is slow to generate, so we will not use it until load stage 3 494 // * in load stage 3, an extra signal io.load_error will be used to 495 496 // now cache ecc error will raise an access fault 497 // at the same time, error info (including error paddr) will be write to 498 // an customized CSR "CACHE_ERROR" 499 if (EnableAccurateLoadError) { 500 io.s3_delayed_load_error := io.dcacheResp.bits.error_delayed && 501 io.csrCtrl.cache_error_enable && 502 RegNext(io.out.valid) 503 } else { 504 io.s3_delayed_load_error := false.B 505 } 506 507 val actually_mmio = pmp.mmio 508 val s2_uop = io.in.bits.uop 509 val s2_mask = io.in.bits.mask 510 val s2_paddr = io.in.bits.paddr 511 val s2_tlb_miss = io.in.bits.tlbMiss 512 val s2_mmio = !s2_is_prefetch && actually_mmio && !s2_exception 513 val s2_cache_miss = io.dcacheResp.bits.miss && !forward_D_or_mshr_valid 514 val s2_cache_replay = io.dcacheResp.bits.replay && !forward_D_or_mshr_valid 515 val s2_cache_tag_error = io.dcacheResp.bits.tag_error 516 val s2_forward_fail = io.lsq.matchInvalid || io.sbuffer.matchInvalid 517 val s2_ldld_violation = io.loadViolationQueryResp.valid && 518 io.loadViolationQueryResp.bits.have_violation && 519 RegNext(io.csrCtrl.ldld_vio_check_enable) 520 val s2_data_invalid = io.lsq.dataInvalid && !s2_ldld_violation && !s2_exception 521 522 io.dcache_kill := pmp.ld || pmp.mmio // move pmp resp kill to outside 523 io.dcacheResp.ready := true.B 524 val dcacheShouldResp = !(s2_tlb_miss || s2_exception || s2_mmio || s2_is_prefetch) 525 assert(!(io.in.valid && (dcacheShouldResp && !io.dcacheResp.valid)), "DCache response got lost") 526 527 // merge forward result 528 // lsq has higher priority than sbuffer 529 val forwardMask = Wire(Vec(8, Bool())) 530 val forwardData = Wire(Vec(8, UInt(8.W))) 531 532 val fullForward = ((~forwardMask.asUInt).asUInt & s2_mask) === 0.U && !io.lsq.dataInvalid 533 io.lsq := DontCare 534 io.sbuffer := DontCare 535 io.fullForward := fullForward 536 537 // generate XLEN/8 Muxs 538 for (i <- 0 until XLEN / 8) { 539 forwardMask(i) := io.lsq.forwardMask(i) || io.sbuffer.forwardMask(i) 540 forwardData(i) := Mux(io.lsq.forwardMask(i), io.lsq.forwardData(i), io.sbuffer.forwardData(i)) 541 } 542 543 XSDebug(io.out.fire, "[FWD LOAD RESP] pc %x fwd %x(%b) + %x(%b)\n", 544 s2_uop.cf.pc, 545 io.lsq.forwardData.asUInt, io.lsq.forwardMask.asUInt, 546 io.in.bits.forwardData.asUInt, io.in.bits.forwardMask.asUInt 547 ) 548 549 // data merge 550 // val rdataVec = VecInit((0 until XLEN / 8).map(j => 551 // Mux(forwardMask(j), forwardData(j), io.dcacheResp.bits.data(8*(j+1)-1, 8*j)) 552 // )) // s2_rdataVec will be write to load queue 553 // val rdata = rdataVec.asUInt 554 // val rdataSel = LookupTree(s2_paddr(2, 0), List( 555 // "b000".U -> rdata(63, 0), 556 // "b001".U -> rdata(63, 8), 557 // "b010".U -> rdata(63, 16), 558 // "b011".U -> rdata(63, 24), 559 // "b100".U -> rdata(63, 32), 560 // "b101".U -> rdata(63, 40), 561 // "b110".U -> rdata(63, 48), 562 // "b111".U -> rdata(63, 56) 563 // )) 564 // val rdataPartialLoad = rdataHelper(s2_uop, rdataSel) // s2_rdataPartialLoad is not used 565 566 io.out.valid := io.in.valid && !s2_tlb_miss && !s2_data_invalid && !io.needReExecute && !s2_is_hw_prefetch 567 // write_lq_safe is needed by dup logic 568 // io.write_lq_safe := !s2_tlb_miss && !s2_data_invalid 569 // Inst will be canceled in store queue / lsq, 570 // so we do not need to care about flush in load / store unit's out.valid 571 io.out.bits := io.in.bits 572 // io.out.bits.data := rdataPartialLoad 573 io.out.bits.data := 0.U // data will be generated in load_s3 574 // when exception occurs, set it to not miss and let it write back to rob (via int port) 575 if (EnableFastForward) { 576 io.out.bits.miss := s2_cache_miss && 577 !s2_exception && 578 !fullForward && 579 !s2_is_prefetch 580 } else { 581 io.out.bits.miss := s2_cache_miss && 582 !s2_exception && 583 !s2_is_prefetch 584 } 585 io.out.bits.uop.ctrl.fpWen := io.in.bits.uop.ctrl.fpWen && !s2_exception 586 587 // val s2_loadDataFromDcache = new LoadDataFromDcacheBundle 588 // s2_loadDataFromDcache.forwardMask := forwardMask 589 // s2_loadDataFromDcache.forwardData := forwardData 590 // s2_loadDataFromDcache.uop := io.out.bits.uop 591 // s2_loadDataFromDcache.addrOffset := s2_paddr(2, 0) 592 // // forward D or mshr 593 // s2_loadDataFromDcache.forward_D := io.forward_D 594 // s2_loadDataFromDcache.forwardData_D := io.forwardData_D 595 // s2_loadDataFromDcache.forward_mshr := io.forward_mshr 596 // s2_loadDataFromDcache.forwardData_mshr := io.forwardData_mshr 597 // s2_loadDataFromDcache.forward_result_valid := io.forward_result_valid 598 // io.loadDataFromDcache := RegEnable(s2_loadDataFromDcache, io.in.valid) 599 io.loadDataFromDcache.respDcacheData := io.dcacheResp.bits.data_delayed 600 io.loadDataFromDcache.forwardMask := RegEnable(forwardMask, io.in.valid) 601 io.loadDataFromDcache.forwardData := RegEnable(forwardData, io.in.valid) 602 io.loadDataFromDcache.uop := RegEnable(io.out.bits.uop, io.in.valid) 603 io.loadDataFromDcache.addrOffset := RegEnable(s2_paddr(2, 0), io.in.valid) 604 // forward D or mshr 605 io.loadDataFromDcache.forward_D := RegEnable(io.forward_D, io.in.valid) 606 io.loadDataFromDcache.forwardData_D := RegEnable(io.forwardData_D, io.in.valid) 607 io.loadDataFromDcache.forward_mshr := RegEnable(io.forward_mshr, io.in.valid) 608 io.loadDataFromDcache.forwardData_mshr := RegEnable(io.forwardData_mshr, io.in.valid) 609 io.loadDataFromDcache.forward_result_valid := RegEnable(io.forward_result_valid, io.in.valid) 610 611 io.s2_can_replay_from_fetch := !s2_mmio && !s2_is_prefetch && !s2_tlb_miss 612 // if forward fail, replay this inst from fetch 613 val debug_forwardFailReplay = s2_forward_fail && !s2_mmio && !s2_is_prefetch && !s2_tlb_miss 614 // if ld-ld violation is detected, replay from this inst from fetch 615 val debug_ldldVioReplay = s2_ldld_violation && !s2_mmio && !s2_is_prefetch && !s2_tlb_miss 616 // io.out.bits.uop.ctrl.replayInst := false.B 617 618 io.out.bits.mmio := s2_mmio 619 io.out.bits.uop.ctrl.flushPipe := s2_mmio && io.sentFastUop 620 io.out.bits.uop.cf.exceptionVec := s2_exception_vec // cache error not included 621 622 // For timing reasons, sometimes we can not let 623 // io.out.bits.miss := s2_cache_miss && !s2_exception && !fullForward 624 // We use io.dataForwarded instead. It means: 625 // 1. Forward logic have prepared all data needed, 626 // and dcache query is no longer needed. 627 // 2. ... or data cache tag error is detected, this kind of inst 628 // will not update miss queue. That is to say, if miss, that inst 629 // may not be refilled 630 // Such inst will be writebacked from load queue. 631 io.dataForwarded := s2_cache_miss && !s2_exception && 632 (fullForward || io.csrCtrl.cache_error_enable && s2_cache_tag_error) 633 // io.out.bits.forwardX will be send to lq 634 io.out.bits.forwardMask := forwardMask 635 // data from dcache is not included in io.out.bits.forwardData 636 io.out.bits.forwardData := forwardData 637 638 io.in.ready := io.out.ready || !io.in.valid 639 640 641 // st-ld violation query 642 val needReExecuteVec = Wire(Vec(StorePipelineWidth, Bool())) 643 val needReExecute = Wire(Bool()) 644 645 for (i <- 0 until StorePipelineWidth) { 646 // NeedFastRecovery Valid when 647 // 1. Fast recovery query request Valid. 648 // 2. Load instruction is younger than requestors(store instructions). 649 // 3. Physical address match. 650 // 4. Data contains. 651 needReExecuteVec(i) := io.reExecuteQuery(i).valid && 652 isAfter(io.in.bits.uop.robIdx, io.reExecuteQuery(i).bits.robIdx) && 653 !s2_tlb_miss && 654 (s2_paddr(PAddrBits-1,3) === io.reExecuteQuery(i).bits.paddr(PAddrBits-1, 3)) && 655 (s2_mask & io.reExecuteQuery(i).bits.mask).orR 656 } 657 needReExecute := needReExecuteVec.asUInt.orR 658 io.needReExecute := needReExecute 659 660 // rs slow feedback port in nanhu is not used for now 661 io.rsFeedback.valid := false.B 662 io.rsFeedback.bits := DontCare 663 664 // request replay from load replay queue, fast port 665 io.replaySlow.valid := io.in.valid && !s2_is_prefetch 666 io.replaySlow.tlb_hited := !s2_tlb_miss 667 io.replaySlow.st_ld_check_ok := !needReExecute 668 if (EnableFastForward) { 669 io.replaySlow.cache_no_replay := !s2_cache_replay || s2_is_prefetch || s2_mmio || s2_exception || fullForward 670 }else { 671 io.replaySlow.cache_no_replay := !s2_cache_replay || s2_is_prefetch || s2_mmio || s2_exception || io.dataForwarded 672 } 673 io.replaySlow.forward_data_valid := !s2_data_invalid || s2_is_prefetch 674 io.replaySlow.cache_hited := !io.out.bits.miss || io.out.bits.mmio 675 io.replaySlow.can_forward_full_data := io.dataForwarded 676 io.replaySlow.ld_idx := io.in.bits.uop.lqIdx.value 677 io.replaySlow.data_invalid_sq_idx := io.dataInvalidSqIdx 678 io.replaySlow.replayCarry := io.dcacheResp.bits.replayCarry 679 io.replaySlow.miss_mshr_id := io.dcacheResp.bits.mshr_id 680 io.replaySlow.data_in_last_beat := io.in.bits.paddr(log2Up(refillBytes)) 681 682 // To be removed 683 val s2_need_replay_from_rs = Wire(Bool()) 684 if (EnableFastForward) { 685 s2_need_replay_from_rs := 686 needReExecute || 687 s2_tlb_miss || // replay if dtlb miss 688 s2_cache_replay && !s2_is_prefetch && !s2_mmio && !s2_exception && !fullForward || // replay if dcache miss queue full / busy 689 s2_data_invalid && !s2_is_prefetch // replay if store to load forward data is not ready 690 } else { 691 // Note that if all parts of data are available in sq / sbuffer, replay required by dcache will not be scheduled 692 s2_need_replay_from_rs := 693 needReExecute || 694 s2_tlb_miss || // replay if dtlb miss 695 s2_cache_replay && !s2_is_prefetch && !s2_mmio && !s2_exception && !io.dataForwarded || // replay if dcache miss queue full / busy 696 s2_data_invalid && !s2_is_prefetch // replay if store to load forward data is not ready 697 } 698 699 // s2_cache_replay is quite slow to generate, send it separately to LQ 700 if (EnableFastForward) { 701 io.s2_dcache_require_replay := s2_cache_replay && !fullForward 702 } else { 703 io.s2_dcache_require_replay := s2_cache_replay && 704 s2_need_replay_from_rs && 705 !io.dataForwarded && 706 !s2_is_prefetch && 707 io.out.bits.miss 708 } 709 710 XSPerfAccumulate("in_valid", io.in.valid) 711 XSPerfAccumulate("in_fire", io.in.fire) 712 XSPerfAccumulate("in_fire_first_issue", io.in.fire && io.in.bits.isFirstIssue) 713 XSPerfAccumulate("dcache_miss", io.in.fire && s2_cache_miss) 714 XSPerfAccumulate("dcache_miss_first_issue", io.in.fire && s2_cache_miss && io.in.bits.isFirstIssue) 715 XSPerfAccumulate("full_forward", io.in.valid && fullForward) 716 XSPerfAccumulate("dcache_miss_full_forward", io.in.valid && s2_cache_miss && fullForward) 717 XSPerfAccumulate("replay", io.rsFeedback.valid && !io.rsFeedback.bits.hit) 718 XSPerfAccumulate("replay_tlb_miss", io.rsFeedback.valid && !io.rsFeedback.bits.hit && s2_tlb_miss) 719 XSPerfAccumulate("replay_cache", io.rsFeedback.valid && !io.rsFeedback.bits.hit && !s2_tlb_miss && s2_cache_replay) 720 XSPerfAccumulate("stall_out", io.out.valid && !io.out.ready) 721 XSPerfAccumulate("replay_from_fetch_forward", io.out.valid && debug_forwardFailReplay) 722 XSPerfAccumulate("replay_from_fetch_load_vio", io.out.valid && debug_ldldVioReplay) 723 XSPerfAccumulate("replay_lq", io.replaySlow.valid && (!io.replaySlow.tlb_hited || !io.replaySlow.cache_no_replay || !io.replaySlow.forward_data_valid)) 724 XSPerfAccumulate("replay_tlb_miss_lq", io.replaySlow.valid && !io.replaySlow.tlb_hited) 725 XSPerfAccumulate("replay_sl_vio", io.replaySlow.valid && io.replaySlow.tlb_hited && !io.replaySlow.st_ld_check_ok) 726 XSPerfAccumulate("replay_cache_lq", io.replaySlow.valid && io.replaySlow.tlb_hited && io.replaySlow.st_ld_check_ok && !io.replaySlow.cache_no_replay) 727 XSPerfAccumulate("replay_cache_miss_lq", io.replaySlow.valid && !io.replaySlow.cache_hited) 728 XSPerfAccumulate("prefetch", io.in.fire && s2_is_prefetch) 729 XSPerfAccumulate("prefetch_ignored", io.in.fire && s2_is_prefetch && s2_cache_replay) // ignore prefetch for mshr full / miss req port conflict 730 XSPerfAccumulate("prefetch_miss", io.in.fire && s2_is_prefetch && s2_cache_miss) // prefetch req miss in l1 731 XSPerfAccumulate("prefetch_hit", io.in.fire && s2_is_prefetch && !s2_cache_miss) // prefetch req hit in l1 732 // prefetch a missed line in l1, and l1 accepted it 733 XSPerfAccumulate("prefetch_accept", io.in.fire && s2_is_prefetch && s2_cache_miss && !s2_cache_replay) 734} 735 736class LoadUnit(implicit p: Parameters) extends XSModule 737 with HasLoadHelper 738 with HasPerfEvents 739 with HasDCacheParameters 740{ 741 val io = IO(new Bundle() { 742 val ldin = Flipped(Decoupled(new ExuInput)) 743 val ldout = Decoupled(new ExuOutput) 744 val redirect = Flipped(ValidIO(new Redirect)) 745 val feedbackSlow = ValidIO(new RSFeedback) 746 val feedbackFast = ValidIO(new RSFeedback) 747 val rsIdx = Input(UInt(log2Up(IssQueSize).W)) 748 val isFirstIssue = Input(Bool()) 749 val dcache = new DCacheLoadIO 750 val sbuffer = new LoadForwardQueryIO 751 val lsq = new LoadToLsqIO 752 val tlDchannel = Input(new DcacheToLduForwardIO) 753 val forward_mshr = Flipped(new LduToMissqueueForwardIO) 754 val refill = Flipped(ValidIO(new Refill)) 755 val fastUop = ValidIO(new MicroOp) // early wakeup signal generated in load_s1, send to RS in load_s2 756 val trigger = Vec(3, new LoadUnitTriggerIO) 757 758 val tlb = new TlbRequestIO(2) 759 val pmp = Flipped(new PMPRespBundle()) // arrive same to tlb now 760 761 // provide prefetch info 762 val prefetch_train = ValidIO(new LdPrefetchTrainBundle()) 763 764 // hardware prefetch to l1 cache req 765 val prefetch_req = Flipped(ValidIO(new L1PrefetchReq)) 766 767 // load to load fast path 768 val fastpathOut = Output(new LoadToLoadIO) 769 val fastpathIn = Input(new LoadToLoadIO) 770 val loadFastMatch = Input(Bool()) 771 val loadFastImm = Input(UInt(12.W)) 772 773 // load ecc 774 val s3_delayed_load_error = Output(Bool()) // load ecc error 775 // Note that io.s3_delayed_load_error and io.lsq.s3_delayed_load_error is different 776 777 // load unit ctrl 778 val csrCtrl = Flipped(new CustomCSRCtrlIO) 779 780 val reExecuteQuery = Flipped(Vec(StorePipelineWidth, Valid(new LoadReExecuteQueryIO))) // load replay 781 val lsqOut = Flipped(Decoupled(new LsPipelineBundle)) 782 val s2IsPointerChasing = Output(Bool()) // provide right pc for hw prefetch 783 }) 784 785 val load_s0 = Module(new LoadUnit_S0) 786 val load_s1 = Module(new LoadUnit_S1) 787 val load_s2 = Module(new LoadUnit_S2) 788 789 load_s0.io.lsqOut <> io.lsqOut 790 791 // load s0 792 load_s0.io.in <> io.ldin 793 load_s0.io.dtlbReq <> io.tlb.req 794 load_s0.io.dcacheReq <> io.dcache.req 795 load_s0.io.rsIdx := io.rsIdx 796 load_s0.io.isFirstIssue := io.isFirstIssue 797 load_s0.io.s0_kill := false.B 798 799 // we try pointerchasing when: 800 // 1) no rs-issued load 801 // 2) no LSQ replayed load 802 // 3) no prefetch request 803 val s0_tryPointerChasing = !io.ldin.valid && !io.lsqOut.valid && io.fastpathIn.valid && !io.prefetch_req.valid 804 val s0_pointerChasingVAddr = io.fastpathIn.data(5, 0) +& io.loadFastImm(5, 0) 805 load_s0.io.fastpath.valid := io.fastpathIn.valid 806 load_s0.io.fastpath.data := Cat(io.fastpathIn.data(XLEN-1, 6), s0_pointerChasingVAddr(5,0)) 807 808 val s1_data = PipelineConnect(load_s0.io.out, load_s1.io.in, true.B, 809 load_s0.io.out.bits.uop.robIdx.needFlush(io.redirect) && !s0_tryPointerChasing).get 810 811 // load s1 812 // update s1_kill when any source has valid request 813 load_s1.io.s1_kill := RegEnable(load_s0.io.s0_kill, false.B, io.ldin.valid || io.lsqOut.valid || io.fastpathIn.valid) 814 io.tlb.req_kill := load_s1.io.s1_kill 815 load_s1.io.dtlbResp <> io.tlb.resp 816 io.dcache.s1_paddr_dup_lsu <> load_s1.io.lsuPAddr 817 io.dcache.s1_paddr_dup_dcache <> load_s1.io.dcachePAddr 818 io.dcache.s1_kill := load_s1.io.dcacheKill 819 load_s1.io.sbuffer <> io.sbuffer 820 load_s1.io.lsq <> io.lsq.forward 821 load_s1.io.loadViolationQueryReq <> io.lsq.loadViolationQuery.req 822 load_s1.io.dcacheBankConflict <> io.dcache.s1_bank_conflict 823 load_s1.io.csrCtrl <> io.csrCtrl 824 load_s1.io.reExecuteQuery := io.reExecuteQuery 825 // provide paddr and vaddr for lq 826 io.lsq.loadPaddrIn.valid := load_s1.io.out.valid && !load_s1.io.out.bits.isHWPrefetch 827 io.lsq.loadPaddrIn.bits.lqIdx := load_s1.io.out.bits.uop.lqIdx 828 io.lsq.loadPaddrIn.bits.paddr := load_s1.io.lsuPAddr 829 830 io.lsq.loadVaddrIn.valid := load_s1.io.in.valid && !load_s1.io.s1_kill && !load_s1.io.out.bits.isHWPrefetch 831 io.lsq.loadVaddrIn.bits.lqIdx := load_s1.io.out.bits.uop.lqIdx 832 io.lsq.loadVaddrIn.bits.vaddr := load_s1.io.out.bits.vaddr 833 834 // when S0 has opportunity to try pointerchasing, make sure it truely goes to S1 835 // which is S0's out is ready and dcache is ready 836 val s0_doTryPointerChasing = s0_tryPointerChasing && load_s0.io.out.ready && load_s0.io.dcacheReq.ready 837 val s1_tryPointerChasing = RegNext(s0_doTryPointerChasing, false.B) 838 val s1_pointerChasingVAddr = RegEnable(s0_pointerChasingVAddr, s0_doTryPointerChasing) 839 val cancelPointerChasing = WireInit(false.B) 840 if (EnableLoadToLoadForward) { 841 // Sometimes, we need to cancel the load-load forwarding. 842 // These can be put at S0 if timing is bad at S1. 843 // Case 0: CACHE_SET(base + offset) != CACHE_SET(base) (lowest 6-bit addition has an overflow) 844 val addressMisMatch = s1_pointerChasingVAddr(6) || RegEnable(io.loadFastImm(11, 6).orR, s0_doTryPointerChasing) 845 // Case 1: the address is not 64-bit aligned or the fuOpType is not LD 846 val addressNotAligned = s1_pointerChasingVAddr(2, 0).orR 847 val fuOpTypeIsNotLd = io.ldin.bits.uop.ctrl.fuOpType =/= LSUOpType.ld 848 // Case 2: this is not a valid load-load pair 849 val notFastMatch = RegEnable(!io.loadFastMatch, s0_tryPointerChasing) 850 // Case 3: this load-load uop is cancelled 851 val isCancelled = !io.ldin.valid 852 when (s1_tryPointerChasing) { 853 cancelPointerChasing := addressMisMatch || addressNotAligned || fuOpTypeIsNotLd || notFastMatch || isCancelled 854 load_s1.io.in.bits.uop := io.ldin.bits.uop 855 val spec_vaddr = s1_data.vaddr 856 val vaddr = Cat(spec_vaddr(VAddrBits - 1, 6), s1_pointerChasingVAddr(5, 3), 0.U(3.W)) 857 load_s1.io.in.bits.vaddr := vaddr 858 load_s1.io.in.bits.rsIdx := io.rsIdx 859 load_s1.io.in.bits.isFirstIssue := io.isFirstIssue 860 // We need to replace vaddr(5, 3). 861 val spec_paddr = io.tlb.resp.bits.paddr(0) 862 load_s1.io.dtlbResp.bits.paddr.foreach(_ := Cat(spec_paddr(PAddrBits - 1, 6), s1_pointerChasingVAddr(5, 3), 0.U(3.W))) 863 } 864 when (cancelPointerChasing) { 865 load_s1.io.s1_kill := true.B 866 }.otherwise { 867 load_s0.io.s0_kill := s1_tryPointerChasing && !io.lsqOut.valid 868 when (s1_tryPointerChasing) { 869 io.ldin.ready := true.B 870 } 871 } 872 873 XSPerfAccumulate("load_to_load_forward", s1_tryPointerChasing && !cancelPointerChasing) 874 XSPerfAccumulate("load_to_load_forward_try", s1_tryPointerChasing) 875 XSPerfAccumulate("load_to_load_forward_fail", cancelPointerChasing) 876 XSPerfAccumulate("load_to_load_forward_fail_cancelled", cancelPointerChasing && isCancelled) 877 XSPerfAccumulate("load_to_load_forward_fail_wakeup_mismatch", cancelPointerChasing && !isCancelled && notFastMatch) 878 XSPerfAccumulate("load_to_load_forward_fail_op_not_ld", 879 cancelPointerChasing && !isCancelled && !notFastMatch && fuOpTypeIsNotLd) 880 XSPerfAccumulate("load_to_load_forward_fail_addr_align", 881 cancelPointerChasing && !isCancelled && !notFastMatch && !fuOpTypeIsNotLd && addressNotAligned) 882 XSPerfAccumulate("load_to_load_forward_fail_set_mismatch", 883 cancelPointerChasing && !isCancelled && !notFastMatch && !fuOpTypeIsNotLd && !addressNotAligned && addressMisMatch) 884 } 885 PipelineConnect(load_s1.io.out, load_s2.io.in, true.B, 886 load_s1.io.out.bits.uop.robIdx.needFlush(io.redirect) || cancelPointerChasing) 887 888 val (forward_D, forwardData_D) = io.tlDchannel.forward(load_s1.io.out.valid && load_s1.io.out.bits.forward_tlDchannel, load_s1.io.out.bits.mshrid, load_s1.io.out.bits.paddr) 889 890 io.forward_mshr.valid := load_s1.io.out.valid && load_s1.io.out.bits.forward_tlDchannel 891 io.forward_mshr.mshrid := load_s1.io.out.bits.mshrid 892 io.forward_mshr.paddr := load_s1.io.out.bits.paddr 893 val (forward_result_valid, forward_mshr, forwardData_mshr) = io.forward_mshr.forward() 894 895 XSPerfAccumulate("successfully_forward_channel_D", forward_D && forward_result_valid) 896 XSPerfAccumulate("successfully_forward_mshr", forward_mshr && forward_result_valid) 897 // load s2 898 load_s2.io.forward_D := forward_D 899 load_s2.io.forwardData_D := forwardData_D 900 load_s2.io.forward_result_valid := forward_result_valid 901 load_s2.io.forward_mshr := forward_mshr 902 load_s2.io.forwardData_mshr := forwardData_mshr 903 io.s2IsPointerChasing := RegEnable(s1_tryPointerChasing && !cancelPointerChasing, load_s1.io.out.fire) 904 io.prefetch_train.bits.fromLsPipelineBundle(load_s2.io.in.bits) 905 // override miss bit 906 io.prefetch_train.bits.miss := io.dcache.resp.bits.miss 907 io.prefetch_train.bits.meta_prefetch := io.dcache.resp.bits.meta_prefetch 908 io.prefetch_train.bits.meta_access := io.dcache.resp.bits.meta_access 909 io.prefetch_train.valid := load_s2.io.in.fire && !load_s2.io.out.bits.mmio && !load_s2.io.in.bits.tlbMiss 910 io.dcache.s2_kill := load_s2.io.dcache_kill // to kill mmio resp which are redirected 911 if (env.FPGAPlatform) 912 io.dcache.s2_pc := DontCare 913 else 914 io.dcache.s2_pc := load_s2.io.out.bits.uop.cf.pc 915 load_s2.io.dcacheResp <> io.dcache.resp 916 load_s2.io.pmpResp <> io.pmp 917 load_s2.io.static_pm := RegNext(io.tlb.resp.bits.static_pm) 918 load_s2.io.lsq.forwardData <> io.lsq.forward.forwardData 919 load_s2.io.lsq.forwardMask <> io.lsq.forward.forwardMask 920 load_s2.io.lsq.forwardMaskFast <> io.lsq.forward.forwardMaskFast // should not be used in load_s2 921 load_s2.io.lsq.dataInvalid <> io.lsq.forward.dataInvalid 922 load_s2.io.lsq.matchInvalid <> io.lsq.forward.matchInvalid 923 load_s2.io.sbuffer.forwardData <> io.sbuffer.forwardData 924 load_s2.io.sbuffer.forwardMask <> io.sbuffer.forwardMask 925 load_s2.io.sbuffer.forwardMaskFast <> io.sbuffer.forwardMaskFast // should not be used in load_s2 926 load_s2.io.sbuffer.dataInvalid <> io.sbuffer.dataInvalid // always false 927 load_s2.io.sbuffer.matchInvalid <> io.sbuffer.matchInvalid 928 load_s2.io.dataForwarded <> io.lsq.s2_load_data_forwarded 929 load_s2.io.dataInvalidSqIdx := io.lsq.forward.dataInvalidSqIdx // provide dataInvalidSqIdx to make wakeup faster 930 load_s2.io.loadViolationQueryResp <> io.lsq.loadViolationQuery.resp 931 load_s2.io.csrCtrl <> io.csrCtrl 932 load_s2.io.sentFastUop := io.fastUop.valid 933 load_s2.io.reExecuteQuery := io.reExecuteQuery 934 // feedback bank conflict / ld-vio check struct hazard to rs 935 io.feedbackFast.bits := RegNext(load_s1.io.rsFeedback.bits) 936 io.feedbackFast.valid := RegNext(load_s1.io.rsFeedback.valid && !load_s1.io.out.bits.uop.robIdx.needFlush(io.redirect)) 937 938 // pre-calcuate sqIdx mask in s0, then send it to lsq in s1 for forwarding 939 val sqIdxMaskReg = RegNext(UIntToMask(load_s0.io.s0_sqIdx.value, StoreQueueSize)) 940 // to enable load-load, sqIdxMask must be calculated based on ldin.uop 941 // If the timing here is not OK, load-load forwarding has to be disabled. 942 // Or we calculate sqIdxMask at RS?? 943 io.lsq.forward.sqIdxMask := sqIdxMaskReg 944 if (EnableLoadToLoadForward) { 945 when (s1_tryPointerChasing) { 946 io.lsq.forward.sqIdxMask := UIntToMask(io.ldin.bits.uop.sqIdx.value, StoreQueueSize) 947 } 948 } 949 950 // // use s2_hit_way to select data received in s1 951 // load_s2.io.dcacheResp.bits.data := Mux1H(RegNext(io.dcache.s1_hit_way), RegNext(io.dcache.s1_data)) 952 // assert(load_s2.io.dcacheResp.bits.data === io.dcache.resp.bits.data) 953 954 // now io.fastUop.valid is sent to RS in load_s2 955 val forward_D_or_mshr_valid = forward_result_valid && (forward_D || forward_mshr) 956 val s2_dcache_hit = io.dcache.s2_hit || forward_D_or_mshr_valid // dcache hit dup in lsu side 957 958 io.fastUop.valid := RegNext( 959 !io.dcache.s1_disable_fast_wakeup && // load fast wakeup should be disabled when dcache data read is not ready 960 load_s1.io.in.valid && // valid load request 961 !load_s1.io.in.bits.isHWPrefetch && // is not hardware prefetch req 962 !load_s1.io.s1_kill && // killed by load-load forwarding 963 !load_s1.io.dtlbResp.bits.fast_miss && // not mmio or tlb miss, pf / af not included here 964 !io.lsq.forward.dataInvalidFast // forward failed 965 ) && 966 !RegNext(load_s1.io.needLdVioCheckRedo) && // load-load violation check: load paddr cam struct hazard 967 !RegNext(load_s1.io.needReExecute) && 968 !RegNext(load_s1.io.out.bits.uop.robIdx.needFlush(io.redirect)) && 969 (load_s2.io.in.valid && !load_s2.io.needReExecute && s2_dcache_hit) // dcache hit in lsu side 970 971 io.fastUop.bits := RegNext(load_s1.io.out.bits.uop) 972 973 XSDebug(load_s0.io.out.valid, 974 p"S0: pc ${Hexadecimal(load_s0.io.out.bits.uop.cf.pc)}, lId ${Hexadecimal(load_s0.io.out.bits.uop.lqIdx.asUInt)}, " + 975 p"vaddr ${Hexadecimal(load_s0.io.out.bits.vaddr)}, mask ${Hexadecimal(load_s0.io.out.bits.mask)}\n") 976 XSDebug(load_s1.io.out.valid, 977 p"S1: pc ${Hexadecimal(load_s1.io.out.bits.uop.cf.pc)}, lId ${Hexadecimal(load_s1.io.out.bits.uop.lqIdx.asUInt)}, tlb_miss ${io.tlb.resp.bits.miss}, " + 978 p"paddr ${Hexadecimal(load_s1.io.out.bits.paddr)}, mmio ${load_s1.io.out.bits.mmio}\n") 979 980 // writeback to LSQ 981 // Current dcache use MSHR 982 // Load queue will be updated at s2 for both hit/miss int/fp load 983 io.lsq.loadIn.valid := load_s2.io.out.valid && !load_s2.io.out.bits.isHWPrefetch 984 // generate LqWriteBundle from LsPipelineBundle 985 io.lsq.loadIn.bits.fromLsPipelineBundle(load_s2.io.out.bits) 986 987 io.lsq.replayFast := load_s1.io.replayFast 988 io.lsq.replaySlow := load_s2.io.replaySlow 989 io.lsq.replaySlow.valid := load_s2.io.replaySlow.valid && !load_s2.io.out.bits.uop.robIdx.needFlush(io.redirect) 990 991 // generate duplicated load queue data wen 992 val load_s2_valid_vec = RegInit(0.U(6.W)) 993 val load_s2_leftFire = load_s1.io.out.valid && load_s2.io.in.ready 994 // val write_lq_safe = load_s2.io.write_lq_safe 995 load_s2_valid_vec := 0x0.U(6.W) 996 when (load_s2_leftFire && !load_s1.io.out.bits.isHWPrefetch) { load_s2_valid_vec := 0x3f.U(6.W)} // TODO: refactor me 997 when (load_s1.io.out.bits.uop.robIdx.needFlush(io.redirect)) { load_s2_valid_vec := 0x0.U(6.W) } 998 assert(RegNext((load_s2.io.in.valid === load_s2_valid_vec(0)) || RegNext(load_s1.io.out.bits.isHWPrefetch))) 999 io.lsq.loadIn.bits.lq_data_wen_dup := load_s2_valid_vec.asBools() 1000 1001 // s2_dcache_require_replay signal will be RegNexted, then used in s3 1002 io.lsq.s2_dcache_require_replay := load_s2.io.s2_dcache_require_replay 1003 1004 // write to rob and writeback bus 1005 val s2_wb_valid = load_s2.io.out.valid && !load_s2.io.out.bits.miss && !load_s2.io.out.bits.mmio 1006 1007 // Int load, if hit, will be writebacked at s2 1008 val hitLoadOut = Wire(Valid(new ExuOutput)) 1009 hitLoadOut.valid := s2_wb_valid 1010 hitLoadOut.bits.uop := load_s2.io.out.bits.uop 1011 hitLoadOut.bits.data := load_s2.io.out.bits.data 1012 hitLoadOut.bits.redirectValid := false.B 1013 hitLoadOut.bits.redirect := DontCare 1014 hitLoadOut.bits.debug.isMMIO := load_s2.io.out.bits.mmio 1015 hitLoadOut.bits.debug.isPerfCnt := false.B 1016 hitLoadOut.bits.debug.paddr := load_s2.io.out.bits.paddr 1017 hitLoadOut.bits.debug.vaddr := load_s2.io.out.bits.vaddr 1018 hitLoadOut.bits.fflags := DontCare 1019 1020 load_s2.io.out.ready := true.B 1021 1022 // load s3 1023 val s3_load_wb_meta_reg = RegNext(Mux(hitLoadOut.valid, hitLoadOut.bits, io.lsq.ldout.bits)) 1024 1025 // data from load queue refill 1026 val s3_loadDataFromLQ = RegEnable(io.lsq.ldRawData, io.lsq.ldout.valid) 1027 val s3_rdataLQ = s3_loadDataFromLQ.mergedData() 1028 val s3_rdataSelLQ = LookupTree(s3_loadDataFromLQ.addrOffset, List( 1029 "b000".U -> s3_rdataLQ(63, 0), 1030 "b001".U -> s3_rdataLQ(63, 8), 1031 "b010".U -> s3_rdataLQ(63, 16), 1032 "b011".U -> s3_rdataLQ(63, 24), 1033 "b100".U -> s3_rdataLQ(63, 32), 1034 "b101".U -> s3_rdataLQ(63, 40), 1035 "b110".U -> s3_rdataLQ(63, 48), 1036 "b111".U -> s3_rdataLQ(63, 56) 1037 )) 1038 val s3_rdataPartialLoadLQ = rdataHelper(s3_loadDataFromLQ.uop, s3_rdataSelLQ) 1039 1040 // data from dcache hit 1041 val s3_loadDataFromDcache = load_s2.io.loadDataFromDcache 1042 val s3_rdataDcache = s3_loadDataFromDcache.mergedData() 1043 val s3_rdataSelDcache = LookupTree(s3_loadDataFromDcache.addrOffset, List( 1044 "b000".U -> s3_rdataDcache(63, 0), 1045 "b001".U -> s3_rdataDcache(63, 8), 1046 "b010".U -> s3_rdataDcache(63, 16), 1047 "b011".U -> s3_rdataDcache(63, 24), 1048 "b100".U -> s3_rdataDcache(63, 32), 1049 "b101".U -> s3_rdataDcache(63, 40), 1050 "b110".U -> s3_rdataDcache(63, 48), 1051 "b111".U -> s3_rdataDcache(63, 56) 1052 )) 1053 val s3_rdataPartialLoadDcache = rdataHelper(s3_loadDataFromDcache.uop, s3_rdataSelDcache) 1054 1055 io.ldout.bits := s3_load_wb_meta_reg 1056 io.ldout.bits.data := Mux(RegNext(hitLoadOut.valid), s3_rdataPartialLoadDcache, s3_rdataPartialLoadLQ) 1057 io.ldout.valid := RegNext(hitLoadOut.valid) && !RegNext(load_s2.io.out.bits.uop.robIdx.needFlush(io.redirect)) || 1058 RegNext(io.lsq.ldout.valid) && !RegNext(io.lsq.ldout.bits.uop.robIdx.needFlush(io.redirect)) && !RegNext(hitLoadOut.valid) 1059 1060 io.ldout.bits.uop.cf.exceptionVec(loadAccessFault) := s3_load_wb_meta_reg.uop.cf.exceptionVec(loadAccessFault) || 1061 RegNext(hitLoadOut.valid) && load_s2.io.s3_delayed_load_error 1062 1063 // fast load to load forward 1064 io.fastpathOut.valid := RegNext(load_s2.io.out.valid) // for debug only 1065 io.fastpathOut.data := s3_loadDataFromDcache.mergedData() // fastpath is for ld only 1066 1067 // feedback tlb miss / dcache miss queue full 1068 io.feedbackSlow.bits := RegNext(load_s2.io.rsFeedback.bits) 1069 io.feedbackSlow.valid := RegNext(load_s2.io.rsFeedback.valid && !load_s2.io.out.bits.uop.robIdx.needFlush(io.redirect)) 1070 // If replay is reported at load_s1, inst will be canceled (will not enter load_s2), 1071 // in that case: 1072 // * replay should not be reported twice 1073 assert(!(RegNext(io.feedbackFast.valid) && io.feedbackSlow.valid)) 1074 // * io.fastUop.valid should not be reported 1075 assert(!RegNext(io.feedbackFast.valid && !io.feedbackFast.bits.hit && io.fastUop.valid)) 1076 1077 // load forward_fail/ldld_violation check 1078 // check for inst in load pipeline 1079 val s3_forward_fail = RegNext(io.lsq.forward.matchInvalid || io.sbuffer.matchInvalid) 1080 val s3_ldld_violation = RegNext( 1081 io.lsq.loadViolationQuery.resp.valid && 1082 io.lsq.loadViolationQuery.resp.bits.have_violation && 1083 RegNext(io.csrCtrl.ldld_vio_check_enable) 1084 ) 1085 val s3_need_replay_from_fetch = s3_forward_fail || s3_ldld_violation 1086 val s3_can_replay_from_fetch = RegEnable(load_s2.io.s2_can_replay_from_fetch, load_s2.io.out.valid) 1087 // 1) use load pipe check result generated in load_s3 iff load_hit 1088 when (RegNext(hitLoadOut.valid)) { 1089 io.ldout.bits.uop.ctrl.replayInst := s3_need_replay_from_fetch 1090 } 1091 // 2) otherwise, write check result to load queue 1092 io.lsq.s3_replay_from_fetch := s3_need_replay_from_fetch && s3_can_replay_from_fetch 1093 1094 // s3_delayed_load_error path is not used for now, as we writeback load result in load_s3 1095 // but we keep this path for future use 1096 io.s3_delayed_load_error := false.B 1097 io.lsq.s3_delayed_load_error := false.B //load_s2.io.s3_delayed_load_error 1098 1099 io.lsq.ldout.ready := !hitLoadOut.valid 1100 1101 when(io.feedbackSlow.valid && !io.feedbackSlow.bits.hit){ 1102 // when need replay from rs, inst should not be writebacked to rob 1103 assert(RegNext(!hitLoadOut.valid)) 1104 assert(RegNext(!io.lsq.loadIn.valid) || RegNext(load_s2.io.s2_dcache_require_replay)) 1105 } 1106 1107 // hareware prefetch to l1 1108 io.prefetch_req <> load_s0.io.prefetch_in 1109 1110 // trigger 1111 val lastValidData = RegEnable(io.ldout.bits.data, io.ldout.fire) 1112 val hitLoadAddrTriggerHitVec = Wire(Vec(3, Bool())) 1113 val lqLoadAddrTriggerHitVec = io.lsq.trigger.lqLoadAddrTriggerHitVec 1114 (0 until 3).map{i => { 1115 val tdata2 = io.trigger(i).tdata2 1116 val matchType = io.trigger(i).matchType 1117 val tEnable = io.trigger(i).tEnable 1118 1119 hitLoadAddrTriggerHitVec(i) := TriggerCmp(load_s2.io.out.bits.vaddr, tdata2, matchType, tEnable) 1120 io.trigger(i).addrHit := Mux(hitLoadOut.valid, hitLoadAddrTriggerHitVec(i), lqLoadAddrTriggerHitVec(i)) 1121 io.trigger(i).lastDataHit := TriggerCmp(lastValidData, tdata2, matchType, tEnable) 1122 }} 1123 io.lsq.trigger.hitLoadAddrTriggerHitVec := hitLoadAddrTriggerHitVec 1124 1125 // hardware performance counter 1126 val perfEvents = Seq( 1127 ("load_s0_in_fire ", load_s0.io.in.fire ), 1128 ("load_to_load_forward ", load_s1.io.out.valid && s1_tryPointerChasing && !cancelPointerChasing ), 1129 ("stall_dcache ", load_s0.io.out.valid && load_s0.io.out.ready && !load_s0.io.dcacheReq.ready ), 1130 ("load_s1_in_fire ", load_s1.io.in.fire ), 1131 ("load_s1_tlb_miss ", load_s1.io.in.fire && load_s1.io.dtlbResp.bits.miss ), 1132 ("load_s2_in_fire ", load_s2.io.in.fire ), 1133 ("load_s2_dcache_miss ", load_s2.io.in.fire && load_s2.io.dcacheResp.bits.miss ), 1134 ("load_s2_replay ", load_s2.io.rsFeedback.valid && !load_s2.io.rsFeedback.bits.hit ), 1135 ("load_s2_replay_tlb_miss ", load_s2.io.rsFeedback.valid && !load_s2.io.rsFeedback.bits.hit && load_s2.io.in.bits.tlbMiss ), 1136 ("load_s2_replay_cache ", load_s2.io.rsFeedback.valid && !load_s2.io.rsFeedback.bits.hit && !load_s2.io.in.bits.tlbMiss && load_s2.io.dcacheResp.bits.miss), 1137 ) 1138 generatePerfEvent() 1139 1140 when(io.ldout.fire){ 1141 XSDebug("ldout %x\n", io.ldout.bits.uop.cf.pc) 1142 } 1143} 1144