1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* 4* XiangShan is licensed under Mulan PSL v2. 5* You can use this software according to the terms and conditions of the Mulan PSL v2. 6* You may obtain a copy of Mulan PSL v2 at: 7* http://license.coscl.org.cn/MulanPSL2 8* 9* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12* 13* See the Mulan PSL v2 for more details. 14***************************************************************************************/ 15 16package xiangshan.mem 17 18import chipsalliance.rocketchip.config.Parameters 19import chisel3._ 20import chisel3.util._ 21import utils._ 22import xiangshan._ 23import xiangshan.cache._ 24import xiangshan.cache.{DCacheWordIO, DCacheLineIO, TlbRequestIO, MemoryOpConstants} 25import xiangshan.mem._ 26import xiangshan.backend.roq.RoqLsqIO 27 28class ExceptionAddrIO(implicit p: Parameters) extends XSBundle { 29 val lsIdx = Input(new LSIdx) 30 val isStore = Input(Bool()) 31 val vaddr = Output(UInt(VAddrBits.W)) 32} 33 34class FwdEntry extends Bundle { 35 val valid = Bool() 36 val data = UInt(8.W) 37} 38 39// inflight miss block reqs 40class InflightBlockInfo(implicit p: Parameters) extends XSBundle { 41 val block_addr = UInt(PAddrBits.W) 42 val valid = Bool() 43} 44 45class LsqEnqIO(implicit p: Parameters) extends XSBundle { 46 val canAccept = Output(Bool()) 47 val needAlloc = Vec(RenameWidth, Input(UInt(2.W))) 48 val req = Vec(RenameWidth, Flipped(ValidIO(new MicroOp))) 49 val resp = Vec(RenameWidth, Output(new LSIdx)) 50} 51 52// Load / Store Queue Wrapper for XiangShan Out of Order LSU 53class LsqWrappper(implicit p: Parameters) extends XSModule with HasDCacheParameters { 54 val io = IO(new Bundle() { 55 val enq = new LsqEnqIO 56 val brqRedirect = Flipped(ValidIO(new Redirect)) 57 val flush = Input(Bool()) 58 val loadIn = Vec(LoadPipelineWidth, Flipped(Valid(new LsPipelineBundle))) 59 val storeIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) 60 val storeDataIn = Vec(StorePipelineWidth, Flipped(Valid(new StoreDataBundle))) // store data, send to sq from rs 61 val loadDataForwarded = Vec(LoadPipelineWidth, Input(Bool())) 62 val needReplayFromRS = Vec(LoadPipelineWidth, Input(Bool())) 63 val sbuffer = Vec(StorePipelineWidth, Decoupled(new DCacheWordReq)) 64 val ldout = Vec(2, DecoupledIO(new ExuOutput)) // writeback int load 65 val mmioStout = DecoupledIO(new ExuOutput) // writeback uncached store 66 val forward = Vec(LoadPipelineWidth, Flipped(new PipeLoadForwardQueryIO)) 67 val roq = Flipped(new RoqLsqIO) 68 val rollback = Output(Valid(new Redirect)) 69 val dcache = Flipped(ValidIO(new Refill)) 70 val uncache = new DCacheWordIO 71 val exceptionAddr = new ExceptionAddrIO 72 val sqempty = Output(Bool()) 73 val issuePtrExt = Output(new SqPtr) 74 val storeIssue = Vec(StorePipelineWidth, Flipped(Valid(new ExuInput))) 75 val sqFull = Output(Bool()) 76 val lqFull = Output(Bool()) 77 }) 78 79 val loadQueue = Module(new LoadQueue) 80 val storeQueue = Module(new StoreQueue) 81 82 // io.enq logic 83 // LSQ: send out canAccept when both load queue and store queue are ready 84 // Dispatch: send instructions to LSQ only when they are ready 85 io.enq.canAccept := loadQueue.io.enq.canAccept && storeQueue.io.enq.canAccept 86 loadQueue.io.enq.sqCanAccept := storeQueue.io.enq.canAccept 87 storeQueue.io.enq.lqCanAccept := loadQueue.io.enq.canAccept 88 for (i <- 0 until RenameWidth) { 89 loadQueue.io.enq.needAlloc(i) := io.enq.needAlloc(i)(0) 90 loadQueue.io.enq.req(i).valid := io.enq.needAlloc(i)(0) && io.enq.req(i).valid 91 loadQueue.io.enq.req(i).bits := io.enq.req(i).bits 92 93 storeQueue.io.enq.needAlloc(i) := io.enq.needAlloc(i)(1) 94 storeQueue.io.enq.req(i).valid := io.enq.needAlloc(i)(1) && io.enq.req(i).valid 95 storeQueue.io.enq.req(i).bits := io.enq.req(i).bits 96 97 io.enq.resp(i).lqIdx := loadQueue.io.enq.resp(i) 98 io.enq.resp(i).sqIdx := storeQueue.io.enq.resp(i) 99 } 100 101 // load queue wiring 102 loadQueue.io.brqRedirect <> io.brqRedirect 103 loadQueue.io.flush <> io.flush 104 loadQueue.io.loadIn <> io.loadIn 105 loadQueue.io.storeIn <> io.storeIn 106 loadQueue.io.loadDataForwarded <> io.loadDataForwarded 107 loadQueue.io.needReplayFromRS <> io.needReplayFromRS 108 loadQueue.io.ldout <> io.ldout 109 loadQueue.io.roq <> io.roq 110 loadQueue.io.rollback <> io.rollback 111 loadQueue.io.dcache <> io.dcache 112 loadQueue.io.exceptionAddr.lsIdx := io.exceptionAddr.lsIdx 113 loadQueue.io.exceptionAddr.isStore := DontCare 114 115 // store queue wiring 116 // storeQueue.io <> DontCare 117 storeQueue.io.brqRedirect <> io.brqRedirect 118 storeQueue.io.flush <> io.flush 119 storeQueue.io.storeIn <> io.storeIn 120 storeQueue.io.storeDataIn <> io.storeDataIn 121 storeQueue.io.sbuffer <> io.sbuffer 122 storeQueue.io.mmioStout <> io.mmioStout 123 storeQueue.io.roq <> io.roq 124 storeQueue.io.exceptionAddr.lsIdx := io.exceptionAddr.lsIdx 125 storeQueue.io.exceptionAddr.isStore := DontCare 126 storeQueue.io.issuePtrExt <> io.issuePtrExt 127 storeQueue.io.storeIssue <> io.storeIssue 128 129 loadQueue.io.load_s1 <> io.forward 130 storeQueue.io.forward <> io.forward // overlap forwardMask & forwardData, DO NOT CHANGE SEQUENCE 131 132 storeQueue.io.sqempty <> io.sqempty 133 134 io.exceptionAddr.vaddr := Mux(io.exceptionAddr.isStore, storeQueue.io.exceptionAddr.vaddr, loadQueue.io.exceptionAddr.vaddr) 135 136 // naive uncache arbiter 137 val s_idle :: s_load :: s_store :: Nil = Enum(3) 138 val pendingstate = RegInit(s_idle) 139 140 switch(pendingstate){ 141 is(s_idle){ 142 when(io.uncache.req.fire()){ 143 pendingstate := Mux(loadQueue.io.uncache.req.valid, s_load, s_store) 144 } 145 } 146 is(s_load){ 147 when(io.uncache.resp.fire()){ 148 pendingstate := s_idle 149 } 150 } 151 is(s_store){ 152 when(io.uncache.resp.fire()){ 153 pendingstate := s_idle 154 } 155 } 156 } 157 158 loadQueue.io.uncache := DontCare 159 storeQueue.io.uncache := DontCare 160 loadQueue.io.uncache.resp.valid := false.B 161 storeQueue.io.uncache.resp.valid := false.B 162 when(loadQueue.io.uncache.req.valid){ 163 io.uncache.req <> loadQueue.io.uncache.req 164 }.otherwise{ 165 io.uncache.req <> storeQueue.io.uncache.req 166 } 167 when(pendingstate === s_load){ 168 io.uncache.resp <> loadQueue.io.uncache.resp 169 }.otherwise{ 170 io.uncache.resp <> storeQueue.io.uncache.resp 171 } 172 173 assert(!(loadQueue.io.uncache.req.valid && storeQueue.io.uncache.req.valid)) 174 assert(!(loadQueue.io.uncache.resp.valid && storeQueue.io.uncache.resp.valid)) 175 assert(!((loadQueue.io.uncache.resp.valid || storeQueue.io.uncache.resp.valid) && pendingstate === s_idle)) 176 177 io.lqFull := loadQueue.io.lqFull 178 io.sqFull := storeQueue.io.sqFull 179} 180