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