xref: /XiangShan/src/main/scala/xiangshan/mem/MemCommon.scala (revision a63155a6a44b3c7714e55906b55ebf92e0efc125)
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  val ldCancel = ValidUndirectioned(UInt(log2Ceil(LoadPipelineWidth).W))
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
153    meta_prefetch := DontCare
154    meta_access := DontCare
155    forward_tlDchannel := DontCare
156    mshrid := DontCare
157    replayCarry := DontCare
158    atomic := DontCare
159    isLoadReplay := DontCare
160    isFastPath := DontCare
161    isFastReplay := DontCare
162    handledByMSHR := DontCare
163    replacementUpdated := DontCare
164    delayedLoadError := DontCare
165    lateKill := DontCare
166    feedbacked := DontCare
167    deqPortIdx := DontCare
168    ldCancel := DontCare
169  }
170}
171
172class LqWriteBundle(implicit p: Parameters) extends LsPipelineBundle {
173  // load inst replay informations
174  val rep_info = new LoadToLsqReplayIO
175  // queue entry data, except flag bits, will be updated if writeQueue is true,
176  // valid bit in LqWriteBundle will be ignored
177  val data_wen_dup = Vec(6, Bool()) // dirty reg dup
178
179
180  def fromLsPipelineBundle(input: LsPipelineBundle) = {
181    vaddr := input.vaddr
182    paddr := input.paddr
183    mask := input.mask
184    data := input.data
185    uop := input.uop
186    wlineflag := input.wlineflag
187    miss := input.miss
188    tlbMiss := input.tlbMiss
189    ptwBack := input.ptwBack
190    mmio := input.mmio
191    atomic := input.atomic
192    rsIdx := input.rsIdx
193    forwardMask := input.forwardMask
194    forwardData := input.forwardData
195    isPrefetch := input.isPrefetch
196    isHWPrefetch := input.isHWPrefetch
197    isFirstIssue := input.isFirstIssue
198    hasROBEntry := input.hasROBEntry
199    isLoadReplay := input.isLoadReplay
200    isFastPath := input.isFastPath
201    isFastReplay := input.isFastReplay
202    mshrid := input.mshrid
203    forward_tlDchannel := input.forward_tlDchannel
204    replayCarry := input.replayCarry
205    dcacheRequireReplay := input.dcacheRequireReplay
206    schedIndex := input.schedIndex
207    handledByMSHR := input.handledByMSHR
208    replacementUpdated := input.replacementUpdated
209    delayedLoadError := input.delayedLoadError
210    lateKill := input.lateKill
211    feedbacked := input.feedbacked
212
213    rep_info := DontCare
214    data_wen_dup := DontCare
215  }
216}
217
218class LoadForwardQueryIO(implicit p: Parameters) extends XSBundle {
219  val vaddr = Output(UInt(VAddrBits.W))
220  val paddr = Output(UInt(PAddrBits.W))
221  val mask = Output(UInt((VLEN/8).W))
222  val uop = Output(new DynInst) // for replay
223  val pc = Output(UInt(VAddrBits.W)) //for debug
224  val valid = Output(Bool())
225
226  val forwardMaskFast = Input(Vec((VLEN/8), Bool())) // resp to load_s1
227  val forwardMask = Input(Vec((VLEN/8), Bool())) // resp to load_s2
228  val forwardData = Input(Vec((VLEN/8), UInt(8.W))) // resp to load_s2
229
230  // val lqIdx = Output(UInt(LoadQueueIdxWidth.W))
231  val sqIdx = Output(new SqPtr)
232
233  // dataInvalid suggests store to load forward found forward should happen,
234  // but data is not available for now. If dataInvalid, load inst should
235  // be replayed from RS. Feedback type should be RSFeedbackType.dataInvalid
236  val dataInvalid = Input(Bool()) // Addr match, but data is not valid for now
237
238  // matchInvalid suggests in store to load forward logic, paddr cam result does
239  // to equal to vaddr cam result. If matchInvalid, a microarchitectural exception
240  // should be raised to flush SQ and committed sbuffer.
241  val matchInvalid = Input(Bool()) // resp to load_s2
242
243  // addrInvalid suggests store to load forward found forward should happen,
244  // but address (SSID) is not available for now. If addrInvalid, load inst should
245  // be replayed from RS. Feedback type should be RSFeedbackType.addrInvalid
246  val addrInvalid = Input(Bool())
247}
248
249// LoadForwardQueryIO used in load pipeline
250//
251// Difference between PipeLoadForwardQueryIO and LoadForwardQueryIO:
252// PipeIO use predecoded sqIdxMask for better forward timing
253class PipeLoadForwardQueryIO(implicit p: Parameters) extends LoadForwardQueryIO {
254  // val sqIdx = Output(new SqPtr) // for debug, should not be used in pipeline for timing reasons
255  // sqIdxMask is calcuated in earlier stage for better timing
256  val sqIdxMask = Output(UInt(StoreQueueSize.W))
257
258  // dataInvalid: addr match, but data is not valid for now
259  val dataInvalidFast = Input(Bool()) // resp to load_s1
260  // val dataInvalid = Input(Bool()) // resp to load_s2
261  val dataInvalidSqIdx = Input(new SqPtr) // resp to load_s2, sqIdx
262  val addrInvalidSqIdx = Input(new SqPtr) // resp to load_s2, sqIdx
263}
264
265// Query load queue for ld-ld violation
266//
267// Req should be send in load_s1
268// Resp will be generated 1 cycle later
269//
270// Note that query req may be !ready, as dcache is releasing a block
271// If it happens, a replay from rs is needed.
272class LoadNukeQueryReq(implicit p: Parameters) extends XSBundle { // provide lqIdx
273  val uop = new DynInst
274  // mask: load's data mask.
275  val mask = UInt((VLEN/8).W)
276
277  // paddr: load's paddr.
278  val paddr      = UInt(PAddrBits.W)
279  // dataInvalid: load data is invalid.
280  val data_valid = Bool()
281}
282
283class LoadNukeQueryResp(implicit p: Parameters) extends XSBundle {
284  // rep_frm_fetch: ld-ld violation check success, replay from fetch.
285  val rep_frm_fetch = Bool()
286}
287
288class LoadNukeQueryIO(implicit p: Parameters) extends XSBundle {
289  val req    = Decoupled(new LoadNukeQueryReq)
290  val resp   = Flipped(Valid(new LoadNukeQueryResp))
291  val revoke = Output(Bool())
292}
293
294class StoreNukeQueryIO(implicit p: Parameters) extends XSBundle {
295  //  robIdx: Requestor's (a store instruction) rob index for match logic.
296  val robIdx = new RobPtr
297
298  //  paddr: requestor's (a store instruction) physical address for match logic.
299  val paddr  = UInt(PAddrBits.W)
300
301  //  mask: requestor's (a store instruction) data width mask for match logic.
302  val mask = UInt((VLEN/8).W)
303}
304
305// Store byte valid mask write bundle
306//
307// Store byte valid mask write to SQ takes 2 cycles
308class StoreMaskBundle(implicit p: Parameters) extends XSBundle {
309  val sqIdx = new SqPtr
310  val mask = UInt((VLEN/8).W)
311}
312
313class LoadDataFromDcacheBundle(implicit p: Parameters) extends DCacheBundle {
314  // old dcache: optimize data sram read fanout
315  // val bankedDcacheData = Vec(DCacheBanks, UInt(64.W))
316  // val bank_oh = UInt(DCacheBanks.W)
317
318  // new dcache
319  val respDcacheData = UInt(VLEN.W)
320  val forwardMask = Vec(VLEN/8, Bool())
321  val forwardData = Vec(VLEN/8, UInt(8.W))
322  val uop = new DynInst // for data selection, only fwen and fuOpType are used
323  val addrOffset = UInt(4.W) // for data selection
324  // forward tilelink D channel
325  val forward_D = Bool()
326  val forwardData_D = Vec(VLEN/8, UInt(8.W))
327
328  // forward mshr data
329  val forward_mshr = Bool()
330  val forwardData_mshr = Vec(VLEN/8, UInt(8.W))
331
332  val forward_result_valid = Bool()
333
334  def dcacheData(): UInt = {
335    // old dcache
336    // val dcache_data = Mux1H(bank_oh, bankedDcacheData)
337    // new dcache
338    val dcache_data = respDcacheData
339    val use_D = forward_D && forward_result_valid
340    val use_mshr = forward_mshr && forward_result_valid
341    Mux(use_D, forwardData_D.asUInt, Mux(use_mshr, forwardData_mshr.asUInt, dcache_data))
342  }
343
344  def mergedData(): UInt = {
345    val rdataVec = VecInit((0 until VLEN / 8).map(j =>
346      Mux(forwardMask(j), forwardData(j), dcacheData()(8*(j+1)-1, 8*j))
347    ))
348    rdataVec.asUInt
349  }
350}
351
352// Load writeback data from load queue (refill)
353class LoadDataFromLQBundle(implicit p: Parameters) extends XSBundle {
354  val lqData = UInt(64.W) // load queue has merged data
355  val uop = new DynInst // for data selection, only fwen and fuOpType are used
356  val addrOffset = UInt(3.W) // for data selection
357
358  def mergedData(): UInt = {
359    lqData
360  }
361}
362
363// Bundle for load / store wait waking up
364class MemWaitUpdateReq(implicit p: Parameters) extends XSBundle {
365  val staIssue = Vec(backendParams.StaCnt, ValidIO(new MemExuInput))
366  val stdIssue = Vec(backendParams.StdCnt, ValidIO(new MemExuInput))
367}
368
369object AddPipelineReg {
370  class PipelineRegModule[T <: Data](gen: T) extends Module {
371    val io = IO(new Bundle() {
372      val in = Flipped(DecoupledIO(gen.cloneType))
373      val out = DecoupledIO(gen.cloneType)
374      val isFlush = Input(Bool())
375    })
376
377    val valid = RegInit(false.B)
378    valid.suggestName("pipeline_reg_valid")
379    when (io.out.fire()) { valid := false.B }
380    when (io.in.fire()) { valid := true.B }
381    when (io.isFlush) { valid := false.B }
382
383    io.in.ready := !valid || io.out.ready
384    io.out.bits := RegEnable(io.in.bits, io.in.fire())
385    io.out.valid := valid //&& !isFlush
386  }
387
388  def apply[T <: Data]
389  (left: DecoupledIO[T], right: DecoupledIO[T], isFlush: Bool,
390   moduleName: Option[String] = None
391  ){
392    val pipelineReg = Module(new PipelineRegModule[T](left.bits.cloneType))
393    if(moduleName.nonEmpty) pipelineReg.suggestName(moduleName.get)
394    pipelineReg.io.in <> left
395    right <> pipelineReg.io.out
396    pipelineReg.io.isFlush := isFlush
397  }
398}
399