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 18 19import chipsalliance.rocketchip.config.{Field, Parameters} 20import chisel3._ 21import chisel3.util._ 22import xiangshan.backend.exu._ 23import xiangshan.backend.dispatch.DispatchParameters 24import xiangshan.cache.DCacheParameters 25import xiangshan.cache.prefetch._ 26import xiangshan.frontend.{BasePredictor, BranchPredictionResp, FTB, FakePredictor, RAS, Tage, ITTage, Tage_SC, FauFTB} 27import xiangshan.frontend.icache.ICacheParameters 28import xiangshan.cache.mmu.{L2TLBParameters, TLBParameters} 29import freechips.rocketchip.diplomacy.AddressSet 30import system.SoCParamsKey 31import huancun._ 32import huancun.debug._ 33import coupledL2._ 34import xiangshan.mem.prefetch.{PrefetcherParams, SMSParams} 35 36import scala.math.min 37 38case object XSTileKey extends Field[Seq[XSCoreParameters]] 39 40case object XSCoreParamsKey extends Field[XSCoreParameters] 41 42case class XSCoreParameters 43( 44 HasPrefetch: Boolean = false, 45 HartId: Int = 0, 46 XLEN: Int = 64, 47 HasMExtension: Boolean = true, 48 HasCExtension: Boolean = true, 49 HasDiv: Boolean = true, 50 HasICache: Boolean = true, 51 HasDCache: Boolean = true, 52 AddrBits: Int = 64, 53 VAddrBits: Int = 39, 54 HasFPU: Boolean = true, 55 HasCustomCSRCacheOp: Boolean = true, 56 FetchWidth: Int = 8, 57 AsidLength: Int = 16, 58 EnableBPU: Boolean = true, 59 EnableBPD: Boolean = true, 60 EnableRAS: Boolean = true, 61 EnableLB: Boolean = false, 62 EnableLoop: Boolean = true, 63 EnableSC: Boolean = true, 64 EnbaleTlbDebug: Boolean = false, 65 EnableJal: Boolean = false, 66 EnableFauFTB: Boolean = true, 67 UbtbGHRLength: Int = 4, 68 // HistoryLength: Int = 512, 69 EnableGHistDiff: Boolean = true, 70 EnableCommitGHistDiff: Boolean = true, 71 UbtbSize: Int = 256, 72 FtbSize: Int = 2048, 73 RasSize: Int = 32, 74 CacheLineSize: Int = 512, 75 FtbWays: Int = 4, 76 TageTableInfos: Seq[Tuple3[Int,Int,Int]] = 77 // Sets Hist Tag 78 // Seq(( 2048, 2, 8), 79 // ( 2048, 9, 8), 80 // ( 2048, 13, 8), 81 // ( 2048, 20, 8), 82 // ( 2048, 26, 8), 83 // ( 2048, 44, 8), 84 // ( 2048, 73, 8), 85 // ( 2048, 256, 8)), 86 Seq(( 4096, 8, 8), 87 ( 4096, 13, 8), 88 ( 4096, 32, 8), 89 ( 4096, 119, 8)), 90 ITTageTableInfos: Seq[Tuple3[Int,Int,Int]] = 91 // Sets Hist Tag 92 Seq(( 256, 4, 9), 93 ( 256, 8, 9), 94 ( 512, 13, 9), 95 ( 512, 16, 9), 96 ( 512, 32, 9)), 97 SCNRows: Int = 512, 98 SCNTables: Int = 4, 99 SCCtrBits: Int = 6, 100 SCHistLens: Seq[Int] = Seq(0, 4, 10, 16), 101 numBr: Int = 2, 102 branchPredictor: Function2[BranchPredictionResp, Parameters, Tuple2[Seq[BasePredictor], BranchPredictionResp]] = 103 ((resp_in: BranchPredictionResp, p: Parameters) => { 104 val ftb = Module(new FTB()(p)) 105 val ubtb =Module(new FauFTB()(p)) 106 // val bim = Module(new BIM()(p)) 107 val tage = Module(new Tage_SC()(p)) 108 val ras = Module(new RAS()(p)) 109 val ittage = Module(new ITTage()(p)) 110 val preds = Seq(ubtb, tage, ftb, ittage, ras) 111 preds.map(_.io := DontCare) 112 113 // ubtb.io.resp_in(0) := resp_in 114 // bim.io.resp_in(0) := ubtb.io.resp 115 // btb.io.resp_in(0) := bim.io.resp 116 // tage.io.resp_in(0) := btb.io.resp 117 // loop.io.resp_in(0) := tage.io.resp 118 ubtb.io.in.bits.resp_in(0) := resp_in 119 tage.io.in.bits.resp_in(0) := ubtb.io.out 120 ftb.io.in.bits.resp_in(0) := tage.io.out 121 ittage.io.in.bits.resp_in(0) := ftb.io.out 122 ras.io.in.bits.resp_in(0) := ittage.io.out 123 124 (preds, ras.io.out) 125 }), 126 IBufSize: Int = 48, 127 DecodeWidth: Int = 6, 128 RenameWidth: Int = 6, 129 CommitWidth: Int = 6, 130 FtqSize: Int = 64, 131 EnableLoadFastWakeUp: Boolean = true, // NOTE: not supported now, make it false 132 IssQueSize: Int = 16, 133 NRPhyRegs: Int = 192, 134 VirtualLoadQueueSize: Int = 80, 135 LoadQueueRARSize: Int = 80, 136 LoadQueueRAWSize: Int = 64, // NOTE: make sure that LoadQueueRAWSize is power of 2. 137 RollbackGroupSize: Int = 8, 138 LoadQueueReplaySize: Int = 72, 139 LoadUncacheBufferSize: Int = 20, 140 LoadQueueNWriteBanks: Int = 8, // NOTE: make sure that LoadQueueRARSize/LoadQueueRAWSize is divided by LoadQueueNWriteBanks 141 StoreQueueSize: Int = 64, 142 StoreQueueNWriteBanks: Int = 8, // NOTE: make sure that StoreQueueSize is divided by StoreQueueNWriteBanks 143 StoreQueueForwardWithMask: Boolean = true, 144 VlsQueueSize: Int = 8, 145 RobSize: Int = 256, 146 dpParams: DispatchParameters = DispatchParameters( 147 IntDqSize = 16, 148 FpDqSize = 16, 149 LsDqSize = 16, 150 IntDqDeqWidth = 4, 151 FpDqDeqWidth = 4, 152 LsDqDeqWidth = 4 153 ), 154 exuParameters: ExuParameters = ExuParameters( 155 JmpCnt = 1, 156 AluCnt = 4, 157 MulCnt = 0, 158 MduCnt = 2, 159 FmacCnt = 4, 160 FmiscCnt = 2, 161 FmiscDivSqrtCnt = 0, 162 LduCnt = 2, 163 StuCnt = 2 164 ), 165 prefetcher: Option[PrefetcherParams] = Some(SMSParams()), 166 LoadPipelineWidth: Int = 2, 167 StorePipelineWidth: Int = 2, 168 VecMemSrcInWidth: Int = 2, 169 VecMemInstWbWidth: Int = 1, 170 VecMemDispatchWidth: Int = 1, 171 StoreBufferSize: Int = 16, 172 StoreBufferThreshold: Int = 7, 173 EnsbufferWidth: Int = 2, 174 UncacheBufferSize: Int = 4, 175 EnableLoadToLoadForward: Boolean = true, 176 EnableFastForward: Boolean = false, 177 EnableLdVioCheckAfterReset: Boolean = true, 178 EnableSoftPrefetchAfterReset: Boolean = true, 179 EnableCacheErrorAfterReset: Boolean = true, 180 EnableDCacheWPU: Boolean = false, 181 EnableAccurateLoadError: Boolean = true, 182 EnableUncacheWriteOutstanding: Boolean = false, 183 MMUAsidLen: Int = 16, // max is 16, 0 is not supported now 184 ReSelectLen: Int = 7, // load replay queue replay select counter len 185 itlbParameters: TLBParameters = TLBParameters( 186 name = "itlb", 187 fetchi = true, 188 useDmode = false, 189 normalNWays = 32, 190 normalReplacer = Some("plru"), 191 superNWays = 4, 192 superReplacer = Some("plru") 193 ), 194 itlbPortNum: Int = 2 + ICacheParameters().prefetchPipeNum + 1, 195 ipmpPortNum: Int = 2 + ICacheParameters().prefetchPipeNum + 1, 196 ldtlbParameters: TLBParameters = TLBParameters( 197 name = "ldtlb", 198 normalNSets = 64, 199 normalNWays = 1, 200 normalAssociative = "sa", 201 normalReplacer = Some("setplru"), 202 superNWays = 16, 203 normalAsVictim = true, 204 outReplace = false, 205 partialStaticPMP = true, 206 outsideRecvFlush = true, 207 saveLevel = true 208 ), 209 sttlbParameters: TLBParameters = TLBParameters( 210 name = "sttlb", 211 normalNSets = 64, 212 normalNWays = 1, 213 normalAssociative = "sa", 214 normalReplacer = Some("setplru"), 215 superNWays = 16, 216 normalAsVictim = true, 217 outReplace = false, 218 partialStaticPMP = true, 219 outsideRecvFlush = true, 220 saveLevel = true 221 ), 222 pftlbParameters: TLBParameters = TLBParameters( 223 name = "pftlb", 224 normalNSets = 64, 225 normalNWays = 1, 226 normalAssociative = "sa", 227 normalReplacer = Some("setplru"), 228 superNWays = 16, 229 normalAsVictim = true, 230 outReplace = false, 231 partialStaticPMP = true, 232 outsideRecvFlush = true, 233 saveLevel = true 234 ), 235 refillBothTlb: Boolean = false, 236 btlbParameters: TLBParameters = TLBParameters( 237 name = "btlb", 238 normalNSets = 1, 239 normalNWays = 64, 240 superNWays = 4, 241 ), 242 l2tlbParameters: L2TLBParameters = L2TLBParameters(), 243 NumPerfCounters: Int = 16, 244 icacheParameters: ICacheParameters = ICacheParameters( 245 tagECC = Some("parity"), 246 dataECC = Some("parity"), 247 replacer = Some("setplru"), 248 nMissEntries = 2, 249 nProbeEntries = 2, 250 nPrefetchEntries = 12, 251 nPrefBufferEntries = 64, 252 hasPrefetch = true, 253 ), 254 dcacheParametersOpt: Option[DCacheParameters] = Some(DCacheParameters( 255 tagECC = Some("secded"), 256 dataECC = Some("secded"), 257 replacer = Some("setplru"), 258 nMissEntries = 16, 259 nProbeEntries = 8, 260 nReleaseEntries = 18 261 )), 262 L2CacheParamsOpt: Option[L2Param] = Some(L2Param( 263 name = "l2", 264 ways = 8, 265 sets = 1024, // default 512KB L2 266 prefetch = Some(coupledL2.prefetch.PrefetchReceiverParams()) 267 )), 268 L2NBanks: Int = 1, 269 usePTWRepeater: Boolean = false, 270 softTLB: Boolean = false, // dpi-c l1tlb debug only 271 softPTW: Boolean = false, // dpi-c l2tlb debug only 272 softPTWDelay: Int = 1 273){ 274 val allHistLens = SCHistLens ++ ITTageTableInfos.map(_._2) ++ TageTableInfos.map(_._2) :+ UbtbGHRLength 275 val HistoryLength = allHistLens.max + numBr * FtqSize + 9 // 256 for the predictor configs now 276 277 val loadExuConfigs = Seq.fill(exuParameters.LduCnt)(LdExeUnitCfg) 278 val storeExuConfigs = Seq.fill(exuParameters.StuCnt)(StaExeUnitCfg) ++ Seq.fill(exuParameters.StuCnt)(StdExeUnitCfg) 279 280 val intExuConfigs = (Seq.fill(exuParameters.AluCnt)(AluExeUnitCfg) ++ 281 Seq.fill(exuParameters.MduCnt)(MulDivExeUnitCfg) :+ JumpCSRExeUnitCfg) 282 283 val fpExuConfigs = 284 Seq.fill(exuParameters.FmacCnt)(FmacExeUnitCfg) ++ 285 Seq.fill(exuParameters.FmiscCnt)(FmiscExeUnitCfg) 286 287 val exuConfigs: Seq[ExuConfig] = intExuConfigs ++ fpExuConfigs ++ loadExuConfigs ++ storeExuConfigs 288} 289 290case object DebugOptionsKey extends Field[DebugOptions] 291 292case class DebugOptions 293( 294 FPGAPlatform: Boolean = false, 295 EnableDifftest: Boolean = false, 296 AlwaysBasicDiff: Boolean = true, 297 EnableDebug: Boolean = false, 298 EnablePerfDebug: Boolean = true, 299 UseDRAMSim: Boolean = false, 300 EnableConstantin: Boolean = false, 301 EnableChiselDB: Boolean = false, 302 AlwaysBasicDB: Boolean = true, 303) 304 305trait HasXSParameter { 306 307 implicit val p: Parameters 308 309 val PAddrBits = p(SoCParamsKey).PAddrBits // PAddrBits is Phyical Memory addr bits 310 311 val coreParams = p(XSCoreParamsKey) 312 val env = p(DebugOptionsKey) 313 314 val XLEN = coreParams.XLEN 315 val minFLen = 32 316 val fLen = 64 317 def xLen = XLEN 318 319 val HasMExtension = coreParams.HasMExtension 320 val HasCExtension = coreParams.HasCExtension 321 val HasDiv = coreParams.HasDiv 322 val HasIcache = coreParams.HasICache 323 val HasDcache = coreParams.HasDCache 324 val AddrBits = coreParams.AddrBits // AddrBits is used in some cases 325 val VAddrBits = coreParams.VAddrBits // VAddrBits is Virtual Memory addr bits 326 val AsidLength = coreParams.AsidLength 327 val ReSelectLen = coreParams.ReSelectLen 328 val AddrBytes = AddrBits / 8 // unused 329 val DataBits = XLEN 330 val DataBytes = DataBits / 8 331 val HasFPU = coreParams.HasFPU 332 val HasCustomCSRCacheOp = coreParams.HasCustomCSRCacheOp 333 val FetchWidth = coreParams.FetchWidth 334 val PredictWidth = FetchWidth * (if (HasCExtension) 2 else 1) 335 val EnableBPU = coreParams.EnableBPU 336 val EnableBPD = coreParams.EnableBPD // enable backing predictor(like Tage) in BPUStage3 337 val EnableRAS = coreParams.EnableRAS 338 val EnableLB = coreParams.EnableLB 339 val EnableLoop = coreParams.EnableLoop 340 val EnableSC = coreParams.EnableSC 341 val EnbaleTlbDebug = coreParams.EnbaleTlbDebug 342 val HistoryLength = coreParams.HistoryLength 343 val EnableGHistDiff = coreParams.EnableGHistDiff 344 val EnableCommitGHistDiff = coreParams.EnableCommitGHistDiff 345 val UbtbGHRLength = coreParams.UbtbGHRLength 346 val UbtbSize = coreParams.UbtbSize 347 val EnableFauFTB = coreParams.EnableFauFTB 348 val FtbSize = coreParams.FtbSize 349 val FtbWays = coreParams.FtbWays 350 val RasSize = coreParams.RasSize 351 352 def getBPDComponents(resp_in: BranchPredictionResp, p: Parameters) = { 353 coreParams.branchPredictor(resp_in, p) 354 } 355 val numBr = coreParams.numBr 356 val TageTableInfos = coreParams.TageTableInfos 357 val TageBanks = coreParams.numBr 358 val SCNRows = coreParams.SCNRows 359 val SCCtrBits = coreParams.SCCtrBits 360 val SCHistLens = coreParams.SCHistLens 361 val SCNTables = coreParams.SCNTables 362 363 val SCTableInfos = Seq.fill(SCNTables)((SCNRows, SCCtrBits)) zip SCHistLens map { 364 case ((n, cb), h) => (n, cb, h) 365 } 366 val ITTageTableInfos = coreParams.ITTageTableInfos 367 type FoldedHistoryInfo = Tuple2[Int, Int] 368 val foldedGHistInfos = 369 (TageTableInfos.map{ case (nRows, h, t) => 370 if (h > 0) 371 Set((h, min(log2Ceil(nRows/numBr), h)), (h, min(h, t)), (h, min(h, t-1))) 372 else 373 Set[FoldedHistoryInfo]() 374 }.reduce(_++_).toSet ++ 375 SCTableInfos.map{ case (nRows, _, h) => 376 if (h > 0) 377 Set((h, min(log2Ceil(nRows/TageBanks), h))) 378 else 379 Set[FoldedHistoryInfo]() 380 }.reduce(_++_).toSet ++ 381 ITTageTableInfos.map{ case (nRows, h, t) => 382 if (h > 0) 383 Set((h, min(log2Ceil(nRows), h)), (h, min(h, t)), (h, min(h, t-1))) 384 else 385 Set[FoldedHistoryInfo]() 386 }.reduce(_++_) ++ 387 Set[FoldedHistoryInfo]((UbtbGHRLength, log2Ceil(UbtbSize))) 388 ).toList 389 390 391 392 val CacheLineSize = coreParams.CacheLineSize 393 val CacheLineHalfWord = CacheLineSize / 16 394 val ExtHistoryLength = HistoryLength + 64 395 val IBufSize = coreParams.IBufSize 396 val DecodeWidth = coreParams.DecodeWidth 397 val RenameWidth = coreParams.RenameWidth 398 val CommitWidth = coreParams.CommitWidth 399 val FtqSize = coreParams.FtqSize 400 val IssQueSize = coreParams.IssQueSize 401 val EnableLoadFastWakeUp = coreParams.EnableLoadFastWakeUp 402 val NRPhyRegs = coreParams.NRPhyRegs 403 val PhyRegIdxWidth = log2Up(NRPhyRegs) 404 val RobSize = coreParams.RobSize 405 val IntRefCounterWidth = log2Ceil(RobSize) 406 val VirtualLoadQueueSize = coreParams.VirtualLoadQueueSize 407 val LoadQueueRARSize = coreParams.LoadQueueRARSize 408 val LoadQueueRAWSize = coreParams.LoadQueueRAWSize 409 val RollbackGroupSize = coreParams.RollbackGroupSize 410 val LoadQueueReplaySize = coreParams.LoadQueueReplaySize 411 val LoadUncacheBufferSize = coreParams.LoadUncacheBufferSize 412 val LoadQueueNWriteBanks = coreParams.LoadQueueNWriteBanks 413 val StoreQueueSize = coreParams.StoreQueueSize 414 val StoreQueueNWriteBanks = coreParams.StoreQueueNWriteBanks 415 val StoreQueueForwardWithMask = coreParams.StoreQueueForwardWithMask 416 val VlsQueueSize = coreParams.VlsQueueSize 417 val dpParams = coreParams.dpParams 418 val exuParameters = coreParams.exuParameters 419 val NRMemReadPorts = exuParameters.LduCnt + 2 * exuParameters.StuCnt 420 val NRIntReadPorts = 2 * exuParameters.AluCnt + NRMemReadPorts 421 val NRIntWritePorts = exuParameters.AluCnt + exuParameters.MduCnt + exuParameters.LduCnt 422 val NRFpReadPorts = 3 * exuParameters.FmacCnt + exuParameters.StuCnt 423 val NRFpWritePorts = exuParameters.FpExuCnt + exuParameters.LduCnt 424 val LoadPipelineWidth = coreParams.LoadPipelineWidth 425 val StorePipelineWidth = coreParams.StorePipelineWidth 426 val VecMemSrcInWidth = coreParams.VecMemSrcInWidth 427 val VecMemInstWbWidth = coreParams.VecMemInstWbWidth 428 val VecMemDispatchWidth = coreParams.VecMemDispatchWidth 429 val StoreBufferSize = coreParams.StoreBufferSize 430 val StoreBufferThreshold = coreParams.StoreBufferThreshold 431 val EnsbufferWidth = coreParams.EnsbufferWidth 432 val UncacheBufferSize = coreParams.UncacheBufferSize 433 val EnableLoadToLoadForward = coreParams.EnableLoadToLoadForward 434 val EnableFastForward = coreParams.EnableFastForward 435 val EnableLdVioCheckAfterReset = coreParams.EnableLdVioCheckAfterReset 436 val EnableSoftPrefetchAfterReset = coreParams.EnableSoftPrefetchAfterReset 437 val EnableCacheErrorAfterReset = coreParams.EnableCacheErrorAfterReset 438 val EnableDCacheWPU = coreParams.EnableDCacheWPU 439 val EnableAccurateLoadError = coreParams.EnableAccurateLoadError 440 val EnableUncacheWriteOutstanding = coreParams.EnableUncacheWriteOutstanding 441 val asidLen = coreParams.MMUAsidLen 442 val BTLBWidth = coreParams.LoadPipelineWidth + coreParams.StorePipelineWidth 443 val refillBothTlb = coreParams.refillBothTlb 444 val itlbParams = coreParams.itlbParameters 445 val ldtlbParams = coreParams.ldtlbParameters 446 val sttlbParams = coreParams.sttlbParameters 447 val pftlbParams = coreParams.pftlbParameters 448 val btlbParams = coreParams.btlbParameters 449 val l2tlbParams = coreParams.l2tlbParameters 450 val NumPerfCounters = coreParams.NumPerfCounters 451 452 val NumRs = (exuParameters.JmpCnt+1)/2 + (exuParameters.AluCnt+1)/2 + (exuParameters.MulCnt+1)/2 + 453 (exuParameters.MduCnt+1)/2 + (exuParameters.FmacCnt+1)/2 + + (exuParameters.FmiscCnt+1)/2 + 454 (exuParameters.FmiscDivSqrtCnt+1)/2 + (exuParameters.LduCnt+1)/2 + 455 (exuParameters.StuCnt+1)/2 + (exuParameters.StuCnt+1)/2 456 457 val instBytes = if (HasCExtension) 2 else 4 458 val instOffsetBits = log2Ceil(instBytes) 459 460 val icacheParameters = coreParams.icacheParameters 461 val dcacheParameters = coreParams.dcacheParametersOpt.getOrElse(DCacheParameters()) 462 463 // dcache block cacheline when lr for LRSCCycles - LRSCBackOff cycles 464 // for constrained LR/SC loop 465 val LRSCCycles = 64 466 // for lr storm 467 val LRSCBackOff = 8 468 469 // cache hierarchy configurations 470 val l1BusDataWidth = 256 471 472 // load violation predict 473 val ResetTimeMax2Pow = 20 //1078576 474 val ResetTimeMin2Pow = 10 //1024 475 // wait table parameters 476 val WaitTableSize = 1024 477 val MemPredPCWidth = log2Up(WaitTableSize) 478 val LWTUse2BitCounter = true 479 // store set parameters 480 val SSITSize = WaitTableSize 481 val LFSTSize = 32 482 val SSIDWidth = log2Up(LFSTSize) 483 val LFSTWidth = 4 484 val StoreSetEnable = true // LWT will be disabled if SS is enabled 485 val LFSTEnable = false 486 val loadExuConfigs = coreParams.loadExuConfigs 487 val storeExuConfigs = coreParams.storeExuConfigs 488 489 val intExuConfigs = coreParams.intExuConfigs 490 491 val fpExuConfigs = coreParams.fpExuConfigs 492 493 val exuConfigs = coreParams.exuConfigs 494 495 val PCntIncrStep: Int = 6 496 val numPCntHc: Int = 25 497 val numPCntPtw: Int = 19 498 499 val numCSRPCntFrontend = 8 500 val numCSRPCntCtrl = 8 501 val numCSRPCntLsu = 8 502 val numCSRPCntHc = 5 503} 504