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.Bundles.{DynInst, MemExuInput} 27import xiangshan.backend.rob.RobPtr 28import xiangshan.cache._ 29import xiangshan.backend.fu.FenceToSbuffer 30import xiangshan.cache.wpu.ReplayCarry 31 32object genWmask { 33 def apply(addr: UInt, sizeEncode: UInt): UInt = { 34 (LookupTree(sizeEncode, List( 35 "b00".U -> 0x1.U, //0001 << addr(2:0) 36 "b01".U -> 0x3.U, //0011 37 "b10".U -> 0xf.U, //1111 38 "b11".U -> 0xff.U //11111111 39 )) << addr(2, 0)).asUInt() 40 } 41} 42 43object genVWmask { 44 def apply(addr: UInt, sizeEncode: UInt): UInt = { 45 (LookupTree(sizeEncode, List( 46 "b00".U -> 0x1.U, //0001 << addr(2:0) 47 "b01".U -> 0x3.U, //0011 48 "b10".U -> 0xf.U, //1111 49 "b11".U -> 0xff.U //11111111 50 )) << addr(3, 0)).asUInt() 51 } 52} 53 54object genWdata { 55 def apply(data: UInt, sizeEncode: UInt): UInt = { 56 LookupTree(sizeEncode, List( 57 "b00".U -> Fill(16, data(7, 0)), 58 "b01".U -> Fill(8, data(15, 0)), 59 "b10".U -> Fill(4, data(31, 0)), 60 "b11".U -> Fill(2, data(63,0)) 61 )) 62 } 63} 64 65object shiftDataToLow { 66 def apply(addr: UInt,data : UInt): UInt = { 67 Mux(addr(3), (data >> 64).asUInt,data) 68 } 69} 70object shiftMaskToLow { 71 def apply(addr: UInt,mask: UInt): UInt = { 72 Mux(addr(3),(mask >> 8).asUInt,mask) 73 } 74} 75 76class LsPipelineBundle(implicit val p: Parameters) extends Bundle with HasXSParameter with HasDCacheParameters{ 77 val uop = new DynInst 78 val vaddr = UInt(VAddrBits.W) 79 val paddr = UInt(PAddrBits.W) 80 // val func = UInt(6.W) 81 val mask = UInt((VLEN/8).W) 82 val data = UInt((VLEN+1).W) 83 val wlineflag = Bool() // store write the whole cache line 84 85 val miss = Bool() 86 val tlbMiss = Bool() 87 val ptwBack = Bool() 88 val mmio = Bool() 89 val atomic = Bool() 90 val rsIdx = UInt(log2Up(MemIQSizeMax).W) 91 92 val forwardMask = Vec(VLEN/8, Bool()) 93 val forwardData = Vec(VLEN/8, UInt(8.W)) 94 95 // prefetch 96 val isPrefetch = Bool() 97 val isHWPrefetch = Bool() 98 def isSWPrefetch = isPrefetch && !isHWPrefetch 99 100 // For debug usage 101 val isFirstIssue = Bool() 102 val hasROBEntry = Bool() 103 104 // For load replay 105 val isLoadReplay = Bool() 106 val isFastPath = Bool() 107 val isFastReplay = Bool() 108 val replayCarry = new ReplayCarry(nWays) 109 110 // For dcache miss load 111 val mshrid = UInt(log2Up(cfg.nMissEntries).W) 112 val handledByMSHR = Bool() 113 val replacementUpdated = Bool() 114 115 val forward_tlDchannel = Bool() 116 val dcacheRequireReplay = Bool() 117 val delayedLoadError = Bool() 118 val lateKill = Bool() 119 val feedbacked = Bool() 120 121 // loadQueueReplay index. 122 val schedIndex = UInt(log2Up(LoadQueueReplaySize).W) 123} 124 125class LdPrefetchTrainBundle(implicit p: Parameters) extends LsPipelineBundle { 126 val meta_prefetch = Bool() 127 val meta_access = Bool() 128 129 def fromLsPipelineBundle(input: LsPipelineBundle) = { 130 vaddr := input.vaddr 131 paddr := input.paddr 132 mask := input.mask 133 data := input.data 134 uop := input.uop 135 wlineflag := input.wlineflag 136 miss := input.miss 137 tlbMiss := input.tlbMiss 138 ptwBack := input.ptwBack 139 mmio := input.mmio 140 rsIdx := input.rsIdx 141 forwardMask := input.forwardMask 142 forwardData := input.forwardData 143 isPrefetch := input.isPrefetch 144 isHWPrefetch := input.isHWPrefetch 145 isFirstIssue := input.isFirstIssue 146 hasROBEntry := input.hasROBEntry 147 dcacheRequireReplay := input.dcacheRequireReplay 148 schedIndex := input.schedIndex 149 150 meta_prefetch := DontCare 151 meta_access := DontCare 152 forward_tlDchannel := DontCare 153 mshrid := DontCare 154 replayCarry := DontCare 155 atomic := DontCare 156 isLoadReplay := DontCare 157 isFastPath := DontCare 158 isFastReplay := DontCare 159 handledByMSHR := DontCare 160 replacementUpdated := DontCare 161 delayedLoadError := DontCare 162 lateKill := DontCare 163 feedbacked := DontCare 164 } 165} 166 167class LqWriteBundle(implicit p: Parameters) extends LsPipelineBundle { 168 // load inst replay informations 169 val rep_info = new LoadToLsqReplayIO 170 // queue entry data, except flag bits, will be updated if writeQueue is true, 171 // valid bit in LqWriteBundle will be ignored 172 val data_wen_dup = Vec(6, Bool()) // dirty reg dup 173 174 175 def fromLsPipelineBundle(input: LsPipelineBundle) = { 176 vaddr := input.vaddr 177 paddr := input.paddr 178 mask := input.mask 179 data := input.data 180 uop := input.uop 181 wlineflag := input.wlineflag 182 miss := input.miss 183 tlbMiss := input.tlbMiss 184 ptwBack := input.ptwBack 185 mmio := input.mmio 186 atomic := input.atomic 187 rsIdx := input.rsIdx 188 forwardMask := input.forwardMask 189 forwardData := input.forwardData 190 isPrefetch := input.isPrefetch 191 isHWPrefetch := input.isHWPrefetch 192 isFirstIssue := input.isFirstIssue 193 hasROBEntry := input.hasROBEntry 194 isLoadReplay := input.isLoadReplay 195 isFastPath := input.isFastPath 196 isFastReplay := input.isFastReplay 197 mshrid := input.mshrid 198 forward_tlDchannel := input.forward_tlDchannel 199 replayCarry := input.replayCarry 200 dcacheRequireReplay := input.dcacheRequireReplay 201 schedIndex := input.schedIndex 202 handledByMSHR := input.handledByMSHR 203 replacementUpdated := input.replacementUpdated 204 delayedLoadError := input.delayedLoadError 205 lateKill := input.lateKill 206 feedbacked := input.feedbacked 207 208 rep_info := DontCare 209 data_wen_dup := DontCare 210 } 211} 212 213class LoadForwardQueryIO(implicit p: Parameters) extends XSBundle { 214 val vaddr = Output(UInt(VAddrBits.W)) 215 val paddr = Output(UInt(PAddrBits.W)) 216 val mask = Output(UInt((VLEN/8).W)) 217 val uop = Output(new DynInst) // for replay 218 val pc = Output(UInt(VAddrBits.W)) //for debug 219 val valid = Output(Bool()) 220 221 val forwardMaskFast = Input(Vec((VLEN/8), Bool())) // resp to load_s1 222 val forwardMask = Input(Vec((VLEN/8), Bool())) // resp to load_s2 223 val forwardData = Input(Vec((VLEN/8), UInt(8.W))) // resp to load_s2 224 225 // val lqIdx = Output(UInt(LoadQueueIdxWidth.W)) 226 val sqIdx = Output(new SqPtr) 227 228 // dataInvalid suggests store to load forward found forward should happen, 229 // but data is not available for now. If dataInvalid, load inst should 230 // be replayed from RS. Feedback type should be RSFeedbackType.dataInvalid 231 val dataInvalid = Input(Bool()) // Addr match, but data is not valid for now 232 233 // matchInvalid suggests in store to load forward logic, paddr cam result does 234 // to equal to vaddr cam result. If matchInvalid, a microarchitectural exception 235 // should be raised to flush SQ and committed sbuffer. 236 val matchInvalid = Input(Bool()) // resp to load_s2 237 238 // addrInvalid suggests store to load forward found forward should happen, 239 // but address (SSID) is not available for now. If addrInvalid, load inst should 240 // be replayed from RS. Feedback type should be RSFeedbackType.addrInvalid 241 val addrInvalid = Input(Bool()) 242} 243 244// LoadForwardQueryIO used in load pipeline 245// 246// Difference between PipeLoadForwardQueryIO and LoadForwardQueryIO: 247// PipeIO use predecoded sqIdxMask for better forward timing 248class PipeLoadForwardQueryIO(implicit p: Parameters) extends LoadForwardQueryIO { 249 // val sqIdx = Output(new SqPtr) // for debug, should not be used in pipeline for timing reasons 250 // sqIdxMask is calcuated in earlier stage for better timing 251 val sqIdxMask = Output(UInt(StoreQueueSize.W)) 252 253 // dataInvalid: addr match, but data is not valid for now 254 val dataInvalidFast = Input(Bool()) // resp to load_s1 255 // val dataInvalid = Input(Bool()) // resp to load_s2 256 val dataInvalidSqIdx = Input(new SqPtr) // resp to load_s2, sqIdx 257 val addrInvalidSqIdx = Input(new SqPtr) // resp to load_s2, sqIdx 258} 259 260// Query load queue for ld-ld violation 261// 262// Req should be send in load_s1 263// Resp will be generated 1 cycle later 264// 265// Note that query req may be !ready, as dcache is releasing a block 266// If it happens, a replay from rs is needed. 267class LoadNukeQueryReq(implicit p: Parameters) extends XSBundle { // provide lqIdx 268 val uop = new DynInst 269 // mask: load's data mask. 270 val mask = UInt((VLEN/8).W) 271 272 // paddr: load's paddr. 273 val paddr = UInt(PAddrBits.W) 274 // dataInvalid: load data is invalid. 275 val data_valid = Bool() 276} 277 278class LoadNukeQueryResp(implicit p: Parameters) extends XSBundle { 279 // rep_frm_fetch: ld-ld violation check success, replay from fetch. 280 val rep_frm_fetch = Bool() 281} 282 283class LoadNukeQueryIO(implicit p: Parameters) extends XSBundle { 284 val req = Decoupled(new LoadNukeQueryReq) 285 val resp = Flipped(Valid(new LoadNukeQueryResp)) 286 val revoke = Output(Bool()) 287} 288 289class StoreNukeQueryIO(implicit p: Parameters) extends XSBundle { 290 // robIdx: Requestor's (a store instruction) rob index for match logic. 291 val robIdx = new RobPtr 292 293 // paddr: requestor's (a store instruction) physical address for match logic. 294 val paddr = UInt(PAddrBits.W) 295 296 // mask: requestor's (a store instruction) data width mask for match logic. 297 val mask = UInt((VLEN/8).W) 298} 299 300// Store byte valid mask write bundle 301// 302// Store byte valid mask write to SQ takes 2 cycles 303class StoreMaskBundle(implicit p: Parameters) extends XSBundle { 304 val sqIdx = new SqPtr 305 val mask = UInt((VLEN/8).W) 306} 307 308class LoadDataFromDcacheBundle(implicit p: Parameters) extends DCacheBundle { 309 // old dcache: optimize data sram read fanout 310 // val bankedDcacheData = Vec(DCacheBanks, UInt(64.W)) 311 // val bank_oh = UInt(DCacheBanks.W) 312 313 // new dcache 314 val respDcacheData = UInt(VLEN.W) 315 val forwardMask = Vec(VLEN/8, Bool()) 316 val forwardData = Vec(VLEN/8, UInt(8.W)) 317 val uop = new DynInst // for data selection, only fwen and fuOpType are used 318 val addrOffset = UInt(4.W) // for data selection 319 // forward tilelink D channel 320 val forward_D = Bool() 321 val forwardData_D = Vec(VLEN/8, UInt(8.W)) 322 323 // forward mshr data 324 val forward_mshr = Bool() 325 val forwardData_mshr = Vec(VLEN/8, UInt(8.W)) 326 327 val forward_result_valid = Bool() 328 329 def dcacheData(): UInt = { 330 // old dcache 331 // val dcache_data = Mux1H(bank_oh, bankedDcacheData) 332 // new dcache 333 val dcache_data = respDcacheData 334 val use_D = forward_D && forward_result_valid 335 val use_mshr = forward_mshr && forward_result_valid 336 Mux(use_D, forwardData_D.asUInt, Mux(use_mshr, forwardData_mshr.asUInt, dcache_data)) 337 } 338 339 def mergedData(): UInt = { 340 val rdataVec = VecInit((0 until VLEN / 8).map(j => 341 Mux(forwardMask(j), forwardData(j), dcacheData()(8*(j+1)-1, 8*j)) 342 )) 343 rdataVec.asUInt 344 } 345} 346 347// Load writeback data from load queue (refill) 348class LoadDataFromLQBundle(implicit p: Parameters) extends XSBundle { 349 val lqData = UInt(64.W) // load queue has merged data 350 val uop = new DynInst // for data selection, only fwen and fuOpType are used 351 val addrOffset = UInt(3.W) // for data selection 352 353 def mergedData(): UInt = { 354 lqData 355 } 356} 357 358// Bundle for load / store wait waking up 359class MemWaitUpdateReq(implicit p: Parameters) extends XSBundle { 360 val staIssue = Vec(backendParams.StaCnt, ValidIO(new MemExuInput)) 361 val stdIssue = Vec(backendParams.StdCnt, ValidIO(new MemExuInput)) 362} 363 364object AddPipelineReg { 365 class PipelineRegModule[T <: Data](gen: T) extends Module { 366 val io = IO(new Bundle() { 367 val in = Flipped(DecoupledIO(gen.cloneType)) 368 val out = DecoupledIO(gen.cloneType) 369 val isFlush = Input(Bool()) 370 }) 371 372 val valid = RegInit(false.B) 373 valid.suggestName("pipeline_reg_valid") 374 when (io.out.fire()) { valid := false.B } 375 when (io.in.fire()) { valid := true.B } 376 when (io.isFlush) { valid := false.B } 377 378 io.in.ready := !valid || io.out.ready 379 io.out.bits := RegEnable(io.in.bits, io.in.fire()) 380 io.out.valid := valid //&& !isFlush 381 } 382 383 def apply[T <: Data] 384 (left: DecoupledIO[T], right: DecoupledIO[T], isFlush: Bool, 385 moduleName: Option[String] = None 386 ){ 387 val pipelineReg = Module(new PipelineRegModule[T](left.bits.cloneType)) 388 if(moduleName.nonEmpty) pipelineReg.suggestName(moduleName.get) 389 pipelineReg.io.in <> left 390 right <> pipelineReg.io.out 391 pipelineReg.io.isFlush := isFlush 392 } 393} 394