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