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 chisel3._ 20import chisel3.util._ 21import difftest._ 22import difftest.common.DifftestMem 23import org.chipsalliance.cde.config.Parameters 24import utility._ 25import utils._ 26import xiangshan._ 27import xiangshan.cache._ 28import xiangshan.cache.{DCacheLineIO, DCacheWordIO, MemoryOpConstants} 29import xiangshan.backend._ 30import xiangshan.backend.rob.{RobLsqIO, RobPtr} 31import xiangshan.backend.Bundles.{DynInst, MemExuOutput} 32import xiangshan.backend.decode.isa.bitfield.{Riscv32BitInst, XSInstBitFields} 33import xiangshan.backend.fu.FuConfig._ 34import xiangshan.backend.fu.FuType 35 36class SqPtr(implicit p: Parameters) extends CircularQueuePtr[SqPtr]( 37 p => p(XSCoreParamsKey).StoreQueueSize 38){ 39} 40 41object SqPtr { 42 def apply(f: Bool, v: UInt)(implicit p: Parameters): SqPtr = { 43 val ptr = Wire(new SqPtr) 44 ptr.flag := f 45 ptr.value := v 46 ptr 47 } 48} 49 50class SqEnqIO(implicit p: Parameters) extends MemBlockBundle { 51 val canAccept = Output(Bool()) 52 val lqCanAccept = Input(Bool()) 53 val needAlloc = Vec(LSQEnqWidth, Input(Bool())) 54 val req = Vec(LSQEnqWidth, Flipped(ValidIO(new DynInst))) 55 val resp = Vec(LSQEnqWidth, Output(new SqPtr)) 56} 57 58class DataBufferEntry (implicit p: Parameters) extends DCacheBundle { 59 val addr = UInt(PAddrBits.W) 60 val vaddr = UInt(VAddrBits.W) 61 val data = UInt(VLEN.W) 62 val mask = UInt((VLEN/8).W) 63 val wline = Bool() 64 val sqPtr = new SqPtr 65 val prefetch = Bool() 66 val vecValid = Bool() 67} 68 69class StoreExceptionBuffer(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper { 70 val io = IO(new Bundle() { 71 val redirect = Flipped(ValidIO(new Redirect)) 72 val storeAddrIn = Vec(StorePipelineWidth + 1, Flipped(ValidIO(new LsPipelineBundle()))) 73 val exceptionAddr = new ExceptionAddrIO 74 }) 75 76 val req_valid = RegInit(false.B) 77 val req = Reg(new LsPipelineBundle()) 78 79 // enqueue 80 // S1: 81 val s1_req = VecInit(io.storeAddrIn.map(_.bits)) 82 val s1_valid = VecInit(io.storeAddrIn.map(_.valid)) 83 84 // S2: delay 1 cycle 85 val s2_req = RegNext(s1_req) 86 val s2_valid = (0 until StorePipelineWidth + 1).map(i => 87 RegNext(s1_valid(i)) && 88 !s2_req(i).uop.robIdx.needFlush(RegNext(io.redirect)) && 89 !s2_req(i).uop.robIdx.needFlush(io.redirect) 90 ) 91 val s2_has_exception = s2_req.map(x => ExceptionNO.selectByFu(x.uop.exceptionVec, StaCfg).asUInt.orR) 92 93 val s2_enqueue = Wire(Vec(StorePipelineWidth + 1, Bool())) 94 for (w <- 0 until StorePipelineWidth + 1) { 95 s2_enqueue(w) := s2_valid(w) && s2_has_exception(w) 96 } 97 98 when (req_valid && req.uop.robIdx.needFlush(io.redirect)) { 99 req_valid := s2_enqueue.asUInt.orR 100 }.elsewhen (s2_enqueue.asUInt.orR) { 101 req_valid := req_valid || true.B 102 } 103 104 def selectOldest[T <: LsPipelineBundle](valid: Seq[Bool], bits: Seq[T]): (Seq[Bool], Seq[T]) = { 105 assert(valid.length == bits.length) 106 if (valid.length == 0 || valid.length == 1) { 107 (valid, bits) 108 } else if (valid.length == 2) { 109 val res = Seq.fill(2)(Wire(Valid(chiselTypeOf(bits(0))))) 110 for (i <- res.indices) { 111 res(i).valid := valid(i) 112 res(i).bits := bits(i) 113 } 114 val oldest = Mux(valid(0) && valid(1), 115 Mux(isAfter(bits(0).uop.robIdx, bits(1).uop.robIdx) || 116 (isNotBefore(bits(0).uop.robIdx, bits(1).uop.robIdx) && bits(0).uop.uopIdx > bits(1).uop.uopIdx), res(1), res(0)), 117 Mux(valid(0) && !valid(1), res(0), res(1))) 118 (Seq(oldest.valid), Seq(oldest.bits)) 119 } else { 120 val left = selectOldest(valid.take(valid.length / 2), bits.take(bits.length / 2)) 121 val right = selectOldest(valid.takeRight(valid.length - (valid.length / 2)), bits.takeRight(bits.length - (bits.length / 2))) 122 selectOldest(left._1 ++ right._1, left._2 ++ right._2) 123 } 124 } 125 126 val reqSel = selectOldest(s2_enqueue, s2_req) 127 128 when (req_valid) { 129 req := Mux(reqSel._1(0) && isAfter(req.uop.robIdx, reqSel._2(0).uop.robIdx) || 130 (isNotBefore(req.uop.robIdx, reqSel._2(0).uop.robIdx) && req.uop.uopIdx > reqSel._2(0).uop.uopIdx), reqSel._2(0), req) 131 } .elsewhen (s2_enqueue.asUInt.orR) { 132 req := reqSel._2(0) 133 } 134 135 io.exceptionAddr.vaddr := req.vaddr 136} 137 138// Store Queue 139class StoreQueue(implicit p: Parameters) extends XSModule 140 with HasDCacheParameters 141 with HasCircularQueuePtrHelper 142 with HasPerfEvents 143 with HasVLSUParameters { 144 val io = IO(new Bundle() { 145 val hartId = Input(UInt(8.W)) 146 val enq = new SqEnqIO 147 val brqRedirect = Flipped(ValidIO(new Redirect)) 148 val vecFeedback = Flipped(ValidIO(new FeedbackToLsqIO)) 149 val storeAddrIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) // store addr, data is not included 150 val storeAddrInRe = Vec(StorePipelineWidth, Input(new LsPipelineBundle())) // store more mmio and exception 151 val storeDataIn = Vec(StorePipelineWidth, Flipped(Valid(new MemExuOutput(isVector = true)))) // store data, send to sq from rs 152 val storeMaskIn = Vec(StorePipelineWidth, Flipped(Valid(new StoreMaskBundle))) // store mask, send to sq from rs 153 val sbuffer = Vec(EnsbufferWidth, Decoupled(new DCacheWordReqWithVaddrAndPfFlag)) // write committed store to sbuffer 154 val uncacheOutstanding = Input(Bool()) 155 val mmioStout = DecoupledIO(new MemExuOutput) // writeback uncached store 156 val vecmmioStout = DecoupledIO(new MemExuOutput(isVector = true)) 157 val forward = Vec(LoadPipelineWidth, Flipped(new PipeLoadForwardQueryIO)) 158 // TODO: scommit is only for scalar store 159 val rob = Flipped(new RobLsqIO) 160 val uncache = new UncacheWordIO 161 // val refill = Flipped(Valid(new DCacheLineReq )) 162 val exceptionAddr = new ExceptionAddrIO 163 val sqEmpty = Output(Bool()) 164 val stAddrReadySqPtr = Output(new SqPtr) 165 val stAddrReadyVec = Output(Vec(StoreQueueSize, Bool())) 166 val stDataReadySqPtr = Output(new SqPtr) 167 val stDataReadyVec = Output(Vec(StoreQueueSize, Bool())) 168 val stIssuePtr = Output(new SqPtr) 169 val sqDeqPtr = Output(new SqPtr) 170 val sqFull = Output(Bool()) 171 val sqCancelCnt = Output(UInt(log2Up(StoreQueueSize + 1).W)) 172 val sqDeq = Output(UInt(log2Ceil(EnsbufferWidth + 1).W)) 173 val force_write = Output(Bool()) 174 }) 175 176 println("StoreQueue: size:" + StoreQueueSize) 177 178 // data modules 179 val uop = Reg(Vec(StoreQueueSize, new DynInst)) 180 // val data = Reg(Vec(StoreQueueSize, new LsqEntry)) 181 val dataModule = Module(new SQDataModule( 182 numEntries = StoreQueueSize, 183 numRead = EnsbufferWidth, 184 numWrite = StorePipelineWidth, 185 numForward = LoadPipelineWidth 186 )) 187 dataModule.io := DontCare 188 val paddrModule = Module(new SQAddrModule( 189 dataWidth = PAddrBits, 190 numEntries = StoreQueueSize, 191 numRead = EnsbufferWidth, 192 numWrite = StorePipelineWidth, 193 numForward = LoadPipelineWidth 194 )) 195 paddrModule.io := DontCare 196 val vaddrModule = Module(new SQAddrModule( 197 dataWidth = VAddrBits, 198 numEntries = StoreQueueSize, 199 numRead = EnsbufferWidth, // sbuffer; badvaddr will be sent from exceptionBuffer 200 numWrite = StorePipelineWidth, 201 numForward = LoadPipelineWidth 202 )) 203 vaddrModule.io := DontCare 204 val dataBuffer = Module(new DatamoduleResultBuffer(new DataBufferEntry)) 205 val exceptionBuffer = Module(new StoreExceptionBuffer) 206 exceptionBuffer.io.redirect := io.brqRedirect 207 exceptionBuffer.io.exceptionAddr.isStore := DontCare 208 // TODO: implement it! 209 exceptionBuffer.io.storeAddrIn(StorePipelineWidth) := DontCare 210 211 val debug_paddr = Reg(Vec(StoreQueueSize, UInt((PAddrBits).W))) 212 val debug_vaddr = Reg(Vec(StoreQueueSize, UInt((VAddrBits).W))) 213 val debug_data = Reg(Vec(StoreQueueSize, UInt((XLEN).W))) 214 215 // state & misc 216 val allocated = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // sq entry has been allocated 217 val addrvalid = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // non-mmio addr is valid 218 val datavalid = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // non-mmio data is valid 219 val allvalid = VecInit((0 until StoreQueueSize).map(i => addrvalid(i) && datavalid(i))) // non-mmio data & addr is valid 220 val committed = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // inst has been committed by rob 221 val pending = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // mmio pending: inst is an mmio inst, it will not be executed until it reachs the end of rob 222 val mmio = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // mmio: inst is an mmio inst 223 val atomic = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) 224 val prefetch = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // need prefetch when committing this store to sbuffer? 225 val isVec = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // vector store instruction 226 //val vec_lastuop = Reg(Vec(StoreQueueSize, Bool())) // last uop of vector store instruction 227 val vecMbCommit = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // vector store committed from merge buffer to rob 228 val vecDataValid = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // vector store need write to sbuffer 229 // val vec_robCommit = Reg(Vec(StoreQueueSize, Bool())) // vector store committed by rob 230 // val vec_secondInv = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // Vector unit-stride, second entry is invalid 231 232 // ptr 233 val enqPtrExt = RegInit(VecInit((0 until io.enq.req.length).map(_.U.asTypeOf(new SqPtr)))) 234 val rdataPtrExt = RegInit(VecInit((0 until EnsbufferWidth).map(_.U.asTypeOf(new SqPtr)))) 235 val deqPtrExt = RegInit(VecInit((0 until EnsbufferWidth).map(_.U.asTypeOf(new SqPtr)))) 236 val cmtPtrExt = RegInit(VecInit((0 until CommitWidth).map(_.U.asTypeOf(new SqPtr)))) 237 val addrReadyPtrExt = RegInit(0.U.asTypeOf(new SqPtr)) 238 val dataReadyPtrExt = RegInit(0.U.asTypeOf(new SqPtr)) 239 240 val enqPtr = enqPtrExt(0).value 241 val deqPtr = deqPtrExt(0).value 242 val cmtPtr = cmtPtrExt(0).value 243 244 val validCount = distanceBetween(enqPtrExt(0), deqPtrExt(0)) 245 val allowEnqueue = validCount <= (StoreQueueSize - LSQStEnqWidth).U 246 247 val deqMask = UIntToMask(deqPtr, StoreQueueSize) 248 val enqMask = UIntToMask(enqPtr, StoreQueueSize) 249 250 // TODO: count commit numbers for scalar / vector store separately 251 val scalarCommitCount = RegInit(0.U(log2Ceil(StoreQueueSize + 1).W)) 252 val scalarCommitted = WireInit(0.U(log2Ceil(CommitWidth + 1).W)) 253 val vecCommitted = WireInit(0.U(log2Ceil(CommitWidth + 1).W)) 254 val commitCount = WireInit(0.U(log2Ceil(CommitWidth + 1).W)) 255 val scommit = RegNext(io.rob.scommit) 256 257 scalarCommitCount := scalarCommitCount + scommit - scalarCommitted 258 259 // store can be committed by ROB 260 io.rob.mmio := DontCare 261 io.rob.uop := DontCare 262 263 // Read dataModule 264 assert(EnsbufferWidth <= 2) 265 // rdataPtrExtNext and rdataPtrExtNext+1 entry will be read from dataModule 266 val rdataPtrExtNext = WireInit(Mux(dataBuffer.io.enq(1).fire, 267 VecInit(rdataPtrExt.map(_ + 2.U)), 268 Mux(dataBuffer.io.enq(0).fire || io.mmioStout.fire || io.vecmmioStout.fire, 269 VecInit(rdataPtrExt.map(_ + 1.U)), 270 rdataPtrExt 271 ) 272 )) 273 274 // deqPtrExtNext traces which inst is about to leave store queue 275 // 276 // io.sbuffer(i).fire is RegNexted, as sbuffer data write takes 2 cycles. 277 // Before data write finish, sbuffer is unable to provide store to load 278 // forward data. As an workaround, deqPtrExt and allocated flag update 279 // is delayed so that load can get the right data from store queue. 280 // 281 // Modify deqPtrExtNext and io.sqDeq with care! 282 val deqPtrExtNext = Mux(RegNext(io.sbuffer(1).fire), 283 VecInit(deqPtrExt.map(_ + 2.U)), 284 Mux((RegNext(io.sbuffer(0).fire)) || io.mmioStout.fire || io.vecmmioStout.fire, 285 VecInit(deqPtrExt.map(_ + 1.U)), 286 deqPtrExt 287 ) 288 ) 289 io.sqDeq := RegNext(Mux(RegNext(io.sbuffer(1).fire), 2.U, 290 Mux((RegNext(io.sbuffer(0).fire)) || io.mmioStout.fire || io.vecmmioStout.fire, 1.U, 0.U) 291 )) 292 assert(!RegNext(RegNext(io.sbuffer(0).fire) && (io.mmioStout.fire || io.vecmmioStout.fire))) 293 294 for (i <- 0 until EnsbufferWidth) { 295 dataModule.io.raddr(i) := rdataPtrExtNext(i).value 296 paddrModule.io.raddr(i) := rdataPtrExtNext(i).value 297 vaddrModule.io.raddr(i) := rdataPtrExtNext(i).value 298 } 299 300 /** 301 * Enqueue at dispatch 302 * 303 * Currently, StoreQueue only allows enqueue when #emptyEntries > EnqWidth 304 */ 305 io.enq.canAccept := allowEnqueue 306 val canEnqueue = io.enq.req.map(_.valid) 307 val enqCancel = io.enq.req.map(_.bits.robIdx.needFlush(io.brqRedirect)) 308 val vStoreFlow = io.enq.req.map(_.bits.numLsElem) 309 val validVStoreFlow = vStoreFlow.zipWithIndex.map{case (vLoadFlowNumItem, index) => Mux(!RegNext(io.brqRedirect.valid) && io.enq.canAccept && io.enq.lqCanAccept && canEnqueue(index), vLoadFlowNumItem, 0.U)} 310 val validVStoreOffset = vStoreFlow.zip(io.enq.needAlloc).map{case (flow, needAllocItem) => Mux(needAllocItem, flow, 0.U)} 311 val validVStoreOffsetRShift = 0.U +: validVStoreOffset.take(vStoreFlow.length - 1) 312 313 for (i <- 0 until io.enq.req.length) { 314 val sqIdx = enqPtrExt(0) + validVStoreOffsetRShift.take(i + 1).reduce(_ + _) 315 val index = io.enq.req(i).bits.sqIdx 316 val enqInstr = io.enq.req(i).bits.instr.asTypeOf(new XSInstBitFields) 317 when (canEnqueue(i) && !enqCancel(i)) { 318 for (j <- 0 until VecMemDispatchMaxNumber) { 319 when (j.U < validVStoreOffset(i)) { 320 uop((index + j.U).value) := io.enq.req(i).bits 321 // NOTE: the index will be used when replay 322 uop((index + j.U).value).sqIdx := sqIdx + j.U 323 allocated((index + j.U).value) := true.B 324 datavalid((index + j.U).value) := false.B 325 addrvalid((index + j.U).value) := false.B 326 committed((index + j.U).value) := false.B 327 pending((index + j.U).value) := false.B 328 prefetch((index + j.U).value) := false.B 329 mmio((index + j.U).value) := false.B 330 isVec((index + j.U).value) := enqInstr.isVecStore // check vector store by the encoding of inst 331 vecMbCommit((index + j.U).value) := false.B 332 vecDataValid((index + j.U).value) := false.B 333 XSError(!io.enq.canAccept || !io.enq.lqCanAccept, s"must accept $i\n") 334 XSError(index.value =/= sqIdx.value, s"must be the same entry $i\n") 335 } 336 } 337 } 338 io.enq.resp(i) := sqIdx 339 } 340 XSDebug(p"(ready, valid): ${io.enq.canAccept}, ${Binary(Cat(io.enq.req.map(_.valid)))}\n") 341 342 /** 343 * Update addr/dataReadyPtr when issue from rs 344 */ 345 // update issuePtr 346 val IssuePtrMoveStride = 4 347 require(IssuePtrMoveStride >= 2) 348 349 val addrReadyLookupVec = (0 until IssuePtrMoveStride).map(addrReadyPtrExt + _.U) 350 val addrReadyLookup = addrReadyLookupVec.map(ptr => allocated(ptr.value) && 351 (mmio(ptr.value) || addrvalid(ptr.value) || vecMbCommit(ptr.value)) 352 && ptr =/= enqPtrExt(0)) 353 val nextAddrReadyPtr = addrReadyPtrExt + PriorityEncoder(VecInit(addrReadyLookup.map(!_) :+ true.B)) 354 addrReadyPtrExt := nextAddrReadyPtr 355 356 (0 until StoreQueueSize).map(i => { 357 io.stAddrReadyVec(i) := RegNext(allocated(i) && (mmio(i) || addrvalid(i))) 358 }) 359 360 when (io.brqRedirect.valid) { 361 addrReadyPtrExt := Mux( 362 isAfter(cmtPtrExt(0), deqPtrExt(0)), 363 cmtPtrExt(0), 364 deqPtrExtNext(0) // for mmio insts, deqPtr may be ahead of cmtPtr 365 ) 366 } 367 368 io.stAddrReadySqPtr := addrReadyPtrExt 369 370 // update 371 val dataReadyLookupVec = (0 until IssuePtrMoveStride).map(dataReadyPtrExt + _.U) 372 val dataReadyLookup = dataReadyLookupVec.map(ptr => allocated(ptr.value) && 373 (mmio(ptr.value) || datavalid(ptr.value) || vecMbCommit(ptr.value)) 374 && ptr =/= enqPtrExt(0)) 375 val nextDataReadyPtr = dataReadyPtrExt + PriorityEncoder(VecInit(dataReadyLookup.map(!_) :+ true.B)) 376 dataReadyPtrExt := nextDataReadyPtr 377 378 (0 until StoreQueueSize).map(i => { 379 io.stDataReadyVec(i) := RegNext(allocated(i) && (mmio(i) || datavalid(i))) 380 }) 381 382 when (io.brqRedirect.valid) { 383 dataReadyPtrExt := Mux( 384 isAfter(cmtPtrExt(0), deqPtrExt(0)), 385 cmtPtrExt(0), 386 deqPtrExtNext(0) // for mmio insts, deqPtr may be ahead of cmtPtr 387 ) 388 } 389 390 io.stDataReadySqPtr := dataReadyPtrExt 391 io.stIssuePtr := enqPtrExt(0) 392 io.sqDeqPtr := deqPtrExt(0) 393 394 /** 395 * Writeback store from store units 396 * 397 * Most store instructions writeback to regfile in the previous cycle. 398 * However, 399 * (1) For an mmio instruction with exceptions, we need to mark it as addrvalid 400 * (in this way it will trigger an exception when it reaches ROB's head) 401 * instead of pending to avoid sending them to lower level. 402 * (2) For an mmio instruction without exceptions, we mark it as pending. 403 * When the instruction reaches ROB's head, StoreQueue sends it to uncache channel. 404 * Upon receiving the response, StoreQueue writes back the instruction 405 * through arbiter with store units. It will later commit as normal. 406 */ 407 408 // Write addr to sq 409 for (i <- 0 until StorePipelineWidth) { 410 paddrModule.io.wen(i) := false.B 411 vaddrModule.io.wen(i) := false.B 412 dataModule.io.mask.wen(i) := false.B 413 val stWbIndex = io.storeAddrIn(i).bits.uop.sqIdx.value 414 exceptionBuffer.io.storeAddrIn(i).valid := io.storeAddrIn(i).fire && !io.storeAddrIn(i).bits.miss && !io.storeAddrIn(i).bits.isvec 415 exceptionBuffer.io.storeAddrIn(i).bits := io.storeAddrIn(i).bits 416 417 when (io.storeAddrIn(i).fire) { 418 val addr_valid = !io.storeAddrIn(i).bits.miss 419 addrvalid(stWbIndex) := addr_valid //!io.storeAddrIn(i).bits.mmio 420 // pending(stWbIndex) := io.storeAddrIn(i).bits.mmio 421 422 paddrModule.io.waddr(i) := stWbIndex 423 paddrModule.io.wdata(i) := io.storeAddrIn(i).bits.paddr 424 paddrModule.io.wmask(i) := io.storeAddrIn(i).bits.mask 425 paddrModule.io.wlineflag(i) := io.storeAddrIn(i).bits.wlineflag 426 paddrModule.io.wen(i) := true.B 427 428 vaddrModule.io.waddr(i) := stWbIndex 429 vaddrModule.io.wdata(i) := io.storeAddrIn(i).bits.vaddr 430 vaddrModule.io.wmask(i) := io.storeAddrIn(i).bits.mask 431 vaddrModule.io.wlineflag(i) := io.storeAddrIn(i).bits.wlineflag 432 vaddrModule.io.wen(i) := true.B 433 434 debug_paddr(paddrModule.io.waddr(i)) := paddrModule.io.wdata(i) 435 436 // mmio(stWbIndex) := io.storeAddrIn(i).bits.mmio 437 438 uop(stWbIndex) := io.storeAddrIn(i).bits.uop 439 uop(stWbIndex).debugInfo := io.storeAddrIn(i).bits.uop.debugInfo 440 441 vecDataValid(stWbIndex) := io.storeAddrIn(i).bits.isvec 442 443 XSInfo("store addr write to sq idx %d pc 0x%x miss:%d vaddr %x paddr %x mmio %x isvec %x\n", 444 io.storeAddrIn(i).bits.uop.sqIdx.value, 445 io.storeAddrIn(i).bits.uop.pc, 446 io.storeAddrIn(i).bits.miss, 447 io.storeAddrIn(i).bits.vaddr, 448 io.storeAddrIn(i).bits.paddr, 449 io.storeAddrIn(i).bits.mmio, 450 io.storeAddrIn(i).bits.isvec 451 ) 452 } 453 454 // re-replinish mmio, for pma/pmp will get mmio one cycle later 455 val storeAddrInFireReg = RegNext(io.storeAddrIn(i).fire && !io.storeAddrIn(i).bits.miss) 456 val stWbIndexReg = RegNext(stWbIndex) 457 when (storeAddrInFireReg) { 458 pending(stWbIndexReg) := io.storeAddrInRe(i).mmio 459 mmio(stWbIndexReg) := io.storeAddrInRe(i).mmio 460 atomic(stWbIndexReg) := io.storeAddrInRe(i).atomic 461 } 462 // dcache miss info (one cycle later than storeIn) 463 // if dcache report a miss in sta pipeline, this store will trigger a prefetch when committing to sbuffer (if EnableAtCommitMissTrigger) 464 when (storeAddrInFireReg) { 465 prefetch(stWbIndexReg) := io.storeAddrInRe(i).miss 466 } 467 468 when(vaddrModule.io.wen(i)){ 469 debug_vaddr(vaddrModule.io.waddr(i)) := vaddrModule.io.wdata(i) 470 } 471 } 472 473 // Write data to sq 474 // Now store data pipeline is actually 2 stages 475 for (i <- 0 until StorePipelineWidth) { 476 dataModule.io.data.wen(i) := false.B 477 val stWbIndex = io.storeDataIn(i).bits.uop.sqIdx.value 478 val isVec = FuType.isVStore(io.storeDataIn(i).bits.uop.fuType) 479 // sq data write takes 2 cycles: 480 // sq data write s0 481 when (io.storeDataIn(i).fire) { 482 // send data write req to data module 483 dataModule.io.data.waddr(i) := stWbIndex 484 dataModule.io.data.wdata(i) := Mux(io.storeDataIn(i).bits.uop.fuOpType === LSUOpType.cbo_zero, 485 0.U, 486 Mux(isVec, 487 io.storeDataIn(i).bits.data, 488 genVWdata(io.storeDataIn(i).bits.data, io.storeDataIn(i).bits.uop.fuOpType(2,0))) 489 ) 490 dataModule.io.data.wen(i) := true.B 491 492 debug_data(dataModule.io.data.waddr(i)) := dataModule.io.data.wdata(i) 493 494 XSInfo("store data write to sq idx %d pc 0x%x data %x -> %x\n", 495 io.storeDataIn(i).bits.uop.sqIdx.value, 496 io.storeDataIn(i).bits.uop.pc, 497 io.storeDataIn(i).bits.data, 498 dataModule.io.data.wdata(i) 499 ) 500 } 501 // sq data write s1 502 when ( 503 RegNext(io.storeDataIn(i).fire) 504 // && !RegNext(io.storeDataIn(i).bits.uop).robIdx.needFlush(io.brqRedirect) 505 ) { 506 datavalid(RegNext(stWbIndex)) := true.B 507 } 508 } 509 510 // Write mask to sq 511 for (i <- 0 until StorePipelineWidth) { 512 // sq mask write s0 513 when (io.storeMaskIn(i).fire) { 514 // send data write req to data module 515 dataModule.io.mask.waddr(i) := io.storeMaskIn(i).bits.sqIdx.value 516 dataModule.io.mask.wdata(i) := io.storeMaskIn(i).bits.mask 517 dataModule.io.mask.wen(i) := true.B 518 } 519 } 520 521 /** 522 * load forward query 523 * 524 * Check store queue for instructions that is older than the load. 525 * The response will be valid at the next cycle after req. 526 */ 527 // check over all lq entries and forward data from the first matched store 528 for (i <- 0 until LoadPipelineWidth) { 529 // Compare deqPtr (deqPtr) and forward.sqIdx, we have two cases: 530 // (1) if they have the same flag, we need to check range(tail, sqIdx) 531 // (2) if they have different flags, we need to check range(tail, VirtualLoadQueueSize) and range(0, sqIdx) 532 // Forward1: Mux(same_flag, range(tail, sqIdx), range(tail, VirtualLoadQueueSize)) 533 // Forward2: Mux(same_flag, 0.U, range(0, sqIdx) ) 534 // i.e. forward1 is the target entries with the same flag bits and forward2 otherwise 535 val differentFlag = deqPtrExt(0).flag =/= io.forward(i).sqIdx.flag 536 val forwardMask = io.forward(i).sqIdxMask 537 // all addrvalid terms need to be checked 538 // Real Vaild: all scalar stores, and vector store with (!inactive && !secondInvalid) 539 val addrRealValidVec = WireInit(VecInit((0 until StoreQueueSize).map(j => addrvalid(j) && allocated(j)))) 540 // vector store will consider all inactive || secondInvalid flows as valid 541 val addrValidVec = WireInit(VecInit((0 until StoreQueueSize).map(j => addrvalid(j) && allocated(j)))) 542 val dataValidVec = WireInit(VecInit((0 until StoreQueueSize).map(j => datavalid(j)))) 543 val allValidVec = WireInit(VecInit((0 until StoreQueueSize).map(j => addrvalid(j) && datavalid(j) && allocated(j)))) 544 545 val lfstEnable = Constantin.createRecord("LFSTEnable", LFSTEnable.B).orR 546 val storeSetHitVec = Mux(lfstEnable, 547 WireInit(VecInit((0 until StoreQueueSize).map(j => io.forward(i).uop.loadWaitBit && uop(j).robIdx === io.forward(i).uop.waitForRobIdx))), 548 WireInit(VecInit((0 until StoreQueueSize).map(j => uop(j).storeSetHit && uop(j).ssid === io.forward(i).uop.ssid))) 549 ) 550 551 val forwardMask1 = Mux(differentFlag, ~deqMask, deqMask ^ forwardMask) 552 val forwardMask2 = Mux(differentFlag, forwardMask, 0.U(StoreQueueSize.W)) 553 val canForward1 = forwardMask1 & allValidVec.asUInt 554 val canForward2 = forwardMask2 & allValidVec.asUInt 555 val needForward = Mux(differentFlag, ~deqMask | forwardMask, deqMask ^ forwardMask) 556 557 XSDebug(p"$i f1 ${Binary(canForward1)} f2 ${Binary(canForward2)} " + 558 p"sqIdx ${io.forward(i).sqIdx} pa ${Hexadecimal(io.forward(i).paddr)}\n" 559 ) 560 561 // do real fwd query (cam lookup in load_s1) 562 dataModule.io.needForward(i)(0) := canForward1 & vaddrModule.io.forwardMmask(i).asUInt 563 dataModule.io.needForward(i)(1) := canForward2 & vaddrModule.io.forwardMmask(i).asUInt 564 565 vaddrModule.io.forwardMdata(i) := io.forward(i).vaddr 566 vaddrModule.io.forwardDataMask(i) := io.forward(i).mask 567 paddrModule.io.forwardMdata(i) := io.forward(i).paddr 568 paddrModule.io.forwardDataMask(i) := io.forward(i).mask 569 570 571 // vaddr cam result does not equal to paddr cam result 572 // replay needed 573 // val vpmaskNotEqual = ((paddrModule.io.forwardMmask(i).asUInt ^ vaddrModule.io.forwardMmask(i).asUInt) & needForward) =/= 0.U 574 // val vaddrMatchFailed = vpmaskNotEqual && io.forward(i).valid 575 val vpmaskNotEqual = ( 576 (RegNext(paddrModule.io.forwardMmask(i).asUInt) ^ RegNext(vaddrModule.io.forwardMmask(i).asUInt)) & 577 RegNext(needForward) & 578 RegNext(addrRealValidVec.asUInt) 579 ) =/= 0.U 580 val vaddrMatchFailed = vpmaskNotEqual && RegNext(io.forward(i).valid) 581 when (vaddrMatchFailed) { 582 XSInfo("vaddrMatchFailed: pc %x pmask %x vmask %x\n", 583 RegNext(io.forward(i).uop.pc), 584 RegNext(needForward & paddrModule.io.forwardMmask(i).asUInt), 585 RegNext(needForward & vaddrModule.io.forwardMmask(i).asUInt) 586 ); 587 } 588 XSPerfAccumulate("vaddr_match_failed", vpmaskNotEqual) 589 XSPerfAccumulate("vaddr_match_really_failed", vaddrMatchFailed) 590 591 // Fast forward mask will be generated immediately (load_s1) 592 io.forward(i).forwardMaskFast := dataModule.io.forwardMaskFast(i) 593 594 // Forward result will be generated 1 cycle later (load_s2) 595 io.forward(i).forwardMask := dataModule.io.forwardMask(i) 596 io.forward(i).forwardData := dataModule.io.forwardData(i) 597 // If addr match, data not ready, mark it as dataInvalid 598 // load_s1: generate dataInvalid in load_s1 to set fastUop 599 val dataInvalidMask1 = (addrValidVec.asUInt & ~dataValidVec.asUInt & vaddrModule.io.forwardMmask(i).asUInt & forwardMask1.asUInt) 600 val dataInvalidMask2 = (addrValidVec.asUInt & ~dataValidVec.asUInt & vaddrModule.io.forwardMmask(i).asUInt & forwardMask2.asUInt) 601 val dataInvalidMask = dataInvalidMask1 | dataInvalidMask2 602 io.forward(i).dataInvalidFast := dataInvalidMask.orR 603 604 // make chisel happy 605 val dataInvalidMask1Reg = Wire(UInt(StoreQueueSize.W)) 606 dataInvalidMask1Reg := RegNext(dataInvalidMask1) 607 // make chisel happy 608 val dataInvalidMask2Reg = Wire(UInt(StoreQueueSize.W)) 609 dataInvalidMask2Reg := RegNext(dataInvalidMask2) 610 val dataInvalidMaskReg = dataInvalidMask1Reg | dataInvalidMask2Reg 611 612 // If SSID match, address not ready, mark it as addrInvalid 613 // load_s2: generate addrInvalid 614 val addrInvalidMask1 = (~addrValidVec.asUInt & storeSetHitVec.asUInt & forwardMask1.asUInt) 615 val addrInvalidMask2 = (~addrValidVec.asUInt & storeSetHitVec.asUInt & forwardMask2.asUInt) 616 // make chisel happy 617 val addrInvalidMask1Reg = Wire(UInt(StoreQueueSize.W)) 618 addrInvalidMask1Reg := RegNext(addrInvalidMask1) 619 // make chisel happy 620 val addrInvalidMask2Reg = Wire(UInt(StoreQueueSize.W)) 621 addrInvalidMask2Reg := RegNext(addrInvalidMask2) 622 val addrInvalidMaskReg = addrInvalidMask1Reg | addrInvalidMask2Reg 623 624 // load_s2 625 io.forward(i).dataInvalid := RegNext(io.forward(i).dataInvalidFast) 626 // check if vaddr forward mismatched 627 io.forward(i).matchInvalid := vaddrMatchFailed 628 629 // data invalid sq index 630 // check whether false fail 631 // check flag 632 val s2_differentFlag = RegNext(differentFlag) 633 val s2_enqPtrExt = RegNext(enqPtrExt(0)) 634 val s2_deqPtrExt = RegNext(deqPtrExt(0)) 635 636 // addr invalid sq index 637 // make chisel happy 638 val addrInvalidMaskRegWire = Wire(UInt(StoreQueueSize.W)) 639 addrInvalidMaskRegWire := addrInvalidMaskReg 640 val addrInvalidFlag = addrInvalidMaskRegWire.orR 641 val hasInvalidAddr = (~addrValidVec.asUInt & needForward).orR 642 643 val addrInvalidSqIdx1 = OHToUInt(Reverse(PriorityEncoderOH(Reverse(addrInvalidMask1Reg)))) 644 val addrInvalidSqIdx2 = OHToUInt(Reverse(PriorityEncoderOH(Reverse(addrInvalidMask2Reg)))) 645 val addrInvalidSqIdx = Mux(addrInvalidMask2Reg.orR, addrInvalidSqIdx2, addrInvalidSqIdx1) 646 647 // store-set content management 648 // +-----------------------+ 649 // | Search a SSID for the | 650 // | load operation | 651 // +-----------------------+ 652 // | 653 // V 654 // +-------------------+ 655 // | load wait strict? | 656 // +-------------------+ 657 // | 658 // V 659 // +----------------------+ 660 // Set| |Clean 661 // V V 662 // +------------------------+ +------------------------------+ 663 // | Waiting for all older | | Wait until the corresponding | 664 // | stores operations | | older store operations | 665 // +------------------------+ +------------------------------+ 666 667 668 669 when (RegNext(io.forward(i).uop.loadWaitStrict)) { 670 io.forward(i).addrInvalidSqIdx := RegNext(io.forward(i).uop.sqIdx - 1.U) 671 } .elsewhen (addrInvalidFlag) { 672 io.forward(i).addrInvalidSqIdx.flag := Mux(!s2_differentFlag || addrInvalidSqIdx >= s2_deqPtrExt.value, s2_deqPtrExt.flag, s2_enqPtrExt.flag) 673 io.forward(i).addrInvalidSqIdx.value := addrInvalidSqIdx 674 } .otherwise { 675 // may be store inst has been written to sbuffer already. 676 io.forward(i).addrInvalidSqIdx := RegNext(io.forward(i).uop.sqIdx) 677 } 678 io.forward(i).addrInvalid := Mux(RegNext(io.forward(i).uop.loadWaitStrict), RegNext(hasInvalidAddr), addrInvalidFlag) 679 680 // data invalid sq index 681 // make chisel happy 682 val dataInvalidMaskRegWire = Wire(UInt(StoreQueueSize.W)) 683 dataInvalidMaskRegWire := dataInvalidMaskReg 684 val dataInvalidFlag = dataInvalidMaskRegWire.orR 685 686 val dataInvalidSqIdx1 = OHToUInt(Reverse(PriorityEncoderOH(Reverse(dataInvalidMask1Reg)))) 687 val dataInvalidSqIdx2 = OHToUInt(Reverse(PriorityEncoderOH(Reverse(dataInvalidMask2Reg)))) 688 val dataInvalidSqIdx = Mux(dataInvalidMask2Reg.orR, dataInvalidSqIdx2, dataInvalidSqIdx1) 689 690 when (dataInvalidFlag) { 691 io.forward(i).dataInvalidSqIdx.flag := Mux(!s2_differentFlag || dataInvalidSqIdx >= s2_deqPtrExt.value, s2_deqPtrExt.flag, s2_enqPtrExt.flag) 692 io.forward(i).dataInvalidSqIdx.value := dataInvalidSqIdx 693 } .otherwise { 694 // may be store inst has been written to sbuffer already. 695 io.forward(i).dataInvalidSqIdx := RegNext(io.forward(i).uop.sqIdx) 696 } 697 } 698 699 /** 700 * Memory mapped IO / other uncached operations 701 * 702 * States: 703 * (1) writeback from store units: mark as pending 704 * (2) when they reach ROB's head, they can be sent to uncache channel 705 * (3) response from uncache channel: mark as datavalidmask.wen 706 * (4) writeback to ROB (and other units): mark as writebacked 707 * (5) ROB commits the instruction: same as normal instructions 708 */ 709 //(2) when they reach ROB's head, they can be sent to uncache channel 710 // TODO: CAN NOT deal with vector mmio now! 711 val s_idle :: s_req :: s_resp :: s_wb :: s_wait :: Nil = Enum(5) 712 val uncacheState = RegInit(s_idle) 713 switch(uncacheState) { 714 is(s_idle) { 715 when(RegNext(io.rob.pendingst && pending(deqPtr) && allocated(deqPtr) && datavalid(deqPtr) && addrvalid(deqPtr))) { 716 uncacheState := s_req 717 } 718 } 719 is(s_req) { 720 when (io.uncache.req.fire) { 721 when (io.uncacheOutstanding) { 722 uncacheState := s_wb 723 } .otherwise { 724 uncacheState := s_resp 725 } 726 } 727 } 728 is(s_resp) { 729 when(io.uncache.resp.fire) { 730 uncacheState := s_wb 731 } 732 } 733 is(s_wb) { 734 when (io.mmioStout.fire || io.vecmmioStout.fire) { 735 uncacheState := s_wait 736 } 737 } 738 is(s_wait) { 739 // A MMIO store can always move cmtPtrExt as it must be ROB head 740 when(scommit > 0.U) { 741 uncacheState := s_idle // ready for next mmio 742 } 743 } 744 } 745 io.uncache.req.valid := uncacheState === s_req 746 747 io.uncache.req.bits := DontCare 748 io.uncache.req.bits.cmd := MemoryOpConstants.M_XWR 749 io.uncache.req.bits.addr := paddrModule.io.rdata(0) // data(deqPtr) -> rdata(0) 750 io.uncache.req.bits.data := shiftDataToLow(paddrModule.io.rdata(0), dataModule.io.rdata(0).data) 751 io.uncache.req.bits.mask := shiftMaskToLow(paddrModule.io.rdata(0), dataModule.io.rdata(0).mask) 752 753 // CBO op type check can be delayed for 1 cycle, 754 // as uncache op will not start in s_idle 755 val cbo_mmio_addr = paddrModule.io.rdata(0) >> 2 << 2 // clear lowest 2 bits for op 756 val cbo_mmio_op = 0.U //TODO 757 val cbo_mmio_data = cbo_mmio_addr | cbo_mmio_op 758 when(RegNext(LSUOpType.isCbo(uop(deqPtr).fuOpType))){ 759 io.uncache.req.bits.addr := DontCare // TODO 760 io.uncache.req.bits.data := paddrModule.io.rdata(0) 761 io.uncache.req.bits.mask := DontCare // TODO 762 } 763 764 io.uncache.req.bits.atomic := atomic(RegNext(rdataPtrExtNext(0)).value) 765 766 when(io.uncache.req.fire){ 767 // mmio store should not be committed until uncache req is sent 768 pending(deqPtr) := false.B 769 770 XSDebug( 771 p"uncache req: pc ${Hexadecimal(uop(deqPtr).pc)} " + 772 p"addr ${Hexadecimal(io.uncache.req.bits.addr)} " + 773 p"data ${Hexadecimal(io.uncache.req.bits.data)} " + 774 p"op ${Hexadecimal(io.uncache.req.bits.cmd)} " + 775 p"mask ${Hexadecimal(io.uncache.req.bits.mask)}\n" 776 ) 777 } 778 779 // (3) response from uncache channel: mark as datavalid 780 io.uncache.resp.ready := true.B 781 782 // (4) scalar store: writeback to ROB (and other units): mark as writebacked 783 io.mmioStout.valid := uncacheState === s_wb && !isVec(deqPtr) 784 io.mmioStout.bits.uop := uop(deqPtr) 785 io.mmioStout.bits.uop.sqIdx := deqPtrExt(0) 786 io.mmioStout.bits.data := shiftDataToLow(paddrModule.io.rdata(0), dataModule.io.rdata(0).data) // dataModule.io.rdata.read(deqPtr) 787 io.mmioStout.bits.debug.isMMIO := true.B 788 io.mmioStout.bits.debug.paddr := DontCare 789 io.mmioStout.bits.debug.isPerfCnt := false.B 790 io.mmioStout.bits.debug.vaddr := DontCare 791 // Remove MMIO inst from store queue after MMIO request is being sent 792 // That inst will be traced by uncache state machine 793 when (io.mmioStout.fire) { 794 allocated(deqPtr) := false.B 795 } 796 797 // (4) or vector store: 798 // TODO: implement it! 799 io.vecmmioStout := DontCare 800 io.vecmmioStout.valid := uncacheState === s_wb && isVec(deqPtr) 801 io.vecmmioStout.bits.uop := uop(deqPtr) 802 io.vecmmioStout.bits.uop.sqIdx := deqPtrExt(0) 803 io.vecmmioStout.bits.data := shiftDataToLow(paddrModule.io.rdata(0), dataModule.io.rdata(0).data) // dataModule.io.rdata.read(deqPtr) 804 io.vecmmioStout.bits.debug.isMMIO := true.B 805 io.vecmmioStout.bits.debug.paddr := DontCare 806 io.vecmmioStout.bits.debug.isPerfCnt := false.B 807 io.vecmmioStout.bits.debug.vaddr := DontCare 808 // Remove MMIO inst from store queue after MMIO request is being sent 809 // That inst will be traced by uncache state machine 810 when (io.vecmmioStout.fire) { 811 allocated(deqPtr) := false.B 812 } 813 814 /** 815 * ROB commits store instructions (mark them as committed) 816 * 817 * (1) When store commits, mark it as committed. 818 * (2) They will not be cancelled and can be sent to lower level. 819 */ 820 XSError(uncacheState =/= s_idle && uncacheState =/= s_wait && commitCount > 0.U, 821 "should not commit instruction when MMIO has not been finished\n") 822 823 val scalarcommitVec = WireInit(VecInit(Seq.fill(CommitWidth)(false.B))) 824 val veccommitVec = WireInit(VecInit(Seq.fill(CommitWidth)(false.B))) 825 // TODO: Deal with vector store mmio 826 for (i <- 0 until CommitWidth) { 827 val veccount = PopCount(veccommitVec.take(i)) 828 when (allocated(cmtPtrExt(i).value) && isVec(cmtPtrExt(i).value) && isNotAfter(uop(cmtPtrExt(i).value).robIdx, io.rob.pendingPtr) && vecMbCommit(cmtPtrExt(i).value)) { 829 if (i == 0){ 830 // TODO: fixme for vector mmio 831 when ((uncacheState === s_idle) || (uncacheState === s_wait && scommit > 0.U)){ 832 committed(cmtPtrExt(0).value) := true.B 833 veccommitVec(i) := true.B 834 } 835 } else { 836 committed(cmtPtrExt(i).value) := true.B 837 veccommitVec(i) := true.B 838 } 839 } .elsewhen (scalarCommitCount > i.U - veccount) { 840 if (i == 0){ 841 when ((uncacheState === s_idle) || (uncacheState === s_wait && scommit > 0.U)){ 842 committed(cmtPtrExt(0).value) := true.B 843 scalarcommitVec(i) := true.B 844 } 845 } else { 846 committed(cmtPtrExt(i).value) := true.B 847 scalarcommitVec(i) := true.B 848 } 849 } 850 } 851 852 scalarCommitted := PopCount(scalarcommitVec) 853 vecCommitted := PopCount(veccommitVec) 854 commitCount := scalarCommitted + vecCommitted 855 856 cmtPtrExt := cmtPtrExt.map(_ + commitCount) 857 858 // committed stores will not be cancelled and can be sent to lower level. 859 // remove retired insts from sq, add retired store to sbuffer 860 861 // Read data from data module 862 // As store queue grows larger and larger, time needed to read data from data 863 // module keeps growing higher. Now we give data read a whole cycle. 864 val mmioStall = mmio(rdataPtrExt(0).value) 865 for (i <- 0 until EnsbufferWidth) { 866 val ptr = rdataPtrExt(i).value 867 dataBuffer.io.enq(i).valid := allocated(ptr) && committed(ptr) && (!isVec(ptr) || vecMbCommit(ptr)) && !mmioStall 868 // Note that store data/addr should both be valid after store's commit 869 assert(!dataBuffer.io.enq(i).valid || allvalid(ptr) || (allocated(ptr) && vecMbCommit(ptr))) 870 dataBuffer.io.enq(i).bits.addr := paddrModule.io.rdata(i) 871 dataBuffer.io.enq(i).bits.vaddr := vaddrModule.io.rdata(i) 872 dataBuffer.io.enq(i).bits.data := dataModule.io.rdata(i).data 873 dataBuffer.io.enq(i).bits.mask := dataModule.io.rdata(i).mask 874 dataBuffer.io.enq(i).bits.wline := paddrModule.io.rlineflag(i) 875 dataBuffer.io.enq(i).bits.sqPtr := rdataPtrExt(i) 876 dataBuffer.io.enq(i).bits.prefetch := prefetch(ptr) 877 dataBuffer.io.enq(i).bits.vecValid := !isVec(ptr) || vecDataValid(ptr) // scalar is always valid 878 } 879 880 // Send data stored in sbufferReqBitsReg to sbuffer 881 for (i <- 0 until EnsbufferWidth) { 882 io.sbuffer(i).valid := dataBuffer.io.deq(i).valid 883 dataBuffer.io.deq(i).ready := io.sbuffer(i).ready 884 // Write line request should have all 1 mask 885 assert(!(io.sbuffer(i).valid && io.sbuffer(i).bits.wline && io.sbuffer(i).bits.vecValid && !io.sbuffer(i).bits.mask.andR)) 886 io.sbuffer(i).bits := DontCare 887 io.sbuffer(i).bits.cmd := MemoryOpConstants.M_XWR 888 io.sbuffer(i).bits.addr := dataBuffer.io.deq(i).bits.addr 889 io.sbuffer(i).bits.vaddr := dataBuffer.io.deq(i).bits.vaddr 890 io.sbuffer(i).bits.data := dataBuffer.io.deq(i).bits.data 891 io.sbuffer(i).bits.mask := dataBuffer.io.deq(i).bits.mask 892 io.sbuffer(i).bits.wline := dataBuffer.io.deq(i).bits.wline 893 io.sbuffer(i).bits.prefetch := dataBuffer.io.deq(i).bits.prefetch 894 io.sbuffer(i).bits.vecValid := dataBuffer.io.deq(i).bits.vecValid 895 896 // io.sbuffer(i).fire is RegNexted, as sbuffer data write takes 2 cycles. 897 // Before data write finish, sbuffer is unable to provide store to load 898 // forward data. As an workaround, deqPtrExt and allocated flag update 899 // is delayed so that load can get the right data from store queue. 900 val ptr = dataBuffer.io.deq(i).bits.sqPtr.value 901 when (RegNext(io.sbuffer(i).fire)) { 902 allocated(RegEnable(ptr, io.sbuffer(i).fire)) := false.B 903 XSDebug("sbuffer "+i+" fire: ptr %d\n", ptr) 904 } 905 } 906 (1 until EnsbufferWidth).foreach(i => when(io.sbuffer(i).fire) { assert(io.sbuffer(i - 1).fire) }) 907 if (coreParams.dcacheParametersOpt.isEmpty) { 908 for (i <- 0 until EnsbufferWidth) { 909 val ptr = deqPtrExt(i).value 910 val ram = DifftestMem(64L * 1024 * 1024 * 1024, 8) 911 val wen = allocated(ptr) && committed(ptr) && !mmio(ptr) 912 val waddr = ((paddrModule.io.rdata(i) - "h80000000".U) >> 3).asUInt 913 val wdata = Mux(paddrModule.io.rdata(i)(3), dataModule.io.rdata(i).data(127, 64), dataModule.io.rdata(i).data(63, 0)) 914 val wmask = Mux(paddrModule.io.rdata(i)(3), dataModule.io.rdata(i).mask(15, 8), dataModule.io.rdata(i).mask(7, 0)) 915 when (wen) { 916 ram.write(waddr, wdata.asTypeOf(Vec(8, UInt(8.W))), wmask.asBools) 917 } 918 } 919 } 920 921 // Read vaddr for mem exception 922 io.exceptionAddr.vaddr := exceptionBuffer.io.exceptionAddr.vaddr 923 924 // vector commit or replay from 925 val vecCommit = Wire(Vec(StoreQueueSize, Bool())) 926 for (i <- 0 until StoreQueueSize) { 927 val fbk = io.vecFeedback 928 vecCommit(i) := fbk.valid && fbk.bits.isCommit && uop(i).robIdx === fbk.bits.robidx && uop(i).uopIdx === fbk.bits.uopidx 929 when (vecCommit(i)) { 930 vecMbCommit(i) := true.B 931 } 932 } 933 934 // misprediction recovery / exception redirect 935 // invalidate sq term using robIdx 936 val needCancel = Wire(Vec(StoreQueueSize, Bool())) 937 for (i <- 0 until StoreQueueSize) { 938 needCancel(i) := uop(i).robIdx.needFlush(io.brqRedirect) && allocated(i) && !committed(i) 939 when (needCancel(i)) { 940 allocated(i) := false.B 941 } 942 } 943 944 /** 945* update pointers 946**/ 947 val enqCancelValid = canEnqueue.zip(io.enq.req).map{case (v , x) => 948 v && x.bits.robIdx.needFlush(io.brqRedirect) 949 } 950 val enqCancelNum = enqCancelValid.zip(io.enq.req).map{case (v, req) => 951 Mux(v, req.bits.numLsElem, 0.U) 952 } 953 val lastEnqCancel = RegNext(enqCancelNum.reduce(_ + _)) // 1 cycle after redirect 954 955 val lastCycleCancelCount = PopCount(RegNext(needCancel)) // 1 cycle after redirect 956 val lastCycleRedirect = RegNext(io.brqRedirect.valid) // 1 cycle after redirect 957 val enqNumber = validVStoreFlow.reduce(_ + _) 958 959 val lastlastCycleRedirect=RegNext(lastCycleRedirect)// 2 cycle after redirect 960 val redirectCancelCount = RegEnable(lastCycleCancelCount + lastEnqCancel, lastCycleRedirect) // 2 cycle after redirect 961 962 when (lastlastCycleRedirect) { 963 // we recover the pointers in 2 cycle after redirect for better timing 964 enqPtrExt := VecInit(enqPtrExt.map(_ - redirectCancelCount)) 965 }.otherwise { 966 // lastCycleRedirect.valid or nornal case 967 // when lastCycleRedirect.valid, enqNumber === 0.U, enqPtrExt will not change 968 enqPtrExt := VecInit(enqPtrExt.map(_ + enqNumber)) 969 } 970 assert(!(lastCycleRedirect && enqNumber =/= 0.U)) 971 972 deqPtrExt := deqPtrExtNext 973 rdataPtrExt := rdataPtrExtNext 974 975 // val dequeueCount = Mux(io.sbuffer(1).fire, 2.U, Mux(io.sbuffer(0).fire || io.mmioStout.fire, 1.U, 0.U)) 976 977 // If redirect at T0, sqCancelCnt is at T2 978 io.sqCancelCnt := redirectCancelCount 979 val ForceWriteUpper = Wire(UInt(log2Up(StoreQueueSize + 1).W)) 980 ForceWriteUpper := Constantin.createRecord("ForceWriteUpper_"+p(XSCoreParamsKey).HartId.toString(), initValue = 60.U) 981 val ForceWriteLower = Wire(UInt(log2Up(StoreQueueSize + 1).W)) 982 ForceWriteLower := Constantin.createRecord("ForceWriteLower_"+p(XSCoreParamsKey).HartId.toString(), initValue = 55.U) 983 984 val valid_cnt = PopCount(allocated) 985 io.force_write := RegNext(Mux(valid_cnt >= ForceWriteUpper, true.B, valid_cnt >= ForceWriteLower && io.force_write), init = false.B) 986 987 // io.sqempty will be used by sbuffer 988 // We delay it for 1 cycle for better timing 989 // When sbuffer need to check if it is empty, the pipeline is blocked, which means delay io.sqempty 990 // for 1 cycle will also promise that sq is empty in that cycle 991 io.sqEmpty := RegNext( 992 enqPtrExt(0).value === deqPtrExt(0).value && 993 enqPtrExt(0).flag === deqPtrExt(0).flag 994 ) 995 // perf counter 996 QueuePerf(StoreQueueSize, validCount, !allowEnqueue) 997 val vecValidVec = WireInit(VecInit((0 until StoreQueueSize).map(i => allocated(i) && isVec(i)))) 998 QueuePerf(StoreQueueSize, PopCount(vecValidVec), !allowEnqueue) 999 io.sqFull := !allowEnqueue 1000 XSPerfAccumulate("mmioCycle", uncacheState =/= s_idle) // lq is busy dealing with uncache req 1001 XSPerfAccumulate("mmioCnt", io.uncache.req.fire) 1002 XSPerfAccumulate("mmio_wb_success", io.mmioStout.fire || io.vecmmioStout.fire) 1003 XSPerfAccumulate("mmio_wb_blocked", (io.mmioStout.valid && !io.mmioStout.ready) || (io.vecmmioStout.valid && !io.vecmmioStout.ready)) 1004 XSPerfAccumulate("validEntryCnt", distanceBetween(enqPtrExt(0), deqPtrExt(0))) 1005 XSPerfAccumulate("cmtEntryCnt", distanceBetween(cmtPtrExt(0), deqPtrExt(0))) 1006 XSPerfAccumulate("nCmtEntryCnt", distanceBetween(enqPtrExt(0), cmtPtrExt(0))) 1007 1008 val perfValidCount = distanceBetween(enqPtrExt(0), deqPtrExt(0)) 1009 val perfEvents = Seq( 1010 ("mmioCycle ", uncacheState =/= s_idle), 1011 ("mmioCnt ", io.uncache.req.fire), 1012 ("mmio_wb_success", io.mmioStout.fire || io.vecmmioStout.fire), 1013 ("mmio_wb_blocked", (io.mmioStout.valid && !io.mmioStout.ready) || (io.vecmmioStout.valid && !io.vecmmioStout.ready)), 1014 ("stq_1_4_valid ", (perfValidCount < (StoreQueueSize.U/4.U))), 1015 ("stq_2_4_valid ", (perfValidCount > (StoreQueueSize.U/4.U)) & (perfValidCount <= (StoreQueueSize.U/2.U))), 1016 ("stq_3_4_valid ", (perfValidCount > (StoreQueueSize.U/2.U)) & (perfValidCount <= (StoreQueueSize.U*3.U/4.U))), 1017 ("stq_4_4_valid ", (perfValidCount > (StoreQueueSize.U*3.U/4.U))), 1018 ) 1019 generatePerfEvent() 1020 1021 // debug info 1022 XSDebug("enqPtrExt %d:%d deqPtrExt %d:%d\n", enqPtrExt(0).flag, enqPtr, deqPtrExt(0).flag, deqPtr) 1023 1024 def PrintFlag(flag: Bool, name: String): Unit = { 1025 when(flag) { 1026 XSDebug(false, true.B, name) 1027 }.otherwise { 1028 XSDebug(false, true.B, " ") 1029 } 1030 } 1031 1032 for (i <- 0 until StoreQueueSize) { 1033 XSDebug(i + ": pc %x va %x pa %x data %x ", 1034 uop(i).pc, 1035 debug_vaddr(i), 1036 debug_paddr(i), 1037 debug_data(i) 1038 ) 1039 PrintFlag(allocated(i), "a") 1040 PrintFlag(allocated(i) && addrvalid(i), "a") 1041 PrintFlag(allocated(i) && datavalid(i), "d") 1042 PrintFlag(allocated(i) && committed(i), "c") 1043 PrintFlag(allocated(i) && pending(i), "p") 1044 PrintFlag(allocated(i) && mmio(i), "m") 1045 XSDebug(false, true.B, "\n") 1046 } 1047 1048} 1049