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