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