xref: /XiangShan/src/main/scala/xiangshan/mem/MemCommon.scala (revision eb163ef08fc5ac1da1f32d948699bd6de053e444)
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
75class LqWriteBundle(implicit p: Parameters) extends LsPipelineBundle {
76  // queue entry data, except flag bits, will be updated if writeQueue is true,
77  // valid bit in LqWriteBundle will be ignored
78  val writeQueueData = Bool()
79
80  def fromLsPipelineBundle(input: LsPipelineBundle) = {
81    vaddr := input.vaddr
82    paddr := input.paddr
83    mask := input.mask
84    data := input.data
85    uop := input.uop
86    wlineflag := input.wlineflag
87    miss := input.miss
88    tlbMiss := input.tlbMiss
89    ptwBack := input.ptwBack
90    mmio := input.mmio
91    rsIdx := input.rsIdx
92    forwardMask := input.forwardMask
93    forwardData := input.forwardData
94    isSoftPrefetch := input.isSoftPrefetch
95    isFirstIssue := input.isFirstIssue
96
97    writeQueueData := false.B
98  }
99}
100
101class LoadForwardQueryIO(implicit p: Parameters) extends XSBundleWithMicroOp {
102  val vaddr = Output(UInt(VAddrBits.W))
103  val paddr = Output(UInt(PAddrBits.W))
104  val mask = Output(UInt(8.W))
105  override val uop = Output(new MicroOp) // for replay
106  val pc = Output(UInt(VAddrBits.W)) //for debug
107  val valid = Output(Bool())
108
109  val forwardMaskFast = Input(Vec(8, Bool())) // resp to load_s1
110  val forwardMask = Input(Vec(8, Bool())) // resp to load_s2
111  val forwardData = Input(Vec(8, UInt(8.W))) // resp to load_s2
112
113  // val lqIdx = Output(UInt(LoadQueueIdxWidth.W))
114  val sqIdx = Output(new SqPtr)
115
116  // dataInvalid suggests store to load forward found forward should happen,
117  // but data is not available for now. If dataInvalid, load inst should
118  // be replayed from RS. Feedback type should be RSFeedbackType.dataInvalid
119  val dataInvalid = Input(Bool()) // Addr match, but data is not valid for now
120
121  // matchInvalid suggests in store to load forward logic, paddr cam result does
122  // to equal to vaddr cam result. If matchInvalid, a microarchitectural exception
123  // should be raised to flush SQ and committed sbuffer.
124  val matchInvalid = Input(Bool()) // resp to load_s2
125}
126
127// LoadForwardQueryIO used in load pipeline
128//
129// Difference between PipeLoadForwardQueryIO and LoadForwardQueryIO:
130// PipeIO use predecoded sqIdxMask for better forward timing
131class PipeLoadForwardQueryIO(implicit p: Parameters) extends LoadForwardQueryIO {
132  // val sqIdx = Output(new SqPtr) // for debug, should not be used in pipeline for timing reasons
133  // sqIdxMask is calcuated in earlier stage for better timing
134  val sqIdxMask = Output(UInt(StoreQueueSize.W))
135
136  // dataInvalid: addr match, but data is not valid for now
137  val dataInvalidFast = Input(Bool()) // resp to load_s1
138  // val dataInvalid = Input(Bool()) // resp to load_s2
139  val dataInvalidSqIdx = Input(UInt(log2Up(StoreQueueSize).W)) // resp to load_s2, sqIdx value
140}
141
142// Query load queue for ld-ld violation
143//
144// Req should be send in load_s1
145// Resp will be generated 1 cycle later
146//
147// Note that query req may be !ready, as dcache is releasing a block
148// If it happens, a replay from rs is needed.
149
150class LoadViolationQueryReq(implicit p: Parameters) extends XSBundleWithMicroOp { // provide lqIdx
151  val paddr = UInt(PAddrBits.W)
152}
153
154class LoadViolationQueryResp(implicit p: Parameters) extends XSBundle {
155  val have_violation = Bool()
156}
157
158class LoadViolationQueryIO(implicit p: Parameters) extends XSBundle {
159  val req = Decoupled(new LoadViolationQueryReq)
160  val resp = Flipped(Valid(new LoadViolationQueryResp))
161}
162
163// Bundle for load / store wait waking up
164class MemWaitUpdateReq(implicit p: Parameters) extends XSBundle {
165  val staIssue = Vec(exuParameters.StuCnt, ValidIO(new ExuInput))
166  val stdIssue = Vec(exuParameters.StuCnt, ValidIO(new ExuInput))
167}
168
169object AddPipelineReg {
170  class PipelineRegModule[T <: Data](gen: T) extends Module {
171    val io = IO(new Bundle() {
172      val in = Flipped(DecoupledIO(gen.cloneType))
173      val out = DecoupledIO(gen.cloneType)
174      val isFlush = Input(Bool())
175    })
176
177    val valid = RegInit(false.B)
178    valid.suggestName("pipeline_reg_valid")
179    when (io.out.fire()) { valid := false.B }
180    when (io.in.fire()) { valid := true.B }
181    when (io.isFlush) { valid := false.B }
182
183    io.in.ready := !valid || io.out.ready
184    io.out.bits := RegEnable(io.in.bits, io.in.fire())
185    io.out.valid := valid //&& !isFlush
186  }
187
188  def apply[T <: Data]
189  (left: DecoupledIO[T], right: DecoupledIO[T], isFlush: Bool,
190   moduleName: Option[String] = None
191  ){
192    val pipelineReg = Module(new PipelineRegModule[T](left.bits.cloneType))
193    if(moduleName.nonEmpty) pipelineReg.suggestName(moduleName.get)
194    pipelineReg.io.in <> left
195    right <> pipelineReg.io.out
196    pipelineReg.io.isFlush := isFlush
197  }
198}
199