xref: /XiangShan/src/main/scala/xiangshan/mem/lsqueue/LSQWrapper.scala (revision 39268f44ebf8a8acf06f33e688f2893b35710d23)
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 = Input(Valid(new Redirect))
42    val loadIn = Vec(LoadPipelineWidth, Flipped(Valid(new LsPipelineBundle)))
43    val storeIn = Vec(StorePipelineWidth, Flipped(Valid(new LsPipelineBundle)))
44    val loadDataForwarded = Vec(LoadPipelineWidth, Input(Bool()))
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.loadIn <> io.loadIn
85  loadQueue.io.storeIn <> io.storeIn
86  loadQueue.io.loadDataForwarded <> io.loadDataForwarded
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.storeIn <> io.storeIn
99  storeQueue.io.sbuffer <> io.sbuffer
100  storeQueue.io.mmioStout <> io.mmioStout
101  storeQueue.io.commits <> io.commits
102  storeQueue.io.roqDeqPtr <> io.roqDeqPtr
103  storeQueue.io.exceptionAddr.lsIdx := io.exceptionAddr.lsIdx
104  storeQueue.io.exceptionAddr.isStore := DontCare
105
106  loadQueue.io.load_s1 <> io.forward
107  storeQueue.io.forward <> io.forward // overlap forwardMask & forwardData, DO NOT CHANGE SEQUENCE
108
109  storeQueue.io.sqempty <> io.sqempty
110
111  io.exceptionAddr.vaddr := Mux(io.exceptionAddr.isStore, storeQueue.io.exceptionAddr.vaddr, loadQueue.io.exceptionAddr.vaddr)
112
113  // naive uncache arbiter
114  val s_idle :: s_load :: s_store :: Nil = Enum(3)
115  val uncacheState = RegInit(s_idle)
116
117  switch(uncacheState){
118    is(s_idle){
119      when(io.uncache.req.fire()){
120        uncacheState := Mux(loadQueue.io.uncache.req.valid, s_load, s_store)
121      }
122    }
123    is(s_load){
124      when(io.uncache.resp.fire()){
125        uncacheState := s_idle
126      }
127    }
128    is(s_store){
129      when(io.uncache.resp.fire()){
130        uncacheState := s_idle
131      }
132    }
133  }
134
135  loadQueue.io.uncache := DontCare
136  storeQueue.io.uncache := DontCare
137  loadQueue.io.uncache.resp.valid := false.B
138  storeQueue.io.uncache.resp.valid := false.B
139  when(loadQueue.io.uncache.req.valid){
140    io.uncache.req <> loadQueue.io.uncache.req
141  }.otherwise{
142    io.uncache.req <> storeQueue.io.uncache.req
143  }
144  when(uncacheState === s_load){
145    io.uncache.resp <> loadQueue.io.uncache.resp
146  }.otherwise{
147    io.uncache.resp <> storeQueue.io.uncache.resp
148  }
149
150  assert(!(loadQueue.io.uncache.req.valid && storeQueue.io.uncache.req.valid))
151  assert(!(loadQueue.io.uncache.resp.valid && storeQueue.io.uncache.resp.valid))
152  assert(!((loadQueue.io.uncache.resp.valid || storeQueue.io.uncache.resp.valid) && uncacheState === s_idle))
153
154}
155