1/*************************************************************************************** 2* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC) 3* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences 4* Copyright (c) 2020-2021 Peng Cheng Laboratory 5* 6* XiangShan is licensed under Mulan PSL v2. 7* You can use this software according to the terms and conditions of the Mulan PSL v2. 8* You may obtain a copy of Mulan PSL v2 at: 9* http://license.coscl.org.cn/MulanPSL2 10* 11* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 12* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 13* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 14* 15* See the Mulan PSL v2 for more details. 16***************************************************************************************/ 17package xiangshan.mem 18 19import chisel3._ 20import chisel3.util._ 21import org.chipsalliance.cde.config._ 22import xiangshan._ 23import xiangshan.backend.rob.{RobLsqIO, RobPtr} 24import xiangshan.ExceptionNO._ 25import xiangshan.cache._ 26import utils._ 27import utility._ 28import xiangshan.backend.Bundles.{DynInst, MemExuOutput, UopIdx} 29import xiangshan.backend.fu.FuConfig.LduCfg 30import xiangshan.backend.decode.isa.bitfield.{InstVType, XSInstBitFields} 31import xiangshan.backend.fu.FuType 32 33class VirtualLoadQueue(implicit p: Parameters) extends XSModule 34 with HasDCacheParameters 35 with HasCircularQueuePtrHelper 36 with HasLoadHelper 37 with HasPerfEvents 38 with HasVLSUParameters { 39 val io = IO(new Bundle() { 40 // control 41 val redirect = Flipped(Valid(new Redirect)) 42 val vecCommit = Vec(VecLoadPipelineWidth, Flipped(ValidIO(new FeedbackToLsqIO))) 43 // from dispatch 44 val enq = new LqEnqIO 45 // from ldu s3 46 val ldin = Vec(LoadPipelineWidth, Flipped(DecoupledIO(new LqWriteBundle))) 47 // to LoadQueueReplay and LoadQueueRAR 48 val ldWbPtr = Output(new LqPtr) 49 // global 50 val lqFull = Output(Bool()) 51 val lqEmpty = Output(Bool()) 52 // to dispatch 53 val lqDeq = Output(UInt(log2Up(CommitWidth + 1).W)) 54 val lqCancelCnt = Output(UInt(log2Up(VirtualLoadQueueSize+1).W)) 55 // for topdown 56 val noUopsIssued = Input(Bool()) 57 }) 58 59 println("VirtualLoadQueue: size: " + VirtualLoadQueueSize) 60 // VirtualLoadQueue field 61 // +-----------+---------+-------+ 62 // | Allocated | MicroOp | Flags | 63 // +-----------+---------+-------+ 64 // Allocated : entry has been allocated already 65 // MicroOp : inst's microOp 66 // Flags : load flags 67 val allocated = RegInit(VecInit(List.fill(VirtualLoadQueueSize)(false.B))) // The control signals need to explicitly indicate the initial value 68 val robIdx = Reg(Vec(VirtualLoadQueueSize, new RobPtr)) 69 val uopIdx = Reg(Vec(VirtualLoadQueueSize, UopIdx())) 70 val isvec = RegInit(VecInit(List.fill(VirtualLoadQueueSize)(false.B))) // vector load flow 71 val committed = Reg(Vec(VirtualLoadQueueSize, Bool())) 72 73 /** 74 * used for debug 75 */ 76 val debug_mmio = Reg(Vec(VirtualLoadQueueSize, Bool())) // mmio: inst is an mmio inst 77 val debug_paddr = Reg(Vec(VirtualLoadQueueSize, UInt(PAddrBits.W))) // mmio: inst's paddr 78 79 // maintain pointers 80 val enqPtrExt = RegInit(VecInit((0 until io.enq.req.length).map(_.U.asTypeOf(new LqPtr)))) 81 val enqPtr = enqPtrExt(0).value 82 val deqPtr = Wire(new LqPtr) 83 val deqPtrNext = Wire(new LqPtr) 84 85 /** 86 * update pointer 87 */ 88 val lastCycleRedirect = RegNext(io.redirect) 89 val lastLastCycleRedirect = RegNext(lastCycleRedirect) 90 91 val validCount = distanceBetween(enqPtrExt(0), deqPtr) 92 val allowEnqueue = validCount <= (VirtualLoadQueueSize - LSQLdEnqWidth).U 93 val canEnqueue = io.enq.req.map(_.valid) 94 val vLoadFlow = io.enq.req.map(_.bits.numLsElem.asTypeOf(UInt(elemIdxBits.W))) 95 val needCancel = WireInit(VecInit((0 until VirtualLoadQueueSize).map(i => { 96 robIdx(i).needFlush(io.redirect) && allocated(i) 97 }))) 98 val lastNeedCancel = GatedValidRegNext(needCancel) 99 val enqCancel = canEnqueue.zip(io.enq.req).map{case (v , x) => 100 v && x.bits.robIdx.needFlush(io.redirect) 101 } 102 val enqCancelNum = enqCancel.zip(vLoadFlow).map{case (v, flow) => 103 Mux(v, flow, 0.U) 104 } 105 val lastEnqCancel = GatedRegNext(enqCancelNum.reduce(_ + _)) 106 val lastCycleCancelCount = PopCount(lastNeedCancel) 107 val redirectCancelCount = RegEnable(lastCycleCancelCount + lastEnqCancel, 0.U, lastCycleRedirect.valid) 108 109 // update enqueue pointer 110 val validVLoadFlow = vLoadFlow.zipWithIndex.map{case (vLoadFlowNumItem, index) => Mux(canEnqueue(index), vLoadFlowNumItem, 0.U)} 111 val validVLoadOffset = vLoadFlow.zip(io.enq.needAlloc).map{case (flow, needAllocItem) => Mux(needAllocItem, flow, 0.U)} 112 val validVLoadOffsetRShift = 0.U +: validVLoadOffset.take(validVLoadFlow.length - 1) 113 114 val enqNumber = validVLoadFlow.reduce(_ + _) 115 val enqPtrExtNextVec = Wire(Vec(io.enq.req.length, new LqPtr)) 116 val enqPtrExtNext = Wire(Vec(io.enq.req.length, new LqPtr)) 117 when (lastLastCycleRedirect.valid) { 118 // we recover the pointers in the next cycle after redirect 119 enqPtrExtNextVec := VecInit(enqPtrExt.map(_ - redirectCancelCount)) 120 } .otherwise { 121 enqPtrExtNextVec := VecInit(enqPtrExt.map(_ + enqNumber)) 122 } 123 assert(!(lastCycleRedirect.valid && enqNumber =/= 0.U)) 124 125 when (isAfter(enqPtrExtNextVec(0), deqPtrNext)) { 126 enqPtrExtNext := enqPtrExtNextVec 127 } .otherwise { 128 enqPtrExtNext := VecInit((0 until io.enq.req.length).map(i => deqPtrNext + i.U)) 129 } 130 enqPtrExt := enqPtrExtNext 131 132 // update dequeue pointer 133 val DeqPtrMoveStride = CommitWidth 134 require(DeqPtrMoveStride == CommitWidth, "DeqPtrMoveStride must be equal to CommitWidth!") 135 val deqLookupVec = VecInit((0 until DeqPtrMoveStride).map(deqPtr + _.U)) 136 val deqLookup = VecInit(deqLookupVec.map(ptr => allocated(ptr.value) && committed(ptr.value) && ptr =/= enqPtrExt(0))) 137 val deqInSameRedirectCycle = VecInit(deqLookupVec.map(ptr => needCancel(ptr.value))) 138 // make chisel happy 139 val deqCountMask = Wire(UInt(DeqPtrMoveStride.W)) 140 deqCountMask := deqLookup.asUInt & (~deqInSameRedirectCycle.asUInt).asUInt 141 val commitCount = PopCount(PriorityEncoderOH(~deqCountMask) - 1.U) 142 val lastCommitCount = GatedRegNext(commitCount) 143 144 // update deqPtr 145 // cycle 1: generate deqPtrNext 146 // cycle 2: update deqPtr 147 val deqPtrUpdateEna = lastCommitCount =/= 0.U 148 deqPtrNext := deqPtr + lastCommitCount 149 deqPtr := RegEnable(deqPtrNext, 0.U.asTypeOf(new LqPtr), deqPtrUpdateEna) 150 151 io.lqDeq := GatedRegNext(lastCommitCount) 152 io.lqCancelCnt := redirectCancelCount 153 io.ldWbPtr := deqPtr 154 io.lqEmpty := RegNext(validCount === 0.U) 155 156 /** 157 * Enqueue at dispatch 158 * 159 * Currently, VirtualLoadQueue only allows enqueue when #emptyEntries > EnqWidth 160 * Dynamic enq based on numLsElem number 161 */ 162 io.enq.canAccept := allowEnqueue 163 val enqLowBound = io.enq.req.map(_.bits.lqIdx) 164 val enqUpBound = io.enq.req.map(x => x.bits.lqIdx + x.bits.numLsElem) 165 val enqCrossLoop = enqLowBound.zip(enqUpBound).map{case (low, up) => low.flag =/= up.flag} 166 167 for(i <- 0 until VirtualLoadQueueSize) { 168 val entryCanEnqSeq = (0 until io.enq.req.length).map { j => 169 val entryHitBound = Mux( 170 enqCrossLoop(j), 171 enqLowBound(j).value <= i.U || i.U < enqUpBound(j).value, 172 enqLowBound(j).value <= i.U && i.U < enqUpBound(j).value 173 ) 174 canEnqueue(j) && !enqCancel(j) && entryHitBound 175 } 176 val entryCanEnq = entryCanEnqSeq.reduce(_ || _) 177 val selectBits = ParallelPriorityMux(entryCanEnqSeq, io.enq.req.map(_.bits)) 178 when (entryCanEnq) { 179 allocated(i) := true.B 180 robIdx(i) := selectBits.robIdx 181 uopIdx(i) := selectBits.uopIdx 182 isvec(i) := FuType.isVLoad(selectBits.fuType) 183 committed(i) := false.B 184 185 debug_mmio(i) := false.B 186 debug_paddr(i) := 0.U 187 } 188 } 189 190 for (i <- 0 until io.enq.req.length) { 191 val lqIdx = enqPtrExt(0) + validVLoadOffsetRShift.take(i + 1).reduce(_ + _) 192 val index = io.enq.req(i).bits.lqIdx 193 XSError(canEnqueue(i) && !enqCancel(i) && (!io.enq.canAccept || !io.enq.sqCanAccept), s"must accept $i\n") 194 XSError(canEnqueue(i) && !enqCancel(i) && index.value =/= lqIdx.value, s"must be the same entry $i\n") 195 io.enq.resp(i) := lqIdx 196 } 197 198 /** 199 * Load commits 200 * 201 * When load commited, mark it as !allocated and move deqPtr forward. 202 */ 203 (0 until DeqPtrMoveStride).map(i => { 204 when (commitCount > i.U) { 205 allocated((deqPtr+i.U).value) := false.B 206 } 207 XSError(commitCount > i.U && !allocated((deqPtr+i.U).value), s"why commit invalid entry $i?\n") 208 }) 209 210 // vector commit or replay 211 val vecLdCommittmp = Wire(Vec(VirtualLoadQueueSize, Vec(VecLoadPipelineWidth, Bool()))) 212 val vecLdCommit = Wire(Vec(VirtualLoadQueueSize, Bool())) 213 for (i <- 0 until VirtualLoadQueueSize) { 214 val cmt = io.vecCommit 215 for (j <- 0 until VecLoadPipelineWidth) { 216 vecLdCommittmp(i)(j) := allocated(i) && cmt(j).valid && robIdx(i) === cmt(j).bits.robidx && uopIdx(i) === cmt(j).bits.uopidx 217 } 218 vecLdCommit(i) := vecLdCommittmp(i).reduce(_ || _) 219 220 when (vecLdCommit(i) && isvec(i)) { 221 committed(i) := true.B 222 } 223 } 224 225 // misprediction recovery / exception redirect 226 // invalidate lq term using robIdx 227 for (i <- 0 until VirtualLoadQueueSize) { 228 when (needCancel(i)) { 229 allocated(i) := false.B 230 } 231 } 232 233 XSDebug(p"(ready, valid): ${io.enq.canAccept}, ${Binary(Cat(io.enq.req.map(_.valid)))}\n") 234 235 /** 236 * Writeback load from load units 237 * 238 * Most load instructions writeback to regfile at the same time. 239 * However, 240 * (1) For ready load instruction (no need replay), it writes back to ROB immediately. 241 */ 242 for(i <- 0 until LoadPipelineWidth) { 243 // most lq status need to be updated immediately after load writeback to lq 244 // flag bits in lq needs to be updated accurately 245 io.ldin(i).ready := true.B 246 val loadWbIndex = io.ldin(i).bits.uop.lqIdx.value 247 248 when (io.ldin(i).valid) { 249 val hasExceptions = ExceptionNO.selectByFu(io.ldin(i).bits.uop.exceptionVec, LduCfg).asUInt.orR 250 val need_rep = io.ldin(i).bits.rep_info.need_rep 251 val need_valid = io.ldin(i).bits.updateAddrValid 252 253 when (!need_rep && need_valid && !io.ldin(i).bits.isvec) { 254 committed(loadWbIndex) := true.B 255 256 // Debug info 257 debug_mmio(loadWbIndex) := io.ldin(i).bits.mmio 258 debug_paddr(loadWbIndex) := io.ldin(i).bits.paddr 259 } 260 261 XSInfo(!need_rep && need_valid, 262 "load hit write to lq idx %d pc 0x%x vaddr %x paddr %x mask %x forwardData %x forwardMask: %x mmio %x isvec %x\n", 263 io.ldin(i).bits.uop.lqIdx.asUInt, 264 io.ldin(i).bits.uop.pc, 265 io.ldin(i).bits.vaddr, 266 io.ldin(i).bits.paddr, 267 io.ldin(i).bits.mask, 268 io.ldin(i).bits.forwardData.asUInt, 269 io.ldin(i).bits.forwardMask.asUInt, 270 io.ldin(i).bits.mmio, 271 io.ldin(i).bits.isvec 272 ) 273 } 274 } 275 276 // perf counter 277 QueuePerf(VirtualLoadQueueSize, validCount, !allowEnqueue) 278 val vecValidVec = WireInit(VecInit((0 until VirtualLoadQueueSize).map(i => allocated(i) && isvec(i)))) 279 QueuePerf(VirtualLoadQueueSize, PopCount(vecValidVec), !allowEnqueue) 280 io.lqFull := !allowEnqueue 281 282 def NLoadNotCompleted = 1 283 val validCountReg = RegNext(validCount) 284 val noUopsIssued = io.noUopsIssued 285 val stallLoad = io.noUopsIssued && (validCountReg >= NLoadNotCompleted.U) 286 val memStallAnyLoad = RegNext(stallLoad) 287 288 XSPerfAccumulate("mem_stall_anyload", memStallAnyLoad) 289 290 val perfEvents: Seq[(String, UInt)] = Seq( 291 ("MEMSTALL_ANY_LOAD", memStallAnyLoad), 292 ) 293 generatePerfEvent() 294 295 // debug info 296 XSDebug("enqPtrExt %d:%d deqPtrExt %d:%d\n", enqPtrExt(0).flag, enqPtr, deqPtr.flag, deqPtr.value) 297 298 def PrintFlag(flag: Bool, name: String): Unit = { 299 XSDebug(false, flag, name) // when(flag) 300 XSDebug(false, !flag, " ") // otherwise 301 } 302 303 for (i <- 0 until VirtualLoadQueueSize) { 304 PrintFlag(allocated(i), "a") 305 PrintFlag(allocated(i) && committed(i), "c") 306 PrintFlag(allocated(i) && isvec(i), "v") 307 XSDebug(false, true.B, "\n") 308 } 309 // end 310} 311