xref: /XiangShan/src/main/scala/xiangshan/mem/lsqueue/LSQWrapper.scala (revision 20e31bd1101a7af14218b345e5b3fb93d629a83b)
1package xiangshan.mem
2
3import chipsalliance.rocketchip.config.Parameters
4import chisel3._
5import chisel3.util._
6import utils._
7import xiangshan._
8import xiangshan.cache._
9import xiangshan.cache.{DCacheWordIO, DCacheLineIO, TlbRequestIO, MemoryOpConstants}
10import xiangshan.mem._
11import xiangshan.backend.roq.RoqLsqIO
12
13class ExceptionAddrIO(implicit p: Parameters) 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 Bundle {
20  val valid = Bool()
21  val data = UInt(8.W)
22}
23
24// inflight miss block reqs
25class InflightBlockInfo(implicit p: Parameters) extends XSBundle {
26  val block_addr = UInt(PAddrBits.W)
27  val valid = Bool()
28}
29
30class LsqEnqIO(implicit p: Parameters) extends XSBundle {
31  val canAccept = Output(Bool())
32  val needAlloc = Vec(RenameWidth, Input(UInt(2.W)))
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(implicit p: Parameters) 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 storeDataIn = Vec(StorePipelineWidth, Flipped(Valid(new StoreDataBundle))) // store data, send to sq from rs
46    val loadDataForwarded = Vec(LoadPipelineWidth, Input(Bool()))
47    val needReplayFromRS = Vec(LoadPipelineWidth, Input(Bool()))
48    val sbuffer = Vec(StorePipelineWidth, Decoupled(new DCacheWordReq))
49    val ldout = Vec(2, DecoupledIO(new ExuOutput)) // writeback int load
50    val mmioStout = DecoupledIO(new ExuOutput) // writeback uncached store
51    val forward = Vec(LoadPipelineWidth, Flipped(new PipeLoadForwardQueryIO))
52    val roq = Flipped(new RoqLsqIO)
53    val rollback = Output(Valid(new Redirect))
54    val dcache = Flipped(ValidIO(new Refill))
55    val uncache = new DCacheWordIO
56    val exceptionAddr = new ExceptionAddrIO
57    val sqempty = Output(Bool())
58    val issuePtrExt = Output(new SqPtr)
59    val storeIssue = Vec(StorePipelineWidth, Flipped(Valid(new ExuInput)))
60    val sqFull = Output(Bool())
61    val lqFull = Output(Bool())
62  })
63
64  val loadQueue = Module(new LoadQueue)
65  val storeQueue = Module(new StoreQueue)
66
67  // io.enq logic
68  // LSQ: send out canAccept when both load queue and store queue are ready
69  // Dispatch: send instructions to LSQ only when they are ready
70  io.enq.canAccept := loadQueue.io.enq.canAccept && storeQueue.io.enq.canAccept
71  loadQueue.io.enq.sqCanAccept := storeQueue.io.enq.canAccept
72  storeQueue.io.enq.lqCanAccept := loadQueue.io.enq.canAccept
73  for (i <- 0 until RenameWidth) {
74    loadQueue.io.enq.needAlloc(i) := io.enq.needAlloc(i)(0)
75    loadQueue.io.enq.req(i).valid := io.enq.needAlloc(i)(0) && io.enq.req(i).valid
76    loadQueue.io.enq.req(i).bits  := io.enq.req(i).bits
77
78    storeQueue.io.enq.needAlloc(i) := io.enq.needAlloc(i)(1)
79    storeQueue.io.enq.req(i).valid := io.enq.needAlloc(i)(1) && io.enq.req(i).valid
80    storeQueue.io.enq.req(i).bits  := io.enq.req(i).bits
81
82    io.enq.resp(i).lqIdx := loadQueue.io.enq.resp(i)
83    io.enq.resp(i).sqIdx := storeQueue.io.enq.resp(i)
84  }
85
86  // load queue wiring
87  loadQueue.io.brqRedirect <> io.brqRedirect
88  loadQueue.io.flush <> io.flush
89  loadQueue.io.loadIn <> io.loadIn
90  loadQueue.io.storeIn <> io.storeIn
91  loadQueue.io.loadDataForwarded <> io.loadDataForwarded
92  loadQueue.io.needReplayFromRS <> io.needReplayFromRS
93  loadQueue.io.ldout <> io.ldout
94  loadQueue.io.roq <> io.roq
95  loadQueue.io.rollback <> io.rollback
96  loadQueue.io.dcache <> io.dcache
97  loadQueue.io.exceptionAddr.lsIdx := io.exceptionAddr.lsIdx
98  loadQueue.io.exceptionAddr.isStore := DontCare
99
100  // store queue wiring
101  // storeQueue.io <> DontCare
102  storeQueue.io.brqRedirect <> io.brqRedirect
103  storeQueue.io.flush <> io.flush
104  storeQueue.io.storeIn <> io.storeIn
105  storeQueue.io.storeDataIn <> io.storeDataIn
106  storeQueue.io.sbuffer <> io.sbuffer
107  storeQueue.io.mmioStout <> io.mmioStout
108  storeQueue.io.roq <> io.roq
109  storeQueue.io.exceptionAddr.lsIdx := io.exceptionAddr.lsIdx
110  storeQueue.io.exceptionAddr.isStore := DontCare
111  storeQueue.io.issuePtrExt <> io.issuePtrExt
112  storeQueue.io.storeIssue <> io.storeIssue
113
114  loadQueue.io.load_s1 <> io.forward
115  storeQueue.io.forward <> io.forward // overlap forwardMask & forwardData, DO NOT CHANGE SEQUENCE
116
117  storeQueue.io.sqempty <> io.sqempty
118
119  io.exceptionAddr.vaddr := Mux(io.exceptionAddr.isStore, storeQueue.io.exceptionAddr.vaddr, loadQueue.io.exceptionAddr.vaddr)
120
121  // naive uncache arbiter
122  val s_idle :: s_load :: s_store :: Nil = Enum(3)
123  val pendingstate = RegInit(s_idle)
124
125  switch(pendingstate){
126    is(s_idle){
127      when(io.uncache.req.fire()){
128        pendingstate := Mux(loadQueue.io.uncache.req.valid, s_load, s_store)
129      }
130    }
131    is(s_load){
132      when(io.uncache.resp.fire()){
133        pendingstate := s_idle
134      }
135    }
136    is(s_store){
137      when(io.uncache.resp.fire()){
138        pendingstate := s_idle
139      }
140    }
141  }
142
143  loadQueue.io.uncache := DontCare
144  storeQueue.io.uncache := DontCare
145  loadQueue.io.uncache.resp.valid := false.B
146  storeQueue.io.uncache.resp.valid := false.B
147  when(loadQueue.io.uncache.req.valid){
148    io.uncache.req <> loadQueue.io.uncache.req
149  }.otherwise{
150    io.uncache.req <> storeQueue.io.uncache.req
151  }
152  when(pendingstate === s_load){
153    io.uncache.resp <> loadQueue.io.uncache.resp
154  }.otherwise{
155    io.uncache.resp <> storeQueue.io.uncache.resp
156  }
157
158  assert(!(loadQueue.io.uncache.req.valid && storeQueue.io.uncache.req.valid))
159  assert(!(loadQueue.io.uncache.resp.valid && storeQueue.io.uncache.resp.valid))
160  assert(!((loadQueue.io.uncache.resp.valid || storeQueue.io.uncache.resp.valid) && pendingstate === s_idle))
161
162  io.lqFull := loadQueue.io.lqFull
163  io.sqFull := storeQueue.io.sqFull
164}
165