1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.mem 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import utils._ 23import utility._ 24import xiangshan._ 25import xiangshan.cache._ 26import xiangshan.cache.{DCacheLineIO, DCacheWordIO, MemoryOpConstants} 27import xiangshan.backend._ 28import xiangshan.backend.rob.{RobLsqIO, RobPtr} 29import difftest._ 30import device.RAMHelper 31import xiangshan.backend.Bundles.{DynInst, MemExuOutput} 32 33class SqPtr(implicit p: Parameters) extends CircularQueuePtr[SqPtr]( 34 p => p(XSCoreParamsKey).StoreQueueSize 35){ 36} 37 38object SqPtr { 39 def apply(f: Bool, v: UInt)(implicit p: Parameters): SqPtr = { 40 val ptr = Wire(new SqPtr) 41 ptr.flag := f 42 ptr.value := v 43 ptr 44 } 45} 46 47class SqEnqIO(implicit p: Parameters) extends MemBlockBundle { 48 val canAccept = Output(Bool()) 49 val lqCanAccept = Input(Bool()) 50 val needAlloc = Vec(LSQEnqWidth, Input(Bool())) 51 val req = Vec(LSQEnqWidth, Flipped(ValidIO(new DynInst))) 52 val resp = Vec(LSQEnqWidth, Output(new SqPtr)) 53} 54 55class DataBufferEntry (implicit p: Parameters) extends DCacheBundle { 56 val addr = UInt(PAddrBits.W) 57 val vaddr = UInt(VAddrBits.W) 58 val data = UInt(VLEN.W) 59 val mask = UInt((VLEN/8).W) 60 val wline = Bool() 61 val sqPtr = new SqPtr 62 val prefetch = Bool() 63} 64 65// Store Queue 66class StoreQueue(implicit p: Parameters) extends XSModule 67 with HasDCacheParameters with HasCircularQueuePtrHelper with HasPerfEvents { 68 val io = IO(new Bundle() { 69 val hartId = Input(UInt(8.W)) 70 val enq = new SqEnqIO 71 val brqRedirect = Flipped(ValidIO(new Redirect)) 72 val storeAddrIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) // store addr, data is not included 73 val storeAddrInRe = Vec(StorePipelineWidth, Input(new LsPipelineBundle())) // store more mmio and exception 74 val storeDataIn = Vec(StorePipelineWidth, Flipped(Valid(new MemExuOutput))) // store data, send to sq from rs 75 val storeMaskIn = Vec(StorePipelineWidth, Flipped(Valid(new StoreMaskBundle))) // store mask, send to sq from rs 76 val sbuffer = Vec(EnsbufferWidth, Decoupled(new DCacheWordReqWithVaddrAndPfFlag)) // write committed store to sbuffer 77 val uncacheOutstanding = Input(Bool()) 78 val mmioStout = DecoupledIO(new MemExuOutput) // writeback uncached store 79 val forward = Vec(LoadPipelineWidth, Flipped(new PipeLoadForwardQueryIO)) 80 val rob = Flipped(new RobLsqIO) 81 val uncache = new UncacheWordIO 82 // val refill = Flipped(Valid(new DCacheLineReq )) 83 val exceptionAddr = new ExceptionAddrIO 84 val sqEmpty = Output(Bool()) 85 val stAddrReadySqPtr = Output(new SqPtr) 86 val stAddrReadyVec = Output(Vec(StoreQueueSize, Bool())) 87 val stDataReadySqPtr = Output(new SqPtr) 88 val stDataReadyVec = Output(Vec(StoreQueueSize, Bool())) 89 val stIssuePtr = Output(new SqPtr) 90 val sqDeqPtr = Output(new SqPtr) 91 val sqFull = Output(Bool()) 92 val sqCancelCnt = Output(UInt(log2Up(StoreQueueSize + 1).W)) 93 val sqDeq = Output(UInt(log2Ceil(EnsbufferWidth + 1).W)) 94 val force_write = Output(Bool()) 95 val vecStoreRetire = Flipped(ValidIO(new SqPtr)) 96 }) 97 98 println("StoreQueue: size:" + StoreQueueSize) 99 100 // data modules 101 val uop = Reg(Vec(StoreQueueSize, new DynInst)) 102 // val data = Reg(Vec(StoreQueueSize, new LsqEntry)) 103 val dataModule = Module(new SQDataModule( 104 numEntries = StoreQueueSize, 105 numRead = EnsbufferWidth, 106 numWrite = StorePipelineWidth, 107 numForward = LoadPipelineWidth 108 )) 109 dataModule.io := DontCare 110 val paddrModule = Module(new SQAddrModule( 111 dataWidth = PAddrBits, 112 numEntries = StoreQueueSize, 113 numRead = EnsbufferWidth, 114 numWrite = StorePipelineWidth, 115 numForward = LoadPipelineWidth 116 )) 117 paddrModule.io := DontCare 118 val vaddrModule = Module(new SQAddrModule( 119 dataWidth = VAddrBits, 120 numEntries = StoreQueueSize, 121 numRead = EnsbufferWidth + 1, // sbuffer + badvaddr 1 (TODO) 122 numWrite = StorePipelineWidth, 123 numForward = LoadPipelineWidth 124 )) 125 vaddrModule.io := DontCare 126 val dataBuffer = Module(new DatamoduleResultBuffer(new DataBufferEntry)) 127 val debug_paddr = Reg(Vec(StoreQueueSize, UInt((PAddrBits).W))) 128 val debug_vaddr = Reg(Vec(StoreQueueSize, UInt((VAddrBits).W))) 129 val debug_data = Reg(Vec(StoreQueueSize, UInt((XLEN).W))) 130 131 // state & misc 132 val allocated = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // sq entry has been allocated 133 val addrvalid = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // non-mmio addr is valid 134 val datavalid = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // non-mmio data is valid 135 val allvalid = VecInit((0 until StoreQueueSize).map(i => addrvalid(i) && datavalid(i))) // non-mmio data & addr is valid 136 val committed = Reg(Vec(StoreQueueSize, Bool())) // inst has been committed by rob 137 val pending = Reg(Vec(StoreQueueSize, Bool())) // mmio pending: inst is an mmio inst, it will not be executed until it reachs the end of rob 138 val mmio = Reg(Vec(StoreQueueSize, Bool())) // mmio: inst is an mmio inst 139 val atomic = Reg(Vec(StoreQueueSize, Bool())) 140 val prefetch = RegInit(VecInit(List.fill(StoreQueueSize)(false.B))) // need prefetch when committing this store to sbuffer? 141 val vec = Reg(Vec(StoreQueueSize, Bool())) 142 143 // ptr 144 val enqPtrExt = RegInit(VecInit((0 until io.enq.req.length).map(_.U.asTypeOf(new SqPtr)))) 145 val rdataPtrExt = RegInit(VecInit((0 until EnsbufferWidth).map(_.U.asTypeOf(new SqPtr)))) 146 val deqPtrExt = RegInit(VecInit((0 until EnsbufferWidth).map(_.U.asTypeOf(new SqPtr)))) 147 val cmtPtrExt = RegInit(VecInit((0 until CommitWidth).map(_.U.asTypeOf(new SqPtr)))) 148 val addrReadyPtrExt = RegInit(0.U.asTypeOf(new SqPtr)) 149 val dataReadyPtrExt = RegInit(0.U.asTypeOf(new SqPtr)) 150 val validCounter = RegInit(0.U(log2Ceil(VirtualLoadQueueSize + 1).W)) 151 152 val enqPtr = enqPtrExt(0).value 153 val deqPtr = deqPtrExt(0).value 154 val cmtPtr = cmtPtrExt(0).value 155 156 val validCount = distanceBetween(enqPtrExt(0), deqPtrExt(0)) 157 val allowEnqueue = validCount <= (StoreQueueSize - StorePipelineWidth).U 158 159 val deqMask = UIntToMask(deqPtr, StoreQueueSize) 160 val enqMask = UIntToMask(enqPtr, StoreQueueSize) 161 162 val commitCount = RegNext(io.rob.scommit) 163 164 // store can be committed by ROB 165 io.rob.mmio := DontCare 166 io.rob.uop := DontCare 167 168 // Read dataModule 169 assert(EnsbufferWidth <= 2) 170 // rdataPtrExtNext and rdataPtrExtNext+1 entry will be read from dataModule 171 val rdataPtrExtNext = WireInit(Mux(dataBuffer.io.enq(1).fire, 172 VecInit(rdataPtrExt.map(_ + 2.U)), 173 Mux(dataBuffer.io.enq(0).fire || io.mmioStout.fire || io.vecStoreRetire.valid, 174 VecInit(rdataPtrExt.map(_ + 1.U)), 175 rdataPtrExt 176 ) 177 )) 178 179 // deqPtrExtNext traces which inst is about to leave store queue 180 // 181 // io.sbuffer(i).fire is RegNexted, as sbuffer data write takes 2 cycles. 182 // Before data write finish, sbuffer is unable to provide store to load 183 // forward data. As an workaround, deqPtrExt and allocated flag update 184 // is delayed so that load can get the right data from store queue. 185 // 186 // Modify deqPtrExtNext and io.sqDeq with care! 187 val deqPtrExtNext = Mux(RegNext(io.sbuffer(1).fire), 188 VecInit(deqPtrExt.map(_ + 2.U)), 189 Mux(RegNext(io.sbuffer(0).fire) || io.mmioStout.fire || io.vecStoreRetire.valid, 190 VecInit(deqPtrExt.map(_ + 1.U)), 191 deqPtrExt 192 ) 193 ) 194 io.sqDeq := RegNext(Mux(RegNext(io.sbuffer(1).fire), 2.U, 195 Mux(RegNext(io.sbuffer(0).fire) || io.mmioStout.fire || io.vecStoreRetire.valid, 1.U, 0.U) 196 )) 197 assert(!RegNext(RegNext(io.sbuffer(0).fire) && io.mmioStout.fire)) 198 199 for (i <- 0 until EnsbufferWidth) { 200 dataModule.io.raddr(i) := rdataPtrExtNext(i).value 201 paddrModule.io.raddr(i) := rdataPtrExtNext(i).value 202 vaddrModule.io.raddr(i) := rdataPtrExtNext(i).value 203 } 204 205 // no inst will be committed 1 cycle before tval update 206 vaddrModule.io.raddr(EnsbufferWidth) := (cmtPtrExt(0) + commitCount).value 207 208 /** 209 * Enqueue at dispatch 210 * 211 * Currently, StoreQueue only allows enqueue when #emptyEntries > EnqWidth 212 */ 213 io.enq.canAccept := allowEnqueue 214 val canEnqueue = io.enq.req.map(_.valid) 215 val enqCancel = io.enq.req.map(_.bits.robIdx.needFlush(io.brqRedirect)) 216 for (i <- 0 until io.enq.req.length) { 217 val offset = if (i == 0) 0.U else PopCount(io.enq.needAlloc.take(i)) 218 val sqIdx = enqPtrExt(offset) 219 val index = io.enq.req(i).bits.sqIdx.value 220 when (canEnqueue(i) && !enqCancel(i)) { 221 uop(index) := io.enq.req(i).bits 222 // NOTE: the index will be used when replay 223 uop(index).sqIdx := sqIdx 224 allocated(index) := true.B 225 datavalid(index) := false.B 226 addrvalid(index) := false.B 227 committed(index) := false.B 228 pending(index) := false.B 229 prefetch(index) := false.B 230 mmio(index) := false.B 231 vec(index) := io.enq.req(i).bits.instr(6, 0) === "b0100111".U // TODO 232 233 XSError(!io.enq.canAccept || !io.enq.lqCanAccept, s"must accept $i\n") 234 XSError(index =/= sqIdx.value, s"must be the same entry $i\n") 235 } 236 io.enq.resp(i) := sqIdx 237 } 238 XSDebug(p"(ready, valid): ${io.enq.canAccept}, ${Binary(Cat(io.enq.req.map(_.valid)))}\n") 239 240 /** 241 * Update addr/dataReadyPtr when issue from rs 242 */ 243 // update issuePtr 244 val IssuePtrMoveStride = 4 245 require(IssuePtrMoveStride >= 2) 246 247 val addrReadyLookupVec = (0 until IssuePtrMoveStride).map(addrReadyPtrExt + _.U) 248 val addrReadyLookup = addrReadyLookupVec.map(ptr => allocated(ptr.value) && (mmio(ptr.value) || addrvalid(ptr.value) || vec(ptr.value)) && ptr =/= enqPtrExt(0)) 249 val nextAddrReadyPtr = addrReadyPtrExt + PriorityEncoder(VecInit(addrReadyLookup.map(!_) :+ true.B)) 250 addrReadyPtrExt := nextAddrReadyPtr 251 252 (0 until StoreQueueSize).map(i => { 253 io.stAddrReadyVec(i) := RegNext(allocated(i) && (mmio(i) || addrvalid(i))) 254 }) 255 256 when (io.brqRedirect.valid) { 257 addrReadyPtrExt := Mux( 258 isAfter(cmtPtrExt(0), deqPtrExt(0)), 259 cmtPtrExt(0), 260 deqPtrExtNext(0) // for mmio insts, deqPtr may be ahead of cmtPtr 261 ) 262 } 263 264 io.stAddrReadySqPtr := addrReadyPtrExt 265 266 // update 267 val dataReadyLookupVec = (0 until IssuePtrMoveStride).map(dataReadyPtrExt + _.U) 268 val dataReadyLookup = dataReadyLookupVec.map(ptr => allocated(ptr.value) && (mmio(ptr.value) || datavalid(ptr.value) || vec(ptr.value)) && ptr =/= enqPtrExt(0)) 269 val nextDataReadyPtr = dataReadyPtrExt + PriorityEncoder(VecInit(dataReadyLookup.map(!_) :+ true.B)) 270 dataReadyPtrExt := nextDataReadyPtr 271 272 (0 until StoreQueueSize).map(i => { 273 io.stDataReadyVec(i) := RegNext(allocated(i) && (mmio(i) || datavalid(i))) 274 }) 275 276 when (io.brqRedirect.valid) { 277 dataReadyPtrExt := Mux( 278 isAfter(cmtPtrExt(0), deqPtrExt(0)), 279 cmtPtrExt(0), 280 deqPtrExtNext(0) // for mmio insts, deqPtr may be ahead of cmtPtr 281 ) 282 } 283 284 io.stDataReadySqPtr := dataReadyPtrExt 285 io.stIssuePtr := enqPtrExt(0) 286 io.sqDeqPtr := deqPtrExt(0) 287 288 /** 289 * Writeback store from store units 290 * 291 * Most store instructions writeback to regfile in the previous cycle. 292 * However, 293 * (1) For an mmio instruction with exceptions, we need to mark it as addrvalid 294 * (in this way it will trigger an exception when it reaches ROB's head) 295 * instead of pending to avoid sending them to lower level. 296 * (2) For an mmio instruction without exceptions, we mark it as pending. 297 * When the instruction reaches ROB's head, StoreQueue sends it to uncache channel. 298 * Upon receiving the response, StoreQueue writes back the instruction 299 * through arbiter with store units. It will later commit as normal. 300 */ 301 302 // Write addr to sq 303 for (i <- 0 until StorePipelineWidth) { 304 paddrModule.io.wen(i) := false.B 305 vaddrModule.io.wen(i) := false.B 306 dataModule.io.mask.wen(i) := false.B 307 val stWbIndex = io.storeAddrIn(i).bits.uop.sqIdx.value 308 when (io.storeAddrIn(i).fire) { 309 val addr_valid = !io.storeAddrIn(i).bits.miss 310 addrvalid(stWbIndex) := addr_valid //!io.storeAddrIn(i).bits.mmio 311 // pending(stWbIndex) := io.storeAddrIn(i).bits.mmio 312 313 paddrModule.io.waddr(i) := stWbIndex 314 paddrModule.io.wdata(i) := io.storeAddrIn(i).bits.paddr 315 paddrModule.io.wmask(i) := io.storeAddrIn(i).bits.mask 316 paddrModule.io.wlineflag(i) := io.storeAddrIn(i).bits.wlineflag 317 paddrModule.io.wen(i) := true.B 318 319 vaddrModule.io.waddr(i) := stWbIndex 320 vaddrModule.io.wdata(i) := io.storeAddrIn(i).bits.vaddr 321 vaddrModule.io.wmask(i) := io.storeAddrIn(i).bits.mask 322 vaddrModule.io.wlineflag(i) := io.storeAddrIn(i).bits.wlineflag 323 vaddrModule.io.wen(i) := true.B 324 325 debug_paddr(paddrModule.io.waddr(i)) := paddrModule.io.wdata(i) 326 327 // mmio(stWbIndex) := io.storeAddrIn(i).bits.mmio 328 329 uop(stWbIndex) := io.storeAddrIn(i).bits.uop 330 uop(stWbIndex).debugInfo := io.storeAddrIn(i).bits.uop.debugInfo 331 XSInfo("store addr write to sq idx %d pc 0x%x miss:%d vaddr %x paddr %x mmio %x\n", 332 io.storeAddrIn(i).bits.uop.sqIdx.value, 333 io.storeAddrIn(i).bits.uop.pc, 334 io.storeAddrIn(i).bits.miss, 335 io.storeAddrIn(i).bits.vaddr, 336 io.storeAddrIn(i).bits.paddr, 337 io.storeAddrIn(i).bits.mmio 338 ) 339 } 340 341 // re-replinish mmio, for pma/pmp will get mmio one cycle later 342 val storeAddrInFireReg = RegNext(io.storeAddrIn(i).fire && !io.storeAddrIn(i).bits.miss) 343 val stWbIndexReg = RegNext(stWbIndex) 344 when (storeAddrInFireReg) { 345 pending(stWbIndexReg) := io.storeAddrInRe(i).mmio 346 mmio(stWbIndexReg) := io.storeAddrInRe(i).mmio 347 atomic(stWbIndexReg) := io.storeAddrInRe(i).atomic 348 } 349 // dcache miss info (one cycle later than storeIn) 350 // if dcache report a miss in sta pipeline, this store will trigger a prefetch when committing to sbuffer (if EnableAtCommitMissTrigger) 351 when (storeAddrInFireReg) { 352 prefetch(stWbIndexReg) := io.storeAddrInRe(i).miss 353 } 354 355 when(vaddrModule.io.wen(i)){ 356 debug_vaddr(vaddrModule.io.waddr(i)) := vaddrModule.io.wdata(i) 357 } 358 } 359 360 // Write data to sq 361 // Now store data pipeline is actually 2 stages 362 for (i <- 0 until StorePipelineWidth) { 363 dataModule.io.data.wen(i) := false.B 364 val stWbIndex = io.storeDataIn(i).bits.uop.sqIdx.value 365 // sq data write takes 2 cycles: 366 // sq data write s0 367 when (io.storeDataIn(i).fire) { 368 // send data write req to data module 369 dataModule.io.data.waddr(i) := stWbIndex 370 dataModule.io.data.wdata(i) := Mux(io.storeDataIn(i).bits.uop.fuOpType === LSUOpType.cbo_zero, 371 0.U, 372 genWdata(io.storeDataIn(i).bits.data, io.storeDataIn(i).bits.uop.fuOpType(1,0)) 373 ) 374 dataModule.io.data.wen(i) := true.B 375 376 debug_data(dataModule.io.data.waddr(i)) := dataModule.io.data.wdata(i) 377 378 XSInfo("store data write to sq idx %d pc 0x%x data %x -> %x\n", 379 io.storeDataIn(i).bits.uop.sqIdx.value, 380 io.storeDataIn(i).bits.uop.pc, 381 io.storeDataIn(i).bits.data, 382 dataModule.io.data.wdata(i) 383 ) 384 } 385 // sq data write s1 386 when ( 387 RegNext(io.storeDataIn(i).fire) 388 // && !RegNext(io.storeDataIn(i).bits.uop).robIdx.needFlush(io.brqRedirect) 389 ) { 390 datavalid(RegNext(stWbIndex)) := true.B 391 } 392 } 393 394 // Write mask to sq 395 for (i <- 0 until StorePipelineWidth) { 396 // sq mask write s0 397 when (io.storeMaskIn(i).fire) { 398 // send data write req to data module 399 dataModule.io.mask.waddr(i) := io.storeMaskIn(i).bits.sqIdx.value 400 dataModule.io.mask.wdata(i) := io.storeMaskIn(i).bits.mask 401 dataModule.io.mask.wen(i) := true.B 402 } 403 } 404 405 /** 406 * load forward query 407 * 408 * Check store queue for instructions that is older than the load. 409 * The response will be valid at the next cycle after req. 410 */ 411 // check over all lq entries and forward data from the first matched store 412 for (i <- 0 until LoadPipelineWidth) { 413 // Compare deqPtr (deqPtr) and forward.sqIdx, we have two cases: 414 // (1) if they have the same flag, we need to check range(tail, sqIdx) 415 // (2) if they have different flags, we need to check range(tail, VirtualLoadQueueSize) and range(0, sqIdx) 416 // Forward1: Mux(same_flag, range(tail, sqIdx), range(tail, VirtualLoadQueueSize)) 417 // Forward2: Mux(same_flag, 0.U, range(0, sqIdx) ) 418 // i.e. forward1 is the target entries with the same flag bits and forward2 otherwise 419 val differentFlag = deqPtrExt(0).flag =/= io.forward(i).sqIdx.flag 420 val forwardMask = io.forward(i).sqIdxMask 421 // all addrvalid terms need to be checked 422 val addrValidVec = WireInit(VecInit((0 until StoreQueueSize).map(i => addrvalid(i) && allocated(i)))) 423 val dataValidVec = WireInit(VecInit((0 until StoreQueueSize).map(i => datavalid(i)))) 424 val allValidVec = WireInit(VecInit((0 until StoreQueueSize).map(i => addrvalid(i) && datavalid(i) && allocated(i)))) 425 426 val storeSetHitVec = 427 if (LFSTEnable) { 428 WireInit(VecInit((0 until StoreQueueSize).map(j => io.forward(i).uop.loadWaitBit && uop(j).robIdx === io.forward(i).uop.waitForRobIdx))) 429 } else { 430 WireInit(VecInit((0 until StoreQueueSize).map(j => uop(j).storeSetHit && uop(j).ssid === io.forward(i).uop.ssid))) 431 } 432 433 val forwardMask1 = Mux(differentFlag, ~deqMask, deqMask ^ forwardMask) 434 val forwardMask2 = Mux(differentFlag, forwardMask, 0.U(StoreQueueSize.W)) 435 val canForward1 = forwardMask1 & allValidVec.asUInt 436 val canForward2 = forwardMask2 & allValidVec.asUInt 437 val needForward = Mux(differentFlag, ~deqMask | forwardMask, deqMask ^ forwardMask) 438 439 XSDebug(p"$i f1 ${Binary(canForward1)} f2 ${Binary(canForward2)} " + 440 p"sqIdx ${io.forward(i).sqIdx} pa ${Hexadecimal(io.forward(i).paddr)}\n" 441 ) 442 443 // do real fwd query (cam lookup in load_s1) 444 dataModule.io.needForward(i)(0) := canForward1 & vaddrModule.io.forwardMmask(i).asUInt 445 dataModule.io.needForward(i)(1) := canForward2 & vaddrModule.io.forwardMmask(i).asUInt 446 447 vaddrModule.io.forwardMdata(i) := io.forward(i).vaddr 448 vaddrModule.io.forwardDataMask(i) := io.forward(i).mask 449 paddrModule.io.forwardMdata(i) := io.forward(i).paddr 450 paddrModule.io.forwardDataMask(i) := io.forward(i).mask 451 452 453 // vaddr cam result does not equal to paddr cam result 454 // replay needed 455 // val vpmaskNotEqual = ((paddrModule.io.forwardMmask(i).asUInt ^ vaddrModule.io.forwardMmask(i).asUInt) & needForward) =/= 0.U 456 // val vaddrMatchFailed = vpmaskNotEqual && io.forward(i).valid 457 val vpmaskNotEqual = ( 458 (RegNext(paddrModule.io.forwardMmask(i).asUInt) ^ RegNext(vaddrModule.io.forwardMmask(i).asUInt)) & 459 RegNext(needForward) & 460 RegNext(addrValidVec.asUInt) 461 ) =/= 0.U 462 val vaddrMatchFailed = vpmaskNotEqual && RegNext(io.forward(i).valid) 463 when (vaddrMatchFailed) { 464 XSInfo("vaddrMatchFailed: pc %x pmask %x vmask %x\n", 465 RegNext(io.forward(i).uop.pc), 466 RegNext(needForward & paddrModule.io.forwardMmask(i).asUInt), 467 RegNext(needForward & vaddrModule.io.forwardMmask(i).asUInt) 468 ); 469 } 470 XSPerfAccumulate("vaddr_match_failed", vpmaskNotEqual) 471 XSPerfAccumulate("vaddr_match_really_failed", vaddrMatchFailed) 472 473 // Fast forward mask will be generated immediately (load_s1) 474 io.forward(i).forwardMaskFast := dataModule.io.forwardMaskFast(i) 475 476 // Forward result will be generated 1 cycle later (load_s2) 477 io.forward(i).forwardMask := dataModule.io.forwardMask(i) 478 io.forward(i).forwardData := dataModule.io.forwardData(i) 479 // If addr match, data not ready, mark it as dataInvalid 480 // load_s1: generate dataInvalid in load_s1 to set fastUop 481 val dataInvalidMask1 = (addrValidVec.asUInt & ~dataValidVec.asUInt & vaddrModule.io.forwardMmask(i).asUInt & forwardMask1.asUInt) 482 val dataInvalidMask2 = (addrValidVec.asUInt & ~dataValidVec.asUInt & vaddrModule.io.forwardMmask(i).asUInt & forwardMask2.asUInt) 483 val dataInvalidMask = dataInvalidMask1 | dataInvalidMask2 484 io.forward(i).dataInvalidFast := dataInvalidMask.orR 485 486 // make chisel happy 487 val dataInvalidMask1Reg = Wire(UInt(StoreQueueSize.W)) 488 dataInvalidMask1Reg := RegNext(dataInvalidMask1) 489 // make chisel happy 490 val dataInvalidMask2Reg = Wire(UInt(StoreQueueSize.W)) 491 dataInvalidMask2Reg := RegNext(dataInvalidMask2) 492 val dataInvalidMaskReg = dataInvalidMask1Reg | dataInvalidMask2Reg 493 494 // If SSID match, address not ready, mark it as addrInvalid 495 // load_s2: generate addrInvalid 496 val addrInvalidMask1 = (~addrValidVec.asUInt & storeSetHitVec.asUInt & forwardMask1.asUInt) 497 val addrInvalidMask2 = (~addrValidVec.asUInt & storeSetHitVec.asUInt & forwardMask2.asUInt) 498 // make chisel happy 499 val addrInvalidMask1Reg = Wire(UInt(StoreQueueSize.W)) 500 addrInvalidMask1Reg := RegNext(addrInvalidMask1) 501 // make chisel happy 502 val addrInvalidMask2Reg = Wire(UInt(StoreQueueSize.W)) 503 addrInvalidMask2Reg := RegNext(addrInvalidMask2) 504 val addrInvalidMaskReg = addrInvalidMask1Reg | addrInvalidMask2Reg 505 506 // load_s2 507 io.forward(i).dataInvalid := RegNext(io.forward(i).dataInvalidFast) 508 // check if vaddr forward mismatched 509 io.forward(i).matchInvalid := vaddrMatchFailed 510 511 // data invalid sq index 512 // check whether false fail 513 // check flag 514 val s2_differentFlag = RegNext(differentFlag) 515 val s2_enqPtrExt = RegNext(enqPtrExt(0)) 516 val s2_deqPtrExt = RegNext(deqPtrExt(0)) 517 518 // addr invalid sq index 519 // make chisel happy 520 val addrInvalidMaskRegWire = Wire(UInt(StoreQueueSize.W)) 521 addrInvalidMaskRegWire := addrInvalidMaskReg 522 val addrInvalidFlag = addrInvalidMaskRegWire.orR 523 val hasInvalidAddr = (~addrValidVec.asUInt & needForward).orR 524 525 val addrInvalidSqIdx1 = OHToUInt(Reverse(PriorityEncoderOH(Reverse(addrInvalidMask1Reg)))) 526 val addrInvalidSqIdx2 = OHToUInt(Reverse(PriorityEncoderOH(Reverse(addrInvalidMask2Reg)))) 527 val addrInvalidSqIdx = Mux(addrInvalidMask2Reg.orR, addrInvalidSqIdx2, addrInvalidSqIdx1) 528 529 // store-set content management 530 // +-----------------------+ 531 // | Search a SSID for the | 532 // | load operation | 533 // +-----------------------+ 534 // | 535 // V 536 // +-------------------+ 537 // | load wait strict? | 538 // +-------------------+ 539 // | 540 // V 541 // +----------------------+ 542 // Set| |Clean 543 // V V 544 // +------------------------+ +------------------------------+ 545 // | Waiting for all older | | Wait until the corresponding | 546 // | stores operations | | older store operations | 547 // +------------------------+ +------------------------------+ 548 549 550 551 when (RegNext(io.forward(i).uop.loadWaitStrict)) { 552 io.forward(i).addrInvalidSqIdx := RegNext(io.forward(i).uop.sqIdx - 1.U) 553 } .elsewhen (addrInvalidFlag) { 554 io.forward(i).addrInvalidSqIdx.flag := Mux(!s2_differentFlag || addrInvalidSqIdx >= s2_deqPtrExt.value, s2_deqPtrExt.flag, s2_enqPtrExt.flag) 555 io.forward(i).addrInvalidSqIdx.value := addrInvalidSqIdx 556 } .otherwise { 557 // may be store inst has been written to sbuffer already. 558 io.forward(i).addrInvalidSqIdx := RegNext(io.forward(i).uop.sqIdx) 559 } 560 io.forward(i).addrInvalid := Mux(RegNext(io.forward(i).uop.loadWaitStrict), RegNext(hasInvalidAddr), addrInvalidFlag) 561 562 // data invalid sq index 563 // make chisel happy 564 val dataInvalidMaskRegWire = Wire(UInt(StoreQueueSize.W)) 565 dataInvalidMaskRegWire := dataInvalidMaskReg 566 val dataInvalidFlag = dataInvalidMaskRegWire.orR 567 568 val dataInvalidSqIdx1 = OHToUInt(Reverse(PriorityEncoderOH(Reverse(dataInvalidMask1Reg)))) 569 val dataInvalidSqIdx2 = OHToUInt(Reverse(PriorityEncoderOH(Reverse(dataInvalidMask2Reg)))) 570 val dataInvalidSqIdx = Mux(dataInvalidMask2Reg.orR, dataInvalidSqIdx2, dataInvalidSqIdx1) 571 572 when (dataInvalidFlag) { 573 io.forward(i).dataInvalidSqIdx.flag := Mux(!s2_differentFlag || dataInvalidSqIdx >= s2_deqPtrExt.value, s2_deqPtrExt.flag, s2_enqPtrExt.flag) 574 io.forward(i).dataInvalidSqIdx.value := dataInvalidSqIdx 575 } .otherwise { 576 // may be store inst has been written to sbuffer already. 577 io.forward(i).dataInvalidSqIdx := RegNext(io.forward(i).uop.sqIdx) 578 } 579 } 580 581 /** 582 * Memory mapped IO / other uncached operations 583 * 584 * States: 585 * (1) writeback from store units: mark as pending 586 * (2) when they reach ROB's head, they can be sent to uncache channel 587 * (3) response from uncache channel: mark as datavalidmask.wen 588 * (4) writeback to ROB (and other units): mark as writebacked 589 * (5) ROB commits the instruction: same as normal instructions 590 */ 591 //(2) when they reach ROB's head, they can be sent to uncache channel 592 val s_idle :: s_req :: s_resp :: s_wb :: s_wait :: Nil = Enum(5) 593 val uncacheState = RegInit(s_idle) 594 switch(uncacheState) { 595 is(s_idle) { 596 when(RegNext(io.rob.pendingst && pending(deqPtr) && allocated(deqPtr) && datavalid(deqPtr) && addrvalid(deqPtr))) { 597 uncacheState := s_req 598 } 599 } 600 is(s_req) { 601 when (io.uncache.req.fire) { 602 when (io.uncacheOutstanding) { 603 uncacheState := s_wb 604 } .otherwise { 605 uncacheState := s_resp 606 } 607 } 608 } 609 is(s_resp) { 610 when(io.uncache.resp.fire) { 611 uncacheState := s_wb 612 } 613 } 614 is(s_wb) { 615 when (io.mmioStout.fire) { 616 uncacheState := s_wait 617 } 618 } 619 is(s_wait) { 620 when(commitCount > 0.U) { 621 uncacheState := s_idle // ready for next mmio 622 } 623 } 624 } 625 io.uncache.req.valid := uncacheState === s_req 626 627 io.uncache.req.bits := DontCare 628 io.uncache.req.bits.cmd := MemoryOpConstants.M_XWR 629 io.uncache.req.bits.addr := paddrModule.io.rdata(0) // data(deqPtr) -> rdata(0) 630 io.uncache.req.bits.data := shiftDataToLow(paddrModule.io.rdata(0), dataModule.io.rdata(0).data) 631 io.uncache.req.bits.mask := shiftMaskToLow(paddrModule.io.rdata(0), dataModule.io.rdata(0).mask) 632 633 // CBO op type check can be delayed for 1 cycle, 634 // as uncache op will not start in s_idle 635 val cbo_mmio_addr = paddrModule.io.rdata(0) >> 2 << 2 // clear lowest 2 bits for op 636 val cbo_mmio_op = 0.U //TODO 637 val cbo_mmio_data = cbo_mmio_addr | cbo_mmio_op 638 when(RegNext(LSUOpType.isCbo(uop(deqPtr).fuOpType))){ 639 io.uncache.req.bits.addr := DontCare // TODO 640 io.uncache.req.bits.data := paddrModule.io.rdata(0) 641 io.uncache.req.bits.mask := DontCare // TODO 642 } 643 644 io.uncache.req.bits.atomic := atomic(RegNext(rdataPtrExtNext(0)).value) 645 646 when(io.uncache.req.fire){ 647 // mmio store should not be committed until uncache req is sent 648 pending(deqPtr) := false.B 649 650 XSDebug( 651 p"uncache req: pc ${Hexadecimal(uop(deqPtr).pc)} " + 652 p"addr ${Hexadecimal(io.uncache.req.bits.addr)} " + 653 p"data ${Hexadecimal(io.uncache.req.bits.data)} " + 654 p"op ${Hexadecimal(io.uncache.req.bits.cmd)} " + 655 p"mask ${Hexadecimal(io.uncache.req.bits.mask)}\n" 656 ) 657 } 658 659 // (3) response from uncache channel: mark as datavalid 660 io.uncache.resp.ready := true.B 661 662 // (4) writeback to ROB (and other units): mark as writebacked 663 io.mmioStout.valid := uncacheState === s_wb 664 io.mmioStout.bits.uop := uop(deqPtr) 665 io.mmioStout.bits.uop.sqIdx := deqPtrExt(0) 666 io.mmioStout.bits.data := shiftDataToLow(paddrModule.io.rdata(0), dataModule.io.rdata(0).data) // dataModule.io.rdata.read(deqPtr) 667 io.mmioStout.bits.debug.isMMIO := true.B 668 io.mmioStout.bits.debug.paddr := DontCare 669 io.mmioStout.bits.debug.isPerfCnt := false.B 670 io.mmioStout.bits.debug.vaddr := DontCare 671 // Remove MMIO inst from store queue after MMIO request is being sent 672 // That inst will be traced by uncache state machine 673 when (io.mmioStout.fire) { 674 allocated(deqPtr) := false.B 675 } 676 677 /** 678 * ROB commits store instructions (mark them as committed) 679 * 680 * (1) When store commits, mark it as committed. 681 * (2) They will not be cancelled and can be sent to lower level. 682 */ 683 XSError(uncacheState =/= s_idle && uncacheState =/= s_wait && commitCount > 0.U, 684 "should not commit instruction when MMIO has not been finished\n") 685 for (i <- 0 until CommitWidth) { 686 when (commitCount > i.U) { // MMIO inst is not in progress 687 if(i == 0){ 688 // MMIO inst should not update committed flag 689 // Note that commit count has been delayed for 1 cycle 690 when(uncacheState === s_idle){ 691 committed(cmtPtrExt(0).value) := true.B 692 } 693 } else { 694 committed(cmtPtrExt(i).value) := true.B 695 } 696 } 697 } 698 cmtPtrExt := cmtPtrExt.map(_ + commitCount) 699 700 // committed stores will not be cancelled and can be sent to lower level. 701 // remove retired insts from sq, add retired store to sbuffer 702 703 // Read data from data module 704 // As store queue grows larger and larger, time needed to read data from data 705 // module keeps growing higher. Now we give data read a whole cycle. 706 707 // Vector stores are written to sbuffer by vector store flow queue rather than sq 708 when (io.vecStoreRetire.valid) { 709 assert(io.vecStoreRetire.bits === rdataPtrExt(0)) 710 assert(vec(rdataPtrExt(0).value), "Vector store flow queue is trying to retire a scalar store") 711 assert(allocated(rdataPtrExt(0).value), "Vector store flow queue is trying to retire an invalid entry") 712 vec(rdataPtrExt(0).value) := false.B 713 allocated(rdataPtrExt(0).value) := false.B 714 } 715 716 val mmioStall = mmio(rdataPtrExt(0).value) 717 val vecStall = vec(rdataPtrExt(0).value) 718 for (i <- 0 until EnsbufferWidth) { 719 val ptr = rdataPtrExt(i).value 720 dataBuffer.io.enq(i).valid := allocated(ptr) && committed(ptr) && !mmioStall && !vecStall 721 // Note that store data/addr should both be valid after store's commit 722 assert(!dataBuffer.io.enq(i).valid || allvalid(ptr)) 723 dataBuffer.io.enq(i).bits.addr := paddrModule.io.rdata(i) 724 dataBuffer.io.enq(i).bits.vaddr := vaddrModule.io.rdata(i) 725 dataBuffer.io.enq(i).bits.data := dataModule.io.rdata(i).data 726 dataBuffer.io.enq(i).bits.mask := dataModule.io.rdata(i).mask 727 dataBuffer.io.enq(i).bits.wline := paddrModule.io.rlineflag(i) 728 dataBuffer.io.enq(i).bits.sqPtr := rdataPtrExt(i) 729 dataBuffer.io.enq(i).bits.prefetch := prefetch(ptr) 730 } 731 732 // Send data stored in sbufferReqBitsReg to sbuffer 733 for (i <- 0 until EnsbufferWidth) { 734 io.sbuffer(i).valid := dataBuffer.io.deq(i).valid 735 dataBuffer.io.deq(i).ready := io.sbuffer(i).ready 736 // Write line request should have all 1 mask 737 assert(!(io.sbuffer(i).valid && io.sbuffer(i).bits.wline && !io.sbuffer(i).bits.mask.andR)) 738 io.sbuffer(i).bits := DontCare 739 io.sbuffer(i).bits.cmd := MemoryOpConstants.M_XWR 740 io.sbuffer(i).bits.addr := dataBuffer.io.deq(i).bits.addr 741 io.sbuffer(i).bits.vaddr := dataBuffer.io.deq(i).bits.vaddr 742 io.sbuffer(i).bits.data := dataBuffer.io.deq(i).bits.data 743 io.sbuffer(i).bits.mask := dataBuffer.io.deq(i).bits.mask 744 io.sbuffer(i).bits.wline := dataBuffer.io.deq(i).bits.wline 745 io.sbuffer(i).bits.prefetch := dataBuffer.io.deq(i).bits.prefetch 746 747 // io.sbuffer(i).fire is RegNexted, as sbuffer data write takes 2 cycles. 748 // Before data write finish, sbuffer is unable to provide store to load 749 // forward data. As an workaround, deqPtrExt and allocated flag update 750 // is delayed so that load can get the right data from store queue. 751 val ptr = dataBuffer.io.deq(i).bits.sqPtr.value 752 when (RegNext(io.sbuffer(i).fire)) { 753 allocated(RegEnable(ptr, io.sbuffer(i).fire)) := false.B 754 XSDebug("sbuffer "+i+" fire: ptr %d\n", ptr) 755 } 756 } 757 (1 until EnsbufferWidth).foreach(i => when(io.sbuffer(i).fire) { assert(io.sbuffer(i - 1).fire) }) 758 if (coreParams.dcacheParametersOpt.isEmpty) { 759 for (i <- 0 until EnsbufferWidth) { 760 val ptr = deqPtrExt(i).value 761 val fakeRAM = Module(new RAMHelper(64L * 1024 * 1024 * 1024)) 762 fakeRAM.clk := clock 763 fakeRAM.en := allocated(ptr) && committed(ptr) && !mmio(ptr) 764 fakeRAM.rIdx := 0.U 765 fakeRAM.wIdx := (paddrModule.io.rdata(i) - "h80000000".U) >> 3 766 fakeRAM.wdata := Mux(paddrModule.io.rdata(i)(3), dataModule.io.rdata(i).data(127,64), dataModule.io.rdata(i).data(63,0)) 767 fakeRAM.wmask := Mux(paddrModule.io.rdata(i)(3), MaskExpand(dataModule.io.rdata(i).mask(15,8)), MaskExpand(dataModule.io.rdata(i).mask(7,0))) 768 fakeRAM.wen := allocated(ptr) && committed(ptr) && !mmio(ptr) 769 } 770 } 771 772 // Read vaddr for mem exception 773 io.exceptionAddr.vaddr := vaddrModule.io.rdata(EnsbufferWidth) 774 775 // misprediction recovery / exception redirect 776 // invalidate sq term using robIdx 777 val needCancel = Wire(Vec(StoreQueueSize, Bool())) 778 for (i <- 0 until StoreQueueSize) { 779 needCancel(i) := uop(i).robIdx.needFlush(io.brqRedirect) && allocated(i) && !committed(i) 780 when (needCancel(i)) { 781 allocated(i) := false.B 782 } 783 } 784 785 /** 786* update pointers 787**/ 788 val lastEnqCancel = PopCount(RegNext(VecInit(canEnqueue.zip(enqCancel).map(x => x._1 && x._2)))) // 1 cycle after redirect 789 val lastCycleCancelCount = PopCount(RegNext(needCancel)) // 1 cycle after redirect 790 val lastCycleRedirect = RegNext(io.brqRedirect.valid) // 1 cycle after redirect 791 val enqNumber = Mux(!lastCycleRedirect&&io.enq.canAccept && io.enq.lqCanAccept, PopCount(io.enq.req.map(_.valid)), 0.U) // 1 cycle after redirect 792 793 val lastlastCycleRedirect=RegNext(lastCycleRedirect)// 2 cycle after redirect 794 val redirectCancelCount = RegEnable(lastCycleCancelCount + lastEnqCancel, lastCycleRedirect) // 2 cycle after redirect 795 796 when (lastlastCycleRedirect) { 797 // we recover the pointers in 2 cycle after redirect for better timing 798 enqPtrExt := VecInit(enqPtrExt.map(_ - redirectCancelCount)) 799 }.otherwise { 800 // lastCycleRedirect.valid or nornal case 801 // when lastCycleRedirect.valid, enqNumber === 0.U, enqPtrExt will not change 802 enqPtrExt := VecInit(enqPtrExt.map(_ + enqNumber)) 803 } 804 assert(!(lastCycleRedirect && enqNumber =/= 0.U)) 805 806 deqPtrExt := deqPtrExtNext 807 rdataPtrExt := rdataPtrExtNext 808 809 // val dequeueCount = Mux(io.sbuffer(1).fire, 2.U, Mux(io.sbuffer(0).fire || io.mmioStout.fire, 1.U, 0.U)) 810 811 // If redirect at T0, sqCancelCnt is at T2 812 io.sqCancelCnt := redirectCancelCount 813 val ForceWriteUpper = Wire(UInt(log2Up(StoreQueueSize + 1).W)) 814 ForceWriteUpper := Constantin.createRecord("ForceWriteUpper_"+p(XSCoreParamsKey).HartId.toString(), initValue = 60.U) 815 val ForceWriteLower = Wire(UInt(log2Up(StoreQueueSize + 1).W)) 816 ForceWriteLower := Constantin.createRecord("ForceWriteLower_"+p(XSCoreParamsKey).HartId.toString(), initValue = 55.U) 817 818 val valid_cnt = PopCount(allocated) 819 io.force_write := RegNext(Mux(valid_cnt >= ForceWriteUpper, true.B, valid_cnt >= ForceWriteLower && io.force_write), init = false.B) 820 821 // io.sqempty will be used by sbuffer 822 // We delay it for 1 cycle for better timing 823 // When sbuffer need to check if it is empty, the pipeline is blocked, which means delay io.sqempty 824 // for 1 cycle will also promise that sq is empty in that cycle 825 io.sqEmpty := RegNext( 826 enqPtrExt(0).value === deqPtrExt(0).value && 827 enqPtrExt(0).flag === deqPtrExt(0).flag 828 ) 829 // perf counter 830 QueuePerf(StoreQueueSize, validCount, !allowEnqueue) 831 io.sqFull := !allowEnqueue 832 XSPerfAccumulate("mmioCycle", uncacheState =/= s_idle) // lq is busy dealing with uncache req 833 XSPerfAccumulate("mmioCnt", io.uncache.req.fire) 834 XSPerfAccumulate("mmio_wb_success", io.mmioStout.fire) 835 XSPerfAccumulate("mmio_wb_blocked", io.mmioStout.valid && !io.mmioStout.ready) 836 XSPerfAccumulate("validEntryCnt", distanceBetween(enqPtrExt(0), deqPtrExt(0))) 837 XSPerfAccumulate("cmtEntryCnt", distanceBetween(cmtPtrExt(0), deqPtrExt(0))) 838 XSPerfAccumulate("nCmtEntryCnt", distanceBetween(enqPtrExt(0), cmtPtrExt(0))) 839 840 val perfValidCount = distanceBetween(enqPtrExt(0), deqPtrExt(0)) 841 val perfEvents = Seq( 842 ("mmioCycle ", uncacheState =/= s_idle), 843 ("mmioCnt ", io.uncache.req.fire), 844 ("mmio_wb_success", io.mmioStout.fire), 845 ("mmio_wb_blocked", io.mmioStout.valid && !io.mmioStout.ready), 846 ("stq_1_4_valid ", (perfValidCount < (StoreQueueSize.U/4.U))), 847 ("stq_2_4_valid ", (perfValidCount > (StoreQueueSize.U/4.U)) & (perfValidCount <= (StoreQueueSize.U/2.U))), 848 ("stq_3_4_valid ", (perfValidCount > (StoreQueueSize.U/2.U)) & (perfValidCount <= (StoreQueueSize.U*3.U/4.U))), 849 ("stq_4_4_valid ", (perfValidCount > (StoreQueueSize.U*3.U/4.U))), 850 ) 851 generatePerfEvent() 852 853 // debug info 854 XSDebug("enqPtrExt %d:%d deqPtrExt %d:%d\n", enqPtrExt(0).flag, enqPtr, deqPtrExt(0).flag, deqPtr) 855 856 def PrintFlag(flag: Bool, name: String): Unit = { 857 when(flag) { 858 XSDebug(false, true.B, name) 859 }.otherwise { 860 XSDebug(false, true.B, " ") 861 } 862 } 863 864 for (i <- 0 until StoreQueueSize) { 865 XSDebug(i + ": pc %x va %x pa %x data %x ", 866 uop(i).pc, 867 debug_vaddr(i), 868 debug_paddr(i), 869 debug_data(i) 870 ) 871 PrintFlag(allocated(i), "a") 872 PrintFlag(allocated(i) && addrvalid(i), "a") 873 PrintFlag(allocated(i) && datavalid(i), "d") 874 PrintFlag(allocated(i) && committed(i), "c") 875 PrintFlag(allocated(i) && pending(i), "p") 876 PrintFlag(allocated(i) && mmio(i), "m") 877 XSDebug(false, true.B, "\n") 878 } 879 880} 881