1package xiangshan.mem 2 3import chisel3._ 4import chisel3.util._ 5import utils._ 6import xiangshan._ 7import xiangshan.cache._ 8import xiangshan.cache.{DCacheWordIO, DCacheLineIO, TlbRequestIO, MemoryOpConstants} 9import xiangshan.backend.LSUOpType 10import xiangshan.mem._ 11import xiangshan.backend.roq.RoqPtr 12 13class ExceptionAddrIO extends XSBundle { 14 val lsIdx = Input(new LSIdx) 15 val isStore = Input(Bool()) 16 val vaddr = Output(UInt(VAddrBits.W)) 17} 18 19class FwdEntry extends XSBundle { 20 val mask = Vec(8, Bool()) 21 val data = Vec(8, UInt(8.W)) 22} 23 24// inflight miss block reqs 25class InflightBlockInfo extends XSBundle { 26 val block_addr = UInt(PAddrBits.W) 27 val valid = Bool() 28} 29 30class LsqEnqIO extends XSBundle { 31 val canAccept = Output(Bool()) 32 val needAlloc = Vec(RenameWidth, Input(Bool())) 33 val req = Vec(RenameWidth, Flipped(ValidIO(new MicroOp))) 34 val resp = Vec(RenameWidth, Output(new LSIdx)) 35} 36 37// Load / Store Queue Wrapper for XiangShan Out of Order LSU 38class LsqWrappper extends XSModule with HasDCacheParameters { 39 val io = IO(new Bundle() { 40 val enq = new LsqEnqIO 41 val brqRedirect = Flipped(ValidIO(new Redirect)) 42 val flush = Input(Bool()) 43 val loadIn = Vec(LoadPipelineWidth, Flipped(Valid(new LsPipelineBundle))) 44 val storeIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle))) 45 val sbuffer = Vec(StorePipelineWidth, Decoupled(new DCacheWordReq)) 46 val ldout = Vec(2, DecoupledIO(new ExuOutput)) // writeback int load 47 val mmioStout = DecoupledIO(new ExuOutput) // writeback uncached store 48 val forward = Vec(LoadPipelineWidth, Flipped(new LoadForwardQueryIO)) 49 val commits = Flipped(new RoqCommitIO) 50 val rollback = Output(Valid(new Redirect)) 51 val dcache = Flipped(ValidIO(new Refill)) 52 val uncache = new DCacheWordIO 53 val roqDeqPtr = Input(new RoqPtr) 54 val exceptionAddr = new ExceptionAddrIO 55 val sqempty = Output(Bool()) 56 }) 57 58 val loadQueue = Module(new LoadQueue) 59 val storeQueue = Module(new StoreQueue) 60 61 // io.enq logic 62 // LSQ: send out canAccept when both load queue and store queue are ready 63 // Dispatch: send instructions to LSQ only when they are ready 64 io.enq.canAccept := loadQueue.io.enq.canAccept && storeQueue.io.enq.canAccept 65 loadQueue.io.enq.sqCanAccept := storeQueue.io.enq.canAccept 66 storeQueue.io.enq.lqCanAccept := loadQueue.io.enq.canAccept 67 for (i <- 0 until RenameWidth) { 68 val isStore = CommitType.lsInstIsStore(io.enq.req(i).bits.ctrl.commitType) 69 70 loadQueue.io.enq.needAlloc(i) := io.enq.needAlloc(i) && !isStore 71 loadQueue.io.enq.req(i).valid := !isStore && io.enq.req(i).valid 72 loadQueue.io.enq.req(i).bits := io.enq.req(i).bits 73 74 storeQueue.io.enq.needAlloc(i) := io.enq.needAlloc(i) && isStore 75 storeQueue.io.enq.req(i).valid := isStore && io.enq.req(i).valid 76 storeQueue.io.enq.req(i).bits := io.enq.req(i).bits 77 78 io.enq.resp(i).lqIdx := loadQueue.io.enq.resp(i) 79 io.enq.resp(i).sqIdx := storeQueue.io.enq.resp(i) 80 } 81 82 // load queue wiring 83 loadQueue.io.brqRedirect <> io.brqRedirect 84 loadQueue.io.flush <> io.flush 85 loadQueue.io.loadIn <> io.loadIn 86 loadQueue.io.storeIn <> io.storeIn 87 loadQueue.io.ldout <> io.ldout 88 loadQueue.io.commits <> io.commits 89 loadQueue.io.rollback <> io.rollback 90 loadQueue.io.dcache <> io.dcache 91 loadQueue.io.roqDeqPtr <> io.roqDeqPtr 92 loadQueue.io.exceptionAddr.lsIdx := io.exceptionAddr.lsIdx 93 loadQueue.io.exceptionAddr.isStore := DontCare 94 95 // store queue wiring 96 // storeQueue.io <> DontCare 97 storeQueue.io.brqRedirect <> io.brqRedirect 98 storeQueue.io.flush <> io.flush 99 storeQueue.io.storeIn <> io.storeIn 100 storeQueue.io.sbuffer <> io.sbuffer 101 storeQueue.io.mmioStout <> io.mmioStout 102 storeQueue.io.commits <> io.commits 103 storeQueue.io.roqDeqPtr <> io.roqDeqPtr 104 storeQueue.io.exceptionAddr.lsIdx := io.exceptionAddr.lsIdx 105 storeQueue.io.exceptionAddr.isStore := DontCare 106 107 loadQueue.io.load_s1 <> io.forward 108 storeQueue.io.forward <> io.forward // overlap forwardMask & forwardData, DO NOT CHANGE SEQUENCE 109 110 storeQueue.io.sqempty <> io.sqempty 111 112 io.exceptionAddr.vaddr := Mux(io.exceptionAddr.isStore, storeQueue.io.exceptionAddr.vaddr, loadQueue.io.exceptionAddr.vaddr) 113 114 // naive uncache arbiter 115 val s_idle :: s_load :: s_store :: Nil = Enum(3) 116 val uncacheState = RegInit(s_idle) 117 118 switch(uncacheState){ 119 is(s_idle){ 120 when(io.uncache.req.fire()){ 121 uncacheState := Mux(loadQueue.io.uncache.req.valid, s_load, s_store) 122 } 123 } 124 is(s_load){ 125 when(io.uncache.resp.fire()){ 126 uncacheState := s_idle 127 } 128 } 129 is(s_store){ 130 when(io.uncache.resp.fire()){ 131 uncacheState := s_idle 132 } 133 } 134 } 135 136 loadQueue.io.uncache := DontCare 137 storeQueue.io.uncache := DontCare 138 loadQueue.io.uncache.resp.valid := false.B 139 storeQueue.io.uncache.resp.valid := false.B 140 when(loadQueue.io.uncache.req.valid){ 141 io.uncache.req <> loadQueue.io.uncache.req 142 }.otherwise{ 143 io.uncache.req <> storeQueue.io.uncache.req 144 } 145 when(uncacheState === s_load){ 146 io.uncache.resp <> loadQueue.io.uncache.resp 147 }.otherwise{ 148 io.uncache.resp <> storeQueue.io.uncache.resp 149 } 150 151 assert(!(loadQueue.io.uncache.req.valid && storeQueue.io.uncache.req.valid)) 152 assert(!(loadQueue.io.uncache.resp.valid && storeQueue.io.uncache.resp.valid)) 153 assert(!((loadQueue.io.uncache.resp.valid || storeQueue.io.uncache.resp.valid) && uncacheState === s_idle)) 154 155} 156