xref: /XiangShan/src/main/scala/xiangshan/mem/MemCommon.scala (revision 7531c765d87d92e3c772b0d55aa810c2041ba3e3)
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 org.chipsalliance.cde.config.Parameters
21import chisel3._
22import chisel3.util._
23import utility._
24import utils._
25import xiangshan._
26import xiangshan.backend.Bundles.{DynInst, MemExuInput}
27import xiangshan.backend.rob.RobPtr
28import xiangshan.cache._
29import xiangshan.backend.fu.FenceToSbuffer
30import xiangshan.cache.wpu.ReplayCarry
31import xiangshan.mem.prefetch.PrefetchReqBundle
32
33object genWmask {
34  def apply(addr: UInt, sizeEncode: UInt): UInt = {
35    (LookupTree(sizeEncode, List(
36      "b00".U -> 0x1.U, //0001 << addr(2:0)
37      "b01".U -> 0x3.U, //0011
38      "b10".U -> 0xf.U, //1111
39      "b11".U -> 0xff.U //11111111
40    )) << addr(2, 0)).asUInt
41  }
42}
43
44object genVWmask {
45  def apply(addr: UInt, sizeEncode: UInt): UInt = {
46    (LookupTree(sizeEncode, List(
47      "b00".U -> 0x1.U, //0001 << addr(2:0)
48      "b01".U -> 0x3.U, //0011
49      "b10".U -> 0xf.U, //1111
50      "b11".U -> 0xff.U //11111111
51    )) << addr(3, 0)).asUInt
52  }
53}
54
55object genWdata {
56  def apply(data: UInt, sizeEncode: UInt): UInt = {
57    LookupTree(sizeEncode, List(
58      "b00".U -> Fill(16, data(7, 0)),
59      "b01".U -> Fill(8, data(15, 0)),
60      "b10".U -> Fill(4, data(31, 0)),
61      "b11".U -> Fill(2, data(63,0))
62    ))
63  }
64}
65
66object shiftDataToLow {
67  def apply(addr: UInt,data : UInt): UInt = {
68    Mux(addr(3), (data >> 64).asUInt,data)
69  }
70}
71object shiftMaskToLow {
72  def apply(addr: UInt,mask: UInt): UInt = {
73    Mux(addr(3),(mask >> 8).asUInt,mask)
74  }
75}
76
77class LsPipelineBundle(implicit p: Parameters) extends XSBundle
78  with HasDCacheParameters
79  with HasVLSUParameters {
80  val uop = new DynInst
81  val vaddr = UInt(VAddrBits.W)
82  val paddr = UInt(PAddrBits.W)
83  // val func = UInt(6.W)
84  val mask = UInt((VLEN/8).W)
85  val data = UInt((VLEN+1).W)
86  val wlineflag = Bool() // store write the whole cache line
87
88  val miss = Bool()
89  val tlbMiss = Bool()
90  val ptwBack = Bool()
91  val mmio = Bool()
92  val atomic = Bool()
93  val rsIdx = UInt(log2Up(MemIQSizeMax).W)
94
95  val forwardMask = Vec(VLEN/8, Bool())
96  val forwardData = Vec(VLEN/8, UInt(8.W))
97
98  // prefetch
99  val isPrefetch = Bool()
100  val isHWPrefetch = Bool()
101  def isSWPrefetch = isPrefetch && !isHWPrefetch
102
103  // vector
104  val isvec = Bool()
105  val is128bit = Bool()
106  val uop_unit_stride_fof = Bool()
107  // val rob_idx_valid = Vec(2,Bool())
108  // val inner_idx = Vec(2,UInt(3.W))
109  // val rob_idx = Vec(2,new RobPtr)
110  val reg_offset = UInt(vOffsetBits.W)
111  // val offset = Vec(2,UInt(4.W))
112  val exp = Bool()
113  val is_first_ele = Bool()
114  val flowIdx = UInt(8.W)
115  val flowPtr = new VlflowPtr()
116  val fqIdx = UInt(log2Ceil(VsFlowSize).W)
117
118  // For debug usage
119  val isFirstIssue = Bool()
120  val hasROBEntry = Bool()
121
122  // For load replay
123  val isLoadReplay = Bool()
124  val isFastPath = Bool()
125  val isFastReplay = Bool()
126  val replayCarry = new ReplayCarry(nWays)
127
128  // For dcache miss load
129  val mshrid = UInt(log2Up(cfg.nMissEntries).W)
130  val handledByMSHR = Bool()
131  val replacementUpdated = Bool()
132  val missDbUpdated = Bool()
133
134  val forward_tlDchannel = Bool()
135  val dcacheRequireReplay = Bool()
136  val delayedLoadError = Bool()
137  val lateKill = Bool()
138  val feedbacked = Bool()
139  val ldCancel = ValidUndirectioned(UInt(log2Ceil(LoadPipelineWidth).W))
140  // loadQueueReplay index.
141  val schedIndex = UInt(log2Up(LoadQueueReplaySize).W)
142
143  // issue dequeue port index
144  val deqPortIdx = UInt(log2Ceil(LoadPipelineWidth).W)
145}
146
147class LdPrefetchTrainBundle(implicit p: Parameters) extends LsPipelineBundle {
148  val meta_prefetch = UInt(L1PfSourceBits.W)
149  val meta_access = Bool()
150
151  def fromLsPipelineBundle(input: LsPipelineBundle) = {
152    vaddr := input.vaddr
153    paddr := input.paddr
154    mask := input.mask
155    data := input.data
156    uop := input.uop
157    wlineflag := input.wlineflag
158    miss := input.miss
159    tlbMiss := input.tlbMiss
160    ptwBack := input.ptwBack
161    mmio := input.mmio
162    rsIdx := input.rsIdx
163    forwardMask := input.forwardMask
164    forwardData := input.forwardData
165    isPrefetch := input.isPrefetch
166    isHWPrefetch := input.isHWPrefetch
167
168    // VLSU
169    isvec := input.isvec
170    is128bit := input.is128bit
171    exp := input.exp
172    flowIdx := input.flowIdx
173    is_first_ele := input.is_first_ele
174    uop_unit_stride_fof := input.uop_unit_stride_fof
175    // rob_idx_valid := input.rob_idx_valid
176    // rob_idx := input.rob_idx
177    // inner_idx := input.inner_idx
178    reg_offset := input.reg_offset
179    // offset := input.offset
180    fqIdx := input.fqIdx
181    flowPtr := input.flowPtr
182    isFirstIssue := input.isFirstIssue
183    dcacheRequireReplay := input.dcacheRequireReplay
184
185    isFirstIssue := input.isFirstIssue
186    hasROBEntry := input.hasROBEntry
187    dcacheRequireReplay := input.dcacheRequireReplay
188    schedIndex := input.schedIndex
189
190    meta_prefetch := DontCare
191    meta_access := DontCare
192    forward_tlDchannel := DontCare
193    mshrid := DontCare
194    replayCarry := DontCare
195    atomic := DontCare
196    isLoadReplay := DontCare
197    isFastPath := DontCare
198    isFastReplay := DontCare
199    handledByMSHR := DontCare
200    replacementUpdated := DontCare
201    missDbUpdated := DontCare
202    delayedLoadError := DontCare
203    lateKill := DontCare
204    feedbacked := DontCare
205    deqPortIdx := DontCare
206    ldCancel := DontCare
207  }
208
209  def asPrefetchReqBundle(): PrefetchReqBundle = {
210    val res = Wire(new PrefetchReqBundle)
211    res.vaddr := this.vaddr
212    res.paddr := this.paddr
213    res.pc    := this.uop.pc
214
215    res
216  }
217}
218
219class StPrefetchTrainBundle(implicit p: Parameters) extends LdPrefetchTrainBundle {}
220
221class LqWriteBundle(implicit p: Parameters) extends LsPipelineBundle {
222  // load inst replay informations
223  val rep_info = new LoadToLsqReplayIO
224  // queue entry data, except flag bits, will be updated if writeQueue is true,
225  // valid bit in LqWriteBundle will be ignored
226  val data_wen_dup = Vec(6, Bool()) // dirty reg dup
227
228
229  def fromLsPipelineBundle(input: LsPipelineBundle) = {
230    vaddr := input.vaddr
231    paddr := input.paddr
232    mask := input.mask
233    data := input.data
234    uop := input.uop
235    wlineflag := input.wlineflag
236    miss := input.miss
237    tlbMiss := input.tlbMiss
238    ptwBack := input.ptwBack
239    mmio := input.mmio
240    atomic := input.atomic
241    rsIdx := input.rsIdx
242    forwardMask := input.forwardMask
243    forwardData := input.forwardData
244    isPrefetch := input.isPrefetch
245    isHWPrefetch := input.isHWPrefetch
246
247    //VLSU
248    isvec := input.isvec
249    is128bit := input.is128bit
250    exp := input.exp
251    uop_unit_stride_fof := input.uop_unit_stride_fof
252    // rob_idx_valid := input.rob_idx_valid
253    // rob_idx := input.rob_idx
254    // inner_idx := input.inner_idx
255    reg_offset := input.reg_offset
256    // offset := input.offset
257    fqIdx := input.fqIdx
258
259    isFirstIssue := input.isFirstIssue
260    hasROBEntry := input.hasROBEntry
261    isLoadReplay := input.isLoadReplay
262    isFastPath := input.isFastPath
263    isFastReplay := input.isFastReplay
264    mshrid := input.mshrid
265    forward_tlDchannel := input.forward_tlDchannel
266    replayCarry := input.replayCarry
267    dcacheRequireReplay := input.dcacheRequireReplay
268    schedIndex := input.schedIndex
269    handledByMSHR := input.handledByMSHR
270    replacementUpdated := input.replacementUpdated
271    missDbUpdated := input.missDbUpdated
272    delayedLoadError := input.delayedLoadError
273    lateKill := input.lateKill
274    feedbacked := input.feedbacked
275
276    rep_info := DontCare
277    data_wen_dup := DontCare
278  }
279}
280
281class LoadForwardQueryIO(implicit p: Parameters) extends XSBundle {
282  val vaddr = Output(UInt(VAddrBits.W))
283  val paddr = Output(UInt(PAddrBits.W))
284  val mask = Output(UInt((VLEN/8).W))
285  val uop = Output(new DynInst) // for replay
286  val pc = Output(UInt(VAddrBits.W)) //for debug
287  val valid = Output(Bool())
288
289  val forwardMaskFast = Input(Vec((VLEN/8), Bool())) // resp to load_s1
290  val forwardMask = Input(Vec((VLEN/8), Bool())) // resp to load_s2
291  val forwardData = Input(Vec((VLEN/8), UInt(8.W))) // resp to load_s2
292
293  // val lqIdx = Output(UInt(LoadQueueIdxWidth.W))
294  val sqIdx = Output(new SqPtr)
295
296  // dataInvalid suggests store to load forward found forward should happen,
297  // but data is not available for now. If dataInvalid, load inst should
298  // be replayed from RS. Feedback type should be RSFeedbackType.dataInvalid
299  val dataInvalid = Input(Bool()) // Addr match, but data is not valid for now
300
301  // matchInvalid suggests in store to load forward logic, paddr cam result does
302  // to equal to vaddr cam result. If matchInvalid, a microarchitectural exception
303  // should be raised to flush SQ and committed sbuffer.
304  val matchInvalid = Input(Bool()) // resp to load_s2
305
306  // addrInvalid suggests store to load forward found forward should happen,
307  // but address (SSID) is not available for now. If addrInvalid, load inst should
308  // be replayed from RS. Feedback type should be RSFeedbackType.addrInvalid
309  val addrInvalid = Input(Bool())
310}
311
312// LoadForwardQueryIO used in load pipeline
313//
314// Difference between PipeLoadForwardQueryIO and LoadForwardQueryIO:
315// PipeIO use predecoded sqIdxMask for better forward timing
316class PipeLoadForwardQueryIO(implicit p: Parameters) extends LoadForwardQueryIO {
317  // val sqIdx = Output(new SqPtr) // for debug, should not be used in pipeline for timing reasons
318  // sqIdxMask is calcuated in earlier stage for better timing
319  val sqIdxMask = Output(UInt(StoreQueueSize.W))
320
321  // dataInvalid: addr match, but data is not valid for now
322  val dataInvalidFast = Input(Bool()) // resp to load_s1
323  // val dataInvalid = Input(Bool()) // resp to load_s2
324  val dataInvalidSqIdx = Input(new SqPtr) // resp to load_s2, sqIdx
325  val addrInvalidSqIdx = Input(new SqPtr) // resp to load_s2, sqIdx
326}
327
328// Query load queue for ld-ld violation
329//
330// Req should be send in load_s1
331// Resp will be generated 1 cycle later
332//
333// Note that query req may be !ready, as dcache is releasing a block
334// If it happens, a replay from rs is needed.
335class LoadNukeQueryReq(implicit p: Parameters) extends XSBundle { // provide lqIdx
336  val uop = new DynInst
337  // mask: load's data mask.
338  val mask = UInt((VLEN/8).W)
339
340  // paddr: load's paddr.
341  val paddr      = UInt(PAddrBits.W)
342  // dataInvalid: load data is invalid.
343  val data_valid = Bool()
344}
345
346class LoadNukeQueryResp(implicit p: Parameters) extends XSBundle {
347  // rep_frm_fetch: ld-ld violation check success, replay from fetch.
348  val rep_frm_fetch = Bool()
349}
350
351class LoadNukeQueryIO(implicit p: Parameters) extends XSBundle {
352  val req    = Decoupled(new LoadNukeQueryReq)
353  val resp   = Flipped(Valid(new LoadNukeQueryResp))
354  val revoke = Output(Bool())
355}
356
357class StoreNukeQueryIO(implicit p: Parameters) extends XSBundle {
358  //  robIdx: Requestor's (a store instruction) rob index for match logic.
359  val robIdx = new RobPtr
360
361  //  paddr: requestor's (a store instruction) physical address for match logic.
362  val paddr  = UInt(PAddrBits.W)
363
364  //  mask: requestor's (a store instruction) data width mask for match logic.
365  val mask = UInt((VLEN/8).W)
366}
367
368// Store byte valid mask write bundle
369//
370// Store byte valid mask write to SQ takes 2 cycles
371class StoreMaskBundle(implicit p: Parameters) extends XSBundle {
372  val sqIdx = new SqPtr
373  val mask = UInt((VLEN/8).W)
374}
375
376class LoadDataFromDcacheBundle(implicit p: Parameters) extends DCacheBundle {
377  // old dcache: optimize data sram read fanout
378  // val bankedDcacheData = Vec(DCacheBanks, UInt(64.W))
379  // val bank_oh = UInt(DCacheBanks.W)
380
381  // new dcache
382  val respDcacheData = UInt(VLEN.W)
383  val forwardMask = Vec(VLEN/8, Bool())
384  val forwardData = Vec(VLEN/8, UInt(8.W))
385  val uop = new DynInst // for data selection, only fwen and fuOpType are used
386  val addrOffset = UInt(4.W) // for data selection
387  // forward tilelink D channel
388  val forward_D = Bool()
389  val forwardData_D = Vec(VLEN/8, UInt(8.W))
390
391  // forward mshr data
392  val forward_mshr = Bool()
393  val forwardData_mshr = Vec(VLEN/8, UInt(8.W))
394
395  val forward_result_valid = Bool()
396
397  def dcacheData(): UInt = {
398    // old dcache
399    // val dcache_data = Mux1H(bank_oh, bankedDcacheData)
400    // new dcache
401    val dcache_data = respDcacheData
402    val use_D = forward_D && forward_result_valid
403    val use_mshr = forward_mshr && forward_result_valid
404    Mux(use_D, forwardData_D.asUInt, Mux(use_mshr, forwardData_mshr.asUInt, dcache_data))
405  }
406
407  def mergedData(): UInt = {
408    val rdataVec = VecInit((0 until VLEN / 8).map(j =>
409      Mux(forwardMask(j), forwardData(j), dcacheData()(8*(j+1)-1, 8*j))
410    ))
411    rdataVec.asUInt
412  }
413}
414
415// Load writeback data from load queue (refill)
416class LoadDataFromLQBundle(implicit p: Parameters) extends XSBundle {
417  val lqData = UInt(64.W) // load queue has merged data
418  val uop = new DynInst // for data selection, only fwen and fuOpType are used
419  val addrOffset = UInt(3.W) // for data selection
420
421  def mergedData(): UInt = {
422    lqData
423  }
424}
425
426// Bundle for load / store wait waking up
427class MemWaitUpdateReq(implicit p: Parameters) extends XSBundle {
428  val robIdx = Vec(backendParams.StaCnt, ValidIO(new RobPtr))
429  val sqIdx = Vec(backendParams.StdCnt, ValidIO(new SqPtr))
430}
431
432object AddPipelineReg {
433  class PipelineRegModule[T <: Data](gen: T) extends Module {
434    val io = IO(new Bundle() {
435      val in = Flipped(DecoupledIO(gen.cloneType))
436      val out = DecoupledIO(gen.cloneType)
437      val isFlush = Input(Bool())
438    })
439
440    val valid = RegInit(false.B)
441    valid.suggestName("pipeline_reg_valid")
442    when (io.out.fire) { valid := false.B }
443    when (io.in.fire) { valid := true.B }
444    when (io.isFlush) { valid := false.B }
445
446    io.in.ready := !valid || io.out.ready
447    io.out.bits := RegEnable(io.in.bits, io.in.fire)
448    io.out.valid := valid //&& !isFlush
449  }
450
451  def apply[T <: Data]
452  (left: DecoupledIO[T], right: DecoupledIO[T], isFlush: Bool,
453   moduleName: Option[String] = None
454  ){
455    val pipelineReg = Module(new PipelineRegModule[T](left.bits.cloneType))
456    if(moduleName.nonEmpty) pipelineReg.suggestName(moduleName.get)
457    pipelineReg.io.in <> left
458    right <> pipelineReg.io.out
459    pipelineReg.io.isFlush := isFlush
460  }
461}
462