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.{RobPtr, RobLsqIO} 23import xiangshan.cache._ 24import xiangshan.backend.fu.fpu.FPU 25import xiangshan.cache._ 26import xiangshan.frontend.FtqPtr 27import xiangshan.ExceptionNO._ 28import xiangshan.cache.dcache.ReplayCarry 29import xiangshan.mem.mdp._ 30import utils._ 31import utility._ 32 33object LoadReplayCauses { 34 // these causes have priority, lower coding has higher priority. 35 // when load replay happens, load unit will select highest priority 36 // from replay causes vector 37 38 /* 39 * Warning: 40 * ************************************************************ 41 * * Don't change the priority. If the priority is changed, * 42 * * deadlock may occur. If you really need to change or * 43 * * add priority, please ensure that no deadlock will occur. * 44 * ************************************************************ 45 * 46 */ 47 // st-ld violation 48 val waitStore = 0 49 // tlb miss check 50 val tlbMiss = 1 51 // st-ld violation re-execute check 52 val schedError = 2 53 // dcache bank conflict check 54 val bankConflict = 3 55 // store-to-load-forwarding check 56 val forwardFail = 4 57 // dcache replay check 58 val dcacheReplay = 5 59 // dcache miss check 60 val dcacheMiss = 6 61 // RAR queue accept check 62 val rarReject = 7 63 // RAW queue accept check 64 val rawReject = 8 65 // total causes 66 val allCauses = 9 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 ExuOutput))) 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 val rarFull = Input(Bool()) 168 val rawFull = Input(Bool()) 169 val l2Hint = Input(Valid(new L2ToL1Hint())) 170 }) 171 172 println("LoadQueueReplay size: " + LoadQueueReplaySize) 173 // LoadQueueReplay field: 174 // +-----------+---------+-------+-------------+--------+ 175 // | Allocated | MicroOp | VAddr | Cause | Flags | 176 // +-----------+---------+-------+-------------+--------+ 177 // Allocated : entry has been allocated already 178 // MicroOp : inst's microOp 179 // VAddr : virtual address 180 // Cause : replay cause 181 // Flags : rar/raw queue allocate flags 182 val allocated = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) // The control signals need to explicitly indicate the initial value 183 val sleep = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 184 val uop = Reg(Vec(LoadQueueReplaySize, new MicroOp)) 185 val vaddrModule = Module(new LqVAddrModule( 186 gen = UInt(VAddrBits.W), 187 numEntries = LoadQueueReplaySize, 188 numRead = LoadPipelineWidth, 189 numWrite = LoadPipelineWidth, 190 numWBank = LoadQueueNWriteBanks, 191 numWDelay = 2, 192 numCamPort = 0)) 193 vaddrModule.io := DontCare 194 val cause = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(LoadReplayCauses.allCauses.W)))) 195 196 // freeliset: store valid entries index. 197 // +---+---+--------------+-----+-----+ 198 // | 0 | 1 | ...... | n-2 | n-1 | 199 // +---+---+--------------+-----+-----+ 200 val freeList = Module(new FreeList( 201 size = LoadQueueReplaySize, 202 allocWidth = LoadPipelineWidth, 203 freeWidth = 4, 204 moduleName = "LoadQueueReplay freelist" 205 )) 206 freeList.io := DontCare 207 /** 208 * used for re-select control 209 */ 210 val credit = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(ReSelectLen.W)))) 211 val selBlocked = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 212 // Ptrs to control which cycle to choose 213 val blockPtrTlb = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(2.W)))) 214 val blockPtrCache = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(2.W)))) 215 val blockPtrOthers = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(2.W)))) 216 // Specific cycles to block 217 val blockCyclesTlb = Reg(Vec(4, UInt(ReSelectLen.W))) 218 blockCyclesTlb := io.tlbReplayDelayCycleCtrl 219 val blockCyclesCache = RegInit(VecInit(Seq(0.U(ReSelectLen.W), 0.U(ReSelectLen.W), 0.U(ReSelectLen.W), 0.U(ReSelectLen.W)))) 220 val blockCyclesOthers = RegInit(VecInit(Seq(0.U(ReSelectLen.W), 0.U(ReSelectLen.W), 0.U(ReSelectLen.W), 0.U(ReSelectLen.W)))) 221 val blockSqIdx = Reg(Vec(LoadQueueReplaySize, new SqPtr)) 222 // block causes 223 val blockByTlbMiss = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 224 val blockByForwardFail = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 225 val blockByWaitStore = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 226 val blockByCacheMiss = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 227 val blockByRARReject = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 228 val blockByRAWReject = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 229 val blockByOthers = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 230 // DCache miss block 231 val missMSHRId = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U((log2Up(cfg.nMissEntries).W))))) 232 // Has this load already updated dcache replacement? 233 val replacementUpdated = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 234 val trueCacheMissReplay = WireInit(VecInit(cause.map(_(LoadReplayCauses.dcacheMiss)))) 235 val creditUpdate = WireInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(ReSelectLen.W)))) 236 (0 until LoadQueueReplaySize).map(i => { 237 creditUpdate(i) := Mux(credit(i) > 0.U(ReSelectLen.W), credit(i)-1.U(ReSelectLen.W), credit(i)) 238 selBlocked(i) := creditUpdate(i) =/= 0.U(ReSelectLen.W) || credit(i) =/= 0.U(ReSelectLen.W) 239 }) 240 val replayCarryReg = RegInit(VecInit(List.fill(LoadQueueReplaySize)(ReplayCarry(0.U, false.B)))) 241 val dataInLastBeatReg = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 242 243 /** 244 * Enqueue 245 */ 246 val canEnqueue = io.enq.map(_.valid) 247 val cancelEnq = io.enq.map(enq => enq.bits.uop.robIdx.needFlush(io.redirect)) 248 val needReplay = io.enq.map(enq => enq.bits.replayInfo.needReplay()) 249 val hasExceptions = io.enq.map(enq => ExceptionNO.selectByFu(enq.bits.uop.cf.exceptionVec, lduCfg).asUInt.orR && !enq.bits.tlbMiss) 250 val loadReplay = io.enq.map(enq => enq.bits.isLoadReplay) 251 val needEnqueue = VecInit((0 until LoadPipelineWidth).map(w => { 252 canEnqueue(w) && !cancelEnq(w) && needReplay(w) && !hasExceptions(w) 253 })) 254 val canFreeVec = VecInit((0 until LoadPipelineWidth).map(w => { 255 canEnqueue(w) && loadReplay(w) && (!needReplay(w) || hasExceptions(w)) 256 })) 257 258 // select LoadPipelineWidth valid index. 259 val lqFull = freeList.io.empty 260 val lqFreeNums = freeList.io.validCount 261 262 // replay logic 263 // release logic generation 264 val storeAddrInSameCycleVec = Wire(Vec(LoadQueueReplaySize, Bool())) 265 val storeDataInSameCycleVec = Wire(Vec(LoadQueueReplaySize, Bool())) 266 val addrNotBlockVec = Wire(Vec(LoadQueueReplaySize, Bool())) 267 val dataNotBlockVec = Wire(Vec(LoadQueueReplaySize, Bool())) 268 val storeAddrValidVec = addrNotBlockVec.asUInt | storeAddrInSameCycleVec.asUInt 269 val storeDataValidVec = dataNotBlockVec.asUInt | storeDataInSameCycleVec.asUInt 270 271 // store data valid check 272 val stAddrReadyVec = io.stAddrReadyVec 273 val stDataReadyVec = io.stDataReadyVec 274 275 for (i <- 0 until LoadQueueReplaySize) { 276 // dequeue 277 // FIXME: store*Ptr is not accurate 278 dataNotBlockVec(i) := !isBefore(io.stDataReadySqPtr, blockSqIdx(i)) || stDataReadyVec(blockSqIdx(i).value) || io.sqEmpty // for better timing 279 addrNotBlockVec(i) := !isBefore(io.stAddrReadySqPtr, blockSqIdx(i)) || stAddrReadyVec(blockSqIdx(i).value) || io.sqEmpty // for better timing 280 281 // store address execute 282 storeAddrInSameCycleVec(i) := VecInit((0 until StorePipelineWidth).map(w => { 283 io.storeAddrIn(w).valid && 284 !io.storeAddrIn(w).bits.miss && 285 blockSqIdx(i) === io.storeAddrIn(w).bits.uop.sqIdx 286 })).asUInt.orR // for better timing 287 288 // store data execute 289 storeDataInSameCycleVec(i) := VecInit((0 until StorePipelineWidth).map(w => { 290 io.storeDataIn(w).valid && 291 blockSqIdx(i) === io.storeDataIn(w).bits.uop.sqIdx 292 })).asUInt.orR // for better timing 293 294 } 295 296 // store addr issue check 297 val stAddrDeqVec = Wire(Vec(LoadQueueReplaySize, Bool())) 298 (0 until LoadQueueReplaySize).map(i => { 299 stAddrDeqVec(i) := allocated(i) && storeAddrValidVec(i) 300 }) 301 302 // store data issue check 303 val stDataDeqVec = Wire(Vec(LoadQueueReplaySize, Bool())) 304 (0 until LoadQueueReplaySize).map(i => { 305 stDataDeqVec(i) := allocated(i) && storeDataValidVec(i) 306 }) 307 308 // update block condition 309 (0 until LoadQueueReplaySize).map(i => { 310 blockByForwardFail(i) := Mux(blockByForwardFail(i) && stDataDeqVec(i), false.B, blockByForwardFail(i)) 311 blockByWaitStore(i) := Mux(blockByWaitStore(i) && stAddrDeqVec(i), false.B, blockByWaitStore(i)) 312 blockByCacheMiss(i) := Mux(blockByCacheMiss(i) && io.refill.valid && io.refill.bits.id === missMSHRId(i), false.B, blockByCacheMiss(i)) 313 314 when (blockByCacheMiss(i) && io.refill.valid && io.refill.bits.id === missMSHRId(i)) { creditUpdate(i) := 0.U } 315 when (blockByRARReject(i) && (!io.rarFull || !isAfter(uop(i).lqIdx, io.ldWbPtr))) { blockByRARReject(i) := false.B } 316 when (blockByRAWReject(i) && (!io.rawFull || !isAfter(uop(i).sqIdx, io.stAddrReadySqPtr))) { blockByRAWReject(i) := false.B } 317 when (blockByTlbMiss(i) && creditUpdate(i) === 0.U) { blockByTlbMiss(i) := false.B } 318 when (blockByOthers(i) && creditUpdate(i) === 0.U) { blockByOthers(i) := false.B } 319 }) 320 321 // Replay is splitted into 3 stages 322 def getRemBits(input: UInt)(rem: Int): UInt = { 323 VecInit((0 until LoadQueueReplaySize / LoadPipelineWidth).map(i => { input(LoadPipelineWidth * i + rem) })).asUInt 324 } 325 326 def getRemSeq(input: Seq[Seq[Bool]])(rem: Int) = { 327 (0 until LoadQueueReplaySize / LoadPipelineWidth).map(i => { input(LoadPipelineWidth * i + rem) }) 328 } 329 330 // stage1: select 2 entries and read their vaddr 331 val s1_oldestSel = Wire(Vec(LoadPipelineWidth, Valid(UInt(log2Up(LoadQueueReplaySize).W)))) 332 val s2_oldestSel = Wire(Vec(LoadPipelineWidth, Valid(UInt(log2Up(LoadQueueReplaySize).W)))) 333 334 // generate mask 335 val needCancel = Wire(Vec(LoadQueueReplaySize, Bool())) 336 // generate enq mask 337 val selectIndexOH = Wire(Vec(LoadPipelineWidth, UInt(LoadQueueReplaySize.W))) 338 val loadEnqFireMask = io.enq.map(x => x.fire && !x.bits.isLoadReplay).zip(selectIndexOH).map(x => Mux(x._1, x._2, 0.U)) 339 val remLoadEnqFireVec = loadEnqFireMask.map(x => VecInit((0 until LoadPipelineWidth).map(rem => getRemBits(x)(rem)))) 340 val remEnqSelVec = Seq.tabulate(LoadPipelineWidth)(w => VecInit(remLoadEnqFireVec.map(x => x(w)))) 341 342 // generate free mask 343 val loadReplayFreeMask = io.enq.map(_.bits).zip(canFreeVec).map(x => Mux(x._2, UIntToOH(x._1.sleepIndex), 0.U)).reduce(_|_) 344 val loadFreeSelMask = VecInit((0 until LoadQueueReplaySize).map(i => { 345 needCancel(i) || loadReplayFreeMask(i) 346 })).asUInt 347 val remFreeSelVec = VecInit(Seq.tabulate(LoadPipelineWidth)(rem => getRemBits(loadFreeSelMask)(rem))) 348 349 // generate cancel mask 350 val loadReplayFireMask = (0 until LoadPipelineWidth).map(w => Mux(io.replay(w).fire, UIntToOH(s2_oldestSel(w).bits), 0.U)).reduce(_|_) 351 val loadCancelSelMask = VecInit((0 until LoadQueueReplaySize).map(i => { 352 needCancel(i) || loadReplayFireMask(i) 353 })).asUInt 354 val remCancelSelVec = VecInit(Seq.tabulate(LoadPipelineWidth)(rem => getRemBits(loadCancelSelMask)(rem))) 355 356 // l2 hint wakes up cache missed load 357 // l2 will send GrantData in next 2/3 cycle, wake up the missed load early and sent them to load pipe, so them will hit the data in D channel or mshr in load S1 358 val loadHintWakeMask = VecInit((0 until LoadQueueReplaySize).map(i => { 359 allocated(i) && sleep(i) && blockByCacheMiss(i) && missMSHRId(i) === io.l2Hint.bits.sourceId && io.l2Hint.valid 360 })).asUInt() 361 // l2 will send 2 beats data in 2 cycles, so if data needed by this load is in first beat, select it this cycle, otherwise next cycle 362 val loadHintSelMask = loadHintWakeMask & VecInit(dataInLastBeatReg.map(!_)).asUInt 363 val remLoadHintSelMask = VecInit((0 until LoadPipelineWidth).map(rem => getRemBits(loadHintSelMask)(rem))) 364 val hintSelValid = loadHintSelMask.orR 365 366 // wake up cache missed load 367 (0 until LoadQueueReplaySize).foreach(i => { 368 when(loadHintWakeMask(i)) { 369 blockByCacheMiss(i) := false.B 370 creditUpdate(i) := 0.U 371 } 372 }) 373 374 // generate replay mask 375 // replay select priority is given as follow 376 // 1. hint wake up load 377 // 2. higher priority load 378 // 3. lower priority load 379 val loadHigherPriorityReplaySelMask = VecInit((0 until LoadQueueReplaySize).map(i => { 380 val blocked = selBlocked(i) || blockByWaitStore(i) || blockByRARReject(i) || blockByRAWReject(i) || blockByOthers(i) || blockByForwardFail(i) || blockByCacheMiss(i) || blockByTlbMiss(i) 381 val hasHigherPriority = cause(i)(LoadReplayCauses.dcacheMiss) || cause(i)(LoadReplayCauses.forwardFail) 382 allocated(i) && sleep(i) && !blocked && !loadCancelSelMask(i) && hasHigherPriority 383 })).asUInt // use uint instead vec to reduce verilog lines 384 val loadLowerPriorityReplaySelMask = VecInit((0 until LoadQueueReplaySize).map(i => { 385 val blocked = selBlocked(i) || blockByWaitStore(i) || blockByRARReject(i) || blockByRAWReject(i) || blockByOthers(i) || blockByForwardFail(i) || blockByCacheMiss(i) || blockByTlbMiss(i) 386 val hasLowerPriority = !cause(i)(LoadReplayCauses.dcacheMiss) && !cause(i)(LoadReplayCauses.forwardFail) 387 allocated(i) && sleep(i) && !blocked && !loadCancelSelMask(i) && hasLowerPriority 388 })).asUInt // use uint instead vec to reduce verilog lines 389 val loadNormalReplaySelMask = loadLowerPriorityReplaySelMask | loadHigherPriorityReplaySelMask | loadHintSelMask 390 val remNormalReplaySelVec = VecInit((0 until LoadPipelineWidth).map(rem => getRemBits(loadNormalReplaySelMask)(rem))) 391 val loadPriorityReplaySelMask = Mux(hintSelValid, loadHintSelMask, Mux(loadHigherPriorityReplaySelMask.orR, loadHigherPriorityReplaySelMask, loadLowerPriorityReplaySelMask)) 392 val remPriorityReplaySelVec = VecInit((0 until LoadPipelineWidth).map(rem => getRemBits(loadPriorityReplaySelMask)(rem))) 393 394 /****************************************************************************************** 395 * WARNING: Make sure that OldestSelectStride must less than or equal stages of load unit.* 396 ****************************************************************************************** 397 */ 398 val OldestSelectStride = 4 399 val oldestPtrExt = (0 until OldestSelectStride).map(i => io.ldWbPtr + i.U) 400 val oldestMatchMaskVec = (0 until LoadQueueReplaySize).map(i => (0 until OldestSelectStride).map(j => loadNormalReplaySelMask(i) && uop(i).lqIdx === oldestPtrExt(j))) 401 val remOldsetMatchMaskVec = (0 until LoadPipelineWidth).map(rem => getRemSeq(oldestMatchMaskVec.map(_.take(1)))(rem)) 402 val remOlderMatchMaskVec = (0 until LoadPipelineWidth).map(rem => getRemSeq(oldestMatchMaskVec.map(_.drop(1)))(rem)) 403 val remOldestSelVec = VecInit(Seq.tabulate(LoadPipelineWidth)(rem => { 404 VecInit((0 until LoadQueueReplaySize / LoadPipelineWidth).map(i => { 405 Mux(VecInit(remOldsetMatchMaskVec(rem).map(_(0))).asUInt.orR, remOldsetMatchMaskVec(rem)(i)(0), remOlderMatchMaskVec(rem)(i).reduce(_|_)) 406 })).asUInt 407 })) 408 val remOldestHintSelVec = remOldestSelVec.zip(remLoadHintSelMask).map { 409 case(oldestVec, hintVec) => oldestVec & hintVec 410 } 411 412 // select oldest logic 413 s1_oldestSel := VecInit((0 until LoadPipelineWidth).map(rport => { 414 // select enqueue earlest inst 415 val ageOldest = AgeDetector(LoadQueueReplaySize / LoadPipelineWidth, remEnqSelVec(rport), remFreeSelVec(rport), remPriorityReplaySelVec(rport)) 416 assert(!(ageOldest.valid && PopCount(ageOldest.bits) > 1.U), "oldest index must be one-hot!") 417 val ageOldestValid = ageOldest.valid 418 val ageOldestIndex = OHToUInt(ageOldest.bits) 419 420 // select program order oldest 421 val issOldestValid = Mux(io.l2Hint.valid, remOldestHintSelVec(rport).orR, remOldestSelVec(rport).orR) 422 val issOldestIndex = Mux(io.l2Hint.valid, OHToUInt(PriorityEncoderOH(remOldestHintSelVec(rport))), OHToUInt(PriorityEncoderOH(remOldestSelVec(rport)))) 423 424 val oldest = Wire(Valid(UInt())) 425 oldest.valid := ageOldest.valid || issOldestValid 426 oldest.bits := Cat(Mux(issOldestValid, issOldestIndex, ageOldestIndex), rport.U(log2Ceil(LoadPipelineWidth).W)) 427 oldest 428 })) 429 430 431 // Replay port reorder 432 class BalanceEntry extends XSBundle { 433 val balance = Bool() 434 val index = UInt(log2Up(LoadQueueReplaySize).W) 435 val port = UInt(log2Up(LoadPipelineWidth).W) 436 } 437 438 def balanceReOrder(sel: Seq[ValidIO[BalanceEntry]]): Seq[ValidIO[BalanceEntry]] = { 439 require(sel.length > 0) 440 val balancePick = ParallelPriorityMux(sel.map(x => (x.valid && x.bits.balance) -> x)) 441 val reorderSel = Wire(Vec(sel.length, ValidIO(new BalanceEntry))) 442 (0 until sel.length).map(i => 443 if (i == 0) { 444 when (balancePick.valid && balancePick.bits.balance) { 445 reorderSel(i) := balancePick 446 } .otherwise { 447 reorderSel(i) := sel(i) 448 } 449 } else { 450 when (balancePick.valid && balancePick.bits.balance && i.U === balancePick.bits.port) { 451 reorderSel(i) := sel(0) 452 } .otherwise { 453 reorderSel(i) := sel(i) 454 } 455 } 456 ) 457 reorderSel 458 } 459 460 // stage2: send replay request to load unit 461 // replay cold down 462 val ColdDownCycles = 16 463 val coldCounter = RegInit(VecInit(List.fill(LoadPipelineWidth)(0.U(log2Up(ColdDownCycles).W)))) 464 val ColdDownThreshold = Wire(UInt(log2Up(ColdDownCycles).W)) 465 ColdDownThreshold := Constantin.createRecord("ColdDownThreshold_"+p(XSCoreParamsKey).HartId.toString(), initValue = 12.U) 466 assert(ColdDownCycles.U > ColdDownThreshold, "ColdDownCycles must great than ColdDownThreshold!") 467 468 def replayCanFire(i: Int) = coldCounter(i) >= 0.U && coldCounter(i) < ColdDownThreshold 469 def coldDownNow(i: Int) = coldCounter(i) >= ColdDownThreshold 470 471 val s1_balanceOldestSelExt = (0 until LoadPipelineWidth).map(i => { 472 val wrapper = Wire(Valid(new BalanceEntry)) 473 wrapper.valid := s1_oldestSel(i).valid 474 wrapper.bits.balance := cause(s1_oldestSel(i).bits)(LoadReplayCauses.bankConflict) 475 wrapper.bits.index := s1_oldestSel(i).bits 476 wrapper.bits.port := i.U 477 wrapper 478 }) 479 val s1_balanceOldestSel = balanceReOrder(s1_balanceOldestSelExt) 480 (0 until LoadPipelineWidth).map(w => { 481 vaddrModule.io.raddr(w) := s1_balanceOldestSel(w).bits.index 482 }) 483 484 for (i <- 0 until LoadPipelineWidth) { 485 val s2_replayIdx = RegNext(s1_balanceOldestSel(i).bits.index) 486 val s2_replayUop = uop(s2_replayIdx) 487 val s2_replayMSHRId = missMSHRId(s2_replayIdx) 488 val s2_replacementUpdated = replacementUpdated(s2_replayIdx) 489 val s2_replayCauses = cause(s2_replayIdx) 490 val s2_replayCarry = replayCarryReg(s2_replayIdx) 491 val s2_replayCacheMissReplay = trueCacheMissReplay(s2_replayIdx) 492 val cancelReplay = s2_replayUop.robIdx.needFlush(io.redirect) 493 494 val s2_loadCancelSelMask = RegNext(loadCancelSelMask) 495 s2_oldestSel(i).valid := RegNext(s1_balanceOldestSel(i).valid) && !s2_loadCancelSelMask(s2_replayIdx) 496 s2_oldestSel(i).bits := s2_replayIdx 497 498 io.replay(i).valid := s2_oldestSel(i).valid && !cancelReplay && replayCanFire(i) 499 io.replay(i).bits := DontCare 500 io.replay(i).bits.uop := s2_replayUop 501 io.replay(i).bits.vaddr := vaddrModule.io.rdata(i) 502 io.replay(i).bits.isFirstIssue := false.B 503 io.replay(i).bits.isLoadReplay := true.B 504 io.replay(i).bits.replayCarry := s2_replayCarry 505 io.replay(i).bits.mshrid := s2_replayMSHRId 506 io.replay(i).bits.replacementUpdated := s2_replacementUpdated 507 io.replay(i).bits.forward_tlDchannel := s2_replayCauses(LoadReplayCauses.dcacheMiss) 508 io.replay(i).bits.sleepIndex := s2_oldestSel(i).bits 509 510 when (io.replay(i).fire) { 511 sleep(s2_oldestSel(i).bits) := false.B 512 assert(allocated(s2_oldestSel(i).bits), s"LoadQueueReplay: why replay an invalid entry ${s2_oldestSel(i).bits} ?\n") 513 } 514 } 515 516 // update cold counter 517 val lastReplay = RegNext(VecInit(io.replay.map(_.fire))) 518 for (i <- 0 until LoadPipelineWidth) { 519 when (lastReplay(i) && io.replay(i).fire) { 520 coldCounter(i) := coldCounter(i) + 1.U 521 } .elsewhen (coldDownNow(i)) { 522 coldCounter(i) := coldCounter(i) + 1.U 523 } .otherwise { 524 coldCounter(i) := 0.U 525 } 526 } 527 528 when(io.refill.valid) { 529 XSDebug("miss resp: paddr:0x%x data %x\n", io.refill.bits.addr, io.refill.bits.data) 530 } 531 532 // LoadQueueReplay deallocate 533 val freeMaskVec = Wire(Vec(LoadQueueReplaySize, Bool())) 534 535 // init 536 freeMaskVec.map(e => e := false.B) 537 538 // Allocate logic 539 val enqValidVec = Wire(Vec(LoadPipelineWidth, Bool())) 540 val enqIndexVec = Wire(Vec(LoadPipelineWidth, UInt())) 541 val enqOffset = Wire(Vec(LoadPipelineWidth, UInt())) 542 543 val newEnqueue = (0 until LoadPipelineWidth).map(i => { 544 needEnqueue(i) && !io.enq(i).bits.isLoadReplay 545 }) 546 547 for ((enq, w) <- io.enq.zipWithIndex) { 548 vaddrModule.io.wen(w) := false.B 549 freeList.io.doAllocate(w) := false.B 550 551 enqOffset(w) := PopCount(newEnqueue.take(w)) 552 freeList.io.allocateReq(w) := newEnqueue(w) 553 554 // Allocated ready 555 enqValidVec(w) := freeList.io.canAllocate(enqOffset(w)) 556 enqIndexVec(w) := Mux(enq.bits.isLoadReplay, enq.bits.sleepIndex, freeList.io.allocateSlot(enqOffset(w))) 557 selectIndexOH(w) := UIntToOH(enqIndexVec(w)) 558 enq.ready := Mux(enq.bits.isLoadReplay, true.B, enqValidVec(w)) 559 560 val enqIndex = enqIndexVec(w) 561 when (needEnqueue(w) && enq.ready) { 562 563 val debug_robIdx = enq.bits.uop.robIdx.asUInt 564 XSError(allocated(enqIndex) && !enq.bits.isLoadReplay, p"LoadQueueReplay: can not accept more load, check: ldu $w, robIdx $debug_robIdx!") 565 XSError(hasExceptions(w), p"LoadQueueReplay: The instruction has exception, it can not be replay, check: ldu $w, robIdx $debug_robIdx!") 566 567 freeList.io.doAllocate(w) := !enq.bits.isLoadReplay 568 569 // Allocate new entry 570 allocated(enqIndex) := true.B 571 sleep(enqIndex) := true.B 572 uop(enqIndex) := enq.bits.uop 573 574 vaddrModule.io.wen(w) := true.B 575 vaddrModule.io.waddr(w) := enqIndex 576 vaddrModule.io.wdata(w) := enq.bits.vaddr 577 578 /** 579 * used for feedback and replay 580 */ 581 // set flags 582 val replayInfo = enq.bits.replayInfo 583 val dataInLastBeat = replayInfo.dataInLastBeat 584 cause(enqIndex) := replayInfo.cause.asUInt 585 586 // update credit 587 val blockCyclesTlbPtr = blockPtrTlb(enqIndex) 588 val blockCyclesCachePtr = blockPtrCache(enqIndex) 589 val blockCyclesOtherPtr = blockPtrOthers(enqIndex) 590 creditUpdate(enqIndex) := Mux(replayInfo.cause(LoadReplayCauses.tlbMiss), blockCyclesTlb(blockCyclesTlbPtr), 591 Mux(replayInfo.cause(LoadReplayCauses.dcacheMiss), blockCyclesCache(blockCyclesCachePtr) + dataInLastBeat, blockCyclesOthers(blockCyclesOtherPtr))) 592 593 // init 594 blockByTlbMiss(enqIndex) := false.B 595 blockByWaitStore(enqIndex) := false.B 596 blockByForwardFail(enqIndex) := false.B 597 blockByCacheMiss(enqIndex) := false.B 598 blockByRARReject(enqIndex) := false.B 599 blockByRAWReject(enqIndex) := false.B 600 blockByOthers(enqIndex) := false.B 601 602 // update block pointer 603 when (replayInfo.cause(LoadReplayCauses.dcacheReplay)) { 604 // normal case: dcache replay 605 blockByOthers(enqIndex) := true.B 606 blockPtrOthers(enqIndex) := Mux(blockPtrOthers(enqIndex) === 3.U(2.W), blockPtrOthers(enqIndex), blockPtrOthers(enqIndex) + 1.U(2.W)) 607 } .elsewhen (replayInfo.cause(LoadReplayCauses.bankConflict) || replayInfo.cause(LoadReplayCauses.schedError)) { 608 // normal case: bank conflict or schedule error 609 // can replay next cycle 610 creditUpdate(enqIndex) := 0.U 611 blockByOthers(enqIndex) := false.B 612 } 613 614 // special case: tlb miss 615 when (replayInfo.cause(LoadReplayCauses.tlbMiss)) { 616 blockByTlbMiss(enqIndex) := true.B 617 blockPtrTlb(enqIndex) := Mux(blockPtrTlb(enqIndex) === 3.U(2.W), blockPtrTlb(enqIndex), blockPtrTlb(enqIndex) + 1.U(2.W)) 618 } 619 620 // special case: dcache miss 621 when (replayInfo.cause(LoadReplayCauses.dcacheMiss) && enq.bits.handledByMSHR) { 622 blockByCacheMiss(enqIndex) := !replayInfo.canForwardFullData && // dcache miss 623 !(io.refill.valid && io.refill.bits.id === replayInfo.missMSHRId) // no refill in this cycle 624 625 blockPtrCache(enqIndex) := Mux(blockPtrCache(enqIndex) === 3.U(2.W), blockPtrCache(enqIndex), blockPtrCache(enqIndex) + 1.U(2.W)) 626 } 627 628 // special case: st-ld violation 629 when (replayInfo.cause(LoadReplayCauses.waitStore)) { 630 blockByWaitStore(enqIndex) := true.B 631 blockSqIdx(enqIndex) := replayInfo.addrInvalidSqIdx 632 blockPtrOthers(enqIndex) := Mux(blockPtrOthers(enqIndex) === 3.U(2.W), blockPtrOthers(enqIndex), blockPtrOthers(enqIndex) + 1.U(2.W)) 633 } 634 635 // special case: data forward fail 636 when (replayInfo.cause(LoadReplayCauses.forwardFail)) { 637 blockByForwardFail(enqIndex) := true.B 638 blockSqIdx(enqIndex) := replayInfo.dataInvalidSqIdx 639 blockPtrOthers(enqIndex) := Mux(blockPtrOthers(enqIndex) === 3.U(2.W), blockPtrOthers(enqIndex), blockPtrOthers(enqIndex) + 1.U(2.W)) 640 } 641 642 // special case: rar reject 643 when (replayInfo.cause(LoadReplayCauses.rarReject)) { 644 blockByRARReject(enqIndex) := true.B 645 blockPtrOthers(enqIndex) := Mux(blockPtrOthers(enqIndex) === 3.U(2.W), blockPtrOthers(enqIndex), blockPtrOthers(enqIndex) + 1.U(2.W)) 646 } 647 648 // special case: raw reject 649 when (replayInfo.cause(LoadReplayCauses.rawReject)) { 650 blockByRAWReject(enqIndex) := true.B 651 blockPtrOthers(enqIndex) := Mux(blockPtrOthers(enqIndex) === 3.U(2.W), blockPtrOthers(enqIndex), blockPtrOthers(enqIndex) + 1.U(2.W)) 652 } 653 654 // extra info 655 replayCarryReg(enqIndex) := replayInfo.replayCarry 656 replacementUpdated(enqIndex) := enq.bits.replacementUpdated 657 // update missMSHRId only when the load has already been handled by mshr 658 when(enq.bits.handledByMSHR) { 659 missMSHRId(enqIndex) := replayInfo.missMSHRId 660 } 661 dataInLastBeatReg(enqIndex) := dataInLastBeat 662 } 663 664 // 665 val sleepIndex = enq.bits.sleepIndex 666 when (enq.valid && enq.bits.isLoadReplay) { 667 when (!needReplay(w) || hasExceptions(w)) { 668 allocated(sleepIndex) := false.B 669 freeMaskVec(sleepIndex) := true.B 670 } .otherwise { 671 sleep(sleepIndex) := true.B 672 } 673 } 674 } 675 676 // misprediction recovery / exception redirect 677 for (i <- 0 until LoadQueueReplaySize) { 678 needCancel(i) := uop(i).robIdx.needFlush(io.redirect) && allocated(i) 679 when (needCancel(i)) { 680 allocated(i) := false.B 681 freeMaskVec(i) := true.B 682 } 683 } 684 685 freeList.io.free := freeMaskVec.asUInt 686 687 io.lqFull := lqFull 688 689 // perf cnt 690 val enqCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay)) 691 val deqCount = PopCount(io.replay.map(_.fire)) 692 val deqBlockCount = PopCount(io.replay.map(r => r.valid && !r.ready)) 693 val replayTlbMissCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.tlbMiss))) 694 val replayWaitStoreCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.waitStore))) 695 val replaySchedErrorCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.schedError))) 696 val replayRARRejectCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.rarReject))) 697 val replayRAWRejectCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.rawReject))) 698 val replayBankConflictCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.bankConflict))) 699 val replayDCacheReplayCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.dcacheReplay))) 700 val replayForwardFailCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.forwardFail))) 701 val replayDCacheMissCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.replayInfo.cause(LoadReplayCauses.dcacheMiss))) 702 XSPerfAccumulate("enq", enqCount) 703 XSPerfAccumulate("deq", deqCount) 704 XSPerfAccumulate("deq_block", deqBlockCount) 705 XSPerfAccumulate("replay_full", io.lqFull) 706 XSPerfAccumulate("replay_rar_reject", replayRARRejectCount) 707 XSPerfAccumulate("replay_raw_reject", replayRAWRejectCount) 708 XSPerfAccumulate("replay_sched_error", replaySchedErrorCount) 709 XSPerfAccumulate("replay_wait_store", replayWaitStoreCount) 710 XSPerfAccumulate("replay_tlb_miss", replayTlbMissCount) 711 XSPerfAccumulate("replay_bank_conflict", replayBankConflictCount) 712 XSPerfAccumulate("replay_dcache_replay", replayDCacheReplayCount) 713 XSPerfAccumulate("replay_forward_fail", replayForwardFailCount) 714 XSPerfAccumulate("replay_dcache_miss", replayDCacheMissCount) 715 XSPerfAccumulate("replay_hint_wakeup", hintSelValid) 716 717 val perfEvents: Seq[(String, UInt)] = Seq( 718 ("enq", enqCount), 719 ("deq", deqCount), 720 ("deq_block", deqBlockCount), 721 ("replay_full", io.lqFull), 722 ("replay_rar_reject", replayRARRejectCount), 723 ("replay_raw_reject", replayRAWRejectCount), 724 ("replay_advance_sched", replaySchedErrorCount), 725 ("replay_wait_store", replayWaitStoreCount), 726 ("replay_tlb_miss", replayTlbMissCount), 727 ("replay_bank_conflict", replayBankConflictCount), 728 ("replay_dcache_replay", replayDCacheReplayCount), 729 ("replay_forward_fail", replayForwardFailCount), 730 ("replay_dcache_miss", replayDCacheMissCount), 731 ) 732 generatePerfEvent() 733 // end 734} 735