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