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._ 25import xiangshan.backend.fu.fpu.FPU 26import xiangshan.backend.rob.RobLsqIO 27import xiangshan.cache._ 28import xiangshan.frontend.FtqPtr 29import xiangshan.ExceptionNO._ 30import chisel3.ExcitingUtils 31import xiangshan.cache.dcache.ReplayCarry 32 33class LqPtr(implicit p: Parameters) extends CircularQueuePtr[LqPtr]( 34 p => p(XSCoreParamsKey).LoadQueueSize 35){ 36} 37 38object LqPtr { 39 def apply(f: Bool, v: UInt)(implicit p: Parameters): LqPtr = { 40 val ptr = Wire(new LqPtr) 41 ptr.flag := f 42 ptr.value := v 43 ptr 44 } 45} 46 47trait HasLoadHelper { this: XSModule => 48 def rdataHelper(uop: MicroOp, rdata: UInt): UInt = { 49 val fpWen = uop.ctrl.fpWen 50 LookupTree(uop.ctrl.fuOpType, List( 51 LSUOpType.lb -> SignExt(rdata(7, 0) , XLEN), 52 LSUOpType.lh -> SignExt(rdata(15, 0), XLEN), 53 /* 54 riscv-spec-20191213: 12.2 NaN Boxing of Narrower Values 55 Any operation that writes a narrower result to an f register must write 56 all 1s to the uppermost FLEN−n bits to yield a legal NaN-boxed value. 57 */ 58 LSUOpType.lw -> Mux(fpWen, FPU.box(rdata, FPU.S), SignExt(rdata(31, 0), XLEN)), 59 LSUOpType.ld -> Mux(fpWen, FPU.box(rdata, FPU.D), SignExt(rdata(63, 0), XLEN)), 60 LSUOpType.lbu -> ZeroExt(rdata(7, 0) , XLEN), 61 LSUOpType.lhu -> ZeroExt(rdata(15, 0), XLEN), 62 LSUOpType.lwu -> ZeroExt(rdata(31, 0), XLEN), 63 )) 64 } 65} 66 67class LqEnqIO(implicit p: Parameters) extends XSBundle { 68 val canAccept = Output(Bool()) 69 val sqCanAccept = Input(Bool()) 70 val needAlloc = Vec(exuParameters.LsExuCnt, Input(Bool())) 71 val req = Vec(exuParameters.LsExuCnt, Flipped(ValidIO(new MicroOp))) 72 val resp = Vec(exuParameters.LsExuCnt, Output(new LqPtr)) 73} 74 75class LqPaddrWriteBundle(implicit p: Parameters) extends XSBundle { 76 val paddr = Output(UInt(PAddrBits.W)) 77 val lqIdx = Output(new LqPtr) 78} 79 80class LqVaddrWriteBundle(implicit p: Parameters) extends XSBundle { 81 val vaddr = Output(UInt(VAddrBits.W)) 82 val lqIdx = Output(new LqPtr) 83} 84 85class LqTriggerIO(implicit p: Parameters) extends XSBundle { 86 val hitLoadAddrTriggerHitVec = Input(Vec(3, Bool())) 87 val lqLoadAddrTriggerHitVec = Output(Vec(3, Bool())) 88} 89 90class LoadQueueIOBundle(implicit p: Parameters) extends XSBundle { 91 val enq = new LqEnqIO 92 val brqRedirect = Flipped(ValidIO(new Redirect)) 93 val loadOut = Vec(LoadPipelineWidth, Decoupled(new LsPipelineBundle)) // select load from lq to load pipeline 94 val loadPaddrIn = Vec(LoadPipelineWidth, Flipped(Valid(new LqPaddrWriteBundle))) 95 val loadVaddrIn = Vec(LoadPipelineWidth, Flipped(Valid(new LqVaddrWriteBundle))) 96 val loadIn = Vec(LoadPipelineWidth, Flipped(Valid(new LqWriteBundle))) 97 val storeIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) 98 val s2_load_data_forwarded = Vec(LoadPipelineWidth, Input(Bool())) 99 val s3_delayed_load_error = Vec(LoadPipelineWidth, Input(Bool())) 100 val s2_dcache_require_replay = Vec(LoadPipelineWidth, Input(Bool())) 101 val s3_replay_from_fetch = Vec(LoadPipelineWidth, Input(Bool())) 102 val ldout = Vec(2, DecoupledIO(new ExuOutput)) // writeback int load 103 val ldRawDataOut = Vec(2, Output(new LoadDataFromLQBundle)) 104 val load_s1 = Vec(LoadPipelineWidth, Flipped(new PipeLoadForwardQueryIO)) // TODO: to be renamed 105 val loadViolationQuery = Vec(LoadPipelineWidth, Flipped(new LoadViolationQueryIO)) 106 val rob = Flipped(new RobLsqIO) 107 val rollback = Output(Valid(new Redirect)) // replay now starts from load instead of store 108 val refill = Flipped(ValidIO(new Refill)) // TODO: to be renamed 109 val release = Flipped(ValidIO(new Release)) 110 val uncache = new UncacheWordIO 111 val exceptionAddr = new ExceptionAddrIO 112 val lqFull = Output(Bool()) 113 val lqCancelCnt = Output(UInt(log2Up(LoadQueueSize + 1).W)) 114 val trigger = Vec(LoadPipelineWidth, new LqTriggerIO) 115 116 // for load replay (recieve feedback from load pipe line) 117 val replayFast = Vec(LoadPipelineWidth, Flipped(new LoadToLsqFastIO)) 118 val replaySlow = Vec(LoadPipelineWidth, Flipped(new LoadToLsqSlowIO)) 119 120 val storeDataValidVec = Vec(StoreQueueSize, Input(Bool())) 121 122 val tlbReplayDelayCycleCtrl = Vec(4, Input(UInt(ReSelectLen.W))) 123} 124 125// Load Queue 126class LoadQueue(implicit p: Parameters) extends XSModule 127 with HasDCacheParameters 128 with HasCircularQueuePtrHelper 129 with HasLoadHelper 130 with HasPerfEvents 131{ 132 val io = IO(new LoadQueueIOBundle()) 133 134 // dontTouch(io) 135 136 println("LoadQueue: size:" + LoadQueueSize) 137 138 val uop = Reg(Vec(LoadQueueSize, new MicroOp)) 139 val replayCarryReg = RegInit(VecInit(List.fill(LoadQueueSize)(ReplayCarry(0.U, false.B)))) 140 // val data = Reg(Vec(LoadQueueSize, new LsRobEntry)) 141 val dataModule = Module(new LoadQueueDataWrapper(LoadQueueSize, wbNumWrite = LoadPipelineWidth)) 142 dataModule.io := DontCare 143 // vaddrModule's read port 0 for exception addr, port 1 for uncache vaddr read, port {2, 3} for load replay 144 val vaddrModule = Module(new SyncDataModuleTemplate(UInt(VAddrBits.W), LoadQueueSize, numRead = 1 + 1 + LoadPipelineWidth, numWrite = LoadPipelineWidth)) 145 vaddrModule.io := DontCare 146 val vaddrTriggerResultModule = Module(new SyncDataModuleTemplate(Vec(3, Bool()), LoadQueueSize, numRead = LoadPipelineWidth, numWrite = LoadPipelineWidth)) 147 vaddrTriggerResultModule.io := DontCare 148 val allocated = RegInit(VecInit(List.fill(LoadQueueSize)(false.B))) // lq entry has been allocated 149 val datavalid = RegInit(VecInit(List.fill(LoadQueueSize)(false.B))) // data is valid 150 val writebacked = RegInit(VecInit(List.fill(LoadQueueSize)(false.B))) // inst has been writebacked to CDB 151 val released = RegInit(VecInit(List.fill(LoadQueueSize)(false.B))) // load data has been released by dcache 152 val error = RegInit(VecInit(List.fill(LoadQueueSize)(false.B))) // load data has been corrupted 153 val miss = Reg(Vec(LoadQueueSize, Bool())) // load inst missed, waiting for miss queue to accept miss request 154 // val listening = Reg(Vec(LoadQueueSize, Bool())) // waiting for refill result 155 val pending = Reg(Vec(LoadQueueSize, Bool())) // mmio pending: inst is an mmio inst, it will not be executed until it reachs the end of rob 156 val refilling = WireInit(VecInit(List.fill(LoadQueueSize)(false.B))) // inst has been writebacked to CDB 157 158 /** 159 * used for load replay control 160 */ 161 162 val tlb_hited = RegInit(VecInit(List.fill(LoadQueueSize)(true.B))) 163 val ld_ld_check_ok = RegInit(VecInit(List.fill(LoadQueueSize)(true.B))) 164 val st_ld_check_ok = RegInit(VecInit(List.fill(LoadQueueSize)(true.B))) 165 val cache_bank_no_conflict = RegInit(VecInit(List.fill(LoadQueueSize)(true.B))) 166 val cache_no_replay = RegInit(VecInit(List.fill(LoadQueueSize)(true.B))) 167 val forward_data_valid = RegInit(VecInit(List.fill(LoadQueueSize)(true.B))) 168 val cache_hited = RegInit(VecInit(List.fill(LoadQueueSize)(true.B))) 169 170 171 /** 172 * used for re-select control 173 */ 174 175 val credit = RegInit(VecInit(List.fill(LoadQueueSize)(0.U(ReSelectLen.W)))) 176 177 // ptrs to control which cycle to choose 178 val block_ptr_tlb = RegInit(VecInit(List.fill(LoadQueueSize)(0.U(2.W)))) 179 val block_ptr_cache = RegInit(VecInit(List.fill(LoadQueueSize)(0.U(2.W)))) 180 val block_ptr_others = RegInit(VecInit(List.fill(LoadQueueSize)(0.U(2.W)))) 181 182 // specific cycles to block 183 val block_cycles_tlb = Reg(Vec(4, UInt(ReSelectLen.W))) 184 block_cycles_tlb := io.tlbReplayDelayCycleCtrl 185 val block_cycles_cache = RegInit(VecInit(Seq(11.U(ReSelectLen.W), 18.U(ReSelectLen.W), 127.U(ReSelectLen.W), 17.U(ReSelectLen.W)))) 186 val block_cycles_others = RegInit(VecInit(Seq(0.U(ReSelectLen.W), 0.U(ReSelectLen.W), 0.U(ReSelectLen.W), 0.U(ReSelectLen.W)))) 187 188 XSPerfAccumulate("block_in_last", PopCount((0 until LoadQueueSize).map(i => block_ptr_cache(i) === 3.U))) 189 190 val sel_blocked = RegInit(VecInit(List.fill(LoadQueueSize)(false.B))) 191 192 // data forward block 193 val block_sq_idx = RegInit(VecInit(List.fill(LoadQueueSize)(0.U((log2Ceil(StoreQueueSize).W))))) 194 val block_by_data_forward_fail = RegInit(VecInit(List.fill(LoadQueueSize)(false.B))) 195 196 // dcache miss block 197 val miss_mshr_id = RegInit(VecInit(List.fill(LoadQueueSize)(0.U((log2Up(cfg.nMissEntries).W))))) 198 val block_by_cache_miss = RegInit(VecInit(List.fill(LoadQueueSize)(false.B))) 199 200 val true_cache_miss_replay = WireInit(VecInit(List.fill(LoadQueueSize)(false.B))) 201 (0 until LoadQueueSize).map{i => { 202 true_cache_miss_replay(i) := tlb_hited(i) && ld_ld_check_ok(i) && st_ld_check_ok(i) && cache_bank_no_conflict(i) && 203 cache_no_replay(i) && forward_data_valid(i) && !cache_hited(i) 204 }} 205 206 val creditUpdate = WireInit(VecInit(List.fill(LoadQueueSize)(0.U(ReSelectLen.W)))) 207 208 credit := creditUpdate 209 210 (0 until LoadQueueSize).map(i => { 211 creditUpdate(i) := Mux(credit(i) > 0.U(ReSelectLen.W), credit(i) - 1.U(ReSelectLen.W), credit(i)) 212 sel_blocked(i) := creditUpdate(i) =/= 0.U(ReSelectLen.W) 213 }) 214 215 (0 until LoadQueueSize).map(i => { 216 block_by_data_forward_fail(i) := Mux(block_by_data_forward_fail(i) === true.B && io.storeDataValidVec(block_sq_idx(i)) === true.B , false.B, block_by_data_forward_fail(i)) 217 }) 218 219 (0 until LoadQueueSize).map(i => { 220 block_by_cache_miss(i) := Mux(block_by_cache_miss(i) === true.B && io.refill.valid && io.refill.bits.id === miss_mshr_id(i), false.B, block_by_cache_miss(i)) 221 when(creditUpdate(i) === 0.U && block_by_cache_miss(i) === true.B) { 222 block_by_cache_miss(i) := false.B 223 } 224 when(block_by_cache_miss(i) === true.B && io.refill.valid && io.refill.bits.id === miss_mshr_id(i)) { 225 creditUpdate(i) := 0.U 226 } 227 }) 228 229 val debug_mmio = Reg(Vec(LoadQueueSize, Bool())) // mmio: inst is an mmio inst 230 val debug_paddr = Reg(Vec(LoadQueueSize, UInt(PAddrBits.W))) // mmio: inst is an mmio inst 231 232 val enqPtrExt = RegInit(VecInit((0 until io.enq.req.length).map(_.U.asTypeOf(new LqPtr)))) 233 val deqPtrExt = RegInit(0.U.asTypeOf(new LqPtr)) 234 val deqPtrExtNext = Wire(new LqPtr) 235 236 val enqPtr = enqPtrExt(0).value 237 val deqPtr = deqPtrExt.value 238 239 val validCount = distanceBetween(enqPtrExt(0), deqPtrExt) 240 val allowEnqueue = validCount <= (LoadQueueSize - LoadPipelineWidth).U 241 242 val deqMask = UIntToMask(deqPtr, LoadQueueSize) 243 val enqMask = UIntToMask(enqPtr, LoadQueueSize) 244 245 val commitCount = RegNext(io.rob.lcommit) 246 247 val release1cycle = io.release 248 val release2cycle = RegNext(io.release) 249 val release2cycle_dup_lsu = RegNext(io.release) 250 251 /** 252 * Enqueue at dispatch 253 * 254 * Currently, LoadQueue only allows enqueue when #emptyEntries > EnqWidth 255 */ 256 io.enq.canAccept := allowEnqueue 257 258 val canEnqueue = io.enq.req.map(_.valid) 259 val enqCancel = io.enq.req.map(_.bits.robIdx.needFlush(io.brqRedirect)) 260 for (i <- 0 until io.enq.req.length) { 261 val offset = if (i == 0) 0.U else PopCount(io.enq.needAlloc.take(i)) 262 val lqIdx = enqPtrExt(offset) 263 val index = io.enq.req(i).bits.lqIdx.value 264 when (canEnqueue(i) && !enqCancel(i)) { 265 uop(index) := io.enq.req(i).bits 266 // NOTE: the index will be used when replay 267 uop(index).lqIdx := lqIdx 268 allocated(index) := true.B 269 datavalid(index) := false.B 270 writebacked(index) := false.B 271 released(index) := false.B 272 miss(index) := false.B 273 pending(index) := false.B 274 error(index) := false.B 275 276 /** 277 * used for load replay control 278 */ 279 tlb_hited(index) := true.B 280 ld_ld_check_ok(index) := true.B 281 st_ld_check_ok(index) := true.B 282 cache_bank_no_conflict(index) := true.B 283 cache_no_replay(index) := true.B 284 forward_data_valid(index) := true.B 285 cache_hited(index) := true.B 286 287 /** 288 * used for delaying load(block-ptr to control how many cycles to block) 289 */ 290 credit(index) := 0.U(ReSelectLen.W) 291 block_ptr_tlb(index) := 0.U(2.W) 292 block_ptr_cache(index) := 0.U(2.W) 293 block_ptr_others(index) := 0.U(2.W) 294 295 block_by_data_forward_fail(index) := false.B 296 block_by_cache_miss(index) := false.B 297 298 XSError(!io.enq.canAccept || !io.enq.sqCanAccept, s"must accept $i\n") 299 XSError(index =/= lqIdx.value, s"must be the same entry $i\n") 300 } 301 io.enq.resp(i) := lqIdx 302 } 303 XSDebug(p"(ready, valid): ${io.enq.canAccept}, ${Binary(Cat(io.enq.req.map(_.valid)))}\n") 304 305 val lastCycleRedirect = RegNext(io.brqRedirect) 306 val lastlastCycleRedirect = RegNext(lastCycleRedirect) 307 308 // replay logic 309 // replay is splited into 2 stages 310 311 // stage1: select 2 entries and read their vaddr 312 val s0_block_load_mask = WireInit(VecInit((0 until LoadQueueSize).map(x=>false.B))) 313 val s1_block_load_mask = RegNext(s0_block_load_mask) 314 val s2_block_load_mask = RegNext(s1_block_load_mask) 315 316 val loadReplaySel = Wire(Vec(LoadPipelineWidth, UInt(log2Up(LoadQueueSize).W))) // index selected last cycle 317 val loadReplaySelV = Wire(Vec(LoadPipelineWidth, Bool())) // index selected in last cycle is valid 318 319 val loadReplaySelVec = VecInit((0 until LoadQueueSize).map(i => { 320 val blocked = s1_block_load_mask(i) || s2_block_load_mask(i) || sel_blocked(i) || block_by_data_forward_fail(i) || block_by_cache_miss(i) 321 allocated(i) && (!tlb_hited(i) || !ld_ld_check_ok(i) || !st_ld_check_ok(i) || !cache_bank_no_conflict(i) || !cache_no_replay(i) || !forward_data_valid(i) || !cache_hited(i)) && !blocked 322 })).asUInt() // use uint instead vec to reduce verilog lines 323 324 val remReplayDeqMask = Seq.tabulate(LoadPipelineWidth)(getRemBits(deqMask)(_)) 325 326 // generate lastCycleSelect mask 327 val remReplayFireMask = Seq.tabulate(LoadPipelineWidth)(rem => getRemBits(UIntToOH(loadReplaySel(rem)))(rem)) 328 329 val loadReplayRemSelVecFire = Seq.tabulate(LoadPipelineWidth)(rem => getRemBits(loadReplaySelVec)(rem) & ~remReplayFireMask(rem)) 330 val loadReplayRemSelVecNotFire = Seq.tabulate(LoadPipelineWidth)(getRemBits(loadReplaySelVec)(_)) 331 332 val replayRemFire = Seq.tabulate(LoadPipelineWidth)(rem => WireInit(false.B)) 333 334 val loadReplayRemSel = Seq.tabulate(LoadPipelineWidth)(rem => Mux( 335 replayRemFire(rem), 336 getFirstOne(toVec(loadReplayRemSelVecFire(rem)), remReplayDeqMask(rem)), 337 getFirstOne(toVec(loadReplayRemSelVecNotFire(rem)), remReplayDeqMask(rem)) 338 )) 339 340 val loadReplaySelGen = Wire(Vec(LoadPipelineWidth, UInt(log2Up(LoadQueueSize).W))) 341 val loadReplaySelVGen = Wire(Vec(LoadPipelineWidth, Bool())) 342 343 (0 until LoadPipelineWidth).foreach(index => { 344 loadReplaySelGen(index) := ( 345 if (LoadPipelineWidth > 1) Cat(loadReplayRemSel(index), index.U(log2Ceil(LoadPipelineWidth).W)) 346 else loadReplayRemSel(index) 347 ) 348 loadReplaySelVGen(index) := Mux(replayRemFire(index), loadReplayRemSelVecFire(index).asUInt.orR, loadReplayRemSelVecNotFire(index).asUInt.orR) 349 }) 350 351 (0 until LoadPipelineWidth).map(i => { 352 // vaddrModule rport 0 and 1 is used by exception and mmio 353 vaddrModule.io.raddr(2 + i) := loadReplaySelGen(i) 354 }) 355 356 (0 until LoadPipelineWidth).map(i => { 357 loadReplaySel(i) := RegNext(loadReplaySelGen(i)) 358 loadReplaySelV(i) := RegNext(loadReplaySelVGen(i), init = false.B) 359 }) 360 361 // stage2: replay to load pipeline (if no load in S0) 362 (0 until LoadPipelineWidth).map(i => { 363 when(replayRemFire(i)) { 364 s0_block_load_mask(loadReplaySel(i)) := true.B 365 } 366 }) 367 368 // init 369 (0 until LoadPipelineWidth).map(i => { 370 replayRemFire(i) := false.B 371 }) 372 373 for(i <- 0 until LoadPipelineWidth) { 374 val replayIdx = loadReplaySel(i) 375 val notRedirectLastCycle = !uop(replayIdx).robIdx.needFlush(RegNext(io.brqRedirect)) 376 377 io.loadOut(i).valid := loadReplaySelV(i) && notRedirectLastCycle 378 379 io.loadOut(i).bits := DontCare 380 io.loadOut(i).bits.uop := uop(replayIdx) 381 io.loadOut(i).bits.vaddr := vaddrModule.io.rdata(LoadPipelineWidth + i) 382 io.loadOut(i).bits.mask := genWmask(vaddrModule.io.rdata(LoadPipelineWidth + i), uop(replayIdx).ctrl.fuOpType(1,0)) 383 io.loadOut(i).bits.isFirstIssue := false.B 384 io.loadOut(i).bits.isLoadReplay := true.B 385 io.loadOut(i).bits.replayCarry := replayCarryReg(replayIdx) 386 io.loadOut(i).bits.mshrid := miss_mshr_id(replayIdx) 387 io.loadOut(i).bits.forward_tlDchannel := !cache_hited(replayIdx) 388 389 when(io.loadOut(i).fire) { 390 replayRemFire(i) := true.B 391 } 392 393 } 394 /** 395 * Writeback load from load units 396 * 397 * Most load instructions writeback to regfile at the same time. 398 * However, 399 * (1) For an mmio instruction with exceptions, it writes back to ROB immediately. 400 * (2) For an mmio instruction without exceptions, it does not write back. 401 * The mmio instruction will be sent to lower level when it reaches ROB's head. 402 * After uncache response, it will write back through arbiter with loadUnit. 403 * (3) For cache misses, it is marked miss and sent to dcache later. 404 * After cache refills, it will write back through arbiter with loadUnit. 405 */ 406 for (i <- 0 until LoadPipelineWidth) { 407 dataModule.io.wb.wen(i) := false.B 408 dataModule.io.paddr.wen(i) := false.B 409 vaddrModule.io.wen(i) := false.B 410 vaddrTriggerResultModule.io.wen(i) := false.B 411 val loadWbIndex = io.loadIn(i).bits.uop.lqIdx.value 412 413 // most lq status need to be updated immediately after load writeback to lq 414 // flag bits in lq needs to be updated accurately 415 when(io.loadIn(i).fire()) { 416 when(io.loadIn(i).bits.miss) { 417 XSInfo(io.loadIn(i).valid, "load miss write to lq idx %d pc 0x%x vaddr %x paddr %x mask %x forwardData %x forwardMask: %x mmio %x\n", 418 io.loadIn(i).bits.uop.lqIdx.asUInt, 419 io.loadIn(i).bits.uop.cf.pc, 420 io.loadIn(i).bits.vaddr, 421 io.loadIn(i).bits.paddr, 422 io.loadIn(i).bits.mask, 423 io.loadIn(i).bits.forwardData.asUInt, 424 io.loadIn(i).bits.forwardMask.asUInt, 425 io.loadIn(i).bits.mmio 426 ) 427 }.otherwise { 428 XSInfo(io.loadIn(i).valid, "load hit write to cbd lqidx %d pc 0x%x vaddr %x paddr %x mask %x forwardData %x forwardMask: %x mmio %x\n", 429 io.loadIn(i).bits.uop.lqIdx.asUInt, 430 io.loadIn(i).bits.uop.cf.pc, 431 io.loadIn(i).bits.vaddr, 432 io.loadIn(i).bits.paddr, 433 io.loadIn(i).bits.mask, 434 io.loadIn(i).bits.forwardData.asUInt, 435 io.loadIn(i).bits.forwardMask.asUInt, 436 io.loadIn(i).bits.mmio 437 )} 438 if(EnableFastForward){ 439 datavalid(loadWbIndex) := !io.loadIn(i).bits.miss && 440 !io.loadIn(i).bits.mmio && // mmio data is not valid until we finished uncache access 441 !io.s2_dcache_require_replay(i) // do not writeback if that inst will be resend from rs 442 } else { 443 datavalid(loadWbIndex) := !io.loadIn(i).bits.miss && 444 !io.loadIn(i).bits.mmio // mmio data is not valid until we finished uncache access 445 } 446 writebacked(loadWbIndex) := !io.loadIn(i).bits.miss && !io.loadIn(i).bits.mmio 447 448 debug_mmio(loadWbIndex) := io.loadIn(i).bits.mmio 449 debug_paddr(loadWbIndex) := io.loadIn(i).bits.paddr 450 451 val dcacheMissed = io.loadIn(i).bits.miss && !io.loadIn(i).bits.mmio 452 if(EnableFastForward){ 453 miss(loadWbIndex) := dcacheMissed && !io.s2_load_data_forwarded(i) && !io.s2_dcache_require_replay(i) 454 } else { 455 miss(loadWbIndex) := dcacheMissed && !io.s2_load_data_forwarded(i) 456 } 457 pending(loadWbIndex) := io.loadIn(i).bits.mmio 458 released(loadWbIndex) := release2cycle.valid && 459 io.loadIn(i).bits.paddr(PAddrBits-1, DCacheLineOffset) === release2cycle.bits.paddr(PAddrBits-1, DCacheLineOffset) || 460 release1cycle.valid && 461 io.loadIn(i).bits.paddr(PAddrBits-1, DCacheLineOffset) === release1cycle.bits.paddr(PAddrBits-1, DCacheLineOffset) 462 } 463 464 // data bit in lq can be updated when load_s2 valid 465 // when(io.loadIn(i).bits.lq_data_wen){ 466 // val loadWbData = Wire(new LQDataEntry) 467 // loadWbData.paddr := io.loadIn(i).bits.paddr 468 // loadWbData.mask := io.loadIn(i).bits.mask 469 // loadWbData.data := io.loadIn(i).bits.forwardData.asUInt // fwd data 470 // loadWbData.fwdMask := io.loadIn(i).bits.forwardMask 471 // dataModule.io.wbWrite(i, loadWbIndex, loadWbData) 472 // dataModule.io.wb.wen(i) := true.B 473 474 // // dirty code for load instr 475 // uop(loadWbIndex).pdest := io.loadIn(i).bits.uop.pdest 476 // uop(loadWbIndex).cf := io.loadIn(i).bits.uop.cf 477 // uop(loadWbIndex).ctrl := io.loadIn(i).bits.uop.ctrl 478 // uop(loadWbIndex).debugInfo := io.loadIn(i).bits.uop.debugInfo 479 480 // vaddrTriggerResultModule.io.waddr(i) := loadWbIndex 481 // vaddrTriggerResultModule.io.wdata(i) := io.trigger(i).hitLoadAddrTriggerHitVec 482 483 // vaddrTriggerResultModule.io.wen(i) := true.B 484 // } 485 486 // dirty code to reduce load_s2.valid fanout 487 when(io.loadIn(i).bits.lq_data_wen_dup(0)){ 488 dataModule.io.wbWrite(i, loadWbIndex, io.loadIn(i).bits.mask) 489 dataModule.io.wb.wen(i) := true.B 490 } 491 // dirty code for load instr 492 when(io.loadIn(i).bits.lq_data_wen_dup(1)){ 493 uop(loadWbIndex).pdest := io.loadIn(i).bits.uop.pdest 494 } 495 when(io.loadIn(i).bits.lq_data_wen_dup(2)){ 496 uop(loadWbIndex).cf := io.loadIn(i).bits.uop.cf 497 } 498 when(io.loadIn(i).bits.lq_data_wen_dup(3)){ 499 uop(loadWbIndex).ctrl := io.loadIn(i).bits.uop.ctrl 500 } 501 when(io.loadIn(i).bits.lq_data_wen_dup(4)){ 502 uop(loadWbIndex).debugInfo := io.loadIn(i).bits.uop.debugInfo 503 } 504 when(io.loadIn(i).bits.lq_data_wen_dup(5)){ 505 vaddrTriggerResultModule.io.waddr(i) := loadWbIndex 506 vaddrTriggerResultModule.io.wdata(i) := io.trigger(i).hitLoadAddrTriggerHitVec 507 vaddrTriggerResultModule.io.wen(i) := true.B 508 } 509 510 when(io.loadPaddrIn(i).valid) { 511 dataModule.io.paddr.wen(i) := true.B 512 dataModule.io.paddr.waddr(i) := io.loadPaddrIn(i).bits.lqIdx.value 513 dataModule.io.paddr.wdata(i) := io.loadPaddrIn(i).bits.paddr 514 } 515 516 // update vaddr in load S1 517 when(io.loadVaddrIn(i).valid) { 518 vaddrModule.io.wen(i) := true.B 519 vaddrModule.io.waddr(i) := io.loadVaddrIn(i).bits.lqIdx.value 520 vaddrModule.io.wdata(i) := io.loadVaddrIn(i).bits.vaddr 521 } 522 523 /** 524 * used for feedback and replay 525 */ 526 when(io.replayFast(i).valid){ 527 val idx = io.replayFast(i).ld_idx 528 529 ld_ld_check_ok(idx) := io.replayFast(i).ld_ld_check_ok 530 st_ld_check_ok(idx) := io.replayFast(i).st_ld_check_ok 531 cache_bank_no_conflict(idx) := io.replayFast(i).cache_bank_no_conflict 532 533 // update tlbReqFirstTime 534 uop(idx).debugInfo := io.replayFast(i).debug 535 536 when(io.replayFast(i).needreplay) { 537 creditUpdate(idx) := block_cycles_others(block_ptr_others(idx)) 538 block_ptr_others(idx) := Mux(block_ptr_others(idx) === 3.U(2.W), block_ptr_others(idx), block_ptr_others(idx) + 1.U(2.W)) 539 // try to replay this load in next cycle 540 s1_block_load_mask(idx) := false.B 541 s2_block_load_mask(idx) := false.B 542 543 // replay this load in next cycle 544 loadReplaySelGen(idx(log2Ceil(LoadPipelineWidth) - 1, 0)) := idx 545 loadReplaySelVGen(idx(log2Ceil(LoadPipelineWidth) - 1, 0)) := true.B 546 } 547 } 548 549 when(io.replaySlow(i).valid){ 550 val idx = io.replaySlow(i).ld_idx 551 552 tlb_hited(idx) := io.replaySlow(i).tlb_hited 553 st_ld_check_ok(idx) := io.replaySlow(i).st_ld_check_ok 554 cache_no_replay(idx) := io.replaySlow(i).cache_no_replay 555 forward_data_valid(idx) := io.replaySlow(i).forward_data_valid 556 replayCarryReg(idx) := io.replaySlow(i).replayCarry 557 cache_hited(idx) := io.replaySlow(i).cache_hited 558 559 // update tlbReqFirstTime 560 uop(idx).debugInfo := io.replaySlow(i).debug 561 562 val invalid_sq_idx = io.replaySlow(i).data_invalid_sq_idx 563 564 when(io.replaySlow(i).needreplay) { 565 // update credit and ptr 566 val data_in_last_beat = io.replaySlow(i).data_in_last_beat 567 creditUpdate(idx) := Mux( !io.replaySlow(i).tlb_hited, block_cycles_tlb(block_ptr_tlb(idx)), 568 Mux(!io.replaySlow(i).cache_no_replay || !io.replaySlow(i).st_ld_check_ok, block_cycles_others(block_ptr_others(idx)), 569 Mux(!io.replaySlow(i).cache_hited, block_cycles_cache(block_ptr_cache(idx)) + data_in_last_beat, 0.U))) 570 when(!io.replaySlow(i).tlb_hited) { 571 block_ptr_tlb(idx) := Mux(block_ptr_tlb(idx) === 3.U(2.W), block_ptr_tlb(idx), block_ptr_tlb(idx) + 1.U(2.W)) 572 }.elsewhen(!io.replaySlow(i).cache_no_replay || !io.replaySlow(i).st_ld_check_ok) { 573 block_ptr_others(idx) := Mux(block_ptr_others(idx) === 3.U(2.W), block_ptr_others(idx), block_ptr_others(idx) + 1.U(2.W)) 574 }.elsewhen(!io.replaySlow(i).cache_hited) { 575 block_ptr_cache(idx) := Mux(block_ptr_cache(idx) === 3.U(2.W), block_ptr_cache(idx), block_ptr_cache(idx) + 1.U(2.W)) 576 } 577 } 578 579 // special case: data forward fail 580 block_by_data_forward_fail(idx) := false.B 581 582 when(!io.replaySlow(i).forward_data_valid && io.replaySlow(i).tlb_hited) { 583 when(!io.storeDataValidVec(invalid_sq_idx)) { 584 block_by_data_forward_fail(idx) := true.B 585 block_sq_idx(idx) := invalid_sq_idx 586 } 587 } 588 589 // special case: cache miss 590 val true_cache_miss = io.replaySlow(i).tlb_hited && io.replaySlow(i).cache_no_replay && io.replaySlow(i).st_ld_check_ok && 591 !io.replaySlow(i).cache_hited && !io.replaySlow(i).can_forward_full_data 592 when(true_cache_miss) { 593 miss_mshr_id(idx) := io.replaySlow(i).miss_mshr_id 594 } 595 block_by_cache_miss(idx) := true_cache_miss && // cache miss 596 !(io.refill.valid && io.refill.bits.id === io.replaySlow(i).miss_mshr_id) && // no refill in this cycle 597 creditUpdate(idx) =/= 0.U // credit is not zero 598 } 599 600 } 601 602 when(io.refill.valid) { 603 XSDebug("miss resp: paddr:0x%x data %x\n", io.refill.bits.addr, io.refill.bits.data) 604 } 605 606 // NOTE: we don't refill data from dcache now! 607 608 val s2_dcache_require_replay = WireInit(VecInit((0 until LoadPipelineWidth).map(i =>{ 609 RegNext(io.loadIn(i).fire()) && RegNext(io.s2_dcache_require_replay(i)) 610 }))) 611 dontTouch(s2_dcache_require_replay) 612 613 for (i <- 0 until LoadPipelineWidth) { 614 val loadWbIndex = io.loadIn(i).bits.uop.lqIdx.value 615 val lastCycleLoadWbIndex = RegNext(loadWbIndex) 616 // update miss state in load s3 617 if(!EnableFastForward){ 618 // s2_dcache_require_replay will be used to update lq flag 1 cycle after for better timing 619 // 620 // io.s2_dcache_require_replay comes from dcache miss req reject, which is quite slow to generate 621 when(s2_dcache_require_replay(i)) { 622 // do not writeback if that inst will be resend from rs 623 // rob writeback will not be triggered by a refill before inst replay 624 miss(lastCycleLoadWbIndex) := false.B // disable refill listening 625 datavalid(lastCycleLoadWbIndex) := false.B // disable refill listening 626 assert(!datavalid(lastCycleLoadWbIndex)) 627 } 628 } 629 // update load error state in load s3 630 when(RegNext(io.loadIn(i).fire()) && io.s3_delayed_load_error(i)){ 631 uop(lastCycleLoadWbIndex).cf.exceptionVec(loadAccessFault) := true.B 632 } 633 // update inst replay from fetch flag in s3 634 when(RegNext(io.loadIn(i).fire()) && io.s3_replay_from_fetch(i)){ 635 uop(lastCycleLoadWbIndex).ctrl.replayInst := true.B 636 } 637 } 638 639 /** 640 * Load commits 641 * 642 * When load commited, mark it as !allocated and move deqPtrExt forward. 643 */ 644 (0 until CommitWidth).map(i => { 645 when(commitCount > i.U){ 646 allocated((deqPtrExt+i.U).value) := false.B 647 XSError(!allocated((deqPtrExt+i.U).value), s"why commit invalid entry $i?\n") 648 } 649 }) 650 651 def toVec(a: UInt): Vec[Bool] = { 652 VecInit(a.asBools) 653 } 654 655 def getRemBits(input: UInt)(rem: Int): UInt = { 656 VecInit((0 until LoadQueueSize / LoadPipelineWidth).map(i => { input(LoadPipelineWidth * i + rem) })).asUInt 657 } 658 659 def getFirstOne(mask: Vec[Bool], startMask: UInt) = { 660 val length = mask.length 661 val highBits = (0 until length).map(i => mask(i) & ~startMask(i)) 662 val highBitsUint = Cat(highBits.reverse) 663 PriorityEncoder(Mux(highBitsUint.orR(), highBitsUint, mask.asUInt)) 664 } 665 666 def getOldest[T <: XSBundleWithMicroOp](valid: Seq[Bool], bits: Seq[T]): (Seq[Bool], Seq[T]) = { 667 assert(valid.length == bits.length) 668 assert(isPow2(valid.length)) 669 if (valid.length == 1) { 670 (valid, bits) 671 } else if (valid.length == 2) { 672 val res = Seq.fill(2)(Wire(ValidIO(chiselTypeOf(bits(0))))) 673 for (i <- res.indices) { 674 res(i).valid := valid(i) 675 res(i).bits := bits(i) 676 } 677 val oldest = Mux(valid(0) && valid(1), Mux(isAfter(bits(0).uop.robIdx, bits(1).uop.robIdx), res(1), res(0)), Mux(valid(0) && !valid(1), res(0), res(1))) 678 (Seq(oldest.valid), Seq(oldest.bits)) 679 } else { 680 val left = getOldest(valid.take(valid.length / 2), bits.take(valid.length / 2)) 681 val right = getOldest(valid.takeRight(valid.length / 2), bits.takeRight(valid.length / 2)) 682 getOldest(left._1 ++ right._1, left._2 ++ right._2) 683 } 684 } 685 686 def getAfterMask(valid: Seq[Bool], uop: Seq[MicroOp]) = { 687 assert(valid.length == uop.length) 688 val length = valid.length 689 (0 until length).map(i => { 690 (0 until length).map(j => { 691 Mux(valid(i) && valid(j), 692 isAfter(uop(i).robIdx, uop(j).robIdx), 693 Mux(!valid(i), true.B, false.B)) 694 }) 695 }) 696 } 697 698 699 /** 700 * Store-Load Memory violation detection 701 * 702 * When store writes back, it searches LoadQueue for younger load instructions 703 * with the same load physical address. They loaded wrong data and need re-execution. 704 * 705 * Cycle 0: Store Writeback 706 * Generate match vector for store address with rangeMask(stPtr, enqPtr). 707 * Cycle 1: Redirect Generation 708 * There're up to 2 possible redirect requests. 709 * Choose the oldest load (part 1). 710 * Cycle 2: Redirect Fire 711 * Choose the oldest load (part 2). 712 * Prepare redirect request according to the detected violation. 713 * Fire redirect request (if valid) 714 */ 715 716 // stage 0: lq lq 717 // | | (paddr match) 718 // stage 1: lq lq 719 // | | 720 // | | 721 // | | 722 // stage 2: lq lq 723 // | | 724 // -------------------- 725 // | 726 // rollback req 727 io.load_s1 := DontCare 728def detectRollback(i: Int) = { 729 val startIndex = io.storeIn(i).bits.uop.lqIdx.value 730 val lqIdxMask = UIntToMask(startIndex, LoadQueueSize) 731 val xorMask = lqIdxMask ^ enqMask 732 val sameFlag = io.storeIn(i).bits.uop.lqIdx.flag === enqPtrExt(0).flag 733 val stToEnqPtrMask = Mux(sameFlag, xorMask, ~xorMask) 734 735 // check if load already in lq needs to be rolledback 736 dataModule.io.violation(i).paddr := io.storeIn(i).bits.paddr 737 dataModule.io.violation(i).mask := io.storeIn(i).bits.mask 738 val addrMaskMatch = RegNext(dataModule.io.violation(i).violationMask) 739 val entryNeedCheck = RegNext(VecInit((0 until LoadQueueSize).map(j => { 740 allocated(j) && stToEnqPtrMask(j) && datavalid(j) 741 }))) 742 val lqViolationVec = VecInit((0 until LoadQueueSize).map(j => { 743 addrMaskMatch(j) && entryNeedCheck(j) 744 })) 745 val lqViolation = lqViolationVec.asUInt().orR() && RegNext(!io.storeIn(i).bits.miss) 746 val lqViolationIndex = getFirstOne(lqViolationVec, RegNext(lqIdxMask)) 747 val lqViolationUop = uop(lqViolationIndex) 748 // lqViolationUop.lqIdx.flag := deqMask(lqViolationIndex) ^ deqPtrExt.flag 749 // lqViolationUop.lqIdx.value := lqViolationIndex 750 XSDebug(lqViolation, p"${Binary(Cat(lqViolationVec))}, $startIndex, $lqViolationIndex\n") 751 752 XSDebug( 753 lqViolation, 754 "need rollback (ld wb before store) pc %x robidx %d target %x\n", 755 io.storeIn(i).bits.uop.cf.pc, io.storeIn(i).bits.uop.robIdx.asUInt, lqViolationUop.robIdx.asUInt 756 ) 757 758 (lqViolation, lqViolationUop) 759 } 760 761 def rollbackSel(a: Valid[MicroOpRbExt], b: Valid[MicroOpRbExt]): ValidIO[MicroOpRbExt] = { 762 Mux( 763 a.valid, 764 Mux( 765 b.valid, 766 Mux(isAfter(a.bits.uop.robIdx, b.bits.uop.robIdx), b, a), // a,b both valid, sel oldest 767 a // sel a 768 ), 769 b // sel b 770 ) 771 } 772 773 // S2: select rollback (part1) and generate rollback request 774 // rollback check 775 // Lq rollback seq check is done in s3 (next stage), as getting rollbackLq MicroOp is slow 776 val rollbackLq = Wire(Vec(StorePipelineWidth, Valid(new MicroOpRbExt))) 777 // store ftq index for store set update 778 val stFtqIdxS2 = Wire(Vec(StorePipelineWidth, new FtqPtr)) 779 val stFtqOffsetS2 = Wire(Vec(StorePipelineWidth, UInt(log2Up(PredictWidth).W))) 780 for (i <- 0 until StorePipelineWidth) { 781 val detectedRollback = detectRollback(i) 782 rollbackLq(i).valid := detectedRollback._1 && RegNext(io.storeIn(i).valid) 783 rollbackLq(i).bits.uop := detectedRollback._2 784 rollbackLq(i).bits.flag := i.U 785 stFtqIdxS2(i) := RegNext(io.storeIn(i).bits.uop.cf.ftqPtr) 786 stFtqOffsetS2(i) := RegNext(io.storeIn(i).bits.uop.cf.ftqOffset) 787 } 788 789 val rollbackLqVReg = rollbackLq.map(x => RegNext(x.valid)) 790 val rollbackLqReg = rollbackLq.map(x => RegEnable(x.bits, x.valid)) 791 792 // S3: select rollback (part2), generate rollback request, then fire rollback request 793 // Note that we use robIdx - 1.U to flush the load instruction itself. 794 // Thus, here if last cycle's robIdx equals to this cycle's robIdx, it still triggers the redirect. 795 796 // select uop in parallel 797 val lqs = getOldest(rollbackLqVReg, rollbackLqReg) 798 val rollbackUopExt = lqs._2(0) 799 val stFtqIdxS3 = RegNext(stFtqIdxS2) 800 val stFtqOffsetS3 = RegNext(stFtqOffsetS2) 801 val rollbackUop = rollbackUopExt.uop 802 val rollbackStFtqIdx = stFtqIdxS3(rollbackUopExt.flag) 803 val rollbackStFtqOffset = stFtqOffsetS3(rollbackUopExt.flag) 804 805 // check if rollback request is still valid in parallel 806 io.rollback.bits.robIdx := rollbackUop.robIdx 807 io.rollback.bits.ftqIdx := rollbackUop.cf.ftqPtr 808 io.rollback.bits.stFtqIdx := rollbackStFtqIdx 809 io.rollback.bits.ftqOffset := rollbackUop.cf.ftqOffset 810 io.rollback.bits.stFtqOffset := rollbackStFtqOffset 811 io.rollback.bits.level := RedirectLevel.flush 812 io.rollback.bits.interrupt := DontCare 813 io.rollback.bits.cfiUpdate := DontCare 814 io.rollback.bits.cfiUpdate.target := rollbackUop.cf.pc 815 io.rollback.bits.debug_runahead_checkpoint_id := rollbackUop.debugInfo.runahead_checkpoint_id 816 // io.rollback.bits.pc := DontCare 817 818 io.rollback.valid := rollbackLqVReg.reduce(_|_) && 819 (!lastCycleRedirect.valid || isBefore(rollbackUop.robIdx, lastCycleRedirect.bits.robIdx)) && 820 (!lastlastCycleRedirect.valid || isBefore(rollbackUop.robIdx, lastlastCycleRedirect.bits.robIdx)) 821 822 when(io.rollback.valid) { 823 // XSDebug("Mem rollback: pc %x robidx %d\n", io.rollback.bits.cfi, io.rollback.bits.robIdx.asUInt) 824 } 825 826 /** 827 * Load-Load Memory violation detection 828 * 829 * When load arrives load_s1, it searches LoadQueue for younger load instructions 830 * with the same load physical address. If younger load has been released (or observed), 831 * the younger load needs to be re-execed. 832 * 833 * For now, if re-exec it found to be needed in load_s1, we mark the older load as replayInst, 834 * the two loads will be replayed if the older load becomes the head of rob. 835 * 836 * When dcache releases a line, mark all writebacked entrys in load queue with 837 * the same line paddr as released. 838 */ 839 840 // Load-Load Memory violation query 841 val deqRightMask = UIntToMask.rightmask(deqPtr, LoadQueueSize) 842 (0 until LoadPipelineWidth).map(i => { 843 dataModule.io.release_violation(i).paddr := io.loadViolationQuery(i).req.bits.paddr 844 io.loadViolationQuery(i).req.ready := true.B 845 io.loadViolationQuery(i).resp.valid := RegNext(io.loadViolationQuery(i).req.fire()) 846 // Generate real violation mask 847 // Note that we use UIntToMask.rightmask here 848 val startIndex = io.loadViolationQuery(i).req.bits.uop.lqIdx.value 849 val lqIdxMask = UIntToMask(startIndex, LoadQueueSize) 850 val xorMask = lqIdxMask ^ enqMask 851 val sameFlag = io.loadViolationQuery(i).req.bits.uop.lqIdx.flag === enqPtrExt(0).flag 852 val ldToEnqPtrMask = Mux(sameFlag, xorMask, ~xorMask) 853 val ldld_violation_mask_gen_1 = WireInit(VecInit((0 until LoadQueueSize).map(j => { 854 ldToEnqPtrMask(j) && // the load is younger than current load 855 allocated(j) && // entry is valid 856 released(j) && // cacheline is released 857 (datavalid(j) || miss(j)) // paddr is valid 858 }))) 859 val ldld_violation_mask_gen_2 = WireInit(VecInit((0 until LoadQueueSize).map(j => { 860 dataModule.io.release_violation(i).match_mask(j)// addr match 861 // addr match result is slow to generate, we RegNext() it 862 }))) 863 val ldld_violation_mask = RegNext(ldld_violation_mask_gen_1).asUInt & RegNext(ldld_violation_mask_gen_2).asUInt 864 dontTouch(ldld_violation_mask) 865 ldld_violation_mask.suggestName("ldldViolationMask_" + i) 866 io.loadViolationQuery(i).resp.bits.have_violation := ldld_violation_mask.orR 867 }) 868 869 // "released" flag update 870 // 871 // When io.release.valid (release1cycle.valid), it uses the last ld-ld paddr cam port to 872 // update release flag in 1 cycle 873 874 when(release1cycle.valid){ 875 // Take over ld-ld paddr cam port 876 dataModule.io.release_violation.takeRight(1)(0).paddr := release1cycle.bits.paddr 877 io.loadViolationQuery.takeRight(1)(0).req.ready := false.B 878 } 879 880 when(release2cycle.valid){ 881 // If a load comes in that cycle, we can not judge if it has ld-ld violation 882 // We replay that load inst from RS 883 io.loadViolationQuery.map(i => i.req.ready := 884 // use lsu side release2cycle_dup_lsu paddr for better timing 885 !i.req.bits.paddr(PAddrBits-1, DCacheLineOffset) === release2cycle_dup_lsu.bits.paddr(PAddrBits-1, DCacheLineOffset) 886 ) 887 // io.loadViolationQuery.map(i => i.req.ready := false.B) // For better timing 888 } 889 890 (0 until LoadQueueSize).map(i => { 891 when(RegNext(dataModule.io.release_violation.takeRight(1)(0).match_mask(i) && 892 allocated(i) && 893 datavalid(i) && 894 release1cycle.valid 895 )){ 896 // Note: if a load has missed in dcache and is waiting for refill in load queue, 897 // its released flag still needs to be set as true if addr matches. 898 released(i) := true.B 899 } 900 }) 901 902 /** 903 * Memory mapped IO / other uncached operations 904 * 905 * States: 906 * (1) writeback from store units: mark as pending 907 * (2) when they reach ROB's head, they can be sent to uncache channel 908 * (3) response from uncache channel: mark as datavalid 909 * (4) writeback to ROB (and other units): mark as writebacked 910 * (5) ROB commits the instruction: same as normal instructions 911 */ 912 //(2) when they reach ROB's head, they can be sent to uncache channel 913 val lqTailMmioPending = WireInit(pending(deqPtr)) 914 val lqTailAllocated = WireInit(allocated(deqPtr)) 915 val s_idle :: s_req :: s_resp :: s_wait :: Nil = Enum(4) 916 val uncacheState = RegInit(s_idle) 917 switch(uncacheState) { 918 is(s_idle) { 919 when(RegNext(io.rob.pendingld && lqTailMmioPending && lqTailAllocated)) { 920 uncacheState := s_req 921 } 922 } 923 is(s_req) { 924 when(io.uncache.req.fire()) { 925 uncacheState := s_resp 926 } 927 } 928 is(s_resp) { 929 when(io.uncache.resp.fire()) { 930 uncacheState := s_wait 931 } 932 } 933 is(s_wait) { 934 when(RegNext(io.rob.commit)) { 935 uncacheState := s_idle // ready for next mmio 936 } 937 } 938 } 939 940 // used for uncache commit 941 val uncacheData = RegInit(0.U(XLEN.W)) 942 val uncacheCommitFired = RegInit(false.B) 943 944 when(uncacheState === s_req) { 945 uncacheCommitFired := false.B 946 } 947 948 io.uncache.req.valid := uncacheState === s_req 949 950 dataModule.io.uncache.raddr := deqPtrExtNext.value 951 952 io.uncache.req.bits := DontCare 953 io.uncache.req.bits.cmd := MemoryOpConstants.M_XRD 954 io.uncache.req.bits.addr := dataModule.io.uncache.rdata.paddr 955 io.uncache.req.bits.data := DontCare 956 io.uncache.req.bits.mask := dataModule.io.uncache.rdata.mask 957 io.uncache.req.bits.id := RegNext(deqPtrExtNext.value) 958 io.uncache.req.bits.instrtype := DontCare 959 io.uncache.req.bits.replayCarry := DontCare 960 io.uncache.req.bits.atomic := true.B 961 962 io.uncache.resp.ready := true.B 963 964 when (io.uncache.req.fire()) { 965 pending(deqPtr) := false.B 966 967 XSDebug("uncache req: pc %x addr %x data %x op %x mask %x\n", 968 uop(deqPtr).cf.pc, 969 io.uncache.req.bits.addr, 970 io.uncache.req.bits.data, 971 io.uncache.req.bits.cmd, 972 io.uncache.req.bits.mask 973 ) 974 } 975 976 // (3) response from uncache channel: mark as datavalid 977 when(io.uncache.resp.fire()){ 978 datavalid(deqPtr) := true.B 979 uncacheData := io.uncache.resp.bits.data(XLEN-1, 0) 980 981 XSDebug("uncache resp: data %x\n", io.refill.bits.data) 982 } 983 984 // writeback mmio load, Note: only use ldout(0) to write back 985 // 986 // Int load writeback will finish (if not blocked) in one cycle 987 io.ldout(0).bits.uop := uop(deqPtr) 988 io.ldout(0).bits.uop.lqIdx := deqPtr.asTypeOf(new LqPtr) 989 io.ldout(0).bits.data := DontCare // not used 990 io.ldout(0).bits.redirectValid := false.B 991 io.ldout(0).bits.redirect := DontCare 992 io.ldout(0).bits.debug.isMMIO := true.B 993 io.ldout(0).bits.debug.isPerfCnt := false.B 994 io.ldout(0).bits.debug.paddr := debug_paddr(deqPtr) 995 io.ldout(0).bits.debug.vaddr := vaddrModule.io.rdata(1) 996 io.ldout(0).bits.fflags := DontCare 997 998 io.ldout(0).valid := (uncacheState === s_wait) && !uncacheCommitFired 999 1000 io.ldout(1).bits := DontCare 1001 io.ldout(1).valid := false.B 1002 1003 // merged data, uop and offset for data sel in load_s3 1004 io.ldRawDataOut(0).lqData := uncacheData 1005 io.ldRawDataOut(0).uop := io.ldout(0).bits.uop 1006 io.ldRawDataOut(0).addrOffset := dataModule.io.uncache.rdata.paddr 1007 1008 io.ldRawDataOut(1) := DontCare 1009 1010 when(io.ldout(0).fire()){ 1011 uncacheCommitFired := true.B 1012 } 1013 1014 XSPerfAccumulate("uncache_load_write_back", io.ldout(0).fire()) 1015 1016 // Read vaddr for mem exception 1017 // no inst will be commited 1 cycle before tval update 1018 vaddrModule.io.raddr(0) := (deqPtrExt + commitCount).value 1019 io.exceptionAddr.vaddr := vaddrModule.io.rdata(0) 1020 1021 // read vaddr for mmio, and only port {1} is used 1022 vaddrModule.io.raddr(1) := deqPtr 1023 1024 (0 until LoadPipelineWidth).map(i => { 1025 if(i == 0) { 1026 vaddrTriggerResultModule.io.raddr(i) := deqPtr 1027 io.trigger(i).lqLoadAddrTriggerHitVec := Mux( 1028 io.ldout(i).valid, 1029 vaddrTriggerResultModule.io.rdata(i), 1030 VecInit(Seq.fill(3)(false.B)) 1031 ) 1032 }else { 1033 vaddrTriggerResultModule.io.raddr(i) := DontCare 1034 io.trigger(i).lqLoadAddrTriggerHitVec := VecInit(Seq.fill(3)(false.B)) 1035 } 1036 // vaddrTriggerResultModule.io.raddr(i) := loadWbSelGen(i) 1037 // io.trigger(i).lqLoadAddrTriggerHitVec := Mux( 1038 // loadWbSelV(i), 1039 // vaddrTriggerResultModule.io.rdata(i), 1040 // VecInit(Seq.fill(3)(false.B)) 1041 // ) 1042 }) 1043 1044 // misprediction recovery / exception redirect 1045 // invalidate lq term using robIdx 1046 val needCancel = Wire(Vec(LoadQueueSize, Bool())) 1047 for (i <- 0 until LoadQueueSize) { 1048 needCancel(i) := uop(i).robIdx.needFlush(io.brqRedirect) && allocated(i) 1049 when (needCancel(i)) { 1050 allocated(i) := false.B 1051 } 1052 } 1053 1054 /** 1055 * update pointers 1056 */ 1057 val lastEnqCancel = PopCount(RegNext(VecInit(canEnqueue.zip(enqCancel).map(x => x._1 && x._2)))) 1058 val lastCycleCancelCount = PopCount(RegNext(needCancel)) 1059 val enqNumber = Mux(io.enq.canAccept && io.enq.sqCanAccept, PopCount(io.enq.req.map(_.valid)), 0.U) 1060 when (lastCycleRedirect.valid) { 1061 // we recover the pointers in the next cycle after redirect 1062 enqPtrExt := VecInit(enqPtrExt.map(_ - (lastCycleCancelCount + lastEnqCancel))) 1063 }.otherwise { 1064 enqPtrExt := VecInit(enqPtrExt.map(_ + enqNumber)) 1065 } 1066 1067 deqPtrExtNext := deqPtrExt + commitCount 1068 deqPtrExt := deqPtrExtNext 1069 1070 io.lqCancelCnt := RegNext(lastCycleCancelCount + lastEnqCancel) 1071 1072 /** 1073 * misc 1074 */ 1075 // perf counter 1076 QueuePerf(LoadQueueSize, validCount, !allowEnqueue) 1077 io.lqFull := !allowEnqueue 1078 XSPerfAccumulate("rollback", io.rollback.valid) // rollback redirect generated 1079 XSPerfAccumulate("mmioCycle", uncacheState =/= s_idle) // lq is busy dealing with uncache req 1080 XSPerfAccumulate("mmioCnt", io.uncache.req.fire()) 1081 XSPerfAccumulate("refill", io.refill.valid) 1082 XSPerfAccumulate("writeback_success", PopCount(VecInit(io.ldout.map(i => i.fire())))) 1083 XSPerfAccumulate("writeback_blocked", PopCount(VecInit(io.ldout.map(i => i.valid && !i.ready)))) 1084 XSPerfAccumulate("utilization_miss", PopCount((0 until LoadQueueSize).map(i => allocated(i) && miss(i)))) 1085 1086 if (env.EnableTopDown) { 1087 val stall_loads_bound = WireDefault(0.B) 1088 ExcitingUtils.addSink(stall_loads_bound, "stall_loads_bound", ExcitingUtils.Perf) 1089 val have_miss_entry = (allocated zip miss).map(x => x._1 && x._2).reduce(_ || _) 1090 val l1d_loads_bound = stall_loads_bound && !have_miss_entry 1091 ExcitingUtils.addSource(l1d_loads_bound, "l1d_loads_bound", ExcitingUtils.Perf) 1092 XSPerfAccumulate("l1d_loads_bound", l1d_loads_bound) 1093 val stall_l1d_load_miss = stall_loads_bound && have_miss_entry 1094 ExcitingUtils.addSource(stall_l1d_load_miss, "stall_l1d_load_miss", ExcitingUtils.Perf) 1095 ExcitingUtils.addSink(WireInit(0.U), "stall_l1d_load_miss", ExcitingUtils.Perf) 1096 } 1097 1098 val perfValidCount = RegNext(validCount) 1099 1100 val perfEvents = Seq( 1101 ("rollback ", io.rollback.valid), 1102 ("mmioCycle ", uncacheState =/= s_idle), 1103 ("mmio_Cnt ", io.uncache.req.fire()), 1104 ("refill ", io.refill.valid), 1105 ("writeback_success", PopCount(VecInit(io.ldout.map(i => i.fire())))), 1106 ("writeback_blocked", PopCount(VecInit(io.ldout.map(i => i.valid && !i.ready)))), 1107 ("ltq_1_4_valid ", (perfValidCount < (LoadQueueSize.U/4.U))), 1108 ("ltq_2_4_valid ", (perfValidCount > (LoadQueueSize.U/4.U)) & (perfValidCount <= (LoadQueueSize.U/2.U))), 1109 ("ltq_3_4_valid ", (perfValidCount > (LoadQueueSize.U/2.U)) & (perfValidCount <= (LoadQueueSize.U*3.U/4.U))), 1110 ("ltq_4_4_valid ", (perfValidCount > (LoadQueueSize.U*3.U/4.U))) 1111 ) 1112 generatePerfEvent() 1113 1114 // debug info 1115 XSDebug("enqPtrExt %d:%d deqPtrExt %d:%d\n", enqPtrExt(0).flag, enqPtr, deqPtrExt.flag, deqPtr) 1116 1117 def PrintFlag(flag: Bool, name: String): Unit = { 1118 when(flag) { 1119 XSDebug(false, true.B, name) 1120 }.otherwise { 1121 XSDebug(false, true.B, " ") 1122 } 1123 } 1124 1125 for (i <- 0 until LoadQueueSize) { 1126 XSDebug(i + " pc %x pa %x ", uop(i).cf.pc, debug_paddr(i)) 1127 PrintFlag(allocated(i), "a") 1128 PrintFlag(allocated(i) && datavalid(i), "v") 1129 PrintFlag(allocated(i) && writebacked(i), "w") 1130 PrintFlag(allocated(i) && miss(i), "m") 1131 PrintFlag(allocated(i) && pending(i), "p") 1132 XSDebug(false, true.B, "\n") 1133 } 1134 1135} 1136