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