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