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