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