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.{RobLsqIO, RobPtr} 23import xiangshan.ExceptionNO._ 24import xiangshan.cache._ 25import utils._ 26import utility._ 27import xiangshan.backend.Bundles.{DynInst, MemExuOutput} 28import xiangshan.backend.fu.FuConfig.LduCfg 29import xiangshan.backend.decode.isa.bitfield.{InstVType, XSInstBitFields} 30 31class VirtualLoadQueue(implicit p: Parameters) extends XSModule 32 with HasDCacheParameters 33 with HasCircularQueuePtrHelper 34 with HasLoadHelper 35 with HasPerfEvents 36 with HasVLSUParameters { 37 val io = IO(new Bundle() { 38 // control 39 val redirect = Flipped(Valid(new Redirect)) 40 val vecCommit = Flipped(ValidIO(new FeedbackToLsqIO)) 41 // from dispatch 42 val enq = new LqEnqIO 43 // from ldu s3 44 val ldin = Vec(LoadPipelineWidth, Flipped(DecoupledIO(new LqWriteBundle))) 45 // to LoadQueueReplay and LoadQueueRAR 46 val ldWbPtr = Output(new LqPtr) 47 // global 48 val lqFull = Output(Bool()) 49 val lqEmpty = Output(Bool()) 50 // to dispatch 51 val lqDeq = Output(UInt(log2Up(CommitWidth + 1).W)) 52 val lqCancelCnt = Output(UInt(log2Up(VirtualLoadQueueSize+1).W)) 53 }) 54 55 println("VirtualLoadQueue: size: " + VirtualLoadQueueSize) 56 // VirtualLoadQueue field 57 // +-----------+---------+-------+ 58 // | Allocated | MicroOp | Flags | 59 // +-----------+---------+-------+ 60 // Allocated : entry has been allocated already 61 // MicroOp : inst's microOp 62 // Flags : load flags 63 val allocated = RegInit(VecInit(List.fill(VirtualLoadQueueSize)(false.B))) // The control signals need to explicitly indicate the initial value 64 val uop = Reg(Vec(VirtualLoadQueueSize, new DynInst)) 65 val addrvalid = RegInit(VecInit(List.fill(VirtualLoadQueueSize)(false.B))) // non-mmio addr is valid 66 val datavalid = RegInit(VecInit(List.fill(VirtualLoadQueueSize)(false.B))) // non-mmio data is valid 67 // vector load: inst -> uop (pdest registor) -> flow (once load operation in loadunit) 68 val isvec = RegInit(VecInit(List.fill(VirtualLoadQueueSize)(false.B))) // vector load flow 69 val veccommitted = RegInit(VecInit(List.fill(VirtualLoadQueueSize)(false.B))) // vector load uop has commited 70 71 /** 72 * used for debug 73 */ 74 val debug_mmio = Reg(Vec(VirtualLoadQueueSize, Bool())) // mmio: inst is an mmio inst 75 val debug_paddr = Reg(Vec(VirtualLoadQueueSize, UInt(PAddrBits.W))) // mmio: inst's paddr 76 77 // maintain pointers 78 val enqPtrExt = RegInit(VecInit((0 until io.enq.req.length).map(_.U.asTypeOf(new LqPtr)))) 79 val enqPtr = enqPtrExt(0).value 80 val deqPtr = Wire(new LqPtr) 81 val deqPtrNext = Wire(new LqPtr) 82 83 /** 84 * update pointer 85 */ 86 val lastCycleRedirect = RegNext(io.redirect) 87 val lastLastCycleRedirect = RegNext(lastCycleRedirect) 88 89 val validCount = distanceBetween(enqPtrExt(0), deqPtr) 90 val allowEnqueue = validCount <= (VirtualLoadQueueSize - LSQLdEnqWidth).U 91 val canEnqueue = io.enq.req.map(_.valid) 92 val needCancel = WireInit(VecInit((0 until VirtualLoadQueueSize).map(i => { 93 uop(i).robIdx.needFlush(io.redirect) && allocated(i) 94 }))) 95 val lastNeedCancel = RegNext(needCancel) 96 val enqCancel = io.enq.req.map(_.bits.robIdx.needFlush(io.redirect)) 97 val lastEnqCancel = PopCount(RegNext(VecInit(canEnqueue.zip(enqCancel).map(x => x._1 && x._2)))) 98 val lastCycleCancelCount = PopCount(lastNeedCancel) 99 val redirectCancelCount = RegEnable(lastCycleCancelCount + lastEnqCancel, 0.U, lastCycleRedirect.valid) 100 101 // update enqueue pointer 102 val vLoadFlow = io.enq.req.map(_.bits.numLsElem) 103 val validVLoadFlow = vLoadFlow.zipWithIndex.map{case (vLoadFlowNum_Item, index) => Mux(io.enq.canAccept && io.enq.sqCanAccept && canEnqueue(index), vLoadFlowNum_Item, 0.U)} 104 val validVLoadOffset = vLoadFlow.zip(io.enq.needAlloc).map{case (flow, needAlloc_Item) => Mux(needAlloc_Item, flow, 0.U)} 105 val validVLoadOffsetRShift = 0.U +: validVLoadOffset.take(validVLoadFlow.length - 1) 106 107 val enqNumber = validVLoadFlow.reduce(_ + _) 108 val enqPtrExtNextVec = Wire(Vec(io.enq.req.length, new LqPtr)) 109 val enqPtrExtNext = Wire(Vec(io.enq.req.length, new LqPtr)) 110 when (lastLastCycleRedirect.valid) { 111 // we recover the pointers in the next cycle after redirect 112 enqPtrExtNextVec := VecInit(enqPtrExt.map(_ - redirectCancelCount)) 113 } .otherwise { 114 enqPtrExtNextVec := VecInit(enqPtrExt.map(_ + enqNumber)) 115 } 116 assert(!(lastCycleRedirect.valid && enqNumber =/= 0.U)) 117 118 when (isAfter(enqPtrExtNextVec(0), deqPtrNext)) { 119 enqPtrExtNext := enqPtrExtNextVec 120 } .otherwise { 121 enqPtrExtNext := VecInit((0 until io.enq.req.length).map(i => deqPtrNext + i.U)) 122 } 123 enqPtrExt := enqPtrExtNext 124 125 // update dequeue pointer 126 val DeqPtrMoveStride = CommitWidth 127 require(DeqPtrMoveStride == CommitWidth, "DeqPtrMoveStride must be equal to CommitWidth!") 128 val deqLookupVec = VecInit((0 until DeqPtrMoveStride).map(deqPtr + _.U)) 129 val deqLookup = VecInit(deqLookupVec.map(ptr => allocated(ptr.value) 130 && ((datavalid(ptr.value) && addrvalid(ptr.value) && !isvec(ptr.value)) || (isvec(ptr.value) && veccommitted(ptr.value))) 131 && ptr =/= enqPtrExt(0))) 132 val deqInSameRedirectCycle = VecInit(deqLookupVec.map(ptr => needCancel(ptr.value))) 133 // make chisel happy 134 val deqCountMask = Wire(UInt(DeqPtrMoveStride.W)) 135 deqCountMask := deqLookup.asUInt & (~deqInSameRedirectCycle.asUInt).asUInt 136 val commitCount = PopCount(PriorityEncoderOH(~deqCountMask) - 1.U) 137 val lastCommitCount = RegNext(commitCount) 138 139 // update deqPtr 140 // cycle 1: generate deqPtrNext 141 // cycle 2: update deqPtr 142 val deqPtrUpdateEna = lastCommitCount =/= 0.U 143 deqPtrNext := deqPtr + lastCommitCount 144 deqPtr := RegEnable(deqPtrNext, 0.U.asTypeOf(new LqPtr), deqPtrUpdateEna) 145 146 io.lqDeq := RegNext(lastCommitCount) 147 io.lqCancelCnt := redirectCancelCount 148 io.ldWbPtr := deqPtr 149 io.lqEmpty := RegNext(validCount === 0.U) 150 151 /** 152 * Enqueue at dispatch 153 * 154 * Currently, VirtualLoadQueue only allows enqueue when #emptyEntries > EnqWidth 155 */ 156 io.enq.canAccept := allowEnqueue 157 for (i <- 0 until io.enq.req.length) { 158 val lqIdx = enqPtrExt(0) + validVLoadOffsetRShift.take(i + 1).reduce(_ + _) 159 val index = io.enq.req(i).bits.lqIdx 160 val enqInstr = io.enq.req(i).bits.instr.asTypeOf(new XSInstBitFields) 161 when (canEnqueue(i) && !enqCancel(i)) { 162 for (j <- 0 until VecMemDispatchMaxNumber) { 163 when (j.U < validVLoadOffset(i)) { 164 allocated((index + j.U).value) := true.B 165 uop((index + j.U).value) := io.enq.req(i).bits 166 uop((index + j.U).value).lqIdx := lqIdx + j.U 167 168 // init 169 addrvalid((index + j.U).value) := false.B 170 datavalid((index + j.U).value) := false.B 171 isvec((index + j.U).value) := enqInstr.isVecLoad 172 veccommitted((index + j.U).value) := false.B 173 174 debug_mmio((index + j.U).value) := false.B 175 debug_paddr((index + j.U).value) := 0.U 176 177 XSError(!io.enq.canAccept || !io.enq.sqCanAccept, s"must accept $i\n") 178 XSError(index.value =/= lqIdx.value, s"must be the same entry $i\n") 179 } 180 } 181 } 182 io.enq.resp(i) := lqIdx 183 } 184 185 /** 186 * Load commits 187 * 188 * When load commited, mark it as !allocated and move deqPtr forward. 189 */ 190 (0 until DeqPtrMoveStride).map(i => { 191 when (commitCount > i.U) { 192 allocated((deqPtr+i.U).value) := false.B 193 XSError(!allocated((deqPtr+i.U).value), s"why commit invalid entry $i?\n") 194 } 195 }) 196 197 // vector commit or replay 198 val vecLdCommit = Wire(Vec(VirtualLoadQueueSize, Bool())) 199 for (i <- 0 until VirtualLoadQueueSize) { 200 vecLdCommit(i) := io.vecCommit.valid && io.vecCommit.bits.isCommit && uop(i).robIdx === io.vecCommit.bits.robidx && uop(i).uopIdx === io.vecCommit.bits.uopidx 201 when (vecLdCommit(i)) { 202 veccommitted(i) := true.B 203 } 204 } 205 206 // misprediction recovery / exception redirect 207 // invalidate lq term using robIdx 208 for (i <- 0 until VirtualLoadQueueSize) { 209 when (needCancel(i)) { 210 allocated(i) := false.B 211 } 212 } 213 214 XSDebug(p"(ready, valid): ${io.enq.canAccept}, ${Binary(Cat(io.enq.req.map(_.valid)))}\n") 215 216 /** 217 * Writeback load from load units 218 * 219 * Most load instructions writeback to regfile at the same time. 220 * However, 221 * (1) For ready load instruction (no need replay), it writes back to ROB immediately. 222 */ 223 for(i <- 0 until LoadPipelineWidth) { 224 // most lq status need to be updated immediately after load writeback to lq 225 // flag bits in lq needs to be updated accurately 226 io.ldin(i).ready := true.B 227 val loadWbIndex = io.ldin(i).bits.uop.lqIdx.value 228 229 when (io.ldin(i).valid) { 230 val hasExceptions = ExceptionNO.selectByFu(io.ldin(i).bits.uop.exceptionVec, LduCfg).asUInt.orR 231 val need_rep = io.ldin(i).bits.rep_info.need_rep 232 233 when (!need_rep) { 234 // update control flag 235 addrvalid(loadWbIndex) := hasExceptions || !io.ldin(i).bits.tlbMiss 236 datavalid(loadWbIndex) := 237 (if (EnableFastForward) { 238 hasExceptions || 239 io.ldin(i).bits.mmio || 240 !io.ldin(i).bits.miss && // dcache miss 241 !io.ldin(i).bits.dcacheRequireReplay // do not writeback if that inst will be resend from rs 242 } else { 243 hasExceptions || 244 io.ldin(i).bits.mmio || 245 !io.ldin(i).bits.miss 246 }) 247 248 // 249 when (io.ldin(i).bits.data_wen_dup(1)) { 250 uop(loadWbIndex) := io.ldin(i).bits.uop 251 } 252 when (io.ldin(i).bits.data_wen_dup(4)) { 253 uop(loadWbIndex).debugInfo := io.ldin(i).bits.uop.debugInfo 254 } 255 uop(loadWbIndex).debugInfo := io.ldin(i).bits.rep_info.debug 256 257 // Debug info 258 debug_mmio(loadWbIndex) := io.ldin(i).bits.mmio 259 debug_paddr(loadWbIndex) := io.ldin(i).bits.paddr 260 261 when (io.ldin(i).bits.usSecondInv) { 262 uop(loadWbIndex + 1.U).robIdx := uop(loadWbIndex).robIdx 263 uop(loadWbIndex + 1.U).uopIdx := uop(loadWbIndex).uopIdx 264 } 265 266 XSInfo(io.ldin(i).valid, 267 "load hit write to lq idx %d pc 0x%x vaddr %x paddr %x mask %x forwardData %x forwardMask: %x mmio %x isvec %x vec_secondInv %x\n", 268 io.ldin(i).bits.uop.lqIdx.asUInt, 269 io.ldin(i).bits.uop.pc, 270 io.ldin(i).bits.vaddr, 271 io.ldin(i).bits.paddr, 272 io.ldin(i).bits.mask, 273 io.ldin(i).bits.forwardData.asUInt, 274 io.ldin(i).bits.forwardMask.asUInt, 275 io.ldin(i).bits.mmio, 276 io.ldin(i).bits.isvec, 277 io.ldin(i).bits.usSecondInv 278 ) 279 } 280 } 281 } 282 283 // perf counter 284 QueuePerf(VirtualLoadQueueSize, validCount, !allowEnqueue) 285 io.lqFull := !allowEnqueue 286 val perfEvents: Seq[(String, UInt)] = Seq() 287 generatePerfEvent() 288 289 // debug info 290 XSDebug("enqPtrExt %d:%d deqPtrExt %d:%d\n", enqPtrExt(0).flag, enqPtr, deqPtr.flag, deqPtr.value) 291 292 def PrintFlag(flag: Bool, name: String): Unit = { 293 when(flag) { 294 XSDebug(false, true.B, name) 295 }.otherwise { 296 XSDebug(false, true.B, " ") 297 } 298 } 299 300 for (i <- 0 until VirtualLoadQueueSize) { 301 XSDebug(i + " pc %x pa %x ", uop(i).pc, debug_paddr(i)) 302 PrintFlag(allocated(i), "v") 303 PrintFlag(allocated(i) && datavalid(i), "d") 304 PrintFlag(allocated(i) && addrvalid(i), "a") 305 PrintFlag(allocated(i) && addrvalid(i) && datavalid(i), "w") 306 PrintFlag(allocated(i) && isvec(i), "c") 307 XSDebug(false, true.B, "\n") 308 } 309 // end 310} 311