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