1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.mem 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import utils._ 23import utility._ 24import xiangshan._ 25import xiangshan.backend.Bundles.{DynInst, MemExuOutput} 26import xiangshan.cache._ 27import xiangshan.cache.{DCacheWordIO, DCacheLineIO, MemoryOpConstants} 28import xiangshan.cache.mmu.{TlbRequestIO, TlbHintIO} 29import xiangshan.mem._ 30import xiangshan.backend._ 31import xiangshan.backend.rob.RobLsqIO 32 33class ExceptionAddrIO(implicit p: Parameters) extends XSBundle { 34 val isStore = Input(Bool()) 35 val vaddr = Output(UInt(VAddrBits.W)) 36 val gpaddr = Output(UInt(GPAddrBits.W)) 37} 38 39class FwdEntry extends Bundle { 40 val validFast = Bool() // validFast is generated the same cycle with query 41 val valid = Bool() // valid is generated 1 cycle after query request 42 val data = UInt(8.W) // data is generated 1 cycle after query request 43} 44 45// inflight miss block reqs 46class InflightBlockInfo(implicit p: Parameters) extends XSBundle { 47 val block_addr = UInt(PAddrBits.W) 48 val valid = Bool() 49} 50 51class LsqEnqIO(implicit p: Parameters) extends MemBlockBundle { 52 val canAccept = Output(Bool()) 53 val needAlloc = Vec(LSQEnqWidth, Input(UInt(2.W))) 54 val req = Vec(LSQEnqWidth, Flipped(ValidIO(new DynInst))) 55 val resp = Vec(LSQEnqWidth, Output(new LSIdx)) 56} 57 58// Load / Store Queue Wrapper for XiangShan Out of Order LSU 59class LsqWrapper(implicit p: Parameters) extends XSModule with HasDCacheParameters with HasPerfEvents { 60 val io = IO(new Bundle() { 61 val hartId = Input(UInt(hartIdLen.W)) 62 val brqRedirect = Flipped(ValidIO(new Redirect)) 63 val enq = new LsqEnqIO 64 val ldu = new Bundle() { 65 val stld_nuke_query = Vec(LoadPipelineWidth, Flipped(new LoadNukeQueryIO)) // from load_s2 66 val ldld_nuke_query = Vec(LoadPipelineWidth, Flipped(new LoadNukeQueryIO)) // from load_s2 67 val ldin = Vec(LoadPipelineWidth, Flipped(Decoupled(new LqWriteBundle))) // from load_s3 68 } 69 val sta = new Bundle() { 70 val storeMaskIn = Vec(StorePipelineWidth, Flipped(Valid(new StoreMaskBundle))) // from store_s0, store mask, send to sq from rs 71 val storeAddrIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) // from store_s1 72 val storeAddrInRe = Vec(StorePipelineWidth, Input(new LsPipelineBundle())) // from store_s2 73 val vecStoreAddrIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) //from store_s2 74 val vecStoreFlowAddrIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) // from vsFlowQueue last element issue 75 } 76 val std = new Bundle() { 77 val storeDataIn = Vec(StorePipelineWidth, Flipped(Valid(new MemExuOutput))) // from store_s0, store data, send to sq from rs 78 } 79 val ldout = Vec(LoadPipelineWidth, DecoupledIO(new MemExuOutput)) 80 val ld_raw_data = Vec(LoadPipelineWidth, Output(new LoadDataFromLQBundle)) 81 val replay = Vec(LoadPipelineWidth, Decoupled(new LsPipelineBundle)) 82 val sbuffer = Vec(EnsbufferWidth, Decoupled(new DCacheWordReqWithVaddrAndPfFlag)) 83 val forward = Vec(LoadPipelineWidth, Flipped(new PipeLoadForwardQueryIO)) 84 val rob = Flipped(new RobLsqIO) 85 val nuke_rollback = Output(Valid(new Redirect)) 86 val nack_rollback = Output(Valid(new Redirect)) 87 val release = Flipped(Valid(new Release)) 88 // val refill = Flipped(Valid(new Refill)) 89 val tl_d_channel = Input(new DcacheToLduForwardIO) 90 val uncacheOutstanding = Input(Bool()) 91 val uncache = new UncacheWordIO 92 val mmioStout = DecoupledIO(new MemExuOutput) // writeback uncached store 93 val sqEmpty = Output(Bool()) 94 val lq_rep_full = Output(Bool()) 95 val sqFull = Output(Bool()) 96 val lqFull = Output(Bool()) 97 val sqCancelCnt = Output(UInt(log2Up(StoreQueueSize+1).W)) 98 val lqCancelCnt = Output(UInt(log2Up(VirtualLoadQueueSize+1).W)) 99 val lqDeq = Output(UInt(log2Up(CommitWidth + 1).W)) 100 val sqDeq = Output(UInt(log2Ceil(EnsbufferWidth + 1).W)) 101 val lqCanAccept = Output(Bool()) 102 val sqCanAccept = Output(Bool()) 103 val lqDeqPtr = Output(new LqPtr) 104 val sqDeqPtr = Output(new SqPtr) 105 val exceptionAddr = new ExceptionAddrIO 106 val trigger = Vec(LoadPipelineWidth, new LqTriggerIO) 107 val issuePtrExt = Output(new SqPtr) 108 val l2_hint = Input(Valid(new L2ToL1Hint())) 109 val tlb_hint = Flipped(new TlbHintIO) 110 val force_write = Output(Bool()) 111 val lqEmpty = Output(Bool()) 112 113 // vector 114 val vecWriteback = Flipped(ValidIO(new MemExuOutput(isVector = true))) 115 val vecStoreRetire = Flipped(ValidIO(new SqPtr)) 116 val vecMMIOReplay = Vec(VecLoadPipelineWidth, DecoupledIO(new LsPipelineBundle())) 117 118 // top-down 119 val debugTopDown = new LoadQueueTopDownIO 120 }) 121 122 val loadQueue = Module(new LoadQueue) 123 val storeQueue = Module(new StoreQueue) 124 125 storeQueue.io.hartId := io.hartId 126 storeQueue.io.uncacheOutstanding := io.uncacheOutstanding 127 128 129 dontTouch(loadQueue.io.tlbReplayDelayCycleCtrl) 130 // Todo: imm 131 val tlbReplayDelayCycleCtrl = WireInit(VecInit(Seq(14.U(ReSelectLen.W), 0.U(ReSelectLen.W), 125.U(ReSelectLen.W), 0.U(ReSelectLen.W)))) 132 loadQueue.io.tlbReplayDelayCycleCtrl := tlbReplayDelayCycleCtrl 133 134 // io.enq logic 135 // LSQ: send out canAccept when both load queue and store queue are ready 136 // Dispatch: send instructions to LSQ only when they are ready 137 io.enq.canAccept := loadQueue.io.enq.canAccept && storeQueue.io.enq.canAccept 138 io.lqCanAccept := loadQueue.io.enq.canAccept 139 io.sqCanAccept := storeQueue.io.enq.canAccept 140 loadQueue.io.enq.sqCanAccept := storeQueue.io.enq.canAccept 141 storeQueue.io.enq.lqCanAccept := loadQueue.io.enq.canAccept 142 io.lqDeqPtr := loadQueue.io.lqDeqPtr 143 io.sqDeqPtr := storeQueue.io.sqDeqPtr 144 for (i <- io.enq.req.indices) { 145 loadQueue.io.enq.needAlloc(i) := io.enq.needAlloc(i)(0) 146 loadQueue.io.enq.req(i).valid := io.enq.needAlloc(i)(0) && io.enq.req(i).valid 147 loadQueue.io.enq.req(i).bits := io.enq.req(i).bits 148 loadQueue.io.enq.req(i).bits.sqIdx := storeQueue.io.enq.resp(i) 149 150 storeQueue.io.enq.needAlloc(i) := io.enq.needAlloc(i)(1) 151 storeQueue.io.enq.req(i).valid := io.enq.needAlloc(i)(1) && io.enq.req(i).valid 152 storeQueue.io.enq.req(i).bits := io.enq.req(i).bits 153 storeQueue.io.enq.req(i).bits := io.enq.req(i).bits 154 storeQueue.io.enq.req(i).bits.lqIdx := loadQueue.io.enq.resp(i) 155 156 io.enq.resp(i).lqIdx := loadQueue.io.enq.resp(i) 157 io.enq.resp(i).sqIdx := storeQueue.io.enq.resp(i) 158 } 159 160 // store queue wiring 161 storeQueue.io.brqRedirect <> io.brqRedirect 162 storeQueue.io.storeAddrIn <> io.sta.storeAddrIn // from store_s1 163 storeQueue.io.vecStoreAddrIn <> io.sta.vecStoreFlowAddrIn // from VsFlowQueue inactivative element isuue 164 storeQueue.io.storeAddrInRe <> io.sta.storeAddrInRe // from store_s2 165 storeQueue.io.storeDataIn <> io.std.storeDataIn // from store_s0 166 storeQueue.io.storeMaskIn <> io.sta.storeMaskIn // from store_s0 167 storeQueue.io.sbuffer <> io.sbuffer 168 storeQueue.io.mmioStout <> io.mmioStout 169 storeQueue.io.rob <> io.rob 170 storeQueue.io.exceptionAddr.isStore := DontCare 171 storeQueue.io.sqCancelCnt <> io.sqCancelCnt 172 storeQueue.io.sqDeq <> io.sqDeq 173 storeQueue.io.sqEmpty <> io.sqEmpty 174 storeQueue.io.sqFull <> io.sqFull 175 storeQueue.io.forward <> io.forward // overlap forwardMask & forwardData, DO NOT CHANGE SEQUENCE 176 storeQueue.io.force_write <> io.force_write 177 storeQueue.io.vecStoreRetire <> io.vecStoreRetire 178 179 /* <------- DANGEROUS: Don't change sequence here ! -------> */ 180 181 // load queue wiring 182 loadQueue.io.redirect <> io.brqRedirect 183 loadQueue.io.ldu <> io.ldu 184 loadQueue.io.ldout <> io.ldout 185 loadQueue.io.ld_raw_data <> io.ld_raw_data 186 loadQueue.io.rob <> io.rob 187 loadQueue.io.nuke_rollback <> io.nuke_rollback 188 loadQueue.io.nack_rollback <> io.nack_rollback 189 loadQueue.io.replay <> io.replay 190 // loadQueue.io.refill <> io.refill 191 loadQueue.io.tl_d_channel <> io.tl_d_channel 192 loadQueue.io.release <> io.release 193 loadQueue.io.trigger <> io.trigger 194 loadQueue.io.exceptionAddr.isStore := DontCare 195 loadQueue.io.lqCancelCnt <> io.lqCancelCnt 196 loadQueue.io.sq.stAddrReadySqPtr <> storeQueue.io.stAddrReadySqPtr 197 loadQueue.io.sq.stAddrReadyVec <> storeQueue.io.stAddrReadyVec 198 loadQueue.io.sq.stDataReadySqPtr <> storeQueue.io.stDataReadySqPtr 199 loadQueue.io.sq.stDataReadyVec <> storeQueue.io.stDataReadyVec 200 loadQueue.io.sq.stIssuePtr <> storeQueue.io.stIssuePtr 201 loadQueue.io.sq.sqEmpty <> storeQueue.io.sqEmpty 202 loadQueue.io.sta.storeAddrIn <> io.sta.storeAddrIn // store_s1 203 loadQueue.io.sta.vecStoreAddrIn <> io.sta.vecStoreAddrIn // store_s1 204 loadQueue.io.std.storeDataIn <> io.std.storeDataIn // store_s0 205 loadQueue.io.lqFull <> io.lqFull 206 loadQueue.io.lq_rep_full <> io.lq_rep_full 207 loadQueue.io.lqDeq <> io.lqDeq 208 loadQueue.io.l2_hint <> io.l2_hint 209 loadQueue.io.tlb_hint <> io.tlb_hint 210 loadQueue.io.lqEmpty <> io.lqEmpty 211 loadQueue.io.vecWriteback <> io.vecWriteback 212 loadQueue.io.vecMMIOReplay <> io.vecMMIOReplay 213 214 // rob commits for lsq is delayed for two cycles, which causes the delayed update for deqPtr in lq/sq 215 // s0: commit 216 // s1: exception find 217 // s2: exception triggered 218 // s3: ptr updated & new address 219 // address will be used at the next cycle after exception is triggered 220 io.exceptionAddr.vaddr := Mux(RegNext(io.exceptionAddr.isStore), storeQueue.io.exceptionAddr.vaddr, loadQueue.io.exceptionAddr.vaddr) 221 io.exceptionAddr.gpaddr := Mux(RegNext(io.exceptionAddr.isStore), storeQueue.io.exceptionAddr.gpaddr, loadQueue.io.exceptionAddr.gpaddr) 222 io.issuePtrExt := storeQueue.io.stAddrReadySqPtr 223 224 // naive uncache arbiter 225 val s_idle :: s_load :: s_store :: Nil = Enum(3) 226 val pendingstate = RegInit(s_idle) 227 228 switch(pendingstate){ 229 is(s_idle){ 230 when(io.uncache.req.fire){ 231 pendingstate := Mux(loadQueue.io.uncache.req.valid, s_load, 232 Mux(io.uncacheOutstanding, s_idle, s_store)) 233 } 234 } 235 is(s_load){ 236 when(io.uncache.resp.fire){ 237 pendingstate := s_idle 238 } 239 } 240 is(s_store){ 241 when(io.uncache.resp.fire){ 242 pendingstate := s_idle 243 } 244 } 245 } 246 247 loadQueue.io.uncache := DontCare 248 storeQueue.io.uncache := DontCare 249 loadQueue.io.uncache.req.ready := false.B 250 storeQueue.io.uncache.req.ready := false.B 251 loadQueue.io.uncache.resp.valid := false.B 252 storeQueue.io.uncache.resp.valid := false.B 253 when(loadQueue.io.uncache.req.valid){ 254 io.uncache.req <> loadQueue.io.uncache.req 255 }.otherwise{ 256 io.uncache.req <> storeQueue.io.uncache.req 257 } 258 when (io.uncacheOutstanding) { 259 io.uncache.resp <> loadQueue.io.uncache.resp 260 } .otherwise { 261 when(pendingstate === s_load){ 262 io.uncache.resp <> loadQueue.io.uncache.resp 263 }.otherwise{ 264 io.uncache.resp <> storeQueue.io.uncache.resp 265 } 266 } 267 268 loadQueue.io.debugTopDown <> io.debugTopDown 269 270 assert(!(loadQueue.io.uncache.req.valid && storeQueue.io.uncache.req.valid)) 271 assert(!(loadQueue.io.uncache.resp.valid && storeQueue.io.uncache.resp.valid)) 272 when (!io.uncacheOutstanding) { 273 assert(!((loadQueue.io.uncache.resp.valid || storeQueue.io.uncache.resp.valid) && pendingstate === s_idle)) 274 } 275 276 277 val perfEvents = Seq(loadQueue, storeQueue).flatMap(_.getPerfEvents) 278 generatePerfEvent() 279} 280 281class LsqEnqCtrl(implicit p: Parameters) extends XSModule { 282 val io = IO(new Bundle { 283 val redirect = Flipped(ValidIO(new Redirect)) 284 // to dispatch 285 val enq = new LsqEnqIO 286 // from `memBlock.io.lqDeq 287 val lcommit = Input(UInt(log2Up(CommitWidth + 1).W)) 288 // from `memBlock.io.sqDeq` 289 val scommit = Input(UInt(log2Ceil(EnsbufferWidth + 1).W)) 290 // from/tp lsq 291 val lqCancelCnt = Input(UInt(log2Up(VirtualLoadQueueSize + 1).W)) 292 val sqCancelCnt = Input(UInt(log2Up(StoreQueueSize + 1).W)) 293 val enqLsq = Flipped(new LsqEnqIO) 294 }) 295 296 val lqPtr = RegInit(0.U.asTypeOf(new LqPtr)) 297 val sqPtr = RegInit(0.U.asTypeOf(new SqPtr)) 298 val lqCounter = RegInit(VirtualLoadQueueSize.U(log2Up(VirtualLoadQueueSize + 1).W)) 299 val sqCounter = RegInit(StoreQueueSize.U(log2Up(StoreQueueSize + 1).W)) 300 val canAccept = RegInit(false.B) 301 302 val loadEnqVec = io.enq.req.zip(io.enq.needAlloc).map(x => x._1.valid && x._2(0)) 303 val storeEnqVec = io.enq.req.zip(io.enq.needAlloc).map(x => x._1.valid && x._2(1)) 304 val loadEnqNumber = PopCount(loadEnqVec) 305 val storeEnqNumber = PopCount(storeEnqVec) 306 val isLastUopVec = io.enq.req.map(_.bits.lastUop) 307 val lqAllocNumber = PopCount(loadEnqVec.zip(isLastUopVec).map(x => x._1 && x._2)) 308 val sqAllocNumber = PopCount(storeEnqVec.zip(isLastUopVec).map(x => x._1 && x._2)) 309 310 // How to update ptr and counter: 311 // (1) by default, updated according to enq/commit 312 // (2) when redirect and dispatch queue is empty, update according to lsq 313 val t1_redirect = RegNext(io.redirect.valid) 314 val t2_redirect = RegNext(t1_redirect) 315 val t2_update = t2_redirect && !VecInit(io.enq.needAlloc.map(_.orR)).asUInt.orR 316 val t3_update = RegNext(t2_update) 317 val t3_lqCancelCnt = RegNext(io.lqCancelCnt) 318 val t3_sqCancelCnt = RegNext(io.sqCancelCnt) 319 when (t3_update) { 320 lqPtr := lqPtr - t3_lqCancelCnt 321 lqCounter := lqCounter + io.lcommit + t3_lqCancelCnt 322 sqPtr := sqPtr - t3_sqCancelCnt 323 sqCounter := sqCounter + io.scommit + t3_sqCancelCnt 324 }.elsewhen (!io.redirect.valid && io.enq.canAccept) { 325 lqPtr := lqPtr + lqAllocNumber 326 lqCounter := lqCounter + io.lcommit - lqAllocNumber 327 sqPtr := sqPtr + sqAllocNumber 328 sqCounter := sqCounter + io.scommit - sqAllocNumber 329 }.otherwise { 330 lqCounter := lqCounter + io.lcommit 331 sqCounter := sqCounter + io.scommit 332 } 333 334 335 val lqMaxAllocate = LSQLdEnqWidth 336 val sqMaxAllocate = LSQStEnqWidth 337 val maxAllocate = lqMaxAllocate max sqMaxAllocate 338 val ldCanAccept = lqCounter >= lqAllocNumber +& lqMaxAllocate.U 339 val sqCanAccept = sqCounter >= sqAllocNumber +& sqMaxAllocate.U 340 // It is possible that t3_update and enq are true at the same clock cycle. 341 // For example, if redirect.valid lasts more than one clock cycle, 342 // after the last redirect, new instructions may enter but previously redirect 343 // has not been resolved (updated according to the cancel count from LSQ). 344 // To solve the issue easily, we block enqueue when t3_update, which is RegNext(t2_update). 345 io.enq.canAccept := RegNext(ldCanAccept && sqCanAccept && !t2_update) 346 val lqOffset = Wire(Vec(io.enq.resp.length, UInt(log2Up(maxAllocate + 1).W))) 347 val sqOffset = Wire(Vec(io.enq.resp.length, UInt(log2Up(maxAllocate + 1).W))) 348 for ((resp, i) <- io.enq.resp.zipWithIndex) { 349 lqOffset(i) := PopCount(io.enq.needAlloc.zip(isLastUopVec).take(i).map(x => x._1(0) && x._2)) 350 resp.lqIdx := lqPtr + lqOffset(i) 351 sqOffset(i) := PopCount(io.enq.needAlloc.zip(isLastUopVec).take(i).map(x => x._1(1) && x._2)) 352 resp.sqIdx := sqPtr + sqOffset(i) 353 } 354 355 io.enqLsq.needAlloc := RegNext(VecInit(io.enq.needAlloc.zip(io.enq.req).map(x => x._1 & Fill(2, x._2.bits.lastUop)))) 356 io.enqLsq.req.zip(io.enq.req).zip(io.enq.resp).foreach{ case ((toLsq, enq), resp) => 357 val do_enq = enq.valid && !io.redirect.valid && io.enq.canAccept && enq.bits.lastUop 358 toLsq.valid := RegNext(do_enq) 359 toLsq.bits := RegEnable(enq.bits, do_enq) 360 toLsq.bits.lqIdx := RegEnable(resp.lqIdx, do_enq) 361 toLsq.bits.sqIdx := RegEnable(resp.sqIdx, do_enq) 362 } 363 364}