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 org.chipsalliance.cde.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.wpu.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 re-execute check 48 val C_MA = 0 49 // tlb miss check 50 val C_TM = 1 51 // store-to-load-forwarding check 52 val C_FF = 2 53 // dcache replay check 54 val C_DR = 3 55 // dcache miss check 56 val C_DM = 4 57 // wpu predict fail 58 val C_WF = 5 59 // dcache bank conflict check 60 val C_BC = 6 61 // RAR queue accept check 62 val C_RAR = 7 63 // RAW queue accept check 64 val C_RAW = 8 65 // st-ld violation 66 val C_NK = 9 67 // total causes 68 val allCauses = 10 69} 70 71class AgeDetector(numEntries: Int, numEnq: Int, regOut: Boolean = true)(implicit p: Parameters) extends XSModule { 72 val io = IO(new Bundle { 73 // NOTE: deq and enq may come at the same cycle. 74 val enq = Vec(numEnq, Input(UInt(numEntries.W))) 75 val deq = Input(UInt(numEntries.W)) 76 val ready = Input(UInt(numEntries.W)) 77 val out = Output(UInt(numEntries.W)) 78 }) 79 80 // age(i)(j): entry i enters queue before entry j 81 val age = Seq.fill(numEntries)(Seq.fill(numEntries)(RegInit(false.B))) 82 val nextAge = Seq.fill(numEntries)(Seq.fill(numEntries)(Wire(Bool()))) 83 84 // to reduce reg usage, only use upper matrix 85 def get_age(row: Int, col: Int): Bool = if (row <= col) age(row)(col) else !age(col)(row) 86 def get_next_age(row: Int, col: Int): Bool = if (row <= col) nextAge(row)(col) else !nextAge(col)(row) 87 def isFlushed(i: Int): Bool = io.deq(i) 88 def isEnqueued(i: Int, numPorts: Int = -1): Bool = { 89 val takePorts = if (numPorts == -1) io.enq.length else numPorts 90 takePorts match { 91 case 0 => false.B 92 case 1 => io.enq.head(i) && !isFlushed(i) 93 case n => VecInit(io.enq.take(n).map(_(i))).asUInt.orR && !isFlushed(i) 94 } 95 } 96 97 for ((row, i) <- nextAge.zipWithIndex) { 98 val thisValid = get_age(i, i) || isEnqueued(i) 99 for ((elem, j) <- row.zipWithIndex) { 100 when (isFlushed(i)) { 101 // (1) when entry i is flushed or dequeues, set row(i) to false.B 102 elem := false.B 103 }.elsewhen (isFlushed(j)) { 104 // (2) when entry j is flushed or dequeues, set column(j) to validVec 105 elem := thisValid 106 }.elsewhen (isEnqueued(i)) { 107 // (3) when entry i enqueues from port k, 108 // (3.1) if entry j enqueues from previous ports, set to false 109 // (3.2) otherwise, set to true if and only of entry j is invalid 110 // overall: !jEnqFromPreviousPorts && !jIsValid 111 val sel = io.enq.map(_(i)) 112 val result = (0 until numEnq).map(k => isEnqueued(j, k)) 113 // why ParallelMux: sel must be one-hot since enq is one-hot 114 elem := !get_age(j, j) && !ParallelMux(sel, result) 115 }.otherwise { 116 // default: unchanged 117 elem := get_age(i, j) 118 } 119 age(i)(j) := elem 120 } 121 } 122 123 def getOldest(get: (Int, Int) => Bool): UInt = { 124 VecInit((0 until numEntries).map(i => { 125 io.ready(i) & VecInit((0 until numEntries).map(j => if (i != j) !io.ready(j) || get(i, j) else true.B)).asUInt.andR 126 })).asUInt 127 } 128 val best = getOldest(get_age) 129 val nextBest = getOldest(get_next_age) 130 131 io.out := (if (regOut) best else nextBest) 132} 133 134object AgeDetector { 135 def apply(numEntries: Int, enq: Vec[UInt], deq: UInt, ready: UInt)(implicit p: Parameters): Valid[UInt] = { 136 val age = Module(new AgeDetector(numEntries, enq.length, regOut = true)) 137 age.io.enq := enq 138 age.io.deq := deq 139 age.io.ready:= ready 140 val out = Wire(Valid(UInt(deq.getWidth.W))) 141 out.valid := age.io.out.orR 142 out.bits := age.io.out 143 out 144 } 145} 146 147 148class LoadQueueReplay(implicit p: Parameters) extends XSModule 149 with HasDCacheParameters 150 with HasCircularQueuePtrHelper 151 with HasLoadHelper 152 with HasPerfEvents 153{ 154 val io = IO(new Bundle() { 155 // control 156 val redirect = Flipped(ValidIO(new Redirect)) 157 158 // from load unit s3 159 val enq = Vec(LoadPipelineWidth, Flipped(Decoupled(new LqWriteBundle))) 160 161 // from sta s1 162 val storeAddrIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) 163 164 // from std s1 165 val storeDataIn = Vec(StorePipelineWidth, Flipped(Valid(new ExuOutput))) 166 167 // queue-based replay 168 val replay = Vec(LoadPipelineWidth, Decoupled(new LsPipelineBundle)) 169 val refill = Flipped(ValidIO(new Refill)) 170 val tl_d_channel = Input(new DcacheToLduForwardIO) 171 172 // from StoreQueue 173 val stAddrReadySqPtr = Input(new SqPtr) 174 val stAddrReadyVec = Input(Vec(StoreQueueSize, Bool())) 175 val stDataReadySqPtr = Input(new SqPtr) 176 val stDataReadyVec = Input(Vec(StoreQueueSize, Bool())) 177 178 // 179 val sqEmpty = Input(Bool()) 180 val lqFull = Output(Bool()) 181 val ldWbPtr = Input(new LqPtr) 182 val rarFull = Input(Bool()) 183 val rawFull = Input(Bool()) 184 val l2_hint = Input(Valid(new L2ToL1Hint())) 185 val tlbReplayDelayCycleCtrl = Vec(4, Input(UInt(ReSelectLen.W))) 186 187 val debugTopDown = new LoadQueueTopDownIO 188 }) 189 190 println("LoadQueueReplay size: " + LoadQueueReplaySize) 191 // LoadQueueReplay field: 192 // +-----------+---------+-------+-------------+--------+ 193 // | Allocated | MicroOp | VAddr | Cause | Flags | 194 // +-----------+---------+-------+-------------+--------+ 195 // Allocated : entry has been allocated already 196 // MicroOp : inst's microOp 197 // VAddr : virtual address 198 // Cause : replay cause 199 // Flags : rar/raw queue allocate flags 200 val allocated = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) // The control signals need to explicitly indicate the initial value 201 val scheduled = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 202 val uop = Reg(Vec(LoadQueueReplaySize, new MicroOp)) 203 val vaddrModule = Module(new LqVAddrModule( 204 gen = UInt(VAddrBits.W), 205 numEntries = LoadQueueReplaySize, 206 numRead = LoadPipelineWidth, 207 numWrite = LoadPipelineWidth, 208 numWBank = LoadQueueNWriteBanks, 209 numWDelay = 2, 210 numCamPort = 0)) 211 vaddrModule.io := DontCare 212 val debug_vaddr = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(VAddrBits.W)))) 213 val cause = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(LoadReplayCauses.allCauses.W)))) 214 val blocking = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 215 216 // freeliset: store valid entries index. 217 // +---+---+--------------+-----+-----+ 218 // | 0 | 1 | ...... | n-2 | n-1 | 219 // +---+---+--------------+-----+-----+ 220 val freeList = Module(new FreeList( 221 size = LoadQueueReplaySize, 222 allocWidth = LoadPipelineWidth, 223 freeWidth = 4, 224 enablePreAlloc = true, 225 moduleName = "LoadQueueReplay freelist" 226 )) 227 freeList.io := DontCare 228 /** 229 * used for re-select control 230 */ 231 val credit = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(ReSelectLen.W)))) 232 val selBlocked = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 233 // Ptrs to control which cycle to choose 234 val blockPtrTlb = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(2.W)))) 235 // Specific cycles to block 236 val blockCyclesTlb = Reg(Vec(4, UInt(ReSelectLen.W))) 237 blockCyclesTlb := io.tlbReplayDelayCycleCtrl 238 val blockSqIdx = Reg(Vec(LoadQueueReplaySize, new SqPtr)) 239 // DCache miss block 240 val missMSHRId = RegInit(VecInit(List.fill(LoadQueueReplaySize)(0.U((log2Up(cfg.nMissEntries).W))))) 241 // Has this load already updated dcache replacement? 242 val replacementUpdated = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 243 val missDbUpdated = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 244 val trueCacheMissReplay = WireInit(VecInit(cause.map(_(LoadReplayCauses.C_DM)))) 245 val creditUpdate = WireInit(VecInit(List.fill(LoadQueueReplaySize)(0.U(ReSelectLen.W)))) 246 (0 until LoadQueueReplaySize).map(i => { 247 creditUpdate(i) := Mux(credit(i) > 0.U(ReSelectLen.W), credit(i)-1.U(ReSelectLen.W), credit(i)) 248 selBlocked(i) := creditUpdate(i) =/= 0.U(ReSelectLen.W) || credit(i) =/= 0.U(ReSelectLen.W) 249 }) 250 val replayCarryReg = RegInit(VecInit(List.fill(LoadQueueReplaySize)(ReplayCarry(nWays, 0.U, false.B)))) 251 val dataInLastBeatReg = RegInit(VecInit(List.fill(LoadQueueReplaySize)(false.B))) 252 253 /** 254 * Enqueue 255 */ 256 val canEnqueue = io.enq.map(_.valid) 257 val cancelEnq = io.enq.map(enq => enq.bits.uop.robIdx.needFlush(io.redirect)) 258 val needReplay = io.enq.map(enq => enq.bits.rep_info.need_rep) 259 val hasExceptions = io.enq.map(enq => ExceptionNO.selectByFu(enq.bits.uop.cf.exceptionVec, lduCfg).asUInt.orR && !enq.bits.tlbMiss) 260 val loadReplay = io.enq.map(enq => enq.bits.isLoadReplay) 261 val needEnqueue = VecInit((0 until LoadPipelineWidth).map(w => { 262 canEnqueue(w) && !cancelEnq(w) && needReplay(w) && !hasExceptions(w) 263 })) 264 val canFreeVec = VecInit((0 until LoadPipelineWidth).map(w => { 265 canEnqueue(w) && loadReplay(w) && (!needReplay(w) || hasExceptions(w)) 266 })) 267 268 // select LoadPipelineWidth valid index. 269 val lqFull = freeList.io.empty 270 val lqFreeNums = freeList.io.validCount 271 272 // replay logic 273 // release logic generation 274 val storeAddrInSameCycleVec = Wire(Vec(LoadQueueReplaySize, Bool())) 275 val storeDataInSameCycleVec = Wire(Vec(LoadQueueReplaySize, Bool())) 276 val addrNotBlockVec = Wire(Vec(LoadQueueReplaySize, Bool())) 277 val dataNotBlockVec = Wire(Vec(LoadQueueReplaySize, Bool())) 278 val storeAddrValidVec = addrNotBlockVec.asUInt | storeAddrInSameCycleVec.asUInt 279 val storeDataValidVec = dataNotBlockVec.asUInt | storeDataInSameCycleVec.asUInt 280 281 // store data valid check 282 val stAddrReadyVec = io.stAddrReadyVec 283 val stDataReadyVec = io.stDataReadyVec 284 285 for (i <- 0 until LoadQueueReplaySize) { 286 // dequeue 287 // FIXME: store*Ptr is not accurate 288 dataNotBlockVec(i) := !isBefore(io.stDataReadySqPtr, blockSqIdx(i)) || stDataReadyVec(blockSqIdx(i).value) || io.sqEmpty // for better timing 289 addrNotBlockVec(i) := !isBefore(io.stAddrReadySqPtr, blockSqIdx(i)) || stAddrReadyVec(blockSqIdx(i).value) || io.sqEmpty // for better timing 290 291 // store address execute 292 storeAddrInSameCycleVec(i) := VecInit((0 until StorePipelineWidth).map(w => { 293 io.storeAddrIn(w).valid && 294 !io.storeAddrIn(w).bits.miss && 295 blockSqIdx(i) === io.storeAddrIn(w).bits.uop.sqIdx 296 })).asUInt.orR // for better timing 297 298 // store data execute 299 storeDataInSameCycleVec(i) := VecInit((0 until StorePipelineWidth).map(w => { 300 io.storeDataIn(w).valid && 301 blockSqIdx(i) === io.storeDataIn(w).bits.uop.sqIdx 302 })).asUInt.orR // for better timing 303 304 } 305 306 // store addr issue check 307 val stAddrDeqVec = Wire(Vec(LoadQueueReplaySize, Bool())) 308 (0 until LoadQueueReplaySize).map(i => { 309 stAddrDeqVec(i) := allocated(i) && storeAddrValidVec(i) 310 }) 311 312 // store data issue check 313 val stDataDeqVec = Wire(Vec(LoadQueueReplaySize, Bool())) 314 (0 until LoadQueueReplaySize).map(i => { 315 stDataDeqVec(i) := allocated(i) && storeDataValidVec(i) 316 }) 317 318 // update blocking condition 319 (0 until LoadQueueReplaySize).map(i => { 320 // case C_MA 321 when (cause(i)(LoadReplayCauses.C_MA)) { 322 blocking(i) := Mux(stAddrDeqVec(i), false.B, blocking(i)) 323 } 324 // case C_TM 325 when (cause(i)(LoadReplayCauses.C_TM)) { 326 blocking(i) := Mux(creditUpdate(i) === 0.U, false.B, blocking(i)) 327 } 328 // case C_FF 329 when (cause(i)(LoadReplayCauses.C_FF)) { 330 blocking(i) := Mux(stDataDeqVec(i), false.B, blocking(i)) 331 } 332 // case C_DM 333 when (cause(i)(LoadReplayCauses.C_DM)) { 334 blocking(i) := Mux(io.tl_d_channel.valid && io.tl_d_channel.mshrid === missMSHRId(i), false.B, blocking(i)) 335 } 336 // case C_RAR 337 when (cause(i)(LoadReplayCauses.C_RAR)) { 338 blocking(i) := Mux((!io.rarFull || !isAfter(uop(i).lqIdx, io.ldWbPtr)), false.B, blocking(i)) 339 } 340 // case C_RAW 341 when (cause(i)(LoadReplayCauses.C_RAW)) { 342 blocking(i) := Mux((!io.rawFull || !isAfter(uop(i).sqIdx, io.stAddrReadySqPtr)), false.B, blocking(i)) 343 } 344 }) 345 346 // Replay is splitted into 3 stages 347 require((LoadQueueReplaySize % LoadPipelineWidth) == 0) 348 def getRemBits(input: UInt)(rem: Int): UInt = { 349 VecInit((0 until LoadQueueReplaySize / LoadPipelineWidth).map(i => { input(LoadPipelineWidth * i + rem) })).asUInt 350 } 351 352 def getRemSeq(input: Seq[Seq[Bool]])(rem: Int) = { 353 (0 until LoadQueueReplaySize / LoadPipelineWidth).map(i => { input(LoadPipelineWidth * i + rem) }) 354 } 355 356 // stage1: select 2 entries and read their vaddr 357 val s0_oldestSel = Wire(Vec(LoadPipelineWidth, Valid(UInt(LoadQueueReplaySize.W)))) 358 val s1_can_go = Wire(Vec(LoadPipelineWidth, Bool())) 359 val s1_oldestSel = Wire(Vec(LoadPipelineWidth, Valid(UInt(log2Up(LoadQueueReplaySize + 1).W)))) 360 val s2_can_go = Wire(Vec(LoadPipelineWidth, Bool())) 361 val s2_oldestSel = Wire(Vec(LoadPipelineWidth, Valid(UInt(log2Up(LoadQueueReplaySize + 1).W)))) 362 363 // generate mask 364 val needCancel = Wire(Vec(LoadQueueReplaySize, Bool())) 365 // generate enq mask 366 val enqIndexOH = Wire(Vec(LoadPipelineWidth, UInt(LoadQueueReplaySize.W))) 367 val s0_loadEnqFireMask = io.enq.map(x => x.fire && !x.bits.isLoadReplay).zip(enqIndexOH).map(x => Mux(x._1, x._2, 0.U)) 368 val s0_remLoadEnqFireVec = s0_loadEnqFireMask.map(x => VecInit((0 until LoadPipelineWidth).map(rem => getRemBits(x)(rem)))) 369 val s0_remEnqSelVec = Seq.tabulate(LoadPipelineWidth)(w => VecInit(s0_remLoadEnqFireVec.map(x => x(w)))) 370 371 // generate free mask 372 val s0_loadFreeSelMask = needCancel.asUInt 373 val s0_remFreeSelVec = VecInit(Seq.tabulate(LoadPipelineWidth)(rem => getRemBits(s0_loadFreeSelMask)(rem))) 374 375 // l2 hint wakes up cache missed load 376 // 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 377 val s0_loadHintWakeMask = VecInit((0 until LoadQueueReplaySize).map(i => { 378 allocated(i) && !scheduled(i) && cause(i)(LoadReplayCauses.C_DM) && blocking(i) && missMSHRId(i) === io.l2_hint.bits.sourceId && io.l2_hint.valid && !needCancel(i) 379 })).asUInt 380 // 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 381 val s0_loadHintSelMask = s0_loadHintWakeMask & VecInit(dataInLastBeatReg.map(!_)).asUInt 382 val s0_remLoadHintSelMask = VecInit((0 until LoadPipelineWidth).map(rem => getRemBits(s0_loadHintSelMask)(rem))) 383 val s0_remHintSelValidVec = VecInit((0 until LoadPipelineWidth).map(rem => ParallelORR(s0_remLoadHintSelMask(rem)))) 384 val s0_hintSelValid = s0_loadHintSelMask.orR 385 386 // wake up cache missed load 387 (0 until LoadQueueReplaySize).foreach(i => { 388 when(s0_loadHintWakeMask(i)) { 389 blocking(i) := false.B 390 creditUpdate(i) := 0.U 391 } 392 }) 393 394 // generate replay mask 395 // replay select priority is given as follow 396 // 1. hint wake up load 397 // 2. higher priority load 398 // 3. lower priority load 399 val s0_loadHigherPriorityReplaySelMask = VecInit((0 until LoadQueueReplaySize).map(i => { 400 val blocked = selBlocked(i) || blocking(i) 401 val hasHigherPriority = cause(i)(LoadReplayCauses.C_DM) || cause(i)(LoadReplayCauses.C_FF) 402 allocated(i) && !scheduled(i) && !blocked && hasHigherPriority && !needCancel(i) 403 })).asUInt // use uint instead vec to reduce verilog lines 404 val s0_remLoadHigherPriorityReplaySelMask = VecInit((0 until LoadPipelineWidth).map(rem => getRemBits(s0_loadHigherPriorityReplaySelMask)(rem))) 405 val s0_loadLowerPriorityReplaySelMask = VecInit((0 until LoadQueueReplaySize).map(i => { 406 val blocked = selBlocked(i) || blocking(i) 407 val hasLowerPriority = !cause(i)(LoadReplayCauses.C_DM) && !cause(i)(LoadReplayCauses.C_FF) 408 allocated(i) && !scheduled(i) && !blocked && hasLowerPriority && !needCancel(i) 409 })).asUInt // use uint instead vec to reduce verilog lines 410 val s0_remLoadLowerPriorityReplaySelMask = VecInit((0 until LoadPipelineWidth).map(rem => getRemBits(s0_loadLowerPriorityReplaySelMask)(rem))) 411 val s0_loadNormalReplaySelMask = s0_loadLowerPriorityReplaySelMask | s0_loadHigherPriorityReplaySelMask | s0_loadHintSelMask 412 val s0_remNormalReplaySelVec = VecInit((0 until LoadPipelineWidth).map(rem => s0_remLoadLowerPriorityReplaySelMask(rem) | s0_remLoadHigherPriorityReplaySelMask(rem) | s0_remLoadHintSelMask(rem))) 413 val s0_remPriorityReplaySelVec = VecInit((0 until LoadPipelineWidth).map(rem => { 414 Mux(s0_remHintSelValidVec(rem), s0_remLoadHintSelMask(rem), 415 Mux(ParallelORR(s0_remLoadHigherPriorityReplaySelMask(rem)), s0_remLoadHigherPriorityReplaySelMask(rem), s0_remLoadLowerPriorityReplaySelMask(rem))) 416 })) 417 /****************************************************************************************************** 418 * WARNING: Make sure that OldestSelectStride must less than or equal stages of load pipeline. * 419 ****************************************************************************************************** 420 */ 421 val OldestSelectStride = 4 422 val oldestPtrExt = (0 until OldestSelectStride).map(i => io.ldWbPtr + i.U) 423 val s0_oldestMatchMaskVec = (0 until LoadQueueReplaySize).map(i => (0 until OldestSelectStride).map(j => s0_loadNormalReplaySelMask(i) && uop(i).lqIdx === oldestPtrExt(j))) 424 val s0_remOldsetMatchMaskVec = (0 until LoadPipelineWidth).map(rem => getRemSeq(s0_oldestMatchMaskVec.map(_.take(1)))(rem)) 425 val s0_remOlderMatchMaskVec = (0 until LoadPipelineWidth).map(rem => getRemSeq(s0_oldestMatchMaskVec.map(_.drop(1)))(rem)) 426 val s0_remOldestSelVec = VecInit(Seq.tabulate(LoadPipelineWidth)(rem => { 427 VecInit((0 until LoadQueueReplaySize / LoadPipelineWidth).map(i => { 428 Mux(ParallelORR(s0_remOldsetMatchMaskVec(rem).map(_(0))), s0_remOldsetMatchMaskVec(rem)(i)(0), s0_remOlderMatchMaskVec(rem)(i).reduce(_|_)) 429 })).asUInt 430 })) 431 val s0_remOldestHintSelVec = s0_remOldestSelVec.zip(s0_remLoadHintSelMask).map { 432 case(oldestVec, hintVec) => oldestVec & hintVec 433 } 434 435 // select oldest logic 436 s0_oldestSel := VecInit((0 until LoadPipelineWidth).map(rport => { 437 // select enqueue earlest inst 438 val ageOldest = AgeDetector(LoadQueueReplaySize / LoadPipelineWidth, s0_remEnqSelVec(rport), s0_remFreeSelVec(rport), s0_remPriorityReplaySelVec(rport)) 439 assert(!(ageOldest.valid && PopCount(ageOldest.bits) > 1.U), "oldest index must be one-hot!") 440 val ageOldestValid = ageOldest.valid 441 val ageOldestIndexOH = ageOldest.bits 442 443 // select program order oldest 444 val l2HintFirst = io.l2_hint.valid && ParallelORR(s0_remOldestHintSelVec(rport)) 445 val issOldestValid = l2HintFirst || ParallelORR(s0_remOldestSelVec(rport)) 446 val issOldestIndexOH = Mux(l2HintFirst, PriorityEncoderOH(s0_remOldestHintSelVec(rport)), PriorityEncoderOH(s0_remOldestSelVec(rport))) 447 448 val oldest = Wire(Valid(UInt())) 449 val oldestSel = Mux(issOldestValid, issOldestIndexOH, ageOldestIndexOH) 450 val oldestBitsVec = Wire(Vec(LoadQueueReplaySize, Bool())) 451 452 require((LoadQueueReplaySize % LoadPipelineWidth) == 0) 453 oldestBitsVec.foreach(e => e := false.B) 454 for (i <- 0 until LoadQueueReplaySize / LoadPipelineWidth) { 455 oldestBitsVec(i * LoadPipelineWidth + rport) := oldestSel(i) 456 } 457 458 oldest.valid := ageOldest.valid || issOldestValid 459 oldest.bits := oldestBitsVec.asUInt 460 oldest 461 })) 462 463 464 // Replay port reorder 465 class BalanceEntry extends XSBundle { 466 val balance = Bool() 467 val index = UInt(log2Up(LoadQueueReplaySize).W) 468 val port = UInt(log2Up(LoadPipelineWidth).W) 469 } 470 471 def balanceReOrder(sel: Seq[ValidIO[BalanceEntry]]): Seq[ValidIO[BalanceEntry]] = { 472 require(sel.length > 0) 473 val balancePick = ParallelPriorityMux(sel.map(x => (x.valid && x.bits.balance) -> x)) 474 val reorderSel = Wire(Vec(sel.length, ValidIO(new BalanceEntry))) 475 (0 until sel.length).map(i => 476 if (i == 0) { 477 when (balancePick.valid && balancePick.bits.balance) { 478 reorderSel(i) := balancePick 479 } .otherwise { 480 reorderSel(i) := sel(i) 481 } 482 } else { 483 when (balancePick.valid && balancePick.bits.balance && i.U === balancePick.bits.port) { 484 reorderSel(i) := sel(0) 485 } .otherwise { 486 reorderSel(i) := sel(i) 487 } 488 } 489 ) 490 reorderSel 491 } 492 493 // stage2: send replay request to load unit 494 // replay cold down 495 val ColdDownCycles = 16 496 val coldCounter = RegInit(VecInit(List.fill(LoadPipelineWidth)(0.U(log2Up(ColdDownCycles).W)))) 497 val ColdDownThreshold = Wire(UInt(log2Up(ColdDownCycles).W)) 498 ColdDownThreshold := Constantin.createRecord("ColdDownThreshold_"+p(XSCoreParamsKey).HartId.toString(), initValue = 12.U) 499 assert(ColdDownCycles.U > ColdDownThreshold, "ColdDownCycles must great than ColdDownThreshold!") 500 501 def replayCanFire(i: Int) = coldCounter(i) >= 0.U && coldCounter(i) < ColdDownThreshold 502 def coldDownNow(i: Int) = coldCounter(i) >= ColdDownThreshold 503 504 val s1_balanceOldestSelExt = (0 until LoadPipelineWidth).map(i => { 505 val wrapper = Wire(Valid(new BalanceEntry)) 506 wrapper.valid := s1_oldestSel(i).valid 507 wrapper.bits.balance := cause(s1_oldestSel(i).bits)(LoadReplayCauses.C_BC) 508 wrapper.bits.index := s1_oldestSel(i).bits 509 wrapper.bits.port := i.U 510 wrapper 511 }) 512 513 val s1_balanceOldestSel = VecInit(balanceReOrder(s1_balanceOldestSelExt)) 514 for (i <- 0 until LoadPipelineWidth) { 515 val s0_can_go = s1_can_go(s1_balanceOldestSel(i).bits.port) || uop(s1_oldestSel(i).bits).robIdx.needFlush(io.redirect) 516 val s0_oldestSelIndexOH = s0_oldestSel(i).bits // one-hot 517 s1_oldestSel(i).valid := RegEnable(s0_oldestSel(i).valid, s0_can_go) 518 s1_oldestSel(i).bits := RegEnable(OHToUInt(s0_oldestSel(i).bits), s0_can_go) 519 520 for (j <- 0 until LoadQueueReplaySize) { 521 when (s0_can_go && s0_oldestSel(i).valid && s0_oldestSelIndexOH(j)) { 522 scheduled(j) := true.B 523 } 524 } 525 } 526 val s2_cancelReplay = Wire(Vec(LoadPipelineWidth, Bool())) 527 for (i <- 0 until LoadPipelineWidth) { 528 val s1_cancel = uop(s1_balanceOldestSel(i).bits.index).robIdx.needFlush(io.redirect) 529 val s1_oldestSelV = s1_balanceOldestSel(i).valid && !s1_cancel 530 s1_can_go(i) := Mux(s2_oldestSel(i).valid && !s2_cancelReplay(i), io.replay(i).ready && replayCanFire(i), true.B) 531 s2_oldestSel(i).valid := RegEnable(s1_oldestSelV, s1_can_go(i)) 532 s2_oldestSel(i).bits := RegEnable(s1_balanceOldestSel(i).bits.index, s1_can_go(i)) 533 534 vaddrModule.io.ren(i) := s1_balanceOldestSel(i).valid && s1_can_go(i) 535 vaddrModule.io.raddr(i) := s1_balanceOldestSel(i).bits.index 536 } 537 538 for (i <- 0 until LoadPipelineWidth) { 539 val s1_replayIdx = s1_balanceOldestSel(i).bits.index 540 val s2_replayUop = RegEnable(uop(s1_replayIdx), s1_can_go(i)) 541 val s2_replayMSHRId = RegEnable(missMSHRId(s1_replayIdx), s1_can_go(i)) 542 val s2_replacementUpdated = RegEnable(replacementUpdated(s1_replayIdx), s1_can_go(i)) 543 val s2_missDbUpdated = RegEnable(missDbUpdated(s1_replayIdx), s1_can_go(i)) 544 val s2_replayCauses = RegEnable(cause(s1_replayIdx), s1_can_go(i)) 545 val s2_replayCarry = RegEnable(replayCarryReg(s1_replayIdx), s1_can_go(i)) 546 val s2_replayCacheMissReplay = RegEnable(trueCacheMissReplay(s1_replayIdx), s1_can_go(i)) 547 s2_cancelReplay(i) := s2_replayUop.robIdx.needFlush(io.redirect) 548 549 s2_can_go(i) := DontCare 550 io.replay(i).valid := s2_oldestSel(i).valid && !s2_cancelReplay(i) && replayCanFire(i) 551 io.replay(i).bits := DontCare 552 io.replay(i).bits.uop := s2_replayUop 553 io.replay(i).bits.vaddr := vaddrModule.io.rdata(i) 554 io.replay(i).bits.isFirstIssue := false.B 555 io.replay(i).bits.isLoadReplay := true.B 556 io.replay(i).bits.replayCarry := s2_replayCarry 557 io.replay(i).bits.mshrid := s2_replayMSHRId 558 io.replay(i).bits.replacementUpdated := s2_replacementUpdated 559 io.replay(i).bits.missDbUpdated := s2_missDbUpdated 560 io.replay(i).bits.forward_tlDchannel := s2_replayCauses(LoadReplayCauses.C_DM) 561 io.replay(i).bits.schedIndex := s2_oldestSel(i).bits 562 563 when (io.replay(i).fire) { 564 XSError(!allocated(s2_oldestSel(i).bits), p"LoadQueueReplay: why replay an invalid entry ${s2_oldestSel(i).bits} ?") 565 } 566 } 567 568 // update cold counter 569 val lastReplay = RegNext(VecInit(io.replay.map(_.fire))) 570 for (i <- 0 until LoadPipelineWidth) { 571 when (lastReplay(i) && io.replay(i).fire) { 572 coldCounter(i) := coldCounter(i) + 1.U 573 } .elsewhen (coldDownNow(i)) { 574 coldCounter(i) := coldCounter(i) + 1.U 575 } .otherwise { 576 coldCounter(i) := 0.U 577 } 578 } 579 580 when(io.refill.valid) { 581 XSDebug("miss resp: paddr:0x%x data %x\n", io.refill.bits.addr, io.refill.bits.data) 582 } 583 584 // LoadQueueReplay deallocate 585 val freeMaskVec = Wire(Vec(LoadQueueReplaySize, Bool())) 586 587 // init 588 freeMaskVec.map(e => e := false.B) 589 590 // Allocate logic 591 val newEnqueue = (0 until LoadPipelineWidth).map(i => { 592 needEnqueue(i) && !io.enq(i).bits.isLoadReplay 593 }) 594 595 for ((enq, w) <- io.enq.zipWithIndex) { 596 vaddrModule.io.wen(w) := false.B 597 freeList.io.doAllocate(w) := false.B 598 599 freeList.io.allocateReq(w) := true.B 600 601 // Allocated ready 602 val offset = PopCount(newEnqueue.take(w)) 603 val canAccept = freeList.io.canAllocate(offset) 604 val enqIndex = Mux(enq.bits.isLoadReplay, enq.bits.schedIndex, freeList.io.allocateSlot(offset)) 605 enqIndexOH(w) := UIntToOH(enqIndex) 606 enq.ready := Mux(enq.bits.isLoadReplay, true.B, canAccept) 607 608 when (needEnqueue(w) && enq.ready) { 609 610 val debug_robIdx = enq.bits.uop.robIdx.asUInt 611 XSError(allocated(enqIndex) && !enq.bits.isLoadReplay, p"LoadQueueReplay: can not accept more load, check: ldu $w, robIdx $debug_robIdx!") 612 XSError(hasExceptions(w), p"LoadQueueReplay: The instruction has exception, it can not be replay, check: ldu $w, robIdx $debug_robIdx!") 613 614 freeList.io.doAllocate(w) := !enq.bits.isLoadReplay 615 616 // Allocate new entry 617 allocated(enqIndex) := true.B 618 scheduled(enqIndex) := false.B 619 uop(enqIndex) := enq.bits.uop 620 621 vaddrModule.io.wen(w) := true.B 622 vaddrModule.io.waddr(w) := enqIndex 623 vaddrModule.io.wdata(w) := enq.bits.vaddr 624 debug_vaddr(enqIndex) := enq.bits.vaddr 625 626 /** 627 * used for feedback and replay 628 */ 629 // set flags 630 val replayInfo = enq.bits.rep_info 631 val dataInLastBeat = replayInfo.last_beat 632 cause(enqIndex) := replayInfo.cause.asUInt 633 634 // update credit 635 val blockCyclesTlbPtr = blockPtrTlb(enqIndex) 636 637 // init 638 blocking(enqIndex) := true.B 639 creditUpdate(enqIndex) := 0.U 640 641 // update blocking pointer 642 when (replayInfo.cause(LoadReplayCauses.C_BC) || 643 replayInfo.cause(LoadReplayCauses.C_NK) || 644 replayInfo.cause(LoadReplayCauses.C_DR)) { 645 // normal case: bank conflict or schedule error or dcache replay 646 // can replay next cycle 647 blocking(enqIndex) := false.B 648 } 649 650 // special case: tlb miss 651 when (replayInfo.cause(LoadReplayCauses.C_TM)) { 652 creditUpdate(enqIndex) := blockCyclesTlb(blockCyclesTlbPtr) 653 blockPtrTlb(enqIndex) := Mux(blockPtrTlb(enqIndex) === 3.U(2.W), blockPtrTlb(enqIndex), blockPtrTlb(enqIndex) + 1.U(2.W)) 654 } 655 656 // special case: dcache miss 657 when (replayInfo.cause(LoadReplayCauses.C_DM) && enq.bits.handledByMSHR) { 658 blocking(enqIndex) := !replayInfo.full_fwd && // dcache miss 659 !(io.tl_d_channel.valid && io.tl_d_channel.mshrid === replayInfo.mshr_id) // no refill in this cycle 660 } 661 662 // special case: st-ld violation 663 when (replayInfo.cause(LoadReplayCauses.C_MA)) { 664 blockSqIdx(enqIndex) := replayInfo.addr_inv_sq_idx 665 } 666 667 // special case: data forward fail 668 when (replayInfo.cause(LoadReplayCauses.C_FF)) { 669 blockSqIdx(enqIndex) := replayInfo.data_inv_sq_idx 670 } 671 // extra info 672 replayCarryReg(enqIndex) := replayInfo.rep_carry 673 replacementUpdated(enqIndex) := enq.bits.replacementUpdated 674 missDbUpdated(enqIndex) := enq.bits.missDbUpdated 675 // update mshr_id only when the load has already been handled by mshr 676 when(enq.bits.handledByMSHR) { 677 missMSHRId(enqIndex) := replayInfo.mshr_id 678 } 679 dataInLastBeatReg(enqIndex) := dataInLastBeat 680 } 681 682 // 683 val schedIndex = enq.bits.schedIndex 684 when (enq.valid && enq.bits.isLoadReplay) { 685 when (!needReplay(w) || hasExceptions(w)) { 686 allocated(schedIndex) := false.B 687 freeMaskVec(schedIndex) := true.B 688 } .otherwise { 689 scheduled(schedIndex) := false.B 690 } 691 } 692 } 693 694 // misprediction recovery / exception redirect 695 for (i <- 0 until LoadQueueReplaySize) { 696 needCancel(i) := uop(i).robIdx.needFlush(io.redirect) && allocated(i) 697 when (needCancel(i)) { 698 allocated(i) := false.B 699 freeMaskVec(i) := true.B 700 } 701 } 702 703 freeList.io.free := freeMaskVec.asUInt 704 705 io.lqFull := lqFull 706 707 // Topdown 708 val robHeadVaddr = io.debugTopDown.robHeadVaddr 709 710 val uop_wrapper = Wire(Vec(LoadQueueReplaySize, new XSBundleWithMicroOp)) 711 (uop_wrapper.zipWithIndex).foreach { 712 case (u, i) => { 713 u.uop := uop(i) 714 } 715 } 716 val lq_match_vec = (debug_vaddr.zip(allocated)).map{case(va, alloc) => alloc && (va === robHeadVaddr.bits)} 717 val rob_head_lq_match = ParallelOperation(lq_match_vec.zip(uop_wrapper), (a: Tuple2[Bool, XSBundleWithMicroOp], b: Tuple2[Bool, XSBundleWithMicroOp]) => { 718 val (a_v, a_uop) = (a._1, a._2) 719 val (b_v, b_uop) = (b._1, b._2) 720 721 val res = Mux(a_v && b_v, Mux(isAfter(a_uop.uop.robIdx, b_uop.uop.robIdx), b_uop, a_uop), 722 Mux(a_v, a_uop, 723 Mux(b_v, b_uop, 724 a_uop))) 725 (a_v || b_v, res) 726 }) 727 728 val lq_match_bits = rob_head_lq_match._2.uop 729 val lq_match = rob_head_lq_match._1 && robHeadVaddr.valid 730 val lq_match_idx = lq_match_bits.lqIdx.value 731 732 val rob_head_tlb_miss = lq_match && cause(lq_match_idx)(LoadReplayCauses.C_TM) 733 val rob_head_nuke = lq_match && cause(lq_match_idx)(LoadReplayCauses.C_NK) 734 val rob_head_mem_amb = lq_match && cause(lq_match_idx)(LoadReplayCauses.C_MA) 735 val rob_head_confilct_replay = lq_match && cause(lq_match_idx)(LoadReplayCauses.C_BC) 736 val rob_head_forward_fail = lq_match && cause(lq_match_idx)(LoadReplayCauses.C_FF) 737 val rob_head_mshrfull_replay = lq_match && cause(lq_match_idx)(LoadReplayCauses.C_DR) 738 val rob_head_dcache_miss = lq_match && cause(lq_match_idx)(LoadReplayCauses.C_DM) 739 val rob_head_rar_nack = lq_match && cause(lq_match_idx)(LoadReplayCauses.C_RAR) 740 val rob_head_raw_nack = lq_match && cause(lq_match_idx)(LoadReplayCauses.C_RAW) 741 val rob_head_other_replay = lq_match && (rob_head_rar_nack || rob_head_raw_nack || rob_head_forward_fail) 742 743 val rob_head_vio_replay = rob_head_nuke || rob_head_mem_amb 744 745 val rob_head_miss_in_dtlb = io.debugTopDown.robHeadMissInDTlb 746 io.debugTopDown.robHeadTlbReplay := rob_head_tlb_miss && !rob_head_miss_in_dtlb 747 io.debugTopDown.robHeadTlbMiss := rob_head_tlb_miss && rob_head_miss_in_dtlb 748 io.debugTopDown.robHeadLoadVio := rob_head_vio_replay 749 io.debugTopDown.robHeadLoadMSHR := rob_head_mshrfull_replay 750 io.debugTopDown.robHeadOtherReplay := rob_head_other_replay 751 val perfValidCount = RegNext(PopCount(allocated)) 752 753 // perf cnt 754 val enqNumber = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay)) 755 val deqNumber = PopCount(io.replay.map(_.fire)) 756 val deqBlockCount = PopCount(io.replay.map(r => r.valid && !r.ready)) 757 val replayTlbMissCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.rep_info.cause(LoadReplayCauses.C_TM))) 758 val replayMemAmbCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.rep_info.cause(LoadReplayCauses.C_NK))) 759 val replayNukeCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.rep_info.cause(LoadReplayCauses.C_MA))) 760 val replayRARRejectCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.rep_info.cause(LoadReplayCauses.C_RAR))) 761 val replayRAWRejectCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.rep_info.cause(LoadReplayCauses.C_RAW))) 762 val replayBankConflictCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.rep_info.cause(LoadReplayCauses.C_BC))) 763 val replayDCacheReplayCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.rep_info.cause(LoadReplayCauses.C_DR))) 764 val replayForwardFailCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.rep_info.cause(LoadReplayCauses.C_FF))) 765 val replayDCacheMissCount = PopCount(io.enq.map(enq => enq.fire && !enq.bits.isLoadReplay && enq.bits.rep_info.cause(LoadReplayCauses.C_DM))) 766 XSPerfAccumulate("enq", enqNumber) 767 XSPerfAccumulate("deq", deqNumber) 768 XSPerfAccumulate("deq_block", deqBlockCount) 769 XSPerfAccumulate("replay_full", io.lqFull) 770 XSPerfAccumulate("replay_rar_nack", replayRARRejectCount) 771 XSPerfAccumulate("replay_raw_nack", replayRAWRejectCount) 772 XSPerfAccumulate("replay_nuke", replayNukeCount) 773 XSPerfAccumulate("replay_mem_amb", replayMemAmbCount) 774 XSPerfAccumulate("replay_tlb_miss", replayTlbMissCount) 775 XSPerfAccumulate("replay_bank_conflict", replayBankConflictCount) 776 XSPerfAccumulate("replay_dcache_replay", replayDCacheReplayCount) 777 XSPerfAccumulate("replay_forward_fail", replayForwardFailCount) 778 XSPerfAccumulate("replay_dcache_miss", replayDCacheMissCount) 779 XSPerfAccumulate("replay_hint_wakeup", s0_hintSelValid) 780 781 val perfEvents: Seq[(String, UInt)] = Seq( 782 ("enq", enqNumber), 783 ("deq", deqNumber), 784 ("deq_block", deqBlockCount), 785 ("replay_full", io.lqFull), 786 ("replay_rar_nack", replayRARRejectCount), 787 ("replay_raw_nack", replayRAWRejectCount), 788 ("replay_nuke", replayNukeCount), 789 ("replay_mem_amb", replayMemAmbCount), 790 ("replay_tlb_miss", replayTlbMissCount), 791 ("replay_bank_conflict", replayBankConflictCount), 792 ("replay_dcache_replay", replayDCacheReplayCount), 793 ("replay_forward_fail", replayForwardFailCount), 794 ("replay_dcache_miss", replayDCacheMissCount), 795 ) 796 generatePerfEvent() 797 // end 798} 799