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