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