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