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