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