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