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