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