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