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.{DCacheWordIO, DCacheLineIO, MemoryOpConstants} 27import xiangshan.cache.mmu.{TlbRequestIO} 28import xiangshan.mem._ 29import xiangshan.backend.rob.RobLsqIO 30 31class ExceptionAddrIO(implicit p: Parameters) extends XSBundle { 32 val isStore = Input(Bool()) 33 val vaddr = Output(UInt(VAddrBits.W)) 34} 35 36class FwdEntry extends Bundle { 37 val validFast = Bool() // validFast is generated the same cycle with query 38 val valid = Bool() // valid is generated 1 cycle after query request 39 val data = UInt(8.W) // data is generated 1 cycle after query request 40} 41 42// inflight miss block reqs 43class InflightBlockInfo(implicit p: Parameters) extends XSBundle { 44 val block_addr = UInt(PAddrBits.W) 45 val valid = Bool() 46} 47 48class LsqEnqIO(implicit p: Parameters) extends XSBundle { 49 val canAccept = Output(Bool()) 50 val needAlloc = Vec(exuParameters.LsExuCnt, Input(UInt(2.W))) 51 val req = Vec(exuParameters.LsExuCnt, Flipped(ValidIO(new MicroOp))) 52 val resp = Vec(exuParameters.LsExuCnt, Output(new LSIdx)) 53} 54 55// Load / Store Queue Wrapper for XiangShan Out of Order LSU 56class LsqWrapper(implicit p: Parameters) extends XSModule with HasDCacheParameters with HasPerfEvents { 57 val io = IO(new Bundle() { 58 val hartId = Input(UInt(8.W)) 59 val brqRedirect = Flipped(ValidIO(new Redirect)) 60 val enq = new LsqEnqIO 61 val ldu = new Bundle() { 62 val storeLoadViolationQuery = Vec(LoadPipelineWidth, Flipped(new LoadViolationQueryIO)) // from load_s2 63 val loadLoadViolationQuery = Vec(LoadPipelineWidth, Flipped(new LoadViolationQueryIO)) // from load_s2 64 val loadIn = Vec(StorePipelineWidth, Flipped(Decoupled(new LqWriteBundle))) // from load_s3 65 } 66 val sta = new Bundle() { 67 val storeMaskIn = Vec(StorePipelineWidth, Flipped(Valid(new StoreMaskBundle))) // from store_s0, store mask, send to sq from rs 68 val storeAddrIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) // from store_s1 69 val storeAddrInRe = Vec(StorePipelineWidth, Input(new LsPipelineBundle())) // from store_s2 70 } 71 val std = new Bundle() { 72 val storeDataIn = Vec(StorePipelineWidth, Flipped(Valid(new ExuOutput))) // from store_s0, store data, send to sq from rs 73 } 74 val loadOut = Vec(LoadPipelineWidth, DecoupledIO(new ExuOutput)) 75 val ldRawDataOut = Vec(LoadPipelineWidth, Output(new LoadDataFromLQBundle)) 76 val replay = Vec(LoadPipelineWidth, Decoupled(new LsPipelineBundle)) 77 val sbuffer = Vec(EnsbufferWidth, Decoupled(new DCacheWordReqWithVaddr)) 78 val forward = Vec(LoadPipelineWidth, Flipped(new PipeLoadForwardQueryIO)) 79 val rob = Flipped(new RobLsqIO) 80 val rollback = Output(Valid(new Redirect)) 81 val release = Flipped(Valid(new Release)) 82 val refill = Flipped(Valid(new Refill)) 83 val uncacheOutstanding = Input(Bool()) 84 val uncache = new UncacheWordIO 85 val mmioStout = DecoupledIO(new ExuOutput) // writeback uncached store 86 val sqEmpty = Output(Bool()) 87 val lqReplayFull = Output(Bool()) 88 val sqFull = Output(Bool()) 89 val lqFull = Output(Bool()) 90 val sqCancelCnt = Output(UInt(log2Up(StoreQueueSize+1).W)) 91 val lqCancelCnt = Output(UInt(log2Up(VirtualLoadQueueSize+1).W)) 92 val lqDeq = Output(UInt(log2Up(CommitWidth + 1).W)) 93 val sqDeq = Output(UInt(log2Ceil(EnsbufferWidth + 1).W)) 94 val exceptionAddr = new ExceptionAddrIO 95 val trigger = Vec(LoadPipelineWidth, new LqTriggerIO) 96 val issuePtrExt = Output(new SqPtr) 97 val l2Hint = Input(Valid(new L2ToL1Hint())) 98 }) 99 100 val loadQueue = Module(new LoadQueue) 101 val storeQueue = Module(new StoreQueue) 102 103 storeQueue.io.hartId := io.hartId 104 storeQueue.io.uncacheOutstanding := io.uncacheOutstanding 105 106 107 dontTouch(loadQueue.io.tlbReplayDelayCycleCtrl) 108 val tlbReplayDelayCycleCtrl = WireInit(VecInit(Seq(15.U(ReSelectLen.W), 0.U(ReSelectLen.W), 126.U(ReSelectLen.W), 0.U(ReSelectLen.W)))) 109 loadQueue.io.tlbReplayDelayCycleCtrl := tlbReplayDelayCycleCtrl 110 111 // io.enq logic 112 // LSQ: send out canAccept when both load queue and store queue are ready 113 // Dispatch: send instructions to LSQ only when they are ready 114 io.enq.canAccept := loadQueue.io.enq.canAccept && storeQueue.io.enq.canAccept 115 loadQueue.io.enq.sqCanAccept := storeQueue.io.enq.canAccept 116 storeQueue.io.enq.lqCanAccept := loadQueue.io.enq.canAccept 117 for (i <- io.enq.req.indices) { 118 loadQueue.io.enq.needAlloc(i) := io.enq.needAlloc(i)(0) 119 loadQueue.io.enq.req(i).valid := io.enq.needAlloc(i)(0) && io.enq.req(i).valid 120 loadQueue.io.enq.req(i).bits := io.enq.req(i).bits 121 loadQueue.io.enq.req(i).bits.sqIdx := storeQueue.io.enq.resp(i) 122 123 storeQueue.io.enq.needAlloc(i) := io.enq.needAlloc(i)(1) 124 storeQueue.io.enq.req(i).valid := io.enq.needAlloc(i)(1) && io.enq.req(i).valid 125 storeQueue.io.enq.req(i).bits := io.enq.req(i).bits 126 storeQueue.io.enq.req(i).bits := io.enq.req(i).bits 127 storeQueue.io.enq.req(i).bits.lqIdx := loadQueue.io.enq.resp(i) 128 129 io.enq.resp(i).lqIdx := loadQueue.io.enq.resp(i) 130 io.enq.resp(i).sqIdx := storeQueue.io.enq.resp(i) 131 } 132 133 // store queue wiring 134 storeQueue.io.brqRedirect <> io.brqRedirect 135 storeQueue.io.storeAddrIn <> io.sta.storeAddrIn // from store_s1 136 storeQueue.io.storeAddrInRe <> io.sta.storeAddrInRe // from store_s2 137 storeQueue.io.storeDataIn <> io.std.storeDataIn // from store_s0 138 storeQueue.io.storeMaskIn <> io.sta.storeMaskIn // from store_s0 139 storeQueue.io.sbuffer <> io.sbuffer 140 storeQueue.io.mmioStout <> io.mmioStout 141 storeQueue.io.rob <> io.rob 142 storeQueue.io.exceptionAddr.isStore := DontCare 143 storeQueue.io.sqCancelCnt <> io.sqCancelCnt 144 storeQueue.io.sqDeq <> io.sqDeq 145 storeQueue.io.sqEmpty <> io.sqEmpty 146 storeQueue.io.sqFull <> io.sqFull 147 storeQueue.io.forward <> io.forward // overlap forwardMask & forwardData, DO NOT CHANGE SEQUENCE 148 149 /* <------- DANGEROUS: Don't change sequence here ! -------> */ 150 151 // load queue wiring 152 loadQueue.io.redirect <> io.brqRedirect 153 loadQueue.io.ldu <> io.ldu 154 loadQueue.io.loadOut <> io.loadOut 155 loadQueue.io.ldRawDataOut <> io.ldRawDataOut 156 loadQueue.io.rob <> io.rob 157 loadQueue.io.rollback <> io.rollback 158 loadQueue.io.replay <> io.replay 159 loadQueue.io.refill <> io.refill 160 loadQueue.io.release <> io.release 161 loadQueue.io.trigger <> io.trigger 162 loadQueue.io.exceptionAddr.isStore := DontCare 163 loadQueue.io.lqCancelCnt <> io.lqCancelCnt 164 loadQueue.io.sq.stAddrReadySqPtr <> storeQueue.io.stAddrReadySqPtr 165 loadQueue.io.sq.stAddrReadyVec <> storeQueue.io.stAddrReadyVec 166 loadQueue.io.sq.stDataReadySqPtr <> storeQueue.io.stDataReadySqPtr 167 loadQueue.io.sq.stDataReadyVec <> storeQueue.io.stDataReadyVec 168 loadQueue.io.sq.stIssuePtr <> storeQueue.io.stIssuePtr 169 loadQueue.io.sq.sqEmpty <> storeQueue.io.sqEmpty 170 loadQueue.io.sta.storeAddrIn <> io.sta.storeAddrIn // store_s1 171 loadQueue.io.std.storeDataIn <> io.std.storeDataIn // store_s0 172 loadQueue.io.lqFull <> io.lqFull 173 loadQueue.io.lqReplayFull <> io.lqReplayFull 174 loadQueue.io.lqDeq <> io.lqDeq 175 loadQueue.io.l2Hint.valid := io.l2Hint.valid 176 loadQueue.io.l2Hint.bits.sourceId := io.l2Hint.bits.sourceId 177 178 // rob commits for lsq is delayed for two cycles, which causes the delayed update for deqPtr in lq/sq 179 // s0: commit 180 // s1: exception find 181 // s2: exception triggered 182 // s3: ptr updated & new address 183 // address will be used at the next cycle after exception is triggered 184 io.exceptionAddr.vaddr := Mux(RegNext(io.exceptionAddr.isStore), storeQueue.io.exceptionAddr.vaddr, loadQueue.io.exceptionAddr.vaddr) 185 io.issuePtrExt := storeQueue.io.stAddrReadySqPtr 186 187 // naive uncache arbiter 188 val s_idle :: s_load :: s_store :: Nil = Enum(3) 189 val pendingstate = RegInit(s_idle) 190 191 switch(pendingstate){ 192 is(s_idle){ 193 when(io.uncache.req.fire() && !io.uncacheOutstanding){ 194 pendingstate := Mux(loadQueue.io.uncache.req.valid, s_load, 195 Mux(io.uncacheOutstanding, s_idle, s_store)) 196 } 197 } 198 is(s_load){ 199 when(io.uncache.resp.fire()){ 200 pendingstate := s_idle 201 } 202 } 203 is(s_store){ 204 when(io.uncache.resp.fire()){ 205 pendingstate := s_idle 206 } 207 } 208 } 209 210 loadQueue.io.uncache := DontCare 211 storeQueue.io.uncache := DontCare 212 loadQueue.io.uncache.resp.valid := false.B 213 storeQueue.io.uncache.resp.valid := false.B 214 when(loadQueue.io.uncache.req.valid){ 215 io.uncache.req <> loadQueue.io.uncache.req 216 }.otherwise{ 217 io.uncache.req <> storeQueue.io.uncache.req 218 } 219 when (io.uncacheOutstanding) { 220 io.uncache.resp <> loadQueue.io.uncache.resp 221 } .otherwise { 222 when(pendingstate === s_load){ 223 io.uncache.resp <> loadQueue.io.uncache.resp 224 }.otherwise{ 225 io.uncache.resp <> storeQueue.io.uncache.resp 226 } 227 } 228 229 230 assert(!(loadQueue.io.uncache.req.valid && storeQueue.io.uncache.req.valid)) 231 assert(!(loadQueue.io.uncache.resp.valid && storeQueue.io.uncache.resp.valid)) 232 when (!io.uncacheOutstanding) { 233 assert(!((loadQueue.io.uncache.resp.valid || storeQueue.io.uncache.resp.valid) && pendingstate === s_idle)) 234 } 235 236 237 val perfEvents = Seq(loadQueue, storeQueue).flatMap(_.getPerfEvents) 238 generatePerfEvent() 239} 240 241class LsqEnqCtrl(implicit p: Parameters) extends XSModule { 242 val io = IO(new Bundle { 243 val redirect = Flipped(ValidIO(new Redirect)) 244 // to dispatch 245 val enq = new LsqEnqIO 246 // from `memBlock.io.lqDeq 247 val lcommit = Input(UInt(log2Up(CommitWidth + 1).W)) 248 // from `memBlock.io.sqDeq` 249 val scommit = Input(UInt(log2Ceil(EnsbufferWidth + 1).W)) 250 // from/tp lsq 251 val lqCancelCnt = Input(UInt(log2Up(VirtualLoadQueueSize + 1).W)) 252 val sqCancelCnt = Input(UInt(log2Up(StoreQueueSize + 1).W)) 253 val enqLsq = Flipped(new LsqEnqIO) 254 }) 255 256 val lqPtr = RegInit(0.U.asTypeOf(new LqPtr)) 257 val sqPtr = RegInit(0.U.asTypeOf(new SqPtr)) 258 val lqCounter = RegInit(VirtualLoadQueueSize.U(log2Up(VirtualLoadQueueSize + 1).W)) 259 val sqCounter = RegInit(StoreQueueSize.U(log2Up(StoreQueueSize + 1).W)) 260 val canAccept = RegInit(false.B) 261 262 val loadEnqNumber = PopCount(io.enq.req.zip(io.enq.needAlloc).map(x => x._1.valid && x._2(0))) 263 val storeEnqNumber = PopCount(io.enq.req.zip(io.enq.needAlloc).map(x => x._1.valid && x._2(1))) 264 265 // How to update ptr and counter: 266 // (1) by default, updated according to enq/commit 267 // (2) when redirect and dispatch queue is empty, update according to lsq 268 val t1_redirect = RegNext(io.redirect.valid) 269 val t2_redirect = RegNext(t1_redirect) 270 val t2_update = t2_redirect && !VecInit(io.enq.needAlloc.map(_.orR)).asUInt.orR 271 val t3_update = RegNext(t2_update) 272 val t3_lqCancelCnt = RegNext(io.lqCancelCnt) 273 val t3_sqCancelCnt = RegNext(io.sqCancelCnt) 274 when (t3_update) { 275 lqPtr := lqPtr - t3_lqCancelCnt 276 lqCounter := lqCounter + io.lcommit + t3_lqCancelCnt 277 sqPtr := sqPtr - t3_sqCancelCnt 278 sqCounter := sqCounter + io.scommit + t3_sqCancelCnt 279 }.elsewhen (!io.redirect.valid && io.enq.canAccept) { 280 lqPtr := lqPtr + loadEnqNumber 281 lqCounter := lqCounter + io.lcommit - loadEnqNumber 282 sqPtr := sqPtr + storeEnqNumber 283 sqCounter := sqCounter + io.scommit - storeEnqNumber 284 }.otherwise { 285 lqCounter := lqCounter + io.lcommit 286 sqCounter := sqCounter + io.scommit 287 } 288 289 290 val maxAllocate = Seq(exuParameters.LduCnt, exuParameters.StuCnt).max 291 val ldCanAccept = lqCounter >= loadEnqNumber +& maxAllocate.U 292 val sqCanAccept = sqCounter >= storeEnqNumber +& maxAllocate.U 293 // It is possible that t3_update and enq are true at the same clock cycle. 294 // For example, if redirect.valid lasts more than one clock cycle, 295 // after the last redirect, new instructions may enter but previously redirect 296 // has not been resolved (updated according to the cancel count from LSQ). 297 // To solve the issue easily, we block enqueue when t3_update, which is RegNext(t2_update). 298 io.enq.canAccept := RegNext(ldCanAccept && sqCanAccept && !t2_update) 299 val lqOffset = Wire(Vec(io.enq.resp.length, UInt(log2Up(maxAllocate + 1).W))) 300 val sqOffset = Wire(Vec(io.enq.resp.length, UInt(log2Up(maxAllocate + 1).W))) 301 for ((resp, i) <- io.enq.resp.zipWithIndex) { 302 lqOffset(i) := PopCount(io.enq.needAlloc.take(i).map(a => a(0))) 303 resp.lqIdx := lqPtr + lqOffset(i) 304 sqOffset(i) := PopCount(io.enq.needAlloc.take(i).map(a => a(1))) 305 resp.sqIdx := sqPtr + sqOffset(i) 306 } 307 308 io.enqLsq.needAlloc := RegNext(io.enq.needAlloc) 309 io.enqLsq.req.zip(io.enq.req).zip(io.enq.resp).foreach{ case ((toLsq, enq), resp) => 310 val do_enq = enq.valid && !io.redirect.valid && io.enq.canAccept 311 toLsq.valid := RegNext(do_enq) 312 toLsq.bits := RegEnable(enq.bits, do_enq) 313 toLsq.bits.lqIdx := RegEnable(resp.lqIdx, do_enq) 314 toLsq.bits.sqIdx := RegEnable(resp.sqIdx, do_enq) 315 } 316 317}