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***************************************************************************************/ 16package xiangshan.mem 17 18import chisel3._ 19import chisel3.util._ 20import chipsalliance.rocketchip.config._ 21import xiangshan._ 22import xiangshan.backend.rob.{RobLsqIO, RobPtr} 23import xiangshan.backend.fu.FuConfig.LduCfg 24import xiangshan.cache._ 25import xiangshan.backend.fu.fpu.FPU 26import xiangshan.cache._ 27import xiangshan.frontend.FtqPtr 28import xiangshan.ExceptionNO._ 29import xiangshan.cache.dcache.ReplayCarry 30import xiangshan.mem.mdp._ 31import utils._ 32import utility._ 33import xiangshan.backend.Bundles.{DynInst, MemExuOutput} 34 35object LoadReplayCauses { 36 // these causes have priority, lower coding has higher priority. 37 // when load replay happens, load unit will select highest priority 38 // from replay causes vector 39 40 /* 41 * Warning: 42 * ************************************************************ 43 * * Don't change the priority. If the priority is changed, * 44 * * deadlock may occur. If you really need to change or * 45 * * add priority, please ensure that no deadlock will occur. * 46 * ************************************************************ 47 * 48 */ 49 // st-ld violation 50 val waitStore = 0 51 // tlb miss check 52 val tlbMiss = 1 53 // st-ld violation re-execute check 54 val schedError = 2 55 // dcache bank conflict check 56 val bankConflict = 3 57 // store-to-load-forwarding check 58 val forwardFail = 4 59 // dcache replay check 60 val dcacheReplay = 5 61 // dcache miss check 62 val dcacheMiss = 6 63 // RAR/RAW queue accept check 64 val rejectEnq = 7 65 // total causes 66 val allCauses = 8 67} 68 69class AgeDetector(numEntries: Int, numEnq: Int, regOut: Boolean = true)(implicit p: Parameters) extends XSModule { 70 val io = IO(new Bundle { 71 // NOTE: deq and enq may come at the same cycle. 72 val enq = Vec(numEnq, Input(UInt(numEntries.W))) 73 val deq = Input(UInt(numEntries.W)) 74 val ready = Input(UInt(numEntries.W)) 75 val out = Output(UInt(numEntries.W)) 76 }) 77 78 // age(i)(j): entry i enters queue before entry j 79 val age = Seq.fill(numEntries)(Seq.fill(numEntries)(RegInit(false.B))) 80 val nextAge = Seq.fill(numEntries)(Seq.fill(numEntries)(Wire(Bool()))) 81 82 // to reduce reg usage, only use upper matrix 83 def get_age(row: Int, col: Int): Bool = if (row <= col) age(row)(col) else !age(col)(row) 84 def get_next_age(row: Int, col: Int): Bool = if (row <= col) nextAge(row)(col) else !nextAge(col)(row) 85 def isFlushed(i: Int): Bool = io.deq(i) 86 def isEnqueued(i: Int, numPorts: Int = -1): Bool = { 87 val takePorts = if (numPorts == -1) io.enq.length else numPorts 88 takePorts match { 89 case 0 => false.B 90 case 1 => io.enq.head(i) && !isFlushed(i) 91 case n => VecInit(io.enq.take(n).map(_(i))).asUInt.orR && !isFlushed(i) 92 } 93 } 94 95 for ((row, i) <- nextAge.zipWithIndex) { 96 val thisValid = get_age(i, i) || isEnqueued(i) 97 for ((elem, j) <- row.zipWithIndex) { 98 when (isFlushed(i)) { 99 // (1) when entry i is flushed or dequeues, set row(i) to false.B 100 elem := false.B 101 }.elsewhen (isFlushed(j)) { 102 // (2) when entry j is flushed or dequeues, set column(j) to validVec 103 elem := thisValid 104 }.elsewhen (isEnqueued(i)) { 105 // (3) when entry i enqueues from port k, 106 // (3.1) if entry j enqueues from previous ports, set to false 107 // (3.2) otherwise, set to true if and only of entry j is invalid 108 // overall: !jEnqFromPreviousPorts && !jIsValid 109 val sel = io.enq.map(_(i)) 110 val result = (0 until numEnq).map(k => isEnqueued(j, k)) 111 // why ParallelMux: sel must be one-hot since enq is one-hot 112 elem := !get_age(j, j) && !ParallelMux(sel, result) 113 }.otherwise { 114 // default: unchanged 115 elem := get_age(i, j) 116 } 117 age(i)(j) := elem 118 } 119 } 120 121 def getOldest(get: (Int, Int) => Bool): UInt = { 122 VecInit((0 until numEntries).map(i => { 123 io.ready(i) & VecInit((0 until numEntries).map(j => if (i != j) !io.ready(j) || get(i, j) else true.B)).asUInt.andR 124 })).asUInt 125 } 126 val best = getOldest(get_age) 127 val nextBest = getOldest(get_next_age) 128 129 io.out := (if (regOut) best else nextBest) 130} 131 132object AgeDetector { 133 def apply(numEntries: Int, enq: Vec[UInt], deq: UInt, ready: UInt)(implicit p: Parameters): Valid[UInt] = { 134 val age = Module(new AgeDetector(numEntries, enq.length, regOut = true)) 135 age.io.enq := enq 136 age.io.deq := deq 137 age.io.ready:= ready 138 val out = Wire(Valid(UInt(deq.getWidth.W))) 139 out.valid := age.io.out.orR 140 out.bits := age.io.out 141 out 142 } 143} 144 145 146class LoadQueueReplay(implicit p: Parameters) extends XSModule 147 with HasDCacheParameters 148 with HasCircularQueuePtrHelper 149 with HasLoadHelper 150 with HasPerfEvents 151{ 152 val io = IO(new Bundle() { 153 val redirect = Flipped(ValidIO(new Redirect)) 154 val enq = Vec(LoadPipelineWidth, Flipped(Decoupled(new LqWriteBundle))) 155 val storeAddrIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) 156 val storeDataIn = Vec(StorePipelineWidth, Flipped(Valid(new MemExuOutput))) 157 val replay = Vec(LoadPipelineWidth, Decoupled(new LsPipelineBundle)) 158 val refill = Flipped(ValidIO(new Refill)) 159 val stAddrReadySqPtr = Input(new SqPtr) 160 val stAddrReadyVec = Input(Vec(StoreQueueSize, Bool())) 161 val stDataReadySqPtr = Input(new SqPtr) 162 val stDataReadyVec = Input(Vec(StoreQueueSize, Bool())) 163 val sqEmpty = Input(Bool()) 164 val lqFull = Output(Bool()) 165 val ldWbPtr = Input(new LqPtr) 166 val tlbReplayDelayCycleCtrl = Vec(4, Input(UInt(ReSelectLen.W))) 167 }) 168 169 println("LoadQueueReplay size: " + LoadQueueReplaySize) 170 // LoadQueueReplay field: 171 // +-----------+---------+-------+-------------+--------+ 172 // | Allocated | MicroOp | VAddr | Cause | Flags | 173 // +-----------+---------+-------+-------------+--------+ 174 // Allocated : entry has been allocated already 175 // MicroOp : inst's microOp 176 // VAddr : virtual address 177 // Cause : replay cause 178 // Flags : rar/raw queue allocate flags 179 val allocated = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) // The control signals need to explicitly indicate the initial value 180 val sleep = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 181 val uop = Reg(Vec(LoadQueueReplaySize, new DynInst)) 182 val vaddrModule = Module(new LqVAddrModule( 183 gen = UInt(VAddrBits.W), 184 numEntries = LoadQueueReplaySize, 185 numRead = LoadPipelineWidth, 186 numWrite = LoadPipelineWidth, 187 numWBank = LoadQueueNWriteBanks, 188 numWDelay = 2, 189 numCamPort = 0)) 190 vaddrModule.io := DontCare 191 val cause = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(LoadReplayCauses.allCauses.W)))) 192 193 // freeliset: store valid entries index. 194 // +---+---+--------------+-----+-----+ 195 // | 0 | 1 | ...... | n-2 | n-1 | 196 // +---+---+--------------+-----+-----+ 197 val freeList = Module(new FreeList( 198 size = LoadQueueReplaySize, 199 allocWidth = LoadPipelineWidth, 200 freeWidth = 4, 201 moduleName = "LoadQueueReplay freelist" 202 )) 203 freeList.io := DontCare 204 /** 205 * used for re-select control 206 */ 207 val credit = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(ReSelectLen.W)))) 208 val selBlocked = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 209 // Ptrs to control which cycle to choose 210 val blockPtrTlb = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(2.W)))) 211 val blockPtrCache = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(2.W)))) 212 val blockPtrOthers = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(2.W)))) 213 // Specific cycles to block 214 val blockCyclesTlb = Reg(Vec(4, UInt(ReSelectLen.W))) 215 blockCyclesTlb := io.tlbReplayDelayCycleCtrl 216 val blockCyclesCache = RegInit(VecInit(Seq(11.U(ReSelectLen.W), 18.U(ReSelectLen.W), 127.U(ReSelectLen.W), 17.U(ReSelectLen.W)))) 217 val blockCyclesOthers = RegInit(VecInit(Seq(0.U(ReSelectLen.W), 0.U(ReSelectLen.W), 0.U(ReSelectLen.W), 0.U(ReSelectLen.W)))) 218 val blockSqIdx = Reg(Vec(LoadQueueReplaySize, new SqPtr)) 219 // block causes 220 val blockByTlbMiss = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 221 val blockByForwardFail = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 222 val blockByWaitStore = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 223 val blockByCacheMiss = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 224 val blockByOthers = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 225 // DCache miss block 226 val missMSHRId = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U((log2Up(cfg.nMissEntries).W))))) 227 val trueCacheMissReplay = WireInit(VecInit(cause.map(_(LoadReplayCauses.dcacheMiss)))) 228 val creditUpdate = WireInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(ReSelectLen.W)))) 229 (0 until LoadQueueReplaySize).map(i => { 230 creditUpdate(i) := Mux(credit(i) > 0.U(ReSelectLen.W), credit(i)-1.U(ReSelectLen.W), credit(i)) 231 selBlocked(i) := creditUpdate(i) =/= 0.U(ReSelectLen.W) || credit(i) =/= 0.U(ReSelectLen.W) 232 }) 233 val replayCarryReg = RegInit(VecInit(List.fill(LoadQueueReplaySize)(ReplayCarry(0.U, false.B)))) 234 235 /** 236 * Enqueue 237 */ 238 val canEnqueue = io.enq.map(_.valid) 239 val cancelEnq = io.enq.map(enq => enq.bits.uop.robIdx.needFlush(io.redirect)) 240 val needReplay = io.enq.map(enq => enq.bits.replayInfo.needReplay()) 241 val hasExceptions = io.enq.map(enq => ExceptionNO.selectByFu(enq.bits.uop.exceptionVec, LduCfg).asUInt.orR && !enq.bits.tlbMiss) 242 val loadReplay = io.enq.map(enq => enq.bits.isLoadReplay) 243 val needEnqueue = VecInit((0 until LoadPipelineWidth).map(w => { 244 canEnqueue(w) && !cancelEnq(w) && needReplay(w) && !hasExceptions(w) 245 })) 246 val canFreeVec = VecInit((0 until LoadPipelineWidth).map(w => { 247 canEnqueue(w) && loadReplay(w) && (!needReplay(w) || hasExceptions(w)) 248 })) 249 250 // select LoadPipelineWidth valid index. 251 val lqFull = freeList.io.empty 252 val lqFreeNums = freeList.io.validCount 253 254 // replay logic 255 // release logic generation 256 val storeAddrInSameCycleVec = Wire(Vec(LoadQueueReplaySize, Bool())) 257 val storeDataInSameCycleVec = Wire(Vec(LoadQueueReplaySize, Bool())) 258 val addrNotBlockVec = Wire(Vec(LoadQueueReplaySize, Bool())) 259 val dataNotBlockVec = Wire(Vec(LoadQueueReplaySize, Bool())) 260 val storeAddrValidVec = addrNotBlockVec.asUInt | storeAddrInSameCycleVec.asUInt 261 val storeDataValidVec = dataNotBlockVec.asUInt | storeDataInSameCycleVec.asUInt 262 263 // store data valid check 264 val stAddrReadyVec = io.stAddrReadyVec 265 val stDataReadyVec = io.stDataReadyVec 266 267 for (i <- 0 until LoadQueueReplaySize) { 268 // dequeue 269 // FIXME: store*Ptr is not accurate 270 dataNotBlockVec(i) := isAfter(io.stAddrReadySqPtr, blockSqIdx(i)) || stDataReadyVec(blockSqIdx(i).value) || io.sqEmpty // for better timing 271 addrNotBlockVec(i) := !isBefore(io.stAddrReadySqPtr, blockSqIdx(i)) || stAddrReadyVec(blockSqIdx(i).value) || io.sqEmpty // for better timing 272 273 // store address execute 274 storeAddrInSameCycleVec(i) := VecInit((0 until StorePipelineWidth).map(w => { 275 io.storeAddrIn(w).valid && 276 !io.storeAddrIn(w).bits.miss && 277 blockSqIdx(i) === io.storeAddrIn(w).bits.uop.sqIdx 278 })).asUInt.orR // for better timing 279 280 // store data execute 281 storeDataInSameCycleVec(i) := VecInit((0 until StorePipelineWidth).map(w => { 282 io.storeDataIn(w).valid && 283 blockSqIdx(i) === io.storeDataIn(w).bits.uop.sqIdx 284 })).asUInt.orR // for better timing 285 286 } 287 288 // store addr issue check 289 val stAddrDeqVec = Wire(Vec(LoadQueueReplaySize, Bool())) 290 (0 until LoadQueueReplaySize).map(i => { 291 stAddrDeqVec(i) := allocated(i) && storeAddrValidVec(i) 292 }) 293 294 // store data issue check 295 val stDataDeqVec = Wire(Vec(LoadQueueReplaySize, Bool())) 296 (0 until LoadQueueReplaySize).map(i => { 297 stDataDeqVec(i) := allocated(i) && storeDataValidVec(i) 298 }) 299 300 // update block condition 301 (0 until LoadQueueReplaySize).map(i => { 302 blockByForwardFail(i) := Mux(blockByForwardFail(i) && stDataDeqVec(i), false.B, blockByForwardFail(i)) 303 blockByWaitStore(i) := Mux(blockByWaitStore(i) && stAddrDeqVec(i), false.B, blockByWaitStore(i)) 304 blockByCacheMiss(i) := Mux(blockByCacheMiss(i) && io.refill.valid && io.refill.bits.id === missMSHRId(i), false.B, blockByCacheMiss(i)) 305 306 when (blockByCacheMiss(i) && io.refill.valid && io.refill.bits.id === missMSHRId(i)) { creditUpdate(i) := 0.U } 307 when (blockByCacheMiss(i) && creditUpdate(i) === 0.U) { blockByCacheMiss(i) := false.B } 308 when (blockByTlbMiss(i) && creditUpdate(i) === 0.U) { blockByTlbMiss(i) := false.B } 309 when (blockByOthers(i) && creditUpdate(i) === 0.U) { blockByOthers(i) := false.B } 310 }) 311 312 // Replay is splitted into 3 stages 313 def getRemBits(input: UInt)(rem: Int): UInt = { 314 VecInit((0 until LoadQueueReplaySize / LoadPipelineWidth).map(i => { input(LoadPipelineWidth * i + rem) })).asUInt 315 } 316 317 // stage1: select 2 entries and read their vaddr 318 val s1_oldestSel = Wire(Vec(LoadPipelineWidth, Valid(UInt(log2Up(LoadQueueReplaySize).W)))) 319 val s2_oldestSel = Wire(Vec(LoadPipelineWidth, Valid(UInt(log2Up(LoadQueueReplaySize).W)))) 320 321 // generate mask 322 val needCancel = Wire(Vec(LoadQueueReplaySize, Bool())) 323 // generate enq mask 324 val selectIndexOH = Wire(Vec(LoadPipelineWidth, UInt(LoadQueueReplaySize.W))) 325 val loadEnqFireMask = io.enq.map(x => x.fire && !x.bits.isLoadReplay).zip(selectIndexOH).map(x => Mux(x._1, x._2, 0.U)) 326 val remLoadEnqFireVec = loadEnqFireMask.map(x => VecInit((0 until LoadPipelineWidth).map(rem => getRemBits(x)(rem)))) 327 val remEnqSelVec = Seq.tabulate(LoadPipelineWidth)(w => VecInit(remLoadEnqFireVec.map(x => x(w)))) 328 329 // generate free mask 330 val loadReplayFreeMask = io.enq.map(_.bits).zip(canFreeVec).map(x => Mux(x._2, UIntToOH(x._1.sleepIndex), 0.U)).reduce(_|_) 331 val loadFreeSelMask = VecInit((0 until LoadQueueReplaySize).map(i => { 332 needCancel(i) || loadReplayFreeMask(i) 333 })).asUInt 334 val remFreeSelVec = VecInit(Seq.tabulate(LoadPipelineWidth)(rem => getRemBits(loadFreeSelMask)(rem))) 335 336 // generate cancel mask 337 val loadReplayFireMask = (0 until LoadPipelineWidth).map(w => Mux(io.replay(w).fire, UIntToOH(s2_oldestSel(w).bits), 0.U)).reduce(_|_) 338 val loadCancelSelMask = VecInit((0 until LoadQueueReplaySize).map(i => { 339 needCancel(i) || loadReplayFireMask(i) 340 })).asUInt 341 val remCancelSelVec = VecInit(Seq.tabulate(LoadPipelineWidth)(rem => getRemBits(loadCancelSelMask)(rem))) 342 343 // generate replay mask 344 val loadReplaySelMask = VecInit((0 until LoadQueueReplaySize).map(i => { 345 val blocked = selBlocked(i) || blockByTlbMiss(i) || blockByForwardFail(i) || blockByCacheMiss(i) || blockByWaitStore(i) || blockByOthers(i) 346 allocated(i) && sleep(i) && !blocked && !loadCancelSelMask(i) 347 })).asUInt // use uint instead vec to reduce verilog lines 348 val oldestPtr = VecInit((0 until CommitWidth).map(x => io.ldWbPtr + x.U)) 349 val oldestSelMask = VecInit((0 until LoadQueueReplaySize).map(i => { 350 loadReplaySelMask(i) && VecInit(oldestPtr.map(_ === uop(i).lqIdx)).asUInt.orR 351 })).asUInt // use uint instead vec to reduce verilog lines 352 val remReplaySelVec = VecInit(Seq.tabulate(LoadPipelineWidth)(rem => getRemBits(loadReplaySelMask)(rem))) 353 val remOldestSelVec = VecInit(Seq.tabulate(LoadPipelineWidth)(rem => getRemBits(oldestSelMask)(rem))) 354 355 // select oldest logic 356 s1_oldestSel := VecInit((0 until LoadPipelineWidth).map(rport => { 357 // select enqueue earlest inst 358 val ageOldest = AgeDetector(LoadQueueReplaySize / LoadPipelineWidth, remEnqSelVec(rport), remFreeSelVec(rport), remReplaySelVec(rport)) 359 assert(!(ageOldest.valid && PopCount(ageOldest.bits) > 1.U), "oldest index must be one-hot!") 360 val ageOldestValid = ageOldest.valid 361 val ageOldestIndex = OHToUInt(ageOldest.bits) 362 363 // select program order oldest 364 val issOldestValid = remOldestSelVec(rport).orR 365 val issOldestIndex = OHToUInt(PriorityEncoderOH(remOldestSelVec(rport))) 366 367 val oldest = Wire(Valid(UInt())) 368 oldest.valid := ageOldest.valid || issOldestValid 369 oldest.bits := Cat(Mux(issOldestValid, issOldestIndex, ageOldestIndex), rport.U(log2Ceil(LoadPipelineWidth).W)) 370 oldest 371 })) 372 373 374 (0 until LoadPipelineWidth).map(w => { 375 vaddrModule.io.raddr(w) := s1_oldestSel(w).bits 376 }) 377 378 // stage2: send replay request to load unit 379 val hasBankConflictVec = RegNext(VecInit(s1_oldestSel.map(x => x.valid && cause(x.bits)(LoadReplayCauses.bankConflict)))) 380 val hasBankConflict = hasBankConflictVec.asUInt.orR 381 val allBankConflict = hasBankConflictVec.asUInt.andR 382 383 // replay cold down 384 val ColdDownCycles = 16 385 386 val coldCounter = RegInit(VecInit(List.fill(LoadPipelineWidth)(0.U(log2Up(ColdDownCycles).W)))) 387 val ColdDownThreshold = Wire(UInt(log2Up(ColdDownCycles).W)) 388 ColdDownThreshold := Constantin.createRecord("ColdDownThreshold_"+p(XSCoreParamsKey).HartId.toString(), initValue = 12.U) 389 assert(ColdDownCycles.U > ColdDownThreshold, "ColdDownCycles must great than ColdDownThreshold!") 390 391 def replayCanFire(i: Int) = coldCounter(i) >= 0.U && coldCounter(i) < ColdDownThreshold 392 def coldDownNow(i: Int) = coldCounter(i) >= ColdDownThreshold 393 394 for (i <- 0 until LoadPipelineWidth) { 395 val s1_replayIdx = s1_oldestSel(i).bits 396 val s2_replayUop = RegNext(uop(s1_replayIdx)) 397 val s2_replayMSHRId = RegNext(missMSHRId(s1_replayIdx)) 398 val s2_replayCauses = RegNext(cause(s1_replayIdx)) 399 val s2_replayCarry = RegNext(replayCarryReg(s1_replayIdx)) 400 val s2_replayCacheMissReplay = RegNext(trueCacheMissReplay(s1_replayIdx)) 401 val cancelReplay = s2_replayUop.robIdx.needFlush(io.redirect) 402 // In order to avoid deadlock, replay one inst which blocked by bank conflict 403 val bankConflictReplay = Mux(hasBankConflict && !allBankConflict, s2_replayCauses(LoadReplayCauses.bankConflict), true.B) 404 405 s2_oldestSel(i).valid := RegNext(s1_oldestSel(i).valid && !loadCancelSelMask(s1_replayIdx)) 406 s2_oldestSel(i).bits := RegNext(s1_oldestSel(i).bits) 407 408 io.replay(i).valid := s2_oldestSel(i).valid && !cancelReplay && bankConflictReplay && replayCanFire(i) 409 io.replay(i).bits := DontCare 410 io.replay(i).bits.uop := s2_replayUop 411 io.replay(i).bits.vaddr := vaddrModule.io.rdata(i) 412 io.replay(i).bits.isFirstIssue := false.B 413 io.replay(i).bits.isLoadReplay := true.B 414 io.replay(i).bits.replayCarry := s2_replayCarry 415 io.replay(i).bits.mshrid := s2_replayMSHRId 416 io.replay(i).bits.forward_tlDchannel := s2_replayCauses(LoadReplayCauses.dcacheMiss) 417 io.replay(i).bits.sleepIndex := s2_oldestSel(i).bits 418 419 when (io.replay(i).fire) { 420 sleep(s2_oldestSel(i).bits) := false.B 421 assert(allocated(s2_oldestSel(i).bits), s"LoadQueueReplay: why replay an invalid entry ${s2_oldestSel(i).bits} ?\n") 422 } 423 } 424 425 // update cold counter 426 val lastReplay = RegNext(VecInit(io.replay.map(_.fire))) 427 for (i <- 0 until LoadPipelineWidth) { 428 when (lastReplay(i) && io.replay(i).fire) { 429 coldCounter(i) := coldCounter(i) + 1.U 430 } .elsewhen (coldDownNow(i)) { 431 coldCounter(i) := coldCounter(i) + 1.U 432 } .otherwise { 433 coldCounter(i) := 0.U 434 } 435 } 436 437 when(io.refill.valid) { 438 XSDebug("miss resp: paddr:0x%x data %x\n", io.refill.bits.addr, io.refill.bits.data) 439 } 440 441 // LoadQueueReplay deallocate 442 val freeMaskVec = Wire(Vec(LoadQueueReplaySize, Bool())) 443 444 // init 445 freeMaskVec.map(e => e := false.B) 446 447 // Allocate logic 448 val enqValidVec = Wire(Vec(LoadPipelineWidth, Bool())) 449 val enqIndexVec = Wire(Vec(LoadPipelineWidth, UInt())) 450 val enqOffset = Wire(Vec(LoadPipelineWidth, UInt())) 451 452 val newEnqueue = (0 until LoadPipelineWidth).map(i => { 453 needEnqueue(i) && !io.enq(i).bits.isLoadReplay 454 }) 455 456 for ((enq, w) <- io.enq.zipWithIndex) { 457 vaddrModule.io.wen(w) := false.B 458 freeList.io.doAllocate(w) := false.B 459 460 enqOffset(w) := PopCount(newEnqueue.take(w)) 461 freeList.io.allocateReq(w) := newEnqueue(w) 462 463 // Allocated ready 464 enqValidVec(w) := freeList.io.canAllocate(enqOffset(w)) 465 enqIndexVec(w) := Mux(enq.bits.isLoadReplay, enq.bits.sleepIndex, freeList.io.allocateSlot(enqOffset(w))) 466 selectIndexOH(w) := UIntToOH(enqIndexVec(w)) 467 enq.ready := Mux(enq.bits.isLoadReplay, true.B, enqValidVec(w)) 468 469 val enqIndex = enqIndexVec(w) 470 when (needEnqueue(w) && enq.ready) { 471 472 val debug_robIdx = enq.bits.uop.robIdx.asUInt 473 XSError(allocated(enqIndex) && !enq.bits.isLoadReplay, p"LoadQueueReplay: can not accept more load, check: ldu $w, robIdx $debug_robIdx!") 474 XSError(hasExceptions(w), p"LoadQueueReplay: The instruction has exception, it can not be replay, check: ldu $w, robIdx $debug_robIdx!") 475 476 freeList.io.doAllocate(w) := !enq.bits.isLoadReplay 477 478 // Allocate new entry 479 allocated(enqIndex) := true.B 480 sleep(enqIndex) := true.B 481 uop(enqIndex) := enq.bits.uop 482 483 vaddrModule.io.wen(w) := true.B 484 vaddrModule.io.waddr(w) := enqIndex 485 vaddrModule.io.wdata(w) := enq.bits.vaddr 486 487 /** 488 * used for feedback and replay 489 */ 490 // set flags 491 val replayInfo = enq.bits.replayInfo 492 val dataInLastBeat = replayInfo.dataInLastBeat 493 cause(enqIndex) := replayInfo.cause.asUInt 494 495 // update credit 496 val blockCyclesTlbPtr = blockPtrTlb(enqIndex) 497 val blockCyclesCachePtr = blockPtrCache(enqIndex) 498 val blockCyclesOtherPtr = blockPtrOthers(enqIndex) 499 creditUpdate(enqIndex) := Mux(replayInfo.cause(LoadReplayCauses.tlbMiss), blockCyclesTlb(blockCyclesTlbPtr), 500 Mux(replayInfo.cause(LoadReplayCauses.dcacheMiss), blockCyclesCache(blockCyclesCachePtr) + dataInLastBeat, blockCyclesOthers(blockCyclesOtherPtr))) 501 502 // init 503 blockByTlbMiss(enqIndex) := false.B 504 blockByWaitStore(enqIndex) := false.B 505 blockByForwardFail(enqIndex) := false.B 506 blockByCacheMiss(enqIndex) := false.B 507 blockByOthers(enqIndex) := false.B 508 509 // update block pointer 510 when (replayInfo.cause(LoadReplayCauses.dcacheReplay) || replayInfo.cause(LoadReplayCauses.rejectEnq)) { 511 // normal case: dcache replay or rar/raw reject 512 blockByOthers(enqIndex) := true.B 513 blockPtrOthers(enqIndex) := Mux(blockPtrOthers(enqIndex) === 3.U(2.W), blockPtrOthers(enqIndex), blockPtrOthers(enqIndex) + 1.U(2.W)) 514 } .elsewhen (replayInfo.cause(LoadReplayCauses.bankConflict) || replayInfo.cause(LoadReplayCauses.schedError)) { 515 // normal case: bank conflict or schedule error 516 // can replay next cycle 517 creditUpdate(enqIndex) := 0.U 518 blockByOthers(enqIndex) := false.B 519 } 520 521 // special case: tlb miss 522 when (replayInfo.cause(LoadReplayCauses.tlbMiss)) { 523 blockByTlbMiss(enqIndex) := true.B 524 blockPtrTlb(enqIndex) := Mux(blockPtrTlb(enqIndex) === 3.U(2.W), blockPtrTlb(enqIndex), blockPtrTlb(enqIndex) + 1.U(2.W)) 525 } 526 527 // special case: dcache miss 528 when (replayInfo.cause(LoadReplayCauses.dcacheMiss)) { 529 blockByCacheMiss(enqIndex) := !replayInfo.canForwardFullData && // dcache miss 530 !(io.refill.valid && io.refill.bits.id === replayInfo.missMSHRId) && // no refill in this cycle 531 creditUpdate(enqIndex) =/= 0.U // credit is not zero 532 blockPtrCache(enqIndex) := Mux(blockPtrCache(enqIndex) === 3.U(2.W), blockPtrCache(enqIndex), blockPtrCache(enqIndex) + 1.U(2.W)) 533 } 534 535 // special case: st-ld violation 536 when (replayInfo.cause(LoadReplayCauses.waitStore)) { 537 blockByWaitStore(enqIndex) := true.B 538 blockSqIdx(enqIndex) := replayInfo.addrInvalidSqIdx 539 blockPtrOthers(enqIndex) := Mux(blockPtrOthers(enqIndex) === 3.U(2.W), blockPtrOthers(enqIndex), blockPtrOthers(enqIndex) + 1.U(2.W)) 540 } 541 542 // special case: data forward fail 543 when (replayInfo.cause(LoadReplayCauses.forwardFail)) { 544 blockByForwardFail(enqIndex) := true.B 545 blockSqIdx(enqIndex) := replayInfo.dataInvalidSqIdx 546 blockPtrOthers(enqIndex) := Mux(blockPtrOthers(enqIndex) === 3.U(2.W), blockPtrOthers(enqIndex), blockPtrOthers(enqIndex) + 1.U(2.W)) 547 } 548 549 // 550 replayCarryReg(enqIndex) := replayInfo.replayCarry 551 missMSHRId(enqIndex) := replayInfo.missMSHRId 552 } 553 554 // 555 val sleepIndex = enq.bits.sleepIndex 556 when (enq.valid && enq.bits.isLoadReplay) { 557 when (!needReplay(w) || hasExceptions(w)) { 558 allocated(sleepIndex) := false.B 559 freeMaskVec(sleepIndex) := true.B 560 } .otherwise { 561 sleep(sleepIndex) := true.B 562 } 563 } 564 } 565 566 // misprediction recovery / exception redirect 567 for (i <- 0 until LoadQueueReplaySize) { 568 needCancel(i) := uop(i).robIdx.needFlush(io.redirect) && allocated(i) 569 when (needCancel(i)) { 570 allocated(i) := false.B 571 freeMaskVec(i) := true.B 572 } 573 } 574 575 freeList.io.free := freeMaskVec.asUInt 576 577 io.lqFull := lqFull 578 579 // perf cnt 580 val enqCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay)) 581 val deqCount = PopCount(io.replay.map(_.fire)) 582 val deqBlockCount = PopCount(io.replay.map(r => r.valid && !r.ready)) 583 val replayTlbMissCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.tlbMiss))) 584 val replayWaitStoreCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.waitStore))) 585 val replaySchedErrorCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.schedError))) 586 val replayRejectEnqCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.rejectEnq))) 587 val replayBankConflictCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.bankConflict))) 588 val replayDCacheReplayCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.dcacheReplay))) 589 val replayForwardFailCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.forwardFail))) 590 val replayDCacheMissCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.dcacheMiss))) 591 XSPerfAccumulate("enq", enqCount) 592 XSPerfAccumulate("deq", deqCount) 593 XSPerfAccumulate("deq_block", deqBlockCount) 594 XSPerfAccumulate("replay_full", io.lqFull) 595 XSPerfAccumulate("replay_reject_enq", replayRejectEnqCount) 596 XSPerfAccumulate("replay_sched_error", replaySchedErrorCount) 597 XSPerfAccumulate("replay_wait_store", replayWaitStoreCount) 598 XSPerfAccumulate("replay_tlb_miss", replayTlbMissCount) 599 XSPerfAccumulate("replay_bank_conflict", replayBankConflictCount) 600 XSPerfAccumulate("replay_dcache_replay", replayDCacheReplayCount) 601 XSPerfAccumulate("replay_forward_fail", replayForwardFailCount) 602 XSPerfAccumulate("replay_dcache_miss", replayDCacheMissCount) 603 604 val perfEvents: Seq[(String, UInt)] = Seq( 605 ("enq", enqCount), 606 ("deq", deqCount), 607 ("deq_block", deqBlockCount), 608 ("replay_full", io.lqFull), 609 ("replay_reject_enq", replayRejectEnqCount), 610 ("replay_advance_sched", replaySchedErrorCount), 611 ("replay_wait_store", replayWaitStoreCount), 612 ("replay_tlb_miss", replayTlbMissCount), 613 ("replay_bank_conflict", replayBankConflictCount), 614 ("replay_dcache_replay", replayDCacheReplayCount), 615 ("replay_forward_fail", replayForwardFailCount), 616 ("replay_dcache_miss", replayDCacheMissCount), 617 ) 618 generatePerfEvent() 619 // end 620} 621