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