xref: /XiangShan/src/main/scala/xiangshan/mem/MemCommon.scala (revision a760aeb0b1b64540d1f49146f82ddd6e87aa1e5e)
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 xiangshan.backend.rob.RobPtr
26import xiangshan.cache._
27import xiangshan.backend.fu.FenceToSbuffer
28
29object genWmask {
30  def apply(addr: UInt, sizeEncode: UInt): UInt = {
31    (LookupTree(sizeEncode, List(
32      "b00".U -> 0x1.U, //0001 << addr(2:0)
33      "b01".U -> 0x3.U, //0011
34      "b10".U -> 0xf.U, //1111
35      "b11".U -> 0xff.U //11111111
36    )) << addr(2, 0)).asUInt()
37  }
38}
39
40object genWdata {
41  def apply(data: UInt, sizeEncode: UInt): UInt = {
42    LookupTree(sizeEncode, List(
43      "b00".U -> Fill(8, data(7, 0)),
44      "b01".U -> Fill(4, data(15, 0)),
45      "b10".U -> Fill(2, data(31, 0)),
46      "b11".U -> data
47    ))
48  }
49}
50
51class LsPipelineBundle(implicit p: Parameters) extends XSBundleWithMicroOp {
52  val vaddr = UInt(VAddrBits.W)
53  val paddr = UInt(PAddrBits.W)
54  // val func = UInt(6.W)
55  val mask = UInt(8.W)
56  val data = UInt((XLEN+1).W)
57  val wlineflag = Bool() // store write the whole cache line
58
59  val miss = Bool()
60  val tlbMiss = Bool()
61  val ptwBack = Bool()
62  val mmio = Bool()
63  val rsIdx = UInt(log2Up(IssQueSize).W)
64
65  val forwardMask = Vec(8, Bool())
66  val forwardData = Vec(8, UInt(8.W))
67
68  //softprefetch
69  val isSoftPrefetch = Bool()
70
71  // For debug usage
72  val isFirstIssue = Bool()
73
74  // For load replay
75  val isLoadReplay = Bool()
76}
77
78class LqWriteBundle(implicit p: Parameters) extends LsPipelineBundle {
79  // queue entry data, except flag bits, will be updated if writeQueue is true,
80  // valid bit in LqWriteBundle will be ignored
81  val lq_data_wen_dup = Vec(6, Bool()) // dirty reg dup
82
83  def fromLsPipelineBundle(input: LsPipelineBundle) = {
84    vaddr := input.vaddr
85    paddr := input.paddr
86    mask := input.mask
87    data := input.data
88    uop := input.uop
89    wlineflag := input.wlineflag
90    miss := input.miss
91    tlbMiss := input.tlbMiss
92    ptwBack := input.ptwBack
93    mmio := input.mmio
94    rsIdx := input.rsIdx
95    forwardMask := input.forwardMask
96    forwardData := input.forwardData
97    isSoftPrefetch := input.isSoftPrefetch
98    isFirstIssue := input.isFirstIssue
99    isLoadReplay := input.isLoadReplay
100
101    lq_data_wen_dup := DontCare
102  }
103}
104
105class LoadForwardQueryIO(implicit p: Parameters) extends XSBundleWithMicroOp {
106  val vaddr = Output(UInt(VAddrBits.W))
107  val paddr = Output(UInt(PAddrBits.W))
108  val mask = Output(UInt(8.W))
109  override val uop = Output(new MicroOp) // for replay
110  val pc = Output(UInt(VAddrBits.W)) //for debug
111  val valid = Output(Bool())
112
113  val forwardMaskFast = Input(Vec(8, Bool())) // resp to load_s1
114  val forwardMask = Input(Vec(8, Bool())) // resp to load_s2
115  val forwardData = Input(Vec(8, UInt(8.W))) // resp to load_s2
116
117  // val lqIdx = Output(UInt(LoadQueueIdxWidth.W))
118  val sqIdx = Output(new SqPtr)
119
120  // dataInvalid suggests store to load forward found forward should happen,
121  // but data is not available for now. If dataInvalid, load inst should
122  // be replayed from RS. Feedback type should be RSFeedbackType.dataInvalid
123  val dataInvalid = Input(Bool()) // Addr match, but data is not valid for now
124
125  // matchInvalid suggests in store to load forward logic, paddr cam result does
126  // to equal to vaddr cam result. If matchInvalid, a microarchitectural exception
127  // should be raised to flush SQ and committed sbuffer.
128  val matchInvalid = Input(Bool()) // resp to load_s2
129}
130
131// LoadForwardQueryIO used in load pipeline
132//
133// Difference between PipeLoadForwardQueryIO and LoadForwardQueryIO:
134// PipeIO use predecoded sqIdxMask for better forward timing
135class PipeLoadForwardQueryIO(implicit p: Parameters) extends LoadForwardQueryIO {
136  // val sqIdx = Output(new SqPtr) // for debug, should not be used in pipeline for timing reasons
137  // sqIdxMask is calcuated in earlier stage for better timing
138  val sqIdxMask = Output(UInt(StoreQueueSize.W))
139
140  // dataInvalid: addr match, but data is not valid for now
141  val dataInvalidFast = Input(Bool()) // resp to load_s1
142  // val dataInvalid = Input(Bool()) // resp to load_s2
143  val dataInvalidSqIdx = Input(UInt(log2Up(StoreQueueSize).W)) // resp to load_s2, sqIdx value
144}
145
146// Query load queue for ld-ld violation
147//
148// Req should be send in load_s1
149// Resp will be generated 1 cycle later
150//
151// Note that query req may be !ready, as dcache is releasing a block
152// If it happens, a replay from rs is needed.
153
154class LoadViolationQueryReq(implicit p: Parameters) extends XSBundleWithMicroOp { // provide lqIdx
155  val paddr = UInt(PAddrBits.W)
156}
157
158class LoadViolationQueryResp(implicit p: Parameters) extends XSBundle {
159  val have_violation = Bool()
160}
161
162class LoadViolationQueryIO(implicit p: Parameters) extends XSBundle {
163  val req = Decoupled(new LoadViolationQueryReq)
164  val resp = Flipped(Valid(new LoadViolationQueryResp))
165}
166
167// Store byte valid mask write bundle
168//
169// Store byte valid mask write to SQ takes 2 cycles
170class StoreMaskBundle(implicit p: Parameters) extends XSBundle {
171  val sqIdx = new SqPtr
172  val mask = UInt(8.W)
173}
174
175class LoadDataFromDcacheBundle(implicit p: Parameters) extends DCacheBundle {
176  val bankedDcacheData = Vec(DCacheBanks, UInt(64.W))
177  val bank_oh = UInt(DCacheBanks.W)
178  val forwardMask = Vec(8, Bool())
179  val forwardData = Vec(8, UInt(8.W))
180  val uop = new MicroOp // for data selection, only fwen and fuOpType are used
181  val addrOffset = UInt(3.W) // for data selection
182
183  // val dcacheData = UInt(64.W)
184  def dcacheData(): UInt = {
185    Mux1H(bank_oh, bankedDcacheData)
186  }
187
188  def mergedData(): UInt = {
189    val rdataVec = VecInit((0 until XLEN / 8).map(j =>
190      Mux(forwardMask(j), forwardData(j), dcacheData()(8*(j+1)-1, 8*j))
191    ))
192    rdataVec.asUInt
193  }
194}
195
196// Load writeback data from load queue (refill)
197class LoadDataFromLQBundle(implicit p: Parameters) extends XSBundle {
198  val lqData = UInt(64.W) // load queue has merged data
199  val uop = new MicroOp // for data selection, only fwen and fuOpType are used
200  val addrOffset = UInt(3.W) // for data selection
201
202  def mergedData(): UInt = {
203    lqData
204  }
205}
206
207// Bundle for load / store wait waking up
208class MemWaitUpdateReq(implicit p: Parameters) extends XSBundle {
209  val staIssue = Vec(exuParameters.StuCnt, ValidIO(new ExuInput))
210  val stdIssue = Vec(exuParameters.StuCnt, ValidIO(new ExuInput))
211}
212
213object AddPipelineReg {
214  class PipelineRegModule[T <: Data](gen: T) extends Module {
215    val io = IO(new Bundle() {
216      val in = Flipped(DecoupledIO(gen.cloneType))
217      val out = DecoupledIO(gen.cloneType)
218      val isFlush = Input(Bool())
219    })
220
221    val valid = RegInit(false.B)
222    valid.suggestName("pipeline_reg_valid")
223    when (io.out.fire()) { valid := false.B }
224    when (io.in.fire()) { valid := true.B }
225    when (io.isFlush) { valid := false.B }
226
227    io.in.ready := !valid || io.out.ready
228    io.out.bits := RegEnable(io.in.bits, io.in.fire())
229    io.out.valid := valid //&& !isFlush
230  }
231
232  def apply[T <: Data]
233  (left: DecoupledIO[T], right: DecoupledIO[T], isFlush: Bool,
234   moduleName: Option[String] = None
235  ){
236    val pipelineReg = Module(new PipelineRegModule[T](left.bits.cloneType))
237    if(moduleName.nonEmpty) pipelineReg.suggestName(moduleName.get)
238    pipelineReg.io.in <> left
239    right <> pipelineReg.io.out
240    pipelineReg.io.isFlush := isFlush
241  }
242}
243