1package xiangshan.mem 2 3import chisel3._ 4import chisel3.util._ 5import utils._ 6import xiangshan._ 7import xiangshan.cache._ 8import xiangshan.cache.{DCacheWordIO, DCacheLineIO, TlbRequestIO, MemoryOpConstants} 9import xiangshan.backend.LSUOpType 10import xiangshan.backend.roq.RoqPtr 11 12 13class SqPtr extends CircularQueuePtr(SqPtr.StoreQueueSize) { } 14 15object SqPtr extends HasXSParameter { 16 def apply(f: Bool, v: UInt): SqPtr = { 17 val ptr = Wire(new SqPtr) 18 ptr.flag := f 19 ptr.value := v 20 ptr 21 } 22} 23 24class SqEnqIO extends XSBundle { 25 val canAccept = Output(Bool()) 26 val needAlloc = Vec(RenameWidth, Input(Bool())) 27 val req = Vec(RenameWidth, Flipped(ValidIO(new MicroOp))) 28 val resp = Vec(RenameWidth, Output(new SqPtr)) 29} 30 31// Store Queue 32class StoreQueue extends XSModule with HasDCacheParameters with HasCircularQueuePtrHelper { 33 val io = IO(new Bundle() { 34 val enq = new SqEnqIO 35 val brqRedirect = Input(Valid(new Redirect)) 36 val storeIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) 37 val sbuffer = Vec(StorePipelineWidth, Decoupled(new DCacheWordReq)) 38 val mmioStout = DecoupledIO(new ExuOutput) // writeback uncached store 39 val forward = Vec(LoadPipelineWidth, Flipped(new LoadForwardQueryIO)) 40 val commits = Flipped(new RoqCommitIO) 41 val uncache = new DCacheWordIO 42 val roqDeqPtr = Input(new RoqPtr) 43 // val refill = Flipped(Valid(new DCacheLineReq )) 44 val exceptionAddr = new ExceptionAddrIO 45 }) 46 47 val uop = Reg(Vec(StoreQueueSize, new MicroOp)) 48 // val data = Reg(Vec(StoreQueueSize, new LsqEntry)) 49 val dataModule = Module(new LSQueueData(StoreQueueSize, StorePipelineWidth)) 50 dataModule.io := DontCare 51 val allocated = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // sq entry has been allocated 52 val datavalid = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // non-mmio data is valid 53 val writebacked = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // inst has been writebacked to CDB 54 val commited = Reg(Vec(StoreQueueSize, Bool())) // inst has been commited by roq 55 val pending = Reg(Vec(StoreQueueSize, Bool())) // mmio pending: inst is an mmio inst, it will not be executed until it reachs the end of roq 56 57 require(StoreQueueSize > RenameWidth) 58 val enqPtrExt = RegInit(VecInit((0 until RenameWidth).map(_.U.asTypeOf(new SqPtr)))) 59 val deqPtrExt = RegInit(VecInit((0 until StorePipelineWidth).map(_.U.asTypeOf(new SqPtr)))) 60 val enqPtr = enqPtrExt(0).value 61 val deqPtr = deqPtrExt(0).value 62 63 val tailMask = UIntToMask(deqPtr, StoreQueueSize) 64 val headMask = UIntToMask(enqPtr, StoreQueueSize) 65 66 /** 67 * Enqueue at dispatch 68 * 69 * Currently, StoreQueue only allows enqueue when #emptyEntries > RenameWidth(EnqWidth) 70 */ 71 val validEntries = distanceBetween(enqPtrExt(0), deqPtrExt(0)) 72 val firedDispatch = io.enq.req.map(_.valid) 73 io.enq.canAccept := validEntries <= (StoreQueueSize - RenameWidth).U 74 XSDebug(p"(ready, valid): ${io.enq.canAccept}, ${Binary(Cat(firedDispatch))}\n") 75 for (i <- 0 until RenameWidth) { 76 val offset = if (i == 0) 0.U else PopCount(io.enq.needAlloc.take(i)) 77 val sqIdx = enqPtrExt(offset) 78 val index = sqIdx.value 79 when (io.enq.req(i).valid && io.enq.canAccept && !io.brqRedirect.valid) { 80 uop(index) := io.enq.req(i).bits 81 allocated(index) := true.B 82 datavalid(index) := false.B 83 writebacked(index) := false.B 84 commited(index) := false.B 85 pending(index) := false.B 86 } 87 io.enq.resp(i) := sqIdx 88 } 89 90 when (Cat(firedDispatch).orR && io.enq.canAccept && !io.brqRedirect.valid) { 91 val enqNumber = PopCount(firedDispatch) 92 enqPtrExt := VecInit(enqPtrExt.map(_ + enqNumber)) 93 XSInfo("dispatched %d insts to sq\n", enqNumber) 94 } 95 96 /** 97 * Writeback store from store units 98 * 99 * Most store instructions writeback to regfile in the previous cycle. 100 * However, 101 * (1) For an mmio instruction with exceptions, we need to mark it as datavalid 102 * (in this way it will trigger an exception when it reaches ROB's head) 103 * instead of pending to avoid sending them to lower level. 104 * (2) For an mmio instruction without exceptions, we mark it as pending. 105 * When the instruction reaches ROB's head, StoreQueue sends it to uncache channel. 106 * Upon receiving the response, StoreQueue writes back the instruction 107 * through arbiter with store units. It will later commit as normal. 108 */ 109 for (i <- 0 until StorePipelineWidth) { 110 dataModule.io.wb(i).wen := false.B 111 when(io.storeIn(i).fire()) { 112 val stWbIndex = io.storeIn(i).bits.uop.sqIdx.value 113 val hasException = io.storeIn(i).bits.uop.cf.exceptionVec.asUInt.orR 114 val hasWritebacked = !io.storeIn(i).bits.mmio || hasException 115 datavalid(stWbIndex) := hasWritebacked 116 writebacked(stWbIndex) := hasWritebacked 117 pending(stWbIndex) := !hasWritebacked // valid mmio require 118 119 val storeWbData = Wire(new LsqEntry) 120 storeWbData := DontCare 121 storeWbData.paddr := io.storeIn(i).bits.paddr 122 storeWbData.vaddr := io.storeIn(i).bits.vaddr 123 storeWbData.mask := io.storeIn(i).bits.mask 124 storeWbData.data := io.storeIn(i).bits.data 125 storeWbData.mmio := io.storeIn(i).bits.mmio 126 storeWbData.exception := io.storeIn(i).bits.uop.cf.exceptionVec.asUInt 127 128 dataModule.io.wbWrite(i, stWbIndex, storeWbData) 129 dataModule.io.wb(i).wen := true.B 130 131 XSInfo("store write to sq idx %d pc 0x%x vaddr %x paddr %x data %x mmio %x roll %x exc %x\n", 132 io.storeIn(i).bits.uop.sqIdx.value, 133 io.storeIn(i).bits.uop.cf.pc, 134 io.storeIn(i).bits.vaddr, 135 io.storeIn(i).bits.paddr, 136 io.storeIn(i).bits.data, 137 io.storeIn(i).bits.mmio, 138 io.storeIn(i).bits.rollback, 139 io.storeIn(i).bits.uop.cf.exceptionVec.asUInt 140 ) 141 } 142 } 143 144 /** 145 * load forward query 146 * 147 * Check store queue for instructions that is older than the load. 148 * The response will be valid at the next cycle after req. 149 */ 150 // check over all lq entries and forward data from the first matched store 151 for (i <- 0 until LoadPipelineWidth) { 152 io.forward(i).forwardMask := 0.U(8.W).asBools 153 io.forward(i).forwardData := DontCare 154 155 // Compare deqPtr (deqPtr) and forward.sqIdx, we have two cases: 156 // (1) if they have the same flag, we need to check range(tail, sqIdx) 157 // (2) if they have different flags, we need to check range(tail, LoadQueueSize) and range(0, sqIdx) 158 // Forward1: Mux(same_flag, range(tail, sqIdx), range(tail, LoadQueueSize)) 159 // Forward2: Mux(same_flag, 0.U, range(0, sqIdx) ) 160 // i.e. forward1 is the target entries with the same flag bits and forward2 otherwise 161 val differentFlag = deqPtrExt(0).flag =/= io.forward(i).sqIdx.flag 162 val forwardMask = UIntToMask(io.forward(i).sqIdx.value, StoreQueueSize) 163 val storeWritebackedVec = WireInit(VecInit(Seq.fill(StoreQueueSize)(false.B))) 164 for (j <- 0 until StoreQueueSize) { 165 storeWritebackedVec(j) := datavalid(j) && allocated(j) // all datavalid terms need to be checked 166 } 167 val needForward1 = Mux(differentFlag, ~tailMask, tailMask ^ forwardMask) & storeWritebackedVec.asUInt 168 val needForward2 = Mux(differentFlag, forwardMask, 0.U(StoreQueueSize.W)) & storeWritebackedVec.asUInt 169 170 XSDebug(p"$i f1 ${Binary(needForward1)} f2 ${Binary(needForward2)} " + 171 p"sqIdx ${io.forward(i).sqIdx} pa ${Hexadecimal(io.forward(i).paddr)}\n" 172 ) 173 174 // do real fwd query 175 dataModule.io.forwardQuery( 176 channel = i, 177 paddr = io.forward(i).paddr, 178 needForward1 = needForward1, 179 needForward2 = needForward2 180 ) 181 182 io.forward(i).forwardMask := dataModule.io.forward(i).forwardMask 183 io.forward(i).forwardData := dataModule.io.forward(i).forwardData 184 } 185 186 /** 187 * Memory mapped IO / other uncached operations 188 * 189 * States: 190 * (1) writeback from store units: mark as pending 191 * (2) when they reach ROB's head, they can be sent to uncache channel 192 * (3) response from uncache channel: mark as datavalid 193 * (4) writeback to ROB (and other units): mark as writebacked 194 * (5) ROB commits the instruction: same as normal instructions 195 */ 196 //(2) when they reach ROB's head, they can be sent to uncache channel 197 val commitType = io.commits.uop(0).ctrl.commitType 198 io.uncache.req.valid := pending(deqPtr) && allocated(deqPtr) && 199 commitType === CommitType.STORE && 200 io.roqDeqPtr === uop(deqPtr).roqIdx && 201 !io.commits.isWalk 202 203 io.uncache.req.bits.cmd := MemoryOpConstants.M_XWR 204 io.uncache.req.bits.addr := dataModule.io.rdata(deqPtr).paddr 205 io.uncache.req.bits.data := dataModule.io.rdata(deqPtr).data 206 io.uncache.req.bits.mask := dataModule.io.rdata(deqPtr).mask 207 208 io.uncache.req.bits.meta.id := DontCare // TODO: // FIXME 209 io.uncache.req.bits.meta.vaddr := DontCare 210 io.uncache.req.bits.meta.paddr := dataModule.io.rdata(deqPtr).paddr 211 io.uncache.req.bits.meta.uop := uop(deqPtr) 212 io.uncache.req.bits.meta.mmio := true.B 213 io.uncache.req.bits.meta.tlb_miss := false.B 214 io.uncache.req.bits.meta.mask := dataModule.io.rdata(deqPtr).mask 215 io.uncache.req.bits.meta.replay := false.B 216 217 when(io.uncache.req.fire()){ 218 pending(deqPtr) := false.B 219 220 XSDebug( 221 p"uncache req: pc ${Hexadecimal(uop(deqPtr).cf.pc)} " + 222 p"addr ${Hexadecimal(io.uncache.req.bits.addr)} " + 223 p"data ${Hexadecimal(io.uncache.req.bits.data)} " + 224 p"op ${Hexadecimal(io.uncache.req.bits.cmd)} " + 225 p"mask ${Hexadecimal(io.uncache.req.bits.mask)}\n" 226 ) 227 } 228 229 // (3) response from uncache channel: mark as datavalid 230 io.uncache.resp.ready := true.B 231 when (io.uncache.resp.fire()) { 232 datavalid(deqPtr) := true.B 233 } 234 235 // (4) writeback to ROB (and other units): mark as writebacked 236 io.mmioStout.valid := allocated(deqPtr) && datavalid(deqPtr) && !writebacked(deqPtr) 237 io.mmioStout.bits.uop := uop(deqPtr) 238 io.mmioStout.bits.uop.sqIdx := deqPtrExt(0) 239 io.mmioStout.bits.uop.cf.exceptionVec := dataModule.io.rdata(deqPtr).exception.asBools 240 io.mmioStout.bits.data := dataModule.io.rdata(deqPtr).data 241 io.mmioStout.bits.redirectValid := false.B 242 io.mmioStout.bits.redirect := DontCare 243 io.mmioStout.bits.brUpdate := DontCare 244 io.mmioStout.bits.debug.isMMIO := true.B 245 io.mmioStout.bits.fflags := DontCare 246 when (io.mmioStout.fire()) { 247 writebacked(deqPtr) := true.B 248 allocated(deqPtr) := false.B 249 deqPtrExt := VecInit(deqPtrExt.map(_ + 1.U)) 250 } 251 252 /** 253 * ROB commits store instructions (mark them as commited) 254 * 255 * (1) When store commits, mark it as commited. 256 * (2) They will not be cancelled and can be sent to lower level. 257 */ 258 for (i <- 0 until CommitWidth) { 259 val storeCommit = !io.commits.isWalk && io.commits.valid(i) && io.commits.uop(i).ctrl.commitType === CommitType.STORE 260 when (storeCommit) { 261 commited(io.commits.uop(i).sqIdx.value) := true.B 262 XSDebug("store commit %d: idx %d %x\n", i.U, io.commits.uop(i).sqIdx.value, io.commits.uop(i).cf.pc) 263 } 264 } 265 266 // Commited stores will not be cancelled and can be sent to lower level. 267 // remove retired insts from sq, add retired store to sbuffer 268 for (i <- 0 until StorePipelineWidth) { 269 val ptr = deqPtrExt(i).value 270 val mmio = dataModule.io.rdata(ptr).mmio 271 io.sbuffer(i).valid := allocated(ptr) && commited(ptr) && !mmio 272 io.sbuffer(i).bits.cmd := MemoryOpConstants.M_XWR 273 io.sbuffer(i).bits.addr := dataModule.io.rdata(ptr).paddr 274 io.sbuffer(i).bits.data := dataModule.io.rdata(ptr).data 275 io.sbuffer(i).bits.mask := dataModule.io.rdata(ptr).mask 276 io.sbuffer(i).bits.meta := DontCare 277 io.sbuffer(i).bits.meta.tlb_miss := false.B 278 io.sbuffer(i).bits.meta.uop := DontCare 279 io.sbuffer(i).bits.meta.mmio := mmio 280 io.sbuffer(i).bits.meta.mask := dataModule.io.rdata(ptr).mask 281 282 when (io.sbuffer(i).fire()) { 283 allocated(ptr) := false.B 284 XSDebug("sbuffer "+i+" fire: ptr %d\n", ptr) 285 } 286 } 287 // note that sbuffer will not accept req(1) if req(0) is not accepted. 288 when (Cat(io.sbuffer.map(_.fire())).orR) { 289 val stepForward = Mux(io.sbuffer(1).fire(), 2.U, 1.U) 290 deqPtrExt := VecInit(deqPtrExt.map(_ + stepForward)) 291 when (io.sbuffer(1).fire()) { 292 assert(io.sbuffer(0).fire()) 293 } 294 } 295 296 // Read vaddr for mem exception 297 io.exceptionAddr.vaddr := dataModule.io.rdata(io.exceptionAddr.lsIdx.sqIdx.value).vaddr 298 299 // misprediction recovery / exception redirect 300 // invalidate sq term using robIdx 301 val needCancel = Wire(Vec(StoreQueueSize, Bool())) 302 for (i <- 0 until StoreQueueSize) { 303 needCancel(i) := uop(i).roqIdx.needFlush(io.brqRedirect) && allocated(i) && !commited(i) 304 when (needCancel(i)) { 305 allocated(i) := false.B 306 } 307 } 308 // we recover the pointers in the next cycle after redirect 309 val lastCycleRedirectValid = RegNext(io.brqRedirect.valid) 310 val needCancelCount = PopCount(RegNext(needCancel)) 311 when (lastCycleRedirectValid) { 312 enqPtrExt := VecInit(enqPtrExt.map(_ - needCancelCount)) 313 } 314 315 // debug info 316 XSDebug("enqPtrExt %d:%d deqPtrExt %d:%d\n", enqPtrExt(0).flag, enqPtr, deqPtrExt(0).flag, deqPtr) 317 318 def PrintFlag(flag: Bool, name: String): Unit = { 319 when(flag) { 320 XSDebug(false, true.B, name) 321 }.otherwise { 322 XSDebug(false, true.B, " ") 323 } 324 } 325 326 for (i <- 0 until StoreQueueSize) { 327 if (i % 4 == 0) XSDebug("") 328 XSDebug(false, true.B, "%x [%x] ", uop(i).cf.pc, dataModule.io.rdata(i).paddr) 329 PrintFlag(allocated(i), "a") 330 PrintFlag(allocated(i) && datavalid(i), "v") 331 PrintFlag(allocated(i) && writebacked(i), "w") 332 PrintFlag(allocated(i) && commited(i), "c") 333 PrintFlag(allocated(i) && pending(i), "p") 334 XSDebug(false, true.B, " ") 335 if (i % 4 == 3 || i == StoreQueueSize - 1) XSDebug(false, true.B, "\n") 336 } 337 338} 339