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