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