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