xref: /XiangShan/src/main/scala/xiangshan/mem/MemCommon.scala (revision d2b20d1a96e238e36a849bd253f65ec7b6a5db38)
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
19
20import chipsalliance.rocketchip.config.Parameters
21import chisel3._
22import chisel3.util._
23import xiangshan._
24import utils._
25import utility._
26import xiangshan.backend.rob.RobPtr
27import xiangshan.cache._
28import xiangshan.backend.fu.FenceToSbuffer
29import xiangshan.cache.dcache.ReplayCarry
30
31object genWmask {
32  def apply(addr: UInt, sizeEncode: UInt): UInt = {
33    (LookupTree(sizeEncode, List(
34      "b00".U -> 0x1.U, //0001 << addr(2:0)
35      "b01".U -> 0x3.U, //0011
36      "b10".U -> 0xf.U, //1111
37      "b11".U -> 0xff.U //11111111
38    )) << addr(2, 0)).asUInt()
39  }
40}
41
42object genWdata {
43  def apply(data: UInt, sizeEncode: UInt): UInt = {
44    LookupTree(sizeEncode, List(
45      "b00".U -> Fill(8, data(7, 0)),
46      "b01".U -> Fill(4, data(15, 0)),
47      "b10".U -> Fill(2, data(31, 0)),
48      "b11".U -> data
49    ))
50  }
51}
52
53class LsPipelineBundle(implicit p: Parameters) extends XSBundleWithMicroOp with HasDCacheParameters{
54  val vaddr = UInt(VAddrBits.W)
55  val paddr = UInt(PAddrBits.W)
56  // val func = UInt(6.W)
57  val mask = UInt(8.W)
58  val data = UInt((XLEN+1).W)
59  val wlineflag = Bool() // store write the whole cache line
60
61  val miss = Bool()
62  val tlbMiss = Bool()
63  val ptwBack = Bool()
64  val mmio = Bool()
65  val atomic = Bool()
66  val rsIdx = UInt(log2Up(IssQueSize).W)
67
68  val forwardMask = Vec(8, Bool())
69  val forwardData = Vec(8, UInt(8.W))
70
71  // prefetch
72  val isPrefetch = Bool()
73  val isHWPrefetch = Bool()
74  def isSWPrefetch = isPrefetch && !isHWPrefetch
75
76  // For debug usage
77  val isFirstIssue = Bool()
78  val hasROBEntry = Bool()
79
80  // For load replay
81  val isLoadReplay = Bool()
82  val replayCarry = new ReplayCarry
83
84  // For dcache miss load
85  val mshrid = UInt(log2Up(cfg.nMissEntries).W)
86  val handledByMSHR = Bool()
87  val replacementUpdated = Bool()
88
89  val forward_tlDchannel = Bool()
90  val dcacheRequireReplay = Bool()
91
92  // loadQueueReplay index.
93  val sleepIndex = UInt(log2Up(LoadQueueReplaySize).W)
94}
95
96class LdPrefetchTrainBundle(implicit p: Parameters) extends LsPipelineBundle {
97  val meta_prefetch = Bool()
98  val meta_access = Bool()
99
100  def fromLsPipelineBundle(input: LsPipelineBundle) = {
101    vaddr := input.vaddr
102    paddr := input.paddr
103    mask := input.mask
104    data := input.data
105    uop := input.uop
106    wlineflag := input.wlineflag
107    miss := input.miss
108    tlbMiss := input.tlbMiss
109    ptwBack := input.ptwBack
110    mmio := input.mmio
111    rsIdx := input.rsIdx
112    forwardMask := input.forwardMask
113    forwardData := input.forwardData
114    isPrefetch := input.isPrefetch
115    isHWPrefetch := input.isHWPrefetch
116    isFirstIssue := input.isFirstIssue
117    hasROBEntry := input.hasROBEntry
118    dcacheRequireReplay := input.dcacheRequireReplay
119    sleepIndex := input.sleepIndex
120
121    meta_prefetch := DontCare
122    meta_access := DontCare
123    forward_tlDchannel := DontCare
124    mshrid := DontCare
125    replayCarry := DontCare
126    atomic := DontCare
127    isLoadReplay := DontCare
128    handledByMSHR := DontCare
129    replacementUpdated := DontCare
130  }
131}
132
133class LqWriteBundle(implicit p: Parameters) extends LsPipelineBundle {
134  // load inst replay informations
135  val replayInfo = new LoadToLsqReplayIO
136  // queue entry data, except flag bits, will be updated if writeQueue is true,
137  // valid bit in LqWriteBundle will be ignored
138  val lqDataWenDup = Vec(6, Bool()) // dirty reg dup
139
140
141  def fromLsPipelineBundle(input: LsPipelineBundle) = {
142    vaddr := input.vaddr
143    paddr := input.paddr
144    mask := input.mask
145    data := input.data
146    uop := input.uop
147    wlineflag := input.wlineflag
148    miss := input.miss
149    tlbMiss := input.tlbMiss
150    ptwBack := input.ptwBack
151    mmio := input.mmio
152    atomic := input.atomic
153    rsIdx := input.rsIdx
154    forwardMask := input.forwardMask
155    forwardData := input.forwardData
156    isPrefetch := input.isPrefetch
157    isHWPrefetch := input.isHWPrefetch
158    isFirstIssue := input.isFirstIssue
159    hasROBEntry := input.hasROBEntry
160    isLoadReplay := input.isLoadReplay
161    mshrid := input.mshrid
162    forward_tlDchannel := input.forward_tlDchannel
163    replayCarry := input.replayCarry
164    dcacheRequireReplay := input.dcacheRequireReplay
165    sleepIndex := input.sleepIndex
166    handledByMSHR := input.handledByMSHR
167    replacementUpdated := input.replacementUpdated
168
169    replayInfo := DontCare
170    lqDataWenDup := DontCare
171  }
172}
173
174class LoadForwardQueryIO(implicit p: Parameters) extends XSBundleWithMicroOp {
175  val vaddr = Output(UInt(VAddrBits.W))
176  val paddr = Output(UInt(PAddrBits.W))
177  val mask = Output(UInt(8.W))
178  override val uop = Output(new MicroOp) // for replay
179  val pc = Output(UInt(VAddrBits.W)) //for debug
180  val valid = Output(Bool())
181
182  val forwardMaskFast = Input(Vec(8, Bool())) // resp to load_s1
183  val forwardMask = Input(Vec(8, Bool())) // resp to load_s2
184  val forwardData = Input(Vec(8, UInt(8.W))) // resp to load_s2
185
186  // val lqIdx = Output(UInt(LoadQueueIdxWidth.W))
187  val sqIdx = Output(new SqPtr)
188
189  // dataInvalid suggests store to load forward found forward should happen,
190  // but data is not available for now. If dataInvalid, load inst should
191  // be replayed from RS. Feedback type should be RSFeedbackType.dataInvalid
192  val dataInvalid = Input(Bool()) // Addr match, but data is not valid for now
193
194  // matchInvalid suggests in store to load forward logic, paddr cam result does
195  // to equal to vaddr cam result. If matchInvalid, a microarchitectural exception
196  // should be raised to flush SQ and committed sbuffer.
197  val matchInvalid = Input(Bool()) // resp to load_s2
198
199  // addrInvalid suggests store to load forward found forward should happen,
200  // but address (SSID) is not available for now. If addrInvalid, load inst should
201  // be replayed from RS. Feedback type should be RSFeedbackType.addrInvalid
202  val addrInvalid = Input(Bool())
203}
204
205// LoadForwardQueryIO used in load pipeline
206//
207// Difference between PipeLoadForwardQueryIO and LoadForwardQueryIO:
208// PipeIO use predecoded sqIdxMask for better forward timing
209class PipeLoadForwardQueryIO(implicit p: Parameters) extends LoadForwardQueryIO {
210  // val sqIdx = Output(new SqPtr) // for debug, should not be used in pipeline for timing reasons
211  // sqIdxMask is calcuated in earlier stage for better timing
212  val sqIdxMask = Output(UInt(StoreQueueSize.W))
213
214  // dataInvalid: addr match, but data is not valid for now
215  val dataInvalidFast = Input(Bool()) // resp to load_s1
216  // val dataInvalid = Input(Bool()) // resp to load_s2
217  val dataInvalidSqIdx = Input(new SqPtr) // resp to load_s2, sqIdx
218  val addrInvalidSqIdx = Input(new SqPtr) // resp to load_s2, sqIdx
219}
220
221// Query load queue for ld-ld violation
222//
223// Req should be send in load_s1
224// Resp will be generated 1 cycle later
225//
226// Note that query req may be !ready, as dcache is releasing a block
227// If it happens, a replay from rs is needed.
228
229class LoadViolationQueryReq(implicit p: Parameters) extends XSBundleWithMicroOp { // provide lqIdx
230  // mask: load's data mask.
231  val mask = UInt(8.W)
232
233  // paddr: load's paddr.
234  val paddr = UInt(PAddrBits.W)
235
236  // dataInvalid: load data is invalid.
237  val datavalid = Bool()
238}
239
240class LoadViolationQueryResp(implicit p: Parameters) extends XSBundle {
241  // replayFromFetch: ld-ld violation check success, replay from fetch.
242  val replayFromFetch = Bool()
243}
244
245class LoadViolationQueryIO(implicit p: Parameters) extends XSBundle {
246  val req = Decoupled(new LoadViolationQueryReq)
247  val resp = Flipped(Valid(new LoadViolationQueryResp))
248  val preReq = Output(Bool())
249  val release = Output(Bool())
250}
251
252class LoadReExecuteQueryIO(implicit p: Parameters) extends XSBundle {
253  //  robIdx: Requestor's (a store instruction) rob index for match logic.
254  val robIdx = new RobPtr
255
256  //  paddr: requestor's (a store instruction) physical address for match logic.
257  val paddr = UInt(PAddrBits.W)
258
259  //  mask: requestor's (a store instruction) data width mask for match logic.
260  val mask = UInt(8.W)
261}
262
263// Store byte valid mask write bundle
264//
265// Store byte valid mask write to SQ takes 2 cycles
266class StoreMaskBundle(implicit p: Parameters) extends XSBundle {
267  val sqIdx = new SqPtr
268  val mask = UInt(8.W)
269}
270
271class LoadDataFromDcacheBundle(implicit p: Parameters) extends DCacheBundle {
272  // old dcache: optimize data sram read fanout
273  // val bankedDcacheData = Vec(DCacheBanks, UInt(64.W))
274  // val bank_oh = UInt(DCacheBanks.W)
275
276  // new dcache
277  val respDcacheData = UInt(XLEN.W)
278  val forwardMask = Vec(8, Bool())
279  val forwardData = Vec(8, UInt(8.W))
280  val uop = new MicroOp // for data selection, only fwen and fuOpType are used
281  val addrOffset = UInt(3.W) // for data selection
282
283  // forward tilelink D channel
284  val forward_D = Input(Bool())
285  val forwardData_D = Input(Vec(8, UInt(8.W)))
286
287  // forward mshr data
288  val forward_mshr = Input(Bool())
289  val forwardData_mshr = Input(Vec(8, UInt(8.W)))
290
291  val forward_result_valid = Input(Bool())
292
293  def dcacheData(): UInt = {
294    // old dcache
295    // val dcache_data = Mux1H(bank_oh, bankedDcacheData)
296    // new dcache
297    val dcache_data = respDcacheData
298    val use_D = forward_D && forward_result_valid
299    val use_mshr = forward_mshr && forward_result_valid
300    Mux(use_D, forwardData_D.asUInt, Mux(use_mshr, forwardData_mshr.asUInt, dcache_data))
301  }
302
303  def mergedData(): UInt = {
304    val rdataVec = VecInit((0 until XLEN / 8).map(j =>
305      Mux(forwardMask(j), forwardData(j), dcacheData()(8*(j+1)-1, 8*j))
306    ))
307    rdataVec.asUInt
308  }
309}
310
311// Load writeback data from load queue (refill)
312class LoadDataFromLQBundle(implicit p: Parameters) extends XSBundle {
313  val lqData = UInt(64.W) // load queue has merged data
314  val uop = new MicroOp // for data selection, only fwen and fuOpType are used
315  val addrOffset = UInt(3.W) // for data selection
316
317  def mergedData(): UInt = {
318    lqData
319  }
320}
321
322// Bundle for load / store wait waking up
323class MemWaitUpdateReq(implicit p: Parameters) extends XSBundle {
324  val staIssue = Vec(exuParameters.StuCnt, ValidIO(new ExuInput))
325  val stdIssue = Vec(exuParameters.StuCnt, ValidIO(new ExuInput))
326}
327
328object AddPipelineReg {
329  class PipelineRegModule[T <: Data](gen: T) extends Module {
330    val io = IO(new Bundle() {
331      val in = Flipped(DecoupledIO(gen.cloneType))
332      val out = DecoupledIO(gen.cloneType)
333      val isFlush = Input(Bool())
334    })
335
336    val valid = RegInit(false.B)
337    valid.suggestName("pipeline_reg_valid")
338    when (io.out.fire()) { valid := false.B }
339    when (io.in.fire()) { valid := true.B }
340    when (io.isFlush) { valid := false.B }
341
342    io.in.ready := !valid || io.out.ready
343    io.out.bits := RegEnable(io.in.bits, io.in.fire())
344    io.out.valid := valid //&& !isFlush
345  }
346
347  def apply[T <: Data]
348  (left: DecoupledIO[T], right: DecoupledIO[T], isFlush: Bool,
349   moduleName: Option[String] = None
350  ){
351    val pipelineReg = Module(new PipelineRegModule[T](left.bits.cloneType))
352    if(moduleName.nonEmpty) pipelineReg.suggestName(moduleName.get)
353    pipelineReg.io.in <> left
354    right <> pipelineReg.io.out
355    pipelineReg.io.isFlush := isFlush
356  }
357}
358