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