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