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 = 0.U +: vLoadFlow.zip(io.enq.needAlloc) 105 .map{case (flow, needAlloc_Item) => Mux(needAlloc_Item, flow, 0.U)} 106 .slice(0, validVLoadFlow.length - 1) 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 offset = PopCount(io.enq.needAlloc.take(i)) 159 val lqIdx = enqPtrExt(0) + validVLoadOffset.take(i + 1).reduce(_ + _) 160// val lqIdx = 0.U.asTypeOf(new LqPtr) 161 val index = io.enq.req(i).bits.lqIdx.value 162 val enqInstr = io.enq.req(i).bits.instr.asTypeOf(new XSInstBitFields) 163 when (canEnqueue(i) && !enqCancel(i)) { 164 allocated(index) := true.B 165 uop(index) := io.enq.req(i).bits 166 uop(index).lqIdx := lqIdx 167 168 // init 169 addrvalid(index) := false.B 170 datavalid(index) := false.B 171 isvec(index) := enqInstr.isVecLoad 172 veccommitted(index) := false.B 173 174 debug_mmio(index) := false.B 175 debug_paddr(index) := 0.U 176 177 XSError(!io.enq.canAccept || !io.enq.sqCanAccept, s"must accept $i\n") 178 XSError(index =/= lqIdx.value, s"must be the same entry $i\n") 179 } 180 io.enq.resp(i) := lqIdx 181 } 182 183 /** 184 * Load commits 185 * 186 * When load commited, mark it as !allocated and move deqPtr forward. 187 */ 188 (0 until DeqPtrMoveStride).map(i => { 189 when (commitCount > i.U) { 190 allocated((deqPtr+i.U).value) := false.B 191 XSError(!allocated((deqPtr+i.U).value), s"why commit invalid entry $i?\n") 192 } 193 }) 194 195 // vector commit or replay 196 val vecLdCommit = Wire(Vec(VirtualLoadQueueSize, Bool())) 197 for (i <- 0 until VirtualLoadQueueSize) { 198 vecLdCommit(i) := io.vecCommit.valid && io.vecCommit.bits.isCommit && uop(i).robIdx === io.vecCommit.bits.robidx && uop(i).uopIdx === io.vecCommit.bits.uopidx 199 when (vecLdCommit(i)) { 200 veccommitted(i) := true.B 201 } 202 } 203 204 // misprediction recovery / exception redirect 205 // invalidate lq term using robIdx 206 for (i <- 0 until VirtualLoadQueueSize) { 207 when (needCancel(i)) { 208 allocated(i) := false.B 209 } 210 } 211 212 XSDebug(p"(ready, valid): ${io.enq.canAccept}, ${Binary(Cat(io.enq.req.map(_.valid)))}\n") 213 214 /** 215 * Writeback load from load units 216 * 217 * Most load instructions writeback to regfile at the same time. 218 * However, 219 * (1) For ready load instruction (no need replay), it writes back to ROB immediately. 220 */ 221 for(i <- 0 until LoadPipelineWidth) { 222 // most lq status need to be updated immediately after load writeback to lq 223 // flag bits in lq needs to be updated accurately 224 io.ldin(i).ready := true.B 225 val loadWbIndex = io.ldin(i).bits.uop.lqIdx.value 226 227 when (io.ldin(i).valid) { 228 val hasExceptions = ExceptionNO.selectByFu(io.ldin(i).bits.uop.exceptionVec, LduCfg).asUInt.orR 229 val need_rep = io.ldin(i).bits.rep_info.need_rep 230 231 when (!need_rep) { 232 // update control flag 233 addrvalid(loadWbIndex) := hasExceptions || !io.ldin(i).bits.tlbMiss 234 datavalid(loadWbIndex) := 235 (if (EnableFastForward) { 236 hasExceptions || 237 io.ldin(i).bits.mmio || 238 !io.ldin(i).bits.miss && // dcache miss 239 !io.ldin(i).bits.dcacheRequireReplay // do not writeback if that inst will be resend from rs 240 } else { 241 hasExceptions || 242 io.ldin(i).bits.mmio || 243 !io.ldin(i).bits.miss 244 }) 245 246 // 247 when (io.ldin(i).bits.data_wen_dup(1)) { 248 uop(loadWbIndex) := io.ldin(i).bits.uop 249 } 250 when (io.ldin(i).bits.data_wen_dup(4)) { 251 uop(loadWbIndex).debugInfo := io.ldin(i).bits.uop.debugInfo 252 } 253 uop(loadWbIndex).debugInfo := io.ldin(i).bits.rep_info.debug 254 255 // Debug info 256 debug_mmio(loadWbIndex) := io.ldin(i).bits.mmio 257 debug_paddr(loadWbIndex) := io.ldin(i).bits.paddr 258 259 when (io.ldin(i).bits.usSecondInv) { 260 uop(loadWbIndex + 1.U).robIdx := uop(loadWbIndex).robIdx 261 uop(loadWbIndex + 1.U).uopIdx := uop(loadWbIndex).uopIdx 262 } 263 264 XSInfo(io.ldin(i).valid, 265 "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", 266 io.ldin(i).bits.uop.lqIdx.asUInt, 267 io.ldin(i).bits.uop.pc, 268 io.ldin(i).bits.vaddr, 269 io.ldin(i).bits.paddr, 270 io.ldin(i).bits.mask, 271 io.ldin(i).bits.forwardData.asUInt, 272 io.ldin(i).bits.forwardMask.asUInt, 273 io.ldin(i).bits.mmio, 274 io.ldin(i).bits.isvec, 275 io.ldin(i).bits.usSecondInv 276 ) 277 } 278 } 279 } 280 281 // perf counter 282 QueuePerf(VirtualLoadQueueSize, validCount, !allowEnqueue) 283 io.lqFull := !allowEnqueue 284 val perfEvents: Seq[(String, UInt)] = Seq() 285 generatePerfEvent() 286 287 // debug info 288 XSDebug("enqPtrExt %d:%d deqPtrExt %d:%d\n", enqPtrExt(0).flag, enqPtr, deqPtr.flag, deqPtr.value) 289 290 def PrintFlag(flag: Bool, name: String): Unit = { 291 when(flag) { 292 XSDebug(false, true.B, name) 293 }.otherwise { 294 XSDebug(false, true.B, " ") 295 } 296 } 297 298 for (i <- 0 until VirtualLoadQueueSize) { 299 XSDebug(i + " pc %x pa %x ", uop(i).pc, debug_paddr(i)) 300 PrintFlag(allocated(i), "v") 301 PrintFlag(allocated(i) && datavalid(i), "d") 302 PrintFlag(allocated(i) && addrvalid(i), "a") 303 PrintFlag(allocated(i) && addrvalid(i) && datavalid(i), "w") 304 PrintFlag(allocated(i) && isvec(i), "c") 305 XSDebug(false, true.B, "\n") 306 } 307 // end 308} 309