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