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