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***************************************************************************************/ 16 17package xiangshan.mem 18 19import chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import utils._ 23import xiangshan._ 24import xiangshan.cache._ 25import xiangshan.cache.{DCacheWordIO, DCacheLineIO, MemoryOpConstants} 26import xiangshan.backend.rob.{RobLsqIO, RobPtr} 27import difftest._ 28import device.RAMHelper 29 30class SqPtr(implicit p: Parameters) extends CircularQueuePtr[SqPtr]( 31 p => p(XSCoreParamsKey).StoreQueueSize 32){ 33 override def cloneType = (new SqPtr).asInstanceOf[this.type] 34} 35 36object SqPtr { 37 def apply(f: Bool, v: UInt)(implicit p: Parameters): SqPtr = { 38 val ptr = Wire(new SqPtr) 39 ptr.flag := f 40 ptr.value := v 41 ptr 42 } 43} 44 45class SqEnqIO(implicit p: Parameters) extends XSBundle { 46 val canAccept = Output(Bool()) 47 val lqCanAccept = Input(Bool()) 48 val needAlloc = Vec(exuParameters.LsExuCnt, Input(Bool())) 49 val req = Vec(exuParameters.LsExuCnt, Flipped(ValidIO(new MicroOp))) 50 val resp = Vec(exuParameters.LsExuCnt, Output(new SqPtr)) 51} 52 53class DataBufferEntry (implicit p: Parameters) extends DCacheBundle { 54 val addr = UInt(PAddrBits.W) 55 val vaddr = UInt(VAddrBits.W) 56 val data = UInt(DataBits.W) 57 val mask = UInt((DataBits/8).W) 58 val wline = Bool() 59 val sqPtr = new SqPtr 60} 61 62// Store Queue 63class StoreQueue(implicit p: Parameters) extends XSModule with HasDCacheParameters with HasCircularQueuePtrHelper { 64 val io = IO(new Bundle() { 65 val enq = new SqEnqIO 66 val brqRedirect = Flipped(ValidIO(new Redirect)) 67 val storeIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) // store addr, data is not included 68 val storeInRe = Vec(StorePipelineWidth, Input(new LsPipelineBundle())) // store more mmio and exception 69 val storeDataIn = Vec(StorePipelineWidth, Flipped(Valid(new StoreDataBundle))) // store data, send to sq from rs 70 val sbuffer = Vec(StorePipelineWidth, Decoupled(new DCacheWordReqWithVaddr)) // write commited store to sbuffer 71 val mmioStout = DecoupledIO(new ExuOutput) // writeback uncached store 72 val forward = Vec(LoadPipelineWidth, Flipped(new PipeLoadForwardQueryIO)) 73 val rob = Flipped(new RobLsqIO) 74 val uncache = new DCacheWordIO 75 // val refill = Flipped(Valid(new DCacheLineReq )) 76 val exceptionAddr = new ExceptionAddrIO 77 val sqempty = Output(Bool()) 78 val issuePtrExt = Output(new SqPtr) // used to wake up delayed load/store 79 val sqFull = Output(Bool()) 80 }) 81 82 println("StoreQueue: size:" + StoreQueueSize) 83 84 // data modules 85 val uop = Reg(Vec(StoreQueueSize, new MicroOp)) 86 // val data = Reg(Vec(StoreQueueSize, new LsqEntry)) 87 val dataModule = Module(new SQDataModule( 88 numEntries = StoreQueueSize, 89 numRead = StorePipelineWidth, 90 numWrite = StorePipelineWidth, 91 numForward = StorePipelineWidth 92 )) 93 dataModule.io := DontCare 94 val paddrModule = Module(new SQAddrModule( 95 dataWidth = PAddrBits, 96 numEntries = StoreQueueSize, 97 numRead = StorePipelineWidth, 98 numWrite = StorePipelineWidth, 99 numForward = StorePipelineWidth 100 )) 101 paddrModule.io := DontCare 102 val vaddrModule = Module(new SQAddrModule( 103 dataWidth = VAddrBits, 104 numEntries = StoreQueueSize, 105 numRead = StorePipelineWidth + 1, // sbuffer 2 + badvaddr 1 (TODO) 106 numWrite = StorePipelineWidth, 107 numForward = StorePipelineWidth 108 )) 109 vaddrModule.io := DontCare 110 val dataBuffer = Module(new DatamoduleResultBuffer(new DataBufferEntry)) 111 val debug_paddr = Reg(Vec(StoreQueueSize, UInt((PAddrBits).W))) 112 val debug_vaddr = Reg(Vec(StoreQueueSize, UInt((VAddrBits).W))) 113 val debug_data = Reg(Vec(StoreQueueSize, UInt((XLEN).W))) 114 115 // state & misc 116 val allocated = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // sq entry has been allocated 117 val addrvalid = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // non-mmio addr is valid 118 val datavalid = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // non-mmio data is valid 119 val allvalid = VecInit((0 until StoreQueueSize).map(i => addrvalid(i) && datavalid(i))) // non-mmio data & addr is valid 120 val commited = Reg(Vec(StoreQueueSize, Bool())) // inst has been commited by rob 121 val pending = Reg(Vec(StoreQueueSize, Bool())) // mmio pending: inst is an mmio inst, it will not be executed until it reachs the end of rob 122 val mmio = Reg(Vec(StoreQueueSize, Bool())) // mmio: inst is an mmio inst 123 124 // ptr 125 val enqPtrExt = RegInit(VecInit((0 until io.enq.req.length).map(_.U.asTypeOf(new SqPtr)))) 126 val rdataPtrExt = RegInit(VecInit((0 until StorePipelineWidth).map(_.U.asTypeOf(new SqPtr)))) 127 val deqPtrExt = RegInit(VecInit((0 until StorePipelineWidth).map(_.U.asTypeOf(new SqPtr)))) 128 val cmtPtrExt = RegInit(VecInit((0 until CommitWidth).map(_.U.asTypeOf(new SqPtr)))) 129 val issuePtrExt = RegInit(0.U.asTypeOf(new SqPtr)) 130 val validCounter = RegInit(0.U(log2Ceil(LoadQueueSize + 1).W)) 131 val allowEnqueue = RegInit(true.B) 132 133 val enqPtr = enqPtrExt(0).value 134 val deqPtr = deqPtrExt(0).value 135 val cmtPtr = cmtPtrExt(0).value 136 137 val deqMask = UIntToMask(deqPtr, StoreQueueSize) 138 val enqMask = UIntToMask(enqPtr, StoreQueueSize) 139 140 val commitCount = RegNext(io.rob.scommit) 141 142 // Read dataModule 143 // rdataPtrExtNext and rdataPtrExtNext+1 entry will be read from dataModule 144 val rdataPtrExtNext = WireInit(Mux(dataBuffer.io.enq(1).fire(), 145 VecInit(rdataPtrExt.map(_ + 2.U)), 146 Mux(dataBuffer.io.enq(0).fire() || io.mmioStout.fire(), 147 VecInit(rdataPtrExt.map(_ + 1.U)), 148 rdataPtrExt 149 ) 150 )) 151 // deqPtrExtNext traces which inst is about to leave store queue 152 val deqPtrExtNext = WireInit(Mux(io.sbuffer(1).fire(), 153 VecInit(deqPtrExt.map(_ + 2.U)), 154 Mux(io.sbuffer(0).fire() || io.mmioStout.fire(), 155 VecInit(deqPtrExt.map(_ + 1.U)), 156 deqPtrExt 157 ) 158 )) 159 for (i <- 0 until StorePipelineWidth) { 160 dataModule.io.raddr(i) := rdataPtrExtNext(i).value 161 paddrModule.io.raddr(i) := rdataPtrExtNext(i).value 162 vaddrModule.io.raddr(i) := rdataPtrExtNext(i).value 163 } 164 165 // no inst will be commited 1 cycle before tval update 166 vaddrModule.io.raddr(StorePipelineWidth) := (cmtPtrExt(0) + commitCount).value 167 168 /** 169 * Enqueue at dispatch 170 * 171 * Currently, StoreQueue only allows enqueue when #emptyEntries > EnqWidth 172 */ 173 io.enq.canAccept := allowEnqueue 174 for (i <- 0 until io.enq.req.length) { 175 val offset = if (i == 0) 0.U else PopCount(io.enq.needAlloc.take(i)) 176 val sqIdx = enqPtrExt(offset) 177 val index = sqIdx.value 178 when (io.enq.req(i).valid && io.enq.canAccept && io.enq.lqCanAccept && !io.brqRedirect.valid) { 179 uop(index) := io.enq.req(i).bits 180 allocated(index) := true.B 181 datavalid(index) := false.B 182 addrvalid(index) := false.B 183 commited(index) := false.B 184 pending(index) := false.B 185 } 186 io.enq.resp(i) := sqIdx 187 } 188 XSDebug(p"(ready, valid): ${io.enq.canAccept}, ${Binary(Cat(io.enq.req.map(_.valid)))}\n") 189 190 /** 191 * Update issuePtr when issue from rs 192 */ 193 // update issuePtr 194 val IssuePtrMoveStride = 4 195 require(IssuePtrMoveStride >= 2) 196 197 val issueLookupVec = (0 until IssuePtrMoveStride).map(issuePtrExt + _.U) 198 val issueLookup = issueLookupVec.map(ptr => allocated(ptr.value) && addrvalid(ptr.value) && datavalid(ptr.value) && ptr =/= enqPtrExt(0)) 199 val nextIssuePtr = issuePtrExt + PriorityEncoder(VecInit(issueLookup.map(!_) :+ true.B)) 200 issuePtrExt := nextIssuePtr 201 202 when (io.brqRedirect.valid) { 203 issuePtrExt := Mux( 204 isAfter(cmtPtrExt(0), deqPtrExt(0)), 205 cmtPtrExt(0), 206 deqPtrExtNext(0) // for mmio insts, deqPtr may be ahead of cmtPtr 207 ) 208 } 209 // send issuePtrExt to rs 210 // io.issuePtrExt := cmtPtrExt(0) 211 io.issuePtrExt := issuePtrExt 212 213 /** 214 * Writeback store from store units 215 * 216 * Most store instructions writeback to regfile in the previous cycle. 217 * However, 218 * (1) For an mmio instruction with exceptions, we need to mark it as addrvalid 219 * (in this way it will trigger an exception when it reaches ROB's head) 220 * instead of pending to avoid sending them to lower level. 221 * (2) For an mmio instruction without exceptions, we mark it as pending. 222 * When the instruction reaches ROB's head, StoreQueue sends it to uncache channel. 223 * Upon receiving the response, StoreQueue writes back the instruction 224 * through arbiter with store units. It will later commit as normal. 225 */ 226 227 // Write addr to sq 228 for (i <- 0 until StorePipelineWidth) { 229 paddrModule.io.wen(i) := false.B 230 vaddrModule.io.wen(i) := false.B 231 dataModule.io.mask.wen(i) := false.B 232 val stWbIndex = io.storeIn(i).bits.uop.sqIdx.value 233 when (io.storeIn(i).fire()) { 234 val addr_valid = !io.storeIn(i).bits.miss 235 addrvalid(stWbIndex) := addr_valid //!io.storeIn(i).bits.mmio 236 // pending(stWbIndex) := io.storeIn(i).bits.mmio 237 238 dataModule.io.mask.waddr(i) := stWbIndex 239 dataModule.io.mask.wdata(i) := io.storeIn(i).bits.mask 240 dataModule.io.mask.wen(i) := addr_valid 241 242 paddrModule.io.waddr(i) := stWbIndex 243 paddrModule.io.wdata(i) := io.storeIn(i).bits.paddr 244 paddrModule.io.wlineflag(i) := io.storeIn(i).bits.wlineflag 245 paddrModule.io.wen(i) := addr_valid 246 247 vaddrModule.io.waddr(i) := stWbIndex 248 vaddrModule.io.wdata(i) := io.storeIn(i).bits.vaddr 249 vaddrModule.io.wlineflag(i) := io.storeIn(i).bits.wlineflag 250 vaddrModule.io.wen(i) := addr_valid 251 252 debug_paddr(paddrModule.io.waddr(i)) := paddrModule.io.wdata(i) 253 254 // mmio(stWbIndex) := io.storeIn(i).bits.mmio 255 256 uop(stWbIndex).debugInfo := io.storeIn(i).bits.uop.debugInfo 257 XSInfo("store addr write to sq idx %d pc 0x%x miss:%d vaddr %x paddr %x mmio %x\n", 258 io.storeIn(i).bits.uop.sqIdx.value, 259 io.storeIn(i).bits.uop.cf.pc, 260 io.storeIn(i).bits.miss, 261 io.storeIn(i).bits.vaddr, 262 io.storeIn(i).bits.paddr, 263 io.storeIn(i).bits.mmio 264 ) 265 } 266 267 // re-replinish mmio, for pma/pmp will get mmio one cycle later 268 val storeInFireReg = RegNext(io.storeIn(i).fire() && !io.storeIn(i).bits.miss) 269 val stWbIndexReg = RegNext(stWbIndex) 270 when (storeInFireReg) { 271 pending(stWbIndexReg) := io.storeInRe(i).mmio 272 mmio(stWbIndexReg) := io.storeInRe(i).mmio 273 } 274 275 when(vaddrModule.io.wen(i)){ 276 debug_vaddr(vaddrModule.io.waddr(i)) := vaddrModule.io.wdata(i) 277 } 278 } 279 280 // Write data to sq 281 for (i <- 0 until StorePipelineWidth) { 282 dataModule.io.data.wen(i) := false.B 283 io.rob.storeDataRobWb(i).valid := false.B 284 io.rob.storeDataRobWb(i).bits := DontCare 285 val stWbIndex = io.storeDataIn(i).bits.uop.sqIdx.value 286 when (io.storeDataIn(i).fire()) { 287 datavalid(stWbIndex) := true.B 288 289 dataModule.io.data.waddr(i) := stWbIndex 290 dataModule.io.data.wdata(i) := Mux(io.storeDataIn(i).bits.uop.ctrl.fuOpType === LSUOpType.cbo_zero, 291 0.U, 292 genWdata(io.storeDataIn(i).bits.data, io.storeDataIn(i).bits.uop.ctrl.fuOpType(1,0)) 293 ) 294 dataModule.io.data.wen(i) := true.B 295 296 debug_data(dataModule.io.data.waddr(i)) := dataModule.io.data.wdata(i) 297 298 io.rob.storeDataRobWb(i).valid := true.B 299 io.rob.storeDataRobWb(i).bits := io.storeDataIn(i).bits.uop.robIdx 300 301 XSInfo("store data write to sq idx %d pc 0x%x data %x -> %x\n", 302 io.storeDataIn(i).bits.uop.sqIdx.value, 303 io.storeDataIn(i).bits.uop.cf.pc, 304 io.storeDataIn(i).bits.data, 305 dataModule.io.data.wdata(i) 306 ) 307 } 308 } 309 310 /** 311 * load forward query 312 * 313 * Check store queue for instructions that is older than the load. 314 * The response will be valid at the next cycle after req. 315 */ 316 // check over all lq entries and forward data from the first matched store 317 for (i <- 0 until LoadPipelineWidth) { 318 // Compare deqPtr (deqPtr) and forward.sqIdx, we have two cases: 319 // (1) if they have the same flag, we need to check range(tail, sqIdx) 320 // (2) if they have different flags, we need to check range(tail, LoadQueueSize) and range(0, sqIdx) 321 // Forward1: Mux(same_flag, range(tail, sqIdx), range(tail, LoadQueueSize)) 322 // Forward2: Mux(same_flag, 0.U, range(0, sqIdx) ) 323 // i.e. forward1 is the target entries with the same flag bits and forward2 otherwise 324 val differentFlag = deqPtrExt(0).flag =/= io.forward(i).sqIdx.flag 325 val forwardMask = io.forward(i).sqIdxMask 326 // all addrvalid terms need to be checked 327 val addrValidVec = WireInit(VecInit((0 until StoreQueueSize).map(i => addrvalid(i) && allocated(i)))) 328 val dataValidVec = WireInit(VecInit((0 until StoreQueueSize).map(i => datavalid(i)))) 329 val allValidVec = WireInit(VecInit((0 until StoreQueueSize).map(i => addrvalid(i) && datavalid(i) && allocated(i)))) 330 val canForward1 = Mux(differentFlag, ~deqMask, deqMask ^ forwardMask) & allValidVec.asUInt 331 val canForward2 = Mux(differentFlag, forwardMask, 0.U(StoreQueueSize.W)) & allValidVec.asUInt 332 val needForward = Mux(differentFlag, ~deqMask | forwardMask, deqMask ^ forwardMask) 333 334 XSDebug(p"$i f1 ${Binary(canForward1)} f2 ${Binary(canForward2)} " + 335 p"sqIdx ${io.forward(i).sqIdx} pa ${Hexadecimal(io.forward(i).paddr)}\n" 336 ) 337 338 // do real fwd query (cam lookup in load_s1) 339 dataModule.io.needForward(i)(0) := canForward1 & vaddrModule.io.forwardMmask(i).asUInt 340 dataModule.io.needForward(i)(1) := canForward2 & vaddrModule.io.forwardMmask(i).asUInt 341 342 vaddrModule.io.forwardMdata(i) := io.forward(i).vaddr 343 paddrModule.io.forwardMdata(i) := io.forward(i).paddr 344 345 // vaddr cam result does not equal to paddr cam result 346 // replay needed 347 // val vpmaskNotEqual = ((paddrModule.io.forwardMmask(i).asUInt ^ vaddrModule.io.forwardMmask(i).asUInt) & needForward) =/= 0.U 348 // val vaddrMatchFailed = vpmaskNotEqual && io.forward(i).valid 349 val vpmaskNotEqual = ((RegNext(paddrModule.io.forwardMmask(i).asUInt) ^ RegNext(vaddrModule.io.forwardMmask(i).asUInt)) & RegNext(needForward)) =/= 0.U 350 val vaddrMatchFailed = vpmaskNotEqual && RegNext(io.forward(i).valid) 351 when (vaddrMatchFailed) { 352 XSInfo("vaddrMatchFailed: pc %x pmask %x vmask %x\n", 353 RegNext(io.forward(i).uop.cf.pc), 354 RegNext(needForward & paddrModule.io.forwardMmask(i).asUInt), 355 RegNext(needForward & vaddrModule.io.forwardMmask(i).asUInt) 356 ); 357 } 358 XSPerfAccumulate("vaddr_match_failed", vpmaskNotEqual) 359 XSPerfAccumulate("vaddr_match_really_failed", vaddrMatchFailed) 360 361 // Fast forward mask will be generated immediately (load_s1) 362 io.forward(i).forwardMaskFast := dataModule.io.forwardMaskFast(i) 363 364 // Forward result will be generated 1 cycle later (load_s2) 365 io.forward(i).forwardMask := dataModule.io.forwardMask(i) 366 io.forward(i).forwardData := dataModule.io.forwardData(i) 367 368 // If addr match, data not ready, mark it as dataInvalid 369 // load_s1: generate dataInvalid in load_s1 to set fastUop 370 io.forward(i).dataInvalidFast := (addrValidVec.asUInt & ~dataValidVec.asUInt & vaddrModule.io.forwardMmask(i).asUInt & needForward).orR 371 val dataInvalidSqIdxReg = RegNext(OHToUInt(addrValidVec.asUInt & ~dataValidVec.asUInt & vaddrModule.io.forwardMmask(i).asUInt & needForward)) 372 // load_s2 373 io.forward(i).dataInvalid := RegNext(io.forward(i).dataInvalidFast) 374 375 // load_s2 376 // check if vaddr forward mismatched 377 io.forward(i).matchInvalid := vaddrMatchFailed 378 io.forward(i).dataInvalidSqIdx := dataInvalidSqIdxReg 379 } 380 381 /** 382 * Memory mapped IO / other uncached operations 383 * 384 * States: 385 * (1) writeback from store units: mark as pending 386 * (2) when they reach ROB's head, they can be sent to uncache channel 387 * (3) response from uncache channel: mark as datavalidmask.wen 388 * (4) writeback to ROB (and other units): mark as writebacked 389 * (5) ROB commits the instruction: same as normal instructions 390 */ 391 //(2) when they reach ROB's head, they can be sent to uncache channel 392 val s_idle :: s_req :: s_resp :: s_wb :: s_wait :: Nil = Enum(5) 393 val uncacheState = RegInit(s_idle) 394 switch(uncacheState) { 395 is(s_idle) { 396 when(io.rob.pendingst && pending(deqPtr) && allocated(deqPtr) && datavalid(deqPtr) && addrvalid(deqPtr)) { 397 uncacheState := s_req 398 } 399 } 400 is(s_req) { 401 when(io.uncache.req.fire()) { 402 uncacheState := s_resp 403 } 404 } 405 is(s_resp) { 406 when(io.uncache.resp.fire()) { 407 uncacheState := s_wb 408 } 409 } 410 is(s_wb) { 411 when (io.mmioStout.fire()) { 412 uncacheState := s_wait 413 } 414 } 415 is(s_wait) { 416 when(commitCount > 0.U) { 417 uncacheState := s_idle // ready for next mmio 418 } 419 } 420 } 421 io.uncache.req.valid := uncacheState === s_req 422 423 io.uncache.req.bits.cmd := MemoryOpConstants.M_XWR 424 io.uncache.req.bits.addr := paddrModule.io.rdata(0) // data(deqPtr) -> rdata(0) 425 io.uncache.req.bits.data := dataModule.io.rdata(0).data 426 io.uncache.req.bits.mask := dataModule.io.rdata(0).mask 427 428 // CBO op type check can be delayed for 1 cycle, 429 // as uncache op will not start in s_idle 430 val cbo_mmio_addr = paddrModule.io.rdata(0) >> 2 << 2 // clear lowest 2 bits for op 431 val cbo_mmio_op = 0.U //TODO 432 val cbo_mmio_data = cbo_mmio_addr | cbo_mmio_op 433 when(RegNext(LSUOpType.isCbo(uop(deqPtr).ctrl.fuOpType))){ 434 io.uncache.req.bits.addr := DontCare // TODO 435 io.uncache.req.bits.data := paddrModule.io.rdata(0) 436 io.uncache.req.bits.mask := DontCare // TODO 437 } 438 439 io.uncache.req.bits.id := DontCare 440 io.uncache.req.bits.instrtype := DontCare 441 442 when(io.uncache.req.fire()){ 443 // mmio store should not be committed until uncache req is sent 444 pending(deqPtr) := false.B 445 446 XSDebug( 447 p"uncache req: pc ${Hexadecimal(uop(deqPtr).cf.pc)} " + 448 p"addr ${Hexadecimal(io.uncache.req.bits.addr)} " + 449 p"data ${Hexadecimal(io.uncache.req.bits.data)} " + 450 p"op ${Hexadecimal(io.uncache.req.bits.cmd)} " + 451 p"mask ${Hexadecimal(io.uncache.req.bits.mask)}\n" 452 ) 453 } 454 455 // (3) response from uncache channel: mark as datavalid 456 io.uncache.resp.ready := true.B 457 458 // (4) writeback to ROB (and other units): mark as writebacked 459 io.mmioStout.valid := uncacheState === s_wb 460 io.mmioStout.bits.uop := uop(deqPtr) 461 io.mmioStout.bits.uop.sqIdx := deqPtrExt(0) 462 io.mmioStout.bits.data := dataModule.io.rdata(0).data // dataModule.io.rdata.read(deqPtr) 463 io.mmioStout.bits.redirectValid := false.B 464 io.mmioStout.bits.redirect := DontCare 465 io.mmioStout.bits.debug.isMMIO := true.B 466 io.mmioStout.bits.debug.paddr := DontCare 467 io.mmioStout.bits.debug.isPerfCnt := false.B 468 io.mmioStout.bits.fflags := DontCare 469 // Remove MMIO inst from store queue after MMIO request is being sent 470 // That inst will be traced by uncache state machine 471 when (io.mmioStout.fire()) { 472 allocated(deqPtr) := false.B 473 } 474 475 /** 476 * ROB commits store instructions (mark them as commited) 477 * 478 * (1) When store commits, mark it as commited. 479 * (2) They will not be cancelled and can be sent to lower level. 480 */ 481 XSError(uncacheState =/= s_idle && uncacheState =/= s_wait && commitCount > 0.U, 482 "should not commit instruction when MMIO has not been finished\n") 483 for (i <- 0 until CommitWidth) { 484 when (commitCount > i.U) { // MMIO inst is not in progress 485 if(i == 0){ 486 // MMIO inst should not update commited flag 487 // Note that commit count has been delayed for 1 cycle 488 when(uncacheState === s_idle){ 489 commited(cmtPtrExt(0).value) := true.B 490 } 491 } else { 492 commited(cmtPtrExt(i).value) := true.B 493 } 494 } 495 } 496 cmtPtrExt := cmtPtrExt.map(_ + commitCount) 497 498 // Commited stores will not be cancelled and can be sent to lower level. 499 // remove retired insts from sq, add retired store to sbuffer 500 501 // Read data from data module 502 // As store queue grows larger and larger, time needed to read data from data 503 // module keeps growing higher. Now we give data read a whole cycle. 504 505 // For now, data read logic width is hardcoded to 2 506 require(StorePipelineWidth == 2) // TODO: add EnsbufferWidth parameter 507 val mmioStall = mmio(rdataPtrExt(0).value) 508 for (i <- 0 until StorePipelineWidth) { 509 val ptr = rdataPtrExt(i).value 510 dataBuffer.io.enq(i).valid := allocated(ptr) && commited(ptr) && !mmioStall 511 // Note that store data/addr should both be valid after store's commit 512 assert(!dataBuffer.io.enq(i).valid || allvalid(ptr)) 513 dataBuffer.io.enq(i).bits.addr := paddrModule.io.rdata(i) 514 dataBuffer.io.enq(i).bits.vaddr := vaddrModule.io.rdata(i) 515 dataBuffer.io.enq(i).bits.data := dataModule.io.rdata(i).data 516 dataBuffer.io.enq(i).bits.mask := dataModule.io.rdata(i).mask 517 dataBuffer.io.enq(i).bits.wline := paddrModule.io.rlineflag(i) 518 dataBuffer.io.enq(i).bits.sqPtr := rdataPtrExt(i) 519 } 520 521 // Send data stored in sbufferReqBitsReg to sbuffer 522 for (i <- 0 until StorePipelineWidth) { 523 io.sbuffer(i).valid := dataBuffer.io.deq(i).valid 524 dataBuffer.io.deq(i).ready := io.sbuffer(i).ready 525 // Write line request should have all 1 mask 526 assert(!(io.sbuffer(i).valid && io.sbuffer(i).bits.wline && !io.sbuffer(i).bits.mask.andR)) 527 io.sbuffer(i).bits.cmd := MemoryOpConstants.M_XWR 528 io.sbuffer(i).bits.addr := dataBuffer.io.deq(i).bits.addr 529 io.sbuffer(i).bits.vaddr := dataBuffer.io.deq(i).bits.vaddr 530 io.sbuffer(i).bits.data := dataBuffer.io.deq(i).bits.data 531 io.sbuffer(i).bits.mask := dataBuffer.io.deq(i).bits.mask 532 io.sbuffer(i).bits.wline := dataBuffer.io.deq(i).bits.wline 533 io.sbuffer(i).bits.id := DontCare 534 io.sbuffer(i).bits.instrtype := DontCare 535 536 val ptr = dataBuffer.io.deq(i).bits.sqPtr.value 537 when (io.sbuffer(i).fire()) { 538 allocated(ptr) := false.B 539 XSDebug("sbuffer "+i+" fire: ptr %d\n", ptr) 540 } 541 } 542 when (io.sbuffer(1).fire()) { 543 assert(io.sbuffer(0).fire()) 544 } 545 if (coreParams.dcacheParametersOpt.isEmpty) { 546 for (i <- 0 until StorePipelineWidth) { 547 val ptr = deqPtrExt(i).value 548 val fakeRAM = Module(new RAMHelper(64L * 1024 * 1024 * 1024)) 549 fakeRAM.clk := clock 550 fakeRAM.en := allocated(ptr) && commited(ptr) && !mmio(ptr) 551 fakeRAM.rIdx := 0.U 552 fakeRAM.wIdx := (paddrModule.io.rdata(i) - "h80000000".U) >> 3 553 fakeRAM.wdata := dataModule.io.rdata(i).data 554 fakeRAM.wmask := MaskExpand(dataModule.io.rdata(i).mask) 555 fakeRAM.wen := allocated(ptr) && commited(ptr) && !mmio(ptr) 556 } 557 } 558 559 if (env.EnableDifftest) { 560 for (i <- 0 until StorePipelineWidth) { 561 val storeCommit = io.sbuffer(i).fire() 562 val waddr = SignExt(io.sbuffer(i).bits.addr, 64) 563 val wdata = io.sbuffer(i).bits.data & MaskExpand(io.sbuffer(i).bits.mask) 564 val wmask = io.sbuffer(i).bits.mask 565 566 val difftest = Module(new DifftestStoreEvent) 567 difftest.io.clock := clock 568 difftest.io.coreid := hardId.U 569 difftest.io.index := i.U 570 difftest.io.valid := storeCommit 571 difftest.io.storeAddr := waddr 572 difftest.io.storeData := wdata 573 difftest.io.storeMask := wmask 574 } 575 } 576 577 // Read vaddr for mem exception 578 io.exceptionAddr.vaddr := vaddrModule.io.rdata(StorePipelineWidth) 579 580 // misprediction recovery / exception redirect 581 // invalidate sq term using robIdx 582 val needCancel = Wire(Vec(StoreQueueSize, Bool())) 583 for (i <- 0 until StoreQueueSize) { 584 needCancel(i) := uop(i).robIdx.needFlush(io.brqRedirect) && allocated(i) && !commited(i) 585 when (needCancel(i)) { 586 allocated(i) := false.B 587 } 588 } 589 590 /** 591 * update pointers 592 */ 593 val lastCycleRedirect = RegNext(io.brqRedirect.valid) 594 val lastCycleCancelCount = PopCount(RegNext(needCancel)) 595 // when io.brqRedirect.valid, we don't allow eneuque even though it may fire. 596 val enqNumber = Mux(io.enq.canAccept && io.enq.lqCanAccept && !io.brqRedirect.valid, PopCount(io.enq.req.map(_.valid)), 0.U) 597 when (lastCycleRedirect) { 598 // we recover the pointers in the next cycle after redirect 599 enqPtrExt := VecInit(enqPtrExt.map(_ - lastCycleCancelCount)) 600 }.otherwise { 601 enqPtrExt := VecInit(enqPtrExt.map(_ + enqNumber)) 602 } 603 604 deqPtrExt := deqPtrExtNext 605 rdataPtrExt := rdataPtrExtNext 606 607 val dequeueCount = Mux(io.sbuffer(1).fire(), 2.U, Mux(io.sbuffer(0).fire() || io.mmioStout.fire(), 1.U, 0.U)) 608 val validCount = distanceBetween(enqPtrExt(0), deqPtrExt(0)) 609 610 allowEnqueue := validCount + enqNumber <= (StoreQueueSize - io.enq.req.length).U 611 612 // io.sqempty will be used by sbuffer 613 // We delay it for 1 cycle for better timing 614 // When sbuffer need to check if it is empty, the pipeline is blocked, which means delay io.sqempty 615 // for 1 cycle will also promise that sq is empty in that cycle 616 io.sqempty := RegNext( 617 enqPtrExt(0).value === deqPtrExt(0).value && 618 enqPtrExt(0).flag === deqPtrExt(0).flag 619 ) 620 621 // perf counter 622 QueuePerf(StoreQueueSize, validCount, !allowEnqueue) 623 io.sqFull := !allowEnqueue 624 XSPerfAccumulate("mmioCycle", uncacheState =/= s_idle) // lq is busy dealing with uncache req 625 XSPerfAccumulate("mmioCnt", io.uncache.req.fire()) 626 XSPerfAccumulate("mmio_wb_success", io.mmioStout.fire()) 627 XSPerfAccumulate("mmio_wb_blocked", io.mmioStout.valid && !io.mmioStout.ready) 628 XSPerfAccumulate("validEntryCnt", distanceBetween(enqPtrExt(0), deqPtrExt(0))) 629 XSPerfAccumulate("cmtEntryCnt", distanceBetween(cmtPtrExt(0), deqPtrExt(0))) 630 XSPerfAccumulate("nCmtEntryCnt", distanceBetween(enqPtrExt(0), cmtPtrExt(0))) 631 632 val perfinfo = IO(new Bundle(){ 633 val perfEvents = Output(new PerfEventsBundle(8)) 634 }) 635 val perfEvents = Seq( 636 ("mmioCycle ", uncacheState =/= s_idle ), 637 ("mmioCnt ", io.uncache.req.fire() ), 638 ("mmio_wb_success ", io.mmioStout.fire() ), 639 ("mmio_wb_blocked ", io.mmioStout.valid && !io.mmioStout.ready ), 640 ("stq_1/4_valid ", (distanceBetween(enqPtrExt(0), deqPtrExt(0)) < (StoreQueueSize.U/4.U)) ), 641 ("stq_2/4_valid ", (distanceBetween(enqPtrExt(0), deqPtrExt(0)) > (StoreQueueSize.U/4.U)) & (distanceBetween(enqPtrExt(0), deqPtrExt(0)) <= (StoreQueueSize.U/2.U)) ), 642 ("stq_3/4_valid ", (distanceBetween(enqPtrExt(0), deqPtrExt(0)) > (StoreQueueSize.U/2.U)) & (distanceBetween(enqPtrExt(0), deqPtrExt(0)) <= (StoreQueueSize.U*3.U/4.U))), 643 ("stq_4/4_valid ", (distanceBetween(enqPtrExt(0), deqPtrExt(0)) > (StoreQueueSize.U*3.U/4.U)) ), 644 ) 645 646 for (((perf_out,(perf_name,perf)),i) <- perfinfo.perfEvents.perf_events.zip(perfEvents).zipWithIndex) { 647 perf_out.incr_step := RegNext(perf) 648 } 649 // debug info 650 XSDebug("enqPtrExt %d:%d deqPtrExt %d:%d\n", enqPtrExt(0).flag, enqPtr, deqPtrExt(0).flag, deqPtr) 651 652 def PrintFlag(flag: Bool, name: String): Unit = { 653 when(flag) { 654 XSDebug(false, true.B, name) 655 }.otherwise { 656 XSDebug(false, true.B, " ") 657 } 658 } 659 660 for (i <- 0 until StoreQueueSize) { 661 XSDebug(i + ": pc %x va %x pa %x data %x ", 662 uop(i).cf.pc, 663 debug_vaddr(i), 664 debug_paddr(i), 665 debug_data(i) 666 ) 667 PrintFlag(allocated(i), "a") 668 PrintFlag(allocated(i) && addrvalid(i), "a") 669 PrintFlag(allocated(i) && datavalid(i), "d") 670 PrintFlag(allocated(i) && commited(i), "c") 671 PrintFlag(allocated(i) && pending(i), "p") 672 PrintFlag(allocated(i) && mmio(i), "m") 673 XSDebug(false, true.B, "\n") 674 } 675 676} 677